2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
5 * Copyright (c) 2012 The FreeBSD Foundation
6 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org>
7 * Copyright (c) 2017 Jakub Wojciech Klama <jceel@FreeBSD.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/types.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/malloc.h>
45 #include <sys/queue.h>
46 #include <sys/sysctl.h>
51 #include <cam/scsi/scsi_all.h>
52 #include <cam/scsi/scsi_da.h>
53 #include <cam/ctl/ctl_io.h>
54 #include <cam/ctl/ctl.h>
55 #include <cam/ctl/ctl_frontend.h>
56 #include <cam/ctl/ctl_util.h>
57 #include <cam/ctl/ctl_backend.h>
58 #include <cam/ctl/ctl_ioctl.h>
59 #include <cam/ctl/ctl_ha.h>
60 #include <cam/ctl/ctl_private.h>
61 #include <cam/ctl/ctl_debug.h>
62 #include <cam/ctl/ctl_error.h>
70 struct ctl_fe_ioctl_params {
73 ctl_fe_ioctl_state state;
77 TAILQ_ENTRY(cfi_port) link;
84 TAILQ_HEAD(, cfi_port) ports;
88 static struct cfi_softc cfi_softc;
91 static int cfi_init(void);
92 static int cfi_shutdown(void);
93 static void cfi_datamove(union ctl_io *io);
94 static void cfi_done(union ctl_io *io);
95 static int cfi_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
97 static void cfi_ioctl_port_create(struct ctl_req *req);
98 static void cfi_ioctl_port_remove(struct ctl_req *req);
100 static struct cdevsw cfi_cdevsw = {
101 .d_version = D_VERSION,
103 .d_ioctl = ctl_ioctl_io
106 static struct ctl_frontend cfi_frontend =
111 .shutdown = cfi_shutdown,
113 CTL_FRONTEND_DECLARE(ctlioctl, cfi_frontend);
118 struct cfi_softc *isoftc = &cfi_softc;
119 struct cfi_port *cfi;
120 struct ctl_port *port;
123 memset(isoftc, 0, sizeof(*isoftc));
124 TAILQ_INIT(&isoftc->ports);
126 cfi = malloc(sizeof(*cfi), M_CTL, M_WAITOK | M_ZERO);
128 port->frontend = &cfi_frontend;
129 port->port_type = CTL_PORT_IOCTL;
130 port->num_requested_ctl_io = 100;
131 port->port_name = "ioctl";
132 port->fe_datamove = cfi_datamove;
133 port->fe_done = cfi_done;
134 port->physical_port = 0;
135 port->targ_port = -1;
137 if ((error = ctl_port_register(port)) != 0) {
138 printf("%s: ioctl port registration failed\n", __func__);
142 ctl_port_online(port);
143 TAILQ_INSERT_TAIL(&isoftc->ports, cfi, link);
150 struct cfi_softc *isoftc = &cfi_softc;
151 struct cfi_port *cfi, *temp;
152 struct ctl_port *port;
155 TAILQ_FOREACH_SAFE(cfi, &isoftc->ports, link, temp) {
157 ctl_port_offline(port);
158 error = ctl_port_deregister(port);
160 printf("%s: ctl_frontend_deregister() failed\n",
165 TAILQ_REMOVE(&isoftc->ports, cfi, link);
173 cfi_ioctl_port_create(struct ctl_req *req)
175 struct cfi_softc *isoftc = &cfi_softc;
176 struct cfi_port *cfi;
177 struct ctl_port *port;
178 struct make_dev_args args;
183 val = dnvlist_get_string(req->args_nvl, "pp", NULL);
185 pp = strtol(val, NULL, 10);
187 val = dnvlist_get_string(req->args_nvl, "vp", NULL);
189 vp = strtol(val, NULL, 10);
192 /* Check for duplicates */
193 TAILQ_FOREACH(cfi, &isoftc->ports, link) {
194 if (pp == cfi->port.physical_port &&
195 vp == cfi->port.virtual_port) {
196 req->status = CTL_LUN_ERROR;
197 snprintf(req->error_str, sizeof(req->error_str),
198 "port %d already exists", pp);
204 /* Find free port number */
205 TAILQ_FOREACH(cfi, &isoftc->ports, link) {
206 pp = MAX(pp, cfi->port.physical_port);
212 cfi = malloc(sizeof(*cfi), M_CTL, M_WAITOK | M_ZERO);
214 port->frontend = &cfi_frontend;
215 port->port_type = CTL_PORT_IOCTL;
216 port->num_requested_ctl_io = 100;
217 port->port_name = "ioctl";
218 port->fe_datamove = cfi_datamove;
219 port->fe_done = cfi_done;
220 port->physical_port = pp;
221 port->virtual_port = vp;
222 port->targ_port = -1;
224 retval = ctl_port_register(port);
226 req->status = CTL_LUN_ERROR;
227 snprintf(req->error_str, sizeof(req->error_str),
228 "ctl_port_register() failed with error %d", retval);
233 req->result_nvl = nvlist_create(0);
234 nvlist_add_number(req->result_nvl, "port_id", port->targ_port);
235 ctl_port_online(port);
237 make_dev_args_init(&args);
238 args.mda_devsw = &cfi_cdevsw;
239 args.mda_uid = UID_ROOT;
240 args.mda_gid = GID_OPERATOR;
241 args.mda_mode = 0600;
242 args.mda_si_drv1 = NULL;
243 args.mda_si_drv2 = cfi;
245 retval = make_dev_s(&args, &cfi->dev, "cam/ctl%d.%d", pp, vp);
247 req->status = CTL_LUN_ERROR;
248 snprintf(req->error_str, sizeof(req->error_str),
249 "make_dev_s() failed with error %d", retval);
254 req->status = CTL_LUN_OK;
255 TAILQ_INSERT_TAIL(&isoftc->ports, cfi, link);
259 cfi_ioctl_port_remove(struct ctl_req *req)
261 struct cfi_softc *isoftc = &cfi_softc;
262 struct cfi_port *cfi = NULL;
266 val = dnvlist_get_string(req->args_nvl, "port_id", NULL);
268 port_id = strtol(val, NULL, 10);
271 req->status = CTL_LUN_ERROR;
272 snprintf(req->error_str, sizeof(req->error_str),
273 "port_id not provided");
277 TAILQ_FOREACH(cfi, &isoftc->ports, link) {
278 if (cfi->port.targ_port == port_id)
283 req->status = CTL_LUN_ERROR;
284 snprintf(req->error_str, sizeof(req->error_str),
285 "cannot find port %d", port_id);
290 if (cfi->port.physical_port == 0 && cfi->port.virtual_port == 0) {
291 req->status = CTL_LUN_ERROR;
292 snprintf(req->error_str, sizeof(req->error_str),
293 "cannot destroy default ioctl port");
298 ctl_port_offline(&cfi->port);
299 ctl_port_deregister(&cfi->port);
300 TAILQ_REMOVE(&isoftc->ports, cfi, link);
301 destroy_dev(cfi->dev);
303 req->status = CTL_LUN_OK;
307 cfi_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
312 if (cmd == CTL_PORT_REQ) {
313 req = (struct ctl_req *)addr;
314 switch (req->reqtype) {
316 cfi_ioctl_port_create(req);
319 cfi_ioctl_port_remove(req);
322 req->status = CTL_LUN_ERROR;
323 snprintf(req->error_str, sizeof(req->error_str),
324 "Unsupported request type %d", req->reqtype);
333 * Data movement routine for the CTL ioctl frontend port.
336 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
338 struct ctl_sg_entry *ext_sglist, *kern_sglist;
339 struct ctl_sg_entry ext_entry, kern_entry;
340 int ext_sglen, ext_sg_entries, kern_sg_entries;
341 int ext_sg_start, ext_offset;
343 int kern_watermark, ext_watermark;
344 int ext_sglist_malloced;
347 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
350 * If this flag is set, fake the data transfer.
352 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
353 ext_sglist_malloced = 0;
354 ctsio->ext_data_filled += ctsio->kern_data_len;
355 ctsio->kern_data_resid = 0;
360 * To simplify things here, if we have a single buffer, stick it in
361 * a S/G entry and just make it a single entry S/G list.
363 if (ctsio->ext_sg_entries > 0) {
366 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
367 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
369 ext_sglist_malloced = 1;
370 if (copyin(ctsio->ext_data_ptr, ext_sglist, ext_sglen) != 0) {
371 ctsio->io_hdr.port_status = 31343;
374 ext_sg_entries = ctsio->ext_sg_entries;
375 ext_sg_start = ext_sg_entries;
378 for (i = 0; i < ext_sg_entries; i++) {
379 if ((len_seen + ext_sglist[i].len) >=
380 ctsio->ext_data_filled) {
382 ext_offset = ctsio->ext_data_filled - len_seen;
385 len_seen += ext_sglist[i].len;
388 ext_sglist = &ext_entry;
389 ext_sglist_malloced = 0;
390 ext_sglist->addr = ctsio->ext_data_ptr;
391 ext_sglist->len = ctsio->ext_data_len;
394 ext_offset = ctsio->ext_data_filled;
397 if (ctsio->kern_sg_entries > 0) {
398 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
399 kern_sg_entries = ctsio->kern_sg_entries;
401 kern_sglist = &kern_entry;
402 kern_sglist->addr = ctsio->kern_data_ptr;
403 kern_sglist->len = ctsio->kern_data_len;
408 ext_watermark = ext_offset;
409 for (i = ext_sg_start, j = 0;
410 i < ext_sg_entries && j < kern_sg_entries;) {
411 uint8_t *ext_ptr, *kern_ptr;
413 len_to_copy = MIN(ext_sglist[i].len - ext_watermark,
414 kern_sglist[j].len - kern_watermark);
416 ext_ptr = (uint8_t *)ext_sglist[i].addr;
417 ext_ptr = ext_ptr + ext_watermark;
418 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
422 panic("need to implement bus address support");
424 kern_ptr = bus_to_virt(kern_sglist[j].addr);
427 kern_ptr = (uint8_t *)kern_sglist[j].addr;
428 kern_ptr = kern_ptr + kern_watermark;
430 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
432 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
433 "bytes to user\n", len_to_copy));
434 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
435 "to %p\n", kern_ptr, ext_ptr));
436 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
437 ctsio->io_hdr.port_status = 31344;
441 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
442 "bytes from user\n", len_to_copy));
443 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
444 "to %p\n", ext_ptr, kern_ptr));
445 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
446 ctsio->io_hdr.port_status = 31345;
451 ctsio->ext_data_filled += len_to_copy;
452 ctsio->kern_data_resid -= len_to_copy;
454 ext_watermark += len_to_copy;
455 if (ext_sglist[i].len == ext_watermark) {
460 kern_watermark += len_to_copy;
461 if (kern_sglist[j].len == kern_watermark) {
467 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
468 "kern_sg_entries: %d\n", ext_sg_entries,
470 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
471 "kern_data_len = %d\n", ctsio->ext_data_len,
472 ctsio->kern_data_len));
475 if (ext_sglist_malloced != 0)
476 free(ext_sglist, M_CTL);
478 return (CTL_RETVAL_COMPLETE);
482 cfi_datamove(union ctl_io *io)
484 struct ctl_fe_ioctl_params *params;
486 params = (struct ctl_fe_ioctl_params *)
487 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
489 mtx_lock(¶ms->ioctl_mtx);
490 params->state = CTL_IOCTL_DATAMOVE;
491 cv_broadcast(¶ms->sem);
492 mtx_unlock(¶ms->ioctl_mtx);
496 cfi_done(union ctl_io *io)
498 struct ctl_fe_ioctl_params *params;
500 params = (struct ctl_fe_ioctl_params *)
501 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
503 mtx_lock(¶ms->ioctl_mtx);
504 params->state = CTL_IOCTL_DONE;
505 cv_broadcast(¶ms->sem);
506 mtx_unlock(¶ms->ioctl_mtx);
510 cfi_submit_wait(union ctl_io *io)
512 struct ctl_fe_ioctl_params params;
513 ctl_fe_ioctl_state last_state;
516 bzero(¶ms, sizeof(params));
517 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
518 cv_init(¶ms.sem, "ctlioccv");
519 params.state = CTL_IOCTL_INPROG;
520 last_state = params.state;
522 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
524 CTL_DEBUG_PRINT(("cfi_submit_wait\n"));
526 /* This shouldn't happen */
527 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
533 mtx_lock(¶ms.ioctl_mtx);
535 * Check the state here, and don't sleep if the state has
536 * already changed (i.e. wakeup has already occurred, but we
537 * weren't waiting yet).
539 if (params.state == last_state) {
540 /* XXX KDM cv_wait_sig instead? */
541 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
543 last_state = params.state;
545 switch (params.state) {
546 case CTL_IOCTL_INPROG:
547 /* Why did we wake up? */
548 /* XXX KDM error here? */
549 mtx_unlock(¶ms.ioctl_mtx);
551 case CTL_IOCTL_DATAMOVE:
552 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
555 * change last_state back to INPROG to avoid
556 * deadlock on subsequent data moves.
558 params.state = last_state = CTL_IOCTL_INPROG;
560 mtx_unlock(¶ms.ioctl_mtx);
561 ctl_ioctl_do_datamove(&io->scsiio);
563 * Note that in some cases, most notably writes,
564 * this will queue the I/O and call us back later.
565 * In other cases, generally reads, this routine
566 * will immediately call back and wake us up,
567 * probably using our own context.
569 io->scsiio.be_move_done(io);
572 mtx_unlock(¶ms.ioctl_mtx);
573 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
577 mtx_unlock(¶ms.ioctl_mtx);
578 /* XXX KDM error here? */
583 mtx_destroy(¶ms.ioctl_mtx);
584 cv_destroy(¶ms.sem);
586 return (CTL_RETVAL_COMPLETE);
590 ctl_ioctl_io(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
593 struct cfi_port *cfi;
595 void *pool_tmp, *sc_tmp;
601 cfi = dev->si_drv2 == NULL
602 ? TAILQ_FIRST(&cfi_softc.ports)
606 * If we haven't been "enabled", don't allow any SCSI I/O
609 if ((cfi->port.status & CTL_PORT_STATUS_ONLINE) == 0)
612 io = ctl_alloc_io(cfi->port.ctl_pool_ref);
615 * Need to save the pool reference so it doesn't get
616 * spammed by the user's ctl_io.
618 pool_tmp = io->io_hdr.pool;
619 sc_tmp = CTL_SOFTC(io);
620 memcpy(io, (void *)addr, sizeof(*io));
621 io->io_hdr.pool = pool_tmp;
622 CTL_SOFTC(io) = sc_tmp;
623 TAILQ_INIT(&io->io_hdr.blocked_queue);
626 * No status yet, so make sure the status is set properly.
628 io->io_hdr.status = CTL_STATUS_NONE;
631 * The user sets the initiator ID, target and LUN IDs.
633 io->io_hdr.nexus.targ_port = cfi->port.targ_port;
634 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
635 if ((io->io_hdr.io_type == CTL_IO_SCSI) &&
636 (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
637 io->scsiio.tag_num = cfi->cur_tag_num++;
639 retval = cfi_submit_wait(io);
641 memcpy((void *)addr, io, sizeof(*io));