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