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