1 /* 2 * Copyright (c) 2007 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Sepherosa Ziehau <sepherosa@gmail.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $DragonFly: src/sys/dev/netif/bwi/if_bwi.c,v 1.19 2008/02/15 11:15:38 sephe Exp $ 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_inet.h" 41 #include "opt_bwi.h" 42 43 #include <sys/param.h> 44 #include <sys/endian.h> 45 #include <sys/kernel.h> 46 #include <sys/bus.h> 47 #include <sys/malloc.h> 48 #include <sys/proc.h> 49 #include <sys/rman.h> 50 #include <sys/socket.h> 51 #include <sys/sockio.h> 52 #include <sys/sysctl.h> 53 #include <sys/systm.h> 54 #include <sys/taskqueue.h> 55 56 #include <net/if.h> 57 #include <net/if_dl.h> 58 #include <net/if_media.h> 59 #include <net/if_types.h> 60 #include <net/if_arp.h> 61 #include <net/ethernet.h> 62 #include <net/if_llc.h> 63 64 #include <net80211/ieee80211_var.h> 65 #include <net80211/ieee80211_radiotap.h> 66 #include <net80211/ieee80211_regdomain.h> 67 #include <net80211/ieee80211_phy.h> 68 #include <net80211/ieee80211_ratectl.h> 69 70 #include <net/bpf.h> 71 72 #ifdef INET 73 #include <netinet/in.h> 74 #include <netinet/if_ether.h> 75 #endif 76 77 #include <machine/bus.h> 78 79 #include <dev/pci/pcivar.h> 80 #include <dev/pci/pcireg.h> 81 82 #include <dev/bwi/bitops.h> 83 #include <dev/bwi/if_bwireg.h> 84 #include <dev/bwi/if_bwivar.h> 85 #include <dev/bwi/bwimac.h> 86 #include <dev/bwi/bwirf.h> 87 88 struct bwi_clock_freq { 89 u_int clkfreq_min; 90 u_int clkfreq_max; 91 }; 92 93 struct bwi_myaddr_bssid { 94 uint8_t myaddr[IEEE80211_ADDR_LEN]; 95 uint8_t bssid[IEEE80211_ADDR_LEN]; 96 } __packed; 97 98 static struct ieee80211vap *bwi_vap_create(struct ieee80211com *, 99 const char [IFNAMSIZ], int, int, int, 100 const uint8_t [IEEE80211_ADDR_LEN], 101 const uint8_t [IEEE80211_ADDR_LEN]); 102 static void bwi_vap_delete(struct ieee80211vap *); 103 static void bwi_init(void *); 104 static int bwi_ioctl(struct ifnet *, u_long, caddr_t); 105 static void bwi_start(struct ifnet *); 106 static void bwi_start_locked(struct ifnet *); 107 static int bwi_raw_xmit(struct ieee80211_node *, struct mbuf *, 108 const struct ieee80211_bpf_params *); 109 static void bwi_watchdog(void *); 110 static void bwi_scan_start(struct ieee80211com *); 111 static void bwi_set_channel(struct ieee80211com *); 112 static void bwi_scan_end(struct ieee80211com *); 113 static int bwi_newstate(struct ieee80211vap *, enum ieee80211_state, int); 114 static void bwi_updateslot(struct ifnet *); 115 static int bwi_media_change(struct ifnet *); 116 117 static void bwi_calibrate(void *); 118 119 static int bwi_calc_rssi(struct bwi_softc *, const struct bwi_rxbuf_hdr *); 120 static int bwi_calc_noise(struct bwi_softc *); 121 static __inline uint8_t bwi_ofdm_plcp2rate(const uint32_t *); 122 static __inline uint8_t bwi_ds_plcp2rate(const struct ieee80211_ds_plcp_hdr *); 123 static void bwi_rx_radiotap(struct bwi_softc *, struct mbuf *, 124 struct bwi_rxbuf_hdr *, const void *, int, int, int); 125 126 static void bwi_restart(void *, int); 127 static void bwi_init_statechg(struct bwi_softc *, int); 128 static void bwi_stop(struct bwi_softc *, int); 129 static void bwi_stop_locked(struct bwi_softc *, int); 130 static int bwi_newbuf(struct bwi_softc *, int, int); 131 static int bwi_encap(struct bwi_softc *, int, struct mbuf *, 132 struct ieee80211_node *); 133 static int bwi_encap_raw(struct bwi_softc *, int, struct mbuf *, 134 struct ieee80211_node *, 135 const struct ieee80211_bpf_params *); 136 137 static void bwi_init_rxdesc_ring32(struct bwi_softc *, uint32_t, 138 bus_addr_t, int, int); 139 static void bwi_reset_rx_ring32(struct bwi_softc *, uint32_t); 140 141 static int bwi_init_tx_ring32(struct bwi_softc *, int); 142 static int bwi_init_rx_ring32(struct bwi_softc *); 143 static int bwi_init_txstats32(struct bwi_softc *); 144 static void bwi_free_tx_ring32(struct bwi_softc *, int); 145 static void bwi_free_rx_ring32(struct bwi_softc *); 146 static void bwi_free_txstats32(struct bwi_softc *); 147 static void bwi_setup_rx_desc32(struct bwi_softc *, int, bus_addr_t, int); 148 static void bwi_setup_tx_desc32(struct bwi_softc *, struct bwi_ring_data *, 149 int, bus_addr_t, int); 150 static int bwi_rxeof32(struct bwi_softc *); 151 static void bwi_start_tx32(struct bwi_softc *, uint32_t, int); 152 static void bwi_txeof_status32(struct bwi_softc *); 153 154 static int bwi_init_tx_ring64(struct bwi_softc *, int); 155 static int bwi_init_rx_ring64(struct bwi_softc *); 156 static int bwi_init_txstats64(struct bwi_softc *); 157 static void bwi_free_tx_ring64(struct bwi_softc *, int); 158 static void bwi_free_rx_ring64(struct bwi_softc *); 159 static void bwi_free_txstats64(struct bwi_softc *); 160 static void bwi_setup_rx_desc64(struct bwi_softc *, int, bus_addr_t, int); 161 static void bwi_setup_tx_desc64(struct bwi_softc *, struct bwi_ring_data *, 162 int, bus_addr_t, int); 163 static int bwi_rxeof64(struct bwi_softc *); 164 static void bwi_start_tx64(struct bwi_softc *, uint32_t, int); 165 static void bwi_txeof_status64(struct bwi_softc *); 166 167 static int bwi_rxeof(struct bwi_softc *, int); 168 static void _bwi_txeof(struct bwi_softc *, uint16_t, int, int); 169 static void bwi_txeof(struct bwi_softc *); 170 static void bwi_txeof_status(struct bwi_softc *, int); 171 static void bwi_enable_intrs(struct bwi_softc *, uint32_t); 172 static void bwi_disable_intrs(struct bwi_softc *, uint32_t); 173 174 static int bwi_dma_alloc(struct bwi_softc *); 175 static void bwi_dma_free(struct bwi_softc *); 176 static int bwi_dma_ring_alloc(struct bwi_softc *, bus_dma_tag_t, 177 struct bwi_ring_data *, bus_size_t, 178 uint32_t); 179 static int bwi_dma_mbuf_create(struct bwi_softc *); 180 static void bwi_dma_mbuf_destroy(struct bwi_softc *, int, int); 181 static int bwi_dma_txstats_alloc(struct bwi_softc *, uint32_t, bus_size_t); 182 static void bwi_dma_txstats_free(struct bwi_softc *); 183 static void bwi_dma_ring_addr(void *, bus_dma_segment_t *, int, int); 184 static void bwi_dma_buf_addr(void *, bus_dma_segment_t *, int, 185 bus_size_t, int); 186 187 static void bwi_power_on(struct bwi_softc *, int); 188 static int bwi_power_off(struct bwi_softc *, int); 189 static int bwi_set_clock_mode(struct bwi_softc *, enum bwi_clock_mode); 190 static int bwi_set_clock_delay(struct bwi_softc *); 191 static void bwi_get_clock_freq(struct bwi_softc *, struct bwi_clock_freq *); 192 static int bwi_get_pwron_delay(struct bwi_softc *sc); 193 static void bwi_set_addr_filter(struct bwi_softc *, uint16_t, 194 const uint8_t *); 195 static void bwi_set_bssid(struct bwi_softc *, const uint8_t *); 196 197 static void bwi_get_card_flags(struct bwi_softc *); 198 static void bwi_get_eaddr(struct bwi_softc *, uint16_t, uint8_t *); 199 200 static int bwi_bus_attach(struct bwi_softc *); 201 static int bwi_bbp_attach(struct bwi_softc *); 202 static int bwi_bbp_power_on(struct bwi_softc *, enum bwi_clock_mode); 203 static void bwi_bbp_power_off(struct bwi_softc *); 204 205 static const char *bwi_regwin_name(const struct bwi_regwin *); 206 static uint32_t bwi_regwin_disable_bits(struct bwi_softc *); 207 static void bwi_regwin_info(struct bwi_softc *, uint16_t *, uint8_t *); 208 static int bwi_regwin_select(struct bwi_softc *, int); 209 210 static void bwi_led_attach(struct bwi_softc *); 211 static void bwi_led_newstate(struct bwi_softc *, enum ieee80211_state); 212 static void bwi_led_event(struct bwi_softc *, int); 213 static void bwi_led_blink_start(struct bwi_softc *, int, int); 214 static void bwi_led_blink_next(void *); 215 static void bwi_led_blink_end(void *); 216 217 static const struct { 218 uint16_t did_min; 219 uint16_t did_max; 220 uint16_t bbp_id; 221 } bwi_bbpid_map[] = { 222 { 0x4301, 0x4301, 0x4301 }, 223 { 0x4305, 0x4307, 0x4307 }, 224 { 0x4403, 0x4403, 0x4402 }, 225 { 0x4610, 0x4615, 0x4610 }, 226 { 0x4710, 0x4715, 0x4710 }, 227 { 0x4720, 0x4725, 0x4309 } 228 }; 229 230 static const struct { 231 uint16_t bbp_id; 232 int nregwin; 233 } bwi_regwin_count[] = { 234 { 0x4301, 5 }, 235 { 0x4306, 6 }, 236 { 0x4307, 5 }, 237 { 0x4310, 8 }, 238 { 0x4401, 3 }, 239 { 0x4402, 3 }, 240 { 0x4610, 9 }, 241 { 0x4704, 9 }, 242 { 0x4710, 9 }, 243 { 0x5365, 7 } 244 }; 245 246 #define CLKSRC(src) \ 247 [BWI_CLKSRC_ ## src] = { \ 248 .freq_min = BWI_CLKSRC_ ##src## _FMIN, \ 249 .freq_max = BWI_CLKSRC_ ##src## _FMAX \ 250 } 251 252 static const struct { 253 u_int freq_min; 254 u_int freq_max; 255 } bwi_clkfreq[BWI_CLKSRC_MAX] = { 256 CLKSRC(LP_OSC), 257 CLKSRC(CS_OSC), 258 CLKSRC(PCI) 259 }; 260 261 #undef CLKSRC 262 263 #define VENDOR_LED_ACT(vendor) \ 264 { \ 265 .vid = PCI_VENDOR_##vendor, \ 266 .led_act = { BWI_VENDOR_LED_ACT_##vendor } \ 267 } 268 269 static const struct { 270 #define PCI_VENDOR_COMPAQ 0x0e11 271 #define PCI_VENDOR_LINKSYS 0x1737 272 uint16_t vid; 273 uint8_t led_act[BWI_LED_MAX]; 274 } bwi_vendor_led_act[] = { 275 VENDOR_LED_ACT(COMPAQ), 276 VENDOR_LED_ACT(LINKSYS) 277 #undef PCI_VENDOR_LINKSYS 278 #undef PCI_VENDOR_COMPAQ 279 }; 280 281 static const uint8_t bwi_default_led_act[BWI_LED_MAX] = 282 { BWI_VENDOR_LED_ACT_DEFAULT }; 283 284 #undef VENDOR_LED_ACT 285 286 static const struct { 287 int on_dur; 288 int off_dur; 289 } bwi_led_duration[109] = { 290 [0] = { 400, 100 }, 291 [2] = { 150, 75 }, 292 [4] = { 90, 45 }, 293 [11] = { 66, 34 }, 294 [12] = { 53, 26 }, 295 [18] = { 42, 21 }, 296 [22] = { 35, 17 }, 297 [24] = { 32, 16 }, 298 [36] = { 21, 10 }, 299 [48] = { 16, 8 }, 300 [72] = { 11, 5 }, 301 [96] = { 9, 4 }, 302 [108] = { 7, 3 } 303 }; 304 305 #ifdef BWI_DEBUG 306 #ifdef BWI_DEBUG_VERBOSE 307 static uint32_t bwi_debug = BWI_DBG_ATTACH | BWI_DBG_INIT | BWI_DBG_TXPOWER; 308 #else 309 static uint32_t bwi_debug; 310 #endif 311 TUNABLE_INT("hw.bwi.debug", (int *)&bwi_debug); 312 #endif /* BWI_DEBUG */ 313 314 static const uint8_t bwi_zero_addr[IEEE80211_ADDR_LEN]; 315 316 uint16_t 317 bwi_read_sprom(struct bwi_softc *sc, uint16_t ofs) 318 { 319 return CSR_READ_2(sc, ofs + BWI_SPROM_START); 320 } 321 322 static __inline void 323 bwi_setup_desc32(struct bwi_softc *sc, struct bwi_desc32 *desc_array, 324 int ndesc, int desc_idx, bus_addr_t paddr, int buf_len, 325 int tx) 326 { 327 struct bwi_desc32 *desc = &desc_array[desc_idx]; 328 uint32_t ctrl, addr, addr_hi, addr_lo; 329 330 addr_lo = __SHIFTOUT(paddr, BWI_DESC32_A_ADDR_MASK); 331 addr_hi = __SHIFTOUT(paddr, BWI_DESC32_A_FUNC_MASK); 332 333 addr = __SHIFTIN(addr_lo, BWI_DESC32_A_ADDR_MASK) | 334 __SHIFTIN(BWI_DESC32_A_FUNC_TXRX, BWI_DESC32_A_FUNC_MASK); 335 336 ctrl = __SHIFTIN(buf_len, BWI_DESC32_C_BUFLEN_MASK) | 337 __SHIFTIN(addr_hi, BWI_DESC32_C_ADDRHI_MASK); 338 if (desc_idx == ndesc - 1) 339 ctrl |= BWI_DESC32_C_EOR; 340 if (tx) { 341 /* XXX */ 342 ctrl |= BWI_DESC32_C_FRAME_START | 343 BWI_DESC32_C_FRAME_END | 344 BWI_DESC32_C_INTR; 345 } 346 347 desc->addr = htole32(addr); 348 desc->ctrl = htole32(ctrl); 349 } 350 351 int 352 bwi_attach(struct bwi_softc *sc) 353 { 354 struct ieee80211com *ic; 355 device_t dev = sc->sc_dev; 356 struct ifnet *ifp; 357 struct bwi_mac *mac; 358 struct bwi_phy *phy; 359 int i, error; 360 uint8_t bands; 361 uint8_t macaddr[IEEE80211_ADDR_LEN]; 362 363 BWI_LOCK_INIT(sc); 364 365 /* 366 * Initialize taskq and various tasks 367 */ 368 sc->sc_tq = taskqueue_create("bwi_taskq", M_NOWAIT | M_ZERO, 369 taskqueue_thread_enqueue, &sc->sc_tq); 370 taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq", 371 device_get_nameunit(dev)); 372 TASK_INIT(&sc->sc_restart_task, 0, bwi_restart, sc); 373 374 callout_init_mtx(&sc->sc_calib_ch, &sc->sc_mtx, 0); 375 376 /* 377 * Initialize sysctl variables 378 */ 379 sc->sc_fw_version = BWI_FW_VERSION3; 380 sc->sc_led_idle = (2350 * hz) / 1000; 381 sc->sc_led_blink = 1; 382 sc->sc_txpwr_calib = 1; 383 #ifdef BWI_DEBUG 384 sc->sc_debug = bwi_debug; 385 #endif 386 bwi_power_on(sc, 1); 387 388 error = bwi_bbp_attach(sc); 389 if (error) 390 goto fail; 391 392 error = bwi_bbp_power_on(sc, BWI_CLOCK_MODE_FAST); 393 if (error) 394 goto fail; 395 396 if (BWI_REGWIN_EXIST(&sc->sc_com_regwin)) { 397 error = bwi_set_clock_delay(sc); 398 if (error) 399 goto fail; 400 401 error = bwi_set_clock_mode(sc, BWI_CLOCK_MODE_FAST); 402 if (error) 403 goto fail; 404 405 error = bwi_get_pwron_delay(sc); 406 if (error) 407 goto fail; 408 } 409 410 error = bwi_bus_attach(sc); 411 if (error) 412 goto fail; 413 414 bwi_get_card_flags(sc); 415 416 bwi_led_attach(sc); 417 418 for (i = 0; i < sc->sc_nmac; ++i) { 419 struct bwi_regwin *old; 420 421 mac = &sc->sc_mac[i]; 422 error = bwi_regwin_switch(sc, &mac->mac_regwin, &old); 423 if (error) 424 goto fail; 425 426 error = bwi_mac_lateattach(mac); 427 if (error) 428 goto fail; 429 430 error = bwi_regwin_switch(sc, old, NULL); 431 if (error) 432 goto fail; 433 } 434 435 /* 436 * XXX First MAC is known to exist 437 * TODO2 438 */ 439 mac = &sc->sc_mac[0]; 440 phy = &mac->mac_phy; 441 442 bwi_bbp_power_off(sc); 443 444 error = bwi_dma_alloc(sc); 445 if (error) 446 goto fail; 447 448 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 449 if (ifp == NULL) { 450 device_printf(dev, "can not if_alloc()\n"); 451 error = ENOSPC; 452 goto fail; 453 } 454 ic = ifp->if_l2com; 455 456 /* set these up early for if_printf use */ 457 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 458 459 ifp->if_softc = sc; 460 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 461 ifp->if_init = bwi_init; 462 ifp->if_ioctl = bwi_ioctl; 463 ifp->if_start = bwi_start; 464 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 465 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 466 IFQ_SET_READY(&ifp->if_snd); 467 callout_init_mtx(&sc->sc_watchdog_timer, &sc->sc_mtx, 0); 468 469 /* 470 * Setup ratesets, phytype, channels and get MAC address 471 */ 472 bands = 0; 473 if (phy->phy_mode == IEEE80211_MODE_11B || 474 phy->phy_mode == IEEE80211_MODE_11G) { 475 setbit(&bands, IEEE80211_MODE_11B); 476 if (phy->phy_mode == IEEE80211_MODE_11B) { 477 ic->ic_phytype = IEEE80211_T_DS; 478 } else { 479 ic->ic_phytype = IEEE80211_T_OFDM; 480 setbit(&bands, IEEE80211_MODE_11G); 481 } 482 483 bwi_get_eaddr(sc, BWI_SPROM_11BG_EADDR, macaddr); 484 if (IEEE80211_IS_MULTICAST(macaddr)) { 485 bwi_get_eaddr(sc, BWI_SPROM_11A_EADDR, macaddr); 486 if (IEEE80211_IS_MULTICAST(macaddr)) { 487 device_printf(dev, 488 "invalid MAC address: %6D\n", 489 macaddr, ":"); 490 } 491 } 492 } else if (phy->phy_mode == IEEE80211_MODE_11A) { 493 /* TODO:11A */ 494 setbit(&bands, IEEE80211_MODE_11A); 495 error = ENXIO; 496 goto fail; 497 } else { 498 panic("unknown phymode %d\n", phy->phy_mode); 499 } 500 501 /* Get locale */ 502 sc->sc_locale = __SHIFTOUT(bwi_read_sprom(sc, BWI_SPROM_CARD_INFO), 503 BWI_SPROM_CARD_INFO_LOCALE); 504 DPRINTF(sc, BWI_DBG_ATTACH, "locale: %d\n", sc->sc_locale); 505 /* XXX use locale */ 506 ieee80211_init_channels(ic, NULL, &bands); 507 508 ic->ic_ifp = ifp; 509 ic->ic_caps = IEEE80211_C_STA | 510 IEEE80211_C_SHSLOT | 511 IEEE80211_C_SHPREAMBLE | 512 IEEE80211_C_WPA | 513 IEEE80211_C_BGSCAN | 514 IEEE80211_C_MONITOR | 515 IEEE80211_C_RATECTL; 516 ic->ic_opmode = IEEE80211_M_STA; 517 ieee80211_ifattach(ic, macaddr); 518 519 ic->ic_headroom = sizeof(struct bwi_txbuf_hdr); 520 521 /* override default methods */ 522 ic->ic_vap_create = bwi_vap_create; 523 ic->ic_vap_delete = bwi_vap_delete; 524 ic->ic_raw_xmit = bwi_raw_xmit; 525 ic->ic_updateslot = bwi_updateslot; 526 ic->ic_scan_start = bwi_scan_start; 527 ic->ic_scan_end = bwi_scan_end; 528 ic->ic_set_channel = bwi_set_channel; 529 530 sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan); 531 532 ieee80211_radiotap_attach(ic, 533 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 534 BWI_TX_RADIOTAP_PRESENT, 535 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 536 BWI_RX_RADIOTAP_PRESENT); 537 538 /* 539 * Add sysctl nodes 540 */ 541 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev), 542 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 543 "fw_version", CTLFLAG_RD, &sc->sc_fw_version, 0, 544 "Firmware version"); 545 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev), 546 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 547 "led_idle", CTLFLAG_RW, &sc->sc_led_idle, 0, 548 "# ticks before LED enters idle state"); 549 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 550 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 551 "led_blink", CTLFLAG_RW, &sc->sc_led_blink, 0, 552 "Allow LED to blink"); 553 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 554 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 555 "txpwr_calib", CTLFLAG_RW, &sc->sc_txpwr_calib, 0, 556 "Enable software TX power calibration"); 557 #ifdef BWI_DEBUG 558 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev), 559 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 560 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "Debug flags"); 561 #endif 562 if (bootverbose) 563 ieee80211_announce(ic); 564 565 return (0); 566 fail: 567 BWI_LOCK_DESTROY(sc); 568 return (error); 569 } 570 571 int 572 bwi_detach(struct bwi_softc *sc) 573 { 574 struct ifnet *ifp = sc->sc_ifp; 575 struct ieee80211com *ic = ifp->if_l2com; 576 int i; 577 578 bwi_stop(sc, 1); 579 callout_drain(&sc->sc_led_blink_ch); 580 callout_drain(&sc->sc_calib_ch); 581 callout_drain(&sc->sc_watchdog_timer); 582 ieee80211_ifdetach(ic); 583 584 for (i = 0; i < sc->sc_nmac; ++i) 585 bwi_mac_detach(&sc->sc_mac[i]); 586 bwi_dma_free(sc); 587 if_free(ifp); 588 taskqueue_free(sc->sc_tq); 589 590 BWI_LOCK_DESTROY(sc); 591 592 return (0); 593 } 594 595 static struct ieee80211vap * 596 bwi_vap_create(struct ieee80211com *ic, 597 const char name[IFNAMSIZ], int unit, int opmode, int flags, 598 const uint8_t bssid[IEEE80211_ADDR_LEN], 599 const uint8_t mac[IEEE80211_ADDR_LEN]) 600 { 601 struct bwi_vap *bvp; 602 struct ieee80211vap *vap; 603 604 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 605 return NULL; 606 bvp = (struct bwi_vap *) malloc(sizeof(struct bwi_vap), 607 M_80211_VAP, M_WAITOK | M_ZERO); 608 if (bvp == NULL) 609 return NULL; 610 vap = &bvp->bv_vap; 611 /* enable s/w bmiss handling for sta mode */ 612 ieee80211_vap_setup(ic, vap, name, unit, opmode, 613 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac); 614 615 /* override default methods */ 616 bvp->bv_newstate = vap->iv_newstate; 617 vap->iv_newstate = bwi_newstate; 618 #if 0 619 vap->iv_update_beacon = bwi_beacon_update; 620 #endif 621 ieee80211_ratectl_init(vap); 622 623 /* complete setup */ 624 ieee80211_vap_attach(vap, bwi_media_change, ieee80211_media_status); 625 ic->ic_opmode = opmode; 626 return vap; 627 } 628 629 static void 630 bwi_vap_delete(struct ieee80211vap *vap) 631 { 632 struct bwi_vap *bvp = BWI_VAP(vap); 633 634 ieee80211_ratectl_deinit(vap); 635 ieee80211_vap_detach(vap); 636 free(bvp, M_80211_VAP); 637 } 638 639 void 640 bwi_suspend(struct bwi_softc *sc) 641 { 642 bwi_stop(sc, 1); 643 } 644 645 void 646 bwi_resume(struct bwi_softc *sc) 647 { 648 struct ifnet *ifp = sc->sc_ifp; 649 650 if (ifp->if_flags & IFF_UP) 651 bwi_init(sc); 652 } 653 654 int 655 bwi_shutdown(struct bwi_softc *sc) 656 { 657 bwi_stop(sc, 1); 658 return 0; 659 } 660 661 static void 662 bwi_power_on(struct bwi_softc *sc, int with_pll) 663 { 664 uint32_t gpio_in, gpio_out, gpio_en; 665 uint16_t status; 666 667 gpio_in = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_IN, 4); 668 if (gpio_in & BWI_PCIM_GPIO_PWR_ON) 669 goto back; 670 671 gpio_out = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4); 672 gpio_en = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, 4); 673 674 gpio_out |= BWI_PCIM_GPIO_PWR_ON; 675 gpio_en |= BWI_PCIM_GPIO_PWR_ON; 676 if (with_pll) { 677 /* Turn off PLL first */ 678 gpio_out |= BWI_PCIM_GPIO_PLL_PWR_OFF; 679 gpio_en |= BWI_PCIM_GPIO_PLL_PWR_OFF; 680 } 681 682 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4); 683 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, gpio_en, 4); 684 DELAY(1000); 685 686 if (with_pll) { 687 /* Turn on PLL */ 688 gpio_out &= ~BWI_PCIM_GPIO_PLL_PWR_OFF; 689 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4); 690 DELAY(5000); 691 } 692 693 back: 694 /* Clear "Signaled Target Abort" */ 695 status = pci_read_config(sc->sc_dev, PCIR_STATUS, 2); 696 status &= ~PCIM_STATUS_STABORT; 697 pci_write_config(sc->sc_dev, PCIR_STATUS, status, 2); 698 } 699 700 static int 701 bwi_power_off(struct bwi_softc *sc, int with_pll) 702 { 703 uint32_t gpio_out, gpio_en; 704 705 pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_IN, 4); /* dummy read */ 706 gpio_out = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4); 707 gpio_en = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, 4); 708 709 gpio_out &= ~BWI_PCIM_GPIO_PWR_ON; 710 gpio_en |= BWI_PCIM_GPIO_PWR_ON; 711 if (with_pll) { 712 gpio_out |= BWI_PCIM_GPIO_PLL_PWR_OFF; 713 gpio_en |= BWI_PCIM_GPIO_PLL_PWR_OFF; 714 } 715 716 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, gpio_out, 4); 717 pci_write_config(sc->sc_dev, BWI_PCIR_GPIO_ENABLE, gpio_en, 4); 718 return 0; 719 } 720 721 int 722 bwi_regwin_switch(struct bwi_softc *sc, struct bwi_regwin *rw, 723 struct bwi_regwin **old_rw) 724 { 725 int error; 726 727 if (old_rw != NULL) 728 *old_rw = NULL; 729 730 if (!BWI_REGWIN_EXIST(rw)) 731 return EINVAL; 732 733 if (sc->sc_cur_regwin != rw) { 734 error = bwi_regwin_select(sc, rw->rw_id); 735 if (error) { 736 device_printf(sc->sc_dev, "can't select regwin %d\n", 737 rw->rw_id); 738 return error; 739 } 740 } 741 742 if (old_rw != NULL) 743 *old_rw = sc->sc_cur_regwin; 744 sc->sc_cur_regwin = rw; 745 return 0; 746 } 747 748 static int 749 bwi_regwin_select(struct bwi_softc *sc, int id) 750 { 751 uint32_t win = BWI_PCIM_REGWIN(id); 752 int i; 753 754 #define RETRY_MAX 50 755 for (i = 0; i < RETRY_MAX; ++i) { 756 pci_write_config(sc->sc_dev, BWI_PCIR_SEL_REGWIN, win, 4); 757 if (pci_read_config(sc->sc_dev, BWI_PCIR_SEL_REGWIN, 4) == win) 758 return 0; 759 DELAY(10); 760 } 761 #undef RETRY_MAX 762 763 return ENXIO; 764 } 765 766 static void 767 bwi_regwin_info(struct bwi_softc *sc, uint16_t *type, uint8_t *rev) 768 { 769 uint32_t val; 770 771 val = CSR_READ_4(sc, BWI_ID_HI); 772 *type = BWI_ID_HI_REGWIN_TYPE(val); 773 *rev = BWI_ID_HI_REGWIN_REV(val); 774 775 DPRINTF(sc, BWI_DBG_ATTACH, "regwin: type 0x%03x, rev %d, " 776 "vendor 0x%04x\n", *type, *rev, 777 __SHIFTOUT(val, BWI_ID_HI_REGWIN_VENDOR_MASK)); 778 } 779 780 static int 781 bwi_bbp_attach(struct bwi_softc *sc) 782 { 783 #define N(arr) (int)(sizeof(arr) / sizeof(arr[0])) 784 uint16_t bbp_id, rw_type; 785 uint8_t rw_rev; 786 uint32_t info; 787 int error, nregwin, i; 788 789 /* 790 * Get 0th regwin information 791 * NOTE: 0th regwin should exist 792 */ 793 error = bwi_regwin_select(sc, 0); 794 if (error) { 795 device_printf(sc->sc_dev, "can't select regwin 0\n"); 796 return error; 797 } 798 bwi_regwin_info(sc, &rw_type, &rw_rev); 799 800 /* 801 * Find out BBP id 802 */ 803 bbp_id = 0; 804 info = 0; 805 if (rw_type == BWI_REGWIN_T_COM) { 806 info = CSR_READ_4(sc, BWI_INFO); 807 bbp_id = __SHIFTOUT(info, BWI_INFO_BBPID_MASK); 808 809 BWI_CREATE_REGWIN(&sc->sc_com_regwin, 0, rw_type, rw_rev); 810 811 sc->sc_cap = CSR_READ_4(sc, BWI_CAPABILITY); 812 } else { 813 for (i = 0; i < N(bwi_bbpid_map); ++i) { 814 if (sc->sc_pci_did >= bwi_bbpid_map[i].did_min && 815 sc->sc_pci_did <= bwi_bbpid_map[i].did_max) { 816 bbp_id = bwi_bbpid_map[i].bbp_id; 817 break; 818 } 819 } 820 if (bbp_id == 0) { 821 device_printf(sc->sc_dev, "no BBP id for device id " 822 "0x%04x\n", sc->sc_pci_did); 823 return ENXIO; 824 } 825 826 info = __SHIFTIN(sc->sc_pci_revid, BWI_INFO_BBPREV_MASK) | 827 __SHIFTIN(0, BWI_INFO_BBPPKG_MASK); 828 } 829 830 /* 831 * Find out number of regwins 832 */ 833 nregwin = 0; 834 if (rw_type == BWI_REGWIN_T_COM && rw_rev >= 4) { 835 nregwin = __SHIFTOUT(info, BWI_INFO_NREGWIN_MASK); 836 } else { 837 for (i = 0; i < N(bwi_regwin_count); ++i) { 838 if (bwi_regwin_count[i].bbp_id == bbp_id) { 839 nregwin = bwi_regwin_count[i].nregwin; 840 break; 841 } 842 } 843 if (nregwin == 0) { 844 device_printf(sc->sc_dev, "no number of win for " 845 "BBP id 0x%04x\n", bbp_id); 846 return ENXIO; 847 } 848 } 849 850 /* Record BBP id/rev for later using */ 851 sc->sc_bbp_id = bbp_id; 852 sc->sc_bbp_rev = __SHIFTOUT(info, BWI_INFO_BBPREV_MASK); 853 sc->sc_bbp_pkg = __SHIFTOUT(info, BWI_INFO_BBPPKG_MASK); 854 device_printf(sc->sc_dev, "BBP: id 0x%04x, rev 0x%x, pkg %d\n", 855 sc->sc_bbp_id, sc->sc_bbp_rev, sc->sc_bbp_pkg); 856 857 DPRINTF(sc, BWI_DBG_ATTACH, "nregwin %d, cap 0x%08x\n", 858 nregwin, sc->sc_cap); 859 860 /* 861 * Create rest of the regwins 862 */ 863 864 /* Don't re-create common regwin, if it is already created */ 865 i = BWI_REGWIN_EXIST(&sc->sc_com_regwin) ? 1 : 0; 866 867 for (; i < nregwin; ++i) { 868 /* 869 * Get regwin information 870 */ 871 error = bwi_regwin_select(sc, i); 872 if (error) { 873 device_printf(sc->sc_dev, 874 "can't select regwin %d\n", i); 875 return error; 876 } 877 bwi_regwin_info(sc, &rw_type, &rw_rev); 878 879 /* 880 * Try attach: 881 * 1) Bus (PCI/PCIE) regwin 882 * 2) MAC regwin 883 * Ignore rest types of regwin 884 */ 885 if (rw_type == BWI_REGWIN_T_BUSPCI || 886 rw_type == BWI_REGWIN_T_BUSPCIE) { 887 if (BWI_REGWIN_EXIST(&sc->sc_bus_regwin)) { 888 device_printf(sc->sc_dev, 889 "bus regwin already exists\n"); 890 } else { 891 BWI_CREATE_REGWIN(&sc->sc_bus_regwin, i, 892 rw_type, rw_rev); 893 } 894 } else if (rw_type == BWI_REGWIN_T_MAC) { 895 /* XXX ignore return value */ 896 bwi_mac_attach(sc, i, rw_rev); 897 } 898 } 899 900 /* At least one MAC shold exist */ 901 if (!BWI_REGWIN_EXIST(&sc->sc_mac[0].mac_regwin)) { 902 device_printf(sc->sc_dev, "no MAC was found\n"); 903 return ENXIO; 904 } 905 KASSERT(sc->sc_nmac > 0, ("no mac's")); 906 907 /* Bus regwin must exist */ 908 if (!BWI_REGWIN_EXIST(&sc->sc_bus_regwin)) { 909 device_printf(sc->sc_dev, "no bus regwin was found\n"); 910 return ENXIO; 911 } 912 913 /* Start with first MAC */ 914 error = bwi_regwin_switch(sc, &sc->sc_mac[0].mac_regwin, NULL); 915 if (error) 916 return error; 917 918 return 0; 919 #undef N 920 } 921 922 int 923 bwi_bus_init(struct bwi_softc *sc, struct bwi_mac *mac) 924 { 925 struct bwi_regwin *old, *bus; 926 uint32_t val; 927 int error; 928 929 bus = &sc->sc_bus_regwin; 930 KASSERT(sc->sc_cur_regwin == &mac->mac_regwin, ("not cur regwin")); 931 932 /* 933 * Tell bus to generate requested interrupts 934 */ 935 if (bus->rw_rev < 6 && bus->rw_type == BWI_REGWIN_T_BUSPCI) { 936 /* 937 * NOTE: Read BWI_FLAGS from MAC regwin 938 */ 939 val = CSR_READ_4(sc, BWI_FLAGS); 940 941 error = bwi_regwin_switch(sc, bus, &old); 942 if (error) 943 return error; 944 945 CSR_SETBITS_4(sc, BWI_INTRVEC, (val & BWI_FLAGS_INTR_MASK)); 946 } else { 947 uint32_t mac_mask; 948 949 mac_mask = 1 << mac->mac_id; 950 951 error = bwi_regwin_switch(sc, bus, &old); 952 if (error) 953 return error; 954 955 val = pci_read_config(sc->sc_dev, BWI_PCIR_INTCTL, 4); 956 val |= mac_mask << 8; 957 pci_write_config(sc->sc_dev, BWI_PCIR_INTCTL, val, 4); 958 } 959 960 if (sc->sc_flags & BWI_F_BUS_INITED) 961 goto back; 962 963 if (bus->rw_type == BWI_REGWIN_T_BUSPCI) { 964 /* 965 * Enable prefetch and burst 966 */ 967 CSR_SETBITS_4(sc, BWI_BUS_CONFIG, 968 BWI_BUS_CONFIG_PREFETCH | BWI_BUS_CONFIG_BURST); 969 970 if (bus->rw_rev < 5) { 971 struct bwi_regwin *com = &sc->sc_com_regwin; 972 973 /* 974 * Configure timeouts for bus operation 975 */ 976 977 /* 978 * Set service timeout and request timeout 979 */ 980 CSR_SETBITS_4(sc, BWI_CONF_LO, 981 __SHIFTIN(BWI_CONF_LO_SERVTO, BWI_CONF_LO_SERVTO_MASK) | 982 __SHIFTIN(BWI_CONF_LO_REQTO, BWI_CONF_LO_REQTO_MASK)); 983 984 /* 985 * If there is common regwin, we switch to that regwin 986 * and switch back to bus regwin once we have done. 987 */ 988 if (BWI_REGWIN_EXIST(com)) { 989 error = bwi_regwin_switch(sc, com, NULL); 990 if (error) 991 return error; 992 } 993 994 /* Let bus know what we have changed */ 995 CSR_WRITE_4(sc, BWI_BUS_ADDR, BWI_BUS_ADDR_MAGIC); 996 CSR_READ_4(sc, BWI_BUS_ADDR); /* Flush */ 997 CSR_WRITE_4(sc, BWI_BUS_DATA, 0); 998 CSR_READ_4(sc, BWI_BUS_DATA); /* Flush */ 999 1000 if (BWI_REGWIN_EXIST(com)) { 1001 error = bwi_regwin_switch(sc, bus, NULL); 1002 if (error) 1003 return error; 1004 } 1005 } else if (bus->rw_rev >= 11) { 1006 /* 1007 * Enable memory read multiple 1008 */ 1009 CSR_SETBITS_4(sc, BWI_BUS_CONFIG, BWI_BUS_CONFIG_MRM); 1010 } 1011 } else { 1012 /* TODO:PCIE */ 1013 } 1014 1015 sc->sc_flags |= BWI_F_BUS_INITED; 1016 back: 1017 return bwi_regwin_switch(sc, old, NULL); 1018 } 1019 1020 static void 1021 bwi_get_card_flags(struct bwi_softc *sc) 1022 { 1023 #define PCI_VENDOR_APPLE 0x106b 1024 #define PCI_VENDOR_DELL 0x1028 1025 sc->sc_card_flags = bwi_read_sprom(sc, BWI_SPROM_CARD_FLAGS); 1026 if (sc->sc_card_flags == 0xffff) 1027 sc->sc_card_flags = 0; 1028 1029 if (sc->sc_pci_subvid == PCI_VENDOR_DELL && 1030 sc->sc_bbp_id == BWI_BBPID_BCM4301 && 1031 sc->sc_pci_revid == 0x74) 1032 sc->sc_card_flags |= BWI_CARD_F_BT_COEXIST; 1033 1034 if (sc->sc_pci_subvid == PCI_VENDOR_APPLE && 1035 sc->sc_pci_subdid == 0x4e && /* XXX */ 1036 sc->sc_pci_revid > 0x40) 1037 sc->sc_card_flags |= BWI_CARD_F_PA_GPIO9; 1038 1039 DPRINTF(sc, BWI_DBG_ATTACH, "card flags 0x%04x\n", sc->sc_card_flags); 1040 #undef PCI_VENDOR_DELL 1041 #undef PCI_VENDOR_APPLE 1042 } 1043 1044 static void 1045 bwi_get_eaddr(struct bwi_softc *sc, uint16_t eaddr_ofs, uint8_t *eaddr) 1046 { 1047 int i; 1048 1049 for (i = 0; i < 3; ++i) { 1050 *((uint16_t *)eaddr + i) = 1051 htobe16(bwi_read_sprom(sc, eaddr_ofs + 2 * i)); 1052 } 1053 } 1054 1055 static void 1056 bwi_get_clock_freq(struct bwi_softc *sc, struct bwi_clock_freq *freq) 1057 { 1058 struct bwi_regwin *com; 1059 uint32_t val; 1060 u_int div; 1061 int src; 1062 1063 bzero(freq, sizeof(*freq)); 1064 com = &sc->sc_com_regwin; 1065 1066 KASSERT(BWI_REGWIN_EXIST(com), ("regwin does not exist")); 1067 KASSERT(sc->sc_cur_regwin == com, ("wrong regwin")); 1068 KASSERT(sc->sc_cap & BWI_CAP_CLKMODE, ("wrong clock mode")); 1069 1070 /* 1071 * Calculate clock frequency 1072 */ 1073 src = -1; 1074 div = 0; 1075 if (com->rw_rev < 6) { 1076 val = pci_read_config(sc->sc_dev, BWI_PCIR_GPIO_OUT, 4); 1077 if (val & BWI_PCIM_GPIO_OUT_CLKSRC) { 1078 src = BWI_CLKSRC_PCI; 1079 div = 64; 1080 } else { 1081 src = BWI_CLKSRC_CS_OSC; 1082 div = 32; 1083 } 1084 } else if (com->rw_rev < 10) { 1085 val = CSR_READ_4(sc, BWI_CLOCK_CTRL); 1086 1087 src = __SHIFTOUT(val, BWI_CLOCK_CTRL_CLKSRC); 1088 if (src == BWI_CLKSRC_LP_OSC) { 1089 div = 1; 1090 } else { 1091 div = (__SHIFTOUT(val, BWI_CLOCK_CTRL_FDIV) + 1) << 2; 1092 1093 /* Unknown source */ 1094 if (src >= BWI_CLKSRC_MAX) 1095 src = BWI_CLKSRC_CS_OSC; 1096 } 1097 } else { 1098 val = CSR_READ_4(sc, BWI_CLOCK_INFO); 1099 1100 src = BWI_CLKSRC_CS_OSC; 1101 div = (__SHIFTOUT(val, BWI_CLOCK_INFO_FDIV) + 1) << 2; 1102 } 1103 1104 KASSERT(src >= 0 && src < BWI_CLKSRC_MAX, ("bad src %d", src)); 1105 KASSERT(div != 0, ("div zero")); 1106 1107 DPRINTF(sc, BWI_DBG_ATTACH, "clksrc %s\n", 1108 src == BWI_CLKSRC_PCI ? "PCI" : 1109 (src == BWI_CLKSRC_LP_OSC ? "LP_OSC" : "CS_OSC")); 1110 1111 freq->clkfreq_min = bwi_clkfreq[src].freq_min / div; 1112 freq->clkfreq_max = bwi_clkfreq[src].freq_max / div; 1113 1114 DPRINTF(sc, BWI_DBG_ATTACH, "clkfreq min %u, max %u\n", 1115 freq->clkfreq_min, freq->clkfreq_max); 1116 } 1117 1118 static int 1119 bwi_set_clock_mode(struct bwi_softc *sc, enum bwi_clock_mode clk_mode) 1120 { 1121 struct bwi_regwin *old, *com; 1122 uint32_t clk_ctrl, clk_src; 1123 int error, pwr_off = 0; 1124 1125 com = &sc->sc_com_regwin; 1126 if (!BWI_REGWIN_EXIST(com)) 1127 return 0; 1128 1129 if (com->rw_rev >= 10 || com->rw_rev < 6) 1130 return 0; 1131 1132 /* 1133 * For common regwin whose rev is [6, 10), the chip 1134 * must be capable to change clock mode. 1135 */ 1136 if ((sc->sc_cap & BWI_CAP_CLKMODE) == 0) 1137 return 0; 1138 1139 error = bwi_regwin_switch(sc, com, &old); 1140 if (error) 1141 return error; 1142 1143 if (clk_mode == BWI_CLOCK_MODE_FAST) 1144 bwi_power_on(sc, 0); /* Don't turn on PLL */ 1145 1146 clk_ctrl = CSR_READ_4(sc, BWI_CLOCK_CTRL); 1147 clk_src = __SHIFTOUT(clk_ctrl, BWI_CLOCK_CTRL_CLKSRC); 1148 1149 switch (clk_mode) { 1150 case BWI_CLOCK_MODE_FAST: 1151 clk_ctrl &= ~BWI_CLOCK_CTRL_SLOW; 1152 clk_ctrl |= BWI_CLOCK_CTRL_IGNPLL; 1153 break; 1154 case BWI_CLOCK_MODE_SLOW: 1155 clk_ctrl |= BWI_CLOCK_CTRL_SLOW; 1156 break; 1157 case BWI_CLOCK_MODE_DYN: 1158 clk_ctrl &= ~(BWI_CLOCK_CTRL_SLOW | 1159 BWI_CLOCK_CTRL_IGNPLL | 1160 BWI_CLOCK_CTRL_NODYN); 1161 if (clk_src != BWI_CLKSRC_CS_OSC) { 1162 clk_ctrl |= BWI_CLOCK_CTRL_NODYN; 1163 pwr_off = 1; 1164 } 1165 break; 1166 } 1167 CSR_WRITE_4(sc, BWI_CLOCK_CTRL, clk_ctrl); 1168 1169 if (pwr_off) 1170 bwi_power_off(sc, 0); /* Leave PLL as it is */ 1171 1172 return bwi_regwin_switch(sc, old, NULL); 1173 } 1174 1175 static int 1176 bwi_set_clock_delay(struct bwi_softc *sc) 1177 { 1178 struct bwi_regwin *old, *com; 1179 int error; 1180 1181 com = &sc->sc_com_regwin; 1182 if (!BWI_REGWIN_EXIST(com)) 1183 return 0; 1184 1185 error = bwi_regwin_switch(sc, com, &old); 1186 if (error) 1187 return error; 1188 1189 if (sc->sc_bbp_id == BWI_BBPID_BCM4321) { 1190 if (sc->sc_bbp_rev == 0) 1191 CSR_WRITE_4(sc, BWI_CONTROL, BWI_CONTROL_MAGIC0); 1192 else if (sc->sc_bbp_rev == 1) 1193 CSR_WRITE_4(sc, BWI_CONTROL, BWI_CONTROL_MAGIC1); 1194 } 1195 1196 if (sc->sc_cap & BWI_CAP_CLKMODE) { 1197 if (com->rw_rev >= 10) { 1198 CSR_FILT_SETBITS_4(sc, BWI_CLOCK_INFO, 0xffff, 0x40000); 1199 } else { 1200 struct bwi_clock_freq freq; 1201 1202 bwi_get_clock_freq(sc, &freq); 1203 CSR_WRITE_4(sc, BWI_PLL_ON_DELAY, 1204 howmany(freq.clkfreq_max * 150, 1000000)); 1205 CSR_WRITE_4(sc, BWI_FREQ_SEL_DELAY, 1206 howmany(freq.clkfreq_max * 15, 1000000)); 1207 } 1208 } 1209 1210 return bwi_regwin_switch(sc, old, NULL); 1211 } 1212 1213 static void 1214 bwi_init(void *xsc) 1215 { 1216 struct bwi_softc *sc = xsc; 1217 struct ifnet *ifp = sc->sc_ifp; 1218 struct ieee80211com *ic = ifp->if_l2com; 1219 1220 BWI_LOCK(sc); 1221 bwi_init_statechg(sc, 1); 1222 BWI_UNLOCK(sc); 1223 1224 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1225 ieee80211_start_all(ic); /* start all vap's */ 1226 } 1227 1228 static void 1229 bwi_init_statechg(struct bwi_softc *sc, int statechg) 1230 { 1231 struct ifnet *ifp = sc->sc_ifp; 1232 struct bwi_mac *mac; 1233 int error; 1234 1235 bwi_stop_locked(sc, statechg); 1236 1237 bwi_bbp_power_on(sc, BWI_CLOCK_MODE_FAST); 1238 1239 /* TODO: 2 MAC */ 1240 1241 mac = &sc->sc_mac[0]; 1242 error = bwi_regwin_switch(sc, &mac->mac_regwin, NULL); 1243 if (error) { 1244 if_printf(ifp, "%s: error %d on regwin switch\n", 1245 __func__, error); 1246 goto bad; 1247 } 1248 error = bwi_mac_init(mac); 1249 if (error) { 1250 if_printf(ifp, "%s: error %d on MAC init\n", __func__, error); 1251 goto bad; 1252 } 1253 1254 bwi_bbp_power_on(sc, BWI_CLOCK_MODE_DYN); 1255 1256 bwi_set_bssid(sc, bwi_zero_addr); /* Clear BSSID */ 1257 bwi_set_addr_filter(sc, BWI_ADDR_FILTER_MYADDR, IF_LLADDR(ifp)); 1258 1259 bwi_mac_reset_hwkeys(mac); 1260 1261 if ((mac->mac_flags & BWI_MAC_F_HAS_TXSTATS) == 0) { 1262 int i; 1263 1264 #define NRETRY 1000 1265 /* 1266 * Drain any possible pending TX status 1267 */ 1268 for (i = 0; i < NRETRY; ++i) { 1269 if ((CSR_READ_4(sc, BWI_TXSTATUS0) & 1270 BWI_TXSTATUS0_VALID) == 0) 1271 break; 1272 CSR_READ_4(sc, BWI_TXSTATUS1); 1273 } 1274 if (i == NRETRY) 1275 if_printf(ifp, "%s: can't drain TX status\n", __func__); 1276 #undef NRETRY 1277 } 1278 1279 if (mac->mac_phy.phy_mode == IEEE80211_MODE_11G) 1280 bwi_mac_updateslot(mac, 1); 1281 1282 /* Start MAC */ 1283 error = bwi_mac_start(mac); 1284 if (error) { 1285 if_printf(ifp, "%s: error %d starting MAC\n", __func__, error); 1286 goto bad; 1287 } 1288 1289 /* Clear stop flag before enabling interrupt */ 1290 sc->sc_flags &= ~BWI_F_STOP; 1291 1292 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1293 callout_reset(&sc->sc_watchdog_timer, hz, bwi_watchdog, sc); 1294 1295 /* Enable intrs */ 1296 bwi_enable_intrs(sc, BWI_INIT_INTRS); 1297 return; 1298 bad: 1299 bwi_stop_locked(sc, 1); 1300 } 1301 1302 static int 1303 bwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1304 { 1305 #define IS_RUNNING(ifp) \ 1306 ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING)) 1307 struct bwi_softc *sc = ifp->if_softc; 1308 struct ieee80211com *ic = ifp->if_l2com; 1309 struct ifreq *ifr = (struct ifreq *) data; 1310 int error = 0, startall = 0; 1311 1312 switch (cmd) { 1313 case SIOCSIFFLAGS: 1314 BWI_LOCK(sc); 1315 if (IS_RUNNING(ifp)) { 1316 struct bwi_mac *mac; 1317 int promisc = -1; 1318 1319 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC, 1320 ("current regwin type %d", 1321 sc->sc_cur_regwin->rw_type)); 1322 mac = (struct bwi_mac *)sc->sc_cur_regwin; 1323 1324 if ((ifp->if_flags & IFF_PROMISC) && 1325 (sc->sc_flags & BWI_F_PROMISC) == 0) { 1326 promisc = 1; 1327 sc->sc_flags |= BWI_F_PROMISC; 1328 } else if ((ifp->if_flags & IFF_PROMISC) == 0 && 1329 (sc->sc_flags & BWI_F_PROMISC)) { 1330 promisc = 0; 1331 sc->sc_flags &= ~BWI_F_PROMISC; 1332 } 1333 1334 if (promisc >= 0) 1335 bwi_mac_set_promisc(mac, promisc); 1336 } 1337 1338 if (ifp->if_flags & IFF_UP) { 1339 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1340 bwi_init_statechg(sc, 1); 1341 startall = 1; 1342 } 1343 } else { 1344 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1345 bwi_stop_locked(sc, 1); 1346 } 1347 BWI_UNLOCK(sc); 1348 if (startall) 1349 ieee80211_start_all(ic); 1350 break; 1351 case SIOCGIFMEDIA: 1352 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 1353 break; 1354 case SIOCGIFADDR: 1355 error = ether_ioctl(ifp, cmd, data); 1356 break; 1357 default: 1358 error = EINVAL; 1359 break; 1360 } 1361 return error; 1362 #undef IS_RUNNING 1363 } 1364 1365 static void 1366 bwi_start(struct ifnet *ifp) 1367 { 1368 struct bwi_softc *sc = ifp->if_softc; 1369 1370 BWI_LOCK(sc); 1371 bwi_start_locked(ifp); 1372 BWI_UNLOCK(sc); 1373 } 1374 1375 static void 1376 bwi_start_locked(struct ifnet *ifp) 1377 { 1378 struct bwi_softc *sc = ifp->if_softc; 1379 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING]; 1380 struct ieee80211_frame *wh; 1381 struct ieee80211_node *ni; 1382 struct ieee80211_key *k; 1383 struct mbuf *m; 1384 int trans, idx; 1385 1386 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1387 return; 1388 1389 trans = 0; 1390 idx = tbd->tbd_idx; 1391 1392 while (tbd->tbd_buf[idx].tb_mbuf == NULL) { 1393 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); /* XXX: LOCK */ 1394 if (m == NULL) 1395 break; 1396 1397 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1398 wh = mtod(m, struct ieee80211_frame *); 1399 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1400 k = ieee80211_crypto_encap(ni, m); 1401 if (k == NULL) { 1402 ieee80211_free_node(ni); 1403 m_freem(m); 1404 ifp->if_oerrors++; 1405 continue; 1406 } 1407 } 1408 wh = NULL; /* Catch any invalid use */ 1409 1410 if (bwi_encap(sc, idx, m, ni) != 0) { 1411 /* 'm' is freed in bwi_encap() if we reach here */ 1412 if (ni != NULL) 1413 ieee80211_free_node(ni); 1414 ifp->if_oerrors++; 1415 continue; 1416 } 1417 1418 trans = 1; 1419 tbd->tbd_used++; 1420 idx = (idx + 1) % BWI_TX_NDESC; 1421 1422 ifp->if_opackets++; 1423 1424 if (tbd->tbd_used + BWI_TX_NSPRDESC >= BWI_TX_NDESC) { 1425 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1426 break; 1427 } 1428 } 1429 tbd->tbd_idx = idx; 1430 1431 if (trans) 1432 sc->sc_tx_timer = 5; 1433 } 1434 1435 static int 1436 bwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 1437 const struct ieee80211_bpf_params *params) 1438 { 1439 struct ieee80211com *ic = ni->ni_ic; 1440 struct ifnet *ifp = ic->ic_ifp; 1441 struct bwi_softc *sc = ifp->if_softc; 1442 /* XXX wme? */ 1443 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING]; 1444 int idx, error; 1445 1446 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1447 ieee80211_free_node(ni); 1448 m_freem(m); 1449 return ENETDOWN; 1450 } 1451 1452 BWI_LOCK(sc); 1453 idx = tbd->tbd_idx; 1454 KASSERT(tbd->tbd_buf[idx].tb_mbuf == NULL, ("slot %d not empty", idx)); 1455 if (params == NULL) { 1456 /* 1457 * Legacy path; interpret frame contents to decide 1458 * precisely how to send the frame. 1459 */ 1460 error = bwi_encap(sc, idx, m, ni); 1461 } else { 1462 /* 1463 * Caller supplied explicit parameters to use in 1464 * sending the frame. 1465 */ 1466 error = bwi_encap_raw(sc, idx, m, ni, params); 1467 } 1468 if (error == 0) { 1469 ifp->if_opackets++; 1470 if (++tbd->tbd_used + BWI_TX_NSPRDESC >= BWI_TX_NDESC) 1471 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1472 tbd->tbd_idx = (idx + 1) % BWI_TX_NDESC; 1473 sc->sc_tx_timer = 5; 1474 } else { 1475 /* NB: m is reclaimed on encap failure */ 1476 ieee80211_free_node(ni); 1477 ifp->if_oerrors++; 1478 } 1479 BWI_UNLOCK(sc); 1480 return error; 1481 } 1482 1483 static void 1484 bwi_watchdog(void *arg) 1485 { 1486 struct bwi_softc *sc; 1487 struct ifnet *ifp; 1488 1489 sc = arg; 1490 ifp = sc->sc_ifp; 1491 BWI_ASSERT_LOCKED(sc); 1492 if (sc->sc_tx_timer != 0 && --sc->sc_tx_timer == 0) { 1493 if_printf(ifp, "watchdog timeout\n"); 1494 ifp->if_oerrors++; 1495 taskqueue_enqueue(sc->sc_tq, &sc->sc_restart_task); 1496 } 1497 callout_reset(&sc->sc_watchdog_timer, hz, bwi_watchdog, sc); 1498 } 1499 1500 static void 1501 bwi_stop(struct bwi_softc *sc, int statechg) 1502 { 1503 BWI_LOCK(sc); 1504 bwi_stop_locked(sc, statechg); 1505 BWI_UNLOCK(sc); 1506 } 1507 1508 static void 1509 bwi_stop_locked(struct bwi_softc *sc, int statechg) 1510 { 1511 struct ifnet *ifp = sc->sc_ifp; 1512 struct bwi_mac *mac; 1513 int i, error, pwr_off = 0; 1514 1515 BWI_ASSERT_LOCKED(sc); 1516 1517 callout_stop(&sc->sc_calib_ch); 1518 callout_stop(&sc->sc_led_blink_ch); 1519 sc->sc_led_blinking = 0; 1520 sc->sc_flags |= BWI_F_STOP; 1521 1522 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1523 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC, 1524 ("current regwin type %d", sc->sc_cur_regwin->rw_type)); 1525 mac = (struct bwi_mac *)sc->sc_cur_regwin; 1526 1527 bwi_disable_intrs(sc, BWI_ALL_INTRS); 1528 CSR_READ_4(sc, BWI_MAC_INTR_MASK); 1529 bwi_mac_stop(mac); 1530 } 1531 1532 for (i = 0; i < sc->sc_nmac; ++i) { 1533 struct bwi_regwin *old_rw; 1534 1535 mac = &sc->sc_mac[i]; 1536 if ((mac->mac_flags & BWI_MAC_F_INITED) == 0) 1537 continue; 1538 1539 error = bwi_regwin_switch(sc, &mac->mac_regwin, &old_rw); 1540 if (error) 1541 continue; 1542 1543 bwi_mac_shutdown(mac); 1544 pwr_off = 1; 1545 1546 bwi_regwin_switch(sc, old_rw, NULL); 1547 } 1548 1549 if (pwr_off) 1550 bwi_bbp_power_off(sc); 1551 1552 sc->sc_tx_timer = 0; 1553 callout_stop(&sc->sc_watchdog_timer); 1554 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1555 } 1556 1557 void 1558 bwi_intr(void *xsc) 1559 { 1560 struct bwi_softc *sc = xsc; 1561 struct ifnet *ifp = sc->sc_ifp; 1562 struct bwi_mac *mac; 1563 uint32_t intr_status; 1564 uint32_t txrx_intr_status[BWI_TXRX_NRING]; 1565 int i, txrx_error, tx = 0, rx_data = -1; 1566 1567 BWI_LOCK(sc); 1568 1569 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 1570 (sc->sc_flags & BWI_F_STOP)) { 1571 BWI_UNLOCK(sc); 1572 return; 1573 } 1574 /* 1575 * Get interrupt status 1576 */ 1577 intr_status = CSR_READ_4(sc, BWI_MAC_INTR_STATUS); 1578 if (intr_status == 0xffffffff) { /* Not for us */ 1579 BWI_UNLOCK(sc); 1580 return; 1581 } 1582 1583 DPRINTF(sc, BWI_DBG_INTR, "intr status 0x%08x\n", intr_status); 1584 1585 intr_status &= CSR_READ_4(sc, BWI_MAC_INTR_MASK); 1586 if (intr_status == 0) { /* Nothing is interesting */ 1587 BWI_UNLOCK(sc); 1588 return; 1589 } 1590 1591 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC, 1592 ("current regwin type %d", sc->sc_cur_regwin->rw_type)); 1593 mac = (struct bwi_mac *)sc->sc_cur_regwin; 1594 1595 txrx_error = 0; 1596 DPRINTF(sc, BWI_DBG_INTR, "%s\n", "TX/RX intr"); 1597 for (i = 0; i < BWI_TXRX_NRING; ++i) { 1598 uint32_t mask; 1599 1600 if (BWI_TXRX_IS_RX(i)) 1601 mask = BWI_TXRX_RX_INTRS; 1602 else 1603 mask = BWI_TXRX_TX_INTRS; 1604 1605 txrx_intr_status[i] = 1606 CSR_READ_4(sc, BWI_TXRX_INTR_STATUS(i)) & mask; 1607 1608 _DPRINTF(sc, BWI_DBG_INTR, ", %d 0x%08x", 1609 i, txrx_intr_status[i]); 1610 1611 if (txrx_intr_status[i] & BWI_TXRX_INTR_ERROR) { 1612 if_printf(ifp, 1613 "%s: intr fatal TX/RX (%d) error 0x%08x\n", 1614 __func__, i, txrx_intr_status[i]); 1615 txrx_error = 1; 1616 } 1617 } 1618 _DPRINTF(sc, BWI_DBG_INTR, "%s\n", ""); 1619 1620 /* 1621 * Acknowledge interrupt 1622 */ 1623 CSR_WRITE_4(sc, BWI_MAC_INTR_STATUS, intr_status); 1624 1625 for (i = 0; i < BWI_TXRX_NRING; ++i) 1626 CSR_WRITE_4(sc, BWI_TXRX_INTR_STATUS(i), txrx_intr_status[i]); 1627 1628 /* Disable all interrupts */ 1629 bwi_disable_intrs(sc, BWI_ALL_INTRS); 1630 1631 /* 1632 * http://bcm-specs.sipsolutions.net/Interrupts 1633 * Says for this bit (0x800): 1634 * "Fatal Error 1635 * 1636 * We got this one while testing things when by accident the 1637 * template ram wasn't set to big endian when it should have 1638 * been after writing the initial values. It keeps on being 1639 * triggered, the only way to stop it seems to shut down the 1640 * chip." 1641 * 1642 * Suggesting that we should never get it and if we do we're not 1643 * feeding TX packets into the MAC correctly if we do... Apparently, 1644 * it is valid only on mac version 5 and higher, but I couldn't 1645 * find a reference for that... Since I see them from time to time 1646 * on my card, this suggests an error in the tx path still... 1647 */ 1648 if (intr_status & BWI_INTR_PHY_TXERR) { 1649 if (mac->mac_flags & BWI_MAC_F_PHYE_RESET) { 1650 if_printf(ifp, "%s: intr PHY TX error\n", __func__); 1651 taskqueue_enqueue(sc->sc_tq, &sc->sc_restart_task); 1652 BWI_UNLOCK(sc); 1653 return; 1654 } 1655 } 1656 1657 if (txrx_error) { 1658 /* TODO: reset device */ 1659 } 1660 1661 if (intr_status & BWI_INTR_TBTT) 1662 bwi_mac_config_ps(mac); 1663 1664 if (intr_status & BWI_INTR_EO_ATIM) 1665 if_printf(ifp, "EO_ATIM\n"); 1666 1667 if (intr_status & BWI_INTR_PMQ) { 1668 for (;;) { 1669 if ((CSR_READ_4(sc, BWI_MAC_PS_STATUS) & 0x8) == 0) 1670 break; 1671 } 1672 CSR_WRITE_2(sc, BWI_MAC_PS_STATUS, 0x2); 1673 } 1674 1675 if (intr_status & BWI_INTR_NOISE) 1676 if_printf(ifp, "intr noise\n"); 1677 1678 if (txrx_intr_status[0] & BWI_TXRX_INTR_RX) { 1679 rx_data = sc->sc_rxeof(sc); 1680 if (sc->sc_flags & BWI_F_STOP) { 1681 BWI_UNLOCK(sc); 1682 return; 1683 } 1684 } 1685 1686 if (txrx_intr_status[3] & BWI_TXRX_INTR_RX) { 1687 sc->sc_txeof_status(sc); 1688 tx = 1; 1689 } 1690 1691 if (intr_status & BWI_INTR_TX_DONE) { 1692 bwi_txeof(sc); 1693 tx = 1; 1694 } 1695 1696 /* Re-enable interrupts */ 1697 bwi_enable_intrs(sc, BWI_INIT_INTRS); 1698 1699 if (sc->sc_blink_led != NULL && sc->sc_led_blink) { 1700 int evt = BWI_LED_EVENT_NONE; 1701 1702 if (tx && rx_data > 0) { 1703 if (sc->sc_rx_rate > sc->sc_tx_rate) 1704 evt = BWI_LED_EVENT_RX; 1705 else 1706 evt = BWI_LED_EVENT_TX; 1707 } else if (tx) { 1708 evt = BWI_LED_EVENT_TX; 1709 } else if (rx_data > 0) { 1710 evt = BWI_LED_EVENT_RX; 1711 } else if (rx_data == 0) { 1712 evt = BWI_LED_EVENT_POLL; 1713 } 1714 1715 if (evt != BWI_LED_EVENT_NONE) 1716 bwi_led_event(sc, evt); 1717 } 1718 1719 BWI_UNLOCK(sc); 1720 } 1721 1722 static void 1723 bwi_scan_start(struct ieee80211com *ic) 1724 { 1725 struct bwi_softc *sc = ic->ic_ifp->if_softc; 1726 1727 BWI_LOCK(sc); 1728 /* Enable MAC beacon promiscuity */ 1729 CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PASS_BCN); 1730 BWI_UNLOCK(sc); 1731 } 1732 1733 static void 1734 bwi_set_channel(struct ieee80211com *ic) 1735 { 1736 struct bwi_softc *sc = ic->ic_ifp->if_softc; 1737 struct ieee80211_channel *c = ic->ic_curchan; 1738 struct bwi_mac *mac; 1739 1740 BWI_LOCK(sc); 1741 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC, 1742 ("current regwin type %d", sc->sc_cur_regwin->rw_type)); 1743 mac = (struct bwi_mac *)sc->sc_cur_regwin; 1744 bwi_rf_set_chan(mac, ieee80211_chan2ieee(ic, c), 0); 1745 1746 sc->sc_rates = ieee80211_get_ratetable(c); 1747 1748 /* 1749 * Setup radio tap channel freq and flags 1750 */ 1751 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq = 1752 htole16(c->ic_freq); 1753 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags = 1754 htole16(c->ic_flags & 0xffff); 1755 1756 BWI_UNLOCK(sc); 1757 } 1758 1759 static void 1760 bwi_scan_end(struct ieee80211com *ic) 1761 { 1762 struct bwi_softc *sc = ic->ic_ifp->if_softc; 1763 1764 BWI_LOCK(sc); 1765 CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PASS_BCN); 1766 BWI_UNLOCK(sc); 1767 } 1768 1769 static int 1770 bwi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1771 { 1772 struct bwi_vap *bvp = BWI_VAP(vap); 1773 const struct ieee80211_txparam *tp; 1774 struct ieee80211com *ic= vap->iv_ic; 1775 struct ifnet *ifp = ic->ic_ifp; 1776 enum ieee80211_state ostate = vap->iv_state; 1777 struct bwi_softc *sc = ifp->if_softc; 1778 struct bwi_mac *mac; 1779 int error; 1780 1781 BWI_LOCK(sc); 1782 1783 callout_stop(&sc->sc_calib_ch); 1784 1785 if (nstate == IEEE80211_S_INIT) 1786 sc->sc_txpwrcb_type = BWI_TXPWR_INIT; 1787 1788 bwi_led_newstate(sc, nstate); 1789 1790 error = bvp->bv_newstate(vap, nstate, arg); 1791 if (error != 0) 1792 goto back; 1793 1794 /* 1795 * Clear the BSSID when we stop a STA 1796 */ 1797 if (vap->iv_opmode == IEEE80211_M_STA) { 1798 if (ostate == IEEE80211_S_RUN && nstate != IEEE80211_S_RUN) { 1799 /* 1800 * Clear out the BSSID. If we reassociate to 1801 * the same AP, this will reinialize things 1802 * correctly... 1803 */ 1804 if (ic->ic_opmode == IEEE80211_M_STA && 1805 !(sc->sc_flags & BWI_F_STOP)) 1806 bwi_set_bssid(sc, bwi_zero_addr); 1807 } 1808 } 1809 1810 if (vap->iv_opmode == IEEE80211_M_MONITOR) { 1811 /* Nothing to do */ 1812 } else if (nstate == IEEE80211_S_RUN) { 1813 bwi_set_bssid(sc, vap->iv_bss->ni_bssid); 1814 1815 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC, 1816 ("current regwin type %d", sc->sc_cur_regwin->rw_type)); 1817 mac = (struct bwi_mac *)sc->sc_cur_regwin; 1818 1819 /* Initial TX power calibration */ 1820 bwi_mac_calibrate_txpower(mac, BWI_TXPWR_INIT); 1821 #ifdef notyet 1822 sc->sc_txpwrcb_type = BWI_TXPWR_FORCE; 1823 #else 1824 sc->sc_txpwrcb_type = BWI_TXPWR_CALIB; 1825 #endif 1826 1827 /* Initializes ratectl for a node. */ 1828 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1829 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) 1830 ieee80211_ratectl_node_init(vap->iv_bss); 1831 1832 callout_reset(&sc->sc_calib_ch, hz, bwi_calibrate, sc); 1833 } 1834 back: 1835 BWI_UNLOCK(sc); 1836 1837 return error; 1838 } 1839 1840 static int 1841 bwi_media_change(struct ifnet *ifp) 1842 { 1843 int error = ieee80211_media_change(ifp); 1844 /* NB: only the fixed rate can change and that doesn't need a reset */ 1845 return (error == ENETRESET ? 0 : error); 1846 } 1847 1848 static int 1849 bwi_dma_alloc(struct bwi_softc *sc) 1850 { 1851 int error, i, has_txstats; 1852 bus_addr_t lowaddr = 0; 1853 bus_size_t tx_ring_sz, rx_ring_sz, desc_sz = 0; 1854 uint32_t txrx_ctrl_step = 0; 1855 1856 has_txstats = 0; 1857 for (i = 0; i < sc->sc_nmac; ++i) { 1858 if (sc->sc_mac[i].mac_flags & BWI_MAC_F_HAS_TXSTATS) { 1859 has_txstats = 1; 1860 break; 1861 } 1862 } 1863 1864 switch (sc->sc_bus_space) { 1865 case BWI_BUS_SPACE_30BIT: 1866 case BWI_BUS_SPACE_32BIT: 1867 if (sc->sc_bus_space == BWI_BUS_SPACE_30BIT) 1868 lowaddr = BWI_BUS_SPACE_MAXADDR; 1869 else 1870 lowaddr = BUS_SPACE_MAXADDR_32BIT; 1871 desc_sz = sizeof(struct bwi_desc32); 1872 txrx_ctrl_step = 0x20; 1873 1874 sc->sc_init_tx_ring = bwi_init_tx_ring32; 1875 sc->sc_free_tx_ring = bwi_free_tx_ring32; 1876 sc->sc_init_rx_ring = bwi_init_rx_ring32; 1877 sc->sc_free_rx_ring = bwi_free_rx_ring32; 1878 sc->sc_setup_rxdesc = bwi_setup_rx_desc32; 1879 sc->sc_setup_txdesc = bwi_setup_tx_desc32; 1880 sc->sc_rxeof = bwi_rxeof32; 1881 sc->sc_start_tx = bwi_start_tx32; 1882 if (has_txstats) { 1883 sc->sc_init_txstats = bwi_init_txstats32; 1884 sc->sc_free_txstats = bwi_free_txstats32; 1885 sc->sc_txeof_status = bwi_txeof_status32; 1886 } 1887 break; 1888 1889 case BWI_BUS_SPACE_64BIT: 1890 lowaddr = BUS_SPACE_MAXADDR; /* XXX */ 1891 desc_sz = sizeof(struct bwi_desc64); 1892 txrx_ctrl_step = 0x40; 1893 1894 sc->sc_init_tx_ring = bwi_init_tx_ring64; 1895 sc->sc_free_tx_ring = bwi_free_tx_ring64; 1896 sc->sc_init_rx_ring = bwi_init_rx_ring64; 1897 sc->sc_free_rx_ring = bwi_free_rx_ring64; 1898 sc->sc_setup_rxdesc = bwi_setup_rx_desc64; 1899 sc->sc_setup_txdesc = bwi_setup_tx_desc64; 1900 sc->sc_rxeof = bwi_rxeof64; 1901 sc->sc_start_tx = bwi_start_tx64; 1902 if (has_txstats) { 1903 sc->sc_init_txstats = bwi_init_txstats64; 1904 sc->sc_free_txstats = bwi_free_txstats64; 1905 sc->sc_txeof_status = bwi_txeof_status64; 1906 } 1907 break; 1908 } 1909 1910 KASSERT(lowaddr != 0, ("lowaddr zero")); 1911 KASSERT(desc_sz != 0, ("desc_sz zero")); 1912 KASSERT(txrx_ctrl_step != 0, ("txrx_ctrl_step zero")); 1913 1914 tx_ring_sz = roundup(desc_sz * BWI_TX_NDESC, BWI_RING_ALIGN); 1915 rx_ring_sz = roundup(desc_sz * BWI_RX_NDESC, BWI_RING_ALIGN); 1916 1917 /* 1918 * Create top level DMA tag 1919 */ 1920 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), /* parent */ 1921 BWI_ALIGN, 0, /* alignment, bounds */ 1922 lowaddr, /* lowaddr */ 1923 BUS_SPACE_MAXADDR, /* highaddr */ 1924 NULL, NULL, /* filter, filterarg */ 1925 MAXBSIZE, /* maxsize */ 1926 BUS_SPACE_UNRESTRICTED, /* nsegments */ 1927 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 1928 BUS_DMA_ALLOCNOW, /* flags */ 1929 NULL, NULL, /* lockfunc, lockarg */ 1930 &sc->sc_parent_dtag); 1931 if (error) { 1932 device_printf(sc->sc_dev, "can't create parent DMA tag\n"); 1933 return error; 1934 } 1935 1936 #define TXRX_CTRL(idx) (BWI_TXRX_CTRL_BASE + (idx) * txrx_ctrl_step) 1937 1938 /* 1939 * Create TX ring DMA stuffs 1940 */ 1941 error = bus_dma_tag_create(sc->sc_parent_dtag, 1942 BWI_RING_ALIGN, 0, 1943 BUS_SPACE_MAXADDR, 1944 BUS_SPACE_MAXADDR, 1945 NULL, NULL, 1946 tx_ring_sz, 1947 1, 1948 BUS_SPACE_MAXSIZE_32BIT, 1949 BUS_DMA_ALLOCNOW, 1950 NULL, NULL, 1951 &sc->sc_txring_dtag); 1952 if (error) { 1953 device_printf(sc->sc_dev, "can't create TX ring DMA tag\n"); 1954 return error; 1955 } 1956 1957 for (i = 0; i < BWI_TX_NRING; ++i) { 1958 error = bwi_dma_ring_alloc(sc, sc->sc_txring_dtag, 1959 &sc->sc_tx_rdata[i], tx_ring_sz, 1960 TXRX_CTRL(i)); 1961 if (error) { 1962 device_printf(sc->sc_dev, "%dth TX ring " 1963 "DMA alloc failed\n", i); 1964 return error; 1965 } 1966 } 1967 1968 /* 1969 * Create RX ring DMA stuffs 1970 */ 1971 error = bus_dma_tag_create(sc->sc_parent_dtag, 1972 BWI_RING_ALIGN, 0, 1973 BUS_SPACE_MAXADDR, 1974 BUS_SPACE_MAXADDR, 1975 NULL, NULL, 1976 rx_ring_sz, 1977 1, 1978 BUS_SPACE_MAXSIZE_32BIT, 1979 BUS_DMA_ALLOCNOW, 1980 NULL, NULL, 1981 &sc->sc_rxring_dtag); 1982 if (error) { 1983 device_printf(sc->sc_dev, "can't create RX ring DMA tag\n"); 1984 return error; 1985 } 1986 1987 error = bwi_dma_ring_alloc(sc, sc->sc_rxring_dtag, &sc->sc_rx_rdata, 1988 rx_ring_sz, TXRX_CTRL(0)); 1989 if (error) { 1990 device_printf(sc->sc_dev, "RX ring DMA alloc failed\n"); 1991 return error; 1992 } 1993 1994 if (has_txstats) { 1995 error = bwi_dma_txstats_alloc(sc, TXRX_CTRL(3), desc_sz); 1996 if (error) { 1997 device_printf(sc->sc_dev, 1998 "TX stats DMA alloc failed\n"); 1999 return error; 2000 } 2001 } 2002 2003 #undef TXRX_CTRL 2004 2005 return bwi_dma_mbuf_create(sc); 2006 } 2007 2008 static void 2009 bwi_dma_free(struct bwi_softc *sc) 2010 { 2011 if (sc->sc_txring_dtag != NULL) { 2012 int i; 2013 2014 for (i = 0; i < BWI_TX_NRING; ++i) { 2015 struct bwi_ring_data *rd = &sc->sc_tx_rdata[i]; 2016 2017 if (rd->rdata_desc != NULL) { 2018 bus_dmamap_unload(sc->sc_txring_dtag, 2019 rd->rdata_dmap); 2020 bus_dmamem_free(sc->sc_txring_dtag, 2021 rd->rdata_desc, 2022 rd->rdata_dmap); 2023 } 2024 } 2025 bus_dma_tag_destroy(sc->sc_txring_dtag); 2026 } 2027 2028 if (sc->sc_rxring_dtag != NULL) { 2029 struct bwi_ring_data *rd = &sc->sc_rx_rdata; 2030 2031 if (rd->rdata_desc != NULL) { 2032 bus_dmamap_unload(sc->sc_rxring_dtag, rd->rdata_dmap); 2033 bus_dmamem_free(sc->sc_rxring_dtag, rd->rdata_desc, 2034 rd->rdata_dmap); 2035 } 2036 bus_dma_tag_destroy(sc->sc_rxring_dtag); 2037 } 2038 2039 bwi_dma_txstats_free(sc); 2040 bwi_dma_mbuf_destroy(sc, BWI_TX_NRING, 1); 2041 2042 if (sc->sc_parent_dtag != NULL) 2043 bus_dma_tag_destroy(sc->sc_parent_dtag); 2044 } 2045 2046 static int 2047 bwi_dma_ring_alloc(struct bwi_softc *sc, bus_dma_tag_t dtag, 2048 struct bwi_ring_data *rd, bus_size_t size, 2049 uint32_t txrx_ctrl) 2050 { 2051 int error; 2052 2053 error = bus_dmamem_alloc(dtag, &rd->rdata_desc, 2054 BUS_DMA_WAITOK | BUS_DMA_ZERO, 2055 &rd->rdata_dmap); 2056 if (error) { 2057 device_printf(sc->sc_dev, "can't allocate DMA mem\n"); 2058 return error; 2059 } 2060 2061 error = bus_dmamap_load(dtag, rd->rdata_dmap, rd->rdata_desc, size, 2062 bwi_dma_ring_addr, &rd->rdata_paddr, 2063 BUS_DMA_NOWAIT); 2064 if (error) { 2065 device_printf(sc->sc_dev, "can't load DMA mem\n"); 2066 bus_dmamem_free(dtag, rd->rdata_desc, rd->rdata_dmap); 2067 rd->rdata_desc = NULL; 2068 return error; 2069 } 2070 2071 rd->rdata_txrx_ctrl = txrx_ctrl; 2072 return 0; 2073 } 2074 2075 static int 2076 bwi_dma_txstats_alloc(struct bwi_softc *sc, uint32_t ctrl_base, 2077 bus_size_t desc_sz) 2078 { 2079 struct bwi_txstats_data *st; 2080 bus_size_t dma_size; 2081 int error; 2082 2083 st = malloc(sizeof(*st), M_DEVBUF, M_NOWAIT | M_ZERO); 2084 if (st == NULL) { 2085 device_printf(sc->sc_dev, "can't allocate txstats data\n"); 2086 return ENOMEM; 2087 } 2088 sc->sc_txstats = st; 2089 2090 /* 2091 * Create TX stats descriptor DMA stuffs 2092 */ 2093 dma_size = roundup(desc_sz * BWI_TXSTATS_NDESC, BWI_RING_ALIGN); 2094 2095 error = bus_dma_tag_create(sc->sc_parent_dtag, 2096 BWI_RING_ALIGN, 2097 0, 2098 BUS_SPACE_MAXADDR, 2099 BUS_SPACE_MAXADDR, 2100 NULL, NULL, 2101 dma_size, 2102 1, 2103 BUS_SPACE_MAXSIZE_32BIT, 2104 BUS_DMA_ALLOCNOW, 2105 NULL, NULL, 2106 &st->stats_ring_dtag); 2107 if (error) { 2108 device_printf(sc->sc_dev, "can't create txstats ring " 2109 "DMA tag\n"); 2110 return error; 2111 } 2112 2113 error = bus_dmamem_alloc(st->stats_ring_dtag, &st->stats_ring, 2114 BUS_DMA_WAITOK | BUS_DMA_ZERO, 2115 &st->stats_ring_dmap); 2116 if (error) { 2117 device_printf(sc->sc_dev, "can't allocate txstats ring " 2118 "DMA mem\n"); 2119 bus_dma_tag_destroy(st->stats_ring_dtag); 2120 st->stats_ring_dtag = NULL; 2121 return error; 2122 } 2123 2124 error = bus_dmamap_load(st->stats_ring_dtag, st->stats_ring_dmap, 2125 st->stats_ring, dma_size, 2126 bwi_dma_ring_addr, &st->stats_ring_paddr, 2127 BUS_DMA_NOWAIT); 2128 if (error) { 2129 device_printf(sc->sc_dev, "can't load txstats ring DMA mem\n"); 2130 bus_dmamem_free(st->stats_ring_dtag, st->stats_ring, 2131 st->stats_ring_dmap); 2132 bus_dma_tag_destroy(st->stats_ring_dtag); 2133 st->stats_ring_dtag = NULL; 2134 return error; 2135 } 2136 2137 /* 2138 * Create TX stats DMA stuffs 2139 */ 2140 dma_size = roundup(sizeof(struct bwi_txstats) * BWI_TXSTATS_NDESC, 2141 BWI_ALIGN); 2142 2143 error = bus_dma_tag_create(sc->sc_parent_dtag, 2144 BWI_ALIGN, 2145 0, 2146 BUS_SPACE_MAXADDR, 2147 BUS_SPACE_MAXADDR, 2148 NULL, NULL, 2149 dma_size, 2150 1, 2151 BUS_SPACE_MAXSIZE_32BIT, 2152 BUS_DMA_ALLOCNOW, 2153 NULL, NULL, 2154 &st->stats_dtag); 2155 if (error) { 2156 device_printf(sc->sc_dev, "can't create txstats DMA tag\n"); 2157 return error; 2158 } 2159 2160 error = bus_dmamem_alloc(st->stats_dtag, (void **)&st->stats, 2161 BUS_DMA_WAITOK | BUS_DMA_ZERO, 2162 &st->stats_dmap); 2163 if (error) { 2164 device_printf(sc->sc_dev, "can't allocate txstats DMA mem\n"); 2165 bus_dma_tag_destroy(st->stats_dtag); 2166 st->stats_dtag = NULL; 2167 return error; 2168 } 2169 2170 error = bus_dmamap_load(st->stats_dtag, st->stats_dmap, st->stats, 2171 dma_size, bwi_dma_ring_addr, &st->stats_paddr, 2172 BUS_DMA_NOWAIT); 2173 if (error) { 2174 device_printf(sc->sc_dev, "can't load txstats DMA mem\n"); 2175 bus_dmamem_free(st->stats_dtag, st->stats, st->stats_dmap); 2176 bus_dma_tag_destroy(st->stats_dtag); 2177 st->stats_dtag = NULL; 2178 return error; 2179 } 2180 2181 st->stats_ctrl_base = ctrl_base; 2182 return 0; 2183 } 2184 2185 static void 2186 bwi_dma_txstats_free(struct bwi_softc *sc) 2187 { 2188 struct bwi_txstats_data *st; 2189 2190 if (sc->sc_txstats == NULL) 2191 return; 2192 st = sc->sc_txstats; 2193 2194 if (st->stats_ring_dtag != NULL) { 2195 bus_dmamap_unload(st->stats_ring_dtag, st->stats_ring_dmap); 2196 bus_dmamem_free(st->stats_ring_dtag, st->stats_ring, 2197 st->stats_ring_dmap); 2198 bus_dma_tag_destroy(st->stats_ring_dtag); 2199 } 2200 2201 if (st->stats_dtag != NULL) { 2202 bus_dmamap_unload(st->stats_dtag, st->stats_dmap); 2203 bus_dmamem_free(st->stats_dtag, st->stats, st->stats_dmap); 2204 bus_dma_tag_destroy(st->stats_dtag); 2205 } 2206 2207 free(st, M_DEVBUF); 2208 } 2209 2210 static void 2211 bwi_dma_ring_addr(void *arg, bus_dma_segment_t *seg, int nseg, int error) 2212 { 2213 KASSERT(nseg == 1, ("too many segments\n")); 2214 *((bus_addr_t *)arg) = seg->ds_addr; 2215 } 2216 2217 static int 2218 bwi_dma_mbuf_create(struct bwi_softc *sc) 2219 { 2220 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata; 2221 int i, j, k, ntx, error; 2222 2223 /* 2224 * Create TX/RX mbuf DMA tag 2225 */ 2226 error = bus_dma_tag_create(sc->sc_parent_dtag, 2227 1, 2228 0, 2229 BUS_SPACE_MAXADDR, 2230 BUS_SPACE_MAXADDR, 2231 NULL, NULL, 2232 MCLBYTES, 2233 1, 2234 BUS_SPACE_MAXSIZE_32BIT, 2235 BUS_DMA_ALLOCNOW, 2236 NULL, NULL, 2237 &sc->sc_buf_dtag); 2238 if (error) { 2239 device_printf(sc->sc_dev, "can't create mbuf DMA tag\n"); 2240 return error; 2241 } 2242 2243 ntx = 0; 2244 2245 /* 2246 * Create TX mbuf DMA map 2247 */ 2248 for (i = 0; i < BWI_TX_NRING; ++i) { 2249 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[i]; 2250 2251 for (j = 0; j < BWI_TX_NDESC; ++j) { 2252 error = bus_dmamap_create(sc->sc_buf_dtag, 0, 2253 &tbd->tbd_buf[j].tb_dmap); 2254 if (error) { 2255 device_printf(sc->sc_dev, "can't create " 2256 "%dth tbd, %dth DMA map\n", i, j); 2257 2258 ntx = i; 2259 for (k = 0; k < j; ++k) { 2260 bus_dmamap_destroy(sc->sc_buf_dtag, 2261 tbd->tbd_buf[k].tb_dmap); 2262 } 2263 goto fail; 2264 } 2265 } 2266 } 2267 ntx = BWI_TX_NRING; 2268 2269 /* 2270 * Create RX mbuf DMA map and a spare DMA map 2271 */ 2272 error = bus_dmamap_create(sc->sc_buf_dtag, 0, 2273 &rbd->rbd_tmp_dmap); 2274 if (error) { 2275 device_printf(sc->sc_dev, 2276 "can't create spare RX buf DMA map\n"); 2277 goto fail; 2278 } 2279 2280 for (j = 0; j < BWI_RX_NDESC; ++j) { 2281 error = bus_dmamap_create(sc->sc_buf_dtag, 0, 2282 &rbd->rbd_buf[j].rb_dmap); 2283 if (error) { 2284 device_printf(sc->sc_dev, "can't create %dth " 2285 "RX buf DMA map\n", j); 2286 2287 for (k = 0; k < j; ++k) { 2288 bus_dmamap_destroy(sc->sc_buf_dtag, 2289 rbd->rbd_buf[j].rb_dmap); 2290 } 2291 bus_dmamap_destroy(sc->sc_buf_dtag, 2292 rbd->rbd_tmp_dmap); 2293 goto fail; 2294 } 2295 } 2296 2297 return 0; 2298 fail: 2299 bwi_dma_mbuf_destroy(sc, ntx, 0); 2300 return error; 2301 } 2302 2303 static void 2304 bwi_dma_mbuf_destroy(struct bwi_softc *sc, int ntx, int nrx) 2305 { 2306 int i, j; 2307 2308 if (sc->sc_buf_dtag == NULL) 2309 return; 2310 2311 for (i = 0; i < ntx; ++i) { 2312 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[i]; 2313 2314 for (j = 0; j < BWI_TX_NDESC; ++j) { 2315 struct bwi_txbuf *tb = &tbd->tbd_buf[j]; 2316 2317 if (tb->tb_mbuf != NULL) { 2318 bus_dmamap_unload(sc->sc_buf_dtag, 2319 tb->tb_dmap); 2320 m_freem(tb->tb_mbuf); 2321 } 2322 if (tb->tb_ni != NULL) 2323 ieee80211_free_node(tb->tb_ni); 2324 bus_dmamap_destroy(sc->sc_buf_dtag, tb->tb_dmap); 2325 } 2326 } 2327 2328 if (nrx) { 2329 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata; 2330 2331 bus_dmamap_destroy(sc->sc_buf_dtag, rbd->rbd_tmp_dmap); 2332 for (j = 0; j < BWI_RX_NDESC; ++j) { 2333 struct bwi_rxbuf *rb = &rbd->rbd_buf[j]; 2334 2335 if (rb->rb_mbuf != NULL) { 2336 bus_dmamap_unload(sc->sc_buf_dtag, 2337 rb->rb_dmap); 2338 m_freem(rb->rb_mbuf); 2339 } 2340 bus_dmamap_destroy(sc->sc_buf_dtag, rb->rb_dmap); 2341 } 2342 } 2343 2344 bus_dma_tag_destroy(sc->sc_buf_dtag); 2345 sc->sc_buf_dtag = NULL; 2346 } 2347 2348 static void 2349 bwi_enable_intrs(struct bwi_softc *sc, uint32_t enable_intrs) 2350 { 2351 CSR_SETBITS_4(sc, BWI_MAC_INTR_MASK, enable_intrs); 2352 } 2353 2354 static void 2355 bwi_disable_intrs(struct bwi_softc *sc, uint32_t disable_intrs) 2356 { 2357 CSR_CLRBITS_4(sc, BWI_MAC_INTR_MASK, disable_intrs); 2358 } 2359 2360 static int 2361 bwi_init_tx_ring32(struct bwi_softc *sc, int ring_idx) 2362 { 2363 struct bwi_ring_data *rd; 2364 struct bwi_txbuf_data *tbd; 2365 uint32_t val, addr_hi, addr_lo; 2366 2367 KASSERT(ring_idx < BWI_TX_NRING, ("ring_idx %d", ring_idx)); 2368 rd = &sc->sc_tx_rdata[ring_idx]; 2369 tbd = &sc->sc_tx_bdata[ring_idx]; 2370 2371 tbd->tbd_idx = 0; 2372 tbd->tbd_used = 0; 2373 2374 bzero(rd->rdata_desc, sizeof(struct bwi_desc32) * BWI_TX_NDESC); 2375 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap, 2376 BUS_DMASYNC_PREWRITE); 2377 2378 addr_lo = __SHIFTOUT(rd->rdata_paddr, BWI_TXRX32_RINGINFO_ADDR_MASK); 2379 addr_hi = __SHIFTOUT(rd->rdata_paddr, BWI_TXRX32_RINGINFO_FUNC_MASK); 2380 2381 val = __SHIFTIN(addr_lo, BWI_TXRX32_RINGINFO_ADDR_MASK) | 2382 __SHIFTIN(BWI_TXRX32_RINGINFO_FUNC_TXRX, 2383 BWI_TXRX32_RINGINFO_FUNC_MASK); 2384 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_RINGINFO, val); 2385 2386 val = __SHIFTIN(addr_hi, BWI_TXRX32_CTRL_ADDRHI_MASK) | 2387 BWI_TXRX32_CTRL_ENABLE; 2388 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_CTRL, val); 2389 2390 return 0; 2391 } 2392 2393 static void 2394 bwi_init_rxdesc_ring32(struct bwi_softc *sc, uint32_t ctrl_base, 2395 bus_addr_t paddr, int hdr_size, int ndesc) 2396 { 2397 uint32_t val, addr_hi, addr_lo; 2398 2399 addr_lo = __SHIFTOUT(paddr, BWI_TXRX32_RINGINFO_ADDR_MASK); 2400 addr_hi = __SHIFTOUT(paddr, BWI_TXRX32_RINGINFO_FUNC_MASK); 2401 2402 val = __SHIFTIN(addr_lo, BWI_TXRX32_RINGINFO_ADDR_MASK) | 2403 __SHIFTIN(BWI_TXRX32_RINGINFO_FUNC_TXRX, 2404 BWI_TXRX32_RINGINFO_FUNC_MASK); 2405 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_RINGINFO, val); 2406 2407 val = __SHIFTIN(hdr_size, BWI_RX32_CTRL_HDRSZ_MASK) | 2408 __SHIFTIN(addr_hi, BWI_TXRX32_CTRL_ADDRHI_MASK) | 2409 BWI_TXRX32_CTRL_ENABLE; 2410 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_CTRL, val); 2411 2412 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_INDEX, 2413 (ndesc - 1) * sizeof(struct bwi_desc32)); 2414 } 2415 2416 static int 2417 bwi_init_rx_ring32(struct bwi_softc *sc) 2418 { 2419 struct bwi_ring_data *rd = &sc->sc_rx_rdata; 2420 int i, error; 2421 2422 sc->sc_rx_bdata.rbd_idx = 0; 2423 2424 for (i = 0; i < BWI_RX_NDESC; ++i) { 2425 error = bwi_newbuf(sc, i, 1); 2426 if (error) { 2427 device_printf(sc->sc_dev, 2428 "can't allocate %dth RX buffer\n", i); 2429 return error; 2430 } 2431 } 2432 bus_dmamap_sync(sc->sc_rxring_dtag, rd->rdata_dmap, 2433 BUS_DMASYNC_PREWRITE); 2434 2435 bwi_init_rxdesc_ring32(sc, rd->rdata_txrx_ctrl, rd->rdata_paddr, 2436 sizeof(struct bwi_rxbuf_hdr), BWI_RX_NDESC); 2437 return 0; 2438 } 2439 2440 static int 2441 bwi_init_txstats32(struct bwi_softc *sc) 2442 { 2443 struct bwi_txstats_data *st = sc->sc_txstats; 2444 bus_addr_t stats_paddr; 2445 int i; 2446 2447 bzero(st->stats, BWI_TXSTATS_NDESC * sizeof(struct bwi_txstats)); 2448 bus_dmamap_sync(st->stats_dtag, st->stats_dmap, BUS_DMASYNC_PREWRITE); 2449 2450 st->stats_idx = 0; 2451 2452 stats_paddr = st->stats_paddr; 2453 for (i = 0; i < BWI_TXSTATS_NDESC; ++i) { 2454 bwi_setup_desc32(sc, st->stats_ring, BWI_TXSTATS_NDESC, i, 2455 stats_paddr, sizeof(struct bwi_txstats), 0); 2456 stats_paddr += sizeof(struct bwi_txstats); 2457 } 2458 bus_dmamap_sync(st->stats_ring_dtag, st->stats_ring_dmap, 2459 BUS_DMASYNC_PREWRITE); 2460 2461 bwi_init_rxdesc_ring32(sc, st->stats_ctrl_base, 2462 st->stats_ring_paddr, 0, BWI_TXSTATS_NDESC); 2463 return 0; 2464 } 2465 2466 static void 2467 bwi_setup_rx_desc32(struct bwi_softc *sc, int buf_idx, bus_addr_t paddr, 2468 int buf_len) 2469 { 2470 struct bwi_ring_data *rd = &sc->sc_rx_rdata; 2471 2472 KASSERT(buf_idx < BWI_RX_NDESC, ("buf_idx %d", buf_idx)); 2473 bwi_setup_desc32(sc, rd->rdata_desc, BWI_RX_NDESC, buf_idx, 2474 paddr, buf_len, 0); 2475 } 2476 2477 static void 2478 bwi_setup_tx_desc32(struct bwi_softc *sc, struct bwi_ring_data *rd, 2479 int buf_idx, bus_addr_t paddr, int buf_len) 2480 { 2481 KASSERT(buf_idx < BWI_TX_NDESC, ("buf_idx %d", buf_idx)); 2482 bwi_setup_desc32(sc, rd->rdata_desc, BWI_TX_NDESC, buf_idx, 2483 paddr, buf_len, 1); 2484 } 2485 2486 static int 2487 bwi_init_tx_ring64(struct bwi_softc *sc, int ring_idx) 2488 { 2489 /* TODO:64 */ 2490 return EOPNOTSUPP; 2491 } 2492 2493 static int 2494 bwi_init_rx_ring64(struct bwi_softc *sc) 2495 { 2496 /* TODO:64 */ 2497 return EOPNOTSUPP; 2498 } 2499 2500 static int 2501 bwi_init_txstats64(struct bwi_softc *sc) 2502 { 2503 /* TODO:64 */ 2504 return EOPNOTSUPP; 2505 } 2506 2507 static void 2508 bwi_setup_rx_desc64(struct bwi_softc *sc, int buf_idx, bus_addr_t paddr, 2509 int buf_len) 2510 { 2511 /* TODO:64 */ 2512 } 2513 2514 static void 2515 bwi_setup_tx_desc64(struct bwi_softc *sc, struct bwi_ring_data *rd, 2516 int buf_idx, bus_addr_t paddr, int buf_len) 2517 { 2518 /* TODO:64 */ 2519 } 2520 2521 static void 2522 bwi_dma_buf_addr(void *arg, bus_dma_segment_t *seg, int nseg, 2523 bus_size_t mapsz __unused, int error) 2524 { 2525 if (!error) { 2526 KASSERT(nseg == 1, ("too many segments(%d)\n", nseg)); 2527 *((bus_addr_t *)arg) = seg->ds_addr; 2528 } 2529 } 2530 2531 static int 2532 bwi_newbuf(struct bwi_softc *sc, int buf_idx, int init) 2533 { 2534 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata; 2535 struct bwi_rxbuf *rxbuf = &rbd->rbd_buf[buf_idx]; 2536 struct bwi_rxbuf_hdr *hdr; 2537 bus_dmamap_t map; 2538 bus_addr_t paddr; 2539 struct mbuf *m; 2540 int error; 2541 2542 KASSERT(buf_idx < BWI_RX_NDESC, ("buf_idx %d", buf_idx)); 2543 2544 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 2545 if (m == NULL) { 2546 error = ENOBUFS; 2547 2548 /* 2549 * If the NIC is up and running, we need to: 2550 * - Clear RX buffer's header. 2551 * - Restore RX descriptor settings. 2552 */ 2553 if (init) 2554 return error; 2555 else 2556 goto back; 2557 } 2558 m->m_len = m->m_pkthdr.len = MCLBYTES; 2559 2560 /* 2561 * Try to load RX buf into temporary DMA map 2562 */ 2563 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, rbd->rbd_tmp_dmap, m, 2564 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT); 2565 if (error) { 2566 m_freem(m); 2567 2568 /* 2569 * See the comment above 2570 */ 2571 if (init) 2572 return error; 2573 else 2574 goto back; 2575 } 2576 2577 if (!init) 2578 bus_dmamap_unload(sc->sc_buf_dtag, rxbuf->rb_dmap); 2579 rxbuf->rb_mbuf = m; 2580 rxbuf->rb_paddr = paddr; 2581 2582 /* 2583 * Swap RX buf's DMA map with the loaded temporary one 2584 */ 2585 map = rxbuf->rb_dmap; 2586 rxbuf->rb_dmap = rbd->rbd_tmp_dmap; 2587 rbd->rbd_tmp_dmap = map; 2588 2589 back: 2590 /* 2591 * Clear RX buf header 2592 */ 2593 hdr = mtod(rxbuf->rb_mbuf, struct bwi_rxbuf_hdr *); 2594 bzero(hdr, sizeof(*hdr)); 2595 bus_dmamap_sync(sc->sc_buf_dtag, rxbuf->rb_dmap, BUS_DMASYNC_PREWRITE); 2596 2597 /* 2598 * Setup RX buf descriptor 2599 */ 2600 sc->sc_setup_rxdesc(sc, buf_idx, rxbuf->rb_paddr, 2601 rxbuf->rb_mbuf->m_len - sizeof(*hdr)); 2602 return error; 2603 } 2604 2605 static void 2606 bwi_set_addr_filter(struct bwi_softc *sc, uint16_t addr_ofs, 2607 const uint8_t *addr) 2608 { 2609 int i; 2610 2611 CSR_WRITE_2(sc, BWI_ADDR_FILTER_CTRL, 2612 BWI_ADDR_FILTER_CTRL_SET | addr_ofs); 2613 2614 for (i = 0; i < (IEEE80211_ADDR_LEN / 2); ++i) { 2615 uint16_t addr_val; 2616 2617 addr_val = (uint16_t)addr[i * 2] | 2618 (((uint16_t)addr[(i * 2) + 1]) << 8); 2619 CSR_WRITE_2(sc, BWI_ADDR_FILTER_DATA, addr_val); 2620 } 2621 } 2622 2623 static int 2624 bwi_rxeof(struct bwi_softc *sc, int end_idx) 2625 { 2626 struct bwi_ring_data *rd = &sc->sc_rx_rdata; 2627 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata; 2628 struct ifnet *ifp = sc->sc_ifp; 2629 struct ieee80211com *ic = ifp->if_l2com; 2630 int idx, rx_data = 0; 2631 2632 idx = rbd->rbd_idx; 2633 while (idx != end_idx) { 2634 struct bwi_rxbuf *rb = &rbd->rbd_buf[idx]; 2635 struct bwi_rxbuf_hdr *hdr; 2636 struct ieee80211_frame_min *wh; 2637 struct ieee80211_node *ni; 2638 struct mbuf *m; 2639 const void *plcp; 2640 uint16_t flags2; 2641 int buflen, wh_ofs, hdr_extra, rssi, noise, type, rate; 2642 2643 m = rb->rb_mbuf; 2644 bus_dmamap_sync(sc->sc_buf_dtag, rb->rb_dmap, 2645 BUS_DMASYNC_POSTREAD); 2646 2647 if (bwi_newbuf(sc, idx, 0)) { 2648 ifp->if_ierrors++; 2649 goto next; 2650 } 2651 2652 hdr = mtod(m, struct bwi_rxbuf_hdr *); 2653 flags2 = le16toh(hdr->rxh_flags2); 2654 2655 hdr_extra = 0; 2656 if (flags2 & BWI_RXH_F2_TYPE2FRAME) 2657 hdr_extra = 2; 2658 wh_ofs = hdr_extra + 6; /* XXX magic number */ 2659 2660 buflen = le16toh(hdr->rxh_buflen); 2661 if (buflen < BWI_FRAME_MIN_LEN(wh_ofs)) { 2662 if_printf(ifp, "%s: zero length data, hdr_extra %d\n", 2663 __func__, hdr_extra); 2664 ifp->if_ierrors++; 2665 m_freem(m); 2666 goto next; 2667 } 2668 2669 plcp = ((const uint8_t *)(hdr + 1) + hdr_extra); 2670 rssi = bwi_calc_rssi(sc, hdr); 2671 noise = bwi_calc_noise(sc); 2672 2673 m->m_pkthdr.rcvif = ifp; 2674 m->m_len = m->m_pkthdr.len = buflen + sizeof(*hdr); 2675 m_adj(m, sizeof(*hdr) + wh_ofs); 2676 2677 if (htole16(hdr->rxh_flags1) & BWI_RXH_F1_OFDM) 2678 rate = bwi_ofdm_plcp2rate(plcp); 2679 else 2680 rate = bwi_ds_plcp2rate(plcp); 2681 2682 /* RX radio tap */ 2683 if (ieee80211_radiotap_active(ic)) 2684 bwi_rx_radiotap(sc, m, hdr, plcp, rate, rssi, noise); 2685 2686 m_adj(m, -IEEE80211_CRC_LEN); 2687 2688 BWI_UNLOCK(sc); 2689 2690 wh = mtod(m, struct ieee80211_frame_min *); 2691 ni = ieee80211_find_rxnode(ic, wh); 2692 if (ni != NULL) { 2693 type = ieee80211_input(ni, m, rssi - noise, noise); 2694 ieee80211_free_node(ni); 2695 } else 2696 type = ieee80211_input_all(ic, m, rssi - noise, noise); 2697 if (type == IEEE80211_FC0_TYPE_DATA) { 2698 rx_data = 1; 2699 sc->sc_rx_rate = rate; 2700 } 2701 2702 BWI_LOCK(sc); 2703 next: 2704 idx = (idx + 1) % BWI_RX_NDESC; 2705 2706 if (sc->sc_flags & BWI_F_STOP) { 2707 /* 2708 * Take the fast lane, don't do 2709 * any damage to softc 2710 */ 2711 return -1; 2712 } 2713 } 2714 2715 rbd->rbd_idx = idx; 2716 bus_dmamap_sync(sc->sc_rxring_dtag, rd->rdata_dmap, 2717 BUS_DMASYNC_PREWRITE); 2718 2719 return rx_data; 2720 } 2721 2722 static int 2723 bwi_rxeof32(struct bwi_softc *sc) 2724 { 2725 uint32_t val, rx_ctrl; 2726 int end_idx, rx_data; 2727 2728 rx_ctrl = sc->sc_rx_rdata.rdata_txrx_ctrl; 2729 2730 val = CSR_READ_4(sc, rx_ctrl + BWI_RX32_STATUS); 2731 end_idx = __SHIFTOUT(val, BWI_RX32_STATUS_INDEX_MASK) / 2732 sizeof(struct bwi_desc32); 2733 2734 rx_data = bwi_rxeof(sc, end_idx); 2735 if (rx_data >= 0) { 2736 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_INDEX, 2737 end_idx * sizeof(struct bwi_desc32)); 2738 } 2739 return rx_data; 2740 } 2741 2742 static int 2743 bwi_rxeof64(struct bwi_softc *sc) 2744 { 2745 /* TODO:64 */ 2746 return 0; 2747 } 2748 2749 static void 2750 bwi_reset_rx_ring32(struct bwi_softc *sc, uint32_t rx_ctrl) 2751 { 2752 int i; 2753 2754 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_CTRL, 0); 2755 2756 #define NRETRY 10 2757 2758 for (i = 0; i < NRETRY; ++i) { 2759 uint32_t status; 2760 2761 status = CSR_READ_4(sc, rx_ctrl + BWI_RX32_STATUS); 2762 if (__SHIFTOUT(status, BWI_RX32_STATUS_STATE_MASK) == 2763 BWI_RX32_STATUS_STATE_DISABLED) 2764 break; 2765 2766 DELAY(1000); 2767 } 2768 if (i == NRETRY) 2769 device_printf(sc->sc_dev, "reset rx ring timedout\n"); 2770 2771 #undef NRETRY 2772 2773 CSR_WRITE_4(sc, rx_ctrl + BWI_RX32_RINGINFO, 0); 2774 } 2775 2776 static void 2777 bwi_free_txstats32(struct bwi_softc *sc) 2778 { 2779 bwi_reset_rx_ring32(sc, sc->sc_txstats->stats_ctrl_base); 2780 } 2781 2782 static void 2783 bwi_free_rx_ring32(struct bwi_softc *sc) 2784 { 2785 struct bwi_ring_data *rd = &sc->sc_rx_rdata; 2786 struct bwi_rxbuf_data *rbd = &sc->sc_rx_bdata; 2787 int i; 2788 2789 bwi_reset_rx_ring32(sc, rd->rdata_txrx_ctrl); 2790 2791 for (i = 0; i < BWI_RX_NDESC; ++i) { 2792 struct bwi_rxbuf *rb = &rbd->rbd_buf[i]; 2793 2794 if (rb->rb_mbuf != NULL) { 2795 bus_dmamap_unload(sc->sc_buf_dtag, rb->rb_dmap); 2796 m_freem(rb->rb_mbuf); 2797 rb->rb_mbuf = NULL; 2798 } 2799 } 2800 } 2801 2802 static void 2803 bwi_free_tx_ring32(struct bwi_softc *sc, int ring_idx) 2804 { 2805 struct bwi_ring_data *rd; 2806 struct bwi_txbuf_data *tbd; 2807 struct ifnet *ifp = sc->sc_ifp; 2808 uint32_t state, val; 2809 int i; 2810 2811 KASSERT(ring_idx < BWI_TX_NRING, ("ring_idx %d", ring_idx)); 2812 rd = &sc->sc_tx_rdata[ring_idx]; 2813 tbd = &sc->sc_tx_bdata[ring_idx]; 2814 2815 #define NRETRY 10 2816 2817 for (i = 0; i < NRETRY; ++i) { 2818 val = CSR_READ_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_STATUS); 2819 state = __SHIFTOUT(val, BWI_TX32_STATUS_STATE_MASK); 2820 if (state == BWI_TX32_STATUS_STATE_DISABLED || 2821 state == BWI_TX32_STATUS_STATE_IDLE || 2822 state == BWI_TX32_STATUS_STATE_STOPPED) 2823 break; 2824 2825 DELAY(1000); 2826 } 2827 if (i == NRETRY) { 2828 if_printf(ifp, "%s: wait for TX ring(%d) stable timed out\n", 2829 __func__, ring_idx); 2830 } 2831 2832 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_CTRL, 0); 2833 for (i = 0; i < NRETRY; ++i) { 2834 val = CSR_READ_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_STATUS); 2835 state = __SHIFTOUT(val, BWI_TX32_STATUS_STATE_MASK); 2836 if (state == BWI_TX32_STATUS_STATE_DISABLED) 2837 break; 2838 2839 DELAY(1000); 2840 } 2841 if (i == NRETRY) 2842 if_printf(ifp, "%s: reset TX ring (%d) timed out\n", 2843 __func__, ring_idx); 2844 2845 #undef NRETRY 2846 2847 DELAY(1000); 2848 2849 CSR_WRITE_4(sc, rd->rdata_txrx_ctrl + BWI_TX32_RINGINFO, 0); 2850 2851 for (i = 0; i < BWI_TX_NDESC; ++i) { 2852 struct bwi_txbuf *tb = &tbd->tbd_buf[i]; 2853 2854 if (tb->tb_mbuf != NULL) { 2855 bus_dmamap_unload(sc->sc_buf_dtag, tb->tb_dmap); 2856 m_freem(tb->tb_mbuf); 2857 tb->tb_mbuf = NULL; 2858 } 2859 if (tb->tb_ni != NULL) { 2860 ieee80211_free_node(tb->tb_ni); 2861 tb->tb_ni = NULL; 2862 } 2863 } 2864 } 2865 2866 static void 2867 bwi_free_txstats64(struct bwi_softc *sc) 2868 { 2869 /* TODO:64 */ 2870 } 2871 2872 static void 2873 bwi_free_rx_ring64(struct bwi_softc *sc) 2874 { 2875 /* TODO:64 */ 2876 } 2877 2878 static void 2879 bwi_free_tx_ring64(struct bwi_softc *sc, int ring_idx) 2880 { 2881 /* TODO:64 */ 2882 } 2883 2884 /* XXX does not belong here */ 2885 #define IEEE80211_OFDM_PLCP_RATE_MASK __BITS(3, 0) 2886 #define IEEE80211_OFDM_PLCP_LEN_MASK __BITS(16, 5) 2887 2888 static __inline void 2889 bwi_ofdm_plcp_header(uint32_t *plcp0, int pkt_len, uint8_t rate) 2890 { 2891 uint32_t plcp; 2892 2893 plcp = __SHIFTIN(ieee80211_rate2plcp(rate, IEEE80211_T_OFDM), 2894 IEEE80211_OFDM_PLCP_RATE_MASK) | 2895 __SHIFTIN(pkt_len, IEEE80211_OFDM_PLCP_LEN_MASK); 2896 *plcp0 = htole32(plcp); 2897 } 2898 2899 static __inline void 2900 bwi_ds_plcp_header(struct ieee80211_ds_plcp_hdr *plcp, int pkt_len, 2901 uint8_t rate) 2902 { 2903 int len, service, pkt_bitlen; 2904 2905 pkt_bitlen = pkt_len * NBBY; 2906 len = howmany(pkt_bitlen * 2, rate); 2907 2908 service = IEEE80211_PLCP_SERVICE_LOCKED; 2909 if (rate == (11 * 2)) { 2910 int pkt_bitlen1; 2911 2912 /* 2913 * PLCP service field needs to be adjusted, 2914 * if TX rate is 11Mbytes/s 2915 */ 2916 pkt_bitlen1 = len * 11; 2917 if (pkt_bitlen1 - pkt_bitlen >= NBBY) 2918 service |= IEEE80211_PLCP_SERVICE_LENEXT7; 2919 } 2920 2921 plcp->i_signal = ieee80211_rate2plcp(rate, IEEE80211_T_CCK); 2922 plcp->i_service = service; 2923 plcp->i_length = htole16(len); 2924 /* NOTE: do NOT touch i_crc */ 2925 } 2926 2927 static __inline void 2928 bwi_plcp_header(const struct ieee80211_rate_table *rt, 2929 void *plcp, int pkt_len, uint8_t rate) 2930 { 2931 enum ieee80211_phytype modtype; 2932 2933 /* 2934 * Assume caller has zeroed 'plcp' 2935 */ 2936 modtype = ieee80211_rate2phytype(rt, rate); 2937 if (modtype == IEEE80211_T_OFDM) 2938 bwi_ofdm_plcp_header(plcp, pkt_len, rate); 2939 else if (modtype == IEEE80211_T_DS) 2940 bwi_ds_plcp_header(plcp, pkt_len, rate); 2941 else 2942 panic("unsupport modulation type %u\n", modtype); 2943 } 2944 2945 static int 2946 bwi_encap(struct bwi_softc *sc, int idx, struct mbuf *m, 2947 struct ieee80211_node *ni) 2948 { 2949 struct ieee80211vap *vap = ni->ni_vap; 2950 struct ifnet *ifp = sc->sc_ifp; 2951 struct ieee80211com *ic = ifp->if_l2com; 2952 struct bwi_ring_data *rd = &sc->sc_tx_rdata[BWI_TX_DATA_RING]; 2953 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING]; 2954 struct bwi_txbuf *tb = &tbd->tbd_buf[idx]; 2955 struct bwi_mac *mac; 2956 struct bwi_txbuf_hdr *hdr; 2957 struct ieee80211_frame *wh; 2958 const struct ieee80211_txparam *tp; 2959 uint8_t rate, rate_fb; 2960 uint32_t mac_ctrl; 2961 uint16_t phy_ctrl; 2962 bus_addr_t paddr; 2963 int type, ismcast, pkt_len, error, rix; 2964 #if 0 2965 const uint8_t *p; 2966 int i; 2967 #endif 2968 2969 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC, 2970 ("current regwin type %d", sc->sc_cur_regwin->rw_type)); 2971 mac = (struct bwi_mac *)sc->sc_cur_regwin; 2972 2973 wh = mtod(m, struct ieee80211_frame *); 2974 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 2975 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 2976 2977 /* Get 802.11 frame len before prepending TX header */ 2978 pkt_len = m->m_pkthdr.len + IEEE80211_CRC_LEN; 2979 2980 /* 2981 * Find TX rate 2982 */ 2983 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 2984 if (type != IEEE80211_FC0_TYPE_DATA || (m->m_flags & M_EAPOL)) { 2985 rate = rate_fb = tp->mgmtrate; 2986 } else if (ismcast) { 2987 rate = rate_fb = tp->mcastrate; 2988 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) { 2989 rate = rate_fb = tp->ucastrate; 2990 } else { 2991 rix = ieee80211_ratectl_rate(ni, NULL, pkt_len); 2992 rate = ni->ni_txrate; 2993 2994 if (rix > 0) { 2995 rate_fb = ni->ni_rates.rs_rates[rix-1] & 2996 IEEE80211_RATE_VAL; 2997 } else { 2998 rate_fb = rate; 2999 } 3000 } 3001 tb->tb_rate[0] = rate; 3002 tb->tb_rate[1] = rate_fb; 3003 sc->sc_tx_rate = rate; 3004 3005 /* 3006 * TX radio tap 3007 */ 3008 if (ieee80211_radiotap_active_vap(vap)) { 3009 sc->sc_tx_th.wt_flags = 0; 3010 if (wh->i_fc[1] & IEEE80211_FC1_WEP) 3011 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP; 3012 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_DS && 3013 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 3014 rate != (1 * 2)) { 3015 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 3016 } 3017 sc->sc_tx_th.wt_rate = rate; 3018 3019 ieee80211_radiotap_tx(vap, m); 3020 } 3021 3022 /* 3023 * Setup the embedded TX header 3024 */ 3025 M_PREPEND(m, sizeof(*hdr), M_DONTWAIT); 3026 if (m == NULL) { 3027 if_printf(ifp, "%s: prepend TX header failed\n", __func__); 3028 return ENOBUFS; 3029 } 3030 hdr = mtod(m, struct bwi_txbuf_hdr *); 3031 3032 bzero(hdr, sizeof(*hdr)); 3033 3034 bcopy(wh->i_fc, hdr->txh_fc, sizeof(hdr->txh_fc)); 3035 bcopy(wh->i_addr1, hdr->txh_addr1, sizeof(hdr->txh_addr1)); 3036 3037 if (!ismcast) { 3038 uint16_t dur; 3039 3040 dur = ieee80211_ack_duration(sc->sc_rates, rate, 3041 ic->ic_flags & ~IEEE80211_F_SHPREAMBLE); 3042 3043 hdr->txh_fb_duration = htole16(dur); 3044 } 3045 3046 hdr->txh_id = __SHIFTIN(BWI_TX_DATA_RING, BWI_TXH_ID_RING_MASK) | 3047 __SHIFTIN(idx, BWI_TXH_ID_IDX_MASK); 3048 3049 bwi_plcp_header(sc->sc_rates, hdr->txh_plcp, pkt_len, rate); 3050 bwi_plcp_header(sc->sc_rates, hdr->txh_fb_plcp, pkt_len, rate_fb); 3051 3052 phy_ctrl = __SHIFTIN(mac->mac_rf.rf_ant_mode, 3053 BWI_TXH_PHY_C_ANTMODE_MASK); 3054 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM) 3055 phy_ctrl |= BWI_TXH_PHY_C_OFDM; 3056 else if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && rate != (2 * 1)) 3057 phy_ctrl |= BWI_TXH_PHY_C_SHPREAMBLE; 3058 3059 mac_ctrl = BWI_TXH_MAC_C_HWSEQ | BWI_TXH_MAC_C_FIRST_FRAG; 3060 if (!ismcast) 3061 mac_ctrl |= BWI_TXH_MAC_C_ACK; 3062 if (ieee80211_rate2phytype(sc->sc_rates, rate_fb) == IEEE80211_T_OFDM) 3063 mac_ctrl |= BWI_TXH_MAC_C_FB_OFDM; 3064 3065 hdr->txh_mac_ctrl = htole32(mac_ctrl); 3066 hdr->txh_phy_ctrl = htole16(phy_ctrl); 3067 3068 /* Catch any further usage */ 3069 hdr = NULL; 3070 wh = NULL; 3071 3072 /* DMA load */ 3073 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m, 3074 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT); 3075 if (error && error != EFBIG) { 3076 if_printf(ifp, "%s: can't load TX buffer (1) %d\n", 3077 __func__, error); 3078 goto back; 3079 } 3080 3081 if (error) { /* error == EFBIG */ 3082 struct mbuf *m_new; 3083 3084 m_new = m_defrag(m, M_DONTWAIT); 3085 if (m_new == NULL) { 3086 if_printf(ifp, "%s: can't defrag TX buffer\n", 3087 __func__); 3088 error = ENOBUFS; 3089 goto back; 3090 } else { 3091 m = m_new; 3092 } 3093 3094 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m, 3095 bwi_dma_buf_addr, &paddr, 3096 BUS_DMA_NOWAIT); 3097 if (error) { 3098 if_printf(ifp, "%s: can't load TX buffer (2) %d\n", 3099 __func__, error); 3100 goto back; 3101 } 3102 } 3103 error = 0; 3104 3105 bus_dmamap_sync(sc->sc_buf_dtag, tb->tb_dmap, BUS_DMASYNC_PREWRITE); 3106 3107 tb->tb_mbuf = m; 3108 tb->tb_ni = ni; 3109 3110 #if 0 3111 p = mtod(m, const uint8_t *); 3112 for (i = 0; i < m->m_pkthdr.len; ++i) { 3113 if (i != 0 && i % 8 == 0) 3114 printf("\n"); 3115 printf("%02x ", p[i]); 3116 } 3117 printf("\n"); 3118 #endif 3119 DPRINTF(sc, BWI_DBG_TX, "idx %d, pkt_len %d, buflen %d\n", 3120 idx, pkt_len, m->m_pkthdr.len); 3121 3122 /* Setup TX descriptor */ 3123 sc->sc_setup_txdesc(sc, rd, idx, paddr, m->m_pkthdr.len); 3124 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap, 3125 BUS_DMASYNC_PREWRITE); 3126 3127 /* Kick start */ 3128 sc->sc_start_tx(sc, rd->rdata_txrx_ctrl, idx); 3129 3130 back: 3131 if (error) 3132 m_freem(m); 3133 return error; 3134 } 3135 3136 static int 3137 bwi_encap_raw(struct bwi_softc *sc, int idx, struct mbuf *m, 3138 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params) 3139 { 3140 struct ifnet *ifp = sc->sc_ifp; 3141 struct ieee80211vap *vap = ni->ni_vap; 3142 struct ieee80211com *ic = ni->ni_ic; 3143 struct bwi_ring_data *rd = &sc->sc_tx_rdata[BWI_TX_DATA_RING]; 3144 struct bwi_txbuf_data *tbd = &sc->sc_tx_bdata[BWI_TX_DATA_RING]; 3145 struct bwi_txbuf *tb = &tbd->tbd_buf[idx]; 3146 struct bwi_mac *mac; 3147 struct bwi_txbuf_hdr *hdr; 3148 struct ieee80211_frame *wh; 3149 uint8_t rate, rate_fb; 3150 uint32_t mac_ctrl; 3151 uint16_t phy_ctrl; 3152 bus_addr_t paddr; 3153 int ismcast, pkt_len, error; 3154 3155 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC, 3156 ("current regwin type %d", sc->sc_cur_regwin->rw_type)); 3157 mac = (struct bwi_mac *)sc->sc_cur_regwin; 3158 3159 wh = mtod(m, struct ieee80211_frame *); 3160 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 3161 3162 /* Get 802.11 frame len before prepending TX header */ 3163 pkt_len = m->m_pkthdr.len + IEEE80211_CRC_LEN; 3164 3165 /* 3166 * Find TX rate 3167 */ 3168 rate = params->ibp_rate0; 3169 if (!ieee80211_isratevalid(ic->ic_rt, rate)) { 3170 /* XXX fall back to mcast/mgmt rate? */ 3171 m_freem(m); 3172 return EINVAL; 3173 } 3174 if (params->ibp_try1 != 0) { 3175 rate_fb = params->ibp_rate1; 3176 if (!ieee80211_isratevalid(ic->ic_rt, rate_fb)) { 3177 /* XXX fall back to rate0? */ 3178 m_freem(m); 3179 return EINVAL; 3180 } 3181 } else 3182 rate_fb = rate; 3183 tb->tb_rate[0] = rate; 3184 tb->tb_rate[1] = rate_fb; 3185 sc->sc_tx_rate = rate; 3186 3187 /* 3188 * TX radio tap 3189 */ 3190 if (ieee80211_radiotap_active_vap(vap)) { 3191 sc->sc_tx_th.wt_flags = 0; 3192 /* XXX IEEE80211_BPF_CRYPTO */ 3193 if (wh->i_fc[1] & IEEE80211_FC1_WEP) 3194 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP; 3195 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE) 3196 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 3197 sc->sc_tx_th.wt_rate = rate; 3198 3199 ieee80211_radiotap_tx(vap, m); 3200 } 3201 3202 /* 3203 * Setup the embedded TX header 3204 */ 3205 M_PREPEND(m, sizeof(*hdr), M_DONTWAIT); 3206 if (m == NULL) { 3207 if_printf(ifp, "%s: prepend TX header failed\n", __func__); 3208 return ENOBUFS; 3209 } 3210 hdr = mtod(m, struct bwi_txbuf_hdr *); 3211 3212 bzero(hdr, sizeof(*hdr)); 3213 3214 bcopy(wh->i_fc, hdr->txh_fc, sizeof(hdr->txh_fc)); 3215 bcopy(wh->i_addr1, hdr->txh_addr1, sizeof(hdr->txh_addr1)); 3216 3217 mac_ctrl = BWI_TXH_MAC_C_HWSEQ | BWI_TXH_MAC_C_FIRST_FRAG; 3218 if (!ismcast && (params->ibp_flags & IEEE80211_BPF_NOACK) == 0) { 3219 uint16_t dur; 3220 3221 dur = ieee80211_ack_duration(sc->sc_rates, rate_fb, 0); 3222 3223 hdr->txh_fb_duration = htole16(dur); 3224 mac_ctrl |= BWI_TXH_MAC_C_ACK; 3225 } 3226 3227 hdr->txh_id = __SHIFTIN(BWI_TX_DATA_RING, BWI_TXH_ID_RING_MASK) | 3228 __SHIFTIN(idx, BWI_TXH_ID_IDX_MASK); 3229 3230 bwi_plcp_header(sc->sc_rates, hdr->txh_plcp, pkt_len, rate); 3231 bwi_plcp_header(sc->sc_rates, hdr->txh_fb_plcp, pkt_len, rate_fb); 3232 3233 phy_ctrl = __SHIFTIN(mac->mac_rf.rf_ant_mode, 3234 BWI_TXH_PHY_C_ANTMODE_MASK); 3235 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM) { 3236 phy_ctrl |= BWI_TXH_PHY_C_OFDM; 3237 mac_ctrl |= BWI_TXH_MAC_C_FB_OFDM; 3238 } else if (params->ibp_flags & IEEE80211_BPF_SHORTPRE) 3239 phy_ctrl |= BWI_TXH_PHY_C_SHPREAMBLE; 3240 3241 hdr->txh_mac_ctrl = htole32(mac_ctrl); 3242 hdr->txh_phy_ctrl = htole16(phy_ctrl); 3243 3244 /* Catch any further usage */ 3245 hdr = NULL; 3246 wh = NULL; 3247 3248 /* DMA load */ 3249 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m, 3250 bwi_dma_buf_addr, &paddr, BUS_DMA_NOWAIT); 3251 if (error != 0) { 3252 struct mbuf *m_new; 3253 3254 if (error != EFBIG) { 3255 if_printf(ifp, "%s: can't load TX buffer (1) %d\n", 3256 __func__, error); 3257 goto back; 3258 } 3259 m_new = m_defrag(m, M_DONTWAIT); 3260 if (m_new == NULL) { 3261 if_printf(ifp, "%s: can't defrag TX buffer\n", 3262 __func__); 3263 error = ENOBUFS; 3264 goto back; 3265 } 3266 m = m_new; 3267 error = bus_dmamap_load_mbuf(sc->sc_buf_dtag, tb->tb_dmap, m, 3268 bwi_dma_buf_addr, &paddr, 3269 BUS_DMA_NOWAIT); 3270 if (error) { 3271 if_printf(ifp, "%s: can't load TX buffer (2) %d\n", 3272 __func__, error); 3273 goto back; 3274 } 3275 } 3276 3277 bus_dmamap_sync(sc->sc_buf_dtag, tb->tb_dmap, BUS_DMASYNC_PREWRITE); 3278 3279 tb->tb_mbuf = m; 3280 tb->tb_ni = ni; 3281 3282 DPRINTF(sc, BWI_DBG_TX, "idx %d, pkt_len %d, buflen %d\n", 3283 idx, pkt_len, m->m_pkthdr.len); 3284 3285 /* Setup TX descriptor */ 3286 sc->sc_setup_txdesc(sc, rd, idx, paddr, m->m_pkthdr.len); 3287 bus_dmamap_sync(sc->sc_txring_dtag, rd->rdata_dmap, 3288 BUS_DMASYNC_PREWRITE); 3289 3290 /* Kick start */ 3291 sc->sc_start_tx(sc, rd->rdata_txrx_ctrl, idx); 3292 back: 3293 if (error) 3294 m_freem(m); 3295 return error; 3296 } 3297 3298 static void 3299 bwi_start_tx32(struct bwi_softc *sc, uint32_t tx_ctrl, int idx) 3300 { 3301 idx = (idx + 1) % BWI_TX_NDESC; 3302 CSR_WRITE_4(sc, tx_ctrl + BWI_TX32_INDEX, 3303 idx * sizeof(struct bwi_desc32)); 3304 } 3305 3306 static void 3307 bwi_start_tx64(struct bwi_softc *sc, uint32_t tx_ctrl, int idx) 3308 { 3309 /* TODO:64 */ 3310 } 3311 3312 static void 3313 bwi_txeof_status32(struct bwi_softc *sc) 3314 { 3315 struct ifnet *ifp = sc->sc_ifp; 3316 uint32_t val, ctrl_base; 3317 int end_idx; 3318 3319 ctrl_base = sc->sc_txstats->stats_ctrl_base; 3320 3321 val = CSR_READ_4(sc, ctrl_base + BWI_RX32_STATUS); 3322 end_idx = __SHIFTOUT(val, BWI_RX32_STATUS_INDEX_MASK) / 3323 sizeof(struct bwi_desc32); 3324 3325 bwi_txeof_status(sc, end_idx); 3326 3327 CSR_WRITE_4(sc, ctrl_base + BWI_RX32_INDEX, 3328 end_idx * sizeof(struct bwi_desc32)); 3329 3330 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) 3331 ifp->if_start(ifp); 3332 } 3333 3334 static void 3335 bwi_txeof_status64(struct bwi_softc *sc) 3336 { 3337 /* TODO:64 */ 3338 } 3339 3340 static void 3341 _bwi_txeof(struct bwi_softc *sc, uint16_t tx_id, int acked, int data_txcnt) 3342 { 3343 struct ifnet *ifp = sc->sc_ifp; 3344 struct bwi_txbuf_data *tbd; 3345 struct bwi_txbuf *tb; 3346 int ring_idx, buf_idx; 3347 struct ieee80211_node *ni; 3348 struct ieee80211vap *vap; 3349 3350 if (tx_id == 0) { 3351 if_printf(ifp, "%s: zero tx id\n", __func__); 3352 return; 3353 } 3354 3355 ring_idx = __SHIFTOUT(tx_id, BWI_TXH_ID_RING_MASK); 3356 buf_idx = __SHIFTOUT(tx_id, BWI_TXH_ID_IDX_MASK); 3357 3358 KASSERT(ring_idx == BWI_TX_DATA_RING, ("ring_idx %d", ring_idx)); 3359 KASSERT(buf_idx < BWI_TX_NDESC, ("buf_idx %d", buf_idx)); 3360 3361 tbd = &sc->sc_tx_bdata[ring_idx]; 3362 KASSERT(tbd->tbd_used > 0, ("tbd_used %d", tbd->tbd_used)); 3363 tbd->tbd_used--; 3364 3365 tb = &tbd->tbd_buf[buf_idx]; 3366 DPRINTF(sc, BWI_DBG_TXEOF, "txeof idx %d, " 3367 "acked %d, data_txcnt %d, ni %p\n", 3368 buf_idx, acked, data_txcnt, tb->tb_ni); 3369 3370 bus_dmamap_unload(sc->sc_buf_dtag, tb->tb_dmap); 3371 3372 ni = tb->tb_ni; 3373 if (tb->tb_ni != NULL) { 3374 const struct bwi_txbuf_hdr *hdr = 3375 mtod(tb->tb_mbuf, const struct bwi_txbuf_hdr *); 3376 vap = ni->ni_vap; 3377 3378 /* NB: update rate control only for unicast frames */ 3379 if (hdr->txh_mac_ctrl & htole32(BWI_TXH_MAC_C_ACK)) { 3380 /* 3381 * Feed back 'acked and data_txcnt'. Note that the 3382 * generic AMRR code only understands one tx rate 3383 * and the estimator doesn't handle real retry counts 3384 * well so to avoid over-aggressive downshifting we 3385 * treat any number of retries as "1". 3386 */ 3387 ieee80211_ratectl_tx_complete(vap, ni, 3388 (data_txcnt > 1) ? IEEE80211_RATECTL_TX_SUCCESS : 3389 IEEE80211_RATECTL_TX_FAILURE, &acked, NULL); 3390 } 3391 3392 /* 3393 * Do any tx complete callback. Note this must 3394 * be done before releasing the node reference. 3395 */ 3396 if (tb->tb_mbuf->m_flags & M_TXCB) 3397 ieee80211_process_callback(ni, tb->tb_mbuf, !acked); 3398 3399 ieee80211_free_node(tb->tb_ni); 3400 tb->tb_ni = NULL; 3401 } 3402 m_freem(tb->tb_mbuf); 3403 tb->tb_mbuf = NULL; 3404 3405 if (tbd->tbd_used == 0) 3406 sc->sc_tx_timer = 0; 3407 3408 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 3409 } 3410 3411 static void 3412 bwi_txeof_status(struct bwi_softc *sc, int end_idx) 3413 { 3414 struct bwi_txstats_data *st = sc->sc_txstats; 3415 int idx; 3416 3417 bus_dmamap_sync(st->stats_dtag, st->stats_dmap, BUS_DMASYNC_POSTREAD); 3418 3419 idx = st->stats_idx; 3420 while (idx != end_idx) { 3421 const struct bwi_txstats *stats = &st->stats[idx]; 3422 3423 if ((stats->txs_flags & BWI_TXS_F_PENDING) == 0) { 3424 int data_txcnt; 3425 3426 data_txcnt = __SHIFTOUT(stats->txs_txcnt, 3427 BWI_TXS_TXCNT_DATA); 3428 _bwi_txeof(sc, le16toh(stats->txs_id), 3429 stats->txs_flags & BWI_TXS_F_ACKED, 3430 data_txcnt); 3431 } 3432 idx = (idx + 1) % BWI_TXSTATS_NDESC; 3433 } 3434 st->stats_idx = idx; 3435 } 3436 3437 static void 3438 bwi_txeof(struct bwi_softc *sc) 3439 { 3440 struct ifnet *ifp = sc->sc_ifp; 3441 3442 for (;;) { 3443 uint32_t tx_status0, tx_status1; 3444 uint16_t tx_id; 3445 int data_txcnt; 3446 3447 tx_status0 = CSR_READ_4(sc, BWI_TXSTATUS0); 3448 if ((tx_status0 & BWI_TXSTATUS0_VALID) == 0) 3449 break; 3450 tx_status1 = CSR_READ_4(sc, BWI_TXSTATUS1); 3451 3452 tx_id = __SHIFTOUT(tx_status0, BWI_TXSTATUS0_TXID_MASK); 3453 data_txcnt = __SHIFTOUT(tx_status0, 3454 BWI_TXSTATUS0_DATA_TXCNT_MASK); 3455 3456 if (tx_status0 & (BWI_TXSTATUS0_AMPDU | BWI_TXSTATUS0_PENDING)) 3457 continue; 3458 3459 _bwi_txeof(sc, le16toh(tx_id), tx_status0 & BWI_TXSTATUS0_ACKED, 3460 data_txcnt); 3461 } 3462 3463 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) 3464 ifp->if_start(ifp); 3465 } 3466 3467 static int 3468 bwi_bbp_power_on(struct bwi_softc *sc, enum bwi_clock_mode clk_mode) 3469 { 3470 bwi_power_on(sc, 1); 3471 return bwi_set_clock_mode(sc, clk_mode); 3472 } 3473 3474 static void 3475 bwi_bbp_power_off(struct bwi_softc *sc) 3476 { 3477 bwi_set_clock_mode(sc, BWI_CLOCK_MODE_SLOW); 3478 bwi_power_off(sc, 1); 3479 } 3480 3481 static int 3482 bwi_get_pwron_delay(struct bwi_softc *sc) 3483 { 3484 struct bwi_regwin *com, *old; 3485 struct bwi_clock_freq freq; 3486 uint32_t val; 3487 int error; 3488 3489 com = &sc->sc_com_regwin; 3490 KASSERT(BWI_REGWIN_EXIST(com), ("no regwin")); 3491 3492 if ((sc->sc_cap & BWI_CAP_CLKMODE) == 0) 3493 return 0; 3494 3495 error = bwi_regwin_switch(sc, com, &old); 3496 if (error) 3497 return error; 3498 3499 bwi_get_clock_freq(sc, &freq); 3500 3501 val = CSR_READ_4(sc, BWI_PLL_ON_DELAY); 3502 sc->sc_pwron_delay = howmany((val + 2) * 1000000, freq.clkfreq_min); 3503 DPRINTF(sc, BWI_DBG_ATTACH, "power on delay %u\n", sc->sc_pwron_delay); 3504 3505 return bwi_regwin_switch(sc, old, NULL); 3506 } 3507 3508 static int 3509 bwi_bus_attach(struct bwi_softc *sc) 3510 { 3511 struct bwi_regwin *bus, *old; 3512 int error; 3513 3514 bus = &sc->sc_bus_regwin; 3515 3516 error = bwi_regwin_switch(sc, bus, &old); 3517 if (error) 3518 return error; 3519 3520 if (!bwi_regwin_is_enabled(sc, bus)) 3521 bwi_regwin_enable(sc, bus, 0); 3522 3523 /* Disable interripts */ 3524 CSR_WRITE_4(sc, BWI_INTRVEC, 0); 3525 3526 return bwi_regwin_switch(sc, old, NULL); 3527 } 3528 3529 static const char * 3530 bwi_regwin_name(const struct bwi_regwin *rw) 3531 { 3532 switch (rw->rw_type) { 3533 case BWI_REGWIN_T_COM: 3534 return "COM"; 3535 case BWI_REGWIN_T_BUSPCI: 3536 return "PCI"; 3537 case BWI_REGWIN_T_MAC: 3538 return "MAC"; 3539 case BWI_REGWIN_T_BUSPCIE: 3540 return "PCIE"; 3541 } 3542 panic("unknown regwin type 0x%04x\n", rw->rw_type); 3543 return NULL; 3544 } 3545 3546 static uint32_t 3547 bwi_regwin_disable_bits(struct bwi_softc *sc) 3548 { 3549 uint32_t busrev; 3550 3551 /* XXX cache this */ 3552 busrev = __SHIFTOUT(CSR_READ_4(sc, BWI_ID_LO), BWI_ID_LO_BUSREV_MASK); 3553 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT | BWI_DBG_MISC, 3554 "bus rev %u\n", busrev); 3555 3556 if (busrev == BWI_BUSREV_0) 3557 return BWI_STATE_LO_DISABLE1; 3558 else if (busrev == BWI_BUSREV_1) 3559 return BWI_STATE_LO_DISABLE2; 3560 else 3561 return (BWI_STATE_LO_DISABLE1 | BWI_STATE_LO_DISABLE2); 3562 } 3563 3564 int 3565 bwi_regwin_is_enabled(struct bwi_softc *sc, struct bwi_regwin *rw) 3566 { 3567 uint32_t val, disable_bits; 3568 3569 disable_bits = bwi_regwin_disable_bits(sc); 3570 val = CSR_READ_4(sc, BWI_STATE_LO); 3571 3572 if ((val & (BWI_STATE_LO_CLOCK | 3573 BWI_STATE_LO_RESET | 3574 disable_bits)) == BWI_STATE_LO_CLOCK) { 3575 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT, "%s is enabled\n", 3576 bwi_regwin_name(rw)); 3577 return 1; 3578 } else { 3579 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT, "%s is disabled\n", 3580 bwi_regwin_name(rw)); 3581 return 0; 3582 } 3583 } 3584 3585 void 3586 bwi_regwin_disable(struct bwi_softc *sc, struct bwi_regwin *rw, uint32_t flags) 3587 { 3588 uint32_t state_lo, disable_bits; 3589 int i; 3590 3591 state_lo = CSR_READ_4(sc, BWI_STATE_LO); 3592 3593 /* 3594 * If current regwin is in 'reset' state, it was already disabled. 3595 */ 3596 if (state_lo & BWI_STATE_LO_RESET) { 3597 DPRINTF(sc, BWI_DBG_ATTACH | BWI_DBG_INIT, 3598 "%s was already disabled\n", bwi_regwin_name(rw)); 3599 return; 3600 } 3601 3602 disable_bits = bwi_regwin_disable_bits(sc); 3603 3604 /* 3605 * Disable normal clock 3606 */ 3607 state_lo = BWI_STATE_LO_CLOCK | disable_bits; 3608 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo); 3609 3610 /* 3611 * Wait until normal clock is disabled 3612 */ 3613 #define NRETRY 1000 3614 for (i = 0; i < NRETRY; ++i) { 3615 state_lo = CSR_READ_4(sc, BWI_STATE_LO); 3616 if (state_lo & disable_bits) 3617 break; 3618 DELAY(10); 3619 } 3620 if (i == NRETRY) { 3621 device_printf(sc->sc_dev, "%s disable clock timeout\n", 3622 bwi_regwin_name(rw)); 3623 } 3624 3625 for (i = 0; i < NRETRY; ++i) { 3626 uint32_t state_hi; 3627 3628 state_hi = CSR_READ_4(sc, BWI_STATE_HI); 3629 if ((state_hi & BWI_STATE_HI_BUSY) == 0) 3630 break; 3631 DELAY(10); 3632 } 3633 if (i == NRETRY) { 3634 device_printf(sc->sc_dev, "%s wait BUSY unset timeout\n", 3635 bwi_regwin_name(rw)); 3636 } 3637 #undef NRETRY 3638 3639 /* 3640 * Reset and disable regwin with gated clock 3641 */ 3642 state_lo = BWI_STATE_LO_RESET | disable_bits | 3643 BWI_STATE_LO_CLOCK | BWI_STATE_LO_GATED_CLOCK | 3644 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK); 3645 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo); 3646 3647 /* Flush pending bus write */ 3648 CSR_READ_4(sc, BWI_STATE_LO); 3649 DELAY(1); 3650 3651 /* Reset and disable regwin */ 3652 state_lo = BWI_STATE_LO_RESET | disable_bits | 3653 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK); 3654 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo); 3655 3656 /* Flush pending bus write */ 3657 CSR_READ_4(sc, BWI_STATE_LO); 3658 DELAY(1); 3659 } 3660 3661 void 3662 bwi_regwin_enable(struct bwi_softc *sc, struct bwi_regwin *rw, uint32_t flags) 3663 { 3664 uint32_t state_lo, state_hi, imstate; 3665 3666 bwi_regwin_disable(sc, rw, flags); 3667 3668 /* Reset regwin with gated clock */ 3669 state_lo = BWI_STATE_LO_RESET | 3670 BWI_STATE_LO_CLOCK | 3671 BWI_STATE_LO_GATED_CLOCK | 3672 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK); 3673 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo); 3674 3675 /* Flush pending bus write */ 3676 CSR_READ_4(sc, BWI_STATE_LO); 3677 DELAY(1); 3678 3679 state_hi = CSR_READ_4(sc, BWI_STATE_HI); 3680 if (state_hi & BWI_STATE_HI_SERROR) 3681 CSR_WRITE_4(sc, BWI_STATE_HI, 0); 3682 3683 imstate = CSR_READ_4(sc, BWI_IMSTATE); 3684 if (imstate & (BWI_IMSTATE_INBAND_ERR | BWI_IMSTATE_TIMEOUT)) { 3685 imstate &= ~(BWI_IMSTATE_INBAND_ERR | BWI_IMSTATE_TIMEOUT); 3686 CSR_WRITE_4(sc, BWI_IMSTATE, imstate); 3687 } 3688 3689 /* Enable regwin with gated clock */ 3690 state_lo = BWI_STATE_LO_CLOCK | 3691 BWI_STATE_LO_GATED_CLOCK | 3692 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK); 3693 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo); 3694 3695 /* Flush pending bus write */ 3696 CSR_READ_4(sc, BWI_STATE_LO); 3697 DELAY(1); 3698 3699 /* Enable regwin with normal clock */ 3700 state_lo = BWI_STATE_LO_CLOCK | 3701 __SHIFTIN(flags, BWI_STATE_LO_FLAGS_MASK); 3702 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo); 3703 3704 /* Flush pending bus write */ 3705 CSR_READ_4(sc, BWI_STATE_LO); 3706 DELAY(1); 3707 } 3708 3709 static void 3710 bwi_set_bssid(struct bwi_softc *sc, const uint8_t *bssid) 3711 { 3712 struct ifnet *ifp = sc->sc_ifp; 3713 struct bwi_mac *mac; 3714 struct bwi_myaddr_bssid buf; 3715 const uint8_t *p; 3716 uint32_t val; 3717 int n, i; 3718 3719 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC, 3720 ("current regwin type %d", sc->sc_cur_regwin->rw_type)); 3721 mac = (struct bwi_mac *)sc->sc_cur_regwin; 3722 3723 bwi_set_addr_filter(sc, BWI_ADDR_FILTER_BSSID, bssid); 3724 3725 bcopy(IF_LLADDR(ifp), buf.myaddr, sizeof(buf.myaddr)); 3726 bcopy(bssid, buf.bssid, sizeof(buf.bssid)); 3727 3728 n = sizeof(buf) / sizeof(val); 3729 p = (const uint8_t *)&buf; 3730 for (i = 0; i < n; ++i) { 3731 int j; 3732 3733 val = 0; 3734 for (j = 0; j < sizeof(val); ++j) 3735 val |= ((uint32_t)(*p++)) << (j * 8); 3736 3737 TMPLT_WRITE_4(mac, 0x20 + (i * sizeof(val)), val); 3738 } 3739 } 3740 3741 static void 3742 bwi_updateslot(struct ifnet *ifp) 3743 { 3744 struct bwi_softc *sc = ifp->if_softc; 3745 struct ieee80211com *ic = ifp->if_l2com; 3746 struct bwi_mac *mac; 3747 3748 BWI_LOCK(sc); 3749 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 3750 DPRINTF(sc, BWI_DBG_80211, "%s\n", __func__); 3751 3752 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC, 3753 ("current regwin type %d", sc->sc_cur_regwin->rw_type)); 3754 mac = (struct bwi_mac *)sc->sc_cur_regwin; 3755 3756 bwi_mac_updateslot(mac, (ic->ic_flags & IEEE80211_F_SHSLOT)); 3757 } 3758 BWI_UNLOCK(sc); 3759 } 3760 3761 static void 3762 bwi_calibrate(void *xsc) 3763 { 3764 struct bwi_softc *sc = xsc; 3765 #ifdef INVARIANTS 3766 struct ifnet *ifp = sc->sc_ifp; 3767 struct ieee80211com *ic = ifp->if_l2com; 3768 #endif 3769 struct bwi_mac *mac; 3770 3771 BWI_ASSERT_LOCKED(sc); 3772 3773 KASSERT(ic->ic_opmode != IEEE80211_M_MONITOR, 3774 ("opmode %d", ic->ic_opmode)); 3775 3776 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC, 3777 ("current regwin type %d", sc->sc_cur_regwin->rw_type)); 3778 mac = (struct bwi_mac *)sc->sc_cur_regwin; 3779 3780 bwi_mac_calibrate_txpower(mac, sc->sc_txpwrcb_type); 3781 sc->sc_txpwrcb_type = BWI_TXPWR_CALIB; 3782 3783 /* XXX 15 seconds */ 3784 callout_reset(&sc->sc_calib_ch, hz * 15, bwi_calibrate, sc); 3785 } 3786 3787 static int 3788 bwi_calc_rssi(struct bwi_softc *sc, const struct bwi_rxbuf_hdr *hdr) 3789 { 3790 struct bwi_mac *mac; 3791 3792 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC, 3793 ("current regwin type %d", sc->sc_cur_regwin->rw_type)); 3794 mac = (struct bwi_mac *)sc->sc_cur_regwin; 3795 3796 return bwi_rf_calc_rssi(mac, hdr); 3797 } 3798 3799 static int 3800 bwi_calc_noise(struct bwi_softc *sc) 3801 { 3802 struct bwi_mac *mac; 3803 3804 KASSERT(sc->sc_cur_regwin->rw_type == BWI_REGWIN_T_MAC, 3805 ("current regwin type %d", sc->sc_cur_regwin->rw_type)); 3806 mac = (struct bwi_mac *)sc->sc_cur_regwin; 3807 3808 return bwi_rf_calc_noise(mac); 3809 } 3810 3811 static __inline uint8_t 3812 bwi_ofdm_plcp2rate(const uint32_t *plcp0) 3813 { 3814 uint32_t plcp; 3815 uint8_t plcp_rate; 3816 3817 plcp = le32toh(*plcp0); 3818 plcp_rate = __SHIFTOUT(plcp, IEEE80211_OFDM_PLCP_RATE_MASK); 3819 return ieee80211_plcp2rate(plcp_rate, IEEE80211_T_OFDM); 3820 } 3821 3822 static __inline uint8_t 3823 bwi_ds_plcp2rate(const struct ieee80211_ds_plcp_hdr *hdr) 3824 { 3825 return ieee80211_plcp2rate(hdr->i_signal, IEEE80211_T_DS); 3826 } 3827 3828 static void 3829 bwi_rx_radiotap(struct bwi_softc *sc, struct mbuf *m, 3830 struct bwi_rxbuf_hdr *hdr, const void *plcp, int rate, int rssi, int noise) 3831 { 3832 const struct ieee80211_frame_min *wh; 3833 3834 sc->sc_rx_th.wr_flags = IEEE80211_RADIOTAP_F_FCS; 3835 if (htole16(hdr->rxh_flags1) & BWI_RXH_F1_SHPREAMBLE) 3836 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 3837 3838 wh = mtod(m, const struct ieee80211_frame_min *); 3839 if (wh->i_fc[1] & IEEE80211_FC1_WEP) 3840 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_WEP; 3841 3842 sc->sc_rx_th.wr_tsf = hdr->rxh_tsf; /* No endian convertion */ 3843 sc->sc_rx_th.wr_rate = rate; 3844 sc->sc_rx_th.wr_antsignal = rssi; 3845 sc->sc_rx_th.wr_antnoise = noise; 3846 } 3847 3848 static void 3849 bwi_led_attach(struct bwi_softc *sc) 3850 { 3851 const uint8_t *led_act = NULL; 3852 uint16_t gpio, val[BWI_LED_MAX]; 3853 int i; 3854 3855 #define N(arr) (int)(sizeof(arr) / sizeof(arr[0])) 3856 3857 for (i = 0; i < N(bwi_vendor_led_act); ++i) { 3858 if (sc->sc_pci_subvid == bwi_vendor_led_act[i].vid) { 3859 led_act = bwi_vendor_led_act[i].led_act; 3860 break; 3861 } 3862 } 3863 if (led_act == NULL) 3864 led_act = bwi_default_led_act; 3865 3866 #undef N 3867 3868 gpio = bwi_read_sprom(sc, BWI_SPROM_GPIO01); 3869 val[0] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_0); 3870 val[1] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_1); 3871 3872 gpio = bwi_read_sprom(sc, BWI_SPROM_GPIO23); 3873 val[2] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_2); 3874 val[3] = __SHIFTOUT(gpio, BWI_SPROM_GPIO_3); 3875 3876 for (i = 0; i < BWI_LED_MAX; ++i) { 3877 struct bwi_led *led = &sc->sc_leds[i]; 3878 3879 if (val[i] == 0xff) { 3880 led->l_act = led_act[i]; 3881 } else { 3882 if (val[i] & BWI_LED_ACT_LOW) 3883 led->l_flags |= BWI_LED_F_ACTLOW; 3884 led->l_act = __SHIFTOUT(val[i], BWI_LED_ACT_MASK); 3885 } 3886 led->l_mask = (1 << i); 3887 3888 if (led->l_act == BWI_LED_ACT_BLINK_SLOW || 3889 led->l_act == BWI_LED_ACT_BLINK_POLL || 3890 led->l_act == BWI_LED_ACT_BLINK) { 3891 led->l_flags |= BWI_LED_F_BLINK; 3892 if (led->l_act == BWI_LED_ACT_BLINK_POLL) 3893 led->l_flags |= BWI_LED_F_POLLABLE; 3894 else if (led->l_act == BWI_LED_ACT_BLINK_SLOW) 3895 led->l_flags |= BWI_LED_F_SLOW; 3896 3897 if (sc->sc_blink_led == NULL) { 3898 sc->sc_blink_led = led; 3899 if (led->l_flags & BWI_LED_F_SLOW) 3900 BWI_LED_SLOWDOWN(sc->sc_led_idle); 3901 } 3902 } 3903 3904 DPRINTF(sc, BWI_DBG_LED | BWI_DBG_ATTACH, 3905 "%dth led, act %d, lowact %d\n", i, 3906 led->l_act, led->l_flags & BWI_LED_F_ACTLOW); 3907 } 3908 callout_init_mtx(&sc->sc_led_blink_ch, &sc->sc_mtx, 0); 3909 } 3910 3911 static __inline uint16_t 3912 bwi_led_onoff(const struct bwi_led *led, uint16_t val, int on) 3913 { 3914 if (led->l_flags & BWI_LED_F_ACTLOW) 3915 on = !on; 3916 if (on) 3917 val |= led->l_mask; 3918 else 3919 val &= ~led->l_mask; 3920 return val; 3921 } 3922 3923 static void 3924 bwi_led_newstate(struct bwi_softc *sc, enum ieee80211_state nstate) 3925 { 3926 struct ifnet *ifp = sc->sc_ifp; 3927 struct ieee80211com *ic = ifp->if_l2com; 3928 uint16_t val; 3929 int i; 3930 3931 if (nstate == IEEE80211_S_INIT) { 3932 callout_stop(&sc->sc_led_blink_ch); 3933 sc->sc_led_blinking = 0; 3934 } 3935 3936 if ((ic->ic_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 3937 return; 3938 3939 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL); 3940 for (i = 0; i < BWI_LED_MAX; ++i) { 3941 struct bwi_led *led = &sc->sc_leds[i]; 3942 int on; 3943 3944 if (led->l_act == BWI_LED_ACT_UNKN || 3945 led->l_act == BWI_LED_ACT_NULL) 3946 continue; 3947 3948 if ((led->l_flags & BWI_LED_F_BLINK) && 3949 nstate != IEEE80211_S_INIT) 3950 continue; 3951 3952 switch (led->l_act) { 3953 case BWI_LED_ACT_ON: /* Always on */ 3954 on = 1; 3955 break; 3956 case BWI_LED_ACT_OFF: /* Always off */ 3957 case BWI_LED_ACT_5GHZ: /* TODO: 11A */ 3958 on = 0; 3959 break; 3960 default: 3961 on = 1; 3962 switch (nstate) { 3963 case IEEE80211_S_INIT: 3964 on = 0; 3965 break; 3966 case IEEE80211_S_RUN: 3967 if (led->l_act == BWI_LED_ACT_11G && 3968 ic->ic_curmode != IEEE80211_MODE_11G) 3969 on = 0; 3970 break; 3971 default: 3972 if (led->l_act == BWI_LED_ACT_ASSOC) 3973 on = 0; 3974 break; 3975 } 3976 break; 3977 } 3978 3979 val = bwi_led_onoff(led, val, on); 3980 } 3981 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val); 3982 } 3983 static void 3984 bwi_led_event(struct bwi_softc *sc, int event) 3985 { 3986 struct bwi_led *led = sc->sc_blink_led; 3987 int rate; 3988 3989 if (event == BWI_LED_EVENT_POLL) { 3990 if ((led->l_flags & BWI_LED_F_POLLABLE) == 0) 3991 return; 3992 if (ticks - sc->sc_led_ticks < sc->sc_led_idle) 3993 return; 3994 } 3995 3996 sc->sc_led_ticks = ticks; 3997 if (sc->sc_led_blinking) 3998 return; 3999 4000 switch (event) { 4001 case BWI_LED_EVENT_RX: 4002 rate = sc->sc_rx_rate; 4003 break; 4004 case BWI_LED_EVENT_TX: 4005 rate = sc->sc_tx_rate; 4006 break; 4007 case BWI_LED_EVENT_POLL: 4008 rate = 0; 4009 break; 4010 default: 4011 panic("unknown LED event %d\n", event); 4012 break; 4013 } 4014 bwi_led_blink_start(sc, bwi_led_duration[rate].on_dur, 4015 bwi_led_duration[rate].off_dur); 4016 } 4017 4018 static void 4019 bwi_led_blink_start(struct bwi_softc *sc, int on_dur, int off_dur) 4020 { 4021 struct bwi_led *led = sc->sc_blink_led; 4022 uint16_t val; 4023 4024 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL); 4025 val = bwi_led_onoff(led, val, 1); 4026 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val); 4027 4028 if (led->l_flags & BWI_LED_F_SLOW) { 4029 BWI_LED_SLOWDOWN(on_dur); 4030 BWI_LED_SLOWDOWN(off_dur); 4031 } 4032 4033 sc->sc_led_blinking = 1; 4034 sc->sc_led_blink_offdur = off_dur; 4035 4036 callout_reset(&sc->sc_led_blink_ch, on_dur, bwi_led_blink_next, sc); 4037 } 4038 4039 static void 4040 bwi_led_blink_next(void *xsc) 4041 { 4042 struct bwi_softc *sc = xsc; 4043 uint16_t val; 4044 4045 val = CSR_READ_2(sc, BWI_MAC_GPIO_CTRL); 4046 val = bwi_led_onoff(sc->sc_blink_led, val, 0); 4047 CSR_WRITE_2(sc, BWI_MAC_GPIO_CTRL, val); 4048 4049 callout_reset(&sc->sc_led_blink_ch, sc->sc_led_blink_offdur, 4050 bwi_led_blink_end, sc); 4051 } 4052 4053 static void 4054 bwi_led_blink_end(void *xsc) 4055 { 4056 struct bwi_softc *sc = xsc; 4057 sc->sc_led_blinking = 0; 4058 } 4059 4060 static void 4061 bwi_restart(void *xsc, int pending) 4062 { 4063 struct bwi_softc *sc = xsc; 4064 struct ifnet *ifp = sc->sc_ifp; 4065 4066 if_printf(ifp, "%s begin, help!\n", __func__); 4067 BWI_LOCK(sc); 4068 bwi_init_statechg(xsc, 0); 4069 #if 0 4070 bwi_start_locked(ifp); 4071 #endif 4072 BWI_UNLOCK(sc); 4073 } 4074