2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Adaptec Inc.
7 * Written by: David Jeffery
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <dev/ips/ipsreg.h>
35 #include <dev/ips/ips.h>
37 #include <dev/pci/pcireg.h>
38 #include <dev/pci/pcivar.h>
40 static int ips_pci_free(ips_softc_t *sc);
41 static void ips_intrhook(void *arg);
43 static int ips_pci_probe(device_t dev)
46 if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
47 (pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)) {
48 device_set_desc(dev, "IBM ServeRAID Adapter");
49 return (BUS_PROBE_DEFAULT);
50 } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
51 (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)) {
52 device_set_desc(dev, "IBM ServeRAID Adapter");
53 return (BUS_PROBE_DEFAULT);
54 } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID_ADAPTEC) &&
55 (pci_get_device(dev) == IPS_MARCO_DEVICE_ID)) {
56 device_set_desc(dev, "Adaptec ServeRAID Adapter");
57 return (BUS_PROBE_DEFAULT);
62 static int ips_pci_attach(device_t dev)
66 DEVICE_PRINTF(1, dev, "in attach.\n");
67 sc = (ips_softc_t *)device_get_softc(dev);
69 mtx_init(&sc->queue_mtx, "IPS bioqueue lock", NULL, MTX_DEF);
70 sema_init(&sc->cmd_sema, 0, "IPS Command Semaphore");
71 callout_init_mtx(&sc->timer, &sc->queue_mtx, 0);
73 if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){
74 sc->ips_adapter_reinit = ips_morpheus_reinit;
75 sc->ips_adapter_intr = ips_morpheus_intr;
76 sc->ips_issue_cmd = ips_issue_morpheus_cmd;
77 sc->ips_poll_cmd = ips_morpheus_poll;
78 } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){
79 sc->ips_adapter_reinit = ips_copperhead_reinit;
80 sc->ips_adapter_intr = ips_copperhead_intr;
81 sc->ips_issue_cmd = ips_issue_copperhead_cmd;
82 sc->ips_poll_cmd = ips_copperhead_poll;
83 } else if (pci_get_device(dev) == IPS_MARCO_DEVICE_ID){
84 sc->ips_adapter_reinit = ips_morpheus_reinit;
85 sc->ips_adapter_intr = ips_morpheus_intr;
86 sc->ips_issue_cmd = ips_issue_morpheus_cmd;
87 sc->ips_poll_cmd = ips_morpheus_poll;
90 /* make sure busmastering is on */
91 pci_enable_busmaster(dev);
92 /* setting up io space */
94 PRINTF(10, "trying MEMIO\n");
95 if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)
96 sc->rid = PCIR_BAR(1);
98 sc->rid = PCIR_BAR(0);
99 sc->iotype = SYS_RES_MEMORY;
100 sc->iores = bus_alloc_resource_any(dev, sc->iotype, &sc->rid,
103 PRINTF(10, "trying PORTIO\n");
104 sc->rid = PCIR_BAR(0);
105 sc->iotype = SYS_RES_IOPORT;
106 sc->iores = bus_alloc_resource_any(dev, sc->iotype,
107 &sc->rid, RF_ACTIVE);
109 if(sc->iores == NULL){
110 device_printf(dev, "resource allocation failed\n");
113 /*allocate an interrupt. when does the irq become active? after leaving attach? */
115 if(!(sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ,
116 &sc->irqrid, RF_SHAREABLE | RF_ACTIVE))){
117 device_printf(dev, "irq allocation failed\n");
120 if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO|INTR_MPSAFE, NULL,
121 sc->ips_adapter_intr, sc, &sc->irqcookie)){
122 device_printf(dev, "irq setup failed\n");
125 if (bus_dma_tag_create( /* PCI parent */bus_get_dma_tag(dev),
128 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
129 /* highaddr */ BUS_SPACE_MAXADDR,
131 /* filterarg */ NULL,
132 /* maxsize */ BUS_SPACE_MAXSIZE_32BIT,
133 /* numsegs */ IPS_MAX_SG_ELEMENTS,
134 /* maxsegsize*/ BUS_SPACE_MAXSIZE_32BIT,
138 &sc->adapter_dmatag) != 0) {
139 device_printf(dev, "can't alloc dma tag\n");
142 sc->ips_ich.ich_func = ips_intrhook;
143 sc->ips_ich.ich_arg = sc;
144 bioq_init(&sc->queue);
145 if (config_intrhook_establish(&sc->ips_ich) != 0) {
146 printf("IPS can't establish configuration hook\n");
156 ips_intrhook(void *arg)
158 struct ips_softc *sc = (struct ips_softc *)arg;
160 config_intrhook_disestablish(&sc->ips_ich);
161 if (ips_adapter_init(sc))
167 static int ips_pci_free(ips_softc_t *sc)
169 if(sc->adapter_dmatag)
170 bus_dma_tag_destroy(sc->adapter_dmatag);
172 bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
174 bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
176 bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
178 mtx_destroy(&sc->queue_mtx);
179 sema_destroy(&sc->cmd_sema);
183 static int ips_pci_detach(device_t dev)
186 DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
187 sc = (ips_softc_t *) device_get_softc(dev);
188 if (sc->configured) {
191 if(ips_adapter_free(sc))
194 bioq_flush(&sc->queue, NULL, ENXIO);
199 static int ips_pci_shutdown(device_t dev)
201 ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev);
202 if (sc->configured) {
208 static device_method_t ips_driver_methods[] = {
209 DEVMETHOD(device_probe, ips_pci_probe),
210 DEVMETHOD(device_attach, ips_pci_attach),
211 DEVMETHOD(device_detach, ips_pci_detach),
212 DEVMETHOD(device_shutdown, ips_pci_shutdown),
216 static driver_t ips_pci_driver = {
222 static devclass_t ips_devclass;
223 DRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, 0, 0);