1 /** 2 * Granch SBNI16 G.SHDSL Modem driver 3 * Written by Denis I. Timofeev, 2002-2003. 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 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/dev/sbsh/if_sbsh.c,v 1.3.2.1 2003/04/15 18:15:07 fjoe Exp $ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/sockio.h> 32 #include <sys/mbuf.h> 33 #include <sys/malloc.h> 34 #include <sys/kernel.h> 35 #include <sys/proc.h> 36 #include <sys/priv.h> 37 #include <sys/socket.h> 38 #include <sys/random.h> 39 #include <sys/serialize.h> 40 #include <sys/bus.h> 41 #include <sys/rman.h> 42 #include <sys/interrupt.h> 43 44 #include <net/if.h> 45 #include <net/ifq_var.h> 46 #include <net/if_arp.h> 47 #include <net/ethernet.h> 48 #include <net/if_media.h> 49 50 #include <net/bpf.h> 51 52 #include <vm/vm.h> 53 #include <vm/pmap.h> 54 55 #include <bus/pci/pcireg.h> 56 #include <bus/pci/pcivar.h> 57 58 #include "if_sbshreg.h" 59 60 /* -------------------------------------------------------------------------- */ 61 62 struct sbni16_hw_regs { 63 u_int8_t CR, CRB, SR, IMR, CTDR, LTDR, CRDR, LRDR; 64 }; 65 66 struct hw_descr { 67 u_int32_t address; 68 u_int32_t length; 69 }; 70 71 struct cx28975_cmdarea { 72 u_int8_t intr_host; 73 u_int8_t intr_8051; 74 u_int8_t map_version; 75 76 u_int8_t in_dest; 77 u_int8_t in_opcode; 78 u_int8_t in_zero; 79 u_int8_t in_length; 80 u_int8_t in_csum; 81 u_int8_t in_data[75]; 82 u_int8_t in_datasum; 83 84 u_int8_t out_dest; 85 u_int8_t out_opcode; 86 u_int8_t out_ack; 87 u_int8_t out_length; 88 u_int8_t out_csum; 89 u_int8_t out_data[75]; 90 u_int8_t out_datasum; 91 }; 92 93 #define XQLEN 8 94 #define RQLEN 8 95 96 struct sbsh_softc { 97 struct arpcom arpcom; /* ethernet common */ 98 99 struct resource *mem_res; 100 struct resource *irq_res; 101 void *intr_hand; 102 103 void *mem_base; /* mapped memory address */ 104 105 volatile struct sbni16_hw_regs *regs; 106 volatile struct hw_descr *tbd; 107 volatile struct hw_descr *rbd; 108 volatile struct cx28975_cmdarea *cmdp; 109 110 /* SBNI16 controller statistics */ 111 struct sbni16_stats { 112 u_int32_t sent_pkts, rcvd_pkts; 113 u_int32_t crc_errs, ufl_errs, ofl_errs, attempts, last_time; 114 } in_stats; 115 116 /* transmit and reception queues */ 117 struct mbuf *xq[XQLEN], *rq[RQLEN]; 118 unsigned head_xq, tail_xq, head_rq, tail_rq; 119 120 /* the descriptors mapped onto the first buffers in xq and rq */ 121 unsigned head_tdesc, head_rdesc; 122 u_int8_t state; 123 }; 124 125 struct cx28975_cfg { 126 u_int8_t *firmw_image; 127 u_int32_t firmw_len; 128 u_int32_t lrate: 10; 129 u_int32_t master: 1; 130 u_int32_t mod: 2; 131 u_int32_t crc16: 1; 132 u_int32_t fill_7e: 1; 133 u_int32_t inv: 1; 134 u_int32_t rburst: 1; 135 u_int32_t wburst: 1; 136 u_int32_t : 14; 137 }; 138 139 /* SHDSL transceiver statistics */ 140 struct dsl_stats { 141 u_int8_t status_1, status_3; 142 u_int8_t attenuat, nmr, tpbo, rpbo; 143 u_int16_t losw, segd, crc, sega, losd; 144 }; 145 146 enum State { NOT_LOADED, DOWN, ACTIVATION, ACTIVE }; 147 148 #define SIOCLOADFIRMW _IOWR('i', 67, struct ifreq) 149 #define SIOCGETSTATS _IOWR('i', 68, struct ifreq) 150 #define SIOCCLRSTATS _IOWR('i', 69, struct ifreq) 151 152 static int sbsh_probe(device_t); 153 static int sbsh_attach(device_t); 154 static int sbsh_detach(device_t); 155 static int sbsh_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *); 156 static void sbsh_shutdown(device_t); 157 static int sbsh_suspend(device_t); 158 static int sbsh_resume(device_t); 159 static void sbsh_watchdog(struct ifnet *); 160 161 static void sbsh_start(struct ifnet *, struct ifaltq_subque *); 162 static void sbsh_init(void *); 163 static void sbsh_stop(struct sbsh_softc *); 164 static void init_card(struct sbsh_softc *); 165 static void sbsh_intr(void *); 166 static void resume_tx(struct sbsh_softc *); 167 static void start_xmit_frames(struct sbsh_softc *); 168 static void encap_frame(struct sbsh_softc *, struct mbuf *); 169 static struct mbuf * repack(struct sbsh_softc *, struct mbuf *); 170 static void free_sent_buffers(struct sbsh_softc *); 171 static void alloc_rx_buffers(struct sbsh_softc *); 172 static void indicate_frames(struct sbsh_softc *); 173 static void drop_queues(struct sbsh_softc *); 174 static void activate(struct sbsh_softc *); 175 static void deactivate(struct sbsh_softc *); 176 static void cx28975_interrupt(struct sbsh_softc *); 177 static int start_cx28975(struct sbsh_softc *, struct cx28975_cfg); 178 static int download_firmware(struct sbsh_softc *, u_int8_t *, u_int32_t); 179 static int issue_cx28975_cmd(struct sbsh_softc *, u_int8_t, 180 u_int8_t *, u_int8_t); 181 182 static device_method_t sbsh_methods[] = { 183 /* Device interface */ 184 DEVMETHOD(device_probe, sbsh_probe), 185 DEVMETHOD(device_attach, sbsh_attach), 186 DEVMETHOD(device_detach, sbsh_detach), 187 DEVMETHOD(device_shutdown, sbsh_shutdown), 188 DEVMETHOD(device_suspend, sbsh_suspend), 189 DEVMETHOD(device_resume, sbsh_resume), 190 191 DEVMETHOD_END 192 }; 193 194 static driver_t sbsh_driver = { 195 "sbsh", 196 sbsh_methods, 197 sizeof(struct sbsh_softc) 198 }; 199 200 static devclass_t sbsh_devclass; 201 202 DECLARE_DUMMY_MODULE(if_sbsh); 203 DRIVER_MODULE(if_sbsh, pci, sbsh_driver, sbsh_devclass, NULL, NULL); 204 205 static int 206 sbsh_probe(device_t dev) 207 { 208 if (pci_get_vendor(dev) != SBNI16_VENDOR 209 || pci_get_device(dev) != SBNI16_DEVICE 210 || pci_get_subdevice(dev) != SBNI16_SUBDEV) 211 return (ENXIO); 212 213 device_set_desc(dev, "Granch SBNI16 G.SHDSL Modem"); 214 return (0); 215 } 216 217 static int 218 sbsh_attach(device_t dev) 219 { 220 struct sbsh_softc *sc; 221 struct ifnet *ifp; 222 int unit, error = 0, rid; 223 224 sc = device_get_softc(dev); 225 unit = device_get_unit(dev); 226 227 rid = PCIR_MAPS + 4; 228 sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 229 0, ~0, 4096, RF_ACTIVE); 230 231 if (sc->mem_res == NULL) { 232 kprintf ("sbsh%d: couldn't map memory\n", unit); 233 error = ENXIO; 234 goto fail; 235 } 236 237 rid = 0; 238 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 239 RF_SHAREABLE | RF_ACTIVE); 240 241 if (sc->irq_res == NULL) { 242 kprintf("sbsh%d: couldn't map interrupt\n", unit); 243 error = ENXIO; 244 goto fail; 245 } 246 247 sc->mem_base = rman_get_virtual(sc->mem_res); 248 init_card(sc); 249 /* generate ethernet MAC address */ 250 *(u_int32_t *)sc->arpcom.ac_enaddr = htonl(0x00ff0192); 251 read_random(sc->arpcom.ac_enaddr + 4, 2, 1); 252 253 ifp = &sc->arpcom.ac_if; 254 ifp->if_softc = sc; 255 if_initname(ifp, "sbsh", unit); 256 ifp->if_mtu = ETHERMTU; 257 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 258 ifp->if_ioctl = sbsh_ioctl; 259 ifp->if_start = sbsh_start; 260 ifp->if_watchdog = sbsh_watchdog; 261 ifp->if_init = sbsh_init; 262 ifp->if_baudrate = 4600000; 263 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN); 264 ifq_set_ready(&ifp->if_snd); 265 266 ether_ifattach(ifp, sc->arpcom.ac_enaddr, NULL); 267 268 ifq_set_cpuid(&ifp->if_snd, rman_get_cpuid(sc->irq_res)); 269 270 error = bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE, 271 sbsh_intr, sc, &sc->intr_hand, 272 ifp->if_serializer); 273 if (error) { 274 ether_ifdetach(ifp); 275 kprintf("sbsh%d: couldn't set up irq\n", unit); 276 goto fail; 277 } 278 279 return(0); 280 281 fail: 282 sbsh_detach(dev); 283 return (error); 284 } 285 286 static int 287 sbsh_detach(device_t dev) 288 { 289 struct sbsh_softc *sc = device_get_softc(dev); 290 struct ifnet *ifp = &sc->arpcom.ac_if; 291 292 if (device_is_attached(dev)) { 293 lwkt_serialize_enter(ifp->if_serializer); 294 sbsh_stop(sc); 295 bus_teardown_intr(dev, sc->irq_res, sc->intr_hand); 296 lwkt_serialize_exit(ifp->if_serializer); 297 298 ether_ifdetach(ifp); 299 } 300 301 if (sc->irq_res) 302 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res); 303 if (sc->mem_res) { 304 bus_release_resource(dev, SYS_RES_MEMORY, PCIR_MAPS + 4, 305 sc->mem_res); 306 } 307 308 return (0); 309 } 310 311 312 static void 313 sbsh_start(struct ifnet *ifp, struct ifaltq_subque *ifsq) 314 { 315 struct sbsh_softc *sc = ifp->if_softc; 316 317 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq); 318 319 if (sc->state == ACTIVE) 320 start_xmit_frames(ifp->if_softc); 321 } 322 323 324 static void 325 sbsh_init(void *xsc) 326 { 327 struct sbsh_softc *sc = xsc; 328 struct ifnet *ifp = &sc->arpcom.ac_if; 329 u_int8_t t; 330 331 if ((ifp->if_flags & IFF_RUNNING) || sc->state == NOT_LOADED) { 332 return; 333 } 334 335 bzero(&sc->in_stats, sizeof(struct sbni16_stats)); 336 sc->head_xq = sc->tail_xq = sc->head_rq = sc->tail_rq = 0; 337 sc->head_tdesc = sc->head_rdesc = 0; 338 339 sc->regs->IMR = EXT; 340 t = 2; 341 issue_cx28975_cmd(sc, _DSL_CLEAR_ERROR_CTRS, &t, 1); 342 if (issue_cx28975_cmd(sc, _DSL_ACTIVATION, &t, 1) == 0) { 343 sc->state = ACTIVATION; 344 345 ifp->if_flags |= IFF_RUNNING; 346 ifq_clr_oactive(&ifp->if_snd); 347 } 348 } 349 350 351 static void 352 sbsh_stop(struct sbsh_softc *sc) 353 { 354 u_int8_t t; 355 356 sc->regs->IMR = EXT; 357 358 t = 0; 359 issue_cx28975_cmd(sc, _DSL_ACTIVATION, &t, 1); 360 if (sc->state == ACTIVE) { 361 t = 1; 362 issue_cx28975_cmd(sc, _DSL_FORCE_DEACTIVATE, &t, 1); 363 /* FIX! activation manager state */ 364 365 /* Is it really must be done here? It calls from intr handler */ 366 deactivate(sc); 367 } 368 369 sc->regs->IMR = 0; 370 sc->state = DOWN; 371 } 372 373 374 static void 375 init_card(struct sbsh_softc *sc) 376 { 377 sc->state = NOT_LOADED; 378 sc->tbd = (struct hw_descr *) sc->mem_base; 379 sc->rbd = (struct hw_descr *) ((u_int8_t *)sc->mem_base + 0x400); 380 sc->regs = (struct sbni16_hw_regs *) ((u_int8_t *)sc->mem_base + 0x800); 381 sc->cmdp = (struct cx28975_cmdarea *) ((u_int8_t *)sc->mem_base + 0xc00); 382 383 sc->regs->CR = 0; 384 sc->regs->SR = 0xff; 385 sc->regs->IMR = 0; 386 } 387 388 389 static int 390 sbsh_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 391 { 392 struct sbsh_softc *sc = ifp->if_softc; 393 struct ifreq *ifr = (struct ifreq *) data; 394 struct cx28975_cfg cfg; 395 struct dsl_stats ds; 396 int error = 0; 397 u_int8_t t; 398 399 switch(cmd) { 400 case SIOCLOADFIRMW: 401 if ((error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY)) != 0) 402 break; 403 if (ifp->if_flags & IFF_UP) 404 error = EBUSY; 405 406 bzero(&cfg, sizeof(cfg)); 407 error = copyin((caddr_t)ifr->ifr_data, 408 (caddr_t)&cfg, 409 sizeof cfg); 410 if (error) { 411 /* fall out */ 412 } else if (start_cx28975(sc, cfg) == 0) { 413 static char *modstr[] = { 414 "TCPAM32", "TCPAM16", "TCPAM8", "TCPAM4" }; 415 if_printf(&sc->arpcom.ac_if, "%s, rate %d, %s\n", 416 cfg.master ? "master" : "slave", 417 cfg.lrate << 3, modstr[cfg.mod]); 418 } else { 419 if_printf(&sc->arpcom.ac_if, 420 "unable to load firmware\n"); 421 error = EIO; 422 } 423 break; 424 425 case SIOCGETSTATS : 426 if ((error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY)) != 0) 427 break; 428 429 t = 0; 430 if (issue_cx28975_cmd(sc, _DSL_FAR_END_ATTEN, &t, 1)) 431 error = EIO; 432 ds.attenuat = sc->cmdp->out_data[0]; 433 434 if (issue_cx28975_cmd(sc, _DSL_NOISE_MARGIN, &t, 1)) 435 error = EIO; 436 ds.nmr = sc->cmdp->out_data[0]; 437 438 if (issue_cx28975_cmd(sc, _DSL_POWER_BACK_OFF_RESULT, &t, 1)) 439 error = EIO; 440 ds.tpbo = sc->cmdp->out_data[0]; 441 ds.rpbo = sc->cmdp->out_data[1]; 442 443 if (!issue_cx28975_cmd(sc, _DSL_HDSL_PERF_ERR_CTRS, &t, 1)) { 444 int i; 445 for (i = 0; i < 10; ++i) 446 ((u_int8_t *) &ds.losw)[i] = 447 sc->cmdp->out_data[i]; 448 } else { 449 error = EIO; 450 } 451 452 ds.status_1 = ((volatile u_int8_t *)sc->cmdp)[0x3c0]; 453 ds.status_3 = ((volatile u_int8_t *)sc->cmdp)[0x3c2]; 454 455 if (error == 0) { 456 error = copyout(&sc->in_stats, 457 ifr->ifr_data, 458 sizeof(struct sbni16_stats)); 459 } 460 if (error == 0) { 461 error = copyout(&ds, 462 (char *)ifr->ifr_data + 463 sizeof(struct sbni16_stats), 464 sizeof(struct dsl_stats)); 465 } 466 break; 467 468 case SIOCCLRSTATS : 469 if (!(error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY))) { 470 bzero(&sc->in_stats, sizeof(struct sbni16_stats)); 471 t = 2; 472 if (issue_cx28975_cmd(sc, _DSL_CLEAR_ERROR_CTRS, &t, 1)) 473 error = EIO; 474 } 475 break; 476 case SIOCSIFFLAGS: 477 if (ifp->if_flags & IFF_UP) { 478 if (!(ifp->if_flags & IFF_RUNNING)) { 479 if (sc->state == NOT_LOADED) { 480 if_printf(ifp, "firmware wasn't loaded\n"); 481 error = EBUSY; 482 } else 483 sbsh_init(sc); 484 } 485 } else { 486 if (ifp->if_flags & IFF_RUNNING) { 487 sbsh_stop(sc); 488 ifp->if_flags &= ~IFF_RUNNING; 489 } 490 } 491 break; 492 493 case SIOCADDMULTI: 494 case SIOCDELMULTI: 495 error = 0; 496 break; 497 default: 498 error = ether_ioctl(ifp, cmd, data); 499 break; 500 } 501 return (error); 502 } 503 504 505 static void 506 sbsh_shutdown(device_t dev) 507 { 508 struct sbsh_softc *sc = device_get_softc(dev); 509 510 lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer); 511 sbsh_stop(sc); 512 lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer); 513 } 514 515 static int 516 sbsh_suspend(device_t dev) 517 { 518 struct sbsh_softc *sc = device_get_softc(dev); 519 520 lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer); 521 sbsh_stop(sc); 522 lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer); 523 524 return (0); 525 } 526 527 static int 528 sbsh_resume(device_t dev) 529 { 530 struct sbsh_softc *sc = device_get_softc(dev); 531 struct ifnet *ifp = &sc->arpcom.ac_if; 532 533 if (ifp->if_flags & IFF_UP) 534 sbsh_init(sc); 535 536 return (0); 537 } 538 539 540 static void 541 sbsh_watchdog(struct ifnet *ifp) 542 { 543 struct sbsh_softc *sc = ifp->if_softc; 544 545 if_printf(ifp, "transmit timeout\n"); 546 547 if (sc->regs->SR & TXS) { 548 sc->regs->SR = TXS; 549 if_printf(ifp, "interrupt posted but not delivered\n"); 550 } 551 free_sent_buffers(sc); 552 } 553 554 /* -------------------------------------------------------------------------- */ 555 556 static void 557 sbsh_intr(void *arg) 558 { 559 struct sbsh_softc *sc = (struct sbsh_softc *)arg; 560 u_int8_t status = sc->regs->SR; 561 562 if (status == 0) 563 return; 564 565 if (status & EXT) { 566 cx28975_interrupt(sc); 567 sc->regs->SR = EXT; 568 } 569 570 if (status & UFL) { 571 resume_tx(sc); 572 sc->regs->SR = UFL; 573 ++sc->in_stats.ufl_errs; 574 IFNET_STAT_INC(&sc->arpcom.ac_if, oerrors, 1); 575 } 576 577 if (status & RXS) { 578 sc->regs->SR = RXS; 579 indicate_frames(sc); 580 alloc_rx_buffers(sc); 581 } 582 583 if (status & TXS) { 584 sc->regs->SR = TXS; 585 free_sent_buffers(sc); 586 } 587 588 if (status & CRC) { 589 ++sc->in_stats.crc_errs; 590 IFNET_STAT_INC(&sc->arpcom.ac_if, ierrors, 1); 591 sc->regs->SR = CRC; 592 } 593 594 if (status & OFL) { 595 ++sc->in_stats.ofl_errs; 596 IFNET_STAT_INC(&sc->arpcom.ac_if, ierrors, 1); 597 sc->regs->SR = OFL; 598 } 599 } 600 601 /* 602 * Look for a first descriptor of a next packet, and write it's number 603 * into CTDR. Then enable the transmitter. 604 */ 605 static void 606 resume_tx(struct sbsh_softc *sc) 607 { 608 u_int32_t cur_tbd = sc->regs->CTDR; 609 610 while (cur_tbd != sc->regs->LTDR 611 && (sc->tbd[cur_tbd++].length & LAST_FRAG) == 0) 612 ; 613 sc->regs->CTDR = cur_tbd; 614 sc->regs->CR |= TXEN; 615 } 616 617 static void 618 start_xmit_frames(struct sbsh_softc *sc) 619 { 620 struct ifnet *ifp = &sc->arpcom.ac_if; 621 struct mbuf *m; 622 623 /* 624 * Check if we have any free descriptor(s) and free space in 625 * our transmit queue. 626 */ 627 while (sc->tail_xq != ((sc->head_xq - 1) & (XQLEN - 1)) 628 && sc->regs->LTDR != ((sc->head_tdesc - 1) & 0x7f)) { 629 630 m = ifq_dequeue(&ifp->if_snd); 631 if (m == NULL) 632 break; 633 if (m->m_pkthdr.len) { 634 BPF_MTAP(ifp, m); 635 encap_frame(sc, m); 636 } else 637 m_freem(m); 638 } 639 640 if (sc->regs->CTDR != sc->regs->LTDR) 641 ifq_set_oactive(&ifp->if_snd); 642 else 643 ifq_clr_oactive(&ifp->if_snd); 644 } 645 646 647 static void 648 encap_frame(struct sbsh_softc *sc, struct mbuf *m_head) 649 { 650 struct mbuf *m; 651 u_int32_t cur_tbd; 652 int done; 653 654 look_for_nonzero: 655 for (m = m_head; !m->m_len; m = m->m_next) 656 ; 657 658 cur_tbd = sc->regs->LTDR & 0x7f; 659 done = 0; 660 do { 661 if (m->m_len < 5 || cur_tbd == ((sc->head_tdesc - 1) & 0x7f)) { 662 if ((m_head = repack(sc, m_head)) != NULL) 663 goto look_for_nonzero; 664 else 665 return; 666 } 667 668 sc->tbd[cur_tbd].address = vtophys(mtod(m, vm_offset_t)); 669 sc->tbd[cur_tbd].length = m->m_len; 670 671 do { 672 m = m->m_next; 673 } while (m && !m->m_len); 674 675 if (!m) { /* last fragment has been reached */ 676 sc->tbd[cur_tbd].length |= LAST_FRAG; 677 done = 1; 678 } 679 680 ++cur_tbd; 681 cur_tbd &= 0x7f; 682 } while (!done); 683 684 sc->xq[sc->tail_xq++] = m_head; 685 sc->tail_xq &= (XQLEN - 1); 686 687 sc->regs->LTDR = cur_tbd; 688 ++sc->in_stats.sent_pkts; 689 IFNET_STAT_INC(&sc->arpcom.ac_if, opackets, 1); 690 } 691 692 static struct mbuf * 693 repack(struct sbsh_softc *sc, struct mbuf *m) 694 { 695 struct mbuf *m_new; 696 697 MGETHDR(m_new, M_NOWAIT, MT_DATA); 698 if (!m_new) { 699 if_printf (&sc->arpcom.ac_if, 700 "unable to get mbuf.\n"); 701 return (NULL); 702 } 703 704 if (m->m_pkthdr.len > MHLEN) { 705 MCLGET(m_new, M_NOWAIT); 706 if (!(m_new->m_flags & M_EXT)) { 707 m_freem(m_new); 708 if_printf (&sc->arpcom.ac_if, 709 "unable to get mbuf cluster.\n"); 710 return (NULL); 711 } 712 } 713 714 m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, caddr_t)); 715 m_new->m_pkthdr.len = m_new->m_len = m->m_pkthdr.len; 716 m_freem(m); 717 return (m_new); 718 } 719 720 static void 721 free_sent_buffers(struct sbsh_softc *sc) 722 { 723 u_int32_t cur_tbd; 724 725 cur_tbd = sc->regs->CTDR; 726 727 while (sc->head_tdesc != cur_tbd) { 728 /* 729 * Be careful! one element in xq may correspond to 730 * multiple descriptors. 731 */ 732 if (sc->tbd[sc->head_tdesc].length & LAST_FRAG) { 733 m_freem(sc->xq[sc->head_xq++]); 734 sc->head_xq &= (XQLEN - 1); 735 } 736 737 sc->tbd[sc->head_tdesc].length = 0; 738 sc->head_tdesc = (sc->head_tdesc + 1) & 0x7f; 739 } 740 741 start_xmit_frames(sc); 742 } 743 744 /* 745 * DON'T use free_sent_buffers to drop the queue! 746 */ 747 static void 748 alloc_rx_buffers(struct sbsh_softc *sc) 749 { 750 unsigned cur_rbd = sc->regs->LRDR & 0x7f; 751 struct mbuf *m; 752 753 while (sc->tail_rq != ((sc->head_rq - 1) & (RQLEN - 1))) { 754 MGETHDR(m, M_NOWAIT, MT_DATA); 755 if (!m) { 756 if_printf (&sc->arpcom.ac_if, 757 "unable to get mbuf.\n"); 758 return; 759 } 760 761 if (SBNI16_MAX_FRAME > MHLEN) { 762 MCLGET(m, M_NOWAIT); 763 if (!(m->m_flags & M_EXT)) { 764 m_freem(m); 765 if_printf (&sc->arpcom.ac_if, 766 "unable to get mbuf cluster.\n"); 767 return; 768 } 769 m->m_pkthdr.len = m->m_len = MCLBYTES; 770 } 771 772 m_adj(m, 2); /* align ip on longword boundaries */ 773 774 sc->rq[sc->tail_rq++] = m; 775 sc->tail_rq &= (RQLEN - 1); 776 777 sc->rbd[cur_rbd].address = vtophys(mtod(m, vm_offset_t)); 778 sc->rbd[cur_rbd].length = 0; 779 sc->regs->LRDR = cur_rbd = (cur_rbd + 1) & 0x7f; 780 } 781 } 782 783 static void 784 indicate_frames(struct sbsh_softc *sc) 785 { 786 struct ifnet *ifp = &sc->arpcom.ac_if; 787 unsigned cur_rbd = sc->regs->CRDR & 0x7f; 788 789 while (sc->head_rdesc != cur_rbd) { 790 struct mbuf *m = sc->rq[sc->head_rq++]; 791 sc->head_rq &= (RQLEN - 1); 792 793 m->m_pkthdr.len = m->m_len = 794 sc->rbd[sc->head_rdesc].length & 0x7ff; 795 m->m_pkthdr.rcvif = ifp; 796 797 ifp->if_input(ifp, m, NULL, -1); 798 ++sc->in_stats.rcvd_pkts; 799 IFNET_STAT_INC(ifp, ipackets, 1); 800 801 sc->head_rdesc = (sc->head_rdesc + 1) & 0x7f; 802 } 803 } 804 805 static void 806 drop_queues(struct sbsh_softc *sc) 807 { 808 while (sc->head_rq != sc->tail_rq) { 809 m_freem(sc->rq[sc->head_rq++]); 810 sc->head_rq &= (RQLEN - 1); 811 } 812 813 while (sc->head_xq != sc->tail_xq) { 814 m_freem(sc->xq[sc->head_xq++]); 815 sc->head_xq &= (XQLEN - 1); 816 } 817 } 818 819 /* -------------------------------------------------------------------------- */ 820 821 static void 822 activate(struct sbsh_softc *sc) 823 { 824 struct timeval tv; 825 826 sc->regs->SR = 0xff; /* clear it! */ 827 sc->regs->CTDR = sc->regs->LTDR = sc->regs->CRDR = sc->regs->LRDR = 0; 828 829 sc->head_tdesc = sc->head_rdesc = 0; 830 alloc_rx_buffers(sc); 831 832 sc->regs->CRB &= ~RXDE; 833 sc->regs->IMR = EXT | RXS | TXS | CRC | OFL | UFL; 834 sc->regs->CR |= TXEN | RXEN; 835 836 sc->state = ACTIVE; 837 ++sc->in_stats.attempts; 838 microtime(&tv); 839 sc->in_stats.last_time = tv.tv_sec; 840 start_xmit_frames(sc); 841 } 842 843 static void 844 deactivate(struct sbsh_softc *sc) 845 { 846 sc->regs->CR &= ~(RXEN | TXEN); 847 sc->regs->CRB |= RXDE; 848 sc->regs->IMR = EXT; 849 sc->regs->CTDR = sc->regs->LTDR; 850 sc->regs->CRDR = sc->regs->LRDR; 851 sc->state = ACTIVATION; 852 853 drop_queues(sc); 854 } 855 856 /* -------------------------------------------------------------------------- */ 857 858 static void 859 cx28975_interrupt(struct sbsh_softc *sc) 860 { 861 volatile struct cx28975_cmdarea *p = sc->cmdp; 862 863 if (p->intr_host != 0xfe) 864 return; 865 866 if (p->out_ack & 0x80) { 867 if (*((volatile u_int8_t *)p + 0x3c7) & 2) { 868 if (sc->state != ACTIVE 869 && (*((volatile u_int8_t *)p + 0x3c0) & 0xc0) == 0x40) { 870 activate(sc); 871 if_printf(&sc->arpcom.ac_if, "connected to peer\n"); 872 } else if (sc->state == ACTIVE 873 && (*((volatile u_int8_t *)p + 0x3c0) & 0xc0) != 0x40) { 874 deactivate(sc); 875 if_printf(&sc->arpcom.ac_if, "carrier lost\n"); 876 } 877 } 878 879 p->intr_host = 0; 880 p->out_ack = 0; 881 } else { 882 wakeup(sc); 883 884 p->intr_host = 0; 885 } 886 } 887 888 /* -------------------------------------------------------------------------- */ 889 890 static int 891 start_cx28975(struct sbsh_softc *sc, struct cx28975_cfg cfg) 892 { 893 static char thresh[] = { +8, -4, -16, -40 }; 894 895 volatile struct cx28975_cmdarea *p = sc->cmdp; 896 u_int8_t t, parm[12]; 897 898 p->intr_host = 0; 899 t = p->intr_host; 900 901 /* reset chip set */ 902 sc->regs->IMR = EXT; 903 sc->regs->CR = 0; 904 sc->regs->SR = 0xff; 905 DELAY(2); 906 sc->regs->CR = XRST; 907 if (cfg.crc16) 908 sc->regs->CR |= CMOD; 909 if (cfg.fill_7e) 910 sc->regs->CR |= FMOD; 911 if (cfg.inv) 912 sc->regs->CR |= PMOD; 913 914 sc->regs->CRB |= RODD | RXDE; 915 if (cfg.rburst) 916 sc->regs->CRB |= RDBE; 917 if (cfg.wburst) 918 sc->regs->CRB |= WTBE; 919 920 tsleep(sc, 0, "sbsh", 0); 921 if ((p->out_ack & 0x1f) != _ACK_BOOT_WAKE_UP) 922 return (-1); 923 924 if (download_firmware(sc, cfg.firmw_image, cfg.firmw_len)) 925 return (-1); 926 927 tsleep(sc, 0, "sbsh", 0); 928 if ((p->out_ack & 0x1f) != _ACK_OPER_WAKE_UP) 929 return (-1); 930 931 t = cfg.master ? 1 : 9; 932 if (issue_cx28975_cmd(sc, _DSL_SYSTEM_ENABLE, &t, 1)) 933 return (-1); 934 935 t = 0x63; 936 if (issue_cx28975_cmd(sc, _DSL_SYSTEM_CONFIG, &t, 1)) 937 return (-1); 938 939 *(u_int16_t *)parm = cfg.lrate >> 3; 940 parm[2] = parm[3] = parm[0]; 941 parm[5] = cfg.lrate & 7; 942 parm[4] = parm[7] = 1; 943 parm[6] = 0; 944 if (issue_cx28975_cmd(sc, _DSL_MULTI_RATE_CONFIG, parm, 8)) 945 return (-1); 946 947 parm[0] = 0x02 | (cfg.mod << 4); 948 parm[1] = 0; 949 if (issue_cx28975_cmd(sc, _DSL_TRAINING_MODE, parm, 2)) 950 return (-1); 951 952 bzero(parm, 12); 953 parm[0] = 0x04; /* pre-activation: G.hs */ 954 parm[4] = 0x04; /* no remote configuration */ 955 parm[7] = 0x01; /* annex A (default) */ 956 parm[8] = 0xff; /* i-bit mask (all bits) */ 957 if (issue_cx28975_cmd(sc, _DSL_PREACTIVATION_CFG, parm, 12)) 958 return (-1); 959 960 parm[0] = 0x03; /* dying gasp time - 3 frames */ 961 parm[1] = thresh[cfg.mod]; 962 parm[2] = 0xff; /* attenuation */ 963 parm[3] = 0x04; /* line probe NMR (+2 dB) */ 964 parm[4] = 0x00; /* reserved */ 965 parm[5] = 0x00; 966 if (issue_cx28975_cmd(sc, _DSL_THRESHOLDS, parm, 6)) 967 return (-1); 968 969 t = cfg.master ? 0x23 : 0x21; 970 if (issue_cx28975_cmd(sc, _DSL_FR_PCM_CONFIG, &t, 1)) 971 return (-1); 972 973 t = 0x02; 974 if (issue_cx28975_cmd(sc, _DSL_INTR_HOST_MASK, &t, 1)) 975 return (-1); 976 977 sc->state = DOWN; 978 return (0); 979 } 980 981 static int 982 download_firmware(struct sbsh_softc *sc, u_int8_t *img, u_int32_t img_len) 983 { 984 u_int32_t t; 985 int i; 986 u_int8_t cksum = 0; 987 988 for (i = 0; i < img_len; ++i) 989 cksum += img[i]; 990 991 t = img_len; 992 if (issue_cx28975_cmd(sc, _DSL_DOWNLOAD_START, (u_int8_t *) &t, 4)) 993 return (-1); 994 995 for (i = 0; img_len >= 75; i += 75, img_len -= 75) { 996 if (issue_cx28975_cmd(sc, _DSL_DOWNLOAD_DATA, img + i, 75)) 997 return (-1); 998 } 999 1000 if (img_len 1001 && issue_cx28975_cmd(sc, _DSL_DOWNLOAD_DATA, img + i, img_len)) 1002 return (-1); 1003 1004 t = (cksum ^ 0xff) + 1; 1005 if (issue_cx28975_cmd(sc, _DSL_DOWNLOAD_END, (u_int8_t *) &t, 1)) 1006 return (-1); 1007 1008 return (0); 1009 } 1010 1011 static int 1012 issue_cx28975_cmd(struct sbsh_softc *sc, u_int8_t cmd, 1013 u_int8_t *data, u_int8_t size) 1014 { 1015 volatile struct cx28975_cmdarea *p = sc->cmdp; 1016 volatile u_int8_t *databuf = p->in_data; 1017 int i; 1018 1019 u_int8_t cksum = 0; 1020 1021 p->in_dest = 0xf0; 1022 p->in_opcode = cmd; 1023 p->in_zero = 0; 1024 p->in_length = --size; 1025 p->in_csum = 0xf0 ^ cmd ^ size ^ 0xaa; 1026 1027 for (i = 0; i <= size; ++i) { 1028 cksum ^= *data; 1029 *databuf++ = *data++; /* only 1 byte per cycle! */ 1030 } 1031 1032 p->in_datasum = cksum ^ 0xaa; 1033 p->out_ack = _ACK_NOT_COMPLETE; 1034 p->intr_8051 = 0xfe; 1035 1036 if (tsleep(sc, 0, "sbsh", hz << 3)) 1037 return (-1); 1038 1039 while (p->out_ack == _ACK_NOT_COMPLETE) 1040 ; /* FIXME ! */ 1041 1042 if ((p->out_ack & 0x1f) == _ACK_PASS) { 1043 p->out_ack = 0; 1044 return (0); 1045 } else { 1046 p->out_ack = 0; 1047 return (-1); 1048 } 1049 } 1050