summaryrefslogtreecommitdiff
path: root/sys/dev/pci/if_rtk_pci.c
blob: 2e38c2db68d8a0f71c85a930b6243e8d2722873b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*	$NetBSD: if_rtk_pci.c,v 1.48 2022/09/24 18:12:42 thorpej Exp $	*/

/*
 * Copyright (c) 1997, 1998
 *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Bill Paul.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 *
 *	FreeBSD Id: if_rl.c,v 1.17 1999/06/19 20:17:37 wpaul Exp
 */

/*
 * Realtek 8129/8139 PCI NIC driver
 *
 * Supports several extremely cheap PCI 10/100 adapters based on
 * the Realtek chipset. Datasheets can be obtained from
 * www.realtek.com.tw.
 *
 * Written by Bill Paul <wpaul@ctr.columbia.edu>
 * Electrical Engineering Department
 * Columbia University, New York City
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_rtk_pci.c,v 1.48 2022/09/24 18:12:42 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/device.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/socket.h>

#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_ether.h>
#include <net/if_dl.h>
#include <net/if_media.h>

#include <sys/bus.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>

#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>

#include <dev/ic/rtl81x9reg.h>
#include <dev/ic/rtl81x9var.h>

struct rtk_pci_softc {
	struct rtk_softc sc_rtk;	/* real rtk softc */

	/* PCI-specific goo.*/
	void *sc_ih;
	pci_chipset_tag_t sc_pc;
};

static const struct rtk_type rtk_pci_devs[] = {
	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8129,
		RTK_8129, "Realtek 8129 10/100BaseTX" },
	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
		RTK_8139, "Realtek 8139 10/100BaseTX" },
	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8138,
		RTK_8139, "Realtek 8138 10/100BaseTX" },
	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139D,
		RTK_8139, "Realtek 8139D 10/100BaseTX" },
	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8100,
		RTK_8139, "Realtek 8100 10/100BaseTX" },
	{ PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_MPX5030,
		RTK_8139, "Accton MPX 5030/5038 10/100BaseTX" },
	{ PCI_VENDOR_DELTA, PCI_PRODUCT_DELTA_8139,
		RTK_8139, "Delta Electronics 8139 10/100BaseTX" },
	{ PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_8139,
		RTK_8139, "Addtron Technology 8139 10/100BaseTX" },
	{ PCI_VENDOR_SEGA, PCI_PRODUCT_SEGA_BROADBAND,
		RTK_8139, "SEGA Broadband Adapter" },
	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE520TX,
		RTK_8139, "D-Link Systems DFE 520TX" }, 
	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE530TXPLUS,
		RTK_8139, "D-Link Systems DFE 530TX+" },
	{ PCI_VENDOR_NORTEL, PCI_PRODUCT_NORTEL_BAYSTACK_21,
		RTK_8139, "Baystack 21 (MPX EN5038) 10/100BaseTX" },
};

static int	rtk_pci_match(device_t, cfdata_t, void *);
static void	rtk_pci_attach(device_t, device_t, void *);
static int	rtk_pci_detach(device_t, int);

CFATTACH_DECL_NEW(rtk_pci, sizeof(struct rtk_pci_softc),
    rtk_pci_match, rtk_pci_attach, rtk_pci_detach, NULL);

static const struct rtk_type *
rtk_pci_lookup(const struct pci_attach_args *pa)
{
	int i;

	for (i = 0; i < __arraycount(rtk_pci_devs); i++) {
		if (PCI_VENDOR(pa->pa_id) != rtk_pci_devs[i].rtk_vid)
			continue;
		if (PCI_PRODUCT(pa->pa_id) == rtk_pci_devs[i].rtk_did)
			return &rtk_pci_devs[i];
	}

	return NULL;
}

static int
rtk_pci_match(device_t parent, cfdata_t cf, void *aux)
{
	struct pci_attach_args *pa = aux;

	if (rtk_pci_lookup(pa) != NULL)
		return 1;

	return 0;
}

/*
 * Attach the interface. Allocate softc structures, do ifmedia
 * setup and ethernet/BPF attach.
 */
static void
rtk_pci_attach(device_t parent, device_t self, void *aux)
{
	struct rtk_pci_softc *psc = device_private(self);
	struct rtk_softc *sc = &psc->sc_rtk;
	struct pci_attach_args *pa = aux;
	pci_chipset_tag_t pc = pa->pa_pc;
	pci_intr_handle_t ih;
	bus_space_tag_t iot, memt;
	bus_space_handle_t ioh, memh;
	bus_size_t iosize, memsize;
	pcireg_t csr;
	const char *intrstr = NULL;
	const struct rtk_type *t;
	bool ioh_valid, memh_valid;
	char intrbuf[PCI_INTRSTR_LEN];

	sc->sc_dev = self;
	psc->sc_pc = pa->pa_pc;

	t = rtk_pci_lookup(pa);
	KASSERT(t != NULL);

	pci_aprint_devinfo_fancy(pa, NULL, t->rtk_name, 1);

	/*
	 * Map control/status registers.
	 *
	 * The original FreeBSD's driver has the following comment:
	 *
	 *   Default to using PIO access for this driver. On SMP systems,
	 *   there appear to be problems with memory mapped mode:
	 *   it looks like doing too many memory mapped access
	 *   back to back in rapid succession can hang the bus.
	 *   I'm inclined to blame this on crummy design/construction
	 *   on the part of Realtek. Memory mapped mode does appear to
	 *   work on uniprocessor systems though.
	 *
	 * On NetBSD, some ports don't support PCI I/O space properly,
	 * so we try to map both and prefer I/O space to mem space.
	 */
	ioh_valid = (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
	    &iot, &ioh, NULL, &iosize) == 0);
	memh_valid = (pci_mapreg_map(pa, RTK_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
	    &memt, &memh, NULL, &memsize) == 0);
	if (ioh_valid) {
		sc->rtk_btag = iot;
		sc->rtk_bhandle = ioh;
		sc->rtk_bsize = iosize;
		if (memh_valid)
			bus_space_unmap(memt, memh, memsize);
	} else if (memh_valid) {
		sc->rtk_btag = memt;
		sc->rtk_bhandle = memh;
		sc->rtk_bsize = memsize;
	} else {
		aprint_error_dev(self, "can't map registers\n");
		return;
	}

	/* Allocate interrupt */
	if (pci_intr_map(pa, &ih)) {
		aprint_error_dev(self, "couldn't map interrupt\n");
		return;
	}
	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
	psc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_NET, rtk_intr, sc,
	    device_xname(self));
	if (psc->sc_ih == NULL) {
		aprint_error_dev(self, "couldn't establish interrupt");
		if (intrstr != NULL)
			aprint_error(" at %s", intrstr);
		aprint_error("\n");
		return;
	}

	if (t->rtk_basetype == RTK_8129)
		sc->sc_quirk |= RTKQ_8129;

	aprint_normal_dev(self, "interrupting at %s\n", intrstr);

	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
	csr |= PCI_COMMAND_MASTER_ENABLE;
	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);

	sc->sc_dmat = pa->pa_dmat;
	sc->sc_flags |= RTK_ENABLED;

	if (pmf_device_register(self, NULL, NULL))
		pmf_class_network_register(self, &sc->ethercom.ec_if);
	else
		aprint_error_dev(self, "couldn't establish power handler\n");

	rtk_attach(sc);
}

static int
rtk_pci_detach(device_t self, int flags)
{
	struct rtk_pci_softc *psc = device_private(self);
	struct rtk_softc *sc = &psc->sc_rtk;
	int rv;

	/* rtk_stop() */
	sc->ethercom.ec_if.if_stop(&sc->ethercom.ec_if, 0);

	rv = rtk_detach(sc);
	if (rv)
		return rv;

	if (psc->sc_ih != NULL) {
		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
		psc->sc_ih = NULL;
	}

	if (sc->rtk_bsize != 0) {
		bus_space_unmap(sc->rtk_btag, sc->rtk_bhandle, sc->rtk_bsize);
		sc->rtk_bsize = 0;
	}

	return 0;
}