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