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