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