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