xref: /minix3/tests/dev/scsipi/libscsitest/scsitest.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: scsitest.c,v 1.2 2014/04/25 00:24:39 pooka Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
5*0a6a1f1dSLionel Sambuc  *
6*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
7*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
8*0a6a1f1dSLionel Sambuc  * are met:
9*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
10*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
11*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
12*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
13*0a6a1f1dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
14*0a6a1f1dSLionel Sambuc  *
15*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16*0a6a1f1dSLionel Sambuc  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17*0a6a1f1dSLionel Sambuc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18*0a6a1f1dSLionel Sambuc  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*0a6a1f1dSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*0a6a1f1dSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21*0a6a1f1dSLionel Sambuc  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*0a6a1f1dSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*0a6a1f1dSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*0a6a1f1dSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*0a6a1f1dSLionel Sambuc  * SUCH DAMAGE.
26*0a6a1f1dSLionel Sambuc  */
27*0a6a1f1dSLionel Sambuc 
28*0a6a1f1dSLionel Sambuc /*
29*0a6a1f1dSLionel Sambuc  * A SCSI target which is useful for debugging our scsipi driver stack.
30*0a6a1f1dSLionel Sambuc  * Currently it pretends to be a single CD.
31*0a6a1f1dSLionel Sambuc  *
32*0a6a1f1dSLionel Sambuc  * Freely add the necessary features for your tests.  Just remember to
33*0a6a1f1dSLionel Sambuc  * run the atf test suite to make sure you didn't cause regressions to
34*0a6a1f1dSLionel Sambuc  * other tests.
35*0a6a1f1dSLionel Sambuc  */
36*0a6a1f1dSLionel Sambuc 
37*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
38*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: scsitest.c,v 1.2 2014/04/25 00:24:39 pooka Exp $");
39*0a6a1f1dSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc #include <sys/param.h>
41*0a6a1f1dSLionel Sambuc #include <sys/atomic.h>
42*0a6a1f1dSLionel Sambuc #include <sys/buf.h>
43*0a6a1f1dSLionel Sambuc #include <sys/device.h>
44*0a6a1f1dSLionel Sambuc #include <sys/malloc.h>
45*0a6a1f1dSLionel Sambuc #include <sys/fcntl.h>
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc #include <dev/scsipi/scsiconf.h>
48*0a6a1f1dSLionel Sambuc #include <dev/scsipi/scsipiconf.h>
49*0a6a1f1dSLionel Sambuc #include <dev/scsipi/scsi_disk.h>
50*0a6a1f1dSLionel Sambuc #include <dev/scsipi/scsipi_cd.h>
51*0a6a1f1dSLionel Sambuc #include <dev/scsipi/scsipi_all.h>
52*0a6a1f1dSLionel Sambuc 
53*0a6a1f1dSLionel Sambuc #include <rump/rumpuser.h>
54*0a6a1f1dSLionel Sambuc 
55*0a6a1f1dSLionel Sambuc #include "scsitest.h"
56*0a6a1f1dSLionel Sambuc 
57*0a6a1f1dSLionel Sambuc int	scsitest_match(device_t, cfdata_t, void *);
58*0a6a1f1dSLionel Sambuc void	scsitest_attach(device_t, device_t, void *);
59*0a6a1f1dSLionel Sambuc 
60*0a6a1f1dSLionel Sambuc struct scsitest {
61*0a6a1f1dSLionel Sambuc 	struct scsipi_channel sc_channel;
62*0a6a1f1dSLionel Sambuc 	struct scsipi_adapter sc_adapter;
63*0a6a1f1dSLionel Sambuc };
64*0a6a1f1dSLionel Sambuc 
65*0a6a1f1dSLionel Sambuc CFATTACH_DECL_NEW(scsitest, sizeof(struct scsitest), scsitest_match,
66*0a6a1f1dSLionel Sambuc 	scsitest_attach, NULL, NULL);
67*0a6a1f1dSLionel Sambuc 
68*0a6a1f1dSLionel Sambuc /*
69*0a6a1f1dSLionel Sambuc  * tosi.iso can be used to deliver CD requests to a host file with the
70*0a6a1f1dSLionel Sambuc  * name in USE_TOSI_ISO (yes, it's extrasimplistic).
71*0a6a1f1dSLionel Sambuc  */
72*0a6a1f1dSLionel Sambuc //#define USE_TOSI_ISO
73*0a6a1f1dSLionel Sambuc 
74*0a6a1f1dSLionel Sambuc #define CDBLOCKSIZE 2048
75*0a6a1f1dSLionel Sambuc static uint32_t mycdsize = 2048;
76*0a6a1f1dSLionel Sambuc static int isofd;
77*0a6a1f1dSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc #define MYCDISO "tosi.iso"
79*0a6a1f1dSLionel Sambuc 
80*0a6a1f1dSLionel Sambuc unsigned rump_scsitest_err[RUMP_SCSITEST_MAXERROR];
81*0a6a1f1dSLionel Sambuc 
82*0a6a1f1dSLionel Sambuc static void
sense_notready(struct scsipi_xfer * xs)83*0a6a1f1dSLionel Sambuc sense_notready(struct scsipi_xfer *xs)
84*0a6a1f1dSLionel Sambuc {
85*0a6a1f1dSLionel Sambuc 	struct scsi_sense_data *sense = &xs->sense.scsi_sense;
86*0a6a1f1dSLionel Sambuc 
87*0a6a1f1dSLionel Sambuc 	xs->error = XS_SENSE;
88*0a6a1f1dSLionel Sambuc 
89*0a6a1f1dSLionel Sambuc 	sense->response_code = 0x70;
90*0a6a1f1dSLionel Sambuc 	sense->flags = SKEY_NOT_READY;
91*0a6a1f1dSLionel Sambuc 	sense->asc = 0x3A;
92*0a6a1f1dSLionel Sambuc 	sense->ascq = 0x00;
93*0a6a1f1dSLionel Sambuc 	sense->extra_len = 6;
94*0a6a1f1dSLionel Sambuc }
95*0a6a1f1dSLionel Sambuc 
96*0a6a1f1dSLionel Sambuc /*
97*0a6a1f1dSLionel Sambuc  * This is pretty much a CD target for now
98*0a6a1f1dSLionel Sambuc  */
99*0a6a1f1dSLionel Sambuc static void
scsitest_request(struct scsipi_channel * chan,scsipi_adapter_req_t req,void * arg)100*0a6a1f1dSLionel Sambuc scsitest_request(struct scsipi_channel *chan,
101*0a6a1f1dSLionel Sambuc 	scsipi_adapter_req_t req, void *arg)
102*0a6a1f1dSLionel Sambuc {
103*0a6a1f1dSLionel Sambuc 	struct scsipi_xfer *xs = arg;
104*0a6a1f1dSLionel Sambuc 	struct scsipi_generic *cmd = xs->cmd;
105*0a6a1f1dSLionel Sambuc #ifdef USE_TOSI_ISO
106*0a6a1f1dSLionel Sambuc 	int error;
107*0a6a1f1dSLionel Sambuc #endif
108*0a6a1f1dSLionel Sambuc 
109*0a6a1f1dSLionel Sambuc 	if (req != ADAPTER_REQ_RUN_XFER)
110*0a6a1f1dSLionel Sambuc 		return;
111*0a6a1f1dSLionel Sambuc 
112*0a6a1f1dSLionel Sambuc 	//show_scsipi_xs(xs);
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc 	switch (cmd->opcode) {
115*0a6a1f1dSLionel Sambuc 	case SCSI_TEST_UNIT_READY:
116*0a6a1f1dSLionel Sambuc 		if (isofd == -1)
117*0a6a1f1dSLionel Sambuc 			sense_notready(xs);
118*0a6a1f1dSLionel Sambuc 
119*0a6a1f1dSLionel Sambuc 		break;
120*0a6a1f1dSLionel Sambuc 	case INQUIRY: {
121*0a6a1f1dSLionel Sambuc 		struct scsipi_inquiry_data *inqbuf = (void *)xs->data;
122*0a6a1f1dSLionel Sambuc 
123*0a6a1f1dSLionel Sambuc 		memset(inqbuf, 0, sizeof(*inqbuf));
124*0a6a1f1dSLionel Sambuc 		inqbuf->device = T_CDROM;
125*0a6a1f1dSLionel Sambuc 		inqbuf->dev_qual2 = SID_REMOVABLE;
126*0a6a1f1dSLionel Sambuc 		strcpy(inqbuf->vendor, "RUMPHOBO");
127*0a6a1f1dSLionel Sambuc 		strcpy(inqbuf->product, "It's a LIE");
128*0a6a1f1dSLionel Sambuc 		strcpy(inqbuf->revision, "0.00");
129*0a6a1f1dSLionel Sambuc 		break;
130*0a6a1f1dSLionel Sambuc 	}
131*0a6a1f1dSLionel Sambuc 	case READ_CD_CAPACITY: {
132*0a6a1f1dSLionel Sambuc 		struct scsipi_read_cd_cap_data *ret = (void *)xs->data;
133*0a6a1f1dSLionel Sambuc 
134*0a6a1f1dSLionel Sambuc 		_lto4b(CDBLOCKSIZE, ret->length);
135*0a6a1f1dSLionel Sambuc 		_lto4b(mycdsize, ret->addr);
136*0a6a1f1dSLionel Sambuc 
137*0a6a1f1dSLionel Sambuc 		break;
138*0a6a1f1dSLionel Sambuc 	}
139*0a6a1f1dSLionel Sambuc 	case READ_DISCINFO: {
140*0a6a1f1dSLionel Sambuc 		struct scsipi_read_discinfo_data *ret = (void *)xs->data;
141*0a6a1f1dSLionel Sambuc 
142*0a6a1f1dSLionel Sambuc 		memset(ret, 0, sizeof(*ret));
143*0a6a1f1dSLionel Sambuc 		break;
144*0a6a1f1dSLionel Sambuc 	}
145*0a6a1f1dSLionel Sambuc 	case READ_TRACKINFO: {
146*0a6a1f1dSLionel Sambuc 		struct scsipi_read_trackinfo_data *ret = (void *)xs->data;
147*0a6a1f1dSLionel Sambuc 
148*0a6a1f1dSLionel Sambuc 		_lto4b(mycdsize, ret->track_size);
149*0a6a1f1dSLionel Sambuc 		break;
150*0a6a1f1dSLionel Sambuc 	}
151*0a6a1f1dSLionel Sambuc 	case READ_TOC: {
152*0a6a1f1dSLionel Sambuc 		struct scsipi_toc_header *ret = (void *)xs->data;
153*0a6a1f1dSLionel Sambuc 
154*0a6a1f1dSLionel Sambuc 		memset(ret, 0, sizeof(*ret));
155*0a6a1f1dSLionel Sambuc 		break;
156*0a6a1f1dSLionel Sambuc 	}
157*0a6a1f1dSLionel Sambuc 	case START_STOP: {
158*0a6a1f1dSLionel Sambuc 		struct scsipi_start_stop *param = (void *)cmd;
159*0a6a1f1dSLionel Sambuc 
160*0a6a1f1dSLionel Sambuc 		if (param->how & SSS_LOEJ) {
161*0a6a1f1dSLionel Sambuc #ifdef USE_TOSI_ISO
162*0a6a1f1dSLionel Sambuc 			rumpuser_close(isofd, &error);
163*0a6a1f1dSLionel Sambuc #endif
164*0a6a1f1dSLionel Sambuc 			isofd = -1;
165*0a6a1f1dSLionel Sambuc 		}
166*0a6a1f1dSLionel Sambuc 		break;
167*0a6a1f1dSLionel Sambuc 	}
168*0a6a1f1dSLionel Sambuc 	case SCSI_SYNCHRONIZE_CACHE_10: {
169*0a6a1f1dSLionel Sambuc 		if (isofd == -1) {
170*0a6a1f1dSLionel Sambuc 			if ((xs->xs_control & XS_CTL_SILENT) == 0)
171*0a6a1f1dSLionel Sambuc 				atomic_inc_uint(&rump_scsitest_err
172*0a6a1f1dSLionel Sambuc 				    [RUMP_SCSITEST_NOISYSYNC]);
173*0a6a1f1dSLionel Sambuc 
174*0a6a1f1dSLionel Sambuc 			sense_notready(xs);
175*0a6a1f1dSLionel Sambuc 		}
176*0a6a1f1dSLionel Sambuc 
177*0a6a1f1dSLionel Sambuc 		break;
178*0a6a1f1dSLionel Sambuc 	}
179*0a6a1f1dSLionel Sambuc 	case GET_CONFIGURATION: {
180*0a6a1f1dSLionel Sambuc 		memset(xs->data, 0, sizeof(struct scsipi_get_conf_data));
181*0a6a1f1dSLionel Sambuc 		break;
182*0a6a1f1dSLionel Sambuc 	}
183*0a6a1f1dSLionel Sambuc 	case SCSI_READ_6_COMMAND: {
184*0a6a1f1dSLionel Sambuc #ifdef USE_TOSI_ISO
185*0a6a1f1dSLionel Sambuc 		struct scsi_rw_6 *param = (void *)cmd;
186*0a6a1f1dSLionel Sambuc 
187*0a6a1f1dSLionel Sambuc 		printf("reading %d bytes from %d\n",
188*0a6a1f1dSLionel Sambuc 		    param->length * CDBLOCKSIZE,
189*0a6a1f1dSLionel Sambuc 		    _3btol(param->addr) * CDBLOCKSIZE);
190*0a6a1f1dSLionel Sambuc 		rumpuser_pread(isofd, xs->data,
191*0a6a1f1dSLionel Sambuc 		     param->length * CDBLOCKSIZE,
192*0a6a1f1dSLionel Sambuc 		     _3btol(param->addr) * CDBLOCKSIZE,
193*0a6a1f1dSLionel Sambuc 		     &error);
194*0a6a1f1dSLionel Sambuc #endif
195*0a6a1f1dSLionel Sambuc 
196*0a6a1f1dSLionel Sambuc 		break;
197*0a6a1f1dSLionel Sambuc 	}
198*0a6a1f1dSLionel Sambuc 	case SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL:
199*0a6a1f1dSLionel Sambuc 		/* hardcoded for now */
200*0a6a1f1dSLionel Sambuc 		break;
201*0a6a1f1dSLionel Sambuc 	default:
202*0a6a1f1dSLionel Sambuc 		printf("unhandled opcode 0x%x\n", cmd->opcode);
203*0a6a1f1dSLionel Sambuc 		break;
204*0a6a1f1dSLionel Sambuc 	}
205*0a6a1f1dSLionel Sambuc 
206*0a6a1f1dSLionel Sambuc 	scsipi_done(xs);
207*0a6a1f1dSLionel Sambuc }
208*0a6a1f1dSLionel Sambuc 
209*0a6a1f1dSLionel Sambuc int
scsitest_match(device_t parent,cfdata_t match,void * aux)210*0a6a1f1dSLionel Sambuc scsitest_match(device_t parent, cfdata_t match, void *aux)
211*0a6a1f1dSLionel Sambuc {
212*0a6a1f1dSLionel Sambuc #ifdef USE_TOSI_ISO
213*0a6a1f1dSLionel Sambuc 	uint64_t fsize;
214*0a6a1f1dSLionel Sambuc 	int error, ft;
215*0a6a1f1dSLionel Sambuc 
216*0a6a1f1dSLionel Sambuc 	if (rumpuser_getfileinfo(MYCDISO, &fsize, &ft, &error))
217*0a6a1f1dSLionel Sambuc 		return 0;
218*0a6a1f1dSLionel Sambuc 	if (ft != RUMPUSER_FT_REG)
219*0a6a1f1dSLionel Sambuc 		return 0;
220*0a6a1f1dSLionel Sambuc 	mycdsize = fsize / CDBLOCKSIZE;
221*0a6a1f1dSLionel Sambuc 
222*0a6a1f1dSLionel Sambuc 	if ((isofd = rumpuser_open(MYCDISO, RUMPUSER_OPEN_RDWR, &error)) == -1)
223*0a6a1f1dSLionel Sambuc 		return 0;
224*0a6a1f1dSLionel Sambuc #else
225*0a6a1f1dSLionel Sambuc 	/*
226*0a6a1f1dSLionel Sambuc 	 * We pretend to have a medium present initially, so != -1.
227*0a6a1f1dSLionel Sambuc 	 */
228*0a6a1f1dSLionel Sambuc 	isofd = -2;
229*0a6a1f1dSLionel Sambuc #endif
230*0a6a1f1dSLionel Sambuc 
231*0a6a1f1dSLionel Sambuc 	return 1;
232*0a6a1f1dSLionel Sambuc }
233*0a6a1f1dSLionel Sambuc 
234*0a6a1f1dSLionel Sambuc void
scsitest_attach(device_t parent,device_t self,void * aux)235*0a6a1f1dSLionel Sambuc scsitest_attach(device_t parent, device_t self, void *aux)
236*0a6a1f1dSLionel Sambuc {
237*0a6a1f1dSLionel Sambuc 	struct scsitest *sc = device_private(self);
238*0a6a1f1dSLionel Sambuc 
239*0a6a1f1dSLionel Sambuc 	aprint_naive("\n");
240*0a6a1f1dSLionel Sambuc 	aprint_normal("\n");
241*0a6a1f1dSLionel Sambuc 
242*0a6a1f1dSLionel Sambuc 	memset(&sc->sc_adapter, 0, sizeof(sc->sc_adapter));
243*0a6a1f1dSLionel Sambuc 	sc->sc_adapter.adapt_nchannels = 1;
244*0a6a1f1dSLionel Sambuc 	sc->sc_adapter.adapt_request = scsitest_request;
245*0a6a1f1dSLionel Sambuc 	sc->sc_adapter.adapt_minphys = minphys;
246*0a6a1f1dSLionel Sambuc 	sc->sc_adapter.adapt_dev = self;
247*0a6a1f1dSLionel Sambuc 	sc->sc_adapter.adapt_max_periph = 1;
248*0a6a1f1dSLionel Sambuc 	sc->sc_adapter.adapt_openings = 1;
249*0a6a1f1dSLionel Sambuc 
250*0a6a1f1dSLionel Sambuc 	memset(&sc->sc_channel, 0, sizeof(sc->sc_channel));
251*0a6a1f1dSLionel Sambuc 	sc->sc_channel.chan_bustype = &scsi_bustype;
252*0a6a1f1dSLionel Sambuc 	sc->sc_channel.chan_ntargets = 2;
253*0a6a1f1dSLionel Sambuc 	sc->sc_channel.chan_nluns = 1;
254*0a6a1f1dSLionel Sambuc 	sc->sc_channel.chan_id = 0;
255*0a6a1f1dSLionel Sambuc 	sc->sc_channel.chan_flags = SCSIPI_CHAN_NOSETTLE;
256*0a6a1f1dSLionel Sambuc 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
257*0a6a1f1dSLionel Sambuc 
258*0a6a1f1dSLionel Sambuc 	config_found_ia(self, "scsi", &sc->sc_channel, scsiprint);
259*0a6a1f1dSLionel Sambuc }
260