1 /*- 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions 4 * are met: 5 * 6 * Copyright 1994-2009 The FreeBSD Project. 7 * All rights reserved. 8 * 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 FREEBSD PROJECT``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * EXEMPLARY,OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 * PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * The views and conclusions contained in the software and documentation 28 * are those of the authors and should not be interpreted as representing 29 * official policies,either expressed or implied, of the FreeBSD Project. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_mfi.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/selinfo.h> 41 #include <sys/module.h> 42 #include <sys/malloc.h> 43 #include <sys/sysctl.h> 44 #include <sys/uio.h> 45 46 #include <sys/bio.h> 47 #include <sys/bus.h> 48 #include <sys/conf.h> 49 #include <sys/disk.h> 50 #include <geom/geom_disk.h> 51 52 #include <vm/vm.h> 53 #include <vm/pmap.h> 54 55 #include <machine/md_var.h> 56 #include <machine/bus.h> 57 #include <sys/rman.h> 58 59 #include <dev/mfi/mfireg.h> 60 #include <dev/mfi/mfi_ioctl.h> 61 #include <dev/mfi/mfivar.h> 62 63 static int mfi_syspd_probe(device_t dev); 64 static int mfi_syspd_attach(device_t dev); 65 static int mfi_syspd_detach(device_t dev); 66 67 static disk_open_t mfi_syspd_open; 68 static disk_close_t mfi_syspd_close; 69 static disk_strategy_t mfi_syspd_strategy; 70 static dumper_t mfi_syspd_dump; 71 72 static devclass_t mfi_syspd_devclass; 73 74 static device_method_t mfi_syspd_methods[] = { 75 DEVMETHOD(device_probe, mfi_syspd_probe), 76 DEVMETHOD(device_attach, mfi_syspd_attach), 77 DEVMETHOD(device_detach, mfi_syspd_detach), 78 { 0, 0 } 79 }; 80 81 static driver_t mfi_syspd_driver = { 82 "mfisyspd", 83 mfi_syspd_methods, 84 sizeof(struct mfi_system_pd) 85 }; 86 87 DRIVER_MODULE(mfisyspd, mfi, mfi_syspd_driver, mfi_syspd_devclass, 0, 0); 88 89 static int 90 mfi_syspd_probe(device_t dev) 91 { 92 return (0); 93 } 94 95 static int 96 mfi_syspd_attach(device_t dev) 97 { 98 struct mfi_system_pd *sc; 99 struct mfi_pd_info *pd_info; 100 struct mfi_system_pending *syspd_pend; 101 uint64_t sectors; 102 uint32_t secsize; 103 104 sc = device_get_softc(dev); 105 pd_info = device_get_ivars(dev); 106 sc->pd_dev = dev; 107 sc->pd_id = pd_info->ref.v.device_id; 108 sc->pd_unit = device_get_unit(dev); 109 sc->pd_info = pd_info; 110 sc->pd_controller = device_get_softc(device_get_parent(dev)); 111 sc->pd_flags = 0; 112 113 sectors = pd_info->raw_size; 114 secsize = MFI_SECTOR_LEN; 115 mtx_lock(&sc->pd_controller->mfi_io_lock); 116 TAILQ_INSERT_TAIL(&sc->pd_controller->mfi_syspd_tqh, sc, pd_link); 117 TAILQ_FOREACH(syspd_pend, &sc->pd_controller->mfi_syspd_pend_tqh, 118 pd_link) { 119 TAILQ_REMOVE(&sc->pd_controller->mfi_syspd_pend_tqh, 120 syspd_pend, pd_link); 121 free(syspd_pend, M_MFIBUF); 122 break; 123 } 124 mtx_unlock(&sc->pd_controller->mfi_io_lock); 125 device_printf(dev, "%juMB (%ju sectors) SYSPD volume (deviceid: %d)\n", 126 sectors / (1024 * 1024 / secsize), sectors, sc->pd_id); 127 sc->pd_disk = disk_alloc(); 128 sc->pd_disk->d_drv1 = sc; 129 sc->pd_disk->d_maxsize = sc->pd_controller->mfi_max_io * secsize; 130 sc->pd_disk->d_name = "mfisyspd"; 131 sc->pd_disk->d_open = mfi_syspd_open; 132 sc->pd_disk->d_close = mfi_syspd_close; 133 sc->pd_disk->d_strategy = mfi_syspd_strategy; 134 sc->pd_disk->d_dump = mfi_syspd_dump; 135 sc->pd_disk->d_unit = sc->pd_unit; 136 sc->pd_disk->d_sectorsize = secsize; 137 sc->pd_disk->d_mediasize = sectors * secsize; 138 if (sc->pd_disk->d_mediasize >= (1 * 1024 * 1024)) { 139 sc->pd_disk->d_fwheads = 255; 140 sc->pd_disk->d_fwsectors = 63; 141 } else { 142 sc->pd_disk->d_fwheads = 64; 143 sc->pd_disk->d_fwsectors = 32; 144 } 145 disk_create(sc->pd_disk, DISK_VERSION); 146 147 device_printf(dev, " SYSPD volume attached\n"); 148 149 return (0); 150 } 151 152 static int 153 mfi_syspd_detach(device_t dev) 154 { 155 struct mfi_system_pd *sc; 156 157 sc = device_get_softc(dev); 158 device_printf(dev, "Detaching syspd\n"); 159 mtx_lock(&sc->pd_controller->mfi_io_lock); 160 if (((sc->pd_disk->d_flags & DISKFLAG_OPEN) || 161 (sc->pd_flags & MFI_DISK_FLAGS_OPEN)) && 162 (sc->pd_controller->mfi_keep_deleted_volumes || 163 sc->pd_controller->mfi_detaching)) { 164 mtx_unlock(&sc->pd_controller->mfi_io_lock); 165 device_printf(dev, "Cant detach syspd\n"); 166 return (EBUSY); 167 } 168 mtx_unlock(&sc->pd_controller->mfi_io_lock); 169 170 disk_destroy(sc->pd_disk); 171 mtx_lock(&sc->pd_controller->mfi_io_lock); 172 TAILQ_REMOVE(&sc->pd_controller->mfi_syspd_tqh, sc, pd_link); 173 mtx_unlock(&sc->pd_controller->mfi_io_lock); 174 free(sc->pd_info, M_MFIBUF); 175 return (0); 176 } 177 178 static int 179 mfi_syspd_open(struct disk *dp) 180 { 181 struct mfi_system_pd *sc; 182 int error; 183 184 sc = dp->d_drv1; 185 mtx_lock(&sc->pd_controller->mfi_io_lock); 186 if (sc->pd_flags & MFI_DISK_FLAGS_DISABLED) 187 error = ENXIO; 188 else { 189 sc->pd_flags |= MFI_DISK_FLAGS_OPEN; 190 error = 0; 191 } 192 mtx_unlock(&sc->pd_controller->mfi_io_lock); 193 return (error); 194 } 195 196 static int 197 mfi_syspd_close(struct disk *dp) 198 { 199 struct mfi_system_pd *sc; 200 201 sc = dp->d_drv1; 202 mtx_lock(&sc->pd_controller->mfi_io_lock); 203 sc->pd_flags &= ~MFI_DISK_FLAGS_OPEN; 204 mtx_unlock(&sc->pd_controller->mfi_io_lock); 205 206 return (0); 207 } 208 209 int 210 mfi_syspd_disable(struct mfi_system_pd *sc) 211 { 212 213 device_printf(sc->pd_dev, "syspd disable \n"); 214 mtx_assert(&sc->pd_controller->mfi_io_lock, MA_OWNED); 215 if (sc->pd_flags & MFI_DISK_FLAGS_OPEN) { 216 if (sc->pd_controller->mfi_delete_busy_volumes) 217 return (0); 218 device_printf(sc->pd_dev, 219 "Unable to delete busy syspd device\n"); 220 return (EBUSY); 221 } 222 sc->pd_flags |= MFI_DISK_FLAGS_DISABLED; 223 return (0); 224 } 225 226 void 227 mfi_syspd_enable(struct mfi_system_pd *sc) 228 { 229 230 device_printf(sc->pd_dev, "syspd enable \n"); 231 mtx_assert(&sc->pd_controller->mfi_io_lock, MA_OWNED); 232 sc->pd_flags &= ~MFI_DISK_FLAGS_DISABLED; 233 } 234 235 static void 236 mfi_syspd_strategy(struct bio *bio) 237 { 238 struct mfi_system_pd *sc; 239 struct mfi_softc *controller; 240 241 sc = bio->bio_disk->d_drv1; 242 243 if (sc == NULL) { 244 bio->bio_error = EINVAL; 245 bio->bio_flags |= BIO_ERROR; 246 bio->bio_resid = bio->bio_bcount; 247 biodone(bio); 248 return; 249 } 250 251 controller = sc->pd_controller; 252 bio->bio_driver1 = (void *)(uintptr_t)sc->pd_id; 253 /* Mark it as system PD IO */ 254 bio->bio_driver2 = (void *)MFI_SYS_PD_IO; 255 mtx_lock(&controller->mfi_io_lock); 256 mfi_enqueue_bio(controller, bio); 257 mfi_startio(controller); 258 mtx_unlock(&controller->mfi_io_lock); 259 return; 260 } 261 262 static int 263 mfi_syspd_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, 264 size_t len) 265 { 266 struct mfi_system_pd *sc; 267 struct mfi_softc *parent_sc; 268 struct disk *dp; 269 int error; 270 271 dp = arg; 272 sc = dp->d_drv1; 273 parent_sc = sc->pd_controller; 274 275 if (len > 0) { 276 if ((error = mfi_dump_syspd_blocks(parent_sc, 277 sc->pd_id, offset / MFI_SECTOR_LEN, virt, len)) != 0) 278 return (error); 279 } else { 280 /* mfi_sync_cache(parent_sc, sc->ld_id); */ 281 } 282 return (0); 283 } 284