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