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