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