2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Sepherosa Ziehau <sepherosa@gmail.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $DragonFly: src/sys/dev/netif/bwi/if_bwi.c,v 1.19 2008/02/15 11:15:38 sephe Exp $
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/endian.h>
48 #include <sys/kernel.h>
50 #include <sys/malloc.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
56 #include <sys/systm.h>
57 #include <sys/taskqueue.h>
60 #include <net/if_var.h>
61 #include <net/if_dl.h>
62 #include <net/if_media.h>
63 #include <net/if_types.h>
64 #include <net/if_arp.h>
65 #include <net/ethernet.h>
66 #include <net/if_llc.h>
68 #include <net80211/ieee80211_var.h>
69 #include <net80211/ieee80211_radiotap.h>
70 #include <net80211/ieee80211_regdomain.h>
71 #include <net80211/ieee80211_phy.h>
72 #include <net80211/ieee80211_ratectl.h>
77 #include <netinet/in.h>
78 #include <netinet/if_ether.h>
81 #include <machine/bus.h>
83 #include <dev/pci/pcivar.h>
84 #include <dev/pci/pcireg.h>
86 #include <dev/bwi/bitops.h>
87 #include <dev/bwi/if_bwireg.h>
88 #include <dev/bwi/if_bwivar.h>
89 #include <dev/bwi/bwimac.h>
90 #include <dev/bwi/bwirf.h>
92 struct bwi_clock_freq {
97 struct bwi_myaddr_bssid {
98 uint8_t myaddr[IEEE80211_ADDR_LEN];
99 uint8_t bssid[IEEE80211_ADDR_LEN];
102 static struct ieee80211vap *bwi_vap_create(struct ieee80211com *,
103 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
104 const uint8_t [IEEE80211_ADDR_LEN],
105 const uint8_t [IEEE80211_ADDR_LEN]);
106 static void bwi_vap_delete(struct ieee80211vap *);
107 static void bwi_init(struct bwi_softc *);
108 static void bwi_parent(struct ieee80211com *);
109 static int bwi_transmit(struct ieee80211com *, struct mbuf *);
110 static void bwi_start_locked(struct bwi_softc *);
111 static int bwi_raw_xmit(struct ieee80211_node *, struct mbuf *,
112 const struct ieee80211_bpf_params *);
113 static void bwi_watchdog(void *);
114 static void bwi_scan_start(struct ieee80211com *);
115 static void bwi_getradiocaps(struct ieee80211com *, int, int *,
116 struct ieee80211_channel[]);
117 static void bwi_set_channel(struct ieee80211com *);
118 static void bwi_scan_end(struct ieee80211com *);
119 static int bwi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
120 static void bwi_updateslot(struct ieee80211com *);
121 static int bwi_media_change(struct ifnet *);
123 static void bwi_calibrate(void *);
125 static int bwi_calc_rssi(struct bwi_softc *, const struct bwi_rxbuf_hdr *);
126 static int bwi_calc_noise(struct bwi_softc *);
127 static __inline uint8_t bwi_plcp2rate(uint32_t, enum ieee80211_phytype);
128 static void bwi_rx_radiotap(struct bwi_softc *, struct mbuf *,
129 struct bwi_rxbuf_hdr *, const void *, int, int, int);
131 static void bwi_restart(void *, int);
132 static void bwi_init_statechg(struct bwi_softc *, int);
133 static void bwi_stop(struct bwi_softc *, int);
134 static void bwi_stop_locked(struct bwi_softc *, int);
135 static int bwi_newbuf(struct bwi_softc *, int, int);
136 static int bwi_encap(struct bwi_softc *, int, struct mbuf *,
137 struct ieee80211_node *);
138 static int bwi_encap_raw(struct bwi_softc *, int, struct mbuf *,
139 struct ieee80211_node *,
140 const struct ieee80211_bpf_params *);
142 static void bwi_init_rxdesc_ring32(struct bwi_softc *, uint32_t,
143 bus_addr_t, int, int);
144 static void bwi_reset_rx_ring32(struct bwi_softc *, uint32_t);
146 static int bwi_init_tx_ring32(struct bwi_softc *, int);
147 static int bwi_init_rx_ring32(struct bwi_softc *);
148 static int bwi_init_txstats32(struct bwi_softc *);
149 static void bwi_free_tx_ring32(struct bwi_softc *, int);
150 static void bwi_free_rx_ring32(struct bwi_softc *);
151 static void bwi_free_txstats32(struct bwi_softc *);
152 static void bwi_setup_rx_desc32(struct bwi_softc *, int, bus_addr_t, int);
153 static void bwi_setup_tx_desc32(struct bwi_softc *, struct bwi_ring_data *,
154 int, bus_addr_t, int);
155 static int bwi_rxeof32(struct bwi_softc *);
156 static void bwi_start_tx32(struct bwi_softc *, uint32_t, int);
157 static void bwi_txeof_status32(struct bwi_softc *);
159 static int bwi_init_tx_ring64(struct bwi_softc *, int);
160 static int bwi_init_rx_ring64(struct bwi_softc *);
161 static int bwi_init_txstats64(struct bwi_softc *);
162 static void bwi_free_tx_ring64(struct bwi_softc *, int);
163 static void bwi_free_rx_ring64(struct bwi_softc *);
164 static void bwi_free_txstats64(struct bwi_softc *);
165 static void bwi_setup_rx_desc64(struct bwi_softc *, int, bus_addr_t, int);
166 static void bwi_setup_tx_desc64(struct bwi_softc *, struct bwi_ring_data *,
167 int, bus_addr_t, int);
168 static int bwi_rxeof64(struct bwi_softc *);
169 static void bwi_start_tx64(struct bwi_softc *, uint32_t, int);
170 static void bwi_txeof_status64(struct bwi_softc *);
172 static int bwi_rxeof(struct bwi_softc *, int);
173 static void _bwi_txeof(struct bwi_softc *, uint16_t, int, int);
174 static void bwi_txeof(struct bwi_softc *);
175 static void bwi_txeof_status(struct bwi_softc *, int);
176 static void bwi_enable_intrs(struct bwi_softc *, uint32_t);
177 static void bwi_disable_intrs(struct bwi_softc *, uint32_t);
179 static int bwi_dma_alloc(struct bwi_softc *);
180 static void bwi_dma_free(struct bwi_softc *);
181 static int bwi_dma_ring_alloc(struct bwi_softc *, bus_dma_tag_t,
182 struct bwi_ring_data *, bus_size_t,
184 static int bwi_dma_mbuf_create(struct bwi_softc *);
185 static void bwi_dma_mbuf_destroy(struct bwi_softc *, int, int);
186 static int bwi_dma_txstats_alloc(struct bwi_softc *, uint32_t, bus_size_t);
187 static void bwi_dma_txstats_free(struct bwi_softc *);
188 static void bwi_dma_ring_addr(void *, bus_dma_segment_t *, int, int);
189 static void bwi_dma_buf_addr(void *, bus_dma_segment_t *, int,
192 static void bwi_power_on(struct bwi_softc *, int);
193 static int bwi_power_off(struct bwi_softc *, int);
194 static int bwi_set_clock_mode(struct bwi_softc *, enum bwi_clock_mode);
195 static int bwi_set_clock_delay(struct bwi_softc *);
196 static void bwi_get_clock_freq(struct bwi_softc *, struct bwi_clock_freq *);
197 static int bwi_get_pwron_delay(struct bwi_softc *sc);
198 static void bwi_set_addr_filter(struct bwi_softc *, uint16_t,
200 static void bwi_set_bssid(struct bwi_softc *, const uint8_t *);
202 static void bwi_get_card_flags(struct bwi_softc *);
203 static void bwi_get_eaddr(struct bwi_softc *, uint16_t, uint8_t *);
205 static int bwi_bus_attach(struct bwi_softc *);
206 static int bwi_bbp_attach(struct bwi_softc *);
207 static int bwi_bbp_power_on(struct bwi_softc *, enum bwi_clock_mode);
208 static void bwi_bbp_power_off(struct bwi_softc *);
210 static const char *bwi_regwin_name(const struct bwi_regwin *);
211 static uint32_t bwi_regwin_disable_bits(struct bwi_softc *);
212 static void bwi_regwin_info(struct bwi_softc *, uint16_t *, uint8_t *);
213 static int bwi_regwin_select(struct bwi_softc *, int);
215 static void bwi_led_attach(struct bwi_softc *);
216 static void bwi_led_newstate(struct bwi_softc *, enum ieee80211_state);
217 static void bwi_led_event(struct bwi_softc *, int);
218 static void bwi_led_blink_start(struct bwi_softc *, int, int);
219 static void bwi_led_blink_next(void *);
220 static void bwi_led_blink_end(void *);
222 static const struct {
226 } bwi_bbpid_map[] = {
227 { 0x4301, 0x4301, 0x4301 },
228 { 0x4305, 0x4307, 0x4307 },
229 { 0x4402, 0x4403, 0x4402 },
230 { 0x4610, 0x4615, 0x4610 },
231 { 0x4710, 0x4715, 0x4710 },
232 { 0x4720, 0x4725, 0x4309 }
235 static const struct {
238 } bwi_regwin_count[] = {
251 #define CLKSRC(src) \
252 [BWI_CLKSRC_ ## src] = { \
253 .freq_min = BWI_CLKSRC_ ##src## _FMIN, \
254 .freq_max = BWI_CLKSRC_ ##src## _FMAX \
257 static const struct {
260 } bwi_clkfreq[BWI_CLKSRC_MAX] = {
268 #define VENDOR_LED_ACT(vendor) \
270 .vid = PCI_VENDOR_##vendor, \
271 .led_act = { BWI_VENDOR_LED_ACT_##vendor } \
274 static const struct {
275 #define PCI_VENDOR_COMPAQ 0x0e11
276 #define PCI_VENDOR_LINKSYS 0x1737
278 uint8_t led_act[BWI_LED_MAX];
279 } bwi_vendor_led_act[] = {
280 VENDOR_LED_ACT(COMPAQ),
281 VENDOR_LED_ACT(LINKSYS)
282 #undef PCI_VENDOR_LINKSYS
283 #undef PCI_VENDOR_COMPAQ
286 static const uint8_t bwi_default_led_act[BWI_LED_MAX] =
287 { BWI_VENDOR_LED_ACT_DEFAULT };
289 #undef VENDOR_LED_ACT
291 static const struct {
294 } bwi_led_duration[109] = {
310 static const uint8_t bwi_chan_2ghz[] =
311 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
314 #ifdef BWI_DEBUG_VERBOSE
315 static uint32_t bwi_debug = BWI_DBG_ATTACH | BWI_DBG_INIT | BWI_DBG_TXPOWER;
317 static uint32_t bwi_debug;
319 TUNABLE_INT("hw.bwi.debug", (int *)&bwi_debug);
320 #endif /* BWI_DEBUG */
322 static const uint8_t bwi_zero_addr[IEEE80211_ADDR_LEN];
325 bwi_read_sprom(struct bwi_softc *sc, uint16_t ofs)
327 return CSR_READ_2(sc, ofs + BWI_SPROM_START);
331 bwi_setup_desc32(struct bwi_softc *sc, struct bwi_desc32 *desc_array,
332 int ndesc, int desc_idx, bus_addr_t paddr, int buf_len,
335 struct bwi_desc32 *desc = &desc_array[desc_idx];
336 uint32_t ctrl, addr, addr_hi, addr_lo;
338 addr_lo = __SHIFTOUT(paddr, BWI_DESC32_A_ADDR_MASK);
339 addr_hi = __SHIFTOUT(paddr, BWI_DESC32_A_FUNC_MASK);
341 addr = __SHIFTIN(addr_lo, BWI_DESC32_A_ADDR_MASK) |
342 __SHIFTIN(BWI_DESC32_A_FUNC_TXRX, BWI_DESC32_A_FUNC_MASK);
344 ctrl = __SHIFTIN(buf_len, BWI_DESC32_C_BUFLEN_MASK) |
345 __SHIFTIN(addr_hi, BWI_DESC32_C_ADDRHI_MASK);
346 if (desc_idx == ndesc - 1)
347 ctrl |= BWI_DESC32_C_EOR;
350 ctrl |= BWI_DESC32_C_FRAME_START |
351 BWI_DESC32_C_FRAME_END |
355 desc->addr = htole32(addr);
356 desc->ctrl = htole32(ctrl);
360 bwi_attach(struct bwi_softc *sc)
362 struct ieee80211com *ic = &sc->sc_ic;
363 device_t dev = sc->sc_dev;
371 * Initialize taskq and various tasks
373 sc->sc_tq = taskqueue_create("bwi_taskq", M_NOWAIT | M_ZERO,
374 taskqueue_thread_enqueue, &sc->sc_tq);
375 taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq",
376 device_get_nameunit(dev));
377 TASK_INIT(&sc->sc_restart_task, 0, bwi_restart, sc);
378 callout_init_mtx(&sc->sc_calib_ch, &sc->sc_mtx, 0);
379 mbufq_init(&sc->sc_snd, ifqmaxlen);
382 * Initialize sysctl variables
384 sc->sc_fw_version = BWI_FW_VERSION3;
385 sc->sc_led_idle = (2350 * hz) / 1000;
386 sc->sc_led_ticks = ticks - sc->sc_led_idle;
387 sc->sc_led_blink = 1;
388 sc->sc_txpwr_calib = 1;
390 sc->sc_debug = bwi_debug;
394 error = bwi_bbp_attach(sc);
398 error = bwi_bbp_power_on(sc, BWI_CLOCK_MODE_FAST);
402 if (BWI_REGWIN_EXIST(&sc->sc_com_regwin)) {
403 error = bwi_set_clock_delay(sc);
407 error = bwi_set_clock_mode(sc, BWI_CLOCK_MODE_FAST);
411 error = bwi_get_pwron_delay(sc);
416 error = bwi_bus_attach(sc);
420 bwi_get_card_flags(sc);
424 for (i = 0; i < sc->sc_nmac; ++i) {
425 struct bwi_regwin *old;
427 mac = &sc->sc_mac[i];
428 error = bwi_regwin_switch(sc, &mac->mac_regwin, &old);
432 error = bwi_mac_lateattach(mac);
436 error = bwi_regwin_switch(sc, old, NULL);
442 * XXX First MAC is known to exist
445 mac = &sc->sc_mac[0];
448 bwi_bbp_power_off(sc);
450 error = bwi_dma_alloc(sc);
454 error = bwi_mac_fw_alloc(mac);
458 callout_init_mtx(&sc->sc_watchdog_timer, &sc->sc_mtx, 0);
461 * Setup ratesets, phytype, channels and get MAC address
463 if (phy->phy_mode == IEEE80211_MODE_11B ||
464 phy->phy_mode == IEEE80211_MODE_11G) {
465 if (phy->phy_mode == IEEE80211_MODE_11B) {
466 ic->ic_phytype = IEEE80211_T_DS;
468 ic->ic_phytype = IEEE80211_T_OFDM;
471 bwi_get_eaddr(sc, BWI_SPROM_11BG_EADDR, ic->ic_macaddr);
472 if (IEEE80211_IS_MULTICAST(ic->ic_macaddr)) {
473 bwi_get_eaddr(sc, BWI_SPROM_11A_EADDR, ic->ic_macaddr);
474 if (IEEE80211_IS_MULTICAST(ic->ic_macaddr)) {
476 "invalid MAC address: %6D\n",
477 ic->ic_macaddr, ":");
480 } else if (phy->phy_mode == IEEE80211_MODE_11A) {
485 panic("unknown phymode %d\n", phy->phy_mode);
489 sc->sc_locale = __SHIFTOUT(bwi_read_sprom(sc, BWI_SPROM_CARD_INFO),
490 BWI_SPROM_CARD_INFO_LOCALE);
491 DPRINTF(sc, BWI_DBG_ATTACH, "locale: %d\n", sc->sc_locale);
496 bwi_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
499 ic->ic_name = device_get_nameunit(dev);
500 ic->ic_caps = IEEE80211_C_STA |
502 IEEE80211_C_SHPREAMBLE |
506 ic->ic_opmode = IEEE80211_M_STA;
507 ieee80211_ifattach(ic);
509 ic->ic_headroom = sizeof(struct bwi_txbuf_hdr);
511 /* override default methods */
512 ic->ic_vap_create = bwi_vap_create;
513 ic->ic_vap_delete = bwi_vap_delete;
514 ic->ic_raw_xmit = bwi_raw_xmit;
515 ic->ic_updateslot = bwi_updateslot;
516 ic->ic_scan_start = bwi_scan_start;
517 ic->ic_scan_end = bwi_scan_end;
518 ic->ic_getradiocaps = bwi_getradiocaps;
519 ic->ic_set_channel = bwi_set_channel;
520 ic->ic_transmit = bwi_transmit;
521 ic->ic_parent = bwi_parent;
523 sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan);
525 ieee80211_radiotap_attach(ic,
526 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
527 BWI_TX_RADIOTAP_PRESENT,
528 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
529 BWI_RX_RADIOTAP_PRESENT);
534 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
535 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
536 "fw_version", CTLFLAG_RD, &sc->sc_fw_version, 0,
538 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
539 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
540 "led_idle", CTLFLAG_RW, &sc->sc_led_idle, 0,
541 "# ticks before LED enters idle state");
542 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
543 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
544 "led_blink", CTLFLAG_RW, &sc->sc_led_blink, 0,
545 "Allow LED to blink");
546 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
547 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
548 "txpwr_calib", CTLFLAG_RW, &sc->sc_txpwr_calib, 0,
549 "Enable software TX power calibration");
551 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
552 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
553 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "Debug flags");
556 ieee80211_announce(ic);
560 BWI_LOCK_DESTROY(sc);
565 bwi_detach(struct bwi_softc *sc)
567 struct ieee80211com *ic = &sc->sc_ic;
571 callout_drain(&sc->sc_led_blink_ch);
572 callout_drain(&sc->sc_calib_ch);
573 callout_drain(&sc->sc_watchdog_timer);
574 ieee80211_ifdetach(ic);
576 for (i = 0; i < sc->sc_nmac; ++i)
577 bwi_mac_detach(&sc->sc_mac[i]);
579 taskqueue_free(sc->sc_tq);
580 mbufq_drain(&sc->sc_snd);
582 BWI_LOCK_DESTROY(sc);
587 static struct ieee80211vap *
588 bwi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
589 enum ieee80211_opmode opmode, int flags,
590 const uint8_t bssid[IEEE80211_ADDR_LEN],
591 const uint8_t mac[IEEE80211_ADDR_LEN])
594 struct ieee80211vap *vap;
596 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
598 bvp = malloc(sizeof(struct bwi_vap), M_80211_VAP, M_WAITOK | M_ZERO);
600 /* enable s/w bmiss handling for sta mode */
601 ieee80211_vap_setup(ic, vap, name, unit, opmode,
602 flags | IEEE80211_CLONE_NOBEACONS, bssid);
604 /* override default methods */
605 bvp->bv_newstate = vap->iv_newstate;
606 vap->iv_newstate = bwi_newstate;
608 vap->iv_update_beacon = bwi_beacon_update;
610 ieee80211_ratectl_init(vap);
613 ieee80211_vap_attach(vap, bwi_media_change, ieee80211_media_status,
615 ic->ic_opmode = opmode;
620 bwi_vap_delete(struct ieee80211vap *vap)
622 struct bwi_vap *bvp = BWI_VAP(vap);
624 ieee80211_ratectl_deinit(vap);
625 ieee80211_vap_detach(vap);
626 free(bvp, M_80211_VAP);
630 bwi_suspend(struct bwi_softc *sc)
636 bwi_resume(struct bwi_softc *sc)
639 if (sc->sc_ic.ic_nrunning > 0)
644 bwi_shutdown(struct bwi_softc *sc)
651 bwi_power_on(struct bwi_softc *sc, int with_pll)
653 uint32_t gpio_in, gpio_out, gpio_en;
656 gpio_in = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_IN, 4);
657 if (gpio_in & BWI_PCIM_GPIO_PWR_ON)
660 gpio_out = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4);
661 gpio_en = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, 4);
663 gpio_out |= BWI_PCIM_GPIO_PWR_ON;
664 gpio_en |= BWI_PCIM_GPIO_PWR_ON;
666 /* Turn off PLL first */
667 gpio_out |= BWI_PCIM_GPIO_PLL_PWR_OFF;
668 gpio_en |= BWI_PCIM_GPIO_PLL_PWR_OFF;
671 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4);
672 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, gpio_en, 4);
677 gpio_out &= ~BWI_PCIM_GPIO_PLL_PWR_OFF;
678 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4);
683 /* Clear "Signaled Target Abort" */
684 status = pci_read_config(sc->sc_dev, PCIR_STATUS, 2);
685 status &= ~PCIM_STATUS_STABORT;
686 pci_write_config(sc->sc_dev, PCIR_STATUS, status, 2);
690 bwi_power_off(struct bwi_softc *sc, int with_pll)
692 uint32_t gpio_out, gpio_en;
694 pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_IN, 4); /* dummy read */
695 gpio_out = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4);
696 gpio_en = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, 4);
698 gpio_out &= ~BWI_PCIM_GPIO_PWR_ON;
699 gpio_en |= BWI_PCIM_GPIO_PWR_ON;
701 gpio_out |= BWI_PCIM_GPIO_PLL_PWR_OFF;
702 gpio_en |= BWI_PCIM_GPIO_PLL_PWR_OFF;
705 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4);
706 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, gpio_en, 4);
711 bwi_regwin_switch(struct bwi_softc *sc, struct bwi_regwin *rw,
712 struct bwi_regwin **old_rw)
719 if (!BWI_REGWIN_EXIST(rw))
722 if (sc->sc_cur_regwin != rw) {
723 error = bwi_regwin_select(sc, rw->rw_id);
725 device_printf(sc->sc_dev, "can't select regwin %d\n",
732 *old_rw = sc->sc_cur_regwin;
733 sc->sc_cur_regwin = rw;
738 bwi_regwin_select(struct bwi_softc *sc, int id)
740 uint32_t win = BWI_PCIM_REGWIN(id);
744 for (i = 0; i < RETRY_MAX; ++i) {
745 pci_write_config(sc->sc_dev, BWI_PCIR_SEL_REGWIN, win, 4);
746 if (pci_read_config(sc->sc_dev, BWI_PCIR_SEL_REGWIN, 4) == win)
756 bwi_regwin_info(struct bwi_softc *sc, uint16_t *type, uint8_t *rev)
760 val = CSR_READ_4(sc, BWI_ID_HI);
761 *type = BWI_ID_HI_REGWIN_TYPE(val);
762 *rev = BWI_ID_HI_REGWIN_REV(val);
764 DPRINTF(sc, BWI_DBG_ATTACH, "regwin: type 0x%03x, rev %d, "
765 "vendor 0x%04x\n", *type, *rev,
766 __SHIFTOUT(val, BWI_ID_HI_REGWIN_VENDOR_MASK));
770 bwi_bbp_attach(struct bwi_softc *sc)
772 uint16_t bbp_id, rw_type;
775 int error, nregwin, i;
778 * Get 0th regwin information
779 * NOTE: 0th regwin should exist
781 error = bwi_regwin_select(sc, 0);
783 device_printf(sc->sc_dev, "can't select regwin 0\n");
786 bwi_regwin_info(sc, &rw_type, &rw_rev);
793 if (rw_type == BWI_REGWIN_T_COM) {
794 info = CSR_READ_4(sc, BWI_INFO);
795 bbp_id = __SHIFTOUT(info, BWI_INFO_BBPID_MASK);
797 BWI_CREATE_REGWIN(&sc->sc_com_regwin, 0, rw_type, rw_rev);
799 sc->sc_cap = CSR_READ_4(sc, BWI_CAPABILITY);
801 for (i = 0; i < nitems(bwi_bbpid_map); ++i) {
802 if (sc->sc_pci_did >= bwi_bbpid_map[i].did_min &&
803 sc->sc_pci_did <= bwi_bbpid_map[i].did_max) {
804 bbp_id = bwi_bbpid_map[i].bbp_id;
809 device_printf(sc->sc_dev, "no BBP id for device id "
810 "0x%04x\n", sc->sc_pci_did);
814 info = __SHIFTIN(sc->sc_pci_revid, BWI_INFO_BBPREV_MASK) |
815 __SHIFTIN(0, BWI_INFO_BBPPKG_MASK);
819 * Find out number of regwins
822 if (rw_type == BWI_REGWIN_T_COM && rw_rev >= 4) {
823 nregwin = __SHIFTOUT(info, BWI_INFO_NREGWIN_MASK);
825 for (i = 0; i < nitems(bwi_regwin_count); ++i) {
826 if (bwi_regwin_count[i].bbp_id == bbp_id) {
827 nregwin = bwi_regwin_count[i].nregwin;
832 device_printf(sc->sc_dev, "no number of win for "
833 "BBP id 0x%04x\n", bbp_id);
838 /* Record BBP id/rev for later using */
839 sc->sc_bbp_id = bbp_id;
840 sc->sc_bbp_rev = __SHIFTOUT(info, BWI_INFO_BBPREV_MASK);
841 sc->sc_bbp_pkg = __SHIFTOUT(info, BWI_INFO_BBPPKG_MASK);
842 device_printf(sc->sc_dev, "BBP: id 0x%04x, rev 0x%x, pkg %d\n",
843 sc->sc_bbp_id, sc->sc_bbp_rev, sc->sc_bbp_pkg);
845 DPRINTF(sc, BWI_DBG_ATTACH, "nregwin %d, cap 0x%08x\n",
846 nregwin, sc->sc_cap);
849 * Create rest of the regwins
852 /* Don't re-create common regwin, if it is already created */
853 i = BWI_REGWIN_EXIST(&sc->sc_com_regwin) ? 1 : 0;
855 for (; i < nregwin; ++i) {
857 * Get regwin information
859 error = bwi_regwin_select(sc, i);
861 device_printf(sc->sc_dev,
862 "can't select regwin %d\n", i);
865 bwi_regwin_info(sc, &rw_type, &rw_rev);
869 * 1) Bus (PCI/PCIE) regwin
871 * Ignore rest types of regwin
873 if (rw_type == BWI_REGWIN_T_BUSPCI ||
874 rw_type == BWI_REGWIN_T_BUSPCIE) {
875 if (BWI_REGWIN_EXIST(&sc->sc_bus_regwin)) {
876 device_printf(sc->sc_dev,
877 "bus regwin already exists\n");
879 BWI_CREATE_REGWIN(&sc->sc_bus_regwin, i,
882 } else if (rw_type == BWI_REGWIN_T_MAC) {
883 /* XXX ignore return value */
884 bwi_mac_attach(sc, i, rw_rev);
888 /* At least one MAC shold exist */
889 if (!BWI_REGWIN_EXIST(&sc->sc_mac[0].mac_regwin)) {
890 device_printf(sc->sc_dev, "no MAC was found\n");
893 KASSERT(sc->sc_nmac > 0, ("no mac's"));
895 /* Bus regwin must exist */
896 if (!BWI_REGWIN_EXIST(&sc->sc_bus_regwin)) {
897 device_printf(sc->sc_dev, "no bus regwin was found\n");
901 /* Start with first MAC */
902 error = bwi_regwin_switch(sc, &sc->sc_mac[0].mac_regwin, NULL);
910 bwi_bus_init(struct bwi_softc *sc, struct bwi_mac *mac)
912 struct bwi_regwin *old, *bus;
916 bus = &sc->sc_bus_regwin;
917 KASSERT(sc->sc_cur_regwin == &mac->mac_regwin, ("not cur regwin"));
920 * Tell bus to generate requested interrupts
922 if (bus->rw_rev < 6 && bus->rw_type == BWI_REGWIN_T_BUSPCI) {
924 * NOTE: Read BWI_FLAGS from MAC regwin
926 val = CSR_READ_4(sc, BWI_FLAGS);
928 error = bwi_regwin_switch(sc, bus, &old);
932 CSR_SETBITS_4(sc, BWI_INTRVEC, (val & BWI_FLAGS_INTR_MASK));
936 mac_mask = 1 << mac->mac_id;
938 error = bwi_regwin_switch(sc, bus, &old);
942 val = pci_read_config(sc->sc_dev, BWI_PCIR_INTCTL, 4);
943 val |= mac_mask << 8;
944 pci_write_config(sc->sc_dev, BWI_PCIR_INTCTL, val, 4);
947 if (sc->sc_flags & BWI_F_BUS_INITED)
950 if (bus->rw_type == BWI_REGWIN_T_BUSPCI) {
952 * Enable prefetch and burst
954 CSR_SETBITS_4(sc, BWI_BUS_CONFIG,
955 BWI_BUS_CONFIG_PREFETCH | BWI_BUS_CONFIG_BURST);
957 if (bus->rw_rev < 5) {
958 struct bwi_regwin *com = &sc->sc_com_regwin;
961 * Configure timeouts for bus operation
965 * Set service timeout and request timeout
967 CSR_SETBITS_4(sc, BWI_CONF_LO,
968 __SHIFTIN(BWI_CONF_LO_SERVTO, BWI_CONF_LO_SERVTO_MASK) |
969 __SHIFTIN(BWI_CONF_LO_REQTO, BWI_CONF_LO_REQTO_MASK));
972 * If there is common regwin, we switch to that regwin
973 * and switch back to bus regwin once we have done.
975 if (BWI_REGWIN_EXIST(com)) {
976 error = bwi_regwin_switch(sc, com, NULL);
981 /* Let bus know what we have changed */
982 CSR_WRITE_4(sc, BWI_BUS_ADDR, BWI_BUS_ADDR_MAGIC);
983 CSR_READ_4(sc, BWI_BUS_ADDR); /* Flush */
984 CSR_WRITE_4(sc, BWI_BUS_DATA, 0);
985 CSR_READ_4(sc, BWI_BUS_DATA); /* Flush */
987 if (BWI_REGWIN_EXIST(com)) {
988 error = bwi_regwin_switch(sc, bus, NULL);
992 } else if (bus->rw_rev >= 11) {
994 * Enable memory read multiple
996 CSR_SETBITS_4(sc, BWI_BUS_CONFIG, BWI_BUS_CONFIG_MRM);
1002 sc->sc_flags |= BWI_F_BUS_INITED;
1004 return bwi_regwin_switch(sc, old, NULL);
1008 bwi_get_card_flags(struct bwi_softc *sc)
1010 #define PCI_VENDOR_APPLE 0x106b
1011 #define PCI_VENDOR_DELL 0x1028
1012 sc->sc_card_flags = bwi_read_sprom(sc, BWI_SPROM_CARD_FLAGS);
1013 if (sc->sc_card_flags == 0xffff)
1014 sc->sc_card_flags = 0;
1016 if (sc->sc_pci_subvid == PCI_VENDOR_DELL &&
1017 sc->sc_bbp_id == BWI_BBPID_BCM4301 &&
1018 sc->sc_pci_revid == 0x74)
1019 sc->sc_card_flags |= BWI_CARD_F_BT_COEXIST;
1021 if (sc->sc_pci_subvid == PCI_VENDOR_APPLE &&
1022 sc->sc_pci_subdid == 0x4e && /* XXX */
1023 sc->sc_pci_revid > 0x40)
1024 sc->sc_card_flags |= BWI_CARD_F_PA_GPIO9;
1026 DPRINTF(sc, BWI_DBG_ATTACH, "card flags 0x%04x\n", sc->sc_card_flags);
1027 #undef PCI_VENDOR_DELL
1028 #undef PCI_VENDOR_APPLE
1032 bwi_get_eaddr(struct bwi_softc *sc, uint16_t eaddr_ofs, uint8_t *eaddr)
1036 for (i = 0; i < 3; ++i) {
1037 *((uint16_t *)eaddr + i) =
1038 htobe16(bwi_read_sprom(sc, eaddr_ofs + 2 * i));
1043 bwi_get_clock_freq(struct bwi_softc *sc, struct bwi_clock_freq *freq)
1045 struct bwi_regwin *com;
1050 bzero(freq, sizeof(*freq));
1051 com = &sc->sc_com_regwin;
1053 KASSERT(BWI_REGWIN_EXIST(com), ("regwin does not exist"));
1054 KASSERT(sc->sc_cur_regwin == com, ("wrong regwin"));
1055 KASSERT(sc->sc_cap & BWI_CAP_CLKMODE, ("wrong clock mode"));
1058 * Calculate clock frequency
1062 if (com->rw_rev < 6) {
1063 val = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4);
1064 if (val & BWI_PCIM_GPIO_OUT_CLKSRC) {
1065 src = BWI_CLKSRC_PCI;
1068 src = BWI_CLKSRC_CS_OSC;
1071 } else if (com->rw_rev < 10) {
1072 val = CSR_READ_4(sc, BWI_CLOCK_CTRL);
1074 src = __SHIFTOUT(val, BWI_CLOCK_CTRL_CLKSRC);
1075 if (src == BWI_CLKSRC_LP_OSC) {
1078 div = (__SHIFTOUT(val, BWI_CLOCK_CTRL_FDIV) + 1) << 2;
1080 /* Unknown source */
1081 if (src >= BWI_CLKSRC_MAX)
1082 src = BWI_CLKSRC_CS_OSC;
1085 val = CSR_READ_4(sc, BWI_CLOCK_INFO);
1087 src = BWI_CLKSRC_CS_OSC;
1088 div = (__SHIFTOUT(val, BWI_CLOCK_INFO_FDIV) + 1) << 2;
1091 KASSERT(src >= 0 && src < BWI_CLKSRC_MAX, ("bad src %d", src));
1092 KASSERT(div != 0, ("div zero"));
1094 DPRINTF(sc, BWI_DBG_ATTACH, "clksrc %s\n",
1095 src == BWI_CLKSRC_PCI ? "PCI" :
1096 (src == BWI_CLKSRC_LP_OSC ? "LP_OSC" : "CS_OSC"));
1098 freq->clkfreq_min = bwi_clkfreq[src].freq_min / div;
1099 freq->clkfreq_max = bwi_clkfreq[src].freq_max / div;
1101 DPRINTF(sc, BWI_DBG_ATTACH, "clkfreq min %u, max %u\n",
1102 freq->clkfreq_min, freq->clkfreq_max);
1106 bwi_set_clock_mode(struct bwi_softc *sc, enum bwi_clock_mode clk_mode)
1108 struct bwi_regwin *old, *com;
1109 uint32_t clk_ctrl, clk_src;
1110 int error, pwr_off = 0;
1112 com = &sc->sc_com_regwin;
1113 if (!BWI_REGWIN_EXIST(com))
1116 if (com->rw_rev >= 10 || com->rw_rev < 6)
1120 * For common regwin whose rev is [6, 10), the chip
1121 * must be capable to change clock mode.
1123 if ((sc->sc_cap & BWI_CAP_CLKMODE) == 0)
1126 error = bwi_regwin_switch(sc, com, &old);
1130 if (clk_mode == BWI_CLOCK_MODE_FAST)
1131 bwi_power_on(sc, 0); /* Don't turn on PLL */
1133 clk_ctrl = CSR_READ_4(sc, BWI_CLOCK_CTRL);
1134 clk_src = __SHIFTOUT(clk_ctrl, BWI_CLOCK_CTRL_CLKSRC);
1137 case BWI_CLOCK_MODE_FAST:
1138 clk_ctrl &= ~BWI_CLOCK_CTRL_SLOW;
1139 clk_ctrl |= BWI_CLOCK_CTRL_IGNPLL;
1141 case BWI_CLOCK_MODE_SLOW:
1142 clk_ctrl |= BWI_CLOCK_CTRL_SLOW;
1144 case BWI_CLOCK_MODE_DYN:
1145 clk_ctrl &= ~(BWI_CLOCK_CTRL_SLOW |
1146 BWI_CLOCK_CTRL_IGNPLL |
1147 BWI_CLOCK_CTRL_NODYN);
1148 if (clk_src != BWI_CLKSRC_CS_OSC) {
1149 clk_ctrl |= BWI_CLOCK_CTRL_NODYN;
1154 CSR_WRITE_4(sc, BWI_CLOCK_CTRL, clk_ctrl);
1157 bwi_power_off(sc, 0); /* Leave PLL as it is */
1159 return bwi_regwin_switch(sc, old, NULL);
1163 bwi_set_clock_delay(struct bwi_softc *sc)
1165 struct bwi_regwin *old, *com;
1168 com = &sc->sc_com_regwin;
1169 if (!BWI_REGWIN_EXIST(com))
1172 error = bwi_regwin_switch(sc, com, &old);
1176 if (sc->sc_bbp_id == BWI_BBPID_BCM4321) {
1177 if (sc->sc_bbp_rev == 0)
1178 CSR_WRITE_4(sc, BWI_CONTROL, BWI_CONTROL_MAGIC0);
1179 else if (sc->sc_bbp_rev == 1)
1180 CSR_WRITE_4(sc, BWI_CONTROL, BWI_CONTROL_MAGIC1);
1183 if (sc->sc_cap & BWI_CAP_CLKMODE) {
1184 if (com->rw_rev >= 10) {
1185 CSR_FILT_SETBITS_4(sc, BWI_CLOCK_INFO, 0xffff, 0x40000);
1187 struct bwi_clock_freq freq;
1189 bwi_get_clock_freq(sc, &freq);
1190 CSR_WRITE_4(sc, BWI_PLL_ON_DELAY,
1191 howmany(freq.clkfreq_max * 150, 1000000));
1192 CSR_WRITE_4(sc, BWI_FREQ_SEL_DELAY,
1193 howmany(freq.clkfreq_max * 15, 1000000));
1197 return bwi_regwin_switch(sc, old, NULL);
1201 bwi_init(struct bwi_softc *sc)
1203 struct ieee80211com *ic = &sc->sc_ic;
1206 bwi_init_statechg(sc, 1);
1209 if (sc->sc_flags & BWI_F_RUNNING)
1210 ieee80211_start_all(ic); /* start all vap's */
1214 bwi_init_statechg(struct bwi_softc *sc, int statechg)
1216 struct bwi_mac *mac;
1219 BWI_ASSERT_LOCKED(sc);
1221 bwi_stop_locked(sc, statechg);
1223 bwi_bbp_power_on(sc, BWI_CLOCK_MODE_FAST);
1227 mac = &sc->sc_mac[0];
1228 error = bwi_regwin_switch(sc, &mac->mac_regwin, NULL);
1230 device_printf(sc->sc_dev, "%s: error %d on regwin switch\n",
1234 error = bwi_mac_init(mac);
1236 device_printf(sc->sc_dev, "%s: error %d on MAC init\n",
1241 bwi_bbp_power_on(sc, BWI_CLOCK_MODE_DYN);
1243 bwi_set_bssid(sc, bwi_zero_addr); /* Clear BSSID */
1244 bwi_set_addr_filter(sc, BWI_ADDR_FILTER_MYADDR, sc->sc_ic.ic_macaddr);
1246 bwi_mac_reset_hwkeys(mac);
1248 if ((mac->mac_flags & BWI_MAC_F_HAS_TXSTATS) == 0) {
1253 * Drain any possible pending TX status
1255 for (i = 0; i < NRETRY; ++i) {
1256 if ((CSR_READ_4(sc, BWI_TXSTATUS0) &
1257 BWI_TXSTATUS0_VALID) == 0)
1259 CSR_READ_4(sc, BWI_TXSTATUS1);
1262 device_printf(sc->sc_dev,
1263 "%s: can't drain TX status\n", __func__);
1267 if (mac->mac_phy.phy_mode == IEEE80211_MODE_11G)
1268 bwi_mac_updateslot(mac, 1);
1271 error = bwi_mac_start(mac);
1273 device_printf(sc->sc_dev, "%s: error %d starting MAC\n",
1278 /* Clear stop flag before enabling interrupt */
1279 sc->sc_flags &= ~BWI_F_STOP;
1280 sc->sc_flags |= BWI_F_RUNNING;
1281 callout_reset(&sc->sc_watchdog_timer, hz, bwi_watchdog, sc);
1284 bwi_enable_intrs(sc, BWI_INIT_INTRS);
1287 bwi_stop_locked(sc, 1);
1291 bwi_parent(struct ieee80211com *ic)
1293 struct bwi_softc *sc = ic->ic_softc;
1297 if (ic->ic_nrunning > 0) {
1298 struct bwi_mac *mac;
1301 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1302 ("current regwin type %d",
1303 sc->sc_cur_regwin->rw_type));
1304 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1306 if (ic->ic_promisc > 0 && (sc->sc_flags & BWI_F_PROMISC) == 0) {
1308 sc->sc_flags |= BWI_F_PROMISC;
1309 } else if (ic->ic_promisc == 0 &&
1310 (sc->sc_flags & BWI_F_PROMISC) != 0) {
1312 sc->sc_flags &= ~BWI_F_PROMISC;
1316 bwi_mac_set_promisc(mac, promisc);
1318 if (ic->ic_nrunning > 0) {
1319 if ((sc->sc_flags & BWI_F_RUNNING) == 0) {
1320 bwi_init_statechg(sc, 1);
1323 } else if (sc->sc_flags & BWI_F_RUNNING)
1324 bwi_stop_locked(sc, 1);
1327 ieee80211_start_all(ic);
1331 bwi_transmit(struct ieee80211com *ic, struct mbuf *m)
1333 struct bwi_softc *sc = ic->ic_softc;
1337 if ((sc->sc_flags & BWI_F_RUNNING) == 0) {
1341 error = mbufq_enqueue(&sc->sc_snd, m);
1346 bwi_start_locked(sc);
1352 bwi_start_locked(struct bwi_softc *sc)
1354 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
1355 struct ieee80211_frame *wh;
1356 struct ieee80211_node *ni;
1360 BWI_ASSERT_LOCKED(sc);
1365 while (tbd->tbd_buf[idx].tb_mbuf == NULL &&
1366 tbd->tbd_used + BWI_TX_NSPRDESC < BWI_TX_NDESC &&
1367 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1368 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1369 wh = mtod(m, struct ieee80211_frame *);
1370 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) != 0 &&
1371 ieee80211_crypto_encap(ni, m) == NULL) {
1372 if_inc_counter(ni->ni_vap->iv_ifp,
1373 IFCOUNTER_OERRORS, 1);
1374 ieee80211_free_node(ni);
1378 if (bwi_encap(sc, idx, m, ni) != 0) {
1379 /* 'm' is freed in bwi_encap() if we reach here */
1381 if_inc_counter(ni->ni_vap->iv_ifp,
1382 IFCOUNTER_OERRORS, 1);
1383 ieee80211_free_node(ni);
1385 counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1390 idx = (idx + 1) % BWI_TX_NDESC;
1395 sc->sc_tx_timer = 5;
1399 bwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1400 const struct ieee80211_bpf_params *params)
1402 struct ieee80211com *ic = ni->ni_ic;
1403 struct bwi_softc *sc = ic->ic_softc;
1405 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
1408 if ((sc->sc_flags & BWI_F_RUNNING) == 0) {
1415 KASSERT(tbd->tbd_buf[idx].tb_mbuf == NULL, ("slot %d not empty", idx));
1416 if (params == NULL) {
1418 * Legacy path; interpret frame contents to decide
1419 * precisely how to send the frame.
1421 error = bwi_encap(sc, idx, m, ni);
1424 * Caller supplied explicit parameters to use in
1425 * sending the frame.
1427 error = bwi_encap_raw(sc, idx, m, ni, params);
1431 tbd->tbd_idx = (idx + 1) % BWI_TX_NDESC;
1432 sc->sc_tx_timer = 5;
1439 bwi_watchdog(void *arg)
1441 struct bwi_softc *sc;
1444 BWI_ASSERT_LOCKED(sc);
1445 if (sc->sc_tx_timer != 0 && --sc->sc_tx_timer == 0) {
1446 device_printf(sc->sc_dev, "watchdog timeout\n");
1447 counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1448 taskqueue_enqueue(sc->sc_tq, &sc->sc_restart_task);
1450 callout_reset(&sc->sc_watchdog_timer, hz, bwi_watchdog, sc);
1454 bwi_stop(struct bwi_softc *sc, int statechg)
1457 bwi_stop_locked(sc, statechg);
1462 bwi_stop_locked(struct bwi_softc *sc, int statechg)
1464 struct bwi_mac *mac;
1465 int i, error, pwr_off = 0;
1467 BWI_ASSERT_LOCKED(sc);
1469 callout_stop(&sc->sc_calib_ch);
1470 callout_stop(&sc->sc_led_blink_ch);
1471 sc->sc_led_blinking = 0;
1472 sc->sc_flags |= BWI_F_STOP;
1474 if (sc->sc_flags & BWI_F_RUNNING) {
1475 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1476 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1477 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1479 bwi_disable_intrs(sc, BWI_ALL_INTRS);
1480 CSR_READ_4(sc, BWI_MAC_INTR_MASK);
1484 for (i = 0; i < sc->sc_nmac; ++i) {
1485 struct bwi_regwin *old_rw;
1487 mac = &sc->sc_mac[i];
1488 if ((mac->mac_flags & BWI_MAC_F_INITED) == 0)
1491 error = bwi_regwin_switch(sc, &mac->mac_regwin, &old_rw);
1495 bwi_mac_shutdown(mac);
1498 bwi_regwin_switch(sc, old_rw, NULL);
1502 bwi_bbp_power_off(sc);
1504 sc->sc_tx_timer = 0;
1505 callout_stop(&sc->sc_watchdog_timer);
1506 sc->sc_flags &= ~BWI_F_RUNNING;
1512 struct bwi_softc *sc = xsc;
1513 struct bwi_mac *mac;
1514 uint32_t intr_status;
1515 uint32_t txrx_intr_status[BWI_TXRX_NRING];
1516 int i, txrx_error, tx = 0, rx_data = -1;
1520 if ((sc->sc_flags & BWI_F_RUNNING) == 0 ||
1521 (sc->sc_flags & BWI_F_STOP)) {
1526 * Get interrupt status
1528 intr_status = CSR_READ_4(sc, BWI_MAC_INTR_STATUS);
1529 if (intr_status == 0xffffffff) { /* Not for us */
1534 DPRINTF(sc, BWI_DBG_INTR, "intr status 0x%08x\n", intr_status);
1536 intr_status &= CSR_READ_4(sc, BWI_MAC_INTR_MASK);
1537 if (intr_status == 0) { /* Nothing is interesting */
1542 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1543 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1544 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1547 DPRINTF(sc, BWI_DBG_INTR, "%s\n", "TX/RX intr");
1548 for (i = 0; i < BWI_TXRX_NRING; ++i) {
1551 if (BWI_TXRX_IS_RX(i))
1552 mask = BWI_TXRX_RX_INTRS;
1554 mask = BWI_TXRX_TX_INTRS;
1556 txrx_intr_status[i] =
1557 CSR_READ_4(sc, BWI_TXRX_INTR_STATUS(i)) & mask;
1559 _DPRINTF(sc, BWI_DBG_INTR, ", %d 0x%08x",
1560 i, txrx_intr_status[i]);
1562 if (txrx_intr_status[i] & BWI_TXRX_INTR_ERROR) {
1563 device_printf(sc->sc_dev,
1564 "%s: intr fatal TX/RX (%d) error 0x%08x\n",
1565 __func__, i, txrx_intr_status[i]);
1569 _DPRINTF(sc, BWI_DBG_INTR, "%s\n", "");
1572 * Acknowledge interrupt
1574 CSR_WRITE_4(sc, BWI_MAC_INTR_STATUS, intr_status);
1576 for (i = 0; i < BWI_TXRX_NRING; ++i)
1577 CSR_WRITE_4(sc, BWI_TXRX_INTR_STATUS(i), txrx_intr_status[i]);
1579 /* Disable all interrupts */
1580 bwi_disable_intrs(sc, BWI_ALL_INTRS);
1583 * http://bcm-specs.sipsolutions.net/Interrupts
1584 * Says for this bit (0x800):
1587 * We got this one while testing things when by accident the
1588 * template ram wasn't set to big endian when it should have
1589 * been after writing the initial values. It keeps on being
1590 * triggered, the only way to stop it seems to shut down the
1593 * Suggesting that we should never get it and if we do we're not
1594 * feeding TX packets into the MAC correctly if we do... Apparently,
1595 * it is valid only on mac version 5 and higher, but I couldn't
1596 * find a reference for that... Since I see them from time to time
1597 * on my card, this suggests an error in the tx path still...
1599 if (intr_status & BWI_INTR_PHY_TXERR) {
1600 if (mac->mac_flags & BWI_MAC_F_PHYE_RESET) {
1601 device_printf(sc->sc_dev, "%s: intr PHY TX error\n",
1603 taskqueue_enqueue(sc->sc_tq, &sc->sc_restart_task);
1610 /* TODO: reset device */
1613 if (intr_status & BWI_INTR_TBTT)
1614 bwi_mac_config_ps(mac);
1616 if (intr_status & BWI_INTR_EO_ATIM)
1617 device_printf(sc->sc_dev, "EO_ATIM\n");
1619 if (intr_status & BWI_INTR_PMQ) {
1621 if ((CSR_READ_4(sc, BWI_MAC_PS_STATUS) & 0x8) == 0)
1624 CSR_WRITE_2(sc, BWI_MAC_PS_STATUS, 0x2);
1627 if (intr_status & BWI_INTR_NOISE)
1628 device_printf(sc->sc_dev, "intr noise\n");
1630 if (txrx_intr_status[0] & BWI_TXRX_INTR_RX) {
1631 rx_data = sc->sc_rxeof(sc);
1632 if (sc->sc_flags & BWI_F_STOP) {
1638 if (txrx_intr_status[3] & BWI_TXRX_INTR_RX) {
1639 sc->sc_txeof_status(sc);
1643 if (intr_status & BWI_INTR_TX_DONE) {
1648 /* Re-enable interrupts */
1649 bwi_enable_intrs(sc, BWI_INIT_INTRS);
1651 if (sc->sc_blink_led != NULL && sc->sc_led_blink) {
1652 int evt = BWI_LED_EVENT_NONE;
1654 if (tx && rx_data > 0) {
1655 if (sc->sc_rx_rate > sc->sc_tx_rate)
1656 evt = BWI_LED_EVENT_RX;
1658 evt = BWI_LED_EVENT_TX;
1660 evt = BWI_LED_EVENT_TX;
1661 } else if (rx_data > 0) {
1662 evt = BWI_LED_EVENT_RX;
1663 } else if (rx_data == 0) {
1664 evt = BWI_LED_EVENT_POLL;
1667 if (evt != BWI_LED_EVENT_NONE)
1668 bwi_led_event(sc, evt);
1675 bwi_scan_start(struct ieee80211com *ic)
1677 struct bwi_softc *sc = ic->ic_softc;
1680 /* Enable MAC beacon promiscuity */
1681 CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PASS_BCN);
1686 bwi_getradiocaps(struct ieee80211com *ic,
1687 int maxchans, int *nchans, struct ieee80211_channel chans[])
1689 struct bwi_softc *sc = ic->ic_softc;
1690 struct bwi_mac *mac;
1691 struct bwi_phy *phy;
1692 uint8_t bands[IEEE80211_MODE_BYTES];
1695 * XXX First MAC is known to exist
1698 mac = &sc->sc_mac[0];
1699 phy = &mac->mac_phy;
1701 memset(bands, 0, sizeof(bands));
1702 switch (phy->phy_mode) {
1703 case IEEE80211_MODE_11G:
1704 setbit(bands, IEEE80211_MODE_11G);
1706 case IEEE80211_MODE_11B:
1707 setbit(bands, IEEE80211_MODE_11B);
1709 case IEEE80211_MODE_11A:
1711 setbit(bands, IEEE80211_MODE_11A);
1712 device_printf(sc->sc_dev, "no 11a support\n");
1715 panic("unknown phymode %d\n", phy->phy_mode);
1718 ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
1719 bwi_chan_2ghz, nitems(bwi_chan_2ghz), bands, 0);
1723 bwi_set_channel(struct ieee80211com *ic)
1725 struct bwi_softc *sc = ic->ic_softc;
1726 struct ieee80211_channel *c = ic->ic_curchan;
1727 struct bwi_mac *mac;
1730 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1731 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1732 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1733 bwi_rf_set_chan(mac, ieee80211_chan2ieee(ic, c), 0);
1735 sc->sc_rates = ieee80211_get_ratetable(c);
1738 * Setup radio tap channel freq and flags
1740 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
1741 htole16(c->ic_freq);
1742 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
1743 htole16(c->ic_flags & 0xffff);
1749 bwi_scan_end(struct ieee80211com *ic)
1751 struct bwi_softc *sc = ic->ic_softc;
1754 CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PASS_BCN);
1759 bwi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1761 struct bwi_vap *bvp = BWI_VAP(vap);
1762 struct ieee80211com *ic= vap->iv_ic;
1763 struct bwi_softc *sc = ic->ic_softc;
1764 enum ieee80211_state ostate = vap->iv_state;
1765 struct bwi_mac *mac;
1770 callout_stop(&sc->sc_calib_ch);
1772 if (nstate == IEEE80211_S_INIT)
1773 sc->sc_txpwrcb_type = BWI_TXPWR_INIT;
1775 bwi_led_newstate(sc, nstate);
1777 error = bvp->bv_newstate(vap, nstate, arg);
1782 * Clear the BSSID when we stop a STA
1784 if (vap->iv_opmode == IEEE80211_M_STA) {
1785 if (ostate == IEEE80211_S_RUN && nstate != IEEE80211_S_RUN) {
1787 * Clear out the BSSID. If we reassociate to
1788 * the same AP, this will reinialize things
1791 if (ic->ic_opmode == IEEE80211_M_STA &&
1792 !(sc->sc_flags & BWI_F_STOP))
1793 bwi_set_bssid(sc, bwi_zero_addr);
1797 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
1799 } else if (nstate == IEEE80211_S_RUN) {
1800 bwi_set_bssid(sc, vap->iv_bss->ni_bssid);
1802 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1803 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1804 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1806 /* Initial TX power calibration */
1807 bwi_mac_calibrate_txpower(mac, BWI_TXPWR_INIT);
1809 sc->sc_txpwrcb_type = BWI_TXPWR_FORCE;
1811 sc->sc_txpwrcb_type = BWI_TXPWR_CALIB;
1814 callout_reset(&sc->sc_calib_ch, hz, bwi_calibrate, sc);
1823 bwi_media_change(struct ifnet *ifp)
1825 int error = ieee80211_media_change(ifp);
1826 /* NB: only the fixed rate can change and that doesn't need a reset */
1827 return (error == ENETRESET ? 0 : error);
1831 bwi_dma_alloc(struct bwi_softc *sc)
1833 int error, i, has_txstats;
1834 bus_addr_t lowaddr = 0;
1835 bus_size_t tx_ring_sz, rx_ring_sz, desc_sz = 0;
1836 uint32_t txrx_ctrl_step = 0;
1839 for (i = 0; i < sc->sc_nmac; ++i) {
1840 if (sc->sc_mac[i].mac_flags & BWI_MAC_F_HAS_TXSTATS) {
1846 switch (sc->sc_bus_space) {
1847 case BWI_BUS_SPACE_30BIT:
1848 case BWI_BUS_SPACE_32BIT:
1849 if (sc->sc_bus_space == BWI_BUS_SPACE_30BIT)
1850 lowaddr = BWI_BUS_SPACE_MAXADDR;
1852 lowaddr = BUS_SPACE_MAXADDR_32BIT;
1853 desc_sz = sizeof(struct bwi_desc32);
1854 txrx_ctrl_step = 0x20;
1856 sc->sc_init_tx_ring = bwi_init_tx_ring32;
1857 sc->sc_free_tx_ring = bwi_free_tx_ring32;
1858 sc->sc_init_rx_ring = bwi_init_rx_ring32;
1859 sc->sc_free_rx_ring = bwi_free_rx_ring32;
1860 sc->sc_setup_rxdesc = bwi_setup_rx_desc32;
1861 sc->sc_setup_txdesc = bwi_setup_tx_desc32;
1862 sc->sc_rxeof = bwi_rxeof32;
1863 sc->sc_start_tx = bwi_start_tx32;
1865 sc->sc_init_txstats = bwi_init_txstats32;
1866 sc->sc_free_txstats = bwi_free_txstats32;
1867 sc->sc_txeof_status = bwi_txeof_status32;
1871 case BWI_BUS_SPACE_64BIT:
1872 lowaddr = BUS_SPACE_MAXADDR; /* XXX */
1873 desc_sz = sizeof(struct bwi_desc64);
1874 txrx_ctrl_step = 0x40;
1876 sc->sc_init_tx_ring = bwi_init_tx_ring64;
1877 sc->sc_free_tx_ring = bwi_free_tx_ring64;
1878 sc->sc_init_rx_ring = bwi_init_rx_ring64;
1879 sc->sc_free_rx_ring = bwi_free_rx_ring64;
1880 sc->sc_setup_rxdesc = bwi_setup_rx_desc64;
1881 sc->sc_setup_txdesc = bwi_setup_tx_desc64;
1882 sc->sc_rxeof = bwi_rxeof64;
1883 sc->sc_start_tx = bwi_start_tx64;
1885 sc->sc_init_txstats = bwi_init_txstats64;
1886 sc->sc_free_txstats = bwi_free_txstats64;
1887 sc->sc_txeof_status = bwi_txeof_status64;
1892 KASSERT(lowaddr != 0, ("lowaddr zero"));
1893 KASSERT(desc_sz != 0, ("desc_sz zero"));
1894 KASSERT(txrx_ctrl_step != 0, ("txrx_ctrl_step zero"));
1896 tx_ring_sz = roundup(desc_sz * BWI_TX_NDESC, BWI_RING_ALIGN);
1897 rx_ring_sz = roundup(desc_sz * BWI_RX_NDESC, BWI_RING_ALIGN);
1900 * Create top level DMA tag
1902 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), /* parent */
1903 BWI_ALIGN, 0, /* alignment, bounds */
1904 lowaddr, /* lowaddr */
1905 BUS_SPACE_MAXADDR, /* highaddr */
1906 NULL, NULL, /* filter, filterarg */
1907 BUS_SPACE_MAXSIZE, /* maxsize */
1908 BUS_SPACE_UNRESTRICTED, /* nsegments */
1909 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1911 NULL, NULL, /* lockfunc, lockarg */
1912 &sc->sc_parent_dtag);
1914 device_printf(sc->sc_dev, "can't create parent DMA tag\n");
1918 #define TXRX_CTRL(idx) (BWI_TXRX_CTRL_BASE + (idx) * txrx_ctrl_step)
1921 * Create TX ring DMA stuffs
1923 error = bus_dma_tag_create(sc->sc_parent_dtag,
1933 &sc->sc_txring_dtag);
1935 device_printf(sc->sc_dev, "can't create TX ring DMA tag\n");
1939 for (i = 0; i < BWI_TX_NRING; ++i) {
1940 error = bwi_dma_ring_alloc(sc, sc->sc_txring_dtag,
1941 &sc->sc_tx_rdata[i], tx_ring_sz,
1944 device_printf(sc->sc_dev, "%dth TX ring "
1945 "DMA alloc failed\n", i);
1951 * Create RX ring DMA stuffs
1953 error = bus_dma_tag_create(sc->sc_parent_dtag,
1963 &sc->sc_rxring_dtag);
1965 device_printf(sc->sc_dev, "can't create RX ring DMA tag\n");
1969 error = bwi_dma_ring_alloc(sc, sc->sc_rxring_dtag, &sc->sc_rx_rdata,
1970 rx_ring_sz, TXRX_CTRL(0));
1972 device_printf(sc->sc_dev, "RX ring DMA alloc failed\n");
1977 error = bwi_dma_txstats_alloc(sc, TXRX_CTRL(3), desc_sz);
1979 device_printf(sc->sc_dev,
1980 "TX stats DMA alloc failed\n");
1987 return bwi_dma_mbuf_create(sc);
1991 bwi_dma_free(struct bwi_softc *sc)
1993 if (sc->sc_txring_dtag != NULL) {
1996 for (i = 0; i < BWI_TX_NRING; ++i) {
1997 struct bwi_ring_data *rd = &sc->sc_tx_rdata[i];
1999 if (rd->rdata_desc != NULL) {
2000 bus_dmamap_unload(sc->sc_txring_dtag,
2002 bus_dmamem_free(sc->sc_txring_dtag,
2007 bus_dma_tag_destroy(sc->sc_txring_dtag);
2010 if (sc->sc_rxring_dtag != NULL) {
2011 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2013 if (rd->rdata_desc != NULL) {
2014 bus_dmamap_unload(sc->sc_rxring_dtag, rd->rdata_dmap);
2015 bus_dmamem_free(sc->sc_rxring_dtag, rd->rdata_desc,
2018 bus_dma_tag_destroy(sc->sc_rxring_dtag);
2021 bwi_dma_txstats_free(sc);
2022 bwi_dma_mbuf_destroy(sc, BWI_TX_NRING, 1);
2024 if (sc->sc_parent_dtag != NULL)
2025 bus_dma_tag_destroy(sc->sc_parent_dtag);
2029 bwi_dma_ring_alloc(struct bwi_softc *sc, bus_dma_tag_t dtag,
2030 struct bwi_ring_data *rd, bus_size_t size,
2035 error = bus_dmamem_alloc(dtag, &rd->rdata_desc,
2036 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2039 device_printf(sc->sc_dev, "can't allocate DMA mem\n");
2043 error = bus_dmamap_load(dtag, rd->rdata_dmap, rd->rdata_desc, size,
2044 bwi_dma_ring_addr, &rd->rdata_paddr,
2047 device_printf(sc->sc_dev, "can't load DMA mem\n");
2048 bus_dmamem_free(dtag, rd->rdata_desc, rd->rdata_dmap);
2049 rd->rdata_desc = NULL;
2053 rd->rdata_txrx_ctrl = txrx_ctrl;
2058 bwi_dma_txstats_alloc(struct bwi_softc *sc, uint32_t ctrl_base,
2061 struct bwi_txstats_data *st;
2062 bus_size_t dma_size;
2065 st = malloc(sizeof(*st), M_DEVBUF, M_NOWAIT | M_ZERO);
2067 device_printf(sc->sc_dev, "can't allocate txstats data\n");
2070 sc->sc_txstats = st;
2073 * Create TX stats descriptor DMA stuffs
2075 dma_size = roundup(desc_sz * BWI_TXSTATS_NDESC, BWI_RING_ALIGN);
2077 error = bus_dma_tag_create(sc->sc_parent_dtag,
2088 &st->stats_ring_dtag);
2090 device_printf(sc->sc_dev, "can't create txstats ring "
2095 error = bus_dmamem_alloc(st->stats_ring_dtag, &st->stats_ring,
2096 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2097 &st->stats_ring_dmap);
2099 device_printf(sc->sc_dev, "can't allocate txstats ring "
2101 bus_dma_tag_destroy(st->stats_ring_dtag);
2102 st->stats_ring_dtag = NULL;
2106 error = bus_dmamap_load(st->stats_ring_dtag, st->stats_ring_dmap,
2107 st->stats_ring, dma_size,
2108 bwi_dma_ring_addr, &st->stats_ring_paddr,
2111 device_printf(sc->sc_dev, "can't load txstats ring DMA mem\n");
2112 bus_dmamem_free(st->stats_ring_dtag, st->stats_ring,
2113 st->stats_ring_dmap);
2114 bus_dma_tag_destroy(st->stats_ring_dtag);
2115 st->stats_ring_dtag = NULL;
2120 * Create TX stats DMA stuffs
2122 dma_size = roundup(sizeof(struct bwi_txstats) * BWI_TXSTATS_NDESC,
2125 error = bus_dma_tag_create(sc->sc_parent_dtag,
2138 device_printf(sc->sc_dev, "can't create txstats DMA tag\n");
2142 error = bus_dmamem_alloc(st->stats_dtag, (void **)&st->stats,
2143 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2146 device_printf(sc->sc_dev, "can't allocate txstats DMA mem\n");
2147 bus_dma_tag_destroy(st->stats_dtag);
2148 st->stats_dtag = NULL;
2152 error = bus_dmamap_load(st->stats_dtag, st->stats_dmap, st->stats,
2153 dma_size, bwi_dma_ring_addr, &st->stats_paddr,
2156 device_printf(sc->sc_dev, "can't load txstats DMA mem\n");
2157 bus_dmamem_free(st->stats_dtag, st->stats, st->stats_dmap);
2158 bus_dma_tag_destroy(st->stats_dtag);
2159 st->stats_dtag = NULL;
2163 st->stats_ctrl_base = ctrl_base;
2168 bwi_dma_txstats_free(struct bwi_softc *sc)
2170 struct bwi_txstats_data *st;
2172 if (sc->sc_txstats == NULL)
2174 st = sc->sc_txstats;
2176 if (st->stats_ring_dtag != NULL) {
2177 bus_dmamap_unload(st->stats_ring_dtag, st->stats_ring_dmap);
2178 bus_dmamem_free(st->stats_ring_dtag, st->stats_ring,
2179 st->stats_ring_dmap);
2180 bus_dma_tag_destroy(st->stats_ring_dtag);
2183 if (st->stats_dtag != NULL) {
2184 bus_dmamap_unload(st->stats_dtag, st->stats_dmap);
2185 bus_dmamem_free(st->stats_dtag, st->stats, st->stats_dmap);
2186 bus_dma_tag_destroy(st->stats_dtag);
2193 bwi_dma_ring_addr(void *arg, bus_dma_segment_t *seg, int nseg, int error)
2195 KASSERT(nseg == 1, ("too many segments\n"));
2196 *((bus_addr_t *)arg) = seg->ds_addr;
2200 bwi_dma_mbuf_create(struct bwi_softc *sc)
2202 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2203 int i, j, k, ntx, error;
2206 * Create TX/RX mbuf DMA tag
2208 error = bus_dma_tag_create(sc->sc_parent_dtag,
2221 device_printf(sc->sc_dev, "can't create mbuf DMA tag\n");
2228 * Create TX mbuf DMA map
2230 for (i = 0; i < BWI_TX_NRING; ++i) {
2231 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[i];
2233 for (j = 0; j < BWI_TX_NDESC; ++j) {
2234 error = bus_dmamap_create(sc->sc_buf_dtag, 0,
2235 &tbd->tbd_buf[j].tb_dmap);
2237 device_printf(sc->sc_dev, "can't create "
2238 "%dth tbd, %dth DMA map\n", i, j);
2241 for (k = 0; k < j; ++k) {
2242 bus_dmamap_destroy(sc->sc_buf_dtag,
2243 tbd->tbd_buf[k].tb_dmap);
2252 * Create RX mbuf DMA map and a spare DMA map
2254 error = bus_dmamap_create(sc->sc_buf_dtag, 0,
2255 &rbd->rbd_tmp_dmap);
2257 device_printf(sc->sc_dev,
2258 "can't create spare RX buf DMA map\n");
2262 for (j = 0; j < BWI_RX_NDESC; ++j) {
2263 error = bus_dmamap_create(sc->sc_buf_dtag, 0,
2264 &rbd->rbd_buf[j].rb_dmap);
2266 device_printf(sc->sc_dev, "can't create %dth "
2267 "RX buf DMA map\n", j);
2269 for (k = 0; k < j; ++k) {
2270 bus_dmamap_destroy(sc->sc_buf_dtag,
2271 rbd->rbd_buf[j].rb_dmap);
2273 bus_dmamap_destroy(sc->sc_buf_dtag,
2281 bwi_dma_mbuf_destroy(sc, ntx, 0);
2286 bwi_dma_mbuf_destroy(struct bwi_softc *sc, int ntx, int nrx)
2290 if (sc->sc_buf_dtag == NULL)
2293 for (i = 0; i < ntx; ++i) {
2294 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[i];
2296 for (j = 0; j < BWI_TX_NDESC; ++j) {
2297 struct bwi_txbuf *tb = &tbd->tbd_buf[j];
2299 if (tb->tb_mbuf != NULL) {
2300 bus_dmamap_unload(sc->sc_buf_dtag,
2302 m_freem(tb->tb_mbuf);
2304 if (tb->tb_ni != NULL)
2305 ieee80211_free_node(tb->tb_ni);
2306 bus_dmamap_destroy(sc->sc_buf_dtag, tb->tb_dmap);
2311 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2313 bus_dmamap_destroy(sc->sc_buf_dtag, rbd->rbd_tmp_dmap);
2314 for (j = 0; j < BWI_RX_NDESC; ++j) {
2315 struct bwi_rxbuf *rb = &rbd->rbd_buf[j];
2317 if (rb->rb_mbuf != NULL) {
2318 bus_dmamap_unload(sc->sc_buf_dtag,
2320 m_freem(rb->rb_mbuf);
2322 bus_dmamap_destroy(sc->sc_buf_dtag, rb->rb_dmap);
2326 bus_dma_tag_destroy(sc->sc_buf_dtag);
2327 sc->sc_buf_dtag = NULL;
2331 bwi_enable_intrs(struct bwi_softc *sc, uint32_t enable_intrs)
2333 CSR_SETBITS_4(sc, BWI_MAC_INTR_MASK, enable_intrs);
2337 bwi_disable_intrs(struct bwi_softc *sc, uint32_t disable_intrs)
2339 CSR_CLRBITS_4(sc, BWI_MAC_INTR_MASK, disable_intrs);
2343 bwi_init_tx_ring32(struct bwi_softc *sc, int ring_idx)
2345 struct bwi_ring_data *rd;
2346 struct bwi_txbuf_data *tbd;
2347 uint32_t val, addr_hi, addr_lo;
2349 KASSERT(ring_idx < BWI_TX_NRING, ("ring_idx %d", ring_idx));
2350 rd = &sc->sc_tx_rdata[ring_idx];
2351 tbd = &sc->sc_tx_bdata[ring_idx];
2356 bzero(rd->rdata_desc, sizeof(struct bwi_desc32) * BWI_TX_NDESC);
2357 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap,
2358 BUS_DMASYNC_PREWRITE);
2360 addr_lo = __SHIFTOUT(rd->rdata_paddr, BWI_TXRX32_RINGINFO_ADDR_MASK);
2361 addr_hi = __SHIFTOUT(rd->rdata_paddr, BWI_TXRX32_RINGINFO_FUNC_MASK);
2363 val = __SHIFTIN(addr_lo, BWI_TXRX32_RINGINFO_ADDR_MASK) |
2364 __SHIFTIN(BWI_TXRX32_RINGINFO_FUNC_TXRX,
2365 BWI_TXRX32_RINGINFO_FUNC_MASK);
2366 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_RINGINFO, val);
2368 val = __SHIFTIN(addr_hi, BWI_TXRX32_CTRL_ADDRHI_MASK) |
2369 BWI_TXRX32_CTRL_ENABLE;
2370 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_CTRL, val);
2376 bwi_init_rxdesc_ring32(struct bwi_softc *sc, uint32_t ctrl_base,
2377 bus_addr_t paddr, int hdr_size, int ndesc)
2379 uint32_t val, addr_hi, addr_lo;
2381 addr_lo = __SHIFTOUT(paddr, BWI_TXRX32_RINGINFO_ADDR_MASK);
2382 addr_hi = __SHIFTOUT(paddr, BWI_TXRX32_RINGINFO_FUNC_MASK);
2384 val = __SHIFTIN(addr_lo, BWI_TXRX32_RINGINFO_ADDR_MASK) |
2385 __SHIFTIN(BWI_TXRX32_RINGINFO_FUNC_TXRX,
2386 BWI_TXRX32_RINGINFO_FUNC_MASK);
2387 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_RINGINFO, val);
2389 val = __SHIFTIN(hdr_size, BWI_RX32_CTRL_HDRSZ_MASK) |
2390 __SHIFTIN(addr_hi, BWI_TXRX32_CTRL_ADDRHI_MASK) |
2391 BWI_TXRX32_CTRL_ENABLE;
2392 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_CTRL, val);
2394 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_INDEX,
2395 (ndesc - 1) * sizeof(struct bwi_desc32));
2399 bwi_init_rx_ring32(struct bwi_softc *sc)
2401 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2404 sc->sc_rx_bdata.rbd_idx = 0;
2406 for (i = 0; i < BWI_RX_NDESC; ++i) {
2407 error = bwi_newbuf(sc, i, 1);
2409 device_printf(sc->sc_dev,
2410 "can't allocate %dth RX buffer\n", i);
2414 bus_dmamap_sync(sc->sc_rxring_dtag, rd->rdata_dmap,
2415 BUS_DMASYNC_PREWRITE);
2417 bwi_init_rxdesc_ring32(sc, rd->rdata_txrx_ctrl, rd->rdata_paddr,
2418 sizeof(struct bwi_rxbuf_hdr), BWI_RX_NDESC);
2423 bwi_init_txstats32(struct bwi_softc *sc)
2425 struct bwi_txstats_data *st = sc->sc_txstats;
2426 bus_addr_t stats_paddr;
2429 bzero(st->stats, BWI_TXSTATS_NDESC * sizeof(struct bwi_txstats));
2430 bus_dmamap_sync(st->stats_dtag, st->stats_dmap, BUS_DMASYNC_PREWRITE);
2434 stats_paddr = st->stats_paddr;
2435 for (i = 0; i < BWI_TXSTATS_NDESC; ++i) {
2436 bwi_setup_desc32(sc, st->stats_ring, BWI_TXSTATS_NDESC, i,
2437 stats_paddr, sizeof(struct bwi_txstats), 0);
2438 stats_paddr += sizeof(struct bwi_txstats);
2440 bus_dmamap_sync(st->stats_ring_dtag, st->stats_ring_dmap,
2441 BUS_DMASYNC_PREWRITE);
2443 bwi_init_rxdesc_ring32(sc, st->stats_ctrl_base,
2444 st->stats_ring_paddr, 0, BWI_TXSTATS_NDESC);
2449 bwi_setup_rx_desc32(struct bwi_softc *sc, int buf_idx, bus_addr_t paddr,
2452 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2454 KASSERT(buf_idx < BWI_RX_NDESC, ("buf_idx %d", buf_idx));
2455 bwi_setup_desc32(sc, rd->rdata_desc, BWI_RX_NDESC, buf_idx,
2460 bwi_setup_tx_desc32(struct bwi_softc *sc, struct bwi_ring_data *rd,
2461 int buf_idx, bus_addr_t paddr, int buf_len)
2463 KASSERT(buf_idx < BWI_TX_NDESC, ("buf_idx %d", buf_idx));
2464 bwi_setup_desc32(sc, rd->rdata_desc, BWI_TX_NDESC, buf_idx,
2469 bwi_init_tx_ring64(struct bwi_softc *sc, int ring_idx)
2476 bwi_init_rx_ring64(struct bwi_softc *sc)
2483 bwi_init_txstats64(struct bwi_softc *sc)
2490 bwi_setup_rx_desc64(struct bwi_softc *sc, int buf_idx, bus_addr_t paddr,
2497 bwi_setup_tx_desc64(struct bwi_softc *sc, struct bwi_ring_data *rd,
2498 int buf_idx, bus_addr_t paddr, int buf_len)
2504 bwi_dma_buf_addr(void *arg, bus_dma_segment_t *seg, int nseg,
2505 bus_size_t mapsz __unused, int error)
2508 KASSERT(nseg == 1, ("too many segments(%d)\n", nseg));
2509 *((bus_addr_t *)arg) = seg->ds_addr;
2514 bwi_newbuf(struct bwi_softc *sc, int buf_idx, int init)
2516 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2517 struct bwi_rxbuf *rxbuf = &rbd->rbd_buf[buf_idx];
2518 struct bwi_rxbuf_hdr *hdr;
2524 KASSERT(buf_idx < BWI_RX_NDESC, ("buf_idx %d", buf_idx));
2526 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2531 * If the NIC is up and running, we need to:
2532 * - Clear RX buffer's header.
2533 * - Restore RX descriptor settings.
2540 m->m_len = m->m_pkthdr.len = MCLBYTES;
2543 * Try to load RX buf into temporary DMA map
2545 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, rbd->rbd_tmp_dmap, m,
2546 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT);
2551 * See the comment above
2560 bus_dmamap_unload(sc->sc_buf_dtag, rxbuf->rb_dmap);
2562 rxbuf->rb_paddr = paddr;
2565 * Swap RX buf's DMA map with the loaded temporary one
2567 map = rxbuf->rb_dmap;
2568 rxbuf->rb_dmap = rbd->rbd_tmp_dmap;
2569 rbd->rbd_tmp_dmap = map;
2573 * Clear RX buf header
2575 hdr = mtod(rxbuf->rb_mbuf, struct bwi_rxbuf_hdr *);
2576 bzero(hdr, sizeof(*hdr));
2577 bus_dmamap_sync(sc->sc_buf_dtag, rxbuf->rb_dmap, BUS_DMASYNC_PREWRITE);
2580 * Setup RX buf descriptor
2582 sc->sc_setup_rxdesc(sc, buf_idx, rxbuf->rb_paddr,
2583 rxbuf->rb_mbuf->m_len - sizeof(*hdr));
2588 bwi_set_addr_filter(struct bwi_softc *sc, uint16_t addr_ofs,
2589 const uint8_t *addr)
2593 CSR_WRITE_2(sc, BWI_ADDR_FILTER_CTRL,
2594 BWI_ADDR_FILTER_CTRL_SET | addr_ofs);
2596 for (i = 0; i < (IEEE80211_ADDR_LEN / 2); ++i) {
2599 addr_val = (uint16_t)addr[i * 2] |
2600 (((uint16_t)addr[(i * 2) + 1]) << 8);
2601 CSR_WRITE_2(sc, BWI_ADDR_FILTER_DATA, addr_val);
2606 bwi_rxeof(struct bwi_softc *sc, int end_idx)
2608 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2609 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2610 struct ieee80211com *ic = &sc->sc_ic;
2611 int idx, rx_data = 0;
2614 while (idx != end_idx) {
2615 struct bwi_rxbuf *rb = &rbd->rbd_buf[idx];
2616 struct bwi_rxbuf_hdr *hdr;
2617 struct ieee80211_frame_min *wh;
2618 struct ieee80211_node *ni;
2622 int buflen, wh_ofs, hdr_extra, rssi, noise, type, rate;
2625 bus_dmamap_sync(sc->sc_buf_dtag, rb->rb_dmap,
2626 BUS_DMASYNC_POSTREAD);
2628 if (bwi_newbuf(sc, idx, 0)) {
2629 counter_u64_add(ic->ic_ierrors, 1);
2633 hdr = mtod(m, struct bwi_rxbuf_hdr *);
2634 flags2 = le16toh(hdr->rxh_flags2);
2637 if (flags2 & BWI_RXH_F2_TYPE2FRAME)
2639 wh_ofs = hdr_extra + 6; /* XXX magic number */
2641 buflen = le16toh(hdr->rxh_buflen);
2642 if (buflen < BWI_FRAME_MIN_LEN(wh_ofs)) {
2643 device_printf(sc->sc_dev,
2644 "%s: zero length data, hdr_extra %d\n",
2645 __func__, hdr_extra);
2646 counter_u64_add(ic->ic_ierrors, 1);
2651 bcopy((uint8_t *)(hdr + 1) + hdr_extra, &plcp, sizeof(plcp));
2652 rssi = bwi_calc_rssi(sc, hdr);
2653 noise = bwi_calc_noise(sc);
2655 m->m_len = m->m_pkthdr.len = buflen + sizeof(*hdr);
2656 m_adj(m, sizeof(*hdr) + wh_ofs);
2658 if (htole16(hdr->rxh_flags1) & BWI_RXH_F1_OFDM)
2659 rate = bwi_plcp2rate(plcp, IEEE80211_T_OFDM);
2661 rate = bwi_plcp2rate(plcp, IEEE80211_T_CCK);
2664 if (ieee80211_radiotap_active(ic))
2665 bwi_rx_radiotap(sc, m, hdr, &plcp, rate, rssi, noise);
2667 m_adj(m, -IEEE80211_CRC_LEN);
2671 wh = mtod(m, struct ieee80211_frame_min *);
2672 ni = ieee80211_find_rxnode(ic, wh);
2674 type = ieee80211_input(ni, m, rssi - noise, noise);
2675 ieee80211_free_node(ni);
2677 type = ieee80211_input_all(ic, m, rssi - noise, noise);
2678 if (type == IEEE80211_FC0_TYPE_DATA) {
2680 sc->sc_rx_rate = rate;
2685 idx = (idx + 1) % BWI_RX_NDESC;
2687 if (sc->sc_flags & BWI_F_STOP) {
2689 * Take the fast lane, don't do
2690 * any damage to softc
2697 bus_dmamap_sync(sc->sc_rxring_dtag, rd->rdata_dmap,
2698 BUS_DMASYNC_PREWRITE);
2704 bwi_rxeof32(struct bwi_softc *sc)
2706 uint32_t val, rx_ctrl;
2707 int end_idx, rx_data;
2709 rx_ctrl = sc->sc_rx_rdata.rdata_txrx_ctrl;
2711 val = CSR_READ_4(sc, rx_ctrl + BWI_RX32_STATUS);
2712 end_idx = __SHIFTOUT(val, BWI_RX32_STATUS_INDEX_MASK) /
2713 sizeof(struct bwi_desc32);
2715 rx_data = bwi_rxeof(sc, end_idx);
2717 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_INDEX,
2718 end_idx * sizeof(struct bwi_desc32));
2724 bwi_rxeof64(struct bwi_softc *sc)
2731 bwi_reset_rx_ring32(struct bwi_softc *sc, uint32_t rx_ctrl)
2735 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_CTRL, 0);
2739 for (i = 0; i < NRETRY; ++i) {
2742 status = CSR_READ_4(sc, rx_ctrl + BWI_RX32_STATUS);
2743 if (__SHIFTOUT(status, BWI_RX32_STATUS_STATE_MASK) ==
2744 BWI_RX32_STATUS_STATE_DISABLED)
2750 device_printf(sc->sc_dev, "reset rx ring timedout\n");
2754 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_RINGINFO, 0);
2758 bwi_free_txstats32(struct bwi_softc *sc)
2760 bwi_reset_rx_ring32(sc, sc->sc_txstats->stats_ctrl_base);
2764 bwi_free_rx_ring32(struct bwi_softc *sc)
2766 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2767 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2770 bwi_reset_rx_ring32(sc, rd->rdata_txrx_ctrl);
2772 for (i = 0; i < BWI_RX_NDESC; ++i) {
2773 struct bwi_rxbuf *rb = &rbd->rbd_buf[i];
2775 if (rb->rb_mbuf != NULL) {
2776 bus_dmamap_unload(sc->sc_buf_dtag, rb->rb_dmap);
2777 m_freem(rb->rb_mbuf);
2784 bwi_free_tx_ring32(struct bwi_softc *sc, int ring_idx)
2786 struct bwi_ring_data *rd;
2787 struct bwi_txbuf_data *tbd;
2788 uint32_t state, val;
2791 KASSERT(ring_idx < BWI_TX_NRING, ("ring_idx %d", ring_idx));
2792 rd = &sc->sc_tx_rdata[ring_idx];
2793 tbd = &sc->sc_tx_bdata[ring_idx];
2797 for (i = 0; i < NRETRY; ++i) {
2798 val = CSR_READ_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_STATUS);
2799 state = __SHIFTOUT(val, BWI_TX32_STATUS_STATE_MASK);
2800 if (state == BWI_TX32_STATUS_STATE_DISABLED ||
2801 state == BWI_TX32_STATUS_STATE_IDLE ||
2802 state == BWI_TX32_STATUS_STATE_STOPPED)
2808 device_printf(sc->sc_dev,
2809 "%s: wait for TX ring(%d) stable timed out\n",
2810 __func__, ring_idx);
2813 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_CTRL, 0);
2814 for (i = 0; i < NRETRY; ++i) {
2815 val = CSR_READ_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_STATUS);
2816 state = __SHIFTOUT(val, BWI_TX32_STATUS_STATE_MASK);
2817 if (state == BWI_TX32_STATUS_STATE_DISABLED)
2823 device_printf(sc->sc_dev, "%s: reset TX ring (%d) timed out\n",
2824 __func__, ring_idx);
2830 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_RINGINFO, 0);
2832 for (i = 0; i < BWI_TX_NDESC; ++i) {
2833 struct bwi_txbuf *tb = &tbd->tbd_buf[i];
2835 if (tb->tb_mbuf != NULL) {
2836 bus_dmamap_unload(sc->sc_buf_dtag, tb->tb_dmap);
2837 m_freem(tb->tb_mbuf);
2840 if (tb->tb_ni != NULL) {
2841 ieee80211_free_node(tb->tb_ni);
2848 bwi_free_txstats64(struct bwi_softc *sc)
2854 bwi_free_rx_ring64(struct bwi_softc *sc)
2860 bwi_free_tx_ring64(struct bwi_softc *sc, int ring_idx)
2865 /* XXX does not belong here */
2866 #define IEEE80211_OFDM_PLCP_RATE_MASK __BITS(3, 0)
2867 #define IEEE80211_OFDM_PLCP_LEN_MASK __BITS(16, 5)
2869 static __inline void
2870 bwi_ofdm_plcp_header(uint32_t *plcp0, int pkt_len, uint8_t rate)
2874 plcp = __SHIFTIN(ieee80211_rate2plcp(rate, IEEE80211_T_OFDM),
2875 IEEE80211_OFDM_PLCP_RATE_MASK) |
2876 __SHIFTIN(pkt_len, IEEE80211_OFDM_PLCP_LEN_MASK);
2877 *plcp0 = htole32(plcp);
2880 static __inline void
2881 bwi_ds_plcp_header(struct ieee80211_ds_plcp_hdr *plcp, int pkt_len,
2884 int len, service, pkt_bitlen;
2886 pkt_bitlen = pkt_len * NBBY;
2887 len = howmany(pkt_bitlen * 2, rate);
2889 service = IEEE80211_PLCP_SERVICE_LOCKED;
2890 if (rate == (11 * 2)) {
2894 * PLCP service field needs to be adjusted,
2895 * if TX rate is 11Mbytes/s
2897 pkt_bitlen1 = len * 11;
2898 if (pkt_bitlen1 - pkt_bitlen >= NBBY)
2899 service |= IEEE80211_PLCP_SERVICE_LENEXT7;
2902 plcp->i_signal = ieee80211_rate2plcp(rate, IEEE80211_T_CCK);
2903 plcp->i_service = service;
2904 plcp->i_length = htole16(len);
2905 /* NOTE: do NOT touch i_crc */
2908 static __inline void
2909 bwi_plcp_header(const struct ieee80211_rate_table *rt,
2910 void *plcp, int pkt_len, uint8_t rate)
2912 enum ieee80211_phytype modtype;
2915 * Assume caller has zeroed 'plcp'
2917 modtype = ieee80211_rate2phytype(rt, rate);
2918 if (modtype == IEEE80211_T_OFDM)
2919 bwi_ofdm_plcp_header(plcp, pkt_len, rate);
2920 else if (modtype == IEEE80211_T_DS)
2921 bwi_ds_plcp_header(plcp, pkt_len, rate);
2923 panic("unsupport modulation type %u\n", modtype);
2927 bwi_encap(struct bwi_softc *sc, int idx, struct mbuf *m,
2928 struct ieee80211_node *ni)
2930 struct ieee80211vap *vap = ni->ni_vap;
2931 struct ieee80211com *ic = &sc->sc_ic;
2932 struct bwi_ring_data *rd = &sc->sc_tx_rdata[BWI_TX_DATA_RING];
2933 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
2934 struct bwi_txbuf *tb = &tbd->tbd_buf[idx];
2935 struct bwi_mac *mac;
2936 struct bwi_txbuf_hdr *hdr;
2937 struct ieee80211_frame *wh;
2938 const struct ieee80211_txparam *tp = ni->ni_txparms;
2939 uint8_t rate, rate_fb;
2943 int type, ismcast, pkt_len, error, rix;
2949 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
2950 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
2951 mac = (struct bwi_mac *)sc->sc_cur_regwin;
2953 wh = mtod(m, struct ieee80211_frame *);
2954 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2955 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2957 /* Get 802.11 frame len before prepending TX header */
2958 pkt_len = m->m_pkthdr.len + IEEE80211_CRC_LEN;
2963 if (type != IEEE80211_FC0_TYPE_DATA || (m->m_flags & M_EAPOL)) {
2964 rate = rate_fb = tp->mgmtrate;
2965 } else if (ismcast) {
2966 rate = rate_fb = tp->mcastrate;
2967 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
2968 rate = rate_fb = tp->ucastrate;
2970 rix = ieee80211_ratectl_rate(ni, NULL, pkt_len);
2971 rate = ni->ni_txrate;
2974 rate_fb = ni->ni_rates.rs_rates[rix-1] &
2980 tb->tb_rate[0] = rate;
2981 tb->tb_rate[1] = rate_fb;
2982 sc->sc_tx_rate = rate;
2987 if (ieee80211_radiotap_active_vap(vap)) {
2988 sc->sc_tx_th.wt_flags = 0;
2989 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)
2990 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2991 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_DS &&
2992 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2994 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2996 sc->sc_tx_th.wt_rate = rate;
2998 ieee80211_radiotap_tx(vap, m);
3002 * Setup the embedded TX header
3004 M_PREPEND(m, sizeof(*hdr), M_NOWAIT);
3006 device_printf(sc->sc_dev, "%s: prepend TX header failed\n",
3010 hdr = mtod(m, struct bwi_txbuf_hdr *);
3012 bzero(hdr, sizeof(*hdr));
3014 bcopy(wh->i_fc, hdr->txh_fc, sizeof(hdr->txh_fc));
3015 bcopy(wh->i_addr1, hdr->txh_addr1, sizeof(hdr->txh_addr1));
3020 dur = ieee80211_ack_duration(sc->sc_rates, rate,
3021 ic->ic_flags & ~IEEE80211_F_SHPREAMBLE);
3023 hdr->txh_fb_duration = htole16(dur);
3026 hdr->txh_id = __SHIFTIN(BWI_TX_DATA_RING, BWI_TXH_ID_RING_MASK) |
3027 __SHIFTIN(idx, BWI_TXH_ID_IDX_MASK);
3029 bwi_plcp_header(sc->sc_rates, hdr->txh_plcp, pkt_len, rate);
3030 bwi_plcp_header(sc->sc_rates, hdr->txh_fb_plcp, pkt_len, rate_fb);
3032 phy_ctrl = __SHIFTIN(mac->mac_rf.rf_ant_mode,
3033 BWI_TXH_PHY_C_ANTMODE_MASK);
3034 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM)
3035 phy_ctrl |= BWI_TXH_PHY_C_OFDM;
3036 else if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && rate != (2 * 1))
3037 phy_ctrl |= BWI_TXH_PHY_C_SHPREAMBLE;
3039 mac_ctrl = BWI_TXH_MAC_C_HWSEQ | BWI_TXH_MAC_C_FIRST_FRAG;
3041 mac_ctrl |= BWI_TXH_MAC_C_ACK;
3042 if (ieee80211_rate2phytype(sc->sc_rates, rate_fb) == IEEE80211_T_OFDM)
3043 mac_ctrl |= BWI_TXH_MAC_C_FB_OFDM;
3045 hdr->txh_mac_ctrl = htole32(mac_ctrl);
3046 hdr->txh_phy_ctrl = htole16(phy_ctrl);
3048 /* Catch any further usage */
3053 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3054 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT);
3055 if (error && error != EFBIG) {
3056 device_printf(sc->sc_dev, "%s: can't load TX buffer (1) %d\n",
3061 if (error) { /* error == EFBIG */
3064 m_new = m_defrag(m, M_NOWAIT);
3065 if (m_new == NULL) {
3066 device_printf(sc->sc_dev,
3067 "%s: can't defrag TX buffer\n", __func__);
3074 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3075 bwi_dma_buf_addr, &paddr,
3078 device_printf(sc->sc_dev,
3079 "%s: can't load TX buffer (2) %d\n",
3086 bus_dmamap_sync(sc->sc_buf_dtag, tb->tb_dmap, BUS_DMASYNC_PREWRITE);
3092 p = mtod(m, const uint8_t *);
3093 for (i = 0; i < m->m_pkthdr.len; ++i) {
3094 if (i != 0 && i % 8 == 0)
3096 printf("%02x ", p[i]);
3100 DPRINTF(sc, BWI_DBG_TX, "idx %d, pkt_len %d, buflen %d\n",
3101 idx, pkt_len, m->m_pkthdr.len);
3103 /* Setup TX descriptor */
3104 sc->sc_setup_txdesc(sc, rd, idx, paddr, m->m_pkthdr.len);
3105 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap,
3106 BUS_DMASYNC_PREWRITE);
3109 sc->sc_start_tx(sc, rd->rdata_txrx_ctrl, idx);
3118 bwi_encap_raw(struct bwi_softc *sc, int idx, struct mbuf *m,
3119 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
3121 struct ieee80211vap *vap = ni->ni_vap;
3122 struct ieee80211com *ic = ni->ni_ic;
3123 struct bwi_ring_data *rd = &sc->sc_tx_rdata[BWI_TX_DATA_RING];
3124 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
3125 struct bwi_txbuf *tb = &tbd->tbd_buf[idx];
3126 struct bwi_mac *mac;
3127 struct bwi_txbuf_hdr *hdr;
3128 struct ieee80211_frame *wh;
3129 uint8_t rate, rate_fb;
3133 int ismcast, pkt_len, error;
3135 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3136 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3137 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3139 wh = mtod(m, struct ieee80211_frame *);
3140 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
3142 /* Get 802.11 frame len before prepending TX header */
3143 pkt_len = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3148 rate = params->ibp_rate0;
3149 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3150 /* XXX fall back to mcast/mgmt rate? */
3154 if (params->ibp_try1 != 0) {
3155 rate_fb = params->ibp_rate1;
3156 if (!ieee80211_isratevalid(ic->ic_rt, rate_fb)) {
3157 /* XXX fall back to rate0? */
3163 tb->tb_rate[0] = rate;
3164 tb->tb_rate[1] = rate_fb;
3165 sc->sc_tx_rate = rate;
3170 if (ieee80211_radiotap_active_vap(vap)) {
3171 sc->sc_tx_th.wt_flags = 0;
3172 /* XXX IEEE80211_BPF_CRYPTO */
3173 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)
3174 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3175 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
3176 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3177 sc->sc_tx_th.wt_rate = rate;
3179 ieee80211_radiotap_tx(vap, m);
3183 * Setup the embedded TX header
3185 M_PREPEND(m, sizeof(*hdr), M_NOWAIT);
3187 device_printf(sc->sc_dev, "%s: prepend TX header failed\n",
3191 hdr = mtod(m, struct bwi_txbuf_hdr *);
3193 bzero(hdr, sizeof(*hdr));
3195 bcopy(wh->i_fc, hdr->txh_fc, sizeof(hdr->txh_fc));
3196 bcopy(wh->i_addr1, hdr->txh_addr1, sizeof(hdr->txh_addr1));
3198 mac_ctrl = BWI_TXH_MAC_C_HWSEQ | BWI_TXH_MAC_C_FIRST_FRAG;
3199 if (!ismcast && (params->ibp_flags & IEEE80211_BPF_NOACK) == 0) {
3202 dur = ieee80211_ack_duration(sc->sc_rates, rate_fb, 0);
3204 hdr->txh_fb_duration = htole16(dur);
3205 mac_ctrl |= BWI_TXH_MAC_C_ACK;
3208 hdr->txh_id = __SHIFTIN(BWI_TX_DATA_RING, BWI_TXH_ID_RING_MASK) |
3209 __SHIFTIN(idx, BWI_TXH_ID_IDX_MASK);
3211 bwi_plcp_header(sc->sc_rates, hdr->txh_plcp, pkt_len, rate);
3212 bwi_plcp_header(sc->sc_rates, hdr->txh_fb_plcp, pkt_len, rate_fb);
3214 phy_ctrl = __SHIFTIN(mac->mac_rf.rf_ant_mode,
3215 BWI_TXH_PHY_C_ANTMODE_MASK);
3216 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM) {
3217 phy_ctrl |= BWI_TXH_PHY_C_OFDM;
3218 mac_ctrl |= BWI_TXH_MAC_C_FB_OFDM;
3219 } else if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
3220 phy_ctrl |= BWI_TXH_PHY_C_SHPREAMBLE;
3222 hdr->txh_mac_ctrl = htole32(mac_ctrl);
3223 hdr->txh_phy_ctrl = htole16(phy_ctrl);
3225 /* Catch any further usage */
3230 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3231 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT);
3235 if (error != EFBIG) {
3236 device_printf(sc->sc_dev,
3237 "%s: can't load TX buffer (1) %d\n",
3241 m_new = m_defrag(m, M_NOWAIT);
3242 if (m_new == NULL) {
3243 device_printf(sc->sc_dev,
3244 "%s: can't defrag TX buffer\n", __func__);
3249 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3250 bwi_dma_buf_addr, &paddr,
3253 device_printf(sc->sc_dev,
3254 "%s: can't load TX buffer (2) %d\n",
3260 bus_dmamap_sync(sc->sc_buf_dtag, tb->tb_dmap, BUS_DMASYNC_PREWRITE);
3265 DPRINTF(sc, BWI_DBG_TX, "idx %d, pkt_len %d, buflen %d\n",
3266 idx, pkt_len, m->m_pkthdr.len);
3268 /* Setup TX descriptor */
3269 sc->sc_setup_txdesc(sc, rd, idx, paddr, m->m_pkthdr.len);
3270 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap,
3271 BUS_DMASYNC_PREWRITE);
3274 sc->sc_start_tx(sc, rd->rdata_txrx_ctrl, idx);
3282 bwi_start_tx32(struct bwi_softc *sc, uint32_t tx_ctrl, int idx)
3284 idx = (idx + 1) % BWI_TX_NDESC;
3285 CSR_WRITE_4(sc, tx_ctrl + BWI_TX32_INDEX,
3286 idx * sizeof(struct bwi_desc32));
3290 bwi_start_tx64(struct bwi_softc *sc, uint32_t tx_ctrl, int idx)
3296 bwi_txeof_status32(struct bwi_softc *sc)
3298 uint32_t val, ctrl_base;
3301 ctrl_base = sc->sc_txstats->stats_ctrl_base;
3303 val = CSR_READ_4(sc, ctrl_base + BWI_RX32_STATUS);
3304 end_idx = __SHIFTOUT(val, BWI_RX32_STATUS_INDEX_MASK) /
3305 sizeof(struct bwi_desc32);
3307 bwi_txeof_status(sc, end_idx);
3309 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_INDEX,
3310 end_idx * sizeof(struct bwi_desc32));
3312 bwi_start_locked(sc);
3316 bwi_txeof_status64(struct bwi_softc *sc)
3322 _bwi_txeof(struct bwi_softc *sc, uint16_t tx_id, int acked, int data_txcnt)
3324 struct bwi_txbuf_data *tbd;
3325 struct bwi_txbuf *tb;
3326 int ring_idx, buf_idx;
3327 struct ieee80211_node *ni;
3330 device_printf(sc->sc_dev, "%s: zero tx id\n", __func__);
3334 ring_idx = __SHIFTOUT(tx_id, BWI_TXH_ID_RING_MASK);
3335 buf_idx = __SHIFTOUT(tx_id, BWI_TXH_ID_IDX_MASK);
3337 KASSERT(ring_idx == BWI_TX_DATA_RING, ("ring_idx %d", ring_idx));
3338 KASSERT(buf_idx < BWI_TX_NDESC, ("buf_idx %d", buf_idx));
3340 tbd = &sc->sc_tx_bdata[ring_idx];
3341 KASSERT(tbd->tbd_used > 0, ("tbd_used %d", tbd->tbd_used));
3344 tb = &tbd->tbd_buf[buf_idx];
3345 DPRINTF(sc, BWI_DBG_TXEOF, "txeof idx %d, "
3346 "acked %d, data_txcnt %d, ni %p\n",
3347 buf_idx, acked, data_txcnt, tb->tb_ni);
3349 bus_dmamap_unload(sc->sc_buf_dtag, tb->tb_dmap);
3351 if ((ni = tb->tb_ni) != NULL) {
3352 const struct bwi_txbuf_hdr *hdr =
3353 mtod(tb->tb_mbuf, const struct bwi_txbuf_hdr *);
3354 struct ieee80211_ratectl_tx_status txs;
3356 /* NB: update rate control only for unicast frames */
3357 if (hdr->txh_mac_ctrl & htole32(BWI_TXH_MAC_C_ACK)) {
3359 * Feed back 'acked and data_txcnt'. Note that the
3360 * generic AMRR code only understands one tx rate
3361 * and the estimator doesn't handle real retry counts
3362 * well so to avoid over-aggressive downshifting we
3363 * treat any number of retries as "1".
3365 txs.flags = IEEE80211_RATECTL_STATUS_LONG_RETRY;
3366 txs.long_retries = acked;
3368 txs.status = IEEE80211_RATECTL_TX_SUCCESS;
3371 IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
3373 ieee80211_ratectl_tx_complete(ni, &txs);
3375 ieee80211_tx_complete(ni, tb->tb_mbuf, !acked);
3378 m_freem(tb->tb_mbuf);
3381 if (tbd->tbd_used == 0)
3382 sc->sc_tx_timer = 0;
3386 bwi_txeof_status(struct bwi_softc *sc, int end_idx)
3388 struct bwi_txstats_data *st = sc->sc_txstats;
3391 bus_dmamap_sync(st->stats_dtag, st->stats_dmap, BUS_DMASYNC_POSTREAD);
3393 idx = st->stats_idx;
3394 while (idx != end_idx) {
3395 const struct bwi_txstats *stats = &st->stats[idx];
3397 if ((stats->txs_flags & BWI_TXS_F_PENDING) == 0) {
3400 data_txcnt = __SHIFTOUT(stats->txs_txcnt,
3401 BWI_TXS_TXCNT_DATA);
3402 _bwi_txeof(sc, le16toh(stats->txs_id),
3403 stats->txs_flags & BWI_TXS_F_ACKED,
3406 idx = (idx + 1) % BWI_TXSTATS_NDESC;
3408 st->stats_idx = idx;
3412 bwi_txeof(struct bwi_softc *sc)
3416 uint32_t tx_status0, tx_status1;
3420 tx_status0 = CSR_READ_4(sc, BWI_TXSTATUS0);
3421 if ((tx_status0 & BWI_TXSTATUS0_VALID) == 0)
3423 tx_status1 = CSR_READ_4(sc, BWI_TXSTATUS1);
3425 tx_id = __SHIFTOUT(tx_status0, BWI_TXSTATUS0_TXID_MASK);
3426 data_txcnt = __SHIFTOUT(tx_status0,
3427 BWI_TXSTATUS0_DATA_TXCNT_MASK);
3429 if (tx_status0 & (BWI_TXSTATUS0_AMPDU | BWI_TXSTATUS0_PENDING))
3432 _bwi_txeof(sc, le16toh(tx_id), tx_status0 & BWI_TXSTATUS0_ACKED,
3436 bwi_start_locked(sc);
3440 bwi_bbp_power_on(struct bwi_softc *sc, enum bwi_clock_mode clk_mode)
3442 bwi_power_on(sc, 1);
3443 return bwi_set_clock_mode(sc, clk_mode);
3447 bwi_bbp_power_off(struct bwi_softc *sc)
3449 bwi_set_clock_mode(sc, BWI_CLOCK_MODE_SLOW);
3450 bwi_power_off(sc, 1);
3454 bwi_get_pwron_delay(struct bwi_softc *sc)
3456 struct bwi_regwin *com, *old;
3457 struct bwi_clock_freq freq;
3461 com = &sc->sc_com_regwin;
3462 KASSERT(BWI_REGWIN_EXIST(com), ("no regwin"));
3464 if ((sc->sc_cap & BWI_CAP_CLKMODE) == 0)
3467 error = bwi_regwin_switch(sc, com, &old);
3471 bwi_get_clock_freq(sc, &freq);
3473 val = CSR_READ_4(sc, BWI_PLL_ON_DELAY);
3474 sc->sc_pwron_delay = howmany((val + 2) * 1000000, freq.clkfreq_min);
3475 DPRINTF(sc, BWI_DBG_ATTACH, "power on delay %u\n", sc->sc_pwron_delay);
3477 return bwi_regwin_switch(sc, old, NULL);
3481 bwi_bus_attach(struct bwi_softc *sc)
3483 struct bwi_regwin *bus, *old;
3486 bus = &sc->sc_bus_regwin;
3488 error = bwi_regwin_switch(sc, bus, &old);
3492 if (!bwi_regwin_is_enabled(sc, bus))
3493 bwi_regwin_enable(sc, bus, 0);
3495 /* Disable interripts */
3496 CSR_WRITE_4(sc, BWI_INTRVEC, 0);
3498 return bwi_regwin_switch(sc, old, NULL);
3502 bwi_regwin_name(const struct bwi_regwin *rw)
3504 switch (rw->rw_type) {
3505 case BWI_REGWIN_T_COM:
3507 case BWI_REGWIN_T_BUSPCI:
3509 case BWI_REGWIN_T_MAC:
3511 case BWI_REGWIN_T_BUSPCIE:
3514 panic("unknown regwin type 0x%04x\n", rw->rw_type);
3519 bwi_regwin_disable_bits(struct bwi_softc *sc)
3523 /* XXX cache this */
3524 busrev = __SHIFTOUT(CSR_READ_4(sc, BWI_ID_LO), BWI_ID_LO_BUSREV_MASK);
3525 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT | BWI_DBG_MISC,
3526 "bus rev %u\n", busrev);
3528 if (busrev == BWI_BUSREV_0)
3529 return BWI_STATE_LO_DISABLE1;
3530 else if (busrev == BWI_BUSREV_1)
3531 return BWI_STATE_LO_DISABLE2;
3533 return (BWI_STATE_LO_DISABLE1 | BWI_STATE_LO_DISABLE2);
3537 bwi_regwin_is_enabled(struct bwi_softc *sc, struct bwi_regwin *rw)
3539 uint32_t val, disable_bits;
3541 disable_bits = bwi_regwin_disable_bits(sc);
3542 val = CSR_READ_4(sc, BWI_STATE_LO);
3544 if ((val & (BWI_STATE_LO_CLOCK |
3545 BWI_STATE_LO_RESET |
3546 disable_bits)) == BWI_STATE_LO_CLOCK) {
3547 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT, "%s is enabled\n",
3548 bwi_regwin_name(rw));
3551 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT, "%s is disabled\n",
3552 bwi_regwin_name(rw));
3558 bwi_regwin_disable(struct bwi_softc *sc, struct bwi_regwin *rw, uint32_t flags)
3560 uint32_t state_lo, disable_bits;
3563 state_lo = CSR_READ_4(sc, BWI_STATE_LO);
3566 * If current regwin is in 'reset' state, it was already disabled.
3568 if (state_lo & BWI_STATE_LO_RESET) {
3569 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT,
3570 "%s was already disabled\n", bwi_regwin_name(rw));
3574 disable_bits = bwi_regwin_disable_bits(sc);
3577 * Disable normal clock
3579 state_lo = BWI_STATE_LO_CLOCK | disable_bits;
3580 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3583 * Wait until normal clock is disabled
3586 for (i = 0; i < NRETRY; ++i) {
3587 state_lo = CSR_READ_4(sc, BWI_STATE_LO);
3588 if (state_lo & disable_bits)
3593 device_printf(sc->sc_dev, "%s disable clock timeout\n",
3594 bwi_regwin_name(rw));
3597 for (i = 0; i < NRETRY; ++i) {
3600 state_hi = CSR_READ_4(sc, BWI_STATE_HI);
3601 if ((state_hi & BWI_STATE_HI_BUSY) == 0)
3606 device_printf(sc->sc_dev, "%s wait BUSY unset timeout\n",
3607 bwi_regwin_name(rw));
3612 * Reset and disable regwin with gated clock
3614 state_lo = BWI_STATE_LO_RESET | disable_bits |
3615 BWI_STATE_LO_CLOCK | BWI_STATE_LO_GATED_CLOCK |
3616 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3617 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3619 /* Flush pending bus write */
3620 CSR_READ_4(sc, BWI_STATE_LO);
3623 /* Reset and disable regwin */
3624 state_lo = BWI_STATE_LO_RESET | disable_bits |
3625 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3626 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3628 /* Flush pending bus write */
3629 CSR_READ_4(sc, BWI_STATE_LO);
3634 bwi_regwin_enable(struct bwi_softc *sc, struct bwi_regwin *rw, uint32_t flags)
3636 uint32_t state_lo, state_hi, imstate;
3638 bwi_regwin_disable(sc, rw, flags);
3640 /* Reset regwin with gated clock */
3641 state_lo = BWI_STATE_LO_RESET |
3642 BWI_STATE_LO_CLOCK |
3643 BWI_STATE_LO_GATED_CLOCK |
3644 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3645 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3647 /* Flush pending bus write */
3648 CSR_READ_4(sc, BWI_STATE_LO);
3651 state_hi = CSR_READ_4(sc, BWI_STATE_HI);
3652 if (state_hi & BWI_STATE_HI_SERROR)
3653 CSR_WRITE_4(sc, BWI_STATE_HI, 0);
3655 imstate = CSR_READ_4(sc, BWI_IMSTATE);
3656 if (imstate & (BWI_IMSTATE_INBAND_ERR | BWI_IMSTATE_TIMEOUT)) {
3657 imstate &= ~(BWI_IMSTATE_INBAND_ERR | BWI_IMSTATE_TIMEOUT);
3658 CSR_WRITE_4(sc, BWI_IMSTATE, imstate);
3661 /* Enable regwin with gated clock */
3662 state_lo = BWI_STATE_LO_CLOCK |
3663 BWI_STATE_LO_GATED_CLOCK |
3664 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3665 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3667 /* Flush pending bus write */
3668 CSR_READ_4(sc, BWI_STATE_LO);
3671 /* Enable regwin with normal clock */
3672 state_lo = BWI_STATE_LO_CLOCK |
3673 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3674 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3676 /* Flush pending bus write */
3677 CSR_READ_4(sc, BWI_STATE_LO);
3682 bwi_set_bssid(struct bwi_softc *sc, const uint8_t *bssid)
3684 struct bwi_mac *mac;
3685 struct bwi_myaddr_bssid buf;
3690 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3691 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3692 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3694 bwi_set_addr_filter(sc, BWI_ADDR_FILTER_BSSID, bssid);
3696 bcopy(sc->sc_ic.ic_macaddr, buf.myaddr, sizeof(buf.myaddr));
3697 bcopy(bssid, buf.bssid, sizeof(buf.bssid));
3699 n = sizeof(buf) / sizeof(val);
3700 p = (const uint8_t *)&buf;
3701 for (i = 0; i < n; ++i) {
3705 for (j = 0; j < sizeof(val); ++j)
3706 val |= ((uint32_t)(*p++)) << (j * 8);
3708 TMPLT_WRITE_4(mac, 0x20 + (i * sizeof(val)), val);
3713 bwi_updateslot(struct ieee80211com *ic)
3715 struct bwi_softc *sc = ic->ic_softc;
3716 struct bwi_mac *mac;
3719 if (sc->sc_flags & BWI_F_RUNNING) {
3720 DPRINTF(sc, BWI_DBG_80211, "%s\n", __func__);
3722 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3723 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3724 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3726 bwi_mac_updateslot(mac, (ic->ic_flags & IEEE80211_F_SHSLOT));
3732 bwi_calibrate(void *xsc)
3734 struct bwi_softc *sc = xsc;
3735 struct bwi_mac *mac;
3737 BWI_ASSERT_LOCKED(sc);
3739 KASSERT(sc->sc_ic.ic_opmode != IEEE80211_M_MONITOR,
3740 ("opmode %d", sc->sc_ic.ic_opmode));
3742 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3743 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3744 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3746 bwi_mac_calibrate_txpower(mac, sc->sc_txpwrcb_type);
3747 sc->sc_txpwrcb_type = BWI_TXPWR_CALIB;
3749 /* XXX 15 seconds */
3750 callout_reset(&sc->sc_calib_ch, hz * 15, bwi_calibrate, sc);
3754 bwi_calc_rssi(struct bwi_softc *sc, const struct bwi_rxbuf_hdr *hdr)
3756 struct bwi_mac *mac;
3758 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3759 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3760 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3762 return bwi_rf_calc_rssi(mac, hdr);
3766 bwi_calc_noise(struct bwi_softc *sc)
3768 struct bwi_mac *mac;
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 return bwi_rf_calc_noise(mac);
3777 static __inline uint8_t
3778 bwi_plcp2rate(const uint32_t plcp0, enum ieee80211_phytype type)
3780 uint32_t plcp = le32toh(plcp0) & IEEE80211_OFDM_PLCP_RATE_MASK;
3781 return (ieee80211_plcp2rate(plcp, type));
3785 bwi_rx_radiotap(struct bwi_softc *sc, struct mbuf *m,
3786 struct bwi_rxbuf_hdr *hdr, const void *plcp, int rate, int rssi, int noise)
3788 const struct ieee80211_frame_min *wh;
3790 sc->sc_rx_th.wr_flags = IEEE80211_RADIOTAP_F_FCS;
3791 if (htole16(hdr->rxh_flags1) & BWI_RXH_F1_SHPREAMBLE)
3792 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3794 wh = mtod(m, const struct ieee80211_frame_min *);
3795 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)
3796 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_WEP;
3798 sc->sc_rx_th.wr_tsf = hdr->rxh_tsf; /* No endian conversion */
3799 sc->sc_rx_th.wr_rate = rate;
3800 sc->sc_rx_th.wr_antsignal = rssi;
3801 sc->sc_rx_th.wr_antnoise = noise;
3805 bwi_led_attach(struct bwi_softc *sc)
3807 const uint8_t *led_act = NULL;
3808 uint16_t gpio, val[BWI_LED_MAX];
3811 for (i = 0; i < nitems(bwi_vendor_led_act); ++i) {
3812 if (sc->sc_pci_subvid == bwi_vendor_led_act[i].vid) {
3813 led_act = bwi_vendor_led_act[i].led_act;
3817 if (led_act == NULL)
3818 led_act = bwi_default_led_act;
3820 gpio = bwi_read_sprom(sc, BWI_SPROM_GPIO01);
3821 val[0] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_0);
3822 val[1] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_1);
3824 gpio = bwi_read_sprom(sc, BWI_SPROM_GPIO23);
3825 val[2] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_2);
3826 val[3] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_3);
3828 for (i = 0; i < BWI_LED_MAX; ++i) {
3829 struct bwi_led *led = &sc->sc_leds[i];
3831 if (val[i] == 0xff) {
3832 led->l_act = led_act[i];
3834 if (val[i] & BWI_LED_ACT_LOW)
3835 led->l_flags |= BWI_LED_F_ACTLOW;
3836 led->l_act = __SHIFTOUT(val[i], BWI_LED_ACT_MASK);
3838 led->l_mask = (1 << i);
3840 if (led->l_act == BWI_LED_ACT_BLINK_SLOW ||
3841 led->l_act == BWI_LED_ACT_BLINK_POLL ||
3842 led->l_act == BWI_LED_ACT_BLINK) {
3843 led->l_flags |= BWI_LED_F_BLINK;
3844 if (led->l_act == BWI_LED_ACT_BLINK_POLL)
3845 led->l_flags |= BWI_LED_F_POLLABLE;
3846 else if (led->l_act == BWI_LED_ACT_BLINK_SLOW)
3847 led->l_flags |= BWI_LED_F_SLOW;
3849 if (sc->sc_blink_led == NULL) {
3850 sc->sc_blink_led = led;
3851 if (led->l_flags & BWI_LED_F_SLOW)
3852 BWI_LED_SLOWDOWN(sc->sc_led_idle);
3856 DPRINTF(sc, BWI_DBG_LED | BWI_DBG_ATTACH,
3857 "%dth led, act %d, lowact %d\n", i,
3858 led->l_act, led->l_flags & BWI_LED_F_ACTLOW);
3860 callout_init_mtx(&sc->sc_led_blink_ch, &sc->sc_mtx, 0);
3863 static __inline uint16_t
3864 bwi_led_onoff(const struct bwi_led *led, uint16_t val, int on)
3866 if (led->l_flags & BWI_LED_F_ACTLOW)
3871 val &= ~led->l_mask;
3876 bwi_led_newstate(struct bwi_softc *sc, enum ieee80211_state nstate)
3878 struct ieee80211com *ic = &sc->sc_ic;
3882 if (nstate == IEEE80211_S_INIT) {
3883 callout_stop(&sc->sc_led_blink_ch);
3884 sc->sc_led_blinking = 0;
3887 if ((sc->sc_flags & BWI_F_RUNNING) == 0)
3890 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL);
3891 for (i = 0; i < BWI_LED_MAX; ++i) {
3892 struct bwi_led *led = &sc->sc_leds[i];
3895 if (led->l_act == BWI_LED_ACT_UNKN ||
3896 led->l_act == BWI_LED_ACT_NULL)
3899 if ((led->l_flags & BWI_LED_F_BLINK) &&
3900 nstate != IEEE80211_S_INIT)
3903 switch (led->l_act) {
3904 case BWI_LED_ACT_ON: /* Always on */
3907 case BWI_LED_ACT_OFF: /* Always off */
3908 case BWI_LED_ACT_5GHZ: /* TODO: 11A */
3914 case IEEE80211_S_INIT:
3917 case IEEE80211_S_RUN:
3918 if (led->l_act == BWI_LED_ACT_11G &&
3919 ic->ic_curmode != IEEE80211_MODE_11G)
3923 if (led->l_act == BWI_LED_ACT_ASSOC)
3930 val = bwi_led_onoff(led, val, on);
3932 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val);
3935 bwi_led_event(struct bwi_softc *sc, int event)
3937 struct bwi_led *led = sc->sc_blink_led;
3940 if (event == BWI_LED_EVENT_POLL) {
3941 if ((led->l_flags & BWI_LED_F_POLLABLE) == 0)
3943 if (ticks - sc->sc_led_ticks < sc->sc_led_idle)
3947 sc->sc_led_ticks = ticks;
3948 if (sc->sc_led_blinking)
3952 case BWI_LED_EVENT_RX:
3953 rate = sc->sc_rx_rate;
3955 case BWI_LED_EVENT_TX:
3956 rate = sc->sc_tx_rate;
3958 case BWI_LED_EVENT_POLL:
3962 panic("unknown LED event %d\n", event);
3965 bwi_led_blink_start(sc, bwi_led_duration[rate].on_dur,
3966 bwi_led_duration[rate].off_dur);
3970 bwi_led_blink_start(struct bwi_softc *sc, int on_dur, int off_dur)
3972 struct bwi_led *led = sc->sc_blink_led;
3975 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL);
3976 val = bwi_led_onoff(led, val, 1);
3977 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val);
3979 if (led->l_flags & BWI_LED_F_SLOW) {
3980 BWI_LED_SLOWDOWN(on_dur);
3981 BWI_LED_SLOWDOWN(off_dur);
3984 sc->sc_led_blinking = 1;
3985 sc->sc_led_blink_offdur = off_dur;
3987 callout_reset(&sc->sc_led_blink_ch, on_dur, bwi_led_blink_next, sc);
3991 bwi_led_blink_next(void *xsc)
3993 struct bwi_softc *sc = xsc;
3996 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL);
3997 val = bwi_led_onoff(sc->sc_blink_led, val, 0);
3998 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val);
4000 callout_reset(&sc->sc_led_blink_ch, sc->sc_led_blink_offdur,
4001 bwi_led_blink_end, sc);
4005 bwi_led_blink_end(void *xsc)
4007 struct bwi_softc *sc = xsc;
4008 sc->sc_led_blinking = 0;
4012 bwi_restart(void *xsc, int pending)
4014 struct bwi_softc *sc = xsc;
4016 device_printf(sc->sc_dev, "%s begin, help!\n", __func__);
4018 bwi_init_statechg(sc, 0);
4020 bwi_start_locked(sc);