if_wi.c revision 82258
1/* 2 * Copyright (c) 1997, 1998, 1999 3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33/* 34 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for FreeBSD. 35 * 36 * Written by Bill Paul <wpaul@ctr.columbia.edu> 37 * Electrical Engineering Department 38 * Columbia University, New York City 39 */ 40 41/* 42 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN 43 * from Lucent. Unlike the older cards, the new ones are programmed 44 * entirely via a firmware-driven controller called the Hermes. 45 * Unfortunately, Lucent will not release the Hermes programming manual 46 * without an NDA (if at all). What they do release is an API library 47 * called the HCF (Hardware Control Functions) which is supposed to 48 * do the device-specific operations of a device driver for you. The 49 * publically available version of the HCF library (the 'HCF Light') is 50 * a) extremely gross, b) lacks certain features, particularly support 51 * for 802.11 frames, and c) is contaminated by the GNU Public License. 52 * 53 * This driver does not use the HCF or HCF Light at all. Instead, it 54 * programs the Hermes controller directly, using information gleaned 55 * from the HCF Light code and corresponding documentation. 56 * 57 * This driver supports both the PCMCIA and ISA versions of the 58 * WaveLAN/IEEE cards. Note however that the ISA card isn't really 59 * anything of the sort: it's actually a PCMCIA bridge adapter 60 * that fits into an ISA slot, into which a PCMCIA WaveLAN card is 61 * inserted. Consequently, you need to use the pccard support for 62 * both the ISA and PCMCIA adapters. 63 */ 64 65#define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */ 66#define WI_HERMES_STATS_WAR /* Work around stats counter bug. */ 67#define WICACHE /* turn on signal strength cache code */ 68 69#include <sys/param.h> 70#include <sys/systm.h> 71#include <sys/sockio.h> 72#include <sys/mbuf.h> 73#include <sys/kernel.h> 74#include <sys/socket.h> 75#include <sys/module.h> 76#include <sys/bus.h> 77#include <sys/syslog.h> 78#include <sys/sysctl.h> 79 80#include <machine/bus.h> 81#include <machine/resource.h> 82#include <machine/md_var.h> 83#include <machine/bus_pio.h> 84#include <sys/rman.h> 85 86#include <pci/pcireg.h> 87#include <pci/pcivar.h> 88 89#include <net/if.h> 90#include <net/if_arp.h> 91#include <net/ethernet.h> 92#include <net/if_dl.h> 93#include <net/if_media.h> 94#include <net/if_types.h> 95#include <net/if_ieee80211.h> 96 97#include <netinet/in.h> 98#include <netinet/in_systm.h> 99#include <netinet/in_var.h> 100#include <netinet/ip.h> 101#include <netinet/if_ether.h> 102 103#include <net/bpf.h> 104 105#include <dev/pccard/pccardvar.h> 106#include <dev/pccard/pccarddevs.h> 107 108#include <dev/wi/if_wavelan_ieee.h> 109#include <dev/wi/if_wireg.h> 110 111#include "card_if.h" 112 113#if !defined(lint) 114static const char rcsid[] = 115 "$FreeBSD: head/sys/dev/wi/if_wi.c 82258 2001-08-24 05:47:50Z nsayer $"; 116#endif 117 118#ifdef foo 119static u_int8_t wi_mcast_addr[6] = { 0x01, 0x60, 0x1D, 0x00, 0x01, 0x00 }; 120#endif 121 122static void wi_intr __P((void *)); 123static void wi_reset __P((struct wi_softc *)); 124static int wi_ioctl __P((struct ifnet *, u_long, caddr_t)); 125static void wi_init __P((void *)); 126static void wi_start __P((struct ifnet *)); 127static void wi_stop __P((struct wi_softc *)); 128static void wi_watchdog __P((struct ifnet *)); 129static void wi_rxeof __P((struct wi_softc *)); 130static void wi_txeof __P((struct wi_softc *, int)); 131static void wi_update_stats __P((struct wi_softc *)); 132static void wi_setmulti __P((struct wi_softc *)); 133 134static int wi_cmd __P((struct wi_softc *, int, int)); 135static int wi_read_record __P((struct wi_softc *, struct wi_ltv_gen *)); 136static int wi_write_record __P((struct wi_softc *, struct wi_ltv_gen *)); 137static int wi_read_data __P((struct wi_softc *, int, 138 int, caddr_t, int)); 139static int wi_write_data __P((struct wi_softc *, int, 140 int, caddr_t, int)); 141static int wi_seek __P((struct wi_softc *, int, int, int)); 142static int wi_alloc_nicmem __P((struct wi_softc *, int, int *)); 143static void wi_inquire __P((void *)); 144static void wi_setdef __P((struct wi_softc *, struct wi_req *)); 145static int wi_mgmt_xmit __P((struct wi_softc *, caddr_t, int)); 146 147#ifdef WICACHE 148static 149void wi_cache_store __P((struct wi_softc *, struct ether_header *, 150 struct mbuf *, unsigned short)); 151#endif 152 153static int wi_generic_attach __P((device_t)); 154static int wi_pccard_match __P((device_t)); 155static int wi_pccard_probe __P((device_t)); 156static int wi_pci_probe __P((device_t)); 157static int wi_pccard_attach __P((device_t)); 158static int wi_pci_attach __P((device_t)); 159static int wi_pccard_detach __P((device_t)); 160static void wi_shutdown __P((device_t)); 161 162static int wi_alloc __P((device_t, int)); 163static void wi_free __P((device_t)); 164 165static int wi_get_cur_ssid __P((struct wi_softc *, char *, int *)); 166static int wi_media_change __P((struct ifnet *)); 167static void wi_media_status __P((struct ifnet *, struct ifmediareq *)); 168 169static device_method_t wi_pccard_methods[] = { 170 /* Device interface */ 171 DEVMETHOD(device_probe, pccard_compat_probe), 172 DEVMETHOD(device_attach, pccard_compat_attach), 173 DEVMETHOD(device_detach, wi_pccard_detach), 174 DEVMETHOD(device_shutdown, wi_shutdown), 175 176 /* Card interface */ 177 DEVMETHOD(card_compat_match, wi_pccard_match), 178 DEVMETHOD(card_compat_probe, wi_pccard_probe), 179 DEVMETHOD(card_compat_attach, wi_pccard_attach), 180 181 { 0, 0 } 182}; 183 184static device_method_t wi_pci_methods[] = { 185 /* Device interface */ 186 DEVMETHOD(device_probe, wi_pci_probe), 187 DEVMETHOD(device_attach, wi_pci_attach), 188 DEVMETHOD(device_detach, wi_pccard_detach), 189 DEVMETHOD(device_shutdown, wi_shutdown), 190 191 { 0, 0 } 192}; 193 194static driver_t wi_pccard_driver = { 195 "wi", 196 wi_pccard_methods, 197 sizeof(struct wi_softc) 198}; 199 200static driver_t wi_pci_driver = { 201 "wi", 202 wi_pci_methods, 203 sizeof(struct wi_softc) 204}; 205 206static struct { 207 unsigned int vendor,device; 208 char *desc; 209} pci_ids[] = { 210 {0x1638, 0x1100, "PRISM2STA PCI WaveLAN/IEEE 802.11"}, 211 {0x1385, 0x4100, "Netgear MA301 PCI IEEE 802.11b"}, 212 {0, 0, NULL} 213}; 214 215static devclass_t wi_pccard_devclass; 216static devclass_t wi_pci_devclass; 217 218DRIVER_MODULE(if_wi, pccard, wi_pccard_driver, wi_pccard_devclass, 0, 0); 219DRIVER_MODULE(if_wi, pci, wi_pci_driver, wi_pci_devclass, 0, 0); 220 221static const struct pccard_product wi_pccard_products[] = { 222 { PCCARD_STR_LUCENT_WAVELAN_IEEE, PCCARD_VENDOR_LUCENT, 223 PCCARD_PRODUCT_LUCENT_WAVELAN_IEEE, 0, 224 PCCARD_CIS_LUCENT_WAVELAN_IEEE }, 225}; 226 227static int wi_pccard_match(dev) 228 device_t dev; 229{ 230 const struct pccard_product *pp; 231 232 if ((pp = pccard_product_lookup(dev, wi_pccard_products, 233 sizeof(wi_pccard_products[0]), NULL)) != NULL) { 234 device_set_desc(dev, pp->pp_name); 235 return 0; 236 } 237 return ENXIO; 238} 239 240static int wi_pccard_probe(dev) 241 device_t dev; 242{ 243 struct wi_softc *sc; 244 int error; 245 246 sc = device_get_softc(dev); 247 sc->wi_gone = 0; 248 249 error = wi_alloc(dev, 0); 250 if (error) 251 return (error); 252 253 wi_free(dev); 254 255 /* Make sure interrupts are disabled. */ 256 CSR_WRITE_2(sc, WI_INT_EN, 0); 257 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 258 259 return (0); 260} 261 262static int 263wi_pci_probe(dev) 264 device_t dev; 265{ 266 struct wi_softc *sc; 267 int i; 268 269 sc = device_get_softc(dev); 270 for(i=0; pci_ids[i].vendor != 0; i++) { 271 if ((pci_get_vendor(dev) == pci_ids[i].vendor) && 272 (pci_get_device(dev) == pci_ids[i].device)) { 273 sc->wi_prism2 = 1; 274 device_set_desc(dev, pci_ids[i].desc); 275 return (0); 276 } 277 } 278 return(ENXIO); 279} 280 281static int wi_pccard_detach(dev) 282 device_t dev; 283{ 284 struct wi_softc *sc; 285 struct ifnet *ifp; 286 287 sc = device_get_softc(dev); 288 WI_LOCK(sc); 289 ifp = &sc->arpcom.ac_if; 290 291 if (sc->wi_gone) { 292 device_printf(dev, "already unloaded\n"); 293 WI_UNLOCK(sc); 294 return(ENODEV); 295 } 296 297 wi_stop(sc); 298 299 /* Delete all remaining media. */ 300 ifmedia_removeall(&sc->ifmedia); 301 302 ether_ifdetach(ifp, ETHER_BPF_SUPPORTED); 303 bus_teardown_intr(dev, sc->irq, sc->wi_intrhand); 304 wi_free(dev); 305 sc->wi_gone = 1; 306 307 WI_UNLOCK(sc); 308 mtx_destroy(&sc->wi_mtx); 309 310 return(0); 311} 312 313static int wi_pccard_attach(device_t dev) 314{ 315 struct wi_softc *sc; 316 int error; 317 u_int32_t flags; 318 319 sc = device_get_softc(dev); 320 321 /* 322 * XXX: quick hack to support Prism II chip. 323 * Currently, we need to set a flags in pccard.conf to specify 324 * which type chip is used. 325 * 326 * We need to replace this code in a future. 327 * It is better to use CIS than using a flag. 328 */ 329 flags = device_get_flags(dev); 330#define WI_FLAGS_PRISM2 0x10000 331 if (flags & WI_FLAGS_PRISM2) { 332 sc->wi_prism2 = 1; 333 if (bootverbose) { 334 device_printf(dev, "found PrismII chip\n"); 335 } 336 } 337 else { 338 sc->wi_prism2 = 0; 339 if (bootverbose) { 340 device_printf(dev, "found Lucent chip\n"); 341 } 342 } 343 344 error = wi_alloc(dev, 0); 345 if (error) { 346 device_printf(dev, "wi_alloc() failed! (%d)\n", error); 347 return (error); 348 } 349 return (wi_generic_attach(dev)); 350} 351 352static int 353wi_pci_attach(device_t dev) 354{ 355 struct wi_softc *sc; 356 u_int32_t command, wanted; 357 u_int16_t reg; 358 int error; 359 360 sc = device_get_softc(dev); 361 362 command = pci_read_config(dev, PCIR_COMMAND, 4); 363 wanted = PCIM_CMD_PORTEN|PCIM_CMD_MEMEN; 364 command |= wanted; 365 pci_write_config(dev, PCIR_COMMAND, command, 4); 366 command = pci_read_config(dev, PCIR_COMMAND, 4); 367 if ((command & wanted) != wanted) { 368 device_printf(dev, "wi_pci_attach() failed to enable pci!\n"); 369 return (ENXIO); 370 } 371 372 error = wi_alloc(dev, WI_PCI_IORES); 373 if (error) 374 return (error); 375 376 /* Make sure interrupts are disabled. */ 377 CSR_WRITE_2(sc, WI_INT_EN, 0); 378 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 379 380 /* We have to do a magic PLX poke to enable interrupts */ 381 sc->local_rid = WI_PCI_LOCALRES; 382 sc->local = bus_alloc_resource(dev, SYS_RES_IOPORT, 383 &sc->local_rid, 0, ~0, 1, RF_ACTIVE); 384 sc->wi_localtag = rman_get_bustag(sc->local); 385 sc->wi_localhandle = rman_get_bushandle(sc->local); 386 command = bus_space_read_4(sc->wi_localtag, sc->wi_localhandle, 387 WI_LOCAL_INTCSR); 388 command |= WI_LOCAL_INTEN; 389 bus_space_write_4(sc->wi_localtag, sc->wi_localhandle, 390 WI_LOCAL_INTCSR, command); 391 bus_release_resource(dev, SYS_RES_IOPORT, sc->local_rid, sc->local); 392 sc->local = NULL; 393 394 sc->mem_rid = WI_PCI_MEMRES; 395 sc->mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid, 396 0, ~0, 1, RF_ACTIVE); 397 if (sc->mem == NULL) { 398 device_printf(dev, "couldn't allocate memory\n"); 399 wi_free(dev); 400 return (ENXIO); 401 } 402 sc->wi_bmemtag = rman_get_bustag(sc->mem); 403 sc->wi_bmemhandle = rman_get_bushandle(sc->mem); 404 405 /* 406 * From Linux driver: 407 * Write COR to enable PC card 408 * This is a subset of the protocol that the pccard bus code 409 * would do. 410 */ 411 CSM_WRITE_1(sc, WI_COR_OFFSET, WI_COR_VALUE); 412 reg = CSM_READ_1(sc, WI_COR_OFFSET); 413 414 CSR_WRITE_2(sc, WI_HFA384X_SWSUPPORT0_OFF, WI_PRISM2STA_MAGIC); 415 reg = CSR_READ_2(sc, WI_HFA384X_SWSUPPORT0_OFF); 416 if (reg != WI_PRISM2STA_MAGIC) { 417 device_printf(dev, 418 "CSR_READ_2(WI_HFA384X_SWSUPPORT0_OFF) " 419 "wanted %d, got %d\n", WI_PRISM2STA_MAGIC, reg); 420 wi_free(dev); 421 return (ENXIO); 422 } 423 424 error = wi_generic_attach(dev); 425 if (error != 0) 426 return (error); 427 428 return (0); 429} 430 431static int 432wi_generic_attach(device_t dev) 433{ 434 struct wi_softc *sc; 435 struct wi_ltv_macaddr mac; 436 struct wi_ltv_gen gen; 437 struct ifnet *ifp; 438 int error; 439 440 sc = device_get_softc(dev); 441 ifp = &sc->arpcom.ac_if; 442 443 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, 444 wi_intr, sc, &sc->wi_intrhand); 445 446 if (error) { 447 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error); 448 wi_free(dev); 449 return (error); 450 } 451 452 mtx_init(&sc->wi_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE); 453 WI_LOCK(sc); 454 455 /* Reset the NIC. */ 456 wi_reset(sc); 457 458 /* 459 * Read the station address. 460 * And do it twice. I've seen PRISM-based cards that return 461 * an error when trying to read it the first time, which causes 462 * the probe to fail. 463 */ 464 mac.wi_type = WI_RID_MAC_NODE; 465 mac.wi_len = 4; 466 wi_read_record(sc, (struct wi_ltv_gen *)&mac); 467 if ((error = wi_read_record(sc, (struct wi_ltv_gen *)&mac)) != 0) { 468 device_printf(dev, "mac read failed %d\n", error); 469 wi_free(dev); 470 return (error); 471 } 472 bcopy((char *)&mac.wi_mac_addr, 473 (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 474 475 device_printf(dev, "Ethernet address: %6D\n", 476 sc->arpcom.ac_enaddr, ":"); 477 478 ifp->if_softc = sc; 479 ifp->if_unit = sc->wi_unit; 480 ifp->if_name = "wi"; 481 ifp->if_mtu = ETHERMTU; 482 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 483 ifp->if_ioctl = wi_ioctl; 484 ifp->if_output = ether_output; 485 ifp->if_start = wi_start; 486 ifp->if_watchdog = wi_watchdog; 487 ifp->if_init = wi_init; 488 ifp->if_baudrate = 10000000; 489 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 490 491 bzero(sc->wi_node_name, sizeof(sc->wi_node_name)); 492 bcopy(WI_DEFAULT_NODENAME, sc->wi_node_name, 493 sizeof(WI_DEFAULT_NODENAME) - 1); 494 495 bzero(sc->wi_net_name, sizeof(sc->wi_net_name)); 496 bcopy(WI_DEFAULT_NETNAME, sc->wi_net_name, 497 sizeof(WI_DEFAULT_NETNAME) - 1); 498 499 bzero(sc->wi_ibss_name, sizeof(sc->wi_ibss_name)); 500 bcopy(WI_DEFAULT_IBSS, sc->wi_ibss_name, 501 sizeof(WI_DEFAULT_IBSS) - 1); 502 503 sc->wi_portnum = WI_DEFAULT_PORT; 504 sc->wi_ptype = WI_PORTTYPE_BSS; 505 sc->wi_ap_density = WI_DEFAULT_AP_DENSITY; 506 sc->wi_rts_thresh = WI_DEFAULT_RTS_THRESH; 507 sc->wi_tx_rate = WI_DEFAULT_TX_RATE; 508 sc->wi_max_data_len = WI_DEFAULT_DATALEN; 509 sc->wi_create_ibss = WI_DEFAULT_CREATE_IBSS; 510 sc->wi_pm_enabled = WI_DEFAULT_PM_ENABLED; 511 sc->wi_max_sleep = WI_DEFAULT_MAX_SLEEP; 512 513 /* 514 * Read the default channel from the NIC. This may vary 515 * depending on the country where the NIC was purchased, so 516 * we can't hard-code a default and expect it to work for 517 * everyone. 518 */ 519 gen.wi_type = WI_RID_OWN_CHNL; 520 gen.wi_len = 2; 521 wi_read_record(sc, &gen); 522 sc->wi_channel = gen.wi_val; 523 524 /* 525 * Find out if we support WEP on this card. 526 */ 527 gen.wi_type = WI_RID_WEP_AVAIL; 528 gen.wi_len = 2; 529 wi_read_record(sc, &gen); 530 sc->wi_has_wep = gen.wi_val; 531 532 if (bootverbose) { 533 device_printf(sc->dev, 534 __FUNCTION__ ":wi_has_wep = %d\n", 535 sc->wi_has_wep); 536 } 537 538 bzero((char *)&sc->wi_stats, sizeof(sc->wi_stats)); 539 540 wi_init(sc); 541 wi_stop(sc); 542 543 ifmedia_init(&sc->ifmedia, 0, wi_media_change, wi_media_status); 544 /* XXX: Should read from card capabilities */ 545#define ADD(m, c) ifmedia_add(&sc->ifmedia, (m), (c), NULL) 546 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1, 547 IFM_IEEE80211_ADHOC, 0), 0); 548 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1, 0, 0), 0); 549 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2, 550 IFM_IEEE80211_ADHOC, 0), 0); 551 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2, 0, 0), 0); 552 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS5, 553 IFM_IEEE80211_ADHOC, 0), 0); 554 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS5, 0, 0), 0); 555 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11, 556 IFM_IEEE80211_ADHOC, 0), 0); 557 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11, 0, 0), 0); 558 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 559 IFM_IEEE80211_ADHOC, 0), 0); 560 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0), 0); 561#undef ADD 562 ifmedia_set(&sc->ifmedia, IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 563 0, 0)); 564 565 566 /* 567 * Call MI attach routine. 568 */ 569 ether_ifattach(ifp, ETHER_BPF_SUPPORTED); 570 callout_handle_init(&sc->wi_stat_ch); 571 WI_UNLOCK(sc); 572 573 return(0); 574} 575 576static void wi_rxeof(sc) 577 struct wi_softc *sc; 578{ 579 struct ifnet *ifp; 580 struct ether_header *eh; 581 struct wi_frame rx_frame; 582 struct mbuf *m; 583 int id; 584 585 ifp = &sc->arpcom.ac_if; 586 587 id = CSR_READ_2(sc, WI_RX_FID); 588 589 /* First read in the frame header */ 590 if (wi_read_data(sc, id, 0, (caddr_t)&rx_frame, sizeof(rx_frame))) { 591 ifp->if_ierrors++; 592 return; 593 } 594 595 if (rx_frame.wi_status & WI_STAT_ERRSTAT) { 596 ifp->if_ierrors++; 597 return; 598 } 599 600 MGETHDR(m, M_DONTWAIT, MT_DATA); 601 if (m == NULL) { 602 ifp->if_ierrors++; 603 return; 604 } 605 MCLGET(m, M_DONTWAIT); 606 if (!(m->m_flags & M_EXT)) { 607 m_freem(m); 608 ifp->if_ierrors++; 609 return; 610 } 611 612 eh = mtod(m, struct ether_header *); 613 m->m_pkthdr.rcvif = ifp; 614 615 if (rx_frame.wi_status == WI_STAT_1042 || 616 rx_frame.wi_status == WI_STAT_TUNNEL || 617 rx_frame.wi_status == WI_STAT_WMP_MSG) { 618 if((rx_frame.wi_dat_len + WI_SNAPHDR_LEN) > MCLBYTES) { 619 device_printf(sc->dev, "oversized packet received " 620 "(wi_dat_len=%d, wi_status=0x%x)\n", 621 rx_frame.wi_dat_len, rx_frame.wi_status); 622 m_freem(m); 623 ifp->if_ierrors++; 624 return; 625 } 626 m->m_pkthdr.len = m->m_len = 627 rx_frame.wi_dat_len + WI_SNAPHDR_LEN; 628 629#if 0 630 bcopy((char *)&rx_frame.wi_addr1, 631 (char *)&eh->ether_dhost, ETHER_ADDR_LEN); 632 if (sc->wi_ptype == WI_PORTTYPE_ADHOC) { 633 bcopy((char *)&rx_frame.wi_addr2, 634 (char *)&eh->ether_shost, ETHER_ADDR_LEN); 635 } else { 636 bcopy((char *)&rx_frame.wi_addr3, 637 (char *)&eh->ether_shost, ETHER_ADDR_LEN); 638 } 639#else 640 bcopy((char *)&rx_frame.wi_dst_addr, 641 (char *)&eh->ether_dhost, ETHER_ADDR_LEN); 642 bcopy((char *)&rx_frame.wi_src_addr, 643 (char *)&eh->ether_shost, ETHER_ADDR_LEN); 644#endif 645 646 bcopy((char *)&rx_frame.wi_type, 647 (char *)&eh->ether_type, ETHER_TYPE_LEN); 648 649 if (wi_read_data(sc, id, WI_802_11_OFFSET, 650 mtod(m, caddr_t) + sizeof(struct ether_header), 651 m->m_len + 2)) { 652 m_freem(m); 653 ifp->if_ierrors++; 654 return; 655 } 656 } else { 657 if((rx_frame.wi_dat_len + 658 sizeof(struct ether_header)) > MCLBYTES) { 659 device_printf(sc->dev, "oversized packet received " 660 "(wi_dat_len=%d, wi_status=0x%x)\n", 661 rx_frame.wi_dat_len, rx_frame.wi_status); 662 m_freem(m); 663 ifp->if_ierrors++; 664 return; 665 } 666 m->m_pkthdr.len = m->m_len = 667 rx_frame.wi_dat_len + sizeof(struct ether_header); 668 669 if (wi_read_data(sc, id, WI_802_3_OFFSET, 670 mtod(m, caddr_t), m->m_len + 2)) { 671 m_freem(m); 672 ifp->if_ierrors++; 673 return; 674 } 675 } 676 677 ifp->if_ipackets++; 678 679 /* Receive packet. */ 680 m_adj(m, sizeof(struct ether_header)); 681#ifdef WICACHE 682 wi_cache_store(sc, eh, m, rx_frame.wi_q_info); 683#endif 684 ether_input(ifp, eh, m); 685} 686 687static void wi_txeof(sc, status) 688 struct wi_softc *sc; 689 int status; 690{ 691 struct ifnet *ifp; 692 693 ifp = &sc->arpcom.ac_if; 694 695 ifp->if_timer = 0; 696 ifp->if_flags &= ~IFF_OACTIVE; 697 698 if (status & WI_EV_TX_EXC) 699 ifp->if_oerrors++; 700 else 701 ifp->if_opackets++; 702 703 return; 704} 705 706void wi_inquire(xsc) 707 void *xsc; 708{ 709 struct wi_softc *sc; 710 struct ifnet *ifp; 711 712 sc = xsc; 713 ifp = &sc->arpcom.ac_if; 714 715 sc->wi_stat_ch = timeout(wi_inquire, sc, hz * 60); 716 717 /* Don't do this while we're transmitting */ 718 if (ifp->if_flags & IFF_OACTIVE) 719 return; 720 721 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_COUNTERS); 722 723 return; 724} 725 726void wi_update_stats(sc) 727 struct wi_softc *sc; 728{ 729 struct wi_ltv_gen gen; 730 u_int16_t id; 731 struct ifnet *ifp; 732 u_int32_t *ptr; 733 int len, i; 734 u_int16_t t; 735 736 ifp = &sc->arpcom.ac_if; 737 738 id = CSR_READ_2(sc, WI_INFO_FID); 739 740 wi_read_data(sc, id, 0, (char *)&gen, 4); 741 742 if (gen.wi_type != WI_INFO_COUNTERS) 743 return; 744 745 len = (gen.wi_len - 1 < sizeof(sc->wi_stats) / 4) ? 746 gen.wi_len - 1 : sizeof(sc->wi_stats) / 4; 747 ptr = (u_int32_t *)&sc->wi_stats; 748 749 for (i = 0; i < len - 1; i++) { 750 t = CSR_READ_2(sc, WI_DATA1); 751#ifdef WI_HERMES_STATS_WAR 752 if (t > 0xF000) 753 t = ~t & 0xFFFF; 754#endif 755 ptr[i] += t; 756 } 757 758 ifp->if_collisions = sc->wi_stats.wi_tx_single_retries + 759 sc->wi_stats.wi_tx_multi_retries + 760 sc->wi_stats.wi_tx_retry_limit; 761 762 return; 763} 764 765static void wi_intr(xsc) 766 void *xsc; 767{ 768 struct wi_softc *sc = xsc; 769 struct ifnet *ifp; 770 u_int16_t status; 771 772 WI_LOCK(sc); 773 774 ifp = &sc->arpcom.ac_if; 775 776 if (sc->wi_gone || !(ifp->if_flags & IFF_UP)) { 777 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 778 CSR_WRITE_2(sc, WI_INT_EN, 0); 779 WI_UNLOCK(sc); 780 return; 781 } 782 783 /* Disable interrupts. */ 784 CSR_WRITE_2(sc, WI_INT_EN, 0); 785 786 status = CSR_READ_2(sc, WI_EVENT_STAT); 787 CSR_WRITE_2(sc, WI_EVENT_ACK, ~WI_INTRS); 788 789 if (status & WI_EV_RX) { 790 wi_rxeof(sc); 791 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 792 } 793 794 if (status & WI_EV_TX) { 795 wi_txeof(sc, status); 796 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX); 797 } 798 799 if (status & WI_EV_ALLOC) { 800 int id; 801 802 id = CSR_READ_2(sc, WI_ALLOC_FID); 803 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 804 if (id == sc->wi_tx_data_id) 805 wi_txeof(sc, status); 806 } 807 808 if (status & WI_EV_INFO) { 809 wi_update_stats(sc); 810 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO); 811 } 812 813 if (status & WI_EV_TX_EXC) { 814 wi_txeof(sc, status); 815 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC); 816 } 817 818 if (status & WI_EV_INFO_DROP) { 819 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO_DROP); 820 } 821 822 /* Re-enable interrupts. */ 823 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 824 825 if (ifp->if_snd.ifq_head != NULL) { 826 wi_start(ifp); 827 } 828 829 WI_UNLOCK(sc); 830 831 return; 832} 833 834static int wi_cmd(sc, cmd, val) 835 struct wi_softc *sc; 836 int cmd; 837 int val; 838{ 839 int i, s = 0; 840 841 /* wait for the busy bit to clear */ 842 for (i = 500; i > 0; i--) { /* 5s */ 843 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY)) { 844 break; 845 } 846 DELAY(10*1000); /* 10 m sec */ 847 } 848 if (i == 0) { 849 return(ETIMEDOUT); 850 } 851 852 CSR_WRITE_2(sc, WI_PARAM0, val); 853 CSR_WRITE_2(sc, WI_PARAM1, 0); 854 CSR_WRITE_2(sc, WI_PARAM2, 0); 855 CSR_WRITE_2(sc, WI_COMMAND, cmd); 856 857 for (i = 0; i < WI_TIMEOUT; i++) { 858 /* 859 * Wait for 'command complete' bit to be 860 * set in the event status register. 861 */ 862 s = CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD; 863 if (s) { 864 /* Ack the event and read result code. */ 865 s = CSR_READ_2(sc, WI_STATUS); 866 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 867#ifdef foo 868 if ((s & WI_CMD_CODE_MASK) != (cmd & WI_CMD_CODE_MASK)) 869 return(EIO); 870#endif 871 if (s & WI_STAT_CMD_RESULT) 872 return(EIO); 873 break; 874 } 875 if (cmd == WI_CMD_INI) 876 DELAY(100); 877 } 878 879 if (i == WI_TIMEOUT) 880 return(ETIMEDOUT); 881 882 return(0); 883} 884 885static void wi_reset(sc) 886 struct wi_softc *sc; 887{ 888#define WI_INIT_TRIES 5 889 int i; 890 891 for (i = 0; i < WI_INIT_TRIES; i++) { 892 if (wi_cmd(sc, WI_CMD_INI, 0) == 0) 893 break; 894 DELAY(50 * 1000); /* 50ms */ 895 } 896 if (i == WI_INIT_TRIES) 897 device_printf(sc->dev, "init failed\n"); 898 899 CSR_WRITE_2(sc, WI_INT_EN, 0); 900 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 901 902 /* Calibrate timer. */ 903 WI_SETVAL(WI_RID_TICK_TIME, 8); 904 905 return; 906} 907 908/* 909 * Read an LTV record from the NIC. 910 */ 911static int wi_read_record(sc, ltv) 912 struct wi_softc *sc; 913 struct wi_ltv_gen *ltv; 914{ 915 u_int16_t *ptr; 916 int i, len, code; 917 struct wi_ltv_gen *oltv, p2ltv; 918 919 oltv = ltv; 920 if (sc->wi_prism2) { 921 switch (ltv->wi_type) { 922 case WI_RID_ENCRYPTION: 923 p2ltv.wi_type = WI_RID_P2_ENCRYPTION; 924 p2ltv.wi_len = 2; 925 ltv = &p2ltv; 926 break; 927 case WI_RID_TX_CRYPT_KEY: 928 p2ltv.wi_type = WI_RID_P2_TX_CRYPT_KEY; 929 p2ltv.wi_len = 2; 930 ltv = &p2ltv; 931 break; 932 } 933 } 934 935 /* Tell the NIC to enter record read mode. */ 936 if (wi_cmd(sc, WI_CMD_ACCESS|WI_ACCESS_READ, ltv->wi_type)) 937 return(EIO); 938 939 /* Seek to the record. */ 940 if (wi_seek(sc, ltv->wi_type, 0, WI_BAP1)) 941 return(EIO); 942 943 /* 944 * Read the length and record type and make sure they 945 * match what we expect (this verifies that we have enough 946 * room to hold all of the returned data). 947 */ 948 len = CSR_READ_2(sc, WI_DATA1); 949 if (len > ltv->wi_len) 950 return(ENOSPC); 951 code = CSR_READ_2(sc, WI_DATA1); 952 if (code != ltv->wi_type) 953 return(EIO); 954 955 ltv->wi_len = len; 956 ltv->wi_type = code; 957 958 /* Now read the data. */ 959 ptr = <v->wi_val; 960 for (i = 0; i < ltv->wi_len - 1; i++) 961 ptr[i] = CSR_READ_2(sc, WI_DATA1); 962 963 if (sc->wi_prism2) { 964 switch (oltv->wi_type) { 965 case WI_RID_TX_RATE: 966 case WI_RID_CUR_TX_RATE: 967 switch (ltv->wi_val) { 968 case 1: oltv->wi_val = 1; break; 969 case 2: oltv->wi_val = 2; break; 970 case 3: oltv->wi_val = 6; break; 971 case 4: oltv->wi_val = 5; break; 972 case 7: oltv->wi_val = 7; break; 973 case 8: oltv->wi_val = 11; break; 974 case 15: oltv->wi_val = 3; break; 975 default: oltv->wi_val = 0x100 + ltv->wi_val; break; 976 } 977 break; 978 case WI_RID_ENCRYPTION: 979 oltv->wi_len = 2; 980 if (ltv->wi_val & 0x01) 981 oltv->wi_val = 1; 982 else 983 oltv->wi_val = 0; 984 break; 985 case WI_RID_TX_CRYPT_KEY: 986 oltv->wi_len = 2; 987 oltv->wi_val = ltv->wi_val; 988 break; 989 } 990 } 991 992 return(0); 993} 994 995/* 996 * Same as read, except we inject data instead of reading it. 997 */ 998static int wi_write_record(sc, ltv) 999 struct wi_softc *sc; 1000 struct wi_ltv_gen *ltv; 1001{ 1002 u_int16_t *ptr; 1003 int i; 1004 struct wi_ltv_gen p2ltv; 1005 1006 if (sc->wi_prism2) { 1007 switch (ltv->wi_type) { 1008 case WI_RID_TX_RATE: 1009 p2ltv.wi_type = WI_RID_TX_RATE; 1010 p2ltv.wi_len = 2; 1011 switch (ltv->wi_val) { 1012 case 1: p2ltv.wi_val = 1; break; 1013 case 2: p2ltv.wi_val = 2; break; 1014 case 3: p2ltv.wi_val = 15; break; 1015 case 5: p2ltv.wi_val = 4; break; 1016 case 6: p2ltv.wi_val = 3; break; 1017 case 7: p2ltv.wi_val = 7; break; 1018 case 11: p2ltv.wi_val = 8; break; 1019 default: return EINVAL; 1020 } 1021 ltv = &p2ltv; 1022 break; 1023 case WI_RID_ENCRYPTION: 1024 p2ltv.wi_type = WI_RID_P2_ENCRYPTION; 1025 p2ltv.wi_len = 2; 1026 if (ltv->wi_val) 1027 p2ltv.wi_val = 0x03; 1028 else 1029 p2ltv.wi_val = 0x90; 1030 ltv = &p2ltv; 1031 break; 1032 case WI_RID_TX_CRYPT_KEY: 1033 p2ltv.wi_type = WI_RID_P2_TX_CRYPT_KEY; 1034 p2ltv.wi_len = 2; 1035 p2ltv.wi_val = ltv->wi_val; 1036 ltv = &p2ltv; 1037 break; 1038 case WI_RID_DEFLT_CRYPT_KEYS: 1039 { 1040 int error; 1041 struct wi_ltv_str ws; 1042 struct wi_ltv_keys *wk = 1043 (struct wi_ltv_keys *)ltv; 1044 1045 for (i = 0; i < 4; i++) { 1046 ws.wi_len = 4; 1047 ws.wi_type = WI_RID_P2_CRYPT_KEY0 + i; 1048 memcpy(ws.wi_str, 1049 &wk->wi_keys[i].wi_keydat, 5); 1050 ws.wi_str[5] = '\0'; 1051 error = wi_write_record(sc, 1052 (struct wi_ltv_gen *)&ws); 1053 if (error) 1054 return error; 1055 } 1056 return 0; 1057 } 1058 } 1059 } 1060 1061 if (wi_seek(sc, ltv->wi_type, 0, WI_BAP1)) 1062 return(EIO); 1063 1064 CSR_WRITE_2(sc, WI_DATA1, ltv->wi_len); 1065 CSR_WRITE_2(sc, WI_DATA1, ltv->wi_type); 1066 1067 ptr = <v->wi_val; 1068 for (i = 0; i < ltv->wi_len - 1; i++) 1069 CSR_WRITE_2(sc, WI_DATA1, ptr[i]); 1070 1071 if (wi_cmd(sc, WI_CMD_ACCESS|WI_ACCESS_WRITE, ltv->wi_type)) 1072 return(EIO); 1073 1074 return(0); 1075} 1076 1077static int wi_seek(sc, id, off, chan) 1078 struct wi_softc *sc; 1079 int id, off, chan; 1080{ 1081 int i; 1082 int selreg, offreg; 1083 int status; 1084 1085 switch (chan) { 1086 case WI_BAP0: 1087 selreg = WI_SEL0; 1088 offreg = WI_OFF0; 1089 break; 1090 case WI_BAP1: 1091 selreg = WI_SEL1; 1092 offreg = WI_OFF1; 1093 break; 1094 default: 1095 device_printf(sc->dev, "invalid data path: %x\n", chan); 1096 return(EIO); 1097 } 1098 1099 CSR_WRITE_2(sc, selreg, id); 1100 CSR_WRITE_2(sc, offreg, off); 1101 1102 for (i = 0; i < WI_TIMEOUT; i++) { 1103 status = CSR_READ_2(sc, offreg); 1104 if (!(status & (WI_OFF_BUSY|WI_OFF_ERR))) 1105 break; 1106 } 1107 1108 if (i == WI_TIMEOUT) { 1109 device_printf(sc->dev, "timeout in wi_seek to %x/%x; last status %x\n", 1110 id, off, status); 1111 return(ETIMEDOUT); 1112 } 1113 1114 return(0); 1115} 1116 1117static int wi_read_data(sc, id, off, buf, len) 1118 struct wi_softc *sc; 1119 int id, off; 1120 caddr_t buf; 1121 int len; 1122{ 1123 int i; 1124 u_int16_t *ptr; 1125 1126 if (wi_seek(sc, id, off, WI_BAP1)) 1127 return(EIO); 1128 1129 ptr = (u_int16_t *)buf; 1130 for (i = 0; i < len / 2; i++) 1131 ptr[i] = CSR_READ_2(sc, WI_DATA1); 1132 1133 return(0); 1134} 1135 1136/* 1137 * According to the comments in the HCF Light code, there is a bug in 1138 * the Hermes (or possibly in certain Hermes firmware revisions) where 1139 * the chip's internal autoincrement counter gets thrown off during 1140 * data writes: the autoincrement is missed, causing one data word to 1141 * be overwritten and subsequent words to be written to the wrong memory 1142 * locations. The end result is that we could end up transmitting bogus 1143 * frames without realizing it. The workaround for this is to write a 1144 * couple of extra guard words after the end of the transfer, then 1145 * attempt to read then back. If we fail to locate the guard words where 1146 * we expect them, we preform the transfer over again. 1147 */ 1148static int wi_write_data(sc, id, off, buf, len) 1149 struct wi_softc *sc; 1150 int id, off; 1151 caddr_t buf; 1152 int len; 1153{ 1154 int i; 1155 u_int16_t *ptr; 1156#ifdef WI_HERMES_AUTOINC_WAR 1157 int retries; 1158 1159 retries = 512; 1160again: 1161#endif 1162 1163 if (wi_seek(sc, id, off, WI_BAP0)) 1164 return(EIO); 1165 1166 ptr = (u_int16_t *)buf; 1167 for (i = 0; i < (len / 2); i++) 1168 CSR_WRITE_2(sc, WI_DATA0, ptr[i]); 1169 1170#ifdef WI_HERMES_AUTOINC_WAR 1171 CSR_WRITE_2(sc, WI_DATA0, 0x1234); 1172 CSR_WRITE_2(sc, WI_DATA0, 0x5678); 1173 1174 if (wi_seek(sc, id, off + len, WI_BAP0)) 1175 return(EIO); 1176 1177 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 || 1178 CSR_READ_2(sc, WI_DATA0) != 0x5678) { 1179 if (--retries >= 0) 1180 goto again; 1181 device_printf(sc->dev, "wi_write_data device timeout\n"); 1182 return (EIO); 1183 } 1184#endif 1185 1186 return(0); 1187} 1188 1189/* 1190 * Allocate a region of memory inside the NIC and zero 1191 * it out. 1192 */ 1193static int wi_alloc_nicmem(sc, len, id) 1194 struct wi_softc *sc; 1195 int len; 1196 int *id; 1197{ 1198 int i; 1199 1200 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len)) { 1201 device_printf(sc->dev, 1202 "failed to allocate %d bytes on NIC\n", len); 1203 return(ENOMEM); 1204 } 1205 1206 for (i = 0; i < WI_TIMEOUT; i++) { 1207 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC) 1208 break; 1209 } 1210 1211 if (i == WI_TIMEOUT) { 1212 device_printf(sc->dev, "time out allocating memory on card\n"); 1213 return(ETIMEDOUT); 1214 } 1215 1216 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 1217 *id = CSR_READ_2(sc, WI_ALLOC_FID); 1218 1219 if (wi_seek(sc, *id, 0, WI_BAP0)) { 1220 device_printf(sc->dev, "seek failed while allocating memory on card\n"); 1221 return(EIO); 1222 } 1223 1224 for (i = 0; i < len / 2; i++) 1225 CSR_WRITE_2(sc, WI_DATA0, 0); 1226 1227 return(0); 1228} 1229 1230static void wi_setmulti(sc) 1231 struct wi_softc *sc; 1232{ 1233 struct ifnet *ifp; 1234 int i = 0; 1235 struct ifmultiaddr *ifma; 1236 struct wi_ltv_mcast mcast; 1237 1238 ifp = &sc->arpcom.ac_if; 1239 1240 bzero((char *)&mcast, sizeof(mcast)); 1241 1242 mcast.wi_type = WI_RID_MCAST; 1243 mcast.wi_len = (3 * 16) + 1; 1244 1245 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 1246 wi_write_record(sc, (struct wi_ltv_gen *)&mcast); 1247 return; 1248 } 1249 1250 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1251 if (ifma->ifma_addr->sa_family != AF_LINK) 1252 continue; 1253 if (i < 16) { 1254 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 1255 (char *)&mcast.wi_mcast[i], ETHER_ADDR_LEN); 1256 i++; 1257 } else { 1258 bzero((char *)&mcast, sizeof(mcast)); 1259 break; 1260 } 1261 } 1262 1263 mcast.wi_len = (i * 3) + 1; 1264 wi_write_record(sc, (struct wi_ltv_gen *)&mcast); 1265 1266 return; 1267} 1268 1269static void wi_setdef(sc, wreq) 1270 struct wi_softc *sc; 1271 struct wi_req *wreq; 1272{ 1273 struct sockaddr_dl *sdl; 1274 struct ifaddr *ifa; 1275 struct ifnet *ifp; 1276 1277 ifp = &sc->arpcom.ac_if; 1278 1279 switch(wreq->wi_type) { 1280 case WI_RID_MAC_NODE: 1281 ifa = ifnet_addrs[ifp->if_index - 1]; 1282 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1283 bcopy((char *)&wreq->wi_val, (char *)&sc->arpcom.ac_enaddr, 1284 ETHER_ADDR_LEN); 1285 bcopy((char *)&wreq->wi_val, LLADDR(sdl), ETHER_ADDR_LEN); 1286 break; 1287 case WI_RID_PORTTYPE: 1288 sc->wi_ptype = wreq->wi_val[0]; 1289 break; 1290 case WI_RID_TX_RATE: 1291 sc->wi_tx_rate = wreq->wi_val[0]; 1292 break; 1293 case WI_RID_MAX_DATALEN: 1294 sc->wi_max_data_len = wreq->wi_val[0]; 1295 break; 1296 case WI_RID_RTS_THRESH: 1297 sc->wi_rts_thresh = wreq->wi_val[0]; 1298 break; 1299 case WI_RID_SYSTEM_SCALE: 1300 sc->wi_ap_density = wreq->wi_val[0]; 1301 break; 1302 case WI_RID_CREATE_IBSS: 1303 sc->wi_create_ibss = wreq->wi_val[0]; 1304 break; 1305 case WI_RID_OWN_CHNL: 1306 sc->wi_channel = wreq->wi_val[0]; 1307 break; 1308 case WI_RID_NODENAME: 1309 bzero(sc->wi_node_name, sizeof(sc->wi_node_name)); 1310 bcopy((char *)&wreq->wi_val[1], sc->wi_node_name, 30); 1311 break; 1312 case WI_RID_DESIRED_SSID: 1313 bzero(sc->wi_net_name, sizeof(sc->wi_net_name)); 1314 bcopy((char *)&wreq->wi_val[1], sc->wi_net_name, 30); 1315 break; 1316 case WI_RID_OWN_SSID: 1317 bzero(sc->wi_ibss_name, sizeof(sc->wi_ibss_name)); 1318 bcopy((char *)&wreq->wi_val[1], sc->wi_ibss_name, 30); 1319 break; 1320 case WI_RID_PM_ENABLED: 1321 sc->wi_pm_enabled = wreq->wi_val[0]; 1322 break; 1323 case WI_RID_MAX_SLEEP: 1324 sc->wi_max_sleep = wreq->wi_val[0]; 1325 break; 1326 case WI_RID_ENCRYPTION: 1327 sc->wi_use_wep = wreq->wi_val[0]; 1328 break; 1329 case WI_RID_TX_CRYPT_KEY: 1330 sc->wi_tx_key = wreq->wi_val[0]; 1331 break; 1332 case WI_RID_DEFLT_CRYPT_KEYS: 1333 bcopy((char *)wreq, (char *)&sc->wi_keys, 1334 sizeof(struct wi_ltv_keys)); 1335 break; 1336 default: 1337 break; 1338 } 1339 1340 /* Reinitialize WaveLAN. */ 1341 wi_init(sc); 1342 1343 return; 1344} 1345 1346static int wi_ioctl(ifp, command, data) 1347 struct ifnet *ifp; 1348 u_long command; 1349 caddr_t data; 1350{ 1351 int error = 0; 1352 int len; 1353 u_int8_t tmpkey[14]; 1354 char tmpssid[IEEE80211_NWID_LEN]; 1355 struct wi_softc *sc; 1356 struct wi_req wreq; 1357 struct ifreq *ifr; 1358 struct ieee80211req *ireq; 1359 struct proc *p = curproc; 1360 1361 sc = ifp->if_softc; 1362 WI_LOCK(sc); 1363 ifr = (struct ifreq *)data; 1364 ireq = (struct ieee80211req *)data; 1365 1366 if (sc->wi_gone) { 1367 error = ENODEV; 1368 goto out; 1369 } 1370 1371 switch(command) { 1372 case SIOCSIFADDR: 1373 case SIOCGIFADDR: 1374 case SIOCSIFMTU: 1375 error = ether_ioctl(ifp, command, data); 1376 break; 1377 case SIOCSIFFLAGS: 1378 if (ifp->if_flags & IFF_UP) { 1379 if (ifp->if_flags & IFF_RUNNING && 1380 ifp->if_flags & IFF_PROMISC && 1381 !(sc->wi_if_flags & IFF_PROMISC)) { 1382 WI_SETVAL(WI_RID_PROMISC, 1); 1383 } else if (ifp->if_flags & IFF_RUNNING && 1384 !(ifp->if_flags & IFF_PROMISC) && 1385 sc->wi_if_flags & IFF_PROMISC) { 1386 WI_SETVAL(WI_RID_PROMISC, 0); 1387 } else 1388 wi_init(sc); 1389 } else { 1390 if (ifp->if_flags & IFF_RUNNING) { 1391 wi_stop(sc); 1392 } 1393 } 1394 sc->wi_if_flags = ifp->if_flags; 1395 error = 0; 1396 break; 1397 case SIOCSIFMEDIA: 1398 case SIOCGIFMEDIA: 1399 error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command); 1400 break; 1401 case SIOCADDMULTI: 1402 case SIOCDELMULTI: 1403 wi_setmulti(sc); 1404 error = 0; 1405 break; 1406 case SIOCGWAVELAN: 1407 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1408 if (error) 1409 break; 1410 /* Don't show WEP keys to non-root users. */ 1411 if (wreq.wi_type == WI_RID_DEFLT_CRYPT_KEYS && suser(p)) 1412 break; 1413 if (wreq.wi_type == WI_RID_IFACE_STATS) { 1414 bcopy((char *)&sc->wi_stats, (char *)&wreq.wi_val, 1415 sizeof(sc->wi_stats)); 1416 wreq.wi_len = (sizeof(sc->wi_stats) / 2) + 1; 1417 } else if (wreq.wi_type == WI_RID_DEFLT_CRYPT_KEYS) { 1418 bcopy((char *)&sc->wi_keys, (char *)&wreq, 1419 sizeof(struct wi_ltv_keys)); 1420 } 1421#ifdef WICACHE 1422 else if (wreq.wi_type == WI_RID_ZERO_CACHE) { 1423 sc->wi_sigitems = sc->wi_nextitem = 0; 1424 } else if (wreq.wi_type == WI_RID_READ_CACHE) { 1425 char *pt = (char *)&wreq.wi_val; 1426 bcopy((char *)&sc->wi_sigitems, 1427 (char *)pt, sizeof(int)); 1428 pt += (sizeof (int)); 1429 wreq.wi_len = sizeof(int) / 2; 1430 bcopy((char *)&sc->wi_sigcache, (char *)pt, 1431 sizeof(struct wi_sigcache) * sc->wi_sigitems); 1432 wreq.wi_len += ((sizeof(struct wi_sigcache) * 1433 sc->wi_sigitems) / 2) + 1; 1434 } 1435#endif 1436 else { 1437 if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq)) { 1438 error = EINVAL; 1439 break; 1440 } 1441 } 1442 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq)); 1443 break; 1444 case SIOCSWAVELAN: 1445 if ((error = suser(p))) 1446 goto out; 1447 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1448 if (error) 1449 break; 1450 if (wreq.wi_type == WI_RID_IFACE_STATS) { 1451 error = EINVAL; 1452 break; 1453 } else if (wreq.wi_type == WI_RID_MGMT_XMIT) { 1454 error = wi_mgmt_xmit(sc, (caddr_t)&wreq.wi_val, 1455 wreq.wi_len); 1456 } else { 1457 error = wi_write_record(sc, (struct wi_ltv_gen *)&wreq); 1458 if (!error) 1459 wi_setdef(sc, &wreq); 1460 } 1461 break; 1462 case SIOCG80211: 1463 switch(ireq->i_type) { 1464 case IEEE80211_IOC_SSID: 1465 if(ireq->i_val == -1) { 1466 bzero(tmpssid, IEEE80211_NWID_LEN); 1467 error = wi_get_cur_ssid(sc, tmpssid, &len); 1468 if (error != 0) 1469 break; 1470 error = copyout(tmpssid, ireq->i_data, 1471 IEEE80211_NWID_LEN); 1472 ireq->i_len = len; 1473 } else if (ireq->i_val == 0) { 1474 error = copyout(sc->wi_net_name, 1475 ireq->i_data, 1476 IEEE80211_NWID_LEN); 1477 ireq->i_len = IEEE80211_NWID_LEN; 1478 } else 1479 error = EINVAL; 1480 break; 1481 case IEEE80211_IOC_NUMSSIDS: 1482 ireq->i_val = 1; 1483 break; 1484 case IEEE80211_IOC_WEP: 1485 if(!sc->wi_has_wep) { 1486 ireq->i_val = IEEE80211_WEP_NOSUP; 1487 } else { 1488 if(sc->wi_use_wep) { 1489 ireq->i_val = 1490 IEEE80211_WEP_MIXED; 1491 } else { 1492 ireq->i_val = 1493 IEEE80211_WEP_OFF; 1494 } 1495 } 1496 break; 1497 case IEEE80211_IOC_WEPKEY: 1498 if(!sc->wi_has_wep || 1499 ireq->i_val < 0 || ireq->i_val > 3) { 1500 error = EINVAL; 1501 break; 1502 } 1503 len = sc->wi_keys.wi_keys[ireq->i_val].wi_keylen; 1504 if (suser(p)) 1505 bcopy(sc->wi_keys.wi_keys[ireq->i_val].wi_keydat, 1506 tmpkey, len); 1507 else 1508 bzero(tmpkey, len); 1509 1510 ireq->i_len = len; 1511 error = copyout(tmpkey, ireq->i_data, len); 1512 1513 break; 1514 case IEEE80211_IOC_NUMWEPKEYS: 1515 if(!sc->wi_has_wep) 1516 error = EINVAL; 1517 else 1518 ireq->i_val = 4; 1519 break; 1520 case IEEE80211_IOC_WEPTXKEY: 1521 if(!sc->wi_has_wep) 1522 error = EINVAL; 1523 else 1524 ireq->i_val = sc->wi_tx_key; 1525 break; 1526 case IEEE80211_IOC_AUTHMODE: 1527 ireq->i_val = IEEE80211_AUTH_NONE; 1528 break; 1529 case IEEE80211_IOC_STATIONNAME: 1530 error = copyout(sc->wi_node_name, 1531 ireq->i_data, IEEE80211_NWID_LEN); 1532 ireq->i_len = IEEE80211_NWID_LEN; 1533 break; 1534 case IEEE80211_IOC_CHANNEL: 1535 wreq.wi_type = WI_RID_CURRENT_CHAN; 1536 wreq.wi_len = WI_MAX_DATALEN; 1537 if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq)) 1538 error = EINVAL; 1539 else { 1540 ireq->i_val = wreq.wi_val[0]; 1541 } 1542 break; 1543 case IEEE80211_IOC_POWERSAVE: 1544 if(sc->wi_pm_enabled) 1545 ireq->i_val = IEEE80211_POWERSAVE_ON; 1546 else 1547 ireq->i_val = IEEE80211_POWERSAVE_OFF; 1548 break; 1549 case IEEE80211_IOC_POWERSAVESLEEP: 1550 ireq->i_val = sc->wi_max_sleep; 1551 break; 1552 default: 1553 error = EINVAL; 1554 } 1555 break; 1556 case SIOCS80211: 1557 if ((error = suser(p))) 1558 goto out; 1559 switch(ireq->i_type) { 1560 case IEEE80211_IOC_SSID: 1561 if (ireq->i_val != 0 || 1562 ireq->i_len > IEEE80211_NWID_LEN) { 1563 error = EINVAL; 1564 break; 1565 } 1566 /* We set both of them */ 1567 bzero(sc->wi_net_name, IEEE80211_NWID_LEN); 1568 error = copyin(ireq->i_data, 1569 sc->wi_net_name, ireq->i_len); 1570 bcopy(sc->wi_net_name, sc->wi_ibss_name, IEEE80211_NWID_LEN); 1571 break; 1572 case IEEE80211_IOC_WEP: 1573 /* 1574 * These cards only support one mode so 1575 * we just turn wep on what ever is 1576 * passed in if it's not OFF. 1577 */ 1578 if (ireq->i_val == IEEE80211_WEP_OFF) { 1579 sc->wi_use_wep = 0; 1580 } else { 1581 sc->wi_use_wep = 1; 1582 } 1583 break; 1584 case IEEE80211_IOC_WEPKEY: 1585 if (ireq->i_val < 0 || ireq->i_val > 3 || 1586 ireq->i_len > 13) { 1587 error = EINVAL; 1588 break; 1589 } 1590 bzero(sc->wi_keys.wi_keys[ireq->i_val].wi_keydat, 13); 1591 error = copyin(ireq->i_data, 1592 sc->wi_keys.wi_keys[ireq->i_val].wi_keydat, 1593 ireq->i_len); 1594 if(error) 1595 break; 1596 sc->wi_keys.wi_keys[ireq->i_val].wi_keylen = 1597 ireq->i_len; 1598 break; 1599 case IEEE80211_IOC_WEPTXKEY: 1600 if (ireq->i_val < 0 || ireq->i_val > 3) { 1601 error = EINVAL; 1602 break; 1603 } 1604 sc->wi_tx_key = ireq->i_val; 1605 break; 1606 case IEEE80211_IOC_AUTHMODE: 1607 error = EINVAL; 1608 break; 1609 case IEEE80211_IOC_STATIONNAME: 1610 if (ireq->i_len > 32) { 1611 error = EINVAL; 1612 break; 1613 } 1614 bzero(sc->wi_node_name, 32); 1615 error = copyin(ireq->i_data, 1616 sc->wi_node_name, ireq->i_len); 1617 break; 1618 case IEEE80211_IOC_CHANNEL: 1619 /* 1620 * The actual range is 1-14, but if you 1621 * set it to 0 you get the default. So 1622 * we let that work too. 1623 */ 1624 if (ireq->i_val < 0 || ireq->i_val > 14) { 1625 error = EINVAL; 1626 break; 1627 } 1628 sc->wi_channel = ireq->i_val; 1629 break; 1630 case IEEE80211_IOC_POWERSAVE: 1631 switch (ireq->i_val) { 1632 case IEEE80211_POWERSAVE_OFF: 1633 sc->wi_pm_enabled = 0; 1634 break; 1635 case IEEE80211_POWERSAVE_ON: 1636 sc->wi_pm_enabled = 1; 1637 break; 1638 default: 1639 error = EINVAL; 1640 break; 1641 } 1642 break; 1643 case IEEE80211_IOC_POWERSAVESLEEP: 1644 if (ireq->i_val < 0) { 1645 error = EINVAL; 1646 break; 1647 } 1648 sc->wi_max_sleep = ireq->i_val; 1649 break; 1650 default: 1651 error = EINVAL; 1652 break; 1653 } 1654 1655 /* Reinitialize WaveLAN. */ 1656 wi_init(sc); 1657 1658 break; 1659 default: 1660 error = EINVAL; 1661 break; 1662 } 1663out: 1664 WI_UNLOCK(sc); 1665 1666 return(error); 1667} 1668 1669static void wi_init(xsc) 1670 void *xsc; 1671{ 1672 struct wi_softc *sc = xsc; 1673 struct ifnet *ifp = &sc->arpcom.ac_if; 1674 struct wi_ltv_macaddr mac; 1675 int id = 0; 1676 1677 WI_LOCK(sc); 1678 1679 if (sc->wi_gone) { 1680 WI_UNLOCK(sc); 1681 return; 1682 } 1683 1684 if (ifp->if_flags & IFF_RUNNING) 1685 wi_stop(sc); 1686 1687 wi_reset(sc); 1688 1689 /* Program max data length. */ 1690 WI_SETVAL(WI_RID_MAX_DATALEN, sc->wi_max_data_len); 1691 1692 /* Enable/disable IBSS creation. */ 1693 WI_SETVAL(WI_RID_CREATE_IBSS, sc->wi_create_ibss); 1694 1695 /* Set the port type. */ 1696 WI_SETVAL(WI_RID_PORTTYPE, sc->wi_ptype); 1697 1698 /* Program the RTS/CTS threshold. */ 1699 WI_SETVAL(WI_RID_RTS_THRESH, sc->wi_rts_thresh); 1700 1701 /* Program the TX rate */ 1702 WI_SETVAL(WI_RID_TX_RATE, sc->wi_tx_rate); 1703 1704 /* Access point density */ 1705 WI_SETVAL(WI_RID_SYSTEM_SCALE, sc->wi_ap_density); 1706 1707 /* Power Management Enabled */ 1708 WI_SETVAL(WI_RID_PM_ENABLED, sc->wi_pm_enabled); 1709 1710 /* Power Managment Max Sleep */ 1711 WI_SETVAL(WI_RID_MAX_SLEEP, sc->wi_max_sleep); 1712 1713 /* Specify the IBSS name */ 1714 WI_SETSTR(WI_RID_OWN_SSID, sc->wi_ibss_name); 1715 1716 /* Specify the network name */ 1717 WI_SETSTR(WI_RID_DESIRED_SSID, sc->wi_net_name); 1718 1719 /* Specify the frequency to use */ 1720 WI_SETVAL(WI_RID_OWN_CHNL, sc->wi_channel); 1721 1722 /* Program the nodename. */ 1723 WI_SETSTR(WI_RID_NODENAME, sc->wi_node_name); 1724 1725 /* Set our MAC address. */ 1726 mac.wi_len = 4; 1727 mac.wi_type = WI_RID_MAC_NODE; 1728 bcopy((char *)&sc->arpcom.ac_enaddr, 1729 (char *)&mac.wi_mac_addr, ETHER_ADDR_LEN); 1730 wi_write_record(sc, (struct wi_ltv_gen *)&mac); 1731 1732 /* Configure WEP. */ 1733 if (sc->wi_has_wep) { 1734 WI_SETVAL(WI_RID_ENCRYPTION, sc->wi_use_wep); 1735 WI_SETVAL(WI_RID_TX_CRYPT_KEY, sc->wi_tx_key); 1736 sc->wi_keys.wi_len = (sizeof(struct wi_ltv_keys) / 2) + 1; 1737 sc->wi_keys.wi_type = WI_RID_DEFLT_CRYPT_KEYS; 1738 wi_write_record(sc, (struct wi_ltv_gen *)&sc->wi_keys); 1739 } 1740 1741 /* Initialize promisc mode. */ 1742 if (ifp->if_flags & IFF_PROMISC) { 1743 WI_SETVAL(WI_RID_PROMISC, 1); 1744 } else { 1745 WI_SETVAL(WI_RID_PROMISC, 0); 1746 } 1747 1748 /* Set multicast filter. */ 1749 wi_setmulti(sc); 1750 1751 /* Enable desired port */ 1752 wi_cmd(sc, WI_CMD_ENABLE|sc->wi_portnum, 0); 1753 1754 if (wi_alloc_nicmem(sc, ETHER_MAX_LEN + sizeof(struct wi_frame) + 8, &id)) 1755 device_printf(sc->dev, "tx buffer allocation failed\n"); 1756 sc->wi_tx_data_id = id; 1757 1758 if (wi_alloc_nicmem(sc, ETHER_MAX_LEN + sizeof(struct wi_frame) + 8, &id)) 1759 device_printf(sc->dev, "mgmt. buffer allocation failed\n"); 1760 sc->wi_tx_mgmt_id = id; 1761 1762 /* enable interrupts */ 1763 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 1764 1765 ifp->if_flags |= IFF_RUNNING; 1766 ifp->if_flags &= ~IFF_OACTIVE; 1767 1768 sc->wi_stat_ch = timeout(wi_inquire, sc, hz * 60); 1769 WI_UNLOCK(sc); 1770 1771 return; 1772} 1773 1774static void wi_start(ifp) 1775 struct ifnet *ifp; 1776{ 1777 struct wi_softc *sc; 1778 struct mbuf *m0; 1779 struct wi_frame tx_frame; 1780 struct ether_header *eh; 1781 int id; 1782 1783 sc = ifp->if_softc; 1784 WI_LOCK(sc); 1785 1786 if (sc->wi_gone) { 1787 WI_UNLOCK(sc); 1788 return; 1789 } 1790 1791 if (ifp->if_flags & IFF_OACTIVE) { 1792 WI_UNLOCK(sc); 1793 return; 1794 } 1795 1796 IF_DEQUEUE(&ifp->if_snd, m0); 1797 if (m0 == NULL) { 1798 WI_UNLOCK(sc); 1799 return; 1800 } 1801 1802 bzero((char *)&tx_frame, sizeof(tx_frame)); 1803 id = sc->wi_tx_data_id; 1804 eh = mtod(m0, struct ether_header *); 1805 1806 /* 1807 * Use RFC1042 encoding for IP and ARP datagrams, 1808 * 802.3 for anything else. 1809 */ 1810 if (ntohs(eh->ether_type) > ETHER_MAX_LEN) { 1811 bcopy((char *)&eh->ether_dhost, 1812 (char *)&tx_frame.wi_addr1, ETHER_ADDR_LEN); 1813 bcopy((char *)&eh->ether_shost, 1814 (char *)&tx_frame.wi_addr2, ETHER_ADDR_LEN); 1815 bcopy((char *)&eh->ether_dhost, 1816 (char *)&tx_frame.wi_dst_addr, ETHER_ADDR_LEN); 1817 bcopy((char *)&eh->ether_shost, 1818 (char *)&tx_frame.wi_src_addr, ETHER_ADDR_LEN); 1819 1820 tx_frame.wi_dat_len = m0->m_pkthdr.len - WI_SNAPHDR_LEN; 1821 tx_frame.wi_frame_ctl = WI_FTYPE_DATA; 1822 tx_frame.wi_dat[0] = htons(WI_SNAP_WORD0); 1823 tx_frame.wi_dat[1] = htons(WI_SNAP_WORD1); 1824 tx_frame.wi_len = htons(m0->m_pkthdr.len - WI_SNAPHDR_LEN); 1825 tx_frame.wi_type = eh->ether_type; 1826 1827 m_copydata(m0, sizeof(struct ether_header), 1828 m0->m_pkthdr.len - sizeof(struct ether_header), 1829 (caddr_t)&sc->wi_txbuf); 1830 1831 wi_write_data(sc, id, 0, (caddr_t)&tx_frame, 1832 sizeof(struct wi_frame)); 1833 wi_write_data(sc, id, WI_802_11_OFFSET, (caddr_t)&sc->wi_txbuf, 1834 (m0->m_pkthdr.len - sizeof(struct ether_header)) + 2); 1835 } else { 1836 tx_frame.wi_dat_len = m0->m_pkthdr.len; 1837 1838 eh->ether_type = htons(m0->m_pkthdr.len - WI_SNAPHDR_LEN); 1839 m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&sc->wi_txbuf); 1840 1841 wi_write_data(sc, id, 0, (caddr_t)&tx_frame, 1842 sizeof(struct wi_frame)); 1843 wi_write_data(sc, id, WI_802_3_OFFSET, (caddr_t)&sc->wi_txbuf, 1844 m0->m_pkthdr.len + 2); 1845 } 1846 1847 /* 1848 * If there's a BPF listner, bounce a copy of 1849 * this frame to him. 1850 */ 1851 if (ifp->if_bpf) 1852 bpf_mtap(ifp, m0); 1853 1854 m_freem(m0); 1855 1856 if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id)) 1857 device_printf(sc->dev, "xmit failed\n"); 1858 1859 ifp->if_flags |= IFF_OACTIVE; 1860 1861 /* 1862 * Set a timeout in case the chip goes out to lunch. 1863 */ 1864 ifp->if_timer = 5; 1865 1866 WI_UNLOCK(sc); 1867 return; 1868} 1869 1870static int wi_mgmt_xmit(sc, data, len) 1871 struct wi_softc *sc; 1872 caddr_t data; 1873 int len; 1874{ 1875 struct wi_frame tx_frame; 1876 int id; 1877 struct wi_80211_hdr *hdr; 1878 caddr_t dptr; 1879 1880 if (sc->wi_gone) 1881 return(ENODEV); 1882 1883 hdr = (struct wi_80211_hdr *)data; 1884 dptr = data + sizeof(struct wi_80211_hdr); 1885 1886 bzero((char *)&tx_frame, sizeof(tx_frame)); 1887 id = sc->wi_tx_mgmt_id; 1888 1889 bcopy((char *)hdr, (char *)&tx_frame.wi_frame_ctl, 1890 sizeof(struct wi_80211_hdr)); 1891 1892 tx_frame.wi_dat_len = len - WI_SNAPHDR_LEN; 1893 tx_frame.wi_len = htons(len - WI_SNAPHDR_LEN); 1894 1895 wi_write_data(sc, id, 0, (caddr_t)&tx_frame, sizeof(struct wi_frame)); 1896 wi_write_data(sc, id, WI_802_11_OFFSET_RAW, dptr, 1897 (len - sizeof(struct wi_80211_hdr)) + 2); 1898 1899 if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id)) { 1900 device_printf(sc->dev, "xmit failed\n"); 1901 return(EIO); 1902 } 1903 1904 return(0); 1905} 1906 1907static void wi_stop(sc) 1908 struct wi_softc *sc; 1909{ 1910 struct ifnet *ifp; 1911 1912 WI_LOCK(sc); 1913 1914 if (sc->wi_gone) { 1915 WI_UNLOCK(sc); 1916 return; 1917 } 1918 1919 ifp = &sc->arpcom.ac_if; 1920 1921 /* 1922 * If the card is gone and the memory port isn't mapped, we will 1923 * (hopefully) get 0xffff back from the status read, which is not 1924 * a valid status value. 1925 */ 1926 if (CSR_READ_2(sc, WI_STATUS) != 0xffff) { 1927 CSR_WRITE_2(sc, WI_INT_EN, 0); 1928 wi_cmd(sc, WI_CMD_DISABLE|sc->wi_portnum, 0); 1929 } 1930 1931 untimeout(wi_inquire, sc, sc->wi_stat_ch); 1932 1933 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE); 1934 1935 WI_UNLOCK(sc); 1936 return; 1937} 1938 1939static void wi_watchdog(ifp) 1940 struct ifnet *ifp; 1941{ 1942 struct wi_softc *sc; 1943 1944 sc = ifp->if_softc; 1945 1946 device_printf(sc->dev, "watchdog timeout\n"); 1947 1948 wi_init(sc); 1949 1950 ifp->if_oerrors++; 1951 1952 return; 1953} 1954 1955static int 1956wi_alloc(dev, io_rid) 1957 device_t dev; 1958 int io_rid; 1959{ 1960 struct wi_softc *sc = device_get_softc(dev); 1961 1962 sc->iobase_rid = io_rid; 1963 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->iobase_rid, 1964 0, ~0, (1 << 6), 1965 rman_make_alignment_flags(1 << 6) | RF_ACTIVE); 1966 if (!sc->iobase) { 1967 device_printf(dev, "No I/O space?!\n"); 1968 return (ENXIO); 1969 } 1970 1971 sc->irq_rid = 0; 1972 sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid, 1973 0, ~0, 1, RF_ACTIVE); 1974 if (!sc->irq) { 1975 wi_free(dev); 1976 device_printf(dev, "No irq?!\n"); 1977 return (ENXIO); 1978 } 1979 1980 sc->dev = dev; 1981 sc->wi_unit = device_get_unit(dev); 1982 sc->wi_io_addr = rman_get_start(sc->iobase); 1983 sc->wi_btag = rman_get_bustag(sc->iobase); 1984 sc->wi_bhandle = rman_get_bushandle(sc->iobase); 1985 1986 return (0); 1987} 1988 1989static void wi_free(dev) 1990 device_t dev; 1991{ 1992 struct wi_softc *sc = device_get_softc(dev); 1993 1994 if (sc->iobase != NULL) { 1995 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase); 1996 sc->iobase = NULL; 1997 } 1998 if (sc->irq != NULL) { 1999 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 2000 sc->irq = NULL; 2001 } 2002 if (sc->mem != NULL) { 2003 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 2004 sc->mem = NULL; 2005 } 2006 2007 return; 2008} 2009 2010static void wi_shutdown(dev) 2011 device_t dev; 2012{ 2013 struct wi_softc *sc; 2014 2015 sc = device_get_softc(dev); 2016 wi_stop(sc); 2017 2018 return; 2019} 2020 2021#ifdef WICACHE 2022/* wavelan signal strength cache code. 2023 * store signal/noise/quality on per MAC src basis in 2024 * a small fixed cache. The cache wraps if > MAX slots 2025 * used. The cache may be zeroed out to start over. 2026 * Two simple filters exist to reduce computation: 2027 * 1. ip only (literally 0x800) which may be used 2028 * to ignore some packets. It defaults to ip only. 2029 * it could be used to focus on broadcast, non-IP 802.11 beacons. 2030 * 2. multicast/broadcast only. This may be used to 2031 * ignore unicast packets and only cache signal strength 2032 * for multicast/broadcast packets (beacons); e.g., Mobile-IP 2033 * beacons and not unicast traffic. 2034 * 2035 * The cache stores (MAC src(index), IP src (major clue), signal, 2036 * quality, noise) 2037 * 2038 * No apologies for storing IP src here. It's easy and saves much 2039 * trouble elsewhere. The cache is assumed to be INET dependent, 2040 * although it need not be. 2041 */ 2042 2043#ifdef documentation 2044 2045int wi_sigitems; /* number of cached entries */ 2046struct wi_sigcache wi_sigcache[MAXWICACHE]; /* array of cache entries */ 2047int wi_nextitem; /* index/# of entries */ 2048 2049 2050#endif 2051 2052/* control variables for cache filtering. Basic idea is 2053 * to reduce cost (e.g., to only Mobile-IP agent beacons 2054 * which are broadcast or multicast). Still you might 2055 * want to measure signal strength with unicast ping packets 2056 * on a pt. to pt. ant. setup. 2057 */ 2058/* set true if you want to limit cache items to broadcast/mcast 2059 * only packets (not unicast). Useful for mobile-ip beacons which 2060 * are broadcast/multicast at network layer. Default is all packets 2061 * so ping/unicast will work say with pt. to pt. antennae setup. 2062 */ 2063static int wi_cache_mcastonly = 0; 2064SYSCTL_INT(_machdep, OID_AUTO, wi_cache_mcastonly, CTLFLAG_RW, 2065 &wi_cache_mcastonly, 0, ""); 2066 2067/* set true if you want to limit cache items to IP packets only 2068*/ 2069static int wi_cache_iponly = 1; 2070SYSCTL_INT(_machdep, OID_AUTO, wi_cache_iponly, CTLFLAG_RW, 2071 &wi_cache_iponly, 0, ""); 2072 2073/* 2074 * Original comments: 2075 * ----------------- 2076 * wi_cache_store, per rx packet store signal 2077 * strength in MAC (src) indexed cache. 2078 * 2079 * follows linux driver in how signal strength is computed. 2080 * In ad hoc mode, we use the rx_quality field. 2081 * signal and noise are trimmed to fit in the range from 47..138. 2082 * rx_quality field MSB is signal strength. 2083 * rx_quality field LSB is noise. 2084 * "quality" is (signal - noise) as is log value. 2085 * note: quality CAN be negative. 2086 * 2087 * In BSS mode, we use the RID for communication quality. 2088 * TBD: BSS mode is currently untested. 2089 * 2090 * Bill's comments: 2091 * --------------- 2092 * Actually, we use the rx_quality field all the time for both "ad-hoc" 2093 * and BSS modes. Why? Because reading an RID is really, really expensive: 2094 * there's a bunch of PIO operations that have to be done to read a record 2095 * from the NIC, and reading the comms quality RID each time a packet is 2096 * received can really hurt performance. We don't have to do this anyway: 2097 * the comms quality field only reflects the values in the rx_quality field 2098 * anyway. The comms quality RID is only meaningful in infrastructure mode, 2099 * but the values it contains are updated based on the rx_quality from 2100 * frames received from the access point. 2101 * 2102 * Also, according to Lucent, the signal strength and noise level values 2103 * can be converted to dBms by subtracting 149, so I've modified the code 2104 * to do that instead of the scaling it did originally. 2105 */ 2106static 2107void wi_cache_store (struct wi_softc *sc, struct ether_header *eh, 2108 struct mbuf *m, unsigned short rx_quality) 2109{ 2110 struct ip *ip = 0; 2111 int i; 2112 static int cache_slot = 0; /* use this cache entry */ 2113 static int wrapindex = 0; /* next "free" cache entry */ 2114 int sig, noise; 2115 int sawip=0; 2116 2117 /* filters: 2118 * 1. ip only 2119 * 2. configurable filter to throw out unicast packets, 2120 * keep multicast only. 2121 */ 2122 2123 if ((ntohs(eh->ether_type) == ETHERTYPE_IP)) { 2124 sawip = 1; 2125 } 2126 2127 /* filter for ip packets only 2128 */ 2129 if (wi_cache_iponly && !sawip) { 2130 return; 2131 } 2132 2133 /* filter for broadcast/multicast only 2134 */ 2135 if (wi_cache_mcastonly && ((eh->ether_dhost[0] & 1) == 0)) { 2136 return; 2137 } 2138 2139#ifdef SIGDEBUG 2140 printf("wi%d: q value %x (MSB=0x%x, LSB=0x%x) \n", sc->wi_unit, 2141 rx_quality & 0xffff, rx_quality >> 8, rx_quality & 0xff); 2142#endif 2143 2144 /* find the ip header. we want to store the ip_src 2145 * address. 2146 */ 2147 if (sawip) { 2148 ip = mtod(m, struct ip *); 2149 } 2150 2151 /* do a linear search for a matching MAC address 2152 * in the cache table 2153 * . MAC address is 6 bytes, 2154 * . var w_nextitem holds total number of entries already cached 2155 */ 2156 for(i = 0; i < sc->wi_nextitem; i++) { 2157 if (! bcmp(eh->ether_shost , sc->wi_sigcache[i].macsrc, 6 )) { 2158 /* Match!, 2159 * so we already have this entry, 2160 * update the data 2161 */ 2162 break; 2163 } 2164 } 2165 2166 /* did we find a matching mac address? 2167 * if yes, then overwrite a previously existing cache entry 2168 */ 2169 if (i < sc->wi_nextitem ) { 2170 cache_slot = i; 2171 } 2172 /* else, have a new address entry,so 2173 * add this new entry, 2174 * if table full, then we need to replace LRU entry 2175 */ 2176 else { 2177 2178 /* check for space in cache table 2179 * note: wi_nextitem also holds number of entries 2180 * added in the cache table 2181 */ 2182 if ( sc->wi_nextitem < MAXWICACHE ) { 2183 cache_slot = sc->wi_nextitem; 2184 sc->wi_nextitem++; 2185 sc->wi_sigitems = sc->wi_nextitem; 2186 } 2187 /* no space found, so simply wrap with wrap index 2188 * and "zap" the next entry 2189 */ 2190 else { 2191 if (wrapindex == MAXWICACHE) { 2192 wrapindex = 0; 2193 } 2194 cache_slot = wrapindex++; 2195 } 2196 } 2197 2198 /* invariant: cache_slot now points at some slot 2199 * in cache. 2200 */ 2201 if (cache_slot < 0 || cache_slot >= MAXWICACHE) { 2202 log(LOG_ERR, "wi_cache_store, bad index: %d of " 2203 "[0..%d], gross cache error\n", 2204 cache_slot, MAXWICACHE); 2205 return; 2206 } 2207 2208 /* store items in cache 2209 * .ip source address 2210 * .mac src 2211 * .signal, etc. 2212 */ 2213 if (sawip) { 2214 sc->wi_sigcache[cache_slot].ipsrc = ip->ip_src.s_addr; 2215 } 2216 bcopy( eh->ether_shost, sc->wi_sigcache[cache_slot].macsrc, 6); 2217 2218 sig = (rx_quality >> 8) & 0xFF; 2219 noise = rx_quality & 0xFF; 2220 sc->wi_sigcache[cache_slot].signal = sig - 149; 2221 sc->wi_sigcache[cache_slot].noise = noise - 149; 2222 sc->wi_sigcache[cache_slot].quality = sig - noise; 2223 2224 return; 2225} 2226#endif 2227 2228static int wi_get_cur_ssid(sc, ssid, len) 2229 struct wi_softc *sc; 2230 char *ssid; 2231 int *len; 2232{ 2233 int error = 0; 2234 struct wi_req wreq; 2235 2236 wreq.wi_len = WI_MAX_DATALEN; 2237 switch (sc->wi_ptype) { 2238 case WI_PORTTYPE_ADHOC: 2239 wreq.wi_type = WI_RID_CURRENT_SSID; 2240 error = wi_read_record(sc, (struct wi_ltv_gen *)&wreq); 2241 if (error != 0) 2242 break; 2243 if (wreq.wi_val[0] > IEEE80211_NWID_LEN) { 2244 error = EINVAL; 2245 break; 2246 } 2247 *len = wreq.wi_val[0]; 2248 bcopy(&wreq.wi_val[1], ssid, IEEE80211_NWID_LEN); 2249 break; 2250 case WI_PORTTYPE_BSS: 2251 wreq.wi_type = WI_RID_COMMQUAL; 2252 error = wi_read_record(sc, (struct wi_ltv_gen *)&wreq); 2253 if (error != 0) 2254 break; 2255 if (wreq.wi_val[0] != 0) /* associated */ { 2256 wreq.wi_type = WI_RID_CURRENT_SSID; 2257 wreq.wi_len = WI_MAX_DATALEN; 2258 error = wi_read_record(sc, (struct wi_ltv_gen *)&wreq); 2259 if (error != 0) 2260 break; 2261 if (wreq.wi_val[0] > IEEE80211_NWID_LEN) { 2262 error = EINVAL; 2263 break; 2264 } 2265 *len = wreq.wi_val[0]; 2266 bcopy(&wreq.wi_val[1], ssid, IEEE80211_NWID_LEN); 2267 } else { 2268 *len = IEEE80211_NWID_LEN; 2269 bcopy(sc->wi_net_name, ssid, IEEE80211_NWID_LEN); 2270 } 2271 break; 2272 default: 2273 error = EINVAL; 2274 break; 2275 } 2276 2277 return error; 2278} 2279 2280static int wi_media_change(ifp) 2281 struct ifnet *ifp; 2282{ 2283 struct wi_softc *sc = ifp->if_softc; 2284 int otype = sc->wi_ptype; 2285 int orate = sc->wi_tx_rate; 2286 2287 if ((sc->ifmedia.ifm_cur->ifm_media & IFM_IEEE80211_ADHOC) != 0) 2288 sc->wi_ptype = WI_PORTTYPE_ADHOC; 2289 else 2290 sc->wi_ptype = WI_PORTTYPE_BSS; 2291 2292 switch (IFM_SUBTYPE(sc->ifmedia.ifm_cur->ifm_media)) { 2293 case IFM_IEEE80211_DS1: 2294 sc->wi_tx_rate = 1; 2295 break; 2296 case IFM_IEEE80211_DS2: 2297 sc->wi_tx_rate = 2; 2298 break; 2299 case IFM_IEEE80211_DS5: 2300 sc->wi_tx_rate = 5; 2301 break; 2302 case IFM_IEEE80211_DS11: 2303 sc->wi_tx_rate = 11; 2304 break; 2305 case IFM_AUTO: 2306 sc->wi_tx_rate = 3; 2307 break; 2308 } 2309 2310 if (otype != sc->wi_ptype || 2311 orate != sc->wi_tx_rate) 2312 wi_init(sc); 2313 2314 return(0); 2315} 2316 2317static void wi_media_status(ifp, imr) 2318 struct ifnet *ifp; 2319 struct ifmediareq *imr; 2320{ 2321 struct wi_req wreq; 2322 struct wi_softc *sc = ifp->if_softc; 2323 2324 if (sc->wi_tx_rate == 3) { 2325 imr->ifm_active = IFM_IEEE80211|IFM_AUTO; 2326 if (sc->wi_ptype == WI_PORTTYPE_ADHOC) 2327 imr->ifm_active |= IFM_IEEE80211_ADHOC; 2328 wreq.wi_type = WI_RID_CUR_TX_RATE; 2329 wreq.wi_len = WI_MAX_DATALEN; 2330 if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq) == 0) { 2331 switch(wreq.wi_val[0]) { 2332 case 1: 2333 imr->ifm_active |= IFM_IEEE80211_DS1; 2334 break; 2335 case 2: 2336 imr->ifm_active |= IFM_IEEE80211_DS2; 2337 break; 2338 case 6: 2339 imr->ifm_active |= IFM_IEEE80211_DS5; 2340 break; 2341 case 11: 2342 imr->ifm_active |= IFM_IEEE80211_DS11; 2343 break; 2344 } 2345 } 2346 } else { 2347 imr->ifm_active = sc->ifmedia.ifm_cur->ifm_media; 2348 } 2349 2350 imr->ifm_status = IFM_AVALID; 2351 if (sc->wi_ptype == WI_PORTTYPE_ADHOC) 2352 /* 2353 * XXX: It would be nice if we could give some actually 2354 * useful status like whether we joined another IBSS or 2355 * created one ourselves. 2356 */ 2357 imr->ifm_status |= IFM_ACTIVE; 2358 else { 2359 wreq.wi_type = WI_RID_COMMQUAL; 2360 wreq.wi_len = WI_MAX_DATALEN; 2361 if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq) == 0 && 2362 wreq.wi_val[0] != 0) 2363 imr->ifm_status |= IFM_ACTIVE; 2364 } 2365} 2366