xref: /freebsd/sys/dev/ida/ida_pci.c (revision 069ac184)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999,2000 Jonathan Lemon
5  * All rights reserved.
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/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 
34 #include <sys/bio.h>
35 #include <sys/bus.h>
36 #include <sys/conf.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 #include <sys/rman.h>
43 
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pcivar.h>
46 
47 #include <geom/geom_disk.h>
48 
49 #include <dev/ida/idavar.h>
50 #include <dev/ida/idareg.h>
51 
52 #define	IDA_PCI_MAX_DMA_ADDR	0xFFFFFFFF
53 #define	IDA_PCI_MAX_DMA_COUNT	0xFFFFFFFF
54 
55 #define	IDA_PCI_MEMADDR		PCIR_BAR(1)		/* Mem I/O Address */
56 
57 #define	IDA_DEVICEID_SMART		0xAE100E11
58 #define	IDA_DEVICEID_DEC_SMART		0x00461011
59 #define	IDA_DEVICEID_NCR_53C1510	0x00101000
60 
61 static int
62 ida_v3_fifo_full(struct ida_softc *ida)
63 {
64 	return (ida_inl(ida, R_CMD_FIFO) == 0);
65 }
66 
67 static void
68 ida_v3_submit(struct ida_softc *ida, struct ida_qcb *qcb)
69 {
70 	ida_outl(ida, R_CMD_FIFO, qcb->hwqcb_busaddr);
71 }
72 
73 static bus_addr_t
74 ida_v3_done(struct ida_softc *ida)
75 {
76 	bus_addr_t completed;
77 
78 	completed = ida_inl(ida, R_DONE_FIFO);
79 	if (completed == -1) {
80 		return (0);			/* fifo is empty */
81 	}
82 	return (completed);
83 }
84 
85 static int
86 ida_v3_int_pending(struct ida_softc *ida)
87 {
88 	return (ida_inl(ida, R_INT_PENDING));
89 }
90 
91 static void
92 ida_v3_int_enable(struct ida_softc *ida, int enable)
93 {
94 	if (enable)
95 		ida->flags |= IDA_INTERRUPTS;
96 	else
97 		ida->flags &= ~IDA_INTERRUPTS;
98 	ida_outl(ida, R_INT_MASK, enable ? INT_ENABLE : INT_DISABLE);
99 }
100 
101 static int
102 ida_v4_fifo_full(struct ida_softc *ida)
103 {
104 	return (ida_inl(ida, R_42XX_REQUEST) != 0);
105 }
106 
107 static void
108 ida_v4_submit(struct ida_softc *ida, struct ida_qcb *qcb)
109 {
110 	ida_outl(ida, R_42XX_REQUEST, qcb->hwqcb_busaddr);
111 }
112 
113 static bus_addr_t
114 ida_v4_done(struct ida_softc *ida)
115 {
116 	bus_addr_t completed;
117 
118 	completed = ida_inl(ida, R_42XX_REPLY);
119 	if (completed == -1)
120 		return (0);			/* fifo is empty */
121 	ida_outl(ida, R_42XX_REPLY, 0);		/* confirm read */
122 	return (completed);
123 }
124 
125 static int
126 ida_v4_int_pending(struct ida_softc *ida)
127 {
128 	return (ida_inl(ida, R_42XX_STATUS) & STATUS_42XX_INT_PENDING);
129 }
130 
131 static void
132 ida_v4_int_enable(struct ida_softc *ida, int enable)
133 {
134 	if (enable)
135 		ida->flags |= IDA_INTERRUPTS;
136 	else
137 		ida->flags &= ~IDA_INTERRUPTS;
138 	ida_outl(ida, R_42XX_INT_MASK,
139 	    enable ? INT_ENABLE_42XX : INT_DISABLE_42XX);
140 }
141 
142 static struct ida_access ida_v3_access = {
143 	ida_v3_fifo_full,
144 	ida_v3_submit,
145 	ida_v3_done,
146 	ida_v3_int_pending,
147 	ida_v3_int_enable,
148 };
149 
150 static struct ida_access ida_v4_access = {
151 	ida_v4_fifo_full,
152 	ida_v4_submit,
153 	ida_v4_done,
154 	ida_v4_int_pending,
155 	ida_v4_int_enable,
156 };
157 
158 static struct ida_board board_id[] = {
159 	{ 0x40300E11, "Compaq SMART-2/P array controller",
160 	    &ida_v3_access, 0 },
161 	{ 0x40310E11, "Compaq SMART-2SL array controller",
162 	    &ida_v3_access, 0 },
163 	{ 0x40320E11, "Compaq Smart Array 3200 controller",
164 	    &ida_v3_access, 0 },
165 	{ 0x40330E11, "Compaq Smart Array 3100ES controller",
166 	    &ida_v3_access, 0 },
167 	{ 0x40340E11, "Compaq Smart Array 221 controller",
168 	    &ida_v3_access, 0 },
169 
170 	{ 0x40400E11, "Compaq Integrated Array controller",
171 	    &ida_v4_access, IDA_FIRMWARE },
172 	{ 0x40480E11, "Compaq RAID LC2 controller",
173 	    &ida_v4_access, IDA_FIRMWARE },
174 	{ 0x40500E11, "Compaq Smart Array 4200 controller",
175 	    &ida_v4_access, 0 },
176 	{ 0x40510E11, "Compaq Smart Array 4250ES controller",
177 	    &ida_v4_access, 0 },
178 	{ 0x40580E11, "Compaq Smart Array 431 controller",
179 	    &ida_v4_access, 0 },
180 
181 	{ 0, "", 0, 0 },
182 };
183 
184 static int ida_pci_probe(device_t dev);
185 static int ida_pci_attach(device_t dev);
186 
187 static device_method_t ida_pci_methods[] = {
188 	DEVMETHOD(device_probe,		ida_pci_probe),
189 	DEVMETHOD(device_attach,	ida_pci_attach),
190 	DEVMETHOD(device_detach,	ida_detach),
191 
192 	DEVMETHOD_END
193 };
194 
195 static driver_t ida_pci_driver = {
196 	"ida",
197 	ida_pci_methods,
198 	sizeof(struct ida_softc)
199 };
200 
201 static struct ida_board *
202 ida_pci_match(device_t dev)
203 {
204 	int i;
205 	u_int32_t id, sub_id;
206 
207 	id = pci_get_devid(dev);
208 	sub_id = pci_get_subdevice(dev) << 16 | pci_get_subvendor(dev);
209 
210 	if (id == IDA_DEVICEID_SMART ||
211 	    id == IDA_DEVICEID_DEC_SMART ||
212 	    id == IDA_DEVICEID_NCR_53C1510) {
213 		for (i = 0; board_id[i].board; i++)
214 			if (board_id[i].board == sub_id)
215 				return (&board_id[i]);
216 	}
217 	return (NULL);
218 }
219 
220 static int
221 ida_pci_probe(device_t dev)
222 {
223 	struct ida_board *board = ida_pci_match(dev);
224 
225 	if (board != NULL) {
226 		device_set_desc(dev, board->desc);
227 		return (BUS_PROBE_DEFAULT);
228 	}
229 	return (ENXIO);
230 }
231 
232 static int
233 ida_pci_attach(device_t dev)
234 {
235 	struct ida_board *board = ida_pci_match(dev);
236 	u_int32_t id = pci_get_devid(dev);
237 	struct ida_softc *ida;
238 	int error, rid;
239 
240 	ida = (struct ida_softc *)device_get_softc(dev);
241 	ida->dev = dev;
242 	ida->cmd = *board->accessor;
243 	ida->flags = board->flags;
244 	mtx_init(&ida->lock, "ida", NULL, MTX_DEF);
245 	callout_init_mtx(&ida->ch, &ida->lock, 0);
246 
247 	ida->regs_res_type = SYS_RES_MEMORY;
248 	ida->regs_res_id = IDA_PCI_MEMADDR;
249 	if (id == IDA_DEVICEID_DEC_SMART)
250 		ida->regs_res_id = PCIR_BAR(0);
251 
252 	ida->regs = bus_alloc_resource_any(dev, ida->regs_res_type,
253 	    &ida->regs_res_id, RF_ACTIVE);
254 	if (ida->regs == NULL) {
255 		device_printf(dev, "can't allocate memory resources\n");
256 		return (ENOMEM);
257 	}
258 
259 	error = bus_dma_tag_create(
260 		/* parent	*/ bus_get_dma_tag(dev),
261 		/* alignment	*/ 1,
262 		/* boundary	*/ 0,
263 		/* lowaddr	*/ BUS_SPACE_MAXADDR_32BIT,
264 		/* highaddr	*/ BUS_SPACE_MAXADDR,
265 		/* filter	*/ NULL,
266 		/* filterarg	*/ NULL,
267 		/* maxsize	*/ BUS_SPACE_MAXSIZE_32BIT,
268 		/* nsegments	*/ BUS_SPACE_UNRESTRICTED,
269 		/* maxsegsize	*/ BUS_SPACE_MAXSIZE_32BIT,
270 		/* flags	*/ BUS_DMA_ALLOCNOW,
271 		/* lockfunc	*/ NULL,
272 		/* lockarg	*/ NULL,
273 		&ida->parent_dmat);
274 	if (error != 0) {
275 		device_printf(dev, "can't allocate DMA tag\n");
276 		ida_free(ida);
277 		return (ENOMEM);
278 	}
279 
280 	rid = 0;
281 	ida->irq_res_type = SYS_RES_IRQ;
282 	ida->irq = bus_alloc_resource_any(dev, ida->irq_res_type, &rid,
283 	    RF_ACTIVE | RF_SHAREABLE);
284 	if (ida->irq == NULL) {
285 	        ida_free(ida);
286 	        return (ENOMEM);
287 	}
288 	error = bus_setup_intr(dev, ida->irq, INTR_TYPE_BIO | INTR_ENTROPY | INTR_MPSAFE,
289 	    NULL, ida_intr, ida, &ida->ih);
290 	if (error) {
291 		device_printf(dev, "can't setup interrupt\n");
292 		ida_free(ida);
293 		return (ENOMEM);
294 	}
295 
296 	error = ida_setup(ida);
297 	if (error) {
298 	        ida_free(ida);
299 	        return (error);
300 	}
301 
302 	return (0);
303 }
304 
305 DRIVER_MODULE(ida, pci, ida_pci_driver, 0, 0);
306 MODULE_PNP_INFO("W32:vendor/device;D:#", pci, ida, board_id,
307     nitems(board_id) - 1);
308