1 /* $NetBSD: umass_scsipi.c,v 1.46 2012/08/20 10:32:31 drochner Exp $ */ 2 3 /* 4 * Copyright (c) 2001, 2003, 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net) at 9 * Carlstedt Research & Technology, Charles M. Hamnnum and Matthew R. Green. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: umass_scsipi.c,v 1.46 2012/08/20 10:32:31 drochner Exp $"); 35 36 #ifdef _KERNEL_OPT 37 #include "opt_umass.h" 38 #endif 39 40 #include "atapibus.h" 41 #include "scsibus.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/conf.h> 47 #include <sys/buf.h> 48 #include <sys/bufq.h> 49 #include <sys/device.h> 50 #include <sys/ioctl.h> 51 #include <sys/lwp.h> 52 #include <sys/malloc.h> 53 54 /* SCSI & ATAPI */ 55 #include <sys/scsiio.h> 56 #include <dev/scsipi/scsi_spc.h> 57 #include <dev/scsipi/scsi_all.h> 58 #include <dev/scsipi/scsipi_all.h> 59 #include <dev/scsipi/scsiconf.h> 60 61 #include <dev/scsipi/atapiconf.h> 62 63 #include <dev/scsipi/scsipi_disk.h> 64 #include <dev/scsipi/scsi_disk.h> 65 #include <dev/scsipi/scsi_changer.h> 66 67 #include <sys/disk.h> /* XXX */ 68 #include <dev/scsipi/sdvar.h> /* XXX */ 69 70 /* USB */ 71 #include <dev/usb/usb.h> 72 #include <dev/usb/usbdi.h> 73 #include <dev/usb/usbdi_util.h> 74 #include <dev/usb/usbdevs.h> 75 76 #include <dev/usb/umassvar.h> 77 #include <dev/usb/umass_scsipi.h> 78 79 struct umass_scsipi_softc { 80 struct umassbus_softc base; 81 82 struct atapi_adapter sc_atapi_adapter; 83 #define sc_adapter sc_atapi_adapter._generic 84 struct scsipi_channel sc_channel; 85 usbd_status sc_sync_status; 86 struct scsi_request_sense sc_sense_cmd; 87 }; 88 89 90 #define SHORT_INQUIRY_LENGTH 36 /* XXX */ 91 92 #define UMASS_ATAPI_DRIVE 0 93 94 Static void umass_scsipi_request(struct scsipi_channel *, 95 scsipi_adapter_req_t, void *); 96 Static void umass_scsipi_minphys(struct buf *bp); 97 Static int umass_scsipi_ioctl(struct scsipi_channel *, u_long, 98 void *, int, proc_t *); 99 Static int umass_scsipi_getgeom(struct scsipi_periph *periph, 100 struct disk_parms *, u_long sectors); 101 102 Static void umass_scsipi_cb(struct umass_softc *sc, void *priv, 103 int residue, int status); 104 Static void umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, 105 int residue, int status); 106 107 Static struct umass_scsipi_softc *umass_scsipi_setup(struct umass_softc *sc); 108 109 #if NATAPIBUS > 0 110 Static void umass_atapi_probe_device(struct atapibus_softc *, int); 111 112 const struct scsipi_bustype umass_atapi_bustype = { 113 SCSIPI_BUSTYPE_ATAPI, 114 atapi_scsipi_cmd, 115 atapi_interpret_sense, 116 atapi_print_addr, 117 scsi_kill_pending, 118 NULL, 119 }; 120 #endif 121 122 123 #if NSCSIBUS > 0 124 int 125 umass_scsi_attach(struct umass_softc *sc) 126 { 127 struct umass_scsipi_softc *scbus; 128 129 scbus = umass_scsipi_setup(sc); 130 131 scbus->sc_channel.chan_bustype = &scsi_bustype; 132 scbus->sc_channel.chan_ntargets = 2; 133 scbus->sc_channel.chan_nluns = sc->maxlun + 1; 134 scbus->sc_channel.chan_id = scbus->sc_channel.chan_ntargets - 1; 135 DPRINTF(UDMASS_USB, ("%s: umass_attach_bus: SCSI\n", 136 device_xname(sc->sc_dev))); 137 138 sc->sc_refcnt++; 139 scbus->base.sc_child = 140 config_found_ia(sc->sc_dev, "scsi", &scbus->sc_channel, 141 scsiprint); 142 if (--sc->sc_refcnt < 0) 143 usb_detach_wakeupold(sc->sc_dev); 144 145 return (0); 146 } 147 #endif 148 149 #if NATAPIBUS > 0 150 int 151 umass_atapi_attach(struct umass_softc *sc) 152 { 153 struct umass_scsipi_softc *scbus; 154 155 scbus = umass_scsipi_setup(sc); 156 scbus->sc_atapi_adapter.atapi_probe_device = umass_atapi_probe_device; 157 158 scbus->sc_channel.chan_bustype = &umass_atapi_bustype; 159 scbus->sc_channel.chan_ntargets = 2; 160 scbus->sc_channel.chan_nluns = 1; 161 162 scbus->sc_channel.chan_defquirks |= sc->sc_busquirks; 163 DPRINTF(UDMASS_USB, ("%s: umass_attach_bus: ATAPI\n", 164 device_xname(sc->sc_dev))); 165 166 sc->sc_refcnt++; 167 scbus->base.sc_child = 168 config_found_ia(sc->sc_dev, "atapi", &scbus->sc_channel, 169 atapiprint); 170 if (--sc->sc_refcnt < 0) 171 usb_detach_wakeupold(sc->sc_dev); 172 173 return (0); 174 } 175 #endif 176 177 Static struct umass_scsipi_softc * 178 umass_scsipi_setup(struct umass_softc *sc) 179 { 180 struct umass_scsipi_softc *scbus; 181 182 scbus = malloc(sizeof *scbus, M_DEVBUF, M_WAITOK | M_ZERO); 183 sc->bus = &scbus->base; 184 185 /* Only use big commands for USB SCSI devices. */ 186 sc->sc_busquirks |= PQUIRK_ONLYBIG; 187 188 /* Fill in the adapter. */ 189 memset(&scbus->sc_adapter, 0, sizeof(scbus->sc_adapter)); 190 scbus->sc_adapter.adapt_dev = sc->sc_dev; 191 scbus->sc_adapter.adapt_nchannels = 1; 192 scbus->sc_adapter.adapt_request = umass_scsipi_request; 193 scbus->sc_adapter.adapt_minphys = umass_scsipi_minphys; 194 scbus->sc_adapter.adapt_ioctl = umass_scsipi_ioctl; 195 scbus->sc_adapter.adapt_getgeom = umass_scsipi_getgeom; 196 197 /* Fill in the channel. */ 198 memset(&scbus->sc_channel, 0, sizeof(scbus->sc_channel)); 199 scbus->sc_channel.chan_adapter = &scbus->sc_adapter; 200 scbus->sc_channel.chan_channel = 0; 201 scbus->sc_channel.chan_flags = SCSIPI_CHAN_OPENINGS | SCSIPI_CHAN_NOSETTLE; 202 scbus->sc_channel.chan_openings = 1; 203 scbus->sc_channel.chan_max_periph = 1; 204 scbus->sc_channel.chan_defquirks |= sc->sc_busquirks; 205 206 return (scbus); 207 } 208 209 Static void 210 umass_scsipi_request(struct scsipi_channel *chan, 211 scsipi_adapter_req_t req, void *arg) 212 { 213 struct scsipi_adapter *adapt = chan->chan_adapter; 214 struct scsipi_periph *periph; 215 struct scsipi_xfer *xs; 216 struct umass_softc *sc = device_private(adapt->adapt_dev); 217 struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus; 218 struct scsipi_generic *cmd; 219 int cmdlen; 220 int dir; 221 #ifdef UMASS_DEBUG 222 microtime(&sc->tv); 223 #endif 224 switch(req) { 225 case ADAPTER_REQ_RUN_XFER: 226 xs = arg; 227 periph = xs->xs_periph; 228 DIF(UDMASS_UPPER, periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS); 229 230 DPRINTF(UDMASS_CMD, ("%s: umass_scsi_cmd: at %"PRIu64".%06"PRIu64": %d:%d " 231 "xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n", 232 device_xname(sc->sc_dev), sc->tv.tv_sec, (uint64_t)sc->tv.tv_usec, 233 periph->periph_target, periph->periph_lun, 234 xs, xs->cmd->opcode, xs->datalen, 235 periph->periph_quirks, xs->xs_control & XS_CTL_POLL)); 236 #if defined(USB_DEBUG) && defined(SCSIPI_DEBUG) 237 if (umassdebug & UDMASS_SCSI) 238 show_scsipi_xs(xs); 239 else if (umassdebug & ~UDMASS_CMD) 240 show_scsipi_cmd(xs); 241 #endif 242 243 if (sc->sc_dying) { 244 xs->error = XS_DRIVER_STUFFUP; 245 goto done; 246 } 247 248 #ifdef UMASS_DEBUG 249 if (SCSIPI_BUSTYPE_TYPE(chan->chan_bustype->bustype_type) == 250 SCSIPI_BUSTYPE_ATAPI ? 251 periph->periph_target != UMASS_ATAPI_DRIVE : 252 periph->periph_target == chan->chan_id) { 253 DPRINTF(UDMASS_SCSI, ("%s: wrong SCSI ID %d\n", 254 device_xname(sc->sc_dev), 255 periph->periph_target)); 256 xs->error = XS_DRIVER_STUFFUP; 257 goto done; 258 } 259 #endif 260 261 cmd = xs->cmd; 262 cmdlen = xs->cmdlen; 263 264 dir = DIR_NONE; 265 if (xs->datalen) { 266 switch (xs->xs_control & 267 (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) { 268 case XS_CTL_DATA_IN: 269 dir = DIR_IN; 270 break; 271 case XS_CTL_DATA_OUT: 272 dir = DIR_OUT; 273 break; 274 } 275 } 276 277 if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) { 278 printf("umass_cmd: large datalen, %d\n", xs->datalen); 279 xs->error = XS_DRIVER_STUFFUP; 280 goto done; 281 } 282 283 if (xs->xs_control & XS_CTL_POLL) { 284 /* Use sync transfer. XXX Broken! */ 285 DPRINTF(UDMASS_SCSI, 286 ("umass_scsi_cmd: sync dir=%d\n", dir)); 287 scbus->sc_sync_status = USBD_INVAL; 288 sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd, 289 cmdlen, xs->data, 290 xs->datalen, dir, 291 xs->timeout, USBD_SYNCHRONOUS, 292 0, xs); 293 DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: done err=%d\n", 294 scbus->sc_sync_status)); 295 switch (scbus->sc_sync_status) { 296 case USBD_NORMAL_COMPLETION: 297 xs->error = XS_NOERROR; 298 break; 299 case USBD_TIMEOUT: 300 xs->error = XS_TIMEOUT; 301 break; 302 default: 303 xs->error = XS_DRIVER_STUFFUP; 304 break; 305 } 306 goto done; 307 } else { 308 DPRINTF(UDMASS_SCSI, 309 ("umass_scsi_cmd: async dir=%d, cmdlen=%d" 310 " datalen=%d\n", 311 dir, cmdlen, xs->datalen)); 312 sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd, 313 cmdlen, xs->data, 314 xs->datalen, dir, 315 xs->timeout, 0, 316 umass_scsipi_cb, xs); 317 return; 318 } 319 320 /* Return if command finishes early. */ 321 done: 322 KERNEL_LOCK(1, curlwp); 323 scsipi_done(xs); 324 KERNEL_UNLOCK_ONE(curlwp); 325 return; 326 default: 327 /* Not supported, nothing to do. */ 328 ; 329 } 330 } 331 332 Static void 333 umass_scsipi_minphys(struct buf *bp) 334 { 335 #ifdef DIAGNOSTIC 336 if (bp->b_bcount <= 0) { 337 printf("umass_scsipi_minphys count(%d) <= 0\n", 338 bp->b_bcount); 339 bp->b_bcount = UMASS_MAX_TRANSFER_SIZE; 340 } 341 #endif 342 if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE) 343 bp->b_bcount = UMASS_MAX_TRANSFER_SIZE; 344 minphys(bp); 345 } 346 347 int 348 umass_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd, 349 void *arg, int flag, proc_t *p) 350 { 351 /*struct umass_softc *sc = link->adapter_softc;*/ 352 /*struct umass_scsipi_softc *scbus = sc->bus;*/ 353 354 switch (cmd) { 355 #if 0 356 case SCBUSIORESET: 357 ccb->ccb_h.status = CAM_REQ_INPROG; 358 umass_reset(sc, umass_cam_cb, (void *) ccb); 359 return (0); 360 #endif 361 default: 362 return (ENOTTY); 363 } 364 } 365 366 Static int 367 umass_scsipi_getgeom(struct scsipi_periph *periph, struct disk_parms *dp, 368 u_long sectors) 369 { 370 struct umass_softc *sc = 371 device_private(periph->periph_channel->chan_adapter->adapt_dev); 372 373 /* If it's not a floppy, we don't know what to do. */ 374 if (sc->sc_cmd != UMASS_CPROTO_UFI) 375 return (0); 376 377 switch (sectors) { 378 case 1440: 379 /* Most likely a single density 3.5" floppy. */ 380 dp->heads = 2; 381 dp->sectors = 9; 382 dp->cyls = 80; 383 return (1); 384 case 2880: 385 /* Most likely a double density 3.5" floppy. */ 386 dp->heads = 2; 387 dp->sectors = 18; 388 dp->cyls = 80; 389 return (1); 390 default: 391 return (0); 392 } 393 } 394 395 Static void 396 umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status) 397 { 398 struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus; 399 struct scsipi_xfer *xs = priv; 400 struct scsipi_periph *periph = xs->xs_periph; 401 int cmdlen, senselen; 402 int s; 403 #ifdef UMASS_DEBUG 404 struct timeval tv; 405 u_int delta; 406 microtime(&tv); 407 delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 + tv.tv_usec - sc->tv.tv_usec; 408 #endif 409 410 DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %"PRIu64".%06"PRIu64", delta=%u: xs=%p residue=%d" 411 " status=%d\n", tv.tv_sec, (uint64_t)tv.tv_usec, delta, xs, residue, status)); 412 413 xs->resid = residue; 414 415 switch (status) { 416 case STATUS_CMD_OK: 417 xs->error = XS_NOERROR; 418 break; 419 420 case STATUS_CMD_UNKNOWN: 421 /* FALLTHROUGH */ 422 case STATUS_CMD_FAILED: 423 /* fetch sense data */ 424 sc->sc_sense = 1; 425 memset(&scbus->sc_sense_cmd, 0, sizeof(scbus->sc_sense_cmd)); 426 scbus->sc_sense_cmd.opcode = SCSI_REQUEST_SENSE; 427 scbus->sc_sense_cmd.byte2 = periph->periph_lun << 428 SCSI_CMD_LUN_SHIFT; 429 430 if (sc->sc_cmd == UMASS_CPROTO_UFI || 431 sc->sc_cmd == UMASS_CPROTO_ATAPI) 432 cmdlen = UFI_COMMAND_LENGTH; /* XXX */ 433 else 434 cmdlen = sizeof(scbus->sc_sense_cmd); 435 if (periph->periph_version < 0x05) /* SPC-3 */ 436 senselen = 18; 437 else 438 senselen = sizeof(xs->sense); 439 scbus->sc_sense_cmd.length = senselen; 440 sc->sc_methods->wire_xfer(sc, periph->periph_lun, 441 &scbus->sc_sense_cmd, cmdlen, 442 &xs->sense, senselen, 443 DIR_IN, xs->timeout, 0, 444 umass_scsipi_sense_cb, xs); 445 return; 446 447 case STATUS_WIRE_FAILED: 448 xs->error = XS_RESET; 449 break; 450 451 default: 452 panic("%s: Unknown status %d in umass_scsipi_cb", 453 device_xname(sc->sc_dev), status); 454 } 455 456 DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %"PRIu64".%06"PRIu64": return xs->error=" 457 "%d, xs->xs_status=0x%x xs->resid=%d\n", 458 tv.tv_sec, (uint64_t)tv.tv_usec, 459 xs->error, xs->xs_status, xs->resid)); 460 461 s = splbio(); 462 KERNEL_LOCK(1, curlwp); 463 scsipi_done(xs); 464 KERNEL_UNLOCK_ONE(curlwp); 465 splx(s); 466 } 467 468 /* 469 * Finalise a completed autosense operation 470 */ 471 Static void 472 umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue, 473 int status) 474 { 475 struct scsipi_xfer *xs = priv; 476 int s; 477 478 DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: xs=%p residue=%d " 479 "status=%d\n", xs, residue, status)); 480 481 sc->sc_sense = 0; 482 switch (status) { 483 case STATUS_CMD_OK: 484 case STATUS_CMD_UNKNOWN: 485 /* getting sense data succeeded */ 486 if (residue == 0 || residue == 14)/* XXX */ 487 xs->error = XS_SENSE; 488 else 489 xs->error = XS_SHORTSENSE; 490 break; 491 default: 492 DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n", 493 device_xname(sc->sc_dev), status)); 494 xs->error = XS_DRIVER_STUFFUP; 495 break; 496 } 497 498 DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: return xs->error=%d, " 499 "xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status, 500 xs->resid)); 501 502 s = splbio(); 503 KERNEL_LOCK(1, curlwp); 504 scsipi_done(xs); 505 KERNEL_UNLOCK_ONE(curlwp); 506 splx(s); 507 } 508 509 #if NATAPIBUS > 0 510 Static void 511 umass_atapi_probe_device(struct atapibus_softc *atapi, int target) 512 { 513 struct scsipi_channel *chan = atapi->sc_channel; 514 struct scsipi_periph *periph; 515 struct scsipibus_attach_args sa; 516 char vendor[33], product[65], revision[17]; 517 struct scsipi_inquiry_data inqbuf; 518 519 DPRINTF(UDMASS_SCSI,("umass_atapi_probe_device: atapi=%p target=%d\n", 520 atapi, target)); 521 522 if (target != UMASS_ATAPI_DRIVE) /* only probe drive 0 */ 523 return; 524 525 KERNEL_LOCK(1, curlwp); 526 527 /* skip if already attached */ 528 if (scsipi_lookup_periph(chan, target, 0) != NULL) { 529 KERNEL_UNLOCK_ONE(curlwp); 530 return; 531 } 532 533 periph = scsipi_alloc_periph(M_NOWAIT); 534 if (periph == NULL) { 535 KERNEL_UNLOCK_ONE(curlwp); 536 aprint_error_dev(atapi->sc_dev, 537 "can't allocate link for drive %d\n", target); 538 return; 539 } 540 541 DIF(UDMASS_UPPER, periph->periph_dbflags |= 1); /* XXX 1 */ 542 periph->periph_channel = chan; 543 periph->periph_switch = &atapi_probe_periphsw; 544 periph->periph_target = target; 545 periph->periph_quirks = chan->chan_defquirks; 546 547 DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: doing inquiry\n")); 548 /* Now go ask the device all about itself. */ 549 memset(&inqbuf, 0, sizeof(inqbuf)); 550 if (scsipi_inquire(periph, &inqbuf, XS_CTL_DISCOVERY) != 0) { 551 KERNEL_UNLOCK_ONE(curlwp); 552 DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: " 553 "scsipi_inquire failed\n")); 554 free(periph, M_DEVBUF); 555 return; 556 } 557 558 scsipi_strvis(vendor, 33, inqbuf.vendor, 8); 559 scsipi_strvis(product, 65, inqbuf.product, 16); 560 scsipi_strvis(revision, 17, inqbuf.revision, 4); 561 562 sa.sa_periph = periph; 563 sa.sa_inqbuf.type = inqbuf.device; 564 sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ? 565 T_REMOV : T_FIXED; 566 if (sa.sa_inqbuf.removable) 567 periph->periph_flags |= PERIPH_REMOVABLE; 568 sa.sa_inqbuf.vendor = vendor; 569 sa.sa_inqbuf.product = product; 570 sa.sa_inqbuf.revision = revision; 571 sa.sa_inqptr = NULL; 572 573 DPRINTF(UDMASS_SCSI, ("umass_atapi_probedev: doing atapi_probedev on " 574 "'%s' '%s' '%s'\n", vendor, product, revision)); 575 atapi_probe_device(atapi, target, periph, &sa); 576 /* atapi_probe_device() frees the periph when there is no device.*/ 577 578 KERNEL_UNLOCK_ONE(curlwp); 579 } 580 #endif 581