Merge from vendor branch AWK:
[dragonfly.git] / sys / dev / atm / en / midway.c
1 /*      $NetBSD: midway.c,v 1.30 1997/09/29 17:40:38 chuck Exp $        */
2 /*      (sync'd to midway.c 1.68)       */
3
4 /*
5  *
6  * Copyright (c) 1996 Charles D. Cranor and Washington University.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Charles D. Cranor and
20  *      Washington University.
21  * 4. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $FreeBSD: src/sys/dev/en/midway.c,v 1.19.2.1 2003/01/23 21:06:42 sam Exp $
36  * $DragonFly: src/sys/dev/atm/en/midway.c,v 1.9 2004/04/01 07:27:16 joerg Exp $
37  */
38
39 /*
40  *
41  * m i d w a y . c   e n i 1 5 5   d r i v e r 
42  *
43  * author: Chuck Cranor <chuck@ccrc.wustl.edu>
44  * started: spring, 1996 (written from scratch).
45  *
46  * notes from the author:
47  *   Extra special thanks go to Werner Almesberger, EPFL LRC.   Werner's
48  *   ENI driver was especially useful in figuring out how this card works.
49  *   I would also like to thank Werner for promptly answering email and being
50  *   generally helpful.
51  */
52
53 #undef  EN_DEBUG
54 #undef  EN_DEBUG_RANGE          /* check ranges on en_read/en_write's? */
55 #define EN_MBUF_OPT             /* try and put more stuff in mbuf? */
56 #define EN_DIAG
57 #define EN_STAT
58 #ifndef EN_DMA
59 #define EN_DMA          1       /* use dma? */
60 #endif
61 #define EN_NOTXDMA      0       /* hook to disable tx dma only */
62 #define EN_NORXDMA      0       /* hook to disable rx dma only */
63 #define EN_DDBHOOK      1       /* compile in ddb functions */
64 #if defined(MIDWAY_ADPONLY)
65 #define EN_ENIDMAFIX    0       /* no ENI cards to worry about */
66 #else
67 #define EN_ENIDMAFIX    1       /* avoid byte DMA on the ENI card (see below) */
68 #endif
69
70 /*
71  * note on EN_ENIDMAFIX: the byte aligner on the ENI version of the card
72  * appears to be broken.   it works just fine if there is no load... however
73  * when the card is loaded the data get corrupted.   to see this, one only
74  * has to use "telnet" over ATM.   do the following command in "telnet":
75  *      cat /usr/share/misc/termcap
76  * "telnet" seems to generate lots of 1023 byte mbufs (which make great
77  * use of the byte aligner).   watch "netstat -s" for checksum errors.
78  * 
79  * I further tested this by adding a function that compared the transmit 
80  * data on the card's SRAM with the data in the mbuf chain _after_ the 
81  * "transmit DMA complete" interrupt.   using the "telnet" test I got data
82  * mismatches where the byte-aligned data should have been.   using ddb
83  * and en_dumpmem() I verified that the DTQs fed into the card were 
84  * absolutely correct.   thus, we are forced to concluded that the ENI
85  * hardware is buggy.   note that the Adaptec version of the card works
86  * just fine with byte DMA.
87  *
88  * bottom line: we set EN_ENIDMAFIX to 1 to avoid byte DMAs on the ENI
89  * card.
90  */
91
92 #if defined(DIAGNOSTIC) && !defined(EN_DIAG)
93 #define EN_DIAG                 /* link in with master DIAG option */
94 #endif
95 #ifdef EN_STAT
96 #define EN_COUNT(X) (X)++
97 #else
98 #define EN_COUNT(X) /* nothing */
99 #endif
100
101 #ifdef EN_DEBUG
102 #undef  EN_DDBHOOK
103 #define EN_DDBHOOK      1
104 #define STATIC /* nothing */
105 #define INLINE /* nothing */
106 #else /* EN_DEBUG */
107 #define STATIC static
108 #define INLINE __inline
109 #endif /* EN_DEBUG */
110
111 #if defined(__DragonFly__) || defined(__FreeBSD__)
112 #include "use_en.h"             /* XXX for midwayvar.h's NEN */
113 #include "opt_inet.h"
114 #include "opt_natm.h"
115 #include "opt_ddb.h"
116 /* enable DDBHOOK when DDB is available */
117 #undef  EN_DDBHOOK
118 #ifdef DDB
119 #define EN_DDBHOOK      1
120 #endif
121 #endif
122
123 #include <sys/param.h>
124 #include <sys/systm.h>
125 #include <sys/queue.h>
126 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__)
127 #include <sys/device.h>
128 #endif
129 #include <sys/sockio.h>
130 #include <sys/mbuf.h>
131 #include <sys/socket.h>
132 #include <sys/proc.h>
133
134 #include <net/if.h>
135 #include <net/if_atm.h>
136
137 #include <vm/vm.h>
138
139 #if defined(INET) || defined(INET6)
140 #include <netinet/in.h>
141 #include <netinet/if_atm.h>
142 #endif
143
144 #ifdef NATM
145 #include <netproto/natm/natm.h>
146 #endif
147
148 #if !defined(__DragonFly__) && !defined(sparc) && !defined(__FreeBSD__)
149 #include <machine/bus.h>
150 #endif
151
152 #if defined(__NetBSD__) || defined(__OpenBSD__)
153 #include <dev/ic/midwayreg.h>
154 #include <dev/ic/midwayvar.h>
155 #if defined(__alpha__)
156 /* XXX XXX NEED REAL DMA MAPPING SUPPORT XXX XXX */
157 #undef vtophys
158 #define vtophys(va)     alpha_XXX_dmamap((vm_offset_t)(va))
159 #endif
160 #elif defined(__DragonFly__) || defined(__FreeBSD__)
161 #include <machine/clock.h>              /* for DELAY */
162 #include "midwayreg.h"
163 #include "midwayvar.h"
164 #include <vm/pmap.h>                    /* for vtophys proto */
165
166 #ifndef IFF_NOTRAILERS
167 #define IFF_NOTRAILERS 0
168 #endif
169
170 #endif  /* __FreeBSD__ */
171
172 #include <net/bpf.h>
173 #if defined(__DragonFly__) || defined(__FreeBSD__)
174 #define BPFATTACH(ifp, dlt, hlen)       bpfattach((ifp), (dlt), (hlen))
175 #else
176 #define BPFATTACH(ifp, dlt, hlen)       bpfattach(&(ifp)->if_bpf, (ifp), (dlt), (hlen))
177 #define BPF_MTAP(ifp, m)                bpf_mtap((ifp)->if_bpf, (m))
178 #endif
179
180 /*
181  * params
182  */
183
184 #ifndef EN_TXHIWAT
185 #define EN_TXHIWAT      (64*1024)       /* max 64 KB waiting to be DMAd out */
186 #endif
187
188 #ifndef EN_MINDMA
189 #define EN_MINDMA       32      /* don't DMA anything less than this (bytes) */
190 #endif
191
192 #define RX_NONE         0xffff  /* recv VC not in use */
193
194 #define EN_OBHDR        ATM_PH_DRIVER7  /* TBD in first mbuf ! */
195 #define EN_OBTRL        ATM_PH_DRIVER8  /* PDU trailier in last mbuf ! */
196
197 #define ENOTHER_FREE    0x01            /* free rxslot */
198 #define ENOTHER_DRAIN   0x02            /* almost free (drain DRQ dma) */
199 #define ENOTHER_RAW     0x04            /* 'raw' access  (aka boodi mode) */
200 #define ENOTHER_SWSL    0x08            /* in software service list */
201
202 static int en_dma = EN_DMA;             /* use DMA (switch off for dbg) */
203
204 /*
205  * autoconfig attachments
206  */
207
208 struct cfdriver en_cd = {
209     0, "en", DV_IFNET,
210 };
211
212 /*
213  * local structures
214  */
215
216 /*
217  * params to en_txlaunch() function
218  */
219
220 struct en_launch {
221   u_int32_t tbd1;               /* TBD 1 */
222   u_int32_t tbd2;               /* TBD 2 */
223   u_int32_t pdu1;               /* PDU 1 (aal5) */
224   int nodma;                    /* don't use DMA */
225   int need;                     /* total space we need (pad out if less data) */
226   int mlen;                     /* length of mbuf (for dtq) */
227   struct mbuf *t;               /* data */
228   u_int32_t aal;                /* aal code */
229   u_int32_t atm_vci;            /* vci */
230   u_int8_t atm_flags;           /* flags */
231 };
232
233
234 /*
235  * dma table (index by # of words)
236  *
237  * plan A: use WMAYBE (obsolete)
238  * plan B: avoid WMAYBE
239  */
240
241 struct en_dmatab {
242   u_int8_t bcode;               /* code */
243   u_int8_t divshift;            /* byte divisor */
244 };
245
246 static struct en_dmatab en_dma_planB[] = {
247   { 0, 0 },             /* 0 */         { MIDDMA_WORD, 2},      /* 1 */
248   { MIDDMA_2WORD, 3},   /* 2 */         { MIDDMA_WORD, 2},      /* 3 */
249   { MIDDMA_4WORD, 4},   /* 4 */         { MIDDMA_WORD, 2},      /* 5 */
250   { MIDDMA_2WORD, 3},   /* 6 */         { MIDDMA_WORD, 2},      /* 7 */
251   { MIDDMA_8WORD, 5},   /* 8 */         { MIDDMA_WORD, 2},      /* 9 */
252   { MIDDMA_2WORD, 3},   /* 10 */        { MIDDMA_WORD, 2},      /* 11 */
253   { MIDDMA_4WORD, 4},   /* 12 */        { MIDDMA_WORD, 2},      /* 13 */
254   { MIDDMA_2WORD, 3},   /* 14 */        { MIDDMA_WORD, 2},      /* 15 */
255   { MIDDMA_16WORD, 6},  /* 16 */
256 };
257
258 static struct en_dmatab *en_dmaplan = en_dma_planB;
259
260 /*
261  * prototypes
262  */
263
264 STATIC INLINE   int en_b2sz (int) __attribute__ ((unused));
265 #ifdef EN_DDBHOOK
266                 int en_dump (int,int);
267                 int en_dumpmem (int,int,int);
268 #endif
269 STATIC          void en_dmaprobe (struct en_softc *);
270 STATIC          int en_dmaprobe_doit (struct en_softc *, u_int8_t *, 
271                     u_int8_t *, int);
272 STATIC INLINE   int en_dqneed (struct en_softc *, caddr_t, u_int,
273                     u_int) __attribute__ ((unused));
274 STATIC          void en_init (struct en_softc *);
275 STATIC          int en_ioctl (struct ifnet *, EN_IOCTL_CMDT, caddr_t,
276                               struct ucred *);
277 STATIC INLINE   int en_k2sz (int) __attribute__ ((unused));
278 STATIC          void en_loadvc (struct en_softc *, int);
279 STATIC          int en_mfix (struct en_softc *, struct mbuf **,
280                     struct mbuf *);
281 STATIC INLINE   struct mbuf *en_mget (struct en_softc *, u_int,
282                     u_int *) __attribute__ ((unused));
283 STATIC INLINE   u_int32_t en_read (struct en_softc *,
284                     u_int32_t) __attribute__ ((unused));
285 STATIC          int en_rxctl (struct en_softc *, struct atm_pseudoioctl *,
286                     int);
287 STATIC          void en_txdma (struct en_softc *, int);
288 STATIC          void en_txlaunch (struct en_softc *, int,
289                     struct en_launch *);
290 STATIC          void en_service (struct en_softc *);
291 STATIC          void en_start (struct ifnet *);
292 STATIC INLINE   int en_sz2b (int) __attribute__ ((unused));
293 STATIC INLINE   void en_write (struct en_softc *, u_int32_t,
294                     u_int32_t) __attribute__ ((unused));
295
296 /*
297  * macros/inline
298  */
299
300 /*
301  * raw read/write macros
302  */
303
304 #define EN_READDAT(SC,R) en_read(SC,R)
305 #define EN_WRITEDAT(SC,R,V) en_write(SC,R,V)
306
307 /*
308  * cooked read/write macros
309  */
310
311 #define EN_READ(SC,R) ntohl(en_read(SC,R))
312 #define EN_WRITE(SC,R,V) en_write(SC,R, htonl(V))
313
314 #define EN_WRAPADD(START,STOP,CUR,VAL) { \
315         (CUR) = (CUR) + (VAL); \
316         if ((CUR) >= (STOP)) \
317                 (CUR) = (START) + ((CUR) - (STOP)); \
318         }
319
320 #define WORD_IDX(START, X) (((X) - (START)) / sizeof(u_int32_t))
321
322 /* we store sc->dtq and sc->drq data in the following format... */
323 #define EN_DQ_MK(SLOT,LEN) (((SLOT) << 20)|(LEN)|(0x80000))
324                                         /* the 0x80000 ensures we != 0 */
325 #define EN_DQ_SLOT(X) ((X) >> 20)
326 #define EN_DQ_LEN(X) ((X) & 0x3ffff)
327
328 /* format of DTQ/DRQ word 1 differs between ENI and ADP */
329 #if defined(MIDWAY_ENIONLY)
330
331 #define MID_MK_TXQ(SC,CNT,CHAN,END,BCODE) \
332         EN_WRITE((SC), (SC)->dtq_us, \
333                 MID_MK_TXQ_ENI((CNT), (CHAN), (END), (BCODE))); 
334
335 #define MID_MK_RXQ(SC,CNT,VCI,END,BCODE) \
336         EN_WRITE((SC), (SC)->drq_us, \
337                 MID_MK_RXQ_ENI((CNT), (VCI), (END), (BCODE))); 
338
339 #elif defined(MIDWAY_ADPONLY)
340
341 #define MID_MK_TXQ(SC,CNT,CHAN,END,JK) \
342         EN_WRITE((SC), (SC)->dtq_us, \
343                 MID_MK_TXQ_ADP((CNT), (CHAN), (END), (JK))); 
344
345 #define MID_MK_RXQ(SC,CNT,VCI,END,JK) \
346         EN_WRITE((SC), (SC)->drq_us, \
347                 MID_MK_RXQ_ADP((CNT), (VCI), (END), (JK))); 
348
349 #else
350
351 #define MID_MK_TXQ(SC,CNT,CHAN,END,JK_OR_BCODE) { \
352         if ((SC)->is_adaptec) \
353           EN_WRITE((SC), (SC)->dtq_us, \
354                   MID_MK_TXQ_ADP((CNT), (CHAN), (END), (JK_OR_BCODE))); \
355         else \
356           EN_WRITE((SC), (SC)->dtq_us, \
357                   MID_MK_TXQ_ENI((CNT), (CHAN), (END), (JK_OR_BCODE))); \
358         }
359
360 #define MID_MK_RXQ(SC,CNT,VCI,END,JK_OR_BCODE) { \
361         if ((SC)->is_adaptec) \
362           EN_WRITE((SC), (SC)->drq_us, \
363                   MID_MK_RXQ_ADP((CNT), (VCI), (END), (JK_OR_BCODE))); \
364         else \
365           EN_WRITE((SC), (SC)->drq_us, \
366                    MID_MK_RXQ_ENI((CNT), (VCI), (END), (JK_OR_BCODE))); \
367         }
368
369 #endif
370
371 /* add an item to the DTQ */
372 #define EN_DTQADD(SC,CNT,CHAN,JK_OR_BCODE,ADDR,LEN,END) { \
373         if (END) \
374           (SC)->dtq[MID_DTQ_A2REG((SC)->dtq_us)] = EN_DQ_MK(CHAN,LEN); \
375         MID_MK_TXQ(SC,CNT,CHAN,END,JK_OR_BCODE); \
376         (SC)->dtq_us += 4; \
377         EN_WRITE((SC), (SC)->dtq_us, (ADDR)); \
378         EN_WRAPADD(MID_DTQOFF, MID_DTQEND, (SC)->dtq_us, 4); \
379         (SC)->dtq_free--; \
380         if (END) \
381           EN_WRITE((SC), MID_DMA_WRTX, MID_DTQ_A2REG((SC)->dtq_us)); \
382 }
383
384 /* DRQ add macro */
385 #define EN_DRQADD(SC,CNT,VCI,JK_OR_BCODE,ADDR,LEN,SLOT,END) { \
386         if (END) \
387           (SC)->drq[MID_DRQ_A2REG((SC)->drq_us)] = EN_DQ_MK(SLOT,LEN); \
388         MID_MK_RXQ(SC,CNT,VCI,END,JK_OR_BCODE); \
389         (SC)->drq_us += 4; \
390         EN_WRITE((SC), (SC)->drq_us, (ADDR)); \
391         EN_WRAPADD(MID_DRQOFF, MID_DRQEND, (SC)->drq_us, 4); \
392         (SC)->drq_free--; \
393         if (END) \
394           EN_WRITE((SC), MID_DMA_WRRX, MID_DRQ_A2REG((SC)->drq_us)); \
395 }
396
397 /*
398  * the driver code
399  *
400  * the code is arranged in a specific way:
401  * [1] short/inline functions
402  * [2] autoconfig stuff
403  * [3] ioctl stuff
404  * [4] reset -> init -> trasmit -> intr -> receive functions
405  *
406  */
407
408 /***********************************************************************/
409
410 /*
411  * en_read: read a word from the card.   this is the only function
412  * that reads from the card.
413  */
414
415 STATIC INLINE u_int32_t en_read(sc, r)
416
417 struct en_softc *sc;
418 u_int32_t r;
419
420 {
421
422 #ifdef EN_DEBUG_RANGE
423   if (r > MID_MAXOFF || (r % 4))
424     panic("en_read out of range, r=0x%x", r);
425 #endif
426
427   return(bus_space_read_4(sc->en_memt, sc->en_base, r));
428 }
429
430 /*
431  * en_write: write a word to the card.   this is the only function that
432  * writes to the card.
433  */
434
435 STATIC INLINE void en_write(sc, r, v)
436
437 struct en_softc *sc;
438 u_int32_t r, v;
439
440 {
441 #ifdef EN_DEBUG_RANGE
442   if (r > MID_MAXOFF || (r % 4))
443     panic("en_write out of range, r=0x%x", r);
444 #endif
445
446   bus_space_write_4(sc->en_memt, sc->en_base, r, v);
447 }
448
449 /*
450  * en_k2sz: convert KBytes to a size parameter (a log2)
451  */
452
453 STATIC INLINE int en_k2sz(k)
454
455 int k;
456
457 {
458   switch(k) {
459     case 1:   return(0);
460     case 2:   return(1);
461     case 4:   return(2);
462     case 8:   return(3);
463     case 16:  return(4);
464     case 32:  return(5);
465     case 64:  return(6);
466     case 128: return(7);
467     default: panic("en_k2sz");
468   }
469   return(0);
470 }
471 #define en_log2(X) en_k2sz(X)
472
473
474 /*
475  * en_b2sz: convert a DMA burst code to its byte size
476  */
477
478 STATIC INLINE int en_b2sz(b)
479
480 int b;
481
482 {
483   switch (b) {
484     case MIDDMA_WORD:   return(1*4);
485     case MIDDMA_2WMAYBE:
486     case MIDDMA_2WORD:  return(2*4);
487     case MIDDMA_4WMAYBE:
488     case MIDDMA_4WORD:  return(4*4);
489     case MIDDMA_8WMAYBE:
490     case MIDDMA_8WORD:  return(8*4);
491     case MIDDMA_16WMAYBE:
492     case MIDDMA_16WORD: return(16*4);
493     default: panic("en_b2sz");
494   }
495   return(0);
496 }
497
498
499 /*
500  * en_sz2b: convert a burst size (bytes) to DMA burst code
501  */
502
503 STATIC INLINE int en_sz2b(sz)
504
505 int sz;
506
507 {
508   switch (sz) {
509     case 1*4:  return(MIDDMA_WORD);
510     case 2*4:  return(MIDDMA_2WORD);
511     case 4*4:  return(MIDDMA_4WORD);
512     case 8*4:  return(MIDDMA_8WORD);
513     case 16*4: return(MIDDMA_16WORD);
514     default: panic("en_sz2b");
515   }
516   return(0);
517 }
518
519
520 /*
521  * en_dqneed: calculate number of DTQ/DRQ's needed for a buffer
522  */
523
524 STATIC INLINE int en_dqneed(sc, data, len, tx)
525
526 struct en_softc *sc;
527 caddr_t data;
528 u_int len, tx;
529
530 {
531   int result, needalign, sz;
532
533 #if !defined(MIDWAY_ENIONLY)
534 #if !defined(MIDWAY_ADPONLY)
535     if (sc->is_adaptec)
536 #endif /* !MIDWAY_ADPONLY */
537       return(1);        /* adaptec can DMA anything in one go */
538 #endif
539     
540 #if !defined(MIDWAY_ADPONLY)
541     result = 0;
542     if (len < EN_MINDMA) {
543       if (!tx)                  /* XXX: conservative */
544         return(1);              /* will copy/DMA_JK */
545     }
546
547     if (tx) {                   /* byte burst? */
548       needalign = (((uintptr_t) (void *) data) % sizeof(u_int32_t));
549       if (needalign) {
550         result++;
551         sz = min(len, sizeof(u_int32_t) - needalign);
552         len -= sz;
553         data += sz;
554       }
555     }
556
557     if (sc->alburst && len) {
558       needalign = (((uintptr_t) (void *) data) & sc->bestburstmask);
559       if (needalign) {
560         result++;               /* alburst */
561         sz = min(len, sc->bestburstlen - needalign);
562         len -= sz;
563       }
564     }
565
566     if (len >= sc->bestburstlen) {
567       sz = len / sc->bestburstlen;
568       sz = sz * sc->bestburstlen;
569       len -= sz;
570       result++;                 /* best shot */
571     }
572     
573     if (len) {
574       result++;                 /* clean up */
575       if (tx && (len % sizeof(u_int32_t)) != 0)
576         result++;               /* byte cleanup */
577     }
578
579     return(result);
580 #endif  /* !MIDWAY_ADPONLY */
581 }
582
583
584 /*
585  * en_mget: get an mbuf chain that can hold totlen bytes and return it
586  * (for recv)   [based on am7990_get from if_le and ieget from if_ie]
587  * after this call the sum of all the m_len's in the chain will be totlen.
588  */
589
590 STATIC INLINE struct mbuf *en_mget(sc, totlen, drqneed)
591
592 struct en_softc *sc;
593 u_int totlen, *drqneed;
594
595 {
596   struct mbuf *m;
597   struct mbuf *top, **mp;
598   *drqneed = 0;
599
600   MGETHDR(m, M_DONTWAIT, MT_DATA);
601   if (m == NULL)
602     return(NULL);
603   m->m_pkthdr.rcvif = &sc->enif;
604   m->m_pkthdr.len = totlen;
605   m->m_len = MHLEN;
606   top = NULL;
607   mp = &top;
608   
609   /* if (top != NULL) then we've already got 1 mbuf on the chain */
610   while (totlen > 0) {
611     if (top) {
612       MGET(m, M_DONTWAIT, MT_DATA);
613       if (!m) {
614         m_freem(top);   
615         return(NULL);   /* out of mbufs */
616       }
617       m->m_len = MLEN;
618     }
619     if (totlen >= MINCLSIZE) {
620       MCLGET(m, M_DONTWAIT);
621       if ((m->m_flags & M_EXT) == 0) {
622         m_free(m);
623         m_freem(top);
624         return(NULL);     /* out of mbuf clusters */
625       }
626       m->m_len = MCLBYTES;
627     }
628     m->m_len = min(totlen, m->m_len);
629     totlen -= m->m_len;
630     *mp = m;
631     mp = &m->m_next;
632
633     *drqneed += en_dqneed(sc, m->m_data, m->m_len, 0);
634
635   }
636   return(top);
637 }
638
639 /***********************************************************************/
640
641 /*
642  * autoconfig stuff
643  */
644
645 void en_attach(sc)
646
647 struct en_softc *sc;
648
649 {
650   struct ifnet *ifp = &sc->enif;
651   int sz;
652   u_int32_t reg, lcv, check, ptr, sav, midvloc;
653
654   /*
655    * probe card to determine memory size.   the stupid ENI card always
656    * reports to PCI that it needs 4MB of space (2MB regs and 2MB RAM).
657    * if it has less than 2MB RAM the addresses wrap in the RAM address space.
658    * (i.e. on a 512KB card addresses 0x3ffffc, 0x37fffc, and 0x2ffffc
659    * are aliases for 0x27fffc  [note that RAM starts at offset 0x200000]).
660    */
661
662   if (sc->en_busreset)
663     sc->en_busreset(sc);
664   EN_WRITE(sc, MID_RESID, 0x0); /* reset card before touching RAM */
665   for (lcv = MID_PROBEOFF; lcv <= MID_MAXOFF ; lcv += MID_PROBSIZE) {
666     EN_WRITE(sc, lcv, lcv);     /* data[address] = address */
667     for (check = MID_PROBEOFF ; check < lcv ; check += MID_PROBSIZE) {
668       reg = EN_READ(sc, check);
669       if (reg != check) {               /* found an alias! */
670         goto done_probe;                /* and quit */
671       }
672     }
673   }
674 done_probe:
675   lcv -= MID_PROBSIZE;                  /* take one step back */
676   sc->en_obmemsz = (lcv + 4) - MID_RAMOFF;
677
678   /*
679    * determine the largest DMA burst supported
680    */
681
682   en_dmaprobe(sc);
683
684   /*
685    * "hello world"
686    */
687
688   if (sc->en_busreset)
689     sc->en_busreset(sc);
690   EN_WRITE(sc, MID_RESID, 0x0);         /* reset */
691   for (lcv = MID_RAMOFF ; lcv < MID_RAMOFF + sc->en_obmemsz ; lcv += 4)
692     EN_WRITE(sc, lcv, 0);       /* zero memory */
693
694   reg = EN_READ(sc, MID_RESID);
695
696   printf("%s: ATM midway v%d, board IDs %d.%d, %s%s%s, %ldKB on-board RAM\n",
697         sc->sc_dev.dv_xname, MID_VER(reg), MID_MID(reg), MID_DID(reg), 
698         (MID_IS_SABRE(reg)) ? "sabre controller, " : "",
699         (MID_IS_SUNI(reg)) ? "SUNI" : "Utopia",
700         (!MID_IS_SUNI(reg) && MID_IS_UPIPE(reg)) ? " (pipelined)" : "",
701         (long)(sc->en_obmemsz / 1024));
702
703   if (sc->is_adaptec) {
704     if (sc->bestburstlen == 64 && sc->alburst == 0)
705       printf("%s: passed 64 byte DMA test\n", sc->sc_dev.dv_xname);
706     else
707       printf("%s: FAILED DMA TEST: burst=%d, alburst=%d\n", 
708             sc->sc_dev.dv_xname, sc->bestburstlen, sc->alburst);
709   } else {
710     printf("%s: maximum DMA burst length = %d bytes%s\n", sc->sc_dev.dv_xname,
711           sc->bestburstlen, (sc->alburst) ? " (must align)" : "");
712   }
713
714   /*
715    * link into network subsystem and prepare card
716    */
717
718 #if defined(__NetBSD__) || defined(__OpenBSD__)
719   bcopy(sc->sc_dev.dv_xname, sc->enif.if_xname, IFNAMSIZ);
720 #endif
721   sc->enif.if_softc = sc;
722   ifp->if_flags = IFF_SIMPLEX|IFF_NOTRAILERS;
723   ifp->if_ioctl = en_ioctl;
724   ifp->if_output = atm_output;
725   ifp->if_start = en_start;
726
727   /*
728    * init softc
729    */
730
731   for (lcv = 0 ; lcv < MID_N_VC ; lcv++) {
732     sc->rxvc2slot[lcv] = RX_NONE;
733     sc->txspeed[lcv] = 0;       /* full */
734     sc->txvc2slot[lcv] = 0;     /* full speed == slot 0 */
735   }
736
737   sz = sc->en_obmemsz - (MID_BUFOFF - MID_RAMOFF);
738   ptr = sav = MID_BUFOFF;
739   ptr = roundup(ptr, EN_TXSZ * 1024);   /* align */
740   sz = sz - (ptr - sav);
741   if (EN_TXSZ*1024 * EN_NTX > sz) {
742     printf("%s: EN_NTX/EN_TXSZ too big\n", sc->sc_dev.dv_xname);
743     return;
744   }
745   for (lcv = 0 ; lcv < EN_NTX ; lcv++) {
746     sc->txslot[lcv].mbsize = 0;
747     sc->txslot[lcv].start = ptr;
748     ptr += (EN_TXSZ * 1024);
749     sz -= (EN_TXSZ * 1024);
750     sc->txslot[lcv].stop = ptr;
751     sc->txslot[lcv].nref = 0;
752     bzero(&sc->txslot[lcv].indma, sizeof(sc->txslot[lcv].indma));
753     bzero(&sc->txslot[lcv].q, sizeof(sc->txslot[lcv].q));
754 #ifdef EN_DEBUG
755     printf("%s: tx%d: start 0x%x, stop 0x%x\n", sc->sc_dev.dv_xname, lcv,
756                 sc->txslot[lcv].start, sc->txslot[lcv].stop);
757 #endif
758   }
759
760   sav = ptr;
761   ptr = roundup(ptr, EN_RXSZ * 1024);   /* align */
762   sz = sz - (ptr - sav);
763   sc->en_nrx = sz / (EN_RXSZ * 1024);
764   if (sc->en_nrx <= 0) {
765     printf("%s: EN_NTX/EN_TXSZ/EN_RXSZ too big\n", sc->sc_dev.dv_xname);
766     return;
767   }
768
769   /* 
770    * ensure that there is always one VC slot on the service list free
771    * so that we can tell the difference between a full and empty list.
772    */
773   if (sc->en_nrx >= MID_N_VC)
774     sc->en_nrx = MID_N_VC - 1;
775
776   for (lcv = 0 ; lcv < sc->en_nrx ; lcv++) {
777     sc->rxslot[lcv].rxhand = NULL;
778     sc->rxslot[lcv].oth_flags = ENOTHER_FREE;
779     bzero(&sc->rxslot[lcv].indma, sizeof(sc->rxslot[lcv].indma));
780     bzero(&sc->rxslot[lcv].q, sizeof(sc->rxslot[lcv].q));
781     midvloc = sc->rxslot[lcv].start = ptr;
782     ptr += (EN_RXSZ * 1024);
783     sz -= (EN_RXSZ * 1024);
784     sc->rxslot[lcv].stop = ptr;
785     midvloc = midvloc - MID_RAMOFF;
786     midvloc = (midvloc & ~((EN_RXSZ*1024) - 1)) >> 2; /* mask, cvt to words */
787     midvloc = midvloc >> MIDV_LOCTOPSHFT;  /* we only want the top 11 bits */
788     midvloc = (midvloc & MIDV_LOCMASK) << MIDV_LOCSHIFT;
789     sc->rxslot[lcv].mode = midvloc | 
790         (en_k2sz(EN_RXSZ) << MIDV_SZSHIFT) | MIDV_TRASH;
791
792 #ifdef EN_DEBUG
793     printf("%s: rx%d: start 0x%x, stop 0x%x, mode 0x%x\n", sc->sc_dev.dv_xname,
794         lcv, sc->rxslot[lcv].start, sc->rxslot[lcv].stop, sc->rxslot[lcv].mode);
795 #endif
796   }
797
798 #ifdef EN_STAT
799   sc->vtrash = sc->otrash = sc->mfix = sc->txmbovr = sc->dmaovr = 0;
800   sc->txoutspace = sc->txdtqout = sc->launch = sc->lheader = sc->ltail = 0;
801   sc->hwpull = sc->swadd = sc->rxqnotus = sc->rxqus = sc->rxoutboth = 0;
802   sc->rxdrqout = sc->ttrash = sc->rxmbufout = sc->mfixfail = 0;
803   sc->headbyte = sc->tailbyte = sc->tailflush = 0;
804 #endif
805   sc->need_drqs = sc->need_dtqs = 0;
806
807   printf("%s: %d %dKB receive buffers, %d %dKB transmit buffers allocated\n",
808         sc->sc_dev.dv_xname, sc->en_nrx, EN_RXSZ, EN_NTX, EN_TXSZ);
809
810   printf("%s: End Station Identifier (mac address) %6D\n",
811          sc->sc_dev.dv_xname, sc->macaddr, ":");
812
813   /*
814    * final commit
815    */
816
817   if_attach(ifp);
818   atm_ifattach(ifp); 
819
820 #if NBPF > 0 
821   BPFATTACH(ifp, DLT_ATM_RFC1483, sizeof(struct atmllc));
822 #endif
823 }
824
825
826 /*
827  * en_dmaprobe: helper function for en_attach.
828  *
829  * see how the card handles DMA by running a few DMA tests.   we need
830  * to figure out the largest number of bytes we can DMA in one burst
831  * ("bestburstlen"), and if the starting address for a burst needs to
832  * be aligned on any sort of boundary or not ("alburst").
833  *
834  * typical findings:
835  * sparc1: bestburstlen=4, alburst=0 (ick, broken DMA!)
836  * sparc2: bestburstlen=64, alburst=1
837  * p166:   bestburstlen=64, alburst=0 
838  */
839
840 #if 1 /* __FreeBSD__ */
841 #define NBURSTS 3       /* number of bursts to use for dmaprobe */
842 #define BOUNDARY 1024   /* test misaligned dma crossing the bounday.
843                            should be n * 64.  at least 64*(NBURSTS+1).
844                            dell P6 with EDO DRAM has 1K bounday problem */
845 #endif
846
847 STATIC void en_dmaprobe(sc)
848
849 struct en_softc *sc;
850
851 {
852 #ifdef NBURSTS
853   /* be careful. kernel stack is only 8K */
854   u_int8_t buffer[BOUNDARY * 2 + 64 * (NBURSTS + 1)]; 
855 #else
856   u_int32_t srcbuf[64], dstbuf[64];
857 #endif
858   u_int8_t *sp, *dp;
859   int bestalgn, bestnotalgn, lcv, try;
860
861   sc->alburst = 0;
862
863 #ifdef NBURSTS
864   /* setup src and dst buf at the end of the boundary */
865   sp = (u_int8_t *)roundup((uintptr_t)(void *)buffer, 64);
866   while (((uintptr_t)(void *)sp & (BOUNDARY - 1)) != (BOUNDARY - 64))
867       sp += 64;
868   dp = sp + BOUNDARY;
869
870   /*
871    * we can't dma across page boundary so that, if buf is at a page
872    * boundary, move it to the next page.  but still either src or dst
873    * will be at the boundary, which should be ok.
874    */
875   if ((((uintptr_t)(void *)sp + 64) & PAGE_MASK) == 0)
876       sp += 64;
877   if ((((uintptr_t)(void *)dp + 64) & PAGE_MASK) == 0)
878       dp += 64;
879 #else /* !NBURSTS */
880   sp = (u_int8_t *) srcbuf;
881   while ((((unsigned long) sp) % MIDDMA_MAXBURST) != 0)
882     sp += 4;
883   dp = (u_int8_t *) dstbuf;
884   while ((((unsigned long) dp) % MIDDMA_MAXBURST) != 0)
885     dp += 4;
886 #endif /* !NBURSTS */
887
888   bestalgn = bestnotalgn = en_dmaprobe_doit(sc, sp, dp, 0);
889
890   for (lcv = 4 ; lcv < MIDDMA_MAXBURST ; lcv += 4) {
891     try = en_dmaprobe_doit(sc, sp+lcv, dp+lcv, 0);
892 #ifdef NBURSTS
893     if (try < bestnotalgn) {
894       bestnotalgn = try;
895       break;
896     }
897 #else
898     if (try < bestnotalgn)
899       bestnotalgn = try;
900 #endif
901   }
902
903   if (bestalgn != bestnotalgn)          /* need bursts aligned */
904     sc->alburst = 1;
905
906   sc->bestburstlen = bestalgn;
907   sc->bestburstshift = en_log2(bestalgn);
908   sc->bestburstmask = sc->bestburstlen - 1; /* must be power of 2 */
909   sc->bestburstcode = en_sz2b(bestalgn);
910
911 #if 1 /* __FreeBSD__ */
912   /*
913    * correct pci chipsets should be able to handle misaligned-64-byte DMA.
914    * but there are too many broken chipsets around.  we try to work around
915    * by finding the best workable dma size, but still some broken machines
916    * exhibit the problem later. so warn it here.
917    */
918   if (bestalgn != 64 || sc->alburst != 0) {
919     printf("%s: WARNING: DMA test detects a broken PCI chipset!\n", 
920            sc->sc_dev.dv_xname);
921     printf("     trying to work around the problem...  but if this doesn't\n");
922     printf("     work for you, you'd better switch to a newer motherboard.\n");
923   }
924 #endif /* 1 */
925     return;
926 }
927
928
929 /*
930  * en_dmaprobe_doit: do actual testing
931  */
932
933 static int
934 en_dmaprobe_doit(sc, sp, dp, wmtry)
935
936 struct en_softc *sc;
937 u_int8_t *sp, *dp;
938 int wmtry;
939
940 {
941   int lcv, retval = 4, cnt, count;
942   u_int32_t reg, bcode, midvloc;
943
944   /*
945    * set up a 1k buffer at MID_BUFOFF
946    */
947
948   if (sc->en_busreset)
949     sc->en_busreset(sc);
950   EN_WRITE(sc, MID_RESID, 0x0); /* reset card before touching RAM */
951
952   midvloc = ((MID_BUFOFF - MID_RAMOFF) / sizeof(u_int32_t)) >> MIDV_LOCTOPSHFT;
953   EN_WRITE(sc, MIDX_PLACE(0), MIDX_MKPLACE(en_k2sz(1), midvloc));
954   EN_WRITE(sc, MID_VC(0), (midvloc << MIDV_LOCSHIFT) 
955                 | (en_k2sz(1) << MIDV_SZSHIFT) | MIDV_TRASH);
956   EN_WRITE(sc, MID_DST_RP(0), 0);
957   EN_WRITE(sc, MID_WP_ST_CNT(0), 0);
958
959 #ifdef NBURSTS
960   for (lcv = 0 ; lcv < 64*NBURSTS; lcv++)       /* set up sample data */
961 #else
962   for (lcv = 0 ; lcv < 68 ; lcv++)              /* set up sample data */
963 #endif
964     sp[lcv] = lcv+1;
965   EN_WRITE(sc, MID_MAST_CSR, MID_MCSR_ENDMA);   /* enable DMA (only) */
966
967   sc->drq_chip = MID_DRQ_REG2A(EN_READ(sc, MID_DMA_RDRX));
968   sc->dtq_chip = MID_DTQ_REG2A(EN_READ(sc, MID_DMA_RDTX));
969
970   /*
971    * try it now . . .  DMA it out, then DMA it back in and compare
972    *
973    * note: in order to get the dma stuff to reverse directions it wants
974    * the "end" flag set!   since we are not dma'ing valid data we may
975    * get an ident mismatch interrupt (which we will ignore).
976    *
977    * note: we've got two different tests rolled up in the same loop
978    * if (wmtry) 
979    *   then we are doing a wmaybe test and wmtry is a byte count
980    *   else we are doing a burst test
981    */
982
983   for (lcv = 8 ; lcv <= MIDDMA_MAXBURST ; lcv = lcv * 2) {
984
985 #ifdef EN_DEBUG
986     printf("DMA test lcv=%d, sp=0x%x, dp=0x%x, wmtry=%d\n",
987            lcv, sp, dp, wmtry);
988 #endif
989
990     /* zero SRAM and dest buffer */
991     for (cnt = 0 ; cnt < 1024; cnt += 4) 
992       EN_WRITE(sc, MID_BUFOFF+cnt, 0);  /* zero memory */
993 #ifdef NBURSTS
994     for (cnt = 0 ; cnt < 64*NBURSTS; cnt++) 
995 #else
996     for (cnt = 0 ; cnt < 68  ; cnt++) 
997 #endif
998       dp[cnt] = 0;
999
1000     if (wmtry) {
1001       count = (sc->bestburstlen - sizeof(u_int32_t)) / sizeof(u_int32_t);
1002       bcode = en_dmaplan[count].bcode;
1003       count = wmtry >> en_dmaplan[count].divshift;
1004     } else {
1005       bcode = en_sz2b(lcv);
1006       count = 1;
1007     }
1008 #ifdef NBURSTS
1009     /* build lcv-byte-DMA x NBURSTS */
1010     if (sc->is_adaptec)
1011       EN_WRITE(sc, sc->dtq_chip, MID_MK_TXQ_ADP(lcv*NBURSTS, 0, MID_DMA_END, 0));
1012     else
1013       EN_WRITE(sc, sc->dtq_chip, MID_MK_TXQ_ENI(count*NBURSTS, 0, MID_DMA_END, bcode));
1014     EN_WRITE(sc, sc->dtq_chip+4, vtophys(sp));
1015     EN_WRAPADD(MID_DTQOFF, MID_DTQEND, sc->dtq_chip, 8);
1016     EN_WRITE(sc, MID_DMA_WRTX, MID_DTQ_A2REG(sc->dtq_chip));
1017     cnt = 1000;
1018     while (EN_READ(sc, MID_DMA_RDTX) != MID_DTQ_A2REG(sc->dtq_chip)) {
1019       DELAY(1);
1020       cnt--;
1021       if (cnt == 0) {
1022         printf("%s: unexpected timeout in tx DMA test\n", sc->sc_dev.dv_xname);
1023 /*
1024         printf("  alignment=0x%x, burst size=%d, dma addr reg=0x%x\n",
1025                (u_long)sp & 63, lcv, EN_READ(sc, MID_DMA_ADDR));
1026 */             
1027         return(retval);         /* timeout, give up */
1028       }
1029     }
1030 #else /* !NBURSTS */
1031     if (sc->is_adaptec)
1032       EN_WRITE(sc, sc->dtq_chip, MID_MK_TXQ_ADP(lcv, 0, MID_DMA_END, 0));
1033     else
1034       EN_WRITE(sc, sc->dtq_chip, MID_MK_TXQ_ENI(count, 0, MID_DMA_END, bcode));
1035     EN_WRITE(sc, sc->dtq_chip+4, vtophys(sp));
1036     EN_WRITE(sc, MID_DMA_WRTX, MID_DTQ_A2REG(sc->dtq_chip+8));
1037     cnt = 1000;
1038     while (EN_READ(sc, MID_DMA_RDTX) == MID_DTQ_A2REG(sc->dtq_chip)) {
1039       DELAY(1);
1040       cnt--;
1041       if (cnt == 0) {
1042         printf("%s: unexpected timeout in tx DMA test\n", sc->sc_dev.dv_xname);
1043         return(retval);         /* timeout, give up */
1044       }
1045     }
1046     EN_WRAPADD(MID_DTQOFF, MID_DTQEND, sc->dtq_chip, 8);
1047 #endif /* !NBURSTS */
1048     reg = EN_READ(sc, MID_INTACK); 
1049     if ((reg & MID_INT_DMA_TX) != MID_INT_DMA_TX) {
1050       printf("%s: unexpected status in tx DMA test: 0x%x\n", 
1051                 sc->sc_dev.dv_xname, reg);
1052       return(retval);
1053     }
1054     EN_WRITE(sc, MID_MAST_CSR, MID_MCSR_ENDMA);   /* re-enable DMA (only) */
1055
1056     /* "return to sender..."  address is known ... */
1057
1058 #ifdef NBURSTS
1059     /* build lcv-byte-DMA x NBURSTS */
1060     if (sc->is_adaptec)
1061       EN_WRITE(sc, sc->drq_chip, MID_MK_RXQ_ADP(lcv*NBURSTS, 0, MID_DMA_END, 0));
1062     else
1063       EN_WRITE(sc, sc->drq_chip, MID_MK_RXQ_ENI(count*NBURSTS, 0, MID_DMA_END, bcode));
1064     EN_WRITE(sc, sc->drq_chip+4, vtophys(dp));
1065     EN_WRAPADD(MID_DRQOFF, MID_DRQEND, sc->drq_chip, 8);
1066     EN_WRITE(sc, MID_DMA_WRRX, MID_DRQ_A2REG(sc->drq_chip));
1067     cnt = 1000;
1068     while (EN_READ(sc, MID_DMA_RDRX) != MID_DRQ_A2REG(sc->drq_chip)) {
1069       DELAY(1);
1070       cnt--;
1071       if (cnt == 0) {
1072         printf("%s: unexpected timeout in rx DMA test\n", sc->sc_dev.dv_xname);
1073         return(retval);         /* timeout, give up */
1074       }
1075     }
1076 #else /* !NBURSTS */
1077     if (sc->is_adaptec)
1078       EN_WRITE(sc, sc->drq_chip, MID_MK_RXQ_ADP(lcv, 0, MID_DMA_END, 0));
1079     else
1080       EN_WRITE(sc, sc->drq_chip, MID_MK_RXQ_ENI(count, 0, MID_DMA_END, bcode));
1081     EN_WRITE(sc, sc->drq_chip+4, vtophys(dp));
1082     EN_WRITE(sc, MID_DMA_WRRX, MID_DRQ_A2REG(sc->drq_chip+8));
1083     cnt = 1000;
1084     while (EN_READ(sc, MID_DMA_RDRX) == MID_DRQ_A2REG(sc->drq_chip)) {
1085       DELAY(1);
1086       cnt--;
1087       if (cnt == 0) {
1088         printf("%s: unexpected timeout in rx DMA test\n", sc->sc_dev.dv_xname);
1089         return(retval);         /* timeout, give up */
1090       }
1091     }
1092     EN_WRAPADD(MID_DRQOFF, MID_DRQEND, sc->drq_chip, 8);
1093 #endif /* !NBURSTS */
1094     reg = EN_READ(sc, MID_INTACK); 
1095     if ((reg & MID_INT_DMA_RX) != MID_INT_DMA_RX) {
1096       printf("%s: unexpected status in rx DMA test: 0x%x\n", 
1097                 sc->sc_dev.dv_xname, reg);
1098       return(retval);
1099     }
1100     EN_WRITE(sc, MID_MAST_CSR, MID_MCSR_ENDMA);   /* re-enable DMA (only) */
1101
1102     if (wmtry) {
1103       return(bcmp(sp, dp, wmtry));  /* wmtry always exits here, no looping */
1104     }
1105   
1106 #ifdef NBURSTS
1107     if (bcmp(sp, dp, lcv * NBURSTS)) {
1108 /*      printf("DMA test failed! lcv=%d, sp=0x%x, dp=0x%x\n", lcv, sp, dp); */
1109       return(retval);           /* failed, use last value */
1110     }
1111 #else
1112     if (bcmp(sp, dp, lcv))
1113       return(retval);           /* failed, use last value */
1114 #endif
1115
1116     retval = lcv;
1117
1118   }
1119   return(retval);               /* studly 64 byte DMA present!  oh baby!! */
1120 }
1121
1122 /***********************************************************************/
1123
1124 /*
1125  * en_ioctl: handle ioctl requests
1126  *
1127  * NOTE: if you add an ioctl to set txspeed, you should choose a new
1128  * TX channel/slot.   Choose the one with the lowest sc->txslot[slot].nref
1129  * value, subtract one from sc->txslot[0].nref, add one to the
1130  * sc->txslot[slot].nref, set sc->txvc2slot[vci] = slot, and then set
1131  * txspeed[vci].
1132  */
1133
1134 STATIC int en_ioctl(ifp, cmd, data, cr)
1135
1136 struct ifnet *ifp;
1137 EN_IOCTL_CMDT cmd;
1138 caddr_t data;
1139 struct ucred *cr;
1140
1141 {
1142     struct en_softc *sc = (struct en_softc *) ifp->if_softc;
1143     struct ifaddr *ifa = (struct ifaddr *) data;
1144     struct ifreq *ifr = (struct ifreq *) data;
1145     struct atm_pseudoioctl *api = (struct atm_pseudoioctl *)data;
1146 #ifdef NATM
1147     struct atm_rawioctl *ario = (struct atm_rawioctl *)data;
1148     int slot;
1149 #endif
1150     int s, error = 0;
1151
1152     s = splnet();
1153
1154     switch (cmd) {
1155         case SIOCATMENA:                /* enable circuit for recv */
1156                 error = en_rxctl(sc, api, 1);
1157                 break;
1158
1159         case SIOCATMDIS:                /* disable circuit for recv */
1160                 error = en_rxctl(sc, api, 0);
1161                 break;
1162
1163 #ifdef NATM
1164         case SIOCXRAWATM:
1165                 if ((slot = sc->rxvc2slot[ario->npcb->npcb_vci]) == RX_NONE) {
1166                         error = EINVAL;
1167                         break;
1168                 }
1169                 if (ario->rawvalue > EN_RXSZ*1024)
1170                         ario->rawvalue = EN_RXSZ*1024;
1171                 if (ario->rawvalue) {
1172                         sc->rxslot[slot].oth_flags |= ENOTHER_RAW;
1173                         sc->rxslot[slot].raw_threshold = ario->rawvalue;
1174                 } else {
1175                         sc->rxslot[slot].oth_flags &= (~ENOTHER_RAW);
1176                         sc->rxslot[slot].raw_threshold = 0;
1177                 }
1178 #ifdef EN_DEBUG
1179                 printf("%s: rxvci%d: turn %s raw (boodi) mode\n",
1180                         sc->sc_dev.dv_xname, ario->npcb->npcb_vci,
1181                         (ario->rawvalue) ? "on" : "off");
1182 #endif
1183                 break;
1184 #endif
1185         case SIOCSIFADDR: 
1186                 ifp->if_flags |= IFF_UP;
1187 #if defined(INET) || defined(INET6)
1188                 if (ifa->ifa_addr->sa_family == AF_INET
1189                     || ifa->ifa_addr->sa_family == AF_INET6) {
1190                         en_reset(sc);
1191                         en_init(sc);
1192                         ifa->ifa_rtrequest = atm_rtrequest; /* ??? */
1193                         break;
1194                 }
1195 #endif /* INET */
1196                 /* what to do if not INET? */
1197                 en_reset(sc);
1198                 en_init(sc);
1199                 break;
1200
1201         case SIOCGIFADDR: 
1202                 error = EINVAL;
1203                 break;
1204
1205         case SIOCSIFFLAGS: 
1206                 error = EINVAL;
1207                 break;
1208
1209 #if defined(SIOCSIFMTU)         /* ??? copied from if_de */
1210 #if !defined(ifr_mtu)
1211 #define ifr_mtu ifr_metric
1212 #endif
1213         case SIOCSIFMTU:
1214             /*
1215              * Set the interface MTU.
1216              */
1217 #ifdef notsure
1218             if (ifr->ifr_mtu > ATMMTU) {
1219                 error = EINVAL;
1220                 break;
1221             }
1222 #endif
1223             ifp->if_mtu = ifr->ifr_mtu;
1224                 /* XXXCDC: do we really need to reset on MTU size change? */
1225             en_reset(sc);
1226             en_init(sc);
1227             break;
1228 #endif /* SIOCSIFMTU */
1229
1230         default: 
1231             error = EINVAL;
1232             break;
1233     }
1234     splx(s);
1235     return error;
1236 }
1237
1238
1239 /*
1240  * en_rxctl: turn on and off VCs for recv.
1241  */
1242
1243 STATIC int en_rxctl(sc, pi, on)
1244
1245 struct en_softc *sc;
1246 struct atm_pseudoioctl *pi;
1247 int on;
1248
1249 {
1250   u_int s, vci, flags, slot;
1251   u_int32_t oldmode, newmode;
1252
1253   vci = ATM_PH_VCI(&pi->aph);
1254   flags = ATM_PH_FLAGS(&pi->aph);
1255
1256 #ifdef EN_DEBUG
1257   printf("%s: %s vpi=%d, vci=%d, flags=%d\n", sc->sc_dev.dv_xname,
1258         (on) ? "enable" : "disable", ATM_PH_VPI(&pi->aph), vci, flags);
1259 #endif
1260
1261   if (ATM_PH_VPI(&pi->aph) || vci >= MID_N_VC)
1262     return(EINVAL);
1263
1264   /*
1265    * turn on VCI!
1266    */
1267
1268   if (on) {
1269     if (sc->rxvc2slot[vci] != RX_NONE)
1270       return(EINVAL);
1271     for (slot = 0 ; slot < sc->en_nrx ; slot++)
1272       if (sc->rxslot[slot].oth_flags & ENOTHER_FREE)
1273         break;
1274     if (slot == sc->en_nrx)
1275       return(ENOSPC);
1276     sc->rxvc2slot[vci] = slot;
1277     sc->rxslot[slot].rxhand = NULL;
1278     oldmode = sc->rxslot[slot].mode;
1279     newmode = (flags & ATM_PH_AAL5) ? MIDV_AAL5 : MIDV_NOAAL;
1280     sc->rxslot[slot].mode = MIDV_SETMODE(oldmode, newmode);
1281     sc->rxslot[slot].atm_vci = vci;
1282     sc->rxslot[slot].atm_flags = flags;
1283     sc->rxslot[slot].oth_flags = 0;
1284     sc->rxslot[slot].rxhand = pi->rxhand;
1285     if (sc->rxslot[slot].indma.ifq_head || sc->rxslot[slot].q.ifq_head)
1286       panic("en_rxctl: left over mbufs on enable");
1287     sc->txspeed[vci] = 0;       /* full speed to start */
1288     sc->txvc2slot[vci] = 0;     /* init value */
1289     sc->txslot[0].nref++;       /* bump reference count */
1290     en_loadvc(sc, vci);         /* does debug printf for us */
1291     return(0);
1292   }
1293
1294   /*
1295    * turn off VCI
1296    */
1297
1298   if (sc->rxvc2slot[vci] == RX_NONE)
1299     return(EINVAL);
1300   slot = sc->rxvc2slot[vci];
1301   if ((sc->rxslot[slot].oth_flags & (ENOTHER_FREE|ENOTHER_DRAIN)) != 0)
1302     return(EINVAL);
1303   s = splimp();         /* block out enintr() */
1304   oldmode = EN_READ(sc, MID_VC(vci));
1305   newmode = MIDV_SETMODE(oldmode, MIDV_TRASH) & ~MIDV_INSERVICE;
1306   EN_WRITE(sc, MID_VC(vci), (newmode | (oldmode & MIDV_INSERVICE)));
1307                 /* halt in tracks, be careful to preserve inserivce bit */
1308   DELAY(27);
1309   sc->rxslot[slot].rxhand = NULL;
1310   sc->rxslot[slot].mode = newmode;
1311
1312   sc->txslot[sc->txvc2slot[vci]].nref--;
1313   sc->txspeed[vci] = 0;
1314   sc->txvc2slot[vci] = 0;
1315
1316   /* if stuff is still going on we are going to have to drain it out */
1317   if (sc->rxslot[slot].indma.ifq_head || 
1318                 sc->rxslot[slot].q.ifq_head ||
1319                 (sc->rxslot[slot].oth_flags & ENOTHER_SWSL) != 0) {
1320     sc->rxslot[slot].oth_flags |= ENOTHER_DRAIN;
1321   } else {
1322     sc->rxslot[slot].oth_flags = ENOTHER_FREE;
1323     sc->rxslot[slot].atm_vci = RX_NONE;
1324     sc->rxvc2slot[vci] = RX_NONE;
1325   }
1326   splx(s);              /* enable enintr() */
1327 #ifdef EN_DEBUG
1328   printf("%s: rx%d: VCI %d is now %s\n", sc->sc_dev.dv_xname, slot, vci,
1329         (sc->rxslot[slot].oth_flags & ENOTHER_DRAIN) ? "draining" : "free");
1330 #endif
1331   return(0);
1332 }
1333
1334 /***********************************************************************/
1335
1336 /*
1337  * en_reset: reset the board, throw away work in progress.
1338  * must en_init to recover.
1339  */
1340
1341 void en_reset(sc)
1342
1343 struct en_softc *sc;
1344
1345 {
1346   struct mbuf *m;
1347   int lcv, slot;
1348
1349 #ifdef EN_DEBUG
1350   printf("%s: reset\n", sc->sc_dev.dv_xname);
1351 #endif
1352
1353   if (sc->en_busreset)
1354     sc->en_busreset(sc);
1355   EN_WRITE(sc, MID_RESID, 0x0); /* reset hardware */
1356
1357   /*
1358    * recv: dump any mbufs we are dma'ing into, if DRAINing, then a reset
1359    * will free us!
1360    */
1361
1362   for (lcv = 0 ; lcv < MID_N_VC ; lcv++) {
1363     if (sc->rxvc2slot[lcv] == RX_NONE)
1364       continue;
1365     slot = sc->rxvc2slot[lcv];
1366     while (1) {
1367       IF_DEQUEUE(&sc->rxslot[slot].indma, m);
1368       if (m == NULL) 
1369         break;          /* >>> exit 'while(1)' here <<< */
1370       m_freem(m);
1371     }
1372     while (1) {
1373       IF_DEQUEUE(&sc->rxslot[slot].q, m);
1374       if (m == NULL) 
1375         break;          /* >>> exit 'while(1)' here <<< */
1376       m_freem(m);
1377     }
1378     sc->rxslot[slot].oth_flags &= ~ENOTHER_SWSL;
1379     if (sc->rxslot[slot].oth_flags & ENOTHER_DRAIN) {
1380       sc->rxslot[slot].oth_flags = ENOTHER_FREE;
1381       sc->rxvc2slot[lcv] = RX_NONE;
1382 #ifdef EN_DEBUG
1383   printf("%s: rx%d: VCI %d is now free\n", sc->sc_dev.dv_xname, slot, lcv);
1384 #endif
1385     }
1386   }
1387
1388   /*
1389    * xmit: dump everything
1390    */
1391
1392   for (lcv = 0 ; lcv < EN_NTX ; lcv++) {
1393     while (1) {
1394       IF_DEQUEUE(&sc->txslot[lcv].indma, m);
1395       if (m == NULL) 
1396         break;          /* >>> exit 'while(1)' here <<< */
1397       m_freem(m);
1398     }
1399     while (1) {
1400       IF_DEQUEUE(&sc->txslot[lcv].q, m);
1401       if (m == NULL) 
1402         break;          /* >>> exit 'while(1)' here <<< */
1403       m_freem(m);
1404     }
1405
1406     sc->txslot[lcv].mbsize = 0;
1407   }
1408
1409   return;
1410 }
1411
1412
1413 /*
1414  * en_init: init board and sync the card with the data in the softc.
1415  */
1416
1417 STATIC void en_init(sc)
1418
1419 struct en_softc *sc;
1420
1421 {
1422   int vc, slot;
1423   u_int32_t loc;
1424
1425   if ((sc->enif.if_flags & IFF_UP) == 0) {
1426 #ifdef EN_DEBUG
1427     printf("%s: going down\n", sc->sc_dev.dv_xname);
1428 #endif
1429     en_reset(sc);                       /* to be safe */
1430     sc->enif.if_flags &= ~IFF_RUNNING;  /* disable */
1431     return;
1432   }
1433
1434 #ifdef EN_DEBUG
1435   printf("%s: going up\n", sc->sc_dev.dv_xname);
1436 #endif
1437   sc->enif.if_flags |= IFF_RUNNING;     /* enable */
1438
1439   if (sc->en_busreset)
1440     sc->en_busreset(sc);
1441   EN_WRITE(sc, MID_RESID, 0x0);         /* reset */
1442
1443   /*
1444    * init obmem data structures: vc tab, dma q's, slist.
1445    *
1446    * note that we set drq_free/dtq_free to one less than the total number
1447    * of DTQ/DRQs present.   we do this because the card uses the condition
1448    * (drq_chip == drq_us) to mean "list is empty"... but if you allow the
1449    * circular list to be completely full then (drq_chip == drq_us) [i.e.
1450    * the drq_us pointer will wrap all the way around].   by restricting
1451    * the number of active requests to (N - 1) we prevent the list from
1452    * becoming completely full.    note that the card will sometimes give
1453    * us an interrupt for a DTQ/DRQ we have already processes... this helps
1454    * keep that interrupt from messing us up.
1455    */
1456
1457   for (vc = 0 ; vc < MID_N_VC ; vc++) 
1458     en_loadvc(sc, vc);
1459
1460   bzero(&sc->drq, sizeof(sc->drq));
1461   sc->drq_free = MID_DRQ_N - 1;         /* N - 1 */
1462   sc->drq_chip = MID_DRQ_REG2A(EN_READ(sc, MID_DMA_RDRX));
1463   EN_WRITE(sc, MID_DMA_WRRX, MID_DRQ_A2REG(sc->drq_chip)); 
1464                                                 /* ensure zero queue */
1465   sc->drq_us = sc->drq_chip;
1466
1467   bzero(&sc->dtq, sizeof(sc->dtq));
1468   sc->dtq_free = MID_DTQ_N - 1;         /* N - 1 */
1469   sc->dtq_chip = MID_DTQ_REG2A(EN_READ(sc, MID_DMA_RDTX));
1470   EN_WRITE(sc, MID_DMA_WRTX, MID_DRQ_A2REG(sc->dtq_chip)); 
1471                                                 /* ensure zero queue */
1472   sc->dtq_us = sc->dtq_chip;
1473
1474   sc->hwslistp = MID_SL_REG2A(EN_READ(sc, MID_SERV_WRITE));
1475   sc->swsl_size = sc->swsl_head = sc->swsl_tail = 0;
1476
1477 #ifdef EN_DEBUG
1478   printf("%s: drq free/chip: %d/0x%x, dtq free/chip: %d/0x%x, hwslist: 0x%x\n", 
1479     sc->sc_dev.dv_xname, sc->drq_free, sc->drq_chip, 
1480     sc->dtq_free, sc->dtq_chip, sc->hwslistp);
1481 #endif
1482
1483   for (slot = 0 ; slot < EN_NTX ; slot++) {
1484     sc->txslot[slot].bfree = EN_TXSZ * 1024;
1485     EN_WRITE(sc, MIDX_READPTR(slot), 0);
1486     EN_WRITE(sc, MIDX_DESCSTART(slot), 0);
1487     loc = sc->txslot[slot].cur = sc->txslot[slot].start;
1488     loc = loc - MID_RAMOFF;
1489     loc = (loc & ~((EN_TXSZ*1024) - 1)) >> 2; /* mask, cvt to words */
1490     loc = loc >> MIDV_LOCTOPSHFT;       /* top 11 bits */
1491     EN_WRITE(sc, MIDX_PLACE(slot), MIDX_MKPLACE(en_k2sz(EN_TXSZ), loc));
1492 #ifdef EN_DEBUG
1493     printf("%s: tx%d: place 0x%x\n", sc->sc_dev.dv_xname,  slot,
1494         EN_READ(sc, MIDX_PLACE(slot)));
1495 #endif
1496   }
1497
1498   /*
1499    * enable!
1500    */
1501
1502   EN_WRITE(sc, MID_INTENA, MID_INT_TX|MID_INT_DMA_OVR|MID_INT_IDENT|
1503         MID_INT_LERR|MID_INT_DMA_ERR|MID_INT_DMA_RX|MID_INT_DMA_TX|
1504         MID_INT_SERVICE| /* >>> MID_INT_SUNI| XXXCDC<<< */ MID_INT_STATS);
1505   EN_WRITE(sc, MID_MAST_CSR, MID_SETIPL(sc->ipl)|MID_MCSR_ENDMA|
1506         MID_MCSR_ENTX|MID_MCSR_ENRX);
1507
1508 }
1509
1510
1511 /*
1512  * en_loadvc: load a vc tab entry from a slot
1513  */
1514
1515 STATIC void en_loadvc(sc, vc)
1516
1517 struct en_softc *sc;
1518 int vc;
1519
1520 {
1521   int slot;
1522   u_int32_t reg = EN_READ(sc, MID_VC(vc));
1523   
1524   reg = MIDV_SETMODE(reg, MIDV_TRASH);
1525   EN_WRITE(sc, MID_VC(vc), reg);
1526   DELAY(27);
1527
1528   if ((slot = sc->rxvc2slot[vc]) == RX_NONE)
1529     return;
1530
1531   /* no need to set CRC */
1532   EN_WRITE(sc, MID_DST_RP(vc), 0);      /* read pointer = 0, desc. start = 0 */
1533   EN_WRITE(sc, MID_WP_ST_CNT(vc), 0);   /* write pointer = 0 */
1534   EN_WRITE(sc, MID_VC(vc), sc->rxslot[slot].mode);  /* set mode, size, loc */
1535   sc->rxslot[slot].cur = sc->rxslot[slot].start;
1536
1537 #ifdef EN_DEBUG
1538     printf("%s: rx%d: assigned to VCI %d\n", sc->sc_dev.dv_xname, slot, vc);
1539 #endif
1540 }
1541
1542
1543 /*
1544  * en_start: start transmitting the next packet that needs to go out
1545  * if there is one.    note that atm_output() has already splimp()'d us.
1546  */
1547
1548 STATIC void en_start(ifp)
1549
1550 struct ifnet *ifp;
1551
1552 {
1553     struct en_softc *sc = (struct en_softc *) ifp->if_softc;
1554     struct ifqueue *ifq = &ifp->if_snd; /* if INPUT QUEUE */
1555     struct mbuf *m, *lastm, *prev;
1556     struct atm_pseudohdr *ap, *new_ap;
1557     int txchan, mlen, got, need, toadd, cellcnt, first;
1558     u_int32_t atm_vpi, atm_vci, atm_flags, *dat, aal;
1559     u_int8_t *cp;
1560
1561     if ((ifp->if_flags & IFF_RUNNING) == 0)
1562         return;
1563
1564     /*
1565      * remove everything from interface queue since we handle all queueing
1566      * locally ... 
1567      */
1568
1569     while (1) {
1570
1571       IF_DEQUEUE(ifq, m);
1572       if (m == NULL)
1573         return;         /* EMPTY: >>> exit here <<< */
1574     
1575       /*
1576        * calculate size of packet (in bytes)
1577        * also, if we are not doing transmit DMA we eliminate all stupid
1578        * (non-word) alignments here using en_mfix().   calls to en_mfix()
1579        * seem to be due to tcp retransmits for the most part.
1580        *
1581        * after this loop mlen total length of mbuf chain (including atm_ph),
1582        * and lastm is a pointer to the last mbuf on the chain.
1583        */
1584
1585       lastm = m;
1586       mlen = 0;
1587       prev = NULL;
1588       while (1) {
1589         /* no DMA? */
1590         if ((!sc->is_adaptec && EN_ENIDMAFIX) || EN_NOTXDMA || !en_dma) {
1591           if ( ((uintptr_t)mtod(lastm, void *) % sizeof(u_int32_t)) != 0 ||
1592             ((lastm->m_len % sizeof(u_int32_t)) != 0 && lastm->m_next)) {
1593             first = (lastm == m);
1594             if (en_mfix(sc, &lastm, prev) == 0) {       /* failed? */
1595               m_freem(m);
1596               m = NULL;
1597               break;
1598             }
1599             if (first)
1600               m = lastm;                /* update */
1601           }
1602           prev = lastm;
1603         }
1604
1605         mlen += lastm->m_len;
1606         if (lastm->m_next == NULL)
1607           break;
1608         lastm = lastm->m_next;
1609       }
1610
1611       if (m == NULL)            /* happens only if mfix fails */
1612         continue;
1613
1614       ap = mtod(m, struct atm_pseudohdr *);
1615
1616       atm_vpi = ATM_PH_VPI(ap);
1617       atm_vci = ATM_PH_VCI(ap);
1618       atm_flags = ATM_PH_FLAGS(ap) & ~(EN_OBHDR|EN_OBTRL);
1619       aal = ((atm_flags & ATM_PH_AAL5) != 0) 
1620                         ? MID_TBD_AAL5 : MID_TBD_NOAAL5;
1621
1622       /*
1623        * check that vpi/vci is one we can use
1624        */
1625
1626       if (atm_vpi || atm_vci > MID_N_VC) {
1627         printf("%s: output vpi=%d, vci=%d out of card range, dropping...\n", 
1628                 sc->sc_dev.dv_xname, atm_vpi, atm_vci);
1629         m_freem(m);
1630         continue;
1631       }
1632
1633       /*
1634        * computing how much padding we need on the end of the mbuf, then
1635        * see if we can put the TBD at the front of the mbuf where the
1636        * link header goes (well behaved protocols will reserve room for us).
1637        * last, check if room for PDU tail.
1638        *
1639        * got = number of bytes of data we have
1640        * cellcnt = number of cells in this mbuf
1641        * need = number of bytes of data + padding we need (excludes TBD)
1642        * toadd = number of bytes of data we need to add to end of mbuf,
1643        *        [including AAL5 PDU, if AAL5]
1644        */
1645
1646       got = mlen - sizeof(struct atm_pseudohdr *);
1647       toadd = (aal == MID_TBD_AAL5) ? MID_PDU_SIZE : 0; /* PDU */
1648       cellcnt = (got + toadd + (MID_ATMDATASZ - 1)) / MID_ATMDATASZ;
1649       need = cellcnt * MID_ATMDATASZ;
1650       toadd = need - got;               /* recompute, including zero padding */
1651
1652 #ifdef EN_DEBUG
1653       printf("%s: txvci%d: mlen=%d, got=%d, need=%d, toadd=%d, cell#=%d\n",
1654         sc->sc_dev.dv_xname, atm_vci, mlen, got, need, toadd, cellcnt);
1655       printf("     leading_space=%d, trailing_space=%d\n", 
1656         M_LEADINGSPACE(m), M_TRAILINGSPACE(lastm));
1657 #endif
1658
1659 #ifdef EN_MBUF_OPT
1660
1661       /*
1662        * note: external storage (M_EXT) can be shared between mbufs
1663        * to avoid copying (see m_copym()).    this means that the same
1664        * data buffer could be shared by several mbufs, and thus it isn't
1665        * a good idea to try and write TBDs or PDUs to M_EXT data areas.
1666        */
1667
1668       if (M_LEADINGSPACE(m) >= MID_TBD_SIZE && (m->m_flags & M_EXT) == 0) {
1669         m->m_data -= MID_TBD_SIZE;
1670         m->m_len += MID_TBD_SIZE;
1671         mlen += MID_TBD_SIZE;
1672         new_ap = mtod(m, struct atm_pseudohdr *);
1673         *new_ap = *ap;                  /* move it back */
1674         ap = new_ap;
1675         dat = ((u_int32_t *) ap) + 1;
1676         /* make sure the TBD is in proper byte order */
1677         *dat++ = htonl(MID_TBD_MK1(aal, sc->txspeed[atm_vci], cellcnt));
1678         *dat = htonl(MID_TBD_MK2(atm_vci, 0, 0));
1679         atm_flags |= EN_OBHDR;
1680       }
1681
1682       if (toadd && (lastm->m_flags & M_EXT) == 0 && 
1683                                         M_TRAILINGSPACE(lastm) >= toadd) {
1684         cp = mtod(lastm, u_int8_t *) + lastm->m_len;
1685         lastm->m_len += toadd;
1686         mlen += toadd;
1687         if (aal == MID_TBD_AAL5) {
1688           bzero(cp, toadd - MID_PDU_SIZE);
1689           dat = (u_int32_t *)(cp + toadd - MID_PDU_SIZE);
1690           /* make sure the PDU is in proper byte order */
1691           *dat = htonl(MID_PDU_MK1(0, 0, got));
1692         } else {
1693           bzero(cp, toadd);
1694         }
1695         atm_flags |= EN_OBTRL;
1696       }
1697       ATM_PH_FLAGS(ap) = atm_flags;     /* update EN_OBHDR/EN_OBTRL bits */
1698 #endif  /* EN_MBUF_OPT */
1699
1700       /*
1701        * get assigned channel (will be zero unless txspeed[atm_vci] is set)
1702        */
1703
1704       txchan = sc->txvc2slot[atm_vci];
1705
1706       if (sc->txslot[txchan].mbsize > EN_TXHIWAT) {
1707         EN_COUNT(sc->txmbovr);
1708         m_freem(m);
1709 #ifdef EN_DEBUG
1710         printf("%s: tx%d: buffer space shortage\n", sc->sc_dev.dv_xname,
1711                 txchan);
1712 #endif
1713         continue;
1714       }
1715
1716       sc->txslot[txchan].mbsize += mlen;
1717
1718 #ifdef EN_DEBUG
1719       printf("%s: tx%d: VPI=%d, VCI=%d, FLAGS=0x%x, speed=0x%x\n",
1720         sc->sc_dev.dv_xname, txchan, atm_vpi, atm_vci, atm_flags, 
1721         sc->txspeed[atm_vci]);
1722       printf("     adjusted mlen=%d, mbsize=%d\n", mlen, 
1723                 sc->txslot[txchan].mbsize);
1724 #endif
1725
1726       IF_ENQUEUE(&sc->txslot[txchan].q, m);
1727
1728       en_txdma(sc, txchan);
1729
1730   }
1731   /*NOTREACHED*/
1732 }
1733
1734
1735 /*
1736  * en_mfix: fix a stupid mbuf
1737  */
1738
1739 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
1740
1741 STATIC int en_mfix(sc, mm, prev)
1742
1743 struct en_softc *sc;
1744 struct mbuf **mm, *prev;
1745
1746 {
1747   struct mbuf *m, *new;
1748   u_char *d, *cp;
1749   int off;
1750   struct mbuf *nxt;
1751
1752   m = *mm;
1753
1754   EN_COUNT(sc->mfix);                   /* count # of calls */
1755 #ifdef EN_DEBUG
1756   printf("%s: mfix mbuf m_data=%p, m_len=%d\n", sc->sc_dev.dv_xname,
1757         m->m_data, m->m_len);
1758 #endif
1759
1760   d = mtod(m, u_char *);
1761   off = ((unsigned long) d) % sizeof(u_int32_t);
1762
1763   if (off) {
1764     if ((m->m_flags & M_EXT) == 0) {
1765       bcopy(d, d - off, m->m_len);   /* ALIGN! (with costly data copy...) */
1766       d -= off;
1767       m->m_data = (caddr_t)d;
1768     } else {
1769       /* can't write to an M_EXT mbuf since it may be shared */
1770       MGET(new, M_DONTWAIT, MT_DATA);
1771       if (!new) {
1772         EN_COUNT(sc->mfixfail);
1773         return(0);
1774       }
1775       MCLGET(new, M_DONTWAIT);
1776       if ((new->m_flags & M_EXT) == 0) {
1777         m_free(new);
1778         EN_COUNT(sc->mfixfail);
1779         return(0);
1780       }
1781       bcopy(d, new->m_data, m->m_len);  /* ALIGN! (with costly data copy...) */
1782       new->m_len = m->m_len;
1783       new->m_next = m->m_next;
1784       if (prev)
1785         prev->m_next = new;
1786       m_free(m);
1787       *mm = m = new;    /* note: 'd' now invalid */
1788     }
1789   }
1790
1791   off = m->m_len % sizeof(u_int32_t);
1792   if (off == 0)
1793     return(1);
1794
1795   d = mtod(m, u_char *) + m->m_len;
1796   off = sizeof(u_int32_t) - off;
1797   
1798   nxt = m->m_next;
1799   while (off--) {
1800     for ( ; nxt != NULL && nxt->m_len == 0 ; nxt = nxt->m_next)
1801       /*null*/;
1802     if (nxt == NULL) {          /* out of data, zero fill */
1803       *d++ = 0;
1804       continue;                 /* next "off" */
1805     }
1806     cp = mtod(nxt, u_char *);
1807     *d++ = *cp++;
1808     m->m_len++;
1809     nxt->m_len--; 
1810     nxt->m_data = (caddr_t)cp;
1811   }
1812   return(1);
1813 }
1814
1815 #else /* __FreeBSD__ */
1816
1817 STATIC int en_makeexclusive(struct en_softc *, struct mbuf **, struct mbuf *);
1818
1819 STATIC int en_makeexclusive(sc, mm, prev)
1820     struct en_softc *sc;
1821     struct mbuf **mm, *prev;
1822 {
1823     struct mbuf *m, *new;
1824
1825     m = *mm;
1826     
1827     if (m->m_flags & M_EXT) {
1828         if (m->m_ext.ext_free) {
1829             /* external buffer isn't an ordinary mbuf cluster! */
1830             printf("%s: mfix: special buffer! can't make a copy!\n",
1831                    sc->sc_dev.dv_xname);
1832             return (0);
1833         }
1834         
1835         if (mclrefcnt[mtocl(m->m_ext.ext_buf)] > 1) {
1836             /* make a real copy of the M_EXT mbuf since it is shared */
1837             MGET(new, M_DONTWAIT, MT_DATA);
1838             if (!new) {
1839                 EN_COUNT(sc->mfixfail);
1840                 return(0);
1841             }
1842             if (m->m_flags & M_PKTHDR)
1843                 M_MOVE_PKTHDR(new, m);
1844             MCLGET(new, M_DONTWAIT);
1845             if ((new->m_flags & M_EXT) == 0) {
1846                 m_free(new);
1847                 EN_COUNT(sc->mfixfail);
1848                 return(0);
1849             }
1850             bcopy(m->m_data, new->m_data, m->m_len);    
1851             new->m_len = m->m_len;
1852             new->m_next = m->m_next;
1853             if (prev)
1854                 prev->m_next = new;
1855             m_free(m);
1856             *mm = new;
1857         }
1858         else {
1859             /* the buffer is not shared, align the data offset using
1860                this buffer. */
1861             u_char *d = mtod(m, u_char *);
1862             int off = ((uintptr_t)(void *)d) % sizeof(u_int32_t);
1863
1864             if (off > 0) {
1865                 bcopy(d, d - off, m->m_len);
1866                 m->m_data = (caddr_t)d - off;
1867             }
1868         }
1869     }
1870     return (1);
1871 }
1872
1873 STATIC int en_mfix(sc, mm, prev)
1874
1875 struct en_softc *sc;
1876 struct mbuf **mm, *prev;
1877
1878 {
1879   struct mbuf *m;
1880   u_char *d, *cp;
1881   int off;
1882   struct mbuf *nxt;
1883
1884   m = *mm;
1885
1886   EN_COUNT(sc->mfix);                   /* count # of calls */
1887 #ifdef EN_DEBUG
1888   printf("%s: mfix mbuf m_data=0x%x, m_len=%d\n", sc->sc_dev.dv_xname,
1889         m->m_data, m->m_len);
1890 #endif
1891
1892   d = mtod(m, u_char *);
1893   off = ((uintptr_t) (void *) d) % sizeof(u_int32_t);
1894
1895   if (off) {
1896     if ((m->m_flags & M_EXT) == 0) {
1897       bcopy(d, d - off, m->m_len);   /* ALIGN! (with costly data copy...) */
1898       d -= off;
1899       m->m_data = (caddr_t)d;
1900     } else {
1901       /* can't write to an M_EXT mbuf since it may be shared */
1902       if (en_makeexclusive(sc, &m, prev) == 0)
1903           return (0);
1904       *mm = m;  /* note: 'd' now invalid */
1905     }
1906   }
1907
1908   off = m->m_len % sizeof(u_int32_t);
1909   if (off == 0)
1910     return(1);
1911
1912   if (m->m_flags & M_EXT) {
1913       /* can't write to an M_EXT mbuf since it may be shared */
1914       if (en_makeexclusive(sc, &m, prev) == 0)
1915           return (0);
1916       *mm = m;  /* note: 'd' now invalid */
1917   }
1918
1919   d = mtod(m, u_char *) + m->m_len;
1920   off = sizeof(u_int32_t) - off;
1921   
1922   nxt = m->m_next;
1923   while (off--) {
1924     if (nxt != NULL && nxt->m_len == 0) {
1925         /* remove an empty mbuf.  this avoids odd byte padding to an empty
1926            last mbuf.  */
1927         m->m_next = nxt = m_free(nxt);
1928     }
1929     if (nxt == NULL) {          /* out of data, zero fill */
1930       *d++ = 0;
1931       continue;                 /* next "off" */
1932     }
1933     cp = mtod(nxt, u_char *);
1934     *d++ = *cp++;
1935     m->m_len++;
1936     nxt->m_len--; 
1937     nxt->m_data = (caddr_t)cp;
1938   }
1939   if (nxt != NULL && nxt->m_len == 0)
1940       m->m_next = m_free(nxt);
1941   return(1);
1942 }
1943
1944 #endif /* __FreeBSD__ */
1945
1946 /*
1947  * en_txdma: start trasmit DMA, if possible
1948  */
1949
1950 STATIC void en_txdma(sc, chan)
1951
1952 struct en_softc *sc;
1953 int chan;
1954
1955 {
1956   struct mbuf *tmp;
1957   struct atm_pseudohdr *ap;
1958   struct en_launch launch;
1959   int datalen = 0, dtqneed, len, ncells;
1960   u_int8_t *cp;
1961   struct ifnet *ifp;
1962
1963 #ifdef EN_DEBUG
1964   printf("%s: tx%d: starting...\n", sc->sc_dev.dv_xname, chan);
1965 #endif
1966
1967   /*
1968    * note: now that txlaunch handles non-word aligned/sized requests
1969    * the only time you can safely set launch.nodma is if you've en_mfix()'d
1970    * the mbuf chain.    this happens only if EN_NOTXDMA || !en_dma.
1971    */
1972
1973   launch.nodma = (EN_NOTXDMA || !en_dma);
1974
1975 again:
1976
1977   /*
1978    * get an mbuf waiting for DMA
1979    */
1980
1981   launch.t = sc->txslot[chan].q.ifq_head; /* peek at head of queue */
1982
1983   if (launch.t == NULL) {
1984 #ifdef EN_DEBUG
1985     printf("%s: tx%d: ...done!\n", sc->sc_dev.dv_xname, chan);
1986 #endif
1987     return;     /* >>> exit here if no data waiting for DMA <<< */
1988   }
1989
1990   /*
1991    * get flags, vci
1992    * 
1993    * note: launch.need = # bytes we need to get on the card
1994    *       dtqneed = # of DTQs we need for this packet
1995    *       launch.mlen = # of bytes in in mbuf chain (<= launch.need)
1996    */
1997
1998   ap = mtod(launch.t, struct atm_pseudohdr *);
1999   launch.atm_vci = ATM_PH_VCI(ap);
2000   launch.atm_flags = ATM_PH_FLAGS(ap);
2001   launch.aal = ((launch.atm_flags & ATM_PH_AAL5) != 0) ? 
2002                 MID_TBD_AAL5 : MID_TBD_NOAAL5;
2003
2004   /*
2005    * XXX: have to recompute the length again, even though we already did
2006    * it in en_start().   might as well compute dtqneed here as well, so 
2007    * this isn't that bad.
2008    */
2009
2010   if ((launch.atm_flags & EN_OBHDR) == 0) {
2011     dtqneed = 1;                /* header still needs to be added */
2012     launch.need = MID_TBD_SIZE; /* not includeded with mbuf */
2013   } else {
2014     dtqneed = 0;                /* header on-board, dma with mbuf */
2015     launch.need = 0;
2016   }
2017
2018   launch.mlen = 0;
2019   for (tmp = launch.t ; tmp != NULL ; tmp = tmp->m_next) {
2020     len = tmp->m_len;
2021     launch.mlen += len;
2022     cp = mtod(tmp, u_int8_t *);
2023     if (tmp == launch.t) {
2024       len -= sizeof(struct atm_pseudohdr); /* don't count this! */
2025       cp += sizeof(struct atm_pseudohdr);
2026     }
2027     launch.need += len;
2028     if (len == 0)
2029       continue;                 /* atm_pseudohdr alone in first mbuf */
2030
2031     dtqneed += en_dqneed(sc, (caddr_t) cp, len, 1);
2032   }
2033
2034   if ((launch.need % sizeof(u_int32_t)) != 0) 
2035     dtqneed++;                  /* need DTQ to FLUSH internal buffer */
2036
2037   if ((launch.atm_flags & EN_OBTRL) == 0) {
2038     if (launch.aal == MID_TBD_AAL5) {
2039       datalen = launch.need - MID_TBD_SIZE;
2040       launch.need += MID_PDU_SIZE;              /* AAL5: need PDU tail */
2041     }
2042     dtqneed++;                  /* need to work on the end a bit */
2043   }
2044
2045   /*
2046    * finish calculation of launch.need (need to figure out how much padding
2047    * we will need).   launch.need includes MID_TBD_SIZE, but we need to
2048    * remove that to so we can round off properly.     we have to add 
2049    * MID_TBD_SIZE back in after calculating ncells.
2050    */
2051
2052   launch.need = roundup(launch.need - MID_TBD_SIZE, MID_ATMDATASZ);
2053   ncells = launch.need / MID_ATMDATASZ;
2054   launch.need += MID_TBD_SIZE;
2055
2056   if (launch.need > EN_TXSZ * 1024) {
2057     printf("%s: tx%d: packet larger than xmit buffer (%d > %d)\n",
2058       sc->sc_dev.dv_xname, chan, launch.need, EN_TXSZ * 1024);
2059     goto dequeue_drop;
2060   }
2061
2062   /*
2063    * note: don't use the entire buffer space.  if WRTX becomes equal
2064    * to RDTX, the transmitter stops assuming the buffer is empty!  --kjc
2065    */
2066   if (launch.need >= sc->txslot[chan].bfree) {
2067     EN_COUNT(sc->txoutspace);
2068 #ifdef EN_DEBUG
2069     printf("%s: tx%d: out of transmit space\n", sc->sc_dev.dv_xname, chan);
2070 #endif
2071     return;             /* >>> exit here if out of obmem buffer space <<< */
2072   }
2073   
2074   /*
2075    * ensure we have enough dtqs to go, if not, wait for more.
2076    */
2077
2078   if (launch.nodma) {
2079     dtqneed = 1;
2080   }
2081   if (dtqneed > sc->dtq_free) {
2082     sc->need_dtqs = 1;
2083     EN_COUNT(sc->txdtqout);
2084 #ifdef EN_DEBUG
2085     printf("%s: tx%d: out of transmit DTQs\n", sc->sc_dev.dv_xname, chan);
2086 #endif
2087     return;             /* >>> exit here if out of dtqs <<< */
2088   }
2089
2090   /*
2091    * it is a go, commit!  dequeue mbuf start working on the xfer.
2092    */
2093
2094   IF_DEQUEUE(&sc->txslot[chan].q, tmp);
2095 #ifdef EN_DIAG
2096   if (launch.t != tmp)
2097     panic("en dequeue");
2098 #endif /* EN_DIAG */
2099
2100   /*
2101    * launch!
2102    */
2103
2104   EN_COUNT(sc->launch);
2105   ifp = &sc->enif;
2106   ifp->if_opackets++;
2107   
2108   if ((launch.atm_flags & EN_OBHDR) == 0) {
2109     EN_COUNT(sc->lheader);
2110     /* store tbd1/tbd2 in host byte order */
2111     launch.tbd1 = MID_TBD_MK1(launch.aal, sc->txspeed[launch.atm_vci], ncells);
2112     launch.tbd2 = MID_TBD_MK2(launch.atm_vci, 0, 0);
2113   }
2114   if ((launch.atm_flags & EN_OBTRL) == 0 && launch.aal == MID_TBD_AAL5) {
2115     EN_COUNT(sc->ltail);
2116     launch.pdu1 = MID_PDU_MK1(0, 0, datalen);  /* host byte order */
2117   }
2118
2119   en_txlaunch(sc, chan, &launch);
2120
2121 #if NBPF > 0
2122   if (ifp->if_bpf) {
2123       /*
2124        * adjust the top of the mbuf to skip the pseudo atm header
2125        * (and TBD, if present) before passing the packet to bpf,
2126        * restore it afterwards.
2127        */
2128       int size = sizeof(struct atm_pseudohdr);
2129       if (launch.atm_flags & EN_OBHDR)
2130           size += MID_TBD_SIZE;
2131
2132       launch.t->m_data += size;
2133       launch.t->m_len -= size;
2134
2135       BPF_MTAP(ifp, launch.t);
2136
2137       launch.t->m_data -= size;
2138       launch.t->m_len += size;
2139   }
2140 #endif /* NBPF > 0 */
2141   /*
2142    * do some housekeeping and get the next packet
2143    */
2144
2145   sc->txslot[chan].bfree -= launch.need;
2146   IF_ENQUEUE(&sc->txslot[chan].indma, launch.t);
2147   goto again;
2148
2149   /*
2150    * END of txdma loop!
2151    */
2152
2153   /*
2154    * error handles
2155    */
2156
2157 dequeue_drop:
2158   IF_DEQUEUE(&sc->txslot[chan].q, tmp);
2159   if (launch.t != tmp)
2160     panic("en dequeue drop");
2161   m_freem(launch.t);
2162   sc->txslot[chan].mbsize -= launch.mlen;
2163   goto again;
2164 }
2165
2166
2167 /*
2168  * en_txlaunch: launch an mbuf into the dma pool!
2169  */
2170
2171 STATIC void en_txlaunch(sc, chan, l)
2172
2173 struct en_softc *sc;
2174 int chan;
2175 struct en_launch *l;
2176
2177 {
2178   struct mbuf *tmp;
2179   u_int32_t cur = sc->txslot[chan].cur,
2180             start = sc->txslot[chan].start,
2181             stop = sc->txslot[chan].stop,
2182             dma, *data, *datastop, count, bcode;
2183   int pad, addtail, need, len, needalign, cnt, end, mx;
2184
2185
2186  /*
2187   * vars:
2188   *   need = # bytes card still needs (decr. to zero)
2189   *   len = # of bytes left in current mbuf
2190   *   cur = our current pointer
2191   *   dma = last place we programmed into the DMA
2192   *   data = pointer into data area of mbuf that needs to go next
2193   *   cnt = # of bytes to transfer in this DTQ
2194   *   bcode/count = DMA burst code, and chip's version of cnt
2195   *
2196   *   a single buffer can require up to 5 DTQs depending on its size
2197   *   and alignment requirements.   the 5 possible requests are:
2198   *   [1] 1, 2, or 3 byte DMA to align src data pointer to word boundary
2199   *   [2] alburst DMA to align src data pointer to bestburstlen
2200   *   [3] 1 or more bestburstlen DMAs
2201   *   [4] clean up burst (to last word boundary)
2202   *   [5] 1, 2, or 3 byte final clean up DMA
2203   */
2204
2205  need = l->need;
2206  dma = cur;
2207  addtail = (l->atm_flags & EN_OBTRL) == 0;      /* add a tail? */
2208
2209 #ifdef EN_DIAG
2210   if ((need - MID_TBD_SIZE) % MID_ATMDATASZ) 
2211     printf("%s: tx%d: bogus trasmit needs (%d)\n", sc->sc_dev.dv_xname, chan,
2212                 need);
2213 #endif
2214 #ifdef EN_DEBUG
2215   printf("%s: tx%d: launch mbuf %p!   cur=0x%x[%d], need=%d, addtail=%d\n",
2216         sc->sc_dev.dv_xname, chan, l->t, cur, (cur-start)/4, need, addtail);
2217   count = EN_READ(sc, MIDX_PLACE(chan));
2218   printf("     HW: base_address=0x%x, size=%d, read=%d, descstart=%d\n",
2219         MIDX_BASE(count), MIDX_SZ(count), EN_READ(sc, MIDX_READPTR(chan)), 
2220         EN_READ(sc, MIDX_DESCSTART(chan)));
2221 #endif
2222
2223  /*
2224   * do we need to insert the TBD by hand?
2225   * note that tbd1/tbd2/pdu1 are in host byte order.
2226   */
2227
2228   if ((l->atm_flags & EN_OBHDR) == 0) {
2229 #ifdef EN_DEBUG
2230     printf("%s: tx%d: insert header 0x%x 0x%x\n", sc->sc_dev.dv_xname,
2231         chan, l->tbd1, l->tbd2);
2232 #endif
2233     EN_WRITE(sc, cur, l->tbd1);
2234     EN_WRAPADD(start, stop, cur, 4);
2235     EN_WRITE(sc, cur, l->tbd2);
2236     EN_WRAPADD(start, stop, cur, 4);
2237     need -= 8;
2238   }
2239
2240   /*
2241    * now do the mbufs...
2242    */
2243
2244   for (tmp = l->t ; tmp != NULL ; tmp = tmp->m_next) {
2245
2246     /* get pointer to data and length */
2247     data = mtod(tmp, u_int32_t *);
2248     len = tmp->m_len;
2249     if (tmp == l->t) {
2250       data += sizeof(struct atm_pseudohdr)/sizeof(u_int32_t);
2251       len -= sizeof(struct atm_pseudohdr);
2252     }
2253
2254     /* now, determine if we should copy it */
2255     if (l->nodma || (len < EN_MINDMA &&
2256        (len % 4) == 0 && ((uintptr_t) (void *) data % 4) == 0 &&
2257        (cur % 4) == 0)) {
2258
2259       /* 
2260        * roundup len: the only time this will change the value of len
2261        * is when l->nodma is true, tmp is the last mbuf, and there is
2262        * a non-word number of bytes to transmit.   in this case it is
2263        * safe to round up because we've en_mfix'd the mbuf (so the first
2264        * byte is word aligned there must be enough free bytes at the end
2265        * to round off to the next word boundary)...
2266        */
2267       len = roundup(len, sizeof(u_int32_t));
2268       datastop = data + (len / sizeof(u_int32_t));
2269       /* copy loop: preserve byte order!!!  use WRITEDAT */
2270       while (data != datastop) {
2271         EN_WRITEDAT(sc, cur, *data);
2272         data++;
2273         EN_WRAPADD(start, stop, cur, 4);
2274       }
2275       need -= len;
2276 #ifdef EN_DEBUG
2277       printf("%s: tx%d: copied %d bytes (%d left, cur now 0x%x)\n", 
2278                 sc->sc_dev.dv_xname, chan, len, need, cur);
2279 #endif
2280       continue;         /* continue on to next mbuf */
2281     }
2282
2283     /* going to do DMA, first make sure the dtq is in sync. */
2284     if (dma != cur) {
2285       EN_DTQADD(sc, WORD_IDX(start,cur), chan, MIDDMA_JK, 0, 0, 0);
2286 #ifdef EN_DEBUG
2287       printf("%s: tx%d: dtq_sync: advance pointer to %d\n",
2288                 sc->sc_dev.dv_xname, chan, cur);
2289 #endif
2290     }
2291
2292     /*
2293      * if this is the last buffer, and it looks like we are going to need to
2294      * flush the internal buffer, can we extend the length of this mbuf to
2295      * avoid the FLUSH?
2296      */
2297
2298     if (tmp->m_next == NULL) {
2299       cnt = (need - len) % sizeof(u_int32_t);
2300       if (cnt && M_TRAILINGSPACE(tmp) >= cnt)
2301         len += cnt;                     /* pad for FLUSH */
2302     }
2303       
2304 #if !defined(MIDWAY_ENIONLY)
2305
2306     /*
2307      * the adaptec DMA engine is smart and handles everything for us.
2308      */
2309
2310     if (sc->is_adaptec) {
2311       /* need to DMA "len" bytes out to card */
2312       need -= len;
2313       EN_WRAPADD(start, stop, cur, len);
2314 #ifdef EN_DEBUG
2315       printf("%s: tx%d: adp_dma %d bytes (%d left, cur now 0x%x)\n",
2316               sc->sc_dev.dv_xname, chan, len, need, cur);
2317 #endif
2318       end = (need == 0) ? MID_DMA_END : 0;
2319       EN_DTQADD(sc, len, chan, 0, vtophys(data), l->mlen, end);
2320       if (end)
2321         goto done;
2322       dma = cur;        /* update dma pointer */
2323       continue;
2324     }
2325 #endif /* !MIDWAY_ENIONLY */
2326
2327 #if !defined(MIDWAY_ADPONLY)
2328
2329     /*
2330      * the ENI DMA engine is not so smart and need more help from us
2331      */
2332
2333     /* do we need to do a DMA op to align to word boundary? */
2334     needalign = (uintptr_t) (void *) data % sizeof(u_int32_t);
2335     if (needalign) {
2336       EN_COUNT(sc->headbyte);
2337       cnt = sizeof(u_int32_t) - needalign;
2338       if (cnt == 2 && len >= cnt) {
2339         count = 1;
2340         bcode = MIDDMA_2BYTE;
2341       } else {
2342         cnt = min(cnt, len);            /* prevent overflow */
2343         count = cnt;
2344         bcode = MIDDMA_BYTE;
2345       }
2346       need -= cnt;
2347       EN_WRAPADD(start, stop, cur, cnt);
2348 #ifdef EN_DEBUG
2349       printf("%s: tx%d: small al_dma %d bytes (%d left, cur now 0x%x)\n",
2350               sc->sc_dev.dv_xname, chan, cnt, need, cur);
2351 #endif
2352       len -= cnt;
2353       end = (need == 0) ? MID_DMA_END : 0;
2354       EN_DTQADD(sc, count, chan, bcode, vtophys(data), l->mlen, end);
2355       if (end)
2356         goto done;
2357       data = (u_int32_t *) ((u_char *)data + cnt);
2358     }
2359
2360     /* do we need to do a DMA op to align? */
2361     if (sc->alburst && 
2362         (needalign = (((uintptr_t) (void *) data) & sc->bestburstmask)) != 0
2363         && len >= sizeof(u_int32_t)) {
2364       cnt = sc->bestburstlen - needalign;
2365       mx = len & ~(sizeof(u_int32_t)-1);        /* don't go past end */
2366       if (cnt > mx) {
2367         cnt = mx;
2368         count = cnt / sizeof(u_int32_t);
2369         bcode = MIDDMA_WORD;
2370       } else {
2371         count = cnt / sizeof(u_int32_t);
2372         bcode = en_dmaplan[count].bcode;
2373         count = cnt >> en_dmaplan[count].divshift;
2374       }
2375       need -= cnt;
2376       EN_WRAPADD(start, stop, cur, cnt);
2377 #ifdef EN_DEBUG
2378       printf("%s: tx%d: al_dma %d bytes (%d left, cur now 0x%x)\n", 
2379                 sc->sc_dev.dv_xname, chan, cnt, need, cur);
2380 #endif
2381       len -= cnt;
2382       end = (need == 0) ? MID_DMA_END : 0;
2383       EN_DTQADD(sc, count, chan, bcode, vtophys(data), l->mlen, end);
2384       if (end)
2385         goto done;
2386       data = (u_int32_t *) ((u_char *)data + cnt);
2387     }
2388
2389     /* do we need to do a max-sized burst? */
2390     if (len >= sc->bestburstlen) {
2391       count = len >> sc->bestburstshift;
2392       cnt = count << sc->bestburstshift;
2393       bcode = sc->bestburstcode;
2394       need -= cnt;
2395       EN_WRAPADD(start, stop, cur, cnt);
2396 #ifdef EN_DEBUG
2397       printf("%s: tx%d: best_dma %d bytes (%d left, cur now 0x%x)\n", 
2398                 sc->sc_dev.dv_xname, chan, cnt, need, cur);
2399 #endif
2400       len -= cnt;
2401       end = (need == 0) ? MID_DMA_END : 0;
2402       EN_DTQADD(sc, count, chan, bcode, vtophys(data), l->mlen, end);
2403       if (end)
2404         goto done;
2405       data = (u_int32_t *) ((u_char *)data + cnt);
2406     }
2407
2408     /* do we need to do a cleanup burst? */
2409     cnt = len & ~(sizeof(u_int32_t)-1);
2410     if (cnt) {
2411       count = cnt / sizeof(u_int32_t);
2412       bcode = en_dmaplan[count].bcode;
2413       count = cnt >> en_dmaplan[count].divshift;
2414       need -= cnt;
2415       EN_WRAPADD(start, stop, cur, cnt);
2416 #ifdef EN_DEBUG
2417       printf("%s: tx%d: cleanup_dma %d bytes (%d left, cur now 0x%x)\n", 
2418                 sc->sc_dev.dv_xname, chan, cnt, need, cur);
2419 #endif
2420       len -= cnt;
2421       end = (need == 0) ? MID_DMA_END : 0;
2422       EN_DTQADD(sc, count, chan, bcode, vtophys(data), l->mlen, end);
2423       if (end)
2424         goto done;
2425       data = (u_int32_t *) ((u_char *)data + cnt);
2426     }
2427
2428     /* any word fragments left? */
2429     if (len) {
2430       EN_COUNT(sc->tailbyte);
2431       if (len == 2) {
2432         count = 1;
2433         bcode = MIDDMA_2BYTE;                 /* use 2byte mode */
2434       } else {
2435         count = len;
2436         bcode = MIDDMA_BYTE;                  /* use 1 byte mode */
2437       }
2438       need -= len;
2439       EN_WRAPADD(start, stop, cur, len);
2440 #ifdef EN_DEBUG
2441       printf("%s: tx%d: byte cleanup_dma %d bytes (%d left, cur now 0x%x)\n",
2442               sc->sc_dev.dv_xname, chan, len, need, cur);
2443 #endif
2444       end = (need == 0) ? MID_DMA_END : 0;
2445       EN_DTQADD(sc, count, chan, bcode, vtophys(data), l->mlen, end);
2446       if (end)
2447         goto done;
2448     }
2449
2450     dma = cur;          /* update dma pointer */
2451 #endif /* !MIDWAY_ADPONLY */
2452
2453   } /* next mbuf, please */
2454
2455   /*
2456    * all mbuf data has been copied out to the obmem (or set up to be DMAd).
2457    * if the trailer or padding needs to be put in, do it now.  
2458    *
2459    * NOTE: experimental results reveal the following fact:
2460    *   if you DMA "X" bytes to the card, where X is not a multiple of 4,
2461    *   then the card will internally buffer the last (X % 4) bytes (in
2462    *   hopes of getting (4 - (X % 4)) more bytes to make a complete word).
2463    *   it is imporant to make sure we don't leave any important data in
2464    *   this internal buffer because it is discarded on the last (end) DTQ.
2465    *   one way to do this is to DMA in (4 - (X % 4)) more bytes to flush
2466    *   the darn thing out.
2467    */
2468
2469   if (addtail) {
2470
2471     pad = need % sizeof(u_int32_t);
2472     if (pad) {
2473       /*
2474        * FLUSH internal data buffer.  pad out with random data from the front
2475        * of the mbuf chain...
2476        */
2477       bcode = (sc->is_adaptec) ? 0 : MIDDMA_BYTE;
2478       EN_COUNT(sc->tailflush);
2479       EN_WRAPADD(start, stop, cur, pad);
2480       EN_DTQADD(sc, pad, chan, bcode, vtophys(l->t->m_data), 0, 0);
2481       need -= pad;
2482 #ifdef EN_DEBUG
2483       printf("%s: tx%d: pad/FLUSH dma %d bytes (%d left, cur now 0x%x)\n", 
2484                 sc->sc_dev.dv_xname, chan, pad, need, cur);
2485 #endif
2486     }
2487
2488     /* copy data */
2489     pad = need / sizeof(u_int32_t);     /* round *down* */
2490     if (l->aal == MID_TBD_AAL5)
2491       pad -= 2;
2492 #ifdef EN_DEBUG
2493       printf("%s: tx%d: padding %d bytes (cur now 0x%x)\n", 
2494                 sc->sc_dev.dv_xname, chan, pad * sizeof(u_int32_t), cur);
2495 #endif
2496     while (pad--) {
2497       EN_WRITEDAT(sc, cur, 0);  /* no byte order issues with zero */
2498       EN_WRAPADD(start, stop, cur, 4);
2499     }
2500     if (l->aal == MID_TBD_AAL5) {
2501       EN_WRITE(sc, cur, l->pdu1); /* in host byte order */
2502       EN_WRAPADD(start, stop, cur, 8);
2503     }
2504   }
2505
2506   if (addtail || dma != cur) {
2507    /* write final descritor  */
2508     EN_DTQADD(sc, WORD_IDX(start,cur), chan, MIDDMA_JK, 0, 
2509                                 l->mlen, MID_DMA_END);
2510     /* dma = cur; */    /* not necessary since we are done */
2511   }
2512
2513 done:
2514   /* update current pointer */
2515   sc->txslot[chan].cur = cur;
2516 #ifdef EN_DEBUG
2517       printf("%s: tx%d: DONE!   cur now = 0x%x\n", 
2518                 sc->sc_dev.dv_xname, chan, cur);
2519 #endif
2520
2521   return;
2522 }
2523
2524
2525 /*
2526  * interrupt handler
2527  */
2528
2529 EN_INTR_TYPE en_intr(arg)
2530
2531 void *arg;
2532
2533 {
2534   struct en_softc *sc = (struct en_softc *) arg;
2535   struct mbuf *m;
2536   struct atm_pseudohdr ah;
2537   struct ifnet *ifp;
2538   u_int32_t reg, kick, val, mask, chip, vci, slot, dtq, drq;
2539   int lcv, idx, need_softserv = 0;
2540
2541   reg = EN_READ(sc, MID_INTACK);
2542
2543   if ((reg & MID_INT_ANY) == 0) 
2544     EN_INTR_RET(0); /* not us */
2545
2546 #ifdef EN_DEBUG
2547   printf("%s: interrupt=0x%b\n", sc->sc_dev.dv_xname, reg, MID_INTBITS);
2548 #endif
2549
2550   /*
2551    * unexpected errors that need a reset
2552    */
2553
2554   if ((reg & (MID_INT_IDENT|MID_INT_LERR|MID_INT_DMA_ERR|MID_INT_SUNI)) != 0) {
2555     printf("%s: unexpected interrupt=0x%b, resetting card\n", 
2556         sc->sc_dev.dv_xname, reg, MID_INTBITS);
2557 #ifdef EN_DEBUG
2558 #ifdef DDB
2559 #if defined(__DragonFly__) || defined(__FreeBSD__)
2560     Debugger("en: unexpected error");
2561 #else
2562     Debugger();
2563 #endif
2564 #endif  /* DDB */
2565     sc->enif.if_flags &= ~IFF_RUNNING; /* FREEZE! */
2566 #else
2567     en_reset(sc);
2568     en_init(sc);
2569 #endif
2570     EN_INTR_RET(1); /* for us */
2571   }
2572
2573   /*******************
2574    * xmit interrupts *
2575    ******************/
2576
2577   kick = 0;                             /* bitmask of channels to kick */
2578   if (reg & MID_INT_TX) {               /* TX done! */
2579
2580     /*
2581      * check for tx complete, if detected then this means that some space
2582      * has come free on the card.   we must account for it and arrange to
2583      * kick the channel to life (in case it is stalled waiting on the card).
2584      */
2585     for (mask = 1, lcv = 0 ; lcv < EN_NTX ; lcv++, mask = mask * 2) {
2586       if (reg & MID_TXCHAN(lcv)) {
2587         kick = kick | mask;     /* want to kick later */
2588         val = EN_READ(sc, MIDX_READPTR(lcv));   /* current read pointer */
2589         val = (val * sizeof(u_int32_t)) + sc->txslot[lcv].start;
2590                                                 /* convert to offset */
2591         if (val > sc->txslot[lcv].cur)
2592           sc->txslot[lcv].bfree = val - sc->txslot[lcv].cur;
2593         else
2594           sc->txslot[lcv].bfree = (val + (EN_TXSZ*1024)) - sc->txslot[lcv].cur;
2595 #ifdef EN_DEBUG
2596         printf("%s: tx%d: trasmit done.   %d bytes now free in buffer\n",
2597                 sc->sc_dev.dv_xname, lcv, sc->txslot[lcv].bfree);
2598 #endif
2599       }
2600     }
2601   }
2602
2603   if (reg & MID_INT_DMA_TX) {           /* TX DMA done! */
2604
2605   /*
2606    * check for TX DMA complete, if detected then this means that some DTQs
2607    * are now free.   it also means some indma mbufs can be freed.
2608    * if we needed DTQs, kick all channels.
2609    */
2610     val = EN_READ(sc, MID_DMA_RDTX);    /* chip's current location */
2611     idx = MID_DTQ_A2REG(sc->dtq_chip);/* where we last saw chip */
2612     if (sc->need_dtqs) {
2613       kick = MID_NTX_CH - 1;            /* assume power of 2, kick all! */
2614       sc->need_dtqs = 0;                /* recalculated in "kick" loop below */
2615 #ifdef EN_DEBUG
2616       printf("%s: cleared need DTQ condition\n", sc->sc_dev.dv_xname);
2617 #endif
2618     }
2619     while (idx != val) {
2620       sc->dtq_free++;
2621       if ((dtq = sc->dtq[idx]) != 0) {
2622         sc->dtq[idx] = 0;       /* don't forget to zero it out when done */
2623         slot = EN_DQ_SLOT(dtq);
2624         IF_DEQUEUE(&sc->txslot[slot].indma, m);
2625         if (!m) panic("enintr: dtqsync");
2626         sc->txslot[slot].mbsize -= EN_DQ_LEN(dtq);
2627 #ifdef EN_DEBUG
2628         printf("%s: tx%d: free %d dma bytes, mbsize now %d\n",
2629                 sc->sc_dev.dv_xname, slot, EN_DQ_LEN(dtq), 
2630                 sc->txslot[slot].mbsize);
2631 #endif
2632         m_freem(m);
2633       }
2634       EN_WRAPADD(0, MID_DTQ_N, idx, 1);
2635     };
2636     sc->dtq_chip = MID_DTQ_REG2A(val);  /* sync softc */
2637   }
2638
2639
2640   /*
2641    * kick xmit channels as needed
2642    */
2643
2644   if (kick) {
2645 #ifdef EN_DEBUG
2646   printf("%s: tx kick mask = 0x%x\n", sc->sc_dev.dv_xname, kick);
2647 #endif
2648     for (mask = 1, lcv = 0 ; lcv < EN_NTX ; lcv++, mask = mask * 2) {
2649       if ((kick & mask) && sc->txslot[lcv].q.ifq_head) {
2650         en_txdma(sc, lcv);              /* kick it! */
2651       }
2652     }           /* for each slot */
2653   }             /* if kick */
2654
2655
2656   /*******************
2657    * recv interrupts *
2658    ******************/
2659
2660   /*
2661    * check for RX DMA complete, and pass the data "upstairs"
2662    */
2663
2664   if (reg & MID_INT_DMA_RX) {
2665     val = EN_READ(sc, MID_DMA_RDRX); /* chip's current location */
2666     idx = MID_DRQ_A2REG(sc->drq_chip);/* where we last saw chip */
2667     while (idx != val) {
2668       sc->drq_free++;
2669       if ((drq = sc->drq[idx]) != 0) {
2670         sc->drq[idx] = 0;       /* don't forget to zero it out when done */
2671         slot = EN_DQ_SLOT(drq);
2672         if (EN_DQ_LEN(drq) == 0) {  /* "JK" trash DMA? */
2673           m = NULL;
2674         } else {
2675           IF_DEQUEUE(&sc->rxslot[slot].indma, m);
2676           if (!m)
2677             panic("enintr: drqsync: %s: lost mbuf in slot %d!",
2678                   sc->sc_dev.dv_xname, slot);
2679         }
2680         /* do something with this mbuf */
2681         if (sc->rxslot[slot].oth_flags & ENOTHER_DRAIN) {  /* drain? */
2682           if (m)
2683             m_freem(m);
2684           vci = sc->rxslot[slot].atm_vci;
2685           if (sc->rxslot[slot].indma.ifq_head == NULL &&
2686                 sc->rxslot[slot].q.ifq_head == NULL &&
2687                 (EN_READ(sc, MID_VC(vci)) & MIDV_INSERVICE) == 0 &&
2688                 (sc->rxslot[slot].oth_flags & ENOTHER_SWSL) == 0) {
2689             sc->rxslot[slot].oth_flags = ENOTHER_FREE; /* done drain */
2690             sc->rxslot[slot].atm_vci = RX_NONE;
2691             sc->rxvc2slot[vci] = RX_NONE;
2692 #ifdef EN_DEBUG
2693             printf("%s: rx%d: VCI %d now free\n", sc->sc_dev.dv_xname,
2694                         slot, vci);
2695 #endif
2696           }
2697         } else if (m != NULL) {
2698           ATM_PH_FLAGS(&ah) = sc->rxslot[slot].atm_flags;
2699           ATM_PH_VPI(&ah) = 0;
2700           ATM_PH_SETVCI(&ah, sc->rxslot[slot].atm_vci);
2701 #ifdef EN_DEBUG
2702           printf("%s: rx%d: rxvci%d: atm_input, mbuf %p, len %d, hand %p\n",
2703                 sc->sc_dev.dv_xname, slot, sc->rxslot[slot].atm_vci, m,
2704                 EN_DQ_LEN(drq), sc->rxslot[slot].rxhand);
2705 #endif
2706
2707           ifp = &sc->enif;
2708           ifp->if_ipackets++;
2709
2710 #if NBPF > 0
2711           if (ifp->if_bpf)
2712             BPF_MTAP(ifp, m);
2713 #endif
2714
2715           atm_input(ifp, &ah, m, sc->rxslot[slot].rxhand);
2716         }
2717
2718       }
2719       EN_WRAPADD(0, MID_DRQ_N, idx, 1);
2720     };
2721     sc->drq_chip = MID_DRQ_REG2A(val);  /* sync softc */
2722
2723     if (sc->need_drqs) {        /* true if we had a DRQ shortage */
2724       need_softserv = 1;
2725       sc->need_drqs = 0;
2726 #ifdef EN_DEBUG
2727         printf("%s: cleared need DRQ condition\n", sc->sc_dev.dv_xname);
2728 #endif
2729     }
2730   }
2731
2732   /*
2733    * handle service interrupts
2734    */
2735
2736   if (reg & MID_INT_SERVICE) {
2737     chip = MID_SL_REG2A(EN_READ(sc, MID_SERV_WRITE));
2738
2739     while (sc->hwslistp != chip) {
2740
2741       /* fetch and remove it from hardware service list */
2742       vci = EN_READ(sc, sc->hwslistp);
2743       EN_WRAPADD(MID_SLOFF, MID_SLEND, sc->hwslistp, 4);/* advance hw ptr */
2744       slot = sc->rxvc2slot[vci];
2745       if (slot == RX_NONE) {
2746 #ifdef EN_DEBUG
2747         printf("%s: unexpected rx interrupt on VCI %d\n", 
2748                 sc->sc_dev.dv_xname, vci);
2749 #endif
2750         EN_WRITE(sc, MID_VC(vci), MIDV_TRASH);  /* rx off, damn it! */
2751         continue;                               /* next */
2752       }
2753       EN_WRITE(sc, MID_VC(vci), sc->rxslot[slot].mode); /* remove from hwsl */
2754       EN_COUNT(sc->hwpull);
2755
2756 #ifdef EN_DEBUG
2757       printf("%s: pulled VCI %d off hwslist\n", sc->sc_dev.dv_xname, vci);
2758 #endif
2759
2760       /* add it to the software service list (if needed) */
2761       if ((sc->rxslot[slot].oth_flags & ENOTHER_SWSL) == 0) {
2762         EN_COUNT(sc->swadd);
2763         need_softserv = 1;
2764         sc->rxslot[slot].oth_flags |= ENOTHER_SWSL;
2765         sc->swslist[sc->swsl_tail] = slot;
2766         EN_WRAPADD(0, MID_SL_N, sc->swsl_tail, 1);
2767         sc->swsl_size++;
2768 #ifdef EN_DEBUG
2769       printf("%s: added VCI %d to swslist\n", sc->sc_dev.dv_xname, vci);
2770 #endif
2771       }
2772     };
2773   }
2774
2775   /*
2776    * now service (function too big to include here)
2777    */
2778
2779   if (need_softserv)
2780     en_service(sc);
2781
2782   /*
2783    * keep our stats
2784    */
2785
2786   if (reg & MID_INT_DMA_OVR) {
2787     EN_COUNT(sc->dmaovr);
2788 #ifdef EN_DEBUG
2789     printf("%s: MID_INT_DMA_OVR\n", sc->sc_dev.dv_xname);
2790 #endif
2791   }
2792   reg = EN_READ(sc, MID_STAT);
2793 #ifdef EN_STAT
2794   sc->otrash += MID_OTRASH(reg);
2795   sc->vtrash += MID_VTRASH(reg);
2796 #endif
2797
2798   EN_INTR_RET(1); /* for us */
2799 }
2800
2801
2802 /*
2803  * en_service: handle a service interrupt
2804  *
2805  * Q: why do we need a software service list?
2806  *
2807  * A: if we remove a VCI from the hardware list and we find that we are
2808  *    out of DRQs we must defer processing until some DRQs become free.
2809  *    so we must remember to look at this RX VCI/slot later, but we can't
2810  *    put it back on the hardware service list (since that isn't allowed).
2811  *    so we instead save it on the software service list.   it would be nice 
2812  *    if we could peek at the VCI on top of the hwservice list without removing
2813  *    it, however this leads to a race condition: if we peek at it and
2814  *    decide we are done with it new data could come in before we have a 
2815  *    chance to remove it from the hwslist.   by the time we get it out of
2816  *    the list the interrupt for the new data will be lost.   oops!
2817  *
2818  */
2819
2820 STATIC void en_service(sc)
2821
2822 struct en_softc *sc;
2823
2824 {
2825   struct mbuf *m, *tmp;
2826   u_int32_t cur, dstart, rbd, pdu, *sav, dma, bcode, count, *data, *datastop;
2827   u_int32_t start, stop, cnt, needalign;
2828   int slot, raw, aal5, llc, vci, fill, mlen, tlen, drqneed, need, needfill, end;
2829
2830   aal5 = 0;             /* Silence gcc */
2831 next_vci:
2832   if (sc->swsl_size == 0) {
2833 #ifdef EN_DEBUG
2834     printf("%s: en_service done\n", sc->sc_dev.dv_xname);
2835 #endif
2836     return;             /* >>> exit here if swsl now empty <<< */
2837   }
2838
2839   /*
2840    * get slot/vci to service
2841    */
2842
2843   slot = sc->swslist[sc->swsl_head];
2844   vci = sc->rxslot[slot].atm_vci;
2845 #ifdef EN_DIAG
2846   if (sc->rxvc2slot[vci] != slot) panic("en_service rx slot/vci sync");
2847 #endif
2848
2849   /*
2850    * determine our mode and if we've got any work to do
2851    */
2852
2853   raw = sc->rxslot[slot].oth_flags & ENOTHER_RAW;
2854   start= sc->rxslot[slot].start;
2855   stop= sc->rxslot[slot].stop;
2856   cur = sc->rxslot[slot].cur;
2857
2858 #ifdef EN_DEBUG
2859   printf("%s: rx%d: service vci=%d raw=%d start/stop/cur=0x%x 0x%x 0x%x\n",
2860         sc->sc_dev.dv_xname, slot, vci, raw, start, stop, cur);
2861 #endif
2862
2863 same_vci:
2864   dstart = MIDV_DSTART(EN_READ(sc, MID_DST_RP(vci)));
2865   dstart = (dstart * sizeof(u_int32_t)) + start;
2866
2867   /* check to see if there is any data at all */
2868   if (dstart == cur) {
2869 defer:                                  /* defer processing */
2870     EN_WRAPADD(0, MID_SL_N, sc->swsl_head, 1); 
2871     sc->rxslot[slot].oth_flags &= ~ENOTHER_SWSL;
2872     sc->swsl_size--;
2873                                         /* >>> remove from swslist <<< */
2874 #ifdef EN_DEBUG
2875     printf("%s: rx%d: remove vci %d from swslist\n", 
2876                 sc->sc_dev.dv_xname, slot, vci);
2877 #endif
2878     goto next_vci;
2879   }
2880
2881   /*
2882    * figure out how many bytes we need
2883    * [mlen = # bytes to go in mbufs, fill = # bytes to dump (MIDDMA_JK)]
2884    */
2885
2886   if (raw) {
2887
2888     /* raw mode (aka boodi mode) */
2889     fill = 0;
2890     if (dstart > cur)
2891       mlen = dstart - cur;
2892     else
2893       mlen = (dstart + (EN_RXSZ*1024)) - cur;
2894
2895     if (mlen < sc->rxslot[slot].raw_threshold)
2896       goto defer;               /* too little data to deal with */
2897
2898   } else {
2899
2900     /* normal mode */
2901     aal5 = (sc->rxslot[slot].atm_flags & ATM_PH_AAL5);
2902     llc = (aal5 && (sc->rxslot[slot].atm_flags & ATM_PH_LLCSNAP)) ? 1 : 0;
2903     rbd = EN_READ(sc, cur);
2904     if (MID_RBD_ID(rbd) != MID_RBD_STDID) 
2905       panic("en_service: id mismatch");
2906
2907     if (rbd & MID_RBD_T) {
2908       mlen = 0;                 /* we've got trash */
2909       fill = MID_RBD_SIZE;
2910       EN_COUNT(sc->ttrash);
2911 #ifdef EN_DEBUG
2912       printf("RX overflow lost %d cells!\n", MID_RBD_CNT(rbd));
2913 #endif
2914     } else if (!aal5) {
2915       mlen = MID_RBD_SIZE + MID_CHDR_SIZE + MID_ATMDATASZ; /* 1 cell (ick!) */
2916       fill = 0;
2917     } else {
2918       struct ifnet *ifp;
2919
2920       tlen = (MID_RBD_CNT(rbd) * MID_ATMDATASZ) + MID_RBD_SIZE;
2921       pdu = cur + tlen - MID_PDU_SIZE;
2922       if (pdu >= stop)
2923         pdu -= (EN_RXSZ*1024);
2924       pdu = EN_READ(sc, pdu);   /* get PDU in correct byte order */
2925       fill = tlen - MID_RBD_SIZE - MID_PDU_LEN(pdu);
2926       if (fill < 0 || (rbd & MID_RBD_CRCERR) != 0) {
2927         static int first = 1;
2928
2929         if (first) {
2930           printf("%s: %s, dropping frame\n", sc->sc_dev.dv_xname,
2931                  (rbd & MID_RBD_CRCERR) ?
2932                  "CRC error" : "invalid AAL5 PDU length");
2933           printf("%s: got %d cells (%d bytes), AAL5 len is %d bytes (pdu=0x%x)\n",
2934                  sc->sc_dev.dv_xname, MID_RBD_CNT(rbd),
2935                  tlen - MID_RBD_SIZE, MID_PDU_LEN(pdu), pdu);
2936 #ifndef EN_DEBUG
2937           printf("CRC error report disabled from now on!\n");
2938           first = 0;
2939 #endif
2940         }
2941         fill = tlen;
2942
2943         ifp = &sc->enif;
2944         ifp->if_ierrors++;
2945
2946       }
2947       mlen = tlen - fill;
2948     }
2949
2950   }
2951
2952   /*
2953    * now allocate mbufs for mlen bytes of data, if out of mbufs, trash all
2954    *
2955    * notes:
2956    *  1. it is possible that we've already allocated an mbuf for this pkt
2957    *     but ran out of DRQs, in which case we saved the allocated mbuf on
2958    *     "q".
2959    *  2. if we save an mbuf in "q" we store the "cur" (pointer) in the front 
2960    *     of the mbuf as an identity (that we can check later), and we also
2961    *     store drqneed (so we don't have to recompute it).
2962    *  3. after this block of code, if m is still NULL then we ran out of mbufs
2963    */
2964   
2965   m = sc->rxslot[slot].q.ifq_head;
2966   drqneed = 1;
2967   if (m) {
2968     sav = mtod(m, u_int32_t *);
2969     if (sav[0] != cur) {
2970 #ifdef EN_DEBUG
2971       printf("%s: rx%d: q'ed mbuf %p not ours\n", 
2972                 sc->sc_dev.dv_xname, slot, m);
2973 #endif
2974       m = NULL;                 /* wasn't ours */
2975       EN_COUNT(sc->rxqnotus);
2976     } else {
2977       EN_COUNT(sc->rxqus);
2978       IF_DEQUEUE(&sc->rxslot[slot].q, m);
2979       drqneed = sav[1];
2980 #ifdef EN_DEBUG
2981       printf("%s: rx%d: recovered q'ed mbuf %p (drqneed=%d)\n", 
2982         sc->sc_dev.dv_xname, slot, m, drqneed);
2983 #endif
2984     }
2985   }
2986
2987   if (mlen != 0 && m == NULL) {
2988     m = en_mget(sc, mlen, &drqneed);            /* allocate! */
2989     if (m == NULL) {
2990       fill += mlen;
2991       mlen = 0;
2992       EN_COUNT(sc->rxmbufout);
2993 #ifdef EN_DEBUG
2994       printf("%s: rx%d: out of mbufs\n", sc->sc_dev.dv_xname, slot);
2995 #endif
2996     }
2997 #ifdef EN_DEBUG
2998     printf("%s: rx%d: allocate mbuf %p, mlen=%d, drqneed=%d\n", 
2999         sc->sc_dev.dv_xname, slot, m, mlen, drqneed);
3000 #endif
3001   }
3002
3003 #ifdef EN_DEBUG
3004   printf("%s: rx%d: VCI %d, mbuf_chain %p, mlen %d, fill %d\n",
3005         sc->sc_dev.dv_xname, slot, vci, m, mlen, fill);
3006 #endif
3007
3008   /*
3009    * now check to see if we've got the DRQs needed.    if we are out of 
3010    * DRQs we must quit (saving our mbuf, if we've got one).
3011    */
3012
3013   needfill = (fill) ? 1 : 0;
3014   if (drqneed + needfill > sc->drq_free) {
3015     sc->need_drqs = 1;  /* flag condition */
3016     if (m == NULL) {
3017       EN_COUNT(sc->rxoutboth);
3018 #ifdef EN_DEBUG
3019       printf("%s: rx%d: out of DRQs *and* mbufs!\n", sc->sc_dev.dv_xname, slot);
3020 #endif
3021       return;           /* >>> exit here if out of both mbufs and DRQs <<< */
3022     }
3023     sav = mtod(m, u_int32_t *);
3024     sav[0] = cur;
3025     sav[1] = drqneed;
3026     IF_ENQUEUE(&sc->rxslot[slot].q, m);
3027     EN_COUNT(sc->rxdrqout);
3028 #ifdef EN_DEBUG
3029     printf("%s: rx%d: out of DRQs\n", sc->sc_dev.dv_xname, slot);
3030 #endif
3031     return;             /* >>> exit here if out of DRQs <<< */
3032   }
3033
3034   /*
3035    * at this point all resources have been allocated and we are commited 
3036    * to servicing this slot.
3037    *
3038    * dma = last location we told chip about
3039    * cur = current location
3040    * mlen = space in the mbuf we want
3041    * need = bytes to xfer in (decrs to zero)
3042    * fill = how much fill we need
3043    * tlen = how much data to transfer to this mbuf
3044    * cnt/bcode/count = <same as xmit>
3045    *
3046    * 'needfill' not used after this point
3047    */
3048
3049   dma = cur;            /* dma = last location we told chip about */
3050   need = roundup(mlen, sizeof(u_int32_t));
3051   fill = fill - (need - mlen);  /* note: may invalidate 'needfill' */
3052
3053   for (tmp = m ; tmp != NULL && need > 0 ; tmp = tmp->m_next) {
3054     tlen = roundup(tmp->m_len, sizeof(u_int32_t)); /* m_len set by en_mget */
3055     data = mtod(tmp, u_int32_t *);
3056
3057 #ifdef EN_DEBUG
3058     printf("%s: rx%d: load mbuf %p, m_len=%d, m_data=%p, tlen=%d\n",
3059         sc->sc_dev.dv_xname, slot, tmp, tmp->m_len, tmp->m_data, tlen);
3060 #endif
3061     
3062     /* copy data */
3063     if (EN_NORXDMA || !en_dma || tlen < EN_MINDMA) {
3064       datastop = (u_int32_t *)((u_char *) data + tlen);
3065       /* copy loop: preserve byte order!!!  use READDAT */
3066       while (data != datastop) {
3067         *data = EN_READDAT(sc, cur);
3068         data++;
3069         EN_WRAPADD(start, stop, cur, 4);
3070       }
3071       need -= tlen;
3072 #ifdef EN_DEBUG
3073       printf("%s: rx%d: vci%d: copied %d bytes (%d left)\n",
3074                 sc->sc_dev.dv_xname, slot, vci, tlen, need);
3075 #endif
3076       continue;
3077     }
3078
3079     /* DMA data (check to see if we need to sync DRQ first) */
3080     if (dma != cur) {
3081       EN_DRQADD(sc, WORD_IDX(start,cur), vci, MIDDMA_JK, 0, 0, 0, 0);
3082 #ifdef EN_DEBUG
3083       printf("%s: rx%d: vci%d: drq_sync: advance pointer to %d\n",
3084                 sc->sc_dev.dv_xname, slot, vci, cur);
3085 #endif
3086     }
3087
3088 #if !defined(MIDWAY_ENIONLY)
3089      
3090     /*
3091      * the adaptec DMA engine is smart and handles everything for us.
3092      */ 
3093   
3094     if (sc->is_adaptec) {
3095       need -= tlen;
3096       EN_WRAPADD(start, stop, cur, tlen);
3097 #ifdef EN_DEBUG
3098       printf("%s: rx%d: vci%d: adp_dma %d bytes (%d left)\n",
3099                 sc->sc_dev.dv_xname, slot, vci, tlen, need);
3100 #endif
3101       end = (need == 0 && !fill) ? MID_DMA_END : 0;
3102       EN_DRQADD(sc, tlen, vci, 0, vtophys(data), mlen, slot, end);
3103       if (end)
3104         goto done;
3105       dma = cur;        /* update dma pointer */
3106       continue;
3107     }
3108 #endif /* !MIDWAY_ENIONLY */
3109
3110
3111 #if !defined(MIDWAY_ADPONLY)
3112
3113     /*
3114      * the ENI DMA engine is not so smart and need more help from us
3115      */
3116
3117     /* do we need to do a DMA op to align? */
3118     if (sc->alburst &&
3119       (needalign = (((uintptr_t) (void *) data) & sc->bestburstmask)) != 0) {
3120       cnt = sc->bestburstlen - needalign;
3121       if (cnt > tlen) {
3122         cnt = tlen;
3123         count = cnt / sizeof(u_int32_t);
3124         bcode = MIDDMA_WORD;
3125       } else {
3126         count = cnt / sizeof(u_int32_t);
3127         bcode = en_dmaplan[count].bcode;
3128         count = cnt >> en_dmaplan[count].divshift;
3129       }
3130       need -= cnt;
3131       EN_WRAPADD(start, stop, cur, cnt);
3132 #ifdef EN_DEBUG
3133       printf("%s: rx%d: vci%d: al_dma %d bytes (%d left)\n",
3134                 sc->sc_dev.dv_xname, slot, vci, cnt, need);
3135 #endif
3136       tlen -= cnt;
3137       end = (need == 0 && !fill) ? MID_DMA_END : 0;
3138       EN_DRQADD(sc, count, vci, bcode, vtophys(data), mlen, slot, end);
3139       if (end)
3140         goto done;
3141       data = (u_int32_t *)((u_char *) data + cnt);   
3142     }
3143
3144     /* do we need a max-sized burst? */
3145     if (tlen >= sc->bestburstlen) {
3146       count = tlen >> sc->bestburstshift;
3147       cnt = count << sc->bestburstshift;
3148       bcode = sc->bestburstcode;
3149       need -= cnt;
3150       EN_WRAPADD(start, stop, cur, cnt);
3151 #ifdef EN_DEBUG
3152       printf("%s: rx%d: vci%d: best_dma %d bytes (%d left)\n",
3153                 sc->sc_dev.dv_xname, slot, vci, cnt, need);
3154 #endif
3155       tlen -= cnt;
3156       end = (need == 0 && !fill) ? MID_DMA_END : 0;
3157       EN_DRQADD(sc, count, vci, bcode, vtophys(data), mlen, slot, end);
3158       if (end)
3159         goto done;
3160       data = (u_int32_t *)((u_char *) data + cnt);   
3161     }
3162
3163     /* do we need to do a cleanup burst? */
3164     if (tlen) {
3165       count = tlen / sizeof(u_int32_t);
3166       bcode = en_dmaplan[count].bcode;
3167       count = tlen >> en_dmaplan[count].divshift;
3168       need -= tlen;
3169       EN_WRAPADD(start, stop, cur, tlen);
3170 #ifdef EN_DEBUG
3171       printf("%s: rx%d: vci%d: cleanup_dma %d bytes (%d left)\n",
3172                 sc->sc_dev.dv_xname, slot, vci, tlen, need);
3173 #endif
3174       end = (need == 0 && !fill) ? MID_DMA_END : 0;
3175       EN_DRQADD(sc, count, vci, bcode, vtophys(data), mlen, slot, end);
3176       if (end)
3177         goto done;
3178     }
3179
3180     dma = cur;          /* update dma pointer */
3181
3182 #endif /* !MIDWAY_ADPONLY */
3183
3184   }
3185
3186   /* skip the end */
3187   if (fill || dma != cur) {
3188 #ifdef EN_DEBUG
3189       if (fill)
3190         printf("%s: rx%d: vci%d: skipping %d bytes of fill\n",
3191                 sc->sc_dev.dv_xname, slot, vci, fill);
3192       else
3193         printf("%s: rx%d: vci%d: syncing chip from 0x%x to 0x%x [cur]\n",
3194                 sc->sc_dev.dv_xname, slot, vci, dma, cur);
3195 #endif
3196     EN_WRAPADD(start, stop, cur, fill);
3197     EN_DRQADD(sc, WORD_IDX(start,cur), vci, MIDDMA_JK, 0, mlen,
3198                                         slot, MID_DMA_END);
3199     /* dma = cur; */    /* not necessary since we are done */
3200   }
3201
3202   /*
3203    * done, remove stuff we don't want to pass up:
3204    *   raw mode (boodi mode): pass everything up for later processing
3205    *   aal5: remove RBD
3206    *   aal0: remove RBD + cell header
3207    */
3208
3209 done:
3210   if (m) {
3211     if (!raw) {
3212       cnt = MID_RBD_SIZE;
3213       if (!aal5) cnt += MID_CHDR_SIZE;
3214       m->m_len -= cnt;                          /* chop! */
3215       m->m_pkthdr.len -= cnt;
3216       m->m_data += cnt;
3217     }
3218     IF_ENQUEUE(&sc->rxslot[slot].indma, m);
3219   }
3220   sc->rxslot[slot].cur = cur;           /* update master copy of 'cur' */
3221
3222 #ifdef EN_DEBUG
3223   printf("%s: rx%d: vci%d: DONE!   cur now =0x%x\n", 
3224         sc->sc_dev.dv_xname, slot, vci, cur);
3225 #endif
3226
3227   goto same_vci;        /* get next packet in this slot */
3228 }
3229
3230
3231 #ifdef EN_DDBHOOK
3232 /*
3233  * functions we can call from ddb
3234  */
3235
3236 /*
3237  * en_dump: dump the state
3238  */
3239
3240 #define END_SWSL        0x00000040              /* swsl state */
3241 #define END_DRQ         0x00000020              /* drq state */
3242 #define END_DTQ         0x00000010              /* dtq state */
3243 #define END_RX          0x00000008              /* rx state */
3244 #define END_TX          0x00000004              /* tx state */
3245 #define END_MREGS       0x00000002              /* registers */
3246 #define END_STATS       0x00000001              /* dump stats */
3247
3248 #define END_BITS "\20\7SWSL\6DRQ\5DTQ\4RX\3TX\2MREGS\1STATS"
3249
3250 /* Do not staticize - meant for calling from DDB! */
3251 int en_dump(unit, level)
3252
3253 int unit, level;
3254
3255 {
3256   struct en_softc *sc;
3257   int lcv, cnt, slot;
3258   u_int32_t ptr, reg;
3259
3260   for (lcv = 0 ; lcv < en_cd.cd_ndevs ; lcv++) {
3261     sc = (struct en_softc *) en_cd.cd_devs[lcv];
3262     if (sc == NULL) continue;
3263     if (unit != -1 && unit != lcv)
3264       continue;
3265
3266     printf("dumping device %s at level 0x%b\n", sc->sc_dev.dv_xname, level,
3267                         END_BITS);
3268
3269     if (sc->dtq_us == 0) {
3270       printf("<hasn't been en_init'd yet>\n");
3271       continue;
3272     }
3273
3274     if (level & END_STATS) {
3275       printf("  en_stats:\n");
3276       printf("    %d mfix (%d failed); %d/%d head/tail byte DMAs, %d flushes\n",
3277            sc->mfix, sc->mfixfail, sc->headbyte, sc->tailbyte, sc->tailflush);
3278       printf("    %d rx dma overflow interrupts\n", sc->dmaovr);
3279       printf("    %d times we ran out of TX space and stalled\n", 
3280                                                         sc->txoutspace);
3281       printf("    %d times we ran out of DTQs\n", sc->txdtqout);
3282       printf("    %d times we launched a packet\n", sc->launch);
3283       printf("    %d times we launched without on-board header\n", sc->lheader);
3284       printf("    %d times we launched without on-board tail\n", sc->ltail);
3285       printf("    %d times we pulled the hw service list\n", sc->hwpull);
3286       printf("    %d times we pushed a vci on the sw service list\n", 
3287                                                                 sc->swadd);
3288       printf("    %d times RX pulled an mbuf from Q that wasn't ours\n", 
3289                                                          sc->rxqnotus);
3290       printf("    %d times RX pulled a good mbuf from Q\n", sc->rxqus);
3291       printf("    %d times we ran out of mbufs *and* DRQs\n", sc->rxoutboth);
3292       printf("    %d times we ran out of DRQs\n", sc->rxdrqout);
3293
3294       printf("    %d trasmit packets dropped due to mbsize\n", sc->txmbovr);
3295       printf("    %d cells trashed due to turned off rxvc\n", sc->vtrash);
3296       printf("    %d cells trashed due to totally full buffer\n", sc->otrash);
3297       printf("    %d cells trashed due almost full buffer\n", sc->ttrash);
3298       printf("    %d rx mbuf allocation failures\n", sc->rxmbufout);
3299 #if defined(NATM) && defined(NATM_STAT)
3300       printf("    natmintr so_rcv: ok/drop cnt: %d/%d, ok/drop bytes: %d/%d\n",
3301         natm_sookcnt, natm_sodropcnt, natm_sookbytes, natm_sodropbytes);
3302 #endif
3303     }
3304
3305     if (level & END_MREGS) {
3306       printf("mregs:\n");
3307       printf("resid = 0x%lx\n", (u_long)EN_READ(sc, MID_RESID));
3308       printf("interrupt status = 0x%b\n", 
3309                                 (int)EN_READ(sc, MID_INTSTAT), MID_INTBITS);
3310       printf("interrupt enable = 0x%b\n", 
3311                                 (int)EN_READ(sc, MID_INTENA), MID_INTBITS);
3312       printf("mcsr = 0x%b\n", (int)EN_READ(sc, MID_MAST_CSR), MID_MCSRBITS);
3313       printf("serv_write = [chip=%ld] [us=%d]\n",
3314                         (long)EN_READ(sc, MID_SERV_WRITE),
3315                         MID_SL_A2REG(sc->hwslistp));
3316       printf("dma addr = 0x%lx\n", (u_long)EN_READ(sc, MID_DMA_ADDR));
3317       printf("DRQ: chip[rd=0x%lx,wr=0x%lx], sc[chip=0x%x,us=0x%x]\n",
3318         (u_long)MID_DRQ_REG2A(EN_READ(sc, MID_DMA_RDRX)), 
3319         (u_long)MID_DRQ_REG2A(EN_READ(sc, MID_DMA_WRRX)),
3320         sc->drq_chip, sc->drq_us);
3321       printf("DTQ: chip[rd=0x%lx,wr=0x%lx], sc[chip=0x%x,us=0x%x]\n",
3322         (u_long)MID_DTQ_REG2A(EN_READ(sc, MID_DMA_RDTX)), 
3323         (u_long)MID_DTQ_REG2A(EN_READ(sc, MID_DMA_WRTX)),
3324         sc->dtq_chip, sc->dtq_us);
3325
3326       printf("  unusal txspeeds: ");
3327       for (cnt = 0 ; cnt < MID_N_VC ; cnt++)
3328         if (sc->txspeed[cnt])
3329           printf(" vci%d=0x%x", cnt, sc->txspeed[cnt]);
3330       printf("\n");
3331
3332       printf("  rxvc slot mappings: ");
3333       for (cnt = 0 ; cnt < MID_N_VC ; cnt++)
3334         if (sc->rxvc2slot[cnt] != RX_NONE)
3335           printf("  %d->%d", cnt, sc->rxvc2slot[cnt]);
3336       printf("\n");
3337
3338     }
3339
3340     if (level & END_TX) {
3341       printf("tx:\n");
3342       for (slot = 0 ; slot < EN_NTX; slot++) {
3343         printf("tx%d: start/stop/cur=0x%x/0x%x/0x%x [%d]  ", slot,
3344           sc->txslot[slot].start, sc->txslot[slot].stop, sc->txslot[slot].cur,
3345                 (sc->txslot[slot].cur - sc->txslot[slot].start)/4);
3346         printf("mbsize=%d, bfree=%d\n", sc->txslot[slot].mbsize,
3347                 sc->txslot[slot].bfree);
3348         printf("txhw: base_address=0x%lx, size=%ld, read=%ld, descstart=%ld\n",
3349           (u_long)MIDX_BASE(EN_READ(sc, MIDX_PLACE(slot))), 
3350           (u_long)MIDX_SZ(EN_READ(sc, MIDX_PLACE(slot))),
3351           (long)EN_READ(sc, MIDX_READPTR(slot)),
3352           (long)EN_READ(sc, MIDX_DESCSTART(slot)));
3353       }
3354     }
3355
3356     if (level & END_RX) {
3357       printf("  recv slots:\n");
3358       for (slot = 0 ; slot < sc->en_nrx; slot++) {
3359         printf("rx%d: vci=%d: start/stop/cur=0x%x/0x%x/0x%x ", slot,
3360           sc->rxslot[slot].atm_vci, sc->rxslot[slot].start, 
3361           sc->rxslot[slot].stop, sc->rxslot[slot].cur);
3362         printf("mode=0x%x, atm_flags=0x%x, oth_flags=0x%x\n", 
3363         sc->rxslot[slot].mode, sc->rxslot[slot].atm_flags, 
3364                 sc->rxslot[slot].oth_flags);
3365         printf("RXHW: mode=0x%lx, DST_RP=0x%lx, WP_ST_CNT=0x%lx\n",
3366           (u_long)EN_READ(sc, MID_VC(sc->rxslot[slot].atm_vci)),
3367           (u_long)EN_READ(sc, MID_DST_RP(sc->rxslot[slot].atm_vci)),
3368           (u_long)EN_READ(sc, MID_WP_ST_CNT(sc->rxslot[slot].atm_vci)));
3369       }
3370     }
3371
3372     if (level & END_DTQ) {
3373       printf("  dtq [need_dtqs=%d,dtq_free=%d]:\n", 
3374                                         sc->need_dtqs, sc->dtq_free);
3375       ptr = sc->dtq_chip;
3376       while (ptr != sc->dtq_us) {
3377         reg = EN_READ(sc, ptr);
3378         printf("\t0x%x=[cnt=%d, chan=%d, end=%d, type=%d @ 0x%lx]\n", 
3379             sc->dtq[MID_DTQ_A2REG(ptr)], MID_DMA_CNT(reg), MID_DMA_TXCHAN(reg),
3380             (reg & MID_DMA_END) != 0, MID_DMA_TYPE(reg),
3381             (u_long)EN_READ(sc, ptr+4));
3382         EN_WRAPADD(MID_DTQOFF, MID_DTQEND, ptr, 8);
3383       }
3384     }
3385
3386     if (level & END_DRQ) {
3387       printf("  drq [need_drqs=%d,drq_free=%d]:\n", 
3388                                         sc->need_drqs, sc->drq_free);
3389       ptr = sc->drq_chip;
3390       while (ptr != sc->drq_us) {
3391         reg = EN_READ(sc, ptr);
3392         printf("\t0x%x=[cnt=%d, chan=%d, end=%d, type=%d @ 0x%lx]\n", 
3393           sc->drq[MID_DRQ_A2REG(ptr)], MID_DMA_CNT(reg), MID_DMA_RXVCI(reg),
3394           (reg & MID_DMA_END) != 0, MID_DMA_TYPE(reg),
3395           (u_long)EN_READ(sc, ptr+4));
3396         EN_WRAPADD(MID_DRQOFF, MID_DRQEND, ptr, 8);
3397       }
3398     }
3399
3400     if (level & END_SWSL) {
3401       printf(" swslist [size=%d]: ", sc->swsl_size);
3402       for (cnt = sc->swsl_head ; cnt != sc->swsl_tail ; 
3403                         cnt = (cnt + 1) % MID_SL_N)
3404         printf("0x%x ", sc->swslist[cnt]);
3405       printf("\n");
3406     }
3407   }
3408   return(0);
3409 }
3410
3411 /*
3412  * en_dumpmem: dump the memory
3413  */
3414
3415 /* Do not staticize - meant for calling from DDB! */
3416 int en_dumpmem(unit, addr, len)
3417
3418 int unit, addr, len;
3419
3420 {
3421   struct en_softc *sc;
3422   u_int32_t reg;
3423
3424   if (unit < 0 || unit > en_cd.cd_ndevs ||
3425         (sc = (struct en_softc *) en_cd.cd_devs[unit]) == NULL) {
3426     printf("invalid unit number: %d\n", unit);
3427     return(0);
3428   }
3429   addr = addr & ~3;
3430   if (addr < MID_RAMOFF || addr + len*4 > MID_MAXOFF || len <= 0) {
3431     printf("invalid addr/len number: %d, %d\n", addr, len);
3432     return(0);
3433   }
3434   printf("dumping %d words starting at offset 0x%x\n", len, addr);
3435   while (len--) {
3436     reg = EN_READ(sc, addr);
3437     printf("mem[0x%x] = 0x%x\n", addr, reg);
3438     addr += 4;
3439   }
3440   return(0);
3441 }
3442 #endif