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.25 2006/12/22 23:26:22 swildner 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/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/thread2.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 *); 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 { 0, 0 } 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, 0, 0); 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_unlimited(sc->arpcom.ac_enaddr + 4, 2); 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 error = bus_setup_intr(dev, sc->irq_res, INTR_NETSAFE, 269 sbsh_intr, sc, &sc->intr_hand, 270 ifp->if_serializer); 271 if (error) { 272 ether_ifdetach(ifp); 273 kprintf("sbsh%d: couldn't set up irq\n", unit); 274 goto fail; 275 } 276 277 return(0); 278 279 fail: 280 sbsh_detach(dev); 281 return (error); 282 } 283 284 static int 285 sbsh_detach(device_t dev) 286 { 287 struct sbsh_softc *sc = device_get_softc(dev); 288 struct ifnet *ifp = &sc->arpcom.ac_if; 289 290 if (device_is_attached(dev)) { 291 lwkt_serialize_enter(ifp->if_serializer); 292 sbsh_stop(sc); 293 bus_teardown_intr(dev, sc->irq_res, sc->intr_hand); 294 lwkt_serialize_exit(ifp->if_serializer); 295 296 ether_ifdetach(ifp); 297 } 298 299 if (sc->irq_res) 300 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res); 301 if (sc->mem_res) { 302 bus_release_resource(dev, SYS_RES_MEMORY, PCIR_MAPS + 4, 303 sc->mem_res); 304 } 305 306 return (0); 307 } 308 309 310 static void 311 sbsh_start(struct ifnet *ifp) 312 { 313 struct sbsh_softc *sc = ifp->if_softc; 314 315 if (sc->state == ACTIVE) 316 start_xmit_frames(ifp->if_softc); 317 } 318 319 320 static void 321 sbsh_init(void *xsc) 322 { 323 struct sbsh_softc *sc = xsc; 324 struct ifnet *ifp = &sc->arpcom.ac_if; 325 u_int8_t t; 326 327 if ((ifp->if_flags & IFF_RUNNING) || sc->state == NOT_LOADED) { 328 return; 329 } 330 331 bzero(&sc->in_stats, sizeof(struct sbni16_stats)); 332 sc->head_xq = sc->tail_xq = sc->head_rq = sc->tail_rq = 0; 333 sc->head_tdesc = sc->head_rdesc = 0; 334 335 sc->regs->IMR = EXT; 336 t = 2; 337 issue_cx28975_cmd(sc, _DSL_CLEAR_ERROR_CTRS, &t, 1); 338 if (issue_cx28975_cmd(sc, _DSL_ACTIVATION, &t, 1) == 0) { 339 sc->state = ACTIVATION; 340 341 ifp->if_flags |= IFF_RUNNING; 342 ifp->if_flags &= ~IFF_OACTIVE; 343 } 344 } 345 346 347 static void 348 sbsh_stop(struct sbsh_softc *sc) 349 { 350 u_int8_t t; 351 352 sc->regs->IMR = EXT; 353 354 t = 0; 355 issue_cx28975_cmd(sc, _DSL_ACTIVATION, &t, 1); 356 if (sc->state == ACTIVE) { 357 t = 1; 358 issue_cx28975_cmd(sc, _DSL_FORCE_DEACTIVATE, &t, 1); 359 /* FIX! activation manager state */ 360 361 /* Is it really must be done here? It calls from intr handler */ 362 deactivate(sc); 363 } 364 365 sc->regs->IMR = 0; 366 sc->state = DOWN; 367 } 368 369 370 static void 371 init_card(struct sbsh_softc *sc) 372 { 373 sc->state = NOT_LOADED; 374 sc->tbd = (struct hw_descr *) sc->mem_base; 375 sc->rbd = (struct hw_descr *) ((u_int8_t *)sc->mem_base + 0x400); 376 sc->regs = (struct sbni16_hw_regs *) ((u_int8_t *)sc->mem_base + 0x800); 377 sc->cmdp = (struct cx28975_cmdarea *) ((u_int8_t *)sc->mem_base + 0xc00); 378 379 sc->regs->CR = 0; 380 sc->regs->SR = 0xff; 381 sc->regs->IMR = 0; 382 } 383 384 385 static int 386 sbsh_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 387 { 388 struct sbsh_softc *sc = ifp->if_softc; 389 struct ifreq *ifr = (struct ifreq *) data; 390 struct cx28975_cfg cfg; 391 struct dsl_stats ds; 392 int error = 0; 393 u_int8_t t; 394 395 switch(cmd) { 396 case SIOCLOADFIRMW: 397 if ((error = suser_cred(cr, NULL_CRED_OKAY)) != 0) 398 break; 399 if (ifp->if_flags & IFF_UP) 400 error = EBUSY; 401 402 bcopy((caddr_t)ifr->ifr_data, (caddr_t)&cfg, sizeof cfg); 403 if (start_cx28975(sc, cfg) == 0) { 404 static char *modstr[] = { 405 "TCPAM32", "TCPAM16", "TCPAM8", "TCPAM4" }; 406 if_printf(&sc->arpcom.ac_if, "%s, rate %d, %s\n", 407 cfg.master ? "master" : "slave", 408 cfg.lrate << 3, modstr[cfg.mod]); 409 } else { 410 if_printf(&sc->arpcom.ac_if, 411 "unable to load firmware\n"); 412 error = EIO; 413 } 414 break; 415 416 case SIOCGETSTATS : 417 if ((error = suser_cred(cr, NULL_CRED_OKAY)) != 0) 418 break; 419 420 t = 0; 421 if (issue_cx28975_cmd(sc, _DSL_FAR_END_ATTEN, &t, 1)) 422 error = EIO; 423 ds.attenuat = sc->cmdp->out_data[0]; 424 425 if (issue_cx28975_cmd(sc, _DSL_NOISE_MARGIN, &t, 1)) 426 error = EIO; 427 ds.nmr = sc->cmdp->out_data[0]; 428 429 if (issue_cx28975_cmd(sc, _DSL_POWER_BACK_OFF_RESULT, &t, 1)) 430 error = EIO; 431 ds.tpbo = sc->cmdp->out_data[0]; 432 ds.rpbo = sc->cmdp->out_data[1]; 433 434 if (!issue_cx28975_cmd(sc, _DSL_HDSL_PERF_ERR_CTRS, &t, 1)) { 435 int i; 436 for (i = 0; i < 10; ++i) 437 ((u_int8_t *) &ds.losw)[i] = 438 sc->cmdp->out_data[i]; 439 } else 440 error = EIO; 441 442 ds.status_1 = ((volatile u_int8_t *)sc->cmdp)[0x3c0]; 443 ds.status_3 = ((volatile u_int8_t *)sc->cmdp)[0x3c2]; 444 445 bcopy(&sc->in_stats, ifr->ifr_data, sizeof(struct sbni16_stats)); 446 bcopy(&ds, (char *)ifr->ifr_data + sizeof(struct sbni16_stats), 447 sizeof(struct dsl_stats)); 448 break; 449 450 case SIOCCLRSTATS : 451 if (!(error = suser_cred(cr, NULL_CRED_OKAY))) { 452 bzero(&sc->in_stats, sizeof(struct sbni16_stats)); 453 t = 2; 454 if (issue_cx28975_cmd(sc, _DSL_CLEAR_ERROR_CTRS, &t, 1)) 455 error = EIO; 456 } 457 break; 458 case SIOCSIFFLAGS: 459 if (ifp->if_flags & IFF_UP) { 460 if (!(ifp->if_flags & IFF_RUNNING)) { 461 if (sc->state == NOT_LOADED) { 462 if_printf(ifp, "firmware wasn't loaded\n"); 463 error = EBUSY; 464 } else 465 sbsh_init(sc); 466 } 467 } else { 468 if (ifp->if_flags & IFF_RUNNING) { 469 sbsh_stop(sc); 470 ifp->if_flags &= ~IFF_RUNNING; 471 } 472 } 473 break; 474 475 case SIOCADDMULTI: 476 case SIOCDELMULTI: 477 error = 0; 478 break; 479 default: 480 error = ether_ioctl(ifp, cmd, data); 481 break; 482 } 483 return (error); 484 } 485 486 487 static void 488 sbsh_shutdown(device_t dev) 489 { 490 struct sbsh_softc *sc = device_get_softc(dev); 491 492 lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer); 493 sbsh_stop(sc); 494 lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer); 495 } 496 497 static int 498 sbsh_suspend(device_t dev) 499 { 500 struct sbsh_softc *sc = device_get_softc(dev); 501 502 lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer); 503 sbsh_stop(sc); 504 lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer); 505 506 return (0); 507 } 508 509 static int 510 sbsh_resume(device_t dev) 511 { 512 struct sbsh_softc *sc = device_get_softc(dev); 513 struct ifnet *ifp = &sc->arpcom.ac_if; 514 515 if (ifp->if_flags & IFF_UP) 516 sbsh_init(sc); 517 518 return (0); 519 } 520 521 522 static void 523 sbsh_watchdog(struct ifnet *ifp) 524 { 525 struct sbsh_softc *sc = ifp->if_softc; 526 527 if_printf(ifp, "transmit timeout\n"); 528 529 if (sc->regs->SR & TXS) { 530 sc->regs->SR = TXS; 531 if_printf(ifp, "interrupt posted but not delivered\n"); 532 } 533 free_sent_buffers(sc); 534 } 535 536 /* -------------------------------------------------------------------------- */ 537 538 static void 539 sbsh_intr(void *arg) 540 { 541 struct sbsh_softc *sc = (struct sbsh_softc *)arg; 542 u_int8_t status = sc->regs->SR; 543 544 if (status == 0) 545 return; 546 547 if (status & EXT) { 548 cx28975_interrupt(sc); 549 sc->regs->SR = EXT; 550 } 551 552 if (status & UFL) { 553 resume_tx(sc); 554 sc->regs->SR = UFL; 555 ++sc->in_stats.ufl_errs; 556 ++sc->arpcom.ac_if.if_oerrors; 557 } 558 559 if (status & RXS) { 560 sc->regs->SR = RXS; 561 indicate_frames(sc); 562 alloc_rx_buffers(sc); 563 } 564 565 if (status & TXS) { 566 sc->regs->SR = TXS; 567 free_sent_buffers(sc); 568 } 569 570 if (status & CRC) { 571 ++sc->in_stats.crc_errs; 572 ++sc->arpcom.ac_if.if_ierrors; 573 sc->regs->SR = CRC; 574 } 575 576 if (status & OFL) { 577 ++sc->in_stats.ofl_errs; 578 ++sc->arpcom.ac_if.if_ierrors; 579 sc->regs->SR = OFL; 580 } 581 } 582 583 /* 584 * Look for a first descriptor of a next packet, and write it's number 585 * into CTDR. Then enable the transmitter. 586 */ 587 static void 588 resume_tx(struct sbsh_softc *sc) 589 { 590 u_int32_t cur_tbd = sc->regs->CTDR; 591 592 while (cur_tbd != sc->regs->LTDR 593 && (sc->tbd[cur_tbd++].length & LAST_FRAG) == 0) 594 ; 595 sc->regs->CTDR = cur_tbd; 596 sc->regs->CR |= TXEN; 597 } 598 599 static void 600 start_xmit_frames(struct sbsh_softc *sc) 601 { 602 struct ifnet *ifp = &sc->arpcom.ac_if; 603 struct mbuf *m; 604 605 /* 606 * Check if we have any free descriptor(s) and free space in 607 * our transmit queue. 608 */ 609 while (sc->tail_xq != ((sc->head_xq - 1) & (XQLEN - 1)) 610 && sc->regs->LTDR != ((sc->head_tdesc - 1) & 0x7f)) { 611 612 m = ifq_dequeue(&ifp->if_snd, NULL); 613 if (m == NULL) 614 break; 615 if (m->m_pkthdr.len) { 616 BPF_MTAP(ifp, m); 617 encap_frame(sc, m); 618 } else 619 m_freem(m); 620 } 621 622 if (sc->regs->CTDR != sc->regs->LTDR) 623 ifp->if_flags |= IFF_OACTIVE; 624 else 625 ifp->if_flags &= ~IFF_OACTIVE; 626 } 627 628 629 static void 630 encap_frame(struct sbsh_softc *sc, struct mbuf *m_head) 631 { 632 struct mbuf *m; 633 u_int32_t cur_tbd; 634 int done; 635 636 look_for_nonzero: 637 for (m = m_head; !m->m_len; m = m->m_next) 638 ; 639 640 cur_tbd = sc->regs->LTDR & 0x7f; 641 done = 0; 642 do { 643 if (m->m_len < 5 || cur_tbd == ((sc->head_tdesc - 1) & 0x7f)) { 644 if ((m_head = repack(sc, m_head)) != NULL) 645 goto look_for_nonzero; 646 else 647 return; 648 } 649 650 sc->tbd[cur_tbd].address = vtophys(mtod(m, vm_offset_t)); 651 sc->tbd[cur_tbd].length = m->m_len; 652 653 do { 654 m = m->m_next; 655 } while (m && !m->m_len); 656 657 if (!m) { /* last fragment has been reached */ 658 sc->tbd[cur_tbd].length |= LAST_FRAG; 659 done = 1; 660 } 661 662 ++cur_tbd; 663 cur_tbd &= 0x7f; 664 } while (!done); 665 666 sc->xq[sc->tail_xq++] = m_head; 667 sc->tail_xq &= (XQLEN - 1); 668 669 sc->regs->LTDR = cur_tbd; 670 ++sc->in_stats.sent_pkts; 671 ++sc->arpcom.ac_if.if_opackets; 672 } 673 674 static struct mbuf * 675 repack(struct sbsh_softc *sc, struct mbuf *m) 676 { 677 struct mbuf *m_new; 678 679 MGETHDR(m_new, MB_DONTWAIT, MT_DATA); 680 if (!m_new) { 681 if_printf (&sc->arpcom.ac_if, 682 "unable to get mbuf.\n"); 683 return (NULL); 684 } 685 686 if (m->m_pkthdr.len > MHLEN) { 687 MCLGET(m_new, MB_DONTWAIT); 688 if (!(m_new->m_flags & M_EXT)) { 689 m_freem(m_new); 690 if_printf (&sc->arpcom.ac_if, 691 "unable to get mbuf cluster.\n"); 692 return (NULL); 693 } 694 } 695 696 m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, caddr_t)); 697 m_new->m_pkthdr.len = m_new->m_len = m->m_pkthdr.len; 698 m_freem(m); 699 return (m_new); 700 } 701 702 static void 703 free_sent_buffers(struct sbsh_softc *sc) 704 { 705 u_int32_t cur_tbd; 706 707 cur_tbd = sc->regs->CTDR; 708 709 while (sc->head_tdesc != cur_tbd) { 710 /* 711 * Be careful! one element in xq may correspond to 712 * multiple descriptors. 713 */ 714 if (sc->tbd[sc->head_tdesc].length & LAST_FRAG) { 715 m_freem(sc->xq[sc->head_xq++]); 716 sc->head_xq &= (XQLEN - 1); 717 } 718 719 sc->tbd[sc->head_tdesc].length = 0; 720 sc->head_tdesc = (sc->head_tdesc + 1) & 0x7f; 721 } 722 723 start_xmit_frames(sc); 724 } 725 726 /* 727 * DON'T use free_sent_buffers to drop the queue! 728 */ 729 static void 730 alloc_rx_buffers(struct sbsh_softc *sc) 731 { 732 unsigned cur_rbd = sc->regs->LRDR & 0x7f; 733 struct mbuf *m; 734 735 while (sc->tail_rq != ((sc->head_rq - 1) & (RQLEN - 1))) { 736 MGETHDR(m, MB_DONTWAIT, MT_DATA); 737 if (!m) { 738 if_printf (&sc->arpcom.ac_if, 739 "unable to get mbuf.\n"); 740 return; 741 } 742 743 if (SBNI16_MAX_FRAME > MHLEN) { 744 MCLGET(m, MB_DONTWAIT); 745 if (!(m->m_flags & M_EXT)) { 746 m_freem(m); 747 if_printf (&sc->arpcom.ac_if, 748 "unable to get mbuf cluster.\n"); 749 return; 750 } 751 m->m_pkthdr.len = m->m_len = MCLBYTES; 752 } 753 754 m_adj(m, 2); /* align ip on longword boundaries */ 755 756 sc->rq[sc->tail_rq++] = m; 757 sc->tail_rq &= (RQLEN - 1); 758 759 sc->rbd[cur_rbd].address = vtophys(mtod(m, vm_offset_t)); 760 sc->rbd[cur_rbd].length = 0; 761 sc->regs->LRDR = cur_rbd = (cur_rbd + 1) & 0x7f; 762 } 763 } 764 765 static void 766 indicate_frames(struct sbsh_softc *sc) 767 { 768 struct ifnet *ifp = &sc->arpcom.ac_if; 769 unsigned cur_rbd = sc->regs->CRDR & 0x7f; 770 771 while (sc->head_rdesc != cur_rbd) { 772 struct mbuf *m = sc->rq[sc->head_rq++]; 773 sc->head_rq &= (RQLEN - 1); 774 775 m->m_pkthdr.len = m->m_len = 776 sc->rbd[sc->head_rdesc].length & 0x7ff; 777 m->m_pkthdr.rcvif = ifp; 778 779 ifp->if_input(ifp, m); 780 ++sc->in_stats.rcvd_pkts; 781 ++ifp->if_ipackets; 782 783 sc->head_rdesc = (sc->head_rdesc + 1) & 0x7f; 784 } 785 } 786 787 static void 788 drop_queues(struct sbsh_softc *sc) 789 { 790 while (sc->head_rq != sc->tail_rq) { 791 m_freem(sc->rq[sc->head_rq++]); 792 sc->head_rq &= (RQLEN - 1); 793 } 794 795 while (sc->head_xq != sc->tail_xq) { 796 m_freem(sc->xq[sc->head_xq++]); 797 sc->head_xq &= (XQLEN - 1); 798 } 799 } 800 801 /* -------------------------------------------------------------------------- */ 802 803 static void 804 activate(struct sbsh_softc *sc) 805 { 806 struct timeval tv; 807 808 sc->regs->SR = 0xff; /* clear it! */ 809 sc->regs->CTDR = sc->regs->LTDR = sc->regs->CRDR = sc->regs->LRDR = 0; 810 811 sc->head_tdesc = sc->head_rdesc = 0; 812 alloc_rx_buffers(sc); 813 814 sc->regs->CRB &= ~RXDE; 815 sc->regs->IMR = EXT | RXS | TXS | CRC | OFL | UFL; 816 sc->regs->CR |= TXEN | RXEN; 817 818 sc->state = ACTIVE; 819 ++sc->in_stats.attempts; 820 microtime(&tv); 821 sc->in_stats.last_time = tv.tv_sec; 822 start_xmit_frames(sc); 823 } 824 825 static void 826 deactivate(struct sbsh_softc *sc) 827 { 828 sc->regs->CR &= ~(RXEN | TXEN); 829 sc->regs->CRB |= RXDE; 830 sc->regs->IMR = EXT; 831 sc->regs->CTDR = sc->regs->LTDR; 832 sc->regs->CRDR = sc->regs->LRDR; 833 sc->state = ACTIVATION; 834 835 drop_queues(sc); 836 } 837 838 /* -------------------------------------------------------------------------- */ 839 840 static void 841 cx28975_interrupt(struct sbsh_softc *sc) 842 { 843 volatile struct cx28975_cmdarea *p = sc->cmdp; 844 u_int8_t t; 845 846 if (p->intr_host != 0xfe) 847 return; 848 849 if (p->out_ack & 0x80) { 850 if (*((volatile u_int8_t *)p + 0x3c7) & 2) { 851 if (sc->state != ACTIVE 852 && (*((volatile u_int8_t *)p + 0x3c0) & 0xc0) == 0x40) { 853 activate(sc); 854 if_printf(&sc->arpcom.ac_if, "connected to peer\n"); 855 } else if (sc->state == ACTIVE 856 && (*((volatile u_int8_t *)p + 0x3c0) & 0xc0) != 0x40) { 857 deactivate(sc); 858 if_printf(&sc->arpcom.ac_if, "carrier lost\n"); 859 } 860 } 861 862 p->intr_host = 0; 863 t = p->intr_host; 864 p->out_ack = 0; 865 } else { 866 wakeup(sc); 867 868 p->intr_host = 0; 869 t = p->intr_host; 870 } 871 } 872 873 /* -------------------------------------------------------------------------- */ 874 875 static int 876 start_cx28975(struct sbsh_softc *sc, struct cx28975_cfg cfg) 877 { 878 static char thresh[] = { +8, -4, -16, -40 }; 879 880 volatile struct cx28975_cmdarea *p = sc->cmdp; 881 u_int8_t t, parm[12]; 882 883 p->intr_host = 0; 884 t = p->intr_host; 885 886 /* reset chip set */ 887 sc->regs->IMR = EXT; 888 sc->regs->CR = 0; 889 sc->regs->SR = 0xff; 890 DELAY(2); 891 sc->regs->CR = XRST; 892 if (cfg.crc16) 893 sc->regs->CR |= CMOD; 894 if (cfg.fill_7e) 895 sc->regs->CR |= FMOD; 896 if (cfg.inv) 897 sc->regs->CR |= PMOD; 898 899 sc->regs->CRB |= RODD | RXDE; 900 if (cfg.rburst) 901 sc->regs->CRB |= RDBE; 902 if (cfg.wburst) 903 sc->regs->CRB |= WTBE; 904 905 tsleep(sc, 0, "sbsh", 0); 906 if ((p->out_ack & 0x1f) != _ACK_BOOT_WAKE_UP) 907 return (-1); 908 909 if (download_firmware(sc, cfg.firmw_image, cfg.firmw_len)) 910 return (-1); 911 912 tsleep(sc, 0, "sbsh", 0); 913 if ((p->out_ack & 0x1f) != _ACK_OPER_WAKE_UP) 914 return (-1); 915 916 t = cfg.master ? 1 : 9; 917 if (issue_cx28975_cmd(sc, _DSL_SYSTEM_ENABLE, &t, 1)) 918 return (-1); 919 920 t = 0x63; 921 if (issue_cx28975_cmd(sc, _DSL_SYSTEM_CONFIG, &t, 1)) 922 return (-1); 923 924 *(u_int16_t *)parm = cfg.lrate >> 3; 925 parm[2] = parm[3] = parm[0]; 926 parm[5] = cfg.lrate & 7; 927 parm[4] = parm[7] = 1; 928 parm[6] = 0; 929 if (issue_cx28975_cmd(sc, _DSL_MULTI_RATE_CONFIG, parm, 8)) 930 return (-1); 931 932 parm[0] = 0x02 | (cfg.mod << 4); 933 parm[1] = 0; 934 if (issue_cx28975_cmd(sc, _DSL_TRAINING_MODE, parm, 2)) 935 return (-1); 936 937 bzero(parm, 12); 938 parm[0] = 0x04; /* pre-activation: G.hs */ 939 parm[4] = 0x04; /* no remote configuration */ 940 parm[7] = 0x01; /* annex A (default) */ 941 parm[8] = 0xff; /* i-bit mask (all bits) */ 942 if (issue_cx28975_cmd(sc, _DSL_PREACTIVATION_CFG, parm, 12)) 943 return (-1); 944 945 parm[0] = 0x03; /* dying gasp time - 3 frames */ 946 parm[1] = thresh[cfg.mod]; 947 parm[2] = 0xff; /* attenuation */ 948 parm[3] = 0x04; /* line probe NMR (+2 dB) */ 949 parm[4] = 0x00; /* reserved */ 950 parm[5] = 0x00; 951 if (issue_cx28975_cmd(sc, _DSL_THRESHOLDS, parm, 6)) 952 return (-1); 953 954 t = cfg.master ? 0x23 : 0x21; 955 if (issue_cx28975_cmd(sc, _DSL_FR_PCM_CONFIG, &t, 1)) 956 return (-1); 957 958 t = 0x02; 959 if (issue_cx28975_cmd(sc, _DSL_INTR_HOST_MASK, &t, 1)) 960 return (-1); 961 962 sc->state = DOWN; 963 return (0); 964 } 965 966 static int 967 download_firmware(struct sbsh_softc *sc, u_int8_t *img, u_int32_t img_len) 968 { 969 u_int32_t t; 970 int i; 971 u_int8_t cksum = 0; 972 973 for (i = 0; i < img_len; ++i) 974 cksum += img[i]; 975 976 t = img_len; 977 if (issue_cx28975_cmd(sc, _DSL_DOWNLOAD_START, (u_int8_t *) &t, 4)) 978 return (-1); 979 980 for (i = 0; img_len >= 75; i += 75, img_len -= 75) { 981 if (issue_cx28975_cmd(sc, _DSL_DOWNLOAD_DATA, img + i, 75)) 982 return (-1); 983 } 984 985 if (img_len 986 && issue_cx28975_cmd(sc, _DSL_DOWNLOAD_DATA, img + i, img_len)) 987 return (-1); 988 989 t = (cksum ^ 0xff) + 1; 990 if (issue_cx28975_cmd(sc, _DSL_DOWNLOAD_END, (u_int8_t *) &t, 1)) 991 return (-1); 992 993 return (0); 994 } 995 996 static int 997 issue_cx28975_cmd(struct sbsh_softc *sc, u_int8_t cmd, 998 u_int8_t *data, u_int8_t size) 999 { 1000 volatile struct cx28975_cmdarea *p = sc->cmdp; 1001 u_int8_t *databuf = p->in_data; 1002 int i; 1003 1004 u_int8_t cksum = 0; 1005 1006 p->in_dest = 0xf0; 1007 p->in_opcode = cmd; 1008 p->in_zero = 0; 1009 p->in_length = --size; 1010 p->in_csum = 0xf0 ^ cmd ^ size ^ 0xaa; 1011 1012 for (i = 0; i <= size; ++i) { 1013 cksum ^= *data; 1014 *databuf++ = *data++; /* only 1 byte per cycle! */ 1015 } 1016 1017 p->in_datasum = cksum ^ 0xaa; 1018 p->out_ack = _ACK_NOT_COMPLETE; 1019 p->intr_8051 = 0xfe; 1020 1021 if (tsleep(sc, 0, "sbsh", hz << 3)) 1022 return (-1); 1023 1024 while (p->out_ack == _ACK_NOT_COMPLETE) 1025 ; /* FIXME ! */ 1026 1027 if ((p->out_ack & 0x1f) == _ACK_PASS) { 1028 p->out_ack = 0; 1029 return (0); 1030 } else { 1031 p->out_ack = 0; 1032 return (-1); 1033 } 1034 } 1035