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