summaryrefslogtreecommitdiffstats
path: root/sys/dev/lnc/if_lnc_pci.c
blob: e35ae61bb7ae97bddc38d9fe3f45476992772d38 (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
/*-
 * Copyright (c) 1994-2000
 *	Paul Richards. 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,
 *    verbatim and that no modifications are made prior to this
 *    point in the file.
 * 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. The name Paul Richards may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY PAUL RICHARDS ``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 PAUL RICHARDS 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.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/socket.h>
#include <sys/malloc.h>
#include <sys/kernel.h>

#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/bus.h>
#include <sys/rman.h>

#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_arp.h>

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

#include <dev/lnc/if_lncreg.h>
#include <dev/lnc/if_lncvar.h>

#define AMD_VENDOR_ID 0x1022
#define PCI_DEVICE_ID_PCNet_PCI	0x2000
#define PCI_DEVICE_ID_PCHome_PCI 0x2001

#define LNC_PROBE_PRIORITY -1

static int
lnc_pci_probe(device_t dev)
{
	if (pci_get_vendor(dev) != AMD_VENDOR_ID)
		return (ENXIO);

	switch(pci_get_device(dev)) {
	case PCI_DEVICE_ID_PCNet_PCI:
		device_set_desc(dev, "PCNet/PCI Ethernet adapter");
		return(LNC_PROBE_PRIORITY);
		break;
	case PCI_DEVICE_ID_PCHome_PCI:
		device_set_desc(dev, "PCHome/PCI Ethernet adapter");
		return(LNC_PROBE_PRIORITY);
		break;
	default:
		return (ENXIO);
		break;
	}
	return (ENXIO);
}

static void
lnc_alloc_callback(void *arg, bus_dma_segment_t *seg, int nseg, int error)
{
	/* Do nothing */
	return;
}

static int
lnc_pci_attach(device_t dev)
{
	lnc_softc_t *sc = device_get_softc(dev);
	unsigned command;
	int err = 0;
	bus_size_t lnc_mem_size;

	device_printf(dev, "Attaching %s\n", device_get_desc(dev));

	command = pci_read_config(dev, PCIR_COMMAND, 4);
	command |= PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN;
	pci_write_config(dev, PCIR_COMMAND, command, 4);

	sc->portrid = PCIR_BAR(0);
	sc->portres = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &sc->portrid,
					     RF_ACTIVE);

	if (! sc->portres) {
		device_printf(dev, "Cannot allocate I/O ports\n");
		lnc_release_resources(dev);
		return (ENXIO);
	}
	
	sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqrid,
					    RF_ACTIVE|RF_SHAREABLE);

	if (! sc->irqres) {
		device_printf(dev, "Cannot allocate irq\n");
		lnc_release_resources(dev);
		return (ENXIO);
	}
	err = bus_setup_intr(dev, sc->irqres, INTR_TYPE_NET, lncintr,
	                     sc, &sc->intrhand);
	if (err) {
		device_printf(dev, "Cannot setup irq handler\n");
		lnc_release_resources(dev);
		return (ENXIO);
	}
	sc->lnc_btag = rman_get_bustag(sc->portres);
	sc->lnc_bhandle = rman_get_bushandle(sc->portres);

	/* XXX temp setting for nic */
	sc->nic.ic = PCnet_PCI;
	sc->nic.ident = NE2100;
	sc->nic.mem_mode = DMA_FIXED;
	sc->nrdre  = NRDRE;
	sc->ntdre  = NTDRE;
	sc->rap = PCNET_RAP;
	sc->rdp = PCNET_RDP;
	sc->bdp = PCNET_BDP;

	/* Create a DMA tag describing the ring memory we need */

	lnc_mem_size = ((NDESC(sc->nrdre) + NDESC(sc->ntdre)) *
			 sizeof(struct host_ring_entry));

	lnc_mem_size += sizeof(struct init_block) + (sizeof(struct mds) *
			(NDESC(sc->nrdre) + NDESC(sc->ntdre))) + MEM_SLEW;

	lnc_mem_size += (NDESC(sc->nrdre) * RECVBUFSIZE) +
			(NDESC(sc->ntdre) * TRANSBUFSIZE);

	err = bus_dma_tag_create(NULL,			/* parent */
				 1,			/* alignement */
				 0,			/* boundary */
				 BUS_SPACE_MAXADDR,	/* lowaddr */
				 BUS_SPACE_MAXADDR,	/* highaddr */
				 NULL, NULL,		/* filter, filterarg */
				 lnc_mem_size,		/* segsize */
				 1,			/* nsegments */
				 BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
				 0,			/* flags */
				 busdma_lock_mutex,	/* lockfunc */
				 &Giant,		/* lockarg */
				 &sc->dmat);

	if (err) {
		device_printf(dev, "Can't create DMA tag\n");
		lnc_release_resources(dev);
		return (ENOMEM);
	}

	err = bus_dmamem_alloc(sc->dmat, (void **)&sc->recv_ring,
	                       BUS_DMA_NOWAIT, &sc->dmamap);

	if (err) {
		device_printf(dev, "Couldn't allocate memory\n");
		lnc_release_resources(dev);
		return (ENOMEM);
	}

	bus_dmamap_load(sc->dmat, sc->dmamap, sc->recv_ring, lnc_mem_size,
			lnc_alloc_callback, sc->recv_ring, BUS_DMA_NOWAIT);

	/* Call generic attach code */
	if (! lnc_attach_common(dev)) {
		device_printf(dev, "Generic attach code failed\n");
		lnc_release_resources(dev);
		return (ENXIO);
	}
	return (0);
}

static int
lnc_pci_detach(device_t dev)
{
	lnc_softc_t *sc = device_get_softc(dev);
	int s = splimp();

	ether_ifdetach(&sc->arpcom.ac_if);

	lnc_stop(sc);
	bus_teardown_intr(dev, sc->irqres, sc->intrhand);
	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irqres);
	bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->portres);

	bus_dmamap_unload(sc->dmat, sc->dmamap);
	bus_dmamem_free(sc->dmat, sc->recv_ring, sc->dmamap);
	bus_dma_tag_destroy(sc->dmat);

	splx(s);
	return (0);
}

static device_method_t lnc_pci_methods[] = {
	DEVMETHOD(device_probe,		lnc_pci_probe),
	DEVMETHOD(device_attach,	lnc_pci_attach),
	DEVMETHOD(device_detach,	lnc_pci_detach),
#ifdef notyet
	DEVMETHOD(device_suspend,	lnc_pci_suspend),
	DEVMETHOD(device_resume,	lnc_pci_resume),
	DEVMETHOD(device_shutdown,	lnc_pci_shutdown),
#endif
	{ 0, 0 }
};

static driver_t lnc_pci_driver = {
	"lnc",
	lnc_pci_methods,
	sizeof(struct lnc_softc),
};

DRIVER_MODULE(lnc, pci, lnc_pci_driver, lnc_devclass, 0, 0);
MODULE_DEPEND(lnc, pci, 1, 1, 1);
MODULE_DEPEND(lnc, ether, 1, 1, 1);
OpenPOWER on IntegriCloud