Home | History | Annotate | Line # | Download | only in usb
umass_scsipi.c revision 1.68
      1  1.68  riastrad /*	$NetBSD: umass_scsipi.c,v 1.68 2021/05/23 08:42:32 riastradh Exp $	*/
      2   1.1  augustss 
      3   1.1  augustss /*
      4  1.43       mrg  * Copyright (c) 2001, 2003, 2012 The NetBSD Foundation, Inc.
      5   1.1  augustss  * All rights reserved.
      6   1.1  augustss  *
      7   1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  1.43       mrg  * Carlstedt Research & Technology, Charles M. Hamnnum and Matthew R. Green.
     10   1.1  augustss  *
     11   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12   1.1  augustss  * modification, are permitted provided that the following conditions
     13   1.1  augustss  * are met:
     14   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19   1.1  augustss  *
     20   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1  augustss  */
     32   1.1  augustss 
     33   1.1  augustss #include <sys/cdefs.h>
     34  1.68  riastrad __KERNEL_RCSID(0, "$NetBSD: umass_scsipi.c,v 1.68 2021/05/23 08:42:32 riastradh Exp $");
     35  1.37  christos 
     36  1.38   mbalmer #ifdef _KERNEL_OPT
     37  1.49     skrll #include "opt_usb.h"
     38  1.38   mbalmer #endif
     39   1.1  augustss 
     40   1.1  augustss #include "atapibus.h"
     41   1.1  augustss #include "scsibus.h"
     42   1.1  augustss 
     43   1.1  augustss #include <sys/param.h>
     44   1.1  augustss #include <sys/buf.h>
     45  1.21      yamt #include <sys/bufq.h>
     46  1.57     skrll #include <sys/conf.h>
     47   1.1  augustss #include <sys/device.h>
     48  1.57     skrll #include <sys/disk.h>		/* XXX */
     49   1.1  augustss #include <sys/ioctl.h>
     50  1.57     skrll #include <sys/kernel.h>
     51  1.59     skrll #include <sys/kmem.h>
     52  1.40        he #include <sys/lwp.h>
     53   1.1  augustss #include <sys/malloc.h>
     54  1.57     skrll #include <sys/systm.h>
     55   1.1  augustss 
     56   1.1  augustss /* SCSI & ATAPI */
     57   1.1  augustss #include <sys/scsiio.h>
     58  1.24   thorpej #include <dev/scsipi/scsi_spc.h>
     59   1.1  augustss #include <dev/scsipi/scsi_all.h>
     60   1.1  augustss #include <dev/scsipi/scsipi_all.h>
     61   1.1  augustss #include <dev/scsipi/scsiconf.h>
     62   1.1  augustss 
     63   1.1  augustss #include <dev/scsipi/atapiconf.h>
     64   1.1  augustss 
     65   1.1  augustss #include <dev/scsipi/scsipi_disk.h>
     66   1.1  augustss #include <dev/scsipi/scsi_disk.h>
     67   1.1  augustss #include <dev/scsipi/scsi_changer.h>
     68   1.1  augustss 
     69   1.1  augustss #include <dev/scsipi/sdvar.h>	/* XXX */
     70   1.1  augustss 
     71   1.1  augustss /* USB */
     72   1.1  augustss #include <dev/usb/usb.h>
     73   1.1  augustss #include <dev/usb/usbdi.h>
     74   1.1  augustss #include <dev/usb/usbdi_util.h>
     75   1.1  augustss #include <dev/usb/usbdevs.h>
     76  1.52     skrll #include <dev/usb/usbhist.h>
     77   1.1  augustss 
     78   1.1  augustss #include <dev/usb/umassvar.h>
     79   1.1  augustss #include <dev/usb/umass_scsipi.h>
     80   1.1  augustss 
     81   1.1  augustss struct umass_scsipi_softc {
     82   1.1  augustss 	struct umassbus_softc	base;
     83   1.1  augustss 
     84   1.1  augustss 	struct atapi_adapter	sc_atapi_adapter;
     85   1.1  augustss #define sc_adapter sc_atapi_adapter._generic
     86   1.1  augustss 	struct scsipi_channel sc_channel;
     87   1.1  augustss 	usbd_status		sc_sync_status;
     88  1.24   thorpej 	struct scsi_request_sense	sc_sense_cmd;
     89   1.1  augustss };
     90   1.1  augustss 
     91   1.1  augustss 
     92   1.1  augustss #define SHORT_INQUIRY_LENGTH    36 /* XXX */
     93   1.1  augustss 
     94   1.1  augustss #define UMASS_ATAPI_DRIVE	0
     95   1.1  augustss 
     96   1.1  augustss Static void umass_scsipi_request(struct scsipi_channel *,
     97   1.1  augustss 				 scsipi_adapter_req_t, void *);
     98  1.50     skrll Static void umass_scsipi_minphys(struct buf *);
     99   1.1  augustss Static int umass_scsipi_ioctl(struct scsipi_channel *, u_long,
    100  1.36    dyoung 			      void *, int, proc_t *);
    101  1.50     skrll Static int umass_scsipi_getgeom(struct scsipi_periph *,
    102  1.50     skrll 				struct disk_parms *, u_long);
    103   1.1  augustss 
    104  1.68  riastrad Static void umass_null_cb(struct umass_softc *, void *,
    105  1.68  riastrad 			  int, int);
    106  1.50     skrll Static void umass_scsipi_cb(struct umass_softc *, void *,
    107  1.50     skrll 			    int, int);
    108  1.50     skrll Static void umass_scsipi_sense_cb(struct umass_softc *, void *,
    109  1.50     skrll 				  int, int);
    110   1.1  augustss 
    111  1.50     skrll Static struct umass_scsipi_softc *umass_scsipi_setup(struct umass_softc *);
    112   1.1  augustss 
    113   1.1  augustss #if NATAPIBUS > 0
    114   1.1  augustss Static void umass_atapi_probe_device(struct atapibus_softc *, int);
    115   1.1  augustss 
    116   1.1  augustss const struct scsipi_bustype umass_atapi_bustype = {
    117  1.64  riastrad 	.bustype_type = SCSIPI_BUSTYPE_ATAPI,
    118  1.64  riastrad 	.bustype_cmd = atapi_scsipi_cmd,
    119  1.64  riastrad 	.bustype_interpret_sense = atapi_interpret_sense,
    120  1.64  riastrad 	.bustype_printaddr = atapi_print_addr,
    121  1.64  riastrad 	.bustype_kill_pending = scsi_kill_pending,
    122  1.64  riastrad 	.bustype_async_event_xfer_mode = NULL,
    123   1.1  augustss };
    124   1.1  augustss #endif
    125   1.1  augustss 
    126   1.1  augustss 
    127   1.1  augustss #if NSCSIBUS > 0
    128   1.1  augustss int
    129   1.1  augustss umass_scsi_attach(struct umass_softc *sc)
    130   1.1  augustss {
    131  1.52     skrll 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
    132   1.1  augustss 	struct umass_scsipi_softc *scbus;
    133   1.1  augustss 
    134   1.1  augustss 	scbus = umass_scsipi_setup(sc);
    135   1.1  augustss 
    136   1.1  augustss 	scbus->sc_channel.chan_bustype = &scsi_bustype;
    137  1.16   mycroft 	scbus->sc_channel.chan_ntargets = 2;
    138   1.1  augustss 	scbus->sc_channel.chan_nluns = sc->maxlun + 1;
    139  1.16   mycroft 	scbus->sc_channel.chan_id = scbus->sc_channel.chan_ntargets - 1;
    140  1.55  pgoyette 	DPRINTFM(UDMASS_USB, "sc %#jx: SCSI", (uintptr_t)sc, 0, 0, 0);
    141   1.9  augustss 
    142  1.56   mlelstv 	mutex_enter(&sc->sc_lock);
    143   1.9  augustss 	sc->sc_refcnt++;
    144  1.56   mlelstv 	mutex_exit(&sc->sc_lock);
    145   1.1  augustss 	scbus->base.sc_child =
    146  1.67   thorpej 	    config_found(sc->sc_dev, &scbus->sc_channel, scsiprint,
    147  1.67   thorpej 			 CFARG_IATTR, "scsi",
    148  1.67   thorpej 			 CFARG_EOL);
    149  1.56   mlelstv 	mutex_enter(&sc->sc_lock);
    150   1.9  augustss 	if (--sc->sc_refcnt < 0)
    151  1.58     skrll 		cv_broadcast(&sc->sc_detach_cv);
    152  1.56   mlelstv 	mutex_exit(&sc->sc_lock);
    153  1.56   mlelstv 
    154   1.1  augustss 
    155  1.50     skrll 	return 0;
    156   1.1  augustss }
    157  1.60  jdolecek 
    158  1.60  jdolecek void
    159  1.60  jdolecek umass_scsi_detach(struct umass_softc *sc)
    160  1.60  jdolecek {
    161  1.60  jdolecek 	struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
    162  1.60  jdolecek 
    163  1.60  jdolecek 	kmem_free(scbus, sizeof(*scbus));
    164  1.60  jdolecek 	sc->bus = NULL;
    165  1.60  jdolecek }
    166   1.1  augustss #endif
    167   1.1  augustss 
    168   1.1  augustss #if NATAPIBUS > 0
    169   1.1  augustss int
    170   1.1  augustss umass_atapi_attach(struct umass_softc *sc)
    171   1.1  augustss {
    172  1.52     skrll 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
    173   1.1  augustss 	struct umass_scsipi_softc *scbus;
    174   1.1  augustss 
    175   1.1  augustss 	scbus = umass_scsipi_setup(sc);
    176   1.1  augustss 	scbus->sc_atapi_adapter.atapi_probe_device =  umass_atapi_probe_device;
    177   1.1  augustss 
    178   1.1  augustss 	scbus->sc_channel.chan_bustype = &umass_atapi_bustype;
    179   1.1  augustss 	scbus->sc_channel.chan_ntargets = 2;
    180   1.1  augustss 	scbus->sc_channel.chan_nluns = 1;
    181   1.1  augustss 
    182   1.2  augustss 	scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
    183  1.55  pgoyette 	DPRINTFM(UDMASS_USB, "sc %#jxp: ATAPI", (uintptr_t)sc, 0, 0, 0);
    184   1.9  augustss 
    185  1.56   mlelstv 	mutex_enter(&sc->sc_lock);
    186   1.9  augustss 	sc->sc_refcnt++;
    187  1.56   mlelstv 	mutex_exit(&sc->sc_lock);
    188   1.1  augustss 	scbus->base.sc_child =
    189  1.67   thorpej 	    config_found(sc->sc_dev, &scbus->sc_channel, atapiprint,
    190  1.67   thorpej 			 CFARG_IATTR, "atapi",
    191  1.67   thorpej 			 CFARG_EOL);
    192  1.56   mlelstv 	mutex_enter(&sc->sc_lock);
    193   1.9  augustss 	if (--sc->sc_refcnt < 0)
    194  1.58     skrll 		cv_broadcast(&sc->sc_detach_cv);
    195  1.56   mlelstv 	mutex_exit(&sc->sc_lock);
    196   1.1  augustss 
    197  1.50     skrll 	return 0;
    198   1.1  augustss }
    199  1.60  jdolecek 
    200  1.60  jdolecek void
    201  1.60  jdolecek umass_atapi_detach(struct umass_softc *sc)
    202  1.60  jdolecek {
    203  1.60  jdolecek 	struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
    204  1.60  jdolecek 
    205  1.60  jdolecek 	kmem_free(scbus, sizeof(*scbus));
    206  1.60  jdolecek 	sc->bus = NULL;
    207  1.60  jdolecek }
    208   1.1  augustss #endif
    209   1.1  augustss 
    210   1.1  augustss Static struct umass_scsipi_softc *
    211   1.1  augustss umass_scsipi_setup(struct umass_softc *sc)
    212   1.1  augustss {
    213   1.1  augustss 	struct umass_scsipi_softc *scbus;
    214   1.1  augustss 
    215  1.59     skrll 	scbus = kmem_zalloc(sizeof(*scbus), KM_SLEEP);
    216   1.1  augustss 	sc->bus = &scbus->base;
    217   1.3  augustss 
    218   1.3  augustss 	/* Only use big commands for USB SCSI devices. */
    219  1.61    kardel 	/* Do not ask for timeouts.  */
    220  1.61    kardel 	sc->sc_busquirks |= PQUIRK_ONLYBIG|PQUIRK_NOREPSUPPOPC;
    221   1.1  augustss 
    222   1.1  augustss 	/* Fill in the adapter. */
    223   1.1  augustss 	memset(&scbus->sc_adapter, 0, sizeof(scbus->sc_adapter));
    224  1.33      cube 	scbus->sc_adapter.adapt_dev = sc->sc_dev;
    225   1.1  augustss 	scbus->sc_adapter.adapt_nchannels = 1;
    226   1.1  augustss 	scbus->sc_adapter.adapt_request = umass_scsipi_request;
    227   1.1  augustss 	scbus->sc_adapter.adapt_minphys = umass_scsipi_minphys;
    228   1.1  augustss 	scbus->sc_adapter.adapt_ioctl = umass_scsipi_ioctl;
    229   1.1  augustss 	scbus->sc_adapter.adapt_getgeom = umass_scsipi_getgeom;
    230  1.54     skrll 	scbus->sc_adapter.adapt_flags = SCSIPI_ADAPT_MPSAFE;
    231   1.1  augustss 
    232   1.1  augustss 	/* Fill in the channel. */
    233   1.1  augustss 	memset(&scbus->sc_channel, 0, sizeof(scbus->sc_channel));
    234   1.1  augustss 	scbus->sc_channel.chan_adapter = &scbus->sc_adapter;
    235   1.1  augustss 	scbus->sc_channel.chan_channel = 0;
    236  1.15   mycroft 	scbus->sc_channel.chan_flags = SCSIPI_CHAN_OPENINGS | SCSIPI_CHAN_NOSETTLE;
    237   1.1  augustss 	scbus->sc_channel.chan_openings = 1;
    238   1.1  augustss 	scbus->sc_channel.chan_max_periph = 1;
    239   1.2  augustss 	scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
    240   1.1  augustss 
    241  1.50     skrll 	return scbus;
    242   1.1  augustss }
    243   1.5  augustss 
    244   1.1  augustss Static void
    245   1.1  augustss umass_scsipi_request(struct scsipi_channel *chan,
    246   1.1  augustss 		scsipi_adapter_req_t req, void *arg)
    247   1.1  augustss {
    248  1.52     skrll 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
    249   1.1  augustss 	struct scsipi_adapter *adapt = chan->chan_adapter;
    250   1.1  augustss 	struct scsipi_periph *periph;
    251   1.1  augustss 	struct scsipi_xfer *xs;
    252  1.33      cube 	struct umass_softc *sc = device_private(adapt->adapt_dev);
    253   1.1  augustss 	struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
    254  1.13   mycroft 	struct scsipi_generic *cmd;
    255   1.1  augustss 	int cmdlen;
    256   1.1  augustss 	int dir;
    257   1.1  augustss #ifdef UMASS_DEBUG
    258   1.1  augustss 	microtime(&sc->tv);
    259   1.1  augustss #endif
    260   1.1  augustss 	switch(req) {
    261   1.1  augustss 	case ADAPTER_REQ_RUN_XFER:
    262   1.1  augustss 		xs = arg;
    263   1.1  augustss 		periph = xs->xs_periph;
    264   1.1  augustss 		DIF(UDMASS_UPPER, periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS);
    265   1.1  augustss 
    266  1.55  pgoyette 		DPRINTFM(UDMASS_CMD, "sc %#jxp: %jd:%jd xs=%#jxp",
    267  1.55  pgoyette 		    (uintptr_t)sc, periph->periph_target, periph->periph_lun,
    268  1.55  pgoyette 		    (uintptr_t)xs);
    269  1.66  christos 		DPRINTFM(UDMASS_CMD, "cmd=0x%02jx datalen=%jd (quirks=%#jx, "
    270  1.55  pgoyette 		    "poll=%jd)", xs->cmd->opcode, xs->datalen,
    271  1.52     skrll 		    periph->periph_quirks, !!(xs->xs_control & XS_CTL_POLL));
    272  1.48  christos #if defined(UMASS_DEBUG) && defined(SCSIPI_DEBUG)
    273   1.1  augustss 		if (umassdebug & UDMASS_SCSI)
    274   1.1  augustss 			show_scsipi_xs(xs);
    275   1.1  augustss 		else if (umassdebug & ~UDMASS_CMD)
    276   1.1  augustss 			show_scsipi_cmd(xs);
    277   1.1  augustss #endif
    278   1.1  augustss 
    279   1.1  augustss 		if (sc->sc_dying) {
    280   1.1  augustss 			xs->error = XS_DRIVER_STUFFUP;
    281   1.1  augustss 			goto done;
    282   1.1  augustss 		}
    283   1.1  augustss 
    284   1.1  augustss #ifdef UMASS_DEBUG
    285  1.44    bouyer 		if (SCSIPI_BUSTYPE_TYPE(chan->chan_bustype->bustype_type) ==
    286  1.44    bouyer 		    SCSIPI_BUSTYPE_ATAPI ?
    287   1.1  augustss 		    periph->periph_target != UMASS_ATAPI_DRIVE :
    288  1.16   mycroft 		    periph->periph_target == chan->chan_id) {
    289  1.55  pgoyette 			DPRINTFM(UDMASS_SCSI, "sc %#jx: wrong SCSI ID %jd",
    290  1.55  pgoyette 			    (uintptr_t)sc, periph->periph_target, 0, 0);
    291   1.1  augustss 			xs->error = XS_DRIVER_STUFFUP;
    292   1.1  augustss 			goto done;
    293   1.1  augustss 		}
    294   1.1  augustss #endif
    295   1.1  augustss 
    296   1.1  augustss 		cmd = xs->cmd;
    297   1.1  augustss 		cmdlen = xs->cmdlen;
    298   1.1  augustss 
    299   1.1  augustss 		dir = DIR_NONE;
    300   1.1  augustss 		if (xs->datalen) {
    301   1.1  augustss 			switch (xs->xs_control &
    302   1.1  augustss 			    (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
    303   1.1  augustss 			case XS_CTL_DATA_IN:
    304   1.1  augustss 				dir = DIR_IN;
    305   1.1  augustss 				break;
    306   1.1  augustss 			case XS_CTL_DATA_OUT:
    307   1.1  augustss 				dir = DIR_OUT;
    308   1.1  augustss 				break;
    309   1.1  augustss 			}
    310   1.1  augustss 		}
    311   1.1  augustss 
    312   1.1  augustss 		if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) {
    313   1.1  augustss 			printf("umass_cmd: large datalen, %d\n", xs->datalen);
    314   1.1  augustss 			xs->error = XS_DRIVER_STUFFUP;
    315   1.1  augustss 			goto done;
    316   1.1  augustss 		}
    317   1.1  augustss 
    318   1.1  augustss 		if (xs->xs_control & XS_CTL_POLL) {
    319   1.1  augustss 			/* Use sync transfer. XXX Broken! */
    320  1.55  pgoyette 			DPRINTFM(UDMASS_SCSI, "sync dir=%jd\n", dir, 0, 0, 0);
    321   1.1  augustss 			scbus->sc_sync_status = USBD_INVAL;
    322   1.1  augustss 			sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
    323   1.1  augustss 						  cmdlen, xs->data,
    324   1.1  augustss 						  xs->datalen, dir,
    325  1.41       mrg 						  xs->timeout, USBD_SYNCHRONOUS,
    326  1.68  riastrad 						  umass_null_cb, xs);
    327  1.55  pgoyette 			DPRINTFM(UDMASS_SCSI, "done err=%jd",
    328  1.52     skrll 			    scbus->sc_sync_status, 0, 0, 0);
    329   1.1  augustss 			switch (scbus->sc_sync_status) {
    330   1.1  augustss 			case USBD_NORMAL_COMPLETION:
    331   1.1  augustss 				xs->error = XS_NOERROR;
    332   1.1  augustss 				break;
    333   1.1  augustss 			case USBD_TIMEOUT:
    334   1.1  augustss 				xs->error = XS_TIMEOUT;
    335   1.1  augustss 				break;
    336   1.1  augustss 			default:
    337   1.1  augustss 				xs->error = XS_DRIVER_STUFFUP;
    338   1.1  augustss 				break;
    339   1.1  augustss 			}
    340   1.1  augustss 			goto done;
    341   1.1  augustss 		} else {
    342  1.55  pgoyette 			DPRINTFM(UDMASS_SCSI, "async dir=%jd, cmdlen=%jd"
    343  1.55  pgoyette 			    " datalen=%jd", dir, cmdlen, xs->datalen, 0);
    344   1.1  augustss 			sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
    345   1.1  augustss 						  cmdlen, xs->data,
    346   1.1  augustss 						  xs->datalen, dir,
    347  1.41       mrg 						  xs->timeout, 0,
    348   1.1  augustss 						  umass_scsipi_cb, xs);
    349   1.1  augustss 			return;
    350   1.1  augustss 		}
    351   1.1  augustss 
    352   1.1  augustss 		/* Return if command finishes early. */
    353   1.1  augustss  done:
    354   1.1  augustss 		scsipi_done(xs);
    355   1.1  augustss 		return;
    356   1.1  augustss 	default:
    357   1.1  augustss 		/* Not supported, nothing to do. */
    358   1.1  augustss 		;
    359   1.1  augustss 	}
    360   1.1  augustss }
    361   1.1  augustss 
    362   1.1  augustss Static void
    363   1.1  augustss umass_scsipi_minphys(struct buf *bp)
    364   1.1  augustss {
    365   1.1  augustss #ifdef DIAGNOSTIC
    366   1.1  augustss 	if (bp->b_bcount <= 0) {
    367  1.20      yamt 		printf("umass_scsipi_minphys count(%d) <= 0\n",
    368   1.1  augustss 		       bp->b_bcount);
    369   1.1  augustss 		bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
    370   1.1  augustss 	}
    371   1.1  augustss #endif
    372   1.1  augustss 	if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE)
    373   1.1  augustss 		bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
    374   1.1  augustss 	minphys(bp);
    375   1.1  augustss }
    376   1.1  augustss 
    377   1.1  augustss int
    378  1.29  christos umass_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd,
    379  1.36    dyoung     void *arg, int flag, proc_t *p)
    380   1.1  augustss {
    381   1.1  augustss 	/*struct umass_softc *sc = link->adapter_softc;*/
    382   1.1  augustss 	/*struct umass_scsipi_softc *scbus = sc->bus;*/
    383   1.1  augustss 
    384   1.1  augustss 	switch (cmd) {
    385   1.1  augustss #if 0
    386   1.1  augustss 	case SCBUSIORESET:
    387   1.1  augustss 		ccb->ccb_h.status = CAM_REQ_INPROG;
    388   1.1  augustss 		umass_reset(sc, umass_cam_cb, (void *) ccb);
    389  1.50     skrll 		return 0;
    390   1.1  augustss #endif
    391   1.1  augustss 	default:
    392  1.50     skrll 		return ENOTTY;
    393   1.1  augustss 	}
    394   1.1  augustss }
    395   1.1  augustss 
    396   1.1  augustss Static int
    397   1.1  augustss umass_scsipi_getgeom(struct scsipi_periph *periph, struct disk_parms *dp,
    398  1.23   reinoud 		     u_long sectors)
    399   1.1  augustss {
    400   1.1  augustss 	struct umass_softc *sc =
    401  1.33      cube 	    device_private(periph->periph_channel->chan_adapter->adapt_dev);
    402   1.1  augustss 
    403   1.1  augustss 	/* If it's not a floppy, we don't know what to do. */
    404   1.1  augustss 	if (sc->sc_cmd != UMASS_CPROTO_UFI)
    405  1.50     skrll 		return 0;
    406   1.1  augustss 
    407   1.1  augustss 	switch (sectors) {
    408   1.1  augustss 	case 1440:
    409   1.1  augustss 		/* Most likely a single density 3.5" floppy. */
    410   1.1  augustss 		dp->heads = 2;
    411   1.1  augustss 		dp->sectors = 9;
    412   1.1  augustss 		dp->cyls = 80;
    413  1.50     skrll 		return 1;
    414   1.1  augustss 	case 2880:
    415   1.1  augustss 		/* Most likely a double density 3.5" floppy. */
    416   1.1  augustss 		dp->heads = 2;
    417   1.1  augustss 		dp->sectors = 18;
    418   1.1  augustss 		dp->cyls = 80;
    419  1.50     skrll 		return 1;
    420   1.1  augustss 	default:
    421  1.50     skrll 		return 0;
    422   1.1  augustss 	}
    423   1.1  augustss }
    424   1.1  augustss 
    425   1.1  augustss Static void
    426  1.68  riastrad umass_null_cb(struct umass_softc *sc, void *priv, int residue, int status)
    427  1.68  riastrad {
    428  1.68  riastrad 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
    429  1.68  riastrad }
    430  1.68  riastrad 
    431  1.68  riastrad Static void
    432   1.1  augustss umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status)
    433   1.1  augustss {
    434  1.52     skrll 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
    435   1.1  augustss 	struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
    436   1.1  augustss 	struct scsipi_xfer *xs = priv;
    437   1.1  augustss 	struct scsipi_periph *periph = xs->xs_periph;
    438  1.46  drochner 	int cmdlen, senselen;
    439   1.1  augustss #ifdef UMASS_DEBUG
    440   1.1  augustss 	struct timeval tv;
    441   1.1  augustss 	u_int delta;
    442   1.1  augustss 	microtime(&tv);
    443   1.1  augustss 	delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 + tv.tv_usec - sc->tv.tv_usec;
    444  1.55  pgoyette 	DPRINTFM(UDMASS_CMD, "delta=%ju: xs=%#jx residue=%jd status=%jd",
    445  1.55  pgoyette 	    delta, (uintptr_t)xs, residue, status);
    446   1.1  augustss #endif
    447   1.1  augustss 
    448   1.1  augustss 
    449   1.1  augustss 	xs->resid = residue;
    450   1.1  augustss 
    451   1.1  augustss 	switch (status) {
    452   1.1  augustss 	case STATUS_CMD_OK:
    453   1.1  augustss 		xs->error = XS_NOERROR;
    454   1.1  augustss 		break;
    455   1.1  augustss 
    456   1.1  augustss 	case STATUS_CMD_UNKNOWN:
    457   1.1  augustss 		/* FALLTHROUGH */
    458   1.1  augustss 	case STATUS_CMD_FAILED:
    459   1.1  augustss 		/* fetch sense data */
    460  1.11   mycroft 		sc->sc_sense = 1;
    461   1.1  augustss 		memset(&scbus->sc_sense_cmd, 0, sizeof(scbus->sc_sense_cmd));
    462  1.24   thorpej 		scbus->sc_sense_cmd.opcode = SCSI_REQUEST_SENSE;
    463   1.1  augustss 		scbus->sc_sense_cmd.byte2 = periph->periph_lun <<
    464   1.1  augustss 		    SCSI_CMD_LUN_SHIFT;
    465   1.1  augustss 
    466  1.18   mycroft 		if (sc->sc_cmd == UMASS_CPROTO_UFI ||
    467  1.18   mycroft 		    sc->sc_cmd == UMASS_CPROTO_ATAPI)
    468  1.18   mycroft 			cmdlen = UFI_COMMAND_LENGTH;	/* XXX */
    469  1.18   mycroft 		else
    470  1.18   mycroft 			cmdlen = sizeof(scbus->sc_sense_cmd);
    471  1.62   mlelstv 		if (periph->periph_version < 0x04) /* SPC-2 */
    472  1.46  drochner 			senselen = 18;
    473  1.46  drochner 		else
    474  1.46  drochner 			senselen = sizeof(xs->sense);
    475  1.46  drochner 		scbus->sc_sense_cmd.length = senselen;
    476   1.1  augustss 		sc->sc_methods->wire_xfer(sc, periph->periph_lun,
    477   1.1  augustss 					  &scbus->sc_sense_cmd, cmdlen,
    478  1.46  drochner 					  &xs->sense, senselen,
    479  1.41       mrg 					  DIR_IN, xs->timeout, 0,
    480   1.1  augustss 					  umass_scsipi_sense_cb, xs);
    481   1.1  augustss 		return;
    482   1.1  augustss 
    483   1.1  augustss 	case STATUS_WIRE_FAILED:
    484   1.1  augustss 		xs->error = XS_RESET;
    485   1.1  augustss 		break;
    486   1.1  augustss 
    487  1.56   mlelstv 	case STATUS_TIMEOUT:
    488  1.56   mlelstv 		xs->error = XS_TIMEOUT;
    489  1.56   mlelstv 		break;
    490  1.56   mlelstv 
    491   1.1  augustss 	default:
    492   1.6    provos 		panic("%s: Unknown status %d in umass_scsipi_cb",
    493  1.36    dyoung 			device_xname(sc->sc_dev), status);
    494   1.1  augustss 	}
    495   1.1  augustss 
    496  1.65  christos 	DPRINTFM(UDMASS_CMD, "return xs->error=%jd, xs->xs_status=%#jx"
    497  1.55  pgoyette 	    " xs->resid=%jd", xs->error, xs->xs_status, xs->resid, 0);
    498   1.1  augustss 
    499   1.1  augustss 	scsipi_done(xs);
    500   1.1  augustss }
    501   1.1  augustss 
    502   1.5  augustss /*
    503   1.1  augustss  * Finalise a completed autosense operation
    504   1.1  augustss  */
    505   1.1  augustss Static void
    506   1.1  augustss umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue,
    507   1.1  augustss 		      int status)
    508   1.1  augustss {
    509  1.52     skrll 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
    510   1.1  augustss 	struct scsipi_xfer *xs = priv;
    511  1.62   mlelstv 	size_t extra;
    512   1.1  augustss 
    513  1.55  pgoyette 	DPRINTFM(UDMASS_CMD, "sc %#jx: xs=%#jx residue=%jd status=%jd",
    514  1.55  pgoyette 	    (uintptr_t)sc, (uintptr_t)xs, residue, status);
    515   1.1  augustss 
    516  1.11   mycroft 	sc->sc_sense = 0;
    517   1.1  augustss 	switch (status) {
    518   1.1  augustss 	case STATUS_CMD_OK:
    519   1.1  augustss 	case STATUS_CMD_UNKNOWN:
    520   1.1  augustss 		/* getting sense data succeeded */
    521  1.62   mlelstv 		extra = sizeof(xs->sense.scsi_sense)
    522  1.62   mlelstv 		      - sizeof(xs->sense.scsi_sense.extra_bytes);
    523  1.62   mlelstv 		if (residue <= extra)
    524   1.1  augustss 			xs->error = XS_SENSE;
    525   1.1  augustss 		else
    526   1.1  augustss 			xs->error = XS_SHORTSENSE;
    527   1.1  augustss 		break;
    528   1.1  augustss 	default:
    529  1.55  pgoyette 		DPRINTFM(UDMASS_SCSI, "sc %#jx: Autosense failed, status %jd",
    530  1.55  pgoyette 		    (uintptr_t)sc, status, 0, 0);
    531   1.1  augustss 		xs->error = XS_DRIVER_STUFFUP;
    532   1.1  augustss 		break;
    533   1.1  augustss 	}
    534   1.1  augustss 
    535  1.65  christos 	DPRINTFM(UDMASS_CMD, "return xs->error=%jd, xs->xs_status=%#jx"
    536  1.55  pgoyette 	    " xs->resid=%jd", xs->error, xs->xs_status, xs->resid, 0);
    537   1.1  augustss 
    538   1.1  augustss 	scsipi_done(xs);
    539   1.1  augustss }
    540   1.1  augustss 
    541   1.1  augustss #if NATAPIBUS > 0
    542   1.1  augustss Static void
    543   1.1  augustss umass_atapi_probe_device(struct atapibus_softc *atapi, int target)
    544   1.1  augustss {
    545  1.52     skrll 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
    546   1.1  augustss 	struct scsipi_channel *chan = atapi->sc_channel;
    547   1.1  augustss 	struct scsipi_periph *periph;
    548   1.1  augustss 	struct scsipibus_attach_args sa;
    549   1.1  augustss 	char vendor[33], product[65], revision[17];
    550   1.1  augustss 	struct scsipi_inquiry_data inqbuf;
    551   1.1  augustss 
    552  1.55  pgoyette 	DPRINTFM(UDMASS_SCSI, "atapi=%#jx target=%jd", (uintptr_t)atapi,
    553  1.55  pgoyette 	    target, 0, 0);
    554   1.1  augustss 
    555   1.1  augustss 	if (target != UMASS_ATAPI_DRIVE)	/* only probe drive 0 */
    556   1.1  augustss 		return;
    557   1.1  augustss 
    558   1.1  augustss 	/* skip if already attached */
    559  1.39       mrg 	if (scsipi_lookup_periph(chan, target, 0) != NULL) {
    560   1.1  augustss 		return;
    561  1.39       mrg 	}
    562   1.1  augustss 
    563  1.63       chs 	periph = scsipi_alloc_periph(M_WAITOK);
    564   1.1  augustss 	DIF(UDMASS_UPPER, periph->periph_dbflags |= 1); /* XXX 1 */
    565   1.1  augustss 	periph->periph_channel = chan;
    566   1.1  augustss 	periph->periph_switch = &atapi_probe_periphsw;
    567   1.1  augustss 	periph->periph_target = target;
    568   1.1  augustss 	periph->periph_quirks = chan->chan_defquirks;
    569   1.1  augustss 
    570  1.52     skrll 	DPRINTFM(UDMASS_SCSI, "doing inquiry", 0, 0, 0, 0);
    571   1.1  augustss 	/* Now go ask the device all about itself. */
    572   1.1  augustss 	memset(&inqbuf, 0, sizeof(inqbuf));
    573  1.35     rmind 	if (scsipi_inquire(periph, &inqbuf, XS_CTL_DISCOVERY) != 0) {
    574  1.52     skrll 		DPRINTFM(UDMASS_SCSI, "scsipi_inquire failed", 0, 0, 0, 0);
    575   1.1  augustss 		free(periph, M_DEVBUF);
    576   1.1  augustss 		return;
    577   1.1  augustss 	}
    578   1.1  augustss 
    579  1.51  christos 	strnvisx(vendor, sizeof(vendor), inqbuf.vendor, 8,
    580  1.51  christos 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    581  1.51  christos 	strnvisx(product, sizeof(product), inqbuf.product, 16,
    582  1.51  christos 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    583  1.51  christos 	strnvisx(revision, sizeof(revision), inqbuf.revision, 4,
    584  1.51  christos 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    585   1.1  augustss 
    586   1.1  augustss 	sa.sa_periph = periph;
    587   1.1  augustss 	sa.sa_inqbuf.type = inqbuf.device;
    588   1.1  augustss 	sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
    589   1.1  augustss 	    T_REMOV : T_FIXED;
    590   1.1  augustss 	if (sa.sa_inqbuf.removable)
    591   1.1  augustss 		periph->periph_flags |= PERIPH_REMOVABLE;
    592   1.1  augustss 	sa.sa_inqbuf.vendor = vendor;
    593   1.1  augustss 	sa.sa_inqbuf.product = product;
    594   1.1  augustss 	sa.sa_inqbuf.revision = revision;
    595   1.1  augustss 	sa.sa_inqptr = NULL;
    596   1.1  augustss 
    597   1.1  augustss 	atapi_probe_device(atapi, target, periph, &sa);
    598   1.1  augustss 	/* atapi_probe_device() frees the periph when there is no device.*/
    599   1.1  augustss }
    600   1.1  augustss #endif
    601