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