ips_pci.c revision 114902
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 114902 2003-05-11 06:36:49Z 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;
53114902Sscottl        ips_softc_t *sc;
54114902Sscottl
55114902Sscottl        DEVICE_PRINTF(1, dev, "in attach.\n");
56114902Sscottl        sc = (ips_softc_t *)device_get_softc(dev);
57114902Sscottl        if(!sc){
58114902Sscottl                printf("how is sc NULL?!\n");
59114902Sscottl                return (ENXIO);
60114902Sscottl        }
61114902Sscottl        bzero(sc, sizeof(ips_softc_t));
62114902Sscottl        sc->dev = dev;
63114902Sscottl
64114902Sscottl        if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){
65114902Sscottl		sc->ips_adapter_reinit = ips_morpheus_reinit;
66114902Sscottl                sc->ips_adapter_intr = ips_morpheus_intr;
67114902Sscottl		sc->ips_issue_cmd    = ips_issue_morpheus_cmd;
68114902Sscottl        } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){
69114902Sscottl		sc->ips_adapter_reinit = ips_copperhead_reinit;
70114902Sscottl                sc->ips_adapter_intr = ips_copperhead_intr;
71114902Sscottl		sc->ips_issue_cmd    = ips_issue_copperhead_cmd;
72114902Sscottl	} else
73114902Sscottl                goto error;
74114902Sscottl        /* make sure busmastering is on */
75114902Sscottl        command = pci_read_config(dev, PCIR_COMMAND, 1);
76114902Sscottl	command |= PCIM_CMD_BUSMASTEREN;
77114902Sscottl	pci_write_config(dev, PCIR_COMMAND, command, 1);
78114902Sscottl        /* seting up io space */
79114902Sscottl        sc->iores = NULL;
80114902Sscottl        if(command & PCIM_CMD_MEMEN){
81114902Sscottl                PRINTF(10, "trying MEMIO\n");
82114902Sscottl		if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)
83114902Sscottl                	sc->rid = PCIR_MAPS;
84114902Sscottl		else
85114902Sscottl			sc->rid = PCIR_MAPS + 4;
86114902Sscottl                sc->iotype = SYS_RES_MEMORY;
87114902Sscottl                sc->iores = bus_alloc_resource(dev, sc->iotype, &sc->rid, 0, ~0, 1, RF_ACTIVE);
88114902Sscottl        }
89114902Sscottl        if(!sc->iores && command & PCIM_CMD_PORTEN){
90114902Sscottl                PRINTF(10, "trying PORTIO\n");
91114902Sscottl                sc->rid = PCIR_MAPS;
92114902Sscottl                sc->iotype = SYS_RES_IOPORT;
93114902Sscottl                sc->iores = bus_alloc_resource(dev, sc->iotype, &sc->rid, 0, ~0, 1, RF_ACTIVE);
94114902Sscottl        }
95114902Sscottl        if(sc->iores == NULL){
96114902Sscottl                device_printf(dev, "resource allocation failed\n");
97114902Sscottl                return (ENXIO);
98114902Sscottl        }
99114902Sscottl        sc->bustag = rman_get_bustag(sc->iores);
100114902Sscottl        sc->bushandle = rman_get_bushandle(sc->iores);
101114902Sscottl        /*allocate an interrupt. when does the irq become active? after leaving attach? */
102114902Sscottl        sc->irqrid = 0;
103114902Sscottl        if(!(sc->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irqrid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE))){
104114902Sscottl                device_printf(dev, "irq allocation failed\n");
105114902Sscottl                goto error;
106114902Sscottl        }
107114902Sscottl	if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO, sc->ips_adapter_intr, sc, &sc->irqcookie)){
108114902Sscottl                device_printf(dev, "irq setup failed\n");
109114902Sscottl                goto error;
110114902Sscottl        }
111114902Sscottl	if (bus_dma_tag_create(	/* parent    */	NULL,
112114902Sscottl				/* alignemnt */	1,
113114902Sscottl				/* boundary  */	0,
114114902Sscottl				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
115114902Sscottl				/* highaddr  */	BUS_SPACE_MAXADDR,
116114902Sscottl				/* filter    */	NULL,
117114902Sscottl				/* filterarg */	NULL,
118114902Sscottl				/* maxsize   */	BUS_SPACE_MAXSIZE_32BIT,
119114902Sscottl				/* numsegs   */	IPS_MAX_SG_ELEMENTS,
120114902Sscottl				/* maxsegsize*/	BUS_SPACE_MAXSIZE_32BIT,
121114902Sscottl				/* flags     */	0,
122114902Sscottl				&sc->adapter_dmatag) != 0) {
123114902Sscottl                printf("IPS can't alloc dma tag\n");
124114902Sscottl                goto error;
125114902Sscottl        }
126114902Sscottl	if(ips_adapter_init(sc))
127114902Sscottl		goto error;
128114902Sscottl        return 0;
129114902Sscottlerror:
130114902Sscottl	ips_pci_free(sc);
131114902Sscottl        return (ENXIO);
132114902Sscottl}
133114902Sscottl
134114902Sscottlstatic int ips_pci_free(ips_softc_t *sc)
135114902Sscottl{
136114902Sscottl	if(sc->adapter_dmatag)
137114902Sscottl		bus_dma_tag_destroy(sc->adapter_dmatag);
138114902Sscottl	if(sc->irqcookie)
139114902Sscottl                bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
140114902Sscottl        if(sc->irqres)
141114902Sscottl               bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
142114902Sscottl        if(sc->iores)
143114902Sscottl                bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
144114902Sscottl	return 0;
145114902Sscottl}
146114902Sscottl
147114902Sscottlstatic int ips_pci_detach(device_t dev)
148114902Sscottl{
149114902Sscottl        ips_softc_t *sc;
150114902Sscottl        DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
151114902Sscottl        sc = (ips_softc_t *) device_get_softc(dev);
152114902Sscottl	ips_flush_cache(sc);
153114902Sscottl	if(ips_adapter_free(sc))
154114902Sscottl		return EBUSY;
155114902Sscottl        ips_pci_free(sc);
156114902Sscottl	mtx_destroy(&sc->cmd_mtx);
157114902Sscottl	return 0;
158114902Sscottl}
159114902Sscottl
160114902Sscottlstatic int ips_pci_shutdown(device_t dev)
161114902Sscottl{
162114902Sscottl	ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev);
163114902Sscottl	ips_flush_cache(sc);
164114902Sscottl	return 0;
165114902Sscottl}
166114902Sscottl
167114902Sscottlstatic device_method_t ips_driver_methods[] = {
168114902Sscottl        DEVMETHOD(device_probe, ips_pci_probe),
169114902Sscottl        DEVMETHOD(device_attach, ips_pci_attach),
170114902Sscottl        DEVMETHOD(device_detach, ips_pci_detach),
171114902Sscottl	DEVMETHOD(device_shutdown, ips_pci_shutdown),
172114902Sscottl        {0,0}
173114902Sscottl};
174114902Sscottl
175114902Sscottlstatic driver_t ips_pci_driver = {
176114902Sscottl        "ips",
177114902Sscottl        ips_driver_methods,
178114902Sscottl        sizeof(ips_softc_t),
179114902Sscottl};
180114902Sscottl
181114902Sscottlstatic devclass_t ips_devclass;
182114902SscottlDRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, 0, 0);
183