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