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