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