Implement kern.do_async_attach. default disabled. To enable add
[dragonfly.git] / sys / dev / disk / ata / ata-all.c
1 /*-
2  * Copyright (c) 1998,1999,2000,2001,2002 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  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/dev/ata/ata-all.c,v 1.50.2.45 2003/03/12 14:47:12 sos Exp $
29  * $DragonFly: src/sys/dev/disk/ata/ata-all.c,v 1.36 2007/05/01 00:05:16 dillon Exp $
30  */
31
32 #include "opt_ata.h"
33 #include "use_ata.h"
34 #include "use_atadisk.h"
35 #include "use_atapicd.h"
36 #include "use_atapifd.h"
37 #include "use_atapist.h"
38 #include "use_atapicam.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/ata.h>
42 #include <sys/kernel.h>
43 #include <sys/conf.h>
44 #include <sys/disk.h>
45 #include <sys/module.h>
46 #include <sys/bus.h>
47 #include <sys/buf.h>
48 #include <sys/malloc.h>
49 #include <sys/devicestat.h>
50 #include <sys/sysctl.h>
51 #include <sys/thread2.h>
52 #include <sys/rman.h>
53
54 #include <machine/stdarg.h>
55 #include <machine/clock.h>
56
57 #include "ata-all.h"
58 #include "ata-disk.h"
59 #include "ata-raid.h"
60 #include "atapi-all.h"
61
62 union ata_request {
63         struct ad_request       ad;
64         struct atapi_request    atapi;
65 };
66
67 /* device structures */
68 static  d_ioctl_t       ataioctl;
69 static struct dev_ops ata_ops = {  
70         { "ata", 159, 0 },
71         .d_open =       nullopen,
72         .d_close =      nullclose,
73         .d_ioctl =      ataioctl,
74 };
75
76 /* prototypes */
77 static void ata_boot_attach(void);
78 static void ata_intr(void *);
79 static int ata_getparam(struct ata_device *, u_int8_t);
80 static int ata_service(struct ata_channel *);
81 static void bswap(int8_t *, int);
82 static void btrim(int8_t *, int);
83 static void bpack(int8_t *, int8_t *, int);
84 static void ata_change_mode(struct ata_device *, int);
85 static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t);
86 static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *);
87
88 /* sysctl vars */
89 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
90
91 int ata_mpipe_size = 4;
92 TUNABLE_INT("hw.ata.mpipe_size", &ata_mpipe_size);
93 SYSCTL_INT(_hw_ata, OID_AUTO, mpipe_size, CTLFLAG_RW, &ata_mpipe_size, 0,
94            "ATA global I/O pipeline max size");
95
96
97 /* global vars */
98 devclass_t ata_devclass;
99
100 /* local vars */
101 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
102
103 /* misc defines */
104 #define DEV_ATAPIALL    NATAPICD > 0 || NATAPIFD > 0 || \
105                         NATAPIST > 0 || NATAPICAM > 0
106
107 int
108 ata_probe(device_t dev)
109 {
110     struct ata_channel *ch;
111     int rid;
112
113     if (!dev || !(ch = device_get_softc(dev)))
114         return ENXIO;
115
116     if (ch->r_io || ch->r_altio || ch->r_irq)
117         return EEXIST;
118
119     /* initialize the softc basics */
120     ch->active = ATA_IDLE;
121     ch->dev = dev;
122
123     rid = ATA_IOADDR_RID;
124     ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 
125                                   ATA_IOSIZE, RF_ACTIVE);
126     if (!ch->r_io)
127         goto failure;
128
129     rid = ATA_ALTADDR_RID;
130     ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
131                                      ATA_ALTIOSIZE, RF_ACTIVE);
132     if (!ch->r_altio)
133         goto failure;
134
135     rid = ATA_BMADDR_RID;
136     ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
137                                     ATA_BMIOSIZE, RF_ACTIVE);
138     if (bootverbose)
139         ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n", 
140                    (int)rman_get_start(ch->r_io),
141                    (int)rman_get_start(ch->r_altio),
142                    (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
143
144     ata_reset(ch);
145
146     ch->device[MASTER].channel = ch;
147     ch->device[MASTER].unit = ATA_MASTER;
148     ch->device[MASTER].mode = ATA_PIO;
149     ch->device[SLAVE].channel = ch;
150     ch->device[SLAVE].unit = ATA_SLAVE;
151     ch->device[SLAVE].mode = ATA_PIO;
152     TAILQ_INIT(&ch->ata_queue);
153     TAILQ_INIT(&ch->atapi_queue);
154
155     mpipe_init(&ch->req_mpipe, M_ATA, sizeof(union ata_request), 
156                 4, ata_mpipe_size, 0, NULL);
157     mpipe_init(&ch->dma_mpipe, M_DEVBUF, PAGE_SIZE, 
158                 4, ata_mpipe_size, MPF_NOZERO, NULL);
159
160     return 0;
161     
162 failure:
163     if (ch->r_io)
164         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
165     if (ch->r_altio)
166         bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
167     if (ch->r_bmio)
168         bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
169     if (bootverbose)
170         ata_printf(ch, -1, "probe allocation failed\n");
171     return ENXIO;
172 }
173
174 int
175 ata_attach(device_t dev)
176 {
177     struct ata_channel *ch;
178     int error, rid;
179
180     if (!dev || !(ch = device_get_softc(dev)))
181         return ENXIO;
182
183     rid = ATA_IRQ_RID;
184     ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
185                                    RF_SHAREABLE | RF_ACTIVE);
186     if (!ch->r_irq) {
187         ata_printf(ch, -1, "unable to allocate interrupt\n");
188         return ENXIO;
189     }
190
191     /*
192      * Traditional ata registers are sensitive to when they can be accessed
193      * in the face of e.g. ongoing DMA.  Do not allow the interrupt to be
194      * polled.
195      */
196     if ((error = bus_setup_intr(dev, ch->r_irq, INTR_NOPOLL,
197                                 ata_intr, ch, &ch->ih, NULL))) {
198         ata_printf(ch, -1, "unable to setup interrupt\n");
199         return error;
200     }
201
202     /*
203      * do not attach devices if we are in early boot, this is done later 
204      * when interrupts are enabled by a hook into the boot process.
205      * otherwise attach what the probe has found in ch->devices.
206      */
207     crit_enter();
208
209     if (ch->devices & ATA_ATA_SLAVE)
210         if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
211             ch->devices &= ~ATA_ATA_SLAVE;
212     if (ch->devices & ATA_ATAPI_SLAVE)
213         if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
214             ch->devices &= ~ATA_ATAPI_SLAVE;
215     if (ch->devices & ATA_ATA_MASTER)
216         if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
217             ch->devices &= ~ATA_ATA_MASTER;
218     if (ch->devices & ATA_ATAPI_MASTER)
219         if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
220             ch->devices &= ~ATA_ATAPI_MASTER;
221 #if NATADISK > 0
222     if (ch->devices & ATA_ATA_MASTER)
223         ad_attach(&ch->device[MASTER], 0);
224     if (ch->devices & ATA_ATA_SLAVE)
225         ad_attach(&ch->device[SLAVE], 0);
226 #endif
227 #if DEV_ATAPIALL
228     if (ch->devices & ATA_ATAPI_MASTER)
229         atapi_attach(&ch->device[MASTER], 0);
230     if (ch->devices & ATA_ATAPI_SLAVE)
231         atapi_attach(&ch->device[SLAVE], 0);
232 #endif
233 #if NATAPICAM > 0
234     atapi_cam_attach_bus(ch);
235 #endif
236     return 0;
237 }
238
239 int
240 ata_detach(device_t dev)
241 {
242     struct ata_channel *ch;
243  
244     if (!dev || !(ch = device_get_softc(dev)) ||
245         !ch->r_io || !ch->r_altio || !ch->r_irq)
246         return ENXIO;
247
248     /* make sure channel is not busy */
249     crit_enter();
250     ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
251 #if NATADISK > 0
252     if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
253         ad_detach(&ch->device[MASTER], 1);
254     if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
255         ad_detach(&ch->device[SLAVE], 1);
256 #endif
257 #if DEV_ATAPIALL
258     if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
259         atapi_detach(&ch->device[MASTER]);
260     if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
261         atapi_detach(&ch->device[SLAVE]);
262 #endif
263 #if NATAPICAM > 0
264     atapi_cam_detach_bus(ch);
265 #endif
266     crit_exit();
267
268     if (ch->device[MASTER].param) {
269         kfree(ch->device[MASTER].param, M_ATA);
270         ch->device[MASTER].param = NULL;
271     }
272     if (ch->device[SLAVE].param) {
273         kfree(ch->device[SLAVE].param, M_ATA);
274         ch->device[SLAVE].param = NULL;
275     }
276     ch->device[MASTER].driver = NULL;
277     ch->device[SLAVE].driver = NULL;
278     ch->device[MASTER].mode = ATA_PIO;
279     ch->device[SLAVE].mode = ATA_PIO;
280     ch->devices = 0;
281     ata_dmafreetags(ch);
282
283     bus_teardown_intr(dev, ch->r_irq, ch->ih);
284     bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
285     if (ch->r_bmio)
286         bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
287     bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
288     bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
289     ch->r_io = NULL;
290     ch->r_altio = NULL;
291     ch->r_bmio = NULL;
292     ch->r_irq = NULL;
293     mpipe_done(&ch->req_mpipe);
294     mpipe_done(&ch->dma_mpipe);
295
296     ATA_UNLOCK_CH(ch);
297     return 0;
298 }
299
300 int
301 ata_suspend(device_t dev)
302 {
303     struct ata_channel *ch;
304
305     if (dev == NULL || (ch = device_get_softc(dev)) == NULL)
306         return ENXIO;
307
308     /* wait for the channel to be IDLE or detached before suspending */
309     while (ch->r_irq) {
310         crit_enter();
311         if (ch->active == ATA_IDLE) {
312             ch->active = ATA_CONTROL;
313             crit_exit();
314             break;
315         }
316         crit_exit();
317         tsleep(ch, 0, "atasusp", hz / 10);
318     }
319     return 0;
320 }
321
322 int
323 ata_resume(device_t dev)
324 {
325     return ata_reinit(device_get_softc(dev));
326 }
327
328 static int
329 ataioctl(struct dev_ioctl_args *ap)
330 {
331     struct ata_cmd *iocmd = (struct ata_cmd *)ap->a_data;
332     struct ata_channel *ch;
333     device_t device = devclass_get_device(ata_devclass, iocmd->channel);
334     int error;
335
336     if (ap->a_cmd != IOCATA)
337         return ENOTTY;
338     
339     if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
340         return ENXIO;
341
342     switch (iocmd->cmd) {
343         case ATAATTACH:
344             /* should enable channel HW on controller that can SOS XXX */   
345             error = ata_probe(device);
346             if (!error)
347                 error = ata_attach(device);
348             return error;
349
350         case ATADETACH:
351             error = ata_detach(device);
352             /* should disable channel HW on controller that can SOS XXX */   
353             return error;
354
355         case ATAREINIT:
356             if (!device || !(ch = device_get_softc(device)))
357                 return ENXIO;
358             crit_enter();       /* interlock non-atomic channel lock */
359             ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
360             if ((error = ata_reinit(ch)))
361                 ATA_UNLOCK_CH(ch);
362             crit_exit();
363             return error;
364
365         case ATAGMODE:
366             if (!device || !(ch = device_get_softc(device)))
367                 return ENXIO;
368
369             if ((iocmd->device == MASTER || iocmd->device == -1) &&
370                 ch->device[MASTER].driver)
371                 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
372             else
373                 iocmd->u.mode.mode[MASTER] = -1;
374
375             if ((iocmd->device == SLAVE || iocmd->device == -1) &&
376                 ch->device[SLAVE].param)
377                 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
378             else
379                 iocmd->u.mode.mode[SLAVE] = -1;
380             return 0;
381
382         case ATASMODE:
383             if (!device || !(ch = device_get_softc(device)))
384                 return ENXIO;
385
386             if ((iocmd->device == MASTER || iocmd->device == -1) &&
387                 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
388                 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
389                 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
390             }
391             else
392                 iocmd->u.mode.mode[MASTER] = -1;
393
394             if ((iocmd->device == SLAVE || iocmd->device == -1) &&
395                 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
396                 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
397                 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
398             }
399             else
400                 iocmd->u.mode.mode[SLAVE] = -1;
401             return 0;
402
403         case ATAGPARM:
404             if (!device || !(ch = device_get_softc(device)))
405                 return ENXIO;
406
407             iocmd->u.param.type[MASTER] = 
408                 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
409             iocmd->u.param.type[SLAVE] =
410                 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
411
412             if (ch->device[MASTER].name)
413                 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
414             if (ch->device[SLAVE].name)
415                 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
416
417             if (ch->device[MASTER].param)
418                 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
419                       sizeof(struct ata_params));
420             if (ch->device[SLAVE].param)
421                 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
422                       sizeof(struct ata_params));
423             return 0;
424
425         case ATAENCSTAT: {
426             struct ata_device *atadev;
427
428             if (!device || !(ch = device_get_softc(device)))
429                 return ENXIO;
430
431             if (iocmd->device == SLAVE)
432                 atadev = &ch->device[SLAVE];
433             else
434                 atadev = &ch->device[MASTER];
435
436             return ata_enclosure_status(atadev,
437                                         &iocmd->u.enclosure.fan,
438                                         &iocmd->u.enclosure.temp,
439                                         &iocmd->u.enclosure.v05,
440                                         &iocmd->u.enclosure.v12);
441         }
442
443 #if NATADISK > 0
444         case ATARAIDREBUILD:
445             return ata_raid_rebuild(iocmd->channel);
446
447         case ATARAIDCREATE:
448             return ata_raid_create(&iocmd->u.raid_setup);
449
450         case ATARAIDDELETE:
451             return ata_raid_delete(iocmd->channel);
452
453         case ATARAIDSTATUS:
454             return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
455 #endif
456 #if DEV_ATAPIALL
457         case ATAPICMD: {
458             struct ata_device *atadev;
459             caddr_t buf;
460
461             if (!device || !(ch = device_get_softc(device)))
462                 return ENXIO;
463
464             if (!(atadev = &ch->device[iocmd->device]) ||
465                 !(ch->devices & (iocmd->device == MASTER ?
466                                  ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
467                 return ENODEV;
468
469             buf = kmalloc(iocmd->u.atapi.count, M_ATA, M_INTWAIT);
470
471             if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
472                 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
473                 if (error)
474                     return error;
475             }
476             error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
477                                     buf, iocmd->u.atapi.count,
478                                     (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
479                                      ATPR_F_READ : 0) | ATPR_F_QUIET, 
480                                     iocmd->u.atapi.timeout, NULL, NULL);
481             if (error) {
482                 iocmd->u.atapi.error = error;
483                 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
484                       sizeof(struct atapi_reqsense));
485                 error = 0;
486             }
487             else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
488                 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
489
490             kfree(buf, M_ATA);
491             return error;
492         }
493 #endif
494         default:
495             break;
496     }
497     return ENOTTY;
498 }
499
500 static int
501 ata_getparam(struct ata_device *atadev, u_int8_t command)
502 {
503     struct ata_params *ata_parm;
504     int retry = 0;
505
506     ata_parm = kmalloc(sizeof(struct ata_params), M_ATA, M_INTWAIT);
507
508     /* apparently some devices needs this repeated */
509     do {
510         if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
511             ata_prtdev(atadev, "%s identify failed\n",
512                        command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
513             kfree(ata_parm, M_ATA);
514             return -1;
515         }
516         if (retry++ > 4) {
517             ata_prtdev(atadev, "%s identify retries exceeded\n",
518                        command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
519             kfree(ata_parm, M_ATA);
520             return -1;
521         }
522     } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
523                                ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
524     ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
525              sizeof(struct ata_params)/sizeof(int16_t));
526
527     if (command == ATA_C_ATA_IDENTIFY ||
528         !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
529           (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
530           (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
531         bswap(ata_parm->model, sizeof(ata_parm->model));
532     btrim(ata_parm->model, sizeof(ata_parm->model));
533     bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
534     bswap(ata_parm->revision, sizeof(ata_parm->revision));
535     btrim(ata_parm->revision, sizeof(ata_parm->revision));
536     bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
537     bswap(ata_parm->serial, sizeof(ata_parm->serial));
538     btrim(ata_parm->serial, sizeof(ata_parm->serial));
539     bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
540     atadev->param = ata_parm;
541     return 0;
542 }
543
544 static void 
545 ata_boot_attach(void)
546 {
547     struct ata_channel *ch;
548     int ctlr;
549
550     crit_enter();
551
552     /*
553      * run through all ata devices and look for real ATA & ATAPI devices
554      * using the hints we found in the early probe, this avoids some of
555      * the delays probing of non-exsistent devices can cause.
556      */
557     for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
558         if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
559             continue;
560         if (ch->devices & ATA_ATA_SLAVE)
561             if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
562                 ch->devices &= ~ATA_ATA_SLAVE;
563         if (ch->devices & ATA_ATAPI_SLAVE)
564             if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
565                 ch->devices &= ~ATA_ATAPI_SLAVE;
566         if (ch->devices & ATA_ATA_MASTER)
567             if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
568                 ch->devices &= ~ATA_ATA_MASTER;
569         if (ch->devices & ATA_ATAPI_MASTER)
570             if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
571                 ch->devices &= ~ATA_ATAPI_MASTER;
572     }
573
574 #if NATADISK > 0
575     /* now we know whats there, do the real attach, first the ATA disks */
576     for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
577         if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
578             continue;
579         if (ch->devices & ATA_ATA_MASTER)
580             ad_attach(&ch->device[MASTER], 0);
581         if (ch->devices & ATA_ATA_SLAVE)
582             ad_attach(&ch->device[SLAVE], 0);
583     }
584     ata_raid_attach();
585 #endif
586 #if DEV_ATAPIALL
587     /* then the atapi devices */
588     for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
589         if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
590             continue;
591         if (ch->devices & ATA_ATAPI_MASTER)
592             atapi_attach(&ch->device[MASTER], 0);
593         if (ch->devices & ATA_ATAPI_SLAVE)
594             atapi_attach(&ch->device[SLAVE], 0);
595 #if NATAPICAM > 0
596         atapi_cam_attach_bus(ch);
597 #endif
598     }
599 #endif
600     crit_exit();
601 }
602
603 static void
604 ata_intr(void *data)
605 {
606     struct ata_channel *ch = (struct ata_channel *)data;
607     /* 
608      * on PCI systems we might share an interrupt line with another
609      * device or our twin ATA channel, so call ch->intr_func to figure 
610      * out if it is really an interrupt we should process here
611      */
612     if (ch->intr_func && ch->intr_func(ch))
613         return;
614
615     /* if drive is busy it didn't interrupt */
616     if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
617         DELAY(100);
618         if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
619             return;
620     }
621
622     /* clear interrupt and get status */
623     ch->status = ATA_INB(ch->r_io, ATA_STATUS);
624
625     if (ch->status & ATA_S_ERROR)
626         ch->error = ATA_INB(ch->r_io, ATA_ERROR);
627
628     /* find & call the responsible driver to process this interrupt */
629     switch (ch->active) {
630 #if NATADISK > 0
631     case ATA_ACTIVE_ATA:
632         if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
633             return;
634         break;
635 #endif
636 #if DEV_ATAPIALL
637     case ATA_ACTIVE_ATAPI:
638         if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
639             return;
640         break;
641 #endif
642     case ATA_WAIT_INTR:
643     case ATA_WAIT_INTR | ATA_CONTROL:
644         wakeup((caddr_t)ch);
645         break;
646
647     case ATA_WAIT_READY:
648     case ATA_WAIT_READY | ATA_CONTROL:
649         break;
650
651     case ATA_IDLE:
652         if (ch->flags & ATA_QUEUED) {
653             ch->active = ATA_ACTIVE;
654             if (ata_service(ch) == ATA_OP_CONTINUES)
655                 return;
656         }
657         /* FALLTHROUGH */
658
659     default:
660 #ifdef ATA_DEBUG
661     {
662         static int intr_count = 0;
663
664         if (intr_count++ < 10)
665             ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
666                        intr_count, ch->active, ch->status);
667     }
668 #endif
669         break;
670     }
671     ch->active &= ATA_CONTROL;
672     if (ch->active & ATA_CONTROL)
673         return;
674     ch->running = NULL;
675     ata_start(ch);
676     return;
677 }
678
679 void
680 ata_start(struct ata_channel *ch)
681 {
682 #if NATADISK > 0
683     struct ad_request *ad_request; 
684 #endif
685 #if DEV_ATAPIALL
686     struct atapi_request *atapi_request;
687 #endif
688
689     crit_enter();       /* interlock non-atomic channel lock */
690     if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) {
691         crit_exit();
692         return;
693     }
694
695 #if NATADISK > 0
696     /* find & call the responsible driver if anything on the ATA queue */
697     if (TAILQ_EMPTY(&ch->ata_queue)) {
698         if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
699             ad_start(&ch->device[MASTER]);
700         if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
701             ad_start(&ch->device[SLAVE]);
702     }
703     if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
704         TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
705         ch->active = ATA_ACTIVE_ATA;
706         ch->running = ad_request;
707
708         /*
709          * The donecount had better be 0 here because the channel may not
710          * have retained the setup for the request (if a retry).
711          */
712         KKASSERT(ad_request->donecount == 0);
713         if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
714             crit_exit();
715             return;
716         }
717     }
718
719 #endif
720 #if DEV_ATAPIALL
721     /* find & call the responsible driver if anything on the ATAPI queue */
722     if (TAILQ_EMPTY(&ch->atapi_queue)) {
723         if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
724             atapi_start(&ch->device[MASTER]);
725         if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
726             atapi_start(&ch->device[SLAVE]);
727     }
728     if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
729         TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
730         ch->active = ATA_ACTIVE_ATAPI;
731         ch->running = atapi_request;
732         if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
733             crit_exit();
734             return;
735         }
736     }
737 #endif
738     ATA_UNLOCK_CH(ch);
739     crit_exit();
740 }
741
742 void
743 ata_reset(struct ata_channel *ch)
744 {
745     u_int8_t lsb, msb, ostat0, ostat1;
746     u_int8_t stat0 = 0, stat1 = 0;
747     int mask = 0, timeout;
748
749     /* do we have any signs of ATA/ATAPI HW being present ? */
750     ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
751     DELAY(10);
752     ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
753     if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
754         stat0 = ATA_S_BUSY;
755         mask |= 0x01;
756     }
757     ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
758     DELAY(10);  
759     ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
760     if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
761         stat1 = ATA_S_BUSY;
762         mask |= 0x02;
763     }
764
765     ch->devices = 0;
766     if (!mask)
767         return;
768
769     /* in some setups we dont want to test for a slave */
770     if (ch->flags & ATA_NO_SLAVE) {
771         stat1 = 0x0;
772         mask &= ~0x02;
773     }
774
775     if (bootverbose)
776         ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
777                    mask, ostat0, ostat1);
778
779     /* reset channel */
780     ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
781     DELAY(10);
782     ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
783     DRIVERSLEEP(10000); 
784     ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
785     DRIVERSLEEP(100000);
786     ATA_INB(ch->r_io, ATA_ERROR);
787
788     /* wait for BUSY to go inactive */
789     for (timeout = 0; timeout < 3100; timeout++) {
790         if (stat0 & ATA_S_BUSY) {
791             ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
792             DELAY(10);
793
794             /* check for ATAPI signature while its still there */
795             lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
796             msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
797             stat0 = ATA_INB(ch->r_io, ATA_STATUS);
798             if (!(stat0 & ATA_S_BUSY)) {
799                 if (bootverbose)
800                     ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
801                 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
802                     ch->devices |= ATA_ATAPI_MASTER;
803             }
804         }
805         if (stat1 & ATA_S_BUSY) {
806             ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
807             DELAY(10);
808
809             /* check for ATAPI signature while its still there */
810             lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
811             msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
812             stat1 = ATA_INB(ch->r_io, ATA_STATUS);
813             if (!(stat1 & ATA_S_BUSY)) {
814                 if (bootverbose)
815                     ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
816                 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
817                     ch->devices |= ATA_ATAPI_SLAVE;
818             }
819         }
820         if (mask == 0x01)      /* wait for master only */
821             if (!(stat0 & ATA_S_BUSY))
822                 break;
823         if (mask == 0x02)      /* wait for slave only */
824             if (!(stat1 & ATA_S_BUSY))
825                 break;
826         if (mask == 0x03)      /* wait for both master & slave */
827             if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
828                 break;
829         DRIVERSLEEP(10000);
830     }   
831     /*
832      * some devices release BUSY before they are ready to accept commands.
833      * We must wait at least 50ms before attempting to issue a command after
834      * BUSY is released.
835      */
836     DRIVERSLEEP(50000);
837     ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
838
839     if (stat0 & ATA_S_BUSY)
840         mask &= ~0x01;
841     if (stat1 & ATA_S_BUSY)
842         mask &= ~0x02;
843     if (bootverbose)
844         ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n", 
845                    mask, stat0, stat1);
846     if (!mask)
847         return;
848
849     if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
850         ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
851         DELAY(10);
852         ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
853         ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
854         lsb = ATA_INB(ch->r_io, ATA_ERROR);
855         msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
856         if (bootverbose)
857             ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
858         if (lsb != 0x58 && msb == 0xa5)
859             ch->devices |= ATA_ATA_MASTER;
860     }
861     if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
862         ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
863         DELAY(10);
864         ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
865         ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
866         lsb = ATA_INB(ch->r_io, ATA_ERROR);
867         msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
868         if (bootverbose)
869             ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
870         if (lsb != 0x58 && msb == 0xa5)
871             ch->devices |= ATA_ATA_SLAVE;
872     }
873     if (bootverbose)
874         ata_printf(ch, -1, "devices=%02x\n", ch->devices);
875 }
876
877 int
878 ata_reinit(struct ata_channel *ch)
879 {
880     int devices, misdev, newdev;
881
882     if (!ch->r_io || !ch->r_altio || !ch->r_irq)
883         return ENXIO;
884
885     ATA_FORCELOCK_CH(ch, ATA_CONTROL);
886     ch->running = NULL;
887     devices = ch->devices;
888     ata_printf(ch, -1, "resetting devices .. ");
889     ata_reset(ch);
890
891     if ((misdev = devices & ~ch->devices)) {
892         if (misdev)
893             kprintf("\n");
894 #if NATADISK > 0
895         if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
896             ad_detach(&ch->device[MASTER], 0);
897         if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
898             ad_detach(&ch->device[SLAVE], 0);
899 #endif
900 #if DEV_ATAPIALL
901         if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
902             atapi_detach(&ch->device[MASTER]);
903         if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
904             atapi_detach(&ch->device[SLAVE]);
905 #endif
906         if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
907             if (ch->device[MASTER].param)
908                 kfree(ch->device[MASTER].param, M_ATA);
909             ch->device[MASTER].param = NULL;
910         }
911         if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
912             if (ch->device[SLAVE].param)
913                 kfree(ch->device[SLAVE].param, M_ATA);
914             ch->device[SLAVE].param = NULL;
915         }
916     }
917     if ((newdev = ~devices & ch->devices)) {
918         if (newdev & ATA_ATA_MASTER)
919             if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
920                 ch->devices &= ~ATA_ATA_MASTER;
921         if (newdev & ATA_ATA_SLAVE)
922             if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
923                 ch->devices &= ~ATA_ATA_SLAVE;
924         if (newdev & ATA_ATAPI_MASTER)
925             if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
926                 ch->devices &= ~ATA_ATAPI_MASTER;
927         if (newdev & ATA_ATAPI_SLAVE)
928             if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
929                 ch->devices &= ~ATA_ATAPI_SLAVE;
930     }
931     newdev = ~devices & ch->devices;
932     if (!misdev && newdev)
933         kprintf("\n");
934 #if NATADISK > 0
935     if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
936         ad_attach(&ch->device[MASTER], 1);
937     else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
938         ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
939         ad_reinit(&ch->device[MASTER]);
940     }
941     if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
942         ad_attach(&ch->device[SLAVE], 1);
943     else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
944         ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
945         ad_reinit(&ch->device[SLAVE]);
946     }
947 #endif
948 #if DEV_ATAPIALL
949     if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
950         atapi_attach(&ch->device[MASTER], 1);
951     else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
952         ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
953         atapi_reinit(&ch->device[MASTER]);
954     }
955     if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
956         atapi_attach(&ch->device[SLAVE], 1);
957     else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
958         ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
959         atapi_reinit(&ch->device[SLAVE]);
960     }
961 #endif
962 #if NATAPICAM > 0
963     if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
964         atapi_cam_reinit_bus(ch);
965 #endif
966     kprintf("done\n");
967     ATA_UNLOCK_CH(ch);
968     ata_start(ch);
969     return 0;
970 }
971
972 static int
973 ata_service(struct ata_channel *ch)
974 {
975     /* do we have a SERVICE request from the drive ? */
976     if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
977         ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
978                  ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
979 #if NATADISK > 0
980         if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
981             if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
982                 return ad_service((struct ad_softc *)
983                                   ch->device[MASTER].driver, 0);
984         }
985         else {
986             if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
987                 return ad_service((struct ad_softc *)
988                                   ch->device[SLAVE].driver, 0);
989         }
990 #endif
991     }
992     return ATA_OP_FINISHED;
993 }
994
995 int
996 ata_wait(struct ata_device *atadev, u_int8_t mask)
997 {
998     int timeout = 0;
999     
1000     DELAY(1);
1001     while (timeout < 5000000) { /* timeout 5 secs */
1002         atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1003
1004         /* if drive fails status, reselect the drive just to be sure */
1005         if (atadev->channel->status == 0xff) {
1006             ata_prtdev(atadev, "no status, reselecting device\n");
1007             ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
1008             DELAY(10);
1009             atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
1010             if (atadev->channel->status == 0xff)
1011                 return -1;
1012         }
1013
1014         /* are we done ? */
1015         if (!(atadev->channel->status & ATA_S_BUSY))
1016             break;            
1017
1018         if (timeout > 1000) {
1019             timeout += 1000;
1020             DELAY(1000);
1021         }
1022         else {
1023             timeout += 10;
1024             DELAY(10);
1025         }
1026     }    
1027     if (atadev->channel->status & ATA_S_ERROR)
1028         atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
1029     if (timeout >= 5000000)      
1030         return -1;          
1031     if (!mask)     
1032         return (atadev->channel->status & ATA_S_ERROR);  
1033     
1034     /* Wait 50 msec for bits wanted. */    
1035     timeout = 5000;
1036     while (timeout--) {   
1037         atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1038         if ((atadev->channel->status & mask) == mask) {
1039             if (atadev->channel->status & ATA_S_ERROR)
1040                 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1041             return (atadev->channel->status & ATA_S_ERROR);           
1042         }
1043         DELAY (10);        
1044     }     
1045     return -1;      
1046 }   
1047
1048 int
1049 ata_command(struct ata_device *atadev, u_int8_t command,
1050            u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1051 {
1052     int error = 0;
1053 #ifdef ATA_DEBUG
1054     ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1055                "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1056                rman_get_start(atadev->channel->r_io), 
1057                command, lba, count, feature, flags);
1058 #endif
1059
1060     /* select device */
1061     ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1062
1063     /* disable interrupt from device */
1064     if (atadev->channel->flags & ATA_QUEUED)
1065         ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1066
1067     /* ready to issue command ? */
1068     if (ata_wait(atadev, 0) < 0) { 
1069         ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1070                    command, atadev->channel->status, atadev->channel->error);
1071         return -1;
1072     }
1073
1074     /* only use 48bit addressing if needed because of the overhead */
1075     if ((lba >= 268435455 || count > 256) && atadev->param &&
1076         atadev->param->support.address48) {
1077         ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1078         ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1079         ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1080         ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1081         ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1082         ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1083         ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1084         ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1085         ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1086         ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1087         ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1088
1089         /* translate command into 48bit version */
1090         switch (command) {
1091         case ATA_C_READ:
1092             command = ATA_C_READ48; break;
1093         case ATA_C_READ_MUL:
1094             command = ATA_C_READ_MUL48; break;
1095         case ATA_C_READ_DMA:
1096             command = ATA_C_READ_DMA48; break;
1097         case ATA_C_READ_DMA_QUEUED:
1098             command = ATA_C_READ_DMA_QUEUED48; break;
1099         case ATA_C_WRITE:
1100             command = ATA_C_WRITE48; break;
1101         case ATA_C_WRITE_MUL:
1102             command = ATA_C_WRITE_MUL48; break;
1103         case ATA_C_WRITE_DMA:
1104             command = ATA_C_WRITE_DMA48; break;
1105         case ATA_C_WRITE_DMA_QUEUED:
1106             command = ATA_C_WRITE_DMA_QUEUED48; break;
1107         case ATA_C_FLUSHCACHE:
1108             command = ATA_C_FLUSHCACHE48; break;
1109         default:
1110             ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1111             return -1;
1112         }
1113     }
1114     else {
1115         ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1116         ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1117         ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1118         ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1119         ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1120         if (atadev->flags & ATA_D_USE_CHS)
1121             ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1122                      ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1123         else
1124             ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1125                      ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1126     }
1127
1128     switch (flags & ATA_WAIT_MASK) {
1129     case ATA_IMMEDIATE:
1130         ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1131
1132         /* enable interrupt */
1133         if (atadev->channel->flags & ATA_QUEUED)
1134             ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1135         break;
1136
1137     case ATA_WAIT_INTR:
1138         atadev->channel->active |= ATA_WAIT_INTR;
1139         ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1140
1141         /* enable interrupt */
1142         if (atadev->channel->flags & ATA_QUEUED)
1143             ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1144
1145         if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1146             ata_prtdev(atadev, "timeout waiting for interrupt\n");
1147             atadev->channel->active &= ~ATA_WAIT_INTR;
1148             error = -1;
1149         }
1150         break;
1151     
1152     case ATA_WAIT_READY:
1153         atadev->channel->active |= ATA_WAIT_READY;
1154         ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1155         if (ata_wait(atadev, ATA_S_READY) < 0) { 
1156             ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1157                        command, atadev->channel->status,atadev->channel->error);
1158             error = -1;
1159         }
1160         atadev->channel->active &= ~ATA_WAIT_READY;
1161         break;
1162     }
1163     return error;
1164 }
1165
1166 static void
1167 ata_enclosure_start(struct ata_device *atadev)
1168 {
1169     ATA_INB(atadev->channel->r_io, ATA_DRIVE);    
1170     DELAY(1);
1171     ATA_INB(atadev->channel->r_io, ATA_DRIVE);    
1172     DELAY(1);
1173     ATA_INB(atadev->channel->r_io, ATA_CMD);      
1174     DELAY(1);
1175     ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);    
1176     DELAY(1);
1177     ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);    
1178     DELAY(1);
1179     ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);    
1180     DELAY(1);
1181     ATA_INB(atadev->channel->r_io, ATA_COUNT);
1182     DELAY(1);
1183     ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1184     DELAY(1);
1185 }
1186
1187 static void
1188 ata_enclosure_end(struct ata_device *atadev)
1189 {
1190     ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);    
1191     DELAY(1);
1192 }
1193
1194 static void
1195 ata_enclosure_chip_start(struct ata_device *atadev)
1196 {
1197     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1198     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1199     DELAY(25);
1200     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1201 }
1202
1203 static void
1204 ata_enclosure_chip_end(struct ata_device *atadev)
1205 {
1206     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1207     DELAY(64);
1208     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1209     DELAY(25);
1210     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1211     DELAY(64);
1212 }
1213
1214 static u_int8_t
1215 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1216 {
1217     u_int8_t val;
1218
1219     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1220     DELAY(64);
1221     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1222     DELAY(25);
1223     val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1224     DELAY(38);
1225     return val;
1226 }
1227
1228 static void
1229 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1230 {
1231     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1232     DELAY(64);
1233     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1234     DELAY(64);
1235 }
1236
1237 static u_int8_t
1238 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1239 {
1240     int i;
1241
1242     if (rw) {
1243         for (i = 0; i < 8; i++)
1244             ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1245     }
1246     else {
1247         for (i = 0; i < 8; i++)
1248             val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1249     }
1250     ata_enclosure_chip_wrbit(atadev, 0);
1251     return val;
1252 }
1253
1254 static u_int8_t
1255 ata_enclosure_sensor(struct ata_device *atadev, 
1256                      int rw, u_int8_t idx, u_int8_t data)
1257 {
1258     ata_enclosure_start(atadev);
1259     ata_enclosure_chip_start(atadev);
1260     ata_enclosure_chip_rw(atadev, 1, 0x5a);
1261     ata_enclosure_chip_rw(atadev, 1, idx);
1262     if (rw) {
1263         ata_enclosure_chip_rw(atadev, 1, data);
1264     }
1265     else {
1266         ata_enclosure_chip_end(atadev);
1267         ata_enclosure_chip_start(atadev);
1268         ata_enclosure_chip_rw(atadev, 1, 0x5b);
1269         data = ata_enclosure_chip_rw(atadev, 0, 0);
1270     }
1271     ata_enclosure_chip_end(atadev); 
1272     ata_enclosure_end(atadev);
1273     return data;
1274 }
1275
1276 static int
1277 ata_enclosure_status(struct ata_device *atadev,
1278                      int *fan, int *temp, int *v05, int *v12)
1279 {
1280     u_int8_t id1, id2, cnt, div;
1281     int error = ENXIO;
1282
1283     if (atadev->flags & ATA_D_ENC_PRESENT) {
1284         ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1285         ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1286         id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1287         ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1288         id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1289         if (id1 == 0xa3 && id2 == 0x5c) {
1290             div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1291                         ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1292             cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1293             if (cnt == 0xff)
1294                 *fan = 0;
1295             else
1296                 *fan = 1350000 / cnt / div;
1297             ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1298             *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1299                     (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1300             *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1301             *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1302             error = 0;
1303         }
1304         ATA_UNLOCK_CH(atadev->channel);
1305     }
1306     return error;
1307 }
1308     
1309 void
1310 ata_enclosure_print(struct ata_device *atadev)
1311 {
1312     u_int8_t id, st;
1313     int fan, temp, v05, v12;
1314
1315     ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1316     ata_enclosure_start(atadev);
1317     id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1318     DELAY(1);
1319     st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1320     DELAY(1);
1321     ata_enclosure_end(atadev);
1322     ATA_UNLOCK_CH(atadev->channel);
1323
1324     switch (id & 0x93) {
1325     case 0x00:
1326         ata_prtdev(atadev, "Universal enclosure");
1327         break;
1328     case 0x01:
1329         ata_prtdev(atadev, "FastSwap enclosure");
1330         break;
1331     case 0x10:
1332     case 0x11:
1333         ata_prtdev(atadev, "SuperSwap enclosure");
1334         break;
1335     default:
1336         atadev->flags &= ~ATA_D_ENC_PRESENT;
1337         return;
1338     }
1339     atadev->flags |= ATA_D_ENC_PRESENT;
1340
1341     if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1342         kprintf(" detected\n");
1343     else
1344         kprintf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1345                fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1346 }
1347
1348 void
1349 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1350 {
1351     if (atadev->flags & ATA_D_ENC_PRESENT) {
1352         u_int8_t reg;
1353
1354         ata_enclosure_start(atadev);
1355         reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);          
1356         DELAY(1);
1357         ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1358                  (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));         
1359         DELAY(1);
1360         ata_enclosure_end(atadev);
1361     }
1362 }
1363
1364 static void
1365 ata_change_mode(struct ata_device *atadev, int mode)
1366 {
1367     int umode, wmode, pmode;
1368
1369     umode = ata_umode(atadev->param);
1370     wmode = ata_wmode(atadev->param);
1371     pmode = ata_pmode(atadev->param);
1372     
1373     switch (mode & ATA_DMA_MASK) {
1374     case ATA_UDMA:
1375         if ((mode & ATA_MODE_MASK) < umode)
1376             umode = mode & ATA_MODE_MASK;
1377         break;
1378     case ATA_WDMA:
1379         if ((mode & ATA_MODE_MASK) < wmode)
1380             wmode = mode & ATA_MODE_MASK;
1381         umode = -1;
1382         break;
1383     default:
1384         if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1385             pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1386         umode = -1;
1387         wmode = -1;
1388     }
1389
1390     crit_enter();       /* interlock non-atomic channel lock */
1391     ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1392     ata_dmainit(atadev, pmode, wmode, umode);
1393     ATA_UNLOCK_CH(atadev->channel);
1394     crit_exit();
1395     ata_start(atadev->channel); /* XXX SOS */
1396 }
1397
1398 int
1399 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1400 {
1401     __va_list ap;
1402     int ret;
1403
1404     if (device == -1)
1405         ret = kprintf("ata%d: ", device_get_unit(ch->dev));
1406     else {
1407         if (ch->device[ATA_DEV(device)].name)
1408             ret = kprintf("%s: ", ch->device[ATA_DEV(device)].name);
1409         else
1410             ret = kprintf("ata%d-%s: ", device_get_unit(ch->dev),
1411                          (device == ATA_MASTER) ? "master" : "slave");
1412     }
1413     __va_start(ap, fmt);
1414     ret += kvprintf(fmt, ap);
1415     __va_end(ap);
1416     return ret;
1417 }
1418
1419 int
1420 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1421 {
1422     __va_list ap;
1423     int ret;
1424
1425     if (atadev->name)
1426         ret = kprintf("%s: ", atadev->name);
1427     else
1428         ret = kprintf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1429                      (atadev->unit == ATA_MASTER) ? "master" : "slave");
1430     __va_start(ap, fmt);
1431     ret += kvprintf(fmt, ap);
1432     __va_end(ap);
1433     return ret;
1434 }
1435
1436 void
1437 ata_set_name(struct ata_device *atadev, char *name, int lun)
1438 {
1439     atadev->name = kmalloc(strlen(name) + 4, M_ATA, M_INTWAIT);
1440     ksprintf(atadev->name, "%s%d", name, lun);
1441 }
1442
1443 void
1444 ata_free_name(struct ata_device *atadev)
1445 {
1446     if (atadev->name)
1447         kfree(atadev->name, M_ATA);
1448     atadev->name = NULL;
1449 }
1450     
1451 int
1452 ata_get_lun(u_int32_t *map)
1453 {
1454     int lun = ffs(~*map) - 1;
1455
1456     *map |= (1 << lun);
1457     return lun;
1458 }
1459
1460 int
1461 ata_test_lun(u_int32_t *map, int lun)
1462 {
1463     return (*map & (1 << lun));
1464 }
1465
1466 void
1467 ata_free_lun(u_int32_t *map, int lun)
1468 {
1469     *map &= ~(1 << lun);
1470 }
1471  
1472 char *
1473 ata_mode2str(int mode)
1474 {
1475     switch (mode) {
1476     case ATA_PIO: return "BIOSPIO";
1477     case ATA_PIO0: return "PIO0";
1478     case ATA_PIO1: return "PIO1";
1479     case ATA_PIO2: return "PIO2";
1480     case ATA_PIO3: return "PIO3";
1481     case ATA_PIO4: return "PIO4";
1482     case ATA_DMA: return "BIOSDMA";
1483     case ATA_WDMA2: return "WDMA2";
1484     case ATA_UDMA2: return "UDMA33";
1485     case ATA_UDMA4: return "UDMA66";
1486     case ATA_UDMA5: return "UDMA100";
1487     case ATA_UDMA6: return "UDMA133";
1488     default: return "???";
1489     }
1490 }
1491
1492 int
1493 ata_pmode(struct ata_params *ap)
1494 {
1495     if (ap->atavalid & ATA_FLAG_64_70) {
1496         if (ap->apiomodes & 2)
1497             return 4;
1498         if (ap->apiomodes & 1) 
1499             return 3;
1500     }   
1501     if (ap->retired_piomode == 2)
1502         return 2;
1503     if (ap->retired_piomode == 1)
1504         return 1;
1505     if (ap->retired_piomode == 0)
1506         return 0;
1507     return -1; 
1508
1509
1510 int
1511 ata_wmode(struct ata_params *ap)
1512 {
1513     if (ap->mwdmamodes & 0x04)
1514         return 2;
1515     if (ap->mwdmamodes & 0x02)
1516         return 1;
1517     if (ap->mwdmamodes & 0x01)
1518         return 0;
1519     return -1;
1520 }
1521
1522 int
1523 ata_umode(struct ata_params *ap)
1524 {
1525     if (ap->atavalid & ATA_FLAG_88) {
1526         if (ap->udmamodes & 0x40)
1527             return 6;
1528         if (ap->udmamodes & 0x20)
1529             return 5;
1530         if (ap->udmamodes & 0x10)
1531             return 4;
1532         if (ap->udmamodes & 0x08)
1533             return 3;
1534         if (ap->udmamodes & 0x04)
1535             return 2;
1536         if (ap->udmamodes & 0x02)
1537             return 1;
1538         if (ap->udmamodes & 0x01)
1539             return 0;
1540     }
1541     return -1;
1542 }
1543
1544 static void
1545 bswap(int8_t *buf, int len) 
1546 {
1547     u_int16_t *ptr = (u_int16_t*)(buf + len);
1548
1549     while (--ptr >= (u_int16_t*)buf)
1550         *ptr = ntohs(*ptr);
1551
1552
1553 static void
1554 btrim(int8_t *buf, int len)
1555
1556     int8_t *ptr;
1557
1558     for (ptr = buf; ptr < buf+len; ++ptr) 
1559         if (!*ptr)
1560             *ptr = ' ';
1561     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1562         *ptr = 0;
1563 }
1564
1565 static void
1566 bpack(int8_t *src, int8_t *dst, int len)
1567 {
1568     int i, j, blank;
1569
1570     for (i = j = blank = 0 ; i < len; i++) {
1571         if (blank && src[i] == ' ') continue;
1572         if (blank && src[i] != ' ') {
1573             dst[j++] = src[i];
1574             blank = 0;
1575             continue;
1576         }
1577         if (src[i] == ' ') {
1578             blank = 1;
1579             if (i == 0)
1580                 continue;
1581         }
1582         dst[j++] = src[i];
1583     }
1584     if (j < len) 
1585         dst[j] = 0x00;
1586 }
1587
1588 static void
1589 ata_init(void)
1590 {
1591     /* register controlling device */
1592     dev_ops_add(&ata_ops, 0, 0);
1593     make_dev(&ata_ops, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1594 }
1595 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)