ips_pci.c revision 143160
1114902Sscottl/*-
2114902Sscottl * Copyright (c) 2002 Adaptec Inc.
3114902Sscottl * All rights reserved.
4114902Sscottl *
5114902Sscottl * Written by: David Jeffery
6114902Sscottl *
7114902Sscottl * Redistribution and use in source and binary forms, with or without
8114902Sscottl * modification, are permitted provided that the following conditions
9114902Sscottl * are met:
10114902Sscottl * 1. Redistributions of source code must retain the above copyright
11114902Sscottl *    notice, this list of conditions and the following disclaimer.
12114902Sscottl * 2. Redistributions in binary form must reproduce the above copyright
13114902Sscottl *    notice, this list of conditions and the following disclaimer in the
14114902Sscottl *    documentation and/or other materials provided with the distribution.
15114902Sscottl *
16114902Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17114902Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18114902Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19114902Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20114902Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21114902Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22114902Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23114902Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24114902Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25114902Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26114902Sscottl * SUCH DAMAGE.
27114902Sscottl */
28114902Sscottl
29119418Sobrien#include <sys/cdefs.h>
30119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/ips/ips_pci.c 143160 2005-03-05 18:17:35Z imp $");
31114902Sscottl
32114902Sscottl#include <dev/ips/ips.h>
33119418Sobrien
34114902Sscottlstatic int ips_pci_free(ips_softc_t *sc);
35119997Spsstatic void ips_intrhook(void *arg);
36114902Sscottl
37114902Sscottlstatic int ips_pci_probe(device_t dev)
38114902Sscottl{
39114902Sscottl
40114902Sscottl        if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
41114902Sscottl	    (pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)) {
42114902Sscottl		device_set_desc(dev, "IBM ServeRAID Adapter");
43143160Simp                return (BUS_PROBE_DEFAULT);
44114902Sscottl        } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
45114902Sscottl	    (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)) {
46114902Sscottl		device_set_desc(dev, "IBM ServeRAID Adapter");
47143160Simp		return (BUS_PROBE_DEFAULT);
48127205Sscottl        } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID_ADAPTEC) &&
49127205Sscottl	    (pci_get_device(dev) == IPS_MARCO_DEVICE_ID)) {
50127205Sscottl		device_set_desc(dev, "Adaptec ServeRAID Adapter");
51143160Simp		return (BUS_PROBE_DEFAULT);
52127205Sscottl	}
53114902Sscottl        return(ENXIO);
54114902Sscottl}
55114902Sscottl
56114902Sscottlstatic int ips_pci_attach(device_t dev)
57114902Sscottl{
58114902Sscottl        u_int32_t command;
59114902Sscottl        ips_softc_t *sc;
60114902Sscottl
61116852Sscottl
62117167Sjhb	if (resource_disabled(device_get_name(dev), device_get_unit(dev))) {
63116852Sscottl		device_printf(dev, "device is disabled\n");
64116852Sscottl		/* but return 0 so the !$)$)*!$*) unit isn't reused */
65116852Sscottl		return (0);
66116852Sscottl	}
67114902Sscottl        DEVICE_PRINTF(1, dev, "in attach.\n");
68114902Sscottl        sc = (ips_softc_t *)device_get_softc(dev);
69114902Sscottl        if(!sc){
70114902Sscottl                printf("how is sc NULL?!\n");
71114902Sscottl                return (ENXIO);
72114902Sscottl        }
73114902Sscottl        bzero(sc, sizeof(ips_softc_t));
74114902Sscottl        sc->dev = dev;
75114902Sscottl
76114902Sscottl        if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){
77114902Sscottl		sc->ips_adapter_reinit = ips_morpheus_reinit;
78114902Sscottl                sc->ips_adapter_intr = ips_morpheus_intr;
79114902Sscottl		sc->ips_issue_cmd    = ips_issue_morpheus_cmd;
80141062Sscottl		sc->ips_poll_cmd     = ips_morpheus_poll;
81114902Sscottl        } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){
82114902Sscottl		sc->ips_adapter_reinit = ips_copperhead_reinit;
83114902Sscottl                sc->ips_adapter_intr = ips_copperhead_intr;
84114902Sscottl		sc->ips_issue_cmd    = ips_issue_copperhead_cmd;
85141062Sscottl		sc->ips_poll_cmd     = ips_copperhead_poll;
86127205Sscottl	} else if (pci_get_device(dev) == IPS_MARCO_DEVICE_ID){
87127205Sscottl		sc->ips_adapter_reinit = ips_morpheus_reinit;
88127205Sscottl		sc->ips_adapter_intr = ips_morpheus_intr;
89127205Sscottl		sc->ips_issue_cmd = ips_issue_morpheus_cmd;
90141062Sscottl		sc->ips_poll_cmd     = ips_morpheus_poll;
91114902Sscottl	} else
92114902Sscottl                goto error;
93114902Sscottl        /* make sure busmastering is on */
94114902Sscottl        command = pci_read_config(dev, PCIR_COMMAND, 1);
95114902Sscottl	command |= PCIM_CMD_BUSMASTEREN;
96114902Sscottl	pci_write_config(dev, PCIR_COMMAND, command, 1);
97114902Sscottl        /* seting up io space */
98114902Sscottl        sc->iores = NULL;
99114902Sscottl        if(command & PCIM_CMD_MEMEN){
100114902Sscottl                PRINTF(10, "trying MEMIO\n");
101127205Sscottl		if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)
102127205Sscottl                	sc->rid = PCIR_BAR(1);
103114902Sscottl		else
104127205Sscottl			sc->rid = PCIR_BAR(0);
105114902Sscottl                sc->iotype = SYS_RES_MEMORY;
106127135Snjl                sc->iores = bus_alloc_resource_any(dev, sc->iotype,
107127135Snjl			&sc->rid, RF_ACTIVE);
108114902Sscottl        }
109114902Sscottl        if(!sc->iores && command & PCIM_CMD_PORTEN){
110114902Sscottl                PRINTF(10, "trying PORTIO\n");
111119690Sjhb                sc->rid = PCIR_BAR(0);
112114902Sscottl                sc->iotype = SYS_RES_IOPORT;
113127135Snjl                sc->iores = bus_alloc_resource_any(dev, sc->iotype,
114127135Snjl			&sc->rid, RF_ACTIVE);
115114902Sscottl        }
116114902Sscottl        if(sc->iores == NULL){
117114902Sscottl                device_printf(dev, "resource allocation failed\n");
118114902Sscottl                return (ENXIO);
119114902Sscottl        }
120114902Sscottl        sc->bustag = rman_get_bustag(sc->iores);
121114902Sscottl        sc->bushandle = rman_get_bushandle(sc->iores);
122114902Sscottl        /*allocate an interrupt. when does the irq become active? after leaving attach? */
123114902Sscottl        sc->irqrid = 0;
124127135Snjl        if(!(sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ,
125127135Snjl		&sc->irqrid, RF_SHAREABLE | RF_ACTIVE))){
126114902Sscottl                device_printf(dev, "irq allocation failed\n");
127114902Sscottl                goto error;
128114902Sscottl        }
129140923Sscottl	if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO|INTR_MPSAFE, sc->ips_adapter_intr, sc, &sc->irqcookie)){
130114902Sscottl                device_printf(dev, "irq setup failed\n");
131114902Sscottl                goto error;
132114902Sscottl        }
133114902Sscottl	if (bus_dma_tag_create(	/* parent    */	NULL,
134114902Sscottl				/* alignemnt */	1,
135114902Sscottl				/* boundary  */	0,
136114902Sscottl				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
137114902Sscottl				/* highaddr  */	BUS_SPACE_MAXADDR,
138114902Sscottl				/* filter    */	NULL,
139114902Sscottl				/* filterarg */	NULL,
140114902Sscottl				/* maxsize   */	BUS_SPACE_MAXSIZE_32BIT,
141114902Sscottl				/* numsegs   */	IPS_MAX_SG_ELEMENTS,
142114902Sscottl				/* maxsegsize*/	BUS_SPACE_MAXSIZE_32BIT,
143114902Sscottl				/* flags     */	0,
144140923Sscottl				/* lockfunc  */ NULL,
145140923Sscottl				/* lockarg   */ NULL,
146114902Sscottl				&sc->adapter_dmatag) != 0) {
147114902Sscottl                printf("IPS can't alloc dma tag\n");
148114902Sscottl                goto error;
149114902Sscottl        }
150119997Sps	sc->ips_ich.ich_func = ips_intrhook;
151119997Sps	sc->ips_ich.ich_arg = sc;
152126364Sscottl	mtx_init(&sc->queue_mtx, "IPS bioqueue lock", MTX_DEF, 0);
153140923Sscottl	sema_init(&sc->cmd_sema, 0, "IPS Command Semaphore");
154126364Sscottl	bioq_init(&sc->queue);
155119997Sps	if (config_intrhook_establish(&sc->ips_ich) != 0) {
156119997Sps		printf("IPS can't establish configuration hook\n");
157114902Sscottl		goto error;
158119997Sps	}
159114902Sscottl        return 0;
160114902Sscottlerror:
161114902Sscottl	ips_pci_free(sc);
162114902Sscottl        return (ENXIO);
163114902Sscottl}
164114902Sscottl
165119997Spsstatic void
166119997Spsips_intrhook(void *arg)
167119997Sps{
168119997Sps	struct ips_softc *sc = (struct ips_softc *)arg;
169119997Sps
170119997Sps	config_intrhook_disestablish(&sc->ips_ich);
171119997Sps	if (ips_adapter_init(sc))
172119997Sps		ips_pci_free(sc);
173119997Sps	else
174119997Sps		sc->configured = 1;
175119997Sps}
176119997Sps
177114902Sscottlstatic int ips_pci_free(ips_softc_t *sc)
178114902Sscottl{
179114902Sscottl	if(sc->adapter_dmatag)
180114902Sscottl		bus_dma_tag_destroy(sc->adapter_dmatag);
181114902Sscottl	if(sc->irqcookie)
182114902Sscottl                bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
183114902Sscottl        if(sc->irqres)
184114902Sscottl               bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
185114902Sscottl        if(sc->iores)
186114902Sscottl                bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
187116852Sscottl	sc->configured = 0;
188140923Sscottl	mtx_destroy(&sc->queue_mtx);
189140923Sscottl	sema_destroy(&sc->cmd_sema);
190114902Sscottl	return 0;
191114902Sscottl}
192114902Sscottl
193114902Sscottlstatic int ips_pci_detach(device_t dev)
194114902Sscottl{
195114902Sscottl        ips_softc_t *sc;
196114902Sscottl        DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
197114902Sscottl        sc = (ips_softc_t *) device_get_softc(dev);
198116852Sscottl	if (sc->configured) {
199116852Sscottl		sc->configured = 0;
200116852Sscottl		ips_flush_cache(sc);
201116852Sscottl		if(ips_adapter_free(sc))
202116852Sscottl			return EBUSY;
203116852Sscottl		ips_pci_free(sc);
204126364Sscottl		bioq_flush(&sc->queue, NULL, ENXIO);
205116852Sscottl	}
206114902Sscottl	return 0;
207114902Sscottl}
208114902Sscottl
209114902Sscottlstatic int ips_pci_shutdown(device_t dev)
210114902Sscottl{
211114902Sscottl	ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev);
212116852Sscottl	if (sc->configured) {
213116852Sscottl		ips_flush_cache(sc);
214116852Sscottl	}
215114902Sscottl	return 0;
216114902Sscottl}
217114902Sscottl
218114902Sscottlstatic device_method_t ips_driver_methods[] = {
219114902Sscottl        DEVMETHOD(device_probe, ips_pci_probe),
220114902Sscottl        DEVMETHOD(device_attach, ips_pci_attach),
221114902Sscottl        DEVMETHOD(device_detach, ips_pci_detach),
222114902Sscottl	DEVMETHOD(device_shutdown, ips_pci_shutdown),
223114902Sscottl        {0,0}
224114902Sscottl};
225114902Sscottl
226114902Sscottlstatic driver_t ips_pci_driver = {
227114902Sscottl        "ips",
228114902Sscottl        ips_driver_methods,
229114902Sscottl        sizeof(ips_softc_t),
230114902Sscottl};
231114902Sscottl
232114902Sscottlstatic devclass_t ips_devclass;
233114902SscottlDRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, 0, 0);
234