Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / sys / dev / netif / ar / if_ar.c
1 /*-
2  * Copyright (c) 1995 - 2001 John Hay.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the author nor the names of any co-contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY John Hay ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL John Hay BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/dev/ar/if_ar.c,v 1.66 2005/01/06 01:42:28 imp Exp $
29  * $DragonFly: src/sys/dev/netif/ar/if_ar.c,v 1.22 2007/06/03 20:51:07 dillon Exp $
30  */
31
32 /*
33  * Programming assumptions and other issues.
34  *
35  * The descriptors of a DMA channel will fit in a 16K memory window.
36  *
37  * The buffers of a transmit DMA channel will fit in a 16K memory window.
38  *
39  * Only the ISA bus cards with X.21 and V.35 is tested.
40  *
41  * When interface is going up, handshaking is set and it is only cleared
42  * when the interface is down'ed.
43  *
44  * There should be a way to set/reset Raw HDLC/PPP, Loopback, DCE/DTE,
45  * internal/external clock, etc.....
46  */
47
48 #include "opt_netgraph.h"
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mbuf.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/module.h>
58 #include <sys/bus.h>
59 #include <sys/serialize.h>
60 #include <sys/rman.h>
61 #include <sys/thread2.h>
62
63 #include <net/if.h>
64 #ifdef NETGRAPH
65 #include <netgraph/ng_message.h>
66 #include <netgraph/netgraph.h>
67 #include <sys/syslog.h>
68 #include <dev/netif/ar/if_ar.h>
69 #else /* NETGRAPH */
70 #include <net/sppp/if_sppp.h>
71 #include <net/bpf.h>
72 #endif /* NETGRAPH */
73
74 #include <machine/md_var.h>
75
76 #include <dev/netif/ic_layer/hd64570.h>
77 #include <dev/netif/ar/if_arregs.h>
78
79 #ifdef TRACE
80 #define TRC(x)               x
81 #else
82 #define TRC(x)
83 #endif
84
85 #define TRCL(x)              x
86
87 #define PPP_HEADER_LEN       4
88
89 devclass_t ar_devclass;
90
91 struct ar_softc {
92 #ifndef NETGRAPH
93         struct sppp ifsppp;
94 #endif /* NETGRAPH */
95         int unit;            /* With regards to all ar devices */
96         int subunit;         /* With regards to this card */
97         struct ar_hardc *hc;
98
99         struct buf_block {
100                 u_int txdesc;        /* On card address */
101                 u_int txstart;       /* On card address */
102                 u_int txend;         /* On card address */
103                 u_int txtail;        /* Index of first unused buffer */
104                 u_int txmax;         /* number of usable buffers/descriptors */
105                 u_int txeda;         /* Error descriptor addresses */
106         }block[AR_TX_BLOCKS];
107
108         char  xmit_busy;     /* Transmitter is busy */
109         char  txb_inuse;     /* Number of tx blocks currently in use */
110         u_char txb_new;      /* Index to where new buffer will be added */
111         u_char txb_next_tx;  /* Index to next block ready to tx */
112
113         u_int rxdesc;        /* On card address */
114         u_int rxstart;       /* On card address */
115         u_int rxend;         /* On card address */
116         u_int rxhind;        /* Index to the head of the rx buffers. */
117         u_int rxmax;         /* number of usable buffers/descriptors */
118
119         int scano;
120         int scachan;
121         sca_regs *sca;
122 #ifdef NETGRAPH
123         int     running;        /* something is attached so we are running */
124         int     dcd;            /* do we have dcd? */
125         /* ---netgraph bits --- */
126         char            nodename[NG_NODESIZ]; /* store our node name */
127         int             datahooks;      /* number of data hooks attached */
128         node_p          node;           /* netgraph node */
129         hook_p          hook;           /* data hook */
130         hook_p          debug_hook;
131         struct ifqueue  xmitq_hipri;    /* hi-priority transmit queue */
132         struct ifqueue  xmitq;          /* transmit queue */
133         int             flags;          /* state */
134 #define SCF_RUNNING     0x01            /* board is active */
135 #define SCF_OACTIVE     0x02            /* output is active */
136         int             out_dog;        /* watchdog cycles output count-down */
137         struct callout  timer;          /* watchdog timer */
138         u_long          inbytes, outbytes;      /* stats */
139         u_long          lastinbytes, lastoutbytes; /* a second ago */
140         u_long          inrate, outrate;        /* highest rate seen */
141         u_long          inlast;         /* last input N secs ago */
142         u_long          out_deficit;    /* output since last input */
143         u_long          oerrors, ierrors[6];
144         u_long          opackets, ipackets;
145 #endif /* NETGRAPH */
146 };
147
148 static int      next_ar_unit = 0;
149 static struct lwkt_serialize ar_serializer;
150
151 #ifdef NETGRAPH
152 #define DOG_HOLDOFF     6       /* dog holds off for 6 secs */
153 #define QUITE_A_WHILE   300     /* 5 MINUTES */
154 #define LOTS_OF_PACKETS 100
155 #endif /* NETGRAPH */
156
157 /*
158  * This translate from irq numbers to
159  * the value that the arnet card needs
160  * in the lower part of the AR_INT_SEL
161  * register.
162  */
163 static int irqtable[16] = {
164         0,      /*  0 */
165         0,      /*  1 */
166         0,      /*  2 */
167         1,      /*  3 */
168         0,      /*  4 */
169         2,      /*  5 */
170         0,      /*  6 */
171         3,      /*  7 */
172         0,      /*  8 */
173         0,      /*  9 */
174         4,      /* 10 */
175         5,      /* 11 */
176         6,      /* 12 */
177         0,      /* 13 */
178         0,      /* 14 */
179         7       /* 15 */
180 };
181
182 #ifndef NETGRAPH
183 DECLARE_DUMMY_MODULE(if_ar);
184 MODULE_DEPEND(if_ar, sppp, 1, 1, 1);
185 #else
186 MODULE_DEPEND(ng_sync_ar, netgraph, 1, 1, 1);
187 #endif
188
189 static void arintr(void *arg);
190 static void ar_xmit(struct ar_softc *sc);
191 #ifndef NETGRAPH
192 static void arstart(struct ifnet *ifp);
193 static int arioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *);
194 static void arwatchdog(struct ifnet *ifp);
195 #else   /* NETGRAPH */
196 static void arstart(struct ar_softc *sc);
197 static void arwatchdog(struct ar_softc *sc);
198 #endif  /* NETGRAPH */
199 static int ar_packet_avail(struct ar_softc *sc, int *len, u_char *rxstat);
200 static void ar_copy_rxbuf(struct mbuf *m, struct ar_softc *sc, int len);
201 static void ar_eat_packet(struct ar_softc *sc, int single);
202 static void ar_get_packets(struct ar_softc *sc);
203
204 static int ar_read_pim_iface(volatile struct ar_hardc *hc, int channel);
205 static void ar_up(struct ar_softc *sc);
206 static void ar_down(struct ar_softc *sc);
207 static void arc_init(struct ar_hardc *hc);
208 static void ar_init_sca(struct ar_hardc *hc, int scano);
209 static void ar_init_msci(struct ar_softc *sc);
210 static void ar_init_rx_dmac(struct ar_softc *sc);
211 static void ar_init_tx_dmac(struct ar_softc *sc);
212 static void ar_dmac_intr(struct ar_hardc *hc, int scano, u_char isr);
213 static void ar_msci_intr(struct ar_hardc *hc, int scano, u_char isr);
214 static void ar_timer_intr(struct ar_hardc *hc, int scano, u_char isr);
215
216 #ifdef  NETGRAPH
217 static  void    ngar_watchdog_frame(void * arg);
218 static  void    ngar_init(void* ignored);
219
220 static ng_constructor_t ngar_constructor;
221 static ng_rcvmsg_t      ngar_rcvmsg;
222 static ng_shutdown_t    ngar_shutdown;
223 static ng_newhook_t     ngar_newhook;
224 /*static ng_findhook_t  ngar_findhook; */
225 static ng_connect_t     ngar_connect;
226 static ng_rcvdata_t     ngar_rcvdata;
227 static ng_disconnect_t  ngar_disconnect;
228         
229 static struct ng_type typestruct = {
230         NG_VERSION,
231         NG_AR_NODE_TYPE,
232         NULL,
233         ngar_constructor,
234         ngar_rcvmsg,
235         ngar_shutdown,
236         ngar_newhook,
237         NULL,
238         ngar_connect,
239         ngar_rcvdata,
240         ngar_rcvdata,
241         ngar_disconnect,
242         NULL
243 };
244
245 static int      ngar_done_init = 0;
246 #endif /* NETGRAPH */
247
248 int
249 ar_attach(device_t device)
250 {
251         struct ar_hardc *hc;
252         struct ar_softc *sc;
253 #ifndef NETGRAPH
254         struct ifnet *ifp;
255         char *iface;
256 #endif  /* NETGRAPH */
257         int unit;
258         int error;
259
260         hc = (struct ar_hardc *)device_get_softc(device);
261         lwkt_serialize_init(&ar_serializer);
262
263         kprintf("arc%d: %uK RAM, %u ports, rev %u.\n",
264                 hc->cunit,
265                 hc->memsize/1024,
266                 hc->numports,
267                 hc->revision);
268         
269         arc_init(hc);
270
271         error = BUS_SETUP_INTR(device_get_parent(device), device, hc->res_irq,
272                                0, arintr, hc,
273                                &hc->intr_cookie, &ar_serializer);
274         if (error)
275                 return (1);
276
277         sc = hc->sc;
278
279         for(unit=0;unit<hc->numports;unit+=NCHAN)
280                 ar_init_sca(hc, unit / NCHAN);
281
282         /*
283          * Now configure each port on the card.
284          */
285         for(unit=0;unit<hc->numports;sc++,unit++) {
286                 sc->hc = hc;
287                 sc->subunit = unit;
288                 sc->unit = next_ar_unit;
289                 next_ar_unit++;
290                 sc->scano = unit / NCHAN;
291                 sc->scachan = unit%NCHAN;
292
293                 ar_init_rx_dmac(sc);
294                 ar_init_tx_dmac(sc);
295                 ar_init_msci(sc);
296
297 #ifndef NETGRAPH
298                 ifp = &sc->ifsppp.pp_if;
299
300                 ifp->if_softc = sc;
301                 if_initname(ifp, device_get_name(device), sc->unit);
302                 ifp->if_mtu = PP_MTU;
303                 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
304                 ifp->if_ioctl = arioctl;
305                 ifp->if_start = arstart;
306                 ifp->if_watchdog = arwatchdog;
307
308                 sc->ifsppp.pp_flags = PP_KEEPALIVE;
309
310                 switch(hc->interface[unit]) {
311                 default: iface = "UNKNOWN"; break;
312                 case AR_IFACE_EIA_232: iface = "EIA-232"; break;
313                 case AR_IFACE_V_35: iface = "EIA-232 or V.35"; break;
314                 case AR_IFACE_EIA_530: iface = "EIA-530"; break;
315                 case AR_IFACE_X_21: iface = "X.21"; break;
316                 case AR_IFACE_COMBO: iface = "COMBO X.21 / EIA-530"; break;
317                 }
318
319                 kprintf("ar%d: Adapter %d, port %d, interface %s.\n",
320                         sc->unit,
321                         hc->cunit,
322                         sc->subunit,
323                         iface);
324
325                 sppp_attach((struct ifnet *)&sc->ifsppp);
326                 if_attach(ifp, &ar_serializer);
327
328                 bpfattach(ifp, DLT_PPP, PPP_HEADER_LEN);
329 #else   /* NETGRAPH */
330                 /*
331                  * we have found a node, make sure our 'type' is availabe.
332                  */
333                 if (ngar_done_init == 0) ngar_init(NULL);
334                 if (ng_make_node_common(&typestruct, &sc->node) != 0)
335                         return (1);
336                 ksprintf(sc->nodename, "%s%d", NG_AR_NODE_TYPE, sc->unit);
337                 if (ng_name_node(sc->node, sc->nodename)) {
338                         NG_NODE_UNREF(sc->node); /* drop it again */
339                         return (1);
340                 }
341                 NG_NODE_SET_PRIVATE(sc->node, sc);
342                 callout_init(&sc->timer);
343                 sc->xmitq.ifq_maxlen = IFQ_MAXLEN;
344                 sc->xmitq_hipri.ifq_maxlen = IFQ_MAXLEN;
345                 sc->running = 0;
346 #endif  /* NETGRAPH */
347         }
348
349         if(hc->bustype == AR_BUS_ISA)
350                 ARC_SET_OFF(hc);
351
352         return (0);
353 }
354
355 int
356 ar_detach(device_t device)
357 {
358         device_t parent = device_get_parent(device);
359         struct ar_hardc *hc = device_get_softc(device);
360         int error;
361
362         lwkt_serialize_enter(&ar_serializer);
363
364         if (hc->intr_cookie != NULL) {
365                 if (BUS_TEARDOWN_INTR(parent, device,
366                         hc->res_irq, hc->intr_cookie) != 0) {
367                                 kprintf("intr teardown failed.. continuing\n");
368                 }
369                 hc->intr_cookie = NULL;
370         }
371
372         /*
373          * deallocate any system resources we may have
374          * allocated on behalf of this driver.
375          */
376         FREE(hc->sc, M_DEVBUF);
377         hc->sc = NULL;
378         hc->mem_start = NULL;
379         error = ar_deallocate_resources(device);
380         lwkt_serialize_exit(&ar_serializer);
381
382         return (error);
383 }
384
385 int
386 ar_allocate_ioport(device_t device, int rid, u_long size)
387 {
388         struct ar_hardc *hc = device_get_softc(device);
389
390         hc->rid_ioport = rid;
391         hc->res_ioport = bus_alloc_resource(device, SYS_RES_IOPORT,
392                         &hc->rid_ioport, 0ul, ~0ul, size, RF_ACTIVE);
393         if (hc->res_ioport == NULL) {
394                 goto errexit;
395         }
396         hc->bt = rman_get_bustag(hc->res_ioport);
397         hc->bh = rman_get_bushandle(hc->res_ioport);
398
399         return (0);
400
401 errexit:
402         ar_deallocate_resources(device);
403         return (ENXIO);
404 }
405
406 int
407 ar_allocate_irq(device_t device, int rid, u_long size)
408 {
409         struct ar_hardc *hc = device_get_softc(device);
410
411         hc->rid_irq = rid;
412         hc->res_irq = bus_alloc_resource_any(device, SYS_RES_IRQ,
413                         &hc->rid_irq, RF_SHAREABLE|RF_ACTIVE);
414         if (hc->res_irq == NULL) {
415                 goto errexit;
416         }
417         return (0);
418
419 errexit:
420         ar_deallocate_resources(device);
421         return (ENXIO);
422 }
423
424 int
425 ar_allocate_memory(device_t device, int rid, u_long size)
426 {
427         struct ar_hardc *hc = device_get_softc(device);
428
429         hc->rid_memory = rid;
430         hc->res_memory = bus_alloc_resource(device, SYS_RES_MEMORY,
431                         &hc->rid_memory, 0ul, ~0ul, size, RF_ACTIVE);
432         if (hc->res_memory == NULL) {
433                 goto errexit;
434         }
435         return (0);
436
437 errexit:
438         ar_deallocate_resources(device);
439         return (ENXIO);
440 }
441
442 int
443 ar_allocate_plx_memory(device_t device, int rid, u_long size)
444 {
445         struct ar_hardc *hc = device_get_softc(device);
446
447         hc->rid_plx_memory = rid;
448         hc->res_plx_memory = bus_alloc_resource(device, SYS_RES_MEMORY,
449                         &hc->rid_plx_memory, 0ul, ~0ul, size, RF_ACTIVE);
450         if (hc->res_plx_memory == NULL) {
451                 goto errexit;
452         }
453         return (0);
454
455 errexit:
456         ar_deallocate_resources(device);
457         return (ENXIO);
458 }
459
460 int
461 ar_deallocate_resources(device_t device)
462 {
463         struct ar_hardc *hc = device_get_softc(device);
464
465         if (hc->res_irq != 0) {
466                 bus_deactivate_resource(device, SYS_RES_IRQ,
467                         hc->rid_irq, hc->res_irq);
468                 bus_release_resource(device, SYS_RES_IRQ,
469                         hc->rid_irq, hc->res_irq);
470                 hc->res_irq = 0;
471         }
472         if (hc->res_ioport != 0) {
473                 bus_deactivate_resource(device, SYS_RES_IOPORT,
474                         hc->rid_ioport, hc->res_ioport);
475                 bus_release_resource(device, SYS_RES_IOPORT,
476                         hc->rid_ioport, hc->res_ioport);
477                 hc->res_ioport = 0;
478         }
479         if (hc->res_memory != 0) {
480                 bus_deactivate_resource(device, SYS_RES_MEMORY,
481                         hc->rid_memory, hc->res_memory);
482                 bus_release_resource(device, SYS_RES_MEMORY,
483                         hc->rid_memory, hc->res_memory);
484                 hc->res_memory = 0;
485         }
486         if (hc->res_plx_memory != 0) {
487                 bus_deactivate_resource(device, SYS_RES_MEMORY,
488                         hc->rid_plx_memory, hc->res_plx_memory);
489                 bus_release_resource(device, SYS_RES_MEMORY,
490                         hc->rid_plx_memory, hc->res_plx_memory);
491                 hc->res_plx_memory = 0;
492         }
493         return (0);
494 }
495
496 /*
497  * First figure out which SCA gave the interrupt.
498  * Process it.
499  * See if there is other interrupts pending.
500  * Repeat until there is no more interrupts.
501  */
502 static void
503 arintr(void *arg)
504 {
505         struct ar_hardc *hc = (struct ar_hardc *)arg;
506         sca_regs *sca;
507         u_char isr0, isr1, isr2, arisr;
508         int scano;
509
510         /* XXX Use the PCI interrupt score board register later */
511         if(hc->bustype == AR_BUS_PCI)
512                 arisr = hc->orbase[AR_ISTAT * 4];
513         else
514                 arisr = ar_inb(hc, AR_ISTAT);
515
516         while(arisr & AR_BD_INT) {
517                 TRC(kprintf("arisr = %x\n", arisr));
518                 if(arisr & AR_INT_0)
519                         scano = 0;
520                 else if(arisr & AR_INT_1)
521                         scano = 1;
522                 else {
523                         /* XXX Oops this shouldn't happen. */
524                         kprintf("arc%d: Interrupted with no interrupt.\n",
525                                 hc->cunit);
526                         return;
527                 }
528                 sca = hc->sca[scano];
529
530                 if(hc->bustype == AR_BUS_ISA)
531                         ARC_SET_SCA(hc, scano);
532
533                 isr0 = sca->isr0;
534                 isr1 = sca->isr1;
535                 isr2 = sca->isr2;
536
537                 TRC(kprintf("arc%d: ARINTR isr0 %x, isr1 %x, isr2 %x\n",
538                         hc->cunit,
539                         isr0,
540                         isr1,
541                         isr2));
542                 if(isr0)
543                         ar_msci_intr(hc, scano, isr0);
544
545                 if(isr1)
546                         ar_dmac_intr(hc, scano, isr1);
547
548                 if(isr2)
549                         ar_timer_intr(hc, scano, isr2);
550
551                 /*
552                  * Proccess the second sca's interrupt if available.
553                  * Else see if there are any new interrupts.
554                  */
555                 if((arisr & AR_INT_0) && (arisr & AR_INT_1))
556                         arisr &= ~AR_INT_0;
557                 else {
558                         if(hc->bustype == AR_BUS_PCI)
559                                 arisr = hc->orbase[AR_ISTAT * 4];
560                         else
561                                 arisr = ar_inb(hc, AR_ISTAT);
562                 }
563         }
564
565         if(hc->bustype == AR_BUS_ISA)
566                 ARC_SET_OFF(hc);
567 }
568
569
570 /*
571  * This will only start the transmitter. It is assumed that the data
572  * is already there. It is normally called from arstart() or ar_dmac_intr().
573  *
574  */
575 static void
576 ar_xmit(struct ar_softc *sc)
577 {
578 #ifndef NETGRAPH
579         struct ifnet *ifp;
580 #endif /* NETGRAPH */
581         dmac_channel *dmac;
582
583 #ifndef NETGRAPH
584         ifp = &sc->ifsppp.pp_if;
585 #endif /* NETGRAPH */
586         dmac = &sc->sca->dmac[DMAC_TXCH(sc->scachan)];
587
588         if(sc->hc->bustype == AR_BUS_ISA)
589                 ARC_SET_SCA(sc->hc, sc->scano);
590         dmac->cda = (u_short)(sc->block[sc->txb_next_tx].txdesc & 0xffff);
591
592         dmac->eda = (u_short)(sc->block[sc->txb_next_tx].txeda & 0xffff);
593         dmac->dsr = SCA_DSR_DE;
594
595         sc->xmit_busy = 1;
596
597         sc->txb_next_tx++;
598         if(sc->txb_next_tx == AR_TX_BLOCKS)
599                 sc->txb_next_tx = 0;
600
601 #ifndef NETGRAPH
602         ifp->if_timer = 2; /* Value in seconds. */
603 #else   /* NETGRAPH */
604         sc->out_dog = DOG_HOLDOFF;      /* give ourself some breathing space*/
605 #endif  /* NETGRAPH */
606         if(sc->hc->bustype == AR_BUS_ISA)
607                 ARC_SET_OFF(sc->hc);
608 }
609
610 /*
611  * This function will be called from the upper level when a user add a
612  * packet to be send, and from the interrupt handler after a finished
613  * transmit.
614  *
615  * This function only place the data in the oncard buffers. It does not
616  * start the transmition. ar_xmit() does that.
617  *
618  * Transmitter idle state is indicated by the IFF_OACTIVE flag. The function
619  * that clears that should ensure that the transmitter and its DMA is
620  * in a "good" idle state.
621  */
622 #ifndef NETGRAPH
623 static void
624 arstart(struct ifnet *ifp)
625 {
626         struct ar_softc *sc = ifp->if_softc;
627 #else   /* NETGRAPH */
628 static void
629 arstart(struct ar_softc *sc)
630 {
631 #endif  /* NETGRAPH */
632         int i, len, tlen;
633         struct mbuf *mtx;
634         u_char *txdata;
635         sca_descriptor *txdesc;
636         struct buf_block *blkp;
637
638 #ifndef NETGRAPH
639         if(!(ifp->if_flags & IFF_RUNNING))
640                 return;
641 #else   /* NETGRAPH */
642 /* XXX */
643 #endif  /* NETGRAPH */
644   
645 top_arstart:
646
647         /*
648          * See if we have space for more packets.
649          */
650         if(sc->txb_inuse == AR_TX_BLOCKS) {
651 #ifndef NETGRAPH
652                 ifp->if_flags |= IFF_OACTIVE;   /* yes, mark active */
653 #else   /* NETGRAPH */
654 /*XXX*/         /*ifp->if_flags |= IFF_OACTIVE;*/       /* yes, mark active */
655 #endif /* NETGRAPH */
656                 return;
657         }
658
659 #ifndef NETGRAPH
660         mtx = sppp_dequeue(ifp);
661 #else   /* NETGRAPH */
662         IF_DEQUEUE(&sc->xmitq_hipri, mtx);
663         if (mtx == NULL) {
664                 IF_DEQUEUE(&sc->xmitq, mtx);
665         }
666 #endif /* NETGRAPH */
667         if(!mtx)
668                 return;
669
670         /*
671          * It is OK to set the memory window outside the loop because
672          * all tx buffers and descriptors are assumed to be in the same
673          * 16K window.
674          */
675         if(sc->hc->bustype == AR_BUS_ISA)
676                 ARC_SET_MEM(sc->hc, sc->block[0].txdesc);
677
678         /*
679          * We stay in this loop until there is nothing in the
680          * TX queue left or the tx buffer is full.
681          */
682         i = 0;
683         blkp = &sc->block[sc->txb_new];
684         txdesc = (sca_descriptor *)
685                 (sc->hc->mem_start + (blkp->txdesc & sc->hc->winmsk));
686         txdata = (u_char *)(sc->hc->mem_start + (blkp->txstart & sc->hc->winmsk));
687         for(;;) {
688                 len = mtx->m_pkthdr.len;
689
690                 TRC(kprintf("ar%d: ARstart len %u\n", sc->unit, len));
691
692                 /*
693                  * We can do this because the tx buffers don't wrap.
694                  */
695                 m_copydata(mtx, 0, len, txdata);
696                 tlen = len;
697                 while(tlen > AR_BUF_SIZ) {
698                         txdesc->stat = 0;
699                         txdesc->len = AR_BUF_SIZ;
700                         tlen -= AR_BUF_SIZ;
701                         txdesc++;
702                         txdata += AR_BUF_SIZ;
703                         i++;
704                 }
705                 /* XXX Move into the loop? */
706                 txdesc->stat = SCA_DESC_EOM;
707                 txdesc->len = tlen;
708                 txdesc++;
709                 txdata += AR_BUF_SIZ;
710                 i++;
711
712 #ifndef NETGRAPH
713                 BPF_MTAP(ifp, mtx);
714                 m_freem(mtx);
715                 ++sc->ifsppp.pp_if.if_opackets;
716 #else   /* NETGRAPH */
717                 m_freem(mtx);
718                 sc->outbytes += len;
719                 ++sc->opackets;
720 #endif  /* NETGRAPH */
721
722                 /*
723                  * Check if we have space for another mbuf.
724                  * XXX This is hardcoded. A packet won't be larger
725                  * than 3 buffers (3 x 512).
726                  */
727                 if((i + 3) >= blkp->txmax)
728                         break;
729
730 #ifndef NETGRAPH
731                 mtx = sppp_dequeue(ifp);
732 #else   /* NETGRAPH */
733                 IF_DEQUEUE(&sc->xmitq_hipri, mtx);
734                 if (mtx == NULL) {
735                         IF_DEQUEUE(&sc->xmitq, mtx);
736                 }
737 #endif /* NETGRAPH */
738                 if(!mtx)
739                         break;
740         }
741
742         blkp->txtail = i;
743
744         /*
745          * Mark the last descriptor, so that the SCA know where
746          * to stop.
747          */
748         txdesc--;
749         txdesc->stat |= SCA_DESC_EOT;
750
751         txdesc = (sca_descriptor *)blkp->txdesc;
752         blkp->txeda = (u_short)((u_int)&txdesc[i]);
753
754 #if 0
755         kprintf("ARstart: %p desc->cp %x\n", &txdesc->cp, txdesc->cp);
756         kprintf("ARstart: %p desc->bp %x\n", &txdesc->bp, txdesc->bp);
757         kprintf("ARstart: %p desc->bpb %x\n", &txdesc->bpb, txdesc->bpb);
758         kprintf("ARstart: %p desc->len %x\n", &txdesc->len, txdesc->len);
759         kprintf("ARstart: %p desc->stat %x\n", &txdesc->stat, txdesc->stat);
760 #endif
761
762         sc->txb_inuse++;
763         sc->txb_new++;
764         if(sc->txb_new == AR_TX_BLOCKS)
765                 sc->txb_new = 0;
766
767         if(sc->xmit_busy == 0)
768                 ar_xmit(sc);
769
770         if(sc->hc->bustype == AR_BUS_ISA)
771                 ARC_SET_OFF(sc->hc);
772
773         goto top_arstart;
774 }
775
776 #ifndef NETGRAPH
777 static int
778 arioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
779 {
780         int error;
781         int was_up, should_be_up;
782         struct ar_softc *sc = ifp->if_softc;
783
784         TRC(if_printf(ifp, "arioctl.\n");)
785
786         was_up = ifp->if_flags & IFF_RUNNING;
787
788         error = sppp_ioctl(ifp, cmd, data);
789         TRC(if_printf(ifp, "ioctl: ifsppp.pp_flags = %x, if_flags %x.\n", 
790                 ((struct sppp *)ifp)->pp_flags, ifp->if_flags);)
791         if(error)
792                 return (error);
793
794         if((cmd != SIOCSIFFLAGS) && cmd != (SIOCSIFADDR))
795                 return (0);
796
797         TRC(if_printf(ifp, "arioctl %s.\n",
798                 (cmd == SIOCSIFFLAGS) ? "SIOCSIFFLAGS" : "SIOCSIFADDR");)
799
800         should_be_up = ifp->if_flags & IFF_RUNNING;
801
802         if(!was_up && should_be_up) {
803                 /* Interface should be up -- start it. */
804                 ar_up(sc);
805                 arstart(ifp);
806                 /* XXX Maybe clear the IFF_UP flag so that the link
807                  * will only go up after sppp lcp and ipcp negotiation.
808                  */
809         } else if(was_up && !should_be_up) {
810                 /* Interface should be down -- stop it. */
811                 ar_down(sc);
812                 sppp_flush(ifp);
813         }
814         return (0);
815 }
816 #endif  /* NETGRAPH */
817
818 /*
819  * This is to catch lost tx interrupts.
820  */
821 static void
822 #ifndef NETGRAPH
823 arwatchdog(struct ifnet *ifp)
824 {
825         struct ar_softc *sc = ifp->if_softc;
826 #else   /* NETGRAPH */
827 arwatchdog(struct ar_softc *sc)
828 {
829 #endif  /* NETGRAPH */
830         msci_channel *msci = &sc->sca->msci[sc->scachan];
831
832 #ifndef NETGRAPH
833         if(!(ifp->if_flags & IFF_RUNNING))
834                 return;
835 #endif  /* NETGRAPH */
836
837         if(sc->hc->bustype == AR_BUS_ISA)
838                 ARC_SET_SCA(sc->hc, sc->scano);
839
840         /* XXX if(sc->ifsppp.pp_if.if_flags & IFF_DEBUG) */
841                 kprintf("ar%d: transmit failed, "
842                         "ST0 %x, ST1 %x, ST3 %x, DSR %x.\n",
843                         sc->unit,
844                         msci->st0,
845                         msci->st1,
846                         msci->st3,
847                         sc->sca->dmac[DMAC_TXCH(sc->scachan)].dsr);
848
849         if(msci->st1 & SCA_ST1_UDRN) {
850                 msci->cmd = SCA_CMD_TXABORT;
851                 msci->cmd = SCA_CMD_TXENABLE;
852                 msci->st1 = SCA_ST1_UDRN;
853         }
854
855         sc->xmit_busy = 0;
856 #ifndef NETGRAPH
857         ifp->if_flags &= ~IFF_OACTIVE;
858 #else   /* NETGRAPH */
859         /* XXX ifp->if_flags &= ~IFF_OACTIVE; */
860 #endif  /* NETGRAPH */
861
862         if(sc->txb_inuse && --sc->txb_inuse)
863                 ar_xmit(sc);
864
865 #ifndef NETGRAPH
866         arstart(ifp);
867 #else   /* NETGRAPH */
868         arstart(sc);
869 #endif  /* NETGRAPH */
870 }
871
872 static void
873 ar_up(struct ar_softc *sc)
874 {
875         sca_regs *sca;
876         msci_channel *msci;
877
878         sca = sc->sca;
879         msci = &sca->msci[sc->scachan];
880
881         TRC(kprintf("ar%d: sca %p, msci %p, ch %d\n",
882                 sc->unit, sca, msci, sc->scachan));
883
884         /*
885          * Enable transmitter and receiver.
886          * Raise DTR and RTS.
887          * Enable interrupts.
888          */
889         if(sc->hc->bustype == AR_BUS_ISA)
890                 ARC_SET_SCA(sc->hc, sc->scano);
891
892         /* XXX
893          * What about using AUTO mode in msci->md0 ???
894          * And what about CTS/DCD etc... ?
895          */
896         if(sc->hc->handshake & AR_SHSK_RTS)
897                 msci->ctl &= ~SCA_CTL_RTS;
898         if(sc->hc->handshake & AR_SHSK_DTR) {
899                 sc->hc->txc_dtr[sc->scano] &= sc->scachan ? 
900                         ~AR_TXC_DTR_DTR1 : ~AR_TXC_DTR_DTR0;
901                 if(sc->hc->bustype == AR_BUS_PCI)
902                         sc->hc->orbase[sc->hc->txc_dtr_off[sc->scano]] =
903                                 sc->hc->txc_dtr[sc->scano];
904                 else
905                         ar_outb(sc->hc, sc->hc->txc_dtr_off[sc->scano],
906                                 sc->hc->txc_dtr[sc->scano]);
907         }
908
909         if(sc->scachan == 0) {
910                 sca->ier0 |= 0x0F;
911                 sca->ier1 |= 0x0F;
912         } else {
913                 sca->ier0 |= 0xF0;
914                 sca->ier1 |= 0xF0;
915         }
916
917         msci->cmd = SCA_CMD_RXENABLE;
918         if(sc->hc->bustype == AR_BUS_ISA)
919                 ar_inb(sc->hc, AR_ID_5); /* XXX slow it down a bit. */
920         msci->cmd = SCA_CMD_TXENABLE;
921
922         if(sc->hc->bustype == AR_BUS_ISA)
923                 ARC_SET_OFF(sc->hc);
924 #ifdef  NETGRAPH
925         callout_reset(&sc->timer, hz, ngar_watchdog_frame, sc);
926         sc->running = 1;
927 #endif  /* NETGRAPH */
928 }
929
930 static void
931 ar_down(struct ar_softc *sc)
932 {
933         sca_regs *sca;
934         msci_channel *msci;
935
936         sca = sc->sca;
937         msci = &sca->msci[sc->scachan];
938
939 #ifdef  NETGRAPH
940         callout_stop(&sc->timer);
941         sc->running = 0;
942 #endif  /* NETGRAPH */
943         /*
944          * Disable transmitter and receiver.
945          * Lower DTR and RTS.
946          * Disable interrupts.
947          */
948         if(sc->hc->bustype == AR_BUS_ISA)
949                 ARC_SET_SCA(sc->hc, sc->scano);
950         msci->cmd = SCA_CMD_RXDISABLE;
951         if(sc->hc->bustype == AR_BUS_ISA)
952                 ar_inb(sc->hc, AR_ID_5); /* XXX slow it down a bit. */
953         msci->cmd = SCA_CMD_TXDISABLE;
954
955         if(sc->hc->handshake & AR_SHSK_RTS)
956                 msci->ctl |= SCA_CTL_RTS;
957         if(sc->hc->handshake & AR_SHSK_DTR) {
958                 sc->hc->txc_dtr[sc->scano] |= sc->scachan ? 
959                         AR_TXC_DTR_DTR1 : AR_TXC_DTR_DTR0;
960                 if(sc->hc->bustype == AR_BUS_PCI)
961                         sc->hc->orbase[sc->hc->txc_dtr_off[sc->scano]] =
962                                 sc->hc->txc_dtr[sc->scano];
963                 else
964                         ar_outb(sc->hc, sc->hc->txc_dtr_off[sc->scano],
965                                 sc->hc->txc_dtr[sc->scano]);
966         }
967
968         if(sc->scachan == 0) {
969                 sca->ier0 &= ~0x0F;
970                 sca->ier1 &= ~0x0F;
971         } else {
972                 sca->ier0 &= ~0xF0;
973                 sca->ier1 &= ~0xF0;
974         }
975
976         if(sc->hc->bustype == AR_BUS_ISA)
977                 ARC_SET_OFF(sc->hc);
978 }
979
980 static int
981 ar_read_pim_iface(volatile struct ar_hardc *hc, int channel)
982 {
983         int ctype, i, val, x;
984         volatile u_char *pimctrl;
985
986         ctype = 0;
987         val = 0;
988
989         pimctrl = hc->orbase + AR_PIMCTRL;
990
991         /* Reset the PIM */
992         *pimctrl = 0x00;
993         *pimctrl = AR_PIM_STROBE;
994
995         /* Check if there is a PIM */
996         *pimctrl = 0x00;
997         *pimctrl = AR_PIM_READ;
998         x = *pimctrl;
999         TRC(kprintf("x = %x", x));
1000         if(x & AR_PIM_DATA) {
1001                 kprintf("No PIM installed\n");
1002                 return (AR_IFACE_UNKNOWN);
1003         }
1004
1005         x = (x >> 1) & 0x01;
1006         val |= x << 0;
1007
1008         /* Now read the next 15 bits */
1009         for(i = 1; i < 16; i++) {
1010                 *pimctrl = AR_PIM_READ;
1011                 *pimctrl = AR_PIM_READ | AR_PIM_STROBE;
1012                 x = *pimctrl;
1013                 TRC(kprintf(" %x ", x));
1014                 x = (x >> 1) & 0x01;
1015                 val |= x << i;
1016                 if(i == 8 && (val & 0x000f) == 0x0004) {
1017                         int ii;
1018                         
1019                         /* Start bit */
1020                         *pimctrl = AR_PIM_A2D_DOUT | AR_PIM_A2D_STROBE;
1021                         *pimctrl = AR_PIM_A2D_DOUT;
1022
1023                         /* Mode bit */
1024                         *pimctrl = AR_PIM_A2D_DOUT | AR_PIM_A2D_STROBE;
1025                         *pimctrl = AR_PIM_A2D_DOUT;
1026
1027                         /* Sign bit */
1028                         *pimctrl = AR_PIM_A2D_DOUT | AR_PIM_A2D_STROBE;
1029                         *pimctrl = AR_PIM_A2D_DOUT;
1030
1031                         /* Select channel */
1032                         *pimctrl = AR_PIM_A2D_STROBE | ((channel & 2) << 2);
1033                         *pimctrl = ((channel & 2) << 2);
1034                         *pimctrl = AR_PIM_A2D_STROBE | ((channel & 1) << 3);
1035                         *pimctrl = ((channel & 1) << 3);
1036
1037                         *pimctrl = AR_PIM_A2D_STROBE;
1038
1039                         x = *pimctrl;
1040                         if(x & AR_PIM_DATA)
1041                                 kprintf("\nOops A2D start bit not zero (%X)\n", x);
1042
1043                         for(ii = 7; ii >= 0; ii--) {
1044                                 *pimctrl = 0x00;
1045                                 *pimctrl = AR_PIM_A2D_STROBE;
1046                                 x = *pimctrl;
1047                                 if(x & AR_PIM_DATA)
1048                                         ctype |= 1 << ii;
1049                         }
1050                 }
1051         }
1052         TRC(kprintf("\nPIM val %x, ctype %x, %d\n", val, ctype, ctype));
1053         *pimctrl = AR_PIM_MODEG;
1054         *pimctrl = AR_PIM_MODEG | AR_PIM_AUTO_LED;
1055         if(ctype > 255)
1056                 return (AR_IFACE_UNKNOWN);
1057         if(ctype > 239)
1058                 return (AR_IFACE_V_35);
1059         if(ctype > 207)
1060                 return (AR_IFACE_EIA_232);
1061         if(ctype > 178)
1062                 return (AR_IFACE_X_21);
1063         if(ctype > 150)
1064                 return (AR_IFACE_EIA_530);
1065         if(ctype > 25)
1066                 return (AR_IFACE_UNKNOWN);
1067         if(ctype > 7)
1068                 return (AR_IFACE_LOOPBACK);
1069         return (AR_IFACE_UNKNOWN);
1070 }
1071
1072 /*
1073  * Initialize the card, allocate memory for the ar_softc structures
1074  * and fill in the pointers.
1075  */
1076 static void
1077 arc_init(struct ar_hardc *hc)
1078 {
1079         struct ar_softc *sc;
1080         int x;
1081         u_int chanmem;
1082         u_int bufmem;
1083         u_int next;
1084         u_int descneeded;
1085         u_char isr, mar;
1086         u_long memst;
1087
1088         MALLOC(sc, struct ar_softc *, hc->numports * sizeof(struct ar_softc),
1089                 M_DEVBUF, M_WAITOK | M_ZERO);
1090         if (sc == NULL)
1091                 return;
1092         hc->sc = sc;
1093
1094         hc->txc_dtr[0] = AR_TXC_DTR_NOTRESET |
1095                          AR_TXC_DTR_DTR0 | AR_TXC_DTR_DTR1;
1096         hc->txc_dtr[1] = AR_TXC_DTR_DTR0 | AR_TXC_DTR_DTR1;
1097         hc->txc_dtr_off[0] = AR_TXC_DTR0;
1098         hc->txc_dtr_off[1] = AR_TXC_DTR2;
1099         if(hc->bustype == AR_BUS_PCI) {
1100                 hc->txc_dtr_off[0] *= 4;
1101                 hc->txc_dtr_off[1] *= 4;
1102         }
1103
1104         /*
1105          * reset the card and wait at least 1uS.
1106          */
1107         if(hc->bustype == AR_BUS_PCI)
1108                 hc->orbase[AR_TXC_DTR0 * 4] = ~AR_TXC_DTR_NOTRESET &
1109                         hc->txc_dtr[0];
1110         else
1111                 ar_outb(hc, AR_TXC_DTR0, ~AR_TXC_DTR_NOTRESET &
1112                         hc->txc_dtr[0]);
1113         DELAY(2);
1114         if(hc->bustype == AR_BUS_PCI)
1115                 hc->orbase[AR_TXC_DTR0 * 4] = hc->txc_dtr[0];
1116         else
1117                 ar_outb(hc, AR_TXC_DTR0, hc->txc_dtr[0]);
1118
1119         if(hc->bustype == AR_BUS_ISA) {
1120                 /*
1121                  * Configure the card.
1122                  * Mem address, irq, 
1123                  */
1124                 memst = rman_get_start(hc->res_memory);
1125                 mar = memst >> 16;
1126                 isr = irqtable[hc->isa_irq] << 1;
1127                 if(isr == 0)
1128                         kprintf("ar%d: Warning illegal interrupt %d\n",
1129                                 hc->cunit, hc->isa_irq);
1130                 isr = isr | ((memst & 0xc000) >> 10);
1131
1132                 hc->sca[0] = (sca_regs *)hc->mem_start;
1133                 hc->sca[1] = (sca_regs *)hc->mem_start;
1134
1135                 ar_outb(hc, AR_MEM_SEL, mar);
1136                 ar_outb(hc, AR_INT_SEL, isr | AR_INTS_CEN);
1137         }
1138
1139         if(hc->bustype == AR_BUS_PCI && hc->interface[0] == AR_IFACE_PIM)
1140                 for(x = 0; x < hc->numports; x++)
1141                         hc->interface[x] = ar_read_pim_iface(hc, x);
1142
1143         /*
1144          * Set the TX clock direction and enable TX.
1145          */
1146         for(x=0;x<hc->numports;x++) {
1147                 switch(hc->interface[x]) {
1148                 case AR_IFACE_V_35:
1149                         hc->txc_dtr[x / NCHAN] |= (x % NCHAN == 0) ?
1150                             AR_TXC_DTR_TX0 : AR_TXC_DTR_TX1;
1151                         hc->txc_dtr[x / NCHAN] |= (x % NCHAN == 0) ?
1152                             AR_TXC_DTR_TXCS0 : AR_TXC_DTR_TXCS1;
1153                         break;
1154                 case AR_IFACE_EIA_530:
1155                 case AR_IFACE_COMBO:
1156                 case AR_IFACE_X_21:
1157                         hc->txc_dtr[x / NCHAN] |= (x % NCHAN == 0) ?
1158                             AR_TXC_DTR_TX0 : AR_TXC_DTR_TX1;
1159                         break;
1160                 }
1161         }
1162
1163         if(hc->bustype == AR_BUS_PCI)
1164                 hc->orbase[AR_TXC_DTR0 * 4] = hc->txc_dtr[0];
1165         else
1166                 ar_outb(hc, AR_TXC_DTR0, hc->txc_dtr[0]);
1167         if(hc->numports > NCHAN) {
1168                 if(hc->bustype == AR_BUS_PCI)
1169                         hc->orbase[AR_TXC_DTR2 * 4] = hc->txc_dtr[1];
1170                 else
1171                         ar_outb(hc, AR_TXC_DTR2, hc->txc_dtr[1]);
1172         }
1173
1174         chanmem = hc->memsize / hc->numports;
1175         next = 0;
1176
1177         for(x=0;x<hc->numports;x++, sc++) {
1178                 int blk;
1179
1180                 sc->sca = hc->sca[x / NCHAN];
1181
1182                 for(blk = 0; blk < AR_TX_BLOCKS; blk++) {
1183                         sc->block[blk].txdesc = next;
1184                         bufmem = (16 * 1024) / AR_TX_BLOCKS;
1185                         descneeded = bufmem / AR_BUF_SIZ;
1186                         sc->block[blk].txstart = sc->block[blk].txdesc +
1187                                 ((((descneeded * sizeof(sca_descriptor)) /
1188                                         AR_BUF_SIZ) + 1) * AR_BUF_SIZ);
1189                         sc->block[blk].txend = next + bufmem;
1190                         sc->block[blk].txmax =
1191                                 (sc->block[blk].txend - sc->block[blk].txstart)
1192                                 / AR_BUF_SIZ;
1193                         next += bufmem;
1194
1195                         TRC(kprintf("ar%d: blk %d: txdesc %x, txstart %x, "
1196                                    "txend %x, txmax %d\n",
1197                                    x,
1198                                    blk,
1199                                    sc->block[blk].txdesc,
1200                                    sc->block[blk].txstart,
1201                                    sc->block[blk].txend,
1202                                    sc->block[blk].txmax));
1203                 }
1204
1205                 sc->rxdesc = next;
1206                 bufmem = chanmem - (bufmem * AR_TX_BLOCKS);
1207                 descneeded = bufmem / AR_BUF_SIZ;
1208                 sc->rxstart = sc->rxdesc +
1209                                 ((((descneeded * sizeof(sca_descriptor)) /
1210                                         AR_BUF_SIZ) + 1) * AR_BUF_SIZ);
1211                 sc->rxend = next + bufmem;
1212                 sc->rxmax = (sc->rxend - sc->rxstart) / AR_BUF_SIZ;
1213                 next += bufmem;
1214                 TRC(kprintf("ar%d: rxdesc %x, rxstart %x, "
1215                            "rxend %x, rxmax %d\n",
1216                            x, sc->rxdesc, sc->rxstart, sc->rxend, sc->rxmax));
1217         }
1218
1219         if(hc->bustype == AR_BUS_PCI)
1220                 hc->orbase[AR_PIMCTRL] = AR_PIM_MODEG | AR_PIM_AUTO_LED;
1221 }
1222
1223
1224 /*
1225  * The things done here are channel independent.
1226  *
1227  *   Configure the sca waitstates.
1228  *   Configure the global interrupt registers.
1229  *   Enable master dma enable.
1230  */
1231 static void
1232 ar_init_sca(struct ar_hardc *hc, int scano)
1233 {
1234         sca_regs *sca;
1235
1236         sca = hc->sca[scano];
1237         if(hc->bustype == AR_BUS_ISA)
1238                 ARC_SET_SCA(hc, scano);
1239
1240         /*
1241          * Do the wait registers.
1242          * Set everything to 0 wait states.
1243          */
1244         sca->pabr0 = 0;
1245         sca->pabr1 = 0;
1246         sca->wcrl  = 0;
1247         sca->wcrm  = 0;
1248         sca->wcrh  = 0;
1249
1250         /*
1251          * Configure the interrupt registers.
1252          * Most are cleared until the interface is configured.
1253          */
1254         sca->ier0 = 0x00; /* MSCI interrupts... Not used with dma. */
1255         sca->ier1 = 0x00; /* DMAC interrupts */
1256         sca->ier2 = 0x00; /* TIMER interrupts... Not used yet. */
1257         sca->itcr = 0x00; /* Use ivr and no intr ack */
1258         sca->ivr  = 0x40; /* Fill in the interrupt vector. */
1259         sca->imvr = 0x40;
1260
1261         /*
1262          * Configure the timers.
1263          * XXX Later
1264          */
1265
1266
1267         /*
1268          * Set the DMA channel priority to rotate between
1269          * all four channels.
1270          *
1271          * Enable all dma channels.
1272          */
1273         if(hc->bustype == AR_BUS_PCI) {
1274                 u_char *t;
1275
1276                 /*
1277                  * Stupid problem with the PCI interface chip that break
1278                  * things.
1279                  * XXX
1280                  */
1281                 t = (u_char *)sca;
1282                 t[AR_PCI_SCA_PCR] = SCA_PCR_PR2;
1283                 t[AR_PCI_SCA_DMER] = SCA_DMER_EN;
1284         } else {
1285                 sca->pcr = SCA_PCR_PR2;
1286                 sca->dmer = SCA_DMER_EN;
1287         }
1288 }
1289
1290
1291 /*
1292  * Configure the msci
1293  *
1294  * NOTE: The serial port configuration is hardcoded at the moment.
1295  */
1296 static void
1297 ar_init_msci(struct ar_softc *sc)
1298 {
1299         msci_channel *msci;
1300
1301         msci = &sc->sca->msci[sc->scachan];
1302
1303         if(sc->hc->bustype == AR_BUS_ISA)
1304                 ARC_SET_SCA(sc->hc, sc->scano);
1305
1306         msci->cmd = SCA_CMD_RESET;
1307
1308         msci->md0 = SCA_MD0_CRC_1 |
1309                     SCA_MD0_CRC_CCITT |
1310                     SCA_MD0_CRC_ENABLE |
1311                     SCA_MD0_MODE_HDLC;
1312         msci->md1 = SCA_MD1_NOADDRCHK;
1313         msci->md2 = SCA_MD2_DUPLEX | SCA_MD2_NRZ;
1314
1315         /*
1316          * Acording to the manual I should give a reset after changing the
1317          * mode registers.
1318          */
1319         msci->cmd = SCA_CMD_RXRESET;
1320         msci->ctl = SCA_CTL_IDLPAT | SCA_CTL_UDRNC | SCA_CTL_RTS;
1321
1322         /*
1323          * For now all interfaces are programmed to use the RX clock for
1324          * the TX clock.
1325          */
1326         switch(sc->hc->interface[sc->subunit]) {
1327         case AR_IFACE_V_35:
1328                 msci->rxs = SCA_RXS_CLK_RXC0 | SCA_RXS_DIV1;
1329                 msci->txs = SCA_TXS_CLK_TXC | SCA_TXS_DIV1;
1330                 break;
1331         case AR_IFACE_X_21:
1332         case AR_IFACE_EIA_530:
1333         case AR_IFACE_COMBO:
1334                 msci->rxs = SCA_RXS_CLK_RXC0 | SCA_RXS_DIV1;
1335                 msci->txs = SCA_TXS_CLK_RX | SCA_TXS_DIV1;
1336         }
1337
1338         msci->tmc = 153;   /* This give 64k for loopback */
1339
1340         /* XXX
1341          * Disable all interrupts for now. I think if you are using
1342          * the dmac you don't use these interrupts.
1343          */
1344         msci->ie0 = 0;
1345         msci->ie1 = 0x0C; /* XXX CTS and DCD (DSR on 570I) level change. */
1346         msci->ie2 = 0;
1347         msci->fie = 0;
1348
1349         msci->sa0 = 0;
1350         msci->sa1 = 0;
1351
1352         msci->idl = 0x7E; /* XXX This is what cisco does. */
1353
1354         /*
1355          * This is what the ARNET diags use.
1356          */
1357         msci->rrc  = 0x0E;
1358         msci->trc0 = 0x12;
1359         msci->trc1 = 0x1F;
1360 }
1361
1362 /*
1363  * Configure the rx dma controller.
1364  */
1365 static void
1366 ar_init_rx_dmac(struct ar_softc *sc)
1367 {
1368         dmac_channel *dmac;
1369         sca_descriptor *rxd;
1370         u_int rxbuf;
1371         u_int rxda;
1372         u_int rxda_d;
1373         int x = 0;
1374
1375         dmac = &sc->sca->dmac[DMAC_RXCH(sc->scachan)];
1376
1377         if(sc->hc->bustype == AR_BUS_ISA)
1378                 ARC_SET_MEM(sc->hc, sc->rxdesc);
1379
1380         rxd = (sca_descriptor *)(sc->hc->mem_start + (sc->rxdesc&sc->hc->winmsk));
1381         rxda_d = (u_int)sc->hc->mem_start - (sc->rxdesc & ~sc->hc->winmsk);
1382
1383         for(rxbuf=sc->rxstart;rxbuf<sc->rxend;rxbuf += AR_BUF_SIZ, rxd++) {
1384                 rxda = (u_int)&rxd[1] - rxda_d;
1385                 rxd->cp = (u_short)(rxda & 0xfffful);
1386
1387                 x++;
1388                 if(x < 6)
1389                 TRC(kprintf("Descrp %p, data pt %x, data %x, ",
1390                         rxd, rxda, rxbuf));
1391
1392                 rxd->bp = (u_short)(rxbuf & 0xfffful);
1393                 rxd->bpb = (u_char)((rxbuf >> 16) & 0xff);
1394                 rxd->len = 0;
1395                 rxd->stat = 0xff; /* The sca write here when it is finished. */
1396
1397                 if(x < 6)
1398                 TRC(kprintf("bpb %x, bp %x.\n", rxd->bpb, rxd->bp));
1399         }
1400         rxd--;
1401         rxd->cp = (u_short)(sc->rxdesc & 0xfffful);
1402
1403         sc->rxhind = 0;
1404
1405         if(sc->hc->bustype == AR_BUS_ISA)
1406                 ARC_SET_SCA(sc->hc, sc->scano);
1407
1408         dmac->dsr = 0;    /* Disable DMA transfer */
1409         dmac->dcr = SCA_DCR_ABRT;
1410
1411         /* XXX maybe also SCA_DMR_CNTE */
1412         dmac->dmr = SCA_DMR_TMOD | SCA_DMR_NF;
1413         dmac->bfl = AR_BUF_SIZ;
1414
1415         dmac->cda = (u_short)(sc->rxdesc & 0xffff);
1416         dmac->sarb = (u_char)((sc->rxdesc >> 16) & 0xff);
1417
1418         rxd = (sca_descriptor *)sc->rxstart;
1419         dmac->eda = (u_short)((u_int)&rxd[sc->rxmax - 1] & 0xffff);
1420
1421         dmac->dir = 0xF0;
1422
1423         dmac->dsr = SCA_DSR_DE;
1424 }
1425
1426 /*
1427  * Configure the TX DMA descriptors.
1428  * Initialize the needed values and chain the descriptors.
1429  */
1430 static void
1431 ar_init_tx_dmac(struct ar_softc *sc)
1432 {
1433         dmac_channel *dmac;
1434         struct buf_block *blkp;
1435         int blk;
1436         sca_descriptor *txd;
1437         u_int txbuf;
1438         u_int txda;
1439         u_int txda_d;
1440
1441         dmac = &sc->sca->dmac[DMAC_TXCH(sc->scachan)];
1442
1443         if(sc->hc->bustype == AR_BUS_ISA)
1444                 ARC_SET_MEM(sc->hc, sc->block[0].txdesc);
1445
1446         for(blk = 0; blk < AR_TX_BLOCKS; blk++) {
1447                 blkp = &sc->block[blk];
1448                 txd = (sca_descriptor *)(sc->hc->mem_start +
1449                                         (blkp->txdesc&sc->hc->winmsk));
1450                 txda_d = (u_int)sc->hc->mem_start -
1451                                 (blkp->txdesc & ~sc->hc->winmsk);
1452
1453                 txbuf=blkp->txstart;
1454                 for(;txbuf<blkp->txend;txbuf += AR_BUF_SIZ, txd++) {
1455                         txda = (u_int)&txd[1] - txda_d;
1456                         txd->cp = (u_short)(txda & 0xfffful);
1457
1458                         txd->bp = (u_short)(txbuf & 0xfffful);
1459                         txd->bpb = (u_char)((txbuf >> 16) & 0xff);
1460                         TRC(kprintf("ar%d: txbuf %x, bpb %x, bp %x\n",
1461                                 sc->unit, txbuf, txd->bpb, txd->bp));
1462                         txd->len = 0;
1463                         txd->stat = 0;
1464                 }
1465                 txd--;
1466                 txd->cp = (u_short)(blkp->txdesc & 0xfffful);
1467
1468                 blkp->txtail = (u_int)txd - (u_int)sc->hc->mem_start;
1469                 TRC(kprintf("TX Descriptors start %x, end %x.\n",
1470                         blkp->txdesc,
1471                         blkp->txtail));
1472         }
1473
1474         if(sc->hc->bustype == AR_BUS_ISA)
1475                 ARC_SET_SCA(sc->hc, sc->scano);
1476
1477         dmac->dsr = 0; /* Disable DMA */
1478         dmac->dcr = SCA_DCR_ABRT;
1479         dmac->dmr = SCA_DMR_TMOD | SCA_DMR_NF;
1480         dmac->dir = SCA_DIR_EOT | SCA_DIR_BOF | SCA_DIR_COF;
1481
1482         dmac->sarb = (u_char)((sc->block[0].txdesc >> 16) & 0xff);
1483 }
1484
1485
1486 /*
1487  * Look through the descriptors to see if there is a complete packet
1488  * available. Stop if we get to where the sca is busy.
1489  *
1490  * Return the length and status of the packet.
1491  * Return nonzero if there is a packet available.
1492  *
1493  * NOTE:
1494  * It seems that we get the interrupt a bit early. The updateing of
1495  * descriptor values is not always completed when this is called.
1496  */
1497 static int
1498 ar_packet_avail(struct ar_softc *sc,
1499                     int *len,
1500                     u_char *rxstat)
1501 {
1502         dmac_channel *dmac;
1503         sca_descriptor *rxdesc;
1504         sca_descriptor *endp;
1505         sca_descriptor *cda;
1506
1507         if(sc->hc->bustype == AR_BUS_ISA)
1508                 ARC_SET_SCA(sc->hc, sc->scano);
1509         dmac = &sc->sca->dmac[DMAC_RXCH(sc->scachan)];
1510         cda = (sca_descriptor *)(sc->hc->mem_start +
1511               ((((u_int)dmac->sarb << 16) + dmac->cda) & sc->hc->winmsk));
1512
1513         if(sc->hc->bustype == AR_BUS_ISA)
1514                 ARC_SET_MEM(sc->hc, sc->rxdesc);
1515         rxdesc = (sca_descriptor *)
1516                         (sc->hc->mem_start + (sc->rxdesc & sc->hc->winmsk));
1517         endp = rxdesc;
1518         rxdesc = &rxdesc[sc->rxhind];
1519         endp = &endp[sc->rxmax];
1520
1521         *len = 0;
1522
1523         while(rxdesc != cda) {
1524                 *len += rxdesc->len;
1525
1526                 if(rxdesc->stat & SCA_DESC_EOM) {
1527                         *rxstat = rxdesc->stat;
1528                         TRC(kprintf("ar%d: PKT AVAIL len %d, %x.\n",
1529                                 sc->unit, *len, *rxstat));
1530                         return (1);
1531                 }
1532
1533                 rxdesc++;
1534                 if(rxdesc == endp)
1535                         rxdesc = (sca_descriptor *)
1536                                (sc->hc->mem_start + (sc->rxdesc & sc->hc->winmsk));
1537         }
1538
1539         *len = 0;
1540         *rxstat = 0;
1541         return (0);
1542 }
1543
1544
1545 /*
1546  * Copy a packet from the on card memory into a provided mbuf.
1547  * Take into account that buffers wrap and that a packet may
1548  * be larger than a buffer.
1549  */
1550 static void 
1551 ar_copy_rxbuf(struct mbuf *m,
1552                    struct ar_softc *sc,
1553                    int len)
1554 {
1555         sca_descriptor *rxdesc;
1556         u_int rxdata;
1557         u_int rxmax;
1558         u_int off = 0;
1559         u_int tlen;
1560
1561         rxdata = sc->rxstart + (sc->rxhind * AR_BUF_SIZ);
1562         rxmax = sc->rxstart + (sc->rxmax * AR_BUF_SIZ);
1563
1564         rxdesc = (sca_descriptor *)
1565                         (sc->hc->mem_start + (sc->rxdesc & sc->hc->winmsk));
1566         rxdesc = &rxdesc[sc->rxhind];
1567
1568         while(len) {
1569                 tlen = (len < AR_BUF_SIZ) ? len : AR_BUF_SIZ;
1570                 if(sc->hc->bustype == AR_BUS_ISA)
1571                         ARC_SET_MEM(sc->hc, rxdata);
1572                 bcopy(sc->hc->mem_start + (rxdata & sc->hc->winmsk), 
1573                         mtod(m, caddr_t) + off,
1574                         tlen);
1575
1576                 off += tlen;
1577                 len -= tlen;
1578
1579                 if(sc->hc->bustype == AR_BUS_ISA)
1580                         ARC_SET_MEM(sc->hc, sc->rxdesc);
1581                 rxdesc->len = 0;
1582                 rxdesc->stat = 0xff;
1583
1584                 rxdata += AR_BUF_SIZ;
1585                 rxdesc++;
1586                 if(rxdata == rxmax) {
1587                         rxdata = sc->rxstart;
1588                         rxdesc = (sca_descriptor *)
1589                                 (sc->hc->mem_start + (sc->rxdesc & sc->hc->winmsk));
1590                 }
1591         }
1592 }
1593
1594 /*
1595  * If single is set, just eat a packet. Otherwise eat everything up to
1596  * where cda points. Update pointers to point to the next packet.
1597  */
1598 static void
1599 ar_eat_packet(struct ar_softc *sc, int single)
1600 {
1601         dmac_channel *dmac;
1602         sca_descriptor *rxdesc;
1603         sca_descriptor *endp;
1604         sca_descriptor *cda;
1605         int loopcnt = 0;
1606         u_char stat;
1607
1608         if(sc->hc->bustype == AR_BUS_ISA)
1609                 ARC_SET_SCA(sc->hc, sc->scano);
1610         dmac = &sc->sca->dmac[DMAC_RXCH(sc->scachan)];
1611         cda = (sca_descriptor *)(sc->hc->mem_start +
1612               ((((u_int)dmac->sarb << 16) + dmac->cda) & sc->hc->winmsk));
1613
1614         /*
1615          * Loop until desc->stat == (0xff || EOM)
1616          * Clear the status and length in the descriptor.
1617          * Increment the descriptor.
1618          */
1619         if(sc->hc->bustype == AR_BUS_ISA)
1620                 ARC_SET_MEM(sc->hc, sc->rxdesc);
1621         rxdesc = (sca_descriptor *)
1622                 (sc->hc->mem_start + (sc->rxdesc & sc->hc->winmsk));
1623         endp = rxdesc;
1624         rxdesc = &rxdesc[sc->rxhind];
1625         endp = &endp[sc->rxmax];
1626
1627         while(rxdesc != cda) {
1628                 loopcnt++;
1629                 if(loopcnt > sc->rxmax) {
1630                         kprintf("ar%d: eat pkt %d loop, cda %p, "
1631                                "rxdesc %p, stat %x.\n",
1632                                sc->unit,
1633                                loopcnt,
1634                                (void *)cda,
1635                                (void *)rxdesc,
1636                                rxdesc->stat);
1637                         break;
1638                 }
1639
1640                 stat = rxdesc->stat;
1641
1642                 rxdesc->len = 0;
1643                 rxdesc->stat = 0xff;
1644
1645                 rxdesc++;
1646                 sc->rxhind++;
1647                 if(rxdesc == endp) {
1648                         rxdesc = (sca_descriptor *)
1649                                (sc->hc->mem_start + (sc->rxdesc & sc->hc->winmsk));
1650                         sc->rxhind = 0;
1651                 }
1652
1653                 if(single && (stat == SCA_DESC_EOM))
1654                         break;
1655         }
1656
1657         /*
1658          * Update the eda to the previous descriptor.
1659          */
1660         if(sc->hc->bustype == AR_BUS_ISA)
1661                 ARC_SET_SCA(sc->hc, sc->scano);
1662
1663         rxdesc = (sca_descriptor *)sc->rxdesc;
1664         rxdesc = &rxdesc[(sc->rxhind + sc->rxmax - 2 ) % sc->rxmax];
1665
1666         sc->sca->dmac[DMAC_RXCH(sc->scachan)].eda = 
1667                         (u_short)((u_int)rxdesc & 0xffff);
1668 }
1669
1670
1671 /*
1672  * While there is packets available in the rx buffer, read them out
1673  * into mbufs and ship them off.
1674  */
1675 static void
1676 ar_get_packets(struct ar_softc *sc)
1677 {
1678         sca_descriptor *rxdesc;
1679         struct mbuf *m = NULL;
1680         int i;
1681         int len;
1682         u_char rxstat;
1683 #ifdef NETGRAPH
1684         int error;
1685 #endif
1686
1687         while(ar_packet_avail(sc, &len, &rxstat)) {
1688                 TRC(kprintf("apa: len %d, rxstat %x\n", len, rxstat));
1689                 if(((rxstat & SCA_DESC_ERRORS) == 0) && (len < MCLBYTES)) {
1690                         m =  m_getl(len, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
1691                         if(m == NULL) {
1692                                 /* eat packet if get mbuf fail!! */
1693                                 ar_eat_packet(sc, 1);
1694                                 continue;
1695                         }
1696 #ifdef NETGRAPH
1697                         m->m_pkthdr.rcvif = NULL;
1698                         sc->inbytes += len;
1699                         sc->inlast = 0;
1700 #else
1701                         m->m_pkthdr.rcvif = &sc->ifsppp.pp_if;
1702 #endif
1703                         m->m_pkthdr.len = m->m_len = len;
1704                         ar_copy_rxbuf(m, sc, len);
1705 #ifdef NETGRAPH
1706                         NG_SEND_DATA_ONLY(error, sc->hook, m);
1707                         sc->ipackets++;
1708 #else
1709                         BPF_MTAP(&sc->ifsppp.pp_if, m);
1710                         sppp_input(&sc->ifsppp.pp_if, m);
1711                         sc->ifsppp.pp_if.if_ipackets++;
1712 #endif
1713                         /*
1714                          * Update the eda to the previous descriptor.
1715                          */
1716                         i = (len + AR_BUF_SIZ - 1) / AR_BUF_SIZ;
1717                         sc->rxhind = (sc->rxhind + i) % sc->rxmax;
1718
1719                         if(sc->hc->bustype == AR_BUS_ISA)
1720                                 ARC_SET_SCA(sc->hc, sc->scano);
1721
1722                         rxdesc = (sca_descriptor *)sc->rxdesc;
1723                         rxdesc =
1724                              &rxdesc[(sc->rxhind + sc->rxmax - 2 ) % sc->rxmax];
1725
1726                         sc->sca->dmac[DMAC_RXCH(sc->scachan)].eda = 
1727                                 (u_short)((u_int)rxdesc & 0xffff);
1728                 } else {
1729                         int tries = 5;
1730
1731                         while((rxstat == 0xff) && --tries)
1732                                 ar_packet_avail(sc, &len, &rxstat);
1733
1734                         /*
1735                          * It look like we get an interrupt early
1736                          * sometimes and then the status is not
1737                          * filled in yet.
1738                          */
1739                         if(tries && (tries != 5))
1740                                 continue;
1741
1742                         ar_eat_packet(sc, 1);
1743
1744 #ifndef NETGRAPH
1745                         sc->ifsppp.pp_if.if_ierrors++;
1746 #else   /* NETGRAPH */
1747                         sc->ierrors[0]++;
1748 #endif  /* NETGRAPH */
1749
1750                         if(sc->hc->bustype == AR_BUS_ISA)
1751                                 ARC_SET_SCA(sc->hc, sc->scano);
1752
1753                         TRCL(kprintf("ar%d: Receive error chan %d, "
1754                                         "stat %x, msci st3 %x,"
1755                                         "rxhind %d, cda %x, eda %x.\n",
1756                                         sc->unit,
1757                                         sc->scachan, 
1758                                         rxstat,
1759                                         sc->sca->msci[sc->scachan].st3,
1760                                         sc->rxhind,
1761                                         sc->sca->dmac[
1762                                                 DMAC_RXCH(sc->scachan)].cda,
1763                                         sc->sca->dmac[
1764                                                 DMAC_RXCH(sc->scachan)].eda));
1765                 }
1766         }
1767 }
1768
1769
1770 /*
1771  * All DMA interrupts come here.
1772  *
1773  * Each channel has two interrupts.
1774  * Interrupt A for errors and Interrupt B for normal stuff like end
1775  * of transmit or receive dmas.
1776  */
1777 static void
1778 ar_dmac_intr(struct ar_hardc *hc, int scano, u_char isr1)
1779 {
1780         u_char dsr;
1781         u_char dotxstart = isr1;
1782         int mch;
1783         struct ar_softc *sc;
1784         sca_regs *sca;
1785         dmac_channel *dmac;
1786
1787         sca = hc->sca[scano];
1788         mch = 0;
1789         /*
1790          * Shortcut if there is no interrupts for dma channel 0 or 1
1791          */
1792         if((isr1 & 0x0F) == 0) {
1793                 mch = 1;
1794                 isr1 >>= 4;
1795         }
1796
1797         do {
1798                 sc = &hc->sc[mch + (NCHAN * scano)];
1799
1800                 /*
1801                  * Transmit channel
1802                  */
1803                 if(isr1 & 0x0C) {
1804                         dmac = &sca->dmac[DMAC_TXCH(mch)];
1805
1806                         if(hc->bustype == AR_BUS_ISA)
1807                                 ARC_SET_SCA(hc, scano);
1808
1809                         dsr = dmac->dsr;
1810                         dmac->dsr = dsr;
1811
1812                         /* Counter overflow */
1813                         if(dsr & SCA_DSR_COF) {
1814                                 kprintf("ar%d: TX DMA Counter overflow, "
1815                                         "txpacket no %lu.\n",
1816                                         sc->unit,
1817 #ifndef NETGRAPH
1818                                         sc->ifsppp.pp_if.if_opackets);
1819                                 sc->ifsppp.pp_if.if_oerrors++;
1820 #else   /* NETGRAPH */
1821                                         sc->opackets);
1822                                 sc->oerrors++;
1823 #endif  /* NETGRAPH */
1824                         }
1825
1826                         /* Buffer overflow */
1827                         if(dsr & SCA_DSR_BOF) {
1828                                 kprintf("ar%d: TX DMA Buffer overflow, "
1829                                         "txpacket no %lu, dsr %02x, "
1830                                         "cda %04x, eda %04x.\n",
1831                                         sc->unit,
1832 #ifndef NETGRAPH
1833                                         sc->ifsppp.pp_if.if_opackets,
1834 #else   /* NETGRAPH */
1835                                         sc->opackets,
1836 #endif  /* NETGRAPH */
1837                                         dsr,
1838                                         dmac->cda,
1839                                         dmac->eda);
1840 #ifndef NETGRAPH
1841                                 sc->ifsppp.pp_if.if_oerrors++;
1842 #else   /* NETGRAPH */
1843                                 sc->oerrors++;
1844 #endif  /* NETGRAPH */
1845                         }
1846
1847                         /* End of Transfer */
1848                         if(dsr & SCA_DSR_EOT) {
1849                                 /*
1850                                  * This should be the most common case.
1851                                  *
1852                                  * Clear the IFF_OACTIVE flag.
1853                                  *
1854                                  * Call arstart to start a new transmit if
1855                                  * there is data to transmit.
1856                                  */
1857                                 sc->xmit_busy = 0;
1858 #ifndef NETGRAPH
1859                                 sc->ifsppp.pp_if.if_flags &= ~IFF_OACTIVE;
1860                                 sc->ifsppp.pp_if.if_timer = 0;
1861 #else   /* NETGRAPH */
1862                         /* XXX  c->ifsppp.pp_if.if_flags &= ~IFF_OACTIVE; */
1863                                 sc->out_dog = 0; /* XXX */
1864 #endif  /* NETGRAPH */
1865
1866                                 if(sc->txb_inuse && --sc->txb_inuse)
1867                                         ar_xmit(sc);
1868                         }
1869                 }
1870
1871                 /*
1872                  * Receive channel
1873                  */
1874                 if(isr1 & 0x03) {
1875                         dmac = &sca->dmac[DMAC_RXCH(mch)];
1876
1877                         if(hc->bustype == AR_BUS_ISA)
1878                                 ARC_SET_SCA(hc, scano);
1879
1880                         dsr = dmac->dsr;
1881                         dmac->dsr = dsr;
1882
1883                         TRC(kprintf("AR: RX DSR %x\n", dsr));
1884
1885                         /* End of frame */
1886                         if(dsr & SCA_DSR_EOM) {
1887                                 TRC(int tt = sc->ifsppp.pp_if.if_ipackets;)
1888                                 TRC(int ind = sc->rxhind;)
1889
1890                                 ar_get_packets(sc);
1891 #ifndef NETGRAPH
1892 #define IPACKETS sc->ifsppp.pp_if.if_ipackets
1893 #else   /* NETGRAPH */
1894 #define IPACKETS sc->ipackets
1895 #endif  /* NETGRAPH */
1896                                 TRC(if(tt == IPACKETS) {
1897                                         sca_descriptor *rxdesc;
1898                                         int i;
1899
1900                                         if(hc->bustype == AR_BUS_ISA)
1901                                                 ARC_SET_SCA(hc, scano);
1902                                         kprintf("AR: RXINTR isr1 %x, dsr %x, "
1903                                                "no data %d pkts, orxhind %d.\n",
1904                                                dotxstart,
1905                                                dsr,
1906                                                tt,
1907                                                ind);
1908                                         kprintf("AR: rxdesc %x, rxstart %x, "
1909                                                "rxend %x, rxhind %d, "
1910                                                "rxmax %d.\n",
1911                                                sc->rxdesc,
1912                                                sc->rxstart,
1913                                                sc->rxend,
1914                                                sc->rxhind,
1915                                                sc->rxmax);
1916                                         kprintf("AR: cda %x, eda %x.\n",
1917                                                dmac->cda,
1918                                                dmac->eda);
1919
1920                                         if(sc->hc->bustype == AR_BUS_ISA)
1921                                                 ARC_SET_MEM(sc->hc,
1922                                                     sc->rxdesc);
1923                                         rxdesc = (sca_descriptor *)
1924                                                  (sc->hc->mem_start +
1925                                                   (sc->rxdesc & sc->hc->winmsk));
1926                                         rxdesc = &rxdesc[sc->rxhind];
1927                                         for(i=0;i<3;i++,rxdesc++)
1928                                                 kprintf("AR: rxdesc->stat %x, "
1929                                                         "len %d.\n",
1930                                                         rxdesc->stat,
1931                                                         rxdesc->len);
1932                                 })
1933                         }
1934
1935                         /* Counter overflow */
1936                         if(dsr & SCA_DSR_COF) {
1937                                 kprintf("ar%d: RX DMA Counter overflow, "
1938                                         "rxpkts %lu.\n",
1939                                         sc->unit,
1940 #ifndef NETGRAPH
1941                                         sc->ifsppp.pp_if.if_ipackets);
1942                                 sc->ifsppp.pp_if.if_ierrors++;
1943 #else   /* NETGRAPH */
1944                                         sc->ipackets);
1945                                 sc->ierrors[1]++;
1946 #endif  /* NETGRAPH */
1947                         }
1948
1949                         /* Buffer overflow */
1950                         if(dsr & SCA_DSR_BOF) {
1951                                 if(hc->bustype == AR_BUS_ISA)
1952                                         ARC_SET_SCA(hc, scano);
1953                                 kprintf("ar%d: RX DMA Buffer overflow, "
1954                                         "rxpkts %lu, rxind %d, "
1955                                         "cda %x, eda %x, dsr %x.\n",
1956                                         sc->unit,
1957 #ifndef NETGRAPH
1958                                         sc->ifsppp.pp_if.if_ipackets,
1959 #else   /* NETGRAPH */
1960                                         sc->ipackets,
1961 #endif  /* NETGRAPH */
1962                                         sc->rxhind,
1963                                         dmac->cda,
1964                                         dmac->eda,
1965                                         dsr);
1966                                 /*
1967                                  * Make sure we eat as many as possible.
1968                                  * Then get the system running again.
1969                                  */
1970                                 ar_eat_packet(sc, 0);
1971 #ifndef NETGRAPH
1972                                 sc->ifsppp.pp_if.if_ierrors++;
1973 #else   /* NETGRAPH */
1974                                 sc->ierrors[2]++;
1975 #endif  /* NETGRAPH */
1976                                 if(hc->bustype == AR_BUS_ISA)
1977                                         ARC_SET_SCA(hc, scano);
1978                                 sca->msci[mch].cmd = SCA_CMD_RXMSGREJ;
1979                                 dmac->dsr = SCA_DSR_DE;
1980
1981                                 TRC(kprintf("ar%d: RX DMA Buffer overflow, "
1982                                         "rxpkts %lu, rxind %d, "
1983                                         "cda %x, eda %x, dsr %x. After\n",
1984                                         sc->unit,
1985                                         sc->ifsppp.pp_if.if_ipackets,
1986                                         sc->rxhind,
1987                                         dmac->cda,
1988                                         dmac->eda,
1989                                         dmac->dsr);)
1990                         }
1991
1992                         /* End of Transfer */
1993                         if(dsr & SCA_DSR_EOT) {
1994                                 /*
1995                                  * If this happen, it means that we are
1996                                  * receiving faster than what the processor
1997                                  * can handle.
1998                                  *
1999                                  * XXX We should enable the dma again.
2000                                  */
2001                                 kprintf("ar%d: RX End of transfer, rxpkts %lu.\n",
2002                                         sc->unit,
2003 #ifndef NETGRAPH
2004                                         sc->ifsppp.pp_if.if_ipackets);
2005                                 sc->ifsppp.pp_if.if_ierrors++;
2006 #else   /* NETGRAPH */
2007                                         sc->ipackets);
2008                                 sc->ierrors[3]++;
2009 #endif  /* NETGRAPH */
2010                         }
2011                 }
2012
2013                 isr1 >>= 4;
2014
2015                 mch++;
2016         }while((mch<NCHAN) && isr1);
2017
2018         /*
2019          * Now that we have done all the urgent things, see if we
2020          * can fill the transmit buffers.
2021          */
2022         for(mch = 0; mch < NCHAN; mch++) {
2023                 if(dotxstart & 0x0C) {
2024                         sc = &hc->sc[mch + (NCHAN * scano)];
2025 #ifndef NETGRAPH
2026                         arstart(&sc->ifsppp.pp_if);
2027 #else   /* NETGRAPH */
2028                         arstart(sc);
2029 #endif  /* NETGRAPH */
2030                 }
2031                 dotxstart >>= 4;
2032         }
2033 }
2034
2035 static void
2036 ar_msci_intr(struct ar_hardc *hc, int scano, u_char isr0)
2037 {
2038         kprintf("arc%d: ARINTR: MSCI\n", hc->cunit);
2039 }
2040
2041 static void
2042 ar_timer_intr(struct ar_hardc *hc, int scano, u_char isr2)
2043 {
2044         kprintf("arc%d: ARINTR: TIMER\n", hc->cunit);
2045 }
2046
2047
2048 #ifdef  NETGRAPH
2049 /*****************************************
2050  * Device timeout/watchdog routine.
2051  * called once per second.
2052  * checks to see that if activity was expected, that it hapenned.
2053  * At present we only look to see if expected output was completed.
2054  */
2055 static void
2056 ngar_watchdog_frame(void * arg)
2057 {
2058         struct ar_softc * sc = arg;
2059         int     speed;
2060
2061         if (sc->running == 0) {
2062                 return; /* if we are not running let timeouts die */
2063         }
2064
2065         lwkt_serialize_enter(&ar_serializer);
2066
2067         /*
2068          * calculate the apparent throughputs 
2069          *  XXX a real hack
2070          */
2071         speed = sc->inbytes - sc->lastinbytes;
2072         sc->lastinbytes = sc->inbytes;
2073         if ( sc->inrate < speed )
2074                 sc->inrate = speed;
2075         speed = sc->outbytes - sc->lastoutbytes;
2076         sc->lastoutbytes = sc->outbytes;
2077         if ( sc->outrate < speed )
2078                 sc->outrate = speed;
2079         sc->inlast++;
2080
2081         if ((sc->inlast > QUITE_A_WHILE)
2082         && (sc->out_deficit > LOTS_OF_PACKETS)) {
2083                 log(LOG_ERR, "ar%d: No response from remote end\n", sc->unit);
2084
2085                 ar_down(sc);
2086                 ar_up(sc);
2087                 sc->inlast = sc->out_deficit = 0;
2088         } else if ( sc->xmit_busy ) { /* no TX -> no TX timeouts */
2089                 if (sc->out_dog == 0) { 
2090                         log(LOG_ERR, "ar%d: Transmit failure.. no clock?\n",
2091                                         sc->unit);
2092
2093                         arwatchdog(sc);
2094 #if 0
2095                         ar_down(sc);
2096                         ar_up(sc);
2097 #endif
2098                         sc->inlast = sc->out_deficit = 0;
2099                 } else {
2100                         sc->out_dog--;
2101                 }
2102         }
2103         lwkt_serialize_exit(&ar_serializer);
2104         callout_reset(&sc->timer, hz, ngar_watchdog_frame, sc);
2105 }
2106
2107 /***********************************************************************
2108  * This section contains the methods for the Netgraph interface
2109  ***********************************************************************/
2110 /*
2111  * It is not possible or allowable to create a node of this type.
2112  * If the hardware exists, it will already have created it.
2113  */
2114 static  int
2115 ngar_constructor(node_p *nodep)
2116 {
2117         return (EINVAL);
2118 }
2119
2120 /*
2121  * give our ok for a hook to be added...
2122  * If we are not running this should kick the device into life.
2123  * The hook's private info points to our stash of info about that
2124  * channel.
2125  */
2126 static int
2127 ngar_newhook(node_p node, hook_p hook, const char *name)
2128 {
2129         struct ar_softc *       sc = NG_NODE_PRIVATE(node);
2130
2131         /*
2132          * check if it's our friend the debug hook
2133          */
2134         if (strcmp(name, NG_AR_HOOK_DEBUG) == 0) {
2135                 NG_HOOK_SET_PRIVATE(hook, NULL); /* paranoid */
2136                 sc->debug_hook = hook;
2137                 return (0);
2138         }
2139
2140         /*
2141          * Check for raw mode hook.
2142          */
2143         if (strcmp(name, NG_AR_HOOK_RAW) != 0) {
2144                 return (EINVAL);
2145         }
2146         NG_HOOK_SET_PRIVATE(hook, sc);
2147         sc->hook = hook;
2148         sc->datahooks++;
2149         ar_up(sc);
2150         return (0);
2151 }
2152
2153 /*
2154  * incoming messages.
2155  * Just respond to the generic TEXT_STATUS message
2156  */
2157 static  int
2158 ngar_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
2159             struct ng_mesg **rptr)
2160 {
2161         struct ar_softc *sc;
2162         int error = 0;
2163         struct ng_mesg *resp = NULL;
2164
2165         sc = NG_NODE_PRIVATE(node);
2166         switch (msg->header.typecookie) {
2167         case    NG_AR_COOKIE: 
2168                 error = EINVAL;
2169                 break;
2170         case    NGM_GENERIC_COOKIE: 
2171                 switch(msg->header.cmd) {
2172                 case NGM_TEXT_STATUS: {
2173                         char        *arg;
2174                         int pos = 0;
2175
2176                         int resplen = sizeof(struct ng_mesg) + 512;
2177                         NG_MKRESPONSE(resp, msg, resplen, M_INTWAIT);
2178                         if (resp == NULL) {
2179                                 error = ENOMEM;
2180                                 break;
2181                         }
2182                         arg = (resp)->data;
2183                         pos = ksprintf(arg, "%ld bytes in, %ld bytes out\n"
2184                             "highest rate seen: %ld B/S in, %ld B/S out\n",
2185                         sc->inbytes, sc->outbytes,
2186                         sc->inrate, sc->outrate);
2187                         pos += ksprintf(arg + pos,
2188                                 "%ld output errors\n",
2189                                 sc->oerrors);
2190                         pos += ksprintf(arg + pos,
2191                                 "ierrors = %ld, %ld, %ld, %ld\n",
2192                                 sc->ierrors[0],
2193                                 sc->ierrors[1],
2194                                 sc->ierrors[2],
2195                                 sc->ierrors[3]);
2196
2197                         (resp)->header.arglen = pos + 1;
2198                         break;
2199                       }
2200                 default:
2201                         error = EINVAL;
2202                         break;
2203                     }
2204                 break;
2205         default:
2206                 error = EINVAL;
2207                 break;
2208         }
2209         /* Take care of synchronous response, if any */
2210         NG_RESPOND_MSG(error, node, retaddr, resp, rptr);
2211         NG_FREE_MSG(msg);
2212         return (error);
2213 }
2214
2215 /*
2216  * get data from another node and transmit it to the correct channel
2217  */
2218 static int
2219 ngar_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
2220 {
2221         int error = 0;
2222         struct ar_softc * sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
2223         struct ifqueue  *xmitq_p;
2224         
2225         /*
2226          * data doesn't come in from just anywhere (e.g control hook)
2227          */
2228         if ( NG_HOOK_PRIVATE(hook) == NULL) {
2229                 error = ENETDOWN;
2230                 goto bad;
2231         }
2232
2233         /* 
2234          * Now queue the data for when it can be sent
2235          */
2236         if (meta && meta->priority > 0)
2237                 xmitq_p = (&sc->xmitq_hipri);
2238         else
2239                 xmitq_p = (&sc->xmitq);
2240
2241         if (IF_QFULL(xmitq_p)) {
2242                 IF_DROP(xmitq_p);
2243
2244                 error = ENOBUFS;
2245                 goto bad;
2246         }
2247         IF_ENQUEUE(xmitq_p, m);
2248         arstart(sc);
2249
2250         return (0);
2251
2252 bad:
2253         /* 
2254          * It was an error case.
2255          * check if we need to free the mbuf, and then return the error
2256          */
2257         NG_FREE_DATA(m, meta);
2258         return (error);
2259 }
2260
2261 /*
2262  * do local shutdown processing..
2263  * this node will refuse to go away, unless the hardware says to..
2264  * don't unref the node, or remove our name. just clear our links up.
2265  */
2266 static  int
2267 ngar_shutdown(node_p node)
2268 {
2269         struct ar_softc * sc = NG_NODE_PRIVATE(node);
2270
2271         ar_down(sc);
2272         NG_NODE_UNREF(node);
2273         /* XXX need to drain the output queues! */
2274
2275         /* The node is dead, long live the node! */
2276         /* stolen from the attach routine */
2277         if (ng_make_node_common(&typestruct, &sc->node) != 0)
2278                 return (0);
2279         ksprintf(sc->nodename, "%s%d", NG_AR_NODE_TYPE, sc->unit);
2280         if (ng_name_node(sc->node, sc->nodename)) {
2281                 sc->node = NULL;
2282                 kprintf("node naming failed\n");
2283                 NG_NODE_UNREF(sc->node); /* node dissappears */
2284                 return (0);
2285         }
2286         NG_NODE_SET_PRIVATE(sc->node, sc);
2287         sc->running = 0;
2288         return (0);
2289 }
2290
2291 /* already linked */
2292 static  int
2293 ngar_connect(hook_p hook)
2294 {
2295         /* be really amiable and just say "YUP that's OK by me! " */
2296         return (0);
2297 }
2298
2299 /*
2300  * notify on hook disconnection (destruction)
2301  *
2302  * Invalidate the private data associated with this dlci.
2303  * For this type, removal of the last link resets tries to destroy the node.
2304  * As the device still exists, the shutdown method will not actually
2305  * destroy the node, but reset the device and leave it 'fresh' :)
2306  *
2307  * The node removal code will remove all references except that owned by the
2308  * driver. 
2309  */
2310 static  int
2311 ngar_disconnect(hook_p hook)
2312 {
2313         struct ar_softc * sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
2314
2315         /*
2316          * If it's the data hook, then free resources etc.
2317          */
2318         if (NG_HOOK_PRIVATE(hook)) {
2319                 sc->datahooks--;
2320                 if (sc->datahooks == 0)
2321                         ar_down(sc);
2322         } else {
2323                 sc->debug_hook = NULL;
2324         }
2325         return (0);
2326 }
2327
2328 /*
2329  * called during bootup
2330  * or LKM loading to put this type into the list of known modules
2331  */
2332 static void
2333 ngar_init(void *ignored)
2334 {
2335         if (ng_newtype(&typestruct))
2336                 kprintf("ngar install failed\n");
2337         ngar_done_init = 1;
2338 }
2339 #endif /* NETGRAPH */
2340
2341 /*
2342  ********************************* END ************************************
2343  */