kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / netif / lnc / if_lnc.c
1 /*-
2  * Copyright (c) 1994-1998
3  *      Paul Richards.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    verbatim and that no modifications are made prior to this
11  *    point in the file.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Paul Richards.
18  * 4. The name Paul Richards may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY PAUL RICHARDS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL PAUL RICHARDS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/i386/isa/if_lnc.c,v 1.68.2.5 2002/02/13 00:43:10 dillon Exp $
34  * $DragonFly: src/sys/dev/netif/lnc/Attic/if_lnc.c,v 1.3 2003/08/07 21:17:03 dillon Exp $
35  */
36
37 /*
38 #define DIAGNOSTIC
39 #define DEBUG
40  *
41  * TODO ----
42  *
43  * This driver will need bounce buffer support when dma'ing to mbufs above the
44  * 16Mb mark.
45  *
46  * Check all the XXX comments -- some of them are just things I've left
47  * unfinished rather than "difficult" problems that were hacked around.
48  *
49  * Check log settings.
50  *
51  * Check how all the arpcom flags get set and used.
52  *
53  * Re-inline and re-static all routines after debugging.
54  *
55  * Remember to assign iobase in SHMEM probe routines.
56  *
57  * Replace all occurences of LANCE-controller-card etc in prints by the name
58  * strings of the appropriate type -- nifty window dressing
59  *
60  * Add DEPCA support -- mostly done.
61  *
62  */
63
64 #include "use_pci.h"
65 #include "use_lnc.h"
66
67 #include "opt_inet.h"
68
69 /* Some defines that should really be in generic locations */
70 #define FCS_LEN 4
71 #define MULTICAST_FILTER_LEN 8
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/conf.h>
76 #include <sys/sockio.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/socket.h>
80 #include <sys/syslog.h>
81
82 #include <net/ethernet.h>
83 #include <net/if.h>
84 #include <net/if_dl.h>
85 #include <net/if_types.h>
86
87 #include <netinet/in.h>
88 #include <netinet/if_ether.h>
89
90 #include <net/bpf.h>
91
92 #ifdef PC98
93 #include <machine/clock.h>
94 #endif
95 #include <machine/md_var.h>
96
97 #include <bus/isa/i386/isa_device.h>
98 #include "if_lnc.h"
99
100 struct lnc_softc {
101         struct arpcom arpcom;               /* see ../../net/if_arp.h */
102         struct nic_info nic;                /* NIC specific info */
103         int nrdre;
104         struct host_ring_entry *recv_ring;  /* start of alloc'd mem */
105         int recv_next;
106         int ntdre;
107         struct host_ring_entry *trans_ring;
108         int trans_next;
109         struct init_block *init_block;      /* Initialisation block */
110         int pending_transmits;        /* No. of transmit descriptors in use */
111         int next_to_send;
112         struct mbuf *mbufs;
113         int mbuf_count;
114         int flags;
115         int rap;
116         int rdp;
117         int bdp;
118 #ifdef DEBUG
119         int lnc_debug;
120 #endif
121         LNCSTATS_STRUCT
122 };
123
124 static struct lnc_softc lnc_softc[NLNC];
125
126 static char const * const nic_ident[] = {
127         "Unknown",
128         "BICC",
129         "NE2100",
130         "DEPCA",
131         "CNET98S",      /* PC-98 */
132 };
133
134 static char const * const ic_ident[] = {
135         "Unknown",
136         "LANCE",
137         "C-LANCE",
138         "PCnet-ISA",
139         "PCnet-ISA+",
140         "PCnet-ISA II",
141         "PCnet-32 VL-Bus",
142         "PCnet-PCI",
143         "PCnet-PCI II",
144         "PCnet-FAST",
145         "PCnet-FAST+",
146         "PCnet-Home",
147 };
148
149 static void lnc_setladrf __P((struct lnc_softc *sc));
150 static void lnc_stop __P((struct lnc_softc *sc));
151 static void lnc_reset __P((struct lnc_softc *sc));
152 static void lnc_free_mbufs __P((struct lnc_softc *sc));
153 static __inline int alloc_mbuf_cluster __P((struct lnc_softc *sc,
154                                             struct host_ring_entry *desc));
155 static __inline struct mbuf *chain_mbufs __P((struct lnc_softc *sc,
156                                               int start_of_packet,
157                                               int pkt_len));
158 static __inline struct mbuf *mbuf_packet __P((struct lnc_softc *sc,
159                                               int start_of_packet,
160                                               int pkt_len));
161 static __inline void lnc_rint __P((struct lnc_softc *sc));
162 static __inline void lnc_tint __P((struct lnc_softc *sc));
163 static int lnc_probe __P((struct isa_device *isa_dev));
164 #ifdef PC98
165 static int cnet98s_probe __P((struct lnc_softc *sc, unsigned iobase));
166 #endif
167 static int ne2100_probe __P((struct lnc_softc *sc, unsigned iobase));
168 static int bicc_probe __P((struct lnc_softc *sc, unsigned iobase));
169 static int dec_macaddr_extract __P((u_char ring[], struct lnc_softc *sc));
170 static int depca_probe __P((struct lnc_softc *sc, unsigned iobase));
171 static int lance_probe __P((struct lnc_softc *sc));
172 static int pcnet_probe __P((struct lnc_softc *sc));
173 static int lnc_attach_sc __P((struct lnc_softc *sc, int unit));
174 static int lnc_attach __P((struct isa_device *isa_dev));
175 static void lnc_init __P((void *));
176 static ointhand2_t lncintr;
177 static __inline int mbuf_to_buffer __P((struct mbuf *m, char *buffer));
178 static __inline struct mbuf *chain_to_cluster __P((struct mbuf *m));
179 static void lnc_start __P((struct ifnet *ifp));
180 static int lnc_ioctl __P((struct ifnet *ifp, u_long command, caddr_t data));
181 static void lnc_watchdog __P((struct ifnet *ifp));
182 #ifdef DEBUG
183 void lnc_dump_state __P((struct lnc_softc *sc));
184 void mbuf_dump_chain __P((struct mbuf *m));
185 #endif
186
187 #if NPCI > 0
188 void *lnc_attach_ne2100_pci __P((int unit, unsigned iobase));
189 #endif
190 void lncintr_sc __P((struct lnc_softc *sc));
191
192 struct isa_driver lncdriver = {lnc_probe, lnc_attach, "lnc"};
193
194 static __inline void
195 write_csr(struct lnc_softc *sc, u_short port, u_short val)
196 {
197         outw(sc->rap, port);
198         outw(sc->rdp, val);
199 }
200
201 static __inline u_short
202 read_csr(struct lnc_softc *sc, u_short port)
203 {
204         outw(sc->rap, port);
205         return (inw(sc->rdp));
206 }
207
208 static __inline void
209 write_bcr(struct lnc_softc *sc, u_short port, u_short val)
210 {
211         outw(sc->rap, port);
212         outw(sc->bdp, val);
213 }
214
215 static __inline u_short
216 read_bcr(struct lnc_softc *sc, u_short port)
217 {
218         outw(sc->rap, port);
219         return (inw(sc->bdp));
220 }
221
222 static __inline u_long
223 ether_crc(const u_char *ether_addr)
224 {
225 #define POLYNOMIAL           0xEDB88320UL
226     u_char i, j, addr;
227     u_int crc = 0xFFFFFFFFUL;
228
229     for (i = 0; i < ETHER_ADDR_LEN; i++) {
230         addr = *ether_addr++;
231         for (j = 0; j < MULTICAST_FILTER_LEN; j++) {
232             crc = (crc >> 1) ^ (((crc ^ addr) & 1) ? POLYNOMIAL : 0);   
233             addr >>= 1;
234         }
235     }
236     return crc;
237 #undef POLYNOMIAL
238 }
239
240 /*
241  * Set up the logical address filter for multicast packets
242  */
243 static __inline void
244 lnc_setladrf(struct lnc_softc *sc)
245 {
246         struct ifnet *ifp = &sc->arpcom.ac_if;
247         struct ifmultiaddr *ifma;
248         u_long index;
249         int i;
250
251         if (sc->flags & IFF_ALLMULTI) {
252             for (i=0; i < MULTICAST_FILTER_LEN; i++)
253                 sc->init_block->ladrf[i] = 0xFF;
254             return;
255         }
256
257         /*
258          * For each multicast address, calculate a crc for that address and
259          * then use the high order 6 bits of the crc as a hash code where
260          * bits 3-5 select the byte of the address filter and bits 0-2 select
261          * the bit within that byte.
262          */
263
264         bzero(sc->init_block->ladrf, MULTICAST_FILTER_LEN);
265         for (ifma = ifp->if_multiaddrs.lh_first; ifma;
266              ifma = ifma->ifma_link.le_next) {
267                 if (ifma->ifma_addr->sa_family != AF_LINK)
268                         continue;
269
270                 index = ether_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr))
271                                 >> 26;
272                 sc->init_block->ladrf[index >> 3] |= 1 << (index & 7);
273         }
274 }
275
276 static void
277 lnc_stop(struct lnc_softc *sc)
278 {
279         write_csr(sc, CSR0, STOP);
280 }
281
282 static void
283 lnc_reset(struct lnc_softc *sc)
284 {
285         lnc_init(sc);
286 }
287
288 static void
289 lnc_free_mbufs(struct lnc_softc *sc)
290 {
291         int i;
292
293         /*
294          * We rely on other routines to keep the buff.mbuf field valid. If
295          * it's not NULL then we assume it points to an allocated mbuf.
296          */
297
298         for (i = 0; i < NDESC(sc->nrdre); i++)
299                 if ((sc->recv_ring + i)->buff.mbuf)
300                         m_free((sc->recv_ring + i)->buff.mbuf);
301
302         for (i = 0; i < NDESC(sc->ntdre); i++)
303                 if ((sc->trans_ring + i)->buff.mbuf)
304                         m_free((sc->trans_ring + i)->buff.mbuf);
305
306         if (sc->mbuf_count)
307                 m_freem(sc->mbufs);
308 }
309
310 static __inline int
311 alloc_mbuf_cluster(struct lnc_softc *sc, struct host_ring_entry *desc)
312 {
313         register struct mds *md = desc->md;
314         struct mbuf *m=0;
315         int addr;
316
317         /* Try and get cluster off local cache */
318         if (sc->mbuf_count) {
319                 sc->mbuf_count--;
320                 m = sc->mbufs;
321                 sc->mbufs = m->m_next;
322                 /* XXX m->m_data = m->m_ext.ext_buf;*/
323         } else {
324                 MGET(m, M_DONTWAIT, MT_DATA);
325         if (!m)
326                         return(1);
327       MCLGET(m, M_DONTWAIT);
328         if (!m->m_ext.ext_buf) {
329                         m_free(m);
330                         return(1);
331                 }
332         }
333
334         desc->buff.mbuf = m;
335         addr = kvtop(m->m_data);
336         md->md0 = addr;
337         md->md1= ((addr >> 16) & 0xff) | OWN;
338         md->md2 = -(short)(MCLBYTES - sizeof(struct pkthdr));
339         md->md3 = 0;
340         return(0);
341 }
342
343 static __inline struct mbuf *
344 chain_mbufs(struct lnc_softc *sc, int start_of_packet, int pkt_len)
345 {
346         struct mbuf *head, *m;
347         struct host_ring_entry *desc;
348
349         /*
350          * Turn head into a pkthdr mbuf --
351          * assumes a pkthdr type mbuf was
352          * allocated to the descriptor
353          * originally.
354          */
355
356         desc = sc->recv_ring + start_of_packet;
357
358         head = desc->buff.mbuf;
359         head->m_flags |= M_PKTHDR;
360
361         m = head;
362         do {
363                 m = desc->buff.mbuf;
364                 m->m_len = min((MCLBYTES - sizeof(struct pkthdr)), pkt_len);
365                 pkt_len -= m->m_len;
366                 if (alloc_mbuf_cluster(sc, desc))
367                         return((struct mbuf *)NULL);
368                 INC_MD_PTR(start_of_packet, sc->nrdre)
369                 desc = sc->recv_ring + start_of_packet;
370                 m->m_next = desc->buff.mbuf;
371         } while (start_of_packet != sc->recv_next);
372
373         m->m_next = 0;
374         return(head);
375 }
376
377 static __inline struct mbuf *
378 mbuf_packet(struct lnc_softc *sc, int start_of_packet, int pkt_len)
379 {
380
381         struct host_ring_entry *start;
382         struct mbuf *head,*m,*m_prev;
383         char *data,*mbuf_data;
384         short blen;
385         int amount;
386
387         /* Get a pkthdr mbuf for the start of packet */
388         MGETHDR(head, M_DONTWAIT, MT_DATA);
389         if (!head) {
390                 LNCSTATS(drop_packet)
391                 return(0);
392         }
393
394         m = head;
395         m->m_len = 0;
396         start = sc->recv_ring + start_of_packet;
397         /*blen = -(start->md->md2);*/
398         blen = RECVBUFSIZE; /* XXX More PCnet-32 crap */
399         data = start->buff.data;
400         mbuf_data = m->m_data;
401
402         while (start_of_packet != sc->recv_next) {
403                 /*
404                  * If the data left fits in a single buffer then set
405                  * blen to the size of the data left.
406                  */
407                 if (pkt_len < blen)
408                         blen = pkt_len;
409
410                 /*
411                  * amount is least of data in current ring buffer and
412                  * amount of space left in current mbuf.
413                  */
414                 amount = min(blen, M_TRAILINGSPACE(m));
415                 if (amount == 0) {
416                         /* mbuf must be empty */
417                         m_prev = m;
418                         MGET(m, M_DONTWAIT, MT_DATA);
419                         if (!m) {
420                                 m_freem(head);
421                                 return(0);
422                         }
423                         if (pkt_len >= MINCLSIZE)
424                                 MCLGET(m, M_DONTWAIT);
425                         m->m_len = 0;
426                         m_prev->m_next = m;
427                         amount = min(blen, M_TRAILINGSPACE(m));
428                         mbuf_data = m->m_data;
429                 }
430                 bcopy(data, mbuf_data, amount);
431                 blen -= amount;
432                 pkt_len -= amount;
433                 m->m_len += amount;
434                 data += amount;
435                 mbuf_data += amount;
436
437                 if (blen == 0) {
438                         start->md->md1 &= HADR;
439                         start->md->md1 |= OWN;
440                         start->md->md2 = -RECVBUFSIZE; /* XXX - shouldn't be necessary */
441                         INC_MD_PTR(start_of_packet, sc->nrdre)
442                         start = sc->recv_ring + start_of_packet;
443                         data = start->buff.data;
444                         /*blen = -(start->md->md2);*/
445                         blen = RECVBUFSIZE; /* XXX More PCnet-32 crap */
446                 }
447         }
448         return(head);
449 }
450
451
452 static __inline void
453 lnc_rint(struct lnc_softc *sc)
454 {
455         struct host_ring_entry *next, *start;
456         int start_of_packet;
457         struct mbuf *head;
458         struct ether_header *eh;
459         int lookahead;
460         int flags;
461         int pkt_len;
462
463         /*
464          * The LANCE will issue a RINT interrupt when the ownership of the
465          * last buffer of a receive packet has been relinquished by the LANCE.
466          * Therefore, it can be assumed that a complete packet can be found
467          * before hitting buffers that are still owned by the LANCE, if not
468          * then there is a bug in the driver that is causing the descriptors
469          * to get out of sync.
470          */
471
472 #ifdef DIAGNOSTIC
473         if ((sc->recv_ring + sc->recv_next)->md->md1 & OWN) {
474                 int unit = sc->arpcom.ac_if.if_unit;
475                 log(LOG_ERR, "lnc%d: Receive interrupt with buffer still owned by controller -- Resetting\n", unit);
476                 lnc_reset(sc);
477                 return;
478         }
479         if (!((sc->recv_ring + sc->recv_next)->md->md1 & STP)) {
480                 int unit = sc->arpcom.ac_if.if_unit;
481                 log(LOG_ERR, "lnc%d: Receive interrupt but not start of packet -- Resetting\n", unit);
482                 lnc_reset(sc);
483                 return;
484         }
485 #endif
486
487         lookahead = 0;
488         next = sc->recv_ring + sc->recv_next;
489         while ((flags = next->md->md1) & STP) {
490
491                 /* Make a note of the start of the packet */
492                 start_of_packet = sc->recv_next;
493
494                 /*
495                  * Find the end of the packet. Even if not data chaining,
496                  * jabber packets can overrun into a second descriptor.
497                  * If there is no error, then the ENP flag is set in the last
498                  * descriptor of the packet. If there is an error then the ERR
499                  * flag will be set in the descriptor where the error occured.
500                  * Therefore, to find the last buffer of a packet we search for
501                  * either ERR or ENP.
502                  */
503
504                 if (!(flags & (ENP | MDERR))) {
505                         do {
506                                 INC_MD_PTR(sc->recv_next, sc->nrdre)
507                                 next = sc->recv_ring + sc->recv_next;
508                                 flags = next->md->md1;
509                         } while (!(flags & (STP | OWN | ENP | MDERR)));
510
511                         if (flags & STP) {
512                                 int unit = sc->arpcom.ac_if.if_unit;
513                                 log(LOG_ERR, "lnc%d: Start of packet found before end of previous in receive ring -- Resetting\n", unit);
514                                 lnc_reset(sc);
515                                 return;
516                         }
517                         if (flags & OWN) {
518                                 if (lookahead) {
519                                         /*
520                                          * Looked ahead into a packet still
521                                          * being received
522                                          */
523                                         sc->recv_next = start_of_packet;
524                                         break;
525                                 } else {
526                                         int unit = sc->arpcom.ac_if.if_unit;
527                                         log(LOG_ERR, "lnc%d: End of received packet not found-- Resetting\n", unit);
528                                         lnc_reset(sc);
529                                         return;
530                                 }
531                         }
532                 }
533
534                 pkt_len = (next->md->md3 & MCNT) - FCS_LEN;
535
536                 /* Move pointer onto start of next packet */
537                 INC_MD_PTR(sc->recv_next, sc->nrdre)
538                 next = sc->recv_ring + sc->recv_next;
539
540                 if (flags & MDERR) {
541                         int unit = sc->arpcom.ac_if.if_unit;
542                         if (flags & RBUFF) {
543                                 LNCSTATS(rbuff)
544                                 log(LOG_ERR, "lnc%d: Receive buffer error\n", unit);
545                         }
546                         if (flags & OFLO) {
547                                 /* OFLO only valid if ENP is not set */
548                                 if (!(flags & ENP)) {
549                                         LNCSTATS(oflo)
550                                         log(LOG_ERR, "lnc%d: Receive overflow error \n", unit);
551                                 }
552                         } else if (flags & ENP) {
553                             if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC)==0) {
554                                 /*
555                                  * FRAM and CRC are valid only if ENP
556                                  * is set and OFLO is not.
557                                  */
558                                 if (flags & FRAM) {
559                                         LNCSTATS(fram)
560                                         log(LOG_ERR, "lnc%d: Framing error\n", unit);
561                                         /*
562                                          * FRAM is only set if there's a CRC
563                                          * error so avoid multiple messages
564                                          */
565                                 } else if (flags & CRC) {
566                                         LNCSTATS(crc)
567                                         log(LOG_ERR, "lnc%d: Receive CRC error\n", unit);
568                                 }
569                             }
570                         }
571
572                         /* Drop packet */
573                         LNCSTATS(rerr)
574                         sc->arpcom.ac_if.if_ierrors++;
575                         while (start_of_packet != sc->recv_next) {
576                                 start = sc->recv_ring + start_of_packet;
577                                 start->md->md2 = -RECVBUFSIZE; /* XXX - shouldn't be necessary */
578                                 start->md->md1 &= HADR;
579                                 start->md->md1 |= OWN;
580                                 INC_MD_PTR(start_of_packet, sc->nrdre)
581                         }
582                 } else { /* Valid packet */
583
584                         sc->arpcom.ac_if.if_ipackets++;
585
586
587                         if (sc->nic.mem_mode == DMA_MBUF)
588                                 head = chain_mbufs(sc, start_of_packet, pkt_len);
589                         else
590                                 head = mbuf_packet(sc, start_of_packet, pkt_len);
591
592                         if (head) {
593                                 /*
594                                  * First mbuf in packet holds the
595                                  * ethernet and packet headers
596                                  */
597                                 head->m_pkthdr.rcvif = &sc->arpcom.ac_if;
598                                 head->m_pkthdr.len = pkt_len ;
599                                 eh = (struct ether_header *) head->m_data;
600
601                                 /*
602                                  * vmware ethernet hardware emulation loops
603                                  * packets back to itself, violates IFF_SIMPLEX.
604                                  * drop it if it is from myself.
605                                  */
606                                 if (bcmp(eh->ether_shost,
607                                       sc->arpcom.ac_enaddr, ETHER_ADDR_LEN) == 0) {
608                                         m_freem(head);
609                                 } else {
610                                         /* Skip over the ether header */
611                                         head->m_data += sizeof *eh;
612                                         head->m_len -= sizeof *eh;
613                                         head->m_pkthdr.len -= sizeof *eh;
614
615                                         ether_input(&sc->arpcom.ac_if, eh, head);
616                                 }
617
618                         } else {
619                                 int unit = sc->arpcom.ac_if.if_unit;
620                                 log(LOG_ERR,"lnc%d: Packet dropped, no mbufs\n",unit);
621                                 LNCSTATS(drop_packet)
622                         }
623                 }
624
625                 lookahead++;
626         }
627
628         /*
629          * At this point all completely received packets have been processed
630          * so clear RINT since any packets that have arrived while we were in
631          * here have been dealt with.
632          */
633
634         outw(sc->rdp, RINT | INEA);
635 }
636
637 static __inline void
638 lnc_tint(struct lnc_softc *sc)
639 {
640         struct host_ring_entry *next, *start;
641         int start_of_packet;
642         int lookahead;
643
644         /*
645          * If the driver is reset in this routine then we return immediately to
646          * the interrupt driver routine. Any interrupts that have occured
647          * since the reset will be dealt with there. sc->trans_next
648          * should point to the start of the first packet that was awaiting
649          * transmission after the last transmit interrupt was dealt with. The
650          * LANCE should have relinquished ownership of that descriptor before
651          * the interrupt. Therefore, sc->trans_next should point to a
652          * descriptor with STP set and OWN cleared. If not then the driver's
653          * pointers are out of sync with the LANCE, which signifies a bug in
654          * the driver. Therefore, the following two checks are really
655          * diagnostic, since if the driver is working correctly they should
656          * never happen.
657          */
658
659 #ifdef DIAGNOSTIC
660         if ((sc->trans_ring + sc->trans_next)->md->md1 & OWN) {
661                 int unit = sc->arpcom.ac_if.if_unit;
662                 log(LOG_ERR, "lnc%d: Transmit interrupt with buffer still owned by controller -- Resetting\n", unit);
663                 lnc_reset(sc);
664                 return;
665         }
666 #endif
667
668
669         /*
670          * The LANCE will write the status information for the packet it just
671          * tried to transmit in one of two places. If the packet was
672          * transmitted successfully then the status will be written into the
673          * last descriptor of the packet. If the transmit failed then the
674          * status will be written into the descriptor that was being accessed
675          * when the error occured and all subsequent descriptors in that
676          * packet will have been relinquished by the LANCE.
677          *
678          * At this point we know that sc->trans_next points to the start
679          * of a packet that the LANCE has just finished trying to transmit.
680          * We now search for a buffer with either ENP or ERR set.
681          */
682
683         lookahead = 0;
684
685         do {
686                 start_of_packet = sc->trans_next;
687                 next = sc->trans_ring + sc->trans_next;
688
689 #ifdef DIAGNOSTIC
690         if (!(next->md->md1 & STP)) {
691                 int unit = sc->arpcom.ac_if.if_unit;
692                 log(LOG_ERR, "lnc%d: Transmit interrupt but not start of packet -- Resetting\n", unit);
693                 lnc_reset(sc);
694                 return;
695         }
696 #endif
697
698                 /*
699                  * Find end of packet.
700                  */
701
702                 if (!(next->md->md1 & (ENP | MDERR))) {
703                         do {
704                                 INC_MD_PTR(sc->trans_next, sc->ntdre)
705                                 next = sc->trans_ring + sc->trans_next;
706                         } while (!(next->md->md1 & (STP | OWN | ENP | MDERR)));
707
708                         if (next->md->md1 & STP) {
709                                 int unit = sc->arpcom.ac_if.if_unit;
710                                 log(LOG_ERR, "lnc%d: Start of packet found before end of previous in transmit ring -- Resetting\n", unit);
711                                 lnc_reset(sc);
712                                 return;
713                         }
714                         if (next->md->md1 & OWN) {
715                                 if (lookahead) {
716                                         /*
717                                          * Looked ahead into a packet still
718                                          * being transmitted
719                                          */
720                                         sc->trans_next = start_of_packet;
721                                         break;
722                                 } else {
723                                         int unit = sc->arpcom.ac_if.if_unit;
724                                         log(LOG_ERR, "lnc%d: End of transmitted packet not found -- Resetting\n", unit);
725                                         lnc_reset(sc);
726                                         return;
727                                 }
728                         }
729                 }
730                 /*
731                  * Check for ERR first since other flags are irrelevant if an
732                  * error occurred.
733                  */
734                 if (next->md->md1 & MDERR) {
735
736                         int unit = sc->arpcom.ac_if.if_unit;
737
738                         LNCSTATS(terr)
739                                 sc->arpcom.ac_if.if_oerrors++;
740
741                         if (next->md->md3 & LCOL) {
742                                 LNCSTATS(lcol)
743                                 log(LOG_ERR, "lnc%d: Transmit late collision  -- Net error?\n", unit);
744                                 sc->arpcom.ac_if.if_collisions++;
745                                 /*
746                                  * Clear TBUFF since it's not valid when LCOL
747                                  * set
748                                  */
749                                 next->md->md3 &= ~TBUFF;
750                         }
751                         if (next->md->md3 & LCAR) {
752                                 LNCSTATS(lcar)
753                                 log(LOG_ERR, "lnc%d: Loss of carrier during transmit -- Net error?\n", unit);
754                         }
755                         if (next->md->md3 & RTRY) {
756                                 LNCSTATS(rtry)
757                                 log(LOG_ERR, "lnc%d: Transmit of packet failed after 16 attempts -- TDR = %d\n", unit, ((sc->trans_ring + sc->trans_next)->md->md3 & TDR));
758                                 sc->arpcom.ac_if.if_collisions += 16;
759                                 /*
760                                  * Clear TBUFF since it's not valid when RTRY
761                                  * set
762                                  */
763                                 next->md->md3 &= ~TBUFF;
764                         }
765                         /*
766                          * TBUFF is only valid if neither LCOL nor RTRY are set.
767                          * We need to check UFLO after LCOL and RTRY so that we
768                          * know whether or not TBUFF is valid. If either are
769                          * set then TBUFF will have been cleared above. A
770                          * UFLO error will turn off the transmitter so we
771                          * have to reset.
772                          *
773                          */
774
775                         if (next->md->md3 & UFLO) {
776                                 LNCSTATS(uflo)
777                                 /*
778                                  * If an UFLO has occured it's possibly due
779                                  * to a TBUFF error
780                                  */
781                                 if (next->md->md3 & TBUFF) {
782                                         LNCSTATS(tbuff)
783                                         log(LOG_ERR, "lnc%d: Transmit buffer error -- Resetting\n", unit);
784                                 } else
785                                         log(LOG_ERR, "lnc%d: Transmit underflow error -- Resetting\n", unit);
786                                 lnc_reset(sc);
787                                 return;
788                         }
789                         do {
790                                 INC_MD_PTR(sc->trans_next, sc->ntdre)
791                                 next = sc->trans_ring + sc->trans_next;
792                         } while (!(next->md->md1 & STP) && (sc->trans_next != sc->next_to_send));
793
794                 } else {
795                         /*
796                          * Since we check for ERR first then if we get here
797                          * the packet was transmitted correctly. There may
798                          * still have been non-fatal errors though.
799                          * Don't bother checking for DEF, waste of time.
800                          */
801
802                         sc->arpcom.ac_if.if_opackets++;
803
804                         if (next->md->md1 & MORE) {
805                                 LNCSTATS(more)
806                                 sc->arpcom.ac_if.if_collisions += 2;
807                         }
808
809                         /*
810                          * ONE is invalid if LCOL is set. If LCOL was set then
811                          * ERR would have also been set and we would have
812                          * returned from lnc_tint above. Therefore we can
813                          * assume if we arrive here that ONE is valid.
814                          *
815                          */
816
817                         if (next->md->md1 & ONE) {
818                                 LNCSTATS(one)
819                                 sc->arpcom.ac_if.if_collisions++;
820                         }
821                         INC_MD_PTR(sc->trans_next, sc->ntdre)
822                         next = sc->trans_ring + sc->trans_next;
823                 }
824
825                 /*
826                  * Clear descriptors and free any mbufs.
827                  */
828
829                 do {
830                         start = sc->trans_ring + start_of_packet;
831                         start->md->md1 &= HADR;
832                         if (sc->nic.mem_mode == DMA_MBUF) {
833                                 /* Cache clusters on a local queue */
834                                 if ((start->buff.mbuf->m_flags & M_EXT) && (sc->mbuf_count < MBUF_CACHE_LIMIT)) {
835                                         if (sc->mbuf_count) {
836                                                 start->buff.mbuf->m_next = sc->mbufs;
837                                                 sc->mbufs = start->buff.mbuf;
838                                         } else
839                                                 sc->mbufs = start->buff.mbuf;
840                                         sc->mbuf_count++;
841                                         start->buff.mbuf = 0;
842                                 } else {
843                                         /* XXX shouldn't this be m_freem ?? */
844                                         m_free(start->buff.mbuf);
845                                         start->buff.mbuf = NULL;
846                                 }
847                         }
848                         sc->pending_transmits--;
849                         INC_MD_PTR(start_of_packet, sc->ntdre)
850                 }while (start_of_packet != sc->trans_next);
851
852                 /*
853                  * There's now at least one free descriptor
854                  * in the ring so indicate that we can accept
855                  * more packets again.
856                  */
857
858                 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
859
860                 lookahead++;
861
862         } while (sc->pending_transmits && !(next->md->md1 & OWN));
863
864         /*
865          * Clear TINT since we've dealt with all
866          * the completed transmissions.
867          */
868
869         outw(sc->rdp, TINT | INEA);
870
871         /* XXX only while doing if_is comparisons */
872         if (!(sc->arpcom.ac_if.if_flags & IFF_OACTIVE))
873                 lnc_start(&sc->arpcom.ac_if);
874
875 }
876
877 static int
878 lnc_probe(struct isa_device * isa_dev)
879 {
880         int nports;
881         int unit = isa_dev->id_unit;
882         struct lnc_softc *sc = &lnc_softc[unit];
883         unsigned iobase = isa_dev->id_iobase;
884
885 #ifdef DIAGNOSTIC
886         int vsw;
887         vsw = inw(isa_dev->id_iobase + PCNET_VSW);
888         printf("Vendor Specific Word = %x\n", vsw);
889 #endif
890
891         nports = bicc_probe(sc, iobase);
892         if (nports == 0)
893                 nports = ne2100_probe(sc, iobase);
894         if (nports == 0)
895                 nports = depca_probe(sc, iobase);
896 #ifdef PC98
897         if (nports == 0)
898                 nports = cnet98s_probe(sc, iobase);
899 #endif
900         return (nports);
901 }
902
903 #ifdef PC98
904 /* ISA Bus Configuration Registers */
905 /* XXX - Should be in ic/Am7990.h */
906 #define MSRDA   0x0000  /* ISACSR0: Master Mode Read Activity */
907 #define MSWRA   0x0001  /* ISACSR1: Master Mode Write Activity */
908 #define MC      0x0002  /* ISACSR2: Miscellaneous Configuration */
909
910 #define LED1    0x0005  /* ISACSR5: LED1 Status */
911 #define LED2    0x0006  /* ISACSR6: LED2 Status */
912 #define LED3    0x0007  /* ISACSR7: LED3 Status */
913
914 #define LED_PSE         0x0080  /* Pulse Stretcher */
915 #define LED_XMTE        0x0010  /* Transmit Status */
916 #define LED_RVPOLE      0x0008  /* Receive Polarity */
917 #define LED_RCVE        0x0004  /* Receive Status */
918 #define LED_JABE        0x0002  /* Jabber */
919 #define LED_COLE        0x0001  /* Collision */
920
921 static int
922 cnet98s_probe(struct lnc_softc *sc, unsigned iobase)
923 {
924         int i;
925         ushort tmp;
926
927         sc->rap = iobase + CNET98S_RAP;
928         sc->rdp = iobase + CNET98S_RDP;
929
930         /* Reset */
931         tmp = inw(iobase + CNET98S_RESET);
932         outw(iobase + CNET98S_RESET, tmp);
933         DELAY(500);
934
935         sc->nic.ic = pcnet_probe(sc);
936         if ((sc->nic.ic == UNKNOWN) || (sc->nic.ic > PCnet_32)) {
937                 return (0);
938         }
939
940         sc->nic.ident = CNET98S;
941         sc->nic.mem_mode = DMA_FIXED;
942
943         /* XXX - For now just use the defines */
944         sc->nrdre = NRDRE;
945         sc->ntdre = NTDRE;
946
947         /* Extract MAC address from PROM */
948         for (i = 0; i < ETHER_ADDR_LEN; i++) {
949                 sc->arpcom.ac_enaddr[i] = inb(iobase + (i * 2));
950         }
951
952         /*
953          * ISA Configuration
954          *
955          * XXX - Following parameters are Contec C-NET(98)S only.
956          *       So, check the Ethernet address here.
957          *
958          *       Contec uses 00 80 4c ?? ?? ??
959          */ 
960         if (sc->arpcom.ac_enaddr[0] == (u_char)0x00
961         &&  sc->arpcom.ac_enaddr[1] == (u_char)0x80
962         &&  sc->arpcom.ac_enaddr[2] == (u_char)0x4c) {
963                 outw(sc->rap, MSRDA);
964                 outw(iobase + CNET98S_IDP, 0x0006);
965                 outw(sc->rap, MSWRA);
966                 outw(iobase + CNET98S_IDP, 0x0006);
967 #ifdef DIAGNOSTIC
968                 outw(sc->rap, MC);
969                 printf("ISACSR2 = %x\n", inw(iobase + CNET98S_IDP));
970 #endif
971                 outw(sc->rap, LED1);
972                 outw(iobase + CNET98S_IDP, LED_PSE | LED_XMTE);
973                 outw(sc->rap, LED2);
974                 outw(iobase + CNET98S_IDP, LED_PSE | LED_RCVE);
975                 outw(sc->rap, LED3);
976                 outw(iobase + CNET98S_IDP, LED_PSE | LED_COLE);
977         }
978                 
979         return (CNET98S_IOSIZE);
980 }
981 #endif
982
983 static int
984 ne2100_probe(struct lnc_softc *sc, unsigned iobase)
985 {
986         int i;
987
988         sc->rap = iobase + PCNET_RAP;
989         sc->rdp = iobase + PCNET_RDP;
990
991         sc->nic.ic = pcnet_probe(sc);
992         if ((sc->nic.ic > 0) && (sc->nic.ic < PCnet_PCI)) {
993                 sc->nic.ident = NE2100;
994                 sc->nic.mem_mode = DMA_FIXED;
995
996                 /* XXX - For now just use the defines */
997                 sc->nrdre = NRDRE;
998                 sc->ntdre = NTDRE;
999
1000                 /* Extract MAC address from PROM */
1001                 for (i = 0; i < ETHER_ADDR_LEN; i++)
1002                         sc->arpcom.ac_enaddr[i] = inb(iobase + i);
1003                 return (NE2100_IOSIZE);
1004         } else {
1005                 return (0);
1006         }
1007 }
1008
1009 static int
1010 bicc_probe(struct lnc_softc *sc, unsigned iobase)
1011 {
1012         int i;
1013
1014         /*
1015          * There isn't any way to determine if a NIC is a BICC. Basically, if
1016          * the lance probe succeeds using the i/o addresses of the BICC then
1017          * we assume it's a BICC.
1018          *
1019          */
1020
1021         sc->rap = iobase + BICC_RAP;
1022         sc->rdp = iobase + BICC_RDP;
1023
1024         /* I think all these cards us the Am7990 */
1025
1026         if ((sc->nic.ic = lance_probe(sc))) {
1027                 sc->nic.ident = BICC;
1028                 sc->nic.mem_mode = DMA_FIXED;
1029
1030                 /* XXX - For now just use the defines */
1031                 sc->nrdre = NRDRE;
1032                 sc->ntdre = NTDRE;
1033
1034                 /* Extract MAC address from PROM */
1035                 for (i = 0; i < ETHER_ADDR_LEN; i++)
1036                         sc->arpcom.ac_enaddr[i] = inb(iobase + (i * 2));
1037
1038                 return (BICC_IOSIZE);
1039         } else {
1040                 return (0);
1041         }
1042 }
1043
1044 /*
1045  * I don't have data sheets for the dec cards but it looks like the mac
1046  * address is contained in a 32 byte ring. Each time you read from the port
1047  * you get the next byte in the ring. The mac address is stored after a
1048  * signature so keep searching for the signature first.
1049  */
1050 static int
1051 dec_macaddr_extract(u_char ring[], struct lnc_softc * sc)
1052 {
1053         const unsigned char signature[] = {0xff, 0x00, 0x55, 0xaa, 0xff, 0x00, 0x55, 0xaa};
1054
1055         int i, j, rindex;
1056
1057         for (i = 0; i < sizeof ring; i++) {
1058                 for (j = 0, rindex = i; j < sizeof signature; j++) {
1059                         if (ring[rindex] != signature[j])
1060                                 break;
1061                         if (++rindex > sizeof ring)
1062                                 rindex = 0;
1063                 }
1064                 if (j == sizeof signature) {
1065                         for (j = 0, rindex = i; j < ETHER_ADDR_LEN; j++) {
1066                                 sc->arpcom.ac_enaddr[j] = ring[rindex];
1067                                 if (++rindex > sizeof ring)
1068                                         rindex = 0;
1069                         }
1070                         return (1);
1071                 }
1072         }
1073         return (0);
1074 }
1075
1076 static int
1077 depca_probe(struct lnc_softc *sc, unsigned iobase)
1078 {
1079         int i;
1080         unsigned char maddr_ring[DEPCA_ADDR_ROM_SIZE];
1081
1082         sc->rap = iobase + DEPCA_RAP;
1083         sc->rdp = iobase + DEPCA_RDP;
1084
1085         if ((sc->nic.ic = lance_probe(sc))) {
1086                 sc->nic.ident = DEPCA;
1087                 sc->nic.mem_mode = SHMEM;
1088
1089                 /* Extract MAC address from PROM */
1090                 for (i = 0; i < DEPCA_ADDR_ROM_SIZE; i++)
1091                         maddr_ring[i] = inb(iobase + DEPCA_ADP);
1092                 if (dec_macaddr_extract(maddr_ring, sc)) {
1093                         return (DEPCA_IOSIZE);
1094                 }
1095         }
1096         return (0);
1097 }
1098
1099 static int
1100 lance_probe(struct lnc_softc *sc)
1101 {
1102         write_csr(sc, CSR0, STOP);
1103
1104         if ((inw(sc->rdp) & STOP) && !(read_csr(sc, CSR3))) {
1105                 /*
1106                  * Check to see if it's a C-LANCE. For the LANCE the INEA bit
1107                  * cannot be set while the STOP bit is. This restriction is
1108                  * removed for the C-LANCE.
1109                  */
1110                 write_csr(sc, CSR0, INEA);
1111                 if (read_csr(sc, CSR0) & INEA)
1112                         return (C_LANCE);
1113                 else
1114                         return (LANCE);
1115         } else
1116                 return (UNKNOWN);
1117 }
1118
1119 static int
1120 pcnet_probe(struct lnc_softc *sc)
1121 {
1122         u_long chip_id;
1123         int type;
1124
1125         /*
1126          * The PCnet family don't reset the RAP register on reset so we'll
1127          * have to write during the probe :-) It does have an ID register
1128          * though so the probe is just a matter of reading it.
1129          */
1130
1131         if ((type = lance_probe(sc))) {
1132                 chip_id = read_csr(sc, CSR89);
1133                 chip_id <<= 16;
1134                 chip_id |= read_csr(sc, CSR88);
1135                 if (chip_id & AMD_MASK) {
1136                         chip_id >>= 12;
1137                         switch (chip_id & PART_MASK) {
1138                         case Am79C960:
1139                                 return (PCnet_ISA);
1140                         case Am79C961:
1141                                 return (PCnet_ISAplus);
1142                         case Am79C961A:
1143                                 return (PCnet_ISA_II);
1144                         case Am79C965:
1145                                 return (PCnet_32);
1146                         case Am79C970:
1147                                 return (PCnet_PCI);
1148                         case Am79C970A:
1149                                 return (PCnet_PCI_II);
1150                         case Am79C971:
1151                                 return (PCnet_FAST);
1152                         case Am79C972:
1153                         case Am79C973:
1154                                 return (PCnet_FASTplus);
1155                         case Am79C978:
1156                                 return (PCnet_Home);
1157                         default:
1158                                 break;
1159                         }
1160                 }
1161         }
1162         return (type);
1163 }
1164
1165 static int
1166 lnc_attach_sc(struct lnc_softc *sc, int unit)
1167 {
1168         int lnc_mem_size;
1169
1170         /*
1171          * Allocate memory for use by the controller.
1172          *
1173          * XXX -- the Am7990 and Am79C960 only have 24 address lines and so can
1174          * only access the lower 16Mb of physical memory. For the moment we
1175          * assume that malloc will allocate memory within the lower 16Mb
1176          * range. This is not a very valid assumption but there's nothing
1177          * that can be done about it yet. For shared memory NICs this isn't
1178          * relevant.
1179          *
1180          */
1181
1182         lnc_mem_size = ((NDESC(sc->nrdre) + NDESC(sc->ntdre)) *
1183                  sizeof(struct host_ring_entry));
1184
1185         if (sc->nic.mem_mode != SHMEM)
1186                 lnc_mem_size += sizeof(struct init_block) + (sizeof(struct mds) *
1187                             (NDESC(sc->nrdre) + NDESC(sc->ntdre))) +
1188                         MEM_SLEW;
1189
1190         /* If using DMA to fixed host buffers then allocate memory for them */
1191
1192         if (sc->nic.mem_mode == DMA_FIXED)
1193                 lnc_mem_size += (NDESC(sc->nrdre) * RECVBUFSIZE) + (NDESC(sc->ntdre) * TRANSBUFSIZE);
1194
1195         if (sc->nic.mem_mode != SHMEM) {
1196                 if (sc->nic.ic < PCnet_32) {
1197                         /* ISA based cards */
1198                         sc->recv_ring = contigmalloc(lnc_mem_size, M_DEVBUF, M_NOWAIT,
1199                                                                                  0ul, 0xfffffful, 4ul, 0x1000000);
1200                 } else {
1201                         /* Non-ISA based cards, 32 bit capable */
1202 #ifdef notyet
1203                         /*
1204                          * For the 32 bit driver we're not fussed where we DMA to
1205                          * though it'll still need to be contiguous
1206                          */
1207                         sc->recv_ring = malloc(lnc_mem_size, M_DEVBUF, M_NOWAIT);
1208 #else
1209                         /*
1210                          * For now it still needs to be below 16MB because the
1211                          * descriptor's can only hold 16 bit addresses.
1212                          */
1213                         sc->recv_ring = contigmalloc(lnc_mem_size, M_DEVBUF, M_NOWAIT,
1214                                                                                  0ul, 0xfffffful, 4ul, 0x1000000);
1215 #endif
1216                 }       
1217         }
1218
1219         if (!sc->recv_ring) {
1220                 log(LOG_ERR, "lnc%d: Couldn't allocate memory for NIC\n", unit);
1221                 return (0);     /* XXX -- attach failed -- not tested in
1222                                  * calling routines */
1223         }
1224
1225         /* Set default mode */
1226         sc->nic.mode = NORMAL;
1227
1228         /* Fill in arpcom structure entries */
1229
1230         sc->arpcom.ac_if.if_softc = sc;
1231         sc->arpcom.ac_if.if_name = lncdriver.name;
1232         sc->arpcom.ac_if.if_unit = unit;
1233         sc->arpcom.ac_if.if_mtu = ETHERMTU;
1234         sc->arpcom.ac_if.if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1235         sc->arpcom.ac_if.if_timer = 0;
1236         sc->arpcom.ac_if.if_output = ether_output;
1237         sc->arpcom.ac_if.if_start = lnc_start;
1238         sc->arpcom.ac_if.if_ioctl = lnc_ioctl;
1239         sc->arpcom.ac_if.if_watchdog = lnc_watchdog;
1240         sc->arpcom.ac_if.if_init = lnc_init;
1241         sc->arpcom.ac_if.if_type = IFT_ETHER;
1242         sc->arpcom.ac_if.if_addrlen = ETHER_ADDR_LEN;
1243         sc->arpcom.ac_if.if_hdrlen = ETHER_HDR_LEN;
1244         sc->arpcom.ac_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
1245
1246         ether_ifattach(&sc->arpcom.ac_if, ETHER_BPF_SUPPORTED);
1247
1248         printf("lnc%d: ", unit);
1249         if (sc->nic.ic == LANCE || sc->nic.ic == C_LANCE)
1250                 printf("%s (%s)",
1251                        nic_ident[sc->nic.ident], ic_ident[sc->nic.ic]);
1252         else
1253                 printf("%s", ic_ident[sc->nic.ic]);
1254         printf(" address %6D\n", sc->arpcom.ac_enaddr, ":");
1255
1256         return (1);
1257 }
1258
1259 static int
1260 lnc_attach(struct isa_device * isa_dev)
1261 {
1262         int unit = isa_dev->id_unit;
1263         struct lnc_softc *sc = &lnc_softc[unit];
1264         int result;
1265
1266         isa_dev->id_ointr = lncintr;
1267         result = lnc_attach_sc (sc, unit);
1268         if (result == 0)
1269                 return (0);
1270
1271 #ifndef PC98
1272         /*
1273          * XXX - is it safe to call isa_dmacascade() after if_attach() 
1274          *       and ether_ifattach() have been called in lnc_attach() ???
1275          */
1276         if ((sc->nic.mem_mode != SHMEM) &&
1277             (sc->nic.ic < PCnet_32))
1278                 isa_dmacascade(isa_dev->id_drq);
1279 #endif
1280
1281         return result;
1282 }
1283
1284 #if NPCI > 0
1285 void *
1286 lnc_attach_ne2100_pci(int unit, unsigned iobase)
1287 {
1288         int i;
1289         struct lnc_softc *sc = malloc(sizeof *sc, M_DEVBUF, M_NOWAIT);
1290
1291         if (sc) {
1292                 bzero (sc, sizeof *sc);
1293
1294                 sc->rap = iobase + PCNET_RAP;
1295                 sc->rdp = iobase + PCNET_RDP;
1296                 sc->bdp = iobase + PCNET_BDP;
1297
1298                 sc->nic.ic = pcnet_probe(sc);
1299                 if (sc->nic.ic >= PCnet_32) {
1300                         sc->nic.ident = NE2100;
1301                         sc->nic.mem_mode = DMA_FIXED;
1302   
1303                         /* XXX - For now just use the defines */
1304                         sc->nrdre = NRDRE;
1305                         sc->ntdre = NTDRE;
1306
1307                         /* Extract MAC address from PROM */
1308                         for (i = 0; i < ETHER_ADDR_LEN; i++)
1309                                 sc->arpcom.ac_enaddr[i] = inb(iobase + i);
1310
1311                         if (lnc_attach_sc(sc, unit) == 0) {
1312                                 free(sc, M_DEVBUF);
1313                                 sc = NULL;
1314                         }
1315                 }
1316                 else {
1317                         free(sc, M_DEVBUF);
1318                         sc = NULL;
1319                 }
1320         }
1321         return sc;
1322 }
1323 #endif
1324
1325 static void
1326 lnc_init(xsc)
1327         void *xsc;
1328 {
1329         struct lnc_softc *sc = xsc;
1330         int s, i;
1331         char *lnc_mem;
1332
1333         /* Check that interface has valid address */
1334
1335         if (TAILQ_EMPTY(&sc->arpcom.ac_if.if_addrhead)) /* XXX unlikely */
1336                 return;
1337
1338         /* Shut down interface */
1339
1340         s = splimp();
1341         lnc_stop(sc);
1342         sc->arpcom.ac_if.if_flags |= IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; /* XXX??? */
1343
1344         /*
1345          * This sets up the memory area for the controller. Memory is set up for
1346          * the initialisation block (12 words of contiguous memory starting
1347          * on a word boundary),the transmit and receive ring structures (each
1348          * entry is 4 words long and must start on a quadword boundary) and
1349          * the data buffers.
1350          *
1351          * The alignment tests are particularly paranoid.
1352          */
1353
1354
1355
1356         sc->recv_next = 0;
1357         sc->trans_ring = sc->recv_ring + NDESC(sc->nrdre);
1358         sc->trans_next = 0;
1359
1360         if (sc->nic.mem_mode == SHMEM)
1361                 lnc_mem = (char *) sc->nic.iobase;
1362         else
1363                 lnc_mem = (char *) (sc->trans_ring + NDESC(sc->ntdre));
1364
1365         lnc_mem = (char *)(((int)lnc_mem + 1) & ~1);
1366         sc->init_block = (struct init_block *) ((int) lnc_mem & ~1);
1367         lnc_mem = (char *) (sc->init_block + 1);
1368         lnc_mem = (char *)(((int)lnc_mem + 7) & ~7);
1369
1370         /* Initialise pointers to descriptor entries */
1371         for (i = 0; i < NDESC(sc->nrdre); i++) {
1372                 (sc->recv_ring + i)->md = (struct mds *) lnc_mem;
1373                 lnc_mem += sizeof(struct mds);
1374         }
1375         for (i = 0; i < NDESC(sc->ntdre); i++) {
1376                 (sc->trans_ring + i)->md = (struct mds *) lnc_mem;
1377                 lnc_mem += sizeof(struct mds);
1378         }
1379
1380         /* Initialise the remaining ring entries */
1381
1382         if (sc->nic.mem_mode == DMA_MBUF) {
1383
1384                 sc->mbufs = 0;
1385                 sc->mbuf_count = 0;
1386
1387                 /* Free previously allocated mbufs */
1388                 if (sc->flags & LNC_INITIALISED)
1389                         lnc_free_mbufs(sc);
1390
1391
1392                 for (i = 0; i < NDESC(sc->nrdre); i++) {
1393                         if (alloc_mbuf_cluster(sc, sc->recv_ring+i)) {
1394                                 log(LOG_ERR, "Initialisation failed -- no mbufs\n");
1395                                 splx(s);
1396                                 return;
1397                         }
1398                 }
1399
1400                 for (i = 0; i < NDESC(sc->ntdre); i++) {
1401                         (sc->trans_ring + i)->buff.mbuf = 0;
1402                         (sc->trans_ring + i)->md->md0 = 0;
1403                         (sc->trans_ring + i)->md->md1 = 0;
1404                         (sc->trans_ring + i)->md->md2 = 0;
1405                         (sc->trans_ring + i)->md->md3 = 0;
1406                 }
1407         } else {
1408                 for (i = 0; i < NDESC(sc->nrdre); i++) {
1409                         (sc->recv_ring + i)->md->md0 = kvtop(lnc_mem);
1410                         (sc->recv_ring + i)->md->md1 = ((kvtop(lnc_mem) >> 16) & 0xff) | OWN;
1411                         (sc->recv_ring + i)->md->md2 = -RECVBUFSIZE;
1412                         (sc->recv_ring + i)->md->md3 = 0;
1413                         (sc->recv_ring + i)->buff.data = lnc_mem;
1414                         lnc_mem += RECVBUFSIZE;
1415                 }
1416                 for (i = 0; i < NDESC(sc->ntdre); i++) {
1417                         (sc->trans_ring + i)->md->md0 = kvtop(lnc_mem);
1418                         (sc->trans_ring + i)->md->md1 = ((kvtop(lnc_mem) >> 16) & 0xff);
1419                         (sc->trans_ring + i)->md->md2 = 0;
1420                         (sc->trans_ring + i)->md->md3 = 0;
1421                         (sc->trans_ring + i)->buff.data = lnc_mem;
1422                         lnc_mem += TRANSBUFSIZE;
1423                 }
1424         }
1425
1426         sc->next_to_send = 0;
1427
1428         /* Set up initialisation block */
1429
1430         sc->init_block->mode = sc->nic.mode;
1431
1432         for (i = 0; i < ETHER_ADDR_LEN; i++)
1433                 sc->init_block->padr[i] = sc->arpcom.ac_enaddr[i];
1434
1435         lnc_setladrf(sc);
1436
1437         sc->init_block->rdra = kvtop(sc->recv_ring->md);
1438         sc->init_block->rlen = ((kvtop(sc->recv_ring->md) >> 16) & 0xff) | (sc->nrdre << 13);
1439         sc->init_block->tdra = kvtop(sc->trans_ring->md);
1440         sc->init_block->tlen = ((kvtop(sc->trans_ring->md) >> 16) & 0xff) | (sc->ntdre << 13);
1441
1442
1443         /* Set flags to show that the memory area is valid */
1444         sc->flags |= LNC_INITIALISED;
1445
1446         sc->pending_transmits = 0;
1447
1448         /* Give the LANCE the physical address of the initialisation block */
1449
1450         if (sc->nic.ic == PCnet_Home) {
1451                 u_short media;
1452                 /* Set PHY_SEL to HomeRun */
1453                 media = read_bcr(sc, BCR49);
1454                 media &= ~3;
1455                 media |= 1;
1456                 write_bcr(sc, BCR49, media);
1457         }
1458
1459         write_csr(sc, CSR1, kvtop(sc->init_block));
1460         write_csr(sc, CSR2, (kvtop(sc->init_block) >> 16) & 0xff);
1461
1462         /*
1463          * Depending on which controller this is, CSR3 has different meanings.
1464          * For the Am7990 it controls DMA operations, for the Am79C960 it
1465          * controls interrupt masks and transmitter algorithms. In either
1466          * case, none of the flags are set.
1467          *
1468          */
1469
1470         write_csr(sc, CSR3, 0);
1471
1472         /* Let's see if it starts */
1473
1474         write_csr(sc, CSR0, INIT);
1475         for (i = 0; i < 1000; i++)
1476                 if (read_csr(sc, CSR0) & IDON)
1477                         break;
1478
1479         /*
1480          * Now that the initialisation is complete there's no reason to
1481          * access anything except CSR0, so we leave RAP pointing there
1482          * so we can just access RDP from now on, saving an outw each
1483          * time.
1484          */
1485
1486         if (read_csr(sc, CSR0) & IDON) {
1487                 /*
1488                  * Enable interrupts, start the LANCE, mark the interface as
1489                  * running and transmit any pending packets.
1490                  */
1491                 write_csr(sc, CSR0, STRT | INEA);
1492                 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
1493                 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
1494                 lnc_start(&sc->arpcom.ac_if);
1495         } else
1496                 log(LOG_ERR, "lnc%d: Initialisation failed\n", 
1497                     sc->arpcom.ac_if.if_unit);
1498
1499         splx(s);
1500 }
1501
1502 /*
1503  * The interrupt flag (INTR) will be set and provided that the interrupt enable
1504  * flag (INEA) is also set, the interrupt pin will be driven low when any of
1505  * the following occur:
1506  *
1507  * 1) Completion of the initialisation routine (IDON). 2) The reception of a
1508  * packet (RINT). 3) The transmission of a packet (TINT). 4) A transmitter
1509  * timeout error (BABL). 5) A missed packet (MISS). 6) A memory error (MERR).
1510  *
1511  * The interrupt flag is cleared when all of the above conditions are cleared.
1512  *
1513  * If the driver is reset from this routine then it first checks to see if any
1514  * interrupts have ocurred since the reset and handles them before returning.
1515  * This is because the NIC may signify a pending interrupt in CSR0 using the
1516  * INTR flag even if a hardware interrupt is currently inhibited (at least I
1517  * think it does from reading the data sheets). We may as well deal with
1518  * these pending interrupts now rather than get the overhead of another
1519  * hardware interrupt immediately upon returning from the interrupt handler.
1520  *
1521  */
1522
1523 void
1524 lncintr_sc(struct lnc_softc *sc)
1525 {
1526         int unit = sc->arpcom.ac_if.if_unit;
1527         u_short csr0;
1528
1529         /*
1530          * INEA is the only bit that can be cleared by writing a 0 to it so
1531          * we have to include it in any writes that clear other flags.
1532          */
1533
1534         while ((csr0 = inw(sc->rdp)) & INTR) {
1535
1536                 /*
1537                  * Clear interrupt flags early to avoid race conditions. The
1538                  * controller can still set these flags even while we're in
1539                  * this interrupt routine. If the flag is still set from the
1540                  * event that caused this interrupt any new events will
1541                  * be missed.
1542                  */
1543
1544 /*              outw(sc->rdp, IDON | CERR | BABL | MISS | MERR | RINT | TINT | INEA); */
1545                 outw(sc->rdp, csr0);
1546
1547                 /* We don't do anything with the IDON flag */
1548
1549                 if (csr0 & ERR) {
1550                         if (csr0 & CERR) {
1551                                 log(LOG_ERR, "lnc%d: Heartbeat error -- SQE test failed\n", unit);
1552                                 LNCSTATS(cerr)
1553                         }
1554                         if (csr0 & BABL) {
1555                                 log(LOG_ERR, "lnc%d: Babble error - more than 1519 bytes transmitted\n", unit);
1556                                 LNCSTATS(babl)
1557                                 sc->arpcom.ac_if.if_oerrors++;
1558                         }
1559                         if (csr0 & MISS) {
1560                                 log(LOG_ERR, "lnc%d: Missed packet -- no receive buffer\n", unit);
1561                                 LNCSTATS(miss)
1562                                 sc->arpcom.ac_if.if_ierrors++;
1563                         }
1564                         if (csr0 & MERR) {
1565                                 log(LOG_ERR, "lnc%d: Memory error  -- Resetting\n", unit);
1566                                 LNCSTATS(merr)
1567                                 lnc_reset(sc);
1568                                 continue;
1569                         }
1570                 }
1571                 if (csr0 & RINT) {
1572                         LNCSTATS(rint)
1573                         lnc_rint(sc);
1574                 }
1575                 if (csr0 & TINT) {
1576                         LNCSTATS(tint)
1577                         sc->arpcom.ac_if.if_timer = 0;
1578                         lnc_tint(sc);
1579                 }
1580
1581                 /*
1582                  * If there's room in the transmit descriptor ring then queue
1583                  * some more transmit packets.
1584                  */
1585
1586                 if (!(sc->arpcom.ac_if.if_flags & IFF_OACTIVE))
1587                         lnc_start(&sc->arpcom.ac_if);
1588         }
1589 }
1590
1591 static void
1592 lncintr(int unit)
1593 {
1594         struct lnc_softc *sc = &lnc_softc[unit];
1595         lncintr_sc (sc);
1596 }
1597
1598 static __inline int
1599 mbuf_to_buffer(struct mbuf *m, char *buffer)
1600 {
1601
1602         int len=0;
1603
1604         for( ; m; m = m->m_next) {
1605                 bcopy(mtod(m, caddr_t), buffer, m->m_len);
1606                 buffer += m->m_len;
1607                 len += m->m_len;
1608         }
1609
1610         return(len);
1611 }
1612
1613 static __inline struct mbuf *
1614 chain_to_cluster(struct mbuf *m)
1615 {
1616         struct mbuf *new;
1617
1618         MGET(new, M_DONTWAIT, MT_DATA);
1619         if (new) {
1620                 MCLGET(new, M_DONTWAIT);
1621                 if (new->m_ext.ext_buf) {
1622                         new->m_len = mbuf_to_buffer(m, new->m_data);
1623                         m_freem(m);
1624                         return(new);
1625                 } else
1626                         m_free(new);
1627         }
1628         return(0);
1629 }
1630
1631 /*
1632  * IFF_OACTIVE and IFF_RUNNING are checked in ether_output so it's redundant
1633  * to check them again since we wouldn't have got here if they were not
1634  * appropriately set. This is also called from lnc_init and lncintr but the
1635  * flags should be ok at those points too.
1636  */
1637
1638 static void
1639 lnc_start(struct ifnet *ifp)
1640 {
1641
1642         struct lnc_softc *sc = ifp->if_softc;
1643         struct host_ring_entry *desc;
1644         int tmp;
1645         int end_of_packet;
1646         struct mbuf *head, *m;
1647         int len, chunk;
1648         int addr;
1649         int no_entries_needed;
1650
1651         do {
1652
1653                 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, head);
1654                 if (!head)
1655                         return;
1656
1657                 if (sc->nic.mem_mode == DMA_MBUF) {
1658
1659                         no_entries_needed = 0;
1660                         for (m=head; m; m = m->m_next)
1661                                 no_entries_needed++;
1662
1663                         /*
1664                          * We try and avoid bcopy as much as possible
1665                          * but there are two cases when we use it.
1666                          *
1667                          * 1) If there are not enough free entries in the ring
1668                          * to hold each mbuf in the chain then compact the
1669                          * chain into a single cluster.
1670                          *
1671                          * 2) The Am7990 and Am79C90 must not have less than
1672                          * 100 bytes in the first descriptor of a chained
1673                          * packet so it's necessary to shuffle the mbuf
1674                          * contents to ensure this.
1675                          */
1676
1677
1678                         if (no_entries_needed > (NDESC(sc->ntdre) - sc->pending_transmits)) {
1679                                 if (!(head = chain_to_cluster(head))) {
1680                                         log(LOG_ERR, "lnc%d: Couldn't get mbuf for transmit packet -- Resetting \n ",ifp->if_unit);
1681                                         lnc_reset(sc);
1682                                         return;
1683                                 }
1684                         } else if ((sc->nic.ic == LANCE) || (sc->nic.ic == C_LANCE)) {
1685                                 if ((head->m_len < 100) && (head->m_next)) {
1686                                         len = 100 - head->m_len;
1687                                         if (M_TRAILINGSPACE(head) < len) {
1688                                                 /*
1689                                                  * Move data to start of data
1690                                                  * area. We assume the first
1691                                                  * mbuf has a packet header
1692                                                  * and is not a cluster.
1693                                                  */
1694                                                 bcopy((caddr_t)head->m_data, (caddr_t)head->m_pktdat, head->m_len);
1695                                                 head->m_data = head->m_pktdat;
1696                                         }
1697                                         m = head->m_next;
1698                                         while (m && (len > 0)) {
1699                                                 chunk = min(len, m->m_len);
1700                                                 bcopy(mtod(m, caddr_t), mtod(head, caddr_t) + head->m_len, chunk);
1701                                                 len -= chunk;
1702                                                 head->m_len += chunk;
1703                                                 m->m_len -= chunk;
1704                                                 m->m_data += chunk;
1705                                                 if (m->m_len <= 0) {
1706                                                         m = m_free(m);
1707                                                         head->m_next = m;
1708                                                 }
1709                                         }
1710                                 }
1711                         }
1712
1713                         tmp = sc->next_to_send;
1714
1715                         /*
1716                          * On entering this loop we know that tmp points to a
1717                          * descriptor with a clear OWN bit.
1718                          */
1719
1720                         desc = sc->trans_ring + tmp;
1721                         len = ETHER_MIN_LEN;
1722                         for (m = head; m; m = m->m_next) {
1723                                 desc->buff.mbuf = m;
1724                                 addr = kvtop(m->m_data);
1725                                 desc->md->md0 = addr;
1726                                 desc->md->md1 = ((addr >> 16) & 0xff);
1727                                 desc->md->md3 = 0;
1728                                 desc->md->md2 = -m->m_len;
1729                                 sc->pending_transmits++;
1730                                 len -= m->m_len;
1731
1732                                 INC_MD_PTR(tmp, sc->ntdre)
1733                                 desc = sc->trans_ring + tmp;
1734                         }
1735
1736                         end_of_packet = tmp;
1737                         DEC_MD_PTR(tmp, sc->ntdre)
1738                         desc = sc->trans_ring + tmp;
1739                         desc->md->md1 |= ENP;
1740
1741                         if (len > 0)
1742                                 desc->md->md2 -= len;
1743
1744                         /*
1745                          * Set OWN bits in reverse order, otherwise the Lance
1746                          * could start sending the packet before all the
1747                          * buffers have been relinquished by the host.
1748                          */
1749
1750                         while (tmp != sc->next_to_send) {
1751                                 desc->md->md1 |= OWN;
1752                                 DEC_MD_PTR(tmp, sc->ntdre)
1753                                 desc = sc->trans_ring + tmp;
1754                         }
1755                         sc->next_to_send = end_of_packet;
1756                         desc->md->md1 |= STP | OWN;
1757                 } else {
1758                         sc->pending_transmits++;
1759                         desc = sc->trans_ring + sc->next_to_send;
1760                         len = mbuf_to_buffer(head, desc->buff.data);
1761                         desc->md->md3 = 0;
1762                         desc->md->md2 = -max(len, ETHER_MIN_LEN - ETHER_CRC_LEN);
1763                         desc->md->md1 |= OWN | STP | ENP;
1764                         INC_MD_PTR(sc->next_to_send, sc->ntdre)
1765                 }
1766
1767                 /* Force an immediate poll of the transmit ring */
1768                 outw(sc->rdp, TDMD | INEA);
1769
1770                 /*
1771                  * Set a timer so if the buggy Am7990.h shuts
1772                  * down we can wake it up.
1773                  */
1774
1775                 ifp->if_timer = 2;
1776
1777                 if (sc->arpcom.ac_if.if_bpf)
1778                         bpf_mtap(&sc->arpcom.ac_if, head);
1779
1780                 if (sc->nic.mem_mode != DMA_MBUF)
1781                         m_freem(head);
1782
1783         } while (sc->pending_transmits < NDESC(sc->ntdre));
1784
1785         /*
1786          * Transmit ring is full so set IFF_OACTIVE
1787          * since we can't buffer any more packets.
1788          */
1789
1790         sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
1791         LNCSTATS(trans_ring_full)
1792 }
1793
1794 static int
1795 lnc_ioctl(struct ifnet * ifp, u_long command, caddr_t data)
1796 {
1797
1798         struct lnc_softc *sc = ifp->if_softc;
1799         int s, error = 0;
1800
1801         s = splimp();
1802
1803         switch (command) {
1804         case SIOCSIFADDR:
1805         case SIOCGIFADDR:
1806         case SIOCSIFMTU:
1807                 error = ether_ioctl(ifp, command, data);
1808                 break;
1809
1810         case SIOCSIFFLAGS:
1811 #ifdef DEBUG
1812                 if (ifp->if_flags & IFF_DEBUG)
1813                         sc->lnc_debug = 1;
1814                 else
1815                         sc->lnc_debug = 0;
1816 #endif
1817                 if (ifp->if_flags & IFF_PROMISC) {
1818                         if (!(sc->nic.mode & PROM)) {
1819                                 sc->nic.mode |= PROM;
1820                                 lnc_init(sc);
1821                         }
1822                 } else if (sc->nic.mode & PROM) {
1823                         sc->nic.mode &= ~PROM;
1824                         lnc_init(sc);
1825                 }
1826
1827                 if ((ifp->if_flags & IFF_ALLMULTI) &&
1828                     !(sc->flags & LNC_ALLMULTI)) {
1829                         sc->flags |= LNC_ALLMULTI;
1830                         lnc_init(sc);
1831                 } else if (!(ifp->if_flags & IFF_ALLMULTI) &&
1832                             (sc->flags & LNC_ALLMULTI)) {
1833                         sc->flags &= ~LNC_ALLMULTI;
1834                         lnc_init(sc);
1835                 }
1836
1837                 if ((ifp->if_flags & IFF_UP) == 0 &&
1838                     (ifp->if_flags & IFF_RUNNING) != 0) {
1839                         /*
1840                          * If interface is marked down and it is running,
1841                          * then stop it.
1842                          */
1843                         lnc_stop(sc);
1844                         ifp->if_flags &= ~IFF_RUNNING;
1845                 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1846                            (ifp->if_flags & IFF_RUNNING) == 0) {
1847                         /*
1848                          * If interface is marked up and it is stopped, then
1849                          * start it.
1850                          */
1851                         lnc_init(sc);
1852                 }
1853                 break;
1854         case SIOCADDMULTI:
1855         case SIOCDELMULTI:
1856                 lnc_init(sc);
1857                 error = 0;
1858                 break;
1859         default:
1860                 error = EINVAL;
1861         }
1862         (void) splx(s);
1863         return error;
1864 }
1865
1866 static void
1867 lnc_watchdog(struct ifnet *ifp)
1868 {
1869         log(LOG_ERR, "lnc%d: Device timeout -- Resetting\n", ifp->if_unit);
1870         ifp->if_oerrors++;
1871         lnc_reset(ifp->if_softc);
1872 }
1873
1874 #ifdef DEBUG
1875 void
1876 lnc_dump_state(struct lnc_softc *sc)
1877 {
1878         int             i;
1879
1880         printf("\nDriver/NIC [%d] state dump\n", sc->arpcom.ac_if.if_unit);
1881         printf("Memory access mode: %b\n", sc->nic.mem_mode, MEM_MODES);
1882         printf("Host memory\n");
1883         printf("-----------\n");
1884
1885         printf("Receive ring: base = %p, next = %p\n",
1886             (void *)sc->recv_ring, (void *)(sc->recv_ring + sc->recv_next));
1887         for (i = 0; i < NDESC(sc->nrdre); i++)
1888                 printf("\t%d:%p md = %p buff = %p\n",
1889                     i, (void *)(sc->recv_ring + i),
1890                     (void *)(sc->recv_ring + i)->md,
1891                     (void *)(sc->recv_ring + i)->buff.data);
1892
1893         printf("Transmit ring: base = %p, next = %p\n",
1894             (void *)sc->trans_ring, (void *)(sc->trans_ring + sc->trans_next));
1895         for (i = 0; i < NDESC(sc->ntdre); i++)
1896                 printf("\t%d:%p md = %p buff = %p\n",
1897                     i, (void *)(sc->trans_ring + i),
1898                     (void *)(sc->trans_ring + i)->md,
1899                     (void *)(sc->trans_ring + i)->buff.data);
1900         printf("Lance memory (may be on host(DMA) or card(SHMEM))\n");
1901         printf("Init block = %p\n", (void *)sc->init_block);
1902         printf("\tmode = %b rlen:rdra = %x:%x tlen:tdra = %x:%x\n",
1903             sc->init_block->mode, INIT_MODE, sc->init_block->rlen,
1904           sc->init_block->rdra, sc->init_block->tlen, sc->init_block->tdra);
1905         printf("Receive descriptor ring\n");
1906         for (i = 0; i < NDESC(sc->nrdre); i++)
1907                 printf("\t%d buffer = 0x%x%x, BCNT = %d,\tMCNT = %u,\tflags = %b\n",
1908                     i, ((sc->recv_ring + i)->md->md1 & HADR),
1909                     (sc->recv_ring + i)->md->md0,
1910                     -(short) (sc->recv_ring + i)->md->md2,
1911                     (sc->recv_ring + i)->md->md3,
1912                     (((sc->recv_ring + i)->md->md1 & ~HADR) >> 8), RECV_MD1);
1913         printf("Transmit descriptor ring\n");
1914         for (i = 0; i < NDESC(sc->ntdre); i++)
1915                 printf("\t%d buffer = 0x%x%x, BCNT = %d,\tflags = %b %b\n",
1916                     i, ((sc->trans_ring + i)->md->md1 & HADR),
1917                     (sc->trans_ring + i)->md->md0,
1918                     -(short) (sc->trans_ring + i)->md->md2,
1919                     ((sc->trans_ring + i)->md->md1 >> 8), TRANS_MD1,
1920                     ((sc->trans_ring + i)->md->md3 >> 10), TRANS_MD3);
1921         printf("\nnext_to_send = %x\n", sc->next_to_send);
1922         printf("\n CSR0 = %b CSR1 = %x CSR2 = %x CSR3 = %x\n\n",
1923             read_csr(sc, CSR0), CSR0_FLAGS, read_csr(sc, CSR1),
1924             read_csr(sc, CSR2), read_csr(sc, CSR3));
1925
1926         /* Set RAP back to CSR0 */
1927         outw(sc->rap, CSR0);
1928 }
1929
1930 void
1931 mbuf_dump_chain(struct mbuf * m)
1932 {
1933
1934 #define MBUF_FLAGS \
1935         "\20\1M_EXT\2M_PKTHDR\3M_EOR\4UNKNOWN\5M_BCAST\6M_MCAST"
1936
1937         if (!m)
1938                 log(LOG_DEBUG, "m == NULL\n");
1939         do {
1940                 log(LOG_DEBUG, "m = %p\n", (void *)m);
1941                 log(LOG_DEBUG, "m_hdr.mh_next = %p\n",
1942                     (void *)m->m_hdr.mh_next);
1943                 log(LOG_DEBUG, "m_hdr.mh_nextpkt = %p\n",
1944                     (void *)m->m_hdr.mh_nextpkt);
1945                 log(LOG_DEBUG, "m_hdr.mh_len = %d\n", m->m_hdr.mh_len);
1946                 log(LOG_DEBUG, "m_hdr.mh_data = %p\n",
1947                     (void *)m->m_hdr.mh_data);
1948                 log(LOG_DEBUG, "m_hdr.mh_type = %d\n", m->m_hdr.mh_type);
1949                 log(LOG_DEBUG, "m_hdr.mh_flags = %b\n", m->m_hdr.mh_flags,
1950                     MBUF_FLAGS);
1951                 if (!(m->m_hdr.mh_flags & (M_PKTHDR | M_EXT)))
1952                         log(LOG_DEBUG, "M_dat.M_databuf = %p\n",
1953                             (void *)m->M_dat.M_databuf);
1954                 else {
1955                         if (m->m_hdr.mh_flags & M_PKTHDR) {
1956                                 log(LOG_DEBUG, "M_dat.MH.MH_pkthdr.len = %d\n",
1957                                     m->M_dat.MH.MH_pkthdr.len);
1958                                 log(LOG_DEBUG,
1959                                     "M_dat.MH.MH_pkthdr.rcvif = %p\n",
1960                                     (void *)m->M_dat.MH.MH_pkthdr.rcvif);
1961                                 if (!(m->m_hdr.mh_flags & M_EXT))
1962                                         log(LOG_DEBUG,
1963                                             "M_dat.MH.MH_dat.MH_databuf = %p\n",
1964                                         (void *)m->M_dat.MH.MH_dat.MH_databuf);
1965                         }
1966                         if (m->m_hdr.mh_flags & M_EXT) {
1967                                 log(LOG_DEBUG,
1968                                     "M_dat.MH.MH_dat.MH_ext.ext_buff %p\n",
1969                                     (void *)m->M_dat.MH.MH_dat.MH_ext.ext_buf);
1970                                 log(LOG_DEBUG,
1971                                     "M_dat.MH.MH_dat.MH_ext.ext_free %p\n",
1972                                     (void *)m->M_dat.MH.MH_dat.MH_ext.ext_free);
1973                                 log(LOG_DEBUG,
1974                                     "M_dat.MH.MH_dat.MH_ext.ext_size %d\n",
1975                                     m->M_dat.MH.MH_dat.MH_ext.ext_size);
1976                         }
1977                 }
1978         } while ((m = m->m_next) != NULL);
1979 }
1980 #endif