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