Correct typo in comment
[dragonfly.git] / sys / dev / netif / lnc / if_lnc_pci.c
CommitLineData
6b8fd2d9
MD
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 $
cdf89432 31 * $DragonFly: src/sys/dev/netif/lnc/if_lnc_pci.c,v 1.9 2005/12/31 14:07:59 sephe Exp $
6b8fd2d9
MD
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>
78195a76 39#include <sys/serialize.h>
6b8fd2d9
MD
40
41#include <machine/bus.h>
42#include <machine/resource.h>
43#include <sys/bus.h>
44#include <sys/rman.h>
45
78195a76
MD
46#include <sys/thread2.h>
47
6b8fd2d9
MD
48#include <net/ethernet.h>
49#include <net/if.h>
50#include <net/if_arp.h>
51
52#include <bus/pci/pcireg.h>
53#include <bus/pci/pcivar.h>
54
55#include <dev/netif/lnc/if_lncreg.h>
56#include <dev/netif/lnc/if_lncvar.h>
57
58#define AMD_VENDOR_ID 0x1022
59#define PCI_DEVICE_ID_PCNet_PCI 0x2000
60#define PCI_DEVICE_ID_PCHome_PCI 0x2001
61
62#define LNC_PROBE_PRIORITY -1
63
c97b05f2
JS
64static int lnc_pci_detach(device_t);
65
6b8fd2d9
MD
66static int
67lnc_pci_probe(device_t dev)
68{
69 if (pci_get_vendor(dev) != AMD_VENDOR_ID)
70 return (ENXIO);
71
72 switch(pci_get_device(dev)) {
73 case PCI_DEVICE_ID_PCNet_PCI:
74 device_set_desc(dev, "PCNet/PCI Ethernet adapter");
75 return(LNC_PROBE_PRIORITY);
76 break;
77 case PCI_DEVICE_ID_PCHome_PCI:
78 device_set_desc(dev, "PCHome/PCI Ethernet adapter");
79 return(LNC_PROBE_PRIORITY);
80 break;
81 default:
82 return (ENXIO);
83 break;
84 }
85 return (ENXIO);
86}
87
88static void
89lnc_alloc_callback(void *arg, bus_dma_segment_t *seg, int nseg, int error)
90{
91 /* Do nothing */
92 return;
93}
94
95static int
96lnc_pci_attach(device_t dev)
97{
98 lnc_softc_t *sc = device_get_softc(dev);
99 unsigned command;
100 int rid = 0;
c97b05f2 101 int error = 0;
6b8fd2d9
MD
102 bus_size_t lnc_mem_size;
103
6b8fd2d9
MD
104 command = pci_read_config(dev, PCIR_COMMAND, 4);
105 command |= PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN;
106 pci_write_config(dev, PCIR_COMMAND, command, 4);
107
108 rid = PCIR_MAPS;
4e6d744d
JS
109 sc->portres = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
110 RF_ACTIVE);
6b8fd2d9 111
c97b05f2 112 if (! sc->portres) {
6b8fd2d9 113 device_printf(dev, "Cannot allocate I/O ports\n");
c97b05f2
JS
114 error = ENXIO;
115 goto fail;
116 }
6b8fd2d9
MD
117
118 rid = 0;
4e6d744d
JS
119 sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
120 RF_ACTIVE | RF_SHAREABLE);
6b8fd2d9 121
c97b05f2 122 if (! sc->irqres) {
6b8fd2d9 123 device_printf(dev, "Cannot allocate irq\n");
c97b05f2
JS
124 error = ENXIO;
125 goto fail;
126 }
6b8fd2d9
MD
127
128 sc->lnc_btag = rman_get_bustag(sc->portres);
129 sc->lnc_bhandle = rman_get_bushandle(sc->portres);
130
131 /* XXX temp setting for nic */
132 sc->nic.ic = PCnet_PCI;
133 sc->nic.ident = NE2100;
134 sc->nic.mem_mode = DMA_FIXED;
135 sc->nrdre = NRDRE;
136 sc->ntdre = NTDRE;
137 sc->rap = PCNET_RAP;
138 sc->rdp = PCNET_RDP;
139 sc->bdp = PCNET_BDP;
140
141 /* Create a DMA tag describing the ring memory we need */
142
143 lnc_mem_size = ((NDESC(sc->nrdre) + NDESC(sc->ntdre)) *
144 sizeof(struct host_ring_entry));
145
146 lnc_mem_size += sizeof(struct init_block) + (sizeof(struct mds) *
147 (NDESC(sc->nrdre) + NDESC(sc->ntdre))) + MEM_SLEW;
148
149 lnc_mem_size += (NDESC(sc->nrdre) * RECVBUFSIZE) +
150 (NDESC(sc->ntdre) * TRANSBUFSIZE);
151
c97b05f2
JS
152 error = bus_dma_tag_create(NULL, /* parent */
153 1, /* alignement */
154 0, /* boundary */
155 BUS_SPACE_MAXADDR_24BIT, /* lowaddr */
156 BUS_SPACE_MAXADDR, /* highaddr */
157 NULL, NULL, /* filter, filterarg */
158 lnc_mem_size, /* segsize */
159 1, /* nsegments */
160 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
161 BUS_DMA_ALLOCNOW, /* flags */
162 &sc->dmat);
163
164 if (error) {
6b8fd2d9 165 device_printf(dev, "Can't create DMA tag\n");
c97b05f2 166 goto fail;
6b8fd2d9
MD
167 }
168
c97b05f2
JS
169 error = bus_dmamem_alloc(sc->dmat, (void **)&sc->recv_ring,
170 BUS_DMA_WAITOK, &sc->dmamap);
171 if (error) {
6b8fd2d9 172 device_printf(dev, "Couldn't allocate memory\n");
c97b05f2 173 goto fail;
6b8fd2d9
MD
174 }
175
c97b05f2
JS
176 error = bus_dmamap_load(sc->dmat, sc->dmamap, sc->recv_ring,
177 lnc_mem_size, lnc_alloc_callback,
178 sc->recv_ring, 0);
179 if (error) {
180 device_printf(dev, "Couldn't map receive ring\n");
181 goto fail;
182 }
6b8fd2d9
MD
183
184 /* Call generic attach code */
185 if (! lnc_attach_common(dev)) {
186 device_printf(dev, "Generic attach code failed\n");
c97b05f2
JS
187 error = ENXIO;
188 goto fail;
189 }
190
78195a76
MD
191 error = bus_setup_intr(dev, sc->irqres, INTR_NETSAFE, lncintr,
192 sc, &sc->intrhand,
193 sc->arpcom.ac_if.if_serializer);
c97b05f2
JS
194 if (error) {
195 device_printf(dev, "Cannot setup irq handler\n");
196 ether_ifdetach(&sc->arpcom.ac_if);
197 goto fail;
6b8fd2d9 198 }
c97b05f2 199
6b8fd2d9 200 return (0);
c97b05f2
JS
201
202fail:
203 lnc_pci_detach(dev);
204 return(error);
6b8fd2d9
MD
205}
206
207static int
208lnc_pci_detach(device_t dev)
209{
210 lnc_softc_t *sc = device_get_softc(dev);
6b8fd2d9 211
6b8fd2d9 212
c97b05f2 213 if (device_is_attached(dev)) {
cdf89432 214 lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer);
c97b05f2 215 lnc_stop(sc);
c97b05f2 216 bus_teardown_intr(dev, sc->irqres, sc->intrhand);
cdf89432
SZ
217 lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer);
218
219 ether_ifdetach(&sc->arpcom.ac_if);
220 }
c97b05f2 221
c97b05f2
JS
222 if (sc->irqres)
223 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irqres);
224 if (sc->portres)
225 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_MAPS,
226 sc->portres);
227
228 if (sc->dmamap) {
229 bus_dmamap_unload(sc->dmat, sc->dmamap);
230 bus_dmamem_free(sc->dmat, sc->recv_ring, sc->dmamap);
231 }
232 if (sc->dmat)
233 bus_dma_tag_destroy(sc->dmat);
6b8fd2d9 234
6b8fd2d9
MD
235 return (0);
236}
237
238static device_method_t lnc_pci_methods[] = {
239 DEVMETHOD(device_probe, lnc_pci_probe),
240 DEVMETHOD(device_attach, lnc_pci_attach),
241 DEVMETHOD(device_detach, lnc_pci_detach),
242#ifdef notyet
243 DEVMETHOD(device_suspend, lnc_pci_suspend),
244 DEVMETHOD(device_resume, lnc_pci_resume),
245 DEVMETHOD(device_shutdown, lnc_pci_shutdown),
246#endif
247 { 0, 0 }
248};
249
250static driver_t lnc_pci_driver = {
251 "lnc",
252 lnc_pci_methods,
253 sizeof(struct lnc_softc),
254};
255
256DRIVER_MODULE(if_lnc, pci, lnc_pci_driver, lnc_devclass, 0, 0);