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