if_fxp_pci.c revision 1.8 1 1.8 jhawk /* $NetBSD: if_fxp_pci.c,v 1.8 2000/05/12 18:46:34 jhawk Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.5 thorpej * Copyright (c) 1997, 1998, 1999, 2000 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 thorpej * NASA Ames Research Center.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.1 thorpej * must display the following acknowledgement:
21 1.1 thorpej * This product includes software developed by the NetBSD
22 1.1 thorpej * Foundation, Inc. and its contributors.
23 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 thorpej * contributors may be used to endorse or promote products derived
25 1.1 thorpej * from this software without specific prior written permission.
26 1.1 thorpej *
27 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.1 thorpej */
39 1.1 thorpej
40 1.1 thorpej /*
41 1.1 thorpej * PCI bus front-end for the Intel i82557 fast Ethernet controller
42 1.1 thorpej * driver. Works with Intel Etherexpress Pro 10+, 100B, 100+ cards.
43 1.1 thorpej */
44 1.1 thorpej
45 1.1 thorpej #include "opt_inet.h"
46 1.1 thorpej #include "opt_ns.h"
47 1.1 thorpej #include "bpfilter.h"
48 1.1 thorpej #include "rnd.h"
49 1.1 thorpej
50 1.1 thorpej #include <sys/param.h>
51 1.1 thorpej #include <sys/systm.h>
52 1.1 thorpej #include <sys/mbuf.h>
53 1.1 thorpej #include <sys/malloc.h>
54 1.1 thorpej #include <sys/kernel.h>
55 1.1 thorpej #include <sys/socket.h>
56 1.1 thorpej #include <sys/ioctl.h>
57 1.1 thorpej #include <sys/errno.h>
58 1.1 thorpej #include <sys/device.h>
59 1.1 thorpej
60 1.1 thorpej #if NRND > 0
61 1.1 thorpej #include <sys/rnd.h>
62 1.1 thorpej #endif
63 1.3 thorpej
64 1.3 thorpej #include <machine/endian.h>
65 1.1 thorpej
66 1.1 thorpej #include <net/if.h>
67 1.1 thorpej #include <net/if_dl.h>
68 1.1 thorpej #include <net/if_media.h>
69 1.1 thorpej #include <net/if_ether.h>
70 1.1 thorpej
71 1.1 thorpej #if NBPFILTER > 0
72 1.1 thorpej #include <net/bpf.h>
73 1.1 thorpej #endif
74 1.1 thorpej
75 1.1 thorpej #ifdef INET
76 1.1 thorpej #include <netinet/in.h>
77 1.1 thorpej #include <netinet/if_inarp.h>
78 1.1 thorpej #endif
79 1.1 thorpej
80 1.1 thorpej #ifdef NS
81 1.1 thorpej #include <netns/ns.h>
82 1.1 thorpej #include <netns/ns_if.h>
83 1.1 thorpej #endif
84 1.1 thorpej
85 1.1 thorpej #include <machine/bus.h>
86 1.1 thorpej #include <machine/intr.h>
87 1.1 thorpej
88 1.1 thorpej #include <dev/mii/miivar.h>
89 1.1 thorpej
90 1.1 thorpej #include <dev/ic/i82557reg.h>
91 1.1 thorpej #include <dev/ic/i82557var.h>
92 1.1 thorpej
93 1.1 thorpej #include <dev/pci/pcivar.h>
94 1.1 thorpej #include <dev/pci/pcireg.h>
95 1.1 thorpej #include <dev/pci/pcidevs.h>
96 1.1 thorpej
97 1.7 jhawk struct fxp_pci_softc {
98 1.7 jhawk struct fxp_softc psc_fxp;
99 1.7 jhawk
100 1.7 jhawk pci_chipset_tag_t psc_pc; /* pci chipset tag */
101 1.7 jhawk pcireg_t psc_regs[0x20>>2]; /* saved PCI config regs (sparse) */
102 1.7 jhawk pcitag_t psc_tag; /* pci register tag */
103 1.7 jhawk void *psc_powerhook; /* power hook */
104 1.7 jhawk };
105 1.6 jhawk
106 1.1 thorpej int fxp_pci_match __P((struct device *, struct cfdata *, void *));
107 1.1 thorpej void fxp_pci_attach __P((struct device *, struct device *, void *));
108 1.1 thorpej
109 1.6 jhawk static void fxp_pci_confreg_restore __P((struct fxp_pci_softc *psc));
110 1.6 jhawk static void fxp_pci_power __P((int why, void *arg));
111 1.6 jhawk
112 1.1 thorpej struct cfattach fxp_pci_ca = {
113 1.6 jhawk sizeof(struct fxp_pci_softc), fxp_pci_match, fxp_pci_attach
114 1.1 thorpej };
115 1.1 thorpej
116 1.5 thorpej const struct fxp_pci_product {
117 1.5 thorpej u_int32_t fpp_prodid; /* PCI product ID */
118 1.5 thorpej const char *fpp_name; /* device name */
119 1.5 thorpej } fxp_pci_products[] = {
120 1.5 thorpej { PCI_PRODUCT_INTEL_82557,
121 1.5 thorpej "Intel i82557 Ethernet" },
122 1.5 thorpej { PCI_PRODUCT_INTEL_IN_BUSINESS,
123 1.5 thorpej "Intel InBusiness Ethernet" },
124 1.5 thorpej
125 1.5 thorpej { 0,
126 1.5 thorpej NULL },
127 1.5 thorpej };
128 1.5 thorpej
129 1.5 thorpej const struct fxp_pci_product *fxp_pci_lookup
130 1.5 thorpej __P((const struct pci_attach_args *));
131 1.5 thorpej
132 1.5 thorpej const struct fxp_pci_product *
133 1.5 thorpej fxp_pci_lookup(pa)
134 1.5 thorpej const struct pci_attach_args *pa;
135 1.5 thorpej {
136 1.5 thorpej const struct fxp_pci_product *fpp;
137 1.5 thorpej
138 1.5 thorpej if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
139 1.5 thorpej return (NULL);
140 1.5 thorpej
141 1.5 thorpej for (fpp = fxp_pci_products; fpp->fpp_name != NULL; fpp++)
142 1.5 thorpej if (PCI_PRODUCT(pa->pa_id) == fpp->fpp_prodid)
143 1.5 thorpej return (fpp);
144 1.5 thorpej
145 1.5 thorpej return (NULL);
146 1.5 thorpej }
147 1.5 thorpej
148 1.1 thorpej int
149 1.1 thorpej fxp_pci_match(parent, match, aux)
150 1.1 thorpej struct device *parent;
151 1.1 thorpej struct cfdata *match;
152 1.1 thorpej void *aux;
153 1.1 thorpej {
154 1.1 thorpej struct pci_attach_args *pa = aux;
155 1.1 thorpej
156 1.5 thorpej if (fxp_pci_lookup(pa) != NULL)
157 1.1 thorpej return (1);
158 1.1 thorpej
159 1.1 thorpej return (0);
160 1.1 thorpej }
161 1.1 thorpej
162 1.6 jhawk /*
163 1.6 jhawk * Restore PCI configuration registers that may have been clobbered.
164 1.6 jhawk * This is necessary due to bugs on the Sony VAIO Z505-series on-board
165 1.6 jhawk * ethernet, after an APM suspend/resume, as well as after an ACPI
166 1.6 jhawk * D3->D0 transition. We call this function from a power hook after
167 1.6 jhawk * APM resume events, as well as after the ACPI D3->D0 transition.
168 1.6 jhawk */
169 1.6 jhawk static void
170 1.6 jhawk fxp_pci_confreg_restore(psc)
171 1.6 jhawk struct fxp_pci_softc *psc;
172 1.6 jhawk {
173 1.6 jhawk pcireg_t reg;
174 1.6 jhawk
175 1.6 jhawk #if 0
176 1.6 jhawk /*
177 1.6 jhawk * Check to see if the command register is blank -- if so, then
178 1.6 jhawk * we'll assume that all the clobberable-registers have been
179 1.6 jhawk * clobbered.
180 1.6 jhawk */
181 1.6 jhawk
182 1.6 jhawk /*
183 1.6 jhawk * In general, the above metric is accurate. Unfortunately,
184 1.6 jhawk * it is inaccurate across a hibernation. Ideally APM/ACPI
185 1.6 jhawk * code should take note of hibernation events and execute
186 1.6 jhawk * a hibernation wakeup hook, but at present a hibernation wake
187 1.6 jhawk * is indistinguishable from a suspend wake.
188 1.6 jhawk */
189 1.6 jhawk
190 1.6 jhawk if (((reg = pci_conf_read(psc->psc_pc, psc->psc_tag,
191 1.6 jhawk PCI_COMMAND_STATUS_REG)) & 0xffff) != 0)
192 1.6 jhawk return;
193 1.6 jhawk #endif
194 1.6 jhawk
195 1.6 jhawk pci_conf_write(psc->psc_pc, psc->psc_tag,
196 1.6 jhawk PCI_COMMAND_STATUS_REG,
197 1.6 jhawk (reg & 0xffff0000) |
198 1.6 jhawk (psc->psc_regs[PCI_COMMAND_STATUS_REG>>2] & 0xffff));
199 1.6 jhawk pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_BHLC_REG,
200 1.6 jhawk psc->psc_regs[PCI_BHLC_REG>>2]);
201 1.6 jhawk pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_MAPREG_START+0x0,
202 1.6 jhawk psc->psc_regs[(PCI_MAPREG_START+0x0)>>2]);
203 1.6 jhawk pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_MAPREG_START+0x4,
204 1.6 jhawk psc->psc_regs[(PCI_MAPREG_START+0x4)>>2]);
205 1.6 jhawk pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_MAPREG_START+0x8,
206 1.6 jhawk psc->psc_regs[(PCI_MAPREG_START+0x8)>>2]);
207 1.6 jhawk }
208 1.6 jhawk
209 1.6 jhawk
210 1.6 jhawk /*
211 1.6 jhawk * Power handler routine. Called when the system is transitioning into/out
212 1.6 jhawk * of power save modes. We restore the (bashed) PCI configuration registers
213 1.6 jhawk * on a resume.
214 1.6 jhawk */
215 1.6 jhawk static void
216 1.6 jhawk fxp_pci_power(why, arg)
217 1.6 jhawk int why;
218 1.6 jhawk void *arg;
219 1.6 jhawk {
220 1.6 jhawk struct fxp_pci_softc *psc = arg;
221 1.6 jhawk
222 1.6 jhawk if (why == PWR_RESUME)
223 1.6 jhawk fxp_pci_confreg_restore(psc);
224 1.6 jhawk
225 1.6 jhawk }
226 1.6 jhawk
227 1.6 jhawk
228 1.1 thorpej void
229 1.1 thorpej fxp_pci_attach(parent, self, aux)
230 1.1 thorpej struct device *parent, *self;
231 1.1 thorpej void *aux;
232 1.1 thorpej {
233 1.6 jhawk struct fxp_pci_softc *psc = (struct fxp_pci_softc *)self;
234 1.1 thorpej struct fxp_softc *sc = (struct fxp_softc *)self;
235 1.1 thorpej struct pci_attach_args *pa = aux;
236 1.1 thorpej pci_chipset_tag_t pc = pa->pa_pc;
237 1.1 thorpej pci_intr_handle_t ih;
238 1.5 thorpej const struct fxp_pci_product *fpp;
239 1.1 thorpej const char *intrstr = NULL;
240 1.1 thorpej bus_space_tag_t iot, memt;
241 1.1 thorpej bus_space_handle_t ioh, memh;
242 1.1 thorpej int ioh_valid, memh_valid;
243 1.1 thorpej bus_addr_t addr;
244 1.1 thorpej bus_size_t size;
245 1.1 thorpej int flags;
246 1.6 jhawk int pci_pwrmgmt_cap_reg, pci_pwrmgmt_csr_reg;
247 1.2 sommerfe
248 1.2 sommerfe sc->sc_enabled = 1;
249 1.2 sommerfe sc->sc_enable = NULL;
250 1.2 sommerfe sc->sc_disable = NULL;
251 1.1 thorpej
252 1.1 thorpej /*
253 1.1 thorpej * Map control/status registers.
254 1.1 thorpej */
255 1.1 thorpej ioh_valid = (pci_mapreg_map(pa, FXP_PCI_IOBA,
256 1.1 thorpej PCI_MAPREG_TYPE_IO, 0,
257 1.1 thorpej &iot, &ioh, NULL, NULL) == 0);
258 1.1 thorpej
259 1.1 thorpej /*
260 1.1 thorpej * Version 2.1 of the PCI spec, page 196, "Address Maps":
261 1.1 thorpej *
262 1.1 thorpej * Prefetchable
263 1.1 thorpej *
264 1.1 thorpej * Set to one if there are no side effects on reads, the
265 1.1 thorpej * device returns all bytes regardless of the byte enables,
266 1.1 thorpej * and host bridges can merge processor writes into this
267 1.1 thorpej * range without causing errors. Bit must be set to zero
268 1.1 thorpej * otherwise.
269 1.1 thorpej *
270 1.1 thorpej * The 82557 incorrectly sets the "prefetchable" bit, resulting
271 1.1 thorpej * in errors on systems which will do merged reads and writes.
272 1.1 thorpej * These errors manifest themselves as all-bits-set when reading
273 1.1 thorpej * from the EEPROM or other < 4 byte registers.
274 1.1 thorpej *
275 1.1 thorpej * We must work around this problem by always forcing the mapping
276 1.1 thorpej * for memory space to be uncacheable. On systems which cannot
277 1.1 thorpej * create an uncacheable mapping (because the firmware mapped it
278 1.1 thorpej * into only cacheable/prefetchable space due to the "prefetchable"
279 1.1 thorpej * bit), we can fall back onto i/o mapped access.
280 1.1 thorpej */
281 1.1 thorpej memh_valid = 0;
282 1.1 thorpej memt = pa->pa_memt;
283 1.1 thorpej if (((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) != 0) &&
284 1.1 thorpej pci_mapreg_info(pa->pa_pc, pa->pa_tag, FXP_PCI_MMBA,
285 1.1 thorpej PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT,
286 1.1 thorpej &addr, &size, &flags) == 0) {
287 1.4 drochner flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
288 1.1 thorpej if (bus_space_map(memt, addr, size, flags, &memh) == 0)
289 1.1 thorpej memh_valid = 1;
290 1.1 thorpej }
291 1.1 thorpej
292 1.1 thorpej if (memh_valid) {
293 1.1 thorpej sc->sc_st = memt;
294 1.1 thorpej sc->sc_sh = memh;
295 1.1 thorpej } else if (ioh_valid) {
296 1.1 thorpej sc->sc_st = iot;
297 1.1 thorpej sc->sc_sh = ioh;
298 1.1 thorpej } else {
299 1.1 thorpej printf(": unable to map device registers\n");
300 1.1 thorpej return;
301 1.1 thorpej }
302 1.1 thorpej
303 1.1 thorpej sc->sc_dmat = pa->pa_dmat;
304 1.1 thorpej
305 1.5 thorpej fpp = fxp_pci_lookup(pa);
306 1.5 thorpej if (fpp == NULL) {
307 1.5 thorpej printf("\n");
308 1.5 thorpej panic("fxp_pci_attach: impossible");
309 1.5 thorpej }
310 1.5 thorpej
311 1.1 thorpej /*
312 1.1 thorpej * XXX Perhaps report '557, '558, '559 based on revision?
313 1.1 thorpej */
314 1.5 thorpej printf(": %s, rev %d\n", fpp->fpp_name, PCI_REVISION(pa->pa_class));
315 1.1 thorpej
316 1.1 thorpej /* Make sure bus-mastering is enabled. */
317 1.1 thorpej pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
318 1.1 thorpej pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
319 1.1 thorpej PCI_COMMAND_MASTER_ENABLE);
320 1.1 thorpej
321 1.6 jhawk /*
322 1.6 jhawk * Under some circumstances (such as APM suspend/resume
323 1.6 jhawk * cycles, and across ACPI power state changes), the
324 1.6 jhawk * i82257-family can lose the contents of critical PCI
325 1.6 jhawk * configuration registers, causing the card to be
326 1.6 jhawk * non-responsive and useless. This occurs on the Sony VAIO
327 1.6 jhawk * Z505-series, among others. Preserve them here so they can
328 1.6 jhawk * be later restored (by fxp_pci_confreg_restore()).
329 1.6 jhawk */
330 1.6 jhawk psc->psc_pc = pc;
331 1.6 jhawk psc->psc_tag = pa->pa_tag;
332 1.6 jhawk psc->psc_regs[PCI_COMMAND_STATUS_REG>>2] =
333 1.6 jhawk pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
334 1.6 jhawk psc->psc_regs[PCI_BHLC_REG>>2] =
335 1.6 jhawk pci_conf_read(pc, pa->pa_tag, PCI_BHLC_REG);
336 1.6 jhawk psc->psc_regs[(PCI_MAPREG_START+0x0)>>2] =
337 1.6 jhawk pci_conf_read(pc, pa->pa_tag, PCI_MAPREG_START+0x0);
338 1.6 jhawk psc->psc_regs[(PCI_MAPREG_START+0x4)>>2] =
339 1.6 jhawk pci_conf_read(pc, pa->pa_tag, PCI_MAPREG_START+0x4);
340 1.6 jhawk psc->psc_regs[(PCI_MAPREG_START+0x8)>>2] =
341 1.6 jhawk pci_conf_read(pc, pa->pa_tag, PCI_MAPREG_START+0x8);
342 1.6 jhawk
343 1.6 jhawk /*
344 1.6 jhawk * Work around BIOS ACPI bugs where the chip is inadvertantly
345 1.6 jhawk * left in ACPI D3 (lowest power state). First confirm the device
346 1.6 jhawk * supports ACPI power management, then move it to the D0 (fully
347 1.6 jhawk * functional) state if it is not already there.
348 1.6 jhawk */
349 1.6 jhawk if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT,
350 1.6 jhawk &pci_pwrmgmt_cap_reg, 0)) {
351 1.6 jhawk pcireg_t reg;
352 1.6 jhawk
353 1.6 jhawk pci_pwrmgmt_csr_reg = pci_pwrmgmt_cap_reg + 4;
354 1.6 jhawk reg = pci_conf_read(pc, pa->pa_tag, pci_pwrmgmt_csr_reg);
355 1.6 jhawk if ((reg & PCI_PMCSR_STATE_MASK) != PCI_PMCSR_STATE_D0) {
356 1.6 jhawk pci_conf_write(pc, pa->pa_tag, pci_pwrmgmt_csr_reg,
357 1.6 jhawk (reg & ~PCI_PMCSR_STATE_MASK) |
358 1.6 jhawk PCI_PMCSR_STATE_D0);
359 1.6 jhawk }
360 1.6 jhawk }
361 1.6 jhawk /* Restore PCI configuration registers. */
362 1.6 jhawk fxp_pci_confreg_restore(psc);
363 1.6 jhawk
364 1.1 thorpej /*
365 1.1 thorpej * Map and establish our interrupt.
366 1.1 thorpej */
367 1.1 thorpej if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
368 1.1 thorpej pa->pa_intrline, &ih)) {
369 1.1 thorpej printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
370 1.1 thorpej return;
371 1.1 thorpej }
372 1.1 thorpej intrstr = pci_intr_string(pc, ih);
373 1.8 jhawk sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, fxp_intr, sc);
374 1.8 jhawk if (sc->sc_ih == NULL) {
375 1.1 thorpej printf("%s: couldn't establish interrupt",
376 1.1 thorpej sc->sc_dev.dv_xname);
377 1.1 thorpej if (intrstr != NULL)
378 1.1 thorpej printf(" at %s", intrstr);
379 1.1 thorpej printf("\n");
380 1.1 thorpej return;
381 1.1 thorpej }
382 1.1 thorpej printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
383 1.1 thorpej
384 1.1 thorpej /* Finish off the attach. */
385 1.1 thorpej fxp_attach(sc);
386 1.6 jhawk
387 1.6 jhawk /* Add a suspend hook to restore PCI config state */
388 1.6 jhawk psc->psc_powerhook = powerhook_establish(fxp_pci_power, psc);
389 1.6 jhawk if (psc->psc_powerhook == NULL)
390 1.6 jhawk printf ("%s: WARNING: unable to establish pci power hook\n",
391 1.6 jhawk sc->sc_dev.dv_xname);
392 1.6 jhawk
393 1.1 thorpej }
394