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