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