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