xref: /dragonfly/sys/dev/netif/lnc/if_lnc_pci.c (revision 1bf4b486)
1 /*
2  * Copyright (c) 1994-2000
3  *	Paul Richards. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    verbatim and that no modifications are made prior to this
11  *    point in the file.
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. The name Paul Richards may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY PAUL RICHARDS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL PAUL RICHARDS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD: src/sys/dev/lnc/if_lnc_pci.c,v 1.25 2001/07/04 13:00:19 nyan Exp $
31  * $DragonFly: src/sys/dev/netif/lnc/if_lnc_pci.c,v 1.6 2005/06/13 22:55:15 joerg Exp $
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/socket.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/thread2.h>
40 
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <sys/bus.h>
44 #include <sys/rman.h>
45 
46 #include <net/ethernet.h>
47 #include <net/if.h>
48 #include <net/if_arp.h>
49 
50 #include <bus/pci/pcireg.h>
51 #include <bus/pci/pcivar.h>
52 
53 #include <dev/netif/lnc/if_lncreg.h>
54 #include <dev/netif/lnc/if_lncvar.h>
55 
56 #define AMD_VENDOR_ID 0x1022
57 #define PCI_DEVICE_ID_PCNet_PCI	0x2000
58 #define PCI_DEVICE_ID_PCHome_PCI 0x2001
59 
60 #define LNC_PROBE_PRIORITY -1
61 
62 static int	lnc_pci_detach(device_t);
63 
64 static int
65 lnc_pci_probe(device_t dev)
66 {
67 	if (pci_get_vendor(dev) != AMD_VENDOR_ID)
68 		return (ENXIO);
69 
70 	switch(pci_get_device(dev)) {
71 	case PCI_DEVICE_ID_PCNet_PCI:
72 		device_set_desc(dev, "PCNet/PCI Ethernet adapter");
73 		return(LNC_PROBE_PRIORITY);
74 		break;
75 	case PCI_DEVICE_ID_PCHome_PCI:
76 		device_set_desc(dev, "PCHome/PCI Ethernet adapter");
77 		return(LNC_PROBE_PRIORITY);
78 		break;
79 	default:
80 		return (ENXIO);
81 		break;
82 	}
83 	return (ENXIO);
84 }
85 
86 static void
87 lnc_alloc_callback(void *arg, bus_dma_segment_t *seg, int nseg, int error)
88 {
89 	/* Do nothing */
90 	return;
91 }
92 
93 static int
94 lnc_pci_attach(device_t dev)
95 {
96 	lnc_softc_t *sc = device_get_softc(dev);
97 	unsigned command;
98 	int rid = 0;
99 	int error = 0;
100 	bus_size_t lnc_mem_size;
101 
102 	command = pci_read_config(dev, PCIR_COMMAND, 4);
103 	command |= PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN;
104 	pci_write_config(dev, PCIR_COMMAND, command, 4);
105 
106 	rid = PCIR_MAPS;
107 	sc->portres = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
108 	    RF_ACTIVE);
109 
110 	if (! sc->portres) {
111 		device_printf(dev, "Cannot allocate I/O ports\n");
112 		error = ENXIO;
113 		goto fail;
114 	}
115 
116 	rid = 0;
117 	sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
118 	    RF_ACTIVE | RF_SHAREABLE);
119 
120 	if (! sc->irqres) {
121 		device_printf(dev, "Cannot allocate irq\n");
122 		error = ENXIO;
123 		goto fail;
124 	}
125 
126 	sc->lnc_btag = rman_get_bustag(sc->portres);
127 	sc->lnc_bhandle = rman_get_bushandle(sc->portres);
128 
129 	/* XXX temp setting for nic */
130 	sc->nic.ic = PCnet_PCI;
131 	sc->nic.ident = NE2100;
132 	sc->nic.mem_mode = DMA_FIXED;
133 	sc->nrdre  = NRDRE;
134 	sc->ntdre  = NTDRE;
135 	sc->rap = PCNET_RAP;
136 	sc->rdp = PCNET_RDP;
137 	sc->bdp = PCNET_BDP;
138 
139 	/* Create a DMA tag describing the ring memory we need */
140 
141 	lnc_mem_size = ((NDESC(sc->nrdre) + NDESC(sc->ntdre)) *
142 			 sizeof(struct host_ring_entry));
143 
144 	lnc_mem_size += sizeof(struct init_block) + (sizeof(struct mds) *
145 			(NDESC(sc->nrdre) + NDESC(sc->ntdre))) + MEM_SLEW;
146 
147 	lnc_mem_size += (NDESC(sc->nrdre) * RECVBUFSIZE) +
148 			(NDESC(sc->ntdre) * TRANSBUFSIZE);
149 
150 	error = bus_dma_tag_create(NULL,		/* parent */
151 				   1,			/* alignement */
152 				   0,			/* boundary */
153 				   BUS_SPACE_MAXADDR_24BIT,	/* lowaddr */
154 				   BUS_SPACE_MAXADDR,	/* highaddr */
155 				   NULL, NULL,		/* filter, filterarg */
156 				   lnc_mem_size,	/* segsize */
157 				   1,			/* nsegments */
158 				   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
159 				   BUS_DMA_ALLOCNOW,	/* flags */
160 				   &sc->dmat);
161 
162 	if (error) {
163 		device_printf(dev, "Can't create DMA tag\n");
164 		goto fail;
165 	}
166 
167 	error = bus_dmamem_alloc(sc->dmat, (void **)&sc->recv_ring,
168 	                       BUS_DMA_WAITOK, &sc->dmamap);
169 	if (error) {
170 		device_printf(dev, "Couldn't allocate memory\n");
171 		goto fail;
172 	}
173 
174 	error = bus_dmamap_load(sc->dmat, sc->dmamap, sc->recv_ring,
175 				lnc_mem_size, lnc_alloc_callback,
176 				sc->recv_ring, 0);
177 	if (error) {
178 		device_printf(dev, "Couldn't map receive ring\n");
179 		goto fail;
180 	}
181 
182 	/* Call generic attach code */
183 	if (! lnc_attach_common(dev)) {
184 		device_printf(dev, "Generic attach code failed\n");
185 		error = ENXIO;
186 		goto fail;
187 	}
188 
189 	error = bus_setup_intr(dev, sc->irqres, INTR_TYPE_NET, lncintr,
190 	                     sc, &sc->intrhand, NULL);
191 	if (error) {
192 		device_printf(dev, "Cannot setup irq handler\n");
193 		ether_ifdetach(&sc->arpcom.ac_if);
194 		goto fail;
195 	}
196 
197 	return (0);
198 
199 fail:
200 	lnc_pci_detach(dev);
201 	return(error);
202 }
203 
204 static int
205 lnc_pci_detach(device_t dev)
206 {
207 	lnc_softc_t *sc = device_get_softc(dev);
208 
209 	crit_enter();
210 
211 	if (device_is_attached(dev)) {
212 		ether_ifdetach(&sc->arpcom.ac_if);
213 		lnc_stop(sc);
214 	}
215 
216 	if (sc->intrhand)
217 		bus_teardown_intr(dev, sc->irqres, sc->intrhand);
218 
219 	crit_exit();
220 
221 	if (sc->irqres)
222 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irqres);
223 	if (sc->portres)
224 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_MAPS,
225 				     sc->portres);
226 
227 	if (sc->dmamap) {
228 		bus_dmamap_unload(sc->dmat, sc->dmamap);
229 		bus_dmamem_free(sc->dmat, sc->recv_ring, sc->dmamap);
230 	}
231 	if (sc->dmat)
232 		bus_dma_tag_destroy(sc->dmat);
233 
234 	return (0);
235 }
236 
237 static device_method_t lnc_pci_methods[] = {
238 	DEVMETHOD(device_probe,		lnc_pci_probe),
239 	DEVMETHOD(device_attach,	lnc_pci_attach),
240 	DEVMETHOD(device_detach,	lnc_pci_detach),
241 #ifdef notyet
242 	DEVMETHOD(device_suspend,	lnc_pci_suspend),
243 	DEVMETHOD(device_resume,	lnc_pci_resume),
244 	DEVMETHOD(device_shutdown,	lnc_pci_shutdown),
245 #endif
246 	{ 0, 0 }
247 };
248 
249 static driver_t lnc_pci_driver = {
250 	"lnc",
251 	lnc_pci_methods,
252 	sizeof(struct lnc_softc),
253 };
254 
255 DRIVER_MODULE(if_lnc, pci, lnc_pci_driver, lnc_devclass, 0, 0);
256