if_sn.c revision 66058
1/* 2 * Copyright (c) 1996 Gardner Buchanan <gbuchanan@shl.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Gardner Buchanan. 16 * 4. The name of Gardner Buchanan may not be used to endorse or promote 17 * products derived from this software without specific prior written 18 * permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * $FreeBSD: head/sys/dev/sn/if_sn.c 66058 2000-09-19 04:39:20Z imp $ 32 */ 33 34/* 35 * This is a driver for SMC's 9000 series of Ethernet adapters. 36 * 37 * This FreeBSD driver is derived from the smc9194 Linux driver by 38 * Erik Stahlman and is Copyright (C) 1996 by Erik Stahlman. 39 * This driver also shamelessly borrows from the FreeBSD ep driver 40 * which is Copyright (C) 1994 Herb Peyerl <hpeyerl@novatel.ca> 41 * All rights reserved. 42 * 43 * It is set up for my SMC91C92 equipped Ampro LittleBoard embedded 44 * PC. It is adapted from Erik Stahlman's Linux driver which worked 45 * with his EFA Info*Express SVC VLB adaptor. According to SMC's databook, 46 * it will work for the entire SMC 9xxx series. (Ha Ha) 47 * 48 * "Features" of the SMC chip: 49 * 4608 byte packet memory. (for the 91C92. Others have more) 50 * EEPROM for configuration 51 * AUI/TP selection 52 * 53 * Authors: 54 * Erik Stahlman erik@vt.edu 55 * Herb Peyerl hpeyerl@novatel.ca 56 * Andres Vega Garcia avega@sophia.inria.fr 57 * Serge Babkin babkin@hq.icb.chel.su 58 * Gardner Buchanan gbuchanan@shl.com 59 * 60 * Sources: 61 * o SMC databook 62 * o "smc9194.c:v0.10(FIXED) 02/15/96 by Erik Stahlman (erik@vt.edu)" 63 * o "if_ep.c,v 1.19 1995/01/24 20:53:45 davidg Exp" 64 * 65 * Known Bugs: 66 * o The hardware multicast filter isn't used yet. 67 * o Setting of the hardware address isn't supported. 68 * o Hardware padding isn't used. 69 */ 70 71/* 72 * Modifications for Megahertz X-Jack Ethernet Card (XJ-10BT) 73 * 74 * Copyright (c) 1996 by Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org> 75 * BSD-nomads, Tokyo, Japan. 76 */ 77/* 78 * Multicast support by Kei TANAKA <kei@pal.xerox.com> 79 * Special thanks to itojun@itojun.org 80 */ 81#define SN_DEBUG 82 83#include <sys/param.h> 84#include <sys/systm.h> 85#include <sys/errno.h> 86#include <sys/sockio.h> 87#include <sys/mbuf.h> 88#include <sys/socket.h> 89#include <sys/syslog.h> 90 91#include <sys/module.h> 92#include <sys/bus.h> 93 94#include <machine/bus.h> 95#include <machine/resource.h> 96#include <sys/rman.h> 97 98#include <net/ethernet.h> 99#include <net/if.h> 100#include <net/if_arp.h> 101#include <net/if_dl.h> 102#include <net/if_types.h> 103#include <net/if_mib.h> 104 105#ifdef INET 106#include <netinet/in.h> 107#include <netinet/in_systm.h> 108#include <netinet/in_var.h> 109#include <netinet/ip.h> 110#endif 111 112#ifdef NS 113#include <netns/ns.h> 114#include <netns/ns_if.h> 115#endif 116 117#include <net/bpf.h> 118#include <net/bpfdesc.h> 119 120#include <machine/clock.h> 121 122#include <dev/sn/if_snreg.h> 123#include <dev/sn/if_snvar.h> 124 125/* Exported variables */ 126devclass_t sn_devclass; 127 128static int snioctl(struct ifnet * ifp, u_long, caddr_t); 129 130static void snresume(struct ifnet *); 131 132void sninit(void *); 133void snread(struct ifnet *); 134void snreset(struct sn_softc *); 135void snstart(struct ifnet *); 136void snstop(struct sn_softc *); 137void snwatchdog(struct ifnet *); 138 139static void sn_setmcast(struct sn_softc *); 140static int sn_getmcf(struct arpcom *ac, u_char *mcf); 141static u_int smc_crc(u_char *); 142 143/* I (GB) have been unlucky getting the hardware padding 144 * to work properly. 145 */ 146#define SW_PAD 147 148static const char *chip_ids[15] = { 149 NULL, NULL, NULL, 150 /* 3 */ "SMC91C90/91C92", 151 /* 4 */ "SMC91C94", 152 /* 5 */ "SMC91C95", 153 NULL, 154 /* 7 */ "SMC91C100", 155 NULL, NULL, NULL, NULL, 156 NULL, NULL, NULL 157}; 158 159int 160sn_attach(device_t dev) 161{ 162 struct sn_softc *sc = device_get_softc(dev); 163 struct ifnet *ifp = &sc->arpcom.ac_if; 164 u_short i; 165 u_char *p; 166 struct ifaddr *ifa; 167 struct sockaddr_dl *sdl; 168 int rev; 169 u_short address; 170 int j; 171 172 sn_activate(dev); 173 174 snstop(sc); 175 176 sc->dev = dev; 177 sc->pages_wanted = -1; 178 179 device_printf(dev, " "); 180 181 SMC_SELECT_BANK(3); 182 rev = inw(BASE + REVISION_REG_W); 183 if (chip_ids[(rev >> 4) & 0xF]) 184 printf("%s ", chip_ids[(rev >> 4) & 0xF]); 185 186 SMC_SELECT_BANK(1); 187 i = inw(BASE + CONFIG_REG_W); 188 printf(i & CR_AUI_SELECT ? "AUI" : "UTP"); 189 190 if (sc->pccard_enaddr) 191 for (j = 0; j < 3; j++) { 192 u_short w; 193 194 w = (u_short)sc->arpcom.ac_enaddr[j * 2] | 195 (((u_short)sc->arpcom.ac_enaddr[j * 2 + 1]) << 8); 196 outw(BASE + IAR_ADDR0_REG_W + j * 2, w); 197 } 198 199 /* 200 * Read the station address from the chip. The MAC address is bank 1, 201 * regs 4 - 9 202 */ 203 SMC_SELECT_BANK(1); 204 p = (u_char *) & sc->arpcom.ac_enaddr; 205 for (i = 0; i < 6; i += 2) { 206 address = inw(BASE + IAR_ADDR0_REG_W + i); 207 p[i + 1] = address >> 8; 208 p[i] = address & 0xFF; 209 } 210 printf(" MAC address %6D\n", sc->arpcom.ac_enaddr, ":"); 211 ifp->if_softc = sc; 212 ifp->if_unit = device_get_unit(dev); 213 ifp->if_name = "sn"; 214 ifp->if_mtu = ETHERMTU; 215 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 216 ifp->if_output = ether_output; 217 ifp->if_start = snstart; 218 ifp->if_ioctl = snioctl; 219 ifp->if_watchdog = snwatchdog; 220 ifp->if_init = sninit; 221 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 222 ifp->if_timer = 0; 223 224 ether_ifattach(ifp, ETHER_BPF_SUPPORTED); 225 226 /* 227 * Fill the hardware address into ifa_addr if we find an AF_LINK 228 * entry. We need to do this so bpf's can get the hardware addr of 229 * this card. netstat likes this too! 230 */ 231 ifa = TAILQ_FIRST(&ifp->if_addrhead); 232 while ((ifa != 0) && (ifa->ifa_addr != 0) && 233 (ifa->ifa_addr->sa_family != AF_LINK)) 234 ifa = TAILQ_NEXT(ifa, ifa_link); 235 236 if ((ifa != 0) && (ifa->ifa_addr != 0)) { 237 sdl = (struct sockaddr_dl *) ifa->ifa_addr; 238 sdl->sdl_type = IFT_ETHER; 239 sdl->sdl_alen = ETHER_ADDR_LEN; 240 sdl->sdl_slen = 0; 241 bcopy(sc->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN); 242 } 243 244 return 0; 245} 246 247 248/* 249 * Reset and initialize the chip 250 */ 251void 252sninit(void *xsc) 253{ 254 register struct sn_softc *sc = xsc; 255 register struct ifnet *ifp = &sc->arpcom.ac_if; 256 int s; 257 int flags; 258 int mask; 259 260 s = splimp(); 261 262 /* 263 * This resets the registers mostly to defaults, but doesn't affect 264 * EEPROM. After the reset cycle, we pause briefly for the chip to 265 * be happy. 266 */ 267 SMC_SELECT_BANK(0); 268 outw(BASE + RECV_CONTROL_REG_W, RCR_SOFTRESET); 269 SMC_DELAY(); 270 outw(BASE + RECV_CONTROL_REG_W, 0x0000); 271 SMC_DELAY(); 272 SMC_DELAY(); 273 274 outw(BASE + TXMIT_CONTROL_REG_W, 0x0000); 275 276 /* 277 * Set the control register to automatically release succesfully 278 * transmitted packets (making the best use out of our limited 279 * memory) and to enable the EPH interrupt on certain TX errors. 280 */ 281 SMC_SELECT_BANK(1); 282 outw(BASE + CONTROL_REG_W, (CTR_AUTO_RELEASE | CTR_TE_ENABLE | 283 CTR_CR_ENABLE | CTR_LE_ENABLE)); 284 285 /* Set squelch level to 240mV (default 480mV) */ 286 flags = inw(BASE + CONFIG_REG_W); 287 flags |= CR_SET_SQLCH; 288 outw(BASE + CONFIG_REG_W, flags); 289 290 /* 291 * Reset the MMU and wait for it to be un-busy. 292 */ 293 SMC_SELECT_BANK(2); 294 outw(BASE + MMU_CMD_REG_W, MMUCR_RESET); 295 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */ 296 ; 297 298 /* 299 * Disable all interrupts 300 */ 301 outb(BASE + INTR_MASK_REG_B, 0x00); 302 303 sn_setmcast(sc); 304 305 /* 306 * Set the transmitter control. We want it enabled. 307 */ 308 flags = TCR_ENABLE; 309 310#ifndef SW_PAD 311 /* 312 * I (GB) have been unlucky getting this to work. 313 */ 314 flags |= TCR_PAD_ENABLE; 315#endif /* SW_PAD */ 316 317 outw(BASE + TXMIT_CONTROL_REG_W, flags); 318 319 320 /* 321 * Now, enable interrupts 322 */ 323 SMC_SELECT_BANK(2); 324 325 mask = IM_EPH_INT | 326 IM_RX_OVRN_INT | 327 IM_RCV_INT | 328 IM_TX_INT; 329 330 outb(BASE + INTR_MASK_REG_B, mask); 331 sc->intr_mask = mask; 332 sc->pages_wanted = -1; 333 334 335 /* 336 * Mark the interface running but not active. 337 */ 338 ifp->if_flags |= IFF_RUNNING; 339 ifp->if_flags &= ~IFF_OACTIVE; 340 341 /* 342 * Attempt to push out any waiting packets. 343 */ 344 snstart(ifp); 345 346 splx(s); 347} 348 349 350void 351snstart(struct ifnet *ifp) 352{ 353 register struct sn_softc *sc = ifp->if_softc; 354 register u_int len; 355 register struct mbuf *m; 356 struct mbuf *top; 357 int s, pad; 358 int mask; 359 u_short length; 360 u_short numPages; 361 u_char packet_no; 362 int time_out; 363 int junk = 0; 364 365 s = splimp(); 366 367 if (sc->arpcom.ac_if.if_flags & IFF_OACTIVE) { 368 splx(s); 369 return; 370 } 371 if (sc->pages_wanted != -1) { 372 splx(s); 373 printf("sn%d: snstart() while memory allocation pending\n", 374 ifp->if_unit); 375 return; 376 } 377startagain: 378 379 /* 380 * Sneak a peek at the next packet 381 */ 382 m = sc->arpcom.ac_if.if_snd.ifq_head; 383 if (m == 0) { 384 splx(s); 385 return; 386 } 387 /* 388 * Compute the frame length and set pad to give an overall even 389 * number of bytes. Below we assume that the packet length is even. 390 */ 391 for (len = 0, top = m; m; m = m->m_next) 392 len += m->m_len; 393 394 pad = (len & 1); 395 396 /* 397 * We drop packets that are too large. Perhaps we should truncate 398 * them instead? 399 */ 400 if (len + pad > ETHER_MAX_LEN - ETHER_CRC_LEN) { 401 printf("sn%d: large packet discarded (A)\n", ifp->if_unit); 402 ++sc->arpcom.ac_if.if_oerrors; 403 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m); 404 m_freem(m); 405 goto readcheck; 406 } 407#ifdef SW_PAD 408 409 /* 410 * If HW padding is not turned on, then pad to ETHER_MIN_LEN. 411 */ 412 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) 413 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len; 414 415#endif /* SW_PAD */ 416 417 length = pad + len; 418 419 /* 420 * The MMU wants the number of pages to be the number of 256 byte 421 * 'pages', minus 1 (A packet can't ever have 0 pages. We also 422 * include space for the status word, byte count and control bytes in 423 * the allocation request. 424 */ 425 numPages = (length + 6) >> 8; 426 427 428 /* 429 * Now, try to allocate the memory 430 */ 431 SMC_SELECT_BANK(2); 432 outw(BASE + MMU_CMD_REG_W, MMUCR_ALLOC | numPages); 433 434 /* 435 * Wait a short amount of time to see if the allocation request 436 * completes. Otherwise, I enable the interrupt and wait for 437 * completion asyncronously. 438 */ 439 440 time_out = MEMORY_WAIT_TIME; 441 do { 442 if (inb(BASE + INTR_STAT_REG_B) & IM_ALLOC_INT) 443 break; 444 } while (--time_out); 445 446 if (!time_out || junk > 10) { 447 448 /* 449 * No memory now. Oh well, wait until the chip finds memory 450 * later. Remember how many pages we were asking for and 451 * enable the allocation completion interrupt. Also set a 452 * watchdog in case we miss the interrupt. We mark the 453 * interface active since there is no point in attempting an 454 * snstart() until after the memory is available. 455 */ 456 mask = inb(BASE + INTR_MASK_REG_B) | IM_ALLOC_INT; 457 outb(BASE + INTR_MASK_REG_B, mask); 458 sc->intr_mask = mask; 459 460 sc->arpcom.ac_if.if_timer = 1; 461 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE; 462 sc->pages_wanted = numPages; 463 464 splx(s); 465 return; 466 } 467 /* 468 * The memory allocation completed. Check the results. 469 */ 470 packet_no = inb(BASE + ALLOC_RESULT_REG_B); 471 if (packet_no & ARR_FAILED) { 472 if (junk++ > 10) 473 printf("sn%d: Memory allocation failed\n", ifp->if_unit); 474 goto startagain; 475 } 476 /* 477 * We have a packet number, so tell the card to use it. 478 */ 479 outb(BASE + PACKET_NUM_REG_B, packet_no); 480 481 /* 482 * Point to the beginning of the packet 483 */ 484 outw(BASE + POINTER_REG_W, PTR_AUTOINC | 0x0000); 485 486 /* 487 * Send the packet length (+6 for status, length and control byte) 488 * and the status word (set to zeros) 489 */ 490 outw(BASE + DATA_REG_W, 0); 491 outb(BASE + DATA_REG_B, (length + 6) & 0xFF); 492 outb(BASE + DATA_REG_B, (length + 6) >> 8); 493 494 /* 495 * Get the packet from the kernel. This will include the Ethernet 496 * frame header, MAC Addresses etc. 497 */ 498 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m); 499 500 /* 501 * Push out the data to the card. 502 */ 503 for (top = m; m != 0; m = m->m_next) { 504 505 /* 506 * Push out words. 507 */ 508 outsw(BASE + DATA_REG_W, mtod(m, caddr_t), m->m_len / 2); 509 510 /* 511 * Push out remaining byte. 512 */ 513 if (m->m_len & 1) 514 outb(BASE + DATA_REG_B, *(mtod(m, caddr_t) + m->m_len - 1)); 515 } 516 517 /* 518 * Push out padding. 519 */ 520 while (pad > 1) { 521 outw(BASE + DATA_REG_W, 0); 522 pad -= 2; 523 } 524 if (pad) 525 outb(BASE + DATA_REG_B, 0); 526 527 /* 528 * Push out control byte and unused packet byte The control byte is 0 529 * meaning the packet is even lengthed and no special CRC handling is 530 * desired. 531 */ 532 outw(BASE + DATA_REG_W, 0); 533 534 /* 535 * Enable the interrupts and let the chipset deal with it Also set a 536 * watchdog in case we miss the interrupt. 537 */ 538 mask = inb(BASE + INTR_MASK_REG_B) | (IM_TX_INT | IM_TX_EMPTY_INT); 539 outb(BASE + INTR_MASK_REG_B, mask); 540 sc->intr_mask = mask; 541 542 outw(BASE + MMU_CMD_REG_W, MMUCR_ENQUEUE); 543 544 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE; 545 sc->arpcom.ac_if.if_timer = 1; 546 547 if (ifp->if_bpf) { 548 bpf_mtap(ifp, top); 549 } 550 551 sc->arpcom.ac_if.if_opackets++; 552 m_freem(top); 553 554 555readcheck: 556 557 /* 558 * Is another packet coming in? We don't want to overflow the tiny 559 * RX FIFO. If nothing has arrived then attempt to queue another 560 * transmit packet. 561 */ 562 if (inw(BASE + FIFO_PORTS_REG_W) & FIFO_REMPTY) 563 goto startagain; 564 565 splx(s); 566 return; 567} 568 569 570 571/* Resume a packet transmit operation after a memory allocation 572 * has completed. 573 * 574 * This is basically a hacked up copy of snstart() which handles 575 * a completed memory allocation the same way snstart() does. 576 * It then passes control to snstart to handle any other queued 577 * packets. 578 */ 579static void 580snresume(struct ifnet *ifp) 581{ 582 register struct sn_softc *sc = ifp->if_softc; 583 register u_int len; 584 register struct mbuf *m; 585 struct mbuf *top; 586 int pad; 587 int mask; 588 u_short length; 589 u_short numPages; 590 u_short pages_wanted; 591 u_char packet_no; 592 593 if (sc->pages_wanted < 0) 594 return; 595 596 pages_wanted = sc->pages_wanted; 597 sc->pages_wanted = -1; 598 599 /* 600 * Sneak a peek at the next packet 601 */ 602 m = sc->arpcom.ac_if.if_snd.ifq_head; 603 if (m == 0) { 604 printf("sn%d: snresume() with nothing to send\n", ifp->if_unit); 605 return; 606 } 607 /* 608 * Compute the frame length and set pad to give an overall even 609 * number of bytes. Below we assume that the packet length is even. 610 */ 611 for (len = 0, top = m; m; m = m->m_next) 612 len += m->m_len; 613 614 pad = (len & 1); 615 616 /* 617 * We drop packets that are too large. Perhaps we should truncate 618 * them instead? 619 */ 620 if (len + pad > ETHER_MAX_LEN - ETHER_CRC_LEN) { 621 printf("sn%d: large packet discarded (B)\n", ifp->if_unit); 622 ++sc->arpcom.ac_if.if_oerrors; 623 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m); 624 m_freem(m); 625 return; 626 } 627#ifdef SW_PAD 628 629 /* 630 * If HW padding is not turned on, then pad to ETHER_MIN_LEN. 631 */ 632 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) 633 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len; 634 635#endif /* SW_PAD */ 636 637 length = pad + len; 638 639 640 /* 641 * The MMU wants the number of pages to be the number of 256 byte 642 * 'pages', minus 1 (A packet can't ever have 0 pages. We also 643 * include space for the status word, byte count and control bytes in 644 * the allocation request. 645 */ 646 numPages = (length + 6) >> 8; 647 648 649 SMC_SELECT_BANK(2); 650 651 /* 652 * The memory allocation completed. Check the results. If it failed, 653 * we simply set a watchdog timer and hope for the best. 654 */ 655 packet_no = inb(BASE + ALLOC_RESULT_REG_B); 656 if (packet_no & ARR_FAILED) { 657 printf("sn%d: Memory allocation failed. Weird.\n", ifp->if_unit); 658 sc->arpcom.ac_if.if_timer = 1; 659 goto try_start; 660 } 661 /* 662 * We have a packet number, so tell the card to use it. 663 */ 664 outb(BASE + PACKET_NUM_REG_B, packet_no); 665 666 /* 667 * Now, numPages should match the pages_wanted recorded when the 668 * memory allocation was initiated. 669 */ 670 if (pages_wanted != numPages) { 671 printf("sn%d: memory allocation wrong size. Weird.\n", ifp->if_unit); 672 /* 673 * If the allocation was the wrong size we simply release the 674 * memory once it is granted. Wait for the MMU to be un-busy. 675 */ 676 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */ 677 ; 678 outw(BASE + MMU_CMD_REG_W, MMUCR_FREEPKT); 679 680 return; 681 } 682 /* 683 * Point to the beginning of the packet 684 */ 685 outw(BASE + POINTER_REG_W, PTR_AUTOINC | 0x0000); 686 687 /* 688 * Send the packet length (+6 for status, length and control byte) 689 * and the status word (set to zeros) 690 */ 691 outw(BASE + DATA_REG_W, 0); 692 outb(BASE + DATA_REG_B, (length + 6) & 0xFF); 693 outb(BASE + DATA_REG_B, (length + 6) >> 8); 694 695 /* 696 * Get the packet from the kernel. This will include the Ethernet 697 * frame header, MAC Addresses etc. 698 */ 699 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m); 700 701 /* 702 * Push out the data to the card. 703 */ 704 for (top = m; m != 0; m = m->m_next) { 705 706 /* 707 * Push out words. 708 */ 709 outsw(BASE + DATA_REG_W, mtod(m, caddr_t), m->m_len / 2); 710 711 /* 712 * Push out remaining byte. 713 */ 714 if (m->m_len & 1) 715 outb(BASE + DATA_REG_B, *(mtod(m, caddr_t) + m->m_len - 1)); 716 } 717 718 /* 719 * Push out padding. 720 */ 721 while (pad > 1) { 722 outw(BASE + DATA_REG_W, 0); 723 pad -= 2; 724 } 725 if (pad) 726 outb(BASE + DATA_REG_B, 0); 727 728 /* 729 * Push out control byte and unused packet byte The control byte is 0 730 * meaning the packet is even lengthed and no special CRC handling is 731 * desired. 732 */ 733 outw(BASE + DATA_REG_W, 0); 734 735 /* 736 * Enable the interrupts and let the chipset deal with it Also set a 737 * watchdog in case we miss the interrupt. 738 */ 739 mask = inb(BASE + INTR_MASK_REG_B) | (IM_TX_INT | IM_TX_EMPTY_INT); 740 outb(BASE + INTR_MASK_REG_B, mask); 741 sc->intr_mask = mask; 742 outw(BASE + MMU_CMD_REG_W, MMUCR_ENQUEUE); 743 744 if (ifp->if_bpf) { 745 bpf_mtap(ifp, top); 746 } 747 748 sc->arpcom.ac_if.if_opackets++; 749 m_freem(top); 750 751try_start: 752 753 /* 754 * Now pass control to snstart() to queue any additional packets 755 */ 756 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE; 757 snstart(ifp); 758 759 /* 760 * We've sent something, so we're active. Set a watchdog in case the 761 * TX_EMPTY interrupt is lost. 762 */ 763 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE; 764 sc->arpcom.ac_if.if_timer = 1; 765 766 return; 767} 768 769 770void 771sn_intr(void *arg) 772{ 773 int status, interrupts; 774 register struct sn_softc *sc = (struct sn_softc *) arg; 775 struct ifnet *ifp = &sc->arpcom.ac_if; 776 int x; 777 778 /* 779 * Chip state registers 780 */ 781 u_char mask; 782 u_char packet_no; 783 u_short tx_status; 784 u_short card_stats; 785 786 /* 787 * if_ep.c did this, so I do too. Yet if_ed.c doesn't. I wonder... 788 */ 789 x = splbio(); 790 791 /* 792 * Clear the watchdog. 793 */ 794 ifp->if_timer = 0; 795 796 SMC_SELECT_BANK(2); 797 798 /* 799 * Obtain the current interrupt mask and clear the hardware mask 800 * while servicing interrupts. 801 */ 802 mask = inb(BASE + INTR_MASK_REG_B); 803 outb(BASE + INTR_MASK_REG_B, 0x00); 804 805 /* 806 * Get the set of interrupts which occurred and eliminate any which 807 * are masked. 808 */ 809 interrupts = inb(BASE + INTR_STAT_REG_B); 810 status = interrupts & mask; 811 812 /* 813 * Now, process each of the interrupt types. 814 */ 815 816 /* 817 * Receive Overrun. 818 */ 819 if (status & IM_RX_OVRN_INT) { 820 821 /* 822 * Acknowlege Interrupt 823 */ 824 SMC_SELECT_BANK(2); 825 outb(BASE + INTR_ACK_REG_B, IM_RX_OVRN_INT); 826 827 ++sc->arpcom.ac_if.if_ierrors; 828 } 829 /* 830 * Got a packet. 831 */ 832 if (status & IM_RCV_INT) { 833#if 1 834 int packet_number; 835 836 SMC_SELECT_BANK(2); 837 packet_number = inw(BASE + FIFO_PORTS_REG_W); 838 839 if (packet_number & FIFO_REMPTY) { 840 841 /* 842 * we got called , but nothing was on the FIFO 843 */ 844 printf("sn: Receive interrupt with nothing on FIFO\n"); 845 846 goto out; 847 } 848#endif 849 snread(ifp); 850 } 851 /* 852 * An on-card memory allocation came through. 853 */ 854 if (status & IM_ALLOC_INT) { 855 856 /* 857 * Disable this interrupt. 858 */ 859 mask &= ~IM_ALLOC_INT; 860 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE; 861 snresume(&sc->arpcom.ac_if); 862 } 863 /* 864 * TX Completion. Handle a transmit error message. This will only be 865 * called when there is an error, because of the AUTO_RELEASE mode. 866 */ 867 if (status & IM_TX_INT) { 868 869 /* 870 * Acknowlege Interrupt 871 */ 872 SMC_SELECT_BANK(2); 873 outb(BASE + INTR_ACK_REG_B, IM_TX_INT); 874 875 packet_no = inw(BASE + FIFO_PORTS_REG_W); 876 packet_no &= FIFO_TX_MASK; 877 878 /* 879 * select this as the packet to read from 880 */ 881 outb(BASE + PACKET_NUM_REG_B, packet_no); 882 883 /* 884 * Position the pointer to the first word from this packet 885 */ 886 outw(BASE + POINTER_REG_W, PTR_AUTOINC | PTR_READ | 0x0000); 887 888 /* 889 * Fetch the TX status word. The value found here will be a 890 * copy of the EPH_STATUS_REG_W at the time the transmit 891 * failed. 892 */ 893 tx_status = inw(BASE + DATA_REG_W); 894 895 if (tx_status & EPHSR_TX_SUC) { 896 device_printf(sc->dev, 897 "Successful packet caused interrupt\n"); 898 } else { 899 ++sc->arpcom.ac_if.if_oerrors; 900 } 901 902 if (tx_status & EPHSR_LATCOL) 903 ++sc->arpcom.ac_if.if_collisions; 904 905 /* 906 * Some of these errors will have disabled transmit. 907 * Re-enable transmit now. 908 */ 909 SMC_SELECT_BANK(0); 910 911#ifdef SW_PAD 912 outw(BASE + TXMIT_CONTROL_REG_W, TCR_ENABLE); 913#else 914 outw(BASE + TXMIT_CONTROL_REG_W, TCR_ENABLE | TCR_PAD_ENABLE); 915#endif /* SW_PAD */ 916 917 /* 918 * kill the failed packet. Wait for the MMU to be un-busy. 919 */ 920 SMC_SELECT_BANK(2); 921 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */ 922 ; 923 outw(BASE + MMU_CMD_REG_W, MMUCR_FREEPKT); 924 925 /* 926 * Attempt to queue more transmits. 927 */ 928 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE; 929 snstart(&sc->arpcom.ac_if); 930 } 931 /* 932 * Transmit underrun. We use this opportunity to update transmit 933 * statistics from the card. 934 */ 935 if (status & IM_TX_EMPTY_INT) { 936 937 /* 938 * Acknowlege Interrupt 939 */ 940 SMC_SELECT_BANK(2); 941 outb(BASE + INTR_ACK_REG_B, IM_TX_EMPTY_INT); 942 943 /* 944 * Disable this interrupt. 945 */ 946 mask &= ~IM_TX_EMPTY_INT; 947 948 SMC_SELECT_BANK(0); 949 card_stats = inw(BASE + COUNTER_REG_W); 950 951 /* 952 * Single collisions 953 */ 954 sc->arpcom.ac_if.if_collisions += card_stats & ECR_COLN_MASK; 955 956 /* 957 * Multiple collisions 958 */ 959 sc->arpcom.ac_if.if_collisions += (card_stats & ECR_MCOLN_MASK) >> 4; 960 961 SMC_SELECT_BANK(2); 962 963 /* 964 * Attempt to enqueue some more stuff. 965 */ 966 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE; 967 snstart(&sc->arpcom.ac_if); 968 } 969 /* 970 * Some other error. Try to fix it by resetting the adapter. 971 */ 972 if (status & IM_EPH_INT) { 973 snstop(sc); 974 sninit(sc); 975 } 976 977out: 978 /* 979 * Handled all interrupt sources. 980 */ 981 982 SMC_SELECT_BANK(2); 983 984 /* 985 * Reestablish interrupts from mask which have not been deselected 986 * during this interrupt. Note that the hardware mask, which was set 987 * to 0x00 at the start of this service routine, may have been 988 * updated by one or more of the interrupt handers and we must let 989 * those new interrupts stay enabled here. 990 */ 991 mask |= inb(BASE + INTR_MASK_REG_B); 992 outb(BASE + INTR_MASK_REG_B, mask); 993 sc->intr_mask = mask; 994 995 splx(x); 996} 997 998void 999snread(register struct ifnet *ifp) 1000{ 1001 struct sn_softc *sc = ifp->if_softc; 1002 struct ether_header *eh; 1003 struct mbuf *m; 1004 short status; 1005 int packet_number; 1006 u_short packet_length; 1007 u_char *data; 1008 1009 SMC_SELECT_BANK(2); 1010#if 0 1011 packet_number = inw(BASE + FIFO_PORTS_REG_W); 1012 1013 if (packet_number & FIFO_REMPTY) { 1014 1015 /* 1016 * we got called , but nothing was on the FIFO 1017 */ 1018 printf("sn: Receive interrupt with nothing on FIFO\n"); 1019 return; 1020 } 1021#endif 1022read_another: 1023 1024 /* 1025 * Start reading from the start of the packet. Since PTR_RCV is set, 1026 * packet number is found in FIFO_PORTS_REG_W, FIFO_RX_MASK. 1027 */ 1028 outw(BASE + POINTER_REG_W, PTR_READ | PTR_RCV | PTR_AUTOINC | 0x0000); 1029 1030 /* 1031 * First two words are status and packet_length 1032 */ 1033 status = inw(BASE + DATA_REG_W); 1034 packet_length = inw(BASE + DATA_REG_W) & RLEN_MASK; 1035 1036 /* 1037 * The packet length contains 3 extra words: status, length, and a 1038 * extra word with the control byte. 1039 */ 1040 packet_length -= 6; 1041 1042 /* 1043 * Account for receive errors and discard. 1044 */ 1045 if (status & RS_ERRORS) { 1046 ++sc->arpcom.ac_if.if_ierrors; 1047 goto out; 1048 } 1049 /* 1050 * A packet is received. 1051 */ 1052 1053 /* 1054 * Adjust for odd-length packet. 1055 */ 1056 if (status & RS_ODDFRAME) 1057 packet_length++; 1058 1059 /* 1060 * Allocate a header mbuf from the kernel. 1061 */ 1062 MGETHDR(m, M_DONTWAIT, MT_DATA); 1063 if (m == NULL) 1064 goto out; 1065 1066 m->m_pkthdr.rcvif = &sc->arpcom.ac_if; 1067 m->m_pkthdr.len = m->m_len = packet_length; 1068 1069 /* 1070 * Attach an mbuf cluster 1071 */ 1072 MCLGET(m, M_DONTWAIT); 1073 1074 /* 1075 * Insist on getting a cluster 1076 */ 1077 if ((m->m_flags & M_EXT) == 0) { 1078 m_freem(m); 1079 ++sc->arpcom.ac_if.if_ierrors; 1080 printf("sn: snread() kernel memory allocation problem\n"); 1081 goto out; 1082 } 1083 eh = mtod(m, struct ether_header *); 1084 1085 /* 1086 * Get packet, including link layer address, from interface. 1087 */ 1088 1089 data = (u_char *) eh; 1090 insw(BASE + DATA_REG_W, data, packet_length >> 1); 1091 if (packet_length & 1) { 1092 data += packet_length & ~1; 1093 *data = inb(BASE + DATA_REG_B); 1094 } 1095 ++sc->arpcom.ac_if.if_ipackets; 1096 1097 /* 1098 * Remove link layer addresses and whatnot. 1099 */ 1100 m->m_pkthdr.len = m->m_len = packet_length - sizeof(struct ether_header); 1101 m->m_data += sizeof(struct ether_header); 1102 1103 ether_input(&sc->arpcom.ac_if, eh, m); 1104 1105out: 1106 1107 /* 1108 * Error or good, tell the card to get rid of this packet Wait for 1109 * the MMU to be un-busy. 1110 */ 1111 SMC_SELECT_BANK(2); 1112 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */ 1113 ; 1114 outw(BASE + MMU_CMD_REG_W, MMUCR_RELEASE); 1115 1116 /* 1117 * Check whether another packet is ready 1118 */ 1119 packet_number = inw(BASE + FIFO_PORTS_REG_W); 1120 if (packet_number & FIFO_REMPTY) { 1121 return; 1122 } 1123 goto read_another; 1124} 1125 1126 1127/* 1128 * Handle IOCTLS. This function is completely stolen from if_ep.c 1129 * As with its progenitor, it does not handle hardware address 1130 * changes. 1131 */ 1132static int 1133snioctl(register struct ifnet *ifp, u_long cmd, caddr_t data) 1134{ 1135 struct sn_softc *sc = ifp->if_softc; 1136 int s, error = 0; 1137 1138 s = splimp(); 1139 1140 switch (cmd) { 1141 case SIOCSIFADDR: 1142 case SIOCGIFADDR: 1143 case SIOCSIFMTU: 1144 error = ether_ioctl(ifp, cmd, data); 1145 break; 1146 1147 case SIOCSIFFLAGS: 1148 if ((ifp->if_flags & IFF_UP) == 0 && ifp->if_flags & IFF_RUNNING) { 1149 ifp->if_flags &= ~IFF_RUNNING; 1150 snstop(sc); 1151 break; 1152 } else { 1153 /* reinitialize card on any parameter change */ 1154 sninit(sc); 1155 break; 1156 } 1157 break; 1158 1159#ifdef notdef 1160 case SIOCGHWADDR: 1161 bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data, 1162 sizeof(sc->sc_addr)); 1163 break; 1164#endif 1165 1166 case SIOCADDMULTI: 1167 /* update multicast filter list. */ 1168 sn_setmcast(sc); 1169 error = 0; 1170 break; 1171 case SIOCDELMULTI: 1172 /* update multicast filter list. */ 1173 sn_setmcast(sc); 1174 error = 0; 1175 break; 1176 default: 1177 error = EINVAL; 1178 } 1179 1180 splx(s); 1181 1182 return (error); 1183} 1184 1185void 1186snreset(struct sn_softc *sc) 1187{ 1188 int s; 1189 1190 s = splimp(); 1191 snstop(sc); 1192 sninit(sc); 1193 1194 splx(s); 1195} 1196 1197void 1198snwatchdog(struct ifnet *ifp) 1199{ 1200 int s; 1201 s = splimp(); 1202 sn_intr(ifp->if_softc); 1203 splx(s); 1204} 1205 1206 1207/* 1. zero the interrupt mask 1208 * 2. clear the enable receive flag 1209 * 3. clear the enable xmit flags 1210 */ 1211void 1212snstop(struct sn_softc *sc) 1213{ 1214 1215 struct ifnet *ifp = &sc->arpcom.ac_if; 1216 1217 /* 1218 * Clear interrupt mask; disable all interrupts. 1219 */ 1220 SMC_SELECT_BANK(2); 1221 outb(BASE + INTR_MASK_REG_B, 0x00); 1222 1223 /* 1224 * Disable transmitter and Receiver 1225 */ 1226 SMC_SELECT_BANK(0); 1227 outw(BASE + RECV_CONTROL_REG_W, 0x0000); 1228 outw(BASE + TXMIT_CONTROL_REG_W, 0x0000); 1229 1230 /* 1231 * Cancel watchdog. 1232 */ 1233 ifp->if_timer = 0; 1234} 1235 1236 1237int 1238sn_activate(device_t dev) 1239{ 1240 struct sn_softc *sc = device_get_softc(dev); 1241 int err; 1242 1243 sc->port_rid = 0; 1244 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid, 1245 0, ~0, SMC_IO_EXTENT, RF_ACTIVE); 1246 if (!sc->port_res) { 1247 if (bootverbose) 1248 device_printf(dev, "Cannot allocate ioport\n"); 1249 return ENOMEM; 1250 } 1251 1252 sc->irq_rid = 0; 1253 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid, 1254 0, ~0, 1, RF_ACTIVE); 1255 if (!sc->irq_res) { 1256 if (bootverbose) 1257 device_printf(dev, "Cannot allocate irq\n"); 1258 sn_deactivate(dev); 1259 return ENOMEM; 1260 } 1261 if ((err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET, sn_intr, sc, 1262 &sc->intrhand)) != 0) { 1263 sn_deactivate(dev); 1264 return err; 1265 } 1266 1267 sc->sn_io_addr = rman_get_start(sc->port_res); 1268 return (0); 1269} 1270 1271void 1272sn_deactivate(device_t dev) 1273{ 1274 struct sn_softc *sc = device_get_softc(dev); 1275 1276 if (sc->intrhand) 1277 bus_teardown_intr(dev, sc->irq_res, sc->intrhand); 1278 sc->intrhand = 0; 1279 if (sc->port_res) 1280 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid, 1281 sc->port_res); 1282 sc->port_res = 0; 1283 if (sc->irq_res) 1284 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, 1285 sc->irq_res); 1286 sc->irq_res = 0; 1287 return; 1288} 1289 1290/* 1291 * Function: sn_probe( device_t dev, int pccard ) 1292 * 1293 * Purpose: 1294 * Tests to see if a given ioaddr points to an SMC9xxx chip. 1295 * Tries to cause as little damage as possible if it's not a SMC chip. 1296 * Returns a 0 on success 1297 * 1298 * Algorithm: 1299 * (1) see if the high byte of BANK_SELECT is 0x33 1300 * (2) compare the ioaddr with the base register's address 1301 * (3) see if I recognize the chip ID in the appropriate register 1302 * 1303 * 1304 */ 1305int 1306sn_probe(device_t dev, int pccard) 1307{ 1308 struct sn_softc *sc = device_get_softc(dev); 1309 u_int bank; 1310 u_short revision_register; 1311 u_short base_address_register; 1312 u_short ioaddr; 1313 int err; 1314 1315 if ((err = sn_activate(dev)) != 0) 1316 return err; 1317 1318 ioaddr = sc->sn_io_addr; 1319#ifdef SN_DEBUG 1320 device_printf(dev, "ioaddr is 0x%x\n", ioaddr); 1321#endif 1322 /* 1323 * First, see if the high byte is 0x33 1324 */ 1325 bank = inw(ioaddr + BANK_SELECT_REG_W); 1326 if ((bank & BSR_DETECT_MASK) != BSR_DETECT_VALUE) { 1327#ifdef SN_DEBUG 1328 device_printf(dev, "test1 failed\n"); 1329#endif 1330 goto error; 1331 } 1332 /* 1333 * The above MIGHT indicate a device, but I need to write to further 1334 * test this. Go to bank 0, then test that the register still 1335 * reports the high byte is 0x33. 1336 */ 1337 outw(ioaddr + BANK_SELECT_REG_W, 0x0000); 1338 bank = inw(ioaddr + BANK_SELECT_REG_W); 1339 if ((bank & BSR_DETECT_MASK) != BSR_DETECT_VALUE) { 1340#ifdef SN_DEBUG 1341 device_printf(dev, "test2 failed\n"); 1342#endif 1343 goto error; 1344 } 1345 /* 1346 * well, we've already written once, so hopefully another time won't 1347 * hurt. This time, I need to switch the bank register to bank 1, so 1348 * I can access the base address register. The contents of the 1349 * BASE_ADDR_REG_W register, after some jiggery pokery, is expected 1350 * to match the I/O port address where the adapter is being probed. 1351 */ 1352 outw(ioaddr + BANK_SELECT_REG_W, 0x0001); 1353 base_address_register = inw(ioaddr + BASE_ADDR_REG_W); 1354 1355 /* 1356 * This test is nonsence on PC-card architecture, so if 1357 * pccard == 1, skip this test. (hosokawa) 1358 */ 1359 if (!pccard && (ioaddr != (base_address_register >> 3 & 0x3E0))) { 1360 1361 /* 1362 * Well, the base address register didn't match. Must not 1363 * have been a SMC chip after all. 1364 */ 1365#ifdef SN_DEBUG 1366 device_printf(dev, "test3 failed ioaddr = 0x%x, " 1367 "base_address_register = 0x%x\n", ioaddr, 1368 base_address_register >> 3 & 0x3E0); 1369#endif 1370 goto error; 1371 } 1372 1373 /* 1374 * Check if the revision register is something that I recognize. 1375 * These might need to be added to later, as future revisions could 1376 * be added. 1377 */ 1378 outw(ioaddr + BANK_SELECT_REG_W, 0x3); 1379 revision_register = inw(ioaddr + REVISION_REG_W); 1380 if (!chip_ids[(revision_register >> 4) & 0xF]) { 1381 1382 /* 1383 * I don't regonize this chip, so... 1384 */ 1385#ifdef SN_DEBUG 1386 device_printf(dev, "test4 failed\n"); 1387#endif 1388 goto error; 1389 } 1390 1391 /* 1392 * at this point I'll assume that the chip is an SMC9xxx. It might be 1393 * prudent to check a listing of MAC addresses against the hardware 1394 * address, or do some other tests. 1395 */ 1396 sn_deactivate(dev); 1397 return 0; 1398 error: 1399 sn_deactivate(dev); 1400 return ENXIO; 1401} 1402 1403#define MCFSZ 8 1404 1405static void 1406sn_setmcast(struct sn_softc *sc) 1407{ 1408 struct ifnet *ifp = (struct ifnet *)sc; 1409 int flags; 1410 1411 /* 1412 * Set the receiver filter. We want receive enabled and auto strip 1413 * of CRC from received packet. If we are promiscuous then set that 1414 * bit too. 1415 */ 1416 flags = RCR_ENABLE | RCR_STRIP_CRC; 1417 1418 if (ifp->if_flags & IFF_PROMISC) { 1419 flags |= RCR_PROMISC | RCR_ALMUL; 1420 } else if (ifp->if_flags & IFF_ALLMULTI) { 1421 flags |= RCR_ALMUL; 1422 } else { 1423 u_char mcf[MCFSZ]; 1424 if (sn_getmcf(&sc->arpcom, mcf)) { 1425 /* set filter */ 1426 SMC_SELECT_BANK(3); 1427 outw(BASE + MULTICAST1_REG_W, 1428 ((u_short)mcf[1] << 8) | mcf[0]); 1429 outw(BASE + MULTICAST2_REG_W, 1430 ((u_short)mcf[3] << 8) | mcf[2]); 1431 outw(BASE + MULTICAST3_REG_W, 1432 ((u_short)mcf[5] << 8) | mcf[4]); 1433 outw(BASE + MULTICAST4_REG_W, 1434 ((u_short)mcf[7] << 8) | mcf[6]); 1435 } else { 1436 flags |= RCR_ALMUL; 1437 } 1438 } 1439 SMC_SELECT_BANK(0); 1440 outw(BASE + RECV_CONTROL_REG_W, flags); 1441} 1442 1443static int 1444sn_getmcf(struct arpcom *ac, u_char *mcf) 1445{ 1446 int i; 1447 register u_int index, index2; 1448 register u_char *af = (u_char *) mcf; 1449 struct ifmultiaddr *ifma; 1450 1451 bzero(mcf, MCFSZ); 1452 1453 for (ifma = ac->ac_if.if_multiaddrs.lh_first; ifma; 1454 ifma = ifma->ifma_link.le_next) { 1455 if (ifma->ifma_addr->sa_family != AF_LINK) 1456 return 0; 1457 index = smc_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)) & 0x3f; 1458 index2 = 0; 1459 for (i = 0; i < 6; i++) { 1460 index2 <<= 1; 1461 index2 |= (index & 0x01); 1462 index >>= 1; 1463 } 1464 af[index2 >> 3] |= 1 << (index2 & 7); 1465 } 1466 return 1; /* use multicast filter */ 1467} 1468 1469static u_int 1470smc_crc(u_char *s) 1471{ 1472 int perByte; 1473 int perBit; 1474 const u_int poly = 0xedb88320; 1475 u_int v = 0xffffffff; 1476 u_char c; 1477 1478 for (perByte = 0; perByte < ETHER_ADDR_LEN; perByte++) { 1479 c = s[perByte]; 1480 for (perBit = 0; perBit < 8; perBit++) { 1481 v = (v >> 1)^(((v ^ c) & 0x01) ? poly : 0); 1482 c >>= 1; 1483 } 1484 } 1485 return v; 1486} 1487