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