xref: /freebsd/sys/dev/ida/ida.c (revision 815308668ae3e9056e84b4b34f40ecc228f6d382)
1db57feb7SJonathan Lemon /*-
2ee7eb00eSJonathan Lemon  * Copyright (c) 1999,2000 Jonathan Lemon
3db57feb7SJonathan Lemon  * All rights reserved.
4db57feb7SJonathan Lemon  *
5db57feb7SJonathan Lemon  # Derived from the original IDA Compaq RAID driver, which is
6db57feb7SJonathan Lemon  * Copyright (c) 1996, 1997, 1998, 1999
7db57feb7SJonathan Lemon  *    Mark Dawson and David James. All rights reserved.
8db57feb7SJonathan Lemon  *
9db57feb7SJonathan Lemon  * Redistribution and use in source and binary forms, with or without
10db57feb7SJonathan Lemon  * modification, are permitted provided that the following conditions
11db57feb7SJonathan Lemon  * are met:
12db57feb7SJonathan Lemon  * 1. Redistributions of source code must retain the above copyright
13db57feb7SJonathan Lemon  *    notice, this list of conditions and the following disclaimer.
14db57feb7SJonathan Lemon  * 2. Redistributions in binary form must reproduce the above copyright
15db57feb7SJonathan Lemon  *    notice, this list of conditions and the following disclaimer in the
16db57feb7SJonathan Lemon  *    documentation and/or other materials provided with the distribution.
17db57feb7SJonathan Lemon  *
18db57feb7SJonathan Lemon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19db57feb7SJonathan Lemon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20db57feb7SJonathan Lemon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21db57feb7SJonathan Lemon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22db57feb7SJonathan Lemon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23db57feb7SJonathan Lemon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24db57feb7SJonathan Lemon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25db57feb7SJonathan Lemon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26db57feb7SJonathan Lemon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27db57feb7SJonathan Lemon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28db57feb7SJonathan Lemon  * SUCH DAMAGE.
29db57feb7SJonathan Lemon  *
30c3aac50fSPeter Wemm  * $FreeBSD$
31db57feb7SJonathan Lemon  */
32db57feb7SJonathan Lemon 
33db57feb7SJonathan Lemon /*
34db57feb7SJonathan Lemon  * Generic driver for Compaq SMART RAID adapters.
35db57feb7SJonathan Lemon  *
36db57feb7SJonathan Lemon  * Specific probe routines are in:
37db57feb7SJonathan Lemon  *	pci/ida_pci.c
38db57feb7SJonathan Lemon  *	i386/eisa/ida_eisa.c
39db57feb7SJonathan Lemon  */
40db57feb7SJonathan Lemon 
41db57feb7SJonathan Lemon #include <pci.h>
42db57feb7SJonathan Lemon 
43db57feb7SJonathan Lemon #include <sys/param.h>
44db57feb7SJonathan Lemon #include <sys/systm.h>
45db57feb7SJonathan Lemon #include <sys/malloc.h>
46db57feb7SJonathan Lemon #include <sys/kernel.h>
47db57feb7SJonathan Lemon 
48db57feb7SJonathan Lemon #include <sys/buf.h>
49db57feb7SJonathan Lemon #include <sys/bus.h>
50db57feb7SJonathan Lemon #include <sys/devicestat.h>
51ee7eb00eSJonathan Lemon #include <sys/disk.h>
52db57feb7SJonathan Lemon 
53db57feb7SJonathan Lemon #if NPCI > 0
54db57feb7SJonathan Lemon #include <machine/bus_memio.h>
55db57feb7SJonathan Lemon #endif
56db57feb7SJonathan Lemon #include <machine/bus_pio.h>
57db57feb7SJonathan Lemon #include <machine/bus.h>
58db57feb7SJonathan Lemon #include <machine/clock.h>
59db57feb7SJonathan Lemon #include <sys/rman.h>
60db57feb7SJonathan Lemon 
61db57feb7SJonathan Lemon #include <dev/ida/idareg.h>
62db57feb7SJonathan Lemon #include <dev/ida/idavar.h>
63db57feb7SJonathan Lemon 
64db57feb7SJonathan Lemon /* prototypes */
65db57feb7SJonathan Lemon static void ida_alloc_qcb(struct ida_softc *ida);
66db57feb7SJonathan Lemon static void ida_construct_qcb(struct ida_softc *ida);
67db57feb7SJonathan Lemon static void ida_start(struct ida_softc *ida);
68db57feb7SJonathan Lemon static void ida_done(struct ida_softc *ida, struct ida_qcb *qcb);
6994db13feSPoul-Henning Kamp static void ida_wait(struct ida_softc *ida, struct ida_qcb *qcb, int delay);
70db57feb7SJonathan Lemon 
71db57feb7SJonathan Lemon void
72db57feb7SJonathan Lemon ida_free(struct ida_softc *ida)
73db57feb7SJonathan Lemon {
74ee7eb00eSJonathan Lemon 	int i;
75db57feb7SJonathan Lemon 
76ee7eb00eSJonathan Lemon 	for (i = 0; i < ida->num_qcbs; i++)
77ee7eb00eSJonathan Lemon 		bus_dmamap_destroy(ida->buffer_dmat, ida->qcbs[i].dmamap);
78db57feb7SJonathan Lemon 
79db57feb7SJonathan Lemon 	if (ida->hwqcb_busaddr)
80db57feb7SJonathan Lemon 		bus_dmamap_unload(ida->hwqcb_dmat, ida->hwqcb_dmamap);
81db57feb7SJonathan Lemon 
82db57feb7SJonathan Lemon 	if (ida->hwqcbs)
83db57feb7SJonathan Lemon 		bus_dmamem_free(ida->hwqcb_dmat, ida->hwqcbs,
84db57feb7SJonathan Lemon 		    ida->hwqcb_dmamap);
85db57feb7SJonathan Lemon 
86db57feb7SJonathan Lemon 	if (ida->buffer_dmat)
87db57feb7SJonathan Lemon 		bus_dma_tag_destroy(ida->buffer_dmat);
88db57feb7SJonathan Lemon 
89db57feb7SJonathan Lemon 	if (ida->hwqcb_dmat)
90db57feb7SJonathan Lemon 		bus_dma_tag_destroy(ida->hwqcb_dmat);
91db57feb7SJonathan Lemon 
92db57feb7SJonathan Lemon 	if (ida->qcbs != NULL)
93db57feb7SJonathan Lemon 		free(ida->qcbs, M_DEVBUF);
94db57feb7SJonathan Lemon 
95db57feb7SJonathan Lemon 	if (ida->ih != NULL)
96db57feb7SJonathan Lemon                 bus_teardown_intr(ida->dev, ida->irq, ida->ih);
97db57feb7SJonathan Lemon 
98db57feb7SJonathan Lemon 	if (ida->irq != NULL)
99db57feb7SJonathan Lemon 		bus_release_resource(ida->dev, ida->irq_res_type,
100db57feb7SJonathan Lemon 		    0, ida->irq);
101db57feb7SJonathan Lemon 
102db57feb7SJonathan Lemon 	if (ida->parent_dmat != NULL)
103db57feb7SJonathan Lemon 		bus_dma_tag_destroy(ida->parent_dmat);
104db57feb7SJonathan Lemon 
105db57feb7SJonathan Lemon 	if (ida->regs != NULL)
106db57feb7SJonathan Lemon 		bus_release_resource(ida->dev, ida->regs_res_type,
107db57feb7SJonathan Lemon 		    ida->regs_res_id, ida->regs);
108db57feb7SJonathan Lemon }
109db57feb7SJonathan Lemon 
110db57feb7SJonathan Lemon /*
111db57feb7SJonathan Lemon  * record bus address from bus_dmamap_load
112db57feb7SJonathan Lemon  */
113db57feb7SJonathan Lemon static void
114db57feb7SJonathan Lemon ida_dma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
115db57feb7SJonathan Lemon {
116db57feb7SJonathan Lemon         bus_addr_t *baddr;
117db57feb7SJonathan Lemon 
118db57feb7SJonathan Lemon         baddr = (bus_addr_t *)arg;
119db57feb7SJonathan Lemon         *baddr = segs->ds_addr;
120db57feb7SJonathan Lemon }
121db57feb7SJonathan Lemon 
122db57feb7SJonathan Lemon static __inline struct ida_qcb *
123db57feb7SJonathan Lemon ida_get_qcb(struct ida_softc *ida)
124db57feb7SJonathan Lemon {
125db57feb7SJonathan Lemon 	struct ida_qcb *qcb;
126db57feb7SJonathan Lemon 
127db57feb7SJonathan Lemon 	if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL) {
128db57feb7SJonathan Lemon 		SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle);
129db57feb7SJonathan Lemon 	} else {
130db57feb7SJonathan Lemon 		ida_alloc_qcb(ida);
131db57feb7SJonathan Lemon 		if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL)
132db57feb7SJonathan Lemon 			SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle);
133db57feb7SJonathan Lemon 	}
134db57feb7SJonathan Lemon 	return (qcb);
135db57feb7SJonathan Lemon }
136db57feb7SJonathan Lemon 
137ee7eb00eSJonathan Lemon static __inline bus_addr_t
138ee7eb00eSJonathan Lemon idahwqcbvtop(struct ida_softc *ida, struct ida_hardware_qcb *hwqcb)
139ee7eb00eSJonathan Lemon {
140ee7eb00eSJonathan Lemon 	return (ida->hwqcb_busaddr +
141ee7eb00eSJonathan Lemon 	    ((bus_addr_t)hwqcb - (bus_addr_t)ida->hwqcbs));
142ee7eb00eSJonathan Lemon }
143ee7eb00eSJonathan Lemon 
144ee7eb00eSJonathan Lemon static __inline struct ida_qcb *
145ee7eb00eSJonathan Lemon idahwqcbptov(struct ida_softc *ida, bus_addr_t hwqcb_addr)
146ee7eb00eSJonathan Lemon {
147ee7eb00eSJonathan Lemon 	struct ida_hardware_qcb *hwqcb;
148ee7eb00eSJonathan Lemon 
149ee7eb00eSJonathan Lemon 	hwqcb = (struct ida_hardware_qcb *)
150ee7eb00eSJonathan Lemon 	    ((bus_addr_t)ida->hwqcbs + (hwqcb_addr - ida->hwqcb_busaddr));
151ee7eb00eSJonathan Lemon 	return (hwqcb->qcb);
152ee7eb00eSJonathan Lemon }
153ee7eb00eSJonathan Lemon 
154db57feb7SJonathan Lemon /*
155db57feb7SJonathan Lemon  * XXX
156db57feb7SJonathan Lemon  * since we allocate all QCB space up front during initialization, then
157db57feb7SJonathan Lemon  * why bother with this routine?
158db57feb7SJonathan Lemon  */
159db57feb7SJonathan Lemon static void
160db57feb7SJonathan Lemon ida_alloc_qcb(struct ida_softc *ida)
161db57feb7SJonathan Lemon {
162db57feb7SJonathan Lemon 	struct ida_qcb *qcb;
163db57feb7SJonathan Lemon 	int error;
164db57feb7SJonathan Lemon 
165db57feb7SJonathan Lemon 	if (ida->num_qcbs >= IDA_QCB_MAX)
166db57feb7SJonathan Lemon 		return;
167db57feb7SJonathan Lemon 
168db57feb7SJonathan Lemon 	qcb = &ida->qcbs[ida->num_qcbs];
169db57feb7SJonathan Lemon 
170db57feb7SJonathan Lemon 	error = bus_dmamap_create(ida->buffer_dmat, /*flags*/0, &qcb->dmamap);
171db57feb7SJonathan Lemon 	if (error != 0)
172db57feb7SJonathan Lemon 		return;
173db57feb7SJonathan Lemon 
174db57feb7SJonathan Lemon 	qcb->flags = QCB_FREE;
175db57feb7SJonathan Lemon 	qcb->hwqcb = &ida->hwqcbs[ida->num_qcbs];
176db57feb7SJonathan Lemon 	qcb->hwqcb->qcb = qcb;
177ee7eb00eSJonathan Lemon 	qcb->hwqcb_busaddr = idahwqcbvtop(ida, qcb->hwqcb);
178db57feb7SJonathan Lemon 	SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle);
179db57feb7SJonathan Lemon 	ida->num_qcbs++;
180db57feb7SJonathan Lemon }
181db57feb7SJonathan Lemon 
182db57feb7SJonathan Lemon int
183db57feb7SJonathan Lemon ida_init(struct ida_softc *ida)
184db57feb7SJonathan Lemon {
185db57feb7SJonathan Lemon 	int error;
186db57feb7SJonathan Lemon 
187db57feb7SJonathan Lemon 	ida->unit = device_get_unit(ida->dev);
188db57feb7SJonathan Lemon 	ida->tag = rman_get_bustag(ida->regs);
189db57feb7SJonathan Lemon 	ida->bsh = rman_get_bushandle(ida->regs);
190db57feb7SJonathan Lemon 
191db57feb7SJonathan Lemon 	SLIST_INIT(&ida->free_qcbs);
192db57feb7SJonathan Lemon 	STAILQ_INIT(&ida->qcb_queue);
1938177437dSPoul-Henning Kamp         bioq_init(&ida->bio_queue);
194db57feb7SJonathan Lemon 
195db57feb7SJonathan Lemon 	ida->qcbs = (struct ida_qcb *)
196db57feb7SJonathan Lemon 	    malloc(IDA_QCB_MAX * sizeof(struct ida_qcb), M_DEVBUF, M_NOWAIT);
197db57feb7SJonathan Lemon 	if (ida->qcbs == NULL)
198db57feb7SJonathan Lemon 		return (ENOMEM);
199db57feb7SJonathan Lemon 	bzero(ida->qcbs, IDA_QCB_MAX * sizeof(struct ida_qcb));
200db57feb7SJonathan Lemon 
201db57feb7SJonathan Lemon 	/*
202db57feb7SJonathan Lemon 	 * Create our DMA tags
203db57feb7SJonathan Lemon 	 */
204db57feb7SJonathan Lemon 
205db57feb7SJonathan Lemon 	/* DMA tag for our hardware QCB structures */
206db57feb7SJonathan Lemon 	error = bus_dma_tag_create(ida->parent_dmat,
207086646f7SJustin T. Gibbs 	    /*alignment*/1, /*boundary*/0,
208db57feb7SJonathan Lemon 	    /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR,
209db57feb7SJonathan Lemon 	    /*filter*/NULL, /*filterarg*/NULL,
210db57feb7SJonathan Lemon 	    IDA_QCB_MAX * sizeof(struct ida_hardware_qcb),
211db57feb7SJonathan Lemon 	    /*nsegments*/1, /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
212db57feb7SJonathan Lemon 	    /*flags*/0, &ida->hwqcb_dmat);
213db57feb7SJonathan Lemon 	if (error)
214db57feb7SJonathan Lemon                 return (ENOMEM);
215db57feb7SJonathan Lemon 
216db57feb7SJonathan Lemon 	/* DMA tag for mapping buffers into device space */
217db57feb7SJonathan Lemon 	error = bus_dma_tag_create(ida->parent_dmat,
218086646f7SJustin T. Gibbs 	    /*alignment*/1, /*boundary*/0,
219db57feb7SJonathan Lemon 	    /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR,
220db57feb7SJonathan Lemon 	    /*filter*/NULL, /*filterarg*/NULL,
221db57feb7SJonathan Lemon 	    /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG,
222db57feb7SJonathan Lemon 	    /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/0, &ida->buffer_dmat);
223db57feb7SJonathan Lemon 	if (error)
224db57feb7SJonathan Lemon                 return (ENOMEM);
225db57feb7SJonathan Lemon 
226db57feb7SJonathan Lemon         /* Allocation of hardware QCBs */
227db57feb7SJonathan Lemon 	/* XXX allocation is rounded to hardware page size */
228db57feb7SJonathan Lemon 	error = bus_dmamem_alloc(ida->hwqcb_dmat,
229db57feb7SJonathan Lemon 	    (void **)&ida->hwqcbs, BUS_DMA_NOWAIT, &ida->hwqcb_dmamap);
230db57feb7SJonathan Lemon 	if (error)
231db57feb7SJonathan Lemon                 return (ENOMEM);
232db57feb7SJonathan Lemon 
233db57feb7SJonathan Lemon         /* And permanently map them in */
234db57feb7SJonathan Lemon         bus_dmamap_load(ida->hwqcb_dmat, ida->hwqcb_dmamap,
235db57feb7SJonathan Lemon 	    ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb),
236db57feb7SJonathan Lemon 	    ida_dma_map_cb, &ida->hwqcb_busaddr, /*flags*/0);
237db57feb7SJonathan Lemon 
238db57feb7SJonathan Lemon 	bzero(ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb));
239db57feb7SJonathan Lemon 
240db57feb7SJonathan Lemon 	ida_alloc_qcb(ida);		/* allocate an initial qcb */
241db57feb7SJonathan Lemon 
242db57feb7SJonathan Lemon 	return (0);
243db57feb7SJonathan Lemon }
244db57feb7SJonathan Lemon 
245db57feb7SJonathan Lemon void
246db57feb7SJonathan Lemon ida_attach(struct ida_softc *ida)
247db57feb7SJonathan Lemon {
248db57feb7SJonathan Lemon 	struct ida_controller_info cinfo;
249db57feb7SJonathan Lemon 	int error, i;
250db57feb7SJonathan Lemon 
251ee7eb00eSJonathan Lemon 	ida->cmd.int_enable(ida, 0);
252db57feb7SJonathan Lemon 
253db57feb7SJonathan Lemon 	error = ida_command(ida, CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo),
254db57feb7SJonathan Lemon 	    IDA_CONTROLLER, DMA_DATA_IN);
255db57feb7SJonathan Lemon 	if (error) {
256db57feb7SJonathan Lemon 		device_printf(ida->dev, "CMD_GET_CTRL_INFO failed.\n");
257db57feb7SJonathan Lemon 		return;
258db57feb7SJonathan Lemon 	}
259db57feb7SJonathan Lemon 
260db57feb7SJonathan Lemon 	device_printf(ida->dev, "drives=%d firm_rev=%c%c%c%c\n",
261db57feb7SJonathan Lemon 	    cinfo.num_drvs, cinfo.firm_rev[0], cinfo.firm_rev[1],
262db57feb7SJonathan Lemon 	    cinfo.firm_rev[2], cinfo.firm_rev[3]);
263db57feb7SJonathan Lemon 
264db57feb7SJonathan Lemon 	ida->num_drives = cinfo.num_drvs;
265db57feb7SJonathan Lemon 
266db57feb7SJonathan Lemon 	for (i = 0; i < ida->num_drives; i++)
267ee7eb00eSJonathan Lemon 		device_add_child(ida->dev, /*"idad"*/NULL, -1);
268db57feb7SJonathan Lemon 
269db57feb7SJonathan Lemon 	bus_generic_attach(ida->dev);
270db57feb7SJonathan Lemon 
271ee7eb00eSJonathan Lemon 	ida->cmd.int_enable(ida, 1);
272ee7eb00eSJonathan Lemon }
273ee7eb00eSJonathan Lemon 
274ee7eb00eSJonathan Lemon int
275ee7eb00eSJonathan Lemon ida_detach(device_t dev)
276ee7eb00eSJonathan Lemon {
277ee7eb00eSJonathan Lemon 	struct ida_softc *ida;
278ee7eb00eSJonathan Lemon 	int error = 0;
279ee7eb00eSJonathan Lemon 
280ee7eb00eSJonathan Lemon         ida = (struct ida_softc *)device_get_softc(dev);
281ee7eb00eSJonathan Lemon 
282ee7eb00eSJonathan Lemon 	/*
283ee7eb00eSJonathan Lemon 	 * XXX
284ee7eb00eSJonathan Lemon 	 * before detaching, we must make sure that the system is
285ee7eb00eSJonathan Lemon 	 * quiescent; nothing mounted, no pending activity.
286ee7eb00eSJonathan Lemon 	 */
287ee7eb00eSJonathan Lemon 
288ee7eb00eSJonathan Lemon 	/*
289ee7eb00eSJonathan Lemon 	 * XXX
290ee7eb00eSJonathan Lemon 	 * now, how are we supposed to maintain a list of our drives?
291ee7eb00eSJonathan Lemon 	 * iterate over our "child devices"?
292ee7eb00eSJonathan Lemon 	 */
293ee7eb00eSJonathan Lemon 
294ee7eb00eSJonathan Lemon 
295ee7eb00eSJonathan Lemon 	ida_free(ida);
296ee7eb00eSJonathan Lemon 	return (error);
297db57feb7SJonathan Lemon }
298db57feb7SJonathan Lemon 
299db57feb7SJonathan Lemon static void
300db57feb7SJonathan Lemon ida_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
301db57feb7SJonathan Lemon {
302db57feb7SJonathan Lemon 	struct ida_hardware_qcb *hwqcb = (struct ida_hardware_qcb *)arg;
303db57feb7SJonathan Lemon 	int i;
304db57feb7SJonathan Lemon 
305db57feb7SJonathan Lemon 	hwqcb->hdr.size = (sizeof(struct ida_req) +
306db57feb7SJonathan Lemon 	    sizeof(struct ida_sgb) * IDA_NSEG) >> 2;
307db57feb7SJonathan Lemon 
308db57feb7SJonathan Lemon 	for (i = 0; i < nsegments; i++) {
309db57feb7SJonathan Lemon 		hwqcb->seg[i].addr = segs[i].ds_addr;
310db57feb7SJonathan Lemon 		hwqcb->seg[i].length = segs[i].ds_len;
311db57feb7SJonathan Lemon 	}
312db57feb7SJonathan Lemon 	hwqcb->req.sgcount = nsegments;
313db57feb7SJonathan Lemon }
314db57feb7SJonathan Lemon 
315db57feb7SJonathan Lemon int
316db57feb7SJonathan Lemon ida_command(struct ida_softc *ida, int command, void *data, int datasize,
317db57feb7SJonathan Lemon 	int drive, int flags)
318db57feb7SJonathan Lemon {
319db57feb7SJonathan Lemon 	struct ida_hardware_qcb *hwqcb;
320db57feb7SJonathan Lemon 	struct ida_qcb *qcb;
321db57feb7SJonathan Lemon 	bus_dmasync_op_t op;
322db57feb7SJonathan Lemon 	int s;
323db57feb7SJonathan Lemon 
324db57feb7SJonathan Lemon 	s = splbio();
325db57feb7SJonathan Lemon 	qcb = ida_get_qcb(ida);
326db57feb7SJonathan Lemon 	splx(s);
327db57feb7SJonathan Lemon 
328db57feb7SJonathan Lemon 	if (qcb == NULL) {
329db57feb7SJonathan Lemon 		printf("ida_command: out of QCBs");
330db57feb7SJonathan Lemon 		return (1);
331db57feb7SJonathan Lemon 	}
332db57feb7SJonathan Lemon 
333db57feb7SJonathan Lemon 	hwqcb = qcb->hwqcb;
334db57feb7SJonathan Lemon 	bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req));
335db57feb7SJonathan Lemon 
336db57feb7SJonathan Lemon 	bus_dmamap_load(ida->buffer_dmat, qcb->dmamap,
337db57feb7SJonathan Lemon 	    (void *)data, datasize, ida_setup_dmamap, hwqcb, 0);
338db57feb7SJonathan Lemon 	op = qcb->flags & DMA_DATA_IN ?
339db57feb7SJonathan Lemon 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE;
340db57feb7SJonathan Lemon 	bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
341db57feb7SJonathan Lemon 
342db57feb7SJonathan Lemon 	hwqcb->hdr.drive = drive;		/* XXX */
343db57feb7SJonathan Lemon 	hwqcb->req.bcount = howmany(datasize, DEV_BSIZE);
344db57feb7SJonathan Lemon 	hwqcb->req.command = command;
345db57feb7SJonathan Lemon 
346db57feb7SJonathan Lemon 	qcb->flags = flags | IDA_COMMAND;
347db57feb7SJonathan Lemon 
348db57feb7SJonathan Lemon 	s = splbio();
349db57feb7SJonathan Lemon 	STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe);
350db57feb7SJonathan Lemon 	ida_start(ida);
351db57feb7SJonathan Lemon 	ida_wait(ida, qcb, 500);
352db57feb7SJonathan Lemon 	splx(s);
353db57feb7SJonathan Lemon 
354db57feb7SJonathan Lemon 	/* XXX should have status returned here? */
355db57feb7SJonathan Lemon 	/* XXX have "status pointer" area in QCB? */
356db57feb7SJonathan Lemon 
357db57feb7SJonathan Lemon 	return (0);
358db57feb7SJonathan Lemon }
359db57feb7SJonathan Lemon 
360db57feb7SJonathan Lemon void
3618177437dSPoul-Henning Kamp ida_submit_buf(struct ida_softc *ida, struct bio *bp)
362db57feb7SJonathan Lemon {
3638177437dSPoul-Henning Kamp         bioq_insert_tail(&ida->bio_queue, bp);
364db57feb7SJonathan Lemon         ida_construct_qcb(ida);
365db57feb7SJonathan Lemon 	ida_start(ida);
366db57feb7SJonathan Lemon }
367db57feb7SJonathan Lemon 
368db57feb7SJonathan Lemon static void
369db57feb7SJonathan Lemon ida_construct_qcb(struct ida_softc *ida)
370db57feb7SJonathan Lemon {
371db57feb7SJonathan Lemon 	struct ida_hardware_qcb *hwqcb;
372db57feb7SJonathan Lemon 	struct ida_qcb *qcb;
373db57feb7SJonathan Lemon 	bus_dmasync_op_t op;
3748177437dSPoul-Henning Kamp 	struct bio *bp;
375db57feb7SJonathan Lemon 
3768177437dSPoul-Henning Kamp 	bp = bioq_first(&ida->bio_queue);
377db57feb7SJonathan Lemon 	if (bp == NULL)
378db57feb7SJonathan Lemon 		return;				/* no more buffers */
379db57feb7SJonathan Lemon 
380db57feb7SJonathan Lemon 	qcb = ida_get_qcb(ida);
381db57feb7SJonathan Lemon 	if (qcb == NULL)
382db57feb7SJonathan Lemon 		return;				/* out of resources */
383db57feb7SJonathan Lemon 
3848177437dSPoul-Henning Kamp 	bioq_remove(&ida->bio_queue, bp);
385db57feb7SJonathan Lemon 	qcb->buf = bp;
386db57feb7SJonathan Lemon 	qcb->flags = 0;
387db57feb7SJonathan Lemon 
388db57feb7SJonathan Lemon 	hwqcb = qcb->hwqcb;
389db57feb7SJonathan Lemon 	bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req));
390db57feb7SJonathan Lemon 
391db57feb7SJonathan Lemon 	bus_dmamap_load(ida->buffer_dmat, qcb->dmamap,
3928177437dSPoul-Henning Kamp 	    (void *)bp->bio_data, bp->bio_bcount, ida_setup_dmamap, hwqcb, 0);
393db57feb7SJonathan Lemon 	op = qcb->flags & DMA_DATA_IN ?
394db57feb7SJonathan Lemon 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE;
395db57feb7SJonathan Lemon 	bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
396db57feb7SJonathan Lemon 
397db57feb7SJonathan Lemon 	/*
398db57feb7SJonathan Lemon 	 * XXX
399db57feb7SJonathan Lemon 	 */
400db57feb7SJonathan Lemon 	{
40181530866SMatthew N. Dodd 		struct idad_softc *drv = (struct idad_softc *)bp->bio_driver1;
402db57feb7SJonathan Lemon 		hwqcb->hdr.drive = drv->unit;
403db57feb7SJonathan Lemon 	}
404db57feb7SJonathan Lemon 
4058177437dSPoul-Henning Kamp 	hwqcb->req.blkno = bp->bio_pblkno;
4068177437dSPoul-Henning Kamp 	hwqcb->req.bcount = howmany(bp->bio_bcount, DEV_BSIZE);
4078177437dSPoul-Henning Kamp 	hwqcb->req.command = bp->bio_cmd == BIO_READ ? CMD_READ : CMD_WRITE;
408db57feb7SJonathan Lemon 
409db57feb7SJonathan Lemon 	STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe);
410db57feb7SJonathan Lemon }
411db57feb7SJonathan Lemon 
412db57feb7SJonathan Lemon /*
413db57feb7SJonathan Lemon  * This routine will be called from ida_intr in order to queue up more
414db57feb7SJonathan Lemon  * I/O, meaning that we may be in an interrupt context.  Hence, we should
415db57feb7SJonathan Lemon  * not muck around with spl() in this routine.
416db57feb7SJonathan Lemon  */
417db57feb7SJonathan Lemon static void
418db57feb7SJonathan Lemon ida_start(struct ida_softc *ida)
419db57feb7SJonathan Lemon {
420db57feb7SJonathan Lemon 	struct ida_qcb *qcb;
421db57feb7SJonathan Lemon 
422db57feb7SJonathan Lemon 	while ((qcb = STAILQ_FIRST(&ida->qcb_queue)) != NULL) {
423ee7eb00eSJonathan Lemon 		if (ida->cmd.fifo_full(ida))
424ee7eb00eSJonathan Lemon 			break;
425db57feb7SJonathan Lemon 		STAILQ_REMOVE_HEAD(&ida->qcb_queue, link.stqe);
426db57feb7SJonathan Lemon 		/*
427db57feb7SJonathan Lemon 		 * XXX
428db57feb7SJonathan Lemon 		 * place the qcb on an active list and set a timeout?
429db57feb7SJonathan Lemon 		 */
430db57feb7SJonathan Lemon 		qcb->state = QCB_ACTIVE;
431ee7eb00eSJonathan Lemon 		ida->cmd.submit(ida, qcb);
432db57feb7SJonathan Lemon 	}
433db57feb7SJonathan Lemon }
434db57feb7SJonathan Lemon 
43594db13feSPoul-Henning Kamp static
436db57feb7SJonathan Lemon void
437db57feb7SJonathan Lemon ida_wait(struct ida_softc *ida, struct ida_qcb *qcb, int delay)
438db57feb7SJonathan Lemon {
439db57feb7SJonathan Lemon 	struct ida_qcb *qcb_done = NULL;
440db57feb7SJonathan Lemon 	bus_addr_t completed;
441db57feb7SJonathan Lemon 
442db57feb7SJonathan Lemon 	if (ida->flags & IDA_ATTACHED) {
443db57feb7SJonathan Lemon 		if (tsleep((caddr_t)qcb, PRIBIO, "idacmd", delay))
444db57feb7SJonathan Lemon 			panic("ida_command: timeout waiting for interrupt");
445db57feb7SJonathan Lemon 		return;
446db57feb7SJonathan Lemon 	}
447db57feb7SJonathan Lemon 
448ee7eb00eSJonathan Lemon 	while ((completed = ida->cmd.done(ida)) == 0) {
449db57feb7SJonathan Lemon 		if (delay-- == 0)
450db57feb7SJonathan Lemon 			panic("ida_wait: timeout waiting for completion");
451db57feb7SJonathan Lemon 		DELAY(10);
452db57feb7SJonathan Lemon 	}
453db57feb7SJonathan Lemon 
454db57feb7SJonathan Lemon 	qcb_done = idahwqcbptov(ida, completed & ~3);
455db57feb7SJonathan Lemon 	if (qcb_done != qcb)
456db57feb7SJonathan Lemon 		panic("ida_wait: incorrect qcb returned");
457db57feb7SJonathan Lemon 	ida_done(ida, qcb);
458db57feb7SJonathan Lemon 	return;
459db57feb7SJonathan Lemon }
460db57feb7SJonathan Lemon 
461db57feb7SJonathan Lemon void
462db57feb7SJonathan Lemon ida_intr(void *data)
463db57feb7SJonathan Lemon {
464db57feb7SJonathan Lemon 	struct ida_softc *ida;
465db57feb7SJonathan Lemon 	struct ida_qcb *qcb;
466db57feb7SJonathan Lemon 	bus_addr_t completed;
467db57feb7SJonathan Lemon 
468db57feb7SJonathan Lemon 	ida = (struct ida_softc *)data;
469db57feb7SJonathan Lemon 
470ee7eb00eSJonathan Lemon 	if (ida->cmd.int_pending(ida) == 0)
471db57feb7SJonathan Lemon 		return;				/* not our interrupt */
472db57feb7SJonathan Lemon 
473ee7eb00eSJonathan Lemon 	while ((completed = ida->cmd.done(ida)) != 0) {
474db57feb7SJonathan Lemon 		qcb = idahwqcbptov(ida, completed & ~3);
475db57feb7SJonathan Lemon 
476db57feb7SJonathan Lemon 		if (qcb == NULL || qcb->state != QCB_ACTIVE) {
477db57feb7SJonathan Lemon 			device_printf(ida->dev,
478db57feb7SJonathan Lemon 			    "ignoring completion %x\n", completed);
479db57feb7SJonathan Lemon 			continue;
480db57feb7SJonathan Lemon 		}
481db57feb7SJonathan Lemon 		ida_done(ida, qcb);
482db57feb7SJonathan Lemon 	}
483db57feb7SJonathan Lemon 	ida_start(ida);
484db57feb7SJonathan Lemon }
485db57feb7SJonathan Lemon 
486db57feb7SJonathan Lemon /*
487db57feb7SJonathan Lemon  * should switch out command type; may be status, not just I/O.
488db57feb7SJonathan Lemon  */
489db57feb7SJonathan Lemon static void
490db57feb7SJonathan Lemon ida_done(struct ida_softc *ida, struct ida_qcb *qcb)
491db57feb7SJonathan Lemon {
492db57feb7SJonathan Lemon 	int error = 0;
493db57feb7SJonathan Lemon 
494db57feb7SJonathan Lemon 	/*
495db57feb7SJonathan Lemon 	 * finish up command
496db57feb7SJonathan Lemon 	 */
497db57feb7SJonathan Lemon 	if (qcb->flags & DMA_DATA_TRANSFER) {
498db57feb7SJonathan Lemon 		bus_dmasync_op_t op;
499db57feb7SJonathan Lemon 
500db57feb7SJonathan Lemon 		op = qcb->flags & DMA_DATA_IN ?
501db57feb7SJonathan Lemon 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE;
502db57feb7SJonathan Lemon 		bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
503db57feb7SJonathan Lemon 		bus_dmamap_unload(ida->buffer_dmat, qcb->dmamap);
504db57feb7SJonathan Lemon 	}
505db57feb7SJonathan Lemon 
506db57feb7SJonathan Lemon 	if (qcb->hwqcb->req.error & SOFT_ERROR)
507db57feb7SJonathan Lemon 		device_printf(ida->dev, "soft error\n");
508db57feb7SJonathan Lemon 	if (qcb->hwqcb->req.error & HARD_ERROR) {
509db57feb7SJonathan Lemon 		error = 1;
510db57feb7SJonathan Lemon 		device_printf(ida->dev, "hard error\n");
511db57feb7SJonathan Lemon 	}
512db57feb7SJonathan Lemon 	if (qcb->hwqcb->req.error & CMD_REJECTED) {
513db57feb7SJonathan Lemon 		error = 1;
514db57feb7SJonathan Lemon 		device_printf(ida->dev, "invalid request\n");
515db57feb7SJonathan Lemon 	}
516db57feb7SJonathan Lemon 
517db57feb7SJonathan Lemon 	if (qcb->flags & IDA_COMMAND) {
518db57feb7SJonathan Lemon 		if (ida->flags & IDA_ATTACHED)
519db57feb7SJonathan Lemon 			wakeup(qcb);
520db57feb7SJonathan Lemon 	} else {
521db57feb7SJonathan Lemon 		if (error)
5228177437dSPoul-Henning Kamp 			qcb->buf->bio_flags |= BIO_ERROR;
52381530866SMatthew N. Dodd 		idad_intr(qcb->buf);
524db57feb7SJonathan Lemon 	}
525db57feb7SJonathan Lemon 
526db57feb7SJonathan Lemon 	qcb->state = QCB_FREE;
527db57feb7SJonathan Lemon 	SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle);
528db57feb7SJonathan Lemon 	ida_construct_qcb(ida);
529db57feb7SJonathan Lemon }
530