1 /* $NetBSD: umass_scsipi.c,v 1.43 2012/03/11 01:06:07 mrg 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.43 2012/03/11 01:06:07 mrg 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 }; 119 #endif 120 121 122 #if NSCSIBUS > 0 123 int 124 umass_scsi_attach(struct umass_softc *sc) 125 { 126 struct umass_scsipi_softc *scbus; 127 128 scbus = umass_scsipi_setup(sc); 129 130 scbus->sc_channel.chan_bustype = &scsi_bustype; 131 scbus->sc_channel.chan_ntargets = 2; 132 scbus->sc_channel.chan_nluns = sc->maxlun + 1; 133 scbus->sc_channel.chan_id = scbus->sc_channel.chan_ntargets - 1; 134 DPRINTF(UDMASS_USB, ("%s: umass_attach_bus: SCSI\n", 135 device_xname(sc->sc_dev))); 136 137 sc->sc_refcnt++; 138 scbus->base.sc_child = 139 config_found_ia(sc->sc_dev, "scsi", &scbus->sc_channel, 140 scsiprint); 141 if (--sc->sc_refcnt < 0) 142 usb_detach_wakeupold(sc->sc_dev); 143 144 return (0); 145 } 146 #endif 147 148 #if NATAPIBUS > 0 149 int 150 umass_atapi_attach(struct umass_softc *sc) 151 { 152 struct umass_scsipi_softc *scbus; 153 154 scbus = umass_scsipi_setup(sc); 155 scbus->sc_atapi_adapter.atapi_probe_device = umass_atapi_probe_device; 156 157 scbus->sc_channel.chan_bustype = &umass_atapi_bustype; 158 scbus->sc_channel.chan_ntargets = 2; 159 scbus->sc_channel.chan_nluns = 1; 160 161 scbus->sc_channel.chan_defquirks |= sc->sc_busquirks; 162 DPRINTF(UDMASS_USB, ("%s: umass_attach_bus: ATAPI\n", 163 device_xname(sc->sc_dev))); 164 165 sc->sc_refcnt++; 166 scbus->base.sc_child = 167 config_found_ia(sc->sc_dev, "atapi", &scbus->sc_channel, 168 atapiprint); 169 if (--sc->sc_refcnt < 0) 170 usb_detach_wakeupold(sc->sc_dev); 171 172 return (0); 173 } 174 #endif 175 176 Static struct umass_scsipi_softc * 177 umass_scsipi_setup(struct umass_softc *sc) 178 { 179 struct umass_scsipi_softc *scbus; 180 181 scbus = malloc(sizeof *scbus, M_DEVBUF, M_WAITOK | M_ZERO); 182 sc->bus = &scbus->base; 183 184 /* Only use big commands for USB SCSI devices. */ 185 sc->sc_busquirks |= PQUIRK_ONLYBIG; 186 187 /* Fill in the adapter. */ 188 memset(&scbus->sc_adapter, 0, sizeof(scbus->sc_adapter)); 189 scbus->sc_adapter.adapt_dev = sc->sc_dev; 190 scbus->sc_adapter.adapt_nchannels = 1; 191 scbus->sc_adapter.adapt_request = umass_scsipi_request; 192 scbus->sc_adapter.adapt_minphys = umass_scsipi_minphys; 193 scbus->sc_adapter.adapt_ioctl = umass_scsipi_ioctl; 194 scbus->sc_adapter.adapt_getgeom = umass_scsipi_getgeom; 195 196 /* Fill in the channel. */ 197 memset(&scbus->sc_channel, 0, sizeof(scbus->sc_channel)); 198 scbus->sc_channel.chan_adapter = &scbus->sc_adapter; 199 scbus->sc_channel.chan_channel = 0; 200 scbus->sc_channel.chan_flags = SCSIPI_CHAN_OPENINGS | SCSIPI_CHAN_NOSETTLE; 201 scbus->sc_channel.chan_openings = 1; 202 scbus->sc_channel.chan_max_periph = 1; 203 scbus->sc_channel.chan_defquirks |= sc->sc_busquirks; 204 205 return (scbus); 206 } 207 208 Static void 209 umass_scsipi_request(struct scsipi_channel *chan, 210 scsipi_adapter_req_t req, void *arg) 211 { 212 struct scsipi_adapter *adapt = chan->chan_adapter; 213 struct scsipi_periph *periph; 214 struct scsipi_xfer *xs; 215 struct umass_softc *sc = device_private(adapt->adapt_dev); 216 struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus; 217 struct scsipi_generic *cmd; 218 int cmdlen; 219 int dir; 220 #ifdef UMASS_DEBUG 221 microtime(&sc->tv); 222 #endif 223 switch(req) { 224 case ADAPTER_REQ_RUN_XFER: 225 xs = arg; 226 periph = xs->xs_periph; 227 DIF(UDMASS_UPPER, periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS); 228 229 DPRINTF(UDMASS_CMD, ("%s: umass_scsi_cmd: at %"PRIu64".%06"PRIu64": %d:%d " 230 "xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n", 231 device_xname(sc->sc_dev), sc->tv.tv_sec, (uint64_t)sc->tv.tv_usec, 232 periph->periph_target, periph->periph_lun, 233 xs, xs->cmd->opcode, xs->datalen, 234 periph->periph_quirks, xs->xs_control & XS_CTL_POLL)); 235 #if defined(USB_DEBUG) && defined(SCSIPI_DEBUG) 236 if (umassdebug & UDMASS_SCSI) 237 show_scsipi_xs(xs); 238 else if (umassdebug & ~UDMASS_CMD) 239 show_scsipi_cmd(xs); 240 #endif 241 242 if (sc->sc_dying) { 243 xs->error = XS_DRIVER_STUFFUP; 244 goto done; 245 } 246 247 #ifdef UMASS_DEBUG 248 if (chan->chan_bustype->bustype_type == SCSIPI_BUSTYPE_ATAPI ? 249 periph->periph_target != UMASS_ATAPI_DRIVE : 250 periph->periph_target == chan->chan_id) { 251 DPRINTF(UDMASS_SCSI, ("%s: wrong SCSI ID %d\n", 252 device_xname(sc->sc_dev), 253 periph->periph_target)); 254 xs->error = XS_DRIVER_STUFFUP; 255 goto done; 256 } 257 #endif 258 259 cmd = xs->cmd; 260 cmdlen = xs->cmdlen; 261 262 dir = DIR_NONE; 263 if (xs->datalen) { 264 switch (xs->xs_control & 265 (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) { 266 case XS_CTL_DATA_IN: 267 dir = DIR_IN; 268 break; 269 case XS_CTL_DATA_OUT: 270 dir = DIR_OUT; 271 break; 272 } 273 } 274 275 if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) { 276 printf("umass_cmd: large datalen, %d\n", xs->datalen); 277 xs->error = XS_DRIVER_STUFFUP; 278 goto done; 279 } 280 281 if (xs->xs_control & XS_CTL_POLL) { 282 /* Use sync transfer. XXX Broken! */ 283 DPRINTF(UDMASS_SCSI, 284 ("umass_scsi_cmd: sync dir=%d\n", dir)); 285 scbus->sc_sync_status = USBD_INVAL; 286 sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd, 287 cmdlen, xs->data, 288 xs->datalen, dir, 289 xs->timeout, USBD_SYNCHRONOUS, 290 0, xs); 291 DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: done err=%d\n", 292 scbus->sc_sync_status)); 293 switch (scbus->sc_sync_status) { 294 case USBD_NORMAL_COMPLETION: 295 xs->error = XS_NOERROR; 296 break; 297 case USBD_TIMEOUT: 298 xs->error = XS_TIMEOUT; 299 break; 300 default: 301 xs->error = XS_DRIVER_STUFFUP; 302 break; 303 } 304 goto done; 305 } else { 306 DPRINTF(UDMASS_SCSI, 307 ("umass_scsi_cmd: async dir=%d, cmdlen=%d" 308 " datalen=%d\n", 309 dir, cmdlen, xs->datalen)); 310 sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd, 311 cmdlen, xs->data, 312 xs->datalen, dir, 313 xs->timeout, 0, 314 umass_scsipi_cb, xs); 315 return; 316 } 317 318 /* Return if command finishes early. */ 319 done: 320 KERNEL_LOCK(1, curlwp); 321 scsipi_done(xs); 322 KERNEL_UNLOCK_ONE(curlwp); 323 return; 324 default: 325 /* Not supported, nothing to do. */ 326 ; 327 } 328 } 329 330 Static void 331 umass_scsipi_minphys(struct buf *bp) 332 { 333 #ifdef DIAGNOSTIC 334 if (bp->b_bcount <= 0) { 335 printf("umass_scsipi_minphys count(%d) <= 0\n", 336 bp->b_bcount); 337 bp->b_bcount = UMASS_MAX_TRANSFER_SIZE; 338 } 339 #endif 340 if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE) 341 bp->b_bcount = UMASS_MAX_TRANSFER_SIZE; 342 minphys(bp); 343 } 344 345 int 346 umass_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd, 347 void *arg, int flag, proc_t *p) 348 { 349 /*struct umass_softc *sc = link->adapter_softc;*/ 350 /*struct umass_scsipi_softc *scbus = sc->bus;*/ 351 352 switch (cmd) { 353 #if 0 354 case SCBUSIORESET: 355 ccb->ccb_h.status = CAM_REQ_INPROG; 356 umass_reset(sc, umass_cam_cb, (void *) ccb); 357 return (0); 358 #endif 359 default: 360 return (ENOTTY); 361 } 362 } 363 364 Static int 365 umass_scsipi_getgeom(struct scsipi_periph *periph, struct disk_parms *dp, 366 u_long sectors) 367 { 368 struct umass_softc *sc = 369 device_private(periph->periph_channel->chan_adapter->adapt_dev); 370 371 /* If it's not a floppy, we don't know what to do. */ 372 if (sc->sc_cmd != UMASS_CPROTO_UFI) 373 return (0); 374 375 switch (sectors) { 376 case 1440: 377 /* Most likely a single density 3.5" floppy. */ 378 dp->heads = 2; 379 dp->sectors = 9; 380 dp->cyls = 80; 381 return (1); 382 case 2880: 383 /* Most likely a double density 3.5" floppy. */ 384 dp->heads = 2; 385 dp->sectors = 18; 386 dp->cyls = 80; 387 return (1); 388 default: 389 return (0); 390 } 391 } 392 393 Static void 394 umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status) 395 { 396 struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus; 397 struct scsipi_xfer *xs = priv; 398 struct scsipi_periph *periph = xs->xs_periph; 399 int cmdlen; 400 int s; 401 #ifdef UMASS_DEBUG 402 struct timeval tv; 403 u_int delta; 404 microtime(&tv); 405 delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 + tv.tv_usec - sc->tv.tv_usec; 406 #endif 407 408 DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %"PRIu64".%06"PRIu64", delta=%u: xs=%p residue=%d" 409 " status=%d\n", tv.tv_sec, (uint64_t)tv.tv_usec, delta, xs, residue, status)); 410 411 xs->resid = residue; 412 413 switch (status) { 414 case STATUS_CMD_OK: 415 xs->error = XS_NOERROR; 416 break; 417 418 case STATUS_CMD_UNKNOWN: 419 /* FALLTHROUGH */ 420 case STATUS_CMD_FAILED: 421 /* fetch sense data */ 422 sc->sc_sense = 1; 423 memset(&scbus->sc_sense_cmd, 0, sizeof(scbus->sc_sense_cmd)); 424 scbus->sc_sense_cmd.opcode = SCSI_REQUEST_SENSE; 425 scbus->sc_sense_cmd.byte2 = periph->periph_lun << 426 SCSI_CMD_LUN_SHIFT; 427 scbus->sc_sense_cmd.length = sizeof(xs->sense); 428 429 if (sc->sc_cmd == UMASS_CPROTO_UFI || 430 sc->sc_cmd == UMASS_CPROTO_ATAPI) 431 cmdlen = UFI_COMMAND_LENGTH; /* XXX */ 432 else 433 cmdlen = sizeof(scbus->sc_sense_cmd); 434 sc->sc_methods->wire_xfer(sc, periph->periph_lun, 435 &scbus->sc_sense_cmd, cmdlen, 436 &xs->sense, sizeof(xs->sense), 437 DIR_IN, xs->timeout, 0, 438 umass_scsipi_sense_cb, xs); 439 return; 440 441 case STATUS_WIRE_FAILED: 442 xs->error = XS_RESET; 443 break; 444 445 default: 446 panic("%s: Unknown status %d in umass_scsipi_cb", 447 device_xname(sc->sc_dev), status); 448 } 449 450 DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %"PRIu64".%06"PRIu64": return xs->error=" 451 "%d, xs->xs_status=0x%x xs->resid=%d\n", 452 tv.tv_sec, (uint64_t)tv.tv_usec, 453 xs->error, xs->xs_status, xs->resid)); 454 455 s = splbio(); 456 KERNEL_LOCK(1, curlwp); 457 scsipi_done(xs); 458 KERNEL_UNLOCK_ONE(curlwp); 459 splx(s); 460 } 461 462 /* 463 * Finalise a completed autosense operation 464 */ 465 Static void 466 umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue, 467 int status) 468 { 469 struct scsipi_xfer *xs = priv; 470 int s; 471 472 DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: xs=%p residue=%d " 473 "status=%d\n", xs, residue, status)); 474 475 sc->sc_sense = 0; 476 switch (status) { 477 case STATUS_CMD_OK: 478 case STATUS_CMD_UNKNOWN: 479 /* getting sense data succeeded */ 480 if (residue == 0 || residue == 14)/* XXX */ 481 xs->error = XS_SENSE; 482 else 483 xs->error = XS_SHORTSENSE; 484 break; 485 default: 486 DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n", 487 device_xname(sc->sc_dev), status)); 488 xs->error = XS_DRIVER_STUFFUP; 489 break; 490 } 491 492 DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: return xs->error=%d, " 493 "xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status, 494 xs->resid)); 495 496 s = splbio(); 497 KERNEL_LOCK(1, curlwp); 498 scsipi_done(xs); 499 KERNEL_UNLOCK_ONE(curlwp); 500 splx(s); 501 } 502 503 #if NATAPIBUS > 0 504 Static void 505 umass_atapi_probe_device(struct atapibus_softc *atapi, int target) 506 { 507 struct scsipi_channel *chan = atapi->sc_channel; 508 struct scsipi_periph *periph; 509 struct scsipibus_attach_args sa; 510 char vendor[33], product[65], revision[17]; 511 struct scsipi_inquiry_data inqbuf; 512 513 DPRINTF(UDMASS_SCSI,("umass_atapi_probe_device: atapi=%p target=%d\n", 514 atapi, target)); 515 516 if (target != UMASS_ATAPI_DRIVE) /* only probe drive 0 */ 517 return; 518 519 KERNEL_LOCK(1, curlwp); 520 521 /* skip if already attached */ 522 if (scsipi_lookup_periph(chan, target, 0) != NULL) { 523 KERNEL_UNLOCK_ONE(curlwp); 524 return; 525 } 526 527 periph = scsipi_alloc_periph(M_NOWAIT); 528 if (periph == NULL) { 529 KERNEL_UNLOCK_ONE(curlwp); 530 aprint_error_dev(atapi->sc_dev, 531 "can't allocate link for drive %d\n", target); 532 return; 533 } 534 535 DIF(UDMASS_UPPER, periph->periph_dbflags |= 1); /* XXX 1 */ 536 periph->periph_channel = chan; 537 periph->periph_switch = &atapi_probe_periphsw; 538 periph->periph_target = target; 539 periph->periph_quirks = chan->chan_defquirks; 540 541 DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: doing inquiry\n")); 542 /* Now go ask the device all about itself. */ 543 memset(&inqbuf, 0, sizeof(inqbuf)); 544 if (scsipi_inquire(periph, &inqbuf, XS_CTL_DISCOVERY) != 0) { 545 KERNEL_UNLOCK_ONE(curlwp); 546 DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: " 547 "scsipi_inquire failed\n")); 548 free(periph, M_DEVBUF); 549 return; 550 } 551 552 scsipi_strvis(vendor, 33, inqbuf.vendor, 8); 553 scsipi_strvis(product, 65, inqbuf.product, 16); 554 scsipi_strvis(revision, 17, inqbuf.revision, 4); 555 556 sa.sa_periph = periph; 557 sa.sa_inqbuf.type = inqbuf.device; 558 sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ? 559 T_REMOV : T_FIXED; 560 if (sa.sa_inqbuf.removable) 561 periph->periph_flags |= PERIPH_REMOVABLE; 562 sa.sa_inqbuf.vendor = vendor; 563 sa.sa_inqbuf.product = product; 564 sa.sa_inqbuf.revision = revision; 565 sa.sa_inqptr = NULL; 566 567 DPRINTF(UDMASS_SCSI, ("umass_atapi_probedev: doing atapi_probedev on " 568 "'%s' '%s' '%s'\n", vendor, product, revision)); 569 atapi_probe_device(atapi, target, periph, &sa); 570 /* atapi_probe_device() frees the periph when there is no device.*/ 571 572 KERNEL_UNLOCK_ONE(curlwp); 573 } 574 #endif 575