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