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