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