xref: /openbsd-src/sys/dev/pci/if_rl_pci.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*	$OpenBSD: if_rl_pci.c,v 1.6 2001/11/06 19:53:19 miod 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 <uvm/uvm_extern.h>              /* for vtophys */
66 #include <machine/bus.h>
67 
68 #include <dev/mii/mii.h>
69 #include <dev/mii/miivar.h>
70 #include <dev/pci/pcireg.h>
71 #include <dev/pci/pcivar.h>
72 #include <dev/pci/pcidevs.h>
73 
74 /*
75  * Default to using PIO access for this driver. On SMP systems,
76  * there appear to be problems with memory mapped mode: it looks like
77  * doing too many memory mapped access back to back in rapid succession
78  * can hang the bus. I'm inclined to blame this on crummy design/construction
79  * on the part of RealTek. Memory mapped mode does appear to work on
80  * uniprocessor systems though.
81  */
82 #define RL_USEIOSPACE
83 
84 #include <dev/ic/rtl81x9reg.h>
85 
86 int rl_pci_match	__P((struct device *, void *, void *));
87 void rl_pci_attach	__P((struct device *, struct device *, void *));
88 
89 struct cfattach rl_pci_ca = {
90 	sizeof(struct rl_softc), rl_pci_match, rl_pci_attach,
91 };
92 
93 struct rl_type rl_pci_devs[] = {
94 	{ PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_5030		},
95 	{ PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_8139		},
96 	{ PCI_VENDOR_DELTA, PCI_PRODUCT_DELTA_8139		},
97 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_530TXPLUS		},
98 	{ PCI_VENDOR_NORTEL, PCI_PRODUCT_NORTEL_BS21		},
99 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8129	},
100 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139	},
101 	{ 0, 0 }
102 };
103 
104 int
105 rl_pci_match(parent, match, aux)
106 	struct device *parent;
107 	void *match;
108 	void *aux;
109 {
110 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
111 	struct rl_type *t;
112 
113 	for (t = rl_pci_devs; t->rl_vid != 0; t++) {
114 		if ((PCI_VENDOR(pa->pa_id) == t->rl_vid) &&
115 		    (PCI_PRODUCT(pa->pa_id) == t->rl_did))
116 			return (1);
117 	}
118 	return (0);
119 }
120 
121 void
122 rl_pci_attach(parent, self, aux)
123 	struct device *parent, *self;
124 	void *aux;
125 {
126 	struct rl_softc *sc = (struct rl_softc *)self;
127 	struct pci_attach_args *pa = aux;
128 	pci_chipset_tag_t pc = pa->pa_pc;
129 	pci_intr_handle_t ih;
130 	const char *intrstr = NULL;
131 	bus_addr_t iobase;
132 	bus_size_t iosize;
133 	u_int32_t command;
134 
135 	command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
136 
137 #ifdef RL_USEIOSPACE
138 	if (!(command & PCI_COMMAND_IO_ENABLE)) {
139 		printf(": failed to enable i/o ports\n");
140 		return;
141 	}
142 
143 	/*
144 	 * Map control/status registers.
145 	 */
146 	if (pci_io_find(pc, pa->pa_tag, RL_PCI_LOIO, &iobase, &iosize)) {
147 		printf(": can't find i/o space\n");
148 		return;
149 	}
150 	if (bus_space_map(pa->pa_iot, iobase, iosize, 0, &sc->rl_bhandle)) {
151 		printf(": can't map i/o space\n");
152 		return;
153 	}
154 	sc->rl_btag = pa->pa_iot;
155 #else
156 	if (!(command & PCI_COMMAND_MEM_ENABLE)) {
157 		printf(": failed to enable memory mapping\n");
158 		return;
159 	}
160 	if (pci_mem_find(pc, pa->pa_tag, RL_PCI_LOMEM, &iobase, &iosize, NULL)){
161 		printf(": can't find mem space\n");
162 		return;
163 	}
164 	if (bus_space_map(pa->pa_memt, iobase, iosize, 0, &sc->rl_bhandle)) {
165 		printf(": can't map mem space\n");
166 		return;
167 	}
168 	sc->rl_btag = pa->pa_memt;
169 #endif
170 
171 	/*
172 	 * Allocate our interrupt.
173 	 */
174 	if (pci_intr_map(pa, &ih)) {
175 		printf(": couldn't map interrupt\n");
176 		return;
177 	}
178 
179 	intrstr = pci_intr_string(pc, ih);
180 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, rl_intr, sc,
181 	    self->dv_xname);
182 	if (sc->sc_ih == NULL) {
183 		printf(": couldn't establish interrupt");
184 		if (intrstr != NULL)
185 			printf(" at %s", intrstr);
186 		printf("\n");
187 		return;
188 	}
189 	printf(": %s", intrstr);
190 
191 	sc->sc_dmat = pa->pa_dmat;
192 
193 	rl_attach(sc);
194 }
195