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