1286807Smav/*- 2286807Smav * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3286807Smav * Copyright (c) 2012 The FreeBSD Foundation 4286807Smav * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org> 5286807Smav * All rights reserved. 6286807Smav * 7286807Smav * Redistribution and use in source and binary forms, with or without 8286807Smav * modification, are permitted provided that the following conditions 9286807Smav * are met: 10286807Smav * 1. Redistributions of source code must retain the above copyright 11286807Smav * notice, this list of conditions and the following disclaimer, 12286807Smav * without modification, immediately at the beginning of the file. 13286807Smav * 2. Redistributions in binary form must reproduce the above copyright 14286807Smav * notice, this list of conditions and the following disclaimer in the 15286807Smav * documentation and/or other materials provided with the distribution. 16286807Smav * 17286807Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18286807Smav * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19286807Smav * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20286807Smav * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21286807Smav * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22286807Smav * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23286807Smav * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24286807Smav * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25286807Smav * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26286807Smav * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27286807Smav */ 28286807Smav 29286807Smav#include <sys/cdefs.h> 30286807Smav__FBSDID("$FreeBSD: stable/11/sys/cam/ctl/ctl_frontend_ioctl.c 345114 2019-03-13 20:28:07Z mav $"); 31286807Smav 32286807Smav#include <sys/param.h> 33286807Smav#include <sys/systm.h> 34286807Smav#include <sys/kernel.h> 35286807Smav#include <sys/types.h> 36286807Smav#include <sys/lock.h> 37286807Smav#include <sys/module.h> 38286807Smav#include <sys/mutex.h> 39286807Smav#include <sys/condvar.h> 40286807Smav#include <sys/malloc.h> 41286807Smav#include <sys/conf.h> 42286807Smav#include <sys/queue.h> 43286807Smav#include <sys/sysctl.h> 44286807Smav 45286807Smav#include <cam/cam.h> 46286807Smav#include <cam/scsi/scsi_all.h> 47286807Smav#include <cam/scsi/scsi_da.h> 48286807Smav#include <cam/ctl/ctl_io.h> 49286807Smav#include <cam/ctl/ctl.h> 50286807Smav#include <cam/ctl/ctl_frontend.h> 51286807Smav#include <cam/ctl/ctl_util.h> 52286807Smav#include <cam/ctl/ctl_backend.h> 53286807Smav#include <cam/ctl/ctl_ioctl.h> 54286807Smav#include <cam/ctl/ctl_ha.h> 55286807Smav#include <cam/ctl/ctl_private.h> 56286807Smav#include <cam/ctl/ctl_debug.h> 57286807Smav#include <cam/ctl/ctl_error.h> 58286807Smav 59288261Smavtypedef enum { 60288261Smav CTL_IOCTL_INPROG, 61288261Smav CTL_IOCTL_DATAMOVE, 62288261Smav CTL_IOCTL_DONE 63288261Smav} ctl_fe_ioctl_state; 64288261Smav 65288261Smavstruct ctl_fe_ioctl_params { 66288261Smav struct cv sem; 67288261Smav struct mtx ioctl_mtx; 68288261Smav ctl_fe_ioctl_state state; 69288261Smav}; 70288261Smav 71286807Smavstruct cfi_softc { 72286807Smav uint32_t cur_tag_num; 73286807Smav struct ctl_port port; 74286807Smav}; 75286807Smav 76286807Smavstatic struct cfi_softc cfi_softc; 77286807Smav 78286807Smavstatic int cfi_init(void); 79313368Smavstatic int cfi_shutdown(void); 80286807Smavstatic void cfi_datamove(union ctl_io *io); 81286807Smavstatic void cfi_done(union ctl_io *io); 82286807Smav 83286807Smavstatic struct ctl_frontend cfi_frontend = 84286807Smav{ 85286807Smav .name = "ioctl", 86286807Smav .init = cfi_init, 87286807Smav .shutdown = cfi_shutdown, 88286807Smav}; 89286807SmavCTL_FRONTEND_DECLARE(ctlioctl, cfi_frontend); 90286807Smav 91286807Smavstatic int 92286807Smavcfi_init(void) 93286807Smav{ 94286807Smav struct cfi_softc *isoftc = &cfi_softc; 95286807Smav struct ctl_port *port; 96313368Smav int error = 0; 97286807Smav 98286807Smav memset(isoftc, 0, sizeof(*isoftc)); 99286807Smav 100286807Smav port = &isoftc->port; 101286807Smav port->frontend = &cfi_frontend; 102286807Smav port->port_type = CTL_PORT_IOCTL; 103286807Smav port->num_requested_ctl_io = 100; 104286807Smav port->port_name = "ioctl"; 105286807Smav port->fe_datamove = cfi_datamove; 106286807Smav port->fe_done = cfi_done; 107286807Smav port->max_targets = 1; 108286807Smav port->max_target_id = 0; 109287621Smav port->targ_port = -1; 110286807Smav port->max_initiators = 1; 111286807Smav 112313368Smav if ((error = ctl_port_register(port)) != 0) { 113286807Smav printf("%s: ioctl port registration failed\n", __func__); 114313368Smav return (error); 115286807Smav } 116286807Smav ctl_port_online(port); 117286807Smav return (0); 118286807Smav} 119286807Smav 120313368Smavstatic int 121286807Smavcfi_shutdown(void) 122286807Smav{ 123286807Smav struct cfi_softc *isoftc = &cfi_softc; 124313368Smav struct ctl_port *port = &isoftc->port; 125313368Smav int error = 0; 126286807Smav 127286807Smav ctl_port_offline(port); 128313368Smav if ((error = ctl_port_deregister(port)) != 0) 129313368Smav printf("%s: ioctl port deregistration failed\n", __func__); 130313368Smav return (error); 131286807Smav} 132286807Smav 133286807Smav/* 134286807Smav * Data movement routine for the CTL ioctl frontend port. 135286807Smav */ 136286807Smavstatic int 137286807Smavctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 138286807Smav{ 139286807Smav struct ctl_sg_entry *ext_sglist, *kern_sglist; 140286807Smav struct ctl_sg_entry ext_entry, kern_entry; 141286807Smav int ext_sglen, ext_sg_entries, kern_sg_entries; 142286807Smav int ext_sg_start, ext_offset; 143313364Smav int len_to_copy; 144286807Smav int kern_watermark, ext_watermark; 145286807Smav int ext_sglist_malloced; 146286807Smav int i, j; 147286807Smav 148286807Smav CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 149286807Smav 150286807Smav /* 151286807Smav * If this flag is set, fake the data transfer. 152286807Smav */ 153286807Smav if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 154312848Smav ext_sglist_malloced = 0; 155313364Smav ctsio->ext_data_filled += ctsio->kern_data_len; 156313364Smav ctsio->kern_data_resid = 0; 157286807Smav goto bailout; 158286807Smav } 159286807Smav 160286807Smav /* 161286807Smav * To simplify things here, if we have a single buffer, stick it in 162286807Smav * a S/G entry and just make it a single entry S/G list. 163286807Smav */ 164288020Smav if (ctsio->ext_sg_entries > 0) { 165286807Smav int len_seen; 166286807Smav 167286807Smav ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 168286807Smav ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 169286807Smav M_WAITOK); 170286807Smav ext_sglist_malloced = 1; 171287860Smav if (copyin(ctsio->ext_data_ptr, ext_sglist, ext_sglen) != 0) { 172287860Smav ctsio->io_hdr.port_status = 31343; 173286807Smav goto bailout; 174286807Smav } 175286807Smav ext_sg_entries = ctsio->ext_sg_entries; 176312848Smav ext_sg_start = ext_sg_entries; 177312848Smav ext_offset = 0; 178286807Smav len_seen = 0; 179286807Smav for (i = 0; i < ext_sg_entries; i++) { 180286807Smav if ((len_seen + ext_sglist[i].len) >= 181286807Smav ctsio->ext_data_filled) { 182286807Smav ext_sg_start = i; 183286807Smav ext_offset = ctsio->ext_data_filled - len_seen; 184286807Smav break; 185286807Smav } 186286807Smav len_seen += ext_sglist[i].len; 187286807Smav } 188286807Smav } else { 189286807Smav ext_sglist = &ext_entry; 190312848Smav ext_sglist_malloced = 0; 191286807Smav ext_sglist->addr = ctsio->ext_data_ptr; 192286807Smav ext_sglist->len = ctsio->ext_data_len; 193286807Smav ext_sg_entries = 1; 194286807Smav ext_sg_start = 0; 195286807Smav ext_offset = ctsio->ext_data_filled; 196286807Smav } 197286807Smav 198286807Smav if (ctsio->kern_sg_entries > 0) { 199286807Smav kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 200286807Smav kern_sg_entries = ctsio->kern_sg_entries; 201286807Smav } else { 202286807Smav kern_sglist = &kern_entry; 203286807Smav kern_sglist->addr = ctsio->kern_data_ptr; 204286807Smav kern_sglist->len = ctsio->kern_data_len; 205286807Smav kern_sg_entries = 1; 206286807Smav } 207286807Smav 208286807Smav kern_watermark = 0; 209286807Smav ext_watermark = ext_offset; 210286807Smav for (i = ext_sg_start, j = 0; 211286807Smav i < ext_sg_entries && j < kern_sg_entries;) { 212286807Smav uint8_t *ext_ptr, *kern_ptr; 213286807Smav 214286807Smav len_to_copy = MIN(ext_sglist[i].len - ext_watermark, 215286807Smav kern_sglist[j].len - kern_watermark); 216286807Smav 217286807Smav ext_ptr = (uint8_t *)ext_sglist[i].addr; 218286807Smav ext_ptr = ext_ptr + ext_watermark; 219286807Smav if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 220286807Smav /* 221286807Smav * XXX KDM fix this! 222286807Smav */ 223286807Smav panic("need to implement bus address support"); 224286807Smav#if 0 225286807Smav kern_ptr = bus_to_virt(kern_sglist[j].addr); 226286807Smav#endif 227286807Smav } else 228286807Smav kern_ptr = (uint8_t *)kern_sglist[j].addr; 229286807Smav kern_ptr = kern_ptr + kern_watermark; 230286807Smav 231286807Smav if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 232286807Smav CTL_FLAG_DATA_IN) { 233286807Smav CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 234286807Smav "bytes to user\n", len_to_copy)); 235286807Smav CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 236286807Smav "to %p\n", kern_ptr, ext_ptr)); 237286807Smav if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 238287860Smav ctsio->io_hdr.port_status = 31344; 239286807Smav goto bailout; 240286807Smav } 241286807Smav } else { 242286807Smav CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 243286807Smav "bytes from user\n", len_to_copy)); 244286807Smav CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 245286807Smav "to %p\n", ext_ptr, kern_ptr)); 246286807Smav if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 247287860Smav ctsio->io_hdr.port_status = 31345; 248286807Smav goto bailout; 249286807Smav } 250286807Smav } 251286807Smav 252313364Smav ctsio->ext_data_filled += len_to_copy; 253313364Smav ctsio->kern_data_resid -= len_to_copy; 254286807Smav 255313364Smav ext_watermark += len_to_copy; 256286807Smav if (ext_sglist[i].len == ext_watermark) { 257286807Smav i++; 258286807Smav ext_watermark = 0; 259286807Smav } 260286807Smav 261313364Smav kern_watermark += len_to_copy; 262286807Smav if (kern_sglist[j].len == kern_watermark) { 263286807Smav j++; 264286807Smav kern_watermark = 0; 265286807Smav } 266286807Smav } 267286807Smav 268286807Smav CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 269286807Smav "kern_sg_entries: %d\n", ext_sg_entries, 270286807Smav kern_sg_entries)); 271286807Smav CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 272286807Smav "kern_data_len = %d\n", ctsio->ext_data_len, 273286807Smav ctsio->kern_data_len)); 274286807Smav 275286807Smavbailout: 276286807Smav if (ext_sglist_malloced != 0) 277286807Smav free(ext_sglist, M_CTL); 278286807Smav 279286807Smav return (CTL_RETVAL_COMPLETE); 280286807Smav} 281286807Smav 282286807Smavstatic void 283286807Smavcfi_datamove(union ctl_io *io) 284286807Smav{ 285286807Smav struct ctl_fe_ioctl_params *params; 286286807Smav 287286807Smav params = (struct ctl_fe_ioctl_params *) 288286807Smav io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 289286807Smav 290286807Smav mtx_lock(¶ms->ioctl_mtx); 291286807Smav params->state = CTL_IOCTL_DATAMOVE; 292286807Smav cv_broadcast(¶ms->sem); 293286807Smav mtx_unlock(¶ms->ioctl_mtx); 294286807Smav} 295286807Smav 296286807Smavstatic void 297286807Smavcfi_done(union ctl_io *io) 298286807Smav{ 299286807Smav struct ctl_fe_ioctl_params *params; 300286807Smav 301286807Smav params = (struct ctl_fe_ioctl_params *) 302286807Smav io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 303286807Smav 304286807Smav mtx_lock(¶ms->ioctl_mtx); 305286807Smav params->state = CTL_IOCTL_DONE; 306286807Smav cv_broadcast(¶ms->sem); 307286807Smav mtx_unlock(¶ms->ioctl_mtx); 308286807Smav} 309286807Smav 310286807Smavstatic int 311286807Smavcfi_submit_wait(union ctl_io *io) 312286807Smav{ 313286807Smav struct ctl_fe_ioctl_params params; 314286807Smav ctl_fe_ioctl_state last_state; 315286807Smav int done, retval; 316286807Smav 317286807Smav bzero(¶ms, sizeof(params)); 318286807Smav mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 319286807Smav cv_init(¶ms.sem, "ctlioccv"); 320286807Smav params.state = CTL_IOCTL_INPROG; 321286807Smav last_state = params.state; 322286807Smav 323286807Smav io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 324286807Smav 325286807Smav CTL_DEBUG_PRINT(("cfi_submit_wait\n")); 326286807Smav 327286807Smav /* This shouldn't happen */ 328286807Smav if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 329286807Smav return (retval); 330286807Smav 331286807Smav done = 0; 332286807Smav 333286807Smav do { 334286807Smav mtx_lock(¶ms.ioctl_mtx); 335286807Smav /* 336286807Smav * Check the state here, and don't sleep if the state has 337298810Spfg * already changed (i.e. wakeup has already occurred, but we 338286807Smav * weren't waiting yet). 339286807Smav */ 340286807Smav if (params.state == last_state) { 341286807Smav /* XXX KDM cv_wait_sig instead? */ 342286807Smav cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 343286807Smav } 344286807Smav last_state = params.state; 345286807Smav 346286807Smav switch (params.state) { 347286807Smav case CTL_IOCTL_INPROG: 348286807Smav /* Why did we wake up? */ 349286807Smav /* XXX KDM error here? */ 350286807Smav mtx_unlock(¶ms.ioctl_mtx); 351286807Smav break; 352286807Smav case CTL_IOCTL_DATAMOVE: 353286807Smav CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 354286807Smav 355286807Smav /* 356286807Smav * change last_state back to INPROG to avoid 357286807Smav * deadlock on subsequent data moves. 358286807Smav */ 359286807Smav params.state = last_state = CTL_IOCTL_INPROG; 360286807Smav 361286807Smav mtx_unlock(¶ms.ioctl_mtx); 362286807Smav ctl_ioctl_do_datamove(&io->scsiio); 363286807Smav /* 364286807Smav * Note that in some cases, most notably writes, 365286807Smav * this will queue the I/O and call us back later. 366286807Smav * In other cases, generally reads, this routine 367286807Smav * will immediately call back and wake us up, 368286807Smav * probably using our own context. 369286807Smav */ 370286807Smav io->scsiio.be_move_done(io); 371286807Smav break; 372286807Smav case CTL_IOCTL_DONE: 373286807Smav mtx_unlock(¶ms.ioctl_mtx); 374286807Smav CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 375286807Smav done = 1; 376286807Smav break; 377286807Smav default: 378286807Smav mtx_unlock(¶ms.ioctl_mtx); 379286807Smav /* XXX KDM error here? */ 380286807Smav break; 381286807Smav } 382286807Smav } while (done == 0); 383286807Smav 384286807Smav mtx_destroy(¶ms.ioctl_mtx); 385286807Smav cv_destroy(¶ms.sem); 386286807Smav 387286807Smav return (CTL_RETVAL_COMPLETE); 388286807Smav} 389286807Smav 390286807Smavint 391286807Smavctl_ioctl_io(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 392286807Smav struct thread *td) 393286807Smav{ 394286807Smav union ctl_io *io; 395312848Smav void *pool_tmp, *sc_tmp; 396286807Smav int retval = 0; 397286807Smav 398286807Smav /* 399286807Smav * If we haven't been "enabled", don't allow any SCSI I/O 400286807Smav * to this FETD. 401286807Smav */ 402286807Smav if ((cfi_softc.port.status & CTL_PORT_STATUS_ONLINE) == 0) 403286807Smav return (EPERM); 404286807Smav 405286807Smav io = ctl_alloc_io(cfi_softc.port.ctl_pool_ref); 406286807Smav 407286807Smav /* 408286807Smav * Need to save the pool reference so it doesn't get 409286807Smav * spammed by the user's ctl_io. 410286807Smav */ 411286807Smav pool_tmp = io->io_hdr.pool; 412312848Smav sc_tmp = CTL_SOFTC(io); 413286807Smav memcpy(io, (void *)addr, sizeof(*io)); 414286807Smav io->io_hdr.pool = pool_tmp; 415312848Smav CTL_SOFTC(io) = sc_tmp; 416345114Smav TAILQ_INIT(&io->io_hdr.blocked_queue); 417286807Smav 418286807Smav /* 419286807Smav * No status yet, so make sure the status is set properly. 420286807Smav */ 421286807Smav io->io_hdr.status = CTL_STATUS_NONE; 422286807Smav 423286807Smav /* 424286807Smav * The user sets the initiator ID, target and LUN IDs. 425286807Smav */ 426286807Smav io->io_hdr.nexus.targ_port = cfi_softc.port.targ_port; 427286807Smav io->io_hdr.flags |= CTL_FLAG_USER_REQ; 428286807Smav if ((io->io_hdr.io_type == CTL_IO_SCSI) && 429286807Smav (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 430286807Smav io->scsiio.tag_num = cfi_softc.cur_tag_num++; 431286807Smav 432286807Smav retval = cfi_submit_wait(io); 433286807Smav if (retval == 0) 434286807Smav memcpy((void *)addr, io, sizeof(*io)); 435286807Smav ctl_free_io(io); 436286807Smav return (retval); 437286807Smav} 438