ata-all.c revision 149457
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1998 - 2005 S�ren Schmidt <sos@FreeBSD.org>
31590Srgrimes * All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer,
101590Srgrimes *    without modification, immediately at the beginning of the file.
111590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer in the
131590Srgrimes *    documentation and/or other materials provided with the distribution.
141590Srgrimes * 3. The name of the author may not be used to endorse or promote products
151590Srgrimes *    derived from this software without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
181590Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
191590Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
201590Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
211590Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
221590Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231590Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241590Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251590Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
261590Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271590Srgrimes */
281590Srgrimes
291590Srgrimes#include <sys/cdefs.h>
301590Srgrimes__FBSDID("$FreeBSD: head/sys/dev/ata/ata-all.c 149457 2005-08-25 15:45:03Z sos $");
311590Srgrimes
321590Srgrimes#include "opt_ata.h"
331590Srgrimes#include <sys/param.h>
341590Srgrimes#include <sys/systm.h>
3574769Smikeh#include <sys/ata.h>
361590Srgrimes#include <sys/kernel.h>
3774769Smikeh#include <sys/module.h>
381590Srgrimes#include <sys/endian.h>
3999112Sobrien#include <sys/ctype.h>
4099112Sobrien#include <sys/conf.h>
411590Srgrimes#include <sys/bus.h>
4291227Sbde#include <sys/bio.h>
4391227Sbde#include <sys/malloc.h>
441590Srgrimes#include <sys/sysctl.h>
451590Srgrimes#include <sys/sema.h>
461590Srgrimes#include <sys/taskqueue.h>
471590Srgrimes#include <vm/uma.h>
481590Srgrimes#include <machine/stdarg.h>
491590Srgrimes#include <machine/resource.h>
501590Srgrimes#include <machine/bus.h>
511590Srgrimes#include <sys/rman.h>
521590Srgrimes#ifdef __alpha__
5392921Simp#include <machine/md_var.h>
5477274Smikeh#endif
551590Srgrimes#include <dev/ata/ata-all.h>
561590Srgrimes#include <ata_if.h>
571590Srgrimes
581590Srgrimes/* device structure */
591590Srgrimesstatic  d_ioctl_t       ata_ioctl;
601590Srgrimesstatic struct cdevsw ata_cdevsw = {
611590Srgrimes	.d_version =    D_VERSION,
621590Srgrimes	.d_flags =      D_NEEDGIANT, /* we need this as newbus isn't mpsafe */
631590Srgrimes	.d_ioctl =      ata_ioctl,
641590Srgrimes	.d_name =       "ata",
6577274Smikeh};
661590Srgrimes
6777274Smikeh/* prototypes */
681590Srgrimesstatic void ata_interrupt(void *);
691590Srgrimesstatic void ata_boot_attach(void);
701590Srgrimesstatic device_t ata_add_child(device_t, struct ata_device *, int);
711590Srgrimesstatic void bswap(int8_t *, int);
721590Srgrimesstatic void btrim(int8_t *, int);
73173438Sddsstatic void bpack(int8_t *, int8_t *, int);
741590Srgrimes
751590Srgrimes/* global vars */
761590SrgrimesMALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
771590Srgrimesint (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
781590Srgrimesdevclass_t ata_devclass;
791590Srgrimesuma_zone_t ata_request_zone;
801590Srgrimesuma_zone_t ata_composite_zone;
8177274Smikehint ata_wc = 1;
821590Srgrimes
831590Srgrimes/* local vars */
841590Srgrimesstatic struct intr_config_hook *ata_delayed_attach = NULL;
851590Srgrimesstatic int ata_dma = 1;
861590Srgrimesstatic int atapi_dma = 1;
871590Srgrimes
8877274Smikeh/* sysctl vars */
891590SrgrimesSYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
901590SrgrimesTUNABLE_INT("hw.ata.ata_dma", &ata_dma);
911590SrgrimesSYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
921590Srgrimes	   "ATA disk DMA mode control");
931590SrgrimesTUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
941590SrgrimesSYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
951590Srgrimes	   "ATAPI device DMA mode control");
961590SrgrimesTUNABLE_INT("hw.ata.wc", &ata_wc);
971590SrgrimesSYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
9877274Smikeh	   "ATA disk write caching");
991590Srgrimes
1001590Srgrimes/*
1011590Srgrimes * newbus device interface related functions
1021590Srgrimes */
1031590Srgrimesint
1041590Srgrimesata_probe(device_t dev)
1051590Srgrimes{
1061590Srgrimes    return 0;
1071590Srgrimes}
1081590Srgrimes
1091590Srgrimesint
1101590Srgrimesata_attach(device_t dev)
1111590Srgrimes{
1121590Srgrimes    struct ata_channel *ch = device_get_softc(dev);
1131590Srgrimes    int error, rid;
1141590Srgrimes
1151590Srgrimes    /* check that we have a virgin channel to attach */
1161590Srgrimes    if (ch->r_irq)
11777274Smikeh	return EEXIST;
11877274Smikeh
1191590Srgrimes    /* initialize the softc basics */
1201590Srgrimes    ch->dev = dev;
1211590Srgrimes    ch->state = ATA_IDLE;
1221590Srgrimes    bzero(&ch->state_mtx, sizeof(struct mtx));
1231590Srgrimes    mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
1241590Srgrimes    bzero(&ch->queue_mtx, sizeof(struct mtx));
1251590Srgrimes    mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
1261590Srgrimes    TAILQ_INIT(&ch->ata_queue);
1271590Srgrimes
12877274Smikeh    /* reset the controller HW, the channel and device(s) */
1291590Srgrimes    while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
13077274Smikeh	tsleep(&error, PRIBIO, "ataatch", 1);
1318874Srgrimes    ATA_RESET(dev);
13277274Smikeh    ATA_LOCKING(dev, ATA_LF_UNLOCK);
1331590Srgrimes
1341590Srgrimes    /* setup interrupt delivery */
1351590Srgrimes    rid = ATA_IRQ_RID;
1361590Srgrimes    ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
13777274Smikeh				       RF_SHAREABLE | RF_ACTIVE);
1381590Srgrimes    if (!ch->r_irq) {
1391590Srgrimes	device_printf(dev, "unable to allocate interrupt\n");
1401590Srgrimes	return ENXIO;
14177274Smikeh    }
1421590Srgrimes    if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS,
1431590Srgrimes				ata_interrupt, ch, &ch->ih))) {
14477274Smikeh	device_printf(dev, "unable to setup interrupt\n");
1451590Srgrimes	return error;
14677274Smikeh    }
14777274Smikeh
14877274Smikeh    /* probe and attach devices on this channel unless we are in early boot */
1491590Srgrimes    if (!ata_delayed_attach)
1501590Srgrimes	ata_identify(dev);
1511590Srgrimes    return 0;
15277274Smikeh}
1531590Srgrimes
15477274Smikehint
1551590Srgrimesata_detach(device_t dev)
1561590Srgrimes{
15777274Smikeh    struct ata_channel *ch = device_get_softc(dev);
15827643Scharnier    device_t *children;
1591590Srgrimes    int nchildren, i;
1601590Srgrimes
16177274Smikeh    /* check that we have a vaild channel to detach */
1621590Srgrimes    if (!ch->r_irq)
1631590Srgrimes	return ENXIO;
1641590Srgrimes
1651590Srgrimes    /* detach & delete all children */
1661590Srgrimes    if (!device_get_children(dev, &children, &nchildren)) {
1671590Srgrimes	for (i = 0; i < nchildren; i++)
1681590Srgrimes	    if (children[i])
1691590Srgrimes		device_delete_child(dev, children[i]);
1701590Srgrimes	free(children, M_TEMP);
1711590Srgrimes    }
17277274Smikeh
1731590Srgrimes    /* release resources */
17477274Smikeh    bus_teardown_intr(dev, ch->r_irq, ch->ih);
1751590Srgrimes    bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
1761590Srgrimes    ch->r_irq = NULL;
1771590Srgrimes    mtx_destroy(&ch->state_mtx);
17877274Smikeh    mtx_destroy(&ch->queue_mtx);
17977274Smikeh    return 0;
1801590Srgrimes}
1811590Srgrimes
1821590Srgrimesint
18377274Smikehata_reinit(device_t dev)
1841590Srgrimes{
18577274Smikeh    struct ata_channel *ch = device_get_softc(dev);
18688227Sache    device_t *children;
18777274Smikeh    int nchildren, i;
1881590Srgrimes
1891590Srgrimes    /* check that we have a vaild channel to reinit */
1901590Srgrimes    if (!ch || !ch->r_irq)
1911590Srgrimes	return ENXIO;
1921590Srgrimes
1931590Srgrimes    if (bootverbose)
1941590Srgrimes	device_printf(dev, "reiniting channel ..\n");
1951590Srgrimes
1961590Srgrimes    /* poll for locking the channel */
1971590Srgrimes    while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
1981590Srgrimes	tsleep(&dev, PRIBIO, "atarini", 1);
1991590Srgrimes
2001590Srgrimes    /* unconditionally grap the channel lock */
2011590Srgrimes    mtx_lock(&ch->state_mtx);
2021590Srgrimes    ch->state = ATA_STALL_QUEUE;
2031590Srgrimes    mtx_unlock(&ch->state_mtx);
2041590Srgrimes
2051590Srgrimes    /* reset the controller HW, the channel and device(s) */
2061590Srgrimes    ATA_RESET(dev);
2071590Srgrimes
2081590Srgrimes    /* reinit the children and delete any that fails */
2091590Srgrimes    if (!device_get_children(dev, &children, &nchildren)) {
2101590Srgrimes	mtx_lock(&Giant);       /* newbus suckage it needs Giant */
2111590Srgrimes	for (i = 0; i < nchildren; i++) {
2121590Srgrimes	    if (children[i] && device_is_attached(children[i]))
2131590Srgrimes		if (ATA_REINIT(children[i])) {
2141590Srgrimes		    /*
2151590Srgrimes		     * if we have a running request and its device matches
2161590Srgrimes		     * this child we need to inform the request that the
2171590Srgrimes		     * device is gone and remove it from ch->running
2181590Srgrimes		     */
21977274Smikeh		    if (ch->running && ch->running->dev == children[i]) {
2201590Srgrimes			device_printf(ch->running->dev,
2211590Srgrimes				      "FAILURE - device detached\n");
2221590Srgrimes			ch->running->dev = NULL;
2231590Srgrimes			ch->running = NULL;
2241590Srgrimes		    }
2251590Srgrimes		    device_delete_child(dev, children[i]);
2261590Srgrimes		}
2271590Srgrimes	}
2281590Srgrimes	free(children, M_TEMP);
2291590Srgrimes	mtx_unlock(&Giant);     /* newbus suckage dealt with, release Giant */
2301590Srgrimes    }
23177274Smikeh
2321590Srgrimes    /* catch request in ch->running if we havn't already */
23377274Smikeh    ata_catch_inflight(dev);
2341590Srgrimes
23577274Smikeh    /* we're done release the channel for new work */
2361590Srgrimes    mtx_lock(&ch->state_mtx);
2371590Srgrimes    ch->state = ATA_IDLE;
2381590Srgrimes    mtx_unlock(&ch->state_mtx);
2391590Srgrimes    ATA_LOCKING(dev, ATA_LF_UNLOCK);
24077274Smikeh
2411590Srgrimes    if (bootverbose)
2421590Srgrimes	device_printf(dev, "reinit done ..\n");
2431590Srgrimes
2441590Srgrimes    /* kick off requests on the queue */
24577274Smikeh    ata_start(dev);
2461590Srgrimes    return 0;
2471590Srgrimes}
2481590Srgrimes
24974769Smikehint
25074769Smikehata_suspend(device_t dev)
2511590Srgrimes{
2521590Srgrimes    struct ata_channel *ch;
25374769Smikeh
25477274Smikeh    /* check for valid device */
25577274Smikeh    if (!dev || !(ch = device_get_softc(dev)))
25674769Smikeh	return ENXIO;
2571590Srgrimes
2581590Srgrimes    /* wait for the channel to be IDLE before entering suspend mode */
25974769Smikeh    while (1) {
26074769Smikeh	mtx_lock(&ch->state_mtx);
26188227Sache	if (ch->state == ATA_IDLE) {
2621590Srgrimes	    ch->state = ATA_ACTIVE;
2631590Srgrimes	    mtx_unlock(&ch->state_mtx);
2641590Srgrimes	    break;
2651590Srgrimes	}
2661590Srgrimes	mtx_unlock(&ch->state_mtx);
2671590Srgrimes	tsleep(ch, PRIBIO, "atasusp", hz/10);
2681590Srgrimes    }
2691590Srgrimes    ATA_LOCKING(dev, ATA_LF_UNLOCK);
2701590Srgrimes    return 0;
2711590Srgrimes}
2721590Srgrimes
2731590Srgrimesint
2741590Srgrimesata_resume(device_t dev)
27518532Sbde{
27618532Sbde    struct ata_channel *ch;
27718532Sbde    int error;
2781590Srgrimes
2791590Srgrimes    /* check for valid device */
2801590Srgrimes    if (!dev || !(ch = device_get_softc(dev)))
2811590Srgrimes	return ENXIO;
2821590Srgrimes
2831590Srgrimes    /* reinit the devices, we dont know what mode/state they are in */
2841590Srgrimes    error = ata_reinit(dev);
2851590Srgrimes
2861590Srgrimes    /* kick off requests on the queue */
2871590Srgrimes    ata_start(dev);
2881590Srgrimes    return error;
2891590Srgrimes}
2901590Srgrimes
29177274Smikehstatic void
29277274Smikehata_interrupt(void *data)
2931590Srgrimes{
29474769Smikeh    struct ata_channel *ch = (struct ata_channel *)data;
29577274Smikeh    struct ata_request *request;
2961590Srgrimes
29718532Sbde    mtx_lock(&ch->state_mtx);
2981590Srgrimes    do {
29977274Smikeh	/* do we have a running request */
30077274Smikeh	if (!(request = ch->running) || (request->flags & ATA_R_TIMEOUT))
3011590Srgrimes	    break;
3021590Srgrimes
3031590Srgrimes	ATA_DEBUG_RQ(request, "interrupt");
3041590Srgrimes
3051590Srgrimes	/* ignore interrupt if device is busy */
3061590Srgrimes	if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
3071590Srgrimes	    DELAY(100);
3081590Srgrimes	    if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
3091590Srgrimes		break;
31077274Smikeh	}
3111590Srgrimes
3121590Srgrimes	/* check for the right state */
3131590Srgrimes	if (ch->state != ATA_ACTIVE && ch->state != ATA_STALL_QUEUE) {
3141590Srgrimes	    device_printf(request->dev,
3151590Srgrimes			  "interrupt state=%d unexpected\n", ch->state);
3161590Srgrimes	    break;
3171590Srgrimes	}
3181590Srgrimes
3191590Srgrimes	/*
3201590Srgrimes	 * we have the HW locks, so end the tranaction for this request
3211590Srgrimes	 * if it finishes immediately otherwise wait for next interrupt
3221590Srgrimes	 */
32377274Smikeh	if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
3241590Srgrimes	    ch->running = NULL;
32577274Smikeh	    if (ch->state == ATA_ACTIVE)
3261590Srgrimes		ch->state = ATA_IDLE;
3271590Srgrimes	    mtx_unlock(&ch->state_mtx);
3281590Srgrimes	    ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
3291590Srgrimes	    ata_finish(request);
3301590Srgrimes	    return;
3311590Srgrimes	}
3321590Srgrimes    } while (0);
3331590Srgrimes    mtx_unlock(&ch->state_mtx);
33477274Smikeh}
3351590Srgrimes
3361590Srgrimes/*
3371590Srgrimes * device related interfaces
3381590Srgrimes */
3391590Srgrimesstatic int
3401590Srgrimesata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
3411590Srgrimes	  int32_t flag, struct thread *td)
3421590Srgrimes{
3431590Srgrimes    device_t device, *children;
3441590Srgrimes    struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
3451590Srgrimes    int *value = (int *)data;
3461590Srgrimes    int i, nchildren, error = ENOTTY;
3471590Srgrimes
3481590Srgrimes    switch (cmd) {
3491590Srgrimes    case IOCATAGMAXCHANNEL:
35088150Smikeh	*value = devclass_get_maxunit(ata_devclass);
35188150Smikeh	error = 0;
352205793Sed	break;
3531590Srgrimes
3541590Srgrimes    case IOCATAREINIT:
3551590Srgrimes	if (*value > devclass_get_maxunit(ata_devclass) ||
3561590Srgrimes	    !(device = devclass_get_device(ata_devclass, *value)))
3571590Srgrimes	    return ENXIO;
3581590Srgrimes	error = ata_reinit(device);
3591590Srgrimes	ata_start(device);
3601590Srgrimes	break;
3611590Srgrimes
3621590Srgrimes    case IOCATAATTACH:
36377274Smikeh	if (*value > devclass_get_maxunit(ata_devclass) ||
3641590Srgrimes	    !(device = devclass_get_device(ata_devclass, *value)))
3651590Srgrimes	    return ENXIO;
36677274Smikeh	/* XXX SOS should enable channel HW on controller */
3671590Srgrimes	error = ata_attach(device);
3681590Srgrimes	break;
3691590Srgrimes
37077274Smikeh    case IOCATADETACH:
37174769Smikeh	if (*value > devclass_get_maxunit(ata_devclass) ||
3721590Srgrimes	    !(device = devclass_get_device(ata_devclass, *value)))
3731590Srgrimes	    return ENXIO;
3741590Srgrimes	error = ata_detach(device);
3751590Srgrimes	/* XXX SOS should disable channel HW on controller */
37677274Smikeh	break;
37777274Smikeh
3781590Srgrimes    case IOCATADEVICES:
3791590Srgrimes	if (devices->channel > devclass_get_maxunit(ata_devclass) ||
3801590Srgrimes	    !(device = devclass_get_device(ata_devclass, devices->channel)))
3811590Srgrimes	    return ENXIO;
3821590Srgrimes	bzero(devices->name[0], 32);
3831590Srgrimes	bzero(&devices->params[0], sizeof(struct ata_params));
3841590Srgrimes	bzero(devices->name[1], 32);
3851590Srgrimes	bzero(&devices->params[1], sizeof(struct ata_params));
38677274Smikeh	if (!device_get_children(device, &children, &nchildren)) {
3871590Srgrimes	    for (i = 0; i < nchildren; i++) {
38877274Smikeh		if (children[i] && device_is_attached(children[i])) {
3891590Srgrimes		    struct ata_device *atadev = device_get_softc(children[i]);
3901590Srgrimes
3911590Srgrimes		    if (atadev->unit == ATA_MASTER) {
3921590Srgrimes			strncpy(devices->name[0],
3931590Srgrimes				device_get_nameunit(children[i]), 32);
3941590Srgrimes			bcopy(&atadev->param, &devices->params[0],
3951590Srgrimes			      sizeof(struct ata_params));
3961590Srgrimes		    }
3971590Srgrimes		    if (atadev->unit == ATA_SLAVE) {
3981590Srgrimes			strncpy(devices->name[1],
3991590Srgrimes				device_get_nameunit(children[i]), 32);
4001590Srgrimes			bcopy(&atadev->param, &devices->params[1],
4011590Srgrimes			      sizeof(struct ata_params));
4021590Srgrimes		    }
4031590Srgrimes		}
40477274Smikeh	    }
4051590Srgrimes	    free(children, M_TEMP);
4061590Srgrimes	    error = 0;
4071590Srgrimes	}
4081590Srgrimes	else
4091590Srgrimes	    error = ENODEV;
4101590Srgrimes	break;
4111590Srgrimes
4121590Srgrimes    default:
4131590Srgrimes	if (ata_raid_ioctl_func)
4141590Srgrimes	    error = ata_raid_ioctl_func(cmd, data);
41577274Smikeh    }
41677274Smikeh    return error;
4171590Srgrimes}
41877274Smikeh
41977274Smikehint
42077274Smikehata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
42177274Smikeh{
42277274Smikeh    struct ata_device *atadev = device_get_softc(dev);
42374769Smikeh    struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
42474769Smikeh    struct ata_params *params = (struct ata_params *)data;
42577274Smikeh    int *mode = (int *)data;
42674769Smikeh    struct ata_request *request;
4271590Srgrimes    caddr_t buf;
4281590Srgrimes    int error;
4291590Srgrimes
43077274Smikeh    switch (cmd) {
4311590Srgrimes    case IOCATAREQUEST:
4321590Srgrimes	if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
4331590Srgrimes	    return ENOMEM;
4341590Srgrimes	}
4351590Srgrimes	if (!(request = ata_alloc_request())) {
4361590Srgrimes	    free(buf, M_ATA);
4371590Srgrimes	    return  ENOMEM;
4381590Srgrimes	}
4391590Srgrimes	if (ioc_request->flags & ATA_CMD_WRITE) {
4401590Srgrimes	    error = copyin(ioc_request->data, buf, ioc_request->count);
4411590Srgrimes	    if (error) {
44277274Smikeh		free(buf, M_ATA);
4431590Srgrimes		ata_free_request(request);
4441590Srgrimes		return error;
4451590Srgrimes	    }
4461590Srgrimes	}
4471590Srgrimes	request->dev = dev;
44877274Smikeh	if (ioc_request->flags & ATA_CMD_ATAPI) {
4491590Srgrimes	    request->flags = ATA_R_ATAPI;
4501590Srgrimes	    bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
4511590Srgrimes	}
4521590Srgrimes	else {
4531590Srgrimes	    request->u.ata.command = ioc_request->u.ata.command;
4541590Srgrimes	    request->u.ata.feature = ioc_request->u.ata.feature;
4551590Srgrimes	    request->u.ata.lba = ioc_request->u.ata.lba;
4561590Srgrimes	    request->u.ata.count = ioc_request->u.ata.count;
4571590Srgrimes	}
4581590Srgrimes	request->timeout = ioc_request->timeout;
4591590Srgrimes	request->data = buf;
4601590Srgrimes	request->bytecount = ioc_request->count;
4611590Srgrimes	request->transfersize = request->bytecount;
4621590Srgrimes	if (ioc_request->flags & ATA_CMD_CONTROL)
4631590Srgrimes	    request->flags |= ATA_R_CONTROL;
4641590Srgrimes	if (ioc_request->flags & ATA_CMD_READ)
4651590Srgrimes	    request->flags |= ATA_R_READ;
4661590Srgrimes	if (ioc_request->flags & ATA_CMD_WRITE)
4671590Srgrimes	    request->flags |= ATA_R_WRITE;
4681590Srgrimes	ata_queue_request(request);
4691590Srgrimes	if (!(request->flags & ATA_R_ATAPI)) {
4701590Srgrimes	    ioc_request->u.ata.command = request->u.ata.command;
4711590Srgrimes	    ioc_request->u.ata.feature = request->u.ata.feature;
4721590Srgrimes	    ioc_request->u.ata.lba = request->u.ata.lba;
4731590Srgrimes	    ioc_request->u.ata.count = request->u.ata.count;
4741590Srgrimes	}
47577274Smikeh	ioc_request->error = request->result;
4761590Srgrimes	if (ioc_request->flags & ATA_CMD_READ)
4771590Srgrimes	    error = copyout(buf, ioc_request->data, ioc_request->count);
4781590Srgrimes	else
4791590Srgrimes	    error = 0;
48077274Smikeh	free(buf, M_ATA);
4811590Srgrimes	ata_free_request(request);
4821590Srgrimes	return error;
4831590Srgrimes
48477274Smikeh    case IOCATAGPARM:
4851590Srgrimes	bcopy(&atadev->param, params, sizeof(struct ata_params));
4861590Srgrimes	return 0;
4871590Srgrimes
4881590Srgrimes    case IOCATASMODE:
4891590Srgrimes	atadev->mode = *mode;
4901590Srgrimes	ATA_SETMODE(device_get_parent(dev), dev);
491102412Scharnier	return 0;
4921590Srgrimes
4931590Srgrimes    case IOCATAGMODE:
4941590Srgrimes	*mode = atadev->mode;
4951590Srgrimes	return 0;
4961590Srgrimes    default:
4971590Srgrimes	return ENOTTY;
4981590Srgrimes    }
499208592Suqs}
500208592Suqs
5011590Srgrimesstatic void
502208592Suqsata_boot_attach(void)
503208592Suqs{
5041590Srgrimes    struct ata_channel *ch;
5051590Srgrimes    int ctlr;
5061590Srgrimes
5071590Srgrimes    /* release the hook that got us here, only needed during boot */
5081590Srgrimes    if (ata_delayed_attach) {
50977274Smikeh	config_intrhook_disestablish(ata_delayed_attach);
5101590Srgrimes	free(ata_delayed_attach, M_TEMP);
51183848Smikeh	ata_delayed_attach = NULL;
51283848Smikeh    }
51377274Smikeh
5141590Srgrimes    /* kick of probe and attach on all channels */
5151590Srgrimes    for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
5161590Srgrimes	if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
5171590Srgrimes	    ata_identify(ch->dev);
5181590Srgrimes	}
5191590Srgrimes    }
5201590Srgrimes}
5211590Srgrimes
5221590Srgrimes
5231590Srgrimes/*
5241590Srgrimes * misc support functions
52577274Smikeh */
5261590Srgrimesstatic device_t
5271590Srgrimesata_add_child(device_t parent, struct ata_device *atadev, int unit)
5281590Srgrimes{
5291590Srgrimes    device_t child;
53077274Smikeh
53177274Smikeh    if ((child = device_add_child(parent, NULL, unit))) {
5321590Srgrimes	device_set_softc(child, atadev);
5331590Srgrimes	device_quiet(child);
53477274Smikeh	atadev->dev = child;
53577274Smikeh	atadev->max_iosize = DEV_BSIZE;
53677274Smikeh	atadev->mode = ATA_PIO_MAX;
53777274Smikeh    }
5381590Srgrimes    return child;
53927643Scharnier}
5401590Srgrimes
54177274Smikehstatic int
5421590Srgrimesata_getparam(device_t parent, struct ata_device *atadev)
54377274Smikeh{
5441590Srgrimes    struct ata_channel *ch = device_get_softc(parent);
5451590Srgrimes    struct ata_request *request;
5461590Srgrimes    u_int8_t command = 0;
5471590Srgrimes    int error = ENOMEM, retries = 2;
54877274Smikeh
54977274Smikeh    if (ch->devices &
5501590Srgrimes	(atadev->unit == ATA_MASTER ? ATA_ATA_MASTER : ATA_ATA_SLAVE))
5511590Srgrimes	command = ATA_ATA_IDENTIFY;
5521590Srgrimes    if (ch->devices &
55377274Smikeh	(atadev->unit == ATA_MASTER ? ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE))
55474769Smikeh	command = ATA_ATAPI_IDENTIFY;
55577274Smikeh    if (!command)
5561590Srgrimes	return ENXIO;
55777274Smikeh
55874769Smikeh    while (retries-- > 0 && error) {
5591590Srgrimes	if (!(request = ata_alloc_request()))
56074769Smikeh	    break;
5611590Srgrimes	request->dev = atadev->dev;
5621590Srgrimes	request->timeout = 1;
5631590Srgrimes	request->retries = 0;
56474769Smikeh	request->u.ata.command = command;
5651590Srgrimes	request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT|ATA_R_QUIET);
5661590Srgrimes	request->data = (void *)&atadev->param;
5671590Srgrimes	request->bytecount = sizeof(struct ata_params);
56874769Smikeh	request->donecount = 0;
5691590Srgrimes	request->transfersize = DEV_BSIZE;
5701590Srgrimes	ata_queue_request(request);
57174769Smikeh	error = request->result;
57274769Smikeh	ata_free_request(request);
5731590Srgrimes    }
5741590Srgrimes
5751590Srgrimes    if (!error && (isprint(atadev->param.model[0]) ||
5761590Srgrimes		   isprint(atadev->param.model[1]))) {
5771590Srgrimes	struct ata_params *atacap = &atadev->param;
57877274Smikeh	char buffer[64];
5791590Srgrimes#if BYTE_ORDER == BIG_ENDIAN
5801590Srgrimes	int16_t *ptr;
5811590Srgrimes
5821590Srgrimes	for (ptr = (int16_t *)atacap;
5831590Srgrimes	     ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
5841590Srgrimes	    *ptr = bswap16(*ptr);
5851590Srgrimes	}
5861590Srgrimes#endif
5871590Srgrimes	if (!(!strncmp(atacap->model, "FX", 2) ||
5881590Srgrimes	      !strncmp(atacap->model, "NEC", 3) ||
58977274Smikeh	      !strncmp(atacap->model, "Pioneer", 7) ||
59077274Smikeh	      !strncmp(atacap->model, "SHARP", 5))) {
5911590Srgrimes	    bswap(atacap->model, sizeof(atacap->model));
59277274Smikeh	    bswap(atacap->revision, sizeof(atacap->revision));
5931590Srgrimes	    bswap(atacap->serial, sizeof(atacap->serial));
5941590Srgrimes	}
59577274Smikeh	btrim(atacap->model, sizeof(atacap->model));
5961590Srgrimes	bpack(atacap->model, atacap->model, sizeof(atacap->model));
5971590Srgrimes	btrim(atacap->revision, sizeof(atacap->revision));
5981590Srgrimes	bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
5991590Srgrimes	btrim(atacap->serial, sizeof(atacap->serial));
6001590Srgrimes	bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
6011590Srgrimes	sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
6021590Srgrimes	device_set_desc_copy(atadev->dev, buffer);
60377274Smikeh	if (bootverbose)
6041590Srgrimes	    printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
6051590Srgrimes		   ch->unit, atadev->unit == ATA_MASTER ? "master":"slave",
60674769Smikeh		   ata_mode2str(ata_pmode(atacap)),
6071590Srgrimes		   ata_mode2str(ata_wmode(atacap)),
6081590Srgrimes		   ata_mode2str(ata_umode(atacap)),
60977274Smikeh		   (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
6101590Srgrimes
6111590Srgrimes	if (atadev->param.config & ATA_PROTO_ATAPI) {
6121590Srgrimes	    if (atapi_dma && ch->dma &&
6131590Srgrimes		(atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
61474769Smikeh		ata_umode(&atadev->param) >= ATA_UDMA2)
6151590Srgrimes		atadev->mode = ATA_DMA_MAX;
6161590Srgrimes	}
6171590Srgrimes	else {
6181590Srgrimes	    if (ata_dma && ch->dma &&
6191590Srgrimes		(ata_umode(&atadev->param) > 0 ||
6201590Srgrimes		 ata_wmode(&atadev->param) > 0))
6211590Srgrimes		atadev->mode = ATA_DMA_MAX;
6221590Srgrimes	}
62377274Smikeh    }
6241590Srgrimes    else {
6251590Srgrimes	if (!error)
62677274Smikeh	    error = ENXIO;
6271590Srgrimes    }
62877274Smikeh    return error;
6291590Srgrimes}
6301590Srgrimes
6311590Srgrimesint
6321590Srgrimesata_identify(device_t dev)
6331590Srgrimes{
634    struct ata_channel *ch = device_get_softc(dev);
635    struct ata_device *master = NULL, *slave = NULL;
636    device_t master_child = NULL, slave_child = NULL;
637    int master_unit = -1, slave_unit = -1;
638
639    if (ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) {
640	if (!(master = malloc(sizeof(struct ata_device),
641			      M_ATA, M_NOWAIT | M_ZERO))) {
642	    device_printf(dev, "out of memory\n");
643	    return ENOMEM;
644	}
645	master->unit = ATA_MASTER;
646    }
647    if (ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) {
648	if (!(slave = malloc(sizeof(struct ata_device),
649			     M_ATA, M_NOWAIT | M_ZERO))) {
650	    free(master, M_ATA);
651	    device_printf(dev, "out of memory\n");
652	    return ENOMEM;
653	}
654	slave->unit = ATA_SLAVE;
655    }
656
657#ifdef ATA_STATIC_ID
658    if (ch->devices & ATA_ATA_MASTER)
659	master_unit = (device_get_unit(dev) << 1);
660#endif
661    if (master && !(master_child = ata_add_child(dev, master, master_unit))) {
662	free(master, M_ATA);
663	master = NULL;
664    }
665#ifdef ATA_STATIC_ID
666    if (ch->devices & ATA_ATA_SLAVE)
667	slave_unit = (device_get_unit(dev) << 1) + 1;
668#endif
669    if (slave && !(slave_child = ata_add_child(dev, slave, slave_unit))) {
670	free(slave, M_ATA);
671	slave = NULL;
672    }
673
674    if (slave && ata_getparam(dev, slave)) {
675	device_delete_child(dev, slave_child);
676	free(slave, M_ATA);
677    }
678    if (master && ata_getparam(dev, master)) {
679	device_delete_child(dev, master_child);
680	free(master, M_ATA);
681    }
682
683    bus_generic_probe(dev);
684    bus_generic_attach(dev);
685    return 0;
686}
687
688void
689ata_default_registers(device_t dev)
690{
691    struct ata_channel *ch = device_get_softc(dev);
692
693    /* fill in the defaults from whats setup already */
694    ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
695    ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
696    ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
697    ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
698    ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
699    ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
700    ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
701    ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
702}
703
704void
705ata_modify_if_48bit(struct ata_request *request)
706{
707    struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
708    struct ata_device *atadev = device_get_softc(request->dev);
709
710    atadev->flags &= ~ATA_D_48BIT_ACTIVE;
711
712    if ((request->u.ata.lba >= ATA_MAX_28BIT_LBA ||
713	 request->u.ata.count > 256) &&
714	atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
715
716	/* translate command into 48bit version */
717	switch (request->u.ata.command) {
718	case ATA_READ:
719	    request->u.ata.command = ATA_READ48;
720	    break;
721	case ATA_READ_MUL:
722	    request->u.ata.command = ATA_READ_MUL48;
723	    break;
724	case ATA_READ_DMA:
725	    if (ch->flags & ATA_NO_48BIT_DMA) {
726		if (request->transfersize > DEV_BSIZE)
727		    request->u.ata.command = ATA_READ_MUL48;
728		else
729		    request->u.ata.command = ATA_READ48;
730		request->flags &= ~ATA_R_DMA;
731	    }
732	    else
733		request->u.ata.command = ATA_READ_DMA48;
734	    break;
735	case ATA_READ_DMA_QUEUED:
736	    if (ch->flags & ATA_NO_48BIT_DMA) {
737		if (request->transfersize > DEV_BSIZE)
738		    request->u.ata.command = ATA_READ_MUL48;
739		else
740		    request->u.ata.command = ATA_READ48;
741		request->flags &= ~ATA_R_DMA;
742	    }
743	    else
744		request->u.ata.command = ATA_READ_DMA_QUEUED48;
745	    break;
746	case ATA_WRITE:
747	    request->u.ata.command = ATA_WRITE48;
748	    break;
749	case ATA_WRITE_MUL:
750	    request->u.ata.command = ATA_WRITE_MUL48;
751	    break;
752	case ATA_WRITE_DMA:
753	    if (ch->flags & ATA_NO_48BIT_DMA) {
754		if (request->transfersize > DEV_BSIZE)
755		    request->u.ata.command = ATA_WRITE_MUL48;
756		else
757		    request->u.ata.command = ATA_WRITE48;
758		request->flags &= ~ATA_R_DMA;
759	    }
760	    else
761		request->u.ata.command = ATA_WRITE_DMA48;
762	    break;
763	case ATA_WRITE_DMA_QUEUED:
764	    if (ch->flags & ATA_NO_48BIT_DMA) {
765		if (request->transfersize > DEV_BSIZE)
766		    request->u.ata.command = ATA_WRITE_MUL48;
767		else
768		    request->u.ata.command = ATA_WRITE48;
769		request->u.ata.command = ATA_WRITE48;
770		request->flags &= ~ATA_R_DMA;
771	    }
772	    else
773		request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
774	    break;
775	case ATA_FLUSHCACHE:
776	    request->u.ata.command = ATA_FLUSHCACHE48;
777	    break;
778	default:
779	    return;
780	}
781	atadev->flags |= ATA_D_48BIT_ACTIVE;
782    }
783}
784
785void
786ata_udelay(int interval)
787{
788    /* for now just use DELAY, the timer/sleep subsytems are not there yet */
789    if (1 || interval < (1000000/hz) || ata_delayed_attach)
790	DELAY(interval);
791    else
792	tsleep(&interval, PRIBIO, "ataslp", interval/(1000000/hz));
793}
794
795char *
796ata_mode2str(int mode)
797{
798    switch (mode) {
799    case ATA_PIO0: return "PIO0";
800    case ATA_PIO1: return "PIO1";
801    case ATA_PIO2: return "PIO2";
802    case ATA_PIO3: return "PIO3";
803    case ATA_PIO4: return "PIO4";
804    case ATA_WDMA0: return "WDMA0";
805    case ATA_WDMA1: return "WDMA1";
806    case ATA_WDMA2: return "WDMA2";
807    case ATA_UDMA0: return "UDMA16";
808    case ATA_UDMA1: return "UDMA25";
809    case ATA_UDMA2: return "UDMA33";
810    case ATA_UDMA3: return "UDMA40";
811    case ATA_UDMA4: return "UDMA66";
812    case ATA_UDMA5: return "UDMA100";
813    case ATA_UDMA6: return "UDMA133";
814    case ATA_SA150: return "SATA150";
815    case ATA_SA300: return "SATA300";
816    default:
817	if (mode & ATA_DMA_MASK)
818	    return "BIOSDMA";
819	else
820	    return "BIOSPIO";
821    }
822}
823
824int
825ata_pmode(struct ata_params *ap)
826{
827    if (ap->atavalid & ATA_FLAG_64_70) {
828	if (ap->apiomodes & 0x02)
829	    return ATA_PIO4;
830	if (ap->apiomodes & 0x01)
831	    return ATA_PIO3;
832    }
833    if (ap->mwdmamodes & 0x04)
834	return ATA_PIO4;
835    if (ap->mwdmamodes & 0x02)
836	return ATA_PIO3;
837    if (ap->mwdmamodes & 0x01)
838	return ATA_PIO2;
839    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
840	return ATA_PIO2;
841    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
842	return ATA_PIO1;
843    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
844	return ATA_PIO0;
845    return ATA_PIO0;
846}
847
848int
849ata_wmode(struct ata_params *ap)
850{
851    if (ap->mwdmamodes & 0x04)
852	return ATA_WDMA2;
853    if (ap->mwdmamodes & 0x02)
854	return ATA_WDMA1;
855    if (ap->mwdmamodes & 0x01)
856	return ATA_WDMA0;
857    return -1;
858}
859
860int
861ata_umode(struct ata_params *ap)
862{
863    if (ap->atavalid & ATA_FLAG_88) {
864	if (ap->udmamodes & 0x40)
865	    return ATA_UDMA6;
866	if (ap->udmamodes & 0x20)
867	    return ATA_UDMA5;
868	if (ap->udmamodes & 0x10)
869	    return ATA_UDMA4;
870	if (ap->udmamodes & 0x08)
871	    return ATA_UDMA3;
872	if (ap->udmamodes & 0x04)
873	    return ATA_UDMA2;
874	if (ap->udmamodes & 0x02)
875	    return ATA_UDMA1;
876	if (ap->udmamodes & 0x01)
877	    return ATA_UDMA0;
878    }
879    return -1;
880}
881
882int
883ata_limit_mode(device_t dev, int mode, int maxmode)
884{
885    struct ata_device *atadev = device_get_softc(dev);
886
887    if (maxmode && mode > maxmode)
888	mode = maxmode;
889
890    if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
891	return min(mode, ata_umode(&atadev->param));
892
893    if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
894	return min(mode, ata_wmode(&atadev->param));
895
896    if (mode > ata_pmode(&atadev->param))
897	return min(mode, ata_pmode(&atadev->param));
898
899    return mode;
900}
901
902static void
903bswap(int8_t *buf, int len)
904{
905    u_int16_t *ptr = (u_int16_t*)(buf + len);
906
907    while (--ptr >= (u_int16_t*)buf)
908	*ptr = ntohs(*ptr);
909}
910
911static void
912btrim(int8_t *buf, int len)
913{
914    int8_t *ptr;
915
916    for (ptr = buf; ptr < buf+len; ++ptr)
917	if (!*ptr || *ptr == '_')
918	    *ptr = ' ';
919    for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
920	*ptr = 0;
921}
922
923static void
924bpack(int8_t *src, int8_t *dst, int len)
925{
926    int i, j, blank;
927
928    for (i = j = blank = 0 ; i < len; i++) {
929	if (blank && src[i] == ' ') continue;
930	if (blank && src[i] != ' ') {
931	    dst[j++] = src[i];
932	    blank = 0;
933	    continue;
934	}
935	if (src[i] == ' ') {
936	    blank = 1;
937	    if (i == 0)
938		continue;
939	}
940	dst[j++] = src[i];
941    }
942    if (j < len)
943	dst[j] = 0x00;
944}
945
946
947/*
948 * module handeling
949 */
950static int
951ata_module_event_handler(module_t mod, int what, void *arg)
952{
953    static struct cdev *atacdev;
954
955    switch (what) {
956    case MOD_LOAD:
957	/* register controlling device */
958	atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
959
960	if (cold) {
961	    /* register boot attach to be run when interrupts are enabled */
962	    if (!(ata_delayed_attach = (struct intr_config_hook *)
963				       malloc(sizeof(struct intr_config_hook),
964					      M_TEMP, M_NOWAIT | M_ZERO))) {
965		printf("ata: malloc of delayed attach hook failed\n");
966		return EIO;
967	    }
968	    ata_delayed_attach->ich_func = (void*)ata_boot_attach;
969	    if (config_intrhook_establish(ata_delayed_attach) != 0) {
970		printf("ata: config_intrhook_establish failed\n");
971		free(ata_delayed_attach, M_TEMP);
972	    }
973	}
974	return 0;
975
976    case MOD_UNLOAD:
977	/* deregister controlling device */
978	destroy_dev(atacdev);
979	return 0;
980
981    default:
982	return EOPNOTSUPP;
983    }
984}
985
986static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
987DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
988MODULE_VERSION(ata, 1);
989
990static void
991ata_init(void)
992{
993    ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
994				   NULL, NULL, NULL, NULL, 0, 0);
995    ata_composite_zone = uma_zcreate("ata_composite",
996				     sizeof(struct ata_composite),
997				     NULL, NULL, NULL, NULL, 0, 0);
998}
999SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1000
1001static void
1002ata_uninit(void)
1003{
1004    uma_zdestroy(ata_composite_zone);
1005    uma_zdestroy(ata_request_zone);
1006}
1007SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);
1008