2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/dev/netif/bwi/if_bwi.c,v 1.19 2008/02/15 11:15:38 sephe Exp $
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/endian.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
54 #include <sys/taskqueue.h>
57 #include <net/if_dl.h>
58 #include <net/if_media.h>
59 #include <net/if_types.h>
60 #include <net/if_arp.h>
61 #include <net/ethernet.h>
62 #include <net/if_llc.h>
64 #include <net80211/ieee80211_var.h>
65 #include <net80211/ieee80211_radiotap.h>
66 #include <net80211/ieee80211_regdomain.h>
67 #include <net80211/ieee80211_phy.h>
68 #include <net80211/ieee80211_ratectl.h>
73 #include <netinet/in.h>
74 #include <netinet/if_ether.h>
77 #include <machine/bus.h>
79 #include <dev/pci/pcivar.h>
80 #include <dev/pci/pcireg.h>
82 #include <dev/bwi/bitops.h>
83 #include <dev/bwi/if_bwireg.h>
84 #include <dev/bwi/if_bwivar.h>
85 #include <dev/bwi/bwimac.h>
86 #include <dev/bwi/bwirf.h>
88 struct bwi_clock_freq {
93 struct bwi_myaddr_bssid {
94 uint8_t myaddr[IEEE80211_ADDR_LEN];
95 uint8_t bssid[IEEE80211_ADDR_LEN];
98 static struct ieee80211vap *bwi_vap_create(struct ieee80211com *,
99 const char [IFNAMSIZ], int, int, int,
100 const uint8_t [IEEE80211_ADDR_LEN],
101 const uint8_t [IEEE80211_ADDR_LEN]);
102 static void bwi_vap_delete(struct ieee80211vap *);
103 static void bwi_init(void *);
104 static int bwi_ioctl(struct ifnet *, u_long, caddr_t);
105 static void bwi_start(struct ifnet *);
106 static void bwi_start_locked(struct ifnet *);
107 static int bwi_raw_xmit(struct ieee80211_node *, struct mbuf *,
108 const struct ieee80211_bpf_params *);
109 static void bwi_watchdog(struct ifnet *);
110 static void bwi_scan_start(struct ieee80211com *);
111 static void bwi_set_channel(struct ieee80211com *);
112 static void bwi_scan_end(struct ieee80211com *);
113 static int bwi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
114 static void bwi_updateslot(struct ifnet *);
115 static int bwi_media_change(struct ifnet *);
117 static void bwi_calibrate(void *);
119 static int bwi_calc_rssi(struct bwi_softc *, const struct bwi_rxbuf_hdr *);
120 static int bwi_calc_noise(struct bwi_softc *);
121 static __inline uint8_t bwi_ofdm_plcp2rate(const uint32_t *);
122 static __inline uint8_t bwi_ds_plcp2rate(const struct ieee80211_ds_plcp_hdr *);
123 static void bwi_rx_radiotap(struct bwi_softc *, struct mbuf *,
124 struct bwi_rxbuf_hdr *, const void *, int, int, int);
126 static void bwi_restart(void *, int);
127 static void bwi_init_statechg(struct bwi_softc *, int);
128 static void bwi_stop(struct bwi_softc *, int);
129 static void bwi_stop_locked(struct bwi_softc *, int);
130 static int bwi_newbuf(struct bwi_softc *, int, int);
131 static int bwi_encap(struct bwi_softc *, int, struct mbuf *,
132 struct ieee80211_node *);
133 static int bwi_encap_raw(struct bwi_softc *, int, struct mbuf *,
134 struct ieee80211_node *,
135 const struct ieee80211_bpf_params *);
137 static void bwi_init_rxdesc_ring32(struct bwi_softc *, uint32_t,
138 bus_addr_t, int, int);
139 static void bwi_reset_rx_ring32(struct bwi_softc *, uint32_t);
141 static int bwi_init_tx_ring32(struct bwi_softc *, int);
142 static int bwi_init_rx_ring32(struct bwi_softc *);
143 static int bwi_init_txstats32(struct bwi_softc *);
144 static void bwi_free_tx_ring32(struct bwi_softc *, int);
145 static void bwi_free_rx_ring32(struct bwi_softc *);
146 static void bwi_free_txstats32(struct bwi_softc *);
147 static void bwi_setup_rx_desc32(struct bwi_softc *, int, bus_addr_t, int);
148 static void bwi_setup_tx_desc32(struct bwi_softc *, struct bwi_ring_data *,
149 int, bus_addr_t, int);
150 static int bwi_rxeof32(struct bwi_softc *);
151 static void bwi_start_tx32(struct bwi_softc *, uint32_t, int);
152 static void bwi_txeof_status32(struct bwi_softc *);
154 static int bwi_init_tx_ring64(struct bwi_softc *, int);
155 static int bwi_init_rx_ring64(struct bwi_softc *);
156 static int bwi_init_txstats64(struct bwi_softc *);
157 static void bwi_free_tx_ring64(struct bwi_softc *, int);
158 static void bwi_free_rx_ring64(struct bwi_softc *);
159 static void bwi_free_txstats64(struct bwi_softc *);
160 static void bwi_setup_rx_desc64(struct bwi_softc *, int, bus_addr_t, int);
161 static void bwi_setup_tx_desc64(struct bwi_softc *, struct bwi_ring_data *,
162 int, bus_addr_t, int);
163 static int bwi_rxeof64(struct bwi_softc *);
164 static void bwi_start_tx64(struct bwi_softc *, uint32_t, int);
165 static void bwi_txeof_status64(struct bwi_softc *);
167 static int bwi_rxeof(struct bwi_softc *, int);
168 static void _bwi_txeof(struct bwi_softc *, uint16_t, int, int);
169 static void bwi_txeof(struct bwi_softc *);
170 static void bwi_txeof_status(struct bwi_softc *, int);
171 static void bwi_enable_intrs(struct bwi_softc *, uint32_t);
172 static void bwi_disable_intrs(struct bwi_softc *, uint32_t);
174 static int bwi_dma_alloc(struct bwi_softc *);
175 static void bwi_dma_free(struct bwi_softc *);
176 static int bwi_dma_ring_alloc(struct bwi_softc *, bus_dma_tag_t,
177 struct bwi_ring_data *, bus_size_t,
179 static int bwi_dma_mbuf_create(struct bwi_softc *);
180 static void bwi_dma_mbuf_destroy(struct bwi_softc *, int, int);
181 static int bwi_dma_txstats_alloc(struct bwi_softc *, uint32_t, bus_size_t);
182 static void bwi_dma_txstats_free(struct bwi_softc *);
183 static void bwi_dma_ring_addr(void *, bus_dma_segment_t *, int, int);
184 static void bwi_dma_buf_addr(void *, bus_dma_segment_t *, int,
187 static void bwi_power_on(struct bwi_softc *, int);
188 static int bwi_power_off(struct bwi_softc *, int);
189 static int bwi_set_clock_mode(struct bwi_softc *, enum bwi_clock_mode);
190 static int bwi_set_clock_delay(struct bwi_softc *);
191 static void bwi_get_clock_freq(struct bwi_softc *, struct bwi_clock_freq *);
192 static int bwi_get_pwron_delay(struct bwi_softc *sc);
193 static void bwi_set_addr_filter(struct bwi_softc *, uint16_t,
195 static void bwi_set_bssid(struct bwi_softc *, const uint8_t *);
197 static void bwi_get_card_flags(struct bwi_softc *);
198 static void bwi_get_eaddr(struct bwi_softc *, uint16_t, uint8_t *);
200 static int bwi_bus_attach(struct bwi_softc *);
201 static int bwi_bbp_attach(struct bwi_softc *);
202 static int bwi_bbp_power_on(struct bwi_softc *, enum bwi_clock_mode);
203 static void bwi_bbp_power_off(struct bwi_softc *);
205 static const char *bwi_regwin_name(const struct bwi_regwin *);
206 static uint32_t bwi_regwin_disable_bits(struct bwi_softc *);
207 static void bwi_regwin_info(struct bwi_softc *, uint16_t *, uint8_t *);
208 static int bwi_regwin_select(struct bwi_softc *, int);
210 static void bwi_led_attach(struct bwi_softc *);
211 static void bwi_led_newstate(struct bwi_softc *, enum ieee80211_state);
212 static void bwi_led_event(struct bwi_softc *, int);
213 static void bwi_led_blink_start(struct bwi_softc *, int, int);
214 static void bwi_led_blink_next(void *);
215 static void bwi_led_blink_end(void *);
217 static const struct {
221 } bwi_bbpid_map[] = {
222 { 0x4301, 0x4301, 0x4301 },
223 { 0x4305, 0x4307, 0x4307 },
224 { 0x4403, 0x4403, 0x4402 },
225 { 0x4610, 0x4615, 0x4610 },
226 { 0x4710, 0x4715, 0x4710 },
227 { 0x4720, 0x4725, 0x4309 }
230 static const struct {
233 } bwi_regwin_count[] = {
246 #define CLKSRC(src) \
247 [BWI_CLKSRC_ ## src] = { \
248 .freq_min = BWI_CLKSRC_ ##src## _FMIN, \
249 .freq_max = BWI_CLKSRC_ ##src## _FMAX \
252 static const struct {
255 } bwi_clkfreq[BWI_CLKSRC_MAX] = {
263 #define VENDOR_LED_ACT(vendor) \
265 .vid = PCI_VENDOR_##vendor, \
266 .led_act = { BWI_VENDOR_LED_ACT_##vendor } \
269 static const struct {
270 #define PCI_VENDOR_COMPAQ 0x0e11
271 #define PCI_VENDOR_LINKSYS 0x1737
273 uint8_t led_act[BWI_LED_MAX];
274 } bwi_vendor_led_act[] = {
275 VENDOR_LED_ACT(COMPAQ),
276 VENDOR_LED_ACT(LINKSYS)
277 #undef PCI_VENDOR_LINKSYS
278 #undef PCI_VENDOR_COMPAQ
281 static const uint8_t bwi_default_led_act[BWI_LED_MAX] =
282 { BWI_VENDOR_LED_ACT_DEFAULT };
284 #undef VENDOR_LED_ACT
286 static const struct {
289 } bwi_led_duration[109] = {
306 #ifdef BWI_DEBUG_VERBOSE
307 static uint32_t bwi_debug = BWI_DBG_ATTACH | BWI_DBG_INIT | BWI_DBG_TXPOWER;
309 static uint32_t bwi_debug;
311 TUNABLE_INT("hw.bwi.debug", (int *)&bwi_debug);
312 #endif /* BWI_DEBUG */
314 static const uint8_t bwi_zero_addr[IEEE80211_ADDR_LEN];
317 bwi_read_sprom(struct bwi_softc *sc, uint16_t ofs)
319 return CSR_READ_2(sc, ofs + BWI_SPROM_START);
323 bwi_setup_desc32(struct bwi_softc *sc, struct bwi_desc32 *desc_array,
324 int ndesc, int desc_idx, bus_addr_t paddr, int buf_len,
327 struct bwi_desc32 *desc = &desc_array[desc_idx];
328 uint32_t ctrl, addr, addr_hi, addr_lo;
330 addr_lo = __SHIFTOUT(paddr, BWI_DESC32_A_ADDR_MASK);
331 addr_hi = __SHIFTOUT(paddr, BWI_DESC32_A_FUNC_MASK);
333 addr = __SHIFTIN(addr_lo, BWI_DESC32_A_ADDR_MASK) |
334 __SHIFTIN(BWI_DESC32_A_FUNC_TXRX, BWI_DESC32_A_FUNC_MASK);
336 ctrl = __SHIFTIN(buf_len, BWI_DESC32_C_BUFLEN_MASK) |
337 __SHIFTIN(addr_hi, BWI_DESC32_C_ADDRHI_MASK);
338 if (desc_idx == ndesc - 1)
339 ctrl |= BWI_DESC32_C_EOR;
342 ctrl |= BWI_DESC32_C_FRAME_START |
343 BWI_DESC32_C_FRAME_END |
347 desc->addr = htole32(addr);
348 desc->ctrl = htole32(ctrl);
352 bwi_attach(struct bwi_softc *sc)
354 struct ieee80211com *ic;
355 device_t dev = sc->sc_dev;
361 uint8_t macaddr[IEEE80211_ADDR_LEN];
366 * Initialize taskq and various tasks
368 sc->sc_tq = taskqueue_create("bwi_taskq", M_NOWAIT | M_ZERO,
369 taskqueue_thread_enqueue, &sc->sc_tq);
370 taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq",
371 device_get_nameunit(dev));
372 TASK_INIT(&sc->sc_restart_task, 0, bwi_restart, sc);
374 callout_init_mtx(&sc->sc_calib_ch, &sc->sc_mtx, 0);
377 * Initialize sysctl variables
379 sc->sc_fw_version = BWI_FW_VERSION3;
380 sc->sc_led_idle = (2350 * hz) / 1000;
381 sc->sc_led_blink = 1;
382 sc->sc_txpwr_calib = 1;
384 sc->sc_debug = bwi_debug;
388 error = bwi_bbp_attach(sc);
392 error = bwi_bbp_power_on(sc, BWI_CLOCK_MODE_FAST);
396 if (BWI_REGWIN_EXIST(&sc->sc_com_regwin)) {
397 error = bwi_set_clock_delay(sc);
401 error = bwi_set_clock_mode(sc, BWI_CLOCK_MODE_FAST);
405 error = bwi_get_pwron_delay(sc);
410 error = bwi_bus_attach(sc);
414 bwi_get_card_flags(sc);
418 for (i = 0; i < sc->sc_nmac; ++i) {
419 struct bwi_regwin *old;
421 mac = &sc->sc_mac[i];
422 error = bwi_regwin_switch(sc, &mac->mac_regwin, &old);
426 error = bwi_mac_lateattach(mac);
430 error = bwi_regwin_switch(sc, old, NULL);
436 * XXX First MAC is known to exist
439 mac = &sc->sc_mac[0];
442 bwi_bbp_power_off(sc);
444 error = bwi_dma_alloc(sc);
448 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
450 device_printf(dev, "can not if_alloc()\n");
456 /* set these up early for if_printf use */
457 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
460 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
461 ifp->if_init = bwi_init;
462 ifp->if_ioctl = bwi_ioctl;
463 ifp->if_start = bwi_start;
464 ifp->if_watchdog = bwi_watchdog;
465 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
466 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
467 IFQ_SET_READY(&ifp->if_snd);
470 * Setup ratesets, phytype, channels and get MAC address
473 if (phy->phy_mode == IEEE80211_MODE_11B ||
474 phy->phy_mode == IEEE80211_MODE_11G) {
475 setbit(&bands, IEEE80211_MODE_11B);
476 if (phy->phy_mode == IEEE80211_MODE_11B) {
477 ic->ic_phytype = IEEE80211_T_DS;
479 ic->ic_phytype = IEEE80211_T_OFDM;
480 setbit(&bands, IEEE80211_MODE_11G);
483 bwi_get_eaddr(sc, BWI_SPROM_11BG_EADDR, macaddr);
484 if (IEEE80211_IS_MULTICAST(macaddr)) {
485 bwi_get_eaddr(sc, BWI_SPROM_11A_EADDR, macaddr);
486 if (IEEE80211_IS_MULTICAST(macaddr)) {
488 "invalid MAC address: %6D\n",
492 } else if (phy->phy_mode == IEEE80211_MODE_11A) {
494 setbit(&bands, IEEE80211_MODE_11A);
498 panic("unknown phymode %d\n", phy->phy_mode);
502 sc->sc_locale = __SHIFTOUT(bwi_read_sprom(sc, BWI_SPROM_CARD_INFO),
503 BWI_SPROM_CARD_INFO_LOCALE);
504 DPRINTF(sc, BWI_DBG_ATTACH, "locale: %d\n", sc->sc_locale);
506 ieee80211_init_channels(ic, NULL, &bands);
509 ic->ic_caps = IEEE80211_C_STA |
511 IEEE80211_C_SHPREAMBLE |
515 ic->ic_opmode = IEEE80211_M_STA;
516 ieee80211_ifattach(ic, macaddr);
518 ic->ic_headroom = sizeof(struct bwi_txbuf_hdr);
520 /* override default methods */
521 ic->ic_vap_create = bwi_vap_create;
522 ic->ic_vap_delete = bwi_vap_delete;
523 ic->ic_raw_xmit = bwi_raw_xmit;
524 ic->ic_updateslot = bwi_updateslot;
525 ic->ic_scan_start = bwi_scan_start;
526 ic->ic_scan_end = bwi_scan_end;
527 ic->ic_set_channel = bwi_set_channel;
529 sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan);
531 ieee80211_radiotap_attach(ic,
532 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
533 BWI_TX_RADIOTAP_PRESENT,
534 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
535 BWI_RX_RADIOTAP_PRESENT);
540 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
541 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
542 "fw_version", CTLFLAG_RD, &sc->sc_fw_version, 0,
544 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
545 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
546 "led_idle", CTLFLAG_RW, &sc->sc_led_idle, 0,
547 "# ticks before LED enters idle state");
548 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
549 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
550 "led_blink", CTLFLAG_RW, &sc->sc_led_blink, 0,
551 "Allow LED to blink");
552 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
553 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
554 "txpwr_calib", CTLFLAG_RW, &sc->sc_txpwr_calib, 0,
555 "Enable software TX power calibration");
557 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
558 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
559 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "Debug flags");
562 ieee80211_announce(ic);
566 BWI_LOCK_DESTROY(sc);
571 bwi_detach(struct bwi_softc *sc)
573 struct ifnet *ifp = sc->sc_ifp;
574 struct ieee80211com *ic = ifp->if_l2com;
578 callout_drain(&sc->sc_led_blink_ch);
579 callout_drain(&sc->sc_calib_ch);
580 ieee80211_ifdetach(ic);
582 for (i = 0; i < sc->sc_nmac; ++i)
583 bwi_mac_detach(&sc->sc_mac[i]);
586 taskqueue_free(sc->sc_tq);
588 BWI_LOCK_DESTROY(sc);
593 static struct ieee80211vap *
594 bwi_vap_create(struct ieee80211com *ic,
595 const char name[IFNAMSIZ], int unit, int opmode, int flags,
596 const uint8_t bssid[IEEE80211_ADDR_LEN],
597 const uint8_t mac[IEEE80211_ADDR_LEN])
600 struct ieee80211vap *vap;
602 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
604 bvp = (struct bwi_vap *) malloc(sizeof(struct bwi_vap),
605 M_80211_VAP, M_WAITOK | M_ZERO);
609 /* enable s/w bmiss handling for sta mode */
610 ieee80211_vap_setup(ic, vap, name, unit, opmode,
611 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
613 /* override default methods */
614 bvp->bv_newstate = vap->iv_newstate;
615 vap->iv_newstate = bwi_newstate;
617 vap->iv_update_beacon = bwi_beacon_update;
619 ieee80211_ratectl_init(vap);
622 ieee80211_vap_attach(vap, bwi_media_change, ieee80211_media_status);
623 ic->ic_opmode = opmode;
628 bwi_vap_delete(struct ieee80211vap *vap)
630 struct bwi_vap *bvp = BWI_VAP(vap);
632 ieee80211_ratectl_deinit(vap);
633 ieee80211_vap_detach(vap);
634 free(bvp, M_80211_VAP);
638 bwi_suspend(struct bwi_softc *sc)
644 bwi_resume(struct bwi_softc *sc)
646 struct ifnet *ifp = sc->sc_ifp;
648 if (ifp->if_flags & IFF_UP)
653 bwi_shutdown(struct bwi_softc *sc)
660 bwi_power_on(struct bwi_softc *sc, int with_pll)
662 uint32_t gpio_in, gpio_out, gpio_en;
665 gpio_in = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_IN, 4);
666 if (gpio_in & BWI_PCIM_GPIO_PWR_ON)
669 gpio_out = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4);
670 gpio_en = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, 4);
672 gpio_out |= BWI_PCIM_GPIO_PWR_ON;
673 gpio_en |= BWI_PCIM_GPIO_PWR_ON;
675 /* Turn off PLL first */
676 gpio_out |= BWI_PCIM_GPIO_PLL_PWR_OFF;
677 gpio_en |= BWI_PCIM_GPIO_PLL_PWR_OFF;
680 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4);
681 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, gpio_en, 4);
686 gpio_out &= ~BWI_PCIM_GPIO_PLL_PWR_OFF;
687 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4);
692 /* Clear "Signaled Target Abort" */
693 status = pci_read_config(sc->sc_dev, PCIR_STATUS, 2);
694 status &= ~PCIM_STATUS_STABORT;
695 pci_write_config(sc->sc_dev, PCIR_STATUS, status, 2);
699 bwi_power_off(struct bwi_softc *sc, int with_pll)
701 uint32_t gpio_out, gpio_en;
703 pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_IN, 4); /* dummy read */
704 gpio_out = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4);
705 gpio_en = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, 4);
707 gpio_out &= ~BWI_PCIM_GPIO_PWR_ON;
708 gpio_en |= BWI_PCIM_GPIO_PWR_ON;
710 gpio_out |= BWI_PCIM_GPIO_PLL_PWR_OFF;
711 gpio_en |= BWI_PCIM_GPIO_PLL_PWR_OFF;
714 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4);
715 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, gpio_en, 4);
720 bwi_regwin_switch(struct bwi_softc *sc, struct bwi_regwin *rw,
721 struct bwi_regwin **old_rw)
728 if (!BWI_REGWIN_EXIST(rw))
731 if (sc->sc_cur_regwin != rw) {
732 error = bwi_regwin_select(sc, rw->rw_id);
734 device_printf(sc->sc_dev, "can't select regwin %d\n",
741 *old_rw = sc->sc_cur_regwin;
742 sc->sc_cur_regwin = rw;
747 bwi_regwin_select(struct bwi_softc *sc, int id)
749 uint32_t win = BWI_PCIM_REGWIN(id);
753 for (i = 0; i < RETRY_MAX; ++i) {
754 pci_write_config(sc->sc_dev, BWI_PCIR_SEL_REGWIN, win, 4);
755 if (pci_read_config(sc->sc_dev, BWI_PCIR_SEL_REGWIN, 4) == win)
765 bwi_regwin_info(struct bwi_softc *sc, uint16_t *type, uint8_t *rev)
769 val = CSR_READ_4(sc, BWI_ID_HI);
770 *type = BWI_ID_HI_REGWIN_TYPE(val);
771 *rev = BWI_ID_HI_REGWIN_REV(val);
773 DPRINTF(sc, BWI_DBG_ATTACH, "regwin: type 0x%03x, rev %d, "
774 "vendor 0x%04x\n", *type, *rev,
775 __SHIFTOUT(val, BWI_ID_HI_REGWIN_VENDOR_MASK));
779 bwi_bbp_attach(struct bwi_softc *sc)
781 #define N(arr) (int)(sizeof(arr) / sizeof(arr[0]))
782 uint16_t bbp_id, rw_type;
785 int error, nregwin, i;
788 * Get 0th regwin information
789 * NOTE: 0th regwin should exist
791 error = bwi_regwin_select(sc, 0);
793 device_printf(sc->sc_dev, "can't select regwin 0\n");
796 bwi_regwin_info(sc, &rw_type, &rw_rev);
803 if (rw_type == BWI_REGWIN_T_COM) {
804 info = CSR_READ_4(sc, BWI_INFO);
805 bbp_id = __SHIFTOUT(info, BWI_INFO_BBPID_MASK);
807 BWI_CREATE_REGWIN(&sc->sc_com_regwin, 0, rw_type, rw_rev);
809 sc->sc_cap = CSR_READ_4(sc, BWI_CAPABILITY);
811 for (i = 0; i < N(bwi_bbpid_map); ++i) {
812 if (sc->sc_pci_did >= bwi_bbpid_map[i].did_min &&
813 sc->sc_pci_did <= bwi_bbpid_map[i].did_max) {
814 bbp_id = bwi_bbpid_map[i].bbp_id;
819 device_printf(sc->sc_dev, "no BBP id for device id "
820 "0x%04x\n", sc->sc_pci_did);
824 info = __SHIFTIN(sc->sc_pci_revid, BWI_INFO_BBPREV_MASK) |
825 __SHIFTIN(0, BWI_INFO_BBPPKG_MASK);
829 * Find out number of regwins
832 if (rw_type == BWI_REGWIN_T_COM && rw_rev >= 4) {
833 nregwin = __SHIFTOUT(info, BWI_INFO_NREGWIN_MASK);
835 for (i = 0; i < N(bwi_regwin_count); ++i) {
836 if (bwi_regwin_count[i].bbp_id == bbp_id) {
837 nregwin = bwi_regwin_count[i].nregwin;
842 device_printf(sc->sc_dev, "no number of win for "
843 "BBP id 0x%04x\n", bbp_id);
848 /* Record BBP id/rev for later using */
849 sc->sc_bbp_id = bbp_id;
850 sc->sc_bbp_rev = __SHIFTOUT(info, BWI_INFO_BBPREV_MASK);
851 sc->sc_bbp_pkg = __SHIFTOUT(info, BWI_INFO_BBPPKG_MASK);
852 device_printf(sc->sc_dev, "BBP: id 0x%04x, rev 0x%x, pkg %d\n",
853 sc->sc_bbp_id, sc->sc_bbp_rev, sc->sc_bbp_pkg);
855 DPRINTF(sc, BWI_DBG_ATTACH, "nregwin %d, cap 0x%08x\n",
856 nregwin, sc->sc_cap);
859 * Create rest of the regwins
862 /* Don't re-create common regwin, if it is already created */
863 i = BWI_REGWIN_EXIST(&sc->sc_com_regwin) ? 1 : 0;
865 for (; i < nregwin; ++i) {
867 * Get regwin information
869 error = bwi_regwin_select(sc, i);
871 device_printf(sc->sc_dev,
872 "can't select regwin %d\n", i);
875 bwi_regwin_info(sc, &rw_type, &rw_rev);
879 * 1) Bus (PCI/PCIE) regwin
881 * Ignore rest types of regwin
883 if (rw_type == BWI_REGWIN_T_BUSPCI ||
884 rw_type == BWI_REGWIN_T_BUSPCIE) {
885 if (BWI_REGWIN_EXIST(&sc->sc_bus_regwin)) {
886 device_printf(sc->sc_dev,
887 "bus regwin already exists\n");
889 BWI_CREATE_REGWIN(&sc->sc_bus_regwin, i,
892 } else if (rw_type == BWI_REGWIN_T_MAC) {
893 /* XXX ignore return value */
894 bwi_mac_attach(sc, i, rw_rev);
898 /* At least one MAC shold exist */
899 if (!BWI_REGWIN_EXIST(&sc->sc_mac[0].mac_regwin)) {
900 device_printf(sc->sc_dev, "no MAC was found\n");
903 KASSERT(sc->sc_nmac > 0, ("no mac's"));
905 /* Bus regwin must exist */
906 if (!BWI_REGWIN_EXIST(&sc->sc_bus_regwin)) {
907 device_printf(sc->sc_dev, "no bus regwin was found\n");
911 /* Start with first MAC */
912 error = bwi_regwin_switch(sc, &sc->sc_mac[0].mac_regwin, NULL);
921 bwi_bus_init(struct bwi_softc *sc, struct bwi_mac *mac)
923 struct bwi_regwin *old, *bus;
927 bus = &sc->sc_bus_regwin;
928 KASSERT(sc->sc_cur_regwin == &mac->mac_regwin, ("not cur regwin"));
931 * Tell bus to generate requested interrupts
933 if (bus->rw_rev < 6 && bus->rw_type == BWI_REGWIN_T_BUSPCI) {
935 * NOTE: Read BWI_FLAGS from MAC regwin
937 val = CSR_READ_4(sc, BWI_FLAGS);
939 error = bwi_regwin_switch(sc, bus, &old);
943 CSR_SETBITS_4(sc, BWI_INTRVEC, (val & BWI_FLAGS_INTR_MASK));
947 mac_mask = 1 << mac->mac_id;
949 error = bwi_regwin_switch(sc, bus, &old);
953 val = pci_read_config(sc->sc_dev, BWI_PCIR_INTCTL, 4);
954 val |= mac_mask << 8;
955 pci_write_config(sc->sc_dev, BWI_PCIR_INTCTL, val, 4);
958 if (sc->sc_flags & BWI_F_BUS_INITED)
961 if (bus->rw_type == BWI_REGWIN_T_BUSPCI) {
963 * Enable prefetch and burst
965 CSR_SETBITS_4(sc, BWI_BUS_CONFIG,
966 BWI_BUS_CONFIG_PREFETCH | BWI_BUS_CONFIG_BURST);
968 if (bus->rw_rev < 5) {
969 struct bwi_regwin *com = &sc->sc_com_regwin;
972 * Configure timeouts for bus operation
976 * Set service timeout and request timeout
978 CSR_SETBITS_4(sc, BWI_CONF_LO,
979 __SHIFTIN(BWI_CONF_LO_SERVTO, BWI_CONF_LO_SERVTO_MASK) |
980 __SHIFTIN(BWI_CONF_LO_REQTO, BWI_CONF_LO_REQTO_MASK));
983 * If there is common regwin, we switch to that regwin
984 * and switch back to bus regwin once we have done.
986 if (BWI_REGWIN_EXIST(com)) {
987 error = bwi_regwin_switch(sc, com, NULL);
992 /* Let bus know what we have changed */
993 CSR_WRITE_4(sc, BWI_BUS_ADDR, BWI_BUS_ADDR_MAGIC);
994 CSR_READ_4(sc, BWI_BUS_ADDR); /* Flush */
995 CSR_WRITE_4(sc, BWI_BUS_DATA, 0);
996 CSR_READ_4(sc, BWI_BUS_DATA); /* Flush */
998 if (BWI_REGWIN_EXIST(com)) {
999 error = bwi_regwin_switch(sc, bus, NULL);
1003 } else if (bus->rw_rev >= 11) {
1005 * Enable memory read multiple
1007 CSR_SETBITS_4(sc, BWI_BUS_CONFIG, BWI_BUS_CONFIG_MRM);
1013 sc->sc_flags |= BWI_F_BUS_INITED;
1015 return bwi_regwin_switch(sc, old, NULL);
1019 bwi_get_card_flags(struct bwi_softc *sc)
1021 #define PCI_VENDOR_APPLE 0x106b
1022 #define PCI_VENDOR_DELL 0x1028
1023 sc->sc_card_flags = bwi_read_sprom(sc, BWI_SPROM_CARD_FLAGS);
1024 if (sc->sc_card_flags == 0xffff)
1025 sc->sc_card_flags = 0;
1027 if (sc->sc_pci_subvid == PCI_VENDOR_DELL &&
1028 sc->sc_bbp_id == BWI_BBPID_BCM4301 &&
1029 sc->sc_pci_revid == 0x74)
1030 sc->sc_card_flags |= BWI_CARD_F_BT_COEXIST;
1032 if (sc->sc_pci_subvid == PCI_VENDOR_APPLE &&
1033 sc->sc_pci_subdid == 0x4e && /* XXX */
1034 sc->sc_pci_revid > 0x40)
1035 sc->sc_card_flags |= BWI_CARD_F_PA_GPIO9;
1037 DPRINTF(sc, BWI_DBG_ATTACH, "card flags 0x%04x\n", sc->sc_card_flags);
1038 #undef PCI_VENDOR_DELL
1039 #undef PCI_VENDOR_APPLE
1043 bwi_get_eaddr(struct bwi_softc *sc, uint16_t eaddr_ofs, uint8_t *eaddr)
1047 for (i = 0; i < 3; ++i) {
1048 *((uint16_t *)eaddr + i) =
1049 htobe16(bwi_read_sprom(sc, eaddr_ofs + 2 * i));
1054 bwi_get_clock_freq(struct bwi_softc *sc, struct bwi_clock_freq *freq)
1056 struct bwi_regwin *com;
1061 bzero(freq, sizeof(*freq));
1062 com = &sc->sc_com_regwin;
1064 KASSERT(BWI_REGWIN_EXIST(com), ("regwin does not exist"));
1065 KASSERT(sc->sc_cur_regwin == com, ("wrong regwin"));
1066 KASSERT(sc->sc_cap & BWI_CAP_CLKMODE, ("wrong clock mode"));
1069 * Calculate clock frequency
1073 if (com->rw_rev < 6) {
1074 val = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4);
1075 if (val & BWI_PCIM_GPIO_OUT_CLKSRC) {
1076 src = BWI_CLKSRC_PCI;
1079 src = BWI_CLKSRC_CS_OSC;
1082 } else if (com->rw_rev < 10) {
1083 val = CSR_READ_4(sc, BWI_CLOCK_CTRL);
1085 src = __SHIFTOUT(val, BWI_CLOCK_CTRL_CLKSRC);
1086 if (src == BWI_CLKSRC_LP_OSC) {
1089 div = (__SHIFTOUT(val, BWI_CLOCK_CTRL_FDIV) + 1) << 2;
1091 /* Unknown source */
1092 if (src >= BWI_CLKSRC_MAX)
1093 src = BWI_CLKSRC_CS_OSC;
1096 val = CSR_READ_4(sc, BWI_CLOCK_INFO);
1098 src = BWI_CLKSRC_CS_OSC;
1099 div = (__SHIFTOUT(val, BWI_CLOCK_INFO_FDIV) + 1) << 2;
1102 KASSERT(src >= 0 && src < BWI_CLKSRC_MAX, ("bad src %d", src));
1103 KASSERT(div != 0, ("div zero"));
1105 DPRINTF(sc, BWI_DBG_ATTACH, "clksrc %s\n",
1106 src == BWI_CLKSRC_PCI ? "PCI" :
1107 (src == BWI_CLKSRC_LP_OSC ? "LP_OSC" : "CS_OSC"));
1109 freq->clkfreq_min = bwi_clkfreq[src].freq_min / div;
1110 freq->clkfreq_max = bwi_clkfreq[src].freq_max / div;
1112 DPRINTF(sc, BWI_DBG_ATTACH, "clkfreq min %u, max %u\n",
1113 freq->clkfreq_min, freq->clkfreq_max);
1117 bwi_set_clock_mode(struct bwi_softc *sc, enum bwi_clock_mode clk_mode)
1119 struct bwi_regwin *old, *com;
1120 uint32_t clk_ctrl, clk_src;
1121 int error, pwr_off = 0;
1123 com = &sc->sc_com_regwin;
1124 if (!BWI_REGWIN_EXIST(com))
1127 if (com->rw_rev >= 10 || com->rw_rev < 6)
1131 * For common regwin whose rev is [6, 10), the chip
1132 * must be capable to change clock mode.
1134 if ((sc->sc_cap & BWI_CAP_CLKMODE) == 0)
1137 error = bwi_regwin_switch(sc, com, &old);
1141 if (clk_mode == BWI_CLOCK_MODE_FAST)
1142 bwi_power_on(sc, 0); /* Don't turn on PLL */
1144 clk_ctrl = CSR_READ_4(sc, BWI_CLOCK_CTRL);
1145 clk_src = __SHIFTOUT(clk_ctrl, BWI_CLOCK_CTRL_CLKSRC);
1148 case BWI_CLOCK_MODE_FAST:
1149 clk_ctrl &= ~BWI_CLOCK_CTRL_SLOW;
1150 clk_ctrl |= BWI_CLOCK_CTRL_IGNPLL;
1152 case BWI_CLOCK_MODE_SLOW:
1153 clk_ctrl |= BWI_CLOCK_CTRL_SLOW;
1155 case BWI_CLOCK_MODE_DYN:
1156 clk_ctrl &= ~(BWI_CLOCK_CTRL_SLOW |
1157 BWI_CLOCK_CTRL_IGNPLL |
1158 BWI_CLOCK_CTRL_NODYN);
1159 if (clk_src != BWI_CLKSRC_CS_OSC) {
1160 clk_ctrl |= BWI_CLOCK_CTRL_NODYN;
1165 CSR_WRITE_4(sc, BWI_CLOCK_CTRL, clk_ctrl);
1168 bwi_power_off(sc, 0); /* Leave PLL as it is */
1170 return bwi_regwin_switch(sc, old, NULL);
1174 bwi_set_clock_delay(struct bwi_softc *sc)
1176 struct bwi_regwin *old, *com;
1179 com = &sc->sc_com_regwin;
1180 if (!BWI_REGWIN_EXIST(com))
1183 error = bwi_regwin_switch(sc, com, &old);
1187 if (sc->sc_bbp_id == BWI_BBPID_BCM4321) {
1188 if (sc->sc_bbp_rev == 0)
1189 CSR_WRITE_4(sc, BWI_CONTROL, BWI_CONTROL_MAGIC0);
1190 else if (sc->sc_bbp_rev == 1)
1191 CSR_WRITE_4(sc, BWI_CONTROL, BWI_CONTROL_MAGIC1);
1194 if (sc->sc_cap & BWI_CAP_CLKMODE) {
1195 if (com->rw_rev >= 10) {
1196 CSR_FILT_SETBITS_4(sc, BWI_CLOCK_INFO, 0xffff, 0x40000);
1198 struct bwi_clock_freq freq;
1200 bwi_get_clock_freq(sc, &freq);
1201 CSR_WRITE_4(sc, BWI_PLL_ON_DELAY,
1202 howmany(freq.clkfreq_max * 150, 1000000));
1203 CSR_WRITE_4(sc, BWI_FREQ_SEL_DELAY,
1204 howmany(freq.clkfreq_max * 15, 1000000));
1208 return bwi_regwin_switch(sc, old, NULL);
1214 struct bwi_softc *sc = xsc;
1215 struct ifnet *ifp = sc->sc_ifp;
1216 struct ieee80211com *ic = ifp->if_l2com;
1219 bwi_init_statechg(sc, 1);
1222 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1223 ieee80211_start_all(ic); /* start all vap's */
1227 bwi_init_statechg(struct bwi_softc *sc, int statechg)
1229 struct ifnet *ifp = sc->sc_ifp;
1230 struct bwi_mac *mac;
1233 bwi_stop_locked(sc, statechg);
1235 bwi_bbp_power_on(sc, BWI_CLOCK_MODE_FAST);
1239 mac = &sc->sc_mac[0];
1240 error = bwi_regwin_switch(sc, &mac->mac_regwin, NULL);
1242 if_printf(ifp, "%s: error %d on regwin switch\n",
1246 error = bwi_mac_init(mac);
1248 if_printf(ifp, "%s: error %d on MAC init\n", __func__, error);
1252 bwi_bbp_power_on(sc, BWI_CLOCK_MODE_DYN);
1254 bwi_set_bssid(sc, bwi_zero_addr); /* Clear BSSID */
1255 bwi_set_addr_filter(sc, BWI_ADDR_FILTER_MYADDR, IF_LLADDR(ifp));
1257 bwi_mac_reset_hwkeys(mac);
1259 if ((mac->mac_flags & BWI_MAC_F_HAS_TXSTATS) == 0) {
1264 * Drain any possible pending TX status
1266 for (i = 0; i < NRETRY; ++i) {
1267 if ((CSR_READ_4(sc, BWI_TXSTATUS0) &
1268 BWI_TXSTATUS0_VALID) == 0)
1270 CSR_READ_4(sc, BWI_TXSTATUS1);
1273 if_printf(ifp, "%s: can't drain TX status\n", __func__);
1277 if (mac->mac_phy.phy_mode == IEEE80211_MODE_11G)
1278 bwi_mac_updateslot(mac, 1);
1281 error = bwi_mac_start(mac);
1283 if_printf(ifp, "%s: error %d starting MAC\n", __func__, error);
1287 /* Clear stop flag before enabling interrupt */
1288 sc->sc_flags &= ~BWI_F_STOP;
1290 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1293 bwi_enable_intrs(sc, BWI_INIT_INTRS);
1296 bwi_stop_locked(sc, 1);
1300 bwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1302 #define IS_RUNNING(ifp) \
1303 ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
1304 struct bwi_softc *sc = ifp->if_softc;
1305 struct ieee80211com *ic = ifp->if_l2com;
1306 struct ifreq *ifr = (struct ifreq *) data;
1307 int error = 0, startall = 0;
1312 if (IS_RUNNING(ifp)) {
1313 struct bwi_mac *mac;
1316 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1317 ("current regwin type %d",
1318 sc->sc_cur_regwin->rw_type));
1319 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1321 if ((ifp->if_flags & IFF_PROMISC) &&
1322 (sc->sc_flags & BWI_F_PROMISC) == 0) {
1324 sc->sc_flags |= BWI_F_PROMISC;
1325 } else if ((ifp->if_flags & IFF_PROMISC) == 0 &&
1326 (sc->sc_flags & BWI_F_PROMISC)) {
1328 sc->sc_flags &= ~BWI_F_PROMISC;
1332 bwi_mac_set_promisc(mac, promisc);
1335 if (ifp->if_flags & IFF_UP) {
1336 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1337 bwi_init_statechg(sc, 1);
1341 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1342 bwi_stop_locked(sc, 1);
1346 ieee80211_start_all(ic);
1349 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1352 error = ether_ioctl(ifp, cmd, data);
1363 bwi_start(struct ifnet *ifp)
1365 struct bwi_softc *sc = ifp->if_softc;
1368 bwi_start_locked(ifp);
1373 bwi_start_locked(struct ifnet *ifp)
1375 struct bwi_softc *sc = ifp->if_softc;
1376 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
1377 struct ieee80211_frame *wh;
1378 struct ieee80211_node *ni;
1379 struct ieee80211_key *k;
1383 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1389 while (tbd->tbd_buf[idx].tb_mbuf == NULL) {
1390 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); /* XXX: LOCK */
1394 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1395 wh = mtod(m, struct ieee80211_frame *);
1396 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1397 k = ieee80211_crypto_encap(ni, m);
1399 ieee80211_free_node(ni);
1405 wh = NULL; /* Catch any invalid use */
1407 if (bwi_encap(sc, idx, m, ni) != 0) {
1408 /* 'm' is freed in bwi_encap() if we reach here */
1410 ieee80211_free_node(ni);
1417 idx = (idx + 1) % BWI_TX_NDESC;
1421 if (tbd->tbd_used + BWI_TX_NSPRDESC >= BWI_TX_NDESC) {
1422 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1433 bwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1434 const struct ieee80211_bpf_params *params)
1436 struct ieee80211com *ic = ni->ni_ic;
1437 struct ifnet *ifp = ic->ic_ifp;
1438 struct bwi_softc *sc = ifp->if_softc;
1440 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
1443 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1444 ieee80211_free_node(ni);
1451 KASSERT(tbd->tbd_buf[idx].tb_mbuf == NULL, ("slot %d not empty", idx));
1452 if (params == NULL) {
1454 * Legacy path; interpret frame contents to decide
1455 * precisely how to send the frame.
1457 error = bwi_encap(sc, idx, m, ni);
1460 * Caller supplied explicit parameters to use in
1461 * sending the frame.
1463 error = bwi_encap_raw(sc, idx, m, ni, params);
1467 if (++tbd->tbd_used + BWI_TX_NSPRDESC >= BWI_TX_NDESC)
1468 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1469 tbd->tbd_idx = (idx + 1) % BWI_TX_NDESC;
1472 /* NB: m is reclaimed on encap failure */
1473 ieee80211_free_node(ni);
1481 bwi_watchdog(struct ifnet *ifp)
1483 struct bwi_softc *sc = ifp->if_softc;
1486 if ((ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1487 if_printf(ifp, "watchdog timeout\n");
1489 taskqueue_enqueue(sc->sc_tq, &sc->sc_restart_task);
1495 bwi_stop(struct bwi_softc *sc, int statechg)
1498 bwi_stop_locked(sc, statechg);
1503 bwi_stop_locked(struct bwi_softc *sc, int statechg)
1505 struct ifnet *ifp = sc->sc_ifp;
1506 struct bwi_mac *mac;
1507 int i, error, pwr_off = 0;
1509 BWI_ASSERT_LOCKED(sc);
1511 callout_stop(&sc->sc_calib_ch);
1512 callout_stop(&sc->sc_led_blink_ch);
1513 sc->sc_led_blinking = 0;
1514 sc->sc_flags |= BWI_F_STOP;
1516 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1517 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1518 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1519 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1521 bwi_disable_intrs(sc, BWI_ALL_INTRS);
1522 CSR_READ_4(sc, BWI_MAC_INTR_MASK);
1526 for (i = 0; i < sc->sc_nmac; ++i) {
1527 struct bwi_regwin *old_rw;
1529 mac = &sc->sc_mac[i];
1530 if ((mac->mac_flags & BWI_MAC_F_INITED) == 0)
1533 error = bwi_regwin_switch(sc, &mac->mac_regwin, &old_rw);
1537 bwi_mac_shutdown(mac);
1540 bwi_regwin_switch(sc, old_rw, NULL);
1544 bwi_bbp_power_off(sc);
1546 sc->sc_tx_timer = 0;
1548 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1554 struct bwi_softc *sc = xsc;
1555 struct ifnet *ifp = sc->sc_ifp;
1556 struct bwi_mac *mac;
1557 uint32_t intr_status;
1558 uint32_t txrx_intr_status[BWI_TXRX_NRING];
1559 int i, txrx_error, tx = 0, rx_data = -1;
1563 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1564 (sc->sc_flags & BWI_F_STOP)) {
1569 * Get interrupt status
1571 intr_status = CSR_READ_4(sc, BWI_MAC_INTR_STATUS);
1572 if (intr_status == 0xffffffff) { /* Not for us */
1577 DPRINTF(sc, BWI_DBG_INTR, "intr status 0x%08x\n", intr_status);
1579 intr_status &= CSR_READ_4(sc, BWI_MAC_INTR_MASK);
1580 if (intr_status == 0) { /* Nothing is interesting */
1585 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1586 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1587 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1590 DPRINTF(sc, BWI_DBG_INTR, "%s\n", "TX/RX intr");
1591 for (i = 0; i < BWI_TXRX_NRING; ++i) {
1594 if (BWI_TXRX_IS_RX(i))
1595 mask = BWI_TXRX_RX_INTRS;
1597 mask = BWI_TXRX_TX_INTRS;
1599 txrx_intr_status[i] =
1600 CSR_READ_4(sc, BWI_TXRX_INTR_STATUS(i)) & mask;
1602 _DPRINTF(sc, BWI_DBG_INTR, ", %d 0x%08x",
1603 i, txrx_intr_status[i]);
1605 if (txrx_intr_status[i] & BWI_TXRX_INTR_ERROR) {
1607 "%s: intr fatal TX/RX (%d) error 0x%08x\n",
1608 __func__, i, txrx_intr_status[i]);
1612 _DPRINTF(sc, BWI_DBG_INTR, "%s\n", "");
1615 * Acknowledge interrupt
1617 CSR_WRITE_4(sc, BWI_MAC_INTR_STATUS, intr_status);
1619 for (i = 0; i < BWI_TXRX_NRING; ++i)
1620 CSR_WRITE_4(sc, BWI_TXRX_INTR_STATUS(i), txrx_intr_status[i]);
1622 /* Disable all interrupts */
1623 bwi_disable_intrs(sc, BWI_ALL_INTRS);
1626 * http://bcm-specs.sipsolutions.net/Interrupts
1627 * Says for this bit (0x800):
1630 * We got this one while testing things when by accident the
1631 * template ram wasn't set to big endian when it should have
1632 * been after writing the initial values. It keeps on being
1633 * triggered, the only way to stop it seems to shut down the
1636 * Suggesting that we should never get it and if we do we're not
1637 * feeding TX packets into the MAC correctly if we do... Apparently,
1638 * it is valid only on mac version 5 and higher, but I couldn't
1639 * find a reference for that... Since I see them from time to time
1640 * on my card, this suggests an error in the tx path still...
1642 if (intr_status & BWI_INTR_PHY_TXERR) {
1643 if (mac->mac_flags & BWI_MAC_F_PHYE_RESET) {
1644 if_printf(ifp, "%s: intr PHY TX error\n", __func__);
1645 taskqueue_enqueue(sc->sc_tq, &sc->sc_restart_task);
1652 /* TODO: reset device */
1655 if (intr_status & BWI_INTR_TBTT)
1656 bwi_mac_config_ps(mac);
1658 if (intr_status & BWI_INTR_EO_ATIM)
1659 if_printf(ifp, "EO_ATIM\n");
1661 if (intr_status & BWI_INTR_PMQ) {
1663 if ((CSR_READ_4(sc, BWI_MAC_PS_STATUS) & 0x8) == 0)
1666 CSR_WRITE_2(sc, BWI_MAC_PS_STATUS, 0x2);
1669 if (intr_status & BWI_INTR_NOISE)
1670 if_printf(ifp, "intr noise\n");
1672 if (txrx_intr_status[0] & BWI_TXRX_INTR_RX) {
1673 rx_data = sc->sc_rxeof(sc);
1674 if (sc->sc_flags & BWI_F_STOP) {
1680 if (txrx_intr_status[3] & BWI_TXRX_INTR_RX) {
1681 sc->sc_txeof_status(sc);
1685 if (intr_status & BWI_INTR_TX_DONE) {
1690 /* Re-enable interrupts */
1691 bwi_enable_intrs(sc, BWI_INIT_INTRS);
1693 if (sc->sc_blink_led != NULL && sc->sc_led_blink) {
1694 int evt = BWI_LED_EVENT_NONE;
1696 if (tx && rx_data > 0) {
1697 if (sc->sc_rx_rate > sc->sc_tx_rate)
1698 evt = BWI_LED_EVENT_RX;
1700 evt = BWI_LED_EVENT_TX;
1702 evt = BWI_LED_EVENT_TX;
1703 } else if (rx_data > 0) {
1704 evt = BWI_LED_EVENT_RX;
1705 } else if (rx_data == 0) {
1706 evt = BWI_LED_EVENT_POLL;
1709 if (evt != BWI_LED_EVENT_NONE)
1710 bwi_led_event(sc, evt);
1717 bwi_scan_start(struct ieee80211com *ic)
1719 struct bwi_softc *sc = ic->ic_ifp->if_softc;
1722 /* Enable MAC beacon promiscuity */
1723 CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PASS_BCN);
1728 bwi_set_channel(struct ieee80211com *ic)
1730 struct bwi_softc *sc = ic->ic_ifp->if_softc;
1731 struct ieee80211_channel *c = ic->ic_curchan;
1732 struct bwi_mac *mac;
1735 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1736 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1737 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1738 bwi_rf_set_chan(mac, ieee80211_chan2ieee(ic, c), 0);
1740 sc->sc_rates = ieee80211_get_ratetable(c);
1743 * Setup radio tap channel freq and flags
1745 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
1746 htole16(c->ic_freq);
1747 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
1748 htole16(c->ic_flags & 0xffff);
1754 bwi_scan_end(struct ieee80211com *ic)
1756 struct bwi_softc *sc = ic->ic_ifp->if_softc;
1759 CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PASS_BCN);
1764 bwi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1766 struct bwi_vap *bvp = BWI_VAP(vap);
1767 const struct ieee80211_txparam *tp;
1768 struct ieee80211com *ic= vap->iv_ic;
1769 struct ifnet *ifp = ic->ic_ifp;
1770 enum ieee80211_state ostate = vap->iv_state;
1771 struct bwi_softc *sc = ifp->if_softc;
1772 struct bwi_mac *mac;
1777 callout_stop(&sc->sc_calib_ch);
1779 if (nstate == IEEE80211_S_INIT)
1780 sc->sc_txpwrcb_type = BWI_TXPWR_INIT;
1782 bwi_led_newstate(sc, nstate);
1784 error = bvp->bv_newstate(vap, nstate, arg);
1789 * Clear the BSSID when we stop a STA
1791 if (vap->iv_opmode == IEEE80211_M_STA) {
1792 if (ostate == IEEE80211_S_RUN && nstate != IEEE80211_S_RUN) {
1794 * Clear out the BSSID. If we reassociate to
1795 * the same AP, this will reinialize things
1798 if (ic->ic_opmode == IEEE80211_M_STA &&
1799 !(sc->sc_flags & BWI_F_STOP))
1800 bwi_set_bssid(sc, bwi_zero_addr);
1804 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
1806 } else if (nstate == IEEE80211_S_RUN) {
1807 bwi_set_bssid(sc, vap->iv_bss->ni_bssid);
1809 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1810 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1811 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1813 /* Initial TX power calibration */
1814 bwi_mac_calibrate_txpower(mac, BWI_TXPWR_INIT);
1816 sc->sc_txpwrcb_type = BWI_TXPWR_FORCE;
1818 sc->sc_txpwrcb_type = BWI_TXPWR_CALIB;
1821 /* Initializes ratectl for a node. */
1822 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1823 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1824 ieee80211_ratectl_node_init(vap->iv_bss);
1826 callout_reset(&sc->sc_calib_ch, hz, bwi_calibrate, sc);
1835 bwi_media_change(struct ifnet *ifp)
1837 int error = ieee80211_media_change(ifp);
1838 /* NB: only the fixed rate can change and that doesn't need a reset */
1839 return (error == ENETRESET ? 0 : error);
1843 bwi_dma_alloc(struct bwi_softc *sc)
1845 int error, i, has_txstats;
1846 bus_addr_t lowaddr = 0;
1847 bus_size_t tx_ring_sz, rx_ring_sz, desc_sz = 0;
1848 uint32_t txrx_ctrl_step = 0;
1851 for (i = 0; i < sc->sc_nmac; ++i) {
1852 if (sc->sc_mac[i].mac_flags & BWI_MAC_F_HAS_TXSTATS) {
1858 switch (sc->sc_bus_space) {
1859 case BWI_BUS_SPACE_30BIT:
1860 case BWI_BUS_SPACE_32BIT:
1861 if (sc->sc_bus_space == BWI_BUS_SPACE_30BIT)
1862 lowaddr = BWI_BUS_SPACE_MAXADDR;
1864 lowaddr = BUS_SPACE_MAXADDR_32BIT;
1865 desc_sz = sizeof(struct bwi_desc32);
1866 txrx_ctrl_step = 0x20;
1868 sc->sc_init_tx_ring = bwi_init_tx_ring32;
1869 sc->sc_free_tx_ring = bwi_free_tx_ring32;
1870 sc->sc_init_rx_ring = bwi_init_rx_ring32;
1871 sc->sc_free_rx_ring = bwi_free_rx_ring32;
1872 sc->sc_setup_rxdesc = bwi_setup_rx_desc32;
1873 sc->sc_setup_txdesc = bwi_setup_tx_desc32;
1874 sc->sc_rxeof = bwi_rxeof32;
1875 sc->sc_start_tx = bwi_start_tx32;
1877 sc->sc_init_txstats = bwi_init_txstats32;
1878 sc->sc_free_txstats = bwi_free_txstats32;
1879 sc->sc_txeof_status = bwi_txeof_status32;
1883 case BWI_BUS_SPACE_64BIT:
1884 lowaddr = BUS_SPACE_MAXADDR; /* XXX */
1885 desc_sz = sizeof(struct bwi_desc64);
1886 txrx_ctrl_step = 0x40;
1888 sc->sc_init_tx_ring = bwi_init_tx_ring64;
1889 sc->sc_free_tx_ring = bwi_free_tx_ring64;
1890 sc->sc_init_rx_ring = bwi_init_rx_ring64;
1891 sc->sc_free_rx_ring = bwi_free_rx_ring64;
1892 sc->sc_setup_rxdesc = bwi_setup_rx_desc64;
1893 sc->sc_setup_txdesc = bwi_setup_tx_desc64;
1894 sc->sc_rxeof = bwi_rxeof64;
1895 sc->sc_start_tx = bwi_start_tx64;
1897 sc->sc_init_txstats = bwi_init_txstats64;
1898 sc->sc_free_txstats = bwi_free_txstats64;
1899 sc->sc_txeof_status = bwi_txeof_status64;
1904 KASSERT(lowaddr != 0, ("lowaddr zero"));
1905 KASSERT(desc_sz != 0, ("desc_sz zero"));
1906 KASSERT(txrx_ctrl_step != 0, ("txrx_ctrl_step zero"));
1908 tx_ring_sz = roundup(desc_sz * BWI_TX_NDESC, BWI_RING_ALIGN);
1909 rx_ring_sz = roundup(desc_sz * BWI_RX_NDESC, BWI_RING_ALIGN);
1912 * Create top level DMA tag
1914 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), /* parent */
1915 BWI_ALIGN, 0, /* alignment, bounds */
1916 lowaddr, /* lowaddr */
1917 BUS_SPACE_MAXADDR, /* highaddr */
1918 NULL, NULL, /* filter, filterarg */
1919 MAXBSIZE, /* maxsize */
1920 BUS_SPACE_UNRESTRICTED, /* nsegments */
1921 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1922 BUS_DMA_ALLOCNOW, /* flags */
1923 NULL, NULL, /* lockfunc, lockarg */
1924 &sc->sc_parent_dtag);
1926 device_printf(sc->sc_dev, "can't create parent DMA tag\n");
1930 #define TXRX_CTRL(idx) (BWI_TXRX_CTRL_BASE + (idx) * txrx_ctrl_step)
1933 * Create TX ring DMA stuffs
1935 error = bus_dma_tag_create(sc->sc_parent_dtag,
1942 BUS_SPACE_MAXSIZE_32BIT,
1945 &sc->sc_txring_dtag);
1947 device_printf(sc->sc_dev, "can't create TX ring DMA tag\n");
1951 for (i = 0; i < BWI_TX_NRING; ++i) {
1952 error = bwi_dma_ring_alloc(sc, sc->sc_txring_dtag,
1953 &sc->sc_tx_rdata[i], tx_ring_sz,
1956 device_printf(sc->sc_dev, "%dth TX ring "
1957 "DMA alloc failed\n", i);
1963 * Create RX ring DMA stuffs
1965 error = bus_dma_tag_create(sc->sc_parent_dtag,
1972 BUS_SPACE_MAXSIZE_32BIT,
1975 &sc->sc_rxring_dtag);
1977 device_printf(sc->sc_dev, "can't create RX ring DMA tag\n");
1981 error = bwi_dma_ring_alloc(sc, sc->sc_rxring_dtag, &sc->sc_rx_rdata,
1982 rx_ring_sz, TXRX_CTRL(0));
1984 device_printf(sc->sc_dev, "RX ring DMA alloc failed\n");
1989 error = bwi_dma_txstats_alloc(sc, TXRX_CTRL(3), desc_sz);
1991 device_printf(sc->sc_dev,
1992 "TX stats DMA alloc failed\n");
1999 return bwi_dma_mbuf_create(sc);
2003 bwi_dma_free(struct bwi_softc *sc)
2005 if (sc->sc_txring_dtag != NULL) {
2008 for (i = 0; i < BWI_TX_NRING; ++i) {
2009 struct bwi_ring_data *rd = &sc->sc_tx_rdata[i];
2011 if (rd->rdata_desc != NULL) {
2012 bus_dmamap_unload(sc->sc_txring_dtag,
2014 bus_dmamem_free(sc->sc_txring_dtag,
2019 bus_dma_tag_destroy(sc->sc_txring_dtag);
2022 if (sc->sc_rxring_dtag != NULL) {
2023 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2025 if (rd->rdata_desc != NULL) {
2026 bus_dmamap_unload(sc->sc_rxring_dtag, rd->rdata_dmap);
2027 bus_dmamem_free(sc->sc_rxring_dtag, rd->rdata_desc,
2030 bus_dma_tag_destroy(sc->sc_rxring_dtag);
2033 bwi_dma_txstats_free(sc);
2034 bwi_dma_mbuf_destroy(sc, BWI_TX_NRING, 1);
2036 if (sc->sc_parent_dtag != NULL)
2037 bus_dma_tag_destroy(sc->sc_parent_dtag);
2041 bwi_dma_ring_alloc(struct bwi_softc *sc, bus_dma_tag_t dtag,
2042 struct bwi_ring_data *rd, bus_size_t size,
2047 error = bus_dmamem_alloc(dtag, &rd->rdata_desc,
2048 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2051 device_printf(sc->sc_dev, "can't allocate DMA mem\n");
2055 error = bus_dmamap_load(dtag, rd->rdata_dmap, rd->rdata_desc, size,
2056 bwi_dma_ring_addr, &rd->rdata_paddr,
2059 device_printf(sc->sc_dev, "can't load DMA mem\n");
2060 bus_dmamem_free(dtag, rd->rdata_desc, rd->rdata_dmap);
2061 rd->rdata_desc = NULL;
2065 rd->rdata_txrx_ctrl = txrx_ctrl;
2070 bwi_dma_txstats_alloc(struct bwi_softc *sc, uint32_t ctrl_base,
2073 struct bwi_txstats_data *st;
2074 bus_size_t dma_size;
2077 st = malloc(sizeof(*st), M_DEVBUF, M_NOWAIT | M_ZERO);
2079 device_printf(sc->sc_dev, "can't allocate txstats data\n");
2082 sc->sc_txstats = st;
2085 * Create TX stats descriptor DMA stuffs
2087 dma_size = roundup(desc_sz * BWI_TXSTATS_NDESC, BWI_RING_ALIGN);
2089 error = bus_dma_tag_create(sc->sc_parent_dtag,
2097 BUS_SPACE_MAXSIZE_32BIT,
2100 &st->stats_ring_dtag);
2102 device_printf(sc->sc_dev, "can't create txstats ring "
2107 error = bus_dmamem_alloc(st->stats_ring_dtag, &st->stats_ring,
2108 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2109 &st->stats_ring_dmap);
2111 device_printf(sc->sc_dev, "can't allocate txstats ring "
2113 bus_dma_tag_destroy(st->stats_ring_dtag);
2114 st->stats_ring_dtag = NULL;
2118 error = bus_dmamap_load(st->stats_ring_dtag, st->stats_ring_dmap,
2119 st->stats_ring, dma_size,
2120 bwi_dma_ring_addr, &st->stats_ring_paddr,
2123 device_printf(sc->sc_dev, "can't load txstats ring DMA mem\n");
2124 bus_dmamem_free(st->stats_ring_dtag, st->stats_ring,
2125 st->stats_ring_dmap);
2126 bus_dma_tag_destroy(st->stats_ring_dtag);
2127 st->stats_ring_dtag = NULL;
2132 * Create TX stats DMA stuffs
2134 dma_size = roundup(sizeof(struct bwi_txstats) * BWI_TXSTATS_NDESC,
2137 error = bus_dma_tag_create(sc->sc_parent_dtag,
2145 BUS_SPACE_MAXSIZE_32BIT,
2150 device_printf(sc->sc_dev, "can't create txstats DMA tag\n");
2154 error = bus_dmamem_alloc(st->stats_dtag, (void **)&st->stats,
2155 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2158 device_printf(sc->sc_dev, "can't allocate txstats DMA mem\n");
2159 bus_dma_tag_destroy(st->stats_dtag);
2160 st->stats_dtag = NULL;
2164 error = bus_dmamap_load(st->stats_dtag, st->stats_dmap, st->stats,
2165 dma_size, bwi_dma_ring_addr, &st->stats_paddr,
2168 device_printf(sc->sc_dev, "can't load txstats DMA mem\n");
2169 bus_dmamem_free(st->stats_dtag, st->stats, st->stats_dmap);
2170 bus_dma_tag_destroy(st->stats_dtag);
2171 st->stats_dtag = NULL;
2175 st->stats_ctrl_base = ctrl_base;
2180 bwi_dma_txstats_free(struct bwi_softc *sc)
2182 struct bwi_txstats_data *st;
2184 if (sc->sc_txstats == NULL)
2186 st = sc->sc_txstats;
2188 if (st->stats_ring_dtag != NULL) {
2189 bus_dmamap_unload(st->stats_ring_dtag, st->stats_ring_dmap);
2190 bus_dmamem_free(st->stats_ring_dtag, st->stats_ring,
2191 st->stats_ring_dmap);
2192 bus_dma_tag_destroy(st->stats_ring_dtag);
2195 if (st->stats_dtag != NULL) {
2196 bus_dmamap_unload(st->stats_dtag, st->stats_dmap);
2197 bus_dmamem_free(st->stats_dtag, st->stats, st->stats_dmap);
2198 bus_dma_tag_destroy(st->stats_dtag);
2205 bwi_dma_ring_addr(void *arg, bus_dma_segment_t *seg, int nseg, int error)
2207 KASSERT(nseg == 1, ("too many segments\n"));
2208 *((bus_addr_t *)arg) = seg->ds_addr;
2212 bwi_dma_mbuf_create(struct bwi_softc *sc)
2214 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2215 int i, j, k, ntx, error;
2218 * Create TX/RX mbuf DMA tag
2220 error = bus_dma_tag_create(sc->sc_parent_dtag,
2228 BUS_SPACE_MAXSIZE_32BIT,
2233 device_printf(sc->sc_dev, "can't create mbuf DMA tag\n");
2240 * Create TX mbuf DMA map
2242 for (i = 0; i < BWI_TX_NRING; ++i) {
2243 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[i];
2245 for (j = 0; j < BWI_TX_NDESC; ++j) {
2246 error = bus_dmamap_create(sc->sc_buf_dtag, 0,
2247 &tbd->tbd_buf[j].tb_dmap);
2249 device_printf(sc->sc_dev, "can't create "
2250 "%dth tbd, %dth DMA map\n", i, j);
2253 for (k = 0; k < j; ++k) {
2254 bus_dmamap_destroy(sc->sc_buf_dtag,
2255 tbd->tbd_buf[k].tb_dmap);
2264 * Create RX mbuf DMA map and a spare DMA map
2266 error = bus_dmamap_create(sc->sc_buf_dtag, 0,
2267 &rbd->rbd_tmp_dmap);
2269 device_printf(sc->sc_dev,
2270 "can't create spare RX buf DMA map\n");
2274 for (j = 0; j < BWI_RX_NDESC; ++j) {
2275 error = bus_dmamap_create(sc->sc_buf_dtag, 0,
2276 &rbd->rbd_buf[j].rb_dmap);
2278 device_printf(sc->sc_dev, "can't create %dth "
2279 "RX buf DMA map\n", j);
2281 for (k = 0; k < j; ++k) {
2282 bus_dmamap_destroy(sc->sc_buf_dtag,
2283 rbd->rbd_buf[j].rb_dmap);
2285 bus_dmamap_destroy(sc->sc_buf_dtag,
2293 bwi_dma_mbuf_destroy(sc, ntx, 0);
2298 bwi_dma_mbuf_destroy(struct bwi_softc *sc, int ntx, int nrx)
2302 if (sc->sc_buf_dtag == NULL)
2305 for (i = 0; i < ntx; ++i) {
2306 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[i];
2308 for (j = 0; j < BWI_TX_NDESC; ++j) {
2309 struct bwi_txbuf *tb = &tbd->tbd_buf[j];
2311 if (tb->tb_mbuf != NULL) {
2312 bus_dmamap_unload(sc->sc_buf_dtag,
2314 m_freem(tb->tb_mbuf);
2316 if (tb->tb_ni != NULL)
2317 ieee80211_free_node(tb->tb_ni);
2318 bus_dmamap_destroy(sc->sc_buf_dtag, tb->tb_dmap);
2323 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2325 bus_dmamap_destroy(sc->sc_buf_dtag, rbd->rbd_tmp_dmap);
2326 for (j = 0; j < BWI_RX_NDESC; ++j) {
2327 struct bwi_rxbuf *rb = &rbd->rbd_buf[j];
2329 if (rb->rb_mbuf != NULL) {
2330 bus_dmamap_unload(sc->sc_buf_dtag,
2332 m_freem(rb->rb_mbuf);
2334 bus_dmamap_destroy(sc->sc_buf_dtag, rb->rb_dmap);
2338 bus_dma_tag_destroy(sc->sc_buf_dtag);
2339 sc->sc_buf_dtag = NULL;
2343 bwi_enable_intrs(struct bwi_softc *sc, uint32_t enable_intrs)
2345 CSR_SETBITS_4(sc, BWI_MAC_INTR_MASK, enable_intrs);
2349 bwi_disable_intrs(struct bwi_softc *sc, uint32_t disable_intrs)
2351 CSR_CLRBITS_4(sc, BWI_MAC_INTR_MASK, disable_intrs);
2355 bwi_init_tx_ring32(struct bwi_softc *sc, int ring_idx)
2357 struct bwi_ring_data *rd;
2358 struct bwi_txbuf_data *tbd;
2359 uint32_t val, addr_hi, addr_lo;
2361 KASSERT(ring_idx < BWI_TX_NRING, ("ring_idx %d", ring_idx));
2362 rd = &sc->sc_tx_rdata[ring_idx];
2363 tbd = &sc->sc_tx_bdata[ring_idx];
2368 bzero(rd->rdata_desc, sizeof(struct bwi_desc32) * BWI_TX_NDESC);
2369 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap,
2370 BUS_DMASYNC_PREWRITE);
2372 addr_lo = __SHIFTOUT(rd->rdata_paddr, BWI_TXRX32_RINGINFO_ADDR_MASK);
2373 addr_hi = __SHIFTOUT(rd->rdata_paddr, BWI_TXRX32_RINGINFO_FUNC_MASK);
2375 val = __SHIFTIN(addr_lo, BWI_TXRX32_RINGINFO_ADDR_MASK) |
2376 __SHIFTIN(BWI_TXRX32_RINGINFO_FUNC_TXRX,
2377 BWI_TXRX32_RINGINFO_FUNC_MASK);
2378 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_RINGINFO, val);
2380 val = __SHIFTIN(addr_hi, BWI_TXRX32_CTRL_ADDRHI_MASK) |
2381 BWI_TXRX32_CTRL_ENABLE;
2382 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_CTRL, val);
2388 bwi_init_rxdesc_ring32(struct bwi_softc *sc, uint32_t ctrl_base,
2389 bus_addr_t paddr, int hdr_size, int ndesc)
2391 uint32_t val, addr_hi, addr_lo;
2393 addr_lo = __SHIFTOUT(paddr, BWI_TXRX32_RINGINFO_ADDR_MASK);
2394 addr_hi = __SHIFTOUT(paddr, BWI_TXRX32_RINGINFO_FUNC_MASK);
2396 val = __SHIFTIN(addr_lo, BWI_TXRX32_RINGINFO_ADDR_MASK) |
2397 __SHIFTIN(BWI_TXRX32_RINGINFO_FUNC_TXRX,
2398 BWI_TXRX32_RINGINFO_FUNC_MASK);
2399 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_RINGINFO, val);
2401 val = __SHIFTIN(hdr_size, BWI_RX32_CTRL_HDRSZ_MASK) |
2402 __SHIFTIN(addr_hi, BWI_TXRX32_CTRL_ADDRHI_MASK) |
2403 BWI_TXRX32_CTRL_ENABLE;
2404 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_CTRL, val);
2406 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_INDEX,
2407 (ndesc - 1) * sizeof(struct bwi_desc32));
2411 bwi_init_rx_ring32(struct bwi_softc *sc)
2413 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2416 sc->sc_rx_bdata.rbd_idx = 0;
2418 for (i = 0; i < BWI_RX_NDESC; ++i) {
2419 error = bwi_newbuf(sc, i, 1);
2421 device_printf(sc->sc_dev,
2422 "can't allocate %dth RX buffer\n", i);
2426 bus_dmamap_sync(sc->sc_rxring_dtag, rd->rdata_dmap,
2427 BUS_DMASYNC_PREWRITE);
2429 bwi_init_rxdesc_ring32(sc, rd->rdata_txrx_ctrl, rd->rdata_paddr,
2430 sizeof(struct bwi_rxbuf_hdr), BWI_RX_NDESC);
2435 bwi_init_txstats32(struct bwi_softc *sc)
2437 struct bwi_txstats_data *st = sc->sc_txstats;
2438 bus_addr_t stats_paddr;
2441 bzero(st->stats, BWI_TXSTATS_NDESC * sizeof(struct bwi_txstats));
2442 bus_dmamap_sync(st->stats_dtag, st->stats_dmap, BUS_DMASYNC_PREWRITE);
2446 stats_paddr = st->stats_paddr;
2447 for (i = 0; i < BWI_TXSTATS_NDESC; ++i) {
2448 bwi_setup_desc32(sc, st->stats_ring, BWI_TXSTATS_NDESC, i,
2449 stats_paddr, sizeof(struct bwi_txstats), 0);
2450 stats_paddr += sizeof(struct bwi_txstats);
2452 bus_dmamap_sync(st->stats_ring_dtag, st->stats_ring_dmap,
2453 BUS_DMASYNC_PREWRITE);
2455 bwi_init_rxdesc_ring32(sc, st->stats_ctrl_base,
2456 st->stats_ring_paddr, 0, BWI_TXSTATS_NDESC);
2461 bwi_setup_rx_desc32(struct bwi_softc *sc, int buf_idx, bus_addr_t paddr,
2464 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2466 KASSERT(buf_idx < BWI_RX_NDESC, ("buf_idx %d", buf_idx));
2467 bwi_setup_desc32(sc, rd->rdata_desc, BWI_RX_NDESC, buf_idx,
2472 bwi_setup_tx_desc32(struct bwi_softc *sc, struct bwi_ring_data *rd,
2473 int buf_idx, bus_addr_t paddr, int buf_len)
2475 KASSERT(buf_idx < BWI_TX_NDESC, ("buf_idx %d", buf_idx));
2476 bwi_setup_desc32(sc, rd->rdata_desc, BWI_TX_NDESC, buf_idx,
2481 bwi_init_tx_ring64(struct bwi_softc *sc, int ring_idx)
2488 bwi_init_rx_ring64(struct bwi_softc *sc)
2495 bwi_init_txstats64(struct bwi_softc *sc)
2502 bwi_setup_rx_desc64(struct bwi_softc *sc, int buf_idx, bus_addr_t paddr,
2509 bwi_setup_tx_desc64(struct bwi_softc *sc, struct bwi_ring_data *rd,
2510 int buf_idx, bus_addr_t paddr, int buf_len)
2516 bwi_dma_buf_addr(void *arg, bus_dma_segment_t *seg, int nseg,
2517 bus_size_t mapsz __unused, int error)
2520 KASSERT(nseg == 1, ("too many segments(%d)\n", nseg));
2521 *((bus_addr_t *)arg) = seg->ds_addr;
2526 bwi_newbuf(struct bwi_softc *sc, int buf_idx, int init)
2528 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2529 struct bwi_rxbuf *rxbuf = &rbd->rbd_buf[buf_idx];
2530 struct bwi_rxbuf_hdr *hdr;
2536 KASSERT(buf_idx < BWI_RX_NDESC, ("buf_idx %d", buf_idx));
2538 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2543 * If the NIC is up and running, we need to:
2544 * - Clear RX buffer's header.
2545 * - Restore RX descriptor settings.
2552 m->m_len = m->m_pkthdr.len = MCLBYTES;
2555 * Try to load RX buf into temporary DMA map
2557 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, rbd->rbd_tmp_dmap, m,
2558 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT);
2563 * See the comment above
2572 bus_dmamap_unload(sc->sc_buf_dtag, rxbuf->rb_dmap);
2574 rxbuf->rb_paddr = paddr;
2577 * Swap RX buf's DMA map with the loaded temporary one
2579 map = rxbuf->rb_dmap;
2580 rxbuf->rb_dmap = rbd->rbd_tmp_dmap;
2581 rbd->rbd_tmp_dmap = map;
2585 * Clear RX buf header
2587 hdr = mtod(rxbuf->rb_mbuf, struct bwi_rxbuf_hdr *);
2588 bzero(hdr, sizeof(*hdr));
2589 bus_dmamap_sync(sc->sc_buf_dtag, rxbuf->rb_dmap, BUS_DMASYNC_PREWRITE);
2592 * Setup RX buf descriptor
2594 sc->sc_setup_rxdesc(sc, buf_idx, rxbuf->rb_paddr,
2595 rxbuf->rb_mbuf->m_len - sizeof(*hdr));
2600 bwi_set_addr_filter(struct bwi_softc *sc, uint16_t addr_ofs,
2601 const uint8_t *addr)
2605 CSR_WRITE_2(sc, BWI_ADDR_FILTER_CTRL,
2606 BWI_ADDR_FILTER_CTRL_SET | addr_ofs);
2608 for (i = 0; i < (IEEE80211_ADDR_LEN / 2); ++i) {
2611 addr_val = (uint16_t)addr[i * 2] |
2612 (((uint16_t)addr[(i * 2) + 1]) << 8);
2613 CSR_WRITE_2(sc, BWI_ADDR_FILTER_DATA, addr_val);
2618 bwi_rxeof(struct bwi_softc *sc, int end_idx)
2620 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2621 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2622 struct ifnet *ifp = sc->sc_ifp;
2623 struct ieee80211com *ic = ifp->if_l2com;
2624 int idx, rx_data = 0;
2627 while (idx != end_idx) {
2628 struct bwi_rxbuf *rb = &rbd->rbd_buf[idx];
2629 struct bwi_rxbuf_hdr *hdr;
2630 struct ieee80211_frame_min *wh;
2631 struct ieee80211_node *ni;
2635 int buflen, wh_ofs, hdr_extra, rssi, noise, type, rate;
2638 bus_dmamap_sync(sc->sc_buf_dtag, rb->rb_dmap,
2639 BUS_DMASYNC_POSTREAD);
2641 if (bwi_newbuf(sc, idx, 0)) {
2646 hdr = mtod(m, struct bwi_rxbuf_hdr *);
2647 flags2 = le16toh(hdr->rxh_flags2);
2650 if (flags2 & BWI_RXH_F2_TYPE2FRAME)
2652 wh_ofs = hdr_extra + 6; /* XXX magic number */
2654 buflen = le16toh(hdr->rxh_buflen);
2655 if (buflen < BWI_FRAME_MIN_LEN(wh_ofs)) {
2656 if_printf(ifp, "%s: zero length data, hdr_extra %d\n",
2657 __func__, hdr_extra);
2663 plcp = ((const uint8_t *)(hdr + 1) + hdr_extra);
2664 rssi = bwi_calc_rssi(sc, hdr);
2665 noise = bwi_calc_noise(sc);
2667 m->m_pkthdr.rcvif = ifp;
2668 m->m_len = m->m_pkthdr.len = buflen + sizeof(*hdr);
2669 m_adj(m, sizeof(*hdr) + wh_ofs);
2671 if (htole16(hdr->rxh_flags1) & BWI_RXH_F1_OFDM)
2672 rate = bwi_ofdm_plcp2rate(plcp);
2674 rate = bwi_ds_plcp2rate(plcp);
2677 if (ieee80211_radiotap_active(ic))
2678 bwi_rx_radiotap(sc, m, hdr, plcp, rate, rssi, noise);
2680 m_adj(m, -IEEE80211_CRC_LEN);
2684 wh = mtod(m, struct ieee80211_frame_min *);
2685 ni = ieee80211_find_rxnode(ic, wh);
2687 type = ieee80211_input(ni, m, rssi - noise, noise);
2688 ieee80211_free_node(ni);
2690 type = ieee80211_input_all(ic, m, rssi - noise, noise);
2691 if (type == IEEE80211_FC0_TYPE_DATA) {
2693 sc->sc_rx_rate = rate;
2698 idx = (idx + 1) % BWI_RX_NDESC;
2700 if (sc->sc_flags & BWI_F_STOP) {
2702 * Take the fast lane, don't do
2703 * any damage to softc
2710 bus_dmamap_sync(sc->sc_rxring_dtag, rd->rdata_dmap,
2711 BUS_DMASYNC_PREWRITE);
2717 bwi_rxeof32(struct bwi_softc *sc)
2719 uint32_t val, rx_ctrl;
2720 int end_idx, rx_data;
2722 rx_ctrl = sc->sc_rx_rdata.rdata_txrx_ctrl;
2724 val = CSR_READ_4(sc, rx_ctrl + BWI_RX32_STATUS);
2725 end_idx = __SHIFTOUT(val, BWI_RX32_STATUS_INDEX_MASK) /
2726 sizeof(struct bwi_desc32);
2728 rx_data = bwi_rxeof(sc, end_idx);
2730 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_INDEX,
2731 end_idx * sizeof(struct bwi_desc32));
2737 bwi_rxeof64(struct bwi_softc *sc)
2744 bwi_reset_rx_ring32(struct bwi_softc *sc, uint32_t rx_ctrl)
2748 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_CTRL, 0);
2752 for (i = 0; i < NRETRY; ++i) {
2755 status = CSR_READ_4(sc, rx_ctrl + BWI_RX32_STATUS);
2756 if (__SHIFTOUT(status, BWI_RX32_STATUS_STATE_MASK) ==
2757 BWI_RX32_STATUS_STATE_DISABLED)
2763 device_printf(sc->sc_dev, "reset rx ring timedout\n");
2767 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_RINGINFO, 0);
2771 bwi_free_txstats32(struct bwi_softc *sc)
2773 bwi_reset_rx_ring32(sc, sc->sc_txstats->stats_ctrl_base);
2777 bwi_free_rx_ring32(struct bwi_softc *sc)
2779 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2780 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2783 bwi_reset_rx_ring32(sc, rd->rdata_txrx_ctrl);
2785 for (i = 0; i < BWI_RX_NDESC; ++i) {
2786 struct bwi_rxbuf *rb = &rbd->rbd_buf[i];
2788 if (rb->rb_mbuf != NULL) {
2789 bus_dmamap_unload(sc->sc_buf_dtag, rb->rb_dmap);
2790 m_freem(rb->rb_mbuf);
2797 bwi_free_tx_ring32(struct bwi_softc *sc, int ring_idx)
2799 struct bwi_ring_data *rd;
2800 struct bwi_txbuf_data *tbd;
2801 struct ifnet *ifp = sc->sc_ifp;
2802 uint32_t state, val;
2805 KASSERT(ring_idx < BWI_TX_NRING, ("ring_idx %d", ring_idx));
2806 rd = &sc->sc_tx_rdata[ring_idx];
2807 tbd = &sc->sc_tx_bdata[ring_idx];
2811 for (i = 0; i < NRETRY; ++i) {
2812 val = CSR_READ_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_STATUS);
2813 state = __SHIFTOUT(val, BWI_TX32_STATUS_STATE_MASK);
2814 if (state == BWI_TX32_STATUS_STATE_DISABLED ||
2815 state == BWI_TX32_STATUS_STATE_IDLE ||
2816 state == BWI_TX32_STATUS_STATE_STOPPED)
2822 if_printf(ifp, "%s: wait for TX ring(%d) stable timed out\n",
2823 __func__, ring_idx);
2826 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_CTRL, 0);
2827 for (i = 0; i < NRETRY; ++i) {
2828 val = CSR_READ_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_STATUS);
2829 state = __SHIFTOUT(val, BWI_TX32_STATUS_STATE_MASK);
2830 if (state == BWI_TX32_STATUS_STATE_DISABLED)
2836 if_printf(ifp, "%s: reset TX ring (%d) timed out\n",
2837 __func__, ring_idx);
2843 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_RINGINFO, 0);
2845 for (i = 0; i < BWI_TX_NDESC; ++i) {
2846 struct bwi_txbuf *tb = &tbd->tbd_buf[i];
2848 if (tb->tb_mbuf != NULL) {
2849 bus_dmamap_unload(sc->sc_buf_dtag, tb->tb_dmap);
2850 m_freem(tb->tb_mbuf);
2853 if (tb->tb_ni != NULL) {
2854 ieee80211_free_node(tb->tb_ni);
2861 bwi_free_txstats64(struct bwi_softc *sc)
2867 bwi_free_rx_ring64(struct bwi_softc *sc)
2873 bwi_free_tx_ring64(struct bwi_softc *sc, int ring_idx)
2878 /* XXX does not belong here */
2879 #define IEEE80211_OFDM_PLCP_RATE_MASK __BITS(3, 0)
2880 #define IEEE80211_OFDM_PLCP_LEN_MASK __BITS(16, 5)
2882 static __inline void
2883 bwi_ofdm_plcp_header(uint32_t *plcp0, int pkt_len, uint8_t rate)
2887 plcp = __SHIFTIN(ieee80211_rate2plcp(rate, IEEE80211_T_OFDM),
2888 IEEE80211_OFDM_PLCP_RATE_MASK) |
2889 __SHIFTIN(pkt_len, IEEE80211_OFDM_PLCP_LEN_MASK);
2890 *plcp0 = htole32(plcp);
2893 static __inline void
2894 bwi_ds_plcp_header(struct ieee80211_ds_plcp_hdr *plcp, int pkt_len,
2897 int len, service, pkt_bitlen;
2899 pkt_bitlen = pkt_len * NBBY;
2900 len = howmany(pkt_bitlen * 2, rate);
2902 service = IEEE80211_PLCP_SERVICE_LOCKED;
2903 if (rate == (11 * 2)) {
2907 * PLCP service field needs to be adjusted,
2908 * if TX rate is 11Mbytes/s
2910 pkt_bitlen1 = len * 11;
2911 if (pkt_bitlen1 - pkt_bitlen >= NBBY)
2912 service |= IEEE80211_PLCP_SERVICE_LENEXT7;
2915 plcp->i_signal = ieee80211_rate2plcp(rate, IEEE80211_T_CCK);
2916 plcp->i_service = service;
2917 plcp->i_length = htole16(len);
2918 /* NOTE: do NOT touch i_crc */
2921 static __inline void
2922 bwi_plcp_header(const struct ieee80211_rate_table *rt,
2923 void *plcp, int pkt_len, uint8_t rate)
2925 enum ieee80211_phytype modtype;
2928 * Assume caller has zeroed 'plcp'
2930 modtype = ieee80211_rate2phytype(rt, rate);
2931 if (modtype == IEEE80211_T_OFDM)
2932 bwi_ofdm_plcp_header(plcp, pkt_len, rate);
2933 else if (modtype == IEEE80211_T_DS)
2934 bwi_ds_plcp_header(plcp, pkt_len, rate);
2936 panic("unsupport modulation type %u\n", modtype);
2940 bwi_encap(struct bwi_softc *sc, int idx, struct mbuf *m,
2941 struct ieee80211_node *ni)
2943 struct ieee80211vap *vap = ni->ni_vap;
2944 struct ifnet *ifp = sc->sc_ifp;
2945 struct ieee80211com *ic = ifp->if_l2com;
2946 struct bwi_ring_data *rd = &sc->sc_tx_rdata[BWI_TX_DATA_RING];
2947 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
2948 struct bwi_txbuf *tb = &tbd->tbd_buf[idx];
2949 struct bwi_mac *mac;
2950 struct bwi_txbuf_hdr *hdr;
2951 struct ieee80211_frame *wh;
2952 const struct ieee80211_txparam *tp;
2953 uint8_t rate, rate_fb;
2957 int type, ismcast, pkt_len, error, rix;
2963 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
2964 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
2965 mac = (struct bwi_mac *)sc->sc_cur_regwin;
2967 wh = mtod(m, struct ieee80211_frame *);
2968 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2969 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2971 /* Get 802.11 frame len before prepending TX header */
2972 pkt_len = m->m_pkthdr.len + IEEE80211_CRC_LEN;
2977 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2978 if (type != IEEE80211_FC0_TYPE_DATA || (m->m_flags & M_EAPOL)) {
2979 rate = rate_fb = tp->mgmtrate;
2980 } else if (ismcast) {
2981 rate = rate_fb = tp->mcastrate;
2982 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
2983 rate = rate_fb = tp->ucastrate;
2985 rix = ieee80211_ratectl_rate(ni, NULL, pkt_len);
2986 rate = ni->ni_txrate;
2989 rate_fb = ni->ni_rates.rs_rates[rix-1] &
2995 tb->tb_rate[0] = rate;
2996 tb->tb_rate[1] = rate_fb;
2997 sc->sc_tx_rate = rate;
3002 if (ieee80211_radiotap_active_vap(vap)) {
3003 sc->sc_tx_th.wt_flags = 0;
3004 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3005 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3006 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_DS &&
3007 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
3009 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3011 sc->sc_tx_th.wt_rate = rate;
3013 ieee80211_radiotap_tx(vap, m);
3017 * Setup the embedded TX header
3019 M_PREPEND(m, sizeof(*hdr), M_DONTWAIT);
3021 if_printf(ifp, "%s: prepend TX header failed\n", __func__);
3024 hdr = mtod(m, struct bwi_txbuf_hdr *);
3026 bzero(hdr, sizeof(*hdr));
3028 bcopy(wh->i_fc, hdr->txh_fc, sizeof(hdr->txh_fc));
3029 bcopy(wh->i_addr1, hdr->txh_addr1, sizeof(hdr->txh_addr1));
3034 dur = ieee80211_ack_duration(sc->sc_rates, rate,
3035 ic->ic_flags & ~IEEE80211_F_SHPREAMBLE);
3037 hdr->txh_fb_duration = htole16(dur);
3040 hdr->txh_id = __SHIFTIN(BWI_TX_DATA_RING, BWI_TXH_ID_RING_MASK) |
3041 __SHIFTIN(idx, BWI_TXH_ID_IDX_MASK);
3043 bwi_plcp_header(sc->sc_rates, hdr->txh_plcp, pkt_len, rate);
3044 bwi_plcp_header(sc->sc_rates, hdr->txh_fb_plcp, pkt_len, rate_fb);
3046 phy_ctrl = __SHIFTIN(mac->mac_rf.rf_ant_mode,
3047 BWI_TXH_PHY_C_ANTMODE_MASK);
3048 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM)
3049 phy_ctrl |= BWI_TXH_PHY_C_OFDM;
3050 else if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && rate != (2 * 1))
3051 phy_ctrl |= BWI_TXH_PHY_C_SHPREAMBLE;
3053 mac_ctrl = BWI_TXH_MAC_C_HWSEQ | BWI_TXH_MAC_C_FIRST_FRAG;
3055 mac_ctrl |= BWI_TXH_MAC_C_ACK;
3056 if (ieee80211_rate2phytype(sc->sc_rates, rate_fb) == IEEE80211_T_OFDM)
3057 mac_ctrl |= BWI_TXH_MAC_C_FB_OFDM;
3059 hdr->txh_mac_ctrl = htole32(mac_ctrl);
3060 hdr->txh_phy_ctrl = htole16(phy_ctrl);
3062 /* Catch any further usage */
3067 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3068 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT);
3069 if (error && error != EFBIG) {
3070 if_printf(ifp, "%s: can't load TX buffer (1) %d\n",
3075 if (error) { /* error == EFBIG */
3078 m_new = m_defrag(m, M_DONTWAIT);
3079 if (m_new == NULL) {
3080 if_printf(ifp, "%s: can't defrag TX buffer\n",
3088 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3089 bwi_dma_buf_addr, &paddr,
3092 if_printf(ifp, "%s: can't load TX buffer (2) %d\n",
3099 bus_dmamap_sync(sc->sc_buf_dtag, tb->tb_dmap, BUS_DMASYNC_PREWRITE);
3105 p = mtod(m, const uint8_t *);
3106 for (i = 0; i < m->m_pkthdr.len; ++i) {
3107 if (i != 0 && i % 8 == 0)
3109 printf("%02x ", p[i]);
3113 DPRINTF(sc, BWI_DBG_TX, "idx %d, pkt_len %d, buflen %d\n",
3114 idx, pkt_len, m->m_pkthdr.len);
3116 /* Setup TX descriptor */
3117 sc->sc_setup_txdesc(sc, rd, idx, paddr, m->m_pkthdr.len);
3118 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap,
3119 BUS_DMASYNC_PREWRITE);
3122 sc->sc_start_tx(sc, rd->rdata_txrx_ctrl, idx);
3131 bwi_encap_raw(struct bwi_softc *sc, int idx, struct mbuf *m,
3132 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
3134 struct ifnet *ifp = sc->sc_ifp;
3135 struct ieee80211vap *vap = ni->ni_vap;
3136 struct ieee80211com *ic = ni->ni_ic;
3137 struct bwi_ring_data *rd = &sc->sc_tx_rdata[BWI_TX_DATA_RING];
3138 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
3139 struct bwi_txbuf *tb = &tbd->tbd_buf[idx];
3140 struct bwi_mac *mac;
3141 struct bwi_txbuf_hdr *hdr;
3142 struct ieee80211_frame *wh;
3143 uint8_t rate, rate_fb;
3147 int ismcast, pkt_len, error;
3149 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3150 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3151 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3153 wh = mtod(m, struct ieee80211_frame *);
3154 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
3156 /* Get 802.11 frame len before prepending TX header */
3157 pkt_len = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3162 rate = params->ibp_rate0;
3163 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3164 /* XXX fall back to mcast/mgmt rate? */
3168 if (params->ibp_try1 != 0) {
3169 rate_fb = params->ibp_rate1;
3170 if (!ieee80211_isratevalid(ic->ic_rt, rate_fb)) {
3171 /* XXX fall back to rate0? */
3177 tb->tb_rate[0] = rate;
3178 tb->tb_rate[1] = rate_fb;
3179 sc->sc_tx_rate = rate;
3184 if (ieee80211_radiotap_active_vap(vap)) {
3185 sc->sc_tx_th.wt_flags = 0;
3186 /* XXX IEEE80211_BPF_CRYPTO */
3187 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3188 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3189 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
3190 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3191 sc->sc_tx_th.wt_rate = rate;
3193 ieee80211_radiotap_tx(vap, m);
3197 * Setup the embedded TX header
3199 M_PREPEND(m, sizeof(*hdr), M_DONTWAIT);
3201 if_printf(ifp, "%s: prepend TX header failed\n", __func__);
3204 hdr = mtod(m, struct bwi_txbuf_hdr *);
3206 bzero(hdr, sizeof(*hdr));
3208 bcopy(wh->i_fc, hdr->txh_fc, sizeof(hdr->txh_fc));
3209 bcopy(wh->i_addr1, hdr->txh_addr1, sizeof(hdr->txh_addr1));
3211 mac_ctrl = BWI_TXH_MAC_C_HWSEQ | BWI_TXH_MAC_C_FIRST_FRAG;
3212 if (!ismcast && (params->ibp_flags & IEEE80211_BPF_NOACK) == 0) {
3215 dur = ieee80211_ack_duration(sc->sc_rates, rate_fb, 0);
3217 hdr->txh_fb_duration = htole16(dur);
3218 mac_ctrl |= BWI_TXH_MAC_C_ACK;
3221 hdr->txh_id = __SHIFTIN(BWI_TX_DATA_RING, BWI_TXH_ID_RING_MASK) |
3222 __SHIFTIN(idx, BWI_TXH_ID_IDX_MASK);
3224 bwi_plcp_header(sc->sc_rates, hdr->txh_plcp, pkt_len, rate);
3225 bwi_plcp_header(sc->sc_rates, hdr->txh_fb_plcp, pkt_len, rate_fb);
3227 phy_ctrl = __SHIFTIN(mac->mac_rf.rf_ant_mode,
3228 BWI_TXH_PHY_C_ANTMODE_MASK);
3229 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM) {
3230 phy_ctrl |= BWI_TXH_PHY_C_OFDM;
3231 mac_ctrl |= BWI_TXH_MAC_C_FB_OFDM;
3232 } else if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
3233 phy_ctrl |= BWI_TXH_PHY_C_SHPREAMBLE;
3235 hdr->txh_mac_ctrl = htole32(mac_ctrl);
3236 hdr->txh_phy_ctrl = htole16(phy_ctrl);
3238 /* Catch any further usage */
3243 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3244 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT);
3248 if (error != EFBIG) {
3249 if_printf(ifp, "%s: can't load TX buffer (1) %d\n",
3253 m_new = m_defrag(m, M_DONTWAIT);
3254 if (m_new == NULL) {
3255 if_printf(ifp, "%s: can't defrag TX buffer\n",
3261 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3262 bwi_dma_buf_addr, &paddr,
3265 if_printf(ifp, "%s: can't load TX buffer (2) %d\n",
3271 bus_dmamap_sync(sc->sc_buf_dtag, tb->tb_dmap, BUS_DMASYNC_PREWRITE);
3276 DPRINTF(sc, BWI_DBG_TX, "idx %d, pkt_len %d, buflen %d\n",
3277 idx, pkt_len, m->m_pkthdr.len);
3279 /* Setup TX descriptor */
3280 sc->sc_setup_txdesc(sc, rd, idx, paddr, m->m_pkthdr.len);
3281 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap,
3282 BUS_DMASYNC_PREWRITE);
3285 sc->sc_start_tx(sc, rd->rdata_txrx_ctrl, idx);
3293 bwi_start_tx32(struct bwi_softc *sc, uint32_t tx_ctrl, int idx)
3295 idx = (idx + 1) % BWI_TX_NDESC;
3296 CSR_WRITE_4(sc, tx_ctrl + BWI_TX32_INDEX,
3297 idx * sizeof(struct bwi_desc32));
3301 bwi_start_tx64(struct bwi_softc *sc, uint32_t tx_ctrl, int idx)
3307 bwi_txeof_status32(struct bwi_softc *sc)
3309 struct ifnet *ifp = sc->sc_ifp;
3310 uint32_t val, ctrl_base;
3313 ctrl_base = sc->sc_txstats->stats_ctrl_base;
3315 val = CSR_READ_4(sc, ctrl_base + BWI_RX32_STATUS);
3316 end_idx = __SHIFTOUT(val, BWI_RX32_STATUS_INDEX_MASK) /
3317 sizeof(struct bwi_desc32);
3319 bwi_txeof_status(sc, end_idx);
3321 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_INDEX,
3322 end_idx * sizeof(struct bwi_desc32));
3324 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
3329 bwi_txeof_status64(struct bwi_softc *sc)
3335 _bwi_txeof(struct bwi_softc *sc, uint16_t tx_id, int acked, int data_txcnt)
3337 struct ifnet *ifp = sc->sc_ifp;
3338 struct bwi_txbuf_data *tbd;
3339 struct bwi_txbuf *tb;
3340 int ring_idx, buf_idx;
3341 struct ieee80211_node *ni;
3342 struct ieee80211vap *vap;
3345 if_printf(ifp, "%s: zero tx id\n", __func__);
3349 ring_idx = __SHIFTOUT(tx_id, BWI_TXH_ID_RING_MASK);
3350 buf_idx = __SHIFTOUT(tx_id, BWI_TXH_ID_IDX_MASK);
3352 KASSERT(ring_idx == BWI_TX_DATA_RING, ("ring_idx %d", ring_idx));
3353 KASSERT(buf_idx < BWI_TX_NDESC, ("buf_idx %d", buf_idx));
3355 tbd = &sc->sc_tx_bdata[ring_idx];
3356 KASSERT(tbd->tbd_used > 0, ("tbd_used %d", tbd->tbd_used));
3359 tb = &tbd->tbd_buf[buf_idx];
3360 DPRINTF(sc, BWI_DBG_TXEOF, "txeof idx %d, "
3361 "acked %d, data_txcnt %d, ni %p\n",
3362 buf_idx, acked, data_txcnt, tb->tb_ni);
3364 bus_dmamap_unload(sc->sc_buf_dtag, tb->tb_dmap);
3367 if (tb->tb_ni != NULL) {
3368 const struct bwi_txbuf_hdr *hdr =
3369 mtod(tb->tb_mbuf, const struct bwi_txbuf_hdr *);
3372 /* NB: update rate control only for unicast frames */
3373 if (hdr->txh_mac_ctrl & htole32(BWI_TXH_MAC_C_ACK)) {
3375 * Feed back 'acked and data_txcnt'. Note that the
3376 * generic AMRR code only understands one tx rate
3377 * and the estimator doesn't handle real retry counts
3378 * well so to avoid over-aggressive downshifting we
3379 * treat any number of retries as "1".
3381 ieee80211_ratectl_tx_complete(vap, ni,
3382 (data_txcnt > 1) ? IEEE80211_RATECTL_TX_SUCCESS :
3383 IEEE80211_RATECTL_TX_FAILURE, &acked, NULL);
3387 * Do any tx complete callback. Note this must
3388 * be done before releasing the node reference.
3390 if (tb->tb_mbuf->m_flags & M_TXCB)
3391 ieee80211_process_callback(ni, tb->tb_mbuf, !acked);
3393 ieee80211_free_node(tb->tb_ni);
3396 m_freem(tb->tb_mbuf);
3399 if (tbd->tbd_used == 0)
3402 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3406 bwi_txeof_status(struct bwi_softc *sc, int end_idx)
3408 struct bwi_txstats_data *st = sc->sc_txstats;
3411 bus_dmamap_sync(st->stats_dtag, st->stats_dmap, BUS_DMASYNC_POSTREAD);
3413 idx = st->stats_idx;
3414 while (idx != end_idx) {
3415 const struct bwi_txstats *stats = &st->stats[idx];
3417 if ((stats->txs_flags & BWI_TXS_F_PENDING) == 0) {
3420 data_txcnt = __SHIFTOUT(stats->txs_txcnt,
3421 BWI_TXS_TXCNT_DATA);
3422 _bwi_txeof(sc, le16toh(stats->txs_id),
3423 stats->txs_flags & BWI_TXS_F_ACKED,
3426 idx = (idx + 1) % BWI_TXSTATS_NDESC;
3428 st->stats_idx = idx;
3432 bwi_txeof(struct bwi_softc *sc)
3434 struct ifnet *ifp = sc->sc_ifp;
3437 uint32_t tx_status0, tx_status1;
3441 tx_status0 = CSR_READ_4(sc, BWI_TXSTATUS0);
3442 if ((tx_status0 & BWI_TXSTATUS0_VALID) == 0)
3444 tx_status1 = CSR_READ_4(sc, BWI_TXSTATUS1);
3446 tx_id = __SHIFTOUT(tx_status0, BWI_TXSTATUS0_TXID_MASK);
3447 data_txcnt = __SHIFTOUT(tx_status0,
3448 BWI_TXSTATUS0_DATA_TXCNT_MASK);
3450 if (tx_status0 & (BWI_TXSTATUS0_AMPDU | BWI_TXSTATUS0_PENDING))
3453 _bwi_txeof(sc, le16toh(tx_id), tx_status0 & BWI_TXSTATUS0_ACKED,
3457 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
3462 bwi_bbp_power_on(struct bwi_softc *sc, enum bwi_clock_mode clk_mode)
3464 bwi_power_on(sc, 1);
3465 return bwi_set_clock_mode(sc, clk_mode);
3469 bwi_bbp_power_off(struct bwi_softc *sc)
3471 bwi_set_clock_mode(sc, BWI_CLOCK_MODE_SLOW);
3472 bwi_power_off(sc, 1);
3476 bwi_get_pwron_delay(struct bwi_softc *sc)
3478 struct bwi_regwin *com, *old;
3479 struct bwi_clock_freq freq;
3483 com = &sc->sc_com_regwin;
3484 KASSERT(BWI_REGWIN_EXIST(com), ("no regwin"));
3486 if ((sc->sc_cap & BWI_CAP_CLKMODE) == 0)
3489 error = bwi_regwin_switch(sc, com, &old);
3493 bwi_get_clock_freq(sc, &freq);
3495 val = CSR_READ_4(sc, BWI_PLL_ON_DELAY);
3496 sc->sc_pwron_delay = howmany((val + 2) * 1000000, freq.clkfreq_min);
3497 DPRINTF(sc, BWI_DBG_ATTACH, "power on delay %u\n", sc->sc_pwron_delay);
3499 return bwi_regwin_switch(sc, old, NULL);
3503 bwi_bus_attach(struct bwi_softc *sc)
3505 struct bwi_regwin *bus, *old;
3508 bus = &sc->sc_bus_regwin;
3510 error = bwi_regwin_switch(sc, bus, &old);
3514 if (!bwi_regwin_is_enabled(sc, bus))
3515 bwi_regwin_enable(sc, bus, 0);
3517 /* Disable interripts */
3518 CSR_WRITE_4(sc, BWI_INTRVEC, 0);
3520 return bwi_regwin_switch(sc, old, NULL);
3524 bwi_regwin_name(const struct bwi_regwin *rw)
3526 switch (rw->rw_type) {
3527 case BWI_REGWIN_T_COM:
3529 case BWI_REGWIN_T_BUSPCI:
3531 case BWI_REGWIN_T_MAC:
3533 case BWI_REGWIN_T_BUSPCIE:
3536 panic("unknown regwin type 0x%04x\n", rw->rw_type);
3541 bwi_regwin_disable_bits(struct bwi_softc *sc)
3545 /* XXX cache this */
3546 busrev = __SHIFTOUT(CSR_READ_4(sc, BWI_ID_LO), BWI_ID_LO_BUSREV_MASK);
3547 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT | BWI_DBG_MISC,
3548 "bus rev %u\n", busrev);
3550 if (busrev == BWI_BUSREV_0)
3551 return BWI_STATE_LO_DISABLE1;
3552 else if (busrev == BWI_BUSREV_1)
3553 return BWI_STATE_LO_DISABLE2;
3555 return (BWI_STATE_LO_DISABLE1 | BWI_STATE_LO_DISABLE2);
3559 bwi_regwin_is_enabled(struct bwi_softc *sc, struct bwi_regwin *rw)
3561 uint32_t val, disable_bits;
3563 disable_bits = bwi_regwin_disable_bits(sc);
3564 val = CSR_READ_4(sc, BWI_STATE_LO);
3566 if ((val & (BWI_STATE_LO_CLOCK |
3567 BWI_STATE_LO_RESET |
3568 disable_bits)) == BWI_STATE_LO_CLOCK) {
3569 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT, "%s is enabled\n",
3570 bwi_regwin_name(rw));
3573 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT, "%s is disabled\n",
3574 bwi_regwin_name(rw));
3580 bwi_regwin_disable(struct bwi_softc *sc, struct bwi_regwin *rw, uint32_t flags)
3582 uint32_t state_lo, disable_bits;
3585 state_lo = CSR_READ_4(sc, BWI_STATE_LO);
3588 * If current regwin is in 'reset' state, it was already disabled.
3590 if (state_lo & BWI_STATE_LO_RESET) {
3591 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT,
3592 "%s was already disabled\n", bwi_regwin_name(rw));
3596 disable_bits = bwi_regwin_disable_bits(sc);
3599 * Disable normal clock
3601 state_lo = BWI_STATE_LO_CLOCK | disable_bits;
3602 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3605 * Wait until normal clock is disabled
3608 for (i = 0; i < NRETRY; ++i) {
3609 state_lo = CSR_READ_4(sc, BWI_STATE_LO);
3610 if (state_lo & disable_bits)
3615 device_printf(sc->sc_dev, "%s disable clock timeout\n",
3616 bwi_regwin_name(rw));
3619 for (i = 0; i < NRETRY; ++i) {
3622 state_hi = CSR_READ_4(sc, BWI_STATE_HI);
3623 if ((state_hi & BWI_STATE_HI_BUSY) == 0)
3628 device_printf(sc->sc_dev, "%s wait BUSY unset timeout\n",
3629 bwi_regwin_name(rw));
3634 * Reset and disable regwin with gated clock
3636 state_lo = BWI_STATE_LO_RESET | disable_bits |
3637 BWI_STATE_LO_CLOCK | BWI_STATE_LO_GATED_CLOCK |
3638 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3639 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3641 /* Flush pending bus write */
3642 CSR_READ_4(sc, BWI_STATE_LO);
3645 /* Reset and disable regwin */
3646 state_lo = BWI_STATE_LO_RESET | disable_bits |
3647 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3648 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3650 /* Flush pending bus write */
3651 CSR_READ_4(sc, BWI_STATE_LO);
3656 bwi_regwin_enable(struct bwi_softc *sc, struct bwi_regwin *rw, uint32_t flags)
3658 uint32_t state_lo, state_hi, imstate;
3660 bwi_regwin_disable(sc, rw, flags);
3662 /* Reset regwin with gated clock */
3663 state_lo = BWI_STATE_LO_RESET |
3664 BWI_STATE_LO_CLOCK |
3665 BWI_STATE_LO_GATED_CLOCK |
3666 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3667 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3669 /* Flush pending bus write */
3670 CSR_READ_4(sc, BWI_STATE_LO);
3673 state_hi = CSR_READ_4(sc, BWI_STATE_HI);
3674 if (state_hi & BWI_STATE_HI_SERROR)
3675 CSR_WRITE_4(sc, BWI_STATE_HI, 0);
3677 imstate = CSR_READ_4(sc, BWI_IMSTATE);
3678 if (imstate & (BWI_IMSTATE_INBAND_ERR | BWI_IMSTATE_TIMEOUT)) {
3679 imstate &= ~(BWI_IMSTATE_INBAND_ERR | BWI_IMSTATE_TIMEOUT);
3680 CSR_WRITE_4(sc, BWI_IMSTATE, imstate);
3683 /* Enable regwin with gated clock */
3684 state_lo = BWI_STATE_LO_CLOCK |
3685 BWI_STATE_LO_GATED_CLOCK |
3686 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3687 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3689 /* Flush pending bus write */
3690 CSR_READ_4(sc, BWI_STATE_LO);
3693 /* Enable regwin with normal clock */
3694 state_lo = BWI_STATE_LO_CLOCK |
3695 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3696 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3698 /* Flush pending bus write */
3699 CSR_READ_4(sc, BWI_STATE_LO);
3704 bwi_set_bssid(struct bwi_softc *sc, const uint8_t *bssid)
3706 struct ifnet *ifp = sc->sc_ifp;
3707 struct bwi_mac *mac;
3708 struct bwi_myaddr_bssid buf;
3713 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3714 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3715 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3717 bwi_set_addr_filter(sc, BWI_ADDR_FILTER_BSSID, bssid);
3719 bcopy(IF_LLADDR(ifp), buf.myaddr, sizeof(buf.myaddr));
3720 bcopy(bssid, buf.bssid, sizeof(buf.bssid));
3722 n = sizeof(buf) / sizeof(val);
3723 p = (const uint8_t *)&buf;
3724 for (i = 0; i < n; ++i) {
3728 for (j = 0; j < sizeof(val); ++j)
3729 val |= ((uint32_t)(*p++)) << (j * 8);
3731 TMPLT_WRITE_4(mac, 0x20 + (i * sizeof(val)), val);
3736 bwi_updateslot(struct ifnet *ifp)
3738 struct bwi_softc *sc = ifp->if_softc;
3739 struct ieee80211com *ic = ifp->if_l2com;
3740 struct bwi_mac *mac;
3743 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3744 DPRINTF(sc, BWI_DBG_80211, "%s\n", __func__);
3746 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3747 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3748 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3750 bwi_mac_updateslot(mac, (ic->ic_flags & IEEE80211_F_SHSLOT));
3756 bwi_calibrate(void *xsc)
3758 struct bwi_softc *sc = xsc;
3760 struct ifnet *ifp = sc->sc_ifp;
3761 struct ieee80211com *ic = ifp->if_l2com;
3763 struct bwi_mac *mac;
3765 BWI_ASSERT_LOCKED(sc);
3767 KASSERT(ic->ic_opmode != IEEE80211_M_MONITOR,
3768 ("opmode %d", ic->ic_opmode));
3770 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3771 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3772 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3774 bwi_mac_calibrate_txpower(mac, sc->sc_txpwrcb_type);
3775 sc->sc_txpwrcb_type = BWI_TXPWR_CALIB;
3777 /* XXX 15 seconds */
3778 callout_reset(&sc->sc_calib_ch, hz * 15, bwi_calibrate, sc);
3782 bwi_calc_rssi(struct bwi_softc *sc, const struct bwi_rxbuf_hdr *hdr)
3784 struct bwi_mac *mac;
3786 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3787 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3788 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3790 return bwi_rf_calc_rssi(mac, hdr);
3794 bwi_calc_noise(struct bwi_softc *sc)
3796 struct bwi_mac *mac;
3798 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3799 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3800 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3802 return bwi_rf_calc_noise(mac);
3805 static __inline uint8_t
3806 bwi_ofdm_plcp2rate(const uint32_t *plcp0)
3811 plcp = le32toh(*plcp0);
3812 plcp_rate = __SHIFTOUT(plcp, IEEE80211_OFDM_PLCP_RATE_MASK);
3813 return ieee80211_plcp2rate(plcp_rate, IEEE80211_T_OFDM);
3816 static __inline uint8_t
3817 bwi_ds_plcp2rate(const struct ieee80211_ds_plcp_hdr *hdr)
3819 return ieee80211_plcp2rate(hdr->i_signal, IEEE80211_T_DS);
3823 bwi_rx_radiotap(struct bwi_softc *sc, struct mbuf *m,
3824 struct bwi_rxbuf_hdr *hdr, const void *plcp, int rate, int rssi, int noise)
3826 const struct ieee80211_frame_min *wh;
3828 sc->sc_rx_th.wr_flags = IEEE80211_RADIOTAP_F_FCS;
3829 if (htole16(hdr->rxh_flags1) & BWI_RXH_F1_SHPREAMBLE)
3830 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3832 wh = mtod(m, const struct ieee80211_frame_min *);
3833 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3834 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_WEP;
3836 sc->sc_rx_th.wr_tsf = hdr->rxh_tsf; /* No endian convertion */
3837 sc->sc_rx_th.wr_rate = rate;
3838 sc->sc_rx_th.wr_antsignal = rssi;
3839 sc->sc_rx_th.wr_antnoise = noise;
3843 bwi_led_attach(struct bwi_softc *sc)
3845 const uint8_t *led_act = NULL;
3846 uint16_t gpio, val[BWI_LED_MAX];
3849 #define N(arr) (int)(sizeof(arr) / sizeof(arr[0]))
3851 for (i = 0; i < N(bwi_vendor_led_act); ++i) {
3852 if (sc->sc_pci_subvid == bwi_vendor_led_act[i].vid) {
3853 led_act = bwi_vendor_led_act[i].led_act;
3857 if (led_act == NULL)
3858 led_act = bwi_default_led_act;
3862 gpio = bwi_read_sprom(sc, BWI_SPROM_GPIO01);
3863 val[0] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_0);
3864 val[1] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_1);
3866 gpio = bwi_read_sprom(sc, BWI_SPROM_GPIO23);
3867 val[2] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_2);
3868 val[3] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_3);
3870 for (i = 0; i < BWI_LED_MAX; ++i) {
3871 struct bwi_led *led = &sc->sc_leds[i];
3873 if (val[i] == 0xff) {
3874 led->l_act = led_act[i];
3876 if (val[i] & BWI_LED_ACT_LOW)
3877 led->l_flags |= BWI_LED_F_ACTLOW;
3878 led->l_act = __SHIFTOUT(val[i], BWI_LED_ACT_MASK);
3880 led->l_mask = (1 << i);
3882 if (led->l_act == BWI_LED_ACT_BLINK_SLOW ||
3883 led->l_act == BWI_LED_ACT_BLINK_POLL ||
3884 led->l_act == BWI_LED_ACT_BLINK) {
3885 led->l_flags |= BWI_LED_F_BLINK;
3886 if (led->l_act == BWI_LED_ACT_BLINK_POLL)
3887 led->l_flags |= BWI_LED_F_POLLABLE;
3888 else if (led->l_act == BWI_LED_ACT_BLINK_SLOW)
3889 led->l_flags |= BWI_LED_F_SLOW;
3891 if (sc->sc_blink_led == NULL) {
3892 sc->sc_blink_led = led;
3893 if (led->l_flags & BWI_LED_F_SLOW)
3894 BWI_LED_SLOWDOWN(sc->sc_led_idle);
3898 DPRINTF(sc, BWI_DBG_LED | BWI_DBG_ATTACH,
3899 "%dth led, act %d, lowact %d\n", i,
3900 led->l_act, led->l_flags & BWI_LED_F_ACTLOW);
3902 callout_init(&sc->sc_led_blink_ch, CALLOUT_MPSAFE);
3905 static __inline uint16_t
3906 bwi_led_onoff(const struct bwi_led *led, uint16_t val, int on)
3908 if (led->l_flags & BWI_LED_F_ACTLOW)
3913 val &= ~led->l_mask;
3918 bwi_led_newstate(struct bwi_softc *sc, enum ieee80211_state nstate)
3920 struct ifnet *ifp = sc->sc_ifp;
3921 struct ieee80211com *ic = ifp->if_l2com;
3925 if (nstate == IEEE80211_S_INIT) {
3926 callout_stop(&sc->sc_led_blink_ch);
3927 sc->sc_led_blinking = 0;
3930 if ((ic->ic_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
3933 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL);
3934 for (i = 0; i < BWI_LED_MAX; ++i) {
3935 struct bwi_led *led = &sc->sc_leds[i];
3938 if (led->l_act == BWI_LED_ACT_UNKN ||
3939 led->l_act == BWI_LED_ACT_NULL)
3942 if ((led->l_flags & BWI_LED_F_BLINK) &&
3943 nstate != IEEE80211_S_INIT)
3946 switch (led->l_act) {
3947 case BWI_LED_ACT_ON: /* Always on */
3950 case BWI_LED_ACT_OFF: /* Always off */
3951 case BWI_LED_ACT_5GHZ: /* TODO: 11A */
3957 case IEEE80211_S_INIT:
3960 case IEEE80211_S_RUN:
3961 if (led->l_act == BWI_LED_ACT_11G &&
3962 ic->ic_curmode != IEEE80211_MODE_11G)
3966 if (led->l_act == BWI_LED_ACT_ASSOC)
3973 val = bwi_led_onoff(led, val, on);
3975 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val);
3978 bwi_led_event(struct bwi_softc *sc, int event)
3980 struct bwi_led *led = sc->sc_blink_led;
3983 if (event == BWI_LED_EVENT_POLL) {
3984 if ((led->l_flags & BWI_LED_F_POLLABLE) == 0)
3986 if (ticks - sc->sc_led_ticks < sc->sc_led_idle)
3990 sc->sc_led_ticks = ticks;
3991 if (sc->sc_led_blinking)
3995 case BWI_LED_EVENT_RX:
3996 rate = sc->sc_rx_rate;
3998 case BWI_LED_EVENT_TX:
3999 rate = sc->sc_tx_rate;
4001 case BWI_LED_EVENT_POLL:
4005 panic("unknown LED event %d\n", event);
4008 bwi_led_blink_start(sc, bwi_led_duration[rate].on_dur,
4009 bwi_led_duration[rate].off_dur);
4013 bwi_led_blink_start(struct bwi_softc *sc, int on_dur, int off_dur)
4015 struct bwi_led *led = sc->sc_blink_led;
4018 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL);
4019 val = bwi_led_onoff(led, val, 1);
4020 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val);
4022 if (led->l_flags & BWI_LED_F_SLOW) {
4023 BWI_LED_SLOWDOWN(on_dur);
4024 BWI_LED_SLOWDOWN(off_dur);
4027 sc->sc_led_blinking = 1;
4028 sc->sc_led_blink_offdur = off_dur;
4030 callout_reset(&sc->sc_led_blink_ch, on_dur, bwi_led_blink_next, sc);
4034 bwi_led_blink_next(void *xsc)
4036 struct bwi_softc *sc = xsc;
4039 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL);
4040 val = bwi_led_onoff(sc->sc_blink_led, val, 0);
4041 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val);
4043 callout_reset(&sc->sc_led_blink_ch, sc->sc_led_blink_offdur,
4044 bwi_led_blink_end, sc);
4048 bwi_led_blink_end(void *xsc)
4050 struct bwi_softc *sc = xsc;
4051 sc->sc_led_blinking = 0;
4055 bwi_restart(void *xsc, int pending)
4057 struct bwi_softc *sc = xsc;
4058 struct ifnet *ifp = sc->sc_ifp;
4060 if_printf(ifp, "%s begin, help!\n", __func__);
4062 bwi_init_statechg(xsc, 0);
4064 bwi_start_locked(ifp);