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