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