scsitest.c revision 1.1 1 1.1 pooka /* $NetBSD: scsitest.c,v 1.1 2014/04/24 21:46:44 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.1 pooka __KERNEL_RCSID(0, "$NetBSD: scsitest.c,v 1.1 2014/04/24 21:46:44 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.1 pooka #include <rump/scsitest.h>
55 1.1 pooka
56 1.1 pooka int scsitest_match(device_t, cfdata_t, void *);
57 1.1 pooka void scsitest_attach(device_t, device_t, void *);
58 1.1 pooka
59 1.1 pooka struct scsitest {
60 1.1 pooka struct scsipi_channel sc_channel;
61 1.1 pooka struct scsipi_adapter sc_adapter;
62 1.1 pooka };
63 1.1 pooka
64 1.1 pooka CFATTACH_DECL_NEW(scsitest, sizeof(struct scsitest), scsitest_match,
65 1.1 pooka scsitest_attach, NULL, NULL);
66 1.1 pooka
67 1.1 pooka /*
68 1.1 pooka * tosi.iso can be used to deliver CD requests to a host file with the
69 1.1 pooka * name in USE_TOSI_ISO (yes, it's extrasimplistic).
70 1.1 pooka */
71 1.1 pooka //#define USE_TOSI_ISO
72 1.1 pooka
73 1.1 pooka #define CDBLOCKSIZE 2048
74 1.1 pooka static uint32_t mycdsize = 2048;
75 1.1 pooka static int isofd;
76 1.1 pooka
77 1.1 pooka #define MYCDISO "tosi.iso"
78 1.1 pooka
79 1.1 pooka unsigned rump_scsitest_err[RUMP_SCSITEST_MAXERROR];
80 1.1 pooka
81 1.1 pooka static void
82 1.1 pooka sense_notready(struct scsipi_xfer *xs)
83 1.1 pooka {
84 1.1 pooka struct scsi_sense_data *sense = &xs->sense.scsi_sense;
85 1.1 pooka
86 1.1 pooka xs->error = XS_SENSE;
87 1.1 pooka
88 1.1 pooka sense->response_code = 0x70;
89 1.1 pooka sense->flags = SKEY_NOT_READY;
90 1.1 pooka sense->asc = 0x3A;
91 1.1 pooka sense->ascq = 0x00;
92 1.1 pooka sense->extra_len = 6;
93 1.1 pooka }
94 1.1 pooka
95 1.1 pooka /*
96 1.1 pooka * This is pretty much a CD target for now
97 1.1 pooka */
98 1.1 pooka static void
99 1.1 pooka scsitest_request(struct scsipi_channel *chan,
100 1.1 pooka scsipi_adapter_req_t req, void *arg)
101 1.1 pooka {
102 1.1 pooka struct scsipi_xfer *xs = arg;
103 1.1 pooka struct scsipi_generic *cmd = xs->cmd;
104 1.1 pooka #ifdef USE_TOSI_ISO
105 1.1 pooka int error;
106 1.1 pooka #endif
107 1.1 pooka
108 1.1 pooka if (req != ADAPTER_REQ_RUN_XFER)
109 1.1 pooka return;
110 1.1 pooka
111 1.1 pooka //show_scsipi_xs(xs);
112 1.1 pooka
113 1.1 pooka switch (cmd->opcode) {
114 1.1 pooka case SCSI_TEST_UNIT_READY:
115 1.1 pooka if (isofd == -1)
116 1.1 pooka sense_notready(xs);
117 1.1 pooka
118 1.1 pooka break;
119 1.1 pooka case INQUIRY: {
120 1.1 pooka struct scsipi_inquiry_data *inqbuf = (void *)xs->data;
121 1.1 pooka
122 1.1 pooka memset(inqbuf, 0, sizeof(*inqbuf));
123 1.1 pooka inqbuf->device = T_CDROM;
124 1.1 pooka inqbuf->dev_qual2 = SID_REMOVABLE;
125 1.1 pooka strcpy(inqbuf->vendor, "RUMPHOBO");
126 1.1 pooka strcpy(inqbuf->product, "It's a LIE");
127 1.1 pooka strcpy(inqbuf->revision, "0.00");
128 1.1 pooka break;
129 1.1 pooka }
130 1.1 pooka case READ_CD_CAPACITY: {
131 1.1 pooka struct scsipi_read_cd_cap_data *ret = (void *)xs->data;
132 1.1 pooka
133 1.1 pooka _lto4b(CDBLOCKSIZE, ret->length);
134 1.1 pooka _lto4b(mycdsize, ret->addr);
135 1.1 pooka
136 1.1 pooka break;
137 1.1 pooka }
138 1.1 pooka case READ_DISCINFO: {
139 1.1 pooka struct scsipi_read_discinfo_data *ret = (void *)xs->data;
140 1.1 pooka
141 1.1 pooka memset(ret, 0, sizeof(*ret));
142 1.1 pooka break;
143 1.1 pooka }
144 1.1 pooka case READ_TRACKINFO: {
145 1.1 pooka struct scsipi_read_trackinfo_data *ret = (void *)xs->data;
146 1.1 pooka
147 1.1 pooka _lto4b(mycdsize, ret->track_size);
148 1.1 pooka break;
149 1.1 pooka }
150 1.1 pooka case READ_TOC: {
151 1.1 pooka struct scsipi_toc_header *ret = (void *)xs->data;
152 1.1 pooka
153 1.1 pooka memset(ret, 0, sizeof(*ret));
154 1.1 pooka break;
155 1.1 pooka }
156 1.1 pooka case START_STOP: {
157 1.1 pooka struct scsipi_start_stop *param = (void *)cmd;
158 1.1 pooka
159 1.1 pooka if (param->how & SSS_LOEJ) {
160 1.1 pooka #ifdef USE_TOSI_ISO
161 1.1 pooka rumpuser_close(isofd, &error);
162 1.1 pooka #endif
163 1.1 pooka isofd = -1;
164 1.1 pooka }
165 1.1 pooka break;
166 1.1 pooka }
167 1.1 pooka case SCSI_SYNCHRONIZE_CACHE_10: {
168 1.1 pooka if (isofd == -1) {
169 1.1 pooka if ((xs->xs_control & XS_CTL_SILENT) == 0)
170 1.1 pooka atomic_inc_uint(&rump_scsitest_err
171 1.1 pooka [RUMP_SCSITEST_NOISYSYNC]);
172 1.1 pooka
173 1.1 pooka sense_notready(xs);
174 1.1 pooka }
175 1.1 pooka
176 1.1 pooka break;
177 1.1 pooka }
178 1.1 pooka case GET_CONFIGURATION: {
179 1.1 pooka memset(xs->data, 0, sizeof(struct scsipi_get_conf_data));
180 1.1 pooka break;
181 1.1 pooka }
182 1.1 pooka case SCSI_READ_6_COMMAND: {
183 1.1 pooka #ifdef USE_TOSI_ISO
184 1.1 pooka struct scsi_rw_6 *param = (void *)cmd;
185 1.1 pooka
186 1.1 pooka printf("reading %d bytes from %d\n",
187 1.1 pooka param->length * CDBLOCKSIZE,
188 1.1 pooka _3btol(param->addr) * CDBLOCKSIZE);
189 1.1 pooka rumpuser_pread(isofd, xs->data,
190 1.1 pooka param->length * CDBLOCKSIZE,
191 1.1 pooka _3btol(param->addr) * CDBLOCKSIZE,
192 1.1 pooka &error);
193 1.1 pooka #endif
194 1.1 pooka
195 1.1 pooka break;
196 1.1 pooka }
197 1.1 pooka case SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL:
198 1.1 pooka /* hardcoded for now */
199 1.1 pooka break;
200 1.1 pooka default:
201 1.1 pooka printf("unhandled opcode 0x%x\n", cmd->opcode);
202 1.1 pooka break;
203 1.1 pooka }
204 1.1 pooka
205 1.1 pooka scsipi_done(xs);
206 1.1 pooka }
207 1.1 pooka
208 1.1 pooka int
209 1.1 pooka scsitest_match(device_t parent, cfdata_t match, void *aux)
210 1.1 pooka {
211 1.1 pooka #ifdef USE_TOSI_ISO
212 1.1 pooka uint64_t fsize;
213 1.1 pooka int error, ft;
214 1.1 pooka
215 1.1 pooka if (rumpuser_getfileinfo(MYCDISO, &fsize, &ft, &error))
216 1.1 pooka return 0;
217 1.1 pooka if (ft != RUMPUSER_FT_REG)
218 1.1 pooka return 0;
219 1.1 pooka mycdsize = fsize / CDBLOCKSIZE;
220 1.1 pooka
221 1.1 pooka if ((isofd = rumpuser_open(MYCDISO, RUMPUSER_OPEN_RDWR, &error)) == -1)
222 1.1 pooka return 0;
223 1.1 pooka #else
224 1.1 pooka /*
225 1.1 pooka * We pretend to have a medium present initially, so != -1.
226 1.1 pooka */
227 1.1 pooka isofd = -2;
228 1.1 pooka #endif
229 1.1 pooka
230 1.1 pooka return 1;
231 1.1 pooka }
232 1.1 pooka
233 1.1 pooka void
234 1.1 pooka scsitest_attach(device_t parent, device_t self, void *aux)
235 1.1 pooka {
236 1.1 pooka struct scsitest *sc = device_private(self);
237 1.1 pooka
238 1.1 pooka aprint_naive("\n");
239 1.1 pooka aprint_normal("\n");
240 1.1 pooka
241 1.1 pooka memset(&sc->sc_adapter, 0, sizeof(sc->sc_adapter));
242 1.1 pooka sc->sc_adapter.adapt_nchannels = 1;
243 1.1 pooka sc->sc_adapter.adapt_request = scsitest_request;
244 1.1 pooka sc->sc_adapter.adapt_minphys = minphys;
245 1.1 pooka sc->sc_adapter.adapt_dev = self;
246 1.1 pooka sc->sc_adapter.adapt_max_periph = 1;
247 1.1 pooka sc->sc_adapter.adapt_openings = 1;
248 1.1 pooka
249 1.1 pooka memset(&sc->sc_channel, 0, sizeof(sc->sc_channel));
250 1.1 pooka sc->sc_channel.chan_bustype = &scsi_bustype;
251 1.1 pooka sc->sc_channel.chan_ntargets = 2;
252 1.1 pooka sc->sc_channel.chan_nluns = 1;
253 1.1 pooka sc->sc_channel.chan_id = 0;
254 1.1 pooka sc->sc_channel.chan_flags = SCSIPI_CHAN_NOSETTLE;
255 1.1 pooka sc->sc_channel.chan_adapter = &sc->sc_adapter;
256 1.1 pooka
257 1.1 pooka config_found_ia(self, "scsi", &sc->sc_channel, scsiprint);
258 1.1 pooka }
259