Merge from vendor branch OPENSSL:
[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.21 2004/08/17 20:59:39 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         /* clone */     NULL,
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
706         /*
707          * The donecount had better be 0 here because the channel may not
708          * have retained the setup for the request (if a retry).
709          */
710         KKASSERT(ad_request->donecount == 0);
711         if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
712             splx(s);
713             return;
714         }
715     }
716
717 #endif
718 #if DEV_ATAPIALL
719     /* find & call the responsible driver if anything on the ATAPI queue */
720     if (TAILQ_EMPTY(&ch->atapi_queue)) {
721         if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
722             atapi_start(&ch->device[MASTER]);
723         if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
724             atapi_start(&ch->device[SLAVE]);
725     }
726     if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
727         TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
728         ch->active = ATA_ACTIVE_ATAPI;
729         ch->running = atapi_request;
730         if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
731             splx(s);
732             return;
733         }
734     }
735 #endif
736     ATA_UNLOCK_CH(ch);
737     splx(s);
738 }
739
740 void
741 ata_reset(struct ata_channel *ch)
742 {
743     u_int8_t lsb, msb, ostat0, ostat1;
744     u_int8_t stat0 = 0, stat1 = 0;
745     int mask = 0, timeout;
746
747     /* do we have any signs of ATA/ATAPI HW being present ? */
748     ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
749     DELAY(10);
750     ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
751     if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
752         stat0 = ATA_S_BUSY;
753         mask |= 0x01;
754     }
755     ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
756     DELAY(10);  
757     ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
758     if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
759         stat1 = ATA_S_BUSY;
760         mask |= 0x02;
761     }
762
763     ch->devices = 0;
764     if (!mask)
765         return;
766
767     /* in some setups we dont want to test for a slave */
768     if (ch->flags & ATA_NO_SLAVE) {
769         stat1 = 0x0;
770         mask &= ~0x02;
771     }
772
773     if (bootverbose)
774         ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
775                    mask, ostat0, ostat1);
776
777     /* reset channel */
778     ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
779     DELAY(10);
780     ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
781     DELAY(10000); 
782     ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
783     DELAY(100000);
784     ATA_INB(ch->r_io, ATA_ERROR);
785
786     /* wait for BUSY to go inactive */
787     for (timeout = 0; timeout < 3100; timeout++) {
788         if (stat0 & ATA_S_BUSY) {
789             ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
790             DELAY(10);
791
792             /* check for ATAPI signature while its still there */
793             lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
794             msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
795             stat0 = ATA_INB(ch->r_io, ATA_STATUS);
796             if (!(stat0 & ATA_S_BUSY)) {
797                 if (bootverbose)
798                     ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
799                 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
800                     ch->devices |= ATA_ATAPI_MASTER;
801             }
802         }
803         if (stat1 & ATA_S_BUSY) {
804             ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
805             DELAY(10);
806
807             /* check for ATAPI signature while its still there */
808             lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
809             msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
810             stat1 = ATA_INB(ch->r_io, ATA_STATUS);
811             if (!(stat1 & ATA_S_BUSY)) {
812                 if (bootverbose)
813                     ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
814                 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
815                     ch->devices |= ATA_ATAPI_SLAVE;
816             }
817         }
818         if (mask == 0x01)      /* wait for master only */
819             if (!(stat0 & ATA_S_BUSY))
820                 break;
821         if (mask == 0x02)      /* wait for slave only */
822             if (!(stat1 & ATA_S_BUSY))
823                 break;
824         if (mask == 0x03)      /* wait for both master & slave */
825             if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
826                 break;
827         DELAY(10000);
828     }   
829     /*
830      * some devices release BUSY before they are ready to accept commands.
831      * We must wait at least 50ms before attempting to issue a command after
832      * BUSY is released.
833      */
834     DELAY(50000);
835     ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
836
837     if (stat0 & ATA_S_BUSY)
838         mask &= ~0x01;
839     if (stat1 & ATA_S_BUSY)
840         mask &= ~0x02;
841     if (bootverbose)
842         ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n", 
843                    mask, stat0, stat1);
844     if (!mask)
845         return;
846
847     if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
848         ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
849         DELAY(10);
850         ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
851         ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
852         lsb = ATA_INB(ch->r_io, ATA_ERROR);
853         msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
854         if (bootverbose)
855             ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
856         if (lsb != 0x58 && msb == 0xa5)
857             ch->devices |= ATA_ATA_MASTER;
858     }
859     if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
860         ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
861         DELAY(10);
862         ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
863         ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
864         lsb = ATA_INB(ch->r_io, ATA_ERROR);
865         msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
866         if (bootverbose)
867             ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
868         if (lsb != 0x58 && msb == 0xa5)
869             ch->devices |= ATA_ATA_SLAVE;
870     }
871     if (bootverbose)
872         ata_printf(ch, -1, "devices=%02x\n", ch->devices);
873 }
874
875 int
876 ata_reinit(struct ata_channel *ch)
877 {
878     int devices, misdev, newdev;
879
880     if (!ch->r_io || !ch->r_altio || !ch->r_irq)
881         return ENXIO;
882
883     ATA_FORCELOCK_CH(ch, ATA_CONTROL);
884     ch->running = NULL;
885     devices = ch->devices;
886     ata_printf(ch, -1, "resetting devices .. ");
887     ata_reset(ch);
888
889     if ((misdev = devices & ~ch->devices)) {
890         if (misdev)
891             printf("\n");
892 #if NATADISK > 0
893         if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
894             ad_detach(&ch->device[MASTER], 0);
895         if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
896             ad_detach(&ch->device[SLAVE], 0);
897 #endif
898 #if DEV_ATAPIALL
899         if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
900             atapi_detach(&ch->device[MASTER]);
901         if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
902             atapi_detach(&ch->device[SLAVE]);
903 #endif
904         if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
905             if (ch->device[MASTER].param)
906                 free(ch->device[MASTER].param, M_ATA);
907             ch->device[MASTER].param = NULL;
908         }
909         if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
910             if (ch->device[SLAVE].param)
911                 free(ch->device[SLAVE].param, M_ATA);
912             ch->device[SLAVE].param = NULL;
913         }
914     }
915     if ((newdev = ~devices & ch->devices)) {
916         if (newdev & ATA_ATA_MASTER)
917             if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
918                 ch->devices &= ~ATA_ATA_MASTER;
919         if (newdev & ATA_ATA_SLAVE)
920             if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
921                 ch->devices &= ~ATA_ATA_SLAVE;
922         if (newdev & ATA_ATAPI_MASTER)
923             if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
924                 ch->devices &= ~ATA_ATAPI_MASTER;
925         if (newdev & ATA_ATAPI_SLAVE)
926             if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
927                 ch->devices &= ~ATA_ATAPI_SLAVE;
928     }
929     newdev = ~devices & ch->devices;
930     if (!misdev && newdev)
931         printf("\n");
932 #if NATADISK > 0
933     if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
934         ad_attach(&ch->device[MASTER], 1);
935     else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
936         ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
937         ad_reinit(&ch->device[MASTER]);
938     }
939     if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
940         ad_attach(&ch->device[SLAVE], 1);
941     else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
942         ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
943         ad_reinit(&ch->device[SLAVE]);
944     }
945 #endif
946 #if DEV_ATAPIALL
947     if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
948         atapi_attach(&ch->device[MASTER], 1);
949     else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
950         ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
951         atapi_reinit(&ch->device[MASTER]);
952     }
953     if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
954         atapi_attach(&ch->device[SLAVE], 1);
955     else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
956         ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
957         atapi_reinit(&ch->device[SLAVE]);
958     }
959 #endif
960 #if NATAPICAM > 0
961     if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
962         atapi_cam_reinit_bus(ch);
963 #endif
964     printf("done\n");
965     ATA_UNLOCK_CH(ch);
966     ata_start(ch);
967     return 0;
968 }
969
970 static int
971 ata_service(struct ata_channel *ch)
972 {
973     /* do we have a SERVICE request from the drive ? */
974     if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
975         ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
976                  ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
977 #if NATADISK > 0
978         if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
979             if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
980                 return ad_service((struct ad_softc *)
981                                   ch->device[MASTER].driver, 0);
982         }
983         else {
984             if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
985                 return ad_service((struct ad_softc *)
986                                   ch->device[SLAVE].driver, 0);
987         }
988 #endif
989     }
990     return ATA_OP_FINISHED;
991 }
992
993 int
994 ata_wait(struct ata_device *atadev, u_int8_t mask)
995 {
996     int timeout = 0;
997     
998     DELAY(1);
999     while (timeout < 5000000) { /* timeout 5 secs */
1000         atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1001
1002         /* if drive fails status, reselect the drive just to be sure */
1003         if (atadev->channel->status == 0xff) {
1004             ata_prtdev(atadev, "no status, reselecting device\n");
1005             ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
1006             DELAY(10);
1007             atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
1008             if (atadev->channel->status == 0xff)
1009                 return -1;
1010         }
1011
1012         /* are we done ? */
1013         if (!(atadev->channel->status & ATA_S_BUSY))
1014             break;            
1015
1016         if (timeout > 1000) {
1017             timeout += 1000;
1018             DELAY(1000);
1019         }
1020         else {
1021             timeout += 10;
1022             DELAY(10);
1023         }
1024     }    
1025     if (atadev->channel->status & ATA_S_ERROR)
1026         atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
1027     if (timeout >= 5000000)      
1028         return -1;          
1029     if (!mask)     
1030         return (atadev->channel->status & ATA_S_ERROR);  
1031     
1032     /* Wait 50 msec for bits wanted. */    
1033     timeout = 5000;
1034     while (timeout--) {   
1035         atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1036         if ((atadev->channel->status & mask) == mask) {
1037             if (atadev->channel->status & ATA_S_ERROR)
1038                 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1039             return (atadev->channel->status & ATA_S_ERROR);           
1040         }
1041         DELAY (10);        
1042     }     
1043     return -1;      
1044 }   
1045
1046 int
1047 ata_command(struct ata_device *atadev, u_int8_t command,
1048            u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1049 {
1050     int error = 0;
1051 #ifdef ATA_DEBUG
1052     ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1053                "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1054                rman_get_start(atadev->channel->r_io), 
1055                command, lba, count, feature, flags);
1056 #endif
1057
1058     /* select device */
1059     ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1060
1061     /* disable interrupt from device */
1062     if (atadev->channel->flags & ATA_QUEUED)
1063         ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1064
1065     /* ready to issue command ? */
1066     if (ata_wait(atadev, 0) < 0) { 
1067         ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1068                    command, atadev->channel->status, atadev->channel->error);
1069         return -1;
1070     }
1071
1072     /* only use 48bit addressing if needed because of the overhead */
1073     if ((lba > 268435455 || count > 256) && atadev->param &&
1074         atadev->param->support.address48) {
1075         ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1076         ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1077         ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1078         ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1079         ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1080         ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1081         ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1082         ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1083         ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1084         ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1085         ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1086
1087         /* translate command into 48bit version */
1088         switch (command) {
1089         case ATA_C_READ:
1090             command = ATA_C_READ48; break;
1091         case ATA_C_READ_MUL:
1092             command = ATA_C_READ_MUL48; break;
1093         case ATA_C_READ_DMA:
1094             command = ATA_C_READ_DMA48; break;
1095         case ATA_C_READ_DMA_QUEUED:
1096             command = ATA_C_READ_DMA_QUEUED48; break;
1097         case ATA_C_WRITE:
1098             command = ATA_C_WRITE48; break;
1099         case ATA_C_WRITE_MUL:
1100             command = ATA_C_WRITE_MUL48; break;
1101         case ATA_C_WRITE_DMA:
1102             command = ATA_C_WRITE_DMA48; break;
1103         case ATA_C_WRITE_DMA_QUEUED:
1104             command = ATA_C_WRITE_DMA_QUEUED48; break;
1105         case ATA_C_FLUSHCACHE:
1106             command = ATA_C_FLUSHCACHE48; break;
1107         default:
1108             ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1109             return -1;
1110         }
1111     }
1112     else {
1113         ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1114         ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1115         ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1116         ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1117         ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1118         if (atadev->flags & ATA_D_USE_CHS)
1119             ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1120                      ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1121         else
1122             ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1123                      ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1124     }
1125
1126     switch (flags & ATA_WAIT_MASK) {
1127     case ATA_IMMEDIATE:
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         break;
1134
1135     case ATA_WAIT_INTR:
1136         atadev->channel->active |= ATA_WAIT_INTR;
1137         ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1138
1139         /* enable interrupt */
1140         if (atadev->channel->flags & ATA_QUEUED)
1141             ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1142
1143         if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
1144             ata_prtdev(atadev, "timeout waiting for interrupt\n");
1145             atadev->channel->active &= ~ATA_WAIT_INTR;
1146             error = -1;
1147         }
1148         break;
1149     
1150     case ATA_WAIT_READY:
1151         atadev->channel->active |= ATA_WAIT_READY;
1152         ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1153         if (ata_wait(atadev, ATA_S_READY) < 0) { 
1154             ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1155                        command, atadev->channel->status,atadev->channel->error);
1156             error = -1;
1157         }
1158         atadev->channel->active &= ~ATA_WAIT_READY;
1159         break;
1160     }
1161     return error;
1162 }
1163
1164 static void
1165 ata_enclosure_start(struct ata_device *atadev)
1166 {
1167     ATA_INB(atadev->channel->r_io, ATA_DRIVE);    
1168     DELAY(1);
1169     ATA_INB(atadev->channel->r_io, ATA_DRIVE);    
1170     DELAY(1);
1171     ATA_INB(atadev->channel->r_io, ATA_CMD);      
1172     DELAY(1);
1173     ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);    
1174     DELAY(1);
1175     ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);    
1176     DELAY(1);
1177     ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);    
1178     DELAY(1);
1179     ATA_INB(atadev->channel->r_io, ATA_COUNT);
1180     DELAY(1);
1181     ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1182     DELAY(1);
1183 }
1184
1185 static void
1186 ata_enclosure_end(struct ata_device *atadev)
1187 {
1188     ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);    
1189     DELAY(1);
1190 }
1191
1192 static void
1193 ata_enclosure_chip_start(struct ata_device *atadev)
1194 {
1195     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1196     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1197     DELAY(25);
1198     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1199 }
1200
1201 static void
1202 ata_enclosure_chip_end(struct ata_device *atadev)
1203 {
1204     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1205     DELAY(64);
1206     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1207     DELAY(25);
1208     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1209     DELAY(64);
1210 }
1211
1212 static u_int8_t
1213 ata_enclosure_chip_rdbit(struct ata_device *atadev)
1214 {
1215     u_int8_t val;
1216
1217     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1218     DELAY(64);
1219     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1220     DELAY(25);
1221     val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1222     DELAY(38);
1223     return val;
1224 }
1225
1226 static void
1227 ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1228 {
1229     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1230     DELAY(64);
1231     ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1232     DELAY(64);
1233 }
1234
1235 static u_int8_t
1236 ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1237 {
1238     int i;
1239
1240     if (rw) {
1241         for (i = 0; i < 8; i++)
1242             ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1243     }
1244     else {
1245         for (i = 0; i < 8; i++)
1246             val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1247     }
1248     ata_enclosure_chip_wrbit(atadev, 0);
1249     return val;
1250 }
1251
1252 static u_int8_t
1253 ata_enclosure_sensor(struct ata_device *atadev, 
1254                      int rw, u_int8_t idx, u_int8_t data)
1255 {
1256     ata_enclosure_start(atadev);
1257     ata_enclosure_chip_start(atadev);
1258     ata_enclosure_chip_rw(atadev, 1, 0x5a);
1259     ata_enclosure_chip_rw(atadev, 1, idx);
1260     if (rw) {
1261         ata_enclosure_chip_rw(atadev, 1, data);
1262     }
1263     else {
1264         ata_enclosure_chip_end(atadev);
1265         ata_enclosure_chip_start(atadev);
1266         ata_enclosure_chip_rw(atadev, 1, 0x5b);
1267         data = ata_enclosure_chip_rw(atadev, 0, 0);
1268     }
1269     ata_enclosure_chip_end(atadev); 
1270     ata_enclosure_end(atadev);
1271     return data;
1272 }
1273
1274 static int
1275 ata_enclosure_status(struct ata_device *atadev,
1276                      int *fan, int *temp, int *v05, int *v12)
1277 {
1278     u_int8_t id1, id2, cnt, div;
1279     int error = ENXIO;
1280
1281     if (atadev->flags & ATA_D_ENC_PRESENT) {
1282         ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1283         ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1284         id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1285         ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1286         id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1287         if (id1 == 0xa3 && id2 == 0x5c) {
1288             div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1289                         ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1290             cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1291             if (cnt == 0xff)
1292                 *fan = 0;
1293             else
1294                 *fan = 1350000 / cnt / div;
1295             ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1296             *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1297                     (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1298             *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1299             *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1300             error = 0;
1301         }
1302         ATA_UNLOCK_CH(atadev->channel);
1303     }
1304     return error;
1305 }
1306     
1307 void
1308 ata_enclosure_print(struct ata_device *atadev)
1309 {
1310     u_int8_t id, st;
1311     int fan, temp, v05, v12;
1312
1313     ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1314     ata_enclosure_start(atadev);
1315     id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1316     DELAY(1);
1317     st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1318     DELAY(1);
1319     ata_enclosure_end(atadev);
1320     ATA_UNLOCK_CH(atadev->channel);
1321
1322     switch (id & 0x93) {
1323     case 0x00:
1324         ata_prtdev(atadev, "Universal enclosure");
1325         break;
1326     case 0x01:
1327         ata_prtdev(atadev, "FastSwap enclosure");
1328         break;
1329     case 0x10:
1330     case 0x11:
1331         ata_prtdev(atadev, "SuperSwap enclosure");
1332         break;
1333     default:
1334         atadev->flags &= ~ATA_D_ENC_PRESENT;
1335         return;
1336     }
1337     atadev->flags |= ATA_D_ENC_PRESENT;
1338
1339     if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
1340         printf(" detected\n");
1341     else
1342         printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
1343                fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1344 }
1345
1346 void
1347 ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1348 {
1349     if (atadev->flags & ATA_D_ENC_PRESENT) {
1350         u_int8_t reg;
1351
1352         ata_enclosure_start(atadev);
1353         reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);          
1354         DELAY(1);
1355         ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1356                  (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));         
1357         DELAY(1);
1358         ata_enclosure_end(atadev);
1359     }
1360 }
1361
1362 static void
1363 ata_change_mode(struct ata_device *atadev, int mode)
1364 {
1365     int umode, wmode, pmode;
1366     int s;
1367
1368     umode = ata_umode(atadev->param);
1369     wmode = ata_wmode(atadev->param);
1370     pmode = ata_pmode(atadev->param);
1371     
1372     switch (mode & ATA_DMA_MASK) {
1373     case ATA_UDMA:
1374         if ((mode & ATA_MODE_MASK) < umode)
1375             umode = mode & ATA_MODE_MASK;
1376         break;
1377     case ATA_WDMA:
1378         if ((mode & ATA_MODE_MASK) < wmode)
1379             wmode = mode & ATA_MODE_MASK;
1380         umode = -1;
1381         break;
1382     default:
1383         if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1384             pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1385         umode = -1;
1386         wmode = -1;
1387     }
1388
1389     s = splbio();       /* interlock non-atomic channel lock */
1390     ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1391     ata_dmainit(atadev, pmode, wmode, umode);
1392     ATA_UNLOCK_CH(atadev->channel);
1393     splx(s);
1394     ata_start(atadev->channel); /* XXX SOS */
1395 }
1396
1397 int
1398 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1399 {
1400     __va_list ap;
1401     int ret;
1402
1403     if (device == -1)
1404         ret = printf("ata%d: ", device_get_unit(ch->dev));
1405     else {
1406         if (ch->device[ATA_DEV(device)].name)
1407             ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
1408         else
1409             ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
1410                          (device == ATA_MASTER) ? "master" : "slave");
1411     }
1412     __va_start(ap, fmt);
1413     ret += vprintf(fmt, ap);
1414     __va_end(ap);
1415     return ret;
1416 }
1417
1418 int
1419 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1420 {
1421     __va_list ap;
1422     int ret;
1423
1424     if (atadev->name)
1425         ret = printf("%s: ", atadev->name);
1426     else
1427         ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
1428                      (atadev->unit == ATA_MASTER) ? "master" : "slave");
1429     __va_start(ap, fmt);
1430     ret += vprintf(fmt, ap);
1431     __va_end(ap);
1432     return ret;
1433 }
1434
1435 void
1436 ata_set_name(struct ata_device *atadev, char *name, int lun)
1437 {
1438     atadev->name = malloc(strlen(name) + 4, M_ATA, M_INTWAIT);
1439     sprintf(atadev->name, "%s%d", name, lun);
1440 }
1441
1442 void
1443 ata_free_name(struct ata_device *atadev)
1444 {
1445     if (atadev->name)
1446         free(atadev->name, M_ATA);
1447     atadev->name = NULL;
1448 }
1449     
1450 int
1451 ata_get_lun(u_int32_t *map)
1452 {
1453     int lun = ffs(~*map) - 1;
1454
1455     *map |= (1 << lun);
1456     return lun;
1457 }
1458
1459 int
1460 ata_test_lun(u_int32_t *map, int lun)
1461 {
1462     return (*map & (1 << lun));
1463 }
1464
1465 void
1466 ata_free_lun(u_int32_t *map, int lun)
1467 {
1468     *map &= ~(1 << lun);
1469 }
1470  
1471 char *
1472 ata_mode2str(int mode)
1473 {
1474     switch (mode) {
1475     case ATA_PIO: return "BIOSPIO";
1476     case ATA_PIO0: return "PIO0";
1477     case ATA_PIO1: return "PIO1";
1478     case ATA_PIO2: return "PIO2";
1479     case ATA_PIO3: return "PIO3";
1480     case ATA_PIO4: return "PIO4";
1481     case ATA_DMA: return "BIOSDMA";
1482     case ATA_WDMA2: return "WDMA2";
1483     case ATA_UDMA2: return "UDMA33";
1484     case ATA_UDMA4: return "UDMA66";
1485     case ATA_UDMA5: return "UDMA100";
1486     case ATA_UDMA6: return "UDMA133";
1487     default: return "???";
1488     }
1489 }
1490
1491 int
1492 ata_pmode(struct ata_params *ap)
1493 {
1494     if (ap->atavalid & ATA_FLAG_64_70) {
1495         if (ap->apiomodes & 2)
1496             return 4;
1497         if (ap->apiomodes & 1) 
1498             return 3;
1499     }   
1500     if (ap->retired_piomode == 2)
1501         return 2;
1502     if (ap->retired_piomode == 1)
1503         return 1;
1504     if (ap->retired_piomode == 0)
1505         return 0;
1506     return -1; 
1507
1508
1509 int
1510 ata_wmode(struct ata_params *ap)
1511 {
1512     if (ap->mwdmamodes & 0x04)
1513         return 2;
1514     if (ap->mwdmamodes & 0x02)
1515         return 1;
1516     if (ap->mwdmamodes & 0x01)
1517         return 0;
1518     return -1;
1519 }
1520
1521 int
1522 ata_umode(struct ata_params *ap)
1523 {
1524     if (ap->atavalid & ATA_FLAG_88) {
1525         if (ap->udmamodes & 0x40)
1526             return 6;
1527         if (ap->udmamodes & 0x20)
1528             return 5;
1529         if (ap->udmamodes & 0x10)
1530             return 4;
1531         if (ap->udmamodes & 0x08)
1532             return 3;
1533         if (ap->udmamodes & 0x04)
1534             return 2;
1535         if (ap->udmamodes & 0x02)
1536             return 1;
1537         if (ap->udmamodes & 0x01)
1538             return 0;
1539     }
1540     return -1;
1541 }
1542
1543 static void
1544 bswap(int8_t *buf, int len) 
1545 {
1546     u_int16_t *ptr = (u_int16_t*)(buf + len);
1547
1548     while (--ptr >= (u_int16_t*)buf)
1549         *ptr = ntohs(*ptr);
1550
1551
1552 static void
1553 btrim(int8_t *buf, int len)
1554
1555     int8_t *ptr;
1556
1557     for (ptr = buf; ptr < buf+len; ++ptr) 
1558         if (!*ptr)
1559             *ptr = ' ';
1560     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1561         *ptr = 0;
1562 }
1563
1564 static void
1565 bpack(int8_t *src, int8_t *dst, int len)
1566 {
1567     int i, j, blank;
1568
1569     for (i = j = blank = 0 ; i < len; i++) {
1570         if (blank && src[i] == ' ') continue;
1571         if (blank && src[i] != ' ') {
1572             dst[j++] = src[i];
1573             blank = 0;
1574             continue;
1575         }
1576         if (src[i] == ' ') {
1577             blank = 1;
1578             if (i == 0)
1579                 continue;
1580         }
1581         dst[j++] = src[i];
1582     }
1583     if (j < len) 
1584         dst[j] = 0x00;
1585 }
1586
1587 static void
1588 ata_init(void)
1589 {
1590     /* register controlling device */
1591     cdevsw_add(&ata_cdevsw, 0, 0);
1592     make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1593
1594     /* register boot attach to be run when interrupts are enabled */
1595     ata_delayed_attach = malloc(sizeof(struct intr_config_hook),
1596                                       M_TEMP, M_WAITOK | M_ZERO);
1597
1598     ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1599     if (config_intrhook_establish(ata_delayed_attach) != 0) {
1600         printf("ata: config_intrhook_establish failed\n");
1601         free(ata_delayed_attach, M_TEMP);
1602     }
1603 }
1604 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)