File:  [DragonFly] / src / sys / dev / disk / ata / ata-disk.c
Revision 1.15: download - view: text, annotated - select for diffs
Mon Mar 29 16:22:23 2004 UTC (10 years, 4 months ago) by dillon
Branches: MAIN
CVS tags: HEAD
Bring in a bunch of well tested MPIPE changes.  Preallocate a minimum
number of mpipe elements when it is initialized.  Use an array to cache
free MPIPE buffers nad remove the data structure overloading that was
previously occuring on the buffer itself.  Add a deconstructor.  Separate
the blocking and non-blocking allocation APIs into their own functions.

The new code still needs Giant, but it's getting a lot closer to being
lock free.

    1: /*-
    2:  * Copyright (c) 1998,1999,2000,2001,2002 Søren Schmidt <sos@FreeBSD.org>
    3:  * All rights reserved.
    4:  *
    5:  * Redistribution and use in source and binary forms, with or without
    6:  * modification, are permitted provided that the following conditions
    7:  * are met:
    8:  * 1. Redistributions of source code must retain the above copyright
    9:  *    notice, this list of conditions and the following disclaimer,
   10:  *    without modification, immediately at the beginning of the file.
   11:  * 2. Redistributions in binary form must reproduce the above copyright
   12:  *    notice, this list of conditions and the following disclaimer in the
   13:  *    documentation and/or other materials provided with the distribution.
   14:  * 3. The name of the author may not be used to endorse or promote products
   15:  *    derived from this software without specific prior written permission.
   16:  *
   17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27:  *
   28:  * $FreeBSD: src/sys/dev/ata/ata-disk.c,v 1.60.2.24 2003/01/30 07:19:59 sos Exp $
   29:  * $DragonFly: src/sys/dev/disk/ata/ata-disk.c,v 1.15 2004/03/29 16:22:23 dillon Exp $
   30:  */
   31: 
   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/malloc.h>
   38: #include <sys/buf.h>
   39: #include <sys/bus.h>
   40: #include <sys/conf.h>
   41: #include <sys/disk.h>
   42: #include <sys/devicestat.h>
   43: #include <sys/cons.h>
   44: #include <sys/sysctl.h>
   45: #include <sys/syslog.h>
   46: #include <vm/vm.h>
   47: #include <vm/pmap.h>
   48: #include <machine/md_var.h>
   49: #include <machine/bus.h>
   50: #include <machine/clock.h>
   51: #include <sys/rman.h>
   52: #include "ata-all.h"
   53: #include "ata-disk.h"
   54: #include "ata-raid.h"
   55: #include <sys/proc.h>
   56: #include <sys/buf2.h>
   57: 
   58: /* device structures */
   59: static d_open_t		adopen;
   60: static d_close_t	adclose;
   61: static d_strategy_t	adstrategy;
   62: static d_dump_t		addump;
   63: 
   64: static struct cdevsw ad_cdevsw = {
   65: 	/* name */	"ad",
   66: 	/* maj */	116,
   67: 	/* flags */	D_DISK,
   68: 	/* port */      NULL,
   69: 	/* autoq */	0,
   70: 
   71: 	/* open */	adopen,
   72: 	/* close */	adclose,
   73: 	/* read */	physread,
   74: 	/* write */	physwrite,
   75: 	/* ioctl */	noioctl,
   76: 	/* poll */	nopoll,
   77: 	/* mmap */	nommap,
   78: 	/* strategy */	adstrategy,
   79: 	/* dump */	addump,
   80: 	/* psize */	nopsize
   81: };
   82: 
   83: /* prototypes */
   84: static void ad_invalidatequeue(struct ad_softc *, struct ad_request *);
   85: static int ad_tagsupported(struct ad_softc *);
   86: static void ad_timeout(struct ad_request *);
   87: static void ad_free(struct ad_request *);
   88: static int ad_version(u_int16_t);
   89: 
   90: /* misc defines */
   91: #define AD_MAX_RETRIES	3
   92: 
   93: /* internal vars */
   94: static u_int32_t adp_lun_map = 0;
   95: static int ata_dma = 1;
   96: static int ata_wc = 1;
   97: static int ata_tags = 0; 
   98: TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
   99: TUNABLE_INT("hw.ata.wc", &ata_wc);
  100: TUNABLE_INT("hw.ata.tags", &ata_tags);
  101: static MALLOC_DEFINE(M_AD, "AD driver", "ATA disk driver");
  102: 
  103: /* sysctl vars */
  104: SYSCTL_DECL(_hw_ata);
  105: SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RD, &ata_dma, 0,
  106: 	   "ATA disk DMA mode control");
  107: SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RD, &ata_wc, 0,
  108: 	   "ATA disk write caching");
  109: SYSCTL_INT(_hw_ata, OID_AUTO, tags, CTLFLAG_RD, &ata_tags, 0,
  110: 	   "ATA disk tagged queuing support");
  111: 
  112: void
  113: ad_attach(struct ata_device *atadev, int alreadylocked)
  114: {
  115:     struct ad_softc *adp;
  116:     dev_t dev;
  117: 
  118:     if (!(adp = malloc(sizeof(struct ad_softc), M_AD, M_WAITOK | M_ZERO))) {
  119: 	ata_prtdev(atadev, "failed to allocate driver storage\n");
  120: 	return;
  121:     }
  122: 
  123:     KKASSERT(atadev->channel->req_mpipe.max_count != 0);
  124: 
  125:     adp->device = atadev;
  126: #ifdef ATA_STATIC_ID
  127:     adp->lun = (device_get_unit(atadev->channel->dev)<<1)+ATA_DEV(atadev->unit);
  128: #else
  129:     adp->lun = ata_get_lun(&adp_lun_map);
  130: #endif
  131:     ata_set_name(atadev, "ad", adp->lun);
  132:     adp->heads = atadev->param->heads;
  133:     adp->sectors = atadev->param->sectors;
  134:     adp->total_secs = atadev->param->cylinders * adp->heads * adp->sectors;	
  135:     bufq_init(&adp->queue);
  136: 
  137:     /* does this device need oldstyle CHS addressing */
  138:     if (!ad_version(atadev->param->version_major) || 
  139: 	!(atadev->param->atavalid & ATA_FLAG_54_58) || !atadev->param->lba_size)
  140: 	adp->flags |= AD_F_CHS_USED;
  141: 
  142:     /* use the 28bit LBA size if valid */
  143:     if (atadev->param->cylinders == 16383 &&
  144: 	adp->total_secs < atadev->param->lba_size)
  145: 	adp->total_secs = atadev->param->lba_size;
  146: 
  147:     /* use the 48bit LBA size if valid */
  148:     if (atadev->param->support.address48 &&
  149: 	atadev->param->lba_size48 > 268435455)
  150: 	adp->total_secs = atadev->param->lba_size48;
  151:     
  152:     if (!alreadylocked)
  153: 	ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
  154:     /* use multiple sectors/interrupt if device supports it */
  155:     adp->transfersize = DEV_BSIZE;
  156:     if (ad_version(atadev->param->version_major)) {
  157: 	int secsperint = max(1, min(atadev->param->sectors_intr, 16));
  158: 
  159: 	if (!ata_command(atadev, ATA_C_SET_MULTI, 0, secsperint,
  160: 			 0, ATA_WAIT_INTR) && !ata_wait(atadev, 0))
  161: 	adp->transfersize *= secsperint;
  162:     }
  163: 
  164:     /* enable read caching if not default on device */
  165:     if (ata_command(atadev, ATA_C_SETFEATURES,
  166: 		    0, 0, ATA_C_F_ENAB_RCACHE, ATA_WAIT_INTR))
  167: 	ata_prtdev(atadev, "enabling readahead cache failed\n");
  168: 
  169:     /* enable write caching if allowed and not default on device */
  170:     if (ata_wc || (ata_tags && ad_tagsupported(adp))) {
  171: 	if (ata_command(atadev, ATA_C_SETFEATURES,
  172: 			0, 0, ATA_C_F_ENAB_WCACHE, ATA_WAIT_INTR))
  173: 	    ata_prtdev(atadev, "enabling write cache failed\n");
  174:     }
  175:     else {
  176: 	if (ata_command(atadev, ATA_C_SETFEATURES,
  177: 			0, 0, ATA_C_F_DIS_WCACHE, ATA_WAIT_INTR))
  178: 	    ata_prtdev(atadev, "disabling write cache failed\n");
  179:     }
  180: 
  181:     /* use DMA if allowed and if drive/controller supports it */
  182:     if (ata_dma)
  183: 	ata_dmainit(atadev, ata_pmode(atadev->param), 
  184: 		    ata_wmode(atadev->param), ata_umode(atadev->param));
  185:     else
  186: 	ata_dmainit(atadev, ata_pmode(atadev->param), -1, -1);
  187: 
  188:     /* use tagged queueing if allowed and supported */
  189:     if (ata_tags && ad_tagsupported(adp)) {
  190: 	adp->num_tags = atadev->param->queuelen;
  191: 	adp->flags |= AD_F_TAG_ENABLED;
  192: 	adp->device->channel->flags |= ATA_QUEUED;
  193: 	if (ata_command(atadev, ATA_C_SETFEATURES,
  194: 			0, 0, ATA_C_F_DIS_RELIRQ, ATA_WAIT_INTR))
  195: 	    ata_prtdev(atadev, "disabling release interrupt failed\n");
  196: 	if (ata_command(atadev, ATA_C_SETFEATURES,
  197: 			0, 0, ATA_C_F_DIS_SRVIRQ, ATA_WAIT_INTR))
  198: 	    ata_prtdev(atadev, "disabling service interrupt failed\n");
  199:     }
  200: 
  201:     ATA_UNLOCK_CH(atadev->channel);
  202: 
  203:     devstat_add_entry(&adp->stats, "ad", adp->lun, DEV_BSIZE,
  204: 		      DEVSTAT_NO_ORDERED_TAGS,
  205: 		      DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
  206: 		      DEVSTAT_PRIORITY_DISK);
  207: 
  208:     dev = disk_create(adp->lun, &adp->disk, 0, &ad_cdevsw);
  209:     dev->si_drv1 = adp;
  210:     dev->si_iosize_max = 256 * DEV_BSIZE;
  211:     adp->dev = dev;
  212: 
  213:     /* construct the disklabel */
  214:     bzero(&adp->disk.d_label, sizeof(struct disklabel));
  215:     adp->disk.d_label.d_secsize = DEV_BSIZE;
  216:     adp->disk.d_label.d_nsectors = adp->sectors;
  217:     adp->disk.d_label.d_ntracks = adp->heads;
  218:     adp->disk.d_label.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors);
  219:     adp->disk.d_label.d_secpercyl = adp->sectors * adp->heads;
  220:     adp->disk.d_label.d_secperunit = adp->total_secs;
  221: 
  222:     atadev->driver = adp;
  223:     atadev->flags = 0;
  224: 
  225:     /* if this disk belongs to an ATA RAID dont print the probe */
  226:     if (ata_raiddisk_attach(adp))
  227: 	adp->flags |= AD_F_RAID_SUBDISK;
  228:     else {
  229: 	if (atadev->driver) {
  230: 	    ad_print(adp);
  231: 	    ata_enclosure_print(atadev);
  232: 	}
  233:     }
  234: }
  235: 
  236: void
  237: ad_detach(struct ata_device *atadev, int flush) /* get rid of flush XXX SOS */
  238: {
  239:     struct ad_softc *adp = atadev->driver;
  240:     struct ad_request *request;
  241:     struct buf *bp;
  242: 
  243:     atadev->flags |= ATA_D_DETACHING;
  244:     ata_prtdev(atadev, "removed from configuration\n");
  245:     ad_invalidatequeue(adp, NULL);
  246:     TAILQ_FOREACH(request, &atadev->channel->ata_queue, chain) {
  247: 	if (request->softc != adp)
  248: 	    continue;
  249: 	TAILQ_REMOVE(&atadev->channel->ata_queue, request, chain);
  250: 	request->bp->b_error = ENXIO;
  251: 	request->bp->b_flags |= B_ERROR;
  252: 	biodone(request->bp);
  253: 	ad_free(request);
  254:     }
  255:     ata_dmafree(atadev);
  256:     while ((bp = bufq_first(&adp->queue))) {
  257: 	bufq_remove(&adp->queue, bp); 
  258: 	bp->b_error = ENXIO;
  259: 	bp->b_flags |= B_ERROR;
  260: 	biodone(bp);
  261:     }
  262:     disk_invalidate(&adp->disk);
  263:     devstat_remove_entry(&adp->stats);
  264:     disk_destroy(&adp->disk);
  265:     if (flush) {
  266: 	if (ata_command(atadev, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
  267: 	    ata_prtdev(atadev, "flushing cache on detach failed\n");
  268:     }
  269:     if (adp->flags & AD_F_RAID_SUBDISK)
  270: 	ata_raiddisk_detach(adp);
  271:     ata_free_name(atadev);
  272:     ata_free_lun(&adp_lun_map, adp->lun);
  273:     atadev->driver = NULL;
  274:     atadev->flags = 0;
  275:     free(adp, M_AD);
  276: }
  277: 
  278: static int
  279: adopen(dev_t dev, int flags, int fmt, struct thread *td)
  280: {
  281:     struct ad_softc *adp = dev->si_drv1;
  282: 
  283:     if (adp->flags & AD_F_RAID_SUBDISK)
  284: 	return EBUSY;
  285:     return 0;
  286: }
  287: 
  288: static int
  289: adclose(dev_t dev, int flags, int fmt, struct thread *td)
  290: {
  291:     struct ad_softc *adp = dev->si_drv1;
  292:     int s;
  293: 
  294:     s = splbio();	/* interlock non-atomic channel lock */
  295:     ATA_SLEEPLOCK_CH(adp->device->channel, ATA_CONTROL);
  296:     if (ata_command(adp->device, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
  297: 	ata_prtdev(adp->device, "flushing cache on close failed\n");
  298:     ATA_UNLOCK_CH(adp->device->channel);
  299:     splx(s);
  300:     return 0;
  301: }
  302: 
  303: static void 
  304: adstrategy(struct buf *bp)
  305: {
  306:     struct ad_softc *adp = bp->b_dev->si_drv1;
  307:     int s;
  308: 
  309:     if (adp->device->flags & ATA_D_DETACHING) {
  310: 	bp->b_error = ENXIO;
  311: 	bp->b_flags |= B_ERROR;
  312: 	biodone(bp);
  313: 	return;
  314:     }
  315:     s = splbio();
  316:     bufqdisksort(&adp->queue, bp);
  317:     splx(s);
  318:     ata_start(adp->device->channel);
  319: }
  320: 
  321: int
  322: addump(dev_t dev)
  323: {
  324:     struct ad_softc *adp = dev->si_drv1;
  325:     struct ad_request request;
  326:     u_int count, blkno, secsize;
  327:     vm_paddr_t addr = 0;
  328:     long blkcnt;
  329:     int dumppages = MAXDUMPPGS;
  330:     int error;
  331:     int i;
  332: 
  333:     if ((error = disk_dumpcheck(dev, &count, &blkno, &secsize)))
  334: 	return error;
  335: 	
  336:     if (!adp)
  337: 	return ENXIO;
  338: 
  339:     /* force PIO mode for dumps */
  340:     adp->device->mode = ATA_PIO;
  341:     ata_reinit(adp->device->channel);
  342: 
  343:     blkcnt = howmany(PAGE_SIZE, secsize);
  344: 
  345:     while (count > 0) {
  346: 	caddr_t va = NULL;
  347: 	DELAY(1000);
  348: 
  349: 	if ((count / blkcnt) < dumppages)
  350: 	    dumppages = count / blkcnt;
  351: 
  352: 	for (i = 0; i < dumppages; ++i) {
  353: 	    vm_paddr_t a = addr + (i * PAGE_SIZE);
  354: 	    if (is_physical_memory(a))
  355: 		va = pmap_kenter_temporary(trunc_page(a), i);
  356: 	    else
  357: 		va = pmap_kenter_temporary(trunc_page(0), i);
  358: 	}
  359: 
  360: 	bzero(&request, sizeof(struct ad_request));
  361: 	request.softc = adp;
  362: 	request.blockaddr = blkno;
  363: 	request.bytecount = PAGE_SIZE * dumppages;
  364: 	request.data = va;
  365: 
  366: 	while (request.bytecount > 0) {
  367: 	    ad_transfer(&request);
  368: 	    if (request.flags & ADR_F_ERROR)
  369: 		return EIO;
  370: 	    request.donecount += request.currentsize;
  371: 	    request.bytecount -= request.currentsize;
  372: 	    DELAY(20);
  373: 	}
  374: 
  375: 	if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
  376: 	    return EINTR;
  377: 
  378: 	blkno += blkcnt * dumppages;
  379: 	count -= blkcnt * dumppages;
  380: 	addr += PAGE_SIZE * dumppages;
  381:     }
  382: 
  383:     if (ata_wait(adp->device, ATA_S_READY | ATA_S_DSC) < 0)
  384: 	ata_prtdev(adp->device, "timeout waiting for final ready\n");
  385:     return 0;
  386: }
  387: 
  388: void
  389: ad_start(struct ata_device *atadev)
  390: {
  391:     struct ad_softc *adp = atadev->driver;
  392:     struct buf *bp = bufq_first(&adp->queue);
  393:     struct ad_request *request;
  394:     int tag = 0;
  395: 
  396:     if (!bp)
  397: 	return;
  398: 
  399:     /* if tagged queueing enabled get next free tag */
  400:     if (adp->flags & AD_F_TAG_ENABLED) {
  401: 	while (tag <= adp->num_tags && adp->tags[tag])
  402: 	    tag++;
  403: 	if (tag > adp->num_tags )
  404: 	    return;
  405:     }
  406: 
  407:     /*
  408:      * Allocate a request.  The allocation can only fail if the pipeline
  409:      * is full, in which case the request will be picked up later when
  410:      * ad_start() is called after another request completes.
  411:      */
  412:     request = mpipe_alloc_nowait(&atadev->channel->req_mpipe);
  413:     if (request == NULL) {
  414: 	ata_prtdev(atadev, "pipeline full allocating request in ad_start\n");
  415: 	return;
  416:     }
  417: 
  418:     /* setup request */
  419:     request->softc = adp;
  420:     request->bp = bp;
  421:     request->blockaddr = bp->b_pblkno;
  422:     request->bytecount = bp->b_bcount;
  423:     request->data = bp->b_data;
  424:     request->tag = tag;
  425:     if (bp->b_flags & B_READ) 
  426: 	request->flags |= ADR_F_READ;
  427:     if (adp->device->mode >= ATA_DMA) {
  428: 	if (ata_dmaalloc(atadev, M_NOWAIT) != 0) {
  429: 	    mpipe_free(&atadev->channel->req_mpipe, request);
  430: 	    ata_prtdev(atadev, "pipeline full allocated dmabuf in ad_start\n");
  431: 	    /* do not revert to PIO, wait for ad_start after I/O completion */
  432: 	    return;
  433: 	}
  434:     }
  435: 
  436:     /* insert in tag array */
  437:     adp->tags[tag] = request;
  438: 
  439:     /* remove from drive queue */
  440:     bufq_remove(&adp->queue, bp); 
  441: 
  442:     /* link onto controller queue */
  443:     TAILQ_INSERT_TAIL(&atadev->channel->ata_queue, request, chain);
  444: }
  445: 
  446: int
  447: ad_transfer(struct ad_request *request)
  448: {
  449:     struct ad_softc *adp;
  450:     u_int64_t lba;
  451:     u_int32_t count, max_count;
  452:     u_int8_t cmd;
  453:     int flags = ATA_IMMEDIATE;
  454: 
  455:     /* get request params */
  456:     adp = request->softc;
  457: 
  458:     /* calculate transfer details */
  459:     lba = request->blockaddr + (request->donecount / DEV_BSIZE);
  460:    
  461:     if (request->donecount == 0) {
  462: 
  463: 	/* start timeout for this transfer */
  464: 	if (dumping)
  465: 	    request->timeout_handle.callout = NULL;
  466: 	else
  467: 	    request->timeout_handle = 
  468: 		timeout((timeout_t*)ad_timeout, request, 10 * hz);
  469: 
  470: 	/* setup transfer parameters */
  471: 	count = howmany(request->bytecount, DEV_BSIZE);
  472: 	max_count = adp->device->param->support.address48 ? 65536 : 256;
  473: 	if (count > max_count) {
  474: 	    ata_prtdev(adp->device,
  475: 		       "count %d size transfers not supported\n", count);
  476: 	    count = max_count;
  477: 	}
  478: 
  479: 	if (adp->flags & AD_F_CHS_USED) {
  480: 	    int sector = (lba % adp->sectors) + 1;
  481: 	    int cylinder = lba / (adp->sectors * adp->heads);
  482: 	    int head = (lba % (adp->sectors * adp->heads)) / adp->sectors;
  483: 
  484: 	    lba = (sector&0xff) | ((cylinder&0xffff)<<8) | ((head&0xf)<<24);
  485: 	    adp->device->flags |= ATA_D_USE_CHS;
  486: 	}
  487: 
  488: 	/* setup first transfer length */
  489: 	request->currentsize = min(request->bytecount, adp->transfersize);
  490: 
  491: 	devstat_start_transaction(&adp->stats);
  492: 
  493: 	/* does this drive & transfer work with DMA ? */
  494: 	request->flags &= ~ADR_F_DMA_USED;
  495: 	if (adp->device->mode >= ATA_DMA &&
  496: 	    !ata_dmasetup(adp->device, request->data, request->bytecount)) {
  497: 	    request->flags |= ADR_F_DMA_USED;
  498: 	    request->currentsize = request->bytecount;
  499: 
  500: 	    /* do we have tags enabled ? */
  501: 	    if (adp->flags & AD_F_TAG_ENABLED) {
  502: 		cmd = (request->flags & ADR_F_READ) ?
  503: 		    ATA_C_READ_DMA_QUEUED : ATA_C_WRITE_DMA_QUEUED;
  504: 
  505: 		if (ata_command(adp->device, cmd, lba,
  506: 				request->tag << 3, count, flags)) {
  507: 		    ata_prtdev(adp->device, "error executing command");
  508: 		    goto transfer_failed;
  509: 		}
  510: 		if (ata_wait(adp->device, ATA_S_READY)) {
  511: 		    ata_prtdev(adp->device, "timeout waiting for READY\n");
  512: 		    goto transfer_failed;
  513: 		}
  514: 		adp->outstanding++;
  515: 
  516: 		/* if ATA bus RELEASE check for SERVICE */
  517: 		if (adp->flags & AD_F_TAG_ENABLED &&
  518: 		    ATA_INB(adp->device->channel->r_io, ATA_IREASON) &
  519: 		    ATA_I_RELEASE)
  520: 		    return ad_service(adp, 1);
  521: 	    }
  522: 	    else {
  523: 		cmd = (request->flags & ADR_F_READ) ?
  524: 		    ATA_C_READ_DMA : ATA_C_WRITE_DMA;
  525: 
  526: 		if (ata_command(adp->device, cmd, lba, count, 0, flags)) {
  527: 		    ata_prtdev(adp->device, "error executing command");
  528: 		    goto transfer_failed;
  529: 		}
  530: #if 0
  531: 		/*
  532: 		 * wait for data transfer phase
  533: 		 *
  534: 		 * well this should be here acording to specs, but older
  535: 		 * promise controllers doesn't like it, they lockup!
  536: 		 */
  537: 		if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
  538: 		    ata_prtdev(adp->device, "timeout waiting for data phase\n");
  539: 		    goto transfer_failed;
  540: 		}
  541: #endif
  542: 	    }
  543: 
  544: 	    /* start transfer, return and wait for interrupt */
  545: 	    ata_dmastart(adp->device, request->data, request->bytecount,
  546: 			request->flags & ADR_F_READ);
  547: 	    return ATA_OP_CONTINUES;
  548: 	}
  549: 
  550: 	/* does this drive support multi sector transfers ? */
  551: 	if (request->currentsize > DEV_BSIZE)
  552: 	    cmd = request->flags&ADR_F_READ ? ATA_C_READ_MUL : ATA_C_WRITE_MUL;
  553: 
  554: 	/* just plain old single sector transfer */
  555: 	else
  556: 	    cmd = request->flags&ADR_F_READ ? ATA_C_READ : ATA_C_WRITE;
  557: 
  558: 	if (ata_command(adp->device, cmd, lba, count, 0, flags)){
  559: 	    ata_prtdev(adp->device, "error executing command");
  560: 	    goto transfer_failed;
  561: 	}
  562:     }
  563:    
  564:     /* calculate this transfer length */
  565:     request->currentsize = min(request->bytecount, adp->transfersize);
  566: 
  567:     /* if this is a PIO read operation, return and wait for interrupt */
  568:     if (request->flags & ADR_F_READ)
  569: 	return ATA_OP_CONTINUES;
  570: 
  571:     /* ready to write PIO data ? */
  572:     if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) < 0) {
  573: 	ata_prtdev(adp->device, "timeout waiting for DRQ");
  574: 	goto transfer_failed;
  575:     }
  576: 
  577:     /* output the data */
  578:     if (adp->device->channel->flags & ATA_USE_16BIT)
  579: 	ATA_OUTSW(adp->device->channel->r_io, ATA_DATA,
  580: 		  (void *)((uintptr_t)request->data + request->donecount),
  581: 		  request->currentsize / sizeof(int16_t));
  582:     else
  583: 	ATA_OUTSL(adp->device->channel->r_io, ATA_DATA,
  584: 		  (void *)((uintptr_t)request->data + request->donecount),
  585: 		  request->currentsize / sizeof(int32_t));
  586:     return ATA_OP_CONTINUES;
  587: 
  588: transfer_failed:
  589:     untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
  590:     ad_invalidatequeue(adp, request);
  591:     printf(" - resetting\n");
  592: 
  593:     /* if retries still permit, reinject this request */
  594:     if (request->retries++ < AD_MAX_RETRIES)
  595: 	TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
  596:     else {
  597: 	/* retries all used up, return error */
  598: 	request->bp->b_error = EIO;
  599: 	request->bp->b_flags |= B_ERROR;
  600: 	request->bp->b_resid = request->bytecount;
  601: 	devstat_end_transaction_buf(&adp->stats, request->bp);
  602: 	biodone(request->bp);
  603: 	ad_free(request);
  604:     }
  605:     ata_reinit(adp->device->channel);
  606:     return ATA_OP_CONTINUES;
  607: }
  608: 
  609: int
  610: ad_interrupt(struct ad_request *request)
  611: {
  612:     struct ad_softc *adp = request->softc;
  613:     int dma_stat = 0;
  614: 
  615:     /* finish DMA transfer */
  616:     if (request->flags & ADR_F_DMA_USED)
  617: 	dma_stat = ata_dmadone(adp->device);
  618: 
  619:     /* do we have a corrected soft error ? */
  620:     if (adp->device->channel->status & ATA_S_CORR)
  621: 	diskerr(request->bp, "soft error (ECC corrected)", LOG_PRINTF,
  622: 		request->blockaddr + (request->donecount / DEV_BSIZE),
  623: 		&adp->disk.d_label);
  624: 
  625:     /* did any real errors happen ? */
  626:     if ((adp->device->channel->status & ATA_S_ERROR) ||
  627: 	(request->flags & ADR_F_DMA_USED && dma_stat & ATA_BMSTAT_ERROR)) {
  628: 	adp->device->channel->error =
  629: 	    ATA_INB(adp->device->channel->r_io, ATA_ERROR);
  630: 	diskerr(request->bp, (adp->device->channel->error & ATA_E_ICRC) ?
  631: 		"UDMA ICRC error" : "hard error", LOG_PRINTF,
  632: 		request->blockaddr + (request->donecount / DEV_BSIZE),
  633: 		&adp->disk.d_label);
  634: 
  635: 	/* if this is a UDMA CRC error, reinject request */
  636: 	if (request->flags & ADR_F_DMA_USED &&
  637: 	    adp->device->channel->error & ATA_E_ICRC) {
  638: 	    untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
  639: 	    ad_invalidatequeue(adp, request);
  640: 
  641: 	    if (request->retries++ < AD_MAX_RETRIES)
  642: 		printf(" retrying\n");
  643: 	    else {
  644: 		ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
  645: 		printf(" falling back to PIO mode\n");
  646: 	    }
  647: 	    TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
  648: 	    return ATA_OP_FINISHED;
  649: 	}
  650: 
  651: 	/* if using DMA, try once again in PIO mode */
  652: 	if (request->flags & ADR_F_DMA_USED) {
  653: 	    untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
  654: 	    ad_invalidatequeue(adp, request);
  655: 	    ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
  656: 	    request->flags |= ADR_F_FORCE_PIO;
  657: 	    printf(" trying PIO mode\n");
  658: 	    TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
  659: 	    return ATA_OP_FINISHED;
  660: 	}
  661: 
  662: 	request->flags |= ADR_F_ERROR;
  663: 	printf(" status=%02x error=%02x\n", 
  664: 	       adp->device->channel->status, adp->device->channel->error);
  665:     }
  666: 
  667:     /* if we arrived here with forced PIO mode, DMA doesn't work right */
  668:     if (request->flags & ADR_F_FORCE_PIO && !(request->flags & ADR_F_ERROR))
  669: 	ata_prtdev(adp->device, "DMA problem fallback to PIO mode\n");
  670: 
  671:     /* if this was a PIO read operation, get the data */
  672:     if (!(request->flags & ADR_F_DMA_USED) &&
  673: 	(request->flags & (ADR_F_READ | ADR_F_ERROR)) == ADR_F_READ) {
  674: 
  675: 	/* ready to receive data? */
  676: 	if ((adp->device->channel->status & ATA_S_READY) == 0)
  677: 	    ata_prtdev(adp->device, "read interrupt arrived early");
  678: 
  679: 	if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) != 0) {
  680: 	    ata_prtdev(adp->device, "read error detected (too) late");
  681: 	    request->flags |= ADR_F_ERROR;
  682: 	}
  683: 	else {
  684: 	    /* data ready, read in */
  685: 	    if (adp->device->channel->flags & ATA_USE_16BIT)
  686: 		ATA_INSW(adp->device->channel->r_io, ATA_DATA,
  687: 			 (void*)((uintptr_t)request->data + request->donecount),
  688: 			 request->currentsize / sizeof(int16_t));
  689: 	    else
  690: 		ATA_INSL(adp->device->channel->r_io, ATA_DATA,
  691: 			 (void*)((uintptr_t)request->data + request->donecount),
  692: 			 request->currentsize / sizeof(int32_t));
  693: 	}
  694:     }
  695: 
  696:     /* finish up transfer */
  697:     if (request->flags & ADR_F_ERROR) {
  698: 	request->bp->b_error = EIO;
  699: 	request->bp->b_flags |= B_ERROR;
  700:     } 
  701:     else {
  702: 	request->bytecount -= request->currentsize;
  703: 	request->donecount += request->currentsize;
  704: 	if (request->bytecount > 0) {
  705: 	    ad_transfer(request);
  706: 	    return ATA_OP_CONTINUES;
  707: 	}
  708:     }
  709: 
  710:     /* disarm timeout for this transfer */
  711:     untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
  712: 
  713:     request->bp->b_resid = request->bytecount;
  714: 
  715:     devstat_end_transaction_buf(&adp->stats, request->bp);
  716:     biodone(request->bp);
  717:     ad_free(request);
  718:     adp->outstanding--;
  719: 
  720:     /* check for SERVICE (tagged operations only) */
  721:     return ad_service(adp, 1);
  722: }
  723: 
  724: int
  725: ad_service(struct ad_softc *adp, int change)
  726: {
  727:     /* do we have to check the other device on this channel ? */
  728:     if (adp->device->channel->flags & ATA_QUEUED && change) {
  729: 	int device = adp->device->unit;
  730: 
  731: 	if (adp->device->unit == ATA_MASTER) {
  732: 	    if ((adp->device->channel->devices & ATA_ATA_SLAVE) &&
  733: 		(adp->device->channel->device[SLAVE].driver) &&
  734: 		((struct ad_softc *) (adp->device->channel->
  735: 		 device[SLAVE].driver))->flags & AD_F_TAG_ENABLED)
  736: 		device = ATA_SLAVE;
  737: 	}
  738: 	else {
  739: 	    if ((adp->device->channel->devices & ATA_ATA_MASTER) &&
  740: 		(adp->device->channel->device[MASTER].driver) &&
  741: 		((struct ad_softc *) (adp->device->channel->
  742: 		 device[MASTER].driver))->flags & AD_F_TAG_ENABLED)
  743: 		device = ATA_MASTER;
  744: 	}
  745: 	if (device != adp->device->unit &&
  746: 	    ((struct ad_softc *)
  747: 	     (adp->device->channel->
  748: 	      device[ATA_DEV(device)].driver))->outstanding > 0) {
  749: 	    ATA_OUTB(adp->device->channel->r_io, ATA_DRIVE, ATA_D_IBM | device);
  750: 	    adp = adp->device->channel->device[ATA_DEV(device)].driver;
  751: 	    DELAY(1);
  752: 	}
  753:     }
  754:     adp->device->channel->status =
  755: 	ATA_INB(adp->device->channel->r_altio, ATA_ALTSTAT);
  756:  
  757:     /* do we have a SERVICE request from the drive ? */
  758:     if (adp->flags & AD_F_TAG_ENABLED &&
  759: 	adp->outstanding > 0 &&
  760: 	adp->device->channel->status & ATA_S_SERVICE) {
  761: 	struct ad_request *request;
  762: 	int tag;
  763: 
  764: 	/* check for error */
  765: 	if (adp->device->channel->status & ATA_S_ERROR) {
  766: 	    ata_prtdev(adp->device, "Oops! controller says s=0x%02x e=0x%02x\n",
  767: 		       adp->device->channel->status,
  768: 		       adp->device->channel->error);
  769: 	    ad_invalidatequeue(adp, NULL);
  770: 	    return ATA_OP_FINISHED;
  771: 	}
  772: 
  773: 	/* issue SERVICE cmd */
  774: 	if (ata_command(adp->device, ATA_C_SERVICE, 0, 0, 0, ATA_IMMEDIATE)) {
  775: 	    ata_prtdev(adp->device, "problem executing SERVICE cmd\n");
  776: 	    ad_invalidatequeue(adp, NULL);
  777: 	    return ATA_OP_FINISHED;
  778: 	}
  779: 
  780: 	/* setup the transfer environment when ready */
  781: 	if (ata_wait(adp->device, ATA_S_READY)) {
  782: 	    ata_prtdev(adp->device, "SERVICE timeout tag=%d s=%02x e=%02x\n",
  783: 		       ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3,
  784: 		       adp->device->channel->status,
  785: 		       adp->device->channel->error);
  786: 	    ad_invalidatequeue(adp, NULL);
  787: 	    return ATA_OP_FINISHED;
  788: 	}
  789: 	tag = ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3;
  790: 	if (!(request = adp->tags[tag])) {
  791: 	    ata_prtdev(adp->device, "no request for tag=%d\n", tag);	
  792: 	    ad_invalidatequeue(adp, NULL);
  793: 	    return ATA_OP_FINISHED;
  794: 	}
  795: 	ATA_FORCELOCK_CH(adp->device->channel, ATA_ACTIVE_ATA);
  796: 	adp->device->channel->running = request;
  797: 	request->serv++;
  798: 
  799: 	/* start DMA transfer when ready */
  800: 	if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
  801: 	    ata_prtdev(adp->device, "timeout starting DMA s=%02x e=%02x\n",
  802: 		       adp->device->channel->status,
  803: 		       adp->device->channel->error);
  804: 	    ad_invalidatequeue(adp, NULL);
  805: 	    return ATA_OP_FINISHED;
  806: 	}
  807: 	ata_dmastart(adp->device, request->data, request->bytecount,
  808: 		    request->flags & ADR_F_READ);
  809: 	return ATA_OP_CONTINUES;
  810:     }
  811:     return ATA_OP_FINISHED;
  812: }
  813: 
  814: static void
  815: ad_free(struct ad_request *request)
  816: {
  817:     int s = splbio();
  818:     ata_dmafree(request->softc->device);
  819:     request->softc->tags[request->tag] = NULL;
  820:     mpipe_free(&request->softc->device->channel->req_mpipe, request);
  821:     splx(s);
  822: }
  823: 
  824: static void
  825: ad_invalidatequeue(struct ad_softc *adp, struct ad_request *request)
  826: {
  827:     /* if tags used invalidate all other tagged transfers */
  828:     if (adp->flags & AD_F_TAG_ENABLED) {
  829: 	struct ad_request *tmpreq;
  830: 	int tag;
  831: 
  832: 	ata_prtdev(adp->device, "invalidating queued requests\n");
  833: 	for (tag = 0; tag <= adp->num_tags; tag++) {
  834: 	    tmpreq = adp->tags[tag];
  835: 	    adp->tags[tag] = NULL;
  836: 	    if (tmpreq == request || tmpreq == NULL)
  837: 		continue;
  838: 	    untimeout((timeout_t *)ad_timeout, tmpreq, tmpreq->timeout_handle);
  839: 	    TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, tmpreq, chain);
  840: 	}
  841: 	if (ata_command(adp->device, ATA_C_NOP,
  842: 			0, 0, ATA_C_F_FLUSHQUEUE, ATA_WAIT_READY))
  843: 	    ata_prtdev(adp->device, "flush queue failed\n");
  844: 	adp->outstanding = 0;
  845:     }
  846: }
  847: 
  848: static int
  849: ad_tagsupported(struct ad_softc *adp)
  850: {
  851:     const char *good[] = {"IBM-DPTA", "IBM-DTLA", NULL};
  852:     int i = 0;
  853: 
  854:     switch (adp->device->channel->chiptype) {
  855:     case 0x4d33105a: /* Promises before TX2 doesn't work with tagged queuing */
  856:     case 0x4d38105a:
  857:     case 0x0d30105a:
  858:     case 0x4d30105a:  
  859: 	return 0;
  860:     }
  861: 
  862:     /* check that drive does DMA, has tags enabled, and is one we know works */
  863:     if (adp->device->mode >= ATA_DMA && adp->device->param->support.queued && 
  864: 	adp->device->param->enabled.queued) {
  865: 	while (good[i] != NULL) {
  866: 	    if (!strncmp(adp->device->param->model, good[i], strlen(good[i])))
  867: 		return 1;
  868: 	    i++;
  869: 	}
  870: 	/* 
  871: 	 * check IBM's new obscure way of naming drives 
  872: 	 * we want "IC" (IBM CORP) and "AT" or "AV" (ATA interface)
  873: 	 * but doesn't care about the other info (size, capacity etc)
  874: 	 */
  875: 	if (!strncmp(adp->device->param->model, "IC", 2) &&
  876: 	    (!strncmp(adp->device->param->model + 8, "AT", 2) ||
  877: 	     !strncmp(adp->device->param->model + 8, "AV", 2)))
  878: 		return 1;
  879:     }
  880:     return 0;
  881: }
  882: 
  883: static void
  884: ad_timeout(struct ad_request *request)
  885: {
  886:     struct ad_softc *adp = request->softc;
  887: 
  888:     adp->device->channel->running = NULL;
  889:     ata_prtdev(adp->device, "%s command timeout tag=%d serv=%d - resetting\n",
  890: 	       (request->flags & ADR_F_READ) ? "READ" : "WRITE",
  891: 	       request->tag, request->serv);
  892: 
  893:     if (request->flags & ADR_F_DMA_USED) {
  894: 	ata_dmadone(adp->device);
  895: 	ad_invalidatequeue(adp, request);
  896: 	if (request->retries == AD_MAX_RETRIES) {
  897: 	    ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
  898: 	    ata_prtdev(adp->device, "trying fallback to PIO mode\n");
  899: 	    request->retries = 0;
  900: 	}
  901:     }
  902: 
  903:     /* if retries still permit, reinject this request */
  904:     if (request->retries++ < AD_MAX_RETRIES) {
  905: 	TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
  906:     }
  907:     else {
  908: 	/* retries all used up, return error */
  909: 	request->bp->b_error = EIO;
  910: 	request->bp->b_flags |= B_ERROR;
  911: 	devstat_end_transaction_buf(&adp->stats, request->bp);
  912: 	biodone(request->bp);
  913: 	ad_free(request);
  914:     }
  915:     ata_reinit(adp->device->channel);
  916: }
  917: 
  918: void
  919: ad_reinit(struct ata_device *atadev)
  920: {
  921:     struct ad_softc *adp = atadev->driver;
  922: 
  923:     /* reinit disk parameters */
  924:     ad_invalidatequeue(atadev->driver, NULL);
  925:     ata_command(atadev, ATA_C_SET_MULTI, 0,
  926: 		adp->transfersize / DEV_BSIZE, 0, ATA_WAIT_READY);
  927:     if (adp->device->mode >= ATA_DMA)
  928: 	ata_dmainit(atadev, ata_pmode(adp->device->param),
  929: 		    ata_wmode(adp->device->param),
  930: 		    ata_umode(adp->device->param));
  931:     else
  932: 	ata_dmainit(atadev, ata_pmode(adp->device->param), -1, -1);
  933: }
  934: 
  935: void
  936: ad_print(struct ad_softc *adp) 
  937: {
  938:     if (bootverbose) {
  939: 	ata_prtdev(adp->device, "<%.40s/%.8s> ATA-%d disk at ata%d-%s\n", 
  940: 		   adp->device->param->model, adp->device->param->revision,
  941: 		   ad_version(adp->device->param->version_major), 
  942: 		   device_get_unit(adp->device->channel->dev),
  943: 		   (adp->device->unit == ATA_MASTER) ? "master" : "slave");
  944: 
  945: 	ata_prtdev(adp->device,
  946: 		   "%lluMB (%llu sectors), %llu C, %u H, %u S, %u B\n",
  947: 		   (unsigned long long)(adp->total_secs /
  948: 		   ((1024L*1024L)/DEV_BSIZE)),
  949: 		   (unsigned long long) adp->total_secs,
  950: 		   (unsigned long long) (adp->total_secs /
  951: 		    (adp->heads * adp->sectors)),
  952: 		   adp->heads, adp->sectors, DEV_BSIZE);
  953: 
  954: 	ata_prtdev(adp->device, "%d secs/int, %d depth queue, %s%s\n", 
  955: 		   adp->transfersize / DEV_BSIZE, adp->num_tags + 1,
  956: 		   (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
  957: 		   ata_mode2str(adp->device->mode));
  958: 
  959: 	ata_prtdev(adp->device, "piomode=%d dmamode=%d udmamode=%d cblid=%d\n",
  960: 		   ata_pmode(adp->device->param), ata_wmode(adp->device->param),
  961: 		   ata_umode(adp->device->param), 
  962: 		   adp->device->param->hwres_cblid);
  963: 
  964:     }
  965:     else
  966: 	ata_prtdev(adp->device,"%lluMB <%.40s> [%lld/%d/%d] at ata%d-%s %s%s\n",
  967: 		   (unsigned long long)(adp->total_secs /
  968: 		   ((1024L * 1024L) / DEV_BSIZE)),
  969: 		   adp->device->param->model,
  970: 		   (unsigned long long)(adp->total_secs /
  971: 		    (adp->heads*adp->sectors)),
  972: 		   adp->heads, adp->sectors,
  973: 		   device_get_unit(adp->device->channel->dev),
  974: 		   (adp->device->unit == ATA_MASTER) ? "master" : "slave",
  975: 		   (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
  976: 		   ata_mode2str(adp->device->mode));
  977: }
  978: 
  979: static int
  980: ad_version(u_int16_t version)
  981: {
  982:     int bit;
  983: 
  984:     if (version == 0xffff)
  985: 	return 0;
  986:     for (bit = 15; bit >= 0; bit--)
  987: 	if (version & (1<<bit))
  988: 	    return bit;
  989:     return 0;
  990: }