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