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