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