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