xref: /freebsd/sys/dev/ips/ips_pci.c (revision a0ee8cc6)
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/ipsreg.h>
33 #include <dev/ips/ips.h>
34 
35 #include <dev/pci/pcireg.h>
36 #include <dev/pci/pcivar.h>
37 
38 static int ips_pci_free(ips_softc_t *sc);
39 static void ips_intrhook(void *arg);
40 
41 static int ips_pci_probe(device_t dev)
42 {
43 
44         if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
45 	    (pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)) {
46 		device_set_desc(dev, "IBM ServeRAID Adapter");
47                 return (BUS_PROBE_DEFAULT);
48         } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
49 	    (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)) {
50 		device_set_desc(dev, "IBM ServeRAID Adapter");
51 		return (BUS_PROBE_DEFAULT);
52         } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID_ADAPTEC) &&
53 	    (pci_get_device(dev) == IPS_MARCO_DEVICE_ID)) {
54 		device_set_desc(dev, "Adaptec ServeRAID Adapter");
55 		return (BUS_PROBE_DEFAULT);
56 	}
57         return(ENXIO);
58 }
59 
60 static int ips_pci_attach(device_t dev)
61 {
62         ips_softc_t *sc;
63 
64         DEVICE_PRINTF(1, dev, "in attach.\n");
65         sc = (ips_softc_t *)device_get_softc(dev);
66         sc->dev = dev;
67 	mtx_init(&sc->queue_mtx, "IPS bioqueue lock", NULL, MTX_DEF);
68 	sema_init(&sc->cmd_sema, 0, "IPS Command Semaphore");
69 	callout_init_mtx(&sc->timer, &sc->queue_mtx, 0);
70 
71         if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){
72 		sc->ips_adapter_reinit = ips_morpheus_reinit;
73                 sc->ips_adapter_intr = ips_morpheus_intr;
74 		sc->ips_issue_cmd    = ips_issue_morpheus_cmd;
75 		sc->ips_poll_cmd     = ips_morpheus_poll;
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 		sc->ips_poll_cmd     = ips_copperhead_poll;
81 	} else if (pci_get_device(dev) == IPS_MARCO_DEVICE_ID){
82 		sc->ips_adapter_reinit = ips_morpheus_reinit;
83 		sc->ips_adapter_intr = ips_morpheus_intr;
84 		sc->ips_issue_cmd = ips_issue_morpheus_cmd;
85 		sc->ips_poll_cmd     = ips_morpheus_poll;
86 	} else
87                 goto error;
88         /* make sure busmastering is on */
89 	pci_enable_busmaster(dev);
90         /* setting up io space */
91         sc->iores = NULL;
92 	PRINTF(10, "trying MEMIO\n");
93 	if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)
94 		sc->rid = PCIR_BAR(1);
95 	else
96 		sc->rid = PCIR_BAR(0);
97 	sc->iotype = SYS_RES_MEMORY;
98 	sc->iores = bus_alloc_resource_any(dev, sc->iotype, &sc->rid,
99 	    RF_ACTIVE);
100         if(!sc->iores){
101                 PRINTF(10, "trying PORTIO\n");
102                 sc->rid = PCIR_BAR(0);
103                 sc->iotype = SYS_RES_IOPORT;
104                 sc->iores = bus_alloc_resource_any(dev, sc->iotype,
105 			&sc->rid, RF_ACTIVE);
106         }
107         if(sc->iores == NULL){
108                 device_printf(dev, "resource allocation failed\n");
109                 return (ENXIO);
110         }
111         /*allocate an interrupt. when does the irq become active? after leaving attach? */
112         sc->irqrid = 0;
113         if(!(sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ,
114 		&sc->irqrid, RF_SHAREABLE | RF_ACTIVE))){
115                 device_printf(dev, "irq allocation failed\n");
116                 goto error;
117         }
118 	if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO|INTR_MPSAFE, NULL,
119 	    sc->ips_adapter_intr, sc, &sc->irqcookie)){
120                 device_printf(dev, "irq setup failed\n");
121                 goto error;
122         }
123 	if (bus_dma_tag_create(	/* PCI parent */bus_get_dma_tag(dev),
124 				/* alignemnt */	1,
125 				/* boundary  */	0,
126 				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
127 				/* highaddr  */	BUS_SPACE_MAXADDR,
128 				/* filter    */	NULL,
129 				/* filterarg */	NULL,
130 				/* maxsize   */	BUS_SPACE_MAXSIZE_32BIT,
131 				/* numsegs   */	IPS_MAX_SG_ELEMENTS,
132 				/* maxsegsize*/	BUS_SPACE_MAXSIZE_32BIT,
133 				/* flags     */	0,
134 				/* lockfunc  */ NULL,
135 				/* lockarg   */ NULL,
136 				&sc->adapter_dmatag) != 0) {
137                 device_printf(dev, "can't alloc dma tag\n");
138                 goto error;
139         }
140 	sc->ips_ich.ich_func = ips_intrhook;
141 	sc->ips_ich.ich_arg = sc;
142 	bioq_init(&sc->queue);
143 	if (config_intrhook_establish(&sc->ips_ich) != 0) {
144 		printf("IPS can't establish configuration hook\n");
145 		goto error;
146 	}
147         return 0;
148 error:
149 	ips_pci_free(sc);
150         return (ENXIO);
151 }
152 
153 static void
154 ips_intrhook(void *arg)
155 {
156 	struct ips_softc *sc = (struct ips_softc *)arg;
157 
158 	config_intrhook_disestablish(&sc->ips_ich);
159 	if (ips_adapter_init(sc))
160 		ips_pci_free(sc);
161 	else
162 		sc->configured = 1;
163 }
164 
165 static int ips_pci_free(ips_softc_t *sc)
166 {
167 	if(sc->adapter_dmatag)
168 		bus_dma_tag_destroy(sc->adapter_dmatag);
169 	if(sc->irqcookie)
170                 bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
171         if(sc->irqres)
172                bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
173         if(sc->iores)
174                 bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
175 	sc->configured = 0;
176 	mtx_destroy(&sc->queue_mtx);
177 	sema_destroy(&sc->cmd_sema);
178 	return 0;
179 }
180 
181 static int ips_pci_detach(device_t dev)
182 {
183         ips_softc_t *sc;
184         DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
185         sc = (ips_softc_t *) device_get_softc(dev);
186 	if (sc->configured) {
187 		sc->configured = 0;
188 		ips_flush_cache(sc);
189 		if(ips_adapter_free(sc))
190 			return EBUSY;
191 		ips_pci_free(sc);
192 		bioq_flush(&sc->queue, NULL, ENXIO);
193 	}
194 	return 0;
195 }
196 
197 static int ips_pci_shutdown(device_t dev)
198 {
199 	ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev);
200 	if (sc->configured) {
201 		ips_flush_cache(sc);
202 	}
203 	return 0;
204 }
205 
206 static device_method_t ips_driver_methods[] = {
207         DEVMETHOD(device_probe, ips_pci_probe),
208         DEVMETHOD(device_attach, ips_pci_attach),
209         DEVMETHOD(device_detach, ips_pci_detach),
210 	DEVMETHOD(device_shutdown, ips_pci_shutdown),
211         {0,0}
212 };
213 
214 static driver_t ips_pci_driver = {
215         "ips",
216         ips_driver_methods,
217         sizeof(ips_softc_t),
218 };
219 
220 static devclass_t ips_devclass;
221 DRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, 0, 0);
222