1 /*- 2 * Copyright (c) 2000 Michael Smith 3 * Copyright (c) 2001 Scott Long 4 * Copyright (c) 2000 BSDi 5 * Copyright (c) 2001 Adaptec, Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: src/sys/dev/aac/aac_pci.c,v 1.3.2.19 2003/11/01 18:44:51 scottl Exp $ 30 * $DragonFly: src/sys/dev/raid/aac/aac_pci.c,v 1.10 2006/10/25 20:56:00 dillon Exp $ 31 */ 32 33 /* 34 * PCI bus interface and resource allocation. 35 */ 36 37 #include "opt_aac.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 43 #include "aac_compat.h" 44 #include <sys/bus.h> 45 #include <sys/conf.h> 46 #include <sys/devicestat.h> 47 #include <sys/disk.h> 48 #include <sys/rman.h> 49 50 #include <bus/pci/pcireg.h> 51 #include <bus/pci/pcivar.h> 52 53 #include "aacreg.h" 54 #include "aac_ioctl.h" 55 #include "aacvar.h" 56 57 static int aac_pci_probe(device_t dev); 58 static int aac_pci_attach(device_t dev); 59 60 static device_method_t aac_methods[] = { 61 /* Device interface */ 62 DEVMETHOD(device_probe, aac_pci_probe), 63 DEVMETHOD(device_attach, aac_pci_attach), 64 DEVMETHOD(device_detach, aac_detach), 65 DEVMETHOD(device_suspend, aac_suspend), 66 DEVMETHOD(device_resume, aac_resume), 67 68 DEVMETHOD(bus_print_child, bus_generic_print_child), 69 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 70 { 0, 0 } 71 }; 72 73 static driver_t aac_pci_driver = { 74 "aac", 75 aac_methods, 76 sizeof(struct aac_softc) 77 }; 78 79 devclass_t aac_devclass; 80 81 DRIVER_MODULE(aac, pci, aac_pci_driver, aac_devclass, 0, 0); 82 83 struct aac_ident 84 { 85 u_int16_t vendor; 86 u_int16_t device; 87 u_int16_t subvendor; 88 u_int16_t subdevice; 89 int hwif; 90 int quirks; 91 char *desc; 92 } aac_identifiers[] = { 93 {0x1028, 0x0001, 0x1028, 0x0001, AAC_HWIF_I960RX, 0, 94 "Dell PERC 2/Si"}, 95 {0x1028, 0x0002, 0x1028, 0x0002, AAC_HWIF_I960RX, 0, 96 "Dell PERC 3/Di"}, 97 {0x1028, 0x0003, 0x1028, 0x0003, AAC_HWIF_I960RX, 0, 98 "Dell PERC 3/Si"}, 99 {0x1028, 0x0004, 0x1028, 0x00d0, AAC_HWIF_I960RX, 0, 100 "Dell PERC 3/Si"}, 101 {0x1028, 0x0002, 0x1028, 0x00d1, AAC_HWIF_I960RX, 0, 102 "Dell PERC 3/Di"}, 103 {0x1028, 0x0002, 0x1028, 0x00d9, AAC_HWIF_I960RX, 0, 104 "Dell PERC 3/Di"}, 105 {0x1028, 0x000a, 0x1028, 0x0106, AAC_HWIF_I960RX, 0, 106 "Dell PERC 3/Di"}, 107 {0x1028, 0x000a, 0x1028, 0x011b, AAC_HWIF_I960RX, 0, 108 "Dell PERC 3/Di"}, 109 {0x1028, 0x000a, 0x1028, 0x0121, AAC_HWIF_I960RX, 0, 110 "Dell PERC 3/Di"}, 111 {0x1011, 0x0046, 0x9005, 0x0364, AAC_HWIF_STRONGARM, 0, 112 "Adaptec AAC-364"}, 113 {0x1011, 0x0046, 0x9005, 0x0365, AAC_HWIF_STRONGARM, 0, 114 "Adaptec SCSI RAID 5400S"}, 115 {0x1011, 0x0046, 0x9005, 0x1364, AAC_HWIF_STRONGARM, AAC_FLAGS_PERC2QC, 116 "Dell PERC 2/QC"}, 117 {0x1011, 0x0046, 0x103c, 0x10c2, AAC_HWIF_STRONGARM, 0, 118 "HP NetRaid-4M"}, 119 {0x9005, 0x0285, 0x9005, 0x0285, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB, 120 "Adaptec SCSI RAID 2200S"}, 121 {0x9005, 0x0285, 0x1028, 0x0287, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB, 122 "Dell PERC 320/DC"}, 123 {0x9005, 0x0285, 0x9005, 0x0286, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB, 124 "Adaptec SCSI RAID 2120S"}, 125 {0x9005, 0x0285, 0x9005, 0x0290, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB, 126 "Adaptec SCSI RAID 2410SA"}, 127 {0x9005, 0x0285, 0x1028, 0x0291, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB, 128 "Dell CERC SATA RAID 2"}, 129 {0x9005, 0x0285, 0x9005, 0x0292, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB, 130 "Adaptec SCSI RAID 2810SA"}, 131 {0, 0, 0, 0, 0, 0, 0} 132 }; 133 134 /* 135 * Determine whether this is one of our supported adapters. 136 */ 137 static int 138 aac_pci_probe(device_t dev) 139 { 140 struct aac_ident *m; 141 142 debug_called(1); 143 144 for (m = aac_identifiers; m->vendor != 0; m++) { 145 if ((m->vendor == pci_get_vendor(dev)) && 146 (m->device == pci_get_device(dev)) && 147 ((m->subvendor == 0) || (m->subvendor == 148 pci_get_subvendor(dev))) && 149 ((m->subdevice == 0) || ((m->subdevice == 150 pci_get_subdevice(dev))))) { 151 152 device_set_desc(dev, m->desc); 153 return(-10); /* allow room to be overridden */ 154 } 155 } 156 return(ENXIO); 157 } 158 159 /* 160 * Allocate resources for our device, set up the bus interface. 161 */ 162 static int 163 aac_pci_attach(device_t dev) 164 { 165 struct aac_softc *sc; 166 int i, error; 167 u_int32_t command; 168 169 debug_called(1); 170 171 /* 172 * Initialise softc. 173 */ 174 sc = device_get_softc(dev); 175 bzero(sc, sizeof(*sc)); 176 sc->aac_dev = dev; 177 178 /* assume failure is 'not configured' */ 179 error = ENXIO; 180 181 /* 182 * Verify that the adapter is correctly set up in PCI space. 183 */ 184 command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2); 185 command |= PCIM_CMD_BUSMASTEREN; 186 pci_write_config(dev, PCIR_COMMAND, command, 2); 187 command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2); 188 if (!(command & PCIM_CMD_BUSMASTEREN)) { 189 device_printf(sc->aac_dev, "can't enable bus-master feature\n"); 190 goto out; 191 } 192 if ((command & PCIM_CMD_MEMEN) == 0) { 193 device_printf(sc->aac_dev, "memory window not available\n"); 194 goto out; 195 } 196 197 /* 198 * Allocate the PCI register window. 199 */ 200 sc->aac_regs_rid = 0x10; /* first base address register */ 201 if ((sc->aac_regs_resource = bus_alloc_resource(sc->aac_dev, 202 SYS_RES_MEMORY, 203 &sc->aac_regs_rid, 204 0, ~0, 1, 205 RF_ACTIVE)) == NULL) { 206 device_printf(sc->aac_dev, 207 "couldn't allocate register window\n"); 208 goto out; 209 } 210 sc->aac_btag = rman_get_bustag(sc->aac_regs_resource); 211 sc->aac_bhandle = rman_get_bushandle(sc->aac_regs_resource); 212 213 /* 214 * Allocate and connect our interrupt. 215 */ 216 sc->aac_irq_rid = 0; 217 if ((sc->aac_irq = bus_alloc_resource(sc->aac_dev, SYS_RES_IRQ, 218 &sc->aac_irq_rid, 0, ~0, 1, 219 RF_SHAREABLE | 220 RF_ACTIVE)) == NULL) { 221 device_printf(sc->aac_dev, "can't allocate interrupt\n"); 222 goto out; 223 } 224 if (bus_setup_intr(sc->aac_dev, sc->aac_irq, 0, 225 aac_intr, sc, &sc->aac_intr, NULL)) { 226 device_printf(sc->aac_dev, "can't set up interrupt\n"); 227 goto out; 228 } 229 230 /* assume failure is 'out of memory' */ 231 error = ENOMEM; 232 233 /* 234 * Allocate the parent bus DMA tag appropriate for our PCI interface. 235 * 236 * Note that some of these controllers are 64-bit capable. 237 */ 238 if (bus_dma_tag_create(NULL, /* parent */ 239 PAGE_SIZE, 0, /* algnmnt, boundary */ 240 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 241 BUS_SPACE_MAXADDR, /* highaddr */ 242 NULL, NULL, /* filter, filterarg */ 243 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */ 244 AAC_MAXSGENTRIES, /* nsegments */ 245 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 246 0, /* flags */ 247 &sc->aac_parent_dmat)) { 248 device_printf(sc->aac_dev, "can't allocate parent DMA tag\n"); 249 goto out; 250 } 251 252 /* 253 * Detect the hardware interface version, set up the bus interface 254 * indirection. 255 */ 256 sc->aac_hwif = AAC_HWIF_UNKNOWN; 257 for (i = 0; aac_identifiers[i].vendor != 0; i++) { 258 if ((aac_identifiers[i].vendor == pci_get_vendor(dev)) && 259 (aac_identifiers[i].device == pci_get_device(dev)) && 260 (aac_identifiers[i].subvendor == pci_get_subvendor(dev)) && 261 (aac_identifiers[i].subdevice == pci_get_subdevice(dev))) { 262 sc->aac_hwif = aac_identifiers[i].hwif; 263 switch(sc->aac_hwif) { 264 case AAC_HWIF_I960RX: 265 debug(2, "set hardware up for i960Rx"); 266 sc->aac_if = aac_rx_interface; 267 break; 268 269 case AAC_HWIF_STRONGARM: 270 debug(2, "set hardware up for StrongARM"); 271 sc->aac_if = aac_sa_interface; 272 break; 273 case AAC_HWIF_FALCON: 274 debug(2, "set hardware up for Falcon/PPC"); 275 sc->aac_if = aac_fa_interface; 276 break; 277 } 278 279 /* Set up quirks */ 280 sc->flags = aac_identifiers[i].quirks; 281 282 break; 283 } 284 } 285 if (sc->aac_hwif == AAC_HWIF_UNKNOWN) { 286 device_printf(sc->aac_dev, "unknown hardware type\n"); 287 error = ENXIO; 288 goto out; 289 } 290 291 292 /* 293 * Do bus-independent initialisation. 294 */ 295 error = aac_attach(sc); 296 297 out: 298 if (error) 299 aac_free(sc); 300 return(error); 301 } 302