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