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