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