Deleted Added
full compact
ips_pci.c (232854) ips_pci.c (254263)
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>
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 232854 2012-03-12 08:03:51Z scottl $");
30__FBSDID("$FreeBSD: head/sys/dev/ips/ips_pci.c 254263 2013-08-12 23:30:01Z scottl $");
31
32#include <dev/ips/ipsreg.h>
33#include <dev/ips/ips.h>
34
35#include <dev/pci/pcireg.h>
36#include <dev/pci/pcivar.h>
37
38static int ips_pci_free(ips_softc_t *sc);
39static void ips_intrhook(void *arg);
40
41static int ips_pci_probe(device_t dev)
42{
43
44 if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
45 (pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)) {
46 device_set_desc(dev, "IBM ServeRAID Adapter");
47 return (BUS_PROBE_DEFAULT);
48 } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
49 (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)) {
50 device_set_desc(dev, "IBM ServeRAID Adapter");
51 return (BUS_PROBE_DEFAULT);
52 } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID_ADAPTEC) &&
53 (pci_get_device(dev) == IPS_MARCO_DEVICE_ID)) {
54 device_set_desc(dev, "Adaptec ServeRAID Adapter");
55 return (BUS_PROBE_DEFAULT);
56 }
57 return(ENXIO);
58}
59
60static int ips_pci_attach(device_t dev)
61{
31
32#include <dev/ips/ipsreg.h>
33#include <dev/ips/ips.h>
34
35#include <dev/pci/pcireg.h>
36#include <dev/pci/pcivar.h>
37
38static int ips_pci_free(ips_softc_t *sc);
39static void ips_intrhook(void *arg);
40
41static int ips_pci_probe(device_t dev)
42{
43
44 if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
45 (pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)) {
46 device_set_desc(dev, "IBM ServeRAID Adapter");
47 return (BUS_PROBE_DEFAULT);
48 } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
49 (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)) {
50 device_set_desc(dev, "IBM ServeRAID Adapter");
51 return (BUS_PROBE_DEFAULT);
52 } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID_ADAPTEC) &&
53 (pci_get_device(dev) == IPS_MARCO_DEVICE_ID)) {
54 device_set_desc(dev, "Adaptec ServeRAID Adapter");
55 return (BUS_PROBE_DEFAULT);
56 }
57 return(ENXIO);
58}
59
60static int ips_pci_attach(device_t dev)
61{
62 u_int32_t command;
63 ips_softc_t *sc;
64
65
66 if (resource_disabled(device_get_name(dev), device_get_unit(dev))) {
67 device_printf(dev, "device is disabled\n");
68 /* but return 0 so the !$)$)*!$*) unit isn't reused */
69 return (0);
70 }
71 DEVICE_PRINTF(1, dev, "in attach.\n");
72 sc = (ips_softc_t *)device_get_softc(dev);
73 if(!sc){
74 printf("how is sc NULL?!\n");
75 return (ENXIO);
76 }
77 bzero(sc, sizeof(ips_softc_t));
78 sc->dev = dev;
79
80 if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){
81 sc->ips_adapter_reinit = ips_morpheus_reinit;
82 sc->ips_adapter_intr = ips_morpheus_intr;
83 sc->ips_issue_cmd = ips_issue_morpheus_cmd;
84 sc->ips_poll_cmd = ips_morpheus_poll;
85 } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){
86 sc->ips_adapter_reinit = ips_copperhead_reinit;
87 sc->ips_adapter_intr = ips_copperhead_intr;
88 sc->ips_issue_cmd = ips_issue_copperhead_cmd;
89 sc->ips_poll_cmd = ips_copperhead_poll;
90 } else if (pci_get_device(dev) == IPS_MARCO_DEVICE_ID){
91 sc->ips_adapter_reinit = ips_morpheus_reinit;
92 sc->ips_adapter_intr = ips_morpheus_intr;
93 sc->ips_issue_cmd = ips_issue_morpheus_cmd;
94 sc->ips_poll_cmd = ips_morpheus_poll;
95 } else
96 goto error;
97 /* make sure busmastering is on */
62 ips_softc_t *sc;
63
64
65 if (resource_disabled(device_get_name(dev), device_get_unit(dev))) {
66 device_printf(dev, "device is disabled\n");
67 /* but return 0 so the !$)$)*!$*) unit isn't reused */
68 return (0);
69 }
70 DEVICE_PRINTF(1, dev, "in attach.\n");
71 sc = (ips_softc_t *)device_get_softc(dev);
72 if(!sc){
73 printf("how is sc NULL?!\n");
74 return (ENXIO);
75 }
76 bzero(sc, sizeof(ips_softc_t));
77 sc->dev = dev;
78
79 if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){
80 sc->ips_adapter_reinit = ips_morpheus_reinit;
81 sc->ips_adapter_intr = ips_morpheus_intr;
82 sc->ips_issue_cmd = ips_issue_morpheus_cmd;
83 sc->ips_poll_cmd = ips_morpheus_poll;
84 } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){
85 sc->ips_adapter_reinit = ips_copperhead_reinit;
86 sc->ips_adapter_intr = ips_copperhead_intr;
87 sc->ips_issue_cmd = ips_issue_copperhead_cmd;
88 sc->ips_poll_cmd = ips_copperhead_poll;
89 } else if (pci_get_device(dev) == IPS_MARCO_DEVICE_ID){
90 sc->ips_adapter_reinit = ips_morpheus_reinit;
91 sc->ips_adapter_intr = ips_morpheus_intr;
92 sc->ips_issue_cmd = ips_issue_morpheus_cmd;
93 sc->ips_poll_cmd = ips_morpheus_poll;
94 } else
95 goto error;
96 /* make sure busmastering is on */
98 command = pci_read_config(dev, PCIR_COMMAND, 1);
99 command |= PCIM_CMD_BUSMASTEREN;
100 pci_write_config(dev, PCIR_COMMAND, command, 1);
97 pci_enable_busmaster(dev);
101 /* seting up io space */
102 sc->iores = NULL;
98 /* seting up io space */
99 sc->iores = NULL;
103 if(command & PCIM_CMD_MEMEN){
104 PRINTF(10, "trying MEMIO\n");
105 if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)
106 sc->rid = PCIR_BAR(1);
107 else
108 sc->rid = PCIR_BAR(0);
109 sc->iotype = SYS_RES_MEMORY;
110 sc->iores = bus_alloc_resource_any(dev, sc->iotype,
111 &sc->rid, RF_ACTIVE);
112 }
113 if(!sc->iores && command & PCIM_CMD_PORTEN){
100 PRINTF(10, "trying MEMIO\n");
101 if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)
102 sc->rid = PCIR_BAR(1);
103 else
104 sc->rid = PCIR_BAR(0);
105 sc->iotype = SYS_RES_MEMORY;
106 sc->iores = bus_alloc_resource_any(dev, sc->iotype, &sc->rid,
107 RF_ACTIVE);
108 if(!sc->iores){
114 PRINTF(10, "trying PORTIO\n");
115 sc->rid = PCIR_BAR(0);
116 sc->iotype = SYS_RES_IOPORT;
117 sc->iores = bus_alloc_resource_any(dev, sc->iotype,
118 &sc->rid, RF_ACTIVE);
119 }
120 if(sc->iores == NULL){
121 device_printf(dev, "resource allocation failed\n");
122 return (ENXIO);
123 }
124 sc->bustag = rman_get_bustag(sc->iores);
125 sc->bushandle = rman_get_bushandle(sc->iores);
126 /*allocate an interrupt. when does the irq become active? after leaving attach? */
127 sc->irqrid = 0;
128 if(!(sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ,
129 &sc->irqrid, RF_SHAREABLE | RF_ACTIVE))){
130 device_printf(dev, "irq allocation failed\n");
131 goto error;
132 }
133 if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO|INTR_MPSAFE, NULL,
134 sc->ips_adapter_intr, sc, &sc->irqcookie)){
135 device_printf(dev, "irq setup failed\n");
136 goto error;
137 }
138 if (bus_dma_tag_create( /* PCI parent */bus_get_dma_tag(dev),
139 /* alignemnt */ 1,
140 /* boundary */ 0,
141 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
142 /* highaddr */ BUS_SPACE_MAXADDR,
143 /* filter */ NULL,
144 /* filterarg */ NULL,
145 /* maxsize */ BUS_SPACE_MAXSIZE_32BIT,
146 /* numsegs */ IPS_MAX_SG_ELEMENTS,
147 /* maxsegsize*/ BUS_SPACE_MAXSIZE_32BIT,
148 /* flags */ 0,
149 /* lockfunc */ NULL,
150 /* lockarg */ NULL,
151 &sc->adapter_dmatag) != 0) {
152 printf("IPS can't alloc dma tag\n");
153 goto error;
154 }
155 sc->ips_ich.ich_func = ips_intrhook;
156 sc->ips_ich.ich_arg = sc;
157 mtx_init(&sc->queue_mtx, "IPS bioqueue lock", NULL, MTX_DEF);
158 sema_init(&sc->cmd_sema, 0, "IPS Command Semaphore");
159 bioq_init(&sc->queue);
160 if (config_intrhook_establish(&sc->ips_ich) != 0) {
161 printf("IPS can't establish configuration hook\n");
162 goto error;
163 }
164 return 0;
165error:
166 ips_pci_free(sc);
167 return (ENXIO);
168}
169
170static void
171ips_intrhook(void *arg)
172{
173 struct ips_softc *sc = (struct ips_softc *)arg;
174
175 config_intrhook_disestablish(&sc->ips_ich);
176 if (ips_adapter_init(sc))
177 ips_pci_free(sc);
178 else
179 sc->configured = 1;
180}
181
182static int ips_pci_free(ips_softc_t *sc)
183{
184 if(sc->adapter_dmatag)
185 bus_dma_tag_destroy(sc->adapter_dmatag);
186 if(sc->irqcookie)
187 bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
188 if(sc->irqres)
189 bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
190 if(sc->iores)
191 bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
192 sc->configured = 0;
193 mtx_destroy(&sc->queue_mtx);
194 sema_destroy(&sc->cmd_sema);
195 return 0;
196}
197
198static int ips_pci_detach(device_t dev)
199{
200 ips_softc_t *sc;
201 DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
202 sc = (ips_softc_t *) device_get_softc(dev);
203 if (sc->configured) {
204 sc->configured = 0;
205 ips_flush_cache(sc);
206 if(ips_adapter_free(sc))
207 return EBUSY;
208 ips_pci_free(sc);
209 bioq_flush(&sc->queue, NULL, ENXIO);
210 }
211 return 0;
212}
213
214static int ips_pci_shutdown(device_t dev)
215{
216 ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev);
217 if (sc->configured) {
218 ips_flush_cache(sc);
219 }
220 return 0;
221}
222
223static device_method_t ips_driver_methods[] = {
224 DEVMETHOD(device_probe, ips_pci_probe),
225 DEVMETHOD(device_attach, ips_pci_attach),
226 DEVMETHOD(device_detach, ips_pci_detach),
227 DEVMETHOD(device_shutdown, ips_pci_shutdown),
228 {0,0}
229};
230
231static driver_t ips_pci_driver = {
232 "ips",
233 ips_driver_methods,
234 sizeof(ips_softc_t),
235};
236
237static devclass_t ips_devclass;
238DRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, 0, 0);
109 PRINTF(10, "trying PORTIO\n");
110 sc->rid = PCIR_BAR(0);
111 sc->iotype = SYS_RES_IOPORT;
112 sc->iores = bus_alloc_resource_any(dev, sc->iotype,
113 &sc->rid, RF_ACTIVE);
114 }
115 if(sc->iores == NULL){
116 device_printf(dev, "resource allocation failed\n");
117 return (ENXIO);
118 }
119 sc->bustag = rman_get_bustag(sc->iores);
120 sc->bushandle = rman_get_bushandle(sc->iores);
121 /*allocate an interrupt. when does the irq become active? after leaving attach? */
122 sc->irqrid = 0;
123 if(!(sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ,
124 &sc->irqrid, RF_SHAREABLE | RF_ACTIVE))){
125 device_printf(dev, "irq allocation failed\n");
126 goto error;
127 }
128 if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO|INTR_MPSAFE, NULL,
129 sc->ips_adapter_intr, sc, &sc->irqcookie)){
130 device_printf(dev, "irq setup failed\n");
131 goto error;
132 }
133 if (bus_dma_tag_create( /* PCI parent */bus_get_dma_tag(dev),
134 /* alignemnt */ 1,
135 /* boundary */ 0,
136 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
137 /* highaddr */ BUS_SPACE_MAXADDR,
138 /* filter */ NULL,
139 /* filterarg */ NULL,
140 /* maxsize */ BUS_SPACE_MAXSIZE_32BIT,
141 /* numsegs */ IPS_MAX_SG_ELEMENTS,
142 /* maxsegsize*/ BUS_SPACE_MAXSIZE_32BIT,
143 /* flags */ 0,
144 /* lockfunc */ NULL,
145 /* lockarg */ NULL,
146 &sc->adapter_dmatag) != 0) {
147 printf("IPS can't alloc dma tag\n");
148 goto error;
149 }
150 sc->ips_ich.ich_func = ips_intrhook;
151 sc->ips_ich.ich_arg = sc;
152 mtx_init(&sc->queue_mtx, "IPS bioqueue lock", NULL, MTX_DEF);
153 sema_init(&sc->cmd_sema, 0, "IPS Command Semaphore");
154 bioq_init(&sc->queue);
155 if (config_intrhook_establish(&sc->ips_ich) != 0) {
156 printf("IPS can't establish configuration hook\n");
157 goto error;
158 }
159 return 0;
160error:
161 ips_pci_free(sc);
162 return (ENXIO);
163}
164
165static void
166ips_intrhook(void *arg)
167{
168 struct ips_softc *sc = (struct ips_softc *)arg;
169
170 config_intrhook_disestablish(&sc->ips_ich);
171 if (ips_adapter_init(sc))
172 ips_pci_free(sc);
173 else
174 sc->configured = 1;
175}
176
177static int ips_pci_free(ips_softc_t *sc)
178{
179 if(sc->adapter_dmatag)
180 bus_dma_tag_destroy(sc->adapter_dmatag);
181 if(sc->irqcookie)
182 bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
183 if(sc->irqres)
184 bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
185 if(sc->iores)
186 bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
187 sc->configured = 0;
188 mtx_destroy(&sc->queue_mtx);
189 sema_destroy(&sc->cmd_sema);
190 return 0;
191}
192
193static int ips_pci_detach(device_t dev)
194{
195 ips_softc_t *sc;
196 DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
197 sc = (ips_softc_t *) device_get_softc(dev);
198 if (sc->configured) {
199 sc->configured = 0;
200 ips_flush_cache(sc);
201 if(ips_adapter_free(sc))
202 return EBUSY;
203 ips_pci_free(sc);
204 bioq_flush(&sc->queue, NULL, ENXIO);
205 }
206 return 0;
207}
208
209static int ips_pci_shutdown(device_t dev)
210{
211 ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev);
212 if (sc->configured) {
213 ips_flush_cache(sc);
214 }
215 return 0;
216}
217
218static device_method_t ips_driver_methods[] = {
219 DEVMETHOD(device_probe, ips_pci_probe),
220 DEVMETHOD(device_attach, ips_pci_attach),
221 DEVMETHOD(device_detach, ips_pci_detach),
222 DEVMETHOD(device_shutdown, ips_pci_shutdown),
223 {0,0}
224};
225
226static driver_t ips_pci_driver = {
227 "ips",
228 ips_driver_methods,
229 sizeof(ips_softc_t),
230};
231
232static devclass_t ips_devclass;
233DRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, 0, 0);