scsipi_ioctl.c revision 1.64.8.1 1 /* $NetBSD: scsipi_ioctl.c,v 1.64.8.1 2008/05/18 12:34:45 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Contributed by HD Associates (hd (at) world.std.com).
34 * Copyright (c) 1992, 1993 HD Associates
35 *
36 * Berkeley style copyright.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: scsipi_ioctl.c,v 1.64.8.1 2008/05/18 12:34:45 yamt Exp $");
41
42 #include "opt_compat_freebsd.h"
43 #include "opt_compat_netbsd.h"
44
45 #include <sys/param.h>
46 #include <sys/errno.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/buf.h>
50 #include <sys/proc.h>
51 #include <sys/device.h>
52 #include <sys/fcntl.h>
53
54 #include <dev/scsipi/scsipi_all.h>
55 #include <dev/scsipi/scsipiconf.h>
56 #include <dev/scsipi/scsipi_base.h>
57 #include <dev/scsipi/scsiconf.h>
58 #include <sys/scsiio.h>
59
60 #include "scsibus.h"
61 #include "atapibus.h"
62
63 struct scsi_ioctl {
64 LIST_ENTRY(scsi_ioctl) si_list;
65 struct buf si_bp;
66 struct uio si_uio;
67 struct iovec si_iov;
68 scsireq_t si_screq;
69 struct scsipi_periph *si_periph;
70 };
71
72 static LIST_HEAD(, scsi_ioctl) si_head;
73
74 static struct scsi_ioctl *
75 si_get(void)
76 {
77 struct scsi_ioctl *si;
78 int s;
79
80 si = malloc(sizeof(struct scsi_ioctl), M_TEMP, M_WAITOK|M_ZERO);
81 buf_init(&si->si_bp);
82 s = splbio();
83 LIST_INSERT_HEAD(&si_head, si, si_list);
84 splx(s);
85 return (si);
86 }
87
88 static void
89 si_free(struct scsi_ioctl *si)
90 {
91 int s;
92
93 s = splbio();
94 LIST_REMOVE(si, si_list);
95 splx(s);
96 buf_destroy(&si->si_bp);
97 free(si, M_TEMP);
98 }
99
100 static struct scsi_ioctl *
101 si_find(struct buf *bp)
102 {
103 struct scsi_ioctl *si;
104 int s;
105
106 s = splbio();
107 for (si = si_head.lh_first; si != 0; si = si->si_list.le_next)
108 if (bp == &si->si_bp)
109 break;
110 splx(s);
111 return (si);
112 }
113
114 /*
115 * We let the user interpret his own sense in the generic scsi world.
116 * This routine is called at interrupt time if the XS_CTL_USERCMD bit was set
117 * in the flags passed to scsi_scsipi_cmd(). No other completion processing
118 * takes place, even if we are running over another device driver.
119 * The lower level routines that call us here, will free the xs and restart
120 * the device's queue if such exists.
121 */
122 void
123 scsipi_user_done(struct scsipi_xfer *xs)
124 {
125 struct buf *bp;
126 struct scsi_ioctl *si;
127 scsireq_t *screq;
128 struct scsipi_periph *periph = xs->xs_periph;
129 int s;
130
131 bp = xs->bp;
132 #ifdef DIAGNOSTIC
133 if (bp == NULL) {
134 scsipi_printaddr(periph);
135 printf("user command with no buf\n");
136 panic("scsipi_user_done");
137 }
138 #endif
139 si = si_find(bp);
140 #ifdef DIAGNOSTIC
141 if (si == NULL) {
142 scsipi_printaddr(periph);
143 printf("user command with no ioctl\n");
144 panic("scsipi_user_done");
145 }
146 #endif
147
148 screq = &si->si_screq;
149
150 SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("user-done\n"));
151
152 screq->retsts = 0;
153 screq->status = xs->status;
154 switch (xs->error) {
155 case XS_NOERROR:
156 SC_DEBUG(periph, SCSIPI_DB3, ("no error\n"));
157 screq->datalen_used =
158 xs->datalen - xs->resid; /* probably rubbish */
159 screq->retsts = SCCMD_OK;
160 break;
161 case XS_SENSE:
162 SC_DEBUG(periph, SCSIPI_DB3, ("have sense\n"));
163 screq->senselen_used = min(sizeof(xs->sense.scsi_sense),
164 SENSEBUFLEN);
165 memcpy(screq->sense, &xs->sense.scsi_sense, screq->senselen);
166 screq->retsts = SCCMD_SENSE;
167 break;
168 case XS_SHORTSENSE:
169 SC_DEBUG(periph, SCSIPI_DB3, ("have short sense\n"));
170 screq->senselen_used = min(sizeof(xs->sense.atapi_sense),
171 SENSEBUFLEN);
172 memcpy(screq->sense, &xs->sense.scsi_sense, screq->senselen);
173 screq->retsts = SCCMD_UNKNOWN; /* XXX need a shortsense here */
174 break;
175 case XS_DRIVER_STUFFUP:
176 scsipi_printaddr(periph);
177 printf("passthrough: adapter inconsistency\n");
178 screq->retsts = SCCMD_UNKNOWN;
179 break;
180 case XS_SELTIMEOUT:
181 SC_DEBUG(periph, SCSIPI_DB3, ("seltimeout\n"));
182 screq->retsts = SCCMD_TIMEOUT;
183 break;
184 case XS_TIMEOUT:
185 SC_DEBUG(periph, SCSIPI_DB3, ("timeout\n"));
186 screq->retsts = SCCMD_TIMEOUT;
187 break;
188 case XS_BUSY:
189 SC_DEBUG(periph, SCSIPI_DB3, ("busy\n"));
190 screq->retsts = SCCMD_BUSY;
191 break;
192 default:
193 scsipi_printaddr(periph);
194 printf("unknown error category %d from adapter\n",
195 xs->error);
196 screq->retsts = SCCMD_UNKNOWN;
197 break;
198 }
199
200 if (xs->xs_control & XS_CTL_ASYNC) {
201 s = splbio();
202 scsipi_put_xs(xs);
203 splx(s);
204 }
205 }
206
207
208 /* Pseudo strategy function
209 * Called by scsipi_do_ioctl() via physio/physstrat if there is to
210 * be data transfered, and directly if there is no data transfer.
211 *
212 * Should I reorganize this so it returns to physio instead
213 * of sleeping in scsiio_scsipi_cmd? Is there any advantage, other
214 * than avoiding the probable duplicate wakeup in iodone? [PD]
215 *
216 * No, seems ok to me... [JRE]
217 * (I don't see any duplicate wakeups)
218 *
219 * Can't be used with block devices or raw_read/raw_write directly
220 * from the cdevsw/bdevsw tables because they couldn't have added
221 * the screq structure. [JRE]
222 */
223 static void
224 scsistrategy(struct buf *bp)
225 {
226 struct scsi_ioctl *si;
227 scsireq_t *screq;
228 struct scsipi_periph *periph;
229 int error;
230 int flags = 0;
231
232 si = si_find(bp);
233 if (si == NULL) {
234 printf("scsistrategy: "
235 "No matching ioctl request found in queue\n");
236 error = EINVAL;
237 goto done;
238 }
239 screq = &si->si_screq;
240 periph = si->si_periph;
241 SC_DEBUG(periph, SCSIPI_DB2, ("user_strategy\n"));
242
243 /*
244 * We're in trouble if physio tried to break up the transfer.
245 */
246 if (bp->b_bcount != screq->datalen) {
247 scsipi_printaddr(periph);
248 printf("physio split the request.. cannot proceed\n");
249 error = EIO;
250 goto done;
251 }
252
253 if (screq->timeout == 0) {
254 error = EINVAL;
255 goto done;
256 }
257
258 if (screq->cmdlen > sizeof(struct scsipi_generic)) {
259 scsipi_printaddr(periph);
260 printf("cmdlen too big\n");
261 error = EFAULT;
262 goto done;
263 }
264
265 if ((screq->flags & SCCMD_READ) && screq->datalen > 0)
266 flags |= XS_CTL_DATA_IN;
267 if ((screq->flags & SCCMD_WRITE) && screq->datalen > 0)
268 flags |= XS_CTL_DATA_OUT;
269 if (screq->flags & SCCMD_TARGET)
270 flags |= XS_CTL_TARGET;
271 if (screq->flags & SCCMD_ESCAPE)
272 flags |= XS_CTL_ESCAPE;
273
274 error = scsipi_command(periph, (void *)screq->cmd, screq->cmdlen,
275 (void *)bp->b_data, screq->datalen,
276 0, /* user must do the retries *//* ignored */
277 screq->timeout, bp, flags | XS_CTL_USERCMD);
278
279 done:
280 bp->b_error = error;
281 biodone(bp);
282 return;
283 }
284
285 /*
286 * Something (e.g. another driver) has called us
287 * with a periph and a scsi-specific ioctl to perform,
288 * better try. If user-level type command, we must
289 * still be running in the context of the calling process
290 */
291 int
292 scsipi_do_ioctl(struct scsipi_periph *periph, dev_t dev, u_long cmd,
293 void *addr, int flag, struct lwp *l)
294 {
295 int error;
296
297 SC_DEBUG(periph, SCSIPI_DB2, ("scsipi_do_ioctl(0x%lx)\n", cmd));
298
299 if (addr == NULL)
300 return EINVAL;
301
302 /* Check for the safe-ness of this request. */
303 switch (cmd) {
304 case OSCIOCIDENTIFY:
305 case SCIOCIDENTIFY:
306 break;
307 case SCIOCCOMMAND:
308 if ((((scsireq_t *)addr)->flags & SCCMD_READ) == 0 &&
309 (flag & FWRITE) == 0)
310 return (EBADF);
311 break;
312 default:
313 if ((flag & FWRITE) == 0)
314 return (EBADF);
315 }
316
317 switch (cmd) {
318 case SCIOCCOMMAND: {
319 scsireq_t *screq = (scsireq_t *)addr;
320 struct scsi_ioctl *si;
321 int len;
322
323 si = si_get();
324 si->si_screq = *screq;
325 si->si_periph = periph;
326 len = screq->datalen;
327 if (len) {
328 si->si_iov.iov_base = screq->databuf;
329 si->si_iov.iov_len = len;
330 si->si_uio.uio_iov = &si->si_iov;
331 si->si_uio.uio_iovcnt = 1;
332 si->si_uio.uio_resid = len;
333 si->si_uio.uio_offset = 0;
334 si->si_uio.uio_rw =
335 (screq->flags & SCCMD_READ) ? UIO_READ : UIO_WRITE;
336 if ((flag & FKIOCTL) == 0) {
337 si->si_uio.uio_vmspace = l->l_proc->p_vmspace;
338 } else {
339 UIO_SETUP_SYSSPACE(&si->si_uio);
340 }
341 error = physio(scsistrategy, &si->si_bp, dev,
342 (screq->flags & SCCMD_READ) ? B_READ : B_WRITE,
343 periph->periph_channel->chan_adapter->adapt_minphys,
344 &si->si_uio);
345 } else {
346 /* if no data, no need to translate it.. */
347 si->si_bp.b_flags = 0;
348 si->si_bp.b_data = 0;
349 si->si_bp.b_bcount = 0;
350 si->si_bp.b_dev = dev;
351 si->si_bp.b_proc = l->l_proc;
352 scsistrategy(&si->si_bp);
353 error = si->si_bp.b_error;
354 }
355 *screq = si->si_screq;
356 si_free(si);
357 return (error);
358 }
359 case SCIOCDEBUG: {
360 int level = *((int *)addr);
361
362 SC_DEBUG(periph, SCSIPI_DB3, ("debug set to %d\n", level));
363 periph->periph_dbflags = 0;
364 if (level & 1)
365 periph->periph_dbflags |= SCSIPI_DB1;
366 if (level & 2)
367 periph->periph_dbflags |= SCSIPI_DB2;
368 if (level & 4)
369 periph->periph_dbflags |= SCSIPI_DB3;
370 if (level & 8)
371 periph->periph_dbflags |= SCSIPI_DB4;
372 return (0);
373 }
374 case SCIOCRECONFIG:
375 case SCIOCDECONFIG:
376 return (EINVAL);
377 case SCIOCIDENTIFY: {
378 struct scsi_addr *sca = (struct scsi_addr *)addr;
379
380 switch (scsipi_periph_bustype(periph)) {
381 case SCSIPI_BUSTYPE_SCSI:
382 sca->type = TYPE_SCSI;
383 sca->addr.scsi.scbus =
384 device_unit(device_parent(periph->periph_dev));
385 sca->addr.scsi.target = periph->periph_target;
386 sca->addr.scsi.lun = periph->periph_lun;
387 return (0);
388 case SCSIPI_BUSTYPE_ATAPI:
389 sca->type = TYPE_ATAPI;
390 sca->addr.atapi.atbus =
391 device_unit(device_parent(periph->periph_dev));
392 sca->addr.atapi.drive = periph->periph_target;
393 return (0);
394 }
395 return (ENXIO);
396 }
397 #if defined(COMPAT_12) || defined(COMPAT_FREEBSD)
398 /* SCIOCIDENTIFY before ATAPI staff merge */
399 case OSCIOCIDENTIFY: {
400 struct oscsi_addr *sca = (struct oscsi_addr *)addr;
401
402 switch (scsipi_periph_bustype(periph)) {
403 case SCSIPI_BUSTYPE_SCSI:
404 sca->scbus =
405 device_unit(device_parent(periph->periph_dev));
406 sca->target = periph->periph_target;
407 sca->lun = periph->periph_lun;
408 return (0);
409 }
410 return (ENODEV);
411 }
412 #endif
413 default:
414 return (ENOTTY);
415 }
416
417 #ifdef DIAGNOSTIC
418 panic("scsipi_do_ioctl: impossible");
419 #endif
420 }
421