1 /*- 2 * Copyright (c) 2006 IronPort Systems 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 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_mfi.h" 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/selinfo.h> 36 #include <sys/module.h> 37 #include <sys/malloc.h> 38 #include <sys/uio.h> 39 40 #include <sys/bio.h> 41 #include <sys/bus.h> 42 #include <sys/conf.h> 43 #include <sys/disk.h> 44 #include <geom/geom_disk.h> 45 46 #include <vm/vm.h> 47 #include <vm/pmap.h> 48 49 #include <machine/md_var.h> 50 #include <machine/bus.h> 51 #include <sys/rman.h> 52 53 #include <dev/mfi/mfireg.h> 54 #include <dev/mfi/mfi_ioctl.h> 55 #include <dev/mfi/mfivar.h> 56 57 static int mfi_disk_probe(device_t dev); 58 static int mfi_disk_attach(device_t dev); 59 static int mfi_disk_detach(device_t dev); 60 61 static disk_open_t mfi_disk_open; 62 static disk_close_t mfi_disk_close; 63 static disk_strategy_t mfi_disk_strategy; 64 static dumper_t mfi_disk_dump; 65 66 static devclass_t mfi_disk_devclass; 67 68 struct mfi_disk { 69 device_t ld_dev; 70 int ld_id; 71 int ld_unit; 72 struct mfi_softc *ld_controller; 73 struct mfi_ld *ld_ld; 74 struct disk *ld_disk; 75 }; 76 77 static device_method_t mfi_disk_methods[] = { 78 DEVMETHOD(device_probe, mfi_disk_probe), 79 DEVMETHOD(device_attach, mfi_disk_attach), 80 DEVMETHOD(device_detach, mfi_disk_detach), 81 { 0, 0 } 82 }; 83 84 static driver_t mfi_disk_driver = { 85 "mfid", 86 mfi_disk_methods, 87 sizeof(struct mfi_disk) 88 }; 89 90 DRIVER_MODULE(mfid, mfi, mfi_disk_driver, mfi_disk_devclass, 0, 0); 91 92 static int 93 mfi_disk_probe(device_t dev) 94 { 95 96 return (0); 97 } 98 99 static int 100 mfi_disk_attach(device_t dev) 101 { 102 struct mfi_disk *sc; 103 struct mfi_ld *ld; 104 uint64_t sectors; 105 uint32_t secsize; 106 107 sc = device_get_softc(dev); 108 ld = device_get_ivars(dev); 109 110 sc->ld_dev = dev; 111 sc->ld_id = ld->ld_id; 112 sc->ld_unit = device_get_unit(dev); 113 sc->ld_ld = device_get_ivars(dev); 114 sc->ld_controller = device_get_softc(device_get_parent(dev)); 115 116 sectors = ld->ld_info->size; 117 secsize = MFI_SECTOR_LEN; 118 TAILQ_INSERT_TAIL(&sc->ld_controller->mfi_ld_tqh, ld, ld_link); 119 120 device_printf(dev, "%juMB (%ju sectors) RAID\n", 121 sectors / (1024 * 1024 / secsize), sectors); 122 123 sc->ld_disk = disk_alloc(); 124 sc->ld_disk->d_drv1 = sc; 125 sc->ld_disk->d_maxsize = sc->ld_controller->mfi_max_io * secsize; 126 sc->ld_disk->d_name = "mfid"; 127 sc->ld_disk->d_open = mfi_disk_open; 128 sc->ld_disk->d_close = mfi_disk_close; 129 sc->ld_disk->d_strategy = mfi_disk_strategy; 130 sc->ld_disk->d_dump = mfi_disk_dump; 131 sc->ld_disk->d_unit = sc->ld_unit; 132 sc->ld_disk->d_sectorsize = secsize; 133 sc->ld_disk->d_mediasize = sectors * secsize; 134 if (sc->ld_disk->d_mediasize >= (1 * 1024 * 1024)) { 135 sc->ld_disk->d_fwheads = 255; 136 sc->ld_disk->d_fwsectors = 63; 137 } else { 138 sc->ld_disk->d_fwheads = 64; 139 sc->ld_disk->d_fwsectors = 32; 140 } 141 disk_create(sc->ld_disk, DISK_VERSION); 142 143 return (0); 144 } 145 146 static int 147 mfi_disk_detach(device_t dev) 148 { 149 struct mfi_disk *sc; 150 151 sc = device_get_softc(dev); 152 153 if (sc->ld_disk->d_flags & DISKFLAG_OPEN) 154 return (EBUSY); 155 156 disk_destroy(sc->ld_disk); 157 return (0); 158 } 159 160 static int 161 mfi_disk_open(struct disk *dp) 162 { 163 164 return (0); 165 } 166 167 static int 168 mfi_disk_close(struct disk *dp) 169 { 170 171 return (0); 172 } 173 174 static void 175 mfi_disk_strategy(struct bio *bio) 176 { 177 struct mfi_disk *sc; 178 struct mfi_softc *controller; 179 180 sc = bio->bio_disk->d_drv1; 181 182 if (sc == NULL) { 183 bio->bio_error = EINVAL; 184 bio->bio_flags |= BIO_ERROR; 185 bio->bio_resid = bio->bio_bcount; 186 biodone(bio); 187 return; 188 } 189 190 controller = sc->ld_controller; 191 bio->bio_driver1 = (void *)(uintptr_t)sc->ld_id; 192 mtx_lock(&controller->mfi_io_lock); 193 mfi_enqueue_bio(controller, bio); 194 mfi_startio(controller); 195 mtx_unlock(&controller->mfi_io_lock); 196 return; 197 } 198 199 void 200 mfi_disk_complete(struct bio *bio) 201 { 202 struct mfi_disk *sc; 203 struct mfi_frame_header *hdr; 204 205 sc = bio->bio_disk->d_drv1; 206 hdr = bio->bio_driver1; 207 208 if (bio->bio_flags & BIO_ERROR) { 209 if (bio->bio_error == 0) 210 bio->bio_error = EIO; 211 disk_err(bio, "hard error", -1, 1); 212 } else { 213 bio->bio_resid = 0; 214 } 215 biodone(bio); 216 } 217 218 static int 219 mfi_disk_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len) 220 { 221 struct mfi_disk *sc; 222 struct mfi_softc *parent_sc; 223 struct disk *dp; 224 int error; 225 226 dp = arg; 227 sc = dp->d_drv1; 228 parent_sc = sc->ld_controller; 229 230 if (len > 0) { 231 if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset / 232 MFI_SECTOR_LEN, virt, len)) != 0) 233 return (error); 234 } else { 235 /* mfi_sync_cache(parent_sc, sc->ld_id); */ 236 } 237 238 return (0); 239 } 240