ips_pci.c revision 232854
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 232854 2012-03-12 08:03:51Z scottl $");
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        u_int32_t command;
63114902Sscottl        ips_softc_t *sc;
64114902Sscottl
65116852Sscottl
66117167Sjhb	if (resource_disabled(device_get_name(dev), device_get_unit(dev))) {
67116852Sscottl		device_printf(dev, "device is disabled\n");
68116852Sscottl		/* but return 0 so the !$)$)*!$*) unit isn't reused */
69116852Sscottl		return (0);
70116852Sscottl	}
71114902Sscottl        DEVICE_PRINTF(1, dev, "in attach.\n");
72114902Sscottl        sc = (ips_softc_t *)device_get_softc(dev);
73114902Sscottl        if(!sc){
74114902Sscottl                printf("how is sc NULL?!\n");
75114902Sscottl                return (ENXIO);
76114902Sscottl        }
77114902Sscottl        bzero(sc, sizeof(ips_softc_t));
78114902Sscottl        sc->dev = dev;
79114902Sscottl
80114902Sscottl        if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){
81114902Sscottl		sc->ips_adapter_reinit = ips_morpheus_reinit;
82114902Sscottl                sc->ips_adapter_intr = ips_morpheus_intr;
83114902Sscottl		sc->ips_issue_cmd    = ips_issue_morpheus_cmd;
84141062Sscottl		sc->ips_poll_cmd     = ips_morpheus_poll;
85114902Sscottl        } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){
86114902Sscottl		sc->ips_adapter_reinit = ips_copperhead_reinit;
87114902Sscottl                sc->ips_adapter_intr = ips_copperhead_intr;
88114902Sscottl		sc->ips_issue_cmd    = ips_issue_copperhead_cmd;
89141062Sscottl		sc->ips_poll_cmd     = ips_copperhead_poll;
90127205Sscottl	} else if (pci_get_device(dev) == IPS_MARCO_DEVICE_ID){
91127205Sscottl		sc->ips_adapter_reinit = ips_morpheus_reinit;
92127205Sscottl		sc->ips_adapter_intr = ips_morpheus_intr;
93127205Sscottl		sc->ips_issue_cmd = ips_issue_morpheus_cmd;
94141062Sscottl		sc->ips_poll_cmd     = ips_morpheus_poll;
95114902Sscottl	} else
96114902Sscottl                goto error;
97114902Sscottl        /* make sure busmastering is on */
98114902Sscottl        command = pci_read_config(dev, PCIR_COMMAND, 1);
99114902Sscottl	command |= PCIM_CMD_BUSMASTEREN;
100114902Sscottl	pci_write_config(dev, PCIR_COMMAND, command, 1);
101114902Sscottl        /* seting up io space */
102114902Sscottl        sc->iores = NULL;
103114902Sscottl        if(command & PCIM_CMD_MEMEN){
104114902Sscottl                PRINTF(10, "trying MEMIO\n");
105127205Sscottl		if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)
106127205Sscottl                	sc->rid = PCIR_BAR(1);
107114902Sscottl		else
108127205Sscottl			sc->rid = PCIR_BAR(0);
109114902Sscottl                sc->iotype = SYS_RES_MEMORY;
110127135Snjl                sc->iores = bus_alloc_resource_any(dev, sc->iotype,
111127135Snjl			&sc->rid, RF_ACTIVE);
112114902Sscottl        }
113114902Sscottl        if(!sc->iores && command & PCIM_CMD_PORTEN){
114114902Sscottl                PRINTF(10, "trying PORTIO\n");
115119690Sjhb                sc->rid = PCIR_BAR(0);
116114902Sscottl                sc->iotype = SYS_RES_IOPORT;
117127135Snjl                sc->iores = bus_alloc_resource_any(dev, sc->iotype,
118127135Snjl			&sc->rid, RF_ACTIVE);
119114902Sscottl        }
120114902Sscottl        if(sc->iores == NULL){
121114902Sscottl                device_printf(dev, "resource allocation failed\n");
122114902Sscottl                return (ENXIO);
123114902Sscottl        }
124114902Sscottl        sc->bustag = rman_get_bustag(sc->iores);
125114902Sscottl        sc->bushandle = rman_get_bushandle(sc->iores);
126114902Sscottl        /*allocate an interrupt. when does the irq become active? after leaving attach? */
127114902Sscottl        sc->irqrid = 0;
128127135Snjl        if(!(sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ,
129127135Snjl		&sc->irqrid, RF_SHAREABLE | RF_ACTIVE))){
130114902Sscottl                device_printf(dev, "irq allocation failed\n");
131114902Sscottl                goto error;
132114902Sscottl        }
133166901Spiso	if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO|INTR_MPSAFE, NULL,
134166901Spiso	    sc->ips_adapter_intr, sc, &sc->irqcookie)){
135114902Sscottl                device_printf(dev, "irq setup failed\n");
136114902Sscottl                goto error;
137114902Sscottl        }
138232854Sscottl	if (bus_dma_tag_create(	/* PCI parent */bus_get_dma_tag(dev),
139114902Sscottl				/* alignemnt */	1,
140114902Sscottl				/* boundary  */	0,
141114902Sscottl				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
142114902Sscottl				/* highaddr  */	BUS_SPACE_MAXADDR,
143114902Sscottl				/* filter    */	NULL,
144114902Sscottl				/* filterarg */	NULL,
145114902Sscottl				/* maxsize   */	BUS_SPACE_MAXSIZE_32BIT,
146114902Sscottl				/* numsegs   */	IPS_MAX_SG_ELEMENTS,
147114902Sscottl				/* maxsegsize*/	BUS_SPACE_MAXSIZE_32BIT,
148114902Sscottl				/* flags     */	0,
149140923Sscottl				/* lockfunc  */ NULL,
150140923Sscottl				/* lockarg   */ NULL,
151114902Sscottl				&sc->adapter_dmatag) != 0) {
152114902Sscottl                printf("IPS can't alloc dma tag\n");
153114902Sscottl                goto error;
154114902Sscottl        }
155119997Sps	sc->ips_ich.ich_func = ips_intrhook;
156119997Sps	sc->ips_ich.ich_arg = sc;
157200620Smjacob	mtx_init(&sc->queue_mtx, "IPS bioqueue lock", NULL, MTX_DEF);
158140923Sscottl	sema_init(&sc->cmd_sema, 0, "IPS Command Semaphore");
159126364Sscottl	bioq_init(&sc->queue);
160119997Sps	if (config_intrhook_establish(&sc->ips_ich) != 0) {
161119997Sps		printf("IPS can't establish configuration hook\n");
162114902Sscottl		goto error;
163119997Sps	}
164114902Sscottl        return 0;
165114902Sscottlerror:
166114902Sscottl	ips_pci_free(sc);
167114902Sscottl        return (ENXIO);
168114902Sscottl}
169114902Sscottl
170119997Spsstatic void
171119997Spsips_intrhook(void *arg)
172119997Sps{
173119997Sps	struct ips_softc *sc = (struct ips_softc *)arg;
174119997Sps
175119997Sps	config_intrhook_disestablish(&sc->ips_ich);
176119997Sps	if (ips_adapter_init(sc))
177119997Sps		ips_pci_free(sc);
178119997Sps	else
179119997Sps		sc->configured = 1;
180119997Sps}
181119997Sps
182114902Sscottlstatic int ips_pci_free(ips_softc_t *sc)
183114902Sscottl{
184114902Sscottl	if(sc->adapter_dmatag)
185114902Sscottl		bus_dma_tag_destroy(sc->adapter_dmatag);
186114902Sscottl	if(sc->irqcookie)
187114902Sscottl                bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
188114902Sscottl        if(sc->irqres)
189114902Sscottl               bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
190114902Sscottl        if(sc->iores)
191114902Sscottl                bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
192116852Sscottl	sc->configured = 0;
193140923Sscottl	mtx_destroy(&sc->queue_mtx);
194140923Sscottl	sema_destroy(&sc->cmd_sema);
195114902Sscottl	return 0;
196114902Sscottl}
197114902Sscottl
198114902Sscottlstatic int ips_pci_detach(device_t dev)
199114902Sscottl{
200114902Sscottl        ips_softc_t *sc;
201114902Sscottl        DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
202114902Sscottl        sc = (ips_softc_t *) device_get_softc(dev);
203116852Sscottl	if (sc->configured) {
204116852Sscottl		sc->configured = 0;
205116852Sscottl		ips_flush_cache(sc);
206116852Sscottl		if(ips_adapter_free(sc))
207116852Sscottl			return EBUSY;
208116852Sscottl		ips_pci_free(sc);
209126364Sscottl		bioq_flush(&sc->queue, NULL, ENXIO);
210116852Sscottl	}
211114902Sscottl	return 0;
212114902Sscottl}
213114902Sscottl
214114902Sscottlstatic int ips_pci_shutdown(device_t dev)
215114902Sscottl{
216114902Sscottl	ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev);
217116852Sscottl	if (sc->configured) {
218116852Sscottl		ips_flush_cache(sc);
219116852Sscottl	}
220114902Sscottl	return 0;
221114902Sscottl}
222114902Sscottl
223114902Sscottlstatic device_method_t ips_driver_methods[] = {
224114902Sscottl        DEVMETHOD(device_probe, ips_pci_probe),
225114902Sscottl        DEVMETHOD(device_attach, ips_pci_attach),
226114902Sscottl        DEVMETHOD(device_detach, ips_pci_detach),
227114902Sscottl	DEVMETHOD(device_shutdown, ips_pci_shutdown),
228114902Sscottl        {0,0}
229114902Sscottl};
230114902Sscottl
231114902Sscottlstatic driver_t ips_pci_driver = {
232114902Sscottl        "ips",
233114902Sscottl        ips_driver_methods,
234114902Sscottl        sizeof(ips_softc_t),
235114902Sscottl};
236114902Sscottl
237114902Sscottlstatic devclass_t ips_devclass;
238114902SscottlDRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, 0, 0);
239