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