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