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$");
31114902Sscottl
32152919Sscottl#include <dev/ips/ipsreg.h>
33114902Sscottl#include <dev/ips/ips.h>
34119418Sobrien
35152919Sscottl#include <dev/pci/pcireg.h>
36152919Sscottl#include <dev/pci/pcivar.h>
37152919Sscottl
38114902Sscottlstatic int ips_pci_free(ips_softc_t *sc);
39119997Spsstatic void ips_intrhook(void *arg);
40114902Sscottl
41114902Sscottlstatic int ips_pci_probe(device_t dev)
42114902Sscottl{
43114902Sscottl
44114902Sscottl        if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
45114902Sscottl	    (pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)) {
46114902Sscottl		device_set_desc(dev, "IBM ServeRAID Adapter");
47143160Simp                return (BUS_PROBE_DEFAULT);
48114902Sscottl        } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
49114902Sscottl	    (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)) {
50114902Sscottl		device_set_desc(dev, "IBM ServeRAID Adapter");
51143160Simp		return (BUS_PROBE_DEFAULT);
52127205Sscottl        } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID_ADAPTEC) &&
53127205Sscottl	    (pci_get_device(dev) == IPS_MARCO_DEVICE_ID)) {
54127205Sscottl		device_set_desc(dev, "Adaptec ServeRAID Adapter");
55143160Simp		return (BUS_PROBE_DEFAULT);
56127205Sscottl	}
57114902Sscottl        return(ENXIO);
58114902Sscottl}
59114902Sscottl
60114902Sscottlstatic int ips_pci_attach(device_t dev)
61114902Sscottl{
62114902Sscottl        ips_softc_t *sc;
63114902Sscottl
64116852Sscottl
65117167Sjhb	if (resource_disabled(device_get_name(dev), device_get_unit(dev))) {
66116852Sscottl		device_printf(dev, "device is disabled\n");
67116852Sscottl		/* but return 0 so the !$)$)*!$*) unit isn't reused */
68116852Sscottl		return (0);
69116852Sscottl	}
70114902Sscottl        DEVICE_PRINTF(1, dev, "in attach.\n");
71114902Sscottl        sc = (ips_softc_t *)device_get_softc(dev);
72114902Sscottl        if(!sc){
73114902Sscottl                printf("how is sc NULL?!\n");
74114902Sscottl                return (ENXIO);
75114902Sscottl        }
76114902Sscottl        bzero(sc, sizeof(ips_softc_t));
77114902Sscottl        sc->dev = dev;
78114902Sscottl
79114902Sscottl        if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){
80114902Sscottl		sc->ips_adapter_reinit = ips_morpheus_reinit;
81114902Sscottl                sc->ips_adapter_intr = ips_morpheus_intr;
82114902Sscottl		sc->ips_issue_cmd    = ips_issue_morpheus_cmd;
83141062Sscottl		sc->ips_poll_cmd     = ips_morpheus_poll;
84114902Sscottl        } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){
85114902Sscottl		sc->ips_adapter_reinit = ips_copperhead_reinit;
86114902Sscottl                sc->ips_adapter_intr = ips_copperhead_intr;
87114902Sscottl		sc->ips_issue_cmd    = ips_issue_copperhead_cmd;
88141062Sscottl		sc->ips_poll_cmd     = ips_copperhead_poll;
89127205Sscottl	} else if (pci_get_device(dev) == IPS_MARCO_DEVICE_ID){
90127205Sscottl		sc->ips_adapter_reinit = ips_morpheus_reinit;
91127205Sscottl		sc->ips_adapter_intr = ips_morpheus_intr;
92127205Sscottl		sc->ips_issue_cmd = ips_issue_morpheus_cmd;
93141062Sscottl		sc->ips_poll_cmd     = ips_morpheus_poll;
94114902Sscottl	} else
95114902Sscottl                goto error;
96114902Sscottl        /* make sure busmastering is on */
97254263Sscottl	pci_enable_busmaster(dev);
98114902Sscottl        /* seting up io space */
99114902Sscottl        sc->iores = NULL;
100254263Sscottl	PRINTF(10, "trying MEMIO\n");
101254263Sscottl	if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)
102254263Sscottl		sc->rid = PCIR_BAR(1);
103254263Sscottl	else
104254263Sscottl		sc->rid = PCIR_BAR(0);
105254263Sscottl	sc->iotype = SYS_RES_MEMORY;
106254263Sscottl	sc->iores = bus_alloc_resource_any(dev, sc->iotype, &sc->rid,
107254263Sscottl	    RF_ACTIVE);
108254263Sscottl        if(!sc->iores){
109114902Sscottl                PRINTF(10, "trying PORTIO\n");
110119690Sjhb                sc->rid = PCIR_BAR(0);
111114902Sscottl                sc->iotype = SYS_RES_IOPORT;
112127135Snjl                sc->iores = bus_alloc_resource_any(dev, sc->iotype,
113127135Snjl			&sc->rid, RF_ACTIVE);
114114902Sscottl        }
115114902Sscottl        if(sc->iores == NULL){
116114902Sscottl                device_printf(dev, "resource allocation failed\n");
117114902Sscottl                return (ENXIO);
118114902Sscottl        }
119114902Sscottl        sc->bustag = rman_get_bustag(sc->iores);
120114902Sscottl        sc->bushandle = rman_get_bushandle(sc->iores);
121114902Sscottl        /*allocate an interrupt. when does the irq become active? after leaving attach? */
122114902Sscottl        sc->irqrid = 0;
123127135Snjl        if(!(sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ,
124127135Snjl		&sc->irqrid, RF_SHAREABLE | RF_ACTIVE))){
125114902Sscottl                device_printf(dev, "irq allocation failed\n");
126114902Sscottl                goto error;
127114902Sscottl        }
128166901Spiso	if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO|INTR_MPSAFE, NULL,
129166901Spiso	    sc->ips_adapter_intr, sc, &sc->irqcookie)){
130114902Sscottl                device_printf(dev, "irq setup failed\n");
131114902Sscottl                goto error;
132114902Sscottl        }
133232854Sscottl	if (bus_dma_tag_create(	/* PCI parent */bus_get_dma_tag(dev),
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;
152200620Smjacob	mtx_init(&sc->queue_mtx, "IPS bioqueue lock", NULL, MTX_DEF);
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