Home | History | Annotate | Line # | Download | only in common
linux_sg.c revision 1.12
      1 /* $NetBSD: linux_sg.c,v 1.12 2007/12/20 23:02:56 dsl Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2004 Soren S. Jorvang.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions, and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: linux_sg.c,v 1.12 2007/12/20 23:02:56 dsl Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/systm.h>
     33 #include <sys/ioctl.h>
     34 #include <sys/file.h>
     35 #include <sys/filedesc.h>
     36 #include <sys/mount.h>
     37 #include <sys/proc.h>
     38 #include <sys/device.h>
     39 
     40 #include <sys/scsiio.h>
     41 #include <dev/scsipi/scsipi_all.h>
     42 #include <dev/scsipi/scsiconf.h>
     43 
     44 #include <sys/syscallargs.h>
     45 
     46 #include <compat/linux/common/linux_types.h>
     47 #include <compat/linux/common/linux_ioctl.h>
     48 #include <compat/linux/common/linux_signal.h>
     49 #include <compat/linux/common/linux_sg.h>
     50 #include <compat/linux/common/linux_ipc.h>
     51 #include <compat/linux/common/linux_sem.h>
     52 
     53 #include <compat/linux/linux_syscallargs.h>
     54 
     55 int linux_sg_version = 30125;
     56 
     57 #ifdef LINUX_SG_DEBUG
     58 #define DPRINTF(a)	printf a
     59 #else
     60 #define DPRINTF(a)
     61 #endif
     62 
     63 #ifdef LINUX_SG_DEBUG
     64 static void dump_sg_io(struct linux_sg_io_hdr *);
     65 static void dump_scsireq(struct scsireq *);
     66 #endif
     67 
     68 static int bsd_to_linux_host_status(int);
     69 static int bsd_to_linux_driver_status(int);
     70 
     71 int
     72 linux_ioctl_sg(struct lwp *l, const struct linux_sys_ioctl_args *uap,
     73     register_t *retval)
     74 {
     75 	struct file *fp;
     76 	struct filedesc *fdp;
     77 	u_long com = SCARG(uap, com);
     78 	int error = 0;
     79 	int (*ioctlf)(struct file *, u_long, void *, struct lwp *);
     80 	struct linux_sg_io_hdr lreq;
     81 	struct scsireq req;
     82 
     83         fdp = l->l_proc->p_fd;
     84 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
     85 		return EBADF;
     86 
     87 	FILE_USE(fp);
     88 	ioctlf = fp->f_ops->fo_ioctl;
     89 
     90 	*retval = 0;
     91 	DPRINTF(("Command = %lx\n", com));
     92 	switch (com) {
     93 	case LINUX_SG_GET_VERSION_NUM: {
     94 		error = copyout(&version, SCARG(uap, data),
     95 		    sizeof(linux_sg_version));
     96 		break;
     97 	}
     98 	case LINUX_SG_IO:
     99 		error = copyin(SCARG(uap, data), &lreq, sizeof(lreq));
    100 		if (error) {
    101 			DPRINTF(("failed to copy in request data %d\n", error));
    102 			break;
    103 		}
    104 
    105 #ifdef LINUX_SG_DEBUG
    106 		dump_sg_io(&lreq);
    107 #endif
    108 		(void)memset(&req, 0, sizeof(req));
    109 		switch (lreq.dxfer_direction) {
    110 		case SG_DXFER_TO_DEV:
    111 			req.flags = SCCMD_WRITE;
    112 			break;
    113 		case SG_DXFER_FROM_DEV:
    114 			req.flags = SCCMD_READ;
    115 			break;
    116 		default:
    117 			DPRINTF(("unknown direction %d\n",
    118 				lreq.dxfer_direction));
    119 			error = EINVAL;
    120 			goto done;
    121 		}
    122 		if (lreq.iovec_count != 0) {
    123 			/* XXX: Not yet */
    124 			error = EOPNOTSUPP;
    125 			DPRINTF(("scatter/gather not supported\n"));
    126 			break;
    127 		}
    128 
    129 		if (lreq.cmd_len > sizeof(req.cmd)) {
    130 			DPRINTF(("invalid command length %d\n", lreq.cmd_len));
    131 			error = EINVAL;
    132 			break;
    133 		}
    134 
    135 		error = copyin(lreq.cmdp, req.cmd, lreq.cmd_len);
    136 		if (error) {
    137 			DPRINTF(("failed to copy in cmd data %d\n", error));
    138 			break;
    139 		}
    140 
    141 		req.timeout = lreq.timeout;
    142 		req.cmdlen = lreq.cmd_len;
    143 		req.datalen = lreq.dxfer_len;
    144 		req.databuf = lreq.dxferp;
    145 
    146 		error = ioctlf(fp, SCIOCCOMMAND, (void *)&req, l);
    147 		if (error) {
    148 			DPRINTF(("SCIOCCOMMAND failed %d\n", error));
    149 			break;
    150 		}
    151 #ifdef LINUX_SG_DEBUG
    152 		dump_scsireq(&req);
    153 #endif
    154 		if (req.senselen_used) {
    155 			if (req.senselen > lreq.mx_sb_len)
    156 				req.senselen = lreq.mx_sb_len;
    157 			lreq.sb_len_wr = req.senselen;
    158 			error = copyout(req.sense, lreq.sbp, req.senselen);
    159 			if (error) {
    160 				DPRINTF(("sense copyout failed %d\n", error));
    161 				break;
    162 			}
    163 		} else {
    164 			lreq.sb_len_wr = 0;
    165 		}
    166 
    167 		lreq.status = req.status;
    168 		lreq.masked_status = 0; 	/* XXX */
    169 		lreq.host_status = bsd_to_linux_host_status(req.retsts);
    170 		lreq.sb_len_wr = req.datalen_used;
    171 		lreq.driver_status = bsd_to_linux_driver_status(req.error);
    172 		lreq.resid = req.datalen - req.datalen_used;
    173 		lreq.duration = req.timeout;	/* XXX */
    174 		lreq.info = 0;			/* XXX */
    175 		error = copyout(&lreq, SCARG(uap, data), sizeof(lreq));
    176 		if (error) {
    177 			DPRINTF(("failed to copy out req data %d\n", error));
    178 		}
    179 		break;
    180 	case LINUX_SG_EMULATED_HOST:
    181 	case LINUX_SG_SET_TRANSFORM:
    182 	case LINUX_SG_GET_TRANSFORM:
    183 	case LINUX_SG_GET_NUM_WAITING:
    184 	case LINUX_SG_SCSI_RESET:
    185 	case LINUX_SG_GET_REQUEST_TABLE:
    186 	case LINUX_SG_SET_KEEP_ORPHAN:
    187 	case LINUX_SG_GET_KEEP_ORPHAN:
    188 	case LINUX_SG_GET_ACCESS_COUNT:
    189 	case LINUX_SG_SET_FORCE_LOW_DMA:
    190 	case LINUX_SG_GET_LOW_DMA:
    191 	case LINUX_SG_GET_SG_TABLESIZE:
    192 	case LINUX_SG_GET_SCSI_ID:
    193 	case LINUX_SG_SET_FORCE_PACK_ID:
    194 	case LINUX_SG_GET_PACK_ID:
    195 	case LINUX_SG_SET_RESERVED_SIZE:
    196 	case LINUX_SG_GET_RESERVED_SIZE:
    197 		error = ENODEV;
    198 		break;
    199 
    200 	/* version 2 interfaces */
    201 	case LINUX_SG_SET_TIMEOUT:
    202 		break;
    203 	case LINUX_SG_GET_TIMEOUT:
    204 		/* ioctl returns value..., grr. */
    205 		*retval = 60;
    206 		break;
    207 	case LINUX_SG_GET_COMMAND_Q:
    208 	case LINUX_SG_SET_COMMAND_Q:
    209 	case LINUX_SG_SET_DEBUG:
    210 	case LINUX_SG_NEXT_CMD_LEN:
    211 		error = ENODEV;
    212 		break;
    213 	}
    214 
    215 done:
    216 	FILE_UNUSE(fp, l);
    217 
    218 	DPRINTF(("Return=%d\n", error));
    219 	return error;
    220 }
    221 
    222 static int
    223 bsd_to_linux_driver_status(int bs)
    224 {
    225 	switch (bs) {
    226 	default:
    227 	case XS_NOERROR:
    228 		return 0;
    229 	case XS_SENSE:
    230 	case XS_SHORTSENSE:
    231 		return LINUX_DRIVER_SENSE;
    232 	case XS_RESOURCE_SHORTAGE:
    233 		return LINUX_DRIVER_SOFT;
    234 	case XS_DRIVER_STUFFUP:
    235 		return LINUX_DRIVER_ERROR;
    236 	case XS_SELTIMEOUT:
    237 	case XS_TIMEOUT:
    238 		return LINUX_DRIVER_TIMEOUT;
    239 	case XS_BUSY:
    240 		return LINUX_DRIVER_BUSY;
    241 	case XS_RESET:
    242 		return LINUX_SUGGEST_ABORT;
    243 	case XS_REQUEUE:
    244 		return LINUX_SUGGEST_RETRY;
    245 	}
    246 }
    247 
    248 static int
    249 bsd_to_linux_host_status(int bs)
    250 {
    251 	switch (bs) {
    252 	case SCCMD_OK:
    253 	case SCCMD_SENSE:
    254 		return LINUX_DID_OK;
    255 	case SCCMD_TIMEOUT:
    256 		return LINUX_DID_TIME_OUT;
    257 	case SCCMD_BUSY:
    258 		return LINUX_DID_BUS_BUSY;
    259 	case SCCMD_UNKNOWN:
    260 	default:
    261 		return LINUX_DID_ERROR;
    262 	}
    263 }
    264 
    265 
    266 
    267 #ifdef LINUX_SG_DEBUG
    268 static void
    269 dump_sg_io(struct linux_sg_io_hdr *lr)
    270 {
    271 	printf("linuxreq [interface_id=%x, dxfer_direction=%d, cmd_len=%d, "
    272 	    "mx_sb_len=%d, iovec_count=%d, dxfer_len=%d, dxferp=%p, "
    273 	    "cmdp=%p, sbp=%p, timeout=%u, flags=%d, pack_id=%d, "
    274 	    "usr_ptr=%p, status=%u, masked_status=%u, sb_len_wr=%u, "
    275 	    "host_status=%u, driver_status=%u, resid=%d, duration=%u, "
    276 	    "info=%u]\n",
    277 	    lr->interface_id, lr->dxfer_direction, lr->cmd_len,
    278 	    lr->mx_sb_len, lr->iovec_count, lr->dxfer_len, lr->dxferp,
    279 	    lr->cmdp, lr->sbp, lr->timeout, lr->flags, lr->pack_id,
    280 	    lr->usr_ptr, lr->status, lr->masked_status, lr->sb_len_wr,
    281 	    lr->host_status, lr->driver_status, lr->resid, lr->duration,
    282 	    lr->info);
    283 }
    284 
    285 static void
    286 dump_scsireq(struct scsireq *br)
    287 {
    288 	int i;
    289 	printf("bsdreq [flags=%lx, timeout=%lu, cmd=[ ",
    290 	    br->flags, br->timeout);
    291 	for (i = 0; i < sizeof(br->cmd) / sizeof(br->cmd[0]); i++)
    292 		printf("%.2u ", br->cmd[i]);
    293 	printf("], cmdlen=%u, databuf=%p, datalen=%lu, datalen_used=%lu, "
    294 	    "sense=[ ",
    295 	    br->cmdlen, br->databuf, br->datalen, br->datalen_used);
    296 	for (i = 0; i < sizeof(br->sense) / sizeof(br->sense[0]); i++)
    297 		printf("%.2u ", br->sense[i]);
    298 	printf("], senselen=%u, senselen_used=%u, status=%u, retsts=%u, "
    299 	    "error=%d]\n",
    300 	    br->senselen, br->senselen_used, br->status, br->retsts, br->error);
    301 }
    302 #endif
    303