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