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