1 /* $OpenBSD: scsi_ioctl.c,v 1.20 2005/05/28 04:08:39 krw Exp $ */
2 /* $NetBSD: scsi_ioctl.c,v 1.23 1996/10/12 23:23:17 christos Exp $ */
3
4 /*
5 * Copyright (c) 1994 Charles Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Charles Hannum.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Contributed by HD Associates (hd@world.std.com).
35 * Copyright (c) 1992, 1993 HD Associates
36 *
37 * Berkeley style copyright.
38 */
39
40 #include <sys/types.h>
41 #include <sys/errno.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/file.h>
45 #include <sys/malloc.h>
46 #include <sys/buf.h>
47 #include <sys/proc.h>
48 #include <sys/device.h>
49 #include <sys/fcntl.h>
50
51 #include <scsi/scsi_all.h>
52 #include <scsi/scsiconf.h>
53 #include <sys/scsiio.h>
54
55 struct scsi_ioctl {
56 LIST_ENTRY(scsi_ioctl) si_list;
57 struct buf si_bp;
58 struct uio si_uio;
59 struct iovec si_iov;
60 scsireq_t si_screq;
61 struct scsi_link *si_sc_link;
62 };
63
64 LIST_HEAD(, scsi_ioctl) si_head;
65
66 struct scsi_ioctl *si_get(void);
67 void si_free(struct scsi_ioctl *);
68 struct scsi_ioctl *si_find(struct buf *);
69 void scsistrategy(struct buf *);
70
71 const unsigned char scsi_readsafe_cmd[256] = {
72 [0x00] = 1, /* TEST UNIT READY */
73 [0x03] = 1, /* REQUEST SENSE */
74 [0x08] = 1, /* READ(6) */
75 [0x12] = 1, /* INQUIRY */
76 [0x1a] = 1, /* MODE SENSE */
77 [0x1b] = 1, /* START STOP */
78 [0x23] = 1, /* READ FORMAT CAPACITIES */
79 [0x25] = 1, /* READ CDVD CAPACITY */
80 [0x28] = 1, /* READ(10) */
81 [0x2b] = 1, /* SEEK */
82 [0x2f] = 1, /* VERIFY(10) */
83 [0x3c] = 1, /* READ BUFFER */
84 [0x3e] = 1, /* READ LONG */
85 [0x42] = 1, /* READ SUBCHANNEL */
86 [0x43] = 1, /* READ TOC PMA ATIP */
87 [0x44] = 1, /* READ HEADER */
88 [0x45] = 1, /* PLAY AUDIO(10) */
89 [0x46] = 1, /* GET CONFIGURATION */
90 [0x47] = 1, /* PLAY AUDIO MSF */
91 [0x48] = 1, /* PLAY AUDIO TI */
92 [0x4a] = 1, /* GET EVENT STATUS NOTIFICATION */
93 [0x4b] = 1, /* PAUSE RESUME */
94 [0x4e] = 1, /* STOP PLAY SCAN */
95 [0x51] = 1, /* READ DISC INFO */
96 [0x52] = 1, /* READ TRACK RZONE INFO */
97 [0x5a] = 1, /* MODE SENSE(10) */
98 [0x88] = 1, /* READ(16) */
99 [0x8f] = 1, /* VERIFY(16) */
100 [0xa4] = 1, /* REPORT KEY */
101 [0xa5] = 1, /* PLAY AUDIO(12) */
102 [0xa8] = 1, /* READ(12) */
103 [0xac] = 1, /* GET PERFORMANCE */
104 [0xad] = 1, /* READ DVD STRUCTURE */
105 [0xb9] = 1, /* READ CD MSF */
106 [0xba] = 1, /* SCAN */
107 [0xbc] = 1, /* PLAY CD */
108 [0xbd] = 1, /* MECHANISM STATUS */
109 [0xbe] = 1 /* READ CD */
110 };
111
112 struct scsi_ioctl *
si_get(void)113 si_get(void)
114 {
115 struct scsi_ioctl *si;
116 int s;
117
118 si = malloc(sizeof(struct scsi_ioctl), M_TEMP, M_WAITOK);
119 bzero(si, sizeof(struct scsi_ioctl));
120 s = splbio();
121 LIST_INSERT_HEAD(&si_head, si, si_list);
122 splx(s);
123 return (si);
124 }
125
126 void
si_free(struct scsi_ioctl * si)127 si_free(struct scsi_ioctl *si)
128 {
129 int s;
130
131 s = splbio();
132 LIST_REMOVE(si, si_list);
133 splx(s);
134 free(si, M_TEMP);
135 }
136
137 struct scsi_ioctl *
si_find(struct buf * bp)138 si_find(struct buf *bp)
139 {
140 struct scsi_ioctl *si;
141 int s;
142
143 s = splbio();
144 LIST_FOREACH(si, &si_head, si_list)
145 if (bp == &si->si_bp)
146 break;
147 splx(s);
148 return (si);
149 }
150
151 /*
152 * We let the user interpret his own sense in the generic scsi world.
153 * This routine is called at interrupt time if the SCSI_USER bit was set
154 * in the flags passed to scsi_scsi_cmd(). No other completion processing
155 * takes place, even if we are running over another device driver.
156 * The lower level routines that call us here, will free the xs and restart
157 * the device's queue if such exists.
158 */
159 void
scsi_user_done(struct scsi_xfer * xs)160 scsi_user_done(struct scsi_xfer *xs)
161 {
162 struct buf *bp;
163 struct scsi_ioctl *si;
164 scsireq_t *screq;
165 struct scsi_link *sc_link;
166
167 splassert(IPL_BIO);
168
169 bp = xs->bp;
170 if (!bp) { /* ALL user requests must have a buf */
171 sc_print_addr(xs->sc_link);
172 printf("User command with no buf\n");
173 return;
174 }
175 si = si_find(bp);
176 if (!si) {
177 sc_print_addr(xs->sc_link);
178 printf("User command with no ioctl\n");
179 return;
180 }
181 screq = &si->si_screq;
182 sc_link = si->si_sc_link;
183 SC_DEBUG(xs->sc_link, SDEV_DB2, ("user-done\n"));
184
185 screq->retsts = 0;
186 screq->status = xs->status;
187 switch (xs->error) {
188 case XS_NOERROR:
189 SC_DEBUG(sc_link, SDEV_DB3, ("no error\n"));
190 screq->datalen_used = xs->datalen - xs->resid; /* probably rubbish */
191 screq->retsts = SCCMD_OK;
192 break;
193 case XS_SENSE:
194 SC_DEBUG(sc_link, SDEV_DB3, ("have sense\n"));
195 screq->senselen_used = min(sizeof(xs->sense), SENSEBUFLEN);
196 bcopy(&xs->sense, screq->sense, screq->senselen);
197 screq->retsts = SCCMD_SENSE;
198 break;
199 case XS_SHORTSENSE:
200 SC_DEBUG(sc_link, SDEV_DB3, ("have short sense\n"));
201 screq->senselen_used = min(sizeof(xs->sense), SENSEBUFLEN);
202 bcopy(&xs->sense, screq->sense, screq->senselen);
203 screq->retsts = SCCMD_UNKNOWN;
204 break;
205 case XS_DRIVER_STUFFUP:
206 sc_print_addr(sc_link);
207 printf("host adapter code inconsistency\n");
208 screq->retsts = SCCMD_UNKNOWN;
209 break;
210 case XS_TIMEOUT:
211 SC_DEBUG(sc_link, SDEV_DB3, ("timeout\n"));
212 screq->retsts = SCCMD_TIMEOUT;
213 break;
214 case XS_BUSY:
215 SC_DEBUG(sc_link, SDEV_DB3, ("busy\n"));
216 screq->retsts = SCCMD_BUSY;
217 break;
218 default:
219 sc_print_addr(sc_link);
220 printf("unknown error category (0x%x) from host adapter code\n",
221 xs->error);
222 screq->retsts = SCCMD_UNKNOWN;
223 break;
224 }
225 biodone(bp); /* we're waiting on it in scsi_strategy() */
226 }
227
228
229 /* Pseudo strategy function
230 * Called by scsi_do_ioctl() via physio/physstrat if there is to
231 * be data transferred, and directly if there is no data transfer.
232 *
233 * Should I reorganize this so it returns to physio instead
234 * of sleeping in scsiio_scsi_cmd? Is there any advantage, other
235 * than avoiding the probable duplicate wakeup in iodone? [PD]
236 *
237 * No, seems ok to me... [JRE]
238 * (I don't see any duplicate wakeups)
239 *
240 * Can't be used with block devices or raw_read/raw_write directly
241 * from the cdevsw/bdevsw tables because they couldn't have added
242 * the screq structure. [JRE]
243 */
244 void
scsistrategy(struct buf * bp)245 scsistrategy(struct buf *bp)
246 {
247 struct scsi_ioctl *si;
248 scsireq_t *screq;
249 struct scsi_link *sc_link;
250 int error;
251 int flags = 0;
252 int s;
253
254 si = si_find(bp);
255 if (!si) {
256 printf("user_strat: No ioctl\n");
257 error = EINVAL;
258 goto bad;
259 }
260 screq = &si->si_screq;
261 sc_link = si->si_sc_link;
262 SC_DEBUG(sc_link, SDEV_DB2, ("user_strategy\n"));
263
264 /*
265 * We're in trouble if physio tried to break up the transfer.
266 */
267 if (bp->b_bcount != screq->datalen) {
268 sc_print_addr(sc_link);
269 printf("physio split the request.. cannot proceed\n");
270 error = EIO;
271 goto bad;
272 }
273
274 if (screq->timeout == 0) {
275 error = EINVAL;
276 goto bad;
277 }
278
279 if (screq->cmdlen > sizeof(struct scsi_generic)) {
280 sc_print_addr(sc_link);
281 printf("cmdlen too big\n");
282 error = EFAULT;
283 goto bad;
284 }
285
286 if (screq->flags & SCCMD_READ)
287 flags |= SCSI_DATA_IN;
288 if (screq->flags & SCCMD_WRITE)
289 flags |= SCSI_DATA_OUT;
290 if (screq->flags & SCCMD_TARGET)
291 flags |= SCSI_TARGET;
292 if (screq->flags & SCCMD_ESCAPE)
293 flags |= SCSI_ESCAPE;
294
295 error = scsi_scsi_cmd(sc_link, (struct scsi_generic *)screq->cmd,
296 screq->cmdlen, (u_char *)bp->b_data, screq->datalen,
297 0, /* user must do the retries *//* ignored */
298 screq->timeout, bp, flags | SCSI_USER | SCSI_NOSLEEP);
299
300 /* because there is a bp, scsi_scsi_cmd will return immediatly */
301 if (error)
302 goto bad;
303
304 SC_DEBUG(sc_link, SDEV_DB3, ("about to sleep\n"));
305 s = splbio();
306 while ((bp->b_flags & B_DONE) == 0)
307 tsleep(bp, PRIBIO, "scistr", 0);
308 splx(s);
309 SC_DEBUG(sc_link, SDEV_DB3, ("back from sleep\n"));
310
311 return;
312
313 bad:
314 bp->b_flags |= B_ERROR;
315 bp->b_error = error;
316 s = splbio();
317 biodone(bp);
318 splx(s);
319 }
320
321 /*
322 * Something (e.g. another driver) has called us
323 * with an sc_link for a target/lun/adapter, and a scsi
324 * specific ioctl to perform, better try.
325 * If user-level type command, we must still be running
326 * in the context of the calling process
327 */
328 int
scsi_do_ioctl(struct scsi_link * sc_link,dev_t dev,u_long cmd,caddr_t addr,int flag,struct proc * p)329 scsi_do_ioctl( struct scsi_link *sc_link, dev_t dev, u_long cmd, caddr_t addr,
330 int flag, struct proc *p)
331 {
332 int error;
333
334 SC_DEBUG(sc_link, SDEV_DB2, ("scsi_do_ioctl(0x%lx)\n", cmd));
335
336 switch(cmd) {
337 case OSCIOCIDENTIFY: {
338 struct oscsi_addr *sca = (struct oscsi_addr *)addr;
339
340 sca->scbus = sc_link->scsibus;
341 sca->target = sc_link->target;
342 sca->lun = sc_link->lun;
343 return (0);
344 }
345 case SCIOCIDENTIFY: {
346 struct scsi_addr *sca = (struct scsi_addr *)addr;
347
348 sca->type = (sc_link->flags & SDEV_ATAPI)
349 ? TYPE_ATAPI : TYPE_SCSI;
350 sca->scbus = sc_link->scsibus;
351 sca->target = sc_link->target;
352 sca->lun = sc_link->lun;
353 return (0);
354 }
355 case SCIOCRECONFIG:
356 case SCIOCDECONFIG:
357 return (EINVAL);
358 case SCIOCCOMMAND:
359 if (scsi_readsafe_cmd[((scsireq_t *)addr)->cmd[0]])
360 break;
361 /* FALLTHROUGH */
362 case SCIOCDEBUG:
363 case SCIOCREPROBE:
364 case OSCIOCREPROBE:
365 case SCIOCRESET:
366 if ((flag & FWRITE) == 0)
367 return (EPERM);
368 break;
369 default:
370 if (sc_link->adapter->ioctl)
371 return ((sc_link->adapter->ioctl)(sc_link, cmd, addr,
372 flag, p));
373 else
374 return (ENOTTY);
375 }
376
377 switch(cmd) {
378 case SCIOCCOMMAND: {
379 scsireq_t *screq = (scsireq_t *)addr;
380 struct scsi_ioctl *si;
381 int len;
382
383 si = si_get();
384 si->si_screq = *screq;
385 si->si_sc_link = sc_link;
386 len = screq->datalen;
387 if (len) {
388 si->si_iov.iov_base = screq->databuf;
389 si->si_iov.iov_len = len;
390 si->si_uio.uio_iov = &si->si_iov;
391 si->si_uio.uio_iovcnt = 1;
392 si->si_uio.uio_resid = len;
393 si->si_uio.uio_offset = 0;
394 si->si_uio.uio_segflg = UIO_USERSPACE;
395 si->si_uio.uio_rw =
396 (screq->flags & SCCMD_READ) ? UIO_READ : UIO_WRITE;
397 si->si_uio.uio_procp = p;
398 error = physio(scsistrategy, &si->si_bp, dev,
399 (screq->flags & SCCMD_READ) ? B_READ : B_WRITE,
400 sc_link->adapter->scsi_minphys, &si->si_uio);
401 } else {
402 /* if no data, no need to translate it.. */
403 si->si_bp.b_flags = 0;
404 si->si_bp.b_data = 0;
405 si->si_bp.b_bcount = 0;
406 si->si_bp.b_dev = dev;
407 si->si_bp.b_proc = p;
408 scsistrategy(&si->si_bp);
409 error = si->si_bp.b_error;
410 }
411 *screq = si->si_screq;
412 si_free(si);
413 return (error);
414 }
415 case SCIOCDEBUG: {
416 int level = *((int *)addr);
417
418 SC_DEBUG(sc_link, SDEV_DB3, ("debug set to %d\n", level));
419 sc_link->flags &= ~SDEV_DBX; /* clear debug bits */
420 if (level & 1)
421 sc_link->flags |= SDEV_DB1;
422 if (level & 2)
423 sc_link->flags |= SDEV_DB2;
424 if (level & 4)
425 sc_link->flags |= SDEV_DB3;
426 if (level & 8)
427 sc_link->flags |= SDEV_DB4;
428 return (0);
429 }
430 case OSCIOCREPROBE: {
431 struct oscsi_addr *sca = (struct oscsi_addr *)addr;
432
433 return (scsi_probe_busses(sca->scbus, sca->target, sca->lun));
434 }
435 case SCIOCREPROBE: {
436 struct scsi_addr *sca = (struct scsi_addr *)addr;
437
438 return (scsi_probe_busses(sca->scbus, sca->target, sca->lun));
439 }
440 case SCIOCRESET: {
441 scsi_scsi_cmd(sc_link, 0, 0, 0, 0, GENRETRY, 2000, NULL,
442 SCSI_RESET);
443 return (0);
444 }
445 default:
446 #ifdef DIAGNOSTIC
447 panic("scsi_do_ioctl: impossible");
448 #endif
449 return (0);
450 }
451 }
452