xref: /freebsd/sys/dev/ida/ida_pci.c (revision 2546665afcaf0d53dc2c7058fee96354b3680f5a)
1 /*-
2  * Copyright (c) 1999,2000 Jonathan Lemon
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 
35 #include <sys/bio.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 
39 #include <machine/bus_memio.h>
40 #include <machine/bus_pio.h>
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 	return (ida_inl(ida, R_DONE_FIFO));
78 }
79 
80 static int
81 ida_v3_int_pending(struct ida_softc *ida)
82 {
83 	return (ida_inl(ida, R_INT_PENDING));
84 }
85 
86 static void
87 ida_v3_int_enable(struct ida_softc *ida, int enable)
88 {
89 	if (enable)
90 		ida->flags |= IDA_INTERRUPTS;
91 	else
92 		ida->flags &= ~IDA_INTERRUPTS;
93 	ida_outl(ida, R_INT_MASK, enable ? INT_ENABLE : INT_DISABLE);
94 }
95 
96 static int
97 ida_v4_fifo_full(struct ida_softc *ida)
98 {
99 	return (ida_inl(ida, R_42XX_REQUEST) != 0);
100 }
101 
102 static void
103 ida_v4_submit(struct ida_softc *ida, struct ida_qcb *qcb)
104 {
105 	ida_outl(ida, R_42XX_REQUEST, qcb->hwqcb_busaddr);
106 }
107 
108 static bus_addr_t
109 ida_v4_done(struct ida_softc *ida)
110 {
111 	bus_addr_t completed;
112 
113 	completed = ida_inl(ida, R_42XX_REPLY);
114 	if (completed == -1)
115 		return (0);			/* fifo is empty */
116 	ida_outl(ida, R_42XX_REPLY, 0);		/* confirm read */
117 	return (completed);
118 }
119 
120 static int
121 ida_v4_int_pending(struct ida_softc *ida)
122 {
123 	return (ida_inl(ida, R_42XX_STATUS) & STATUS_42XX_INT_PENDING);
124 }
125 
126 static void
127 ida_v4_int_enable(struct ida_softc *ida, int enable)
128 {
129 	if (enable)
130 		ida->flags |= IDA_INTERRUPTS;
131 	else
132 		ida->flags &= ~IDA_INTERRUPTS;
133 	ida_outl(ida, R_42XX_INT_MASK,
134 	    enable ? INT_ENABLE_42XX : INT_DISABLE_42XX);
135 }
136 
137 static struct ida_access ida_v3_access = {
138 	ida_v3_fifo_full,
139 	ida_v3_submit,
140 	ida_v3_done,
141 	ida_v3_int_pending,
142 	ida_v3_int_enable,
143 };
144 
145 static struct ida_access ida_v4_access = {
146 	ida_v4_fifo_full,
147 	ida_v4_submit,
148 	ida_v4_done,
149 	ida_v4_int_pending,
150 	ida_v4_int_enable,
151 };
152 
153 static struct ida_board board_id[] = {
154 	{ 0x40300E11, "Compaq SMART-2/P array controller",
155 	    &ida_v3_access, 0 },
156 	{ 0x40310E11, "Compaq SMART-2SL array controller",
157 	    &ida_v3_access, 0 },
158 	{ 0x40320E11, "Compaq Smart Array 3200 controller",
159 	    &ida_v3_access, 0 },
160 	{ 0x40330E11, "Compaq Smart Array 3100ES controller",
161 	    &ida_v3_access, 0 },
162 	{ 0x40340E11, "Compaq Smart Array 221 controller",
163 	    &ida_v3_access, 0 },
164 
165 	{ 0x40400E11, "Compaq Integrated Array controller",
166 	    &ida_v4_access, IDA_FIRMWARE },
167 	{ 0x40480E11, "Compaq RAID LC2 controller",
168 	    &ida_v4_access, IDA_FIRMWARE },
169 	{ 0x40500E11, "Compaq Smart Array 4200 controller",
170 	    &ida_v4_access, 0 },
171 	{ 0x40510E11, "Compaq Smart Array 4250ES controller",
172 	    &ida_v4_access, 0 },
173 	{ 0x40580E11, "Compaq Smart Array 431 controller",
174 	    &ida_v4_access, 0 },
175 
176 	{ 0, "", 0, 0 },
177 };
178 
179 static int ida_pci_probe(device_t dev);
180 static int ida_pci_attach(device_t dev);
181 
182 static device_method_t ida_pci_methods[] = {
183 	DEVMETHOD(device_probe,		ida_pci_probe),
184 	DEVMETHOD(device_attach,	ida_pci_attach),
185 	DEVMETHOD(device_detach,	ida_detach),
186 
187 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
188 
189 	{ 0, 0 }
190 };
191 
192 static driver_t ida_pci_driver = {
193 	"ida",
194 	ida_pci_methods,
195 	sizeof(struct ida_softc)
196 };
197 
198 static devclass_t ida_devclass;
199 
200 static struct ida_board *
201 ida_pci_match(device_t dev)
202 {
203 	int i;
204 	u_int32_t id, sub_id;
205 
206 	id = pci_get_devid(dev);
207 	sub_id = pci_get_subdevice(dev) << 16 | pci_get_subvendor(dev);
208 
209 	if (id == IDA_DEVICEID_SMART ||
210 	    id == IDA_DEVICEID_DEC_SMART ||
211 	    id == IDA_DEVICEID_NCR_53C1510) {
212 		for (i = 0; board_id[i].board; i++)
213 			if (board_id[i].board == sub_id)
214 				return (&board_id[i]);
215 	}
216 	return (NULL);
217 }
218 
219 static int
220 ida_pci_probe(device_t dev)
221 {
222 	struct ida_board *board = ida_pci_match(dev);
223 
224 	if (board != NULL) {
225 		device_set_desc(dev, board->desc);
226 		return (0);
227 	}
228 	return (ENXIO);
229 }
230 
231 static int
232 ida_pci_attach(device_t dev)
233 {
234 	struct ida_board *board = ida_pci_match(dev);
235 	u_int32_t id = pci_get_devid(dev);
236 	struct ida_softc *ida;
237 	u_int command;
238 	int error, rid;
239 
240 	command = pci_read_config(dev, PCIR_COMMAND, 1);
241 
242 	/*
243 	 * it appears that this board only does MEMIO access.
244 	 */
245 	if ((command & PCIM_CMD_MEMEN) == 0) {
246                 device_printf(dev, "Only memory mapped I/O is supported\n");
247 		return (ENXIO);
248 	}
249 
250 	ida = (struct ida_softc *)device_get_softc(dev);
251 	ida->dev = dev;
252 	ida->cmd = *board->accessor;
253 	ida->flags = board->flags;
254 
255 	ida->regs_res_type = SYS_RES_MEMORY;
256 	ida->regs_res_id = IDA_PCI_MEMADDR;
257 	if (id == IDA_DEVICEID_DEC_SMART)
258 		ida->regs_res_id = PCIR_BAR(0);
259 
260 	ida->regs = bus_alloc_resource_any(dev, ida->regs_res_type,
261 	    &ida->regs_res_id, RF_ACTIVE);
262 	if (ida->regs == NULL) {
263 		device_printf(dev, "can't allocate memory resources\n");
264 		return (ENOMEM);
265 	}
266 
267 	error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
268 	    /*boundary*/0, /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
269 	    /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, /*filterarg*/NULL,
270 	    /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG,
271 	    /*maxsegsize*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/BUS_DMA_ALLOCNOW,
272 	    /*lockfunc*/NULL, /*lockarg*/NULL, &ida->parent_dmat);
273 	if (error != 0) {
274 		device_printf(dev, "can't allocate DMA tag\n");
275 		ida_free(ida);
276 		return (ENOMEM);
277 	}
278 
279 	rid = 0;
280         ida->irq_res_type = SYS_RES_IRQ;
281 	ida->irq = bus_alloc_resource_any(dev, ida->irq_res_type, &rid,
282 	    RF_ACTIVE | RF_SHAREABLE);
283         if (ida->irq == NULL) {
284                 ida_free(ida);
285                 return (ENOMEM);
286         }
287 	error = bus_setup_intr(dev, ida->irq, INTR_TYPE_BIO | INTR_ENTROPY,
288 	    ida_intr, ida, &ida->ih);
289 	if (error) {
290 		device_printf(dev, "can't setup interrupt\n");
291 		ida_free(ida);
292 		return (ENOMEM);
293 	}
294 
295 	error = ida_init(ida);
296 	if (error) {
297                 ida_free(ida);
298                 return (error);
299         }
300 	ida_attach(ida);
301 	ida->flags |= IDA_ATTACHED;
302 
303 	return (0);
304 }
305 
306 DRIVER_MODULE(ida, pci, ida_pci_driver, ida_devclass, 0, 0);
307