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