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