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