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] = {
311 #ifdef BWI_DEBUG_VERBOSE
312 static uint32_t bwi_debug = BWI_DBG_ATTACH | BWI_DBG_INIT | BWI_DBG_TXPOWER;
314 static uint32_t bwi_debug;
316 TUNABLE_INT("hw.bwi.debug", (int *)&bwi_debug);
317 #endif /* BWI_DEBUG */
319 static const uint8_t bwi_zero_addr[IEEE80211_ADDR_LEN];
322 bwi_read_sprom(struct bwi_softc *sc, uint16_t ofs)
324 return CSR_READ_2(sc, ofs + BWI_SPROM_START);
328 bwi_setup_desc32(struct bwi_softc *sc, struct bwi_desc32 *desc_array,
329 int ndesc, int desc_idx, bus_addr_t paddr, int buf_len,
332 struct bwi_desc32 *desc = &desc_array[desc_idx];
333 uint32_t ctrl, addr, addr_hi, addr_lo;
335 addr_lo = __SHIFTOUT(paddr, BWI_DESC32_A_ADDR_MASK);
336 addr_hi = __SHIFTOUT(paddr, BWI_DESC32_A_FUNC_MASK);
338 addr = __SHIFTIN(addr_lo, BWI_DESC32_A_ADDR_MASK) |
339 __SHIFTIN(BWI_DESC32_A_FUNC_TXRX, BWI_DESC32_A_FUNC_MASK);
341 ctrl = __SHIFTIN(buf_len, BWI_DESC32_C_BUFLEN_MASK) |
342 __SHIFTIN(addr_hi, BWI_DESC32_C_ADDRHI_MASK);
343 if (desc_idx == ndesc - 1)
344 ctrl |= BWI_DESC32_C_EOR;
347 ctrl |= BWI_DESC32_C_FRAME_START |
348 BWI_DESC32_C_FRAME_END |
352 desc->addr = htole32(addr);
353 desc->ctrl = htole32(ctrl);
357 bwi_attach(struct bwi_softc *sc)
359 struct ieee80211com *ic = &sc->sc_ic;
360 device_t dev = sc->sc_dev;
368 * Initialize taskq and various tasks
370 sc->sc_tq = taskqueue_create("bwi_taskq", M_NOWAIT | M_ZERO,
371 taskqueue_thread_enqueue, &sc->sc_tq);
372 taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq",
373 device_get_nameunit(dev));
374 TASK_INIT(&sc->sc_restart_task, 0, bwi_restart, sc);
375 callout_init_mtx(&sc->sc_calib_ch, &sc->sc_mtx, 0);
376 mbufq_init(&sc->sc_snd, ifqmaxlen);
379 * Initialize sysctl variables
381 sc->sc_fw_version = BWI_FW_VERSION3;
382 sc->sc_led_idle = (2350 * hz) / 1000;
383 sc->sc_led_ticks = ticks - sc->sc_led_idle;
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 error = bwi_mac_fw_alloc(mac);
455 callout_init_mtx(&sc->sc_watchdog_timer, &sc->sc_mtx, 0);
458 * Setup ratesets, phytype, channels and get MAC address
460 if (phy->phy_mode == IEEE80211_MODE_11B ||
461 phy->phy_mode == IEEE80211_MODE_11G) {
462 if (phy->phy_mode == IEEE80211_MODE_11B) {
463 ic->ic_phytype = IEEE80211_T_DS;
465 ic->ic_phytype = IEEE80211_T_OFDM;
468 bwi_get_eaddr(sc, BWI_SPROM_11BG_EADDR, ic->ic_macaddr);
469 if (IEEE80211_IS_MULTICAST(ic->ic_macaddr)) {
470 bwi_get_eaddr(sc, BWI_SPROM_11A_EADDR, ic->ic_macaddr);
471 if (IEEE80211_IS_MULTICAST(ic->ic_macaddr)) {
473 "invalid MAC address: %6D\n",
474 ic->ic_macaddr, ":");
477 } else if (phy->phy_mode == IEEE80211_MODE_11A) {
482 panic("unknown phymode %d\n", phy->phy_mode);
486 sc->sc_locale = __SHIFTOUT(bwi_read_sprom(sc, BWI_SPROM_CARD_INFO),
487 BWI_SPROM_CARD_INFO_LOCALE);
488 DPRINTF(sc, BWI_DBG_ATTACH, "locale: %d\n", sc->sc_locale);
493 bwi_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
496 ic->ic_name = device_get_nameunit(dev);
497 ic->ic_caps = IEEE80211_C_STA |
499 IEEE80211_C_SHPREAMBLE |
503 ic->ic_opmode = IEEE80211_M_STA;
504 ieee80211_ifattach(ic);
506 ic->ic_headroom = sizeof(struct bwi_txbuf_hdr);
508 /* override default methods */
509 ic->ic_vap_create = bwi_vap_create;
510 ic->ic_vap_delete = bwi_vap_delete;
511 ic->ic_raw_xmit = bwi_raw_xmit;
512 ic->ic_updateslot = bwi_updateslot;
513 ic->ic_scan_start = bwi_scan_start;
514 ic->ic_scan_end = bwi_scan_end;
515 ic->ic_getradiocaps = bwi_getradiocaps;
516 ic->ic_set_channel = bwi_set_channel;
517 ic->ic_transmit = bwi_transmit;
518 ic->ic_parent = bwi_parent;
520 sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan);
522 ieee80211_radiotap_attach(ic,
523 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
524 BWI_TX_RADIOTAP_PRESENT,
525 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
526 BWI_RX_RADIOTAP_PRESENT);
531 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
532 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
533 "fw_version", CTLFLAG_RD, &sc->sc_fw_version, 0,
535 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
536 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
537 "led_idle", CTLFLAG_RW, &sc->sc_led_idle, 0,
538 "# ticks before LED enters idle state");
539 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
540 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
541 "led_blink", CTLFLAG_RW, &sc->sc_led_blink, 0,
542 "Allow LED to blink");
543 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
544 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
545 "txpwr_calib", CTLFLAG_RW, &sc->sc_txpwr_calib, 0,
546 "Enable software TX power calibration");
548 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
549 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
550 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "Debug flags");
553 ieee80211_announce(ic);
557 BWI_LOCK_DESTROY(sc);
562 bwi_detach(struct bwi_softc *sc)
564 struct ieee80211com *ic = &sc->sc_ic;
568 callout_drain(&sc->sc_led_blink_ch);
569 callout_drain(&sc->sc_calib_ch);
570 callout_drain(&sc->sc_watchdog_timer);
571 ieee80211_ifdetach(ic);
573 for (i = 0; i < sc->sc_nmac; ++i)
574 bwi_mac_detach(&sc->sc_mac[i]);
576 taskqueue_free(sc->sc_tq);
577 mbufq_drain(&sc->sc_snd);
579 BWI_LOCK_DESTROY(sc);
584 static struct ieee80211vap *
585 bwi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
586 enum ieee80211_opmode opmode, int flags,
587 const uint8_t bssid[IEEE80211_ADDR_LEN],
588 const uint8_t mac[IEEE80211_ADDR_LEN])
591 struct ieee80211vap *vap;
593 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
595 bvp = malloc(sizeof(struct bwi_vap), M_80211_VAP, M_WAITOK | M_ZERO);
597 /* enable s/w bmiss handling for sta mode */
598 ieee80211_vap_setup(ic, vap, name, unit, opmode,
599 flags | IEEE80211_CLONE_NOBEACONS, bssid);
601 /* override default methods */
602 bvp->bv_newstate = vap->iv_newstate;
603 vap->iv_newstate = bwi_newstate;
605 vap->iv_update_beacon = bwi_beacon_update;
607 ieee80211_ratectl_init(vap);
610 ieee80211_vap_attach(vap, bwi_media_change, ieee80211_media_status,
612 ic->ic_opmode = opmode;
617 bwi_vap_delete(struct ieee80211vap *vap)
619 struct bwi_vap *bvp = BWI_VAP(vap);
621 ieee80211_ratectl_deinit(vap);
622 ieee80211_vap_detach(vap);
623 free(bvp, M_80211_VAP);
627 bwi_suspend(struct bwi_softc *sc)
633 bwi_resume(struct bwi_softc *sc)
636 if (sc->sc_ic.ic_nrunning > 0)
641 bwi_shutdown(struct bwi_softc *sc)
648 bwi_power_on(struct bwi_softc *sc, int with_pll)
650 uint32_t gpio_in, gpio_out, gpio_en;
653 gpio_in = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_IN, 4);
654 if (gpio_in & BWI_PCIM_GPIO_PWR_ON)
657 gpio_out = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4);
658 gpio_en = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, 4);
660 gpio_out |= BWI_PCIM_GPIO_PWR_ON;
661 gpio_en |= BWI_PCIM_GPIO_PWR_ON;
663 /* Turn off PLL first */
664 gpio_out |= BWI_PCIM_GPIO_PLL_PWR_OFF;
665 gpio_en |= BWI_PCIM_GPIO_PLL_PWR_OFF;
668 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4);
669 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, gpio_en, 4);
674 gpio_out &= ~BWI_PCIM_GPIO_PLL_PWR_OFF;
675 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4);
680 /* Clear "Signaled Target Abort" */
681 status = pci_read_config(sc->sc_dev, PCIR_STATUS, 2);
682 status &= ~PCIM_STATUS_STABORT;
683 pci_write_config(sc->sc_dev, PCIR_STATUS, status, 2);
687 bwi_power_off(struct bwi_softc *sc, int with_pll)
689 uint32_t gpio_out, gpio_en;
691 pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_IN, 4); /* dummy read */
692 gpio_out = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4);
693 gpio_en = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, 4);
695 gpio_out &= ~BWI_PCIM_GPIO_PWR_ON;
696 gpio_en |= BWI_PCIM_GPIO_PWR_ON;
698 gpio_out |= BWI_PCIM_GPIO_PLL_PWR_OFF;
699 gpio_en |= BWI_PCIM_GPIO_PLL_PWR_OFF;
702 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4);
703 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, gpio_en, 4);
708 bwi_regwin_switch(struct bwi_softc *sc, struct bwi_regwin *rw,
709 struct bwi_regwin **old_rw)
716 if (!BWI_REGWIN_EXIST(rw))
719 if (sc->sc_cur_regwin != rw) {
720 error = bwi_regwin_select(sc, rw->rw_id);
722 device_printf(sc->sc_dev, "can't select regwin %d\n",
729 *old_rw = sc->sc_cur_regwin;
730 sc->sc_cur_regwin = rw;
735 bwi_regwin_select(struct bwi_softc *sc, int id)
737 uint32_t win = BWI_PCIM_REGWIN(id);
741 for (i = 0; i < RETRY_MAX; ++i) {
742 pci_write_config(sc->sc_dev, BWI_PCIR_SEL_REGWIN, win, 4);
743 if (pci_read_config(sc->sc_dev, BWI_PCIR_SEL_REGWIN, 4) == win)
753 bwi_regwin_info(struct bwi_softc *sc, uint16_t *type, uint8_t *rev)
757 val = CSR_READ_4(sc, BWI_ID_HI);
758 *type = BWI_ID_HI_REGWIN_TYPE(val);
759 *rev = BWI_ID_HI_REGWIN_REV(val);
761 DPRINTF(sc, BWI_DBG_ATTACH, "regwin: type 0x%03x, rev %d, "
762 "vendor 0x%04x\n", *type, *rev,
763 __SHIFTOUT(val, BWI_ID_HI_REGWIN_VENDOR_MASK));
767 bwi_bbp_attach(struct bwi_softc *sc)
769 uint16_t bbp_id, rw_type;
772 int error, nregwin, i;
775 * Get 0th regwin information
776 * NOTE: 0th regwin should exist
778 error = bwi_regwin_select(sc, 0);
780 device_printf(sc->sc_dev, "can't select regwin 0\n");
783 bwi_regwin_info(sc, &rw_type, &rw_rev);
790 if (rw_type == BWI_REGWIN_T_COM) {
791 info = CSR_READ_4(sc, BWI_INFO);
792 bbp_id = __SHIFTOUT(info, BWI_INFO_BBPID_MASK);
794 BWI_CREATE_REGWIN(&sc->sc_com_regwin, 0, rw_type, rw_rev);
796 sc->sc_cap = CSR_READ_4(sc, BWI_CAPABILITY);
798 for (i = 0; i < nitems(bwi_bbpid_map); ++i) {
799 if (sc->sc_pci_did >= bwi_bbpid_map[i].did_min &&
800 sc->sc_pci_did <= bwi_bbpid_map[i].did_max) {
801 bbp_id = bwi_bbpid_map[i].bbp_id;
806 device_printf(sc->sc_dev, "no BBP id for device id "
807 "0x%04x\n", sc->sc_pci_did);
811 info = __SHIFTIN(sc->sc_pci_revid, BWI_INFO_BBPREV_MASK) |
812 __SHIFTIN(0, BWI_INFO_BBPPKG_MASK);
816 * Find out number of regwins
819 if (rw_type == BWI_REGWIN_T_COM && rw_rev >= 4) {
820 nregwin = __SHIFTOUT(info, BWI_INFO_NREGWIN_MASK);
822 for (i = 0; i < nitems(bwi_regwin_count); ++i) {
823 if (bwi_regwin_count[i].bbp_id == bbp_id) {
824 nregwin = bwi_regwin_count[i].nregwin;
829 device_printf(sc->sc_dev, "no number of win for "
830 "BBP id 0x%04x\n", bbp_id);
835 /* Record BBP id/rev for later using */
836 sc->sc_bbp_id = bbp_id;
837 sc->sc_bbp_rev = __SHIFTOUT(info, BWI_INFO_BBPREV_MASK);
838 sc->sc_bbp_pkg = __SHIFTOUT(info, BWI_INFO_BBPPKG_MASK);
839 device_printf(sc->sc_dev, "BBP: id 0x%04x, rev 0x%x, pkg %d\n",
840 sc->sc_bbp_id, sc->sc_bbp_rev, sc->sc_bbp_pkg);
842 DPRINTF(sc, BWI_DBG_ATTACH, "nregwin %d, cap 0x%08x\n",
843 nregwin, sc->sc_cap);
846 * Create rest of the regwins
849 /* Don't re-create common regwin, if it is already created */
850 i = BWI_REGWIN_EXIST(&sc->sc_com_regwin) ? 1 : 0;
852 for (; i < nregwin; ++i) {
854 * Get regwin information
856 error = bwi_regwin_select(sc, i);
858 device_printf(sc->sc_dev,
859 "can't select regwin %d\n", i);
862 bwi_regwin_info(sc, &rw_type, &rw_rev);
866 * 1) Bus (PCI/PCIE) regwin
868 * Ignore rest types of regwin
870 if (rw_type == BWI_REGWIN_T_BUSPCI ||
871 rw_type == BWI_REGWIN_T_BUSPCIE) {
872 if (BWI_REGWIN_EXIST(&sc->sc_bus_regwin)) {
873 device_printf(sc->sc_dev,
874 "bus regwin already exists\n");
876 BWI_CREATE_REGWIN(&sc->sc_bus_regwin, i,
879 } else if (rw_type == BWI_REGWIN_T_MAC) {
880 /* XXX ignore return value */
881 bwi_mac_attach(sc, i, rw_rev);
885 /* At least one MAC shold exist */
886 if (!BWI_REGWIN_EXIST(&sc->sc_mac[0].mac_regwin)) {
887 device_printf(sc->sc_dev, "no MAC was found\n");
890 KASSERT(sc->sc_nmac > 0, ("no mac's"));
892 /* Bus regwin must exist */
893 if (!BWI_REGWIN_EXIST(&sc->sc_bus_regwin)) {
894 device_printf(sc->sc_dev, "no bus regwin was found\n");
898 /* Start with first MAC */
899 error = bwi_regwin_switch(sc, &sc->sc_mac[0].mac_regwin, NULL);
907 bwi_bus_init(struct bwi_softc *sc, struct bwi_mac *mac)
909 struct bwi_regwin *old, *bus;
913 bus = &sc->sc_bus_regwin;
914 KASSERT(sc->sc_cur_regwin == &mac->mac_regwin, ("not cur regwin"));
917 * Tell bus to generate requested interrupts
919 if (bus->rw_rev < 6 && bus->rw_type == BWI_REGWIN_T_BUSPCI) {
921 * NOTE: Read BWI_FLAGS from MAC regwin
923 val = CSR_READ_4(sc, BWI_FLAGS);
925 error = bwi_regwin_switch(sc, bus, &old);
929 CSR_SETBITS_4(sc, BWI_INTRVEC, (val & BWI_FLAGS_INTR_MASK));
933 mac_mask = 1 << mac->mac_id;
935 error = bwi_regwin_switch(sc, bus, &old);
939 val = pci_read_config(sc->sc_dev, BWI_PCIR_INTCTL, 4);
940 val |= mac_mask << 8;
941 pci_write_config(sc->sc_dev, BWI_PCIR_INTCTL, val, 4);
944 if (sc->sc_flags & BWI_F_BUS_INITED)
947 if (bus->rw_type == BWI_REGWIN_T_BUSPCI) {
949 * Enable prefetch and burst
951 CSR_SETBITS_4(sc, BWI_BUS_CONFIG,
952 BWI_BUS_CONFIG_PREFETCH | BWI_BUS_CONFIG_BURST);
954 if (bus->rw_rev < 5) {
955 struct bwi_regwin *com = &sc->sc_com_regwin;
958 * Configure timeouts for bus operation
962 * Set service timeout and request timeout
964 CSR_SETBITS_4(sc, BWI_CONF_LO,
965 __SHIFTIN(BWI_CONF_LO_SERVTO, BWI_CONF_LO_SERVTO_MASK) |
966 __SHIFTIN(BWI_CONF_LO_REQTO, BWI_CONF_LO_REQTO_MASK));
969 * If there is common regwin, we switch to that regwin
970 * and switch back to bus regwin once we have done.
972 if (BWI_REGWIN_EXIST(com)) {
973 error = bwi_regwin_switch(sc, com, NULL);
978 /* Let bus know what we have changed */
979 CSR_WRITE_4(sc, BWI_BUS_ADDR, BWI_BUS_ADDR_MAGIC);
980 CSR_READ_4(sc, BWI_BUS_ADDR); /* Flush */
981 CSR_WRITE_4(sc, BWI_BUS_DATA, 0);
982 CSR_READ_4(sc, BWI_BUS_DATA); /* Flush */
984 if (BWI_REGWIN_EXIST(com)) {
985 error = bwi_regwin_switch(sc, bus, NULL);
989 } else if (bus->rw_rev >= 11) {
991 * Enable memory read multiple
993 CSR_SETBITS_4(sc, BWI_BUS_CONFIG, BWI_BUS_CONFIG_MRM);
999 sc->sc_flags |= BWI_F_BUS_INITED;
1001 return bwi_regwin_switch(sc, old, NULL);
1005 bwi_get_card_flags(struct bwi_softc *sc)
1007 #define PCI_VENDOR_APPLE 0x106b
1008 #define PCI_VENDOR_DELL 0x1028
1009 sc->sc_card_flags = bwi_read_sprom(sc, BWI_SPROM_CARD_FLAGS);
1010 if (sc->sc_card_flags == 0xffff)
1011 sc->sc_card_flags = 0;
1013 if (sc->sc_pci_subvid == PCI_VENDOR_DELL &&
1014 sc->sc_bbp_id == BWI_BBPID_BCM4301 &&
1015 sc->sc_pci_revid == 0x74)
1016 sc->sc_card_flags |= BWI_CARD_F_BT_COEXIST;
1018 if (sc->sc_pci_subvid == PCI_VENDOR_APPLE &&
1019 sc->sc_pci_subdid == 0x4e && /* XXX */
1020 sc->sc_pci_revid > 0x40)
1021 sc->sc_card_flags |= BWI_CARD_F_PA_GPIO9;
1023 DPRINTF(sc, BWI_DBG_ATTACH, "card flags 0x%04x\n", sc->sc_card_flags);
1024 #undef PCI_VENDOR_DELL
1025 #undef PCI_VENDOR_APPLE
1029 bwi_get_eaddr(struct bwi_softc *sc, uint16_t eaddr_ofs, uint8_t *eaddr)
1033 for (i = 0; i < 3; ++i) {
1034 *((uint16_t *)eaddr + i) =
1035 htobe16(bwi_read_sprom(sc, eaddr_ofs + 2 * i));
1040 bwi_get_clock_freq(struct bwi_softc *sc, struct bwi_clock_freq *freq)
1042 struct bwi_regwin *com;
1047 bzero(freq, sizeof(*freq));
1048 com = &sc->sc_com_regwin;
1050 KASSERT(BWI_REGWIN_EXIST(com), ("regwin does not exist"));
1051 KASSERT(sc->sc_cur_regwin == com, ("wrong regwin"));
1052 KASSERT(sc->sc_cap & BWI_CAP_CLKMODE, ("wrong clock mode"));
1055 * Calculate clock frequency
1059 if (com->rw_rev < 6) {
1060 val = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4);
1061 if (val & BWI_PCIM_GPIO_OUT_CLKSRC) {
1062 src = BWI_CLKSRC_PCI;
1065 src = BWI_CLKSRC_CS_OSC;
1068 } else if (com->rw_rev < 10) {
1069 val = CSR_READ_4(sc, BWI_CLOCK_CTRL);
1071 src = __SHIFTOUT(val, BWI_CLOCK_CTRL_CLKSRC);
1072 if (src == BWI_CLKSRC_LP_OSC) {
1075 div = (__SHIFTOUT(val, BWI_CLOCK_CTRL_FDIV) + 1) << 2;
1077 /* Unknown source */
1078 if (src >= BWI_CLKSRC_MAX)
1079 src = BWI_CLKSRC_CS_OSC;
1082 val = CSR_READ_4(sc, BWI_CLOCK_INFO);
1084 src = BWI_CLKSRC_CS_OSC;
1085 div = (__SHIFTOUT(val, BWI_CLOCK_INFO_FDIV) + 1) << 2;
1088 KASSERT(src >= 0 && src < BWI_CLKSRC_MAX, ("bad src %d", src));
1089 KASSERT(div != 0, ("div zero"));
1091 DPRINTF(sc, BWI_DBG_ATTACH, "clksrc %s\n",
1092 src == BWI_CLKSRC_PCI ? "PCI" :
1093 (src == BWI_CLKSRC_LP_OSC ? "LP_OSC" : "CS_OSC"));
1095 freq->clkfreq_min = bwi_clkfreq[src].freq_min / div;
1096 freq->clkfreq_max = bwi_clkfreq[src].freq_max / div;
1098 DPRINTF(sc, BWI_DBG_ATTACH, "clkfreq min %u, max %u\n",
1099 freq->clkfreq_min, freq->clkfreq_max);
1103 bwi_set_clock_mode(struct bwi_softc *sc, enum bwi_clock_mode clk_mode)
1105 struct bwi_regwin *old, *com;
1106 uint32_t clk_ctrl, clk_src;
1107 int error, pwr_off = 0;
1109 com = &sc->sc_com_regwin;
1110 if (!BWI_REGWIN_EXIST(com))
1113 if (com->rw_rev >= 10 || com->rw_rev < 6)
1117 * For common regwin whose rev is [6, 10), the chip
1118 * must be capable to change clock mode.
1120 if ((sc->sc_cap & BWI_CAP_CLKMODE) == 0)
1123 error = bwi_regwin_switch(sc, com, &old);
1127 if (clk_mode == BWI_CLOCK_MODE_FAST)
1128 bwi_power_on(sc, 0); /* Don't turn on PLL */
1130 clk_ctrl = CSR_READ_4(sc, BWI_CLOCK_CTRL);
1131 clk_src = __SHIFTOUT(clk_ctrl, BWI_CLOCK_CTRL_CLKSRC);
1134 case BWI_CLOCK_MODE_FAST:
1135 clk_ctrl &= ~BWI_CLOCK_CTRL_SLOW;
1136 clk_ctrl |= BWI_CLOCK_CTRL_IGNPLL;
1138 case BWI_CLOCK_MODE_SLOW:
1139 clk_ctrl |= BWI_CLOCK_CTRL_SLOW;
1141 case BWI_CLOCK_MODE_DYN:
1142 clk_ctrl &= ~(BWI_CLOCK_CTRL_SLOW |
1143 BWI_CLOCK_CTRL_IGNPLL |
1144 BWI_CLOCK_CTRL_NODYN);
1145 if (clk_src != BWI_CLKSRC_CS_OSC) {
1146 clk_ctrl |= BWI_CLOCK_CTRL_NODYN;
1151 CSR_WRITE_4(sc, BWI_CLOCK_CTRL, clk_ctrl);
1154 bwi_power_off(sc, 0); /* Leave PLL as it is */
1156 return bwi_regwin_switch(sc, old, NULL);
1160 bwi_set_clock_delay(struct bwi_softc *sc)
1162 struct bwi_regwin *old, *com;
1165 com = &sc->sc_com_regwin;
1166 if (!BWI_REGWIN_EXIST(com))
1169 error = bwi_regwin_switch(sc, com, &old);
1173 if (sc->sc_bbp_id == BWI_BBPID_BCM4321) {
1174 if (sc->sc_bbp_rev == 0)
1175 CSR_WRITE_4(sc, BWI_CONTROL, BWI_CONTROL_MAGIC0);
1176 else if (sc->sc_bbp_rev == 1)
1177 CSR_WRITE_4(sc, BWI_CONTROL, BWI_CONTROL_MAGIC1);
1180 if (sc->sc_cap & BWI_CAP_CLKMODE) {
1181 if (com->rw_rev >= 10) {
1182 CSR_FILT_SETBITS_4(sc, BWI_CLOCK_INFO, 0xffff, 0x40000);
1184 struct bwi_clock_freq freq;
1186 bwi_get_clock_freq(sc, &freq);
1187 CSR_WRITE_4(sc, BWI_PLL_ON_DELAY,
1188 howmany(freq.clkfreq_max * 150, 1000000));
1189 CSR_WRITE_4(sc, BWI_FREQ_SEL_DELAY,
1190 howmany(freq.clkfreq_max * 15, 1000000));
1194 return bwi_regwin_switch(sc, old, NULL);
1198 bwi_init(struct bwi_softc *sc)
1200 struct ieee80211com *ic = &sc->sc_ic;
1203 bwi_init_statechg(sc, 1);
1206 if (sc->sc_flags & BWI_F_RUNNING)
1207 ieee80211_start_all(ic); /* start all vap's */
1211 bwi_init_statechg(struct bwi_softc *sc, int statechg)
1213 struct bwi_mac *mac;
1216 BWI_ASSERT_LOCKED(sc);
1218 bwi_stop_locked(sc, statechg);
1220 bwi_bbp_power_on(sc, BWI_CLOCK_MODE_FAST);
1224 mac = &sc->sc_mac[0];
1225 error = bwi_regwin_switch(sc, &mac->mac_regwin, NULL);
1227 device_printf(sc->sc_dev, "%s: error %d on regwin switch\n",
1231 error = bwi_mac_init(mac);
1233 device_printf(sc->sc_dev, "%s: error %d on MAC init\n",
1238 bwi_bbp_power_on(sc, BWI_CLOCK_MODE_DYN);
1240 bwi_set_bssid(sc, bwi_zero_addr); /* Clear BSSID */
1241 bwi_set_addr_filter(sc, BWI_ADDR_FILTER_MYADDR, sc->sc_ic.ic_macaddr);
1243 bwi_mac_reset_hwkeys(mac);
1245 if ((mac->mac_flags & BWI_MAC_F_HAS_TXSTATS) == 0) {
1250 * Drain any possible pending TX status
1252 for (i = 0; i < NRETRY; ++i) {
1253 if ((CSR_READ_4(sc, BWI_TXSTATUS0) &
1254 BWI_TXSTATUS0_VALID) == 0)
1256 CSR_READ_4(sc, BWI_TXSTATUS1);
1259 device_printf(sc->sc_dev,
1260 "%s: can't drain TX status\n", __func__);
1264 if (mac->mac_phy.phy_mode == IEEE80211_MODE_11G)
1265 bwi_mac_updateslot(mac, 1);
1268 error = bwi_mac_start(mac);
1270 device_printf(sc->sc_dev, "%s: error %d starting MAC\n",
1275 /* Clear stop flag before enabling interrupt */
1276 sc->sc_flags &= ~BWI_F_STOP;
1277 sc->sc_flags |= BWI_F_RUNNING;
1278 callout_reset(&sc->sc_watchdog_timer, hz, bwi_watchdog, sc);
1281 bwi_enable_intrs(sc, BWI_INIT_INTRS);
1284 bwi_stop_locked(sc, 1);
1288 bwi_parent(struct ieee80211com *ic)
1290 struct bwi_softc *sc = ic->ic_softc;
1294 if (ic->ic_nrunning > 0) {
1295 struct bwi_mac *mac;
1298 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1299 ("current regwin type %d",
1300 sc->sc_cur_regwin->rw_type));
1301 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1303 if (ic->ic_promisc > 0 && (sc->sc_flags & BWI_F_PROMISC) == 0) {
1305 sc->sc_flags |= BWI_F_PROMISC;
1306 } else if (ic->ic_promisc == 0 &&
1307 (sc->sc_flags & BWI_F_PROMISC) != 0) {
1309 sc->sc_flags &= ~BWI_F_PROMISC;
1313 bwi_mac_set_promisc(mac, promisc);
1315 if (ic->ic_nrunning > 0) {
1316 if ((sc->sc_flags & BWI_F_RUNNING) == 0) {
1317 bwi_init_statechg(sc, 1);
1320 } else if (sc->sc_flags & BWI_F_RUNNING)
1321 bwi_stop_locked(sc, 1);
1324 ieee80211_start_all(ic);
1328 bwi_transmit(struct ieee80211com *ic, struct mbuf *m)
1330 struct bwi_softc *sc = ic->ic_softc;
1334 if ((sc->sc_flags & BWI_F_RUNNING) == 0) {
1338 error = mbufq_enqueue(&sc->sc_snd, m);
1343 bwi_start_locked(sc);
1349 bwi_start_locked(struct bwi_softc *sc)
1351 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
1352 struct ieee80211_frame *wh;
1353 struct ieee80211_node *ni;
1357 BWI_ASSERT_LOCKED(sc);
1362 while (tbd->tbd_buf[idx].tb_mbuf == NULL &&
1363 tbd->tbd_used + BWI_TX_NSPRDESC < BWI_TX_NDESC &&
1364 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1365 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1366 wh = mtod(m, struct ieee80211_frame *);
1367 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) != 0 &&
1368 ieee80211_crypto_encap(ni, m) == NULL) {
1369 if_inc_counter(ni->ni_vap->iv_ifp,
1370 IFCOUNTER_OERRORS, 1);
1371 ieee80211_free_node(ni);
1375 if (bwi_encap(sc, idx, m, ni) != 0) {
1376 /* 'm' is freed in bwi_encap() if we reach here */
1378 if_inc_counter(ni->ni_vap->iv_ifp,
1379 IFCOUNTER_OERRORS, 1);
1380 ieee80211_free_node(ni);
1382 counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1387 idx = (idx + 1) % BWI_TX_NDESC;
1392 sc->sc_tx_timer = 5;
1396 bwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1397 const struct ieee80211_bpf_params *params)
1399 struct ieee80211com *ic = ni->ni_ic;
1400 struct bwi_softc *sc = ic->ic_softc;
1402 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
1405 if ((sc->sc_flags & BWI_F_RUNNING) == 0) {
1412 KASSERT(tbd->tbd_buf[idx].tb_mbuf == NULL, ("slot %d not empty", idx));
1413 if (params == NULL) {
1415 * Legacy path; interpret frame contents to decide
1416 * precisely how to send the frame.
1418 error = bwi_encap(sc, idx, m, ni);
1421 * Caller supplied explicit parameters to use in
1422 * sending the frame.
1424 error = bwi_encap_raw(sc, idx, m, ni, params);
1428 tbd->tbd_idx = (idx + 1) % BWI_TX_NDESC;
1429 sc->sc_tx_timer = 5;
1436 bwi_watchdog(void *arg)
1438 struct bwi_softc *sc;
1441 BWI_ASSERT_LOCKED(sc);
1442 if (sc->sc_tx_timer != 0 && --sc->sc_tx_timer == 0) {
1443 device_printf(sc->sc_dev, "watchdog timeout\n");
1444 counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1445 taskqueue_enqueue(sc->sc_tq, &sc->sc_restart_task);
1447 callout_reset(&sc->sc_watchdog_timer, hz, bwi_watchdog, sc);
1451 bwi_stop(struct bwi_softc *sc, int statechg)
1454 bwi_stop_locked(sc, statechg);
1459 bwi_stop_locked(struct bwi_softc *sc, int statechg)
1461 struct bwi_mac *mac;
1462 int i, error, pwr_off = 0;
1464 BWI_ASSERT_LOCKED(sc);
1466 callout_stop(&sc->sc_calib_ch);
1467 callout_stop(&sc->sc_led_blink_ch);
1468 sc->sc_led_blinking = 0;
1469 sc->sc_flags |= BWI_F_STOP;
1471 if (sc->sc_flags & BWI_F_RUNNING) {
1472 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1473 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1474 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1476 bwi_disable_intrs(sc, BWI_ALL_INTRS);
1477 CSR_READ_4(sc, BWI_MAC_INTR_MASK);
1481 for (i = 0; i < sc->sc_nmac; ++i) {
1482 struct bwi_regwin *old_rw;
1484 mac = &sc->sc_mac[i];
1485 if ((mac->mac_flags & BWI_MAC_F_INITED) == 0)
1488 error = bwi_regwin_switch(sc, &mac->mac_regwin, &old_rw);
1492 bwi_mac_shutdown(mac);
1495 bwi_regwin_switch(sc, old_rw, NULL);
1499 bwi_bbp_power_off(sc);
1501 sc->sc_tx_timer = 0;
1502 callout_stop(&sc->sc_watchdog_timer);
1503 sc->sc_flags &= ~BWI_F_RUNNING;
1509 struct bwi_softc *sc = xsc;
1510 struct bwi_mac *mac;
1511 uint32_t intr_status;
1512 uint32_t txrx_intr_status[BWI_TXRX_NRING];
1513 int i, txrx_error, tx = 0, rx_data = -1;
1517 if ((sc->sc_flags & BWI_F_RUNNING) == 0 ||
1518 (sc->sc_flags & BWI_F_STOP)) {
1523 * Get interrupt status
1525 intr_status = CSR_READ_4(sc, BWI_MAC_INTR_STATUS);
1526 if (intr_status == 0xffffffff) { /* Not for us */
1531 DPRINTF(sc, BWI_DBG_INTR, "intr status 0x%08x\n", intr_status);
1533 intr_status &= CSR_READ_4(sc, BWI_MAC_INTR_MASK);
1534 if (intr_status == 0) { /* Nothing is interesting */
1539 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1540 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1541 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1544 DPRINTF(sc, BWI_DBG_INTR, "%s\n", "TX/RX intr");
1545 for (i = 0; i < BWI_TXRX_NRING; ++i) {
1548 if (BWI_TXRX_IS_RX(i))
1549 mask = BWI_TXRX_RX_INTRS;
1551 mask = BWI_TXRX_TX_INTRS;
1553 txrx_intr_status[i] =
1554 CSR_READ_4(sc, BWI_TXRX_INTR_STATUS(i)) & mask;
1556 _DPRINTF(sc, BWI_DBG_INTR, ", %d 0x%08x",
1557 i, txrx_intr_status[i]);
1559 if (txrx_intr_status[i] & BWI_TXRX_INTR_ERROR) {
1560 device_printf(sc->sc_dev,
1561 "%s: intr fatal TX/RX (%d) error 0x%08x\n",
1562 __func__, i, txrx_intr_status[i]);
1566 _DPRINTF(sc, BWI_DBG_INTR, "%s\n", "");
1569 * Acknowledge interrupt
1571 CSR_WRITE_4(sc, BWI_MAC_INTR_STATUS, intr_status);
1573 for (i = 0; i < BWI_TXRX_NRING; ++i)
1574 CSR_WRITE_4(sc, BWI_TXRX_INTR_STATUS(i), txrx_intr_status[i]);
1576 /* Disable all interrupts */
1577 bwi_disable_intrs(sc, BWI_ALL_INTRS);
1580 * http://bcm-specs.sipsolutions.net/Interrupts
1581 * Says for this bit (0x800):
1584 * We got this one while testing things when by accident the
1585 * template ram wasn't set to big endian when it should have
1586 * been after writing the initial values. It keeps on being
1587 * triggered, the only way to stop it seems to shut down the
1590 * Suggesting that we should never get it and if we do we're not
1591 * feeding TX packets into the MAC correctly if we do... Apparently,
1592 * it is valid only on mac version 5 and higher, but I couldn't
1593 * find a reference for that... Since I see them from time to time
1594 * on my card, this suggests an error in the tx path still...
1596 if (intr_status & BWI_INTR_PHY_TXERR) {
1597 if (mac->mac_flags & BWI_MAC_F_PHYE_RESET) {
1598 device_printf(sc->sc_dev, "%s: intr PHY TX error\n",
1600 taskqueue_enqueue(sc->sc_tq, &sc->sc_restart_task);
1607 /* TODO: reset device */
1610 if (intr_status & BWI_INTR_TBTT)
1611 bwi_mac_config_ps(mac);
1613 if (intr_status & BWI_INTR_EO_ATIM)
1614 device_printf(sc->sc_dev, "EO_ATIM\n");
1616 if (intr_status & BWI_INTR_PMQ) {
1618 if ((CSR_READ_4(sc, BWI_MAC_PS_STATUS) & 0x8) == 0)
1621 CSR_WRITE_2(sc, BWI_MAC_PS_STATUS, 0x2);
1624 if (intr_status & BWI_INTR_NOISE)
1625 device_printf(sc->sc_dev, "intr noise\n");
1627 if (txrx_intr_status[0] & BWI_TXRX_INTR_RX) {
1628 rx_data = sc->sc_rxeof(sc);
1629 if (sc->sc_flags & BWI_F_STOP) {
1635 if (txrx_intr_status[3] & BWI_TXRX_INTR_RX) {
1636 sc->sc_txeof_status(sc);
1640 if (intr_status & BWI_INTR_TX_DONE) {
1645 /* Re-enable interrupts */
1646 bwi_enable_intrs(sc, BWI_INIT_INTRS);
1648 if (sc->sc_blink_led != NULL && sc->sc_led_blink) {
1649 int evt = BWI_LED_EVENT_NONE;
1651 if (tx && rx_data > 0) {
1652 if (sc->sc_rx_rate > sc->sc_tx_rate)
1653 evt = BWI_LED_EVENT_RX;
1655 evt = BWI_LED_EVENT_TX;
1657 evt = BWI_LED_EVENT_TX;
1658 } else if (rx_data > 0) {
1659 evt = BWI_LED_EVENT_RX;
1660 } else if (rx_data == 0) {
1661 evt = BWI_LED_EVENT_POLL;
1664 if (evt != BWI_LED_EVENT_NONE)
1665 bwi_led_event(sc, evt);
1672 bwi_scan_start(struct ieee80211com *ic)
1674 struct bwi_softc *sc = ic->ic_softc;
1677 /* Enable MAC beacon promiscuity */
1678 CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PASS_BCN);
1683 bwi_getradiocaps(struct ieee80211com *ic,
1684 int maxchans, int *nchans, struct ieee80211_channel chans[])
1686 struct bwi_softc *sc = ic->ic_softc;
1687 struct bwi_mac *mac;
1688 struct bwi_phy *phy;
1689 uint8_t bands[IEEE80211_MODE_BYTES];
1692 * XXX First MAC is known to exist
1695 mac = &sc->sc_mac[0];
1696 phy = &mac->mac_phy;
1698 memset(bands, 0, sizeof(bands));
1699 switch (phy->phy_mode) {
1700 case IEEE80211_MODE_11G:
1701 setbit(bands, IEEE80211_MODE_11G);
1703 case IEEE80211_MODE_11B:
1704 setbit(bands, IEEE80211_MODE_11B);
1706 case IEEE80211_MODE_11A:
1708 setbit(bands, IEEE80211_MODE_11A);
1709 device_printf(sc->sc_dev, "no 11a support\n");
1712 panic("unknown phymode %d\n", phy->phy_mode);
1715 ieee80211_add_channels_default_2ghz(chans, maxchans, nchans, bands, 0);
1719 bwi_set_channel(struct ieee80211com *ic)
1721 struct bwi_softc *sc = ic->ic_softc;
1722 struct ieee80211_channel *c = ic->ic_curchan;
1723 struct bwi_mac *mac;
1726 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1727 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1728 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1729 bwi_rf_set_chan(mac, ieee80211_chan2ieee(ic, c), 0);
1731 sc->sc_rates = ieee80211_get_ratetable(c);
1736 bwi_scan_end(struct ieee80211com *ic)
1738 struct bwi_softc *sc = ic->ic_softc;
1741 CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PASS_BCN);
1746 bwi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1748 struct bwi_vap *bvp = BWI_VAP(vap);
1749 struct ieee80211com *ic= vap->iv_ic;
1750 struct bwi_softc *sc = ic->ic_softc;
1751 enum ieee80211_state ostate = vap->iv_state;
1752 struct bwi_mac *mac;
1757 callout_stop(&sc->sc_calib_ch);
1759 if (nstate == IEEE80211_S_INIT)
1760 sc->sc_txpwrcb_type = BWI_TXPWR_INIT;
1762 bwi_led_newstate(sc, nstate);
1764 error = bvp->bv_newstate(vap, nstate, arg);
1769 * Clear the BSSID when we stop a STA
1771 if (vap->iv_opmode == IEEE80211_M_STA) {
1772 if (ostate == IEEE80211_S_RUN && nstate != IEEE80211_S_RUN) {
1774 * Clear out the BSSID. If we reassociate to
1775 * the same AP, this will reinialize things
1778 if (ic->ic_opmode == IEEE80211_M_STA &&
1779 !(sc->sc_flags & BWI_F_STOP))
1780 bwi_set_bssid(sc, bwi_zero_addr);
1784 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
1786 } else if (nstate == IEEE80211_S_RUN) {
1787 bwi_set_bssid(sc, vap->iv_bss->ni_bssid);
1789 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
1790 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
1791 mac = (struct bwi_mac *)sc->sc_cur_regwin;
1793 /* Initial TX power calibration */
1794 bwi_mac_calibrate_txpower(mac, BWI_TXPWR_INIT);
1796 sc->sc_txpwrcb_type = BWI_TXPWR_FORCE;
1798 sc->sc_txpwrcb_type = BWI_TXPWR_CALIB;
1801 callout_reset(&sc->sc_calib_ch, hz, bwi_calibrate, sc);
1810 bwi_media_change(struct ifnet *ifp)
1812 int error = ieee80211_media_change(ifp);
1813 /* NB: only the fixed rate can change and that doesn't need a reset */
1814 return (error == ENETRESET ? 0 : error);
1818 bwi_dma_alloc(struct bwi_softc *sc)
1820 int error, i, has_txstats;
1821 bus_addr_t lowaddr = 0;
1822 bus_size_t tx_ring_sz, rx_ring_sz, desc_sz = 0;
1823 uint32_t txrx_ctrl_step = 0;
1826 for (i = 0; i < sc->sc_nmac; ++i) {
1827 if (sc->sc_mac[i].mac_flags & BWI_MAC_F_HAS_TXSTATS) {
1833 switch (sc->sc_bus_space) {
1834 case BWI_BUS_SPACE_30BIT:
1835 case BWI_BUS_SPACE_32BIT:
1836 if (sc->sc_bus_space == BWI_BUS_SPACE_30BIT)
1837 lowaddr = BWI_BUS_SPACE_MAXADDR;
1839 lowaddr = BUS_SPACE_MAXADDR_32BIT;
1840 desc_sz = sizeof(struct bwi_desc32);
1841 txrx_ctrl_step = 0x20;
1843 sc->sc_init_tx_ring = bwi_init_tx_ring32;
1844 sc->sc_free_tx_ring = bwi_free_tx_ring32;
1845 sc->sc_init_rx_ring = bwi_init_rx_ring32;
1846 sc->sc_free_rx_ring = bwi_free_rx_ring32;
1847 sc->sc_setup_rxdesc = bwi_setup_rx_desc32;
1848 sc->sc_setup_txdesc = bwi_setup_tx_desc32;
1849 sc->sc_rxeof = bwi_rxeof32;
1850 sc->sc_start_tx = bwi_start_tx32;
1852 sc->sc_init_txstats = bwi_init_txstats32;
1853 sc->sc_free_txstats = bwi_free_txstats32;
1854 sc->sc_txeof_status = bwi_txeof_status32;
1858 case BWI_BUS_SPACE_64BIT:
1859 lowaddr = BUS_SPACE_MAXADDR; /* XXX */
1860 desc_sz = sizeof(struct bwi_desc64);
1861 txrx_ctrl_step = 0x40;
1863 sc->sc_init_tx_ring = bwi_init_tx_ring64;
1864 sc->sc_free_tx_ring = bwi_free_tx_ring64;
1865 sc->sc_init_rx_ring = bwi_init_rx_ring64;
1866 sc->sc_free_rx_ring = bwi_free_rx_ring64;
1867 sc->sc_setup_rxdesc = bwi_setup_rx_desc64;
1868 sc->sc_setup_txdesc = bwi_setup_tx_desc64;
1869 sc->sc_rxeof = bwi_rxeof64;
1870 sc->sc_start_tx = bwi_start_tx64;
1872 sc->sc_init_txstats = bwi_init_txstats64;
1873 sc->sc_free_txstats = bwi_free_txstats64;
1874 sc->sc_txeof_status = bwi_txeof_status64;
1879 KASSERT(lowaddr != 0, ("lowaddr zero"));
1880 KASSERT(desc_sz != 0, ("desc_sz zero"));
1881 KASSERT(txrx_ctrl_step != 0, ("txrx_ctrl_step zero"));
1883 tx_ring_sz = roundup(desc_sz * BWI_TX_NDESC, BWI_RING_ALIGN);
1884 rx_ring_sz = roundup(desc_sz * BWI_RX_NDESC, BWI_RING_ALIGN);
1887 * Create top level DMA tag
1889 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), /* parent */
1890 BWI_ALIGN, 0, /* alignment, bounds */
1891 lowaddr, /* lowaddr */
1892 BUS_SPACE_MAXADDR, /* highaddr */
1893 NULL, NULL, /* filter, filterarg */
1894 BUS_SPACE_MAXSIZE, /* maxsize */
1895 BUS_SPACE_UNRESTRICTED, /* nsegments */
1896 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1898 NULL, NULL, /* lockfunc, lockarg */
1899 &sc->sc_parent_dtag);
1901 device_printf(sc->sc_dev, "can't create parent DMA tag\n");
1905 #define TXRX_CTRL(idx) (BWI_TXRX_CTRL_BASE + (idx) * txrx_ctrl_step)
1908 * Create TX ring DMA stuffs
1910 error = bus_dma_tag_create(sc->sc_parent_dtag,
1920 &sc->sc_txring_dtag);
1922 device_printf(sc->sc_dev, "can't create TX ring DMA tag\n");
1926 for (i = 0; i < BWI_TX_NRING; ++i) {
1927 error = bwi_dma_ring_alloc(sc, sc->sc_txring_dtag,
1928 &sc->sc_tx_rdata[i], tx_ring_sz,
1931 device_printf(sc->sc_dev, "%dth TX ring "
1932 "DMA alloc failed\n", i);
1938 * Create RX ring DMA stuffs
1940 error = bus_dma_tag_create(sc->sc_parent_dtag,
1950 &sc->sc_rxring_dtag);
1952 device_printf(sc->sc_dev, "can't create RX ring DMA tag\n");
1956 error = bwi_dma_ring_alloc(sc, sc->sc_rxring_dtag, &sc->sc_rx_rdata,
1957 rx_ring_sz, TXRX_CTRL(0));
1959 device_printf(sc->sc_dev, "RX ring DMA alloc failed\n");
1964 error = bwi_dma_txstats_alloc(sc, TXRX_CTRL(3), desc_sz);
1966 device_printf(sc->sc_dev,
1967 "TX stats DMA alloc failed\n");
1974 return bwi_dma_mbuf_create(sc);
1978 bwi_dma_free(struct bwi_softc *sc)
1980 if (sc->sc_txring_dtag != NULL) {
1983 for (i = 0; i < BWI_TX_NRING; ++i) {
1984 struct bwi_ring_data *rd = &sc->sc_tx_rdata[i];
1986 if (rd->rdata_desc != NULL) {
1987 bus_dmamap_unload(sc->sc_txring_dtag,
1989 bus_dmamem_free(sc->sc_txring_dtag,
1994 bus_dma_tag_destroy(sc->sc_txring_dtag);
1997 if (sc->sc_rxring_dtag != NULL) {
1998 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2000 if (rd->rdata_desc != NULL) {
2001 bus_dmamap_unload(sc->sc_rxring_dtag, rd->rdata_dmap);
2002 bus_dmamem_free(sc->sc_rxring_dtag, rd->rdata_desc,
2005 bus_dma_tag_destroy(sc->sc_rxring_dtag);
2008 bwi_dma_txstats_free(sc);
2009 bwi_dma_mbuf_destroy(sc, BWI_TX_NRING, 1);
2011 if (sc->sc_parent_dtag != NULL)
2012 bus_dma_tag_destroy(sc->sc_parent_dtag);
2016 bwi_dma_ring_alloc(struct bwi_softc *sc, bus_dma_tag_t dtag,
2017 struct bwi_ring_data *rd, bus_size_t size,
2022 error = bus_dmamem_alloc(dtag, &rd->rdata_desc,
2023 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2026 device_printf(sc->sc_dev, "can't allocate DMA mem\n");
2030 error = bus_dmamap_load(dtag, rd->rdata_dmap, rd->rdata_desc, size,
2031 bwi_dma_ring_addr, &rd->rdata_paddr,
2034 device_printf(sc->sc_dev, "can't load DMA mem\n");
2035 bus_dmamem_free(dtag, rd->rdata_desc, rd->rdata_dmap);
2036 rd->rdata_desc = NULL;
2040 rd->rdata_txrx_ctrl = txrx_ctrl;
2045 bwi_dma_txstats_alloc(struct bwi_softc *sc, uint32_t ctrl_base,
2048 struct bwi_txstats_data *st;
2049 bus_size_t dma_size;
2052 st = malloc(sizeof(*st), M_DEVBUF, M_NOWAIT | M_ZERO);
2054 device_printf(sc->sc_dev, "can't allocate txstats data\n");
2057 sc->sc_txstats = st;
2060 * Create TX stats descriptor DMA stuffs
2062 dma_size = roundup(desc_sz * BWI_TXSTATS_NDESC, BWI_RING_ALIGN);
2064 error = bus_dma_tag_create(sc->sc_parent_dtag,
2075 &st->stats_ring_dtag);
2077 device_printf(sc->sc_dev, "can't create txstats ring "
2082 error = bus_dmamem_alloc(st->stats_ring_dtag, &st->stats_ring,
2083 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2084 &st->stats_ring_dmap);
2086 device_printf(sc->sc_dev, "can't allocate txstats ring "
2088 bus_dma_tag_destroy(st->stats_ring_dtag);
2089 st->stats_ring_dtag = NULL;
2093 error = bus_dmamap_load(st->stats_ring_dtag, st->stats_ring_dmap,
2094 st->stats_ring, dma_size,
2095 bwi_dma_ring_addr, &st->stats_ring_paddr,
2098 device_printf(sc->sc_dev, "can't load txstats ring DMA mem\n");
2099 bus_dmamem_free(st->stats_ring_dtag, st->stats_ring,
2100 st->stats_ring_dmap);
2101 bus_dma_tag_destroy(st->stats_ring_dtag);
2102 st->stats_ring_dtag = NULL;
2107 * Create TX stats DMA stuffs
2109 dma_size = roundup(sizeof(struct bwi_txstats) * BWI_TXSTATS_NDESC,
2112 error = bus_dma_tag_create(sc->sc_parent_dtag,
2125 device_printf(sc->sc_dev, "can't create txstats DMA tag\n");
2129 error = bus_dmamem_alloc(st->stats_dtag, (void **)&st->stats,
2130 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2133 device_printf(sc->sc_dev, "can't allocate txstats DMA mem\n");
2134 bus_dma_tag_destroy(st->stats_dtag);
2135 st->stats_dtag = NULL;
2139 error = bus_dmamap_load(st->stats_dtag, st->stats_dmap, st->stats,
2140 dma_size, bwi_dma_ring_addr, &st->stats_paddr,
2143 device_printf(sc->sc_dev, "can't load txstats DMA mem\n");
2144 bus_dmamem_free(st->stats_dtag, st->stats, st->stats_dmap);
2145 bus_dma_tag_destroy(st->stats_dtag);
2146 st->stats_dtag = NULL;
2150 st->stats_ctrl_base = ctrl_base;
2155 bwi_dma_txstats_free(struct bwi_softc *sc)
2157 struct bwi_txstats_data *st;
2159 if (sc->sc_txstats == NULL)
2161 st = sc->sc_txstats;
2163 if (st->stats_ring_dtag != NULL) {
2164 bus_dmamap_unload(st->stats_ring_dtag, st->stats_ring_dmap);
2165 bus_dmamem_free(st->stats_ring_dtag, st->stats_ring,
2166 st->stats_ring_dmap);
2167 bus_dma_tag_destroy(st->stats_ring_dtag);
2170 if (st->stats_dtag != NULL) {
2171 bus_dmamap_unload(st->stats_dtag, st->stats_dmap);
2172 bus_dmamem_free(st->stats_dtag, st->stats, st->stats_dmap);
2173 bus_dma_tag_destroy(st->stats_dtag);
2180 bwi_dma_ring_addr(void *arg, bus_dma_segment_t *seg, int nseg, int error)
2182 KASSERT(nseg == 1, ("too many segments\n"));
2183 *((bus_addr_t *)arg) = seg->ds_addr;
2187 bwi_dma_mbuf_create(struct bwi_softc *sc)
2189 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2190 int i, j, k, ntx, error;
2193 * Create TX/RX mbuf DMA tag
2195 error = bus_dma_tag_create(sc->sc_parent_dtag,
2208 device_printf(sc->sc_dev, "can't create mbuf DMA tag\n");
2215 * Create TX mbuf DMA map
2217 for (i = 0; i < BWI_TX_NRING; ++i) {
2218 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[i];
2220 for (j = 0; j < BWI_TX_NDESC; ++j) {
2221 error = bus_dmamap_create(sc->sc_buf_dtag, 0,
2222 &tbd->tbd_buf[j].tb_dmap);
2224 device_printf(sc->sc_dev, "can't create "
2225 "%dth tbd, %dth DMA map\n", i, j);
2228 for (k = 0; k < j; ++k) {
2229 bus_dmamap_destroy(sc->sc_buf_dtag,
2230 tbd->tbd_buf[k].tb_dmap);
2239 * Create RX mbuf DMA map and a spare DMA map
2241 error = bus_dmamap_create(sc->sc_buf_dtag, 0,
2242 &rbd->rbd_tmp_dmap);
2244 device_printf(sc->sc_dev,
2245 "can't create spare RX buf DMA map\n");
2249 for (j = 0; j < BWI_RX_NDESC; ++j) {
2250 error = bus_dmamap_create(sc->sc_buf_dtag, 0,
2251 &rbd->rbd_buf[j].rb_dmap);
2253 device_printf(sc->sc_dev, "can't create %dth "
2254 "RX buf DMA map\n", j);
2256 for (k = 0; k < j; ++k) {
2257 bus_dmamap_destroy(sc->sc_buf_dtag,
2258 rbd->rbd_buf[j].rb_dmap);
2260 bus_dmamap_destroy(sc->sc_buf_dtag,
2268 bwi_dma_mbuf_destroy(sc, ntx, 0);
2273 bwi_dma_mbuf_destroy(struct bwi_softc *sc, int ntx, int nrx)
2277 if (sc->sc_buf_dtag == NULL)
2280 for (i = 0; i < ntx; ++i) {
2281 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[i];
2283 for (j = 0; j < BWI_TX_NDESC; ++j) {
2284 struct bwi_txbuf *tb = &tbd->tbd_buf[j];
2286 if (tb->tb_mbuf != NULL) {
2287 bus_dmamap_unload(sc->sc_buf_dtag,
2289 m_freem(tb->tb_mbuf);
2291 if (tb->tb_ni != NULL)
2292 ieee80211_free_node(tb->tb_ni);
2293 bus_dmamap_destroy(sc->sc_buf_dtag, tb->tb_dmap);
2298 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2300 bus_dmamap_destroy(sc->sc_buf_dtag, rbd->rbd_tmp_dmap);
2301 for (j = 0; j < BWI_RX_NDESC; ++j) {
2302 struct bwi_rxbuf *rb = &rbd->rbd_buf[j];
2304 if (rb->rb_mbuf != NULL) {
2305 bus_dmamap_unload(sc->sc_buf_dtag,
2307 m_freem(rb->rb_mbuf);
2309 bus_dmamap_destroy(sc->sc_buf_dtag, rb->rb_dmap);
2313 bus_dma_tag_destroy(sc->sc_buf_dtag);
2314 sc->sc_buf_dtag = NULL;
2318 bwi_enable_intrs(struct bwi_softc *sc, uint32_t enable_intrs)
2320 CSR_SETBITS_4(sc, BWI_MAC_INTR_MASK, enable_intrs);
2324 bwi_disable_intrs(struct bwi_softc *sc, uint32_t disable_intrs)
2326 CSR_CLRBITS_4(sc, BWI_MAC_INTR_MASK, disable_intrs);
2330 bwi_init_tx_ring32(struct bwi_softc *sc, int ring_idx)
2332 struct bwi_ring_data *rd;
2333 struct bwi_txbuf_data *tbd;
2334 uint32_t val, addr_hi, addr_lo;
2336 KASSERT(ring_idx < BWI_TX_NRING, ("ring_idx %d", ring_idx));
2337 rd = &sc->sc_tx_rdata[ring_idx];
2338 tbd = &sc->sc_tx_bdata[ring_idx];
2343 bzero(rd->rdata_desc, sizeof(struct bwi_desc32) * BWI_TX_NDESC);
2344 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap,
2345 BUS_DMASYNC_PREWRITE);
2347 addr_lo = __SHIFTOUT(rd->rdata_paddr, BWI_TXRX32_RINGINFO_ADDR_MASK);
2348 addr_hi = __SHIFTOUT(rd->rdata_paddr, BWI_TXRX32_RINGINFO_FUNC_MASK);
2350 val = __SHIFTIN(addr_lo, BWI_TXRX32_RINGINFO_ADDR_MASK) |
2351 __SHIFTIN(BWI_TXRX32_RINGINFO_FUNC_TXRX,
2352 BWI_TXRX32_RINGINFO_FUNC_MASK);
2353 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_RINGINFO, val);
2355 val = __SHIFTIN(addr_hi, BWI_TXRX32_CTRL_ADDRHI_MASK) |
2356 BWI_TXRX32_CTRL_ENABLE;
2357 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_CTRL, val);
2363 bwi_init_rxdesc_ring32(struct bwi_softc *sc, uint32_t ctrl_base,
2364 bus_addr_t paddr, int hdr_size, int ndesc)
2366 uint32_t val, addr_hi, addr_lo;
2368 addr_lo = __SHIFTOUT(paddr, BWI_TXRX32_RINGINFO_ADDR_MASK);
2369 addr_hi = __SHIFTOUT(paddr, BWI_TXRX32_RINGINFO_FUNC_MASK);
2371 val = __SHIFTIN(addr_lo, BWI_TXRX32_RINGINFO_ADDR_MASK) |
2372 __SHIFTIN(BWI_TXRX32_RINGINFO_FUNC_TXRX,
2373 BWI_TXRX32_RINGINFO_FUNC_MASK);
2374 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_RINGINFO, val);
2376 val = __SHIFTIN(hdr_size, BWI_RX32_CTRL_HDRSZ_MASK) |
2377 __SHIFTIN(addr_hi, BWI_TXRX32_CTRL_ADDRHI_MASK) |
2378 BWI_TXRX32_CTRL_ENABLE;
2379 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_CTRL, val);
2381 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_INDEX,
2382 (ndesc - 1) * sizeof(struct bwi_desc32));
2386 bwi_init_rx_ring32(struct bwi_softc *sc)
2388 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2391 sc->sc_rx_bdata.rbd_idx = 0;
2393 for (i = 0; i < BWI_RX_NDESC; ++i) {
2394 error = bwi_newbuf(sc, i, 1);
2396 device_printf(sc->sc_dev,
2397 "can't allocate %dth RX buffer\n", i);
2401 bus_dmamap_sync(sc->sc_rxring_dtag, rd->rdata_dmap,
2402 BUS_DMASYNC_PREWRITE);
2404 bwi_init_rxdesc_ring32(sc, rd->rdata_txrx_ctrl, rd->rdata_paddr,
2405 sizeof(struct bwi_rxbuf_hdr), BWI_RX_NDESC);
2410 bwi_init_txstats32(struct bwi_softc *sc)
2412 struct bwi_txstats_data *st = sc->sc_txstats;
2413 bus_addr_t stats_paddr;
2416 bzero(st->stats, BWI_TXSTATS_NDESC * sizeof(struct bwi_txstats));
2417 bus_dmamap_sync(st->stats_dtag, st->stats_dmap, BUS_DMASYNC_PREWRITE);
2421 stats_paddr = st->stats_paddr;
2422 for (i = 0; i < BWI_TXSTATS_NDESC; ++i) {
2423 bwi_setup_desc32(sc, st->stats_ring, BWI_TXSTATS_NDESC, i,
2424 stats_paddr, sizeof(struct bwi_txstats), 0);
2425 stats_paddr += sizeof(struct bwi_txstats);
2427 bus_dmamap_sync(st->stats_ring_dtag, st->stats_ring_dmap,
2428 BUS_DMASYNC_PREWRITE);
2430 bwi_init_rxdesc_ring32(sc, st->stats_ctrl_base,
2431 st->stats_ring_paddr, 0, BWI_TXSTATS_NDESC);
2436 bwi_setup_rx_desc32(struct bwi_softc *sc, int buf_idx, bus_addr_t paddr,
2439 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2441 KASSERT(buf_idx < BWI_RX_NDESC, ("buf_idx %d", buf_idx));
2442 bwi_setup_desc32(sc, rd->rdata_desc, BWI_RX_NDESC, buf_idx,
2447 bwi_setup_tx_desc32(struct bwi_softc *sc, struct bwi_ring_data *rd,
2448 int buf_idx, bus_addr_t paddr, int buf_len)
2450 KASSERT(buf_idx < BWI_TX_NDESC, ("buf_idx %d", buf_idx));
2451 bwi_setup_desc32(sc, rd->rdata_desc, BWI_TX_NDESC, buf_idx,
2456 bwi_init_tx_ring64(struct bwi_softc *sc, int ring_idx)
2463 bwi_init_rx_ring64(struct bwi_softc *sc)
2470 bwi_init_txstats64(struct bwi_softc *sc)
2477 bwi_setup_rx_desc64(struct bwi_softc *sc, int buf_idx, bus_addr_t paddr,
2484 bwi_setup_tx_desc64(struct bwi_softc *sc, struct bwi_ring_data *rd,
2485 int buf_idx, bus_addr_t paddr, int buf_len)
2491 bwi_dma_buf_addr(void *arg, bus_dma_segment_t *seg, int nseg,
2492 bus_size_t mapsz __unused, int error)
2495 KASSERT(nseg == 1, ("too many segments(%d)\n", nseg));
2496 *((bus_addr_t *)arg) = seg->ds_addr;
2501 bwi_newbuf(struct bwi_softc *sc, int buf_idx, int init)
2503 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2504 struct bwi_rxbuf *rxbuf = &rbd->rbd_buf[buf_idx];
2505 struct bwi_rxbuf_hdr *hdr;
2511 KASSERT(buf_idx < BWI_RX_NDESC, ("buf_idx %d", buf_idx));
2513 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2518 * If the NIC is up and running, we need to:
2519 * - Clear RX buffer's header.
2520 * - Restore RX descriptor settings.
2527 m->m_len = m->m_pkthdr.len = MCLBYTES;
2530 * Try to load RX buf into temporary DMA map
2532 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, rbd->rbd_tmp_dmap, m,
2533 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT);
2538 * See the comment above
2547 bus_dmamap_unload(sc->sc_buf_dtag, rxbuf->rb_dmap);
2549 rxbuf->rb_paddr = paddr;
2552 * Swap RX buf's DMA map with the loaded temporary one
2554 map = rxbuf->rb_dmap;
2555 rxbuf->rb_dmap = rbd->rbd_tmp_dmap;
2556 rbd->rbd_tmp_dmap = map;
2560 * Clear RX buf header
2562 hdr = mtod(rxbuf->rb_mbuf, struct bwi_rxbuf_hdr *);
2563 bzero(hdr, sizeof(*hdr));
2564 bus_dmamap_sync(sc->sc_buf_dtag, rxbuf->rb_dmap, BUS_DMASYNC_PREWRITE);
2567 * Setup RX buf descriptor
2569 sc->sc_setup_rxdesc(sc, buf_idx, rxbuf->rb_paddr,
2570 rxbuf->rb_mbuf->m_len - sizeof(*hdr));
2575 bwi_set_addr_filter(struct bwi_softc *sc, uint16_t addr_ofs,
2576 const uint8_t *addr)
2580 CSR_WRITE_2(sc, BWI_ADDR_FILTER_CTRL,
2581 BWI_ADDR_FILTER_CTRL_SET | addr_ofs);
2583 for (i = 0; i < (IEEE80211_ADDR_LEN / 2); ++i) {
2586 addr_val = (uint16_t)addr[i * 2] |
2587 (((uint16_t)addr[(i * 2) + 1]) << 8);
2588 CSR_WRITE_2(sc, BWI_ADDR_FILTER_DATA, addr_val);
2593 bwi_rxeof(struct bwi_softc *sc, int end_idx)
2595 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2596 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2597 struct ieee80211com *ic = &sc->sc_ic;
2598 int idx, rx_data = 0;
2601 while (idx != end_idx) {
2602 struct bwi_rxbuf *rb = &rbd->rbd_buf[idx];
2603 struct bwi_rxbuf_hdr *hdr;
2604 struct ieee80211_frame_min *wh;
2605 struct ieee80211_node *ni;
2609 int buflen, wh_ofs, hdr_extra, rssi, noise, type, rate;
2612 bus_dmamap_sync(sc->sc_buf_dtag, rb->rb_dmap,
2613 BUS_DMASYNC_POSTREAD);
2615 if (bwi_newbuf(sc, idx, 0)) {
2616 counter_u64_add(ic->ic_ierrors, 1);
2620 hdr = mtod(m, struct bwi_rxbuf_hdr *);
2621 flags2 = le16toh(hdr->rxh_flags2);
2624 if (flags2 & BWI_RXH_F2_TYPE2FRAME)
2626 wh_ofs = hdr_extra + 6; /* XXX magic number */
2628 buflen = le16toh(hdr->rxh_buflen);
2629 if (buflen < BWI_FRAME_MIN_LEN(wh_ofs)) {
2630 device_printf(sc->sc_dev,
2631 "%s: zero length data, hdr_extra %d\n",
2632 __func__, hdr_extra);
2633 counter_u64_add(ic->ic_ierrors, 1);
2638 bcopy((uint8_t *)(hdr + 1) + hdr_extra, &plcp, sizeof(plcp));
2639 rssi = bwi_calc_rssi(sc, hdr);
2640 noise = bwi_calc_noise(sc);
2642 m->m_len = m->m_pkthdr.len = buflen + sizeof(*hdr);
2643 m_adj(m, sizeof(*hdr) + wh_ofs);
2645 if (htole16(hdr->rxh_flags1) & BWI_RXH_F1_OFDM)
2646 rate = bwi_plcp2rate(plcp, IEEE80211_T_OFDM);
2648 rate = bwi_plcp2rate(plcp, IEEE80211_T_CCK);
2651 if (ieee80211_radiotap_active(ic))
2652 bwi_rx_radiotap(sc, m, hdr, &plcp, rate, rssi, noise);
2654 m_adj(m, -IEEE80211_CRC_LEN);
2658 wh = mtod(m, struct ieee80211_frame_min *);
2659 ni = ieee80211_find_rxnode(ic, wh);
2661 type = ieee80211_input(ni, m, rssi - noise, noise);
2662 ieee80211_free_node(ni);
2664 type = ieee80211_input_all(ic, m, rssi - noise, noise);
2665 if (type == IEEE80211_FC0_TYPE_DATA) {
2667 sc->sc_rx_rate = rate;
2672 idx = (idx + 1) % BWI_RX_NDESC;
2674 if (sc->sc_flags & BWI_F_STOP) {
2676 * Take the fast lane, don't do
2677 * any damage to softc
2684 bus_dmamap_sync(sc->sc_rxring_dtag, rd->rdata_dmap,
2685 BUS_DMASYNC_PREWRITE);
2691 bwi_rxeof32(struct bwi_softc *sc)
2693 uint32_t val, rx_ctrl;
2694 int end_idx, rx_data;
2696 rx_ctrl = sc->sc_rx_rdata.rdata_txrx_ctrl;
2698 val = CSR_READ_4(sc, rx_ctrl + BWI_RX32_STATUS);
2699 end_idx = __SHIFTOUT(val, BWI_RX32_STATUS_INDEX_MASK) /
2700 sizeof(struct bwi_desc32);
2702 rx_data = bwi_rxeof(sc, end_idx);
2704 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_INDEX,
2705 end_idx * sizeof(struct bwi_desc32));
2711 bwi_rxeof64(struct bwi_softc *sc)
2718 bwi_reset_rx_ring32(struct bwi_softc *sc, uint32_t rx_ctrl)
2722 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_CTRL, 0);
2726 for (i = 0; i < NRETRY; ++i) {
2729 status = CSR_READ_4(sc, rx_ctrl + BWI_RX32_STATUS);
2730 if (__SHIFTOUT(status, BWI_RX32_STATUS_STATE_MASK) ==
2731 BWI_RX32_STATUS_STATE_DISABLED)
2737 device_printf(sc->sc_dev, "reset rx ring timedout\n");
2741 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_RINGINFO, 0);
2745 bwi_free_txstats32(struct bwi_softc *sc)
2747 bwi_reset_rx_ring32(sc, sc->sc_txstats->stats_ctrl_base);
2751 bwi_free_rx_ring32(struct bwi_softc *sc)
2753 struct bwi_ring_data *rd = &sc->sc_rx_rdata;
2754 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata;
2757 bwi_reset_rx_ring32(sc, rd->rdata_txrx_ctrl);
2759 for (i = 0; i < BWI_RX_NDESC; ++i) {
2760 struct bwi_rxbuf *rb = &rbd->rbd_buf[i];
2762 if (rb->rb_mbuf != NULL) {
2763 bus_dmamap_unload(sc->sc_buf_dtag, rb->rb_dmap);
2764 m_freem(rb->rb_mbuf);
2771 bwi_free_tx_ring32(struct bwi_softc *sc, int ring_idx)
2773 struct bwi_ring_data *rd;
2774 struct bwi_txbuf_data *tbd;
2775 uint32_t state, val;
2778 KASSERT(ring_idx < BWI_TX_NRING, ("ring_idx %d", ring_idx));
2779 rd = &sc->sc_tx_rdata[ring_idx];
2780 tbd = &sc->sc_tx_bdata[ring_idx];
2784 for (i = 0; i < NRETRY; ++i) {
2785 val = CSR_READ_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_STATUS);
2786 state = __SHIFTOUT(val, BWI_TX32_STATUS_STATE_MASK);
2787 if (state == BWI_TX32_STATUS_STATE_DISABLED ||
2788 state == BWI_TX32_STATUS_STATE_IDLE ||
2789 state == BWI_TX32_STATUS_STATE_STOPPED)
2795 device_printf(sc->sc_dev,
2796 "%s: wait for TX ring(%d) stable timed out\n",
2797 __func__, ring_idx);
2800 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_CTRL, 0);
2801 for (i = 0; i < NRETRY; ++i) {
2802 val = CSR_READ_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_STATUS);
2803 state = __SHIFTOUT(val, BWI_TX32_STATUS_STATE_MASK);
2804 if (state == BWI_TX32_STATUS_STATE_DISABLED)
2810 device_printf(sc->sc_dev, "%s: reset TX ring (%d) timed out\n",
2811 __func__, ring_idx);
2817 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_RINGINFO, 0);
2819 for (i = 0; i < BWI_TX_NDESC; ++i) {
2820 struct bwi_txbuf *tb = &tbd->tbd_buf[i];
2822 if (tb->tb_mbuf != NULL) {
2823 bus_dmamap_unload(sc->sc_buf_dtag, tb->tb_dmap);
2824 m_freem(tb->tb_mbuf);
2827 if (tb->tb_ni != NULL) {
2828 ieee80211_free_node(tb->tb_ni);
2835 bwi_free_txstats64(struct bwi_softc *sc)
2841 bwi_free_rx_ring64(struct bwi_softc *sc)
2847 bwi_free_tx_ring64(struct bwi_softc *sc, int ring_idx)
2852 /* XXX does not belong here */
2853 #define IEEE80211_OFDM_PLCP_RATE_MASK __BITS(3, 0)
2854 #define IEEE80211_OFDM_PLCP_LEN_MASK __BITS(16, 5)
2856 static __inline void
2857 bwi_ofdm_plcp_header(uint32_t *plcp0, int pkt_len, uint8_t rate)
2861 plcp = __SHIFTIN(ieee80211_rate2plcp(rate, IEEE80211_T_OFDM),
2862 IEEE80211_OFDM_PLCP_RATE_MASK) |
2863 __SHIFTIN(pkt_len, IEEE80211_OFDM_PLCP_LEN_MASK);
2864 *plcp0 = htole32(plcp);
2867 static __inline void
2868 bwi_ds_plcp_header(struct ieee80211_ds_plcp_hdr *plcp, int pkt_len,
2871 int len, service, pkt_bitlen;
2873 pkt_bitlen = pkt_len * NBBY;
2874 len = howmany(pkt_bitlen * 2, rate);
2876 service = IEEE80211_PLCP_SERVICE_LOCKED;
2877 if (rate == (11 * 2)) {
2881 * PLCP service field needs to be adjusted,
2882 * if TX rate is 11Mbytes/s
2884 pkt_bitlen1 = len * 11;
2885 if (pkt_bitlen1 - pkt_bitlen >= NBBY)
2886 service |= IEEE80211_PLCP_SERVICE_LENEXT7;
2889 plcp->i_signal = ieee80211_rate2plcp(rate, IEEE80211_T_CCK);
2890 plcp->i_service = service;
2891 plcp->i_length = htole16(len);
2892 /* NOTE: do NOT touch i_crc */
2895 static __inline void
2896 bwi_plcp_header(const struct ieee80211_rate_table *rt,
2897 void *plcp, int pkt_len, uint8_t rate)
2899 enum ieee80211_phytype modtype;
2902 * Assume caller has zeroed 'plcp'
2904 modtype = ieee80211_rate2phytype(rt, rate);
2905 if (modtype == IEEE80211_T_OFDM)
2906 bwi_ofdm_plcp_header(plcp, pkt_len, rate);
2907 else if (modtype == IEEE80211_T_DS)
2908 bwi_ds_plcp_header(plcp, pkt_len, rate);
2910 panic("unsupport modulation type %u\n", modtype);
2914 bwi_encap(struct bwi_softc *sc, int idx, struct mbuf *m,
2915 struct ieee80211_node *ni)
2917 struct ieee80211vap *vap = ni->ni_vap;
2918 struct ieee80211com *ic = &sc->sc_ic;
2919 struct bwi_ring_data *rd = &sc->sc_tx_rdata[BWI_TX_DATA_RING];
2920 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
2921 struct bwi_txbuf *tb = &tbd->tbd_buf[idx];
2922 struct bwi_mac *mac;
2923 struct bwi_txbuf_hdr *hdr;
2924 struct ieee80211_frame *wh;
2925 const struct ieee80211_txparam *tp = ni->ni_txparms;
2926 uint8_t rate, rate_fb;
2930 int type, ismcast, pkt_len, error, rix;
2936 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
2937 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
2938 mac = (struct bwi_mac *)sc->sc_cur_regwin;
2940 wh = mtod(m, struct ieee80211_frame *);
2941 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2942 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2944 /* Get 802.11 frame len before prepending TX header */
2945 pkt_len = m->m_pkthdr.len + IEEE80211_CRC_LEN;
2950 if (type != IEEE80211_FC0_TYPE_DATA || (m->m_flags & M_EAPOL)) {
2951 rate = rate_fb = tp->mgmtrate;
2952 } else if (ismcast) {
2953 rate = rate_fb = tp->mcastrate;
2954 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
2955 rate = rate_fb = tp->ucastrate;
2957 rix = ieee80211_ratectl_rate(ni, NULL, pkt_len);
2958 rate = ni->ni_txrate;
2961 rate_fb = ni->ni_rates.rs_rates[rix-1] &
2967 tb->tb_rate[0] = rate;
2968 tb->tb_rate[1] = rate_fb;
2969 sc->sc_tx_rate = rate;
2974 if (ieee80211_radiotap_active_vap(vap)) {
2975 sc->sc_tx_th.wt_flags = 0;
2976 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)
2977 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2978 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_DS &&
2979 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2981 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2983 sc->sc_tx_th.wt_rate = rate;
2985 ieee80211_radiotap_tx(vap, m);
2989 * Setup the embedded TX header
2991 M_PREPEND(m, sizeof(*hdr), M_NOWAIT);
2993 device_printf(sc->sc_dev, "%s: prepend TX header failed\n",
2997 hdr = mtod(m, struct bwi_txbuf_hdr *);
2999 bzero(hdr, sizeof(*hdr));
3001 bcopy(wh->i_fc, hdr->txh_fc, sizeof(hdr->txh_fc));
3002 bcopy(wh->i_addr1, hdr->txh_addr1, sizeof(hdr->txh_addr1));
3007 dur = ieee80211_ack_duration(sc->sc_rates, rate,
3008 ic->ic_flags & ~IEEE80211_F_SHPREAMBLE);
3010 hdr->txh_fb_duration = htole16(dur);
3013 hdr->txh_id = __SHIFTIN(BWI_TX_DATA_RING, BWI_TXH_ID_RING_MASK) |
3014 __SHIFTIN(idx, BWI_TXH_ID_IDX_MASK);
3016 bwi_plcp_header(sc->sc_rates, hdr->txh_plcp, pkt_len, rate);
3017 bwi_plcp_header(sc->sc_rates, hdr->txh_fb_plcp, pkt_len, rate_fb);
3019 phy_ctrl = __SHIFTIN(mac->mac_rf.rf_ant_mode,
3020 BWI_TXH_PHY_C_ANTMODE_MASK);
3021 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM)
3022 phy_ctrl |= BWI_TXH_PHY_C_OFDM;
3023 else if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && rate != (2 * 1))
3024 phy_ctrl |= BWI_TXH_PHY_C_SHPREAMBLE;
3026 mac_ctrl = BWI_TXH_MAC_C_HWSEQ | BWI_TXH_MAC_C_FIRST_FRAG;
3028 mac_ctrl |= BWI_TXH_MAC_C_ACK;
3029 if (ieee80211_rate2phytype(sc->sc_rates, rate_fb) == IEEE80211_T_OFDM)
3030 mac_ctrl |= BWI_TXH_MAC_C_FB_OFDM;
3032 hdr->txh_mac_ctrl = htole32(mac_ctrl);
3033 hdr->txh_phy_ctrl = htole16(phy_ctrl);
3035 /* Catch any further usage */
3040 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3041 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT);
3042 if (error && error != EFBIG) {
3043 device_printf(sc->sc_dev, "%s: can't load TX buffer (1) %d\n",
3048 if (error) { /* error == EFBIG */
3051 m_new = m_defrag(m, M_NOWAIT);
3052 if (m_new == NULL) {
3053 device_printf(sc->sc_dev,
3054 "%s: can't defrag TX buffer\n", __func__);
3061 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3062 bwi_dma_buf_addr, &paddr,
3065 device_printf(sc->sc_dev,
3066 "%s: can't load TX buffer (2) %d\n",
3073 bus_dmamap_sync(sc->sc_buf_dtag, tb->tb_dmap, BUS_DMASYNC_PREWRITE);
3079 p = mtod(m, const uint8_t *);
3080 for (i = 0; i < m->m_pkthdr.len; ++i) {
3081 if (i != 0 && i % 8 == 0)
3083 printf("%02x ", p[i]);
3087 DPRINTF(sc, BWI_DBG_TX, "idx %d, pkt_len %d, buflen %d\n",
3088 idx, pkt_len, m->m_pkthdr.len);
3090 /* Setup TX descriptor */
3091 sc->sc_setup_txdesc(sc, rd, idx, paddr, m->m_pkthdr.len);
3092 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap,
3093 BUS_DMASYNC_PREWRITE);
3096 sc->sc_start_tx(sc, rd->rdata_txrx_ctrl, idx);
3105 bwi_encap_raw(struct bwi_softc *sc, int idx, struct mbuf *m,
3106 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
3108 struct ieee80211vap *vap = ni->ni_vap;
3109 struct ieee80211com *ic = ni->ni_ic;
3110 struct bwi_ring_data *rd = &sc->sc_tx_rdata[BWI_TX_DATA_RING];
3111 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING];
3112 struct bwi_txbuf *tb = &tbd->tbd_buf[idx];
3113 struct bwi_mac *mac;
3114 struct bwi_txbuf_hdr *hdr;
3115 struct ieee80211_frame *wh;
3116 uint8_t rate, rate_fb;
3120 int ismcast, pkt_len, error;
3122 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3123 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3124 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3126 wh = mtod(m, struct ieee80211_frame *);
3127 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
3129 /* Get 802.11 frame len before prepending TX header */
3130 pkt_len = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3135 rate = params->ibp_rate0;
3136 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3137 /* XXX fall back to mcast/mgmt rate? */
3141 if (params->ibp_try1 != 0) {
3142 rate_fb = params->ibp_rate1;
3143 if (!ieee80211_isratevalid(ic->ic_rt, rate_fb)) {
3144 /* XXX fall back to rate0? */
3150 tb->tb_rate[0] = rate;
3151 tb->tb_rate[1] = rate_fb;
3152 sc->sc_tx_rate = rate;
3157 if (ieee80211_radiotap_active_vap(vap)) {
3158 sc->sc_tx_th.wt_flags = 0;
3159 /* XXX IEEE80211_BPF_CRYPTO */
3160 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)
3161 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3162 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
3163 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3164 sc->sc_tx_th.wt_rate = rate;
3166 ieee80211_radiotap_tx(vap, m);
3170 * Setup the embedded TX header
3172 M_PREPEND(m, sizeof(*hdr), M_NOWAIT);
3174 device_printf(sc->sc_dev, "%s: prepend TX header failed\n",
3178 hdr = mtod(m, struct bwi_txbuf_hdr *);
3180 bzero(hdr, sizeof(*hdr));
3182 bcopy(wh->i_fc, hdr->txh_fc, sizeof(hdr->txh_fc));
3183 bcopy(wh->i_addr1, hdr->txh_addr1, sizeof(hdr->txh_addr1));
3185 mac_ctrl = BWI_TXH_MAC_C_HWSEQ | BWI_TXH_MAC_C_FIRST_FRAG;
3186 if (!ismcast && (params->ibp_flags & IEEE80211_BPF_NOACK) == 0) {
3189 dur = ieee80211_ack_duration(sc->sc_rates, rate_fb, 0);
3191 hdr->txh_fb_duration = htole16(dur);
3192 mac_ctrl |= BWI_TXH_MAC_C_ACK;
3195 hdr->txh_id = __SHIFTIN(BWI_TX_DATA_RING, BWI_TXH_ID_RING_MASK) |
3196 __SHIFTIN(idx, BWI_TXH_ID_IDX_MASK);
3198 bwi_plcp_header(sc->sc_rates, hdr->txh_plcp, pkt_len, rate);
3199 bwi_plcp_header(sc->sc_rates, hdr->txh_fb_plcp, pkt_len, rate_fb);
3201 phy_ctrl = __SHIFTIN(mac->mac_rf.rf_ant_mode,
3202 BWI_TXH_PHY_C_ANTMODE_MASK);
3203 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM) {
3204 phy_ctrl |= BWI_TXH_PHY_C_OFDM;
3205 mac_ctrl |= BWI_TXH_MAC_C_FB_OFDM;
3206 } else if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
3207 phy_ctrl |= BWI_TXH_PHY_C_SHPREAMBLE;
3209 hdr->txh_mac_ctrl = htole32(mac_ctrl);
3210 hdr->txh_phy_ctrl = htole16(phy_ctrl);
3212 /* Catch any further usage */
3217 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3218 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT);
3222 if (error != EFBIG) {
3223 device_printf(sc->sc_dev,
3224 "%s: can't load TX buffer (1) %d\n",
3228 m_new = m_defrag(m, M_NOWAIT);
3229 if (m_new == NULL) {
3230 device_printf(sc->sc_dev,
3231 "%s: can't defrag TX buffer\n", __func__);
3236 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m,
3237 bwi_dma_buf_addr, &paddr,
3240 device_printf(sc->sc_dev,
3241 "%s: can't load TX buffer (2) %d\n",
3247 bus_dmamap_sync(sc->sc_buf_dtag, tb->tb_dmap, BUS_DMASYNC_PREWRITE);
3252 DPRINTF(sc, BWI_DBG_TX, "idx %d, pkt_len %d, buflen %d\n",
3253 idx, pkt_len, m->m_pkthdr.len);
3255 /* Setup TX descriptor */
3256 sc->sc_setup_txdesc(sc, rd, idx, paddr, m->m_pkthdr.len);
3257 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap,
3258 BUS_DMASYNC_PREWRITE);
3261 sc->sc_start_tx(sc, rd->rdata_txrx_ctrl, idx);
3269 bwi_start_tx32(struct bwi_softc *sc, uint32_t tx_ctrl, int idx)
3271 idx = (idx + 1) % BWI_TX_NDESC;
3272 CSR_WRITE_4(sc, tx_ctrl + BWI_TX32_INDEX,
3273 idx * sizeof(struct bwi_desc32));
3277 bwi_start_tx64(struct bwi_softc *sc, uint32_t tx_ctrl, int idx)
3283 bwi_txeof_status32(struct bwi_softc *sc)
3285 uint32_t val, ctrl_base;
3288 ctrl_base = sc->sc_txstats->stats_ctrl_base;
3290 val = CSR_READ_4(sc, ctrl_base + BWI_RX32_STATUS);
3291 end_idx = __SHIFTOUT(val, BWI_RX32_STATUS_INDEX_MASK) /
3292 sizeof(struct bwi_desc32);
3294 bwi_txeof_status(sc, end_idx);
3296 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_INDEX,
3297 end_idx * sizeof(struct bwi_desc32));
3299 bwi_start_locked(sc);
3303 bwi_txeof_status64(struct bwi_softc *sc)
3309 _bwi_txeof(struct bwi_softc *sc, uint16_t tx_id, int acked, int data_txcnt)
3311 struct bwi_txbuf_data *tbd;
3312 struct bwi_txbuf *tb;
3313 int ring_idx, buf_idx;
3314 struct ieee80211_node *ni;
3317 device_printf(sc->sc_dev, "%s: zero tx id\n", __func__);
3321 ring_idx = __SHIFTOUT(tx_id, BWI_TXH_ID_RING_MASK);
3322 buf_idx = __SHIFTOUT(tx_id, BWI_TXH_ID_IDX_MASK);
3324 KASSERT(ring_idx == BWI_TX_DATA_RING, ("ring_idx %d", ring_idx));
3325 KASSERT(buf_idx < BWI_TX_NDESC, ("buf_idx %d", buf_idx));
3327 tbd = &sc->sc_tx_bdata[ring_idx];
3328 KASSERT(tbd->tbd_used > 0, ("tbd_used %d", tbd->tbd_used));
3331 tb = &tbd->tbd_buf[buf_idx];
3332 DPRINTF(sc, BWI_DBG_TXEOF, "txeof idx %d, "
3333 "acked %d, data_txcnt %d, ni %p\n",
3334 buf_idx, acked, data_txcnt, tb->tb_ni);
3336 bus_dmamap_unload(sc->sc_buf_dtag, tb->tb_dmap);
3338 if ((ni = tb->tb_ni) != NULL) {
3339 const struct bwi_txbuf_hdr *hdr =
3340 mtod(tb->tb_mbuf, const struct bwi_txbuf_hdr *);
3341 struct ieee80211_ratectl_tx_status txs;
3343 /* NB: update rate control only for unicast frames */
3344 if (hdr->txh_mac_ctrl & htole32(BWI_TXH_MAC_C_ACK)) {
3346 * Feed back 'acked and data_txcnt'. Note that the
3347 * generic AMRR code only understands one tx rate
3348 * and the estimator doesn't handle real retry counts
3349 * well so to avoid over-aggressive downshifting we
3350 * treat any number of retries as "1".
3352 txs.flags = IEEE80211_RATECTL_STATUS_LONG_RETRY;
3353 txs.long_retries = acked;
3355 txs.status = IEEE80211_RATECTL_TX_SUCCESS;
3358 IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
3360 ieee80211_ratectl_tx_complete(ni, &txs);
3362 ieee80211_tx_complete(ni, tb->tb_mbuf, !acked);
3365 m_freem(tb->tb_mbuf);
3368 if (tbd->tbd_used == 0)
3369 sc->sc_tx_timer = 0;
3373 bwi_txeof_status(struct bwi_softc *sc, int end_idx)
3375 struct bwi_txstats_data *st = sc->sc_txstats;
3378 bus_dmamap_sync(st->stats_dtag, st->stats_dmap, BUS_DMASYNC_POSTREAD);
3380 idx = st->stats_idx;
3381 while (idx != end_idx) {
3382 const struct bwi_txstats *stats = &st->stats[idx];
3384 if ((stats->txs_flags & BWI_TXS_F_PENDING) == 0) {
3387 data_txcnt = __SHIFTOUT(stats->txs_txcnt,
3388 BWI_TXS_TXCNT_DATA);
3389 _bwi_txeof(sc, le16toh(stats->txs_id),
3390 stats->txs_flags & BWI_TXS_F_ACKED,
3393 idx = (idx + 1) % BWI_TXSTATS_NDESC;
3395 st->stats_idx = idx;
3399 bwi_txeof(struct bwi_softc *sc)
3403 uint32_t tx_status0, tx_status1;
3407 tx_status0 = CSR_READ_4(sc, BWI_TXSTATUS0);
3408 if ((tx_status0 & BWI_TXSTATUS0_VALID) == 0)
3410 tx_status1 = CSR_READ_4(sc, BWI_TXSTATUS1);
3412 tx_id = __SHIFTOUT(tx_status0, BWI_TXSTATUS0_TXID_MASK);
3413 data_txcnt = __SHIFTOUT(tx_status0,
3414 BWI_TXSTATUS0_DATA_TXCNT_MASK);
3416 if (tx_status0 & (BWI_TXSTATUS0_AMPDU | BWI_TXSTATUS0_PENDING))
3419 _bwi_txeof(sc, le16toh(tx_id), tx_status0 & BWI_TXSTATUS0_ACKED,
3423 bwi_start_locked(sc);
3427 bwi_bbp_power_on(struct bwi_softc *sc, enum bwi_clock_mode clk_mode)
3429 bwi_power_on(sc, 1);
3430 return bwi_set_clock_mode(sc, clk_mode);
3434 bwi_bbp_power_off(struct bwi_softc *sc)
3436 bwi_set_clock_mode(sc, BWI_CLOCK_MODE_SLOW);
3437 bwi_power_off(sc, 1);
3441 bwi_get_pwron_delay(struct bwi_softc *sc)
3443 struct bwi_regwin *com, *old;
3444 struct bwi_clock_freq freq;
3448 com = &sc->sc_com_regwin;
3449 KASSERT(BWI_REGWIN_EXIST(com), ("no regwin"));
3451 if ((sc->sc_cap & BWI_CAP_CLKMODE) == 0)
3454 error = bwi_regwin_switch(sc, com, &old);
3458 bwi_get_clock_freq(sc, &freq);
3460 val = CSR_READ_4(sc, BWI_PLL_ON_DELAY);
3461 sc->sc_pwron_delay = howmany((val + 2) * 1000000, freq.clkfreq_min);
3462 DPRINTF(sc, BWI_DBG_ATTACH, "power on delay %u\n", sc->sc_pwron_delay);
3464 return bwi_regwin_switch(sc, old, NULL);
3468 bwi_bus_attach(struct bwi_softc *sc)
3470 struct bwi_regwin *bus, *old;
3473 bus = &sc->sc_bus_regwin;
3475 error = bwi_regwin_switch(sc, bus, &old);
3479 if (!bwi_regwin_is_enabled(sc, bus))
3480 bwi_regwin_enable(sc, bus, 0);
3482 /* Disable interripts */
3483 CSR_WRITE_4(sc, BWI_INTRVEC, 0);
3485 return bwi_regwin_switch(sc, old, NULL);
3489 bwi_regwin_name(const struct bwi_regwin *rw)
3491 switch (rw->rw_type) {
3492 case BWI_REGWIN_T_COM:
3494 case BWI_REGWIN_T_BUSPCI:
3496 case BWI_REGWIN_T_MAC:
3498 case BWI_REGWIN_T_BUSPCIE:
3501 panic("unknown regwin type 0x%04x\n", rw->rw_type);
3506 bwi_regwin_disable_bits(struct bwi_softc *sc)
3510 /* XXX cache this */
3511 busrev = __SHIFTOUT(CSR_READ_4(sc, BWI_ID_LO), BWI_ID_LO_BUSREV_MASK);
3512 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT | BWI_DBG_MISC,
3513 "bus rev %u\n", busrev);
3515 if (busrev == BWI_BUSREV_0)
3516 return BWI_STATE_LO_DISABLE1;
3517 else if (busrev == BWI_BUSREV_1)
3518 return BWI_STATE_LO_DISABLE2;
3520 return (BWI_STATE_LO_DISABLE1 | BWI_STATE_LO_DISABLE2);
3524 bwi_regwin_is_enabled(struct bwi_softc *sc, struct bwi_regwin *rw)
3526 uint32_t val, disable_bits;
3528 disable_bits = bwi_regwin_disable_bits(sc);
3529 val = CSR_READ_4(sc, BWI_STATE_LO);
3531 if ((val & (BWI_STATE_LO_CLOCK |
3532 BWI_STATE_LO_RESET |
3533 disable_bits)) == BWI_STATE_LO_CLOCK) {
3534 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT, "%s is enabled\n",
3535 bwi_regwin_name(rw));
3538 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT, "%s is disabled\n",
3539 bwi_regwin_name(rw));
3545 bwi_regwin_disable(struct bwi_softc *sc, struct bwi_regwin *rw, uint32_t flags)
3547 uint32_t state_lo, disable_bits;
3550 state_lo = CSR_READ_4(sc, BWI_STATE_LO);
3553 * If current regwin is in 'reset' state, it was already disabled.
3555 if (state_lo & BWI_STATE_LO_RESET) {
3556 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT,
3557 "%s was already disabled\n", bwi_regwin_name(rw));
3561 disable_bits = bwi_regwin_disable_bits(sc);
3564 * Disable normal clock
3566 state_lo = BWI_STATE_LO_CLOCK | disable_bits;
3567 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3570 * Wait until normal clock is disabled
3573 for (i = 0; i < NRETRY; ++i) {
3574 state_lo = CSR_READ_4(sc, BWI_STATE_LO);
3575 if (state_lo & disable_bits)
3580 device_printf(sc->sc_dev, "%s disable clock timeout\n",
3581 bwi_regwin_name(rw));
3584 for (i = 0; i < NRETRY; ++i) {
3587 state_hi = CSR_READ_4(sc, BWI_STATE_HI);
3588 if ((state_hi & BWI_STATE_HI_BUSY) == 0)
3593 device_printf(sc->sc_dev, "%s wait BUSY unset timeout\n",
3594 bwi_regwin_name(rw));
3599 * Reset and disable regwin with gated clock
3601 state_lo = BWI_STATE_LO_RESET | disable_bits |
3602 BWI_STATE_LO_CLOCK | BWI_STATE_LO_GATED_CLOCK |
3603 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3604 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3606 /* Flush pending bus write */
3607 CSR_READ_4(sc, BWI_STATE_LO);
3610 /* Reset and disable regwin */
3611 state_lo = BWI_STATE_LO_RESET | disable_bits |
3612 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3613 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3615 /* Flush pending bus write */
3616 CSR_READ_4(sc, BWI_STATE_LO);
3621 bwi_regwin_enable(struct bwi_softc *sc, struct bwi_regwin *rw, uint32_t flags)
3623 uint32_t state_lo, state_hi, imstate;
3625 bwi_regwin_disable(sc, rw, flags);
3627 /* Reset regwin with gated clock */
3628 state_lo = BWI_STATE_LO_RESET |
3629 BWI_STATE_LO_CLOCK |
3630 BWI_STATE_LO_GATED_CLOCK |
3631 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3632 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3634 /* Flush pending bus write */
3635 CSR_READ_4(sc, BWI_STATE_LO);
3638 state_hi = CSR_READ_4(sc, BWI_STATE_HI);
3639 if (state_hi & BWI_STATE_HI_SERROR)
3640 CSR_WRITE_4(sc, BWI_STATE_HI, 0);
3642 imstate = CSR_READ_4(sc, BWI_IMSTATE);
3643 if (imstate & (BWI_IMSTATE_INBAND_ERR | BWI_IMSTATE_TIMEOUT)) {
3644 imstate &= ~(BWI_IMSTATE_INBAND_ERR | BWI_IMSTATE_TIMEOUT);
3645 CSR_WRITE_4(sc, BWI_IMSTATE, imstate);
3648 /* Enable regwin with gated clock */
3649 state_lo = BWI_STATE_LO_CLOCK |
3650 BWI_STATE_LO_GATED_CLOCK |
3651 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3652 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3654 /* Flush pending bus write */
3655 CSR_READ_4(sc, BWI_STATE_LO);
3658 /* Enable regwin with normal clock */
3659 state_lo = BWI_STATE_LO_CLOCK |
3660 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK);
3661 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
3663 /* Flush pending bus write */
3664 CSR_READ_4(sc, BWI_STATE_LO);
3669 bwi_set_bssid(struct bwi_softc *sc, const uint8_t *bssid)
3671 struct bwi_mac *mac;
3672 struct bwi_myaddr_bssid buf;
3677 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3678 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3679 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3681 bwi_set_addr_filter(sc, BWI_ADDR_FILTER_BSSID, bssid);
3683 bcopy(sc->sc_ic.ic_macaddr, buf.myaddr, sizeof(buf.myaddr));
3684 bcopy(bssid, buf.bssid, sizeof(buf.bssid));
3686 n = sizeof(buf) / sizeof(val);
3687 p = (const uint8_t *)&buf;
3688 for (i = 0; i < n; ++i) {
3692 for (j = 0; j < sizeof(val); ++j)
3693 val |= ((uint32_t)(*p++)) << (j * 8);
3695 TMPLT_WRITE_4(mac, 0x20 + (i * sizeof(val)), val);
3700 bwi_updateslot(struct ieee80211com *ic)
3702 struct bwi_softc *sc = ic->ic_softc;
3703 struct bwi_mac *mac;
3706 if (sc->sc_flags & BWI_F_RUNNING) {
3707 DPRINTF(sc, BWI_DBG_80211, "%s\n", __func__);
3709 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3710 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3711 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3713 bwi_mac_updateslot(mac, (ic->ic_flags & IEEE80211_F_SHSLOT));
3719 bwi_calibrate(void *xsc)
3721 struct bwi_softc *sc = xsc;
3722 struct bwi_mac *mac;
3724 BWI_ASSERT_LOCKED(sc);
3726 KASSERT(sc->sc_ic.ic_opmode != IEEE80211_M_MONITOR,
3727 ("opmode %d", sc->sc_ic.ic_opmode));
3729 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3730 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3731 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3733 bwi_mac_calibrate_txpower(mac, sc->sc_txpwrcb_type);
3734 sc->sc_txpwrcb_type = BWI_TXPWR_CALIB;
3736 /* XXX 15 seconds */
3737 callout_reset(&sc->sc_calib_ch, hz * 15, bwi_calibrate, sc);
3741 bwi_calc_rssi(struct bwi_softc *sc, const struct bwi_rxbuf_hdr *hdr)
3743 struct bwi_mac *mac;
3745 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3746 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3747 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3749 return bwi_rf_calc_rssi(mac, hdr);
3753 bwi_calc_noise(struct bwi_softc *sc)
3755 struct bwi_mac *mac;
3757 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC,
3758 ("current regwin type %d", sc->sc_cur_regwin->rw_type));
3759 mac = (struct bwi_mac *)sc->sc_cur_regwin;
3761 return bwi_rf_calc_noise(mac);
3764 static __inline uint8_t
3765 bwi_plcp2rate(const uint32_t plcp0, enum ieee80211_phytype type)
3767 uint32_t plcp = le32toh(plcp0) & IEEE80211_OFDM_PLCP_RATE_MASK;
3768 return (ieee80211_plcp2rate(plcp, type));
3772 bwi_rx_radiotap(struct bwi_softc *sc, struct mbuf *m,
3773 struct bwi_rxbuf_hdr *hdr, const void *plcp, int rate, int rssi, int noise)
3775 const struct ieee80211_frame_min *wh;
3777 sc->sc_rx_th.wr_flags = IEEE80211_RADIOTAP_F_FCS;
3778 if (htole16(hdr->rxh_flags1) & BWI_RXH_F1_SHPREAMBLE)
3779 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3781 wh = mtod(m, const struct ieee80211_frame_min *);
3782 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)
3783 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_WEP;
3785 sc->sc_rx_th.wr_tsf = hdr->rxh_tsf; /* No endian conversion */
3786 sc->sc_rx_th.wr_rate = rate;
3787 sc->sc_rx_th.wr_antsignal = rssi;
3788 sc->sc_rx_th.wr_antnoise = noise;
3792 bwi_led_attach(struct bwi_softc *sc)
3794 const uint8_t *led_act = NULL;
3795 uint16_t gpio, val[BWI_LED_MAX];
3798 for (i = 0; i < nitems(bwi_vendor_led_act); ++i) {
3799 if (sc->sc_pci_subvid == bwi_vendor_led_act[i].vid) {
3800 led_act = bwi_vendor_led_act[i].led_act;
3804 if (led_act == NULL)
3805 led_act = bwi_default_led_act;
3807 gpio = bwi_read_sprom(sc, BWI_SPROM_GPIO01);
3808 val[0] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_0);
3809 val[1] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_1);
3811 gpio = bwi_read_sprom(sc, BWI_SPROM_GPIO23);
3812 val[2] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_2);
3813 val[3] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_3);
3815 for (i = 0; i < BWI_LED_MAX; ++i) {
3816 struct bwi_led *led = &sc->sc_leds[i];
3818 if (val[i] == 0xff) {
3819 led->l_act = led_act[i];
3821 if (val[i] & BWI_LED_ACT_LOW)
3822 led->l_flags |= BWI_LED_F_ACTLOW;
3823 led->l_act = __SHIFTOUT(val[i], BWI_LED_ACT_MASK);
3825 led->l_mask = (1 << i);
3827 if (led->l_act == BWI_LED_ACT_BLINK_SLOW ||
3828 led->l_act == BWI_LED_ACT_BLINK_POLL ||
3829 led->l_act == BWI_LED_ACT_BLINK) {
3830 led->l_flags |= BWI_LED_F_BLINK;
3831 if (led->l_act == BWI_LED_ACT_BLINK_POLL)
3832 led->l_flags |= BWI_LED_F_POLLABLE;
3833 else if (led->l_act == BWI_LED_ACT_BLINK_SLOW)
3834 led->l_flags |= BWI_LED_F_SLOW;
3836 if (sc->sc_blink_led == NULL) {
3837 sc->sc_blink_led = led;
3838 if (led->l_flags & BWI_LED_F_SLOW)
3839 BWI_LED_SLOWDOWN(sc->sc_led_idle);
3843 DPRINTF(sc, BWI_DBG_LED | BWI_DBG_ATTACH,
3844 "%dth led, act %d, lowact %d\n", i,
3845 led->l_act, led->l_flags & BWI_LED_F_ACTLOW);
3847 callout_init_mtx(&sc->sc_led_blink_ch, &sc->sc_mtx, 0);
3850 static __inline uint16_t
3851 bwi_led_onoff(const struct bwi_led *led, uint16_t val, int on)
3853 if (led->l_flags & BWI_LED_F_ACTLOW)
3858 val &= ~led->l_mask;
3863 bwi_led_newstate(struct bwi_softc *sc, enum ieee80211_state nstate)
3865 struct ieee80211com *ic = &sc->sc_ic;
3869 if (nstate == IEEE80211_S_INIT) {
3870 callout_stop(&sc->sc_led_blink_ch);
3871 sc->sc_led_blinking = 0;
3874 if ((sc->sc_flags & BWI_F_RUNNING) == 0)
3877 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL);
3878 for (i = 0; i < BWI_LED_MAX; ++i) {
3879 struct bwi_led *led = &sc->sc_leds[i];
3882 if (led->l_act == BWI_LED_ACT_UNKN ||
3883 led->l_act == BWI_LED_ACT_NULL)
3886 if ((led->l_flags & BWI_LED_F_BLINK) &&
3887 nstate != IEEE80211_S_INIT)
3890 switch (led->l_act) {
3891 case BWI_LED_ACT_ON: /* Always on */
3894 case BWI_LED_ACT_OFF: /* Always off */
3895 case BWI_LED_ACT_5GHZ: /* TODO: 11A */
3901 case IEEE80211_S_INIT:
3904 case IEEE80211_S_RUN:
3905 if (led->l_act == BWI_LED_ACT_11G &&
3906 ic->ic_curmode != IEEE80211_MODE_11G)
3910 if (led->l_act == BWI_LED_ACT_ASSOC)
3917 val = bwi_led_onoff(led, val, on);
3919 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val);
3922 bwi_led_event(struct bwi_softc *sc, int event)
3924 struct bwi_led *led = sc->sc_blink_led;
3927 if (event == BWI_LED_EVENT_POLL) {
3928 if ((led->l_flags & BWI_LED_F_POLLABLE) == 0)
3930 if (ticks - sc->sc_led_ticks < sc->sc_led_idle)
3934 sc->sc_led_ticks = ticks;
3935 if (sc->sc_led_blinking)
3939 case BWI_LED_EVENT_RX:
3940 rate = sc->sc_rx_rate;
3942 case BWI_LED_EVENT_TX:
3943 rate = sc->sc_tx_rate;
3945 case BWI_LED_EVENT_POLL:
3949 panic("unknown LED event %d\n", event);
3952 bwi_led_blink_start(sc, bwi_led_duration[rate].on_dur,
3953 bwi_led_duration[rate].off_dur);
3957 bwi_led_blink_start(struct bwi_softc *sc, int on_dur, int off_dur)
3959 struct bwi_led *led = sc->sc_blink_led;
3962 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL);
3963 val = bwi_led_onoff(led, val, 1);
3964 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val);
3966 if (led->l_flags & BWI_LED_F_SLOW) {
3967 BWI_LED_SLOWDOWN(on_dur);
3968 BWI_LED_SLOWDOWN(off_dur);
3971 sc->sc_led_blinking = 1;
3972 sc->sc_led_blink_offdur = off_dur;
3974 callout_reset(&sc->sc_led_blink_ch, on_dur, bwi_led_blink_next, sc);
3978 bwi_led_blink_next(void *xsc)
3980 struct bwi_softc *sc = xsc;
3983 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL);
3984 val = bwi_led_onoff(sc->sc_blink_led, val, 0);
3985 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val);
3987 callout_reset(&sc->sc_led_blink_ch, sc->sc_led_blink_offdur,
3988 bwi_led_blink_end, sc);
3992 bwi_led_blink_end(void *xsc)
3994 struct bwi_softc *sc = xsc;
3995 sc->sc_led_blinking = 0;
3999 bwi_restart(void *xsc, int pending)
4001 struct bwi_softc *sc = xsc;
4003 device_printf(sc->sc_dev, "%s begin, help!\n", __func__);
4005 bwi_init_statechg(sc, 0);
4007 bwi_start_locked(sc);