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