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$"); 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 59 struct cfi_softc { 60 uint32_t cur_tag_num; 61 struct ctl_port port; 62 }; 63 64 static struct cfi_softc cfi_softc; 65 66 static int cfi_init(void); 67 static void cfi_shutdown(void); 68 static void cfi_datamove(union ctl_io *io); 69 static void cfi_done(union ctl_io *io); 70 71 static struct ctl_frontend cfi_frontend = 72 { 73 .name = "ioctl", 74 .init = cfi_init, 75 .shutdown = cfi_shutdown, 76 }; 77 CTL_FRONTEND_DECLARE(ctlioctl, cfi_frontend); 78 79 static int 80 cfi_init(void) 81 { 82 struct cfi_softc *isoftc = &cfi_softc; 83 struct ctl_port *port; 84 85 memset(isoftc, 0, sizeof(*isoftc)); 86 87 port = &isoftc->port; 88 port->frontend = &cfi_frontend; 89 port->port_type = CTL_PORT_IOCTL; 90 port->num_requested_ctl_io = 100; 91 port->port_name = "ioctl"; 92 port->fe_datamove = cfi_datamove; 93 port->fe_done = cfi_done; 94 port->max_targets = 1; 95 port->max_target_id = 0; 96 port->max_initiators = 1; 97 98 if (ctl_port_register(port) != 0) { 99 printf("%s: ioctl port registration failed\n", __func__); 100 return (0); 101 } 102 ctl_port_online(port); 103 return (0); 104 } 105 106 void 107 cfi_shutdown(void) 108 { 109 struct cfi_softc *isoftc = &cfi_softc; 110 struct ctl_port *port; 111 112 port = &isoftc->port; 113 ctl_port_offline(port); 114 if (ctl_port_deregister(&isoftc->port) != 0) 115 printf("%s: ctl_frontend_deregister() failed\n", __func__); 116 } 117 118 /* 119 * Data movement routine for the CTL ioctl frontend port. 120 */ 121 static int 122 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 123 { 124 struct ctl_sg_entry *ext_sglist, *kern_sglist; 125 struct ctl_sg_entry ext_entry, kern_entry; 126 int ext_sglen, ext_sg_entries, kern_sg_entries; 127 int ext_sg_start, ext_offset; 128 int len_to_copy, len_copied; 129 int kern_watermark, ext_watermark; 130 int ext_sglist_malloced; 131 int i, j; 132 133 ext_sglist_malloced = 0; 134 ext_sg_start = 0; 135 ext_offset = 0; 136 137 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 138 139 /* 140 * If this flag is set, fake the data transfer. 141 */ 142 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 143 ctsio->ext_data_filled = ctsio->ext_data_len; 144 goto bailout; 145 } 146 147 /* 148 * To simplify things here, if we have a single buffer, stick it in 149 * a S/G entry and just make it a single entry S/G list. 150 */ 151 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 152 int len_seen; 153 154 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 155 156 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 157 M_WAITOK); 158 ext_sglist_malloced = 1; 159 if (copyin(ctsio->ext_data_ptr, ext_sglist, 160 ext_sglen) != 0) { 161 ctl_set_internal_failure(ctsio, 162 /*sks_valid*/ 0, 163 /*retry_count*/ 0); 164 goto bailout; 165 } 166 ext_sg_entries = ctsio->ext_sg_entries; 167 len_seen = 0; 168 for (i = 0; i < ext_sg_entries; i++) { 169 if ((len_seen + ext_sglist[i].len) >= 170 ctsio->ext_data_filled) { 171 ext_sg_start = i; 172 ext_offset = ctsio->ext_data_filled - len_seen; 173 break; 174 } 175 len_seen += ext_sglist[i].len; 176 } 177 } else { 178 ext_sglist = &ext_entry; 179 ext_sglist->addr = ctsio->ext_data_ptr; 180 ext_sglist->len = ctsio->ext_data_len; 181 ext_sg_entries = 1; 182 ext_sg_start = 0; 183 ext_offset = ctsio->ext_data_filled; 184 } 185 186 if (ctsio->kern_sg_entries > 0) { 187 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 188 kern_sg_entries = ctsio->kern_sg_entries; 189 } else { 190 kern_sglist = &kern_entry; 191 kern_sglist->addr = ctsio->kern_data_ptr; 192 kern_sglist->len = ctsio->kern_data_len; 193 kern_sg_entries = 1; 194 } 195 196 197 kern_watermark = 0; 198 ext_watermark = ext_offset; 199 len_copied = 0; 200 for (i = ext_sg_start, j = 0; 201 i < ext_sg_entries && j < kern_sg_entries;) { 202 uint8_t *ext_ptr, *kern_ptr; 203 204 len_to_copy = MIN(ext_sglist[i].len - ext_watermark, 205 kern_sglist[j].len - kern_watermark); 206 207 ext_ptr = (uint8_t *)ext_sglist[i].addr; 208 ext_ptr = ext_ptr + ext_watermark; 209 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 210 /* 211 * XXX KDM fix this! 212 */ 213 panic("need to implement bus address support"); 214 #if 0 215 kern_ptr = bus_to_virt(kern_sglist[j].addr); 216 #endif 217 } else 218 kern_ptr = (uint8_t *)kern_sglist[j].addr; 219 kern_ptr = kern_ptr + kern_watermark; 220 221 kern_watermark += len_to_copy; 222 ext_watermark += len_to_copy; 223 224 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 225 CTL_FLAG_DATA_IN) { 226 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 227 "bytes to user\n", len_to_copy)); 228 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 229 "to %p\n", kern_ptr, ext_ptr)); 230 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 231 ctl_set_internal_failure(ctsio, 232 /*sks_valid*/ 0, 233 /*retry_count*/ 0); 234 goto bailout; 235 } 236 } else { 237 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 238 "bytes from user\n", len_to_copy)); 239 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 240 "to %p\n", ext_ptr, kern_ptr)); 241 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 242 ctl_set_internal_failure(ctsio, 243 /*sks_valid*/ 0, 244 /*retry_count*/0); 245 goto bailout; 246 } 247 } 248 249 len_copied += len_to_copy; 250 251 if (ext_sglist[i].len == ext_watermark) { 252 i++; 253 ext_watermark = 0; 254 } 255 256 if (kern_sglist[j].len == kern_watermark) { 257 j++; 258 kern_watermark = 0; 259 } 260 } 261 262 ctsio->ext_data_filled += len_copied; 263 264 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 265 "kern_sg_entries: %d\n", ext_sg_entries, 266 kern_sg_entries)); 267 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 268 "kern_data_len = %d\n", ctsio->ext_data_len, 269 ctsio->kern_data_len)); 270 271 272 /* XXX KDM set residual?? */ 273 bailout: 274 275 if (ext_sglist_malloced != 0) 276 free(ext_sglist, M_CTL); 277 278 return (CTL_RETVAL_COMPLETE); 279 } 280 281 static void 282 cfi_datamove(union ctl_io *io) 283 { 284 struct ctl_fe_ioctl_params *params; 285 286 params = (struct ctl_fe_ioctl_params *) 287 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 288 289 mtx_lock(¶ms->ioctl_mtx); 290 params->state = CTL_IOCTL_DATAMOVE; 291 cv_broadcast(¶ms->sem); 292 mtx_unlock(¶ms->ioctl_mtx); 293 } 294 295 static void 296 cfi_done(union ctl_io *io) 297 { 298 struct ctl_fe_ioctl_params *params; 299 300 params = (struct ctl_fe_ioctl_params *) 301 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 302 303 mtx_lock(¶ms->ioctl_mtx); 304 params->state = CTL_IOCTL_DONE; 305 cv_broadcast(¶ms->sem); 306 mtx_unlock(¶ms->ioctl_mtx); 307 } 308 309 static int 310 cfi_submit_wait(union ctl_io *io) 311 { 312 struct ctl_fe_ioctl_params params; 313 ctl_fe_ioctl_state last_state; 314 int done, retval; 315 316 retval = 0; 317 318 bzero(¶ms, sizeof(params)); 319 320 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 321 cv_init(¶ms.sem, "ctlioccv"); 322 params.state = CTL_IOCTL_INPROG; 323 last_state = params.state; 324 325 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 326 327 CTL_DEBUG_PRINT(("cfi_submit_wait\n")); 328 329 /* This shouldn't happen */ 330 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 331 return (retval); 332 333 done = 0; 334 335 do { 336 mtx_lock(¶ms.ioctl_mtx); 337 /* 338 * Check the state here, and don't sleep if the state has 339 * already changed (i.e. wakeup has already occured, but we 340 * weren't waiting yet). 341 */ 342 if (params.state == last_state) { 343 /* XXX KDM cv_wait_sig instead? */ 344 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 345 } 346 last_state = params.state; 347 348 switch (params.state) { 349 case CTL_IOCTL_INPROG: 350 /* Why did we wake up? */ 351 /* XXX KDM error here? */ 352 mtx_unlock(¶ms.ioctl_mtx); 353 break; 354 case CTL_IOCTL_DATAMOVE: 355 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 356 357 /* 358 * change last_state back to INPROG to avoid 359 * deadlock on subsequent data moves. 360 */ 361 params.state = last_state = CTL_IOCTL_INPROG; 362 363 mtx_unlock(¶ms.ioctl_mtx); 364 ctl_ioctl_do_datamove(&io->scsiio); 365 /* 366 * Note that in some cases, most notably writes, 367 * this will queue the I/O and call us back later. 368 * In other cases, generally reads, this routine 369 * will immediately call back and wake us up, 370 * probably using our own context. 371 */ 372 io->scsiio.be_move_done(io); 373 break; 374 case CTL_IOCTL_DONE: 375 mtx_unlock(¶ms.ioctl_mtx); 376 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 377 done = 1; 378 break; 379 default: 380 mtx_unlock(¶ms.ioctl_mtx); 381 /* XXX KDM error here? */ 382 break; 383 } 384 } while (done == 0); 385 386 mtx_destroy(¶ms.ioctl_mtx); 387 cv_destroy(¶ms.sem); 388 389 return (CTL_RETVAL_COMPLETE); 390 } 391 392 int 393 ctl_ioctl_io(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 394 struct thread *td) 395 { 396 union ctl_io *io; 397 void *pool_tmp; 398 int retval = 0; 399 400 /* 401 * If we haven't been "enabled", don't allow any SCSI I/O 402 * to this FETD. 403 */ 404 if ((cfi_softc.port.status & CTL_PORT_STATUS_ONLINE) == 0) 405 return (EPERM); 406 407 io = ctl_alloc_io(cfi_softc.port.ctl_pool_ref); 408 409 /* 410 * Need to save the pool reference so it doesn't get 411 * spammed by the user's ctl_io. 412 */ 413 pool_tmp = io->io_hdr.pool; 414 memcpy(io, (void *)addr, sizeof(*io)); 415 io->io_hdr.pool = pool_tmp; 416 417 /* 418 * No status yet, so make sure the status is set properly. 419 */ 420 io->io_hdr.status = CTL_STATUS_NONE; 421 422 /* 423 * The user sets the initiator ID, target and LUN IDs. 424 */ 425 io->io_hdr.nexus.targ_port = cfi_softc.port.targ_port; 426 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 427 if ((io->io_hdr.io_type == CTL_IO_SCSI) && 428 (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 429 io->scsiio.tag_num = cfi_softc.cur_tag_num++; 430 431 retval = cfi_submit_wait(io); 432 if (retval == 0) 433 memcpy((void *)addr, io, sizeof(*io)); 434 ctl_free_io(io); 435 return (retval); 436 } 437