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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/types.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/malloc.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.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>
65 struct ctl_fe_ioctl_params {
68 ctl_fe_ioctl_state state;
76 static struct cfi_softc cfi_softc;
78 static int cfi_init(void);
79 static int cfi_shutdown(void);
80 static void cfi_datamove(union ctl_io *io);
81 static void cfi_done(union ctl_io *io);
83 static struct ctl_frontend cfi_frontend =
87 .shutdown = cfi_shutdown,
89 CTL_FRONTEND_DECLARE(ctlioctl, cfi_frontend);
94 struct cfi_softc *isoftc = &cfi_softc;
95 struct ctl_port *port;
98 memset(isoftc, 0, sizeof(*isoftc));
100 port = &isoftc->port;
101 port->frontend = &cfi_frontend;
102 port->port_type = CTL_PORT_IOCTL;
103 port->num_requested_ctl_io = 100;
104 port->port_name = "ioctl";
105 port->fe_datamove = cfi_datamove;
106 port->fe_done = cfi_done;
107 port->targ_port = -1;
108 port->max_initiators = 1;
110 if ((error = ctl_port_register(port)) != 0) {
111 printf("%s: ioctl port registration failed\n", __func__);
114 ctl_port_online(port);
121 struct cfi_softc *isoftc = &cfi_softc;
122 struct ctl_port *port = &isoftc->port;
125 ctl_port_offline(port);
126 if ((error = ctl_port_deregister(port)) != 0)
127 printf("%s: ioctl port deregistration failed\n", __func__);
132 * Data movement routine for the CTL ioctl frontend port.
135 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
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;
142 int kern_watermark, ext_watermark;
143 int ext_sglist_malloced;
146 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
149 * If this flag is set, fake the data transfer.
151 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
152 ext_sglist_malloced = 0;
153 ctsio->ext_data_filled += ctsio->kern_data_len;
154 ctsio->kern_data_resid = 0;
159 * To simplify things here, if we have a single buffer, stick it in
160 * a S/G entry and just make it a single entry S/G list.
162 if (ctsio->ext_sg_entries > 0) {
165 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
166 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
168 ext_sglist_malloced = 1;
169 if (copyin(ctsio->ext_data_ptr, ext_sglist, ext_sglen) != 0) {
170 ctsio->io_hdr.port_status = 31343;
173 ext_sg_entries = ctsio->ext_sg_entries;
174 ext_sg_start = ext_sg_entries;
177 for (i = 0; i < ext_sg_entries; i++) {
178 if ((len_seen + ext_sglist[i].len) >=
179 ctsio->ext_data_filled) {
181 ext_offset = ctsio->ext_data_filled - len_seen;
184 len_seen += ext_sglist[i].len;
187 ext_sglist = &ext_entry;
188 ext_sglist_malloced = 0;
189 ext_sglist->addr = ctsio->ext_data_ptr;
190 ext_sglist->len = ctsio->ext_data_len;
193 ext_offset = ctsio->ext_data_filled;
196 if (ctsio->kern_sg_entries > 0) {
197 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
198 kern_sg_entries = ctsio->kern_sg_entries;
200 kern_sglist = &kern_entry;
201 kern_sglist->addr = ctsio->kern_data_ptr;
202 kern_sglist->len = ctsio->kern_data_len;
207 ext_watermark = ext_offset;
208 for (i = ext_sg_start, j = 0;
209 i < ext_sg_entries && j < kern_sg_entries;) {
210 uint8_t *ext_ptr, *kern_ptr;
212 len_to_copy = MIN(ext_sglist[i].len - ext_watermark,
213 kern_sglist[j].len - kern_watermark);
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) {
221 panic("need to implement bus address support");
223 kern_ptr = bus_to_virt(kern_sglist[j].addr);
226 kern_ptr = (uint8_t *)kern_sglist[j].addr;
227 kern_ptr = kern_ptr + kern_watermark;
229 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
231 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
232 "bytes to user\n", len_to_copy));
233 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
234 "to %p\n", kern_ptr, ext_ptr));
235 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
236 ctsio->io_hdr.port_status = 31344;
240 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
241 "bytes from user\n", len_to_copy));
242 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
243 "to %p\n", ext_ptr, kern_ptr));
244 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
245 ctsio->io_hdr.port_status = 31345;
250 ctsio->ext_data_filled += len_to_copy;
251 ctsio->kern_data_resid -= len_to_copy;
253 ext_watermark += len_to_copy;
254 if (ext_sglist[i].len == ext_watermark) {
259 kern_watermark += len_to_copy;
260 if (kern_sglist[j].len == kern_watermark) {
266 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
267 "kern_sg_entries: %d\n", ext_sg_entries,
269 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
270 "kern_data_len = %d\n", ctsio->ext_data_len,
271 ctsio->kern_data_len));
274 if (ext_sglist_malloced != 0)
275 free(ext_sglist, M_CTL);
277 return (CTL_RETVAL_COMPLETE);
281 cfi_datamove(union ctl_io *io)
283 struct ctl_fe_ioctl_params *params;
285 params = (struct ctl_fe_ioctl_params *)
286 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
288 mtx_lock(¶ms->ioctl_mtx);
289 params->state = CTL_IOCTL_DATAMOVE;
290 cv_broadcast(¶ms->sem);
291 mtx_unlock(¶ms->ioctl_mtx);
295 cfi_done(union ctl_io *io)
297 struct ctl_fe_ioctl_params *params;
299 params = (struct ctl_fe_ioctl_params *)
300 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
302 mtx_lock(¶ms->ioctl_mtx);
303 params->state = CTL_IOCTL_DONE;
304 cv_broadcast(¶ms->sem);
305 mtx_unlock(¶ms->ioctl_mtx);
309 cfi_submit_wait(union ctl_io *io)
311 struct ctl_fe_ioctl_params params;
312 ctl_fe_ioctl_state last_state;
315 bzero(¶ms, sizeof(params));
316 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
317 cv_init(¶ms.sem, "ctlioccv");
318 params.state = CTL_IOCTL_INPROG;
319 last_state = params.state;
321 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
323 CTL_DEBUG_PRINT(("cfi_submit_wait\n"));
325 /* This shouldn't happen */
326 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
332 mtx_lock(¶ms.ioctl_mtx);
334 * Check the state here, and don't sleep if the state has
335 * already changed (i.e. wakeup has already occurred, but we
336 * weren't waiting yet).
338 if (params.state == last_state) {
339 /* XXX KDM cv_wait_sig instead? */
340 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
342 last_state = params.state;
344 switch (params.state) {
345 case CTL_IOCTL_INPROG:
346 /* Why did we wake up? */
347 /* XXX KDM error here? */
348 mtx_unlock(¶ms.ioctl_mtx);
350 case CTL_IOCTL_DATAMOVE:
351 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
354 * change last_state back to INPROG to avoid
355 * deadlock on subsequent data moves.
357 params.state = last_state = CTL_IOCTL_INPROG;
359 mtx_unlock(¶ms.ioctl_mtx);
360 ctl_ioctl_do_datamove(&io->scsiio);
362 * Note that in some cases, most notably writes,
363 * this will queue the I/O and call us back later.
364 * In other cases, generally reads, this routine
365 * will immediately call back and wake us up,
366 * probably using our own context.
368 io->scsiio.be_move_done(io);
371 mtx_unlock(¶ms.ioctl_mtx);
372 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
376 mtx_unlock(¶ms.ioctl_mtx);
377 /* XXX KDM error here? */
382 mtx_destroy(¶ms.ioctl_mtx);
383 cv_destroy(¶ms.sem);
385 return (CTL_RETVAL_COMPLETE);
389 ctl_ioctl_io(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
393 void *pool_tmp, *sc_tmp;
397 * If we haven't been "enabled", don't allow any SCSI I/O
400 if ((cfi_softc.port.status & CTL_PORT_STATUS_ONLINE) == 0)
403 io = ctl_alloc_io(cfi_softc.port.ctl_pool_ref);
406 * Need to save the pool reference so it doesn't get
407 * spammed by the user's ctl_io.
409 pool_tmp = io->io_hdr.pool;
410 sc_tmp = CTL_SOFTC(io);
411 memcpy(io, (void *)addr, sizeof(*io));
412 io->io_hdr.pool = pool_tmp;
413 CTL_SOFTC(io) = sc_tmp;
416 * No status yet, so make sure the status is set properly.
418 io->io_hdr.status = CTL_STATUS_NONE;
421 * The user sets the initiator ID, target and LUN IDs.
423 io->io_hdr.nexus.targ_port = cfi_softc.port.targ_port;
424 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
425 if ((io->io_hdr.io_type == CTL_IO_SCSI) &&
426 (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
427 io->scsiio.tag_num = cfi_softc.cur_tag_num++;
429 retval = cfi_submit_wait(io);
431 memcpy((void *)addr, io, sizeof(*io));