xref: /dragonfly/sys/dev/disk/nata/ata-all.c (revision 0dace59e)
1 /*-
2  * Copyright (c) 1998 - 2006 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/ata/ata-all.c,v 1.279 2007/02/23 16:25:08 jhb Exp $
27  * $DragonFly: src/sys/dev/disk/nata/ata-all.c,v 1.14 2008/03/24 06:41:56 dillon Exp $
28  */
29 
30 #include "opt_ata.h"
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/callout.h>
35 #include <sys/conf.h>
36 #include <sys/ctype.h>
37 #include <sys/device.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/libkern.h>
41 #include <sys/lock.h>		/* for {get,rel}_mplock() */
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/nata.h>
45 #include <sys/objcache.h>
46 #include <sys/queue.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49 
50 #include <sys/spinlock2.h>
51 #include <sys/mplock2.h>
52 
53 #include "ata-all.h"
54 #include "ata_if.h"
55 
56 /* device structure */
57 static  d_ioctl_t       ata_ioctl;
58 static struct dev_ops ata_ops = {
59 	{ "ata", 159, 0 },
60 	.d_open =	nullopen,
61 	.d_close =	nullclose,
62 	.d_ioctl =	ata_ioctl,
63 };
64 
65 /* prototypes */
66 #if 0
67 static void ata_boot_attach(void);
68 #endif
69 static device_t ata_add_child(device_t, struct ata_device *, int);
70 static int ata_getparam(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 struct objcache *ata_request_cache;
80 struct objcache *ata_composite_cache;
81 struct objcache_malloc_args ata_request_malloc_args = {
82 	sizeof(struct ata_request), M_ATA };
83 struct objcache_malloc_args ata_composite_malloc_args = {
84 	sizeof(struct ata_composite), M_ATA };
85 int ata_wc = 1;
86 
87 /* local vars */
88 static int ata_dma = 1;
89 static int atapi_dma = 1;
90 
91 /* sysctl vars */
92 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
93 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
94 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RW, &ata_dma, 0,
95 	   "ATA disk DMA mode control");
96 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
97 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RW, &atapi_dma, 0,
98 	   "ATAPI device DMA mode control");
99 TUNABLE_INT("hw.ata.wc", &ata_wc);
100 SYSCTL_INT(_hw_ata, OID_AUTO, ata_wc, CTLFLAG_RW, &ata_wc, 0,
101 	   "ATA disk write caching");
102 
103 /*
104  * newbus device interface related functions
105  */
106 int
107 ata_probe(device_t dev)
108 {
109     return 0;
110 }
111 
112 int
113 ata_attach(device_t dev)
114 {
115     struct ata_channel *ch = device_get_softc(dev);
116     int error, rid;
117 
118     /* check that we have a virgin channel to attach */
119     if (ch->r_irq)
120 	return EEXIST;
121 
122     /* initialize the softc basics */
123     ch->dev = dev;
124     ch->state = ATA_IDLE;
125     spin_init(&ch->state_mtx);
126     spin_init(&ch->queue_mtx);
127     ata_queue_init(ch);
128 
129     /* reset the controller HW, the channel and device(s) */
130     while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
131 	tsleep(&error, 0, "ataatch", 1);
132     ATA_RESET(dev);
133     ATA_LOCKING(dev, ATA_LF_UNLOCK);
134 
135     /* setup interrupt delivery */
136     rid = ATA_IRQ_RID;
137     ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
138 				       RF_SHAREABLE | RF_ACTIVE);
139     if (!ch->r_irq) {
140 	device_printf(dev, "unable to allocate interrupt\n");
141 	return ENXIO;
142     }
143     if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS,
144 				(driver_intr_t *)ata_interrupt, ch, &ch->ih,
145 				NULL))) {
146 	device_printf(dev, "unable to setup interrupt\n");
147 	return error;
148     }
149 
150     /* probe and attach devices on this channel unless we are in early boot */
151     ata_identify(dev);
152     return 0;
153 }
154 
155 int
156 ata_detach(device_t dev)
157 {
158     struct ata_channel *ch = device_get_softc(dev);
159     device_t *children;
160     int nchildren, i;
161 
162     /* check that we have a valid channel to detach */
163     if (!ch->r_irq)
164 	return ENXIO;
165 
166     /* grap the channel lock so no new requests gets launched */
167     spin_lock(&ch->state_mtx);
168     ch->state |= ATA_STALL_QUEUE;
169     spin_unlock(&ch->state_mtx);
170 
171     /* detach & delete all children */
172     if (!device_get_children(dev, &children, &nchildren)) {
173 	for (i = 0; i < nchildren; i++)
174 	    if (children[i])
175 		device_delete_child(dev, children[i]);
176 	kfree(children, M_TEMP);
177     }
178 
179     /* release resources */
180     bus_teardown_intr(dev, ch->r_irq, ch->ih);
181     bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
182     ch->r_irq = NULL;
183     spin_uninit(&ch->state_mtx);
184     spin_uninit(&ch->queue_mtx);
185     return 0;
186 }
187 
188 int
189 ata_reinit(device_t dev)
190 {
191     struct ata_channel *ch = device_get_softc(dev);
192     struct ata_request *request;
193     device_t *children;
194     int nchildren, i;
195 
196     /* check that we have a valid channel to reinit */
197     if (!ch || !ch->r_irq)
198 	return ENXIO;
199 
200     if (bootverbose)
201 	device_printf(dev, "reiniting channel ..\n");
202 
203     /* poll for locking the channel */
204     while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
205 	tsleep(&dev, 0, "atarini", 1);
206 
207     /* catch eventual request in ch->running */
208     spin_lock(&ch->state_mtx);
209     if ((request = ch->running))
210 	callout_stop(&request->callout);
211     ch->running = NULL;
212 
213     /* unconditionally grap the channel lock */
214     ch->state |= ATA_STALL_QUEUE;
215     spin_unlock(&ch->state_mtx);
216 
217     /* reset the controller HW, the channel and device(s) */
218     ATA_RESET(dev);
219 
220     /* reinit the children and delete any that fails */
221     if (!device_get_children(dev, &children, &nchildren)) {
222 	get_mplock();
223 	for (i = 0; i < nchildren; i++) {
224 	    /* did any children go missing ? */
225 	    if (children[i] && device_is_attached(children[i]) &&
226 		ATA_REINIT(children[i])) {
227 		/*
228 		 * if we had a running request and its device matches
229 		 * this child we need to inform the request that the
230 		 * device is gone.
231 		 */
232 		if (request && request->dev == children[i]) {
233 		    request->result = ENXIO;
234 		    device_printf(request->dev, "FAILURE - device detached\n");
235 
236 		    /* if not timeout finish request here */
237 		    if (!(request->flags & ATA_R_TIMEOUT))
238 			    ata_finish(request);
239 		    request = NULL;
240 		}
241 		device_delete_child(dev, children[i]);
242 	    }
243 	}
244 	kfree(children, M_TEMP);
245 	rel_mplock();
246     }
247 
248     /* if we still have a good request put it on the queue again */
249     if (request && !(request->flags & ATA_R_TIMEOUT)) {
250 	device_printf(request->dev,
251 		      "WARNING - %s requeued due to channel reset",
252 		      ata_cmd2str(request));
253 	if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
254 	    kprintf(" LBA=%ju", request->u.ata.lba);
255 	kprintf("\n");
256 	request->flags |= ATA_R_REQUEUE;
257 	ata_queue_request(request);
258     }
259 
260     /* we're done release the channel for new work */
261     spin_lock(&ch->state_mtx);
262     ch->state = ATA_IDLE;
263     spin_unlock(&ch->state_mtx);
264     ATA_LOCKING(dev, ATA_LF_UNLOCK);
265 
266     if (bootverbose)
267 	device_printf(dev, "reinit done ..\n");
268 
269     /* kick off requests on the queue */
270     ata_start(dev);
271     return 0;
272 }
273 
274 int
275 ata_suspend(device_t dev)
276 {
277     struct ata_channel *ch;
278 
279     /* check for valid device */
280     if (!dev || !(ch = device_get_softc(dev)))
281 	return ENXIO;
282 
283     /* wait for the channel to be IDLE or detached before suspending */
284     while (ch->r_irq) {
285 	spin_lock(&ch->state_mtx);
286 	if (ch->state == ATA_IDLE) {
287 	    ch->state = ATA_ACTIVE;
288 	    spin_unlock(&ch->state_mtx);
289 	    break;
290 	}
291 	spin_unlock(&ch->state_mtx);
292 	tsleep(ch, 0, "atasusp", hz/10);
293     }
294     ATA_LOCKING(dev, ATA_LF_UNLOCK);
295     return 0;
296 }
297 
298 int
299 ata_resume(device_t dev)
300 {
301     struct ata_channel *ch;
302     int error;
303 
304     /* check for valid device */
305     if (!dev || !(ch = device_get_softc(dev)))
306 	return ENXIO;
307 
308     /* reinit the devices, we dont know what mode/state they are in */
309     error = ata_reinit(dev);
310 
311     /* kick off requests on the queue */
312     ata_start(dev);
313     return error;
314 }
315 
316 int
317 ata_interrupt(void *data)
318 {
319     struct ata_channel *ch = (struct ata_channel *)data;
320     struct ata_request *request;
321 
322     spin_lock(&ch->state_mtx);
323     do {
324 	/*
325 	 * Ignore interrupt if its not for us.  This may also have the
326 	 * side effect of processing events unrelated to I/O requests.
327 	 */
328 	if (ch->hw.status && !ch->hw.status(ch->dev))
329 	    break;
330 
331 	/*
332 	 * Check if we have a running request, and make sure it has been
333 	 * completely queued.  Otherwise the channel status may indicate
334 	 * not-busy when, in fact, the command had not yet been issued.
335 	 */
336 	if ((request = ch->running) == NULL)
337 	    break;
338 	if ((request->flags & ATA_R_HWCMDQUEUED) == 0) {
339 	    kprintf("ata_interrupt: early interrupt\n");
340 	    break;
341 	}
342 
343 	ATA_DEBUG_RQ(request, "interrupt");
344 
345 	/* safetycheck for the right state */
346 	if (ch->state == ATA_IDLE) {
347 	    device_printf(request->dev, "interrupt on idle channel ignored\n");
348 	    break;
349 	}
350 
351 	/*
352 	 * we have the HW locks, so end the transaction for this request
353 	 * if it finishes immediately otherwise wait for next interrupt
354 	 */
355 	if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
356 	    ch->running = NULL;
357 	    if (ch->state == ATA_ACTIVE)
358 		ch->state = ATA_IDLE;
359 	    spin_unlock(&ch->state_mtx);
360 	    ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
361 	    ata_finish(request);
362 	    return 1;
363 	}
364     } while (0);
365     spin_unlock(&ch->state_mtx);
366     return 0;
367 }
368 
369 /*
370  * device related interfaces
371  */
372 static int
373 ata_ioctl(struct dev_ioctl_args *ap)
374 {
375     device_t device, *children;
376     struct ata_ioc_devices *devices = (struct ata_ioc_devices *)ap->a_data;
377     int *value = (int *)ap->a_data;
378     int i, nchildren, error = ENOTTY;
379 
380     switch (ap->a_cmd) {
381     case IOCATAGMAXCHANNEL:
382 	*value = devclass_get_maxunit(ata_devclass);
383 	error = 0;
384 	break;
385 
386     case IOCATAREINIT:
387 	if (*value > devclass_get_maxunit(ata_devclass) ||
388 	    !(device = devclass_get_device(ata_devclass, *value)))
389 	    return ENXIO;
390 	error = ata_reinit(device);
391 	ata_start(device);
392 	break;
393 
394     case IOCATAATTACH:
395 	if (*value > devclass_get_maxunit(ata_devclass) ||
396 	    !(device = devclass_get_device(ata_devclass, *value)))
397 	    return ENXIO;
398 	/* XXX SOS should enable channel HW on controller */
399 	error = ata_attach(device);
400 	break;
401 
402     case IOCATADETACH:
403 	if (*value > devclass_get_maxunit(ata_devclass) ||
404 	    !(device = devclass_get_device(ata_devclass, *value)))
405 	    return ENXIO;
406 	error = ata_detach(device);
407 	/* XXX SOS should disable channel HW on controller */
408 	break;
409 
410     case IOCATADEVICES:
411 	if (devices->channel > devclass_get_maxunit(ata_devclass) ||
412 	    !(device = devclass_get_device(ata_devclass, devices->channel)))
413 	    return ENXIO;
414 	bzero(devices->name[0], 32);
415 	bzero(&devices->params[0], sizeof(struct ata_params));
416 	bzero(devices->name[1], 32);
417 	bzero(&devices->params[1], sizeof(struct ata_params));
418 	if (!device_get_children(device, &children, &nchildren)) {
419 	    for (i = 0; i < nchildren; i++) {
420 		if (children[i] && device_is_attached(children[i])) {
421 		    struct ata_device *atadev = device_get_softc(children[i]);
422 
423 		    if (atadev->unit == ATA_MASTER) {
424 			strncpy(devices->name[0],
425 				device_get_nameunit(children[i]), 32);
426 			bcopy(&atadev->param, &devices->params[0],
427 			      sizeof(struct ata_params));
428 		    }
429 		    if (atadev->unit == ATA_SLAVE) {
430 			strncpy(devices->name[1],
431 				device_get_nameunit(children[i]), 32);
432 			bcopy(&atadev->param, &devices->params[1],
433 			      sizeof(struct ata_params));
434 		    }
435 		}
436 	    }
437 	    kfree(children, M_TEMP);
438 	    error = 0;
439 	}
440 	else
441 	    error = ENODEV;
442 	break;
443 
444     default:
445 	if (ata_raid_ioctl_func)
446 	    error = ata_raid_ioctl_func(ap->a_cmd, ap->a_data);
447     }
448     return error;
449 }
450 
451 int
452 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
453 {
454     struct ata_device *atadev = device_get_softc(dev);
455     struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
456     struct ata_params *params = (struct ata_params *)data;
457     int *mode = (int *)data;
458     struct ata_request *request;
459     caddr_t buf;
460     int error;
461 
462     switch (cmd) {
463     case IOCATAREQUEST:
464 	if (!(buf = kmalloc(ioc_request->count, M_ATA, M_WAITOK | M_NULLOK))) {
465 	    return ENOMEM;
466 	}
467 	if (!(request = ata_alloc_request())) {
468 	    kfree(buf, M_ATA);
469 	    return  ENOMEM;
470 	}
471 	if (ioc_request->flags & ATA_CMD_WRITE) {
472 	    error = copyin(ioc_request->data, buf, ioc_request->count);
473 	    if (error) {
474 		kfree(buf, M_ATA);
475 		ata_free_request(request);
476 		return error;
477 	    }
478 	}
479 	request->dev = dev;
480 	if (ioc_request->flags & ATA_CMD_ATAPI) {
481 	    request->flags = ATA_R_ATAPI;
482 	    bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
483 	}
484 	else {
485 	    request->u.ata.command = ioc_request->u.ata.command;
486 	    request->u.ata.feature = ioc_request->u.ata.feature;
487 	    request->u.ata.lba = ioc_request->u.ata.lba;
488 	    request->u.ata.count = ioc_request->u.ata.count;
489 	}
490 	request->timeout = ioc_request->timeout;
491 	request->data = buf;
492 	request->bytecount = ioc_request->count;
493 	request->transfersize = request->bytecount;
494 	if (ioc_request->flags & ATA_CMD_CONTROL)
495 	    request->flags |= ATA_R_CONTROL;
496 	if (ioc_request->flags & ATA_CMD_READ)
497 	    request->flags |= ATA_R_READ;
498 	if (ioc_request->flags & ATA_CMD_WRITE)
499 	    request->flags |= ATA_R_WRITE;
500 	ata_queue_request(request);
501 	if (request->flags & ATA_R_ATAPI) {
502 	    bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense,
503 		  sizeof(struct atapi_sense));
504 	}
505 	else {
506 	    ioc_request->u.ata.command = request->u.ata.command;
507 	    ioc_request->u.ata.feature = request->u.ata.feature;
508 	    ioc_request->u.ata.lba = request->u.ata.lba;
509 	    ioc_request->u.ata.count = request->u.ata.count;
510 	}
511 	ioc_request->error = request->result;
512 	if (ioc_request->flags & ATA_CMD_READ)
513 	    error = copyout(buf, ioc_request->data, ioc_request->count);
514 	else
515 	    error = 0;
516 	kfree(buf, M_ATA);
517 	ata_free_request(request);
518 	return error;
519 
520     case IOCATAGPARM:
521 	ata_getparam(atadev, 0);
522 	bcopy(&atadev->param, params, sizeof(struct ata_params));
523 	return 0;
524 
525     case IOCATASMODE:
526 	atadev->mode = *mode;
527 	ATA_SETMODE(device_get_parent(dev), dev);
528 	return 0;
529 
530     case IOCATAGMODE:
531 	*mode = atadev->mode;
532 	return 0;
533     default:
534 	return ENOTTY;
535     }
536 }
537 
538 #if 0
539 
540 static void
541 ata_boot_attach(void)
542 {
543     struct ata_channel *ch;
544     int ctlr;
545 
546     get_mplock();
547 
548     /* kick of probe and attach on all channels */
549     for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
550 	if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
551 	    ata_identify(ch->dev);
552 	}
553     }
554 
555     rel_mplock();
556 }
557 
558 #endif
559 
560 
561 /*
562  * misc support functions
563  */
564 static device_t
565 ata_add_child(device_t parent, struct ata_device *atadev, int unit)
566 {
567     device_t child;
568 
569     if ((child = device_add_child(parent, NULL, unit))) {
570 	device_set_softc(child, atadev);
571 	device_quiet(child);
572 	atadev->dev = child;
573 	atadev->max_iosize = DEV_BSIZE;
574 	atadev->mode = ATA_PIO_MAX;
575     }
576     return child;
577 }
578 
579 static int
580 ata_getparam(struct ata_device *atadev, int init)
581 {
582     struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
583     struct ata_request *request;
584     u_int8_t command = 0;
585     int error = ENOMEM, retries = 2;
586 
587     if (ch->devices &
588 	(atadev->unit == ATA_MASTER ? ATA_ATA_MASTER : ATA_ATA_SLAVE))
589 	command = ATA_ATA_IDENTIFY;
590     if (ch->devices &
591 	(atadev->unit == ATA_MASTER ? ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE))
592 	command = ATA_ATAPI_IDENTIFY;
593     if (!command)
594 	return ENXIO;
595 
596     while (retries-- > 0 && error) {
597 	if (!(request = ata_alloc_request()))
598 	    break;
599 	request->dev = atadev->dev;
600 	request->timeout = 1;
601 	request->retries = 0;
602 	request->u.ata.command = command;
603 	request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT|ATA_R_QUIET);
604 	request->data = (void *)&atadev->param;
605 	request->bytecount = sizeof(struct ata_params);
606 	request->donecount = 0;
607 	request->transfersize = DEV_BSIZE;
608 	ata_queue_request(request);
609 	error = request->result;
610 	ata_free_request(request);
611     }
612 
613     if (!error && (isprint(atadev->param.model[0]) ||
614 		   isprint(atadev->param.model[1]))) {
615 	struct ata_params *atacap = &atadev->param;
616 	char buffer[64];
617 	int16_t *ptr;
618 
619 	for (ptr = (int16_t *)atacap;
620 	     ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
621 	    *ptr = le16toh(*ptr);
622 	}
623 	if (!(!strncmp(atacap->model, "FX", 2) ||
624 	      !strncmp(atacap->model, "NEC", 3) ||
625 	      !strncmp(atacap->model, "Pioneer", 7) ||
626 	      !strncmp(atacap->model, "SHARP", 5))) {
627 	    bswap(atacap->model, sizeof(atacap->model));
628 	    bswap(atacap->revision, sizeof(atacap->revision));
629 	    bswap(atacap->serial, sizeof(atacap->serial));
630 	}
631 	btrim(atacap->model, sizeof(atacap->model));
632 	bpack(atacap->model, atacap->model, sizeof(atacap->model));
633 	btrim(atacap->revision, sizeof(atacap->revision));
634 	bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
635 	btrim(atacap->serial, sizeof(atacap->serial));
636 	bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
637 
638 	if (bootverbose)
639 	    kprintf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
640 		   device_get_unit(ch->dev),
641 		   atadev->unit == ATA_MASTER ? "master" : "slave",
642 		   ata_mode2str(ata_pmode(atacap)),
643 		   ata_mode2str(ata_wmode(atacap)),
644 		   ata_mode2str(ata_umode(atacap)),
645 		   (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
646 
647 	if (init) {
648 	    ksprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
649 	    device_set_desc_copy(atadev->dev, buffer);
650 	    if ((atadev->param.config & ATA_PROTO_ATAPI) &&
651 		(atadev->param.config != ATA_CFA_MAGIC1) &&
652 		(atadev->param.config != ATA_CFA_MAGIC2)) {
653 		if (atapi_dma && ch->dma &&
654 		    (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
655 		    ata_umode(&atadev->param) >= ATA_UDMA2)
656 		    atadev->mode = ATA_DMA_MAX;
657 	    }
658 	    else {
659 		if (ata_dma && ch->dma &&
660 		    (ata_umode(&atadev->param) > 0 ||
661 		     ata_wmode(&atadev->param) > 0))
662 		    atadev->mode = ATA_DMA_MAX;
663 	    }
664 	}
665     }
666     else {
667 	if (!error)
668 	    error = ENXIO;
669     }
670     return error;
671 }
672 
673 int
674 ata_identify(device_t dev)
675 {
676     struct ata_channel *ch = device_get_softc(dev);
677     struct ata_device *master = NULL, *slave = NULL;
678     device_t master_child = NULL, slave_child = NULL;
679     int master_unit = -1, slave_unit = -1;
680 
681     if (ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) {
682 	if (!(master = kmalloc(sizeof(struct ata_device),
683 			      M_ATA, M_INTWAIT | M_ZERO))) {
684 	    device_printf(dev, "out of memory\n");
685 	    return ENOMEM;
686 	}
687 	master->unit = ATA_MASTER;
688     }
689     if (ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) {
690 	if (!(slave = kmalloc(sizeof(struct ata_device),
691 			     M_ATA, M_INTWAIT | M_ZERO))) {
692 	    kfree(master, M_ATA);
693 	    device_printf(dev, "out of memory\n");
694 	    return ENOMEM;
695 	}
696 	slave->unit = ATA_SLAVE;
697     }
698 
699 #ifdef ATA_STATIC_ID
700     if (ch->devices & ATA_ATA_MASTER)
701 	master_unit = (device_get_unit(dev) << 1);
702 #endif
703     if (master && !(master_child = ata_add_child(dev, master, master_unit))) {
704 	kfree(master, M_ATA);
705 	master = NULL;
706     }
707 #ifdef ATA_STATIC_ID
708     if (ch->devices & ATA_ATA_SLAVE)
709 	slave_unit = (device_get_unit(dev) << 1) + 1;
710 #endif
711     if (slave && !(slave_child = ata_add_child(dev, slave, slave_unit))) {
712 	kfree(slave, M_ATA);
713 	slave = NULL;
714     }
715 
716     if (slave && ata_getparam(slave, 1)) {
717 	device_delete_child(dev, slave_child);
718 	kfree(slave, M_ATA);
719     }
720     if (master && ata_getparam(master, 1)) {
721 	device_delete_child(dev, master_child);
722 	kfree(master, M_ATA);
723     }
724 
725     bus_generic_probe(dev);
726     bus_generic_attach(dev);
727     return 0;
728 }
729 
730 void
731 ata_default_registers(device_t dev)
732 {
733     struct ata_channel *ch = device_get_softc(dev);
734 
735     /* fill in the defaults from whats setup already */
736     ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
737     ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
738     ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
739     ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
740     ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
741     ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
742     ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
743     ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
744 }
745 
746 void
747 ata_modify_if_48bit(struct ata_request *request)
748 {
749     struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
750     struct ata_device *atadev = device_get_softc(request->dev);
751 
752     atadev->flags &= ~ATA_D_48BIT_ACTIVE;
753 
754     if ((request->u.ata.lba + request->u.ata.count >= ATA_MAX_28BIT_LBA ||
755 	 request->u.ata.count > 256) &&
756 	atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
757 
758 	/* translate command into 48bit version */
759 	switch (request->u.ata.command) {
760 	case ATA_READ:
761 	    request->u.ata.command = ATA_READ48;
762 	    break;
763 	case ATA_READ_MUL:
764 	    request->u.ata.command = ATA_READ_MUL48;
765 	    break;
766 	case ATA_READ_DMA:
767 	    if (ch->flags & ATA_NO_48BIT_DMA) {
768 		if (request->transfersize > DEV_BSIZE)
769 		    request->u.ata.command = ATA_READ_MUL48;
770 		else
771 		    request->u.ata.command = ATA_READ48;
772 		request->flags &= ~ATA_R_DMA;
773 	    }
774 	    else
775 		request->u.ata.command = ATA_READ_DMA48;
776 	    break;
777 	case ATA_READ_DMA_QUEUED:
778 	    if (ch->flags & ATA_NO_48BIT_DMA) {
779 		if (request->transfersize > DEV_BSIZE)
780 		    request->u.ata.command = ATA_READ_MUL48;
781 		else
782 		    request->u.ata.command = ATA_READ48;
783 		request->flags &= ~ATA_R_DMA;
784 	    }
785 	    else
786 		request->u.ata.command = ATA_READ_DMA_QUEUED48;
787 	    break;
788 	case ATA_WRITE:
789 	    request->u.ata.command = ATA_WRITE48;
790 	    break;
791 	case ATA_WRITE_MUL:
792 	    request->u.ata.command = ATA_WRITE_MUL48;
793 	    break;
794 	case ATA_WRITE_DMA:
795 	    if (ch->flags & ATA_NO_48BIT_DMA) {
796 		if (request->transfersize > DEV_BSIZE)
797 		    request->u.ata.command = ATA_WRITE_MUL48;
798 		else
799 		    request->u.ata.command = ATA_WRITE48;
800 		request->flags &= ~ATA_R_DMA;
801 	    }
802 	    else
803 		request->u.ata.command = ATA_WRITE_DMA48;
804 	    break;
805 	case ATA_WRITE_DMA_QUEUED:
806 	    if (ch->flags & ATA_NO_48BIT_DMA) {
807 		if (request->transfersize > DEV_BSIZE)
808 		    request->u.ata.command = ATA_WRITE_MUL48;
809 		else
810 		    request->u.ata.command = ATA_WRITE48;
811 		request->u.ata.command = ATA_WRITE48;
812 		request->flags &= ~ATA_R_DMA;
813 	    }
814 	    else
815 		request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
816 	    break;
817 	case ATA_FLUSHCACHE:
818 	    request->u.ata.command = ATA_FLUSHCACHE48;
819 	    break;
820 	case ATA_READ_NATIVE_MAX_ADDDRESS:
821 	    request->u.ata.command = ATA_READ_NATIVE_MAX_ADDDRESS48;
822 	    break;
823 	case ATA_SET_MAX_ADDRESS:
824 	    request->u.ata.command = ATA_SET_MAX_ADDRESS48;
825 	    break;
826 	default:
827 	    return;
828 	}
829 	atadev->flags |= ATA_D_48BIT_ACTIVE;
830     }
831 }
832 
833 void
834 ata_udelay(int interval)
835 {
836     /*
837      * We can't use tsleep here, because we might be called from callout
838      * context.
839      */
840     if (1 || interval < (1000000/hz))
841 	DELAY(interval);
842     else
843 	tsleep(&interval, 0, "ataslp", 1 + interval / (1000000 / hz));
844 }
845 
846 char *
847 ata_mode2str(int mode)
848 {
849     switch (mode) {
850     case -1: return "UNSUPPORTED";
851     case ATA_PIO0: return "PIO0";
852     case ATA_PIO1: return "PIO1";
853     case ATA_PIO2: return "PIO2";
854     case ATA_PIO3: return "PIO3";
855     case ATA_PIO4: return "PIO4";
856     case ATA_WDMA0: return "WDMA0";
857     case ATA_WDMA1: return "WDMA1";
858     case ATA_WDMA2: return "WDMA2";
859     case ATA_UDMA0: return "UDMA16";
860     case ATA_UDMA1: return "UDMA25";
861     case ATA_UDMA2: return "UDMA33";
862     case ATA_UDMA3: return "UDMA40";
863     case ATA_UDMA4: return "UDMA66";
864     case ATA_UDMA5: return "UDMA100";
865     case ATA_UDMA6: return "UDMA133";
866     case ATA_SA150: return "SATA150";
867     case ATA_SA300: return "SATA300";
868     case ATA_USB: return "USB";
869     case ATA_USB1: return "USB1";
870     case ATA_USB2: return "USB2";
871     default:
872 	if (mode & ATA_DMA_MASK)
873 	    return "BIOSDMA";
874 	else
875 	    return "BIOSPIO";
876     }
877 }
878 
879 int
880 ata_pmode(struct ata_params *ap)
881 {
882     if (ap->atavalid & ATA_FLAG_64_70) {
883 	if (ap->apiomodes & 0x02)
884 	    return ATA_PIO4;
885 	if (ap->apiomodes & 0x01)
886 	    return ATA_PIO3;
887     }
888     if (ap->mwdmamodes & 0x04)
889 	return ATA_PIO4;
890     if (ap->mwdmamodes & 0x02)
891 	return ATA_PIO3;
892     if (ap->mwdmamodes & 0x01)
893 	return ATA_PIO2;
894     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
895 	return ATA_PIO2;
896     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
897 	return ATA_PIO1;
898     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
899 	return ATA_PIO0;
900     return ATA_PIO0;
901 }
902 
903 int
904 ata_wmode(struct ata_params *ap)
905 {
906     if (ap->mwdmamodes & 0x04)
907 	return ATA_WDMA2;
908     if (ap->mwdmamodes & 0x02)
909 	return ATA_WDMA1;
910     if (ap->mwdmamodes & 0x01)
911 	return ATA_WDMA0;
912     return -1;
913 }
914 
915 int
916 ata_umode(struct ata_params *ap)
917 {
918     if (ap->atavalid & ATA_FLAG_88) {
919 	if (ap->udmamodes & 0x40)
920 	    return ATA_UDMA6;
921 	if (ap->udmamodes & 0x20)
922 	    return ATA_UDMA5;
923 	if (ap->udmamodes & 0x10)
924 	    return ATA_UDMA4;
925 	if (ap->udmamodes & 0x08)
926 	    return ATA_UDMA3;
927 	if (ap->udmamodes & 0x04)
928 	    return ATA_UDMA2;
929 	if (ap->udmamodes & 0x02)
930 	    return ATA_UDMA1;
931 	if (ap->udmamodes & 0x01)
932 	    return ATA_UDMA0;
933     }
934     return -1;
935 }
936 
937 int
938 ata_limit_mode(device_t dev, int mode, int maxmode)
939 {
940     struct ata_device *atadev = device_get_softc(dev);
941 
942     if (maxmode && mode > maxmode)
943 	mode = maxmode;
944 
945     if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
946 	return min(mode, ata_umode(&atadev->param));
947 
948     if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
949 	return min(mode, ata_wmode(&atadev->param));
950 
951     if (mode > ata_pmode(&atadev->param))
952 	return min(mode, ata_pmode(&atadev->param));
953 
954     return mode;
955 }
956 
957 static void
958 bswap(int8_t *buf, int len)
959 {
960     u_int16_t *ptr = (u_int16_t*)(buf + len);
961 
962     while (--ptr >= (u_int16_t*)buf)
963 	*ptr = ntohs(*ptr);
964 }
965 
966 static void
967 btrim(int8_t *buf, int len)
968 {
969     int8_t *ptr;
970 
971     for (ptr = buf; ptr < buf+len; ++ptr)
972 	if (!*ptr || *ptr == '_')
973 	    *ptr = ' ';
974     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
975 	*ptr = 0;
976 }
977 
978 static void
979 bpack(int8_t *src, int8_t *dst, int len)
980 {
981     int i, j, blank;
982 
983     for (i = j = blank = 0 ; i < len; i++) {
984 	if (blank && src[i] == ' ') continue;
985 	if (blank && src[i] != ' ') {
986 	    dst[j++] = src[i];
987 	    blank = 0;
988 	    continue;
989 	}
990 	if (src[i] == ' ') {
991 	    blank = 1;
992 	    if (i == 0)
993 		continue;
994 	}
995 	dst[j++] = src[i];
996     }
997     if (j < len)
998 	dst[j] = 0x00;
999 }
1000 
1001 
1002 /*
1003  * module handeling
1004  */
1005 static int
1006 ata_module_event_handler(module_t mod, int what, void *arg)
1007 {
1008     /* static because we need the reference at destruction time */
1009     static cdev_t atacdev;
1010 
1011     switch (what) {
1012     case MOD_LOAD:
1013 	/* register controlling device */
1014 	atacdev = make_dev(&ata_ops, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1015 	reference_dev(atacdev);
1016 	return 0;
1017 
1018     case MOD_UNLOAD:
1019 	/* deregister controlling device */
1020 	destroy_dev(atacdev);
1021 	dev_ops_remove_all(&ata_ops);
1022 	return 0;
1023 
1024     default:
1025 	return EOPNOTSUPP;
1026     }
1027 }
1028 
1029 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1030 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1031 MODULE_VERSION(ata, 1);
1032 
1033 /*
1034  * Construct a completely zero'ed ata_request. On objcache_put(), an
1035  * ata_request object is also zero'ed, so objcache_get() is guaranteed to give
1036  * completely zero'ed objects without spending too much time.
1037  */
1038 static boolean_t
1039 ata_request_cache_ctor(void *obj, void *private, int ocflags)
1040 {
1041     struct ata_request *arp = obj;
1042 
1043     bzero(arp, sizeof(struct ata_request));
1044     return(TRUE);
1045 }
1046 
1047 /*
1048  * Construct a completely zero'ed ata_composite. On objcache_put(), an
1049  * ata_composite object is also zero'ed, so objcache_get() is guaranteed to give
1050  * completely zero'ed objects without spending too much time.
1051  */
1052 static boolean_t
1053 ata_composite_cache_ctor(void *obj, void *private, int ocflags)
1054 {
1055     struct ata_composite *acp = obj;
1056 
1057     bzero(acp, sizeof(struct ata_composite));
1058     return(TRUE);
1059 }
1060 
1061 static void
1062 ata_init(void)
1063 {
1064     ata_request_cache = objcache_create("ata_request", 0, 0,
1065 					ata_request_cache_ctor, NULL, NULL,
1066 					objcache_malloc_alloc,
1067 					objcache_malloc_free,
1068 					&ata_request_malloc_args);
1069     ata_composite_cache = objcache_create("ata_composite", 0, 0,
1070 					  ata_composite_cache_ctor, NULL, NULL,
1071 					  objcache_malloc_alloc,
1072 					  objcache_malloc_free,
1073 					  &ata_composite_malloc_args);
1074 }
1075 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1076 
1077 static void
1078 ata_uninit(void)
1079 {
1080     objcache_destroy(ata_composite_cache);
1081     objcache_destroy(ata_request_cache);
1082 }
1083 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);
1084