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