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