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