xref: /freebsd/sys/dev/ata/ata-all.c (revision 7afc53b8dfcc7d5897920ce6cc7e842fbb4ab813)
1 /*-
2  * Copyright (c) 1998 - 2005 S�ren Schmidt <sos@FreeBSD.org>
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  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_ata.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/ata.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/endian.h>
39 #include <sys/ctype.h>
40 #include <sys/conf.h>
41 #include <sys/bus.h>
42 #include <sys/bio.h>
43 #include <sys/malloc.h>
44 #include <sys/sysctl.h>
45 #include <sys/sema.h>
46 #include <sys/taskqueue.h>
47 #include <vm/uma.h>
48 #include <machine/stdarg.h>
49 #include <machine/resource.h>
50 #include <machine/bus.h>
51 #include <sys/rman.h>
52 #ifdef __alpha__
53 #include <machine/md_var.h>
54 #endif
55 #include <dev/ata/ata-all.h>
56 #include <ata_if.h>
57 
58 /* device structure */
59 static  d_ioctl_t       ata_ioctl;
60 static struct cdevsw ata_cdevsw = {
61 	.d_version =    D_VERSION,
62 	.d_flags =      D_NEEDGIANT, /* we need this as newbus isn't mpsafe */
63 	.d_ioctl =      ata_ioctl,
64 	.d_name =       "ata",
65 };
66 
67 /* prototypes */
68 static void ata_interrupt(void *);
69 static void ata_boot_attach(void);
70 static device_t ata_add_child(device_t, struct ata_device *, int);
71 static void bswap(int8_t *, int);
72 static void btrim(int8_t *, int);
73 static void bpack(int8_t *, int8_t *, int);
74 
75 /* global vars */
76 MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
77 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
78 devclass_t ata_devclass;
79 uma_zone_t ata_request_zone;
80 uma_zone_t ata_composite_zone;
81 int ata_wc = 1;
82 
83 /* local vars */
84 static struct intr_config_hook *ata_delayed_attach = NULL;
85 static int ata_dma = 1;
86 static int atapi_dma = 1;
87 
88 /* sysctl vars */
89 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
90 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
91 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
92 	   "ATA disk DMA mode control");
93 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
94 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
95 	   "ATAPI device DMA mode control");
96 TUNABLE_INT("hw.ata.wc", &ata_wc);
97 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
98 	   "ATA disk write caching");
99 
100 /*
101  * newbus device interface related functions
102  */
103 int
104 ata_probe(device_t dev)
105 {
106     return 0;
107 }
108 
109 int
110 ata_attach(device_t dev)
111 {
112     struct ata_channel *ch = device_get_softc(dev);
113     int error, rid;
114 
115     /* check that we have a virgin channel to attach */
116     if (ch->r_irq)
117 	return EEXIST;
118 
119     /* initialize the softc basics */
120     ch->dev = dev;
121     ch->state = ATA_IDLE;
122     bzero(&ch->state_mtx, sizeof(struct mtx));
123     mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
124     bzero(&ch->queue_mtx, sizeof(struct mtx));
125     mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
126     TAILQ_INIT(&ch->ata_queue);
127 
128     /* reset the controller HW, the channel and device(s) */
129     while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
130 	tsleep(&error, PRIBIO, "ataatch", 1);
131     ATA_RESET(dev);
132     ATA_LOCKING(dev, ATA_LF_UNLOCK);
133 
134     /* setup interrupt delivery */
135     rid = ATA_IRQ_RID;
136     ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
137 				       RF_SHAREABLE | RF_ACTIVE);
138     if (!ch->r_irq) {
139 	device_printf(dev, "unable to allocate interrupt\n");
140 	return ENXIO;
141     }
142     if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS,
143 				ata_interrupt, ch, &ch->ih))) {
144 	device_printf(dev, "unable to setup interrupt\n");
145 	return error;
146     }
147 
148     /* probe and attach devices on this channel unless we are in early boot */
149     if (!ata_delayed_attach)
150 	ata_identify(dev);
151     return 0;
152 }
153 
154 int
155 ata_detach(device_t dev)
156 {
157     struct ata_channel *ch = device_get_softc(dev);
158     device_t *children;
159     int nchildren, i;
160 
161     /* check that we have a vaild channel to detach */
162     if (!ch->r_irq)
163 	return ENXIO;
164 
165     /* detach & delete all children */
166     if (!device_get_children(dev, &children, &nchildren)) {
167 	for (i = 0; i < nchildren; i++)
168 	    if (children[i])
169 		device_delete_child(dev, children[i]);
170 	free(children, M_TEMP);
171     }
172 
173     /* release resources */
174     bus_teardown_intr(dev, ch->r_irq, ch->ih);
175     bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
176     ch->r_irq = NULL;
177     mtx_destroy(&ch->state_mtx);
178     mtx_destroy(&ch->queue_mtx);
179     return 0;
180 }
181 
182 int
183 ata_reinit(device_t dev)
184 {
185     struct ata_channel *ch = device_get_softc(dev);
186     device_t *children;
187     int nchildren, i;
188 
189     /* check that we have a vaild channel to reinit */
190     if (!ch || !ch->r_irq)
191 	return ENXIO;
192 
193     if (bootverbose)
194 	device_printf(dev, "reiniting channel ..\n");
195 
196     /* poll for locking the channel */
197     while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
198 	tsleep(&dev, PRIBIO, "atarini", 1);
199 
200     /* unconditionally grap the channel lock */
201     mtx_lock(&ch->state_mtx);
202     ch->state = ATA_STALL_QUEUE;
203     mtx_unlock(&ch->state_mtx);
204 
205     /* reset the controller HW, the channel and device(s) */
206     ATA_RESET(dev);
207 
208     /* reinit the children and delete any that fails */
209     if (!device_get_children(dev, &children, &nchildren)) {
210 	mtx_lock(&Giant);       /* newbus suckage it needs Giant */
211 	for (i = 0; i < nchildren; i++) {
212 	    if (children[i] && device_is_attached(children[i]))
213 		if (ATA_REINIT(children[i])) {
214 		    /*
215 		     * if we have a running request and its device matches
216 		     * this child we need to inform the request that the
217 		     * device is gone and remove it from ch->running
218 		     */
219 		    if (ch->running && ch->running->dev == children[i]) {
220 			device_printf(ch->running->dev,
221 				      "FAILURE - device detached\n");
222 			ch->running->dev = NULL;
223 			ch->running = NULL;
224 		    }
225 		    device_delete_child(dev, children[i]);
226 		}
227 	}
228 	free(children, M_TEMP);
229 	mtx_unlock(&Giant);     /* newbus suckage dealt with, release Giant */
230     }
231 
232     /* catch request in ch->running if we havn't already */
233     ata_catch_inflight(dev);
234 
235     /* we're done release the channel for new work */
236     mtx_lock(&ch->state_mtx);
237     ch->state = ATA_IDLE;
238     mtx_unlock(&ch->state_mtx);
239     ATA_LOCKING(dev, ATA_LF_UNLOCK);
240 
241     if (bootverbose)
242 	device_printf(dev, "reinit done ..\n");
243 
244     /* kick off requests on the queue */
245     ata_start(dev);
246     return 0;
247 }
248 
249 int
250 ata_suspend(device_t dev)
251 {
252     struct ata_channel *ch;
253 
254     /* check for valid device */
255     if (!dev || !(ch = device_get_softc(dev)))
256 	return ENXIO;
257 
258     /* wait for the channel to be IDLE before entering suspend mode */
259     while (1) {
260 	mtx_lock(&ch->state_mtx);
261 	if (ch->state == ATA_IDLE) {
262 	    ch->state = ATA_ACTIVE;
263 	    mtx_unlock(&ch->state_mtx);
264 	    break;
265 	}
266 	mtx_unlock(&ch->state_mtx);
267 	tsleep(ch, PRIBIO, "atasusp", hz/10);
268     }
269     ATA_LOCKING(dev, ATA_LF_UNLOCK);
270     return 0;
271 }
272 
273 int
274 ata_resume(device_t dev)
275 {
276     struct ata_channel *ch;
277     int error;
278 
279     /* check for valid device */
280     if (!dev || !(ch = device_get_softc(dev)))
281 	return ENXIO;
282 
283     /* reinit the devices, we dont know what mode/state they are in */
284     error = ata_reinit(dev);
285 
286     /* kick off requests on the queue */
287     ata_start(dev);
288     return error;
289 }
290 
291 static void
292 ata_interrupt(void *data)
293 {
294     struct ata_channel *ch = (struct ata_channel *)data;
295     struct ata_request *request;
296 
297     mtx_lock(&ch->state_mtx);
298     do {
299 	/* do we have a running request */
300 	if (!(request = ch->running) || (request->flags & ATA_R_TIMEOUT))
301 	    break;
302 
303 	ATA_DEBUG_RQ(request, "interrupt");
304 
305 	/* ignore interrupt if device is busy */
306 	if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
307 	    DELAY(100);
308 	    if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
309 		break;
310 	}
311 
312 	/* check for the right state */
313 	if (ch->state != ATA_ACTIVE && ch->state != ATA_STALL_QUEUE) {
314 	    device_printf(request->dev,
315 			  "interrupt state=%d unexpected\n", ch->state);
316 	    break;
317 	}
318 
319 	/*
320 	 * we have the HW locks, so end the tranaction for this request
321 	 * if it finishes immediately otherwise wait for next interrupt
322 	 */
323 	if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
324 	    ch->running = NULL;
325 	    if (ch->state == ATA_ACTIVE)
326 		ch->state = ATA_IDLE;
327 	    mtx_unlock(&ch->state_mtx);
328 	    ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
329 	    ata_finish(request);
330 	    return;
331 	}
332     } while (0);
333     mtx_unlock(&ch->state_mtx);
334 }
335 
336 /*
337  * device related interfaces
338  */
339 static int
340 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
341 	  int32_t flag, struct thread *td)
342 {
343     device_t device, *children;
344     struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
345     int *value = (int *)data;
346     int i, nchildren, error = ENOTTY;
347 
348     switch (cmd) {
349     case IOCATAGMAXCHANNEL:
350 	*value = devclass_get_maxunit(ata_devclass);
351 	error = 0;
352 	break;
353 
354     case IOCATAREINIT:
355 	if (*value > devclass_get_maxunit(ata_devclass) ||
356 	    !(device = devclass_get_device(ata_devclass, *value)))
357 	    return ENXIO;
358 	error = ata_reinit(device);
359 	ata_start(device);
360 	break;
361 
362     case IOCATAATTACH:
363 	if (*value > devclass_get_maxunit(ata_devclass) ||
364 	    !(device = devclass_get_device(ata_devclass, *value)))
365 	    return ENXIO;
366 	/* XXX SOS should enable channel HW on controller */
367 	error = ata_attach(device);
368 	break;
369 
370     case IOCATADETACH:
371 	if (*value > devclass_get_maxunit(ata_devclass) ||
372 	    !(device = devclass_get_device(ata_devclass, *value)))
373 	    return ENXIO;
374 	error = ata_detach(device);
375 	/* XXX SOS should disable channel HW on controller */
376 	break;
377 
378     case IOCATADEVICES:
379 	if (devices->channel > devclass_get_maxunit(ata_devclass) ||
380 	    !(device = devclass_get_device(ata_devclass, devices->channel)))
381 	    return ENXIO;
382 	bzero(devices->name[0], 32);
383 	bzero(&devices->params[0], sizeof(struct ata_params));
384 	bzero(devices->name[1], 32);
385 	bzero(&devices->params[1], sizeof(struct ata_params));
386 	if (!device_get_children(device, &children, &nchildren)) {
387 	    for (i = 0; i < nchildren; i++) {
388 		if (children[i] && device_is_attached(children[i])) {
389 		    struct ata_device *atadev = device_get_softc(children[i]);
390 
391 		    if (atadev->unit == ATA_MASTER) {
392 			strncpy(devices->name[0],
393 				device_get_nameunit(children[i]), 32);
394 			bcopy(&atadev->param, &devices->params[0],
395 			      sizeof(struct ata_params));
396 		    }
397 		    if (atadev->unit == ATA_SLAVE) {
398 			strncpy(devices->name[1],
399 				device_get_nameunit(children[i]), 32);
400 			bcopy(&atadev->param, &devices->params[1],
401 			      sizeof(struct ata_params));
402 		    }
403 		}
404 	    }
405 	    free(children, M_TEMP);
406 	    error = 0;
407 	}
408 	else
409 	    error = ENODEV;
410 	break;
411 
412     default:
413 	if (ata_raid_ioctl_func)
414 	    error = ata_raid_ioctl_func(cmd, data);
415     }
416     return error;
417 }
418 
419 int
420 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
421 {
422     struct ata_device *atadev = device_get_softc(dev);
423     struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
424     struct ata_params *params = (struct ata_params *)data;
425     int *mode = (int *)data;
426     struct ata_request *request;
427     caddr_t buf;
428     int error;
429 
430     switch (cmd) {
431     case IOCATAREQUEST:
432 	if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
433 	    return ENOMEM;
434 	}
435 	if (!(request = ata_alloc_request())) {
436 	    free(buf, M_ATA);
437 	    return  ENOMEM;
438 	}
439 	if (ioc_request->flags & ATA_CMD_WRITE) {
440 	    error = copyin(ioc_request->data, buf, ioc_request->count);
441 	    if (error) {
442 		free(buf, M_ATA);
443 		ata_free_request(request);
444 		return error;
445 	    }
446 	}
447 	request->dev = dev;
448 	if (ioc_request->flags & ATA_CMD_ATAPI) {
449 	    request->flags = ATA_R_ATAPI;
450 	    bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
451 	}
452 	else {
453 	    request->u.ata.command = ioc_request->u.ata.command;
454 	    request->u.ata.feature = ioc_request->u.ata.feature;
455 	    request->u.ata.lba = ioc_request->u.ata.lba;
456 	    request->u.ata.count = ioc_request->u.ata.count;
457 	}
458 	request->timeout = ioc_request->timeout;
459 	request->data = buf;
460 	request->bytecount = ioc_request->count;
461 	request->transfersize = request->bytecount;
462 	if (ioc_request->flags & ATA_CMD_CONTROL)
463 	    request->flags |= ATA_R_CONTROL;
464 	if (ioc_request->flags & ATA_CMD_READ)
465 	    request->flags |= ATA_R_READ;
466 	if (ioc_request->flags & ATA_CMD_WRITE)
467 	    request->flags |= ATA_R_WRITE;
468 	ata_queue_request(request);
469 	if (!(request->flags & ATA_R_ATAPI)) {
470 	    ioc_request->u.ata.command = request->u.ata.command;
471 	    ioc_request->u.ata.feature = request->u.ata.feature;
472 	    ioc_request->u.ata.lba = request->u.ata.lba;
473 	    ioc_request->u.ata.count = request->u.ata.count;
474 	}
475 	ioc_request->error = request->result;
476 	if (ioc_request->flags & ATA_CMD_READ)
477 	    error = copyout(buf, ioc_request->data, ioc_request->count);
478 	else
479 	    error = 0;
480 	free(buf, M_ATA);
481 	ata_free_request(request);
482 	return error;
483 
484     case IOCATAGPARM:
485 	bcopy(&atadev->param, params, sizeof(struct ata_params));
486 	return 0;
487 
488     case IOCATASMODE:
489 	atadev->mode = *mode;
490 	ATA_SETMODE(device_get_parent(dev), dev);
491 	return 0;
492 
493     case IOCATAGMODE:
494 	*mode = atadev->mode;
495 	return 0;
496     default:
497 	return ENOTTY;
498     }
499 }
500 
501 static void
502 ata_boot_attach(void)
503 {
504     struct ata_channel *ch;
505     int ctlr;
506 
507     /* release the hook that got us here, only needed during boot */
508     if (ata_delayed_attach) {
509 	config_intrhook_disestablish(ata_delayed_attach);
510 	free(ata_delayed_attach, M_TEMP);
511 	ata_delayed_attach = NULL;
512     }
513 
514     /* kick of probe and attach on all channels */
515     for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
516 	if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
517 	    ata_identify(ch->dev);
518 	}
519     }
520 }
521 
522 
523 /*
524  * misc support functions
525  */
526 static device_t
527 ata_add_child(device_t parent, struct ata_device *atadev, int unit)
528 {
529     device_t child;
530 
531     if ((child = device_add_child(parent, NULL, unit))) {
532 	device_set_softc(child, atadev);
533 	device_quiet(child);
534 	atadev->dev = child;
535 	atadev->max_iosize = DEV_BSIZE;
536 	atadev->mode = ATA_PIO_MAX;
537     }
538     return child;
539 }
540 
541 static int
542 ata_getparam(device_t parent, struct ata_device *atadev)
543 {
544     struct ata_channel *ch = device_get_softc(parent);
545     struct ata_request *request;
546     u_int8_t command = 0;
547     int error = ENOMEM, retries = 2;
548 
549     if (ch->devices &
550 	(atadev->unit == ATA_MASTER ? ATA_ATA_MASTER : ATA_ATA_SLAVE))
551 	command = ATA_ATA_IDENTIFY;
552     if (ch->devices &
553 	(atadev->unit == ATA_MASTER ? ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE))
554 	command = ATA_ATAPI_IDENTIFY;
555     if (!command)
556 	return ENXIO;
557 
558     while (retries-- > 0 && error) {
559 	if (!(request = ata_alloc_request()))
560 	    break;
561 	request->dev = atadev->dev;
562 	request->timeout = 1;
563 	request->retries = 0;
564 	request->u.ata.command = command;
565 	request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT|ATA_R_QUIET);
566 	request->data = (void *)&atadev->param;
567 	request->bytecount = sizeof(struct ata_params);
568 	request->donecount = 0;
569 	request->transfersize = DEV_BSIZE;
570 	ata_queue_request(request);
571 	error = request->result;
572 	ata_free_request(request);
573     }
574 
575     if (!error && (isprint(atadev->param.model[0]) ||
576 		   isprint(atadev->param.model[1]))) {
577 	struct ata_params *atacap = &atadev->param;
578 	char buffer[64];
579 #if BYTE_ORDER == BIG_ENDIAN
580 	int16_t *ptr;
581 
582 	for (ptr = (int16_t *)atacap;
583 	     ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
584 	    *ptr = bswap16(*ptr);
585 	}
586 #endif
587 	if (!(!strncmp(atacap->model, "FX", 2) ||
588 	      !strncmp(atacap->model, "NEC", 3) ||
589 	      !strncmp(atacap->model, "Pioneer", 7) ||
590 	      !strncmp(atacap->model, "SHARP", 5))) {
591 	    bswap(atacap->model, sizeof(atacap->model));
592 	    bswap(atacap->revision, sizeof(atacap->revision));
593 	    bswap(atacap->serial, sizeof(atacap->serial));
594 	}
595 	btrim(atacap->model, sizeof(atacap->model));
596 	bpack(atacap->model, atacap->model, sizeof(atacap->model));
597 	btrim(atacap->revision, sizeof(atacap->revision));
598 	bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
599 	btrim(atacap->serial, sizeof(atacap->serial));
600 	bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
601 	sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
602 	device_set_desc_copy(atadev->dev, buffer);
603 	if (bootverbose)
604 	    printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
605 		   ch->unit, atadev->unit == ATA_MASTER ? "master":"slave",
606 		   ata_mode2str(ata_pmode(atacap)),
607 		   ata_mode2str(ata_wmode(atacap)),
608 		   ata_mode2str(ata_umode(atacap)),
609 		   (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
610 
611 	if (atadev->param.config & ATA_PROTO_ATAPI) {
612 	    if (atapi_dma && ch->dma &&
613 		(atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
614 		ata_umode(&atadev->param) >= ATA_UDMA2)
615 		atadev->mode = ATA_DMA_MAX;
616 	}
617 	else {
618 	    if (ata_dma && ch->dma)
619 		atadev->mode = ATA_DMA_MAX;
620 	}
621     }
622     else {
623 	if (!error)
624 	    error = ENXIO;
625     }
626     return error;
627 }
628 
629 int
630 ata_identify(device_t dev)
631 {
632     struct ata_channel *ch = device_get_softc(dev);
633     struct ata_device *master = NULL, *slave = NULL;
634     device_t master_child = NULL, slave_child = NULL;
635     int master_unit = -1, slave_unit = -1;
636 
637     if (ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) {
638 	if (!(master = malloc(sizeof(struct ata_device),
639 			      M_ATA, M_NOWAIT | M_ZERO))) {
640 	    device_printf(dev, "out of memory\n");
641 	    return ENOMEM;
642 	}
643 	master->unit = ATA_MASTER;
644     }
645     if (ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) {
646 	if (!(slave = malloc(sizeof(struct ata_device),
647 			     M_ATA, M_NOWAIT | M_ZERO))) {
648 	    free(master, M_ATA);
649 	    device_printf(dev, "out of memory\n");
650 	    return ENOMEM;
651 	}
652 	slave->unit = ATA_SLAVE;
653     }
654 
655 #ifdef ATA_STATIC_ID
656     if (ch->devices & ATA_ATA_MASTER)
657 	master_unit = (device_get_unit(dev) << 1);
658 #endif
659     if (master && !(master_child = ata_add_child(dev, master, master_unit))) {
660 	free(master, M_ATA);
661 	master = NULL;
662     }
663 #ifdef ATA_STATIC_ID
664     if (ch->devices & ATA_ATA_SLAVE)
665 	slave_unit = (device_get_unit(dev) << 1) + 1;
666 #endif
667     if (slave && !(slave_child = ata_add_child(dev, slave, slave_unit))) {
668 	free(slave, M_ATA);
669 	slave = NULL;
670     }
671 
672     if (slave && ata_getparam(dev, slave)) {
673 	device_delete_child(dev, slave_child);
674 	free(slave, M_ATA);
675     }
676     if (master && ata_getparam(dev, master)) {
677 	device_delete_child(dev, master_child);
678 	free(master, M_ATA);
679     }
680 
681     bus_generic_probe(dev);
682     bus_generic_attach(dev);
683     return 0;
684 }
685 
686 void
687 ata_default_registers(device_t dev)
688 {
689     struct ata_channel *ch = device_get_softc(dev);
690 
691     /* fill in the defaults from whats setup already */
692     ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
693     ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
694     ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
695     ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
696     ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
697     ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
698     ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
699     ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
700 }
701 
702 u_int8_t
703 ata_modify_if_48bit(struct ata_request *request)
704 {
705     struct ata_device *atadev = device_get_softc(request->dev);
706     u_int8_t command = request->u.ata.command;
707 
708     atadev->flags &= ~ATA_D_48BIT_ACTIVE;
709 
710     if ((request->u.ata.lba >= ATA_MAX_28BIT_LBA ||
711 	 request->u.ata.count > 256) &&
712 	atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
713 
714 	/* translate command into 48bit version */
715 	switch (command) {
716 	case ATA_READ:
717 	    command = ATA_READ48; break;
718 	case ATA_READ_MUL:
719 	    command = ATA_READ_MUL48; break;
720 	case ATA_READ_DMA:
721 	    command = ATA_READ_DMA48; break;
722 	case ATA_READ_DMA_QUEUED:
723 	    command = ATA_READ_DMA_QUEUED48; break;
724 	case ATA_WRITE:
725 	    command = ATA_WRITE48; break;
726 	case ATA_WRITE_MUL:
727 	    command = ATA_WRITE_MUL48; break;
728 	case ATA_WRITE_DMA:
729 	    command = ATA_WRITE_DMA48; break;
730 	case ATA_WRITE_DMA_QUEUED:
731 	    command = ATA_WRITE_DMA_QUEUED48; break;
732 	case ATA_FLUSHCACHE:
733 	    command = ATA_FLUSHCACHE48; break;
734 	default:
735 	    return command;
736 	}
737 	atadev->flags |= ATA_D_48BIT_ACTIVE;
738     }
739     return command;
740 }
741 
742 void
743 ata_udelay(int interval)
744 {
745     /* for now just use DELAY, the timer/sleep subsytems are not there yet */
746     if (1 || interval < (1000000/hz) || ata_delayed_attach)
747 	DELAY(interval);
748     else
749 	tsleep(&interval, PRIBIO, "ataslp", interval/(1000000/hz));
750 }
751 
752 char *
753 ata_mode2str(int mode)
754 {
755     switch (mode) {
756     case ATA_PIO0: return "PIO0";
757     case ATA_PIO1: return "PIO1";
758     case ATA_PIO2: return "PIO2";
759     case ATA_PIO3: return "PIO3";
760     case ATA_PIO4: return "PIO4";
761     case ATA_WDMA0: return "WDMA0";
762     case ATA_WDMA1: return "WDMA1";
763     case ATA_WDMA2: return "WDMA2";
764     case ATA_UDMA0: return "UDMA16";
765     case ATA_UDMA1: return "UDMA25";
766     case ATA_UDMA2: return "UDMA33";
767     case ATA_UDMA3: return "UDMA40";
768     case ATA_UDMA4: return "UDMA66";
769     case ATA_UDMA5: return "UDMA100";
770     case ATA_UDMA6: return "UDMA133";
771     case ATA_SA150: return "SATA150";
772     default:
773 	if (mode & ATA_DMA_MASK)
774 	    return "BIOSDMA";
775 	else
776 	    return "BIOSPIO";
777     }
778 }
779 
780 int
781 ata_pmode(struct ata_params *ap)
782 {
783     if (ap->atavalid & ATA_FLAG_64_70) {
784 	if (ap->apiomodes & 0x02)
785 	    return ATA_PIO4;
786 	if (ap->apiomodes & 0x01)
787 	    return ATA_PIO3;
788     }
789     if (ap->mwdmamodes & 0x04)
790 	return ATA_PIO4;
791     if (ap->mwdmamodes & 0x02)
792 	return ATA_PIO3;
793     if (ap->mwdmamodes & 0x01)
794 	return ATA_PIO2;
795     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
796 	return ATA_PIO2;
797     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
798 	return ATA_PIO1;
799     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
800 	return ATA_PIO0;
801     return ATA_PIO0;
802 }
803 
804 int
805 ata_wmode(struct ata_params *ap)
806 {
807     if (ap->mwdmamodes & 0x04)
808 	return ATA_WDMA2;
809     if (ap->mwdmamodes & 0x02)
810 	return ATA_WDMA1;
811     if (ap->mwdmamodes & 0x01)
812 	return ATA_WDMA0;
813     return -1;
814 }
815 
816 int
817 ata_umode(struct ata_params *ap)
818 {
819     if (ap->atavalid & ATA_FLAG_88) {
820 	if (ap->udmamodes & 0x40)
821 	    return ATA_UDMA6;
822 	if (ap->udmamodes & 0x20)
823 	    return ATA_UDMA5;
824 	if (ap->udmamodes & 0x10)
825 	    return ATA_UDMA4;
826 	if (ap->udmamodes & 0x08)
827 	    return ATA_UDMA3;
828 	if (ap->udmamodes & 0x04)
829 	    return ATA_UDMA2;
830 	if (ap->udmamodes & 0x02)
831 	    return ATA_UDMA1;
832 	if (ap->udmamodes & 0x01)
833 	    return ATA_UDMA0;
834     }
835     return -1;
836 }
837 
838 int
839 ata_limit_mode(device_t dev, int mode, int maxmode)
840 {
841     struct ata_device *atadev = device_get_softc(dev);
842 
843     if (maxmode && mode > maxmode)
844 	mode = maxmode;
845 
846     if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
847 	return min(mode, ata_umode(&atadev->param));
848 
849     if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
850 	return min(mode, ata_wmode(&atadev->param));
851 
852     if (mode > ata_pmode(&atadev->param))
853 	return min(mode, ata_pmode(&atadev->param));
854 
855     return mode;
856 }
857 
858 static void
859 bswap(int8_t *buf, int len)
860 {
861     u_int16_t *ptr = (u_int16_t*)(buf + len);
862 
863     while (--ptr >= (u_int16_t*)buf)
864 	*ptr = ntohs(*ptr);
865 }
866 
867 static void
868 btrim(int8_t *buf, int len)
869 {
870     int8_t *ptr;
871 
872     for (ptr = buf; ptr < buf+len; ++ptr)
873 	if (!*ptr || *ptr == '_')
874 	    *ptr = ' ';
875     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
876 	*ptr = 0;
877 }
878 
879 static void
880 bpack(int8_t *src, int8_t *dst, int len)
881 {
882     int i, j, blank;
883 
884     for (i = j = blank = 0 ; i < len; i++) {
885 	if (blank && src[i] == ' ') continue;
886 	if (blank && src[i] != ' ') {
887 	    dst[j++] = src[i];
888 	    blank = 0;
889 	    continue;
890 	}
891 	if (src[i] == ' ') {
892 	    blank = 1;
893 	    if (i == 0)
894 		continue;
895 	}
896 	dst[j++] = src[i];
897     }
898     if (j < len)
899 	dst[j] = 0x00;
900 }
901 
902 
903 /*
904  * module handeling
905  */
906 static int
907 ata_module_event_handler(module_t mod, int what, void *arg)
908 {
909     static struct cdev *atacdev;
910 
911     switch (what) {
912     case MOD_LOAD:
913 	/* register controlling device */
914 	atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
915 
916 	if (cold) {
917 	    /* register boot attach to be run when interrupts are enabled */
918 	    if (!(ata_delayed_attach = (struct intr_config_hook *)
919 				       malloc(sizeof(struct intr_config_hook),
920 					      M_TEMP, M_NOWAIT | M_ZERO))) {
921 		printf("ata: malloc of delayed attach hook failed\n");
922 		return EIO;
923 	    }
924 	    ata_delayed_attach->ich_func = (void*)ata_boot_attach;
925 	    if (config_intrhook_establish(ata_delayed_attach) != 0) {
926 		printf("ata: config_intrhook_establish failed\n");
927 		free(ata_delayed_attach, M_TEMP);
928 	    }
929 	}
930 	return 0;
931 
932     case MOD_UNLOAD:
933 	/* deregister controlling device */
934 	destroy_dev(atacdev);
935 	return 0;
936 
937     default:
938 	return EOPNOTSUPP;
939     }
940 }
941 
942 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
943 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
944 MODULE_VERSION(ata, 1);
945 
946 static void
947 ata_init(void)
948 {
949     ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
950 				   NULL, NULL, NULL, NULL, 0, 0);
951     ata_composite_zone = uma_zcreate("ata_composite",
952 				     sizeof(struct ata_composite),
953 				     NULL, NULL, NULL, NULL, 0, 0);
954 }
955 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
956 
957 static void
958 ata_uninit(void)
959 {
960     uma_zdestroy(ata_composite_zone);
961     uma_zdestroy(ata_request_zone);
962 }
963 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);
964