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