1c1b3d7c5SThomas E. Spanjaard /*- 2c1b3d7c5SThomas E. Spanjaard * Copyright (c) 1998 - 2006 S�ren Schmidt <sos@FreeBSD.org> 3c1b3d7c5SThomas E. Spanjaard * All rights reserved. 4c1b3d7c5SThomas E. Spanjaard * 5c1b3d7c5SThomas E. Spanjaard * Redistribution and use in source and binary forms, with or without 6c1b3d7c5SThomas E. Spanjaard * modification, are permitted provided that the following conditions 7c1b3d7c5SThomas E. Spanjaard * are met: 8c1b3d7c5SThomas E. Spanjaard * 1. Redistributions of source code must retain the above copyright 9c1b3d7c5SThomas E. Spanjaard * notice, this list of conditions and the following disclaimer, 10c1b3d7c5SThomas E. Spanjaard * without modification, immediately at the beginning of the file. 11c1b3d7c5SThomas E. Spanjaard * 2. Redistributions in binary form must reproduce the above copyright 12c1b3d7c5SThomas E. Spanjaard * notice, this list of conditions and the following disclaimer in the 13c1b3d7c5SThomas E. Spanjaard * documentation and/or other materials provided with the distribution. 14c1b3d7c5SThomas E. Spanjaard * 15c1b3d7c5SThomas E. Spanjaard * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16c1b3d7c5SThomas E. Spanjaard * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17c1b3d7c5SThomas E. Spanjaard * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18c1b3d7c5SThomas E. Spanjaard * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19c1b3d7c5SThomas E. Spanjaard * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20c1b3d7c5SThomas E. Spanjaard * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21c1b3d7c5SThomas E. Spanjaard * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22c1b3d7c5SThomas E. Spanjaard * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23c1b3d7c5SThomas E. Spanjaard * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24c1b3d7c5SThomas E. Spanjaard * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25c1b3d7c5SThomas E. Spanjaard * 2602d7aa4aSSascha Wildner * $FreeBSD: src/sys/dev/ata/ata-disk.c,v 1.199 2006/09/14 19:12:29 sos Exp $ 27a9bf1b8cSMatthew Dillon * $DragonFly: src/sys/dev/disk/nata/ata-disk.c,v 1.9 2008/08/29 20:08:38 dillon Exp $ 28c1b3d7c5SThomas E. Spanjaard */ 29c1b3d7c5SThomas E. Spanjaard 30c1b3d7c5SThomas E. Spanjaard #include "opt_ata.h" 31c1b3d7c5SThomas E. Spanjaard 32c1b3d7c5SThomas E. Spanjaard #include <sys/param.h> 33c1b3d7c5SThomas E. Spanjaard #include <sys/bio.h> 34c1b3d7c5SThomas E. Spanjaard #include <sys/buf.h> 35c1b3d7c5SThomas E. Spanjaard #include <sys/bus.h> 36c1b3d7c5SThomas E. Spanjaard #include <sys/device.h> 37c1b3d7c5SThomas E. Spanjaard #include <sys/devicestat.h> 38c1b3d7c5SThomas E. Spanjaard #include <sys/disk.h> 39c1b3d7c5SThomas E. Spanjaard #include <sys/libkern.h> 40c1b3d7c5SThomas E. Spanjaard #include <sys/malloc.h> 41c1b3d7c5SThomas E. Spanjaard #include <sys/module.h> 42c1b3d7c5SThomas E. Spanjaard #include <sys/nata.h> 43c1b3d7c5SThomas E. Spanjaard #include <sys/systm.h> 44c1b3d7c5SThomas E. Spanjaard 45c1b3d7c5SThomas E. Spanjaard #include <vm/pmap.h> 46c1b3d7c5SThomas E. Spanjaard 47c1b3d7c5SThomas E. Spanjaard #include <machine/md_var.h> 48c1b3d7c5SThomas E. Spanjaard 49c1b3d7c5SThomas E. Spanjaard #include "ata-all.h" 50c1b3d7c5SThomas E. Spanjaard #include "ata-disk.h" 51c1b3d7c5SThomas E. Spanjaard #include "ata_if.h" 52c1b3d7c5SThomas E. Spanjaard 53c1b3d7c5SThomas E. Spanjaard /* device structure */ 54c1b3d7c5SThomas E. Spanjaard static d_open_t ad_open; 55c1b3d7c5SThomas E. Spanjaard static d_close_t ad_close; 56c1b3d7c5SThomas E. Spanjaard static d_ioctl_t ad_ioctl; 57c1b3d7c5SThomas E. Spanjaard static d_strategy_t ad_strategy; 58c1b3d7c5SThomas E. Spanjaard static d_dump_t ad_dump; 59c1b3d7c5SThomas E. Spanjaard static struct dev_ops ad_ops = { 60c1b3d7c5SThomas E. Spanjaard { "ad", 116, D_DISK }, 61c1b3d7c5SThomas E. Spanjaard .d_open = ad_open, 62c1b3d7c5SThomas E. Spanjaard .d_close = ad_close, 63c1b3d7c5SThomas E. Spanjaard .d_read = physread, 64c1b3d7c5SThomas E. Spanjaard .d_write = physwrite, 65c1b3d7c5SThomas E. Spanjaard .d_ioctl = ad_ioctl, 66c1b3d7c5SThomas E. Spanjaard .d_strategy = ad_strategy, 67c1b3d7c5SThomas E. Spanjaard .d_dump = ad_dump, 68c1b3d7c5SThomas E. Spanjaard }; 69c1b3d7c5SThomas E. Spanjaard 70c1b3d7c5SThomas E. Spanjaard /* prototypes */ 71c1b3d7c5SThomas E. Spanjaard static void ad_init(device_t); 72c1b3d7c5SThomas E. Spanjaard static void ad_done(struct ata_request *); 73c1b3d7c5SThomas E. Spanjaard static void ad_describe(device_t dev); 74c1b3d7c5SThomas E. Spanjaard static int ad_version(u_int16_t); 75c1b3d7c5SThomas E. Spanjaard 76c1b3d7c5SThomas E. Spanjaard /* local vars */ 77c1b3d7c5SThomas E. Spanjaard static MALLOC_DEFINE(M_AD, "ad_driver", "ATA disk driver"); 78c1b3d7c5SThomas E. Spanjaard 79c1b3d7c5SThomas E. Spanjaard static int 80c1b3d7c5SThomas E. Spanjaard ad_probe(device_t dev) 81c1b3d7c5SThomas E. Spanjaard { 82c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(dev); 83c1b3d7c5SThomas E. Spanjaard 84c1b3d7c5SThomas E. Spanjaard if (!(atadev->param.config & ATA_PROTO_ATAPI) || 85c1b3d7c5SThomas E. Spanjaard (atadev->param.config == ATA_CFA_MAGIC1) || 863ec9ecbcSMatthew Dillon (atadev->param.config == ATA_CFA_MAGIC2) || 873ec9ecbcSMatthew Dillon (atadev->param.config == ATA_CFA_MAGIC3)) 88c1b3d7c5SThomas E. Spanjaard return 0; 89c1b3d7c5SThomas E. Spanjaard else 90c1b3d7c5SThomas E. Spanjaard return ENXIO; 91c1b3d7c5SThomas E. Spanjaard } 92c1b3d7c5SThomas E. Spanjaard 93c1b3d7c5SThomas E. Spanjaard static int 94c1b3d7c5SThomas E. Spanjaard ad_attach(device_t dev) 95c1b3d7c5SThomas E. Spanjaard { 96c1b3d7c5SThomas E. Spanjaard struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 97c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(dev); 98a688b15cSMatthew Dillon struct disk_info info; 99c1b3d7c5SThomas E. Spanjaard struct ad_softc *adp; 100c1b3d7c5SThomas E. Spanjaard cdev_t cdev; 101c1b3d7c5SThomas E. Spanjaard u_int32_t lbasize; 102c1b3d7c5SThomas E. Spanjaard u_int64_t lbasize48; 103c1b3d7c5SThomas E. Spanjaard 104c1b3d7c5SThomas E. Spanjaard /* check that we have a virgin disk to attach */ 105c1b3d7c5SThomas E. Spanjaard if (device_get_ivars(dev)) 106c1b3d7c5SThomas E. Spanjaard return EEXIST; 107c1b3d7c5SThomas E. Spanjaard 108a01741bbSMatthew Dillon adp = kmalloc(sizeof(struct ad_softc), M_AD, M_INTWAIT | M_ZERO); 109c1b3d7c5SThomas E. Spanjaard device_set_ivars(dev, adp); 110c1b3d7c5SThomas E. Spanjaard 11187870bc8SMatthew Dillon if ((atadev->param.atavalid & ATA_FLAG_54_58) && 11287870bc8SMatthew Dillon atadev->param.current_heads && atadev->param.current_sectors) { 113c1b3d7c5SThomas E. Spanjaard adp->heads = atadev->param.current_heads; 114c1b3d7c5SThomas E. Spanjaard adp->sectors = atadev->param.current_sectors; 115c1b3d7c5SThomas E. Spanjaard adp->total_secs = (u_int32_t)atadev->param.current_size_1 | 116c1b3d7c5SThomas E. Spanjaard ((u_int32_t)atadev->param.current_size_2 << 16); 117c1b3d7c5SThomas E. Spanjaard } 118c1b3d7c5SThomas E. Spanjaard else { 119c1b3d7c5SThomas E. Spanjaard adp->heads = atadev->param.heads; 120c1b3d7c5SThomas E. Spanjaard adp->sectors = atadev->param.sectors; 121c1b3d7c5SThomas E. Spanjaard adp->total_secs = atadev->param.cylinders * adp->heads * adp->sectors; 122c1b3d7c5SThomas E. Spanjaard } 123c1b3d7c5SThomas E. Spanjaard lbasize = (u_int32_t)atadev->param.lba_size_1 | 124c1b3d7c5SThomas E. Spanjaard ((u_int32_t)atadev->param.lba_size_2 << 16); 125c1b3d7c5SThomas E. Spanjaard 126c1b3d7c5SThomas E. Spanjaard /* does this device need oldstyle CHS addressing */ 127c1b3d7c5SThomas E. Spanjaard if (!ad_version(atadev->param.version_major) || !lbasize) 128c1b3d7c5SThomas E. Spanjaard atadev->flags |= ATA_D_USE_CHS; 129c1b3d7c5SThomas E. Spanjaard 130c1b3d7c5SThomas E. Spanjaard /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 131c1b3d7c5SThomas E. Spanjaard if (atadev->param.cylinders == 16383 || adp->total_secs < lbasize) 132c1b3d7c5SThomas E. Spanjaard adp->total_secs = lbasize; 133c1b3d7c5SThomas E. Spanjaard 134c1b3d7c5SThomas E. Spanjaard /* use the 48bit LBA size if valid */ 135c1b3d7c5SThomas E. Spanjaard lbasize48 = ((u_int64_t)atadev->param.lba_size48_1) | 136c1b3d7c5SThomas E. Spanjaard ((u_int64_t)atadev->param.lba_size48_2 << 16) | 137c1b3d7c5SThomas E. Spanjaard ((u_int64_t)atadev->param.lba_size48_3 << 32) | 138c1b3d7c5SThomas E. Spanjaard ((u_int64_t)atadev->param.lba_size48_4 << 48); 139c1b3d7c5SThomas E. Spanjaard if ((atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) && 140c1b3d7c5SThomas E. Spanjaard lbasize48 > ATA_MAX_28BIT_LBA) 141c1b3d7c5SThomas E. Spanjaard adp->total_secs = lbasize48; 142c1b3d7c5SThomas E. Spanjaard 143c1b3d7c5SThomas E. Spanjaard /* init device parameters */ 144c1b3d7c5SThomas E. Spanjaard ad_init(dev); 145c1b3d7c5SThomas E. Spanjaard 146c1b3d7c5SThomas E. Spanjaard /* create the disk device */ 147c1b3d7c5SThomas E. Spanjaard /* XXX TGEN Maybe use DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, 148c1b3d7c5SThomas E. Spanjaard DEVSTAT_PRIORITY_MAX. */ 149c1b3d7c5SThomas E. Spanjaard devstat_add_entry(&adp->stats, "ad", device_get_unit(dev), DEV_BSIZE, 150c1b3d7c5SThomas E. Spanjaard DEVSTAT_NO_ORDERED_TAGS, 151c1b3d7c5SThomas E. Spanjaard DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE, 152c1b3d7c5SThomas E. Spanjaard DEVSTAT_PRIORITY_DISK); 153a688b15cSMatthew Dillon cdev = disk_create(device_get_unit(dev), &adp->disk, &ad_ops); 154c1b3d7c5SThomas E. Spanjaard cdev->si_drv1 = dev; 155c1b3d7c5SThomas E. Spanjaard if (ch->dma) 156c1b3d7c5SThomas E. Spanjaard cdev->si_iosize_max = ch->dma->max_iosize; 157c1b3d7c5SThomas E. Spanjaard else 158c1b3d7c5SThomas E. Spanjaard cdev->si_iosize_max = DFLTPHYS; 159c1b3d7c5SThomas E. Spanjaard adp->cdev = cdev; 160a688b15cSMatthew Dillon 161a688b15cSMatthew Dillon bzero(&info, sizeof(info)); 162a688b15cSMatthew Dillon info.d_media_blksize = DEV_BSIZE; /* mandatory */ 163a688b15cSMatthew Dillon info.d_media_blocks = adp->total_secs; 164a688b15cSMatthew Dillon 165a688b15cSMatthew Dillon info.d_secpertrack = adp->sectors; /* optional */ 166a688b15cSMatthew Dillon info.d_nheads = adp->heads; 167a688b15cSMatthew Dillon info.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors); 168a688b15cSMatthew Dillon info.d_secpercyl = adp->sectors * adp->heads; 16955230951SMatthew Dillon info.d_serialno = atadev->param.serial; 170a688b15cSMatthew Dillon 171c1b3d7c5SThomas E. Spanjaard device_add_child(dev, "subdisk", device_get_unit(dev)); 172c1b3d7c5SThomas E. Spanjaard bus_generic_attach(dev); 173c1b3d7c5SThomas E. Spanjaard 174c1b3d7c5SThomas E. Spanjaard /* announce we are here */ 175c1b3d7c5SThomas E. Spanjaard ad_describe(dev); 176a688b15cSMatthew Dillon 177a688b15cSMatthew Dillon disk_setdiskinfo(&adp->disk, &info); 178a688b15cSMatthew Dillon 179c1b3d7c5SThomas E. Spanjaard return 0; 180c1b3d7c5SThomas E. Spanjaard } 181c1b3d7c5SThomas E. Spanjaard 182c1b3d7c5SThomas E. Spanjaard static int 183c1b3d7c5SThomas E. Spanjaard ad_detach(device_t dev) 184c1b3d7c5SThomas E. Spanjaard { 185c1b3d7c5SThomas E. Spanjaard struct ad_softc *adp = device_get_ivars(dev); 186c1b3d7c5SThomas E. Spanjaard device_t *children; 187c1b3d7c5SThomas E. Spanjaard int nchildren, i; 188c1b3d7c5SThomas E. Spanjaard 189c1b3d7c5SThomas E. Spanjaard /* check that we have a valid disk to detach */ 190c1b3d7c5SThomas E. Spanjaard if (!adp) 191c1b3d7c5SThomas E. Spanjaard return ENXIO; 192c1b3d7c5SThomas E. Spanjaard 193c1b3d7c5SThomas E. Spanjaard #if 0 /* XXX TGEN Probably useless, we fail the queue below. */ 194c1b3d7c5SThomas E. Spanjaard /* check that the disk is closed */ 195c1b3d7c5SThomas E. Spanjaard if (adp->ad_flags & AD_DISK_OPEN) 196c1b3d7c5SThomas E. Spanjaard return EBUSY; 197c1b3d7c5SThomas E. Spanjaard #endif /* 0 */ 198c1b3d7c5SThomas E. Spanjaard 199c1b3d7c5SThomas E. Spanjaard /* detach & delete all children */ 200c1b3d7c5SThomas E. Spanjaard if (!device_get_children(dev, &children, &nchildren)) { 201c1b3d7c5SThomas E. Spanjaard for (i = 0; i < nchildren; i++) 202c1b3d7c5SThomas E. Spanjaard if (children[i]) 203c1b3d7c5SThomas E. Spanjaard device_delete_child(dev, children[i]); 204c1b3d7c5SThomas E. Spanjaard kfree(children, M_TEMP); 205c1b3d7c5SThomas E. Spanjaard } 206c1b3d7c5SThomas E. Spanjaard 207c1b3d7c5SThomas E. Spanjaard /* detroy disk from the system so we dont get any further requests */ 208c1b3d7c5SThomas E. Spanjaard disk_invalidate(&adp->disk); 209c1b3d7c5SThomas E. Spanjaard disk_destroy(&adp->disk); 210c1b3d7c5SThomas E. Spanjaard 211c1b3d7c5SThomas E. Spanjaard /* fail requests on the queue and any thats "in flight" for this device */ 212c1b3d7c5SThomas E. Spanjaard ata_fail_requests(dev); 213c1b3d7c5SThomas E. Spanjaard 214c1b3d7c5SThomas E. Spanjaard /* dont leave anything behind */ 215c1b3d7c5SThomas E. Spanjaard /* disk_destroy() already took care of the dev_ops */ 216c1b3d7c5SThomas E. Spanjaard devstat_remove_entry(&adp->stats); 217c1b3d7c5SThomas E. Spanjaard device_set_ivars(dev, NULL); 218c1b3d7c5SThomas E. Spanjaard kfree(adp, M_AD); 219c1b3d7c5SThomas E. Spanjaard return 0; 220c1b3d7c5SThomas E. Spanjaard } 221c1b3d7c5SThomas E. Spanjaard 222c1b3d7c5SThomas E. Spanjaard static void 223c1b3d7c5SThomas E. Spanjaard ad_shutdown(device_t dev) 224c1b3d7c5SThomas E. Spanjaard { 225c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(dev); 226c1b3d7c5SThomas E. Spanjaard 227c1b3d7c5SThomas E. Spanjaard if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE) 228c1b3d7c5SThomas E. Spanjaard ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0); 229c1b3d7c5SThomas E. Spanjaard } 230c1b3d7c5SThomas E. Spanjaard 231c1b3d7c5SThomas E. Spanjaard static int 232c1b3d7c5SThomas E. Spanjaard ad_reinit(device_t dev) 233c1b3d7c5SThomas E. Spanjaard { 234c1b3d7c5SThomas E. Spanjaard struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 235c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(dev); 236c1b3d7c5SThomas E. Spanjaard 237c1b3d7c5SThomas E. Spanjaard /* if detach pending, return error */ 238c1b3d7c5SThomas E. Spanjaard if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATA_MASTER)) || 239c1b3d7c5SThomas E. Spanjaard ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATA_SLAVE))) { 240c1b3d7c5SThomas E. Spanjaard return 1; 241c1b3d7c5SThomas E. Spanjaard } 242c1b3d7c5SThomas E. Spanjaard ad_init(dev); 243c1b3d7c5SThomas E. Spanjaard return 0; 244c1b3d7c5SThomas E. Spanjaard } 245c1b3d7c5SThomas E. Spanjaard 246c1b3d7c5SThomas E. Spanjaard static int 247c1b3d7c5SThomas E. Spanjaard ad_open(struct dev_open_args *ap) 248c1b3d7c5SThomas E. Spanjaard { 249c1b3d7c5SThomas E. Spanjaard device_t dev = ap->a_head.a_dev->si_drv1; 250c1b3d7c5SThomas E. Spanjaard struct ad_softc *adp = device_get_ivars(dev); 251c1b3d7c5SThomas E. Spanjaard 252c1b3d7c5SThomas E. Spanjaard if (!adp || adp->cdev == NULL) 253c1b3d7c5SThomas E. Spanjaard return ENXIO; 254c1b3d7c5SThomas E. Spanjaard if(!device_is_attached(dev)) 255c1b3d7c5SThomas E. Spanjaard return EBUSY; 256c1b3d7c5SThomas E. Spanjaard 257c1b3d7c5SThomas E. Spanjaard #if 0 /* XXX TGEN Probably useless, queue will be failed on detach. */ 258c1b3d7c5SThomas E. Spanjaard adp->ad_flags &= AD_DISK_OPEN; 259c1b3d7c5SThomas E. Spanjaard #endif /* 0 */ 260c1b3d7c5SThomas E. Spanjaard return 0; 261c1b3d7c5SThomas E. Spanjaard } 262c1b3d7c5SThomas E. Spanjaard 263c1b3d7c5SThomas E. Spanjaard static int 264c1b3d7c5SThomas E. Spanjaard ad_close(struct dev_close_args *ap) 265c1b3d7c5SThomas E. Spanjaard { 26687870bc8SMatthew Dillon #if 0 /* XXX TGEN Probably useless, queue will be failed on detach. */ 267c1b3d7c5SThomas E. Spanjaard device_t dev = ap->a_head.a_dev->si_drv1; 268c1b3d7c5SThomas E. Spanjaard struct ad_softc *adp = device_get_ivars(dev); 269c1b3d7c5SThomas E. Spanjaard adp->ad_flags |= AD_DISK_OPEN; 270c1b3d7c5SThomas E. Spanjaard #endif /* 0 */ 271c1b3d7c5SThomas E. Spanjaard return 0; 272c1b3d7c5SThomas E. Spanjaard } 273c1b3d7c5SThomas E. Spanjaard 274c1b3d7c5SThomas E. Spanjaard static int 275c1b3d7c5SThomas E. Spanjaard ad_ioctl(struct dev_ioctl_args *ap) 276c1b3d7c5SThomas E. Spanjaard { 277c1b3d7c5SThomas E. Spanjaard return ata_device_ioctl(ap->a_head.a_dev->si_drv1, ap->a_cmd, ap->a_data); 278c1b3d7c5SThomas E. Spanjaard } 279c1b3d7c5SThomas E. Spanjaard 280c1b3d7c5SThomas E. Spanjaard static int 281c1b3d7c5SThomas E. Spanjaard ad_strategy(struct dev_strategy_args *ap) 282c1b3d7c5SThomas E. Spanjaard { 283c1b3d7c5SThomas E. Spanjaard device_t dev = ap->a_head.a_dev->si_drv1; 284c1b3d7c5SThomas E. Spanjaard struct bio *bp = ap->a_bio; 285c1b3d7c5SThomas E. Spanjaard struct buf *bbp = bp->bio_buf; 286c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(dev); 287c1b3d7c5SThomas E. Spanjaard struct ata_request *request; 288c1b3d7c5SThomas E. Spanjaard struct ad_softc *adp = device_get_ivars(dev); 289c1b3d7c5SThomas E. Spanjaard 290c1b3d7c5SThomas E. Spanjaard if (!(request = ata_alloc_request())) { 291c1b3d7c5SThomas E. Spanjaard device_printf(dev, "FAILURE - out of memory in strategy\n"); 292c1b3d7c5SThomas E. Spanjaard bbp->b_flags |= B_ERROR; 293c1b3d7c5SThomas E. Spanjaard bbp->b_error = ENOMEM; 294c1b3d7c5SThomas E. Spanjaard biodone(bp); 295c1b3d7c5SThomas E. Spanjaard return(0); 296c1b3d7c5SThomas E. Spanjaard } 297c1b3d7c5SThomas E. Spanjaard 298c1b3d7c5SThomas E. Spanjaard /* setup request */ 299c1b3d7c5SThomas E. Spanjaard request->dev = dev; 300c1b3d7c5SThomas E. Spanjaard request->bio = bp; 301c1b3d7c5SThomas E. Spanjaard request->callback = ad_done; 3023c0963e0SMatthew Dillon request->timeout = ATA_DEFAULT_TIMEOUT; 303c1b3d7c5SThomas E. Spanjaard request->retries = 2; 304c1b3d7c5SThomas E. Spanjaard request->data = bbp->b_data; 305c1b3d7c5SThomas E. Spanjaard request->bytecount = bbp->b_bcount; 306c1b3d7c5SThomas E. Spanjaard /* lba is block granularity, convert byte granularity bio_offset */ 307c1b3d7c5SThomas E. Spanjaard request->u.ata.lba = (u_int64_t)(bp->bio_offset >> DEV_BSHIFT); 308c1b3d7c5SThomas E. Spanjaard request->u.ata.count = request->bytecount / DEV_BSIZE; 309c1b3d7c5SThomas E. Spanjaard request->transfersize = min(bbp->b_bcount, atadev->max_iosize); 310c1b3d7c5SThomas E. Spanjaard 311c1b3d7c5SThomas E. Spanjaard switch (bbp->b_cmd) { 312c1b3d7c5SThomas E. Spanjaard case BUF_CMD_READ: 313c1b3d7c5SThomas E. Spanjaard request->flags = ATA_R_READ; 314c1b3d7c5SThomas E. Spanjaard if (atadev->mode >= ATA_DMA) { 315c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_READ_DMA; 316c1b3d7c5SThomas E. Spanjaard request->flags |= ATA_R_DMA; 317c1b3d7c5SThomas E. Spanjaard } 318c1b3d7c5SThomas E. Spanjaard else if (request->transfersize > DEV_BSIZE) 319c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_READ_MUL; 320c1b3d7c5SThomas E. Spanjaard else 321c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_READ; 322c1b3d7c5SThomas E. Spanjaard break; 323c1b3d7c5SThomas E. Spanjaard case BUF_CMD_WRITE: 324c1b3d7c5SThomas E. Spanjaard request->flags = ATA_R_WRITE; 325c1b3d7c5SThomas E. Spanjaard if (atadev->mode >= ATA_DMA) { 326c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_WRITE_DMA; 327c1b3d7c5SThomas E. Spanjaard request->flags |= ATA_R_DMA; 328c1b3d7c5SThomas E. Spanjaard } 329c1b3d7c5SThomas E. Spanjaard else if (request->transfersize > DEV_BSIZE) 330c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_WRITE_MUL; 331c1b3d7c5SThomas E. Spanjaard else 332c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_WRITE; 333c1b3d7c5SThomas E. Spanjaard break; 33487870bc8SMatthew Dillon case BUF_CMD_FLUSH: 33587870bc8SMatthew Dillon request->u.ata.lba = 0; 33687870bc8SMatthew Dillon request->u.ata.count = 0; 33787870bc8SMatthew Dillon request->u.ata.feature = 0; 33887870bc8SMatthew Dillon request->bytecount = 0; 33987870bc8SMatthew Dillon request->transfersize = 0; 34087870bc8SMatthew Dillon request->flags = ATA_R_CONTROL; 34187870bc8SMatthew Dillon request->u.ata.command = ATA_FLUSHCACHE; 34287870bc8SMatthew Dillon break; 343c1b3d7c5SThomas E. Spanjaard default: 344c1b3d7c5SThomas E. Spanjaard device_printf(dev, "FAILURE - unknown BUF operation\n"); 345c1b3d7c5SThomas E. Spanjaard ata_free_request(request); 346c1b3d7c5SThomas E. Spanjaard bbp->b_flags |= B_ERROR; 347c1b3d7c5SThomas E. Spanjaard bbp->b_error = EIO; 348c1b3d7c5SThomas E. Spanjaard biodone(bp); 349c1b3d7c5SThomas E. Spanjaard return(0); 350c1b3d7c5SThomas E. Spanjaard } 351c1b3d7c5SThomas E. Spanjaard request->flags |= ATA_R_ORDERED; 352c1b3d7c5SThomas E. Spanjaard devstat_start_transaction(&adp->stats); 353c1b3d7c5SThomas E. Spanjaard ata_queue_request(request); 354c1b3d7c5SThomas E. Spanjaard return(0); 355c1b3d7c5SThomas E. Spanjaard } 356c1b3d7c5SThomas E. Spanjaard 357c1b3d7c5SThomas E. Spanjaard static void 358c1b3d7c5SThomas E. Spanjaard ad_done(struct ata_request *request) 359c1b3d7c5SThomas E. Spanjaard { 360c1b3d7c5SThomas E. Spanjaard struct ad_softc *adp = device_get_ivars(request->dev); 361c1b3d7c5SThomas E. Spanjaard struct bio *bp = request->bio; 362c1b3d7c5SThomas E. Spanjaard struct buf *bbp = bp->bio_buf; 363c1b3d7c5SThomas E. Spanjaard 364c1b3d7c5SThomas E. Spanjaard /* finish up transfer */ 365c1b3d7c5SThomas E. Spanjaard if ((bbp->b_error = request->result)) 366c1b3d7c5SThomas E. Spanjaard bbp->b_flags |= B_ERROR; 367c1b3d7c5SThomas E. Spanjaard bbp->b_resid = bbp->b_bcount - request->donecount; 368c1b3d7c5SThomas E. Spanjaard devstat_end_transaction_buf(&adp->stats, bbp); 369c1b3d7c5SThomas E. Spanjaard biodone(bp); 370c1b3d7c5SThomas E. Spanjaard ata_free_request(request); 371c1b3d7c5SThomas E. Spanjaard } 372c1b3d7c5SThomas E. Spanjaard 373c1b3d7c5SThomas E. Spanjaard static int 374c1b3d7c5SThomas E. Spanjaard ad_dump(struct dev_dump_args *ap) 375c1b3d7c5SThomas E. Spanjaard { 376c1b3d7c5SThomas E. Spanjaard device_t dev = ap->a_head.a_dev->si_drv1; 377c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(dev); 3785c16e43eSThomas E. Spanjaard struct ata_request request; 379c1b3d7c5SThomas E. Spanjaard 380*ef352403SAlex Hornung ata_drop_requests(dev); 381b24cd69cSAlex Hornung /* 382b24cd69cSAlex Hornung * 0 length means flush buffers and return 383b24cd69cSAlex Hornung */ 384b24cd69cSAlex Hornung if (ap->a_length == 0) { 385b24cd69cSAlex Hornung /* flush buffers to media */ 386b24cd69cSAlex Hornung if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE) 387b24cd69cSAlex Hornung return ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0); 388c1b3d7c5SThomas E. Spanjaard else 389b24cd69cSAlex Hornung return ENXIO; 390c1b3d7c5SThomas E. Spanjaard } 391c1b3d7c5SThomas E. Spanjaard 3925c16e43eSThomas E. Spanjaard bzero(&request, sizeof(struct ata_request)); 3935c16e43eSThomas E. Spanjaard request.dev = dev; 394b24cd69cSAlex Hornung 395b24cd69cSAlex Hornung request.data = ap->a_virtual; 396b24cd69cSAlex Hornung request.bytecount = ap->a_length; 3975c16e43eSThomas E. Spanjaard request.transfersize = min(request.bytecount, atadev->max_iosize); 398b24cd69cSAlex Hornung request.flags = ATA_R_WRITE; 399b24cd69cSAlex Hornung 4005c16e43eSThomas E. Spanjaard if (atadev->mode >= ATA_DMA) { 4015c16e43eSThomas E. Spanjaard request.u.ata.command = ATA_WRITE_DMA; 4025c16e43eSThomas E. Spanjaard request.flags |= ATA_DMA; 4035c16e43eSThomas E. Spanjaard } else if (request.transfersize > DEV_BSIZE) 4045c16e43eSThomas E. Spanjaard request.u.ata.command = ATA_WRITE_MUL; 4055c16e43eSThomas E. Spanjaard else 4065c16e43eSThomas E. Spanjaard request.u.ata.command = ATA_WRITE; 407b24cd69cSAlex Hornung request.u.ata.lba = ap->a_offset / DEV_BSIZE; 408b24cd69cSAlex Hornung request.u.ata.count = request.bytecount / DEV_BSIZE; 409b24cd69cSAlex Hornung 410b24cd69cSAlex Hornung request.timeout = ATA_DEFAULT_TIMEOUT; 411b24cd69cSAlex Hornung request.retries = 2; 412b24cd69cSAlex Hornung 4135c16e43eSThomas E. Spanjaard ata_queue_request(&request); 4145c16e43eSThomas E. Spanjaard return request.result; 415c1b3d7c5SThomas E. Spanjaard } 416c1b3d7c5SThomas E. Spanjaard 417c1b3d7c5SThomas E. Spanjaard static void 418c1b3d7c5SThomas E. Spanjaard ad_init(device_t dev) 419c1b3d7c5SThomas E. Spanjaard { 420c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(dev); 421c1b3d7c5SThomas E. Spanjaard 422c1b3d7c5SThomas E. Spanjaard ATA_SETMODE(device_get_parent(dev), dev); 423c1b3d7c5SThomas E. Spanjaard 424c1b3d7c5SThomas E. Spanjaard /* enable readahead caching */ 425c1b3d7c5SThomas E. Spanjaard if (atadev->param.support.command1 & ATA_SUPPORT_LOOKAHEAD) 426c1b3d7c5SThomas E. Spanjaard ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0); 427c1b3d7c5SThomas E. Spanjaard 428c1b3d7c5SThomas E. Spanjaard /* enable write caching if supported and configured */ 429c1b3d7c5SThomas E. Spanjaard if (atadev->param.support.command1 & ATA_SUPPORT_WRITECACHE) { 430c1b3d7c5SThomas E. Spanjaard if (ata_wc) 431c1b3d7c5SThomas E. Spanjaard ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0); 432c1b3d7c5SThomas E. Spanjaard else 433c1b3d7c5SThomas E. Spanjaard ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0); 434c1b3d7c5SThomas E. Spanjaard } 435c1b3d7c5SThomas E. Spanjaard 436c1b3d7c5SThomas E. Spanjaard /* use multiple sectors/interrupt if device supports it */ 437c1b3d7c5SThomas E. Spanjaard if (ad_version(atadev->param.version_major)) { 43879db9382SMatthew Dillon int secsperint = max(1, min(atadev->param.sectors_intr & 0xff, 16)); 439c1b3d7c5SThomas E. Spanjaard 440c1b3d7c5SThomas E. Spanjaard if (!ata_controlcmd(dev, ATA_SET_MULTI, 0, 0, secsperint)) 441c1b3d7c5SThomas E. Spanjaard atadev->max_iosize = secsperint * DEV_BSIZE; 442c1b3d7c5SThomas E. Spanjaard } 443c1b3d7c5SThomas E. Spanjaard else 444c1b3d7c5SThomas E. Spanjaard atadev->max_iosize = DEV_BSIZE; 445c1b3d7c5SThomas E. Spanjaard } 446c1b3d7c5SThomas E. Spanjaard 447c1b3d7c5SThomas E. Spanjaard void 448c1b3d7c5SThomas E. Spanjaard ad_describe(device_t dev) 449c1b3d7c5SThomas E. Spanjaard { 450c1b3d7c5SThomas E. Spanjaard struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 451c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(dev); 452c1b3d7c5SThomas E. Spanjaard struct ad_softc *adp = device_get_ivars(dev); 453c1b3d7c5SThomas E. Spanjaard u_int8_t *marker, vendor[64], product[64]; 454c1b3d7c5SThomas E. Spanjaard 455c1b3d7c5SThomas E. Spanjaard /* try to seperate the ATA model string into vendor and model parts */ 456c1b3d7c5SThomas E. Spanjaard if ((marker = index(atadev->param.model, ' ')) || 457c1b3d7c5SThomas E. Spanjaard (marker = index(atadev->param.model, '-'))) { 458c1b3d7c5SThomas E. Spanjaard int len = (marker - atadev->param.model); 459c1b3d7c5SThomas E. Spanjaard 460c1b3d7c5SThomas E. Spanjaard strncpy(vendor, atadev->param.model, len); 461c1b3d7c5SThomas E. Spanjaard vendor[len++] = 0; 462c1b3d7c5SThomas E. Spanjaard strcat(vendor, " "); 463c1b3d7c5SThomas E. Spanjaard strncpy(product, atadev->param.model + len, 40 - len); 464c1b3d7c5SThomas E. Spanjaard vendor[40 - len] = 0; 465c1b3d7c5SThomas E. Spanjaard } 466c1b3d7c5SThomas E. Spanjaard else { 467c1b3d7c5SThomas E. Spanjaard if (!strncmp(atadev->param.model, "ST", 2)) 468c1b3d7c5SThomas E. Spanjaard strcpy(vendor, "Seagate "); 4693ec9ecbcSMatthew Dillon else if (!strncmp(atadev->param.model, "HDS", 3)) 4703ec9ecbcSMatthew Dillon strcpy(vendor, "Hitachi "); 471c1b3d7c5SThomas E. Spanjaard else 472c1b3d7c5SThomas E. Spanjaard strcpy(vendor, ""); 473c1b3d7c5SThomas E. Spanjaard strncpy(product, atadev->param.model, 40); 474c1b3d7c5SThomas E. Spanjaard } 475c1b3d7c5SThomas E. Spanjaard 476c1b3d7c5SThomas E. Spanjaard device_printf(dev, "%lluMB <%s%s %.8s> at ata%d-%s %s%s\n", 477c1b3d7c5SThomas E. Spanjaard (unsigned long long)(adp->total_secs / (1048576 / DEV_BSIZE)), 478c1b3d7c5SThomas E. Spanjaard vendor, product, atadev->param.revision, 479c1b3d7c5SThomas E. Spanjaard device_get_unit(ch->dev), 480c1b3d7c5SThomas E. Spanjaard (atadev->unit == ATA_MASTER) ? "master" : "slave", 481c1b3d7c5SThomas E. Spanjaard (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "", 482c1b3d7c5SThomas E. Spanjaard ata_mode2str(atadev->mode)); 483c1b3d7c5SThomas E. Spanjaard if (bootverbose) { 484c1b3d7c5SThomas E. Spanjaard device_printf(dev, "%llu sectors [%lluC/%dH/%dS] " 485c1b3d7c5SThomas E. Spanjaard "%d sectors/interrupt %d depth queue\n", 486c1b3d7c5SThomas E. Spanjaard (unsigned long long)adp->total_secs,(unsigned long long)( 487c1b3d7c5SThomas E. Spanjaard adp->total_secs / (adp->heads * adp->sectors)), 488c1b3d7c5SThomas E. Spanjaard adp->heads, adp->sectors, atadev->max_iosize / DEV_BSIZE, 489c1b3d7c5SThomas E. Spanjaard adp->num_tags + 1); 490c1b3d7c5SThomas E. Spanjaard } 491c1b3d7c5SThomas E. Spanjaard } 492c1b3d7c5SThomas E. Spanjaard 493c1b3d7c5SThomas E. Spanjaard static int 494c1b3d7c5SThomas E. Spanjaard ad_version(u_int16_t version) 495c1b3d7c5SThomas E. Spanjaard { 496c1b3d7c5SThomas E. Spanjaard int bit; 497c1b3d7c5SThomas E. Spanjaard 498c1b3d7c5SThomas E. Spanjaard if (version == 0xffff) 499c1b3d7c5SThomas E. Spanjaard return 0; 500c1b3d7c5SThomas E. Spanjaard for (bit = 15; bit >= 0; bit--) 501c1b3d7c5SThomas E. Spanjaard if (version & (1<<bit)) 502c1b3d7c5SThomas E. Spanjaard return bit; 503c1b3d7c5SThomas E. Spanjaard return 0; 504c1b3d7c5SThomas E. Spanjaard } 505c1b3d7c5SThomas E. Spanjaard 506c1b3d7c5SThomas E. Spanjaard static device_method_t ad_methods[] = { 507c1b3d7c5SThomas E. Spanjaard /* device interface */ 508c1b3d7c5SThomas E. Spanjaard DEVMETHOD(device_probe, ad_probe), 509c1b3d7c5SThomas E. Spanjaard DEVMETHOD(device_attach, ad_attach), 510c1b3d7c5SThomas E. Spanjaard DEVMETHOD(device_detach, ad_detach), 511c1b3d7c5SThomas E. Spanjaard DEVMETHOD(device_shutdown, ad_shutdown), 512c1b3d7c5SThomas E. Spanjaard 513c1b3d7c5SThomas E. Spanjaard /* ATA methods */ 514c1b3d7c5SThomas E. Spanjaard DEVMETHOD(ata_reinit, ad_reinit), 515c1b3d7c5SThomas E. Spanjaard 516c1b3d7c5SThomas E. Spanjaard { 0, 0 } 517c1b3d7c5SThomas E. Spanjaard }; 518c1b3d7c5SThomas E. Spanjaard 519c1b3d7c5SThomas E. Spanjaard static driver_t ad_driver = { 520c1b3d7c5SThomas E. Spanjaard "ad", 521c1b3d7c5SThomas E. Spanjaard ad_methods, 522c1b3d7c5SThomas E. Spanjaard 0, 523c1b3d7c5SThomas E. Spanjaard }; 524c1b3d7c5SThomas E. Spanjaard 525c1b3d7c5SThomas E. Spanjaard devclass_t ad_devclass; 526c1b3d7c5SThomas E. Spanjaard 527c1b3d7c5SThomas E. Spanjaard DRIVER_MODULE(ad, ata, ad_driver, ad_devclass, NULL, NULL); 528c1b3d7c5SThomas E. Spanjaard MODULE_VERSION(ad, 1); 529c1b3d7c5SThomas E. Spanjaard MODULE_DEPEND(ad, ata, 1, 1, 1); 530