xref: /netbsd-src/sys/dev/pci/if_ex_pci.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: if_ex_pci.c,v 1.15 2000/12/28 22:59:13 sommerfeld Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Frank van der Linden; Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include "opt_inet.h"
41 #include "opt_ns.h"
42 #include "bpfilter.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/ioctl.h>
49 #include <sys/errno.h>
50 #include <sys/syslog.h>
51 #include <sys/select.h>
52 #include <sys/device.h>
53 
54 #include <net/if.h>
55 #include <net/if_dl.h>
56 #include <net/if_ether.h>
57 #include <net/if_media.h>
58 
59 #ifdef INET
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip.h>
64 #include <netinet/if_inarp.h>
65 #endif
66 
67 #ifdef NS
68 #include <netns/ns.h>
69 #include <netns/ns_if.h>
70 #endif
71 
72 #if NBPFILTER > 0
73 #include <net/bpf.h>
74 #include <net/bpfdesc.h>
75 #endif
76 
77 #include <machine/cpu.h>
78 #include <machine/bus.h>
79 #include <machine/intr.h>
80 
81 #include <dev/mii/miivar.h>
82 #include <dev/mii/mii.h>
83 
84 #include <dev/ic/elink3var.h>
85 #include <dev/ic/elink3reg.h>
86 #include <dev/ic/elinkxlreg.h>
87 #include <dev/ic/elinkxlvar.h>
88 
89 #include <dev/pci/pcivar.h>
90 #include <dev/pci/pcireg.h>
91 #include <dev/pci/pcidevs.h>
92 
93 struct ex_pci_softc {
94 	struct ex_softc sc_ex;
95 	pci_chipset_tag_t sc_chiptag;
96 	pcitag_t sc_pcitag;
97 };
98 
99 /*
100  * PCI constants.
101  * XXX These should be in a common file!
102  */
103 #define PCI_CONN		0x48    /* Connector type */
104 #define PCI_CBIO		0x10    /* Configuration Base IO Address */
105 #define PCI_POWERCTL		0xe0
106 #define PCI_FUNCMEM		0x18
107 
108 #define PCI_INTRACK		0x00008000
109 
110 int ex_pci_match __P((struct device *, struct cfdata *, void *));
111 void ex_pci_attach __P((struct device *, struct device *, void *));
112 void ex_pci_intr_ack __P((struct ex_softc *));
113 
114 struct cfattach ex_pci_ca = {
115 	sizeof(struct ex_pci_softc), ex_pci_match, ex_pci_attach
116 };
117 
118 const struct ex_pci_product {
119 	u_int32_t	epp_prodid;	/* PCI product ID */
120 	int		epp_flags;	/* initial softc flags */
121 	const char	*epp_name;	/* device name */
122 } ex_pci_products[] = {
123 	{ PCI_PRODUCT_3COM_3C900TPO,	0,
124 	  "3c900-TPO Ethernet" },
125 	{ PCI_PRODUCT_3COM_3C900COMBO,	0,
126 	  "3c900-COMBO Ethernet" },
127 
128 	{ PCI_PRODUCT_3COM_3C905TX,	EX_CONF_MII,
129 	  "3c905-TX 10/100 Ethernet" },
130 	{ PCI_PRODUCT_3COM_3C905T4,	EX_CONF_MII,
131 	  "3c905-T4 10/100 Ethernet" },
132 
133 	{ PCI_PRODUCT_3COM_3C900BTPO,	EX_CONF_90XB,
134 	  "3c900B-TPO Ethernet" },
135 	{ PCI_PRODUCT_3COM_3C900BCOMBO,	EX_CONF_90XB,
136 	  "3c900B-COMBO Ethernet" },
137 	{ PCI_PRODUCT_3COM_3C900BTPC,   EX_CONF_90XB,
138 	  "3c900B-TPC Ethernet" },
139 
140 	{ PCI_PRODUCT_3COM_3C905BTX,	EX_CONF_90XB|EX_CONF_MII|EX_CONF_INTPHY,
141 	  "3c905B-TX 10/100 Ethernet" },
142 	{ PCI_PRODUCT_3COM_3C905BT4,	EX_CONF_90XB|EX_CONF_MII,
143 	  "3c905B-T4 10/100 Ethernet" },
144 	{ PCI_PRODUCT_3COM_3C905BCOMBO,	EX_CONF_90XB/*|EX_CONF_MII|EX_CONF_INTPHY*/,
145 	  "3c905B-COMBO 10/100 Ethernet" },
146 	{ PCI_PRODUCT_3COM_3C905BFX,	EX_CONF_90XB,
147 	  "3c905B-FX 10/100 Ethernet" },
148 
149 	/* XXX Internal PHY? */
150 	{ PCI_PRODUCT_3COM_3C980SRV,	EX_CONF_90XB,
151 	  "3c980 Server Adapter 10/100 Ethernet" },
152 	{ PCI_PRODUCT_3COM_3C980CTXM,	EX_CONF_90XB,
153 	  "3c980C-TXM 10/100 Ethernet" },
154 
155 	{ PCI_PRODUCT_3COM_3C905CTX,	EX_CONF_90XB|EX_CONF_MII,
156 	  "3c905C-TX 10/100 Ethernet with mngmt" },
157 
158 	{ PCI_PRODUCT_3COM_3C450TX,		EX_CONF_90XB,
159 	  "3c450-TX 10/100 Ethernet" },
160 
161 	{ PCI_PRODUCT_3COM_3CSOHO100TX,	EX_CONF_90XB,
162 	  "3cSOHO100-TX 10/100 Ethernet" },
163 
164 	{ PCI_PRODUCT_3COM_3C555,
165 	   EX_CONF_90XB | EX_CONF_MII | EX_CONF_EEPROM_OFF |
166 	   EX_CONF_EEPROM_8BIT,
167 	  "3c555 MiniPCI 10/100 Ethernet" },
168 
169 	{ PCI_PRODUCT_3COM_3C556,
170 	   EX_CONF_90XB | EX_CONF_MII | EX_CONF_EEPROM_OFF |
171 	   EX_CONF_PCI_FUNCREG | EX_CONF_RESETHACK | EX_CONF_INV_LED_POLARITY |
172 	   EX_CONF_PHY_POWER | EX_CONF_EEPROM_8BIT,
173 	  "3c556 MiniPCI 10/100 Ethernet" },
174 
175 	{ PCI_PRODUCT_3COM_3C556B,
176 	   EX_CONF_90XB | EX_CONF_MII | EX_CONF_EEPROM_OFF |
177 	   EX_CONF_PCI_FUNCREG | EX_CONF_RESETHACK | EX_CONF_INV_LED_POLARITY |
178 	   EX_CONF_PHY_POWER,
179 	  "3c556B MiniPCI 10/100 Ethernet" },
180 
181 	{ 0,				0,
182 	  NULL },
183 };
184 
185 const struct ex_pci_product *ex_pci_lookup
186     __P((const struct pci_attach_args *));
187 
188 const struct ex_pci_product *
189 ex_pci_lookup(pa)
190 	const struct pci_attach_args *pa;
191 {
192 	const struct ex_pci_product *epp;
193 
194 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3COM)
195 		return (NULL);
196 
197 	for (epp = ex_pci_products; epp->epp_name != NULL; epp++)
198 		if (PCI_PRODUCT(pa->pa_id) == epp->epp_prodid)
199 			return (epp);
200 	return (NULL);
201 }
202 
203 int
204 ex_pci_match(parent, match, aux)
205 	struct device *parent;
206 	struct cfdata *match;
207 	void *aux;
208 {
209 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
210 
211 	if (ex_pci_lookup(pa) != NULL)
212 		return (2);	/* beat ep_pci */
213 
214 	return (0);
215 }
216 
217 void
218 ex_pci_attach(parent, self, aux)
219 	struct device *parent, *self;
220 	void *aux;
221 {
222 	struct ex_softc *sc = (void *)self;
223 	struct ex_pci_softc *psc = (void *)self;
224 	struct pci_attach_args *pa = aux;
225 	pci_chipset_tag_t pc = pa->pa_pc;
226 	pci_intr_handle_t ih;
227 	const struct ex_pci_product *epp;
228 	const char *intrstr = NULL;
229 	int rev, pmreg;
230 	pcireg_t reg;
231 
232 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
233 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
234 		printf(": can't map i/o space\n");
235 		return;
236 	}
237 
238 	epp = ex_pci_lookup(pa);
239 	if (epp == NULL) {
240 		printf("\n");
241 		panic("ex_pci_attach: impossible");
242 	}
243 
244 	rev = PCI_REVISION(pci_conf_read(pc, pa->pa_tag, PCI_CLASS_REG));
245 	printf(": 3Com %s (rev. 0x%x)\n", epp->epp_name, rev);
246 
247 	sc->enable = NULL;
248 	sc->disable = NULL;
249 	sc->enabled = 1;
250 
251 	sc->sc_dmat = pa->pa_dmat;
252 
253 	sc->ex_bustype = EX_BUS_PCI;
254 	sc->ex_conf = epp->epp_flags;
255 
256 	/* Enable the card. */
257 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
258 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
259 	    PCI_COMMAND_MASTER_ENABLE);
260 
261 	/* Get it out of power save mode if needed (BIOS bugs) */
262 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
263 		reg = pci_conf_read(pc, pa->pa_tag, pmreg + 4) & 0x3;
264 		if (reg == 3) {
265 			/*
266 			 * The card has lost all configuration data in
267 			 * this state, so punt.
268 			 */
269 			printf("%s: unable to wake up from power state D3\n",
270 			    sc->sc_dev.dv_xname);
271 			return;
272 		}
273 		if (reg != 0) {
274 			printf("%s: waking up from power state D%d\n",
275 			    sc->sc_dev.dv_xname, reg);
276 			pci_conf_write(pc, pa->pa_tag, pmreg + 4, 0);
277 		}
278 	}
279 
280 	/* Map and establish the interrupt. */
281 	if (pci_intr_map(pa, &ih)) {
282 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
283 		return;
284 	}
285 
286 	if (sc->ex_conf & EX_CONF_PCI_FUNCREG) {
287 		sc->intr_ack = ex_pci_intr_ack;
288 		psc->sc_chiptag = pa->pa_pc;
289 		psc->sc_pcitag = pa->pa_tag;
290 	}
291 
292 	intrstr = pci_intr_string(pc, ih);
293 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ex_intr, sc);
294 	if (sc->sc_ih == NULL) {
295 		printf("%s: couldn't establish interrupt",
296 		    sc->sc_dev.dv_xname);
297 		if (intrstr != NULL)
298 			printf(" at %s", intrstr);
299 		printf("\n");
300 		return;
301 	}
302 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
303 
304 	ex_config(sc);
305 }
306 
307 void
308 ex_pci_intr_ack(sc)
309 	struct ex_softc *sc;
310 {
311 	struct ex_pci_softc *psc = (struct ex_pci_softc *)sc;
312 
313 	pci_conf_write(psc->sc_chiptag, psc->sc_pcitag, PCI_FUNCMEM + 4,
314 	    PCI_INTRACK);
315 }
316