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