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