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