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