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