ata-all.c revision 74450
1/*-
2 * Copyright (c) 1998,1999,2000,2001 S�ren Schmidt
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: head/sys/dev/ata/ata-all.c 74450 2001-03-19 08:04:54Z sos $
29 */
30
31#include "pci.h"
32#include "opt_ata.h"
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/ata.h>
36#include <sys/kernel.h>
37#include <sys/conf.h>
38#include <sys/disk.h>
39#include <sys/module.h>
40#include <sys/bus.h>
41#include <sys/bio.h>
42#include <sys/malloc.h>
43#include <sys/devicestat.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/atapi-all.h>
55
56/* device structures */
57static  d_ioctl_t       ataioctl;
58static 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	/* bmaj */	-1
73};
74
75/* prototypes */
76static void ata_boot_attach(void);
77static void ata_intr(void *);
78static int ata_getparam(struct ata_softc *, int, u_int8_t);
79static int ata_service(struct ata_softc *);
80static char *active2str(int);
81static void bswap(int8_t *, int);
82static void btrim(int8_t *, int);
83static void bpack(int8_t *, int8_t *, int);
84static void ata_change_mode(struct ata_softc *, int, int);
85
86/* sysctl vars */
87SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
88
89/* global vars */
90devclass_t ata_devclass;
91
92/* local vars */
93static struct intr_config_hook *ata_delayed_attach = NULL;
94static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
95
96/* misc defines */
97#define MASTER	0
98#define SLAVE	1
99
100int
101ata_probe(device_t dev)
102{
103    struct ata_softc *scp;
104    int rid;
105
106    if (!dev)
107	return ENXIO;
108    scp = device_get_softc(dev);
109    if (!scp)
110	return ENXIO;
111    if (scp->r_io || scp->r_altio || scp->r_irq)
112	return EEXIST;
113
114    /* initialize the softc basics */
115    scp->active = ATA_IDLE;
116    scp->dev = dev;
117
118    rid = ATA_IOADDR_RID;
119    scp->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
120				   ATA_IOSIZE, RF_ACTIVE);
121    if (!scp->r_io)
122	goto failure;
123
124    rid = ATA_ALTADDR_RID;
125    scp->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
126				      ATA_ALTIOSIZE, RF_ACTIVE);
127    if (!scp->r_altio)
128	goto failure;
129
130    rid = ATA_BMADDR_RID;
131    scp->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
132				     ATA_BMIOSIZE, RF_ACTIVE);
133    if (bootverbose)
134	ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
135		   (int)rman_get_start(scp->r_io),
136		   (int)rman_get_start(scp->r_altio),
137		   (scp->r_bmio) ? (int)rman_get_start(scp->r_bmio) : 0);
138
139    ata_reset(scp);
140
141    TAILQ_INIT(&scp->ata_queue);
142    TAILQ_INIT(&scp->atapi_queue);
143    return 0;
144
145failure:
146    if (scp->r_io)
147	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
148    if (scp->r_altio)
149	bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio);
150    if (scp->r_bmio)
151	bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
152    if (bootverbose)
153	ata_printf(scp, -1, "probe allocation failed\n");
154    return ENXIO;
155}
156
157int
158ata_attach(device_t dev)
159{
160    struct ata_softc *scp;
161    int error, rid;
162
163    if (!dev)
164	return ENXIO;
165    scp = device_get_softc(dev);
166    if (!scp)
167	return ENXIO;
168
169    rid = ATA_IRQ_RID;
170    scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
171				    RF_SHAREABLE | RF_ACTIVE);
172    if (!scp->r_irq) {
173	ata_printf(scp, -1, "unable to allocate interrupt\n");
174	return ENXIO;
175    }
176    if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO|INTR_ENTROPY,
177				ata_intr, scp, &scp->ih)))
178	return error;
179
180    /*
181     * do not attach devices if we are in early boot, this is done later
182     * when interrupts are enabled by a hook into the boot process.
183     * otherwise attach what the probe has found in scp->devices.
184     */
185    if (!ata_delayed_attach) {
186	if (scp->devices & ATA_ATA_SLAVE)
187	    if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
188		scp->devices &= ~ATA_ATA_SLAVE;
189	if (scp->devices & ATA_ATAPI_SLAVE)
190	    if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
191		scp->devices &= ~ATA_ATAPI_SLAVE;
192	if (scp->devices & ATA_ATA_MASTER)
193	    if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
194		scp->devices &= ~ATA_ATA_MASTER;
195	if (scp->devices & ATA_ATAPI_MASTER)
196	    if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
197		scp->devices &= ~ATA_ATAPI_MASTER;
198#ifdef DEV_ATADISK
199	if (scp->devices & ATA_ATA_MASTER)
200	    ad_attach(scp, ATA_MASTER);
201	if (scp->devices & ATA_ATA_SLAVE)
202	    ad_attach(scp, ATA_SLAVE);
203#endif
204#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
205	if (scp->devices & ATA_ATAPI_MASTER)
206	    atapi_attach(scp, ATA_MASTER);
207	if (scp->devices & ATA_ATAPI_SLAVE)
208	    atapi_attach(scp, ATA_SLAVE);
209#endif
210    }
211    return 0;
212}
213
214int
215ata_detach(device_t dev)
216{
217    struct ata_softc *scp;
218    int s;
219
220    if (!dev)
221	return ENXIO;
222    scp = device_get_softc(dev);
223    if (!scp || !scp->devices)
224	return ENXIO;
225
226    /* make sure channel is not busy SOS XXX */
227    s = splbio();
228    while (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_CONTROL))
229        tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
230    splx(s);
231
232    /* disable interrupts on devices */
233    ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
234    ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
235    ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
236    ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
237
238#ifdef DEV_ATADISK
239    if (scp->devices & ATA_ATA_MASTER && scp->dev_softc[MASTER])
240	ad_detach(scp->dev_softc[MASTER], 1);
241    if (scp->devices & ATA_ATA_SLAVE && scp->dev_softc[SLAVE])
242	ad_detach(scp->dev_softc[SLAVE], 1);
243#endif
244#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
245    if (scp->devices & ATA_ATAPI_MASTER && scp->dev_softc[MASTER])
246	atapi_detach(scp->dev_softc[MASTER]);
247    if (scp->devices & ATA_ATAPI_SLAVE && scp->dev_softc[SLAVE])
248	atapi_detach(scp->dev_softc[SLAVE]);
249#endif
250
251    if (scp->dev_param[MASTER]) {
252	free(scp->dev_param[MASTER], M_ATA);
253	scp->dev_param[MASTER] = NULL;
254    }
255    if (scp->dev_param[SLAVE]) {
256	free(scp->dev_param[SLAVE], M_ATA);
257	scp->dev_param[SLAVE] = NULL;
258    }
259    scp->dev_softc[MASTER] = NULL;
260    scp->dev_softc[SLAVE] = NULL;
261    scp->mode[MASTER] = ATA_PIO;
262    scp->mode[SLAVE] = ATA_PIO;
263    scp->devices = 0;
264
265    bus_teardown_intr(dev, scp->r_irq, scp->ih);
266    bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, scp->r_irq);
267    if (scp->r_bmio)
268	bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
269    bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, scp->r_altio);
270    bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
271    scp->r_io = NULL;
272    scp->r_altio = NULL;
273    scp->r_bmio = NULL;
274    scp->r_irq = NULL;
275    scp->active = ATA_IDLE;
276    return 0;
277}
278
279int
280ata_resume(device_t dev)
281{
282    struct ata_softc *scp = device_get_softc(dev);
283
284    ata_reinit(scp);
285    return 0;
286}
287
288static int
289ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
290{
291    int error = 0;
292
293    switch (cmd) {
294	case ATAATTACH: {
295	    device_t device = devclass_get_device(ata_devclass, *(int *)addr);
296	    /* should enable channel HW on controller that can SOS XXX */
297	    if (!device)
298		error = ENXIO;
299	    if (!error)
300		error = ata_probe(device);
301	    if (!error)
302		error = ata_attach(device);
303	    break;
304	}
305
306	case ATADETACH: {
307	    device_t device = devclass_get_device(ata_devclass, *(int *)addr);
308	    if (!device)
309		error = ENXIO;
310	    if (!error)
311		error = ata_detach(device);
312	    /* should disable channel HW on controller that can SOS XXX */
313	    break;
314	}
315
316	case ATAREINIT: {
317	    device_t device = devclass_get_device(ata_devclass, *(int *)addr);
318	    struct ata_softc *scp;
319	    int s;
320
321	    if (!device)
322		return ENXIO;
323	    scp = device_get_softc(device);
324	    if (!scp)
325		return ENXIO;
326
327	    /* make sure channel is not busy SOS XXX */
328	    s = splbio();
329	    while (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE))
330        	tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
331	    error = ata_reinit(scp);
332	    splx(s);
333	    break;
334	}
335
336	case ATAGMODE: {
337	    struct ata_modes *mode = (struct ata_modes *)addr;
338	    device_t device = devclass_get_device(ata_devclass, mode->channel);
339	    struct ata_softc *scp;
340
341	    if (!device)
342		return ENXIO;
343	    scp = device_get_softc(device);
344	    if (!scp)
345		return ENXIO;
346	    mode->mode[MASTER] = scp->mode[MASTER];
347	    mode->mode[SLAVE] = scp->mode[SLAVE];
348	    break;
349	}
350
351	case ATASMODE: {
352	    struct ata_modes *mode = (struct ata_modes *)addr;
353	    device_t device = devclass_get_device(ata_devclass, mode->channel);
354	    struct ata_softc *scp;
355
356	    if (!device)
357		return ENXIO;
358	    scp = device_get_softc(device);
359	    if (!scp)
360		return ENXIO;
361	    if (mode->mode[MASTER] >= 0)
362		ata_change_mode(scp, ATA_MASTER, mode->mode[MASTER]);
363	    if (mode->mode[SLAVE] >= 0)
364		ata_change_mode(scp, ATA_SLAVE, mode->mode[SLAVE]);
365	    mode->mode[MASTER] = scp->mode[MASTER];
366	    mode->mode[SLAVE] = scp->mode[SLAVE];
367	    break;
368	}
369
370	case ATAGPARM: {
371	    struct ata_param *parm = (struct ata_param *)addr;
372	    device_t device = devclass_get_device(ata_devclass, parm->channel);
373	    struct ata_softc *scp;
374
375	    if (!device)
376		return ENXIO;
377	    scp = device_get_softc(device);
378	    if (!scp)
379		return ENXIO;
380
381	    parm->type[MASTER] =
382		scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
383	    parm->type[SLAVE] =
384		scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
385
386	    if (scp->dev_name[MASTER])
387		strcpy(parm->name[MASTER], scp->dev_name[MASTER]);
388	    if (scp->dev_name[SLAVE])
389		strcpy(parm->name[SLAVE], scp->dev_name[SLAVE]);
390
391	    if (scp->dev_param[MASTER])
392		bcopy(scp->dev_param[MASTER], &parm->params[MASTER],
393		      sizeof(struct ata_params));
394	    if (scp->dev_param[SLAVE])
395		bcopy(scp->dev_param[SLAVE], &parm->params[SLAVE],
396		      sizeof(struct ata_params));
397	    break;
398	}
399
400	default:
401	    error = ENOTTY;
402    }
403    return error;
404}
405
406static int
407ata_getparam(struct ata_softc *scp, int device, u_int8_t command)
408{
409    struct ata_params *ata_parm;
410    int8_t buffer[DEV_BSIZE];
411    int retry = 0;
412
413    /* select drive */
414    ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
415    DELAY(1);
416
417    /* enable interrupt */
418    ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
419    DELAY(1);
420
421    /* apparently some devices needs this repeated */
422    do {
423	if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
424	    ata_printf(scp, device, "identify failed\n");
425	    return -1;
426	}
427	if (retry++ > 4) {
428	    ata_printf(scp, device, "identify retries exceeded\n");
429	    return -1;
430	}
431    } while (ata_wait(scp, device,
432		      ((command == ATA_C_ATAPI_IDENTIFY) ?
433			ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
434
435    ATA_INSW(scp->r_io, ATA_DATA, (int16_t *)buffer,
436	     sizeof(buffer)/sizeof(int16_t));
437    ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
438    if (!ata_parm) {
439	ata_printf(scp, device, "malloc for identify data failed\n");
440        return -1;
441    }
442    bcopy(buffer, ata_parm, sizeof(struct ata_params));
443    if (command == ATA_C_ATA_IDENTIFY ||
444	!((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
445          (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X')))
446        bswap(ata_parm->model, sizeof(ata_parm->model));
447    btrim(ata_parm->model, sizeof(ata_parm->model));
448    bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
449    bswap(ata_parm->revision, sizeof(ata_parm->revision));
450    btrim(ata_parm->revision, sizeof(ata_parm->revision));
451    bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
452    scp->dev_param[ATA_DEV(device)] = ata_parm;
453    return 0;
454}
455
456static void
457ata_boot_attach(void)
458{
459    struct ata_softc *scp;
460    int ctlr;
461
462    /*
463     * run through all ata devices and look for real ATA & ATAPI devices
464     * using the hints we found in the early probe, this avoids some of
465     * the delays probing of non-exsistent devices can cause.
466     */
467    for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
468	if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
469	    continue;
470	if (scp->devices & ATA_ATA_SLAVE)
471	    if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
472		scp->devices &= ~ATA_ATA_SLAVE;
473	if (scp->devices & ATA_ATAPI_SLAVE)
474	    if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
475		scp->devices &= ~ATA_ATAPI_SLAVE;
476	if (scp->devices & ATA_ATA_MASTER)
477	    if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
478		scp->devices &= ~ATA_ATA_MASTER;
479	if (scp->devices & ATA_ATAPI_MASTER)
480	    if (ata_getparam(scp, ATA_MASTER, ATA_C_ATAPI_IDENTIFY))
481		scp->devices &= ~ATA_ATAPI_MASTER;
482    }
483
484#ifdef DEV_ATADISK
485    /* now we know whats there, do the real attach, first the ATA disks */
486    for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
487	if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
488	    continue;
489	if (scp->devices & ATA_ATA_MASTER)
490	    ad_attach(scp, ATA_MASTER);
491	if (scp->devices & ATA_ATA_SLAVE)
492	    ad_attach(scp, ATA_SLAVE);
493    }
494#endif
495#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
496    /* then the atapi devices */
497    for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
498	if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
499	    continue;
500	if (scp->devices & ATA_ATAPI_MASTER)
501	    atapi_attach(scp, ATA_MASTER);
502	if (scp->devices & ATA_ATAPI_SLAVE)
503	    atapi_attach(scp, ATA_SLAVE);
504    }
505#endif
506    if (ata_delayed_attach) {
507	config_intrhook_disestablish(ata_delayed_attach);
508	free(ata_delayed_attach, M_ATA);
509	ata_delayed_attach = NULL;
510    }
511}
512
513static void
514ata_intr(void *data)
515{
516    struct ata_softc *scp = (struct ata_softc *)data;
517
518    /*
519     * on PCI systems we might share an interrupt line with another
520     * device or our twin ATA channel, so call scp->intr_func to figure
521     * out if it is really an interrupt we should process here
522     */
523    if (scp->intr_func && scp->intr_func(scp))
524	return;
525
526    /* if drive is busy it didn't interrupt */
527    if (ATA_INB(scp->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
528	DELAY(100);
529	if (!(ATA_INB(scp->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
530	    return;
531    }
532
533    /* clear interrupt and get status */
534    scp->status = ATA_INB(scp->r_io, ATA_STATUS);
535
536    if (scp->status & ATA_S_ERROR)
537	scp->error = ATA_INB(scp->r_io, ATA_ERROR);
538
539    /* find & call the responsible driver to process this interrupt */
540    switch (scp->active) {
541#ifdef DEV_ATADISK
542    case ATA_ACTIVE_ATA:
543	if (!scp->running || ad_interrupt(scp->running) == ATA_OP_CONTINUES)
544	    return;
545	break;
546#endif
547#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
548    case ATA_ACTIVE_ATAPI:
549	if (!scp->running || atapi_interrupt(scp->running) == ATA_OP_CONTINUES)
550	    return;
551	break;
552#endif
553    case ATA_WAIT_INTR:
554    case ATA_WAIT_INTR | ATA_CONTROL:
555	wakeup((caddr_t)scp);
556	break;
557
558    case ATA_WAIT_READY:
559    case ATA_WAIT_READY | ATA_CONTROL:
560	break;
561
562    case ATA_IDLE:
563	if (scp->flags & ATA_QUEUED) {
564	    scp->active = ATA_ACTIVE; /* XXX */
565	    if (ata_service(scp) == ATA_OP_CONTINUES)
566		return;
567	}
568	/* FALLTHROUGH */
569
570    default:
571#ifdef ATA_DEBUG
572    {
573	static int intr_count = 0;
574
575	if (intr_count++ < 10)
576	    ata_printf(scp, -1, "unwanted interrupt %d %sstatus = %02x\n",
577		       intr_count, active2str(scp->active), scp->status);
578    }
579#endif
580    }
581    scp->active &= ATA_CONTROL;
582    if (scp->active & ATA_CONTROL)
583	return;
584    scp->running = NULL;
585    ata_start(scp);
586    return;
587}
588
589void
590ata_start(struct ata_softc *scp)
591{
592#ifdef DEV_ATADISK
593    struct ad_request *ad_request;
594#endif
595#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
596    struct atapi_request *atapi_request;
597#endif
598
599    if (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE))
600	return;
601
602#ifdef DEV_ATADISK
603    /* find & call the responsible driver if anything on the ATA queue */
604    if (TAILQ_EMPTY(&scp->ata_queue)) {
605	if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[MASTER])
606	    ad_start((struct ad_softc *)scp->dev_softc[MASTER]);
607	if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[SLAVE])
608	    ad_start((struct ad_softc *)scp->dev_softc[SLAVE]);
609    }
610    if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
611	TAILQ_REMOVE(&scp->ata_queue, ad_request, chain);
612	scp->active = ATA_ACTIVE_ATA;
613	scp->running = ad_request;
614	if (ad_transfer(ad_request) == ATA_OP_CONTINUES)
615	    return;
616    }
617
618#endif
619#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
620    /* find & call the responsible driver if anything on the ATAPI queue */
621    if (TAILQ_EMPTY(&scp->atapi_queue)) {
622	if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[MASTER])
623	    atapi_start((struct atapi_softc *)scp->dev_softc[MASTER]);
624	if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[SLAVE])
625	    atapi_start((struct atapi_softc *)scp->dev_softc[SLAVE]);
626    }
627    if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
628	TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain);
629	scp->active = ATA_ACTIVE_ATAPI;
630	scp->running = atapi_request;
631	atapi_transfer(atapi_request);
632	return;
633    }
634#endif
635    scp->active = ATA_IDLE;
636}
637
638void
639ata_reset(struct ata_softc *scp)
640{
641    u_int8_t lsb, msb, ostat0, ostat1;
642    u_int8_t stat0 = ATA_S_BUSY, stat1 = ATA_S_BUSY;
643    int mask = 0, timeout;
644
645    /* do we have any signs of ATA/ATAPI HW being present ? */
646    ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
647    DELAY(10);
648    ostat0 = ATA_INB(scp->r_io, ATA_STATUS);
649    if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5)
650	mask |= 0x01;
651    ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
652    DELAY(10);
653    ostat1 = ATA_INB(scp->r_io, ATA_STATUS);
654    if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5)
655	mask |= 0x02;
656
657    scp->devices = 0;
658    if (!mask)
659	return;
660
661    /* in some setups we dont want to test for a slave */
662    if (scp->flags & ATA_NO_SLAVE)
663	mask &= ~0x02;
664
665    if (bootverbose)
666	ata_printf(scp, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
667		   mask, ostat0, ostat1);
668
669    /* reset channel */
670    ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
671    DELAY(10000);
672    ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS);
673    DELAY(100000);
674    ATA_INB(scp->r_io, ATA_ERROR);
675
676    /* wait for BUSY to go inactive */
677    for (timeout = 0; timeout < 310000; timeout++) {
678	if (stat0 & ATA_S_BUSY) {
679            ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
680            DELAY(10);
681            stat0 = ATA_INB(scp->r_io, ATA_STATUS);
682            if (!(stat0 & ATA_S_BUSY)) {
683                /* check for ATAPI signature while its still there */
684		lsb = ATA_INB(scp->r_io, ATA_CYL_LSB);
685		msb = ATA_INB(scp->r_io, ATA_CYL_MSB);
686		if (bootverbose)
687		    ata_printf(scp, ATA_MASTER,
688			       "ATAPI probe %02x %02x\n", lsb, msb);
689		if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
690                    scp->devices |= ATA_ATAPI_MASTER;
691            }
692        }
693        if (stat1 & ATA_S_BUSY) {
694            ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
695            DELAY(10);
696            stat1 = ATA_INB(scp->r_io, ATA_STATUS);
697            if (!(stat1 & ATA_S_BUSY)) {
698                /* check for ATAPI signature while its still there */
699		lsb = ATA_INB(scp->r_io, ATA_CYL_LSB);
700		msb = ATA_INB(scp->r_io, ATA_CYL_MSB);
701		if (bootverbose)
702		    ata_printf(scp, ATA_SLAVE,
703			       "ATAPI probe %02x %02x\n", lsb, msb);
704		if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
705                    scp->devices |= ATA_ATAPI_SLAVE;
706            }
707        }
708	if (mask == 0x01)      /* wait for master only */
709	    if (!(stat0 & ATA_S_BUSY))
710		break;
711	if (mask == 0x02)      /* wait for slave only */
712	    if (!(stat1 & ATA_S_BUSY))
713		break;
714	if (mask == 0x03)      /* wait for both master & slave */
715	    if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
716		break;
717	DELAY(100);
718    }
719    DELAY(10);
720    ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
721
722    if (stat0 & ATA_S_BUSY)
723	mask &= ~0x01;
724    if (stat1 & ATA_S_BUSY)
725	mask &= ~0x02;
726    if (bootverbose)
727	ata_printf(scp, -1, "mask=%02x stat0=%02x stat1=%02x\n",
728		   mask, stat0, stat1);
729    if (!mask)
730	return;
731
732    if (mask & 0x01 && ostat0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
733        ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
734        DELAY(10);
735	ATA_OUTB(scp->r_io, ATA_ERROR, 0x58);
736	ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5);
737	lsb = ATA_INB(scp->r_io, ATA_ERROR);
738	msb = ATA_INB(scp->r_io, ATA_CYL_LSB);
739	if (bootverbose)
740	    ata_printf(scp, ATA_MASTER, "ATA probe %02x %02x\n", lsb, msb);
741        if (lsb != 0x58 && msb == 0xa5)
742            scp->devices |= ATA_ATA_MASTER;
743    }
744    if (mask & 0x02 && ostat1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
745        ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
746        DELAY(10);
747	ATA_OUTB(scp->r_io, ATA_ERROR, 0x58);
748	ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5);
749	lsb = ATA_INB(scp->r_io, ATA_ERROR);
750	msb = ATA_INB(scp->r_io, ATA_CYL_LSB);
751	if (bootverbose)
752	    ata_printf(scp, ATA_SLAVE, "ATA probe %02x %02x\n", lsb, msb);
753        if (lsb != 0x58 && msb == 0xa5)
754            scp->devices |= ATA_ATA_SLAVE;
755    }
756    if (bootverbose)
757	ata_printf(scp, -1, "devices=%02x\n", scp->devices);
758}
759
760int
761ata_reinit(struct ata_softc *scp)
762{
763    int devices, misdev, newdev;
764
765    if (!scp->r_io || !scp->r_altio || !scp->r_irq)
766	return ENXIO;
767    scp->active = ATA_CONTROL;
768    scp->running = NULL;
769    devices = scp->devices;
770    ata_printf(scp, -1, "resetting devices .. ");
771    ata_reset(scp);
772
773    if ((misdev = devices & ~scp->devices)) {
774	if (misdev)
775	    printf("\n");
776#ifdef DEV_ATADISK
777	if (misdev & ATA_ATA_MASTER && scp->dev_softc[MASTER])
778	    ad_detach(scp->dev_softc[MASTER], 0);
779	if (misdev & ATA_ATA_SLAVE && scp->dev_softc[SLAVE])
780	    ad_detach(scp->dev_softc[SLAVE], 0);
781#endif
782#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
783	if (misdev & ATA_ATAPI_MASTER && scp->dev_softc[MASTER])
784	    atapi_detach(scp->dev_softc[MASTER]);
785	if (misdev & ATA_ATAPI_SLAVE && scp->dev_softc[SLAVE])
786	    atapi_detach(scp->dev_softc[SLAVE]);
787#endif
788	if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
789	    free(scp->dev_param[MASTER], M_ATA);
790	    scp->dev_param[MASTER] = NULL;
791	}
792	if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
793	    free(scp->dev_param[SLAVE], M_ATA);
794	    scp->dev_param[SLAVE] = NULL;
795	}
796    }
797    if ((newdev = ~devices & scp->devices)) {
798	if (newdev & ATA_ATA_MASTER)
799	    if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
800		newdev &= ~ATA_ATA_MASTER;
801	if (newdev & ATA_ATA_SLAVE)
802	    if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
803		newdev &= ~ATA_ATA_SLAVE;
804	if (newdev & ATA_ATAPI_MASTER)
805	    if (ata_getparam(scp, ATA_MASTER, ATA_C_ATAPI_IDENTIFY))
806		newdev &= ~ATA_ATAPI_MASTER;
807	if (newdev & ATA_ATAPI_SLAVE)
808	    if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
809		newdev &= ~ATA_ATAPI_SLAVE;
810    }
811    if (!misdev && newdev)
812	printf("\n");
813#ifdef DEV_ATADISK
814    if (newdev & ATA_ATA_MASTER && !scp->dev_softc[MASTER])
815	ad_attach(scp, ATA_MASTER);
816    else if (scp->devices & ATA_ATA_MASTER && scp->dev_softc[MASTER])
817	ad_reinit((struct ad_softc *)scp->dev_softc[MASTER]);
818    if (newdev & ATA_ATA_SLAVE && !scp->dev_softc[SLAVE])
819	ad_attach(scp, ATA_SLAVE);
820    else if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[SLAVE])
821	ad_reinit((struct ad_softc *)scp->dev_softc[SLAVE]);
822#endif
823#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
824    if (newdev & ATA_ATAPI_MASTER && !scp->dev_softc[MASTER])
825	atapi_attach(scp, ATA_MASTER);
826    else if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[MASTER])
827	atapi_reinit((struct atapi_softc *)scp->dev_softc[MASTER]);
828    if (newdev & ATA_ATAPI_SLAVE && !scp->dev_softc[SLAVE])
829	atapi_attach(scp, ATA_SLAVE);
830    else if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[SLAVE])
831	atapi_reinit((struct atapi_softc *)scp->dev_softc[SLAVE]);
832#endif
833    printf("done\n");
834    scp->active = ATA_IDLE;
835    ata_start(scp);
836    return 0;
837}
838
839static int
840ata_service(struct ata_softc *scp)
841{
842    /* do we have a SERVICE request from the drive ? */
843    if ((scp->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE){
844	ATA_OUTB(scp->r_bmio, ATA_BMSTAT_PORT,
845		 ata_dmastatus(scp) | ATA_BMSTAT_INTERRUPT);
846#ifdef DEV_ATADISK
847	if ((ATA_INB(scp->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
848	    if ((scp->devices & ATA_ATA_MASTER) && scp->dev_softc[MASTER])
849		return ad_service((struct ad_softc *)scp->dev_softc[MASTER], 0);
850	}
851	else {
852	    if ((scp->devices & ATA_ATA_SLAVE) && scp->dev_softc[SLAVE])
853		return ad_service((struct ad_softc *)scp->dev_softc[SLAVE], 0);
854	}
855#endif
856    }
857    return ATA_OP_FINISHED;
858}
859
860int
861ata_wait(struct ata_softc *scp, int device, u_int8_t mask)
862{
863    int timeout = 0;
864
865    DELAY(1);
866    while (timeout < 5000000) {	/* timeout 5 secs */
867	scp->status = ATA_INB(scp->r_io, ATA_STATUS);
868
869	/* if drive fails status, reselect the drive just to be sure */
870	if (scp->status == 0xff) {
871	    ata_printf(scp, device, "no status, reselecting device\n");
872	    ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
873	    DELAY(10);
874	    scp->status = ATA_INB(scp->r_io, ATA_STATUS);
875	    if (scp->status == 0xff)
876		return -1;
877	}
878
879	/* are we done ? */
880	if (!(scp->status & ATA_S_BUSY))
881	    break;
882
883	if (timeout > 1000) {
884	    timeout += 1000;
885	    DELAY(1000);
886	}
887	else {
888	    timeout += 10;
889	    DELAY(10);
890	}
891    }
892    if (scp->status & ATA_S_ERROR)
893	scp->error = ATA_INB(scp->r_io, ATA_ERROR);
894    if (timeout >= 5000000)
895	return -1;
896    if (!mask)
897	return (scp->status & ATA_S_ERROR);
898
899    /* Wait 50 msec for bits wanted. */
900    timeout = 5000;
901    while (timeout--) {
902	scp->status = ATA_INB(scp->r_io, ATA_STATUS);
903	if ((scp->status & mask) == mask) {
904	    if (scp->status & ATA_S_ERROR)
905		scp->error = ATA_INB(scp->r_io, ATA_ERROR);
906	    return (scp->status & ATA_S_ERROR);
907	}
908	DELAY (10);
909    }
910    return -1;
911}
912
913int
914ata_command(struct ata_softc *scp, int device, u_int8_t command,
915	   u_int16_t cylinder, u_int8_t head, u_int8_t sector,
916	   u_int8_t count, u_int8_t feature, int flags)
917{
918    int error = 0;
919#ifdef ATA_DEBUG
920    ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, "
921	       "c=%d, h=%d, s=%d, count=%d, feature=%d, flags=%02x\n",
922	       rman_get_start(scp->r_io), command, cylinder, head, sector,
923	       count, feature, flags);
924
925    /* sanity checks */
926    switch(scp->active) {
927    case ATA_IDLE:
928	break;
929
930    case ATA_CONTROL:
931	if (flags == ATA_WAIT_INTR || flags == ATA_WAIT_READY)
932	    break;
933	goto out;
934
935    case ATA_ACTIVE_ATA:
936    case ATA_ACTIVE_ATAPI:
937	if (flags == ATA_IMMEDIATE)
938	    break;
939
940    default:
941out:
942	printf("ata_command called %s flags=%s cmd=%02x\n",
943	       active2str(scp->active), active2str(flags), command);
944	break;
945    }
946#endif
947
948    /* disable interrupt from device */
949    if (scp->flags & ATA_QUEUED)
950	ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
951
952    /* select device */
953    ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
954
955    /* ready to issue command ? */
956    if (ata_wait(scp, device, 0) < 0) {
957	ata_printf(scp, device,
958		   "timeout waiting to give command=%02x s=%02x e=%02x\n",
959		   command, scp->status, scp->error);
960	return -1;
961    }
962
963    ATA_OUTB(scp->r_io, ATA_FEATURE, feature);
964    ATA_OUTB(scp->r_io, ATA_COUNT, count);
965    ATA_OUTB(scp->r_io, ATA_SECTOR, sector);
966    ATA_OUTB(scp->r_io, ATA_CYL_MSB, cylinder >> 8);
967    ATA_OUTB(scp->r_io, ATA_CYL_LSB, cylinder);
968    ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device | head);
969
970    switch (flags) {
971    case ATA_WAIT_INTR:
972	scp->active |= ATA_WAIT_INTR;
973	asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz);
974	ATA_OUTB(scp->r_io, ATA_CMD, command);
975
976	/* enable interrupt */
977	if (scp->flags & ATA_QUEUED)
978	    ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
979
980	if (await(PRIBIO, 10 * hz)) {
981	    ata_printf(scp, device, "ata_command: timeout waiting for intr\n");
982	    scp->active &= ~ATA_WAIT_INTR;
983	    error = -1;
984	}
985	break;
986
987    case ATA_WAIT_READY:
988	scp->active |= ATA_WAIT_READY;
989	ATA_OUTB(scp->r_io, ATA_CMD, command);
990	if (ata_wait(scp, device, ATA_S_READY) < 0) {
991	    ata_printf(scp, device,
992		       "timeout waiting for command=%02x s=%02x e=%02x\n",
993		       command, scp->status, scp->error);
994	    error = -1;
995	}
996	scp->active &= ~ATA_WAIT_READY;
997	break;
998
999    case ATA_IMMEDIATE:
1000	ATA_OUTB(scp->r_io, ATA_CMD, command);
1001	break;
1002
1003    default:
1004	ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
1005		   active2str(flags));
1006    }
1007    /* enable interrupt */
1008    if (scp->flags & ATA_QUEUED)
1009	ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1010    return error;
1011}
1012
1013void
1014ata_set_name(struct ata_softc *scp, int device, char *name)
1015{
1016    scp->dev_name[ATA_DEV(device)] = malloc(strlen(name) + 1, M_ATA, M_NOWAIT);
1017    if (scp->dev_name[ATA_DEV(device)])
1018	strcpy(scp->dev_name[ATA_DEV(device)], name);
1019}
1020
1021void
1022ata_free_name(struct ata_softc *scp, int device)
1023{
1024    if (scp->dev_name[ATA_DEV(device)])
1025	free(scp->dev_name[ATA_DEV(device)], M_ATA);
1026}
1027
1028int
1029ata_get_lun(u_int32_t *map)
1030{
1031    int lun = ffs(~*map) - 1;
1032
1033    *map |= (1 << lun);
1034    return lun;
1035}
1036
1037int
1038ata_test_lun(u_int32_t *map, int lun)
1039{
1040    return (*map & (1 << lun));
1041}
1042
1043void
1044ata_free_lun(u_int32_t *map, int lun)
1045{
1046    *map &= ~(1 << lun);
1047}
1048
1049int
1050ata_printf(struct ata_softc *scp, int device, const char * fmt, ...)
1051{
1052    va_list ap;
1053    int ret;
1054
1055    if (device == -1)
1056	ret = printf("ata%d: ", device_get_unit(scp->dev));
1057    else {
1058	if (scp->dev_name[ATA_DEV(device)])
1059	    ret = printf("%s: ", scp->dev_name[ATA_DEV(device)]);
1060	else
1061	    ret = printf("ata%d-%s: ", device_get_unit(scp->dev),
1062			 (device == ATA_MASTER) ? "master" : "slave");
1063    }
1064    va_start(ap, fmt);
1065    ret += vprintf(fmt, ap);
1066    va_end(ap);
1067    return ret;
1068}
1069
1070char *
1071ata_mode2str(int mode)
1072{
1073    switch (mode) {
1074    case ATA_PIO: return "BIOSPIO";
1075    case ATA_PIO0: return "PIO0";
1076    case ATA_PIO1: return "PIO1";
1077    case ATA_PIO2: return "PIO2";
1078    case ATA_PIO3: return "PIO3";
1079    case ATA_PIO4: return "PIO4";
1080    case ATA_WDMA2: return "WDMA2";
1081    case ATA_UDMA2: return "UDMA33";
1082    case ATA_UDMA4: return "UDMA66";
1083    case ATA_UDMA5: return "UDMA100";
1084    case ATA_DMA: return "BIOSDMA";
1085    default: return "???";
1086    }
1087}
1088
1089int
1090ata_pio2mode(int pio)
1091{
1092    switch (pio) {
1093    default:
1094    case 0: return ATA_PIO0;
1095    case 1: return ATA_PIO1;
1096    case 2: return ATA_PIO2;
1097    case 3: return ATA_PIO3;
1098    case 4: return ATA_PIO4;
1099    }
1100}
1101
1102int
1103ata_pmode(struct ata_params *ap)
1104{
1105    if (ap->atavalid & ATA_FLAG_64_70) {
1106	if (ap->apiomodes & 2)
1107	    return 4;
1108	if (ap->apiomodes & 1)
1109	    return 3;
1110    }
1111    if (ap->opiomode == 2)
1112	return 2;
1113    if (ap->opiomode == 1)
1114	return 1;
1115    if (ap->opiomode == 0)
1116	return 0;
1117    return -1;
1118}
1119
1120int
1121ata_wmode(struct ata_params *ap)
1122{
1123    if (ap->wdmamodes & 4)
1124	return 2;
1125    if (ap->wdmamodes & 2)
1126	return 1;
1127    if (ap->wdmamodes & 1)
1128	return 0;
1129    return -1;
1130}
1131
1132int
1133ata_umode(struct ata_params *ap)
1134{
1135    if (ap->atavalid & ATA_FLAG_88) {
1136	if (ap->udmamodes & 0x20)
1137	    return 5;
1138	if (ap->udmamodes & 0x10)
1139	    return 4;
1140	if (ap->udmamodes & 0x08)
1141	    return 3;
1142	if (ap->udmamodes & 0x04)
1143	    return 2;
1144	if (ap->udmamodes & 0x02)
1145	    return 1;
1146	if (ap->udmamodes & 0x01)
1147	    return 0;
1148    }
1149    return -1;
1150}
1151
1152static char *
1153active2str(int active)
1154{
1155    static char buf[64];
1156
1157    bzero(buf, sizeof(buf));
1158    if (active & ATA_IDLE)
1159	strcat(buf, "ATA_IDLE ");
1160    if (active & ATA_IMMEDIATE)
1161	strcat(buf, "ATA_IMMEDIATE ");
1162    if (active & ATA_WAIT_INTR)
1163	strcat(buf, "ATA_WAIT_INTR ");
1164    if (active & ATA_WAIT_READY)
1165	strcat(buf, "ATA_WAIT_READY ");
1166    if (active & ATA_ACTIVE)
1167	strcat(buf, "ATA_ACTIVE ");
1168    if (active & ATA_ACTIVE_ATA)
1169	strcat(buf, "ATA_ACTIVE_ATA ");
1170    if (active & ATA_ACTIVE_ATAPI)
1171	strcat(buf, "ATA_ACTIVE_ATAPI ");
1172    if (active & ATA_CONTROL)
1173	strcat(buf, "ATA_CONTROL ");
1174    return buf;
1175}
1176
1177static void
1178bswap(int8_t *buf, int len)
1179{
1180    u_int16_t *ptr = (u_int16_t*)(buf + len);
1181
1182    while (--ptr >= (u_int16_t*)buf)
1183	*ptr = ntohs(*ptr);
1184}
1185
1186static void
1187btrim(int8_t *buf, int len)
1188{
1189    int8_t *ptr;
1190
1191    for (ptr = buf; ptr < buf+len; ++ptr)
1192	if (!*ptr)
1193	    *ptr = ' ';
1194    for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1195	*ptr = 0;
1196}
1197
1198static void
1199bpack(int8_t *src, int8_t *dst, int len)
1200{
1201    int i, j, blank;
1202
1203    for (i = j = blank = 0 ; i < len; i++) {
1204	if (blank && src[i] == ' ') continue;
1205	if (blank && src[i] != ' ') {
1206	    dst[j++] = src[i];
1207	    blank = 0;
1208	    continue;
1209	}
1210	if (src[i] == ' ') {
1211	    blank = 1;
1212	    if (i == 0)
1213		continue;
1214	}
1215	dst[j++] = src[i];
1216    }
1217    if (j < len)
1218	dst[j] = 0x00;
1219}
1220
1221static void
1222ata_change_mode(struct ata_softc *scp, int device, int mode)
1223{
1224    int umode, wmode, pmode;
1225    int s = splbio();
1226
1227    while (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE))
1228	tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
1229
1230    umode = ata_umode(ATA_PARAM(scp, device));
1231    wmode = ata_wmode(ATA_PARAM(scp, device));
1232    pmode = ata_pmode(ATA_PARAM(scp, device));
1233
1234    switch (mode & ATA_DMA_MASK) {
1235    case ATA_UDMA:
1236	if ((mode & ATA_MODE_MASK) < umode)
1237	    umode = mode & ATA_MODE_MASK;
1238	break;
1239    case ATA_WDMA:
1240	if ((mode & ATA_MODE_MASK) < wmode)
1241	    wmode = mode & ATA_MODE_MASK;
1242	umode = -1;
1243	break;
1244    default:
1245	if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1246	    pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1247	umode = -1;
1248	wmode = -1;
1249    }
1250    ata_dmainit(scp, device, pmode, wmode, umode);
1251
1252    scp->active = ATA_IDLE;
1253    ata_start(scp);
1254    splx(s);
1255}
1256
1257static void
1258ata_init(void)
1259{
1260    /* register controlling device */
1261    make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0666, "ata");
1262
1263    /* register boot attach to be run when interrupts are enabled */
1264    if (!(ata_delayed_attach = (struct intr_config_hook *)
1265			       malloc(sizeof(struct intr_config_hook),
1266				      M_TEMP, M_NOWAIT | M_ZERO))) {
1267	printf("ata: malloc of delayed attach hook failed\n");
1268	return;
1269    }
1270
1271    ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1272    if (config_intrhook_establish(ata_delayed_attach) != 0) {
1273	printf("ata: config_intrhook_establish failed\n");
1274	free(ata_delayed_attach, M_TEMP);
1275    }
1276}
1277SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
1278