xref: /freebsd/sys/dev/ips/ips_pci.c (revision d086ded32300bc0f33fb1574d0bcfccfbc60881d)
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  * $FreeBSD$
29  */
30 
31 
32 #include <dev/ips/ips.h>
33 static int ips_pci_free(ips_softc_t *sc);
34 
35 static int ips_pci_probe(device_t dev)
36 {
37 
38         if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
39 	    (pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)) {
40 		device_set_desc(dev, "IBM ServeRAID Adapter");
41                 return 0;
42         } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
43 	    (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)) {
44 		device_set_desc(dev, "IBM ServeRAID Adapter");
45 		return (0);
46         }
47         return(ENXIO);
48 }
49 
50 static int ips_pci_attach(device_t dev)
51 {
52         u_int32_t command;
53         ips_softc_t *sc;
54 
55         DEVICE_PRINTF(1, dev, "in attach.\n");
56         sc = (ips_softc_t *)device_get_softc(dev);
57         if(!sc){
58                 printf("how is sc NULL?!\n");
59                 return (ENXIO);
60         }
61         bzero(sc, sizeof(ips_softc_t));
62         sc->dev = dev;
63 
64         if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){
65 		sc->ips_adapter_reinit = ips_morpheus_reinit;
66                 sc->ips_adapter_intr = ips_morpheus_intr;
67 		sc->ips_issue_cmd    = ips_issue_morpheus_cmd;
68         } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){
69 		sc->ips_adapter_reinit = ips_copperhead_reinit;
70                 sc->ips_adapter_intr = ips_copperhead_intr;
71 		sc->ips_issue_cmd    = ips_issue_copperhead_cmd;
72 	} else
73                 goto error;
74         /* make sure busmastering is on */
75         command = pci_read_config(dev, PCIR_COMMAND, 1);
76 	command |= PCIM_CMD_BUSMASTEREN;
77 	pci_write_config(dev, PCIR_COMMAND, command, 1);
78         /* seting up io space */
79         sc->iores = NULL;
80         if(command & PCIM_CMD_MEMEN){
81                 PRINTF(10, "trying MEMIO\n");
82 		if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)
83                 	sc->rid = PCIR_MAPS;
84 		else
85 			sc->rid = PCIR_MAPS + 4;
86                 sc->iotype = SYS_RES_MEMORY;
87                 sc->iores = bus_alloc_resource(dev, sc->iotype, &sc->rid, 0, ~0, 1, RF_ACTIVE);
88         }
89         if(!sc->iores && command & PCIM_CMD_PORTEN){
90                 PRINTF(10, "trying PORTIO\n");
91                 sc->rid = PCIR_MAPS;
92                 sc->iotype = SYS_RES_IOPORT;
93                 sc->iores = bus_alloc_resource(dev, sc->iotype, &sc->rid, 0, ~0, 1, RF_ACTIVE);
94         }
95         if(sc->iores == NULL){
96                 device_printf(dev, "resource allocation failed\n");
97                 return (ENXIO);
98         }
99         sc->bustag = rman_get_bustag(sc->iores);
100         sc->bushandle = rman_get_bushandle(sc->iores);
101         /*allocate an interrupt. when does the irq become active? after leaving attach? */
102         sc->irqrid = 0;
103         if(!(sc->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irqrid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE))){
104                 device_printf(dev, "irq allocation failed\n");
105                 goto error;
106         }
107 	if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO, sc->ips_adapter_intr, sc, &sc->irqcookie)){
108                 device_printf(dev, "irq setup failed\n");
109                 goto error;
110         }
111 	if (bus_dma_tag_create(	/* parent    */	NULL,
112 				/* alignemnt */	1,
113 				/* boundary  */	0,
114 				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
115 				/* highaddr  */	BUS_SPACE_MAXADDR,
116 				/* filter    */	NULL,
117 				/* filterarg */	NULL,
118 				/* maxsize   */	BUS_SPACE_MAXSIZE_32BIT,
119 				/* numsegs   */	IPS_MAX_SG_ELEMENTS,
120 				/* maxsegsize*/	BUS_SPACE_MAXSIZE_32BIT,
121 				/* flags     */	0,
122 				&sc->adapter_dmatag) != 0) {
123                 printf("IPS can't alloc dma tag\n");
124                 goto error;
125         }
126 	if(ips_adapter_init(sc))
127 		goto error;
128         return 0;
129 error:
130 	ips_pci_free(sc);
131         return (ENXIO);
132 }
133 
134 static int ips_pci_free(ips_softc_t *sc)
135 {
136 	if(sc->adapter_dmatag)
137 		bus_dma_tag_destroy(sc->adapter_dmatag);
138 	if(sc->irqcookie)
139                 bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
140         if(sc->irqres)
141                bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
142         if(sc->iores)
143                 bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
144 	return 0;
145 }
146 
147 static int ips_pci_detach(device_t dev)
148 {
149         ips_softc_t *sc;
150         DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
151         sc = (ips_softc_t *) device_get_softc(dev);
152 	ips_flush_cache(sc);
153 	if(ips_adapter_free(sc))
154 		return EBUSY;
155         ips_pci_free(sc);
156 	mtx_destroy(&sc->cmd_mtx);
157 	return 0;
158 }
159 
160 static int ips_pci_shutdown(device_t dev)
161 {
162 	ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev);
163 	ips_flush_cache(sc);
164 	return 0;
165 }
166 
167 static device_method_t ips_driver_methods[] = {
168         DEVMETHOD(device_probe, ips_pci_probe),
169         DEVMETHOD(device_attach, ips_pci_attach),
170         DEVMETHOD(device_detach, ips_pci_detach),
171 	DEVMETHOD(device_shutdown, ips_pci_shutdown),
172         {0,0}
173 };
174 
175 static driver_t ips_pci_driver = {
176         "ips",
177         ips_driver_methods,
178         sizeof(ips_softc_t),
179 };
180 
181 static devclass_t ips_devclass;
182 DRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, 0, 0);
183