if_cnw.c revision 1.45
1/* $NetBSD: if_cnw.c,v 1.45 2008/11/07 00:20:12 dyoung Exp $ */ 2 3/*- 4 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Michael Eriksson. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32/* 33 * Copyright (c) 1996, 1997 Berkeley Software Design, Inc. 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that this notice is retained, 38 * the conditions in the following notices are met, and terms applying 39 * to contributors in the following notices also apply to Berkeley 40 * Software Design, Inc. 41 * 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. All advertising materials mentioning features or use of this software 48 * must display the following acknowledgement: 49 * This product includes software developed by 50 * Berkeley Software Design, Inc. 51 * 4. Neither the name of the Berkeley Software Design, Inc. nor the names 52 * of its contributors may be used to endorse or promote products derived 53 * from this software without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND 56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE 59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65 * SUCH DAMAGE. 66 * 67 * Paul Borman, December 1996 68 * 69 * This driver is derived from a generic frame work which is 70 * Copyright(c) 1994,1995,1996 71 * Yoichi Shinoda, Yoshitaka Tokugawa, WIDE Project, Wildboar Project 72 * and Foretune. All rights reserved. 73 * 74 * A linux driver was used as the "hardware reference manual" (i.e., 75 * to determine registers and a general outline of how the card works) 76 * That driver is publically available and copyright 77 * 78 * John Markus Bj�rndalen 79 * Department of Computer Science 80 * University of Troms� 81 * Norway 82 * johnm@staff.cs.uit.no, http://www.cs.uit.no/~johnm/ 83 */ 84 85/* 86 * This is a driver for the Xircom CreditCard Netwave (also known as 87 * the Netwave Airsurfer) wireless LAN PCMCIA adapter. 88 * 89 * When this driver was developed, the Linux Netwave driver was used 90 * as a hardware manual. That driver is Copyright (c) 1997 University 91 * of Troms�, Norway. It is part of the Linux pcmcia-cs package that 92 * can be found at http://pcmcia-cs.sourceforge.net/. The most recent 93 * version of the pcmcia-cs package when this driver was written was 94 * 3.0.6. 95 * 96 * Unfortunately, a lot of explicit numeric constants were used in the 97 * Linux driver. I have tried to use symbolic names whenever possible, 98 * but since I don't have any real hardware documentation, there's 99 * still one or two "magic numbers" :-(. 100 * 101 * Driver limitations: This driver doesn't do multicasting or receiver 102 * promiscuity, because of missing hardware documentation. I couldn't 103 * get receiver promiscuity to work, and I haven't even tried 104 * multicast. Volunteers are welcome, of course :-). 105 */ 106 107#include <sys/cdefs.h> 108__KERNEL_RCSID(0, "$NetBSD: if_cnw.c,v 1.45 2008/11/07 00:20:12 dyoung Exp $"); 109 110#include "opt_inet.h" 111#include "bpfilter.h" 112 113#include <sys/param.h> 114#include <sys/systm.h> 115#include <sys/device.h> 116#include <sys/socket.h> 117#include <sys/mbuf.h> 118#include <sys/ioctl.h> 119#include <sys/proc.h> 120#include <sys/kauth.h> 121 122#include <net/if.h> 123 124#include <dev/pcmcia/if_cnwreg.h> 125#include <dev/pcmcia/if_cnwioctl.h> 126 127#include <dev/pcmcia/pcmciareg.h> 128#include <dev/pcmcia/pcmciavar.h> 129#include <dev/pcmcia/pcmciadevs.h> 130 131#include <net/if_dl.h> 132#include <net/if_ether.h> 133 134#ifdef INET 135#include <netinet/in.h> 136#include <netinet/in_systm.h> 137#include <netinet/in_var.h> 138#include <netinet/ip.h> 139#include <netinet/if_inarp.h> 140#endif 141 142#if NBPFILTER > 0 143#include <net/bpf.h> 144#include <net/bpfdesc.h> 145#endif 146 147/* 148 * Let these be patchable variables, initialized from macros that can 149 * be set in the kernel config file. Someone with lots of spare time 150 * could probably write a nice Netwave configuration program to do 151 * this a little bit more elegantly :-). 152 */ 153#ifndef CNW_DOMAIN 154#define CNW_DOMAIN 0x100 155#endif 156int cnw_domain = CNW_DOMAIN; /* Domain */ 157#ifndef CNW_SCRAMBLEKEY 158#define CNW_SCRAMBLEKEY 0 159#endif 160int cnw_skey = CNW_SCRAMBLEKEY; /* Scramble key */ 161 162/* 163 * The card appears to work much better when we only allow one packet 164 * "in the air" at a time. This is done by not allowing another packet 165 * on the card, even if there is room. Turning this off will allow the 166 * driver to stuff packets on the card as soon as a transmit buffer is 167 * available. This does increase the number of collisions, though. 168 * We can que a second packet if there are transmit buffers available, 169 * but we do not actually send the packet until the last packet has 170 * been written. 171 */ 172#define ONE_AT_A_TIME 173 174/* 175 * Netwave cards choke if we try to use io memory address >= 0x400. 176 * Even though, CIS tuple does not talk about this. 177 * Use memory mapped access. 178 */ 179#define MEMORY_MAPPED 180 181int cnw_match(struct device *, struct cfdata *, void *); 182void cnw_attach(struct device *, struct device *, void *); 183int cnw_detach(struct device *, int); 184 185int cnw_activate(struct device *, enum devact); 186 187struct cnw_softc { 188 struct device sc_dev; /* Device glue (must be first) */ 189 struct ethercom sc_ethercom; /* Ethernet common part */ 190 int sc_domain; /* Netwave domain */ 191 int sc_skey; /* Netwave scramble key */ 192 struct cnwstats sc_stats; 193 194 /* PCMCIA-specific stuff */ 195 struct pcmcia_function *sc_pf; /* PCMCIA function */ 196#ifndef MEMORY_MAPPED 197 struct pcmcia_io_handle sc_pcioh; /* PCMCIA I/O space handle */ 198 int sc_iowin; /* ...window */ 199 bus_space_tag_t sc_iot; /* ...bus_space tag */ 200 bus_space_handle_t sc_ioh; /* ...bus_space handle */ 201#endif 202 struct pcmcia_mem_handle sc_pcmemh; /* PCMCIA memory handle */ 203 bus_size_t sc_memoff; /* ...offset */ 204 int sc_memwin; /* ...window */ 205 bus_space_tag_t sc_memt; /* ...bus_space tag */ 206 bus_space_handle_t sc_memh; /* ...bus_space handle */ 207 void *sc_ih; /* Interrupt cookie */ 208 struct timeval sc_txlast; /* When the last xmit was made */ 209 int sc_active; /* Currently xmitting a packet */ 210 211 int sc_resource; /* Resources alloc'ed on attach */ 212#define CNW_RES_PCIC 1 213#define CNW_RES_IO 2 214#define CNW_RES_MEM 4 215#define CNW_RES_NET 8 216}; 217 218CFATTACH_DECL(cnw, sizeof(struct cnw_softc), 219 cnw_match, cnw_attach, cnw_detach, cnw_activate); 220 221void cnw_reset(struct cnw_softc *); 222void cnw_init(struct cnw_softc *); 223int cnw_enable(struct cnw_softc *sc); 224void cnw_disable(struct cnw_softc *sc); 225void cnw_config(struct cnw_softc *sc, u_int8_t *); 226void cnw_start(struct ifnet *); 227void cnw_transmit(struct cnw_softc *, struct mbuf *); 228struct mbuf *cnw_read(struct cnw_softc *); 229void cnw_recv(struct cnw_softc *); 230int cnw_intr(void *arg); 231int cnw_ioctl(struct ifnet *, u_long, void *); 232void cnw_watchdog(struct ifnet *); 233static int cnw_setdomain(struct cnw_softc *, int); 234static int cnw_setkey(struct cnw_softc *, int); 235 236/* ---------------------------------------------------------------- */ 237 238/* Help routines */ 239static int wait_WOC(struct cnw_softc *, int); 240static int read16(struct cnw_softc *, int); 241static int cnw_cmd(struct cnw_softc *, int, int, int, int); 242 243/* 244 * Wait until the WOC (Write Operation Complete) bit in the 245 * ASR (Adapter Status Register) is asserted. 246 */ 247static int 248wait_WOC(sc, line) 249 struct cnw_softc *sc; 250 int line; 251{ 252 int i, asr; 253 254 for (i = 0; i < 5000; i++) { 255#ifndef MEMORY_MAPPED 256 asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR); 257#else 258 asr = bus_space_read_1(sc->sc_memt, sc->sc_memh, 259 sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR); 260#endif 261 if (asr & CNW_ASR_WOC) 262 return (0); 263 DELAY(100); 264 } 265 if (line > 0) 266 printf("%s: wedged at line %d\n", device_xname(&sc->sc_dev), line); 267 return (1); 268} 269#define WAIT_WOC(sc) wait_WOC(sc, __LINE__) 270 271 272/* 273 * Read a 16 bit value from the card. 274 */ 275static int 276read16(sc, offset) 277 struct cnw_softc *sc; 278 int offset; 279{ 280 int hi, lo; 281 int offs = sc->sc_memoff + offset; 282 283 /* This could presumably be done more efficient with 284 * bus_space_read_2(), but I don't know anything about the 285 * byte sex guarantees... Besides, this is pretty cheap as 286 * well :-) 287 */ 288 lo = bus_space_read_1(sc->sc_memt, sc->sc_memh, offs); 289 hi = bus_space_read_1(sc->sc_memt, sc->sc_memh, offs + 1); 290 return ((hi << 8) | lo); 291} 292 293 294/* 295 * Send a command to the card by writing it to the command buffer. 296 */ 297int 298cnw_cmd(sc, cmd, count, arg1, arg2) 299 struct cnw_softc *sc; 300 int cmd, count, arg1, arg2; 301{ 302 int ptr = sc->sc_memoff + CNW_EREG_CB; 303 304 if (wait_WOC(sc, 0)) { 305 printf("%s: wedged when issuing cmd 0x%x\n", 306 device_xname(&sc->sc_dev), cmd); 307 /* 308 * We'll continue anyway, as that's probably the best 309 * thing we can do; at least the user knows there's a 310 * problem, and can reset the interface with ifconfig 311 * down/up. 312 */ 313 } 314 315 bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr, cmd); 316 if (count > 0) { 317 bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr + 1, arg1); 318 if (count > 1) 319 bus_space_write_1(sc->sc_memt, sc->sc_memh, 320 ptr + 2, arg2); 321 } 322 bus_space_write_1(sc->sc_memt, sc->sc_memh, 323 ptr + count + 1, CNW_CMD_EOC); 324 return (0); 325} 326#define CNW_CMD0(sc, cmd) \ 327 do { cnw_cmd(sc, cmd, 0, 0, 0); } while (0) 328#define CNW_CMD1(sc, cmd, arg1) \ 329 do { cnw_cmd(sc, cmd, 1, arg1 , 0); } while (0) 330#define CNW_CMD2(sc, cmd, arg1, arg2) \ 331 do { cnw_cmd(sc, cmd, 2, arg1, arg2); } while (0) 332 333/* ---------------------------------------------------------------- */ 334 335/* 336 * Reset the hardware. 337 */ 338void 339cnw_reset(sc) 340 struct cnw_softc *sc; 341{ 342#ifdef CNW_DEBUG 343 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG) 344 printf("%s: resetting\n", device_xname(&sc->sc_dev)); 345#endif 346 wait_WOC(sc, 0); 347#ifndef MEMORY_MAPPED 348 bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, CNW_PMR_RESET); 349#else 350 bus_space_write_1(sc->sc_memt, sc->sc_memh, 351 sc->sc_memoff + CNW_IOM_OFF + CNW_REG_PMR, CNW_PMR_RESET); 352#endif 353 bus_space_write_1(sc->sc_memt, sc->sc_memh, 354 sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_WOC); 355#ifndef MEMORY_MAPPED 356 bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, 0); 357#else 358 bus_space_write_1(sc->sc_memt, sc->sc_memh, 359 sc->sc_memoff + CNW_IOM_OFF + CNW_REG_PMR, 0); 360#endif 361} 362 363 364/* 365 * Initialize the card. 366 */ 367void 368cnw_init(sc) 369 struct cnw_softc *sc; 370{ 371 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 372 const u_int8_t rxmode = 373 CNW_RXCONF_RXENA | CNW_RXCONF_BCAST | CNW_RXCONF_AMP; 374 375 /* Reset the card */ 376 cnw_reset(sc); 377 378 /* Issue a NOP to check the card */ 379 CNW_CMD0(sc, CNW_CMD_NOP); 380 381 /* Set up receive configuration */ 382 CNW_CMD1(sc, CNW_CMD_SRC, 383 rxmode | ((ifp->if_flags & IFF_PROMISC) ? CNW_RXCONF_PRO : 0)); 384 385 /* Set up transmit configuration */ 386 CNW_CMD1(sc, CNW_CMD_STC, CNW_TXCONF_TXENA); 387 388 /* Set domain */ 389 CNW_CMD2(sc, CNW_CMD_SMD, sc->sc_domain, sc->sc_domain >> 8); 390 391 /* Set scramble key */ 392 CNW_CMD2(sc, CNW_CMD_SSK, sc->sc_skey, sc->sc_skey >> 8); 393 394 /* Enable interrupts */ 395 WAIT_WOC(sc); 396#ifndef MEMORY_MAPPED 397 bus_space_write_1(sc->sc_iot, sc->sc_ioh, 398 CNW_REG_IMR, CNW_IMR_IENA | CNW_IMR_RFU1); 399#else 400 bus_space_write_1(sc->sc_memt, sc->sc_memh, 401 sc->sc_memoff + CNW_IOM_OFF + CNW_REG_IMR, 402 CNW_IMR_IENA | CNW_IMR_RFU1); 403#endif 404 405 /* Enable receiver */ 406 CNW_CMD0(sc, CNW_CMD_ER); 407 408 /* "Set the IENA bit in COR" */ 409 WAIT_WOC(sc); 410#ifndef MEMORY_MAPPED 411 bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_COR, 412 CNW_COR_IENA | CNW_COR_LVLREQ); 413#else 414 bus_space_write_1(sc->sc_memt, sc->sc_memh, 415 sc->sc_memoff + CNW_IOM_OFF + CNW_REG_COR, 416 CNW_COR_IENA | CNW_COR_LVLREQ); 417#endif 418} 419 420 421/* 422 * Enable and initialize the card. 423 */ 424int 425cnw_enable(sc) 426 struct cnw_softc *sc; 427{ 428 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 429 430 if ((ifp->if_flags & IFF_RUNNING) != 0) 431 return (0); 432 433 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_NET, cnw_intr, sc); 434 if (sc->sc_ih == NULL) { 435 aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt handler\n"); 436 return (EIO); 437 } 438 if (pcmcia_function_enable(sc->sc_pf) != 0) { 439 aprint_error_dev(&sc->sc_dev, "couldn't enable card\n"); 440 return (EIO); 441 } 442 sc->sc_resource |= CNW_RES_PCIC; 443 cnw_init(sc); 444 ifp->if_flags &= ~IFF_OACTIVE; 445 ifp->if_flags |= IFF_RUNNING; 446 return (0); 447} 448 449 450/* 451 * Stop and disable the card. 452 */ 453void 454cnw_disable(sc) 455 struct cnw_softc *sc; 456{ 457 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 458 459 if ((ifp->if_flags & IFF_RUNNING) == 0) 460 return; 461 462 pcmcia_function_disable(sc->sc_pf); 463 sc->sc_resource &= ~CNW_RES_PCIC; 464 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih); 465 ifp->if_flags &= ~IFF_RUNNING; 466 ifp->if_timer = 0; 467} 468 469 470/* 471 * Match the hardware we handle. 472 */ 473int 474cnw_match(struct device *parent, struct cfdata *match, 475 void *aux) 476{ 477 struct pcmcia_attach_args *pa = aux; 478 479 if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM && 480 pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_801) 481 return 1; 482 if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM && 483 pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_802) 484 return 1; 485 return 0; 486} 487 488 489/* 490 * Attach the card. 491 */ 492void 493cnw_attach(struct device *parent, struct device *self, void *aux) 494{ 495 struct cnw_softc *sc = (void *) self; 496 struct pcmcia_attach_args *pa = aux; 497 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 498 u_int8_t macaddr[ETHER_ADDR_LEN]; 499 int i; 500 bus_size_t memsize; 501 502 sc->sc_resource = 0; 503 504 /* Enable the card */ 505 sc->sc_pf = pa->pf; 506 pcmcia_function_init(sc->sc_pf, SIMPLEQ_FIRST(&sc->sc_pf->cfe_head)); 507 if (pcmcia_function_enable(sc->sc_pf)) { 508 aprint_error_dev(self, "function enable failed\n"); 509 return; 510 } 511 sc->sc_resource |= CNW_RES_PCIC; 512 513 /* Map I/O register and "memory" */ 514#ifndef MEMORY_MAPPED 515 if (pcmcia_io_alloc(sc->sc_pf, 0, CNW_IO_SIZE, CNW_IO_SIZE, 516 &sc->sc_pcioh) != 0) { 517 aprint_error_dev(self, "can't allocate i/o space\n"); 518 goto fail; 519 } 520 if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_IO16, &sc->sc_pcioh, 521 &sc->sc_iowin) != 0) { 522 aprint_error_dev(self, "can't map i/o space\n"); 523 pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh); 524 goto fail; 525 } 526 sc->sc_iot = sc->sc_pcioh.iot; 527 sc->sc_ioh = sc->sc_pcioh.ioh; 528 sc->sc_resource |= CNW_RES_IO; 529#endif 530#ifndef MEMORY_MAPPED 531 memsize = CNW_MEM_SIZE; 532#else 533 memsize = CNW_MEM_SIZE + CNW_IOM_SIZE; 534#endif 535 if (pcmcia_mem_alloc(sc->sc_pf, memsize, &sc->sc_pcmemh) != 0) { 536 aprint_error_dev(self, "can't allocate memory\n"); 537 goto fail; 538 } 539 if (pcmcia_mem_map(sc->sc_pf, PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON, 540 CNW_MEM_ADDR, memsize, &sc->sc_pcmemh, &sc->sc_memoff, 541 &sc->sc_memwin) != 0) { 542 aprint_error_dev(self, "can't map memory\n"); 543 pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh); 544 goto fail; 545 } 546 sc->sc_memt = sc->sc_pcmemh.memt; 547 sc->sc_memh = sc->sc_pcmemh.memh; 548 sc->sc_resource |= CNW_RES_MEM; 549 550 /* Finish setup of softc */ 551 sc->sc_domain = cnw_domain; 552 sc->sc_skey = cnw_skey; 553 554 /* Get MAC address */ 555 cnw_reset(sc); 556 for (i = 0; i < ETHER_ADDR_LEN; i++) 557 macaddr[i] = bus_space_read_1(sc->sc_memt, sc->sc_memh, 558 sc->sc_memoff + CNW_EREG_PA + i); 559 printf("%s: address %s\n", device_xname(&sc->sc_dev), 560 ether_sprintf(macaddr)); 561 562 /* Set up ifnet structure */ 563 strlcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ); 564 ifp->if_softc = sc; 565 ifp->if_start = cnw_start; 566 ifp->if_ioctl = cnw_ioctl; 567 ifp->if_watchdog = cnw_watchdog; 568 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | 569 IFF_NOTRAILERS; 570 IFQ_SET_READY(&ifp->if_snd); 571 572 /* Attach the interface */ 573 if_attach(ifp); 574 ether_ifattach(ifp, macaddr); 575 576 sc->sc_resource |= CNW_RES_NET; 577 578 ifp->if_baudrate = IF_Mbps(1); 579 580 /* Disable the card now, and turn it on when the interface goes up */ 581 pcmcia_function_disable(sc->sc_pf); 582 sc->sc_resource &= ~CNW_RES_PCIC; 583 return; 584 585fail: 586#ifndef MEMORY_MAPPED 587 if ((sc->sc_resource & CNW_RES_IO) != 0) { 588 pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin); 589 pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh); 590 sc->sc_resource &= ~CNW_RES_IO; 591 } 592#endif 593 if ((sc->sc_resource & CNW_RES_PCIC) != 0) { 594 pcmcia_function_disable(sc->sc_pf); 595 sc->sc_resource &= ~CNW_RES_PCIC; 596 } 597} 598 599/* 600 * Start outputting on the interface. 601 */ 602void 603cnw_start(ifp) 604 struct ifnet *ifp; 605{ 606 struct cnw_softc *sc = ifp->if_softc; 607 struct mbuf *m0; 608 int lif; 609 int asr; 610#ifdef ONE_AT_A_TIME 611 struct timeval now; 612#endif 613 614#ifdef CNW_DEBUG 615 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG) 616 printf("%s: cnw_start\n", ifp->if_xname); 617 if (ifp->if_flags & IFF_OACTIVE) 618 printf("%s: cnw_start reentered\n", ifp->if_xname); 619#endif 620 621 ifp->if_flags |= IFF_OACTIVE; 622 623 for (;;) { 624#ifdef ONE_AT_A_TIME 625 microtime(&now); 626 now.tv_sec -= sc->sc_txlast.tv_sec; 627 now.tv_usec -= sc->sc_txlast.tv_usec; 628 if (now.tv_usec < 0) { 629 now.tv_usec += 1000000; 630 now.tv_sec--; 631 } 632 633 /* 634 * Don't ship this packet out until the last 635 * packet has left the building. 636 * If we have not tried to send a packet for 1/5 637 * a second then we assume we lost an interrupt, 638 * lets go on and send the next packet anyhow. 639 * 640 * I suppose we could check to see if it is okay 641 * to put additional packets on the card (beyond 642 * the one already waiting to be sent) but I don't 643 * think we would get any improvement in speed as 644 * we should have ample time to put the next packet 645 * on while this one is going out. 646 */ 647 if (sc->sc_active && now.tv_sec == 0 && now.tv_usec < 200000) 648 break; 649#endif 650 651 /* Make sure the link integrity field is on */ 652 WAIT_WOC(sc); 653 lif = bus_space_read_1(sc->sc_memt, sc->sc_memh, 654 sc->sc_memoff + CNW_EREG_LIF); 655 if (lif == 0) { 656#ifdef CNW_DEBUG 657 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG) 658 printf("%s: link integrity %d\n", ifp->if_xname, lif); 659#endif 660 break; 661 } 662 663 /* Is there any buffer space available on the card? */ 664 WAIT_WOC(sc); 665#ifndef MEMORY_MAPPED 666 asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR); 667#else 668 asr = bus_space_read_1(sc->sc_memt, sc->sc_memh, 669 sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR); 670#endif 671 if (!(asr & CNW_ASR_TXBA)) { 672#ifdef CNW_DEBUG 673 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG) 674 printf("%s: no buffer space\n", ifp->if_xname); 675#endif 676 break; 677 } 678 679 sc->sc_stats.nws_tx++; 680 681 IFQ_DEQUEUE(&ifp->if_snd, m0); 682 if (m0 == 0) 683 break; 684 685#if NBPFILTER > 0 686 if (ifp->if_bpf) 687 bpf_mtap(ifp->if_bpf, m0); 688#endif 689 690 cnw_transmit(sc, m0); 691 ++ifp->if_opackets; 692 ifp->if_timer = 3; /* start watchdog timer */ 693 694 microtime(&sc->sc_txlast); 695 sc->sc_active = 1; 696 } 697 698 ifp->if_flags &= ~IFF_OACTIVE; 699} 700 701/* 702 * Transmit a packet. 703 */ 704void 705cnw_transmit(sc, m0) 706 struct cnw_softc *sc; 707 struct mbuf *m0; 708{ 709 int buffer, bufsize, bufoffset, bufptr, bufspace, len, mbytes, n; 710 struct mbuf *m; 711 u_int8_t *mptr; 712 713 /* Get buffer info from card */ 714 buffer = read16(sc, CNW_EREG_TDP); 715 bufsize = read16(sc, CNW_EREG_TDP + 2); 716 bufoffset = read16(sc, CNW_EREG_TDP + 4); 717#ifdef CNW_DEBUG 718 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG) 719 printf("%s: cnw_transmit b=0x%x s=%d o=0x%x\n", 720 device_xname(&sc->sc_dev), buffer, bufsize, bufoffset); 721#endif 722 723 /* Copy data from mbuf chain to card buffers */ 724 bufptr = sc->sc_memoff + buffer + bufoffset; 725 bufspace = bufsize; 726 len = 0; 727 for (m = m0; m; ) { 728 mptr = mtod(m, u_int8_t *); 729 mbytes = m->m_len; 730 len += mbytes; 731 while (mbytes > 0) { 732 if (bufspace == 0) { 733 buffer = read16(sc, buffer); 734 bufptr = sc->sc_memoff + buffer + bufoffset; 735 bufspace = bufsize; 736#ifdef CNW_DEBUG 737 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG) 738 printf("%s: next buffer @0x%x\n", 739 device_xname(&sc->sc_dev), buffer); 740#endif 741 } 742 n = mbytes <= bufspace ? mbytes : bufspace; 743 bus_space_write_region_1(sc->sc_memt, sc->sc_memh, 744 bufptr, mptr, n); 745 bufptr += n; 746 bufspace -= n; 747 mptr += n; 748 mbytes -= n; 749 } 750 MFREE(m, m0); 751 m = m0; 752 } 753 754 /* Issue transmit command */ 755 CNW_CMD2(sc, CNW_CMD_TL, len, len >> 8); 756} 757 758 759/* 760 * Pull a packet from the card into an mbuf chain. 761 */ 762struct mbuf * 763cnw_read(sc) 764 struct cnw_softc *sc; 765{ 766 struct mbuf *m, *top, **mp; 767 int totbytes, buffer, bufbytes, bufptr, mbytes, n; 768 u_int8_t *mptr; 769 770 WAIT_WOC(sc); 771 totbytes = read16(sc, CNW_EREG_RDP); 772#ifdef CNW_DEBUG 773 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG) 774 printf("%s: recv %d bytes\n", device_xname(&sc->sc_dev), totbytes); 775#endif 776 buffer = CNW_EREG_RDP + 2; 777 bufbytes = 0; 778 bufptr = 0; /* XXX make gcc happy */ 779 780 MGETHDR(m, M_DONTWAIT, MT_DATA); 781 if (m == 0) 782 return (0); 783 m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if; 784 m->m_pkthdr.len = totbytes; 785 mbytes = MHLEN; 786 top = 0; 787 mp = ⊤ 788 789 while (totbytes > 0) { 790 if (top) { 791 MGET(m, M_DONTWAIT, MT_DATA); 792 if (m == 0) { 793 m_freem(top); 794 return (0); 795 } 796 mbytes = MLEN; 797 } 798 if (totbytes >= MINCLSIZE) { 799 MCLGET(m, M_DONTWAIT); 800 if ((m->m_flags & M_EXT) == 0) { 801 m_free(m); 802 m_freem(top); 803 return (0); 804 } 805 mbytes = MCLBYTES; 806 } 807 if (!top) { 808 int pad = ALIGN(sizeof(struct ether_header)) - 809 sizeof(struct ether_header); 810 m->m_data += pad; 811 mbytes -= pad; 812 } 813 mptr = mtod(m, u_int8_t *); 814 mbytes = m->m_len = min(totbytes, mbytes); 815 totbytes -= mbytes; 816 while (mbytes > 0) { 817 if (bufbytes == 0) { 818 buffer = read16(sc, buffer); 819 bufbytes = read16(sc, buffer + 2); 820 bufptr = sc->sc_memoff + buffer + 821 read16(sc, buffer + 4); 822#ifdef CNW_DEBUG 823 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG) 824 printf("%s: %d bytes @0x%x+0x%lx\n", 825 device_xname(&sc->sc_dev), bufbytes, 826 buffer, bufptr - buffer - 827 sc->sc_memoff); 828#endif 829 } 830 n = mbytes <= bufbytes ? mbytes : bufbytes; 831 bus_space_read_region_1(sc->sc_memt, sc->sc_memh, 832 bufptr, mptr, n); 833 bufbytes -= n; 834 bufptr += n; 835 mbytes -= n; 836 mptr += n; 837 } 838 *mp = m; 839 mp = &m->m_next; 840 } 841 842 return (top); 843} 844 845 846/* 847 * Handle received packets. 848 */ 849void 850cnw_recv(sc) 851 struct cnw_softc *sc; 852{ 853 int rser; 854 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 855 struct mbuf *m; 856 857 for (;;) { 858 WAIT_WOC(sc); 859 rser = bus_space_read_1(sc->sc_memt, sc->sc_memh, 860 sc->sc_memoff + CNW_EREG_RSER); 861 if (!(rser & CNW_RSER_RXAVAIL)) 862 return; 863 864 /* Pull packet off card */ 865 m = cnw_read(sc); 866 867 /* Acknowledge packet */ 868 CNW_CMD0(sc, CNW_CMD_SRP); 869 870 /* Did we manage to get the packet from the interface? */ 871 if (m == 0) { 872 ++ifp->if_ierrors; 873 return; 874 } 875 ++ifp->if_ipackets; 876 877#if NBPFILTER > 0 878 if (ifp->if_bpf) 879 bpf_mtap(ifp->if_bpf, m); 880#endif 881 882 /* Pass the packet up. */ 883 (*ifp->if_input)(ifp, m); 884 } 885} 886 887 888/* 889 * Interrupt handler. 890 */ 891int 892cnw_intr(arg) 893 void *arg; 894{ 895 struct cnw_softc *sc = arg; 896 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 897 int ret, status, rser, tser; 898 899 if ((sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING) == 0 || 900 !device_is_active(&sc->sc_dev)) 901 return (0); 902 ifp->if_timer = 0; /* stop watchdog timer */ 903 904 ret = 0; 905 for (;;) { 906 WAIT_WOC(sc); 907#ifndef MEMORY_MAPPED 908 status = bus_space_read_1(sc->sc_iot, sc->sc_ioh, 909 CNW_REG_CCSR); 910#else 911 status = bus_space_read_1(sc->sc_memt, sc->sc_memh, 912 sc->sc_memoff + CNW_IOM_OFF + CNW_REG_CCSR); 913#endif 914 if (!(status & 0x02)) { 915 if (ret == 0) 916 printf("%s: spurious interrupt\n", 917 device_xname(&sc->sc_dev)); 918 return (ret); 919 } 920 ret = 1; 921#ifndef MEMORY_MAPPED 922 status = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR); 923#else 924 status = bus_space_read_1(sc->sc_memt, sc->sc_memh, 925 sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR); 926#endif 927 928 /* Anything to receive? */ 929 if (status & CNW_ASR_RXRDY) { 930 sc->sc_stats.nws_rx++; 931 cnw_recv(sc); 932 } 933 934 /* Receive error */ 935 if (status & CNW_ASR_RXERR) { 936 /* 937 * I get a *lot* of spurious receive errors 938 * (many per second), even when the interface 939 * is quiescent, so we don't increment 940 * if_ierrors here. 941 */ 942 rser = bus_space_read_1(sc->sc_memt, sc->sc_memh, 943 sc->sc_memoff + CNW_EREG_RSER); 944 945 /* RX statistics */ 946 sc->sc_stats.nws_rxerr++; 947 if (rser & CNW_RSER_RXBIG) 948 sc->sc_stats.nws_rxframe++; 949 if (rser & CNW_RSER_RXCRC) 950 sc->sc_stats.nws_rxcrcerror++; 951 if (rser & CNW_RSER_RXOVERRUN) 952 sc->sc_stats.nws_rxoverrun++; 953 if (rser & CNW_RSER_RXOVERFLOW) 954 sc->sc_stats.nws_rxoverflow++; 955 if (rser & CNW_RSER_RXERR) 956 sc->sc_stats.nws_rxerrors++; 957 if (rser & CNW_RSER_RXAVAIL) 958 sc->sc_stats.nws_rxavail++; 959 960 /* Clear error bits in RSER */ 961 WAIT_WOC(sc); 962 bus_space_write_1(sc->sc_memt, sc->sc_memh, 963 sc->sc_memoff + CNW_EREG_RSERW, 964 CNW_RSER_RXERR | 965 (rser & (CNW_RSER_RXCRC | CNW_RSER_RXBIG))); 966 /* Clear RXERR in ASR */ 967 WAIT_WOC(sc); 968 bus_space_write_1(sc->sc_memt, sc->sc_memh, 969 sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_RXERR); 970 } 971 972 /* Transmit done */ 973 if (status & CNW_ASR_TXDN) { 974 tser = bus_space_read_1(sc->sc_memt, sc->sc_memh, 975 CNW_EREG_TSER); 976 977 /* TX statistics */ 978 if (tser & CNW_TSER_TXERR) 979 sc->sc_stats.nws_txerrors++; 980 if (tser & CNW_TSER_TXNOAP) 981 sc->sc_stats.nws_txlostcd++; 982 if (tser & CNW_TSER_TXGU) 983 sc->sc_stats.nws_txabort++; 984 985 if (tser & CNW_TSER_TXOK) { 986 sc->sc_stats.nws_txokay++; 987 sc->sc_stats.nws_txretries[status & 0xf]++; 988 WAIT_WOC(sc); 989 bus_space_write_1(sc->sc_memt, sc->sc_memh, 990 sc->sc_memoff + CNW_EREG_TSERW, 991 CNW_TSER_TXOK | CNW_TSER_RTRY); 992 } 993 994 if (tser & CNW_TSER_ERROR) { 995 ++ifp->if_oerrors; 996 WAIT_WOC(sc); 997 bus_space_write_1(sc->sc_memt, sc->sc_memh, 998 sc->sc_memoff + CNW_EREG_TSERW, 999 (tser & CNW_TSER_ERROR) | 1000 CNW_TSER_RTRY); 1001 } 1002 1003 sc->sc_active = 0; 1004 ifp->if_flags &= ~IFF_OACTIVE; 1005 1006 /* Continue to send packets from the queue */ 1007 cnw_start(&sc->sc_ethercom.ec_if); 1008 } 1009 1010 } 1011} 1012 1013 1014/* 1015 * Handle device ioctls. 1016 */ 1017int 1018cnw_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1019{ 1020 struct cnw_softc *sc = ifp->if_softc; 1021 struct ifaddr *ifa = (struct ifaddr *)data; 1022 struct ifreq *ifr = (struct ifreq *)data; 1023 int s, error = 0; 1024 struct lwp *l = curlwp; /*XXX*/ 1025 1026 switch (cmd) { 1027 case SIOCINITIFADDR: 1028 case SIOCSIFFLAGS: 1029 case SIOCADDMULTI: 1030 case SIOCDELMULTI: 1031 case SIOCGCNWDOMAIN: 1032 case SIOCGCNWSTATS: 1033 break; 1034 case SIOCSCNWDOMAIN: 1035 case SIOCSCNWKEY: 1036 case SIOCGCNWSTATUS: 1037 error = kauth_authorize_generic(l->l_cred, 1038 KAUTH_GENERIC_ISSUSER, NULL); 1039 if (error) 1040 return (error); 1041 break; 1042 default: 1043 return (EINVAL); 1044 } 1045 1046 s = splnet(); 1047 1048 switch (cmd) { 1049 1050 case SIOCINITIFADDR: 1051 if (!(ifp->if_flags & IFF_RUNNING) && 1052 (error = cnw_enable(sc)) != 0) 1053 break; 1054 ifp->if_flags |= IFF_UP; 1055 cnw_init(sc); 1056 switch (ifa->ifa_addr->sa_family) { 1057#ifdef INET 1058 case AF_INET: 1059 arp_ifinit(&sc->sc_ethercom.ec_if, ifa); 1060 break; 1061#endif 1062 default: 1063 break; 1064 } 1065 break; 1066 1067 case SIOCSIFFLAGS: 1068 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 1069 break; 1070 /* XXX re-use ether_ioctl() */ 1071 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { 1072 case IFF_RUNNING: 1073 /* 1074 * The interface is marked down and it is running, so 1075 * stop it. 1076 */ 1077 cnw_disable(sc); 1078 break; 1079 case IFF_UP: 1080 /* 1081 * The interface is marked up and it is stopped, so 1082 * start it. 1083 */ 1084 error = cnw_enable(sc); 1085 break; 1086 default: 1087 /* IFF_PROMISC may be changed */ 1088 cnw_init(sc); 1089 break; 1090 } 1091 break; 1092 1093 case SIOCADDMULTI: 1094 case SIOCDELMULTI: 1095 /* Update our multicast list. */ 1096 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) { 1097 if (ifp->if_flags & IFF_RUNNING) 1098 cnw_init(sc); 1099 error = 0; 1100 } 1101 break; 1102 1103 case SIOCGCNWDOMAIN: 1104 ifr->ifr_domain = sc->sc_domain; 1105 break; 1106 1107 case SIOCSCNWDOMAIN: 1108 error = cnw_setdomain(sc, ifr->ifr_domain); 1109 break; 1110 1111 case SIOCSCNWKEY: 1112 error = cnw_setkey(sc, ifr->ifr_key); 1113 break; 1114 1115 case SIOCGCNWSTATUS: 1116 if ((ifp->if_flags & IFF_RUNNING) == 0) 1117 break; 1118 bus_space_read_region_1(sc->sc_memt, sc->sc_memh, 1119 sc->sc_memoff + CNW_EREG_CB, 1120 ((struct cnwstatus *)data)->data, 1121 sizeof(((struct cnwstatus *)data)->data)); 1122 break; 1123 1124 case SIOCGCNWSTATS: 1125 memcpy((void *)&(((struct cnwistats *)data)->stats), 1126 (void *)&sc->sc_stats, sizeof(struct cnwstats)); 1127 break; 1128 1129 default: 1130 error = ether_ioctl(ifp, cmd, data); 1131 break; 1132 } 1133 1134 splx(s); 1135 return (error); 1136} 1137 1138 1139/* 1140 * Device timeout/watchdog routine. Entered if the device neglects to 1141 * generate an interrupt after a transmit has been started on it. 1142 */ 1143void 1144cnw_watchdog(ifp) 1145 struct ifnet *ifp; 1146{ 1147 struct cnw_softc *sc = ifp->if_softc; 1148 1149 printf("%s: device timeout; card reset\n", device_xname(&sc->sc_dev)); 1150 ++ifp->if_oerrors; 1151 cnw_init(sc); 1152} 1153 1154int 1155cnw_setdomain(sc, domain) 1156 struct cnw_softc *sc; 1157 int domain; 1158{ 1159 int s; 1160 1161 if (domain & ~0x1ff) 1162 return EINVAL; 1163 1164 s = splnet(); 1165 CNW_CMD2(sc, CNW_CMD_SMD, domain, domain >> 8); 1166 splx(s); 1167 1168 sc->sc_domain = domain; 1169 return 0; 1170} 1171 1172int 1173cnw_setkey(sc, key) 1174 struct cnw_softc *sc; 1175 int key; 1176{ 1177 int s; 1178 1179 if (key & ~0xffff) 1180 return EINVAL; 1181 1182 s = splnet(); 1183 CNW_CMD2(sc, CNW_CMD_SSK, key, key >> 8); 1184 splx(s); 1185 1186 sc->sc_skey = key; 1187 return 0; 1188} 1189 1190int 1191cnw_activate(self, act) 1192 struct device *self; 1193 enum devact act; 1194{ 1195 struct cnw_softc *sc = (struct cnw_softc *)self; 1196 int rv = 0, s; 1197 1198 s = splnet(); 1199 switch (act) { 1200 case DVACT_ACTIVATE: 1201 rv = EOPNOTSUPP; 1202 break; 1203 1204 case DVACT_DEACTIVATE: 1205 if_deactivate(&sc->sc_ethercom.ec_if); 1206 break; 1207 } 1208 splx(s); 1209 return (rv); 1210} 1211 1212int 1213cnw_detach(struct device *self, int flags) 1214{ 1215 struct cnw_softc *sc = (struct cnw_softc *)self; 1216 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 1217 1218 /* cnw_disable() checks IFF_RUNNING */ 1219 cnw_disable(sc); 1220 1221 if ((sc->sc_resource & CNW_RES_NET) != 0) { 1222 ether_ifdetach(ifp); 1223 if_detach(ifp); 1224 } 1225 1226#ifndef MEMORY_MAPPED 1227 /* unmap and free our i/o windows */ 1228 if ((sc->sc_resource & CNW_RES_IO) != 0) { 1229 pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin); 1230 pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh); 1231 } 1232#endif 1233 1234 /* unmap and free our memory windows */ 1235 if ((sc->sc_resource & CNW_RES_MEM) != 0) { 1236 pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwin); 1237 pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh); 1238 } 1239 1240 return (0); 1241} 1242