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