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