Merge branch 'vendor/XZ'
[dragonfly.git] / sys / dev / raid / ips / ips_pci.c
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  * $FreeBSD: src/sys/dev/ips/ips_pci.c,v 1.10 2004/03/19 17:36:47 scottl Exp $
29  */
30
31 #include <dev/raid/ips/ips.h>
32
33 static int ips_pci_free(ips_softc_t *sc);
34 static void ips_intrhook(void *arg);
35
36 static struct ips_pci_product {
37         uint16_t vendor, device;
38         const char *desc;
39         int (*ips_adapter_reinit)(struct ips_softc *, int);
40         void (*ips_adapter_intr)(void *);
41         void (*ips_issue_cmd)(ips_command_t *);
42         void (*ips_poll_cmd)(ips_command_t *);
43 } ips_pci_products[] = {
44         { IPS_VENDOR_ID, IPS_MORPHEUS_DEVICE_ID, "IBM ServeRAID Adapter",
45           ips_morpheus_reinit, ips_morpheus_intr, ips_issue_morpheus_cmd,
46           ips_morpheus_poll },
47         { IPS_VENDOR_ID, IPS_COPPERHEAD_DEVICE_ID, "IBM ServeRAID Adapter",
48           ips_copperhead_reinit, ips_copperhead_intr, ips_issue_copperhead_cmd,
49           ips_copperhead_poll },
50         { IPS_VENDOR_ID_ADAPTEC, IPS_MARCO_DEVICE_ID,
51           "Adaptec ServeRAID Adapter", ips_morpheus_reinit, ips_morpheus_intr,
52           ips_issue_morpheus_cmd, ips_morpheus_poll },
53         { 0, 0, NULL }
54 };
55
56 static int
57 ips_pci_probe(device_t dev)
58 {
59         uint16_t vendor = pci_get_vendor(dev);
60         uint16_t device = pci_get_device(dev);
61         struct ips_pci_product *pp;
62         ips_softc_t *sc;
63
64         for (pp = ips_pci_products; pp->vendor; pp++) {
65                 if (vendor == pp->vendor && device == pp->device) {
66                         sc = (ips_softc_t *)device_get_softc(dev);
67                         sc->ips_adapter_reinit = pp->ips_adapter_reinit;
68                         sc->ips_adapter_intr = pp->ips_adapter_intr;
69                         sc->ips_issue_cmd = pp->ips_issue_cmd;
70                         sc->ips_poll_cmd = pp->ips_poll_cmd;
71                         device_set_desc(dev, pp->desc);
72                         return (0);
73                 }
74         }
75         return (ENXIO);
76 }
77
78 static int
79 ips_pci_attach(device_t dev)
80 {
81         u_int32_t command;
82         ips_softc_t *sc;
83         int error;
84
85         if (resource_disabled(device_get_name(dev), device_get_unit(dev))) {
86                 device_printf(dev, "device is disabled\n");
87                 /* but return 0 so the !$)$)*!$*) unit isn't reused */
88                 return (0);
89         }
90         DEVICE_PRINTF(1, dev, "in attach.\n");
91         sc = (ips_softc_t *)device_get_softc(dev);
92         sc->dev = dev;
93         /* make sure busmastering is on */
94         pci_enable_busmaster(dev);
95         command = pci_read_config(dev, PCIR_COMMAND, 1);
96         /* seting up io space */
97         sc->iores = NULL;
98         if (command & PCIM_CMD_MEMEN) {
99                 PRINTF(10, "trying MEMIO\n");
100                 if (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)
101                         sc->rid = PCIR_BAR(1);
102                 else
103                         sc->rid = PCIR_BAR(0);
104                 sc->iotype = SYS_RES_MEMORY;
105                 sc->iores = bus_alloc_resource_any(dev, sc->iotype,
106                     &sc->rid, RF_ACTIVE);
107         }
108         if (sc->iores == NULL && command & PCIM_CMD_PORTEN) {
109                 PRINTF(10, "trying PORTIO\n");
110                 sc->rid = PCIR_BAR(0);
111                 sc->iotype = SYS_RES_IOPORT;
112                 sc->iores = bus_alloc_resource_any(dev, sc->iotype,
113                     &sc->rid, RF_ACTIVE);
114         }
115         if (sc->iores == NULL) {
116                 device_printf(dev, "resource allocation failed\n");
117                 return (ENXIO);
118         }
119         sc->bustag = rman_get_bustag(sc->iores);
120         sc->bushandle = rman_get_bushandle(sc->iores);
121         /*
122          * allocate an interrupt. when does the irq become active?
123          * after leaving attach?
124          */
125         sc->irqrid = 0;
126         if ((sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ,
127             &sc->irqrid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
128                 device_printf(dev, "irq allocation failed\n");
129                 goto error;
130         }
131         error = bus_setup_intr(dev, sc->irqres, 0,
132                                sc->ips_adapter_intr, sc, 
133                                &sc->irqcookie, NULL);
134         if (error) {
135                 device_printf(dev, "irq setup failed\n");
136                 goto error;
137         }
138         if (bus_dma_tag_create( /* parent    */ NULL,
139                                 /* alignemnt */ 1,
140                                 /* boundary  */ 0,
141                                 /* lowaddr   */ BUS_SPACE_MAXADDR_32BIT,
142                                 /* highaddr  */ BUS_SPACE_MAXADDR,
143                                 /* filter    */ NULL,
144                                 /* filterarg */ NULL,
145                                 /* maxsize   */ BUS_SPACE_MAXSIZE_32BIT,
146                                 /* numsegs   */ IPS_MAX_SG_ELEMENTS,
147                                 /* maxsegsize*/ BUS_SPACE_MAXSIZE_32BIT,
148                                 /* flags     */ 0,
149                                 &sc->adapter_dmatag) != 0) {
150                 kprintf("IPS can't alloc dma tag\n");
151                 goto error;
152         }
153         sc->ips_ich.ich_func = ips_intrhook;
154         sc->ips_ich.ich_arg = sc;
155         sc->ips_ich.ich_desc = "ips";
156         lockinit(&sc->queue_lock, "ipslk", 0, LK_CANRECURSE);
157         bioq_init(&sc->bio_queue);
158         if (config_intrhook_establish(&sc->ips_ich) != 0) {
159                 kprintf("IPS can't establish configuration hook\n");
160                 goto error;
161         }
162         return 0;
163 error:
164         ips_pci_free(sc);
165         return (ENXIO);
166 }
167
168 static void
169 ips_intrhook(void *arg)
170 {
171         struct ips_softc *sc = arg;
172
173         config_intrhook_disestablish(&sc->ips_ich);
174         if (ips_adapter_init(sc))
175                 ips_pci_free(sc);
176         else
177                 sc->configured = 1;
178 }
179
180 static int
181 ips_pci_free(ips_softc_t *sc)
182 {
183
184         if (sc->adapter_dmatag)
185                 bus_dma_tag_destroy(sc->adapter_dmatag);
186         if (sc->irqcookie)
187                 bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
188         if (sc->irqres)
189                 bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid,
190                     sc->irqres);
191         if (sc->iores)
192                 bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
193         sc->configured = 0;
194         return 0;
195 }
196
197 static int
198 ips_pci_detach(device_t dev)
199 {
200         ips_softc_t *sc;
201
202         DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
203         sc = (ips_softc_t *)device_get_softc(dev);
204         if (sc->configured) {
205                 sc->configured = 0;
206                 ips_flush_cache(sc);
207                 if (ips_adapter_free(sc))
208                         return EBUSY;
209                 ips_pci_free(sc);
210         }
211         return 0;
212 }
213
214 static int
215 ips_pci_shutdown(device_t dev)
216 {
217         ips_softc_t *sc;
218
219         sc = (ips_softc_t *)device_get_softc(dev);
220         if (sc->configured)
221                 ips_flush_cache(sc);
222         return 0;
223 }
224
225 static device_method_t ips_driver_methods[] = {
226         DEVMETHOD(device_probe, ips_pci_probe),
227         DEVMETHOD(device_attach, ips_pci_attach),
228         DEVMETHOD(device_detach, ips_pci_detach),
229         DEVMETHOD(device_shutdown, ips_pci_shutdown),
230         { 0, 0 }
231 };
232
233 static driver_t ips_pci_driver = {
234         "ips",
235         ips_driver_methods,
236         sizeof(ips_softc_t),
237 };
238
239 static devclass_t ips_devclass;
240 DRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, NULL, NULL);