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