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