1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * Copyright 1994-2009 The FreeBSD Project. 9 * All rights reserved. 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR 21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 * EXEMPLARY,OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 * PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY 25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * The views and conclusions contained in the software and documentation 30 * are those of the authors and should not be interpreted as representing 31 * official policies,either expressed or implied, of the FreeBSD Project. 32 */ 33 34 #include <sys/cdefs.h> 35 #include "opt_mfi.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/module.h> 42 #include <sys/malloc.h> 43 #include <sys/mutex.h> 44 #include <sys/selinfo.h> 45 #include <sys/sysctl.h> 46 #include <sys/uio.h> 47 48 #include <sys/bio.h> 49 #include <sys/bus.h> 50 #include <sys/conf.h> 51 #include <sys/disk.h> 52 #include <geom/geom_disk.h> 53 54 #include <vm/vm.h> 55 #include <vm/pmap.h> 56 57 #include <machine/md_var.h> 58 #include <machine/bus.h> 59 #include <sys/rman.h> 60 61 #include <dev/mfi/mfireg.h> 62 #include <dev/mfi/mfi_ioctl.h> 63 #include <dev/mfi/mfivar.h> 64 65 static int mfi_syspd_probe(device_t dev); 66 static int mfi_syspd_attach(device_t dev); 67 static int mfi_syspd_detach(device_t dev); 68 69 static disk_open_t mfi_syspd_open; 70 static disk_close_t mfi_syspd_close; 71 static disk_strategy_t mfi_syspd_strategy; 72 static dumper_t mfi_syspd_dump; 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, 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 = min(sc->pd_controller->mfi_max_io * secsize, 130 (sc->pd_controller->mfi_max_sge - 1) * PAGE_SIZE); 131 sc->pd_disk->d_name = "mfisyspd"; 132 sc->pd_disk->d_open = mfi_syspd_open; 133 sc->pd_disk->d_close = mfi_syspd_close; 134 sc->pd_disk->d_strategy = mfi_syspd_strategy; 135 sc->pd_disk->d_dump = mfi_syspd_dump; 136 sc->pd_disk->d_unit = sc->pd_unit; 137 sc->pd_disk->d_sectorsize = secsize; 138 sc->pd_disk->d_mediasize = sectors * secsize; 139 if (sc->pd_disk->d_mediasize >= (1 * 1024 * 1024)) { 140 sc->pd_disk->d_fwheads = 255; 141 sc->pd_disk->d_fwsectors = 63; 142 } else { 143 sc->pd_disk->d_fwheads = 64; 144 sc->pd_disk->d_fwsectors = 32; 145 } 146 sc->pd_disk->d_flags = DISKFLAG_UNMAPPED_BIO; 147 disk_create(sc->pd_disk, DISK_VERSION); 148 149 device_printf(dev, " SYSPD volume attached\n"); 150 151 return (0); 152 } 153 154 static int 155 mfi_syspd_detach(device_t dev) 156 { 157 struct mfi_system_pd *sc; 158 159 sc = device_get_softc(dev); 160 device_printf(dev, "Detaching syspd\n"); 161 mtx_lock(&sc->pd_controller->mfi_io_lock); 162 if (((sc->pd_disk->d_flags & DISKFLAG_OPEN) || 163 (sc->pd_flags & MFI_DISK_FLAGS_OPEN)) && 164 (sc->pd_controller->mfi_keep_deleted_volumes || 165 sc->pd_controller->mfi_detaching)) { 166 mtx_unlock(&sc->pd_controller->mfi_io_lock); 167 device_printf(dev, "Cant detach syspd\n"); 168 return (EBUSY); 169 } 170 mtx_unlock(&sc->pd_controller->mfi_io_lock); 171 172 disk_destroy(sc->pd_disk); 173 mtx_lock(&sc->pd_controller->mfi_io_lock); 174 TAILQ_REMOVE(&sc->pd_controller->mfi_syspd_tqh, sc, pd_link); 175 mtx_unlock(&sc->pd_controller->mfi_io_lock); 176 free(sc->pd_info, M_MFIBUF); 177 return (0); 178 } 179 180 static int 181 mfi_syspd_open(struct disk *dp) 182 { 183 struct mfi_system_pd *sc; 184 int error; 185 186 sc = dp->d_drv1; 187 mtx_lock(&sc->pd_controller->mfi_io_lock); 188 if (sc->pd_flags & MFI_DISK_FLAGS_DISABLED) 189 error = ENXIO; 190 else { 191 sc->pd_flags |= MFI_DISK_FLAGS_OPEN; 192 error = 0; 193 } 194 mtx_unlock(&sc->pd_controller->mfi_io_lock); 195 return (error); 196 } 197 198 static int 199 mfi_syspd_close(struct disk *dp) 200 { 201 struct mfi_system_pd *sc; 202 203 sc = dp->d_drv1; 204 mtx_lock(&sc->pd_controller->mfi_io_lock); 205 sc->pd_flags &= ~MFI_DISK_FLAGS_OPEN; 206 mtx_unlock(&sc->pd_controller->mfi_io_lock); 207 208 return (0); 209 } 210 211 int 212 mfi_syspd_disable(struct mfi_system_pd *sc) 213 { 214 215 device_printf(sc->pd_dev, "syspd disable \n"); 216 mtx_assert(&sc->pd_controller->mfi_io_lock, MA_OWNED); 217 if (sc->pd_flags & MFI_DISK_FLAGS_OPEN) { 218 if (sc->pd_controller->mfi_delete_busy_volumes) 219 return (0); 220 device_printf(sc->pd_dev, 221 "Unable to delete busy syspd device\n"); 222 return (EBUSY); 223 } 224 sc->pd_flags |= MFI_DISK_FLAGS_DISABLED; 225 return (0); 226 } 227 228 void 229 mfi_syspd_enable(struct mfi_system_pd *sc) 230 { 231 232 device_printf(sc->pd_dev, "syspd enable \n"); 233 mtx_assert(&sc->pd_controller->mfi_io_lock, MA_OWNED); 234 sc->pd_flags &= ~MFI_DISK_FLAGS_DISABLED; 235 } 236 237 static void 238 mfi_syspd_strategy(struct bio *bio) 239 { 240 struct mfi_system_pd *sc; 241 struct mfi_softc *controller; 242 243 sc = bio->bio_disk->d_drv1; 244 245 if (sc == NULL) { 246 bio->bio_error = EINVAL; 247 bio->bio_flags |= BIO_ERROR; 248 bio->bio_resid = bio->bio_bcount; 249 biodone(bio); 250 return; 251 } 252 253 controller = sc->pd_controller; 254 bio->bio_driver1 = (void *)(uintptr_t)sc->pd_id; 255 /* Mark it as system PD IO */ 256 bio->bio_driver2 = (void *)MFI_SYS_PD_IO; 257 mtx_lock(&controller->mfi_io_lock); 258 mfi_enqueue_bio(controller, bio); 259 mfi_startio(controller); 260 mtx_unlock(&controller->mfi_io_lock); 261 return; 262 } 263 264 static int 265 mfi_syspd_dump(void *arg, void *virt, off_t offset, size_t len) 266 { 267 struct mfi_system_pd *sc; 268 struct mfi_softc *parent_sc; 269 struct disk *dp; 270 int error; 271 272 dp = arg; 273 sc = dp->d_drv1; 274 parent_sc = sc->pd_controller; 275 276 if (len > 0) { 277 if ((error = mfi_dump_syspd_blocks(parent_sc, 278 sc->pd_id, offset / MFI_SECTOR_LEN, virt, len)) != 0) 279 return (error); 280 } else { 281 /* mfi_sync_cache(parent_sc, sc->ld_id); */ 282 } 283 return (0); 284 } 285