1 /*- 2 * Written by: David Jeffery 3 * Copyright (c) 2002 Adaptec Inc. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 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 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: src/sys/dev/ips/ips_disk.c,v 1.4 2003/09/22 04:59:07 njl Exp $ 28 * $DragonFly: src/sys/dev/raid/ips/ips_disk.c,v 1.4 2004/09/06 16:30:10 joerg Exp $ 29 */ 30 31 #include <dev/raid/ips/ips.h> 32 #include <dev/raid/ips/ips_disk.h> 33 #include <sys/stat.h> 34 35 static int ipsd_probe(device_t dev); 36 static int ipsd_attach(device_t dev); 37 static int ipsd_detach(device_t dev); 38 39 static disk_open_t ipsd_open; 40 static disk_close_t ipsd_close; 41 static disk_strategy_t ipsd_strategy; 42 43 static struct cdevsw ipsd_cdevsw = { 44 .d_name = "ipsd", 45 .d_maj = IPSD_CDEV_MAJOR, 46 .d_flags = D_DISK, 47 .d_port = NULL, 48 .d_clone = NULL, 49 .old_open = ipsd_open, 50 .old_close = ipsd_close, 51 .old_strategy = ipsd_strategy, 52 .old_read = physread, 53 .old_write = physwrite, 54 }; 55 56 static device_method_t ipsd_methods[] = { 57 DEVMETHOD(device_probe, ipsd_probe), 58 DEVMETHOD(device_attach, ipsd_attach), 59 DEVMETHOD(device_detach, ipsd_detach), 60 { 0, 0 } 61 }; 62 63 64 static driver_t ipsd_driver = { 65 "ipsd", 66 ipsd_methods, 67 sizeof(ipsdisk_softc_t) 68 }; 69 70 static devclass_t ipsd_devclass; 71 DRIVER_MODULE(ipsd, ips, ipsd_driver, ipsd_devclass, 0, 0); 72 73 /* 74 * handle opening of disk device. It must set up all information about 75 * the geometry and size of the disk 76 */ 77 static int 78 ipsd_open(dev_t dev, int oflags, int devtype, d_thread_t *td) 79 { 80 ipsdisk_softc_t *dsc = dev->si_drv1; 81 82 if (dsc == NULL) 83 return (ENXIO); 84 dsc->state |= IPS_DEV_OPEN; 85 DEVICE_PRINTF(2, dsc->dev, "I'm open\n"); 86 return 0; 87 } 88 89 static int 90 ipsd_close(dev_t dev, int oflags, int devtype, d_thread_t *td) 91 { 92 ipsdisk_softc_t *dsc = dev->si_drv1; 93 94 dsc->state &= ~IPS_DEV_OPEN; 95 DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n"); 96 return 0; 97 } 98 99 /* ipsd_finish is called to clean up and return a completed IO request */ 100 void 101 ipsd_finish(struct bio *iobuf) 102 { 103 if (iobuf->bio_flags & BIO_ERROR) { 104 ipsdisk_softc_t *dsc; 105 dsc = iobuf->bio_disk->d_drv1; 106 device_printf(dsc->dev, "iobuf error %d\n", iobuf->bio_error); 107 } else 108 iobuf->bio_resid = 0; 109 110 biodone(iobuf); 111 } 112 113 114 static void 115 ipsd_strategy(struct bio *iobuf) 116 { 117 ipsdisk_softc_t *dsc; 118 119 dsc = iobuf->bio_disk->d_drv1; 120 DEVICE_PRINTF(8, dsc->dev, "in strategy\n"); 121 iobuf->bio_driver1 = (void *)(uintptr_t)dsc->sc->drives[dsc->disk_number].drivenum; 122 ips_start_io_request(dsc->sc, iobuf); 123 } 124 125 static int 126 ipsd_probe(device_t dev) 127 { 128 DEVICE_PRINTF(2, dev, "in probe\n"); 129 device_set_desc(dev, "Logical Drive"); 130 return 0; 131 } 132 133 static int 134 ipsd_attach(device_t dev) 135 { 136 device_t adapter; 137 ipsdisk_softc_t *dsc; 138 struct disklabel *label; 139 u_int totalsectors; 140 u_int nheads, nsectors; 141 142 DEVICE_PRINTF(2, dev, "in attach\n"); 143 dsc = (ipsdisk_softc_t *)device_get_softc(dev); 144 bzero(dsc, sizeof(ipsdisk_softc_t)); 145 adapter = device_get_parent(dev); 146 dsc->dev = dev; 147 dsc->sc = device_get_softc(adapter); 148 dsc->unit = device_get_unit(dev); 149 dsc->disk_number = (uintptr_t) device_get_ivars(dev); 150 totalsectors = dsc->sc->drives[dsc->disk_number].sector_count; 151 if ((totalsectors > 0x400000) && 152 ((dsc->sc->adapter_info.miscflags & 0x8) == 0)) { 153 nheads = IPS_NORM_HEADS; 154 nsectors = IPS_NORM_SECTORS; 155 } else { 156 nheads = IPS_COMP_HEADS; 157 nsectors = IPS_COMP_SECTORS; 158 } 159 dsc->ipsd_dev_t = disk_create(dsc->unit, &dsc->ipsd_disk, 0, 160 &ipsd_cdevsw); 161 dsc->ipsd_dev_t->si_drv1 = dsc; 162 dsc->ipsd_dev_t->si_iosize_max = IPS_MAX_IO_SIZE; 163 label = &dsc->ipsd_disk.d_label; 164 bzero(label, sizeof(*label)); 165 label->d_ntracks = nheads; 166 label->d_nsectors = nsectors; 167 label->d_type = DTYPE_ESDI; 168 label->d_secsize = IPS_BLKSIZE; 169 label->d_ncylinders = totalsectors / nheads / nsectors; 170 label->d_secpercyl = nsectors / nheads; 171 label->d_secperunit = totalsectors; 172 device_printf(dev, "Logical Drive (%dMB)\n", 173 dsc->sc->drives[dsc->disk_number].sector_count >> 11); 174 return 0; 175 } 176 177 static int 178 ipsd_detach(device_t dev) 179 { 180 ipsdisk_softc_t *dsc; 181 182 DEVICE_PRINTF(2, dev, "in detach\n"); 183 dsc = (ipsdisk_softc_t *)device_get_softc(dev); 184 if (dsc->state & IPS_DEV_OPEN) 185 return (EBUSY); 186 disk_destroy(&dsc->ipsd_disk); 187 return 0; 188 } 189 190