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