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 } 49 return(ENXIO); 50 } 51 52 static int ips_pci_attach(device_t dev) 53 { 54 u_int32_t command; 55 ips_softc_t *sc; 56 57 58 if (resource_disabled(device_get_name(dev), device_get_unit(dev))) { 59 device_printf(dev, "device is disabled\n"); 60 /* but return 0 so the !$)$)*!$*) unit isn't reused */ 61 return (0); 62 } 63 DEVICE_PRINTF(1, dev, "in attach.\n"); 64 sc = (ips_softc_t *)device_get_softc(dev); 65 if(!sc){ 66 printf("how is sc NULL?!\n"); 67 return (ENXIO); 68 } 69 bzero(sc, sizeof(ips_softc_t)); 70 sc->dev = dev; 71 72 if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){ 73 sc->ips_adapter_reinit = ips_morpheus_reinit; 74 sc->ips_adapter_intr = ips_morpheus_intr; 75 sc->ips_issue_cmd = ips_issue_morpheus_cmd; 76 } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){ 77 sc->ips_adapter_reinit = ips_copperhead_reinit; 78 sc->ips_adapter_intr = ips_copperhead_intr; 79 sc->ips_issue_cmd = ips_issue_copperhead_cmd; 80 } else 81 goto error; 82 /* make sure busmastering is on */ 83 command = pci_read_config(dev, PCIR_COMMAND, 1); 84 command |= PCIM_CMD_BUSMASTEREN; 85 pci_write_config(dev, PCIR_COMMAND, command, 1); 86 /* seting up io space */ 87 sc->iores = NULL; 88 if(command & PCIM_CMD_MEMEN){ 89 PRINTF(10, "trying MEMIO\n"); 90 if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID) 91 sc->rid = PCIR_BAR(0); 92 else 93 sc->rid = PCIR_BAR(1); 94 sc->iotype = SYS_RES_MEMORY; 95 sc->iores = bus_alloc_resource(dev, sc->iotype, &sc->rid, 0, ~0, 1, RF_ACTIVE); 96 } 97 if(!sc->iores && command & PCIM_CMD_PORTEN){ 98 PRINTF(10, "trying PORTIO\n"); 99 sc->rid = PCIR_BAR(0); 100 sc->iotype = SYS_RES_IOPORT; 101 sc->iores = bus_alloc_resource(dev, sc->iotype, &sc->rid, 0, ~0, 1, RF_ACTIVE); 102 } 103 if(sc->iores == NULL){ 104 device_printf(dev, "resource allocation failed\n"); 105 return (ENXIO); 106 } 107 sc->bustag = rman_get_bustag(sc->iores); 108 sc->bushandle = rman_get_bushandle(sc->iores); 109 /*allocate an interrupt. when does the irq become active? after leaving attach? */ 110 sc->irqrid = 0; 111 if(!(sc->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irqrid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE))){ 112 device_printf(dev, "irq allocation failed\n"); 113 goto error; 114 } 115 if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO, sc->ips_adapter_intr, sc, &sc->irqcookie)){ 116 device_printf(dev, "irq setup failed\n"); 117 goto error; 118 } 119 if (bus_dma_tag_create( /* parent */ NULL, 120 /* alignemnt */ 1, 121 /* boundary */ 0, 122 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT, 123 /* highaddr */ BUS_SPACE_MAXADDR, 124 /* filter */ NULL, 125 /* filterarg */ NULL, 126 /* maxsize */ BUS_SPACE_MAXSIZE_32BIT, 127 /* numsegs */ IPS_MAX_SG_ELEMENTS, 128 /* maxsegsize*/ BUS_SPACE_MAXSIZE_32BIT, 129 /* flags */ 0, 130 /* lockfunc */ busdma_lock_mutex, 131 /* lockarg */ &Giant, 132 &sc->adapter_dmatag) != 0) { 133 printf("IPS can't alloc dma tag\n"); 134 goto error; 135 } 136 sc->ips_ich.ich_func = ips_intrhook; 137 sc->ips_ich.ich_arg = sc; 138 if (config_intrhook_establish(&sc->ips_ich) != 0) { 139 printf("IPS can't establish configuration hook\n"); 140 goto error; 141 } 142 return 0; 143 error: 144 ips_pci_free(sc); 145 return (ENXIO); 146 } 147 148 static void 149 ips_intrhook(void *arg) 150 { 151 struct ips_softc *sc = (struct ips_softc *)arg; 152 153 config_intrhook_disestablish(&sc->ips_ich); 154 if (ips_adapter_init(sc)) 155 ips_pci_free(sc); 156 else 157 sc->configured = 1; 158 } 159 160 static int ips_pci_free(ips_softc_t *sc) 161 { 162 if(sc->adapter_dmatag) 163 bus_dma_tag_destroy(sc->adapter_dmatag); 164 if(sc->irqcookie) 165 bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie); 166 if(sc->irqres) 167 bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres); 168 if(sc->iores) 169 bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores); 170 sc->configured = 0; 171 return 0; 172 } 173 174 static int ips_pci_detach(device_t dev) 175 { 176 ips_softc_t *sc; 177 DEVICE_PRINTF(1, dev, "detaching ServeRaid\n"); 178 sc = (ips_softc_t *) device_get_softc(dev); 179 if (sc->configured) { 180 sc->configured = 0; 181 ips_flush_cache(sc); 182 if(ips_adapter_free(sc)) 183 return EBUSY; 184 ips_pci_free(sc); 185 mtx_destroy(&sc->cmd_mtx); 186 } 187 return 0; 188 } 189 190 static int ips_pci_shutdown(device_t dev) 191 { 192 ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev); 193 if (sc->configured) { 194 ips_flush_cache(sc); 195 } 196 return 0; 197 } 198 199 static device_method_t ips_driver_methods[] = { 200 DEVMETHOD(device_probe, ips_pci_probe), 201 DEVMETHOD(device_attach, ips_pci_attach), 202 DEVMETHOD(device_detach, ips_pci_detach), 203 DEVMETHOD(device_shutdown, ips_pci_shutdown), 204 {0,0} 205 }; 206 207 static driver_t ips_pci_driver = { 208 "ips", 209 ips_driver_methods, 210 sizeof(ips_softc_t), 211 }; 212 213 static devclass_t ips_devclass; 214 DRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, 0, 0); 215