The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/ata/ata-disk.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1998 - 2004 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 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   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/bio.h>
   39 #include <sys/bus.h>
   40 #include <sys/conf.h>
   41 #include <sys/disk.h>
   42 #include <sys/cons.h>
   43 #include <sys/sysctl.h>
   44 #include <sys/sema.h>
   45 #include <sys/taskqueue.h>
   46 #include <vm/uma.h>
   47 #include <machine/md_var.h>
   48 #include <machine/bus.h>
   49 #include <sys/rman.h>
   50 #include <geom/geom_disk.h>
   51 #include <dev/ata/ata-all.h>
   52 #include <dev/ata/ata-pci.h>
   53 #include <dev/ata/ata-disk.h>
   54 #include <dev/ata/ata-raid.h>
   55 
   56 /* prototypes */
   57 static void ad_detach(struct ata_device *);
   58 static void ad_config(struct ata_device *);
   59 static void ad_start(struct ata_device *);
   60 static void ad_done(struct ata_request *);
   61 static disk_open_t adopen;
   62 static disk_strategy_t adstrategy;
   63 static dumper_t addump;
   64 void ad_print(struct ad_softc *);
   65 static int ad_version(u_int16_t);
   66 
   67 /* internal vars */
   68 static MALLOC_DEFINE(M_AD, "AD driver", "ATA disk driver");
   69 static u_int32_t adp_lun_map = 0;
   70 
   71 void
   72 ad_attach(struct ata_device *atadev)
   73 {
   74     struct ad_softc *adp;
   75     u_int32_t lbasize;
   76     u_int64_t lbasize48;
   77 
   78     if (!(adp = malloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) {
   79         ata_prtdev(atadev, "FAILURE - could not allocate driver storage\n");
   80         atadev->attach = NULL;
   81         return;
   82     }
   83     atadev->softc = adp;
   84     adp->device = atadev;
   85 
   86 #ifdef ATA_STATIC_ID
   87     adp->lun = (device_get_unit(atadev->channel->dev)<<1)+ATA_DEV(atadev->unit);
   88 #else
   89     adp->lun = ata_get_lun(&adp_lun_map);
   90 #endif
   91     ata_set_name(atadev, "ad", adp->lun);
   92     adp->heads = atadev->param->heads;
   93     adp->sectors = atadev->param->sectors;
   94     adp->total_secs = atadev->param->cylinders * adp->heads * adp->sectors;     
   95     if (adp->device->channel->flags & ATA_USE_PC98GEOM &&
   96         adp->total_secs < 17 * 8 * 65536) {
   97         adp->sectors = 17;
   98         adp->heads = 8;
   99     }
  100     mtx_init(&adp->queue_mtx, "ATA disk bioqueue lock", NULL, MTX_DEF);
  101     bioq_init(&adp->queue);
  102 
  103     lbasize = (u_int32_t)atadev->param->lba_size_1 |
  104                ((u_int32_t)atadev->param->lba_size_2 << 16);
  105 
  106     /* does this device need oldstyle CHS addressing */
  107     if (!ad_version(atadev->param->version_major) || !lbasize)
  108         atadev->flags |= ATA_D_USE_CHS;
  109 
  110     /* use the 28bit LBA size if valid or bigger than the CHS mapping */
  111     if (atadev->param->cylinders == 16383 || adp->total_secs < lbasize)
  112         adp->total_secs = lbasize;
  113 
  114     lbasize48 = ((u_int64_t)atadev->param->lba_size48_1) |
  115                 ((u_int64_t)atadev->param->lba_size48_2 << 16) |
  116                 ((u_int64_t)atadev->param->lba_size48_3 << 32) |
  117                 ((u_int64_t)atadev->param->lba_size48_4 << 48);
  118 
  119     /* use the 48bit LBA size if valid */
  120     if ((atadev->param->support.command2 & ATA_SUPPORT_ADDRESS48) &&
  121         lbasize48 > ATA_MAX_28BIT_LBA)
  122         adp->total_secs = lbasize48;
  123 
  124     /* setup the function ptrs */
  125     atadev->detach = ad_detach;
  126     atadev->config = ad_config;
  127     atadev->start = ad_start;
  128 
  129     /* config device features */
  130     ad_config(atadev);
  131 
  132     /* lets create the disk device */
  133     adp->disk = disk_alloc();
  134     adp->disk->d_open = adopen;
  135     adp->disk->d_strategy = adstrategy;
  136     adp->disk->d_dump = addump;
  137     adp->disk->d_name = "ad";
  138     adp->disk->d_drv1 = adp;
  139     if (atadev->channel->dma)
  140         adp->disk->d_maxsize = atadev->channel->dma->max_iosize;
  141     else
  142         adp->disk->d_maxsize = DFLTPHYS;
  143     adp->disk->d_sectorsize = DEV_BSIZE;
  144     adp->disk->d_mediasize = DEV_BSIZE * (off_t)adp->total_secs;
  145     adp->disk->d_fwsectors = adp->sectors;
  146     adp->disk->d_fwheads = adp->heads;
  147     adp->disk->d_unit = adp->lun;
  148     disk_create(adp->disk, DISK_VERSION);
  149 
  150     /* announce we are here */
  151     ad_print(adp);
  152 
  153 #ifdef DEV_ATARAID
  154     ata_raiddisk_attach(adp);
  155 #endif
  156 }
  157 
  158 static void
  159 ad_detach(struct ata_device *atadev)
  160 {
  161     struct ad_softc *adp = atadev->softc;
  162 
  163 #ifdef DEV_ATARAID
  164     if (adp->flags & AD_F_RAID_SUBDISK)
  165         ata_raiddisk_detach(adp);
  166 #endif
  167     disk_destroy(adp->disk);
  168     ata_prtdev(atadev, "WARNING - removed from configuration\n");
  169     mtx_lock(&adp->queue_mtx);
  170     bioq_flush(&adp->queue, NULL, ENXIO);
  171     mtx_unlock(&adp->queue_mtx);
  172     mtx_destroy(&adp->queue_mtx);
  173     ata_free_name(atadev);
  174     ata_free_lun(&adp_lun_map, adp->lun);
  175     atadev->attach = NULL;
  176     atadev->detach = NULL;
  177     atadev->start = NULL;
  178     atadev->softc = NULL;
  179     atadev->flags = 0;
  180     free(adp, M_AD);
  181 }
  182 
  183 static void
  184 ad_config(struct ata_device *atadev)
  185 {
  186     struct ad_softc *adp = atadev->softc;
  187 
  188     /* enable read caching */
  189     ata_controlcmd(atadev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0);
  190 
  191     /* enable write caching if enabled */
  192     if (ata_wc)
  193         ata_controlcmd(atadev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0);
  194     else
  195         ata_controlcmd(atadev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0);
  196 
  197     /* use multiple sectors/interrupt if device supports it */
  198     adp->max_iosize = DEV_BSIZE;
  199     if (ad_version(atadev->param->version_major)) {
  200         int secsperint = max(1, min(atadev->param->sectors_intr, 16));
  201 
  202         if (!ata_controlcmd(atadev, ATA_SET_MULTI, 0, 0, secsperint))
  203             adp->max_iosize = secsperint * DEV_BSIZE;
  204     }
  205 }
  206 
  207 static int
  208 adopen(struct disk *dp)
  209 {
  210     struct ad_softc *adp = dp->d_drv1;
  211 
  212     if (adp == NULL || adp->device->flags & ATA_D_DETACHING)
  213         return ENXIO;
  214     return 0;
  215 }
  216 
  217 static void 
  218 adstrategy(struct bio *bp)
  219 {
  220     struct ad_softc *adp = bp->bio_disk->d_drv1;
  221 
  222     mtx_lock(&adp->queue_mtx);
  223     bioq_disksort(&adp->queue, bp);
  224     mtx_unlock(&adp->queue_mtx);
  225     ata_start(adp->device->channel);
  226 }
  227 
  228 static void
  229 ad_start(struct ata_device *atadev)
  230 {
  231     struct ad_softc *adp = atadev->softc;
  232     struct bio *bp;
  233     struct ata_request *request;
  234 
  235     /* remove request from drive queue */
  236     mtx_lock(&adp->queue_mtx);
  237     bp = bioq_first(&adp->queue);
  238     if (!bp) {
  239         mtx_unlock(&adp->queue_mtx);
  240         return;
  241     }
  242     bioq_remove(&adp->queue, bp); 
  243     mtx_unlock(&adp->queue_mtx);
  244     if (adp->device->flags & ATA_D_DETACHING) {
  245         biofinish(bp, NULL, ENXIO);
  246         return;
  247     }
  248 
  249     if (!(request = ata_alloc_request())) {
  250         ata_prtdev(atadev, "FAILURE - out of memory in start\n");
  251         biofinish(bp, NULL, ENOMEM);
  252         return;
  253     }
  254 
  255     /* setup request */
  256     request->device = atadev;
  257     request->bio = bp;
  258     request->callback = ad_done;
  259     request->timeout = 5;
  260     request->retries = 2;
  261     request->data = bp->bio_data;
  262     request->bytecount = bp->bio_bcount;
  263 
  264     /* convert LBA contents if this is an old non-LBA device */
  265     if (atadev->flags & ATA_D_USE_CHS) {
  266         int sector = (bp->bio_pblkno % adp->sectors) + 1;
  267         int cylinder = bp->bio_pblkno / (adp->sectors * adp->heads);
  268         int head = (bp->bio_pblkno %
  269                     (adp->sectors * adp->heads)) / adp->sectors;
  270 
  271         request->u.ata.lba =
  272             (sector & 0xff) | (cylinder & 0xffff) << 8 | (head & 0xf) << 24;
  273     }
  274     else
  275         request->u.ata.lba = bp->bio_pblkno;
  276 
  277     request->u.ata.count = request->bytecount / DEV_BSIZE;
  278     request->transfersize = min(bp->bio_bcount, adp->max_iosize);
  279 
  280     switch (bp->bio_cmd) {
  281     case BIO_READ:
  282         request->flags |= ATA_R_READ;
  283         if (atadev->mode >= ATA_DMA) {
  284             request->u.ata.command = ATA_READ_DMA;
  285             request->flags |= ATA_R_DMA;
  286         }
  287         else if (adp->max_iosize > DEV_BSIZE)
  288             request->u.ata.command = ATA_READ_MUL;
  289         else
  290             request->u.ata.command = ATA_READ;
  291 
  292         break;
  293     case BIO_WRITE:
  294         request->flags |= ATA_R_WRITE;
  295         if (atadev->mode >= ATA_DMA) {
  296             request->u.ata.command = ATA_WRITE_DMA;
  297             request->flags |= ATA_R_DMA;
  298         }
  299         else if (adp->max_iosize > DEV_BSIZE)
  300             request->u.ata.command = ATA_WRITE_MUL;
  301         else
  302             request->u.ata.command = ATA_WRITE;
  303         break;
  304     default:
  305         ata_prtdev(atadev, "FAILURE - unknown BIO operation\n");
  306         ata_free_request(request);
  307         biofinish(bp, NULL, EIO);
  308         return;
  309     }
  310     ata_queue_request(request);
  311 }
  312 
  313 static void
  314 ad_done(struct ata_request *request)
  315 {
  316     struct bio *bp = request->bio;
  317 
  318     /* finish up transfer */
  319     if ((bp->bio_error = request->result))
  320         bp->bio_flags |= BIO_ERROR;
  321     bp->bio_resid = bp->bio_bcount - request->donecount;
  322     biodone(bp);
  323     ata_free_request(request);
  324 }
  325 
  326 static int
  327 addump(void *arg, void *virtual, vm_offset_t physical,
  328        off_t offset, size_t length)
  329 {
  330     struct ata_request request;
  331     struct disk *dp = arg;
  332     struct ad_softc *adp = dp->d_drv1;
  333 
  334     if (!adp)
  335         return ENXIO;
  336 
  337     bzero(&request, sizeof(struct ata_request));
  338     request.device = adp->device;
  339 
  340     if (length) {
  341         request.data = virtual;
  342         request.bytecount = length;
  343         request.transfersize = min(length, adp->max_iosize);
  344         request.flags = ATA_R_WRITE;
  345         if (adp->max_iosize > DEV_BSIZE)
  346             request.u.ata.command = ATA_WRITE_MUL;
  347         else
  348             request.u.ata.command = ATA_WRITE;
  349         request.u.ata.lba = offset / DEV_BSIZE;
  350         request.u.ata.count = request.bytecount / DEV_BSIZE;
  351     }
  352     else {
  353         request.u.ata.command = ATA_FLUSHCACHE;
  354         request.flags = ATA_R_CONTROL;
  355     }
  356 
  357     if (request.device->channel->
  358         hw.begin_transaction(&request) == ATA_OP_CONTINUES) {
  359         do {
  360             DELAY(20);
  361         } while (request.device->channel->
  362                  hw.end_transaction(&request) == ATA_OP_CONTINUES);
  363         ata_finish(&request);
  364     }
  365     if (request.status & ATA_S_ERROR)
  366         return EIO;
  367     return 0;
  368 }
  369 
  370 void
  371 ad_print(struct ad_softc *adp) 
  372 {
  373     if (bootverbose) {
  374         ata_prtdev(adp->device, "<%.40s/%.8s> ATA-%d disk at ata%d-%s\n", 
  375                    adp->device->param->model, adp->device->param->revision,
  376                    ad_version(adp->device->param->version_major), 
  377                    device_get_unit(adp->device->channel->dev),
  378                    (adp->device->unit == ATA_MASTER) ? "master" : "slave");
  379 
  380         ata_prtdev(adp->device,
  381                    "%lluMB (%llu sectors), %llu C, %u H, %u S, %u B\n",
  382                    (unsigned long long)(adp->total_secs /
  383                                         ((1024L*1024L)/DEV_BSIZE)),
  384                    (unsigned long long)adp->total_secs,
  385                    (unsigned long long)(adp->total_secs /
  386                                         (adp->heads * adp->sectors)),
  387                    adp->heads, adp->sectors, DEV_BSIZE);
  388 
  389         ata_prtdev(adp->device, "%d secs/int, %d depth queue, %s%s\n", 
  390                    adp->max_iosize / DEV_BSIZE, adp->num_tags + 1,
  391                    (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
  392                    ata_mode2str(adp->device->mode));
  393     }
  394     else {
  395         ata_prtdev(adp->device,
  396                    "%lluMB <%.40s/%.8s> [%lld/%d/%d] at ata%d-%s %s%s\n",
  397                    (unsigned long long)(adp->total_secs /
  398                                         ((1024L * 1024L) / DEV_BSIZE)),
  399                    adp->device->param->model, adp->device->param->revision,
  400                    (unsigned long long)(adp->total_secs /
  401                                         (adp->heads * adp->sectors)),
  402                    adp->heads, adp->sectors,
  403                    device_get_unit(adp->device->channel->dev),
  404                    (adp->device->unit == ATA_MASTER) ? "master" : "slave",
  405                    (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
  406                    ata_mode2str(adp->device->mode));
  407     }
  408 }
  409 
  410 static int
  411 ad_version(u_int16_t version)
  412 {
  413     int bit;
  414 
  415     if (version == 0xffff)
  416         return 0;
  417     for (bit = 15; bit >= 0; bit--)
  418         if (version & (1<<bit))
  419             return bit;
  420     return 0;
  421 }

Cache object: f0b13ddd49c049bf577e2945ba05e1f0


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.