xref: /dflybsd-src/sys/dev/raid/ips/ips_pci.c (revision 030b0c8c4cf27c560ccec70410c8e21934ae677d)
1edc6143bSDavid Rhodus /*-
2edc6143bSDavid Rhodus  * Copyright (c) 2002 Adaptec Inc.
3edc6143bSDavid Rhodus  * All rights reserved.
4edc6143bSDavid Rhodus  *
5edc6143bSDavid Rhodus  * Written by: David Jeffery
6edc6143bSDavid Rhodus  *
7edc6143bSDavid Rhodus  * Redistribution and use in source and binary forms, with or without
8edc6143bSDavid Rhodus  * modification, are permitted provided that the following conditions
9edc6143bSDavid Rhodus  * are met:
10edc6143bSDavid Rhodus  * 1. Redistributions of source code must retain the above copyright
11edc6143bSDavid Rhodus  *    notice, this list of conditions and the following disclaimer.
12edc6143bSDavid Rhodus  * 2. Redistributions in binary form must reproduce the above copyright
13edc6143bSDavid Rhodus  *    notice, this list of conditions and the following disclaimer in the
14edc6143bSDavid Rhodus  *    documentation and/or other materials provided with the distribution.
15edc6143bSDavid Rhodus  *
16edc6143bSDavid Rhodus  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17edc6143bSDavid Rhodus  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18edc6143bSDavid Rhodus  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19edc6143bSDavid Rhodus  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20edc6143bSDavid Rhodus  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21edc6143bSDavid Rhodus  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22edc6143bSDavid Rhodus  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23edc6143bSDavid Rhodus  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24edc6143bSDavid Rhodus  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25edc6143bSDavid Rhodus  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26edc6143bSDavid Rhodus  * SUCH DAMAGE.
27edc6143bSDavid Rhodus  *
28c27aad16SJoerg Sonnenberger  * $FreeBSD: src/sys/dev/ips/ips_pci.c,v 1.10 2004/03/19 17:36:47 scottl Exp $
29edc6143bSDavid Rhodus  */
30edc6143bSDavid Rhodus 
31edc6143bSDavid Rhodus #include <dev/raid/ips/ips.h>
32edc6143bSDavid Rhodus 
33edc6143bSDavid Rhodus static int ips_pci_free(ips_softc_t *sc);
34edc6143bSDavid Rhodus static void ips_intrhook(void *arg);
35edc6143bSDavid Rhodus 
3630fe4a89SJoerg Sonnenberger static struct ips_pci_product {
3730fe4a89SJoerg Sonnenberger 	uint16_t vendor, device;
3855591fc2SJoerg Sonnenberger 	const char *desc;
3955591fc2SJoerg Sonnenberger 	int (*ips_adapter_reinit)(struct ips_softc *, int);
4030fe4a89SJoerg Sonnenberger 	void (*ips_adapter_intr)(void *);
4130fe4a89SJoerg Sonnenberger 	void (*ips_issue_cmd)(ips_command_t *);
424751a114SMatthew Dillon 	void (*ips_poll_cmd)(ips_command_t *);
4355591fc2SJoerg Sonnenberger } ips_pci_products[] = {
4430fe4a89SJoerg Sonnenberger 	{ IPS_VENDOR_ID, IPS_MORPHEUS_DEVICE_ID, "IBM ServeRAID Adapter",
454751a114SMatthew Dillon 	  ips_morpheus_reinit, ips_morpheus_intr, ips_issue_morpheus_cmd,
464751a114SMatthew Dillon 	  ips_morpheus_poll },
4730fe4a89SJoerg Sonnenberger 	{ IPS_VENDOR_ID, IPS_COPPERHEAD_DEVICE_ID, "IBM ServeRAID Adapter",
484751a114SMatthew Dillon 	  ips_copperhead_reinit, ips_copperhead_intr, ips_issue_copperhead_cmd,
494751a114SMatthew Dillon 	  ips_copperhead_poll },
5071b118e1SMatthew Dillon 	{ IPS_VENDOR_ID_ADAPTEC, IPS_MARCO_DEVICE_ID,
5171b118e1SMatthew Dillon 	  "Adaptec ServeRAID Adapter", ips_morpheus_reinit, ips_morpheus_intr,
524751a114SMatthew Dillon 	  ips_issue_morpheus_cmd, ips_morpheus_poll },
5330fe4a89SJoerg Sonnenberger 	{ 0, 0, NULL }
5430fe4a89SJoerg Sonnenberger };
5530fe4a89SJoerg Sonnenberger 
56edc6143bSDavid Rhodus static int
ips_pci_probe(device_t dev)57edc6143bSDavid Rhodus ips_pci_probe(device_t dev)
58edc6143bSDavid Rhodus {
5930fe4a89SJoerg Sonnenberger 	uint16_t vendor = pci_get_vendor(dev);
6030fe4a89SJoerg Sonnenberger 	uint16_t device = pci_get_device(dev);
6155591fc2SJoerg Sonnenberger 	struct ips_pci_product *pp;
6230fe4a89SJoerg Sonnenberger 	ips_softc_t *sc;
6330fe4a89SJoerg Sonnenberger 
6430fe4a89SJoerg Sonnenberger 	for (pp = ips_pci_products; pp->vendor; pp++) {
6530fe4a89SJoerg Sonnenberger 		if (vendor == pp->vendor && device == pp->device) {
6630fe4a89SJoerg Sonnenberger 			sc = (ips_softc_t *)device_get_softc(dev);
6755591fc2SJoerg Sonnenberger 			sc->ips_adapter_reinit = pp->ips_adapter_reinit;
6855591fc2SJoerg Sonnenberger 			sc->ips_adapter_intr = pp->ips_adapter_intr;
6955591fc2SJoerg Sonnenberger 			sc->ips_issue_cmd = pp->ips_issue_cmd;
704751a114SMatthew Dillon 			sc->ips_poll_cmd = pp->ips_poll_cmd;
7130fe4a89SJoerg Sonnenberger 			device_set_desc(dev, pp->desc);
72edc6143bSDavid Rhodus 			return (0);
7330fe4a89SJoerg Sonnenberger 		}
74edc6143bSDavid Rhodus 	}
75edc6143bSDavid Rhodus 	return (ENXIO);
76edc6143bSDavid Rhodus }
77edc6143bSDavid Rhodus 
78edc6143bSDavid Rhodus static int
ips_pci_attach(device_t dev)79edc6143bSDavid Rhodus ips_pci_attach(device_t dev)
80edc6143bSDavid Rhodus {
81edc6143bSDavid Rhodus 	u_int32_t command;
82edc6143bSDavid Rhodus 	ips_softc_t *sc;
83e9cb6d99SMatthew Dillon 	int error;
84edc6143bSDavid Rhodus 
85edc6143bSDavid Rhodus 	if (resource_disabled(device_get_name(dev), device_get_unit(dev))) {
86edc6143bSDavid Rhodus 		device_printf(dev, "device is disabled\n");
87edc6143bSDavid Rhodus 		/* but return 0 so the !$)$)*!$*) unit isn't reused */
88edc6143bSDavid Rhodus 		return (0);
89edc6143bSDavid Rhodus 	}
90edc6143bSDavid Rhodus 	DEVICE_PRINTF(1, dev, "in attach.\n");
91edc6143bSDavid Rhodus 	sc = (ips_softc_t *)device_get_softc(dev);
92edc6143bSDavid Rhodus 	sc->dev = dev;
93edc6143bSDavid Rhodus 	/* make sure busmastering is on */
9430fe4a89SJoerg Sonnenberger 	pci_enable_busmaster(dev);
95edc6143bSDavid Rhodus 	command = pci_read_config(dev, PCIR_COMMAND, 1);
96edc6143bSDavid Rhodus 	/* seting up io space */
97edc6143bSDavid Rhodus 	sc->iores = NULL;
98edc6143bSDavid Rhodus 	if (command & PCIM_CMD_MEMEN) {
99edc6143bSDavid Rhodus 		PRINTF(10, "trying MEMIO\n");
100c27aad16SJoerg Sonnenberger 		if (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)
101edc6143bSDavid Rhodus 			sc->rid = PCIR_BAR(1);
102c27aad16SJoerg Sonnenberger 		else
103c27aad16SJoerg Sonnenberger 			sc->rid = PCIR_BAR(0);
104edc6143bSDavid Rhodus 		sc->iotype = SYS_RES_MEMORY;
105c27aad16SJoerg Sonnenberger 		sc->iores = bus_alloc_resource_any(dev, sc->iotype,
106c27aad16SJoerg Sonnenberger 		    &sc->rid, RF_ACTIVE);
107edc6143bSDavid Rhodus 	}
108edc6143bSDavid Rhodus 	if (sc->iores == NULL && command & PCIM_CMD_PORTEN) {
109edc6143bSDavid Rhodus 		PRINTF(10, "trying PORTIO\n");
110edc6143bSDavid Rhodus 		sc->rid = PCIR_BAR(0);
111edc6143bSDavid Rhodus 		sc->iotype = SYS_RES_IOPORT;
112c27aad16SJoerg Sonnenberger 		sc->iores = bus_alloc_resource_any(dev, sc->iotype,
113c27aad16SJoerg Sonnenberger 		    &sc->rid, RF_ACTIVE);
114edc6143bSDavid Rhodus 	}
115edc6143bSDavid Rhodus 	if (sc->iores == NULL) {
116edc6143bSDavid Rhodus 		device_printf(dev, "resource allocation failed\n");
117edc6143bSDavid Rhodus 		return (ENXIO);
118edc6143bSDavid Rhodus 	}
119edc6143bSDavid Rhodus 	sc->bustag = rman_get_bustag(sc->iores);
120edc6143bSDavid Rhodus 	sc->bushandle = rman_get_bushandle(sc->iores);
121edc6143bSDavid Rhodus 	/*
122edc6143bSDavid Rhodus 	 * allocate an interrupt. when does the irq become active?
123edc6143bSDavid Rhodus 	 * after leaving attach?
124edc6143bSDavid Rhodus 	 */
125edc6143bSDavid Rhodus 	sc->irqrid = 0;
126c27aad16SJoerg Sonnenberger 	if ((sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ,
127c27aad16SJoerg Sonnenberger 	    &sc->irqrid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
128edc6143bSDavid Rhodus 		device_printf(dev, "irq allocation failed\n");
129edc6143bSDavid Rhodus 		goto error;
130edc6143bSDavid Rhodus 	}
131ee61f228SMatthew Dillon 	error = bus_setup_intr(dev, sc->irqres, 0,
132e9cb6d99SMatthew Dillon 			       sc->ips_adapter_intr, sc,
133e9cb6d99SMatthew Dillon 			       &sc->irqcookie, NULL);
134e9cb6d99SMatthew Dillon 	if (error) {
135edc6143bSDavid Rhodus 		device_printf(dev, "irq setup failed\n");
136edc6143bSDavid Rhodus 		goto error;
137edc6143bSDavid Rhodus 	}
138edc6143bSDavid Rhodus 	if (bus_dma_tag_create(	/* parent    */	NULL,
139edc6143bSDavid Rhodus 				/* alignemnt */	1,
140edc6143bSDavid Rhodus 				/* boundary  */	0,
141edc6143bSDavid Rhodus 				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
142edc6143bSDavid Rhodus 				/* highaddr  */	BUS_SPACE_MAXADDR,
143edc6143bSDavid Rhodus 				/* maxsize   */	BUS_SPACE_MAXSIZE_32BIT,
144edc6143bSDavid Rhodus 				/* numsegs   */	IPS_MAX_SG_ELEMENTS,
145edc6143bSDavid Rhodus 				/* maxsegsize*/	BUS_SPACE_MAXSIZE_32BIT,
146edc6143bSDavid Rhodus 				/* flags     */	0,
147edc6143bSDavid Rhodus 				&sc->adapter_dmatag) != 0) {
148e3869ec7SSascha Wildner 		kprintf("IPS can't alloc dma tag\n");
149edc6143bSDavid Rhodus 		goto error;
150edc6143bSDavid Rhodus 	}
151edc6143bSDavid Rhodus 	sc->ips_ich.ich_func = ips_intrhook;
152edc6143bSDavid Rhodus 	sc->ips_ich.ich_arg = sc;
1537493964bSYONETANI Tomokazu 	sc->ips_ich.ich_desc = "ips";
154845f5eadSYONETANI Tomokazu 	lockinit(&sc->queue_lock, "ipslk", 0, LK_CANRECURSE);
15581b5c339SMatthew Dillon 	bioq_init(&sc->bio_queue);
156edc6143bSDavid Rhodus 	if (config_intrhook_establish(&sc->ips_ich) != 0) {
157e3869ec7SSascha Wildner 		kprintf("IPS can't establish configuration hook\n");
158edc6143bSDavid Rhodus 		goto error;
159edc6143bSDavid Rhodus 	}
160edc6143bSDavid Rhodus 	return 0;
161edc6143bSDavid Rhodus error:
162edc6143bSDavid Rhodus 	ips_pci_free(sc);
163edc6143bSDavid Rhodus 	return (ENXIO);
164edc6143bSDavid Rhodus }
165edc6143bSDavid Rhodus 
166edc6143bSDavid Rhodus static void
ips_intrhook(void * arg)167edc6143bSDavid Rhodus ips_intrhook(void *arg)
168edc6143bSDavid Rhodus {
169edc6143bSDavid Rhodus 	struct ips_softc *sc = arg;
170edc6143bSDavid Rhodus 
171edc6143bSDavid Rhodus 	config_intrhook_disestablish(&sc->ips_ich);
172edc6143bSDavid Rhodus 	if (ips_adapter_init(sc))
173edc6143bSDavid Rhodus 		ips_pci_free(sc);
174edc6143bSDavid Rhodus 	else
175edc6143bSDavid Rhodus 		sc->configured = 1;
176edc6143bSDavid Rhodus }
177edc6143bSDavid Rhodus 
178edc6143bSDavid Rhodus static int
ips_pci_free(ips_softc_t * sc)179edc6143bSDavid Rhodus ips_pci_free(ips_softc_t *sc)
180edc6143bSDavid Rhodus {
181edc6143bSDavid Rhodus 
182edc6143bSDavid Rhodus 	if (sc->adapter_dmatag)
183edc6143bSDavid Rhodus 		bus_dma_tag_destroy(sc->adapter_dmatag);
184edc6143bSDavid Rhodus 	if (sc->irqcookie)
185edc6143bSDavid Rhodus 		bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
186edc6143bSDavid Rhodus 	if (sc->irqres)
187edc6143bSDavid Rhodus 		bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid,
188edc6143bSDavid Rhodus 		    sc->irqres);
189edc6143bSDavid Rhodus 	if (sc->iores)
190edc6143bSDavid Rhodus 		bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
191edc6143bSDavid Rhodus 	sc->configured = 0;
192edc6143bSDavid Rhodus 	return 0;
193edc6143bSDavid Rhodus }
194edc6143bSDavid Rhodus 
195edc6143bSDavid Rhodus static int
ips_pci_detach(device_t dev)196edc6143bSDavid Rhodus ips_pci_detach(device_t dev)
197edc6143bSDavid Rhodus {
198edc6143bSDavid Rhodus 	ips_softc_t *sc;
199edc6143bSDavid Rhodus 
200edc6143bSDavid Rhodus 	DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
201edc6143bSDavid Rhodus 	sc = (ips_softc_t *)device_get_softc(dev);
202edc6143bSDavid Rhodus 	if (sc->configured) {
203edc6143bSDavid Rhodus 		sc->configured = 0;
204edc6143bSDavid Rhodus 		ips_flush_cache(sc);
205edc6143bSDavid Rhodus 		if (ips_adapter_free(sc))
206edc6143bSDavid Rhodus 			return EBUSY;
207edc6143bSDavid Rhodus 		ips_pci_free(sc);
208edc6143bSDavid Rhodus 	}
209edc6143bSDavid Rhodus 	return 0;
210edc6143bSDavid Rhodus }
211edc6143bSDavid Rhodus 
212edc6143bSDavid Rhodus static int
ips_pci_shutdown(device_t dev)213edc6143bSDavid Rhodus ips_pci_shutdown(device_t dev)
214edc6143bSDavid Rhodus {
215edc6143bSDavid Rhodus 	ips_softc_t *sc;
216edc6143bSDavid Rhodus 
217edc6143bSDavid Rhodus 	sc = (ips_softc_t *)device_get_softc(dev);
218edc6143bSDavid Rhodus 	if (sc->configured)
219edc6143bSDavid Rhodus 		ips_flush_cache(sc);
220edc6143bSDavid Rhodus 	return 0;
221edc6143bSDavid Rhodus }
222edc6143bSDavid Rhodus 
223edc6143bSDavid Rhodus static device_method_t ips_driver_methods[] = {
224edc6143bSDavid Rhodus 	DEVMETHOD(device_probe, ips_pci_probe),
225edc6143bSDavid Rhodus 	DEVMETHOD(device_attach, ips_pci_attach),
226edc6143bSDavid Rhodus 	DEVMETHOD(device_detach, ips_pci_detach),
227edc6143bSDavid Rhodus 	DEVMETHOD(device_shutdown, ips_pci_shutdown),
228*d3c9c58eSSascha Wildner 	DEVMETHOD_END
229edc6143bSDavid Rhodus };
230edc6143bSDavid Rhodus 
231edc6143bSDavid Rhodus static driver_t ips_pci_driver = {
232edc6143bSDavid Rhodus 	"ips",
233edc6143bSDavid Rhodus 	ips_driver_methods,
234edc6143bSDavid Rhodus 	sizeof(ips_softc_t),
235edc6143bSDavid Rhodus };
236edc6143bSDavid Rhodus 
237edc6143bSDavid Rhodus static devclass_t ips_devclass;
238aa2b9d05SSascha Wildner DRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, NULL, NULL);
239