if_ex_pci.c revision 1.1 1 /* $NetBSD: if_ex_pci.c,v 1.1 1998/11/04 00:31:05 fvdl 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.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include "opt_inet.h"
40 #include "opt_ns.h"
41 #include "bpfilter.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
50 #include <sys/select.h>
51 #include <sys/device.h>
52
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #include <net/if_ether.h>
56 #include <net/if_media.h>
57
58 #ifdef INET
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/if_inarp.h>
64 #endif
65
66 #ifdef NS
67 #include <netns/ns.h>
68 #include <netns/ns_if.h>
69 #endif
70
71 #if NBPFILTER > 0
72 #include <net/bpf.h>
73 #include <net/bpfdesc.h>
74 #endif
75
76 #include <machine/cpu.h>
77 #include <machine/bus.h>
78 #include <machine/intr.h>
79
80 #include <dev/mii/miivar.h>
81 #include <dev/mii/mii.h>
82
83 #include <dev/ic/elink3var.h>
84 #include <dev/ic/elink3reg.h>
85 #include <dev/ic/elinkxlreg.h>
86 #include <dev/ic/elinkxlvar.h>
87
88 #include <dev/pci/pcivar.h>
89 #include <dev/pci/pcireg.h>
90 #include <dev/pci/pcidevs.h>
91
92 /*
93 * PCI constants.
94 * XXX These should be in a common file!
95 */
96 #define PCI_CONN 0x48 /* Connector type */
97 #define PCI_CBIO 0x10 /* Configuration Base IO Address */
98 #define PCI_CAPPTR 0x34
99 #define PCI_CAPID 0xdc
100 #define PCI_POWERCAP 0xde
101 #define PCI_POWERCTL 0xe0
102
103 int ex_pci_match __P((struct device *, struct cfdata *, void *));
104 void ex_pci_attach __P((struct device *, struct device *, void *));
105
106 struct cfattach ex_pci_ca = {
107 sizeof(struct ex_softc), ex_pci_match, ex_pci_attach
108 };
109
110 int
111 ex_pci_match(parent, match, aux)
112 struct device *parent;
113 struct cfdata *match;
114 void *aux;
115 {
116 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
117
118 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3COM)
119 return 0;
120
121 switch (PCI_PRODUCT(pa->pa_id)) {
122 case PCI_PRODUCT_3COM_3C900TPO:
123 case PCI_PRODUCT_3COM_3C900COMBO:
124 case PCI_PRODUCT_3COM_3C905TX:
125 case PCI_PRODUCT_3COM_3C905T4:
126 case PCI_PRODUCT_3COM_3C900BTPO:
127 case PCI_PRODUCT_3COM_3C900BCOMBO:
128 case PCI_PRODUCT_3COM_3C905BT4:
129 case PCI_PRODUCT_3COM_3C905BTX:
130 break;
131 default:
132 return 0;
133 }
134
135 return 1;
136 }
137
138 void
139 ex_pci_attach(parent, self, aux)
140 struct device *parent, *self;
141 void *aux;
142 {
143 struct ex_softc *sc = (void *)self;
144 struct pci_attach_args *pa = aux;
145 pci_chipset_tag_t pc = pa->pa_pc;
146 pci_intr_handle_t ih;
147 char *model;
148 const char *intrstr = NULL;
149 int pmode;
150
151 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
152 &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
153 printf(": can't map i/o space\n");
154 return;
155 }
156
157 sc->sc_dmat = pa->pa_dmat;
158
159 sc->ex_bustype = EX_BUS_PCI;
160 sc->ex_conf = 0;
161
162 switch (PCI_PRODUCT(pa->pa_id)) {
163 case PCI_PRODUCT_3COM_3C900TPO:
164 case PCI_PRODUCT_3COM_3C900COMBO:
165 model = "3Com 3C900 Ethernet";
166 break;
167 case PCI_PRODUCT_3COM_3C905TX:
168 case PCI_PRODUCT_3COM_3C905T4:
169 sc->ex_conf = EX_CONF_MII;
170 model = "3Com 3C905 Ethernet";
171 break;
172 case PCI_PRODUCT_3COM_3C900BTPO:
173 case PCI_PRODUCT_3COM_3C900BCOMBO:
174 sc->ex_conf = EX_CONF_90XB;
175 model = "3Com 3C900B Ethernet";
176 break;
177 case PCI_PRODUCT_3COM_3C905BTX:
178 case PCI_PRODUCT_3COM_3C905BT4:
179 sc->ex_conf = (EX_CONF_MII | EX_CONF_90XB);
180 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3COM_3C905BTX)
181 sc->ex_conf |= EX_CONF_INTPHY;
182 model = "3Com 3C905B Ethernet";
183 break;
184 default:
185 model = "unknown model!";
186 break;
187 }
188
189 printf(": %s\n", model);
190
191 sc->enable = NULL;
192 sc->disable = NULL;
193 sc->enabled = 1;
194
195 /* Enable the card. */
196 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
197 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
198 PCI_COMMAND_MASTER_ENABLE);
199
200 /* Get it out of power save mode if needed (BIOS bugs) */
201 if ((pci_conf_read(pc, pa->pa_tag, PCI_CAPPTR) & 0xff) == PCI_CAPID &&
202 (pci_conf_read(pc, pa->pa_tag, PCI_CAPID) & 0xff) == 0x01) {
203 pmode = pci_conf_read(pc, pa->pa_tag, PCI_POWERCTL) & 0x3;
204 if (pmode == 3) {
205 /*
206 * The card has lost all configuration data in
207 * this state, so punt.
208 */
209 printf("%s: unable to wake up from power state D3\n",
210 sc->sc_dev.dv_xname);
211 return;
212 }
213 if (pmode != 0) {
214 printf("%s: waking up from power state D%d\n",
215 sc->sc_dev.dv_xname, pmode);
216 pci_conf_write(pc, pa->pa_tag, PCI_POWERCTL, 0);
217 }
218 }
219
220 /* Map and establish the interrupt. */
221 if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
222 pa->pa_intrline, &ih)) {
223 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
224 return;
225 }
226 intrstr = pci_intr_string(pc, ih);
227 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ex_intr, sc);
228 if (sc->sc_ih == NULL) {
229 printf("%s: couldn't establish interrupt",
230 sc->sc_dev.dv_xname);
231 if (intrstr != NULL)
232 printf(" at %s", intrstr);
233 printf("\n");
234 return;
235 }
236 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
237
238 ex_config(sc);
239
240 }
241