ctl_frontend_ioctl.c revision 312849
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer, 12 * without modification, immediately at the beginning of the file. 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_frontend_ioctl.c 312849 2017-01-26 21:21:59Z mav $"); 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/kernel.h> 35#include <sys/types.h> 36#include <sys/lock.h> 37#include <sys/module.h> 38#include <sys/mutex.h> 39#include <sys/condvar.h> 40#include <sys/malloc.h> 41#include <sys/conf.h> 42#include <sys/queue.h> 43#include <sys/sysctl.h> 44 45#include <cam/cam.h> 46#include <cam/scsi/scsi_all.h> 47#include <cam/scsi/scsi_da.h> 48#include <cam/ctl/ctl_io.h> 49#include <cam/ctl/ctl.h> 50#include <cam/ctl/ctl_frontend.h> 51#include <cam/ctl/ctl_util.h> 52#include <cam/ctl/ctl_backend.h> 53#include <cam/ctl/ctl_ioctl.h> 54#include <cam/ctl/ctl_ha.h> 55#include <cam/ctl/ctl_private.h> 56#include <cam/ctl/ctl_debug.h> 57#include <cam/ctl/ctl_error.h> 58 59typedef enum { 60 CTL_IOCTL_INPROG, 61 CTL_IOCTL_DATAMOVE, 62 CTL_IOCTL_DONE 63} ctl_fe_ioctl_state; 64 65struct ctl_fe_ioctl_params { 66 struct cv sem; 67 struct mtx ioctl_mtx; 68 ctl_fe_ioctl_state state; 69}; 70 71struct cfi_softc { 72 uint32_t cur_tag_num; 73 struct ctl_port port; 74}; 75 76static struct cfi_softc cfi_softc; 77 78static int cfi_init(void); 79static void cfi_shutdown(void); 80static void cfi_datamove(union ctl_io *io); 81static void cfi_done(union ctl_io *io); 82 83static struct ctl_frontend cfi_frontend = 84{ 85 .name = "ioctl", 86 .init = cfi_init, 87 .shutdown = cfi_shutdown, 88}; 89CTL_FRONTEND_DECLARE(ctlioctl, cfi_frontend); 90 91static int 92cfi_init(void) 93{ 94 struct cfi_softc *isoftc = &cfi_softc; 95 struct ctl_port *port; 96 97 memset(isoftc, 0, sizeof(*isoftc)); 98 99 port = &isoftc->port; 100 port->frontend = &cfi_frontend; 101 port->port_type = CTL_PORT_IOCTL; 102 port->num_requested_ctl_io = 100; 103 port->port_name = "ioctl"; 104 port->fe_datamove = cfi_datamove; 105 port->fe_done = cfi_done; 106 port->max_targets = 1; 107 port->max_target_id = 0; 108 port->targ_port = -1; 109 port->max_initiators = 1; 110 111 if (ctl_port_register(port) != 0) { 112 printf("%s: ioctl port registration failed\n", __func__); 113 return (0); 114 } 115 ctl_port_online(port); 116 return (0); 117} 118 119void 120cfi_shutdown(void) 121{ 122 struct cfi_softc *isoftc = &cfi_softc; 123 struct ctl_port *port; 124 125 port = &isoftc->port; 126 ctl_port_offline(port); 127 if (ctl_port_deregister(&isoftc->port) != 0) 128 printf("%s: ctl_frontend_deregister() failed\n", __func__); 129} 130 131/* 132 * Data movement routine for the CTL ioctl frontend port. 133 */ 134static int 135ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 136{ 137 struct ctl_sg_entry *ext_sglist, *kern_sglist; 138 struct ctl_sg_entry ext_entry, kern_entry; 139 int ext_sglen, ext_sg_entries, kern_sg_entries; 140 int ext_sg_start, ext_offset; 141 int len_to_copy, len_copied; 142 int kern_watermark, ext_watermark; 143 int ext_sglist_malloced; 144 int i, j; 145 146 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 147 148 /* 149 * If this flag is set, fake the data transfer. 150 */ 151 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 152 ext_sglist_malloced = 0; 153 ctsio->ext_data_filled = ctsio->ext_data_len; 154 goto bailout; 155 } 156 157 /* 158 * To simplify things here, if we have a single buffer, stick it in 159 * a S/G entry and just make it a single entry S/G list. 160 */ 161 if (ctsio->ext_sg_entries > 0) { 162 int len_seen; 163 164 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 165 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 166 M_WAITOK); 167 ext_sglist_malloced = 1; 168 if (copyin(ctsio->ext_data_ptr, ext_sglist, ext_sglen) != 0) { 169 ctsio->io_hdr.port_status = 31343; 170 goto bailout; 171 } 172 ext_sg_entries = ctsio->ext_sg_entries; 173 ext_sg_start = ext_sg_entries; 174 ext_offset = 0; 175 len_seen = 0; 176 for (i = 0; i < ext_sg_entries; i++) { 177 if ((len_seen + ext_sglist[i].len) >= 178 ctsio->ext_data_filled) { 179 ext_sg_start = i; 180 ext_offset = ctsio->ext_data_filled - len_seen; 181 break; 182 } 183 len_seen += ext_sglist[i].len; 184 } 185 } else { 186 ext_sglist = &ext_entry; 187 ext_sglist_malloced = 0; 188 ext_sglist->addr = ctsio->ext_data_ptr; 189 ext_sglist->len = ctsio->ext_data_len; 190 ext_sg_entries = 1; 191 ext_sg_start = 0; 192 ext_offset = ctsio->ext_data_filled; 193 } 194 195 if (ctsio->kern_sg_entries > 0) { 196 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 197 kern_sg_entries = ctsio->kern_sg_entries; 198 } else { 199 kern_sglist = &kern_entry; 200 kern_sglist->addr = ctsio->kern_data_ptr; 201 kern_sglist->len = ctsio->kern_data_len; 202 kern_sg_entries = 1; 203 } 204 205 kern_watermark = 0; 206 ext_watermark = ext_offset; 207 len_copied = 0; 208 for (i = ext_sg_start, j = 0; 209 i < ext_sg_entries && j < kern_sg_entries;) { 210 uint8_t *ext_ptr, *kern_ptr; 211 212 len_to_copy = MIN(ext_sglist[i].len - ext_watermark, 213 kern_sglist[j].len - kern_watermark); 214 215 ext_ptr = (uint8_t *)ext_sglist[i].addr; 216 ext_ptr = ext_ptr + ext_watermark; 217 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 218 /* 219 * XXX KDM fix this! 220 */ 221 panic("need to implement bus address support"); 222#if 0 223 kern_ptr = bus_to_virt(kern_sglist[j].addr); 224#endif 225 } else 226 kern_ptr = (uint8_t *)kern_sglist[j].addr; 227 kern_ptr = kern_ptr + kern_watermark; 228 229 kern_watermark += len_to_copy; 230 ext_watermark += len_to_copy; 231 232 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 233 CTL_FLAG_DATA_IN) { 234 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 235 "bytes to user\n", len_to_copy)); 236 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 237 "to %p\n", kern_ptr, ext_ptr)); 238 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 239 ctsio->io_hdr.port_status = 31344; 240 goto bailout; 241 } 242 } else { 243 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 244 "bytes from user\n", len_to_copy)); 245 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 246 "to %p\n", ext_ptr, kern_ptr)); 247 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 248 ctsio->io_hdr.port_status = 31345; 249 goto bailout; 250 } 251 } 252 253 len_copied += len_to_copy; 254 255 if (ext_sglist[i].len == ext_watermark) { 256 i++; 257 ext_watermark = 0; 258 } 259 260 if (kern_sglist[j].len == kern_watermark) { 261 j++; 262 kern_watermark = 0; 263 } 264 } 265 266 ctsio->ext_data_filled += len_copied; 267 268 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 269 "kern_sg_entries: %d\n", ext_sg_entries, 270 kern_sg_entries)); 271 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 272 "kern_data_len = %d\n", ctsio->ext_data_len, 273 ctsio->kern_data_len)); 274 275 /* 276 * Report write underflow as error, since CTL and backends don't 277 * really support it. 278 */ 279 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT && 280 j < kern_sg_entries) { 281 ctsio->io_hdr.port_status = 43; 282 } 283 284bailout: 285 if (ext_sglist_malloced != 0) 286 free(ext_sglist, M_CTL); 287 288 return (CTL_RETVAL_COMPLETE); 289} 290 291static void 292cfi_datamove(union ctl_io *io) 293{ 294 struct ctl_fe_ioctl_params *params; 295 296 params = (struct ctl_fe_ioctl_params *) 297 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 298 299 mtx_lock(¶ms->ioctl_mtx); 300 params->state = CTL_IOCTL_DATAMOVE; 301 cv_broadcast(¶ms->sem); 302 mtx_unlock(¶ms->ioctl_mtx); 303} 304 305static void 306cfi_done(union ctl_io *io) 307{ 308 struct ctl_fe_ioctl_params *params; 309 310 params = (struct ctl_fe_ioctl_params *) 311 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 312 313 mtx_lock(¶ms->ioctl_mtx); 314 params->state = CTL_IOCTL_DONE; 315 cv_broadcast(¶ms->sem); 316 mtx_unlock(¶ms->ioctl_mtx); 317} 318 319static int 320cfi_submit_wait(union ctl_io *io) 321{ 322 struct ctl_fe_ioctl_params params; 323 ctl_fe_ioctl_state last_state; 324 int done, retval; 325 326 bzero(¶ms, sizeof(params)); 327 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 328 cv_init(¶ms.sem, "ctlioccv"); 329 params.state = CTL_IOCTL_INPROG; 330 last_state = params.state; 331 332 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 333 334 CTL_DEBUG_PRINT(("cfi_submit_wait\n")); 335 336 /* This shouldn't happen */ 337 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 338 return (retval); 339 340 done = 0; 341 342 do { 343 mtx_lock(¶ms.ioctl_mtx); 344 /* 345 * Check the state here, and don't sleep if the state has 346 * already changed (i.e. wakeup has already occurred, but we 347 * weren't waiting yet). 348 */ 349 if (params.state == last_state) { 350 /* XXX KDM cv_wait_sig instead? */ 351 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 352 } 353 last_state = params.state; 354 355 switch (params.state) { 356 case CTL_IOCTL_INPROG: 357 /* Why did we wake up? */ 358 /* XXX KDM error here? */ 359 mtx_unlock(¶ms.ioctl_mtx); 360 break; 361 case CTL_IOCTL_DATAMOVE: 362 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 363 364 /* 365 * change last_state back to INPROG to avoid 366 * deadlock on subsequent data moves. 367 */ 368 params.state = last_state = CTL_IOCTL_INPROG; 369 370 mtx_unlock(¶ms.ioctl_mtx); 371 ctl_ioctl_do_datamove(&io->scsiio); 372 /* 373 * Note that in some cases, most notably writes, 374 * this will queue the I/O and call us back later. 375 * In other cases, generally reads, this routine 376 * will immediately call back and wake us up, 377 * probably using our own context. 378 */ 379 io->scsiio.be_move_done(io); 380 break; 381 case CTL_IOCTL_DONE: 382 mtx_unlock(¶ms.ioctl_mtx); 383 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 384 done = 1; 385 break; 386 default: 387 mtx_unlock(¶ms.ioctl_mtx); 388 /* XXX KDM error here? */ 389 break; 390 } 391 } while (done == 0); 392 393 mtx_destroy(¶ms.ioctl_mtx); 394 cv_destroy(¶ms.sem); 395 396 return (CTL_RETVAL_COMPLETE); 397} 398 399int 400ctl_ioctl_io(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 401 struct thread *td) 402{ 403 union ctl_io *io; 404 void *pool_tmp, *sc_tmp; 405 int retval = 0; 406 407 /* 408 * If we haven't been "enabled", don't allow any SCSI I/O 409 * to this FETD. 410 */ 411 if ((cfi_softc.port.status & CTL_PORT_STATUS_ONLINE) == 0) 412 return (EPERM); 413 414 io = ctl_alloc_io(cfi_softc.port.ctl_pool_ref); 415 416 /* 417 * Need to save the pool reference so it doesn't get 418 * spammed by the user's ctl_io. 419 */ 420 pool_tmp = io->io_hdr.pool; 421 sc_tmp = CTL_SOFTC(io); 422 memcpy(io, (void *)addr, sizeof(*io)); 423 io->io_hdr.pool = pool_tmp; 424 CTL_SOFTC(io) = sc_tmp; 425 426 /* 427 * No status yet, so make sure the status is set properly. 428 */ 429 io->io_hdr.status = CTL_STATUS_NONE; 430 431 /* 432 * The user sets the initiator ID, target and LUN IDs. 433 */ 434 io->io_hdr.nexus.targ_port = cfi_softc.port.targ_port; 435 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 436 if ((io->io_hdr.io_type == CTL_IO_SCSI) && 437 (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 438 io->scsiio.tag_num = cfi_softc.cur_tag_num++; 439 440 retval = cfi_submit_wait(io); 441 if (retval == 0) 442 memcpy((void *)addr, io, sizeof(*io)); 443 ctl_free_io(io); 444 return (retval); 445} 446