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