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