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_amrr.h>
68 #include <net80211/ieee80211_phy.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 struct ieee80211_node *bwi_node_alloc(struct ieee80211vap *,
116 const uint8_t [IEEE80211_ADDR_LEN]);
117 static void bwi_newassoc(struct ieee80211_node *, int);
118 static int bwi_media_change(struct ifnet *);
120 static void bwi_calibrate(void *);
122 static int bwi_calc_rssi(struct bwi_softc *, const struct bwi_rxbuf_hdr *);
123 static int bwi_calc_noise(struct bwi_softc *);
124 static __inline uint8_t bwi_ofdm_plcp2rate(const uint32_t *);
125 static __inline uint8_t bwi_ds_plcp2rate(const struct ieee80211_ds_plcp_hdr *);
126 static void bwi_rx_radiotap(struct bwi_softc *, struct mbuf *,
127 struct bwi_rxbuf_hdr *, const void *, int, int, int);
129 static void bwi_restart(void *, int);
130 static void bwi_init_statechg(struct bwi_softc *, int);
131 static void bwi_stop(struct bwi_softc *, int);
132 static void bwi_stop_locked(struct bwi_softc *, int);
133 static int bwi_newbuf(struct bwi_softc *, int, int);
134 static int bwi_encap(struct bwi_softc *, int, struct mbuf *,
135 struct ieee80211_node *);
136 static int bwi_encap_raw(struct bwi_softc *, int, struct mbuf *,
137 struct ieee80211_node *,
138 const struct ieee80211_bpf_params *);
140 static void bwi_init_rxdesc_ring32(struct bwi_softc *, uint32_t,
141 bus_addr_t, int, int);
142 static void bwi_reset_rx_ring32(struct bwi_softc *, uint32_t);
144 static int bwi_init_tx_ring32(struct bwi_softc *, int);
145 static int bwi_init_rx_ring32(struct bwi_softc *);
146 static int bwi_init_txstats32(struct bwi_softc *);
147 static void bwi_free_tx_ring32(struct bwi_softc *, int);
148 static void bwi_free_rx_ring32(struct bwi_softc *);
149 static void bwi_free_txstats32(struct bwi_softc *);
150 static void bwi_setup_rx_desc32(struct bwi_softc *, int, bus_addr_t, int);
151 static void bwi_setup_tx_desc32(struct bwi_softc *, struct bwi_ring_data *,
152 int, bus_addr_t, int);
153 static int bwi_rxeof32(struct bwi_softc *);
154 static void bwi_start_tx32(struct bwi_softc *, uint32_t, int);
155 static void bwi_txeof_status32(struct bwi_softc *);
157 static int bwi_init_tx_ring64(struct bwi_softc *, int);
158 static int bwi_init_rx_ring64(struct bwi_softc *);
159 static int bwi_init_txstats64(struct bwi_softc *);
160 static void bwi_free_tx_ring64(struct bwi_softc *, int);
161 static void bwi_free_rx_ring64(struct bwi_softc *);
162 static void bwi_free_txstats64(struct bwi_softc *);
163 static void bwi_setup_rx_desc64(struct bwi_softc *, int, bus_addr_t, int);
164 static void bwi_setup_tx_desc64(struct bwi_softc *, struct bwi_ring_data *,
165 int, bus_addr_t, int);
166 static int bwi_rxeof64(struct bwi_softc *);
167 static void bwi_start_tx64(struct bwi_softc *, uint32_t, int);
168 static void bwi_txeof_status64(struct bwi_softc *);
170 static int bwi_rxeof(struct bwi_softc *, int);
171 static void _bwi_txeof(struct bwi_softc *, uint16_t, int, int);
172 static void bwi_txeof(struct bwi_softc *);
173 static void bwi_txeof_status(struct bwi_softc *, int);
174 static void bwi_enable_intrs(struct bwi_softc *, uint32_t);
175 static void bwi_disable_intrs(struct bwi_softc *, uint32_t);
177 static int bwi_dma_alloc(struct bwi_softc *);
178 static void bwi_dma_free(struct bwi_softc *);
179 static int bwi_dma_ring_alloc(struct bwi_softc *, bus_dma_tag_t,
180 struct bwi_ring_data *, bus_size_t,
182 static int bwi_dma_mbuf_create(struct bwi_softc *);
183 static void bwi_dma_mbuf_destroy(struct bwi_softc *, int, int);
184 static int bwi_dma_txstats_alloc(struct bwi_softc *, uint32_t, bus_size_t);
185 static void bwi_dma_txstats_free(struct bwi_softc *);
186 static void bwi_dma_ring_addr(void *, bus_dma_segment_t *, int, int);
187 static void bwi_dma_buf_addr(void *, bus_dma_segment_t *, int,
190 static void bwi_power_on(struct bwi_softc *, int);
191 static int bwi_power_off(struct bwi_softc *, int);
192 static int bwi_set_clock_mode(struct bwi_softc *, enum bwi_clock_mode);
193 static int bwi_set_clock_delay(struct bwi_softc *);
194 static void bwi_get_clock_freq(struct bwi_softc *, struct bwi_clock_freq *);
195 static int bwi_get_pwron_delay(struct bwi_softc *sc);
196 static void bwi_set_addr_filter(struct bwi_softc *, uint16_t,
198 static void bwi_set_bssid(struct bwi_softc *, const uint8_t *);
200 static void bwi_get_card_flags(struct bwi_softc *);
201 static void bwi_get_eaddr(struct bwi_softc *, uint16_t, uint8_t *);
203 static int bwi_bus_attach(struct bwi_softc *);
204 static int bwi_bbp_attach(struct bwi_softc *);
205 static int bwi_bbp_power_on(struct bwi_softc *, enum bwi_clock_mode);
206 static void bwi_bbp_power_off(struct bwi_softc *);
208 static const char *bwi_regwin_name(const struct bwi_regwin *);
209 static uint32_t bwi_regwin_disable_bits(struct bwi_softc *);
210 static void bwi_regwin_info(struct bwi_softc *, uint16_t *, uint8_t *);
211 static int bwi_regwin_select(struct bwi_softc *, int);
213 static void bwi_led_attach(struct bwi_softc *);
214 static void bwi_led_newstate(struct bwi_softc *, enum ieee80211_state);
215 static void bwi_led_event(struct bwi_softc *, int);
216 static void bwi_led_blink_start(struct bwi_softc *, int, int);
217 static void bwi_led_blink_next(void *);
218 static void bwi_led_blink_end(void *);
220 static const struct {
224 } bwi_bbpid_map[] = {
225 { 0x4301, 0x4301, 0x4301 },
226 { 0x4305, 0x4307, 0x4307 },
227 { 0x4403, 0x4403, 0x4402 },
228 { 0x4610, 0x4615, 0x4610 },
229 { 0x4710, 0x4715, 0x4710 },
230 { 0x4720, 0x4725, 0x4309 }
233 static const struct {
236 } bwi_regwin_count[] = {
249 #define CLKSRC(src) \
250 [BWI_CLKSRC_ ## src] = { \
251 .freq_min = BWI_CLKSRC_ ##src## _FMIN, \
252 .freq_max = BWI_CLKSRC_ ##src## _FMAX \
255 static const struct {
258 } bwi_clkfreq[BWI_CLKSRC_MAX] = {
266 #define VENDOR_LED_ACT(vendor) \
268 .vid = PCI_VENDOR_##vendor, \
269 .led_act = { BWI_VENDOR_LED_ACT_##vendor } \
272 static const struct {
273 #define PCI_VENDOR_COMPAQ 0x0e11
274 #define PCI_VENDOR_LINKSYS 0x1737
276 uint8_t led_act[BWI_LED_MAX];
277 } bwi_vendor_led_act[] = {
278 VENDOR_LED_ACT(COMPAQ),
279 VENDOR_LED_ACT(LINKSYS)
280 #undef PCI_VENDOR_LINKSYS
281 #undef PCI_VENDOR_COMPAQ
284 static const uint8_t bwi_default_led_act[BWI_LED_MAX] =
285 { BWI_VENDOR_LED_ACT_DEFAULT };
287 #undef VENDOR_LED_ACT
289 static const struct {
292 } bwi_led_duration[109] = {
309 #ifdef BWI_DEBUG_VERBOSE
310 static uint32_t bwi_debug = BWI_DBG_ATTACH | BWI_DBG_INIT | BWI_DBG_TXPOWER;
312 static uint32_t bwi_debug;
314 TUNABLE_INT("hw.bwi.debug", (int *)&bwi_debug);
315 #endif /* BWI_DEBUG */
317 static const uint8_t bwi_zero_addr[IEEE80211_ADDR_LEN];
320 bwi_read_sprom(struct bwi_softc *sc, uint16_t ofs)
322 return CSR_READ_2(sc, ofs + BWI_SPROM_START);
326 bwi_setup_desc32(struct bwi_softc *sc, struct bwi_desc32 *desc_array,
327 int ndesc, int desc_idx, bus_addr_t paddr, int buf_len,
330 struct bwi_desc32 *desc = &desc_array[desc_idx];
331 uint32_t ctrl, addr, addr_hi, addr_lo;
333 addr_lo = __SHIFTOUT(paddr, BWI_DESC32_A_ADDR_MASK);
334 addr_hi = __SHIFTOUT(paddr, BWI_DESC32_A_FUNC_MASK);
336 addr = __SHIFTIN(addr_lo, BWI_DESC32_A_ADDR_MASK) |
337 __SHIFTIN(BWI_DESC32_A_FUNC_TXRX, BWI_DESC32_A_FUNC_MASK);
339 ctrl = __SHIFTIN(buf_len, BWI_DESC32_C_BUFLEN_MASK) |
340 __SHIFTIN(addr_hi, BWI_DESC32_C_ADDRHI_MASK);
341 if (desc_idx == ndesc - 1)
342 ctrl |= BWI_DESC32_C_EOR;
345 ctrl |= BWI_DESC32_C_FRAME_START |
346 BWI_DESC32_C_FRAME_END |
350 desc->addr = htole32(addr);
351 desc->ctrl = htole32(ctrl);
355 bwi_attach(struct bwi_softc *sc)
357 struct ieee80211com *ic;
358 device_t dev = sc->sc_dev;
364 uint8_t macaddr[IEEE80211_ADDR_LEN];
369 * Initialize taskq and various tasks
371 sc->sc_tq = taskqueue_create("bwi_taskq", M_NOWAIT | M_ZERO,
372 taskqueue_thread_enqueue, &sc->sc_tq);
373 taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq",
374 device_get_nameunit(dev));
375 TASK_INIT(&sc->sc_restart_task, 0, bwi_restart, sc);
377 callout_init_mtx(&sc->sc_calib_ch, &sc->sc_mtx, 0);
380 * Initialize sysctl variables
382 sc->sc_fw_version = BWI_FW_VERSION3;
383 sc->sc_led_idle = (2350 * hz) / 1000;
384 sc->sc_led_blink = 1;
385 sc->sc_txpwr_calib = 1;
387 sc->sc_debug = bwi_debug;
391 error = bwi_bbp_attach(sc);
395 error = bwi_bbp_power_on(sc, BWI_CLOCK_MODE_FAST);
399 if (BWI_REGWIN_EXIST(&sc->sc_com_regwin)) {
400 error = bwi_set_clock_delay(sc);
404 error = bwi_set_clock_mode(sc, BWI_CLOCK_MODE_FAST);
408 error = bwi_get_pwron_delay(sc);
413 error = bwi_bus_attach(sc);
417 bwi_get_card_flags(sc);
421 for (i = 0; i < sc->sc_nmac; ++i) {
422 struct bwi_regwin *old;
424 mac = &sc->sc_mac[i];
425 error = bwi_regwin_switch(sc, &mac->mac_regwin, &old);
429 error = bwi_mac_lateattach(mac);
433 error = bwi_regwin_switch(sc, old, NULL);
439 * XXX First MAC is known to exist
442 mac = &sc->sc_mac[0];
445 bwi_bbp_power_off(sc);
447 error = bwi_dma_alloc(sc);
451 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
453 device_printf(dev, "can not if_alloc()\n");
459 /* set these up early for if_printf use */
460 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
463 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
464 ifp->if_init = bwi_init;
465 ifp->if_ioctl = bwi_ioctl;
466 ifp->if_start = bwi_start;
467 ifp->if_watchdog = bwi_watchdog;
468 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
469 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
470 IFQ_SET_READY(&ifp->if_snd);
473 * Setup ratesets, phytype, channels and get MAC address
476 if (phy->phy_mode == IEEE80211_MODE_11B ||
477 phy->phy_mode == IEEE80211_MODE_11G) {
478 setbit(&bands, IEEE80211_MODE_11B);
479 if (phy->phy_mode == IEEE80211_MODE_11B) {
480 ic->ic_phytype = IEEE80211_T_DS;
482 ic->ic_phytype = IEEE80211_T_OFDM;
483 setbit(&bands, IEEE80211_MODE_11G);
486 bwi_get_eaddr(sc, BWI_SPROM_11BG_EADDR, macaddr);
487 if (IEEE80211_IS_MULTICAST(macaddr)) {
488 bwi_get_eaddr(sc, BWI_SPROM_11A_EADDR, macaddr);
489 if (IEEE80211_IS_MULTICAST(macaddr)) {
491 "invalid MAC address: %6D\n",
495 } else if (phy->phy_mode == IEEE80211_MODE_11A) {
497 setbit(&bands, IEEE80211_MODE_11A);
501 panic("unknown phymode %d\n", phy->phy_mode);
505 sc->sc_locale = __SHIFTOUT(bwi_read_sprom(sc, BWI_SPROM_CARD_INFO),
506 BWI_SPROM_CARD_INFO_LOCALE);
507 DPRINTF(sc, BWI_DBG_ATTACH, "locale: %d\n", sc->sc_locale);
509 ieee80211_init_channels(ic, NULL, &bands);
512 ic->ic_caps = IEEE80211_C_STA |
514 IEEE80211_C_SHPREAMBLE |
518 ic->ic_opmode = IEEE80211_M_STA;
519 ieee80211_ifattach(ic, macaddr);
521 ic->ic_headroom = sizeof(struct bwi_txbuf_hdr);
523 /* override default methods */
524 ic->ic_vap_create = bwi_vap_create;
525 ic->ic_vap_delete = bwi_vap_delete;
526 ic->ic_raw_xmit = bwi_raw_xmit;
527 ic->ic_updateslot = bwi_updateslot;
528 ic->ic_node_alloc = bwi_node_alloc;
529 ic->ic_scan_start = bwi_scan_start;
530 ic->ic_scan_end = bwi_scan_end;
531 ic->ic_set_channel = bwi_set_channel;
533 sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan);
535 ieee80211_radiotap_attach(ic,
536 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
537 BWI_TX_RADIOTAP_PRESENT,
538 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
539 BWI_RX_RADIOTAP_PRESENT);
544 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
545 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
546 "fw_version", CTLFLAG_RD, &sc->sc_fw_version, 0,
548 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
549 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
550 "led_idle", CTLFLAG_RW, &sc->sc_led_idle, 0,
551 "# ticks before LED enters idle state");
552 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
553 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
554 "led_blink", CTLFLAG_RW, &sc->sc_led_blink, 0,
555 "Allow LED to blink");
556 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
557 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
558 "txpwr_calib", CTLFLAG_RW, &sc->sc_txpwr_calib, 0,
559 "Enable software TX power calibration");
561 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
562 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
563 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "Debug flags");
566 ieee80211_announce(ic);
570 BWI_LOCK_DESTROY(sc);
575 bwi_detach(struct bwi_softc *sc)
577 struct ifnet *ifp = sc->sc_ifp;
578 struct ieee80211com *ic = ifp->if_l2com;
582 callout_drain(&sc->sc_led_blink_ch);
583 callout_drain(&sc->sc_calib_ch);
584 ieee80211_ifdetach(ic);
586 for (i = 0; i < sc->sc_nmac; ++i)
587 bwi_mac_detach(&sc->sc_mac[i]);
590 taskqueue_free(sc->sc_tq);
592 BWI_LOCK_DESTROY(sc);
597 static struct ieee80211vap *
598 bwi_vap_create(struct ieee80211com *ic,
599 const char name[IFNAMSIZ], int unit, int opmode, int flags,
600 const uint8_t bssid[IEEE80211_ADDR_LEN],
601 const uint8_t mac[IEEE80211_ADDR_LEN])
604 struct ieee80211vap *vap;
606 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
608 bvp = (struct bwi_vap *) malloc(sizeof(struct bwi_vap),
609 M_80211_VAP, M_WAITOK | M_ZERO);
613 /* enable s/w bmiss handling for sta mode */
614 ieee80211_vap_setup(ic, vap, name, unit, opmode,
615 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
617 /* override default methods */
618 bvp->bv_newstate = vap->iv_newstate;
619 vap->iv_newstate = bwi_newstate;
621 vap->iv_update_beacon = bwi_beacon_update;
623 ieee80211_amrr_init(&bvp->bv_amrr, vap,
624 IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
625 IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD,
629 ieee80211_vap_attach(vap, bwi_media_change, ieee80211_media_status);
630 ic->ic_opmode = opmode;
635 bwi_vap_delete(struct ieee80211vap *vap)
637 struct bwi_vap *bvp = BWI_VAP(vap);
639 ieee80211_amrr_cleanup(&bvp->bv_amrr);
640 ieee80211_vap_detach(vap);
641 free(bvp, M_80211_VAP);
645 bwi_suspend(struct bwi_softc *sc)
651 bwi_resume(struct bwi_softc *sc)
653 struct ifnet *ifp = sc->sc_ifp;
655 if (ifp->if_flags & IFF_UP)
660 bwi_shutdown(struct bwi_softc *sc)
667 bwi_power_on(struct bwi_softc *sc, int with_pll)
669 uint32_t gpio_in, gpio_out, gpio_en;
672 gpio_in = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_IN, 4);
673 if (gpio_in & BWI_PCIM_GPIO_PWR_ON)
676 gpio_out = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4);
677 gpio_en = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, 4);
679 gpio_out |= BWI_PCIM_GPIO_PWR_ON;
680 gpio_en |= BWI_PCIM_GPIO_PWR_ON;
682 /* Turn off PLL first */
683 gpio_out |= BWI_PCIM_GPIO_PLL_PWR_OFF;
684 gpio_en |= BWI_PCIM_GPIO_PLL_PWR_OFF;
687 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4);
688 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, gpio_en, 4);
693 gpio_out &= ~BWI_PCIM_GPIO_PLL_PWR_OFF;
694 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4);
699 /* Clear "Signaled Target Abort" */
700 status = pci_read_config(sc->sc_dev, PCIR_STATUS, 2);
701 status &= ~PCIM_STATUS_STABORT;
702 pci_write_config(sc->sc_dev, PCIR_STATUS, status, 2);
706 bwi_power_off(struct bwi_softc *sc, int with_pll)
708 uint32_t gpio_out, gpio_en;
710 pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_IN, 4); /* dummy read */
711 gpio_out = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4);
712 gpio_en = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, 4);
714 gpio_out &= ~BWI_PCIM_GPIO_PWR_ON;
715 gpio_en |= BWI_PCIM_GPIO_PWR_ON;
717 gpio_out |= BWI_PCIM_GPIO_PLL_PWR_OFF;
718 gpio_en |= BWI_PCIM_GPIO_PLL_PWR_OFF;
721 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4);
722 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, gpio_en, 4);
727 bwi_regwin_switch(struct bwi_softc *sc, struct bwi_regwin *rw,
728 struct bwi_regwin **old_rw)
735 if (!BWI_REGWIN_EXIST(rw))
738 if (sc->sc_cur_regwin != rw) {
739 error = bwi_regwin_select(sc, rw->rw_id);
741 device_printf(sc->sc_dev, "can't select regwin %d\n",
748 *old_rw = sc->sc_cur_regwin;
749 sc->sc_cur_regwin = rw;
754 bwi_regwin_select(struct bwi_softc *sc, int id)
756 uint32_t win = BWI_PCIM_REGWIN(id);
760 for (i = 0; i < RETRY_MAX; ++i) {
761 pci_write_config(sc->sc_dev, BWI_PCIR_SEL_REGWIN, win, 4);
762 if (pci_read_config(sc->sc_dev, BWI_PCIR_SEL_REGWIN, 4) == win)
772 bwi_regwin_info(struct bwi_softc *sc, uint16_t *type, uint8_t *rev)
776 val = CSR_READ_4(sc, BWI_ID_HI);
777 *type = BWI_ID_HI_REGWIN_TYPE(val);
778 *rev = BWI_ID_HI_REGWIN_REV(val);
780 DPRINTF(sc, BWI_DBG_ATTACH, "regwin: type 0x%03x, rev %d, "
781 "vendor 0x%04x\n", *type, *rev,
782 __SHIFTOUT(val, BWI_ID_HI_REGWIN_VENDOR_MASK));
786 bwi_bbp_attach(struct bwi_softc *sc)
788 #define N(arr) (int)(sizeof(arr) / sizeof(arr[0]))
789 uint16_t bbp_id, rw_type;
792 int error, nregwin, i;
795 * Get 0th regwin information
796 * NOTE: 0th regwin should exist
798 error = bwi_regwin_select(sc, 0);
800 device_printf(sc->sc_dev, "can't select regwin 0\n");
803 bwi_regwin_info(sc, &rw_type, &rw_rev);
810 if (rw_type == BWI_REGWIN_T_COM) {
811 info = CSR_READ_4(sc, BWI_INFO);
812 bbp_id = __SHIFTOUT(info, BWI_INFO_BBPID_MASK);
814 BWI_CREATE_REGWIN(&sc->sc_com_regwin, 0, rw_type, rw_rev);
816 sc->sc_cap = CSR_READ_4(sc, BWI_CAPABILITY);
818 for (i = 0; i < N(bwi_bbpid_map); ++i) {
819 if (sc->sc_pci_did >= bwi_bbpid_map[i].did_min &&
820 sc->sc_pci_did <= bwi_bbpid_map[i].did_max) {
821 bbp_id = bwi_bbpid_map[i].bbp_id;
826 device_printf(sc->sc_dev, "no BBP id for device id "
827 "0x%04x\n", sc->sc_pci_did);
831 info = __SHIFTIN(sc->sc_pci_revid, BWI_INFO_BBPREV_MASK) |
832 __SHIFTIN(0, BWI_INFO_BBPPKG_MASK);
836 * Find out number of regwins
839 if (rw_type == BWI_REGWIN_T_COM && rw_rev >= 4) {
840 nregwin = __SHIFTOUT(info, BWI_INFO_NREGWIN_MASK);
842 for (i = 0; i < N(bwi_regwin_count); ++i) {
843 if (bwi_regwin_count[i].bbp_id == bbp_id) {
844 nregwin = bwi_regwin_count[i].nregwin;
849 device_printf(sc->sc_dev, "no number of win for "
850 "BBP id 0x%04x\n", bbp_id);
855 /* Record BBP id/rev for later using */
856 sc->sc_bbp_id = bbp_id;
857 sc->sc_bbp_rev = __SHIFTOUT(info, BWI_INFO_BBPREV_MASK);
858 sc->sc_bbp_pkg = __SHIFTOUT(info, BWI_INFO_BBPPKG_MASK);
859 device_printf(sc->sc_dev, "BBP: id 0x%04x, rev 0x%x, pkg %d\n",
860 sc->sc_bbp_id, sc->sc_bbp_rev, sc->sc_bbp_pkg);
862 DPRINTF(sc, BWI_DBG_ATTACH, "nregwin %d, cap 0x%08x\n",
863 nregwin, sc->sc_cap);
866 * Create rest of the regwins
869 /* Don't re-create common regwin, if it is already created */
870 i = BWI_REGWIN_EXIST(&sc->sc_com_regwin) ? 1 : 0;
872 for (; i < nregwin; ++i) {
874 * Get regwin information
876 error = bwi_regwin_select(sc, i);
878 device_printf(sc->sc_dev,
879 "can't select regwin %d\n", i);
882 bwi_regwin_info(sc, &rw_type, &rw_rev);
886 * 1) Bus (PCI/PCIE) regwin
888 * Ignore rest types of regwin
890 if (rw_type == BWI_REGWIN_T_BUSPCI ||
891 rw_type == BWI_REGWIN_T_BUSPCIE) {
892 if (BWI_REGWIN_EXIST(&sc->sc_bus_regwin)) {
893 device_printf(sc->sc_dev,
894 "bus regwin already exists\n");
896 BWI_CREATE_REGWIN(&sc->sc_bus_regwin, i,
899 } else if (rw_type == BWI_REGWIN_T_MAC) {
900 /* XXX ignore return value */
901 bwi_mac_attach(sc, i, rw_rev);
905 /* At least one MAC shold exist */
906 if (!BWI_REGWIN_EXIST(&sc->sc_mac[0].mac_regwin)) {
907 device_printf(sc->sc_dev, "no MAC was found\n");
910 KASSERT(sc->sc_nmac > 0, ("no mac's"));
912 /* Bus regwin must exist */
913 if (!BWI_REGWIN_EXIST(&sc->sc_bus_regwin)) {
914 device_printf(sc->sc_dev, "no bus regwin was found\n");
918 /* Start with first MAC */
919 error = bwi_regwin_switch(sc, &sc->sc_mac[0].mac_regwin, NULL);
928 bwi_bus_init(struct bwi_softc *sc, struct bwi_mac *mac)
930 struct bwi_regwin *old, *bus;
934 bus = &sc->sc_bus_regwin;
935 KASSERT(sc->sc_cur_regwin == &mac->mac_regwin, ("not cur regwin"));
938 * Tell bus to generate requested interrupts
940 if (bus->rw_rev < 6 && bus->rw_type == BWI_REGWIN_T_BUSPCI) {
942 * NOTE: Read BWI_FLAGS from MAC regwin
944 val = CSR_READ_4(sc, BWI_FLAGS);
946 error = bwi_regwin_switch(sc, bus, &old);
950 CSR_SETBITS_4(sc, BWI_INTRVEC, (val & BWI_FLAGS_INTR_MASK));
954 mac_mask = 1 << mac->mac_id;
956 error = bwi_regwin_switch(sc, bus, &old);
960 val = pci_read_config(sc->sc_dev, BWI_PCIR_INTCTL, 4);
961 val |= mac_mask << 8;
962 pci_write_config(sc->sc_dev, BWI_PCIR_INTCTL, val, 4);
965 if (sc->sc_flags & BWI_F_BUS_INITED)
968 if (bus->rw_type == BWI_REGWIN_T_BUSPCI) {
970 * Enable prefetch and burst
972 CSR_SETBITS_4(sc, BWI_BUS_CONFIG,
973 BWI_BUS_CONFIG_PREFETCH | BWI_BUS_CONFIG_BURST);
975 if (bus->rw_rev < 5) {
976 struct bwi_regwin *com = &sc->sc_com_regwin;
979 * Configure timeouts for bus operation
983 * Set service timeout and request timeout
985 CSR_SETBITS_4(sc, BWI_CONF_LO,
986 __SHIFTIN(BWI_CONF_LO_SERVTO, BWI_CONF_LO_SERVTO_MASK) |
987 __SHIFTIN(BWI_CONF_LO_REQTO, BWI_CONF_LO_REQTO_MASK));
990 * If there is common regwin, we switch to that regwin
991 * and switch back to bus regwin once we have done.
993 if (BWI_REGWIN_EXIST(com)) {
994 error = bwi_regwin_switch(sc, com, NULL);
999 /* Let bus know what we have changed */
1000 CSR_WRITE_4(sc, BWI_BUS_ADDR, BWI_BUS_ADDR_MAGIC);
1001 CSR_READ_4(sc, BWI_BUS_ADDR); /* Flush */
1002 CSR_WRITE_4(sc, BWI_BUS_DATA, 0);
1003 CSR_READ_4(sc, BWI_BUS_DATA); /* Flush */
1005 if (BWI_REGWIN_EXIST(com)) {
1006 error = bwi_regwin_switch(sc, bus, NULL);
1010 } else if (bus->rw_rev >= 11) {
1012 * Enable memory read multiple
1014 CSR_SETBITS_4(sc, BWI_BUS_CONFIG, BWI_BUS_CONFIG_MRM);
1020 sc->sc_flags |= BWI_F_BUS_INITED;
1022 return bwi_regwin_switch(sc, old, NULL);
1026 bwi_get_card_flags(struct bwi_softc *sc)
1028 #define PCI_VENDOR_APPLE 0x106b
1029 #define PCI_VENDOR_DELL 0x1028
1030 sc->sc_card_flags = bwi_read_sprom(sc, BWI_SPROM_CARD_FLAGS);
1031 if (sc->sc_card_flags == 0xffff)
1032 sc->sc_card_flags = 0;
1034 if (sc->sc_pci_subvid == PCI_VENDOR_DELL &&
1035 sc->sc_bbp_id == BWI_BBPID_BCM4301 &&
1036 sc->sc_pci_revid == 0x74)
1037 sc->sc_card_flags |= BWI_CARD_F_BT_COEXIST;
1039 if (sc->sc_pci_subvid == PCI_VENDOR_APPLE &&
1040 sc->sc_pci_subdid == 0x4e && /* XXX */
1041 sc->sc_pci_revid > 0x40)
1042 sc->sc_card_flags |= BWI_CARD_F_PA_GPIO9;
1044 DPRINTF(sc, BWI_DBG_ATTACH, "card flags 0x%04x\n", sc->sc_card_flags);
1045 #undef PCI_VENDOR_DELL
1046 #undef PCI_VENDOR_APPLE
1050 bwi_get_eaddr(struct bwi_softc *sc, uint16_t eaddr_ofs, uint8_t *eaddr)
1054 for (i = 0; i < 3; ++i) {
1055 *((uint16_t *)eaddr + i) =
1056 htobe16(bwi_read_sprom(sc, eaddr_ofs + 2 * i));
1061 bwi_get_clock_freq(struct bwi_softc *sc, struct bwi_clock_freq *freq)
1063 struct bwi_regwin *com;
1068 bzero(freq, sizeof(*freq));
1069 com = &sc->sc_com_regwin;
1071 KASSERT(BWI_REGWIN_EXIST(com), ("regwin does not exist"));
1072 KASSERT(sc->sc_cur_regwin == com, ("wrong regwin"));
1073 KASSERT(sc->sc_cap & BWI_CAP_CLKMODE, ("wrong clock mode"));
1076 * Calculate clock frequency
1080 if (com->rw_rev < 6) {
1081 val = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4);
1082 if (val & BWI_PCIM_GPIO_OUT_CLKSRC) {
1083 src = BWI_CLKSRC_PCI;
1086 src = BWI_CLKSRC_CS_OSC;
1089 } else if (com->rw_rev < 10) {
1090 val = CSR_READ_4(sc, BWI_CLOCK_CTRL);
1092 src = __SHIFTOUT(val, BWI_CLOCK_CTRL_CLKSRC);
1093 if (src == BWI_CLKSRC_LP_OSC) {
1096 div = (__SHIFTOUT(val, BWI_CLOCK_CTRL_FDIV) + 1) << 2;
1098 /* Unknown source */
1099 if (src >= BWI_CLKSRC_MAX)
1100 src = BWI_CLKSRC_CS_OSC;
1103 val = CSR_READ_4(sc, BWI_CLOCK_INFO);
1105 src = BWI_CLKSRC_CS_OSC;
1106 div = (__SHIFTOUT(val, BWI_CLOCK_INFO_FDIV) + 1) << 2;
1109 KASSERT(src >= 0 && src < BWI_CLKSRC_MAX, ("bad src %d", src));
1110 KASSERT(div != 0, ("div zero"));
1112 DPRINTF(sc, BWI_DBG_ATTACH, "clksrc %s\n",
1113 src == BWI_CLKSRC_PCI ? "PCI" :
1114 (src == BWI_CLKSRC_LP_OSC ? "LP_OSC" : "CS_OSC"));
1116 freq->clkfreq_min = bwi_clkfreq[src].freq_min / div;
1117 freq->clkfreq_max = bwi_clkfreq[src].freq_max / div;
1119 DPRINTF(sc, BWI_DBG_ATTACH, "clkfreq min %u, max %u\n",
1120 freq->clkfreq_min, freq->clkfreq_max);
1124 bwi_set_clock_mode(struct bwi_softc *sc, enum bwi_clock_mode clk_mode)
1126 struct bwi_regwin *old, *com;
1127 uint32_t clk_ctrl, clk_src;
1128 int error, pwr_off = 0;
1130 com = &sc->sc_com_regwin;
1131 if (!BWI_REGWIN_EXIST(com))
1134 if (com->rw_rev >= 10 || com->rw_rev < 6)
1138 * For common regwin whose rev is [6, 10), the chip
1139 * must be capable to change clock mode.
1141 if ((sc->sc_cap & BWI_CAP_CLKMODE) == 0)
1144 error = bwi_regwin_switch(sc, com, &old);
1148 if (clk_mode == BWI_CLOCK_MODE_FAST)
1149 bwi_power_on(sc, 0); /* Don't turn on PLL */
1151 clk_ctrl = CSR_READ_4(sc, BWI_CLOCK_CTRL);
1152 clk_src = __SHIFTOUT(clk_ctrl, BWI_CLOCK_CTRL_CLKSRC);
1155 case BWI_CLOCK_MODE_FAST:
1156 clk_ctrl &= ~BWI_CLOCK_CTRL_SLOW;
1157 clk_ctrl |= BWI_CLOCK_CTRL_IGNPLL;
1159 case BWI_CLOCK_MODE_SLOW:
1160 clk_ctrl |= BWI_CLOCK_CTRL_SLOW;
1162 case BWI_CLOCK_MODE_DYN:
1163 clk_ctrl &= ~(BWI_CLOCK_CTRL_SLOW |
1164 BWI_CLOCK_CTRL_IGNPLL |
1165 BWI_CLOCK_CTRL_NODYN);
1166 if (clk_src != BWI_CLKSRC_CS_OSC) {
1167 clk_ctrl |= BWI_CLOCK_CTRL_NODYN;
1172 CSR_WRITE_4(sc, BWI_CLOCK_CTRL, clk_ctrl);
1175 bwi_power_off(sc, 0); /* Leave PLL as it is */
1177 return bwi_regwin_switch(sc, old, NULL);
1181 bwi_set_clock_delay(struct bwi_softc *sc)
1183 struct bwi_regwin *old, *com;
1186 com = &sc->sc_com_regwin;
1187 if (!BWI_REGWIN_EXIST(com))
1190 error = bwi_regwin_switch(sc, com, &old);
1194 if (sc->sc_bbp_id == BWI_BBPID_BCM4321) {
1195 if (sc->sc_bbp_rev == 0)
1196 CSR_WRITE_4(sc, BWI_CONTROL, BWI_CONTROL_MAGIC0);
1197 else if (sc->sc_bbp_rev == 1)
1198 CSR_WRITE_4(sc, BWI_CONTROL, BWI_CONTROL_MAGIC1);
1201 if (sc->sc_cap & BWI_CAP_CLKMODE) {
1202 if (com->rw_rev >= 10) {
1203 CSR_FILT_SETBITS_4(sc, BWI_CLOCK_INFO, 0xffff, 0x40000);
1205 struct bwi_clock_freq freq;
1207 bwi_get_clock_freq(sc, &freq);
1208 CSR_WRITE_4(sc, BWI_PLL_ON_DELAY,
1209 howmany(freq.clkfreq_max * 150, 1000000));
1210 CSR_WRITE_4(sc, BWI_FREQ_SEL_DELAY,
1211 howmany(freq.clkfreq_max * 15, 1000000));
1215 return bwi_regwin_switch(sc, old, NULL);
1221 struct bwi_softc *sc = xsc;
1222 struct ifnet *ifp = sc->sc_ifp;
1223 struct ieee80211com *ic = ifp->if_l2com;
1226 bwi_init_statechg(sc, 1);
1229 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1230 ieee80211_start_all(ic); /* start all vap's */
1234 bwi_init_statechg(struct bwi_softc *sc, int statechg)
1236 struct ifnet *ifp = sc->sc_ifp;
1237 struct bwi_mac *mac;
1240 bwi_stop_locked(sc, statechg);
1242 bwi_bbp_power_on(sc, BWI_CLOCK_MODE_FAST);
1246 mac = &sc->sc_mac[0];
1247 error = bwi_regwin_switch(sc, &mac->mac_regwin, NULL);
1249 if_printf(ifp, "%s: error %d on regwin switch\n",
1253 error = bwi_mac_init(mac);
1255 if_printf(ifp, "%s: error %d on MAC init\n", __func__, error);
1259 bwi_bbp_power_on(sc, BWI_CLOCK_MODE_DYN);
1261 bwi_set_bssid(sc, bwi_zero_addr); /* Clear BSSID */
1262 bwi_set_addr_filter(sc, BWI_ADDR_FILTER_MYADDR, IF_LLADDR(ifp));
1264 bwi_mac_reset_hwkeys(mac);
1266 if ((mac->mac_flags & BWI_MAC_F_HAS_TXSTATS) == 0) {
1271 * Drain any possible pending TX status
1273 for (i = 0; i < NRETRY; ++i) {
1274 if ((CSR_READ_4(sc, BWI_TXSTATUS0) &
1275 BWI_TXSTATUS0_VALID) == 0)
1277 CSR_READ_4(sc, BWI_TXSTATUS1);
1280 if_printf(ifp, "%s: can't drain TX status\n", __func__);
1284 if (mac->mac_phy.phy_mode == IEEE80211_MODE_11G)
1285 bwi_mac_updateslot(mac, 1);
1288 error = bwi_mac_start(mac);
1290 if_printf(ifp, "%s: error %d starting MAC\n", __func__, error);
1294 /* Clear stop flag before enabling interrupt */
1295 sc->sc_flags &= ~BWI_F_STOP;
1297 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1300 bwi_enable_intrs(sc, BWI_INIT_INTRS);
1303 bwi_stop_locked(sc, 1);
1307 bwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1309 #define IS_RUNNING(ifp) \
1310 ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
1311 struct bwi_softc *sc = ifp->if_softc;
1312 struct ieee80211com *ic = ifp->if_l2com;
1313 struct ifreq *ifr = (struct ifreq *) data;
1314 int error = 0, startall = 0;
1319 if (IS_RUNNING(ifp)) {
1320 struct bwi_mac *mac;
1323 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1324 ("current regwin type %d",
1325 sc->sc_cur_regwin->rw_type));
1326 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1328 if ((ifp->if_flags & IFF_PROMISC) &&
1329 (sc->sc_flags & BWI_F_PROMISC) == 0) {
1331 sc->sc_flags |= BWI_F_PROMISC;
1332 } else if ((ifp->if_flags & IFF_PROMISC) == 0 &&
1333 (sc->sc_flags & BWI_F_PROMISC)) {
1335 sc->sc_flags &= ~BWI_F_PROMISC;
1339 bwi_mac_set_promisc(mac, promisc);
1342 if (ifp->if_flags & IFF_UP) {
1343 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1344 bwi_init_statechg(sc, 1);
1348 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1349 bwi_stop_locked(sc, 1);
1353 ieee80211_start_all(ic);
1356 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1359 error = ether_ioctl(ifp, cmd, data);
1370 bwi_start(struct ifnet *ifp)
1372 struct bwi_softc *sc = ifp->if_softc;
1375 bwi_start_locked(ifp);
1380 bwi_start_locked(struct ifnet *ifp)
1382 struct bwi_softc *sc = ifp->if_softc;
1383 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
1384 struct ieee80211_frame *wh;
1385 struct ieee80211_node *ni;
1386 struct ieee80211_key *k;
1390 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1396 while (tbd->tbd_buf[idx].tb_mbuf == NULL) {
1397 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); /* XXX: LOCK */
1401 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1402 wh = mtod(m, struct ieee80211_frame *);
1403 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1404 k = ieee80211_crypto_encap(ni, m);
1406 ieee80211_free_node(ni);
1412 wh = NULL; /* Catch any invalid use */
1414 if (bwi_encap(sc, idx, m, ni) != 0) {
1415 /* 'm' is freed in bwi_encap() if we reach here */
1417 ieee80211_free_node(ni);
1424 idx = (idx + 1) % BWI_TX_NDESC;
1428 if (tbd->tbd_used + BWI_TX_NSPRDESC >= BWI_TX_NDESC) {
1429 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1440 bwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1441 const struct ieee80211_bpf_params *params)
1443 struct ieee80211com *ic = ni->ni_ic;
1444 struct ifnet *ifp = ic->ic_ifp;
1445 struct bwi_softc *sc = ifp->if_softc;
1447 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
1450 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1451 ieee80211_free_node(ni);
1458 KASSERT(tbd->tbd_buf[idx].tb_mbuf == NULL, ("slot %d not empty", idx));
1459 if (params == NULL) {
1461 * Legacy path; interpret frame contents to decide
1462 * precisely how to send the frame.
1464 error = bwi_encap(sc, idx, m, ni);
1467 * Caller supplied explicit parameters to use in
1468 * sending the frame.
1470 error = bwi_encap_raw(sc, idx, m, ni, params);
1474 if (++tbd->tbd_used + BWI_TX_NSPRDESC >= BWI_TX_NDESC)
1475 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1476 tbd->tbd_idx = (idx + 1) % BWI_TX_NDESC;
1479 /* NB: m is reclaimed on encap failure */
1480 ieee80211_free_node(ni);
1488 bwi_watchdog(struct ifnet *ifp)
1490 struct bwi_softc *sc = ifp->if_softc;
1493 if ((ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1494 if_printf(ifp, "watchdog timeout\n");
1496 taskqueue_enqueue(sc->sc_tq, &sc->sc_restart_task);
1502 bwi_stop(struct bwi_softc *sc, int statechg)
1505 bwi_stop_locked(sc, statechg);
1510 bwi_stop_locked(struct bwi_softc *sc, int statechg)
1512 struct ifnet *ifp = sc->sc_ifp;
1513 struct bwi_mac *mac;
1514 int i, error, pwr_off = 0;
1516 BWI_ASSERT_LOCKED(sc);
1518 callout_stop(&sc->sc_calib_ch);
1519 callout_stop(&sc->sc_led_blink_ch);
1520 sc->sc_led_blinking = 0;
1521 sc->sc_flags |= BWI_F_STOP;
1523 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1524 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1525 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1526 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1528 bwi_disable_intrs(sc, BWI_ALL_INTRS);
1529 CSR_READ_4(sc, BWI_MAC_INTR_MASK);
1533 for (i = 0; i < sc->sc_nmac; ++i) {
1534 struct bwi_regwin *old_rw;
1536 mac = &sc->sc_mac[i];
1537 if ((mac->mac_flags & BWI_MAC_F_INITED) == 0)
1540 error = bwi_regwin_switch(sc, &mac->mac_regwin, &old_rw);
1544 bwi_mac_shutdown(mac);
1547 bwi_regwin_switch(sc, old_rw, NULL);
1551 bwi_bbp_power_off(sc);
1553 sc->sc_tx_timer = 0;
1555 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1561 struct bwi_softc *sc = xsc;
1562 struct ifnet *ifp = sc->sc_ifp;
1563 struct bwi_mac *mac;
1564 uint32_t intr_status;
1565 uint32_t txrx_intr_status[BWI_TXRX_NRING];
1566 int i, txrx_error, tx = 0, rx_data = -1;
1570 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1571 (sc->sc_flags & BWI_F_STOP)) {
1576 * Get interrupt status
1578 intr_status = CSR_READ_4(sc, BWI_MAC_INTR_STATUS);
1579 if (intr_status == 0xffffffff) { /* Not for us */
1584 DPRINTF(sc, BWI_DBG_INTR, "intr status 0x%08x\n", intr_status);
1586 intr_status &= CSR_READ_4(sc, BWI_MAC_INTR_MASK);
1587 if (intr_status == 0) { /* Nothing is interesting */
1592 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1593 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1594 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1597 DPRINTF(sc, BWI_DBG_INTR, "%s\n", "TX/RX intr");
1598 for (i = 0; i < BWI_TXRX_NRING; ++i) {
1601 if (BWI_TXRX_IS_RX(i))
1602 mask = BWI_TXRX_RX_INTRS;
1604 mask = BWI_TXRX_TX_INTRS;
1606 txrx_intr_status[i] =
1607 CSR_READ_4(sc, BWI_TXRX_INTR_STATUS(i)) & mask;
1609 _DPRINTF(sc, BWI_DBG_INTR, ", %d 0x%08x",
1610 i, txrx_intr_status[i]);
1612 if (txrx_intr_status[i] & BWI_TXRX_INTR_ERROR) {
1614 "%s: intr fatal TX/RX (%d) error 0x%08x\n",
1615 __func__, i, txrx_intr_status[i]);
1619 _DPRINTF(sc, BWI_DBG_INTR, "%s\n", "");
1622 * Acknowledge interrupt
1624 CSR_WRITE_4(sc, BWI_MAC_INTR_STATUS, intr_status);
1626 for (i = 0; i < BWI_TXRX_NRING; ++i)
1627 CSR_WRITE_4(sc, BWI_TXRX_INTR_STATUS(i), txrx_intr_status[i]);
1629 /* Disable all interrupts */
1630 bwi_disable_intrs(sc, BWI_ALL_INTRS);
1633 * http://bcm-specs.sipsolutions.net/Interrupts
1634 * Says for this bit (0x800):
1637 * We got this one while testing things when by accident the
1638 * template ram wasn't set to big endian when it should have
1639 * been after writing the initial values. It keeps on being
1640 * triggered, the only way to stop it seems to shut down the
1643 * Suggesting that we should never get it and if we do we're not
1644 * feeding TX packets into the MAC correctly if we do... Apparently,
1645 * it is valid only on mac version 5 and higher, but I couldn't
1646 * find a reference for that... Since I see them from time to time
1647 * on my card, this suggests an error in the tx path still...
1649 if (intr_status & BWI_INTR_PHY_TXERR) {
1650 if (mac->mac_flags & BWI_MAC_F_PHYE_RESET) {
1651 if_printf(ifp, "%s: intr PHY TX error\n", __func__);
1652 taskqueue_enqueue(sc->sc_tq, &sc->sc_restart_task);
1659 /* TODO: reset device */
1662 if (intr_status & BWI_INTR_TBTT)
1663 bwi_mac_config_ps(mac);
1665 if (intr_status & BWI_INTR_EO_ATIM)
1666 if_printf(ifp, "EO_ATIM\n");
1668 if (intr_status & BWI_INTR_PMQ) {
1670 if ((CSR_READ_4(sc, BWI_MAC_PS_STATUS) & 0x8) == 0)
1673 CSR_WRITE_2(sc, BWI_MAC_PS_STATUS, 0x2);
1676 if (intr_status & BWI_INTR_NOISE)
1677 if_printf(ifp, "intr noise\n");
1679 if (txrx_intr_status[0] & BWI_TXRX_INTR_RX) {
1680 rx_data = sc->sc_rxeof(sc);
1681 if (sc->sc_flags & BWI_F_STOP) {
1687 if (txrx_intr_status[3] & BWI_TXRX_INTR_RX) {
1688 sc->sc_txeof_status(sc);
1692 if (intr_status & BWI_INTR_TX_DONE) {
1697 /* Re-enable interrupts */
1698 bwi_enable_intrs(sc, BWI_INIT_INTRS);
1700 if (sc->sc_blink_led != NULL && sc->sc_led_blink) {
1701 int evt = BWI_LED_EVENT_NONE;
1703 if (tx && rx_data > 0) {
1704 if (sc->sc_rx_rate > sc->sc_tx_rate)
1705 evt = BWI_LED_EVENT_RX;
1707 evt = BWI_LED_EVENT_TX;
1709 evt = BWI_LED_EVENT_TX;
1710 } else if (rx_data > 0) {
1711 evt = BWI_LED_EVENT_RX;
1712 } else if (rx_data == 0) {
1713 evt = BWI_LED_EVENT_POLL;
1716 if (evt != BWI_LED_EVENT_NONE)
1717 bwi_led_event(sc, evt);
1724 bwi_scan_start(struct ieee80211com *ic)
1726 struct bwi_softc *sc = ic->ic_ifp->if_softc;
1729 /* Enable MAC beacon promiscuity */
1730 CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PASS_BCN);
1735 bwi_set_channel(struct ieee80211com *ic)
1737 struct bwi_softc *sc = ic->ic_ifp->if_softc;
1738 struct ieee80211_channel *c = ic->ic_curchan;
1739 struct bwi_mac *mac;
1742 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1743 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1744 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1745 bwi_rf_set_chan(mac, ieee80211_chan2ieee(ic, c), 0);
1747 sc->sc_rates = ieee80211_get_ratetable(c);
1750 * Setup radio tap channel freq and flags
1752 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
1753 htole16(c->ic_freq);
1754 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
1755 htole16(c->ic_flags & 0xffff);
1761 bwi_scan_end(struct ieee80211com *ic)
1763 struct bwi_softc *sc = ic->ic_ifp->if_softc;
1766 CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PASS_BCN);
1771 bwi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1773 struct bwi_vap *bvp = BWI_VAP(vap);
1774 struct ieee80211com *ic= vap->iv_ic;
1775 struct ifnet *ifp = ic->ic_ifp;
1776 enum ieee80211_state ostate = vap->iv_state;
1777 struct bwi_softc *sc = ifp->if_softc;
1778 struct bwi_mac *mac;
1779 struct ieee80211_node *ni = vap->iv_bss;
1784 callout_stop(&sc->sc_calib_ch);
1786 if (nstate == IEEE80211_S_INIT)
1787 sc->sc_txpwrcb_type = BWI_TXPWR_INIT;
1789 bwi_led_newstate(sc, nstate);
1791 error = bvp->bv_newstate(vap, nstate, arg);
1796 * Clear the BSSID when we stop a STA
1798 if (vap->iv_opmode == IEEE80211_M_STA) {
1799 if (ostate == IEEE80211_S_RUN && nstate != IEEE80211_S_RUN) {
1801 * Clear out the BSSID. If we reassociate to
1802 * the same AP, this will reinialize things
1805 if (ic->ic_opmode == IEEE80211_M_STA &&
1806 !(sc->sc_flags & BWI_F_STOP))
1807 bwi_set_bssid(sc, bwi_zero_addr);
1811 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
1813 } else if (nstate == IEEE80211_S_RUN) {
1814 bwi_set_bssid(sc, vap->iv_bss->ni_bssid);
1816 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1817 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1818 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1820 /* Initial TX power calibration */
1821 bwi_mac_calibrate_txpower(mac, BWI_TXPWR_INIT);
1823 sc->sc_txpwrcb_type = BWI_TXPWR_FORCE;
1825 sc->sc_txpwrcb_type = BWI_TXPWR_CALIB;
1827 if (vap->iv_opmode == IEEE80211_M_STA) {
1828 /* fake a join to init the tx rate */
1829 bwi_newassoc(ni, 1);
1832 callout_reset(&sc->sc_calib_ch, hz, bwi_calibrate, sc);
1841 static struct ieee80211_node *
1842 bwi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1844 struct bwi_node *bn;
1846 bn = malloc(sizeof(struct bwi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
1847 return bn != NULL ? &bn->ni : NULL;
1851 bwi_newassoc(struct ieee80211_node *ni, int isnew)
1853 struct ieee80211vap *vap = ni->ni_vap;
1855 ieee80211_amrr_node_init(&BWI_VAP(vap)->bv_amrr,
1856 &BWI_NODE(ni)->amn, ni);
1860 bwi_media_change(struct ifnet *ifp)
1862 int error = ieee80211_media_change(ifp);
1863 /* NB: only the fixed rate can change and that doesn't need a reset */
1864 return (error == ENETRESET ? 0 : error);
1868 bwi_dma_alloc(struct bwi_softc *sc)
1870 int error, i, has_txstats;
1871 bus_addr_t lowaddr = 0;
1872 bus_size_t tx_ring_sz, rx_ring_sz, desc_sz = 0;
1873 uint32_t txrx_ctrl_step = 0;
1876 for (i = 0; i < sc->sc_nmac; ++i) {
1877 if (sc->sc_mac[i].mac_flags & BWI_MAC_F_HAS_TXSTATS) {
1883 switch (sc->sc_bus_space) {
1884 case BWI_BUS_SPACE_30BIT:
1885 case BWI_BUS_SPACE_32BIT:
1886 if (sc->sc_bus_space == BWI_BUS_SPACE_30BIT)
1887 lowaddr = BWI_BUS_SPACE_MAXADDR;
1889 lowaddr = BUS_SPACE_MAXADDR_32BIT;
1890 desc_sz = sizeof(struct bwi_desc32);
1891 txrx_ctrl_step = 0x20;
1893 sc->sc_init_tx_ring = bwi_init_tx_ring32;
1894 sc->sc_free_tx_ring = bwi_free_tx_ring32;
1895 sc->sc_init_rx_ring = bwi_init_rx_ring32;
1896 sc->sc_free_rx_ring = bwi_free_rx_ring32;
1897 sc->sc_setup_rxdesc = bwi_setup_rx_desc32;
1898 sc->sc_setup_txdesc = bwi_setup_tx_desc32;
1899 sc->sc_rxeof = bwi_rxeof32;
1900 sc->sc_start_tx = bwi_start_tx32;
1902 sc->sc_init_txstats = bwi_init_txstats32;
1903 sc->sc_free_txstats = bwi_free_txstats32;
1904 sc->sc_txeof_status = bwi_txeof_status32;
1908 case BWI_BUS_SPACE_64BIT:
1909 lowaddr = BUS_SPACE_MAXADDR; /* XXX */
1910 desc_sz = sizeof(struct bwi_desc64);
1911 txrx_ctrl_step = 0x40;
1913 sc->sc_init_tx_ring = bwi_init_tx_ring64;
1914 sc->sc_free_tx_ring = bwi_free_tx_ring64;
1915 sc->sc_init_rx_ring = bwi_init_rx_ring64;
1916 sc->sc_free_rx_ring = bwi_free_rx_ring64;
1917 sc->sc_setup_rxdesc = bwi_setup_rx_desc64;
1918 sc->sc_setup_txdesc = bwi_setup_tx_desc64;
1919 sc->sc_rxeof = bwi_rxeof64;
1920 sc->sc_start_tx = bwi_start_tx64;
1922 sc->sc_init_txstats = bwi_init_txstats64;
1923 sc->sc_free_txstats = bwi_free_txstats64;
1924 sc->sc_txeof_status = bwi_txeof_status64;
1929 KASSERT(lowaddr != 0, ("lowaddr zero"));
1930 KASSERT(desc_sz != 0, ("desc_sz zero"));
1931 KASSERT(txrx_ctrl_step != 0, ("txrx_ctrl_step zero"));
1933 tx_ring_sz = roundup(desc_sz * BWI_TX_NDESC, BWI_RING_ALIGN);
1934 rx_ring_sz = roundup(desc_sz * BWI_RX_NDESC, BWI_RING_ALIGN);
1937 * Create top level DMA tag
1939 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), /* parent */
1940 BWI_ALIGN, 0, /* alignment, bounds */
1941 lowaddr, /* lowaddr */
1942 BUS_SPACE_MAXADDR, /* highaddr */
1943 NULL, NULL, /* filter, filterarg */
1944 MAXBSIZE, /* maxsize */
1945 BUS_SPACE_UNRESTRICTED, /* nsegments */
1946 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1947 BUS_DMA_ALLOCNOW, /* flags */
1948 NULL, NULL, /* lockfunc, lockarg */
1949 &sc->sc_parent_dtag);
1951 device_printf(sc->sc_dev, "can't create parent DMA tag\n");
1955 #define TXRX_CTRL(idx) (BWI_TXRX_CTRL_BASE + (idx) * txrx_ctrl_step)
1958 * Create TX ring DMA stuffs
1960 error = bus_dma_tag_create(sc->sc_parent_dtag,
1967 BUS_SPACE_MAXSIZE_32BIT,
1970 &sc->sc_txring_dtag);
1972 device_printf(sc->sc_dev, "can't create TX ring DMA tag\n");
1976 for (i = 0; i < BWI_TX_NRING; ++i) {
1977 error = bwi_dma_ring_alloc(sc, sc->sc_txring_dtag,
1978 &sc->sc_tx_rdata[i], tx_ring_sz,
1981 device_printf(sc->sc_dev, "%dth TX ring "
1982 "DMA alloc failed\n", i);
1988 * Create RX ring DMA stuffs
1990 error = bus_dma_tag_create(sc->sc_parent_dtag,
1997 BUS_SPACE_MAXSIZE_32BIT,
2000 &sc->sc_rxring_dtag);
2002 device_printf(sc->sc_dev, "can't create RX ring DMA tag\n");
2006 error = bwi_dma_ring_alloc(sc, sc->sc_rxring_dtag, &sc->sc_rx_rdata,
2007 rx_ring_sz, TXRX_CTRL(0));
2009 device_printf(sc->sc_dev, "RX ring DMA alloc failed\n");
2014 error = bwi_dma_txstats_alloc(sc, TXRX_CTRL(3), desc_sz);
2016 device_printf(sc->sc_dev,
2017 "TX stats DMA alloc failed\n");
2024 return bwi_dma_mbuf_create(sc);
2028 bwi_dma_free(struct bwi_softc *sc)
2030 if (sc->sc_txring_dtag != NULL) {
2033 for (i = 0; i < BWI_TX_NRING; ++i) {
2034 struct bwi_ring_data *rd = &sc->sc_tx_rdata[i];
2036 if (rd->rdata_desc != NULL) {
2037 bus_dmamap_unload(sc->sc_txring_dtag,
2039 bus_dmamem_free(sc->sc_txring_dtag,
2044 bus_dma_tag_destroy(sc->sc_txring_dtag);
2047 if (sc->sc_rxring_dtag != NULL) {
2048 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2050 if (rd->rdata_desc != NULL) {
2051 bus_dmamap_unload(sc->sc_rxring_dtag, rd->rdata_dmap);
2052 bus_dmamem_free(sc->sc_rxring_dtag, rd->rdata_desc,
2055 bus_dma_tag_destroy(sc->sc_rxring_dtag);
2058 bwi_dma_txstats_free(sc);
2059 bwi_dma_mbuf_destroy(sc, BWI_TX_NRING, 1);
2061 if (sc->sc_parent_dtag != NULL)
2062 bus_dma_tag_destroy(sc->sc_parent_dtag);
2066 bwi_dma_ring_alloc(struct bwi_softc *sc, bus_dma_tag_t dtag,
2067 struct bwi_ring_data *rd, bus_size_t size,
2072 error = bus_dmamem_alloc(dtag, &rd->rdata_desc,
2073 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2076 device_printf(sc->sc_dev, "can't allocate DMA mem\n");
2080 error = bus_dmamap_load(dtag, rd->rdata_dmap, rd->rdata_desc, size,
2081 bwi_dma_ring_addr, &rd->rdata_paddr,
2084 device_printf(sc->sc_dev, "can't load DMA mem\n");
2085 bus_dmamem_free(dtag, rd->rdata_desc, rd->rdata_dmap);
2086 rd->rdata_desc = NULL;
2090 rd->rdata_txrx_ctrl = txrx_ctrl;
2095 bwi_dma_txstats_alloc(struct bwi_softc *sc, uint32_t ctrl_base,
2098 struct bwi_txstats_data *st;
2099 bus_size_t dma_size;
2102 st = malloc(sizeof(*st), M_DEVBUF, M_NOWAIT | M_ZERO);
2104 device_printf(sc->sc_dev, "can't allocate txstats data\n");
2107 sc->sc_txstats = st;
2110 * Create TX stats descriptor DMA stuffs
2112 dma_size = roundup(desc_sz * BWI_TXSTATS_NDESC, BWI_RING_ALIGN);
2114 error = bus_dma_tag_create(sc->sc_parent_dtag,
2122 BUS_SPACE_MAXSIZE_32BIT,
2125 &st->stats_ring_dtag);
2127 device_printf(sc->sc_dev, "can't create txstats ring "
2132 error = bus_dmamem_alloc(st->stats_ring_dtag, &st->stats_ring,
2133 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2134 &st->stats_ring_dmap);
2136 device_printf(sc->sc_dev, "can't allocate txstats ring "
2138 bus_dma_tag_destroy(st->stats_ring_dtag);
2139 st->stats_ring_dtag = NULL;
2143 error = bus_dmamap_load(st->stats_ring_dtag, st->stats_ring_dmap,
2144 st->stats_ring, dma_size,
2145 bwi_dma_ring_addr, &st->stats_ring_paddr,
2148 device_printf(sc->sc_dev, "can't load txstats ring DMA mem\n");
2149 bus_dmamem_free(st->stats_ring_dtag, st->stats_ring,
2150 st->stats_ring_dmap);
2151 bus_dma_tag_destroy(st->stats_ring_dtag);
2152 st->stats_ring_dtag = NULL;
2157 * Create TX stats DMA stuffs
2159 dma_size = roundup(sizeof(struct bwi_txstats) * BWI_TXSTATS_NDESC,
2162 error = bus_dma_tag_create(sc->sc_parent_dtag,
2170 BUS_SPACE_MAXSIZE_32BIT,
2175 device_printf(sc->sc_dev, "can't create txstats DMA tag\n");
2179 error = bus_dmamem_alloc(st->stats_dtag, (void **)&st->stats,
2180 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2183 device_printf(sc->sc_dev, "can't allocate txstats DMA mem\n");
2184 bus_dma_tag_destroy(st->stats_dtag);
2185 st->stats_dtag = NULL;
2189 error = bus_dmamap_load(st->stats_dtag, st->stats_dmap, st->stats,
2190 dma_size, bwi_dma_ring_addr, &st->stats_paddr,
2193 device_printf(sc->sc_dev, "can't load txstats DMA mem\n");
2194 bus_dmamem_free(st->stats_dtag, st->stats, st->stats_dmap);
2195 bus_dma_tag_destroy(st->stats_dtag);
2196 st->stats_dtag = NULL;
2200 st->stats_ctrl_base = ctrl_base;
2205 bwi_dma_txstats_free(struct bwi_softc *sc)
2207 struct bwi_txstats_data *st;
2209 if (sc->sc_txstats == NULL)
2211 st = sc->sc_txstats;
2213 if (st->stats_ring_dtag != NULL) {
2214 bus_dmamap_unload(st->stats_ring_dtag, st->stats_ring_dmap);
2215 bus_dmamem_free(st->stats_ring_dtag, st->stats_ring,
2216 st->stats_ring_dmap);
2217 bus_dma_tag_destroy(st->stats_ring_dtag);
2220 if (st->stats_dtag != NULL) {
2221 bus_dmamap_unload(st->stats_dtag, st->stats_dmap);
2222 bus_dmamem_free(st->stats_dtag, st->stats, st->stats_dmap);
2223 bus_dma_tag_destroy(st->stats_dtag);
2230 bwi_dma_ring_addr(void *arg, bus_dma_segment_t *seg, int nseg, int error)
2232 KASSERT(nseg == 1, ("too many segments\n"));
2233 *((bus_addr_t *)arg) = seg->ds_addr;
2237 bwi_dma_mbuf_create(struct bwi_softc *sc)
2239 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2240 int i, j, k, ntx, error;
2243 * Create TX/RX mbuf DMA tag
2245 error = bus_dma_tag_create(sc->sc_parent_dtag,
2253 BUS_SPACE_MAXSIZE_32BIT,
2258 device_printf(sc->sc_dev, "can't create mbuf DMA tag\n");
2265 * Create TX mbuf DMA map
2267 for (i = 0; i < BWI_TX_NRING; ++i) {
2268 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[i];
2270 for (j = 0; j < BWI_TX_NDESC; ++j) {
2271 error = bus_dmamap_create(sc->sc_buf_dtag, 0,
2272 &tbd->tbd_buf[j].tb_dmap);
2274 device_printf(sc->sc_dev, "can't create "
2275 "%dth tbd, %dth DMA map\n", i, j);
2278 for (k = 0; k < j; ++k) {
2279 bus_dmamap_destroy(sc->sc_buf_dtag,
2280 tbd->tbd_buf[k].tb_dmap);
2289 * Create RX mbuf DMA map and a spare DMA map
2291 error = bus_dmamap_create(sc->sc_buf_dtag, 0,
2292 &rbd->rbd_tmp_dmap);
2294 device_printf(sc->sc_dev,
2295 "can't create spare RX buf DMA map\n");
2299 for (j = 0; j < BWI_RX_NDESC; ++j) {
2300 error = bus_dmamap_create(sc->sc_buf_dtag, 0,
2301 &rbd->rbd_buf[j].rb_dmap);
2303 device_printf(sc->sc_dev, "can't create %dth "
2304 "RX buf DMA map\n", j);
2306 for (k = 0; k < j; ++k) {
2307 bus_dmamap_destroy(sc->sc_buf_dtag,
2308 rbd->rbd_buf[j].rb_dmap);
2310 bus_dmamap_destroy(sc->sc_buf_dtag,
2318 bwi_dma_mbuf_destroy(sc, ntx, 0);
2323 bwi_dma_mbuf_destroy(struct bwi_softc *sc, int ntx, int nrx)
2327 if (sc->sc_buf_dtag == NULL)
2330 for (i = 0; i < ntx; ++i) {
2331 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[i];
2333 for (j = 0; j < BWI_TX_NDESC; ++j) {
2334 struct bwi_txbuf *tb = &tbd->tbd_buf[j];
2336 if (tb->tb_mbuf != NULL) {
2337 bus_dmamap_unload(sc->sc_buf_dtag,
2339 m_freem(tb->tb_mbuf);
2341 if (tb->tb_ni != NULL)
2342 ieee80211_free_node(tb->tb_ni);
2343 bus_dmamap_destroy(sc->sc_buf_dtag, tb->tb_dmap);
2348 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2350 bus_dmamap_destroy(sc->sc_buf_dtag, rbd->rbd_tmp_dmap);
2351 for (j = 0; j < BWI_RX_NDESC; ++j) {
2352 struct bwi_rxbuf *rb = &rbd->rbd_buf[j];
2354 if (rb->rb_mbuf != NULL) {
2355 bus_dmamap_unload(sc->sc_buf_dtag,
2357 m_freem(rb->rb_mbuf);
2359 bus_dmamap_destroy(sc->sc_buf_dtag, rb->rb_dmap);
2363 bus_dma_tag_destroy(sc->sc_buf_dtag);
2364 sc->sc_buf_dtag = NULL;
2368 bwi_enable_intrs(struct bwi_softc *sc, uint32_t enable_intrs)
2370 CSR_SETBITS_4(sc, BWI_MAC_INTR_MASK, enable_intrs);
2374 bwi_disable_intrs(struct bwi_softc *sc, uint32_t disable_intrs)
2376 CSR_CLRBITS_4(sc, BWI_MAC_INTR_MASK, disable_intrs);
2380 bwi_init_tx_ring32(struct bwi_softc *sc, int ring_idx)
2382 struct bwi_ring_data *rd;
2383 struct bwi_txbuf_data *tbd;
2384 uint32_t val, addr_hi, addr_lo;
2386 KASSERT(ring_idx < BWI_TX_NRING, ("ring_idx %d", ring_idx));
2387 rd = &sc->sc_tx_rdata[ring_idx];
2388 tbd = &sc->sc_tx_bdata[ring_idx];
2393 bzero(rd->rdata_desc, sizeof(struct bwi_desc32) * BWI_TX_NDESC);
2394 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap,
2395 BUS_DMASYNC_PREWRITE);
2397 addr_lo = __SHIFTOUT(rd->rdata_paddr, BWI_TXRX32_RINGINFO_ADDR_MASK);
2398 addr_hi = __SHIFTOUT(rd->rdata_paddr, BWI_TXRX32_RINGINFO_FUNC_MASK);
2400 val = __SHIFTIN(addr_lo, BWI_TXRX32_RINGINFO_ADDR_MASK) |
2401 __SHIFTIN(BWI_TXRX32_RINGINFO_FUNC_TXRX,
2402 BWI_TXRX32_RINGINFO_FUNC_MASK);
2403 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_RINGINFO, val);
2405 val = __SHIFTIN(addr_hi, BWI_TXRX32_CTRL_ADDRHI_MASK) |
2406 BWI_TXRX32_CTRL_ENABLE;
2407 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_CTRL, val);
2413 bwi_init_rxdesc_ring32(struct bwi_softc *sc, uint32_t ctrl_base,
2414 bus_addr_t paddr, int hdr_size, int ndesc)
2416 uint32_t val, addr_hi, addr_lo;
2418 addr_lo = __SHIFTOUT(paddr, BWI_TXRX32_RINGINFO_ADDR_MASK);
2419 addr_hi = __SHIFTOUT(paddr, BWI_TXRX32_RINGINFO_FUNC_MASK);
2421 val = __SHIFTIN(addr_lo, BWI_TXRX32_RINGINFO_ADDR_MASK) |
2422 __SHIFTIN(BWI_TXRX32_RINGINFO_FUNC_TXRX,
2423 BWI_TXRX32_RINGINFO_FUNC_MASK);
2424 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_RINGINFO, val);
2426 val = __SHIFTIN(hdr_size, BWI_RX32_CTRL_HDRSZ_MASK) |
2427 __SHIFTIN(addr_hi, BWI_TXRX32_CTRL_ADDRHI_MASK) |
2428 BWI_TXRX32_CTRL_ENABLE;
2429 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_CTRL, val);
2431 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_INDEX,
2432 (ndesc - 1) * sizeof(struct bwi_desc32));
2436 bwi_init_rx_ring32(struct bwi_softc *sc)
2438 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2441 sc->sc_rx_bdata.rbd_idx = 0;
2443 for (i = 0; i < BWI_RX_NDESC; ++i) {
2444 error = bwi_newbuf(sc, i, 1);
2446 device_printf(sc->sc_dev,
2447 "can't allocate %dth RX buffer\n", i);
2451 bus_dmamap_sync(sc->sc_rxring_dtag, rd->rdata_dmap,
2452 BUS_DMASYNC_PREWRITE);
2454 bwi_init_rxdesc_ring32(sc, rd->rdata_txrx_ctrl, rd->rdata_paddr,
2455 sizeof(struct bwi_rxbuf_hdr), BWI_RX_NDESC);
2460 bwi_init_txstats32(struct bwi_softc *sc)
2462 struct bwi_txstats_data *st = sc->sc_txstats;
2463 bus_addr_t stats_paddr;
2466 bzero(st->stats, BWI_TXSTATS_NDESC * sizeof(struct bwi_txstats));
2467 bus_dmamap_sync(st->stats_dtag, st->stats_dmap, BUS_DMASYNC_PREWRITE);
2471 stats_paddr = st->stats_paddr;
2472 for (i = 0; i < BWI_TXSTATS_NDESC; ++i) {
2473 bwi_setup_desc32(sc, st->stats_ring, BWI_TXSTATS_NDESC, i,
2474 stats_paddr, sizeof(struct bwi_txstats), 0);
2475 stats_paddr += sizeof(struct bwi_txstats);
2477 bus_dmamap_sync(st->stats_ring_dtag, st->stats_ring_dmap,
2478 BUS_DMASYNC_PREWRITE);
2480 bwi_init_rxdesc_ring32(sc, st->stats_ctrl_base,
2481 st->stats_ring_paddr, 0, BWI_TXSTATS_NDESC);
2486 bwi_setup_rx_desc32(struct bwi_softc *sc, int buf_idx, bus_addr_t paddr,
2489 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2491 KASSERT(buf_idx < BWI_RX_NDESC, ("buf_idx %d", buf_idx));
2492 bwi_setup_desc32(sc, rd->rdata_desc, BWI_RX_NDESC, buf_idx,
2497 bwi_setup_tx_desc32(struct bwi_softc *sc, struct bwi_ring_data *rd,
2498 int buf_idx, bus_addr_t paddr, int buf_len)
2500 KASSERT(buf_idx < BWI_TX_NDESC, ("buf_idx %d", buf_idx));
2501 bwi_setup_desc32(sc, rd->rdata_desc, BWI_TX_NDESC, buf_idx,
2506 bwi_init_tx_ring64(struct bwi_softc *sc, int ring_idx)
2513 bwi_init_rx_ring64(struct bwi_softc *sc)
2520 bwi_init_txstats64(struct bwi_softc *sc)
2527 bwi_setup_rx_desc64(struct bwi_softc *sc, int buf_idx, bus_addr_t paddr,
2534 bwi_setup_tx_desc64(struct bwi_softc *sc, struct bwi_ring_data *rd,
2535 int buf_idx, bus_addr_t paddr, int buf_len)
2541 bwi_dma_buf_addr(void *arg, bus_dma_segment_t *seg, int nseg,
2542 bus_size_t mapsz __unused, int error)
2545 KASSERT(nseg == 1, ("too many segments(%d)\n", nseg));
2546 *((bus_addr_t *)arg) = seg->ds_addr;
2551 bwi_newbuf(struct bwi_softc *sc, int buf_idx, int init)
2553 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2554 struct bwi_rxbuf *rxbuf = &rbd->rbd_buf[buf_idx];
2555 struct bwi_rxbuf_hdr *hdr;
2561 KASSERT(buf_idx < BWI_RX_NDESC, ("buf_idx %d", buf_idx));
2563 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2568 * If the NIC is up and running, we need to:
2569 * - Clear RX buffer's header.
2570 * - Restore RX descriptor settings.
2577 m->m_len = m->m_pkthdr.len = MCLBYTES;
2580 * Try to load RX buf into temporary DMA map
2582 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, rbd->rbd_tmp_dmap, m,
2583 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT);
2588 * See the comment above
2597 bus_dmamap_unload(sc->sc_buf_dtag, rxbuf->rb_dmap);
2599 rxbuf->rb_paddr = paddr;
2602 * Swap RX buf's DMA map with the loaded temporary one
2604 map = rxbuf->rb_dmap;
2605 rxbuf->rb_dmap = rbd->rbd_tmp_dmap;
2606 rbd->rbd_tmp_dmap = map;
2610 * Clear RX buf header
2612 hdr = mtod(rxbuf->rb_mbuf, struct bwi_rxbuf_hdr *);
2613 bzero(hdr, sizeof(*hdr));
2614 bus_dmamap_sync(sc->sc_buf_dtag, rxbuf->rb_dmap, BUS_DMASYNC_PREWRITE);
2617 * Setup RX buf descriptor
2619 sc->sc_setup_rxdesc(sc, buf_idx, rxbuf->rb_paddr,
2620 rxbuf->rb_mbuf->m_len - sizeof(*hdr));
2625 bwi_set_addr_filter(struct bwi_softc *sc, uint16_t addr_ofs,
2626 const uint8_t *addr)
2630 CSR_WRITE_2(sc, BWI_ADDR_FILTER_CTRL,
2631 BWI_ADDR_FILTER_CTRL_SET | addr_ofs);
2633 for (i = 0; i < (IEEE80211_ADDR_LEN / 2); ++i) {
2636 addr_val = (uint16_t)addr[i * 2] |
2637 (((uint16_t)addr[(i * 2) + 1]) << 8);
2638 CSR_WRITE_2(sc, BWI_ADDR_FILTER_DATA, addr_val);
2643 bwi_rxeof(struct bwi_softc *sc, int end_idx)
2645 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2646 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2647 struct ifnet *ifp = sc->sc_ifp;
2648 struct ieee80211com *ic = ifp->if_l2com;
2649 int idx, rx_data = 0;
2652 while (idx != end_idx) {
2653 struct bwi_rxbuf *rb = &rbd->rbd_buf[idx];
2654 struct bwi_rxbuf_hdr *hdr;
2655 struct ieee80211_frame_min *wh;
2656 struct ieee80211_node *ni;
2660 int buflen, wh_ofs, hdr_extra, rssi, noise, type, rate;
2663 bus_dmamap_sync(sc->sc_buf_dtag, rb->rb_dmap,
2664 BUS_DMASYNC_POSTREAD);
2666 if (bwi_newbuf(sc, idx, 0)) {
2671 hdr = mtod(m, struct bwi_rxbuf_hdr *);
2672 flags2 = le16toh(hdr->rxh_flags2);
2675 if (flags2 & BWI_RXH_F2_TYPE2FRAME)
2677 wh_ofs = hdr_extra + 6; /* XXX magic number */
2679 buflen = le16toh(hdr->rxh_buflen);
2680 if (buflen < BWI_FRAME_MIN_LEN(wh_ofs)) {
2681 if_printf(ifp, "%s: zero length data, hdr_extra %d\n",
2682 __func__, hdr_extra);
2688 plcp = ((const uint8_t *)(hdr + 1) + hdr_extra);
2689 rssi = bwi_calc_rssi(sc, hdr);
2690 noise = bwi_calc_noise(sc);
2692 m->m_pkthdr.rcvif = ifp;
2693 m->m_len = m->m_pkthdr.len = buflen + sizeof(*hdr);
2694 m_adj(m, sizeof(*hdr) + wh_ofs);
2696 if (htole16(hdr->rxh_flags1) & BWI_RXH_F1_OFDM)
2697 rate = bwi_ofdm_plcp2rate(plcp);
2699 rate = bwi_ds_plcp2rate(plcp);
2702 if (ieee80211_radiotap_active(ic))
2703 bwi_rx_radiotap(sc, m, hdr, plcp, rate, rssi, noise);
2705 m_adj(m, -IEEE80211_CRC_LEN);
2709 wh = mtod(m, struct ieee80211_frame_min *);
2710 ni = ieee80211_find_rxnode(ic, wh);
2712 type = ieee80211_input(ni, m, rssi - noise, noise);
2713 ieee80211_free_node(ni);
2715 type = ieee80211_input_all(ic, m, rssi - noise, noise);
2716 if (type == IEEE80211_FC0_TYPE_DATA) {
2718 sc->sc_rx_rate = rate;
2723 idx = (idx + 1) % BWI_RX_NDESC;
2725 if (sc->sc_flags & BWI_F_STOP) {
2727 * Take the fast lane, don't do
2728 * any damage to softc
2735 bus_dmamap_sync(sc->sc_rxring_dtag, rd->rdata_dmap,
2736 BUS_DMASYNC_PREWRITE);
2742 bwi_rxeof32(struct bwi_softc *sc)
2744 uint32_t val, rx_ctrl;
2745 int end_idx, rx_data;
2747 rx_ctrl = sc->sc_rx_rdata.rdata_txrx_ctrl;
2749 val = CSR_READ_4(sc, rx_ctrl + BWI_RX32_STATUS);
2750 end_idx = __SHIFTOUT(val, BWI_RX32_STATUS_INDEX_MASK) /
2751 sizeof(struct bwi_desc32);
2753 rx_data = bwi_rxeof(sc, end_idx);
2755 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_INDEX,
2756 end_idx * sizeof(struct bwi_desc32));
2762 bwi_rxeof64(struct bwi_softc *sc)
2769 bwi_reset_rx_ring32(struct bwi_softc *sc, uint32_t rx_ctrl)
2773 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_CTRL, 0);
2777 for (i = 0; i < NRETRY; ++i) {
2780 status = CSR_READ_4(sc, rx_ctrl + BWI_RX32_STATUS);
2781 if (__SHIFTOUT(status, BWI_RX32_STATUS_STATE_MASK) ==
2782 BWI_RX32_STATUS_STATE_DISABLED)
2788 device_printf(sc->sc_dev, "reset rx ring timedout\n");
2792 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_RINGINFO, 0);
2796 bwi_free_txstats32(struct bwi_softc *sc)
2798 bwi_reset_rx_ring32(sc, sc->sc_txstats->stats_ctrl_base);
2802 bwi_free_rx_ring32(struct bwi_softc *sc)
2804 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2805 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2808 bwi_reset_rx_ring32(sc, rd->rdata_txrx_ctrl);
2810 for (i = 0; i < BWI_RX_NDESC; ++i) {
2811 struct bwi_rxbuf *rb = &rbd->rbd_buf[i];
2813 if (rb->rb_mbuf != NULL) {
2814 bus_dmamap_unload(sc->sc_buf_dtag, rb->rb_dmap);
2815 m_freem(rb->rb_mbuf);
2822 bwi_free_tx_ring32(struct bwi_softc *sc, int ring_idx)
2824 struct bwi_ring_data *rd;
2825 struct bwi_txbuf_data *tbd;
2826 struct ifnet *ifp = sc->sc_ifp;
2827 uint32_t state, val;
2830 KASSERT(ring_idx < BWI_TX_NRING, ("ring_idx %d", ring_idx));
2831 rd = &sc->sc_tx_rdata[ring_idx];
2832 tbd = &sc->sc_tx_bdata[ring_idx];
2836 for (i = 0; i < NRETRY; ++i) {
2837 val = CSR_READ_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_STATUS);
2838 state = __SHIFTOUT(val, BWI_TX32_STATUS_STATE_MASK);
2839 if (state == BWI_TX32_STATUS_STATE_DISABLED ||
2840 state == BWI_TX32_STATUS_STATE_IDLE ||
2841 state == BWI_TX32_STATUS_STATE_STOPPED)
2847 if_printf(ifp, "%s: wait for TX ring(%d) stable timed out\n",
2848 __func__, ring_idx);
2851 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_CTRL, 0);
2852 for (i = 0; i < NRETRY; ++i) {
2853 val = CSR_READ_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_STATUS);
2854 state = __SHIFTOUT(val, BWI_TX32_STATUS_STATE_MASK);
2855 if (state == BWI_TX32_STATUS_STATE_DISABLED)
2861 if_printf(ifp, "%s: reset TX ring (%d) timed out\n",
2862 __func__, ring_idx);
2868 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_RINGINFO, 0);
2870 for (i = 0; i < BWI_TX_NDESC; ++i) {
2871 struct bwi_txbuf *tb = &tbd->tbd_buf[i];
2873 if (tb->tb_mbuf != NULL) {
2874 bus_dmamap_unload(sc->sc_buf_dtag, tb->tb_dmap);
2875 m_freem(tb->tb_mbuf);
2878 if (tb->tb_ni != NULL) {
2879 ieee80211_free_node(tb->tb_ni);
2886 bwi_free_txstats64(struct bwi_softc *sc)
2892 bwi_free_rx_ring64(struct bwi_softc *sc)
2898 bwi_free_tx_ring64(struct bwi_softc *sc, int ring_idx)
2903 /* XXX does not belong here */
2904 #define IEEE80211_OFDM_PLCP_RATE_MASK __BITS(3, 0)
2905 #define IEEE80211_OFDM_PLCP_LEN_MASK __BITS(16, 5)
2907 static __inline void
2908 bwi_ofdm_plcp_header(uint32_t *plcp0, int pkt_len, uint8_t rate)
2912 plcp = __SHIFTIN(ieee80211_rate2plcp(rate, IEEE80211_T_OFDM),
2913 IEEE80211_OFDM_PLCP_RATE_MASK) |
2914 __SHIFTIN(pkt_len, IEEE80211_OFDM_PLCP_LEN_MASK);
2915 *plcp0 = htole32(plcp);
2918 static __inline void
2919 bwi_ds_plcp_header(struct ieee80211_ds_plcp_hdr *plcp, int pkt_len,
2922 int len, service, pkt_bitlen;
2924 pkt_bitlen = pkt_len * NBBY;
2925 len = howmany(pkt_bitlen * 2, rate);
2927 service = IEEE80211_PLCP_SERVICE_LOCKED;
2928 if (rate == (11 * 2)) {
2932 * PLCP service field needs to be adjusted,
2933 * if TX rate is 11Mbytes/s
2935 pkt_bitlen1 = len * 11;
2936 if (pkt_bitlen1 - pkt_bitlen >= NBBY)
2937 service |= IEEE80211_PLCP_SERVICE_LENEXT7;
2940 plcp->i_signal = ieee80211_rate2plcp(rate, IEEE80211_T_CCK);
2941 plcp->i_service = service;
2942 plcp->i_length = htole16(len);
2943 /* NOTE: do NOT touch i_crc */
2946 static __inline void
2947 bwi_plcp_header(const struct ieee80211_rate_table *rt,
2948 void *plcp, int pkt_len, uint8_t rate)
2950 enum ieee80211_phytype modtype;
2953 * Assume caller has zeroed 'plcp'
2955 modtype = ieee80211_rate2phytype(rt, rate);
2956 if (modtype == IEEE80211_T_OFDM)
2957 bwi_ofdm_plcp_header(plcp, pkt_len, rate);
2958 else if (modtype == IEEE80211_T_DS)
2959 bwi_ds_plcp_header(plcp, pkt_len, rate);
2961 panic("unsupport modulation type %u\n", modtype);
2965 bwi_encap(struct bwi_softc *sc, int idx, struct mbuf *m,
2966 struct ieee80211_node *ni)
2968 struct ieee80211vap *vap = ni->ni_vap;
2969 struct ifnet *ifp = sc->sc_ifp;
2970 struct ieee80211com *ic = ifp->if_l2com;
2971 struct bwi_ring_data *rd = &sc->sc_tx_rdata[BWI_TX_DATA_RING];
2972 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
2973 struct bwi_txbuf *tb = &tbd->tbd_buf[idx];
2974 struct bwi_mac *mac;
2975 struct bwi_txbuf_hdr *hdr;
2976 struct ieee80211_frame *wh;
2977 const struct ieee80211_txparam *tp;
2978 uint8_t rate, rate_fb;
2982 int type, ismcast, pkt_len, error, rix;
2988 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
2989 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
2990 mac = (struct bwi_mac *)sc->sc_cur_regwin;
2992 wh = mtod(m, struct ieee80211_frame *);
2993 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2994 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2996 /* Get 802.11 frame len before prepending TX header */
2997 pkt_len = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3002 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
3003 if (type != IEEE80211_FC0_TYPE_DATA || (m->m_flags & M_EAPOL)) {
3004 rate = rate_fb = tp->mgmtrate;
3005 } else if (ismcast) {
3006 rate = rate_fb = tp->mcastrate;
3007 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
3008 rate = rate_fb = tp->ucastrate;
3010 rix = ieee80211_amrr_choose(ni, &BWI_NODE(ni)->amn);
3011 rate = ni->ni_txrate;
3014 rate_fb = ni->ni_rates.rs_rates[rix-1] &
3020 tb->tb_rate[0] = rate;
3021 tb->tb_rate[1] = rate_fb;
3022 sc->sc_tx_rate = rate;
3027 if (ieee80211_radiotap_active_vap(vap)) {
3028 sc->sc_tx_th.wt_flags = 0;
3029 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3030 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3031 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_DS &&
3032 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
3034 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3036 sc->sc_tx_th.wt_rate = rate;
3038 ieee80211_radiotap_tx(vap, m);
3042 * Setup the embedded TX header
3044 M_PREPEND(m, sizeof(*hdr), M_DONTWAIT);
3046 if_printf(ifp, "%s: prepend TX header failed\n", __func__);
3049 hdr = mtod(m, struct bwi_txbuf_hdr *);
3051 bzero(hdr, sizeof(*hdr));
3053 bcopy(wh->i_fc, hdr->txh_fc, sizeof(hdr->txh_fc));
3054 bcopy(wh->i_addr1, hdr->txh_addr1, sizeof(hdr->txh_addr1));
3059 dur = ieee80211_ack_duration(sc->sc_rates, rate,
3060 ic->ic_flags & ~IEEE80211_F_SHPREAMBLE);
3062 hdr->txh_fb_duration = htole16(dur);
3065 hdr->txh_id = __SHIFTIN(BWI_TX_DATA_RING, BWI_TXH_ID_RING_MASK) |
3066 __SHIFTIN(idx, BWI_TXH_ID_IDX_MASK);
3068 bwi_plcp_header(sc->sc_rates, hdr->txh_plcp, pkt_len, rate);
3069 bwi_plcp_header(sc->sc_rates, hdr->txh_fb_plcp, pkt_len, rate_fb);
3071 phy_ctrl = __SHIFTIN(mac->mac_rf.rf_ant_mode,
3072 BWI_TXH_PHY_C_ANTMODE_MASK);
3073 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM)
3074 phy_ctrl |= BWI_TXH_PHY_C_OFDM;
3075 else if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && rate != (2 * 1))
3076 phy_ctrl |= BWI_TXH_PHY_C_SHPREAMBLE;
3078 mac_ctrl = BWI_TXH_MAC_C_HWSEQ | BWI_TXH_MAC_C_FIRST_FRAG;
3080 mac_ctrl |= BWI_TXH_MAC_C_ACK;
3081 if (ieee80211_rate2phytype(sc->sc_rates, rate_fb) == IEEE80211_T_OFDM)
3082 mac_ctrl |= BWI_TXH_MAC_C_FB_OFDM;
3084 hdr->txh_mac_ctrl = htole32(mac_ctrl);
3085 hdr->txh_phy_ctrl = htole16(phy_ctrl);
3087 /* Catch any further usage */
3092 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3093 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT);
3094 if (error && error != EFBIG) {
3095 if_printf(ifp, "%s: can't load TX buffer (1) %d\n",
3100 if (error) { /* error == EFBIG */
3103 m_new = m_defrag(m, M_DONTWAIT);
3104 if (m_new == NULL) {
3105 if_printf(ifp, "%s: can't defrag TX buffer\n",
3113 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3114 bwi_dma_buf_addr, &paddr,
3117 if_printf(ifp, "%s: can't load TX buffer (2) %d\n",
3124 bus_dmamap_sync(sc->sc_buf_dtag, tb->tb_dmap, BUS_DMASYNC_PREWRITE);
3130 p = mtod(m, const uint8_t *);
3131 for (i = 0; i < m->m_pkthdr.len; ++i) {
3132 if (i != 0 && i % 8 == 0)
3134 printf("%02x ", p[i]);
3138 DPRINTF(sc, BWI_DBG_TX, "idx %d, pkt_len %d, buflen %d\n",
3139 idx, pkt_len, m->m_pkthdr.len);
3141 /* Setup TX descriptor */
3142 sc->sc_setup_txdesc(sc, rd, idx, paddr, m->m_pkthdr.len);
3143 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap,
3144 BUS_DMASYNC_PREWRITE);
3147 sc->sc_start_tx(sc, rd->rdata_txrx_ctrl, idx);
3156 bwi_encap_raw(struct bwi_softc *sc, int idx, struct mbuf *m,
3157 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
3159 struct ifnet *ifp = sc->sc_ifp;
3160 struct ieee80211vap *vap = ni->ni_vap;
3161 struct ieee80211com *ic = ni->ni_ic;
3162 struct bwi_ring_data *rd = &sc->sc_tx_rdata[BWI_TX_DATA_RING];
3163 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
3164 struct bwi_txbuf *tb = &tbd->tbd_buf[idx];
3165 struct bwi_mac *mac;
3166 struct bwi_txbuf_hdr *hdr;
3167 struct ieee80211_frame *wh;
3168 uint8_t rate, rate_fb;
3172 int ismcast, pkt_len, error;
3174 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3175 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3176 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3178 wh = mtod(m, struct ieee80211_frame *);
3179 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
3181 /* Get 802.11 frame len before prepending TX header */
3182 pkt_len = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3187 rate = params->ibp_rate0;
3188 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3189 /* XXX fall back to mcast/mgmt rate? */
3193 if (params->ibp_try1 != 0) {
3194 rate_fb = params->ibp_rate1;
3195 if (!ieee80211_isratevalid(ic->ic_rt, rate_fb)) {
3196 /* XXX fall back to rate0? */
3202 tb->tb_rate[0] = rate;
3203 tb->tb_rate[1] = rate_fb;
3204 sc->sc_tx_rate = rate;
3209 if (ieee80211_radiotap_active_vap(vap)) {
3210 sc->sc_tx_th.wt_flags = 0;
3211 /* XXX IEEE80211_BPF_CRYPTO */
3212 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3213 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3214 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
3215 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3216 sc->sc_tx_th.wt_rate = rate;
3218 ieee80211_radiotap_tx(vap, m);
3222 * Setup the embedded TX header
3224 M_PREPEND(m, sizeof(*hdr), M_DONTWAIT);
3226 if_printf(ifp, "%s: prepend TX header failed\n", __func__);
3229 hdr = mtod(m, struct bwi_txbuf_hdr *);
3231 bzero(hdr, sizeof(*hdr));
3233 bcopy(wh->i_fc, hdr->txh_fc, sizeof(hdr->txh_fc));
3234 bcopy(wh->i_addr1, hdr->txh_addr1, sizeof(hdr->txh_addr1));
3236 mac_ctrl = BWI_TXH_MAC_C_HWSEQ | BWI_TXH_MAC_C_FIRST_FRAG;
3237 if (!ismcast && (params->ibp_flags & IEEE80211_BPF_NOACK) == 0) {
3240 dur = ieee80211_ack_duration(sc->sc_rates, rate_fb, 0);
3242 hdr->txh_fb_duration = htole16(dur);
3243 mac_ctrl |= BWI_TXH_MAC_C_ACK;
3246 hdr->txh_id = __SHIFTIN(BWI_TX_DATA_RING, BWI_TXH_ID_RING_MASK) |
3247 __SHIFTIN(idx, BWI_TXH_ID_IDX_MASK);
3249 bwi_plcp_header(sc->sc_rates, hdr->txh_plcp, pkt_len, rate);
3250 bwi_plcp_header(sc->sc_rates, hdr->txh_fb_plcp, pkt_len, rate_fb);
3252 phy_ctrl = __SHIFTIN(mac->mac_rf.rf_ant_mode,
3253 BWI_TXH_PHY_C_ANTMODE_MASK);
3254 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM) {
3255 phy_ctrl |= BWI_TXH_PHY_C_OFDM;
3256 mac_ctrl |= BWI_TXH_MAC_C_FB_OFDM;
3257 } else if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
3258 phy_ctrl |= BWI_TXH_PHY_C_SHPREAMBLE;
3260 hdr->txh_mac_ctrl = htole32(mac_ctrl);
3261 hdr->txh_phy_ctrl = htole16(phy_ctrl);
3263 /* Catch any further usage */
3268 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3269 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT);
3273 if (error != EFBIG) {
3274 if_printf(ifp, "%s: can't load TX buffer (1) %d\n",
3278 m_new = m_defrag(m, M_DONTWAIT);
3279 if (m_new == NULL) {
3280 if_printf(ifp, "%s: can't defrag TX buffer\n",
3286 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3287 bwi_dma_buf_addr, &paddr,
3290 if_printf(ifp, "%s: can't load TX buffer (2) %d\n",
3296 bus_dmamap_sync(sc->sc_buf_dtag, tb->tb_dmap, BUS_DMASYNC_PREWRITE);
3301 DPRINTF(sc, BWI_DBG_TX, "idx %d, pkt_len %d, buflen %d\n",
3302 idx, pkt_len, m->m_pkthdr.len);
3304 /* Setup TX descriptor */
3305 sc->sc_setup_txdesc(sc, rd, idx, paddr, m->m_pkthdr.len);
3306 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap,
3307 BUS_DMASYNC_PREWRITE);
3310 sc->sc_start_tx(sc, rd->rdata_txrx_ctrl, idx);
3318 bwi_start_tx32(struct bwi_softc *sc, uint32_t tx_ctrl, int idx)
3320 idx = (idx + 1) % BWI_TX_NDESC;
3321 CSR_WRITE_4(sc, tx_ctrl + BWI_TX32_INDEX,
3322 idx * sizeof(struct bwi_desc32));
3326 bwi_start_tx64(struct bwi_softc *sc, uint32_t tx_ctrl, int idx)
3332 bwi_txeof_status32(struct bwi_softc *sc)
3334 struct ifnet *ifp = sc->sc_ifp;
3335 uint32_t val, ctrl_base;
3338 ctrl_base = sc->sc_txstats->stats_ctrl_base;
3340 val = CSR_READ_4(sc, ctrl_base + BWI_RX32_STATUS);
3341 end_idx = __SHIFTOUT(val, BWI_RX32_STATUS_INDEX_MASK) /
3342 sizeof(struct bwi_desc32);
3344 bwi_txeof_status(sc, end_idx);
3346 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_INDEX,
3347 end_idx * sizeof(struct bwi_desc32));
3349 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
3354 bwi_txeof_status64(struct bwi_softc *sc)
3360 _bwi_txeof(struct bwi_softc *sc, uint16_t tx_id, int acked, int data_txcnt)
3362 struct ifnet *ifp = sc->sc_ifp;
3363 struct bwi_txbuf_data *tbd;
3364 struct bwi_txbuf *tb;
3365 int ring_idx, buf_idx;
3366 struct ieee80211_node *ni;
3369 if_printf(ifp, "%s: zero tx id\n", __func__);
3373 ring_idx = __SHIFTOUT(tx_id, BWI_TXH_ID_RING_MASK);
3374 buf_idx = __SHIFTOUT(tx_id, BWI_TXH_ID_IDX_MASK);
3376 KASSERT(ring_idx == BWI_TX_DATA_RING, ("ring_idx %d", ring_idx));
3377 KASSERT(buf_idx < BWI_TX_NDESC, ("buf_idx %d", buf_idx));
3379 tbd = &sc->sc_tx_bdata[ring_idx];
3380 KASSERT(tbd->tbd_used > 0, ("tbd_used %d", tbd->tbd_used));
3383 tb = &tbd->tbd_buf[buf_idx];
3384 DPRINTF(sc, BWI_DBG_TXEOF, "txeof idx %d, "
3385 "acked %d, data_txcnt %d, ni %p\n",
3386 buf_idx, acked, data_txcnt, tb->tb_ni);
3388 bus_dmamap_unload(sc->sc_buf_dtag, tb->tb_dmap);
3391 if (tb->tb_ni != NULL) {
3392 struct bwi_node *bn = (struct bwi_node *) tb->tb_ni;
3393 const struct bwi_txbuf_hdr *hdr =
3394 mtod(tb->tb_mbuf, const struct bwi_txbuf_hdr *);
3396 /* NB: update rate control only for unicast frames */
3397 if (hdr->txh_mac_ctrl & htole32(BWI_TXH_MAC_C_ACK)) {
3399 * Feed back 'acked and data_txcnt'. Note that the
3400 * generic AMRR code only understands one tx rate
3401 * and the estimator doesn't handle real retry counts
3402 * well so to avoid over-aggressive downshifting we
3403 * treat any number of retries as "1".
3405 ieee80211_amrr_tx_complete(&bn->amn, acked,
3410 * Do any tx complete callback. Note this must
3411 * be done before releasing the node reference.
3413 if (tb->tb_mbuf->m_flags & M_TXCB)
3414 ieee80211_process_callback(ni, tb->tb_mbuf, !acked);
3416 ieee80211_free_node(tb->tb_ni);
3419 m_freem(tb->tb_mbuf);
3422 if (tbd->tbd_used == 0)
3425 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3429 bwi_txeof_status(struct bwi_softc *sc, int end_idx)
3431 struct bwi_txstats_data *st = sc->sc_txstats;
3434 bus_dmamap_sync(st->stats_dtag, st->stats_dmap, BUS_DMASYNC_POSTREAD);
3436 idx = st->stats_idx;
3437 while (idx != end_idx) {
3438 const struct bwi_txstats *stats = &st->stats[idx];
3440 if ((stats->txs_flags & BWI_TXS_F_PENDING) == 0) {
3443 data_txcnt = __SHIFTOUT(stats->txs_txcnt,
3444 BWI_TXS_TXCNT_DATA);
3445 _bwi_txeof(sc, le16toh(stats->txs_id),
3446 stats->txs_flags & BWI_TXS_F_ACKED,
3449 idx = (idx + 1) % BWI_TXSTATS_NDESC;
3451 st->stats_idx = idx;
3455 bwi_txeof(struct bwi_softc *sc)
3457 struct ifnet *ifp = sc->sc_ifp;
3460 uint32_t tx_status0, tx_status1;
3464 tx_status0 = CSR_READ_4(sc, BWI_TXSTATUS0);
3465 if ((tx_status0 & BWI_TXSTATUS0_VALID) == 0)
3467 tx_status1 = CSR_READ_4(sc, BWI_TXSTATUS1);
3469 tx_id = __SHIFTOUT(tx_status0, BWI_TXSTATUS0_TXID_MASK);
3470 data_txcnt = __SHIFTOUT(tx_status0,
3471 BWI_TXSTATUS0_DATA_TXCNT_MASK);
3473 if (tx_status0 & (BWI_TXSTATUS0_AMPDU | BWI_TXSTATUS0_PENDING))
3476 _bwi_txeof(sc, le16toh(tx_id), tx_status0 & BWI_TXSTATUS0_ACKED,
3480 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
3485 bwi_bbp_power_on(struct bwi_softc *sc, enum bwi_clock_mode clk_mode)
3487 bwi_power_on(sc, 1);
3488 return bwi_set_clock_mode(sc, clk_mode);
3492 bwi_bbp_power_off(struct bwi_softc *sc)
3494 bwi_set_clock_mode(sc, BWI_CLOCK_MODE_SLOW);
3495 bwi_power_off(sc, 1);
3499 bwi_get_pwron_delay(struct bwi_softc *sc)
3501 struct bwi_regwin *com, *old;
3502 struct bwi_clock_freq freq;
3506 com = &sc->sc_com_regwin;
3507 KASSERT(BWI_REGWIN_EXIST(com), ("no regwin"));
3509 if ((sc->sc_cap & BWI_CAP_CLKMODE) == 0)
3512 error = bwi_regwin_switch(sc, com, &old);
3516 bwi_get_clock_freq(sc, &freq);
3518 val = CSR_READ_4(sc, BWI_PLL_ON_DELAY);
3519 sc->sc_pwron_delay = howmany((val + 2) * 1000000, freq.clkfreq_min);
3520 DPRINTF(sc, BWI_DBG_ATTACH, "power on delay %u\n", sc->sc_pwron_delay);
3522 return bwi_regwin_switch(sc, old, NULL);
3526 bwi_bus_attach(struct bwi_softc *sc)
3528 struct bwi_regwin *bus, *old;
3531 bus = &sc->sc_bus_regwin;
3533 error = bwi_regwin_switch(sc, bus, &old);
3537 if (!bwi_regwin_is_enabled(sc, bus))
3538 bwi_regwin_enable(sc, bus, 0);
3540 /* Disable interripts */
3541 CSR_WRITE_4(sc, BWI_INTRVEC, 0);
3543 return bwi_regwin_switch(sc, old, NULL);
3547 bwi_regwin_name(const struct bwi_regwin *rw)
3549 switch (rw->rw_type) {
3550 case BWI_REGWIN_T_COM:
3552 case BWI_REGWIN_T_BUSPCI:
3554 case BWI_REGWIN_T_MAC:
3556 case BWI_REGWIN_T_BUSPCIE:
3559 panic("unknown regwin type 0x%04x\n", rw->rw_type);
3564 bwi_regwin_disable_bits(struct bwi_softc *sc)
3568 /* XXX cache this */
3569 busrev = __SHIFTOUT(CSR_READ_4(sc, BWI_ID_LO), BWI_ID_LO_BUSREV_MASK);
3570 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT | BWI_DBG_MISC,
3571 "bus rev %u\n", busrev);
3573 if (busrev == BWI_BUSREV_0)
3574 return BWI_STATE_LO_DISABLE1;
3575 else if (busrev == BWI_BUSREV_1)
3576 return BWI_STATE_LO_DISABLE2;
3578 return (BWI_STATE_LO_DISABLE1 | BWI_STATE_LO_DISABLE2);
3582 bwi_regwin_is_enabled(struct bwi_softc *sc, struct bwi_regwin *rw)
3584 uint32_t val, disable_bits;
3586 disable_bits = bwi_regwin_disable_bits(sc);
3587 val = CSR_READ_4(sc, BWI_STATE_LO);
3589 if ((val & (BWI_STATE_LO_CLOCK |
3590 BWI_STATE_LO_RESET |
3591 disable_bits)) == BWI_STATE_LO_CLOCK) {
3592 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT, "%s is enabled\n",
3593 bwi_regwin_name(rw));
3596 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT, "%s is disabled\n",
3597 bwi_regwin_name(rw));
3603 bwi_regwin_disable(struct bwi_softc *sc, struct bwi_regwin *rw, uint32_t flags)
3605 uint32_t state_lo, disable_bits;
3608 state_lo = CSR_READ_4(sc, BWI_STATE_LO);
3611 * If current regwin is in 'reset' state, it was already disabled.
3613 if (state_lo & BWI_STATE_LO_RESET) {
3614 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT,
3615 "%s was already disabled\n", bwi_regwin_name(rw));
3619 disable_bits = bwi_regwin_disable_bits(sc);
3622 * Disable normal clock
3624 state_lo = BWI_STATE_LO_CLOCK | disable_bits;
3625 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3628 * Wait until normal clock is disabled
3631 for (i = 0; i < NRETRY; ++i) {
3632 state_lo = CSR_READ_4(sc, BWI_STATE_LO);
3633 if (state_lo & disable_bits)
3638 device_printf(sc->sc_dev, "%s disable clock timeout\n",
3639 bwi_regwin_name(rw));
3642 for (i = 0; i < NRETRY; ++i) {
3645 state_hi = CSR_READ_4(sc, BWI_STATE_HI);
3646 if ((state_hi & BWI_STATE_HI_BUSY) == 0)
3651 device_printf(sc->sc_dev, "%s wait BUSY unset timeout\n",
3652 bwi_regwin_name(rw));
3657 * Reset and disable regwin with gated clock
3659 state_lo = BWI_STATE_LO_RESET | disable_bits |
3660 BWI_STATE_LO_CLOCK | BWI_STATE_LO_GATED_CLOCK |
3661 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3662 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3664 /* Flush pending bus write */
3665 CSR_READ_4(sc, BWI_STATE_LO);
3668 /* Reset and disable regwin */
3669 state_lo = BWI_STATE_LO_RESET | disable_bits |
3670 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3671 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3673 /* Flush pending bus write */
3674 CSR_READ_4(sc, BWI_STATE_LO);
3679 bwi_regwin_enable(struct bwi_softc *sc, struct bwi_regwin *rw, uint32_t flags)
3681 uint32_t state_lo, state_hi, imstate;
3683 bwi_regwin_disable(sc, rw, flags);
3685 /* Reset regwin with gated clock */
3686 state_lo = BWI_STATE_LO_RESET |
3687 BWI_STATE_LO_CLOCK |
3688 BWI_STATE_LO_GATED_CLOCK |
3689 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3690 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3692 /* Flush pending bus write */
3693 CSR_READ_4(sc, BWI_STATE_LO);
3696 state_hi = CSR_READ_4(sc, BWI_STATE_HI);
3697 if (state_hi & BWI_STATE_HI_SERROR)
3698 CSR_WRITE_4(sc, BWI_STATE_HI, 0);
3700 imstate = CSR_READ_4(sc, BWI_IMSTATE);
3701 if (imstate & (BWI_IMSTATE_INBAND_ERR | BWI_IMSTATE_TIMEOUT)) {
3702 imstate &= ~(BWI_IMSTATE_INBAND_ERR | BWI_IMSTATE_TIMEOUT);
3703 CSR_WRITE_4(sc, BWI_IMSTATE, imstate);
3706 /* Enable regwin with gated clock */
3707 state_lo = BWI_STATE_LO_CLOCK |
3708 BWI_STATE_LO_GATED_CLOCK |
3709 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3710 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3712 /* Flush pending bus write */
3713 CSR_READ_4(sc, BWI_STATE_LO);
3716 /* Enable regwin with normal clock */
3717 state_lo = BWI_STATE_LO_CLOCK |
3718 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3719 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3721 /* Flush pending bus write */
3722 CSR_READ_4(sc, BWI_STATE_LO);
3727 bwi_set_bssid(struct bwi_softc *sc, const uint8_t *bssid)
3729 struct ifnet *ifp = sc->sc_ifp;
3730 struct bwi_mac *mac;
3731 struct bwi_myaddr_bssid buf;
3736 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3737 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3738 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3740 bwi_set_addr_filter(sc, BWI_ADDR_FILTER_BSSID, bssid);
3742 bcopy(IF_LLADDR(ifp), buf.myaddr, sizeof(buf.myaddr));
3743 bcopy(bssid, buf.bssid, sizeof(buf.bssid));
3745 n = sizeof(buf) / sizeof(val);
3746 p = (const uint8_t *)&buf;
3747 for (i = 0; i < n; ++i) {
3751 for (j = 0; j < sizeof(val); ++j)
3752 val |= ((uint32_t)(*p++)) << (j * 8);
3754 TMPLT_WRITE_4(mac, 0x20 + (i * sizeof(val)), val);
3759 bwi_updateslot(struct ifnet *ifp)
3761 struct bwi_softc *sc = ifp->if_softc;
3762 struct ieee80211com *ic = ifp->if_l2com;
3763 struct bwi_mac *mac;
3766 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3767 DPRINTF(sc, BWI_DBG_80211, "%s\n", __func__);
3769 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3770 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3771 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3773 bwi_mac_updateslot(mac, (ic->ic_flags & IEEE80211_F_SHSLOT));
3779 bwi_calibrate(void *xsc)
3781 struct bwi_softc *sc = xsc;
3783 struct ifnet *ifp = sc->sc_ifp;
3784 struct ieee80211com *ic = ifp->if_l2com;
3786 struct bwi_mac *mac;
3788 BWI_ASSERT_LOCKED(sc);
3790 KASSERT(ic->ic_opmode != IEEE80211_M_MONITOR,
3791 ("opmode %d", ic->ic_opmode));
3793 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3794 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3795 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3797 bwi_mac_calibrate_txpower(mac, sc->sc_txpwrcb_type);
3798 sc->sc_txpwrcb_type = BWI_TXPWR_CALIB;
3800 /* XXX 15 seconds */
3801 callout_reset(&sc->sc_calib_ch, hz * 15, bwi_calibrate, sc);
3805 bwi_calc_rssi(struct bwi_softc *sc, const struct bwi_rxbuf_hdr *hdr)
3807 struct bwi_mac *mac;
3809 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3810 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3811 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3813 return bwi_rf_calc_rssi(mac, hdr);
3817 bwi_calc_noise(struct bwi_softc *sc)
3819 struct bwi_mac *mac;
3821 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3822 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3823 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3825 return bwi_rf_calc_noise(mac);
3828 static __inline uint8_t
3829 bwi_ofdm_plcp2rate(const uint32_t *plcp0)
3834 plcp = le32toh(*plcp0);
3835 plcp_rate = __SHIFTOUT(plcp, IEEE80211_OFDM_PLCP_RATE_MASK);
3836 return ieee80211_plcp2rate(plcp_rate, IEEE80211_T_OFDM);
3839 static __inline uint8_t
3840 bwi_ds_plcp2rate(const struct ieee80211_ds_plcp_hdr *hdr)
3842 return ieee80211_plcp2rate(hdr->i_signal, IEEE80211_T_DS);
3846 bwi_rx_radiotap(struct bwi_softc *sc, struct mbuf *m,
3847 struct bwi_rxbuf_hdr *hdr, const void *plcp, int rate, int rssi, int noise)
3849 const struct ieee80211_frame_min *wh;
3851 sc->sc_rx_th.wr_flags = IEEE80211_RADIOTAP_F_FCS;
3852 if (htole16(hdr->rxh_flags1) & BWI_RXH_F1_SHPREAMBLE)
3853 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3855 wh = mtod(m, const struct ieee80211_frame_min *);
3856 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3857 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_WEP;
3859 sc->sc_rx_th.wr_tsf = hdr->rxh_tsf; /* No endian convertion */
3860 sc->sc_rx_th.wr_rate = rate;
3861 sc->sc_rx_th.wr_antsignal = rssi;
3862 sc->sc_rx_th.wr_antnoise = noise;
3866 bwi_led_attach(struct bwi_softc *sc)
3868 const uint8_t *led_act = NULL;
3869 uint16_t gpio, val[BWI_LED_MAX];
3872 #define N(arr) (int)(sizeof(arr) / sizeof(arr[0]))
3874 for (i = 0; i < N(bwi_vendor_led_act); ++i) {
3875 if (sc->sc_pci_subvid == bwi_vendor_led_act[i].vid) {
3876 led_act = bwi_vendor_led_act[i].led_act;
3880 if (led_act == NULL)
3881 led_act = bwi_default_led_act;
3885 gpio = bwi_read_sprom(sc, BWI_SPROM_GPIO01);
3886 val[0] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_0);
3887 val[1] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_1);
3889 gpio = bwi_read_sprom(sc, BWI_SPROM_GPIO23);
3890 val[2] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_2);
3891 val[3] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_3);
3893 for (i = 0; i < BWI_LED_MAX; ++i) {
3894 struct bwi_led *led = &sc->sc_leds[i];
3896 if (val[i] == 0xff) {
3897 led->l_act = led_act[i];
3899 if (val[i] & BWI_LED_ACT_LOW)
3900 led->l_flags |= BWI_LED_F_ACTLOW;
3901 led->l_act = __SHIFTOUT(val[i], BWI_LED_ACT_MASK);
3903 led->l_mask = (1 << i);
3905 if (led->l_act == BWI_LED_ACT_BLINK_SLOW ||
3906 led->l_act == BWI_LED_ACT_BLINK_POLL ||
3907 led->l_act == BWI_LED_ACT_BLINK) {
3908 led->l_flags |= BWI_LED_F_BLINK;
3909 if (led->l_act == BWI_LED_ACT_BLINK_POLL)
3910 led->l_flags |= BWI_LED_F_POLLABLE;
3911 else if (led->l_act == BWI_LED_ACT_BLINK_SLOW)
3912 led->l_flags |= BWI_LED_F_SLOW;
3914 if (sc->sc_blink_led == NULL) {
3915 sc->sc_blink_led = led;
3916 if (led->l_flags & BWI_LED_F_SLOW)
3917 BWI_LED_SLOWDOWN(sc->sc_led_idle);
3921 DPRINTF(sc, BWI_DBG_LED | BWI_DBG_ATTACH,
3922 "%dth led, act %d, lowact %d\n", i,
3923 led->l_act, led->l_flags & BWI_LED_F_ACTLOW);
3925 callout_init(&sc->sc_led_blink_ch, CALLOUT_MPSAFE);
3928 static __inline uint16_t
3929 bwi_led_onoff(const struct bwi_led *led, uint16_t val, int on)
3931 if (led->l_flags & BWI_LED_F_ACTLOW)
3936 val &= ~led->l_mask;
3941 bwi_led_newstate(struct bwi_softc *sc, enum ieee80211_state nstate)
3943 struct ifnet *ifp = sc->sc_ifp;
3944 struct ieee80211com *ic = ifp->if_l2com;
3948 if (nstate == IEEE80211_S_INIT) {
3949 callout_stop(&sc->sc_led_blink_ch);
3950 sc->sc_led_blinking = 0;
3953 if ((ic->ic_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
3956 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL);
3957 for (i = 0; i < BWI_LED_MAX; ++i) {
3958 struct bwi_led *led = &sc->sc_leds[i];
3961 if (led->l_act == BWI_LED_ACT_UNKN ||
3962 led->l_act == BWI_LED_ACT_NULL)
3965 if ((led->l_flags & BWI_LED_F_BLINK) &&
3966 nstate != IEEE80211_S_INIT)
3969 switch (led->l_act) {
3970 case BWI_LED_ACT_ON: /* Always on */
3973 case BWI_LED_ACT_OFF: /* Always off */
3974 case BWI_LED_ACT_5GHZ: /* TODO: 11A */
3980 case IEEE80211_S_INIT:
3983 case IEEE80211_S_RUN:
3984 if (led->l_act == BWI_LED_ACT_11G &&
3985 ic->ic_curmode != IEEE80211_MODE_11G)
3989 if (led->l_act == BWI_LED_ACT_ASSOC)
3996 val = bwi_led_onoff(led, val, on);
3998 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val);
4001 bwi_led_event(struct bwi_softc *sc, int event)
4003 struct bwi_led *led = sc->sc_blink_led;
4006 if (event == BWI_LED_EVENT_POLL) {
4007 if ((led->l_flags & BWI_LED_F_POLLABLE) == 0)
4009 if (ticks - sc->sc_led_ticks < sc->sc_led_idle)
4013 sc->sc_led_ticks = ticks;
4014 if (sc->sc_led_blinking)
4018 case BWI_LED_EVENT_RX:
4019 rate = sc->sc_rx_rate;
4021 case BWI_LED_EVENT_TX:
4022 rate = sc->sc_tx_rate;
4024 case BWI_LED_EVENT_POLL:
4028 panic("unknown LED event %d\n", event);
4031 bwi_led_blink_start(sc, bwi_led_duration[rate].on_dur,
4032 bwi_led_duration[rate].off_dur);
4036 bwi_led_blink_start(struct bwi_softc *sc, int on_dur, int off_dur)
4038 struct bwi_led *led = sc->sc_blink_led;
4041 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL);
4042 val = bwi_led_onoff(led, val, 1);
4043 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val);
4045 if (led->l_flags & BWI_LED_F_SLOW) {
4046 BWI_LED_SLOWDOWN(on_dur);
4047 BWI_LED_SLOWDOWN(off_dur);
4050 sc->sc_led_blinking = 1;
4051 sc->sc_led_blink_offdur = off_dur;
4053 callout_reset(&sc->sc_led_blink_ch, on_dur, bwi_led_blink_next, sc);
4057 bwi_led_blink_next(void *xsc)
4059 struct bwi_softc *sc = xsc;
4062 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL);
4063 val = bwi_led_onoff(sc->sc_blink_led, val, 0);
4064 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val);
4066 callout_reset(&sc->sc_led_blink_ch, sc->sc_led_blink_offdur,
4067 bwi_led_blink_end, sc);
4071 bwi_led_blink_end(void *xsc)
4073 struct bwi_softc *sc = xsc;
4074 sc->sc_led_blinking = 0;
4078 bwi_restart(void *xsc, int pending)
4080 struct bwi_softc *sc = xsc;
4081 struct ifnet *ifp = sc->sc_ifp;
4083 if_printf(ifp, "%s begin, help!\n", __func__);
4085 bwi_init_statechg(xsc, 0);
4087 bwi_start_locked(ifp);