scsipi_ioctl.c revision 1.64.6.1 1 1.64.6.1 mjf /* $NetBSD: scsipi_ioctl.c,v 1.64.6.1 2008/06/02 13:23:51 mjf Exp $ */
2 1.7 cgd
3 1.33 mycroft /*-
4 1.49 mycroft * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
5 1.33 mycroft * All rights reserved.
6 1.33 mycroft *
7 1.33 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.33 mycroft * by Charles M. Hannum.
9 1.4 mycroft *
10 1.4 mycroft * Redistribution and use in source and binary forms, with or without
11 1.4 mycroft * modification, are permitted provided that the following conditions
12 1.4 mycroft * are met:
13 1.4 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.4 mycroft * notice, this list of conditions and the following disclaimer.
15 1.4 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.4 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.4 mycroft * documentation and/or other materials provided with the distribution.
18 1.4 mycroft *
19 1.33 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.33 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.33 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.33 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.33 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.33 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.33 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.33 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.33 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.33 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.33 mycroft * POSSIBILITY OF SUCH DAMAGE.
30 1.4 mycroft */
31 1.4 mycroft
32 1.27 enami /*
33 1.1 mycroft * Contributed by HD Associates (hd (at) world.std.com).
34 1.1 mycroft * Copyright (c) 1992, 1993 HD Associates
35 1.1 mycroft *
36 1.27 enami * Berkeley style copyright.
37 1.1 mycroft */
38 1.41 lukem
39 1.41 lukem #include <sys/cdefs.h>
40 1.64.6.1 mjf __KERNEL_RCSID(0, "$NetBSD: scsipi_ioctl.c,v 1.64.6.1 2008/06/02 13:23:51 mjf Exp $");
41 1.30 thorpej
42 1.30 thorpej #include "opt_compat_freebsd.h"
43 1.31 jonathan #include "opt_compat_netbsd.h"
44 1.2 mycroft
45 1.42 lukem #include <sys/param.h>
46 1.1 mycroft #include <sys/errno.h>
47 1.19 thorpej #include <sys/systm.h>
48 1.1 mycroft #include <sys/malloc.h>
49 1.1 mycroft #include <sys/buf.h>
50 1.1 mycroft #include <sys/proc.h>
51 1.1 mycroft #include <sys/device.h>
52 1.21 thorpej #include <sys/fcntl.h>
53 1.1 mycroft
54 1.25 bouyer #include <dev/scsipi/scsipi_all.h>
55 1.25 bouyer #include <dev/scsipi/scsipiconf.h>
56 1.38 bouyer #include <dev/scsipi/scsipi_base.h>
57 1.25 bouyer #include <dev/scsipi/scsiconf.h>
58 1.1 mycroft #include <sys/scsiio.h>
59 1.27 enami
60 1.25 bouyer #include "scsibus.h"
61 1.25 bouyer #include "atapibus.h"
62 1.1 mycroft
63 1.4 mycroft struct scsi_ioctl {
64 1.11 mycroft LIST_ENTRY(scsi_ioctl) si_list;
65 1.11 mycroft struct buf si_bp;
66 1.11 mycroft struct uio si_uio;
67 1.11 mycroft struct iovec si_iov;
68 1.9 mycroft scsireq_t si_screq;
69 1.38 bouyer struct scsipi_periph *si_periph;
70 1.11 mycroft };
71 1.11 mycroft
72 1.47 thorpej static LIST_HEAD(, scsi_ioctl) si_head;
73 1.20 christos
74 1.47 thorpej static struct scsi_ioctl *
75 1.47 thorpej si_get(void)
76 1.4 mycroft {
77 1.4 mycroft struct scsi_ioctl *si;
78 1.11 mycroft int s;
79 1.4 mycroft
80 1.43 tsutsui si = malloc(sizeof(struct scsi_ioctl), M_TEMP, M_WAITOK|M_ZERO);
81 1.64 ad buf_init(&si->si_bp);
82 1.11 mycroft s = splbio();
83 1.11 mycroft LIST_INSERT_HEAD(&si_head, si, si_list);
84 1.11 mycroft splx(s);
85 1.11 mycroft return (si);
86 1.4 mycroft }
87 1.4 mycroft
88 1.47 thorpej static void
89 1.47 thorpej si_free(struct scsi_ioctl *si)
90 1.4 mycroft {
91 1.11 mycroft int s;
92 1.4 mycroft
93 1.11 mycroft s = splbio();
94 1.11 mycroft LIST_REMOVE(si, si_list);
95 1.4 mycroft splx(s);
96 1.64 ad buf_destroy(&si->si_bp);
97 1.11 mycroft free(si, M_TEMP);
98 1.4 mycroft }
99 1.4 mycroft
100 1.47 thorpej static struct scsi_ioctl *
101 1.47 thorpej si_find(struct buf *bp)
102 1.4 mycroft {
103 1.4 mycroft struct scsi_ioctl *si;
104 1.11 mycroft int s;
105 1.4 mycroft
106 1.11 mycroft s = splbio();
107 1.11 mycroft for (si = si_head.lh_first; si != 0; si = si->si_list.le_next)
108 1.11 mycroft if (bp == &si->si_bp)
109 1.11 mycroft break;
110 1.4 mycroft splx(s);
111 1.11 mycroft return (si);
112 1.4 mycroft }
113 1.1 mycroft
114 1.1 mycroft /*
115 1.1 mycroft * We let the user interpret his own sense in the generic scsi world.
116 1.37 thorpej * This routine is called at interrupt time if the XS_CTL_USERCMD bit was set
117 1.25 bouyer * in the flags passed to scsi_scsipi_cmd(). No other completion processing
118 1.1 mycroft * takes place, even if we are running over another device driver.
119 1.1 mycroft * The lower level routines that call us here, will free the xs and restart
120 1.1 mycroft * the device's queue if such exists.
121 1.1 mycroft */
122 1.2 mycroft void
123 1.47 thorpej scsipi_user_done(struct scsipi_xfer *xs)
124 1.1 mycroft {
125 1.2 mycroft struct buf *bp;
126 1.13 mycroft struct scsi_ioctl *si;
127 1.1 mycroft scsireq_t *screq;
128 1.38 bouyer struct scsipi_periph *periph = xs->xs_periph;
129 1.38 bouyer int s;
130 1.1 mycroft
131 1.1 mycroft bp = xs->bp;
132 1.38 bouyer #ifdef DIAGNOSTIC
133 1.38 bouyer if (bp == NULL) {
134 1.38 bouyer scsipi_printaddr(periph);
135 1.38 bouyer printf("user command with no buf\n");
136 1.38 bouyer panic("scsipi_user_done");
137 1.1 mycroft }
138 1.38 bouyer #endif
139 1.4 mycroft si = si_find(bp);
140 1.38 bouyer #ifdef DIAGNOSTIC
141 1.27 enami if (si == NULL) {
142 1.38 bouyer scsipi_printaddr(periph);
143 1.38 bouyer printf("user command with no ioctl\n");
144 1.38 bouyer panic("scsipi_user_done");
145 1.4 mycroft }
146 1.38 bouyer #endif
147 1.38 bouyer
148 1.9 mycroft screq = &si->si_screq;
149 1.38 bouyer
150 1.38 bouyer SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("user-done\n"));
151 1.1 mycroft
152 1.1 mycroft screq->retsts = 0;
153 1.1 mycroft screq->status = xs->status;
154 1.9 mycroft switch (xs->error) {
155 1.2 mycroft case XS_NOERROR:
156 1.38 bouyer SC_DEBUG(periph, SCSIPI_DB3, ("no error\n"));
157 1.27 enami screq->datalen_used =
158 1.27 enami xs->datalen - xs->resid; /* probably rubbish */
159 1.1 mycroft screq->retsts = SCCMD_OK;
160 1.1 mycroft break;
161 1.2 mycroft case XS_SENSE:
162 1.38 bouyer SC_DEBUG(periph, SCSIPI_DB3, ("have sense\n"));
163 1.27 enami screq->senselen_used = min(sizeof(xs->sense.scsi_sense),
164 1.27 enami SENSEBUFLEN);
165 1.38 bouyer memcpy(screq->sense, &xs->sense.scsi_sense, screq->senselen);
166 1.1 mycroft screq->retsts = SCCMD_SENSE;
167 1.35 bouyer break;
168 1.35 bouyer case XS_SHORTSENSE:
169 1.38 bouyer SC_DEBUG(periph, SCSIPI_DB3, ("have short sense\n"));
170 1.35 bouyer screq->senselen_used = min(sizeof(xs->sense.atapi_sense),
171 1.35 bouyer SENSEBUFLEN);
172 1.40 thorpej memcpy(screq->sense, &xs->sense.scsi_sense, screq->senselen);
173 1.35 bouyer screq->retsts = SCCMD_UNKNOWN; /* XXX need a shortsense here */
174 1.1 mycroft break;
175 1.2 mycroft case XS_DRIVER_STUFFUP:
176 1.38 bouyer scsipi_printaddr(periph);
177 1.38 bouyer printf("passthrough: adapter inconsistency\n");
178 1.1 mycroft screq->retsts = SCCMD_UNKNOWN;
179 1.1 mycroft break;
180 1.36 thorpej case XS_SELTIMEOUT:
181 1.38 bouyer SC_DEBUG(periph, SCSIPI_DB3, ("seltimeout\n"));
182 1.36 thorpej screq->retsts = SCCMD_TIMEOUT;
183 1.36 thorpej break;
184 1.2 mycroft case XS_TIMEOUT:
185 1.38 bouyer SC_DEBUG(periph, SCSIPI_DB3, ("timeout\n"));
186 1.1 mycroft screq->retsts = SCCMD_TIMEOUT;
187 1.1 mycroft break;
188 1.2 mycroft case XS_BUSY:
189 1.38 bouyer SC_DEBUG(periph, SCSIPI_DB3, ("busy\n"));
190 1.1 mycroft screq->retsts = SCCMD_BUSY;
191 1.1 mycroft break;
192 1.1 mycroft default:
193 1.38 bouyer scsipi_printaddr(periph);
194 1.38 bouyer printf("unknown error category %d from adapter\n",
195 1.36 thorpej xs->error);
196 1.1 mycroft screq->retsts = SCCMD_UNKNOWN;
197 1.1 mycroft break;
198 1.1 mycroft }
199 1.38 bouyer
200 1.38 bouyer if (xs->xs_control & XS_CTL_ASYNC) {
201 1.38 bouyer s = splbio();
202 1.38 bouyer scsipi_put_xs(xs);
203 1.38 bouyer splx(s);
204 1.38 bouyer }
205 1.1 mycroft }
206 1.1 mycroft
207 1.1 mycroft
208 1.1 mycroft /* Pseudo strategy function
209 1.25 bouyer * Called by scsipi_do_ioctl() via physio/physstrat if there is to
210 1.1 mycroft * be data transfered, and directly if there is no data transfer.
211 1.27 enami *
212 1.1 mycroft * Should I reorganize this so it returns to physio instead
213 1.25 bouyer * of sleeping in scsiio_scsipi_cmd? Is there any advantage, other
214 1.1 mycroft * than avoiding the probable duplicate wakeup in iodone? [PD]
215 1.1 mycroft *
216 1.1 mycroft * No, seems ok to me... [JRE]
217 1.1 mycroft * (I don't see any duplicate wakeups)
218 1.1 mycroft *
219 1.1 mycroft * Can't be used with block devices or raw_read/raw_write directly
220 1.1 mycroft * from the cdevsw/bdevsw tables because they couldn't have added
221 1.1 mycroft * the screq structure. [JRE]
222 1.1 mycroft */
223 1.47 thorpej static void
224 1.47 thorpej scsistrategy(struct buf *bp)
225 1.1 mycroft {
226 1.4 mycroft struct scsi_ioctl *si;
227 1.1 mycroft scsireq_t *screq;
228 1.38 bouyer struct scsipi_periph *periph;
229 1.13 mycroft int error;
230 1.5 mycroft int flags = 0;
231 1.1 mycroft
232 1.4 mycroft si = si_find(bp);
233 1.27 enami if (si == NULL) {
234 1.55 martin printf("scsistrategy: "
235 1.55 martin "No matching ioctl request found in queue\n");
236 1.12 mycroft error = EINVAL;
237 1.63 ad goto done;
238 1.4 mycroft }
239 1.9 mycroft screq = &si->si_screq;
240 1.38 bouyer periph = si->si_periph;
241 1.38 bouyer SC_DEBUG(periph, SCSIPI_DB2, ("user_strategy\n"));
242 1.1 mycroft
243 1.4 mycroft /*
244 1.4 mycroft * We're in trouble if physio tried to break up the transfer.
245 1.1 mycroft */
246 1.1 mycroft if (bp->b_bcount != screq->datalen) {
247 1.38 bouyer scsipi_printaddr(periph);
248 1.23 christos printf("physio split the request.. cannot proceed\n");
249 1.12 mycroft error = EIO;
250 1.63 ad goto done;
251 1.1 mycroft }
252 1.1 mycroft
253 1.1 mycroft if (screq->timeout == 0) {
254 1.12 mycroft error = EINVAL;
255 1.63 ad goto done;
256 1.1 mycroft }
257 1.1 mycroft
258 1.25 bouyer if (screq->cmdlen > sizeof(struct scsipi_generic)) {
259 1.38 bouyer scsipi_printaddr(periph);
260 1.23 christos printf("cmdlen too big\n");
261 1.12 mycroft error = EFAULT;
262 1.63 ad goto done;
263 1.1 mycroft }
264 1.1 mycroft
265 1.54 bouyer if ((screq->flags & SCCMD_READ) && screq->datalen > 0)
266 1.37 thorpej flags |= XS_CTL_DATA_IN;
267 1.54 bouyer if ((screq->flags & SCCMD_WRITE) && screq->datalen > 0)
268 1.37 thorpej flags |= XS_CTL_DATA_OUT;
269 1.1 mycroft if (screq->flags & SCCMD_TARGET)
270 1.37 thorpej flags |= XS_CTL_TARGET;
271 1.1 mycroft if (screq->flags & SCCMD_ESCAPE)
272 1.37 thorpej flags |= XS_CTL_ESCAPE;
273 1.2 mycroft
274 1.50 mycroft error = scsipi_command(periph, (void *)screq->cmd, screq->cmdlen,
275 1.50 mycroft (void *)bp->b_data, screq->datalen,
276 1.4 mycroft 0, /* user must do the retries *//* ignored */
277 1.53 yamt screq->timeout, bp, flags | XS_CTL_USERCMD);
278 1.12 mycroft
279 1.63 ad done:
280 1.63 ad bp->b_error = error;
281 1.12 mycroft biodone(bp);
282 1.53 yamt return;
283 1.1 mycroft }
284 1.1 mycroft
285 1.1 mycroft /*
286 1.1 mycroft * Something (e.g. another driver) has called us
287 1.38 bouyer * with a periph and a scsi-specific ioctl to perform,
288 1.38 bouyer * better try. If user-level type command, we must
289 1.38 bouyer * still be running in the context of the calling process
290 1.1 mycroft */
291 1.2 mycroft int
292 1.52 reinoud scsipi_do_ioctl(struct scsipi_periph *periph, dev_t dev, u_long cmd,
293 1.62 christos void *addr, int flag, struct lwp *l)
294 1.1 mycroft {
295 1.4 mycroft int error;
296 1.1 mycroft
297 1.38 bouyer SC_DEBUG(periph, SCSIPI_DB2, ("scsipi_do_ioctl(0x%lx)\n", cmd));
298 1.21 thorpej
299 1.61 christos if (addr == NULL)
300 1.61 christos return EINVAL;
301 1.61 christos
302 1.21 thorpej /* Check for the safe-ness of this request. */
303 1.21 thorpej switch (cmd) {
304 1.29 bouyer case OSCIOCIDENTIFY:
305 1.21 thorpej case SCIOCIDENTIFY:
306 1.21 thorpej break;
307 1.24 augustss case SCIOCCOMMAND:
308 1.24 augustss if ((((scsireq_t *)addr)->flags & SCCMD_READ) == 0 &&
309 1.24 augustss (flag & FWRITE) == 0)
310 1.27 enami return (EBADF);
311 1.24 augustss break;
312 1.21 thorpej default:
313 1.21 thorpej if ((flag & FWRITE) == 0)
314 1.27 enami return (EBADF);
315 1.21 thorpej }
316 1.11 mycroft
317 1.27 enami switch (cmd) {
318 1.4 mycroft case SCIOCCOMMAND: {
319 1.4 mycroft scsireq_t *screq = (scsireq_t *)addr;
320 1.8 mycroft struct scsi_ioctl *si;
321 1.4 mycroft int len;
322 1.4 mycroft
323 1.11 mycroft si = si_get();
324 1.9 mycroft si->si_screq = *screq;
325 1.38 bouyer si->si_periph = periph;
326 1.16 mycroft len = screq->datalen;
327 1.4 mycroft if (len) {
328 1.11 mycroft si->si_iov.iov_base = screq->databuf;
329 1.8 mycroft si->si_iov.iov_len = len;
330 1.8 mycroft si->si_uio.uio_iov = &si->si_iov;
331 1.8 mycroft si->si_uio.uio_iovcnt = 1;
332 1.15 mycroft si->si_uio.uio_resid = len;
333 1.8 mycroft si->si_uio.uio_offset = 0;
334 1.27 enami si->si_uio.uio_rw =
335 1.8 mycroft (screq->flags & SCCMD_READ) ? UIO_READ : UIO_WRITE;
336 1.58 yamt if ((flag & FKIOCTL) == 0) {
337 1.58 yamt si->si_uio.uio_vmspace = l->l_proc->p_vmspace;
338 1.58 yamt } else {
339 1.58 yamt UIO_SETUP_SYSSPACE(&si->si_uio);
340 1.58 yamt }
341 1.11 mycroft error = physio(scsistrategy, &si->si_bp, dev,
342 1.8 mycroft (screq->flags & SCCMD_READ) ? B_READ : B_WRITE,
343 1.38 bouyer periph->periph_channel->chan_adapter->adapt_minphys,
344 1.38 bouyer &si->si_uio);
345 1.4 mycroft } else {
346 1.4 mycroft /* if no data, no need to translate it.. */
347 1.16 mycroft si->si_bp.b_flags = 0;
348 1.11 mycroft si->si_bp.b_data = 0;
349 1.16 mycroft si->si_bp.b_bcount = 0;
350 1.14 mycroft si->si_bp.b_dev = dev;
351 1.56 christos si->si_bp.b_proc = l->l_proc;
352 1.11 mycroft scsistrategy(&si->si_bp);
353 1.11 mycroft error = si->si_bp.b_error;
354 1.1 mycroft }
355 1.16 mycroft *screq = si->si_screq;
356 1.11 mycroft si_free(si);
357 1.27 enami return (error);
358 1.4 mycroft }
359 1.4 mycroft case SCIOCDEBUG: {
360 1.4 mycroft int level = *((int *)addr);
361 1.1 mycroft
362 1.38 bouyer SC_DEBUG(periph, SCSIPI_DB3, ("debug set to %d\n", level));
363 1.38 bouyer periph->periph_dbflags = 0;
364 1.4 mycroft if (level & 1)
365 1.38 bouyer periph->periph_dbflags |= SCSIPI_DB1;
366 1.4 mycroft if (level & 2)
367 1.38 bouyer periph->periph_dbflags |= SCSIPI_DB2;
368 1.4 mycroft if (level & 4)
369 1.38 bouyer periph->periph_dbflags |= SCSIPI_DB3;
370 1.4 mycroft if (level & 8)
371 1.38 bouyer periph->periph_dbflags |= SCSIPI_DB4;
372 1.27 enami return (0);
373 1.4 mycroft }
374 1.4 mycroft case SCIOCRECONFIG:
375 1.4 mycroft case SCIOCDECONFIG:
376 1.27 enami return (EINVAL);
377 1.4 mycroft case SCIOCIDENTIFY: {
378 1.4 mycroft struct scsi_addr *sca = (struct scsi_addr *)addr;
379 1.27 enami
380 1.38 bouyer switch (scsipi_periph_bustype(periph)) {
381 1.38 bouyer case SCSIPI_BUSTYPE_SCSI:
382 1.25 bouyer sca->type = TYPE_SCSI;
383 1.38 bouyer sca->addr.scsi.scbus =
384 1.60 thorpej device_unit(device_parent(periph->periph_dev));
385 1.38 bouyer sca->addr.scsi.target = periph->periph_target;
386 1.38 bouyer sca->addr.scsi.lun = periph->periph_lun;
387 1.27 enami return (0);
388 1.38 bouyer case SCSIPI_BUSTYPE_ATAPI:
389 1.25 bouyer sca->type = TYPE_ATAPI;
390 1.38 bouyer sca->addr.atapi.atbus =
391 1.60 thorpej device_unit(device_parent(periph->periph_dev));
392 1.38 bouyer sca->addr.atapi.drive = periph->periph_target;
393 1.27 enami return (0);
394 1.25 bouyer }
395 1.27 enami return (ENXIO);
396 1.4 mycroft }
397 1.26 enami #if defined(COMPAT_12) || defined(COMPAT_FREEBSD)
398 1.26 enami /* SCIOCIDENTIFY before ATAPI staff merge */
399 1.26 enami case OSCIOCIDENTIFY: {
400 1.26 enami struct oscsi_addr *sca = (struct oscsi_addr *)addr;
401 1.26 enami
402 1.38 bouyer switch (scsipi_periph_bustype(periph)) {
403 1.38 bouyer case SCSIPI_BUSTYPE_SCSI:
404 1.60 thorpej sca->scbus =
405 1.60 thorpej device_unit(device_parent(periph->periph_dev));
406 1.38 bouyer sca->target = periph->periph_target;
407 1.38 bouyer sca->lun = periph->periph_lun;
408 1.27 enami return (0);
409 1.27 enami }
410 1.27 enami return (ENODEV);
411 1.26 enami }
412 1.26 enami #endif
413 1.4 mycroft default:
414 1.27 enami return (ENOTTY);
415 1.1 mycroft }
416 1.1 mycroft
417 1.4 mycroft #ifdef DIAGNOSTIC
418 1.25 bouyer panic("scsipi_do_ioctl: impossible");
419 1.4 mycroft #endif
420 1.1 mycroft }
421