blob: eec2b846640c245ffab00ce566c9dd61f7aac0f6 [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 6000 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/varargs.h>
#include <sys/policy.h>
#include <sys/pci.h>
#include "iwp_calibration.h"
#include "iwp_hw.h"
#include "iwp_eeprom.h"
#include "iwp_var.h"
#include <inet/wifi_ioctl.h>
#ifdef DEBUG
#define IWP_DEBUG_80211 (1 << 0)
#define IWP_DEBUG_CMD (1 << 1)
#define IWP_DEBUG_DMA (1 << 2)
#define IWP_DEBUG_EEPROM (1 << 3)
#define IWP_DEBUG_FW (1 << 4)
#define IWP_DEBUG_HW (1 << 5)
#define IWP_DEBUG_INTR (1 << 6)
#define IWP_DEBUG_MRR (1 << 7)
#define IWP_DEBUG_PIO (1 << 8)
#define IWP_DEBUG_RX (1 << 9)
#define IWP_DEBUG_SCAN (1 << 10)
#define IWP_DEBUG_TX (1 << 11)
#define IWP_DEBUG_RATECTL (1 << 12)
#define IWP_DEBUG_RADIO (1 << 13)
#define IWP_DEBUG_RESUME (1 << 14)
#define IWP_DEBUG_CALIBRATION (1 << 15)
/*
* if want to see debug message of a given section,
* please set this flag to one of above values
*/
uint32_t iwp_dbg_flags = 0;
#define IWP_DBG(x) \
iwp_dbg x
#else
#define IWP_DBG(x)
#endif
static void *iwp_soft_state_p = NULL;
/*
* ucode will be compiled into driver image
*/
static uint8_t iwp_fw_bin [] = {
#include "fw-iw/iwp.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 iwp_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 iwp_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 iwp_dma_accattr = {
DDI_DEVICE_ATTR_V0,
DDI_NEVERSWAP_ACC,
DDI_STRICTORDER_ACC,
DDI_DEFAULT_ACC
};
static int iwp_ring_init(iwp_sc_t *);
static void iwp_ring_free(iwp_sc_t *);
static int iwp_alloc_shared(iwp_sc_t *);
static void iwp_free_shared(iwp_sc_t *);
static int iwp_alloc_kw(iwp_sc_t *);
static void iwp_free_kw(iwp_sc_t *);
static int iwp_alloc_fw_dma(iwp_sc_t *);
static void iwp_free_fw_dma(iwp_sc_t *);
static int iwp_alloc_rx_ring(iwp_sc_t *);
static void iwp_reset_rx_ring(iwp_sc_t *);
static void iwp_free_rx_ring(iwp_sc_t *);
static int iwp_alloc_tx_ring(iwp_sc_t *, iwp_tx_ring_t *,
int, int);
static void iwp_reset_tx_ring(iwp_sc_t *, iwp_tx_ring_t *);
static void iwp_free_tx_ring(iwp_tx_ring_t *);
static ieee80211_node_t *iwp_node_alloc(ieee80211com_t *);
static void iwp_node_free(ieee80211_node_t *);
static int iwp_newstate(ieee80211com_t *, enum ieee80211_state, int);
static void iwp_mac_access_enter(iwp_sc_t *);
static void iwp_mac_access_exit(iwp_sc_t *);
static uint32_t iwp_reg_read(iwp_sc_t *, uint32_t);
static void iwp_reg_write(iwp_sc_t *, uint32_t, uint32_t);
static int iwp_load_init_firmware(iwp_sc_t *);
static int iwp_load_run_firmware(iwp_sc_t *);
static void iwp_tx_intr(iwp_sc_t *, iwp_rx_desc_t *);
static void iwp_cmd_intr(iwp_sc_t *, iwp_rx_desc_t *);
static uint_t iwp_intr(caddr_t, caddr_t);
static int iwp_eep_load(iwp_sc_t *);
static void iwp_get_mac_from_eep(iwp_sc_t *);
static int iwp_eep_sem_down(iwp_sc_t *);
static void iwp_eep_sem_up(iwp_sc_t *);
static uint_t iwp_rx_softintr(caddr_t, caddr_t);
static uint8_t iwp_rate_to_plcp(int);
static int iwp_cmd(iwp_sc_t *, int, const void *, int, int);
static void iwp_set_led(iwp_sc_t *, uint8_t, uint8_t, uint8_t);
static int iwp_hw_set_before_auth(iwp_sc_t *);
static int iwp_scan(iwp_sc_t *);
static int iwp_config(iwp_sc_t *);
static void iwp_stop_master(iwp_sc_t *);
static int iwp_power_up(iwp_sc_t *);
static int iwp_preinit(iwp_sc_t *);
static int iwp_init(iwp_sc_t *);
static void iwp_stop(iwp_sc_t *);
static int iwp_quiesce(dev_info_t *t);
static void iwp_amrr_init(iwp_amrr_t *);
static void iwp_amrr_timeout(iwp_sc_t *);
static void iwp_amrr_ratectl(void *, ieee80211_node_t *);
static void iwp_ucode_alive(iwp_sc_t *, iwp_rx_desc_t *);
static void iwp_rx_phy_intr(iwp_sc_t *, iwp_rx_desc_t *);
static void iwp_rx_mpdu_intr(iwp_sc_t *, iwp_rx_desc_t *);
static void iwp_release_calib_buffer(iwp_sc_t *);
static int iwp_init_common(iwp_sc_t *);
static uint8_t *iwp_eep_addr_trans(iwp_sc_t *, uint32_t);
static int iwp_put_seg_fw(iwp_sc_t *, uint32_t, uint32_t, uint32_t);
static int iwp_alive_common(iwp_sc_t *);
static void iwp_save_calib_result(iwp_sc_t *, iwp_rx_desc_t *);
static int iwp_attach(dev_info_t *, ddi_attach_cmd_t);
static int iwp_detach(dev_info_t *, ddi_detach_cmd_t);
static void iwp_destroy_locks(iwp_sc_t *);
static int iwp_send(ieee80211com_t *, mblk_t *, uint8_t);
static void iwp_thread(iwp_sc_t *);
static int iwp_run_state_config(iwp_sc_t *);
static int iwp_fast_recover(iwp_sc_t *);
static void iwp_overwrite_ic_default(iwp_sc_t *);
static int iwp_add_ap_sta(iwp_sc_t *);
static int iwp_alloc_dma_mem(iwp_sc_t *, size_t,
ddi_dma_attr_t *, ddi_device_acc_attr_t *,
uint_t, iwp_dma_t *);
static void iwp_free_dma_mem(iwp_dma_t *);
static int iwp_eep_ver_chk(iwp_sc_t *);
static void iwp_set_chip_param(iwp_sc_t *);
/*
* GLD specific operations
*/
static int iwp_m_stat(void *, uint_t, uint64_t *);
static int iwp_m_start(void *);
static void iwp_m_stop(void *);
static int iwp_m_unicst(void *, const uint8_t *);
static int iwp_m_multicst(void *, boolean_t, const uint8_t *);
static int iwp_m_promisc(void *, boolean_t);
static mblk_t *iwp_m_tx(void *, mblk_t *);
static void iwp_m_ioctl(void *, queue_t *, mblk_t *);
static int iwp_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 iwp_m_getprop(void *arg, const char *pr_name,
mac_prop_id_t wldp_pr_num, uint_t wldp_length, void *wldp_buf);
static void iwp_m_propinfo(void *, const char *, mac_prop_id_t,
mac_prop_info_handle_t);
/*
* Supported rates for 802.11b/g modes (in 500Kbps unit).
*/
static const struct ieee80211_rateset iwp_rateset_11b =
{ 4, { 2, 4, 11, 22 } };
static const struct ieee80211_rateset iwp_rateset_11g =
{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
/*
* For mfthread only
*/
extern pri_t minclsyspri;
#define DRV_NAME_SP "iwp"
/*
* Module Loading Data & Entry Points
*/
DDI_DEFINE_STREAM_OPS(iwp_devops, nulldev, nulldev, iwp_attach,
iwp_detach, nodev, NULL, D_MP, NULL, iwp_quiesce);
static struct modldrv iwp_modldrv = {
&mod_driverops,
"Intel(R) PumaPeak driver(N)",
&iwp_devops
};
static struct modlinkage iwp_modlinkage = {
MODREV_1,
&iwp_modldrv,
NULL
};
int
_init(void)
{
int status;
status = ddi_soft_state_init(&iwp_soft_state_p,
sizeof (iwp_sc_t), 1);
if (status != DDI_SUCCESS) {
return (status);
}
mac_init_ops(&iwp_devops, DRV_NAME_SP);
status = mod_install(&iwp_modlinkage);
if (status != DDI_SUCCESS) {
mac_fini_ops(&iwp_devops);
ddi_soft_state_fini(&iwp_soft_state_p);
}
return (status);
}
int
_fini(void)
{
int status;
status = mod_remove(&iwp_modlinkage);
if (DDI_SUCCESS == status) {
mac_fini_ops(&iwp_devops);
ddi_soft_state_fini(&iwp_soft_state_p);
}
return (status);
}
int
_info(struct modinfo *mip)
{
return (mod_info(&iwp_modlinkage, mip));
}
/*
* Mac Call Back entries
*/
mac_callbacks_t iwp_m_callbacks = {
MC_IOCTL | MC_SETPROP | MC_GETPROP | MC_PROPINFO,
iwp_m_stat,
iwp_m_start,
iwp_m_stop,
iwp_m_promisc,
iwp_m_multicst,
iwp_m_unicst,
iwp_m_tx,
NULL,
iwp_m_ioctl,
NULL,
NULL,
NULL,
iwp_m_setprop,
iwp_m_getprop,
iwp_m_propinfo
};
#ifdef DEBUG
void
iwp_dbg(uint32_t flags, const char *fmt, ...)
{
va_list ap;
if (flags & iwp_dbg_flags) {
va_start(ap, fmt);
vcmn_err(CE_NOTE, fmt, ap);
va_end(ap);
}
}
#endif /* DEBUG */
/*
* device operations
*/
int
iwp_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
iwp_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(iwp_soft_state_p,
instance);
ASSERT(sc != NULL);
if (sc->sc_flags & IWP_F_RUNNING) {
(void) iwp_init(sc);
}
atomic_and_32(&sc->sc_flags, ~IWP_F_SUSPEND);
IWP_DBG((IWP_DEBUG_RESUME, "iwp_attach(): "
"resume\n"));
return (DDI_SUCCESS);
default:
goto attach_fail1;
}
instance = ddi_get_instance(dip);
err = ddi_soft_state_zalloc(iwp_soft_state_p, instance);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_attach(): "
"failed to allocate soft state\n");
goto attach_fail1;
}
sc = ddi_get_soft_state(iwp_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,
&iwp_reg_accattr, &sc->sc_cfg_handle);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_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 != 0x422B) &&
(sc->sc_dev_id != 0x422C) &&
(sc->sc_dev_id != 0x4238) &&
(sc->sc_dev_id != 0x4239) &&
(sc->sc_dev_id != 0x008d) &&
(sc->sc_dev_id != 0x008e)) {
cmn_err(CE_WARN, "iwp_attach(): "
"Do not support this device\n");
goto attach_fail3;
}
iwp_set_chip_param(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(0x1000 + 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, &iwp_reg_accattr, &sc->sc_handle);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_attach(): "
"failed to map device regs\n");
goto attach_fail3;
}
/*
* this is used to differentiate type of hardware
*/
sc->sc_hw_rev = IWP_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, "iwp_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, "iwp_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, "iwp_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, "iwp_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 = iwp_alloc_shared(sc);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_attach(): "
"failed to allocate shared page\n");
goto attach_fail7;
}
(void) memset(sc->sc_shared, 0, sizeof (iwp_shared_t));
/*
* Allocate keep warm page.
*/
err = iwp_alloc_kw(sc);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_attach(): "
"failed to allocate keep warm page\n");
goto attach_fail8;
}
/*
* Do some necessary hardware initializations.
*/
err = iwp_preinit(sc);
if (err != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_attach(): "
"failed to initialize hardware\n");
goto attach_fail9;
}
/*
* get hardware configurations from eeprom
*/
err = iwp_eep_load(sc);
if (err != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_attach(): "
"failed to load eeprom\n");
goto attach_fail9;
}
/*
* calibration information from EEPROM
*/
sc->sc_eep_calib = (struct iwp_eep_calibration *)
iwp_eep_addr_trans(sc, EEP_CALIBRATION);
err = iwp_eep_ver_chk(sc);
if (err != IWP_SUCCESS) {
goto attach_fail9;
}
/*
* get MAC address of this chipset
*/
iwp_get_mac_from_eep(sc);
/*
* initialize TX and RX ring buffers
*/
err = iwp_ring_init(sc);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_attach(): "
"failed to allocate and initialize ring\n");
goto attach_fail9;
}
sc->sc_hdr = (iwp_firmware_hdr_t *)iwp_fw_bin;
/*
* copy ucode to dma buffer
*/
err = iwp_alloc_fw_dma(sc);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_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_OFDM;
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;
/*
* set supported .11b and .11g rates
*/
ic->ic_sup_rates[IEEE80211_MODE_11B] = iwp_rateset_11b;
ic->ic_sup_rates[IEEE80211_MODE_11G] = iwp_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;
}
ic->ic_ibss_chan = &ic->ic_sup_channels[0];
ic->ic_xmit = iwp_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.
*/
iwp_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,
iwp_rx_softintr, (caddr_t)sc);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_attach(): "
"add soft interrupt failed\n");
goto attach_fail12;
}
err = ddi_intr_add_handler(sc->sc_intr_htable[0], iwp_intr,
(caddr_t)sc, NULL);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_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, "iwp_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, "iwp_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 = &iwp_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, "iwp_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, "iwp_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,
iwp_thread, sc, 0, &p0, TS_RUN, minclsyspri);
}
atomic_or_32(&sc->sc_flags, IWP_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:
iwp_free_fw_dma(sc);
attach_fail10:
iwp_ring_free(sc);
attach_fail9:
iwp_free_kw(sc);
attach_fail8:
iwp_free_shared(sc);
attach_fail7:
iwp_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(iwp_soft_state_p, instance);
attach_fail1:
return (DDI_FAILURE);
}
int
iwp_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
iwp_sc_t *sc;
ieee80211com_t *ic;
int err;
sc = ddi_get_soft_state(iwp_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, ~IWP_F_HW_ERR_RECOVER);
atomic_and_32(&sc->sc_flags, ~IWP_F_RATE_AUTO_CTL);
atomic_or_32(&sc->sc_flags, IWP_F_SUSPEND);
if (sc->sc_flags & IWP_F_RUNNING) {
iwp_stop(sc);
ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
}
IWP_DBG((IWP_DEBUG_RESUME, "iwp_detach(): "
"suspend\n"));
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
if (!(sc->sc_flags & IWP_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
*/
iwp_stop(sc);
DELAY(500000);
/*
* release buffer for calibration
*/
iwp_release_calib_buffer(sc);
/*
* Unregiste from GLD
*/
(void) mac_unregister(sc->sc_ic.ic_mach);
mutex_enter(&sc->sc_glock);
iwp_free_fw_dma(sc);
iwp_ring_free(sc);
iwp_free_kw(sc);
iwp_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);
iwp_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(iwp_soft_state_p, ddi_get_instance(dip));
return (DDI_SUCCESS);
}
/*
* destroy all locks
*/
static void
iwp_destroy_locks(iwp_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
iwp_alloc_dma_mem(iwp_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, iwp_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
iwp_free_dma_mem(iwp_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
iwp_alloc_fw_dma(iwp_sc_t *sc)
{
int err = DDI_FAILURE;
iwp_dma_t *dma_p;
char *t;
/*
* firmware image layout:
* |HDR|<-TEXT->|<-DATA->|<-INIT_TEXT->|<-INIT_DATA->|<-BOOT->|
*/
/*
* Check firmware image size.
*/
if (LE_32(sc->sc_hdr->init_textsz) > RTC_INST_SIZE) {
cmn_err(CE_WARN, "iwp_alloc_fw_dma(): "
"firmware init text size 0x%x is too large\n",
LE_32(sc->sc_hdr->init_textsz));
goto fail;
}
if (LE_32(sc->sc_hdr->init_datasz) > RTC_DATA_SIZE) {
cmn_err(CE_WARN, "iwp_alloc_fw_dma(): "
"firmware init data size 0x%x is too large\n",
LE_32(sc->sc_hdr->init_datasz));
goto fail;
}
if (LE_32(sc->sc_hdr->textsz) > RTC_INST_SIZE) {
cmn_err(CE_WARN, "iwp_alloc_fw_dma(): "
"firmware text size 0x%x is too large\n",
LE_32(sc->sc_hdr->textsz));
goto fail;
}
if (LE_32(sc->sc_hdr->datasz) > RTC_DATA_SIZE) {
cmn_err(CE_WARN, "iwp_alloc_fw_dma(): "
"firmware data size 0x%x is too large\n",
LE_32(sc->sc_hdr->datasz));
goto fail;
}
/*
* copy text of runtime ucode
*/
t = (char *)(sc->sc_hdr + 1);
err = iwp_alloc_dma_mem(sc, LE_32(sc->sc_hdr->textsz),
&fw_dma_attr, &iwp_dma_accattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&sc->sc_dma_fw_text);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_alloc_fw_dma(): "
"failed to allocate text dma memory.\n");
goto fail;
}
dma_p = &sc->sc_dma_fw_text;
IWP_DBG((IWP_DEBUG_DMA, "iwp_alloc_fw_dma(): "
"text[ncookies:%d addr:%lx size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
(void) memcpy(dma_p->mem_va, t, LE_32(sc->sc_hdr->textsz));
/*
* copy data and bak-data of runtime ucode
*/
t += LE_32(sc->sc_hdr->textsz);
err = iwp_alloc_dma_mem(sc, LE_32(sc->sc_hdr->datasz),
&fw_dma_attr, &iwp_dma_accattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&sc->sc_dma_fw_data);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_alloc_fw_dma(): "
"failed to allocate data dma memory\n");
goto fail;
}
dma_p = &sc->sc_dma_fw_data;
IWP_DBG((IWP_DEBUG_DMA, "iwp_alloc_fw_dma(): "
"data[ncookies:%d addr:%lx size:%lx]\n",
dma_p->ncookies, dma_p->cookie.dmac_address,
dma_p->cookie.dmac_size));
(void) memcpy(dma_p->mem_va, t, LE_32(sc->sc_hdr->datasz));
err = iwp_alloc_dma_mem(sc, LE_32(sc->sc_hdr->datasz),
&fw_dma_attr, &iwp_dma_accattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&sc->sc_dma_fw_data_bak);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_alloc_fw_dma(): "
"failed to allocate data bakup dma memory\n");
goto fail;
}
dma_p = &sc->sc_dma_fw_data_bak;
IWP_DBG((IWP_DEBUG_DMA, "iwp_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));
(void) memcpy(dma_p->mem_va, t, LE_32(sc->sc_hdr->datasz));
/*
* copy text of init ucode
*/
t += LE_32(sc->sc_hdr->datasz);
err = iwp_alloc_dma_mem(sc, LE_32(sc->sc_hdr->init_textsz),
&fw_dma_attr, &iwp_dma_accattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&sc->sc_dma_fw_init_text);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_alloc_fw_dma(): "
"failed to allocate init text dma memory\n");
goto fail;
}
dma_p = &sc->sc_dma_fw_init_text;
IWP_DBG((IWP_DEBUG_DMA, "iwp_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));
(void) memcpy(dma_p->mem_va, t, LE_32(sc->sc_hdr->init_textsz));
/*
* copy data of init ucode
*/
t += LE_32(sc->sc_hdr->init_textsz);
err = iwp_alloc_dma_mem(sc, LE_32(sc->sc_hdr->init_datasz),
&fw_dma_attr, &iwp_dma_accattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&sc->sc_dma_fw_init_data);
if (err != DDI_SUCCESS) {
cmn_err(CE_WARN, "iwp_alloc_fw_dma(): "
"failed to allocate init data dma memory\n");
goto fail;
}
dma_p = &sc->sc_dma_fw_init_data;
IWP_DBG((IWP_DEBUG_DMA, "iwp_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));
(void) memcpy(dma_p->mem_va, t, LE_32(sc->sc_hdr->init_datasz));
sc->sc_boot = t + LE_32(sc->sc_hdr->init_datasz);
fail:
return (err);
}
static void
iwp_free_fw_dma(iwp_sc_t *sc)
{
iwp_free_dma_mem(&sc->sc_dma_fw_text);
iwp_free_dma_mem(&sc->sc_dma_fw_data);
iwp_free_dma_mem(&sc->sc_dma_fw_data_bak);
iwp_free_dma_mem(&sc->sc_dma_fw_init_text);
iwp_free_dma_mem(&sc->sc_dma_fw_init_data);
}
/*
* Allocate a shared buffer between host and NIC.
*/
static int
iwp_alloc_shared(iwp_sc_t *sc)
{
#ifdef DEBUG
iwp_dma_t *dma_p;
#endif
int err = DDI_FAILURE;
/*
* must be aligned on a 4K-page boundary
*/
err = iwp_alloc_dma_mem(sc, sizeof (iwp_shared_t),
&sh_dma_attr, &iwp_dma_descattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&sc->sc_dma_sh);
if (err != DDI_SUCCESS) {
goto fail;
}
sc->sc_shared = (iwp_shared_t *)sc->sc_dma_sh.mem_va;
#ifdef DEBUG
dma_p = &sc->sc_dma_sh;
#endif
IWP_DBG((IWP_DEBUG_DMA, "iwp_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:
iwp_free_shared(sc);
return (err);
}
static void
iwp_free_shared(iwp_sc_t *sc)
{
iwp_free_dma_mem(&sc->sc_dma_sh);
}
/*
* Allocate a keep warm page.
*/
static int
iwp_alloc_kw(iwp_sc_t *sc)
{
#ifdef DEBUG
iwp_dma_t *dma_p;
#endif
int err = DDI_FAILURE;
/*
* must be aligned on a 4K-page boundary
*/
err = iwp_alloc_dma_mem(sc, IWP_KW_SIZE,
&kw_dma_attr, &iwp_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
IWP_DBG((IWP_DEBUG_DMA, "iwp_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:
iwp_free_kw(sc);
return (err);
}
static void
iwp_free_kw(iwp_sc_t *sc)
{
iwp_free_dma_mem(&sc->sc_dma_kw);
}
/*
* initialize RX ring buffers
*/
static int
iwp_alloc_rx_ring(iwp_sc_t *sc)
{
iwp_rx_ring_t *ring;
iwp_rx_data_t *data;
#ifdef DEBUG
iwp_dma_t *dma_p;
#endif
int i, err = DDI_FAILURE;
ring = &sc->sc_rxq;
ring->cur = 0;
/*
* allocate RX description ring buffer
*/
err = iwp_alloc_dma_mem(sc, RX_QUEUE_SIZE * sizeof (uint32_t),
&ring_desc_dma_attr, &iwp_dma_descattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&ring->dma_desc);
if (err != DDI_SUCCESS) {
IWP_DBG((IWP_DEBUG_DMA, "iwp_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
IWP_DBG((IWP_DEBUG_DMA, "iwp_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 = iwp_alloc_dma_mem(sc, sc->sc_dmabuf_sz,
&rx_buffer_dma_attr, &iwp_dma_accattr,
DDI_DMA_READ | DDI_DMA_STREAMING,
&data->dma_data);
if (err != DDI_SUCCESS) {
IWP_DBG((IWP_DEBUG_DMA, "iwp_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
IWP_DBG((IWP_DEBUG_DMA, "iwp_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));
IWP_DMA_SYNC(ring->dma_desc, DDI_DMA_SYNC_FORDEV);
return (err);
fail:
iwp_free_rx_ring(sc);
return (err);
}
/*
* disable RX ring
*/
static void
iwp_reset_rx_ring(iwp_sc_t *sc)
{
int n;
iwp_mac_access_enter(sc);
IWP_WRITE(sc, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
for (n = 0; n < 2000; n++) {
if (IWP_READ(sc, FH_MEM_RSSR_RX_STATUS_REG) & (1 << 24)) {
break;
}
DELAY(1000);
}
#ifdef DEBUG
if (2000 == n) {
IWP_DBG((IWP_DEBUG_DMA, "iwp_reset_rx_ring(): "
"timeout resetting Rx ring\n"));
}
#endif
iwp_mac_access_exit(sc);
sc->sc_rxq.cur = 0;
}
static void
iwp_free_rx_ring(iwp_sc_t *sc)
{
int i;
for (i = 0; i < RX_QUEUE_SIZE; i++) {
if (sc->sc_rxq.data[i].dma_data.dma_hdl) {
IWP_DMA_SYNC(sc->sc_rxq.data[i].dma_data,
DDI_DMA_SYNC_FORCPU);
}
iwp_free_dma_mem(&sc->sc_rxq.data[i].dma_data);
}
if (sc->sc_rxq.dma_desc.dma_hdl) {
IWP_DMA_SYNC(sc->sc_rxq.dma_desc, DDI_DMA_SYNC_FORDEV);
}
iwp_free_dma_mem(&sc->sc_rxq.dma_desc);
}
/*
* initialize TX ring buffers
*/
static int
iwp_alloc_tx_ring(iwp_sc_t *sc, iwp_tx_ring_t *ring,
int slots, int qid)
{
iwp_tx_data_t *data;
iwp_tx_desc_t *desc_h;
uint32_t paddr_desc_h;
iwp_cmd_t *cmd_h;
uint32_t paddr_cmd_h;
#ifdef DEBUG
iwp_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 = iwp_alloc_dma_mem(sc,
TFD_QUEUE_SIZE_MAX * sizeof (iwp_tx_desc_t),
&ring_desc_dma_attr, &iwp_dma_descattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&ring->dma_desc);
if (err != DDI_SUCCESS) {
IWP_DBG((IWP_DEBUG_DMA, "iwp_alloc_tx_ring(): "
"dma alloc tx ring desc[%d] "
"failed\n", qid));
goto fail;
}
#ifdef DEBUG
dma_p = &ring->dma_desc;
#endif
IWP_DBG((IWP_DEBUG_DMA, "iwp_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 = (iwp_tx_desc_t *)ring->dma_desc.mem_va;
paddr_desc_h = ring->dma_desc.cookie.dmac_address;
/*
* allocate buffer for ucode command
*/
err = iwp_alloc_dma_mem(sc,
TFD_QUEUE_SIZE_MAX * sizeof (iwp_cmd_t),
&cmd_dma_attr, &iwp_dma_accattr,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
&ring->dma_cmd);
if (err != DDI_SUCCESS) {
IWP_DBG((IWP_DEBUG_DMA, "iwp_alloc_tx_ring(): "
"dma alloc tx ring cmd[%d]"
" failed\n", qid));
goto fail;
}
#ifdef DEBUG
dma_p = &ring->dma_cmd;
#endif
IWP_DBG((IWP_DEBUG_DMA, "iwp_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 = (iwp_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 (iwp_tx_data_t) * TFD_QUEUE_SIZE_MAX,
KM_NOSLEEP);
if (NULL == ring->data) {
IWP_DBG((IWP_DEBUG_DMA, "iwp_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 = iwp_alloc_dma_mem(sc, sc->sc_dmabuf_sz,
&tx_buffer_dma_attr, &iwp_dma_accattr,
DDI_DMA_WRITE | DDI_DMA_STREAMING,
&data->dma_data);
if (err != DDI_SUCCESS) {
IWP_DBG((IWP_DEBUG_DMA, "iwp_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
IWP_DBG((IWP_DEBUG_DMA, "iwp_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:
iwp_free_tx_ring(ring);
return (err);
}
/*
* disable TX ring
*/
static void
iwp_reset_tx_ring(iwp_sc_t *sc, iwp_tx_ring_t *ring)
{
iwp_tx_data_t *data;
int i, n;
iwp_mac_access_enter(sc);
IWP_WRITE(sc, IWP_FH_TCSR_CHNL_TX_CONFIG_REG(ring->qid), 0);
for (n = 0; n < 200; n++) {
if (IWP_READ(sc, IWP_FH_TSSR_TX_STATUS_REG) &
IWP_FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ring->qid)) {
break;
}
DELAY(10);
}
#ifdef DEBUG
if (200 == n) {
IWP_DBG((IWP_DEBUG_DMA, "iwp_reset_tx_ring(): "
"timeout reset tx ring %d\n",
ring->qid));
}
#endif
iwp_mac_access_exit(sc);
/* by pass, if it's quiesce */
if (!(sc->sc_flags & IWP_F_QUIESCED)) {
for (i = 0; i < ring->count; i++) {
data = &ring->data[i];
IWP_DMA_SYNC(data->dma_data, DDI_DMA_SYNC_FORDEV);
}
}
ring->queued = 0;
ring->cur = 0;
ring->desc_cur = 0;
}
static void
iwp_free_tx_ring(iwp_tx_ring_t *ring)
{
int i;
if (ring->dma_desc.dma_hdl != NULL) {
IWP_DMA_SYNC(ring->dma_desc, DDI_DMA_SYNC_FORDEV);
}
iwp_free_dma_mem(&ring->dma_desc);
if (ring->dma_cmd.dma_hdl != NULL) {
IWP_DMA_SYNC(ring->dma_cmd, DDI_DMA_SYNC_FORDEV);
}
iwp_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) {
IWP_DMA_SYNC(ring->data[i].dma_data,
DDI_DMA_SYNC_FORDEV);
}
iwp_free_dma_mem(&ring->data[i].dma_data);
}
kmem_free(ring->data, ring->count * sizeof (iwp_tx_data_t));
}
}
/*
* initialize TX and RX ring
*/
static int
iwp_ring_init(iwp_sc_t *sc)
{
int i, err = DDI_FAILURE;
for (i = 0; i < IWP_NUM_QUEUES; i++) {
if (IWP_CMD_QUEUE_NUM == i) {
continue;
}
err = iwp_alloc_tx_ring(sc, &sc->sc_txq[i], TFD_TX_CMD_SLOTS,
i);
if (err != DDI_SUCCESS) {
goto fail;
}
}
/*
* initialize command queue
*/
err = iwp_alloc_tx_ring(sc, &sc->sc_txq[IWP_CMD_QUEUE_NUM],
TFD_CMD_SLOTS, IWP_CMD_QUEUE_NUM);
if (err != DDI_SUCCESS) {
goto fail;
}
err = iwp_alloc_rx_ring(sc);
if (err != DDI_SUCCESS) {
goto fail;
}
fail:
return (err);
}
static void
iwp_ring_free(iwp_sc_t *sc)
{
int i = IWP_NUM_QUEUES;
iwp_free_rx_ring(sc);
while (--i >= 0) {
iwp_free_tx_ring(&sc->sc_txq[i]);
}
}
/* ARGSUSED */
static ieee80211_node_t *
iwp_node_alloc(ieee80211com_t *ic)
{
iwp_amrr_t *amrr;
amrr = kmem_zalloc(sizeof (iwp_amrr_t), KM_SLEEP);
if (NULL == amrr) {
cmn_err(CE_WARN, "iwp_node_alloc(): "
"failed to allocate memory for amrr structure\n");
return (NULL);
}
iwp_amrr_init(amrr);
return (&amrr->in);
}
static void
iwp_node_free(ieee80211_node_t *in)
{
ieee80211com_t *ic;
if ((NULL == in) ||
(NULL == in->in_ic)) {
cmn_err(CE_WARN, "iwp_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 (iwp_amrr_t));
}
/*
* change station's state. this function will be invoked by 80211 module
* when need to change staton's state.
*/
static int
iwp_newstate(ieee80211com_t *ic, enum ieee80211_state nstate, int arg)
{
iwp_sc_t *sc;
ieee80211_node_t *in;
enum ieee80211_state ostate;
iwp_add_sta_t node;
int i, err = IWP_FAIL;
if (NULL == ic) {
return (err);
}
sc = (iwp_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, IWP_F_SCANNING);
iwp_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);
IWP_DBG((IWP_DEBUG_80211, "iwp_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 = iwp_cmd(sc, REPLY_RXON, &sc->sc_config,
sizeof (iwp_rxon_cmd_t), 1);
if (err != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_newstate(): "
"could not clear association\n");
atomic_and_32(&sc->sc_flags, ~IWP_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 = IWP_BROADCAST_ID;
err = iwp_cmd(sc, REPLY_ADD_STA, &node,
sizeof (node), 1);
if (err != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_newstate(): "
"could not add broadcast node\n");
atomic_and_32(&sc->sc_flags, ~IWP_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 = iwp_scan(sc)) != 0)) {
cmn_err(CE_WARN, "iwp_newstate(): "
"could not initiate scan\n");
atomic_and_32(&sc->sc_flags, ~IWP_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, ~IWP_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 = iwp_hw_set_before_auth(sc)) != 0) {
IWP_DBG((IWP_DEBUG_80211, "iwp_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, ~IWP_F_SCANNING);
}
if (IEEE80211_M_MONITOR == ic->ic_opmode) {
/* let LED blink when monitoring */
iwp_set_led(sc, 2, 10, 10);
break;
}
IWP_DBG((IWP_DEBUG_80211, "iwp_newstate(): "
"associated.\n"));
err = iwp_run_state_config(sc);
if (err != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_newstate(): "
"failed to set up association\n");
mutex_exit(&sc->sc_glock);
return (err);
}
/*
* start automatic rate control
*/
if (IEEE80211_FIXED_RATE_NONE == ic->ic_fixed_rate) {
atomic_or_32(&sc->sc_flags, IWP_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, ~IWP_F_RATE_AUTO_CTL);
}
/*
* set LED on after associated
*/
iwp_set_led(sc, 2, 0, 1);
break;
case IEEE80211_S_INIT:
if (ostate == IEEE80211_S_SCAN) {
atomic_and_32(&sc->sc_flags, ~IWP_F_SCANNING);
}
/*
* set LED off after init
*/
iwp_set_led(sc, 2, 1, 0);
break;
case IEEE80211_S_ASSOC:
if (ostate == IEEE80211_S_SCAN) {
atomic_and_32(&sc->sc_flags, ~IWP_F_SCANNING);
}
break;
}
mutex_exit(&sc->sc_glock);
return (sc->sc_newstate(ic, nstate, arg));
}
/*
* exclusive access to mac begin.
*/
static void
iwp_mac_access_enter(iwp_sc_t *sc)
{
uint32_t tmp;
int n;
tmp = IWP_READ(sc, CSR_GP_CNTRL);
IWP_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 ((IWP_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) {
IWP_DBG((IWP_DEBUG_PIO, "iwp_mac_access_enter(): "
"could not lock memory\n"));
}
#endif
}
/*
* exclusive access to mac end.
*/
static void
iwp_mac_access_exit(iwp_sc_t *sc)
{
uint32_t tmp = IWP_READ(sc, CSR_GP_CNTRL);
IWP_WRITE(sc, CSR_GP_CNTRL,
tmp & ~CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
}
/*
* this function defined here for future use.
* static uint32_t
* iwp_mem_read(iwp_sc_t *sc, uint32_t addr)
* {
* IWP_WRITE(sc, HBUS_TARG_MEM_RADDR, addr);
* return (IWP_READ(sc, HBUS_TARG_MEM_RDAT));
* }
*/
/*
* write mac memory
*/
static void
iwp_mem_write(iwp_sc_t *sc, uint32_t addr, uint32_t data)
{
IWP_WRITE(sc, HBUS_TARG_MEM_WADDR, addr);
IWP_WRITE(sc, HBUS_TARG_MEM_WDAT, data);
}
/*
* read mac register
*/
static uint32_t
iwp_reg_read(iwp_sc_t *sc, uint32_t addr)
{
IWP_WRITE(sc, HBUS_TARG_PRPH_RADDR, addr | (3 << 24));
return (IWP_READ(sc, HBUS_TARG_PRPH_RDAT));
}
/*
* write mac register
*/
static void
iwp_reg_write(iwp_sc_t *sc, uint32_t addr, uint32_t data)
{
IWP_WRITE(sc, HBUS_TARG_PRPH_WADDR, addr | (3 << 24));
IWP_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
iwp_load_init_firmware(iwp_sc_t *sc)
{
int err = IWP_FAIL;
clock_t clk;
atomic_and_32(&sc->sc_flags, ~IWP_F_PUT_SEG);
/*
* load init_text section of uCode to hardware
*/
err = iwp_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 != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_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 & IWP_F_PUT_SEG)) {
if (cv_timedwait(&sc->sc_put_seg_cv, &sc->sc_glock, clk) < 0) {
break;
}
}
if (!(sc->sc_flags & IWP_F_PUT_SEG)) {
cmn_err(CE_WARN, "iwp_load_init_firmware(): "
"timeout waiting for init uCode load.\n");
return (IWP_FAIL);
}
atomic_and_32(&sc->sc_flags, ~IWP_F_PUT_SEG);
/*
* load init_data section of uCode to hardware
*/
err = iwp_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 != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_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 & IWP_F_PUT_SEG)) {
if (cv_timedwait(&sc->sc_put_seg_cv, &sc->sc_glock, clk) < 0) {
break;
}
}
if (!(sc->sc_flags & IWP_F_PUT_SEG)) {
cmn_err(CE_WARN, "iwp_load_init_firmware(): "
"timeout waiting for init_data uCode load.\n");
return (IWP_FAIL);
}
atomic_and_32(&sc->sc_flags, ~IWP_F_PUT_SEG);
return (err);
}
static int
iwp_load_run_firmware(iwp_sc_t *sc)
{
int err = IWP_FAIL;
clock_t clk;
atomic_and_32(&sc->sc_flags, ~IWP_F_PUT_SEG);
/*
* load init_text section of uCode to hardware
*/
err = iwp_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 != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_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 & IWP_F_PUT_SEG)) {
if (cv_timedwait(&sc->sc_put_seg_cv, &sc->sc_glock, clk) < 0) {
break;
}
}
if (!(sc->sc_flags & IWP_F_PUT_SEG)) {
cmn_err(CE_WARN, "iwp_load_run_firmware(): "
"timeout waiting for run uCode load.\n");
return (IWP_FAIL);
}
atomic_and_32(&sc->sc_flags, ~IWP_F_PUT_SEG);
/*
* load run_data section of uCode to hardware
*/
err = iwp_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 != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_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 & IWP_F_PUT_SEG)) {
if (cv_timedwait(&sc->sc_put_seg_cv, &sc->sc_glock, clk) < 0) {
break;
}
}
if (!(sc->sc_flags & IWP_F_PUT_SEG)) {
cmn_err(CE_WARN, "iwp_load_run_firmware(): "
"timeout waiting for run_data uCode load.\n");
return (IWP_FAIL);
}
atomic_and_32(&sc->sc_flags, ~IWP_F_PUT_SEG);
return (err);
}
/*
* this function will be invoked to receive phy information
* when a frame is received.
*/
static void
iwp_rx_phy_intr(iwp_sc_t *sc, iwp_rx_desc_t *desc)
{
sc->sc_rx_phy_res.flag = 1;
(void) memcpy(sc->sc_rx_phy_res.buf, (uint8_t *)(desc + 1),
sizeof (iwp_rx_phy_res_t));
}
/*
* this function will be invoked to receive body of frame when
* a frame is received.
*/
static void
iwp_rx_mpdu_intr(iwp_sc_t *sc, iwp_rx_desc_t *desc)
{
ieee80211com_t *ic = &sc->sc_ic;
#ifdef DEBUG
iwp_rx_ring_t *ring = &sc->sc_rxq;
#endif
struct ieee80211_frame *wh;
struct iwp_rx_non_cfg_phy *phyinfo;
struct iwp_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;
iwp_rx_phy_res_t *stat;
ieee80211_node_t *in;
/*
* assuming not 11n here. cope with 11n in phase-II
*/
mpdu_size = (struct iwp_rx_mpdu_body_size *)(desc + 1);
stat = (iwp_rx_phy_res_t *)sc->sc_rx_phy_res.buf;
if (stat->cfg_phy_cnt > 20) {
return;
}
phyinfo = (struct iwp_rx_non_cfg_phy *)stat->non_cfg_phy;
temp = LE_32(phyinfo->non_cfg_phy[IWP_RX_RES_AGC_IDX]);
agc = (temp & IWP_OFDM_AGC_MSK) >> IWP_OFDM_AGC_BIT_POS;
temp = LE_32(phyinfo->non_cfg_phy[IWP_RX_RES_RSSI_AB_IDX]);
arssi = (temp & IWP_OFDM_RSSI_A_MSK) >> IWP_OFDM_RSSI_A_BIT_POS;
brssi = (temp & IWP_OFDM_RSSI_B_MSK) >> IWP_OFDM_RSSI_B_BIT_POS;
temp = LE_32(phyinfo->non_cfg_phy[IWP_RX_RES_RSSI_C_IDX]);
crssi = (temp & IWP_OFDM_RSSI_C_MSK) >> IWP_OFDM_RSSI_C_BIT_POS;
mrssi = MAX(arssi, brssi);
mrssi = MAX(mrssi, crssi);
t = mrssi - agc - IWP_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 iwp_rx_mpdu_body_size) + len);
bcopy(tail, &crc, 4);
IWP_DBG((IWP_DEBUG_RX, "iwp_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)) {
IWP_DBG((IWP_DEBUG_RX, "iwp_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)) {
IWP_DBG((IWP_DEBUG_RX, "iwp_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 iwp_rx_mpdu_body_size));
if (IEEE80211_FC0_SUBTYPE_ASSOC_RESP == *(uint8_t *)wh) {
sc->sc_assoc_id = *((uint16_t *)(wh + 1) + 2);
IWP_DBG((IWP_DEBUG_RX, "iwp_rx_mpdu_intr(): "
"rx : association id = %x\n",
sc->sc_assoc_id));
}
#ifdef DEBUG
if (iwp_dbg_flags & IWP_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) {
(void) memcpy(mp->b_wptr, wh, 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++;
IWP_DBG((IWP_DEBUG_RX, "iwp_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
iwp_tx_intr(iwp_sc_t *sc, iwp_rx_desc_t *desc)
{
ieee80211com_t *ic = &sc->sc_ic;
iwp_tx_ring_t *ring = &sc->sc_txq[desc->hdr.qid & 0x3];
iwp_tx_stat_t *stat = (iwp_tx_stat_t *)(desc + 1);
iwp_amrr_t *amrr;
if (NULL == ic->ic_bss) {
return;
}
amrr = (iwp_amrr_t *)ic->ic_bss;
amrr->txcnt++;
IWP_DBG((IWP_DEBUG_RATECTL, "iwp_tx_intr(): "
"tx: %d cnt\n", amrr->txcnt));
if (stat->ntries > 0) {
amrr->retrycnt++;
sc->sc_tx_retries++;
IWP_DBG((IWP_DEBUG_TX, "iwp_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
iwp_cmd_intr(iwp_sc_t *sc, iwp_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);
IWP_DBG((IWP_DEBUG_CMD, "iwp_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
iwp_ucode_alive(iwp_sc_t *sc, iwp_rx_desc_t *desc)
{
uint32_t rv;
struct iwp_calib_cfg_cmd cmd;
struct iwp_alive_resp *ar =
(struct iwp_alive_resp *)(desc + 1);
struct iwp_calib_results *res_p = &sc->sc_calib_results;
/*
* the microcontroller is ready
*/
IWP_DBG((IWP_DEBUG_FW, "iwp_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) {
IWP_DBG((IWP_DEBUG_FW, "iwp_ucode_alive(): "
"microcontroller initialization failed\n"));
}
#endif
/*
* determine if init alive or runtime alive.
*/
if (INITIALIZE_SUBTYPE == ar->ver_subtype) {
IWP_DBG((IWP_DEBUG_FW, "iwp_ucode_alive(): "
"initialization alive received.\n"));
(void) memcpy(&sc->sc_card_alive_init, ar,
sizeof (struct iwp_init_alive_resp));
/*
* necessary configuration to NIC
*/
mutex_enter(&sc->sc_glock);
rv = iwp_alive_common(sc);
if (rv != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_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 = IWP_CALIB_INIT_CFG_ALL;
cmd.ucd_calib_cfg.once.start = IWP_CALIB_INIT_CFG_ALL;
cmd.ucd_calib_cfg.once.send_res = IWP_CALIB_INIT_CFG_ALL;
cmd.ucd_calib_cfg.flags = IWP_CALIB_INIT_CFG_ALL;
/*
* require ucode execute calibration
*/
rv = iwp_cmd(sc, CALIBRATION_CFG_CMD, &cmd, sizeof (cmd), 1);
if (rv != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_ucode_alive(): "
"failed to send calibration configure command.\n");
mutex_exit(&sc->sc_glock);
return;
}
mutex_exit(&sc->sc_glock);
} else { /* runtime alive */
IWP_DBG((IWP_DEBUG_FW, "iwp_ucode_alive(): "
"runtime alive received.\n"));
(void) memcpy(&sc->sc_card_alive_run, ar,
sizeof (struct iwp_alive_resp));
mutex_enter(&sc->sc_glock);
/*
* necessary configuration to NIC
*/
rv = iwp_alive_common(sc);
if (rv != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_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 = iwp_cmd(sc, REPLY_PHY_CALIBRATION_CMD,
res_p->lo_res, res_p->lo_res_len, 1);
if (rv != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_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 = iwp_cmd(sc, REPLY_PHY_CALIBRATION_CMD,
res_p->tx_iq_res, res_p->tx_iq_res_len, 1);
if (rv != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_ucode_alive(): "
"failed to send TX IQ"
"calibration command.\n");
mutex_exit(&sc->sc_glock);
return;
}
DELAY(1000);
}
/*
* send the result of TX IQ perd calibration to uCode.
*/
if (res_p->tx_iq_perd_res != NULL) {
rv = iwp_cmd(sc, REPLY_PHY_CALIBRATION_CMD,
res_p->tx_iq_perd_res,
res_p->tx_iq_perd_res_len, 1);
if (rv != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_ucode_alive(): "
"failed to send TX IQ perd"
"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 = iwp_cmd(sc, REPLY_PHY_CALIBRATION_CMD,
res_p->base_band_res,
res_p->base_band_res_len, 1);
if (rv != IWP_SUCCESS) {
cmn_err(CE_WARN, "iwp_ucode_alive(): "
"failed to send Base Band"
"calibration command.\n");
mutex_exit(&sc->sc_glock);
return;
}
DELAY(1000);
}
atomic_or_32(&sc->sc_flags, IWP_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
iwp_rx_softintr(caddr_t arg, caddr_t unused)
{
iwp_sc_t *sc;
ieee80211com_t *ic;
iwp_rx_desc_t *desc;
iwp_rx_data_t *data;
uint32_t index;
if (NULL == arg) {
return (DDI_INTR_UNCLAIMED);
}
sc = (iwp_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 = (iwp_rx_desc_t *)data->dma_data.mem_va;
IWP_DBG((IWP_DEBUG_INTR, "iwp_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)) {
iwp_cmd_intr(sc, desc);
}
switch (desc->hdr.type) {
case REPLY_RX_PHY_CMD:
iwp_rx_phy_intr(sc, desc);
break;
case REPLY_RX_MPDU_CMD:
iwp_rx_mpdu_intr(sc, desc);
break;
case REPLY_TX:
iwp_tx_intr(sc, desc);
break;
case REPLY_ALIVE:
iwp_ucode_alive(sc, desc);
break;
case CARD_STATE_NOTIFICATION:
{
uint32_t *status = (uint32_t *)(desc + 1);
IWP_DBG((IWP_DEBUG_RADIO, "iwp_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
* iwp_thread() tries to recover it after the
* button is pushed again(ON)
*/
cmn_err(CE_NOTE, "iwp_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,
IWP_F_HW_ERR_RECOVER | IWP_F_RADIO_OFF);
}
break;
}
case SCAN_START_NOTIFICATION:
{
iwp_start_scan_t *scan =
(iwp_start_scan_t *)(desc + 1);
IWP_DBG((IWP_DEBUG_SCAN, "iwp_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
iwp_stop_scan_t *scan =
(iwp_stop_scan_t *)(desc + 1);
IWP_DBG((IWP_DEBUG_SCAN, "iwp_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:
iwp_save_calib_result(sc, desc);
break;
case CALIBRATION_COMPLETE_NOTIFICATION:
mutex_enter(&sc->sc_glock);
atomic_or_32(&sc->sc_flags, IWP_F_FW_INIT);
cv_signal(&sc->sc_ucode_cv);
mutex_exit(&sc->sc_glock);
break;
case MISSED_BEACONS_NOTIFICATION:
{
struct iwp_beacon_missed *miss =
(struct iwp_beacon_missed *)(desc + 1);
if ((ic->ic_state == IEEE80211_S_RUN) &&
(LE_32(miss->consecutive) > 50)) {
cmn_err(CE_NOTE, "iwp: iwp_rx_softintr(): "
"beacon missed %d/%d\n",
LE_32(miss->consecutive),
LE_32(miss->total));
(void) ieee80211_new_state(ic,
IEEE80211_S_INIT, -1);
}
break;
}
}
sc->sc_rxq.cur = (sc->sc_rxq.cur + 1) % RX_QUEUE_SIZE;
}
/*
* driver dealt with what received in rx queue and tell the information
* to the firmware.
*/
index = (0 == index) ? RX_QUEUE_SIZE - 1 : index - 1;
IWP_WRITE(sc, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, index & (~7));
/*
* re-enable interrupts
*/
IWP_WRITE(sc, CSR_INT_MASK, CSR_INI_SET_MASK);
return (DDI_INTR_CLAIMED);
}
/*
* the handle of interrupt
*/
/* ARGSUSED */
static uint_t
iwp_intr(caddr_t arg, caddr_t unused)
{
iwp_sc_t *sc;
uint32_t r, rfh;
if (NULL == arg) {
return (DDI_INTR_UNCLAIMED);
}
sc = (iwp_sc_t *)arg;
r = IWP_READ(sc, CSR_INT);
if (0 == r || 0xffffffff == r) {
return (DDI_INTR_UNCLAIMED);
}
IWP_DBG((IWP_DEBUG_INTR, "iwp_intr(): "
"interrupt reg %x\n", r));
rfh = IWP_READ(sc, CSR_FH_INT_STATUS);
IWP_DBG((IWP_DEBUG_INTR, "iwp_intr(): "
"FH interrupt reg %x\n", rfh));
/*
* disable interrupts
*/
IWP_WRITE(sc, CSR_INT_MASK, 0);
/*
* ack interrupts
*/
IWP_WRITE(sc, CSR_INT, r);
IWP_WRITE(sc, CSR_FH_INT_STATUS, rfh);
if (r & (BIT_INT_SWERROR | BIT_INT_ERR)) {
IWP_DBG((IWP_DEBUG_FW, "iwp_intr(): "
"fatal firmware error\n"));
iwp_stop(sc);
sc->sc_ostate = sc->sc_ic.ic_state;
/* notify upper layer */
if (!IWP_CHK_FAST_RECOVER(sc)) {
ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
}
atomic_or_32(&sc->sc_flags, IWP_F_HW_ERR_RECOVER);
return (DDI_INTR_CLAIMED);
}
if (r & BIT_INT_RF_KILL) {
uint32_t tmp = IWP_READ(sc, CSR_GP_CNTRL);
if (tmp & (1 << 27)) {
cmn_err(CE_NOTE, "RF switch: radio on\n");
}
}
if ((r & (BIT_INT_FH_RX | BIT_INT_SW_RX)) ||
(rfh & FH_INT_RX_MASK)) {
(void) ddi_intr_trigger_softint(sc->sc_soft_hdl, NULL);
return (DDI_INTR_CLAIMED);
}
if (r & BIT_INT_FH_TX) {
mutex_enter(&sc->sc_glock);
atomic_or_32(&sc->sc_flags, IWP_F_PUT_SEG);
cv_signal(&sc->sc_put_seg_cv);
mutex_exit(&sc->sc_glock);
}
#ifdef DEBUG
if (r & BIT_INT_ALIVE) {
IWP_DBG((IWP_DEBUG_FW, "iwp_intr(): "
"firmware initialized.\n"));
}
#endif
/*
* re-enable interrupts
*/
IWP_WRITE(sc, CSR_INT_MASK, CSR_INI_SET_MASK);