Home | History | Annotate | Line # | Download | only in common
linux_sg.c revision 1.7.20.1
      1 /* $NetBSD: linux_sg.c,v 1.7.20.1 2006/11/18 21:39:08 ad 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.7.20.1 2006/11/18 21:39:08 ad 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/sa.h>
     45 #include <sys/syscallargs.h>
     46 
     47 #include <compat/linux/common/linux_types.h>
     48 #include <compat/linux/common/linux_ioctl.h>
     49 #include <compat/linux/common/linux_signal.h>
     50 #include <compat/linux/common/linux_sg.h>
     51 
     52 #include <compat/linux/linux_syscallargs.h>
     53 
     54 int linux_sg_version = 30125;
     55 
     56 #ifdef LINUX_SG_DEBUG
     57 #define DPRINTF(a)	printf a
     58 #else
     59 #define DPRINTF(a)
     60 #endif
     61 
     62 #ifdef LINUX_SG_DEBUG
     63 static void dump_sg_io(struct linux_sg_io_hdr *);
     64 static void dump_scsireq(struct scsireq *);
     65 #endif
     66 
     67 static int bsd_to_linux_host_status(int);
     68 static int bsd_to_linux_driver_status(int);
     69 
     70 int
     71 linux_ioctl_sg(struct lwp *l, struct linux_sys_ioctl_args *uap,
     72     register_t *retval)
     73 {
     74 	struct file *fp;
     75 	struct filedesc *fdp;
     76 	u_long com = SCARG(uap, com);
     77 	int error = 0;
     78 	int (*ioctlf)(struct file *, u_long, void *, struct lwp *);
     79 	struct linux_sg_io_hdr lreq;
     80 	struct scsireq req;
     81 
     82         fdp = l->l_proc->p_fd;
     83 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
     84 		return EBADF;
     85 
     86 	FILE_USE(fp);
     87 	ioctlf = fp->f_ops->fo_ioctl;
     88 
     89 	*retval = 0;
     90 	DPRINTF(("Command = %lx\n", com));
     91 	switch (com) {
     92 	case LINUX_SG_GET_VERSION_NUM: {
     93 		error = copyout(&version, SCARG(uap, data),
     94 		    sizeof(linux_sg_version));
     95 		break;
     96 	}
     97 	case LINUX_SG_IO:
     98 		error = copyin(SCARG(uap, data), &lreq, sizeof(lreq));
     99 		if (error) {
    100 			DPRINTF(("failed to copy in request data %d\n", error));
    101 			break;
    102 		}
    103 
    104 #ifdef LINUX_SG_DEBUG
    105 		dump_sg_io(&lreq);
    106 #endif
    107 		(void)memset(&req, 0, sizeof(req));
    108 		switch (lreq.dxfer_direction) {
    109 		case SG_DXFER_TO_DEV:
    110 			req.flags = SCCMD_WRITE;
    111 			break;
    112 		case SG_DXFER_FROM_DEV:
    113 			req.flags = SCCMD_READ;
    114 			break;
    115 		default:
    116 			DPRINTF(("unknown direction %d\n",
    117 				lreq.dxfer_direction));
    118 			error = EINVAL;
    119 			goto done;
    120 		}
    121 		if (lreq.iovec_count != 0) {
    122 			/* XXX: Not yet */
    123 			error = EOPNOTSUPP;
    124 			DPRINTF(("scatter/gather not supported\n"));
    125 			break;
    126 		}
    127 
    128 		if (lreq.cmd_len > sizeof(req.cmd)) {
    129 			DPRINTF(("invalid command length %d\n", lreq.cmd_len));
    130 			error = EINVAL;
    131 			break;
    132 		}
    133 
    134 		error = copyin(lreq.cmdp, req.cmd, lreq.cmd_len);
    135 		if (error) {
    136 			DPRINTF(("failed to copy in cmd data %d\n", error));
    137 			break;
    138 		}
    139 
    140 		req.timeout = lreq.timeout;
    141 		req.cmdlen = lreq.cmd_len;
    142 		req.datalen = lreq.dxfer_len;
    143 		req.databuf = lreq.dxferp;
    144 
    145 		error = ioctlf(fp, SCIOCCOMMAND, (caddr_t)&req, l);
    146 		if (error) {
    147 			DPRINTF(("SCIOCCOMMAND failed %d\n", error));
    148 			break;
    149 		}
    150 #ifdef LINUX_SG_DEBUG
    151 		dump_scsireq(&req);
    152 #endif
    153 		if (req.senselen_used) {
    154 			if (req.senselen > lreq.mx_sb_len)
    155 				req.senselen = lreq.mx_sb_len;
    156 			lreq.sb_len_wr = req.senselen;
    157 			error = copyout(req.sense, lreq.sbp, req.senselen);
    158 			if (error) {
    159 				DPRINTF(("sense copyout failed %d\n", error));
    160 				break;
    161 			}
    162 		} else {
    163 			lreq.sb_len_wr = 0;
    164 		}
    165 
    166 		lreq.status = req.status;
    167 		lreq.masked_status = 0; 	/* XXX */
    168 		lreq.host_status = bsd_to_linux_host_status(req.retsts);
    169 		lreq.sb_len_wr = req.datalen_used;
    170 		lreq.driver_status = bsd_to_linux_driver_status(req.error);
    171 		lreq.resid = req.datalen - req.datalen_used;
    172 		lreq.duration = req.timeout;	/* XXX */
    173 		lreq.info = 0;			/* XXX */
    174 		error = copyout(&lreq, SCARG(uap, data), sizeof(lreq));
    175 		if (error) {
    176 			DPRINTF(("failed to copy out req data %d\n", error));
    177 		}
    178 		break;
    179 	case LINUX_SG_EMULATED_HOST:
    180 	case LINUX_SG_SET_TRANSFORM:
    181 	case LINUX_SG_GET_TRANSFORM:
    182 	case LINUX_SG_GET_NUM_WAITING:
    183 	case LINUX_SG_SCSI_RESET:
    184 	case LINUX_SG_GET_REQUEST_TABLE:
    185 	case LINUX_SG_SET_KEEP_ORPHAN:
    186 	case LINUX_SG_GET_KEEP_ORPHAN:
    187 	case LINUX_SG_GET_ACCESS_COUNT:
    188 	case LINUX_SG_SET_FORCE_LOW_DMA:
    189 	case LINUX_SG_GET_LOW_DMA:
    190 	case LINUX_SG_GET_SG_TABLESIZE:
    191 	case LINUX_SG_GET_SCSI_ID:
    192 	case LINUX_SG_SET_FORCE_PACK_ID:
    193 	case LINUX_SG_GET_PACK_ID:
    194 	case LINUX_SG_SET_RESERVED_SIZE:
    195 	case LINUX_SG_GET_RESERVED_SIZE:
    196 		error = ENODEV;
    197 		break;
    198 
    199 	/* version 2 interfaces */
    200 	case LINUX_SG_SET_TIMEOUT:
    201 		break;
    202 	case LINUX_SG_GET_TIMEOUT:
    203 		/* ioctl returns value..., grr. */
    204 		*retval = 60;
    205 		break;
    206 	case LINUX_SG_GET_COMMAND_Q:
    207 	case LINUX_SG_SET_COMMAND_Q:
    208 	case LINUX_SG_SET_DEBUG:
    209 	case LINUX_SG_NEXT_CMD_LEN:
    210 		error = ENODEV;
    211 		break;
    212 	}
    213 
    214 done:
    215 	FILE_UNUSE(fp, l);
    216 
    217 	DPRINTF(("Return=%d\n", error));
    218 	return error;
    219 }
    220 
    221 static int
    222 bsd_to_linux_driver_status(int bs)
    223 {
    224 	switch (bs) {
    225 	default:
    226 	case XS_NOERROR:
    227 		return 0;
    228 	case XS_SENSE:
    229 	case XS_SHORTSENSE:
    230 		return LINUX_DRIVER_SENSE;
    231 	case XS_RESOURCE_SHORTAGE:
    232 		return LINUX_DRIVER_SOFT;
    233 	case XS_DRIVER_STUFFUP:
    234 		return LINUX_DRIVER_ERROR;
    235 	case XS_SELTIMEOUT:
    236 	case XS_TIMEOUT:
    237 		return LINUX_DRIVER_TIMEOUT;
    238 	case XS_BUSY:
    239 		return LINUX_DRIVER_BUSY;
    240 	case XS_RESET:
    241 		return LINUX_SUGGEST_ABORT;
    242 	case XS_REQUEUE:
    243 		return LINUX_SUGGEST_RETRY;
    244 	}
    245 }
    246 
    247 static int
    248 bsd_to_linux_host_status(int bs)
    249 {
    250 	switch (bs) {
    251 	case SCCMD_OK:
    252 	case SCCMD_SENSE:
    253 		return LINUX_DID_OK;
    254 	case SCCMD_TIMEOUT:
    255 		return LINUX_DID_TIME_OUT;
    256 	case SCCMD_BUSY:
    257 		return LINUX_DID_BUS_BUSY;
    258 	case SCCMD_UNKNOWN:
    259 	default:
    260 		return LINUX_DID_ERROR;
    261 	}
    262 }
    263 
    264 
    265 
    266 #ifdef LINUX_SG_DEBUG
    267 static void
    268 dump_sg_io(struct linux_sg_io_hdr *lr)
    269 {
    270 	printf("linuxreq [interface_id=%x, dxfer_direction=%d, cmd_len=%d, "
    271 	    "mx_sb_len=%d, iovec_count=%d, dxfer_len=%d, dxferp=%p, "
    272 	    "cmdp=%p, sbp=%p, timeout=%u, flags=%d, pack_id=%d, "
    273 	    "usr_ptr=%p, status=%u, masked_status=%u, sb_len_wr=%u, "
    274 	    "host_status=%u, driver_status=%u, resid=%d, duration=%u, "
    275 	    "info=%u]\n",
    276 	    lr->interface_id, lr->dxfer_direction, lr->cmd_len,
    277 	    lr->mx_sb_len, lr->iovec_count, lr->dxfer_len, lr->dxferp,
    278 	    lr->cmdp, lr->sbp, lr->timeout, lr->flags, lr->pack_id,
    279 	    lr->usr_ptr, lr->status, lr->masked_status, lr->sb_len_wr,
    280 	    lr->host_status, lr->driver_status, lr->resid, lr->duration,
    281 	    lr->info);
    282 }
    283 
    284 static void
    285 dump_scsireq(struct scsireq *br)
    286 {
    287 	int i;
    288 	printf("bsdreq [flags=%lx, timeout=%lu, cmd=[ ",
    289 	    br->flags, br->timeout);
    290 	for (i = 0; i < sizeof(br->cmd) / sizeof(br->cmd[0]); i++)
    291 		printf("%.2u ", br->cmd[i]);
    292 	printf("], cmdlen=%u, databuf=%p, datalen=%lu, datalen_used=%lu, "
    293 	    "sense=[ ",
    294 	    br->cmdlen, br->databuf, br->datalen, br->datalen_used);
    295 	for (i = 0; i < sizeof(br->sense) / sizeof(br->sense[0]); i++)
    296 		printf("%.2u ", br->sense[i]);
    297 	printf("], senselen=%u, senselen_used=%u, status=%u, retsts=%u, "
    298 	    "error=%d]\n",
    299 	    br->senselen, br->senselen_used, br->status, br->retsts, br->error);
    300 }
    301 #endif
    302