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/sysctl.h> 39 #include <sys/uio.h> 40 41 #include <sys/bio.h> 42 #include <sys/bus.h> 43 #include <sys/conf.h> 44 #include <sys/disk.h> 45 #include <geom/geom_disk.h> 46 47 #include <vm/vm.h> 48 #include <vm/pmap.h> 49 50 #include <machine/md_var.h> 51 #include <machine/bus.h> 52 #include <sys/rman.h> 53 54 #include <dev/mfi/mfireg.h> 55 #include <dev/mfi/mfi_ioctl.h> 56 #include <dev/mfi/mfivar.h> 57 58 static int mfi_disk_probe(device_t dev); 59 static int mfi_disk_attach(device_t dev); 60 static int mfi_disk_detach(device_t dev); 61 62 static disk_open_t mfi_disk_open; 63 static disk_close_t mfi_disk_close; 64 static disk_strategy_t mfi_disk_strategy; 65 static dumper_t mfi_disk_dump; 66 67 static devclass_t mfi_disk_devclass; 68 69 static device_method_t mfi_disk_methods[] = { 70 DEVMETHOD(device_probe, mfi_disk_probe), 71 DEVMETHOD(device_attach, mfi_disk_attach), 72 DEVMETHOD(device_detach, mfi_disk_detach), 73 { 0, 0 } 74 }; 75 76 static driver_t mfi_disk_driver = { 77 "mfid", 78 mfi_disk_methods, 79 sizeof(struct mfi_disk) 80 }; 81 82 DRIVER_MODULE(mfid, mfi, mfi_disk_driver, mfi_disk_devclass, 0, 0); 83 84 static int 85 mfi_disk_probe(device_t dev) 86 { 87 88 return (0); 89 } 90 91 static int 92 mfi_disk_attach(device_t dev) 93 { 94 struct mfi_disk *sc; 95 struct mfi_ld_info *ld_info; 96 uint64_t sectors; 97 uint32_t secsize; 98 char *state; 99 100 sc = device_get_softc(dev); 101 ld_info = device_get_ivars(dev); 102 103 sc->ld_dev = dev; 104 sc->ld_id = ld_info->ld_config.properties.ld.v.target_id; 105 sc->ld_unit = device_get_unit(dev); 106 sc->ld_info = ld_info; 107 sc->ld_controller = device_get_softc(device_get_parent(dev)); 108 sc->ld_flags = 0; 109 110 sectors = ld_info->size; 111 secsize = MFI_SECTOR_LEN; 112 mtx_lock(&sc->ld_controller->mfi_io_lock); 113 TAILQ_INSERT_TAIL(&sc->ld_controller->mfi_ld_tqh, sc, ld_link); 114 mtx_unlock(&sc->ld_controller->mfi_io_lock); 115 116 switch (ld_info->ld_config.params.state) { 117 case MFI_LD_STATE_OFFLINE: 118 state = "offline"; 119 break; 120 case MFI_LD_STATE_PARTIALLY_DEGRADED: 121 state = "partially degraded"; 122 break; 123 case MFI_LD_STATE_DEGRADED: 124 state = "degraded"; 125 break; 126 case MFI_LD_STATE_OPTIMAL: 127 state = "optimal"; 128 break; 129 default: 130 state = "unknown"; 131 break; 132 } 133 device_printf(dev, "%juMB (%ju sectors) RAID volume '%s' is %s\n", 134 sectors / (1024 * 1024 / secsize), sectors, 135 ld_info->ld_config.properties.name, 136 state); 137 138 sc->ld_disk = disk_alloc(); 139 sc->ld_disk->d_drv1 = sc; 140 sc->ld_disk->d_maxsize = min(sc->ld_controller->mfi_max_io * secsize, 141 (sc->ld_controller->mfi_max_sge - 1) * PAGE_SIZE); 142 sc->ld_disk->d_name = "mfid"; 143 sc->ld_disk->d_open = mfi_disk_open; 144 sc->ld_disk->d_close = mfi_disk_close; 145 sc->ld_disk->d_strategy = mfi_disk_strategy; 146 sc->ld_disk->d_dump = mfi_disk_dump; 147 sc->ld_disk->d_unit = sc->ld_unit; 148 sc->ld_disk->d_sectorsize = secsize; 149 sc->ld_disk->d_mediasize = sectors * secsize; 150 if (sc->ld_disk->d_mediasize >= (1 * 1024 * 1024)) { 151 sc->ld_disk->d_fwheads = 255; 152 sc->ld_disk->d_fwsectors = 63; 153 } else { 154 sc->ld_disk->d_fwheads = 64; 155 sc->ld_disk->d_fwsectors = 32; 156 } 157 disk_create(sc->ld_disk, DISK_VERSION); 158 159 return (0); 160 } 161 162 static int 163 mfi_disk_detach(device_t dev) 164 { 165 struct mfi_disk *sc; 166 167 sc = device_get_softc(dev); 168 169 mtx_lock(&sc->ld_controller->mfi_io_lock); 170 if (((sc->ld_disk->d_flags & DISKFLAG_OPEN) || 171 (sc->ld_flags & MFI_DISK_FLAGS_OPEN)) && 172 (sc->ld_controller->mfi_keep_deleted_volumes || 173 sc->ld_controller->mfi_detaching)) { 174 mtx_unlock(&sc->ld_controller->mfi_io_lock); 175 return (EBUSY); 176 } 177 mtx_unlock(&sc->ld_controller->mfi_io_lock); 178 179 disk_destroy(sc->ld_disk); 180 mtx_lock(&sc->ld_controller->mfi_io_lock); 181 TAILQ_REMOVE(&sc->ld_controller->mfi_ld_tqh, sc, ld_link); 182 mtx_unlock(&sc->ld_controller->mfi_io_lock); 183 free(sc->ld_info, M_MFIBUF); 184 return (0); 185 } 186 187 static int 188 mfi_disk_open(struct disk *dp) 189 { 190 struct mfi_disk *sc; 191 int error; 192 193 sc = dp->d_drv1; 194 mtx_lock(&sc->ld_controller->mfi_io_lock); 195 if (sc->ld_flags & MFI_DISK_FLAGS_DISABLED) 196 error = ENXIO; 197 else { 198 sc->ld_flags |= MFI_DISK_FLAGS_OPEN; 199 error = 0; 200 } 201 mtx_unlock(&sc->ld_controller->mfi_io_lock); 202 203 return (error); 204 } 205 206 static int 207 mfi_disk_close(struct disk *dp) 208 { 209 struct mfi_disk *sc; 210 211 sc = dp->d_drv1; 212 mtx_lock(&sc->ld_controller->mfi_io_lock); 213 sc->ld_flags &= ~MFI_DISK_FLAGS_OPEN; 214 mtx_unlock(&sc->ld_controller->mfi_io_lock); 215 216 return (0); 217 } 218 219 int 220 mfi_disk_disable(struct mfi_disk *sc) 221 { 222 223 mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED); 224 if (sc->ld_flags & MFI_DISK_FLAGS_OPEN) { 225 if (sc->ld_controller->mfi_delete_busy_volumes) 226 return (0); 227 device_printf(sc->ld_dev, "Unable to delete busy device\n"); 228 return (EBUSY); 229 } 230 sc->ld_flags |= MFI_DISK_FLAGS_DISABLED; 231 return (0); 232 } 233 234 void 235 mfi_disk_enable(struct mfi_disk *sc) 236 { 237 238 mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED); 239 sc->ld_flags &= ~MFI_DISK_FLAGS_DISABLED; 240 } 241 242 static void 243 mfi_disk_strategy(struct bio *bio) 244 { 245 struct mfi_disk *sc; 246 struct mfi_softc *controller; 247 248 sc = bio->bio_disk->d_drv1; 249 250 if (sc == NULL) { 251 bio->bio_error = EINVAL; 252 bio->bio_flags |= BIO_ERROR; 253 bio->bio_resid = bio->bio_bcount; 254 biodone(bio); 255 return; 256 } 257 258 controller = sc->ld_controller; 259 bio->bio_driver1 = (void *)(uintptr_t)sc->ld_id; 260 mtx_lock(&controller->mfi_io_lock); 261 mfi_enqueue_bio(controller, bio); 262 mfi_startio(controller); 263 mtx_unlock(&controller->mfi_io_lock); 264 return; 265 } 266 267 void 268 mfi_disk_complete(struct bio *bio) 269 { 270 struct mfi_disk *sc; 271 struct mfi_frame_header *hdr; 272 273 sc = bio->bio_disk->d_drv1; 274 hdr = bio->bio_driver1; 275 276 if (bio->bio_flags & BIO_ERROR) { 277 if (bio->bio_error == 0) 278 bio->bio_error = EIO; 279 disk_err(bio, "hard error", -1, 1); 280 } else { 281 bio->bio_resid = 0; 282 } 283 biodone(bio); 284 } 285 286 static int 287 mfi_disk_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len) 288 { 289 struct mfi_disk *sc; 290 struct mfi_softc *parent_sc; 291 struct disk *dp; 292 int error; 293 294 dp = arg; 295 sc = dp->d_drv1; 296 parent_sc = sc->ld_controller; 297 298 if (len > 0) { 299 if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset / 300 MFI_SECTOR_LEN, virt, len)) != 0) 301 return (error); 302 } else { 303 /* mfi_sync_cache(parent_sc, sc->ld_id); */ 304 } 305 306 return (0); 307 } 308