if_de.c revision 7104
1/*-
2 * Copyright (c) 1994 Matt Thomas (thomas@lkg.dec.com)
3 * 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 * 2. The name of the author may not be used to endorse or promote products
11 *    derived from this software withough specific prior written permission
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * $Id: if_de.c,v 1.17 1995/03/16 17:41:20 se Exp $
25 *
26 */
27
28/*
29 * DEC DC21040 PCI Ethernet Controller
30 *
31 * Written by Matt Thomas
32 * BPF support code stolen directly from if_ec.c
33 *
34 *   This driver supports the DEC DE435 or any other PCI
35 *   board which support DC21040.
36 */
37
38#define IF_DE_C_PATCHLEVEL  "pl1 95/03/09"
39#include "de.h"
40#if NDE > 0
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/mbuf.h>
45#include <sys/protosw.h>
46#include <sys/socket.h>
47#include <sys/ioctl.h>
48#include <sys/errno.h>
49#include <sys/malloc.h>
50#include <sys/kernel.h>
51#include <sys/proc.h>	/* only for declaration of wakeup() used by vm.h */
52#include <sys/devconf.h>
53#include <machine/clock.h>
54
55#include <net/if.h>
56#include <net/if_types.h>
57#include <net/if_dl.h>
58#include <net/route.h>
59
60#include "bpfilter.h"
61#if NBPFILTER > 0
62#include <net/bpf.h>
63#include <net/bpfdesc.h>
64#endif
65
66#ifdef INET
67#include <netinet/in.h>
68#include <netinet/in_systm.h>
69#include <netinet/in_var.h>
70#include <netinet/ip.h>
71#include <netinet/if_ether.h>
72#endif
73
74#ifdef NS
75#include <netns/ns.h>
76#include <netns/ns_if.h>
77#endif
78
79#include <vm/vm.h>
80#include <vm/vm_kern.h>
81#include <vm/vm_param.h>
82
83
84#include <pci.h>
85#if NPCI > 0
86#include <pci/pcivar.h>
87#endif
88
89#include <pci/dc21040.h>
90
91/*
92 * This module supports the DEC DC21040 PCI Ethernet Controller.
93 */
94
95typedef struct {
96    unsigned long addr;
97    unsigned long length;
98} tulip_addrvec_t;
99
100typedef struct {
101    tulip_desc_t *ri_first;
102    tulip_desc_t *ri_last;
103    tulip_desc_t *ri_nextin;
104    tulip_desc_t *ri_nextout;
105    int ri_max;
106    int ri_free;
107} tulip_ringinfo_t;
108
109typedef struct {
110    volatile tulip_uint32_t *csr_busmode;		/* CSR0 */
111    volatile tulip_uint32_t *csr_txpoll;		/* CSR1 */
112    volatile tulip_uint32_t *csr_rxpoll;		/* CSR2 */
113    volatile tulip_uint32_t *csr_rxlist;		/* CSR3 */
114    volatile tulip_uint32_t *csr_txlist;		/* CSR4 */
115    volatile tulip_uint32_t *csr_status;		/* CSR5 */
116    volatile tulip_uint32_t *csr_command;		/* CSR6 */
117    volatile tulip_uint32_t *csr_intr;			/* CSR7 */
118    volatile tulip_uint32_t *csr_missed_frame;		/* CSR8 */
119    volatile tulip_sint32_t *csr_enetrom;		/* CSR9 */
120    volatile tulip_uint32_t *csr_reserved;		/* CSR10 */
121    volatile tulip_uint32_t *csr_full_duplex;		/* CSR11 */
122    volatile tulip_uint32_t *csr_sia_status;		/* CSR12 */
123    volatile tulip_uint32_t *csr_sia_connectivity;	/* CSR13 */
124    volatile tulip_uint32_t *csr_sia_tx_rx;		/* CSR14 */
125    volatile tulip_uint32_t *csr_sia_general;		/* CSR15 */
126} tulip_regfile_t;
127
128/*
129 * The DC21040 has a stupid restriction in that the receive
130 * buffers must be longword aligned.  But since Ethernet
131 * headers are not a multiple of longwords in size this forces
132 * the data to non-longword aligned.  Since IP requires the
133 * data to be longword aligned, we need to copy it after it has
134 * been DMA'ed in our memory.
135 *
136 * Since we have to copy it anyways, we might as well as allocate
137 * dedicated receive space for the input.  This allows to use a
138 * small receive buffer size and more ring entries to be able to
139 * better keep with a flood of tiny Ethernet packets.
140 *
141 * The receive space MUST ALWAYS be a multiple of the page size.
142 * And the number of receive descriptors multiplied by the size
143 * of the receive buffers must equal the recevive space.  This
144 * is so that we can manipulate the page tables so that even if a
145 * packet wraps around the end of the receive space, we can
146 * treat it as virtually contiguous.
147 */
148#define	TULIP_RXBUFSIZE		512
149#define	TULIP_RXDESCS		128
150#define	TULIP_RXSPACE		(TULIP_RXBUFSIZE * TULIP_RXDESCS)
151#define	TULIP_TXDESCS		128
152
153typedef struct {
154    struct arpcom tulip_ac;
155    tulip_regfile_t tulip_csrs;
156    vm_offset_t tulip_rxspace;
157    unsigned tulip_flags;
158#define	TULIP_WANTSETUP		0x01
159#define	TULIP_WANTHASH		0x02
160#define	TULIP_DOINGSETUP	0x04
161#define	TULIP_ALTPHYS		0x08	/* use AUI */
162    unsigned char tulip_rombuf[32];
163    tulip_uint32_t tulip_setupbuf[192/sizeof(tulip_uint32_t)];
164    tulip_uint32_t tulip_setupdata[192/sizeof(tulip_uint32_t)];
165    tulip_uint32_t tulip_intrmask;
166    tulip_uint32_t tulip_cmdmode;
167    tulip_uint32_t tulip_revinfo;
168#if NBPFILTER > 0
169    caddr_t tulip_bpf;			/* BPF context */
170#endif
171    struct ifqueue tulip_txq;
172    tulip_ringinfo_t tulip_rxinfo;
173    tulip_ringinfo_t tulip_txinfo;
174} tulip_softc_t;
175
176#ifndef IFF_ALTPHYS
177#define	IFF_ALTPHYS	IFF_LINK0		/* In case it isn't defined */
178#endif
179typedef enum { TULIP_DC21040, TULIP_DC21140 } tulip_chipid_t;
180const char *tulip_chipdescs[] = {
181    "DC21040 [10Mb/s]",
182    "DC21140 [100Mb/s]",
183};
184
185tulip_softc_t *tulips[NDE];
186tulip_chipid_t tulip_chipids[NDE];
187
188#define	tulip_if	tulip_ac.ac_if
189#define	tulip_unit	tulip_ac.ac_if.if_unit
190#define	tulip_name	tulip_ac.ac_if.if_name
191#define	tulip_hwaddr	tulip_ac.ac_enaddr
192
193#define	TULIP_CRC32_POLY	0xEDB88320UL	/* CRC-32 Poly -- Little Endian */
194#define	TULIP_CHECK_RXCRC	0
195#define	TULIP_MAX_TXSEG		30
196
197#define	TULIP_ADDREQUAL(a1, a2) \
198	(((u_short *)a1)[0] == ((u_short *)a2)[0] \
199	 && ((u_short *)a1)[1] == ((u_short *)a2)[1] \
200	 && ((u_short *)a1)[2] == ((u_short *)a2)[2])
201#define	TULIP_ADDRBRDCST(a1) \
202	(((u_short *)a1)[0] == 0xFFFFU \
203	 && ((u_short *)a1)[1] == 0xFFFFU \
204	 && ((u_short *)a1)[2] == 0xFFFFU)
205
206static void tulip_start(struct ifnet *ifp);
207static void tulip_addr_filter(tulip_softc_t *sc);
208
209#if __FreeBSD__ > 1
210#define	TULIP_IFRESET_ARGS	int unit
211#define	TULIP_RESET(sc)		tulip_reset((sc)->tulip_unit)
212#else
213#define	TULIP_IFRESET_ARGS	int unit, int uban
214#define	TULIP_RESET(sc)		tulip_reset((sc)->tulip_unit, 0)
215#endif
216
217static void
218tulip_reset(
219    TULIP_IFRESET_ARGS)
220{
221    tulip_softc_t *sc = tulips[unit];
222    tulip_ringinfo_t *ri;
223    tulip_desc_t *di;
224    vm_offset_t vmoff;
225
226    *sc->tulip_csrs.csr_busmode = TULIP_BUSMODE_SWRESET;
227    DELAY(10);	/* Wait 10 microsends (actually 50 PCI cycles but at
228		   33MHz that comes to two microseconds but wait a
229		   bit longer anyways) */
230
231    /*
232     * Use the
233     */
234    *sc->tulip_csrs.csr_sia_connectivity = TULIP_SIACONN_RESET;
235    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
236	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
237	    printf("%s%d: enabling Thinwire/AUI port\n",
238		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
239	*sc->tulip_csrs.csr_sia_connectivity = TULIP_SIACONN_AUI;
240	sc->tulip_flags |= TULIP_ALTPHYS;
241    } else {
242	if (sc->tulip_flags & TULIP_ALTPHYS)
243	    printf("%s%d: enabling 10baseT/UTP port\n",
244		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
245	*sc->tulip_csrs.csr_sia_connectivity = TULIP_SIACONN_10BASET;
246	sc->tulip_flags &= ~TULIP_ALTPHYS;
247    }
248    *sc->tulip_csrs.csr_txlist = vtophys(&sc->tulip_txinfo.ri_first[0]);
249    *sc->tulip_csrs.csr_rxlist = vtophys(&sc->tulip_rxinfo.ri_first[0]);
250    *sc->tulip_csrs.csr_intr = 0;
251    *sc->tulip_csrs.csr_busmode = 0x4800;
252
253    sc->tulip_txq.ifq_maxlen = TULIP_TXDESCS;
254    /*
255     * Free all the mbufs that were on the transmit ring.
256     */
257    for (;;) {
258	struct mbuf *m;
259	IF_DEQUEUE(&sc->tulip_txq, m);
260	if (m == NULL)
261	    break;
262	m_freem(m);
263    }
264
265    ri = &sc->tulip_txinfo;
266    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
267    ri->ri_free = ri->ri_max;
268    for (di = ri->ri_first; di < ri->ri_last; di++)
269	di->d_status = 0;
270
271    /*
272     * We need to collect all the mbufs were on the
273     * receive ring before we reinit it either to put
274     * them back on or to know if we have to allocate
275     * more.
276     */
277    ri = &sc->tulip_rxinfo;
278    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
279    ri->ri_free = ri->ri_max;
280    for (vmoff = vtophys(sc->tulip_rxspace), di = ri->ri_first;
281	    di < ri->ri_last; di++, vmoff += TULIP_RXBUFSIZE) {
282	di->d_status |= TULIP_DSTS_OWNER;
283	di->d_length1 = TULIP_RXBUFSIZE; di->d_addr1 = vmoff;
284	di->d_length2 = 0; di->d_addr2 = 0;
285    }
286
287    sc->tulip_intrmask = TULIP_STS_NORMALINTR|TULIP_STS_RXINTR|TULIP_STS_TXINTR
288	|TULIP_STS_ABNRMLINTR|TULIP_STS_SYSERROR|TULIP_STS_TXSTOPPED
289	    |TULIP_STS_TXBABBLE|TULIP_STS_LINKFAIL|TULIP_STS_RXSTOPPED;
290    sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_WANTSETUP);
291    tulip_addr_filter(sc);
292}
293
294static void
295tulip_init(
296    int unit)
297{
298    tulip_softc_t *sc = tulips[unit];
299
300    if (sc->tulip_if.if_flags & IFF_UP) {
301	sc->tulip_if.if_flags |= IFF_RUNNING;
302	if (sc->tulip_if.if_flags & IFF_PROMISC) {
303	    sc->tulip_cmdmode |= TULIP_CMD_PROMISCUOUS;
304	} else {
305	    sc->tulip_cmdmode &= ~TULIP_CMD_PROMISCUOUS;
306	    if (sc->tulip_if.if_flags & IFF_ALLMULTI) {
307		sc->tulip_cmdmode |= TULIP_CMD_ALLMULTI;
308	    } else {
309		sc->tulip_cmdmode &= ~TULIP_CMD_ALLMULTI;
310	    }
311	}
312	sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
313	if ((sc->tulip_flags & TULIP_WANTSETUP) == 0) {
314	    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
315	    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
316	} else {
317	    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
318	    tulip_start(&sc->tulip_if);
319	}
320	sc->tulip_cmdmode |= TULIP_CMD_THRSHLD160;
321	sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT;
322	*sc->tulip_csrs.csr_intr = sc->tulip_intrmask;
323	*sc->tulip_csrs.csr_command = sc->tulip_cmdmode;
324    } else {
325	TULIP_RESET(sc);
326	sc->tulip_if.if_flags &= ~IFF_RUNNING;
327    }
328}
329
330
331#if TULIP_CHECK_RXCRC
332static unsigned
333tulip_crc32(
334    u_char *addr,
335    int len)
336{
337    unsigned int crc = 0xFFFFFFFF;
338    static unsigned int crctbl[256];
339    int idx;
340    static int done;
341    /*
342     * initialize the multicast address CRC table
343     */
344    for (idx = 0; !done && idx < 256; idx++) {
345	unsigned int tmp = idx;
346	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
347	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
348	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
349	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
350	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
351	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
352	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
353	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
354	crctbl[idx] = tmp;
355    }
356    done = 1;
357
358    while (len-- > 0)
359	crc = (crc >> 8) ^ crctbl[*addr++] ^ crctbl[crc & 0xFF];
360
361    return crc;
362}
363#endif
364
365static void
366tulip_rx_intr(
367    tulip_softc_t *sc)
368{
369    tulip_ringinfo_t *ri = &sc->tulip_rxinfo;
370    struct ifnet *ifp = &sc->tulip_if;
371
372    for (;;) {
373	tulip_desc_t *eop;
374	int total_len, ndescs;
375	caddr_t bufaddr = (caddr_t) sc->tulip_rxspace;
376
377	for (ndescs = 1, eop = ri->ri_nextin;; ndescs++) {
378	    if (((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER)
379		return;
380
381	    if (eop->d_status & TULIP_DSTS_RxLASTDESC)
382		break;
383	    if (++eop == ri->ri_last)
384		eop = ri->ri_first;
385	}
386
387	bufaddr += TULIP_RXBUFSIZE * (ri->ri_nextin - ri->ri_first);
388	total_len = ((eop->d_status >> 16) & 0x7FF) - 4;
389
390	if ((eop->d_status & TULIP_DSTS_ERRSUM) == 0) {
391	    struct ether_header eh;
392	    struct mbuf *m;
393
394#if TULIP_CHECK_RXCRC
395	    unsigned crc = tulip_crc32(bufaddr, total_len);
396	    if (~crc != *((unsigned *) &bufaddr[total_len])) {
397		printf("de0: bad rx crc: %08x [rx] != %08x\n",
398		       *((unsigned *) &bufaddr[total_len]), ~crc);
399		goto next;
400	    }
401#endif
402	    eh = *(struct ether_header *) bufaddr;
403#if NBPFILTER > 0
404	    if (sc->tulip_bpf != NULL) {
405		bpf_tap(sc->tulip_bpf, bufaddr, total_len);
406		if ((eh.ether_dhost[0] & 1) == 0 &&
407		    !TULIP_ADDREQUAL(eh.ether_dhost, sc->tulip_ac.ac_enaddr))
408		    goto next;
409	    } else if (!TULIP_ADDREQUAL(eh.ether_dhost, sc->tulip_ac.ac_enaddr)
410		    && !TULIP_ADDRBRDCST(eh.ether_dhost)) {
411		    goto next;
412	    }
413#endif
414	    MGETHDR(m, M_DONTWAIT, MT_DATA);
415	    if (m != NULL) {
416		m->m_pkthdr.rcvif = ifp;
417		total_len -= sizeof(eh);
418		if (total_len > MHLEN) {
419		    MCLGET(m, M_DONTWAIT);
420		    if ((m->m_flags & M_EXT) == 0) {
421			m_freem(m);
422			ifp->if_ierrors++;
423			goto next;
424		    }
425		}
426		bcopy(bufaddr + sizeof(eh), mtod(m, caddr_t), total_len);
427		m->m_len = m->m_pkthdr.len = total_len;
428		ether_input(ifp, &eh, m);
429	    } else {
430		ifp->if_ierrors++;
431	    }
432	} else {
433	    ifp->if_ierrors++;
434	}
435next:
436	ifp->if_ipackets++;
437	while (ndescs-- > 0) {
438	    ri->ri_nextin->d_status |= TULIP_DSTS_OWNER;
439	    if (++ri->ri_nextin == ri->ri_last)
440		ri->ri_nextin = ri->ri_first;
441	}
442    }
443}
444
445static int
446tulip_tx_intr(
447    tulip_softc_t *sc)
448{
449    tulip_ringinfo_t *ri = &sc->tulip_txinfo;
450    struct mbuf *m;
451    int xmits = 0;
452
453    while (ri->ri_free < ri->ri_max) {
454	if (((volatile tulip_desc_t *) ri->ri_nextin)->d_status & TULIP_DSTS_OWNER)
455	    break;
456
457	if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxLASTSEG) {
458	    if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxSETUPPKT) {
459		/*
460		 * We've just finished processing a setup packet.
461		 * Mark that we can finished it.  If there's not
462		 * another pending, startup the TULIP receiver.
463		 * Make sure we ack the RXSTOPPED so we won't get
464		 * an abormal interrupt indication.
465		 */
466		sc->tulip_flags &= ~TULIP_DOINGSETUP;
467		if ((sc->tulip_flags & TULIP_WANTSETUP) == 0) {
468		    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
469		    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
470		    *sc->tulip_csrs.csr_status = TULIP_STS_RXSTOPPED;
471		    *sc->tulip_csrs.csr_command = sc->tulip_cmdmode;
472		    *sc->tulip_csrs.csr_intr = sc->tulip_intrmask;
473		}
474	   } else {
475		IF_DEQUEUE(&sc->tulip_txq, m);
476		m_freem(m);
477		sc->tulip_if.if_collisions +=
478		    (ri->ri_nextin->d_status & TULIP_DSTS_TxCOLLMASK)
479			>> TULIP_DSTS_V_TxCOLLCNT;
480		if (ri->ri_nextin->d_status & TULIP_DSTS_ERRSUM)
481		    sc->tulip_if.if_oerrors++;
482		xmits++;
483	    }
484	}
485
486	if (++ri->ri_nextin == ri->ri_last)
487	    ri->ri_nextin = ri->ri_first;
488	ri->ri_free++;
489	sc->tulip_if.if_flags &= ~IFF_OACTIVE;
490    }
491    sc->tulip_if.if_opackets += xmits;
492    return xmits;
493}
494
495static int
496tulip_txsegment(
497    tulip_softc_t *sc,
498    struct mbuf *m,
499    tulip_addrvec_t *avp,
500    size_t maxseg)
501{
502    int segcnt;
503
504    for (segcnt = 0; m; m = m->m_next) {
505	int len = m->m_len;
506	caddr_t addr = mtod(m, caddr_t);
507	unsigned clsize = CLBYTES - (((u_long) addr) & (CLBYTES-1));
508
509	while (len > 0) {
510	    unsigned slen = min(len, clsize);
511	    if (segcnt < maxseg) {
512		avp->addr = vtophys(addr);
513		avp->length = slen;
514	    }
515	    len -= slen;
516	    addr += slen;
517	    clsize = CLBYTES;
518	    avp++;
519	    segcnt++;
520	}
521    }
522    if (segcnt >= maxseg) {
523	printf("%s%d: tulip_txsegment: extremely fragmented packet encountered (%d segments)\n",
524	       sc->tulip_name, sc->tulip_unit, segcnt);
525	return -1;
526    }
527    avp->addr = 0;
528    avp->length = 0;
529    return segcnt;
530}
531
532static void
533tulip_start(
534    struct ifnet *ifp)
535{
536    tulip_softc_t *sc = (tulip_softc_t *) ifp;
537    struct ifqueue *ifq = &ifp->if_snd;
538    tulip_ringinfo_t *ri = &sc->tulip_txinfo;
539    tulip_desc_t *sop, *eop;
540    struct mbuf *m;
541    tulip_addrvec_t addrvec[TULIP_MAX_TXSEG+1], *avp;
542    int segcnt;
543    tulip_uint32_t d_status;
544
545    if ((ifp->if_flags & IFF_RUNNING) == 0)
546	return;
547
548    for (;;) {
549	if (sc->tulip_flags & TULIP_WANTSETUP) {
550	    if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) {
551		ifp->if_flags |= IFF_OACTIVE;
552		return;
553	    }
554	    bcopy(sc->tulip_setupdata, sc->tulip_setupbuf,
555		   sizeof(sc->tulip_setupbuf));
556	    sc->tulip_flags &= ~TULIP_WANTSETUP;
557	    sc->tulip_flags |= TULIP_DOINGSETUP;
558	    ri->ri_free--;
559	    ri->ri_nextout->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
560	    ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG
561		    |TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR;
562	    if (sc->tulip_flags & TULIP_WANTHASH)
563		ri->ri_nextout->d_flag |= TULIP_DFLAG_TxHASHFILT;
564	    ri->ri_nextout->d_length1 = sizeof(sc->tulip_setupbuf);
565	    ri->ri_nextout->d_addr1 = vtophys(sc->tulip_setupbuf);
566	    ri->ri_nextout->d_length2 = 0;
567	    ri->ri_nextout->d_addr2 = 0;
568	    ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
569	    *sc->tulip_csrs.csr_txpoll = 1;
570	    /*
571	     * Advance the ring for the next transmit packet.
572	     */
573	    if (++ri->ri_nextout == ri->ri_last)
574		ri->ri_nextout = ri->ri_first;
575	}
576
577	IF_DEQUEUE(ifq, m);
578	if (m == NULL)
579	    break;
580
581	/*
582	 * First find out how many and which different pages
583	 * the mbuf data occupies.  Then check to see if we
584	 * have enough descriptor space in our transmit ring
585	 * to actually send it.
586	 */
587	segcnt = tulip_txsegment(sc, m, addrvec,
588				 min(ri->ri_max - 1, TULIP_MAX_TXSEG));
589	if (segcnt < 0) {
590	    struct mbuf *m0;
591	    MGETHDR(m0, M_DONTWAIT, MT_DATA);
592	    if (m0 != NULL) {
593		if (m->m_pkthdr.len > MHLEN) {
594		    MCLGET(m0, M_DONTWAIT);
595		    if ((m0->m_flags & M_EXT) == 0) {
596			m_freem(m);
597			continue;
598		    }
599		}
600		m_copydata(m, 0, m0->m_pkthdr.len, mtod(m0, caddr_t));
601		m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len;
602		m_freem(m);
603		IF_PREPEND(ifq, m0);
604		continue;
605	    } else {
606		m_freem(m);
607		continue;
608	    }
609	}
610	if (ri->ri_free - 2 <= (segcnt + 1) >> 1)
611	    break;
612
613	ri->ri_free -= (segcnt + 1) >> 1;
614	/*
615	 * Now we fill in our transmit descriptors.  This is
616	 * a bit reminiscent of going on the Ark two by two
617	 * since each descriptor for the TULIP can describe
618	 * two buffers.  So we advance through the address
619	 * vector two entries at a time to to fill each
620	 * descriptor.  Clear the first and last segment bits
621	 * in each descriptor (actually just clear everything
622	 * but the end-of-ring or chain bits) to make sure
623	 * we don't get messed up by previously sent packets.
624	 */
625	sop = ri->ri_nextout;
626	d_status = 0;
627	avp = addrvec;
628	do {
629	    eop = ri->ri_nextout;
630	    eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
631	    eop->d_status = d_status;
632	    eop->d_addr1 = avp->addr; eop->d_length1 = avp->length; avp++;
633	    eop->d_addr2 = avp->addr; eop->d_length2 = avp->length; avp++;
634	    d_status = TULIP_DSTS_OWNER;
635	    if (++ri->ri_nextout == ri->ri_last)
636		ri->ri_nextout = ri->ri_first;
637	} while ((segcnt -= 2) > 0);
638#if NBPFILTER > 0
639	    if (sc->tulip_bpf != NULL)
640		bpf_mtap(sc->tulip_bpf, m);
641#endif
642	/*
643	 * The descriptors have been filled in.  Mark the first
644	 * and last segments, indicate we want a transmit complete
645	 * interrupt, give the descriptors to the TULIP, and tell
646	 * it to transmit!
647	 */
648
649	IF_ENQUEUE(&sc->tulip_txq, m);
650	eop->d_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR;
651	sop->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
652	sop->d_status = TULIP_DSTS_OWNER;
653
654	*sc->tulip_csrs.csr_txpoll = 1;
655    }
656    if (m != NULL) {
657	ifp->if_flags |= IFF_OACTIVE;
658	IF_PREPEND(ifq, m);
659    }
660}
661
662static int
663tulip_intr(
664    tulip_softc_t *sc)
665{
666    tulip_uint32_t csr;
667    int active=0;
668
669    while ((csr = *sc->tulip_csrs.csr_status) & (TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR)) {
670	active=1;
671	*sc->tulip_csrs.csr_status = csr & sc->tulip_intrmask;
672
673	if (csr & TULIP_STS_SYSERROR) {
674	    if ((csr & TULIP_STS_ERRORMASK) == TULIP_STS_ERR_PARITY) {
675		TULIP_RESET(sc);
676		tulip_init(sc->tulip_unit);
677		return 1;
678	    }
679	}
680	if (csr & TULIP_STS_ABNRMLINTR) {
681	    printf("%s%d: abnormal interrupt: 0x%05x [0x%05x]\n",
682		   sc->tulip_name, sc->tulip_unit, csr, csr & sc->tulip_intrmask);
683	    *sc->tulip_csrs.csr_command = sc->tulip_cmdmode;
684	}
685	if (csr & TULIP_STS_RXINTR)
686	    tulip_rx_intr(sc);
687	if (sc->tulip_txinfo.ri_free < sc->tulip_txinfo.ri_max) {
688	    tulip_tx_intr(sc);
689	    tulip_start(&sc->tulip_if);
690	}
691    }
692    return (active);
693}
694
695/*
696 *  This is the standard method of reading the DEC Address ROMS.
697 */
698static int
699tulip_read_macaddr(
700    tulip_softc_t *sc)
701{
702    int cksum, rom_cksum, idx;
703    tulip_sint32_t csr;
704    unsigned char tmpbuf[8];
705    static u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA };
706
707    *sc->tulip_csrs.csr_enetrom = 1;
708    for (idx = 0; idx < 32; idx++) {
709	int cnt = 0;
710	while ((csr = *sc->tulip_csrs.csr_enetrom) < 0 && cnt < 10000)
711	    cnt++;
712	sc->tulip_rombuf[idx] = csr & 0xFF;
713    }
714
715    if (bcmp(&sc->tulip_rombuf[0], &sc->tulip_rombuf[16], 8) != 0) {
716	/*
717	 * Some folks don't use the standard ethernet rom format
718	 * but instead just put the address in the first 6 bytes
719	 * of the rom and let the rest be all 0xffs.  (Can we say
720	 * ZNYX???)
721	 */
722	for (idx = 6; idx < 32; idx++) {
723	    if (sc->tulip_rombuf[idx] != 0xFF)
724		return -4;
725	}
726	/*
727	 * Make sure the address is not multicast or locally assigned
728	 * that the OUI is not 00-00-00.
729	 */
730	if ((sc->tulip_rombuf[0] & 3) != 0)
731	    return -4;
732	if (sc->tulip_rombuf[0] == 0 && sc->tulip_rombuf[1] == 0
733		&& sc->tulip_rombuf[2] == 0)
734	    return -4;
735	bcopy(sc->tulip_rombuf, sc->tulip_hwaddr, 6);
736	return 0;
737    }
738    if (bcmp(&sc->tulip_rombuf[24], testpat, 8) != 0)
739	return -3;
740
741    tmpbuf[0] = sc->tulip_rombuf[15]; tmpbuf[1] = sc->tulip_rombuf[14];
742    tmpbuf[2] = sc->tulip_rombuf[13]; tmpbuf[3] = sc->tulip_rombuf[12];
743    tmpbuf[4] = sc->tulip_rombuf[11]; tmpbuf[5] = sc->tulip_rombuf[10];
744    tmpbuf[6] = sc->tulip_rombuf[9];  tmpbuf[7] = sc->tulip_rombuf[8];
745    if (bcmp(&sc->tulip_rombuf[0], tmpbuf, 8) != 0)
746	return -2;
747
748    bcopy(sc->tulip_rombuf, sc->tulip_hwaddr, 6);
749
750    cksum = *(u_short *) &sc->tulip_hwaddr[0];
751    cksum *= 2;
752    if (cksum > 65535) cksum -= 65535;
753    cksum += *(u_short *) &sc->tulip_hwaddr[2];
754    if (cksum > 65535) cksum -= 65535;
755    cksum *= 2;
756    if (cksum > 65535) cksum -= 65535;
757    cksum += *(u_short *) &sc->tulip_hwaddr[4];
758    if (cksum >= 65535) cksum -= 65535;
759
760    rom_cksum = *(u_short *) &sc->tulip_rombuf[6];
761
762    if (cksum != rom_cksum)
763	return -1;
764    return 0;
765}
766
767static unsigned
768tulip_mchash(
769    unsigned char *mca)
770{
771    u_int idx, bit, data, crc = 0xFFFFFFFFUL;
772
773#ifdef __alpha
774    for (data = *(__unaligned u_long *) mca, bit = 0; bit < 48; bit++, data >>=
7751)
776        crc = (crc >> 1) ^ (((crc ^ data) & 1) ? TULIP_CRC32_POLY : 0);
777#else
778    for (idx = 0; idx < 6; idx++)
779        for (data = *mca++, bit = 0; bit < 8; bit++, data >>= 1)
780            crc = (crc >> 1) ^ (((crc ^ data) & 1) ? TULIP_CRC32_POLY : 0);
781#endif
782    return crc & 0x1FF;
783}
784
785static void
786tulip_addr_filter(
787    tulip_softc_t *sc)
788{
789    tulip_uint32_t *sp = sc->tulip_setupdata;
790    struct ether_multistep step;
791    struct ether_multi *enm;
792    int i;
793
794    sc->tulip_flags &= ~TULIP_WANTHASH;
795    sc->tulip_flags |= TULIP_WANTSETUP;
796    sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
797    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
798    if (sc->tulip_ac.ac_multicnt > 14) {
799	unsigned hash;
800	/*
801	 * If we have more than 14 multicasts, we have
802	 * go into hash perfect mode (512 bit multicast
803	 * hash and one perfect hardware).
804	 */
805
806	bzero(sc->tulip_setupdata, sizeof(sc->tulip_setupdata));
807	hash = tulip_mchash(etherbroadcastaddr);
808	sp[hash >> 4] |= 1 << (hash & 0xF);
809	ETHER_FIRST_MULTI(step, &sc->tulip_ac, enm);
810	while (enm != NULL) {
811	    hash = tulip_mchash(enm->enm_addrlo);
812	    sp[hash >> 4] |= 1 << (hash & 0xF);
813	    ETHER_NEXT_MULTI(step, enm);
814	}
815	sc->tulip_cmdmode |= TULIP_WANTHASH;
816	sp[40] = ((u_short *) sc->tulip_ac.ac_enaddr)[0];
817	sp[41] = ((u_short *) sc->tulip_ac.ac_enaddr)[1];
818	sp[42] = ((u_short *) sc->tulip_ac.ac_enaddr)[2];
819    } else {
820	/*
821	 * Else can get perfect filtering for 16 addresses.
822	 */
823	i = 0;
824	ETHER_FIRST_MULTI(step, &sc->tulip_ac, enm);
825	for (; enm != NULL; i++) {
826	    *sp++ = ((u_short *) enm->enm_addrlo)[0];
827	    *sp++ = ((u_short *) enm->enm_addrlo)[1];
828	    *sp++ = ((u_short *) enm->enm_addrlo)[2];
829	    ETHER_NEXT_MULTI(step, enm);
830	}
831	/*
832	 * If an IP address is enabled, turn on broadcast
833	 */
834	if (sc->tulip_ac.ac_ipaddr.s_addr != 0) {
835	    i++;
836	    *sp++ = 0xFFFF;
837	    *sp++ = 0xFFFF;
838	    *sp++ = 0xFFFF;
839	}
840	/*
841	 * Pad the rest with our hardware address
842	 */
843	for (; i < 16; i++) {
844	    *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[0];
845	    *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[1];
846	    *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[2];
847	}
848    }
849}
850
851/*extern void arp_ifinit(struct arpcom *, struct ifaddr*);*/
852
853static int
854tulip_ioctl(
855    struct ifnet *ifp,
856    int cmd,
857    caddr_t data)
858{
859    tulip_softc_t *sc = tulips[ifp->if_unit];
860    struct ifaddr *ifa = (struct ifaddr *)data;
861    struct ifreq *ifr = (struct ifreq *) data;
862    int s, error = 0;
863
864    s = splimp();
865
866    switch (cmd) {
867	case SIOCSIFADDR: {
868
869	    ifp->if_flags |= IFF_UP;
870	    switch(ifa->ifa_addr->sa_family) {
871#ifdef INET
872		case AF_INET: {
873		    ((struct arpcom *)ifp)->ac_ipaddr = IA_SIN(ifa)->sin_addr;
874		    tulip_addr_filter(sc);	/* reset multicast filtering */
875		    (*ifp->if_init)(ifp->if_unit);
876		    arp_ifinit((struct arpcom *)ifp, ifa);
877		    break;
878		}
879#endif /* INET */
880
881#ifdef NS
882		/* This magic copied from if_is.c; I don't use XNS,
883		 * so I have no way of telling if this actually
884		 * works or not.
885		 */
886		case AF_NS: {
887		    struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
888		    if (ns_nullhost(*ina)) {
889			ina->x_host = *(union ns_host *)(sc->tulip_ac.ac_enaddr);
890		    } else {
891			ifp->if_flags &= ~IFF_RUNNING;
892			bcopy((caddr_t)ina->x_host.c_host,
893			      (caddr_t)sc->tulip_ac.ac_enaddr,
894			      sizeof sc->tulip_ac.ac_enaddr);
895		    }
896
897		    (*ifp->if_init)(ifp->if_unit);
898		    break;
899		}
900#endif /* NS */
901
902		default: {
903		    (*ifp->if_init)(ifp->if_unit);
904		    break;
905		}
906	    }
907	    break;
908	}
909
910	case SIOCSIFFLAGS: {
911	    /*
912	     * Changing the connection forces a reset.
913	     */
914	    if (sc->tulip_flags & TULIP_ALTPHYS) {
915		if ((ifp->if_flags & IFF_ALTPHYS) == 0)
916		    TULIP_RESET(sc);
917	    } else {
918		if (ifp->if_flags & IFF_ALTPHYS)
919		    TULIP_RESET(sc);
920	    }
921	    (*ifp->if_init)(ifp->if_unit);
922	    break;
923	}
924
925	case SIOCADDMULTI:
926	case SIOCDELMULTI: {
927	    /*
928	     * Update multicast listeners
929	     */
930	    if (cmd == SIOCADDMULTI)
931		error = ether_addmulti(ifr, &sc->tulip_ac);
932	    else
933		error = ether_delmulti(ifr, &sc->tulip_ac);
934
935	    if (error == ENETRESET) {
936		tulip_addr_filter(sc);		/* reset multicast filtering */
937		(*ifp->if_init)(ifp->if_unit);
938		error = 0;
939	    }
940	    break;
941	}
942	case SIOCSIFMTU:
943	    /*
944	     * Set the interface MTU.
945	     */
946	    if (ifr->ifr_mtu > ETHERMTU) {
947		error = EINVAL;
948	    } else {
949		ifp->if_mtu = ifr->ifr_mtu;
950	    }
951	    break;
952
953	default: {
954	    error = EINVAL;
955	    break;
956	}
957    }
958
959    splx(s);
960    return error;
961}
962
963static void
964tulip_attach(
965    tulip_softc_t *sc)
966{
967    struct ifnet *ifp = &sc->tulip_if;
968    int cnt;
969
970    ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
971
972    *sc->tulip_csrs.csr_sia_connectivity = 0;
973    *sc->tulip_csrs.csr_sia_connectivity = TULIP_SIACONN_10BASET;
974    for (cnt = 0; cnt < 240000; cnt++) {
975	if ((*sc->tulip_csrs.csr_sia_status & TULIP_SIASTS_LINKFAIL) == 0)
976	    break;
977	DELAY(10);
978    }
979    if (*sc->tulip_csrs.csr_sia_status & TULIP_SIASTS_LINKFAIL) {
980	ifp->if_flags |= IFF_ALTPHYS;
981    } else {
982	sc->tulip_flags |= TULIP_ALTPHYS;
983    }
984    TULIP_RESET(sc);
985
986    ifp->if_init = tulip_init;
987    ifp->if_ioctl = tulip_ioctl;
988    ifp->if_output = ether_output;
989    ifp->if_reset = tulip_reset;
990    ifp->if_start = tulip_start;
991
992    printf("%s%d: %s pass %d.%d ethernet address %s\n",
993	   sc->tulip_name, sc->tulip_unit,
994	   tulip_chipdescs[tulip_chipids[sc->tulip_unit]],
995	   (sc->tulip_revinfo & 0xF0) >> 4,
996	   sc->tulip_revinfo & 0x0F,
997	   ether_sprintf(sc->tulip_hwaddr));
998
999    if_attach(ifp);
1000
1001#if NBPFILTER > 0
1002    bpfattach(&sc->tulip_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
1003#endif
1004}
1005
1006static void
1007tulip_initcsrs(
1008    tulip_softc_t *sc,
1009    volatile tulip_uint32_t *va_csrs,
1010    size_t csr_size)
1011{
1012    sc->tulip_csrs.csr_busmode		= va_csrs +  0 * csr_size;
1013    sc->tulip_csrs.csr_txpoll		= va_csrs +  1 * csr_size;
1014    sc->tulip_csrs.csr_rxpoll		= va_csrs +  2 * csr_size;
1015    sc->tulip_csrs.csr_rxlist		= va_csrs +  3 * csr_size;
1016    sc->tulip_csrs.csr_txlist		= va_csrs +  4 * csr_size;
1017    sc->tulip_csrs.csr_status		= va_csrs +  5 * csr_size;
1018    sc->tulip_csrs.csr_command		= va_csrs +  6 * csr_size;
1019    sc->tulip_csrs.csr_intr		= va_csrs +  7 * csr_size;
1020    sc->tulip_csrs.csr_missed_frame	= va_csrs +  8 * csr_size;
1021    sc->tulip_csrs.csr_enetrom		= va_csrs +  9 * csr_size;
1022    sc->tulip_csrs.csr_reserved		= va_csrs + 10 * csr_size;
1023    sc->tulip_csrs.csr_full_duplex	= va_csrs + 11 * csr_size;
1024    sc->tulip_csrs.csr_sia_status	= va_csrs + 12 * csr_size;
1025    sc->tulip_csrs.csr_sia_connectivity	= va_csrs + 13 * csr_size;
1026    sc->tulip_csrs.csr_sia_tx_rx 	= va_csrs + 14 * csr_size;
1027    sc->tulip_csrs.csr_sia_general	= va_csrs + 15 * csr_size;
1028}
1029
1030static void
1031tulip_initring(
1032    tulip_softc_t *sc,
1033    tulip_ringinfo_t *ri,
1034    tulip_desc_t *descs,
1035    int ndescs)
1036{
1037    ri->ri_max = ndescs;
1038    ri->ri_first = descs;
1039    ri->ri_last = ri->ri_first + ri->ri_max;
1040    bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max);
1041    ri->ri_last[-1].d_flag = TULIP_DFLAG_ENDRING;
1042}
1043
1044#if NPCI > 0
1045/*
1046 * This is the PCI configuration support.  Since the DC21040 is available
1047 * on both EISA and PCI boards, one must be careful in how defines the
1048 * DC21040 in the config file.
1049 */
1050static char* tulip_pci_probe (pcici_t config_id, pcidi_t device_id);
1051static void  tulip_pci_attach(pcici_t config_id, int unit);
1052static u_long tulip_count;
1053static int tulip_pci_shutdown(struct kern_devconf *, int);
1054
1055struct pci_device dedevice = {
1056    "de",
1057    tulip_pci_probe,
1058    tulip_pci_attach,
1059   &tulip_count,
1060    tulip_pci_shutdown,
1061};
1062
1063DATA_SET (pcidevice_set, dedevice);
1064
1065#define	PCI_CFID	0x00	/* Configuration ID */
1066#define	PCI_CFCS	0x04	/* Configurtion Command/Status */
1067#define	PCI_CFRV	0x08	/* Configuration Revision */
1068#define	PCI_CFLT	0x0c	/* Configuration Latency Timer */
1069#define	PCI_CBIO	0x10	/* Configuration Base IO Address */
1070#define	PCI_CBMA	0x14	/* Configuration Base Memory Address */
1071#define	PCI_CFIT	0x3c	/* Configuration Interrupt */
1072#define	PCI_CFDA	0x40	/* Configuration Driver Area */
1073
1074#define	TULIP_PCI_CSRSIZE	(8 / sizeof(tulip_uint32_t))
1075static char*
1076tulip_pci_probe(
1077    pcici_t config_id,
1078    pcidi_t device_id)
1079{
1080    int idx;
1081    for (idx = 0; idx < NDE; idx++) {
1082	if (tulips[idx] == NULL) {
1083	    if (device_id == 0x00021011ul) {
1084		tulip_chipids[idx] = TULIP_DC21040;
1085		return "Digital DC21040 Ethernet";
1086	    }
1087	    if (device_id == 0x00091011ul) {
1088		tulip_chipids[idx] = TULIP_DC21140;
1089		return "Digital DC21140 Fast Ethernet";
1090	    }
1091	    return NULL;
1092	}
1093    }
1094    return NULL;
1095}
1096
1097static void
1098tulip_pci_attach(
1099    pcici_t config_id,
1100    int unit)
1101{
1102    tulip_softc_t *sc;
1103    int retval, idx;
1104    vm_offset_t va_csrs, pa_csrs;
1105    tulip_desc_t *rxdescs, *txdescs;
1106
1107    sc = (tulip_softc_t *) malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
1108    if (sc == NULL)
1109	return;
1110
1111    rxdescs = (tulip_desc_t *)
1112	malloc(sizeof(tulip_desc_t) * TULIP_RXDESCS, M_DEVBUF, M_NOWAIT);
1113    if (rxdescs == NULL) {
1114	free((caddr_t) sc, M_DEVBUF);
1115	return;
1116    }
1117
1118    txdescs = (tulip_desc_t *)
1119	malloc(sizeof(tulip_desc_t) * TULIP_TXDESCS, M_DEVBUF, M_NOWAIT);
1120    if (txdescs == NULL) {
1121	free((caddr_t) rxdescs, M_DEVBUF);
1122	free((caddr_t) sc, M_DEVBUF);
1123	return;
1124    }
1125
1126    bzero(sc, sizeof(*sc));				/* Zero out the softc*/
1127    sc->tulip_rxspace = vm_page_alloc_contig(TULIP_RXSPACE + NBPG, 0, 0xffffffff, PAGE_SIZE);
1128    /*
1129     * We've allocated an extra page of receive space so we can double map
1130     * the first page of the receive space into the page after the last page
1131     * of the receive space.  This means that even if a receive wraps around
1132     * the end of the receive space, it will still virtually contiguous and
1133     * that greatly simplifies the recevie logic.
1134     */
1135    pmap_enter(pmap_kernel(), sc->tulip_rxspace + TULIP_RXSPACE,
1136	       vtophys(sc->tulip_rxspace), VM_PROT_READ|VM_PROT_WRITE, TRUE);
1137
1138    sc->tulip_unit = unit;
1139    sc->tulip_name = "de";
1140    retval = pci_map_mem(config_id, PCI_CBMA, &va_csrs, &pa_csrs);
1141    if (!retval) {
1142	kmem_free(kernel_map, sc->tulip_rxspace, TULIP_RXSPACE + NBPG);
1143	free((caddr_t) txdescs, M_DEVBUF);
1144	free((caddr_t) rxdescs, M_DEVBUF);
1145	free((caddr_t) sc, M_DEVBUF);
1146	return;
1147    }
1148    tulips[unit] = sc;
1149    tulip_initcsrs(sc, (volatile tulip_uint32_t *) va_csrs, TULIP_PCI_CSRSIZE);
1150    tulip_initring(sc, &sc->tulip_rxinfo, rxdescs, TULIP_RXDESCS);
1151    tulip_initring(sc, &sc->tulip_txinfo, txdescs, TULIP_TXDESCS);
1152    sc->tulip_revinfo = pci_conf_read(config_id, PCI_CFRV);
1153    if ((retval = tulip_read_macaddr(sc)) < 0) {
1154	printf("de%d: can't read ENET ROM (why=%d) (", sc->tulip_unit, retval);
1155	for (idx = 0; idx < 32; idx++)
1156	    printf("%02x", sc->tulip_rombuf[idx]);
1157	printf("\n");
1158	printf("%s%d: %s pass %d.%d ethernet address %s\n",
1159	       sc->tulip_name, sc->tulip_unit,
1160	       tulip_chipdescs[tulip_chipids[sc->tulip_unit]],
1161	       (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F,
1162	       "unknown");
1163    } else {
1164	TULIP_RESET(sc);
1165	tulip_attach(sc);
1166	pci_map_int (config_id, tulip_intr, (void*) sc, &net_imask);
1167    }
1168}
1169
1170static int
1171tulip_pci_shutdown(kdc, force)
1172	struct kern_devconf *kdc;
1173	int force;
1174{
1175	tulip_softc_t *sc = tulips[kdc->kdc_unit];
1176
1177	*sc->tulip_csrs.csr_busmode = TULIP_BUSMODE_SWRESET;
1178	DELAY(10);	/* Wait 10 microsends (actually 50 PCI cycles but at
1179			   33MHz that comes to two microseconds but wait a
1180			   bit longer anyways) */
1181	(void) dev_detach(kdc);
1182	return 0;
1183}
1184
1185#endif /* NPCI > 0 */
1186#endif /* NDE > 0 */
1187