if_fxp_pci.c revision 1.73 1 /* $NetBSD: if_fxp_pci.c,v 1.73 2010/02/24 22:38:00 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * PCI bus front-end for the Intel i82557 fast Ethernet controller
35 * driver. Works with Intel Etherexpress Pro 10+, 100B, 100+ cards.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: if_fxp_pci.c,v 1.73 2010/02/24 22:38:00 dyoung Exp $");
40
41 #include "rnd.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/malloc.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/ioctl.h>
50 #include <sys/errno.h>
51 #include <sys/device.h>
52
53 #if NRND > 0
54 #include <sys/rnd.h>
55 #endif
56
57 #include <machine/endian.h>
58
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_media.h>
62 #include <net/if_ether.h>
63
64 #include <sys/bus.h>
65 #include <sys/intr.h>
66
67 #include <dev/mii/miivar.h>
68
69 #include <dev/ic/i82557reg.h>
70 #include <dev/ic/i82557var.h>
71
72 #include <dev/pci/pcivar.h>
73 #include <dev/pci/pcireg.h>
74 #include <dev/pci/pcidevs.h>
75
76 struct fxp_pci_softc {
77 struct fxp_softc psc_fxp;
78
79 pci_chipset_tag_t psc_pc; /* pci chipset tag */
80 pcireg_t psc_regs[0x20>>2]; /* saved PCI config regs (sparse) */
81 pcitag_t psc_tag; /* pci register tag */
82
83 int psc_pwrmgmt_csr_reg; /* ACPI power management register */
84 pcireg_t psc_pwrmgmt_csr; /* ...and the contents at D0 */
85 struct pci_conf_state psc_pciconf; /* standard PCI configuration regs */
86 };
87
88 static int fxp_pci_match(device_t, cfdata_t, void *);
89 static void fxp_pci_attach(device_t, device_t, void *);
90
91 static int fxp_pci_enable(struct fxp_softc *);
92 static void fxp_pci_disable(struct fxp_softc *);
93
94 static void fxp_pci_confreg_restore(struct fxp_pci_softc *psc);
95 static bool fxp_pci_resume(device_t dv, const pmf_qual_t *);
96
97 CFATTACH_DECL_NEW(fxp_pci, sizeof(struct fxp_pci_softc),
98 fxp_pci_match, fxp_pci_attach, NULL, NULL);
99
100 static const struct fxp_pci_product {
101 uint32_t fpp_prodid; /* PCI product ID */
102 const char *fpp_name; /* device name */
103 } fxp_pci_products[] = {
104 { PCI_PRODUCT_INTEL_82557,
105 "Intel i82557 Ethernet" },
106 { PCI_PRODUCT_INTEL_82559ER,
107 "Intel i82559ER Ethernet" },
108 { PCI_PRODUCT_INTEL_IN_BUSINESS,
109 "Intel InBusiness Ethernet" },
110 { PCI_PRODUCT_INTEL_82801BA_LAN,
111 "Intel i82562 Ethernet" },
112 { PCI_PRODUCT_INTEL_82801E_LAN_1,
113 "Intel i82801E Ethernet" },
114 { PCI_PRODUCT_INTEL_82801E_LAN_2,
115 "Intel i82801E Ethernet" },
116 { PCI_PRODUCT_INTEL_PRO_100_VE_0,
117 "Intel PRO/100 VE Network Controller" },
118 { PCI_PRODUCT_INTEL_PRO_100_VE_1,
119 "Intel PRO/100 VE Network Controller" },
120 { PCI_PRODUCT_INTEL_PRO_100_VE_2,
121 "Intel PRO/100 VE Network Controller with 82562ET/EZ PHY" },
122 { PCI_PRODUCT_INTEL_PRO_100_VE_3,
123 "Intel PRO/100 VE Network Controller with 82562ET/EZ (CNR) PHY" },
124 { PCI_PRODUCT_INTEL_PRO_100_VE_4,
125 "Intel PRO/100 VE (MOB) Network Controller" },
126 { PCI_PRODUCT_INTEL_PRO_100_VE_5,
127 "Intel PRO/100 VE (LOM) Network Controller" },
128 { PCI_PRODUCT_INTEL_PRO_100_VE_6,
129 "Intel PRO/100 VE Network Controller" },
130 { PCI_PRODUCT_INTEL_PRO_100_VE_7,
131 "Intel PRO/100 VE Network Controller" },
132 { PCI_PRODUCT_INTEL_PRO_100_VE_8,
133 "Intel PRO/100 VE Network Controller" },
134 { PCI_PRODUCT_INTEL_PRO_100_VM_0,
135 "Intel PRO/100 VM Network Controller" },
136 { PCI_PRODUCT_INTEL_PRO_100_VM_1,
137 "Intel PRO/100 VM Network Controller" },
138 { PCI_PRODUCT_INTEL_PRO_100_VM_2,
139 "Intel PRO/100 VM Network Controller" },
140 { PCI_PRODUCT_INTEL_PRO_100_VM_3,
141 "Intel PRO/100 VM Network Controller with 82562EM/EX PHY" },
142 { PCI_PRODUCT_INTEL_PRO_100_VM_4,
143 "Intel PRO/100 VM Network Controller with 82562EM/EX (CNR) PHY" },
144 { PCI_PRODUCT_INTEL_PRO_100_VM_5,
145 "Intel PRO/100 VM (MOB) Network Controller" },
146 { PCI_PRODUCT_INTEL_PRO_100_VM_6,
147 "Intel PRO/100 VM Network Controller with 82562ET/EZ PHY" },
148 { PCI_PRODUCT_INTEL_PRO_100_M,
149 "Intel PRO/100 M Network Controller" },
150 { PCI_PRODUCT_INTEL_82801EB_LAN,
151 "Intel 82801EB/ER (ICH5) Network Controller" },
152 { PCI_PRODUCT_INTEL_82801FB_LAN,
153 "Intel 82562EZ (ICH6)" },
154 { PCI_PRODUCT_INTEL_82801G_LAN,
155 "Intel 82801GB/GR (ICH7) Network Controller" },
156 { PCI_PRODUCT_INTEL_82801GB_LAN,
157 "Intel 82801GB 10/100 Network Controller" },
158 { 0,
159 NULL },
160 };
161
162 static const struct fxp_pci_product *
163 fxp_pci_lookup(const struct pci_attach_args *pa)
164 {
165 const struct fxp_pci_product *fpp;
166
167 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
168 return (NULL);
169
170 for (fpp = fxp_pci_products; fpp->fpp_name != NULL; fpp++)
171 if (PCI_PRODUCT(pa->pa_id) == fpp->fpp_prodid)
172 return (fpp);
173
174 return (NULL);
175 }
176
177 static int
178 fxp_pci_match(device_t parent, cfdata_t match, void *aux)
179 {
180 struct pci_attach_args *pa = aux;
181
182 if (fxp_pci_lookup(pa) != NULL)
183 return (1);
184
185 return (0);
186 }
187
188 /*
189 * On resume : (XXX it is necessary with new pmf framework ?)
190 * Restore PCI configuration registers that may have been clobbered.
191 * This is necessary due to bugs on the Sony VAIO Z505-series on-board
192 * ethernet, after an APM suspend/resume, as well as after an ACPI
193 * D3->D0 transition. We call this function from a power hook after
194 * APM resume events, as well as after the ACPI D3->D0 transition.
195 */
196 static void
197 fxp_pci_confreg_restore(struct fxp_pci_softc *psc)
198 {
199 pcireg_t reg;
200
201 #if 0
202 /*
203 * Check to see if the command register is blank -- if so, then
204 * we'll assume that all the clobberable-registers have been
205 * clobbered.
206 */
207
208 /*
209 * In general, the above metric is accurate. Unfortunately,
210 * it is inaccurate across a hibernation. Ideally APM/ACPI
211 * code should take note of hibernation events and execute
212 * a hibernation wakeup hook, but at present a hibernation wake
213 * is indistinguishable from a suspend wake.
214 */
215
216 if (((reg = pci_conf_read(psc->psc_pc, psc->psc_tag,
217 PCI_COMMAND_STATUS_REG)) & 0xffff) != 0)
218 return;
219 #else
220 reg = pci_conf_read(psc->psc_pc, psc->psc_tag, PCI_COMMAND_STATUS_REG);
221 #endif
222
223 pci_conf_write(psc->psc_pc, psc->psc_tag,
224 PCI_COMMAND_STATUS_REG,
225 (reg & 0xffff0000) |
226 (psc->psc_regs[PCI_COMMAND_STATUS_REG>>2] & 0xffff));
227 pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_BHLC_REG,
228 psc->psc_regs[PCI_BHLC_REG>>2]);
229 pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_MAPREG_START+0x0,
230 psc->psc_regs[(PCI_MAPREG_START+0x0)>>2]);
231 pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_MAPREG_START+0x4,
232 psc->psc_regs[(PCI_MAPREG_START+0x4)>>2]);
233 pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_MAPREG_START+0x8,
234 psc->psc_regs[(PCI_MAPREG_START+0x8)>>2]);
235 }
236
237 static bool
238 fxp_pci_resume(device_t dv, const pmf_qual_t *qual)
239 {
240 struct fxp_pci_softc *psc = device_private(dv);
241 fxp_pci_confreg_restore(psc);
242
243 return true;
244 }
245
246 static void
247 fxp_pci_attach(device_t parent, device_t self, void *aux)
248 {
249 struct fxp_pci_softc *psc = device_private(self);
250 struct fxp_softc *sc = &psc->psc_fxp;
251 struct pci_attach_args *pa = aux;
252 pci_chipset_tag_t pc = pa->pa_pc;
253 pci_intr_handle_t ih;
254 const struct fxp_pci_product *fpp;
255 const char *chipname = NULL;
256 const char *intrstr = NULL;
257 bus_space_tag_t iot, memt;
258 bus_space_handle_t ioh, memh;
259 int ioh_valid, memh_valid;
260 bus_addr_t addr;
261 bus_size_t size;
262 int flags;
263 int error;
264
265 sc->sc_dev = self;
266
267 aprint_naive(": Ethernet controller\n");
268
269 /*
270 * Map control/status registers.
271 */
272 ioh_valid = (pci_mapreg_map(pa, FXP_PCI_IOBA,
273 PCI_MAPREG_TYPE_IO, 0,
274 &iot, &ioh, NULL, NULL) == 0);
275
276 /*
277 * Version 2.1 of the PCI spec, page 196, "Address Maps":
278 *
279 * Prefetchable
280 *
281 * Set to one if there are no side effects on reads, the
282 * device returns all bytes regardless of the byte enables,
283 * and host bridges can merge processor writes into this
284 * range without causing errors. Bit must be set to zero
285 * otherwise.
286 *
287 * The 82557 incorrectly sets the "prefetchable" bit, resulting
288 * in errors on systems which will do merged reads and writes.
289 * These errors manifest themselves as all-bits-set when reading
290 * from the EEPROM or other < 4 byte registers.
291 *
292 * We must work around this problem by always forcing the mapping
293 * for memory space to be uncacheable. On systems which cannot
294 * create an uncacheable mapping (because the firmware mapped it
295 * into only cacheable/prefetchable space due to the "prefetchable"
296 * bit), we can fall back onto i/o mapped access.
297 */
298 memh_valid = 0;
299 memt = pa->pa_memt;
300 if (((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) != 0) &&
301 pci_mapreg_info(pa->pa_pc, pa->pa_tag, FXP_PCI_MMBA,
302 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT,
303 &addr, &size, &flags) == 0) {
304 flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
305 if (bus_space_map(memt, addr, size, flags, &memh) == 0)
306 memh_valid = 1;
307 }
308
309 if (memh_valid) {
310 sc->sc_st = memt;
311 sc->sc_sh = memh;
312 } else if (ioh_valid) {
313 sc->sc_st = iot;
314 sc->sc_sh = ioh;
315 } else {
316 aprint_error(": unable to map device registers\n");
317 return;
318 }
319
320 sc->sc_dmat = pa->pa_dmat;
321
322 fpp = fxp_pci_lookup(pa);
323 if (fpp == NULL) {
324 printf("\n");
325 panic("fxp_pci_attach: impossible");
326 }
327
328 sc->sc_rev = PCI_REVISION(pa->pa_class);
329
330 switch (fpp->fpp_prodid) {
331 case PCI_PRODUCT_INTEL_82557:
332 case PCI_PRODUCT_INTEL_IN_BUSINESS:
333
334 if (sc->sc_rev >= FXP_REV_82558_A4) {
335 chipname = "i82558 Ethernet";
336 sc->sc_flags |= FXPF_FC|FXPF_EXT_TXCB;
337 /*
338 * Enable the MWI command for memory writes.
339 */
340 if (pa->pa_flags & PCI_FLAGS_MWI_OKAY)
341 sc->sc_flags |= FXPF_MWI;
342 }
343 if (sc->sc_rev >= FXP_REV_82559_A0) {
344 chipname = "i82559 Ethernet";
345 sc->sc_flags |= FXPF_82559_RXCSUM;
346 }
347 if (sc->sc_rev >= FXP_REV_82559S_A)
348 chipname = "i82559S Ethernet";
349 if (sc->sc_rev >= FXP_REV_82550) {
350 chipname = "i82550 Ethernet";
351 sc->sc_flags &= ~FXPF_82559_RXCSUM;
352 sc->sc_flags |= FXPF_EXT_RFA;
353 }
354 if (sc->sc_rev >= FXP_REV_82551)
355 chipname = "i82551 Ethernet";
356
357 /*
358 * Mark all i82559 and i82550 revisions as having
359 * the "resume bug". See i82557.c for details.
360 */
361 if (sc->sc_rev >= FXP_REV_82559_A0)
362 sc->sc_flags |= FXPF_HAS_RESUME_BUG;
363
364 aprint_normal(": %s, rev %d\n", chipname != NULL ? chipname :
365 fpp->fpp_name, sc->sc_rev);
366 break;
367
368 case PCI_PRODUCT_INTEL_82559ER:
369 sc->sc_flags |= FXPF_FC|FXPF_EXT_TXCB;
370
371 /*
372 * i82559ER/82551ER don't support RX hardware checksumming
373 * even though it has a newer revision number than 82559_A0.
374 */
375
376 /* All i82559 have the "resume bug". */
377 sc->sc_flags |= FXPF_HAS_RESUME_BUG;
378
379 /* Enable the MWI command for memory writes. */
380 if (pa->pa_flags & PCI_FLAGS_MWI_OKAY)
381 sc->sc_flags |= FXPF_MWI;
382
383 if (sc->sc_rev >= FXP_REV_82551)
384 chipname = "Intel i82551ER Ethernet";
385
386 aprint_normal(": %s, rev %d\n", chipname != NULL ? chipname :
387 fpp->fpp_name, sc->sc_rev);
388 break;
389
390 case PCI_PRODUCT_INTEL_82801BA_LAN:
391 case PCI_PRODUCT_INTEL_PRO_100_VE_0:
392 case PCI_PRODUCT_INTEL_PRO_100_VE_1:
393 case PCI_PRODUCT_INTEL_PRO_100_VM_0:
394 case PCI_PRODUCT_INTEL_PRO_100_VM_1:
395 case PCI_PRODUCT_INTEL_82562EH_HPNA_0:
396 case PCI_PRODUCT_INTEL_82562EH_HPNA_1:
397 case PCI_PRODUCT_INTEL_82562EH_HPNA_2:
398 case PCI_PRODUCT_INTEL_PRO_100_VM_2:
399 /*
400 * The ICH-2 and ICH-3 have the "resume bug".
401 */
402 sc->sc_flags |= FXPF_HAS_RESUME_BUG;
403 /* FALLTHROUGH */
404
405 default:
406 aprint_normal(": %s, rev %d\n", fpp->fpp_name, sc->sc_rev);
407 if (sc->sc_rev >= FXP_REV_82558_A4)
408 sc->sc_flags |= FXPF_FC|FXPF_EXT_TXCB;
409 if (sc->sc_rev >= FXP_REV_82559_A0)
410 sc->sc_flags |= FXPF_82559_RXCSUM;
411
412 break;
413 }
414
415 /* Make sure bus-mastering is enabled. */
416 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
417 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
418 PCI_COMMAND_MASTER_ENABLE);
419
420 /*
421 * Under some circumstances (such as APM suspend/resume
422 * cycles, and across ACPI power state changes), the
423 * i82257-family can lose the contents of critical PCI
424 * configuration registers, causing the card to be
425 * non-responsive and useless. This occurs on the Sony VAIO
426 * Z505-series, among others. Preserve them here so they can
427 * be later restored (by fxp_pci_confreg_restore()).
428 */
429 psc->psc_pc = pc;
430 psc->psc_tag = pa->pa_tag;
431 psc->psc_regs[PCI_COMMAND_STATUS_REG>>2] =
432 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
433 psc->psc_regs[PCI_BHLC_REG>>2] =
434 pci_conf_read(pc, pa->pa_tag, PCI_BHLC_REG);
435 psc->psc_regs[(PCI_MAPREG_START+0x0)>>2] =
436 pci_conf_read(pc, pa->pa_tag, PCI_MAPREG_START+0x0);
437 psc->psc_regs[(PCI_MAPREG_START+0x4)>>2] =
438 pci_conf_read(pc, pa->pa_tag, PCI_MAPREG_START+0x4);
439 psc->psc_regs[(PCI_MAPREG_START+0x8)>>2] =
440 pci_conf_read(pc, pa->pa_tag, PCI_MAPREG_START+0x8);
441
442 /* power up chip */
443 switch ((error = pci_activate(pa->pa_pc, pa->pa_tag, self,
444 pci_activate_null))) {
445 case EOPNOTSUPP:
446 break;
447 case 0:
448 sc->sc_enable = fxp_pci_enable;
449 sc->sc_disable = fxp_pci_disable;
450 break;
451 default:
452 aprint_error_dev(self, "cannot activate %d\n", error);
453 return;
454 }
455
456 /* Restore PCI configuration registers. */
457 fxp_pci_confreg_restore(psc);
458
459 sc->sc_enabled = 1;
460
461 /*
462 * Map and establish our interrupt.
463 */
464 if (pci_intr_map(pa, &ih)) {
465 aprint_error_dev(self, "couldn't map interrupt\n");
466 return;
467 }
468 intrstr = pci_intr_string(pc, ih);
469 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, fxp_intr, sc);
470 if (sc->sc_ih == NULL) {
471 aprint_error_dev(self, "couldn't establish interrupt");
472 if (intrstr != NULL)
473 aprint_error(" at %s", intrstr);
474 aprint_error("\n");
475 return;
476 }
477 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
478
479 /* Finish off the attach. */
480 fxp_attach(sc);
481 if (sc->sc_disable != NULL)
482 fxp_disable(sc);
483
484 /* Add a suspend hook to restore PCI config state */
485 if (pmf_device_register(self, NULL, fxp_pci_resume))
486 pmf_class_network_register(self, &sc->sc_ethercom.ec_if);
487 else
488 aprint_error_dev(self, "couldn't establish power handler\n");
489 }
490
491 static int
492 fxp_pci_enable(struct fxp_softc *sc)
493 {
494 struct fxp_pci_softc *psc = (void *) sc;
495
496 #if 0
497 printf("%s: going to power state D0\n", device_xname(self));
498 #endif
499
500 /* Bring the device into D0 power state. */
501 pci_conf_write(psc->psc_pc, psc->psc_tag,
502 psc->psc_pwrmgmt_csr_reg, psc->psc_pwrmgmt_csr);
503
504 /* Now restore the configuration registers. */
505 fxp_pci_confreg_restore(psc);
506
507 return (0);
508 }
509
510 static void
511 fxp_pci_disable(struct fxp_softc *sc)
512 {
513 struct fxp_pci_softc *psc = (void *) sc;
514
515 /*
516 * for some 82558_A4 and 82558_B0, entering D3 state makes
517 * media detection disordered.
518 */
519 if (sc->sc_rev <= FXP_REV_82558_B0)
520 return;
521
522 #if 0
523 printf("%s: going to power state D3\n", device_xname(self));
524 #endif
525
526 /* Put the device into D3 state. */
527 pci_conf_write(psc->psc_pc, psc->psc_tag,
528 psc->psc_pwrmgmt_csr_reg, (psc->psc_pwrmgmt_csr &
529 ~PCI_PMCSR_STATE_MASK) | PCI_PMCSR_STATE_D3);
530 }
531