xref: /dragonfly/sys/dev/raid/ida/ida.c (revision af79c6e5)
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  * $FreeBSD: src/sys/dev/ida/ida.c,v 1.7.2.3 2001/03/01 01:57:32 ps Exp $
31  * $DragonFly: src/sys/dev/raid/ida/ida.c,v 1.6 2003/11/20 22:07:33 dillon Exp $
32  */
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 <use_pci.h>
43 
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/proc.h>
50 #include <sys/buf.h>
51 #include <sys/bus.h>
52 #include <sys/devicestat.h>
53 #include <sys/disk.h>
54 
55 #if NPCI > 0
56 #include <machine/bus_memio.h>
57 #endif
58 #include <machine/bus_pio.h>
59 #include <machine/bus.h>
60 #include <machine/clock.h>
61 #include <sys/rman.h>
62 #include <sys/buf2.h>
63 
64 #include "idareg.h"
65 #include "idavar.h"
66 
67 /* prototypes */
68 static void ida_alloc_qcb(struct ida_softc *ida);
69 static void ida_construct_qcb(struct ida_softc *ida);
70 static void ida_start(struct ida_softc *ida);
71 static void ida_done(struct ida_softc *ida, struct ida_qcb *qcb);
72 static int ida_wait(struct ida_softc *ida, struct ida_qcb *qcb);
73 
74 DECLARE_DUMMY_MODULE(ida);
75 
76 void
77 ida_free(struct ida_softc *ida)
78 {
79 	int i;
80 
81 	for (i = 0; i < ida->num_qcbs; i++)
82 		bus_dmamap_destroy(ida->buffer_dmat, ida->qcbs[i].dmamap);
83 
84 	if (ida->hwqcb_busaddr)
85 		bus_dmamap_unload(ida->hwqcb_dmat, ida->hwqcb_dmamap);
86 
87 	if (ida->hwqcbs)
88 		bus_dmamem_free(ida->hwqcb_dmat, ida->hwqcbs,
89 		    ida->hwqcb_dmamap);
90 
91 	if (ida->buffer_dmat)
92 		bus_dma_tag_destroy(ida->buffer_dmat);
93 
94 	if (ida->hwqcb_dmat)
95 		bus_dma_tag_destroy(ida->hwqcb_dmat);
96 
97 	if (ida->qcbs != NULL)
98 		free(ida->qcbs, M_DEVBUF);
99 
100 	if (ida->ih != NULL)
101                 bus_teardown_intr(ida->dev, ida->irq, ida->ih);
102 
103 	if (ida->irq != NULL)
104 		bus_release_resource(ida->dev, ida->irq_res_type,
105 		    0, ida->irq);
106 
107 	if (ida->parent_dmat != NULL)
108 		bus_dma_tag_destroy(ida->parent_dmat);
109 
110 	if (ida->regs != NULL)
111 		bus_release_resource(ida->dev, ida->regs_res_type,
112 		    ida->regs_res_id, ida->regs);
113 }
114 
115 /*
116  * record bus address from bus_dmamap_load
117  */
118 static void
119 ida_dma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
120 {
121         bus_addr_t *baddr;
122 
123         baddr = (bus_addr_t *)arg;
124         *baddr = segs->ds_addr;
125 }
126 
127 static __inline struct ida_qcb *
128 ida_get_qcb(struct ida_softc *ida)
129 {
130 	struct ida_qcb *qcb;
131 
132 	if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL) {
133 		SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle);
134 	} else {
135 		ida_alloc_qcb(ida);
136 		if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL)
137 			SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle);
138 	}
139 	return (qcb);
140 }
141 
142 static __inline bus_addr_t
143 idahwqcbvtop(struct ida_softc *ida, struct ida_hardware_qcb *hwqcb)
144 {
145 	return (ida->hwqcb_busaddr +
146 	    ((bus_addr_t)hwqcb - (bus_addr_t)ida->hwqcbs));
147 }
148 
149 static __inline struct ida_qcb *
150 idahwqcbptov(struct ida_softc *ida, bus_addr_t hwqcb_addr)
151 {
152 	struct ida_hardware_qcb *hwqcb;
153 
154 	hwqcb = (struct ida_hardware_qcb *)
155 	    ((bus_addr_t)ida->hwqcbs + (hwqcb_addr - ida->hwqcb_busaddr));
156 	return (hwqcb->qcb);
157 }
158 
159 /*
160  * XXX
161  * since we allocate all QCB space up front during initialization, then
162  * why bother with this routine?
163  */
164 static void
165 ida_alloc_qcb(struct ida_softc *ida)
166 {
167 	struct ida_qcb *qcb;
168 	int error;
169 
170 	if (ida->num_qcbs >= IDA_QCB_MAX)
171 		return;
172 
173 	qcb = &ida->qcbs[ida->num_qcbs];
174 
175 	error = bus_dmamap_create(ida->buffer_dmat, /*flags*/0, &qcb->dmamap);
176 	if (error != 0)
177 		return;
178 
179 	qcb->flags = QCB_FREE;
180 	qcb->hwqcb = &ida->hwqcbs[ida->num_qcbs];
181 	qcb->hwqcb->qcb = qcb;
182 	qcb->hwqcb_busaddr = idahwqcbvtop(ida, qcb->hwqcb);
183 	SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle);
184 	ida->num_qcbs++;
185 }
186 
187 int
188 ida_init(struct ida_softc *ida)
189 {
190 	int error;
191 
192 	ida->unit = device_get_unit(ida->dev);
193 	ida->tag = rman_get_bustag(ida->regs);
194 	ida->bsh = rman_get_bushandle(ida->regs);
195 
196 	SLIST_INIT(&ida->free_qcbs);
197 	STAILQ_INIT(&ida->qcb_queue);
198         bufq_init(&ida->buf_queue);
199 
200 	ida->qcbs = (struct ida_qcb *)
201 	    malloc(IDA_QCB_MAX * sizeof(struct ida_qcb), M_DEVBUF, M_NOWAIT);
202 	if (ida->qcbs == NULL)
203 		return (ENOMEM);
204 	bzero(ida->qcbs, IDA_QCB_MAX * sizeof(struct ida_qcb));
205 
206 	/*
207 	 * Create our DMA tags
208 	 */
209 
210 	/* DMA tag for our hardware QCB structures */
211 	error = bus_dma_tag_create(ida->parent_dmat,
212 	    /*alignment*/1, /*boundary*/0,
213 	    /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR,
214 	    /*filter*/NULL, /*filterarg*/NULL,
215 	    IDA_QCB_MAX * sizeof(struct ida_hardware_qcb),
216 	    /*nsegments*/1, /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
217 	    /*flags*/0, &ida->hwqcb_dmat);
218 	if (error)
219                 return (ENOMEM);
220 
221 	/* DMA tag for mapping buffers into device space */
222 	error = bus_dma_tag_create(ida->parent_dmat,
223 	    /*alignment*/1, /*boundary*/0,
224 	    /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR,
225 	    /*filter*/NULL, /*filterarg*/NULL,
226 	    /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG,
227 	    /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/0, &ida->buffer_dmat);
228 	if (error)
229                 return (ENOMEM);
230 
231         /* Allocation of hardware QCBs */
232 	/* XXX allocation is rounded to hardware page size */
233 	error = bus_dmamem_alloc(ida->hwqcb_dmat,
234 	    (void **)&ida->hwqcbs, BUS_DMA_NOWAIT, &ida->hwqcb_dmamap);
235 	if (error)
236                 return (ENOMEM);
237 
238         /* And permanently map them in */
239         bus_dmamap_load(ida->hwqcb_dmat, ida->hwqcb_dmamap,
240 	    ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb),
241 	    ida_dma_map_cb, &ida->hwqcb_busaddr, /*flags*/0);
242 
243 	bzero(ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb));
244 
245 	ida_alloc_qcb(ida);		/* allocate an initial qcb */
246 
247 	return (0);
248 }
249 
250 void
251 ida_attach(struct ida_softc *ida)
252 {
253 	struct ida_controller_info cinfo;
254 	int error, i;
255 
256 	ida->cmd.int_enable(ida, 0);
257 
258 	error = ida_command(ida, CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo),
259 	    IDA_CONTROLLER, 0, DMA_DATA_IN);
260 	if (error) {
261 		device_printf(ida->dev, "CMD_GET_CTRL_INFO failed.\n");
262 		return;
263 	}
264 
265 	device_printf(ida->dev, "drives=%d firm_rev=%c%c%c%c\n",
266 	    cinfo.num_drvs, cinfo.firm_rev[0], cinfo.firm_rev[1],
267 	    cinfo.firm_rev[2], cinfo.firm_rev[3]);
268 
269 	if (ida->flags & IDA_FIRMWARE) {
270 		int data;
271 
272 		error = ida_command(ida, CMD_START_FIRMWARE,
273 		    &data, sizeof(data), IDA_CONTROLLER, 0, DMA_DATA_IN);
274 		if (error) {
275 			device_printf(ida->dev, "CMD_START_FIRMWARE failed.\n");
276 			return;
277 		}
278 	}
279 
280 	ida->num_drives = 0;
281 	for (i = 0; i < cinfo.num_drvs; i++)
282 		device_add_child(ida->dev, /*"idad"*/NULL, -1);
283 
284 	bus_generic_attach(ida->dev);
285 
286 	ida->cmd.int_enable(ida, 1);
287 }
288 
289 int
290 ida_detach(device_t dev)
291 {
292 	struct ida_softc *ida;
293 	int error = 0;
294 
295         ida = (struct ida_softc *)device_get_softc(dev);
296 
297 	/*
298 	 * XXX
299 	 * before detaching, we must make sure that the system is
300 	 * quiescent; nothing mounted, no pending activity.
301 	 */
302 
303 	/*
304 	 * XXX
305 	 * now, how are we supposed to maintain a list of our drives?
306 	 * iterate over our "child devices"?
307 	 */
308 
309 
310 	ida_free(ida);
311 	return (error);
312 }
313 
314 static void
315 ida_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
316 {
317 	struct ida_hardware_qcb *hwqcb = (struct ida_hardware_qcb *)arg;
318 	int i;
319 
320 	hwqcb->hdr.size = (sizeof(struct ida_req) +
321 	    sizeof(struct ida_sgb) * IDA_NSEG) >> 2;
322 
323 	for (i = 0; i < nsegments; i++) {
324 		hwqcb->seg[i].addr = segs[i].ds_addr;
325 		hwqcb->seg[i].length = segs[i].ds_len;
326 	}
327 	hwqcb->req.sgcount = nsegments;
328 }
329 
330 int
331 ida_command(struct ida_softc *ida, int command, void *data, int datasize,
332 	int drive, u_int32_t pblkno, int flags)
333 {
334 	struct ida_hardware_qcb *hwqcb;
335 	struct ida_qcb *qcb;
336 	bus_dmasync_op_t op;
337 	int s, error;
338 
339 	s = splbio();
340 	qcb = ida_get_qcb(ida);
341 	splx(s);
342 
343 	if (qcb == NULL) {
344 		printf("ida_command: out of QCBs");
345 		return (EAGAIN);
346 	}
347 
348 	hwqcb = qcb->hwqcb;
349 	bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req));
350 
351 	bus_dmamap_load(ida->buffer_dmat, qcb->dmamap,
352 	    (void *)data, datasize, ida_setup_dmamap, hwqcb, 0);
353 	op = qcb->flags & DMA_DATA_IN ?
354 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE;
355 	bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
356 
357 	hwqcb->hdr.drive = drive;
358 	hwqcb->req.blkno = pblkno;
359 	hwqcb->req.bcount = howmany(datasize, DEV_BSIZE);
360 	hwqcb->req.command = command;
361 
362 	qcb->flags = flags | IDA_COMMAND;
363 
364 	s = splbio();
365 	STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe);
366 	ida_start(ida);
367 	error = ida_wait(ida, qcb);
368 	splx(s);
369 
370 	/* XXX should have status returned here? */
371 	/* XXX have "status pointer" area in QCB? */
372 
373 	return (error);
374 }
375 
376 void
377 ida_submit_buf(struct ida_softc *ida, struct buf *bp)
378 {
379         bufq_insert_tail(&ida->buf_queue, bp);
380         ida_construct_qcb(ida);
381 	ida_start(ida);
382 }
383 
384 static void
385 ida_construct_qcb(struct ida_softc *ida)
386 {
387 	struct ida_hardware_qcb *hwqcb;
388 	struct ida_qcb *qcb;
389 	bus_dmasync_op_t op;
390 	struct buf *bp;
391 
392 	bp = bufq_first(&ida->buf_queue);
393 	if (bp == NULL)
394 		return;				/* no more buffers */
395 
396 	qcb = ida_get_qcb(ida);
397 	if (qcb == NULL)
398 		return;				/* out of resources */
399 
400 	bufq_remove(&ida->buf_queue, bp);
401 	qcb->buf = bp;
402 	qcb->flags = 0;
403 
404 	hwqcb = qcb->hwqcb;
405 	bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req));
406 
407 	bus_dmamap_load(ida->buffer_dmat, qcb->dmamap,
408 	    (void *)bp->b_data, bp->b_bcount, ida_setup_dmamap, hwqcb, 0);
409 	op = qcb->flags & DMA_DATA_IN ?
410 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE;
411 	bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
412 
413 	{
414 		struct idad_softc *drv = (struct idad_softc *)bp->b_driver1;
415 		hwqcb->hdr.drive = drv->drive;
416 	}
417 
418 	hwqcb->req.blkno = bp->b_pblkno;
419 	hwqcb->req.bcount = howmany(bp->b_bcount, DEV_BSIZE);
420 	hwqcb->req.command = bp->b_flags & B_READ ? CMD_READ : CMD_WRITE;
421 
422 	STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe);
423 }
424 
425 /*
426  * This routine will be called from ida_intr in order to queue up more
427  * I/O, meaning that we may be in an interrupt context.  Hence, we should
428  * not muck around with spl() in this routine.
429  */
430 static void
431 ida_start(struct ida_softc *ida)
432 {
433 	struct ida_qcb *qcb;
434 
435 	while ((qcb = STAILQ_FIRST(&ida->qcb_queue)) != NULL) {
436 		if (ida->cmd.fifo_full(ida))
437 			break;
438 		STAILQ_REMOVE_HEAD(&ida->qcb_queue, link.stqe);
439 		/*
440 		 * XXX
441 		 * place the qcb on an active list and set a timeout?
442 		 */
443 		qcb->state = QCB_ACTIVE;
444 		ida->cmd.submit(ida, qcb);
445 	}
446 }
447 
448 static int
449 ida_wait(struct ida_softc *ida, struct ida_qcb *qcb)
450 {
451 	struct ida_qcb *qcb_done = NULL;
452 	bus_addr_t completed;
453 	int delay;
454 
455 	if (ida->flags & IDA_INTERRUPTS) {
456 		if (tsleep((caddr_t)qcb, 0, "idacmd", 5 * hz))
457 			return (ETIMEDOUT);
458 		return (0);
459 	}
460 
461 again:
462 	delay = 5 * 1000 * 100;			/* 5 sec delay */
463 	while ((completed = ida->cmd.done(ida)) == 0) {
464 		if (delay-- == 0)
465 			return (ETIMEDOUT);
466 		DELAY(10);
467 	}
468 
469 	qcb_done = idahwqcbptov(ida, completed & ~3);
470 	if (qcb_done != qcb)
471 		goto again;
472 	ida_done(ida, qcb);
473 	return (0);
474 }
475 
476 void
477 ida_intr(void *data)
478 {
479 	struct ida_softc *ida;
480 	struct ida_qcb *qcb;
481 	bus_addr_t completed;
482 
483 	ida = (struct ida_softc *)data;
484 
485 	if (ida->cmd.int_pending(ida) == 0)
486 		return;				/* not our interrupt */
487 
488 	while ((completed = ida->cmd.done(ida)) != 0) {
489 		qcb = idahwqcbptov(ida, completed & ~3);
490 
491 		if (qcb == NULL || qcb->state != QCB_ACTIVE) {
492 			device_printf(ida->dev,
493 			    "ignoring completion %x\n", completed);
494 			continue;
495 		}
496 		ida_done(ida, qcb);
497 	}
498 	ida_start(ida);
499 }
500 
501 /*
502  * should switch out command type; may be status, not just I/O.
503  */
504 static void
505 ida_done(struct ida_softc *ida, struct ida_qcb *qcb)
506 {
507 	int error = 0;
508 
509 	/*
510 	 * finish up command
511 	 */
512 	if (qcb->flags & DMA_DATA_TRANSFER) {
513 		bus_dmasync_op_t op;
514 
515 		op = qcb->flags & DMA_DATA_IN ?
516 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE;
517 		bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
518 		bus_dmamap_unload(ida->buffer_dmat, qcb->dmamap);
519 	}
520 
521 	if (qcb->hwqcb->req.error & SOFT_ERROR)
522 		device_printf(ida->dev, "soft error\n");
523 	if (qcb->hwqcb->req.error & HARD_ERROR) {
524 		error = 1;
525 		device_printf(ida->dev, "hard error\n");
526 	}
527 	if (qcb->hwqcb->req.error & CMD_REJECTED) {
528 		error = 1;
529 		device_printf(ida->dev, "invalid request\n");
530 	}
531 
532 	if (qcb->flags & IDA_COMMAND) {
533 		if (ida->flags & IDA_INTERRUPTS)
534 			wakeup(qcb);
535 	} else {
536 		if (error)
537 			qcb->buf->b_flags |= B_ERROR;
538 		idad_intr(qcb->buf);
539 	}
540 
541 	qcb->state = QCB_FREE;
542 	SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle);
543 	ida_construct_qcb(ida);
544 }
545