Home | History | Annotate | Line # | Download | only in dev
vatapi.c revision 1.1
      1  1.1  reinoud /* $NetBSD: vatapi.c,v 1.1 2018/06/05 20:02:43 reinoud Exp $ */
      2  1.1  reinoud 
      3  1.1  reinoud /*-
      4  1.1  reinoud  * Copyright (c) 2018 Reinoud Zandijk <reinoud (at) NetBSD.org>
      5  1.1  reinoud  * All rights reserved.
      6  1.1  reinoud  *
      7  1.1  reinoud  * Redistribution and use in source and binary forms, with or without
      8  1.1  reinoud  * modification, are permitted provided that the following conditions
      9  1.1  reinoud  * are met:
     10  1.1  reinoud  * 1. Redistributions of source code must retain the above copyright
     11  1.1  reinoud  *    notice, this list of conditions and the following disclaimer.
     12  1.1  reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  reinoud  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  reinoud  *    documentation and/or other materials provided with the distribution.
     15  1.1  reinoud  *
     16  1.1  reinoud  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  reinoud  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  reinoud  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  reinoud  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  reinoud  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  reinoud  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  reinoud  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  reinoud  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  reinoud  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  reinoud  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  reinoud  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  reinoud  */
     28  1.1  reinoud 
     29  1.1  reinoud #include <sys/cdefs.h>
     30  1.1  reinoud __KERNEL_RCSID(0, "$NetBSD: vatapi.c,v 1.1 2018/06/05 20:02:43 reinoud Exp $");
     31  1.1  reinoud 
     32  1.1  reinoud #include <sys/param.h>
     33  1.1  reinoud #include <sys/proc.h>
     34  1.1  reinoud #include <sys/systm.h>
     35  1.1  reinoud #include <sys/device.h>
     36  1.1  reinoud #include <sys/buf.h>
     37  1.1  reinoud #include <sys/disk.h>
     38  1.1  reinoud #include <sys/kmem.h>
     39  1.1  reinoud #include <sys/malloc.h>
     40  1.1  reinoud #include <sys/scsiio.h>
     41  1.1  reinoud 
     42  1.1  reinoud #include <machine/mainbus.h>
     43  1.1  reinoud #include <machine/thunk.h>
     44  1.1  reinoud #include <machine/intr.h>
     45  1.1  reinoud 
     46  1.1  reinoud #include <dev/scsipi/scsi_all.h>	/* for SCSI status */
     47  1.1  reinoud #include <dev/scsipi/scsipi_all.h>
     48  1.1  reinoud #include <dev/scsipi/scsipiconf.h>
     49  1.1  reinoud #include <dev/scsipi/atapiconf.h>
     50  1.1  reinoud 
     51  1.1  reinoud /* parameter? */
     52  1.1  reinoud #define VDEV_ATAPI_DRIVE	0
     53  1.1  reinoud #define MAX_SIZE		((1<<16))
     54  1.1  reinoud 
     55  1.1  reinoud /* forwards */
     56  1.1  reinoud struct vatapi_softc;
     57  1.1  reinoud 
     58  1.1  reinoud static int	vatapi_match(device_t, cfdata_t, void *);
     59  1.1  reinoud static void	vatapi_attach(device_t, device_t, void *);
     60  1.1  reinoud static void	vatapi_callback(device_t self);
     61  1.1  reinoud 
     62  1.1  reinoud static void	vatapi_minphys(struct buf *bp);
     63  1.1  reinoud static void	vatapi_kill_pending(struct scsipi_periph *periph);
     64  1.1  reinoud static void	vatapi_scsipi_request(struct scsipi_channel *chan,
     65  1.1  reinoud 	scsipi_adapter_req_t req, void *arg);
     66  1.1  reinoud static void	vatapi_probe_device(struct atapibus_softc *, int);
     67  1.1  reinoud 
     68  1.1  reinoud static void	vatapi_complete(void *arg);
     69  1.1  reinoud 
     70  1.1  reinoud /* for debugging */
     71  1.1  reinoud void	scsipi_print_sense_data_real(struct scsi_sense_data *sense, int verbosity);
     72  1.1  reinoud 
     73  1.1  reinoud 
     74  1.1  reinoud /* Note its one vdev, one adapter, one channel for now */
     75  1.1  reinoud struct vatapi_softc {
     76  1.1  reinoud 	device_t	sc_dev;
     77  1.1  reinoud 	device_t	sc_pdev;
     78  1.1  reinoud 
     79  1.1  reinoud 	int		sc_flags;
     80  1.1  reinoud #define VATAPI_FLAG_POLLING	1
     81  1.1  reinoud #define VATAPI_FLAG_INTR	2
     82  1.1  reinoud 	/* backing `device' with its active command */
     83  1.1  reinoud 	int		sc_fd;
     84  1.1  reinoud 	void		*sc_ih;
     85  1.1  reinoud 	struct scsipi_xfer *sc_xs;
     86  1.1  reinoud 
     87  1.1  reinoud 	/* atapibus */
     88  1.1  reinoud 	device_t	sc_vatapibus;
     89  1.1  reinoud 	struct atapi_adapter    sc_atapi_adapter;
     90  1.1  reinoud #define sc_adapter sc_atapi_adapter._generic
     91  1.1  reinoud 	struct scsipi_channel sc_channel;
     92  1.1  reinoud };
     93  1.1  reinoud 
     94  1.1  reinoud CFATTACH_DECL_NEW(vatapi_thunkbus, sizeof(struct vatapi_softc),
     95  1.1  reinoud     vatapi_match, vatapi_attach, NULL, NULL);
     96  1.1  reinoud 
     97  1.1  reinoud 
     98  1.1  reinoud static const struct scsipi_bustype vatapi_bustype = {
     99  1.1  reinoud 	SCSIPI_BUSTYPE_ATAPI,
    100  1.1  reinoud 	atapi_scsipi_cmd,
    101  1.1  reinoud 	atapi_interpret_sense,
    102  1.1  reinoud 	atapi_print_addr,
    103  1.1  reinoud 	vatapi_kill_pending,
    104  1.1  reinoud 	NULL
    105  1.1  reinoud };
    106  1.1  reinoud 
    107  1.1  reinoud int
    108  1.1  reinoud vatapi_match(device_t parent, cfdata_t match, void *opaque)
    109  1.1  reinoud {
    110  1.1  reinoud 	struct thunkbus_attach_args *taa = opaque;
    111  1.1  reinoud 
    112  1.1  reinoud 	if (taa->taa_type != THUNKBUS_TYPE_VATAPI)
    113  1.1  reinoud 		return 0;
    114  1.1  reinoud 
    115  1.1  reinoud 	return 1;
    116  1.1  reinoud }
    117  1.1  reinoud 
    118  1.1  reinoud static void
    119  1.1  reinoud vatapi_attach(device_t parent, device_t self, void *opaque)
    120  1.1  reinoud {
    121  1.1  reinoud 	struct vatapi_softc *sc = device_private(self);
    122  1.1  reinoud 	struct thunkbus_attach_args *taa = opaque;
    123  1.1  reinoud 
    124  1.1  reinoud 	sc->sc_dev = self;
    125  1.1  reinoud 	sc->sc_pdev = parent;
    126  1.1  reinoud 
    127  1.1  reinoud 	/* open device */
    128  1.1  reinoud 	sc->sc_fd = thunk_open(taa->u.vdev.path, O_RDWR, 0);
    129  1.1  reinoud 	if (sc->sc_fd < 0) {
    130  1.1  reinoud 		aprint_error(": error %d opening path\n", thunk_geterrno());
    131  1.1  reinoud 		return;
    132  1.1  reinoud 	}
    133  1.1  reinoud 
    134  1.1  reinoud 	aprint_naive("\n");
    135  1.1  reinoud 	aprint_normal("\n");
    136  1.1  reinoud 
    137  1.1  reinoud 	sc->sc_ih = softint_establish(SOFTINT_BIO,
    138  1.1  reinoud 		vatapi_complete, sc);
    139  1.1  reinoud 
    140  1.1  reinoud 	/* rest of the configuration is deferred */
    141  1.1  reinoud 	config_interrupts(self, vatapi_callback);
    142  1.1  reinoud }
    143  1.1  reinoud 
    144  1.1  reinoud static void
    145  1.1  reinoud vatapi_callback(device_t self)
    146  1.1  reinoud {
    147  1.1  reinoud 	struct vatapi_softc *sc = device_private(self);
    148  1.1  reinoud 	struct scsipi_adapter *adapt = &sc->sc_adapter;
    149  1.1  reinoud 	struct scsipi_channel *chan  = &sc->sc_channel;
    150  1.1  reinoud 
    151  1.1  reinoud 	/* set up the scsipi adapter and channel */
    152  1.1  reinoud 	memset(adapt, 0, sizeof(*adapt));
    153  1.1  reinoud 	adapt->adapt_dev = sc->sc_dev;
    154  1.1  reinoud 	adapt->adapt_nchannels = 1;
    155  1.1  reinoud 	adapt->adapt_request   = vatapi_scsipi_request;
    156  1.1  reinoud 	adapt->adapt_minphys   = vatapi_minphys;
    157  1.1  reinoud 	adapt->adapt_flags     = 0; //SCSIPI_ADAPT_POLL_ONLY;
    158  1.1  reinoud 	sc->sc_atapi_adapter.atapi_probe_device = vatapi_probe_device;
    159  1.1  reinoud 
    160  1.1  reinoud 	memset(chan,  0, sizeof(*chan));
    161  1.1  reinoud 	chan->chan_adapter    = adapt;
    162  1.1  reinoud 	chan->chan_bustype    = &vatapi_bustype;
    163  1.1  reinoud 	chan->chan_channel    = 0;	/* location */
    164  1.1  reinoud 	chan->chan_flags      = SCSIPI_CHAN_OPENINGS;
    165  1.1  reinoud 	chan->chan_openings   = 1;
    166  1.1  reinoud 	chan->chan_max_periph = 1;
    167  1.1  reinoud 	chan->chan_ntargets   = 1;
    168  1.1  reinoud 	chan->chan_nluns      = 1;
    169  1.1  reinoud 
    170  1.1  reinoud 	/* set polling */
    171  1.1  reinoud 	//sc->sc_flags = VATAPI_FLAG_POLLING;
    172  1.1  reinoud 
    173  1.1  reinoud 	/* we `discovered' an atapi adapter */
    174  1.1  reinoud 	sc->sc_vatapibus = config_found_ia(sc->sc_dev, "atapi", chan,
    175  1.1  reinoud 		atapiprint);
    176  1.1  reinoud }
    177  1.1  reinoud 
    178  1.1  reinoud 
    179  1.1  reinoud /* XXX why is it be called minphys, when it enforces maxphys? */
    180  1.1  reinoud static void
    181  1.1  reinoud vatapi_minphys(struct buf *bp)
    182  1.1  reinoud {
    183  1.1  reinoud 	if (bp->b_bcount > MAX_SIZE)
    184  1.1  reinoud 		bp->b_bcount = MAX_SIZE;
    185  1.1  reinoud 	minphys(bp);
    186  1.1  reinoud }
    187  1.1  reinoud 
    188  1.1  reinoud /*
    189  1.1  reinoud  * ATAPI device probing
    190  1.1  reinoud  */
    191  1.1  reinoud static void
    192  1.1  reinoud vatapi_probe_device(struct atapibus_softc *atapi, int target)
    193  1.1  reinoud {
    194  1.1  reinoud 	struct scsipi_channel *chan = atapi->sc_channel;
    195  1.1  reinoud 	struct scsipi_periph *periph;
    196  1.1  reinoud 	struct scsipibus_attach_args sa;
    197  1.1  reinoud 	char vendor[33], product[65], revision[17];
    198  1.1  reinoud 	struct scsipi_inquiry_data inqbuf;
    199  1.1  reinoud 
    200  1.1  reinoud 	if (target != VDEV_ATAPI_DRIVE)	/* only probe drive 0 */
    201  1.1  reinoud 		return;
    202  1.1  reinoud 
    203  1.1  reinoud 	/* skip if already attached */
    204  1.1  reinoud 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
    205  1.1  reinoud 		return;
    206  1.1  reinoud 
    207  1.1  reinoud 	/* allocate and set up periph structure */
    208  1.1  reinoud 	periph = scsipi_alloc_periph(M_NOWAIT);
    209  1.1  reinoud 	if (periph == NULL) {
    210  1.1  reinoud 		aprint_error_dev(atapi->sc_dev,
    211  1.1  reinoud 		    "can't allocate link for drive %d\n", target);
    212  1.1  reinoud 		return;
    213  1.1  reinoud 	}
    214  1.1  reinoud 	periph->periph_channel = chan;
    215  1.1  reinoud 	periph->periph_switch = &atapi_probe_periphsw;
    216  1.1  reinoud 	periph->periph_target = target;
    217  1.1  reinoud 	periph->periph_quirks = chan->chan_defquirks;
    218  1.1  reinoud 
    219  1.1  reinoud 	/* inquiry */
    220  1.1  reinoud 	aprint_verbose("%s: inquiry devices\n", __func__);
    221  1.1  reinoud 	memset(&inqbuf, 0, sizeof(inqbuf));
    222  1.1  reinoud 	if (scsipi_inquire(periph, &inqbuf, XS_CTL_DISCOVERY) != 0) {
    223  1.1  reinoud 		aprint_error_dev(atapi->sc_dev, ": scsipi_inquire failed\n");
    224  1.1  reinoud 		free(periph, M_DEVBUF);
    225  1.1  reinoud 		return;
    226  1.1  reinoud 	}
    227  1.1  reinoud 
    228  1.1  reinoud #define scsipi_strvis(a, al, b, bl) strlcpy(a, b, al)
    229  1.1  reinoud 	scsipi_strvis(vendor, 33, inqbuf.vendor, 8);
    230  1.1  reinoud 	scsipi_strvis(product, 65, inqbuf.product, 16);
    231  1.1  reinoud 	scsipi_strvis(revision, 17, inqbuf.revision, 4);
    232  1.1  reinoud #undef scsipi_strvis
    233  1.1  reinoud 
    234  1.1  reinoud 	sa.sa_periph = periph;
    235  1.1  reinoud 	sa.sa_inqbuf.type = inqbuf.device;
    236  1.1  reinoud 	sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
    237  1.1  reinoud 	    T_REMOV : T_FIXED;
    238  1.1  reinoud 	if (sa.sa_inqbuf.removable)
    239  1.1  reinoud 		periph->periph_flags |= PERIPH_REMOVABLE;
    240  1.1  reinoud 	sa.sa_inqbuf.vendor = vendor;
    241  1.1  reinoud 	sa.sa_inqbuf.product = product;
    242  1.1  reinoud 	sa.sa_inqbuf.revision = revision;
    243  1.1  reinoud 	sa.sa_inqptr = NULL;
    244  1.1  reinoud 
    245  1.1  reinoud 	/* ATAPI demands only READ10 and higher IIRC */
    246  1.1  reinoud 	periph->periph_quirks |= PQUIRK_ONLYBIG;
    247  1.1  reinoud 
    248  1.1  reinoud 	aprint_verbose(": probedev on vendor '%s' product '%s' revision '%s'\n",
    249  1.1  reinoud 		vendor, product, revision);
    250  1.1  reinoud 
    251  1.1  reinoud 	atapi_probe_device(atapi, target, periph, &sa);
    252  1.1  reinoud 	/* atapi_probe_device() frees the periph when there is no device.*/
    253  1.1  reinoud }
    254  1.1  reinoud 
    255  1.1  reinoud /*
    256  1.1  reinoud  * Issue a request for a periph.
    257  1.1  reinoud  */
    258  1.1  reinoud static void
    259  1.1  reinoud vatapi_scsipi_request(struct scsipi_channel *chan,
    260  1.1  reinoud 	scsipi_adapter_req_t req, void *arg)
    261  1.1  reinoud {
    262  1.1  reinoud 	device_t sc_dev = chan->chan_adapter->adapt_dev;
    263  1.1  reinoud 	struct vatapi_softc *sc = device_private(sc_dev);
    264  1.1  reinoud  	struct scsipi_xfer *xs = arg;
    265  1.1  reinoud 
    266  1.1  reinoud 	switch (req) {
    267  1.1  reinoud  	case ADAPTER_REQ_GROW_RESOURCES:
    268  1.1  reinoud  	case ADAPTER_REQ_SET_XFER_MODE:
    269  1.1  reinoud 		return;
    270  1.1  reinoud 	case ADAPTER_REQ_RUN_XFER :
    271  1.1  reinoud 		KASSERT(sc->sc_xs == NULL);
    272  1.1  reinoud 
    273  1.1  reinoud 		/* queue the command */
    274  1.1  reinoud 		KASSERT(sc->sc_xs == NULL);
    275  1.1  reinoud 		sc->sc_xs = xs;
    276  1.1  reinoud 		softint_schedule(sc->sc_ih);
    277  1.1  reinoud 	}
    278  1.1  reinoud }
    279  1.1  reinoud 
    280  1.1  reinoud 
    281  1.1  reinoud static void
    282  1.1  reinoud vatapi_complete(void *arg)
    283  1.1  reinoud {
    284  1.1  reinoud 	struct vatapi_softc *sc = arg;
    285  1.1  reinoud  	struct scsipi_xfer *xs = sc->sc_xs;
    286  1.1  reinoud 	scsireq_t kreq;
    287  1.1  reinoud 
    288  1.1  reinoud 	memset(&kreq, 0, sizeof(kreq));
    289  1.1  reinoud 	memcpy(kreq.cmd, xs->cmd, xs->cmdlen);
    290  1.1  reinoud 	kreq.cmdlen = xs->cmdlen;
    291  1.1  reinoud 	kreq.databuf = xs->data;		/* in virt? */
    292  1.1  reinoud 	kreq.datalen = xs->datalen;
    293  1.1  reinoud 	kreq.timeout = xs->timeout;
    294  1.1  reinoud 
    295  1.1  reinoud 	kreq.flags = (xs->xs_control & XS_CTL_DATA_IN) ?
    296  1.1  reinoud 		SCCMD_READ : SCCMD_WRITE;
    297  1.1  reinoud 
    298  1.1  reinoud 	kreq.senselen = sizeof(struct scsi_sense_data);
    299  1.1  reinoud 
    300  1.1  reinoud 	xs->error = XS_SHORTSENSE;
    301  1.1  reinoud 	/* this is silly, but better make sure */
    302  1.1  reinoud 	thunk_assert_presence((vaddr_t) kreq.databuf,
    303  1.1  reinoud 		(size_t) kreq.datalen);
    304  1.1  reinoud 
    305  1.1  reinoud 	if (thunk_ioctl(sc->sc_fd, SCIOCCOMMAND, &kreq) != -1) {
    306  1.1  reinoud 		switch (kreq.retsts) {
    307  1.1  reinoud 		case SCCMD_OK :
    308  1.1  reinoud 			xs->resid = 0;
    309  1.1  reinoud 			xs->error = 0;
    310  1.1  reinoud 			break;
    311  1.1  reinoud 		case SCCMD_TIMEOUT :
    312  1.1  reinoud 			break;
    313  1.1  reinoud 		case SCCMD_BUSY :
    314  1.1  reinoud 			break;
    315  1.1  reinoud 		case SCCMD_SENSE :
    316  1.1  reinoud 			xs->error = XS_SHORTSENSE;	/* ATAPI */
    317  1.1  reinoud 			memcpy(&xs->sense.scsi_sense, kreq.sense,
    318  1.1  reinoud 				sizeof(struct scsi_sense_data));
    319  1.1  reinoud //			scsipi_print_sense_data_real(
    320  1.1  reinoud //				(struct scsi_sense_data *) kreq.sense, 1);
    321  1.1  reinoud 			break;
    322  1.1  reinoud 		default:
    323  1.1  reinoud 			thunk_printf("unhandled/unknown retstst %d\n", kreq.retsts);
    324  1.1  reinoud 			break;
    325  1.1  reinoud 		}
    326  1.1  reinoud 	} else {
    327  1.1  reinoud 		printf("thunk_ioctl == -1, errno %d\n", thunk_geterrno());
    328  1.1  reinoud 	}
    329  1.1  reinoud 	sc->sc_xs = NULL;
    330  1.1  reinoud 	scsipi_done(xs);
    331  1.1  reinoud }
    332  1.1  reinoud 
    333  1.1  reinoud 
    334  1.1  reinoud /*
    335  1.1  reinoud  * Kill off all pending xfers for a periph.
    336  1.1  reinoud  *
    337  1.1  reinoud  * Must be called at splbio().
    338  1.1  reinoud  */
    339  1.1  reinoud static void
    340  1.1  reinoud vatapi_kill_pending(struct scsipi_periph *periph)
    341  1.1  reinoud {
    342  1.1  reinoud 	/* do we need to do anything ? (yet?) */
    343  1.1  reinoud 	printf("%s: target %d\n", __func__, periph->periph_target);
    344  1.1  reinoud }
    345  1.1  reinoud 
    346