scsipi_ioctl.c revision 1.20 1 /* $NetBSD: scsipi_ioctl.c,v 1.20 1996/02/14 21:47:22 christos 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 (at) 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
48 #include <scsi/scsi_all.h>
49 #include <scsi/scsiconf.h>
50 #include <sys/scsiio.h>
51
52 struct scsi_ioctl {
53 LIST_ENTRY(scsi_ioctl) si_list;
54 struct buf si_bp;
55 struct uio si_uio;
56 struct iovec si_iov;
57 scsireq_t si_screq;
58 struct scsi_link *si_sc_link;
59 };
60
61 LIST_HEAD(, scsi_ioctl) si_head;
62
63 struct scsi_ioctl *si_get __P((void));
64 void si_free __P((struct scsi_ioctl *));
65 struct scsi_ioctl *si_find __P((struct buf *));
66 void scsistrategy __P((struct buf *));
67
68 struct scsi_ioctl *
69 si_get()
70 {
71 struct scsi_ioctl *si;
72 int s;
73
74 si = malloc(sizeof(struct scsi_ioctl), M_TEMP, M_WAITOK);
75 bzero(si, sizeof(struct scsi_ioctl));
76 s = splbio();
77 LIST_INSERT_HEAD(&si_head, si, si_list);
78 splx(s);
79 return (si);
80 }
81
82 void
83 si_free(si)
84 struct scsi_ioctl *si;
85 {
86 int s;
87
88 s = splbio();
89 LIST_REMOVE(si, si_list);
90 splx(s);
91 free(si, M_TEMP);
92 }
93
94 struct scsi_ioctl *
95 si_find(bp)
96 struct buf *bp;
97 {
98 struct scsi_ioctl *si;
99 int s;
100
101 s = splbio();
102 for (si = si_head.lh_first; si != 0; si = si->si_list.le_next)
103 if (bp == &si->si_bp)
104 break;
105 splx(s);
106 return (si);
107 }
108
109 /*
110 * We let the user interpret his own sense in the generic scsi world.
111 * This routine is called at interrupt time if the SCSI_USER bit was set
112 * in the flags passed to scsi_scsi_cmd(). No other completion processing
113 * takes place, even if we are running over another device driver.
114 * The lower level routines that call us here, will free the xs and restart
115 * the device's queue if such exists.
116 */
117 void
118 scsi_user_done(xs)
119 struct scsi_xfer *xs;
120 {
121 struct buf *bp;
122 struct scsi_ioctl *si;
123 scsireq_t *screq;
124 struct scsi_link *sc_link;
125
126 bp = xs->bp;
127 if (!bp) { /* ALL user requests must have a buf */
128 sc_print_addr(xs->sc_link);
129 printf("User command with no buf\n");
130 return;
131 }
132 si = si_find(bp);
133 if (!si) {
134 sc_print_addr(xs->sc_link);
135 printf("User command with no ioctl\n");
136 return;
137 }
138 screq = &si->si_screq;
139 sc_link = si->si_sc_link;
140 SC_DEBUG(xs->sc_link, SDEV_DB2, ("user-done\n"));
141
142 screq->retsts = 0;
143 screq->status = xs->status;
144 switch (xs->error) {
145 case XS_NOERROR:
146 SC_DEBUG(sc_link, SDEV_DB3, ("no error\n"));
147 screq->datalen_used = xs->datalen - xs->resid; /* probably rubbish */
148 screq->retsts = SCCMD_OK;
149 break;
150 case XS_SENSE:
151 SC_DEBUG(sc_link, SDEV_DB3, ("have sense\n"));
152 screq->senselen_used = min(sizeof(xs->sense), SENSEBUFLEN);
153 bcopy(&xs->sense, screq->sense, screq->senselen);
154 screq->retsts = SCCMD_SENSE;
155 break;
156 case XS_DRIVER_STUFFUP:
157 sc_print_addr(sc_link);
158 printf("host adapter code inconsistency\n");
159 screq->retsts = SCCMD_UNKNOWN;
160 break;
161 case XS_TIMEOUT:
162 SC_DEBUG(sc_link, SDEV_DB3, ("timeout\n"));
163 screq->retsts = SCCMD_TIMEOUT;
164 break;
165 case XS_BUSY:
166 SC_DEBUG(sc_link, SDEV_DB3, ("busy\n"));
167 screq->retsts = SCCMD_BUSY;
168 break;
169 default:
170 sc_print_addr(sc_link);
171 printf("unknown error category from host adapter code\n");
172 screq->retsts = SCCMD_UNKNOWN;
173 break;
174 }
175 biodone(bp); /* we're waiting on it in scsi_strategy() */
176 }
177
178
179 /* Pseudo strategy function
180 * Called by scsi_do_ioctl() via physio/physstrat if there is to
181 * be data transfered, and directly if there is no data transfer.
182 *
183 * Should I reorganize this so it returns to physio instead
184 * of sleeping in scsiio_scsi_cmd? Is there any advantage, other
185 * than avoiding the probable duplicate wakeup in iodone? [PD]
186 *
187 * No, seems ok to me... [JRE]
188 * (I don't see any duplicate wakeups)
189 *
190 * Can't be used with block devices or raw_read/raw_write directly
191 * from the cdevsw/bdevsw tables because they couldn't have added
192 * the screq structure. [JRE]
193 */
194 void
195 scsistrategy(bp)
196 struct buf *bp;
197 {
198 struct scsi_ioctl *si;
199 scsireq_t *screq;
200 struct scsi_link *sc_link;
201 int error;
202 int flags = 0;
203 int s;
204
205 si = si_find(bp);
206 if (!si) {
207 printf("user_strat: No ioctl\n");
208 error = EINVAL;
209 goto bad;
210 }
211 screq = &si->si_screq;
212 sc_link = si->si_sc_link;
213 SC_DEBUG(sc_link, SDEV_DB2, ("user_strategy\n"));
214
215 /*
216 * We're in trouble if physio tried to break up the transfer.
217 */
218 if (bp->b_bcount != screq->datalen) {
219 sc_print_addr(sc_link);
220 printf("physio split the request.. cannot proceed\n");
221 error = EIO;
222 goto bad;
223 }
224
225 if (screq->timeout == 0) {
226 error = EINVAL;
227 goto bad;
228 }
229
230 if (screq->cmdlen > sizeof(struct scsi_generic)) {
231 sc_print_addr(sc_link);
232 printf("cmdlen too big\n");
233 error = EFAULT;
234 goto bad;
235 }
236
237 if (screq->flags & SCCMD_READ)
238 flags |= SCSI_DATA_IN;
239 if (screq->flags & SCCMD_WRITE)
240 flags |= SCSI_DATA_OUT;
241 if (screq->flags & SCCMD_TARGET)
242 flags |= SCSI_TARGET;
243 if (screq->flags & SCCMD_ESCAPE)
244 flags |= SCSI_ESCAPE;
245
246 error = scsi_scsi_cmd(sc_link, (struct scsi_generic *)screq->cmd,
247 screq->cmdlen, (u_char *)bp->b_data, screq->datalen,
248 0, /* user must do the retries *//* ignored */
249 screq->timeout, bp, flags | SCSI_USER | SCSI_NOSLEEP);
250
251 /* because there is a bp, scsi_scsi_cmd will return immediatly */
252 if (error)
253 goto bad;
254
255 SC_DEBUG(sc_link, SDEV_DB3, ("about to sleep\n"));
256 s = splbio();
257 while ((bp->b_flags & B_DONE) == 0)
258 tsleep(bp, PRIBIO, "scistr", 0);
259 splx(s);
260 SC_DEBUG(sc_link, SDEV_DB3, ("back from sleep\n"));
261
262 return;
263
264 bad:
265 bp->b_flags |= B_ERROR;
266 bp->b_error = error;
267 biodone(bp);
268 }
269
270 /*
271 * Something (e.g. another driver) has called us
272 * with an sc_link for a target/lun/adapter, and a scsi
273 * specific ioctl to perform, better try.
274 * If user-level type command, we must still be running
275 * in the context of the calling process
276 */
277 int
278 scsi_do_ioctl(sc_link, dev, cmd, addr, flag, p)
279 struct scsi_link *sc_link;
280 dev_t dev;
281 u_long cmd;
282 caddr_t addr;
283 int flag;
284 struct proc *p;
285 {
286 int error;
287
288 SC_DEBUG(sc_link, SDEV_DB2, ("scsi_do_ioctl(0x%lx)\n", cmd));
289
290 switch(cmd) {
291 case SCIOCCOMMAND: {
292 scsireq_t *screq = (scsireq_t *)addr;
293 struct scsi_ioctl *si;
294 int len;
295
296 si = si_get();
297 si->si_screq = *screq;
298 si->si_sc_link = sc_link;
299 len = screq->datalen;
300 if (len) {
301 si->si_iov.iov_base = screq->databuf;
302 si->si_iov.iov_len = len;
303 si->si_uio.uio_iov = &si->si_iov;
304 si->si_uio.uio_iovcnt = 1;
305 si->si_uio.uio_resid = len;
306 si->si_uio.uio_offset = 0;
307 si->si_uio.uio_segflg = UIO_USERSPACE;
308 si->si_uio.uio_rw =
309 (screq->flags & SCCMD_READ) ? UIO_READ : UIO_WRITE;
310 si->si_uio.uio_procp = p;
311 error = physio(scsistrategy, &si->si_bp, dev,
312 (screq->flags & SCCMD_READ) ? B_READ : B_WRITE,
313 sc_link->adapter->scsi_minphys, &si->si_uio);
314 } else {
315 /* if no data, no need to translate it.. */
316 si->si_bp.b_flags = 0;
317 si->si_bp.b_data = 0;
318 si->si_bp.b_bcount = 0;
319 si->si_bp.b_dev = dev;
320 si->si_bp.b_proc = p;
321 scsistrategy(&si->si_bp);
322 error = si->si_bp.b_error;
323 }
324 *screq = si->si_screq;
325 si_free(si);
326 return error;
327 }
328 case SCIOCDEBUG: {
329 int level = *((int *)addr);
330
331 SC_DEBUG(sc_link, SDEV_DB3, ("debug set to %d\n", level));
332 sc_link->flags &= ~SDEV_DBX; /* clear debug bits */
333 if (level & 1)
334 sc_link->flags |= SDEV_DB1;
335 if (level & 2)
336 sc_link->flags |= SDEV_DB2;
337 if (level & 4)
338 sc_link->flags |= SDEV_DB3;
339 if (level & 8)
340 sc_link->flags |= SDEV_DB4;
341 return 0;
342 }
343 case SCIOCREPROBE: {
344 struct scsi_addr *sca = (struct scsi_addr *)addr;
345
346 return scsi_probe_busses(sca->scbus, sca->target, sca->lun);
347 }
348 case SCIOCRECONFIG:
349 case SCIOCDECONFIG:
350 return EINVAL;
351 case SCIOCIDENTIFY: {
352 struct scsi_addr *sca = (struct scsi_addr *)addr;
353
354 sca->scbus = sc_link->scsibus;
355 sca->target = sc_link->target;
356 sca->lun = sc_link->lun;
357 return 0;
358 }
359 default:
360 return ENOTTY;
361 }
362
363 #ifdef DIAGNOSTIC
364 panic("scsi_do_ioctl: impossible");
365 #endif
366 }
367