blob: 39b6f27e7fd80102f427628e9d2de33bcec852c0 [file] [log] [blame]
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 2009, Intel Corporation
* All rights reserved.
*/
/*
* Copyright (c) 2006
* Copyright (c) 2007
* Damien Bergamini <damien.bergamini@free.fr>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Intel(R) WiFi Link 5100/5300 Driver
*/
#include <sys/types.h>
#include <sys/byteorder.h>
#include <sys/conf.h>
#include <sys/cmn_err.h>
#include <sys/stat.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/strsubr.h>
#include <sys/ethernet.h>
#include <inet/common.h>
#include <inet/nd.h>
#include <inet/mi.h>
#include <sys/note.h>
#include <sys/stream.h>
#include <sys/strsun.h>
#include <sys/modctl.h>
#include <sys/devops.h>
#include <sys/dlpi.h>
#include <sys/mac_provider.h>
#include <sys/mac_wifi.h>
#include <sys/net80211.h>
#include <sys/net80211_proto.h>
#include <sys/net80211_ht.h>
#include <sys/varargs.h>
#include <sys/policy.h>
#include <sys/pci.h>
#include "iwh_calibration.h"
#include "iwh_hw.h"
#include "iwh_eeprom.h"
#include "iwh_var.h"
#include <inet/wifi_ioctl.h>
#ifdef DEBUG
#define IWH_DEBUG_80211 (1 << 0)
#define IWH_DEBUG_CMD (1 << 1)
#define IWH_DEBUG_DMA (1 << 2)
#define IWH_DEBUG_EEPROM (1 << 3)
#define IWH_DEBUG_FW (1 << 4)
#define IWH_DEBUG_HW (1 << 5)
#define IWH_DEBUG_INTR (1 << 6)
#define IWH_DEBUG_MRR (1 << 7)
#define IWH_DEBUG_PIO (1 << 8)
#define IWH_DEBUG_RX (1 << 9)
#define IWH_DEBUG_SCAN (1 << 10)
#define IWH_DEBUG_TX (1 << 11)
#define IWH_DEBUG_RATECTL (1 << 12)
#define IWH_DEBUG_RADIO (1 << 13)
#define IWH_DEBUG_RESUME (1 << 14)
#define IWH_DEBUG_CALIBRATION (1 << 15)
#define IWH_DEBUG_BA (1 << 16)
#define IWH_DEBUG_RXON (1 << 17)
#define IWH_DEBUG_HWRATE (1 << 18)
#define IWH_DEBUG_HTRATE (1 << 19)
#define IWH_DEBUG_QOS (1 << 20)
/*
* if want to see debug message of a given section,
* please set this flag to one of above values
*/
uint32_t iwh_dbg_flags = 0;
#define IWH_DBG(x) \
iwh_dbg x
#else
#define IWH_DBG(x)
#endif
#define MS(v, f) (((v) & f) >> f##_S)
static void *iwh_soft_state_p = NULL;
/*
* ucode will be compiled into driver image
*/
static uint8_t iwh_fw_5000_bin[] = {
#include "fw-iw/fw_5000/iwh_5000.ucode"
};
static uint8_t iwh_fw_5150_bin[] = {
#include "fw-iw/fw_5150/iwh_5150.ucode"
};
/*
* DMA attributes for a shared page
*/
static ddi_dma_attr_t sh_dma_attr = {
DMA_ATTR_V0, /* version of this structure */
0, /* lowest usable address */
0xffffffffU, /* highest usable address */
0xffffffffU, /* maximum DMAable byte count */
0x1000, /* alignment in bytes */
0x1000, /* burst sizes (any?) */
1, /* minimum transfer */
0xffffffffU, /* maximum transfer */
0xffffffffU, /* maximum segment length */
1, /* maximum number of segments */
1, /* granularity */
0, /* flags (reserved) */
};
/*
* DMA attributes for a keep warm DRAM descriptor
*/
static ddi_dma_attr_t kw_dma_attr = {
DMA_ATTR_V0, /* version of this structure */
0, /* lowest usable address */
0xffffffffU, /* highest usable address */
0xffffffffU, /* maximum DMAable byte count */
0x1000, /* alignment in bytes */
0x1000, /* burst sizes (any?) */
1, /* minimum transfer */
0xffffffffU, /* maximum transfer */
0xffffffffU, /* maximum segment length */
1, /* maximum number of segments */
1, /* granularity */
0, /* flags (reserved) */
};
/*
* DMA attributes for a ring descriptor
*/
static ddi_dma_attr_t ring_desc_dma_attr = {
DMA_ATTR_V0, /* version of this structure */
0, /* lowest usable address */
0xffffffffU, /* highest usable address */
0xffffffffU, /* maximum DMAable byte count */
0x100, /* alignment in bytes */
0x100, /* burst sizes (any?) */
1, /* minimum transfer */
0xffffffffU, /* maximum transfer */
0xffffffffU, /* maximum segment length */
1, /* maximum number of segments */
1, /* granularity */
0, /* flags (reserved) */
};
/*
* DMA attributes for a cmd
*/
static ddi_dma_attr_t cmd_dma_attr = {
DMA_ATTR_V0, /* version of this structure */
0, /* lowest usable address */
0xffffffffU, /* highest usable address */
0xffffffffU, /* maximum DMAable byte count */
4, /* alignment in bytes */
0x100, /* burst sizes (any?) */
1, /* minimum transfer */
0xffffffffU, /* maximum transfer */
0xffffffffU, /* maximum segment length */
1, /* maximum number of segments */
1, /* granularity */
0, /* flags (reserved) */
};
/*
* DMA attributes for a rx buffer
*/
static ddi_dma_attr_t rx_buffer_dma_attr = {
DMA_ATTR_V0, /* version of this structure */
0, /* lowest usable address */
0xffffffffU, /* highest usable address */
0xffffffffU, /* maximum DMAable byte count */
0x100, /* alignment in bytes */
0x100, /* burst sizes (any?) */
1, /* minimum transfer */
0xffffffffU, /* maximum transfer */
0xffffffffU, /* maximum segment length */
1, /* maximum number of segments */
1, /* granularity */
0, /* flags (reserved) */
};
/*
* DMA attributes for a tx buffer.
* the maximum number of segments is 4 for the hardware.
* now all the wifi drivers put the whole frame in a single
* descriptor, so we define the maximum number of segments 1,
* just the same as the rx_buffer. we consider leverage the HW
* ability in the future, that is why we don't define rx and tx
* buffer_dma_attr as the same.
*/
static ddi_dma_attr_t tx_buffer_dma_attr = {
DMA_ATTR_V0, /* version of this structure */
0, /* lowest usable address */
0xffffffffU, /* highest usable address */
0xffffffffU, /* maximum DMAable byte count */
4, /* alignment in bytes */
0x100, /* burst sizes (any?) */
1, /* minimum transfer */
0xffffffffU, /* maximum transfer */
0xffffffffU, /* maximum segment length */
1, /* maximum number of segments */
1, /* granularity */
0, /* flags (reserved) */
};
/*
* DMA attributes for text and data part in the firmware
*/
static ddi_dma_attr_t fw_dma_attr = {
DMA_ATTR_V0, /* version of this structure */
0, /* lowest usable address */
0xffffffffU, /* highest usable address */
0x7fffffff, /* maximum DMAable byte count */
0x10, /* alignment in bytes */
0x100, /* burst sizes (any?) */
1, /* minimum transfer */
0xffffffffU, /* maximum transfer */
0xffffffffU, /* maximum segment length */
1, /* maximum number of segments */
1, /* granularity */
0, /* flags (reserved) */
};
/*
* regs access attributes
*/
static ddi_device_acc_attr_t iwh_reg_accattr = {
DDI_DEVICE_ATTR_V0,
DDI_STRUCTURE_LE_ACC,
DDI_STRICTORDER_ACC,
DDI_DEFAULT_ACC
};
/*
* DMA access attributes for descriptor
*/
static ddi_device_acc_attr_t iwh_dma_descattr = {
DDI_DEVICE_ATTR_V0,
DDI_STRUCTURE_LE_ACC,
DDI_STRICTORDER_ACC,
DDI_DEFAULT_ACC
};
/*
* DMA access attributes
*/
static ddi_device_acc_attr_t iwh_dma_accattr = {
DDI_DEVICE_ATTR_V0,
DDI_NEVERSWAP_ACC,
DDI_STRICTORDER_ACC,
DDI_DEFAULT_ACC
};
static int iwh_ring_init(iwh_sc_t *);
static void iwh_ring_free(iwh_sc_t *);
static int iwh_alloc_shared(iwh_sc_t *);
static void iwh_free_shared(iwh_sc_t *);
static int iwh_alloc_kw(iwh_sc_t *);
static void iwh_free_kw(iwh_sc_t *);
static int iwh_alloc_fw_dma(iwh_sc_t *);
static void iwh_free_fw_dma(iwh_sc_t *);
static int iwh_alloc_rx_ring(iwh_sc_t *);
static void iwh_reset_rx_ring(iwh_sc_t *);
static void iwh_free_rx_ring(iwh_sc_t *);
static int iwh_alloc_tx_ring(iwh_sc_t *, iwh_tx_ring_t *,
int, int);
static void iwh_reset_tx_ring(iwh_sc_t *, iwh_tx_ring_t *);
static void iwh_free_tx_ring(iwh_tx_ring_t *);
static ieee80211_node_t *iwh_node_alloc(ieee80211com_t *);
static void iwh_node_free(ieee80211_node_t *);
static int iwh_newstate(ieee80211com_t *, enum ieee80211_state, int);
static void iwh_mac_access_enter(iwh_sc_t *);
static void iwh_mac_access_exit(iwh_sc_t *);
static uint32_t iwh_reg_read(iwh_sc_t *, uint32_t);
static void iwh_reg_write(iwh_sc_t *, uint32_t, uint32_t);
static int iwh_load_init_firmware(iwh_sc_t *);
static int iwh_load_run_firmware(iwh_sc_t *);
static void iwh_tx_intr(iwh_sc_t *, iwh_rx_desc_t *);
static void iwh_cmd_intr(iwh_sc_t *, iwh_rx_desc_t *);
static uint_t iwh_intr(caddr_t, caddr_t);
static int iwh_eep_load(iwh_sc_t *);
static void iwh_get_mac_from_eep(iwh_sc_t *);
static int iwh_eep_sem_down(iwh_sc_t *);
static void iwh_eep_sem_up(iwh_sc_t *);
static uint_t iwh_rx_softintr(caddr_t, caddr_t);
static uint8_t iwh_rate_to_plcp(int);
static int iwh_cmd(iwh_sc_t *, int, const void *, int, int);
static void iwh_set_led(iwh_sc_t *, uint8_t, uint8_t, uint8_t);
static int iwh_hw_set_before_auth(iwh_sc_t *);
static int iwh_scan(iwh_sc_t *);
static int iwh_config(iwh_sc_t *);
static void iwh_stop_master(iwh_sc_t *);
static int iwh_power_up(iwh_sc_t *);
static int iwh_preinit(iwh_sc_t *);
static int iwh_init(iwh_sc_t *);
static void iwh_stop(iwh_sc_t *);
static int iwh_quiesce(dev_info_t *t);
static void iwh_amrr_init(iwh_amrr_t *);
static void iwh_amrr_timeout(iwh_sc_t *);
static void iwh_amrr_ratectl(void *, ieee80211_node_t *);
static void iwh_ucode_alive(iwh_sc_t *, iwh_rx_desc_t *);
static void iwh_rx_phy_intr(iwh_sc_t *, iwh_rx_desc_t *);
static void iwh_rx_mpdu_intr(iwh_sc_t *, iwh_rx_desc_t *);
static void iwh_release_calib_buffer(iwh_sc_t *);
static int iwh_init_common(iwh_sc_t *);
static uint8_t *iwh_eep_addr_trans(iwh_sc_t *, uint32_t);
static int iwh_put_seg_fw(iwh_sc_t *, uint32_t, uint32_t, uint32_t);
static int iwh_alive_common(iwh_sc_t *);
static void iwh_save_calib_result(iwh_sc_t *, iwh_rx_desc_t *);
static int iwh_tx_power_table(iwh_sc_t *, int);
static int iwh_attach(dev_info_t *, ddi_attach_cmd_t);
static int iwh_detach(dev_info_t *, ddi_detach_cmd_t);
static void iwh_destroy_locks(iwh_sc_t *);
static int iwh_send(ieee80211com_t *, mblk_t *, uint8_t);
static void iwh_thread(iwh_sc_t *);
static int iwh_run_state_config(iwh_sc_t *);
static int iwh_fast_recover(iwh_sc_t *);
static int iwh_wme_update(ieee80211com_t *);
static int iwh_qosparam_to_hw(iwh_sc_t *, int);
static int iwh_wme_to_qos_ac(int);
static uint16_t iwh_cw_e_to_cw(uint8_t);
static int iwh_wmeparam_check(struct wmeParams *);
static inline int iwh_wme_tid_qos_ac(int);
static inline int iwh_qos_ac_to_txq(int);
static int iwh_wme_tid_to_txq(int);
static void iwh_init_ht_conf(iwh_sc_t *);
static void iwh_overwrite_11n_rateset(iwh_sc_t *);
static void iwh_overwrite_ic_default(iwh_sc_t *);
static void iwh_config_rxon_chain(iwh_sc_t *);
static int iwh_add_ap_sta(iwh_sc_t *);
static int iwh_ap_lq(iwh_sc_t *);
static void iwh_recv_action(struct ieee80211_node *,
const uint8_t *, const uint8_t *);
static int iwh_send_action(struct ieee80211_node *,
int, int, uint16_t[4]);
static int iwh_is_max_rate(ieee80211_node_t *);
static int iwh_is_min_rate(ieee80211_node_t *);
static void iwh_increase_rate(ieee80211_node_t *);
static void iwh_decrease_rate(ieee80211_node_t *);
static int iwh_alloc_dma_mem(iwh_sc_t *, size_t,
ddi_dma_attr_t *, ddi_device_acc_attr_t *,
uint_t, iwh_dma_t *);
static void iwh_free_dma_mem(iwh_dma_t *);
static int iwh_reset_hw(iwh_sc_t *);
/*
* GLD specific operations
*/
static int iwh_m_stat(void *, uint_t, uint64_t *);
static int iwh_m_start(void *);
static void iwh_m_stop(void *);
static int iwh_m_unicst(void *, const uint8_t *);
static int iwh_m_multicst(void *, boolean_t, const uint8_t *);
static int iwh_m_promisc(void *, boolean_t);
static mblk_t *iwh_m_tx(void *, mblk_t *);
static void iwh_m_ioctl(void *, queue_t *, mblk_t *);
static int iwh_m_setprop(void *arg, const char *pr_name,
mac_prop_id_t wldp_pr_num, uint_t wldp_length, const void *wldp_buf);
static int iwh_m_getprop(void *arg, const char *pr_name,
mac_prop_id_t wldp_pr_num, uint_t wldp_length,
void *wldp_buf);
static void iwh_m_propinfo(void *arg, const char *pr_name,
mac_prop_id_t wldp_pr_num, mac_prop_info_handle_t mph);
/*
* Supported rates for 802.11b/g modes (in 500Kbps unit).
*/
static const struct ieee80211_rateset iwh_rateset_11b =
{ 4, { 2, 4, 11, 22 } };
static const struct ieee80211_rateset iwh_rateset_11g =
{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
/*
* Default 11n reates supported by this station.
*/
extern struct ieee80211_htrateset ieee80211_rateset_11n;
/*
* For mfthread only
*/
extern pri_t minclsyspri;
#define DRV_NAME_SP "iwh"
/*
* Module Loading Data & Entry Points
*/
DDI_DEFINE_STREAM_OPS(iwh_devops, nulldev, nulldev, iwh_attach,
iwh_detach, nodev, NULL, D_MP, NULL, iwh_quiesce);
static struct modldrv iwh_modldrv = {
&mod_driverops,
"Intel(R) ShirleyPeak/EchoPeak driver(N)",
&iwh_devops
};
static struct modlinkage iwh_modlinkage = {
MODREV_1,
&iwh_modldrv,
NULL
};
int
_init(void)
{
int status;
status = ddi_soft_state_init(&iwh_soft_state_p,
sizeof (iwh_sc_t), 1);
if (status != DDI_SUCCESS) {
return (status);
}
mac_init_ops(&iwh_devops, DRV_NAME_SP);
status = mod_install(&iwh_modlinkage);
if (status != DDI_SUCCESS) {
mac_fini_ops(&iwh_devops);
ddi_soft_state_fini(&iwh_soft_state_p);
}
return (status);
}
int
_fini(void)
{
int status;
status = mod_remove(&iwh_modlinkage);
if (DDI_SUCCESS == status) {
mac_fini_ops(&iwh_devops);
ddi_soft_state_fini(&iwh_soft_state_p);
}
return (status);
}
int
_info(struct modinfo *mip)
{
return (mod_info(&iwh_modlinkage, mip));
}
/*
* Mac Call Back entries
*/
mac_callbacks_t iwh_m_callbacks = {
MC_IOCTL | MC_SETPROP | MC_GETPROP | MC_PROPINFO,
iwh_m_stat,
iwh_m_start,
iwh_m_stop,
iwh_m_promisc,
iwh_m_multicst,
iwh_m_unicst,
iwh_m_tx,
NULL,
iwh_m_ioctl,
NULL,
NULL,
NULL,
iwh_m_setprop,
iwh_m_getprop,
iwh_m_propinfo
};
#ifdef DEBUG
void
iwh_dbg(uint32_t flags, const char *fmt, ...)
{
va_list ap;
if (flags & iwh_dbg_flags) {
va_start(ap, fmt);
vcmn_err(CE_NOTE, fmt, ap);
va_end(ap);
}
}
#endif /* DEBUG */
/*
* device operations
*/
int
iwh_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
iwh_sc_t *sc;
ieee80211com_t *ic;
int instance, i;
char strbuf[32];
wifi_data_t wd = { 0 };
mac_register_t *macp;
int intr_type;
int intr_count;
int intr_actual;
int err = DDI_FAILURE;
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
instance = ddi_get_instance(dip);
sc = ddi_get_soft_state(iwh_soft_state_p,
instance);
ASSERT(sc != NULL);
if (sc->sc_flags & IWH_F_RUNNING) {
(void) iwh_init(sc);
}
atomic_and_32(&sc->sc_flags, ~IWH_F_SUSPEND);
IWH_DBG((IWH_DEBUG_RESUME, "iwh_attach(): "
"resume\n"));
return (DDI_SUCCESS);
default:
goto attach_fail1;
}
instance = ddi_get_instance(dip);
err = ddi_soft_state_zalloc(iwh_soft_state_p, instance);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"failed to allocate soft state\n");
goto attach_fail1;
}
sc = ddi_get_soft_state(iwh_soft_state_p, instance);
ASSERT(sc != NULL);
sc->sc_dip = dip;
/*
* map configure space
*/
err = ddi_regs_map_setup(dip, 0, &sc->sc_cfg_base, 0, 0,
&iwh_reg_accattr, &sc->sc_cfg_handle);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"failed to map config spaces regs\n");
goto attach_fail2;
}
sc->sc_dev_id = ddi_get16(sc->sc_cfg_handle,
(uint16_t *)(sc->sc_cfg_base + PCI_CONF_DEVID));
if ((sc->sc_dev_id != 0x4232) &&
(sc->sc_dev_id != 0x4235) &&
(sc->sc_dev_id != 0x4236) &&
(sc->sc_dev_id != 0x4237) &&
(sc->sc_dev_id != 0x423a) &&
(sc->sc_dev_id != 0x423b) &&
(sc->sc_dev_id != 0x423c) &&
(sc->sc_dev_id != 0x423d)) {
cmn_err(CE_WARN, "iwh_attach(): "
"Do not support this device\n");
goto attach_fail3;
}
iwh_init_ht_conf(sc);
iwh_overwrite_11n_rateset(sc);
sc->sc_rev = ddi_get8(sc->sc_cfg_handle,
(uint8_t *)(sc->sc_cfg_base + PCI_CONF_REVID));
/*
* keep from disturbing C3 state of CPU
*/
ddi_put8(sc->sc_cfg_handle, (uint8_t *)(sc->sc_cfg_base +
PCI_CFG_RETRY_TIMEOUT), 0);
/*
* determine the size of buffer for frame and command to ucode
*/
sc->sc_clsz = ddi_get16(sc->sc_cfg_handle,
(uint16_t *)(sc->sc_cfg_base + PCI_CONF_CACHE_LINESZ));
if (!sc->sc_clsz) {
sc->sc_clsz = 16;
}
sc->sc_clsz = (sc->sc_clsz << 2);
sc->sc_dmabuf_sz = roundup(0x2000 + sizeof (struct ieee80211_frame) +
IEEE80211_MTU + IEEE80211_CRC_LEN +
(IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
IEEE80211_WEP_CRCLEN), sc->sc_clsz);
/*
* Map operating registers
*/
err = ddi_regs_map_setup(dip, 1, &sc->sc_base,
0, 0, &iwh_reg_accattr, &sc->sc_handle);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"failed to map device regs\n");
goto attach_fail3;
}
/*
* this is used to differentiate type of hardware
*/
sc->sc_hw_rev = IWH_READ(sc, CSR_HW_REV);
err = ddi_intr_get_supported_types(dip, &intr_type);
if ((err != DDI_SUCCESS) || (!(intr_type & DDI_INTR_TYPE_FIXED))) {
cmn_err(CE_WARN, "iwh_attach(): "
"fixed type interrupt is not supported\n");
goto attach_fail4;
}
err = ddi_intr_get_nintrs(dip, DDI_INTR_TYPE_FIXED, &intr_count);
if ((err != DDI_SUCCESS) || (intr_count != 1)) {
cmn_err(CE_WARN, "iwh_attach(): "
"no fixed interrupts\n");
goto attach_fail4;
}
sc->sc_intr_htable = kmem_zalloc(sizeof (ddi_intr_handle_t), KM_SLEEP);
err = ddi_intr_alloc(dip, sc->sc_intr_htable, DDI_INTR_TYPE_FIXED, 0,
intr_count, &intr_actual, 0);
if ((err != DDI_SUCCESS) || (intr_actual != 1)) {
cmn_err(CE_WARN, "iwh_attach(): "
"ddi_intr_alloc() failed 0x%x\n", err);
goto attach_fail5;
}
err = ddi_intr_get_pri(sc->sc_intr_htable[0], &sc->sc_intr_pri);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"ddi_intr_get_pri() failed 0x%x\n", err);
goto attach_fail6;
}
mutex_init(&sc->sc_glock, NULL, MUTEX_DRIVER,
DDI_INTR_PRI(sc->sc_intr_pri));
mutex_init(&sc->sc_tx_lock, NULL, MUTEX_DRIVER,
DDI_INTR_PRI(sc->sc_intr_pri));
mutex_init(&sc->sc_mt_lock, NULL, MUTEX_DRIVER,
DDI_INTR_PRI(sc->sc_intr_pri));
cv_init(&sc->sc_cmd_cv, NULL, CV_DRIVER, NULL);
cv_init(&sc->sc_put_seg_cv, NULL, CV_DRIVER, NULL);
cv_init(&sc->sc_ucode_cv, NULL, CV_DRIVER, NULL);
/*
* initialize the mfthread
*/
cv_init(&sc->sc_mt_cv, NULL, CV_DRIVER, NULL);
sc->sc_mf_thread = NULL;
sc->sc_mf_thread_switch = 0;
/*
* Allocate shared buffer for communication between driver and ucode.
*/
err = iwh_alloc_shared(sc);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"failed to allocate shared page\n");
goto attach_fail7;
}
(void) memset(sc->sc_shared, 0, sizeof (iwh_shared_t));
/*
* Allocate keep warm page.
*/
err = iwh_alloc_kw(sc);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"failed to allocate keep warm page\n");
goto attach_fail8;
}
err = iwh_reset_hw(sc);
if (err != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"failed to reset hardware\n");
goto attach_fail9;
}
/*
* Do some necessary hardware initializations.
*/
err = iwh_preinit(sc);
if (err != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"failed to initialize hardware\n");
goto attach_fail9;
}
/*
* get hardware configurations from eeprom
*/
err = iwh_eep_load(sc);
if (err != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"failed to load eeprom\n");
goto attach_fail9;
}
if (IWH_READ_EEP_SHORT(sc, EEP_VERSION) < 0x011a) {
IWH_DBG((IWH_DEBUG_EEPROM, "iwh_attach(): "
"unsupported eeprom detected\n"));
goto attach_fail9;
}
/*
* get MAC address of this chipset
*/
iwh_get_mac_from_eep(sc);
/*
* calibration information from EEPROM
*/
sc->sc_eep_calib = (struct iwh_eep_calibration *)
iwh_eep_addr_trans(sc, EEP_CALIBRATION);
/*
* initialize TX and RX ring buffers
*/
err = iwh_ring_init(sc);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"failed to allocate and initialize ring\n");
goto attach_fail9;
}
if ((0x423c == sc->sc_dev_id) || (0x423d == sc->sc_dev_id)) {
sc->sc_hdr = (iwh_firmware_hdr_t *)iwh_fw_5150_bin;
} else {
sc->sc_hdr = (iwh_firmware_hdr_t *)iwh_fw_5000_bin;
}
/*
* copy ucode to dma buffer
*/
err = iwh_alloc_fw_dma(sc);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"failed to allocate firmware dma\n");
goto attach_fail10;
}
/*
* Initialize the wifi part, which will be used by
* 802.11 module
*/
ic = &sc->sc_ic;
ic->ic_phytype = IEEE80211_T_HT;
ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
ic->ic_state = IEEE80211_S_INIT;
ic->ic_maxrssi = 100; /* experimental number */
ic->ic_caps = IEEE80211_C_SHPREAMBLE | IEEE80211_C_TXPMGT |
IEEE80211_C_PMGT | IEEE80211_C_SHSLOT;
/*
* Support WPA/WPA2
*/
ic->ic_caps |= IEEE80211_C_WPA;
/*
* Support QoS/WME
*/
ic->ic_caps |= IEEE80211_C_WME;
ic->ic_wme.wme_update = iwh_wme_update;
/*
* Support 802.11n/HT
*/
if (sc->sc_ht_conf.ht_support) {
ic->ic_htcaps = IEEE80211_HTC_HT |
IEEE80211_HTC_AMSDU;
ic->ic_htcaps |= IEEE80211_HTCAP_MAXAMSDU_7935;
}
/*
* set supported .11b and .11g rates
*/
ic->ic_sup_rates[IEEE80211_MODE_11B] = iwh_rateset_11b;
ic->ic_sup_rates[IEEE80211_MODE_11G] = iwh_rateset_11g;
/*
* set supported .11b and .11g channels (1 through 11)
*/
for (i = 1; i <= 11; i++) {
ic->ic_sup_channels[i].ich_freq =
ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
ic->ic_sup_channels[i].ich_flags =
IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ |
IEEE80211_CHAN_PASSIVE;
if (sc->sc_ht_conf.cap & HT_CAP_SUP_WIDTH) {
ic->ic_sup_channels[i].ich_flags |=
IEEE80211_CHAN_HT40;
} else {
ic->ic_sup_channels[i].ich_flags |=
IEEE80211_CHAN_HT20;
}
}
ic->ic_ibss_chan = &ic->ic_sup_channels[0];
ic->ic_xmit = iwh_send;
/*
* attach to 802.11 module
*/
ieee80211_attach(ic);
/*
* different instance has different WPA door
*/
(void) snprintf(ic->ic_wpadoor, MAX_IEEE80211STR, "%s_%s%d", WPA_DOOR,
ddi_driver_name(dip),
ddi_get_instance(dip));
/*
* Overwrite 80211 default configurations.
*/
iwh_overwrite_ic_default(sc);
/*
* initialize 802.11 module
*/
ieee80211_media_init(ic);
/*
* initialize default tx key
*/
ic->ic_def_txkey = 0;
err = ddi_intr_add_softint(dip, &sc->sc_soft_hdl, DDI_INTR_SOFTPRI_MAX,
iwh_rx_softintr, (caddr_t)sc);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"add soft interrupt failed\n");
goto attach_fail12;
}
err = ddi_intr_add_handler(sc->sc_intr_htable[0], iwh_intr,
(caddr_t)sc, NULL);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"ddi_intr_add_handle() failed\n");
goto attach_fail13;
}
err = ddi_intr_enable(sc->sc_intr_htable[0]);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"ddi_intr_enable() failed\n");
goto attach_fail14;
}
/*
* Initialize pointer to device specific functions
*/
wd.wd_secalloc = WIFI_SEC_NONE;
wd.wd_opmode = ic->ic_opmode;
IEEE80211_ADDR_COPY(wd.wd_bssid, ic->ic_macaddr);
/*
* create relation to GLD
*/
macp = mac_alloc(MAC_VERSION);
if (NULL == macp) {
cmn_err(CE_WARN, "iwh_attach(): "
"failed to do mac_alloc()\n");
goto attach_fail15;
}
macp->m_type_ident = MAC_PLUGIN_IDENT_WIFI;
macp->m_driver = sc;
macp->m_dip = dip;
macp->m_src_addr = ic->ic_macaddr;
macp->m_callbacks = &iwh_m_callbacks;
macp->m_min_sdu = 0;
macp->m_max_sdu = IEEE80211_MTU;
macp->m_pdata = &wd;
macp->m_pdata_size = sizeof (wd);
/*
* Register the macp to mac
*/
err = mac_register(macp, &ic->ic_mach);
mac_free(macp);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"failed to do mac_register()\n");
goto attach_fail15;
}
/*
* Create minor node of type DDI_NT_NET_WIFI
*/
(void) snprintf(strbuf, sizeof (strbuf), DRV_NAME_SP"%d", instance);
err = ddi_create_minor_node(dip, strbuf, S_IFCHR,
instance + 1, DDI_NT_NET_WIFI, 0);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_attach(): "
"failed to do ddi_create_minor_node()\n");
}
/*
* Notify link is down now
*/
mac_link_update(ic->ic_mach, LINK_STATE_DOWN);
/*
* create the mf thread to handle the link status,
* recovery fatal error, etc.
*/
sc->sc_mf_thread_switch = 1;
if (NULL == sc->sc_mf_thread) {
sc->sc_mf_thread = thread_create((caddr_t)NULL, 0,
iwh_thread, sc, 0, &p0, TS_RUN, minclsyspri);
}
atomic_or_32(&sc->sc_flags, IWH_F_ATTACHED);
return (DDI_SUCCESS);
attach_fail15:
(void) ddi_intr_disable(sc->sc_intr_htable[0]);
attach_fail14:
(void) ddi_intr_remove_handler(sc->sc_intr_htable[0]);
attach_fail13:
(void) ddi_intr_remove_softint(sc->sc_soft_hdl);
sc->sc_soft_hdl = NULL;
attach_fail12:
ieee80211_detach(ic);
attach_fail11:
iwh_free_fw_dma(sc);
attach_fail10:
iwh_ring_free(sc);
attach_fail9:
iwh_free_kw(sc);
attach_fail8:
iwh_free_shared(sc);
attach_fail7:
iwh_destroy_locks(sc);
attach_fail6:
(void) ddi_intr_free(sc->sc_intr_htable[0]);
attach_fail5:
kmem_free(sc->sc_intr_htable, sizeof (ddi_intr_handle_t));
attach_fail4:
ddi_regs_map_free(&sc->sc_handle);
attach_fail3:
ddi_regs_map_free(&sc->sc_cfg_handle);
attach_fail2:
ddi_soft_state_free(iwh_soft_state_p, instance);
attach_fail1:
return (DDI_FAILURE);
}
int
iwh_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
iwh_sc_t *sc;
ieee80211com_t *ic;
int err;
sc = ddi_get_soft_state(iwh_soft_state_p, ddi_get_instance(dip));
ASSERT(sc != NULL);
ic = &sc->sc_ic;
switch (cmd) {
case DDI_DETACH:
break;
case DDI_SUSPEND:
atomic_and_32(&sc->sc_flags, ~IWH_F_HW_ERR_RECOVER);
atomic_and_32(&sc->sc_flags, ~IWH_F_RATE_AUTO_CTL);
atomic_or_32(&sc->sc_flags, IWH_F_SUSPEND);
if (sc->sc_flags & IWH_F_RUNNING) {
ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
iwh_stop(sc);
}
IWH_DBG((IWH_DEBUG_RESUME, "iwh_detach(): "
"suspend\n"));
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
if (!(sc->sc_flags & IWH_F_ATTACHED)) {
return (DDI_FAILURE);
}
/*
* Destroy the mf_thread
*/
sc->sc_mf_thread_switch = 0;
mutex_enter(&sc->sc_mt_lock);
while (sc->sc_mf_thread != NULL) {
if (cv_wait_sig(&sc->sc_mt_cv, &sc->sc_mt_lock) == 0) {
break;
}
}
mutex_exit(&sc->sc_mt_lock);
err = mac_disable(sc->sc_ic.ic_mach);
if (err != DDI_SUCCESS) {
return (err);
}
/*
* stop chipset
*/
iwh_stop(sc);
DELAY(500000);
/*
* release buffer for calibration
*/
iwh_release_calib_buffer(sc);
/*
* Unregiste from GLD
*/
(void) mac_unregister(sc->sc_ic.ic_mach);
mutex_enter(&sc->sc_glock);
iwh_free_fw_dma(sc);
iwh_ring_free(sc);
iwh_free_kw(sc);
iwh_free_shared(sc);
mutex_exit(&sc->sc_glock);
(void) ddi_intr_disable(sc->sc_intr_htable[0]);
(void) ddi_intr_remove_handler(sc->sc_intr_htable[0]);
(void) ddi_intr_free(sc->sc_intr_htable[0]);
kmem_free(sc->sc_intr_htable, sizeof (ddi_intr_handle_t));
(void) ddi_intr_remove_softint(sc->sc_soft_hdl);
sc->sc_soft_hdl = NULL;
/*
* detach from 80211 module
*/
ieee80211_detach(&sc->sc_ic);
iwh_destroy_locks(sc);
ddi_regs_map_free(&sc->sc_handle);
ddi_regs_map_free(&sc->sc_cfg_handle);
ddi_remove_minor_node(dip, NULL);
ddi_soft_state_free(iwh_soft_state_p, ddi_get_instance(dip));
return (DDI_SUCCESS);
}
/*
* destroy all locks
*/
static void
iwh_destroy_locks(iwh_sc_t *sc)
{
cv_destroy(&sc->sc_mt_cv);
cv_destroy(&sc->sc_cmd_cv);
cv_destroy(&sc->sc_put_seg_cv);
cv_destroy(&sc->sc_ucode_cv);
mutex_destroy(&sc->sc_mt_lock);
mutex_destroy(&sc->sc_tx_lock);
mutex_destroy(&sc->sc_glock);
}
/*
* Allocate an area of memory and a DMA handle for accessing it
*/
static int
iwh_alloc_dma_mem(iwh_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, iwh_dma_t *dma_p)
{
caddr_t vaddr;
int err = DDI_FAILURE;
/*
* Allocate handle
*/
err = ddi_dma_alloc_handle(sc->sc_dip, dma_attr_p,
DDI_DMA_SLEEP, NULL, &dma_p->dma_hdl);
if (err != DDI_SUCCESS) {
dma_p->dma_hdl = NULL;
return (DDI_FAILURE);
}
/*
* Allocate memory
*/
err = ddi_dma_mem_alloc(dma_p->dma_hdl, memsize, acc_attr_p,
dma_flags & (DDI_DMA_CONSISTENT | DDI_DMA_STREAMING),
DDI_DMA_SLEEP, NULL, &vaddr, &dma_p->alength, &dma_p->acc_hdl);
if (err != DDI_SUCCESS) {
ddi_dma_free_handle(&dma_p->dma_hdl);
dma_p->dma_hdl = NULL;
dma_p->acc_hdl = NULL;
return (DDI_FAILURE);
}
/*
* Bind the two together
*/
dma_p->mem_va = vaddr;
err = ddi_dma_addr_bind_handle(dma_p->dma_hdl, NULL,
vaddr, dma_p->alength, dma_flags, DDI_DMA_SLEEP, NULL,
&dma_p->cookie, &dma_p->ncookies);
if (err != DDI_DMA_MAPPED) {
ddi_dma_mem_free(&dma_p->acc_hdl);
ddi_dma_free_handle(&dma_p->dma_hdl);
dma_p->acc_hdl = NULL;
dma_p->dma_hdl = NULL;
return (DDI_FAILURE);
}
dma_p->nslots = ~0U;
dma_p->size = ~0U;
dma_p->token = ~0U;
dma_p->offset = 0;
return (DDI_SUCCESS);
}
/*
* Free one allocated area of DMAable memory
*/
static void
iwh_free_dma_mem(iwh_dma_t *dma_p)
{
if (dma_p->dma_hdl != NULL) {
if (dma_p->ncookies) {
(void) ddi_dma_unbind_handle(dma_p->dma_hdl);
dma_p->ncookies = 0;
}
ddi_dma_free_handle(&dma_p->dma_hdl);
dma_p->dma_hdl = NULL;
}
if (dma_p->acc_hdl != NULL) {
ddi_dma_mem_free(&dma_p->acc_hdl);
dma_p->acc_hdl = NULL;
}
}
/*
* copy ucode into dma buffers
*/
static int
iwh_alloc_fw_dma(iwh_sc_t *sc)
{
int err = DDI_FAILURE;
iwh_dma_t *dma_p;
char *t;
/*
* firmware image layout:
* |HDR|<-TEXT->|<-DATA->|<-INIT_TEXT->|<-INIT_DATA->|<-BOOT->|
*/
/*
* copy text of runtime ucode
*/
t = (char *)(sc->sc_hdr + 1);
err = iwh_alloc_dma_mem(sc, LE_32(sc->sc_hdr->textsz),
&fw_dma_attr, &iwh_dma_accattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&sc->sc_dma_fw_text);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_alloc_fw_dma(): "
"failed to allocate text dma memory.\n");
goto fail;
}
dma_p = &sc->sc_dma_fw_text;
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_fw_dma(): "
"text[ncookies:%d addr:%lx size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
bcopy(t, dma_p->mem_va, LE_32(sc->sc_hdr->textsz));
/*
* copy data and bak-data of runtime ucode
*/
t += LE_32(sc->sc_hdr->textsz);
err = iwh_alloc_dma_mem(sc, LE_32(sc->sc_hdr->datasz),
&fw_dma_attr, &iwh_dma_accattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&sc->sc_dma_fw_data);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_alloc_fw_dma(): "
"failed to allocate data dma memory\n");
goto fail;
}
dma_p = &sc->sc_dma_fw_data;
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_fw_dma(): "
"data[ncookies:%d addr:%lx size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
bcopy(t, dma_p->mem_va, LE_32(sc->sc_hdr->datasz));
err = iwh_alloc_dma_mem(sc, LE_32(sc->sc_hdr->datasz),
&fw_dma_attr, &iwh_dma_accattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&sc->sc_dma_fw_data_bak);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_alloc_fw_dma(): "
"failed to allocate data bakup dma memory\n");
goto fail;
}
dma_p = &sc->sc_dma_fw_data_bak;
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_fw_dma(): "
"data_bak[ncookies:%d addr:%lx "
"size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
bcopy(t, dma_p->mem_va, LE_32(sc->sc_hdr->datasz));
/*
* copy text of init ucode
*/
t += LE_32(sc->sc_hdr->datasz);
err = iwh_alloc_dma_mem(sc, LE_32(sc->sc_hdr->init_textsz),
&fw_dma_attr, &iwh_dma_accattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&sc->sc_dma_fw_init_text);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_alloc_fw_dma(): "
"failed to allocate init text dma memory\n");
goto fail;
}
dma_p = &sc->sc_dma_fw_init_text;
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_fw_dma(): "
"init_text[ncookies:%d addr:%lx "
"size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
bcopy(t, dma_p->mem_va, LE_32(sc->sc_hdr->init_textsz));
/*
* copy data of init ucode
*/
t += LE_32(sc->sc_hdr->init_textsz);
err = iwh_alloc_dma_mem(sc, LE_32(sc->sc_hdr->init_datasz),
&fw_dma_attr, &iwh_dma_accattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&sc->sc_dma_fw_init_data);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwh_alloc_fw_dma(): "
"failed to allocate init data dma memory\n");
goto fail;
}
dma_p = &sc->sc_dma_fw_init_data;
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_fw_dma(): "
"init_data[ncookies:%d addr:%lx "
"size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
bcopy(t, dma_p->mem_va, LE_32(sc->sc_hdr->init_datasz));
sc->sc_boot = t + LE_32(sc->sc_hdr->init_datasz);
fail:
return (err);
}
static void
iwh_free_fw_dma(iwh_sc_t *sc)
{
iwh_free_dma_mem(&sc->sc_dma_fw_text);
iwh_free_dma_mem(&sc->sc_dma_fw_data);
iwh_free_dma_mem(&sc->sc_dma_fw_data_bak);
iwh_free_dma_mem(&sc->sc_dma_fw_init_text);
iwh_free_dma_mem(&sc->sc_dma_fw_init_data);
}
/*
* Allocate a shared buffer between host and NIC.
*/
static int
iwh_alloc_shared(iwh_sc_t *sc)
{
#ifdef DEBUG
iwh_dma_t *dma_p;
#endif
int err = DDI_FAILURE;
/*
* must be aligned on a 4K-page boundary
*/
err = iwh_alloc_dma_mem(sc, sizeof (iwh_shared_t),
&sh_dma_attr, &iwh_dma_descattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&sc->sc_dma_sh);
if (err != DDI_SUCCESS) {
goto fail;
}
sc->sc_shared = (iwh_shared_t *)sc->sc_dma_sh.mem_va;
#ifdef DEBUG
dma_p = &sc->sc_dma_sh;
#endif
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_shared(): "
"sh[ncookies:%d addr:%lx size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
return (err);
fail:
iwh_free_shared(sc);
return (err);
}
static void
iwh_free_shared(iwh_sc_t *sc)
{
iwh_free_dma_mem(&sc->sc_dma_sh);
}
/*
* Allocate a keep warm page.
*/
static int
iwh_alloc_kw(iwh_sc_t *sc)
{
#ifdef DEBUG
iwh_dma_t *dma_p;
#endif
int err = DDI_FAILURE;
/*
* must be aligned on a 4K-page boundary
*/
err = iwh_alloc_dma_mem(sc, IWH_KW_SIZE,
&kw_dma_attr, &iwh_dma_descattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&sc->sc_dma_kw);
if (err != DDI_SUCCESS) {
goto fail;
}
#ifdef DEBUG
dma_p = &sc->sc_dma_kw;
#endif
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_kw(): "
"kw[ncookies:%d addr:%lx size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
return (err);
fail:
iwh_free_kw(sc);
return (err);
}
static void
iwh_free_kw(iwh_sc_t *sc)
{
iwh_free_dma_mem(&sc->sc_dma_kw);
}
/*
* initialize RX ring buffers
*/
static int
iwh_alloc_rx_ring(iwh_sc_t *sc)
{
iwh_rx_ring_t *ring;
iwh_rx_data_t *data;
#ifdef DEBUG
iwh_dma_t *dma_p;
#endif
int i, err = DDI_FAILURE;
ring = &sc->sc_rxq;
ring->cur = 0;
/*
* allocate RX description ring buffer
*/
err = iwh_alloc_dma_mem(sc, RX_QUEUE_SIZE * sizeof (uint32_t),
&ring_desc_dma_attr, &iwh_dma_descattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&ring->dma_desc);
if (err != DDI_SUCCESS) {
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_rx_ring(): "
"dma alloc rx ring desc "
"failed\n"));
goto fail;
}
ring->desc = (uint32_t *)ring->dma_desc.mem_va;
#ifdef DEBUG
dma_p = &ring->dma_desc;
#endif
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_rx_ring(): "
"rx bd[ncookies:%d addr:%lx size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
/*
* Allocate Rx frame buffers.
*/
for (i = 0; i < RX_QUEUE_SIZE; i++) {
data = &ring->data[i];
err = iwh_alloc_dma_mem(sc, sc->sc_dmabuf_sz,
&rx_buffer_dma_attr, &iwh_dma_accattr,
DDI_DMA_READ | DDI_DMA_STREAMING,
&data->dma_data);
if (err != DDI_SUCCESS) {
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_rx_ring(): "
"dma alloc rx ring "
"buf[%d] failed\n", i));
goto fail;
}
/*
* the physical address bit [8-36] are used,
* instead of bit [0-31] in 3945.
*/
ring->desc[i] = (uint32_t)
(data->dma_data.cookie.dmac_address >> 8);
}
#ifdef DEBUG
dma_p = &ring->data[0].dma_data;
#endif
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_rx_ring(): "
"rx buffer[0][ncookies:%d addr:%lx "
"size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
IWH_DMA_SYNC(ring->dma_desc, DDI_DMA_SYNC_FORDEV);
return (err);
fail:
iwh_free_rx_ring(sc);
return (err);
}
/*
* disable RX ring
*/
static void
iwh_reset_rx_ring(iwh_sc_t *sc)
{
int n;
iwh_mac_access_enter(sc);
IWH_WRITE(sc, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
for (n = 0; n < 2000; n++) {
if (IWH_READ(sc, FH_MEM_RSSR_RX_STATUS_REG) & (1 << 24)) {
break;
}
DELAY(1000);
}
#ifdef DEBUG
if (2000 == n) {
IWH_DBG((IWH_DEBUG_DMA, "iwh_reset_rx_ring(): "
"timeout resetting Rx ring\n"));
}
#endif
iwh_mac_access_exit(sc);
sc->sc_rxq.cur = 0;
}
static void
iwh_free_rx_ring(iwh_sc_t *sc)
{
int i;
for (i = 0; i < RX_QUEUE_SIZE; i++) {
if (sc->sc_rxq.data[i].dma_data.dma_hdl) {
IWH_DMA_SYNC(sc->sc_rxq.data[i].dma_data,
DDI_DMA_SYNC_FORCPU);
}
iwh_free_dma_mem(&sc->sc_rxq.data[i].dma_data);
}
if (sc->sc_rxq.dma_desc.dma_hdl) {
IWH_DMA_SYNC(sc->sc_rxq.dma_desc, DDI_DMA_SYNC_FORDEV);
}
iwh_free_dma_mem(&sc->sc_rxq.dma_desc);
}
/*
* initialize TX ring buffers
*/
static int
iwh_alloc_tx_ring(iwh_sc_t *sc, iwh_tx_ring_t *ring,
int slots, int qid)
{
iwh_tx_data_t *data;
iwh_tx_desc_t *desc_h;
uint32_t paddr_desc_h;
iwh_cmd_t *cmd_h;
uint32_t paddr_cmd_h;
#ifdef DEBUG
iwh_dma_t *dma_p;
#endif
int i, err = DDI_FAILURE;
ring->qid = qid;
ring->count = TFD_QUEUE_SIZE_MAX;
ring->window = slots;
ring->queued = 0;
ring->cur = 0;
ring->desc_cur = 0;
/*
* allocate buffer for TX descriptor ring
*/
err = iwh_alloc_dma_mem(sc,
TFD_QUEUE_SIZE_MAX * sizeof (iwh_tx_desc_t),
&ring_desc_dma_attr, &iwh_dma_descattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&ring->dma_desc);
if (err != DDI_SUCCESS) {
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_tx_ring(): "
"dma alloc tx ring desc[%d] "
"failed\n", qid));
goto fail;
}
#ifdef DEBUG
dma_p = &ring->dma_desc;
#endif
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_tx_ring(): "
"tx bd[ncookies:%d addr:%lx size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
desc_h = (iwh_tx_desc_t *)ring->dma_desc.mem_va;
paddr_desc_h = ring->dma_desc.cookie.dmac_address;
/*
* allocate buffer for ucode command
*/
err = iwh_alloc_dma_mem(sc,
TFD_QUEUE_SIZE_MAX * sizeof (iwh_cmd_t),
&cmd_dma_attr, &iwh_dma_accattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&ring->dma_cmd);
if (err != DDI_SUCCESS) {
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_tx_ring(): "
"dma alloc tx ring cmd[%d]"
" failed\n", qid));
goto fail;
}
#ifdef DEBUG
dma_p = &ring->dma_cmd;
#endif
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_tx_ring(): "
"tx cmd[ncookies:%d addr:%lx size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
cmd_h = (iwh_cmd_t *)ring->dma_cmd.mem_va;
paddr_cmd_h = ring->dma_cmd.cookie.dmac_address;
/*
* Allocate Tx frame buffers.
*/
ring->data = kmem_zalloc(sizeof (iwh_tx_data_t) * TFD_QUEUE_SIZE_MAX,
KM_NOSLEEP);
if (NULL == ring->data) {
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_tx_ring(): "
"could not allocate "
"tx data slots\n"));
goto fail;
}
for (i = 0; i < TFD_QUEUE_SIZE_MAX; i++) {
data = &ring->data[i];
err = iwh_alloc_dma_mem(sc, sc->sc_dmabuf_sz,
&tx_buffer_dma_attr, &iwh_dma_accattr,
DDI_DMA_WRITE | DDI_DMA_STREAMING,
&data->dma_data);
if (err != DDI_SUCCESS) {
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_tx_ring(): "
"dma alloc tx "
"ring buf[%d] failed\n", i));
goto fail;
}
data->desc = desc_h + i;
data->paddr_desc = paddr_desc_h +
_PTRDIFF(data->desc, desc_h);
data->cmd = cmd_h + i;
data->paddr_cmd = paddr_cmd_h +
_PTRDIFF(data->cmd, cmd_h);
}
#ifdef DEBUG
dma_p = &ring->data[0].dma_data;
#endif
IWH_DBG((IWH_DEBUG_DMA, "iwh_alloc_tx_ring(): "
"tx buffer[0][ncookies:%d addr:%lx "
"size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
return (err);
fail:
iwh_free_tx_ring(ring);
return (err);
}
/*
* disable TX ring
*/
static void
iwh_reset_tx_ring(iwh_sc_t *sc, iwh_tx_ring_t *ring)
{
iwh_tx_data_t *data;
int i, n;
iwh_mac_access_enter(sc);
IWH_WRITE(sc, IWH_FH_TCSR_CHNL_TX_CONFIG_REG(ring->qid), 0);
for (n = 0; n < 200; n++) {
if (IWH_READ(sc, IWH_FH_TSSR_TX_STATUS_REG) &
IWH_FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ring->qid)) {
break;
}
DELAY(10);
}
#ifdef DEBUG
if (200 == n) {
IWH_DBG((IWH_DEBUG_DMA, "iwh_reset_tx_ring(): "
"timeout reset tx ring %d\n",
ring->qid));
}
#endif
iwh_mac_access_exit(sc);
/*
* by pass, if it's quiesce
*/
if (!(sc->sc_flags & IWH_F_QUIESCED)) {
for (i = 0; i < ring->count; i++) {
data = &ring->data[i];
IWH_DMA_SYNC(data->dma_data, DDI_DMA_SYNC_FORDEV);
}
}
ring->queued = 0;
ring->cur = 0;
ring->desc_cur = 0;
}
static void
iwh_free_tx_ring(iwh_tx_ring_t *ring)
{
int i;
if (ring->dma_desc.dma_hdl != NULL) {
IWH_DMA_SYNC(ring->dma_desc, DDI_DMA_SYNC_FORDEV);
}
iwh_free_dma_mem(&ring->dma_desc);
if (ring->dma_cmd.dma_hdl != NULL) {
IWH_DMA_SYNC(ring->dma_cmd, DDI_DMA_SYNC_FORDEV);
}
iwh_free_dma_mem(&ring->dma_cmd);
if (ring->data != NULL) {
for (i = 0; i < ring->count; i++) {
if (ring->data[i].dma_data.dma_hdl) {
IWH_DMA_SYNC(ring->data[i].dma_data,
DDI_DMA_SYNC_FORDEV);
}
iwh_free_dma_mem(&ring->data[i].dma_data);
}
kmem_free(ring->data, ring->count * sizeof (iwh_tx_data_t));
}
}
/*
* initialize TX and RX ring
*/
static int
iwh_ring_init(iwh_sc_t *sc)
{
int i, err = DDI_FAILURE;
for (i = 0; i < IWH_NUM_QUEUES; i++) {
if (IWH_CMD_QUEUE_NUM == i) {
continue;
}
err = iwh_alloc_tx_ring(sc, &sc->sc_txq[i], TFD_TX_CMD_SLOTS,
i);
if (err != DDI_SUCCESS) {
goto fail;
}
}
/*
* initialize command queue
*/
err = iwh_alloc_tx_ring(sc, &sc->sc_txq[IWH_CMD_QUEUE_NUM],
TFD_CMD_SLOTS, IWH_CMD_QUEUE_NUM);
if (err != DDI_SUCCESS) {
goto fail;
}
err = iwh_alloc_rx_ring(sc);
if (err != DDI_SUCCESS) {
goto fail;
}
fail:
return (err);
}
static void
iwh_ring_free(iwh_sc_t *sc)
{
int i = IWH_NUM_QUEUES;
iwh_free_rx_ring(sc);
while (--i >= 0) {
iwh_free_tx_ring(&sc->sc_txq[i]);
}
}
/* ARGSUSED */
static ieee80211_node_t *
iwh_node_alloc(ieee80211com_t *ic)
{
iwh_amrr_t *amrr;
amrr = kmem_zalloc(sizeof (iwh_amrr_t), KM_SLEEP);
if (NULL == amrr) {
cmn_err(CE_WARN, "iwh_node_alloc(): "
"failed to allocate memory for amrr structure\n");
return (NULL);
}
iwh_amrr_init(amrr);
return (&amrr->in);
}
static void
iwh_node_free(ieee80211_node_t *in)
{
ieee80211com_t *ic;
if ((NULL == in) ||
(NULL == in->in_ic)) {
cmn_err(CE_WARN, "iwh_node_free() "
"Got a NULL point from Net80211 module\n");
return;
}
ic = in->in_ic;
if (ic->ic_node_cleanup != NULL) {
ic->ic_node_cleanup(in);
}
if (in->in_wpa_ie != NULL) {
ieee80211_free(in->in_wpa_ie);
}
if (in->in_wme_ie != NULL) {
ieee80211_free(in->in_wme_ie);
}
if (in->in_htcap_ie != NULL) {
ieee80211_free(in->in_htcap_ie);
}
kmem_free(in, sizeof (iwh_amrr_t));
}
/*
* change station's state. this function will be invoked by 80211 module
* when need to change staton's state.
*/
static int
iwh_newstate(ieee80211com_t *ic, enum ieee80211_state nstate, int arg)
{
iwh_sc_t *sc;
ieee80211_node_t *in;
enum ieee80211_state ostate;
iwh_add_sta_t node;
iwh_amrr_t *amrr;
uint8_t r;
int i, err = IWH_FAIL;
if (NULL == ic) {
return (err);
}
sc = (iwh_sc_t *)ic;
in = ic->ic_bss;
ostate = ic->ic_state;
mutex_enter(&sc->sc_glock);
switch (nstate) {
case IEEE80211_S_SCAN:
switch (ostate) {
case IEEE80211_S_INIT:
atomic_or_32(&sc->sc_flags, IWH_F_SCANNING);
iwh_set_led(sc, 2, 10, 2);
/*
* clear association to receive beacons from
* all BSS'es
*/
sc->sc_config.assoc_id = 0;
sc->sc_config.filter_flags &=
~LE_32(RXON_FILTER_ASSOC_MSK);
IWH_DBG((IWH_DEBUG_80211, "iwh_newstate(): "
"config chan %d "
"flags %x filter_flags %x\n",
LE_16(sc->sc_config.chan),
LE_32(sc->sc_config.flags),
LE_32(sc->sc_config.filter_flags)));
err = iwh_cmd(sc, REPLY_RXON, &sc->sc_config,
sizeof (iwh_rxon_cmd_t), 1);
if (err != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_newstate(): "
"could not clear association\n");
atomic_and_32(&sc->sc_flags, ~IWH_F_SCANNING);
mutex_exit(&sc->sc_glock);
return (err);
}
/*
* add broadcast node to send probe request
*/
(void) memset(&node, 0, sizeof (node));
(void) memset(&node.sta.addr, 0xff, IEEE80211_ADDR_LEN);
node.sta.sta_id = IWH_BROADCAST_ID;
err = iwh_cmd(sc, REPLY_ADD_STA, &node,
sizeof (node), 1);
if (err != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_newstate(): "
"could not add broadcast node\n");
atomic_and_32(&sc->sc_flags, ~IWH_F_SCANNING);
mutex_exit(&sc->sc_glock);
return (err);
}
break;
case IEEE80211_S_SCAN:
mutex_exit(&sc->sc_glock);
/* step to next channel before actual FW scan */
err = sc->sc_newstate(ic, nstate, arg);
mutex_enter(&sc->sc_glock);
if ((err != 0) || ((err = iwh_scan(sc)) != 0)) {
cmn_err(CE_WARN, "iwh_newstate(): "
"could not initiate scan\n");
atomic_and_32(&sc->sc_flags, ~IWH_F_SCANNING);
ieee80211_cancel_scan(ic);
}
mutex_exit(&sc->sc_glock);
return (err);
default:
break;
}
sc->sc_clk = 0;
break;
case IEEE80211_S_AUTH:
if (ostate == IEEE80211_S_SCAN) {
atomic_and_32(&sc->sc_flags, ~IWH_F_SCANNING);
}
/*
* reset state to handle reassociations correctly
*/
sc->sc_config.assoc_id = 0;
sc->sc_config.filter_flags &= ~LE_32(RXON_FILTER_ASSOC_MSK);
/*
* before sending authentication and association request frame,
* we need do something in the hardware, such as setting the
* channel same to the target AP...
*/
if ((err = iwh_hw_set_before_auth(sc)) != 0) {
IWH_DBG((IWH_DEBUG_80211, "iwh_newstate(): "
"could not send authentication request\n"));
mutex_exit(&sc->sc_glock);
return (err);
}
break;
case IEEE80211_S_RUN:
if (ostate == IEEE80211_S_SCAN) {
atomic_and_32(&sc->sc_flags, ~IWH_F_SCANNING);
}
if (IEEE80211_M_MONITOR == ic->ic_opmode) {
/*
* let LED blink when monitoring
*/
iwh_set_led(sc, 2, 10, 10);
break;
}
IWH_DBG((IWH_DEBUG_80211, "iwh_newstate(): "
"associated.\n"));
err = iwh_run_state_config(sc);
if (err != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_newstate(): "
"failed to set up association\n");
mutex_exit(&sc->sc_glock);
return (err);
}
/*
* start automatic rate control
*/
if ((in->in_flags & IEEE80211_NODE_HT) &&
(sc->sc_ht_conf.ht_support) &&
(in->in_htrates.rs_nrates > 0) &&
(in->in_htrates.rs_nrates <= IEEE80211_HTRATE_MAXSIZE)) {
amrr = (iwh_amrr_t *)in;
for (i = in->in_htrates.rs_nrates - 1; i > 0; i--) {
r = in->in_htrates.rs_rates[i] &
IEEE80211_RATE_VAL;
if ((r != 0) && (r <= 0xd) &&
(sc->sc_ht_conf.tx_support_mcs[r/8] &
(1 << (r%8)))) {
amrr->ht_mcs_idx = r;
atomic_or_32(&sc->sc_flags,
IWH_F_RATE_AUTO_CTL);
break;
}
}
} else {
if (IEEE80211_FIXED_RATE_NONE == ic->ic_fixed_rate) {
atomic_or_32(&sc->sc_flags,
IWH_F_RATE_AUTO_CTL);
/*
* set rate to some reasonable initial value
*/
i = in->in_rates.ir_nrates - 1;
while (i > 0 && IEEE80211_RATE(i) > 72) {
i--;
}
in->in_txrate = i;
} else {
atomic_and_32(&sc->sc_flags,
~IWH_F_RATE_AUTO_CTL);
}
}
/*
* set LED on after associated
*/
iwh_set_led(sc, 2, 0, 1);
break;
case IEEE80211_S_INIT:
if (ostate == IEEE80211_S_SCAN) {
atomic_and_32(&sc->sc_flags, ~IWH_F_SCANNING);
}
/*
* set LED off after init
*/
iwh_set_led(sc, 2, 1, 0);
break;
case IEEE80211_S_ASSOC:
if (ostate == IEEE80211_S_SCAN) {
atomic_and_32(&sc->sc_flags, ~IWH_F_SCANNING);
}
break;
}
mutex_exit(&sc->sc_glock);
return (sc->sc_newstate(ic, nstate, arg));
}
/*
* exclusive access to mac begin.
*/
static void
iwh_mac_access_enter(iwh_sc_t *sc)
{
uint32_t tmp;
int n;
tmp = IWH_READ(sc, CSR_GP_CNTRL);
IWH_WRITE(sc, CSR_GP_CNTRL,
tmp | CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
/*
* wait until we succeed
*/
for (n = 0; n < 1000; n++) {
if ((IWH_READ(sc, CSR_GP_CNTRL) &
(CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP)) ==
CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN) {
break;
}
DELAY(10);
}
#ifdef DEBUG
if (1000 == n) {
IWH_DBG((IWH_DEBUG_PIO, "iwh_mac_access_enter(): "
"could not lock memory\n"));
}
#endif
}
/*
* exclusive access to mac end.
*/
static void
iwh_mac_access_exit(iwh_sc_t *sc)
{
uint32_t tmp = IWH_READ(sc, CSR_GP_CNTRL);
IWH_WRITE(sc, CSR_GP_CNTRL,
tmp & ~CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
}
/*
* this function defined here for future use.
* static uint32_t
* iwh_mem_read(iwh_sc_t *sc, uint32_t addr)
* {
* IWH_WRITE(sc, HBUS_TARG_MEM_RADDR, addr);
* return (IWH_READ(sc, HBUS_TARG_MEM_RDAT));
* }
*/
/*
* write mac memory
*/
static void
iwh_mem_write(iwh_sc_t *sc, uint32_t addr, uint32_t data)
{
IWH_WRITE(sc, HBUS_TARG_MEM_WADDR, addr);
IWH_WRITE(sc, HBUS_TARG_MEM_WDAT, data);
}
/*
* read mac register
*/
static uint32_t
iwh_reg_read(iwh_sc_t *sc, uint32_t addr)
{
IWH_WRITE(sc, HBUS_TARG_PRPH_RADDR, addr | (3 << 24));
return (IWH_READ(sc, HBUS_TARG_PRPH_RDAT));
}
/*
* write mac register
*/
static void
iwh_reg_write(iwh_sc_t *sc, uint32_t addr, uint32_t data)
{
IWH_WRITE(sc, HBUS_TARG_PRPH_WADDR, addr | (3 << 24));
IWH_WRITE(sc, HBUS_TARG_PRPH_WDAT, data);
}
/*
* steps of loading ucode:
* load init ucode=>init alive=>calibrate=>
* receive calibration result=>reinitialize NIC=>
* load runtime ucode=>runtime alive=>
* send calibration result=>running.
*/
static int
iwh_load_init_firmware(iwh_sc_t *sc)
{
int err = IWH_FAIL;
clock_t clk;
atomic_and_32(&sc->sc_flags, ~IWH_F_PUT_SEG);
/*
* load init_text section of uCode to hardware
*/
err = iwh_put_seg_fw(sc, sc->sc_dma_fw_init_text.cookie.dmac_address,
RTC_INST_LOWER_BOUND, sc->sc_dma_fw_init_text.cookie.dmac_size);
if (err != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_load_init_firmware(): "
"failed to write init uCode.\n");
return (err);
}
clk = ddi_get_lbolt() + drv_usectohz(1000000);
/*
* wait loading init_text until completed or timeout
*/
while (!(sc->sc_flags & IWH_F_PUT_SEG)) {
if (cv_timedwait(&sc->sc_put_seg_cv, &sc->sc_glock, clk) < 0) {
break;
}
}
if (!(sc->sc_flags & IWH_F_PUT_SEG)) {
cmn_err(CE_WARN, "iwh_load_init_firmware(): "
"timeout waiting for init uCode load.\n");
return (IWH_FAIL);
}
atomic_and_32(&sc->sc_flags, ~IWH_F_PUT_SEG);
/*
* load init_data section of uCode to hardware
*/
err = iwh_put_seg_fw(sc, sc->sc_dma_fw_init_data.cookie.dmac_address,
RTC_DATA_LOWER_BOUND, sc->sc_dma_fw_init_data.cookie.dmac_size);
if (err != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_load_init_firmware(): "
"failed to write init_data uCode.\n");
return (err);
}
clk = ddi_get_lbolt() + drv_usectohz(1000000);
/*
* wait loading init_data until completed or timeout
*/
while (!(sc->sc_flags & IWH_F_PUT_SEG)) {
if (cv_timedwait(&sc->sc_put_seg_cv, &sc->sc_glock, clk) < 0) {
break;
}
}
if (!(sc->sc_flags & IWH_F_PUT_SEG)) {
cmn_err(CE_WARN, "iwh_load_init_firmware(): "
"timeout waiting for init_data uCode load.\n");
return (IWH_FAIL);
}
atomic_and_32(&sc->sc_flags, ~IWH_F_PUT_SEG);
return (err);
}
static int
iwh_load_run_firmware(iwh_sc_t *sc)
{
int err = IWH_FAIL;
clock_t clk;
atomic_and_32(&sc->sc_flags, ~IWH_F_PUT_SEG);
/*
* load init_text section of uCode to hardware
*/
err = iwh_put_seg_fw(sc, sc->sc_dma_fw_text.cookie.dmac_address,
RTC_INST_LOWER_BOUND, sc->sc_dma_fw_text.cookie.dmac_size);
if (err != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_load_run_firmware(): "
"failed to write run uCode.\n");
return (err);
}
clk = ddi_get_lbolt() + drv_usectohz(1000000);
/*
* wait loading run_text until completed or timeout
*/
while (!(sc->sc_flags & IWH_F_PUT_SEG)) {
if (cv_timedwait(&sc->sc_put_seg_cv, &sc->sc_glock, clk) < 0) {
break;
}
}
if (!(sc->sc_flags & IWH_F_PUT_SEG)) {
cmn_err(CE_WARN, "iwh_load_run_firmware(): "
"timeout waiting for run uCode load.\n");
return (IWH_FAIL);
}
atomic_and_32(&sc->sc_flags, ~IWH_F_PUT_SEG);
/*
* load run_data section of uCode to hardware
*/
err = iwh_put_seg_fw(sc, sc->sc_dma_fw_data_bak.cookie.dmac_address,
RTC_DATA_LOWER_BOUND, sc->sc_dma_fw_data.cookie.dmac_size);
if (err != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_load_run_firmware(): "
"failed to write run_data uCode.\n");
return (err);
}
clk = ddi_get_lbolt() + drv_usectohz(1000000);
/*
* wait loading run_data until completed or timeout
*/
while (!(sc->sc_flags & IWH_F_PUT_SEG)) {
if (cv_timedwait(&sc->sc_put_seg_cv, &sc->sc_glock, clk) < 0) {
break;
}
}
if (!(sc->sc_flags & IWH_F_PUT_SEG)) {
cmn_err(CE_WARN, "iwh_load_run_firmware(): "
"timeout waiting for run_data uCode load.\n");
return (IWH_FAIL);
}
atomic_and_32(&sc->sc_flags, ~IWH_F_PUT_SEG);
return (err);
}
/*
* this function will be invoked to receive phy information
* when a frame is received.
*/
static void
iwh_rx_phy_intr(iwh_sc_t *sc, iwh_rx_desc_t *desc)
{
sc->sc_rx_phy_res.flag = 1;
bcopy((uint8_t *)(desc + 1), sc->sc_rx_phy_res.buf,
sizeof (iwh_rx_phy_res_t));
}
/*
* this function will be invoked to receive body of frame when
* a frame is received.
*/
static void
iwh_rx_mpdu_intr(iwh_sc_t *sc, iwh_rx_desc_t *desc)
{
ieee80211com_t *ic = &sc->sc_ic;
#ifdef DEBUG
iwh_rx_ring_t *ring = &sc->sc_rxq;
#endif
struct ieee80211_frame *wh;
struct iwh_rx_non_cfg_phy *phyinfo;
struct iwh_rx_mpdu_body_size *mpdu_size;
mblk_t *mp;
int16_t t;
uint16_t len, rssi, agc;
uint32_t temp, crc, *tail;
uint32_t arssi, brssi, crssi, mrssi;
iwh_rx_phy_res_t *stat;
ieee80211_node_t *in;
/*
* assuming not 11n here. cope with 11n in phase-II
*/
mpdu_size = (struct iwh_rx_mpdu_body_size *)(desc + 1);
stat = (iwh_rx_phy_res_t *)sc->sc_rx_phy_res.buf;
if (stat->cfg_phy_cnt > 20) {
return;
}
phyinfo = (struct iwh_rx_non_cfg_phy *)stat->non_cfg_phy;
temp = LE_32(phyinfo->non_cfg_phy[IWH_RX_RES_AGC_IDX]);
agc = (temp & IWH_OFDM_AGC_MSK) >> IWH_OFDM_AGC_BIT_POS;
temp = LE_32(phyinfo->non_cfg_phy[IWH_RX_RES_RSSI_AB_IDX]);
arssi = (temp & IWH_OFDM_RSSI_A_MSK) >> IWH_OFDM_RSSI_A_BIT_POS;
brssi = (temp & IWH_OFDM_RSSI_B_MSK) >> IWH_OFDM_RSSI_B_BIT_POS;
temp = LE_32(phyinfo->non_cfg_phy[IWH_RX_RES_RSSI_C_IDX]);
crssi = (temp & IWH_OFDM_RSSI_C_MSK) >> IWH_OFDM_RSSI_C_BIT_POS;
mrssi = MAX(arssi, brssi);
mrssi = MAX(mrssi, crssi);
t = mrssi - agc - IWH_RSSI_OFFSET;
/*
* convert dBm to percentage
*/
rssi = (100 * 75 * 75 - (-20 - t) * (15 * 75 + 62 * (-20 - t)))
/ (75 * 75);
if (rssi > 100) {
rssi = 100;
}
if (rssi < 1) {
rssi = 1;
}
/*
* size of frame, not include FCS
*/
len = LE_16(mpdu_size->byte_count);
tail = (uint32_t *)((uint8_t *)(desc + 1) +
sizeof (struct iwh_rx_mpdu_body_size) + len);
bcopy(tail, &crc, 4);
IWH_DBG((IWH_DEBUG_RX, "iwh_rx_mpdu_intr(): "
"rx intr: idx=%d phy_len=%x len=%d "
"rate=%x chan=%d tstamp=%x non_cfg_phy_count=%x "
"cfg_phy_count=%x tail=%x", ring->cur, sizeof (*stat),
len, stat->rate.r.s.rate, stat->channel,
LE_32(stat->timestampl), stat->non_cfg_phy_cnt,
stat->cfg_phy_cnt, LE_32(crc)));
if ((len < 16) || (len > sc->sc_dmabuf_sz)) {
IWH_DBG((IWH_DEBUG_RX, "iwh_rx_mpdu_intr(): "
"rx frame oversize\n"));
return;
}
/*
* discard Rx frames with bad CRC
*/
if ((LE_32(crc) &
(RX_RES_STATUS_NO_CRC32_ERROR | RX_RES_STATUS_NO_RXE_OVERFLOW)) !=
(RX_RES_STATUS_NO_CRC32_ERROR | RX_RES_STATUS_NO_RXE_OVERFLOW)) {
IWH_DBG((IWH_DEBUG_RX, "iwh_rx_mpdu_intr(): "
"rx crc error tail: %x\n",
LE_32(crc)));
sc->sc_rx_err++;
return;
}
wh = (struct ieee80211_frame *)
((uint8_t *)(desc + 1)+ sizeof (struct iwh_rx_mpdu_body_size));
if (IEEE80211_FC0_SUBTYPE_ASSOC_RESP == *(uint8_t *)wh) {
sc->sc_assoc_id = *((uint16_t *)(wh + 1) + 2);
IWH_DBG((IWH_DEBUG_RX, "iwh_rx_mpdu_intr(): "
"rx : association id = %x\n",
sc->sc_assoc_id));
}
#ifdef DEBUG
if (iwh_dbg_flags & IWH_DEBUG_RX) {
ieee80211_dump_pkt((uint8_t *)wh, len, 0, 0);
}
#endif
in = ieee80211_find_rxnode(ic, wh);
mp = allocb(len, BPRI_MED);
if (mp) {
bcopy(wh, mp->b_wptr, len);
mp->b_wptr += len;
/*
* send the frame to the 802.11 layer
*/
(void) ieee80211_input(ic, mp, in, rssi, 0);
} else {
sc->sc_rx_nobuf++;
IWH_DBG((IWH_DEBUG_RX, "iwh_rx_mpdu_intr(): "
"alloc rx buf failed\n"));
}
/*
* release node reference
*/
ieee80211_free_node(in);
}
/*
* process correlative affairs after a frame is sent.
*/
static void
iwh_tx_intr(iwh_sc_t *sc, iwh_rx_desc_t *desc)
{
ieee80211com_t *ic = &sc->sc_ic;
iwh_tx_ring_t *ring = &sc->sc_txq[desc->hdr.qid & 0x3];
iwh_tx_stat_t *stat = (iwh_tx_stat_t *)(desc + 1);
iwh_amrr_t *amrr;
if (NULL == ic->ic_bss) {
return;
}
amrr = (iwh_amrr_t *)ic->ic_bss;
amrr->txcnt++;
IWH_DBG((IWH_DEBUG_RATECTL, "iwh_tx_intr(): "
"tx: %d cnt\n", amrr->txcnt));
if (stat->ntries > 0) {
amrr->retrycnt++;
sc->sc_tx_retries++;
IWH_DBG((IWH_DEBUG_TX, "iwh_tx_intr(): "
"tx: %d retries\n",
sc->sc_tx_retries));
}
mutex_enter(&sc->sc_mt_lock);
sc->sc_tx_timer = 0;
mutex_exit(&sc->sc_mt_lock);
mutex_enter(&sc->sc_tx_lock);
ring->queued--;
if (ring->queued < 0) {
ring->queued = 0;
}
if ((sc->sc_need_reschedule) && (ring->queued <= (ring->count >> 3))) {
sc->sc_need_reschedule = 0;
mutex_exit(&sc->sc_tx_lock);
mac_tx_update(ic->ic_mach);
mutex_enter(&sc->sc_tx_lock);
}
mutex_exit(&sc->sc_tx_lock);
}
/*
* inform a given command has been executed
*/
static void
iwh_cmd_intr(iwh_sc_t *sc, iwh_rx_desc_t *desc)
{
if ((desc->hdr.qid & 7) != 4) {
return;
}
if (sc->sc_cmd_accum > 0) {
sc->sc_cmd_accum--;
return;
}
mutex_enter(&sc->sc_glock);
sc->sc_cmd_flag = SC_CMD_FLG_DONE;
cv_signal(&sc->sc_cmd_cv);
mutex_exit(&sc->sc_glock);
IWH_DBG((IWH_DEBUG_CMD, "iwh_cmd_intr(): "
"qid=%x idx=%d flags=%x type=0x%x\n",
desc->hdr.qid, desc->hdr.idx, desc->hdr.flags,
desc->hdr.type));
}
/*
* this function will be invoked when alive notification occur.
*/
static void
iwh_ucode_alive(iwh_sc_t *sc, iwh_rx_desc_t *desc)
{
uint32_t rv;
struct iwh_calib_cfg_cmd cmd;
struct iwh_alive_resp *ar =
(struct iwh_alive_resp *)(desc + 1);
struct iwh_calib_results *res_p = &sc->sc_calib_results;
/*
* the microcontroller is ready
*/
IWH_DBG((IWH_DEBUG_FW, "iwh_ucode_alive(): "
"microcode alive notification minor: %x major: %x type: "
"%x subtype: %x\n",
ar->ucode_minor, ar->ucode_minor, ar->ver_type, ar->ver_subtype));
#ifdef DEBUG
if (LE_32(ar->is_valid) != UCODE_VALID_OK) {
IWH_DBG((IWH_DEBUG_FW, "iwh_ucode_alive(): "
"microcontroller initialization failed\n"));
}
#endif
/*
* determine if init alive or runtime alive.
*/
if (INITIALIZE_SUBTYPE == ar->ver_subtype) {
IWH_DBG((IWH_DEBUG_FW, "iwh_ucode_alive(): "
"initialization alive received.\n"));
bcopy(ar, &sc->sc_card_alive_init,
sizeof (struct iwh_init_alive_resp));
/*
* necessary configuration to NIC
*/
mutex_enter(&sc->sc_glock);
rv = iwh_alive_common(sc);
if (rv != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_ucode_alive(): "
"common alive process failed in init alive.\n");
mutex_exit(&sc->sc_glock);
return;
}
(void) memset(&cmd, 0, sizeof (cmd));
cmd.ucd_calib_cfg.once.is_enable = IWH_CALIB_INIT_CFG_ALL;
cmd.ucd_calib_cfg.once.start = IWH_CALIB_INIT_CFG_ALL;
cmd.ucd_calib_cfg.once.send_res = IWH_CALIB_INIT_CFG_ALL;
cmd.ucd_calib_cfg.flags = IWH_CALIB_INIT_CFG_ALL;
/*
* require ucode execute calibration
*/
rv = iwh_cmd(sc, CALIBRATION_CFG_CMD, &cmd, sizeof (cmd), 1);
if (rv != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_ucode_alive(): "
"failed to send calibration configure command.\n");
mutex_exit(&sc->sc_glock);
return;
}
mutex_exit(&sc->sc_glock);
} else { /* runtime alive */
IWH_DBG((IWH_DEBUG_FW, "iwh_ucode_alive(): "
"runtime alive received.\n"));
bcopy(ar, &sc->sc_card_alive_run,
sizeof (struct iwh_alive_resp));
mutex_enter(&sc->sc_glock);
/*
* necessary configuration to NIC
*/
rv = iwh_alive_common(sc);
if (rv != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_ucode_alive(): "
"common alive process failed in run alive.\n");
mutex_exit(&sc->sc_glock);
return;
}
/*
* send the result of local oscilator calibration to uCode.
*/
if (res_p->lo_res != NULL) {
rv = iwh_cmd(sc, REPLY_PHY_CALIBRATION_CMD,
res_p->lo_res, res_p->lo_res_len, 1);
if (rv != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_ucode_alive(): "
"failed to send local"
"oscilator calibration command.\n");
mutex_exit(&sc->sc_glock);
return;
}
DELAY(1000);
}
/*
* send the result of TX IQ calibration to uCode.
*/
if (res_p->tx_iq_res != NULL) {
rv = iwh_cmd(sc, REPLY_PHY_CALIBRATION_CMD,
res_p->tx_iq_res, res_p->tx_iq_res_len, 1);
if (rv != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_ucode_alive(): "
"failed to send TX IQ"
"calibration command.\n");
mutex_exit(&sc->sc_glock);
return;
}
DELAY(1000);
}
/*
* sned the result of TX IQ perd calibration to uCode.
*/
if (res_p->tx_iq_perd_res != NULL) {
rv = iwh_cmd(sc, REPLY_PHY_CALIBRATION_CMD,
res_p->tx_iq_perd_res,
res_p->tx_iq_perd_res_len, 1);
if (rv != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_ucode_alive(): "
"failed to send TX IQ perd"
"calibration command.\n");
mutex_exit(&sc->sc_glock);
return;
}
DELAY(1000);
}
/*
* send the result of DC calibration to uCode.
*/
if (res_p->dc_res != NULL) {
rv = iwh_cmd(sc, REPLY_PHY_CALIBRATION_CMD,
res_p->dc_res,
res_p->dc_res_len, 1);
if (rv != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_ucode_alive(): "
"failed to send DC"
"calibration command.\n");
mutex_exit(&sc->sc_glock);
return;
}
DELAY(1000);
}
/*
* send the result of BASE BAND calibration to uCode.
*/
if (res_p->base_band_res != NULL) {
rv = iwh_cmd(sc, REPLY_PHY_CALIBRATION_CMD,
res_p->base_band_res,
res_p->base_band_res_len, 1);
if (rv != IWH_SUCCESS) {
cmn_err(CE_WARN, "iwh_ucode_alive(): "
"failed to send BASE BAND"
"calibration command.\n");
mutex_exit(&sc->sc_glock);
return;
}
DELAY(1000);
}
atomic_or_32(&sc->sc_flags, IWH_F_FW_INIT);
cv_signal(&sc->sc_ucode_cv);
mutex_exit(&sc->sc_glock);
}
}
/*
* deal with receiving frames, command response
* and all notifications from ucode.
*/
/* ARGSUSED */
static uint_t
iwh_rx_softintr(caddr_t arg, caddr_t unused)
{
iwh_sc_t *sc;
ieee80211com_t *ic;
iwh_rx_desc_t *desc;
iwh_rx_data_t *data;
uint32_t index;
if (NULL == arg) {
return (DDI_INTR_UNCLAIMED);
}
sc = (iwh_sc_t *)arg;
ic = &sc->sc_ic;
/*
* firmware has moved the index of the rx queue, driver get it,
* and deal with it.
*/
index = (sc->sc_shared->val0) & 0xfff;
while (sc->sc_rxq.cur != index) {
data = &sc->sc_rxq.data[sc->sc_rxq.cur];
desc = (iwh_rx_desc_t *)data->dma_data.mem_va;
IWH_DBG((IWH_DEBUG_INTR, "iwh_rx_softintr(): "
"rx notification index = %d"
" cur = %d qid=%x idx=%d flags=%x type=%x len=%d\n",
index, sc->sc_rxq.cur, desc->hdr.qid, desc->hdr.idx,
desc->hdr.flags, desc->hdr.type, LE_32(desc->len)));
/*
* a command other than a tx need to be replied
*/
if (!(desc->hdr.qid & 0x80) &&
(desc->hdr.type != REPLY_SCAN_CMD) &&
(desc->hdr.type != REPLY_TX)) {
iwh_cmd_intr(sc, desc);
}
switch (desc->hdr.type) {
case REPLY_RX_PHY_CMD:
iwh_rx_phy_intr(sc, desc);
break;
case REPLY_RX_MPDU_CMD:
iwh_rx_mpdu_intr(sc, desc);
break;
case REPLY_TX:
iwh_tx_intr(sc, desc);
break;
case REPLY_ALIVE:
iwh_ucode_alive(sc, desc);
break;
case CARD_STATE_NOTIFICATION:
{
uint32_t *status = (uint32_t *)(desc + 1);
IWH_DBG((IWH_DEBUG_RADIO, "iwh_rx_softintr(): "
"state changed to %x\n",
LE_32(*status)));
if (LE_32(*status) & 1) {
/*
* the radio button has to be pushed(OFF). It
* is considered as a hw error, the
* iwh_thread() tries to recover it after the
* button is pushed again(ON)
*/
cmn_err(CE_NOTE, "iwh_rx_softintr(): "
"radio transmitter is off\n");
sc->sc_ostate = sc->sc_ic.ic_state;
ieee80211_new_state(&sc->sc_ic,
IEEE80211_S_INIT, -1);
atomic_or_32(&sc->sc_flags,
(IWH_F_HW_ERR_RECOVER | IWH_F_RADIO_OFF));
}
break;
}
case SCAN_START_NOTIFICATION:
{
iwh_start_scan_t *scan =
(iwh_start_scan_t *)(desc + 1);
IWH_DBG((IWH_DEBUG_SCAN, "iwh_rx_softintr(): "
"scanning channel %d status %x\n",
scan->chan, LE_32(scan->status)));
ic->ic_curchan = &ic->ic_sup_channels[scan->chan];
break;
}
case SCAN_COMPLETE_NOTIFICATION:
{
#ifdef DEBUG
iwh_stop_scan_t *scan =
(iwh_stop_scan_t *)(desc + 1);
IWH_DBG((IWH_DEBUG_SCAN, "iwh_rx_softintr(): "
"completed channel %d (burst of %d) status %02x\n",
scan->chan, scan->nchan, scan->status));
#endif
sc->sc_scan_pending++;
break;
}
case STATISTICS_NOTIFICATION:
{
/*
* handle statistics notification
*/
break;
}
case CALIBRATION_RES_NOTIFICATION:
iwh_save_calib_result(sc, desc);
break;
case CALIBRATION_COMPLETE_NOTIFICATION:
mutex_enter(&sc->sc_glock);
atomic_or_32(&sc->sc_flags, IWH_F_FW_INIT);
cv_signal(&sc->sc_ucode_cv);
mutex_exit(&sc->sc_glock);
break;
case MISSED_BEACONS_NOTIFICATION:
/* handle beacon miss by software mechanism */
break;
}
sc->sc_rxq.cur = (sc->sc_rxq.cur + 1) % RX_QUEUE_SIZE;
}