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