1 /* $NetBSD: linux_sg.c,v 1.5.2.1 2006/10/11 16:13:56 ghen 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.5.2.1 2006/10/11 16:13:56 ghen 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 proc *p, 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 proc *);
79 struct linux_sg_io_hdr lreq;
80 struct scsireq req;
81
82 fdp = p->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, p);
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 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, p);
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
Cache object: 9833db5882177005704f752973aec8d2
|