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