umass_scsipi.c revision 1.53 1 1.53 skrll /* $NetBSD: umass_scsipi.c,v 1.53 2016/07/03 07:27:37 skrll 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.53 skrll __KERNEL_RCSID(0, "$NetBSD: umass_scsipi.c,v 1.53 2016/07/03 07:27:37 skrll 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/systm.h>
45 1.1 augustss #include <sys/kernel.h>
46 1.1 augustss #include <sys/conf.h>
47 1.1 augustss #include <sys/buf.h>
48 1.21 yamt #include <sys/bufq.h>
49 1.1 augustss #include <sys/device.h>
50 1.1 augustss #include <sys/ioctl.h>
51 1.40 he #include <sys/lwp.h>
52 1.1 augustss #include <sys/malloc.h>
53 1.1 augustss
54 1.1 augustss /* SCSI & ATAPI */
55 1.1 augustss #include <sys/scsiio.h>
56 1.24 thorpej #include <dev/scsipi/scsi_spc.h>
57 1.1 augustss #include <dev/scsipi/scsi_all.h>
58 1.1 augustss #include <dev/scsipi/scsipi_all.h>
59 1.1 augustss #include <dev/scsipi/scsiconf.h>
60 1.1 augustss
61 1.1 augustss #include <dev/scsipi/atapiconf.h>
62 1.1 augustss
63 1.1 augustss #include <dev/scsipi/scsipi_disk.h>
64 1.1 augustss #include <dev/scsipi/scsi_disk.h>
65 1.1 augustss #include <dev/scsipi/scsi_changer.h>
66 1.1 augustss
67 1.1 augustss #include <sys/disk.h> /* XXX */
68 1.1 augustss #include <dev/scsipi/sdvar.h> /* XXX */
69 1.1 augustss
70 1.1 augustss /* USB */
71 1.1 augustss #include <dev/usb/usb.h>
72 1.1 augustss #include <dev/usb/usbdi.h>
73 1.1 augustss #include <dev/usb/usbdi_util.h>
74 1.1 augustss #include <dev/usb/usbdevs.h>
75 1.52 skrll #include <dev/usb/usbhist.h>
76 1.1 augustss
77 1.1 augustss #include <dev/usb/umassvar.h>
78 1.1 augustss #include <dev/usb/umass_scsipi.h>
79 1.1 augustss
80 1.1 augustss struct umass_scsipi_softc {
81 1.1 augustss struct umassbus_softc base;
82 1.1 augustss
83 1.1 augustss struct atapi_adapter sc_atapi_adapter;
84 1.1 augustss #define sc_adapter sc_atapi_adapter._generic
85 1.1 augustss struct scsipi_channel sc_channel;
86 1.1 augustss usbd_status sc_sync_status;
87 1.24 thorpej struct scsi_request_sense sc_sense_cmd;
88 1.1 augustss };
89 1.1 augustss
90 1.1 augustss
91 1.1 augustss #define SHORT_INQUIRY_LENGTH 36 /* XXX */
92 1.1 augustss
93 1.1 augustss #define UMASS_ATAPI_DRIVE 0
94 1.1 augustss
95 1.1 augustss Static void umass_scsipi_request(struct scsipi_channel *,
96 1.1 augustss scsipi_adapter_req_t, void *);
97 1.50 skrll Static void umass_scsipi_minphys(struct buf *);
98 1.1 augustss Static int umass_scsipi_ioctl(struct scsipi_channel *, u_long,
99 1.36 dyoung void *, int, proc_t *);
100 1.50 skrll Static int umass_scsipi_getgeom(struct scsipi_periph *,
101 1.50 skrll struct disk_parms *, u_long);
102 1.1 augustss
103 1.50 skrll Static void umass_scsipi_cb(struct umass_softc *, void *,
104 1.50 skrll int, int);
105 1.50 skrll Static void umass_scsipi_sense_cb(struct umass_softc *, void *,
106 1.50 skrll int, int);
107 1.1 augustss
108 1.50 skrll Static struct umass_scsipi_softc *umass_scsipi_setup(struct umass_softc *);
109 1.1 augustss
110 1.1 augustss #if NATAPIBUS > 0
111 1.1 augustss Static void umass_atapi_probe_device(struct atapibus_softc *, int);
112 1.1 augustss
113 1.1 augustss const struct scsipi_bustype umass_atapi_bustype = {
114 1.1 augustss SCSIPI_BUSTYPE_ATAPI,
115 1.1 augustss atapi_scsipi_cmd,
116 1.1 augustss atapi_interpret_sense,
117 1.1 augustss atapi_print_addr,
118 1.1 augustss scsi_kill_pending,
119 1.45 bouyer NULL,
120 1.1 augustss };
121 1.1 augustss #endif
122 1.1 augustss
123 1.1 augustss
124 1.1 augustss #if NSCSIBUS > 0
125 1.1 augustss int
126 1.1 augustss umass_scsi_attach(struct umass_softc *sc)
127 1.1 augustss {
128 1.52 skrll UMASSHIST_FUNC(); UMASSHIST_CALLED();
129 1.1 augustss struct umass_scsipi_softc *scbus;
130 1.1 augustss
131 1.1 augustss scbus = umass_scsipi_setup(sc);
132 1.1 augustss
133 1.1 augustss scbus->sc_channel.chan_bustype = &scsi_bustype;
134 1.16 mycroft scbus->sc_channel.chan_ntargets = 2;
135 1.1 augustss scbus->sc_channel.chan_nluns = sc->maxlun + 1;
136 1.16 mycroft scbus->sc_channel.chan_id = scbus->sc_channel.chan_ntargets - 1;
137 1.52 skrll DPRINTFM(UDMASS_USB, "sc %p: SCSI", sc, 0, 0, 0);
138 1.9 augustss
139 1.9 augustss sc->sc_refcnt++;
140 1.1 augustss scbus->base.sc_child =
141 1.33 cube config_found_ia(sc->sc_dev, "scsi", &scbus->sc_channel,
142 1.25 drochner scsiprint);
143 1.9 augustss if (--sc->sc_refcnt < 0)
144 1.42 mrg usb_detach_wakeupold(sc->sc_dev);
145 1.1 augustss
146 1.50 skrll return 0;
147 1.1 augustss }
148 1.1 augustss #endif
149 1.1 augustss
150 1.1 augustss #if NATAPIBUS > 0
151 1.1 augustss int
152 1.1 augustss umass_atapi_attach(struct umass_softc *sc)
153 1.1 augustss {
154 1.52 skrll UMASSHIST_FUNC(); UMASSHIST_CALLED();
155 1.1 augustss struct umass_scsipi_softc *scbus;
156 1.1 augustss
157 1.1 augustss scbus = umass_scsipi_setup(sc);
158 1.1 augustss scbus->sc_atapi_adapter.atapi_probe_device = umass_atapi_probe_device;
159 1.1 augustss
160 1.1 augustss scbus->sc_channel.chan_bustype = &umass_atapi_bustype;
161 1.1 augustss scbus->sc_channel.chan_ntargets = 2;
162 1.1 augustss scbus->sc_channel.chan_nluns = 1;
163 1.1 augustss
164 1.2 augustss scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
165 1.52 skrll DPRINTFM(UDMASS_USB, "sc %p: ATAPI", sc, 0, 0, 0);
166 1.9 augustss
167 1.9 augustss sc->sc_refcnt++;
168 1.1 augustss scbus->base.sc_child =
169 1.33 cube config_found_ia(sc->sc_dev, "atapi", &scbus->sc_channel,
170 1.25 drochner atapiprint);
171 1.9 augustss if (--sc->sc_refcnt < 0)
172 1.42 mrg usb_detach_wakeupold(sc->sc_dev);
173 1.1 augustss
174 1.50 skrll return 0;
175 1.1 augustss }
176 1.1 augustss #endif
177 1.1 augustss
178 1.1 augustss Static struct umass_scsipi_softc *
179 1.1 augustss umass_scsipi_setup(struct umass_softc *sc)
180 1.1 augustss {
181 1.1 augustss struct umass_scsipi_softc *scbus;
182 1.1 augustss
183 1.50 skrll scbus = malloc(sizeof(*scbus), M_DEVBUF, M_WAITOK | M_ZERO);
184 1.1 augustss sc->bus = &scbus->base;
185 1.3 augustss
186 1.3 augustss /* Only use big commands for USB SCSI devices. */
187 1.3 augustss sc->sc_busquirks |= PQUIRK_ONLYBIG;
188 1.1 augustss
189 1.1 augustss /* Fill in the adapter. */
190 1.1 augustss memset(&scbus->sc_adapter, 0, sizeof(scbus->sc_adapter));
191 1.33 cube scbus->sc_adapter.adapt_dev = sc->sc_dev;
192 1.1 augustss scbus->sc_adapter.adapt_nchannels = 1;
193 1.1 augustss scbus->sc_adapter.adapt_request = umass_scsipi_request;
194 1.1 augustss scbus->sc_adapter.adapt_minphys = umass_scsipi_minphys;
195 1.1 augustss scbus->sc_adapter.adapt_ioctl = umass_scsipi_ioctl;
196 1.1 augustss scbus->sc_adapter.adapt_getgeom = umass_scsipi_getgeom;
197 1.1 augustss
198 1.1 augustss /* Fill in the channel. */
199 1.1 augustss memset(&scbus->sc_channel, 0, sizeof(scbus->sc_channel));
200 1.1 augustss scbus->sc_channel.chan_adapter = &scbus->sc_adapter;
201 1.1 augustss scbus->sc_channel.chan_channel = 0;
202 1.15 mycroft scbus->sc_channel.chan_flags = SCSIPI_CHAN_OPENINGS | SCSIPI_CHAN_NOSETTLE;
203 1.1 augustss scbus->sc_channel.chan_openings = 1;
204 1.1 augustss scbus->sc_channel.chan_max_periph = 1;
205 1.2 augustss scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
206 1.1 augustss
207 1.50 skrll return scbus;
208 1.1 augustss }
209 1.5 augustss
210 1.1 augustss Static void
211 1.1 augustss umass_scsipi_request(struct scsipi_channel *chan,
212 1.1 augustss scsipi_adapter_req_t req, void *arg)
213 1.1 augustss {
214 1.52 skrll UMASSHIST_FUNC(); UMASSHIST_CALLED();
215 1.1 augustss struct scsipi_adapter *adapt = chan->chan_adapter;
216 1.1 augustss struct scsipi_periph *periph;
217 1.1 augustss struct scsipi_xfer *xs;
218 1.33 cube struct umass_softc *sc = device_private(adapt->adapt_dev);
219 1.1 augustss struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
220 1.13 mycroft struct scsipi_generic *cmd;
221 1.1 augustss int cmdlen;
222 1.1 augustss int dir;
223 1.1 augustss #ifdef UMASS_DEBUG
224 1.1 augustss microtime(&sc->tv);
225 1.1 augustss #endif
226 1.1 augustss switch(req) {
227 1.1 augustss case ADAPTER_REQ_RUN_XFER:
228 1.1 augustss xs = arg;
229 1.1 augustss periph = xs->xs_periph;
230 1.1 augustss DIF(UDMASS_UPPER, periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS);
231 1.1 augustss
232 1.52 skrll DPRINTFM(UDMASS_CMD, "sc %p: %d:%d xs=%p", sc,
233 1.52 skrll periph->periph_target, periph->periph_lun, xs);
234 1.52 skrll DPRINTFM(UDMASS_CMD, "cmd=0x%02x datalen=%d (quirks=0x%x, "
235 1.52 skrll "poll=%d)", xs->cmd->opcode, xs->datalen,
236 1.52 skrll periph->periph_quirks, !!(xs->xs_control & XS_CTL_POLL));
237 1.48 christos #if defined(UMASS_DEBUG) && defined(SCSIPI_DEBUG)
238 1.1 augustss if (umassdebug & UDMASS_SCSI)
239 1.1 augustss show_scsipi_xs(xs);
240 1.1 augustss else if (umassdebug & ~UDMASS_CMD)
241 1.1 augustss show_scsipi_cmd(xs);
242 1.1 augustss #endif
243 1.1 augustss
244 1.1 augustss if (sc->sc_dying) {
245 1.1 augustss xs->error = XS_DRIVER_STUFFUP;
246 1.1 augustss goto done;
247 1.1 augustss }
248 1.1 augustss
249 1.1 augustss #ifdef UMASS_DEBUG
250 1.44 bouyer if (SCSIPI_BUSTYPE_TYPE(chan->chan_bustype->bustype_type) ==
251 1.44 bouyer SCSIPI_BUSTYPE_ATAPI ?
252 1.1 augustss periph->periph_target != UMASS_ATAPI_DRIVE :
253 1.16 mycroft periph->periph_target == chan->chan_id) {
254 1.52 skrll DPRINTFM(UDMASS_SCSI, "sc %p: wrong SCSI ID %d", sc,
255 1.52 skrll periph->periph_target, 0, 0);
256 1.1 augustss xs->error = XS_DRIVER_STUFFUP;
257 1.1 augustss goto done;
258 1.1 augustss }
259 1.1 augustss #endif
260 1.1 augustss
261 1.1 augustss cmd = xs->cmd;
262 1.1 augustss cmdlen = xs->cmdlen;
263 1.1 augustss
264 1.1 augustss dir = DIR_NONE;
265 1.1 augustss if (xs->datalen) {
266 1.1 augustss switch (xs->xs_control &
267 1.1 augustss (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
268 1.1 augustss case XS_CTL_DATA_IN:
269 1.1 augustss dir = DIR_IN;
270 1.1 augustss break;
271 1.1 augustss case XS_CTL_DATA_OUT:
272 1.1 augustss dir = DIR_OUT;
273 1.1 augustss break;
274 1.1 augustss }
275 1.1 augustss }
276 1.1 augustss
277 1.1 augustss if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) {
278 1.1 augustss printf("umass_cmd: large datalen, %d\n", xs->datalen);
279 1.1 augustss xs->error = XS_DRIVER_STUFFUP;
280 1.1 augustss goto done;
281 1.1 augustss }
282 1.1 augustss
283 1.1 augustss if (xs->xs_control & XS_CTL_POLL) {
284 1.1 augustss /* Use sync transfer. XXX Broken! */
285 1.52 skrll DPRINTFM(UDMASS_SCSI, "sync dir=%d\n", dir, 0, 0, 0);
286 1.1 augustss scbus->sc_sync_status = USBD_INVAL;
287 1.1 augustss sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
288 1.1 augustss cmdlen, xs->data,
289 1.1 augustss xs->datalen, dir,
290 1.41 mrg xs->timeout, USBD_SYNCHRONOUS,
291 1.41 mrg 0, xs);
292 1.52 skrll DPRINTFM(UDMASS_SCSI, "done err=%d",
293 1.52 skrll scbus->sc_sync_status, 0, 0, 0);
294 1.1 augustss switch (scbus->sc_sync_status) {
295 1.1 augustss case USBD_NORMAL_COMPLETION:
296 1.1 augustss xs->error = XS_NOERROR;
297 1.1 augustss break;
298 1.1 augustss case USBD_TIMEOUT:
299 1.1 augustss xs->error = XS_TIMEOUT;
300 1.1 augustss break;
301 1.1 augustss default:
302 1.1 augustss xs->error = XS_DRIVER_STUFFUP;
303 1.1 augustss break;
304 1.1 augustss }
305 1.1 augustss goto done;
306 1.1 augustss } else {
307 1.52 skrll DPRINTFM(UDMASS_SCSI, "async dir=%d, cmdlen=%d"
308 1.52 skrll " datalen=%d", dir, cmdlen, xs->datalen, 0);
309 1.1 augustss sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
310 1.1 augustss cmdlen, xs->data,
311 1.1 augustss xs->datalen, dir,
312 1.41 mrg xs->timeout, 0,
313 1.1 augustss umass_scsipi_cb, xs);
314 1.1 augustss return;
315 1.1 augustss }
316 1.1 augustss
317 1.1 augustss /* Return if command finishes early. */
318 1.1 augustss done:
319 1.39 mrg KERNEL_LOCK(1, curlwp);
320 1.1 augustss scsipi_done(xs);
321 1.39 mrg KERNEL_UNLOCK_ONE(curlwp);
322 1.1 augustss return;
323 1.1 augustss default:
324 1.1 augustss /* Not supported, nothing to do. */
325 1.1 augustss ;
326 1.1 augustss }
327 1.1 augustss }
328 1.1 augustss
329 1.1 augustss Static void
330 1.1 augustss umass_scsipi_minphys(struct buf *bp)
331 1.1 augustss {
332 1.1 augustss #ifdef DIAGNOSTIC
333 1.1 augustss if (bp->b_bcount <= 0) {
334 1.20 yamt printf("umass_scsipi_minphys count(%d) <= 0\n",
335 1.1 augustss bp->b_bcount);
336 1.1 augustss bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
337 1.1 augustss }
338 1.1 augustss #endif
339 1.1 augustss if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE)
340 1.1 augustss bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
341 1.1 augustss minphys(bp);
342 1.1 augustss }
343 1.1 augustss
344 1.1 augustss int
345 1.29 christos umass_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd,
346 1.36 dyoung void *arg, int flag, proc_t *p)
347 1.1 augustss {
348 1.1 augustss /*struct umass_softc *sc = link->adapter_softc;*/
349 1.1 augustss /*struct umass_scsipi_softc *scbus = sc->bus;*/
350 1.1 augustss
351 1.1 augustss switch (cmd) {
352 1.1 augustss #if 0
353 1.1 augustss case SCBUSIORESET:
354 1.1 augustss ccb->ccb_h.status = CAM_REQ_INPROG;
355 1.1 augustss umass_reset(sc, umass_cam_cb, (void *) ccb);
356 1.50 skrll return 0;
357 1.1 augustss #endif
358 1.1 augustss default:
359 1.50 skrll return ENOTTY;
360 1.1 augustss }
361 1.1 augustss }
362 1.1 augustss
363 1.1 augustss Static int
364 1.1 augustss umass_scsipi_getgeom(struct scsipi_periph *periph, struct disk_parms *dp,
365 1.23 reinoud u_long sectors)
366 1.1 augustss {
367 1.1 augustss struct umass_softc *sc =
368 1.33 cube device_private(periph->periph_channel->chan_adapter->adapt_dev);
369 1.1 augustss
370 1.1 augustss /* If it's not a floppy, we don't know what to do. */
371 1.1 augustss if (sc->sc_cmd != UMASS_CPROTO_UFI)
372 1.50 skrll return 0;
373 1.1 augustss
374 1.1 augustss switch (sectors) {
375 1.1 augustss case 1440:
376 1.1 augustss /* Most likely a single density 3.5" floppy. */
377 1.1 augustss dp->heads = 2;
378 1.1 augustss dp->sectors = 9;
379 1.1 augustss dp->cyls = 80;
380 1.50 skrll return 1;
381 1.1 augustss case 2880:
382 1.1 augustss /* Most likely a double density 3.5" floppy. */
383 1.1 augustss dp->heads = 2;
384 1.1 augustss dp->sectors = 18;
385 1.1 augustss dp->cyls = 80;
386 1.50 skrll return 1;
387 1.1 augustss default:
388 1.50 skrll return 0;
389 1.1 augustss }
390 1.1 augustss }
391 1.1 augustss
392 1.1 augustss Static void
393 1.1 augustss umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status)
394 1.1 augustss {
395 1.52 skrll UMASSHIST_FUNC(); UMASSHIST_CALLED();
396 1.1 augustss struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
397 1.1 augustss struct scsipi_xfer *xs = priv;
398 1.1 augustss struct scsipi_periph *periph = xs->xs_periph;
399 1.46 drochner int cmdlen, senselen;
400 1.1 augustss int s;
401 1.1 augustss #ifdef UMASS_DEBUG
402 1.1 augustss struct timeval tv;
403 1.1 augustss u_int delta;
404 1.1 augustss microtime(&tv);
405 1.1 augustss delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 + tv.tv_usec - sc->tv.tv_usec;
406 1.53 skrll DPRINTFM(UDMASS_CMD, "delta=%u: xs=%p residue=%d status=%d", delta, xs,
407 1.53 skrll residue, status);
408 1.1 augustss #endif
409 1.1 augustss
410 1.1 augustss
411 1.1 augustss xs->resid = residue;
412 1.1 augustss
413 1.1 augustss switch (status) {
414 1.1 augustss case STATUS_CMD_OK:
415 1.1 augustss xs->error = XS_NOERROR;
416 1.1 augustss break;
417 1.1 augustss
418 1.1 augustss case STATUS_CMD_UNKNOWN:
419 1.1 augustss /* FALLTHROUGH */
420 1.1 augustss case STATUS_CMD_FAILED:
421 1.1 augustss /* fetch sense data */
422 1.11 mycroft sc->sc_sense = 1;
423 1.1 augustss memset(&scbus->sc_sense_cmd, 0, sizeof(scbus->sc_sense_cmd));
424 1.24 thorpej scbus->sc_sense_cmd.opcode = SCSI_REQUEST_SENSE;
425 1.1 augustss scbus->sc_sense_cmd.byte2 = periph->periph_lun <<
426 1.1 augustss SCSI_CMD_LUN_SHIFT;
427 1.1 augustss
428 1.18 mycroft if (sc->sc_cmd == UMASS_CPROTO_UFI ||
429 1.18 mycroft sc->sc_cmd == UMASS_CPROTO_ATAPI)
430 1.18 mycroft cmdlen = UFI_COMMAND_LENGTH; /* XXX */
431 1.18 mycroft else
432 1.18 mycroft cmdlen = sizeof(scbus->sc_sense_cmd);
433 1.46 drochner if (periph->periph_version < 0x05) /* SPC-3 */
434 1.46 drochner senselen = 18;
435 1.46 drochner else
436 1.46 drochner senselen = sizeof(xs->sense);
437 1.46 drochner scbus->sc_sense_cmd.length = senselen;
438 1.1 augustss sc->sc_methods->wire_xfer(sc, periph->periph_lun,
439 1.1 augustss &scbus->sc_sense_cmd, cmdlen,
440 1.46 drochner &xs->sense, senselen,
441 1.41 mrg DIR_IN, xs->timeout, 0,
442 1.1 augustss umass_scsipi_sense_cb, xs);
443 1.1 augustss return;
444 1.1 augustss
445 1.1 augustss case STATUS_WIRE_FAILED:
446 1.1 augustss xs->error = XS_RESET;
447 1.1 augustss break;
448 1.1 augustss
449 1.1 augustss default:
450 1.6 provos panic("%s: Unknown status %d in umass_scsipi_cb",
451 1.36 dyoung device_xname(sc->sc_dev), status);
452 1.1 augustss }
453 1.1 augustss
454 1.52 skrll DPRINTFM(UDMASS_CMD, "return xs->error=%d, xs->xs_status=0x%x"
455 1.52 skrll " xs->resid=%d", xs->error, xs->xs_status, xs->resid, 0);
456 1.1 augustss
457 1.1 augustss s = splbio();
458 1.39 mrg KERNEL_LOCK(1, curlwp);
459 1.1 augustss scsipi_done(xs);
460 1.39 mrg KERNEL_UNLOCK_ONE(curlwp);
461 1.1 augustss splx(s);
462 1.1 augustss }
463 1.1 augustss
464 1.5 augustss /*
465 1.1 augustss * Finalise a completed autosense operation
466 1.1 augustss */
467 1.1 augustss Static void
468 1.1 augustss umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue,
469 1.1 augustss int status)
470 1.1 augustss {
471 1.52 skrll UMASSHIST_FUNC(); UMASSHIST_CALLED();
472 1.1 augustss struct scsipi_xfer *xs = priv;
473 1.1 augustss int s;
474 1.1 augustss
475 1.52 skrll DPRINTFM(UDMASS_CMD, "sc %p: xs=%p residue=%d status=%d", sc, xs,
476 1.52 skrll residue, status);
477 1.1 augustss
478 1.11 mycroft sc->sc_sense = 0;
479 1.1 augustss switch (status) {
480 1.1 augustss case STATUS_CMD_OK:
481 1.1 augustss case STATUS_CMD_UNKNOWN:
482 1.1 augustss /* getting sense data succeeded */
483 1.1 augustss if (residue == 0 || residue == 14)/* XXX */
484 1.1 augustss xs->error = XS_SENSE;
485 1.1 augustss else
486 1.1 augustss xs->error = XS_SHORTSENSE;
487 1.1 augustss break;
488 1.1 augustss default:
489 1.52 skrll DPRINTFM(UDMASS_SCSI, "sc %p: Autosense failed, status %d",
490 1.52 skrll sc, status, 0, 0);
491 1.1 augustss xs->error = XS_DRIVER_STUFFUP;
492 1.1 augustss break;
493 1.1 augustss }
494 1.1 augustss
495 1.52 skrll DPRINTFM(UDMASS_CMD, "return xs->error=%d, xs->xs_status=0x%x"
496 1.52 skrll " xs->resid=%d", xs->error, xs->xs_status, xs->resid, 0);
497 1.1 augustss
498 1.1 augustss s = splbio();
499 1.39 mrg KERNEL_LOCK(1, curlwp);
500 1.1 augustss scsipi_done(xs);
501 1.39 mrg KERNEL_UNLOCK_ONE(curlwp);
502 1.1 augustss splx(s);
503 1.1 augustss }
504 1.1 augustss
505 1.1 augustss #if NATAPIBUS > 0
506 1.1 augustss Static void
507 1.1 augustss umass_atapi_probe_device(struct atapibus_softc *atapi, int target)
508 1.1 augustss {
509 1.52 skrll UMASSHIST_FUNC(); UMASSHIST_CALLED();
510 1.1 augustss struct scsipi_channel *chan = atapi->sc_channel;
511 1.1 augustss struct scsipi_periph *periph;
512 1.1 augustss struct scsipibus_attach_args sa;
513 1.1 augustss char vendor[33], product[65], revision[17];
514 1.1 augustss struct scsipi_inquiry_data inqbuf;
515 1.1 augustss
516 1.52 skrll DPRINTFM(UDMASS_SCSI, "atapi=%p target=%d", atapi, target, 0, 0);
517 1.1 augustss
518 1.1 augustss if (target != UMASS_ATAPI_DRIVE) /* only probe drive 0 */
519 1.1 augustss return;
520 1.1 augustss
521 1.39 mrg KERNEL_LOCK(1, curlwp);
522 1.39 mrg
523 1.1 augustss /* skip if already attached */
524 1.39 mrg if (scsipi_lookup_periph(chan, target, 0) != NULL) {
525 1.39 mrg KERNEL_UNLOCK_ONE(curlwp);
526 1.1 augustss return;
527 1.39 mrg }
528 1.1 augustss
529 1.1 augustss periph = scsipi_alloc_periph(M_NOWAIT);
530 1.1 augustss if (periph == NULL) {
531 1.39 mrg KERNEL_UNLOCK_ONE(curlwp);
532 1.31 cube aprint_error_dev(atapi->sc_dev,
533 1.31 cube "can't allocate link for drive %d\n", target);
534 1.5 augustss return;
535 1.1 augustss }
536 1.1 augustss
537 1.1 augustss DIF(UDMASS_UPPER, periph->periph_dbflags |= 1); /* XXX 1 */
538 1.1 augustss periph->periph_channel = chan;
539 1.1 augustss periph->periph_switch = &atapi_probe_periphsw;
540 1.1 augustss periph->periph_target = target;
541 1.1 augustss periph->periph_quirks = chan->chan_defquirks;
542 1.1 augustss
543 1.52 skrll DPRINTFM(UDMASS_SCSI, "doing inquiry", 0, 0, 0, 0);
544 1.1 augustss /* Now go ask the device all about itself. */
545 1.1 augustss memset(&inqbuf, 0, sizeof(inqbuf));
546 1.35 rmind if (scsipi_inquire(periph, &inqbuf, XS_CTL_DISCOVERY) != 0) {
547 1.39 mrg KERNEL_UNLOCK_ONE(curlwp);
548 1.52 skrll DPRINTFM(UDMASS_SCSI, "scsipi_inquire failed", 0, 0, 0, 0);
549 1.1 augustss free(periph, M_DEVBUF);
550 1.1 augustss return;
551 1.1 augustss }
552 1.1 augustss
553 1.51 christos strnvisx(vendor, sizeof(vendor), inqbuf.vendor, 8,
554 1.51 christos VIS_TRIM|VIS_SAFE|VIS_OCTAL);
555 1.51 christos strnvisx(product, sizeof(product), inqbuf.product, 16,
556 1.51 christos VIS_TRIM|VIS_SAFE|VIS_OCTAL);
557 1.51 christos strnvisx(revision, sizeof(revision), inqbuf.revision, 4,
558 1.51 christos VIS_TRIM|VIS_SAFE|VIS_OCTAL);
559 1.1 augustss
560 1.1 augustss sa.sa_periph = periph;
561 1.1 augustss sa.sa_inqbuf.type = inqbuf.device;
562 1.1 augustss sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
563 1.1 augustss T_REMOV : T_FIXED;
564 1.1 augustss if (sa.sa_inqbuf.removable)
565 1.1 augustss periph->periph_flags |= PERIPH_REMOVABLE;
566 1.1 augustss sa.sa_inqbuf.vendor = vendor;
567 1.1 augustss sa.sa_inqbuf.product = product;
568 1.1 augustss sa.sa_inqbuf.revision = revision;
569 1.1 augustss sa.sa_inqptr = NULL;
570 1.1 augustss
571 1.1 augustss atapi_probe_device(atapi, target, periph, &sa);
572 1.1 augustss /* atapi_probe_device() frees the periph when there is no device.*/
573 1.39 mrg
574 1.39 mrg KERNEL_UNLOCK_ONE(curlwp);
575 1.1 augustss }
576 1.1 augustss #endif
577