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