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