ips_pci.c revision 117126
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 * $FreeBSD: head/sys/dev/ips/ips_pci.c 117126 2003-07-01 15:52:06Z scottl $
29114902Sscottl */
30114902Sscottl
31114902Sscottl
32114902Sscottl#include <dev/ips/ips.h>
33114902Sscottlstatic int ips_pci_free(ips_softc_t *sc);
34114902Sscottl
35114902Sscottlstatic int ips_pci_probe(device_t dev)
36114902Sscottl{
37114902Sscottl
38114902Sscottl        if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
39114902Sscottl	    (pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)) {
40114902Sscottl		device_set_desc(dev, "IBM ServeRAID Adapter");
41114902Sscottl                return 0;
42114902Sscottl        } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
43114902Sscottl	    (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)) {
44114902Sscottl		device_set_desc(dev, "IBM ServeRAID Adapter");
45114902Sscottl		return (0);
46114902Sscottl        }
47114902Sscottl        return(ENXIO);
48114902Sscottl}
49114902Sscottl
50114902Sscottlstatic int ips_pci_attach(device_t dev)
51114902Sscottl{
52114902Sscottl        u_int32_t command;
53116852Sscottl        int tval;
54114902Sscottl        ips_softc_t *sc;
55114902Sscottl
56116852Sscottl
57116852Sscottl	tval = 0;
58116852Sscottl	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
59116852Sscottl	    "disable", &tval) == 0 && tval) {
60116852Sscottl		device_printf(dev, "device is disabled\n");
61116852Sscottl		/* but return 0 so the !$)$)*!$*) unit isn't reused */
62116852Sscottl		return (0);
63116852Sscottl	}
64114902Sscottl        DEVICE_PRINTF(1, dev, "in attach.\n");
65114902Sscottl        sc = (ips_softc_t *)device_get_softc(dev);
66114902Sscottl        if(!sc){
67114902Sscottl                printf("how is sc NULL?!\n");
68114902Sscottl                return (ENXIO);
69114902Sscottl        }
70114902Sscottl        bzero(sc, sizeof(ips_softc_t));
71114902Sscottl        sc->dev = dev;
72114902Sscottl
73114902Sscottl        if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){
74114902Sscottl		sc->ips_adapter_reinit = ips_morpheus_reinit;
75114902Sscottl                sc->ips_adapter_intr = ips_morpheus_intr;
76114902Sscottl		sc->ips_issue_cmd    = ips_issue_morpheus_cmd;
77114902Sscottl        } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){
78114902Sscottl		sc->ips_adapter_reinit = ips_copperhead_reinit;
79114902Sscottl                sc->ips_adapter_intr = ips_copperhead_intr;
80114902Sscottl		sc->ips_issue_cmd    = ips_issue_copperhead_cmd;
81114902Sscottl	} else
82114902Sscottl                goto error;
83114902Sscottl        /* make sure busmastering is on */
84114902Sscottl        command = pci_read_config(dev, PCIR_COMMAND, 1);
85114902Sscottl	command |= PCIM_CMD_BUSMASTEREN;
86114902Sscottl	pci_write_config(dev, PCIR_COMMAND, command, 1);
87114902Sscottl        /* seting up io space */
88114902Sscottl        sc->iores = NULL;
89114902Sscottl        if(command & PCIM_CMD_MEMEN){
90114902Sscottl                PRINTF(10, "trying MEMIO\n");
91114902Sscottl		if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)
92114902Sscottl                	sc->rid = PCIR_MAPS;
93114902Sscottl		else
94114902Sscottl			sc->rid = PCIR_MAPS + 4;
95114902Sscottl                sc->iotype = SYS_RES_MEMORY;
96114902Sscottl                sc->iores = bus_alloc_resource(dev, sc->iotype, &sc->rid, 0, ~0, 1, RF_ACTIVE);
97114902Sscottl        }
98114902Sscottl        if(!sc->iores && command & PCIM_CMD_PORTEN){
99114902Sscottl                PRINTF(10, "trying PORTIO\n");
100114902Sscottl                sc->rid = PCIR_MAPS;
101114902Sscottl                sc->iotype = SYS_RES_IOPORT;
102114902Sscottl                sc->iores = bus_alloc_resource(dev, sc->iotype, &sc->rid, 0, ~0, 1, RF_ACTIVE);
103114902Sscottl        }
104114902Sscottl        if(sc->iores == NULL){
105114902Sscottl                device_printf(dev, "resource allocation failed\n");
106114902Sscottl                return (ENXIO);
107114902Sscottl        }
108114902Sscottl        sc->bustag = rman_get_bustag(sc->iores);
109114902Sscottl        sc->bushandle = rman_get_bushandle(sc->iores);
110114902Sscottl        /*allocate an interrupt. when does the irq become active? after leaving attach? */
111114902Sscottl        sc->irqrid = 0;
112114902Sscottl        if(!(sc->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irqrid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE))){
113114902Sscottl                device_printf(dev, "irq allocation failed\n");
114114902Sscottl                goto error;
115114902Sscottl        }
116114902Sscottl	if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO, sc->ips_adapter_intr, sc, &sc->irqcookie)){
117114902Sscottl                device_printf(dev, "irq setup failed\n");
118114902Sscottl                goto error;
119114902Sscottl        }
120114902Sscottl	if (bus_dma_tag_create(	/* parent    */	NULL,
121114902Sscottl				/* alignemnt */	1,
122114902Sscottl				/* boundary  */	0,
123114902Sscottl				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
124114902Sscottl				/* highaddr  */	BUS_SPACE_MAXADDR,
125114902Sscottl				/* filter    */	NULL,
126114902Sscottl				/* filterarg */	NULL,
127114902Sscottl				/* maxsize   */	BUS_SPACE_MAXSIZE_32BIT,
128114902Sscottl				/* numsegs   */	IPS_MAX_SG_ELEMENTS,
129114902Sscottl				/* maxsegsize*/	BUS_SPACE_MAXSIZE_32BIT,
130114902Sscottl				/* flags     */	0,
131117126Sscottl				/* lockfunc  */ busdma_lock_mutex,
132117126Sscottl				/* lockarg   */ &Giant,
133114902Sscottl				&sc->adapter_dmatag) != 0) {
134114902Sscottl                printf("IPS can't alloc dma tag\n");
135114902Sscottl                goto error;
136114902Sscottl        }
137114902Sscottl	if(ips_adapter_init(sc))
138114902Sscottl		goto error;
139116852Sscottl        sc->configured = 1;
140114902Sscottl        return 0;
141114902Sscottlerror:
142114902Sscottl	ips_pci_free(sc);
143114902Sscottl        return (ENXIO);
144114902Sscottl}
145114902Sscottl
146114902Sscottlstatic int ips_pci_free(ips_softc_t *sc)
147114902Sscottl{
148114902Sscottl	if(sc->adapter_dmatag)
149114902Sscottl		bus_dma_tag_destroy(sc->adapter_dmatag);
150114902Sscottl	if(sc->irqcookie)
151114902Sscottl                bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
152114902Sscottl        if(sc->irqres)
153114902Sscottl               bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
154114902Sscottl        if(sc->iores)
155114902Sscottl                bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
156116852Sscottl	sc->configured = 0;
157114902Sscottl	return 0;
158114902Sscottl}
159114902Sscottl
160114902Sscottlstatic int ips_pci_detach(device_t dev)
161114902Sscottl{
162114902Sscottl        ips_softc_t *sc;
163114902Sscottl        DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
164114902Sscottl        sc = (ips_softc_t *) device_get_softc(dev);
165116852Sscottl	if (sc->configured) {
166116852Sscottl		sc->configured = 0;
167116852Sscottl		ips_flush_cache(sc);
168116852Sscottl		if(ips_adapter_free(sc))
169116852Sscottl			return EBUSY;
170116852Sscottl		ips_pci_free(sc);
171116852Sscottl		mtx_destroy(&sc->cmd_mtx);
172116852Sscottl	}
173114902Sscottl	return 0;
174114902Sscottl}
175114902Sscottl
176114902Sscottlstatic int ips_pci_shutdown(device_t dev)
177114902Sscottl{
178114902Sscottl	ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev);
179116852Sscottl	if (sc->configured) {
180116852Sscottl		ips_flush_cache(sc);
181116852Sscottl	}
182114902Sscottl	return 0;
183114902Sscottl}
184114902Sscottl
185114902Sscottlstatic device_method_t ips_driver_methods[] = {
186114902Sscottl        DEVMETHOD(device_probe, ips_pci_probe),
187114902Sscottl        DEVMETHOD(device_attach, ips_pci_attach),
188114902Sscottl        DEVMETHOD(device_detach, ips_pci_detach),
189114902Sscottl	DEVMETHOD(device_shutdown, ips_pci_shutdown),
190114902Sscottl        {0,0}
191114902Sscottl};
192114902Sscottl
193114902Sscottlstatic driver_t ips_pci_driver = {
194114902Sscottl        "ips",
195114902Sscottl        ips_driver_methods,
196114902Sscottl        sizeof(ips_softc_t),
197114902Sscottl};
198114902Sscottl
199114902Sscottlstatic devclass_t ips_devclass;
200114902SscottlDRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, 0, 0);
201