xref: /freebsd/sys/dev/ata/ata-all.c (revision b2db760808f74bb53c232900091c9da801ebbfcc)
1 /*-
2  * Copyright (c) 1998 - 2008 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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_ata.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/ata.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/endian.h>
37 #include <sys/ctype.h>
38 #include <sys/conf.h>
39 #include <sys/bus.h>
40 #include <sys/bio.h>
41 #include <sys/malloc.h>
42 #include <sys/sysctl.h>
43 #include <sys/sema.h>
44 #include <sys/taskqueue.h>
45 #include <vm/uma.h>
46 #include <machine/stdarg.h>
47 #include <machine/resource.h>
48 #include <machine/bus.h>
49 #include <sys/rman.h>
50 #include <dev/ata/ata-all.h>
51 #include <dev/pci/pcivar.h>
52 #include <ata_if.h>
53 
54 #ifdef ATA_CAM
55 #include <cam/cam.h>
56 #include <cam/cam_ccb.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_debug.h>
60 #endif
61 
62 #ifndef ATA_CAM
63 /* device structure */
64 static  d_ioctl_t       ata_ioctl;
65 static struct cdevsw ata_cdevsw = {
66 	.d_version =    D_VERSION,
67 	.d_flags =      D_NEEDGIANT, /* we need this as newbus isn't mpsafe */
68 	.d_ioctl =      ata_ioctl,
69 	.d_name =       "ata",
70 };
71 #endif
72 
73 /* prototypes */
74 #ifndef ATA_CAM
75 static void ata_boot_attach(void);
76 static device_t ata_add_child(device_t, struct ata_device *, int);
77 #else
78 static void ataaction(struct cam_sim *sim, union ccb *ccb);
79 static void atapoll(struct cam_sim *sim);
80 #endif
81 static void ata_conn_event(void *, int);
82 static void bswap(int8_t *, int);
83 static void btrim(int8_t *, int);
84 static void bpack(int8_t *, int8_t *, int);
85 static void ata_interrupt_locked(void *data);
86 
87 /* global vars */
88 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer");
89 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
90 struct intr_config_hook *ata_delayed_attach = NULL;
91 devclass_t ata_devclass;
92 uma_zone_t ata_request_zone;
93 uma_zone_t ata_composite_zone;
94 int ata_wc = 1;
95 int ata_setmax = 0;
96 int ata_dma_check_80pin = 1;
97 
98 /* local vars */
99 static int ata_dma = 1;
100 static int atapi_dma = 1;
101 
102 /* sysctl vars */
103 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
104 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
105 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
106 	   "ATA disk DMA mode control");
107 TUNABLE_INT("hw.ata.ata_dma_check_80pin", &ata_dma_check_80pin);
108 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma_check_80pin,
109 	   CTLFLAG_RW, &ata_dma_check_80pin, 1,
110 	   "Check for 80pin cable before setting ATA DMA mode");
111 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
112 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
113 	   "ATAPI device DMA mode control");
114 TUNABLE_INT("hw.ata.wc", &ata_wc);
115 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
116 	   "ATA disk write caching");
117 TUNABLE_INT("hw.ata.setmax", &ata_setmax);
118 SYSCTL_INT(_hw_ata, OID_AUTO, setmax, CTLFLAG_RDTUN, &ata_setmax, 0,
119 	   "ATA disk set max native address");
120 
121 /*
122  * newbus device interface related functions
123  */
124 int
125 ata_probe(device_t dev)
126 {
127     return 0;
128 }
129 
130 int
131 ata_attach(device_t dev)
132 {
133     struct ata_channel *ch = device_get_softc(dev);
134     int error, rid;
135 #ifdef ATA_CAM
136     struct cam_devq *devq;
137     const char *res;
138     char buf[64];
139     int i, mode;
140 #endif
141 
142     /* check that we have a virgin channel to attach */
143     if (ch->r_irq)
144 	return EEXIST;
145 
146     /* initialize the softc basics */
147     ch->dev = dev;
148     ch->state = ATA_IDLE;
149     bzero(&ch->state_mtx, sizeof(struct mtx));
150     mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
151     bzero(&ch->queue_mtx, sizeof(struct mtx));
152     mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
153     TAILQ_INIT(&ch->ata_queue);
154     TASK_INIT(&ch->conntask, 0, ata_conn_event, dev);
155 #ifdef ATA_CAM
156 	for (i = 0; i < 16; i++) {
157 		ch->user[i].mode = 0;
158 		snprintf(buf, sizeof(buf), "dev%d.mode", i);
159 		if (resource_string_value(device_get_name(dev),
160 		    device_get_unit(dev), buf, &res) == 0)
161 			mode = ata_str2mode(res);
162 		else if (resource_string_value(device_get_name(dev),
163 		    device_get_unit(dev), "mode", &res) == 0)
164 			mode = ata_str2mode(res);
165 		else
166 			mode = -1;
167 		if (mode >= 0)
168 			ch->user[i].mode = mode;
169 		if (ch->flags & ATA_SATA)
170 			ch->user[i].bytecount = 8192;
171 		else
172 			ch->user[i].bytecount = MAXPHYS;
173 		ch->curr[i] = ch->user[i];
174 	}
175 #endif
176 
177     /* reset the controller HW, the channel and device(s) */
178     while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
179 	pause("ataatch", 1);
180 #ifndef ATA_CAM
181     ATA_RESET(dev);
182 #endif
183     ATA_LOCKING(dev, ATA_LF_UNLOCK);
184 
185     /* allocate DMA resources if DMA HW present*/
186     if (ch->dma.alloc)
187 	ch->dma.alloc(dev);
188 
189     /* setup interrupt delivery */
190     rid = ATA_IRQ_RID;
191     ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
192 				       RF_SHAREABLE | RF_ACTIVE);
193     if (!ch->r_irq) {
194 	device_printf(dev, "unable to allocate interrupt\n");
195 	return ENXIO;
196     }
197     if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
198 				ata_interrupt, ch, &ch->ih))) {
199 	bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq);
200 	device_printf(dev, "unable to setup interrupt\n");
201 	return error;
202     }
203 
204 #ifndef ATA_CAM
205     /* probe and attach devices on this channel unless we are in early boot */
206     if (!ata_delayed_attach)
207 	ata_identify(dev);
208     return (0);
209 #else
210 	mtx_lock(&ch->state_mtx);
211 	/* Create the device queue for our SIM. */
212 	devq = cam_simq_alloc(1);
213 	if (devq == NULL) {
214 		device_printf(dev, "Unable to allocate simq\n");
215 		error = ENOMEM;
216 		goto err1;
217 	}
218 	/* Construct SIM entry */
219 	ch->sim = cam_sim_alloc(ataaction, atapoll, "ata", ch,
220 	    device_get_unit(dev), &ch->state_mtx, 1, 0, devq);
221 	if (ch->sim == NULL) {
222 		device_printf(dev, "unable to allocate sim\n");
223 		cam_simq_free(devq);
224 		error = ENOMEM;
225 		goto err1;
226 	}
227 	if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
228 		device_printf(dev, "unable to register xpt bus\n");
229 		error = ENXIO;
230 		goto err2;
231 	}
232 	if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
233 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
234 		device_printf(dev, "unable to create path\n");
235 		error = ENXIO;
236 		goto err3;
237 	}
238 	mtx_unlock(&ch->state_mtx);
239 	return (0);
240 
241 err3:
242 	xpt_bus_deregister(cam_sim_path(ch->sim));
243 err2:
244 	cam_sim_free(ch->sim, /*free_devq*/TRUE);
245 	ch->sim = NULL;
246 err1:
247 	bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq);
248 	mtx_unlock(&ch->state_mtx);
249 	return (error);
250 #endif
251 }
252 
253 int
254 ata_detach(device_t dev)
255 {
256     struct ata_channel *ch = device_get_softc(dev);
257 #ifndef ATA_CAM
258     device_t *children;
259     int nchildren, i;
260 #endif
261 
262     /* check that we have a valid channel to detach */
263     if (!ch->r_irq)
264 	return ENXIO;
265 
266     /* grap the channel lock so no new requests gets launched */
267     mtx_lock(&ch->state_mtx);
268     ch->state |= ATA_STALL_QUEUE;
269     mtx_unlock(&ch->state_mtx);
270 
271 #ifndef ATA_CAM
272     /* detach & delete all children */
273     if (!device_get_children(dev, &children, &nchildren)) {
274 	for (i = 0; i < nchildren; i++)
275 	    if (children[i])
276 		device_delete_child(dev, children[i]);
277 	free(children, M_TEMP);
278     }
279 #endif
280     taskqueue_drain(taskqueue_thread, &ch->conntask);
281 
282 #ifdef ATA_CAM
283 	mtx_lock(&ch->state_mtx);
284 	xpt_async(AC_LOST_DEVICE, ch->path, NULL);
285 	xpt_free_path(ch->path);
286 	xpt_bus_deregister(cam_sim_path(ch->sim));
287 	cam_sim_free(ch->sim, /*free_devq*/TRUE);
288 	ch->sim = NULL;
289 	mtx_unlock(&ch->state_mtx);
290 #endif
291 
292     /* release resources */
293     bus_teardown_intr(dev, ch->r_irq, ch->ih);
294     bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
295     ch->r_irq = NULL;
296 
297     /* free DMA resources if DMA HW present*/
298     if (ch->dma.free)
299 	ch->dma.free(dev);
300 
301     mtx_destroy(&ch->state_mtx);
302     mtx_destroy(&ch->queue_mtx);
303     return 0;
304 }
305 
306 static void
307 ata_conn_event(void *context, int dummy)
308 {
309 	device_t dev = (device_t)context;
310 #ifdef ATA_CAM
311 	struct ata_channel *ch = device_get_softc(dev);
312 	union ccb *ccb;
313 
314 	mtx_lock(&ch->state_mtx);
315 	if (ch->sim == NULL) {
316 		mtx_unlock(&ch->state_mtx);
317 		return;
318 	}
319 	ata_reinit(dev);
320 	if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
321 		return;
322 	if (xpt_create_path(&ccb->ccb_h.path, NULL,
323 	    cam_sim_path(ch->sim),
324 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
325 		xpt_free_ccb(ccb);
326 		return;
327 	}
328 	xpt_rescan(ccb);
329 	mtx_unlock(&ch->state_mtx);
330 #else
331 	ata_reinit(dev);
332 #endif
333 }
334 
335 int
336 ata_reinit(device_t dev)
337 {
338     struct ata_channel *ch = device_get_softc(dev);
339     struct ata_request *request;
340 #ifndef ATA_CAM
341     device_t *children;
342     int nchildren, i;
343 
344     /* check that we have a valid channel to reinit */
345     if (!ch || !ch->r_irq)
346 	return ENXIO;
347 
348     if (bootverbose)
349 	device_printf(dev, "reiniting channel ..\n");
350 
351     /* poll for locking the channel */
352     while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
353 	pause("atarini", 1);
354 
355     /* catch eventual request in ch->running */
356     mtx_lock(&ch->state_mtx);
357     if (ch->state & ATA_STALL_QUEUE) {
358 	/* Recursive reinits and reinits during detach prohobited. */
359 	mtx_unlock(&ch->state_mtx);
360 	return (ENXIO);
361     }
362     if ((request = ch->running))
363 	callout_stop(&request->callout);
364     ch->running = NULL;
365 
366     /* unconditionally grap the channel lock */
367     ch->state |= ATA_STALL_QUEUE;
368     mtx_unlock(&ch->state_mtx);
369 
370     /* reset the controller HW, the channel and device(s) */
371     ATA_RESET(dev);
372 
373     /* reinit the children and delete any that fails */
374     if (!device_get_children(dev, &children, &nchildren)) {
375 	mtx_lock(&Giant);       /* newbus suckage it needs Giant */
376 	for (i = 0; i < nchildren; i++) {
377 	    /* did any children go missing ? */
378 	    if (children[i] && device_is_attached(children[i]) &&
379 		ATA_REINIT(children[i])) {
380 		/*
381 		 * if we had a running request and its device matches
382 		 * this child we need to inform the request that the
383 		 * device is gone.
384 		 */
385 		if (request && request->dev == children[i]) {
386 		    request->result = ENXIO;
387 		    device_printf(request->dev, "FAILURE - device detached\n");
388 
389 		    /* if not timeout finish request here */
390 		    if (!(request->flags & ATA_R_TIMEOUT))
391 			    ata_finish(request);
392 		    request = NULL;
393 		}
394 		device_delete_child(dev, children[i]);
395 	    }
396 	}
397 	free(children, M_TEMP);
398 	mtx_unlock(&Giant);     /* newbus suckage dealt with, release Giant */
399     }
400 
401     /* if we still have a good request put it on the queue again */
402     if (request && !(request->flags & ATA_R_TIMEOUT)) {
403 	device_printf(request->dev,
404 		      "WARNING - %s requeued due to channel reset",
405 		      ata_cmd2str(request));
406 	if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
407 	    printf(" LBA=%ju", request->u.ata.lba);
408 	printf("\n");
409 	request->flags |= ATA_R_REQUEUE;
410 	ata_queue_request(request);
411     }
412 
413     /* we're done release the channel for new work */
414     mtx_lock(&ch->state_mtx);
415     ch->state = ATA_IDLE;
416     mtx_unlock(&ch->state_mtx);
417     ATA_LOCKING(dev, ATA_LF_UNLOCK);
418 
419     /* Add new children. */
420 /*    ata_identify(dev); */
421 
422     if (bootverbose)
423 	device_printf(dev, "reinit done ..\n");
424 
425     /* kick off requests on the queue */
426     ata_start(dev);
427 #else
428 	xpt_freeze_simq(ch->sim, 1);
429 	if ((request = ch->running)) {
430 		ch->running = NULL;
431 		if (ch->state == ATA_ACTIVE)
432 		    ch->state = ATA_IDLE;
433 		callout_stop(&request->callout);
434 		if (ch->dma.unload)
435 		    ch->dma.unload(request);
436 		request->result = ERESTART;
437 		ata_cam_end_transaction(dev, request);
438 	}
439 	/* reset the controller HW, the channel and device(s) */
440 	ATA_RESET(dev);
441 	/* Tell the XPT about the event */
442 	xpt_async(AC_BUS_RESET, ch->path, NULL);
443 	xpt_release_simq(ch->sim, TRUE);
444 #endif
445 	return(0);
446 }
447 
448 int
449 ata_suspend(device_t dev)
450 {
451     struct ata_channel *ch;
452 
453     /* check for valid device */
454     if (!dev || !(ch = device_get_softc(dev)))
455 	return ENXIO;
456 
457 #ifdef ATA_CAM
458 	mtx_lock(&ch->state_mtx);
459 	xpt_freeze_simq(ch->sim, 1);
460 	while (ch->state != ATA_IDLE)
461 		msleep(ch, &ch->state_mtx, PRIBIO, "atasusp", hz/100);
462 	mtx_unlock(&ch->state_mtx);
463 #else
464     /* wait for the channel to be IDLE or detached before suspending */
465     while (ch->r_irq) {
466 	mtx_lock(&ch->state_mtx);
467 	if (ch->state == ATA_IDLE) {
468 	    ch->state = ATA_ACTIVE;
469 	    mtx_unlock(&ch->state_mtx);
470 	    break;
471 	}
472 	mtx_unlock(&ch->state_mtx);
473 	tsleep(ch, PRIBIO, "atasusp", hz/10);
474     }
475     ATA_LOCKING(dev, ATA_LF_UNLOCK);
476 #endif
477     return(0);
478 }
479 
480 int
481 ata_resume(device_t dev)
482 {
483     struct ata_channel *ch;
484     int error;
485 
486     /* check for valid device */
487     if (!dev || !(ch = device_get_softc(dev)))
488 	return ENXIO;
489 
490 #ifdef ATA_CAM
491 	mtx_lock(&ch->state_mtx);
492 	error = ata_reinit(dev);
493 	xpt_release_simq(ch->sim, TRUE);
494 	mtx_unlock(&ch->state_mtx);
495 #else
496     /* reinit the devices, we dont know what mode/state they are in */
497     error = ata_reinit(dev);
498     /* kick off requests on the queue */
499     ata_start(dev);
500 #endif
501     return error;
502 }
503 
504 void
505 ata_interrupt(void *data)
506 {
507 #ifdef ATA_CAM
508     struct ata_channel *ch = (struct ata_channel *)data;
509 
510     mtx_lock(&ch->state_mtx);
511 #endif
512     ata_interrupt_locked(data);
513 #ifdef ATA_CAM
514     mtx_unlock(&ch->state_mtx);
515 #endif
516 }
517 
518 static void
519 ata_interrupt_locked(void *data)
520 {
521     struct ata_channel *ch = (struct ata_channel *)data;
522     struct ata_request *request;
523 
524 #ifndef ATA_CAM
525     mtx_lock(&ch->state_mtx);
526 #endif
527     do {
528 	/* ignore interrupt if its not for us */
529 	if (ch->hw.status && !ch->hw.status(ch->dev))
530 	    break;
531 
532 	/* do we have a running request */
533 	if (!(request = ch->running))
534 	    break;
535 
536 	ATA_DEBUG_RQ(request, "interrupt");
537 
538 	/* safetycheck for the right state */
539 	if (ch->state == ATA_IDLE) {
540 	    device_printf(request->dev, "interrupt on idle channel ignored\n");
541 	    break;
542 	}
543 
544 	/*
545 	 * we have the HW locks, so end the transaction for this request
546 	 * if it finishes immediately otherwise wait for next interrupt
547 	 */
548 	if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
549 	    ch->running = NULL;
550 	    if (ch->state == ATA_ACTIVE)
551 		ch->state = ATA_IDLE;
552 #ifdef ATA_CAM
553 	    ata_cam_end_transaction(ch->dev, request);
554 #else
555 	    mtx_unlock(&ch->state_mtx);
556 	    ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
557 	    ata_finish(request);
558 #endif
559 	    return;
560 	}
561     } while (0);
562 #ifndef ATA_CAM
563     mtx_unlock(&ch->state_mtx);
564 #endif
565 }
566 
567 void
568 ata_print_cable(device_t dev, u_int8_t *who)
569 {
570     device_printf(dev,
571                   "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
572 }
573 
574 int
575 ata_check_80pin(device_t dev, int mode)
576 {
577     struct ata_device *atadev = device_get_softc(dev);
578 
579     if (!ata_dma_check_80pin) {
580         if (bootverbose)
581             device_printf(dev, "Skipping 80pin cable check\n");
582         return mode;
583     }
584 
585     if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
586         ata_print_cable(dev, "device");
587         mode = ATA_UDMA2;
588     }
589     return mode;
590 }
591 
592 void
593 ata_setmode(device_t dev)
594 {
595 	struct ata_channel *ch = device_get_softc(device_get_parent(dev));
596 	struct ata_device *atadev = device_get_softc(dev);
597 	int error, mode, pmode;
598 
599 	mode = atadev->mode;
600 	do {
601 		pmode = mode = ata_limit_mode(dev, mode, ATA_DMA_MAX);
602 		mode = ATA_SETMODE(device_get_parent(dev), atadev->unit, mode);
603 		if ((ch->flags & (ATA_CHECKS_CABLE | ATA_SATA)) == 0)
604 			mode = ata_check_80pin(dev, mode);
605 	} while (pmode != mode); /* Interate till successfull negotiation. */
606 	error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
607 	if (bootverbose)
608 	        device_printf(dev, "%ssetting %s\n",
609 		    (error) ? "FAILURE " : "", ata_mode2str(mode));
610 	atadev->mode = mode;
611 }
612 
613 /*
614  * device related interfaces
615  */
616 #ifndef ATA_CAM
617 static int
618 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
619 	  int32_t flag, struct thread *td)
620 {
621     device_t device, *children;
622     struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
623     int *value = (int *)data;
624     int i, nchildren, error = ENOTTY;
625 
626     switch (cmd) {
627     case IOCATAGMAXCHANNEL:
628 	/* In case we have channel 0..n this will return n+1. */
629 	*value = devclass_get_maxunit(ata_devclass);
630 	error = 0;
631 	break;
632 
633     case IOCATAREINIT:
634 	if (*value >= devclass_get_maxunit(ata_devclass) ||
635 	    !(device = devclass_get_device(ata_devclass, *value)) ||
636 	    !device_is_attached(device))
637 	    return ENXIO;
638 	error = ata_reinit(device);
639 	break;
640 
641     case IOCATAATTACH:
642 	if (*value >= devclass_get_maxunit(ata_devclass) ||
643 	    !(device = devclass_get_device(ata_devclass, *value)) ||
644 	    !device_is_attached(device))
645 	    return ENXIO;
646 	error = DEVICE_ATTACH(device);
647 	break;
648 
649     case IOCATADETACH:
650 	if (*value >= devclass_get_maxunit(ata_devclass) ||
651 	    !(device = devclass_get_device(ata_devclass, *value)) ||
652 	    !device_is_attached(device))
653 	    return ENXIO;
654 	error = DEVICE_DETACH(device);
655 	break;
656 
657     case IOCATADEVICES:
658 	if (devices->channel >= devclass_get_maxunit(ata_devclass) ||
659 	    !(device = devclass_get_device(ata_devclass, devices->channel)) ||
660 	    !device_is_attached(device))
661 	    return ENXIO;
662 	bzero(devices->name[0], 32);
663 	bzero(&devices->params[0], sizeof(struct ata_params));
664 	bzero(devices->name[1], 32);
665 	bzero(&devices->params[1], sizeof(struct ata_params));
666 	if (!device_get_children(device, &children, &nchildren)) {
667 	    for (i = 0; i < nchildren; i++) {
668 		if (children[i] && device_is_attached(children[i])) {
669 		    struct ata_device *atadev = device_get_softc(children[i]);
670 
671 		    if (atadev->unit == ATA_MASTER) { /* XXX SOS PM */
672 			strncpy(devices->name[0],
673 				device_get_nameunit(children[i]), 32);
674 			bcopy(&atadev->param, &devices->params[0],
675 			      sizeof(struct ata_params));
676 		    }
677 		    if (atadev->unit == ATA_SLAVE) { /* XXX SOS PM */
678 			strncpy(devices->name[1],
679 				device_get_nameunit(children[i]), 32);
680 			bcopy(&atadev->param, &devices->params[1],
681 			      sizeof(struct ata_params));
682 		    }
683 		}
684 	    }
685 	    free(children, M_TEMP);
686 	    error = 0;
687 	}
688 	else
689 	    error = ENODEV;
690 	break;
691 
692     default:
693 	if (ata_raid_ioctl_func)
694 	    error = ata_raid_ioctl_func(cmd, data);
695     }
696     return error;
697 }
698 #endif
699 
700 int
701 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
702 {
703     struct ata_device *atadev = device_get_softc(dev);
704     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
705     struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
706     struct ata_params *params = (struct ata_params *)data;
707     int *mode = (int *)data;
708     struct ata_request *request;
709     caddr_t buf;
710     int error;
711 
712     switch (cmd) {
713     case IOCATAREQUEST:
714 	if (ioc_request->count >
715 	    (ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS)) {
716 		return (EFBIG);
717 	}
718 	if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
719 	    return ENOMEM;
720 	}
721 	if (!(request = ata_alloc_request())) {
722 	    free(buf, M_ATA);
723 	    return  ENOMEM;
724 	}
725 	request->dev = atadev->dev;
726 	if (ioc_request->flags & ATA_CMD_WRITE) {
727 	    error = copyin(ioc_request->data, buf, ioc_request->count);
728 	    if (error) {
729 		free(buf, M_ATA);
730 		ata_free_request(request);
731 		return error;
732 	    }
733 	}
734 	if (ioc_request->flags & ATA_CMD_ATAPI) {
735 	    request->flags = ATA_R_ATAPI;
736 	    bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
737 	}
738 	else {
739 	    request->u.ata.command = ioc_request->u.ata.command;
740 	    request->u.ata.feature = ioc_request->u.ata.feature;
741 	    request->u.ata.lba = ioc_request->u.ata.lba;
742 	    request->u.ata.count = ioc_request->u.ata.count;
743 	}
744 	request->timeout = ioc_request->timeout;
745 	request->data = buf;
746 	request->bytecount = ioc_request->count;
747 	request->transfersize = request->bytecount;
748 	if (ioc_request->flags & ATA_CMD_CONTROL)
749 	    request->flags |= ATA_R_CONTROL;
750 	if (ioc_request->flags & ATA_CMD_READ)
751 	    request->flags |= ATA_R_READ;
752 	if (ioc_request->flags & ATA_CMD_WRITE)
753 	    request->flags |= ATA_R_WRITE;
754 	ata_queue_request(request);
755 	if (request->flags & ATA_R_ATAPI) {
756 	    bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense,
757 		  sizeof(struct atapi_sense));
758 	}
759 	else {
760 	    ioc_request->u.ata.command = request->u.ata.command;
761 	    ioc_request->u.ata.feature = request->u.ata.feature;
762 	    ioc_request->u.ata.lba = request->u.ata.lba;
763 	    ioc_request->u.ata.count = request->u.ata.count;
764 	}
765 	ioc_request->error = request->result;
766 	if (ioc_request->flags & ATA_CMD_READ)
767 	    error = copyout(buf, ioc_request->data, ioc_request->count);
768 	else
769 	    error = 0;
770 	free(buf, M_ATA);
771 	ata_free_request(request);
772 	return error;
773 
774     case IOCATAGPARM:
775 	ata_getparam(atadev, 0);
776 	bcopy(&atadev->param, params, sizeof(struct ata_params));
777 	return 0;
778 
779     case IOCATASMODE:
780 	atadev->mode = *mode;
781 	ata_setmode(dev);
782 	return 0;
783 
784     case IOCATAGMODE:
785 	*mode = atadev->mode |
786 	    (ATA_GETREV(device_get_parent(dev), atadev->unit) << 8);
787 	return 0;
788     case IOCATASSPINDOWN:
789 	atadev->spindown = *mode;
790 	return 0;
791     case IOCATAGSPINDOWN:
792 	*mode = atadev->spindown;
793 	return 0;
794     default:
795 	return ENOTTY;
796     }
797 }
798 
799 #ifndef ATA_CAM
800 static void
801 ata_boot_attach(void)
802 {
803     struct ata_channel *ch;
804     int ctlr;
805 
806     mtx_lock(&Giant);       /* newbus suckage it needs Giant */
807 
808     /* kick of probe and attach on all channels */
809     for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
810 	if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
811 	    ata_identify(ch->dev);
812 	}
813     }
814 
815     /* release the hook that got us here, we are only needed once during boot */
816     if (ata_delayed_attach) {
817 	config_intrhook_disestablish(ata_delayed_attach);
818 	free(ata_delayed_attach, M_TEMP);
819 	ata_delayed_attach = NULL;
820     }
821 
822     mtx_unlock(&Giant);     /* newbus suckage dealt with, release Giant */
823 }
824 #endif
825 
826 /*
827  * misc support functions
828  */
829 #ifndef ATA_CAM
830 static device_t
831 ata_add_child(device_t parent, struct ata_device *atadev, int unit)
832 {
833     device_t child;
834 
835     if ((child = device_add_child(parent, NULL, unit))) {
836 	device_set_softc(child, atadev);
837 	device_quiet(child);
838 	atadev->dev = child;
839 	atadev->max_iosize = DEV_BSIZE;
840 	atadev->mode = ATA_PIO_MAX;
841     }
842     return child;
843 }
844 #endif
845 
846 int
847 ata_getparam(struct ata_device *atadev, int init)
848 {
849     struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
850     struct ata_request *request;
851     const char *res;
852     char buf[64];
853     u_int8_t command = 0;
854     int error = ENOMEM, retries = 2, mode = -1;
855 
856     if (ch->devices & (ATA_ATA_MASTER << atadev->unit))
857 	command = ATA_ATA_IDENTIFY;
858     if (ch->devices & (ATA_ATAPI_MASTER << atadev->unit))
859 	command = ATA_ATAPI_IDENTIFY;
860     if (!command)
861 	return ENXIO;
862 
863     while (retries-- > 0 && error) {
864 	if (!(request = ata_alloc_request()))
865 	    break;
866 	request->dev = atadev->dev;
867 	request->timeout = 1;
868 	request->retries = 0;
869 	request->u.ata.command = command;
870 	request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT);
871 	if (!bootverbose)
872 	    request->flags |= ATA_R_QUIET;
873 	request->data = (void *)&atadev->param;
874 	request->bytecount = sizeof(struct ata_params);
875 	request->donecount = 0;
876 	request->transfersize = DEV_BSIZE;
877 	ata_queue_request(request);
878 	error = request->result;
879 	ata_free_request(request);
880     }
881 
882     if (!error && (isprint(atadev->param.model[0]) ||
883 		   isprint(atadev->param.model[1]))) {
884 	struct ata_params *atacap = &atadev->param;
885 	int16_t *ptr;
886 
887 	for (ptr = (int16_t *)atacap;
888 	     ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
889 	    *ptr = le16toh(*ptr);
890 	}
891 	if (!(!strncmp(atacap->model, "FX", 2) ||
892 	      !strncmp(atacap->model, "NEC", 3) ||
893 	      !strncmp(atacap->model, "Pioneer", 7) ||
894 	      !strncmp(atacap->model, "SHARP", 5))) {
895 	    bswap(atacap->model, sizeof(atacap->model));
896 	    bswap(atacap->revision, sizeof(atacap->revision));
897 	    bswap(atacap->serial, sizeof(atacap->serial));
898 	}
899 	btrim(atacap->model, sizeof(atacap->model));
900 	bpack(atacap->model, atacap->model, sizeof(atacap->model));
901 	btrim(atacap->revision, sizeof(atacap->revision));
902 	bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
903 	btrim(atacap->serial, sizeof(atacap->serial));
904 	bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
905 
906 	if (bootverbose)
907 	    printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
908 		   device_get_unit(ch->dev),
909 		   ata_unit2str(atadev),
910 		   ata_mode2str(ata_pmode(atacap)),
911 		   ata_mode2str(ata_wmode(atacap)),
912 		   ata_mode2str(ata_umode(atacap)),
913 		   (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
914 
915 	if (init) {
916 	    char buffer[64];
917 
918 	    sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
919 	    device_set_desc_copy(atadev->dev, buffer);
920 	    if ((atadev->param.config & ATA_PROTO_ATAPI) &&
921 		(atadev->param.config != ATA_CFA_MAGIC1) &&
922 		(atadev->param.config != ATA_CFA_MAGIC2)) {
923 		if (atapi_dma &&
924 		    (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
925 		    ata_umode(&atadev->param) >= ATA_UDMA2)
926 		    atadev->mode = ATA_DMA_MAX;
927 	    }
928 	    else {
929 		if (ata_dma &&
930 		    (ata_umode(&atadev->param) > 0 ||
931 		     ata_wmode(&atadev->param) > 0))
932 		    atadev->mode = ATA_DMA_MAX;
933 	    }
934 	    snprintf(buf, sizeof(buf), "dev%d.mode", atadev->unit);
935 	    if (resource_string_value(device_get_name(ch->dev),
936 	        device_get_unit(ch->dev), buf, &res) == 0)
937 		    mode = ata_str2mode(res);
938 	    else if (resource_string_value(device_get_name(ch->dev),
939 		device_get_unit(ch->dev), "mode", &res) == 0)
940 		    mode = ata_str2mode(res);
941 	    if (mode >= 0)
942 		    atadev->mode = mode;
943 	}
944     }
945     else {
946 	if (!error)
947 	    error = ENXIO;
948     }
949     return error;
950 }
951 
952 #ifndef ATA_CAM
953 int
954 ata_identify(device_t dev)
955 {
956     struct ata_channel *ch = device_get_softc(dev);
957     struct ata_device *atadev;
958     device_t *children;
959     device_t child, master = NULL;
960     int nchildren, i, n = ch->devices;
961 
962     if (bootverbose)
963 	device_printf(dev, "Identifying devices: %08x\n", ch->devices);
964 
965     mtx_lock(&Giant);
966     /* Skip existing devices. */
967     if (!device_get_children(dev, &children, &nchildren)) {
968 	for (i = 0; i < nchildren; i++) {
969 	    if (children[i] && (atadev = device_get_softc(children[i])))
970 		n &= ~((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << atadev->unit);
971 	}
972 	free(children, M_TEMP);
973     }
974     /* Create new devices. */
975     if (bootverbose)
976 	device_printf(dev, "New devices: %08x\n", n);
977     if (n == 0) {
978 	mtx_unlock(&Giant);
979 	return (0);
980     }
981     for (i = 0; i < ATA_PM; ++i) {
982 	if (n & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i))) {
983 	    int unit = -1;
984 
985 	    if (!(atadev = malloc(sizeof(struct ata_device),
986 				  M_ATA, M_NOWAIT | M_ZERO))) {
987 		device_printf(dev, "out of memory\n");
988 		return ENOMEM;
989 	    }
990 	    atadev->unit = i;
991 #ifdef ATA_STATIC_ID
992 	    if (n & (ATA_ATA_MASTER << i))
993 		unit = (device_get_unit(dev) << 1) + i;
994 #endif
995 	    if ((child = ata_add_child(dev, atadev, unit))) {
996 		/*
997 		 * PATA slave should be identified first, to allow
998 		 * device cable detection on master to work properly.
999 		 */
1000 		if (i == 0 && (n & ATA_PORTMULTIPLIER) == 0 &&
1001 			(n & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << 1)) != 0) {
1002 		    master = child;
1003 		    continue;
1004 		}
1005 		if (ata_getparam(atadev, 1)) {
1006 		    device_delete_child(dev, child);
1007 		    free(atadev, M_ATA);
1008 		}
1009 	    }
1010 	    else
1011 		free(atadev, M_ATA);
1012 	}
1013     }
1014     if (master) {
1015 	atadev = device_get_softc(master);
1016 	if (ata_getparam(atadev, 1)) {
1017 	    device_delete_child(dev, master);
1018 	    free(atadev, M_ATA);
1019 	}
1020     }
1021     bus_generic_probe(dev);
1022     bus_generic_attach(dev);
1023     mtx_unlock(&Giant);
1024     return 0;
1025 }
1026 #endif
1027 
1028 void
1029 ata_default_registers(device_t dev)
1030 {
1031     struct ata_channel *ch = device_get_softc(dev);
1032 
1033     /* fill in the defaults from whats setup already */
1034     ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
1035     ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
1036     ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
1037     ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
1038     ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
1039     ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
1040     ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
1041     ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
1042 }
1043 
1044 void
1045 ata_modify_if_48bit(struct ata_request *request)
1046 {
1047     struct ata_channel *ch = device_get_softc(request->parent);
1048     struct ata_device *atadev = device_get_softc(request->dev);
1049 
1050     request->flags &= ~ATA_R_48BIT;
1051 
1052     if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA ||
1053 	 request->u.ata.count > 256) &&
1054 	atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
1055 
1056 	/* translate command into 48bit version */
1057 	switch (request->u.ata.command) {
1058 	case ATA_READ:
1059 	    request->u.ata.command = ATA_READ48;
1060 	    break;
1061 	case ATA_READ_MUL:
1062 	    request->u.ata.command = ATA_READ_MUL48;
1063 	    break;
1064 	case ATA_READ_DMA:
1065 	    if (ch->flags & ATA_NO_48BIT_DMA) {
1066 		if (request->transfersize > DEV_BSIZE)
1067 		    request->u.ata.command = ATA_READ_MUL48;
1068 		else
1069 		    request->u.ata.command = ATA_READ48;
1070 		request->flags &= ~ATA_R_DMA;
1071 	    }
1072 	    else
1073 		request->u.ata.command = ATA_READ_DMA48;
1074 	    break;
1075 	case ATA_READ_DMA_QUEUED:
1076 	    if (ch->flags & ATA_NO_48BIT_DMA) {
1077 		if (request->transfersize > DEV_BSIZE)
1078 		    request->u.ata.command = ATA_READ_MUL48;
1079 		else
1080 		    request->u.ata.command = ATA_READ48;
1081 		request->flags &= ~ATA_R_DMA;
1082 	    }
1083 	    else
1084 		request->u.ata.command = ATA_READ_DMA_QUEUED48;
1085 	    break;
1086 	case ATA_WRITE:
1087 	    request->u.ata.command = ATA_WRITE48;
1088 	    break;
1089 	case ATA_WRITE_MUL:
1090 	    request->u.ata.command = ATA_WRITE_MUL48;
1091 	    break;
1092 	case ATA_WRITE_DMA:
1093 	    if (ch->flags & ATA_NO_48BIT_DMA) {
1094 		if (request->transfersize > DEV_BSIZE)
1095 		    request->u.ata.command = ATA_WRITE_MUL48;
1096 		else
1097 		    request->u.ata.command = ATA_WRITE48;
1098 		request->flags &= ~ATA_R_DMA;
1099 	    }
1100 	    else
1101 		request->u.ata.command = ATA_WRITE_DMA48;
1102 	    break;
1103 	case ATA_WRITE_DMA_QUEUED:
1104 	    if (ch->flags & ATA_NO_48BIT_DMA) {
1105 		if (request->transfersize > DEV_BSIZE)
1106 		    request->u.ata.command = ATA_WRITE_MUL48;
1107 		else
1108 		    request->u.ata.command = ATA_WRITE48;
1109 		request->u.ata.command = ATA_WRITE48;
1110 		request->flags &= ~ATA_R_DMA;
1111 	    }
1112 	    else
1113 		request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
1114 	    break;
1115 	case ATA_FLUSHCACHE:
1116 	    request->u.ata.command = ATA_FLUSHCACHE48;
1117 	    break;
1118 	case ATA_SET_MAX_ADDRESS:
1119 	    request->u.ata.command = ATA_SET_MAX_ADDRESS48;
1120 	    break;
1121 	default:
1122 	    return;
1123 	}
1124 	request->flags |= ATA_R_48BIT;
1125     }
1126     else if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
1127 
1128 	/* translate command into 48bit version */
1129 	switch (request->u.ata.command) {
1130 	case ATA_FLUSHCACHE:
1131 	    request->u.ata.command = ATA_FLUSHCACHE48;
1132 	    break;
1133 	case ATA_READ_NATIVE_MAX_ADDRESS:
1134 	    request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48;
1135 	    break;
1136 	case ATA_SET_MAX_ADDRESS:
1137 	    request->u.ata.command = ATA_SET_MAX_ADDRESS48;
1138 	    break;
1139 	default:
1140 	    return;
1141 	}
1142 	request->flags |= ATA_R_48BIT;
1143     }
1144 }
1145 
1146 void
1147 ata_udelay(int interval)
1148 {
1149     /* for now just use DELAY, the timer/sleep subsytems are not there yet */
1150     if (1 || interval < (1000000/hz) || ata_delayed_attach)
1151 	DELAY(interval);
1152     else
1153 	pause("ataslp", interval/(1000000/hz));
1154 }
1155 
1156 char *
1157 ata_unit2str(struct ata_device *atadev)
1158 {
1159     struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
1160     static char str[8];
1161 
1162     if (ch->devices & ATA_PORTMULTIPLIER)
1163 	sprintf(str, "port%d", atadev->unit);
1164     else
1165 	sprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave");
1166     return str;
1167 }
1168 
1169 const char *
1170 ata_mode2str(int mode)
1171 {
1172     switch (mode) {
1173     case -1: return "UNSUPPORTED";
1174     case ATA_PIO0: return "PIO0";
1175     case ATA_PIO1: return "PIO1";
1176     case ATA_PIO2: return "PIO2";
1177     case ATA_PIO3: return "PIO3";
1178     case ATA_PIO4: return "PIO4";
1179     case ATA_WDMA0: return "WDMA0";
1180     case ATA_WDMA1: return "WDMA1";
1181     case ATA_WDMA2: return "WDMA2";
1182     case ATA_UDMA0: return "UDMA16";
1183     case ATA_UDMA1: return "UDMA25";
1184     case ATA_UDMA2: return "UDMA33";
1185     case ATA_UDMA3: return "UDMA40";
1186     case ATA_UDMA4: return "UDMA66";
1187     case ATA_UDMA5: return "UDMA100";
1188     case ATA_UDMA6: return "UDMA133";
1189     case ATA_SA150: return "SATA150";
1190     case ATA_SA300: return "SATA300";
1191     default:
1192 	if (mode & ATA_DMA_MASK)
1193 	    return "BIOSDMA";
1194 	else
1195 	    return "BIOSPIO";
1196     }
1197 }
1198 
1199 int
1200 ata_str2mode(const char *str)
1201 {
1202 
1203 	if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
1204 	if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
1205 	if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
1206 	if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
1207 	if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
1208 	if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
1209 	if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
1210 	if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
1211 	if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
1212 	if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
1213 	if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
1214 	if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
1215 	if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
1216 	if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
1217 	if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
1218 	if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
1219 	if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
1220 	if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
1221 	if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
1222 	if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
1223 	if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
1224 	if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
1225 	return (-1);
1226 }
1227 
1228 const char *
1229 ata_satarev2str(int rev)
1230 {
1231 	switch (rev) {
1232 	case 0: return "";
1233 	case 1: return "SATA 1.5Gb/s";
1234 	case 2: return "SATA 3Gb/s";
1235 	case 3: return "SATA 6Gb/s";
1236 	case 0xff: return "SATA";
1237 	default: return "???";
1238 	}
1239 }
1240 
1241 int
1242 ata_atapi(device_t dev, int target)
1243 {
1244     struct ata_channel *ch = device_get_softc(dev);
1245 
1246     return (ch->devices & (ATA_ATAPI_MASTER << target));
1247 }
1248 
1249 int
1250 ata_pmode(struct ata_params *ap)
1251 {
1252     if (ap->atavalid & ATA_FLAG_64_70) {
1253 	if (ap->apiomodes & 0x02)
1254 	    return ATA_PIO4;
1255 	if (ap->apiomodes & 0x01)
1256 	    return ATA_PIO3;
1257     }
1258     if (ap->mwdmamodes & 0x04)
1259 	return ATA_PIO4;
1260     if (ap->mwdmamodes & 0x02)
1261 	return ATA_PIO3;
1262     if (ap->mwdmamodes & 0x01)
1263 	return ATA_PIO2;
1264     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
1265 	return ATA_PIO2;
1266     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
1267 	return ATA_PIO1;
1268     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
1269 	return ATA_PIO0;
1270     return ATA_PIO0;
1271 }
1272 
1273 int
1274 ata_wmode(struct ata_params *ap)
1275 {
1276     if (ap->mwdmamodes & 0x04)
1277 	return ATA_WDMA2;
1278     if (ap->mwdmamodes & 0x02)
1279 	return ATA_WDMA1;
1280     if (ap->mwdmamodes & 0x01)
1281 	return ATA_WDMA0;
1282     return -1;
1283 }
1284 
1285 int
1286 ata_umode(struct ata_params *ap)
1287 {
1288     if (ap->atavalid & ATA_FLAG_88) {
1289 	if (ap->udmamodes & 0x40)
1290 	    return ATA_UDMA6;
1291 	if (ap->udmamodes & 0x20)
1292 	    return ATA_UDMA5;
1293 	if (ap->udmamodes & 0x10)
1294 	    return ATA_UDMA4;
1295 	if (ap->udmamodes & 0x08)
1296 	    return ATA_UDMA3;
1297 	if (ap->udmamodes & 0x04)
1298 	    return ATA_UDMA2;
1299 	if (ap->udmamodes & 0x02)
1300 	    return ATA_UDMA1;
1301 	if (ap->udmamodes & 0x01)
1302 	    return ATA_UDMA0;
1303     }
1304     return -1;
1305 }
1306 
1307 int
1308 ata_limit_mode(device_t dev, int mode, int maxmode)
1309 {
1310     struct ata_device *atadev = device_get_softc(dev);
1311 
1312     if (maxmode && mode > maxmode)
1313 	mode = maxmode;
1314 
1315     if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
1316 	return min(mode, ata_umode(&atadev->param));
1317 
1318     if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
1319 	return min(mode, ata_wmode(&atadev->param));
1320 
1321     if (mode > ata_pmode(&atadev->param))
1322 	return min(mode, ata_pmode(&atadev->param));
1323 
1324     return mode;
1325 }
1326 
1327 static void
1328 bswap(int8_t *buf, int len)
1329 {
1330     u_int16_t *ptr = (u_int16_t*)(buf + len);
1331 
1332     while (--ptr >= (u_int16_t*)buf)
1333 	*ptr = ntohs(*ptr);
1334 }
1335 
1336 static void
1337 btrim(int8_t *buf, int len)
1338 {
1339     int8_t *ptr;
1340 
1341     for (ptr = buf; ptr < buf+len; ++ptr)
1342 	if (!*ptr || *ptr == '_')
1343 	    *ptr = ' ';
1344     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1345 	*ptr = 0;
1346 }
1347 
1348 static void
1349 bpack(int8_t *src, int8_t *dst, int len)
1350 {
1351     int i, j, blank;
1352 
1353     for (i = j = blank = 0 ; i < len; i++) {
1354 	if (blank && src[i] == ' ') continue;
1355 	if (blank && src[i] != ' ') {
1356 	    dst[j++] = src[i];
1357 	    blank = 0;
1358 	    continue;
1359 	}
1360 	if (src[i] == ' ') {
1361 	    blank = 1;
1362 	    if (i == 0)
1363 		continue;
1364 	}
1365 	dst[j++] = src[i];
1366     }
1367     if (j < len)
1368 	dst[j] = 0x00;
1369 }
1370 
1371 #ifdef ATA_CAM
1372 void
1373 ata_cam_begin_transaction(device_t dev, union ccb *ccb)
1374 {
1375 	struct ata_channel *ch = device_get_softc(dev);
1376 	struct ata_request *request;
1377 
1378 	if (!(request = ata_alloc_request())) {
1379 		device_printf(dev, "FAILURE - out of memory in start\n");
1380 		ccb->ccb_h.status = CAM_REQ_INVALID;
1381 		xpt_done(ccb);
1382 		return;
1383 	}
1384 	bzero(request, sizeof(*request));
1385 
1386 	/* setup request */
1387 	request->dev = NULL;
1388 	request->parent = dev;
1389 	request->unit = ccb->ccb_h.target_id;
1390 	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1391 		request->data = ccb->ataio.data_ptr;
1392 		request->bytecount = ccb->ataio.dxfer_len;
1393 		request->u.ata.command = ccb->ataio.cmd.command;
1394 		request->u.ata.feature = ((uint16_t)ccb->ataio.cmd.features_exp << 8) |
1395 					  (uint16_t)ccb->ataio.cmd.features;
1396 		request->u.ata.count = ((uint16_t)ccb->ataio.cmd.sector_count_exp << 8) |
1397 					(uint16_t)ccb->ataio.cmd.sector_count;
1398 		if (ccb->ataio.cmd.flags & CAM_ATAIO_48BIT) {
1399 			request->flags |= ATA_R_48BIT;
1400 			request->u.ata.lba =
1401 				     ((uint64_t)ccb->ataio.cmd.lba_high_exp << 40) |
1402 				     ((uint64_t)ccb->ataio.cmd.lba_mid_exp << 32) |
1403 				     ((uint64_t)ccb->ataio.cmd.lba_low_exp << 24);
1404 		} else {
1405 			request->u.ata.lba =
1406 				     ((uint64_t)(ccb->ataio.cmd.device & 0x0f) << 24);
1407 		}
1408 		request->u.ata.lba |= ((uint64_t)ccb->ataio.cmd.lba_high << 16) |
1409 				      ((uint64_t)ccb->ataio.cmd.lba_mid << 8) |
1410 				       (uint64_t)ccb->ataio.cmd.lba_low;
1411 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1412 		    ccb->ataio.cmd.flags & CAM_ATAIO_DMA)
1413 			request->flags |= ATA_R_DMA;
1414 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1415 			request->flags |= ATA_R_READ;
1416 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1417 			request->flags |= ATA_R_WRITE;
1418 	} else {
1419 		request->data = ccb->csio.data_ptr;
1420 		request->bytecount = ccb->csio.dxfer_len;
1421 		bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1422 		    ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1423 		    request->u.atapi.ccb, ccb->csio.cdb_len);
1424 		request->flags |= ATA_R_ATAPI;
1425 		if (ch->curr[ccb->ccb_h.target_id].atapi == 16)
1426 			request->flags |= ATA_R_ATAPI16;
1427 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1428 		    ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1429 			request->flags |= ATA_R_DMA;
1430 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1431 			request->flags |= ATA_R_READ;
1432 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1433 			request->flags |= ATA_R_WRITE;
1434 	}
1435 	request->transfersize = min(request->bytecount,
1436 	    ch->curr[ccb->ccb_h.target_id].bytecount);
1437 	request->retries = 0;
1438 	request->timeout = (ccb->ccb_h.timeout + 999) / 1000;
1439 	callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED);
1440 	request->ccb = ccb;
1441 
1442 	ch->running = request;
1443 	ch->state = ATA_ACTIVE;
1444 	if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
1445 	    ch->running = NULL;
1446 	    ch->state = ATA_IDLE;
1447 	    ata_cam_end_transaction(dev, request);
1448 	    return;
1449 	}
1450 }
1451 
1452 void
1453 ata_cam_end_transaction(device_t dev, struct ata_request *request)
1454 {
1455 	struct ata_channel *ch = device_get_softc(dev);
1456 	union ccb *ccb = request->ccb;
1457 	int fatalerr = 0;
1458 
1459 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1460 	if (request->flags & ATA_R_TIMEOUT) {
1461 		xpt_freeze_simq(ch->sim, 1);
1462 		ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1463 		ccb->ccb_h.status |= CAM_CMD_TIMEOUT | CAM_RELEASE_SIMQ;
1464 		fatalerr = 1;
1465 	} else if (request->status & ATA_S_ERROR) {
1466 		if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1467 			ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1468 		} else {
1469 			ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1470 			ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1471 		}
1472 	} else if (request->result == ERESTART)
1473 		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1474 	else if (request->result != 0)
1475 		ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1476 	else
1477 		ccb->ccb_h.status |= CAM_REQ_CMP;
1478 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP &&
1479 	    !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1480 		xpt_freeze_devq(ccb->ccb_h.path, 1);
1481 		ccb->ccb_h.status |= CAM_DEV_QFRZN;
1482 	}
1483 	if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1484 	    ((request->status & ATA_S_ERROR) ||
1485 	    (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT))) {
1486 		struct ata_res *res = &ccb->ataio.res;
1487 		res->status = request->status;
1488 		res->error = request->error;
1489 		res->lba_low = request->u.ata.lba;
1490 		res->lba_mid = request->u.ata.lba >> 8;
1491 		res->lba_high = request->u.ata.lba >> 16;
1492 		res->device = request->u.ata.lba >> 24;
1493 		res->lba_low_exp = request->u.ata.lba >> 24;
1494 		res->lba_mid_exp = request->u.ata.lba >> 32;
1495 		res->lba_high_exp = request->u.ata.lba >> 40;
1496 		res->sector_count = request->u.ata.count;
1497 		res->sector_count_exp = request->u.ata.count >> 8;
1498 	}
1499 	ata_free_request(request);
1500 	xpt_done(ccb);
1501 	/* Do error recovery if needed. */
1502 	if (fatalerr)
1503 		ata_reinit(dev);
1504 }
1505 
1506 static int
1507 ata_check_ids(device_t dev, union ccb *ccb)
1508 {
1509 	struct ata_channel *ch = device_get_softc(dev);
1510 
1511 	if (ccb->ccb_h.target_id > ((ch->flags & ATA_NO_SLAVE) ? 0 : 1)) {
1512 		ccb->ccb_h.status = CAM_TID_INVALID;
1513 		xpt_done(ccb);
1514 		return (-1);
1515 	}
1516 	if (ccb->ccb_h.target_lun != 0) {
1517 		ccb->ccb_h.status = CAM_LUN_INVALID;
1518 		xpt_done(ccb);
1519 		return (-1);
1520 	}
1521 	return (0);
1522 }
1523 
1524 static void
1525 ataaction(struct cam_sim *sim, union ccb *ccb)
1526 {
1527 	device_t dev, parent;
1528 	struct ata_channel *ch;
1529 
1530 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ataaction func_code=%x\n",
1531 	    ccb->ccb_h.func_code));
1532 
1533 	ch = (struct ata_channel *)cam_sim_softc(sim);
1534 	dev = ch->dev;
1535 	switch (ccb->ccb_h.func_code) {
1536 	/* Common cases first */
1537 	case XPT_ATA_IO:	/* Execute the requested I/O operation */
1538 	case XPT_SCSI_IO:
1539 		if (ata_check_ids(dev, ccb))
1540 			return;
1541 		if ((ch->devices & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER)
1542 		    << ccb->ccb_h.target_id)) == 0) {
1543 			ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1544 			break;
1545 		}
1546 		if (ch->running)
1547 			device_printf(dev, "already running!\n");
1548 		if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1549 		    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1550 		    (ccb->ataio.cmd.control & ATA_A_RESET)) {
1551 			struct ata_res *res = &ccb->ataio.res;
1552 
1553 			bzero(res, sizeof(*res));
1554 			if (ch->devices & (ATA_ATA_MASTER << ccb->ccb_h.target_id)) {
1555 				res->lba_high = 0;
1556 				res->lba_mid = 0;
1557 			} else {
1558 				res->lba_high = 0xeb;
1559 				res->lba_mid = 0x14;
1560 			}
1561 			ccb->ccb_h.status = CAM_REQ_CMP;
1562 			break;
1563 		}
1564 		ata_cam_begin_transaction(dev, ccb);
1565 		return;
1566 	case XPT_EN_LUN:		/* Enable LUN as a target */
1567 	case XPT_TARGET_IO:		/* Execute target I/O request */
1568 	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
1569 	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
1570 	case XPT_ABORT:			/* Abort the specified CCB */
1571 		/* XXX Implement */
1572 		ccb->ccb_h.status = CAM_REQ_INVALID;
1573 		break;
1574 	case XPT_SET_TRAN_SETTINGS:
1575 	{
1576 		struct	ccb_trans_settings *cts = &ccb->cts;
1577 		struct	ata_cam_device *d;
1578 
1579 		if (ata_check_ids(dev, ccb))
1580 			return;
1581 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1582 			d = &ch->curr[ccb->ccb_h.target_id];
1583 		else
1584 			d = &ch->user[ccb->ccb_h.target_id];
1585 		if (ch->flags & ATA_SATA) {
1586 			if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
1587 				d->revision = cts->xport_specific.sata.revision;
1588 			if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE) {
1589 				if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1590 					d->mode = ATA_SETMODE(ch->dev,
1591 					    ccb->ccb_h.target_id,
1592 					    cts->xport_specific.sata.mode);
1593 				} else
1594 					d->mode = cts->xport_specific.sata.mode;
1595 			}
1596 			if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
1597 				d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
1598 			if (cts->xport_specific.sata.valid & CTS_SATA_VALID_ATAPI)
1599 				d->atapi = cts->xport_specific.sata.atapi;
1600 		} else {
1601 			if (cts->xport_specific.ata.valid & CTS_ATA_VALID_MODE) {
1602 				if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1603 					d->mode = ATA_SETMODE(ch->dev,
1604 					    ccb->ccb_h.target_id,
1605 					    cts->xport_specific.ata.mode);
1606 				} else
1607 					d->mode = cts->xport_specific.ata.mode;
1608 			}
1609 			if (cts->xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
1610 				d->bytecount = cts->xport_specific.ata.bytecount;
1611 			if (cts->xport_specific.ata.valid & CTS_ATA_VALID_ATAPI)
1612 				d->atapi = cts->xport_specific.ata.atapi;
1613 		}
1614 		ccb->ccb_h.status = CAM_REQ_CMP;
1615 		break;
1616 	}
1617 	case XPT_GET_TRAN_SETTINGS:
1618 	{
1619 		struct	ccb_trans_settings *cts = &ccb->cts;
1620 		struct  ata_cam_device *d;
1621 
1622 		if (ata_check_ids(dev, ccb))
1623 			return;
1624 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1625 			d = &ch->curr[ccb->ccb_h.target_id];
1626 		else
1627 			d = &ch->user[ccb->ccb_h.target_id];
1628 		cts->protocol = PROTO_ATA;
1629 		cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1630 		if (ch->flags & ATA_SATA) {
1631 			cts->transport = XPORT_SATA;
1632 			cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1633 			cts->xport_specific.sata.valid = 0;
1634 			cts->xport_specific.sata.mode = d->mode;
1635 			cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
1636 			cts->xport_specific.sata.bytecount = d->bytecount;
1637 			cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
1638 			if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1639 				cts->xport_specific.sata.revision =
1640 				    ATA_GETREV(dev, ccb->ccb_h.target_id);
1641 				if (cts->xport_specific.sata.revision != 0xff) {
1642 					cts->xport_specific.sata.valid |=
1643 					    CTS_SATA_VALID_REVISION;
1644 				}
1645 			} else {
1646 				cts->xport_specific.sata.revision = d->revision;
1647 				cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
1648 			}
1649 			cts->xport_specific.sata.atapi = d->atapi;
1650 			cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
1651 		} else {
1652 			cts->transport = XPORT_ATA;
1653 			cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1654 			cts->xport_specific.ata.valid = 0;
1655 			cts->xport_specific.ata.mode = d->mode;
1656 			cts->xport_specific.ata.valid |= CTS_ATA_VALID_MODE;
1657 			cts->xport_specific.ata.bytecount = d->bytecount;
1658 			cts->xport_specific.ata.valid |= CTS_ATA_VALID_BYTECOUNT;
1659 			cts->xport_specific.ata.atapi = d->atapi;
1660 			cts->xport_specific.ata.valid |= CTS_ATA_VALID_ATAPI;
1661 		}
1662 		ccb->ccb_h.status = CAM_REQ_CMP;
1663 		break;
1664 	}
1665 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
1666 	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
1667 		ata_reinit(dev);
1668 		ccb->ccb_h.status = CAM_REQ_CMP;
1669 		break;
1670 	case XPT_TERM_IO:		/* Terminate the I/O process */
1671 		/* XXX Implement */
1672 		ccb->ccb_h.status = CAM_REQ_INVALID;
1673 		break;
1674 	case XPT_PATH_INQ:		/* Path routing inquiry */
1675 	{
1676 		struct ccb_pathinq *cpi = &ccb->cpi;
1677 
1678 		parent = device_get_parent(dev);
1679 		cpi->version_num = 1; /* XXX??? */
1680 		cpi->hba_inquiry = PI_SDTR_ABLE;
1681 		cpi->target_sprt = 0;
1682 		cpi->hba_misc = PIM_SEQSCAN;
1683 		cpi->hba_eng_cnt = 0;
1684 		if (ch->flags & ATA_NO_SLAVE)
1685 			cpi->max_target = 0;
1686 		else
1687 			cpi->max_target = 1;
1688 		cpi->max_lun = 0;
1689 		cpi->initiator_id = 0;
1690 		cpi->bus_id = cam_sim_bus(sim);
1691 		if (ch->flags & ATA_SATA)
1692 			cpi->base_transfer_speed = 150000;
1693 		else
1694 			cpi->base_transfer_speed = 3300;
1695 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1696 		strncpy(cpi->hba_vid, "ATA", HBA_IDLEN);
1697 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1698 		cpi->unit_number = cam_sim_unit(sim);
1699 		if (ch->flags & ATA_SATA)
1700 			cpi->transport = XPORT_SATA;
1701 		else
1702 			cpi->transport = XPORT_ATA;
1703 		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1704 		cpi->protocol = PROTO_ATA;
1705 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
1706 		cpi->maxio = ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS;
1707 		if (device_get_devclass(device_get_parent(parent)) ==
1708 		    devclass_find("pci")) {
1709 			cpi->hba_vendor = pci_get_vendor(parent);
1710 			cpi->hba_device = pci_get_device(parent);
1711 			cpi->hba_subvendor = pci_get_subvendor(parent);
1712 			cpi->hba_subdevice = pci_get_subdevice(parent);
1713 		}
1714 		cpi->ccb_h.status = CAM_REQ_CMP;
1715 		break;
1716 	}
1717 	default:
1718 		ccb->ccb_h.status = CAM_REQ_INVALID;
1719 		break;
1720 	}
1721 	xpt_done(ccb);
1722 }
1723 
1724 static void
1725 atapoll(struct cam_sim *sim)
1726 {
1727 	struct ata_channel *ch = (struct ata_channel *)cam_sim_softc(sim);
1728 
1729 	ata_interrupt_locked(ch);
1730 }
1731 #endif
1732 
1733 /*
1734  * module handeling
1735  */
1736 static int
1737 ata_module_event_handler(module_t mod, int what, void *arg)
1738 {
1739 #ifndef ATA_CAM
1740     static struct cdev *atacdev;
1741 #endif
1742 
1743     switch (what) {
1744     case MOD_LOAD:
1745 #ifndef ATA_CAM
1746 	/* register controlling device */
1747 	atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1748 
1749 	if (cold) {
1750 	    /* register boot attach to be run when interrupts are enabled */
1751 	    if (!(ata_delayed_attach = (struct intr_config_hook *)
1752 				       malloc(sizeof(struct intr_config_hook),
1753 					      M_TEMP, M_NOWAIT | M_ZERO))) {
1754 		printf("ata: malloc of delayed attach hook failed\n");
1755 		return EIO;
1756 	    }
1757 	    ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1758 	    if (config_intrhook_establish(ata_delayed_attach) != 0) {
1759 		printf("ata: config_intrhook_establish failed\n");
1760 		free(ata_delayed_attach, M_TEMP);
1761 	    }
1762 	}
1763 #endif
1764 	return 0;
1765 
1766     case MOD_UNLOAD:
1767 #ifndef ATA_CAM
1768 	/* deregister controlling device */
1769 	destroy_dev(atacdev);
1770 #endif
1771 	return 0;
1772 
1773     default:
1774 	return EOPNOTSUPP;
1775     }
1776 }
1777 
1778 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1779 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1780 MODULE_VERSION(ata, 1);
1781 #ifdef ATA_CAM
1782 MODULE_DEPEND(ata, cam, 1, 1, 1);
1783 #endif
1784 
1785 static void
1786 ata_init(void)
1787 {
1788     ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
1789 				   NULL, NULL, NULL, NULL, 0, 0);
1790     ata_composite_zone = uma_zcreate("ata_composite",
1791 				     sizeof(struct ata_composite),
1792 				     NULL, NULL, NULL, NULL, 0, 0);
1793 }
1794 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1795 
1796 static void
1797 ata_uninit(void)
1798 {
1799     uma_zdestroy(ata_composite_zone);
1800     uma_zdestroy(ata_request_zone);
1801 }
1802 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);
1803