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