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