]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/lnc/if_lnc_pci.c
This commit was generated by cvs2svn to compensate for changes in r147466,
[FreeBSD/FreeBSD.git] / sys / dev / lnc / if_lnc_pci.c
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
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/module.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 <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52
53 #include <dev/lnc/if_lncreg.h>
54 #include <dev/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 static int
61 lnc_pci_probe(device_t dev)
62 {
63         if (pci_get_vendor(dev) != AMD_VENDOR_ID)
64                 return (ENXIO);
65
66         switch(pci_get_device(dev)) {
67         case PCI_DEVICE_ID_PCNet_PCI:
68                 device_set_desc(dev, "PCNet/PCI Ethernet adapter");
69                 return(BUS_PROBE_DEFAULT);
70                 break;
71         case PCI_DEVICE_ID_PCHome_PCI:
72                 device_set_desc(dev, "PCHome/PCI Ethernet adapter");
73                 return(BUS_PROBE_DEFAULT);
74                 break;
75         default:
76                 return (ENXIO);
77                 break;
78         }
79         return (ENXIO);
80 }
81
82 static void
83 lnc_alloc_callback(void *arg, bus_dma_segment_t *seg, int nseg, int error)
84 {
85         /* Do nothing */
86         return;
87 }
88
89 static int
90 lnc_pci_attach(device_t dev)
91 {
92         lnc_softc_t *sc = device_get_softc(dev);
93         unsigned command;
94         int err = 0;
95         bus_size_t lnc_mem_size;
96
97         device_printf(dev, "Attaching %s\n", device_get_desc(dev));
98
99         command = pci_read_config(dev, PCIR_COMMAND, 4);
100         command |= PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN;
101         pci_write_config(dev, PCIR_COMMAND, command, 4);
102
103         sc->portrid = PCIR_BAR(0);
104         sc->portres = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &sc->portrid,
105                                              RF_ACTIVE);
106
107         if (! sc->portres) {
108                 device_printf(dev, "Cannot allocate I/O ports\n");
109                 lnc_release_resources(dev);
110                 return (ENXIO);
111         }
112         
113         sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqrid,
114                                             RF_ACTIVE|RF_SHAREABLE);
115
116         if (! sc->irqres) {
117                 device_printf(dev, "Cannot allocate irq\n");
118                 lnc_release_resources(dev);
119                 return (ENXIO);
120         }
121         err = bus_setup_intr(dev, sc->irqres, INTR_TYPE_NET, lncintr,
122                              sc, &sc->intrhand);
123         if (err) {
124                 device_printf(dev, "Cannot setup irq handler\n");
125                 lnc_release_resources(dev);
126                 return (ENXIO);
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
152         err = 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                                  0,                     /* flags */
162                                  busdma_lock_mutex,     /* lockfunc */
163                                  &Giant,                /* lockarg */
164                                  &sc->dmat);
165
166         if (err) {
167                 device_printf(dev, "Can't create DMA tag\n");
168                 lnc_release_resources(dev);
169                 return (ENOMEM);
170         }
171
172         err = bus_dmamem_alloc(sc->dmat, (void **)&sc->recv_ring,
173                                BUS_DMA_NOWAIT, &sc->dmamap);
174
175         if (err) {
176                 device_printf(dev, "Couldn't allocate memory\n");
177                 lnc_release_resources(dev);
178                 return (ENOMEM);
179         }
180
181         bus_dmamap_load(sc->dmat, sc->dmamap, sc->recv_ring, lnc_mem_size,
182                         lnc_alloc_callback, sc->recv_ring, BUS_DMA_NOWAIT);
183
184         /* Call generic attach code */
185         if (! lnc_attach_common(dev)) {
186                 device_printf(dev, "Generic attach code failed\n");
187                 lnc_release_resources(dev);
188                 return (ENXIO);
189         }
190         return (0);
191 }
192
193 static device_method_t lnc_pci_methods[] = {
194         DEVMETHOD(device_probe,         lnc_pci_probe),
195         DEVMETHOD(device_attach,        lnc_pci_attach),
196         DEVMETHOD(device_detach,        lnc_detach_common),
197 #ifdef notyet
198         DEVMETHOD(device_suspend,       lnc_pci_suspend),
199         DEVMETHOD(device_resume,        lnc_pci_resume),
200         DEVMETHOD(device_shutdown,      lnc_pci_shutdown),
201 #endif
202         { 0, 0 }
203 };
204
205 static driver_t lnc_pci_driver = {
206         "lnc",
207         lnc_pci_methods,
208         sizeof(struct lnc_softc),
209 };
210
211 DRIVER_MODULE(lnc, pci, lnc_pci_driver, lnc_devclass, 0, 0);
212 MODULE_DEPEND(lnc, pci, 1, 1, 1);
213 MODULE_DEPEND(lnc, ether, 1, 1, 1);