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