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