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