linux_sg.c revision 1.5.4.2 1 1.5.4.2 kent /* $NetBSD: linux_sg.c,v 1.5.4.2 2005/04/29 11:28:40 kent Exp $ */
2 1.5.4.2 kent
3 1.5.4.2 kent /*
4 1.5.4.2 kent * Copyright (c) 2004 Soren S. Jorvang. All rights reserved.
5 1.5.4.2 kent *
6 1.5.4.2 kent * Redistribution and use in source and binary forms, with or without
7 1.5.4.2 kent * modification, are permitted provided that the following conditions
8 1.5.4.2 kent * are met:
9 1.5.4.2 kent * 1. Redistributions of source code must retain the above copyright
10 1.5.4.2 kent * notice, this list of conditions, and the following disclaimer.
11 1.5.4.2 kent * 2. Redistributions in binary form must reproduce the above copyright
12 1.5.4.2 kent * notice, this list of conditions and the following disclaimer in the
13 1.5.4.2 kent * documentation and/or other materials provided with the distribution.
14 1.5.4.2 kent *
15 1.5.4.2 kent * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.5.4.2 kent * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.5.4.2 kent * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.5.4.2 kent * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.5.4.2 kent * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.5.4.2 kent * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.5.4.2 kent * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.5.4.2 kent * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.5.4.2 kent * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.5.4.2 kent * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.5.4.2 kent * SUCH DAMAGE.
26 1.5.4.2 kent */
27 1.5.4.2 kent
28 1.5.4.2 kent #include <sys/cdefs.h>
29 1.5.4.2 kent __KERNEL_RCSID(0, "$NetBSD: linux_sg.c,v 1.5.4.2 2005/04/29 11:28:40 kent Exp $");
30 1.5.4.2 kent
31 1.5.4.2 kent #include <sys/param.h>
32 1.5.4.2 kent #include <sys/systm.h>
33 1.5.4.2 kent #include <sys/ioctl.h>
34 1.5.4.2 kent #include <sys/file.h>
35 1.5.4.2 kent #include <sys/filedesc.h>
36 1.5.4.2 kent #include <sys/mount.h>
37 1.5.4.2 kent #include <sys/proc.h>
38 1.5.4.2 kent
39 1.5.4.2 kent #include <sys/scsiio.h>
40 1.5.4.2 kent #include <dev/scsipi/scsipi_all.h>
41 1.5.4.2 kent #include <dev/scsipi/scsiconf.h>
42 1.5.4.2 kent
43 1.5.4.2 kent #include <sys/sa.h>
44 1.5.4.2 kent #include <sys/syscallargs.h>
45 1.5.4.2 kent
46 1.5.4.2 kent #include <compat/linux/common/linux_types.h>
47 1.5.4.2 kent #include <compat/linux/common/linux_ioctl.h>
48 1.5.4.2 kent #include <compat/linux/common/linux_signal.h>
49 1.5.4.2 kent #include <compat/linux/common/linux_sg.h>
50 1.5.4.2 kent
51 1.5.4.2 kent #include <compat/linux/linux_syscallargs.h>
52 1.5.4.2 kent
53 1.5.4.2 kent int linux_sg_version = 30125;
54 1.5.4.2 kent
55 1.5.4.2 kent #ifdef LINUX_SG_DEBUG
56 1.5.4.2 kent #define DPRINTF(a) printf a
57 1.5.4.2 kent #else
58 1.5.4.2 kent #define DPRINTF(a)
59 1.5.4.2 kent #endif
60 1.5.4.2 kent
61 1.5.4.2 kent #ifdef LINUX_SG_DEBUG
62 1.5.4.2 kent static void dump_sg_io(struct linux_sg_io_hdr *);
63 1.5.4.2 kent static void dump_scsireq(struct scsireq *);
64 1.5.4.2 kent #endif
65 1.5.4.2 kent
66 1.5.4.2 kent static int bsd_to_linux_host_status(int);
67 1.5.4.2 kent static int bsd_to_linux_driver_status(int);
68 1.5.4.2 kent
69 1.5.4.2 kent int
70 1.5.4.2 kent linux_ioctl_sg(struct proc *p, struct linux_sys_ioctl_args *uap,
71 1.5.4.2 kent register_t *retval)
72 1.5.4.2 kent {
73 1.5.4.2 kent struct file *fp;
74 1.5.4.2 kent struct filedesc *fdp;
75 1.5.4.2 kent u_long com = SCARG(uap, com);
76 1.5.4.2 kent int error = 0;
77 1.5.4.2 kent int (*ioctlf)(struct file *, u_long, void *, struct proc *);
78 1.5.4.2 kent struct linux_sg_io_hdr lreq;
79 1.5.4.2 kent struct scsireq req;
80 1.5.4.2 kent
81 1.5.4.2 kent fdp = p->p_fd;
82 1.5.4.2 kent if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
83 1.5.4.2 kent return EBADF;
84 1.5.4.2 kent
85 1.5.4.2 kent FILE_USE(fp);
86 1.5.4.2 kent ioctlf = fp->f_ops->fo_ioctl;
87 1.5.4.2 kent
88 1.5.4.2 kent *retval = 0;
89 1.5.4.2 kent DPRINTF(("Command = %lx\n", com));
90 1.5.4.2 kent switch (com) {
91 1.5.4.2 kent case LINUX_SG_GET_VERSION_NUM: {
92 1.5.4.2 kent error = copyout(&version, SCARG(uap, data),
93 1.5.4.2 kent sizeof(linux_sg_version));
94 1.5.4.2 kent break;
95 1.5.4.2 kent }
96 1.5.4.2 kent case LINUX_SG_IO:
97 1.5.4.2 kent error = copyin(SCARG(uap, data), &lreq, sizeof(lreq));
98 1.5.4.2 kent if (error) {
99 1.5.4.2 kent DPRINTF(("failed to copy in request data %d\n", error));
100 1.5.4.2 kent break;
101 1.5.4.2 kent }
102 1.5.4.2 kent
103 1.5.4.2 kent #ifdef LINUX_SG_DEBUG
104 1.5.4.2 kent dump_sg_io(&lreq);
105 1.5.4.2 kent #endif
106 1.5.4.2 kent (void)memset(&req, 0, sizeof(req));
107 1.5.4.2 kent switch (lreq.dxfer_direction) {
108 1.5.4.2 kent case SG_DXFER_TO_DEV:
109 1.5.4.2 kent req.flags = SCCMD_WRITE;
110 1.5.4.2 kent break;
111 1.5.4.2 kent case SG_DXFER_FROM_DEV:
112 1.5.4.2 kent req.flags = SCCMD_READ;
113 1.5.4.2 kent break;
114 1.5.4.2 kent default:
115 1.5.4.2 kent DPRINTF(("unknown direction %d\n",
116 1.5.4.2 kent lreq.dxfer_direction));
117 1.5.4.2 kent error = EINVAL;
118 1.5.4.2 kent goto done;
119 1.5.4.2 kent }
120 1.5.4.2 kent if (lreq.iovec_count != 0) {
121 1.5.4.2 kent /* XXX: Not yet */
122 1.5.4.2 kent error = EOPNOTSUPP;
123 1.5.4.2 kent DPRINTF(("scatter/gather not supported\n"));
124 1.5.4.2 kent break;
125 1.5.4.2 kent }
126 1.5.4.2 kent
127 1.5.4.2 kent if (lreq.cmd_len > sizeof(req.cmd)) {
128 1.5.4.2 kent DPRINTF(("invalid command length %d\n", lreq.cmd_len));
129 1.5.4.2 kent error = EINVAL;
130 1.5.4.2 kent break;
131 1.5.4.2 kent }
132 1.5.4.2 kent
133 1.5.4.2 kent error = copyin(lreq.cmdp, req.cmd, lreq.cmd_len);
134 1.5.4.2 kent if (error) {
135 1.5.4.2 kent DPRINTF(("failed to copy in cmd data %d\n", error));
136 1.5.4.2 kent break;
137 1.5.4.2 kent }
138 1.5.4.2 kent
139 1.5.4.2 kent req.timeout = lreq.timeout;
140 1.5.4.2 kent req.cmdlen = lreq.cmd_len;
141 1.5.4.2 kent req.datalen = lreq.dxfer_len;
142 1.5.4.2 kent req.databuf = lreq.dxferp;
143 1.5.4.2 kent
144 1.5.4.2 kent error = ioctlf(fp, SCIOCCOMMAND, (caddr_t)&req, p);
145 1.5.4.2 kent if (error) {
146 1.5.4.2 kent DPRINTF(("SCIOCCOMMAND failed %d\n", error));
147 1.5.4.2 kent break;
148 1.5.4.2 kent }
149 1.5.4.2 kent #ifdef LINUX_SG_DEBUG
150 1.5.4.2 kent dump_scsireq(&req);
151 1.5.4.2 kent #endif
152 1.5.4.2 kent if (req.senselen_used) {
153 1.5.4.2 kent if (req.senselen > lreq.mx_sb_len)
154 1.5.4.2 kent req.senselen = lreq.mx_sb_len;
155 1.5.4.2 kent lreq.sb_len_wr = req.senselen;
156 1.5.4.2 kent error = copyout(req.sense, lreq.sbp, req.senselen);
157 1.5.4.2 kent if (error) {
158 1.5.4.2 kent DPRINTF(("sense copyout failed %d\n", error));
159 1.5.4.2 kent break;
160 1.5.4.2 kent }
161 1.5.4.2 kent } else {
162 1.5.4.2 kent lreq.sb_len_wr = 0;
163 1.5.4.2 kent }
164 1.5.4.2 kent
165 1.5.4.2 kent lreq.status = req.status;
166 1.5.4.2 kent lreq.masked_status = 0; /* XXX */
167 1.5.4.2 kent lreq.host_status = bsd_to_linux_host_status(req.retsts);
168 1.5.4.2 kent lreq.sb_len_wr = req.datalen_used;
169 1.5.4.2 kent lreq.driver_status = bsd_to_linux_driver_status(req.error);
170 1.5.4.2 kent lreq.resid = req.datalen - req.datalen_used;
171 1.5.4.2 kent lreq.duration = req.timeout; /* XXX */
172 1.5.4.2 kent lreq.info = 0; /* XXX */
173 1.5.4.2 kent error = copyout(&lreq, SCARG(uap, data), sizeof(lreq));
174 1.5.4.2 kent if (error)
175 1.5.4.2 kent DPRINTF(("failed to copy out req data %d\n", error));
176 1.5.4.2 kent break;
177 1.5.4.2 kent case LINUX_SG_EMULATED_HOST:
178 1.5.4.2 kent case LINUX_SG_SET_TRANSFORM:
179 1.5.4.2 kent case LINUX_SG_GET_TRANSFORM:
180 1.5.4.2 kent case LINUX_SG_GET_NUM_WAITING:
181 1.5.4.2 kent case LINUX_SG_SCSI_RESET:
182 1.5.4.2 kent case LINUX_SG_GET_REQUEST_TABLE:
183 1.5.4.2 kent case LINUX_SG_SET_KEEP_ORPHAN:
184 1.5.4.2 kent case LINUX_SG_GET_KEEP_ORPHAN:
185 1.5.4.2 kent case LINUX_SG_GET_ACCESS_COUNT:
186 1.5.4.2 kent case LINUX_SG_SET_FORCE_LOW_DMA:
187 1.5.4.2 kent case LINUX_SG_GET_LOW_DMA:
188 1.5.4.2 kent case LINUX_SG_GET_SG_TABLESIZE:
189 1.5.4.2 kent case LINUX_SG_GET_SCSI_ID:
190 1.5.4.2 kent case LINUX_SG_SET_FORCE_PACK_ID:
191 1.5.4.2 kent case LINUX_SG_GET_PACK_ID:
192 1.5.4.2 kent case LINUX_SG_SET_RESERVED_SIZE:
193 1.5.4.2 kent case LINUX_SG_GET_RESERVED_SIZE:
194 1.5.4.2 kent error = ENODEV;
195 1.5.4.2 kent break;
196 1.5.4.2 kent
197 1.5.4.2 kent /* version 2 interfaces */
198 1.5.4.2 kent case LINUX_SG_SET_TIMEOUT:
199 1.5.4.2 kent break;
200 1.5.4.2 kent case LINUX_SG_GET_TIMEOUT:
201 1.5.4.2 kent /* ioctl returns value..., grr. */
202 1.5.4.2 kent *retval = 60;
203 1.5.4.2 kent break;
204 1.5.4.2 kent case LINUX_SG_GET_COMMAND_Q:
205 1.5.4.2 kent case LINUX_SG_SET_COMMAND_Q:
206 1.5.4.2 kent case LINUX_SG_SET_DEBUG:
207 1.5.4.2 kent case LINUX_SG_NEXT_CMD_LEN:
208 1.5.4.2 kent error = ENODEV;
209 1.5.4.2 kent break;
210 1.5.4.2 kent }
211 1.5.4.2 kent
212 1.5.4.2 kent done:
213 1.5.4.2 kent FILE_UNUSE(fp, p);
214 1.5.4.2 kent
215 1.5.4.2 kent DPRINTF(("Return=%d\n", error));
216 1.5.4.2 kent return error;
217 1.5.4.2 kent }
218 1.5.4.2 kent
219 1.5.4.2 kent static int
220 1.5.4.2 kent bsd_to_linux_driver_status(int bs)
221 1.5.4.2 kent {
222 1.5.4.2 kent switch (bs) {
223 1.5.4.2 kent default:
224 1.5.4.2 kent case XS_NOERROR:
225 1.5.4.2 kent return 0;
226 1.5.4.2 kent case XS_SENSE:
227 1.5.4.2 kent case XS_SHORTSENSE:
228 1.5.4.2 kent return LINUX_DRIVER_SENSE;
229 1.5.4.2 kent case XS_RESOURCE_SHORTAGE:
230 1.5.4.2 kent return LINUX_DRIVER_SOFT;
231 1.5.4.2 kent case XS_DRIVER_STUFFUP:
232 1.5.4.2 kent return LINUX_DRIVER_ERROR;
233 1.5.4.2 kent case XS_SELTIMEOUT:
234 1.5.4.2 kent case XS_TIMEOUT:
235 1.5.4.2 kent return LINUX_DRIVER_TIMEOUT;
236 1.5.4.2 kent case XS_BUSY:
237 1.5.4.2 kent return LINUX_DRIVER_BUSY;
238 1.5.4.2 kent case XS_RESET:
239 1.5.4.2 kent return LINUX_SUGGEST_ABORT;
240 1.5.4.2 kent case XS_REQUEUE:
241 1.5.4.2 kent return LINUX_SUGGEST_RETRY;
242 1.5.4.2 kent }
243 1.5.4.2 kent }
244 1.5.4.2 kent
245 1.5.4.2 kent static int
246 1.5.4.2 kent bsd_to_linux_host_status(int bs)
247 1.5.4.2 kent {
248 1.5.4.2 kent switch (bs) {
249 1.5.4.2 kent case SCCMD_OK:
250 1.5.4.2 kent case SCCMD_SENSE:
251 1.5.4.2 kent return LINUX_DID_OK;
252 1.5.4.2 kent case SCCMD_TIMEOUT:
253 1.5.4.2 kent return LINUX_DID_TIME_OUT;
254 1.5.4.2 kent case SCCMD_BUSY:
255 1.5.4.2 kent return LINUX_DID_BUS_BUSY;
256 1.5.4.2 kent case SCCMD_UNKNOWN:
257 1.5.4.2 kent default:
258 1.5.4.2 kent return LINUX_DID_ERROR;
259 1.5.4.2 kent }
260 1.5.4.2 kent }
261 1.5.4.2 kent
262 1.5.4.2 kent
263 1.5.4.2 kent
264 1.5.4.2 kent #ifdef LINUX_SG_DEBUG
265 1.5.4.2 kent static void
266 1.5.4.2 kent dump_sg_io(struct linux_sg_io_hdr *lr)
267 1.5.4.2 kent {
268 1.5.4.2 kent printf("linuxreq [interface_id=%x, dxfer_direction=%d, cmd_len=%d, "
269 1.5.4.2 kent "mx_sb_len=%d, iovec_count=%d, dxfer_len=%d, dxferp=%p, "
270 1.5.4.2 kent "cmdp=%p, sbp=%p, timeout=%u, flags=%d, pack_id=%d, "
271 1.5.4.2 kent "usr_ptr=%p, status=%u, masked_status=%u, sb_len_wr=%u, "
272 1.5.4.2 kent "host_status=%u, driver_status=%u, resid=%d, duration=%u, "
273 1.5.4.2 kent "info=%u]\n",
274 1.5.4.2 kent lr->interface_id, lr->dxfer_direction, lr->cmd_len,
275 1.5.4.2 kent lr->mx_sb_len, lr->iovec_count, lr->dxfer_len, lr->dxferp,
276 1.5.4.2 kent lr->cmdp, lr->sbp, lr->timeout, lr->flags, lr->pack_id,
277 1.5.4.2 kent lr->usr_ptr, lr->status, lr->masked_status, lr->sb_len_wr,
278 1.5.4.2 kent lr->host_status, lr->driver_status, lr->resid, lr->duration,
279 1.5.4.2 kent lr->info);
280 1.5.4.2 kent }
281 1.5.4.2 kent
282 1.5.4.2 kent static void
283 1.5.4.2 kent dump_scsireq(struct scsireq *br)
284 1.5.4.2 kent {
285 1.5.4.2 kent int i;
286 1.5.4.2 kent printf("bsdreq [flags=%lx, timeout=%lu, cmd=[ ",
287 1.5.4.2 kent br->flags, br->timeout);
288 1.5.4.2 kent for (i = 0; i < sizeof(br->cmd) / sizeof(br->cmd[0]); i++)
289 1.5.4.2 kent printf("%.2u ", br->cmd[i]);
290 1.5.4.2 kent printf("], cmdlen=%u, databuf=%p, datalen=%lu, datalen_used=%lu, "
291 1.5.4.2 kent "sense=[ ",
292 1.5.4.2 kent br->cmdlen, br->databuf, br->datalen, br->datalen_used);
293 1.5.4.2 kent for (i = 0; i < sizeof(br->sense) / sizeof(br->sense[0]); i++)
294 1.5.4.2 kent printf("%.2u ", br->sense[i]);
295 1.5.4.2 kent printf("], senselen=%u, senselen_used=%u, status=%u, retsts=%u, "
296 1.5.4.2 kent "error=%d]\n",
297 1.5.4.2 kent br->senselen, br->senselen_used, br->status, br->retsts, br->error);
298 1.5.4.2 kent }
299 1.5.4.2 kent #endif
300