ips_pci.c revision 127135
1/*-
2 * Copyright (c) 2002 Adaptec Inc.
3 * All rights reserved.
4 *
5 * Written by: David Jeffery
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/dev/ips/ips_pci.c 127135 2004-03-17 17:50:55Z njl $");
31
32#include <dev/ips/ips.h>
33
34static int ips_pci_free(ips_softc_t *sc);
35static void ips_intrhook(void *arg);
36
37static int ips_pci_probe(device_t dev)
38{
39
40        if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
41	    (pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)) {
42		device_set_desc(dev, "IBM ServeRAID Adapter");
43                return 0;
44        } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
45	    (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)) {
46		device_set_desc(dev, "IBM ServeRAID Adapter");
47		return (0);
48        }
49        return(ENXIO);
50}
51
52static int ips_pci_attach(device_t dev)
53{
54        u_int32_t command;
55        ips_softc_t *sc;
56
57
58	if (resource_disabled(device_get_name(dev), device_get_unit(dev))) {
59		device_printf(dev, "device is disabled\n");
60		/* but return 0 so the !$)$)*!$*) unit isn't reused */
61		return (0);
62	}
63        DEVICE_PRINTF(1, dev, "in attach.\n");
64        sc = (ips_softc_t *)device_get_softc(dev);
65        if(!sc){
66                printf("how is sc NULL?!\n");
67                return (ENXIO);
68        }
69        bzero(sc, sizeof(ips_softc_t));
70        sc->dev = dev;
71
72        if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){
73		sc->ips_adapter_reinit = ips_morpheus_reinit;
74                sc->ips_adapter_intr = ips_morpheus_intr;
75		sc->ips_issue_cmd    = ips_issue_morpheus_cmd;
76        } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){
77		sc->ips_adapter_reinit = ips_copperhead_reinit;
78                sc->ips_adapter_intr = ips_copperhead_intr;
79		sc->ips_issue_cmd    = ips_issue_copperhead_cmd;
80	} else
81                goto error;
82        /* make sure busmastering is on */
83        command = pci_read_config(dev, PCIR_COMMAND, 1);
84	command |= PCIM_CMD_BUSMASTEREN;
85	pci_write_config(dev, PCIR_COMMAND, command, 1);
86        /* seting up io space */
87        sc->iores = NULL;
88        if(command & PCIM_CMD_MEMEN){
89                PRINTF(10, "trying MEMIO\n");
90		if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)
91                	sc->rid = PCIR_BAR(0);
92		else
93			sc->rid = PCIR_BAR(1);
94                sc->iotype = SYS_RES_MEMORY;
95                sc->iores = bus_alloc_resource_any(dev, sc->iotype,
96			&sc->rid, RF_ACTIVE);
97        }
98        if(!sc->iores && command & PCIM_CMD_PORTEN){
99                PRINTF(10, "trying PORTIO\n");
100                sc->rid = PCIR_BAR(0);
101                sc->iotype = SYS_RES_IOPORT;
102                sc->iores = bus_alloc_resource_any(dev, sc->iotype,
103			&sc->rid, RF_ACTIVE);
104        }
105        if(sc->iores == NULL){
106                device_printf(dev, "resource allocation failed\n");
107                return (ENXIO);
108        }
109        sc->bustag = rman_get_bustag(sc->iores);
110        sc->bushandle = rman_get_bushandle(sc->iores);
111        /*allocate an interrupt. when does the irq become active? after leaving attach? */
112        sc->irqrid = 0;
113        if(!(sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ,
114		&sc->irqrid, RF_SHAREABLE | RF_ACTIVE))){
115                device_printf(dev, "irq allocation failed\n");
116                goto error;
117        }
118	if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO, sc->ips_adapter_intr, sc, &sc->irqcookie)){
119                device_printf(dev, "irq setup failed\n");
120                goto error;
121        }
122	if (bus_dma_tag_create(	/* parent    */	NULL,
123				/* alignemnt */	1,
124				/* boundary  */	0,
125				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
126				/* highaddr  */	BUS_SPACE_MAXADDR,
127				/* filter    */	NULL,
128				/* filterarg */	NULL,
129				/* maxsize   */	BUS_SPACE_MAXSIZE_32BIT,
130				/* numsegs   */	IPS_MAX_SG_ELEMENTS,
131				/* maxsegsize*/	BUS_SPACE_MAXSIZE_32BIT,
132				/* flags     */	0,
133				/* lockfunc  */ busdma_lock_mutex,
134				/* lockarg   */ &Giant,
135				&sc->adapter_dmatag) != 0) {
136                printf("IPS can't alloc dma tag\n");
137                goto error;
138        }
139	sc->ips_ich.ich_func = ips_intrhook;
140	sc->ips_ich.ich_arg = sc;
141	mtx_init(&sc->queue_mtx, "IPS bioqueue lock", MTX_DEF, 0);
142	bioq_init(&sc->queue);
143	if (config_intrhook_establish(&sc->ips_ich) != 0) {
144		printf("IPS can't establish configuration hook\n");
145		goto error;
146	}
147        return 0;
148error:
149	ips_pci_free(sc);
150        return (ENXIO);
151}
152
153static void
154ips_intrhook(void *arg)
155{
156	struct ips_softc *sc = (struct ips_softc *)arg;
157
158	config_intrhook_disestablish(&sc->ips_ich);
159	if (ips_adapter_init(sc))
160		ips_pci_free(sc);
161	else
162		sc->configured = 1;
163}
164
165static int ips_pci_free(ips_softc_t *sc)
166{
167	if(sc->adapter_dmatag)
168		bus_dma_tag_destroy(sc->adapter_dmatag);
169	if(sc->irqcookie)
170                bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
171        if(sc->irqres)
172               bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
173        if(sc->iores)
174                bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
175	sc->configured = 0;
176	return 0;
177}
178
179static int ips_pci_detach(device_t dev)
180{
181        ips_softc_t *sc;
182        DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
183        sc = (ips_softc_t *) device_get_softc(dev);
184	if (sc->configured) {
185		sc->configured = 0;
186		ips_flush_cache(sc);
187		if(ips_adapter_free(sc))
188			return EBUSY;
189		ips_pci_free(sc);
190		bioq_flush(&sc->queue, NULL, ENXIO);
191	}
192	return 0;
193}
194
195static int ips_pci_shutdown(device_t dev)
196{
197	ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev);
198	if (sc->configured) {
199		ips_flush_cache(sc);
200	}
201	return 0;
202}
203
204static device_method_t ips_driver_methods[] = {
205        DEVMETHOD(device_probe, ips_pci_probe),
206        DEVMETHOD(device_attach, ips_pci_attach),
207        DEVMETHOD(device_detach, ips_pci_detach),
208	DEVMETHOD(device_shutdown, ips_pci_shutdown),
209        {0,0}
210};
211
212static driver_t ips_pci_driver = {
213        "ips",
214        ips_driver_methods,
215        sizeof(ips_softc_t),
216};
217
218static devclass_t ips_devclass;
219DRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, 0, 0);
220