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