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