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