xref: /onnv-gate/usr/src/uts/common/io/iwk/iwk2.c (revision 11878:ac93462db6d7)
16169Shx147065 /*
2*11878SVenu.Iyer@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
36169Shx147065  * Use is subject to license terms.
46169Shx147065  */
56169Shx147065 
66169Shx147065 /*
76169Shx147065  * Copyright (c) 2007, Intel Corporation
86169Shx147065  * All rights reserved.
96169Shx147065  */
106169Shx147065 
116169Shx147065 /*
126169Shx147065  * Copyright (c) 2006
136169Shx147065  * Copyright (c) 2007
146169Shx147065  *	Damien Bergamini <damien.bergamini@free.fr>
156169Shx147065  *
166169Shx147065  * Permission to use, copy, modify, and distribute this software for any
176169Shx147065  * purpose with or without fee is hereby granted, provided that the above
186169Shx147065  * copyright notice and this permission notice appear in all copies.
196169Shx147065  *
206169Shx147065  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
216169Shx147065  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
226169Shx147065  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
236169Shx147065  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
246169Shx147065  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
256169Shx147065  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
266169Shx147065  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
276169Shx147065  */
286169Shx147065 
296169Shx147065 /*
306169Shx147065  * Driver for Intel PRO/Wireless 4965AGN(kedron) 802.11 network adapters.
316169Shx147065  */
326169Shx147065 
336169Shx147065 #include <sys/types.h>
346169Shx147065 #include <sys/byteorder.h>
356169Shx147065 #include <sys/conf.h>
366169Shx147065 #include <sys/cmn_err.h>
376169Shx147065 #include <sys/stat.h>
386169Shx147065 #include <sys/ddi.h>
396169Shx147065 #include <sys/sunddi.h>
406169Shx147065 #include <sys/strsubr.h>
416169Shx147065 #include <sys/ethernet.h>
426169Shx147065 #include <inet/common.h>
436169Shx147065 #include <inet/nd.h>
446169Shx147065 #include <inet/mi.h>
456169Shx147065 #include <sys/note.h>
466169Shx147065 #include <sys/stream.h>
476169Shx147065 #include <sys/strsun.h>
486169Shx147065 #include <sys/modctl.h>
496169Shx147065 #include <sys/devops.h>
506169Shx147065 #include <sys/dlpi.h>
518275SEric Cheng #include <sys/mac_provider.h>
526169Shx147065 #include <sys/mac_wifi.h>
536169Shx147065 #include <sys/net80211.h>
546169Shx147065 #include <sys/net80211_proto.h>
556169Shx147065 #include <sys/varargs.h>
566169Shx147065 #include <sys/policy.h>
576169Shx147065 #include <sys/pci.h>
586169Shx147065 
597569SXinghua.Wen@Sun.COM #include "iwk_calibration.h"
606169Shx147065 #include "iwk_hw.h"
616169Shx147065 #include "iwk_eeprom.h"
626169Shx147065 #include "iwk2_var.h"
636169Shx147065 #include <inet/wifi_ioctl.h>
646169Shx147065 
656169Shx147065 #ifdef DEBUG
666169Shx147065 #define	IWK_DEBUG_80211		(1 << 0)
676169Shx147065 #define	IWK_DEBUG_CMD		(1 << 1)
686169Shx147065 #define	IWK_DEBUG_DMA		(1 << 2)
696169Shx147065 #define	IWK_DEBUG_EEPROM	(1 << 3)
706169Shx147065 #define	IWK_DEBUG_FW		(1 << 4)
716169Shx147065 #define	IWK_DEBUG_HW		(1 << 5)
726169Shx147065 #define	IWK_DEBUG_INTR		(1 << 6)
736169Shx147065 #define	IWK_DEBUG_MRR		(1 << 7)
746169Shx147065 #define	IWK_DEBUG_PIO		(1 << 8)
756169Shx147065 #define	IWK_DEBUG_RX		(1 << 9)
766169Shx147065 #define	IWK_DEBUG_SCAN		(1 << 10)
776169Shx147065 #define	IWK_DEBUG_TX		(1 << 11)
786169Shx147065 #define	IWK_DEBUG_RATECTL	(1 << 12)
796169Shx147065 #define	IWK_DEBUG_RADIO		(1 << 13)
806249Shx147065 #define	IWK_DEBUG_RESUME	(1 << 14)
817569SXinghua.Wen@Sun.COM #define	IWK_DEBUG_CALIBRATION	(1 << 15)
826169Shx147065 uint32_t iwk_dbg_flags = 0;
836169Shx147065 #define	IWK_DBG(x) \
846169Shx147065 	iwk_dbg x
856169Shx147065 #else
866169Shx147065 #define	IWK_DBG(x)
876169Shx147065 #endif
886169Shx147065 
896169Shx147065 static void	*iwk_soft_state_p = NULL;
906169Shx147065 static uint8_t iwk_fw_bin [] = {
916169Shx147065 #include "fw-iw/iw4965.ucode.hex"
926169Shx147065 };
936169Shx147065 
946169Shx147065 /* DMA attributes for a shared page */
956169Shx147065 static ddi_dma_attr_t sh_dma_attr = {
966169Shx147065 	DMA_ATTR_V0,	/* version of this structure */
976169Shx147065 	0,		/* lowest usable address */
986169Shx147065 	0xffffffffU,	/* highest usable address */
996169Shx147065 	0xffffffffU,	/* maximum DMAable byte count */
1006169Shx147065 	0x1000,		/* alignment in bytes */
1016169Shx147065 	0x1000,		/* burst sizes (any?) */
1026169Shx147065 	1,		/* minimum transfer */
1036169Shx147065 	0xffffffffU,	/* maximum transfer */
1046169Shx147065 	0xffffffffU,	/* maximum segment length */
1056169Shx147065 	1,		/* maximum number of segments */
1066169Shx147065 	1,		/* granularity */
1076169Shx147065 	0,		/* flags (reserved) */
1086169Shx147065 };
1096169Shx147065 
1106169Shx147065 /* DMA attributes for a keep warm DRAM descriptor */
1116169Shx147065 static ddi_dma_attr_t kw_dma_attr = {
1126169Shx147065 	DMA_ATTR_V0,	/* version of this structure */
1136169Shx147065 	0,		/* lowest usable address */
1146169Shx147065 	0xffffffffU,	/* highest usable address */
1156169Shx147065 	0xffffffffU,	/* maximum DMAable byte count */
1166169Shx147065 	0x1000,		/* alignment in bytes */
1176169Shx147065 	0x1000,		/* burst sizes (any?) */
1186169Shx147065 	1,		/* minimum transfer */
1196169Shx147065 	0xffffffffU,	/* maximum transfer */
1206169Shx147065 	0xffffffffU,	/* maximum segment length */
1216169Shx147065 	1,		/* maximum number of segments */
1226169Shx147065 	1,		/* granularity */
1236169Shx147065 	0,		/* flags (reserved) */
1246169Shx147065 };
1256169Shx147065 
1266169Shx147065 /* DMA attributes for a ring descriptor */
1276169Shx147065 static ddi_dma_attr_t ring_desc_dma_attr = {
1286169Shx147065 	DMA_ATTR_V0,	/* version of this structure */
1296169Shx147065 	0,		/* lowest usable address */
1306169Shx147065 	0xffffffffU,	/* highest usable address */
1316169Shx147065 	0xffffffffU,	/* maximum DMAable byte count */
1326169Shx147065 	0x100,		/* alignment in bytes */
1336169Shx147065 	0x100,		/* burst sizes (any?) */
1346169Shx147065 	1,		/* minimum transfer */
1356169Shx147065 	0xffffffffU,	/* maximum transfer */
1366169Shx147065 	0xffffffffU,	/* maximum segment length */
1376169Shx147065 	1,		/* maximum number of segments */
1386169Shx147065 	1,		/* granularity */
1396169Shx147065 	0,		/* flags (reserved) */
1406169Shx147065 };
1416169Shx147065 
1426169Shx147065 /* DMA attributes for a cmd */
1436169Shx147065 static ddi_dma_attr_t cmd_dma_attr = {
1446169Shx147065 	DMA_ATTR_V0,	/* version of this structure */
1456169Shx147065 	0,		/* lowest usable address */
1466169Shx147065 	0xffffffffU,	/* highest usable address */
1476169Shx147065 	0xffffffffU,	/* maximum DMAable byte count */
1486169Shx147065 	4,		/* alignment in bytes */
1496169Shx147065 	0x100,		/* burst sizes (any?) */
1506169Shx147065 	1,		/* minimum transfer */
1516169Shx147065 	0xffffffffU,	/* maximum transfer */
1526169Shx147065 	0xffffffffU,	/* maximum segment length */
1536169Shx147065 	1,		/* maximum number of segments */
1546169Shx147065 	1,		/* granularity */
1556169Shx147065 	0,		/* flags (reserved) */
1566169Shx147065 };
1576169Shx147065 
1586169Shx147065 /* DMA attributes for a rx buffer */
1596169Shx147065 static ddi_dma_attr_t rx_buffer_dma_attr = {
1606169Shx147065 	DMA_ATTR_V0,	/* version of this structure */
1616169Shx147065 	0,		/* lowest usable address */
1626169Shx147065 	0xffffffffU,	/* highest usable address */
1636169Shx147065 	0xffffffffU,	/* maximum DMAable byte count */
1646169Shx147065 	0x100,		/* alignment in bytes */
1656169Shx147065 	0x100,		/* burst sizes (any?) */
1666169Shx147065 	1,		/* minimum transfer */
1676169Shx147065 	0xffffffffU,	/* maximum transfer */
1686169Shx147065 	0xffffffffU,	/* maximum segment length */
1696169Shx147065 	1,		/* maximum number of segments */
1706169Shx147065 	1,		/* granularity */
1716169Shx147065 	0,		/* flags (reserved) */
1726169Shx147065 };
1736169Shx147065 
1746169Shx147065 /*
1756169Shx147065  * DMA attributes for a tx buffer.
1766169Shx147065  * the maximum number of segments is 4 for the hardware.
1776169Shx147065  * now all the wifi drivers put the whole frame in a single
1786169Shx147065  * descriptor, so we define the maximum  number of segments 1,
1796169Shx147065  * just the same as the rx_buffer. we consider leverage the HW
1806169Shx147065  * ability in the future, that is why we don't define rx and tx
1816169Shx147065  * buffer_dma_attr as the same.
1826169Shx147065  */
1836169Shx147065 static ddi_dma_attr_t tx_buffer_dma_attr = {
1846169Shx147065 	DMA_ATTR_V0,	/* version of this structure */
1856169Shx147065 	0,		/* lowest usable address */
1866169Shx147065 	0xffffffffU,	/* highest usable address */
1876169Shx147065 	0xffffffffU,	/* maximum DMAable byte count */
1886169Shx147065 	4,		/* alignment in bytes */
1896169Shx147065 	0x100,		/* burst sizes (any?) */
1906169Shx147065 	1,		/* minimum transfer */
1916169Shx147065 	0xffffffffU,	/* maximum transfer */
1926169Shx147065 	0xffffffffU,	/* maximum segment length */
1936169Shx147065 	1,		/* maximum number of segments */
1946169Shx147065 	1,		/* granularity */
1956169Shx147065 	0,		/* flags (reserved) */
1966169Shx147065 };
1976169Shx147065 
1986169Shx147065 /* DMA attributes for text and data part in the firmware */
1996169Shx147065 static ddi_dma_attr_t fw_dma_attr = {
2006169Shx147065 	DMA_ATTR_V0,	/* version of this structure */
2016169Shx147065 	0,		/* lowest usable address */
2026169Shx147065 	0xffffffffU,	/* highest usable address */
2036169Shx147065 	0x7fffffff,	/* maximum DMAable byte count */
2046169Shx147065 	0x10,		/* alignment in bytes */
2056169Shx147065 	0x100,		/* burst sizes (any?) */
2066169Shx147065 	1,		/* minimum transfer */
2076169Shx147065 	0xffffffffU,	/* maximum transfer */
2086169Shx147065 	0xffffffffU,	/* maximum segment length */
2096169Shx147065 	1,		/* maximum number of segments */
2106169Shx147065 	1,		/* granularity */
2116169Shx147065 	0,		/* flags (reserved) */
2126169Shx147065 };
2136169Shx147065 
2146169Shx147065 
2156169Shx147065 /* regs access attributes */
2166169Shx147065 static ddi_device_acc_attr_t iwk_reg_accattr = {
2176169Shx147065 	DDI_DEVICE_ATTR_V0,
2186169Shx147065 	DDI_STRUCTURE_LE_ACC,
2196169Shx147065 	DDI_STRICTORDER_ACC,
2206169Shx147065 	DDI_DEFAULT_ACC
2216169Shx147065 };
2226169Shx147065 
2239327SFei.Feng@Sun.COM /* DMA access attributes for Descriptor */
2249327SFei.Feng@Sun.COM static ddi_device_acc_attr_t iwk_dma_descattr = {
2259327SFei.Feng@Sun.COM 	DDI_DEVICE_ATTR_V0,
2269327SFei.Feng@Sun.COM 	DDI_STRUCTURE_LE_ACC,
2279327SFei.Feng@Sun.COM 	DDI_STRICTORDER_ACC,
2289327SFei.Feng@Sun.COM 	DDI_DEFAULT_ACC
2299327SFei.Feng@Sun.COM };
2309327SFei.Feng@Sun.COM 
2316169Shx147065 /* DMA access attributes */
2326169Shx147065 static ddi_device_acc_attr_t iwk_dma_accattr = {
2336169Shx147065 	DDI_DEVICE_ATTR_V0,
2346169Shx147065 	DDI_NEVERSWAP_ACC,
2356169Shx147065 	DDI_STRICTORDER_ACC,
2366169Shx147065 	DDI_DEFAULT_ACC
2376169Shx147065 };
2386169Shx147065 
2396169Shx147065 static int	iwk_ring_init(iwk_sc_t *);
2406169Shx147065 static void	iwk_ring_free(iwk_sc_t *);
2416169Shx147065 static int	iwk_alloc_shared(iwk_sc_t *);
2426169Shx147065 static void	iwk_free_shared(iwk_sc_t *);
2436169Shx147065 static int	iwk_alloc_kw(iwk_sc_t *);
2446169Shx147065 static void	iwk_free_kw(iwk_sc_t *);
2456169Shx147065 static int	iwk_alloc_fw_dma(iwk_sc_t *);
2466169Shx147065 static void	iwk_free_fw_dma(iwk_sc_t *);
2476169Shx147065 static int	iwk_alloc_rx_ring(iwk_sc_t *);
2486169Shx147065 static void	iwk_reset_rx_ring(iwk_sc_t *);
2496169Shx147065 static void	iwk_free_rx_ring(iwk_sc_t *);
2506169Shx147065 static int	iwk_alloc_tx_ring(iwk_sc_t *, iwk_tx_ring_t *,
2516169Shx147065     int, int);
2526169Shx147065 static void	iwk_reset_tx_ring(iwk_sc_t *, iwk_tx_ring_t *);
2536169Shx147065 static void	iwk_free_tx_ring(iwk_sc_t *, iwk_tx_ring_t *);
2546169Shx147065 
2556249Shx147065 static ieee80211_node_t *iwk_node_alloc(ieee80211com_t *);
2566249Shx147065 static void	iwk_node_free(ieee80211_node_t *);
2576169Shx147065 static int	iwk_newstate(ieee80211com_t *, enum ieee80211_state, int);
2586169Shx147065 static int	iwk_key_set(ieee80211com_t *, const struct ieee80211_key *,
2596169Shx147065     const uint8_t mac[IEEE80211_ADDR_LEN]);
2606169Shx147065 static void	iwk_mac_access_enter(iwk_sc_t *);
2616169Shx147065 static void	iwk_mac_access_exit(iwk_sc_t *);
2626169Shx147065 static uint32_t	iwk_reg_read(iwk_sc_t *, uint32_t);
2636169Shx147065 static void	iwk_reg_write(iwk_sc_t *, uint32_t, uint32_t);
2646169Shx147065 static void	iwk_reg_write_region_4(iwk_sc_t *, uint32_t,
2656169Shx147065 		    uint32_t *, int);
2666169Shx147065 static int	iwk_load_firmware(iwk_sc_t *);
2676169Shx147065 static void	iwk_rx_intr(iwk_sc_t *, iwk_rx_desc_t *,
2686169Shx147065 		    iwk_rx_data_t *);
2696169Shx147065 static void	iwk_tx_intr(iwk_sc_t *, iwk_rx_desc_t *,
2706169Shx147065 		    iwk_rx_data_t *);
2716169Shx147065 static void	iwk_cmd_intr(iwk_sc_t *, iwk_rx_desc_t *);
2727569SXinghua.Wen@Sun.COM static uint_t   iwk_intr(caddr_t, caddr_t);
2736169Shx147065 static int	iwk_eep_load(iwk_sc_t *sc);
2746169Shx147065 static void	iwk_get_mac_from_eep(iwk_sc_t *sc);
2756169Shx147065 static int	iwk_eep_sem_down(iwk_sc_t *sc);
2766169Shx147065 static void	iwk_eep_sem_up(iwk_sc_t *sc);
2777569SXinghua.Wen@Sun.COM static uint_t   iwk_rx_softintr(caddr_t, caddr_t);
2786169Shx147065 static uint8_t	iwk_rate_to_plcp(int);
2796169Shx147065 static int	iwk_cmd(iwk_sc_t *, int, const void *, int, int);
2806169Shx147065 static void	iwk_set_led(iwk_sc_t *, uint8_t, uint8_t, uint8_t);
2816169Shx147065 static int	iwk_hw_set_before_auth(iwk_sc_t *);
2826169Shx147065 static int	iwk_scan(iwk_sc_t *);
2836169Shx147065 static int	iwk_config(iwk_sc_t *);
2846169Shx147065 static void	iwk_stop_master(iwk_sc_t *);
2856169Shx147065 static int	iwk_power_up(iwk_sc_t *);
2866169Shx147065 static int	iwk_preinit(iwk_sc_t *);
2876169Shx147065 static int	iwk_init(iwk_sc_t *);
2886169Shx147065 static void	iwk_stop(iwk_sc_t *);
2896249Shx147065 static void	iwk_amrr_init(iwk_amrr_t *);
2906249Shx147065 static void	iwk_amrr_timeout(iwk_sc_t *);
2916249Shx147065 static void	iwk_amrr_ratectl(void *, ieee80211_node_t *);
2927569SXinghua.Wen@Sun.COM static int32_t	iwk_curr_tempera(iwk_sc_t *sc);
2937569SXinghua.Wen@Sun.COM static int	iwk_tx_power_calibration(iwk_sc_t *sc);
2947569SXinghua.Wen@Sun.COM static inline int	iwk_is_24G_band(iwk_sc_t *sc);
2957569SXinghua.Wen@Sun.COM static inline int	iwk_is_fat_channel(iwk_sc_t *sc);
2967569SXinghua.Wen@Sun.COM static int	iwk_txpower_grp(uint16_t channel);
2977569SXinghua.Wen@Sun.COM static struct	iwk_eep_channel *iwk_get_eep_channel(iwk_sc_t *sc,
2987569SXinghua.Wen@Sun.COM     uint16_t channel,
2997569SXinghua.Wen@Sun.COM     int is_24G, int is_fat, int is_hi_chan);
3007569SXinghua.Wen@Sun.COM static int32_t	iwk_band_number(iwk_sc_t *sc, uint16_t channel);
3017569SXinghua.Wen@Sun.COM static int	iwk_division(int32_t num, int32_t denom, int32_t *res);
3027569SXinghua.Wen@Sun.COM static int32_t	iwk_interpolate_value(int32_t x, int32_t x1, int32_t y1,
3037569SXinghua.Wen@Sun.COM     int32_t x2, int32_t y2);
3047569SXinghua.Wen@Sun.COM static int	iwk_channel_interpolate(iwk_sc_t *sc, uint16_t channel,
3057569SXinghua.Wen@Sun.COM     struct iwk_eep_calib_channel_info *chan_info);
3067569SXinghua.Wen@Sun.COM static int32_t	iwk_voltage_compensation(int32_t eep_voltage,
3077569SXinghua.Wen@Sun.COM     int32_t curr_voltage);
3087569SXinghua.Wen@Sun.COM static int32_t	iwk_min_power_index(int32_t rate_pow_idx, int32_t is_24G);
3097569SXinghua.Wen@Sun.COM static int	iwk_txpower_table_cmd_init(iwk_sc_t *sc,
3107569SXinghua.Wen@Sun.COM     struct iwk_tx_power_db *tp_db);
3117569SXinghua.Wen@Sun.COM static void	iwk_statistics_notify(iwk_sc_t *sc, iwk_rx_desc_t *desc);
3127569SXinghua.Wen@Sun.COM static int	iwk_is_associated(iwk_sc_t *sc);
3137569SXinghua.Wen@Sun.COM static int	iwk_rxgain_diff_init(iwk_sc_t *sc);
3147569SXinghua.Wen@Sun.COM static int	iwk_rxgain_diff(iwk_sc_t *sc);
3157569SXinghua.Wen@Sun.COM static int	iwk_rx_sens_init(iwk_sc_t *sc);
3167569SXinghua.Wen@Sun.COM static int	iwk_rx_sens(iwk_sc_t *sc);
3177569SXinghua.Wen@Sun.COM static int	iwk_cck_sens(iwk_sc_t *sc, uint32_t actual_rx_time);
3187569SXinghua.Wen@Sun.COM static int	iwk_ofdm_sens(iwk_sc_t *sc, uint32_t actual_rx_time);
3198594SFei.Feng@Sun.COM static void	iwk_recv_mgmt(struct ieee80211com *ic, mblk_t *mp,
3208594SFei.Feng@Sun.COM     struct ieee80211_node *in, int subtype, int rssi, uint32_t rstamp);
3217569SXinghua.Wen@Sun.COM 
3227569SXinghua.Wen@Sun.COM static void	iwk_write_event_log(iwk_sc_t *);
3237569SXinghua.Wen@Sun.COM static void	iwk_write_error_log(iwk_sc_t *);
3247569SXinghua.Wen@Sun.COM 
3257569SXinghua.Wen@Sun.COM static int	iwk_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
3267569SXinghua.Wen@Sun.COM static int	iwk_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
3277891SPengcheng.Chen@Sun.COM static int	iwk_quiesce(dev_info_t *dip);
3286169Shx147065 
3296169Shx147065 /*
3306169Shx147065  * GLD specific operations
3316169Shx147065  */
3326169Shx147065 static int	iwk_m_stat(void *arg, uint_t stat, uint64_t *val);
3336169Shx147065 static int	iwk_m_start(void *arg);
3346169Shx147065 static void	iwk_m_stop(void *arg);
3356169Shx147065 static int	iwk_m_unicst(void *arg, const uint8_t *macaddr);
3366169Shx147065 static int	iwk_m_multicst(void *arg, boolean_t add, const uint8_t *m);
3376169Shx147065 static int	iwk_m_promisc(void *arg, boolean_t on);
3387569SXinghua.Wen@Sun.COM static mblk_t 	*iwk_m_tx(void *arg, mblk_t *mp);
3396169Shx147065 static void	iwk_m_ioctl(void *arg, queue_t *wq, mblk_t *mp);
3407663SSowmini.Varadhan@Sun.COM static int	iwk_m_setprop(void *arg, const char *pr_name,
3417663SSowmini.Varadhan@Sun.COM 	mac_prop_id_t wldp_pr_name, uint_t wldp_length, const void *wldp_buf);
3427663SSowmini.Varadhan@Sun.COM static int	iwk_m_getprop(void *arg, const char *pr_name,
343*11878SVenu.Iyer@Sun.COM 	mac_prop_id_t wldp_pr_name, uint_t wldp_length, void *wldp_buf);
344*11878SVenu.Iyer@Sun.COM static void	iwk_m_propinfo(void *arg, const char *pr_name,
345*11878SVenu.Iyer@Sun.COM     mac_prop_id_t wldp_pr_num, mac_prop_info_handle_t mph);
3466169Shx147065 static void	iwk_destroy_locks(iwk_sc_t *sc);
3476169Shx147065 static int	iwk_send(ieee80211com_t *ic, mblk_t *mp, uint8_t type);
3486169Shx147065 static void	iwk_thread(iwk_sc_t *sc);
3498727SPengcheng.Chen@Sun.COM static void	iwk_watchdog(void *arg);
3508594SFei.Feng@Sun.COM static int	iwk_run_state_config_ibss(ieee80211com_t *ic);
3518594SFei.Feng@Sun.COM static int	iwk_run_state_config_sta(ieee80211com_t *ic);
3528902SPengcheng.Chen@Sun.COM static int	iwk_fast_recover(iwk_sc_t *sc);
3538594SFei.Feng@Sun.COM static int	iwk_start_tx_beacon(ieee80211com_t *ic);
3548594SFei.Feng@Sun.COM static int	iwk_clean_add_node_ibss(struct ieee80211com *ic,
3558594SFei.Feng@Sun.COM     uint8_t addr[IEEE80211_ADDR_LEN], uint8_t *index2);
3566169Shx147065 
3576169Shx147065 /*
3586169Shx147065  * Supported rates for 802.11b/g modes (in 500Kbps unit).
3596169Shx147065  * 11a and 11n support will be added later.
3606169Shx147065  */
3616169Shx147065 static const struct ieee80211_rateset iwk_rateset_11b =
3626169Shx147065 	{ 4, { 2, 4, 11, 22 } };
3636169Shx147065 
3646169Shx147065 static const struct ieee80211_rateset iwk_rateset_11g =
3656169Shx147065 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
3666169Shx147065 
3676169Shx147065 /*
3686169Shx147065  * For mfthread only
3696169Shx147065  */
3706169Shx147065 extern pri_t minclsyspri;
3716169Shx147065 
3726169Shx147065 #define	DRV_NAME_4965	"iwk"
3736169Shx147065 
3746169Shx147065 /*
3756169Shx147065  * Module Loading Data & Entry Points
3766169Shx147065  */
3776169Shx147065 DDI_DEFINE_STREAM_OPS(iwk_devops, nulldev, nulldev, iwk_attach,
3787891SPengcheng.Chen@Sun.COM     iwk_detach, nodev, NULL, D_MP, NULL, iwk_quiesce);
3796169Shx147065 
3806169Shx147065 static struct modldrv iwk_modldrv = {
3816169Shx147065 	&mod_driverops,
3826169Shx147065 	"Intel(R) 4965AGN driver(N)",
3836169Shx147065 	&iwk_devops
3846169Shx147065 };
3856169Shx147065 
3866169Shx147065 static struct modlinkage iwk_modlinkage = {
3876169Shx147065 	MODREV_1,
3886169Shx147065 	&iwk_modldrv,
3896169Shx147065 	NULL
3906169Shx147065 };
3916169Shx147065 
3926169Shx147065 int
_init(void)3936169Shx147065 _init(void)
3946169Shx147065 {
3956169Shx147065 	int	status;
3966169Shx147065 
3976169Shx147065 	status = ddi_soft_state_init(&iwk_soft_state_p,
3986169Shx147065 	    sizeof (iwk_sc_t), 1);
3996169Shx147065 	if (status != DDI_SUCCESS)
4006169Shx147065 		return (status);
4016169Shx147065 
4026169Shx147065 	mac_init_ops(&iwk_devops, DRV_NAME_4965);
4036169Shx147065 	status = mod_install(&iwk_modlinkage);
4046169Shx147065 	if (status != DDI_SUCCESS) {
4056169Shx147065 		mac_fini_ops(&iwk_devops);
4066169Shx147065 		ddi_soft_state_fini(&iwk_soft_state_p);
4076169Shx147065 	}
4086169Shx147065 
4096169Shx147065 	return (status);
4106169Shx147065 }
4116169Shx147065 
4126169Shx147065 int
_fini(void)4136169Shx147065 _fini(void)
4146169Shx147065 {
4156169Shx147065 	int status;
4166169Shx147065 
4176169Shx147065 	status = mod_remove(&iwk_modlinkage);
4186169Shx147065 	if (status == DDI_SUCCESS) {
4196169Shx147065 		mac_fini_ops(&iwk_devops);
4206169Shx147065 		ddi_soft_state_fini(&iwk_soft_state_p);
4216169Shx147065 	}
4226169Shx147065 
4236169Shx147065 	return (status);
4246169Shx147065 }
4256169Shx147065 
4266169Shx147065 int
_info(struct modinfo * mip)4276169Shx147065 _info(struct modinfo *mip)
4286169Shx147065 {
4296169Shx147065 	return (mod_info(&iwk_modlinkage, mip));
4306169Shx147065 }
4316169Shx147065 
4326169Shx147065 /*
4336169Shx147065  * Mac Call Back entries
4346169Shx147065  */
4356169Shx147065 mac_callbacks_t	iwk_m_callbacks = {
436*11878SVenu.Iyer@Sun.COM 	MC_IOCTL | MC_SETPROP | MC_GETPROP | MC_PROPINFO,
4376169Shx147065 	iwk_m_stat,
4386169Shx147065 	iwk_m_start,
4396169Shx147065 	iwk_m_stop,
4406169Shx147065 	iwk_m_promisc,
4416169Shx147065 	iwk_m_multicst,
4426169Shx147065 	iwk_m_unicst,
4436169Shx147065 	iwk_m_tx,
444*11878SVenu.Iyer@Sun.COM 	NULL,
4457663SSowmini.Varadhan@Sun.COM 	iwk_m_ioctl,
4467663SSowmini.Varadhan@Sun.COM 	NULL,
4477663SSowmini.Varadhan@Sun.COM 	NULL,
4487663SSowmini.Varadhan@Sun.COM 	NULL,
4497663SSowmini.Varadhan@Sun.COM 	iwk_m_setprop,
450*11878SVenu.Iyer@Sun.COM 	iwk_m_getprop,
451*11878SVenu.Iyer@Sun.COM 	iwk_m_propinfo
4526169Shx147065 };
4536169Shx147065 
4546169Shx147065 #ifdef DEBUG
4556169Shx147065 void
iwk_dbg(uint32_t flags,const char * fmt,...)4566169Shx147065 iwk_dbg(uint32_t flags, const char *fmt, ...)
4576169Shx147065 {
4586169Shx147065 	va_list	ap;
4596169Shx147065 
4606169Shx147065 	if (flags & iwk_dbg_flags) {
4616169Shx147065 		va_start(ap, fmt);
4626169Shx147065 		vcmn_err(CE_NOTE, fmt, ap);
4636169Shx147065 		va_end(ap);
4646169Shx147065 	}
4656169Shx147065 }
4666169Shx147065 #endif
4676169Shx147065 
4686169Shx147065 /*
4696169Shx147065  * device operations
4706169Shx147065  */
4716169Shx147065 int
iwk_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4726169Shx147065 iwk_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4736169Shx147065 {
4746169Shx147065 	iwk_sc_t		*sc;
4756169Shx147065 	ieee80211com_t	*ic;
4766169Shx147065 	int			instance, err, i;
4776169Shx147065 	char			strbuf[32];
4786169Shx147065 	wifi_data_t		wd = { 0 };
4796169Shx147065 	mac_register_t		*macp;
4806169Shx147065 
4817569SXinghua.Wen@Sun.COM 	int			intr_type;
4827569SXinghua.Wen@Sun.COM 	int			intr_count;
4837569SXinghua.Wen@Sun.COM 	int			intr_actual;
4847569SXinghua.Wen@Sun.COM 
4856795Shx147065 	switch (cmd) {
4866795Shx147065 	case DDI_ATTACH:
4876795Shx147065 		break;
4886795Shx147065 	case DDI_RESUME:
4896795Shx147065 		sc = ddi_get_soft_state(iwk_soft_state_p,
4906795Shx147065 		    ddi_get_instance(dip));
4916795Shx147065 		ASSERT(sc != NULL);
4928796SPengcheng.Chen@Sun.COM 
4938796SPengcheng.Chen@Sun.COM 		mutex_enter(&sc->sc_glock);
4948796SPengcheng.Chen@Sun.COM 		sc->sc_flags &= ~IWK_F_SUSPEND;
4958796SPengcheng.Chen@Sun.COM 		mutex_exit(&sc->sc_glock);
4968796SPengcheng.Chen@Sun.COM 
4978349SPengcheng.Chen@Sun.COM 		if (sc->sc_flags & IWK_F_RUNNING)
4988349SPengcheng.Chen@Sun.COM 			(void) iwk_init(sc);
4998349SPengcheng.Chen@Sun.COM 
5006795Shx147065 		mutex_enter(&sc->sc_glock);
5018349SPengcheng.Chen@Sun.COM 		sc->sc_flags |= IWK_F_LAZY_RESUME;
5026795Shx147065 		mutex_exit(&sc->sc_glock);
5038349SPengcheng.Chen@Sun.COM 
5046795Shx147065 		IWK_DBG((IWK_DEBUG_RESUME, "iwk: resume\n"));
5056795Shx147065 		return (DDI_SUCCESS);
5066795Shx147065 	default:
5076169Shx147065 		err = DDI_FAILURE;
5086169Shx147065 		goto attach_fail1;
5096169Shx147065 	}
5106169Shx147065 
5116169Shx147065 	instance = ddi_get_instance(dip);
5126169Shx147065 	err = ddi_soft_state_zalloc(iwk_soft_state_p, instance);
5136169Shx147065 	if (err != DDI_SUCCESS) {
5146169Shx147065 		cmn_err(CE_WARN,
5156169Shx147065 		    "iwk_attach(): failed to allocate soft state\n");
5166169Shx147065 		goto attach_fail1;
5176169Shx147065 	}
5186169Shx147065 	sc = ddi_get_soft_state(iwk_soft_state_p, instance);
5196169Shx147065 	sc->sc_dip = dip;
5206169Shx147065 
5216169Shx147065 	err = ddi_regs_map_setup(dip, 0, &sc->sc_cfg_base, 0, 0,
5226169Shx147065 	    &iwk_reg_accattr, &sc->sc_cfg_handle);
5236169Shx147065 	if (err != DDI_SUCCESS) {
5246169Shx147065 		cmn_err(CE_WARN,
5256169Shx147065 		    "iwk_attach(): failed to map config spaces regs\n");
5266169Shx147065 		goto attach_fail2;
5276169Shx147065 	}
5286169Shx147065 	sc->sc_rev = ddi_get8(sc->sc_cfg_handle,
5296169Shx147065 	    (uint8_t *)(sc->sc_cfg_base + PCI_CONF_REVID));
5306169Shx147065 	ddi_put8(sc->sc_cfg_handle, (uint8_t *)(sc->sc_cfg_base + 0x41), 0);
5316169Shx147065 	sc->sc_clsz = ddi_get16(sc->sc_cfg_handle,
5326169Shx147065 	    (uint16_t *)(sc->sc_cfg_base + PCI_CONF_CACHE_LINESZ));
5336169Shx147065 	if (!sc->sc_clsz)
5346169Shx147065 		sc->sc_clsz = 16;
5356169Shx147065 	sc->sc_clsz = (sc->sc_clsz << 2);
5366169Shx147065 	sc->sc_dmabuf_sz = roundup(0x1000 + sizeof (struct ieee80211_frame) +
5376169Shx147065 	    IEEE80211_MTU + IEEE80211_CRC_LEN +
5386169Shx147065 	    (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
5396169Shx147065 	    IEEE80211_WEP_CRCLEN), sc->sc_clsz);
5406169Shx147065 	/*
5416169Shx147065 	 * Map operating registers
5426169Shx147065 	 */
5436169Shx147065 	err = ddi_regs_map_setup(dip, 1, &sc->sc_base,
5446169Shx147065 	    0, 0, &iwk_reg_accattr, &sc->sc_handle);
5456169Shx147065 	if (err != DDI_SUCCESS) {
5466169Shx147065 		cmn_err(CE_WARN,
5476169Shx147065 		    "iwk_attach(): failed to map device regs\n");
5486169Shx147065 		goto attach_fail2a;
5496169Shx147065 	}
5506169Shx147065 
5517569SXinghua.Wen@Sun.COM 	err = ddi_intr_get_supported_types(dip, &intr_type);
5527569SXinghua.Wen@Sun.COM 	if ((err != DDI_SUCCESS) || (!(intr_type & DDI_INTR_TYPE_FIXED))) {
5537569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_attach(): "
5547569SXinghua.Wen@Sun.COM 		    "Fixed type interrupt is not supported\n");
5557569SXinghua.Wen@Sun.COM 		goto attach_fail_intr_a;
5567569SXinghua.Wen@Sun.COM 	}
5577569SXinghua.Wen@Sun.COM 
5587569SXinghua.Wen@Sun.COM 	err = ddi_intr_get_nintrs(dip, DDI_INTR_TYPE_FIXED, &intr_count);
5597569SXinghua.Wen@Sun.COM 	if ((err != DDI_SUCCESS) || (intr_count != 1)) {
5607569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_attach(): "
5617569SXinghua.Wen@Sun.COM 		    "No fixed interrupts\n");
5627569SXinghua.Wen@Sun.COM 		goto attach_fail_intr_a;
5637569SXinghua.Wen@Sun.COM 	}
5647569SXinghua.Wen@Sun.COM 
5657569SXinghua.Wen@Sun.COM 	sc->sc_intr_htable = kmem_zalloc(sizeof (ddi_intr_handle_t), KM_SLEEP);
5667569SXinghua.Wen@Sun.COM 
5677569SXinghua.Wen@Sun.COM 	err = ddi_intr_alloc(dip, sc->sc_intr_htable, DDI_INTR_TYPE_FIXED, 0,
5687569SXinghua.Wen@Sun.COM 	    intr_count, &intr_actual, 0);
5697569SXinghua.Wen@Sun.COM 	if ((err != DDI_SUCCESS) || (intr_actual != 1)) {
5707569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_attach(): "
5717569SXinghua.Wen@Sun.COM 		    "ddi_intr_alloc() failed 0x%x\n", err);
5727569SXinghua.Wen@Sun.COM 		goto attach_fail_intr_b;
5737569SXinghua.Wen@Sun.COM 	}
5747569SXinghua.Wen@Sun.COM 
5757569SXinghua.Wen@Sun.COM 	err = ddi_intr_get_pri(sc->sc_intr_htable[0], &sc->sc_intr_pri);
5766169Shx147065 	if (err != DDI_SUCCESS) {
5777569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_attach(): "
5787569SXinghua.Wen@Sun.COM 		    "ddi_intr_get_pri() failed 0x%x\n", err);
5797569SXinghua.Wen@Sun.COM 		goto attach_fail_intr_c;
5807569SXinghua.Wen@Sun.COM 	}
5817569SXinghua.Wen@Sun.COM 
5827569SXinghua.Wen@Sun.COM 	mutex_init(&sc->sc_glock, NULL, MUTEX_DRIVER,
5837569SXinghua.Wen@Sun.COM 	    DDI_INTR_PRI(sc->sc_intr_pri));
5847569SXinghua.Wen@Sun.COM 	mutex_init(&sc->sc_tx_lock, NULL, MUTEX_DRIVER,
5857569SXinghua.Wen@Sun.COM 	    DDI_INTR_PRI(sc->sc_intr_pri));
5867569SXinghua.Wen@Sun.COM 	mutex_init(&sc->sc_mt_lock, NULL, MUTEX_DRIVER,
5877569SXinghua.Wen@Sun.COM 	    DDI_INTR_PRI(sc->sc_intr_pri));
5888594SFei.Feng@Sun.COM 	mutex_init(&sc->sc_ibss.node_tb_lock, NULL, MUTEX_DRIVER,
5898594SFei.Feng@Sun.COM 	    DDI_INTR_PRI(sc->sc_intr_pri));
5907569SXinghua.Wen@Sun.COM 
5916169Shx147065 	cv_init(&sc->sc_fw_cv, NULL, CV_DRIVER, NULL);
5926169Shx147065 	cv_init(&sc->sc_cmd_cv, NULL, CV_DRIVER, NULL);
5936169Shx147065 	cv_init(&sc->sc_tx_cv, "tx-ring", CV_DRIVER, NULL);
5946169Shx147065 	/*
5956169Shx147065 	 * initialize the mfthread
5966169Shx147065 	 */
5976169Shx147065 	cv_init(&sc->sc_mt_cv, NULL, CV_DRIVER, NULL);
5986169Shx147065 	sc->sc_mf_thread = NULL;
5996169Shx147065 	sc->sc_mf_thread_switch = 0;
6006169Shx147065 
6016169Shx147065 	/*
6026169Shx147065 	 * Allocate shared page.
6036169Shx147065 	 */
6046169Shx147065 	err = iwk_alloc_shared(sc);
6056169Shx147065 	if (err != DDI_SUCCESS) {
6067569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_attach(): "
6077569SXinghua.Wen@Sun.COM 		    "failed to allocate shared page\n");
6086169Shx147065 		goto attach_fail3;
6096169Shx147065 	}
6106169Shx147065 
6116169Shx147065 	/*
6126169Shx147065 	 * Allocate keep warm page.
6136169Shx147065 	 */
6146169Shx147065 	err = iwk_alloc_kw(sc);
6156169Shx147065 	if (err != DDI_SUCCESS) {
6167569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_attach(): "
6177569SXinghua.Wen@Sun.COM 		    "failed to allocate keep warm page\n");
6186169Shx147065 		goto attach_fail3a;
6196169Shx147065 	}
6206169Shx147065 
6216169Shx147065 	/*
6226169Shx147065 	 * Do some necessary hardware initializations.
6236169Shx147065 	 */
6246169Shx147065 	err = iwk_preinit(sc);
6256169Shx147065 	if (err != DDI_SUCCESS) {
6267569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_attach(): "
6277569SXinghua.Wen@Sun.COM 		    "failed to init hardware\n");
6286169Shx147065 		goto attach_fail4;
6296169Shx147065 	}
6306169Shx147065 
6316169Shx147065 	/* initialize EEPROM */
6326169Shx147065 	err = iwk_eep_load(sc);  /* get hardware configurations from eeprom */
6336169Shx147065 	if (err != 0) {
6346169Shx147065 		cmn_err(CE_WARN, "iwk_attach(): failed to load eeprom\n");
6356169Shx147065 		goto attach_fail4;
6366169Shx147065 	}
6376169Shx147065 
6389327SFei.Feng@Sun.COM 	if (LE_16(sc->sc_eep_map.calib_version) < EEP_TX_POWER_VERSION_NEW) {
6397691SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "older EEPROM detected\n");
6406169Shx147065 		goto attach_fail4;
6416169Shx147065 	}
6426169Shx147065 
6436169Shx147065 	iwk_get_mac_from_eep(sc);
6446169Shx147065 
6456169Shx147065 	err = iwk_ring_init(sc);
6466169Shx147065 	if (err != DDI_SUCCESS) {
6476169Shx147065 		cmn_err(CE_WARN, "iwk_attach(): "
6486169Shx147065 		    "failed to allocate and initialize ring\n");
6496169Shx147065 		goto attach_fail4;
6506169Shx147065 	}
6516169Shx147065 
6526169Shx147065 	sc->sc_hdr = (iwk_firmware_hdr_t *)iwk_fw_bin;
6536169Shx147065 
6546169Shx147065 	err = iwk_alloc_fw_dma(sc);
6556169Shx147065 	if (err != DDI_SUCCESS) {
6566169Shx147065 		cmn_err(CE_WARN, "iwk_attach(): "
6576169Shx147065 		    "failed to allocate firmware dma\n");
6586169Shx147065 		goto attach_fail5;
6596169Shx147065 	}
6606169Shx147065 
6616169Shx147065 	/*
6626169Shx147065 	 * Initialize the wifi part, which will be used by
6636169Shx147065 	 * generic layer
6646169Shx147065 	 */
6656169Shx147065 	ic = &sc->sc_ic;
6666169Shx147065 	ic->ic_phytype  = IEEE80211_T_OFDM;
6676169Shx147065 	ic->ic_opmode   = IEEE80211_M_STA; /* default to BSS mode */
6686169Shx147065 	ic->ic_state    = IEEE80211_S_INIT;
6696169Shx147065 	ic->ic_maxrssi  = 100; /* experimental number */
6706169Shx147065 	ic->ic_caps = IEEE80211_C_SHPREAMBLE | IEEE80211_C_TXPMGT |
6716169Shx147065 	    IEEE80211_C_PMGT | IEEE80211_C_SHSLOT;
6726169Shx147065 	/*
6736169Shx147065 	 * use software WEP and TKIP, hardware CCMP;
6746169Shx147065 	 */
6756169Shx147065 	ic->ic_caps |= IEEE80211_C_AES_CCM;
6766169Shx147065 	/*
6776169Shx147065 	 * Support WPA/WPA2
6786169Shx147065 	 */
6796169Shx147065 	ic->ic_caps |= IEEE80211_C_WPA;
6808594SFei.Feng@Sun.COM 	/*
6818594SFei.Feng@Sun.COM 	 * support Adhoc mode
6828594SFei.Feng@Sun.COM 	 */
6838594SFei.Feng@Sun.COM 	ic->ic_caps |= IEEE80211_C_IBSS;
6847569SXinghua.Wen@Sun.COM 
6856169Shx147065 	/* set supported .11b and .11g rates */
6866169Shx147065 	ic->ic_sup_rates[IEEE80211_MODE_11B] = iwk_rateset_11b;
6876169Shx147065 	ic->ic_sup_rates[IEEE80211_MODE_11G] = iwk_rateset_11g;
6886169Shx147065 
6897691SPengcheng.Chen@Sun.COM 	/* set supported .11b and .11g channels (1 through 11) */
6907691SPengcheng.Chen@Sun.COM 	for (i = 1; i <= 11; i++) {
6916169Shx147065 		ic->ic_sup_channels[i].ich_freq =
6926169Shx147065 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
6936169Shx147065 		ic->ic_sup_channels[i].ich_flags =
6946169Shx147065 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
6957691SPengcheng.Chen@Sun.COM 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ |
6967691SPengcheng.Chen@Sun.COM 		    IEEE80211_CHAN_PASSIVE;
6976169Shx147065 	}
6988594SFei.Feng@Sun.COM 	ic->ic_ibss_chan = &ic->ic_sup_channels[0];
6997569SXinghua.Wen@Sun.COM 
7006169Shx147065 	ic->ic_xmit = iwk_send;
7016169Shx147065 	/*
7026169Shx147065 	 * init Wifi layer
7036169Shx147065 	 */
7046169Shx147065 	ieee80211_attach(ic);
7056169Shx147065 
7066169Shx147065 	/*
7076169Shx147065 	 * different instance has different WPA door
7086169Shx147065 	 */
7096169Shx147065 	(void) snprintf(ic->ic_wpadoor, MAX_IEEE80211STR, "%s_%s%d", WPA_DOOR,
7106169Shx147065 	    ddi_driver_name(dip),
7116169Shx147065 	    ddi_get_instance(dip));
7126169Shx147065 
7136169Shx147065 	/*
7146169Shx147065 	 * Override 80211 default routines
7156169Shx147065 	 */
7166169Shx147065 	sc->sc_newstate = ic->ic_newstate;
7176169Shx147065 	ic->ic_newstate = iwk_newstate;
7188727SPengcheng.Chen@Sun.COM 	ic->ic_watchdog = iwk_watchdog;
7197569SXinghua.Wen@Sun.COM 	sc->sc_recv_mgmt = ic->ic_recv_mgmt;
7208594SFei.Feng@Sun.COM 	ic->ic_recv_mgmt = iwk_recv_mgmt;
7216249Shx147065 	ic->ic_node_alloc = iwk_node_alloc;
7226249Shx147065 	ic->ic_node_free = iwk_node_free;
7236169Shx147065 	ic->ic_crypto.cs_key_set = iwk_key_set;
7246169Shx147065 	ieee80211_media_init(ic);
7256169Shx147065 	/*
7266169Shx147065 	 * initialize default tx key
7276169Shx147065 	 */
7286169Shx147065 	ic->ic_def_txkey = 0;
7297569SXinghua.Wen@Sun.COM 	err = ddi_intr_add_softint(dip, &sc->sc_soft_hdl, DDI_INTR_SOFTPRI_MAX,
7307569SXinghua.Wen@Sun.COM 	    iwk_rx_softintr, (caddr_t)sc);
7316169Shx147065 	if (err != DDI_SUCCESS) {
7327569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_attach(): "
7337569SXinghua.Wen@Sun.COM 		    "add soft interrupt failed\n");
7346169Shx147065 		goto attach_fail7;
7356169Shx147065 	}
7366169Shx147065 
7376169Shx147065 	/*
7386169Shx147065 	 * Add the interrupt handler
7396169Shx147065 	 */
7407569SXinghua.Wen@Sun.COM 	err = ddi_intr_add_handler(sc->sc_intr_htable[0], iwk_intr,
7417569SXinghua.Wen@Sun.COM 	    (caddr_t)sc, NULL);
7426169Shx147065 	if (err != DDI_SUCCESS) {
7437569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_attach(): "
7447569SXinghua.Wen@Sun.COM 		    "ddi_intr_add_handle() failed\n");
7456169Shx147065 		goto attach_fail8;
7466169Shx147065 	}
7476169Shx147065 
7487569SXinghua.Wen@Sun.COM 	err = ddi_intr_enable(sc->sc_intr_htable[0]);
7497569SXinghua.Wen@Sun.COM 	if (err != DDI_SUCCESS) {
7507569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_attach(): "
7517569SXinghua.Wen@Sun.COM 		    "ddi_intr_enable() failed\n");
7527569SXinghua.Wen@Sun.COM 		goto attach_fail_intr_d;
7537569SXinghua.Wen@Sun.COM 	}
7547569SXinghua.Wen@Sun.COM 
7556169Shx147065 	/*
7566169Shx147065 	 * Initialize pointer to device specific functions
7576169Shx147065 	 */
7586169Shx147065 	wd.wd_secalloc = WIFI_SEC_NONE;
7596169Shx147065 	wd.wd_opmode = ic->ic_opmode;
7606169Shx147065 	IEEE80211_ADDR_COPY(wd.wd_bssid, ic->ic_macaddr);
7616169Shx147065 
7626169Shx147065 	macp = mac_alloc(MAC_VERSION);
7638594SFei.Feng@Sun.COM 	if (macp == NULL) {
7646169Shx147065 		cmn_err(CE_WARN,
7656169Shx147065 		    "iwk_attach(): failed to do mac_alloc()\n");
7666169Shx147065 		goto attach_fail9;
7676169Shx147065 	}
7686169Shx147065 
7696169Shx147065 	macp->m_type_ident	= MAC_PLUGIN_IDENT_WIFI;
7706169Shx147065 	macp->m_driver		= sc;
7716169Shx147065 	macp->m_dip		= dip;
7726169Shx147065 	macp->m_src_addr	= ic->ic_macaddr;
7736169Shx147065 	macp->m_callbacks	= &iwk_m_callbacks;
7746169Shx147065 	macp->m_min_sdu		= 0;
7756169Shx147065 	macp->m_max_sdu		= IEEE80211_MTU;
7766169Shx147065 	macp->m_pdata		= &wd;
7776169Shx147065 	macp->m_pdata_size	= sizeof (wd);
7786169Shx147065 
7796169Shx147065 	/*
7806169Shx147065 	 * Register the macp to mac
7816169Shx147065 	 */
7826169Shx147065 	err = mac_register(macp, &ic->ic_mach);
7836169Shx147065 	mac_free(macp);
7846169Shx147065 	if (err != DDI_SUCCESS) {
7856169Shx147065 		cmn_err(CE_WARN,
7866169Shx147065 		    "iwk_attach(): failed to do mac_register()\n");
7876169Shx147065 		goto attach_fail9;
7886169Shx147065 	}
7896169Shx147065 
7906169Shx147065 	/*
7916169Shx147065 	 * Create minor node of type DDI_NT_NET_WIFI
7926169Shx147065 	 */
7936169Shx147065 	(void) snprintf(strbuf, sizeof (strbuf), DRV_NAME_4965"%d", instance);
7946169Shx147065 	err = ddi_create_minor_node(dip, strbuf, S_IFCHR,
7956169Shx147065 	    instance + 1, DDI_NT_NET_WIFI, 0);
7966169Shx147065 	if (err != DDI_SUCCESS)
7976169Shx147065 		cmn_err(CE_WARN,
7986169Shx147065 		    "iwk_attach(): failed to do ddi_create_minor_node()\n");
7996169Shx147065 
8006169Shx147065 	/*
8016169Shx147065 	 * Notify link is down now
8026169Shx147065 	 */
8036169Shx147065 	mac_link_update(ic->ic_mach, LINK_STATE_DOWN);
8046169Shx147065 
8056169Shx147065 	/*
8066169Shx147065 	 * create the mf thread to handle the link status,
8076169Shx147065 	 * recovery fatal error, etc.
8086169Shx147065 	 */
8096169Shx147065 	sc->sc_mf_thread_switch = 1;
8106169Shx147065 	if (sc->sc_mf_thread == NULL)
8116169Shx147065 		sc->sc_mf_thread = thread_create((caddr_t)NULL, 0,
8126169Shx147065 		    iwk_thread, sc, 0, &p0, TS_RUN, minclsyspri);
8136169Shx147065 
8146169Shx147065 	sc->sc_flags |= IWK_F_ATTACHED;
8156169Shx147065 
8166169Shx147065 	return (DDI_SUCCESS);
8176169Shx147065 attach_fail9:
8187569SXinghua.Wen@Sun.COM 	(void) ddi_intr_disable(sc->sc_intr_htable[0]);
8197569SXinghua.Wen@Sun.COM attach_fail_intr_d:
8207569SXinghua.Wen@Sun.COM 	(void) ddi_intr_remove_handler(sc->sc_intr_htable[0]);
8217569SXinghua.Wen@Sun.COM 
8226169Shx147065 attach_fail8:
8237569SXinghua.Wen@Sun.COM 	(void) ddi_intr_remove_softint(sc->sc_soft_hdl);
8247569SXinghua.Wen@Sun.COM 	sc->sc_soft_hdl = NULL;
8256169Shx147065 attach_fail7:
8266169Shx147065 	ieee80211_detach(ic);
8276169Shx147065 attach_fail6:
8286169Shx147065 	iwk_free_fw_dma(sc);
8296169Shx147065 attach_fail5:
8306169Shx147065 	iwk_ring_free(sc);
8316169Shx147065 attach_fail4:
8326169Shx147065 	iwk_free_kw(sc);
8336169Shx147065 attach_fail3a:
8346169Shx147065 	iwk_free_shared(sc);
8356169Shx147065 attach_fail3:
8366169Shx147065 	iwk_destroy_locks(sc);
8377569SXinghua.Wen@Sun.COM attach_fail_intr_c:
8387569SXinghua.Wen@Sun.COM 	(void) ddi_intr_free(sc->sc_intr_htable[0]);
8397569SXinghua.Wen@Sun.COM attach_fail_intr_b:
8407569SXinghua.Wen@Sun.COM 	kmem_free(sc->sc_intr_htable, sizeof (ddi_intr_handle_t));
8417569SXinghua.Wen@Sun.COM attach_fail_intr_a:
8426169Shx147065 	ddi_regs_map_free(&sc->sc_handle);
8436169Shx147065 attach_fail2a:
8446169Shx147065 	ddi_regs_map_free(&sc->sc_cfg_handle);
8456169Shx147065 attach_fail2:
8466169Shx147065 	ddi_soft_state_free(iwk_soft_state_p, instance);
8476169Shx147065 attach_fail1:
8486169Shx147065 	return (err);
8496169Shx147065 }
8506169Shx147065 
8516169Shx147065 int
iwk_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)8526169Shx147065 iwk_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
8536169Shx147065 {
8546169Shx147065 	iwk_sc_t	*sc;
8556169Shx147065 	int err;
8566169Shx147065 
8576169Shx147065 	sc = ddi_get_soft_state(iwk_soft_state_p, ddi_get_instance(dip));
8586169Shx147065 	ASSERT(sc != NULL);
8596169Shx147065 
8606795Shx147065 	switch (cmd) {
8616795Shx147065 	case DDI_DETACH:
8626795Shx147065 		break;
8636795Shx147065 	case DDI_SUSPEND:
8648349SPengcheng.Chen@Sun.COM 		mutex_enter(&sc->sc_glock);
8658349SPengcheng.Chen@Sun.COM 		sc->sc_flags |= IWK_F_SUSPEND;
8668349SPengcheng.Chen@Sun.COM 		mutex_exit(&sc->sc_glock);
8676795Shx147065 		if (sc->sc_flags & IWK_F_RUNNING) {
8686795Shx147065 			iwk_stop(sc);
8696795Shx147065 		}
8708349SPengcheng.Chen@Sun.COM 
8716795Shx147065 		IWK_DBG((IWK_DEBUG_RESUME, "iwk: suspend\n"));
8726795Shx147065 		return (DDI_SUCCESS);
8736795Shx147065 	default:
8746169Shx147065 		return (DDI_FAILURE);
8756795Shx147065 	}
8766249Shx147065 
8776169Shx147065 	if (!(sc->sc_flags & IWK_F_ATTACHED))
8786169Shx147065 		return (DDI_FAILURE);
8796169Shx147065 
8807569SXinghua.Wen@Sun.COM 	err = mac_disable(sc->sc_ic.ic_mach);
8817569SXinghua.Wen@Sun.COM 	if (err != DDI_SUCCESS)
8827569SXinghua.Wen@Sun.COM 		return (err);
8837569SXinghua.Wen@Sun.COM 
8846169Shx147065 	/*
8856169Shx147065 	 * Destroy the mf_thread
8866169Shx147065 	 */
8876169Shx147065 	mutex_enter(&sc->sc_mt_lock);
8886169Shx147065 	sc->sc_mf_thread_switch = 0;
8896169Shx147065 	while (sc->sc_mf_thread != NULL) {
8906169Shx147065 		if (cv_wait_sig(&sc->sc_mt_cv, &sc->sc_mt_lock) == 0)
8916169Shx147065 			break;
8926169Shx147065 	}
8936169Shx147065 	mutex_exit(&sc->sc_mt_lock);
8946169Shx147065 
8956169Shx147065 	iwk_stop(sc);
8966169Shx147065 	DELAY(500000);
8976169Shx147065 
8986169Shx147065 	/*
8996169Shx147065 	 * Unregiste from the MAC layer subsystem
9006169Shx147065 	 */
9017569SXinghua.Wen@Sun.COM 	(void) mac_unregister(sc->sc_ic.ic_mach);
9026169Shx147065 
9036169Shx147065 	mutex_enter(&sc->sc_glock);
9046169Shx147065 	iwk_free_fw_dma(sc);
9056169Shx147065 	iwk_ring_free(sc);
9066169Shx147065 	iwk_free_kw(sc);
9076169Shx147065 	iwk_free_shared(sc);
9086169Shx147065 	mutex_exit(&sc->sc_glock);
9096169Shx147065 
9107569SXinghua.Wen@Sun.COM 	(void) ddi_intr_disable(sc->sc_intr_htable[0]);
9117569SXinghua.Wen@Sun.COM 	(void) ddi_intr_remove_handler(sc->sc_intr_htable[0]);
9127569SXinghua.Wen@Sun.COM 	(void) ddi_intr_free(sc->sc_intr_htable[0]);
9137569SXinghua.Wen@Sun.COM 	kmem_free(sc->sc_intr_htable, sizeof (ddi_intr_handle_t));
9147569SXinghua.Wen@Sun.COM 
9157569SXinghua.Wen@Sun.COM 	(void) ddi_intr_remove_softint(sc->sc_soft_hdl);
9167569SXinghua.Wen@Sun.COM 	sc->sc_soft_hdl = NULL;
9176169Shx147065 
9186169Shx147065 	/*
9196169Shx147065 	 * detach ieee80211
9206169Shx147065 	 */
9216169Shx147065 	ieee80211_detach(&sc->sc_ic);
9226169Shx147065 
9236169Shx147065 	iwk_destroy_locks(sc);
9246169Shx147065 
9256169Shx147065 	ddi_regs_map_free(&sc->sc_handle);
9266169Shx147065 	ddi_regs_map_free(&sc->sc_cfg_handle);
9276169Shx147065 	ddi_remove_minor_node(dip, NULL);
9286169Shx147065 	ddi_soft_state_free(iwk_soft_state_p, ddi_get_instance(dip));
9296169Shx147065 
9306169Shx147065 	return (DDI_SUCCESS);
9316169Shx147065 }
9326169Shx147065 
9337891SPengcheng.Chen@Sun.COM /*
9347891SPengcheng.Chen@Sun.COM  * quiesce(9E) entry point.
9357891SPengcheng.Chen@Sun.COM  *
9367891SPengcheng.Chen@Sun.COM  * This function is called when the system is single-threaded at high
9377891SPengcheng.Chen@Sun.COM  * PIL with preemption disabled. Therefore, this function must not be
9387891SPengcheng.Chen@Sun.COM  * blocked.
9397891SPengcheng.Chen@Sun.COM  *
9407891SPengcheng.Chen@Sun.COM  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
9417891SPengcheng.Chen@Sun.COM  * DDI_FAILURE indicates an error condition and should almost never happen.
9427891SPengcheng.Chen@Sun.COM  */
9437891SPengcheng.Chen@Sun.COM int
iwk_quiesce(dev_info_t * dip)9447891SPengcheng.Chen@Sun.COM iwk_quiesce(dev_info_t *dip)
9457891SPengcheng.Chen@Sun.COM {
9467891SPengcheng.Chen@Sun.COM 	iwk_sc_t	*sc;
9477891SPengcheng.Chen@Sun.COM 
9487891SPengcheng.Chen@Sun.COM 	sc = ddi_get_soft_state(iwk_soft_state_p, ddi_get_instance(dip));
9497891SPengcheng.Chen@Sun.COM 	ASSERT(sc != NULL);
9507891SPengcheng.Chen@Sun.COM 
9517891SPengcheng.Chen@Sun.COM 	/* no message prints and no lock accquisition */
9527891SPengcheng.Chen@Sun.COM #ifdef DEBUG
9537891SPengcheng.Chen@Sun.COM 	iwk_dbg_flags = 0;
9547891SPengcheng.Chen@Sun.COM #endif
9557891SPengcheng.Chen@Sun.COM 	sc->sc_flags |= IWK_F_QUIESCED;
9567891SPengcheng.Chen@Sun.COM 
9577891SPengcheng.Chen@Sun.COM 	iwk_stop(sc);
9587891SPengcheng.Chen@Sun.COM 
9597891SPengcheng.Chen@Sun.COM 	return (DDI_SUCCESS);
9607891SPengcheng.Chen@Sun.COM }
9617891SPengcheng.Chen@Sun.COM 
9626169Shx147065 static void
iwk_destroy_locks(iwk_sc_t * sc)9636169Shx147065 iwk_destroy_locks(iwk_sc_t *sc)
9646169Shx147065 {
9656169Shx147065 	cv_destroy(&sc->sc_mt_cv);
9666169Shx147065 	mutex_destroy(&sc->sc_mt_lock);
9676169Shx147065 	cv_destroy(&sc->sc_tx_cv);
9686169Shx147065 	cv_destroy(&sc->sc_cmd_cv);
9696169Shx147065 	cv_destroy(&sc->sc_fw_cv);
9706169Shx147065 	mutex_destroy(&sc->sc_tx_lock);
9716169Shx147065 	mutex_destroy(&sc->sc_glock);
9726169Shx147065 }
9736169Shx147065 
9746169Shx147065 /*
9756169Shx147065  * Allocate an area of memory and a DMA handle for accessing it
9766169Shx147065  */
9776169Shx147065 static int
iwk_alloc_dma_mem(iwk_sc_t * sc,size_t memsize,ddi_dma_attr_t * dma_attr_p,ddi_device_acc_attr_t * acc_attr_p,uint_t dma_flags,iwk_dma_t * dma_p)9786169Shx147065 iwk_alloc_dma_mem(iwk_sc_t *sc, size_t memsize,
9796169Shx147065     ddi_dma_attr_t *dma_attr_p, ddi_device_acc_attr_t *acc_attr_p,
9806169Shx147065     uint_t dma_flags, iwk_dma_t *dma_p)
9816169Shx147065 {
9826169Shx147065 	caddr_t vaddr;
9836169Shx147065 	int err;
9846169Shx147065 
9856169Shx147065 	/*
9866169Shx147065 	 * Allocate handle
9876169Shx147065 	 */
9886169Shx147065 	err = ddi_dma_alloc_handle(sc->sc_dip, dma_attr_p,
9896169Shx147065 	    DDI_DMA_SLEEP, NULL, &dma_p->dma_hdl);
9906169Shx147065 	if (err != DDI_SUCCESS) {
9916169Shx147065 		dma_p->dma_hdl = NULL;
9926169Shx147065 		return (DDI_FAILURE);
9936169Shx147065 	}
9946169Shx147065 
9956169Shx147065 	/*
9966169Shx147065 	 * Allocate memory
9976169Shx147065 	 */
9986169Shx147065 	err = ddi_dma_mem_alloc(dma_p->dma_hdl, memsize, acc_attr_p,
9996169Shx147065 	    dma_flags & (DDI_DMA_CONSISTENT | DDI_DMA_STREAMING),
10006169Shx147065 	    DDI_DMA_SLEEP, NULL, &vaddr, &dma_p->alength, &dma_p->acc_hdl);
10016169Shx147065 	if (err != DDI_SUCCESS) {
10026169Shx147065 		ddi_dma_free_handle(&dma_p->dma_hdl);
10036169Shx147065 		dma_p->dma_hdl = NULL;
10046169Shx147065 		dma_p->acc_hdl = NULL;
10056169Shx147065 		return (DDI_FAILURE);
10066169Shx147065 	}
10076169Shx147065 
10086169Shx147065 	/*
10096169Shx147065 	 * Bind the two together
10106169Shx147065 	 */
10116169Shx147065 	dma_p->mem_va = vaddr;
10126169Shx147065 	err = ddi_dma_addr_bind_handle(dma_p->dma_hdl, NULL,
10136169Shx147065 	    vaddr, dma_p->alength, dma_flags, DDI_DMA_SLEEP, NULL,
10146169Shx147065 	    &dma_p->cookie, &dma_p->ncookies);
10156169Shx147065 	if (err != DDI_DMA_MAPPED) {
10166169Shx147065 		ddi_dma_mem_free(&dma_p->acc_hdl);
10176169Shx147065 		ddi_dma_free_handle(&dma_p->dma_hdl);
10186169Shx147065 		dma_p->acc_hdl = NULL;
10196169Shx147065 		dma_p->dma_hdl = NULL;
10206169Shx147065 		return (DDI_FAILURE);
10216169Shx147065 	}
10226169Shx147065 
10236169Shx147065 	dma_p->nslots = ~0U;
10246169Shx147065 	dma_p->size = ~0U;
10256169Shx147065 	dma_p->token = ~0U;
10266169Shx147065 	dma_p->offset = 0;
10276169Shx147065 	return (DDI_SUCCESS);
10286169Shx147065 }
10296169Shx147065 
10306169Shx147065 /*
10316169Shx147065  * Free one allocated area of DMAable memory
10326169Shx147065  */
10336169Shx147065 static void
iwk_free_dma_mem(iwk_dma_t * dma_p)10346169Shx147065 iwk_free_dma_mem(iwk_dma_t *dma_p)
10356169Shx147065 {
10366169Shx147065 	if (dma_p->dma_hdl != NULL) {
10376169Shx147065 		if (dma_p->ncookies) {
10386169Shx147065 			(void) ddi_dma_unbind_handle(dma_p->dma_hdl);
10396169Shx147065 			dma_p->ncookies = 0;
10406169Shx147065 		}
10416169Shx147065 		ddi_dma_free_handle(&dma_p->dma_hdl);
10426169Shx147065 		dma_p->dma_hdl = NULL;
10436169Shx147065 	}
10446169Shx147065 
10456169Shx147065 	if (dma_p->acc_hdl != NULL) {
10466169Shx147065 		ddi_dma_mem_free(&dma_p->acc_hdl);
10476169Shx147065 		dma_p->acc_hdl = NULL;
10486169Shx147065 	}
10496169Shx147065 }
10506169Shx147065 
10516169Shx147065 /*
10526169Shx147065  *
10536169Shx147065  */
10546169Shx147065 static int
iwk_alloc_fw_dma(iwk_sc_t * sc)10556169Shx147065 iwk_alloc_fw_dma(iwk_sc_t *sc)
10566169Shx147065 {
10576169Shx147065 	int err = DDI_SUCCESS;
10586169Shx147065 	iwk_dma_t *dma_p;
10596169Shx147065 	char *t;
10606169Shx147065 
10616169Shx147065 	/*
10626169Shx147065 	 * firmware image layout:
10636169Shx147065 	 * |HDR|<-TEXT->|<-DATA->|<-INIT_TEXT->|<-INIT_DATA->|<-BOOT->|
10646169Shx147065 	 */
10656169Shx147065 	t = (char *)(sc->sc_hdr + 1);
10666169Shx147065 	err = iwk_alloc_dma_mem(sc, LE_32(sc->sc_hdr->textsz),
10676169Shx147065 	    &fw_dma_attr, &iwk_dma_accattr,
10686169Shx147065 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
10696169Shx147065 	    &sc->sc_dma_fw_text);
10706169Shx147065 	dma_p = &sc->sc_dma_fw_text;
10716169Shx147065 	IWK_DBG((IWK_DEBUG_DMA, "text[ncookies:%d addr:%lx size:%lx]\n",
10726169Shx147065 	    dma_p->ncookies, dma_p->cookie.dmac_address,
10736169Shx147065 	    dma_p->cookie.dmac_size));
10746169Shx147065 	if (err != DDI_SUCCESS) {
10756169Shx147065 		cmn_err(CE_WARN, "iwk_alloc_fw_dma(): failed to alloc"
10766169Shx147065 		    " text dma memory");
10776169Shx147065 		goto fail;
10786169Shx147065 	}
10796169Shx147065 	(void) memcpy(dma_p->mem_va, t, LE_32(sc->sc_hdr->textsz));
10806169Shx147065 
10816169Shx147065 	t += LE_32(sc->sc_hdr->textsz);
10826169Shx147065 	err = iwk_alloc_dma_mem(sc, LE_32(sc->sc_hdr->datasz),
10836169Shx147065 	    &fw_dma_attr, &iwk_dma_accattr,
10846169Shx147065 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
10856169Shx147065 	    &sc->sc_dma_fw_data);
10866169Shx147065 	dma_p = &sc->sc_dma_fw_data;
10876169Shx147065 	IWK_DBG((IWK_DEBUG_DMA, "data[ncookies:%d addr:%lx size:%lx]\n",
10886169Shx147065 	    dma_p->ncookies, dma_p->cookie.dmac_address,
10896169Shx147065 	    dma_p->cookie.dmac_size));
10906169Shx147065 	if (err != DDI_SUCCESS) {
10916169Shx147065 		cmn_err(CE_WARN, "iwk_alloc_fw_dma(): failed to alloc"
10926169Shx147065 		    " data dma memory");
10936169Shx147065 		goto fail;
10946169Shx147065 	}
10956169Shx147065 	(void) memcpy(dma_p->mem_va, t, LE_32(sc->sc_hdr->datasz));
10966169Shx147065 
10976169Shx147065 	err = iwk_alloc_dma_mem(sc, LE_32(sc->sc_hdr->datasz),
10986169Shx147065 	    &fw_dma_attr, &iwk_dma_accattr,
10996169Shx147065 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
11006169Shx147065 	    &sc->sc_dma_fw_data_bak);
11016169Shx147065 	dma_p = &sc->sc_dma_fw_data_bak;
11026169Shx147065 	IWK_DBG((IWK_DEBUG_DMA, "data_bak[ncookies:%d addr:%lx "
11036169Shx147065 	    "size:%lx]\n",
11046169Shx147065 	    dma_p->ncookies, dma_p->cookie.dmac_address,
11056169Shx147065 	    dma_p->cookie.dmac_size));
11066169Shx147065 	if (err != DDI_SUCCESS) {
11076169Shx147065 		cmn_err(CE_WARN, "iwk_alloc_fw_dma(): failed to alloc"
11086169Shx147065 		    " data bakeup dma memory");
11096169Shx147065 		goto fail;
11106169Shx147065 	}
11116169Shx147065 	(void) memcpy(dma_p->mem_va, t, LE_32(sc->sc_hdr->datasz));
11126169Shx147065 
11136169Shx147065 	t += LE_32(sc->sc_hdr->datasz);
11146169Shx147065 	err = iwk_alloc_dma_mem(sc, LE_32(sc->sc_hdr->init_textsz),
11156169Shx147065 	    &fw_dma_attr, &iwk_dma_accattr,
11166169Shx147065 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
11176169Shx147065 	    &sc->sc_dma_fw_init_text);
11186169Shx147065 	dma_p = &sc->sc_dma_fw_init_text;
11196169Shx147065 	IWK_DBG((IWK_DEBUG_DMA, "init_text[ncookies:%d addr:%lx "
11206169Shx147065 	    "size:%lx]\n",
11216169Shx147065 	    dma_p->ncookies, dma_p->cookie.dmac_address,
11226169Shx147065 	    dma_p->cookie.dmac_size));
11236169Shx147065 	if (err != DDI_SUCCESS) {
11246169Shx147065 		cmn_err(CE_WARN, "iwk_alloc_fw_dma(): failed to alloc"
11256169Shx147065 		    "init text dma memory");
11266169Shx147065 		goto fail;
11276169Shx147065 	}
11286169Shx147065 	(void) memcpy(dma_p->mem_va, t, LE_32(sc->sc_hdr->init_textsz));
11296169Shx147065 
11306169Shx147065 	t += LE_32(sc->sc_hdr->init_textsz);
11316169Shx147065 	err = iwk_alloc_dma_mem(sc, LE_32(sc->sc_hdr->init_datasz),
11326169Shx147065 	    &fw_dma_attr, &iwk_dma_accattr,
11336169Shx147065 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
11346169Shx147065 	    &sc->sc_dma_fw_init_data);
11356169Shx147065 	dma_p = &sc->sc_dma_fw_init_data;
11366169Shx147065 	IWK_DBG((IWK_DEBUG_DMA, "init_data[ncookies:%d addr:%lx "
11376169Shx147065 	    "size:%lx]\n",
11386169Shx147065 	    dma_p->ncookies, dma_p->cookie.dmac_address,
11396169Shx147065 	    dma_p->cookie.dmac_size));
11406169Shx147065 	if (err != DDI_SUCCESS) {
11416169Shx147065 		cmn_err(CE_WARN, "iwk_alloc_fw_dma(): failed to alloc"
11426169Shx147065 		    "init data dma memory");
11436169Shx147065 		goto fail;
11446169Shx147065 	}
11456169Shx147065 	(void) memcpy(dma_p->mem_va, t, LE_32(sc->sc_hdr->init_datasz));
11466169Shx147065 
11476169Shx147065 	sc->sc_boot = t + LE_32(sc->sc_hdr->init_datasz);
11486169Shx147065 fail:
11496169Shx147065 	return (err);
11506169Shx147065 }
11516169Shx147065 
11526169Shx147065 static void
iwk_free_fw_dma(iwk_sc_t * sc)11536169Shx147065 iwk_free_fw_dma(iwk_sc_t *sc)
11546169Shx147065 {
11556169Shx147065 	iwk_free_dma_mem(&sc->sc_dma_fw_text);
11566169Shx147065 	iwk_free_dma_mem(&sc->sc_dma_fw_data);
11576169Shx147065 	iwk_free_dma_mem(&sc->sc_dma_fw_data_bak);
11586169Shx147065 	iwk_free_dma_mem(&sc->sc_dma_fw_init_text);
11596169Shx147065 	iwk_free_dma_mem(&sc->sc_dma_fw_init_data);
11606169Shx147065 }
11616169Shx147065 
11626169Shx147065 /*
11636169Shx147065  * Allocate a shared page between host and NIC.
11646169Shx147065  */
11656169Shx147065 static int
iwk_alloc_shared(iwk_sc_t * sc)11666169Shx147065 iwk_alloc_shared(iwk_sc_t *sc)
11676169Shx147065 {
11686169Shx147065 	iwk_dma_t *dma_p;
11696169Shx147065 	int err = DDI_SUCCESS;
11706169Shx147065 
11716169Shx147065 	/* must be aligned on a 4K-page boundary */
11726169Shx147065 	err = iwk_alloc_dma_mem(sc, sizeof (iwk_shared_t),
11739327SFei.Feng@Sun.COM 	    &sh_dma_attr, &iwk_dma_descattr,
11746169Shx147065 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
11756169Shx147065 	    &sc->sc_dma_sh);
11766169Shx147065 	if (err != DDI_SUCCESS)
11776169Shx147065 		goto fail;
11786169Shx147065 	sc->sc_shared = (iwk_shared_t *)sc->sc_dma_sh.mem_va;
11796169Shx147065 
11806169Shx147065 	dma_p = &sc->sc_dma_sh;
11816169Shx147065 	IWK_DBG((IWK_DEBUG_DMA, "sh[ncookies:%d addr:%lx size:%lx]\n",
11826169Shx147065 	    dma_p->ncookies, dma_p->cookie.dmac_address,
11836169Shx147065 	    dma_p->cookie.dmac_size));
11846169Shx147065 
11856169Shx147065 	return (err);
11866169Shx147065 fail:
11876169Shx147065 	iwk_free_shared(sc);
11886169Shx147065 	return (err);
11896169Shx147065 }
11906169Shx147065 
11916169Shx147065 static void
iwk_free_shared(iwk_sc_t * sc)11926169Shx147065 iwk_free_shared(iwk_sc_t *sc)
11936169Shx147065 {
11946169Shx147065 	iwk_free_dma_mem(&sc->sc_dma_sh);
11956169Shx147065 }
11966169Shx147065 
11976169Shx147065 /*
11986169Shx147065  * Allocate a keep warm page.
11996169Shx147065  */
12006169Shx147065 static int
iwk_alloc_kw(iwk_sc_t * sc)12016169Shx147065 iwk_alloc_kw(iwk_sc_t *sc)
12026169Shx147065 {
12036169Shx147065 	iwk_dma_t *dma_p;
12046169Shx147065 	int err = DDI_SUCCESS;
12056169Shx147065 
12066169Shx147065 	/* must be aligned on a 4K-page boundary */
12076169Shx147065 	err = iwk_alloc_dma_mem(sc, IWK_KW_SIZE,
12086169Shx147065 	    &kw_dma_attr, &iwk_dma_accattr,
12096169Shx147065 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
12106169Shx147065 	    &sc->sc_dma_kw);
12116169Shx147065 	if (err != DDI_SUCCESS)
12126169Shx147065 		goto fail;
12136169Shx147065 
12146169Shx147065 	dma_p = &sc->sc_dma_kw;
12156169Shx147065 	IWK_DBG((IWK_DEBUG_DMA, "kw[ncookies:%d addr:%lx size:%lx]\n",
12166169Shx147065 	    dma_p->ncookies, dma_p->cookie.dmac_address,
12176169Shx147065 	    dma_p->cookie.dmac_size));
12186169Shx147065 
12196169Shx147065 	return (err);
12206169Shx147065 fail:
12216169Shx147065 	iwk_free_kw(sc);
12226169Shx147065 	return (err);
12236169Shx147065 }
12246169Shx147065 
12256169Shx147065 static void
iwk_free_kw(iwk_sc_t * sc)12266169Shx147065 iwk_free_kw(iwk_sc_t *sc)
12276169Shx147065 {
12286169Shx147065 	iwk_free_dma_mem(&sc->sc_dma_kw);
12296169Shx147065 }
12306169Shx147065 
12316169Shx147065 static int
iwk_alloc_rx_ring(iwk_sc_t * sc)12326169Shx147065 iwk_alloc_rx_ring(iwk_sc_t *sc)
12336169Shx147065 {
12346169Shx147065 	iwk_rx_ring_t *ring;
12356169Shx147065 	iwk_rx_data_t *data;
12366169Shx147065 	iwk_dma_t *dma_p;
12376169Shx147065 	int i, err = DDI_SUCCESS;
12386169Shx147065 
12396169Shx147065 	ring = &sc->sc_rxq;
12406169Shx147065 	ring->cur = 0;
12416169Shx147065 
12426169Shx147065 	err = iwk_alloc_dma_mem(sc, RX_QUEUE_SIZE * sizeof (uint32_t),
12439327SFei.Feng@Sun.COM 	    &ring_desc_dma_attr, &iwk_dma_descattr,
12446169Shx147065 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
12456169Shx147065 	    &ring->dma_desc);
12466169Shx147065 	if (err != DDI_SUCCESS) {
12477691SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "dma alloc rx ring desc failed\n");
12486169Shx147065 		goto fail;
12496169Shx147065 	}
12506169Shx147065 	ring->desc = (uint32_t *)ring->dma_desc.mem_va;
12516169Shx147065 	dma_p = &ring->dma_desc;
12526169Shx147065 	IWK_DBG((IWK_DEBUG_DMA, "rx bd[ncookies:%d addr:%lx size:%lx]\n",
12536169Shx147065 	    dma_p->ncookies, dma_p->cookie.dmac_address,
12546169Shx147065 	    dma_p->cookie.dmac_size));
12556169Shx147065 
12566169Shx147065 	/*
12576169Shx147065 	 * Allocate Rx buffers.
12586169Shx147065 	 */
12596169Shx147065 	for (i = 0; i < RX_QUEUE_SIZE; i++) {
12606169Shx147065 		data = &ring->data[i];
12616169Shx147065 		err = iwk_alloc_dma_mem(sc, sc->sc_dmabuf_sz,
12626169Shx147065 		    &rx_buffer_dma_attr, &iwk_dma_accattr,
12636169Shx147065 		    DDI_DMA_READ | DDI_DMA_STREAMING,
12646169Shx147065 		    &data->dma_data);
12656169Shx147065 		if (err != DDI_SUCCESS) {
12667691SPengcheng.Chen@Sun.COM 			cmn_err(CE_WARN, "dma alloc rx ring buf[%d] "
12677691SPengcheng.Chen@Sun.COM 			    "failed\n", i);
12686169Shx147065 			goto fail;
12696169Shx147065 		}
12706169Shx147065 		/*
12716169Shx147065 		 * the physical address bit [8-36] are used,
12726169Shx147065 		 * instead of bit [0-31] in 3945.
12736169Shx147065 		 */
12749327SFei.Feng@Sun.COM 		ring->desc[i] = (uint32_t)
12759327SFei.Feng@Sun.COM 		    (data->dma_data.cookie.dmac_address >> 8);
12766169Shx147065 	}
12776169Shx147065 	dma_p = &ring->data[0].dma_data;
12786169Shx147065 	IWK_DBG((IWK_DEBUG_DMA, "rx buffer[0][ncookies:%d addr:%lx "
12796169Shx147065 	    "size:%lx]\n",
12806169Shx147065 	    dma_p->ncookies, dma_p->cookie.dmac_address,
12816169Shx147065 	    dma_p->cookie.dmac_size));
12826169Shx147065 
12836169Shx147065 	IWK_DMA_SYNC(ring->dma_desc, DDI_DMA_SYNC_FORDEV);
12846169Shx147065 
12856169Shx147065 	return (err);
12866169Shx147065 
12876169Shx147065 fail:
12886169Shx147065 	iwk_free_rx_ring(sc);
12896169Shx147065 	return (err);
12906169Shx147065 }
12916169Shx147065 
12926169Shx147065 static void
iwk_reset_rx_ring(iwk_sc_t * sc)12936169Shx147065 iwk_reset_rx_ring(iwk_sc_t *sc)
12946169Shx147065 {
12956169Shx147065 	int n;
12966169Shx147065 
12976169Shx147065 	iwk_mac_access_enter(sc);
12986169Shx147065 	IWK_WRITE(sc, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
12996169Shx147065 	for (n = 0; n < 2000; n++) {
13006169Shx147065 		if (IWK_READ(sc, FH_MEM_RSSR_RX_STATUS_REG) & (1 << 24))
13016169Shx147065 			break;
13026169Shx147065 		DELAY(1000);
13036169Shx147065 	}
13047891SPengcheng.Chen@Sun.COM 
13056169Shx147065 	if (n == 2000)
13066169Shx147065 		IWK_DBG((IWK_DEBUG_DMA, "timeout resetting Rx ring\n"));
13077891SPengcheng.Chen@Sun.COM 
13086169Shx147065 	iwk_mac_access_exit(sc);
13096169Shx147065 
13106169Shx147065 	sc->sc_rxq.cur = 0;
13116169Shx147065 }
13126169Shx147065 
13136169Shx147065 static void
iwk_free_rx_ring(iwk_sc_t * sc)13146169Shx147065 iwk_free_rx_ring(iwk_sc_t *sc)
13156169Shx147065 {
13166169Shx147065 	int i;
13176169Shx147065 
13186169Shx147065 	for (i = 0; i < RX_QUEUE_SIZE; i++) {
13196169Shx147065 		if (sc->sc_rxq.data[i].dma_data.dma_hdl)
13206169Shx147065 			IWK_DMA_SYNC(sc->sc_rxq.data[i].dma_data,
13216169Shx147065 			    DDI_DMA_SYNC_FORCPU);
13226169Shx147065 		iwk_free_dma_mem(&sc->sc_rxq.data[i].dma_data);
13236169Shx147065 	}
13246169Shx147065 
13256169Shx147065 	if (sc->sc_rxq.dma_desc.dma_hdl)
13266169Shx147065 		IWK_DMA_SYNC(sc->sc_rxq.dma_desc, DDI_DMA_SYNC_FORDEV);
13276169Shx147065 	iwk_free_dma_mem(&sc->sc_rxq.dma_desc);
13286169Shx147065 }
13296169Shx147065 
13306169Shx147065 static int
iwk_alloc_tx_ring(iwk_sc_t * sc,iwk_tx_ring_t * ring,int slots,int qid)13316169Shx147065 iwk_alloc_tx_ring(iwk_sc_t *sc, iwk_tx_ring_t *ring,
13326169Shx147065     int slots, int qid)
13336169Shx147065 {
13346169Shx147065 	iwk_tx_data_t *data;
13356169Shx147065 	iwk_tx_desc_t *desc_h;
13366169Shx147065 	uint32_t paddr_desc_h;
13376169Shx147065 	iwk_cmd_t *cmd_h;
13386169Shx147065 	uint32_t paddr_cmd_h;
13396169Shx147065 	iwk_dma_t *dma_p;
13406169Shx147065 	int i, err = DDI_SUCCESS;
13416169Shx147065 
13426169Shx147065 	ring->qid = qid;
13436169Shx147065 	ring->count = TFD_QUEUE_SIZE_MAX;
13446169Shx147065 	ring->window = slots;
13456169Shx147065 	ring->queued = 0;
13466169Shx147065 	ring->cur = 0;
13476169Shx147065 
13486169Shx147065 	err = iwk_alloc_dma_mem(sc,
13496169Shx147065 	    TFD_QUEUE_SIZE_MAX * sizeof (iwk_tx_desc_t),
13509327SFei.Feng@Sun.COM 	    &ring_desc_dma_attr, &iwk_dma_descattr,
13516169Shx147065 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
13526169Shx147065 	    &ring->dma_desc);
13536169Shx147065 	if (err != DDI_SUCCESS) {
13547691SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "dma alloc tx ring desc[%d] "
13557691SPengcheng.Chen@Sun.COM 		    "failed\n", qid);
13566169Shx147065 		goto fail;
13576169Shx147065 	}
13586169Shx147065 	dma_p = &ring->dma_desc;
13596169Shx147065 	IWK_DBG((IWK_DEBUG_DMA, "tx bd[ncookies:%d addr:%lx size:%lx]\n",
13606169Shx147065 	    dma_p->ncookies, dma_p->cookie.dmac_address,
13616169Shx147065 	    dma_p->cookie.dmac_size));
13626169Shx147065 
13636169Shx147065 	desc_h = (iwk_tx_desc_t *)ring->dma_desc.mem_va;
13646169Shx147065 	paddr_desc_h = ring->dma_desc.cookie.dmac_address;
13656169Shx147065 
13666169Shx147065 	err = iwk_alloc_dma_mem(sc,
13676169Shx147065 	    TFD_QUEUE_SIZE_MAX * sizeof (iwk_cmd_t),
13686169Shx147065 	    &cmd_dma_attr, &iwk_dma_accattr,
13696169Shx147065 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
13706169Shx147065 	    &ring->dma_cmd);
13716169Shx147065 	if (err != DDI_SUCCESS) {
13727691SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "dma alloc tx ring cmd[%d] "
13737691SPengcheng.Chen@Sun.COM 		    "failed\n", qid);
13746169Shx147065 		goto fail;
13756169Shx147065 	}
13766169Shx147065 	dma_p = &ring->dma_cmd;
13776169Shx147065 	IWK_DBG((IWK_DEBUG_DMA, "tx cmd[ncookies:%d addr:%lx size:%lx]\n",
13786169Shx147065 	    dma_p->ncookies, dma_p->cookie.dmac_address,
13796169Shx147065 	    dma_p->cookie.dmac_size));
13806169Shx147065 
13816169Shx147065 	cmd_h = (iwk_cmd_t *)ring->dma_cmd.mem_va;
13826169Shx147065 	paddr_cmd_h = ring->dma_cmd.cookie.dmac_address;
13836169Shx147065 
13846169Shx147065 	/*
13856169Shx147065 	 * Allocate Tx buffers.
13866169Shx147065 	 */
13876169Shx147065 	ring->data = kmem_zalloc(sizeof (iwk_tx_data_t) * TFD_QUEUE_SIZE_MAX,
13886169Shx147065 	    KM_NOSLEEP);
13896169Shx147065 	if (ring->data == NULL) {
13907691SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "could not allocate tx data slots\n");
13916169Shx147065 		goto fail;
13926169Shx147065 	}
13936169Shx147065 
13946169Shx147065 	for (i = 0; i < TFD_QUEUE_SIZE_MAX; i++) {
13956169Shx147065 		data = &ring->data[i];
13966169Shx147065 		err = iwk_alloc_dma_mem(sc, sc->sc_dmabuf_sz,
13976169Shx147065 		    &tx_buffer_dma_attr, &iwk_dma_accattr,
13986169Shx147065 		    DDI_DMA_WRITE | DDI_DMA_STREAMING,
13996169Shx147065 		    &data->dma_data);
14006169Shx147065 		if (err != DDI_SUCCESS) {
14017691SPengcheng.Chen@Sun.COM 			cmn_err(CE_WARN, "dma alloc tx ring "
14027691SPengcheng.Chen@Sun.COM 			    "buf[%d] failed\n", i);
14036169Shx147065 			goto fail;
14046169Shx147065 		}
14056169Shx147065 
14066169Shx147065 		data->desc = desc_h + i;
14076169Shx147065 		data->paddr_desc = paddr_desc_h +
14087249Sff224033 		    _PTRDIFF(data->desc, desc_h);
14096169Shx147065 		data->cmd = cmd_h +  i; /* (i % slots); */
14107569SXinghua.Wen@Sun.COM 		/* ((i % slots) * sizeof (iwk_cmd_t)); */
14116169Shx147065 		data->paddr_cmd = paddr_cmd_h +
14127249Sff224033 		    _PTRDIFF(data->cmd, cmd_h);
14136169Shx147065 	}
14146169Shx147065 	dma_p = &ring->data[0].dma_data;
14156169Shx147065 	IWK_DBG((IWK_DEBUG_DMA, "tx buffer[0][ncookies:%d addr:%lx "
14166169Shx147065 	    "size:%lx]\n",
14176169Shx147065 	    dma_p->ncookies, dma_p->cookie.dmac_address,
14186169Shx147065 	    dma_p->cookie.dmac_size));
14196169Shx147065 
14206169Shx147065 	return (err);
14216169Shx147065 
14226169Shx147065 fail:
14236169Shx147065 	if (ring->data)
14246169Shx147065 		kmem_free(ring->data,
14256169Shx147065 		    sizeof (iwk_tx_data_t) * TFD_QUEUE_SIZE_MAX);
14266169Shx147065 	iwk_free_tx_ring(sc, ring);
14276169Shx147065 	return (err);
14286169Shx147065 }
14296169Shx147065 
14306169Shx147065 static void
iwk_reset_tx_ring(iwk_sc_t * sc,iwk_tx_ring_t * ring)14316169Shx147065 iwk_reset_tx_ring(iwk_sc_t *sc, iwk_tx_ring_t *ring)
14326169Shx147065 {
14336169Shx147065 	iwk_tx_data_t *data;
14346169Shx147065 	int i, n;
14356169Shx147065 
14366169Shx147065 	iwk_mac_access_enter(sc);
14376169Shx147065 
14386169Shx147065 	IWK_WRITE(sc, IWK_FH_TCSR_CHNL_TX_CONFIG_REG(ring->qid), 0);
14396169Shx147065 	for (n = 0; n < 200; n++) {
14406169Shx147065 		if (IWK_READ(sc, IWK_FH_TSSR_TX_STATUS_REG) &
14416169Shx147065 		    IWK_FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ring->qid))
14426169Shx147065 			break;
14436169Shx147065 		DELAY(10);
14446169Shx147065 	}
14457691SPengcheng.Chen@Sun.COM 	if (n == 200) {
14466169Shx147065 		IWK_DBG((IWK_DEBUG_DMA, "timeout reset tx ring %d\n",
14476169Shx147065 		    ring->qid));
14486169Shx147065 	}
14496169Shx147065 	iwk_mac_access_exit(sc);
14506169Shx147065 
14516169Shx147065 	for (i = 0; i < ring->count; i++) {
14526169Shx147065 		data = &ring->data[i];
14536169Shx147065 		IWK_DMA_SYNC(data->dma_data, DDI_DMA_SYNC_FORDEV);
14546169Shx147065 	}
14556169Shx147065 
14566169Shx147065 	ring->queued = 0;
14576169Shx147065 	ring->cur = 0;
14586169Shx147065 }
14596169Shx147065 
14606169Shx147065 /*ARGSUSED*/
14616169Shx147065 static void
iwk_free_tx_ring(iwk_sc_t * sc,iwk_tx_ring_t * ring)14626169Shx147065 iwk_free_tx_ring(iwk_sc_t *sc, iwk_tx_ring_t *ring)
14636169Shx147065 {
14646169Shx147065 	int i;
14656169Shx147065 
14666169Shx147065 	if (ring->dma_desc.dma_hdl != NULL)
14676169Shx147065 		IWK_DMA_SYNC(ring->dma_desc, DDI_DMA_SYNC_FORDEV);
14686169Shx147065 	iwk_free_dma_mem(&ring->dma_desc);
14696169Shx147065 
14706169Shx147065 	if (ring->dma_cmd.dma_hdl != NULL)
14716169Shx147065 		IWK_DMA_SYNC(ring->dma_cmd, DDI_DMA_SYNC_FORDEV);
14726169Shx147065 	iwk_free_dma_mem(&ring->dma_cmd);
14736169Shx147065 
14746169Shx147065 	if (ring->data != NULL) {
14756169Shx147065 		for (i = 0; i < ring->count; i++) {
14766169Shx147065 			if (ring->data[i].dma_data.dma_hdl)
14776169Shx147065 				IWK_DMA_SYNC(ring->data[i].dma_data,
14786169Shx147065 				    DDI_DMA_SYNC_FORDEV);
14796169Shx147065 			iwk_free_dma_mem(&ring->data[i].dma_data);
14806169Shx147065 		}
14816169Shx147065 		kmem_free(ring->data, ring->count * sizeof (iwk_tx_data_t));
14826169Shx147065 	}
14836169Shx147065 }
14846169Shx147065 
14856169Shx147065 static int
iwk_ring_init(iwk_sc_t * sc)14866169Shx147065 iwk_ring_init(iwk_sc_t *sc)
14876169Shx147065 {
14886169Shx147065 	int i, err = DDI_SUCCESS;
14896169Shx147065 
14906169Shx147065 	for (i = 0; i < IWK_NUM_QUEUES; i++) {
14916169Shx147065 		if (i == IWK_CMD_QUEUE_NUM)
14926169Shx147065 			continue;
14936169Shx147065 		err = iwk_alloc_tx_ring(sc, &sc->sc_txq[i], TFD_TX_CMD_SLOTS,
14946169Shx147065 		    i);
14956169Shx147065 		if (err != DDI_SUCCESS)
14966169Shx147065 			goto fail;
14976169Shx147065 	}
14986169Shx147065 	err = iwk_alloc_tx_ring(sc, &sc->sc_txq[IWK_CMD_QUEUE_NUM],
14996169Shx147065 	    TFD_CMD_SLOTS, IWK_CMD_QUEUE_NUM);
15006169Shx147065 	if (err != DDI_SUCCESS)
15016169Shx147065 		goto fail;
15026169Shx147065 	err = iwk_alloc_rx_ring(sc);
15036169Shx147065 	if (err != DDI_SUCCESS)
15046169Shx147065 		goto fail;
15056169Shx147065 	return (err);
15066169Shx147065 
15076169Shx147065 fail:
15086169Shx147065 	return (err);
15096169Shx147065 }
15106169Shx147065 
15116169Shx147065 static void
iwk_ring_free(iwk_sc_t * sc)15126169Shx147065 iwk_ring_free(iwk_sc_t *sc)
15136169Shx147065 {
15146169Shx147065 	int i = IWK_NUM_QUEUES;
15156169Shx147065 
15166169Shx147065 	iwk_free_rx_ring(sc);
15176169Shx147065 	while (--i >= 0) {
15186169Shx147065 		iwk_free_tx_ring(sc, &sc->sc_txq[i]);
15196169Shx147065 	}
15206169Shx147065 }
15216169Shx147065 
15226249Shx147065 /* ARGSUSED */
15236249Shx147065 static ieee80211_node_t *
iwk_node_alloc(ieee80211com_t * ic)15246249Shx147065 iwk_node_alloc(ieee80211com_t *ic)
15256249Shx147065 {
15266249Shx147065 	iwk_amrr_t *amrr;
15276249Shx147065 
15286249Shx147065 	amrr = kmem_zalloc(sizeof (iwk_amrr_t), KM_SLEEP);
15296249Shx147065 	if (amrr != NULL)
15306249Shx147065 		iwk_amrr_init(amrr);
15316249Shx147065 	return (&amrr->in);
15326249Shx147065 }
15336249Shx147065 
15346249Shx147065 static void
iwk_node_free(ieee80211_node_t * in)15356249Shx147065 iwk_node_free(ieee80211_node_t *in)
15366249Shx147065 {
15376249Shx147065 	ieee80211com_t *ic = in->in_ic;
15386249Shx147065 
15396249Shx147065 	ic->ic_node_cleanup(in);
15406249Shx147065 	if (in->in_wpa_ie != NULL)
15416249Shx147065 		ieee80211_free(in->in_wpa_ie);
15426249Shx147065 	kmem_free(in, sizeof (iwk_amrr_t));
15436249Shx147065 }
15446249Shx147065 
15456169Shx147065 /*ARGSUSED*/
15466169Shx147065 static int
iwk_newstate(ieee80211com_t * ic,enum ieee80211_state nstate,int arg)15476169Shx147065 iwk_newstate(ieee80211com_t *ic, enum ieee80211_state nstate, int arg)
15486169Shx147065 {
15496169Shx147065 	iwk_sc_t *sc = (iwk_sc_t *)ic;
15506249Shx147065 	ieee80211_node_t *in = ic->ic_bss;
15516169Shx147065 	enum ieee80211_state ostate = ic->ic_state;
15526169Shx147065 	int i, err = IWK_SUCCESS;
15536169Shx147065 
15546169Shx147065 	mutex_enter(&sc->sc_glock);
15556169Shx147065 	switch (nstate) {
15566169Shx147065 	case IEEE80211_S_SCAN:
15577691SPengcheng.Chen@Sun.COM 		switch (ostate) {
15587691SPengcheng.Chen@Sun.COM 		case IEEE80211_S_INIT:
15597691SPengcheng.Chen@Sun.COM 		{
15607691SPengcheng.Chen@Sun.COM 			iwk_add_sta_t node;
15617691SPengcheng.Chen@Sun.COM 
15627691SPengcheng.Chen@Sun.COM 			sc->sc_flags |= IWK_F_SCANNING;
15638727SPengcheng.Chen@Sun.COM 			sc->sc_scan_pending = 0;
15646169Shx147065 			iwk_set_led(sc, 2, 10, 2);
15656169Shx147065 
15667691SPengcheng.Chen@Sun.COM 			/*
15677691SPengcheng.Chen@Sun.COM 			 * clear association to receive beacons from
15687691SPengcheng.Chen@Sun.COM 			 * all BSS'es
15697691SPengcheng.Chen@Sun.COM 			 */
15707691SPengcheng.Chen@Sun.COM 			sc->sc_config.assoc_id = 0;
15717691SPengcheng.Chen@Sun.COM 			sc->sc_config.filter_flags &=
15727691SPengcheng.Chen@Sun.COM 			    ~LE_32(RXON_FILTER_ASSOC_MSK);
15737691SPengcheng.Chen@Sun.COM 
15747691SPengcheng.Chen@Sun.COM 			IWK_DBG((IWK_DEBUG_80211, "config chan %d "
15757691SPengcheng.Chen@Sun.COM 			    "flags %x filter_flags %x\n", sc->sc_config.chan,
15767691SPengcheng.Chen@Sun.COM 			    sc->sc_config.flags, sc->sc_config.filter_flags));
15777691SPengcheng.Chen@Sun.COM 
15787691SPengcheng.Chen@Sun.COM 			err = iwk_cmd(sc, REPLY_RXON, &sc->sc_config,
15797691SPengcheng.Chen@Sun.COM 			    sizeof (iwk_rxon_cmd_t), 1);
15807691SPengcheng.Chen@Sun.COM 			if (err != IWK_SUCCESS) {
15817691SPengcheng.Chen@Sun.COM 				cmn_err(CE_WARN,
15827691SPengcheng.Chen@Sun.COM 				    "could not clear association\n");
15837691SPengcheng.Chen@Sun.COM 				sc->sc_flags &= ~IWK_F_SCANNING;
15846169Shx147065 				mutex_exit(&sc->sc_glock);
15856169Shx147065 				return (err);
15866169Shx147065 			}
15877691SPengcheng.Chen@Sun.COM 
15887691SPengcheng.Chen@Sun.COM 			/* add broadcast node to send probe request */
15897691SPengcheng.Chen@Sun.COM 			(void) memset(&node, 0, sizeof (node));
15907691SPengcheng.Chen@Sun.COM 			(void) memset(&node.bssid, 0xff, IEEE80211_ADDR_LEN);
15917691SPengcheng.Chen@Sun.COM 			node.id = IWK_BROADCAST_ID;
15927691SPengcheng.Chen@Sun.COM 			err = iwk_cmd(sc, REPLY_ADD_STA, &node,
15937691SPengcheng.Chen@Sun.COM 			    sizeof (node), 1);
15947691SPengcheng.Chen@Sun.COM 			if (err != IWK_SUCCESS) {
15957691SPengcheng.Chen@Sun.COM 				cmn_err(CE_WARN, "could not add "
15967691SPengcheng.Chen@Sun.COM 				    "broadcast node\n");
15977691SPengcheng.Chen@Sun.COM 				sc->sc_flags &= ~IWK_F_SCANNING;
15987691SPengcheng.Chen@Sun.COM 				mutex_exit(&sc->sc_glock);
15997691SPengcheng.Chen@Sun.COM 				return (err);
16007691SPengcheng.Chen@Sun.COM 			}
16017691SPengcheng.Chen@Sun.COM 			break;
16027691SPengcheng.Chen@Sun.COM 		}
16038727SPengcheng.Chen@Sun.COM 
16048727SPengcheng.Chen@Sun.COM 		case IEEE80211_S_AUTH:
16058727SPengcheng.Chen@Sun.COM 		case IEEE80211_S_ASSOC:
16068727SPengcheng.Chen@Sun.COM 		case IEEE80211_S_RUN:
16078727SPengcheng.Chen@Sun.COM 			sc->sc_flags |= IWK_F_SCANNING;
16088727SPengcheng.Chen@Sun.COM 			sc->sc_scan_pending = 0;
16098727SPengcheng.Chen@Sun.COM 
16108727SPengcheng.Chen@Sun.COM 			iwk_set_led(sc, 2, 10, 2);
16118727SPengcheng.Chen@Sun.COM 			/* FALLTHRU */
16127691SPengcheng.Chen@Sun.COM 		case IEEE80211_S_SCAN:
16137691SPengcheng.Chen@Sun.COM 			mutex_exit(&sc->sc_glock);
16147691SPengcheng.Chen@Sun.COM 			/* step to next channel before actual FW scan */
16157691SPengcheng.Chen@Sun.COM 			err = sc->sc_newstate(ic, nstate, arg);
16167691SPengcheng.Chen@Sun.COM 			mutex_enter(&sc->sc_glock);
16177691SPengcheng.Chen@Sun.COM 			if ((err != 0) || ((err = iwk_scan(sc)) != 0)) {
16187691SPengcheng.Chen@Sun.COM 				cmn_err(CE_WARN,
16197691SPengcheng.Chen@Sun.COM 				    "could not initiate scan\n");
16207691SPengcheng.Chen@Sun.COM 				sc->sc_flags &= ~IWK_F_SCANNING;
16217691SPengcheng.Chen@Sun.COM 				ieee80211_cancel_scan(ic);
16227691SPengcheng.Chen@Sun.COM 			}
16237691SPengcheng.Chen@Sun.COM 			mutex_exit(&sc->sc_glock);
16247691SPengcheng.Chen@Sun.COM 			return (err);
16257691SPengcheng.Chen@Sun.COM 		default:
16267691SPengcheng.Chen@Sun.COM 			break;
16277691SPengcheng.Chen@Sun.COM 
16286169Shx147065 		}
16296249Shx147065 		sc->sc_clk = 0;
16307691SPengcheng.Chen@Sun.COM 		break;
16316169Shx147065 
16326169Shx147065 	case IEEE80211_S_AUTH:
16337691SPengcheng.Chen@Sun.COM 		if (ostate == IEEE80211_S_SCAN) {
16347691SPengcheng.Chen@Sun.COM 			sc->sc_flags &= ~IWK_F_SCANNING;
16357691SPengcheng.Chen@Sun.COM 		}
16367691SPengcheng.Chen@Sun.COM 
16376169Shx147065 		/* reset state to handle reassociations correctly */
16386169Shx147065 		sc->sc_config.assoc_id = 0;
16396169Shx147065 		sc->sc_config.filter_flags &= ~LE_32(RXON_FILTER_ASSOC_MSK);
16406169Shx147065 
16416169Shx147065 		/*
16426169Shx147065 		 * before sending authentication and association request frame,
16436169Shx147065 		 * we need do something in the hardware, such as setting the
16446169Shx147065 		 * channel same to the target AP...
16456169Shx147065 		 */
16466169Shx147065 		if ((err = iwk_hw_set_before_auth(sc)) != 0) {
16477691SPengcheng.Chen@Sun.COM 			cmn_err(CE_WARN, "could not setup firmware for "
16487691SPengcheng.Chen@Sun.COM 			    "authentication\n");
16496169Shx147065 			mutex_exit(&sc->sc_glock);
16506169Shx147065 			return (err);
16516169Shx147065 		}
16526169Shx147065 		break;
16536169Shx147065 
16546169Shx147065 	case IEEE80211_S_RUN:
16557691SPengcheng.Chen@Sun.COM 		if (ostate == IEEE80211_S_SCAN) {
16567691SPengcheng.Chen@Sun.COM 			sc->sc_flags &= ~IWK_F_SCANNING;
16577691SPengcheng.Chen@Sun.COM 		}
16587691SPengcheng.Chen@Sun.COM 
16596169Shx147065 		if (ic->ic_opmode == IEEE80211_M_MONITOR) {
16606169Shx147065 			/* let LED blink when monitoring */
16616169Shx147065 			iwk_set_led(sc, 2, 10, 10);
16626169Shx147065 			break;
16636169Shx147065 		}
16647569SXinghua.Wen@Sun.COM 		IWK_DBG((IWK_DEBUG_80211, "iwk: associated."));
16657569SXinghua.Wen@Sun.COM 
16668594SFei.Feng@Sun.COM 		/* IBSS mode */
16678594SFei.Feng@Sun.COM 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
16688594SFei.Feng@Sun.COM 			/*
16698594SFei.Feng@Sun.COM 			 * clean all nodes in ibss node table
16708594SFei.Feng@Sun.COM 			 * in order to be consistent with hardware
16718594SFei.Feng@Sun.COM 			 */
16728594SFei.Feng@Sun.COM 			err = iwk_run_state_config_ibss(ic);
16738594SFei.Feng@Sun.COM 			if (err != IWK_SUCCESS) {
16748594SFei.Feng@Sun.COM 				cmn_err(CE_WARN, "iwk_newstate(): "
16758594SFei.Feng@Sun.COM 				    "failed to update configuration "
16768594SFei.Feng@Sun.COM 				    "in IBSS mode\n");
16778594SFei.Feng@Sun.COM 				mutex_exit(&sc->sc_glock);
16788594SFei.Feng@Sun.COM 				return (err);
16798594SFei.Feng@Sun.COM 			}
16808594SFei.Feng@Sun.COM 		}
16818594SFei.Feng@Sun.COM 
16827569SXinghua.Wen@Sun.COM 		/* none IBSS mode */
16837569SXinghua.Wen@Sun.COM 		if (ic->ic_opmode != IEEE80211_M_IBSS) {
16847569SXinghua.Wen@Sun.COM 			/* update adapter's configuration */
16858594SFei.Feng@Sun.COM 			err = iwk_run_state_config_sta(ic);
16867569SXinghua.Wen@Sun.COM 			if (err != IWK_SUCCESS) {
16878594SFei.Feng@Sun.COM 				cmn_err(CE_WARN, "iwk_newstate(): "
16888594SFei.Feng@Sun.COM 				    "failed to update configuration "
16898594SFei.Feng@Sun.COM 				    "in none IBSS mode\n");
16907569SXinghua.Wen@Sun.COM 				mutex_exit(&sc->sc_glock);
16917569SXinghua.Wen@Sun.COM 				return (err);
16927569SXinghua.Wen@Sun.COM 			}
16936169Shx147065 		}
16947569SXinghua.Wen@Sun.COM 
16957569SXinghua.Wen@Sun.COM 		/* obtain current temperature of chipset */
16967569SXinghua.Wen@Sun.COM 		sc->sc_tempera = iwk_curr_tempera(sc);
16976169Shx147065 
16986169Shx147065 		/*
16997569SXinghua.Wen@Sun.COM 		 * make Tx power calibration to determine
17007569SXinghua.Wen@Sun.COM 		 * the gains of DSP and radio
17016169Shx147065 		 */
17027569SXinghua.Wen@Sun.COM 		err = iwk_tx_power_calibration(sc);
17037569SXinghua.Wen@Sun.COM 		if (err) {
17047569SXinghua.Wen@Sun.COM 			cmn_err(CE_WARN, "iwk_newstate(): "
17057569SXinghua.Wen@Sun.COM 			    "failed to set tx power table\n");
17068594SFei.Feng@Sun.COM 			mutex_exit(&sc->sc_glock);
17076169Shx147065 			return (err);
17086169Shx147065 		}
17096169Shx147065 
17108594SFei.Feng@Sun.COM 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
17118594SFei.Feng@Sun.COM 
17128594SFei.Feng@Sun.COM 			/*
17138594SFei.Feng@Sun.COM 			 * allocate and transmit beacon frames
17148594SFei.Feng@Sun.COM 			 */
17158594SFei.Feng@Sun.COM 			err = iwk_start_tx_beacon(ic);
17168594SFei.Feng@Sun.COM 			if (err != IWK_SUCCESS) {
17178594SFei.Feng@Sun.COM 				cmn_err(CE_WARN, "iwk_newstate(): "
17188594SFei.Feng@Sun.COM 				    "can't transmit beacon frames\n");
17198594SFei.Feng@Sun.COM 				mutex_exit(&sc->sc_glock);
17208594SFei.Feng@Sun.COM 				return (err);
17218594SFei.Feng@Sun.COM 			}
17228594SFei.Feng@Sun.COM 		}
17238594SFei.Feng@Sun.COM 
17246249Shx147065 		/* start automatic rate control */
17256249Shx147065 		mutex_enter(&sc->sc_mt_lock);
17266249Shx147065 		if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
17276249Shx147065 			sc->sc_flags |= IWK_F_RATE_AUTO_CTL;
17286249Shx147065 			/* set rate to some reasonable initial value */
17296249Shx147065 			i = in->in_rates.ir_nrates - 1;
17306249Shx147065 			while (i > 0 && IEEE80211_RATE(i) > 72)
17316249Shx147065 				i--;
17326249Shx147065 			in->in_txrate = i;
17336249Shx147065 		} else {
17346249Shx147065 			sc->sc_flags &= ~IWK_F_RATE_AUTO_CTL;
17356249Shx147065 		}
17366249Shx147065 		mutex_exit(&sc->sc_mt_lock);
17376249Shx147065 
17386169Shx147065 		/* set LED on after associated */
17396169Shx147065 		iwk_set_led(sc, 2, 0, 1);
17406169Shx147065 		break;
17416169Shx147065 
17426169Shx147065 	case IEEE80211_S_INIT:
17437691SPengcheng.Chen@Sun.COM 		if (ostate == IEEE80211_S_SCAN) {
17447691SPengcheng.Chen@Sun.COM 			sc->sc_flags &= ~IWK_F_SCANNING;
17457691SPengcheng.Chen@Sun.COM 		}
17467691SPengcheng.Chen@Sun.COM 
17476169Shx147065 		/* set LED off after init */
17486169Shx147065 		iwk_set_led(sc, 2, 1, 0);
17496169Shx147065 		break;
17506169Shx147065 	case IEEE80211_S_ASSOC:
17517691SPengcheng.Chen@Sun.COM 		if (ostate == IEEE80211_S_SCAN) {
17527691SPengcheng.Chen@Sun.COM 			sc->sc_flags &= ~IWK_F_SCANNING;
17537691SPengcheng.Chen@Sun.COM 		}
17547691SPengcheng.Chen@Sun.COM 
17556169Shx147065 		break;
17566169Shx147065 	}
17576169Shx147065 
17586169Shx147065 	mutex_exit(&sc->sc_glock);
17597569SXinghua.Wen@Sun.COM 
17607569SXinghua.Wen@Sun.COM 	err = sc->sc_newstate(ic, nstate, arg);
17617569SXinghua.Wen@Sun.COM 
17627569SXinghua.Wen@Sun.COM 	if (nstate == IEEE80211_S_RUN) {
17637569SXinghua.Wen@Sun.COM 
17647569SXinghua.Wen@Sun.COM 		mutex_enter(&sc->sc_glock);
17657569SXinghua.Wen@Sun.COM 
17667569SXinghua.Wen@Sun.COM 		/*
17677569SXinghua.Wen@Sun.COM 		 * make initialization for Receiver
17687569SXinghua.Wen@Sun.COM 		 * sensitivity calibration
17697569SXinghua.Wen@Sun.COM 		 */
17707569SXinghua.Wen@Sun.COM 		err = iwk_rx_sens_init(sc);
17717569SXinghua.Wen@Sun.COM 		if (err) {
17727569SXinghua.Wen@Sun.COM 			cmn_err(CE_WARN, "iwk_newstate(): "
17737569SXinghua.Wen@Sun.COM 			    "failed to init RX sensitivity\n");
17747691SPengcheng.Chen@Sun.COM 			mutex_exit(&sc->sc_glock);
17757569SXinghua.Wen@Sun.COM 			return (err);
17767569SXinghua.Wen@Sun.COM 		}
17777569SXinghua.Wen@Sun.COM 
17787569SXinghua.Wen@Sun.COM 		/* make initialization for Receiver gain balance */
17797569SXinghua.Wen@Sun.COM 		err = iwk_rxgain_diff_init(sc);
17807569SXinghua.Wen@Sun.COM 		if (err) {
17817569SXinghua.Wen@Sun.COM 			cmn_err(CE_WARN, "iwk_newstate(): "
17827569SXinghua.Wen@Sun.COM 			    "failed to init phy calibration\n");
17837691SPengcheng.Chen@Sun.COM 			mutex_exit(&sc->sc_glock);
17847569SXinghua.Wen@Sun.COM 			return (err);
17857569SXinghua.Wen@Sun.COM 		}
17867569SXinghua.Wen@Sun.COM 
17877569SXinghua.Wen@Sun.COM 		mutex_exit(&sc->sc_glock);
17887569SXinghua.Wen@Sun.COM 
17897569SXinghua.Wen@Sun.COM 	}
17907569SXinghua.Wen@Sun.COM 
17917569SXinghua.Wen@Sun.COM 	return (err);
17926169Shx147065 }
17936169Shx147065 
17948727SPengcheng.Chen@Sun.COM static void
iwk_watchdog(void * arg)17958727SPengcheng.Chen@Sun.COM iwk_watchdog(void *arg)
17968727SPengcheng.Chen@Sun.COM {
17978727SPengcheng.Chen@Sun.COM 	iwk_sc_t *sc = arg;
17988727SPengcheng.Chen@Sun.COM 	struct ieee80211com *ic = &sc->sc_ic;
17998727SPengcheng.Chen@Sun.COM #ifdef DEBUG
18008727SPengcheng.Chen@Sun.COM 	timeout_id_t timeout_id = ic->ic_watchdog_timer;
18018727SPengcheng.Chen@Sun.COM #endif
18028727SPengcheng.Chen@Sun.COM 
18038727SPengcheng.Chen@Sun.COM 	ieee80211_stop_watchdog(ic);
18048727SPengcheng.Chen@Sun.COM 
18058727SPengcheng.Chen@Sun.COM 	if ((ic->ic_state != IEEE80211_S_AUTH) &&
18068727SPengcheng.Chen@Sun.COM 	    (ic->ic_state != IEEE80211_S_ASSOC))
18078727SPengcheng.Chen@Sun.COM 		return;
18088727SPengcheng.Chen@Sun.COM 
18098727SPengcheng.Chen@Sun.COM 	if (ic->ic_bss->in_fails > 0) {
18108727SPengcheng.Chen@Sun.COM 		IWK_DBG((IWK_DEBUG_80211, "watchdog (0x%x) reset: "
18118727SPengcheng.Chen@Sun.COM 		    "node (0x%x)\n", timeout_id, &ic->ic_bss));
18128727SPengcheng.Chen@Sun.COM 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
18138727SPengcheng.Chen@Sun.COM 	} else {
18148727SPengcheng.Chen@Sun.COM 		IWK_DBG((IWK_DEBUG_80211, "watchdog (0x%x) timeout: "
18158727SPengcheng.Chen@Sun.COM 		    "node (0x%x), retry (%d)\n",
18168727SPengcheng.Chen@Sun.COM 		    timeout_id, &ic->ic_bss, ic->ic_bss->in_fails + 1));
18178727SPengcheng.Chen@Sun.COM 		ieee80211_watchdog(ic);
18188727SPengcheng.Chen@Sun.COM 	}
18198727SPengcheng.Chen@Sun.COM }
18208727SPengcheng.Chen@Sun.COM 
18216169Shx147065 /*ARGSUSED*/
iwk_key_set(ieee80211com_t * ic,const struct ieee80211_key * k,const uint8_t mac[IEEE80211_ADDR_LEN])18226169Shx147065 static int iwk_key_set(ieee80211com_t *ic, const struct ieee80211_key *k,
18236169Shx147065     const uint8_t mac[IEEE80211_ADDR_LEN])
18246169Shx147065 {
18256169Shx147065 	iwk_sc_t *sc = (iwk_sc_t *)ic;
18266169Shx147065 	iwk_add_sta_t node;
18276169Shx147065 	int err;
18288594SFei.Feng@Sun.COM 	uint8_t index1;
18296169Shx147065 
18306169Shx147065 	switch (k->wk_cipher->ic_cipher) {
18316169Shx147065 	case IEEE80211_CIPHER_WEP:
18326169Shx147065 	case IEEE80211_CIPHER_TKIP:
18336169Shx147065 		return (1); /* sofeware do it. */
18346169Shx147065 	case IEEE80211_CIPHER_AES_CCM:
18356169Shx147065 		break;
18366169Shx147065 	default:
18376169Shx147065 		return (0);
18386169Shx147065 	}
18399327SFei.Feng@Sun.COM 	sc->sc_config.filter_flags &= ~LE_32(RXON_FILTER_DIS_DECRYPT_MSK |
18407569SXinghua.Wen@Sun.COM 	    RXON_FILTER_DIS_GRP_DECRYPT_MSK);
18416169Shx147065 
18426169Shx147065 	mutex_enter(&sc->sc_glock);
18436169Shx147065 
18446169Shx147065 	/* update ap/multicast node */
18456169Shx147065 	(void) memset(&node, 0, sizeof (node));
18466169Shx147065 	if (IEEE80211_IS_MULTICAST(mac)) {
18476169Shx147065 		(void) memset(node.bssid, 0xff, 6);
18486169Shx147065 		node.id = IWK_BROADCAST_ID;
18498594SFei.Feng@Sun.COM 	} else if (ic->ic_opmode == IEEE80211_M_IBSS) {
18508594SFei.Feng@Sun.COM 		mutex_exit(&sc->sc_glock);
18518594SFei.Feng@Sun.COM 		mutex_enter(&sc->sc_ibss.node_tb_lock);
18528594SFei.Feng@Sun.COM 
18538594SFei.Feng@Sun.COM 		/*
18548594SFei.Feng@Sun.COM 		 * search for node in ibss node table
18558594SFei.Feng@Sun.COM 		 */
18568594SFei.Feng@Sun.COM 		for (index1 = IWK_STA_ID; index1 < IWK_STATION_COUNT;
18578594SFei.Feng@Sun.COM 		    index1++) {
18588594SFei.Feng@Sun.COM 			if (sc->sc_ibss.ibss_node_tb[index1].used &&
18598594SFei.Feng@Sun.COM 			    IEEE80211_ADDR_EQ(sc->sc_ibss.
18608594SFei.Feng@Sun.COM 			    ibss_node_tb[index1].node.bssid,
18618594SFei.Feng@Sun.COM 			    mac)) {
18628594SFei.Feng@Sun.COM 				break;
18638594SFei.Feng@Sun.COM 			}
18648594SFei.Feng@Sun.COM 		}
18658594SFei.Feng@Sun.COM 		if (index1 >= IWK_BROADCAST_ID) {
18668594SFei.Feng@Sun.COM 			cmn_err(CE_WARN, "iwk_key_set(): "
18678594SFei.Feng@Sun.COM 			    "have no this node in hardware node table\n");
18688594SFei.Feng@Sun.COM 			mutex_exit(&sc->sc_ibss.node_tb_lock);
18698594SFei.Feng@Sun.COM 			return (0);
18708594SFei.Feng@Sun.COM 		} else {
18718594SFei.Feng@Sun.COM 			/*
18728594SFei.Feng@Sun.COM 			 * configure key for given node in hardware
18738594SFei.Feng@Sun.COM 			 */
18748594SFei.Feng@Sun.COM 			if (k->wk_flags & IEEE80211_KEY_XMIT) {
18758594SFei.Feng@Sun.COM 				sc->sc_ibss.ibss_node_tb[index1].
18768594SFei.Feng@Sun.COM 				    node.key_flags = 0;
18778594SFei.Feng@Sun.COM 				sc->sc_ibss.ibss_node_tb[index1].
18788594SFei.Feng@Sun.COM 				    node.keyp = k->wk_keyix;
18798594SFei.Feng@Sun.COM 			} else {
18808594SFei.Feng@Sun.COM 				sc->sc_ibss.ibss_node_tb[index1].
18818594SFei.Feng@Sun.COM 				    node.key_flags = (1 << 14);
18828594SFei.Feng@Sun.COM 				sc->sc_ibss.ibss_node_tb[index1].
18838594SFei.Feng@Sun.COM 				    node.keyp = k->wk_keyix + 4;
18848594SFei.Feng@Sun.COM 			}
18858594SFei.Feng@Sun.COM 
18868594SFei.Feng@Sun.COM 			(void) memcpy(sc->sc_ibss.ibss_node_tb[index1].node.key,
18878594SFei.Feng@Sun.COM 			    k->wk_key, k->wk_keylen);
18888594SFei.Feng@Sun.COM 			sc->sc_ibss.ibss_node_tb[index1].node.key_flags |=
18898594SFei.Feng@Sun.COM 			    (STA_KEY_FLG_CCMP | (1 << 3) | (k->wk_keyix << 8));
18909327SFei.Feng@Sun.COM 			sc->sc_ibss.ibss_node_tb[index1].node.key_flags =
18919327SFei.Feng@Sun.COM 			    LE_16(sc->sc_ibss.ibss_node_tb[index1].
18929327SFei.Feng@Sun.COM 			    node.key_flags);
18938594SFei.Feng@Sun.COM 			sc->sc_ibss.ibss_node_tb[index1].node.sta_mask =
18948594SFei.Feng@Sun.COM 			    STA_MODIFY_KEY_MASK;
18958594SFei.Feng@Sun.COM 			sc->sc_ibss.ibss_node_tb[index1].node.control = 1;
18968594SFei.Feng@Sun.COM 
18978594SFei.Feng@Sun.COM 			mutex_enter(&sc->sc_glock);
18988594SFei.Feng@Sun.COM 			err = iwk_cmd(sc, REPLY_ADD_STA,
18998594SFei.Feng@Sun.COM 			    &sc->sc_ibss.ibss_node_tb[index1].node,
19008594SFei.Feng@Sun.COM 			    sizeof (iwk_add_sta_t), 1);
19018594SFei.Feng@Sun.COM 			if (err != IWK_SUCCESS) {
19028594SFei.Feng@Sun.COM 				cmn_err(CE_WARN, "iwk_key_set(): "
19038594SFei.Feng@Sun.COM 				    "failed to update IBSS node in hardware\n");
19048594SFei.Feng@Sun.COM 				mutex_exit(&sc->sc_glock);
19058594SFei.Feng@Sun.COM 				mutex_exit(&sc->sc_ibss.node_tb_lock);
19068594SFei.Feng@Sun.COM 				return (0);
19078594SFei.Feng@Sun.COM 			}
19088594SFei.Feng@Sun.COM 			mutex_exit(&sc->sc_glock);
19098594SFei.Feng@Sun.COM 		}
19108594SFei.Feng@Sun.COM 		mutex_exit(&sc->sc_ibss.node_tb_lock);
19118594SFei.Feng@Sun.COM 		return (1);
19126169Shx147065 	} else {
19136169Shx147065 		IEEE80211_ADDR_COPY(node.bssid, ic->ic_bss->in_bssid);
19146169Shx147065 		node.id = IWK_AP_ID;
19156169Shx147065 	}
19166169Shx147065 	if (k->wk_flags & IEEE80211_KEY_XMIT) {
19176169Shx147065 		node.key_flags = 0;
19186169Shx147065 		node.keyp = k->wk_keyix;
19196169Shx147065 	} else {
19206169Shx147065 		node.key_flags = (1 << 14);
19216169Shx147065 		node.keyp = k->wk_keyix + 4;
19226169Shx147065 	}
19236169Shx147065 	(void) memcpy(node.key, k->wk_key, k->wk_keylen);
19246169Shx147065 	node.key_flags |= (STA_KEY_FLG_CCMP | (1 << 3) | (k->wk_keyix << 8));
19259327SFei.Feng@Sun.COM 	node.key_flags = LE_16(node.key_flags);
19266169Shx147065 	node.sta_mask = STA_MODIFY_KEY_MASK;
19276169Shx147065 	node.control = 1;
19286169Shx147065 	err = iwk_cmd(sc, REPLY_ADD_STA, &node, sizeof (node), 1);
19296169Shx147065 	if (err != IWK_SUCCESS) {
19306169Shx147065 		cmn_err(CE_WARN, "iwk_key_set():"
19316169Shx147065 		    "failed to update ap node\n");
19326169Shx147065 		mutex_exit(&sc->sc_glock);
19336169Shx147065 		return (0);
19346169Shx147065 	}
19356169Shx147065 	mutex_exit(&sc->sc_glock);
19366169Shx147065 	return (1);
19376169Shx147065 }
19386169Shx147065 
19396169Shx147065 /*
19406169Shx147065  * exclusive access to mac begin.
19416169Shx147065  */
19426169Shx147065 static void
iwk_mac_access_enter(iwk_sc_t * sc)19436169Shx147065 iwk_mac_access_enter(iwk_sc_t *sc)
19446169Shx147065 {
19456169Shx147065 	uint32_t tmp;
19466169Shx147065 	int n;
19476169Shx147065 
19486169Shx147065 	tmp = IWK_READ(sc, CSR_GP_CNTRL);
19496169Shx147065 	IWK_WRITE(sc, CSR_GP_CNTRL,
19506169Shx147065 	    tmp | CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
19516169Shx147065 
19526169Shx147065 	/* wait until we succeed */
19536169Shx147065 	for (n = 0; n < 1000; n++) {
19546169Shx147065 		if ((IWK_READ(sc, CSR_GP_CNTRL) &
19556169Shx147065 		    (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
19566169Shx147065 		    CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP)) ==
19576169Shx147065 		    CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN)
19586169Shx147065 			break;
19596169Shx147065 		DELAY(10);
19606169Shx147065 	}
19616169Shx147065 	if (n == 1000)
19626169Shx147065 		IWK_DBG((IWK_DEBUG_PIO, "could not lock memory\n"));
19636169Shx147065 }
19646169Shx147065 
19656169Shx147065 /*
19666169Shx147065  * exclusive access to mac end.
19676169Shx147065  */
19686169Shx147065 static void
iwk_mac_access_exit(iwk_sc_t * sc)19696169Shx147065 iwk_mac_access_exit(iwk_sc_t *sc)
19706169Shx147065 {
19716169Shx147065 	uint32_t tmp = IWK_READ(sc, CSR_GP_CNTRL);
19726169Shx147065 	IWK_WRITE(sc, CSR_GP_CNTRL,
19736169Shx147065 	    tmp & ~CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
19746169Shx147065 }
19756169Shx147065 
19767569SXinghua.Wen@Sun.COM static uint32_t
iwk_mem_read(iwk_sc_t * sc,uint32_t addr)19777569SXinghua.Wen@Sun.COM iwk_mem_read(iwk_sc_t *sc, uint32_t addr)
19787569SXinghua.Wen@Sun.COM {
19797569SXinghua.Wen@Sun.COM 	IWK_WRITE(sc, HBUS_TARG_MEM_RADDR, addr);
19807569SXinghua.Wen@Sun.COM 	return (IWK_READ(sc, HBUS_TARG_MEM_RDAT));
19817569SXinghua.Wen@Sun.COM }
19826169Shx147065 
19836169Shx147065 static void
iwk_mem_write(iwk_sc_t * sc,uint32_t addr,uint32_t data)19846169Shx147065 iwk_mem_write(iwk_sc_t *sc, uint32_t addr, uint32_t data)
19856169Shx147065 {
19866169Shx147065 	IWK_WRITE(sc, HBUS_TARG_MEM_WADDR, addr);
19876169Shx147065 	IWK_WRITE(sc, HBUS_TARG_MEM_WDAT, data);
19886169Shx147065 }
19896169Shx147065 
19906169Shx147065 static uint32_t
iwk_reg_read(iwk_sc_t * sc,uint32_t addr)19916169Shx147065 iwk_reg_read(iwk_sc_t *sc, uint32_t addr)
19926169Shx147065 {
19936169Shx147065 	IWK_WRITE(sc, HBUS_TARG_PRPH_RADDR, addr | (3 << 24));
19946169Shx147065 	return (IWK_READ(sc, HBUS_TARG_PRPH_RDAT));
19956169Shx147065 }
19966169Shx147065 
19976169Shx147065 static void
iwk_reg_write(iwk_sc_t * sc,uint32_t addr,uint32_t data)19986169Shx147065 iwk_reg_write(iwk_sc_t *sc, uint32_t addr, uint32_t data)
19996169Shx147065 {
20006169Shx147065 	IWK_WRITE(sc, HBUS_TARG_PRPH_WADDR, addr | (3 << 24));
20016169Shx147065 	IWK_WRITE(sc, HBUS_TARG_PRPH_WDAT, data);
20026169Shx147065 }
20036169Shx147065 
20046169Shx147065 static void
iwk_reg_write_region_4(iwk_sc_t * sc,uint32_t addr,uint32_t * data,int wlen)20056169Shx147065 iwk_reg_write_region_4(iwk_sc_t *sc, uint32_t addr,
20066169Shx147065     uint32_t *data, int wlen)
20076169Shx147065 {
20086169Shx147065 	for (; wlen > 0; wlen--, data++, addr += 4)
20099327SFei.Feng@Sun.COM 		iwk_reg_write(sc, addr, LE_32(*data));
20106169Shx147065 }
20116169Shx147065 
20126169Shx147065 
20136169Shx147065 /*
20146169Shx147065  * ucode load/initialization steps:
20156169Shx147065  * 1)  load Bootstrap State Machine (BSM) with "bootstrap" uCode image.
20166169Shx147065  * BSM contains a small memory that *always* stays powered up, so it can
20176169Shx147065  * retain the bootstrap program even when the card is in a power-saving
20186169Shx147065  * power-down state.  The BSM loads the small program into ARC processor's
20196169Shx147065  * instruction memory when triggered by power-up.
20206169Shx147065  * 2)  load Initialize image via bootstrap program.
20216169Shx147065  * The Initialize image sets up regulatory and calibration data for the
20226169Shx147065  * Runtime/Protocol uCode. This sends a REPLY_ALIVE notification when completed.
20236169Shx147065  * The 4965 reply contains calibration data for temperature, voltage and tx gain
20246169Shx147065  * correction.
20256169Shx147065  */
20266169Shx147065 static int
iwk_load_firmware(iwk_sc_t * sc)20276169Shx147065 iwk_load_firmware(iwk_sc_t *sc)
20286169Shx147065 {
20296169Shx147065 	uint32_t *boot_fw = (uint32_t *)sc->sc_boot;
20309327SFei.Feng@Sun.COM 	uint32_t size = LE_32(sc->sc_hdr->bootsz);
20316169Shx147065 	int n, err = IWK_SUCCESS;
20326169Shx147065 
20336169Shx147065 	/*
20346169Shx147065 	 * The physical address bit [4-35] of the initialize uCode.
20356169Shx147065 	 * In the initialize alive notify interrupt the physical address of
20366169Shx147065 	 * the runtime ucode will be set for loading.
20376169Shx147065 	 */
20386169Shx147065 	iwk_mac_access_enter(sc);
20396169Shx147065 
20406169Shx147065 	iwk_reg_write(sc, BSM_DRAM_INST_PTR_REG,
20416169Shx147065 	    sc->sc_dma_fw_init_text.cookie.dmac_address >> 4);
20426169Shx147065 	iwk_reg_write(sc, BSM_DRAM_DATA_PTR_REG,
20436169Shx147065 	    sc->sc_dma_fw_init_data.cookie.dmac_address >> 4);
20446169Shx147065 	iwk_reg_write(sc, BSM_DRAM_INST_BYTECOUNT_REG,
20456169Shx147065 	    sc->sc_dma_fw_init_text.cookie.dmac_size);
20466169Shx147065 	iwk_reg_write(sc, BSM_DRAM_DATA_BYTECOUNT_REG,
20476169Shx147065 	    sc->sc_dma_fw_init_data.cookie.dmac_size);
20486169Shx147065 
20496169Shx147065 	/* load bootstrap code into BSM memory */
20506169Shx147065 	iwk_reg_write_region_4(sc, BSM_SRAM_LOWER_BOUND, boot_fw,
20516169Shx147065 	    size / sizeof (uint32_t));
20526169Shx147065 
20536169Shx147065 	iwk_reg_write(sc, BSM_WR_MEM_SRC_REG, 0);
20546169Shx147065 	iwk_reg_write(sc, BSM_WR_MEM_DST_REG, RTC_INST_LOWER_BOUND);
20556169Shx147065 	iwk_reg_write(sc, BSM_WR_DWCOUNT_REG, size / sizeof (uint32_t));
20566169Shx147065 
20576169Shx147065 	/*
20586169Shx147065 	 * prepare to load initialize uCode
20596169Shx147065 	 */
20606169Shx147065 	iwk_reg_write(sc, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START);
20616169Shx147065 
20626169Shx147065 	/* wait while the adapter is busy loading the firmware */
20636169Shx147065 	for (n = 0; n < 1000; n++) {
20646169Shx147065 		if (!(iwk_reg_read(sc, BSM_WR_CTRL_REG) &
20656169Shx147065 		    BSM_WR_CTRL_REG_BIT_START))
20666169Shx147065 			break;
20676169Shx147065 		DELAY(10);
20686169Shx147065 	}
20696169Shx147065 	if (n == 1000) {
20707691SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "timeout transferring firmware\n");
20716169Shx147065 		err = ETIMEDOUT;
20726169Shx147065 		return (err);
20736169Shx147065 	}
20746169Shx147065 
20756169Shx147065 	/* for future power-save mode use */
20766169Shx147065 	iwk_reg_write(sc, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START_EN);
20776169Shx147065 
20786169Shx147065 	iwk_mac_access_exit(sc);
20796169Shx147065 
20806169Shx147065 	return (err);
20816169Shx147065 }
20826169Shx147065 
20836169Shx147065 /*ARGSUSED*/
20846169Shx147065 static void
iwk_rx_intr(iwk_sc_t * sc,iwk_rx_desc_t * desc,iwk_rx_data_t * data)20856169Shx147065 iwk_rx_intr(iwk_sc_t *sc, iwk_rx_desc_t *desc, iwk_rx_data_t *data)
20866169Shx147065 {
20876169Shx147065 	ieee80211com_t *ic = &sc->sc_ic;
20886169Shx147065 	iwk_rx_ring_t *ring = &sc->sc_rxq;
20896169Shx147065 	iwk_rx_phy_res_t *stat;
20906169Shx147065 	ieee80211_node_t *in;
20916169Shx147065 	uint32_t *tail;
20926169Shx147065 	struct ieee80211_frame *wh;
20936169Shx147065 	mblk_t *mp;
20946169Shx147065 	uint16_t len, rssi, mrssi, agc;
20956169Shx147065 	int16_t t;
20966169Shx147065 	uint32_t ants, i;
20976169Shx147065 	struct iwk_rx_non_cfg_phy *phyinfo;
20989327SFei.Feng@Sun.COM 	uint32_t crc;
20996169Shx147065 
21006169Shx147065 	/* assuming not 11n here. cope with 11n in phase-II */
21016169Shx147065 	stat = (iwk_rx_phy_res_t *)(desc + 1);
21026169Shx147065 	if (stat->cfg_phy_cnt > 20) {
21036169Shx147065 		return;
21046169Shx147065 	}
21056169Shx147065 
21069327SFei.Feng@Sun.COM 	for (i = 0; i < RX_RES_PHY_CNT; i++)
21079327SFei.Feng@Sun.COM 		stat->non_cfg_phy[i] = LE_16(stat->non_cfg_phy[i]);
21089327SFei.Feng@Sun.COM 
21096169Shx147065 	phyinfo = (struct iwk_rx_non_cfg_phy *)stat->non_cfg_phy;
21106169Shx147065 	agc = (phyinfo->agc_info & IWK_AGC_DB_MASK) >> IWK_AGC_DB_POS;
21116169Shx147065 	mrssi = 0;
21129327SFei.Feng@Sun.COM 	ants = (LE_16(stat->phy_flags) & RX_PHY_FLAGS_ANTENNAE_MASK) >>
21137569SXinghua.Wen@Sun.COM 	    RX_PHY_FLAGS_ANTENNAE_OFFSET;
21146169Shx147065 	for (i = 0; i < 3; i++) {
21156169Shx147065 		if (ants & (1 << i))
21166169Shx147065 			mrssi = MAX(mrssi, phyinfo->rssi_info[i << 1]);
21176169Shx147065 	}
21186169Shx147065 	t = mrssi - agc - 44; /* t is the dBM value */
21196169Shx147065 	/*
21206169Shx147065 	 * convert dBm to percentage ???
21216169Shx147065 	 */
21227569SXinghua.Wen@Sun.COM 	rssi = (100 * 75 * 75 - (-20 - t) * (15 * 75 + 62 * (-20 - t))) /
21237569SXinghua.Wen@Sun.COM 	    (75 * 75);
21246169Shx147065 	if (rssi > 100)
21256169Shx147065 		rssi = 100;
21266169Shx147065 	if (rssi < 1)
21276169Shx147065 		rssi = 1;
21289327SFei.Feng@Sun.COM 	len = LE_16(stat->byte_count);
21299327SFei.Feng@Sun.COM 	tail = (uint32_t *)((caddr_t)(stat + 1) + stat->cfg_phy_cnt + len);
21309327SFei.Feng@Sun.COM 	bcopy(tail, &crc, 4);
21316169Shx147065 
21326169Shx147065 	IWK_DBG((IWK_DEBUG_RX, "rx intr: idx=%d phy_len=%x len=%d "
21336169Shx147065 	    "rate=%x chan=%d tstamp=%x non_cfg_phy_count=%x "
21346169Shx147065 	    "cfg_phy_count=%x tail=%x", ring->cur, sizeof (*stat),
21359327SFei.Feng@Sun.COM 	    len, stat->rate.r.s.rate, LE_16(stat->channel),
21366169Shx147065 	    LE_32(stat->timestampl), stat->non_cfg_phy_cnt,
21379327SFei.Feng@Sun.COM 	    stat->cfg_phy_cnt, LE_32(crc)));
21386169Shx147065 
21396169Shx147065 	if ((len < 16) || (len > sc->sc_dmabuf_sz)) {
21406169Shx147065 		IWK_DBG((IWK_DEBUG_RX, "rx frame oversize\n"));
21416169Shx147065 		return;
21426169Shx147065 	}
21436169Shx147065 
21446169Shx147065 	/*
21456169Shx147065 	 * discard Rx frames with bad CRC
21466169Shx147065 	 */
21479327SFei.Feng@Sun.COM 	if ((LE_32(crc) &
21486169Shx147065 	    (RX_RES_STATUS_NO_CRC32_ERROR | RX_RES_STATUS_NO_RXE_OVERFLOW)) !=
21496169Shx147065 	    (RX_RES_STATUS_NO_CRC32_ERROR | RX_RES_STATUS_NO_RXE_OVERFLOW)) {
21506169Shx147065 		IWK_DBG((IWK_DEBUG_RX, "rx crc error tail: %x\n",
21519327SFei.Feng@Sun.COM 		    LE_32(crc)));
21526169Shx147065 		sc->sc_rx_err++;
21536169Shx147065 		return;
21546169Shx147065 	}
21556169Shx147065 
21566169Shx147065 	wh = (struct ieee80211_frame *)
21576169Shx147065 	    ((uint8_t *)(stat + 1)+ stat->cfg_phy_cnt);
21586169Shx147065 	if (*(uint8_t *)wh == IEEE80211_FC0_SUBTYPE_ASSOC_RESP) {
21596169Shx147065 		sc->sc_assoc_id = *((uint16_t *)(wh + 1) + 2);
21606169Shx147065 		IWK_DBG((IWK_DEBUG_RX, "rx : association id = %x\n",
21616169Shx147065 		    sc->sc_assoc_id));
21626169Shx147065 	}
21636169Shx147065 #ifdef DEBUG
21646169Shx147065 	if (iwk_dbg_flags & IWK_DEBUG_RX)
21656169Shx147065 		ieee80211_dump_pkt((uint8_t *)wh, len, 0, 0);
21666169Shx147065 #endif
21676169Shx147065 	in = ieee80211_find_rxnode(ic, wh);
21686169Shx147065 	mp = allocb(len, BPRI_MED);
21696169Shx147065 	if (mp) {
21706169Shx147065 		(void) memcpy(mp->b_wptr, wh, len);
21716169Shx147065 		mp->b_wptr += len;
21726169Shx147065 
21736169Shx147065 		/* send the frame to the 802.11 layer */
21746169Shx147065 		(void) ieee80211_input(ic, mp, in, rssi, 0);
21756169Shx147065 	} else {
21766169Shx147065 		sc->sc_rx_nobuf++;
21776169Shx147065 		IWK_DBG((IWK_DEBUG_RX,
21786169Shx147065 		    "iwk_rx_intr(): alloc rx buf failed\n"));
21796169Shx147065 	}
21806169Shx147065 	/* release node reference */
21816169Shx147065 	ieee80211_free_node(in);
21826169Shx147065 }
21836169Shx147065 
21846169Shx147065 /*ARGSUSED*/
21856169Shx147065 static void
iwk_tx_intr(iwk_sc_t * sc,iwk_rx_desc_t * desc,iwk_rx_data_t * data)21866169Shx147065 iwk_tx_intr(iwk_sc_t *sc, iwk_rx_desc_t *desc, iwk_rx_data_t *data)
21876169Shx147065 {
21886169Shx147065 	ieee80211com_t *ic = &sc->sc_ic;
21896169Shx147065 	iwk_tx_ring_t *ring = &sc->sc_txq[desc->hdr.qid & 0x3];
21906169Shx147065 	iwk_tx_stat_t *stat = (iwk_tx_stat_t *)(desc + 1);
21916249Shx147065 	iwk_amrr_t *amrr = (iwk_amrr_t *)ic->ic_bss;
21926169Shx147065 
21936169Shx147065 	IWK_DBG((IWK_DEBUG_TX, "tx done: qid=%d idx=%d"
21946169Shx147065 	    " retries=%d frame_count=%x nkill=%d "
21956169Shx147065 	    "rate=%x duration=%d status=%x\n",
21966169Shx147065 	    desc->hdr.qid, desc->hdr.idx, stat->ntries, stat->frame_count,
21976169Shx147065 	    stat->bt_kill_count, stat->rate.r.s.rate,
21989327SFei.Feng@Sun.COM 	    LE_16(stat->duration), LE_32(stat->status)));
21996169Shx147065 
22006249Shx147065 	amrr->txcnt++;
22016249Shx147065 	IWK_DBG((IWK_DEBUG_RATECTL, "tx: %d cnt\n", amrr->txcnt));
22026169Shx147065 	if (stat->ntries > 0) {
22036249Shx147065 		amrr->retrycnt++;
22046169Shx147065 		sc->sc_tx_retries++;
22056169Shx147065 		IWK_DBG((IWK_DEBUG_TX, "tx: %d retries\n",
22066169Shx147065 		    sc->sc_tx_retries));
22076169Shx147065 	}
22086169Shx147065 
22096169Shx147065 	sc->sc_tx_timer = 0;
22106169Shx147065 
22116169Shx147065 	mutex_enter(&sc->sc_tx_lock);
22126169Shx147065 	ring->queued--;
22136169Shx147065 	if (ring->queued < 0)
22146169Shx147065 		ring->queued = 0;
22156169Shx147065 	if ((sc->sc_need_reschedule) && (ring->queued <= (ring->count << 3))) {
22166169Shx147065 		sc->sc_need_reschedule = 0;
22176169Shx147065 		mutex_exit(&sc->sc_tx_lock);
22186169Shx147065 		mac_tx_update(ic->ic_mach);
22196169Shx147065 		mutex_enter(&sc->sc_tx_lock);
22206169Shx147065 	}
22216169Shx147065 	mutex_exit(&sc->sc_tx_lock);
22226169Shx147065 }
22236169Shx147065 
22246169Shx147065 static void
iwk_cmd_intr(iwk_sc_t * sc,iwk_rx_desc_t * desc)22256169Shx147065 iwk_cmd_intr(iwk_sc_t *sc, iwk_rx_desc_t *desc)
22266169Shx147065 {
22276169Shx147065 	if ((desc->hdr.qid & 7) != 4) {
22286169Shx147065 		return;
22296169Shx147065 	}
22306169Shx147065 	mutex_enter(&sc->sc_glock);
22316169Shx147065 	sc->sc_flags |= IWK_F_CMD_DONE;
22326169Shx147065 	cv_signal(&sc->sc_cmd_cv);
22336169Shx147065 	mutex_exit(&sc->sc_glock);
22346169Shx147065 	IWK_DBG((IWK_DEBUG_CMD, "rx cmd: "
22356169Shx147065 	    "qid=%x idx=%d flags=%x type=0x%x\n",
22366169Shx147065 	    desc->hdr.qid, desc->hdr.idx, desc->hdr.flags,
22376169Shx147065 	    desc->hdr.type));
22386169Shx147065 }
22396169Shx147065 
22406169Shx147065 static void
iwk_ucode_alive(iwk_sc_t * sc,iwk_rx_desc_t * desc)22416169Shx147065 iwk_ucode_alive(iwk_sc_t *sc, iwk_rx_desc_t *desc)
22426169Shx147065 {
22436169Shx147065 	uint32_t base, i;
22446169Shx147065 	struct iwk_alive_resp *ar =
22456169Shx147065 	    (struct iwk_alive_resp *)(desc + 1);
22466169Shx147065 
22476169Shx147065 	/* the microcontroller is ready */
22486169Shx147065 	IWK_DBG((IWK_DEBUG_FW,
22496169Shx147065 	    "microcode alive notification minor: %x major: %x type:"
22506169Shx147065 	    " %x subtype: %x\n",
22516169Shx147065 	    ar->ucode_minor, ar->ucode_minor, ar->ver_type, ar->ver_subtype));
22526169Shx147065 
22536169Shx147065 	if (LE_32(ar->is_valid) != UCODE_VALID_OK) {
22546169Shx147065 		IWK_DBG((IWK_DEBUG_FW,
22556169Shx147065 		    "microcontroller initialization failed\n"));
22566169Shx147065 	}
22576169Shx147065 	if (ar->ver_subtype == INITIALIZE_SUBTYPE) {
22586169Shx147065 		IWK_DBG((IWK_DEBUG_FW,
22596169Shx147065 		    "initialization alive received.\n"));
22606169Shx147065 		(void) memcpy(&sc->sc_card_alive_init, ar,
22616169Shx147065 		    sizeof (struct iwk_init_alive_resp));
22626169Shx147065 		/* XXX get temperature */
22636169Shx147065 		iwk_mac_access_enter(sc);
22646169Shx147065 		iwk_reg_write(sc, BSM_DRAM_INST_PTR_REG,
22656169Shx147065 		    sc->sc_dma_fw_text.cookie.dmac_address >> 4);
22666169Shx147065 		iwk_reg_write(sc, BSM_DRAM_DATA_PTR_REG,
22676169Shx147065 		    sc->sc_dma_fw_data_bak.cookie.dmac_address >> 4);
22686169Shx147065 		iwk_reg_write(sc, BSM_DRAM_DATA_BYTECOUNT_REG,
22696169Shx147065 		    sc->sc_dma_fw_data.cookie.dmac_size);
22706169Shx147065 		iwk_reg_write(sc, BSM_DRAM_INST_BYTECOUNT_REG,
22716169Shx147065 		    sc->sc_dma_fw_text.cookie.dmac_size | 0x80000000);
22726169Shx147065 		iwk_mac_access_exit(sc);
22736169Shx147065 	} else {
22746169Shx147065 		IWK_DBG((IWK_DEBUG_FW, "runtime alive received.\n"));
22756169Shx147065 		(void) memcpy(&sc->sc_card_alive_run, ar,
22766169Shx147065 		    sizeof (struct iwk_alive_resp));
22776169Shx147065 
22786169Shx147065 		/*
22796169Shx147065 		 * Init SCD related registers to make Tx work. XXX
22806169Shx147065 		 */
22816169Shx147065 		iwk_mac_access_enter(sc);
22826169Shx147065 
22836169Shx147065 		/* read sram address of data base */
22846169Shx147065 		sc->sc_scd_base = iwk_reg_read(sc, SCD_SRAM_BASE_ADDR);
22856169Shx147065 
22866169Shx147065 		/* clear and init SCD_CONTEXT_DATA_OFFSET area. 128 bytes */
22876169Shx147065 		for (base = sc->sc_scd_base + SCD_CONTEXT_DATA_OFFSET, i = 0;
22886169Shx147065 		    i < 128; i += 4)
22896169Shx147065 			iwk_mem_write(sc, base + i, 0);
22906169Shx147065 
22916169Shx147065 		/* clear and init SCD_TX_STTS_BITMAP_OFFSET area. 256 bytes */
22926169Shx147065 		for (base = sc->sc_scd_base + SCD_TX_STTS_BITMAP_OFFSET;
22936169Shx147065 		    i < 256; i += 4)
22946169Shx147065 			iwk_mem_write(sc, base + i, 0);
22956169Shx147065 
22966169Shx147065 		/* clear and init SCD_TRANSLATE_TBL_OFFSET area. 32 bytes */
22976169Shx147065 		for (base = sc->sc_scd_base + SCD_TRANSLATE_TBL_OFFSET;
22986169Shx147065 		    i < sizeof (uint16_t) * IWK_NUM_QUEUES; i += 4)
22996169Shx147065 			iwk_mem_write(sc, base + i, 0);
23006169Shx147065 
23016169Shx147065 		iwk_reg_write(sc, SCD_DRAM_BASE_ADDR,
23026169Shx147065 		    sc->sc_dma_sh.cookie.dmac_address >> 10);
23036169Shx147065 		iwk_reg_write(sc, SCD_QUEUECHAIN_SEL, 0);
23046169Shx147065 
23056169Shx147065 		/* initiate the tx queues */
23066169Shx147065 		for (i = 0; i < IWK_NUM_QUEUES; i++) {
23076169Shx147065 			iwk_reg_write(sc, SCD_QUEUE_RDPTR(i), 0);
23086169Shx147065 			IWK_WRITE(sc, HBUS_TARG_WRPTR, (i << 8));
23096169Shx147065 			iwk_mem_write(sc, sc->sc_scd_base +
23106169Shx147065 			    SCD_CONTEXT_QUEUE_OFFSET(i),
23116169Shx147065 			    (SCD_WIN_SIZE & 0x7f));
23126169Shx147065 			iwk_mem_write(sc, sc->sc_scd_base +
23136169Shx147065 			    SCD_CONTEXT_QUEUE_OFFSET(i) + sizeof (uint32_t),
23146169Shx147065 			    (SCD_FRAME_LIMIT & 0x7f) << 16);
23156169Shx147065 		}
23166169Shx147065 		/* interrupt enable on each queue0-7 */
23176169Shx147065 		iwk_reg_write(sc, SCD_INTERRUPT_MASK,
23186169Shx147065 		    (1 << IWK_NUM_QUEUES) - 1);
23196169Shx147065 		/* enable  each channel 0-7 */
23206169Shx147065 		iwk_reg_write(sc, SCD_TXFACT,
23216169Shx147065 		    SCD_TXFACT_REG_TXFIFO_MASK(0, 7));
23226169Shx147065 		/*
23236169Shx147065 		 * queue 0-7 maps to FIFO 0-7 and
23246169Shx147065 		 * all queues work under FIFO mode (none-scheduler-ack)
23256169Shx147065 		 */
23266169Shx147065 		for (i = 0; i < 7; i++) {
23276169Shx147065 			iwk_reg_write(sc,
23286169Shx147065 			    SCD_QUEUE_STATUS_BITS(i),
23296169Shx147065 			    (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
23306169Shx147065 			    (i << SCD_QUEUE_STTS_REG_POS_TXF)|
23316169Shx147065 			    SCD_QUEUE_STTS_REG_MSK);
23326169Shx147065 		}
23336169Shx147065 		iwk_mac_access_exit(sc);
23346169Shx147065 
23356169Shx147065 		sc->sc_flags |= IWK_F_FW_INIT;
23366169Shx147065 		cv_signal(&sc->sc_fw_cv);
23376169Shx147065 	}
23386169Shx147065 
23396169Shx147065 }
23406169Shx147065 
23416169Shx147065 static uint_t
23427569SXinghua.Wen@Sun.COM /* LINTED: argument unused in function: unused */
iwk_rx_softintr(caddr_t arg,caddr_t unused)23437569SXinghua.Wen@Sun.COM iwk_rx_softintr(caddr_t arg, caddr_t unused)
23446169Shx147065 {
23456169Shx147065 	iwk_sc_t *sc = (iwk_sc_t *)arg;
23466169Shx147065 	ieee80211com_t *ic = &sc->sc_ic;
23476169Shx147065 	iwk_rx_desc_t *desc;
23486169Shx147065 	iwk_rx_data_t *data;
23496169Shx147065 	uint32_t index;
23506169Shx147065 
23516169Shx147065 	mutex_enter(&sc->sc_glock);
23526169Shx147065 	if (sc->sc_rx_softint_pending != 1) {
23536169Shx147065 		mutex_exit(&sc->sc_glock);
23546169Shx147065 		return (DDI_INTR_UNCLAIMED);
23556169Shx147065 	}
23566169Shx147065 	/* disable interrupts */
23576169Shx147065 	IWK_WRITE(sc, CSR_INT_MASK, 0);
23586169Shx147065 	mutex_exit(&sc->sc_glock);
23596169Shx147065 
23606169Shx147065 	/*
23616169Shx147065 	 * firmware has moved the index of the rx queue, driver get it,
23626169Shx147065 	 * and deal with it.
23636169Shx147065 	 */
23649327SFei.Feng@Sun.COM 	index = sc->sc_shared->val0 & 0xfff;
23656169Shx147065 
23666169Shx147065 	while (sc->sc_rxq.cur != index) {
23676169Shx147065 		data = &sc->sc_rxq.data[sc->sc_rxq.cur];
23686169Shx147065 		desc = (iwk_rx_desc_t *)data->dma_data.mem_va;
23696169Shx147065 
23706169Shx147065 		IWK_DBG((IWK_DEBUG_INTR, "rx notification index = %d"
23716169Shx147065 		    " cur = %d qid=%x idx=%d flags=%x type=%x len=%d\n",
23726169Shx147065 		    index, sc->sc_rxq.cur, desc->hdr.qid, desc->hdr.idx,
23736169Shx147065 		    desc->hdr.flags, desc->hdr.type, LE_32(desc->len)));
23746169Shx147065 
23756169Shx147065 		/* a command other than a tx need to be replied */
23766169Shx147065 		if (!(desc->hdr.qid & 0x80) &&
23776169Shx147065 		    (desc->hdr.type != REPLY_RX_PHY_CMD) &&
23787569SXinghua.Wen@Sun.COM 		    (desc->hdr.type != REPLY_TX) &&
23797569SXinghua.Wen@Sun.COM 		    (desc->hdr.type != REPLY_TX_PWR_TABLE_CMD) &&
23807569SXinghua.Wen@Sun.COM 		    (desc->hdr.type != REPLY_PHY_CALIBRATION_CMD) &&
23817569SXinghua.Wen@Sun.COM 		    (desc->hdr.type != SENSITIVITY_CMD))
23826169Shx147065 			iwk_cmd_intr(sc, desc);
23836169Shx147065 
23846169Shx147065 		switch (desc->hdr.type) {
23856169Shx147065 		case REPLY_4965_RX:
23866169Shx147065 			iwk_rx_intr(sc, desc, data);
23876169Shx147065 			break;
23886169Shx147065 
23896169Shx147065 		case REPLY_TX:
23906169Shx147065 			iwk_tx_intr(sc, desc, data);
23916169Shx147065 			break;
23926169Shx147065 
23936169Shx147065 		case REPLY_ALIVE:
23946169Shx147065 			iwk_ucode_alive(sc, desc);
23956169Shx147065 			break;
23966169Shx147065 
23976169Shx147065 		case CARD_STATE_NOTIFICATION:
23986169Shx147065 		{
23996169Shx147065 			uint32_t *status = (uint32_t *)(desc + 1);
24006169Shx147065 
24016169Shx147065 			IWK_DBG((IWK_DEBUG_RADIO, "state changed to %x\n",
24026169Shx147065 			    LE_32(*status)));
24036169Shx147065 
24046169Shx147065 			if (LE_32(*status) & 1) {
24056249Shx147065 				/*
24066249Shx147065 				 * the radio button has to be pushed(OFF). It
24076249Shx147065 				 * is considered as a hw error, the
24086249Shx147065 				 * iwk_thread() tries to recover it after the
24096249Shx147065 				 * button is pushed again(ON)
24106249Shx147065 				 */
24116169Shx147065 				cmn_err(CE_NOTE,
24127569SXinghua.Wen@Sun.COM 				    "iwk_rx_softintr(): "
24137569SXinghua.Wen@Sun.COM 				    "Radio transmitter is off\n");
24146249Shx147065 				sc->sc_ostate = sc->sc_ic.ic_state;
24156249Shx147065 				ieee80211_new_state(&sc->sc_ic,
24166249Shx147065 				    IEEE80211_S_INIT, -1);
24176249Shx147065 				sc->sc_flags |=
24186249Shx147065 				    (IWK_F_HW_ERR_RECOVER | IWK_F_RADIO_OFF);
24196169Shx147065 			}
24206169Shx147065 			break;
24216169Shx147065 		}
24226169Shx147065 		case SCAN_START_NOTIFICATION:
24236169Shx147065 		{
24246169Shx147065 			iwk_start_scan_t *scan =
24256169Shx147065 			    (iwk_start_scan_t *)(desc + 1);
24266169Shx147065 
24276169Shx147065 			IWK_DBG((IWK_DEBUG_SCAN,
24286169Shx147065 			    "scanning channel %d status %x\n",
24296169Shx147065 			    scan->chan, LE_32(scan->status)));
24306169Shx147065 
24316169Shx147065 			ic->ic_curchan = &ic->ic_sup_channels[scan->chan];
24326169Shx147065 			break;
24336169Shx147065 		}
24346169Shx147065 		case SCAN_COMPLETE_NOTIFICATION:
24357691SPengcheng.Chen@Sun.COM 		{
24367691SPengcheng.Chen@Sun.COM 			iwk_stop_scan_t *scan =
24377691SPengcheng.Chen@Sun.COM 			    (iwk_stop_scan_t *)(desc + 1);
24387691SPengcheng.Chen@Sun.COM 
24397691SPengcheng.Chen@Sun.COM 			IWK_DBG((IWK_DEBUG_SCAN,
24407691SPengcheng.Chen@Sun.COM 			    "completed channel %d (burst of %d) status %02x\n",
24417691SPengcheng.Chen@Sun.COM 			    scan->chan, scan->nchan, scan->status));
24427691SPengcheng.Chen@Sun.COM 
24437691SPengcheng.Chen@Sun.COM 			sc->sc_scan_pending++;
24446169Shx147065 			break;
24457691SPengcheng.Chen@Sun.COM 		}
24467569SXinghua.Wen@Sun.COM 		case STATISTICS_NOTIFICATION:
24477569SXinghua.Wen@Sun.COM 			/* handle statistics notification */
24487569SXinghua.Wen@Sun.COM 			iwk_statistics_notify(sc, desc);
24497569SXinghua.Wen@Sun.COM 			break;
24507569SXinghua.Wen@Sun.COM 		}
24517569SXinghua.Wen@Sun.COM 
24526169Shx147065 		sc->sc_rxq.cur = (sc->sc_rxq.cur + 1) % RX_QUEUE_SIZE;
24536169Shx147065 	}
24546169Shx147065 
24556169Shx147065 	/*
24566169Shx147065 	 * driver dealt with what reveived in rx queue and tell the information
24576169Shx147065 	 * to the firmware.
24586169Shx147065 	 */
24596169Shx147065 	index = (index == 0) ? RX_QUEUE_SIZE - 1 : index - 1;
24606169Shx147065 	IWK_WRITE(sc, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, index & (~7));
24616169Shx147065 
24626169Shx147065 	mutex_enter(&sc->sc_glock);
24636169Shx147065 	/* re-enable interrupts */
24646169Shx147065 	IWK_WRITE(sc, CSR_INT_MASK, CSR_INI_SET_MASK);
24656169Shx147065 	sc->sc_rx_softint_pending = 0;
24666169Shx147065 	mutex_exit(&sc->sc_glock);
24676169Shx147065 
24686169Shx147065 	return (DDI_INTR_CLAIMED);
24696169Shx147065 }
24706169Shx147065 
24716169Shx147065 static uint_t
24727569SXinghua.Wen@Sun.COM /* LINTED: argument unused in function: unused */
iwk_intr(caddr_t arg,caddr_t unused)24737569SXinghua.Wen@Sun.COM iwk_intr(caddr_t arg, caddr_t unused)
24746169Shx147065 {
24756169Shx147065 	iwk_sc_t *sc = (iwk_sc_t *)arg;
24766169Shx147065 	uint32_t r, rfh;
24776169Shx147065 
24786169Shx147065 	mutex_enter(&sc->sc_glock);
24796795Shx147065 
24806795Shx147065 	if (sc->sc_flags & IWK_F_SUSPEND) {
24816795Shx147065 		mutex_exit(&sc->sc_glock);
24826795Shx147065 		return (DDI_INTR_UNCLAIMED);
24836795Shx147065 	}
24846795Shx147065 
24856169Shx147065 	r = IWK_READ(sc, CSR_INT);
24866169Shx147065 	if (r == 0 || r == 0xffffffff) {
24876169Shx147065 		mutex_exit(&sc->sc_glock);
24886169Shx147065 		return (DDI_INTR_UNCLAIMED);
24896169Shx147065 	}
24906169Shx147065 
24916169Shx147065 	IWK_DBG((IWK_DEBUG_INTR, "interrupt reg %x\n", r));
24926169Shx147065 
24936169Shx147065 	rfh = IWK_READ(sc, CSR_FH_INT_STATUS);
24946169Shx147065 	IWK_DBG((IWK_DEBUG_INTR, "FH interrupt reg %x\n", rfh));
24956169Shx147065 	/* disable interrupts */
24966169Shx147065 	IWK_WRITE(sc, CSR_INT_MASK, 0);
24976169Shx147065 	/* ack interrupts */
24986169Shx147065 	IWK_WRITE(sc, CSR_INT, r);
24996169Shx147065 	IWK_WRITE(sc, CSR_FH_INT_STATUS, rfh);
25006169Shx147065 
25017569SXinghua.Wen@Sun.COM 	if (sc->sc_soft_hdl == NULL) {
25026169Shx147065 		mutex_exit(&sc->sc_glock);
25036169Shx147065 		return (DDI_INTR_CLAIMED);
25046169Shx147065 	}
25056169Shx147065 	if (r & (BIT_INT_SWERROR | BIT_INT_ERR)) {
25067691SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "fatal firmware error\n");
25076169Shx147065 		mutex_exit(&sc->sc_glock);
25087569SXinghua.Wen@Sun.COM #ifdef DEBUG
25097569SXinghua.Wen@Sun.COM 		/* dump event and error logs to dmesg */
25107569SXinghua.Wen@Sun.COM 		iwk_write_error_log(sc);
25117569SXinghua.Wen@Sun.COM 		iwk_write_event_log(sc);
25127569SXinghua.Wen@Sun.COM #endif /* DEBUG */
25136169Shx147065 		iwk_stop(sc);
25146169Shx147065 		sc->sc_ostate = sc->sc_ic.ic_state;
25158902SPengcheng.Chen@Sun.COM 
25168902SPengcheng.Chen@Sun.COM 		/* not capable of fast recovery */
25178902SPengcheng.Chen@Sun.COM 		if (!IWK_CHK_FAST_RECOVER(sc))
25188902SPengcheng.Chen@Sun.COM 			ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
25198902SPengcheng.Chen@Sun.COM 
25206169Shx147065 		sc->sc_flags |= IWK_F_HW_ERR_RECOVER;
25216169Shx147065 		return (DDI_INTR_CLAIMED);
25226169Shx147065 	}
25236169Shx147065 
25246169Shx147065 	if (r & BIT_INT_RF_KILL) {
25259327SFei.Feng@Sun.COM 		uint32_t tmp = IWK_READ(sc, CSR_GP_CNTRL);
25269327SFei.Feng@Sun.COM 		if (tmp & (1 << 27))
25279327SFei.Feng@Sun.COM 			cmn_err(CE_NOTE, "RF switch: radio on\n");
25286169Shx147065 	}
25296169Shx147065 
25306169Shx147065 	if ((r & (BIT_INT_FH_RX | BIT_INT_SW_RX)) ||
25316169Shx147065 	    (rfh & FH_INT_RX_MASK)) {
25326169Shx147065 		sc->sc_rx_softint_pending = 1;
25337569SXinghua.Wen@Sun.COM 		(void) ddi_intr_trigger_softint(sc->sc_soft_hdl, NULL);
25346169Shx147065 	}
25356169Shx147065 
25366169Shx147065 	if (r & BIT_INT_ALIVE)	{
25376169Shx147065 		IWK_DBG((IWK_DEBUG_FW, "firmware initialized.\n"));
25386169Shx147065 	}
25396169Shx147065 
25406169Shx147065 	/* re-enable interrupts */
25416169Shx147065 	IWK_WRITE(sc, CSR_INT_MASK, CSR_INI_SET_MASK);
25426169Shx147065 	mutex_exit(&sc->sc_glock);
25436169Shx147065 
25446169Shx147065 	return (DDI_INTR_CLAIMED);
25456169Shx147065 }
25466169Shx147065 
25476169Shx147065 static uint8_t
iwk_rate_to_plcp(int rate)25486169Shx147065 iwk_rate_to_plcp(int rate)
25496169Shx147065 {
25506169Shx147065 	uint8_t ret;
25516169Shx147065 
25526169Shx147065 	switch (rate) {
25536169Shx147065 	/* CCK rates */
25546169Shx147065 	case 2:
25556169Shx147065 		ret = 0xa;
25566169Shx147065 		break;
25576169Shx147065 	case 4:
25586169Shx147065 		ret = 0x14;
25596169Shx147065 		break;
25606169Shx147065 	case 11:
25616169Shx147065 		ret = 0x37;
25626169Shx147065 		break;
25636169Shx147065 	case 22:
25646169Shx147065 		ret = 0x6e;
25656169Shx147065 		break;
25666169Shx147065 	/* OFDM rates */
25676169Shx147065 	case 12:
25686169Shx147065 		ret = 0xd;
25696169Shx147065 		break;
25706169Shx147065 	case 18:
25716169Shx147065 		ret = 0xf;
25726169Shx147065 		break;
25736169Shx147065 	case 24:
25746169Shx147065 		ret = 0x5;
25756169Shx147065 		break;
25766169Shx147065 	case 36:
25776169Shx147065 		ret = 0x7;
25786169Shx147065 		break;
25796169Shx147065 	case 48:
25806169Shx147065 		ret = 0x9;
25816169Shx147065 		break;
25826169Shx147065 	case 72:
25836169Shx147065 		ret = 0xb;
25846169Shx147065 		break;
25856169Shx147065 	case 96:
25866169Shx147065 		ret = 0x1;
25876169Shx147065 		break;
25886169Shx147065 	case 108:
25896169Shx147065 		ret = 0x3;
25906169Shx147065 		break;
25916169Shx147065 	default:
25926169Shx147065 		ret = 0;
25936169Shx147065 		break;
25946169Shx147065 	}
25956169Shx147065 	return (ret);
25966169Shx147065 }
25976169Shx147065 
25986169Shx147065 static mblk_t *
iwk_m_tx(void * arg,mblk_t * mp)25996169Shx147065 iwk_m_tx(void *arg, mblk_t *mp)
26006169Shx147065 {
26016169Shx147065 	iwk_sc_t	*sc = (iwk_sc_t *)arg;
26026169Shx147065 	ieee80211com_t	*ic = &sc->sc_ic;
26036169Shx147065 	mblk_t			*next;
26046169Shx147065 
26056795Shx147065 	if (sc->sc_flags & IWK_F_SUSPEND) {
26066795Shx147065 		freemsgchain(mp);
26076795Shx147065 		return (NULL);
26086795Shx147065 	}
26096795Shx147065 
26106169Shx147065 	if (ic->ic_state != IEEE80211_S_RUN) {
26116169Shx147065 		freemsgchain(mp);
26126169Shx147065 		return (NULL);
26136169Shx147065 	}
26146169Shx147065 
26158902SPengcheng.Chen@Sun.COM 	if ((sc->sc_flags & IWK_F_HW_ERR_RECOVER) &&
26168902SPengcheng.Chen@Sun.COM 	    IWK_CHK_FAST_RECOVER(sc)) {
26178902SPengcheng.Chen@Sun.COM 		IWK_DBG((IWK_DEBUG_FW, "iwk_m_tx(): hold queue\n"));
26188902SPengcheng.Chen@Sun.COM 		return (mp);
26198902SPengcheng.Chen@Sun.COM 	}
26208902SPengcheng.Chen@Sun.COM 
26216169Shx147065 	while (mp != NULL) {
26226169Shx147065 		next = mp->b_next;
26236169Shx147065 		mp->b_next = NULL;
26246169Shx147065 		if (iwk_send(ic, mp, IEEE80211_FC0_TYPE_DATA) != 0) {
26256169Shx147065 			mp->b_next = next;
26266169Shx147065 			break;
26276169Shx147065 		}
26286169Shx147065 		mp = next;
26296169Shx147065 	}
26306169Shx147065 	return (mp);
26316169Shx147065 }
26326169Shx147065 
26336169Shx147065 /* ARGSUSED */
26346169Shx147065 static int
iwk_send(ieee80211com_t * ic,mblk_t * mp,uint8_t type)26356169Shx147065 iwk_send(ieee80211com_t *ic, mblk_t *mp, uint8_t type)
26366169Shx147065 {
26376169Shx147065 	iwk_sc_t *sc = (iwk_sc_t *)ic;
26386169Shx147065 	iwk_tx_ring_t *ring;
26396169Shx147065 	iwk_tx_desc_t *desc;
26406169Shx147065 	iwk_tx_data_t *data;
26416169Shx147065 	iwk_cmd_t *cmd;
26426169Shx147065 	iwk_tx_cmd_t *tx;
26436169Shx147065 	ieee80211_node_t *in;
26446169Shx147065 	struct ieee80211_frame *wh;
26456169Shx147065 	struct ieee80211_key *k = NULL;
26466169Shx147065 	mblk_t *m, *m0;
26476169Shx147065 	int rate, hdrlen, len, len0, mblen, off, err = IWK_SUCCESS;
26486169Shx147065 	uint16_t masks = 0;
26498594SFei.Feng@Sun.COM 	uint8_t index, index1, index2;
26506169Shx147065 
26516169Shx147065 	ring = &sc->sc_txq[0];
26526169Shx147065 	data = &ring->data[ring->cur];
26536169Shx147065 	desc = data->desc;
26546169Shx147065 	cmd = data->cmd;
26556169Shx147065 	bzero(desc, sizeof (*desc));
26566169Shx147065 	bzero(cmd, sizeof (*cmd));
26576169Shx147065 
26586169Shx147065 	mutex_enter(&sc->sc_tx_lock);
26596795Shx147065 	if (sc->sc_flags & IWK_F_SUSPEND) {
26606795Shx147065 		mutex_exit(&sc->sc_tx_lock);
26616795Shx147065 		if ((type & IEEE80211_FC0_TYPE_MASK) !=
26626795Shx147065 		    IEEE80211_FC0_TYPE_DATA) {
26636795Shx147065 			freemsg(mp);
26646795Shx147065 		}
26656795Shx147065 		err = IWK_FAIL;
26666795Shx147065 		goto exit;
26676795Shx147065 	}
26686795Shx147065 
26696169Shx147065 	if (ring->queued > ring->count - 64) {
26706169Shx147065 		IWK_DBG((IWK_DEBUG_TX, "iwk_send(): no txbuf\n"));
26716169Shx147065 		sc->sc_need_reschedule = 1;
26726169Shx147065 		mutex_exit(&sc->sc_tx_lock);
26736169Shx147065 		if ((type & IEEE80211_FC0_TYPE_MASK) !=
26746169Shx147065 		    IEEE80211_FC0_TYPE_DATA) {
26756169Shx147065 			freemsg(mp);
26766169Shx147065 		}
26776169Shx147065 		sc->sc_tx_nobuf++;
26786169Shx147065 		err = IWK_FAIL;
26796169Shx147065 		goto exit;
26806169Shx147065 	}
26816169Shx147065 	mutex_exit(&sc->sc_tx_lock);
26826169Shx147065 
26836169Shx147065 	hdrlen = sizeof (struct ieee80211_frame);
26846169Shx147065 
26856169Shx147065 	m = allocb(msgdsize(mp) + 32, BPRI_MED);
26866169Shx147065 	if (m == NULL) { /* can not alloc buf, drop this package */
26876169Shx147065 		cmn_err(CE_WARN,
26886169Shx147065 		    "iwk_send(): failed to allocate msgbuf\n");
26896169Shx147065 		freemsg(mp);
26906169Shx147065 		err = IWK_SUCCESS;
26916169Shx147065 		goto exit;
26926169Shx147065 	}
26936169Shx147065 	for (off = 0, m0 = mp; m0 != NULL; m0 = m0->b_cont) {
26946169Shx147065 		mblen = MBLKL(m0);
26956169Shx147065 		(void) memcpy(m->b_rptr + off, m0->b_rptr, mblen);
26966169Shx147065 		off += mblen;
26976169Shx147065 	}
26986169Shx147065 	m->b_wptr += off;
26996169Shx147065 	freemsg(mp);
27006169Shx147065 
27016169Shx147065 	wh = (struct ieee80211_frame *)m->b_rptr;
27026169Shx147065 
27038594SFei.Feng@Sun.COM 	if (ic->ic_opmode == IEEE80211_M_IBSS &&
27048594SFei.Feng@Sun.COM 	    (!(IEEE80211_IS_MULTICAST(wh->i_addr1)))) {
27058594SFei.Feng@Sun.COM 		mutex_enter(&sc->sc_glock);
27068594SFei.Feng@Sun.COM 		mutex_enter(&sc->sc_ibss.node_tb_lock);
27078594SFei.Feng@Sun.COM 
27088594SFei.Feng@Sun.COM 		/*
27098594SFei.Feng@Sun.COM 		 * search for node in ibss node table
27108594SFei.Feng@Sun.COM 		 */
27118594SFei.Feng@Sun.COM 		for (index1 = IWK_STA_ID;
27128594SFei.Feng@Sun.COM 		    index1 < IWK_STATION_COUNT; index1++) {
27138594SFei.Feng@Sun.COM 			if (sc->sc_ibss.ibss_node_tb[index1].used &&
27148594SFei.Feng@Sun.COM 			    IEEE80211_ADDR_EQ(sc->sc_ibss.
27158594SFei.Feng@Sun.COM 			    ibss_node_tb[index1].node.bssid,
27168594SFei.Feng@Sun.COM 			    wh->i_addr1)) {
27178594SFei.Feng@Sun.COM 				break;
27188594SFei.Feng@Sun.COM 			}
27198594SFei.Feng@Sun.COM 		}
27208594SFei.Feng@Sun.COM 
27218594SFei.Feng@Sun.COM 		/*
27228594SFei.Feng@Sun.COM 		 * if don't find in ibss node table
27238594SFei.Feng@Sun.COM 		 */
27248594SFei.Feng@Sun.COM 		if (index1 >= IWK_BROADCAST_ID) {
27258594SFei.Feng@Sun.COM 			err = iwk_clean_add_node_ibss(ic,
27268594SFei.Feng@Sun.COM 			    wh->i_addr1, &index2);
27278594SFei.Feng@Sun.COM 			if (err != IWK_SUCCESS) {
27288594SFei.Feng@Sun.COM 				cmn_err(CE_WARN, "iwk_send(): "
27298594SFei.Feng@Sun.COM 				    "failed to clean all nodes "
27308594SFei.Feng@Sun.COM 				    "and add one node\n");
27318594SFei.Feng@Sun.COM 				mutex_exit(&sc->sc_ibss.node_tb_lock);
27328594SFei.Feng@Sun.COM 				mutex_exit(&sc->sc_glock);
27338594SFei.Feng@Sun.COM 				freemsg(m);
27348594SFei.Feng@Sun.COM 				sc->sc_tx_err++;
27358594SFei.Feng@Sun.COM 				err = IWK_SUCCESS;
27368594SFei.Feng@Sun.COM 				goto exit;
27378594SFei.Feng@Sun.COM 			}
27388594SFei.Feng@Sun.COM 			index = index2;
27398594SFei.Feng@Sun.COM 		} else {
27408594SFei.Feng@Sun.COM 			index = index1;
27418594SFei.Feng@Sun.COM 		}
27428594SFei.Feng@Sun.COM 		mutex_exit(&sc->sc_ibss.node_tb_lock);
27438594SFei.Feng@Sun.COM 		mutex_exit(&sc->sc_glock);
27448594SFei.Feng@Sun.COM 	}
27458594SFei.Feng@Sun.COM 
27466169Shx147065 	in = ieee80211_find_txnode(ic, wh->i_addr1);
27476169Shx147065 	if (in == NULL) {
27486169Shx147065 		cmn_err(CE_WARN, "iwk_send(): failed to find tx node\n");
27496169Shx147065 		freemsg(m);
27506169Shx147065 		sc->sc_tx_err++;
27516169Shx147065 		err = IWK_SUCCESS;
27526169Shx147065 		goto exit;
27536169Shx147065 	}
27546169Shx147065 	(void) ieee80211_encap(ic, m, in);
27556169Shx147065 
27566169Shx147065 	cmd->hdr.type = REPLY_TX;
27576169Shx147065 	cmd->hdr.flags = 0;
27586169Shx147065 	cmd->hdr.qid = ring->qid;
27596169Shx147065 	cmd->hdr.idx = ring->cur;
27606169Shx147065 
27616169Shx147065 	tx = (iwk_tx_cmd_t *)cmd->data;
27626169Shx147065 	tx->tx_flags = 0;
27636169Shx147065 
27646169Shx147065 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
27656169Shx147065 		tx->tx_flags &= ~(LE_32(TX_CMD_FLG_ACK_MSK));
27666169Shx147065 	} else {
27676169Shx147065 		tx->tx_flags |= LE_32(TX_CMD_FLG_ACK_MSK);
27686169Shx147065 	}
27696169Shx147065 
27706169Shx147065 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
27716169Shx147065 		k = ieee80211_crypto_encap(ic, m);
27726169Shx147065 		if (k == NULL) {
27736169Shx147065 			freemsg(m);
27746169Shx147065 			sc->sc_tx_err++;
27756169Shx147065 			err = IWK_SUCCESS;
27766169Shx147065 			goto exit;
27776169Shx147065 		}
27786169Shx147065 
27796169Shx147065 		if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_AES_CCM) {
27806169Shx147065 			tx->sec_ctl = 2; /* for CCMP */
27816169Shx147065 			tx->tx_flags |= LE_32(TX_CMD_FLG_ACK_MSK);
27826169Shx147065 			(void) memcpy(&tx->key, k->wk_key, k->wk_keylen);
27836169Shx147065 		}
27846169Shx147065 
27856169Shx147065 		/* packet header may have moved, reset our local pointer */
27866169Shx147065 		wh = (struct ieee80211_frame *)m->b_rptr;
27876169Shx147065 	}
27886169Shx147065 
27896169Shx147065 	len = msgdsize(m);
27906169Shx147065 
27916169Shx147065 #ifdef DEBUG
27926169Shx147065 	if (iwk_dbg_flags & IWK_DEBUG_TX)
27936169Shx147065 		ieee80211_dump_pkt((uint8_t *)wh, hdrlen, 0, 0);
27946169Shx147065 #endif
27956169Shx147065 
27966169Shx147065 	/* pickup a rate */
27976169Shx147065 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
27986169Shx147065 	    IEEE80211_FC0_TYPE_MGT) {
27996169Shx147065 		/* mgmt frames are sent at 1M */
28006169Shx147065 		rate = in->in_rates.ir_rates[0];
28016169Shx147065 	} else {
28026169Shx147065 		/*
28036249Shx147065 		 * do it here for the software way rate control.
28046249Shx147065 		 * later for rate scaling in hardware.
28056169Shx147065 		 * maybe like the following, for management frame:
28066169Shx147065 		 * tx->initial_rate_index = LINK_QUAL_MAX_RETRY_NUM - 1;
28076169Shx147065 		 * for data frame:
28086169Shx147065 		 * tx->tx_flags |= (LE_32(TX_CMD_FLG_STA_RATE_MSK));
28096169Shx147065 		 * rate = in->in_rates.ir_rates[in->in_txrate];
28106169Shx147065 		 * tx->initial_rate_index = 1;
28116169Shx147065 		 *
28126169Shx147065 		 * now the txrate is determined in tx cmd flags, set to the
28136169Shx147065 		 * max value 54M for 11g and 11M for 11b.
28146169Shx147065 		 */
28156169Shx147065 
28166169Shx147065 		if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
28176169Shx147065 			rate = ic->ic_fixed_rate;
28186169Shx147065 		} else {
28196169Shx147065 			rate = in->in_rates.ir_rates[in->in_txrate];
28206169Shx147065 		}
28216169Shx147065 	}
28226169Shx147065 	rate &= IEEE80211_RATE_VAL;
28236169Shx147065 	IWK_DBG((IWK_DEBUG_TX, "tx rate[%d of %d] = %x",
28246169Shx147065 	    in->in_txrate, in->in_rates.ir_nrates, rate));
28256169Shx147065 
28266169Shx147065 	tx->tx_flags |= (LE_32(TX_CMD_FLG_SEQ_CTL_MSK));
28276169Shx147065 
28286169Shx147065 	len0 = roundup(4 + sizeof (iwk_tx_cmd_t) + hdrlen, 4);
28296169Shx147065 	if (len0 != (4 + sizeof (iwk_tx_cmd_t) + hdrlen))
28309327SFei.Feng@Sun.COM 		tx->tx_flags |= LE_32(TX_CMD_FLG_MH_PAD_MSK);
28316169Shx147065 
28326169Shx147065 	/* retrieve destination node's id */
28336169Shx147065 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
28346169Shx147065 		tx->sta_id = IWK_BROADCAST_ID;
28356169Shx147065 	} else {
28368594SFei.Feng@Sun.COM 		if (ic->ic_opmode == IEEE80211_M_IBSS)
28378594SFei.Feng@Sun.COM 			tx->sta_id = index;
28388594SFei.Feng@Sun.COM 		else
28397569SXinghua.Wen@Sun.COM 			tx->sta_id = IWK_AP_ID;
28406169Shx147065 	}
28416169Shx147065 
28426169Shx147065 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
28436169Shx147065 	    IEEE80211_FC0_TYPE_MGT) {
28446169Shx147065 		/* tell h/w to set timestamp in probe responses */
28456169Shx147065 		if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
28466169Shx147065 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
28476169Shx147065 			tx->tx_flags |= LE_32(TX_CMD_FLG_TSF_MSK);
28486169Shx147065 
28496169Shx147065 		if (((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
28506169Shx147065 		    IEEE80211_FC0_SUBTYPE_ASSOC_REQ) ||
28516169Shx147065 		    ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
28526169Shx147065 		    IEEE80211_FC0_SUBTYPE_REASSOC_REQ))
28539327SFei.Feng@Sun.COM 			tx->timeout.pm_frame_timeout = LE_16(3);
28546169Shx147065 		else
28559327SFei.Feng@Sun.COM 			tx->timeout.pm_frame_timeout = LE_16(2);
28566169Shx147065 	} else
28576169Shx147065 		tx->timeout.pm_frame_timeout = 0;
28586169Shx147065 	if (rate == 2 || rate == 4 || rate == 11 || rate == 22)
28596169Shx147065 		masks |= RATE_MCS_CCK_MSK;
28606169Shx147065 
28616169Shx147065 	masks |= RATE_MCS_ANT_B_MSK;
28629327SFei.Feng@Sun.COM 	tx->rate.r.rate_n_flags = LE_32(iwk_rate_to_plcp(rate) | masks);
28636169Shx147065 
28646169Shx147065 	IWK_DBG((IWK_DEBUG_TX, "tx flag = %x",
28659327SFei.Feng@Sun.COM 	    LE_32(tx->tx_flags)));
28666169Shx147065 
28676169Shx147065 	tx->rts_retry_limit = 60;
28686169Shx147065 	tx->data_retry_limit = 15;
28696169Shx147065 
28706169Shx147065 	tx->stop_time.life_time  = LE_32(0xffffffff);
28716169Shx147065 
28726169Shx147065 	tx->len = LE_16(len);
28736169Shx147065 
28746169Shx147065 	tx->dram_lsb_ptr =
28759327SFei.Feng@Sun.COM 	    LE_32(data->paddr_cmd + 4 + offsetof(iwk_tx_cmd_t, scratch));
28766169Shx147065 	tx->dram_msb_ptr = 0;
28776169Shx147065 	tx->driver_txop = 0;
28786169Shx147065 	tx->next_frame_len = 0;
28796169Shx147065 
28806169Shx147065 	(void) memcpy(tx + 1, m->b_rptr, hdrlen);
28816169Shx147065 	m->b_rptr += hdrlen;
28826169Shx147065 	(void) memcpy(data->dma_data.mem_va, m->b_rptr, len - hdrlen);
28836169Shx147065 
28846169Shx147065 	IWK_DBG((IWK_DEBUG_TX, "sending data: qid=%d idx=%d len=%d",
28856169Shx147065 	    ring->qid, ring->cur, len));
28866169Shx147065 
28876169Shx147065 	/*
28886169Shx147065 	 * first segment includes the tx cmd plus the 802.11 header,
28896169Shx147065 	 * the second includes the remaining of the 802.11 frame.
28906169Shx147065 	 */
28919327SFei.Feng@Sun.COM 	desc->val0 = 2 << 24;
28929327SFei.Feng@Sun.COM 	desc->pa[0].tb1_addr = data->paddr_cmd;
28936169Shx147065 	desc->pa[0].val1 = ((len0 << 4) & 0xfff0) |
28946169Shx147065 	    ((data->dma_data.cookie.dmac_address & 0xffff) << 16);
28956169Shx147065 	desc->pa[0].val2 =
28966169Shx147065 	    ((data->dma_data.cookie.dmac_address & 0xffff0000) >> 16) |
28976169Shx147065 	    ((len - hdrlen) << 20);
28986169Shx147065 	IWK_DBG((IWK_DEBUG_TX, "phy addr1 = 0x%x phy addr2 = 0x%x "
28996169Shx147065 	    "len1 = 0x%x, len2 = 0x%x val1 = 0x%x val2 = 0x%x",
29006169Shx147065 	    data->paddr_cmd, data->dma_data.cookie.dmac_address,
29019327SFei.Feng@Sun.COM 	    len0, len - hdrlen, LE_32(desc->pa[0].val1),
29029327SFei.Feng@Sun.COM 	    LE_32(desc->pa[0].val2)));
29036169Shx147065 
29046169Shx147065 	mutex_enter(&sc->sc_tx_lock);
29056169Shx147065 	ring->queued++;
29066169Shx147065 	mutex_exit(&sc->sc_tx_lock);
29076169Shx147065 
29086169Shx147065 	/* kick ring */
29097569SXinghua.Wen@Sun.COM 	sc->sc_shared->queues_byte_cnt_tbls[ring->qid].
29107569SXinghua.Wen@Sun.COM 	    tfd_offset[ring->cur].val = 8 + len;
29116169Shx147065 	if (ring->cur < IWK_MAX_WIN_SIZE) {
29126169Shx147065 		sc->sc_shared->queues_byte_cnt_tbls[ring->qid].
29136169Shx147065 		    tfd_offset[IWK_QUEUE_SIZE + ring->cur].val = 8 + len;
29146169Shx147065 	}
29156169Shx147065 
29166169Shx147065 	IWK_DMA_SYNC(data->dma_data, DDI_DMA_SYNC_FORDEV);
29176169Shx147065 	IWK_DMA_SYNC(ring->dma_desc, DDI_DMA_SYNC_FORDEV);
29186169Shx147065 
29196169Shx147065 	ring->cur = (ring->cur + 1) % ring->count;
29206169Shx147065 	IWK_WRITE(sc, HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
29216169Shx147065 	freemsg(m);
29226169Shx147065 	/* release node reference */
29236169Shx147065 	ieee80211_free_node(in);
29246169Shx147065 
29256169Shx147065 	ic->ic_stats.is_tx_bytes += len;
29266169Shx147065 	ic->ic_stats.is_tx_frags++;
29276169Shx147065 
29286169Shx147065 	if (sc->sc_tx_timer == 0)
29298902SPengcheng.Chen@Sun.COM 		sc->sc_tx_timer = 4;
29308902SPengcheng.Chen@Sun.COM 
29316169Shx147065 exit:
29326169Shx147065 	return (err);
29336169Shx147065 }
29346169Shx147065 
29356169Shx147065 static void
iwk_m_ioctl(void * arg,queue_t * wq,mblk_t * mp)29366169Shx147065 iwk_m_ioctl(void* arg, queue_t *wq, mblk_t *mp)
29376169Shx147065 {
29386169Shx147065 	iwk_sc_t	*sc  = (iwk_sc_t *)arg;
29396169Shx147065 	ieee80211com_t	*ic = &sc->sc_ic;
29408594SFei.Feng@Sun.COM 
29418594SFei.Feng@Sun.COM 	enum ieee80211_opmode		oldmod;
29428594SFei.Feng@Sun.COM 	iwk_tx_power_table_cmd_t	txpower;
29438594SFei.Feng@Sun.COM 	iwk_add_sta_t			node;
29448594SFei.Feng@Sun.COM 	iwk_link_quality_cmd_t		link_quality;
29458594SFei.Feng@Sun.COM 	uint16_t			masks = 0;
29468594SFei.Feng@Sun.COM 	int				i, err, err1;
29478594SFei.Feng@Sun.COM 
29488594SFei.Feng@Sun.COM 	oldmod = ic->ic_opmode;
29496169Shx147065 
29506169Shx147065 	err = ieee80211_ioctl(ic, wq, mp);
29517569SXinghua.Wen@Sun.COM 
29528594SFei.Feng@Sun.COM 	/*
29538594SFei.Feng@Sun.COM 	 * return to STA mode
29548594SFei.Feng@Sun.COM 	 */
29558594SFei.Feng@Sun.COM 	if ((0 == err || ENETRESET == err) && (oldmod != ic->ic_opmode) &&
29568594SFei.Feng@Sun.COM 	    (ic->ic_opmode == IEEE80211_M_STA)) {
29578594SFei.Feng@Sun.COM 		/* configure rxon */
29588594SFei.Feng@Sun.COM 		(void) memset(&sc->sc_config, 0, sizeof (iwk_rxon_cmd_t));
29598594SFei.Feng@Sun.COM 		IEEE80211_ADDR_COPY(sc->sc_config.node_addr, ic->ic_macaddr);
29608594SFei.Feng@Sun.COM 		IEEE80211_ADDR_COPY(sc->sc_config.wlap_bssid, ic->ic_macaddr);
29619327SFei.Feng@Sun.COM 		sc->sc_config.chan =
29629327SFei.Feng@Sun.COM 		    LE_16(ieee80211_chan2ieee(ic, ic->ic_curchan));
29639327SFei.Feng@Sun.COM 		sc->sc_config.flags = LE_32(RXON_FLG_TSF2HOST_MSK |
29648594SFei.Feng@Sun.COM 		    RXON_FLG_AUTO_DETECT_MSK |
29658594SFei.Feng@Sun.COM 		    RXON_FLG_BAND_24G_MSK);
29669327SFei.Feng@Sun.COM 		sc->sc_config.flags &= LE_32(~RXON_FLG_CCK_MSK);
29678594SFei.Feng@Sun.COM 		switch (ic->ic_opmode) {
29688594SFei.Feng@Sun.COM 		case IEEE80211_M_STA:
29698594SFei.Feng@Sun.COM 			sc->sc_config.dev_type = RXON_DEV_TYPE_ESS;
29708594SFei.Feng@Sun.COM 			sc->sc_config.filter_flags |=
29718594SFei.Feng@Sun.COM 			    LE_32(RXON_FILTER_ACCEPT_GRP_MSK |
29728594SFei.Feng@Sun.COM 			    RXON_FILTER_DIS_DECRYPT_MSK |
29738594SFei.Feng@Sun.COM 			    RXON_FILTER_DIS_GRP_DECRYPT_MSK);
29748594SFei.Feng@Sun.COM 			break;
29758594SFei.Feng@Sun.COM 		case IEEE80211_M_IBSS:
29768594SFei.Feng@Sun.COM 		case IEEE80211_M_AHDEMO:
29778594SFei.Feng@Sun.COM 			sc->sc_config.dev_type = RXON_DEV_TYPE_IBSS;
29789327SFei.Feng@Sun.COM 			sc->sc_config.flags |=
29799327SFei.Feng@Sun.COM 			    LE_32(RXON_FLG_SHORT_PREAMBLE_MSK);
29808594SFei.Feng@Sun.COM 			sc->sc_config.filter_flags =
29818594SFei.Feng@Sun.COM 			    LE_32(RXON_FILTER_ACCEPT_GRP_MSK |
29828594SFei.Feng@Sun.COM 			    RXON_FILTER_DIS_DECRYPT_MSK |
29838594SFei.Feng@Sun.COM 			    RXON_FILTER_DIS_GRP_DECRYPT_MSK);
29848594SFei.Feng@Sun.COM 			break;
29858594SFei.Feng@Sun.COM 		case IEEE80211_M_HOSTAP:
29868594SFei.Feng@Sun.COM 			sc->sc_config.dev_type = RXON_DEV_TYPE_AP;
29878594SFei.Feng@Sun.COM 			break;
29888594SFei.Feng@Sun.COM 		case IEEE80211_M_MONITOR:
29898594SFei.Feng@Sun.COM 			sc->sc_config.dev_type = RXON_DEV_TYPE_SNIFFER;
29908594SFei.Feng@Sun.COM 			sc->sc_config.filter_flags |=
29918594SFei.Feng@Sun.COM 			    LE_32(RXON_FILTER_ACCEPT_GRP_MSK |
29928594SFei.Feng@Sun.COM 			    RXON_FILTER_CTL2HOST_MSK |
29938594SFei.Feng@Sun.COM 			    RXON_FILTER_PROMISC_MSK);
29948594SFei.Feng@Sun.COM 			break;
29958594SFei.Feng@Sun.COM 		}
29968594SFei.Feng@Sun.COM 		sc->sc_config.cck_basic_rates  = 0x0f;
29978594SFei.Feng@Sun.COM 		sc->sc_config.ofdm_basic_rates = 0xff;
29988594SFei.Feng@Sun.COM 		sc->sc_config.ofdm_ht_single_stream_basic_rates = 0xff;
29998594SFei.Feng@Sun.COM 		sc->sc_config.ofdm_ht_dual_stream_basic_rates = 0xff;
30008594SFei.Feng@Sun.COM 		/* set antenna */
30018594SFei.Feng@Sun.COM 		mutex_enter(&sc->sc_glock);
30029327SFei.Feng@Sun.COM 		sc->sc_config.rx_chain = LE_16(RXON_RX_CHAIN_DRIVER_FORCE_MSK |
30039327SFei.Feng@Sun.COM 		    (0x7 << RXON_RX_CHAIN_VALID_POS) |
30048594SFei.Feng@Sun.COM 		    (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
30058594SFei.Feng@Sun.COM 		    (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
30068594SFei.Feng@Sun.COM 		err1 = iwk_cmd(sc, REPLY_RXON, &sc->sc_config,
30078594SFei.Feng@Sun.COM 		    sizeof (iwk_rxon_cmd_t), 1);
30088594SFei.Feng@Sun.COM 		if (err1 != IWK_SUCCESS) {
30098594SFei.Feng@Sun.COM 			cmn_err(CE_WARN, "iwk_m_ioctl(): "
30108594SFei.Feng@Sun.COM 			    "failed to set configure command"
30118594SFei.Feng@Sun.COM 			    " please run (ifconfig unplumb and"
30128594SFei.Feng@Sun.COM 			    " ifconfig plumb)\n");
30138594SFei.Feng@Sun.COM 		}
30148594SFei.Feng@Sun.COM 		/*
30158594SFei.Feng@Sun.COM 		 * set Tx power for 2.4GHz channels
30168594SFei.Feng@Sun.COM 		 * (need further investigation. fix tx power at present)
30178594SFei.Feng@Sun.COM 		 */
30188594SFei.Feng@Sun.COM 		(void) memset(&txpower, 0, sizeof (txpower));
30198594SFei.Feng@Sun.COM 		txpower.band = 1; /* for 2.4G */
30208594SFei.Feng@Sun.COM 		txpower.channel = sc->sc_config.chan;
30218594SFei.Feng@Sun.COM 		txpower.channel_normal_width = 0;
30228594SFei.Feng@Sun.COM 		for (i = 0; i < POWER_TABLE_NUM_HT_OFDM_ENTRIES; i++) {
30238594SFei.Feng@Sun.COM 			txpower.tx_power.ht_ofdm_power[i].
30249327SFei.Feng@Sun.COM 			    s.ramon_tx_gain = LE_16(0x3f3f);
30258594SFei.Feng@Sun.COM 			txpower.tx_power.ht_ofdm_power[i].
30269327SFei.Feng@Sun.COM 			    s.dsp_predis_atten = LE_16(110 | (110 << 8));
30278594SFei.Feng@Sun.COM 		}
30289327SFei.Feng@Sun.COM 		txpower.tx_power.legacy_cck_power.s.
30299327SFei.Feng@Sun.COM 		    ramon_tx_gain = LE_16(0x3f3f);
30309327SFei.Feng@Sun.COM 		txpower.tx_power.legacy_cck_power.s.
30319327SFei.Feng@Sun.COM 		    dsp_predis_atten = LE_16(110 | (110 << 8));
30328594SFei.Feng@Sun.COM 		err1 = iwk_cmd(sc, REPLY_TX_PWR_TABLE_CMD, &txpower,
30338594SFei.Feng@Sun.COM 		    sizeof (txpower), 1);
30348594SFei.Feng@Sun.COM 		if (err1 != IWK_SUCCESS) {
30358594SFei.Feng@Sun.COM 			cmn_err(CE_WARN, "iwk_m_ioctl(): failed to set txpower"
30368594SFei.Feng@Sun.COM 			    " please run (ifconfig unplumb "
30378594SFei.Feng@Sun.COM 			    "and ifconfig plumb)\n");
30388594SFei.Feng@Sun.COM 		}
30398594SFei.Feng@Sun.COM 		/* add broadcast node so that we can send broadcast frame */
30408594SFei.Feng@Sun.COM 		(void) memset(&node, 0, sizeof (node));
30418594SFei.Feng@Sun.COM 		(void) memset(node.bssid, 0xff, 6);
30428594SFei.Feng@Sun.COM 		node.id = IWK_BROADCAST_ID;
30438594SFei.Feng@Sun.COM 		err1 = iwk_cmd(sc, REPLY_ADD_STA, &node, sizeof (node), 1);
30448594SFei.Feng@Sun.COM 		if (err1 != IWK_SUCCESS) {
30458594SFei.Feng@Sun.COM 			cmn_err(CE_WARN, "iwk_m_ioctl(): "
30468594SFei.Feng@Sun.COM 			    "failed to add broadcast node\n");
30478594SFei.Feng@Sun.COM 		}
30488594SFei.Feng@Sun.COM 
30498594SFei.Feng@Sun.COM 		/* TX_LINK_QUALITY cmd */
30508594SFei.Feng@Sun.COM 		(void) memset(&link_quality, 0, sizeof (link_quality));
30518594SFei.Feng@Sun.COM 		for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
30528594SFei.Feng@Sun.COM 			masks |= RATE_MCS_CCK_MSK;
30538594SFei.Feng@Sun.COM 			masks |= RATE_MCS_ANT_B_MSK;
30548594SFei.Feng@Sun.COM 			masks &= ~RATE_MCS_ANT_A_MSK;
30558594SFei.Feng@Sun.COM 			link_quality.rate_n_flags[i] =
30569327SFei.Feng@Sun.COM 			    LE_32(iwk_rate_to_plcp(2) | masks);
30578594SFei.Feng@Sun.COM 		}
30588594SFei.Feng@Sun.COM 		link_quality.general_params.single_stream_ant_msk = 2;
30598594SFei.Feng@Sun.COM 		link_quality.general_params.dual_stream_ant_msk = 3;
30608594SFei.Feng@Sun.COM 		link_quality.agg_params.agg_dis_start_th = 3;
30618594SFei.Feng@Sun.COM 		link_quality.agg_params.agg_time_limit = LE_16(4000);
30628594SFei.Feng@Sun.COM 		link_quality.sta_id = IWK_BROADCAST_ID;
30638594SFei.Feng@Sun.COM 		err1 = iwk_cmd(sc, REPLY_TX_LINK_QUALITY_CMD, &link_quality,
30648594SFei.Feng@Sun.COM 		    sizeof (link_quality), 1);
30658594SFei.Feng@Sun.COM 		if (err1 != IWK_SUCCESS) {
30668594SFei.Feng@Sun.COM 			cmn_err(CE_WARN, "iwk_m_ioctl(): "
30678594SFei.Feng@Sun.COM 			    "failed to config link quality table\n");
30688594SFei.Feng@Sun.COM 		}
30698594SFei.Feng@Sun.COM 		mutex_exit(&sc->sc_glock);
30708594SFei.Feng@Sun.COM 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
30718594SFei.Feng@Sun.COM 	}
30728594SFei.Feng@Sun.COM 
30736169Shx147065 	if (err == ENETRESET) {
30746249Shx147065 		/*
30756249Shx147065 		 * This is special for the hidden AP connection.
30766249Shx147065 		 * In any case, we should make sure only one 'scan'
30776249Shx147065 		 * in the driver for a 'connect' CLI command. So
30786249Shx147065 		 * when connecting to a hidden AP, the scan is just
30796249Shx147065 		 * sent out to the air when we know the desired
30806249Shx147065 		 * essid of the AP we want to connect.
30816249Shx147065 		 */
30826249Shx147065 		if (ic->ic_des_esslen) {
30837691SPengcheng.Chen@Sun.COM 			if (sc->sc_flags & IWK_F_RUNNING) {
30847691SPengcheng.Chen@Sun.COM 				iwk_m_stop(sc);
30857691SPengcheng.Chen@Sun.COM 				(void) iwk_m_start(sc);
30867691SPengcheng.Chen@Sun.COM 				(void) ieee80211_new_state(ic,
30877691SPengcheng.Chen@Sun.COM 				    IEEE80211_S_SCAN, -1);
30887691SPengcheng.Chen@Sun.COM 			}
30896249Shx147065 		}
30906169Shx147065 	}
30916169Shx147065 }
30926169Shx147065 
30937663SSowmini.Varadhan@Sun.COM /*
30947663SSowmini.Varadhan@Sun.COM  * callback functions for set/get properties
30957663SSowmini.Varadhan@Sun.COM  */
3096*11878SVenu.Iyer@Sun.COM 
30977663SSowmini.Varadhan@Sun.COM static int
iwk_m_getprop(void * arg,const char * pr_name,mac_prop_id_t wldp_pr_num,uint_t wldp_length,void * wldp_buf)30987663SSowmini.Varadhan@Sun.COM iwk_m_getprop(void *arg, const char *pr_name, mac_prop_id_t wldp_pr_num,
3099*11878SVenu.Iyer@Sun.COM     uint_t wldp_length, void *wldp_buf)
31007663SSowmini.Varadhan@Sun.COM {
31017663SSowmini.Varadhan@Sun.COM 	int		err = 0;
31027663SSowmini.Varadhan@Sun.COM 	iwk_sc_t	*sc = (iwk_sc_t *)arg;
31037663SSowmini.Varadhan@Sun.COM 
31047663SSowmini.Varadhan@Sun.COM 	err = ieee80211_getprop(&sc->sc_ic, pr_name, wldp_pr_num,
3105*11878SVenu.Iyer@Sun.COM 	    wldp_length, wldp_buf);
31067663SSowmini.Varadhan@Sun.COM 
31077663SSowmini.Varadhan@Sun.COM 	return (err);
31087663SSowmini.Varadhan@Sun.COM }
3109*11878SVenu.Iyer@Sun.COM 
31107663SSowmini.Varadhan@Sun.COM static int
iwk_m_setprop(void * arg,const char * pr_name,mac_prop_id_t wldp_pr_num,uint_t wldp_length,const void * wldp_buf)31117663SSowmini.Varadhan@Sun.COM iwk_m_setprop(void *arg, const char *pr_name, mac_prop_id_t wldp_pr_num,
31127663SSowmini.Varadhan@Sun.COM     uint_t wldp_length, const void *wldp_buf)
31137663SSowmini.Varadhan@Sun.COM {
31147663SSowmini.Varadhan@Sun.COM 	int		err;
31157663SSowmini.Varadhan@Sun.COM 	iwk_sc_t	*sc = (iwk_sc_t *)arg;
31167663SSowmini.Varadhan@Sun.COM 	ieee80211com_t	*ic = &sc->sc_ic;
31177663SSowmini.Varadhan@Sun.COM 
31187663SSowmini.Varadhan@Sun.COM 	err = ieee80211_setprop(ic, pr_name, wldp_pr_num, wldp_length,
31197663SSowmini.Varadhan@Sun.COM 	    wldp_buf);
31207663SSowmini.Varadhan@Sun.COM 
31217663SSowmini.Varadhan@Sun.COM 	if (err == ENETRESET) {
31227663SSowmini.Varadhan@Sun.COM 		if (ic->ic_des_esslen) {
31237691SPengcheng.Chen@Sun.COM 			if (sc->sc_flags & IWK_F_RUNNING) {
31247691SPengcheng.Chen@Sun.COM 				iwk_m_stop(sc);
31257691SPengcheng.Chen@Sun.COM 				(void) iwk_m_start(sc);
31267691SPengcheng.Chen@Sun.COM 				(void) ieee80211_new_state(ic,
31277691SPengcheng.Chen@Sun.COM 				    IEEE80211_S_SCAN, -1);
31287691SPengcheng.Chen@Sun.COM 			}
31297663SSowmini.Varadhan@Sun.COM 		}
31307663SSowmini.Varadhan@Sun.COM 		err = 0;
31317663SSowmini.Varadhan@Sun.COM 	}
31327663SSowmini.Varadhan@Sun.COM 
31337663SSowmini.Varadhan@Sun.COM 	return (err);
31347663SSowmini.Varadhan@Sun.COM }
31357663SSowmini.Varadhan@Sun.COM 
3136*11878SVenu.Iyer@Sun.COM static void
iwk_m_propinfo(void * arg,const char * pr_name,mac_prop_id_t wldp_pr_num,mac_prop_info_handle_t mph)3137*11878SVenu.Iyer@Sun.COM iwk_m_propinfo(void *arg, const char *pr_name, mac_prop_id_t wldp_pr_num,
3138*11878SVenu.Iyer@Sun.COM     mac_prop_info_handle_t mph)
3139*11878SVenu.Iyer@Sun.COM {
3140*11878SVenu.Iyer@Sun.COM 	iwk_sc_t	*sc = (iwk_sc_t *)arg;
3141*11878SVenu.Iyer@Sun.COM 	ieee80211com_t	*ic = &sc->sc_ic;
3142*11878SVenu.Iyer@Sun.COM 
3143*11878SVenu.Iyer@Sun.COM 	ieee80211_propinfo(ic, pr_name, wldp_pr_num, mph);
3144*11878SVenu.Iyer@Sun.COM }
3145*11878SVenu.Iyer@Sun.COM 
31466169Shx147065 /*ARGSUSED*/
31476169Shx147065 static int
iwk_m_stat(void * arg,uint_t stat,uint64_t * val)31486169Shx147065 iwk_m_stat(void *arg, uint_t stat, uint64_t *val)
31496169Shx147065 {
31506169Shx147065 	iwk_sc_t	*sc  = (iwk_sc_t *)arg;
31516169Shx147065 	ieee80211com_t	*ic = &sc->sc_ic;
31528350SPengcheng.Chen@Sun.COM 	ieee80211_node_t *in;
31536169Shx147065 
31546169Shx147065 	mutex_enter(&sc->sc_glock);
31556169Shx147065 	switch (stat) {
31566169Shx147065 	case MAC_STAT_IFSPEED:
31578350SPengcheng.Chen@Sun.COM 		in = ic->ic_bss;
31586169Shx147065 		*val = ((ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) ?
31598350SPengcheng.Chen@Sun.COM 		    IEEE80211_RATE(in->in_txrate) :
31608350SPengcheng.Chen@Sun.COM 		    ic->ic_fixed_rate) / 2 * 1000000;
31616169Shx147065 		break;
31626169Shx147065 	case MAC_STAT_NOXMTBUF:
31636169Shx147065 		*val = sc->sc_tx_nobuf;
31646169Shx147065 		break;
31656169Shx147065 	case MAC_STAT_NORCVBUF:
31666169Shx147065 		*val = sc->sc_rx_nobuf;
31676169Shx147065 		break;
31686169Shx147065 	case MAC_STAT_IERRORS:
31696169Shx147065 		*val = sc->sc_rx_err;
31706169Shx147065 		break;
31716169Shx147065 	case MAC_STAT_RBYTES:
31726169Shx147065 		*val = ic->ic_stats.is_rx_bytes;
31736169Shx147065 		break;
31746169Shx147065 	case MAC_STAT_IPACKETS:
31756169Shx147065 		*val = ic->ic_stats.is_rx_frags;
31766169Shx147065 		break;
31776169Shx147065 	case MAC_STAT_OBYTES:
31786169Shx147065 		*val = ic->ic_stats.is_tx_bytes;
31796169Shx147065 		break;
31806169Shx147065 	case MAC_STAT_OPACKETS:
31816169Shx147065 		*val = ic->ic_stats.is_tx_frags;
31826169Shx147065 		break;
31836169Shx147065 	case MAC_STAT_OERRORS:
31846169Shx147065 	case WIFI_STAT_TX_FAILED:
31856169Shx147065 		*val = sc->sc_tx_err;
31866169Shx147065 		break;
31876169Shx147065 	case WIFI_STAT_TX_RETRANS:
31886169Shx147065 		*val = sc->sc_tx_retries;
31896169Shx147065 		break;
31906169Shx147065 	case WIFI_STAT_FCS_ERRORS:
31916169Shx147065 	case WIFI_STAT_WEP_ERRORS:
31926169Shx147065 	case WIFI_STAT_TX_FRAGS:
31936169Shx147065 	case WIFI_STAT_MCAST_TX:
31946169Shx147065 	case WIFI_STAT_RTS_SUCCESS:
31956169Shx147065 	case WIFI_STAT_RTS_FAILURE:
31966169Shx147065 	case WIFI_STAT_ACK_FAILURE:
31976169Shx147065 	case WIFI_STAT_RX_FRAGS:
31986169Shx147065 	case WIFI_STAT_MCAST_RX:
31996169Shx147065 	case WIFI_STAT_RX_DUPS:
32006169Shx147065 		mutex_exit(&sc->sc_glock);
32016169Shx147065 		return (ieee80211_stat(ic, stat, val));
32026169Shx147065 	default:
32036169Shx147065 		mutex_exit(&sc->sc_glock);
32046169Shx147065 		return (ENOTSUP);
32056169Shx147065 	}
32066169Shx147065 	mutex_exit(&sc->sc_glock);
32076169Shx147065 
32086169Shx147065 	return (IWK_SUCCESS);
32096169Shx147065 
32106169Shx147065 }
32116169Shx147065 
32126169Shx147065 static int
iwk_m_start(void * arg)32136169Shx147065 iwk_m_start(void *arg)
32146169Shx147065 {
32156169Shx147065 	iwk_sc_t *sc = (iwk_sc_t *)arg;
32166169Shx147065 	ieee80211com_t	*ic = &sc->sc_ic;
32176169Shx147065 	int err;
32186169Shx147065 
32196169Shx147065 	err = iwk_init(sc);
32206169Shx147065 
32216169Shx147065 	if (err != IWK_SUCCESS) {
32226249Shx147065 		/*
32236249Shx147065 		 * The hw init err(eg. RF is OFF). Return Success to make
32246249Shx147065 		 * the 'plumb' succeed. The iwk_thread() tries to re-init
32256249Shx147065 		 * background.
32266249Shx147065 		 */
32276249Shx147065 		mutex_enter(&sc->sc_glock);
32286249Shx147065 		sc->sc_flags |= IWK_F_HW_ERR_RECOVER;
32296249Shx147065 		mutex_exit(&sc->sc_glock);
32306249Shx147065 		return (IWK_SUCCESS);
32316169Shx147065 	}
32326249Shx147065 
32336169Shx147065 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
32346169Shx147065 
32356249Shx147065 	mutex_enter(&sc->sc_glock);
32366249Shx147065 	sc->sc_flags |= IWK_F_RUNNING;
32376249Shx147065 	mutex_exit(&sc->sc_glock);
32386249Shx147065 
32396249Shx147065 	return (IWK_SUCCESS);
32406169Shx147065 }
32416169Shx147065 
32426169Shx147065 static void
iwk_m_stop(void * arg)32436169Shx147065 iwk_m_stop(void *arg)
32446169Shx147065 {
32456169Shx147065 	iwk_sc_t *sc = (iwk_sc_t *)arg;
32466169Shx147065 	ieee80211com_t	*ic = &sc->sc_ic;
32476169Shx147065 
32486169Shx147065 	iwk_stop(sc);
32496169Shx147065 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
32508727SPengcheng.Chen@Sun.COM 	ieee80211_stop_watchdog(ic);
32516169Shx147065 	mutex_enter(&sc->sc_mt_lock);
32526169Shx147065 	sc->sc_flags &= ~IWK_F_HW_ERR_RECOVER;
32536249Shx147065 	sc->sc_flags &= ~IWK_F_RATE_AUTO_CTL;
32546169Shx147065 	mutex_exit(&sc->sc_mt_lock);
32556249Shx147065 	mutex_enter(&sc->sc_glock);
32566249Shx147065 	sc->sc_flags &= ~IWK_F_RUNNING;
32576249Shx147065 	mutex_exit(&sc->sc_glock);
32586169Shx147065 }
32596169Shx147065 
32606169Shx147065 /*ARGSUSED*/
32616169Shx147065 static int
iwk_m_unicst(void * arg,const uint8_t * macaddr)32626169Shx147065 iwk_m_unicst(void *arg, const uint8_t *macaddr)
32636169Shx147065 {
32646169Shx147065 	iwk_sc_t *sc = (iwk_sc_t *)arg;
32656169Shx147065 	ieee80211com_t	*ic = &sc->sc_ic;
32666169Shx147065 	int err;
32676169Shx147065 
32686169Shx147065 	if (!IEEE80211_ADDR_EQ(ic->ic_macaddr, macaddr)) {
32696169Shx147065 		IEEE80211_ADDR_COPY(ic->ic_macaddr, macaddr);
32706169Shx147065 		mutex_enter(&sc->sc_glock);
32716169Shx147065 		err = iwk_config(sc);
32726169Shx147065 		mutex_exit(&sc->sc_glock);
32736169Shx147065 		if (err != IWK_SUCCESS) {
32746169Shx147065 			cmn_err(CE_WARN,
32756169Shx147065 			    "iwk_m_unicst(): "
32766169Shx147065 			    "failed to configure device\n");
32776169Shx147065 			goto fail;
32786169Shx147065 		}
32796169Shx147065 	}
32806169Shx147065 	return (IWK_SUCCESS);
32816169Shx147065 fail:
32826169Shx147065 	return (err);
32836169Shx147065 }
32846169Shx147065 
32856169Shx147065 /*ARGSUSED*/
32866169Shx147065 static int
iwk_m_multicst(void * arg,boolean_t add,const uint8_t * m)32876169Shx147065 iwk_m_multicst(void *arg, boolean_t add, const uint8_t *m)
32886169Shx147065 {
32896169Shx147065 	return (IWK_SUCCESS);
32906169Shx147065 }
32916169Shx147065 
32926169Shx147065 /*ARGSUSED*/
32936169Shx147065 static int
iwk_m_promisc(void * arg,boolean_t on)32946169Shx147065 iwk_m_promisc(void *arg, boolean_t on)
32956169Shx147065 {
32966169Shx147065 	return (IWK_SUCCESS);
32976169Shx147065 }
32986169Shx147065 
32996169Shx147065 static void
iwk_thread(iwk_sc_t * sc)33006169Shx147065 iwk_thread(iwk_sc_t *sc)
33016169Shx147065 {
33026169Shx147065 	ieee80211com_t	*ic = &sc->sc_ic;
33036249Shx147065 	clock_t clk;
33046169Shx147065 	int times = 0, err, n = 0, timeout = 0;
33056249Shx147065 	uint32_t tmp;
33066169Shx147065 
33076169Shx147065 	mutex_enter(&sc->sc_mt_lock);
33086169Shx147065 	while (sc->sc_mf_thread_switch) {
33096249Shx147065 		tmp = IWK_READ(sc, CSR_GP_CNTRL);
33106249Shx147065 		if (tmp & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW) {
33116249Shx147065 			sc->sc_flags &= ~IWK_F_RADIO_OFF;
33126249Shx147065 		} else {
33136249Shx147065 			sc->sc_flags |= IWK_F_RADIO_OFF;
33146249Shx147065 		}
33156249Shx147065 		/*
33166795Shx147065 		 * If in SUSPEND or the RF is OFF, do nothing
33176249Shx147065 		 */
33186795Shx147065 		if ((sc->sc_flags & IWK_F_SUSPEND) ||
33196795Shx147065 		    (sc->sc_flags & IWK_F_RADIO_OFF)) {
33206249Shx147065 			mutex_exit(&sc->sc_mt_lock);
33216249Shx147065 			delay(drv_usectohz(100000));
33226249Shx147065 			mutex_enter(&sc->sc_mt_lock);
33236249Shx147065 			continue;
33246249Shx147065 		}
33256249Shx147065 
33266169Shx147065 		/*
33276169Shx147065 		 * recovery fatal error
33286169Shx147065 		 */
33296169Shx147065 		if (ic->ic_mach &&
33306169Shx147065 		    (sc->sc_flags & IWK_F_HW_ERR_RECOVER)) {
33316169Shx147065 
33326169Shx147065 			IWK_DBG((IWK_DEBUG_FW,
33336169Shx147065 			    "iwk_thread(): "
33346169Shx147065 			    "try to recover fatal hw error: %d\n", times++));
33356169Shx147065 
33366169Shx147065 			iwk_stop(sc);
33376169Shx147065 
33388902SPengcheng.Chen@Sun.COM 			if (IWK_CHK_FAST_RECOVER(sc)) {
33398902SPengcheng.Chen@Sun.COM 				/* save runtime configuration */
33408902SPengcheng.Chen@Sun.COM 				bcopy(&sc->sc_config, &sc->sc_config_save,
33418902SPengcheng.Chen@Sun.COM 				    sizeof (sc->sc_config));
33428902SPengcheng.Chen@Sun.COM 			} else {
33438902SPengcheng.Chen@Sun.COM 				mutex_exit(&sc->sc_mt_lock);
33448902SPengcheng.Chen@Sun.COM 				ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
33458902SPengcheng.Chen@Sun.COM 				delay(drv_usectohz(2000000 + n*500000));
33468902SPengcheng.Chen@Sun.COM 				mutex_enter(&sc->sc_mt_lock);
33478902SPengcheng.Chen@Sun.COM 			}
33487691SPengcheng.Chen@Sun.COM 
33496169Shx147065 			err = iwk_init(sc);
33506169Shx147065 			if (err != IWK_SUCCESS) {
33516169Shx147065 				n++;
33526169Shx147065 				if (n < 20)
33536169Shx147065 					continue;
33546169Shx147065 			}
33556169Shx147065 			n = 0;
33566249Shx147065 			if (!err)
33576249Shx147065 				sc->sc_flags |= IWK_F_RUNNING;
33588902SPengcheng.Chen@Sun.COM 
33598902SPengcheng.Chen@Sun.COM 			if (!IWK_CHK_FAST_RECOVER(sc) ||
33608902SPengcheng.Chen@Sun.COM 			    iwk_fast_recover(sc) != IWK_SUCCESS) {
33618902SPengcheng.Chen@Sun.COM 				sc->sc_flags &= ~IWK_F_HW_ERR_RECOVER;
33628902SPengcheng.Chen@Sun.COM 
33638902SPengcheng.Chen@Sun.COM 				mutex_exit(&sc->sc_mt_lock);
33648902SPengcheng.Chen@Sun.COM 				delay(drv_usectohz(2000000));
33658902SPengcheng.Chen@Sun.COM 				if (sc->sc_ostate != IEEE80211_S_INIT)
33668902SPengcheng.Chen@Sun.COM 					ieee80211_new_state(ic,
33678902SPengcheng.Chen@Sun.COM 					    IEEE80211_S_SCAN, 0);
33688902SPengcheng.Chen@Sun.COM 				mutex_enter(&sc->sc_mt_lock);
33698902SPengcheng.Chen@Sun.COM 			}
33706169Shx147065 		}
33716169Shx147065 
33728349SPengcheng.Chen@Sun.COM 		if (ic->ic_mach && (sc->sc_flags & IWK_F_LAZY_RESUME)) {
33738349SPengcheng.Chen@Sun.COM 			IWK_DBG((IWK_DEBUG_RESUME,
33748902SPengcheng.Chen@Sun.COM 			    "iwk_thread(): lazy resume\n"));
33758902SPengcheng.Chen@Sun.COM 
33768349SPengcheng.Chen@Sun.COM 			sc->sc_flags &= ~IWK_F_LAZY_RESUME;
33778349SPengcheng.Chen@Sun.COM 			mutex_exit(&sc->sc_mt_lock);
33788349SPengcheng.Chen@Sun.COM 			/*
33798349SPengcheng.Chen@Sun.COM 			 * NB: under WPA mode, this call hangs (door problem?)
33808349SPengcheng.Chen@Sun.COM 			 * when called in iwk_attach() and iwk_detach() while
33818349SPengcheng.Chen@Sun.COM 			 * system is in the procedure of CPR. To be safe, let
33828349SPengcheng.Chen@Sun.COM 			 * the thread do this.
33838349SPengcheng.Chen@Sun.COM 			 */
33848349SPengcheng.Chen@Sun.COM 			ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
33858349SPengcheng.Chen@Sun.COM 			mutex_enter(&sc->sc_mt_lock);
33868349SPengcheng.Chen@Sun.COM 		}
33878349SPengcheng.Chen@Sun.COM 
33887691SPengcheng.Chen@Sun.COM 		if (ic->ic_mach &&
33897691SPengcheng.Chen@Sun.COM 		    (sc->sc_flags & IWK_F_SCANNING) && sc->sc_scan_pending) {
33907691SPengcheng.Chen@Sun.COM 			IWK_DBG((IWK_DEBUG_SCAN,
33917691SPengcheng.Chen@Sun.COM 			    "iwk_thread(): "
33927691SPengcheng.Chen@Sun.COM 			    "wait for probe response\n"));
33937691SPengcheng.Chen@Sun.COM 			sc->sc_scan_pending--;
33947691SPengcheng.Chen@Sun.COM 			mutex_exit(&sc->sc_mt_lock);
33957691SPengcheng.Chen@Sun.COM 			delay(drv_usectohz(200000));
33968349SPengcheng.Chen@Sun.COM 			if (sc->sc_flags & IWK_F_SCANNING)
33978349SPengcheng.Chen@Sun.COM 				ieee80211_next_scan(ic);
33987691SPengcheng.Chen@Sun.COM 			mutex_enter(&sc->sc_mt_lock);
33997691SPengcheng.Chen@Sun.COM 		}
34007691SPengcheng.Chen@Sun.COM 
34016249Shx147065 		/*
34026249Shx147065 		 * rate ctl
34036249Shx147065 		 */
34046249Shx147065 		if (ic->ic_mach &&
34056249Shx147065 		    (sc->sc_flags & IWK_F_RATE_AUTO_CTL)) {
34066249Shx147065 			clk = ddi_get_lbolt();
34076249Shx147065 			if (clk > sc->sc_clk + drv_usectohz(500000)) {
34086249Shx147065 				iwk_amrr_timeout(sc);
34096249Shx147065 			}
34106249Shx147065 		}
34116249Shx147065 
341211224SQuaker.Fang@Sun.COM 		if ((ic->ic_state == IEEE80211_S_RUN) &&
341311224SQuaker.Fang@Sun.COM 		    (ic->ic_beaconmiss++ > 50)) {	/* 5 seconds */
341411224SQuaker.Fang@Sun.COM 			cmn_err(CE_WARN, "iwk: beacon missed for 5 seconds\n");
341511224SQuaker.Fang@Sun.COM 			(void) ieee80211_new_state(ic,
341611224SQuaker.Fang@Sun.COM 			    IEEE80211_S_INIT, -1);
341711224SQuaker.Fang@Sun.COM 		}
341811224SQuaker.Fang@Sun.COM 
34196169Shx147065 		mutex_exit(&sc->sc_mt_lock);
34206169Shx147065 		delay(drv_usectohz(100000));
34216169Shx147065 		mutex_enter(&sc->sc_mt_lock);
34226169Shx147065 
34236169Shx147065 		if (sc->sc_tx_timer) {
34246169Shx147065 			timeout++;
34256169Shx147065 			if (timeout == 10) {
34266169Shx147065 				sc->sc_tx_timer--;
34276169Shx147065 				if (sc->sc_tx_timer == 0) {
34286169Shx147065 					sc->sc_flags |= IWK_F_HW_ERR_RECOVER;
34296169Shx147065 					sc->sc_ostate = IEEE80211_S_RUN;
34306169Shx147065 					IWK_DBG((IWK_DEBUG_FW,
34316169Shx147065 					    "iwk_thread(): try to recover from"
34326169Shx147065 					    " 'send fail\n"));
34336169Shx147065 				}
34346169Shx147065 				timeout = 0;
34356169Shx147065 			}
34366169Shx147065 		}
34376169Shx147065 
34386169Shx147065 	}
34396169Shx147065 	sc->sc_mf_thread = NULL;
34406169Shx147065 	cv_signal(&sc->sc_mt_cv);
34416169Shx147065 	mutex_exit(&sc->sc_mt_lock);
34426169Shx147065 }
34436169Shx147065 
34446169Shx147065 
34456169Shx147065 /*
34466169Shx147065  * Send a command to the firmware.
34476169Shx147065  */
34486169Shx147065 static int
iwk_cmd(iwk_sc_t * sc,int code,const void * buf,int size,int async)34496169Shx147065 iwk_cmd(iwk_sc_t *sc, int code, const void *buf, int size, int async)
34506169Shx147065 {
34516169Shx147065 	iwk_tx_ring_t *ring = &sc->sc_txq[IWK_CMD_QUEUE_NUM];
34526169Shx147065 	iwk_tx_desc_t *desc;
34536169Shx147065 	iwk_cmd_t *cmd;
34547569SXinghua.Wen@Sun.COM 	clock_t clk;
34556169Shx147065 
34566169Shx147065 	ASSERT(size <= sizeof (cmd->data));
34576169Shx147065 	ASSERT(mutex_owned(&sc->sc_glock));
34586169Shx147065 
34596169Shx147065 	IWK_DBG((IWK_DEBUG_CMD, "iwk_cmd() code[%d]", code));
34606169Shx147065 	desc = ring->data[ring->cur].desc;
34616169Shx147065 	cmd = ring->data[ring->cur].cmd;
34626169Shx147065 
34636169Shx147065 	cmd->hdr.type = (uint8_t)code;
34646169Shx147065 	cmd->hdr.flags = 0;
34656169Shx147065 	cmd->hdr.qid = ring->qid;
34666169Shx147065 	cmd->hdr.idx = ring->cur;
34676169Shx147065 	(void) memcpy(cmd->data, buf, size);
34686169Shx147065 	(void) memset(desc, 0, sizeof (*desc));
34696169Shx147065 
34709327SFei.Feng@Sun.COM 	desc->val0 = 1 << 24;
34716169Shx147065 	desc->pa[0].tb1_addr =
34726169Shx147065 	    (uint32_t)(ring->data[ring->cur].paddr_cmd & 0xffffffff);
34736169Shx147065 	desc->pa[0].val1 = ((4 + size) << 4) & 0xfff0;
34746169Shx147065 
34756169Shx147065 	/* kick cmd ring XXX */
34767569SXinghua.Wen@Sun.COM 	sc->sc_shared->queues_byte_cnt_tbls[ring->qid].
34777569SXinghua.Wen@Sun.COM 	    tfd_offset[ring->cur].val = 8;
34786169Shx147065 	if (ring->cur < IWK_MAX_WIN_SIZE) {
34797569SXinghua.Wen@Sun.COM 		sc->sc_shared->queues_byte_cnt_tbls[ring->qid].
34807569SXinghua.Wen@Sun.COM 		    tfd_offset[IWK_QUEUE_SIZE + ring->cur].val = 8;
34816169Shx147065 	}
34826169Shx147065 	ring->cur = (ring->cur + 1) % ring->count;
34836169Shx147065 	IWK_WRITE(sc, HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
34846169Shx147065 
34856169Shx147065 	if (async)
34866169Shx147065 		return (IWK_SUCCESS);
34876169Shx147065 	else {
34886169Shx147065 		sc->sc_flags &= ~IWK_F_CMD_DONE;
34896169Shx147065 		clk = ddi_get_lbolt() + drv_usectohz(2000000);
34906169Shx147065 		while (!(sc->sc_flags & IWK_F_CMD_DONE)) {
34917569SXinghua.Wen@Sun.COM 			if (cv_timedwait(&sc->sc_cmd_cv, &sc->sc_glock, clk) <
34927569SXinghua.Wen@Sun.COM 			    0)
34936169Shx147065 				break;
34946169Shx147065 		}
34956169Shx147065 		if (sc->sc_flags & IWK_F_CMD_DONE)
34966169Shx147065 			return (IWK_SUCCESS);
34976169Shx147065 		else
34986169Shx147065 			return (IWK_FAIL);
34996169Shx147065 	}
35006169Shx147065 }
35016169Shx147065 
35026169Shx147065 static void
iwk_set_led(iwk_sc_t * sc,uint8_t id,uint8_t off,uint8_t on)35036169Shx147065 iwk_set_led(iwk_sc_t *sc, uint8_t id, uint8_t off, uint8_t on)
35046169Shx147065 {
35056169Shx147065 	iwk_led_cmd_t led;
35066169Shx147065 
35076169Shx147065 	led.interval = LE_32(100000);	/* unit: 100ms */
35086169Shx147065 	led.id = id;
35096169Shx147065 	led.off = off;
35106169Shx147065 	led.on = on;
35116169Shx147065 
35126169Shx147065 	(void) iwk_cmd(sc, REPLY_LEDS_CMD, &led, sizeof (led), 1);
35136169Shx147065 }
35146169Shx147065 
35156169Shx147065 static int
iwk_hw_set_before_auth(iwk_sc_t * sc)35166169Shx147065 iwk_hw_set_before_auth(iwk_sc_t *sc)
35176169Shx147065 {
35186169Shx147065 	ieee80211com_t *ic = &sc->sc_ic;
35196169Shx147065 	ieee80211_node_t *in = ic->ic_bss;
35206169Shx147065 	iwk_add_sta_t node;
35216169Shx147065 	iwk_link_quality_cmd_t link_quality;
35226169Shx147065 	struct ieee80211_rateset rs;
35236169Shx147065 	uint16_t masks = 0, rate;
35246169Shx147065 	int i, err;
35256169Shx147065 
35268727SPengcheng.Chen@Sun.COM 	if (in->in_chan == IEEE80211_CHAN_ANYC) {
35278727SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "iwk_hw_set_before_auth():"
35288727SPengcheng.Chen@Sun.COM 		    "channel (%d) isn't in proper range\n",
35299327SFei.Feng@Sun.COM 		    LE_16(ieee80211_chan2ieee(ic, in->in_chan)));
35308727SPengcheng.Chen@Sun.COM 		return (IWK_FAIL);
35318727SPengcheng.Chen@Sun.COM 	}
35328727SPengcheng.Chen@Sun.COM 
35336169Shx147065 	/* update adapter's configuration according the info of target AP */
35346169Shx147065 	IEEE80211_ADDR_COPY(sc->sc_config.bssid, in->in_bssid);
35359327SFei.Feng@Sun.COM 	sc->sc_config.chan = LE_16(ieee80211_chan2ieee(ic, in->in_chan));
35366169Shx147065 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
35376169Shx147065 		sc->sc_config.cck_basic_rates  = 0x03;
35386169Shx147065 		sc->sc_config.ofdm_basic_rates = 0;
35396169Shx147065 	} else if ((in->in_chan != IEEE80211_CHAN_ANYC) &&
35406169Shx147065 	    (IEEE80211_IS_CHAN_5GHZ(in->in_chan))) {
35416169Shx147065 		sc->sc_config.cck_basic_rates  = 0;
35426169Shx147065 		sc->sc_config.ofdm_basic_rates = 0x15;
35436169Shx147065 	} else { /* assume 802.11b/g */
35446169Shx147065 		sc->sc_config.cck_basic_rates  = 0x0f;
35456169Shx147065 		sc->sc_config.ofdm_basic_rates = 0xff;
35466169Shx147065 	}
35476169Shx147065 
35486169Shx147065 	sc->sc_config.flags &= ~LE_32(RXON_FLG_SHORT_PREAMBLE_MSK |
35496169Shx147065 	    RXON_FLG_SHORT_SLOT_MSK);
35506169Shx147065 
35516169Shx147065 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
35526169Shx147065 		sc->sc_config.flags |= LE_32(RXON_FLG_SHORT_SLOT_MSK);
35536169Shx147065 	else
35546169Shx147065 		sc->sc_config.flags &= LE_32(~RXON_FLG_SHORT_SLOT_MSK);
35556169Shx147065 
35566169Shx147065 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
35576169Shx147065 		sc->sc_config.flags |= LE_32(RXON_FLG_SHORT_PREAMBLE_MSK);
35586169Shx147065 	else
35596169Shx147065 		sc->sc_config.flags &= LE_32(~RXON_FLG_SHORT_PREAMBLE_MSK);
35606169Shx147065 
35616169Shx147065 	IWK_DBG((IWK_DEBUG_80211, "config chan %d flags %x "
35626169Shx147065 	    "filter_flags %x  cck %x ofdm %x"
35636169Shx147065 	    " bssid:%02x:%02x:%02x:%02x:%02x:%2x\n",
35649327SFei.Feng@Sun.COM 	    LE_16(sc->sc_config.chan), LE_32(sc->sc_config.flags),
35659327SFei.Feng@Sun.COM 	    LE_32(sc->sc_config.filter_flags),
35666169Shx147065 	    sc->sc_config.cck_basic_rates, sc->sc_config.ofdm_basic_rates,
35676169Shx147065 	    sc->sc_config.bssid[0], sc->sc_config.bssid[1],
35686169Shx147065 	    sc->sc_config.bssid[2], sc->sc_config.bssid[3],
35696169Shx147065 	    sc->sc_config.bssid[4], sc->sc_config.bssid[5]));
35706169Shx147065 	err = iwk_cmd(sc, REPLY_RXON, &sc->sc_config,
35716169Shx147065 	    sizeof (iwk_rxon_cmd_t), 1);
35726169Shx147065 	if (err != IWK_SUCCESS) {
35736169Shx147065 		cmn_err(CE_WARN, "iwk_hw_set_before_auth():"
35746169Shx147065 		    " failed to config chan%d\n",
35756169Shx147065 		    sc->sc_config.chan);
35766169Shx147065 		return (err);
35776169Shx147065 	}
35786169Shx147065 
35797569SXinghua.Wen@Sun.COM 	/* obtain current temperature of chipset */
35807569SXinghua.Wen@Sun.COM 	sc->sc_tempera = iwk_curr_tempera(sc);
35817569SXinghua.Wen@Sun.COM 
35827569SXinghua.Wen@Sun.COM 	/* make Tx power calibration to determine the gains of DSP and radio */
35837569SXinghua.Wen@Sun.COM 	err = iwk_tx_power_calibration(sc);
35847569SXinghua.Wen@Sun.COM 	if (err) {
35856169Shx147065 		cmn_err(CE_WARN, "iwk_hw_set_before_auth():"
35867569SXinghua.Wen@Sun.COM 		    "failed to set tx power table\n");
35876169Shx147065 		return (err);
35886169Shx147065 	}
35896169Shx147065 
35906169Shx147065 	/* add default AP node */
35916169Shx147065 	(void) memset(&node, 0, sizeof (node));
35926169Shx147065 	IEEE80211_ADDR_COPY(node.bssid, in->in_bssid);
35936169Shx147065 	node.id = IWK_AP_ID;
35946169Shx147065 	err = iwk_cmd(sc, REPLY_ADD_STA, &node, sizeof (node), 1);
35956169Shx147065 	if (err != IWK_SUCCESS) {
35967569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_hw_set_before_auth(): "
35977569SXinghua.Wen@Sun.COM 		    "failed to add BSS node\n");
35986169Shx147065 		return (err);
35996169Shx147065 	}
36006169Shx147065 
36018594SFei.Feng@Sun.COM 	/* TX_LINK_QUALITY cmd */
36026169Shx147065 	(void) memset(&link_quality, 0, sizeof (link_quality));
36036169Shx147065 	rs = ic->ic_sup_rates[ieee80211_chan2mode(ic, ic->ic_curchan)];
36046169Shx147065 	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
36056169Shx147065 		if (i < rs.ir_nrates)
36066169Shx147065 			rate = rs.ir_rates[rs.ir_nrates - i];
36076169Shx147065 		else
36086169Shx147065 			rate = 2;
36096169Shx147065 		if (rate == 2 || rate == 4 || rate == 11 || rate == 22)
36106169Shx147065 			masks |= RATE_MCS_CCK_MSK;
36116169Shx147065 		masks |= RATE_MCS_ANT_B_MSK;
36126169Shx147065 		masks &= ~RATE_MCS_ANT_A_MSK;
36136169Shx147065 		link_quality.rate_n_flags[i] =
36149327SFei.Feng@Sun.COM 		    LE_32(iwk_rate_to_plcp(rate) | masks);
36156169Shx147065 	}
36166169Shx147065 
36176169Shx147065 	link_quality.general_params.single_stream_ant_msk = 2;
36186169Shx147065 	link_quality.general_params.dual_stream_ant_msk = 3;
36196169Shx147065 	link_quality.agg_params.agg_dis_start_th = 3;
36206169Shx147065 	link_quality.agg_params.agg_time_limit = LE_16(4000);
36216169Shx147065 	link_quality.sta_id = IWK_AP_ID;
36226169Shx147065 	err = iwk_cmd(sc, REPLY_TX_LINK_QUALITY_CMD, &link_quality,
36236169Shx147065 	    sizeof (link_quality), 1);
36246169Shx147065 	if (err != IWK_SUCCESS) {
36256169Shx147065 		cmn_err(CE_WARN, "iwk_hw_set_before_auth(): "
36266169Shx147065 		    "failed to config link quality table\n");
36276169Shx147065 		return (err);
36286169Shx147065 	}
36296169Shx147065 
36306169Shx147065 	return (IWK_SUCCESS);
36316169Shx147065 }
36326169Shx147065 
36336169Shx147065 /*
36346169Shx147065  * Send a scan request(assembly scan cmd) to the firmware.
36356169Shx147065  */
36366169Shx147065 static int
iwk_scan(iwk_sc_t * sc)36376169Shx147065 iwk_scan(iwk_sc_t *sc)
36386169Shx147065 {
36396169Shx147065 	ieee80211com_t *ic = &sc->sc_ic;
36406169Shx147065 	iwk_tx_ring_t *ring = &sc->sc_txq[IWK_CMD_QUEUE_NUM];
36416169Shx147065 	iwk_tx_desc_t *desc;
36426169Shx147065 	iwk_tx_data_t *data;
36436169Shx147065 	iwk_cmd_t *cmd;
36446169Shx147065 	iwk_scan_hdr_t *hdr;
36456169Shx147065 	iwk_scan_chan_t *chan;
36466169Shx147065 	struct ieee80211_frame *wh;
36476169Shx147065 	ieee80211_node_t *in = ic->ic_bss;
36487691SPengcheng.Chen@Sun.COM 	uint8_t essid[IEEE80211_NWID_LEN+1];
36496169Shx147065 	struct ieee80211_rateset *rs;
36506169Shx147065 	enum ieee80211_phymode mode;
36516169Shx147065 	uint8_t *frm;
36526169Shx147065 	int i, pktlen, nrates;
36536169Shx147065 
36546169Shx147065 	data = &ring->data[ring->cur];
36556169Shx147065 	desc = data->desc;
36566169Shx147065 	cmd = (iwk_cmd_t *)data->dma_data.mem_va;
36576169Shx147065 
36586169Shx147065 	cmd->hdr.type = REPLY_SCAN_CMD;
36596169Shx147065 	cmd->hdr.flags = 0;
36606169Shx147065 	cmd->hdr.qid = ring->qid;
36616169Shx147065 	cmd->hdr.idx = ring->cur | 0x40;
36626169Shx147065 
36636169Shx147065 	hdr = (iwk_scan_hdr_t *)cmd->data;
36646169Shx147065 	(void) memset(hdr, 0, sizeof (iwk_scan_hdr_t));
36657691SPengcheng.Chen@Sun.COM 	hdr->nchan = 1;
36667691SPengcheng.Chen@Sun.COM 	hdr->quiet_time = LE_16(50);
36676169Shx147065 	hdr->quiet_plcp_th = LE_16(1);
36686169Shx147065 
36699327SFei.Feng@Sun.COM 	hdr->flags = LE_32(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK);
36709327SFei.Feng@Sun.COM 	hdr->rx_chain = LE_16(RXON_RX_CHAIN_DRIVER_FORCE_MSK |
36719327SFei.Feng@Sun.COM 	    (0x7 << RXON_RX_CHAIN_VALID_POS) |
36726169Shx147065 	    (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
36736169Shx147065 	    (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
36746169Shx147065 
36759327SFei.Feng@Sun.COM 	hdr->tx_cmd.tx_flags = LE_32(TX_CMD_FLG_SEQ_CTL_MSK);
36766169Shx147065 	hdr->tx_cmd.sta_id = IWK_BROADCAST_ID;
36779327SFei.Feng@Sun.COM 	hdr->tx_cmd.stop_time.life_time = LE_32(0xffffffff);
36789327SFei.Feng@Sun.COM 	hdr->tx_cmd.tx_flags |= LE_32(0x200);
36799327SFei.Feng@Sun.COM 	hdr->tx_cmd.rate.r.rate_n_flags = LE_32(iwk_rate_to_plcp(2));
36806169Shx147065 	hdr->tx_cmd.rate.r.rate_n_flags |=
36819327SFei.Feng@Sun.COM 	    LE_32(RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
36826169Shx147065 	hdr->direct_scan[0].len = ic->ic_des_esslen;
36836169Shx147065 	hdr->direct_scan[0].id  = IEEE80211_ELEMID_SSID;
36846169Shx147065 
36857691SPengcheng.Chen@Sun.COM 	if (ic->ic_des_esslen) {
36867691SPengcheng.Chen@Sun.COM 		bcopy(ic->ic_des_essid, essid, ic->ic_des_esslen);
36877691SPengcheng.Chen@Sun.COM 		essid[ic->ic_des_esslen] = '\0';
36887691SPengcheng.Chen@Sun.COM 		IWK_DBG((IWK_DEBUG_SCAN, "directed scan %s\n", essid));
36897691SPengcheng.Chen@Sun.COM 
36906169Shx147065 		bcopy(ic->ic_des_essid, hdr->direct_scan[0].ssid,
36916169Shx147065 		    ic->ic_des_esslen);
36927691SPengcheng.Chen@Sun.COM 	} else {
36936169Shx147065 		bzero(hdr->direct_scan[0].ssid,
36946169Shx147065 		    sizeof (hdr->direct_scan[0].ssid));
36957691SPengcheng.Chen@Sun.COM 	}
36966169Shx147065 	/*
36976169Shx147065 	 * a probe request frame is required after the REPLY_SCAN_CMD
36986169Shx147065 	 */
36996169Shx147065 	wh = (struct ieee80211_frame *)(hdr + 1);
37006169Shx147065 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
37016169Shx147065 	    IEEE80211_FC0_SUBTYPE_PROBE_REQ;
37026169Shx147065 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
37036169Shx147065 	(void) memset(wh->i_addr1, 0xff, 6);
37046169Shx147065 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_macaddr);
37056169Shx147065 	(void) memset(wh->i_addr3, 0xff, 6);
37066169Shx147065 	*(uint16_t *)&wh->i_dur[0] = 0;
37076169Shx147065 	*(uint16_t *)&wh->i_seq[0] = 0;
37086169Shx147065 
37096169Shx147065 	frm = (uint8_t *)(wh + 1);
37106169Shx147065 
37116169Shx147065 	/* essid IE */
37127691SPengcheng.Chen@Sun.COM 	if (in->in_esslen) {
37137691SPengcheng.Chen@Sun.COM 		bcopy(in->in_essid, essid, in->in_esslen);
37147691SPengcheng.Chen@Sun.COM 		essid[in->in_esslen] = '\0';
37157691SPengcheng.Chen@Sun.COM 		IWK_DBG((IWK_DEBUG_SCAN, "probe with ESSID %s\n",
37167691SPengcheng.Chen@Sun.COM 		    essid));
37177691SPengcheng.Chen@Sun.COM 	}
37186169Shx147065 	*frm++ = IEEE80211_ELEMID_SSID;
37196169Shx147065 	*frm++ = in->in_esslen;
37206169Shx147065 	(void) memcpy(frm, in->in_essid, in->in_esslen);
37216169Shx147065 	frm += in->in_esslen;
37226169Shx147065 
37236169Shx147065 	mode = ieee80211_chan2mode(ic, ic->ic_curchan);
37246169Shx147065 	rs = &ic->ic_sup_rates[mode];
37256169Shx147065 
37266169Shx147065 	/* supported rates IE */
37276169Shx147065 	*frm++ = IEEE80211_ELEMID_RATES;
37286169Shx147065 	nrates = rs->ir_nrates;
37296169Shx147065 	if (nrates > IEEE80211_RATE_SIZE)
37306169Shx147065 		nrates = IEEE80211_RATE_SIZE;
37316169Shx147065 	*frm++ = (uint8_t)nrates;
37326169Shx147065 	(void) memcpy(frm, rs->ir_rates, nrates);
37336169Shx147065 	frm += nrates;
37346169Shx147065 
37356169Shx147065 	/* supported xrates IE */
37366169Shx147065 	if (rs->ir_nrates > IEEE80211_RATE_SIZE) {
37376169Shx147065 		nrates = rs->ir_nrates - IEEE80211_RATE_SIZE;
37386169Shx147065 		*frm++ = IEEE80211_ELEMID_XRATES;
37396169Shx147065 		*frm++ = (uint8_t)nrates;
37406169Shx147065 		(void) memcpy(frm, rs->ir_rates + IEEE80211_RATE_SIZE, nrates);
37416169Shx147065 		frm += nrates;
37426169Shx147065 	}
37436169Shx147065 
37446169Shx147065 	/* optionnal IE (usually for wpa) */
37456169Shx147065 	if (ic->ic_opt_ie != NULL) {
37466169Shx147065 		(void) memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
37476169Shx147065 		frm += ic->ic_opt_ie_len;
37486169Shx147065 	}
37496169Shx147065 
37506169Shx147065 	/* setup length of probe request */
37517249Sff224033 	hdr->tx_cmd.len = LE_16(_PTRDIFF(frm, wh));
37529327SFei.Feng@Sun.COM 	hdr->len = LE_16(hdr->nchan * sizeof (iwk_scan_chan_t) +
37539327SFei.Feng@Sun.COM 	    LE_16(hdr->tx_cmd.len) + sizeof (iwk_scan_hdr_t));
37546169Shx147065 
37556169Shx147065 	/*
37566169Shx147065 	 * the attribute of the scan channels are required after the probe
37576169Shx147065 	 * request frame.
37586169Shx147065 	 */
37596169Shx147065 	chan = (iwk_scan_chan_t *)frm;
37606169Shx147065 	for (i = 1; i <= hdr->nchan; i++, chan++) {
37617691SPengcheng.Chen@Sun.COM 		if (ic->ic_des_esslen) {
37627691SPengcheng.Chen@Sun.COM 			chan->type = 3;
37637691SPengcheng.Chen@Sun.COM 		} else {
37647691SPengcheng.Chen@Sun.COM 			chan->type = 1;
37657691SPengcheng.Chen@Sun.COM 		}
37667691SPengcheng.Chen@Sun.COM 
37677691SPengcheng.Chen@Sun.COM 		chan->chan = ieee80211_chan2ieee(ic, ic->ic_curchan);
37686169Shx147065 		chan->tpc.tx_gain = 0x3f;
37696169Shx147065 		chan->tpc.dsp_atten = 110;
37707691SPengcheng.Chen@Sun.COM 		chan->active_dwell = LE_16(50);
37716169Shx147065 		chan->passive_dwell = LE_16(120);
37726169Shx147065 
37736169Shx147065 		frm += sizeof (iwk_scan_chan_t);
37746169Shx147065 	}
37756169Shx147065 
37767249Sff224033 	pktlen = _PTRDIFF(frm, cmd);
37776169Shx147065 
37786169Shx147065 	(void) memset(desc, 0, sizeof (*desc));
37799327SFei.Feng@Sun.COM 	desc->val0 = 1 << 24;
37806169Shx147065 	desc->pa[0].tb1_addr =
37816169Shx147065 	    (uint32_t)(data->dma_data.cookie.dmac_address & 0xffffffff);
37826169Shx147065 	desc->pa[0].val1 = (pktlen << 4) & 0xfff0;
37836169Shx147065 
37846169Shx147065 	/*
37856169Shx147065 	 * maybe for cmd, filling the byte cnt table is not necessary.
37866169Shx147065 	 * anyway, we fill it here.
37876169Shx147065 	 */
37887569SXinghua.Wen@Sun.COM 	sc->sc_shared->queues_byte_cnt_tbls[ring->qid].
37897569SXinghua.Wen@Sun.COM 	    tfd_offset[ring->cur].val = 8;
37906169Shx147065 	if (ring->cur < IWK_MAX_WIN_SIZE) {
37917569SXinghua.Wen@Sun.COM 		sc->sc_shared->queues_byte_cnt_tbls[ring->qid].
37927569SXinghua.Wen@Sun.COM 		    tfd_offset[IWK_QUEUE_SIZE + ring->cur].val = 8;
37936169Shx147065 	}
37946249Shx147065 
37956169Shx147065 	/* kick cmd ring */
37966169Shx147065 	ring->cur = (ring->cur + 1) % ring->count;
37976169Shx147065 	IWK_WRITE(sc, HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
37986169Shx147065 
37996169Shx147065 	return (IWK_SUCCESS);
38006169Shx147065 }
38016169Shx147065 
38026169Shx147065 static int
iwk_config(iwk_sc_t * sc)38036169Shx147065 iwk_config(iwk_sc_t *sc)
38046169Shx147065 {
38056169Shx147065 	ieee80211com_t *ic = &sc->sc_ic;
38066169Shx147065 	iwk_powertable_cmd_t powertable;
38076169Shx147065 	iwk_bt_cmd_t bt;
38086169Shx147065 	iwk_add_sta_t node;
38096169Shx147065 	iwk_link_quality_cmd_t link_quality;
38106169Shx147065 	int i, err;
38116169Shx147065 	uint16_t masks = 0;
38126169Shx147065 
38136169Shx147065 	/*
38146169Shx147065 	 * set power mode. Disable power management at present, do it later
38156169Shx147065 	 */
38166169Shx147065 	(void) memset(&powertable, 0, sizeof (powertable));
38176169Shx147065 	powertable.flags = LE_16(0x8);
38186169Shx147065 	err = iwk_cmd(sc, POWER_TABLE_CMD, &powertable,
38196169Shx147065 	    sizeof (powertable), 0);
38206169Shx147065 	if (err != IWK_SUCCESS) {
38216169Shx147065 		cmn_err(CE_WARN, "iwk_config(): failed to set power mode\n");
38226169Shx147065 		return (err);
38236169Shx147065 	}
38246169Shx147065 
38256169Shx147065 	/* configure bt coexistence */
38266169Shx147065 	(void) memset(&bt, 0, sizeof (bt));
38276169Shx147065 	bt.flags = 3;
38286169Shx147065 	bt.lead_time = 0xaa;
38296169Shx147065 	bt.max_kill = 1;
38306169Shx147065 	err = iwk_cmd(sc, REPLY_BT_CONFIG, &bt,
38316169Shx147065 	    sizeof (bt), 0);
38326169Shx147065 	if (err != IWK_SUCCESS) {
38336169Shx147065 		cmn_err(CE_WARN,
38346169Shx147065 		    "iwk_config(): "
38356169Shx147065 		    "failed to configurate bt coexistence\n");
38366169Shx147065 		return (err);
38376169Shx147065 	}
38386169Shx147065 
38396169Shx147065 	/* configure rxon */
38406169Shx147065 	(void) memset(&sc->sc_config, 0, sizeof (iwk_rxon_cmd_t));
38416169Shx147065 	IEEE80211_ADDR_COPY(sc->sc_config.node_addr, ic->ic_macaddr);
38426169Shx147065 	IEEE80211_ADDR_COPY(sc->sc_config.wlap_bssid, ic->ic_macaddr);
38439327SFei.Feng@Sun.COM 	sc->sc_config.chan = LE_16(ieee80211_chan2ieee(ic, ic->ic_curchan));
38449327SFei.Feng@Sun.COM 	sc->sc_config.flags = LE_32(RXON_FLG_TSF2HOST_MSK |
38457569SXinghua.Wen@Sun.COM 	    RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_BAND_24G_MSK);
38469327SFei.Feng@Sun.COM 	sc->sc_config.flags &= LE_32(~RXON_FLG_CCK_MSK);
38476169Shx147065 	switch (ic->ic_opmode) {
38486169Shx147065 	case IEEE80211_M_STA:
38496169Shx147065 		sc->sc_config.dev_type = RXON_DEV_TYPE_ESS;
38506169Shx147065 		sc->sc_config.filter_flags |= LE_32(RXON_FILTER_ACCEPT_GRP_MSK |
38516169Shx147065 		    RXON_FILTER_DIS_DECRYPT_MSK |
38526169Shx147065 		    RXON_FILTER_DIS_GRP_DECRYPT_MSK);
38536169Shx147065 		break;
38548594SFei.Feng@Sun.COM 	case IEEE80211_M_IBSS:
38556169Shx147065 	case IEEE80211_M_AHDEMO:
38566169Shx147065 		sc->sc_config.dev_type = RXON_DEV_TYPE_IBSS;
38579327SFei.Feng@Sun.COM 		sc->sc_config.flags |= LE_32(RXON_FLG_SHORT_PREAMBLE_MSK);
38587569SXinghua.Wen@Sun.COM 		sc->sc_config.filter_flags = LE_32(RXON_FILTER_ACCEPT_GRP_MSK |
38597569SXinghua.Wen@Sun.COM 		    RXON_FILTER_DIS_DECRYPT_MSK |
38607569SXinghua.Wen@Sun.COM 		    RXON_FILTER_DIS_GRP_DECRYPT_MSK);
38616169Shx147065 		break;
38626169Shx147065 	case IEEE80211_M_HOSTAP:
38636169Shx147065 		sc->sc_config.dev_type = RXON_DEV_TYPE_AP;
38646169Shx147065 		break;
38656169Shx147065 	case IEEE80211_M_MONITOR:
38666169Shx147065 		sc->sc_config.dev_type = RXON_DEV_TYPE_SNIFFER;
38676169Shx147065 		sc->sc_config.filter_flags |= LE_32(RXON_FILTER_ACCEPT_GRP_MSK |
38686169Shx147065 		    RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
38696169Shx147065 		break;
38706169Shx147065 	}
38716169Shx147065 	sc->sc_config.cck_basic_rates  = 0x0f;
38726169Shx147065 	sc->sc_config.ofdm_basic_rates = 0xff;
38736169Shx147065 
38746169Shx147065 	sc->sc_config.ofdm_ht_single_stream_basic_rates = 0xff;
38756169Shx147065 	sc->sc_config.ofdm_ht_dual_stream_basic_rates = 0xff;
38766169Shx147065 
38776169Shx147065 	/* set antenna */
38786169Shx147065 
38799327SFei.Feng@Sun.COM 	sc->sc_config.rx_chain = LE_16(RXON_RX_CHAIN_DRIVER_FORCE_MSK |
38809327SFei.Feng@Sun.COM 	    (0x7 << RXON_RX_CHAIN_VALID_POS) |
38816169Shx147065 	    (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
38826169Shx147065 	    (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
38836169Shx147065 
38846169Shx147065 	err = iwk_cmd(sc, REPLY_RXON, &sc->sc_config,
38856169Shx147065 	    sizeof (iwk_rxon_cmd_t), 0);
38866169Shx147065 	if (err != IWK_SUCCESS) {
38876169Shx147065 		cmn_err(CE_WARN, "iwk_config(): "
38886169Shx147065 		    "failed to set configure command\n");
38896169Shx147065 		return (err);
38906169Shx147065 	}
38917569SXinghua.Wen@Sun.COM 	/* obtain current temperature of chipset */
38927569SXinghua.Wen@Sun.COM 	sc->sc_tempera = iwk_curr_tempera(sc);
38937569SXinghua.Wen@Sun.COM 
38947569SXinghua.Wen@Sun.COM 	/* make Tx power calibration to determine the gains of DSP and radio */
38957569SXinghua.Wen@Sun.COM 	err = iwk_tx_power_calibration(sc);
38967569SXinghua.Wen@Sun.COM 	if (err) {
38977569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_config(): "
38987569SXinghua.Wen@Sun.COM 		    "failed to set tx power table\n");
38996169Shx147065 		return (err);
39006169Shx147065 	}
39016169Shx147065 
39026169Shx147065 	/* add broadcast node so that we can send broadcast frame */
39036169Shx147065 	(void) memset(&node, 0, sizeof (node));
39046169Shx147065 	(void) memset(node.bssid, 0xff, 6);
39056169Shx147065 	node.id = IWK_BROADCAST_ID;
39066169Shx147065 	err = iwk_cmd(sc, REPLY_ADD_STA, &node, sizeof (node), 0);
39076169Shx147065 	if (err != IWK_SUCCESS) {
39086169Shx147065 		cmn_err(CE_WARN, "iwk_config(): "
39096169Shx147065 		    "failed to add broadcast node\n");
39106169Shx147065 		return (err);
39116169Shx147065 	}
39126169Shx147065 
39136169Shx147065 	/* TX_LINK_QUALITY cmd ? */
39146169Shx147065 	(void) memset(&link_quality, 0, sizeof (link_quality));
39156169Shx147065 	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
39166169Shx147065 		masks |= RATE_MCS_CCK_MSK;
39176169Shx147065 		masks |= RATE_MCS_ANT_B_MSK;
39186169Shx147065 		masks &= ~RATE_MCS_ANT_A_MSK;
39199327SFei.Feng@Sun.COM 		link_quality.rate_n_flags[i] =
39209327SFei.Feng@Sun.COM 		    LE_32(iwk_rate_to_plcp(2) | masks);
39216169Shx147065 	}
39226169Shx147065 
39236169Shx147065 	link_quality.general_params.single_stream_ant_msk = 2;
39246169Shx147065 	link_quality.general_params.dual_stream_ant_msk = 3;
39256169Shx147065 	link_quality.agg_params.agg_dis_start_th = 3;
39266169Shx147065 	link_quality.agg_params.agg_time_limit = LE_16(4000);
39276169Shx147065 	link_quality.sta_id = IWK_BROADCAST_ID;
39286169Shx147065 	err = iwk_cmd(sc, REPLY_TX_LINK_QUALITY_CMD, &link_quality,
39296169Shx147065 	    sizeof (link_quality), 0);
39306169Shx147065 	if (err != IWK_SUCCESS) {
39316169Shx147065 		cmn_err(CE_WARN, "iwk_config(): "
39326169Shx147065 		    "failed to config link quality table\n");
39336169Shx147065 		return (err);
39346169Shx147065 	}
39356169Shx147065 
39366169Shx147065 	return (IWK_SUCCESS);
39376169Shx147065 }
39386169Shx147065 
39396169Shx147065 static void
iwk_stop_master(iwk_sc_t * sc)39406169Shx147065 iwk_stop_master(iwk_sc_t *sc)
39416169Shx147065 {
39426169Shx147065 	uint32_t tmp;
39436169Shx147065 	int n;
39446169Shx147065 
39456169Shx147065 	tmp = IWK_READ(sc, CSR_RESET);
39466169Shx147065 	IWK_WRITE(sc, CSR_RESET, tmp | CSR_RESET_REG_FLAG_STOP_MASTER);
39476169Shx147065 
39486169Shx147065 	tmp = IWK_READ(sc, CSR_GP_CNTRL);
39496169Shx147065 	if ((tmp & CSR_GP_CNTRL_REG_MSK_POWER_SAVE_TYPE) ==
39506169Shx147065 	    CSR_GP_CNTRL_REG_FLAG_MAC_POWER_SAVE)
39516169Shx147065 		return;
39526169Shx147065 
39536169Shx147065 	for (n = 0; n < 2000; n++) {
39546169Shx147065 		if (IWK_READ(sc, CSR_RESET) &
39556169Shx147065 		    CSR_RESET_REG_FLAG_MASTER_DISABLED)
39566169Shx147065 			break;
39576169Shx147065 		DELAY(1000);
39586169Shx147065 	}
39596169Shx147065 	if (n == 2000)
39606169Shx147065 		IWK_DBG((IWK_DEBUG_HW,
39616169Shx147065 		    "timeout waiting for master stop\n"));
39626169Shx147065 }
39636169Shx147065 
39646169Shx147065 static int
iwk_power_up(iwk_sc_t * sc)39656169Shx147065 iwk_power_up(iwk_sc_t *sc)
39666169Shx147065 {
39676169Shx147065 	uint32_t tmp;
39686169Shx147065 
39696169Shx147065 	iwk_mac_access_enter(sc);
39706169Shx147065 	tmp = iwk_reg_read(sc, ALM_APMG_PS_CTL);
39716169Shx147065 	tmp &= ~APMG_PS_CTRL_REG_MSK_POWER_SRC;
39726169Shx147065 	tmp |= APMG_PS_CTRL_REG_VAL_POWER_SRC_VMAIN;
39736169Shx147065 	iwk_reg_write(sc, ALM_APMG_PS_CTL, tmp);
39746169Shx147065 	iwk_mac_access_exit(sc);
39756169Shx147065 
39766169Shx147065 	DELAY(5000);
39776169Shx147065 	return (IWK_SUCCESS);
39786169Shx147065 }
39796169Shx147065 
39806169Shx147065 static int
iwk_preinit(iwk_sc_t * sc)39816169Shx147065 iwk_preinit(iwk_sc_t *sc)
39826169Shx147065 {
39836169Shx147065 	uint32_t tmp;
39846169Shx147065 	int n;
39856169Shx147065 	uint8_t vlink;
39866169Shx147065 
39876169Shx147065 	/* clear any pending interrupts */
39886169Shx147065 	IWK_WRITE(sc, CSR_INT, 0xffffffff);
39896169Shx147065 
39906169Shx147065 	tmp = IWK_READ(sc, CSR_GIO_CHICKEN_BITS);
39916169Shx147065 	IWK_WRITE(sc, CSR_GIO_CHICKEN_BITS,
39926169Shx147065 	    tmp | CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
39936169Shx147065 
39946169Shx147065 	tmp = IWK_READ(sc, CSR_GP_CNTRL);
39956169Shx147065 	IWK_WRITE(sc, CSR_GP_CNTRL, tmp | CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
39966169Shx147065 
39976169Shx147065 	/* wait for clock ready */
39986169Shx147065 	for (n = 0; n < 1000; n++) {
39996169Shx147065 		if (IWK_READ(sc, CSR_GP_CNTRL) &
40006169Shx147065 		    CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY)
40016169Shx147065 			break;
40026169Shx147065 		DELAY(10);
40036169Shx147065 	}
40046169Shx147065 	if (n == 1000) {
40057691SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN,
40067691SPengcheng.Chen@Sun.COM 		    "iwk_preinit(): timeout waiting for clock ready\n");
40076169Shx147065 		return (ETIMEDOUT);
40086169Shx147065 	}
40096169Shx147065 	iwk_mac_access_enter(sc);
40106169Shx147065 	tmp = iwk_reg_read(sc, APMG_CLK_CTRL_REG);
40116169Shx147065 	iwk_reg_write(sc, APMG_CLK_CTRL_REG, tmp |
40126169Shx147065 	    APMG_CLK_REG_VAL_DMA_CLK_RQT | APMG_CLK_REG_VAL_BSM_CLK_RQT);
40136169Shx147065 
40146169Shx147065 	DELAY(20);
40156169Shx147065 	tmp = iwk_reg_read(sc, ALM_APMG_PCIDEV_STT);
40166169Shx147065 	iwk_reg_write(sc, ALM_APMG_PCIDEV_STT, tmp |
40176169Shx147065 	    APMG_DEV_STATE_REG_VAL_L1_ACTIVE_DISABLE);
40186169Shx147065 	iwk_mac_access_exit(sc);
40196169Shx147065 
40206169Shx147065 	IWK_WRITE(sc, CSR_INT_COALESCING, 512 / 32); /* ??? */
40216169Shx147065 
40226169Shx147065 	(void) iwk_power_up(sc);
40236169Shx147065 
40246169Shx147065 	if ((sc->sc_rev & 0x80) == 0x80 && (sc->sc_rev & 0x7f) < 8) {
40256169Shx147065 		tmp = ddi_get32(sc->sc_cfg_handle,
40266169Shx147065 		    (uint32_t *)(sc->sc_cfg_base + 0xe8));
40276169Shx147065 		ddi_put32(sc->sc_cfg_handle,
40286169Shx147065 		    (uint32_t *)(sc->sc_cfg_base + 0xe8),
40296169Shx147065 		    tmp & ~(1 << 11));
40306169Shx147065 	}
40316169Shx147065 
40326169Shx147065 
40336169Shx147065 	vlink = ddi_get8(sc->sc_cfg_handle,
40346169Shx147065 	    (uint8_t *)(sc->sc_cfg_base + 0xf0));
40356169Shx147065 	ddi_put8(sc->sc_cfg_handle, (uint8_t *)(sc->sc_cfg_base + 0xf0),
40366169Shx147065 	    vlink & ~2);
40376169Shx147065 
40386169Shx147065 	tmp = IWK_READ(sc, CSR_SW_VER);
40396169Shx147065 	tmp |= CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
40407569SXinghua.Wen@Sun.COM 	    CSR_HW_IF_CONFIG_REG_BIT_MAC_SI |
40417569SXinghua.Wen@Sun.COM 	    CSR_HW_IF_CONFIG_REG_BIT_KEDRON_R;
40426169Shx147065 	IWK_WRITE(sc, CSR_SW_VER, tmp);
40436169Shx147065 
40446169Shx147065 	/* make sure power supply on each part of the hardware */
40456169Shx147065 	iwk_mac_access_enter(sc);
40466169Shx147065 	tmp = iwk_reg_read(sc, ALM_APMG_PS_CTL);
40476169Shx147065 	tmp |= APMG_PS_CTRL_REG_VAL_ALM_R_RESET_REQ;
40486169Shx147065 	iwk_reg_write(sc, ALM_APMG_PS_CTL, tmp);
40496169Shx147065 	DELAY(5);
40506169Shx147065 	tmp = iwk_reg_read(sc, ALM_APMG_PS_CTL);
40516169Shx147065 	tmp &= ~APMG_PS_CTRL_REG_VAL_ALM_R_RESET_REQ;
40526169Shx147065 	iwk_reg_write(sc, ALM_APMG_PS_CTL, tmp);
40536169Shx147065 	iwk_mac_access_exit(sc);
40546169Shx147065 	return (IWK_SUCCESS);
40556169Shx147065 }
40566169Shx147065 
40576169Shx147065 /*
40586169Shx147065  * set up semphore flag to own EEPROM
40596169Shx147065  */
iwk_eep_sem_down(iwk_sc_t * sc)40606169Shx147065 static int iwk_eep_sem_down(iwk_sc_t *sc)
40616169Shx147065 {
40626169Shx147065 	int count1, count2;
40636169Shx147065 	uint32_t tmp;
40646169Shx147065 
40656169Shx147065 	for (count1 = 0; count1 < 1000; count1++) {
40666169Shx147065 		tmp = IWK_READ(sc, CSR_HW_IF_CONFIG_REG);
40676169Shx147065 		IWK_WRITE(sc, CSR_HW_IF_CONFIG_REG,
40686169Shx147065 		    tmp | CSR_HW_IF_CONFIG_REG_EEP_SEM);
40696169Shx147065 
40706169Shx147065 		for (count2 = 0; count2 < 2; count2++) {
40716169Shx147065 			if (IWK_READ(sc, CSR_HW_IF_CONFIG_REG) &
40726169Shx147065 			    CSR_HW_IF_CONFIG_REG_EEP_SEM)
40736169Shx147065 				return (IWK_SUCCESS);
40746169Shx147065 			DELAY(10000);
40756169Shx147065 		}
40766169Shx147065 	}
40776169Shx147065 	return (IWK_FAIL);
40786169Shx147065 }
40796169Shx147065 
40806169Shx147065 /*
40816169Shx147065  * reset semphore flag to release EEPROM
40826169Shx147065  */
iwk_eep_sem_up(iwk_sc_t * sc)40836169Shx147065 static void iwk_eep_sem_up(iwk_sc_t *sc)
40846169Shx147065 {
40856169Shx147065 	uint32_t tmp;
40866169Shx147065 
40876169Shx147065 	tmp = IWK_READ(sc, CSR_HW_IF_CONFIG_REG);
40886169Shx147065 	IWK_WRITE(sc, CSR_HW_IF_CONFIG_REG,
40896169Shx147065 	    tmp & (~CSR_HW_IF_CONFIG_REG_EEP_SEM));
40906169Shx147065 }
40916169Shx147065 
40926169Shx147065 /*
40936169Shx147065  * This function load all infomation in eeprom into iwk_eep
40946169Shx147065  * structure in iwk_sc_t structure
40956169Shx147065  */
iwk_eep_load(iwk_sc_t * sc)40966169Shx147065 static int iwk_eep_load(iwk_sc_t *sc)
40976169Shx147065 {
40986169Shx147065 	int i, rr;
40996169Shx147065 	uint32_t rv, tmp, eep_gp;
41006169Shx147065 	uint16_t addr, eep_sz = sizeof (sc->sc_eep_map);
41016169Shx147065 	uint16_t *eep_p = (uint16_t *)&sc->sc_eep_map;
41026169Shx147065 
41036169Shx147065 	/* read eeprom gp register in CSR */
41046169Shx147065 	eep_gp = IWK_READ(sc, CSR_EEPROM_GP);
41056169Shx147065 	if ((eep_gp & CSR_EEPROM_GP_VALID_MSK) ==
41066169Shx147065 	    CSR_EEPROM_GP_BAD_SIGNATURE) {
41077691SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "EEPROM not found\n");
41086169Shx147065 		return (IWK_FAIL);
41096169Shx147065 	}
41106169Shx147065 
41116169Shx147065 	rr = iwk_eep_sem_down(sc);
41126169Shx147065 	if (rr != 0) {
41137691SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "failed to own EEPROM\n");
41146169Shx147065 		return (IWK_FAIL);
41156169Shx147065 	}
41166169Shx147065 
41176169Shx147065 	for (addr = 0; addr < eep_sz; addr += 2) {
41186169Shx147065 		IWK_WRITE(sc, CSR_EEPROM_REG, addr<<1);
41196169Shx147065 		tmp = IWK_READ(sc, CSR_EEPROM_REG);
41206169Shx147065 		IWK_WRITE(sc, CSR_EEPROM_REG, tmp & ~(0x2));
41216169Shx147065 
41226169Shx147065 		for (i = 0; i < 10; i++) {
41236169Shx147065 			rv = IWK_READ(sc, CSR_EEPROM_REG);
41246169Shx147065 			if (rv & 1)
41256169Shx147065 				break;
41266169Shx147065 			DELAY(10);
41276169Shx147065 		}
41286169Shx147065 
41296169Shx147065 		if (!(rv & 1)) {
41307691SPengcheng.Chen@Sun.COM 			cmn_err(CE_WARN, "time out when read EEPROM\n");
41316169Shx147065 			iwk_eep_sem_up(sc);
41326169Shx147065 			return (IWK_FAIL);
41336169Shx147065 		}
41346169Shx147065 
41359327SFei.Feng@Sun.COM 		eep_p[addr/2] = LE_16(rv >> 16);
41366169Shx147065 	}
41376169Shx147065 
41386169Shx147065 	iwk_eep_sem_up(sc);
41396169Shx147065 	return (IWK_SUCCESS);
41406169Shx147065 }
41416169Shx147065 
41426169Shx147065 /*
41436169Shx147065  * init mac address in ieee80211com_t struct
41446169Shx147065  */
iwk_get_mac_from_eep(iwk_sc_t * sc)41456169Shx147065 static void iwk_get_mac_from_eep(iwk_sc_t *sc)
41466169Shx147065 {
41476169Shx147065 	ieee80211com_t *ic = &sc->sc_ic;
41486169Shx147065 	struct iwk_eep *ep = &sc->sc_eep_map;
41496169Shx147065 
41506169Shx147065 	IEEE80211_ADDR_COPY(ic->ic_macaddr, ep->mac_address);
41516169Shx147065 
41526169Shx147065 	IWK_DBG((IWK_DEBUG_EEPROM, "mac:%2x:%2x:%2x:%2x:%2x:%2x\n",
41536169Shx147065 	    ic->ic_macaddr[0], ic->ic_macaddr[1], ic->ic_macaddr[2],
41546169Shx147065 	    ic->ic_macaddr[3], ic->ic_macaddr[4], ic->ic_macaddr[5]));
41556169Shx147065 }
41566169Shx147065 
41576169Shx147065 static int
iwk_init(iwk_sc_t * sc)41586169Shx147065 iwk_init(iwk_sc_t *sc)
41596169Shx147065 {
41606169Shx147065 	int qid, n, err;
41616169Shx147065 	clock_t clk;
41626249Shx147065 	uint32_t tmp;
41636169Shx147065 
41646169Shx147065 	mutex_enter(&sc->sc_glock);
41656169Shx147065 	sc->sc_flags &= ~IWK_F_FW_INIT;
41666169Shx147065 
41676169Shx147065 	(void) iwk_preinit(sc);
41686169Shx147065 
41696249Shx147065 	tmp = IWK_READ(sc, CSR_GP_CNTRL);
41706249Shx147065 	if (!(tmp & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)) {
41719327SFei.Feng@Sun.COM 		cmn_err(CE_NOTE, "iwk_init(): Radio transmitter is off\n");
41726249Shx147065 		goto fail1;
41736249Shx147065 	}
41746249Shx147065 
41756169Shx147065 	/* init Rx ring */
41766169Shx147065 	iwk_mac_access_enter(sc);
41776169Shx147065 	IWK_WRITE(sc, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
41786169Shx147065 
41796169Shx147065 	IWK_WRITE(sc, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
41806169Shx147065 	IWK_WRITE(sc, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
41816169Shx147065 	    sc->sc_rxq.dma_desc.cookie.dmac_address >> 8);
41826169Shx147065 
41836169Shx147065 	IWK_WRITE(sc, FH_RSCSR_CHNL0_STTS_WPTR_REG,
41846169Shx147065 	    ((uint32_t)(sc->sc_dma_sh.cookie.dmac_address +
41856169Shx147065 	    offsetof(struct iwk_shared, val0)) >> 4));
41866169Shx147065 
41876169Shx147065 	IWK_WRITE(sc, FH_MEM_RCSR_CHNL0_CONFIG_REG,
41886169Shx147065 	    FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
41896169Shx147065 	    FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
41906169Shx147065 	    IWK_FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K |
41916169Shx147065 	    (RX_QUEUE_SIZE_LOG <<
41926169Shx147065 	    FH_RCSR_RX_CONFIG_RBDCB_SIZE_BITSHIFT));
41936169Shx147065 	iwk_mac_access_exit(sc);
41946169Shx147065 	IWK_WRITE(sc, FH_RSCSR_CHNL0_RBDCB_WPTR_REG,
41956169Shx147065 	    (RX_QUEUE_SIZE - 1) & ~0x7);
41966169Shx147065 
41976169Shx147065 	/* init Tx rings */
41986169Shx147065 	iwk_mac_access_enter(sc);
41996169Shx147065 	iwk_reg_write(sc, SCD_TXFACT, 0);
42006169Shx147065 
42016795Shx147065 	/* keep warm page */
42026169Shx147065 	iwk_reg_write(sc, IWK_FH_KW_MEM_ADDR_REG,
42036169Shx147065 	    sc->sc_dma_kw.cookie.dmac_address >> 4);
42046169Shx147065 
42056169Shx147065 	for (qid = 0; qid < IWK_NUM_QUEUES; qid++) {
42066169Shx147065 		IWK_WRITE(sc, FH_MEM_CBBC_QUEUE(qid),
42076169Shx147065 		    sc->sc_txq[qid].dma_desc.cookie.dmac_address >> 8);
42086169Shx147065 		IWK_WRITE(sc, IWK_FH_TCSR_CHNL_TX_CONFIG_REG(qid),
42096169Shx147065 		    IWK_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
42106169Shx147065 		    IWK_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL);
42116169Shx147065 	}
42126169Shx147065 	iwk_mac_access_exit(sc);
42136169Shx147065 
42146169Shx147065 	/* clear "radio off" and "disable command" bits */
42156169Shx147065 	IWK_WRITE(sc, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
42166169Shx147065 	IWK_WRITE(sc, CSR_UCODE_DRV_GP1_CLR,
42176169Shx147065 	    CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
42186169Shx147065 
42196169Shx147065 	/* clear any pending interrupts */
42206169Shx147065 	IWK_WRITE(sc, CSR_INT, 0xffffffff);
42216169Shx147065 
42226169Shx147065 	/* enable interrupts */
42236169Shx147065 	IWK_WRITE(sc, CSR_INT_MASK, CSR_INI_SET_MASK);
42246169Shx147065 
42256169Shx147065 	IWK_WRITE(sc, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
42266169Shx147065 	IWK_WRITE(sc, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
42276169Shx147065 
42286169Shx147065 	/*
42296169Shx147065 	 * backup ucode data part for future use.
42306169Shx147065 	 */
42316169Shx147065 	(void) memcpy(sc->sc_dma_fw_data_bak.mem_va,
42326169Shx147065 	    sc->sc_dma_fw_data.mem_va,
42336169Shx147065 	    sc->sc_dma_fw_data.alength);
42346169Shx147065 
42356169Shx147065 	for (n = 0; n < 2; n++) {
42366169Shx147065 		/* load firmware init segment into NIC */
42376169Shx147065 		err = iwk_load_firmware(sc);
42386169Shx147065 		if (err != IWK_SUCCESS) {
42396169Shx147065 			cmn_err(CE_WARN, "iwk_init(): "
42406169Shx147065 			    "failed to setup boot firmware\n");
42416169Shx147065 			continue;
42426169Shx147065 		}
42436169Shx147065 
42446169Shx147065 		/* now press "execute" start running */
42456169Shx147065 		IWK_WRITE(sc, CSR_RESET, 0);
42466169Shx147065 		break;
42476169Shx147065 	}
42486169Shx147065 	if (n == 2) {
42497569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_init(): failed to load firmware\n");
42506169Shx147065 		goto fail1;
42516169Shx147065 	}
42526169Shx147065 	/* ..and wait at most one second for adapter to initialize */
42536169Shx147065 	clk = ddi_get_lbolt() + drv_usectohz(2000000);
42546169Shx147065 	while (!(sc->sc_flags & IWK_F_FW_INIT)) {
42556169Shx147065 		if (cv_timedwait(&sc->sc_fw_cv, &sc->sc_glock, clk) < 0)
42566169Shx147065 			break;
42576169Shx147065 	}
42586169Shx147065 	if (!(sc->sc_flags & IWK_F_FW_INIT)) {
42596169Shx147065 		cmn_err(CE_WARN,
42606169Shx147065 		    "iwk_init(): timeout waiting for firmware init\n");
42616169Shx147065 		goto fail1;
42626169Shx147065 	}
42636169Shx147065 
42646169Shx147065 	/*
42656169Shx147065 	 * at this point, the firmware is loaded OK, then config the hardware
42666169Shx147065 	 * with the ucode API, including rxon, txpower, etc.
42676169Shx147065 	 */
42686169Shx147065 	err = iwk_config(sc);
42696169Shx147065 	if (err) {
42706169Shx147065 		cmn_err(CE_WARN, "iwk_init(): failed to configure device\n");
42716169Shx147065 		goto fail1;
42726169Shx147065 	}
42736169Shx147065 
42746169Shx147065 	/* at this point, hardware may receive beacons :) */
42756169Shx147065 	mutex_exit(&sc->sc_glock);
42766169Shx147065 	return (IWK_SUCCESS);
42776169Shx147065 
42786169Shx147065 fail1:
42796169Shx147065 	err = IWK_FAIL;
42806169Shx147065 	mutex_exit(&sc->sc_glock);
42816169Shx147065 	return (err);
42826169Shx147065 }
42836169Shx147065 
42846169Shx147065 static void
iwk_stop(iwk_sc_t * sc)42856169Shx147065 iwk_stop(iwk_sc_t *sc)
42866169Shx147065 {
42876169Shx147065 	uint32_t tmp;
42886169Shx147065 	int i;
42896169Shx147065 
42907891SPengcheng.Chen@Sun.COM 	if (!(sc->sc_flags & IWK_F_QUIESCED))
42917891SPengcheng.Chen@Sun.COM 		mutex_enter(&sc->sc_glock);
42926169Shx147065 
42936169Shx147065 	IWK_WRITE(sc, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
42946169Shx147065 	/* disable interrupts */
42956169Shx147065 	IWK_WRITE(sc, CSR_INT_MASK, 0);
42966169Shx147065 	IWK_WRITE(sc, CSR_INT, CSR_INI_SET_MASK);
42976169Shx147065 	IWK_WRITE(sc, CSR_FH_INT_STATUS, 0xffffffff);
42986169Shx147065 
42996169Shx147065 	/* reset all Tx rings */
43006169Shx147065 	for (i = 0; i < IWK_NUM_QUEUES; i++)
43016169Shx147065 		iwk_reset_tx_ring(sc, &sc->sc_txq[i]);
43026169Shx147065 
43036169Shx147065 	/* reset Rx ring */
43046169Shx147065 	iwk_reset_rx_ring(sc);
43056169Shx147065 
43066169Shx147065 	iwk_mac_access_enter(sc);
43076169Shx147065 	iwk_reg_write(sc, ALM_APMG_CLK_DIS, APMG_CLK_REG_VAL_DMA_CLK_RQT);
43086169Shx147065 	iwk_mac_access_exit(sc);
43096169Shx147065 
43106169Shx147065 	DELAY(5);
43116169Shx147065 
43126169Shx147065 	iwk_stop_master(sc);
43136169Shx147065 
43146169Shx147065 	sc->sc_tx_timer = 0;
43158727SPengcheng.Chen@Sun.COM 	sc->sc_flags &= ~IWK_F_SCANNING;
43168727SPengcheng.Chen@Sun.COM 	sc->sc_scan_pending = 0;
43178727SPengcheng.Chen@Sun.COM 
43186169Shx147065 	tmp = IWK_READ(sc, CSR_RESET);
43196169Shx147065 	IWK_WRITE(sc, CSR_RESET, tmp | CSR_RESET_REG_FLAG_SW_RESET);
43207891SPengcheng.Chen@Sun.COM 
43217891SPengcheng.Chen@Sun.COM 	if (!(sc->sc_flags & IWK_F_QUIESCED))
43227891SPengcheng.Chen@Sun.COM 		mutex_exit(&sc->sc_glock);
43236169Shx147065 }
43246249Shx147065 
43256249Shx147065 /*
43266249Shx147065  * Naive implementation of the Adaptive Multi Rate Retry algorithm:
43276249Shx147065  * "IEEE 802.11 Rate Adaptation: A Practical Approach"
43286249Shx147065  * Mathieu Lacage, Hossein Manshaei, Thierry Turletti
43296249Shx147065  * INRIA Sophia - Projet Planete
43306249Shx147065  * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
43316249Shx147065  */
43326249Shx147065 #define	is_success(amrr)	\
43336249Shx147065 	((amrr)->retrycnt < (amrr)->txcnt / 10)
43346249Shx147065 #define	is_failure(amrr)	\
43356249Shx147065 	((amrr)->retrycnt > (amrr)->txcnt / 3)
43366249Shx147065 #define	is_enough(amrr)		\
43376249Shx147065 	((amrr)->txcnt > 100)
43386249Shx147065 #define	is_min_rate(in)		\
43396249Shx147065 	((in)->in_txrate == 0)
43406249Shx147065 #define	is_max_rate(in)		\
43416249Shx147065 	((in)->in_txrate == (in)->in_rates.ir_nrates - 1)
43426249Shx147065 #define	increase_rate(in)	\
43436249Shx147065 	((in)->in_txrate++)
43446249Shx147065 #define	decrease_rate(in)	\
43456249Shx147065 	((in)->in_txrate--)
43466249Shx147065 #define	reset_cnt(amrr)		\
43476249Shx147065 	{ (amrr)->txcnt = (amrr)->retrycnt = 0; }
43486249Shx147065 
43496249Shx147065 #define	IWK_AMRR_MIN_SUCCESS_THRESHOLD	 1
43506249Shx147065 #define	IWK_AMRR_MAX_SUCCESS_THRESHOLD	15
43516249Shx147065 
43526249Shx147065 static void
iwk_amrr_init(iwk_amrr_t * amrr)43536249Shx147065 iwk_amrr_init(iwk_amrr_t *amrr)
43546249Shx147065 {
43556249Shx147065 	amrr->success = 0;
43566249Shx147065 	amrr->recovery = 0;
43576249Shx147065 	amrr->txcnt = amrr->retrycnt = 0;
43586249Shx147065 	amrr->success_threshold = IWK_AMRR_MIN_SUCCESS_THRESHOLD;
43596249Shx147065 }
43606249Shx147065 
43616249Shx147065 static void
iwk_amrr_timeout(iwk_sc_t * sc)43626249Shx147065 iwk_amrr_timeout(iwk_sc_t *sc)
43636249Shx147065 {
43646249Shx147065 	ieee80211com_t *ic = &sc->sc_ic;
43656249Shx147065 
43666249Shx147065 	IWK_DBG((IWK_DEBUG_RATECTL, "iwk_amrr_timeout() enter\n"));
43676249Shx147065 	if (ic->ic_opmode == IEEE80211_M_STA)
43686249Shx147065 		iwk_amrr_ratectl(NULL, ic->ic_bss);
43696249Shx147065 	else
43706249Shx147065 		ieee80211_iterate_nodes(&ic->ic_sta, iwk_amrr_ratectl, NULL);
43716249Shx147065 	sc->sc_clk = ddi_get_lbolt();
43726249Shx147065 }
43736249Shx147065 
43746249Shx147065 /* ARGSUSED */
43756249Shx147065 static void
iwk_amrr_ratectl(void * arg,ieee80211_node_t * in)43766249Shx147065 iwk_amrr_ratectl(void *arg, ieee80211_node_t *in)
43776249Shx147065 {
43786249Shx147065 	iwk_amrr_t *amrr = (iwk_amrr_t *)in;
43796249Shx147065 	int need_change = 0;
43806249Shx147065 
43816249Shx147065 	if (is_success(amrr) && is_enough(amrr)) {
43826249Shx147065 		amrr->success++;
43836249Shx147065 		if (amrr->success >= amrr->success_threshold &&
43846249Shx147065 		    !is_max_rate(in)) {
43856249Shx147065 			amrr->recovery = 1;
43866249Shx147065 			amrr->success = 0;
43876249Shx147065 			increase_rate(in);
43886249Shx147065 			IWK_DBG((IWK_DEBUG_RATECTL,
43896249Shx147065 			    "AMRR increasing rate %d (txcnt=%d retrycnt=%d)\n",
43906249Shx147065 			    in->in_txrate, amrr->txcnt, amrr->retrycnt));
43916249Shx147065 			need_change = 1;
43926249Shx147065 		} else {
43936249Shx147065 			amrr->recovery = 0;
43946249Shx147065 		}
43956249Shx147065 	} else if (is_failure(amrr)) {
43966249Shx147065 		amrr->success = 0;
43976249Shx147065 		if (!is_min_rate(in)) {
43986249Shx147065 			if (amrr->recovery) {
43996249Shx147065 				amrr->success_threshold++;
44006249Shx147065 				if (amrr->success_threshold >
44016249Shx147065 				    IWK_AMRR_MAX_SUCCESS_THRESHOLD)
44026249Shx147065 					amrr->success_threshold =
44036249Shx147065 					    IWK_AMRR_MAX_SUCCESS_THRESHOLD;
44046249Shx147065 			} else {
44056249Shx147065 				amrr->success_threshold =
44066249Shx147065 				    IWK_AMRR_MIN_SUCCESS_THRESHOLD;
44076249Shx147065 			}
44086249Shx147065 			decrease_rate(in);
44096249Shx147065 			IWK_DBG((IWK_DEBUG_RATECTL,
44106249Shx147065 			    "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)\n",
44116249Shx147065 			    in->in_txrate, amrr->txcnt, amrr->retrycnt));
44126249Shx147065 			need_change = 1;
44136249Shx147065 		}
44146249Shx147065 		amrr->recovery = 0;	/* paper is incorrect */
44156249Shx147065 	}
44166249Shx147065 
44176249Shx147065 	if (is_enough(amrr) || need_change)
44186249Shx147065 		reset_cnt(amrr);
44196249Shx147065 }
44207569SXinghua.Wen@Sun.COM 
44217569SXinghua.Wen@Sun.COM /*
44227569SXinghua.Wen@Sun.COM  * calculate 4965 chipset's kelvin temperature according to
44237569SXinghua.Wen@Sun.COM  * the data of init alive and satistics notification.
44247569SXinghua.Wen@Sun.COM  * The details is described in iwk_calibration.h file
44257569SXinghua.Wen@Sun.COM  */
iwk_curr_tempera(iwk_sc_t * sc)44267569SXinghua.Wen@Sun.COM static int32_t iwk_curr_tempera(iwk_sc_t *sc)
44277569SXinghua.Wen@Sun.COM {
44287569SXinghua.Wen@Sun.COM 	int32_t  tempera;
44297569SXinghua.Wen@Sun.COM 	int32_t  r1, r2, r3;
44307569SXinghua.Wen@Sun.COM 	uint32_t  r4_u;
44317569SXinghua.Wen@Sun.COM 	int32_t   r4_s;
44327569SXinghua.Wen@Sun.COM 
44337569SXinghua.Wen@Sun.COM 	if (iwk_is_fat_channel(sc)) {
44349327SFei.Feng@Sun.COM 		r1 = (int32_t)LE_32(sc->sc_card_alive_init.therm_r1[1]);
44359327SFei.Feng@Sun.COM 		r2 = (int32_t)LE_32(sc->sc_card_alive_init.therm_r2[1]);
44369327SFei.Feng@Sun.COM 		r3 = (int32_t)LE_32(sc->sc_card_alive_init.therm_r3[1]);
44379327SFei.Feng@Sun.COM 		r4_u = LE_32(sc->sc_card_alive_init.therm_r4[1]);
44387569SXinghua.Wen@Sun.COM 	} else {
44399327SFei.Feng@Sun.COM 		r1 = (int32_t)LE_32(sc->sc_card_alive_init.therm_r1[0]);
44409327SFei.Feng@Sun.COM 		r2 = (int32_t)LE_32(sc->sc_card_alive_init.therm_r2[0]);
44419327SFei.Feng@Sun.COM 		r3 = (int32_t)LE_32(sc->sc_card_alive_init.therm_r3[0]);
44429327SFei.Feng@Sun.COM 		r4_u = LE_32(sc->sc_card_alive_init.therm_r4[0]);
44437569SXinghua.Wen@Sun.COM 	}
44447569SXinghua.Wen@Sun.COM 
44457569SXinghua.Wen@Sun.COM 	if (sc->sc_flags & IWK_F_STATISTICS) {
44469327SFei.Feng@Sun.COM 		r4_s = (int32_t)(LE_32(sc->sc_statistics.general.temperature) <<
44477569SXinghua.Wen@Sun.COM 		    (31-23)) >> (31-23);
44487569SXinghua.Wen@Sun.COM 	} else {
44497569SXinghua.Wen@Sun.COM 		r4_s = (int32_t)(r4_u << (31-23)) >> (31-23);
44507569SXinghua.Wen@Sun.COM 	}
44517569SXinghua.Wen@Sun.COM 
44527569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_CALIBRATION, "temperature R[1-4]: %d %d %d %d\n",
44537569SXinghua.Wen@Sun.COM 	    r1, r2, r3, r4_s));
44547569SXinghua.Wen@Sun.COM 
44557569SXinghua.Wen@Sun.COM 	if (r3 == r1) {
44567569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_curr_tempera(): "
44577569SXinghua.Wen@Sun.COM 		    "failed to calculate temperature"
44587569SXinghua.Wen@Sun.COM 		    "because r3 = r1\n");
44597569SXinghua.Wen@Sun.COM 		return (DDI_FAILURE);
44607569SXinghua.Wen@Sun.COM 	}
44617569SXinghua.Wen@Sun.COM 
44627569SXinghua.Wen@Sun.COM 	tempera = TEMPERATURE_CALIB_A_VAL * (r4_s - r2);
44637569SXinghua.Wen@Sun.COM 	tempera /= (r3 - r1);
44647569SXinghua.Wen@Sun.COM 	tempera = (tempera*97) / 100 + TEMPERATURE_CALIB_KELVIN_OFFSET;
44657569SXinghua.Wen@Sun.COM 
44667569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_CALIBRATION, "calculated temperature: %dK, %dC\n",
44677569SXinghua.Wen@Sun.COM 	    tempera, KELVIN_TO_CELSIUS(tempera)));
44687569SXinghua.Wen@Sun.COM 
44697569SXinghua.Wen@Sun.COM 	return (tempera);
44707569SXinghua.Wen@Sun.COM }
44717569SXinghua.Wen@Sun.COM 
44727569SXinghua.Wen@Sun.COM /* Determine whether 4965 is using 2.4 GHz band */
iwk_is_24G_band(iwk_sc_t * sc)44737569SXinghua.Wen@Sun.COM static inline int iwk_is_24G_band(iwk_sc_t *sc)
44747569SXinghua.Wen@Sun.COM {
44759327SFei.Feng@Sun.COM 	return (LE_32(sc->sc_config.flags) & RXON_FLG_BAND_24G_MSK);
44767569SXinghua.Wen@Sun.COM }
44777569SXinghua.Wen@Sun.COM 
44787569SXinghua.Wen@Sun.COM /* Determine whether 4965 is using fat channel */
iwk_is_fat_channel(iwk_sc_t * sc)44797569SXinghua.Wen@Sun.COM static inline int iwk_is_fat_channel(iwk_sc_t *sc)
44807569SXinghua.Wen@Sun.COM {
44819327SFei.Feng@Sun.COM 	return ((LE_32(sc->sc_config.flags) &
44829327SFei.Feng@Sun.COM 	    RXON_FLG_CHANNEL_MODE_PURE_40_MSK) ||
44839327SFei.Feng@Sun.COM 	    (LE_32(sc->sc_config.flags) & RXON_FLG_CHANNEL_MODE_MIXED_MSK));
44847569SXinghua.Wen@Sun.COM }
44857569SXinghua.Wen@Sun.COM 
44867569SXinghua.Wen@Sun.COM /*
44877569SXinghua.Wen@Sun.COM  * In MIMO mode, determine which group 4965's current channel belong to.
44887569SXinghua.Wen@Sun.COM  * For more infomation about "channel group",
44897569SXinghua.Wen@Sun.COM  * please refer to iwk_calibration.h file
44907569SXinghua.Wen@Sun.COM  */
iwk_txpower_grp(uint16_t channel)44917569SXinghua.Wen@Sun.COM static int iwk_txpower_grp(uint16_t channel)
44927569SXinghua.Wen@Sun.COM {
44937569SXinghua.Wen@Sun.COM 	if (channel >= CALIB_IWK_TX_ATTEN_GR5_FCH &&
44947569SXinghua.Wen@Sun.COM 	    channel <= CALIB_IWK_TX_ATTEN_GR5_LCH) {
44957569SXinghua.Wen@Sun.COM 		return (CALIB_CH_GROUP_5);
44967569SXinghua.Wen@Sun.COM 	}
44977569SXinghua.Wen@Sun.COM 
44987569SXinghua.Wen@Sun.COM 	if (channel >= CALIB_IWK_TX_ATTEN_GR1_FCH &&
44997569SXinghua.Wen@Sun.COM 	    channel <= CALIB_IWK_TX_ATTEN_GR1_LCH) {
45007569SXinghua.Wen@Sun.COM 		return (CALIB_CH_GROUP_1);
45017569SXinghua.Wen@Sun.COM 	}
45027569SXinghua.Wen@Sun.COM 
45037569SXinghua.Wen@Sun.COM 	if (channel >= CALIB_IWK_TX_ATTEN_GR2_FCH &&
45047569SXinghua.Wen@Sun.COM 	    channel <= CALIB_IWK_TX_ATTEN_GR2_LCH) {
45057569SXinghua.Wen@Sun.COM 		return (CALIB_CH_GROUP_2);
45067569SXinghua.Wen@Sun.COM 	}
45077569SXinghua.Wen@Sun.COM 
45087569SXinghua.Wen@Sun.COM 	if (channel >= CALIB_IWK_TX_ATTEN_GR3_FCH &&
45097569SXinghua.Wen@Sun.COM 	    channel <= CALIB_IWK_TX_ATTEN_GR3_LCH) {
45107569SXinghua.Wen@Sun.COM 		return (CALIB_CH_GROUP_3);
45117569SXinghua.Wen@Sun.COM 	}
45127569SXinghua.Wen@Sun.COM 
45137569SXinghua.Wen@Sun.COM 	if (channel >= CALIB_IWK_TX_ATTEN_GR4_FCH &&
45147569SXinghua.Wen@Sun.COM 	    channel <= CALIB_IWK_TX_ATTEN_GR4_LCH) {
45157569SXinghua.Wen@Sun.COM 		return (CALIB_CH_GROUP_4);
45167569SXinghua.Wen@Sun.COM 	}
45177569SXinghua.Wen@Sun.COM 
45187569SXinghua.Wen@Sun.COM 	cmn_err(CE_WARN, "iwk_txpower_grp(): "
45197569SXinghua.Wen@Sun.COM 	    "can't find txpower group for channel %d.\n", channel);
45207569SXinghua.Wen@Sun.COM 
45217569SXinghua.Wen@Sun.COM 	return (DDI_FAILURE);
45227569SXinghua.Wen@Sun.COM }
45237569SXinghua.Wen@Sun.COM 
45247569SXinghua.Wen@Sun.COM /* 2.4 GHz */
45257569SXinghua.Wen@Sun.COM static uint16_t iwk_eep_band_1[14] = {
45267569SXinghua.Wen@Sun.COM 	1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
45277569SXinghua.Wen@Sun.COM };
45287569SXinghua.Wen@Sun.COM 
45297569SXinghua.Wen@Sun.COM /* 5.2 GHz bands */
45307569SXinghua.Wen@Sun.COM static uint16_t iwk_eep_band_2[13] = {
45317569SXinghua.Wen@Sun.COM 	183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
45327569SXinghua.Wen@Sun.COM };
45337569SXinghua.Wen@Sun.COM 
45347569SXinghua.Wen@Sun.COM static uint16_t iwk_eep_band_3[12] = {
45357569SXinghua.Wen@Sun.COM 	34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
45367569SXinghua.Wen@Sun.COM };
45377569SXinghua.Wen@Sun.COM 
45387569SXinghua.Wen@Sun.COM static uint16_t iwk_eep_band_4[11] = {
45397569SXinghua.Wen@Sun.COM 	100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
45407569SXinghua.Wen@Sun.COM };
45417569SXinghua.Wen@Sun.COM 
45427569SXinghua.Wen@Sun.COM static uint16_t iwk_eep_band_5[6] = {
45437569SXinghua.Wen@Sun.COM 	145, 149, 153, 157, 161, 165
45447569SXinghua.Wen@Sun.COM };
45457569SXinghua.Wen@Sun.COM 
45467569SXinghua.Wen@Sun.COM static uint16_t iwk_eep_band_6[7] = {
45477569SXinghua.Wen@Sun.COM 	1, 2, 3, 4, 5, 6, 7
45487569SXinghua.Wen@Sun.COM };
45497569SXinghua.Wen@Sun.COM 
45507569SXinghua.Wen@Sun.COM static uint16_t iwk_eep_band_7[11] = {
45517569SXinghua.Wen@Sun.COM 	36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
45527569SXinghua.Wen@Sun.COM };
45537569SXinghua.Wen@Sun.COM 
45547569SXinghua.Wen@Sun.COM /* Get regulatory data from eeprom for a given channel */
iwk_get_eep_channel(iwk_sc_t * sc,uint16_t channel,int is_24G,int is_fat,int is_hi_chan)45557569SXinghua.Wen@Sun.COM static struct iwk_eep_channel *iwk_get_eep_channel(iwk_sc_t *sc,
45567569SXinghua.Wen@Sun.COM     uint16_t channel,
45577569SXinghua.Wen@Sun.COM     int is_24G, int is_fat, int is_hi_chan)
45587569SXinghua.Wen@Sun.COM {
45597569SXinghua.Wen@Sun.COM 	int32_t i;
45607569SXinghua.Wen@Sun.COM 	uint16_t chan;
45617569SXinghua.Wen@Sun.COM 
45627569SXinghua.Wen@Sun.COM 	if (is_fat) {  /* 11n mode */
45637569SXinghua.Wen@Sun.COM 
45647569SXinghua.Wen@Sun.COM 		if (is_hi_chan) {
45657569SXinghua.Wen@Sun.COM 			chan = channel - 4;
45667569SXinghua.Wen@Sun.COM 		} else {
45677569SXinghua.Wen@Sun.COM 			chan = channel;
45687569SXinghua.Wen@Sun.COM 		}
45697569SXinghua.Wen@Sun.COM 
45707569SXinghua.Wen@Sun.COM 		for (i = 0; i < 7; i++) {
45717569SXinghua.Wen@Sun.COM 			if (iwk_eep_band_6[i] == chan) {
45727569SXinghua.Wen@Sun.COM 				return (&sc->sc_eep_map.band_24_channels[i]);
45737569SXinghua.Wen@Sun.COM 			}
45747569SXinghua.Wen@Sun.COM 		}
45757569SXinghua.Wen@Sun.COM 		for (i = 0; i < 11; i++) {
45767569SXinghua.Wen@Sun.COM 			if (iwk_eep_band_7[i] == chan) {
45777569SXinghua.Wen@Sun.COM 				return (&sc->sc_eep_map.band_52_channels[i]);
45787569SXinghua.Wen@Sun.COM 			}
45797569SXinghua.Wen@Sun.COM 		}
45807569SXinghua.Wen@Sun.COM 	} else if (is_24G) {  /* 2.4 GHz band */
45817569SXinghua.Wen@Sun.COM 		for (i = 0; i < 14; i++) {
45827569SXinghua.Wen@Sun.COM 			if (iwk_eep_band_1[i] == channel) {
45837569SXinghua.Wen@Sun.COM 				return (&sc->sc_eep_map.band_1_channels[i]);
45847569SXinghua.Wen@Sun.COM 			}
45857569SXinghua.Wen@Sun.COM 		}
45867569SXinghua.Wen@Sun.COM 	} else {  /* 5 GHz band */
45877569SXinghua.Wen@Sun.COM 		for (i = 0; i < 13; i++) {
45887569SXinghua.Wen@Sun.COM 			if (iwk_eep_band_2[i] == channel) {
45897569SXinghua.Wen@Sun.COM 				return (&sc->sc_eep_map.band_2_channels[i]);
45907569SXinghua.Wen@Sun.COM 			}
45917569SXinghua.Wen@Sun.COM 		}
45927569SXinghua.Wen@Sun.COM 		for (i = 0; i < 12; i++) {
45937569SXinghua.Wen@Sun.COM 			if (iwk_eep_band_3[i] == channel) {
45947569SXinghua.Wen@Sun.COM 				return (&sc->sc_eep_map.band_3_channels[i]);
45957569SXinghua.Wen@Sun.COM 			}
45967569SXinghua.Wen@Sun.COM 		}
45977569SXinghua.Wen@Sun.COM 		for (i = 0; i < 11; i++) {
45987569SXinghua.Wen@Sun.COM 			if (iwk_eep_band_4[i] == channel) {
45997569SXinghua.Wen@Sun.COM 				return (&sc->sc_eep_map.band_4_channels[i]);
46007569SXinghua.Wen@Sun.COM 			}
46017569SXinghua.Wen@Sun.COM 		}
46027569SXinghua.Wen@Sun.COM 		for (i = 0; i < 6; i++) {
46037569SXinghua.Wen@Sun.COM 			if (iwk_eep_band_5[i] == channel) {
46047569SXinghua.Wen@Sun.COM 				return (&sc->sc_eep_map.band_5_channels[i]);
46057569SXinghua.Wen@Sun.COM 			}
46067569SXinghua.Wen@Sun.COM 		}
46077569SXinghua.Wen@Sun.COM 	}
46087569SXinghua.Wen@Sun.COM 
46097569SXinghua.Wen@Sun.COM 	return (NULL);
46107569SXinghua.Wen@Sun.COM }
46117569SXinghua.Wen@Sun.COM 
46127569SXinghua.Wen@Sun.COM /*
46137569SXinghua.Wen@Sun.COM  * Determine which subband a given channel belongs
46147569SXinghua.Wen@Sun.COM  * to in 2.4 GHz or 5 GHz band
46157569SXinghua.Wen@Sun.COM  */
iwk_band_number(iwk_sc_t * sc,uint16_t channel)46167569SXinghua.Wen@Sun.COM static int32_t iwk_band_number(iwk_sc_t *sc, uint16_t channel)
46177569SXinghua.Wen@Sun.COM {
46187569SXinghua.Wen@Sun.COM 	int32_t b_n = -1;
46197569SXinghua.Wen@Sun.COM 
46207569SXinghua.Wen@Sun.COM 	for (b_n = 0; b_n < EEP_TX_POWER_BANDS; b_n++) {
46217569SXinghua.Wen@Sun.COM 		if (0 == sc->sc_eep_map.calib_info.band_info_tbl[b_n].ch_from) {
46227569SXinghua.Wen@Sun.COM 			continue;
46237569SXinghua.Wen@Sun.COM 		}
46247569SXinghua.Wen@Sun.COM 
46257569SXinghua.Wen@Sun.COM 		if ((channel >=
46267569SXinghua.Wen@Sun.COM 		    (uint16_t)sc->sc_eep_map.calib_info.
46277569SXinghua.Wen@Sun.COM 		    band_info_tbl[b_n].ch_from) &&
46287569SXinghua.Wen@Sun.COM 		    (channel <=
46297569SXinghua.Wen@Sun.COM 		    (uint16_t)sc->sc_eep_map.calib_info.
46307569SXinghua.Wen@Sun.COM 		    band_info_tbl[b_n].ch_to)) {
46317569SXinghua.Wen@Sun.COM 			break;
46327569SXinghua.Wen@Sun.COM 		}
46337569SXinghua.Wen@Sun.COM 	}
46347569SXinghua.Wen@Sun.COM 
46357569SXinghua.Wen@Sun.COM 	return (b_n);
46367569SXinghua.Wen@Sun.COM }
46377569SXinghua.Wen@Sun.COM 
46387569SXinghua.Wen@Sun.COM /* Make a special division for interpolation operation */
iwk_division(int32_t num,int32_t denom,int32_t * res)46397569SXinghua.Wen@Sun.COM static int iwk_division(int32_t num, int32_t denom, int32_t *res)
46407569SXinghua.Wen@Sun.COM {
46417569SXinghua.Wen@Sun.COM 	int32_t sign = 1;
46427569SXinghua.Wen@Sun.COM 
46437569SXinghua.Wen@Sun.COM 	if (num < 0) {
46447569SXinghua.Wen@Sun.COM 		sign = -sign;
46457569SXinghua.Wen@Sun.COM 		num = -num;
46467569SXinghua.Wen@Sun.COM 	}
46477569SXinghua.Wen@Sun.COM 
46487569SXinghua.Wen@Sun.COM 	if (denom < 0) {
46497569SXinghua.Wen@Sun.COM 		sign = -sign;
46507569SXinghua.Wen@Sun.COM 		denom = -denom;
46517569SXinghua.Wen@Sun.COM 	}
46527569SXinghua.Wen@Sun.COM 
46537569SXinghua.Wen@Sun.COM 	*res = ((num*2 + denom) / (denom*2)) * sign;
46547569SXinghua.Wen@Sun.COM 
46557569SXinghua.Wen@Sun.COM 	return (IWK_SUCCESS);
46567569SXinghua.Wen@Sun.COM }
46577569SXinghua.Wen@Sun.COM 
46587569SXinghua.Wen@Sun.COM /* Make interpolation operation */
iwk_interpolate_value(int32_t x,int32_t x1,int32_t y1,int32_t x2,int32_t y2)46597569SXinghua.Wen@Sun.COM static int32_t iwk_interpolate_value(int32_t x, int32_t x1, int32_t y1,
46607569SXinghua.Wen@Sun.COM     int32_t x2, int32_t y2)
46617569SXinghua.Wen@Sun.COM {
46627569SXinghua.Wen@Sun.COM 	int32_t val;
46637569SXinghua.Wen@Sun.COM 
46647569SXinghua.Wen@Sun.COM 	if (x2 == x1) {
46657569SXinghua.Wen@Sun.COM 		return (y1);
46667569SXinghua.Wen@Sun.COM 	} else {
46677569SXinghua.Wen@Sun.COM 		(void) iwk_division((x2-x)*(y1-y2), (x2-x1), &val);
46687569SXinghua.Wen@Sun.COM 		return (val + y2);
46697569SXinghua.Wen@Sun.COM 	}
46707569SXinghua.Wen@Sun.COM }
46717569SXinghua.Wen@Sun.COM 
46727569SXinghua.Wen@Sun.COM /* Get interpolation measurement data of a given channel for all chains. */
iwk_channel_interpolate(iwk_sc_t * sc,uint16_t channel,struct iwk_eep_calib_channel_info * chan_info)46737569SXinghua.Wen@Sun.COM static int iwk_channel_interpolate(iwk_sc_t *sc, uint16_t channel,
46747569SXinghua.Wen@Sun.COM     struct iwk_eep_calib_channel_info *chan_info)
46757569SXinghua.Wen@Sun.COM {
46767569SXinghua.Wen@Sun.COM 	int32_t ban_n;
46777569SXinghua.Wen@Sun.COM 	uint32_t ch1_n, ch2_n;
46787569SXinghua.Wen@Sun.COM 	int32_t c, m;
46797569SXinghua.Wen@Sun.COM 	struct iwk_eep_calib_measure *m1_p, *m2_p, *m_p;
46807569SXinghua.Wen@Sun.COM 
46817569SXinghua.Wen@Sun.COM 	/* determine subband number */
46827569SXinghua.Wen@Sun.COM 	ban_n = iwk_band_number(sc, channel);
46837569SXinghua.Wen@Sun.COM 	if (ban_n >= EEP_TX_POWER_BANDS) {
46847569SXinghua.Wen@Sun.COM 		return (DDI_FAILURE);
46857569SXinghua.Wen@Sun.COM 	}
46867569SXinghua.Wen@Sun.COM 
46877569SXinghua.Wen@Sun.COM 	ch1_n =
46887569SXinghua.Wen@Sun.COM 	    (uint32_t)sc->sc_eep_map.calib_info.band_info_tbl[ban_n].ch1.ch_num;
46897569SXinghua.Wen@Sun.COM 	ch2_n =
46907569SXinghua.Wen@Sun.COM 	    (uint32_t)sc->sc_eep_map.calib_info.band_info_tbl[ban_n].ch2.ch_num;
46917569SXinghua.Wen@Sun.COM 
46927569SXinghua.Wen@Sun.COM 	chan_info->ch_num = (uint8_t)channel;  /* given channel number */
46937569SXinghua.Wen@Sun.COM 
46947569SXinghua.Wen@Sun.COM 	/*
46957569SXinghua.Wen@Sun.COM 	 * go through all chains on chipset
46967569SXinghua.Wen@Sun.COM 	 */
46977569SXinghua.Wen@Sun.COM 	for (c = 0; c < EEP_TX_POWER_TX_CHAINS; c++) {
46987569SXinghua.Wen@Sun.COM 		/*
46997569SXinghua.Wen@Sun.COM 		 * go through all factory measurements
47007569SXinghua.Wen@Sun.COM 		 */
47017569SXinghua.Wen@Sun.COM 		for (m = 0; m < EEP_TX_POWER_MEASUREMENTS; m++) {
47027569SXinghua.Wen@Sun.COM 			m1_p =
47037569SXinghua.Wen@Sun.COM 			    &(sc->sc_eep_map.calib_info.
47047569SXinghua.Wen@Sun.COM 			    band_info_tbl[ban_n].ch1.measure[c][m]);
47057569SXinghua.Wen@Sun.COM 			m2_p =
47067569SXinghua.Wen@Sun.COM 			    &(sc->sc_eep_map.calib_info.band_info_tbl[ban_n].
47077569SXinghua.Wen@Sun.COM 			    ch2.measure[c][m]);
47087569SXinghua.Wen@Sun.COM 			m_p = &(chan_info->measure[c][m]);
47097569SXinghua.Wen@Sun.COM 
47107569SXinghua.Wen@Sun.COM 			/*
47117569SXinghua.Wen@Sun.COM 			 * make interpolation to get actual
47127569SXinghua.Wen@Sun.COM 			 * Tx power for given channel
47137569SXinghua.Wen@Sun.COM 			 */
47147569SXinghua.Wen@Sun.COM 			m_p->actual_pow = iwk_interpolate_value(channel,
47157569SXinghua.Wen@Sun.COM 			    ch1_n, m1_p->actual_pow,
47167569SXinghua.Wen@Sun.COM 			    ch2_n, m2_p->actual_pow);
47177569SXinghua.Wen@Sun.COM 
47187569SXinghua.Wen@Sun.COM 			/* make interpolation to get index into gain table */
47197569SXinghua.Wen@Sun.COM 			m_p->gain_idx = iwk_interpolate_value(channel,
47207569SXinghua.Wen@Sun.COM 			    ch1_n, m1_p->gain_idx,
47217569SXinghua.Wen@Sun.COM 			    ch2_n, m2_p->gain_idx);
47227569SXinghua.Wen@Sun.COM 
47237569SXinghua.Wen@Sun.COM 			/* make interpolation to get chipset temperature */
47247569SXinghua.Wen@Sun.COM 			m_p->temperature = iwk_interpolate_value(channel,
47257569SXinghua.Wen@Sun.COM 			    ch1_n, m1_p->temperature,
47267569SXinghua.Wen@Sun.COM 			    ch2_n, m2_p->temperature);
47277569SXinghua.Wen@Sun.COM 
47287569SXinghua.Wen@Sun.COM 			/*
47297569SXinghua.Wen@Sun.COM 			 * make interpolation to get power
47307569SXinghua.Wen@Sun.COM 			 * amp detector level
47317569SXinghua.Wen@Sun.COM 			 */
47327569SXinghua.Wen@Sun.COM 			m_p->pa_det = iwk_interpolate_value(channel, ch1_n,
47337569SXinghua.Wen@Sun.COM 			    m1_p->pa_det,
47347569SXinghua.Wen@Sun.COM 			    ch2_n, m2_p->pa_det);
47357569SXinghua.Wen@Sun.COM 		}
47367569SXinghua.Wen@Sun.COM 	}
47377569SXinghua.Wen@Sun.COM 
47387569SXinghua.Wen@Sun.COM 	return (IWK_SUCCESS);
47397569SXinghua.Wen@Sun.COM }
47407569SXinghua.Wen@Sun.COM 
47417569SXinghua.Wen@Sun.COM /*
47427569SXinghua.Wen@Sun.COM  * Calculate voltage compensation for Tx power. For more infomation,
47437569SXinghua.Wen@Sun.COM  * please refer to iwk_calibration.h file
47447569SXinghua.Wen@Sun.COM  */
iwk_voltage_compensation(int32_t eep_voltage,int32_t curr_voltage)47457569SXinghua.Wen@Sun.COM static int32_t iwk_voltage_compensation(int32_t eep_voltage,
47467569SXinghua.Wen@Sun.COM     int32_t curr_voltage)
47477569SXinghua.Wen@Sun.COM {
47487569SXinghua.Wen@Sun.COM 	int32_t vol_comp = 0;
47497569SXinghua.Wen@Sun.COM 
47507569SXinghua.Wen@Sun.COM 	if ((TX_POWER_IWK_ILLEGAL_VOLTAGE == eep_voltage) ||
47517569SXinghua.Wen@Sun.COM 	    (TX_POWER_IWK_ILLEGAL_VOLTAGE == curr_voltage)) {
47527569SXinghua.Wen@Sun.COM 		return (vol_comp);
47537569SXinghua.Wen@Sun.COM 	}
47547569SXinghua.Wen@Sun.COM 
47557569SXinghua.Wen@Sun.COM 	(void) iwk_division(curr_voltage-eep_voltage,
47567569SXinghua.Wen@Sun.COM 	    TX_POWER_IWK_VOLTAGE_CODES_PER_03V, &vol_comp);
47577569SXinghua.Wen@Sun.COM 
47587569SXinghua.Wen@Sun.COM 	if (curr_voltage > eep_voltage) {
47597569SXinghua.Wen@Sun.COM 		vol_comp *= 2;
47607569SXinghua.Wen@Sun.COM 	}
47617569SXinghua.Wen@Sun.COM 	if ((vol_comp < -2) || (vol_comp > 2)) {
47627569SXinghua.Wen@Sun.COM 		vol_comp = 0;
47637569SXinghua.Wen@Sun.COM 	}
47647569SXinghua.Wen@Sun.COM 
47657569SXinghua.Wen@Sun.COM 	return (vol_comp);
47667569SXinghua.Wen@Sun.COM }
47677569SXinghua.Wen@Sun.COM 
47687569SXinghua.Wen@Sun.COM /*
47697569SXinghua.Wen@Sun.COM  * Thermal compensation values for txpower for various frequency ranges ...
47707569SXinghua.Wen@Sun.COM  * ratios from 3:1 to 4.5:1 of degrees (Celsius) per half-dB gain adjust
47717569SXinghua.Wen@Sun.COM  */
47727569SXinghua.Wen@Sun.COM static struct iwk_txpower_tempera_comp {
47737569SXinghua.Wen@Sun.COM 	int32_t degrees_per_05db_a;
47747569SXinghua.Wen@Sun.COM 	int32_t degrees_per_05db_a_denom;
47757569SXinghua.Wen@Sun.COM } txpower_tempera_comp_table[CALIB_CH_GROUP_MAX] = {
47767569SXinghua.Wen@Sun.COM 	{9, 2},			/* group 0 5.2, ch  34-43 */
47777569SXinghua.Wen@Sun.COM 	{4, 1},			/* group 1 5.2, ch  44-70 */
47787569SXinghua.Wen@Sun.COM 	{4, 1},			/* group 2 5.2, ch  71-124 */
47797569SXinghua.Wen@Sun.COM 	{4, 1},			/* group 3 5.2, ch 125-200 */
47807569SXinghua.Wen@Sun.COM 	{3, 1}			/* group 4 2.4, ch   all */
47817569SXinghua.Wen@Sun.COM };
47827569SXinghua.Wen@Sun.COM 
47837569SXinghua.Wen@Sun.COM /*
47847569SXinghua.Wen@Sun.COM  * bit-rate-dependent table to prevent Tx distortion, in half-dB units,
47857569SXinghua.Wen@Sun.COM  * for OFDM 6, 12, 18, 24, 36, 48, 54, 60 MBit, and CCK all rates.
47867569SXinghua.Wen@Sun.COM  */
47877569SXinghua.Wen@Sun.COM static int32_t back_off_table[] = {
47887569SXinghua.Wen@Sun.COM 	10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 20 MHz */
47897569SXinghua.Wen@Sun.COM 	10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 20 MHz */
47907569SXinghua.Wen@Sun.COM 	10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 40 MHz */
47917569SXinghua.Wen@Sun.COM 	10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 40 MHz */
47927569SXinghua.Wen@Sun.COM 	10			/* CCK */
47937569SXinghua.Wen@Sun.COM };
47947569SXinghua.Wen@Sun.COM 
47957569SXinghua.Wen@Sun.COM /* determine minimum Tx power index in gain table */
iwk_min_power_index(int32_t rate_pow_idx,int32_t is_24G)47967569SXinghua.Wen@Sun.COM static int32_t iwk_min_power_index(int32_t rate_pow_idx, int32_t is_24G)
47977569SXinghua.Wen@Sun.COM {
47987569SXinghua.Wen@Sun.COM 	if ((!is_24G) && ((rate_pow_idx & 7) <= 4)) {
47997569SXinghua.Wen@Sun.COM 		return (MIN_TX_GAIN_INDEX_52GHZ_EXT);
48007569SXinghua.Wen@Sun.COM 	}
48017569SXinghua.Wen@Sun.COM 
48027569SXinghua.Wen@Sun.COM 	return (MIN_TX_GAIN_INDEX);
48037569SXinghua.Wen@Sun.COM }
48047569SXinghua.Wen@Sun.COM 
48057569SXinghua.Wen@Sun.COM /*
48067569SXinghua.Wen@Sun.COM  * Determine DSP and radio gain according to temperature and other factors.
48077569SXinghua.Wen@Sun.COM  * This function is the majority of Tx power calibration
48087569SXinghua.Wen@Sun.COM  */
iwk_txpower_table_cmd_init(iwk_sc_t * sc,struct iwk_tx_power_db * tp_db)48097569SXinghua.Wen@Sun.COM static int iwk_txpower_table_cmd_init(iwk_sc_t *sc,
48107569SXinghua.Wen@Sun.COM     struct iwk_tx_power_db *tp_db)
48117569SXinghua.Wen@Sun.COM {
48127569SXinghua.Wen@Sun.COM 	int is_24G, is_fat, is_high_chan, is_mimo;
48137569SXinghua.Wen@Sun.COM 	int c, r;
48147569SXinghua.Wen@Sun.COM 	int32_t target_power;
48157569SXinghua.Wen@Sun.COM 	int32_t tx_grp = CALIB_CH_GROUP_MAX;
48167569SXinghua.Wen@Sun.COM 	uint16_t channel;
48177569SXinghua.Wen@Sun.COM 	uint8_t saturation_power;
48187569SXinghua.Wen@Sun.COM 	int32_t regu_power;
48197569SXinghua.Wen@Sun.COM 	int32_t curr_regu_power;
48207569SXinghua.Wen@Sun.COM 	struct iwk_eep_channel *eep_chan_p;
48217569SXinghua.Wen@Sun.COM 	struct iwk_eep_calib_channel_info eep_chan_calib;
48227569SXinghua.Wen@Sun.COM 	int32_t eep_voltage, init_voltage;
48237569SXinghua.Wen@Sun.COM 	int32_t voltage_compensation;
48247569SXinghua.Wen@Sun.COM 	int32_t temperature;
48257569SXinghua.Wen@Sun.COM 	int32_t degrees_per_05db_num;
48267569SXinghua.Wen@Sun.COM 	int32_t degrees_per_05db_denom;
48277569SXinghua.Wen@Sun.COM 	struct iwk_eep_calib_measure *measure_p;
48287569SXinghua.Wen@Sun.COM 	int32_t interpo_temp;
48297569SXinghua.Wen@Sun.COM 	int32_t power_limit;
48307569SXinghua.Wen@Sun.COM 	int32_t atten_value;
48317569SXinghua.Wen@Sun.COM 	int32_t tempera_comp[2];
48327569SXinghua.Wen@Sun.COM 	int32_t interpo_gain_idx[2];
48337569SXinghua.Wen@Sun.COM 	int32_t interpo_actual_pow[2];
48347569SXinghua.Wen@Sun.COM 	union iwk_tx_power_dual_stream txpower_gains;
48357569SXinghua.Wen@Sun.COM 	int32_t txpower_gains_idx;
48367569SXinghua.Wen@Sun.COM 
48379327SFei.Feng@Sun.COM 	channel = LE_16(sc->sc_config.chan);
48387569SXinghua.Wen@Sun.COM 
48397569SXinghua.Wen@Sun.COM 	/* 2.4 GHz or 5 GHz band */
48407569SXinghua.Wen@Sun.COM 	is_24G = iwk_is_24G_band(sc);
48417569SXinghua.Wen@Sun.COM 
48427569SXinghua.Wen@Sun.COM 	/* fat channel or not */
48437569SXinghua.Wen@Sun.COM 	is_fat = iwk_is_fat_channel(sc);
48447569SXinghua.Wen@Sun.COM 
48457569SXinghua.Wen@Sun.COM 	/*
48467569SXinghua.Wen@Sun.COM 	 * using low half channel number or high half channel number
48477569SXinghua.Wen@Sun.COM 	 * identify fat channel
48487569SXinghua.Wen@Sun.COM 	 */
48499327SFei.Feng@Sun.COM 	if (is_fat && (LE_32(sc->sc_config.flags) &
48507569SXinghua.Wen@Sun.COM 	    RXON_FLG_CONTROL_CHANNEL_LOC_HIGH_MSK)) {
48517569SXinghua.Wen@Sun.COM 		is_high_chan = 1;
48527569SXinghua.Wen@Sun.COM 	}
48537569SXinghua.Wen@Sun.COM 
48547569SXinghua.Wen@Sun.COM 	if ((channel > 0) && (channel < 200)) {
48557569SXinghua.Wen@Sun.COM 		/* get regulatory channel data from eeprom */
48567569SXinghua.Wen@Sun.COM 		eep_chan_p = iwk_get_eep_channel(sc, channel, is_24G,
48577569SXinghua.Wen@Sun.COM 		    is_fat, is_high_chan);
48587569SXinghua.Wen@Sun.COM 		if (NULL == eep_chan_p) {
48597569SXinghua.Wen@Sun.COM 			cmn_err(CE_WARN,
48607569SXinghua.Wen@Sun.COM 			    "iwk_txpower_table_cmd_init(): "
48617569SXinghua.Wen@Sun.COM 			    "can't get channel infomation\n");
48627569SXinghua.Wen@Sun.COM 			return (DDI_FAILURE);
48637569SXinghua.Wen@Sun.COM 		}
48647569SXinghua.Wen@Sun.COM 	} else {
48657569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_txpower_table_cmd_init(): "
48667569SXinghua.Wen@Sun.COM 		    "channel(%d) isn't in proper range\n",
48677569SXinghua.Wen@Sun.COM 		    channel);
48687569SXinghua.Wen@Sun.COM 		return (DDI_FAILURE);
48697569SXinghua.Wen@Sun.COM 	}
48707569SXinghua.Wen@Sun.COM 
48717569SXinghua.Wen@Sun.COM 	/* initial value of Tx power */
48727569SXinghua.Wen@Sun.COM 	sc->sc_user_txpower = (int32_t)eep_chan_p->max_power_avg;
48737569SXinghua.Wen@Sun.COM 	if (sc->sc_user_txpower < IWK_TX_POWER_TARGET_POWER_MIN) {
48747569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_txpower_table_cmd_init(): "
48757569SXinghua.Wen@Sun.COM 		    "user TX power is too weak\n");
48767569SXinghua.Wen@Sun.COM 		return (DDI_FAILURE);
48777569SXinghua.Wen@Sun.COM 	} else if (sc->sc_user_txpower > IWK_TX_POWER_TARGET_POWER_MAX) {
48787569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_txpower_table_cmd_init(): "
48797569SXinghua.Wen@Sun.COM 		    "user TX power is too strong\n");
48807569SXinghua.Wen@Sun.COM 		return (DDI_FAILURE);
48817569SXinghua.Wen@Sun.COM 	}
48827569SXinghua.Wen@Sun.COM 
48837569SXinghua.Wen@Sun.COM 	target_power = 2 * sc->sc_user_txpower;
48847569SXinghua.Wen@Sun.COM 
48857569SXinghua.Wen@Sun.COM 	/* determine which group current channel belongs to */
48867569SXinghua.Wen@Sun.COM 	tx_grp = iwk_txpower_grp(channel);
48877569SXinghua.Wen@Sun.COM 	if (tx_grp < 0) {
48887569SXinghua.Wen@Sun.COM 		return (tx_grp);
48897569SXinghua.Wen@Sun.COM 	}
48907569SXinghua.Wen@Sun.COM 
48917569SXinghua.Wen@Sun.COM 
48927569SXinghua.Wen@Sun.COM 	if (is_fat) {
48937569SXinghua.Wen@Sun.COM 		if (is_high_chan) {
48947569SXinghua.Wen@Sun.COM 			channel -= 2;
48957569SXinghua.Wen@Sun.COM 		} else {
48967569SXinghua.Wen@Sun.COM 			channel += 2;
48977569SXinghua.Wen@Sun.COM 		}
48987569SXinghua.Wen@Sun.COM 	}
48997569SXinghua.Wen@Sun.COM 
49007569SXinghua.Wen@Sun.COM 	/* determine saturation power */
49017569SXinghua.Wen@Sun.COM 	if (is_24G) {
49027569SXinghua.Wen@Sun.COM 		saturation_power =
49037569SXinghua.Wen@Sun.COM 		    sc->sc_eep_map.calib_info.saturation_power24;
49047569SXinghua.Wen@Sun.COM 	} else {
49057569SXinghua.Wen@Sun.COM 		saturation_power =
49067569SXinghua.Wen@Sun.COM 		    sc->sc_eep_map.calib_info.saturation_power52;
49077569SXinghua.Wen@Sun.COM 	}
49087569SXinghua.Wen@Sun.COM 
49097569SXinghua.Wen@Sun.COM 	if (saturation_power < IWK_TX_POWER_SATURATION_MIN ||
49107569SXinghua.Wen@Sun.COM 	    saturation_power > IWK_TX_POWER_SATURATION_MAX) {
49117569SXinghua.Wen@Sun.COM 		if (is_24G) {
49127569SXinghua.Wen@Sun.COM 			saturation_power = IWK_TX_POWER_DEFAULT_SATURATION_24;
49137569SXinghua.Wen@Sun.COM 		} else {
49147569SXinghua.Wen@Sun.COM 			saturation_power = IWK_TX_POWER_DEFAULT_SATURATION_52;
49157569SXinghua.Wen@Sun.COM 		}
49167569SXinghua.Wen@Sun.COM 	}
49177569SXinghua.Wen@Sun.COM 
49187569SXinghua.Wen@Sun.COM 	/* determine regulatory power */
49197569SXinghua.Wen@Sun.COM 	regu_power = (int32_t)eep_chan_p->max_power_avg * 2;
49207569SXinghua.Wen@Sun.COM 	if ((regu_power < IWK_TX_POWER_REGULATORY_MIN) ||
49217569SXinghua.Wen@Sun.COM 	    (regu_power > IWK_TX_POWER_REGULATORY_MAX)) {
49227569SXinghua.Wen@Sun.COM 		if (is_24G) {
49237569SXinghua.Wen@Sun.COM 			regu_power = IWK_TX_POWER_DEFAULT_REGULATORY_24;
49247569SXinghua.Wen@Sun.COM 		} else {
49257569SXinghua.Wen@Sun.COM 			regu_power = IWK_TX_POWER_DEFAULT_REGULATORY_52;
49267569SXinghua.Wen@Sun.COM 		}
49277569SXinghua.Wen@Sun.COM 	}
49287569SXinghua.Wen@Sun.COM 
49297569SXinghua.Wen@Sun.COM 	/*
49307569SXinghua.Wen@Sun.COM 	 * get measurement data for current channel
49317569SXinghua.Wen@Sun.COM 	 * suach as temperature,index to gain table,actual Tx power
49327569SXinghua.Wen@Sun.COM 	 */
49337569SXinghua.Wen@Sun.COM 	(void) iwk_channel_interpolate(sc, channel, &eep_chan_calib);
49347569SXinghua.Wen@Sun.COM 
49359327SFei.Feng@Sun.COM 	eep_voltage = (int32_t)LE_16(sc->sc_eep_map.calib_info.voltage);
49369327SFei.Feng@Sun.COM 	init_voltage = (int32_t)LE_32(sc->sc_card_alive_init.voltage);
49377569SXinghua.Wen@Sun.COM 
49387569SXinghua.Wen@Sun.COM 	/* calculate voltage compensation to Tx power */
49397569SXinghua.Wen@Sun.COM 	voltage_compensation =
49407569SXinghua.Wen@Sun.COM 	    iwk_voltage_compensation(eep_voltage, init_voltage);
49417569SXinghua.Wen@Sun.COM 
49427569SXinghua.Wen@Sun.COM 	if (sc->sc_tempera >= IWK_TX_POWER_TEMPERATURE_MIN) {
49437569SXinghua.Wen@Sun.COM 		temperature = sc->sc_tempera;
49447569SXinghua.Wen@Sun.COM 	} else {
49457569SXinghua.Wen@Sun.COM 		temperature = IWK_TX_POWER_TEMPERATURE_MIN;
49467569SXinghua.Wen@Sun.COM 	}
49477569SXinghua.Wen@Sun.COM 	if (sc->sc_tempera <= IWK_TX_POWER_TEMPERATURE_MAX) {
49487569SXinghua.Wen@Sun.COM 		temperature = sc->sc_tempera;
49497569SXinghua.Wen@Sun.COM 	} else {
49507569SXinghua.Wen@Sun.COM 		temperature = IWK_TX_POWER_TEMPERATURE_MAX;
49517569SXinghua.Wen@Sun.COM 	}
49527569SXinghua.Wen@Sun.COM 	temperature = KELVIN_TO_CELSIUS(temperature);
49537569SXinghua.Wen@Sun.COM 
49547569SXinghua.Wen@Sun.COM 	degrees_per_05db_num =
49557569SXinghua.Wen@Sun.COM 	    txpower_tempera_comp_table[tx_grp].degrees_per_05db_a;
49567569SXinghua.Wen@Sun.COM 	degrees_per_05db_denom =
49577569SXinghua.Wen@Sun.COM 	    txpower_tempera_comp_table[tx_grp].degrees_per_05db_a_denom;
49587569SXinghua.Wen@Sun.COM 
49597569SXinghua.Wen@Sun.COM 	for (c = 0; c < 2; c++) {  /* go through all chains */
49607569SXinghua.Wen@Sun.COM 		measure_p = &eep_chan_calib.measure[c][1];
49617569SXinghua.Wen@Sun.COM 		interpo_temp = measure_p->temperature;
49627569SXinghua.Wen@Sun.COM 
49637569SXinghua.Wen@Sun.COM 		/* determine temperature compensation to Tx power */
49647569SXinghua.Wen@Sun.COM 		(void) iwk_division(
49657569SXinghua.Wen@Sun.COM 		    (temperature-interpo_temp)*degrees_per_05db_denom,
49667569SXinghua.Wen@Sun.COM 		    degrees_per_05db_num, &tempera_comp[c]);
49677569SXinghua.Wen@Sun.COM 
49687569SXinghua.Wen@Sun.COM 		interpo_gain_idx[c] = measure_p->gain_idx;
49697569SXinghua.Wen@Sun.COM 		interpo_actual_pow[c] = measure_p->actual_pow;
49707569SXinghua.Wen@Sun.COM 	}
49717569SXinghua.Wen@Sun.COM 
49727569SXinghua.Wen@Sun.COM 	/*
49737569SXinghua.Wen@Sun.COM 	 * go through all rate entries in Tx power table
49747569SXinghua.Wen@Sun.COM 	 */
49757569SXinghua.Wen@Sun.COM 	for (r = 0; r < POWER_TABLE_NUM_ENTRIES; r++) {
49767569SXinghua.Wen@Sun.COM 		if (r & 0x8) {
49777569SXinghua.Wen@Sun.COM 			/* need to lower regulatory power for MIMO mode */
49787569SXinghua.Wen@Sun.COM 			curr_regu_power = regu_power -
49797569SXinghua.Wen@Sun.COM 			    IWK_TX_POWER_MIMO_REGULATORY_COMPENSATION;
49807569SXinghua.Wen@Sun.COM 			is_mimo = 1;
49817569SXinghua.Wen@Sun.COM 		} else {
49827569SXinghua.Wen@Sun.COM 			curr_regu_power = regu_power;
49837569SXinghua.Wen@Sun.COM 			is_mimo = 0;
49847569SXinghua.Wen@Sun.COM 		}
49857569SXinghua.Wen@Sun.COM 
49867569SXinghua.Wen@Sun.COM 		power_limit = saturation_power - back_off_table[r];
49877569SXinghua.Wen@Sun.COM 		if (power_limit > curr_regu_power) {
49887569SXinghua.Wen@Sun.COM 			/* final Tx power limit */
49897569SXinghua.Wen@Sun.COM 			power_limit = curr_regu_power;
49907569SXinghua.Wen@Sun.COM 		}
49917569SXinghua.Wen@Sun.COM 
49927569SXinghua.Wen@Sun.COM 		if (target_power > power_limit) {
49937569SXinghua.Wen@Sun.COM 			target_power = power_limit; /* final target Tx power */
49947569SXinghua.Wen@Sun.COM 		}
49957569SXinghua.Wen@Sun.COM 
49967569SXinghua.Wen@Sun.COM 		for (c = 0; c < 2; c++) {	  /* go through all Tx chains */
49977569SXinghua.Wen@Sun.COM 			if (is_mimo) {
49987569SXinghua.Wen@Sun.COM 				atten_value =
49999327SFei.Feng@Sun.COM 				    LE_32(sc->sc_card_alive_init.
50009327SFei.Feng@Sun.COM 				    tx_atten[tx_grp][c]);
50017569SXinghua.Wen@Sun.COM 			} else {
50027569SXinghua.Wen@Sun.COM 				atten_value = 0;
50037569SXinghua.Wen@Sun.COM 			}
50047569SXinghua.Wen@Sun.COM 
50057569SXinghua.Wen@Sun.COM 			/*
50067569SXinghua.Wen@Sun.COM 			 * calculate index in gain table
50077569SXinghua.Wen@Sun.COM 			 * this step is very important
50087569SXinghua.Wen@Sun.COM 			 */
50097569SXinghua.Wen@Sun.COM 			txpower_gains_idx = interpo_gain_idx[c] -
50107569SXinghua.Wen@Sun.COM 			    (target_power - interpo_actual_pow[c]) -
50117569SXinghua.Wen@Sun.COM 			    tempera_comp[c] - voltage_compensation +
50127569SXinghua.Wen@Sun.COM 			    atten_value;
50137569SXinghua.Wen@Sun.COM 
50147569SXinghua.Wen@Sun.COM 			if (txpower_gains_idx <
50157569SXinghua.Wen@Sun.COM 			    iwk_min_power_index(r, is_24G)) {
50167569SXinghua.Wen@Sun.COM 				txpower_gains_idx =
50177569SXinghua.Wen@Sun.COM 				    iwk_min_power_index(r, is_24G);
50187569SXinghua.Wen@Sun.COM 			}
50197569SXinghua.Wen@Sun.COM 
50207569SXinghua.Wen@Sun.COM 			if (!is_24G) {
50217569SXinghua.Wen@Sun.COM 				/*
50227569SXinghua.Wen@Sun.COM 				 * support negative index for 5 GHz
50237569SXinghua.Wen@Sun.COM 				 * band
50247569SXinghua.Wen@Sun.COM 				 */
50257569SXinghua.Wen@Sun.COM 				txpower_gains_idx += 9;
50267569SXinghua.Wen@Sun.COM 			}
50277569SXinghua.Wen@Sun.COM 
50287569SXinghua.Wen@Sun.COM 			if (POWER_TABLE_CCK_ENTRY == r) {
50297569SXinghua.Wen@Sun.COM 				/* for CCK mode, make necessary attenuaton */
50307569SXinghua.Wen@Sun.COM 				txpower_gains_idx +=
50317569SXinghua.Wen@Sun.COM 				    IWK_TX_POWER_CCK_COMPENSATION_C_STEP;
50327569SXinghua.Wen@Sun.COM 			}
50337569SXinghua.Wen@Sun.COM 
50347569SXinghua.Wen@Sun.COM 			if (txpower_gains_idx > 107) {
50357569SXinghua.Wen@Sun.COM 				txpower_gains_idx = 107;
50367569SXinghua.Wen@Sun.COM 			} else if (txpower_gains_idx < 0) {
50377569SXinghua.Wen@Sun.COM 				txpower_gains_idx = 0;
50387569SXinghua.Wen@Sun.COM 			}
50397569SXinghua.Wen@Sun.COM 
50407569SXinghua.Wen@Sun.COM 			/* search DSP and radio gains in gain table */
50417569SXinghua.Wen@Sun.COM 			txpower_gains.s.radio_tx_gain[c] =
50427569SXinghua.Wen@Sun.COM 			    gains_table[is_24G][txpower_gains_idx].radio;
50437569SXinghua.Wen@Sun.COM 			txpower_gains.s.dsp_predis_atten[c] =
50447569SXinghua.Wen@Sun.COM 			    gains_table[is_24G][txpower_gains_idx].dsp;
50457569SXinghua.Wen@Sun.COM 
50467569SXinghua.Wen@Sun.COM 			IWK_DBG((IWK_DEBUG_CALIBRATION,
50477569SXinghua.Wen@Sun.COM 			    "rate_index: %d, "
50487569SXinghua.Wen@Sun.COM 			    "gain_index %d, c: %d,is_mimo: %d\n",
50497569SXinghua.Wen@Sun.COM 			    r, txpower_gains_idx, c, is_mimo));
50507569SXinghua.Wen@Sun.COM 		}
50517569SXinghua.Wen@Sun.COM 
50527569SXinghua.Wen@Sun.COM 		/* initialize Tx power table */
50537569SXinghua.Wen@Sun.COM 		if (r < POWER_TABLE_NUM_HT_OFDM_ENTRIES) {
50549327SFei.Feng@Sun.COM 			tp_db->ht_ofdm_power[r].dw = LE_32(txpower_gains.dw);
50557569SXinghua.Wen@Sun.COM 		} else {
50569327SFei.Feng@Sun.COM 			tp_db->legacy_cck_power.dw = LE_32(txpower_gains.dw);
50577569SXinghua.Wen@Sun.COM 		}
50587569SXinghua.Wen@Sun.COM 	}
50597569SXinghua.Wen@Sun.COM 
50607569SXinghua.Wen@Sun.COM 	return (IWK_SUCCESS);
50617569SXinghua.Wen@Sun.COM }
50627569SXinghua.Wen@Sun.COM 
50637569SXinghua.Wen@Sun.COM /*
50647569SXinghua.Wen@Sun.COM  * make Tx power calibration to adjust Tx power.
50657569SXinghua.Wen@Sun.COM  * This is completed by sending out Tx power table command.
50667569SXinghua.Wen@Sun.COM  */
iwk_tx_power_calibration(iwk_sc_t * sc)50677569SXinghua.Wen@Sun.COM static int iwk_tx_power_calibration(iwk_sc_t *sc)
50687569SXinghua.Wen@Sun.COM {
50697569SXinghua.Wen@Sun.COM 	iwk_tx_power_table_cmd_t cmd;
50707569SXinghua.Wen@Sun.COM 	int rv;
50717569SXinghua.Wen@Sun.COM 
50727569SXinghua.Wen@Sun.COM 	if (sc->sc_flags & IWK_F_SCANNING) {
50737569SXinghua.Wen@Sun.COM 		return (IWK_SUCCESS);
50747569SXinghua.Wen@Sun.COM 	}
50757569SXinghua.Wen@Sun.COM 
50767569SXinghua.Wen@Sun.COM 	/* necessary initialization to Tx power table command */
50777569SXinghua.Wen@Sun.COM 	cmd.band = (uint8_t)iwk_is_24G_band(sc);
50787569SXinghua.Wen@Sun.COM 	cmd.channel = sc->sc_config.chan;
50797569SXinghua.Wen@Sun.COM 	cmd.channel_normal_width = 0;
50807569SXinghua.Wen@Sun.COM 
50817569SXinghua.Wen@Sun.COM 	/* initialize Tx power table */
50827569SXinghua.Wen@Sun.COM 	rv = iwk_txpower_table_cmd_init(sc, &cmd.tx_power);
50837569SXinghua.Wen@Sun.COM 	if (rv) {
50847569SXinghua.Wen@Sun.COM 		cmn_err(CE_NOTE, "rv= %d\n", rv);
50857569SXinghua.Wen@Sun.COM 		return (rv);
50867569SXinghua.Wen@Sun.COM 	}
50877569SXinghua.Wen@Sun.COM 
50887569SXinghua.Wen@Sun.COM 	/* send out Tx power table command */
50897569SXinghua.Wen@Sun.COM 	rv = iwk_cmd(sc, REPLY_TX_PWR_TABLE_CMD, &cmd, sizeof (cmd), 1);
50907569SXinghua.Wen@Sun.COM 	if (rv) {
50917569SXinghua.Wen@Sun.COM 		return (rv);
50927569SXinghua.Wen@Sun.COM 	}
50937569SXinghua.Wen@Sun.COM 
50947569SXinghua.Wen@Sun.COM 	/* record current temperature */
50957569SXinghua.Wen@Sun.COM 	sc->sc_last_tempera = sc->sc_tempera;
50967569SXinghua.Wen@Sun.COM 
50977569SXinghua.Wen@Sun.COM 	return (IWK_SUCCESS);
50987569SXinghua.Wen@Sun.COM }
50997569SXinghua.Wen@Sun.COM 
51007569SXinghua.Wen@Sun.COM /* This function is the handler of statistics notification from uCode */
iwk_statistics_notify(iwk_sc_t * sc,iwk_rx_desc_t * desc)51017569SXinghua.Wen@Sun.COM static void iwk_statistics_notify(iwk_sc_t *sc, iwk_rx_desc_t *desc)
51027569SXinghua.Wen@Sun.COM {
51037569SXinghua.Wen@Sun.COM 	int is_diff;
51047569SXinghua.Wen@Sun.COM 	struct iwk_notif_statistics *statistics_p =
51057569SXinghua.Wen@Sun.COM 	    (struct iwk_notif_statistics *)(desc + 1);
51067569SXinghua.Wen@Sun.COM 
51077569SXinghua.Wen@Sun.COM 	mutex_enter(&sc->sc_glock);
51087569SXinghua.Wen@Sun.COM 
51097569SXinghua.Wen@Sun.COM 	is_diff = (sc->sc_statistics.general.temperature !=
51107569SXinghua.Wen@Sun.COM 	    statistics_p->general.temperature) ||
51119327SFei.Feng@Sun.COM 	    (LE_32(sc->sc_statistics.flag) &
51129327SFei.Feng@Sun.COM 	    STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
51139327SFei.Feng@Sun.COM 	    (LE_32(statistics_p->flag) & STATISTICS_REPLY_FLG_FAT_MODE_MSK);
51147569SXinghua.Wen@Sun.COM 
51157569SXinghua.Wen@Sun.COM 	/* update statistics data */
51167569SXinghua.Wen@Sun.COM 	(void) memcpy(&sc->sc_statistics, statistics_p,
51177569SXinghua.Wen@Sun.COM 	    sizeof (struct iwk_notif_statistics));
51187569SXinghua.Wen@Sun.COM 
51197569SXinghua.Wen@Sun.COM 	sc->sc_flags |= IWK_F_STATISTICS;
51207569SXinghua.Wen@Sun.COM 
51217569SXinghua.Wen@Sun.COM 	if (!(sc->sc_flags & IWK_F_SCANNING)) {
51227569SXinghua.Wen@Sun.COM 		/* make Receiver gain balance calibration */
51237569SXinghua.Wen@Sun.COM 		(void) iwk_rxgain_diff(sc);
51247569SXinghua.Wen@Sun.COM 
51257569SXinghua.Wen@Sun.COM 		/* make Receiver sensitivity calibration */
51267569SXinghua.Wen@Sun.COM 		(void) iwk_rx_sens(sc);
51277569SXinghua.Wen@Sun.COM 	}
51287569SXinghua.Wen@Sun.COM 
51297569SXinghua.Wen@Sun.COM 
51307569SXinghua.Wen@Sun.COM 	if (!is_diff) {
51317569SXinghua.Wen@Sun.COM 		mutex_exit(&sc->sc_glock);
51327569SXinghua.Wen@Sun.COM 		return;
51337569SXinghua.Wen@Sun.COM 	}
51347569SXinghua.Wen@Sun.COM 
51357569SXinghua.Wen@Sun.COM 	/* calibration current temperature of 4965 chipset */
51367569SXinghua.Wen@Sun.COM 	sc->sc_tempera = iwk_curr_tempera(sc);
51377569SXinghua.Wen@Sun.COM 
51387569SXinghua.Wen@Sun.COM 	/* distinct temperature change will trigger Tx power calibration */
51397569SXinghua.Wen@Sun.COM 	if (((sc->sc_tempera - sc->sc_last_tempera) >= 3) ||
51407569SXinghua.Wen@Sun.COM 	    ((sc->sc_last_tempera - sc->sc_tempera) >= 3)) {
51417569SXinghua.Wen@Sun.COM 		/* make Tx power calibration */
51427569SXinghua.Wen@Sun.COM 		(void) iwk_tx_power_calibration(sc);
51437569SXinghua.Wen@Sun.COM 	}
51447569SXinghua.Wen@Sun.COM 
51457569SXinghua.Wen@Sun.COM 	mutex_exit(&sc->sc_glock);
51467569SXinghua.Wen@Sun.COM }
51477569SXinghua.Wen@Sun.COM 
51487569SXinghua.Wen@Sun.COM /* Determine this station is in associated state or not */
iwk_is_associated(iwk_sc_t * sc)51497569SXinghua.Wen@Sun.COM static int iwk_is_associated(iwk_sc_t *sc)
51507569SXinghua.Wen@Sun.COM {
51519327SFei.Feng@Sun.COM 	return (LE_32(sc->sc_config.filter_flags) & RXON_FILTER_ASSOC_MSK);
51527569SXinghua.Wen@Sun.COM }
51537569SXinghua.Wen@Sun.COM 
51547569SXinghua.Wen@Sun.COM /* Make necessary preparation for Receiver gain balance calibration */
iwk_rxgain_diff_init(iwk_sc_t * sc)51557569SXinghua.Wen@Sun.COM static int iwk_rxgain_diff_init(iwk_sc_t *sc)
51567569SXinghua.Wen@Sun.COM {
51577569SXinghua.Wen@Sun.COM 	int i, rv;
51587569SXinghua.Wen@Sun.COM 	struct iwk_calibration_cmd cmd;
51597569SXinghua.Wen@Sun.COM 	struct iwk_rx_gain_diff *gain_diff_p;
51607569SXinghua.Wen@Sun.COM 
51617569SXinghua.Wen@Sun.COM 	gain_diff_p = &sc->sc_rxgain_diff;
51627569SXinghua.Wen@Sun.COM 
51637569SXinghua.Wen@Sun.COM 	(void) memset(gain_diff_p, 0, sizeof (struct iwk_rx_gain_diff));
51647569SXinghua.Wen@Sun.COM 	(void) memset(&cmd, 0, sizeof (struct iwk_calibration_cmd));
51657569SXinghua.Wen@Sun.COM 
51667569SXinghua.Wen@Sun.COM 	for (i = 0; i < RX_CHAINS_NUM; i++) {
51677569SXinghua.Wen@Sun.COM 		gain_diff_p->gain_diff_chain[i] = CHAIN_GAIN_DIFF_INIT_VAL;
51687569SXinghua.Wen@Sun.COM 	}
51697569SXinghua.Wen@Sun.COM 
51707569SXinghua.Wen@Sun.COM 	if (iwk_is_associated(sc)) {
51717569SXinghua.Wen@Sun.COM 		cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
51727569SXinghua.Wen@Sun.COM 		cmd.diff_gain_a = 0;
51737569SXinghua.Wen@Sun.COM 		cmd.diff_gain_b = 0;
51747569SXinghua.Wen@Sun.COM 		cmd.diff_gain_c = 0;
51757569SXinghua.Wen@Sun.COM 
51767569SXinghua.Wen@Sun.COM 		/* assume the gains of every Rx chains is balanceable */
51777569SXinghua.Wen@Sun.COM 		rv = iwk_cmd(sc, REPLY_PHY_CALIBRATION_CMD, &cmd,
51787569SXinghua.Wen@Sun.COM 		    sizeof (cmd), 1);
51797569SXinghua.Wen@Sun.COM 		if (rv) {
51807569SXinghua.Wen@Sun.COM 			return (rv);
51817569SXinghua.Wen@Sun.COM 		}
51827569SXinghua.Wen@Sun.COM 
51837569SXinghua.Wen@Sun.COM 		gain_diff_p->state = IWK_GAIN_DIFF_ACCUMULATE;
51847569SXinghua.Wen@Sun.COM 	}
51857569SXinghua.Wen@Sun.COM 
51867569SXinghua.Wen@Sun.COM 	return (IWK_SUCCESS);
51877569SXinghua.Wen@Sun.COM }
51887569SXinghua.Wen@Sun.COM 
51897569SXinghua.Wen@Sun.COM /*
51907569SXinghua.Wen@Sun.COM  * make Receiver gain balance to balance Rx gain between Rx chains
51917569SXinghua.Wen@Sun.COM  * and determine which chain is disconnected
51927569SXinghua.Wen@Sun.COM  */
iwk_rxgain_diff(iwk_sc_t * sc)51937569SXinghua.Wen@Sun.COM static int iwk_rxgain_diff(iwk_sc_t *sc)
51947569SXinghua.Wen@Sun.COM {
51957569SXinghua.Wen@Sun.COM 	int i, is_24G, rv;
51967569SXinghua.Wen@Sun.COM 	int max_beacon_chain_n;
51977569SXinghua.Wen@Sun.COM 	int min_noise_chain_n;
51987569SXinghua.Wen@Sun.COM 	uint16_t channel_n;
51997569SXinghua.Wen@Sun.COM 	int32_t beacon_diff;
52007569SXinghua.Wen@Sun.COM 	int32_t noise_diff;
52017569SXinghua.Wen@Sun.COM 	uint32_t noise_chain_a, noise_chain_b, noise_chain_c;
52027569SXinghua.Wen@Sun.COM 	uint32_t beacon_chain_a, beacon_chain_b, beacon_chain_c;
52037569SXinghua.Wen@Sun.COM 	struct iwk_calibration_cmd cmd;
52047569SXinghua.Wen@Sun.COM 	uint32_t beacon_aver[RX_CHAINS_NUM] = {0xFFFFFFFF};
52057569SXinghua.Wen@Sun.COM 	uint32_t noise_aver[RX_CHAINS_NUM] = {0xFFFFFFFF};
52067569SXinghua.Wen@Sun.COM 	struct statistics_rx_non_phy *rx_general_p =
52077569SXinghua.Wen@Sun.COM 	    &sc->sc_statistics.rx.general;
52087569SXinghua.Wen@Sun.COM 	struct iwk_rx_gain_diff *gain_diff_p = &sc->sc_rxgain_diff;
52097569SXinghua.Wen@Sun.COM 
52107569SXinghua.Wen@Sun.COM 	if (INTERFERENCE_DATA_AVAILABLE !=
52119327SFei.Feng@Sun.COM 	    LE_32(rx_general_p->interference_data_flag)) {
52127569SXinghua.Wen@Sun.COM 		return (IWK_SUCCESS);
52137569SXinghua.Wen@Sun.COM 	}
52147569SXinghua.Wen@Sun.COM 
52157569SXinghua.Wen@Sun.COM 	if (IWK_GAIN_DIFF_ACCUMULATE != gain_diff_p->state) {
52167569SXinghua.Wen@Sun.COM 		return (IWK_SUCCESS);
52177569SXinghua.Wen@Sun.COM 	}
52187569SXinghua.Wen@Sun.COM 
52197569SXinghua.Wen@Sun.COM 	is_24G = iwk_is_24G_band(sc);
52207569SXinghua.Wen@Sun.COM 	channel_n = sc->sc_config.chan;	 /* channel number */
52217569SXinghua.Wen@Sun.COM 
52229327SFei.Feng@Sun.COM 	if ((channel_n != (LE_32(sc->sc_statistics.flag) >> 16)) ||
52237569SXinghua.Wen@Sun.COM 	    ((STATISTICS_REPLY_FLG_BAND_24G_MSK ==
52249327SFei.Feng@Sun.COM 	    (LE_32(sc->sc_statistics.flag) &
52259327SFei.Feng@Sun.COM 	    STATISTICS_REPLY_FLG_BAND_24G_MSK)) &&
52267569SXinghua.Wen@Sun.COM 	    !is_24G)) {
52277569SXinghua.Wen@Sun.COM 		return (IWK_SUCCESS);
52287569SXinghua.Wen@Sun.COM 	}
52297569SXinghua.Wen@Sun.COM 
52307569SXinghua.Wen@Sun.COM 	/* Rx chain's noise strength from statistics notification */
52319327SFei.Feng@Sun.COM 	noise_chain_a = LE_32(rx_general_p->beacon_silence_rssi_a) & 0xFF;
52329327SFei.Feng@Sun.COM 	noise_chain_b = LE_32(rx_general_p->beacon_silence_rssi_b) & 0xFF;
52339327SFei.Feng@Sun.COM 	noise_chain_c = LE_32(rx_general_p->beacon_silence_rssi_c) & 0xFF;
52347569SXinghua.Wen@Sun.COM 
52357569SXinghua.Wen@Sun.COM 	/* Rx chain's beacon strength from statistics notification */
52369327SFei.Feng@Sun.COM 	beacon_chain_a = LE_32(rx_general_p->beacon_rssi_a) & 0xFF;
52379327SFei.Feng@Sun.COM 	beacon_chain_b = LE_32(rx_general_p->beacon_rssi_b) & 0xFF;
52389327SFei.Feng@Sun.COM 	beacon_chain_c = LE_32(rx_general_p->beacon_rssi_c) & 0xFF;
52397569SXinghua.Wen@Sun.COM 
52407569SXinghua.Wen@Sun.COM 	gain_diff_p->beacon_count++;
52417569SXinghua.Wen@Sun.COM 
52427569SXinghua.Wen@Sun.COM 	/* accumulate chain's noise strength */
52437569SXinghua.Wen@Sun.COM 	gain_diff_p->noise_stren_a += noise_chain_a;
52447569SXinghua.Wen@Sun.COM 	gain_diff_p->noise_stren_b += noise_chain_b;
52457569SXinghua.Wen@Sun.COM 	gain_diff_p->noise_stren_c += noise_chain_c;
52467569SXinghua.Wen@Sun.COM 
52477569SXinghua.Wen@Sun.COM 	/* accumulate chain's beacon strength */
52487569SXinghua.Wen@Sun.COM 	gain_diff_p->beacon_stren_a += beacon_chain_a;
52497569SXinghua.Wen@Sun.COM 	gain_diff_p->beacon_stren_b += beacon_chain_b;
52507569SXinghua.Wen@Sun.COM 	gain_diff_p->beacon_stren_c += beacon_chain_c;
52517569SXinghua.Wen@Sun.COM 
52527569SXinghua.Wen@Sun.COM 	if (BEACON_NUM_20 == gain_diff_p->beacon_count) {
52537569SXinghua.Wen@Sun.COM 		/* calculate average beacon strength */
52547569SXinghua.Wen@Sun.COM 		beacon_aver[0] = (gain_diff_p->beacon_stren_a) / BEACON_NUM_20;
52557569SXinghua.Wen@Sun.COM 		beacon_aver[1] = (gain_diff_p->beacon_stren_b) / BEACON_NUM_20;
52567569SXinghua.Wen@Sun.COM 		beacon_aver[2] = (gain_diff_p->beacon_stren_c) / BEACON_NUM_20;
52577569SXinghua.Wen@Sun.COM 
52587569SXinghua.Wen@Sun.COM 		/* calculate average noise strength */
52597569SXinghua.Wen@Sun.COM 		noise_aver[0] = (gain_diff_p->noise_stren_a) / BEACON_NUM_20;
52607569SXinghua.Wen@Sun.COM 		noise_aver[1] = (gain_diff_p->noise_stren_b) / BEACON_NUM_20;
52617569SXinghua.Wen@Sun.COM 		noise_aver[2] = (gain_diff_p->noise_stren_b) / BEACON_NUM_20;
52627569SXinghua.Wen@Sun.COM 
52637569SXinghua.Wen@Sun.COM 		/* determine maximum beacon strength among 3 chains */
52647569SXinghua.Wen@Sun.COM 		if ((beacon_aver[0] >= beacon_aver[1]) &&
52657569SXinghua.Wen@Sun.COM 		    (beacon_aver[0] >= beacon_aver[2])) {
52667569SXinghua.Wen@Sun.COM 			max_beacon_chain_n = 0;
52677569SXinghua.Wen@Sun.COM 			gain_diff_p->connected_chains = 1 << 0;
52687569SXinghua.Wen@Sun.COM 		} else if (beacon_aver[1] >= beacon_aver[2]) {
52697569SXinghua.Wen@Sun.COM 			max_beacon_chain_n = 1;
52707569SXinghua.Wen@Sun.COM 			gain_diff_p->connected_chains = 1 << 1;
52717569SXinghua.Wen@Sun.COM 		} else {
52727569SXinghua.Wen@Sun.COM 			max_beacon_chain_n = 2;
52737569SXinghua.Wen@Sun.COM 			gain_diff_p->connected_chains = 1 << 2;
52747569SXinghua.Wen@Sun.COM 		}
52757569SXinghua.Wen@Sun.COM 
52767569SXinghua.Wen@Sun.COM 		/* determine which chain is disconnected */
52777569SXinghua.Wen@Sun.COM 		for (i = 0; i < RX_CHAINS_NUM; i++) {
52787569SXinghua.Wen@Sun.COM 			if (i != max_beacon_chain_n) {
52797569SXinghua.Wen@Sun.COM 				beacon_diff = beacon_aver[max_beacon_chain_n] -
52807569SXinghua.Wen@Sun.COM 				    beacon_aver[i];
52817569SXinghua.Wen@Sun.COM 				if (beacon_diff > MAX_ALLOWED_DIFF) {
52827569SXinghua.Wen@Sun.COM 					gain_diff_p->disconnect_chain[i] = 1;
52837569SXinghua.Wen@Sun.COM 				} else {
52847569SXinghua.Wen@Sun.COM 					gain_diff_p->connected_chains |=
52857569SXinghua.Wen@Sun.COM 					    (1 << i);
52867569SXinghua.Wen@Sun.COM 				}
52877569SXinghua.Wen@Sun.COM 			}
52887569SXinghua.Wen@Sun.COM 		}
52897569SXinghua.Wen@Sun.COM 
52907569SXinghua.Wen@Sun.COM 		/*
52917569SXinghua.Wen@Sun.COM 		 * if chain A and B are both disconnected,
52927569SXinghua.Wen@Sun.COM 		 * assume the stronger in beacon strength is connected
52937569SXinghua.Wen@Sun.COM 		 */
52947569SXinghua.Wen@Sun.COM 		if (gain_diff_p->disconnect_chain[0] &&
52957569SXinghua.Wen@Sun.COM 		    gain_diff_p->disconnect_chain[1]) {
52967569SXinghua.Wen@Sun.COM 			if (beacon_aver[0] >= beacon_aver[1]) {
52977569SXinghua.Wen@Sun.COM 				gain_diff_p->disconnect_chain[0] = 0;
52987569SXinghua.Wen@Sun.COM 				gain_diff_p->connected_chains |= (1 << 0);
52997569SXinghua.Wen@Sun.COM 			} else {
53007569SXinghua.Wen@Sun.COM 				gain_diff_p->disconnect_chain[1] = 0;
53017569SXinghua.Wen@Sun.COM 				gain_diff_p->connected_chains |= (1 << 1);
53027569SXinghua.Wen@Sun.COM 			}
53037569SXinghua.Wen@Sun.COM 		}
53047569SXinghua.Wen@Sun.COM 
53057569SXinghua.Wen@Sun.COM 		/* determine minimum noise strength among 3 chains */
53067569SXinghua.Wen@Sun.COM 		if (!gain_diff_p->disconnect_chain[0]) {
53077569SXinghua.Wen@Sun.COM 			min_noise_chain_n = 0;
53087569SXinghua.Wen@Sun.COM 
53097569SXinghua.Wen@Sun.COM 			for (i = 0; i < RX_CHAINS_NUM; i++) {
53107569SXinghua.Wen@Sun.COM 				if (!gain_diff_p->disconnect_chain[i] &&
53117569SXinghua.Wen@Sun.COM 				    (noise_aver[i] <=
53127569SXinghua.Wen@Sun.COM 				    noise_aver[min_noise_chain_n])) {
53137569SXinghua.Wen@Sun.COM 					min_noise_chain_n = i;
53147569SXinghua.Wen@Sun.COM 				}
53157569SXinghua.Wen@Sun.COM 
53167569SXinghua.Wen@Sun.COM 			}
53177569SXinghua.Wen@Sun.COM 		} else {
53187569SXinghua.Wen@Sun.COM 			min_noise_chain_n = 1;
53197569SXinghua.Wen@Sun.COM 
53207569SXinghua.Wen@Sun.COM 			for (i = 0; i < RX_CHAINS_NUM; i++) {
53217569SXinghua.Wen@Sun.COM 				if (!gain_diff_p->disconnect_chain[i] &&
53227569SXinghua.Wen@Sun.COM 				    (noise_aver[i] <=
53237569SXinghua.Wen@Sun.COM 				    noise_aver[min_noise_chain_n])) {
53247569SXinghua.Wen@Sun.COM 					min_noise_chain_n = i;
53257569SXinghua.Wen@Sun.COM 				}
53267569SXinghua.Wen@Sun.COM 			}
53277569SXinghua.Wen@Sun.COM 		}
53287569SXinghua.Wen@Sun.COM 
53297569SXinghua.Wen@Sun.COM 		gain_diff_p->gain_diff_chain[min_noise_chain_n] = 0;
53307569SXinghua.Wen@Sun.COM 
53317569SXinghua.Wen@Sun.COM 		/* determine gain difference between chains */
53327569SXinghua.Wen@Sun.COM 		for (i = 0; i < RX_CHAINS_NUM; i++) {
53337569SXinghua.Wen@Sun.COM 			if (!gain_diff_p->disconnect_chain[i] &&
53347569SXinghua.Wen@Sun.COM 			    (CHAIN_GAIN_DIFF_INIT_VAL ==
53357569SXinghua.Wen@Sun.COM 			    gain_diff_p->gain_diff_chain[i])) {
53367569SXinghua.Wen@Sun.COM 
53377569SXinghua.Wen@Sun.COM 				noise_diff = noise_aver[i] -
53387569SXinghua.Wen@Sun.COM 				    noise_aver[min_noise_chain_n];
53397569SXinghua.Wen@Sun.COM 				gain_diff_p->gain_diff_chain[i] =
53407569SXinghua.Wen@Sun.COM 				    (uint8_t)((noise_diff * 10) / 15);
53417569SXinghua.Wen@Sun.COM 
53427569SXinghua.Wen@Sun.COM 				if (gain_diff_p->gain_diff_chain[i] > 3) {
53437569SXinghua.Wen@Sun.COM 					gain_diff_p->gain_diff_chain[i] = 3;
53447569SXinghua.Wen@Sun.COM 				}
53457569SXinghua.Wen@Sun.COM 
53467569SXinghua.Wen@Sun.COM 				gain_diff_p->gain_diff_chain[i] |= (1 << 2);
53477569SXinghua.Wen@Sun.COM 			} else {
53487569SXinghua.Wen@Sun.COM 				gain_diff_p->gain_diff_chain[i] = 0;
53497569SXinghua.Wen@Sun.COM 			}
53507569SXinghua.Wen@Sun.COM 		}
53517569SXinghua.Wen@Sun.COM 
53527569SXinghua.Wen@Sun.COM 		if (!gain_diff_p->gain_diff_send) {
53537569SXinghua.Wen@Sun.COM 			gain_diff_p->gain_diff_send = 1;
53547569SXinghua.Wen@Sun.COM 
53557569SXinghua.Wen@Sun.COM 			(void) memset(&cmd, 0, sizeof (cmd));
53567569SXinghua.Wen@Sun.COM 
53577569SXinghua.Wen@Sun.COM 			cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
53587569SXinghua.Wen@Sun.COM 			cmd.diff_gain_a = gain_diff_p->gain_diff_chain[0];
53597569SXinghua.Wen@Sun.COM 			cmd.diff_gain_b = gain_diff_p->gain_diff_chain[1];
53607569SXinghua.Wen@Sun.COM 			cmd.diff_gain_c = gain_diff_p->gain_diff_chain[2];
53617569SXinghua.Wen@Sun.COM 
53627569SXinghua.Wen@Sun.COM 			/*
53637569SXinghua.Wen@Sun.COM 			 * send out PHY calibration command to
53647569SXinghua.Wen@Sun.COM 			 * adjust every chain's Rx gain
53657569SXinghua.Wen@Sun.COM 			 */
53667569SXinghua.Wen@Sun.COM 			rv = iwk_cmd(sc, REPLY_PHY_CALIBRATION_CMD,
53677569SXinghua.Wen@Sun.COM 			    &cmd, sizeof (cmd), 1);
53687569SXinghua.Wen@Sun.COM 			if (rv) {
53697569SXinghua.Wen@Sun.COM 				return (rv);
53707569SXinghua.Wen@Sun.COM 			}
53717569SXinghua.Wen@Sun.COM 
53727569SXinghua.Wen@Sun.COM 			gain_diff_p->state = IWK_GAIN_DIFF_CALIBRATED;
53737569SXinghua.Wen@Sun.COM 		}
53747569SXinghua.Wen@Sun.COM 
53757569SXinghua.Wen@Sun.COM 		gain_diff_p->beacon_stren_a = 0;
53767569SXinghua.Wen@Sun.COM 		gain_diff_p->beacon_stren_b = 0;
53777569SXinghua.Wen@Sun.COM 		gain_diff_p->beacon_stren_c = 0;
53787569SXinghua.Wen@Sun.COM 
53797569SXinghua.Wen@Sun.COM 		gain_diff_p->noise_stren_a = 0;
53807569SXinghua.Wen@Sun.COM 		gain_diff_p->noise_stren_b = 0;
53817569SXinghua.Wen@Sun.COM 		gain_diff_p->noise_stren_c = 0;
53827569SXinghua.Wen@Sun.COM 	}
53837569SXinghua.Wen@Sun.COM 
53847569SXinghua.Wen@Sun.COM 	return (IWK_SUCCESS);
53857569SXinghua.Wen@Sun.COM }
53867569SXinghua.Wen@Sun.COM 
53877569SXinghua.Wen@Sun.COM /* Make necessary preparation for Receiver sensitivity calibration */
iwk_rx_sens_init(iwk_sc_t * sc)53887569SXinghua.Wen@Sun.COM static int iwk_rx_sens_init(iwk_sc_t *sc)
53897569SXinghua.Wen@Sun.COM {
53907569SXinghua.Wen@Sun.COM 	int i, rv;
53917569SXinghua.Wen@Sun.COM 	struct iwk_rx_sensitivity_cmd cmd;
53927569SXinghua.Wen@Sun.COM 	struct iwk_rx_sensitivity *rx_sens_p = &sc->sc_rx_sens;
53937569SXinghua.Wen@Sun.COM 
53947569SXinghua.Wen@Sun.COM 	(void) memset(&cmd, 0, sizeof (struct iwk_rx_sensitivity_cmd));
53957569SXinghua.Wen@Sun.COM 	(void) memset(rx_sens_p, 0, sizeof (struct iwk_rx_sensitivity));
53967569SXinghua.Wen@Sun.COM 
53977569SXinghua.Wen@Sun.COM 	rx_sens_p->auto_corr_ofdm_x4 = 90;
53987569SXinghua.Wen@Sun.COM 	rx_sens_p->auto_corr_mrc_ofdm_x4 = 170;
53997569SXinghua.Wen@Sun.COM 	rx_sens_p->auto_corr_ofdm_x1 = 105;
54007569SXinghua.Wen@Sun.COM 	rx_sens_p->auto_corr_mrc_ofdm_x1 = 220;
54017569SXinghua.Wen@Sun.COM 
54027569SXinghua.Wen@Sun.COM 	rx_sens_p->auto_corr_cck_x4 = 125;
54037569SXinghua.Wen@Sun.COM 	rx_sens_p->auto_corr_mrc_cck_x4 = 200;
54047569SXinghua.Wen@Sun.COM 	rx_sens_p->min_energy_det_cck = 100;
54057569SXinghua.Wen@Sun.COM 
54067569SXinghua.Wen@Sun.COM 	rx_sens_p->flags &= (~IWK_SENSITIVITY_CALIB_ALLOW_MSK);
54077569SXinghua.Wen@Sun.COM 	rx_sens_p->flags &= (~IWK_SENSITIVITY_OFDM_UPDATE_MSK);
54087569SXinghua.Wen@Sun.COM 	rx_sens_p->flags &= (~IWK_SENSITIVITY_CCK_UPDATE_MSK);
54097569SXinghua.Wen@Sun.COM 
54107569SXinghua.Wen@Sun.COM 	rx_sens_p->last_bad_plcp_cnt_ofdm = 0;
54117569SXinghua.Wen@Sun.COM 	rx_sens_p->last_false_alarm_cnt_ofdm = 0;
54127569SXinghua.Wen@Sun.COM 	rx_sens_p->last_bad_plcp_cnt_cck = 0;
54137569SXinghua.Wen@Sun.COM 	rx_sens_p->last_false_alarm_cnt_cck = 0;
54147569SXinghua.Wen@Sun.COM 
54157569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_curr_state = IWK_TOO_MANY_FALSE_ALARM;
54167569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_prev_state = IWK_TOO_MANY_FALSE_ALARM;
54177569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_no_false_alarm_num = 0;
54187569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_beacon_idx = 0;
54197569SXinghua.Wen@Sun.COM 
54207569SXinghua.Wen@Sun.COM 	for (i = 0; i < 10; i++) {
54217569SXinghua.Wen@Sun.COM 		rx_sens_p->cck_beacon_min[i] = 0;
54227569SXinghua.Wen@Sun.COM 	}
54237569SXinghua.Wen@Sun.COM 
54247569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_noise_idx = 0;
54257569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_noise_ref = 0;
54267569SXinghua.Wen@Sun.COM 
54277569SXinghua.Wen@Sun.COM 	for (i = 0; i < 20; i++) {
54287569SXinghua.Wen@Sun.COM 		rx_sens_p->cck_noise_max[i] = 0;
54297569SXinghua.Wen@Sun.COM 	}
54307569SXinghua.Wen@Sun.COM 
54317569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_noise_diff = 0;
54327569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_no_false_alarm_num = 0;
54337569SXinghua.Wen@Sun.COM 
54349327SFei.Feng@Sun.COM 	cmd.control = LE_16(IWK_SENSITIVITY_CONTROL_WORK_TABLE);
54357569SXinghua.Wen@Sun.COM 
54367569SXinghua.Wen@Sun.COM 	cmd.table[AUTO_CORR32_X4_TH_ADD_MIN_IDX] =
54379327SFei.Feng@Sun.COM 	    LE_16(rx_sens_p->auto_corr_ofdm_x4);
54387569SXinghua.Wen@Sun.COM 	cmd.table[AUTO_CORR32_X4_TH_ADD_MIN_MRC_IDX] =
54399327SFei.Feng@Sun.COM 	    LE_16(rx_sens_p->auto_corr_mrc_ofdm_x4);
54407569SXinghua.Wen@Sun.COM 	cmd.table[AUTO_CORR32_X1_TH_ADD_MIN_IDX] =
54419327SFei.Feng@Sun.COM 	    LE_16(rx_sens_p->auto_corr_ofdm_x1);
54427569SXinghua.Wen@Sun.COM 	cmd.table[AUTO_CORR32_X1_TH_ADD_MIN_MRC_IDX] =
54439327SFei.Feng@Sun.COM 	    LE_16(rx_sens_p->auto_corr_mrc_ofdm_x1);
54447569SXinghua.Wen@Sun.COM 
54457569SXinghua.Wen@Sun.COM 	cmd.table[AUTO_CORR40_X4_TH_ADD_MIN_IDX] =
54469327SFei.Feng@Sun.COM 	    LE_16(rx_sens_p->auto_corr_cck_x4);
54477569SXinghua.Wen@Sun.COM 	cmd.table[AUTO_CORR40_X4_TH_ADD_MIN_MRC_IDX] =
54489327SFei.Feng@Sun.COM 	    LE_16(rx_sens_p->auto_corr_mrc_cck_x4);
54499327SFei.Feng@Sun.COM 	cmd.table[MIN_ENERGY_CCK_DET_IDX] =
54509327SFei.Feng@Sun.COM 	    LE_16(rx_sens_p->min_energy_det_cck);
54519327SFei.Feng@Sun.COM 
54529327SFei.Feng@Sun.COM 	cmd.table[MIN_ENERGY_OFDM_DET_IDX] = LE_16(100);
54539327SFei.Feng@Sun.COM 	cmd.table[BARKER_CORR_TH_ADD_MIN_IDX] = LE_16(190);
54549327SFei.Feng@Sun.COM 	cmd.table[BARKER_CORR_TH_ADD_MIN_MRC_IDX] = LE_16(390);
54559327SFei.Feng@Sun.COM 	cmd.table[PTAM_ENERGY_TH_IDX] = LE_16(62);
54567569SXinghua.Wen@Sun.COM 
54577569SXinghua.Wen@Sun.COM 	/* at first, set up Rx to maximum sensitivity */
54587569SXinghua.Wen@Sun.COM 	rv = iwk_cmd(sc, SENSITIVITY_CMD, &cmd, sizeof (cmd), 1);
54597569SXinghua.Wen@Sun.COM 	if (rv) {
54607569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_rx_sens_init(): "
54617569SXinghua.Wen@Sun.COM 		    "in the process of initialization, "
54627569SXinghua.Wen@Sun.COM 		    "failed to send rx sensitivity command\n");
54637569SXinghua.Wen@Sun.COM 		return (rv);
54647569SXinghua.Wen@Sun.COM 	}
54657569SXinghua.Wen@Sun.COM 
54667569SXinghua.Wen@Sun.COM 	rx_sens_p->flags |= IWK_SENSITIVITY_CALIB_ALLOW_MSK;
54677569SXinghua.Wen@Sun.COM 
54687569SXinghua.Wen@Sun.COM 	return (IWK_SUCCESS);
54697569SXinghua.Wen@Sun.COM }
54707569SXinghua.Wen@Sun.COM 
54717569SXinghua.Wen@Sun.COM /*
54727569SXinghua.Wen@Sun.COM  * make Receiver sensitivity calibration to adjust every chain's Rx sensitivity.
54737569SXinghua.Wen@Sun.COM  * for more infomation, please refer to iwk_calibration.h file
54747569SXinghua.Wen@Sun.COM  */
iwk_rx_sens(iwk_sc_t * sc)54757569SXinghua.Wen@Sun.COM static int iwk_rx_sens(iwk_sc_t *sc)
54767569SXinghua.Wen@Sun.COM {
54777569SXinghua.Wen@Sun.COM 	int rv;
54787569SXinghua.Wen@Sun.COM 	uint32_t actual_rx_time;
54797569SXinghua.Wen@Sun.COM 	struct statistics_rx_non_phy *rx_general_p =
54807569SXinghua.Wen@Sun.COM 	    &sc->sc_statistics.rx.general;
54817569SXinghua.Wen@Sun.COM 	struct iwk_rx_sensitivity *rx_sens_p = &sc->sc_rx_sens;
54827569SXinghua.Wen@Sun.COM 	struct iwk_rx_sensitivity_cmd cmd;
54837569SXinghua.Wen@Sun.COM 
54847569SXinghua.Wen@Sun.COM 	if (!(rx_sens_p->flags & IWK_SENSITIVITY_CALIB_ALLOW_MSK)) {
54857569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_rx_sens(): "
54867569SXinghua.Wen@Sun.COM 		    "sensitivity initialization has not finished.\n");
54877569SXinghua.Wen@Sun.COM 		return (DDI_FAILURE);
54887569SXinghua.Wen@Sun.COM 	}
54897569SXinghua.Wen@Sun.COM 
54907569SXinghua.Wen@Sun.COM 	if (INTERFERENCE_DATA_AVAILABLE !=
54919327SFei.Feng@Sun.COM 	    LE_32(rx_general_p->interference_data_flag)) {
54927569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_rx_sens(): "
54937569SXinghua.Wen@Sun.COM 		    "can't make rx sensitivity calibration,"
54947569SXinghua.Wen@Sun.COM 		    "because of invalid statistics\n");
54957569SXinghua.Wen@Sun.COM 		return (DDI_FAILURE);
54967569SXinghua.Wen@Sun.COM 	}
54977569SXinghua.Wen@Sun.COM 
54989327SFei.Feng@Sun.COM 	actual_rx_time = LE_32(rx_general_p->channel_load);
54997569SXinghua.Wen@Sun.COM 	if (!actual_rx_time) {
55007891SPengcheng.Chen@Sun.COM 		IWK_DBG((IWK_DEBUG_CALIBRATION, "iwk_rx_sens(): "
55017569SXinghua.Wen@Sun.COM 		    "can't make rx sensitivity calibration,"
55027891SPengcheng.Chen@Sun.COM 		    "because has not enough rx time\n"));
55037569SXinghua.Wen@Sun.COM 		return (DDI_FAILURE);
55047569SXinghua.Wen@Sun.COM 	}
55057569SXinghua.Wen@Sun.COM 
55067569SXinghua.Wen@Sun.COM 	/* make Rx sensitivity calibration for OFDM mode */
55077569SXinghua.Wen@Sun.COM 	rv = iwk_ofdm_sens(sc, actual_rx_time);
55087569SXinghua.Wen@Sun.COM 	if (rv) {
55097569SXinghua.Wen@Sun.COM 		return (rv);
55107569SXinghua.Wen@Sun.COM 	}
55117569SXinghua.Wen@Sun.COM 
55127569SXinghua.Wen@Sun.COM 	/* make Rx sensitivity calibration for CCK mode */
55137569SXinghua.Wen@Sun.COM 	rv = iwk_cck_sens(sc, actual_rx_time);
55147569SXinghua.Wen@Sun.COM 	if (rv) {
55157569SXinghua.Wen@Sun.COM 		return (rv);
55167569SXinghua.Wen@Sun.COM 	}
55177569SXinghua.Wen@Sun.COM 
55187569SXinghua.Wen@Sun.COM 	/*
55197569SXinghua.Wen@Sun.COM 	 * if the sum of false alarm had not changed, nothing will be done
55207569SXinghua.Wen@Sun.COM 	 */
55217569SXinghua.Wen@Sun.COM 	if ((!(rx_sens_p->flags & IWK_SENSITIVITY_OFDM_UPDATE_MSK)) &&
55227569SXinghua.Wen@Sun.COM 	    (!(rx_sens_p->flags & IWK_SENSITIVITY_CCK_UPDATE_MSK))) {
55237569SXinghua.Wen@Sun.COM 		return (IWK_SUCCESS);
55247569SXinghua.Wen@Sun.COM 	}
55257569SXinghua.Wen@Sun.COM 
55267569SXinghua.Wen@Sun.COM 	cmd.control = IWK_SENSITIVITY_CONTROL_WORK_TABLE;
55277569SXinghua.Wen@Sun.COM 
55287569SXinghua.Wen@Sun.COM 	cmd.table[AUTO_CORR32_X4_TH_ADD_MIN_IDX] =
55297569SXinghua.Wen@Sun.COM 	    rx_sens_p->auto_corr_ofdm_x4;
55307569SXinghua.Wen@Sun.COM 	cmd.table[AUTO_CORR32_X4_TH_ADD_MIN_MRC_IDX] =
55317569SXinghua.Wen@Sun.COM 	    rx_sens_p->auto_corr_mrc_ofdm_x4;
55327569SXinghua.Wen@Sun.COM 	cmd.table[AUTO_CORR32_X1_TH_ADD_MIN_IDX] =
55337569SXinghua.Wen@Sun.COM 	    rx_sens_p->auto_corr_ofdm_x1;
55347569SXinghua.Wen@Sun.COM 	cmd.table[AUTO_CORR32_X1_TH_ADD_MIN_MRC_IDX] =
55357569SXinghua.Wen@Sun.COM 	    rx_sens_p->auto_corr_mrc_ofdm_x1;
55367569SXinghua.Wen@Sun.COM 
55377569SXinghua.Wen@Sun.COM 	cmd.table[AUTO_CORR40_X4_TH_ADD_MIN_IDX] =
55387569SXinghua.Wen@Sun.COM 	    rx_sens_p->auto_corr_cck_x4;
55397569SXinghua.Wen@Sun.COM 	cmd.table[AUTO_CORR40_X4_TH_ADD_MIN_MRC_IDX] =
55407569SXinghua.Wen@Sun.COM 	    rx_sens_p->auto_corr_mrc_cck_x4;
55417569SXinghua.Wen@Sun.COM 	cmd.table[MIN_ENERGY_CCK_DET_IDX] =
55427569SXinghua.Wen@Sun.COM 	    rx_sens_p->min_energy_det_cck;
55437569SXinghua.Wen@Sun.COM 
55447569SXinghua.Wen@Sun.COM 	cmd.table[MIN_ENERGY_OFDM_DET_IDX] = 100;
55457569SXinghua.Wen@Sun.COM 	cmd.table[BARKER_CORR_TH_ADD_MIN_IDX] = 190;
55467569SXinghua.Wen@Sun.COM 	cmd.table[BARKER_CORR_TH_ADD_MIN_MRC_IDX] = 390;
55477569SXinghua.Wen@Sun.COM 	cmd.table[PTAM_ENERGY_TH_IDX] = 62;
55487569SXinghua.Wen@Sun.COM 
55497569SXinghua.Wen@Sun.COM 	/*
55507569SXinghua.Wen@Sun.COM 	 * send sensitivity command to complete actual sensitivity calibration
55517569SXinghua.Wen@Sun.COM 	 */
55527569SXinghua.Wen@Sun.COM 	rv = iwk_cmd(sc, SENSITIVITY_CMD, &cmd, sizeof (cmd), 1);
55537569SXinghua.Wen@Sun.COM 	if (rv) {
55547569SXinghua.Wen@Sun.COM 		cmn_err(CE_WARN, "iwk_rx_sens(): "
55557569SXinghua.Wen@Sun.COM 		    "fail to send rx sensitivity command\n");
55567569SXinghua.Wen@Sun.COM 		return (rv);
55577569SXinghua.Wen@Sun.COM 	}
55587569SXinghua.Wen@Sun.COM 
55597569SXinghua.Wen@Sun.COM 	return (IWK_SUCCESS);
55607569SXinghua.Wen@Sun.COM 
55617569SXinghua.Wen@Sun.COM }
55627569SXinghua.Wen@Sun.COM 
55637569SXinghua.Wen@Sun.COM /*
55647569SXinghua.Wen@Sun.COM  * make Rx sensitivity calibration for CCK mode.
55657569SXinghua.Wen@Sun.COM  * This is preparing parameters for Sensitivity command
55667569SXinghua.Wen@Sun.COM  */
iwk_cck_sens(iwk_sc_t * sc,uint32_t actual_rx_time)55677569SXinghua.Wen@Sun.COM static int iwk_cck_sens(iwk_sc_t *sc, uint32_t actual_rx_time)
55687569SXinghua.Wen@Sun.COM {
55697569SXinghua.Wen@Sun.COM 	int i;
55707569SXinghua.Wen@Sun.COM 	uint8_t noise_a, noise_b, noise_c;
55717569SXinghua.Wen@Sun.COM 	uint8_t max_noise_abc, max_noise_20;
55727569SXinghua.Wen@Sun.COM 	uint32_t beacon_a, beacon_b, beacon_c;
55737569SXinghua.Wen@Sun.COM 	uint32_t min_beacon_abc, max_beacon_10;
55747569SXinghua.Wen@Sun.COM 	uint32_t cck_fa, cck_bp;
55757569SXinghua.Wen@Sun.COM 	uint32_t cck_sum_fa_bp;
55767569SXinghua.Wen@Sun.COM 	uint32_t temp;
55777569SXinghua.Wen@Sun.COM 	struct statistics_rx_non_phy *rx_general_p =
55787569SXinghua.Wen@Sun.COM 	    &sc->sc_statistics.rx.general;
55797569SXinghua.Wen@Sun.COM 	struct iwk_rx_sensitivity *rx_sens_p = &sc->sc_rx_sens;
55807569SXinghua.Wen@Sun.COM 
55819327SFei.Feng@Sun.COM 	cck_fa = LE_32(sc->sc_statistics.rx.cck.false_alarm_cnt);
55829327SFei.Feng@Sun.COM 	cck_bp = LE_32(sc->sc_statistics.rx.cck.plcp_err);
55837569SXinghua.Wen@Sun.COM 
55847569SXinghua.Wen@Sun.COM 	/* accumulate false alarm */
55857569SXinghua.Wen@Sun.COM 	if (rx_sens_p->last_false_alarm_cnt_cck > cck_fa) {
55867569SXinghua.Wen@Sun.COM 		temp = rx_sens_p->last_false_alarm_cnt_cck;
55877569SXinghua.Wen@Sun.COM 		rx_sens_p->last_false_alarm_cnt_cck = cck_fa;
55887569SXinghua.Wen@Sun.COM 		cck_fa += (0xFFFFFFFF - temp);
55897569SXinghua.Wen@Sun.COM 	} else {
55907569SXinghua.Wen@Sun.COM 		cck_fa -= rx_sens_p->last_false_alarm_cnt_cck;
55917569SXinghua.Wen@Sun.COM 		rx_sens_p->last_false_alarm_cnt_cck += cck_fa;
55927569SXinghua.Wen@Sun.COM 	}
55937569SXinghua.Wen@Sun.COM 
55947569SXinghua.Wen@Sun.COM 	/* accumulate bad plcp */
55957569SXinghua.Wen@Sun.COM 	if (rx_sens_p->last_bad_plcp_cnt_cck > cck_bp) {
55967569SXinghua.Wen@Sun.COM 		temp = rx_sens_p->last_bad_plcp_cnt_cck;
55977569SXinghua.Wen@Sun.COM 		rx_sens_p->last_bad_plcp_cnt_cck = cck_bp;
55987569SXinghua.Wen@Sun.COM 		cck_bp += (0xFFFFFFFF - temp);
55997569SXinghua.Wen@Sun.COM 	} else {
56007569SXinghua.Wen@Sun.COM 		cck_bp -= rx_sens_p->last_bad_plcp_cnt_cck;
56017569SXinghua.Wen@Sun.COM 		rx_sens_p->last_bad_plcp_cnt_cck += cck_bp;
56027569SXinghua.Wen@Sun.COM 	}
56037569SXinghua.Wen@Sun.COM 
56047569SXinghua.Wen@Sun.COM 	/*
56057569SXinghua.Wen@Sun.COM 	 * calculate relative value
56067569SXinghua.Wen@Sun.COM 	 */
56077569SXinghua.Wen@Sun.COM 	cck_sum_fa_bp = (cck_fa + cck_bp) * 200 * 1024;
56087569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_noise_diff = 0;
56097569SXinghua.Wen@Sun.COM 
56107569SXinghua.Wen@Sun.COM 	noise_a =
56119327SFei.Feng@Sun.COM 	    (uint8_t)((LE_32(rx_general_p->beacon_silence_rssi_a) & 0xFF00) >>
56129327SFei.Feng@Sun.COM 	    8);
56137569SXinghua.Wen@Sun.COM 	noise_b =
56149327SFei.Feng@Sun.COM 	    (uint8_t)((LE_32(rx_general_p->beacon_silence_rssi_b) & 0xFF00) >>
56159327SFei.Feng@Sun.COM 	    8);
56167569SXinghua.Wen@Sun.COM 	noise_c =
56179327SFei.Feng@Sun.COM 	    (uint8_t)((LE_32(rx_general_p->beacon_silence_rssi_c) & 0xFF00) >>
56189327SFei.Feng@Sun.COM 	    8);
56199327SFei.Feng@Sun.COM 
56209327SFei.Feng@Sun.COM 	beacon_a = LE_32(rx_general_p->beacon_energy_a);
56219327SFei.Feng@Sun.COM 	beacon_b = LE_32(rx_general_p->beacon_energy_b);
56229327SFei.Feng@Sun.COM 	beacon_c = LE_32(rx_general_p->beacon_energy_c);
56237569SXinghua.Wen@Sun.COM 
56247569SXinghua.Wen@Sun.COM 	/* determine maximum noise among 3 chains */
56257569SXinghua.Wen@Sun.COM 	if ((noise_a >= noise_b) && (noise_a >= noise_c)) {
56267569SXinghua.Wen@Sun.COM 		max_noise_abc = noise_a;
56277569SXinghua.Wen@Sun.COM 	} else if (noise_b >= noise_c) {
56287569SXinghua.Wen@Sun.COM 		max_noise_abc = noise_b;
56297569SXinghua.Wen@Sun.COM 	} else {
56307569SXinghua.Wen@Sun.COM 		max_noise_abc = noise_c;
56317569SXinghua.Wen@Sun.COM 	}
56327569SXinghua.Wen@Sun.COM 
56337569SXinghua.Wen@Sun.COM 	/* record maximum noise among 3 chains */
56347569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_noise_max[rx_sens_p->cck_noise_idx] = max_noise_abc;
56357569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_noise_idx++;
56367569SXinghua.Wen@Sun.COM 	if (rx_sens_p->cck_noise_idx >= 20) {
56377569SXinghua.Wen@Sun.COM 		rx_sens_p->cck_noise_idx = 0;
56387569SXinghua.Wen@Sun.COM 	}
56397569SXinghua.Wen@Sun.COM 
56407569SXinghua.Wen@Sun.COM 	/* determine maximum noise among 20 max noise */
56417569SXinghua.Wen@Sun.COM 	max_noise_20 = rx_sens_p->cck_noise_max[0];
56427569SXinghua.Wen@Sun.COM 	for (i = 0; i < 20; i++) {
56437569SXinghua.Wen@Sun.COM 		if (rx_sens_p->cck_noise_max[i] >= max_noise_20) {
56447569SXinghua.Wen@Sun.COM 			max_noise_20 = rx_sens_p->cck_noise_max[i];
56457569SXinghua.Wen@Sun.COM 		}
56467569SXinghua.Wen@Sun.COM 	}
56477569SXinghua.Wen@Sun.COM 
56487569SXinghua.Wen@Sun.COM 	/* determine minimum beacon among 3 chains */
56497569SXinghua.Wen@Sun.COM 	if ((beacon_a <= beacon_b) && (beacon_a <= beacon_c)) {
56507569SXinghua.Wen@Sun.COM 		min_beacon_abc = beacon_a;
56517569SXinghua.Wen@Sun.COM 	} else if (beacon_b <= beacon_c) {
56527569SXinghua.Wen@Sun.COM 		min_beacon_abc = beacon_b;
56537569SXinghua.Wen@Sun.COM 	} else {
56547569SXinghua.Wen@Sun.COM 		min_beacon_abc = beacon_c;
56557569SXinghua.Wen@Sun.COM 	}
56567569SXinghua.Wen@Sun.COM 
56577569SXinghua.Wen@Sun.COM 	/* record miminum beacon among 3 chains */
56587569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_beacon_min[rx_sens_p->cck_beacon_idx] = min_beacon_abc;
56597569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_beacon_idx++;
56607569SXinghua.Wen@Sun.COM 	if (rx_sens_p->cck_beacon_idx >= 10) {
56617569SXinghua.Wen@Sun.COM 		rx_sens_p->cck_beacon_idx = 0;
56627569SXinghua.Wen@Sun.COM 	}
56637569SXinghua.Wen@Sun.COM 
56647569SXinghua.Wen@Sun.COM 	/* determine maximum beacon among 10 miminum beacon among 3 chains */
56657569SXinghua.Wen@Sun.COM 	max_beacon_10 = rx_sens_p->cck_beacon_min[0];
56667569SXinghua.Wen@Sun.COM 	for (i = 0; i < 10; i++) {
56677569SXinghua.Wen@Sun.COM 		if (rx_sens_p->cck_beacon_min[i] >= max_beacon_10) {
56687569SXinghua.Wen@Sun.COM 			max_beacon_10 = rx_sens_p->cck_beacon_min[i];
56697569SXinghua.Wen@Sun.COM 		}
56707569SXinghua.Wen@Sun.COM 	}
56717569SXinghua.Wen@Sun.COM 
56727569SXinghua.Wen@Sun.COM 	/* add a little margin */
56737569SXinghua.Wen@Sun.COM 	max_beacon_10 += 6;
56747569SXinghua.Wen@Sun.COM 
56757569SXinghua.Wen@Sun.COM 	/* record the count of having no false alarms */
56767569SXinghua.Wen@Sun.COM 	if (cck_sum_fa_bp < (5 * actual_rx_time)) {
56777569SXinghua.Wen@Sun.COM 		rx_sens_p->cck_no_false_alarm_num++;
56787569SXinghua.Wen@Sun.COM 	} else {
56797569SXinghua.Wen@Sun.COM 		rx_sens_p->cck_no_false_alarm_num = 0;
56807569SXinghua.Wen@Sun.COM 	}
56817569SXinghua.Wen@Sun.COM 
56827569SXinghua.Wen@Sun.COM 	/*
56837569SXinghua.Wen@Sun.COM 	 * adjust parameters in sensitivity command
56847569SXinghua.Wen@Sun.COM 	 * according to different status.
56857569SXinghua.Wen@Sun.COM 	 * for more infomation, please refer to iwk_calibration.h file
56867569SXinghua.Wen@Sun.COM 	 */
56877569SXinghua.Wen@Sun.COM 	if (cck_sum_fa_bp > (50 * actual_rx_time)) {
56887569SXinghua.Wen@Sun.COM 		rx_sens_p->cck_curr_state = IWK_TOO_MANY_FALSE_ALARM;
56897569SXinghua.Wen@Sun.COM 
56907569SXinghua.Wen@Sun.COM 		if (rx_sens_p->auto_corr_cck_x4 > 160) {
56917569SXinghua.Wen@Sun.COM 			rx_sens_p->cck_noise_ref = max_noise_20;
56927569SXinghua.Wen@Sun.COM 
56937569SXinghua.Wen@Sun.COM 			if (rx_sens_p->min_energy_det_cck > 2) {
56947569SXinghua.Wen@Sun.COM 				rx_sens_p->min_energy_det_cck -= 2;
56957569SXinghua.Wen@Sun.COM 			}
56967569SXinghua.Wen@Sun.COM 		}
56977569SXinghua.Wen@Sun.COM 
56987569SXinghua.Wen@Sun.COM 		if (rx_sens_p->auto_corr_cck_x4 < 160) {
56997569SXinghua.Wen@Sun.COM 			rx_sens_p->auto_corr_cck_x4 = 160 + 1;
57007569SXinghua.Wen@Sun.COM 		} else {
57017569SXinghua.Wen@Sun.COM 			if ((rx_sens_p->auto_corr_cck_x4 + 3) < 200) {
57027569SXinghua.Wen@Sun.COM 				rx_sens_p->auto_corr_cck_x4 += 3;
57037569SXinghua.Wen@Sun.COM 			} else {
57047569SXinghua.Wen@Sun.COM 				rx_sens_p->auto_corr_cck_x4 = 200;
57057569SXinghua.Wen@Sun.COM 			}
57067569SXinghua.Wen@Sun.COM 		}
57077569SXinghua.Wen@Sun.COM 
57087569SXinghua.Wen@Sun.COM 		if ((rx_sens_p->auto_corr_mrc_cck_x4 + 3) < 400) {
57097569SXinghua.Wen@Sun.COM 			rx_sens_p->auto_corr_mrc_cck_x4 += 3;
57107569SXinghua.Wen@Sun.COM 		} else {
57117569SXinghua.Wen@Sun.COM 			rx_sens_p->auto_corr_mrc_cck_x4 = 400;
57127569SXinghua.Wen@Sun.COM 		}
57137569SXinghua.Wen@Sun.COM 
57147569SXinghua.Wen@Sun.COM 		rx_sens_p->flags |= IWK_SENSITIVITY_CCK_UPDATE_MSK;
57157569SXinghua.Wen@Sun.COM 
57167569SXinghua.Wen@Sun.COM 	} else if (cck_sum_fa_bp < (5 * actual_rx_time)) {
57177569SXinghua.Wen@Sun.COM 		rx_sens_p->cck_curr_state = IWK_TOO_FEW_FALSE_ALARM;
57187569SXinghua.Wen@Sun.COM 
57197569SXinghua.Wen@Sun.COM 		rx_sens_p->cck_noise_diff = (int32_t)rx_sens_p->cck_noise_ref -
57207569SXinghua.Wen@Sun.COM 		    (int32_t)max_noise_20;
57217569SXinghua.Wen@Sun.COM 
57227569SXinghua.Wen@Sun.COM 		if ((rx_sens_p->cck_prev_state != IWK_TOO_MANY_FALSE_ALARM) &&
57237569SXinghua.Wen@Sun.COM 		    ((rx_sens_p->cck_noise_diff > 2) ||
57247569SXinghua.Wen@Sun.COM 		    (rx_sens_p->cck_no_false_alarm_num > 100))) {
57257569SXinghua.Wen@Sun.COM 			if ((rx_sens_p->min_energy_det_cck + 2) < 97) {
57267569SXinghua.Wen@Sun.COM 				rx_sens_p->min_energy_det_cck += 2;
57277569SXinghua.Wen@Sun.COM 			} else {
57287569SXinghua.Wen@Sun.COM 				rx_sens_p->min_energy_det_cck = 97;
57297569SXinghua.Wen@Sun.COM 			}
57307569SXinghua.Wen@Sun.COM 
57317569SXinghua.Wen@Sun.COM 			if ((rx_sens_p->auto_corr_cck_x4 - 3) > 125) {
57327569SXinghua.Wen@Sun.COM 				rx_sens_p->auto_corr_cck_x4 -= 3;
57337569SXinghua.Wen@Sun.COM 			} else {
57347569SXinghua.Wen@Sun.COM 				rx_sens_p->auto_corr_cck_x4 = 125;
57357569SXinghua.Wen@Sun.COM 			}
57367569SXinghua.Wen@Sun.COM 
57377569SXinghua.Wen@Sun.COM 			if ((rx_sens_p->auto_corr_mrc_cck_x4 -3) > 200) {
57387569SXinghua.Wen@Sun.COM 				rx_sens_p->auto_corr_mrc_cck_x4 -= 3;
57397569SXinghua.Wen@Sun.COM 			} else {
57407569SXinghua.Wen@Sun.COM 				rx_sens_p->auto_corr_mrc_cck_x4 = 200;
57417569SXinghua.Wen@Sun.COM 			}
57427569SXinghua.Wen@Sun.COM 
57437569SXinghua.Wen@Sun.COM 			rx_sens_p->flags |= IWK_SENSITIVITY_CCK_UPDATE_MSK;
57447569SXinghua.Wen@Sun.COM 		} else {
57457569SXinghua.Wen@Sun.COM 			rx_sens_p->flags &= (~IWK_SENSITIVITY_CCK_UPDATE_MSK);
57467569SXinghua.Wen@Sun.COM 		}
57477569SXinghua.Wen@Sun.COM 	} else {
57487569SXinghua.Wen@Sun.COM 		rx_sens_p->cck_curr_state = IWK_GOOD_RANGE_FALSE_ALARM;
57497569SXinghua.Wen@Sun.COM 
57507569SXinghua.Wen@Sun.COM 		rx_sens_p->cck_noise_ref = max_noise_20;
57517569SXinghua.Wen@Sun.COM 
57527569SXinghua.Wen@Sun.COM 		if (IWK_TOO_MANY_FALSE_ALARM == rx_sens_p->cck_prev_state) {
57537569SXinghua.Wen@Sun.COM 			rx_sens_p->min_energy_det_cck -= 8;
57547569SXinghua.Wen@Sun.COM 		}
57557569SXinghua.Wen@Sun.COM 
57567569SXinghua.Wen@Sun.COM 		rx_sens_p->flags &= (~IWK_SENSITIVITY_CCK_UPDATE_MSK);
57577569SXinghua.Wen@Sun.COM 	}
57587569SXinghua.Wen@Sun.COM 
57597569SXinghua.Wen@Sun.COM 	if (rx_sens_p->min_energy_det_cck < max_beacon_10) {
57607569SXinghua.Wen@Sun.COM 		rx_sens_p->min_energy_det_cck = (uint16_t)max_beacon_10;
57617569SXinghua.Wen@Sun.COM 	}
57627569SXinghua.Wen@Sun.COM 
57637569SXinghua.Wen@Sun.COM 	rx_sens_p->cck_prev_state = rx_sens_p->cck_curr_state;
57647569SXinghua.Wen@Sun.COM 
57657569SXinghua.Wen@Sun.COM 	return (IWK_SUCCESS);
57667569SXinghua.Wen@Sun.COM }
57677569SXinghua.Wen@Sun.COM 
57687569SXinghua.Wen@Sun.COM /*
57697569SXinghua.Wen@Sun.COM  * make Rx sensitivity calibration for OFDM mode.
57707569SXinghua.Wen@Sun.COM  * This is preparing parameters for Sensitivity command
57717569SXinghua.Wen@Sun.COM  */
iwk_ofdm_sens(iwk_sc_t * sc,uint32_t actual_rx_time)57727569SXinghua.Wen@Sun.COM static int iwk_ofdm_sens(iwk_sc_t *sc, uint32_t actual_rx_time)
57737569SXinghua.Wen@Sun.COM {
57747569SXinghua.Wen@Sun.COM 	uint32_t temp;
57757569SXinghua.Wen@Sun.COM 	uint16_t temp1;
57767569SXinghua.Wen@Sun.COM 	uint32_t ofdm_fa, ofdm_bp;
57777569SXinghua.Wen@Sun.COM 	uint32_t ofdm_sum_fa_bp;
57787569SXinghua.Wen@Sun.COM 	struct iwk_rx_sensitivity *rx_sens_p = &sc->sc_rx_sens;
57797569SXinghua.Wen@Sun.COM 
57809327SFei.Feng@Sun.COM 	ofdm_fa = LE_32(sc->sc_statistics.rx.ofdm.false_alarm_cnt);
57819327SFei.Feng@Sun.COM 	ofdm_bp = LE_32(sc->sc_statistics.rx.ofdm.plcp_err);
57827569SXinghua.Wen@Sun.COM 
57837569SXinghua.Wen@Sun.COM 	/* accumulate false alarm */
57847569SXinghua.Wen@Sun.COM 	if (rx_sens_p->last_false_alarm_cnt_ofdm > ofdm_fa) {
57857569SXinghua.Wen@Sun.COM 		temp = rx_sens_p->last_false_alarm_cnt_ofdm;
57867569SXinghua.Wen@Sun.COM 		rx_sens_p->last_false_alarm_cnt_ofdm = ofdm_fa;
57877569SXinghua.Wen@Sun.COM 		ofdm_fa += (0xFFFFFFFF - temp);
57887569SXinghua.Wen@Sun.COM 	} else {
57897569SXinghua.Wen@Sun.COM 		ofdm_fa -= rx_sens_p->last_false_alarm_cnt_ofdm;
57907569SXinghua.Wen@Sun.COM 		rx_sens_p->last_false_alarm_cnt_ofdm += ofdm_fa;
57917569SXinghua.Wen@Sun.COM 	}
57927569SXinghua.Wen@Sun.COM 
57937569SXinghua.Wen@Sun.COM 	/* accumulate bad plcp */
57947569SXinghua.Wen@Sun.COM 	if (rx_sens_p->last_bad_plcp_cnt_ofdm > ofdm_bp) {
57957569SXinghua.Wen@Sun.COM 		temp = rx_sens_p->last_bad_plcp_cnt_ofdm;
57967569SXinghua.Wen@Sun.COM 		rx_sens_p->last_bad_plcp_cnt_ofdm = ofdm_bp;
57977569SXinghua.Wen@Sun.COM 		ofdm_bp += (0xFFFFFFFF - temp);
57987569SXinghua.Wen@Sun.COM 	} else {
57997569SXinghua.Wen@Sun.COM 		ofdm_bp -= rx_sens_p->last_bad_plcp_cnt_ofdm;
58007569SXinghua.Wen@Sun.COM 		rx_sens_p->last_bad_plcp_cnt_ofdm += ofdm_bp;
58017569SXinghua.Wen@Sun.COM 	}
58027569SXinghua.Wen@Sun.COM 
58037569SXinghua.Wen@Sun.COM 	ofdm_sum_fa_bp = (ofdm_fa + ofdm_bp) * 200 * 1024; /* relative value */
58047569SXinghua.Wen@Sun.COM 
58057569SXinghua.Wen@Sun.COM 	/*
58067569SXinghua.Wen@Sun.COM 	 * adjust parameter in sensitivity command according to different status
58077569SXinghua.Wen@Sun.COM 	 */
58087569SXinghua.Wen@Sun.COM 	if (ofdm_sum_fa_bp > (50 * actual_rx_time)) {
58097569SXinghua.Wen@Sun.COM 		temp1 = rx_sens_p->auto_corr_ofdm_x4 + 1;
58107569SXinghua.Wen@Sun.COM 		rx_sens_p->auto_corr_ofdm_x4 = (temp1 <= 120) ? temp1 : 120;
58117569SXinghua.Wen@Sun.COM 
58127569SXinghua.Wen@Sun.COM 		temp1 = rx_sens_p->auto_corr_mrc_ofdm_x4 + 1;
58137569SXinghua.Wen@Sun.COM 		rx_sens_p->auto_corr_mrc_ofdm_x4 =
58147569SXinghua.Wen@Sun.COM 		    (temp1 <= 210) ? temp1 : 210;
58157569SXinghua.Wen@Sun.COM 
58167569SXinghua.Wen@Sun.COM 		temp1 = rx_sens_p->auto_corr_ofdm_x1 + 1;
58177569SXinghua.Wen@Sun.COM 		rx_sens_p->auto_corr_ofdm_x1 = (temp1 <= 140) ? temp1 : 140;
58187569SXinghua.Wen@Sun.COM 
58197569SXinghua.Wen@Sun.COM 		temp1 = rx_sens_p->auto_corr_mrc_ofdm_x1 + 1;
58207569SXinghua.Wen@Sun.COM 		rx_sens_p->auto_corr_mrc_ofdm_x1 =
58217569SXinghua.Wen@Sun.COM 		    (temp1 <= 270) ? temp1 : 270;
58227569SXinghua.Wen@Sun.COM 
58237569SXinghua.Wen@Sun.COM 		rx_sens_p->flags |= IWK_SENSITIVITY_OFDM_UPDATE_MSK;
58247569SXinghua.Wen@Sun.COM 
58257569SXinghua.Wen@Sun.COM 	} else if (ofdm_sum_fa_bp < (5 * actual_rx_time)) {
58267569SXinghua.Wen@Sun.COM 		temp1 = rx_sens_p->auto_corr_ofdm_x4 - 1;
58277569SXinghua.Wen@Sun.COM 		rx_sens_p->auto_corr_ofdm_x4 = (temp1 >= 85) ? temp1 : 85;
58287569SXinghua.Wen@Sun.COM 
58297569SXinghua.Wen@Sun.COM 		temp1 = rx_sens_p->auto_corr_mrc_ofdm_x4 - 1;
58307569SXinghua.Wen@Sun.COM 		rx_sens_p->auto_corr_mrc_ofdm_x4 =
58317569SXinghua.Wen@Sun.COM 		    (temp1 >= 170) ? temp1 : 170;
58327569SXinghua.Wen@Sun.COM 
58337569SXinghua.Wen@Sun.COM 		temp1 = rx_sens_p->auto_corr_ofdm_x1 - 1;
58347569SXinghua.Wen@Sun.COM 		rx_sens_p->auto_corr_ofdm_x1 = (temp1 >= 105) ? temp1 : 105;
58357569SXinghua.Wen@Sun.COM 
58367569SXinghua.Wen@Sun.COM 		temp1 = rx_sens_p->auto_corr_mrc_ofdm_x1 - 1;
58377569SXinghua.Wen@Sun.COM 		rx_sens_p->auto_corr_mrc_ofdm_x1 =
58387569SXinghua.Wen@Sun.COM 		    (temp1 >= 220) ? temp1 : 220;
58397569SXinghua.Wen@Sun.COM 
58407569SXinghua.Wen@Sun.COM 		rx_sens_p->flags |= IWK_SENSITIVITY_OFDM_UPDATE_MSK;
58417569SXinghua.Wen@Sun.COM 
58427569SXinghua.Wen@Sun.COM 	} else {
58437569SXinghua.Wen@Sun.COM 		rx_sens_p->flags &= (~IWK_SENSITIVITY_OFDM_UPDATE_MSK);
58447569SXinghua.Wen@Sun.COM 	}
58457569SXinghua.Wen@Sun.COM 
58467569SXinghua.Wen@Sun.COM 	return (IWK_SUCCESS);
58477569SXinghua.Wen@Sun.COM }
58487569SXinghua.Wen@Sun.COM 
58497569SXinghua.Wen@Sun.COM /*
58508594SFei.Feng@Sun.COM  * additional process to management frames
58518594SFei.Feng@Sun.COM  */
iwk_recv_mgmt(struct ieee80211com * ic,mblk_t * mp,struct ieee80211_node * in,int subtype,int rssi,uint32_t rstamp)58528594SFei.Feng@Sun.COM static void iwk_recv_mgmt(struct ieee80211com *ic, mblk_t *mp,
58538594SFei.Feng@Sun.COM     struct ieee80211_node *in,
58548594SFei.Feng@Sun.COM     int subtype, int rssi, uint32_t rstamp)
58558594SFei.Feng@Sun.COM {
58568594SFei.Feng@Sun.COM 	iwk_sc_t *sc = (iwk_sc_t *)ic;
58578594SFei.Feng@Sun.COM 	struct ieee80211_frame *wh;
58588594SFei.Feng@Sun.COM 	uint8_t index1, index2;
58598594SFei.Feng@Sun.COM 	int err;
58608594SFei.Feng@Sun.COM 
58618594SFei.Feng@Sun.COM 	sc->sc_recv_mgmt(ic, mp, in, subtype, rssi, rstamp);
58628594SFei.Feng@Sun.COM 
58638594SFei.Feng@Sun.COM 	mutex_enter(&sc->sc_glock);
58648594SFei.Feng@Sun.COM 	switch (subtype) {
58658594SFei.Feng@Sun.COM 	case IEEE80211_FC0_SUBTYPE_BEACON:
58668594SFei.Feng@Sun.COM 		if (sc->sc_ibss.ibss_beacon.syncbeacon && in == ic->ic_bss &&
58678594SFei.Feng@Sun.COM 		    ic->ic_state == IEEE80211_S_RUN) {
58688594SFei.Feng@Sun.COM 			if (ieee80211_beacon_update(ic, in,
58698594SFei.Feng@Sun.COM 			    &sc->sc_ibss.ibss_beacon.iwk_boff,
58708594SFei.Feng@Sun.COM 			    sc->sc_ibss.ibss_beacon.mp, 0)) {
58718594SFei.Feng@Sun.COM 				bcopy(sc->sc_ibss.ibss_beacon.mp->b_rptr,
58728594SFei.Feng@Sun.COM 				    sc->sc_ibss.ibss_beacon.beacon_cmd.
58738594SFei.Feng@Sun.COM 				    bcon_frame,
58748594SFei.Feng@Sun.COM 				    MBLKL(sc->sc_ibss.ibss_beacon.mp));
58758594SFei.Feng@Sun.COM 			}
58768594SFei.Feng@Sun.COM 			err = iwk_cmd(sc, REPLY_TX_BEACON,
58778594SFei.Feng@Sun.COM 			    &sc->sc_ibss.ibss_beacon.beacon_cmd,
58788594SFei.Feng@Sun.COM 			    sc->sc_ibss.ibss_beacon.beacon_cmd_len, 1);
58798594SFei.Feng@Sun.COM 			if (err != IWK_SUCCESS) {
58808594SFei.Feng@Sun.COM 				cmn_err(CE_WARN, "iwk_recv_mgmt(): "
58818594SFei.Feng@Sun.COM 				    "failed to TX beacon.\n");
58828594SFei.Feng@Sun.COM 			}
58838594SFei.Feng@Sun.COM 			sc->sc_ibss.ibss_beacon.syncbeacon = 0;
58848594SFei.Feng@Sun.COM 		}
58858594SFei.Feng@Sun.COM 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
58868594SFei.Feng@Sun.COM 		    ic->ic_state == IEEE80211_S_RUN) {
58878594SFei.Feng@Sun.COM 			wh = (struct ieee80211_frame *)mp->b_rptr;
58888594SFei.Feng@Sun.COM 			mutex_enter(&sc->sc_ibss.node_tb_lock);
58898594SFei.Feng@Sun.COM 			/*
58908594SFei.Feng@Sun.COM 			 * search for node in ibss node table
58918594SFei.Feng@Sun.COM 			 */
58928594SFei.Feng@Sun.COM 			for (index1 = IWK_STA_ID; index1 < IWK_STATION_COUNT;
58938594SFei.Feng@Sun.COM 			    index1++) {
58948594SFei.Feng@Sun.COM 				if (sc->sc_ibss.ibss_node_tb[index1].used &&
58958594SFei.Feng@Sun.COM 				    IEEE80211_ADDR_EQ(sc->sc_ibss.
58968594SFei.Feng@Sun.COM 				    ibss_node_tb[index1].node.bssid,
58978594SFei.Feng@Sun.COM 				    wh->i_addr2)) {
58988594SFei.Feng@Sun.COM 					break;
58998594SFei.Feng@Sun.COM 				}
59008594SFei.Feng@Sun.COM 			}
59018594SFei.Feng@Sun.COM 			/*
59028594SFei.Feng@Sun.COM 			 * if don't find in ibss node table
59038594SFei.Feng@Sun.COM 			 */
59048594SFei.Feng@Sun.COM 			if (index1 >= IWK_BROADCAST_ID) {
59058594SFei.Feng@Sun.COM 				err = iwk_clean_add_node_ibss(ic,
59068594SFei.Feng@Sun.COM 				    wh->i_addr2, &index2);
59078594SFei.Feng@Sun.COM 				if (err != IWK_SUCCESS) {
59088594SFei.Feng@Sun.COM 					cmn_err(CE_WARN, "iwk_recv_mgmt(): "
59098594SFei.Feng@Sun.COM 					    "failed to clean all nodes "
59108594SFei.Feng@Sun.COM 					    "and add one node\n");
59118594SFei.Feng@Sun.COM 				}
59128594SFei.Feng@Sun.COM 			}
59138594SFei.Feng@Sun.COM 			mutex_exit(&sc->sc_ibss.node_tb_lock);
59148594SFei.Feng@Sun.COM 		}
59158594SFei.Feng@Sun.COM 		break;
59168594SFei.Feng@Sun.COM 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
59178594SFei.Feng@Sun.COM 		break;
59188594SFei.Feng@Sun.COM 	}
59198594SFei.Feng@Sun.COM 	mutex_exit(&sc->sc_glock);
59208594SFei.Feng@Sun.COM }
59218594SFei.Feng@Sun.COM 
59228594SFei.Feng@Sun.COM /*
59237569SXinghua.Wen@Sun.COM  * 1)  log_event_table_ptr indicates base of the event log.  This traces
59247569SXinghua.Wen@Sun.COM  *     a 256-entry history of uCode execution within a circular buffer.
59257569SXinghua.Wen@Sun.COM  *     Its header format is:
59267569SXinghua.Wen@Sun.COM  *
59277569SXinghua.Wen@Sun.COM  *	uint32_t log_size;	log capacity (in number of entries)
59287569SXinghua.Wen@Sun.COM  *	uint32_t type;	(1) timestamp with each entry, (0) no timestamp
59297569SXinghua.Wen@Sun.COM  *	uint32_t wraps;	# times uCode has wrapped to top of circular buffer
59307569SXinghua.Wen@Sun.COM  *      uint32_t write_index;	next circular buffer entry that uCode would fill
59317569SXinghua.Wen@Sun.COM  *
59327569SXinghua.Wen@Sun.COM  *     The header is followed by the circular buffer of log entries.  Entries
59337569SXinghua.Wen@Sun.COM  *     with timestamps have the following format:
59347569SXinghua.Wen@Sun.COM  *
59357569SXinghua.Wen@Sun.COM  *	uint32_t event_id;     range 0 - 1500
59367569SXinghua.Wen@Sun.COM  *	uint32_t timestamp;    low 32 bits of TSF (of network, if associated)
59377569SXinghua.Wen@Sun.COM  *	uint32_t data;         event_id-specific data value
59387569SXinghua.Wen@Sun.COM  *
59397569SXinghua.Wen@Sun.COM  *     Entries without timestamps contain only event_id and data.
59407569SXinghua.Wen@Sun.COM  */
59417569SXinghua.Wen@Sun.COM 
59427569SXinghua.Wen@Sun.COM /*
59437569SXinghua.Wen@Sun.COM  * iwk_write_event_log - Write event log to dmesg
59447569SXinghua.Wen@Sun.COM  */
iwk_write_event_log(iwk_sc_t * sc)59457569SXinghua.Wen@Sun.COM static void iwk_write_event_log(iwk_sc_t *sc)
59467569SXinghua.Wen@Sun.COM {
59477569SXinghua.Wen@Sun.COM 	uint32_t log_event_table_ptr;	/* Start address of event table */
59487569SXinghua.Wen@Sun.COM 	uint32_t startptr;	/* Start address of log data */
59497569SXinghua.Wen@Sun.COM 	uint32_t logptr;	/* address of log data entry */
59507569SXinghua.Wen@Sun.COM 	uint32_t i, n, num_events;
59517569SXinghua.Wen@Sun.COM 	uint32_t event_id, data1, data2; /* log data */
59527569SXinghua.Wen@Sun.COM 
59537569SXinghua.Wen@Sun.COM 	uint32_t log_size;   /* log capacity (in number of entries) */
59547569SXinghua.Wen@Sun.COM 	uint32_t type;	/* (1)timestamp with each entry,(0) no timestamp */
59557569SXinghua.Wen@Sun.COM 	uint32_t wraps;	/* # times uCode has wrapped to */
59567569SXinghua.Wen@Sun.COM 			/* the top of circular buffer */
59577569SXinghua.Wen@Sun.COM 	uint32_t idx; /* index of entry to be filled in next */
59587569SXinghua.Wen@Sun.COM 
59599327SFei.Feng@Sun.COM 	log_event_table_ptr = LE_32(sc->sc_card_alive_run.log_event_table_ptr);
59607569SXinghua.Wen@Sun.COM 	if (!(log_event_table_ptr)) {
59617569SXinghua.Wen@Sun.COM 		IWK_DBG((IWK_DEBUG_EEPROM, "NULL event table pointer\n"));
59627569SXinghua.Wen@Sun.COM 		return;
59637569SXinghua.Wen@Sun.COM 	}
59647569SXinghua.Wen@Sun.COM 
59657569SXinghua.Wen@Sun.COM 	iwk_mac_access_enter(sc);
59667569SXinghua.Wen@Sun.COM 
59677569SXinghua.Wen@Sun.COM 	/* Read log header */
59687569SXinghua.Wen@Sun.COM 	log_size = iwk_mem_read(sc, log_event_table_ptr);
59697569SXinghua.Wen@Sun.COM 	log_event_table_ptr += sizeof (uint32_t); /* addr of "type" */
59707569SXinghua.Wen@Sun.COM 	type = iwk_mem_read(sc, log_event_table_ptr);
59717569SXinghua.Wen@Sun.COM 	log_event_table_ptr += sizeof (uint32_t); /* addr of "wraps" */
59727569SXinghua.Wen@Sun.COM 	wraps = iwk_mem_read(sc, log_event_table_ptr);
59737569SXinghua.Wen@Sun.COM 	log_event_table_ptr += sizeof (uint32_t); /* addr of "idx" */
59747569SXinghua.Wen@Sun.COM 	idx = iwk_mem_read(sc, log_event_table_ptr);
59757569SXinghua.Wen@Sun.COM 	startptr = log_event_table_ptr +
59767569SXinghua.Wen@Sun.COM 	    sizeof (uint32_t); /* addr of start of log data */
59777569SXinghua.Wen@Sun.COM 	if (!log_size & !wraps) {
59787569SXinghua.Wen@Sun.COM 		IWK_DBG((IWK_DEBUG_EEPROM, "Empty log\n"));
59797569SXinghua.Wen@Sun.COM 		iwk_mac_access_exit(sc);
59807569SXinghua.Wen@Sun.COM 		return;
59817569SXinghua.Wen@Sun.COM 	}
59827569SXinghua.Wen@Sun.COM 
59837569SXinghua.Wen@Sun.COM 	if (!wraps) {
59847569SXinghua.Wen@Sun.COM 		num_events = idx;
59857569SXinghua.Wen@Sun.COM 		logptr = startptr;
59867569SXinghua.Wen@Sun.COM 	} else {
59877569SXinghua.Wen@Sun.COM 		num_events = log_size - idx;
59887569SXinghua.Wen@Sun.COM 		n = type ? 2 : 3;
59897569SXinghua.Wen@Sun.COM 		logptr = startptr + (idx * n * sizeof (uint32_t));
59907569SXinghua.Wen@Sun.COM 	}
59917569SXinghua.Wen@Sun.COM 
59927569SXinghua.Wen@Sun.COM 	for (i = 0; i < num_events; i++) {
59937569SXinghua.Wen@Sun.COM 		event_id = iwk_mem_read(sc, logptr);
59947569SXinghua.Wen@Sun.COM 		logptr += sizeof (uint32_t);
59957569SXinghua.Wen@Sun.COM 		data1 = iwk_mem_read(sc, logptr);
59967569SXinghua.Wen@Sun.COM 		logptr += sizeof (uint32_t);
59977569SXinghua.Wen@Sun.COM 		if (type == 0) { /* no timestamp */
59987569SXinghua.Wen@Sun.COM 			IWK_DBG((IWK_DEBUG_EEPROM, "Event ID=%d, Data=%x0x",
59997569SXinghua.Wen@Sun.COM 			    event_id, data1));
60007569SXinghua.Wen@Sun.COM 		} else { /* timestamp */
60017569SXinghua.Wen@Sun.COM 			data2 = iwk_mem_read(sc, logptr);
60027569SXinghua.Wen@Sun.COM 			IWK_DBG((IWK_DEBUG_EEPROM,
60037569SXinghua.Wen@Sun.COM 			    "Time=%d, Event ID=%d, Data=0x%x\n",
60047569SXinghua.Wen@Sun.COM 			    data1, event_id, data2));
60057569SXinghua.Wen@Sun.COM 			logptr += sizeof (uint32_t);
60067569SXinghua.Wen@Sun.COM 		}
60077569SXinghua.Wen@Sun.COM 	}
60087569SXinghua.Wen@Sun.COM 
60097569SXinghua.Wen@Sun.COM 	/*
60107569SXinghua.Wen@Sun.COM 	 * Print the wrapped around entries, if any
60117569SXinghua.Wen@Sun.COM 	 */
60127569SXinghua.Wen@Sun.COM 	if (wraps) {
60137569SXinghua.Wen@Sun.COM 		logptr = startptr;
60147569SXinghua.Wen@Sun.COM 		for (i = 0; i < idx; i++) {
60157569SXinghua.Wen@Sun.COM 			event_id = iwk_mem_read(sc, logptr);
60167569SXinghua.Wen@Sun.COM 			logptr += sizeof (uint32_t);
60177569SXinghua.Wen@Sun.COM 			data1 = iwk_mem_read(sc, logptr);
60187569SXinghua.Wen@Sun.COM 			logptr += sizeof (uint32_t);
60197569SXinghua.Wen@Sun.COM 			if (type == 0) { /* no timestamp */
60207569SXinghua.Wen@Sun.COM 				IWK_DBG((IWK_DEBUG_EEPROM,
60217569SXinghua.Wen@Sun.COM 				    "Event ID=%d, Data=%x0x", event_id, data1));
60227569SXinghua.Wen@Sun.COM 			} else { /* timestamp */
60237569SXinghua.Wen@Sun.COM 				data2 = iwk_mem_read(sc, logptr);
60247569SXinghua.Wen@Sun.COM 				IWK_DBG((IWK_DEBUG_EEPROM,
60257569SXinghua.Wen@Sun.COM 				    "Time = %d, Event ID=%d, Data=0x%x\n",
60267569SXinghua.Wen@Sun.COM 				    data1, event_id, data2));
60277569SXinghua.Wen@Sun.COM 				logptr += sizeof (uint32_t);
60287569SXinghua.Wen@Sun.COM 			}
60297569SXinghua.Wen@Sun.COM 		}
60307569SXinghua.Wen@Sun.COM 	}
60317569SXinghua.Wen@Sun.COM 
60327569SXinghua.Wen@Sun.COM 	iwk_mac_access_exit(sc);
60337569SXinghua.Wen@Sun.COM }
60347569SXinghua.Wen@Sun.COM 
60357569SXinghua.Wen@Sun.COM /*
60367569SXinghua.Wen@Sun.COM  * error_event_table_ptr indicates base of the error log.  This contains
60377569SXinghua.Wen@Sun.COM  * information about any uCode error that occurs.  For 4965, the format is:
60387569SXinghua.Wen@Sun.COM  *
60397569SXinghua.Wen@Sun.COM  * uint32_t valid;        (nonzero) valid, (0) log is empty
60407569SXinghua.Wen@Sun.COM  * uint32_t error_id;     type of error
60417569SXinghua.Wen@Sun.COM  * uint32_t pc;           program counter
60427569SXinghua.Wen@Sun.COM  * uint32_t blink1;       branch link
60437569SXinghua.Wen@Sun.COM  * uint32_t blink2;       branch link
60447569SXinghua.Wen@Sun.COM  * uint32_t ilink1;       interrupt link
60457569SXinghua.Wen@Sun.COM  * uint32_t ilink2;       interrupt link
60467569SXinghua.Wen@Sun.COM  * uint32_t data1;        error-specific data
60477569SXinghua.Wen@Sun.COM  * uint32_t data2;        error-specific data
60487569SXinghua.Wen@Sun.COM  * uint32_t line;         source code line of error
60497569SXinghua.Wen@Sun.COM  * uint32_t bcon_time;    beacon timer
60507569SXinghua.Wen@Sun.COM  * uint32_t tsf_low;      network timestamp function timer
60517569SXinghua.Wen@Sun.COM  * uint32_t tsf_hi;       network timestamp function timer
60527569SXinghua.Wen@Sun.COM  */
60537569SXinghua.Wen@Sun.COM /*
60547569SXinghua.Wen@Sun.COM  * iwk_write_error_log - Write error log to dmesg
60557569SXinghua.Wen@Sun.COM  */
iwk_write_error_log(iwk_sc_t * sc)60567569SXinghua.Wen@Sun.COM static void iwk_write_error_log(iwk_sc_t *sc)
60577569SXinghua.Wen@Sun.COM {
60587569SXinghua.Wen@Sun.COM 	uint32_t err_ptr;	/* Start address of error log */
60597569SXinghua.Wen@Sun.COM 	uint32_t valid;		/* is error log valid */
60607569SXinghua.Wen@Sun.COM 
60619327SFei.Feng@Sun.COM 	err_ptr = LE_32(sc->sc_card_alive_run.error_event_table_ptr);
60627569SXinghua.Wen@Sun.COM 	if (!(err_ptr)) {
60637569SXinghua.Wen@Sun.COM 		IWK_DBG((IWK_DEBUG_EEPROM, "NULL error table pointer\n"));
60647569SXinghua.Wen@Sun.COM 		return;
60657569SXinghua.Wen@Sun.COM 	}
60667569SXinghua.Wen@Sun.COM 
60677569SXinghua.Wen@Sun.COM 	iwk_mac_access_enter(sc);
60687569SXinghua.Wen@Sun.COM 
60697569SXinghua.Wen@Sun.COM 	valid = iwk_mem_read(sc, err_ptr);
60707569SXinghua.Wen@Sun.COM 	if (!(valid)) {
60717569SXinghua.Wen@Sun.COM 		IWK_DBG((IWK_DEBUG_EEPROM, "Error data not valid\n"));
60727569SXinghua.Wen@Sun.COM 		iwk_mac_access_exit(sc);
60737569SXinghua.Wen@Sun.COM 		return;
60747569SXinghua.Wen@Sun.COM 	}
60757569SXinghua.Wen@Sun.COM 	err_ptr += sizeof (uint32_t);
60767569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_EEPROM, "err=%d ", iwk_mem_read(sc, err_ptr)));
60777569SXinghua.Wen@Sun.COM 	err_ptr += sizeof (uint32_t);
60787569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_EEPROM, "pc=0x%X ", iwk_mem_read(sc, err_ptr)));
60797569SXinghua.Wen@Sun.COM 	err_ptr += sizeof (uint32_t);
60807569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_EEPROM,
60817569SXinghua.Wen@Sun.COM 	    "branch link1=0x%X ", iwk_mem_read(sc, err_ptr)));
60827569SXinghua.Wen@Sun.COM 	err_ptr += sizeof (uint32_t);
60837569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_EEPROM,
60847569SXinghua.Wen@Sun.COM 	    "branch link2=0x%X ", iwk_mem_read(sc, err_ptr)));
60857569SXinghua.Wen@Sun.COM 	err_ptr += sizeof (uint32_t);
60867569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_EEPROM,
60877569SXinghua.Wen@Sun.COM 	    "interrupt link1=0x%X ", iwk_mem_read(sc, err_ptr)));
60887569SXinghua.Wen@Sun.COM 	err_ptr += sizeof (uint32_t);
60897569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_EEPROM,
60907569SXinghua.Wen@Sun.COM 	    "interrupt link2=0x%X ", iwk_mem_read(sc, err_ptr)));
60917569SXinghua.Wen@Sun.COM 	err_ptr += sizeof (uint32_t);
60927569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_EEPROM, "data1=0x%X ", iwk_mem_read(sc, err_ptr)));
60937569SXinghua.Wen@Sun.COM 	err_ptr += sizeof (uint32_t);
60947569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_EEPROM, "data2=0x%X ", iwk_mem_read(sc, err_ptr)));
60957569SXinghua.Wen@Sun.COM 	err_ptr += sizeof (uint32_t);
60967569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_EEPROM, "line=%d ", iwk_mem_read(sc, err_ptr)));
60977569SXinghua.Wen@Sun.COM 	err_ptr += sizeof (uint32_t);
60987569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_EEPROM, "bcon_time=%d ", iwk_mem_read(sc, err_ptr)));
60997569SXinghua.Wen@Sun.COM 	err_ptr += sizeof (uint32_t);
61007569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_EEPROM, "tsf_low=%d ", iwk_mem_read(sc, err_ptr)));
61017569SXinghua.Wen@Sun.COM 	err_ptr += sizeof (uint32_t);
61027569SXinghua.Wen@Sun.COM 	IWK_DBG((IWK_DEBUG_EEPROM, "tsf_hi=%d\n", iwk_mem_read(sc, err_ptr)));
61037569SXinghua.Wen@Sun.COM 
61047569SXinghua.Wen@Sun.COM 	iwk_mac_access_exit(sc);
61057569SXinghua.Wen@Sun.COM }
61068594SFei.Feng@Sun.COM 
61078594SFei.Feng@Sun.COM static int
iwk_run_state_config_ibss(ieee80211com_t * ic)61088594SFei.Feng@Sun.COM iwk_run_state_config_ibss(ieee80211com_t *ic)
61098594SFei.Feng@Sun.COM {
61108594SFei.Feng@Sun.COM 	iwk_sc_t *sc = (iwk_sc_t *)ic;
61118594SFei.Feng@Sun.COM 	ieee80211_node_t *in = ic->ic_bss;
61128594SFei.Feng@Sun.COM 	int i, err = IWK_SUCCESS;
61138594SFei.Feng@Sun.COM 
61148594SFei.Feng@Sun.COM 	mutex_enter(&sc->sc_ibss.node_tb_lock);
61158594SFei.Feng@Sun.COM 
61168594SFei.Feng@Sun.COM 	/*
61178594SFei.Feng@Sun.COM 	 * clean all nodes in ibss node table assure be
61188594SFei.Feng@Sun.COM 	 * consistent with hardware
61198594SFei.Feng@Sun.COM 	 */
61208594SFei.Feng@Sun.COM 	for (i = IWK_STA_ID; i < IWK_STATION_COUNT; i++) {
61218594SFei.Feng@Sun.COM 		sc->sc_ibss.ibss_node_tb[i].used = 0;
61228594SFei.Feng@Sun.COM 		(void) memset(&sc->sc_ibss.ibss_node_tb[i].node,
61238594SFei.Feng@Sun.COM 		    0,
61248594SFei.Feng@Sun.COM 		    sizeof (iwk_add_sta_t));
61258594SFei.Feng@Sun.COM 	}
61268594SFei.Feng@Sun.COM 
61278594SFei.Feng@Sun.COM 	sc->sc_ibss.node_number = 0;
61288594SFei.Feng@Sun.COM 
61298594SFei.Feng@Sun.COM 	mutex_exit(&sc->sc_ibss.node_tb_lock);
61308594SFei.Feng@Sun.COM 
61318594SFei.Feng@Sun.COM 	/*
61328594SFei.Feng@Sun.COM 	 * configure RX and TX
61338594SFei.Feng@Sun.COM 	 */
61348594SFei.Feng@Sun.COM 	sc->sc_config.dev_type = RXON_DEV_TYPE_IBSS;
61358594SFei.Feng@Sun.COM 
61369327SFei.Feng@Sun.COM 	sc->sc_config.flags |= LE_32(RXON_FLG_SHORT_PREAMBLE_MSK);
61378594SFei.Feng@Sun.COM 	sc->sc_config.filter_flags =
61388594SFei.Feng@Sun.COM 	    LE_32(RXON_FILTER_ACCEPT_GRP_MSK |
61398594SFei.Feng@Sun.COM 	    RXON_FILTER_DIS_DECRYPT_MSK |
61408594SFei.Feng@Sun.COM 	    RXON_FILTER_DIS_GRP_DECRYPT_MSK);
61418594SFei.Feng@Sun.COM 
61428594SFei.Feng@Sun.COM 	sc->sc_config.assoc_id = 0;
61438594SFei.Feng@Sun.COM 
61448594SFei.Feng@Sun.COM 	IEEE80211_ADDR_COPY(sc->sc_config.bssid, in->in_bssid);
61459327SFei.Feng@Sun.COM 	sc->sc_config.chan = LE_16(ieee80211_chan2ieee(ic,
61469327SFei.Feng@Sun.COM 	    in->in_chan));
61478594SFei.Feng@Sun.COM 
61488594SFei.Feng@Sun.COM 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
61498594SFei.Feng@Sun.COM 		sc->sc_config.cck_basic_rates = 0x03;
61508594SFei.Feng@Sun.COM 		sc->sc_config.ofdm_basic_rates = 0;
61518594SFei.Feng@Sun.COM 	} else if ((in->in_chan != IEEE80211_CHAN_ANYC) &&
61528594SFei.Feng@Sun.COM 	    (IEEE80211_IS_CHAN_5GHZ(in->in_chan))) {
61538594SFei.Feng@Sun.COM 		sc->sc_config.cck_basic_rates = 0;
61548594SFei.Feng@Sun.COM 		sc->sc_config.ofdm_basic_rates = 0x15;
61558594SFei.Feng@Sun.COM 
61568594SFei.Feng@Sun.COM 	} else {
61578594SFei.Feng@Sun.COM 		sc->sc_config.cck_basic_rates = 0x0f;
61588594SFei.Feng@Sun.COM 		sc->sc_config.ofdm_basic_rates = 0xff;
61598594SFei.Feng@Sun.COM 	}
61608594SFei.Feng@Sun.COM 
61618594SFei.Feng@Sun.COM 	sc->sc_config.flags &=
61628594SFei.Feng@Sun.COM 	    ~LE_32(RXON_FLG_SHORT_PREAMBLE_MSK |
61638594SFei.Feng@Sun.COM 	    RXON_FLG_SHORT_SLOT_MSK);
61648594SFei.Feng@Sun.COM 
61658594SFei.Feng@Sun.COM 	if (ic->ic_flags & IEEE80211_F_SHSLOT) {
61668594SFei.Feng@Sun.COM 		sc->sc_config.flags |=
61678594SFei.Feng@Sun.COM 		    LE_32(RXON_FLG_SHORT_SLOT_MSK);
61688594SFei.Feng@Sun.COM 	}
61698594SFei.Feng@Sun.COM 
61708594SFei.Feng@Sun.COM 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) {
61718594SFei.Feng@Sun.COM 		sc->sc_config.flags |=
61728594SFei.Feng@Sun.COM 		    LE_32(RXON_FLG_SHORT_PREAMBLE_MSK);
61738594SFei.Feng@Sun.COM 	}
61748594SFei.Feng@Sun.COM 
61758594SFei.Feng@Sun.COM 	sc->sc_config.filter_flags |=
61768594SFei.Feng@Sun.COM 	    LE_32(RXON_FILTER_ASSOC_MSK);
61778594SFei.Feng@Sun.COM 
61788594SFei.Feng@Sun.COM 	err = iwk_cmd(sc, REPLY_RXON, &sc->sc_config,
61798594SFei.Feng@Sun.COM 	    sizeof (iwk_rxon_cmd_t), 1);
61808594SFei.Feng@Sun.COM 	if (err != IWK_SUCCESS) {
61818594SFei.Feng@Sun.COM 		cmn_err(CE_WARN, "iwk_run_state_config_ibss(): "
61828594SFei.Feng@Sun.COM 		    "failed to update configuration.\n");
61838594SFei.Feng@Sun.COM 		return (err);
61848594SFei.Feng@Sun.COM 	}
61858594SFei.Feng@Sun.COM 
61868594SFei.Feng@Sun.COM 	return (err);
61878594SFei.Feng@Sun.COM 
61888594SFei.Feng@Sun.COM }
61898594SFei.Feng@Sun.COM 
61908594SFei.Feng@Sun.COM static int
iwk_run_state_config_sta(ieee80211com_t * ic)61918594SFei.Feng@Sun.COM iwk_run_state_config_sta(ieee80211com_t *ic)
61928594SFei.Feng@Sun.COM {
61938594SFei.Feng@Sun.COM 	iwk_sc_t *sc = (iwk_sc_t *)ic;
61948594SFei.Feng@Sun.COM 	ieee80211_node_t *in = ic->ic_bss;
61958594SFei.Feng@Sun.COM 	int err = IWK_SUCCESS;
61968594SFei.Feng@Sun.COM 
61978594SFei.Feng@Sun.COM 	/* update adapter's configuration */
61988594SFei.Feng@Sun.COM 	if (sc->sc_assoc_id != in->in_associd) {
61998594SFei.Feng@Sun.COM 		cmn_err(CE_WARN, "iwk_run_state_config_sta(): "
62008594SFei.Feng@Sun.COM 		    "associate ID mismatch: expected %d, "
62018594SFei.Feng@Sun.COM 		    "got %d\n",
62028594SFei.Feng@Sun.COM 		    in->in_associd, sc->sc_assoc_id);
62038594SFei.Feng@Sun.COM 	}
62049327SFei.Feng@Sun.COM 	sc->sc_config.assoc_id = LE_16(in->in_associd & 0x3fff);
62058594SFei.Feng@Sun.COM 
62068594SFei.Feng@Sun.COM 	/*
62078594SFei.Feng@Sun.COM 	 * short preamble/slot time are
62088594SFei.Feng@Sun.COM 	 * negotiated when associating
62098594SFei.Feng@Sun.COM 	 */
62108594SFei.Feng@Sun.COM 	sc->sc_config.flags &=
62118594SFei.Feng@Sun.COM 	    ~LE_32(RXON_FLG_SHORT_PREAMBLE_MSK |
62128594SFei.Feng@Sun.COM 	    RXON_FLG_SHORT_SLOT_MSK);
62138594SFei.Feng@Sun.COM 
62148594SFei.Feng@Sun.COM 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
62158594SFei.Feng@Sun.COM 		sc->sc_config.flags |=
62168594SFei.Feng@Sun.COM 		    LE_32(RXON_FLG_SHORT_SLOT_MSK);
62178594SFei.Feng@Sun.COM 
62188594SFei.Feng@Sun.COM 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
62198594SFei.Feng@Sun.COM 		sc->sc_config.flags |=
62208594SFei.Feng@Sun.COM 		    LE_32(RXON_FLG_SHORT_PREAMBLE_MSK);
62218594SFei.Feng@Sun.COM 
62228594SFei.Feng@Sun.COM 	sc->sc_config.filter_flags |=
62238594SFei.Feng@Sun.COM 	    LE_32(RXON_FILTER_ASSOC_MSK);
62248594SFei.Feng@Sun.COM 
62258594SFei.Feng@Sun.COM 	if (ic->ic_opmode != IEEE80211_M_STA)
62268594SFei.Feng@Sun.COM 		sc->sc_config.filter_flags |=
62278594SFei.Feng@Sun.COM 		    LE_32(RXON_FILTER_BCON_AWARE_MSK);
62288594SFei.Feng@Sun.COM 
62298594SFei.Feng@Sun.COM 	IWK_DBG((IWK_DEBUG_80211, "config chan %d flags %x"
62308594SFei.Feng@Sun.COM 	    " filter_flags %x\n",
62318594SFei.Feng@Sun.COM 	    sc->sc_config.chan, sc->sc_config.flags,
62328594SFei.Feng@Sun.COM 	    sc->sc_config.filter_flags));
62338594SFei.Feng@Sun.COM 
62348594SFei.Feng@Sun.COM 	err = iwk_cmd(sc, REPLY_RXON, &sc->sc_config,
62358594SFei.Feng@Sun.COM 	    sizeof (iwk_rxon_cmd_t), 1);
62368594SFei.Feng@Sun.COM 	if (err != IWK_SUCCESS) {
62378594SFei.Feng@Sun.COM 		cmn_err(CE_WARN, "iwk_run_state_config_sta(): "
62388594SFei.Feng@Sun.COM 		    "failed to update configuration\n");
62398594SFei.Feng@Sun.COM 		return (err);
62408594SFei.Feng@Sun.COM 	}
62418594SFei.Feng@Sun.COM 
62428594SFei.Feng@Sun.COM 	return (err);
62438594SFei.Feng@Sun.COM }
62448594SFei.Feng@Sun.COM 
62458594SFei.Feng@Sun.COM static int
iwk_fast_recover(iwk_sc_t * sc)62468902SPengcheng.Chen@Sun.COM iwk_fast_recover(iwk_sc_t *sc)
62478902SPengcheng.Chen@Sun.COM {
62488902SPengcheng.Chen@Sun.COM 	ieee80211com_t *ic = &sc->sc_ic;
62498902SPengcheng.Chen@Sun.COM 	int err;
62508902SPengcheng.Chen@Sun.COM 
62518902SPengcheng.Chen@Sun.COM 	mutex_enter(&sc->sc_glock);
62528902SPengcheng.Chen@Sun.COM 
62538902SPengcheng.Chen@Sun.COM 	/* restore runtime configuration */
62548902SPengcheng.Chen@Sun.COM 	bcopy(&sc->sc_config_save, &sc->sc_config,
62558902SPengcheng.Chen@Sun.COM 	    sizeof (sc->sc_config));
62568902SPengcheng.Chen@Sun.COM 
62578902SPengcheng.Chen@Sun.COM 	/* reset state to handle reassociations correctly */
62588902SPengcheng.Chen@Sun.COM 	sc->sc_config.assoc_id = 0;
62598902SPengcheng.Chen@Sun.COM 	sc->sc_config.filter_flags &=
62608902SPengcheng.Chen@Sun.COM 	    ~LE_32(RXON_FILTER_ASSOC_MSK);
62618902SPengcheng.Chen@Sun.COM 
62628902SPengcheng.Chen@Sun.COM 	if ((err = iwk_hw_set_before_auth(sc)) != 0) {
62638902SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "iwk_fast_recover(): "
62648902SPengcheng.Chen@Sun.COM 		    "failed to setup authentication\n");
62658902SPengcheng.Chen@Sun.COM 		mutex_exit(&sc->sc_glock);
62668902SPengcheng.Chen@Sun.COM 		return (err);
62678902SPengcheng.Chen@Sun.COM 	}
62688902SPengcheng.Chen@Sun.COM 
62698902SPengcheng.Chen@Sun.COM 	bcopy(&sc->sc_config_save, &sc->sc_config,
62708902SPengcheng.Chen@Sun.COM 	    sizeof (sc->sc_config));
62718902SPengcheng.Chen@Sun.COM 
62728902SPengcheng.Chen@Sun.COM 	/* update adapter's configuration */
62738902SPengcheng.Chen@Sun.COM 	err = iwk_run_state_config_sta(ic);
62748902SPengcheng.Chen@Sun.COM 	if (err != IWK_SUCCESS) {
62758902SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "iwk_fast_recover(): "
62768902SPengcheng.Chen@Sun.COM 		    "failed to setup association\n");
62778902SPengcheng.Chen@Sun.COM 		mutex_exit(&sc->sc_glock);
62788902SPengcheng.Chen@Sun.COM 		return (err);
62798902SPengcheng.Chen@Sun.COM 	}
62808902SPengcheng.Chen@Sun.COM 
62818902SPengcheng.Chen@Sun.COM 	/* obtain current temperature of chipset */
62828902SPengcheng.Chen@Sun.COM 	sc->sc_tempera = iwk_curr_tempera(sc);
62838902SPengcheng.Chen@Sun.COM 
62848902SPengcheng.Chen@Sun.COM 	/*
62858902SPengcheng.Chen@Sun.COM 	 * make Tx power calibration to determine
62868902SPengcheng.Chen@Sun.COM 	 * the gains of DSP and radio
62878902SPengcheng.Chen@Sun.COM 	 */
62888902SPengcheng.Chen@Sun.COM 	err = iwk_tx_power_calibration(sc);
62898902SPengcheng.Chen@Sun.COM 	if (err) {
62908902SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "iwk_fast_recover(): "
62918902SPengcheng.Chen@Sun.COM 		    "failed to set tx power table\n");
62928902SPengcheng.Chen@Sun.COM 		mutex_exit(&sc->sc_glock);
62938902SPengcheng.Chen@Sun.COM 		return (err);
62948902SPengcheng.Chen@Sun.COM 	}
62958902SPengcheng.Chen@Sun.COM 
62968902SPengcheng.Chen@Sun.COM 	/*
62978902SPengcheng.Chen@Sun.COM 	 * make initialization for Receiver
62988902SPengcheng.Chen@Sun.COM 	 * sensitivity calibration
62998902SPengcheng.Chen@Sun.COM 	 */
63008902SPengcheng.Chen@Sun.COM 	err = iwk_rx_sens_init(sc);
63018902SPengcheng.Chen@Sun.COM 	if (err) {
63028902SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "iwk_fast_recover(): "
63038902SPengcheng.Chen@Sun.COM 		    "failed to init RX sensitivity\n");
63048902SPengcheng.Chen@Sun.COM 		mutex_exit(&sc->sc_glock);
63058902SPengcheng.Chen@Sun.COM 		return (err);
63068902SPengcheng.Chen@Sun.COM 	}
63078902SPengcheng.Chen@Sun.COM 
63088902SPengcheng.Chen@Sun.COM 	/* make initialization for Receiver gain balance */
63098902SPengcheng.Chen@Sun.COM 	err = iwk_rxgain_diff_init(sc);
63108902SPengcheng.Chen@Sun.COM 	if (err) {
63118902SPengcheng.Chen@Sun.COM 		cmn_err(CE_WARN, "iwk_fast_recover(): "
63128902SPengcheng.Chen@Sun.COM 		    "failed to init phy calibration\n");
63138902SPengcheng.Chen@Sun.COM 		mutex_exit(&sc->sc_glock);
63148902SPengcheng.Chen@Sun.COM 		return (err);
63158902SPengcheng.Chen@Sun.COM 
63168902SPengcheng.Chen@Sun.COM 	}
63178902SPengcheng.Chen@Sun.COM 	/* set LED on */
63188902SPengcheng.Chen@Sun.COM 	iwk_set_led(sc, 2, 0, 1);
63198902SPengcheng.Chen@Sun.COM 
63208902SPengcheng.Chen@Sun.COM 	mutex_exit(&sc->sc_glock);
63218902SPengcheng.Chen@Sun.COM 
63228902SPengcheng.Chen@Sun.COM 	/* update keys */
63238902SPengcheng.Chen@Sun.COM 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
63248902SPengcheng.Chen@Sun.COM 		for (int i = 0; i < IEEE80211_KEY_MAX; i++) {
63258902SPengcheng.Chen@Sun.COM 			if (ic->ic_nw_keys[i].wk_keyix == IEEE80211_KEYIX_NONE)
63268902SPengcheng.Chen@Sun.COM 				continue;
63278902SPengcheng.Chen@Sun.COM 			err = iwk_key_set(ic, &ic->ic_nw_keys[i],
63288902SPengcheng.Chen@Sun.COM 			    ic->ic_bss->in_macaddr);
63298902SPengcheng.Chen@Sun.COM 			/* failure */
63308902SPengcheng.Chen@Sun.COM 			if (err == 0) {
63318902SPengcheng.Chen@Sun.COM 				cmn_err(CE_WARN, "iwk_fast_recover(): "
63328902SPengcheng.Chen@Sun.COM 				    "failed to setup hardware keys\n");
63338902SPengcheng.Chen@Sun.COM 				return (IWK_FAIL);
63348902SPengcheng.Chen@Sun.COM 			}
63358902SPengcheng.Chen@Sun.COM 		}
63368902SPengcheng.Chen@Sun.COM 	}
63378902SPengcheng.Chen@Sun.COM 
63388902SPengcheng.Chen@Sun.COM 	sc->sc_flags &= ~IWK_F_HW_ERR_RECOVER;
63398902SPengcheng.Chen@Sun.COM 
63408902SPengcheng.Chen@Sun.COM 	/* start queue */
63418902SPengcheng.Chen@Sun.COM 	IWK_DBG((IWK_DEBUG_FW, "iwk_fast_recover(): resume xmit\n"));
63428902SPengcheng.Chen@Sun.COM 	mac_tx_update(ic->ic_mach);
63438902SPengcheng.Chen@Sun.COM 
63448902SPengcheng.Chen@Sun.COM 
63458902SPengcheng.Chen@Sun.COM 	return (IWK_SUCCESS);
63468902SPengcheng.Chen@Sun.COM }
63478902SPengcheng.Chen@Sun.COM 
63488902SPengcheng.Chen@Sun.COM static int
iwk_start_tx_beacon(ieee80211com_t * ic)63498594SFei.Feng@Sun.COM iwk_start_tx_beacon(ieee80211com_t *ic)
63508594SFei.Feng@Sun.COM {
63518594SFei.Feng@Sun.COM 	iwk_sc_t *sc = (iwk_sc_t *)ic;
63528594SFei.Feng@Sun.COM 	ieee80211_node_t *in = ic->ic_bss;
63538594SFei.Feng@Sun.COM 	int err = IWK_SUCCESS;
63548594SFei.Feng@Sun.COM 	iwk_tx_beacon_cmd_t  *tx_beacon_p;
63558594SFei.Feng@Sun.COM 	uint16_t  masks = 0;
63568594SFei.Feng@Sun.COM 	mblk_t *mp;
63578594SFei.Feng@Sun.COM 	int rate;
63588594SFei.Feng@Sun.COM 
63598594SFei.Feng@Sun.COM 	/*
63608594SFei.Feng@Sun.COM 	 * allocate and transmit beacon frames
63618594SFei.Feng@Sun.COM 	 */
63628594SFei.Feng@Sun.COM 	tx_beacon_p = &sc->sc_ibss.ibss_beacon.beacon_cmd;
63638594SFei.Feng@Sun.COM 
63648594SFei.Feng@Sun.COM 	(void) memset(tx_beacon_p, 0,
63658594SFei.Feng@Sun.COM 	    sizeof (iwk_tx_beacon_cmd_t));
63668594SFei.Feng@Sun.COM 	rate = 0;
63678594SFei.Feng@Sun.COM 	masks = 0;
63688594SFei.Feng@Sun.COM 
63698594SFei.Feng@Sun.COM 	tx_beacon_p->config.sta_id = IWK_BROADCAST_ID;
63708594SFei.Feng@Sun.COM 	tx_beacon_p->config.stop_time.life_time =
63718594SFei.Feng@Sun.COM 	    LE_32(0xffffffff);
63728594SFei.Feng@Sun.COM 
63738594SFei.Feng@Sun.COM 	if (sc->sc_ibss.ibss_beacon.mp != NULL) {
63748594SFei.Feng@Sun.COM 		freemsg(sc->sc_ibss.ibss_beacon.mp);
63758594SFei.Feng@Sun.COM 		sc->sc_ibss.ibss_beacon.mp = NULL;
63768594SFei.Feng@Sun.COM 	}
63778594SFei.Feng@Sun.COM 
63788594SFei.Feng@Sun.COM 	sc->sc_ibss.ibss_beacon.mp =
63798594SFei.Feng@Sun.COM 	    ieee80211_beacon_alloc(ic, in,
63808594SFei.Feng@Sun.COM 	    &sc->sc_ibss.ibss_beacon.iwk_boff);
63818594SFei.Feng@Sun.COM 	if (sc->sc_ibss.ibss_beacon.mp == NULL) {
63828594SFei.Feng@Sun.COM 		cmn_err(CE_WARN, "iwk_start_tx_beacon(): "
63838594SFei.Feng@Sun.COM 		    "failed to get beacon frame.\n");
63848594SFei.Feng@Sun.COM 		return (IWK_FAIL);
63858594SFei.Feng@Sun.COM 	}
63868594SFei.Feng@Sun.COM 
63878594SFei.Feng@Sun.COM 	mp = sc->sc_ibss.ibss_beacon.mp;
63888594SFei.Feng@Sun.COM 
63898594SFei.Feng@Sun.COM 	ASSERT(mp->b_cont == NULL);
63908594SFei.Feng@Sun.COM 
63918594SFei.Feng@Sun.COM 	bcopy(mp->b_rptr, tx_beacon_p->bcon_frame, MBLKL(mp));
63928594SFei.Feng@Sun.COM 
63939327SFei.Feng@Sun.COM 	tx_beacon_p->config.len = LE_16((uint16_t)(MBLKL(mp)));
63948594SFei.Feng@Sun.COM 	sc->sc_ibss.ibss_beacon.beacon_cmd_len =
63958594SFei.Feng@Sun.COM 	    sizeof (iwk_tx_cmd_t) +
63969327SFei.Feng@Sun.COM 	    4 + LE_16(tx_beacon_p->config.len);
63978594SFei.Feng@Sun.COM 
63988594SFei.Feng@Sun.COM 	/*
63998594SFei.Feng@Sun.COM 	 * beacons are sent at 1M
64008594SFei.Feng@Sun.COM 	 */
64018594SFei.Feng@Sun.COM 	rate = in->in_rates.ir_rates[0];
64028594SFei.Feng@Sun.COM 	rate &= IEEE80211_RATE_VAL;
64038594SFei.Feng@Sun.COM 
64048594SFei.Feng@Sun.COM 	if (2 == rate || 4 == rate || 11 == rate ||
64058594SFei.Feng@Sun.COM 	    22 == rate) {
64068594SFei.Feng@Sun.COM 		masks |= RATE_MCS_CCK_MSK;
64078594SFei.Feng@Sun.COM 	}
64088594SFei.Feng@Sun.COM 
64098594SFei.Feng@Sun.COM 	masks |= RATE_MCS_ANT_B_MSK;
64108594SFei.Feng@Sun.COM 
64118594SFei.Feng@Sun.COM 	tx_beacon_p->config.rate.r.rate_n_flags =
64129327SFei.Feng@Sun.COM 	    LE_32(iwk_rate_to_plcp(rate) | masks);
64138594SFei.Feng@Sun.COM 
64148594SFei.Feng@Sun.COM 
64158594SFei.Feng@Sun.COM 	tx_beacon_p->config.tx_flags =
64169327SFei.Feng@Sun.COM 	    LE_32(TX_CMD_FLG_SEQ_CTL_MSK | TX_CMD_FLG_TSF_MSK);
64178594SFei.Feng@Sun.COM 
64188594SFei.Feng@Sun.COM 	if (ic->ic_bss->in_tstamp.tsf != 0) {
64198594SFei.Feng@Sun.COM 		sc->sc_ibss.ibss_beacon.syncbeacon = 1;
64208594SFei.Feng@Sun.COM 	} else {
64218594SFei.Feng@Sun.COM 		if (ieee80211_beacon_update(ic, in,
64228594SFei.Feng@Sun.COM 		    &sc->sc_ibss.ibss_beacon.iwk_boff,
64238594SFei.Feng@Sun.COM 		    mp, 0)) {
64248594SFei.Feng@Sun.COM 			bcopy(mp->b_rptr,
64258594SFei.Feng@Sun.COM 			    tx_beacon_p->bcon_frame,
64268594SFei.Feng@Sun.COM 			    MBLKL(mp));
64278594SFei.Feng@Sun.COM 		}
64288594SFei.Feng@Sun.COM 
64298594SFei.Feng@Sun.COM 		err = iwk_cmd(sc, REPLY_TX_BEACON,
64308594SFei.Feng@Sun.COM 		    tx_beacon_p,
64318594SFei.Feng@Sun.COM 		    sc->sc_ibss.ibss_beacon.beacon_cmd_len,
64328594SFei.Feng@Sun.COM 		    1);
64338594SFei.Feng@Sun.COM 		if (err != IWK_SUCCESS) {
64348594SFei.Feng@Sun.COM 			cmn_err(CE_WARN, "iwk_start_tx_beacon(): "
64358594SFei.Feng@Sun.COM 			    "failed to TX beacon.\n");
64368594SFei.Feng@Sun.COM 			return (err);
64378594SFei.Feng@Sun.COM 		}
64388594SFei.Feng@Sun.COM 
64398594SFei.Feng@Sun.COM 		sc->sc_ibss.ibss_beacon.syncbeacon = 0;
64408594SFei.Feng@Sun.COM 	}
64418594SFei.Feng@Sun.COM 
64428594SFei.Feng@Sun.COM 	return (err);
64438594SFei.Feng@Sun.COM }
64448594SFei.Feng@Sun.COM 
64458594SFei.Feng@Sun.COM static int
iwk_clean_add_node_ibss(struct ieee80211com * ic,uint8_t addr[IEEE80211_ADDR_LEN],uint8_t * index2)64468594SFei.Feng@Sun.COM iwk_clean_add_node_ibss(struct ieee80211com *ic,
64478594SFei.Feng@Sun.COM     uint8_t addr[IEEE80211_ADDR_LEN], uint8_t *index2)
64488594SFei.Feng@Sun.COM {
64498594SFei.Feng@Sun.COM 	iwk_sc_t *sc = (iwk_sc_t *)ic;
64508594SFei.Feng@Sun.COM 	uint8_t	index;
64518594SFei.Feng@Sun.COM 	iwk_add_sta_t bc_node;
64528594SFei.Feng@Sun.COM 	iwk_link_quality_cmd_t bc_link_quality;
64538594SFei.Feng@Sun.COM 	iwk_link_quality_cmd_t link_quality;
64548594SFei.Feng@Sun.COM 	uint16_t  bc_masks = 0;
64558594SFei.Feng@Sun.COM 	uint16_t  masks = 0;
64568594SFei.Feng@Sun.COM 	int i, rate;
64578594SFei.Feng@Sun.COM 	struct ieee80211_rateset rs;
64588594SFei.Feng@Sun.COM 	iwk_ibss_node_t *ibss_node_p;
64598594SFei.Feng@Sun.COM 	int err = IWK_SUCCESS;
64608594SFei.Feng@Sun.COM 
64618594SFei.Feng@Sun.COM 	/*
64628594SFei.Feng@Sun.COM 	 * find a location that is not
64638594SFei.Feng@Sun.COM 	 * used in ibss node table
64648594SFei.Feng@Sun.COM 	 */
64658594SFei.Feng@Sun.COM 	for (index = IWK_STA_ID;
64668594SFei.Feng@Sun.COM 	    index < IWK_STATION_COUNT; index++) {
64678594SFei.Feng@Sun.COM 		if (!sc->sc_ibss.ibss_node_tb[index].used) {
64688594SFei.Feng@Sun.COM 			break;
64698594SFei.Feng@Sun.COM 		}
64708594SFei.Feng@Sun.COM 	}
64718594SFei.Feng@Sun.COM 
64728594SFei.Feng@Sun.COM 	/*
64738594SFei.Feng@Sun.COM 	 * if have too many nodes in hardware, clean up
64748594SFei.Feng@Sun.COM 	 */
64758594SFei.Feng@Sun.COM 	if (index < IWK_BROADCAST_ID &&
64768594SFei.Feng@Sun.COM 	    sc->sc_ibss.node_number >= 25) {
64778594SFei.Feng@Sun.COM 		if (iwk_cmd(sc, REPLY_REMOVE_ALL_STA,
64788594SFei.Feng@Sun.COM 		    NULL, 0, 1) != IWK_SUCCESS) {
64798594SFei.Feng@Sun.COM 			cmn_err(CE_WARN, "iwk_clean_add_node_ibss(): "
64808594SFei.Feng@Sun.COM 			    "failed to remove all nodes in hardware\n");
64818594SFei.Feng@Sun.COM 			return (IWK_FAIL);
64828594SFei.Feng@Sun.COM 		}
64838594SFei.Feng@Sun.COM 
64848594SFei.Feng@Sun.COM 		for (i = IWK_STA_ID; i < IWK_STATION_COUNT; i++) {
64858594SFei.Feng@Sun.COM 			sc->sc_ibss.ibss_node_tb[i].used = 0;
64868594SFei.Feng@Sun.COM 			(void) memset(&sc->sc_ibss.ibss_node_tb[i].node,
64878594SFei.Feng@Sun.COM 			    0, sizeof (iwk_add_sta_t));
64888594SFei.Feng@Sun.COM 		}
64898594SFei.Feng@Sun.COM 
64908594SFei.Feng@Sun.COM 		sc->sc_ibss.node_number = 0;
64918594SFei.Feng@Sun.COM 
64928594SFei.Feng@Sun.COM 		/*
64938594SFei.Feng@Sun.COM 		 * add broadcast node so that we
64948594SFei.Feng@Sun.COM 		 * can send broadcast frame
64958594SFei.Feng@Sun.COM 		 */
64968594SFei.Feng@Sun.COM 		(void) memset(&bc_node, 0, sizeof (bc_node));
64978594SFei.Feng@Sun.COM 		(void) memset(bc_node.bssid, 0xff, 6);
64988594SFei.Feng@Sun.COM 		bc_node.id = IWK_BROADCAST_ID;
64998594SFei.Feng@Sun.COM 
65008594SFei.Feng@Sun.COM 		err = iwk_cmd(sc, REPLY_ADD_STA, &bc_node, sizeof (bc_node), 1);
65018594SFei.Feng@Sun.COM 		if (err != IWK_SUCCESS) {
65028594SFei.Feng@Sun.COM 		cmn_err(CE_WARN, "iwk_clean_add_node_ibss(): "
65038594SFei.Feng@Sun.COM 		    "failed to add broadcast node\n");
65048594SFei.Feng@Sun.COM 		return (err);
65058594SFei.Feng@Sun.COM 		}
65068594SFei.Feng@Sun.COM 
65078594SFei.Feng@Sun.COM 		/* TX_LINK_QUALITY cmd */
65088594SFei.Feng@Sun.COM 		(void) memset(&bc_link_quality, 0, sizeof (bc_link_quality));
65098594SFei.Feng@Sun.COM 		for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
65108594SFei.Feng@Sun.COM 			bc_masks |= RATE_MCS_CCK_MSK;
65118594SFei.Feng@Sun.COM 			bc_masks |= RATE_MCS_ANT_B_MSK;
65128594SFei.Feng@Sun.COM 			bc_masks &= ~RATE_MCS_ANT_A_MSK;
65138594SFei.Feng@Sun.COM 			bc_link_quality.rate_n_flags[i] =
65149327SFei.Feng@Sun.COM 			    LE_32(iwk_rate_to_plcp(2) | bc_masks);
65158594SFei.Feng@Sun.COM 		}
65168594SFei.Feng@Sun.COM 
65178594SFei.Feng@Sun.COM 		bc_link_quality.general_params.single_stream_ant_msk = 2;
65188594SFei.Feng@Sun.COM 		bc_link_quality.general_params.dual_stream_ant_msk = 3;
65198594SFei.Feng@Sun.COM 		bc_link_quality.agg_params.agg_dis_start_th = 3;
65208594SFei.Feng@Sun.COM 		bc_link_quality.agg_params.agg_time_limit = LE_16(4000);
65218594SFei.Feng@Sun.COM 		bc_link_quality.sta_id = IWK_BROADCAST_ID;
65228594SFei.Feng@Sun.COM 
65238594SFei.Feng@Sun.COM 		err = iwk_cmd(sc, REPLY_TX_LINK_QUALITY_CMD,
65248594SFei.Feng@Sun.COM 		    &bc_link_quality, sizeof (bc_link_quality), 1);
65258594SFei.Feng@Sun.COM 		if (err != IWK_SUCCESS) {
65268594SFei.Feng@Sun.COM 			cmn_err(CE_WARN, "iwk_clean_add_node_ibss(): "
65278594SFei.Feng@Sun.COM 			    "failed to config link quality table\n");
65288594SFei.Feng@Sun.COM 			return (err);
65298594SFei.Feng@Sun.COM 		}
65308594SFei.Feng@Sun.COM 	}
65318594SFei.Feng@Sun.COM 
65328594SFei.Feng@Sun.COM 	if (index >= IWK_BROADCAST_ID) {
65338594SFei.Feng@Sun.COM 		cmn_err(CE_WARN, "iwk_clean_add_node_ibss(): "
65348594SFei.Feng@Sun.COM 		    "the count of node in hardware is too much\n");
65358594SFei.Feng@Sun.COM 		return (IWK_FAIL);
65368594SFei.Feng@Sun.COM 	}
65378594SFei.Feng@Sun.COM 
65388594SFei.Feng@Sun.COM 	/*
65398594SFei.Feng@Sun.COM 	 * add a node into hardware
65408594SFei.Feng@Sun.COM 	 */
65418594SFei.Feng@Sun.COM 	ibss_node_p = &sc->sc_ibss.ibss_node_tb[index];
65428594SFei.Feng@Sun.COM 
65438594SFei.Feng@Sun.COM 	ibss_node_p->used = 1;
65448594SFei.Feng@Sun.COM 
65458594SFei.Feng@Sun.COM 	(void) memset(&ibss_node_p->node, 0,
65468594SFei.Feng@Sun.COM 	    sizeof (iwk_add_sta_t));
65478594SFei.Feng@Sun.COM 
65488594SFei.Feng@Sun.COM 	IEEE80211_ADDR_COPY(ibss_node_p->node.bssid, addr);
65498594SFei.Feng@Sun.COM 	ibss_node_p->node.id = index;
65508594SFei.Feng@Sun.COM 	ibss_node_p->node.control = 0;
65518594SFei.Feng@Sun.COM 	ibss_node_p->node.flags = 0;
65528594SFei.Feng@Sun.COM 
65538594SFei.Feng@Sun.COM 	err = iwk_cmd(sc, REPLY_ADD_STA, &ibss_node_p->node,
65548594SFei.Feng@Sun.COM 	    sizeof (iwk_add_sta_t), 1);
65558594SFei.Feng@Sun.COM 	if (err != IWK_SUCCESS) {
65568594SFei.Feng@Sun.COM 		cmn_err(CE_WARN, "iwk_clean_add_node_ibss(): "
65578594SFei.Feng@Sun.COM 		    "failed to add IBSS node\n");
65588594SFei.Feng@Sun.COM 		ibss_node_p->used = 0;
65598594SFei.Feng@Sun.COM 		(void) memset(&ibss_node_p->node, 0,
65608594SFei.Feng@Sun.COM 		    sizeof (iwk_add_sta_t));
65618594SFei.Feng@Sun.COM 		return (err);
65628594SFei.Feng@Sun.COM 	}
65638594SFei.Feng@Sun.COM 
65648594SFei.Feng@Sun.COM 	sc->sc_ibss.node_number++;
65658594SFei.Feng@Sun.COM 
65668594SFei.Feng@Sun.COM 	(void) memset(&link_quality, 0, sizeof (link_quality));
65678594SFei.Feng@Sun.COM 
65688594SFei.Feng@Sun.COM 	rs = ic->ic_sup_rates[ieee80211_chan2mode(ic,
65698594SFei.Feng@Sun.COM 	    ic->ic_curchan)];
65708594SFei.Feng@Sun.COM 
65718594SFei.Feng@Sun.COM 	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
65728594SFei.Feng@Sun.COM 		if (i < rs.ir_nrates) {
65738594SFei.Feng@Sun.COM 			rate = rs.
65748594SFei.Feng@Sun.COM 			    ir_rates[rs.ir_nrates - i];
65758594SFei.Feng@Sun.COM 		} else {
65768594SFei.Feng@Sun.COM 			rate = 2;
65778594SFei.Feng@Sun.COM 		}
65788594SFei.Feng@Sun.COM 
65798594SFei.Feng@Sun.COM 		if (2 == rate || 4 == rate ||
65808594SFei.Feng@Sun.COM 		    11 == rate || 22 == rate) {
65818594SFei.Feng@Sun.COM 			masks |= RATE_MCS_CCK_MSK;
65828594SFei.Feng@Sun.COM 		}
65838594SFei.Feng@Sun.COM 
65848594SFei.Feng@Sun.COM 		masks |= RATE_MCS_ANT_B_MSK;
65858594SFei.Feng@Sun.COM 		masks &= ~RATE_MCS_ANT_A_MSK;
65868594SFei.Feng@Sun.COM 
65878594SFei.Feng@Sun.COM 		link_quality.rate_n_flags[i] =
65889327SFei.Feng@Sun.COM 		    LE_32(iwk_rate_to_plcp(rate) | masks);
65898594SFei.Feng@Sun.COM 	}
65908594SFei.Feng@Sun.COM 
65918594SFei.Feng@Sun.COM 	link_quality.general_params.single_stream_ant_msk = 2;
65928594SFei.Feng@Sun.COM 	link_quality.general_params.dual_stream_ant_msk = 3;
65938594SFei.Feng@Sun.COM 	link_quality.agg_params.agg_dis_start_th = 3;
65948594SFei.Feng@Sun.COM 	link_quality.agg_params.agg_time_limit = LE_16(4000);
65958594SFei.Feng@Sun.COM 	link_quality.sta_id = ibss_node_p->node.id;
65968594SFei.Feng@Sun.COM 
65978594SFei.Feng@Sun.COM 	err = iwk_cmd(sc, REPLY_TX_LINK_QUALITY_CMD,
65988594SFei.Feng@Sun.COM 	    &link_quality, sizeof (link_quality), 1);
65998594SFei.Feng@Sun.COM 	if (err != IWK_SUCCESS) {
66008594SFei.Feng@Sun.COM 		cmn_err(CE_WARN, "iwk_clean_add_node_ibss(): "
66018594SFei.Feng@Sun.COM 		    "failed to set up TX link quality\n");
66028594SFei.Feng@Sun.COM 		ibss_node_p->used = 0;
66038594SFei.Feng@Sun.COM 		(void) memset(ibss_node_p->node.bssid, 0, 6);
66048594SFei.Feng@Sun.COM 		return (err);
66058594SFei.Feng@Sun.COM 	}
66068594SFei.Feng@Sun.COM 
66078594SFei.Feng@Sun.COM 	*index2 = index;
66088594SFei.Feng@Sun.COM 
66098594SFei.Feng@Sun.COM 	return (err);
66108594SFei.Feng@Sun.COM }
6611