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