Home | History | Annotate | Line # | Download | only in pci
if_iwn.c revision 1.38
      1  1.38     joerg /*	$NetBSD: if_iwn.c,v 1.38 2010/04/05 07:20:26 joerg Exp $	*/
      2  1.33  christos /*	$OpenBSD: if_iwn.c,v 1.49 2009/03/29 21:53:52 sthen Exp $	*/
      3   1.1      ober 
      4   1.1      ober /*-
      5  1.33  christos  * Copyright (c) 2007, 2008
      6   1.1      ober  *	Damien Bergamini <damien.bergamini (at) free.fr>
      7   1.1      ober  *
      8   1.1      ober  * Permission to use, copy, modify, and distribute this software for any
      9   1.1      ober  * purpose with or without fee is hereby granted, provided that the above
     10   1.1      ober  * copyright notice and this permission notice appear in all copies.
     11   1.1      ober  *
     12   1.1      ober  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13   1.1      ober  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14   1.1      ober  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15   1.1      ober  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16   1.1      ober  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17   1.1      ober  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18   1.1      ober  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19   1.1      ober  */
     20   1.1      ober 
     21   1.1      ober /*
     22  1.33  christos  * Driver for Intel Wireless WiFi Link 4965 and Intel WiFi Link 5000 Series
     23  1.33  christos  * 802.11 network adapters.
     24   1.1      ober  */
     25  1.33  christos #include <sys/cdefs.h>
     26  1.38     joerg __KERNEL_RCSID(0, "$NetBSD: if_iwn.c,v 1.38 2010/04/05 07:20:26 joerg Exp $");
     27   1.1      ober 
     28   1.1      ober 
     29   1.1      ober #include <sys/param.h>
     30   1.1      ober #include <sys/sockio.h>
     31   1.1      ober #include <sys/sysctl.h>
     32   1.1      ober #include <sys/mbuf.h>
     33   1.1      ober #include <sys/kernel.h>
     34   1.1      ober #include <sys/socket.h>
     35   1.1      ober #include <sys/systm.h>
     36   1.1      ober #include <sys/malloc.h>
     37  1.17      cube #include <sys/mutex.h>
     38   1.1      ober #include <sys/conf.h>
     39   1.1      ober #include <sys/kauth.h>
     40   1.1      ober #include <sys/callout.h>
     41   1.1      ober 
     42   1.1      ober #include <machine/bus.h>
     43   1.1      ober #include <machine/endian.h>
     44   1.1      ober #include <machine/intr.h>
     45   1.1      ober 
     46   1.1      ober #include <dev/pci/pcireg.h>
     47   1.1      ober #include <dev/pci/pcivar.h>
     48   1.1      ober #include <dev/pci/pcidevs.h>
     49   1.1      ober 
     50   1.1      ober #include <net/bpf.h>
     51   1.1      ober #include <net/if.h>
     52   1.1      ober #include <net/if_arp.h>
     53   1.1      ober #include <net/if_dl.h>
     54   1.1      ober #include <net/if_media.h>
     55   1.1      ober #include <net/if_types.h>
     56   1.1      ober 
     57   1.1      ober #include <netinet/in.h>
     58   1.1      ober #include <netinet/in_systm.h>
     59   1.1      ober #include <netinet/in_var.h>
     60   1.1      ober #include <net/if_ether.h>
     61   1.1      ober #include <netinet/ip.h>
     62   1.1      ober 
     63   1.1      ober #include <net80211/ieee80211_var.h>
     64   1.1      ober #include <net80211/ieee80211_amrr.h>
     65   1.1      ober #include <net80211/ieee80211_radiotap.h>
     66   1.1      ober 
     67   1.1      ober #include <dev/firmload.h>
     68   1.1      ober 
     69   1.1      ober #include <dev/pci/if_iwnreg.h>
     70   1.1      ober #include <dev/pci/if_iwnvar.h>
     71   1.1      ober 
     72  1.33  christos static const pci_product_id_t iwn_devices[] = {
     73  1.33  christos 	PCI_PRODUCT_INTEL_PRO_WL_4965AGN_1,
     74  1.33  christos 	PCI_PRODUCT_INTEL_PRO_WL_4965AGN_2,
     75  1.33  christos 	PCI_PRODUCT_INTEL_PRO_WL_5100AGN_1,
     76  1.33  christos 	PCI_PRODUCT_INTEL_PRO_WL_5100AGN_2,
     77  1.33  christos #ifdef notyet
     78  1.33  christos 	PCI_PRODUCT_INTEL_PRO_WL_5150AGN_1,
     79  1.33  christos 	PCI_PRODUCT_INTEL_PRO_WL_5150AGN_2,
     80  1.33  christos #endif
     81  1.33  christos 	PCI_PRODUCT_INTEL_PRO_WL_5300AGN_1,
     82  1.33  christos 	PCI_PRODUCT_INTEL_PRO_WL_5300AGN_2,
     83  1.33  christos 	PCI_PRODUCT_INTEL_PRO_WL_5350AGN_1,
     84  1.33  christos 	PCI_PRODUCT_INTEL_PRO_WL_5350AGN_2,
     85  1.33  christos #ifdef notyet
     86  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_4965_1,
     87  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_4965_2,
     88  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_5100_1,
     89  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_5100_2,
     90  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_5150_1,
     91  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_5150_2,
     92  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_5300_1,
     93  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_5300_2,
     94  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_5350_1,
     95  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_5350_2,
     96  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_6000_1,
     97  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_6000_2,
     98  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_6000_3,
     99  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_6000_4,
    100  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_6050_1,
    101  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_6050_2,
    102  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_6050_3,
    103  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_6050_4,
    104  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_1000_1,
    105  1.33  christos 	PCI_PRODUCT_INTEL_WIFI_LINK_1000_2,
    106  1.33  christos #endif
    107   1.1      ober };
    108   1.1      ober 
    109   1.1      ober /*
    110   1.1      ober  * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
    111   1.1      ober  */
    112   1.1      ober static const struct ieee80211_rateset iwn_rateset_11a =
    113   1.1      ober 	{ 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
    114   1.1      ober 
    115   1.1      ober static const struct ieee80211_rateset iwn_rateset_11b =
    116  1.33  christos 	{ 4, { 2, 4, 11, 22 } };
    117   1.1      ober 
    118   1.1      ober static const struct ieee80211_rateset iwn_rateset_11g =
    119  1.33  christos 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
    120   1.1      ober 
    121   1.1      ober 
    122  1.33  christos static int	iwn_match(device_t , struct cfdata *, void *);
    123  1.33  christos static void	iwn_attach(device_t , device_t, void *);
    124  1.33  christos static int	iwn_detach(device_t, int);
    125  1.33  christos 
    126  1.33  christos const struct	iwn_hal *iwn_hal_attach(struct iwn_softc *);
    127  1.33  christos static int	iwn_nic_lock(struct iwn_softc *);
    128  1.33  christos static int	iwn_eeprom_lock(struct iwn_softc *);
    129  1.33  christos static int	iwn_read_prom_data(struct iwn_softc *, uint32_t, void *, int);
    130  1.33  christos static void	iwn_radiotap_attach(struct iwn_softc *);
    131  1.33  christos static int	iwn_dma_contig_alloc(bus_dma_tag_t, struct iwn_dma_info *,
    132  1.33  christos 		    void **, bus_size_t, bus_size_t, int);
    133  1.33  christos static void	iwn_dma_contig_free(struct iwn_dma_info *);
    134  1.33  christos static int	iwn_alloc_sched(struct iwn_softc *);
    135  1.33  christos static void	iwn_free_sched(struct iwn_softc *);
    136  1.33  christos static int	iwn_alloc_kw(struct iwn_softc *);
    137  1.33  christos static void	iwn_free_kw(struct iwn_softc *);
    138  1.33  christos static int	iwn_alloc_fwmem(struct iwn_softc *);
    139  1.33  christos static void	iwn_free_fwmem(struct iwn_softc *);
    140  1.33  christos static struct	iwn_rbuf *iwn_alloc_rbuf(struct iwn_softc *);
    141  1.33  christos static void	iwn_free_rbuf(struct mbuf *, void *, size_t, void *);
    142  1.33  christos static int	iwn_alloc_rpool(struct iwn_softc *);
    143  1.33  christos static void	iwn_free_rpool(struct iwn_softc *);
    144  1.33  christos static int	iwn_alloc_rx_ring(struct iwn_softc *, struct iwn_rx_ring *);
    145  1.33  christos static void	iwn_reset_rx_ring(struct iwn_softc *, struct iwn_rx_ring *);
    146  1.33  christos static void	iwn_free_rx_ring(struct iwn_softc *, struct iwn_rx_ring *);
    147  1.33  christos static int	iwn_alloc_tx_ring(struct iwn_softc *, struct iwn_tx_ring *,
    148  1.33  christos 		    int, int);
    149  1.33  christos static void	iwn_reset_tx_ring(struct iwn_softc *, struct iwn_tx_ring *);
    150  1.33  christos static void	iwn_free_tx_ring(struct iwn_softc *, struct iwn_tx_ring *);
    151  1.33  christos static int	iwn_read_eeprom(struct iwn_softc *);
    152  1.33  christos static void	iwn4965_read_eeprom(struct iwn_softc *);
    153  1.33  christos static void	iwn5000_read_eeprom(struct iwn_softc *);
    154  1.33  christos static void	iwn_read_eeprom_channels(struct iwn_softc *, int, uint32_t);
    155  1.33  christos static struct	ieee80211_node *iwn_node_alloc(struct ieee80211_node_table *);
    156  1.33  christos static void	iwn_newassoc(struct ieee80211_node *, int);
    157  1.33  christos static int	iwn_media_change(struct ifnet *);
    158  1.33  christos static int	iwn_newstate(struct ieee80211com *, enum ieee80211_state, int);
    159  1.33  christos static void	iwn_iter_func(void *, struct ieee80211_node *);
    160  1.33  christos static void	iwn_calib_timeout(void *);
    161  1.33  christos #if 0
    162  1.33  christos static int	iwn_ccmp_decap(struct iwn_softc *, struct mbuf *,
    163  1.33  christos 		    struct ieee80211_key *);
    164  1.33  christos #endif
    165  1.33  christos static void	iwn_rx_phy(struct iwn_softc *, struct iwn_rx_desc *);
    166  1.33  christos static void	iwn_rx_done(struct iwn_softc *, struct iwn_rx_desc *,
    167  1.33  christos 		    struct iwn_rx_data *);
    168  1.33  christos static void	iwn5000_rx_calib_results(struct iwn_softc *,
    169  1.33  christos     struct iwn_rx_desc *, struct iwn_rx_data *);
    170  1.33  christos static void	iwn_rx_statistics(struct iwn_softc *, struct iwn_rx_desc *,
    171  1.33  christos     struct iwn_rx_data *);
    172  1.33  christos static void	iwn4965_tx_done(struct iwn_softc *, struct iwn_rx_desc *,
    173  1.33  christos     struct iwn_rx_data *);
    174  1.33  christos static void	iwn5000_tx_done(struct iwn_softc *, struct iwn_rx_desc *,
    175   1.2      ober     struct iwn_rx_data *);
    176  1.33  christos static void	iwn_tx_done(struct iwn_softc *, struct iwn_rx_desc *, int,
    177  1.33  christos 		    uint8_t);
    178  1.33  christos static void	iwn_cmd_done(struct iwn_softc *, struct iwn_rx_desc *);
    179  1.33  christos static void	iwn_notif_intr(struct iwn_softc *);
    180  1.33  christos static void	iwn_wakeup_intr(struct iwn_softc *);
    181  1.33  christos static void	iwn_fatal_intr(struct iwn_softc *);
    182  1.33  christos static int	iwn_intr(void *);
    183  1.33  christos static void	iwn4965_update_sched(struct iwn_softc *, int, int, uint8_t,
    184  1.33  christos 		    uint16_t);
    185  1.33  christos static void	iwn5000_update_sched(struct iwn_softc *, int, int, uint8_t,
    186  1.33  christos 		    uint16_t);
    187  1.33  christos static void	iwn5000_reset_sched(struct iwn_softc *, int, int);
    188  1.33  christos static int	iwn_tx(struct iwn_softc *, struct mbuf *,
    189  1.33  christos 		    struct ieee80211_node *, int);
    190  1.33  christos static void	iwn_start(struct ifnet *);
    191  1.33  christos static void	iwn_watchdog(struct ifnet *);
    192  1.33  christos static int	iwn_ioctl(struct ifnet *, u_long, void *);
    193  1.33  christos static int	iwn_cmd(struct iwn_softc *, int, const void *, int, int);
    194  1.33  christos static int	iwn_wme_update(struct ieee80211com *);
    195  1.33  christos static int	iwn4965_add_node(struct iwn_softc *, struct iwn_node_info *,
    196  1.33  christos 		    int);
    197  1.33  christos static int	iwn5000_add_node(struct iwn_softc *, struct iwn_node_info *,
    198  1.33  christos 		    int);
    199  1.33  christos static int	iwn_set_link_quality(struct iwn_softc *,
    200  1.33  christos 		    struct ieee80211_node *);
    201  1.33  christos static int	iwn_add_broadcast_node(struct iwn_softc *, int);
    202  1.33  christos static void	iwn_set_led(struct iwn_softc *, uint8_t, uint8_t, uint8_t);
    203  1.33  christos static int	iwn_set_critical_temp(struct iwn_softc *);
    204  1.33  christos static int	iwn_set_timing(struct iwn_softc *, struct ieee80211_node *);
    205  1.33  christos //static void	iwn4965_power_calibration(struct iwn_softc *, int);
    206  1.33  christos static int	iwn4965_set_txpower(struct iwn_softc *, int);
    207  1.33  christos static int	iwn5000_set_txpower(struct iwn_softc *, int);
    208  1.33  christos static int	iwn4965_get_rssi(const struct iwn_rx_stat *);
    209  1.33  christos static int	iwn5000_get_rssi(const struct iwn_rx_stat *);
    210  1.33  christos static int	iwn_get_noise(const struct iwn_rx_general_stats *);
    211  1.33  christos static int	iwn4965_get_temperature(struct iwn_softc *);
    212  1.33  christos static int	iwn5000_get_temperature(struct iwn_softc *);
    213  1.33  christos static int	iwn_init_sensitivity(struct iwn_softc *);
    214  1.33  christos static void	iwn_collect_noise(struct iwn_softc *,
    215  1.33  christos 		    const struct iwn_rx_general_stats *);
    216  1.33  christos static int	iwn4965_init_gains(struct iwn_softc *);
    217  1.33  christos static int	iwn5000_init_gains(struct iwn_softc *);
    218  1.33  christos static int	iwn4965_set_gains(struct iwn_softc *);
    219  1.33  christos static int	iwn5000_set_gains(struct iwn_softc *);
    220  1.33  christos static void	iwn_tune_sensitivity(struct iwn_softc *,
    221  1.33  christos 		    const struct iwn_rx_stats *);
    222  1.33  christos static int	iwn_send_sensitivity(struct iwn_softc *);
    223  1.33  christos // XXX  static int	iwn_set_pslevel(struct iwn_softc *, int, int, int);
    224  1.33  christos static int	iwn_config(struct iwn_softc *);
    225  1.33  christos static int	iwn_scan(struct iwn_softc *, uint16_t);
    226  1.33  christos static int	iwn_auth(struct iwn_softc *);
    227  1.33  christos static int	iwn_run(struct iwn_softc *);
    228  1.33  christos #ifdef notyet
    229  1.33  christos static void	iwn_delete_key(struct ieee80211com *, struct ieee80211_node *,
    230  1.33  christos 		    struct ieee80211_key *);
    231  1.33  christos #endif
    232  1.33  christos #ifndef IEEE80211_NO_HT
    233  1.33  christos static int	iwn_ampdu_rx_start(struct ieee80211com *,
    234  1.33  christos 		    struct ieee80211_node *, uint8_t, uint16_t);
    235  1.33  christos static void	iwn_ampdu_rx_stop(struct ieee80211com *,
    236  1.33  christos 		    struct ieee80211_node *, uint8_t, uint16_t);
    237  1.33  christos static int	iwn_ampdu_tx_start(struct ieee80211com *,
    238  1.33  christos 		    struct ieee80211_node *, uint8_t, uint16_t);
    239  1.33  christos static void	iwn_ampdu_tx_stop(struct ieee80211com *,
    240  1.33  christos 		    struct ieee80211_node *, uint8_t, uint16_t);
    241  1.33  christos static void	iwn4965_ampdu_tx_start(struct iwn_softc *,
    242  1.33  christos 		    struct ieee80211_node *, uint8_t, uint16_t);
    243  1.33  christos static void	iwn4965_ampdu_tx_stop(struct iwn_softc *,
    244  1.33  christos 		    uint8_t, uint16_t);
    245  1.33  christos static void	iwn5000_ampdu_tx_start(struct iwn_softc *,
    246  1.33  christos 		    struct ieee80211_node *, uint8_t, uint16_t);
    247  1.33  christos static void	iwn5000_ampdu_tx_stop(struct iwn_softc *,
    248  1.33  christos 		    uint8_t, uint16_t);
    249  1.33  christos #endif
    250  1.33  christos static int	iwn5000_query_calibration(struct iwn_softc *);
    251  1.33  christos static int	iwn5000_send_calibration(struct iwn_softc *);
    252  1.33  christos static int	iwn4965_post_alive(struct iwn_softc *);
    253  1.33  christos static int	iwn5000_post_alive(struct iwn_softc *);
    254  1.33  christos static int	iwn4965_load_bootcode(struct iwn_softc *, const uint8_t *,
    255  1.33  christos 		    int);
    256  1.33  christos static int	iwn4965_load_firmware(struct iwn_softc *);
    257  1.33  christos static int	iwn5000_load_firmware_section(struct iwn_softc *, uint32_t,
    258  1.33  christos 		    const uint8_t *, int);
    259  1.33  christos static int	iwn5000_load_firmware(struct iwn_softc *);
    260  1.33  christos static int	iwn_read_firmware(struct iwn_softc *);
    261  1.33  christos static int	iwn_clock_wait(struct iwn_softc *);
    262  1.33  christos static int	iwn4965_apm_init(struct iwn_softc *);
    263  1.33  christos static int	iwn5000_apm_init(struct iwn_softc *);
    264  1.33  christos static void	iwn_apm_stop_master(struct iwn_softc *);
    265  1.33  christos static void	iwn_apm_stop(struct iwn_softc *);
    266  1.33  christos static int	iwn4965_nic_config(struct iwn_softc *);
    267  1.33  christos static int	iwn5000_nic_config(struct iwn_softc *);
    268  1.33  christos static int	iwn_hw_init(struct iwn_softc *);
    269  1.33  christos static void	iwn_hw_stop(struct iwn_softc *);
    270  1.33  christos static int	iwn_init(struct ifnet *);
    271  1.33  christos static void	iwn_stop(struct ifnet *, int);
    272  1.33  christos static void	iwn_fix_channel(struct ieee80211com *, struct mbuf *);
    273  1.37    dyoung static bool	iwn_resume(device_t, const pmf_qual_t *);
    274   1.1      ober 
    275   1.1      ober #define IWN_DEBUG
    276   1.1      ober #ifdef IWN_DEBUG
    277   1.1      ober #define DPRINTF(x)	do { if (iwn_debug > 0) printf x; } while (0)
    278   1.1      ober #define DPRINTFN(n, x)	do { if (iwn_debug >= (n)) printf x; } while (0)
    279  1.11     blymn int iwn_debug = 0;
    280   1.1      ober #else
    281   1.1      ober #define DPRINTF(x)
    282   1.1      ober #define DPRINTFN(n, x)
    283   1.1      ober #endif
    284  1.33  christos #ifdef IWN_DEBUG
    285  1.33  christos static void	iwn4965_print_power_group(struct iwn_softc *, int);
    286  1.33  christos #endif
    287  1.33  christos 
    288  1.33  christos static const struct iwn_hal iwn4965_hal = {
    289  1.33  christos 	iwn4965_load_firmware,
    290  1.33  christos 	iwn4965_read_eeprom,
    291  1.33  christos 	iwn4965_post_alive,
    292  1.33  christos 	iwn4965_apm_init,
    293  1.33  christos 	iwn4965_nic_config,
    294  1.33  christos 	iwn4965_update_sched,
    295  1.33  christos 	iwn4965_get_temperature,
    296  1.33  christos 	iwn4965_get_rssi,
    297  1.33  christos 	iwn4965_set_txpower,
    298  1.33  christos 	iwn4965_init_gains,
    299  1.33  christos 	iwn4965_set_gains,
    300  1.33  christos 	iwn4965_add_node,
    301  1.33  christos 	iwn4965_tx_done,
    302  1.33  christos #ifndef IEEE80211_NO_HT
    303  1.33  christos 	iwn4965_ampdu_tx_start,
    304  1.33  christos 	iwn4965_ampdu_tx_stop,
    305  1.33  christos #endif
    306  1.33  christos 	&iwn4965_sensitivity_limits,
    307  1.33  christos 	IWN4965_NTXQUEUES,
    308  1.33  christos 	IWN4965_ID_BROADCAST,
    309  1.33  christos 	IWN4965_RXONSZ,
    310  1.33  christos 	IWN4965_SCHEDSZ,
    311  1.33  christos 	IWN4965_FW_TEXT_MAXSZ,
    312  1.33  christos 	IWN4965_FW_DATA_MAXSZ,
    313  1.33  christos 	IWN4965_FWSZ,
    314  1.33  christos 	IWN4965_SCHED_TXFACT
    315  1.33  christos };
    316   1.1      ober 
    317  1.33  christos static const struct iwn_hal iwn5000_hal = {
    318  1.33  christos 	iwn5000_load_firmware,
    319  1.33  christos 	iwn5000_read_eeprom,
    320  1.33  christos 	iwn5000_post_alive,
    321  1.33  christos 	iwn5000_apm_init,
    322  1.33  christos 	iwn5000_nic_config,
    323  1.33  christos 	iwn5000_update_sched,
    324  1.33  christos 	iwn5000_get_temperature,
    325  1.33  christos 	iwn5000_get_rssi,
    326  1.33  christos 	iwn5000_set_txpower,
    327  1.33  christos 	iwn5000_init_gains,
    328  1.33  christos 	iwn5000_set_gains,
    329  1.33  christos 	iwn5000_add_node,
    330  1.33  christos 	iwn5000_tx_done,
    331  1.33  christos #ifndef IEEE80211_NO_HT
    332  1.33  christos 	iwn5000_ampdu_tx_start,
    333  1.33  christos 	iwn5000_ampdu_tx_stop,
    334  1.11     blymn #endif
    335  1.33  christos 	&iwn5000_sensitivity_limits,
    336  1.33  christos 	IWN5000_NTXQUEUES,
    337  1.33  christos 	IWN5000_ID_BROADCAST,
    338  1.33  christos 	IWN5000_RXONSZ,
    339  1.33  christos 	IWN5000_SCHEDSZ,
    340  1.33  christos 	IWN5000_FW_TEXT_MAXSZ,
    341  1.33  christos 	IWN5000_FW_DATA_MAXSZ,
    342  1.33  christos 	IWN5000_FWSZ,
    343  1.33  christos 	IWN5000_SCHED_TXFACT
    344  1.33  christos };
    345  1.11     blymn 
    346   1.8     blymn CFATTACH_DECL_NEW(iwn, sizeof(struct iwn_softc), iwn_match, iwn_attach,
    347  1.33  christos 		iwn_detach, NULL);
    348  1.33  christos 
    349   1.1      ober 
    350   1.1      ober static int
    351  1.29    cegger iwn_match(device_t parent, cfdata_t match __unused, void *aux)
    352   1.1      ober {
    353   1.2      ober 	struct pci_attach_args *pa = aux;
    354  1.33  christos 	size_t i;
    355   1.8     blymn 
    356   1.2      ober 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    357   1.2      ober 		return 0;
    358   1.1      ober 
    359  1.33  christos 	for (i = 0; i < __arraycount(iwn_devices); i++)
    360  1.33  christos 		if (PCI_PRODUCT(pa->pa_id) == iwn_devices[i])
    361  1.33  christos 			return 1;
    362   1.1      ober 
    363   1.2      ober 	return 0;
    364   1.1      ober }
    365   1.1      ober 
    366   1.1      ober static void
    367   1.1      ober iwn_attach(device_t parent __unused, device_t self, void *aux)
    368   1.1      ober {
    369   1.1      ober 	struct iwn_softc *sc = device_private(self);
    370   1.1      ober 	struct ieee80211com *ic = &sc->sc_ic;
    371   1.1      ober 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    372   1.1      ober 	struct pci_attach_args *pa = aux;
    373  1.33  christos 	const struct iwn_hal *hal;
    374   1.1      ober 	const char *intrstr;
    375   1.1      ober 	char devinfo[256];
    376   1.1      ober 	pci_intr_handle_t ih;
    377  1.33  christos 	pcireg_t memtype, reg;
    378   1.8     blymn 	int i, error, revision;
    379   1.1      ober 
    380   1.1      ober 	sc->sc_dev = self;
    381   1.2      ober 	sc->sc_pct = pa->pa_pc;
    382   1.1      ober 	sc->sc_pcitag = pa->pa_tag;
    383   1.1      ober 
    384   1.1      ober 	callout_init(&sc->calib_to, 0);
    385   1.1      ober 	callout_setfunc(&sc->calib_to, iwn_calib_timeout, sc);
    386   1.8     blymn 
    387   1.1      ober 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
    388   1.1      ober 	revision = PCI_REVISION(pa->pa_class);
    389  1.34     njoly 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
    390   1.8     blymn 
    391  1.33  christos 	/*
    392  1.33  christos 	 * Get the offset of the PCI Express Capability Structure in PCI
    393  1.33  christos 	 * Configuration Space (the vendor driver hard-codes it as E0h.)
    394  1.33  christos 	 */
    395  1.33  christos 	error = pci_get_capability(sc->sc_pct, sc->sc_pcitag,
    396  1.33  christos 	    PCI_CAP_PCIEXPRESS, &sc->sc_cap_off, NULL);
    397  1.33  christos 	if (error == 0) {
    398  1.34     njoly 		aprint_error_dev(self, "PCIe capability structure not found!\n");
    399  1.33  christos 		return;
    400  1.33  christos 	}
    401   1.1      ober 
    402  1.33  christos 	/* Clear device-specific "PCI retry timeout" register (41h). */
    403  1.33  christos 	reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
    404  1.33  christos 	reg &= ~0xff00;
    405  1.33  christos 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, reg);
    406   1.1      ober 
    407   1.1      ober 	/* enable bus-mastering */
    408  1.33  christos 	reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
    409  1.33  christos 	reg |= PCI_COMMAND_MASTER_ENABLE;
    410  1.33  christos 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, reg);
    411   1.1      ober 
    412   1.1      ober 	/* map the register window */
    413   1.1      ober 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, IWN_PCI_BAR0);
    414   1.1      ober 	error = pci_mapreg_map(pa, IWN_PCI_BAR0, memtype, 0, &sc->sc_st,
    415   1.1      ober 	    &sc->sc_sh, NULL, &sc->sc_sz);
    416   1.1      ober 	if (error != 0) {
    417   1.1      ober 		aprint_error_dev(self, "could not map memory space\n");
    418   1.1      ober 		return;
    419   1.1      ober 	}
    420  1.33  christos #if 1
    421   1.1      ober 	sc->sc_dmat = pa->pa_dmat;
    422  1.33  christos #else
    423  1.20     blymn 	/* XXX may not be needed */
    424  1.20     blymn 	if (bus_dmatag_subregion(pa->pa_dmat, 0, 3 << 30,
    425  1.20     blymn 	    &(sc->sc_dmat), BUS_DMA_NOWAIT) != 0) {
    426  1.20     blymn 		aprint_error_dev(self,
    427  1.20     blymn 		    "WARNING: failed to restrict dma range, "
    428  1.20     blymn 		    "falling back to parent bus dma range\n");
    429  1.20     blymn 		sc->sc_dmat = pa->pa_dmat;
    430  1.20     blymn 	}
    431  1.33  christos #endif
    432   1.1      ober 
    433  1.33  christos 	/* Install interrupt handler. */
    434   1.1      ober 	if (pci_intr_map(pa, &ih) != 0) {
    435  1.34     njoly 		aprint_error_dev(self, "could not map interrupt\n");
    436   1.1      ober 		return;
    437   1.1      ober 	}
    438   1.1      ober 	intrstr = pci_intr_string(sc->sc_pct, ih);
    439   1.1      ober 	sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwn_intr, sc);
    440   1.3     skrll 
    441   1.1      ober 	if (sc->sc_ih == NULL) {
    442   1.8     blymn 		aprint_error_dev(self, "could not establish interrupt");
    443   1.1      ober 		if (intrstr != NULL)
    444   1.1      ober 			aprint_error(" at %s", intrstr);
    445   1.1      ober 		aprint_error("\n");
    446   1.1      ober 		return;
    447   1.1      ober 	}
    448   1.1      ober 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    449   1.1      ober 
    450  1.33  christos 	/* Attach Hardware Abstraction Layer. */
    451  1.33  christos 	if ((hal = iwn_hal_attach(sc)) == NULL)
    452  1.33  christos 		return;
    453  1.33  christos 
    454  1.33  christos 	/* Power ON adapter. */
    455  1.33  christos 	if ((error = hal->apm_init(sc)) != 0) {
    456  1.33  christos 		aprint_error_dev(self, "could not power ON adapter\n");
    457  1.33  christos 		return;
    458  1.33  christos 	}
    459  1.33  christos 
    460  1.33  christos 	/* Read MAC address, channels, etc from EEPROM. */
    461  1.33  christos 	if ((error = iwn_read_eeprom(sc)) != 0) {
    462  1.33  christos 		aprint_error_dev(self, "could not read EEPROM\n");
    463   1.2      ober 		return;
    464   1.1      ober 	}
    465   1.8     blymn 
    466  1.33  christos 	/* Allocate DMA memory for firmware transfers. */
    467   1.1      ober 	if ((error = iwn_alloc_fwmem(sc)) != 0) {
    468  1.33  christos 		aprint_error_dev(self,
    469  1.33  christos 		    "could not allocate memory for firmware\n");
    470   1.1      ober 		return;
    471   1.1      ober 	}
    472   1.1      ober 
    473  1.33  christos 	/* Allocate "Keep Warm" page. */
    474   1.1      ober 	if ((error = iwn_alloc_kw(sc)) != 0) {
    475   1.1      ober 		aprint_error_dev(self, "could not allocate keep warm page\n");
    476   1.1      ober 		goto fail1;
    477   1.1      ober 	}
    478   1.1      ober 
    479  1.33  christos 	/* Allocate TX scheduler "rings". */
    480  1.33  christos 	if ((error = iwn_alloc_sched(sc)) != 0) {
    481  1.33  christos 		aprint_error_dev(self,
    482  1.33  christos 		    "could not allocate TX scheduler rings\n");
    483   1.1      ober 		goto fail2;
    484   1.1      ober 	}
    485   1.1      ober 
    486  1.33  christos 	/* Allocate RX buffers. */
    487   1.1      ober 	if ((error = iwn_alloc_rpool(sc)) != 0) {
    488  1.33  christos 		aprint_error_dev(self, "could not allocate RX buffers\n");
    489   1.1      ober 		goto fail3;
    490   1.1      ober 	}
    491   1.1      ober 
    492  1.33  christos 	/* Allocate TX rings (16 on 4965AGN, 20 on 5000.) */
    493  1.33  christos 	for (i = 0; i < hal->ntxqs; i++) {
    494   1.1      ober 		struct iwn_tx_ring *txq = &sc->txq[i];
    495   1.1      ober 		error = iwn_alloc_tx_ring(sc, txq, IWN_TX_RING_COUNT, i);
    496   1.1      ober 		if (error != 0) {
    497  1.33  christos 			aprint_error_dev(self,
    498  1.33  christos 			    "could not allocate TX ring %d\n", i);
    499   1.1      ober 			goto fail4;
    500   1.1      ober 		}
    501   1.1      ober 	}
    502   1.8     blymn 
    503  1.33  christos 	/* Allocate RX ring. */
    504  1.33  christos 	if (iwn_alloc_rx_ring(sc, &sc->rxq) != 0) {
    505  1.33  christos 		aprint_error_dev(self, "could not allocate RX ring\n");
    506   1.2      ober 		goto fail4;
    507   1.1      ober 	}
    508   1.1      ober 
    509  1.33  christos 	/* Power OFF adapter. */
    510  1.33  christos 	iwn_apm_stop(sc);
    511  1.33  christos 	/* Clear pending interrupts. */
    512  1.33  christos 	IWN_WRITE(sc, IWN_INT, 0xffffffff);
    513  1.33  christos 
    514  1.34     njoly 	aprint_normal_dev(self, "MIMO %dT%dR, %.4s, address %s\n", sc->ntxchains,
    515  1.33  christos 	    sc->nrxchains, sc->eeprom_domain, ether_sprintf(ic->ic_myaddr));
    516  1.33  christos 
    517  1.33  christos 	/* Initialization firmware has not been loaded yet. */
    518  1.33  christos 	sc->sc_flags |= IWN_FLAG_FIRST_BOOT;
    519   1.1      ober 
    520  1.28     blymn 	/* Set the state of the RF kill switch */
    521  1.33  christos 	sc->sc_radio = (IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_RFKILL);
    522  1.28     blymn 
    523   1.1      ober 	ic->ic_ifp = ifp;
    524   1.1      ober 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
    525   1.1      ober 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
    526   1.1      ober 	ic->ic_state = IEEE80211_S_INIT;
    527   1.1      ober 
    528  1.33  christos 	/* Set device capabilities. */
    529   1.1      ober 	ic->ic_caps =
    530   1.1      ober 	    IEEE80211_C_IBSS |		/* IBSS mode support */
    531  1.33  christos 	    IEEE80211_C_WPA |		/* 802.11i */
    532   1.1      ober 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
    533   1.1      ober 	    IEEE80211_C_TXPMGT |	/* tx power management */
    534   1.1      ober 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
    535  1.33  christos 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
    536  1.15  christos 	    IEEE80211_C_WME;		/* 802.11e */
    537   1.8     blymn 
    538  1.33  christos 	/* Set supported rates. */
    539   1.1      ober 	ic->ic_sup_rates[IEEE80211_MODE_11B] = iwn_rateset_11b;
    540   1.1      ober 	ic->ic_sup_rates[IEEE80211_MODE_11G] = iwn_rateset_11g;
    541  1.33  christos 	if (sc->sc_flags & IWN_FLAG_HAS_5GHZ) {
    542  1.33  christos 		ic->ic_sup_rates[IEEE80211_MODE_11A] = iwn_rateset_11a;
    543  1.33  christos 	}
    544   1.1      ober 
    545  1.33  christos 	/* IBSS channel undefined for now. */
    546   1.1      ober 	ic->ic_ibss_chan = &ic->ic_channels[0];
    547   1.1      ober 
    548  1.24     blymn 	memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
    549  1.24     blymn 	ic->ic_des_esslen = 0;
    550  1.24     blymn 
    551   1.1      ober 	ifp->if_softc = sc;
    552   1.1      ober 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    553  1.33  christos 	ifp->if_stop = iwn_stop;
    554   1.1      ober 	ifp->if_init = iwn_init;
    555   1.1      ober 	ifp->if_ioctl = iwn_ioctl;
    556   1.1      ober 	ifp->if_start = iwn_start;
    557   1.1      ober 	ifp->if_watchdog = iwn_watchdog;
    558   1.1      ober 	IFQ_SET_READY(&ifp->if_snd);
    559   1.1      ober 	memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    560   1.1      ober 
    561   1.1      ober 	if_attach(ifp);
    562   1.1      ober 	ieee80211_ifattach(ic);
    563   1.1      ober 	ic->ic_node_alloc = iwn_node_alloc;
    564   1.1      ober 	ic->ic_newassoc = iwn_newassoc;
    565   1.1      ober 	ic->ic_wme.wme_update = iwn_wme_update;
    566  1.33  christos #ifdef notyet
    567  1.33  christos 	ic->ic_updateedca = iwn_updateedca;
    568  1.33  christos 	ic->ic_set_key = iwn_set_key;
    569  1.33  christos 	ic->ic_delete_key = iwn_delete_key;
    570  1.33  christos #endif
    571  1.33  christos #ifndef IEEE80211_NO_HT
    572  1.33  christos 	ic->ic_ampdu_rx_start = iwn_ampdu_rx_start;
    573  1.33  christos 	ic->ic_ampdu_rx_stop = iwn_ampdu_rx_stop;
    574  1.33  christos 	ic->ic_ampdu_tx_start = iwn_ampdu_tx_start;
    575  1.33  christos 	ic->ic_ampdu_tx_stop = iwn_ampdu_tx_stop;
    576  1.33  christos #endif
    577   1.1      ober 
    578  1.33  christos 	/* Override 802.11 state transition machine. */
    579   1.1      ober 	sc->sc_newstate = ic->ic_newstate;
    580   1.1      ober 	ic->ic_newstate = iwn_newstate;
    581   1.1      ober 	ieee80211_media_init(ic, iwn_media_change, ieee80211_media_status);
    582   1.1      ober 
    583   1.1      ober 	sc->amrr.amrr_min_success_threshold =  1;
    584   1.1      ober 	sc->amrr.amrr_max_success_threshold = 15;
    585   1.1      ober 
    586  1.32   tsutsui 	if (pmf_device_register(self, NULL, iwn_resume))
    587  1.32   tsutsui 		pmf_class_network_register(self, ifp);
    588  1.32   tsutsui 	else
    589   1.1      ober 		aprint_error_dev(self, "couldn't establish power handler\n");
    590   1.1      ober 
    591   1.1      ober 	iwn_radiotap_attach(sc);
    592   1.8     blymn 
    593   1.1      ober 	ieee80211_announce(ic);
    594   1.1      ober 
    595   1.1      ober 	return;
    596   1.1      ober 
    597  1.33  christos 	/* Free allocated memory if something failed during attachment. */
    598   1.1      ober fail4:	while (--i >= 0)
    599   1.1      ober 		iwn_free_tx_ring(sc, &sc->txq[i]);
    600   1.1      ober 	iwn_free_rpool(sc);
    601  1.33  christos fail3:	iwn_free_sched(sc);
    602   1.1      ober fail2:	iwn_free_kw(sc);
    603   1.1      ober fail1:	iwn_free_fwmem(sc);
    604   1.1      ober }
    605   1.1      ober 
    606   1.1      ober static int
    607  1.30    cegger iwn_detach(device_t self, int flags __unused)
    608   1.1      ober {
    609  1.31    cegger 	struct iwn_softc *sc = device_private(self);
    610   1.1      ober 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    611   1.1      ober 	int ac;
    612   1.1      ober 
    613   1.1      ober 	iwn_stop(ifp, 1);
    614   1.1      ober 	if (ifp != NULL)
    615  1.38     joerg 		bpf_detach(ifp);
    616   1.1      ober 	ieee80211_ifdetach(&sc->sc_ic);
    617   1.1      ober 	if (ifp != NULL)
    618   1.1      ober 		if_detach(ifp);
    619   1.1      ober 
    620  1.33  christos 	for (ac = 0; ac < sc->sc_hal->ntxqs; ac++)
    621   1.1      ober 		iwn_free_tx_ring(sc, &sc->txq[ac]);
    622   1.1      ober 	iwn_free_rx_ring(sc, &sc->rxq);
    623   1.1      ober 	iwn_free_rpool(sc);
    624  1.33  christos 	iwn_free_sched(sc);
    625   1.1      ober 
    626   1.1      ober 	if (sc->sc_ih != NULL) {
    627   1.1      ober 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
    628   1.1      ober 		sc->sc_ih = NULL;
    629   1.1      ober 	}
    630   1.1      ober 
    631   1.1      ober 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
    632   1.1      ober 
    633   1.1      ober 	return 0;
    634   1.1      ober }
    635   1.1      ober 
    636  1.33  christos const struct iwn_hal *
    637  1.33  christos iwn_hal_attach(struct iwn_softc *sc)
    638  1.33  christos {
    639  1.33  christos 	sc->hw_type = (IWN_READ(sc, IWN_HW_REV) >> 4) & 0xf;
    640  1.33  christos 
    641  1.33  christos 	switch (sc->hw_type) {
    642  1.33  christos 	case IWN_HW_REV_TYPE_4965:
    643  1.33  christos 		sc->sc_hal = &iwn4965_hal;
    644  1.33  christos 		sc->fwname = "iwlwifi-4965-1.ucode";
    645  1.33  christos 		sc->critical_temp = IWN_CTOK(110);
    646  1.33  christos 		sc->txantmsk = IWN_ANT_A | IWN_ANT_B;
    647  1.33  christos 		sc->rxantmsk = IWN_ANT_ABC;
    648  1.33  christos 		sc->ntxchains = 2;
    649  1.33  christos 		sc->nrxchains = 3;
    650  1.33  christos 		break;
    651  1.33  christos 	case IWN_HW_REV_TYPE_5100:
    652  1.33  christos 		sc->sc_hal = &iwn5000_hal;
    653  1.33  christos 		sc->fwname = "iwlwifi-5000-1.ucode";
    654  1.33  christos 		sc->critical_temp = 110;
    655  1.33  christos 		sc->txantmsk = IWN_ANT_B;
    656  1.33  christos 		sc->rxantmsk = IWN_ANT_A | IWN_ANT_B;
    657  1.33  christos 		sc->ntxchains = 1;
    658  1.33  christos 		sc->nrxchains = 2;
    659  1.33  christos 		break;
    660  1.33  christos 	case IWN_HW_REV_TYPE_5150:
    661  1.33  christos 		sc->sc_hal = &iwn5000_hal;
    662  1.33  christos 		sc->fwname = "iwlwifi-5150-1.ucode";
    663  1.33  christos 		/* NB: critical temperature will be read from EEPROM. */
    664  1.33  christos 		sc->txantmsk = IWN_ANT_A;
    665  1.33  christos 		sc->rxantmsk = IWN_ANT_A | IWN_ANT_B;
    666  1.33  christos 		sc->ntxchains = 1;
    667  1.33  christos 		sc->nrxchains = 2;
    668  1.33  christos 		break;
    669  1.33  christos 	case IWN_HW_REV_TYPE_5300:
    670  1.33  christos 	case IWN_HW_REV_TYPE_5350:
    671  1.33  christos 		sc->sc_hal = &iwn5000_hal;
    672  1.33  christos 		sc->fwname = "iwlwifi-5000-1.ucode";
    673  1.33  christos 		sc->critical_temp = 110;
    674  1.33  christos 		sc->txantmsk = sc->rxantmsk = IWN_ANT_ABC;
    675  1.33  christos 		sc->ntxchains = sc->nrxchains = 3;
    676  1.33  christos 		break;
    677  1.33  christos #ifdef notyet
    678  1.33  christos 	case IWN_HW_REV_TYPE_1000:
    679  1.33  christos 		sc->sc_hal = &iwn5000_hal;
    680  1.33  christos 		sc->fwname = "iwn-1000";
    681  1.33  christos 		sc->critical_temp = 110;
    682  1.33  christos 		sc->txantmsk = IWN_ANT_A;
    683  1.33  christos 		sc->rxantmsk = IWN_ANT_A | IWN_ANT_B;
    684  1.33  christos 		sc->ntxchains = 1;
    685  1.33  christos 		sc->nrxchains = 2;
    686  1.33  christos 		break;
    687  1.33  christos 	case IWN_HW_REV_TYPE_6000:
    688  1.33  christos 		sc->sc_hal = &iwn5000_hal;
    689  1.33  christos 		sc->fwname = "iwn-6000";
    690  1.33  christos 		sc->critical_temp = 110;
    691  1.33  christos 		sc->txantmsk = IWN_ANT_ABC;
    692  1.33  christos 		sc->rxantmsk = IWN_ANT_ABC;
    693  1.33  christos 		sc->ntxchains = 3;
    694  1.33  christos 		sc->nrxchains = 3;
    695  1.33  christos 		break;
    696  1.33  christos 	case IWN_HW_REV_TYPE_6050:
    697  1.33  christos 		sc->sc_hal = &iwn5000_hal;
    698  1.33  christos 		sc->fwname = "iwn-6050";
    699  1.33  christos 		sc->critical_temp = 110;
    700  1.33  christos 		sc->txantmsk = IWN_ANT_ABC;
    701  1.33  christos 		sc->rxantmsk = IWN_ANT_ABC;
    702  1.33  christos 		sc->ntxchains = 3;
    703  1.33  christos 		sc->nrxchains = 3;
    704  1.33  christos 		break;
    705  1.33  christos #endif
    706  1.33  christos 	default:
    707  1.33  christos 		printf(": adapter type %d not supported\n", sc->hw_type);
    708  1.33  christos 		return NULL;
    709  1.33  christos 	}
    710  1.33  christos 	return sc->sc_hal;
    711  1.33  christos }
    712  1.33  christos 
    713  1.33  christos #if 0
    714  1.33  christos /*
    715  1.33  christos  * Attach the adapter's on-board thermal sensor to the sensors framework.
    716  1.33  christos  */
    717  1.33  christos void
    718  1.33  christos iwn_sensor_attach(struct iwn_softc *sc)
    719  1.33  christos {
    720  1.33  christos 	strlcpy(sc->sensordev.xname, sc->sc_dev.dv_xname,
    721  1.33  christos 	    sizeof sc->sensordev.xname);
    722  1.33  christos 	sc->sensor.type = SENSOR_TEMP;
    723  1.33  christos 	/* Temperature is not valid unless interface is up. */
    724  1.33  christos 	sc->sensor.value = 0;
    725  1.33  christos 	sc->sensor.flags = SENSOR_FINVALID;
    726  1.33  christos 	sensor_attach(&sc->sensordev, &sc->sensor);
    727  1.33  christos 	sensordev_install(&sc->sensordev);
    728  1.33  christos }
    729  1.33  christos #endif /* 0 */
    730  1.33  christos 
    731   1.1      ober /*
    732   1.1      ober  * Attach the interface to 802.11 radiotap.
    733   1.1      ober  */
    734   1.1      ober static void
    735   1.1      ober iwn_radiotap_attach(struct iwn_softc *sc)
    736   1.1      ober {
    737   1.1      ober 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    738  1.36     pooka 
    739  1.38     joerg 	bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
    740  1.36     pooka 	    sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
    741  1.36     pooka 	    &sc->sc_drvbpf);
    742   1.1      ober 
    743   1.1      ober 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
    744   1.1      ober 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
    745   1.1      ober 	sc->sc_rxtap.wr_ihdr.it_present = htole32(IWN_RX_RADIOTAP_PRESENT);
    746   1.1      ober 
    747   1.1      ober 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
    748   1.1      ober 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
    749   1.1      ober 	sc->sc_txtap.wt_ihdr.it_present = htole32(IWN_TX_RADIOTAP_PRESENT);
    750   1.1      ober }
    751   1.1      ober 
    752  1.33  christos #if 0 /* XXX */
    753   1.1      ober /*
    754   1.1      ober  * Build a beacon frame that the firmware will broadcast periodically in
    755   1.1      ober  * IBSS or HostAP modes.
    756   1.1      ober  */
    757   1.1      ober static int
    758   1.1      ober iwn_setup_beacon(struct iwn_softc *sc, struct ieee80211_node *ni)
    759   1.1      ober {
    760   1.1      ober 	struct ieee80211com *ic = &sc->sc_ic;
    761   1.1      ober 	struct iwn_tx_ring *ring = &sc->txq[4];
    762   1.1      ober 	struct iwn_tx_desc *desc;
    763   1.1      ober 	struct iwn_tx_data *data;
    764   1.1      ober 	struct iwn_tx_cmd *cmd;
    765   1.1      ober 	struct iwn_cmd_beacon *bcn;
    766   1.1      ober 	struct ieee80211_beacon_offsets bo;
    767   1.1      ober 	struct mbuf *m0;
    768   1.1      ober 	bus_addr_t paddr;
    769   1.1      ober 	int error;
    770   1.1      ober 
    771   1.1      ober 	desc = &ring->desc[ring->cur];
    772   1.1      ober 	data = &ring->data[ring->cur];
    773   1.1      ober 	m0 = ieee80211_beacon_alloc(ic, ni, &bo);
    774   1.1      ober 	if (m0 == NULL) {
    775  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not allocate beacon frame\n"
    776  1.33  christos );
    777   1.1      ober 		return ENOMEM;
    778   1.1      ober 	}
    779   1.1      ober 	cmd = &ring->cmd[ring->cur];
    780   1.1      ober 	cmd->code = IWN_CMD_SET_BEACON;
    781   1.1      ober 	cmd->flags = 0;
    782   1.1      ober 	cmd->qid = ring->qid;
    783   1.1      ober 	cmd->idx = ring->cur;
    784   1.1      ober 
    785   1.1      ober 	bcn = (struct iwn_cmd_beacon *)cmd->data;
    786   1.1      ober 	memset(bcn, 0, sizeof (struct iwn_cmd_beacon));
    787  1.33  christos 	bcn->id = sc->sc_hal->broadcast_id;
    788   1.1      ober 	bcn->lifetime = htole32(IWN_LIFETIME_INFINITE);
    789   1.1      ober 	bcn->len = htole16(m0->m_pkthdr.len);
    790  1.33  christos #if 0
    791  1.33  christos 	XXX
    792   1.1      ober 	bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
    793  1.33  christos 		iwn_plcp_signal(12) : iwn_plcp_signal(2);
    794  1.33  christos #endif
    795  1.20     blymn 	bcn->flags2 = 0x2; /* RATE_MCS_CCK_MSK */
    796  1.33  christos 	bcn->flags = htole32(IWN_TX_AUTO_SEQ | IWN_TX_INSERT_TSTAMP;
    797  1.33  christos 		// XXX | IWN_TX_USE_NODE_RATE);
    798   1.1      ober 
    799   1.1      ober 	/* save and trim IEEE802.11 header */
    800   1.1      ober 	m_copydata(m0, 0, sizeof (struct ieee80211_frame), (void *)&bcn->wh);
    801   1.1      ober 	m_adj(m0, sizeof (struct ieee80211_frame));
    802   1.1      ober 
    803   1.1      ober 	/* assume beacon frame is contiguous */
    804   1.1      ober 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
    805  1.33  christos 		BUS_DMA_READ | BUS_DMA_NOWAIT);
    806   1.1      ober 	if (error) {
    807   1.1      ober 		aprint_error_dev(sc->sc_dev, "could not map beacon\n");
    808   1.1      ober 		m_freem(m0);
    809   1.1      ober 		return error;
    810   1.1      ober 	}
    811   1.1      ober 
    812   1.1      ober 	data->m = m0;
    813   1.1      ober 
    814   1.1      ober 	/* first scatter/gather segment is used by the beacon command */
    815   1.1      ober 	paddr = ring->cmd_dma.paddr + ring->cur * sizeof (struct iwn_tx_cmd);
    816   1.8     blymn 
    817   1.1      ober 	IWN_SET_DESC_NSEGS(desc, 2);
    818   1.1      ober 	IWN_SET_DESC_SEG(desc, 0, paddr , 4 + sizeof(struct iwn_cmd_beacon));
    819   1.1      ober 	IWN_SET_DESC_SEG(desc, 1,  data->map->dm_segs[0].ds_addr,
    820  1.33  christos 					   data->map->dm_segs[1].ds_len);
    821   1.8     blymn 
    822  1.20     blymn 	bus_dmamap_sync(sc->sc_dmat, data->map, 0,
    823  1.20     blymn 	    data->map->dm_mapsize /* calc? */, BUS_DMASYNC_PREWRITE);
    824   1.1      ober 
    825   1.1      ober 	/* kick cmd ring */
    826   1.1      ober 	ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT;
    827   1.1      ober 	IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur);
    828  1.33  christos 	return 0;
    829  1.33  christos }
    830  1.33  christos #endif
    831  1.33  christos 
    832  1.33  christos static int
    833  1.33  christos iwn_nic_lock(struct iwn_softc *sc)
    834  1.33  christos {
    835  1.33  christos 	int ntries;
    836  1.33  christos 
    837  1.33  christos 	/* Request exclusive access to NIC. */
    838  1.33  christos 	IWN_SETBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_MAC_ACCESS_REQ);
    839  1.33  christos 
    840  1.33  christos 	/* Spin until we actually get the lock. */
    841  1.33  christos 	for (ntries = 0; ntries < 1000; ntries++) {
    842  1.33  christos 		if ((IWN_READ(sc, IWN_GP_CNTRL) &
    843  1.33  christos 		     (IWN_GP_CNTRL_MAC_ACCESS_ENA | IWN_GP_CNTRL_SLEEP)) ==
    844  1.33  christos 		    IWN_GP_CNTRL_MAC_ACCESS_ENA)
    845  1.33  christos 			return 0;
    846  1.33  christos 		DELAY(10);
    847  1.33  christos 	}
    848  1.33  christos 	return ETIMEDOUT;
    849  1.33  christos }
    850  1.33  christos 
    851  1.33  christos static __inline void
    852  1.33  christos iwn_nic_unlock(struct iwn_softc *sc)
    853  1.33  christos {
    854  1.33  christos 	IWN_CLRBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_MAC_ACCESS_REQ);
    855  1.33  christos }
    856  1.33  christos 
    857  1.33  christos static __inline uint32_t
    858  1.33  christos iwn_prph_read(struct iwn_softc *sc, uint32_t addr)
    859  1.33  christos {
    860  1.33  christos 	IWN_WRITE(sc, IWN_PRPH_RADDR, IWN_PRPH_DWORD | addr);
    861  1.33  christos 	return IWN_READ(sc, IWN_PRPH_RDATA);
    862  1.33  christos }
    863  1.33  christos 
    864  1.33  christos static __inline void
    865  1.33  christos iwn_prph_write(struct iwn_softc *sc, uint32_t addr, uint32_t data)
    866  1.33  christos {
    867  1.33  christos 	IWN_WRITE(sc, IWN_PRPH_WADDR, IWN_PRPH_DWORD | addr);
    868  1.33  christos 	IWN_WRITE(sc, IWN_PRPH_WDATA, data);
    869  1.33  christos }
    870  1.33  christos 
    871  1.33  christos static __inline void
    872  1.33  christos iwn_prph_setbits(struct iwn_softc *sc, uint32_t addr, uint32_t mask)
    873  1.33  christos {
    874  1.33  christos 	iwn_prph_write(sc, addr, iwn_prph_read(sc, addr) | mask);
    875  1.33  christos }
    876  1.33  christos 
    877  1.33  christos static __inline void
    878  1.33  christos iwn_prph_clrbits(struct iwn_softc *sc, uint32_t addr, uint32_t mask)
    879  1.33  christos {
    880  1.33  christos 	iwn_prph_write(sc, addr, iwn_prph_read(sc, addr) & ~mask);
    881  1.33  christos }
    882  1.33  christos 
    883  1.33  christos static __inline void
    884  1.33  christos iwn_prph_write_region_4(struct iwn_softc *sc, uint32_t addr,
    885  1.33  christos     const uint32_t *data, int count)
    886  1.33  christos {
    887  1.33  christos 	for (; count > 0; count--, data++, addr += 4)
    888  1.33  christos 		iwn_prph_write(sc, addr, *data);
    889  1.33  christos }
    890  1.33  christos 
    891  1.33  christos static __inline uint32_t
    892  1.33  christos iwn_mem_read(struct iwn_softc *sc, uint32_t addr)
    893  1.33  christos {
    894  1.33  christos 	IWN_WRITE(sc, IWN_MEM_RADDR, addr);
    895  1.33  christos 	return IWN_READ(sc, IWN_MEM_RDATA);
    896  1.33  christos }
    897  1.33  christos 
    898  1.33  christos static __inline void
    899  1.33  christos iwn_mem_write(struct iwn_softc *sc, uint32_t addr, uint32_t data)
    900  1.33  christos {
    901  1.33  christos 	IWN_WRITE(sc, IWN_MEM_WADDR, addr);
    902  1.33  christos 	IWN_WRITE(sc, IWN_MEM_WDATA, data);
    903  1.33  christos }
    904  1.33  christos 
    905  1.33  christos static __inline void
    906  1.33  christos iwn_mem_write_2(struct iwn_softc *sc, uint32_t addr, uint16_t data)
    907  1.33  christos {
    908  1.33  christos 	uint32_t tmp;
    909  1.33  christos 
    910  1.33  christos 	tmp = iwn_mem_read(sc, addr & ~3);
    911  1.33  christos 	if (addr & 3)
    912  1.33  christos 		tmp = (tmp & 0x0000ffff) | data << 16;
    913  1.33  christos 	else
    914  1.33  christos 		tmp = (tmp & 0xffff0000) | data;
    915  1.33  christos 	iwn_mem_write(sc, addr & ~3, tmp);
    916  1.33  christos }
    917  1.33  christos 
    918  1.33  christos static __inline void
    919  1.33  christos iwn_mem_read_region_4(struct iwn_softc *sc, uint32_t addr, uint32_t *data,
    920  1.33  christos     int count)
    921  1.33  christos {
    922  1.33  christos 	for (; count > 0; count--, addr += 4)
    923  1.33  christos 		*data++ = iwn_mem_read(sc, addr);
    924  1.33  christos }
    925  1.33  christos 
    926  1.33  christos static __inline void
    927  1.33  christos iwn_mem_set_region_4(struct iwn_softc *sc, uint32_t addr, uint32_t val,
    928  1.33  christos     int count)
    929  1.33  christos {
    930  1.33  christos 	for (; count > 0; count--, addr += 4)
    931  1.33  christos 		iwn_mem_write(sc, addr, val);
    932  1.33  christos }
    933  1.33  christos 
    934  1.33  christos static int
    935  1.33  christos iwn_eeprom_lock(struct iwn_softc *sc)
    936  1.33  christos {
    937  1.33  christos 	int i, ntries;
    938  1.33  christos 
    939  1.33  christos 	for (i = 0; i < 100; i++) {
    940  1.33  christos 		/* Request exclusive access to EEPROM. */
    941  1.33  christos 		IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
    942  1.33  christos 		    IWN_HW_IF_CONFIG_EEPROM_LOCKED);
    943  1.33  christos 
    944  1.33  christos 		/* Spin until we actually get the lock. */
    945  1.33  christos 		for (ntries = 0; ntries < 100; ntries++) {
    946  1.33  christos 			if (IWN_READ(sc, IWN_HW_IF_CONFIG) &
    947  1.33  christos 			    IWN_HW_IF_CONFIG_EEPROM_LOCKED)
    948  1.33  christos 				return 0;
    949  1.33  christos 			DELAY(10);
    950  1.33  christos 		}
    951  1.33  christos 	}
    952  1.33  christos 	return ETIMEDOUT;
    953  1.33  christos }
    954  1.33  christos 
    955  1.33  christos static __inline void
    956  1.33  christos iwn_eeprom_unlock(struct iwn_softc *sc)
    957  1.33  christos {
    958  1.33  christos 	IWN_CLRBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_EEPROM_LOCKED);
    959  1.33  christos }
    960  1.33  christos 
    961  1.33  christos static int
    962  1.33  christos iwn_read_prom_data(struct iwn_softc *sc, uint32_t addr, void *data, int count)
    963  1.33  christos {
    964  1.33  christos 	uint8_t *out = data;
    965  1.33  christos 	uint32_t val;
    966  1.33  christos 	int ntries;
    967   1.1      ober 
    968  1.33  christos 	for (; count > 0; count -= 2, addr++) {
    969  1.33  christos 		IWN_WRITE(sc, IWN_EEPROM, addr << 2);
    970  1.33  christos 		IWN_CLRBITS(sc, IWN_EEPROM, IWN_EEPROM_CMD);
    971  1.33  christos 
    972  1.33  christos 		for (ntries = 0; ntries < 10; ntries++) {
    973  1.33  christos 			val = IWN_READ(sc, IWN_EEPROM);
    974  1.33  christos 			if (val & IWN_EEPROM_READ_VALID)
    975  1.33  christos 				break;
    976  1.33  christos 			DELAY(5);
    977  1.33  christos 		}
    978  1.33  christos 		if (ntries == 10) {
    979  1.33  christos 			aprint_error_dev(sc->sc_dev, "could not read EEPROM\n");
    980  1.33  christos 			return ETIMEDOUT;
    981  1.33  christos 		}
    982  1.33  christos 		*out++ = val >> 16;
    983  1.33  christos 		if (count > 1)
    984  1.33  christos 			*out++ = val >> 24;
    985  1.33  christos 	}
    986   1.1      ober 	return 0;
    987   1.1      ober }
    988   1.1      ober 
    989   1.1      ober static int
    990   1.1      ober iwn_dma_contig_alloc(bus_dma_tag_t tag, struct iwn_dma_info *dma, void **kvap,
    991   1.1      ober     bus_size_t size, bus_size_t alignment, int flags)
    992   1.1      ober {
    993   1.1      ober 	int nsegs, error;
    994   1.1      ober 
    995   1.1      ober 	dma->tag = tag;
    996   1.1      ober 	dma->size = size;
    997   1.1      ober 
    998   1.1      ober 	error = bus_dmamap_create(tag, size, 1, size, 0, flags, &dma->map);
    999   1.1      ober 	if (error != 0)
   1000   1.1      ober 		goto fail;
   1001   1.1      ober 
   1002   1.1      ober 	error = bus_dmamem_alloc(tag, size, alignment, 0, &dma->seg, 1, &nsegs,
   1003   1.1      ober 	    flags);
   1004   1.1      ober 	if (error != 0)
   1005   1.1      ober 		goto fail;
   1006   1.1      ober 
   1007   1.1      ober 	error = bus_dmamem_map(tag, &dma->seg, 1, size, &dma->vaddr, flags);
   1008   1.1      ober 	if (error != 0)
   1009   1.1      ober 		goto fail;
   1010   1.1      ober 
   1011   1.1      ober 	error = bus_dmamap_load(tag, dma->map, dma->vaddr, size, NULL, flags);
   1012   1.1      ober 	if (error != 0)
   1013   1.1      ober 		goto fail;
   1014   1.1      ober 
   1015   1.1      ober 	memset(dma->vaddr, 0, size);
   1016  1.33  christos 	bus_dmamap_sync(tag, dma->map, 0, size, BUS_DMASYNC_PREWRITE);
   1017   1.1      ober 
   1018   1.1      ober 	dma->paddr = dma->map->dm_segs[0].ds_addr;
   1019   1.1      ober 	if (kvap != NULL)
   1020   1.1      ober 		*kvap = dma->vaddr;
   1021   1.1      ober 
   1022   1.1      ober 	return 0;
   1023   1.1      ober 
   1024   1.1      ober fail:	iwn_dma_contig_free(dma);
   1025   1.1      ober 	return error;
   1026   1.1      ober }
   1027   1.1      ober 
   1028   1.1      ober static void
   1029   1.1      ober iwn_dma_contig_free(struct iwn_dma_info *dma)
   1030   1.1      ober {
   1031   1.1      ober 	if (dma->map != NULL) {
   1032   1.1      ober 		if (dma->vaddr != NULL) {
   1033  1.33  christos 			bus_dmamap_sync(dma->tag, dma->map, 0, dma->size,
   1034  1.33  christos 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1035   1.1      ober 			bus_dmamap_unload(dma->tag, dma->map);
   1036   1.1      ober 			bus_dmamem_unmap(dma->tag, dma->vaddr, dma->size);
   1037   1.1      ober 			bus_dmamem_free(dma->tag, &dma->seg, 1);
   1038   1.1      ober 			dma->vaddr = NULL;
   1039   1.1      ober 		}
   1040   1.1      ober 		bus_dmamap_destroy(dma->tag, dma->map);
   1041   1.1      ober 		dma->map = NULL;
   1042   1.1      ober 	}
   1043   1.1      ober }
   1044   1.1      ober 
   1045   1.1      ober static int
   1046  1.33  christos iwn_alloc_sched(struct iwn_softc *sc)
   1047   1.1      ober {
   1048  1.15  christos 	int error;
   1049  1.33  christos 	/* TX scheduler rings must be aligned on a 1KB boundary. */
   1050  1.33  christos 	error = iwn_dma_contig_alloc(sc->sc_dmat, &sc->sched_dma,
   1051  1.33  christos 	    (void **)&sc->sched, sc->sc_hal->schedsz, 1024, BUS_DMA_NOWAIT);
   1052   1.1      ober 	if (error != 0)
   1053   1.2      ober 		aprint_error_dev(sc->sc_dev,
   1054   1.2      ober 		    "could not allocate shared area DMA memory\n");
   1055   1.1      ober 	return error;
   1056   1.1      ober }
   1057   1.1      ober 
   1058   1.1      ober static void
   1059  1.33  christos iwn_free_sched(struct iwn_softc *sc)
   1060   1.1      ober {
   1061  1.33  christos 	iwn_dma_contig_free(&sc->sched_dma);
   1062   1.1      ober }
   1063   1.1      ober 
   1064   1.1      ober static int
   1065   1.1      ober iwn_alloc_kw(struct iwn_softc *sc)
   1066   1.1      ober {
   1067  1.33  christos 	/* "Keep Warm" page must be aligned on a 16-byte boundary. */
   1068  1.33  christos 	return iwn_dma_contig_alloc(sc->sc_dmat, &sc->kw_dma, NULL, 4096,
   1069  1.33  christos 	    4096, BUS_DMA_NOWAIT);
   1070   1.1      ober }
   1071   1.1      ober 
   1072   1.1      ober static void
   1073   1.1      ober iwn_free_kw(struct iwn_softc *sc)
   1074   1.1      ober {
   1075   1.1      ober 	iwn_dma_contig_free(&sc->kw_dma);
   1076   1.1      ober }
   1077   1.1      ober 
   1078   1.1      ober static int
   1079   1.1      ober iwn_alloc_fwmem(struct iwn_softc *sc)
   1080   1.1      ober {
   1081   1.1      ober 	int error;
   1082  1.33  christos 	/* Must be aligned on a 16-byte boundary. */
   1083   1.1      ober 	error = iwn_dma_contig_alloc(sc->sc_dmat, &sc->fw_dma, NULL,
   1084  1.33  christos 	    sc->sc_hal->fwsz, 16, BUS_DMA_NOWAIT);
   1085  1.33  christos 	if (error != 0) {
   1086   1.1      ober 		aprint_error_dev(sc->sc_dev,
   1087  1.33  christos 		    "could not allocate firmware transfer area DMA memory\n");
   1088   1.1      ober 	}
   1089   1.1      ober 	return error;
   1090   1.1      ober }
   1091   1.1      ober 
   1092   1.1      ober static void
   1093   1.1      ober iwn_free_fwmem(struct iwn_softc *sc)
   1094   1.1      ober {
   1095   1.1      ober 	iwn_dma_contig_free(&sc->fw_dma);
   1096   1.1      ober }
   1097   1.1      ober 
   1098   1.1      ober static struct iwn_rbuf *
   1099   1.1      ober iwn_alloc_rbuf(struct iwn_softc *sc)
   1100   1.1      ober {
   1101   1.1      ober 	struct iwn_rbuf *rbuf;
   1102  1.33  christos 	mutex_enter(&sc->rxq.freelist_mtx);
   1103   1.1      ober 
   1104   1.1      ober 	rbuf = SLIST_FIRST(&sc->rxq.freelist);
   1105  1.17      cube 	if (rbuf != NULL) {
   1106  1.17      cube 		SLIST_REMOVE_HEAD(&sc->rxq.freelist, next);
   1107  1.17      cube 		sc->rxq.nb_free_entries --;
   1108  1.17      cube 	}
   1109  1.17      cube 	mutex_exit(&sc->rxq.freelist_mtx);
   1110   1.1      ober 	return rbuf;
   1111   1.1      ober }
   1112   1.1      ober 
   1113   1.1      ober /*
   1114   1.1      ober  * This is called automatically by the network stack when the mbuf to which
   1115  1.33  christos  * our RX buffer is attached is freed.
   1116   1.1      ober  */
   1117   1.1      ober static void
   1118   1.1      ober iwn_free_rbuf(struct mbuf* m, void *buf,  size_t size, void *arg)
   1119   1.1      ober {
   1120   1.1      ober 	struct iwn_rbuf *rbuf = arg;
   1121   1.1      ober 	struct iwn_softc *sc = rbuf->sc;
   1122   1.1      ober 
   1123  1.33  christos 	/* Put the RX buffer back in the free list. */
   1124  1.17      cube 	mutex_enter(&sc->rxq.freelist_mtx);
   1125   1.1      ober 	SLIST_INSERT_HEAD(&sc->rxq.freelist, rbuf, next);
   1126  1.17      cube 	mutex_exit(&sc->rxq.freelist_mtx);
   1127  1.33  christos 
   1128   1.1      ober 	sc->rxq.nb_free_entries ++;
   1129   1.1      ober 	if (__predict_true(m != NULL))
   1130   1.1      ober 		pool_cache_put(mb_cache, m);
   1131   1.1      ober }
   1132   1.1      ober 
   1133   1.1      ober static int
   1134   1.1      ober iwn_alloc_rpool(struct iwn_softc *sc)
   1135   1.1      ober {
   1136   1.1      ober 	struct iwn_rx_ring *ring = &sc->rxq;
   1137   1.1      ober 	struct iwn_rbuf *rbuf;
   1138   1.1      ober 	int i, error;
   1139   1.1      ober 
   1140  1.17      cube 	mutex_init(&ring->freelist_mtx, MUTEX_DEFAULT, IPL_NET);
   1141  1.17      cube 
   1142  1.33  christos 	/* Allocate a big chunk of DMA'able memory... */
   1143   1.1      ober 	error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->buf_dma, NULL,
   1144  1.33  christos 	    IWN_RBUF_COUNT * IWN_RBUF_SIZE, PAGE_SIZE, BUS_DMA_NOWAIT);
   1145   1.1      ober 	if (error != 0) {
   1146   1.3     skrll 		aprint_error_dev(sc->sc_dev,
   1147  1.33  christos 		    "could not allocate RX buffers DMA memory\n");
   1148   1.1      ober 		return error;
   1149   1.1      ober 	}
   1150  1.33  christos 	/* ...and split it into chunks of IWN_RBUF_SIZE bytes. */
   1151   1.1      ober 	SLIST_INIT(&ring->freelist);
   1152   1.1      ober 	for (i = 0; i < IWN_RBUF_COUNT; i++) {
   1153   1.1      ober 		rbuf = &ring->rbuf[i];
   1154   1.1      ober 
   1155  1.33  christos 		rbuf->sc = sc;	/* Backpointer for callbacks. */
   1156  1.33  christos 		rbuf->vaddr = (void *)((vaddr_t)ring->buf_dma.vaddr + i * IWN_RBUF_SIZE);
   1157   1.1      ober 		rbuf->paddr = ring->buf_dma.paddr + i * IWN_RBUF_SIZE;
   1158   1.1      ober 
   1159   1.1      ober 		SLIST_INSERT_HEAD(&ring->freelist, rbuf, next);
   1160   1.1      ober 	}
   1161   1.1      ober 	ring->nb_free_entries = IWN_RBUF_COUNT;
   1162   1.1      ober 	return 0;
   1163   1.1      ober }
   1164   1.1      ober 
   1165   1.1      ober static void
   1166   1.1      ober iwn_free_rpool(struct iwn_softc *sc)
   1167   1.1      ober {
   1168   1.1      ober 	iwn_dma_contig_free(&sc->rxq.buf_dma);
   1169   1.1      ober }
   1170   1.1      ober 
   1171   1.1      ober static int
   1172   1.1      ober iwn_alloc_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring)
   1173   1.1      ober {
   1174  1.15  christos 	struct iwn_rx_data *data;
   1175  1.33  christos 	bus_size_t size;
   1176  1.15  christos 	struct iwn_rbuf *rbuf;
   1177  1.15  christos 	int i, error;
   1178   1.8     blymn 
   1179   1.1      ober 	ring->cur = 0;
   1180   1.1      ober 
   1181  1.33  christos 	/* Allocate RX descriptors (256-byte aligned.) */
   1182  1.33  christos 	size = IWN_RX_RING_COUNT * sizeof (struct iwn_rx_desc);
   1183   1.1      ober 	error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
   1184  1.33  christos 	    (void **)&ring->desc, size, 256, BUS_DMA_NOWAIT);
   1185  1.33  christos 	if (error != 0) {
   1186  1.33  christos 		aprint_error_dev(sc->sc_dev,
   1187  1.33  christos 		    "could not allocate RX ring DMA memory\n");
   1188  1.33  christos 		goto fail;
   1189  1.33  christos 	}
   1190  1.33  christos 
   1191  1.33  christos 	/* Allocate RX status area (16-byte aligned.) */
   1192  1.33  christos 	error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->stat_dma,
   1193  1.33  christos 	    (void **)&ring->stat, sizeof (struct iwn_rx_status), 16,
   1194  1.33  christos 	    BUS_DMA_NOWAIT);
   1195   1.1      ober 	if (error != 0) {
   1196   1.3     skrll 		aprint_error_dev(sc->sc_dev,
   1197  1.33  christos 		    "could not allocate RX status DMA memory\n");
   1198   1.1      ober 		goto fail;
   1199   1.1      ober 	}
   1200   1.1      ober 
   1201   1.1      ober 	/*
   1202  1.33  christos 	 * Allocate and map RX buffers.
   1203   1.1      ober 	 */
   1204   1.1      ober 	for (i = 0; i < IWN_RX_RING_COUNT; i++) {
   1205   1.1      ober 		data = &ring->data[i];
   1206   1.8     blymn 
   1207  1.33  christos 		error = bus_dmamap_create(sc->sc_dmat, IWN_RBUF_SIZE, 1,
   1208  1.33  christos 		    IWN_RBUF_SIZE, 0, BUS_DMA_NOWAIT, &data->map);
   1209  1.33  christos 		if (error != 0) {
   1210  1.33  christos 			aprint_error_dev(sc->sc_dev,
   1211  1.33  christos 			    "could not create RX buf DMA map\n");
   1212  1.33  christos 			goto fail;
   1213  1.33  christos 		}
   1214   1.1      ober 		MGETHDR(data->m, M_DONTWAIT, MT_DATA);
   1215   1.1      ober 		if (data->m == NULL) {
   1216  1.33  christos 			aprint_error_dev(sc->sc_dev,
   1217  1.33  christos 			    "could not allocate RX mbuf\n");
   1218   1.1      ober 			error = ENOMEM;
   1219   1.1      ober 			goto fail;
   1220   1.1      ober 		}
   1221   1.1      ober 		if ((rbuf = iwn_alloc_rbuf(sc)) == NULL) {
   1222   1.1      ober 			m_freem(data->m);
   1223   1.1      ober 			data->m = NULL;
   1224  1.33  christos 			aprint_error_dev(sc->sc_dev,
   1225  1.33  christos 			   "could not allocate RX buffer\n");
   1226   1.1      ober 			error = ENOMEM;
   1227   1.1      ober 			goto fail;
   1228   1.1      ober 		}
   1229  1.33  christos 		/* Attach RX buffer to mbuf header. */
   1230   1.1      ober 		MEXTADD(data->m, rbuf->vaddr, IWN_RBUF_SIZE, 0, iwn_free_rbuf,
   1231   1.1      ober 		    rbuf);
   1232  1.33  christos 		data->m->m_flags |= M_EXT_RW;
   1233  1.33  christos 		error = bus_dmamap_load(sc->sc_dmat, data->map,
   1234  1.33  christos 		    data->m->m_ext.ext_buf, IWN_RBUF_SIZE, NULL,
   1235  1.33  christos 		    BUS_DMA_NOWAIT);
   1236  1.33  christos 		if (error != 0) {
   1237  1.33  christos 			aprint_error_dev(sc->sc_dev, "can't not map mbuf"
   1238  1.33  christos 			" (error %d)\n", error);
   1239  1.33  christos 			goto fail;
   1240  1.33  christos 		}
   1241   1.1      ober 
   1242  1.33  christos 		/* Set physical address of RX buffer (256-byte aligned.) */
   1243  1.33  christos 		ring->desc[i] = htole32(data->map->dm_segs[0].ds_addr >> 8);
   1244   1.1      ober 	}
   1245   1.1      ober 
   1246  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
   1247  1.33  christos 	    0, ring->desc_dma.size, BUS_DMASYNC_PREWRITE);
   1248  1.33  christos 
   1249   1.1      ober 	return 0;
   1250   1.1      ober 
   1251   1.1      ober fail:	iwn_free_rx_ring(sc, ring);
   1252   1.1      ober 	return error;
   1253   1.1      ober }
   1254   1.1      ober 
   1255   1.1      ober static void
   1256   1.1      ober iwn_reset_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring)
   1257   1.1      ober {
   1258   1.1      ober 	int ntries;
   1259   1.1      ober 
   1260  1.33  christos 	if (iwn_nic_lock(sc) == 0) {
   1261  1.33  christos 		IWN_WRITE(sc, IWN_FH_RX_CONFIG, 0);
   1262  1.33  christos 		for (ntries = 0; ntries < 1000; ntries++) {
   1263  1.33  christos 			if (IWN_READ(sc, IWN_FH_RX_STATUS) &
   1264  1.33  christos 			    IWN_FH_RX_STATUS_IDLE)
   1265  1.33  christos 				break;
   1266  1.33  christos 			DELAY(10);
   1267  1.33  christos 		}
   1268  1.33  christos 		iwn_nic_unlock(sc);
   1269   1.1      ober 	}
   1270   1.1      ober 	ring->cur = 0;
   1271  1.33  christos 	sc->last_rx_valid = 0;
   1272   1.1      ober }
   1273   1.1      ober 
   1274   1.1      ober static void
   1275   1.1      ober iwn_free_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring)
   1276   1.1      ober {
   1277   1.1      ober 	int i;
   1278   1.1      ober 
   1279   1.1      ober 	iwn_dma_contig_free(&ring->desc_dma);
   1280  1.33  christos 	iwn_dma_contig_free(&ring->stat_dma);
   1281   1.1      ober 
   1282   1.1      ober 	for (i = 0; i < IWN_RX_RING_COUNT; i++) {
   1283  1.33  christos 		struct iwn_rx_data *data = &ring->data[i];
   1284  1.33  christos 
   1285  1.33  christos 		if (data->m != NULL) {
   1286  1.33  christos 			bus_dmamap_sync(sc->sc_dmat, data->map, 0,
   1287  1.33  christos 			    data->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1288  1.33  christos 			bus_dmamap_unload(sc->sc_dmat, data->map);
   1289  1.33  christos 			m_freem(data->m);
   1290  1.33  christos 		}
   1291  1.33  christos 		if (data->map != NULL)
   1292  1.33  christos 			bus_dmamap_destroy(sc->sc_dmat, data->map);
   1293   1.1      ober 	}
   1294   1.1      ober }
   1295   1.1      ober 
   1296   1.1      ober static int
   1297   1.1      ober iwn_alloc_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring, int count,
   1298   1.1      ober     int qid)
   1299   1.1      ober {
   1300  1.33  christos 	bus_addr_t paddr;
   1301   1.2      ober 	struct iwn_tx_data *data;
   1302  1.33  christos 	int i, error, size;
   1303   1.1      ober 
   1304   1.1      ober 	ring->qid = qid;
   1305   1.1      ober 	ring->count = count;
   1306   1.1      ober 	ring->queued = 0;
   1307   1.1      ober 	ring->cur = 0;
   1308   1.1      ober 
   1309  1.33  christos 	/* Allocate TX descriptors (256-byte aligned.) */
   1310  1.33  christos 	size = count * sizeof (struct iwn_tx_desc);
   1311   1.1      ober 	error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
   1312  1.33  christos 	    (void **)&ring->desc, size, 256, BUS_DMA_NOWAIT);
   1313   1.1      ober 	if (error != 0) {
   1314  1.33  christos 		aprint_error_dev(sc->sc_dev,
   1315  1.33  christos 		    "could not allocate TX ring DMA memory\n");
   1316   1.1      ober 		goto fail;
   1317   1.1      ober 	}
   1318  1.33  christos 	/*
   1319  1.33  christos 	 * We only use rings 0 through 4 (4 EDCA + cmd) so there is no need
   1320  1.33  christos 	 * to allocate commands space for other rings.
   1321  1.33  christos 	 * XXX Do we really need to allocate descriptors for other rings?
   1322  1.33  christos 	 */
   1323  1.33  christos 	if (qid > 4)
   1324  1.33  christos 		return 0;
   1325   1.1      ober 
   1326  1.33  christos 	size = count * sizeof (struct iwn_tx_cmd);
   1327   1.1      ober 	error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->cmd_dma,
   1328  1.33  christos 	    (void **)&ring->cmd, size, 4, BUS_DMA_NOWAIT);
   1329   1.1      ober 	if (error != 0) {
   1330  1.33  christos 		aprint_error_dev(sc->sc_dev,
   1331  1.33  christos 		    "could not allocate TX cmd DMA memory\n");
   1332   1.1      ober 		goto fail;
   1333   1.1      ober 	}
   1334   1.1      ober 
   1335  1.33  christos 	paddr = ring->cmd_dma.paddr;
   1336   1.1      ober 
   1337   1.1      ober 	for (i = 0; i < count; i++) {
   1338   1.2      ober 		data = &ring->data[i];
   1339   1.1      ober 
   1340  1.33  christos 		data->cmd_paddr = paddr;
   1341  1.33  christos 		data->scratch_paddr = paddr + 12;
   1342  1.33  christos 		paddr += sizeof (struct iwn_tx_cmd);
   1343  1.33  christos 
   1344   1.1      ober 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
   1345   1.1      ober 		    IWN_MAX_SCATTER - 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
   1346   1.1      ober 		    &data->map);
   1347   1.1      ober 		if (error != 0) {
   1348  1.33  christos 			aprint_error_dev(sc->sc_dev,
   1349  1.33  christos 			    "could not create TX buf DMA map\n");
   1350   1.1      ober 			goto fail;
   1351   1.1      ober 		}
   1352   1.1      ober 	}
   1353   1.1      ober 	return 0;
   1354   1.1      ober 
   1355   1.1      ober fail:	iwn_free_tx_ring(sc, ring);
   1356   1.1      ober 	return error;
   1357   1.1      ober }
   1358   1.1      ober 
   1359   1.1      ober static void
   1360   1.1      ober iwn_reset_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring)
   1361   1.1      ober {
   1362  1.15  christos 	struct iwn_tx_data *data;
   1363   1.1      ober 	uint32_t tmp;
   1364   1.1      ober 	int i, ntries;
   1365   1.1      ober 
   1366  1.33  christos 	if (iwn_nic_lock(sc) == 0) {
   1367  1.33  christos 		IWN_WRITE(sc, IWN_FH_TX_CONFIG(ring->qid), 0);
   1368  1.33  christos 		for (ntries = 0; ntries < 200; ntries++) {
   1369  1.33  christos 			tmp = IWN_READ(sc, IWN_FH_TX_STATUS);
   1370  1.33  christos 			if ((tmp & IWN_FH_TX_STATUS_IDLE(ring->qid)) ==
   1371  1.33  christos 			    IWN_FH_TX_STATUS_IDLE(ring->qid))
   1372  1.33  christos 				break;
   1373  1.33  christos 			DELAY(10);
   1374  1.33  christos 		}
   1375  1.33  christos 		iwn_nic_unlock(sc);
   1376   1.1      ober 	}
   1377   1.1      ober 	for (i = 0; i < ring->count; i++) {
   1378   1.1      ober 		data = &ring->data[i];
   1379   1.1      ober 
   1380   1.1      ober 		if (data->m != NULL) {
   1381  1.33  christos 			bus_dmamap_sync(sc->sc_dmat, data->map, 0,
   1382  1.33  christos 			    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1383   1.1      ober 			bus_dmamap_unload(sc->sc_dmat, data->map);
   1384   1.1      ober 			m_freem(data->m);
   1385   1.1      ober 			data->m = NULL;
   1386   1.1      ober 		}
   1387   1.1      ober 	}
   1388  1.33  christos 	/* Clear TX descriptors. */
   1389  1.33  christos 	memset(ring->desc, 0, ring->desc_dma.size);
   1390  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0,
   1391  1.33  christos 	    ring->desc_dma.size, BUS_DMASYNC_PREWRITE);
   1392  1.33  christos 	sc->qfullmsk &= ~(1 << ring->qid);
   1393   1.1      ober 	ring->queued = 0;
   1394   1.1      ober 	ring->cur = 0;
   1395   1.1      ober }
   1396   1.1      ober 
   1397   1.1      ober static void
   1398   1.1      ober iwn_free_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring)
   1399   1.1      ober {
   1400   1.2      ober 	struct iwn_tx_data *data;
   1401   1.2      ober 	int i;
   1402   1.1      ober 
   1403   1.1      ober 	iwn_dma_contig_free(&ring->desc_dma);
   1404   1.1      ober 	iwn_dma_contig_free(&ring->cmd_dma);
   1405   1.1      ober 
   1406   1.1      ober 	if (ring->data != NULL) {
   1407   1.1      ober 		for (i = 0; i < ring->count; i++) {
   1408   1.1      ober 			data = &ring->data[i];
   1409   1.1      ober 
   1410   1.1      ober 			if (data->m != NULL) {
   1411  1.33  christos 				bus_dmamap_sync(sc->sc_dmat, data->map, 0,
   1412  1.33  christos 				    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1413   1.1      ober 				bus_dmamap_unload(sc->sc_dmat, data->map);
   1414   1.1      ober 				m_freem(data->m);
   1415   1.1      ober 			}
   1416  1.33  christos 			if (data->map != NULL)
   1417  1.33  christos 				bus_dmamap_destroy(sc->sc_dmat, data->map);
   1418   1.1      ober 		}
   1419   1.1      ober 		free(ring->data, M_DEVBUF);
   1420   1.1      ober 	}
   1421   1.1      ober }
   1422   1.1      ober 
   1423  1.33  christos static int
   1424  1.33  christos iwn_read_eeprom(struct iwn_softc *sc)
   1425   1.1      ober {
   1426  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   1427  1.33  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1428  1.33  christos 	uint16_t val;
   1429  1.33  christos 	int error;
   1430  1.33  christos 
   1431  1.33  christos 	if ((IWN_READ(sc, IWN_EEPROM_GP) & 0x6) == 0) {
   1432  1.33  christos 		aprint_error_dev(sc->sc_dev, "bad EEPROM signature\n");
   1433  1.33  christos 		return EIO;
   1434  1.33  christos 	}
   1435  1.33  christos 	if ((error = iwn_eeprom_lock(sc)) != 0) {
   1436  1.33  christos 		aprint_error_dev(sc->sc_dev,
   1437  1.33  christos 		    "could not lock EEPROM (error=%d)\n", error);
   1438  1.33  christos 		return error;
   1439  1.33  christos 	}
   1440  1.33  christos 
   1441  1.33  christos 	iwn_read_prom_data(sc, IWN_EEPROM_RFCFG, &val, 2);
   1442  1.33  christos 	sc->rfcfg = le16toh(val);
   1443  1.33  christos 	DPRINTF(("radio config=0x%04x\n", sc->rfcfg));
   1444  1.33  christos 
   1445  1.33  christos 	/* Read MAC address. */
   1446  1.33  christos 	iwn_read_prom_data(sc, IWN_EEPROM_MAC, ic->ic_myaddr, 6);
   1447  1.33  christos 
   1448  1.33  christos 	/* Read adapter-specific information from EEPROM. */
   1449  1.33  christos 	hal->read_eeprom(sc);
   1450  1.33  christos 
   1451  1.33  christos 	iwn_eeprom_unlock(sc);
   1452  1.33  christos 	return 0;
   1453  1.33  christos }
   1454  1.33  christos 
   1455  1.33  christos static void
   1456  1.33  christos iwn4965_read_eeprom(struct iwn_softc *sc)
   1457  1.33  christos {
   1458  1.33  christos 	uint32_t addr;
   1459  1.33  christos 	uint16_t val;
   1460  1.33  christos 	int i;
   1461  1.33  christos 
   1462  1.33  christos 	/* Read regulatory domain (4 ASCII characters.) */
   1463  1.33  christos 	iwn_read_prom_data(sc, IWN4965_EEPROM_DOMAIN, sc->eeprom_domain, 4);
   1464  1.33  christos 
   1465  1.33  christos 	/* Read the list of authorized channels (20MHz ones only.) */
   1466  1.33  christos 	for (i = 0; i < 5; i++) {
   1467  1.33  christos 		addr = iwn4965_regulatory_bands[i];
   1468  1.33  christos 		iwn_read_eeprom_channels(sc, i, addr);
   1469  1.33  christos 	}
   1470  1.33  christos 
   1471  1.33  christos 	/* Read maximum allowed TX power for 2GHz and 5GHz bands. */
   1472  1.33  christos 	iwn_read_prom_data(sc, IWN4965_EEPROM_MAXPOW, &val, 2);
   1473  1.33  christos 	sc->maxpwr2GHz = val & 0xff;
   1474  1.33  christos 	sc->maxpwr5GHz = val >> 8;
   1475  1.33  christos 	/* Check that EEPROM values are within valid range. */
   1476  1.33  christos 	if (sc->maxpwr5GHz < 20 || sc->maxpwr5GHz > 50)
   1477  1.33  christos 		sc->maxpwr5GHz = 38;
   1478  1.33  christos 	if (sc->maxpwr2GHz < 20 || sc->maxpwr2GHz > 50)
   1479  1.33  christos 		sc->maxpwr2GHz = 38;
   1480  1.33  christos 	DPRINTF(("maxpwr 2GHz=%d 5GHz=%d\n", sc->maxpwr2GHz, sc->maxpwr5GHz));
   1481  1.33  christos 
   1482  1.33  christos 	/* Read samples for each TX power group. */
   1483  1.33  christos 	iwn_read_prom_data(sc, IWN4965_EEPROM_BANDS, sc->bands,
   1484  1.33  christos 	    sizeof sc->bands);
   1485  1.33  christos 
   1486  1.33  christos 	/* Read voltage at which samples were taken. */
   1487  1.33  christos 	iwn_read_prom_data(sc, IWN4965_EEPROM_VOLTAGE, &val, 2);
   1488  1.33  christos 	sc->eeprom_voltage = (int16_t)le16toh(val);
   1489  1.33  christos 	DPRINTF(("voltage=%d (in 0.3V)\n", sc->eeprom_voltage));
   1490  1.33  christos 
   1491  1.33  christos #ifdef IWN_DEBUG
   1492  1.33  christos 	/* Print samples. */
   1493  1.33  christos 	if (iwn_debug > 0) {
   1494  1.33  christos 		for (i = 0; i < IWN_NBANDS; i++)
   1495  1.33  christos 			iwn4965_print_power_group(sc, i);
   1496  1.33  christos 	}
   1497  1.33  christos #endif
   1498  1.33  christos }
   1499  1.33  christos 
   1500  1.33  christos #ifdef IWN_DEBUG
   1501  1.33  christos static void
   1502  1.33  christos iwn4965_print_power_group(struct iwn_softc *sc, int i)
   1503  1.33  christos {
   1504  1.33  christos 	struct iwn4965_eeprom_band *band = &sc->bands[i];
   1505  1.33  christos 	struct iwn4965_eeprom_chan_samples *chans = band->chans;
   1506  1.33  christos 	int j, c;
   1507  1.33  christos 
   1508  1.33  christos 	printf("===band %d===\n", i);
   1509  1.33  christos 	printf("chan lo=%d, chan hi=%d\n", band->lo, band->hi);
   1510  1.33  christos 	printf("chan1 num=%d\n", chans[0].num);
   1511  1.33  christos 	for (c = 0; c < 2; c++) {
   1512  1.33  christos 		for (j = 0; j < IWN_NSAMPLES; j++) {
   1513  1.33  christos 			printf("chain %d, sample %d: temp=%d gain=%d "
   1514  1.33  christos 			    "power=%d pa_det=%d\n", c, j,
   1515  1.33  christos 			    chans[0].samples[c][j].temp,
   1516  1.33  christos 			    chans[0].samples[c][j].gain,
   1517  1.33  christos 			    chans[0].samples[c][j].power,
   1518  1.33  christos 			    chans[0].samples[c][j].pa_det);
   1519  1.33  christos 		}
   1520  1.33  christos 	}
   1521  1.33  christos 	printf("chan2 num=%d\n", chans[1].num);
   1522  1.33  christos 	for (c = 0; c < 2; c++) {
   1523  1.33  christos 		for (j = 0; j < IWN_NSAMPLES; j++) {
   1524  1.33  christos 			printf("chain %d, sample %d: temp=%d gain=%d "
   1525  1.33  christos 			    "power=%d pa_det=%d\n", c, j,
   1526  1.33  christos 			    chans[1].samples[c][j].temp,
   1527  1.33  christos 			    chans[1].samples[c][j].gain,
   1528  1.33  christos 			    chans[1].samples[c][j].power,
   1529  1.33  christos 			    chans[1].samples[c][j].pa_det);
   1530  1.33  christos 		}
   1531  1.33  christos 	}
   1532  1.33  christos }
   1533  1.33  christos #endif
   1534  1.33  christos 
   1535  1.33  christos static void
   1536  1.33  christos iwn5000_read_eeprom(struct iwn_softc *sc)
   1537  1.33  christos {
   1538  1.33  christos 	int32_t temp, volt, delta;
   1539  1.33  christos 	uint32_t base, addr;
   1540  1.33  christos 	uint16_t val;
   1541  1.33  christos 	int i;
   1542  1.33  christos 
   1543  1.33  christos 	/* Read regulatory domain (4 ASCII characters.) */
   1544  1.33  christos 	iwn_read_prom_data(sc, IWN5000_EEPROM_REG, &val, 2);
   1545  1.33  christos 	base = le16toh(val);
   1546  1.33  christos 	iwn_read_prom_data(sc, base + IWN5000_EEPROM_DOMAIN,
   1547  1.33  christos 	    sc->eeprom_domain, 4);
   1548  1.33  christos 
   1549  1.33  christos 	/* Read the list of authorized channels (20MHz ones only.) */
   1550  1.33  christos 	for (i = 0; i < 5; i++) {
   1551  1.33  christos 		addr = base + iwn5000_regulatory_bands[i];
   1552  1.33  christos 		iwn_read_eeprom_channels(sc, i, addr);
   1553  1.33  christos 	}
   1554  1.33  christos 
   1555  1.33  christos 	iwn_read_prom_data(sc, IWN5000_EEPROM_CAL, &val, 2);
   1556  1.33  christos 	base = le16toh(val);
   1557  1.33  christos 	if (sc->hw_type == IWN_HW_REV_TYPE_5150) {
   1558  1.33  christos 		/* Compute critical temperature (in Kelvin.) */
   1559  1.33  christos 		iwn_read_prom_data(sc, base + IWN5000_EEPROM_TEMP, &val, 2);
   1560  1.33  christos 		temp = le16toh(val);
   1561  1.33  christos 		iwn_read_prom_data(sc, base + IWN5000_EEPROM_VOLT, &val, 2);
   1562  1.33  christos 		volt = le16toh(val);
   1563  1.33  christos 		delta = temp - (volt / -5);
   1564  1.33  christos 		sc->critical_temp = (IWN_CTOK(110) - delta) * -5;
   1565  1.33  christos 		DPRINTF(("temp=%d volt=%d delta=%dK\n",
   1566  1.33  christos 		    temp, volt, delta));
   1567  1.33  christos 	} else {
   1568  1.33  christos 		/* Read crystal calibration. */
   1569  1.33  christos 		iwn_read_prom_data(sc, base + IWN5000_EEPROM_CRYSTAL,
   1570  1.33  christos 		    &sc->eeprom_crystal, sizeof (uint32_t));
   1571  1.33  christos 		DPRINTF(("crystal calibration 0x%08x\n",
   1572  1.33  christos 		    le32toh(sc->eeprom_crystal)));
   1573  1.33  christos 	}
   1574  1.33  christos }
   1575  1.33  christos 
   1576  1.33  christos static void
   1577  1.33  christos iwn_read_eeprom_channels(struct iwn_softc *sc, int n, uint32_t addr)
   1578  1.33  christos {
   1579  1.33  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1580  1.33  christos 	const struct iwn_chan_band *band = &iwn_bands[n];
   1581  1.33  christos 	struct iwn_eeprom_chan channels[IWN_MAX_CHAN_PER_BAND];
   1582  1.33  christos 	uint8_t chan;
   1583  1.33  christos 	int i;
   1584  1.33  christos 
   1585  1.33  christos 	iwn_read_prom_data(sc, addr, channels,
   1586  1.33  christos 	    band->nchan * sizeof (struct iwn_eeprom_chan));
   1587  1.33  christos 
   1588  1.33  christos 	for (i = 0; i < band->nchan; i++) {
   1589  1.33  christos 		if (!(channels[i].flags & IWN_EEPROM_CHAN_VALID))
   1590  1.33  christos 			continue;
   1591  1.33  christos 
   1592  1.33  christos 		chan = band->chan[i];
   1593  1.33  christos 
   1594  1.33  christos 		if (n == 0) {	/* 2GHz band */
   1595  1.33  christos 			ic->ic_channels[chan].ic_freq =
   1596  1.33  christos 			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
   1597  1.33  christos 			ic->ic_channels[chan].ic_flags =
   1598  1.33  christos 			    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
   1599  1.33  christos 			    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
   1600  1.33  christos 
   1601  1.33  christos 		} else {	/* 5GHz band */
   1602  1.33  christos 			/*
   1603  1.33  christos 			 * Some adapters support channels 7, 8, 11 and 12
   1604  1.33  christos 			 * both in the 2GHz and 4.9GHz bands.
   1605  1.33  christos 			 * Because of limitations in our net80211 layer,
   1606  1.33  christos 			 * we don't support them in the 4.9GHz band.
   1607  1.33  christos 			 */
   1608  1.33  christos 			if (chan <= 14)
   1609  1.33  christos 				continue;
   1610  1.33  christos 
   1611  1.33  christos 			ic->ic_channels[chan].ic_freq =
   1612  1.33  christos 			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ);
   1613  1.33  christos 			ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A;
   1614  1.33  christos 			/* We have at least one valid 5GHz channel. */
   1615  1.33  christos 			sc->sc_flags |= IWN_FLAG_HAS_5GHZ;
   1616  1.33  christos 		}
   1617  1.33  christos 
   1618  1.33  christos 		/* Is active scan allowed on this channel? */
   1619  1.33  christos 		if (!(channels[i].flags & IWN_EEPROM_CHAN_ACTIVE)) {
   1620  1.33  christos 			ic->ic_channels[chan].ic_flags |=
   1621  1.33  christos 			    IEEE80211_CHAN_PASSIVE;
   1622  1.33  christos 		}
   1623  1.33  christos 
   1624  1.33  christos 		/* Save maximum allowed TX power for this channel. */
   1625  1.33  christos 		sc->maxpwr[chan] = channels[i].maxpwr;
   1626  1.33  christos 
   1627  1.33  christos 		DPRINTF(("adding chan %d flags=0x%x maxpwr=%d\n",
   1628  1.33  christos 		    chan, channels[i].flags, sc->maxpwr[chan]));
   1629  1.33  christos 	}
   1630  1.33  christos }
   1631  1.33  christos 
   1632  1.33  christos /*ARGUSED*/
   1633  1.33  christos static struct ieee80211_node *
   1634  1.33  christos iwn_node_alloc(struct ieee80211_node_table *nt __unused)
   1635  1.33  christos {
   1636  1.33  christos 	struct iwn_node *wn;
   1637   1.1      ober 
   1638  1.19     freza 	wn = malloc(sizeof (struct iwn_node), M_80211_NODE, M_NOWAIT | M_ZERO);
   1639   1.1      ober 	return (struct ieee80211_node *)wn;
   1640   1.1      ober }
   1641   1.1      ober 
   1642   1.1      ober static void
   1643   1.1      ober iwn_newassoc(struct ieee80211_node *ni, int isnew)
   1644   1.1      ober {
   1645   1.1      ober 	struct iwn_softc *sc = ni->ni_ic->ic_ifp->if_softc;
   1646  1.33  christos 	struct iwn_node *wn = (void *)ni;
   1647  1.33  christos 	uint8_t rate;
   1648  1.33  christos 	int ridx, i;
   1649  1.33  christos 
   1650  1.33  christos 	ieee80211_amrr_node_init(&sc->amrr, &wn->amn);
   1651  1.33  christos 
   1652  1.33  christos 	for (i = 0; i < ni->ni_rates.rs_nrates; i++) {
   1653  1.33  christos 		rate = ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL;
   1654  1.33  christos 		/* Map 802.11 rate to HW rate index. */
   1655  1.33  christos 		for (ridx = 0; ridx <= IWN_RIDX_MAX; ridx++)
   1656  1.33  christos 			if (iwn_rates[ridx].rate == rate)
   1657  1.33  christos 				break;
   1658  1.33  christos 		wn->ridx[i] = ridx;
   1659  1.33  christos 		/* Initial TX rate <= 24Mbps. */
   1660  1.33  christos 		if (rate <= 48)
   1661  1.33  christos 			ni->ni_txrate = i;
   1662  1.33  christos 	}
   1663   1.1      ober }
   1664   1.1      ober 
   1665   1.1      ober static int
   1666   1.1      ober iwn_media_change(struct ifnet *ifp)
   1667   1.1      ober {
   1668  1.33  christos 	struct iwn_softc *sc = ifp->if_softc;
   1669  1.33  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1670  1.33  christos 	uint8_t rate, ridx;
   1671   1.1      ober 	int error;
   1672   1.1      ober 
   1673   1.1      ober 	error = ieee80211_media_change(ifp);
   1674   1.1      ober 	if (error != ENETRESET)
   1675   1.1      ober 		return error;
   1676   1.1      ober 
   1677  1.33  christos 	if (ic->ic_fixed_rate != -1) {
   1678  1.33  christos 		rate = ic->ic_sup_rates[ic->ic_curmode].
   1679  1.33  christos 		    rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
   1680  1.33  christos 		/* Map 802.11 rate to HW rate index. */
   1681  1.33  christos 		for (ridx = 0; ridx <= IWN_RIDX_MAX; ridx++)
   1682  1.33  christos 			if (iwn_rates[ridx].rate == rate)
   1683  1.33  christos 				break;
   1684  1.33  christos 		sc->fixed_ridx = ridx;
   1685  1.33  christos 	}
   1686   1.1      ober 
   1687  1.33  christos 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
   1688  1.33  christos 	    (IFF_UP | IFF_RUNNING)) {
   1689  1.33  christos 		iwn_stop(ifp, 0);
   1690  1.33  christos 		error = iwn_init(ifp);
   1691  1.33  christos 	}
   1692  1.33  christos 	return error;
   1693   1.1      ober }
   1694   1.1      ober 
   1695   1.1      ober static int
   1696   1.1      ober iwn_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
   1697   1.1      ober {
   1698   1.1      ober 	struct ifnet *ifp = ic->ic_ifp;
   1699   1.1      ober 	struct iwn_softc *sc = ifp->if_softc;
   1700   1.1      ober 	int error;
   1701   1.1      ober 
   1702   1.1      ober 	callout_stop(&sc->calib_to);
   1703   1.1      ober 
   1704   1.1      ober 	switch (nstate) {
   1705   1.1      ober 	case IEEE80211_S_SCAN:
   1706   1.1      ober 		if (sc->is_scanning)
   1707   1.1      ober 			break;
   1708  1.33  christos 
   1709   1.1      ober 		sc->is_scanning = true;
   1710   1.1      ober 		ieee80211_node_table_reset(&ic->ic_scan);
   1711   1.1      ober 		ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
   1712   1.1      ober 
   1713  1.33  christos 		/* Make the link LED blink while we're scanning. */
   1714  1.33  christos 		iwn_set_led(sc, IWN_LED_LINK, 10, 10);
   1715   1.1      ober 
   1716  1.33  christos 		if ((error = iwn_scan(sc, IEEE80211_CHAN_2GHZ)) != 0) {
   1717  1.33  christos 			aprint_error_dev(sc->sc_dev,
   1718  1.33  christos 			    "could not initiate scan\n");
   1719   1.1      ober 			return error;
   1720   1.1      ober 		}
   1721   1.1      ober 		ic->ic_state = nstate;
   1722   1.1      ober 		return 0;
   1723   1.1      ober 
   1724   1.1      ober 	case IEEE80211_S_ASSOC:
   1725   1.1      ober 		if (ic->ic_state != IEEE80211_S_RUN)
   1726   1.1      ober 			break;
   1727   1.1      ober 		/* FALLTHROUGH */
   1728   1.1      ober 	case IEEE80211_S_AUTH:
   1729  1.33  christos 		/* Reset state to handle reassociations correctly. */
   1730  1.33  christos 		sc->rxon.associd = 0;
   1731  1.33  christos 		sc->rxon.filter &= ~htole32(IWN_FILTER_BSS);
   1732  1.33  christos 		sc->calib.state = IWN_CALIB_STATE_INIT;
   1733   1.1      ober 
   1734   1.1      ober 		if ((error = iwn_auth(sc)) != 0) {
   1735  1.20     blymn 			aprint_error_dev(sc->sc_dev,
   1736  1.33  christos 			    "could not move to auth state\n");
   1737   1.1      ober 			return error;
   1738   1.1      ober 		}
   1739   1.1      ober 		break;
   1740   1.1      ober 
   1741   1.1      ober 	case IEEE80211_S_RUN:
   1742   1.1      ober 		if ((error = iwn_run(sc)) != 0) {
   1743  1.20     blymn 			aprint_error_dev(sc->sc_dev,
   1744  1.33  christos 			    "could not move to run state\n");
   1745   1.1      ober 			return error;
   1746   1.1      ober 		}
   1747   1.1      ober 		break;
   1748   1.1      ober 
   1749   1.1      ober 	case IEEE80211_S_INIT:
   1750   1.1      ober 		sc->is_scanning = false;
   1751  1.33  christos 		sc->calib.state = IWN_CALIB_STATE_INIT;
   1752   1.1      ober 		break;
   1753   1.1      ober 	}
   1754   1.1      ober 
   1755   1.1      ober 	return sc->sc_newstate(ic, nstate, arg);
   1756   1.1      ober }
   1757   1.1      ober 
   1758   1.1      ober static void
   1759  1.33  christos iwn_iter_func(void *arg, struct ieee80211_node *ni)
   1760   1.1      ober {
   1761  1.33  christos 	struct iwn_softc *sc = arg;
   1762  1.33  christos 	struct iwn_node *wn = (struct iwn_node *)ni;
   1763   1.1      ober 
   1764  1.33  christos 	ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn);
   1765   1.1      ober }
   1766   1.1      ober 
   1767   1.1      ober static void
   1768  1.33  christos iwn_calib_timeout(void *arg)
   1769   1.1      ober {
   1770  1.33  christos 	struct iwn_softc *sc = arg;
   1771  1.33  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1772  1.33  christos 	int s;
   1773   1.1      ober 
   1774  1.33  christos 	if (ic->ic_fixed_rate == -1) {
   1775  1.33  christos 		s = splnet();
   1776  1.33  christos 		if (ic->ic_opmode == IEEE80211_M_STA)
   1777  1.33  christos 			iwn_iter_func(sc, ic->ic_bss);
   1778  1.33  christos 		else
   1779  1.33  christos 			ieee80211_iterate_nodes(&ic->ic_sta, iwn_iter_func, sc);
   1780  1.33  christos 		splx(s);
   1781  1.33  christos 	}
   1782  1.33  christos 	/* Force automatic TX power calibration every 60 secs. */
   1783  1.33  christos 	if (++sc->calib_cnt >= 120) {
   1784  1.33  christos 		uint32_t flags = 0;
   1785   1.1      ober 
   1786  1.33  christos 		DPRINTF(("sending request for statistics\n"));
   1787  1.33  christos 		(void)iwn_cmd(sc, IWN_CMD_GET_STATISTICS, &flags,
   1788  1.33  christos 		    sizeof flags, 1);
   1789  1.33  christos 		sc->calib_cnt = 0;
   1790  1.33  christos 	}
   1791  1.33  christos 	/* Automatic rate control triggered every 500ms. */
   1792  1.33  christos 	callout_schedule(&sc->calib_to, hz/2);
   1793   1.1      ober }
   1794   1.1      ober 
   1795  1.33  christos #if 0
   1796   1.1      ober static int
   1797  1.33  christos iwn_ccmp_decap(struct iwn_softc *sc, struct mbuf *m, struct ieee80211_key *k)
   1798   1.1      ober {
   1799  1.33  christos 	struct ieee80211_frame *wh;
   1800  1.33  christos 	uint64_t pn, *prsc;
   1801  1.33  christos 	uint8_t *ivp;
   1802  1.33  christos 	uint8_t tid;
   1803  1.33  christos 	int hdrlen;
   1804   1.1      ober 
   1805  1.33  christos 	wh = mtod(m, struct ieee80211_frame *);
   1806  1.33  christos 	hdrlen = ieee80211_get_hdrlen(wh);
   1807  1.33  christos 	ivp = (uint8_t *)wh + hdrlen;
   1808   1.1      ober 
   1809  1.33  christos 	/* Check that ExtIV bit is be set. */
   1810  1.33  christos 	if (!(ivp[3] & IEEE80211_WEP_EXTIV)) {
   1811  1.33  christos 		DPRINTF(("CCMP decap ExtIV not set\n"));
   1812  1.33  christos 		return 1;
   1813  1.33  christos 	}
   1814  1.33  christos 	tid = ieee80211_has_qos(wh) ?
   1815  1.33  christos 	    ieee80211_get_qos(wh) & IEEE80211_QOS_TID : 0;
   1816  1.33  christos 	prsc = &k->k_rsc[tid];
   1817  1.33  christos 
   1818  1.33  christos 	/* Extract the 48-bit PN from the CCMP header. */
   1819  1.33  christos 	pn = (uint64_t)ivp[0]       |
   1820  1.33  christos 	     (uint64_t)ivp[1] <<  8 |
   1821  1.33  christos 	     (uint64_t)ivp[4] << 16 |
   1822  1.33  christos 	     (uint64_t)ivp[5] << 24 |
   1823  1.33  christos 	     (uint64_t)ivp[6] << 32 |
   1824  1.33  christos 	     (uint64_t)ivp[7] << 40;
   1825  1.33  christos 	if (pn <= *prsc) {
   1826  1.33  christos 		/*
   1827  1.33  christos 		 * Not necessarily a replayed frame since we did not check
   1828  1.33  christos 		 * the sequence number of the 802.11 header yet.
   1829  1.33  christos 		 */
   1830  1.33  christos 		DPRINTF(("CCMP replayed\n"));
   1831  1.33  christos 		return 1;
   1832   1.1      ober 	}
   1833  1.33  christos 	/* Update last seen packet number. */
   1834  1.33  christos 	*prsc = pn;
   1835   1.1      ober 
   1836  1.33  christos 	/* Clear Protected bit and strip IV. */
   1837  1.33  christos 	wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
   1838  1.33  christos 	memmove(mtod(m, char *) + IEEE80211_CCMP_HDRLEN, wh, hdrlen);
   1839  1.33  christos 	m_adj(m, IEEE80211_CCMP_HDRLEN);
   1840  1.33  christos 	/* Strip MIC. */
   1841  1.33  christos 	m_adj(m, -IEEE80211_CCMP_MICLEN);
   1842  1.33  christos 	return 0;
   1843   1.1      ober }
   1844  1.33  christos #endif
   1845   1.1      ober 
   1846   1.1      ober /*
   1847  1.33  christos  * Process an RX_PHY firmware notification.  This is usually immediately
   1848  1.33  christos  * followed by an MPDU_RX_DONE notification.
   1849   1.1      ober  */
   1850  1.33  christos void
   1851  1.33  christos iwn_rx_phy(struct iwn_softc *sc, struct iwn_rx_desc *desc)
   1852   1.1      ober {
   1853  1.33  christos 	struct iwn_rx_stat *stat = (struct iwn_rx_stat *)(desc + 1);
   1854   1.1      ober 
   1855  1.33  christos 	DPRINTFN(2, ("received PHY stats\n"));
   1856  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, sc->rxq.buf_dma.map,
   1857  1.33  christos 	    (vaddr_t)stat - (vaddr_t)sc->rxq.buf_dma.vaddr, sizeof (*stat),
   1858  1.33  christos 	    BUS_DMASYNC_POSTREAD);
   1859   1.1      ober 
   1860  1.33  christos 	/* Save RX statistics, they will be used on MPDU_RX_DONE. */
   1861  1.33  christos 	memcpy(&sc->last_rx_stat, stat, sizeof (*stat));
   1862  1.33  christos 	sc->last_rx_valid = 1;
   1863   1.1      ober }
   1864   1.1      ober 
   1865   1.1      ober /*
   1866  1.33  christos  * Process an RX_DONE (4965AGN only) or MPDU_RX_DONE firmware notification.
   1867  1.33  christos  * Each MPDU_RX_DONE notification must be preceded by an RX_PHY one.
   1868   1.1      ober  */
   1869  1.33  christos void
   1870  1.33  christos iwn_rx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc,
   1871  1.33  christos     struct iwn_rx_data *data)
   1872   1.1      ober {
   1873  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   1874  1.33  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1875  1.33  christos 	struct ifnet *ifp = ic->ic_ifp;
   1876  1.33  christos 	struct iwn_rx_ring *ring = &sc->rxq;
   1877  1.33  christos 	struct iwn_rbuf *rbuf;
   1878  1.33  christos 	struct ieee80211_frame *wh;
   1879  1.33  christos 	struct ieee80211_node *ni;
   1880  1.33  christos 	struct mbuf *m, *m1;
   1881  1.33  christos 	struct iwn_rx_stat *stat;
   1882  1.33  christos 	char * head;
   1883  1.33  christos 	uint32_t flags;
   1884  1.33  christos 	int len, rssi, error;
   1885   1.1      ober 
   1886  1.33  christos 	if (desc->type == IWN_MPDU_RX_DONE) {
   1887  1.33  christos 		/* Check for prior RX_PHY notification. */
   1888  1.33  christos 		if (!sc->last_rx_valid) {
   1889  1.33  christos 			DPRINTF(("missing RX_PHY\n"));
   1890  1.33  christos 			ifp->if_ierrors++;
   1891  1.33  christos 			return;
   1892  1.33  christos 		}
   1893  1.33  christos 		sc->last_rx_valid = 0;
   1894  1.33  christos 		stat = &sc->last_rx_stat;
   1895  1.33  christos 	} else
   1896  1.33  christos 		stat = (struct iwn_rx_stat *)(desc + 1);
   1897   1.1      ober 
   1898  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, IWN_RBUF_SIZE,
   1899  1.33  christos 	    BUS_DMASYNC_POSTREAD);
   1900   1.1      ober 
   1901  1.33  christos 	if (stat->cfg_phy_len > IWN_STAT_MAXLEN) {
   1902  1.33  christos 		aprint_error_dev(sc->sc_dev, "invalid RX statistic header\n");
   1903  1.33  christos 		ifp->if_ierrors++;
   1904  1.33  christos 		return;
   1905  1.33  christos 	}
   1906  1.33  christos 	if (desc->type == IWN_MPDU_RX_DONE) {
   1907  1.33  christos 		struct iwn_rx_mpdu *mpdu =
   1908  1.33  christos 		    (struct iwn_rx_mpdu *)(desc + 1);
   1909  1.33  christos 		head = (char *)(mpdu + 1);
   1910  1.33  christos 		len = le16toh(mpdu->len);
   1911  1.33  christos 	} else {
   1912  1.33  christos 		head = (char *)(stat + 1) + stat->cfg_phy_len;
   1913  1.33  christos 		len = le16toh(stat->len);
   1914  1.33  christos 	}
   1915   1.1      ober 
   1916  1.33  christos 	flags = le32toh(*(uint32_t *)(head + len));
   1917   1.1      ober 
   1918  1.33  christos 	/* Discard frames with a bad FCS early. */
   1919  1.33  christos 	if ((flags & IWN_RX_NOERROR) != IWN_RX_NOERROR) {
   1920  1.33  christos 		DPRINTFN(2, ("RX flags error %x\n", flags));
   1921  1.33  christos 		ifp->if_ierrors++;
   1922  1.33  christos 		return;
   1923   1.1      ober 	}
   1924  1.33  christos 	/* Discard frames that are too short. */
   1925  1.33  christos 	if (len < sizeof (struct ieee80211_frame)) {
   1926  1.33  christos 		DPRINTF(("frame too short: %d\n", len));
   1927  1.33  christos 		ic->ic_stats.is_rx_tooshort++;
   1928  1.33  christos 		ifp->if_ierrors++;
   1929  1.33  christos 		return;
   1930   1.1      ober 	}
   1931   1.1      ober 
   1932  1.33  christos 	/*
   1933  1.33  christos 	 * See comment in if_wpi.c:wpi_rx_intr() about locking
   1934  1.33  christos 	 * nb_free_entries here.  In short:  it's not required.
   1935  1.33  christos 	 */
   1936  1.33  christos 	MGETHDR(m1, M_DONTWAIT, MT_DATA);
   1937  1.33  christos 	if (m1 == NULL) {
   1938  1.33  christos 		ic->ic_stats.is_rx_nobuf++;
   1939  1.33  christos 		ifp->if_ierrors++;
   1940  1.33  christos 		return;
   1941   1.1      ober 	}
   1942  1.33  christos 	if (sc->rxq.nb_free_entries <= 0) {
   1943  1.33  christos 		ic->ic_stats.is_rx_nobuf++;
   1944   1.1      ober 		ifp->if_ierrors++;
   1945  1.33  christos 		m_freem(m1);
   1946   1.1      ober 		return;
   1947   1.1      ober 	}
   1948  1.33  christos 	rbuf = iwn_alloc_rbuf(sc);
   1949  1.33  christos 	/* Attach RX buffer to mbuf header. */
   1950  1.33  christos 	MEXTADD(m1, rbuf->vaddr, IWN_RBUF_SIZE, 0, iwn_free_rbuf,
   1951  1.33  christos 	    rbuf);
   1952  1.33  christos 	m1->m_flags |= M_EXT_RW;
   1953  1.33  christos 	bus_dmamap_unload(sc->sc_dmat, data->map);
   1954   1.1      ober 
   1955  1.33  christos 	error = bus_dmamap_load(sc->sc_dmat, data->map, m1->m_ext.ext_buf,
   1956  1.33  christos 	    IWN_RBUF_SIZE, NULL, BUS_DMA_NOWAIT);
   1957  1.33  christos 	if (error != 0) {
   1958  1.33  christos 		m_freem(m1);
   1959   1.1      ober 
   1960  1.33  christos 		/* Try to reload the old mbuf. */
   1961  1.33  christos 		error = bus_dmamap_load(sc->sc_dmat, data->map,
   1962  1.33  christos 		    data->m->m_ext.ext_buf, IWN_RBUF_SIZE, NULL,
   1963  1.33  christos 		    BUS_DMA_NOWAIT);
   1964  1.33  christos 		if (error != 0) {
   1965  1.33  christos 			panic("%s: could not load old RX mbuf",
   1966  1.33  christos 			    device_xname(sc->sc_dev));
   1967  1.33  christos 		}
   1968  1.33  christos 		/* Physical address may have changed. */
   1969  1.33  christos 		ring->desc[ring->cur] =
   1970  1.33  christos 		    htole32(data->map->dm_segs[0].ds_addr >> 8);
   1971  1.33  christos 		bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
   1972  1.33  christos 		    ring->cur * sizeof (uint32_t), sizeof (uint32_t),
   1973  1.33  christos 		    BUS_DMASYNC_PREWRITE);
   1974   1.1      ober 		ifp->if_ierrors++;
   1975   1.1      ober 		return;
   1976   1.1      ober 	}
   1977  1.33  christos 	m = data->m;
   1978  1.33  christos 	data->m = m1;
   1979   1.1      ober 
   1980  1.33  christos 	/* Update RX descriptor. */
   1981  1.33  christos 	ring->desc[ring->cur] = htole32(data->map->dm_segs[0].ds_addr >> 8);
   1982  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
   1983  1.33  christos 	    ring->cur * sizeof (uint32_t), sizeof (uint32_t),
   1984  1.33  christos 	    BUS_DMASYNC_PREWRITE);
   1985   1.1      ober 
   1986  1.33  christos 	/* Finalize mbuf. */
   1987   1.1      ober 	m->m_pkthdr.rcvif = ifp;
   1988   1.1      ober 	m->m_data = head;
   1989   1.1      ober 	m->m_pkthdr.len = m->m_len = len;
   1990   1.1      ober 
   1991  1.33  christos 	/* Grab a reference to the source node. */
   1992  1.33  christos 	wh = mtod(m, struct ieee80211_frame *);
   1993  1.33  christos 	ni = ieee80211_find_rxnode(ic,(struct ieee80211_frame_min *)wh);
   1994  1.33  christos 
   1995  1.33  christos #if 0
   1996  1.33  christos 	rxi.rxi_flags = 0;
   1997  1.33  christos 	if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
   1998  1.33  christos 	    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
   1999  1.33  christos 	    (ni->ni_flags & IEEE80211_NODE_RXPROT) &&
   2000  1.33  christos 	    ni->ni_pairwise_key.k_cipher == IEEE80211_CIPHER_CCMP) {
   2001  1.33  christos 		if ((flags & IWN_RX_CIPHER_MASK) != IWN_RX_CIPHER_CCMP) {
   2002  1.33  christos 			ic->ic_stats.is_ccmp_dec_errs++;
   2003  1.33  christos 			ifp->if_ierrors++;
   2004  1.33  christos 			return;
   2005  1.33  christos 		}
   2006  1.33  christos 		/* Check whether decryption was successful or not. */
   2007  1.33  christos 		if ((desc->type == IWN_MPDU_RX_DONE &&
   2008  1.33  christos 		     (flags & (IWN_RX_MPDU_DEC | IWN_RX_MPDU_MIC_OK)) !=
   2009  1.33  christos 		      (IWN_RX_MPDU_DEC | IWN_RX_MPDU_MIC_OK)) ||
   2010  1.33  christos 		    (desc->type != IWN_MPDU_RX_DONE &&
   2011  1.33  christos 		     (flags & IWN_RX_DECRYPT_MASK) != IWN_RX_DECRYPT_OK)) {
   2012  1.33  christos 			DPRINTF(("CCMP decryption failed 0x%x\n", flags));
   2013  1.33  christos 			ic->ic_stats.is_ccmp_dec_errs++;
   2014   1.1      ober 			ifp->if_ierrors++;
   2015   1.1      ober 			return;
   2016   1.1      ober 		}
   2017  1.33  christos 		if (iwn_ccmp_decap(sc, m, &ni->ni_pairwise_key) != 0) {
   2018   1.1      ober 			ifp->if_ierrors++;
   2019   1.1      ober 			return;
   2020   1.1      ober 		}
   2021  1.33  christos 		rxi.rxi_flags |= IEEE80211_RXI_HWDEC;
   2022   1.1      ober 	}
   2023  1.33  christos #endif
   2024   1.1      ober 
   2025  1.33  christos 	rssi = hal->get_rssi(stat);
   2026  1.22       rtr 	if (ic->ic_state == IEEE80211_S_SCAN)
   2027   1.1      ober 		iwn_fix_channel(ic, m);
   2028   1.1      ober 
   2029   1.1      ober 	if (sc->sc_drvbpf != NULL) {
   2030   1.2      ober 		struct iwn_rx_radiotap_header *tap = &sc->sc_rxtap;
   2031   1.1      ober 
   2032   1.1      ober 		tap->wr_flags = 0;
   2033  1.33  christos 		if (stat->flags & htole16(IWN_STAT_FLAG_SHPREAMBLE))
   2034  1.33  christos 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
   2035   1.1      ober 		tap->wr_chan_freq =
   2036   1.1      ober 		    htole16(ic->ic_channels[stat->chan].ic_freq);
   2037   1.1      ober 		tap->wr_chan_flags =
   2038   1.1      ober 		    htole16(ic->ic_channels[stat->chan].ic_flags);
   2039   1.1      ober 		tap->wr_dbm_antsignal = (int8_t)rssi;
   2040   1.1      ober 		tap->wr_dbm_antnoise = (int8_t)sc->noise;
   2041   1.1      ober 		tap->wr_tsft = stat->tstamp;
   2042   1.1      ober 		switch (stat->rate) {
   2043  1.33  christos 		/* CCK rates. */
   2044   1.1      ober 		case  10: tap->wr_rate =   2; break;
   2045   1.1      ober 		case  20: tap->wr_rate =   4; break;
   2046   1.1      ober 		case  55: tap->wr_rate =  11; break;
   2047   1.1      ober 		case 110: tap->wr_rate =  22; break;
   2048  1.33  christos 		/* OFDM rates. */
   2049   1.1      ober 		case 0xd: tap->wr_rate =  12; break;
   2050   1.1      ober 		case 0xf: tap->wr_rate =  18; break;
   2051   1.1      ober 		case 0x5: tap->wr_rate =  24; break;
   2052   1.1      ober 		case 0x7: tap->wr_rate =  36; break;
   2053   1.1      ober 		case 0x9: tap->wr_rate =  48; break;
   2054   1.1      ober 		case 0xb: tap->wr_rate =  72; break;
   2055   1.1      ober 		case 0x1: tap->wr_rate =  96; break;
   2056   1.1      ober 		case 0x3: tap->wr_rate = 108; break;
   2057  1.33  christos 		/* Unknown rate: should not happen. */
   2058   1.1      ober 		default:  tap->wr_rate =   0;
   2059   1.1      ober 		}
   2060   1.1      ober 
   2061  1.38     joerg 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
   2062   1.1      ober 	}
   2063   1.1      ober 
   2064  1.33  christos 	/* Send the frame to the 802.11 layer. */
   2065   1.1      ober 	ieee80211_input(ic, m, ni, rssi, 0);
   2066   1.1      ober 
   2067  1.33  christos 	/* Node is no longer needed. */
   2068   1.1      ober 	ieee80211_free_node(ni);
   2069   1.1      ober }
   2070   1.1      ober 
   2071  1.33  christos /*
   2072  1.33  christos  * Process a CALIBRATION_RESULT notification sent by the initialization
   2073  1.33  christos  * firmware on response to a CMD_CALIB_CONFIG command (5000 only.)
   2074  1.33  christos  */
   2075  1.33  christos void
   2076  1.33  christos iwn5000_rx_calib_results(struct iwn_softc *sc, struct iwn_rx_desc *desc,
   2077  1.33  christos     struct iwn_rx_data *data)
   2078  1.33  christos {
   2079  1.33  christos 	struct iwn_phy_calib *calib = (struct iwn_phy_calib *)(desc + 1);
   2080  1.33  christos 	int len, idx = -1;
   2081  1.33  christos 
   2082  1.33  christos 	/* Runtime firmware should not send such a notification. */
   2083  1.33  christos 	if (!(sc->sc_flags & IWN_FLAG_FIRST_BOOT))
   2084  1.33  christos 		return;
   2085  1.33  christos 
   2086  1.33  christos 	len = (le32toh(desc->len) & 0x3fff) - 4;
   2087  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc), len,
   2088  1.33  christos 	    BUS_DMASYNC_POSTREAD);
   2089  1.33  christos 
   2090  1.33  christos 	switch (calib->code) {
   2091  1.33  christos 	case IWN5000_PHY_CALIB_DC:
   2092  1.33  christos 		if (sc->hw_type == IWN_HW_REV_TYPE_5150)
   2093  1.33  christos 			idx = 0;
   2094  1.33  christos 		break;
   2095  1.33  christos 	case IWN5000_PHY_CALIB_LO:
   2096  1.33  christos 		idx = 1;
   2097  1.33  christos 		break;
   2098  1.33  christos 	case IWN5000_PHY_CALIB_TX_IQ:
   2099  1.33  christos 		idx = 2;
   2100  1.33  christos 		break;
   2101  1.33  christos 	case IWN5000_PHY_CALIB_TX_IQ_PERD:
   2102  1.33  christos 		if (sc->hw_type != IWN_HW_REV_TYPE_5150)
   2103  1.33  christos 			idx = 3;
   2104  1.33  christos 		break;
   2105  1.33  christos 	case IWN5000_PHY_CALIB_BASE_BAND:
   2106  1.33  christos 		idx = 4;
   2107  1.33  christos 		break;
   2108  1.33  christos 	}
   2109  1.33  christos 	if (idx == -1)	/* Ignore other results. */
   2110  1.33  christos 		return;
   2111  1.33  christos 
   2112  1.33  christos 	/* Save calibration result. */
   2113  1.33  christos 	if (sc->calibcmd[idx].buf != NULL)
   2114  1.33  christos 		free(sc->calibcmd[idx].buf, M_DEVBUF);
   2115  1.33  christos 	sc->calibcmd[idx].buf = malloc(len, M_DEVBUF, M_NOWAIT);
   2116  1.33  christos 	if (sc->calibcmd[idx].buf == NULL) {
   2117  1.33  christos 		DPRINTF(("not enough memory for calibration result %d\n",
   2118  1.33  christos 		    calib->code));
   2119  1.33  christos 		return;
   2120  1.33  christos 	}
   2121  1.33  christos 	DPRINTF(("saving calibration result code=%d len=%d\n",
   2122  1.33  christos 	    calib->code, len));
   2123  1.33  christos 	sc->calibcmd[idx].len = len;
   2124  1.33  christos 	memcpy(sc->calibcmd[idx].buf, calib, len);
   2125  1.33  christos }
   2126  1.33  christos 
   2127   1.1      ober 
   2128   1.1      ober /*
   2129   1.1      ober  * XXX: Hack to set the current channel to the value advertised in beacons or
   2130   1.1      ober  * probe responses. Only used during AP detection.
   2131   1.1      ober  * XXX: Duplicated from if_iwi.c
   2132   1.1      ober  */
   2133   1.1      ober static void
   2134   1.1      ober iwn_fix_channel(struct ieee80211com *ic, struct mbuf *m)
   2135   1.1      ober {
   2136   1.1      ober 	struct ieee80211_frame *wh;
   2137   1.1      ober 	uint8_t subtype;
   2138   1.1      ober 	uint8_t *frm, *efrm;
   2139   1.1      ober 
   2140   1.1      ober 	wh = mtod(m, struct ieee80211_frame *);
   2141   1.1      ober 
   2142   1.1      ober 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
   2143   1.1      ober 		return;
   2144   1.1      ober 
   2145   1.1      ober 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
   2146   1.1      ober 
   2147   1.1      ober 	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
   2148   1.1      ober 	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
   2149   1.1      ober 		return;
   2150   1.1      ober 
   2151   1.1      ober 	frm = (uint8_t *)(wh + 1);
   2152   1.1      ober 	efrm = mtod(m, uint8_t *) + m->m_len;
   2153   1.1      ober 
   2154  1.33  christos 	frm += 12;      /* skip tstamp, bintval and capinfo fields */
   2155   1.1      ober 	while (frm < efrm) {
   2156   1.1      ober 		if (*frm == IEEE80211_ELEMID_DSPARMS)
   2157   1.1      ober #if IEEE80211_CHAN_MAX < 255
   2158  1.33  christos 		if (frm[2] <= IEEE80211_CHAN_MAX)
   2159   1.1      ober #endif
   2160  1.33  christos 			ic->ic_curchan = &ic->ic_channels[frm[2]];
   2161   1.1      ober 
   2162   1.1      ober 		frm += frm[1] + 2;
   2163   1.1      ober 	}
   2164   1.1      ober }
   2165   1.1      ober 
   2166  1.33  christos 
   2167  1.33  christos /*
   2168  1.33  christos  * Process an RX_STATISTICS or BEACON_STATISTICS firmware notification.
   2169  1.33  christos  * The latter is sent by the firmware after each received beacon.
   2170  1.33  christos  */
   2171   1.1      ober static void
   2172  1.33  christos iwn_rx_statistics(struct iwn_softc *sc, struct iwn_rx_desc *desc,
   2173  1.33  christos     struct iwn_rx_data *data)
   2174   1.1      ober {
   2175   1.1      ober 	struct ieee80211com *ic = &sc->sc_ic;
   2176   1.1      ober 	struct iwn_calib_state *calib = &sc->calib;
   2177   1.1      ober 	struct iwn_stats *stats = (struct iwn_stats *)(desc + 1);
   2178   1.1      ober 
   2179  1.33  christos 	/* Ignore statistics received during a scan. */
   2180   1.1      ober 	if (ic->ic_state != IEEE80211_S_RUN)
   2181   1.1      ober 		return;
   2182   1.1      ober 
   2183  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
   2184  1.33  christos 	    sizeof (*stats), BUS_DMASYNC_POSTREAD);
   2185  1.33  christos 
   2186   1.1      ober 	DPRINTFN(3, ("received statistics (cmd=%d)\n", desc->type));
   2187  1.33  christos 	sc->calib_cnt = 0;	/* Reset TX power calibration timeout. */
   2188   1.1      ober 
   2189  1.33  christos #if 0
   2190  1.33  christos 	/* Test if temperature has changed. */
   2191   1.1      ober 	if (stats->general.temp != sc->rawtemp) {
   2192  1.33  christos 		/* Convert "raw" temperature to degC. */
   2193   1.1      ober 		sc->rawtemp = stats->general.temp;
   2194  1.33  christos 		temp = hal->get_temperature(sc);
   2195  1.33  christos 		DPRINTFN(2, ("temperature=%dC\n", temp));
   2196   1.1      ober 
   2197  1.33  christos 		/* Update temperature sensor. */
   2198  1.33  christos 		sc->sensor.value = IWN_CTOMUK(temp);
   2199  1.33  christos 		sc->sensor.flags &= ~SENSOR_FINVALID;
   2200  1.33  christos 
   2201  1.33  christos 		/* Update TX power if need be (4965AGN only.) */
   2202  1.33  christos 		if (sc->hw_type == IWN_HW_REV_TYPE_4965)
   2203  1.33  christos 			iwn4965_power_calibration(sc, temp);
   2204   1.1      ober 	}
   2205  1.33  christos #endif
   2206   1.1      ober 
   2207   1.1      ober 	if (desc->type != IWN_BEACON_STATISTICS)
   2208  1.33  christos 		return;	/* Reply to a statistics request. */
   2209   1.1      ober 
   2210   1.1      ober 	sc->noise = iwn_get_noise(&stats->rx.general);
   2211   1.1      ober 
   2212  1.33  christos 	/* Test that RSSI and noise are present in stats report. */
   2213   1.1      ober 	if (le32toh(stats->rx.general.flags) != 1) {
   2214   1.1      ober 		DPRINTF(("received statistics without RSSI\n"));
   2215   1.1      ober 		return;
   2216   1.1      ober 	}
   2217   1.1      ober 
   2218   1.1      ober 	if (calib->state == IWN_CALIB_STATE_ASSOC)
   2219  1.33  christos 		iwn_collect_noise(sc, &stats->rx.general);
   2220   1.1      ober 	else if (calib->state == IWN_CALIB_STATE_RUN)
   2221   1.1      ober 		iwn_tune_sensitivity(sc, &stats->rx);
   2222   1.1      ober }
   2223   1.1      ober 
   2224  1.33  christos /*
   2225  1.33  christos  * Process a TX_DONE firmware notification.  Unfortunately, the 4965AGN
   2226  1.33  christos  * and 5000 adapters have different incompatible TX status formats.
   2227  1.33  christos  */
   2228  1.33  christos static void
   2229  1.33  christos iwn4965_tx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc,
   2230  1.33  christos     struct iwn_rx_data *data)
   2231  1.33  christos {
   2232  1.33  christos 	struct iwn4965_tx_stat *stat = (struct iwn4965_tx_stat *)(desc + 1);
   2233  1.33  christos 
   2234  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
   2235  1.33  christos 	    sizeof (*stat), BUS_DMASYNC_POSTREAD);
   2236  1.33  christos 	iwn_tx_done(sc, desc, stat->retrycnt, le32toh(stat->status) & 0xff);
   2237  1.33  christos }
   2238  1.33  christos 
   2239  1.33  christos static void
   2240  1.33  christos iwn5000_tx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc,
   2241  1.33  christos     struct iwn_rx_data *data)
   2242  1.33  christos {
   2243  1.33  christos 	struct iwn5000_tx_stat *stat = (struct iwn5000_tx_stat *)(desc + 1);
   2244  1.33  christos 
   2245  1.33  christos 	/* Reset TX scheduler slot. */
   2246  1.33  christos 	iwn5000_reset_sched(sc, desc->qid & 0xf, desc->idx);
   2247  1.33  christos 
   2248  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
   2249  1.33  christos 	    sizeof (*stat), BUS_DMASYNC_POSTREAD);
   2250  1.33  christos 	iwn_tx_done(sc, desc, stat->retrycnt, le16toh(stat->status) & 0xff);
   2251  1.33  christos }
   2252  1.33  christos 
   2253  1.33  christos /*
   2254  1.33  christos  * Adapter-independent backend for TX_DONE firmware notifications.
   2255  1.33  christos  */
   2256   1.1      ober static void
   2257  1.33  christos iwn_tx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc, int retrycnt,
   2258  1.33  christos     uint8_t status)
   2259   1.1      ober {
   2260   1.1      ober 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
   2261   1.1      ober 	struct iwn_tx_ring *ring = &sc->txq[desc->qid & 0xf];
   2262  1.33  christos 	struct iwn_tx_data *data = &ring->data[desc->idx];
   2263  1.33  christos 	struct iwn_node *wn = (struct iwn_node *)data->ni;
   2264   1.1      ober 
   2265  1.33  christos 	/* Update rate control statistics. */
   2266   1.1      ober 	wn->amn.amn_txcnt++;
   2267  1.33  christos 	if (retrycnt > 0)
   2268   1.1      ober 		wn->amn.amn_retrycnt++;
   2269   1.1      ober 
   2270   1.1      ober 	if (status != 1 && status != 2)
   2271   1.1      ober 		ifp->if_oerrors++;
   2272   1.1      ober 	else
   2273   1.1      ober 		ifp->if_opackets++;
   2274   1.1      ober 
   2275  1.33  christos 	/* Unmap and free mbuf. */
   2276  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
   2277  1.33  christos 	    BUS_DMASYNC_POSTWRITE);
   2278  1.33  christos 	bus_dmamap_unload(sc->sc_dmat, data->map);
   2279  1.33  christos 	m_freem(data->m);
   2280  1.33  christos 	data->m = NULL;
   2281  1.33  christos 	ieee80211_free_node(data->ni);
   2282  1.33  christos 	data->ni = NULL;
   2283   1.1      ober 
   2284   1.1      ober 	sc->sc_tx_timer = 0;
   2285  1.33  christos 	if (--ring->queued < IWN_TX_RING_LOMARK) {
   2286  1.33  christos 		sc->qfullmsk &= ~(1 << ring->qid);
   2287  1.33  christos 		if (sc->qfullmsk == 0 && (ifp->if_flags & IFF_OACTIVE)) {
   2288  1.33  christos 			ifp->if_flags &= ~IFF_OACTIVE;
   2289  1.33  christos 			iwn_start(ifp);
   2290  1.33  christos 		}
   2291  1.33  christos 	}
   2292   1.1      ober }
   2293   1.1      ober 
   2294  1.33  christos /*
   2295  1.33  christos  * Process a "command done" firmware notification.  This is where we wakeup
   2296  1.33  christos  * processes waiting for a synchronous command completion.
   2297  1.33  christos  */
   2298   1.1      ober static void
   2299  1.33  christos iwn_cmd_done(struct iwn_softc *sc, struct iwn_rx_desc *desc)
   2300   1.1      ober {
   2301   1.1      ober 	struct iwn_tx_ring *ring = &sc->txq[4];
   2302   1.1      ober 	struct iwn_tx_data *data;
   2303   1.1      ober 
   2304   1.1      ober 	if ((desc->qid & 0xf) != 4)
   2305  1.33  christos 		return;	/* Not a command ack. */
   2306   1.1      ober 
   2307   1.1      ober 	data = &ring->data[desc->idx];
   2308   1.1      ober 
   2309  1.33  christos 	/* If the command was mapped in an mbuf, free it. */
   2310   1.1      ober 	if (data->m != NULL) {
   2311  1.33  christos 		bus_dmamap_sync(sc->sc_dmat, data->map, 0,
   2312  1.33  christos 		    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   2313   1.1      ober 		bus_dmamap_unload(sc->sc_dmat, data->map);
   2314   1.1      ober 		m_freem(data->m);
   2315   1.1      ober 		data->m = NULL;
   2316   1.1      ober 	}
   2317  1.33  christos 	wakeup(&ring->desc[desc->idx]);
   2318   1.1      ober }
   2319   1.1      ober 
   2320  1.33  christos /*
   2321  1.33  christos  * Process an INT_FH_RX or INT_SW_RX interrupt.
   2322  1.33  christos  */
   2323   1.1      ober static void
   2324  1.28     blymn iwn_microcode_ready(struct iwn_softc *sc, struct iwn_ucode_info *uc)
   2325  1.28     blymn {
   2326  1.28     blymn 
   2327  1.33  christos 	/* The microcontroller is ready */
   2328  1.28     blymn 	DPRINTF(("microcode alive notification version=%d.%d "
   2329  1.28     blymn 		 "subtype=%x alive=%x\n", uc->major, uc->minor,
   2330  1.28     blymn 		 uc->subtype, le32toh(uc->valid)));
   2331  1.28     blymn 
   2332  1.33  christos 
   2333  1.28     blymn 	if (le32toh(uc->valid) != 1) {
   2334  1.28     blymn 		aprint_error_dev(sc->sc_dev, "microcontroller initialization "
   2335  1.28     blymn 				 "failed\n");
   2336  1.28     blymn 		return;
   2337  1.28     blymn 	}
   2338  1.28     blymn 	if (uc->subtype == IWN_UCODE_INIT) {
   2339  1.28     blymn 		/* save microcontroller's report */
   2340  1.28     blymn 		memcpy(&sc->ucode_info, uc, sizeof (*uc));
   2341  1.28     blymn 	}
   2342  1.33  christos 	/* Save the address of the error log in SRAM. */
   2343  1.33  christos 	sc->errptr = le32toh(uc->errptr);
   2344  1.28     blymn }
   2345  1.28     blymn 
   2346  1.28     blymn static void
   2347   1.1      ober iwn_notif_intr(struct iwn_softc *sc)
   2348   1.1      ober {
   2349   1.1      ober 	struct ieee80211com *ic = &sc->sc_ic;
   2350   1.1      ober 	struct ifnet *ifp = ic->ic_ifp;
   2351  1.28     blymn 	struct iwn_rx_data *data;
   2352  1.28     blymn 	struct iwn_rx_desc *desc;
   2353   1.1      ober 	uint16_t hw;
   2354   1.1      ober 
   2355  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, sc->rxq.stat_dma.map,
   2356  1.33  christos 	    0, sc->rxq.stat_dma.size, BUS_DMASYNC_POSTREAD);
   2357  1.33  christos 
   2358  1.33  christos 	hw = le16toh(sc->rxq.stat->closed_count) & 0xfff;
   2359  1.28     blymn 
   2360  1.28     blymn 	/*
   2361  1.28     blymn 	 * If the radio is disabled then down the interface and stop
   2362  1.28     blymn 	 * processing - scan the queue for a microcode load command
   2363  1.28     blymn 	 * result.  It is the only thing that we can do with the radio
   2364  1.28     blymn 	 * off.
   2365  1.28     blymn 	 */
   2366  1.28     blymn 	if (!sc->sc_radio) {
   2367  1.28     blymn 		while (sc->rxq.cur != hw) {
   2368  1.28     blymn 			data = &sc->rxq.data[sc->rxq.cur];
   2369  1.28     blymn 			desc = (void *)data->m->m_ext.ext_buf;
   2370  1.33  christos 			bus_dmamap_sync(sc->sc_dmat, data->map, 0,
   2371  1.33  christos 			    sizeof(*desc), BUS_DMASYNC_POSTREAD);
   2372  1.28     blymn 			if (desc->type == IWN_UC_READY) {
   2373  1.28     blymn 				iwn_microcode_ready(sc,
   2374  1.28     blymn 				    (struct iwn_ucode_info *)(desc + 1));
   2375  1.28     blymn 			} else if (desc->type == IWN_STATE_CHANGED) {
   2376  1.28     blymn 				uint32_t *status = (uint32_t *)(desc + 1);
   2377  1.28     blymn 
   2378  1.28     blymn 				/* enabled/disabled notification */
   2379  1.28     blymn 				DPRINTF(("state changed to %x\n",
   2380  1.28     blymn 					 le32toh(*status)));
   2381  1.28     blymn 
   2382  1.28     blymn 				sc->sc_radio = !(le32toh(*status) & 1);
   2383  1.28     blymn 			}
   2384  1.28     blymn 
   2385  1.28     blymn 			sc->rxq.cur = (sc->rxq.cur + 1) % IWN_RX_RING_COUNT;
   2386  1.28     blymn 		}
   2387  1.28     blymn 
   2388  1.28     blymn 		if (!sc->sc_radio) {
   2389  1.28     blymn 			iwn_stop(ifp, 1);
   2390  1.28     blymn 		}
   2391  1.28     blymn 
   2392  1.28     blymn 		return;
   2393  1.28     blymn 	}
   2394  1.28     blymn 
   2395  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, sc->rxq.stat_dma.map,
   2396  1.33  christos 	    0, sc->rxq.stat_dma.size, BUS_DMASYNC_POSTREAD);
   2397  1.33  christos 
   2398  1.33  christos 	hw = le16toh(sc->rxq.stat->closed_count) & 0xfff;
   2399   1.1      ober 	while (sc->rxq.cur != hw) {
   2400  1.28     blymn 		data = &sc->rxq.data[sc->rxq.cur];
   2401  1.28     blymn 		desc = (void *)data->m->m_ext.ext_buf;
   2402   1.1      ober 
   2403  1.33  christos 		bus_dmamap_sync(sc->sc_dmat, data->map, 0, sizeof (*desc),
   2404  1.33  christos 		    BUS_DMASYNC_POSTREAD);
   2405  1.33  christos 
   2406  1.33  christos 		DPRINTFN(4, ("notification qid=%d idx=%d flags=%x type=%d\n",
   2407  1.33  christos 		    desc->qid & 0xf, desc->idx, desc->flags, desc->type));
   2408   1.1      ober 
   2409  1.33  christos 		if (!(desc->qid & 0x80))	/* Reply to a command. */
   2410  1.33  christos 			iwn_cmd_done(sc, desc);
   2411   1.1      ober 
   2412   1.1      ober 		switch (desc->type) {
   2413  1.33  christos 		case IWN_RX_PHY:
   2414  1.33  christos 			iwn_rx_phy(sc, desc);
   2415   1.1      ober 			break;
   2416   1.1      ober 
   2417  1.33  christos 		case IWN_RX_DONE:		/* 4965AGN only. */
   2418  1.33  christos 		case IWN_MPDU_RX_DONE:
   2419  1.33  christos 			/* An 802.11 frame has been received. */
   2420  1.33  christos 			iwn_rx_done(sc, desc, data);
   2421   1.1      ober 			break;
   2422   1.1      ober 
   2423   1.1      ober 		case IWN_TX_DONE:
   2424  1.33  christos 			/* An 802.11 frame has been transmitted. */
   2425  1.33  christos 			sc->sc_hal->tx_done(sc, desc, data);
   2426   1.1      ober 			break;
   2427   1.1      ober 
   2428   1.1      ober 		case IWN_RX_STATISTICS:
   2429   1.1      ober 		case IWN_BEACON_STATISTICS:
   2430  1.33  christos 			iwn_rx_statistics(sc, desc, data);
   2431   1.1      ober 			break;
   2432   1.1      ober 
   2433   1.1      ober 		case IWN_BEACON_MISSED:
   2434   1.1      ober 		{
   2435   1.1      ober 			struct iwn_beacon_missed *miss =
   2436   1.1      ober 			    (struct iwn_beacon_missed *)(desc + 1);
   2437  1.33  christos 
   2438  1.33  christos 			bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
   2439  1.33  christos 			    sizeof (*miss), BUS_DMASYNC_POSTREAD);
   2440   1.1      ober 			/*
   2441   1.1      ober 			 * If more than 5 consecutive beacons are missed,
   2442   1.1      ober 			 * reinitialize the sensitivity state machine.
   2443   1.1      ober 			 */
   2444  1.33  christos 			DPRINTF(("beacons missed %d/%d\n",
   2445  1.33  christos 			    le32toh(miss->consecutive), le32toh(miss->total)));
   2446   1.1      ober 			if (ic->ic_state == IEEE80211_S_RUN &&
   2447   1.1      ober 			    le32toh(miss->consecutive) > 5)
   2448   1.1      ober 				(void)iwn_init_sensitivity(sc);
   2449   1.1      ober 			break;
   2450   1.1      ober 		}
   2451   1.1      ober 		case IWN_UC_READY:
   2452   1.1      ober 		{
   2453  1.28     blymn 			iwn_microcode_ready(sc,
   2454  1.28     blymn 			    (struct iwn_ucode_info *)(desc + 1));
   2455   1.1      ober 			break;
   2456   1.1      ober 		}
   2457   1.1      ober 		case IWN_STATE_CHANGED:
   2458   1.1      ober 		{
   2459   1.1      ober 			uint32_t *status = (uint32_t *)(desc + 1);
   2460   1.1      ober 
   2461  1.33  christos 			/* Enabled/disabled notification. */
   2462  1.33  christos 			bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
   2463  1.33  christos 			    sizeof (*status), BUS_DMASYNC_POSTREAD);
   2464   1.1      ober 			DPRINTF(("state changed to %x\n", le32toh(*status)));
   2465   1.1      ober 
   2466  1.28     blymn 			sc->sc_radio = !(le32toh(*status) & 1);
   2467  1.33  christos 
   2468   1.1      ober 			if (le32toh(*status) & 1) {
   2469  1.33  christos 				/* The radio button has to be pushed. */
   2470  1.33  christos 				aprint_error_dev(sc->sc_dev,
   2471  1.33  christos 				    "Radio transmitter is off\n");
   2472  1.33  christos 				/* Turn the interface down. */
   2473   1.1      ober 				iwn_stop(ifp, 1);
   2474  1.33  christos 				return;	/* No further processing. */
   2475   1.1      ober 			}
   2476   1.1      ober 			break;
   2477   1.1      ober 		}
   2478   1.1      ober 		case IWN_START_SCAN:
   2479   1.1      ober 		{
   2480   1.1      ober 			struct iwn_start_scan *scan =
   2481   1.1      ober 			    (struct iwn_start_scan *)(desc + 1);
   2482   1.1      ober 
   2483  1.33  christos 			bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
   2484  1.33  christos 			    sizeof (*scan), BUS_DMASYNC_POSTREAD);
   2485   1.1      ober 			DPRINTFN(2, ("scanning channel %d status %x\n",
   2486  1.33  christos 			    scan->chan, le32toh(scan->status)));
   2487   1.1      ober 
   2488  1.33  christos 			/* Fix current channel. */
   2489   1.1      ober 			ic->ic_bss->ni_chan = &ic->ic_channels[scan->chan];
   2490   1.1      ober 			break;
   2491   1.1      ober 		}
   2492   1.1      ober 		case IWN_STOP_SCAN:
   2493   1.1      ober 		{
   2494   1.1      ober 			struct iwn_stop_scan *scan =
   2495   1.1      ober 			    (struct iwn_stop_scan *)(desc + 1);
   2496   1.1      ober 
   2497  1.33  christos 			bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
   2498  1.33  christos 			    sizeof (*scan), BUS_DMASYNC_POSTREAD);
   2499   1.1      ober 			DPRINTF(("scan finished nchan=%d status=%d chan=%d\n",
   2500  1.33  christos 			    scan->nchan, scan->status, scan->chan));
   2501   1.1      ober 
   2502  1.33  christos 			if (scan->status == 1 && scan->chan <= 14 &&
   2503  1.33  christos 			    (sc->sc_flags & IWN_FLAG_HAS_5GHZ)) {
   2504   1.1      ober 				/*
   2505  1.33  christos 				 * We just finished scanning 2GHz channels,
   2506  1.33  christos 				 * start scanning 5GHz ones.
   2507   1.1      ober 				 */
   2508  1.33  christos 				if (iwn_scan(sc, IEEE80211_CHAN_5GHZ) == 0)
   2509   1.1      ober 					break;
   2510   1.1      ober 			}
   2511   1.1      ober 			sc->is_scanning = false;
   2512   1.1      ober 			ieee80211_end_scan(ic);
   2513   1.1      ober 			break;
   2514   1.1      ober 		}
   2515  1.33  christos 		case IWN5000_CALIBRATION_RESULT:
   2516  1.33  christos 			iwn5000_rx_calib_results(sc, desc, data);
   2517  1.33  christos 			break;
   2518  1.33  christos 
   2519  1.33  christos 		case IWN5000_CALIBRATION_DONE:
   2520  1.33  christos 			wakeup(sc);
   2521  1.33  christos 			break;
   2522   1.1      ober 		}
   2523   1.1      ober 
   2524   1.1      ober 		sc->rxq.cur = (sc->rxq.cur + 1) % IWN_RX_RING_COUNT;
   2525   1.1      ober 	}
   2526   1.1      ober 
   2527  1.33  christos 	/* Tell the firmware what we have processed. */
   2528   1.1      ober 	hw = (hw == 0) ? IWN_RX_RING_COUNT - 1 : hw - 1;
   2529  1.33  christos 	IWN_WRITE(sc, IWN_FH_RX_WPTR, hw & ~7);
   2530   1.1      ober }
   2531   1.1      ober 
   2532  1.33  christos /*
   2533  1.33  christos  * Process an INT_WAKEUP interrupt raised when the microcontroller wakes up
   2534  1.33  christos  * from power-down sleep mode.
   2535  1.33  christos  */
   2536  1.33  christos static void
   2537  1.33  christos iwn_wakeup_intr(struct iwn_softc *sc)
   2538   1.1      ober {
   2539  1.33  christos 	int qid;
   2540   1.1      ober 
   2541  1.33  christos 	DPRINTF(("ucode wakeup from power-down sleep\n"));
   2542   1.1      ober 
   2543  1.33  christos 	/* Wakeup RX and TX rings. */
   2544  1.33  christos 	IWN_WRITE(sc, IWN_FH_RX_WPTR, sc->rxq.cur & ~7);
   2545  1.33  christos 	for (qid = 0; qid < 6; qid++) {
   2546  1.33  christos 		struct iwn_tx_ring *ring = &sc->txq[qid];
   2547  1.33  christos 		IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | ring->cur);
   2548   1.1      ober 	}
   2549  1.33  christos }
   2550   1.1      ober 
   2551  1.33  christos /*
   2552  1.33  christos  * Dump the error log of the firmware when a firmware panic occurs.  Although
   2553  1.33  christos  * we can't debug the firmware because it is neither open source nor free, it
   2554  1.33  christos  * can help us to identify certain classes of problems.
   2555  1.33  christos  */
   2556  1.33  christos void
   2557  1.33  christos iwn_fatal_intr(struct iwn_softc *sc)
   2558  1.33  christos {
   2559  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   2560  1.33  christos 	struct iwn_fw_dump dump;
   2561  1.33  christos 	int i;
   2562   1.1      ober 
   2563  1.33  christos 	/* Check that the error log address is valid. */
   2564  1.33  christos 	if (sc->errptr < IWN_FW_DATA_BASE ||
   2565  1.33  christos 	    sc->errptr + sizeof (dump) >
   2566  1.33  christos 	    IWN_FW_DATA_BASE + hal->fw_data_maxsz) {
   2567  1.33  christos 		aprint_error_dev(sc->sc_dev,
   2568  1.33  christos 		    "bad firmware error log address 0x%08x\n", sc->errptr);
   2569  1.33  christos 		return;
   2570  1.33  christos 	}
   2571  1.33  christos 	if (iwn_nic_lock(sc) != 0) {
   2572  1.33  christos 		aprint_error_dev(sc->sc_dev,
   2573  1.33  christos 		    "could not read firmware error log\n");
   2574  1.33  christos 		return;
   2575  1.33  christos 	}
   2576  1.33  christos 	/* Read firmware error log from SRAM. */
   2577  1.33  christos 	iwn_mem_read_region_4(sc, sc->errptr, (uint32_t *)&dump,
   2578  1.33  christos 	    sizeof (dump) / sizeof (uint32_t));
   2579  1.33  christos 	iwn_nic_unlock(sc);
   2580   1.1      ober 
   2581  1.33  christos 	if (dump.valid == 0) {
   2582  1.33  christos 		aprint_error_dev(sc->sc_dev, "firmware error log is empty\n");
   2583  1.33  christos 		return;
   2584  1.33  christos 	}
   2585  1.33  christos 	printf("firmware error log:\n");
   2586  1.33  christos #if 0
   2587  1.33  christos 	printf("  error type      = \"%s\" (0x%08X)\n",
   2588  1.33  christos 	    (dump.id < nitems(iwn_fw_errmsg)) ?
   2589  1.33  christos 		iwn_fw_errmsg[dump.id] : "UNKNOWN",
   2590  1.33  christos 	    dump.id);
   2591  1.33  christos #endif
   2592  1.33  christos 	printf("  program counter = 0x%08X\n", dump.pc);
   2593  1.33  christos 	printf("  source line     = 0x%08X\n", dump.src_line);
   2594  1.33  christos 	printf("  error data      = 0x%08X%08X\n",
   2595  1.33  christos 	    dump.error_data[0], dump.error_data[1]);
   2596  1.33  christos 	printf("  branch link     = 0x%08X%08X\n",
   2597  1.33  christos 	    dump.branch_link[0], dump.branch_link[1]);
   2598  1.33  christos 	printf("  interrupt link  = 0x%08X%08X\n",
   2599  1.33  christos 	    dump.interrupt_link[0], dump.interrupt_link[1]);
   2600  1.33  christos 	printf("  time	    = %u\n", dump.time[0]);
   2601  1.33  christos 
   2602  1.33  christos 	/* Dump driver status (TX and RX rings) while we're here. */
   2603  1.33  christos 	printf("driver status:\n");
   2604  1.33  christos 	for (i = 0; i < hal->ntxqs; i++) {
   2605  1.33  christos 		struct iwn_tx_ring *ring = &sc->txq[i];
   2606  1.33  christos 		printf("  tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n",
   2607  1.33  christos 		    i, ring->qid, ring->cur, ring->queued);
   2608  1.33  christos 	}
   2609  1.33  christos 	printf("  rx ring: cur=%d\n", sc->rxq.cur);
   2610  1.33  christos 	printf("  802.11 state %d\n", sc->sc_ic.ic_state);
   2611  1.33  christos }
   2612  1.33  christos 
   2613  1.33  christos static int
   2614  1.33  christos iwn_intr(void *arg)
   2615  1.33  christos {
   2616  1.33  christos 	struct iwn_softc *sc = arg;
   2617  1.33  christos 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
   2618  1.33  christos 	uint32_t r1, r2;
   2619  1.33  christos 
   2620  1.33  christos 	/* Disable interrupts. */
   2621  1.33  christos 	IWN_WRITE(sc, IWN_MASK, 0);
   2622  1.33  christos 
   2623  1.33  christos 	r1 = IWN_READ(sc, IWN_INT);
   2624  1.33  christos 	r2 = IWN_READ(sc, IWN_FH_INT);
   2625  1.33  christos 
   2626  1.33  christos 	if (r1 == 0 && r2 == 0) {
   2627  1.33  christos 		if (ifp->if_flags & IFF_UP)
   2628  1.33  christos 			IWN_WRITE(sc, IWN_MASK, IWN_INT_MASK);
   2629  1.33  christos 		return 0;	/* Interrupt not for us. */
   2630  1.33  christos 	}
   2631  1.33  christos 	if (r1 == 0xffffffff || (r1 & 0xfffffff0) == 0xa5a5a5a0)
   2632  1.33  christos 		return 0;	/* Hardware gone! */
   2633  1.33  christos 
   2634  1.33  christos 	/* Acknowledge interrupts. */
   2635  1.33  christos 	IWN_WRITE(sc, IWN_INT, r1);
   2636  1.33  christos 	IWN_WRITE(sc, IWN_FH_INT, r2);
   2637   1.1      ober 
   2638  1.33  christos 	if (r1 & IWN_INT_RF_TOGGLED) {
   2639  1.33  christos 		uint32_t tmp = IWN_READ(sc, IWN_GP_CNTRL);
   2640   1.8     blymn 		aprint_error_dev(sc->sc_dev, "RF switch: radio %s\n",
   2641  1.33  christos 		    (tmp & IWN_GP_CNTRL_RFKILL) ? "enabled" : "disabled");
   2642  1.33  christos 		sc->sc_radio = (tmp & IWN_GP_CNTRL_RFKILL);
   2643   1.1      ober 	}
   2644  1.33  christos 	if (r1 & IWN_INT_CT_REACHED) {
   2645   1.1      ober 		aprint_error_dev(sc->sc_dev, "critical temperature reached!\n");
   2646  1.33  christos 		/* XXX Reduce TX power? */
   2647   1.1      ober 	}
   2648  1.33  christos 	if (r1 & (IWN_INT_SW_ERR | IWN_INT_HW_ERR)) {
   2649   1.1      ober 		aprint_error_dev(sc->sc_dev, "fatal firmware error\n");
   2650  1.33  christos 		/* Dump firmware error log and stop. */
   2651  1.33  christos 		iwn_fatal_intr(sc);
   2652   1.1      ober 		iwn_stop(sc->sc_ic.ic_ifp, 1);
   2653   1.1      ober 		return 1;
   2654   1.1      ober 	}
   2655  1.33  christos 	if ((r1 & (IWN_INT_FH_RX | IWN_INT_SW_RX)) ||
   2656  1.33  christos 	    (r2 & IWN_FH_INT_RX))
   2657  1.33  christos 		iwn_notif_intr(sc);
   2658  1.33  christos 
   2659  1.33  christos 	if ((r1 & IWN_INT_FH_TX) || (r2 & IWN_FH_INT_TX))
   2660  1.33  christos 		wakeup(sc);	/* FH DMA transfer completed. */
   2661   1.1      ober 
   2662  1.33  christos 	if (r1 & IWN_INT_ALIVE)
   2663  1.33  christos 		wakeup(sc);	/* Firmware is alive. */
   2664   1.1      ober 
   2665  1.33  christos 	if (r1 & IWN_INT_WAKEUP)
   2666  1.33  christos 		iwn_wakeup_intr(sc);
   2667   1.1      ober 
   2668  1.33  christos 	/* Re-enable interrupts. */
   2669   1.1      ober 	if (ifp->if_flags & IFF_UP)
   2670  1.33  christos 		IWN_WRITE(sc, IWN_MASK, IWN_INT_MASK);
   2671   1.1      ober 
   2672   1.1      ober 	return 1;
   2673   1.1      ober }
   2674   1.1      ober 
   2675  1.33  christos /*
   2676  1.33  christos  * Update TX scheduler ring when transmitting an 802.11 frame (4965AGN and
   2677  1.33  christos  * 5000 adapters use a slightly different format.)
   2678  1.33  christos  */
   2679  1.33  christos static void
   2680  1.33  christos iwn4965_update_sched(struct iwn_softc *sc, int qid, int idx, uint8_t id,
   2681  1.33  christos     uint16_t len)
   2682  1.33  christos {
   2683  1.33  christos 	uint16_t *w = &sc->sched[qid * IWN4965_SCHED_COUNT + idx];
   2684  1.33  christos 
   2685  1.33  christos 	*w = htole16(len + 8);
   2686  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, sc->sched_dma.map,
   2687  1.33  christos 	    (char *)(void *)w - (char *)(void *)sc->sched_dma.vaddr,
   2688  1.33  christos 	    sizeof (uint16_t), BUS_DMASYNC_PREWRITE);
   2689  1.33  christos 	if (idx < IWN_SCHED_WINSZ) {
   2690  1.33  christos 		*(w + IWN_TX_RING_COUNT) = *w;
   2691  1.33  christos 		bus_dmamap_sync(sc->sc_dmat, sc->sched_dma.map,
   2692  1.33  christos 		    (char *)(void *)(w + IWN_TX_RING_COUNT) -
   2693  1.33  christos 		    (char *)(void *)sc->sched_dma.vaddr,
   2694  1.33  christos 		    sizeof (uint16_t), BUS_DMASYNC_PREWRITE);
   2695  1.33  christos 	}
   2696  1.33  christos }
   2697  1.33  christos 
   2698  1.33  christos static void
   2699  1.33  christos iwn5000_update_sched(struct iwn_softc *sc, int qid, int idx, uint8_t id,
   2700  1.33  christos     uint16_t len)
   2701   1.1      ober {
   2702  1.33  christos 	uint16_t *w = &sc->sched[qid * IWN5000_SCHED_COUNT + idx];
   2703  1.33  christos 
   2704  1.33  christos 	*w = htole16(id << 12 | (len + 8));
   2705  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, sc->sched_dma.map,
   2706  1.33  christos 	    (char *)(void *)w - (char *)(void *)sc->sched_dma.vaddr,
   2707  1.33  christos 	    sizeof (uint16_t), BUS_DMASYNC_PREWRITE);
   2708  1.33  christos 	if (idx < IWN_SCHED_WINSZ) {
   2709  1.33  christos 		*(w + IWN_TX_RING_COUNT) = *w;
   2710  1.33  christos 		bus_dmamap_sync(sc->sc_dmat, sc->sched_dma.map,
   2711  1.33  christos 		    (char *)(void *)(w + IWN_TX_RING_COUNT) -
   2712  1.33  christos 		    (char *)(void *)sc->sched_dma.vaddr,
   2713  1.33  christos 		    sizeof (uint16_t), BUS_DMASYNC_PREWRITE);
   2714  1.33  christos 	}
   2715   1.1      ober }
   2716   1.1      ober 
   2717  1.33  christos static void
   2718  1.33  christos iwn5000_reset_sched(struct iwn_softc *sc, int qid, int idx)
   2719  1.33  christos {
   2720  1.33  christos 	uint16_t *w = &sc->sched[qid * IWN5000_SCHED_COUNT + idx];
   2721  1.33  christos 
   2722  1.33  christos 	*w = (*w & htole16(0xf000)) | htole16(1);
   2723  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, sc->sched_dma.map,
   2724  1.33  christos 	    (char *)(void *)w - (char *)(void *)sc->sched_dma.vaddr,
   2725  1.33  christos 	    sizeof (uint16_t), BUS_DMASYNC_PREWRITE);
   2726  1.33  christos 	if (idx < IWN_SCHED_WINSZ) {
   2727  1.33  christos 		*(w + IWN_TX_RING_COUNT) = *w;
   2728  1.33  christos 		bus_dmamap_sync(sc->sc_dmat, sc->sched_dma.map,
   2729  1.33  christos 		    (char *)(void *)(w + IWN_TX_RING_COUNT) -
   2730  1.33  christos 		    (char *)(void *)sc->sched_dma.vaddr,
   2731  1.33  christos 		    sizeof (uint16_t), BUS_DMASYNC_PREWRITE);
   2732  1.33  christos 	}
   2733  1.33  christos }
   2734   1.1      ober 
   2735   1.1      ober static int
   2736  1.33  christos iwn_tx(struct iwn_softc *sc, struct mbuf *m, struct ieee80211_node *ni, int ac)
   2737   1.1      ober {
   2738  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   2739   1.1      ober 	struct ieee80211com *ic = &sc->sc_ic;
   2740  1.33  christos 	struct iwn_node *wn = (void *)ni;
   2741  1.33  christos 	struct iwn_tx_ring *ring;
   2742   1.1      ober 	struct iwn_tx_desc *desc;
   2743   1.1      ober 	struct iwn_tx_data *data;
   2744   1.1      ober 	struct iwn_tx_cmd *cmd;
   2745   1.1      ober 	struct iwn_cmd_data *tx;
   2746  1.33  christos 	const struct iwn_rate *rinfo;
   2747   1.1      ober 	struct ieee80211_frame *wh;
   2748  1.33  christos 	struct ieee80211_key *k = NULL;
   2749   1.1      ober 	const struct chanAccParams *cap;
   2750  1.33  christos 	struct mbuf *m1;
   2751   1.1      ober 	uint32_t flags;
   2752  1.33  christos 	u_int hdrlen;
   2753  1.33  christos 	bus_dma_segment_t *seg;
   2754  1.33  christos 	uint8_t ridx, txant, type;
   2755  1.33  christos 	int i, totlen, error, pad, noack;
   2756   1.1      ober 
   2757  1.33  christos 	wh = mtod(m, struct ieee80211_frame *);
   2758  1.33  christos 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
   2759   1.1      ober 
   2760  1.33  christos 	/* JAF XXX two lines above were not in wpi. check we don't duplicate this */
   2761   1.1      ober 
   2762   1.1      ober 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
   2763   1.1      ober 		hdrlen = sizeof (struct ieee80211_qosframe);
   2764   1.1      ober 		cap = &ic->ic_wme.wme_chanParams;
   2765   1.1      ober 		noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
   2766  1.33  christos 	} else {
   2767   1.1      ober 		hdrlen = sizeof (struct ieee80211_frame);
   2768  1.33  christos 		noack = 0;
   2769  1.33  christos 	}
   2770  1.33  christos 
   2771   1.1      ober 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   2772  1.33  christos 		k = ieee80211_crypto_encap(ic, ni, m);
   2773   1.1      ober 		if (k == NULL) {
   2774  1.33  christos 			m_freem(m);
   2775   1.1      ober 			return ENOBUFS;
   2776   1.1      ober 		}
   2777   1.1      ober 		/* packet header may have moved, reset our local pointer */
   2778  1.33  christos 		wh = mtod(m, struct ieee80211_frame *);
   2779   1.1      ober 	}
   2780   1.1      ober 
   2781  1.33  christos 	ring = &sc->txq[ac];
   2782  1.33  christos 	desc = &ring->desc[ring->cur];
   2783  1.33  christos 	data = &ring->data[ring->cur];
   2784  1.33  christos 
   2785  1.33  christos 	/* Choose a TX rate index. */
   2786  1.11     blymn 	if (type == IEEE80211_FC0_TYPE_MGT) {
   2787   1.1      ober 		/* mgmt frames are sent at the lowest available bit-rate */
   2788  1.33  christos 		ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
   2789  1.33  christos 		    IWN_RIDX_OFDM6 : IWN_RIDX_CCK1;
   2790   1.1      ober 	} else {
   2791   1.2      ober 		if (ic->ic_fixed_rate != -1) {
   2792  1.33  christos 			ridx = sc->fixed_ridx;
   2793   1.2      ober 		} else
   2794  1.33  christos 			ridx = wn->ridx[ni->ni_txrate];
   2795   1.1      ober 	}
   2796  1.33  christos 	rinfo = &iwn_rates[ridx];
   2797   1.1      ober 
   2798   1.1      ober 	if (sc->sc_drvbpf != NULL) {
   2799   1.1      ober 		struct iwn_tx_radiotap_header *tap = &sc->sc_txtap;
   2800   1.1      ober 
   2801   1.1      ober 		tap->wt_flags = 0;
   2802   1.1      ober 		tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq);
   2803   1.1      ober 		tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags);
   2804  1.33  christos 		tap->wt_rate = rinfo->rate;
   2805   1.1      ober 		tap->wt_hwqueue = ac;
   2806   1.1      ober 		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
   2807   1.1      ober 			tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
   2808   1.1      ober 
   2809  1.38     joerg 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
   2810  1.33  christos 	}
   2811  1.33  christos 
   2812  1.33  christos 	totlen = m->m_pkthdr.len;
   2813  1.33  christos 
   2814  1.33  christos 	/* Encrypt the frame if need be. */
   2815  1.33  christos #ifdef IEEE80211_FC1_PROTECTED
   2816  1.33  christos 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
   2817  1.33  christos 		/* Retrieve key for TX. */
   2818  1.33  christos 		k = ieee80211_get_txkey(ic, wh, ni);
   2819  1.33  christos 		if (k->k_cipher != IEEE80211_CIPHER_CCMP) {
   2820  1.33  christos 			/* Do software encryption. */
   2821  1.33  christos 			if ((m = ieee80211_encrypt(ic, m, k)) == NULL)
   2822  1.33  christos 				return ENOBUFS;
   2823  1.33  christos 			/* 802.11 header may have moved. */
   2824  1.33  christos 			wh = mtod(m, struct ieee80211_frame *);
   2825  1.33  christos 			totlen = m->m_pkthdr.len;
   2826  1.33  christos 
   2827  1.33  christos 		} else	/* HW appends CCMP MIC. */
   2828  1.33  christos 			totlen += IEEE80211_CCMP_HDRLEN;
   2829   1.1      ober 	}
   2830   1.1      ober #endif
   2831   1.1      ober 
   2832  1.33  christos 	/* Prepare TX firmware command. */
   2833   1.1      ober 	cmd = &ring->cmd[ring->cur];
   2834   1.1      ober 	cmd->code = IWN_CMD_TX_DATA;
   2835   1.1      ober 	cmd->flags = 0;
   2836   1.1      ober 	cmd->qid = ring->qid;
   2837   1.1      ober 	cmd->idx = ring->cur;
   2838   1.8     blymn 
   2839   1.1      ober 	tx = (struct iwn_cmd_data *)cmd->data;
   2840  1.33  christos 	/* NB: No need to clear tx, all fields are reinitialized here. */
   2841  1.33  christos 	tx->scratch = 0;	/* clear "scratch" area */
   2842   1.8     blymn 
   2843  1.33  christos 	flags = 0;
   2844  1.33  christos 	if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   2845   1.1      ober 		flags |= IWN_TX_NEED_ACK;
   2846  1.33  christos 	} else if (m->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold)
   2847  1.33  christos 		flags |= IWN_TX_NEED_RTS | IWN_TX_FULL_TXOP;
   2848  1.33  christos 
   2849  1.33  christos #ifdef notyet
   2850  1.33  christos 	if ((wh->i_fc[0] &
   2851  1.33  christos 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
   2852  1.33  christos 	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR))
   2853  1.33  christos 		flags |= IWN_TX_IMM_BA;		/* Cannot happen yet. */
   2854  1.33  christos #endif
   2855  1.33  christos 
   2856  1.33  christos 	if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG)
   2857  1.33  christos 		flags |= IWN_TX_MORE_FRAG;	/* Cannot happen yet. */
   2858  1.33  christos 
   2859  1.33  christos 	/* Check if frame must be protected using RTS/CTS or CTS-to-self. */
   2860  1.33  christos 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   2861  1.33  christos 		/* NB: Group frames are sent using CCK in 802.11b/g. */
   2862  1.33  christos 		if (totlen + IEEE80211_CRC_LEN > ic->ic_rtsthreshold) {
   2863  1.33  christos 			flags |= IWN_TX_NEED_RTS;
   2864  1.33  christos 		} else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
   2865  1.33  christos 		    ridx >= IWN_RIDX_OFDM6) {
   2866  1.33  christos 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
   2867  1.33  christos 				flags |= IWN_TX_NEED_CTS;
   2868  1.33  christos 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
   2869  1.33  christos 				flags |= IWN_TX_NEED_RTS;
   2870  1.33  christos 		}
   2871  1.33  christos 		if (flags & (IWN_TX_NEED_RTS | IWN_TX_NEED_CTS)) {
   2872  1.33  christos 			if (sc->hw_type != IWN_HW_REV_TYPE_4965) {
   2873  1.33  christos 				/* 5000 autoselects RTS/CTS or CTS-to-self. */
   2874  1.33  christos 				flags &= ~(IWN_TX_NEED_RTS | IWN_TX_NEED_CTS);
   2875  1.33  christos 				flags |= IWN_TX_NEED_PROTECTION;
   2876  1.33  christos 			} else
   2877  1.33  christos 				flags |= IWN_TX_FULL_TXOP;
   2878  1.33  christos 		}
   2879  1.33  christos 	}
   2880   1.8     blymn 
   2881  1.33  christos 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
   2882  1.33  christos 	    type != IEEE80211_FC0_TYPE_DATA)
   2883  1.33  christos 		tx->id = hal->broadcast_id;
   2884  1.20     blymn 	else
   2885  1.33  christos 		tx->id = wn->id;
   2886  1.11     blymn 
   2887   1.1      ober 	if (type == IEEE80211_FC0_TYPE_MGT) {
   2888   1.1      ober 		uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
   2889   1.1      ober 
   2890  1.33  christos #ifndef IEEE80211_STA_ONLY
   2891  1.33  christos 		/* Tell HW to set timestamp in probe responses. */
   2892  1.11     blymn 		if ((subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) ||
   2893  1.11     blymn 		    (subtype == IEEE80211_FC0_SUBTYPE_PROBE_REQ))
   2894   1.1      ober 			flags |= IWN_TX_INSERT_TSTAMP;
   2895  1.33  christos #endif
   2896   1.1      ober 		if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
   2897  1.20     blymn 		    subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ ||
   2898  1.20     blymn 		    subtype == IEEE80211_FC0_SUBTYPE_AUTH ||
   2899  1.20     blymn 		    subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
   2900  1.11     blymn 			flags &= ~IWN_TX_NEED_RTS;
   2901  1.11     blymn 			flags |= IWN_TX_NEED_CTS;
   2902   1.1      ober 			tx->timeout = htole16(3);
   2903  1.11     blymn 		} else
   2904   1.1      ober 			tx->timeout = htole16(2);
   2905   1.1      ober 	} else
   2906   1.1      ober 		tx->timeout = htole16(0);
   2907   1.8     blymn 
   2908   1.1      ober 	if (hdrlen & 3) {
   2909  1.33  christos 		/* First segment's length must be a multiple of 4. */
   2910   1.1      ober 		flags |= IWN_TX_NEED_PADDING;
   2911   1.1      ober 		pad = 4 - (hdrlen & 3);
   2912   1.1      ober 	} else
   2913   1.1      ober 		pad = 0;
   2914   1.1      ober 
   2915  1.33  christos #if 0
   2916  1.11     blymn 	if (type == IEEE80211_FC0_TYPE_CTL) {
   2917  1.11     blymn 		uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
   2918  1.11     blymn 
   2919  1.11     blymn 		/* tell h/w to set timestamp in probe responses */
   2920  1.11     blymn 		if (subtype == 0x0080) /* linux says this is "back request" */
   2921  1.11     blymn 			/* linux says (1 << 6) is IMM_BA_RSP_MASK */
   2922  1.11     blymn 			flags |= (IWN_TX_NEED_ACK | (1 << 6));
   2923  1.11     blymn 	}
   2924  1.33  christos #endif
   2925  1.11     blymn 
   2926  1.33  christos 	tx->len = htole16(totlen);
   2927  1.33  christos 	tx->tid = 0/* tid */;
   2928   1.1      ober 	tx->rts_ntries = 60;
   2929   1.1      ober 	tx->data_ntries = 15;
   2930   1.1      ober 	tx->lifetime = htole32(IWN_LIFETIME_INFINITE);
   2931  1.33  christos 	tx->plcp = rinfo->plcp;
   2932  1.33  christos 	tx->rflags = rinfo->flags;
   2933  1.33  christos 	if (tx->id == hal->broadcast_id) {
   2934  1.33  christos 		/* Group or management frame. */
   2935  1.33  christos 		tx->linkq = 0;
   2936  1.33  christos 		/* XXX Alternate between antenna A and B? */
   2937  1.33  christos 		txant = IWN_LSB(sc->txantmsk);
   2938  1.33  christos 		tx->rflags |= IWN_RFLAG_ANT(txant);
   2939   1.1      ober 	} else {
   2940  1.33  christos 		tx->linkq = ni->ni_rates.rs_nrates - ni->ni_txrate - 1;
   2941  1.33  christos 		flags |= IWN_TX_LINKQ;	/* enable MRR */
   2942   1.1      ober 	}
   2943  1.33  christos 	/* Set physical address of "scratch area". */
   2944  1.33  christos 	tx->loaddr = htole32(IWN_LOADDR(data->scratch_paddr));
   2945  1.33  christos 	tx->hiaddr = IWN_HIADDR(data->scratch_paddr);
   2946   1.1      ober 
   2947  1.33  christos 	/* Copy 802.11 header in TX command. */
   2948  1.20     blymn 	memcpy(((uint8_t *)tx) + sizeof(*tx), wh, hdrlen);
   2949   1.1      ober 
   2950  1.33  christos 	/* Trim 802.11 header. */
   2951  1.33  christos 	m_adj(m, hdrlen);
   2952  1.33  christos 	tx->security = 0;
   2953  1.33  christos 
   2954  1.33  christos #ifdef notyet
   2955  1.33  christos 	if (k != NULL && k->k_cipher == IEEE80211_CIPHER_CCMP) {
   2956  1.33  christos 		/* Trim 802.11 header and prepend CCMP IV. */
   2957  1.33  christos 		m_adj(m, hdrlen - IEEE80211_CCMP_HDRLEN);
   2958  1.33  christos 		ivp = mtod(m, uint8_t *);
   2959  1.33  christos 		k->k_tsc++;
   2960  1.33  christos 		ivp[0] = k->k_tsc;
   2961  1.33  christos 		ivp[1] = k->k_tsc >> 8;
   2962  1.33  christos 		ivp[2] = 0;
   2963  1.33  christos 		ivp[3] = k->k_id << 6 | IEEE80211_WEP_EXTIV;
   2964  1.33  christos 		ivp[4] = k->k_tsc >> 16;
   2965  1.33  christos 		ivp[5] = k->k_tsc >> 24;
   2966  1.33  christos 		ivp[6] = k->k_tsc >> 32;
   2967  1.33  christos 		ivp[7] = k->k_tsc >> 40;
   2968  1.33  christos 
   2969  1.33  christos 		tx->security = IWN_CIPHER_CCMP;
   2970  1.33  christos 		/* XXX flags |= IWN_TX_AMPDU_CCMP; */
   2971  1.33  christos 		memcpy(tx->key, k->k_key, k->k_len);
   2972  1.33  christos 
   2973  1.33  christos 		/* TX scheduler includes CCMP MIC len w/5000 Series. */
   2974  1.33  christos 		if (sc->hw_type != IWN_HW_REV_TYPE_4965)
   2975  1.33  christos 			totlen += IEEE80211_CCMP_MICLEN;
   2976  1.33  christos 	} else {
   2977  1.33  christos 		/* Trim 802.11 header. */
   2978  1.33  christos 		m_adj(m, hdrlen);
   2979  1.33  christos 		tx->security = 0;
   2980  1.33  christos 	}
   2981  1.33  christos #endif
   2982  1.33  christos 	tx->flags = htole32(flags);
   2983  1.33  christos 
   2984  1.33  christos 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m,
   2985   1.1      ober 	    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   2986   1.1      ober 	if (error != 0 && error != EFBIG) {
   2987  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
   2988  1.33  christos 		    error);
   2989  1.33  christos 		m_freem(m);
   2990   1.1      ober 		return error;
   2991   1.1      ober 	}
   2992   1.1      ober 	if (error != 0) {
   2993  1.33  christos 		/* Too many DMA segments, linearize mbuf. */
   2994  1.33  christos 		MGETHDR(m1, M_DONTWAIT, MT_DATA);
   2995  1.33  christos 		if (m1 == NULL) {
   2996  1.33  christos 			m_freem(m);
   2997  1.33  christos 			return ENOBUFS;
   2998   1.1      ober 		}
   2999  1.33  christos 		if (m->m_pkthdr.len > MHLEN) {
   3000  1.33  christos 			MCLGET(m1, M_DONTWAIT);
   3001  1.33  christos 			if (!(m1->m_flags & M_EXT)) {
   3002  1.33  christos 				m_freem(m);
   3003  1.33  christos 				m_freem(m1);
   3004  1.33  christos 				return ENOBUFS;
   3005   1.1      ober 			}
   3006   1.1      ober 		}
   3007  1.33  christos 		m_copydata(m, 0, m->m_pkthdr.len, mtod(m1, void *));
   3008  1.33  christos 		m1->m_pkthdr.len = m1->m_len = m->m_pkthdr.len;
   3009  1.33  christos 		m_freem(m);
   3010  1.33  christos 		m = m1;
   3011   1.1      ober 
   3012  1.33  christos 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m,
   3013   1.1      ober 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   3014   1.1      ober 		if (error != 0) {
   3015  1.33  christos 			aprint_error_dev(sc->sc_dev,
   3016  1.33  christos 			    "could not map mbuf (error %d)\n", error);
   3017  1.33  christos 			m_freem(m);
   3018   1.1      ober 			return error;
   3019   1.1      ober 		}
   3020   1.1      ober 	}
   3021   1.1      ober 
   3022  1.33  christos 	data->m = m;
   3023   1.1      ober 	data->ni = ni;
   3024   1.1      ober 
   3025   1.1      ober 	DPRINTFN(4, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
   3026  1.33  christos 	    ring->qid, ring->cur, m->m_pkthdr.len, data->map->dm_nsegs));
   3027   1.1      ober 
   3028  1.33  christos 	/* Fill TX descriptor. */
   3029  1.33  christos 	desc->nsegs = 1 + data->map->dm_nsegs;
   3030  1.33  christos 	/* First DMA segment is used by the TX command. */
   3031  1.33  christos 	desc->segs[0].addr = htole32(IWN_LOADDR(data->cmd_paddr));
   3032  1.33  christos 	desc->segs[0].len  = htole16(IWN_HIADDR(data->cmd_paddr) |
   3033  1.33  christos 	    (4 + sizeof (*tx) + hdrlen + pad) << 4);
   3034  1.33  christos 	/* Other DMA segments are for data payload. */
   3035  1.33  christos 	seg = data->map->dm_segs;
   3036   1.1      ober 	for (i = 1; i <= data->map->dm_nsegs; i++) {
   3037  1.33  christos 		desc->segs[i].addr = htole32(IWN_LOADDR(seg->ds_addr));
   3038  1.33  christos 		desc->segs[i].len  = htole16(IWN_HIADDR(seg->ds_addr) |
   3039  1.33  christos 		    seg->ds_len << 4);
   3040  1.33  christos 		seg++;
   3041  1.33  christos 	}
   3042  1.33  christos 
   3043  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
   3044  1.33  christos 	    BUS_DMASYNC_PREWRITE);
   3045  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, ring->cmd_dma.map,
   3046  1.33  christos 	    (char *)(void *)cmd - (char *)(void *)ring->cmd_dma.vaddr,
   3047  1.33  christos 	    sizeof (*cmd), BUS_DMASYNC_PREWRITE);
   3048  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
   3049  1.33  christos 	    (char *)(void *)desc - (char *)(void *)ring->desc_dma.vaddr,
   3050  1.33  christos 	    sizeof (*desc), BUS_DMASYNC_PREWRITE);
   3051   1.1      ober 
   3052  1.33  christos 	/* Update TX scheduler. */
   3053  1.33  christos 	hal->update_sched(sc, ring->qid, ring->cur, tx->id, totlen);
   3054   1.1      ober 
   3055  1.33  christos 	/* Kick TX ring. */
   3056  1.33  christos 	ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT;
   3057  1.33  christos 	IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
   3058  1.20     blymn 
   3059  1.33  christos 	/* Mark TX ring as full if we reach a certain threshold. */
   3060  1.33  christos 	if (++ring->queued > IWN_TX_RING_HIMARK)
   3061  1.33  christos 		sc->qfullmsk |= 1 << ring->qid;
   3062   1.1      ober 
   3063   1.1      ober 	return 0;
   3064   1.1      ober }
   3065   1.1      ober 
   3066   1.1      ober static void
   3067   1.1      ober iwn_start(struct ifnet *ifp)
   3068   1.1      ober {
   3069   1.1      ober 	struct iwn_softc *sc = ifp->if_softc;
   3070   1.1      ober 	struct ieee80211com *ic = &sc->sc_ic;
   3071   1.1      ober 	struct ieee80211_node *ni;
   3072   1.1      ober 	struct ether_header *eh;
   3073  1.33  christos 	struct mbuf *m;
   3074   1.1      ober 	int ac;
   3075   1.1      ober 
   3076  1.11     blymn 	DPRINTFN(5, ("iwn_start enter\n"));
   3077  1.11     blymn 
   3078   1.1      ober 	/*
   3079   1.1      ober 	 * net80211 may still try to send management frames even if the
   3080  1.28     blymn 	 * IFF_RUNNING flag is not set... Also, don't bother if the radio
   3081  1.28     blymn 	 * is not enabled.
   3082   1.1      ober 	 */
   3083  1.28     blymn 	if (((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) ||
   3084  1.28     blymn 	    !sc->sc_radio)
   3085   1.1      ober 		return;
   3086   1.1      ober 
   3087   1.1      ober 	for (;;) {
   3088  1.33  christos 		if (sc->qfullmsk != 0) {
   3089  1.33  christos 			ifp->if_flags |= IFF_OACTIVE;
   3090  1.33  christos 			break;
   3091  1.33  christos 		}
   3092  1.33  christos 		/* Send pending management frames first. */
   3093  1.33  christos 		IF_DEQUEUE(&ic->ic_mgtq, m);
   3094  1.33  christos 		if (m != NULL) {
   3095  1.33  christos 			ni = (void *)m->m_pkthdr.rcvif;
   3096  1.33  christos 			ac = 0;
   3097  1.33  christos 			goto sendit;
   3098  1.33  christos 		}
   3099  1.33  christos 		if (ic->ic_state != IEEE80211_S_RUN)
   3100  1.33  christos 			break;
   3101   1.8     blymn 
   3102  1.33  christos 		/* Encapsulate and send data frames. */
   3103  1.33  christos 		IFQ_DEQUEUE(&ifp->if_snd, m);
   3104  1.33  christos 		if (m == NULL)
   3105  1.33  christos 			break;
   3106  1.33  christos 		if (m->m_len < sizeof (*eh) &&
   3107  1.33  christos 		    (m = m_pullup(m, sizeof (*eh))) == NULL) {
   3108  1.33  christos 			ifp->if_oerrors++;
   3109  1.33  christos 			continue;
   3110  1.33  christos 		}
   3111  1.33  christos 		eh = mtod(m, struct ether_header *);
   3112  1.33  christos 		ni = ieee80211_find_txnode(ic, eh->ether_dhost);
   3113  1.33  christos 		if (ni == NULL) {
   3114  1.33  christos 			m_freem(m);
   3115  1.33  christos 			ifp->if_oerrors++;
   3116  1.33  christos 			continue;
   3117  1.33  christos 		}
   3118  1.33  christos 		/* classify mbuf so we can find which tx ring to use */
   3119  1.33  christos 		if (ieee80211_classify(ic, m, ni) != 0) {
   3120  1.33  christos 			m_freem(m);
   3121  1.33  christos 			ieee80211_free_node(ni);
   3122  1.33  christos 			ifp->if_oerrors++;
   3123  1.33  christos 			continue;
   3124  1.33  christos 		}
   3125   1.1      ober 
   3126  1.33  christos 		/* no QoS encapsulation for EAPOL frames */
   3127  1.33  christos 		ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
   3128  1.33  christos 		    M_WME_GETAC(m) : WME_AC_BE;
   3129  1.38     joerg 		bpf_mtap(ifp, m);
   3130  1.33  christos 		if ((m = ieee80211_encap(ic, m, ni)) == NULL) {
   3131  1.33  christos 			ieee80211_free_node(ni);
   3132  1.33  christos 			ifp->if_oerrors++;
   3133  1.33  christos 			continue;
   3134  1.33  christos 		}
   3135  1.33  christos sendit:
   3136  1.38     joerg 		bpf_mtap3(ic->ic_rawbpf, m);
   3137  1.33  christos 		if (iwn_tx(sc, m, ni, ac) != 0) {
   3138  1.33  christos 			ieee80211_free_node(ni);
   3139  1.33  christos 			ifp->if_oerrors++;
   3140  1.33  christos 			continue;
   3141   1.1      ober 		}
   3142   1.1      ober 
   3143   1.1      ober 		sc->sc_tx_timer = 5;
   3144   1.1      ober 		ifp->if_timer = 1;
   3145   1.1      ober 	}
   3146   1.1      ober }
   3147   1.1      ober 
   3148   1.1      ober static void
   3149   1.1      ober iwn_watchdog(struct ifnet *ifp)
   3150   1.1      ober {
   3151   1.1      ober 	struct iwn_softc *sc = ifp->if_softc;
   3152   1.1      ober 
   3153   1.1      ober 	ifp->if_timer = 0;
   3154   1.1      ober 
   3155   1.1      ober 	if (sc->sc_tx_timer > 0) {
   3156   1.1      ober 		if (--sc->sc_tx_timer == 0) {
   3157   1.8     blymn 			aprint_error_dev(sc->sc_dev, "device timeout\n");
   3158   1.1      ober 			iwn_stop(ifp, 1);
   3159   1.1      ober 			ifp->if_oerrors++;
   3160   1.1      ober 			return;
   3161   1.1      ober 		}
   3162   1.1      ober 		ifp->if_timer = 1;
   3163   1.1      ober 	}
   3164   1.1      ober 
   3165   1.1      ober 	ieee80211_watchdog(&sc->sc_ic);
   3166   1.1      ober }
   3167   1.1      ober 
   3168   1.1      ober static int
   3169  1.33  christos iwn_ioctl(struct ifnet *ifp, u_long cmd, void* data)
   3170   1.1      ober {
   3171  1.33  christos #define IS_RUNNING(ifp) \
   3172   1.1      ober 	((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
   3173   1.8     blymn 
   3174   1.1      ober 	struct iwn_softc *sc = ifp->if_softc;
   3175   1.1      ober 	struct ieee80211com *ic = &sc->sc_ic;
   3176   1.1      ober 	int s, error = 0;
   3177   1.1      ober 
   3178   1.1      ober 	s = splnet();
   3179   1.1      ober 
   3180   1.1      ober 	switch (cmd) {
   3181  1.33  christos 	case SIOCSIFADDR:
   3182  1.33  christos 		/* FALLTHROUGH */
   3183   1.1      ober 	case SIOCSIFFLAGS:
   3184  1.25    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   3185  1.25    dyoung 			break;
   3186   1.1      ober 		if (ifp->if_flags & IFF_UP) {
   3187  1.28     blymn 			/*
   3188  1.28     blymn 			 * resync the radio state just in case we missed
   3189  1.28     blymn 			 * and event.
   3190  1.28     blymn 			 */
   3191  1.28     blymn 			sc->sc_radio =
   3192  1.33  christos 			    (IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_RFKILL);
   3193  1.28     blymn 
   3194  1.28     blymn 			if (!sc->sc_radio) {
   3195  1.33  christos 				error = EBUSY; /* XXX not really but same as els
   3196  1.33  christos ewhere in driver */
   3197  1.28     blymn 				if (ifp->if_flags & IFF_RUNNING)
   3198  1.28     blymn 					iwn_stop(ifp, 1);
   3199  1.28     blymn 			} else if (!(ifp->if_flags & IFF_RUNNING))
   3200  1.33  christos 				error = iwn_init(ifp);
   3201   1.1      ober 		} else {
   3202   1.1      ober 			if (ifp->if_flags & IFF_RUNNING)
   3203   1.1      ober 				iwn_stop(ifp, 1);
   3204   1.1      ober 		}
   3205   1.1      ober 		break;
   3206   1.1      ober 
   3207   1.1      ober 	case SIOCADDMULTI:
   3208   1.1      ober 	case SIOCDELMULTI:
   3209   1.1      ober 		/* XXX no h/w multicast filter? --dyoung */
   3210   1.1      ober 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   3211  1.33  christos 			 /* setup multicast filter, etc */
   3212  1.33  christos 			error = 0;
   3213  1.33  christos 		}
   3214  1.33  christos 		break;
   3215  1.33  christos 
   3216  1.33  christos #if 0
   3217  1.33  christos 	case SIOCS80211POWER:
   3218  1.33  christos 		error = ieee80211_ioctl(ifp, cmd, data);
   3219  1.33  christos 		if (error != ENETRESET)
   3220  1.33  christos 			break;
   3221  1.33  christos 		if (ic->ic_state == IEEE80211_S_RUN &&
   3222  1.33  christos 		    sc->calib.state == IWN_CALIB_STATE_RUN) {
   3223  1.33  christos 			if (ic->ic_flags & IEEE80211_F_PMGTON)
   3224  1.33  christos 				error = iwn_set_pslevel(sc, 0, 3, 0);
   3225  1.33  christos 			else	/* back to CAM */
   3226  1.33  christos 				error = iwn_set_pslevel(sc, 0, 0, 0);
   3227  1.33  christos 		} else {
   3228  1.33  christos 			/* Defer until transition to IWN_CALIB_STATE_RUN. */
   3229   1.1      ober 			error = 0;
   3230   1.1      ober 		}
   3231   1.1      ober 		break;
   3232  1.33  christos #endif
   3233   1.1      ober 
   3234   1.1      ober 	default:
   3235   1.1      ober 		error = ieee80211_ioctl(ic, cmd, data);
   3236   1.1      ober 	}
   3237   1.1      ober 
   3238   1.1      ober 	if (error == ENETRESET) {
   3239  1.33  christos 		error = 0;
   3240   1.8     blymn 		if (IS_RUNNING(ifp) &&
   3241  1.33  christos 		    (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)) {
   3242  1.33  christos 			iwn_stop(ifp, 0);
   3243  1.33  christos 			error = iwn_init(ifp);
   3244  1.33  christos 		}
   3245   1.1      ober 	}
   3246   1.1      ober 	splx(s);
   3247   1.1      ober 	return error;
   3248   1.1      ober #undef IS_RUNNING
   3249   1.1      ober }
   3250   1.1      ober 
   3251  1.33  christos /*
   3252  1.33  christos  * Send a command to the firmware.
   3253  1.33  christos  */
   3254  1.33  christos static int
   3255  1.33  christos iwn_cmd(struct iwn_softc *sc, int code, const void *buf, int size, int async)
   3256   1.1      ober {
   3257  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   3258  1.33  christos 	struct iwn_tx_ring *ring = &sc->txq[4];
   3259  1.33  christos 	struct iwn_tx_desc *desc;
   3260  1.33  christos 	struct iwn_tx_data *data;
   3261  1.33  christos 	struct iwn_tx_cmd *cmd;
   3262  1.33  christos 	struct mbuf *m;
   3263  1.33  christos 	bus_addr_t paddr;
   3264  1.33  christos 	int totlen, error;
   3265  1.33  christos 
   3266  1.33  christos 	desc = &ring->desc[ring->cur];
   3267  1.33  christos 	data = &ring->data[ring->cur];
   3268  1.33  christos 	totlen = 4 + size;
   3269   1.1      ober 
   3270  1.33  christos 	if (size > sizeof cmd->data) {
   3271  1.33  christos 		/* Command is too large to fit in a descriptor. */
   3272  1.33  christos 		if (totlen > MCLBYTES)
   3273  1.33  christos 			return EINVAL;
   3274  1.33  christos 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   3275  1.33  christos 		if (m == NULL)
   3276  1.33  christos 			return ENOMEM;
   3277  1.33  christos 		if (totlen > MHLEN) {
   3278  1.33  christos 			MCLGET(m, M_DONTWAIT);
   3279  1.33  christos 			if (!(m->m_flags & M_EXT)) {
   3280  1.33  christos 				m_freem(m);
   3281  1.33  christos 				return ENOMEM;
   3282  1.33  christos 			}
   3283  1.33  christos 		}
   3284  1.33  christos 		cmd = mtod(m, struct iwn_tx_cmd *);
   3285  1.33  christos 		error = bus_dmamap_load(sc->sc_dmat, data->map, cmd, totlen,
   3286  1.33  christos 		    NULL, BUS_DMA_NOWAIT);
   3287  1.33  christos 		if (error != 0) {
   3288  1.33  christos 			m_freem(m);
   3289  1.33  christos 			return error;
   3290  1.33  christos 		}
   3291  1.33  christos 		data->m = m;
   3292  1.33  christos 		paddr = data->map->dm_segs[0].ds_addr;
   3293  1.33  christos 	} else {
   3294  1.33  christos 		cmd = &ring->cmd[ring->cur];
   3295  1.33  christos 		paddr = data->cmd_paddr;
   3296   1.1      ober 	}
   3297   1.1      ober 
   3298  1.33  christos 	cmd->code = code;
   3299  1.33  christos 	cmd->flags = 0;
   3300  1.33  christos 	cmd->qid = ring->qid;
   3301  1.33  christos 	cmd->idx = ring->cur;
   3302  1.33  christos 	memcpy(cmd->data, buf, size);
   3303   1.1      ober 
   3304  1.33  christos 	desc->nsegs = 1;
   3305  1.33  christos 	desc->segs[0].addr = htole32(IWN_LOADDR(paddr));
   3306  1.33  christos 	desc->segs[0].len  = htole16(IWN_HIADDR(paddr) | totlen << 4);
   3307  1.33  christos 
   3308  1.33  christos 	if (size > sizeof cmd->data) {
   3309  1.33  christos 		bus_dmamap_sync(sc->sc_dmat, data->map, 0, totlen,
   3310  1.33  christos 		    BUS_DMASYNC_PREWRITE);
   3311  1.33  christos 	} else {
   3312  1.33  christos 		bus_dmamap_sync(sc->sc_dmat, ring->cmd_dma.map,
   3313  1.33  christos 		    (char *)(void *)cmd - (char *)(void *)ring->cmd_dma.vaddr,
   3314  1.33  christos 		    totlen, BUS_DMASYNC_PREWRITE);
   3315  1.33  christos 	}
   3316  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
   3317  1.33  christos 	    (char *)(void *)desc - (char *)(void *)ring->desc_dma.vaddr,
   3318  1.33  christos 	    sizeof (*desc), BUS_DMASYNC_PREWRITE);
   3319   1.1      ober 
   3320  1.33  christos 	/* Update TX scheduler. */
   3321  1.33  christos 	hal->update_sched(sc, ring->qid, ring->cur, 0, 0);
   3322   1.1      ober 
   3323  1.33  christos 	/* Kick command ring. */
   3324  1.33  christos 	ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT;
   3325  1.33  christos 	IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
   3326   1.1      ober 
   3327  1.33  christos 	return async ? 0 : tsleep(desc, PCATCH, "iwncmd", hz);
   3328   1.1      ober }
   3329   1.1      ober 
   3330  1.33  christos static int
   3331  1.33  christos iwn_add_node(struct iwn_softc *sc, struct ieee80211_node *ni, bool broadcast,
   3332  1.33  christos     bool async, uint32_t htflags)
   3333   1.1      ober {
   3334  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   3335  1.33  christos 	struct iwn_node_info node;
   3336  1.33  christos 	int error;
   3337   1.1      ober 
   3338  1.33  christos 	error = 0;
   3339   1.1      ober 
   3340  1.33  christos 	memset(&node, 0, sizeof node);
   3341  1.33  christos 	if (broadcast == true) {
   3342  1.33  christos 		IEEE80211_ADDR_COPY(node.macaddr, etherbroadcastaddr);
   3343  1.33  christos 		node.id = hal->broadcast_id;
   3344  1.33  christos 		DPRINTF(("adding broadcast node\n"));
   3345  1.33  christos 	} else {
   3346  1.33  christos 		IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
   3347  1.33  christos 		node.id = IWN_ID_BSS;
   3348  1.33  christos 		node.htflags = htole32(htflags);
   3349  1.33  christos 		DPRINTF(("adding BSS node\n"));
   3350  1.33  christos 	}
   3351  1.33  christos 	if ((error = hal->add_node(sc, &node, async)) != 0) {
   3352  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not add %s node\n",
   3353  1.33  christos 		    (broadcast == 1)? "broadcast" : "BSS");
   3354  1.33  christos 		return error;
   3355  1.33  christos 	}
   3356  1.33  christos 	DPRINTF(("setting link quality for node %d\n", node.id));
   3357  1.33  christos 	if ((error = iwn_set_link_quality(sc, ni)) != 0) {
   3358  1.33  christos 		aprint_error_dev(sc->sc_dev,
   3359  1.33  christos 				 "could not setup MRR for %s node\n",
   3360  1.33  christos 				 (broadcast == 1)? "broadcast" : "BSS");
   3361  1.33  christos 		return error;
   3362  1.33  christos 	}
   3363  1.33  christos 	if ((error = iwn_init_sensitivity(sc)) != 0) {
   3364  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not set sensitivity\n");
   3365  1.33  christos 		return error;
   3366  1.33  christos 	}
   3367   1.1      ober 
   3368  1.33  christos 	return error;
   3369  1.33  christos }
   3370   1.1      ober 
   3371   1.1      ober 
   3372  1.33  christos static int
   3373  1.33  christos iwn4965_add_node(struct iwn_softc *sc, struct iwn_node_info *node, int async)
   3374  1.33  christos {
   3375  1.33  christos 	struct iwn4965_node_info hnode;
   3376  1.33  christos 	char *src, *dst;
   3377   1.1      ober 
   3378  1.33  christos 	/*
   3379  1.33  christos 	 * We use the node structure for 5000 Series internally (it is
   3380  1.33  christos 	 * a superset of the one for 4965AGN). We thus copy the common
   3381  1.33  christos 	 * fields before sending the command.
   3382  1.33  christos 	 */
   3383  1.33  christos 	src = (char *)node;
   3384  1.33  christos 	dst = (char *)&hnode;
   3385  1.33  christos 	memcpy(dst, src, 48);
   3386  1.33  christos 	/* Skip TSC, RX MIC and TX MIC fields from ``src''. */
   3387  1.33  christos 	memcpy(dst + 48, src + 72, 20);
   3388  1.33  christos 	return iwn_cmd(sc, IWN_CMD_ADD_NODE, &hnode, sizeof hnode, async);
   3389   1.1      ober }
   3390   1.1      ober 
   3391  1.33  christos static int
   3392  1.33  christos iwn5000_add_node(struct iwn_softc *sc, struct iwn_node_info *node, int async)
   3393   1.1      ober {
   3394  1.33  christos 	/* Direct mapping. */
   3395  1.33  christos 	return iwn_cmd(sc, IWN_CMD_ADD_NODE, node, sizeof (*node), async);
   3396   1.1      ober }
   3397   1.1      ober 
   3398   1.1      ober static int
   3399  1.33  christos iwn_set_link_quality(struct iwn_softc *sc, struct ieee80211_node *ni)
   3400   1.1      ober {
   3401  1.33  christos 	struct iwn_node *wn = (void *)ni;
   3402  1.33  christos 	struct ieee80211_rateset *rs = &ni->ni_rates;
   3403  1.33  christos 	struct iwn_cmd_link_quality linkq;
   3404  1.33  christos 	const struct iwn_rate *rinfo;
   3405  1.33  christos 	uint8_t txant;
   3406  1.33  christos 	int i, txrate;
   3407  1.33  christos 
   3408  1.33  christos 	/* Use the first valid TX antenna. */
   3409  1.33  christos 	txant = IWN_LSB(sc->txantmsk);
   3410  1.33  christos 
   3411  1.33  christos 	memset(&linkq, 0, sizeof linkq);
   3412  1.33  christos 	linkq.id = wn->id;
   3413  1.33  christos 	linkq.antmsk_1stream = txant;
   3414  1.33  christos 	linkq.antmsk_2stream = IWN_ANT_A | IWN_ANT_B;
   3415  1.33  christos 	linkq.ampdu_max = 64;
   3416  1.33  christos 	linkq.ampdu_threshold = 3;
   3417  1.33  christos 	linkq.ampdu_limit = htole16(4000);	/* 4ms */
   3418   1.1      ober 
   3419  1.33  christos 	/* Start at highest available bit-rate. */
   3420  1.33  christos 	txrate = rs->rs_nrates - 1;
   3421  1.33  christos 	for (i = 0; i < IWN_MAX_TX_RETRIES; i++) {
   3422  1.33  christos 		rinfo = &iwn_rates[wn->ridx[txrate]];
   3423  1.33  christos 		linkq.retry[i].plcp = rinfo->plcp;
   3424  1.33  christos 		linkq.retry[i].rflags = rinfo->flags;
   3425  1.33  christos 		linkq.retry[i].rflags |= IWN_RFLAG_ANT(txant);
   3426  1.33  christos 		/* Next retry at immediate lower bit-rate. */
   3427  1.33  christos 		if (txrate > 0)
   3428  1.33  christos 			txrate--;
   3429   1.1      ober 	}
   3430  1.33  christos 	return iwn_cmd(sc, IWN_CMD_LINK_QUALITY, &linkq, sizeof linkq, 1);
   3431   1.1      ober }
   3432   1.1      ober 
   3433   1.1      ober /*
   3434  1.33  christos  * Broadcast node is used to send group-addressed and management frames.
   3435   1.1      ober  */
   3436   1.1      ober static int
   3437  1.33  christos iwn_add_broadcast_node(struct iwn_softc *sc, int async)
   3438   1.1      ober {
   3439  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   3440  1.33  christos 	struct iwn_node_info node;
   3441  1.33  christos 	struct iwn_cmd_link_quality linkq;
   3442  1.33  christos 	const struct iwn_rate *rinfo;
   3443  1.33  christos 	uint8_t txant;
   3444  1.33  christos 	int i, error;
   3445   1.1      ober 
   3446  1.33  christos 	memset(&node, 0, sizeof node);
   3447  1.33  christos 	IEEE80211_ADDR_COPY(node.macaddr, etherbroadcastaddr);
   3448  1.33  christos 	node.id = hal->broadcast_id;
   3449  1.33  christos 	DPRINTF(("adding broadcast node\n"));
   3450  1.33  christos 	if ((error = hal->add_node(sc, &node, async)) != 0)
   3451  1.33  christos 		return error;
   3452   1.1      ober 
   3453  1.33  christos 	/* Use the first valid TX antenna. */
   3454  1.33  christos 	txant = IWN_LSB(sc->txantmsk);
   3455   1.1      ober 
   3456  1.33  christos 	memset(&linkq, 0, sizeof linkq);
   3457  1.33  christos 	linkq.id = hal->broadcast_id;
   3458  1.33  christos 	linkq.antmsk_1stream = txant;
   3459  1.33  christos 	linkq.antmsk_2stream = IWN_ANT_A | IWN_ANT_B;
   3460  1.33  christos 	linkq.ampdu_max = 64;
   3461  1.33  christos 	linkq.ampdu_threshold = 3;
   3462  1.33  christos 	linkq.ampdu_limit = htole16(4000);	/* 4ms */
   3463  1.33  christos 
   3464  1.33  christos 	/* Use lowest mandatory bit-rate. */
   3465  1.33  christos 	rinfo = (sc->sc_ic.ic_curmode != IEEE80211_MODE_11A) ?
   3466  1.33  christos 	    &iwn_rates[IWN_RIDX_CCK1] : &iwn_rates[IWN_RIDX_OFDM6];
   3467  1.33  christos 	linkq.retry[0].plcp = rinfo->plcp;
   3468  1.33  christos 	linkq.retry[0].rflags = rinfo->flags;
   3469  1.33  christos 	linkq.retry[0].rflags |= IWN_RFLAG_ANT(txant);
   3470  1.33  christos 	/* Use same bit-rate for all TX retries. */
   3471  1.33  christos 	for (i = 1; i < IWN_MAX_TX_RETRIES; i++) {
   3472  1.33  christos 		linkq.retry[i].plcp = linkq.retry[0].plcp;
   3473  1.33  christos 		linkq.retry[i].rflags = linkq.retry[0].rflags;
   3474  1.33  christos 	}
   3475  1.33  christos 	return iwn_cmd(sc, IWN_CMD_LINK_QUALITY, &linkq, sizeof linkq, async);
   3476  1.33  christos }
   3477  1.33  christos 
   3478  1.33  christos #ifdef notyet
   3479  1.33  christos static void
   3480  1.33  christos iwn_updateedca(struct ieee80211com *ic)
   3481  1.33  christos {
   3482  1.33  christos #define IWN_EXP2(x)	((1 << (x)) - 1)	/* CWmin = 2^ECWmin - 1 */
   3483  1.33  christos 	struct iwn_softc *sc = ic->ic_softc;
   3484  1.33  christos 	struct iwn_edca_params cmd;
   3485  1.33  christos 	int aci;
   3486   1.1      ober 
   3487  1.33  christos 	memset(&cmd, 0, sizeof cmd);
   3488  1.33  christos 	cmd.flags = htole32(IWN_EDCA_UPDATE);
   3489  1.33  christos 	for (aci = 0; aci < EDCA_NUM_AC; aci++) {
   3490  1.33  christos 		const struct ieee80211_edca_ac_params *ac =
   3491  1.33  christos 		    &ic->ic_edca_ac[aci];
   3492  1.33  christos 		cmd.ac[aci].aifsn = ac->ac_aifsn;
   3493  1.33  christos 		cmd.ac[aci].cwmin = htole16(IWN_EXP2(ac->ac_ecwmin));
   3494  1.33  christos 		cmd.ac[aci].cwmax = htole16(IWN_EXP2(ac->ac_ecwmax));
   3495  1.33  christos 		cmd.ac[aci].txoplimit =
   3496  1.33  christos 		    htole16(IEEE80211_TXOP_TO_US(ac->ac_txoplimit));
   3497   1.1      ober 	}
   3498  1.33  christos 	(void)iwn_cmd(sc, IWN_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1);
   3499   1.1      ober #undef IWN_EXP2
   3500   1.1      ober }
   3501  1.33  christos #endif
   3502   1.1      ober 
   3503   1.1      ober static void
   3504   1.1      ober iwn_set_led(struct iwn_softc *sc, uint8_t which, uint8_t off, uint8_t on)
   3505   1.1      ober {
   3506   1.1      ober 	struct iwn_cmd_led led;
   3507   1.1      ober 
   3508  1.33  christos 	/* Clear microcode LED ownership. */
   3509  1.33  christos 	IWN_CLRBITS(sc, IWN_LED, IWN_LED_BSM_CTRL);
   3510  1.33  christos 
   3511   1.1      ober 	led.which = which;
   3512  1.33  christos 	led.unit = htole32(10000);	/* on/off in unit of 100ms */
   3513   1.1      ober 	led.off = off;
   3514   1.1      ober 	led.on = on;
   3515   1.1      ober 	(void)iwn_cmd(sc, IWN_CMD_SET_LED, &led, sizeof led, 1);
   3516   1.1      ober }
   3517   1.1      ober 
   3518   1.1      ober /*
   3519  1.33  christos  * Set the critical temperature at which the firmware will notify us.
   3520   1.1      ober  */
   3521   1.1      ober static int
   3522   1.1      ober iwn_set_critical_temp(struct iwn_softc *sc)
   3523   1.1      ober {
   3524   1.1      ober 	struct iwn_critical_temp crit;
   3525   1.1      ober 
   3526  1.33  christos 	IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_CTEMP_STOP_RF);
   3527   1.1      ober 
   3528   1.1      ober 	memset(&crit, 0, sizeof crit);
   3529  1.33  christos 	crit.tempR = htole32(sc->critical_temp);
   3530  1.33  christos 	DPRINTF(("setting critical temperature to %u\n", sc->critical_temp));
   3531   1.1      ober 	return iwn_cmd(sc, IWN_CMD_SET_CRITICAL_TEMP, &crit, sizeof crit, 0);
   3532   1.1      ober }
   3533   1.1      ober 
   3534  1.33  christos static int
   3535  1.33  christos iwn_set_timing(struct iwn_softc *sc, struct ieee80211_node *ni)
   3536   1.1      ober {
   3537  1.33  christos 	struct iwn_cmd_timing cmd;
   3538   1.1      ober 	uint64_t val, mod;
   3539   1.1      ober 
   3540  1.33  christos 	memset(&cmd, 0, sizeof cmd);
   3541  1.33  christos 	memcpy(&cmd.tstamp, ni->ni_tstamp.data, sizeof (uint64_t));
   3542  1.33  christos 	cmd.bintval = htole16(ni->ni_intval);
   3543  1.33  christos 	cmd.lintval = htole16(10);
   3544   1.1      ober 
   3545  1.33  christos 	/* Compute remaining time until next beacon. */
   3546   1.1      ober 	val = (uint64_t)ni->ni_intval * 1024;	/* msecs -> usecs */
   3547  1.33  christos 	mod = le64toh(cmd.tstamp) % val;
   3548  1.33  christos 	cmd.binitval = htole32((uint32_t)(val - mod));
   3549   1.1      ober 
   3550  1.33  christos 	DPRINTF(("timing bintval=%u, tstamp=%llu, init=%u\n",
   3551  1.33  christos 	    ni->ni_intval, (unsigned long long)le64toh(cmd.tstamp),
   3552  1.33  christos 	    (uint32_t)(val - mod)));
   3553   1.1      ober 
   3554  1.33  christos 	return iwn_cmd(sc, IWN_CMD_TIMING, &cmd, sizeof cmd, 1);
   3555   1.1      ober }
   3556   1.1      ober 
   3557  1.33  christos #if 0
   3558   1.1      ober static void
   3559  1.33  christos iwn4965_power_calibration(struct iwn_softc *sc, int temp)
   3560   1.1      ober {
   3561  1.33  christos 	/* Adjust TX power if need be (delta >= 3 degC.) */
   3562   1.1      ober 	DPRINTF(("temperature %d->%d\n", sc->temp, temp));
   3563  1.33  christos 	if (abs(temp - sc->temp) >= 3) {
   3564  1.33  christos 		/* Record temperature of last calibration. */
   3565  1.33  christos 		sc->temp = temp;
   3566  1.33  christos 		(void)iwn4965_set_txpower(sc, 1);
   3567   1.1      ober 	}
   3568   1.1      ober }
   3569  1.33  christos #endif
   3570   1.1      ober 
   3571   1.1      ober /*
   3572  1.33  christos  * Set TX power for current channel (each rate has its own power settings).
   3573   1.1      ober  * This function takes into account the regulatory information from EEPROM,
   3574   1.1      ober  * the current temperature and the current voltage.
   3575   1.1      ober  */
   3576   1.1      ober static int
   3577  1.33  christos iwn4965_set_txpower(struct iwn_softc *sc, int async)
   3578   1.1      ober {
   3579  1.33  christos /* Fixed-point arithmetic division using a n-bit fractional part. */
   3580  1.33  christos #define fdivround(a, b, n)	\
   3581   1.1      ober 	((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
   3582  1.33  christos /* Linear interpolation. */
   3583  1.33  christos #define interpolate(x, x1, y1, x2, y2, n)	\
   3584   1.1      ober 	((y1) + fdivround(((int)(x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
   3585   1.1      ober 
   3586   1.1      ober 	static const int tdiv[IWN_NATTEN_GROUPS] = { 9, 8, 8, 8, 6 };
   3587   1.1      ober 	struct ieee80211com *ic = &sc->sc_ic;
   3588   1.1      ober 	struct iwn_ucode_info *uc = &sc->ucode_info;
   3589  1.33  christos 	struct ieee80211_channel *ch;
   3590  1.33  christos 	struct iwn4965_cmd_txpower cmd;
   3591  1.33  christos 	struct iwn4965_eeprom_chan_samples *chans;
   3592   1.1      ober 	const uint8_t *rf_gain, *dsp_gain;
   3593   1.1      ober 	int32_t vdiff, tdiff;
   3594   1.1      ober 	int i, c, grp, maxpwr;
   3595  1.33  christos 	uint8_t chan;
   3596   1.1      ober 
   3597  1.33  christos 	/* Retrieve current channel from last RXON. */
   3598  1.33  christos 	chan = sc->rxon.chan;
   3599  1.33  christos 	DPRINTF(("setting TX power for channel %d\n", chan));
   3600  1.33  christos 	ch = &ic->ic_channels[chan];
   3601   1.1      ober 
   3602   1.1      ober 	memset(&cmd, 0, sizeof cmd);
   3603   1.1      ober 	cmd.band = IEEE80211_IS_CHAN_5GHZ(ch) ? 0 : 1;
   3604   1.1      ober 	cmd.chan = chan;
   3605   1.1      ober 
   3606   1.1      ober 	if (IEEE80211_IS_CHAN_5GHZ(ch)) {
   3607  1.33  christos 		maxpwr   = sc->maxpwr5GHz;
   3608  1.33  christos 		rf_gain  = iwn4965_rf_gain_5ghz;
   3609  1.33  christos 		dsp_gain = iwn4965_dsp_gain_5ghz;
   3610   1.1      ober 	} else {
   3611  1.33  christos 		maxpwr   = sc->maxpwr2GHz;
   3612  1.33  christos 		rf_gain  = iwn4965_rf_gain_2ghz;
   3613  1.33  christos 		dsp_gain = iwn4965_dsp_gain_2ghz;
   3614   1.1      ober 	}
   3615   1.1      ober 
   3616  1.33  christos 	/* Compute voltage compensation. */
   3617   1.1      ober 	vdiff = ((int32_t)le32toh(uc->volt) - sc->eeprom_voltage) / 7;
   3618   1.1      ober 	if (vdiff > 0)
   3619   1.1      ober 		vdiff *= 2;
   3620   1.1      ober 	if (abs(vdiff) > 2)
   3621   1.1      ober 		vdiff = 0;
   3622   1.1      ober 	DPRINTF(("voltage compensation=%d (UCODE=%d, EEPROM=%d)\n",
   3623  1.33  christos 	    vdiff, le32toh(uc->volt), sc->eeprom_voltage));
   3624   1.1      ober 
   3625  1.33  christos 	/* Get channel's attenuation group. */
   3626   1.1      ober 	if (chan <= 20)		/* 1-20 */
   3627   1.1      ober 		grp = 4;
   3628   1.1      ober 	else if (chan <= 43)	/* 34-43 */
   3629   1.1      ober 		grp = 0;
   3630   1.1      ober 	else if (chan <= 70)	/* 44-70 */
   3631   1.1      ober 		grp = 1;
   3632   1.1      ober 	else if (chan <= 124)	/* 71-124 */
   3633   1.1      ober 		grp = 2;
   3634   1.1      ober 	else			/* 125-200 */
   3635   1.1      ober 		grp = 3;
   3636   1.1      ober 	DPRINTF(("chan %d, attenuation group=%d\n", chan, grp));
   3637   1.1      ober 
   3638  1.33  christos 	/* Get channel's sub-band. */
   3639   1.1      ober 	for (i = 0; i < IWN_NBANDS; i++)
   3640   1.1      ober 		if (sc->bands[i].lo != 0 &&
   3641   1.1      ober 		    sc->bands[i].lo <= chan && chan <= sc->bands[i].hi)
   3642   1.1      ober 			break;
   3643   1.1      ober 	chans = sc->bands[i].chans;
   3644   1.1      ober 	DPRINTF(("chan %d sub-band=%d\n", chan, i));
   3645   1.1      ober 
   3646  1.33  christos 	for (c = 0; c < 2; c++) {
   3647   1.1      ober 		uint8_t power, gain, temp;
   3648   1.1      ober 		int maxchpwr, pwr, ridx, idx;
   3649   1.1      ober 
   3650   1.1      ober 		power = interpolate(chan,
   3651   1.1      ober 		    chans[0].num, chans[0].samples[c][1].power,
   3652   1.1      ober 		    chans[1].num, chans[1].samples[c][1].power, 1);
   3653   1.1      ober 		gain  = interpolate(chan,
   3654   1.1      ober 		    chans[0].num, chans[0].samples[c][1].gain,
   3655   1.1      ober 		    chans[1].num, chans[1].samples[c][1].gain, 1);
   3656   1.1      ober 		temp  = interpolate(chan,
   3657   1.1      ober 		    chans[0].num, chans[0].samples[c][1].temp,
   3658   1.1      ober 		    chans[1].num, chans[1].samples[c][1].temp, 1);
   3659  1.33  christos 		DPRINTF(("TX chain %d: power=%d gain=%d temp=%d\n",
   3660  1.33  christos 		    c, power, gain, temp));
   3661   1.1      ober 
   3662  1.33  christos 		/* Compute temperature compensation. */
   3663   1.1      ober 		tdiff = ((sc->temp - temp) * 2) / tdiv[grp];
   3664   1.1      ober 		DPRINTF(("temperature compensation=%d (current=%d, "
   3665  1.33  christos 		    "EEPROM=%d)\n", tdiff, sc->temp, temp));
   3666   1.1      ober 
   3667   1.1      ober 		for (ridx = 0; ridx <= IWN_RIDX_MAX; ridx++) {
   3668   1.1      ober 			maxchpwr = sc->maxpwr[chan] * 2;
   3669  1.33  christos 			if ((ridx / 8) & 1)
   3670  1.33  christos 				maxchpwr -= 6;	/* MIMO 2T: -3dB */
   3671   1.1      ober 
   3672  1.33  christos 			pwr = maxpwr;
   3673   1.1      ober 
   3674  1.33  christos 			/* Adjust TX power based on rate. */
   3675  1.33  christos 			if ((ridx % 8) == 5)
   3676  1.33  christos 				pwr -= 15;	/* OFDM48: -7.5dB */
   3677  1.33  christos 			else if ((ridx % 8) == 6)
   3678  1.33  christos 				pwr -= 17;	/* OFDM54: -8.5dB */
   3679  1.33  christos 			else if ((ridx % 8) == 7)
   3680  1.33  christos 				pwr -= 20;	/* OFDM60: -10dB */
   3681  1.33  christos 			else
   3682  1.33  christos 				pwr -= 10;	/* Others: -5dB */
   3683   1.1      ober 
   3684  1.33  christos 			/* Do not exceed channel's max TX power. */
   3685   1.1      ober 			if (pwr > maxchpwr)
   3686   1.1      ober 				pwr = maxchpwr;
   3687   1.1      ober 
   3688   1.1      ober 			idx = gain - (pwr - power) - tdiff - vdiff;
   3689   1.1      ober 			if ((ridx / 8) & 1)	/* MIMO */
   3690   1.1      ober 				idx += (int32_t)le32toh(uc->atten[grp][c]);
   3691   1.1      ober 
   3692   1.1      ober 			if (cmd.band == 0)
   3693   1.1      ober 				idx += 9;	/* 5GHz */
   3694   1.1      ober 			if (ridx == IWN_RIDX_MAX)
   3695   1.1      ober 				idx += 5;	/* CCK */
   3696   1.1      ober 
   3697  1.33  christos 			/* Make sure idx stays in a valid range. */
   3698   1.1      ober 			if (idx < 0)
   3699   1.1      ober 				idx = 0;
   3700  1.33  christos 			else if (idx > IWN4965_MAX_PWR_INDEX)
   3701  1.33  christos 				idx = IWN4965_MAX_PWR_INDEX;
   3702   1.1      ober 
   3703  1.33  christos 			DPRINTF(("TX chain %d, rate idx %d: power=%d\n",
   3704  1.33  christos 			    c, ridx, idx));
   3705   1.1      ober 			cmd.power[ridx].rf_gain[c] = rf_gain[idx];
   3706   1.1      ober 			cmd.power[ridx].dsp_gain[c] = dsp_gain[idx];
   3707   1.1      ober 		}
   3708   1.1      ober 	}
   3709   1.1      ober 
   3710  1.33  christos 	DPRINTF(("setting TX power for chan %d\n", chan));
   3711   1.1      ober 	return iwn_cmd(sc, IWN_CMD_TXPOWER, &cmd, sizeof cmd, async);
   3712   1.1      ober 
   3713   1.1      ober #undef interpolate
   3714   1.1      ober #undef fdivround
   3715   1.1      ober }
   3716   1.1      ober 
   3717  1.33  christos static int
   3718  1.33  christos iwn5000_set_txpower(struct iwn_softc *sc, int async)
   3719  1.33  christos {
   3720  1.33  christos 	struct iwn5000_cmd_txpower cmd;
   3721  1.33  christos 
   3722  1.33  christos 	/*
   3723  1.33  christos 	 * TX power calibration is handled automatically by the firmware
   3724  1.33  christos 	 * for 5000 Series.
   3725  1.33  christos 	 */
   3726  1.33  christos 	memset(&cmd, 0, sizeof cmd);
   3727  1.33  christos 	cmd.global_limit = 2 * IWN5000_TXPOWER_MAX_DBM;	/* 16 dBm */
   3728  1.33  christos 	cmd.flags = IWN5000_TXPOWER_NO_CLOSED;
   3729  1.33  christos 	cmd.srv_limit = IWN5000_TXPOWER_AUTO;
   3730  1.33  christos 	DPRINTF(("setting TX power\n"));
   3731  1.33  christos 	return iwn_cmd(sc, IWN_CMD_TXPOWER_DBM, &cmd, sizeof cmd, async);
   3732  1.33  christos }
   3733  1.33  christos 
   3734   1.1      ober /*
   3735  1.33  christos  * Retrieve the maximum RSSI (in dBm) among receivers.
   3736   1.1      ober  */
   3737   1.1      ober static int
   3738  1.33  christos iwn4965_get_rssi(const struct iwn_rx_stat *stat)
   3739   1.1      ober {
   3740  1.33  christos 	const struct iwn4965_rx_phystat *phy = (const void *)stat->phybuf;
   3741   1.1      ober 	uint8_t mask, agc;
   3742   1.1      ober 	int rssi;
   3743   1.1      ober 
   3744  1.33  christos 	mask = (le16toh(phy->antenna) >> 4) & 0x7;
   3745  1.33  christos 	agc  = (le16toh(phy->agc) >> 7) & 0x7f;
   3746   1.1      ober 
   3747   1.1      ober 	rssi = 0;
   3748  1.33  christos 	if (mask & IWN_ANT_A)
   3749  1.33  christos 		rssi = MAX(rssi, phy->rssi[0]);
   3750  1.33  christos 	if (mask & IWN_ANT_B)
   3751  1.33  christos 		rssi = MAX(rssi, phy->rssi[2]);
   3752  1.33  christos 	if (mask & IWN_ANT_C)
   3753  1.33  christos 		rssi = MAX(rssi, phy->rssi[4]);
   3754  1.33  christos 
   3755  1.33  christos 	return rssi - agc - IWN_RSSI_TO_DBM;
   3756  1.33  christos }
   3757  1.33  christos 
   3758  1.33  christos static int
   3759  1.33  christos iwn5000_get_rssi(const struct iwn_rx_stat *stat)
   3760  1.33  christos {
   3761  1.33  christos 	const struct iwn5000_rx_phystat *phy = (const void *)stat->phybuf;
   3762  1.33  christos 	uint8_t agc;
   3763  1.33  christos 	int rssi;
   3764  1.33  christos 
   3765  1.33  christos 	agc = (le32toh(phy->agc) >> 9) & 0x7f;
   3766  1.33  christos 
   3767  1.33  christos 	rssi = MAX(le16toh(phy->rssi[0]) & 0xff,
   3768  1.33  christos 		   le16toh(phy->rssi[1]) & 0xff);
   3769  1.33  christos 	rssi = MAX(le16toh(phy->rssi[2]) & 0xff, rssi);
   3770   1.1      ober 
   3771   1.1      ober 	return rssi - agc - IWN_RSSI_TO_DBM;
   3772   1.1      ober }
   3773   1.1      ober 
   3774   1.1      ober /*
   3775  1.33  christos  * Retrieve the average noise (in dBm) among receivers.
   3776   1.1      ober  */
   3777   1.1      ober static int
   3778   1.1      ober iwn_get_noise(const struct iwn_rx_general_stats *stats)
   3779   1.1      ober {
   3780   1.1      ober 	int i, total, nbant, noise;
   3781   1.1      ober 
   3782   1.1      ober 	total = nbant = 0;
   3783   1.1      ober 	for (i = 0; i < 3; i++) {
   3784   1.1      ober 		if ((noise = le32toh(stats->noise[i]) & 0xff) == 0)
   3785   1.1      ober 			continue;
   3786   1.1      ober 		total += noise;
   3787   1.1      ober 		nbant++;
   3788   1.1      ober 	}
   3789  1.33  christos 	/* There should be at least one antenna but check anyway. */
   3790   1.1      ober 	return (nbant == 0) ? -127 : (total / nbant) - 107;
   3791   1.1      ober }
   3792   1.1      ober 
   3793   1.1      ober /*
   3794  1.33  christos  * Compute temperature (in degC) from last received statistics.
   3795   1.1      ober  */
   3796   1.1      ober static int
   3797  1.33  christos iwn4965_get_temperature(struct iwn_softc *sc)
   3798   1.1      ober {
   3799   1.1      ober 	struct iwn_ucode_info *uc = &sc->ucode_info;
   3800   1.1      ober 	int32_t r1, r2, r3, r4, temp;
   3801   1.1      ober 
   3802   1.1      ober 	r1 = le32toh(uc->temp[0].chan20MHz);
   3803   1.1      ober 	r2 = le32toh(uc->temp[1].chan20MHz);
   3804   1.1      ober 	r3 = le32toh(uc->temp[2].chan20MHz);
   3805   1.1      ober 	r4 = le32toh(sc->rawtemp);
   3806   1.1      ober 
   3807  1.33  christos 	if (r1 == r3)	/* Prevents division by 0 (should not happen.) */
   3808   1.1      ober 		return 0;
   3809   1.1      ober 
   3810  1.33  christos 	/* Sign-extend 23-bit R4 value to 32-bit. */
   3811   1.1      ober 	r4 = (r4 << 8) >> 8;
   3812  1.33  christos 	/* Compute temperature in Kelvin. */
   3813   1.1      ober 	temp = (259 * (r4 - r2)) / (r3 - r1);
   3814   1.1      ober 	temp = (temp * 97) / 100 + 8;
   3815   1.1      ober 
   3816   1.1      ober 	DPRINTF(("temperature %dK/%dC\n", temp, IWN_KTOC(temp)));
   3817   1.1      ober 	return IWN_KTOC(temp);
   3818   1.1      ober }
   3819   1.1      ober 
   3820  1.33  christos static int
   3821  1.33  christos iwn5000_get_temperature(struct iwn_softc *sc)
   3822  1.33  christos {
   3823  1.33  christos 	/*
   3824  1.33  christos 	 * Temperature is not used by the driver for 5000 Series because
   3825  1.33  christos 	 * TX power calibration is handled by firmware.  We export it to
   3826  1.33  christos 	 * users through the sensor framework though.
   3827  1.33  christos 	 */
   3828  1.33  christos 	return le32toh(sc->rawtemp);
   3829  1.33  christos }
   3830  1.33  christos 
   3831   1.1      ober /*
   3832   1.1      ober  * Initialize sensitivity calibration state machine.
   3833   1.1      ober  */
   3834   1.1      ober static int
   3835   1.1      ober iwn_init_sensitivity(struct iwn_softc *sc)
   3836   1.1      ober {
   3837  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   3838   1.1      ober 	struct iwn_calib_state *calib = &sc->calib;
   3839  1.33  christos 	uint32_t flags;
   3840   1.1      ober 	int error;
   3841   1.1      ober 
   3842  1.33  christos 	/* Reset calibration state machine. */
   3843   1.1      ober 	memset(calib, 0, sizeof (*calib));
   3844   1.1      ober 	calib->state = IWN_CALIB_STATE_INIT;
   3845   1.1      ober 	calib->cck_state = IWN_CCK_STATE_HIFA;
   3846  1.33  christos 	/* Set initial correlation values. */
   3847  1.33  christos 	calib->ofdm_x1     = hal->limits->min_ofdm_x1;
   3848  1.33  christos 	calib->ofdm_mrc_x1 = hal->limits->min_ofdm_mrc_x1;
   3849  1.33  christos 	calib->ofdm_x4     = 90;
   3850  1.33  christos 	calib->ofdm_mrc_x4 = hal->limits->min_ofdm_mrc_x4;
   3851  1.33  christos 	calib->cck_x4      = 125;
   3852  1.33  christos 	calib->cck_mrc_x4  = hal->limits->min_cck_mrc_x4;
   3853  1.33  christos 	calib->energy_cck  = hal->limits->energy_cck;
   3854   1.1      ober 
   3855  1.33  christos 	/* Write initial sensitivity. */
   3856   1.1      ober 	if ((error = iwn_send_sensitivity(sc)) != 0)
   3857   1.1      ober 		return error;
   3858   1.1      ober 
   3859  1.33  christos 	/* Write initial gains. */
   3860  1.33  christos 	if ((error = hal->init_gains(sc)) != 0)
   3861  1.33  christos 		return error;
   3862  1.33  christos 
   3863  1.33  christos 	/* Request statistics at each beacon interval. */
   3864  1.33  christos 	flags = 0;
   3865  1.33  christos 	DPRINTF(("sending request for statistics\n"));
   3866  1.33  christos 	return iwn_cmd(sc, IWN_CMD_GET_STATISTICS, &flags, sizeof flags, 1);
   3867   1.1      ober }
   3868   1.1      ober 
   3869   1.1      ober /*
   3870   1.1      ober  * Collect noise and RSSI statistics for the first 20 beacons received
   3871   1.1      ober  * after association and use them to determine connected antennas and
   3872  1.33  christos  * to set differential gains.
   3873   1.1      ober  */
   3874   1.1      ober static void
   3875  1.33  christos iwn_collect_noise(struct iwn_softc *sc,
   3876   1.1      ober     const struct iwn_rx_general_stats *stats)
   3877   1.1      ober {
   3878  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   3879   1.1      ober 	struct iwn_calib_state *calib = &sc->calib;
   3880  1.33  christos 	uint32_t val;
   3881  1.33  christos 	int i;
   3882   1.1      ober 
   3883  1.33  christos 	/* Accumulate RSSI and noise for all 3 antennas. */
   3884   1.1      ober 	for (i = 0; i < 3; i++) {
   3885   1.1      ober 		calib->rssi[i] += le32toh(stats->rssi[i]) & 0xff;
   3886   1.1      ober 		calib->noise[i] += le32toh(stats->noise[i]) & 0xff;
   3887   1.1      ober 	}
   3888  1.33  christos 	/* NB: We update differential gains only once after 20 beacons. */
   3889   1.1      ober 	if (++calib->nbeacons < 20)
   3890   1.1      ober 		return;
   3891   1.1      ober 
   3892  1.33  christos 	/* Determine highest average RSSI. */
   3893  1.33  christos 	val = MAX(calib->rssi[0], calib->rssi[1]);
   3894  1.33  christos 	val = MAX(calib->rssi[2], val);
   3895   1.1      ober 
   3896  1.33  christos 	/* Determine which antennas are connected. */
   3897   1.1      ober 	sc->antmsk = 0;
   3898   1.1      ober 	for (i = 0; i < 3; i++)
   3899   1.1      ober 		if (val - calib->rssi[i] <= 15 * 20)
   3900   1.1      ober 			sc->antmsk |= 1 << i;
   3901  1.33  christos 	/* If none of the TX antennas are connected, keep at least one. */
   3902  1.33  christos 	if ((sc->antmsk & sc->txantmsk) == 0)
   3903  1.33  christos 		sc->antmsk |= IWN_LSB(sc->txantmsk);
   3904  1.33  christos 
   3905  1.33  christos 	(void)hal->set_gains(sc);
   3906  1.33  christos 	calib->state = IWN_CALIB_STATE_RUN;
   3907  1.33  christos 
   3908  1.33  christos #ifdef notyet
   3909  1.33  christos 	/* XXX Disable RX chains with no antennas connected. */
   3910  1.33  christos 	sc->rxon.rxchain = htole16(IWN_RXCHAIN_SEL(sc->antmsk));
   3911  1.33  christos 	(void)iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->rxon, hal->rxonsz, 1);
   3912  1.33  christos 
   3913  1.33  christos 	/* Enable power-saving mode if requested by user. */
   3914  1.33  christos 	if (sc->sc_ic.ic_flags & IEEE80211_F_PMGTON)
   3915  1.33  christos 		(void)iwn_set_pslevel(sc, 0, 3, 1);
   3916  1.33  christos #endif
   3917  1.33  christos }
   3918  1.33  christos 
   3919  1.33  christos static int
   3920  1.33  christos iwn4965_init_gains(struct iwn_softc *sc)
   3921  1.33  christos {
   3922  1.33  christos 	struct iwn_phy_calib_gain cmd;
   3923  1.33  christos 
   3924  1.33  christos 	memset(&cmd, 0, sizeof cmd);
   3925  1.33  christos 	cmd.code = IWN4965_PHY_CALIB_DIFF_GAIN;
   3926  1.33  christos 	/* Differential gains initially set to 0 for all 3 antennas. */
   3927  1.33  christos 	DPRINTF(("setting initial differential gains\n"));
   3928  1.33  christos 	return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1);
   3929  1.33  christos }
   3930  1.33  christos 
   3931  1.33  christos static int
   3932  1.33  christos iwn5000_init_gains(struct iwn_softc *sc)
   3933  1.33  christos {
   3934  1.33  christos 	struct iwn_phy_calib cmd;
   3935  1.33  christos 
   3936  1.33  christos 	if (sc->hw_type == IWN_HW_REV_TYPE_6000 ||
   3937  1.33  christos 	    sc->hw_type == IWN_HW_REV_TYPE_6050)
   3938  1.33  christos 		return 0;
   3939  1.33  christos 
   3940  1.33  christos 	memset(&cmd, 0, sizeof cmd);
   3941  1.33  christos 	cmd.code = IWN5000_PHY_CALIB_RESET_NOISE_GAIN;
   3942  1.33  christos 	cmd.ngroups = 1;
   3943  1.33  christos 	cmd.isvalid = 1;
   3944  1.33  christos 	DPRINTF(("setting initial differential gains\n"));
   3945  1.33  christos 	return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1);
   3946  1.33  christos }
   3947  1.33  christos 
   3948  1.33  christos static int
   3949  1.33  christos iwn4965_set_gains(struct iwn_softc *sc)
   3950  1.33  christos {
   3951  1.33  christos 	struct iwn_calib_state *calib = &sc->calib;
   3952  1.33  christos 	struct iwn_phy_calib_gain cmd;
   3953  1.33  christos 	int i, delta, noise;
   3954   1.1      ober 
   3955  1.33  christos 	/* Get minimal noise among connected antennas. */
   3956  1.33  christos 	noise = INT_MAX;	/* NB: There's at least one antenna. */
   3957   1.1      ober 	for (i = 0; i < 3; i++)
   3958   1.1      ober 		if (sc->antmsk & (1 << i))
   3959  1.33  christos 			noise = MIN(calib->noise[i], noise);
   3960   1.1      ober 
   3961   1.1      ober 	memset(&cmd, 0, sizeof cmd);
   3962  1.33  christos 	cmd.code = IWN4965_PHY_CALIB_DIFF_GAIN;
   3963  1.33  christos 	/* Set differential gains for connected antennas. */
   3964   1.1      ober 	for (i = 0; i < 3; i++) {
   3965   1.1      ober 		if (sc->antmsk & (1 << i)) {
   3966  1.33  christos 			/* Compute attenuation (in unit of 1.5dB). */
   3967  1.33  christos 			delta = (noise - (int32_t)calib->noise[i]) / 30;
   3968  1.33  christos 			/* NB: delta <= 0 */
   3969  1.33  christos 			/* Limit to [-4.5dB,0]. */
   3970  1.33  christos 			cmd.gain[i] = MIN(abs(delta), 3);
   3971  1.33  christos 			if (delta < 0)
   3972  1.33  christos 				cmd.gain[i] |= 1 << 2;	/* sign bit */
   3973   1.1      ober 		}
   3974   1.1      ober 	}
   3975   1.1      ober 	DPRINTF(("setting differential gains Ant A/B/C: %x/%x/%x (%x)\n",
   3976  1.33  christos 	    cmd.gain[0], cmd.gain[1], cmd.gain[2], sc->antmsk));
   3977  1.33  christos 	return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1);
   3978  1.33  christos }
   3979  1.33  christos 
   3980  1.33  christos static int
   3981  1.33  christos iwn5000_set_gains(struct iwn_softc *sc)
   3982  1.33  christos {
   3983  1.33  christos 	struct iwn_calib_state *calib = &sc->calib;
   3984  1.33  christos 	struct iwn_phy_calib_gain cmd;
   3985  1.33  christos 	int i, delta;
   3986  1.33  christos 
   3987  1.33  christos 	if (sc->hw_type == IWN_HW_REV_TYPE_6000 ||
   3988  1.33  christos 	    sc->hw_type == IWN_HW_REV_TYPE_6050)
   3989  1.33  christos 		return 0;
   3990  1.33  christos 
   3991  1.33  christos 	memset(&cmd, 0, sizeof cmd);
   3992  1.33  christos 	cmd.code = IWN5000_PHY_CALIB_NOISE_GAIN;
   3993  1.33  christos 	cmd.ngroups = 1;
   3994  1.33  christos 	cmd.isvalid = 1;
   3995  1.33  christos 	/* Set differential gains for antennas B and C. */
   3996  1.33  christos 	for (i = 1; i < 3; i++) {
   3997  1.33  christos 		if (sc->antmsk & (1 << i)) {
   3998  1.33  christos 			/* The delta is relative to antenna A. */
   3999  1.33  christos 			delta = ((int32_t)calib->noise[0] -
   4000  1.33  christos 			    (int32_t)calib->noise[i]) / 30;
   4001  1.33  christos 			/* Limit to [-4.5dB,+4.5dB]. */
   4002  1.33  christos 			cmd.gain[i - 1] = MIN(abs(delta), 3);
   4003  1.33  christos 			if (delta < 0)
   4004  1.33  christos 				cmd.gain[i - 1] |= 1 << 2;	/* sign bit */
   4005  1.33  christos 		}
   4006  1.33  christos 	}
   4007  1.33  christos 	DPRINTF(("setting differential gains Ant B/C: %x/%x (%x)\n",
   4008  1.33  christos 	    cmd.gain[0], cmd.gain[1], sc->antmsk));
   4009  1.33  christos 	return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1);
   4010   1.1      ober }
   4011   1.1      ober 
   4012   1.1      ober /*
   4013  1.33  christos  * Tune RF RX sensitivity based on the number of false alarms detected
   4014   1.1      ober  * during the last beacon period.
   4015   1.1      ober  */
   4016   1.1      ober static void
   4017   1.1      ober iwn_tune_sensitivity(struct iwn_softc *sc, const struct iwn_rx_stats *stats)
   4018   1.1      ober {
   4019  1.33  christos #define inc(val, inc, max)			\
   4020  1.33  christos 	if ((val) < (max)) {			\
   4021  1.33  christos 		if ((val) < (max) - (inc))	\
   4022  1.33  christos 			(val) += (inc);		\
   4023  1.33  christos 		else				\
   4024  1.33  christos 			(val) = (max);		\
   4025  1.33  christos 		needs_update = 1;		\
   4026  1.33  christos 	}
   4027  1.33  christos #define dec(val, dec, min)			\
   4028  1.33  christos 	if ((val) > (min)) {			\
   4029  1.33  christos 		if ((val) > (min) + (dec))	\
   4030  1.33  christos 			(val) -= (dec);		\
   4031  1.33  christos 		else				\
   4032  1.33  christos 			(val) = (min);		\
   4033  1.33  christos 		needs_update = 1;		\
   4034   1.1      ober 	}
   4035   1.1      ober 
   4036  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   4037  1.33  christos 	const struct iwn_sensitivity_limits *limits = hal->limits;
   4038   1.1      ober 	struct iwn_calib_state *calib = &sc->calib;
   4039   1.1      ober 	uint32_t val, rxena, fa;
   4040   1.1      ober 	uint32_t energy[3], energy_min;
   4041   1.1      ober 	uint8_t noise[3], noise_ref;
   4042   1.1      ober 	int i, needs_update = 0;
   4043   1.1      ober 
   4044  1.33  christos 	/* Check that we've been enabled long enough. */
   4045   1.1      ober 	if ((rxena = le32toh(stats->general.load)) == 0)
   4046   1.1      ober 		return;
   4047   1.1      ober 
   4048  1.33  christos 	/* Compute number of false alarms since last call for OFDM. */
   4049   1.1      ober 	fa  = le32toh(stats->ofdm.bad_plcp) - calib->bad_plcp_ofdm;
   4050   1.1      ober 	fa += le32toh(stats->ofdm.fa) - calib->fa_ofdm;
   4051   1.1      ober 	fa *= 200 * 1024;	/* 200TU */
   4052   1.1      ober 
   4053  1.33  christos 	/* Save counters values for next call. */
   4054   1.1      ober 	calib->bad_plcp_ofdm = le32toh(stats->ofdm.bad_plcp);
   4055   1.1      ober 	calib->fa_ofdm = le32toh(stats->ofdm.fa);
   4056   1.1      ober 
   4057   1.1      ober 	if (fa > 50 * rxena) {
   4058  1.33  christos 		/* High false alarm count, decrease sensitivity. */
   4059   1.1      ober 		DPRINTFN(2, ("OFDM high false alarm count: %u\n", fa));
   4060  1.33  christos 		inc(calib->ofdm_x1,     1, limits->max_ofdm_x1);
   4061  1.33  christos 		inc(calib->ofdm_mrc_x1, 1, limits->max_ofdm_mrc_x1);
   4062  1.33  christos 		inc(calib->ofdm_x4,     1, limits->max_ofdm_x4);
   4063  1.33  christos 		inc(calib->ofdm_mrc_x4, 1, limits->max_ofdm_mrc_x4);
   4064   1.1      ober 
   4065   1.1      ober 	} else if (fa < 5 * rxena) {
   4066  1.33  christos 		/* Low false alarm count, increase sensitivity. */
   4067   1.1      ober 		DPRINTFN(2, ("OFDM low false alarm count: %u\n", fa));
   4068  1.33  christos 		dec(calib->ofdm_x1,     1, limits->min_ofdm_x1);
   4069  1.33  christos 		dec(calib->ofdm_mrc_x1, 1, limits->min_ofdm_mrc_x1);
   4070  1.33  christos 		dec(calib->ofdm_x4,     1, limits->min_ofdm_x4);
   4071  1.33  christos 		dec(calib->ofdm_mrc_x4, 1, limits->min_ofdm_mrc_x4);
   4072   1.1      ober 	}
   4073   1.1      ober 
   4074  1.33  christos 	/* Compute maximum noise among 3 receivers. */
   4075   1.1      ober 	for (i = 0; i < 3; i++)
   4076   1.1      ober 		noise[i] = (le32toh(stats->general.noise[i]) >> 8) & 0xff;
   4077  1.33  christos 	val = MAX(noise[0], noise[1]);
   4078  1.33  christos 	val = MAX(noise[2], val);
   4079  1.33  christos 	/* Insert it into our samples table. */
   4080   1.1      ober 	calib->noise_samples[calib->cur_noise_sample] = val;
   4081   1.1      ober 	calib->cur_noise_sample = (calib->cur_noise_sample + 1) % 20;
   4082   1.1      ober 
   4083  1.33  christos 	/* Compute maximum noise among last 20 samples. */
   4084   1.1      ober 	noise_ref = calib->noise_samples[0];
   4085   1.1      ober 	for (i = 1; i < 20; i++)
   4086  1.33  christos 		noise_ref = MAX(noise_ref, calib->noise_samples[i]);
   4087   1.1      ober 
   4088  1.33  christos 	/* Compute maximum energy among 3 receivers. */
   4089   1.1      ober 	for (i = 0; i < 3; i++)
   4090   1.1      ober 		energy[i] = le32toh(stats->general.energy[i]);
   4091  1.33  christos 	val = MIN(energy[0], energy[1]);
   4092  1.33  christos 	val = MIN(energy[2], val);
   4093  1.33  christos 	/* Insert it into our samples table. */
   4094   1.1      ober 	calib->energy_samples[calib->cur_energy_sample] = val;
   4095   1.1      ober 	calib->cur_energy_sample = (calib->cur_energy_sample + 1) % 10;
   4096   1.1      ober 
   4097  1.33  christos 	/* Compute minimum energy among last 10 samples. */
   4098   1.1      ober 	energy_min = calib->energy_samples[0];
   4099   1.1      ober 	for (i = 1; i < 10; i++)
   4100  1.33  christos 		energy_min = MAX(energy_min, calib->energy_samples[i]);
   4101   1.1      ober 	energy_min += 6;
   4102   1.1      ober 
   4103  1.33  christos 	/* Compute number of false alarms since last call for CCK. */
   4104   1.1      ober 	fa  = le32toh(stats->cck.bad_plcp) - calib->bad_plcp_cck;
   4105   1.1      ober 	fa += le32toh(stats->cck.fa) - calib->fa_cck;
   4106   1.1      ober 	fa *= 200 * 1024;	/* 200TU */
   4107   1.1      ober 
   4108  1.33  christos 	/* Save counters values for next call. */
   4109   1.1      ober 	calib->bad_plcp_cck = le32toh(stats->cck.bad_plcp);
   4110   1.1      ober 	calib->fa_cck = le32toh(stats->cck.fa);
   4111   1.1      ober 
   4112   1.1      ober 	if (fa > 50 * rxena) {
   4113  1.33  christos 		/* High false alarm count, decrease sensitivity. */
   4114   1.1      ober 		DPRINTFN(2, ("CCK high false alarm count: %u\n", fa));
   4115   1.1      ober 		calib->cck_state = IWN_CCK_STATE_HIFA;
   4116   1.1      ober 		calib->low_fa = 0;
   4117   1.1      ober 
   4118  1.33  christos 		if (calib->cck_x4 > 160) {
   4119   1.1      ober 			calib->noise_ref = noise_ref;
   4120   1.1      ober 			if (calib->energy_cck > 2)
   4121  1.33  christos 				dec(calib->energy_cck, 2, energy_min);
   4122   1.1      ober 		}
   4123  1.33  christos 		if (calib->cck_x4 < 160) {
   4124  1.33  christos 			calib->cck_x4 = 161;
   4125   1.1      ober 			needs_update = 1;
   4126   1.1      ober 		} else
   4127  1.33  christos 			inc(calib->cck_x4, 3, limits->max_cck_x4);
   4128   1.1      ober 
   4129  1.33  christos 		inc(calib->cck_mrc_x4, 3, limits->max_cck_mrc_x4);
   4130   1.1      ober 
   4131   1.1      ober 	} else if (fa < 5 * rxena) {
   4132  1.33  christos 		/* Low false alarm count, increase sensitivity. */
   4133   1.1      ober 		DPRINTFN(2, ("CCK low false alarm count: %u\n", fa));
   4134   1.1      ober 		calib->cck_state = IWN_CCK_STATE_LOFA;
   4135   1.1      ober 		calib->low_fa++;
   4136   1.1      ober 
   4137  1.33  christos 		if (calib->cck_state != IWN_CCK_STATE_INIT &&
   4138  1.33  christos 		    (((int32_t)calib->noise_ref - (int32_t)noise_ref) > 2 ||
   4139  1.33  christos 		     calib->low_fa > 100)) {
   4140  1.33  christos 			inc(calib->energy_cck, 2, limits->min_energy_cck);
   4141  1.33  christos 			dec(calib->cck_x4,     3, limits->min_cck_x4);
   4142  1.33  christos 			dec(calib->cck_mrc_x4, 3, limits->min_cck_mrc_x4);
   4143   1.1      ober 		}
   4144   1.1      ober 	} else {
   4145  1.33  christos 		/* Not worth to increase or decrease sensitivity. */
   4146   1.1      ober 		DPRINTFN(2, ("CCK normal false alarm count: %u\n", fa));
   4147   1.1      ober 		calib->low_fa = 0;
   4148   1.1      ober 		calib->noise_ref = noise_ref;
   4149   1.1      ober 
   4150   1.1      ober 		if (calib->cck_state == IWN_CCK_STATE_HIFA) {
   4151  1.33  christos 			/* Previous interval had many false alarms. */
   4152  1.33  christos 			dec(calib->energy_cck, 8, energy_min);
   4153   1.1      ober 		}
   4154   1.1      ober 		calib->cck_state = IWN_CCK_STATE_INIT;
   4155   1.1      ober 	}
   4156   1.1      ober 
   4157   1.1      ober 	if (needs_update)
   4158   1.1      ober 		(void)iwn_send_sensitivity(sc);
   4159  1.33  christos #undef dec
   4160  1.33  christos #undef inc
   4161   1.1      ober }
   4162   1.1      ober 
   4163   1.1      ober static int
   4164   1.1      ober iwn_send_sensitivity(struct iwn_softc *sc)
   4165   1.1      ober {
   4166  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   4167   1.1      ober 	struct iwn_calib_state *calib = &sc->calib;
   4168   1.1      ober 	struct iwn_sensitivity_cmd cmd;
   4169   1.1      ober 
   4170   1.1      ober 	memset(&cmd, 0, sizeof cmd);
   4171   1.1      ober 	cmd.which = IWN_SENSITIVITY_WORKTBL;
   4172  1.33  christos 	/* OFDM modulation. */
   4173  1.33  christos 	cmd.corr_ofdm_x1     = htole16(calib->ofdm_x1);
   4174  1.33  christos 	cmd.corr_ofdm_mrc_x1 = htole16(calib->ofdm_mrc_x1);
   4175  1.33  christos 	cmd.corr_ofdm_x4     = htole16(calib->ofdm_x4);
   4176  1.33  christos 	cmd.corr_ofdm_mrc_x4 = htole16(calib->ofdm_mrc_x4);
   4177  1.33  christos 	cmd.energy_ofdm      = htole16(hal->limits->energy_ofdm);
   4178  1.33  christos 	cmd.energy_ofdm_th   = htole16(62);
   4179  1.33  christos 	/* CCK modulation. */
   4180  1.33  christos 	cmd.corr_cck_x4      = htole16(calib->cck_x4);
   4181  1.33  christos 	cmd.corr_cck_mrc_x4  = htole16(calib->cck_mrc_x4);
   4182  1.33  christos 	cmd.energy_cck       = htole16(calib->energy_cck);
   4183  1.33  christos 	/* Barker modulation: use default values. */
   4184  1.33  christos 	cmd.corr_barker      = htole16(190);
   4185  1.33  christos 	cmd.corr_barker_mrc  = htole16(390);
   4186  1.33  christos 
   4187  1.33  christos 	DPRINTFN(2, ("setting sensitivity %d/%d/%d/%d/%d/%d/%d\n",
   4188  1.33  christos 	    calib->ofdm_x1, calib->ofdm_mrc_x1, calib->ofdm_x4,
   4189  1.33  christos 	    calib->ofdm_mrc_x4, calib->cck_x4, calib->cck_mrc_x4,
   4190  1.33  christos 	    calib->energy_cck));
   4191  1.33  christos 	return iwn_cmd(sc, IWN_CMD_SET_SENSITIVITY, &cmd, sizeof cmd, 1);
   4192  1.33  christos }
   4193  1.33  christos 
   4194  1.33  christos #if 0
   4195  1.33  christos /*
   4196  1.33  christos  * Set STA mode power saving level (between 0 and 5).
   4197  1.33  christos  * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving.
   4198  1.33  christos  */
   4199  1.33  christos static int
   4200  1.33  christos iwn_set_pslevel(struct iwn_softc *sc, int dtim, int level, int async)
   4201  1.33  christos {
   4202  1.33  christos 	struct iwn_pmgt_cmd cmd;
   4203  1.33  christos 	const struct iwn_pmgt *pmgt;
   4204  1.33  christos 	uint32_t umax, skip_dtim;
   4205  1.33  christos 	pcireg_t reg;
   4206  1.33  christos 	int i;
   4207  1.33  christos 
   4208  1.33  christos 	/* Select which PS parameters to use. */
   4209  1.33  christos 	if (dtim <= 2)
   4210  1.33  christos 		pmgt = &iwn_pmgt[0][level];
   4211  1.33  christos 	else if (dtim <= 10)
   4212  1.33  christos 		pmgt = &iwn_pmgt[1][level];
   4213  1.33  christos 	else
   4214  1.33  christos 		pmgt = &iwn_pmgt[2][level];
   4215  1.33  christos 
   4216  1.33  christos 	memset(&cmd, 0, sizeof cmd);
   4217  1.33  christos 	if (level != 0)	/* not CAM */
   4218  1.33  christos 		cmd.flags |= htole16(IWN_PS_ALLOW_SLEEP);
   4219  1.33  christos 	if (level == 5)
   4220  1.33  christos 		cmd.flags |= htole16(IWN_PS_FAST_PD);
   4221  1.33  christos 	/* Retrieve PCIe Active State Power Management (ASPM). */
   4222  1.33  christos 	reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
   4223  1.33  christos 	    sc->sc_cap_off + PCI_PCIE_LCSR);
   4224  1.33  christos 	if (!(reg & PCI_PCIE_LCSR_ASPM_L0S))	/* L0s Entry disabled. */
   4225  1.33  christos 		cmd.flags |= htole16(IWN_PS_PCI_PMGT);
   4226  1.33  christos 	cmd.rxtimeout = htole32(pmgt->rxtimeout * 1024);
   4227  1.33  christos 	cmd.txtimeout = htole32(pmgt->txtimeout * 1024);
   4228  1.33  christos 
   4229  1.33  christos 	if (dtim == 0) {
   4230  1.33  christos 		dtim = 1;
   4231  1.33  christos 		skip_dtim = 0;
   4232  1.33  christos 	} else
   4233  1.33  christos 		skip_dtim = pmgt->skip_dtim;
   4234  1.33  christos 	if (skip_dtim != 0) {
   4235  1.33  christos 		cmd.flags |= htole16(IWN_PS_SLEEP_OVER_DTIM);
   4236  1.33  christos 		umax = pmgt->intval[4];
   4237  1.33  christos 		if (umax == (uint32_t)-1)
   4238  1.33  christos 			umax = dtim * (skip_dtim + 1);
   4239  1.33  christos 		else if (umax > dtim)
   4240  1.33  christos 			umax = (umax / dtim) * dtim;
   4241  1.33  christos 	} else
   4242  1.33  christos 		umax = dtim;
   4243  1.33  christos 	for (i = 0; i < 5; i++)
   4244  1.33  christos 		cmd.intval[i] = htole32(MIN(umax, pmgt->intval[i]));
   4245   1.1      ober 
   4246  1.33  christos 	DPRINTF(("setting power saving level to %d\n", level));
   4247  1.33  christos 	return iwn_cmd(sc, IWN_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async);
   4248   1.1      ober }
   4249  1.33  christos #endif
   4250   1.1      ober 
   4251   1.1      ober static int
   4252  1.33  christos iwn_config(struct iwn_softc *sc)
   4253  1.11     blymn {
   4254  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   4255  1.33  christos 	struct ieee80211com *ic = &sc->sc_ic;
   4256  1.33  christos 	struct ifnet *ifp = ic->ic_ifp;
   4257  1.33  christos 	struct iwn_bluetooth bluetooth;
   4258  1.33  christos 	uint16_t rxchain;
   4259  1.11     blymn 	int error;
   4260  1.33  christos 	struct iwn_pmgt_cmd power;
   4261  1.11     blymn 
   4262  1.11     blymn 
   4263  1.33  christos #if 0
   4264  1.33  christos 	/* Set power saving level to CAM during initialization. */
   4265  1.33  christos 	if ((error = iwn_set_pslevel(sc, 0, 0, 0)) != 0) {
   4266  1.33  christos 		aprint_error_dev(sc->sc_dev,
   4267  1.33  christos 		    "could not set power saving level\n");
   4268  1.33  christos 		return error;
   4269  1.11     blymn 	}
   4270  1.33  christos #else
   4271  1.33  christos 	/* set power mode */
   4272  1.33  christos 	memset(&power, 0, sizeof power);
   4273  1.33  christos 	power.flags = htole16(/*IWN_POWER_CAM*/0 | 0x8);
   4274  1.33  christos 	DPRINTF(("setting power mode\n"));
   4275  1.33  christos 	error = iwn_cmd(sc, IWN_CMD_SET_POWER_MODE, &power, sizeof power, 0);
   4276  1.11     blymn 	if (error != 0) {
   4277  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not set power mode\n");
   4278  1.11     blymn 		return error;
   4279  1.11     blymn 	}
   4280  1.33  christos #endif
   4281  1.33  christos 
   4282  1.33  christos 	/* Configure bluetooth coexistence. */
   4283  1.33  christos 	memset(&bluetooth, 0, sizeof bluetooth);
   4284  1.33  christos 	bluetooth.flags = 3;
   4285  1.33  christos 	bluetooth.lead = 0xaa;
   4286  1.33  christos 	bluetooth.kill = 1;
   4287  1.33  christos 	DPRINTF(("configuring bluetooth coexistence\n"));
   4288  1.33  christos 	error = iwn_cmd(sc, IWN_CMD_BT_COEX, &bluetooth, sizeof bluetooth, 0);
   4289  1.33  christos 	if (error != 0) {
   4290  1.11     blymn 		aprint_error_dev(sc->sc_dev,
   4291  1.33  christos 		    "could not configure bluetooth coexistence\n");
   4292  1.11     blymn 		return error;
   4293  1.11     blymn 	}
   4294  1.11     blymn 
   4295  1.33  christos 	/* Configure adapter. */
   4296  1.33  christos 	memset(&sc->rxon, 0, sizeof (struct iwn_rxon));
   4297  1.33  christos 	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
   4298  1.33  christos 	IEEE80211_ADDR_COPY(sc->rxon.myaddr, ic->ic_myaddr);
   4299  1.33  christos 	IEEE80211_ADDR_COPY(sc->rxon.wlap, ic->ic_myaddr);
   4300  1.33  christos 	/* Set default channel. */
   4301  1.33  christos 	sc->rxon.chan = htole16(ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
   4302  1.33  christos 	sc->rxon.flags = htole32(IWN_RXON_TSF | IWN_RXON_CTS_TO_SELF);
   4303  1.33  christos 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_ibss_chan))
   4304  1.33  christos 		sc->rxon.flags |= htole32(IWN_RXON_AUTO | IWN_RXON_24GHZ);
   4305  1.33  christos 	switch (ic->ic_opmode) {
   4306  1.33  christos 	case IEEE80211_M_STA:
   4307  1.33  christos 		sc->rxon.mode = IWN_MODE_STA;
   4308  1.33  christos 		sc->rxon.filter = htole32(IWN_FILTER_MULTICAST);
   4309  1.33  christos 		break;
   4310  1.33  christos 	case IEEE80211_M_MONITOR:
   4311  1.33  christos 		sc->rxon.mode = IWN_MODE_MONITOR;
   4312  1.33  christos 		sc->rxon.filter = htole32(IWN_FILTER_MULTICAST |
   4313  1.33  christos 		    IWN_FILTER_CTL | IWN_FILTER_PROMISC);
   4314  1.33  christos 		break;
   4315  1.33  christos 	default:
   4316  1.33  christos 		/* Should not get there. */
   4317  1.33  christos 		break;
   4318   1.1      ober 	}
   4319  1.33  christos 	sc->rxon.cck_mask  = 0x0f;	/* not yet negotiated */
   4320  1.33  christos 	sc->rxon.ofdm_mask = 0xff;	/* not yet negotiated */
   4321  1.33  christos 	sc->rxon.ht_single_mask = 0xff;
   4322  1.33  christos 	sc->rxon.ht_dual_mask = 0xff;
   4323  1.33  christos 	rxchain = IWN_RXCHAIN_VALID(IWN_ANT_ABC) | IWN_RXCHAIN_IDLE_COUNT(2) |
   4324  1.33  christos 	    IWN_RXCHAIN_MIMO_COUNT(2);
   4325  1.33  christos 	sc->rxon.rxchain = htole16(rxchain);
   4326  1.33  christos 	DPRINTF(("setting configuration\n"));
   4327  1.33  christos #ifdef notdef
   4328  1.20     blymn 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
   4329  1.33  christos 		sc->rxon.flags |= htole32(IWN_RXON_SHSLOT);
   4330  1.20     blymn 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   4331  1.33  christos 		sc->rxon.flags |= htole32(IWN_RXON_SHPREAMBLE);
   4332  1.33  christos 	sc->rxon.filter &= ~htole32(IWN_FILTER_BSS);
   4333  1.33  christos #endif
   4334  1.33  christos 	DPRINTF(("rxon chan %d flags %x cck %x ofdm %x\n", sc->rxon.chan,
   4335  1.33  christos 	    sc->rxon.flags, sc->rxon.cck_mask, sc->rxon.ofdm_mask));
   4336  1.33  christos 	error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->rxon, hal->rxonsz, 0);
   4337   1.1      ober 	if (error != 0) {
   4338  1.33  christos 		aprint_error_dev(sc->sc_dev, "configure command failed\n");
   4339   1.1      ober 		return error;
   4340   1.1      ober 	}
   4341   1.1      ober 
   4342  1.33  christos 	/* Configuration has changed, set TX power accordingly. */
   4343  1.33  christos 	if ((error = hal->set_txpower(sc, 0)) != 0) {
   4344  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not set TX power\n");
   4345   1.1      ober 		return error;
   4346   1.1      ober 	}
   4347   1.1      ober 
   4348  1.33  christos 	if ((error = iwn_add_broadcast_node(sc, 0)) != 0) {
   4349  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not add broadcast node\n");
   4350  1.11     blymn 		return error;
   4351  1.33  christos 	}
   4352  1.11     blymn 
   4353  1.33  christos 	if ((error = iwn_set_critical_temp(sc)) != 0) {
   4354  1.33  christos 		aprint_error_dev(sc->sc_dev,
   4355  1.33  christos 		    "could not set critical temperature\n");
   4356  1.33  christos 		return error;
   4357  1.33  christos 	}
   4358  1.33  christos 	return 0;
   4359  1.33  christos }
   4360  1.33  christos 
   4361  1.33  christos static int
   4362  1.33  christos iwn_scan(struct iwn_softc *sc, uint16_t flags)
   4363  1.33  christos {
   4364  1.33  christos 	struct ieee80211com *ic = &sc->sc_ic;
   4365  1.33  christos 	struct iwn_scan_hdr *hdr;
   4366  1.33  christos 	struct iwn_cmd_data *tx;
   4367  1.33  christos 	struct iwn_scan_chan *chan;
   4368  1.33  christos 	struct ieee80211_frame *wh;
   4369  1.33  christos 	struct ieee80211_rateset *rs;
   4370  1.33  christos 	struct ieee80211_channel *c;
   4371  1.33  christos 	enum ieee80211_phymode mode;
   4372  1.33  christos 	uint8_t *buf, *frm;
   4373  1.33  christos 	uint16_t rxchain;
   4374  1.33  christos 	uint8_t txant;
   4375  1.33  christos 	int buflen, error, nrates;
   4376  1.33  christos 
   4377  1.33  christos 	buf = malloc(IWN_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO);
   4378  1.33  christos 	if (buf == NULL) {
   4379  1.33  christos 		aprint_error_dev(sc->sc_dev,
   4380  1.33  christos 		    "could not allocate buffer for scan command\n");
   4381  1.33  christos 		return ENOMEM;
   4382  1.33  christos 	}
   4383  1.33  christos 	hdr = (struct iwn_scan_hdr *)buf;
   4384  1.33  christos 	/*
   4385  1.33  christos 	 * Move to the next channel if no frames are received within 10ms
   4386  1.33  christos 	 * after sending the probe request.
   4387  1.33  christos 	 */
   4388  1.33  christos 	hdr->quiet_time = htole16(10);		/* timeout in milliseconds */
   4389  1.33  christos 	hdr->quiet_threshold = htole16(1);	/* min # of packets */
   4390  1.33  christos 
   4391  1.33  christos 	/* Select antennas for scanning. */
   4392  1.33  christos 	rxchain = IWN_RXCHAIN_FORCE | IWN_RXCHAIN_VALID(IWN_ANT_ABC) |
   4393  1.33  christos 	    IWN_RXCHAIN_MIMO(IWN_ANT_ABC);
   4394  1.33  christos 	if ((flags & IEEE80211_CHAN_5GHZ) &&
   4395  1.33  christos 	    sc->hw_type == IWN_HW_REV_TYPE_4965) {
   4396  1.33  christos 		/* Ant A must be avoided in 5GHz because of an HW bug. */
   4397  1.33  christos 		rxchain |= IWN_RXCHAIN_SEL(IWN_ANT_B | IWN_ANT_C);
   4398  1.33  christos 	} else	/* Use all available RX antennas. */
   4399  1.33  christos 		rxchain |= IWN_RXCHAIN_SEL(IWN_ANT_ABC);
   4400  1.33  christos 	hdr->rxchain = htole16(rxchain);
   4401  1.33  christos 	hdr->filter = htole32(IWN_FILTER_MULTICAST | IWN_FILTER_BEACON);
   4402  1.33  christos 
   4403  1.33  christos 	tx = &(hdr->tx_cmd);
   4404  1.33  christos 	tx->flags = htole32(IWN_TX_AUTO_SEQ);
   4405  1.33  christos 	tx->id = sc->sc_hal->broadcast_id;
   4406  1.33  christos 	tx->lifetime = htole32(IWN_LIFETIME_INFINITE);
   4407  1.33  christos 
   4408  1.33  christos 	if (flags & IEEE80211_CHAN_5GHZ) {
   4409  1.33  christos 		hdr->crc_threshold = htole16(1);
   4410  1.33  christos 		/* Send probe requests at 6Mbps. */
   4411  1.33  christos 		tx->plcp = iwn_rates[IWN_RIDX_OFDM6].plcp;
   4412  1.33  christos 		rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
   4413  1.33  christos 	} else {
   4414  1.33  christos 		hdr->flags = htole32(IWN_RXON_24GHZ | IWN_RXON_AUTO);
   4415  1.33  christos 		/* Send probe requests at 1Mbps. */
   4416  1.33  christos 		tx->plcp = iwn_rates[IWN_RIDX_CCK1].plcp;
   4417  1.33  christos 		tx->rflags = IWN_RFLAG_CCK;
   4418  1.33  christos 		rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
   4419  1.33  christos 	}
   4420  1.33  christos 	/* Use the first valid TX antenna. */
   4421  1.33  christos 	txant = IWN_LSB(sc->txantmsk);
   4422  1.33  christos 	tx->rflags |= IWN_RFLAG_ANT(txant);
   4423  1.33  christos 
   4424  1.33  christos 	if (ic->ic_des_esslen != 0) {
   4425  1.33  christos 		hdr->scan_essid[0].id = IEEE80211_ELEMID_SSID;
   4426  1.33  christos 		hdr->scan_essid[0].len = ic->ic_des_esslen;
   4427  1.33  christos 		memcpy(hdr->scan_essid[0].data, ic->ic_des_essid, ic->ic_des_esslen);
   4428  1.33  christos 	}
   4429  1.33  christos 	/*
   4430  1.33  christos 	 * Build a probe request frame.  Most of the following code is a
   4431  1.33  christos 	 * copy & paste of what is done in net80211.
   4432  1.33  christos 	 */
   4433  1.33  christos 	wh = &(hdr->wh);
   4434  1.33  christos 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
   4435  1.33  christos 	    IEEE80211_FC0_SUBTYPE_PROBE_REQ;
   4436  1.33  christos 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   4437  1.33  christos 	IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr);
   4438  1.33  christos 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
   4439  1.33  christos 	IEEE80211_ADDR_COPY(wh->i_addr3, etherbroadcastaddr);
   4440  1.33  christos 	*(uint16_t *)&wh->i_dur[0] = 0;	/* filled by HW */
   4441  1.33  christos 	*(uint16_t *)&wh->i_seq[0] = 0;	/* filled by HW */
   4442  1.33  christos 
   4443  1.33  christos 	frm = &(hdr->data[0]);
   4444  1.33  christos 	/* add empty SSID IE */
   4445  1.33  christos 	*frm++ = IEEE80211_ELEMID_SSID;
   4446  1.33  christos 	*frm++ = 0;
   4447  1.33  christos 
   4448  1.33  christos 	mode = ieee80211_chan2mode(ic, ic->ic_ibss_chan);
   4449  1.33  christos 	rs = &ic->ic_sup_rates[mode];
   4450  1.33  christos 
   4451  1.33  christos 	/* add supported rates IE */
   4452  1.33  christos 	*frm++ = IEEE80211_ELEMID_RATES;
   4453  1.33  christos 	nrates = rs->rs_nrates;
   4454  1.33  christos 	if (nrates > IEEE80211_RATE_SIZE)
   4455  1.33  christos 		nrates = IEEE80211_RATE_SIZE;
   4456  1.33  christos 	*frm++ = nrates;
   4457  1.33  christos 	memcpy(frm, rs->rs_rates, nrates);
   4458  1.33  christos 	frm += nrates;
   4459  1.33  christos 
   4460  1.33  christos 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
   4461  1.33  christos 		nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
   4462  1.33  christos 		*frm++ = IEEE80211_ELEMID_XRATES;
   4463  1.33  christos 		*frm++ = nrates;
   4464  1.33  christos 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
   4465  1.33  christos 		frm += nrates;
   4466  1.33  christos 	}
   4467  1.33  christos 
   4468  1.33  christos 	/* Set length of probe request. */
   4469  1.33  christos 	tx->len = htole16(frm - (uint8_t *)wh);
   4470  1.33  christos 
   4471  1.33  christos 	chan = (struct iwn_scan_chan *)frm;
   4472  1.33  christos 	for (c  = &ic->ic_channels[1];
   4473  1.33  christos 	     c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) {
   4474  1.33  christos 		if ((c->ic_flags & flags) != flags)
   4475  1.33  christos 			continue;
   4476  1.33  christos 
   4477  1.33  christos 		chan->chan = htole16(ieee80211_chan2ieee(ic, c));
   4478  1.33  christos 		DPRINTFN(2, ("adding channel %d\n", chan->chan));
   4479  1.33  christos 		chan->flags = 0;
   4480  1.33  christos 		if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE))
   4481  1.33  christos 			chan->flags |= htole32(IWN_CHAN_ACTIVE);
   4482  1.33  christos 		if (ic->ic_des_esslen != 0)
   4483  1.33  christos 			chan->flags |= htole32(IWN_CHAN_NPBREQS(1));
   4484  1.33  christos 		chan->dsp_gain = 0x6e;
   4485  1.33  christos 		if (IEEE80211_IS_CHAN_5GHZ(c)) {
   4486  1.33  christos 			chan->rf_gain = 0x3b;
   4487  1.33  christos 			chan->active  = htole16(24);
   4488  1.33  christos 			chan->passive = htole16(110);
   4489  1.33  christos 		} else {
   4490  1.33  christos 			chan->rf_gain = 0x28;
   4491  1.33  christos 			chan->active  = htole16(36);
   4492  1.33  christos 			chan->passive = htole16(120);
   4493  1.33  christos 		}
   4494  1.33  christos 		hdr->nchan++;
   4495  1.33  christos 		chan++;
   4496  1.33  christos 	}
   4497  1.33  christos 
   4498  1.33  christos 	buflen = (uint8_t *)chan - buf;
   4499  1.33  christos 	hdr->len = htole16(buflen);
   4500  1.33  christos 
   4501  1.33  christos 	DPRINTF(("sending scan command nchan=%d\n", hdr->nchan));
   4502  1.33  christos 	error = iwn_cmd(sc, IWN_CMD_SCAN, buf, buflen, 1);
   4503  1.33  christos 	free(buf, M_DEVBUF);
   4504  1.33  christos 	return error;
   4505  1.33  christos }
   4506  1.33  christos 
   4507  1.33  christos static int
   4508  1.33  christos iwn_auth(struct iwn_softc *sc)
   4509  1.33  christos {
   4510  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   4511  1.33  christos 	struct ieee80211com *ic = &sc->sc_ic;
   4512  1.33  christos 	struct ieee80211_node *ni = ic->ic_bss;
   4513  1.33  christos 	int error;
   4514  1.33  christos 
   4515  1.33  christos 	sc->calib.state = IWN_CALIB_STATE_INIT;
   4516  1.33  christos 
   4517  1.33  christos 	/* Update adapter's configuration. */
   4518  1.33  christos 	sc->rxon.associd = 0;
   4519  1.33  christos 	IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
   4520  1.33  christos 	sc->rxon.chan = htole16(ieee80211_chan2ieee(ic, ni->ni_chan));
   4521  1.33  christos 	sc->rxon.flags = htole32(IWN_RXON_TSF | IWN_RXON_CTS_TO_SELF);
   4522  1.33  christos 	if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
   4523  1.33  christos 		sc->rxon.flags |= htole32(IWN_RXON_AUTO | IWN_RXON_24GHZ);
   4524  1.33  christos 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
   4525  1.33  christos 		sc->rxon.flags |= htole32(IWN_RXON_SHSLOT);
   4526  1.33  christos 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   4527  1.33  christos 		sc->rxon.flags |= htole32(IWN_RXON_SHPREAMBLE);
   4528  1.33  christos 	switch (ic->ic_curmode) {
   4529  1.33  christos 	case IEEE80211_MODE_11A:
   4530  1.33  christos 		sc->rxon.cck_mask  = 0;
   4531  1.33  christos 		sc->rxon.ofdm_mask = 0x15;
   4532  1.33  christos 		break;
   4533  1.33  christos 	case IEEE80211_MODE_11B:
   4534  1.33  christos 		sc->rxon.cck_mask  = 0x03;
   4535  1.33  christos 		sc->rxon.ofdm_mask = 0;
   4536  1.33  christos 		break;
   4537  1.33  christos 	default:	/* Assume 802.11b/g. */
   4538  1.33  christos 		sc->rxon.cck_mask  = 0x0f;
   4539  1.33  christos 		sc->rxon.ofdm_mask = 0x15;
   4540  1.33  christos 		break;
   4541  1.33  christos 	}
   4542  1.33  christos #if 1
   4543  1.33  christos 	DPRINTF(("rxon chan %d flags %x cck %x ofdm %x\n", sc->rxon.chan,
   4544  1.33  christos 	    sc->rxon.flags, sc->rxon.cck_mask, sc->rxon.ofdm_mask));
   4545  1.33  christos 	error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->rxon, hal->rxonsz, 1);
   4546  1.33  christos 	if (error != 0) {
   4547  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not configure\n");
   4548  1.33  christos 		return error;
   4549  1.33  christos 	}
   4550  1.33  christos 
   4551  1.33  christos 	/* Configuration has changed, set TX power accordingly. */
   4552  1.33  christos 	if ((error = hal->set_txpower(sc, 1)) != 0) {
   4553  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not set TX power\n");
   4554  1.33  christos 		return error;
   4555  1.33  christos 	}
   4556  1.33  christos 	/*
   4557  1.33  christos 	 * Reconfiguring RXON clears the firmware's nodes table so we must
   4558  1.33  christos 	 * add the broadcast node again.
   4559  1.33  christos 	 */
   4560  1.33  christos 	if ((error = iwn_add_broadcast_node(sc, 1)) != 0) {
   4561  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not add broadcast node\n");
   4562  1.33  christos 		return error;
   4563  1.33  christos 	}
   4564  1.33  christos #else
   4565  1.33  christos /*	iwn_enable_tsf(sc, ni);*/
   4566  1.33  christos 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
   4567  1.33  christos 		sc->rxon.flags |= htole32(IWN_RXON_SHSLOT);
   4568  1.33  christos 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   4569  1.33  christos 		sc->rxon.flags |= htole32(IWN_RXON_SHPREAMBLE);
   4570  1.33  christos 	sc->rxon.filter &= ~htole32(IWN_FILTER_BSS);
   4571  1.33  christos 
   4572  1.33  christos 	DPRINTF(("rxon chan %d flags %x cck %x ofdm %x\n", sc->rxon.chan,
   4573  1.33  christos 	    sc->rxon.flags, sc->rxon.cck_mask, sc->rxon.ofdm_mask));
   4574  1.33  christos 	error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->rxon, hal->rxonsz, 1);
   4575  1.33  christos 	if (error != 0) {
   4576  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not configure\n");
   4577  1.33  christos 		return error;
   4578  1.33  christos 	}
   4579  1.33  christos 
   4580  1.33  christos 	/* Configuration has changed, set TX power accordingly. */
   4581  1.33  christos 	if ((error = hal->set_txpower(sc, 1)) != 0) {
   4582  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not set TX power\n");
   4583  1.33  christos 		return error;
   4584  1.33  christos 	}
   4585  1.33  christos 	/*
   4586  1.33  christos 	 * Reconfiguring RXON clears the firmware's nodes table so we must
   4587  1.33  christos 	 * add the broadcast node again.
   4588  1.33  christos 	 */
   4589  1.33  christos 	if ((error = iwn_add_broadcast_node(sc, 1)) != 0) {
   4590  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not add broadcast node\n");
   4591  1.33  christos 		return error;
   4592  1.33  christos 	}
   4593  1.11     blymn 	/* add BSS node */
   4594  1.33  christos 	DPRINTF(("adding BSS node from auth\n"));
   4595  1.20     blymn 	if ((error = iwn_add_node(sc, ni, false, true, 0)) != 0)
   4596   1.1      ober 		return error;
   4597  1.11     blymn 
   4598  1.11     blymn 	if (ic->ic_opmode == IEEE80211_M_STA) {
   4599  1.11     blymn 		/* fake a join to init the tx rate */
   4600  1.11     blymn 		iwn_newassoc(ni, 1);
   4601   1.1      ober 	}
   4602  1.33  christos 
   4603  1.11     blymn 	if ((error = iwn_init_sensitivity(sc)) != 0) {
   4604  1.11     blymn 		aprint_error_dev(sc->sc_dev, "could not set sensitivity\n");
   4605   1.1      ober 		return error;
   4606   1.1      ober 	}
   4607  1.33  christos #endif
   4608   1.1      ober 	return 0;
   4609   1.1      ober }
   4610   1.1      ober 
   4611   1.1      ober static int
   4612   1.1      ober iwn_run(struct iwn_softc *sc)
   4613   1.1      ober {
   4614  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   4615   1.1      ober 	struct ieee80211com *ic = &sc->sc_ic;
   4616   1.1      ober 	struct ieee80211_node *ni = ic->ic_bss;
   4617   1.1      ober 	int error;
   4618   1.1      ober 
   4619   1.1      ober 	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
   4620  1.33  christos 		/* Link LED blinks while monitoring. */
   4621   1.1      ober 		iwn_set_led(sc, IWN_LED_LINK, 5, 5);
   4622   1.1      ober 		return 0;
   4623   1.1      ober 	}
   4624  1.33  christos 	if ((error = iwn_set_timing(sc, ni)) != 0) {
   4625  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not set timing\n");
   4626  1.33  christos 		return error;
   4627  1.33  christos 	}
   4628   1.1      ober 
   4629  1.33  christos 	/* Update adapter's configuration. */
   4630  1.33  christos 	sc->rxon.associd = htole16(IEEE80211_AID(ni->ni_associd));
   4631  1.33  christos 	/* Short preamble and slot time are negotiated when associating. */
   4632  1.33  christos 	sc->rxon.flags &= ~htole32(IWN_RXON_SHPREAMBLE | IWN_RXON_SHSLOT);
   4633   1.1      ober 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
   4634  1.33  christos 		sc->rxon.flags |= htole32(IWN_RXON_SHSLOT);
   4635   1.1      ober 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   4636  1.33  christos 		sc->rxon.flags |= htole32(IWN_RXON_SHPREAMBLE);
   4637  1.33  christos 	sc->rxon.filter |= htole32(IWN_FILTER_BSS);
   4638  1.33  christos 	DPRINTF(("rxon chan %d flags %x\n", sc->rxon.chan, sc->rxon.flags));
   4639  1.33  christos 	error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->rxon, hal->rxonsz, 1);
   4640   1.1      ober 	if (error != 0) {
   4641  1.11     blymn 		aprint_error_dev(sc->sc_dev,
   4642  1.33  christos 		    "could not update configuration\n");
   4643   1.1      ober 		return error;
   4644   1.1      ober 	}
   4645   1.1      ober 
   4646  1.33  christos 	/* Configuration has changed, set TX power accordingly. */
   4647  1.33  christos 	if ((error = hal->set_txpower(sc, 1)) != 0) {
   4648  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not set TX power\n");
   4649   1.1      ober 		return error;
   4650   1.1      ober 	}
   4651   1.1      ober 
   4652  1.33  christos 	/* Fake a join to initialize the TX rate. */
   4653  1.33  christos 	((struct iwn_node *)ni)->id = IWN_ID_BSS;
   4654  1.33  christos 	iwn_newassoc(ni, 1);
   4655  1.33  christos 
   4656  1.33  christos 	/* Add BSS node. */
   4657  1.33  christos 	iwn_add_node(sc, ni, false, true, 0);
   4658  1.33  christos 	/* Start periodic calibration timer. */
   4659  1.33  christos 	sc->calib.state = IWN_CALIB_STATE_ASSOC;
   4660  1.33  christos 	sc->calib_cnt = 0;
   4661  1.33  christos 	callout_schedule(&sc->calib_to, hz / 2);
   4662  1.33  christos 
   4663  1.33  christos 	/* Link LED always on while associated. */
   4664  1.33  christos 	iwn_set_led(sc, IWN_LED_LINK, 0, 1);
   4665  1.33  christos 	return 0;
   4666  1.33  christos }
   4667  1.33  christos 
   4668  1.33  christos static int
   4669  1.33  christos iwn_wme_update(struct ieee80211com *ic)
   4670  1.33  christos {
   4671  1.33  christos #define IWN_EXP2(v)    htole16((1 << (v)) - 1)
   4672  1.33  christos #define IWN_USEC(v)    htole16(IEEE80211_TXOP_TO_US(v))
   4673  1.33  christos 	struct iwn_softc *sc = ic->ic_ifp->if_softc;
   4674  1.33  christos 	const struct wmeParams *wmep;
   4675  1.33  christos 	struct iwn_edca_params cmd;
   4676  1.33  christos 	int ac;
   4677  1.33  christos 
   4678  1.33  christos 	/* don't override default WME values if WME is not actually enabled */
   4679  1.33  christos 	if (!(ic->ic_flags & IEEE80211_F_WME))
   4680  1.33  christos 		return 0;
   4681  1.33  christos 	cmd.flags = 0;
   4682  1.33  christos 	for (ac = 0; ac < WME_NUM_AC; ac++) {
   4683  1.33  christos 		wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
   4684  1.33  christos 		cmd.ac[ac].aifsn = wmep->wmep_aifsn;
   4685  1.33  christos 		cmd.ac[ac].cwmin = IWN_EXP2(wmep->wmep_logcwmin);
   4686  1.33  christos 		cmd.ac[ac].cwmax = IWN_EXP2(wmep->wmep_logcwmax);
   4687  1.33  christos 		cmd.ac[ac].txoplimit  = IWN_USEC(wmep->wmep_txopLimit);
   4688  1.33  christos 
   4689  1.33  christos 		DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
   4690  1.33  christos 					"txop=%d\n", ac, cmd.ac[ac].aifsn,
   4691  1.33  christos 					cmd.ac[ac].cwmin,
   4692  1.33  christos 					cmd.ac[ac].cwmax, cmd.ac[ac].txoplimit));
   4693  1.33  christos 	}
   4694  1.33  christos 	return iwn_cmd(sc, IWN_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1);
   4695  1.33  christos #undef IWN_USEC
   4696  1.33  christos #undef IWN_EXP2
   4697  1.33  christos }
   4698  1.33  christos 
   4699  1.33  christos #if 0
   4700  1.33  christos /*
   4701  1.33  christos  * We support CCMP hardware encryption/decryption of unicast frames only.
   4702  1.33  christos  * HW support for TKIP really sucks.  We should let TKIP die anyway.
   4703  1.33  christos  */
   4704  1.33  christos static int
   4705  1.33  christos iwn_set_key(struct ieee80211com *ic, struct ieee80211_node *ni,
   4706  1.33  christos     struct ieee80211_key *k)
   4707  1.33  christos {
   4708  1.33  christos 	struct iwn_softc *sc = ic->ic_softc;
   4709  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   4710  1.33  christos 	struct iwn_node *wn = (void *)ni;
   4711  1.33  christos 	struct iwn_node_info node;
   4712  1.33  christos 	uint16_t kflags;
   4713  1.33  christos 
   4714  1.33  christos 	if ((k->k_flags & IEEE80211_KEY_GROUP) ||
   4715  1.33  christos 	    k->k_cipher != IEEE80211_CIPHER_CCMP)
   4716  1.33  christos 		return ieee80211_set_key(ic, ni, k);
   4717  1.33  christos 
   4718  1.33  christos 	kflags = IWN_KFLAG_CCMP | IWN_KFLAG_MAP | IWN_KFLAG_KID(k->k_id);
   4719  1.33  christos 	if (k->k_flags & IEEE80211_KEY_GROUP)
   4720  1.33  christos 		kflags |= IWN_KFLAG_GROUP;
   4721  1.33  christos 
   4722  1.33  christos 	memset(&node, 0, sizeof node);
   4723  1.33  christos 	node.id = (k->k_flags & IEEE80211_KEY_GROUP) ?
   4724  1.33  christos 	    hal->broadcast_id : wn->id;
   4725  1.33  christos 	node.control = IWN_NODE_UPDATE;
   4726  1.33  christos 	node.flags = IWN_FLAG_SET_KEY;
   4727  1.33  christos 	node.kflags = htole16(kflags);
   4728  1.33  christos 	node.kid = k->k_id;
   4729  1.33  christos 	memcpy(node.key, k->k_key, k->k_len);
   4730  1.33  christos 	DPRINTF(("set key id=%d for node %d\n", k->k_id, node.id));
   4731  1.33  christos 	return hal->add_node(sc, &node, 1);
   4732  1.33  christos }
   4733  1.33  christos 
   4734  1.33  christos static void
   4735  1.33  christos iwn_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni,
   4736  1.33  christos     struct ieee80211_key *k)
   4737  1.33  christos {
   4738  1.33  christos 	struct iwn_softc *sc = ic->ic_softc;
   4739  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   4740  1.33  christos 	struct iwn_node *wn = (void *)ni;
   4741  1.33  christos 	struct iwn_node_info node;
   4742   1.1      ober 
   4743  1.33  christos 	if ((k->k_flags & IEEE80211_KEY_GROUP) ||
   4744  1.33  christos 	    k->k_cipher != IEEE80211_CIPHER_CCMP) {
   4745  1.33  christos 		/* See comment about other ciphers above. */
   4746  1.33  christos 		ieee80211_delete_key(ic, ni, k);
   4747  1.33  christos 		return;
   4748   1.1      ober 	}
   4749  1.33  christos 	if (ic->ic_state != IEEE80211_S_RUN)
   4750  1.33  christos 		return;	/* Nothing to do. */
   4751  1.33  christos 	memset(&node, 0, sizeof node);
   4752  1.33  christos 	node.id = (k->k_flags & IEEE80211_KEY_GROUP) ?
   4753  1.33  christos 	    hal->broadcast_id : wn->id;
   4754  1.33  christos 	node.control = IWN_NODE_UPDATE;
   4755  1.33  christos 	node.flags = IWN_FLAG_SET_KEY;
   4756  1.33  christos 	node.kflags = htole16(IWN_KFLAG_INVALID);
   4757  1.33  christos 	node.kid = 0xff;
   4758  1.33  christos 	DPRINTF(("delete keys for node %d\n", node.id));
   4759  1.33  christos 	(void)hal->add_node(sc, &node, 1);
   4760  1.33  christos }
   4761  1.33  christos #endif
   4762  1.33  christos 
   4763  1.33  christos #ifndef IEEE80211_NO_HT
   4764  1.33  christos /*
   4765  1.33  christos  * This function is called by upper layer when a ADDBA request is received
   4766  1.33  christos  * from another STA and before the ADDBA response is sent.
   4767  1.33  christos  */
   4768  1.33  christos static int
   4769  1.33  christos iwn_ampdu_rx_start(struct ieee80211com *ic, struct ieee80211_node *ni,
   4770  1.33  christos     uint8_t tid, uint16_t ssn)
   4771  1.33  christos {
   4772  1.33  christos 	struct iwn_softc *sc = ic->ic_softc;
   4773  1.33  christos 	struct iwn_node *wn = (void *)ni;
   4774  1.33  christos 	struct iwn_node_info node;
   4775  1.33  christos 
   4776  1.33  christos 	memset(&node, 0, sizeof node);
   4777  1.33  christos 	node.id = wn->id;
   4778  1.33  christos 	node.control = IWN_NODE_UPDATE;
   4779  1.33  christos 	node.flags = IWN_FLAG_SET_ADDBA;
   4780  1.33  christos 	node.addba_tid = tid;
   4781  1.33  christos 	node.addba_ssn = htole16(ssn);
   4782  1.33  christos 	DPRINTFN(2, ("ADDBA RA=%d TID=%d SSN=%d\n", wn->id, tid, ssn));
   4783  1.33  christos 	return sc->sc_hal->add_node(sc, &node, 1);
   4784  1.33  christos }
   4785  1.33  christos 
   4786  1.33  christos /*
   4787  1.33  christos  * This function is called by upper layer on teardown of an HT-immediate
   4788  1.33  christos  * Block Ack (eg. uppon receipt of a DELBA frame.)
   4789  1.33  christos  */
   4790  1.33  christos static void
   4791  1.33  christos iwn_ampdu_rx_stop(struct ieee80211com *ic, struct ieee80211_node *ni,
   4792  1.33  christos     uint8_t tid, uint16_t ssn)
   4793  1.33  christos {
   4794  1.33  christos 	struct iwn_softc *sc = ic->ic_softc;
   4795  1.33  christos 	struct iwn_node *wn = (void *)ni;
   4796  1.33  christos 	struct iwn_node_info node;
   4797   1.1      ober 
   4798  1.33  christos 	memset(&node, 0, sizeof node);
   4799  1.33  christos 	node.id = wn->id;
   4800  1.33  christos 	node.control = IWN_NODE_UPDATE;
   4801  1.33  christos 	node.flags = IWN_FLAG_SET_DELBA;
   4802  1.33  christos 	node.delba_tid = tid;
   4803  1.33  christos 	DPRINTFN(2, ("DELBA RA=%d TID=%d\n", wn->id, tid));
   4804  1.33  christos 	(void)sc->sc_hal->add_node(sc, &node, 1);
   4805  1.33  christos }
   4806  1.33  christos 
   4807  1.33  christos /*
   4808  1.33  christos  * This function is called by upper layer when a ADDBA response is received
   4809  1.33  christos  * from another STA.
   4810  1.33  christos  */
   4811  1.33  christos static int
   4812  1.33  christos iwn_ampdu_tx_start(struct ieee80211com *ic, struct ieee80211_node *ni,
   4813  1.33  christos     uint8_t tid, uint16_t ssn)
   4814  1.33  christos {
   4815  1.33  christos 	struct iwn_softc *sc = ic->ic_softc;
   4816  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   4817  1.33  christos 	struct iwn_node *wn = (void *)ni;
   4818  1.33  christos 	struct iwn_node_info node;
   4819  1.33  christos 	int error;
   4820  1.33  christos 
   4821  1.33  christos 	/* Enable TX for the specified RA/TID. */
   4822  1.33  christos 	wn->disable_tid &= ~(1 << tid);
   4823  1.33  christos 	memset(&node, 0, sizeof node);
   4824  1.33  christos 	node.id = wn->id;
   4825  1.33  christos 	node.control = IWN_NODE_UPDATE;
   4826  1.33  christos 	node.flags = IWN_FLAG_SET_DISABLE_TID;
   4827  1.33  christos 	node.disable_tid = htole16(wn->disable_tid);
   4828  1.33  christos 	error = hal->add_node(sc, &node, 1);
   4829  1.33  christos 	if (error != 0)
   4830  1.33  christos 		return error;
   4831  1.33  christos 
   4832  1.33  christos 	if ((error = iwn_nic_lock(sc)) != 0)
   4833  1.33  christos 		return error;
   4834  1.33  christos 	hal->ampdu_tx_start(sc, ni, tid, ssn);
   4835  1.33  christos 	iwn_nic_unlock(sc);
   4836  1.33  christos 	return 0;
   4837  1.33  christos }
   4838  1.33  christos 
   4839  1.33  christos static void
   4840  1.33  christos iwn_ampdu_tx_stop(struct ieee80211com *ic, struct ieee80211_node *ni,
   4841  1.33  christos     uint8_t tid, uint16_t ssn)
   4842  1.33  christos {
   4843  1.33  christos 	struct iwn_softc *sc = ic->ic_softc;
   4844  1.33  christos 
   4845  1.33  christos 	if (iwn_nic_lock(sc) != 0)
   4846  1.33  christos 		return;
   4847  1.33  christos 	sc->sc_hal->ampdu_tx_stop(sc, tid, ssn);
   4848  1.33  christos 	iwn_nic_unlock(sc);
   4849  1.33  christos }
   4850  1.33  christos 
   4851  1.33  christos static void
   4852  1.33  christos iwn4965_ampdu_tx_start(struct iwn_softc *sc, struct ieee80211_node *ni,
   4853  1.33  christos     uint8_t tid, uint16_t ssn)
   4854  1.33  christos {
   4855  1.33  christos 	struct iwn_node *wn = (void *)ni;
   4856  1.33  christos 	int qid = 7 + tid;
   4857  1.33  christos 
   4858  1.33  christos 	/* Stop TX scheduler while we're changing its configuration. */
   4859  1.33  christos 	iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
   4860  1.33  christos 	    IWN4965_TXQ_STATUS_CHGACT);
   4861  1.33  christos 
   4862  1.33  christos 	/* Assign RA/TID translation to the queue. */
   4863  1.33  christos 	iwn_mem_write_2(sc, sc->sched_base + IWN4965_SCHED_TRANS_TBL(qid),
   4864  1.33  christos 	    wn->id << 4 | tid);
   4865  1.33  christos 
   4866  1.33  christos 	/* Enable chain mode for the queue. */
   4867  1.33  christos 	iwn_prph_setbits(sc, IWN4965_SCHED_QCHAIN_SEL, 1 << qid);
   4868  1.33  christos 
   4869  1.33  christos 	/* Set starting sequence number from the ADDBA request. */
   4870  1.33  christos 	IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ssn);
   4871  1.33  christos 	iwn_prph_write(sc, IWN4965_SCHED_QUEUE_RDPTR(qid), ssn);
   4872  1.33  christos 
   4873  1.33  christos 	/* Set scheduler window size. */
   4874  1.33  christos 	iwn_mem_write(sc, sc->sched_base + IWN4965_SCHED_QUEUE_OFFSET(qid),
   4875  1.33  christos 	    IWN_SCHED_WINSZ);
   4876  1.33  christos 	/* Set scheduler frame limit. */
   4877  1.33  christos 	iwn_mem_write(sc, sc->sched_base + IWN4965_SCHED_QUEUE_OFFSET(qid) + 4,
   4878  1.33  christos 	    IWN_SCHED_LIMIT << 16);
   4879  1.33  christos 
   4880  1.33  christos 	/* Enable interrupts for the queue. */
   4881  1.33  christos 	iwn_prph_setbits(sc, IWN4965_SCHED_INTR_MASK, 1 << qid);
   4882  1.33  christos 
   4883  1.33  christos 	/* Mark the queue as active. */
   4884  1.33  christos 	iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
   4885  1.33  christos 	    IWN4965_TXQ_STATUS_ACTIVE | IWN4965_TXQ_STATUS_AGGR_ENA |
   4886  1.33  christos 	    iwn_tid2fifo[tid] << 1);
   4887  1.33  christos }
   4888  1.33  christos 
   4889  1.33  christos static void
   4890  1.33  christos iwn4965_ampdu_tx_stop(struct iwn_softc *sc, uint8_t tid, uint16_t ssn)
   4891  1.33  christos {
   4892  1.33  christos 	int qid = 7 + tid;
   4893  1.33  christos 
   4894  1.33  christos 	/* Stop TX scheduler while we're changing its configuration. */
   4895  1.33  christos 	iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
   4896  1.33  christos 	    IWN4965_TXQ_STATUS_CHGACT);
   4897  1.33  christos 
   4898  1.33  christos 	/* Set starting sequence number from the ADDBA request. */
   4899  1.33  christos 	IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ssn);
   4900  1.33  christos 	iwn_prph_write(sc, IWN4965_SCHED_QUEUE_RDPTR(qid), ssn);
   4901  1.33  christos 
   4902  1.33  christos 	/* Disable interrupts for the queue. */
   4903  1.33  christos 	iwn_prph_clrbits(sc, IWN4965_SCHED_INTR_MASK, 1 << qid);
   4904  1.33  christos 
   4905  1.33  christos 	/* Mark the queue as inactive. */
   4906  1.33  christos 	iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
   4907  1.33  christos 	    IWN4965_TXQ_STATUS_INACTIVE | iwn_tid2fifo[tid] << 1);
   4908  1.33  christos }
   4909  1.33  christos 
   4910  1.33  christos static void
   4911  1.33  christos iwn5000_ampdu_tx_start(struct iwn_softc *sc, struct ieee80211_node *ni,
   4912  1.33  christos     uint8_t tid, uint16_t ssn)
   4913  1.33  christos {
   4914  1.33  christos 	struct iwn_node *wn = (void *)ni;
   4915  1.33  christos 	int qid = 10 + tid;
   4916  1.33  christos 
   4917  1.33  christos 	/* Stop TX scheduler while we're changing its configuration. */
   4918  1.33  christos 	iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
   4919  1.33  christos 	    IWN5000_TXQ_STATUS_CHGACT);
   4920  1.33  christos 
   4921  1.33  christos 	/* Assign RA/TID translation to the queue. */
   4922  1.33  christos 	iwn_mem_write_2(sc, sc->sched_base + IWN5000_SCHED_TRANS_TBL(qid),
   4923  1.33  christos 	    wn->id << 4 | tid);
   4924  1.33  christos 
   4925  1.33  christos 	/* Enable chain mode for the queue. */
   4926  1.33  christos 	iwn_prph_setbits(sc, IWN5000_SCHED_QCHAIN_SEL, 1 << qid);
   4927  1.33  christos 
   4928  1.33  christos 	/* Enable aggregation for the queue. */
   4929  1.33  christos 	iwn_prph_setbits(sc, IWN5000_SCHED_AGGR_SEL, 1 << qid);
   4930  1.33  christos 
   4931  1.33  christos 	/* Set starting sequence number from the ADDBA request. */
   4932  1.33  christos 	IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ssn);
   4933  1.33  christos 	iwn_prph_write(sc, IWN5000_SCHED_QUEUE_RDPTR(qid), ssn);
   4934  1.33  christos 
   4935  1.33  christos 	/* Set scheduler window size and frame limit. */
   4936  1.33  christos 	iwn_mem_write(sc, sc->sched_base + IWN5000_SCHED_QUEUE_OFFSET(qid) + 4,
   4937  1.33  christos 	    IWN_SCHED_LIMIT << 16 | IWN_SCHED_WINSZ);
   4938  1.33  christos 
   4939  1.33  christos 	/* Enable interrupts for the queue. */
   4940  1.33  christos 	iwn_prph_setbits(sc, IWN5000_SCHED_INTR_MASK, 1 << qid);
   4941  1.33  christos 
   4942  1.33  christos 	/* Mark the queue as active. */
   4943  1.33  christos 	iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
   4944  1.33  christos 	    IWN5000_TXQ_STATUS_ACTIVE | iwn_tid2fifo[tid]);
   4945  1.33  christos }
   4946  1.33  christos 
   4947  1.33  christos static void
   4948  1.33  christos iwn5000_ampdu_tx_stop(struct iwn_softc *sc, uint8_t tid, uint16_t ssn)
   4949  1.33  christos {
   4950  1.33  christos 	int qid = 10 + tid;
   4951  1.33  christos 
   4952  1.33  christos 	/* Stop TX scheduler while we're changing its configuration. */
   4953  1.33  christos 	iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
   4954  1.33  christos 	    IWN5000_TXQ_STATUS_CHGACT);
   4955  1.33  christos 
   4956  1.33  christos 	/* Disable aggregation for the queue. */
   4957  1.33  christos 	iwn_prph_clrbits(sc, IWN5000_SCHED_AGGR_SEL, 1 << qid);
   4958  1.33  christos 
   4959  1.33  christos 	/* Set starting sequence number from the ADDBA request. */
   4960  1.33  christos 	IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ssn);
   4961  1.33  christos 	iwn_prph_write(sc, IWN5000_SCHED_QUEUE_RDPTR(qid), ssn);
   4962  1.33  christos 
   4963  1.33  christos 	/* Disable interrupts for the queue. */
   4964  1.33  christos 	iwn_prph_clrbits(sc, IWN5000_SCHED_INTR_MASK, 1 << qid);
   4965  1.33  christos 
   4966  1.33  christos 	/* Mark the queue as inactive. */
   4967  1.33  christos 	iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
   4968  1.33  christos 	    IWN5000_TXQ_STATUS_INACTIVE | iwn_tid2fifo[tid]);
   4969  1.33  christos }
   4970  1.33  christos #endif /* 0 */
   4971  1.33  christos 
   4972  1.33  christos /*
   4973  1.33  christos  * Query calibration tables from the initialization firmware.  We do this
   4974  1.33  christos  * only once at first boot.  Called from a process context.
   4975  1.33  christos  */
   4976  1.33  christos static int
   4977  1.33  christos iwn5000_query_calibration(struct iwn_softc *sc)
   4978  1.33  christos {
   4979  1.33  christos 	struct iwn5000_calib_config cmd;
   4980  1.33  christos 	int error;
   4981  1.33  christos 
   4982  1.33  christos 	memset(&cmd, 0, sizeof cmd);
   4983  1.33  christos 	cmd.ucode.once.enable = 0xffffffff;
   4984  1.33  christos 	cmd.ucode.once.start  = 0xffffffff;
   4985  1.33  christos 	cmd.ucode.once.send   = 0xffffffff;
   4986  1.33  christos 	cmd.ucode.flags       = 0xffffffff;
   4987  1.33  christos 	DPRINTF(("sending calibration query\n"));
   4988  1.33  christos 	error = iwn_cmd(sc, IWN5000_CMD_CALIB_CONFIG, &cmd, sizeof cmd, 0);
   4989  1.33  christos 	if (error != 0)
   4990   1.1      ober 		return error;
   4991   1.1      ober 
   4992  1.33  christos 	/* Wait at most two seconds for calibration to complete. */
   4993  1.33  christos 	return tsleep(sc, PCATCH, "iwncal", 2 * hz);
   4994  1.33  christos }
   4995  1.33  christos 
   4996  1.33  christos /*
   4997  1.33  christos  * Send calibration results to the runtime firmware.  These results were
   4998  1.33  christos  * obtained on first boot from the initialization firmware.
   4999  1.33  christos  */
   5000  1.33  christos static int
   5001  1.33  christos iwn5000_send_calibration(struct iwn_softc *sc)
   5002  1.33  christos {
   5003  1.33  christos 	int idx, error;
   5004   1.1      ober 
   5005  1.33  christos 	for (idx = 0; idx < 5; idx++) {
   5006  1.33  christos 		if (sc->calibcmd[idx].buf == NULL)
   5007  1.33  christos 			continue;	/* No results available. */
   5008  1.33  christos 		DPRINTF(("send calibration result idx=%d len=%d\n",
   5009  1.33  christos 		    idx, sc->calibcmd[idx].len));
   5010  1.33  christos 		error = iwn_cmd(sc, IWN_CMD_PHY_CALIB, sc->calibcmd[idx].buf,
   5011  1.33  christos 		    sc->calibcmd[idx].len, 0);
   5012  1.33  christos 		if (error != 0) {
   5013  1.11     blymn 			aprint_error_dev(sc->sc_dev,
   5014  1.33  christos 			    "could not send calibration result\n");
   5015  1.11     blymn 			return error;
   5016  1.11     blymn 		}
   5017  1.11     blymn 	}
   5018  1.33  christos 	return 0;
   5019  1.33  christos }
   5020  1.33  christos 
   5021  1.33  christos /*
   5022  1.33  christos  * This function is called after the runtime firmware notifies us of its
   5023  1.33  christos  * readiness (called in a process context.)
   5024  1.33  christos  */
   5025  1.33  christos static int
   5026  1.33  christos iwn4965_post_alive(struct iwn_softc *sc)
   5027  1.33  christos {
   5028  1.33  christos 	int error, qid;
   5029  1.11     blymn 
   5030  1.33  christos 	if ((error = iwn_nic_lock(sc)) != 0)
   5031  1.33  christos 		return error;
   5032  1.11     blymn 
   5033  1.33  christos 	/* Clear TX scheduler's state in SRAM. */
   5034  1.33  christos 	sc->sched_base = iwn_prph_read(sc, IWN_SCHED_SRAM_ADDR);
   5035  1.33  christos 	iwn_mem_set_region_4(sc, sc->sched_base + IWN4965_SCHED_CTX_OFF, 0,
   5036  1.33  christos 	    IWN4965_SCHED_CTX_LEN);
   5037  1.33  christos 
   5038  1.33  christos 	/* Set physical address of TX scheduler rings (1KB aligned.) */
   5039  1.33  christos 	iwn_prph_write(sc, IWN4965_SCHED_DRAM_ADDR, sc->sched_dma.paddr >> 10);
   5040  1.33  christos 
   5041  1.33  christos 	IWN_SETBITS(sc, IWN_FH_TX_CHICKEN, IWN_FH_TX_CHICKEN_SCHED_RETRY);
   5042  1.33  christos 
   5043  1.33  christos 	/* Disable chain mode for all our 16 queues. */
   5044  1.33  christos 	iwn_prph_write(sc, IWN4965_SCHED_QCHAIN_SEL, 0);
   5045  1.33  christos 
   5046  1.33  christos 	for (qid = 0; qid < IWN4965_NTXQUEUES; qid++) {
   5047  1.33  christos 		iwn_prph_write(sc, IWN4965_SCHED_QUEUE_RDPTR(qid), 0);
   5048  1.33  christos 		IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | 0);
   5049  1.33  christos 
   5050  1.33  christos 		/* Set scheduler window size. */
   5051  1.33  christos 		iwn_mem_write(sc, sc->sched_base +
   5052  1.33  christos 		    IWN4965_SCHED_QUEUE_OFFSET(qid), IWN_SCHED_WINSZ);
   5053  1.33  christos 		/* Set scheduler frame limit. */
   5054  1.33  christos 		iwn_mem_write(sc, sc->sched_base +
   5055  1.33  christos 		    IWN4965_SCHED_QUEUE_OFFSET(qid) + 4,
   5056  1.33  christos 		    IWN_SCHED_LIMIT << 16);
   5057  1.33  christos 	}
   5058  1.33  christos 
   5059  1.33  christos 	/* Enable interrupts for all our 16 queues. */
   5060  1.33  christos 	iwn_prph_write(sc, IWN4965_SCHED_INTR_MASK, 0xffff);
   5061  1.33  christos 	/* Identify TX FIFO rings (0-7). */
   5062  1.33  christos 	iwn_prph_write(sc, IWN4965_SCHED_TXFACT, 0xff);
   5063   1.1      ober 
   5064  1.33  christos 	/* Mark TX rings (4 EDCA + cmd + 2 HCCA) as active. */
   5065  1.33  christos 	for (qid = 0; qid < 7; qid++) {
   5066  1.33  christos 		static uint8_t qid2fifo[] = { 3, 2, 1, 0, 4, 5, 6 };
   5067  1.33  christos 		iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
   5068  1.33  christos 		    IWN4965_TXQ_STATUS_ACTIVE | qid2fifo[qid] << 1);
   5069  1.33  christos 	}
   5070  1.33  christos 	iwn_nic_unlock(sc);
   5071   1.1      ober 	return 0;
   5072   1.1      ober }
   5073   1.1      ober 
   5074   1.1      ober /*
   5075  1.33  christos  * This function is called after the initialization or runtime firmware
   5076  1.33  christos  * notifies us of its readiness (called in a process context.)
   5077   1.1      ober  */
   5078   1.1      ober static int
   5079  1.33  christos iwn5000_post_alive(struct iwn_softc *sc)
   5080   1.1      ober {
   5081  1.33  christos 	struct iwn5000_wimax_coex wimax;
   5082  1.33  christos 	int error, qid;
   5083  1.33  christos 
   5084  1.33  christos 	if ((error = iwn_nic_lock(sc)) != 0)
   5085  1.33  christos 		return error;
   5086   1.1      ober 
   5087  1.33  christos 	/* Clear TX scheduler's state in SRAM. */
   5088  1.33  christos 	sc->sched_base = iwn_prph_read(sc, IWN_SCHED_SRAM_ADDR);
   5089  1.33  christos 	iwn_mem_set_region_4(sc, sc->sched_base + IWN5000_SCHED_CTX_OFF, 0,
   5090  1.33  christos 	    IWN5000_SCHED_CTX_LEN);
   5091  1.33  christos 
   5092  1.33  christos 	/* Set physical address of TX scheduler rings (1KB aligned.) */
   5093  1.33  christos 	iwn_prph_write(sc, IWN5000_SCHED_DRAM_ADDR, sc->sched_dma.paddr >> 10);
   5094  1.33  christos 
   5095  1.33  christos 	IWN_SETBITS(sc, IWN_FH_TX_CHICKEN, IWN_FH_TX_CHICKEN_SCHED_RETRY);
   5096  1.33  christos 
   5097  1.33  christos 	/* Enable chain mode for all our 20 queues. */
   5098  1.33  christos 	iwn_prph_write(sc, IWN5000_SCHED_QCHAIN_SEL, 0xfffff);
   5099  1.33  christos 	iwn_prph_write(sc, IWN5000_SCHED_AGGR_SEL, 0);
   5100  1.33  christos 
   5101  1.33  christos 	for (qid = 0; qid < IWN5000_NTXQUEUES; qid++) {
   5102  1.33  christos 		iwn_prph_write(sc, IWN5000_SCHED_QUEUE_RDPTR(qid), 0);
   5103  1.33  christos 		IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | 0);
   5104  1.33  christos 
   5105  1.33  christos 		iwn_mem_write(sc, sc->sched_base +
   5106  1.33  christos 		    IWN5000_SCHED_QUEUE_OFFSET(qid), 0);
   5107  1.33  christos 		/* Set scheduler window size and frame limit. */
   5108  1.33  christos 		iwn_mem_write(sc, sc->sched_base +
   5109  1.33  christos 		    IWN5000_SCHED_QUEUE_OFFSET(qid) + 4,
   5110  1.33  christos 		    IWN_SCHED_LIMIT << 16 | IWN_SCHED_WINSZ);
   5111  1.33  christos 	}
   5112  1.33  christos 
   5113  1.33  christos 	/* Enable interrupts for all our 20 queues. */
   5114  1.33  christos 	iwn_prph_write(sc, IWN5000_SCHED_INTR_MASK, 0xfffff);
   5115  1.33  christos 	/* Identify TX FIFO rings (0-7). */
   5116  1.33  christos 	iwn_prph_write(sc, IWN5000_SCHED_TXFACT, 0xff);
   5117   1.1      ober 
   5118  1.33  christos 	/* Mark TX rings (4 EDCA + cmd + 2 HCCA) as active. */
   5119  1.33  christos 	for (qid = 0; qid < 7; qid++) {
   5120  1.33  christos 		static uint8_t qid2fifo[] = { 3, 2, 1, 0, 7, 5, 6 };
   5121  1.33  christos 		iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
   5122  1.33  christos 		    IWN5000_TXQ_STATUS_ACTIVE | qid2fifo[qid]);
   5123  1.33  christos 	}
   5124  1.33  christos 	iwn_nic_unlock(sc);
   5125  1.33  christos 
   5126  1.33  christos 	/* Configure WiMAX (IEEE 802.16e) coexistence. */
   5127  1.33  christos 	memset(&wimax, 0, sizeof wimax);
   5128  1.33  christos 	DPRINTF(("Configuring WiMAX coexistence\n"));
   5129  1.33  christos 	error = iwn_cmd(sc, IWN5000_CMD_WIMAX_COEX, &wimax, sizeof wimax, 0);
   5130  1.33  christos 	if (error != 0) {
   5131  1.33  christos 		aprint_error_dev(sc->sc_dev,
   5132  1.33  christos 		    "could not configure WiMAX coexistence\n");
   5133  1.33  christos 		return error;
   5134   1.1      ober 	}
   5135  1.20     blymn 
   5136  1.33  christos 	if (sc->hw_type != IWN_HW_REV_TYPE_5150) {
   5137  1.33  christos 		struct iwn5000_phy_calib_crystal cmd;
   5138  1.33  christos 
   5139  1.33  christos 		/* Perform crystal calibration. */
   5140  1.33  christos 		memset(&cmd, 0, sizeof cmd);
   5141  1.33  christos 		cmd.code = IWN5000_PHY_CALIB_CRYSTAL;
   5142  1.33  christos 		cmd.ngroups = 1;
   5143  1.33  christos 		cmd.isvalid = 1;
   5144  1.33  christos 		cmd.cap_pin[0] = le32toh(sc->eeprom_crystal) & 0xff;
   5145  1.33  christos 		cmd.cap_pin[1] = (le32toh(sc->eeprom_crystal) >> 16) & 0xff;
   5146  1.33  christos 		DPRINTF(("sending crystal calibration %d, %d\n",
   5147  1.33  christos 		    cmd.cap_pin[0], cmd.cap_pin[1]));
   5148  1.33  christos 		error = iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 0);
   5149  1.33  christos 		if (error != 0) {
   5150  1.33  christos 			aprint_error_dev(sc->sc_dev,
   5151  1.33  christos 			    "crystal calibration failed\n");
   5152  1.33  christos 			return error;
   5153  1.33  christos 		}
   5154  1.33  christos 	}
   5155  1.33  christos 	if (sc->sc_flags & IWN_FLAG_FIRST_BOOT) {
   5156  1.33  christos 		/* Query calibration from the initialization firmware. */
   5157  1.33  christos 		if ((error = iwn5000_query_calibration(sc)) != 0) {
   5158  1.33  christos 			aprint_error_dev(sc->sc_dev,
   5159  1.33  christos 			    "could not query calibration\n");
   5160  1.33  christos 			return error;
   5161  1.33  christos 		}
   5162  1.33  christos 		/*
   5163  1.33  christos 		 * We have the calibration results now so we can skip
   5164  1.33  christos 		 * loading the initialization firmware next time.
   5165  1.33  christos 		 */
   5166  1.33  christos 		sc->sc_flags &= ~IWN_FLAG_FIRST_BOOT;
   5167  1.33  christos 
   5168  1.33  christos 		/* Reboot (call ourselves recursively!) */
   5169  1.33  christos 		iwn_hw_stop(sc);
   5170  1.33  christos 		error = iwn_hw_init(sc);
   5171  1.33  christos 	} else {
   5172  1.33  christos 		/* Send calibration results to runtime firmware. */
   5173  1.33  christos 		error = iwn5000_send_calibration(sc);
   5174   1.1      ober 	}
   5175  1.33  christos 	return error;
   5176  1.33  christos }
   5177  1.33  christos 
   5178  1.33  christos /*
   5179  1.33  christos  * The firmware boot code is small and is intended to be copied directly into
   5180  1.33  christos  * the NIC internal memory (no DMA transfer.)
   5181  1.33  christos  */
   5182  1.33  christos static int
   5183  1.33  christos iwn4965_load_bootcode(struct iwn_softc *sc, const uint8_t *ucode, int size)
   5184  1.33  christos {
   5185  1.33  christos 	int error, ntries;
   5186  1.33  christos 
   5187  1.33  christos 	size /= sizeof (uint32_t);
   5188   1.1      ober 
   5189  1.33  christos 	if ((error = iwn_nic_lock(sc)) != 0)
   5190  1.33  christos 		return error;
   5191   1.1      ober 
   5192  1.33  christos 	/* Copy microcode image into NIC memory. */
   5193  1.33  christos 	iwn_prph_write_region_4(sc, IWN_BSM_SRAM_BASE,
   5194  1.33  christos 	    (const uint32_t *)ucode, size);
   5195   1.1      ober 
   5196  1.33  christos 	iwn_prph_write(sc, IWN_BSM_WR_MEM_SRC, 0);
   5197  1.33  christos 	iwn_prph_write(sc, IWN_BSM_WR_MEM_DST, IWN_FW_TEXT_BASE);
   5198  1.33  christos 	iwn_prph_write(sc, IWN_BSM_WR_DWCOUNT, size);
   5199   1.1      ober 
   5200  1.33  christos 	/* Start boot load now. */
   5201  1.33  christos 	iwn_prph_write(sc, IWN_BSM_WR_CTRL, IWN_BSM_WR_CTRL_START);
   5202   1.1      ober 
   5203  1.33  christos 	/* Wait for transfer to complete. */
   5204  1.33  christos 	for (ntries = 0; ntries < 1000; ntries++) {
   5205  1.33  christos 		if (!(iwn_prph_read(sc, IWN_BSM_WR_CTRL) &
   5206  1.33  christos 		    IWN_BSM_WR_CTRL_START))
   5207  1.33  christos 			break;
   5208  1.33  christos 		DELAY(10);
   5209  1.33  christos 	}
   5210  1.33  christos 	if (ntries == 1000) {
   5211  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
   5212  1.33  christos 		iwn_nic_unlock(sc);
   5213  1.33  christos 		return ETIMEDOUT;
   5214   1.1      ober 	}
   5215   1.1      ober 
   5216  1.33  christos 	/* Enable boot after power up. */
   5217  1.33  christos 	iwn_prph_write(sc, IWN_BSM_WR_CTRL, IWN_BSM_WR_CTRL_START_EN);
   5218   1.1      ober 
   5219  1.33  christos 	iwn_nic_unlock(sc);
   5220  1.33  christos 	return 0;
   5221  1.33  christos }
   5222   1.1      ober 
   5223  1.33  christos static int
   5224  1.33  christos iwn4965_load_firmware(struct iwn_softc *sc)
   5225  1.33  christos {
   5226  1.33  christos 	struct iwn_fw_info *fw = &sc->fw;
   5227  1.33  christos 	struct iwn_dma_info *dma = &sc->fw_dma;
   5228  1.33  christos 	int error;
   5229   1.1      ober 
   5230  1.33  christos 	/* Copy initialization sections into pre-allocated DMA-safe memory. */
   5231  1.33  christos 	memcpy(dma->vaddr, fw->init.data, fw->init.datasz);
   5232  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, dma->map, 0, fw->init.datasz,
   5233  1.33  christos 	    BUS_DMASYNC_PREWRITE);
   5234  1.33  christos 	memcpy((char *)dma->vaddr + IWN4965_FW_DATA_MAXSZ,
   5235  1.33  christos 	    fw->init.text, fw->init.textsz);
   5236  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, dma->map, IWN4965_FW_DATA_MAXSZ,
   5237  1.33  christos 	    fw->init.textsz, BUS_DMASYNC_PREWRITE);
   5238   1.1      ober 
   5239  1.33  christos 	/* Tell adapter where to find initialization sections. */
   5240  1.33  christos 	if ((error = iwn_nic_lock(sc)) != 0)
   5241  1.33  christos 		return error;
   5242  1.33  christos 	iwn_prph_write(sc, IWN_BSM_DRAM_DATA_ADDR, dma->paddr >> 4);
   5243  1.33  christos 	iwn_prph_write(sc, IWN_BSM_DRAM_DATA_SIZE, fw->init.datasz);
   5244  1.33  christos 	iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_ADDR,
   5245  1.33  christos 	    (dma->paddr + IWN4965_FW_DATA_MAXSZ) >> 4);
   5246  1.33  christos 	iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_SIZE, fw->init.textsz);
   5247  1.33  christos 	iwn_nic_unlock(sc);
   5248   1.1      ober 
   5249  1.33  christos 	/* Load firmware boot code. */
   5250  1.33  christos 	error = iwn4965_load_bootcode(sc, fw->boot.text, fw->boot.textsz);
   5251  1.33  christos 	if (error != 0) {
   5252  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
   5253  1.33  christos 		return error;
   5254  1.33  christos 	}
   5255  1.33  christos 	/* Now press "execute". */
   5256  1.33  christos 	IWN_WRITE(sc, IWN_RESET, 0);
   5257   1.1      ober 
   5258  1.33  christos 	/* Wait at most one second for first alive notification. */
   5259  1.33  christos 	if ((error = tsleep(sc, PCATCH, "iwninit", hz)) != 0) {
   5260  1.33  christos 		aprint_error_dev(sc->sc_dev,
   5261  1.33  christos 		    "timeout waiting for adapter to initialize %d\n", error);
   5262  1.33  christos 		return error;
   5263  1.33  christos 	}
   5264   1.1      ober 
   5265  1.33  christos 	/* Retrieve current temperature for initial TX power calibration. */
   5266  1.33  christos 	sc->rawtemp = sc->ucode_info.temp[3].chan20MHz;
   5267  1.33  christos 	sc->temp = iwn4965_get_temperature(sc);
   5268   1.1      ober 
   5269  1.33  christos 	/* Copy runtime sections into pre-allocated DMA-safe memory. */
   5270  1.33  christos 	memcpy(dma->vaddr, fw->main.data, fw->main.datasz);
   5271  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, dma->map, 0, fw->main.datasz,
   5272  1.33  christos 	    BUS_DMASYNC_PREWRITE);
   5273  1.33  christos 	memcpy((char *)dma->vaddr + IWN4965_FW_DATA_MAXSZ,
   5274  1.33  christos 	    fw->main.text, fw->main.textsz);
   5275  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, dma->map, IWN4965_FW_DATA_MAXSZ,
   5276  1.33  christos 	    fw->main.textsz, BUS_DMASYNC_PREWRITE);
   5277   1.1      ober 
   5278  1.33  christos 	/* Tell adapter where to find runtime sections. */
   5279  1.33  christos 	if ((error = iwn_nic_lock(sc)) != 0)
   5280  1.33  christos 		return error;
   5281  1.33  christos 	iwn_prph_write(sc, IWN_BSM_DRAM_DATA_ADDR, dma->paddr >> 4);
   5282  1.33  christos 	iwn_prph_write(sc, IWN_BSM_DRAM_DATA_SIZE, fw->main.datasz);
   5283  1.33  christos 	iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_ADDR,
   5284  1.33  christos 	    (dma->paddr + IWN4965_FW_DATA_MAXSZ) >> 4);
   5285  1.33  christos 	iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_SIZE,
   5286  1.33  christos 	    IWN_FW_UPDATED | fw->main.textsz);
   5287  1.33  christos 	iwn_nic_unlock(sc);
   5288   1.1      ober 
   5289  1.33  christos 	return 0;
   5290  1.33  christos }
   5291   1.1      ober 
   5292  1.33  christos static int
   5293  1.33  christos iwn5000_load_firmware_section(struct iwn_softc *sc, uint32_t dst,
   5294  1.33  christos     const uint8_t *section, int size)
   5295  1.33  christos {
   5296  1.33  christos 	struct iwn_dma_info *dma = &sc->fw_dma;
   5297  1.33  christos 	int error;
   5298   1.1      ober 
   5299  1.33  christos 	/* Copy firmware section into pre-allocated DMA-safe memory. */
   5300  1.33  christos 	memcpy(dma->vaddr, section, size);
   5301  1.33  christos 	bus_dmamap_sync(sc->sc_dmat, dma->map, 0, size, BUS_DMASYNC_PREWRITE);
   5302   1.1      ober 
   5303  1.33  christos 	if ((error = iwn_nic_lock(sc)) != 0)
   5304   1.1      ober 		return error;
   5305   1.1      ober 
   5306  1.33  christos 	IWN_WRITE(sc, IWN_FH_TX_CONFIG(IWN_SRVC_CHNL),
   5307  1.33  christos 	    IWN_FH_TX_CONFIG_DMA_PAUSE);
   5308   1.1      ober 
   5309  1.33  christos 	IWN_WRITE(sc, IWN_FH_SRAM_ADDR(IWN_SRVC_CHNL), dst);
   5310  1.33  christos 	IWN_WRITE(sc, IWN_FH_TFBD_CTRL0(IWN_SRVC_CHNL),
   5311  1.33  christos 	    IWN_LOADDR(dma->paddr));
   5312  1.33  christos 	IWN_WRITE(sc, IWN_FH_TFBD_CTRL1(IWN_SRVC_CHNL),
   5313  1.33  christos 	    IWN_HIADDR(dma->paddr) << 28 | size);
   5314  1.33  christos 	IWN_WRITE(sc, IWN_FH_TXBUF_STATUS(IWN_SRVC_CHNL),
   5315  1.33  christos 	    IWN_FH_TXBUF_STATUS_TBNUM(1) |
   5316  1.33  christos 	    IWN_FH_TXBUF_STATUS_TBIDX(1) |
   5317  1.33  christos 	    IWN_FH_TXBUF_STATUS_TFBD_VALID);
   5318  1.33  christos 
   5319  1.33  christos 	/* Kick Flow Handler to start DMA transfer. */
   5320  1.33  christos 	IWN_WRITE(sc, IWN_FH_TX_CONFIG(IWN_SRVC_CHNL),
   5321  1.33  christos 	    IWN_FH_TX_CONFIG_DMA_ENA | IWN_FH_TX_CONFIG_CIRQ_HOST_ENDTFD);
   5322  1.20     blymn 
   5323  1.33  christos 	iwn_nic_unlock(sc);
   5324   1.1      ober 
   5325  1.33  christos 	/* Wait at most five seconds for FH DMA transfer to complete. */
   5326  1.33  christos 	return tsleep(sc, PCATCH, "iwninit", 5 * hz);
   5327   1.1      ober }
   5328   1.1      ober 
   5329   1.1      ober static int
   5330  1.33  christos iwn5000_load_firmware(struct iwn_softc *sc)
   5331   1.1      ober {
   5332  1.33  christos 	struct iwn_fw_part *fw;
   5333   1.1      ober 	int error;
   5334   1.1      ober 
   5335  1.33  christos 	/* Load the initialization firmware on first boot only. */
   5336  1.33  christos 	fw = (sc->sc_flags & IWN_FLAG_FIRST_BOOT) ?
   5337  1.33  christos 	    &sc->fw.init : &sc->fw.main;
   5338  1.33  christos 
   5339  1.33  christos 	error = iwn5000_load_firmware_section(sc, IWN_FW_TEXT_BASE,
   5340  1.33  christos 	    fw->text, fw->textsz);
   5341  1.33  christos 	if (error != 0) {
   5342  1.33  christos 		aprint_error_dev(sc->sc_dev,
   5343  1.33  christos 		    "could not load firmware %s section\n",
   5344  1.33  christos 		    ".text");
   5345  1.33  christos 		return error;
   5346  1.33  christos 	}
   5347  1.33  christos 	error = iwn5000_load_firmware_section(sc, IWN_FW_DATA_BASE,
   5348  1.33  christos 	    fw->data, fw->datasz);
   5349   1.1      ober 	if (error != 0) {
   5350  1.33  christos 		aprint_error_dev(sc->sc_dev,
   5351  1.33  christos 		    "could not load firmware %s section\n",
   5352  1.33  christos 		    ".data");
   5353   1.1      ober 		return error;
   5354   1.1      ober 	}
   5355   1.1      ober 
   5356  1.33  christos 	/* Now press "execute". */
   5357  1.33  christos 	IWN_WRITE(sc, IWN_RESET, 0);
   5358  1.33  christos 	return 0;
   5359  1.33  christos }
   5360  1.33  christos 
   5361  1.33  christos static int
   5362  1.33  christos iwn_read_firmware(struct iwn_softc *sc)
   5363  1.33  christos {
   5364  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   5365  1.33  christos 	struct iwn_fw_info *fw = &sc->fw;
   5366  1.33  christos 	struct iwn_firmware_hdr hdr;
   5367  1.33  christos 	firmware_handle_t fwh;
   5368  1.33  christos 	size_t size;
   5369  1.33  christos 	int error;
   5370  1.33  christos 
   5371  1.33  christos 	/* Read firmware image from filesystem. */
   5372  1.33  christos 	if ((error = firmware_open("if_iwn", sc->fwname, &fwh)) != 0) {
   5373  1.33  christos 		aprint_error_dev(sc->sc_dev,
   5374  1.33  christos 		    "could not read firmware file %s\n", sc->fwname);
   5375   1.1      ober 		return error;
   5376   1.1      ober 	}
   5377  1.33  christos 	size = firmware_get_size(fwh);
   5378  1.33  christos 	if (size < sizeof (hdr)) {
   5379  1.33  christos 		aprint_error_dev(sc->sc_dev,
   5380  1.33  christos 		    "truncated firmware header: %zu bytes\n", size);
   5381  1.33  christos 		error = EINVAL;
   5382  1.33  christos 		goto fail2;
   5383  1.33  christos 	}
   5384  1.33  christos 	/* Extract firmware header information. */
   5385  1.33  christos 	if ((error = firmware_read(fwh, 0, &hdr,
   5386  1.33  christos 	    sizeof (struct iwn_firmware_hdr))) != 0) {
   5387  1.33  christos 		aprint_error_dev(sc->sc_dev, "can't get firmware header\n");
   5388  1.33  christos 		goto fail2;
   5389  1.33  christos 	}
   5390  1.33  christos 	fw->main.textsz = le32toh(hdr.main_textsz);
   5391  1.33  christos 	fw->main.datasz = le32toh(hdr.main_datasz);
   5392  1.33  christos 	fw->init.textsz = le32toh(hdr.init_textsz);
   5393  1.33  christos 	fw->init.datasz = le32toh(hdr.init_datasz);
   5394  1.33  christos 	fw->boot.textsz = le32toh(hdr.boot_textsz);
   5395  1.33  christos 	fw->boot.datasz = 0;
   5396  1.33  christos 
   5397  1.33  christos 	/* Sanity-check firmware header. */
   5398  1.33  christos 	if (fw->main.textsz > hal->fw_text_maxsz ||
   5399  1.33  christos 	    fw->main.datasz > hal->fw_data_maxsz ||
   5400  1.33  christos 	    fw->init.textsz > hal->fw_text_maxsz ||
   5401  1.33  christos 	    fw->init.datasz > hal->fw_data_maxsz ||
   5402  1.33  christos 	    fw->boot.textsz > IWN_FW_BOOT_TEXT_MAXSZ ||
   5403  1.33  christos 	    (fw->boot.textsz & 3) != 0) {
   5404  1.33  christos 		aprint_error_dev(sc->sc_dev, "invalid firmware header\n");
   5405  1.33  christos 		error = EINVAL;
   5406  1.33  christos 		goto fail2;
   5407  1.33  christos 	}
   5408   1.1      ober 
   5409  1.33  christos 	/* Check that all firmware sections fit. */
   5410  1.33  christos 	if (size < sizeof (hdr) + fw->main.textsz + fw->main.datasz +
   5411  1.33  christos 	    fw->init.textsz + fw->init.datasz + fw->boot.textsz) {
   5412  1.33  christos 		aprint_error_dev(sc->sc_dev,
   5413  1.33  christos 		    "firmware file too short: %zu bytes\n", size);
   5414  1.33  christos 		error = EINVAL;
   5415  1.33  christos 		goto fail2;
   5416   1.1      ober 	}
   5417  1.33  christos 	fw->data = firmware_malloc(size);
   5418  1.33  christos 	if (fw->data == NULL) {
   5419  1.33  christos 		aprint_error_dev(sc->sc_dev,
   5420  1.33  christos 		    "not enough memory to stock firmware\n");
   5421  1.33  christos 		error = ENOMEM;
   5422  1.33  christos 		goto fail2;
   5423   1.1      ober 	}
   5424  1.33  christos 	if ((error = firmware_read(fwh, 0, fw->data, size)) != 0) {
   5425  1.33  christos 		aprint_error_dev(sc->sc_dev, "can't get firmware\n");
   5426  1.33  christos 		goto fail3;
   5427   1.1      ober 	}
   5428   1.1      ober 
   5429  1.33  christos 	/* Get pointers to firmware sections. */
   5430  1.33  christos 	fw->main.text = fw->data + sizeof (struct iwn_firmware_hdr);
   5431  1.33  christos 	fw->main.data = fw->main.text + fw->main.textsz;
   5432  1.33  christos 	fw->init.text = fw->main.data + fw->main.datasz;
   5433  1.33  christos 	fw->init.data = fw->init.text + fw->init.textsz;
   5434  1.33  christos 	fw->boot.text = fw->init.data + fw->init.datasz;
   5435  1.33  christos 
   5436  1.33  christos 	return 0;
   5437  1.33  christos fail3:	firmware_free(fw->data, size);
   5438  1.33  christos fail2:	firmware_close(fwh);
   5439  1.33  christos 	return error;
   5440  1.33  christos }
   5441  1.33  christos 
   5442  1.33  christos static int
   5443  1.33  christos iwn_clock_wait(struct iwn_softc *sc)
   5444  1.33  christos {
   5445  1.33  christos 	int ntries;
   5446  1.33  christos 
   5447  1.33  christos 	/* Set "initialization complete" bit. */
   5448  1.33  christos 	IWN_SETBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_INIT_DONE);
   5449  1.33  christos 
   5450  1.33  christos 	/* Wait for clock stabilization. */
   5451  1.33  christos 	for (ntries = 0; ntries < 25000; ntries++) {
   5452  1.33  christos 		if (IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_MAC_CLOCK_READY)
   5453  1.33  christos 			return 0;
   5454  1.33  christos 		DELAY(100);
   5455   1.1      ober 	}
   5456  1.33  christos 	aprint_error_dev(sc->sc_dev,
   5457  1.33  christos 	    "timeout waiting for clock stabilization\n");
   5458  1.33  christos 	return ETIMEDOUT;
   5459  1.33  christos }
   5460  1.33  christos 
   5461  1.33  christos static int
   5462  1.33  christos iwn4965_apm_init(struct iwn_softc *sc)
   5463  1.33  christos {
   5464  1.33  christos 	int error;
   5465  1.33  christos 
   5466  1.33  christos 	/* Disable L0s. */
   5467  1.33  christos 	IWN_SETBITS(sc, IWN_GIO_CHICKEN, IWN_GIO_CHICKEN_DIS_L0S_TIMER);
   5468  1.33  christos 	IWN_SETBITS(sc, IWN_GIO_CHICKEN, IWN_GIO_CHICKEN_L1A_NO_L0S_RX);
   5469   1.1      ober 
   5470  1.33  christos 	if ((error = iwn_clock_wait(sc)) != 0)
   5471   1.1      ober 		return error;
   5472   1.1      ober 
   5473  1.33  christos 	if ((error = iwn_nic_lock(sc)) != 0)
   5474   1.1      ober 		return error;
   5475  1.33  christos 	/* Enable DMA. */
   5476  1.33  christos 	iwn_prph_write(sc, IWN_APMG_CLK_CTRL,
   5477  1.33  christos 	    IWN_APMG_CLK_CTRL_DMA_CLK_RQT | IWN_APMG_CLK_CTRL_BSM_CLK_RQT);
   5478  1.33  christos 	DELAY(20);
   5479  1.33  christos 	/* Disable L1. */
   5480  1.33  christos 	iwn_prph_setbits(sc, IWN_APMG_PCI_STT, IWN_APMG_PCI_STT_L1A_DIS);
   5481  1.33  christos 	iwn_nic_unlock(sc);
   5482   1.1      ober 
   5483   1.1      ober 	return 0;
   5484   1.1      ober }
   5485   1.1      ober 
   5486  1.33  christos static int
   5487  1.33  christos iwn5000_apm_init(struct iwn_softc *sc)
   5488   1.1      ober {
   5489  1.33  christos 	int error;
   5490   1.1      ober 
   5491  1.33  christos 	/* Disable L0s. */
   5492  1.33  christos 	IWN_SETBITS(sc, IWN_GIO_CHICKEN, IWN_GIO_CHICKEN_DIS_L0S_TIMER);
   5493  1.33  christos 	IWN_SETBITS(sc, IWN_GIO_CHICKEN, IWN_GIO_CHICKEN_L1A_NO_L0S_RX);
   5494   1.1      ober 
   5495  1.33  christos 	/* Set Flow Handler wait threshold to the maximum. */
   5496  1.33  christos 	IWN_SETBITS(sc, IWN_DBG_HPET_MEM, 0xffff0000);
   5497   1.1      ober 
   5498  1.33  christos 	/* Enable HAP to move adapter from L1a to L0s. */
   5499  1.33  christos 	IWN_SETBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_HAP_WAKE_L1A);
   5500   1.1      ober 
   5501  1.33  christos 	if (sc->hw_type != IWN_HW_REV_TYPE_6000 &&
   5502  1.33  christos 	    sc->hw_type != IWN_HW_REV_TYPE_6050)
   5503  1.33  christos 		IWN_SETBITS(sc, IWN_ANA_PLL, IWN_ANA_PLL_INIT);
   5504   1.1      ober 
   5505  1.33  christos 	if ((error = iwn_clock_wait(sc)) != 0)
   5506  1.33  christos 	return error;
   5507   1.1      ober 
   5508  1.33  christos 	if ((error = iwn_nic_lock(sc)) != 0)
   5509  1.33  christos 		return error;
   5510  1.33  christos 	/* Enable DMA. */
   5511  1.33  christos 	iwn_prph_write(sc, IWN_APMG_CLK_CTRL, IWN_APMG_CLK_CTRL_DMA_CLK_RQT);
   5512  1.33  christos 	DELAY(20);
   5513  1.33  christos 	/* Disable L1. */
   5514  1.33  christos 	iwn_prph_setbits(sc, IWN_APMG_PCI_STT, IWN_APMG_PCI_STT_L1A_DIS);
   5515  1.33  christos 	iwn_nic_unlock(sc);
   5516   1.1      ober 
   5517  1.33  christos 	return 0;
   5518   1.1      ober }
   5519   1.1      ober 
   5520   1.1      ober static void
   5521  1.33  christos iwn_apm_stop_master(struct iwn_softc *sc)
   5522   1.1      ober {
   5523   1.1      ober 	int ntries;
   5524   1.1      ober 
   5525  1.33  christos 	IWN_SETBITS(sc, IWN_RESET, IWN_RESET_STOP_MASTER);
   5526   1.1      ober 	for (ntries = 0; ntries < 100; ntries++) {
   5527  1.33  christos 		if (IWN_READ(sc, IWN_RESET) & IWN_RESET_MASTER_DISABLED)
   5528  1.33  christos 			return;
   5529   1.1      ober 		DELAY(10);
   5530   1.1      ober 	}
   5531  1.33  christos 	aprint_error_dev(sc->sc_dev, "timeout waiting for master\n");
   5532   1.1      ober }
   5533   1.1      ober 
   5534  1.33  christos static void
   5535  1.33  christos iwn_apm_stop(struct iwn_softc *sc)
   5536   1.1      ober {
   5537  1.33  christos 	iwn_apm_stop_master(sc);
   5538   1.1      ober 
   5539  1.33  christos 	IWN_SETBITS(sc, IWN_RESET, IWN_RESET_SW);
   5540  1.33  christos 	DELAY(10);
   5541  1.33  christos 	/* Clear "initialization complete" bit. */
   5542  1.33  christos 	IWN_CLRBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_INIT_DONE);
   5543  1.33  christos }
   5544   1.1      ober 
   5545  1.33  christos static int
   5546  1.33  christos iwn4965_nic_config(struct iwn_softc *sc)
   5547  1.33  christos {
   5548  1.33  christos 	pcireg_t reg;
   5549   1.1      ober 
   5550  1.33  christos 	/* Retrieve PCIe Active State Power Management (ASPM). */
   5551  1.33  christos 	reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
   5552  1.33  christos 	    sc->sc_cap_off + PCI_PCIE_LCSR);
   5553  1.33  christos 	if (reg & PCI_PCIE_LCSR_ASPM_L1)	/* L1 Entry enabled. */
   5554  1.33  christos 		IWN_SETBITS(sc, IWN_GIO, IWN_GIO_L0S_ENA);
   5555  1.33  christos 	else
   5556  1.33  christos 		IWN_CLRBITS(sc, IWN_GIO, IWN_GIO_L0S_ENA);
   5557   1.1      ober 
   5558  1.33  christos 	if (IWN_RFCFG_TYPE(sc->rfcfg) == 1) {
   5559  1.33  christos 		/*
   5560  1.33  christos 		 * I don't believe this to be correct but this is what the
   5561  1.33  christos 		 * vendor driver is doing. Probably the bits should not be
   5562  1.33  christos 		 * shifted in IWN_RFCFG_*.
   5563  1.33  christos 		 */
   5564  1.33  christos 		IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
   5565  1.33  christos 		    IWN_RFCFG_TYPE(sc->rfcfg) |
   5566  1.33  christos 		    IWN_RFCFG_STEP(sc->rfcfg) |
   5567  1.33  christos 		    IWN_RFCFG_DASH(sc->rfcfg));
   5568   1.1      ober 	}
   5569  1.33  christos 	IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
   5570  1.33  christos 	    IWN_HW_IF_CONFIG_RADIO_SI | IWN_HW_IF_CONFIG_MAC_SI);
   5571   1.1      ober 	return 0;
   5572   1.1      ober }
   5573   1.1      ober 
   5574  1.33  christos static int
   5575  1.33  christos iwn5000_nic_config(struct iwn_softc *sc)
   5576   1.1      ober {
   5577  1.33  christos 	int error;
   5578  1.33  christos 	pcireg_t reg;
   5579  1.33  christos 
   5580  1.33  christos 	/* Retrieve PCIe Active State Power Management (ASPM). */
   5581  1.33  christos 	reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
   5582  1.33  christos 	    sc->sc_cap_off + PCI_PCIE_LCSR);
   5583  1.33  christos 	if (reg & PCI_PCIE_LCSR_ASPM_L1)	/* L1 Entry enabled. */
   5584  1.33  christos 		IWN_SETBITS(sc, IWN_GIO, IWN_GIO_L0S_ENA);
   5585  1.33  christos 	else
   5586  1.33  christos 		IWN_CLRBITS(sc, IWN_GIO, IWN_GIO_L0S_ENA);
   5587   1.1      ober 
   5588  1.33  christos 	if (IWN_RFCFG_TYPE(sc->rfcfg) < 3) {
   5589  1.33  christos 		IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
   5590  1.33  christos 		    IWN_RFCFG_TYPE(sc->rfcfg) |
   5591  1.33  christos 		    IWN_RFCFG_STEP(sc->rfcfg) |
   5592  1.33  christos 		    IWN_RFCFG_DASH(sc->rfcfg));
   5593  1.33  christos 	}
   5594  1.33  christos 	IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
   5595  1.33  christos 	    IWN_HW_IF_CONFIG_RADIO_SI | IWN_HW_IF_CONFIG_MAC_SI);
   5596   1.1      ober 
   5597  1.33  christos 	if ((error = iwn_nic_lock(sc)) != 0)
   5598  1.33  christos 		return error;
   5599  1.33  christos 	iwn_prph_setbits(sc, IWN_APMG_PS, IWN_APMG_PS_EARLY_PWROFF_DIS);
   5600  1.33  christos 	iwn_nic_unlock(sc);
   5601  1.33  christos 	return 0;
   5602   1.1      ober }
   5603   1.1      ober 
   5604   1.1      ober static int
   5605  1.33  christos iwn_hw_init(struct iwn_softc *sc)
   5606   1.1      ober {
   5607  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   5608   1.1      ober 	int error, qid;
   5609   1.1      ober 
   5610  1.33  christos 	/* Clear pending interrupts. */
   5611  1.33  christos 	IWN_WRITE(sc, IWN_INT, 0xffffffff);
   5612  1.33  christos 
   5613  1.33  christos 	if ((error = hal->apm_init(sc)) != 0) {
   5614  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not power ON adapter\n");
   5615  1.33  christos 		return error;
   5616   1.1      ober 	}
   5617   1.1      ober 
   5618  1.33  christos 	/* Select VMAIN power source. */
   5619  1.33  christos 	if ((error = iwn_nic_lock(sc)) != 0)
   5620  1.33  christos 		return error;
   5621  1.33  christos 	iwn_prph_clrbits(sc, IWN_APMG_PS, IWN_APMG_PS_PWR_SRC_MASK);
   5622  1.33  christos 	iwn_nic_unlock(sc);
   5623  1.33  christos 
   5624  1.33  christos 	/* Perform adapter-specific initialization. */
   5625  1.33  christos 	if ((error = hal->nic_config(sc)) != 0)
   5626  1.33  christos 		return error;
   5627   1.1      ober 
   5628  1.33  christos 	/* Initialize RX ring. */
   5629  1.33  christos 	if ((error = iwn_nic_lock(sc)) != 0)
   5630  1.33  christos 		return error;
   5631  1.33  christos 	IWN_WRITE(sc, IWN_FH_RX_CONFIG, 0);
   5632  1.33  christos 	IWN_WRITE(sc, IWN_FH_RX_WPTR, 0);
   5633  1.33  christos 	/* Set physical address of RX ring (256-byte aligned.) */
   5634  1.33  christos 	IWN_WRITE(sc, IWN_FH_RX_BASE, sc->rxq.desc_dma.paddr >> 8);
   5635  1.33  christos 	/* Set physical address of RX status (16-byte aligned.) */
   5636  1.33  christos 	IWN_WRITE(sc, IWN_FH_STATUS_WPTR, sc->rxq.stat_dma.paddr >> 4);
   5637  1.33  christos 	/* Enable RX. */
   5638  1.33  christos 	IWN_WRITE(sc, IWN_FH_RX_CONFIG,
   5639  1.33  christos 	    IWN_FH_RX_CONFIG_ENA	   |
   5640  1.33  christos 	    IWN_FH_RX_CONFIG_IGN_RXF_EMPTY |	/* HW bug workaround */
   5641  1.33  christos 	    IWN_FH_RX_CONFIG_IRQ_DST_HOST  |
   5642  1.33  christos 	    IWN_FH_RX_CONFIG_SINGLE_FRAME  |
   5643  1.33  christos 	    IWN_FH_RX_CONFIG_RB_TIMEOUT(0) |
   5644  1.33  christos 	    IWN_FH_RX_CONFIG_NRBD(IWN_RX_RING_COUNT_LOG));
   5645  1.33  christos 	iwn_nic_unlock(sc);
   5646  1.33  christos 	IWN_WRITE(sc, IWN_FH_RX_WPTR, (IWN_RX_RING_COUNT - 1) & ~7);
   5647   1.1      ober 
   5648  1.33  christos 	if ((error = iwn_nic_lock(sc)) != 0)
   5649  1.33  christos 		return error;
   5650   1.1      ober 
   5651  1.33  christos 	/* Initialize TX scheduler. */
   5652  1.33  christos 	iwn_prph_write(sc, hal->sched_txfact_addr, 0);
   5653   1.1      ober 
   5654  1.33  christos 	/* Set physical address of "keep warm" page (16-byte aligned.) */
   5655  1.33  christos 	IWN_WRITE(sc, IWN_FH_KW_ADDR, sc->kw_dma.paddr >> 4);
   5656   1.1      ober 
   5657  1.33  christos 	/* Initialize TX rings. */
   5658  1.33  christos 	for (qid = 0; qid < hal->ntxqs; qid++) {
   5659   1.1      ober 		struct iwn_tx_ring *txq = &sc->txq[qid];
   5660  1.33  christos 
   5661  1.33  christos 		/* Set physical address of TX ring (256-byte aligned.) */
   5662  1.33  christos 		IWN_WRITE(sc, IWN_FH_CBBC_QUEUE(qid),
   5663  1.33  christos 		    txq->desc_dma.paddr >> 8);
   5664  1.33  christos 		/* Enable TX for this ring. */
   5665  1.33  christos 		IWN_WRITE(sc, IWN_FH_TX_CONFIG(qid),
   5666  1.33  christos 		    IWN_FH_TX_CONFIG_DMA_ENA |
   5667  1.33  christos 		    IWN_FH_TX_CONFIG_DMA_CREDIT_ENA);
   5668  1.33  christos 	}
   5669  1.33  christos 	iwn_nic_unlock(sc);
   5670  1.33  christos 
   5671  1.33  christos 	/* Clear "radio off" and "commands blocked" bits. */
   5672  1.33  christos 	IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_RFKILL);
   5673  1.33  christos 	IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_CMD_BLOCKED);
   5674  1.33  christos 
   5675  1.33  christos 	/* Clear pending interrupts. */
   5676  1.33  christos 	IWN_WRITE(sc, IWN_INT, 0xffffffff);
   5677  1.33  christos 	/* Enable interrupt coalescing. */
   5678  1.33  christos 	IWN_WRITE(sc, IWN_INT_COALESCING, 512 / 8);
   5679  1.33  christos 	/* Enable interrupts. */
   5680  1.33  christos 	IWN_WRITE(sc, IWN_MASK, IWN_INT_MASK);
   5681  1.33  christos 
   5682  1.33  christos 	/* _Really_ make sure "radio off" bit is cleared! */
   5683  1.33  christos 	IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_RFKILL);
   5684  1.33  christos 	IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_RFKILL);
   5685  1.33  christos 
   5686  1.33  christos 	if ((error = hal->load_firmware(sc)) != 0) {
   5687  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not load firmware\n");
   5688  1.33  christos 		return error;
   5689  1.33  christos 	}
   5690  1.33  christos 	/* Wait at most one second for firmware alive notification. */
   5691  1.33  christos 	if ((error = tsleep(sc, PCATCH, "iwninit", hz)) != 0) {
   5692  1.33  christos 		aprint_error_dev(sc->sc_dev,
   5693  1.33  christos 		    "timeout waiting for adapter to initialize %d\n" ,error);
   5694  1.33  christos 		return error;
   5695  1.33  christos 	}
   5696  1.33  christos 	/* Do post-firmware initialization. */
   5697  1.33  christos 	return hal->post_alive(sc);
   5698  1.33  christos }
   5699  1.33  christos 
   5700  1.33  christos static void
   5701  1.33  christos iwn_hw_stop(struct iwn_softc *sc)
   5702  1.33  christos {
   5703  1.33  christos 	const struct iwn_hal *hal = sc->sc_hal;
   5704  1.33  christos 	int qid;
   5705  1.33  christos 
   5706  1.33  christos 	IWN_WRITE(sc, IWN_RESET, IWN_RESET_NEVO);
   5707  1.33  christos 
   5708  1.33  christos 	/* Disable interrupts. */
   5709  1.33  christos 	IWN_WRITE(sc, IWN_MASK, 0);
   5710  1.33  christos 	IWN_WRITE(sc, IWN_INT, 0xffffffff);
   5711  1.33  christos 	IWN_WRITE(sc, IWN_FH_INT, 0xffffffff);
   5712  1.33  christos 
   5713  1.33  christos 	/* Make sure we no longer hold the NIC lock. */
   5714  1.33  christos 	iwn_nic_unlock(sc);
   5715  1.33  christos 
   5716  1.33  christos 	/* Stop TX scheduler. */
   5717  1.33  christos 	iwn_prph_write(sc, hal->sched_txfact_addr, 0);
   5718  1.33  christos 
   5719  1.33  christos 	/* Stop all TX rings. */
   5720  1.33  christos 	for (qid = 0; qid < hal->ntxqs; qid++)
   5721  1.33  christos 		iwn_reset_tx_ring(sc, &sc->txq[qid]);
   5722  1.33  christos 
   5723  1.33  christos 	/* Stop RX ring. */
   5724  1.33  christos 	iwn_reset_rx_ring(sc, &sc->rxq);
   5725  1.33  christos 
   5726  1.33  christos 	if (iwn_nic_lock(sc) == 0) {
   5727  1.33  christos 		iwn_prph_write(sc, IWN_APMG_CLK_DIS, IWN_APMG_CLK_DMA_RQT);
   5728  1.33  christos 		iwn_nic_unlock(sc);
   5729   1.1      ober 	}
   5730  1.33  christos 	DELAY(5);
   5731  1.33  christos 	/* Power OFF adapter. */
   5732  1.33  christos 	iwn_apm_stop(sc);
   5733  1.33  christos }
   5734  1.33  christos 
   5735  1.33  christos static int
   5736  1.33  christos iwn_init(struct ifnet *ifp)
   5737  1.33  christos {
   5738  1.33  christos 	struct iwn_softc *sc = ifp->if_softc;
   5739  1.33  christos 	struct ieee80211com *ic = &sc->sc_ic;
   5740  1.33  christos 	int error;
   5741   1.1      ober 
   5742  1.33  christos 	/* Check that the radio is not disabled by hardware switch. */
   5743  1.33  christos 	if (!(IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_RFKILL)) {
   5744  1.33  christos 		aprint_error_dev(sc->sc_dev,
   5745  1.33  christos 		    "radio is disabled by hardware switch\n");
   5746  1.28     blymn 		sc->sc_radio = false;
   5747  1.33  christos 		error = EPERM;	/* :-) */
   5748  1.33  christos 		goto fail;
   5749   1.1      ober 	}
   5750  1.28     blymn 	sc->sc_radio = true;
   5751  1.28     blymn 
   5752  1.33  christos 	/* Read firmware images from the filesystem. */
   5753  1.33  christos 	if ((error = iwn_read_firmware(sc)) != 0) {
   5754  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not read firmware\n");
   5755  1.33  christos 		goto fail;
   5756   1.1      ober 	}
   5757   1.1      ober 
   5758  1.33  christos 	/* Initialize hardware and upload firmware. */
   5759  1.33  christos 	error = iwn_hw_init(sc);
   5760  1.33  christos 	free(sc->fw.data, M_DEVBUF);
   5761  1.33  christos 	if (error != 0) {
   5762  1.33  christos 		aprint_error_dev(sc->sc_dev, "could not initialize hardware\n");
   5763  1.33  christos 		goto fail;
   5764  1.33  christos 	}
   5765   1.8     blymn 
   5766  1.33  christos 	/* Configure adapter now that it is ready. */
   5767   1.1      ober 	if ((error = iwn_config(sc)) != 0) {
   5768   1.1      ober 		aprint_error_dev(sc->sc_dev, "could not configure device\n");
   5769  1.33  christos 		goto fail;
   5770   1.1      ober 	}
   5771   1.1      ober 
   5772   1.1      ober 	ifp->if_flags &= ~IFF_OACTIVE;
   5773   1.1      ober 	ifp->if_flags |= IFF_RUNNING;
   5774   1.1      ober 
   5775   1.1      ober 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   5776  1.33  christos 		if (ic->ic_opmode != IEEE80211_ROAMING_MANUAL)
   5777   1.1      ober 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   5778  1.33  christos 	} else
   5779   1.1      ober 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   5780   1.1      ober 
   5781   1.1      ober 	return 0;
   5782   1.1      ober 
   5783  1.33  christos fail:	iwn_stop(ifp, 1);
   5784   1.1      ober 	return error;
   5785   1.1      ober }
   5786   1.1      ober 
   5787   1.1      ober static void
   5788   1.1      ober iwn_stop(struct ifnet *ifp, int disable)
   5789   1.1      ober {
   5790   1.1      ober 	struct iwn_softc *sc = ifp->if_softc;
   5791   1.1      ober 	struct ieee80211com *ic = &sc->sc_ic;
   5792   1.1      ober 
   5793   1.1      ober 	ifp->if_timer = sc->sc_tx_timer = 0;
   5794   1.1      ober 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   5795   1.1      ober 
   5796   1.1      ober 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   5797   1.1      ober 
   5798  1.33  christos 	/* Power OFF hardware. */
   5799  1.33  christos 	iwn_hw_stop(sc);
   5800   1.1      ober 
   5801  1.33  christos #if 0
   5802  1.33  christos 	/* Temperature sensor is no longer valid. */
   5803  1.33  christos 	sc->sensor.value = 0;
   5804  1.33  christos 	sc->sensor.flags |= SENSOR_FINVALID;
   5805  1.33  christos #endif
   5806   1.1      ober }
   5807   1.1      ober 
   5808   1.1      ober static bool
   5809  1.37    dyoung iwn_resume(device_t dv, const pmf_qual_t *qual)
   5810   1.1      ober {
   5811  1.33  christos #if 0
   5812   1.1      ober 	struct iwn_softc *sc = device_private(dv);
   5813   1.1      ober 
   5814   1.1      ober 	(void)iwn_reset(sc);
   5815  1.33  christos #endif
   5816   1.1      ober 
   5817   1.1      ober 	return true;
   5818   1.1      ober }
   5819