12aedd662SScott Long /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
42aedd662SScott Long * Copyright (c) 2002 Adaptec Inc.
52aedd662SScott Long * All rights reserved.
62aedd662SScott Long *
72aedd662SScott Long * Written by: David Jeffery
82aedd662SScott Long *
92aedd662SScott Long * Redistribution and use in source and binary forms, with or without
102aedd662SScott Long * modification, are permitted provided that the following conditions
112aedd662SScott Long * are met:
122aedd662SScott Long * 1. Redistributions of source code must retain the above copyright
132aedd662SScott Long * notice, this list of conditions and the following disclaimer.
142aedd662SScott Long * 2. Redistributions in binary form must reproduce the above copyright
152aedd662SScott Long * notice, this list of conditions and the following disclaimer in the
162aedd662SScott Long * documentation and/or other materials provided with the distribution.
172aedd662SScott Long *
182aedd662SScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
192aedd662SScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202aedd662SScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212aedd662SScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
222aedd662SScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232aedd662SScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242aedd662SScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252aedd662SScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262aedd662SScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272aedd662SScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282aedd662SScott Long * SUCH DAMAGE.
292aedd662SScott Long */
302aedd662SScott Long
31aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
32f94dfeb4SScott Long #include <dev/ips/ipsreg.h>
332aedd662SScott Long #include <dev/ips/ips.h>
34aad970f1SDavid E. O'Brien
35f94dfeb4SScott Long #include <dev/pci/pcireg.h>
36f94dfeb4SScott Long #include <dev/pci/pcivar.h>
37f94dfeb4SScott Long
382aedd662SScott Long static int ips_pci_free(ips_softc_t *sc);
39faf13262SPaul Saab static void ips_intrhook(void *arg);
402aedd662SScott Long
ips_pci_probe(device_t dev)412aedd662SScott Long static int ips_pci_probe(device_t dev)
422aedd662SScott Long {
432aedd662SScott Long
442aedd662SScott Long if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
452aedd662SScott Long (pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)) {
462aedd662SScott Long device_set_desc(dev, "IBM ServeRAID Adapter");
47b77e575eSWarner Losh return (BUS_PROBE_DEFAULT);
482aedd662SScott Long } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
492aedd662SScott Long (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)) {
502aedd662SScott Long device_set_desc(dev, "IBM ServeRAID Adapter");
51b77e575eSWarner Losh return (BUS_PROBE_DEFAULT);
5233ad16c0SScott Long } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID_ADAPTEC) &&
5333ad16c0SScott Long (pci_get_device(dev) == IPS_MARCO_DEVICE_ID)) {
5433ad16c0SScott Long device_set_desc(dev, "Adaptec ServeRAID Adapter");
55b77e575eSWarner Losh return (BUS_PROBE_DEFAULT);
562aedd662SScott Long }
572aedd662SScott Long return(ENXIO);
582aedd662SScott Long }
592aedd662SScott Long
ips_pci_attach(device_t dev)602aedd662SScott Long static int ips_pci_attach(device_t dev)
612aedd662SScott Long {
622aedd662SScott Long ips_softc_t *sc;
632aedd662SScott Long
642aedd662SScott Long DEVICE_PRINTF(1, dev, "in attach.\n");
652aedd662SScott Long sc = (ips_softc_t *)device_get_softc(dev);
662aedd662SScott Long sc->dev = dev;
67352176c8SJohn Baldwin mtx_init(&sc->queue_mtx, "IPS bioqueue lock", NULL, MTX_DEF);
68352176c8SJohn Baldwin sema_init(&sc->cmd_sema, 0, "IPS Command Semaphore");
69352176c8SJohn Baldwin callout_init_mtx(&sc->timer, &sc->queue_mtx, 0);
702aedd662SScott Long
712aedd662SScott Long if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){
722aedd662SScott Long sc->ips_adapter_reinit = ips_morpheus_reinit;
732aedd662SScott Long sc->ips_adapter_intr = ips_morpheus_intr;
742aedd662SScott Long sc->ips_issue_cmd = ips_issue_morpheus_cmd;
757765040eSScott Long sc->ips_poll_cmd = ips_morpheus_poll;
762aedd662SScott Long } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){
772aedd662SScott Long sc->ips_adapter_reinit = ips_copperhead_reinit;
782aedd662SScott Long sc->ips_adapter_intr = ips_copperhead_intr;
792aedd662SScott Long sc->ips_issue_cmd = ips_issue_copperhead_cmd;
807765040eSScott Long sc->ips_poll_cmd = ips_copperhead_poll;
8133ad16c0SScott Long } else if (pci_get_device(dev) == IPS_MARCO_DEVICE_ID){
8233ad16c0SScott Long sc->ips_adapter_reinit = ips_morpheus_reinit;
8333ad16c0SScott Long sc->ips_adapter_intr = ips_morpheus_intr;
8433ad16c0SScott Long sc->ips_issue_cmd = ips_issue_morpheus_cmd;
857765040eSScott Long sc->ips_poll_cmd = ips_morpheus_poll;
862aedd662SScott Long } else
872aedd662SScott Long goto error;
882aedd662SScott Long /* make sure busmastering is on */
89c68534f1SScott Long pci_enable_busmaster(dev);
90352176c8SJohn Baldwin /* setting up io space */
912aedd662SScott Long sc->iores = NULL;
922aedd662SScott Long PRINTF(10, "trying MEMIO\n");
9333ad16c0SScott Long if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)
94e27951b2SJohn Baldwin sc->rid = PCIR_BAR(1);
9533ad16c0SScott Long else
9633ad16c0SScott Long sc->rid = PCIR_BAR(0);
972aedd662SScott Long sc->iotype = SYS_RES_MEMORY;
98c68534f1SScott Long sc->iores = bus_alloc_resource_any(dev, sc->iotype, &sc->rid,
99c68534f1SScott Long RF_ACTIVE);
100c68534f1SScott Long if(!sc->iores){
1012aedd662SScott Long PRINTF(10, "trying PORTIO\n");
102e27951b2SJohn Baldwin sc->rid = PCIR_BAR(0);
1032aedd662SScott Long sc->iotype = SYS_RES_IOPORT;
1045f96beb9SNate Lawson sc->iores = bus_alloc_resource_any(dev, sc->iotype,
1055f96beb9SNate Lawson &sc->rid, RF_ACTIVE);
1062aedd662SScott Long }
1072aedd662SScott Long if(sc->iores == NULL){
1082aedd662SScott Long device_printf(dev, "resource allocation failed\n");
1092aedd662SScott Long return (ENXIO);
1102aedd662SScott Long }
1112aedd662SScott Long /*allocate an interrupt. when does the irq become active? after leaving attach? */
1122aedd662SScott Long sc->irqrid = 0;
1135f96beb9SNate Lawson if(!(sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ,
1145f96beb9SNate Lawson &sc->irqrid, RF_SHAREABLE | RF_ACTIVE))){
1152aedd662SScott Long device_printf(dev, "irq allocation failed\n");
1162aedd662SScott Long goto error;
1172aedd662SScott Long }
118ef544f63SPaolo Pisati if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO|INTR_MPSAFE, NULL,
119ef544f63SPaolo Pisati sc->ips_adapter_intr, sc, &sc->irqcookie)){
1202aedd662SScott Long device_printf(dev, "irq setup failed\n");
1212aedd662SScott Long goto error;
1222aedd662SScott Long }
123b6f97155SScott Long if (bus_dma_tag_create( /* PCI parent */bus_get_dma_tag(dev),
1242aedd662SScott Long /* alignemnt */ 1,
1252aedd662SScott Long /* boundary */ 0,
1262aedd662SScott Long /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
1272aedd662SScott Long /* highaddr */ BUS_SPACE_MAXADDR,
1282aedd662SScott Long /* filter */ NULL,
1292aedd662SScott Long /* filterarg */ NULL,
1302aedd662SScott Long /* maxsize */ BUS_SPACE_MAXSIZE_32BIT,
1312aedd662SScott Long /* numsegs */ IPS_MAX_SG_ELEMENTS,
1322aedd662SScott Long /* maxsegsize*/ BUS_SPACE_MAXSIZE_32BIT,
1332aedd662SScott Long /* flags */ 0,
13403a908f2SScott Long /* lockfunc */ NULL,
13503a908f2SScott Long /* lockarg */ NULL,
1362aedd662SScott Long &sc->adapter_dmatag) != 0) {
137352176c8SJohn Baldwin device_printf(dev, "can't alloc dma tag\n");
1382aedd662SScott Long goto error;
1392aedd662SScott Long }
140faf13262SPaul Saab sc->ips_ich.ich_func = ips_intrhook;
141faf13262SPaul Saab sc->ips_ich.ich_arg = sc;
142b234a120SScott Long bioq_init(&sc->queue);
143faf13262SPaul Saab if (config_intrhook_establish(&sc->ips_ich) != 0) {
144faf13262SPaul Saab printf("IPS can't establish configuration hook\n");
1452aedd662SScott Long goto error;
146faf13262SPaul Saab }
1472aedd662SScott Long return 0;
1482aedd662SScott Long error:
1492aedd662SScott Long ips_pci_free(sc);
1502aedd662SScott Long return (ENXIO);
1512aedd662SScott Long }
1522aedd662SScott Long
153faf13262SPaul Saab static void
ips_intrhook(void * arg)154faf13262SPaul Saab ips_intrhook(void *arg)
155faf13262SPaul Saab {
156faf13262SPaul Saab struct ips_softc *sc = (struct ips_softc *)arg;
157faf13262SPaul Saab
158faf13262SPaul Saab config_intrhook_disestablish(&sc->ips_ich);
159faf13262SPaul Saab if (ips_adapter_init(sc))
160faf13262SPaul Saab ips_pci_free(sc);
161faf13262SPaul Saab else
162faf13262SPaul Saab sc->configured = 1;
163faf13262SPaul Saab }
164faf13262SPaul Saab
ips_pci_free(ips_softc_t * sc)1652aedd662SScott Long static int ips_pci_free(ips_softc_t *sc)
1662aedd662SScott Long {
1672aedd662SScott Long if(sc->adapter_dmatag)
1682aedd662SScott Long bus_dma_tag_destroy(sc->adapter_dmatag);
1692aedd662SScott Long if(sc->irqcookie)
1702aedd662SScott Long bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
1712aedd662SScott Long if(sc->irqres)
1722aedd662SScott Long bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
1732aedd662SScott Long if(sc->iores)
1742aedd662SScott Long bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
175dea4622dSScott Long sc->configured = 0;
17603a908f2SScott Long mtx_destroy(&sc->queue_mtx);
17703a908f2SScott Long sema_destroy(&sc->cmd_sema);
1782aedd662SScott Long return 0;
1792aedd662SScott Long }
1802aedd662SScott Long
ips_pci_detach(device_t dev)1812aedd662SScott Long static int ips_pci_detach(device_t dev)
1822aedd662SScott Long {
1832aedd662SScott Long ips_softc_t *sc;
1842aedd662SScott Long DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
1852aedd662SScott Long sc = (ips_softc_t *) device_get_softc(dev);
186dea4622dSScott Long if (sc->configured) {
187dea4622dSScott Long sc->configured = 0;
1882aedd662SScott Long ips_flush_cache(sc);
1892aedd662SScott Long if(ips_adapter_free(sc))
1902aedd662SScott Long return EBUSY;
1912aedd662SScott Long ips_pci_free(sc);
192b234a120SScott Long bioq_flush(&sc->queue, NULL, ENXIO);
193dea4622dSScott Long }
1942aedd662SScott Long return 0;
1952aedd662SScott Long }
1962aedd662SScott Long
ips_pci_shutdown(device_t dev)1972aedd662SScott Long static int ips_pci_shutdown(device_t dev)
1982aedd662SScott Long {
1992aedd662SScott Long ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev);
200dea4622dSScott Long if (sc->configured) {
2012aedd662SScott Long ips_flush_cache(sc);
202dea4622dSScott Long }
2032aedd662SScott Long return 0;
2042aedd662SScott Long }
2052aedd662SScott Long
2062aedd662SScott Long static device_method_t ips_driver_methods[] = {
2072aedd662SScott Long DEVMETHOD(device_probe, ips_pci_probe),
2082aedd662SScott Long DEVMETHOD(device_attach, ips_pci_attach),
2092aedd662SScott Long DEVMETHOD(device_detach, ips_pci_detach),
2102aedd662SScott Long DEVMETHOD(device_shutdown, ips_pci_shutdown),
2112aedd662SScott Long {0,0}
2122aedd662SScott Long };
2132aedd662SScott Long
2142aedd662SScott Long static driver_t ips_pci_driver = {
2152aedd662SScott Long "ips",
2162aedd662SScott Long ips_driver_methods,
2172aedd662SScott Long sizeof(ips_softc_t),
2182aedd662SScott Long };
2192aedd662SScott Long
2203144501aSJohn Baldwin DRIVER_MODULE(ips, pci, ips_pci_driver, 0, 0);
221