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