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