1 /*- 2 * Copyright (c) 2002 Adaptec Inc. 3 * All rights reserved. 4 * 5 * Written by: David Jeffery 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <dev/ips/ips.h> 33 34 static int ips_pci_free(ips_softc_t *sc); 35 static void ips_intrhook(void *arg); 36 37 static int ips_pci_probe(device_t dev) 38 { 39 40 if ((pci_get_vendor(dev) == IPS_VENDOR_ID) && 41 (pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)) { 42 device_set_desc(dev, "IBM ServeRAID Adapter"); 43 return 0; 44 } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID) && 45 (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)) { 46 device_set_desc(dev, "IBM ServeRAID Adapter"); 47 return (0); 48 } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID_ADAPTEC) && 49 (pci_get_device(dev) == IPS_MARCO_DEVICE_ID)) { 50 device_set_desc(dev, "Adaptec ServeRAID Adapter"); 51 return (0); 52 } 53 return(ENXIO); 54 } 55 56 static int ips_pci_attach(device_t dev) 57 { 58 u_int32_t command; 59 ips_softc_t *sc; 60 61 62 if (resource_disabled(device_get_name(dev), device_get_unit(dev))) { 63 device_printf(dev, "device is disabled\n"); 64 /* but return 0 so the !$)$)*!$*) unit isn't reused */ 65 return (0); 66 } 67 DEVICE_PRINTF(1, dev, "in attach.\n"); 68 sc = (ips_softc_t *)device_get_softc(dev); 69 if(!sc){ 70 printf("how is sc NULL?!\n"); 71 return (ENXIO); 72 } 73 bzero(sc, sizeof(ips_softc_t)); 74 sc->dev = dev; 75 76 if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){ 77 sc->ips_adapter_reinit = ips_morpheus_reinit; 78 sc->ips_adapter_intr = ips_morpheus_intr; 79 sc->ips_issue_cmd = ips_issue_morpheus_cmd; 80 } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){ 81 sc->ips_adapter_reinit = ips_copperhead_reinit; 82 sc->ips_adapter_intr = ips_copperhead_intr; 83 sc->ips_issue_cmd = ips_issue_copperhead_cmd; 84 } else if (pci_get_device(dev) == IPS_MARCO_DEVICE_ID){ 85 sc->ips_adapter_reinit = ips_morpheus_reinit; 86 sc->ips_adapter_intr = ips_morpheus_intr; 87 sc->ips_issue_cmd = ips_issue_morpheus_cmd; 88 } else 89 goto error; 90 /* make sure busmastering is on */ 91 command = pci_read_config(dev, PCIR_COMMAND, 1); 92 command |= PCIM_CMD_BUSMASTEREN; 93 pci_write_config(dev, PCIR_COMMAND, command, 1); 94 /* seting up io space */ 95 sc->iores = NULL; 96 if(command & PCIM_CMD_MEMEN){ 97 PRINTF(10, "trying MEMIO\n"); 98 if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID) 99 sc->rid = PCIR_BAR(1); 100 else 101 sc->rid = PCIR_BAR(0); 102 sc->iotype = SYS_RES_MEMORY; 103 sc->iores = bus_alloc_resource_any(dev, sc->iotype, 104 &sc->rid, RF_ACTIVE); 105 } 106 if(!sc->iores && command & PCIM_CMD_PORTEN){ 107 PRINTF(10, "trying PORTIO\n"); 108 sc->rid = PCIR_BAR(0); 109 sc->iotype = SYS_RES_IOPORT; 110 sc->iores = bus_alloc_resource_any(dev, sc->iotype, 111 &sc->rid, RF_ACTIVE); 112 } 113 if(sc->iores == NULL){ 114 device_printf(dev, "resource allocation failed\n"); 115 return (ENXIO); 116 } 117 sc->bustag = rman_get_bustag(sc->iores); 118 sc->bushandle = rman_get_bushandle(sc->iores); 119 /*allocate an interrupt. when does the irq become active? after leaving attach? */ 120 sc->irqrid = 0; 121 if(!(sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, 122 &sc->irqrid, RF_SHAREABLE | RF_ACTIVE))){ 123 device_printf(dev, "irq allocation failed\n"); 124 goto error; 125 } 126 if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO, sc->ips_adapter_intr, sc, &sc->irqcookie)){ 127 device_printf(dev, "irq setup failed\n"); 128 goto error; 129 } 130 if (bus_dma_tag_create( /* parent */ NULL, 131 /* alignemnt */ 1, 132 /* boundary */ 0, 133 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT, 134 /* highaddr */ BUS_SPACE_MAXADDR, 135 /* filter */ NULL, 136 /* filterarg */ NULL, 137 /* maxsize */ BUS_SPACE_MAXSIZE_32BIT, 138 /* numsegs */ IPS_MAX_SG_ELEMENTS, 139 /* maxsegsize*/ BUS_SPACE_MAXSIZE_32BIT, 140 /* flags */ 0, 141 /* lockfunc */ busdma_lock_mutex, 142 /* lockarg */ &Giant, 143 &sc->adapter_dmatag) != 0) { 144 printf("IPS can't alloc dma tag\n"); 145 goto error; 146 } 147 sc->ips_ich.ich_func = ips_intrhook; 148 sc->ips_ich.ich_arg = sc; 149 mtx_init(&sc->queue_mtx, "IPS bioqueue lock", MTX_DEF, 0); 150 bioq_init(&sc->queue); 151 if (config_intrhook_establish(&sc->ips_ich) != 0) { 152 printf("IPS can't establish configuration hook\n"); 153 goto error; 154 } 155 return 0; 156 error: 157 ips_pci_free(sc); 158 return (ENXIO); 159 } 160 161 static void 162 ips_intrhook(void *arg) 163 { 164 struct ips_softc *sc = (struct ips_softc *)arg; 165 166 config_intrhook_disestablish(&sc->ips_ich); 167 if (ips_adapter_init(sc)) 168 ips_pci_free(sc); 169 else 170 sc->configured = 1; 171 } 172 173 static int ips_pci_free(ips_softc_t *sc) 174 { 175 if(sc->adapter_dmatag) 176 bus_dma_tag_destroy(sc->adapter_dmatag); 177 if(sc->irqcookie) 178 bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie); 179 if(sc->irqres) 180 bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres); 181 if(sc->iores) 182 bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores); 183 sc->configured = 0; 184 return 0; 185 } 186 187 static int ips_pci_detach(device_t dev) 188 { 189 ips_softc_t *sc; 190 DEVICE_PRINTF(1, dev, "detaching ServeRaid\n"); 191 sc = (ips_softc_t *) device_get_softc(dev); 192 if (sc->configured) { 193 sc->configured = 0; 194 ips_flush_cache(sc); 195 if(ips_adapter_free(sc)) 196 return EBUSY; 197 ips_pci_free(sc); 198 bioq_flush(&sc->queue, NULL, ENXIO); 199 } 200 return 0; 201 } 202 203 static int ips_pci_shutdown(device_t dev) 204 { 205 ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev); 206 if (sc->configured) { 207 ips_flush_cache(sc); 208 } 209 return 0; 210 } 211 212 static device_method_t ips_driver_methods[] = { 213 DEVMETHOD(device_probe, ips_pci_probe), 214 DEVMETHOD(device_attach, ips_pci_attach), 215 DEVMETHOD(device_detach, ips_pci_detach), 216 DEVMETHOD(device_shutdown, ips_pci_shutdown), 217 {0,0} 218 }; 219 220 static driver_t ips_pci_driver = { 221 "ips", 222 ips_driver_methods, 223 sizeof(ips_softc_t), 224 }; 225 226 static devclass_t ips_devclass; 227 DRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, 0, 0); 228