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