135863739SMike Smith /*- 235863739SMike Smith * Copyright (c) 2000 Michael Smith 3c6eafcf2SScott Long * Copyright (c) 2001 Scott Long 435863739SMike Smith * Copyright (c) 2000 BSDi 5fadfef89SScott Long * Copyright (c) 2001 Adaptec, Inc. 635863739SMike Smith * All rights reserved. 735863739SMike Smith * 835863739SMike Smith * Redistribution and use in source and binary forms, with or without 935863739SMike Smith * modification, are permitted provided that the following conditions 1035863739SMike Smith * are met: 1135863739SMike Smith * 1. Redistributions of source code must retain the above copyright 1235863739SMike Smith * notice, this list of conditions and the following disclaimer. 1335863739SMike Smith * 2. Redistributions in binary form must reproduce the above copyright 1435863739SMike Smith * notice, this list of conditions and the following disclaimer in the 1535863739SMike Smith * documentation and/or other materials provided with the distribution. 1635863739SMike Smith * 1735863739SMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1835863739SMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1935863739SMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2035863739SMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2135863739SMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2235863739SMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2335863739SMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2435863739SMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2535863739SMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2635863739SMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2735863739SMike Smith * SUCH DAMAGE. 2835863739SMike Smith */ 2935863739SMike Smith 30aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 31aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 32aad970f1SDavid E. O'Brien 3335863739SMike Smith /* 3435863739SMike Smith * PCI bus interface and resource allocation. 3535863739SMike Smith */ 3635863739SMike Smith 37f6c4dd3fSScott Long #include "opt_aac.h" 38f6c4dd3fSScott Long 3935863739SMike Smith #include <sys/param.h> 4035863739SMike Smith #include <sys/systm.h> 4135863739SMike Smith #include <sys/kernel.h> 42fe12f24bSPoul-Henning Kamp #include <sys/module.h> 4335863739SMike Smith 449e2e96d8SScott Long #include <sys/bio.h> 4535863739SMike Smith #include <sys/bus.h> 4635863739SMike Smith #include <sys/conf.h> 4735863739SMike Smith #include <sys/disk.h> 4835863739SMike Smith 4935863739SMike Smith #include <machine/bus.h> 5035863739SMike Smith #include <machine/resource.h> 5135863739SMike Smith #include <sys/rman.h> 5235863739SMike Smith 53dc5f7dd0SWarner Losh #include <dev/pci/pcireg.h> 54dc5f7dd0SWarner Losh #include <dev/pci/pcivar.h> 5535863739SMike Smith 5635863739SMike Smith #include <dev/aac/aacreg.h> 570b0594cdSScott Long #include <sys/aac_ioctl.h> 5835863739SMike Smith #include <dev/aac/aacvar.h> 5935863739SMike Smith 6035863739SMike Smith static int aac_pci_probe(device_t dev); 6135863739SMike Smith static int aac_pci_attach(device_t dev); 6235863739SMike Smith 6335863739SMike Smith static device_method_t aac_methods[] = { 6435863739SMike Smith /* Device interface */ 6535863739SMike Smith DEVMETHOD(device_probe, aac_pci_probe), 6635863739SMike Smith DEVMETHOD(device_attach, aac_pci_attach), 6735863739SMike Smith DEVMETHOD(device_detach, aac_detach), 6835863739SMike Smith DEVMETHOD(device_suspend, aac_suspend), 6935863739SMike Smith DEVMETHOD(device_resume, aac_resume), 7035863739SMike Smith 7135863739SMike Smith DEVMETHOD(bus_print_child, bus_generic_print_child), 7235863739SMike Smith DEVMETHOD(bus_driver_added, bus_generic_driver_added), 7335863739SMike Smith { 0, 0 } 7435863739SMike Smith }; 7535863739SMike Smith 7635863739SMike Smith static driver_t aac_pci_driver = { 7735863739SMike Smith "aac", 7835863739SMike Smith aac_methods, 7935863739SMike Smith sizeof(struct aac_softc) 8035863739SMike Smith }; 8135863739SMike Smith 82e45bef2aSMike Smith static devclass_t aac_devclass; 83e45bef2aSMike Smith 8435863739SMike Smith DRIVER_MODULE(aac, pci, aac_pci_driver, aac_devclass, 0, 0); 8535863739SMike Smith 8635863739SMike Smith struct aac_ident 8735863739SMike Smith { 8835863739SMike Smith u_int16_t vendor; 8935863739SMike Smith u_int16_t device; 9035863739SMike Smith u_int16_t subvendor; 9135863739SMike Smith u_int16_t subdevice; 9235863739SMike Smith int hwif; 93fe3cb0e1SScott Long int quirks; 9435863739SMike Smith char *desc; 9535863739SMike Smith } aac_identifiers[] = { 96a6d35632SScott Long {0x1028, 0x0001, 0x1028, 0x0001, AAC_HWIF_I960RX, 0, 97fe3cb0e1SScott Long "Dell PERC 2/Si"}, 98a6d35632SScott Long {0x1028, 0x0002, 0x1028, 0x0002, AAC_HWIF_I960RX, 0, 99fe3cb0e1SScott Long "Dell PERC 3/Di"}, 100a6d35632SScott Long {0x1028, 0x0003, 0x1028, 0x0003, AAC_HWIF_I960RX, 0, 101fe3cb0e1SScott Long "Dell PERC 3/Si"}, 102a6d35632SScott Long {0x1028, 0x0004, 0x1028, 0x00d0, AAC_HWIF_I960RX, 0, 103fe3cb0e1SScott Long "Dell PERC 3/Si"}, 104a6d35632SScott Long {0x1028, 0x0002, 0x1028, 0x00d1, AAC_HWIF_I960RX, 0, 105fe3cb0e1SScott Long "Dell PERC 3/Di"}, 106a6d35632SScott Long {0x1028, 0x0002, 0x1028, 0x00d9, AAC_HWIF_I960RX, 0, 107fe3cb0e1SScott Long "Dell PERC 3/Di"}, 108a6d35632SScott Long {0x1028, 0x000a, 0x1028, 0x0106, AAC_HWIF_I960RX, 0, 109fe3cb0e1SScott Long "Dell PERC 3/Di"}, 110a6d35632SScott Long {0x1028, 0x000a, 0x1028, 0x011b, AAC_HWIF_I960RX, 0, 111fe3cb0e1SScott Long "Dell PERC 3/Di"}, 112a6d35632SScott Long {0x1028, 0x000a, 0x1028, 0x0121, AAC_HWIF_I960RX, 0, 113fe3cb0e1SScott Long "Dell PERC 3/Di"}, 114a6d35632SScott Long {0x1011, 0x0046, 0x9005, 0x0364, AAC_HWIF_STRONGARM, 0, 115fe3cb0e1SScott Long "Adaptec AAC-364"}, 1164b00f859SScott Long {0x1011, 0x0046, 0x9005, 0x0365, AAC_HWIF_STRONGARM, 1174b00f859SScott Long AAC_FLAGS_BROKEN_MEMMAP, "Adaptec SCSI RAID 5400S"}, 118a6d35632SScott Long {0x1011, 0x0046, 0x9005, 0x1364, AAC_HWIF_STRONGARM, AAC_FLAGS_PERC2QC, 119a6d35632SScott Long "Dell PERC 2/QC"}, 120a6d35632SScott Long {0x1011, 0x0046, 0x103c, 0x10c2, AAC_HWIF_STRONGARM, 0, 121a9f3d2c7SScott Long "HP NetRaid-4M"}, 122a6d35632SScott Long {0x9005, 0x0285, 0x9005, 0x0285, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB | 123a6d35632SScott Long AAC_FLAGS_256FIBS, "Adaptec SCSI RAID 2200S"}, 124a6d35632SScott Long {0x9005, 0x0285, 0x1028, 0x0287, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB | 125a6d35632SScott Long AAC_FLAGS_256FIBS, "Dell PERC 320/DC"}, 126a6d35632SScott Long {0x9005, 0x0285, 0x9005, 0x0286, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB | 127a6d35632SScott Long AAC_FLAGS_256FIBS, "Adaptec SCSI RAID 2120S"}, 1289d5be300SScott Long {0x9005, 0x0285, 0x9005, 0x0290, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB, 1299d5be300SScott Long "Adaptec SCSI RAID 2410SA"}, 130ad452e65SScott Long {0x9005, 0x0285, 0x1028, 0x0291, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB, 131ad452e65SScott Long "Dell CERC SATA RAID 2"}, 132ad452e65SScott Long {0x9005, 0x0285, 0x9005, 0x0292, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB, 133ad452e65SScott Long "Adaptec SCSI RAID 2810SA"}, 134b1e56e58SScott Long {0x9005, 0x0285, 0x9005, 0x0293, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB, 135b1e56e58SScott Long "Adaptec SCSI RAID 21610SA"}, 136ecefe571SScott Long {0x9005, 0x0286, 0x9005, 0x028c, AAC_HWIF_RKT, 0, 137ecefe571SScott Long "Adaptec SCSI RAID 2230S"}, 1384afedc31SScott Long {0x9005, 0x0286, 0x9005, 0x028d, AAC_HWIF_RKT, 0, 1394afedc31SScott Long "Adaptec SCSI RAID 2130S"}, 140fe3cb0e1SScott Long {0, 0, 0, 0, 0, 0, 0} 14135863739SMike Smith }; 14235863739SMike Smith 143914da7d0SScott Long /* 14435863739SMike Smith * Determine whether this is one of our supported adapters. 14535863739SMike Smith */ 14635863739SMike Smith static int 14735863739SMike Smith aac_pci_probe(device_t dev) 14835863739SMike Smith { 14935863739SMike Smith struct aac_ident *m; 15035863739SMike Smith 15135863739SMike Smith debug_called(1); 15235863739SMike Smith 15335863739SMike Smith for (m = aac_identifiers; m->vendor != 0; m++) { 15435863739SMike Smith if ((m->vendor == pci_get_vendor(dev)) && 15535863739SMike Smith (m->device == pci_get_device(dev)) && 156914da7d0SScott Long ((m->subvendor == 0) || (m->subvendor == 157914da7d0SScott Long pci_get_subvendor(dev))) && 158914da7d0SScott Long ((m->subdevice == 0) || ((m->subdevice == 159914da7d0SScott Long pci_get_subdevice(dev))))) { 16035863739SMike Smith 16135863739SMike Smith device_set_desc(dev, m->desc); 162494f3ca1SWarner Losh return(BUS_PROBE_DEFAULT); 16335863739SMike Smith } 16435863739SMike Smith } 16535863739SMike Smith return(ENXIO); 16635863739SMike Smith } 16735863739SMike Smith 168914da7d0SScott Long /* 16935863739SMike Smith * Allocate resources for our device, set up the bus interface. 17035863739SMike Smith */ 17135863739SMike Smith static int 17235863739SMike Smith aac_pci_attach(device_t dev) 17335863739SMike Smith { 17435863739SMike Smith struct aac_softc *sc; 17535863739SMike Smith int i, error; 17635863739SMike Smith u_int32_t command; 17735863739SMike Smith 17835863739SMike Smith debug_called(1); 17935863739SMike Smith 18035863739SMike Smith /* 18135863739SMike Smith * Initialise softc. 18235863739SMike Smith */ 18335863739SMike Smith sc = device_get_softc(dev); 18435863739SMike Smith bzero(sc, sizeof(*sc)); 18535863739SMike Smith sc->aac_dev = dev; 18635863739SMike Smith 18735863739SMike Smith /* assume failure is 'not configured' */ 18835863739SMike Smith error = ENXIO; 18935863739SMike Smith 19035863739SMike Smith /* 19135863739SMike Smith * Verify that the adapter is correctly set up in PCI space. 19235863739SMike Smith */ 19335863739SMike Smith command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2); 19435863739SMike Smith command |= PCIM_CMD_BUSMASTEREN; 19535863739SMike Smith pci_write_config(dev, PCIR_COMMAND, command, 2); 19635863739SMike Smith command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2); 19735863739SMike Smith if (!(command & PCIM_CMD_BUSMASTEREN)) { 19835863739SMike Smith device_printf(sc->aac_dev, "can't enable bus-master feature\n"); 19935863739SMike Smith goto out; 20035863739SMike Smith } 20135863739SMike Smith if ((command & PCIM_CMD_MEMEN) == 0) { 20235863739SMike Smith device_printf(sc->aac_dev, "memory window not available\n"); 20335863739SMike Smith goto out; 20435863739SMike Smith } 20535863739SMike Smith 20635863739SMike Smith /* 20735863739SMike Smith * Allocate the PCI register window. 20835863739SMike Smith */ 2095b80c0d7SScott Long sc->aac_regs_rid = PCIR_BAR(0); 2105f96beb9SNate Lawson if ((sc->aac_regs_resource = bus_alloc_resource_any(sc->aac_dev, 211914da7d0SScott Long SYS_RES_MEMORY, 212914da7d0SScott Long &sc->aac_regs_rid, 2135f96beb9SNate Lawson RF_ACTIVE)) == 2145f96beb9SNate Lawson NULL) { 215914da7d0SScott Long device_printf(sc->aac_dev, 216914da7d0SScott Long "couldn't allocate register window\n"); 21735863739SMike Smith goto out; 21835863739SMike Smith } 21935863739SMike Smith sc->aac_btag = rman_get_bustag(sc->aac_regs_resource); 22035863739SMike Smith sc->aac_bhandle = rman_get_bushandle(sc->aac_regs_resource); 22135863739SMike Smith 22235863739SMike Smith /* 22335863739SMike Smith * Allocate and connect our interrupt. 22435863739SMike Smith */ 22535863739SMike Smith sc->aac_irq_rid = 0; 2265f96beb9SNate Lawson if ((sc->aac_irq = bus_alloc_resource_any(sc->aac_dev, SYS_RES_IRQ, 2275f96beb9SNate Lawson &sc->aac_irq_rid, 228914da7d0SScott Long RF_SHAREABLE | 229914da7d0SScott Long RF_ACTIVE)) == NULL) { 23035863739SMike Smith device_printf(sc->aac_dev, "can't allocate interrupt\n"); 23135863739SMike Smith goto out; 23235863739SMike Smith } 2339c3a7fceSScott Long if (bus_setup_intr(sc->aac_dev, sc->aac_irq, 2349148fa21SScott Long INTR_FAST|INTR_TYPE_BIO, aac_intr, 2359c3a7fceSScott Long sc, &sc->aac_intr)) { 236f893fdd5SScott Long device_printf(sc->aac_dev, "can't set up FAST interrupt\n"); 237f893fdd5SScott Long if (bus_setup_intr(sc->aac_dev, sc->aac_irq, 2387cec30e7SDag-Erling Smørgrav INTR_MPSAFE|INTR_ENTROPY|INTR_TYPE_BIO, 239d7f2f66eSScott Long aac_intr, sc, &sc->aac_intr)) { 240f893fdd5SScott Long device_printf(sc->aac_dev, 241f893fdd5SScott Long "can't set up MPSAFE interrupt\n"); 24235863739SMike Smith goto out; 24335863739SMike Smith } 244f893fdd5SScott Long } 24535863739SMike Smith 24635863739SMike Smith /* assume failure is 'out of memory' */ 24735863739SMike Smith error = ENOMEM; 24835863739SMike Smith 24935863739SMike Smith /* 25035863739SMike Smith * Allocate the parent bus DMA tag appropriate for our PCI interface. 25135863739SMike Smith * 25235863739SMike Smith * Note that some of these controllers are 64-bit capable. 25335863739SMike Smith */ 25435863739SMike Smith if (bus_dma_tag_create(NULL, /* parent */ 255cbfd045bSScott Long PAGE_SIZE, 0, /* algnmnt, boundary */ 256a6d35632SScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 25735863739SMike Smith BUS_SPACE_MAXADDR, /* highaddr */ 25835863739SMike Smith NULL, NULL, /* filter, filterarg */ 259a6d35632SScott Long BUS_SPACE_MAXSIZE_32BIT, /* maxsize */ 260914da7d0SScott Long AAC_MAXSGENTRIES, /* nsegments */ 26135863739SMike Smith BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 262a6d35632SScott Long 0, /* flags */ 263f6b1c44dSScott Long NULL, NULL, /* No locking needed */ 26435863739SMike Smith &sc->aac_parent_dmat)) { 26535863739SMike Smith device_printf(sc->aac_dev, "can't allocate parent DMA tag\n"); 26635863739SMike Smith goto out; 26735863739SMike Smith } 26835863739SMike Smith 26935863739SMike Smith /* 270c6eafcf2SScott Long * Detect the hardware interface version, set up the bus interface 271c6eafcf2SScott Long * indirection. 27235863739SMike Smith */ 27335863739SMike Smith for (i = 0; aac_identifiers[i].vendor != 0; i++) { 27435863739SMike Smith if ((aac_identifiers[i].vendor == pci_get_vendor(dev)) && 275fe3cb0e1SScott Long (aac_identifiers[i].device == pci_get_device(dev)) && 276fe3cb0e1SScott Long (aac_identifiers[i].subvendor == pci_get_subvendor(dev)) && 277fe3cb0e1SScott Long (aac_identifiers[i].subdevice == pci_get_subdevice(dev))) { 27835863739SMike Smith sc->aac_hwif = aac_identifiers[i].hwif; 27935863739SMike Smith switch(sc->aac_hwif) { 28035863739SMike Smith case AAC_HWIF_I960RX: 28135863739SMike Smith debug(2, "set hardware up for i960Rx"); 28235863739SMike Smith sc->aac_if = aac_rx_interface; 28335863739SMike Smith break; 28435863739SMike Smith case AAC_HWIF_STRONGARM: 28535863739SMike Smith debug(2, "set hardware up for StrongARM"); 28635863739SMike Smith sc->aac_if = aac_sa_interface; 28735863739SMike Smith break; 288b3457b51SScott Long case AAC_HWIF_FALCON: 289b3457b51SScott Long debug(2, "set hardware up for Falcon/PPC"); 290b3457b51SScott Long sc->aac_if = aac_fa_interface; 291b3457b51SScott Long break; 2924afedc31SScott Long case AAC_HWIF_RKT: 2934afedc31SScott Long debug(2, "setu hardware up for Rocket/MIPS"); 2944afedc31SScott Long sc->aac_if = aac_rkt_interface; 2954afedc31SScott Long break; 2964afedc31SScott Long default: 2974afedc31SScott Long sc->aac_hwif = AAC_HWIF_UNKNOWN; 2984afedc31SScott Long break; 29935863739SMike Smith } 300fe3cb0e1SScott Long 301fe3cb0e1SScott Long /* Set up quirks */ 302a6d35632SScott Long sc->flags = aac_identifiers[i].quirks; 303fe3cb0e1SScott Long 30435863739SMike Smith break; 30535863739SMike Smith } 30635863739SMike Smith } 3070b94a66eSMike Smith if (sc->aac_hwif == AAC_HWIF_UNKNOWN) { 3080b94a66eSMike Smith device_printf(sc->aac_dev, "unknown hardware type\n"); 3090b94a66eSMike Smith error = ENXIO; 3100b94a66eSMike Smith goto out; 3110b94a66eSMike Smith } 31235863739SMike Smith 313fe94b852SScott Long 314fe94b852SScott Long /* 31535863739SMike Smith * Do bus-independent initialisation. 31635863739SMike Smith */ 31735863739SMike Smith error = aac_attach(sc); 31835863739SMike Smith 31935863739SMike Smith out: 32035863739SMike Smith if (error) 32135863739SMike Smith aac_free(sc); 32235863739SMike Smith return(error); 32335863739SMike Smith } 324608d7ac2SScott Long 325608d7ac2SScott Long /* 326608d7ac2SScott Long * Do nothing driver that will attach to the SCSI channels of a Dell PERC 327608d7ac2SScott Long * controller. This is needed to keep the power management subsystem from 328608d7ac2SScott Long * trying to power down these devices. 329608d7ac2SScott Long */ 330608d7ac2SScott Long static int aacch_probe(device_t dev); 331608d7ac2SScott Long static int aacch_attach(device_t dev); 332608d7ac2SScott Long static int aacch_detach(device_t dev); 333608d7ac2SScott Long 334608d7ac2SScott Long static device_method_t aacch_methods[] = { 335608d7ac2SScott Long /* Device interface */ 336608d7ac2SScott Long DEVMETHOD(device_probe, aacch_probe), 337608d7ac2SScott Long DEVMETHOD(device_attach, aacch_attach), 338608d7ac2SScott Long DEVMETHOD(device_detach, aacch_detach), 339608d7ac2SScott Long { 0, 0 } 340608d7ac2SScott Long }; 341608d7ac2SScott Long 342608d7ac2SScott Long struct aacch_softc { 343608d7ac2SScott Long device_t dev; 344608d7ac2SScott Long }; 345608d7ac2SScott Long 346608d7ac2SScott Long static driver_t aacch_driver = { 347608d7ac2SScott Long "aacch", 348608d7ac2SScott Long aacch_methods, 349608d7ac2SScott Long sizeof(struct aacch_softc) 350608d7ac2SScott Long }; 351608d7ac2SScott Long 352608d7ac2SScott Long static devclass_t aacch_devclass; 353608d7ac2SScott Long DRIVER_MODULE(aacch, pci, aacch_driver, aacch_devclass, 0, 0); 354608d7ac2SScott Long 355608d7ac2SScott Long static int 356608d7ac2SScott Long aacch_probe(device_t dev) 357608d7ac2SScott Long { 358608d7ac2SScott Long 3591b3a4f4eSScott Long if ((pci_get_vendor(dev) != 0x9005) || 3601b3a4f4eSScott Long (pci_get_device(dev) != 0x00c5)) 361608d7ac2SScott Long return (ENXIO); 362608d7ac2SScott Long 363608d7ac2SScott Long device_set_desc(dev, "AAC RAID Channel"); 364608d7ac2SScott Long return (-10); 365608d7ac2SScott Long } 366608d7ac2SScott Long 367608d7ac2SScott Long static int 368608d7ac2SScott Long aacch_attach(device_t dev) 369608d7ac2SScott Long { 370608d7ac2SScott Long struct aacch_softc *sc; 371608d7ac2SScott Long 372608d7ac2SScott Long sc = device_get_softc(dev); 373608d7ac2SScott Long 374608d7ac2SScott Long sc->dev = dev; 375608d7ac2SScott Long 376608d7ac2SScott Long return (0); 377608d7ac2SScott Long } 378608d7ac2SScott Long 379608d7ac2SScott Long static int 380608d7ac2SScott Long aacch_detach(device_t dev) 381608d7ac2SScott Long { 382608d7ac2SScott Long 383608d7ac2SScott Long return (0); 384608d7ac2SScott Long } 385608d7ac2SScott Long 386