xref: /openbsd-src/sys/dev/pci/if_rl_pci.c (revision 4a44a0e8724533dbfeeef6bca64988f5081aff32)
1 /*	$OpenBSD: if_rl_pci.c,v 1.17 2009/06/02 17:27:39 jsg Exp $ */
2 
3 /*
4  * Copyright (c) 1997, 1998
5  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "bpfilter.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sockio.h>
40 #include <sys/mbuf.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/device.h>
45 #include <sys/timeout.h>
46 
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 
51 #ifdef INET
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/in_var.h>
55 #include <netinet/ip.h>
56 #include <netinet/if_ether.h>
57 #endif
58 
59 #include <net/if_media.h>
60 
61 #if NBPFILTER > 0
62 #include <net/bpf.h>
63 #endif
64 
65 #include <machine/bus.h>
66 
67 #include <dev/mii/mii.h>
68 #include <dev/mii/miivar.h>
69 #include <dev/pci/pcireg.h>
70 #include <dev/pci/pcivar.h>
71 #include <dev/pci/pcidevs.h>
72 
73 /*
74  * Default to using PIO access for this driver. On SMP systems,
75  * there appear to be problems with memory mapped mode: it looks like
76  * doing too many memory mapped access back to back in rapid succession
77  * can hang the bus. I'm inclined to blame this on crummy design/construction
78  * on the part of RealTek. Memory mapped mode does appear to work on
79  * uniprocessor systems though.
80  */
81 #define RL_USEIOSPACE
82 
83 #include <dev/ic/rtl81x9reg.h>
84 
85 int rl_pci_match(struct device *, void *, void *);
86 void rl_pci_attach(struct device *, struct device *, void *);
87 int rl_pci_detach(struct device *, int);
88 
89 struct rl_pci_softc {
90 	struct rl_softc		psc_softc;
91 	pci_chipset_tag_t	psc_pc;
92 	bus_size_t		psc_mapsize;
93 };
94 
95 struct cfattach rl_pci_ca = {
96 	sizeof(struct rl_pci_softc), rl_pci_match, rl_pci_attach, rl_pci_detach
97 };
98 
99 const struct pci_matchid rl_pci_devices[] = {
100 	{ PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_5030 },
101 	{ PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_8139 },
102 	{ PCI_VENDOR_DELTA, PCI_PRODUCT_DELTA_8139 },
103 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_530TXPLUS },
104 	{ PCI_VENDOR_NORTEL, PCI_PRODUCT_NORTEL_BS21 },
105 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8129 },
106 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139D },
107 	{ PCI_VENDOR_ABOCOM, PCI_PRODUCT_ABOCOM_FE2000VX },
108 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8138 },
109 	{ PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_CB_TXD },
110 	{ PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_2CB_TXD },
111 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE690TXD },
112 	{ PCI_VENDOR_PLANEX, PCI_PRODUCT_PLANEX_FNW_3603_TX },
113 	{ PCI_VENDOR_PLANEX, PCI_PRODUCT_PLANEX_FNW_3800_TX }
114 };
115 
116 int
117 rl_pci_match(parent, match, aux)
118 	struct device *parent;
119 	void *match;
120 	void *aux;
121 {
122 	struct pci_attach_args *pa = aux;
123 
124 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK &&
125 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8139 &&
126 	    PCI_REVISION(pa->pa_class) == 0x10)
127 		return (1);
128 
129 	return (pci_matchbyid((struct pci_attach_args *)aux, rl_pci_devices,
130 	    sizeof(rl_pci_devices)/sizeof(rl_pci_devices[0])));
131 }
132 
133 void
134 rl_pci_attach(parent, self, aux)
135 	struct device *parent, *self;
136 	void *aux;
137 {
138 	struct rl_pci_softc *psc = (void *)self;
139 	struct rl_softc *sc = &psc->psc_softc;
140 	struct pci_attach_args *pa = aux;
141 	pci_chipset_tag_t pc = pa->pa_pc;
142 	pci_intr_handle_t ih;
143 	const char *intrstr = NULL;
144 
145 	/*
146 	 * Map control/status registers.
147 	 */
148 
149 #ifdef RL_USEIOSPACE
150 	if (pci_mapreg_map(pa, RL_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
151 	    &sc->rl_btag, &sc->rl_bhandle, NULL, &psc->psc_mapsize, 0)) {
152 		printf(": can't map i/o space\n");
153 		return;
154 	}
155 #else
156 	if (pci_mapreg_map(pa, RL_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
157 	    &sc->rl_btag, &sc->rl_bhandle, NULL, &psc->psc_mapsize, 0)){
158 		printf(": can't map mem space\n");
159 		return;
160 	}
161 #endif
162 
163 	/*
164 	 * Allocate our interrupt.
165 	 */
166 	if (pci_intr_map(pa, &ih)) {
167 		printf(": couldn't map interrupt\n");
168 		bus_space_unmap(sc->rl_btag, sc->rl_bhandle, psc->psc_mapsize);
169 		return;
170 	}
171 
172 	psc->psc_pc = pa->pa_pc;
173 	intrstr = pci_intr_string(pc, ih);
174 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, rl_intr, sc,
175 	    self->dv_xname);
176 	if (sc->sc_ih == NULL) {
177 		printf(": couldn't establish interrupt");
178 		if (intrstr != NULL)
179 			printf(" at %s", intrstr);
180 		printf("\n");
181 		bus_space_unmap(sc->rl_btag, sc->rl_bhandle, psc->psc_mapsize);
182 		return;
183 	}
184 	printf(": %s", intrstr);
185 
186 	sc->sc_dmat = pa->pa_dmat;
187 
188 	rl_attach(sc);
189 }
190 
191 int
192 rl_pci_detach(struct device *self, int flags)
193 {
194 	struct rl_pci_softc *psc = (void *)self;
195 	struct rl_softc *sc = &psc->psc_softc;
196 	int rv;
197 
198 	rv = rl_detach(sc);
199 	if (rv)
200 		return (rv);
201 
202 	if (sc->sc_ih != NULL)
203 		pci_intr_disestablish(psc->psc_pc, sc->sc_ih);
204 
205 	bus_space_unmap(sc->rl_btag, sc->rl_bhandle, psc->psc_mapsize);
206 
207 	return (0);
208 }
209 
210