if_re_pci.c revision 1.23.2.3 1 /* $NetBSD: if_re_pci.c,v 1.23.2.3 2007/03/24 14:55:31 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998-2003
5 * Bill Paul <wpaul (at) windriver.com>. 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 /* $FreeBSD: /repoman/r/ncvs/src/sys/dev/re/if_re.c,v 1.20 2004/04/11 20:34:08 ru Exp $ */
36
37 /*
38 * RealTek 8139C+/8169/8169S/8110S PCI NIC driver
39 *
40 * Written by Bill Paul <wpaul (at) windriver.com>
41 * Senior Networking Software Engineer
42 * Wind River Systems
43 *
44 * NetBSD bus-specific frontends for written by
45 * Jonathan Stone <jonathan (at) netbsd.org>
46 */
47
48 #include "bpfilter.h"
49 #include "vlan.h"
50
51 #include <sys/types.h>
52
53 #include <sys/param.h>
54 #include <sys/endian.h>
55 #include <sys/systm.h>
56 #include <sys/sockio.h>
57 #include <sys/mbuf.h>
58 #include <sys/malloc.h>
59 #include <sys/kernel.h>
60 #include <sys/socket.h>
61 #include <sys/device.h>
62
63 #include <net/if.h>
64 #include <net/if_arp.h>
65 #include <net/if_dl.h>
66 #include <net/if_ether.h>
67 #include <net/if_media.h>
68 #include <net/if_vlanvar.h>
69
70 #include <machine/bus.h>
71
72 #include <dev/mii/mii.h>
73 #include <dev/mii/miivar.h>
74
75 #include <dev/pci/pcireg.h>
76 #include <dev/pci/pcivar.h>
77 #include <dev/pci/pcidevs.h>
78
79 #include <dev/ic/rtl81x9reg.h>
80 #include <dev/ic/rtl81x9var.h>
81 #include <dev/ic/rtl8169var.h>
82
83 struct re_pci_softc {
84 struct rtk_softc sc_rtk;
85
86 void *sc_ih;
87 pci_chipset_tag_t sc_pc;
88 pcitag_t sc_pcitag;
89 };
90
91 static int re_pci_match(struct device *, struct cfdata *, void *);
92 static void re_pci_attach(struct device *, struct device *, void *);
93
94 /*
95 * Various supported device vendors/types and their names.
96 */
97 static const struct rtk_type re_devs[] = {
98 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
99 RTK_8139CPLUS,
100 "RealTek 8139C+ 10/100BaseTX" },
101 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8101E,
102 RTK_8101E,
103 "RealTek 8100E/8101E PCIe 10/100BaseTX" },
104 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168,
105 RTK_8168,
106 "RealTek 8168B/8111B PCIe Gigabit Ethernet" },
107 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169,
108 RTK_8169,
109 "RealTek 8169/8110 Gigabit Ethernet" },
110 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169SC,
111 RTK_8169,
112 "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
113 { PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_LAPCIGT,
114 RTK_8169,
115 "Corega CG-LAPCIGT Gigabit Ethernet" },
116 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE528T,
117 RTK_8169,
118 "D-Link DGE-528T Gigabit Ethernet" },
119 { PCI_VENDOR_USR2, PCI_PRODUCT_USR2_USR997902,
120 RTK_8169,
121 "US Robotics (3Com) USR997902 Gigabit Ethernet" },
122 { PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_EG1032,
123 RTK_8169,
124 "Linksys EG1032 rev. 3 Gigabit Ethernet" },
125 { 0, 0, 0, NULL }
126 };
127
128 #define RE_LINKSYS_EG1032_SUBID 0x00241737
129
130 CFATTACH_DECL(re_pci, sizeof(struct re_pci_softc), re_pci_match, re_pci_attach,
131 NULL, NULL);
132
133 /*
134 * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
135 * IDs against our list and return a device name if we find a match.
136 */
137 static int
138 re_pci_match(struct device *parent, struct cfdata *match, void *aux)
139 {
140 const struct rtk_type *t;
141 struct pci_attach_args *pa = aux;
142 pcireg_t subid;
143
144 subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
145
146 /* special-case Linksys EG1032, since rev 2 uses sk(4) */
147 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_LINKSYS &&
148 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_LINKSYS_EG1032) {
149 if (subid != RE_LINKSYS_EG1032_SUBID)
150 return 0;
151 }
152 /* Don't match 8139 other than C-PLUS */
153 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK &&
154 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8139) {
155 if (PCI_REVISION(pa->pa_class) != 0x20)
156 return 0;
157 }
158
159 t = re_devs;
160
161 while (t->rtk_name != NULL) {
162 if ((PCI_VENDOR(pa->pa_id) == t->rtk_vid) &&
163 (PCI_PRODUCT(pa->pa_id) == t->rtk_did)) {
164 return 2; /* defect rtk(4) */
165 }
166 t++;
167 }
168
169 return 0;
170 }
171
172 static void
173 re_pci_attach(struct device *parent, struct device *self, void *aux)
174 {
175 struct re_pci_softc *psc = (void *)self;
176 struct rtk_softc *sc = &psc->sc_rtk;
177 struct pci_attach_args *pa = aux;
178 pci_chipset_tag_t pc = pa->pa_pc;
179 pci_intr_handle_t ih;
180 const char *intrstr = NULL;
181 const struct rtk_type *t;
182 uint32_t hwrev;
183 int error = 0;
184 pcireg_t pmreg, memtype;
185 bool ioh_valid, memh_valid;
186 pcireg_t command;
187 bus_space_tag_t iot, memt;
188 bus_space_handle_t ioh, memh;
189 bus_size_t iosize, memsize, bsize;
190
191
192 /*
193 * Handle power management nonsense.
194 */
195 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
196 command = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR);
197 if (command & PCI_PMCSR_STATE_MASK) {
198 u_int32_t iobase, membase, irq;
199
200 /* Save important PCI config data. */
201 iobase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOIO);
202 membase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOMEM);
203 irq = pci_conf_read(pc, pa->pa_tag, PCI_INTERRUPT_REG);
204
205 /* Reset the power state. */
206 aprint_normal("%s: chip is is in D%d power mode "
207 "-- setting to D0\n", sc->sc_dev.dv_xname,
208 command & PCI_PMCSR_STATE_MASK);
209
210 command &= ~PCI_PMCSR_STATE_MASK;
211 pci_conf_write(pc, pa->pa_tag,
212 pmreg + PCI_PMCSR, command);
213
214 /* Restore PCI config data. */
215 pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOIO, iobase);
216 pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOMEM, membase);
217 pci_conf_write(pc, pa->pa_tag, PCI_INTERRUPT_REG, irq);
218 }
219 }
220
221 /*
222 * Map control/status registers.
223 */
224 command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
225 command |= PCI_COMMAND_MASTER_ENABLE;
226 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
227
228 ioh_valid = (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
229 &iot, &ioh, NULL, &iosize) == 0);
230 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, RTK_PCI_LOMEM);
231 switch (memtype) {
232 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
233 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
234 memh_valid = (pci_mapreg_map(pa, RTK_PCI_LOMEM,
235 memtype, 0, &memt, &memh, NULL, &memsize) == 0);
236 break;
237 default:
238 memh_valid = 0;
239 break;
240 }
241
242 if (ioh_valid) {
243 sc->rtk_btag = iot;
244 sc->rtk_bhandle = ioh;
245 bsize = iosize;
246 } else if (memh_valid) {
247 sc->rtk_btag = memt;
248 sc->rtk_bhandle = memh;
249 bsize = memsize;
250 } else {
251 aprint_error("%s: can't map registers\n", sc->sc_dev.dv_xname);
252 return;
253 }
254
255 t = re_devs;
256 hwrev = CSR_READ_4(sc, RTK_TXCFG) & RTK_TXCFG_HWREV;
257
258 while (t->rtk_name != NULL) {
259 if ((PCI_VENDOR(pa->pa_id) == t->rtk_vid) &&
260 (PCI_PRODUCT(pa->pa_id) == t->rtk_did)) {
261 break;
262 }
263 t++;
264 }
265
266 if (t->rtk_basetype == RTK_8139CPLUS)
267 sc->sc_quirk |= RTKQ_8139CPLUS;
268
269 if (t->rtk_basetype == RTK_8168 ||
270 t->rtk_basetype == RTK_8101E)
271 sc->sc_quirk |= RTKQ_PCIE;
272
273 aprint_normal(": %s (rev. 0x%02x)\n",
274 t->rtk_name, PCI_REVISION(pa->pa_class));
275
276 sc->sc_dmat = pa->pa_dmat;
277
278 /*
279 * No power/enable/disable machinery for PCI attac;
280 * mark the card enabled now.
281 */
282 sc->sc_flags |= RTK_ENABLED;
283
284 /* Hook interrupt last to avoid having to lock softc */
285 /* Allocate interrupt */
286 if (pci_intr_map(pa, &ih)) {
287 aprint_error("%s: couldn't map interrupt\n",
288 sc->sc_dev.dv_xname);
289 return;
290 }
291 intrstr = pci_intr_string(pc, ih);
292 psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, re_intr, sc);
293 if (psc->sc_ih == NULL) {
294 aprint_error("%s: couldn't establish interrupt",
295 sc->sc_dev.dv_xname);
296 if (intrstr != NULL)
297 aprint_error(" at %s", intrstr);
298 aprint_error("\n");
299 return;
300 }
301 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
302
303 re_attach(sc);
304
305 /*
306 * Perform hardware diagnostic on the original RTL8169.
307 * Some 32-bit cards were incorrectly wired and would
308 * malfunction if plugged into a 64-bit slot.
309 */
310 if (hwrev == RTK_HWREV_8169) {
311 error = re_diag(sc);
312 if (error) {
313 aprint_error(
314 "%s: attach aborted due to hardware diag failure\n",
315 sc->sc_dev.dv_xname);
316
317 re_detach(sc);
318
319 if (psc->sc_ih != NULL) {
320 pci_intr_disestablish(pc, psc->sc_ih);
321 psc->sc_ih = NULL;
322 }
323
324 if (ioh_valid)
325 bus_space_unmap(iot, ioh, iosize);
326 if (memh_valid)
327 bus_space_unmap(memt, memh, memsize);
328 }
329 }
330 }
331