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