umass_scsipi.c revision 1.37 1 /* $NetBSD: umass_scsipi.c,v 1.37 2011/08/23 16:16:43 christos Exp $ */
2
3 /*
4 * Copyright (c) 2001, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) at
9 * Carlstedt Research & Technology and by Charles M. Hamnnum.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: umass_scsipi.c,v 1.37 2011/08/23 16:16:43 christos Exp $");
35
36 #include "opt_umass.h"
37
38 #include "atapibus.h"
39 #include "scsibus.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/conf.h>
45 #include <sys/buf.h>
46 #include <sys/bufq.h>
47 #include <sys/device.h>
48 #include <sys/ioctl.h>
49 #include <sys/malloc.h>
50
51 /* SCSI & ATAPI */
52 #include <sys/scsiio.h>
53 #include <dev/scsipi/scsi_spc.h>
54 #include <dev/scsipi/scsi_all.h>
55 #include <dev/scsipi/scsipi_all.h>
56 #include <dev/scsipi/scsiconf.h>
57
58 #include <dev/scsipi/atapiconf.h>
59
60 #include <dev/scsipi/scsipi_disk.h>
61 #include <dev/scsipi/scsi_disk.h>
62 #include <dev/scsipi/scsi_changer.h>
63
64 #include <sys/disk.h> /* XXX */
65 #include <dev/scsipi/sdvar.h> /* XXX */
66
67 /* USB */
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbdevs.h>
72
73 #include <dev/usb/umassvar.h>
74 #include <dev/usb/umass_scsipi.h>
75
76 struct umass_scsipi_softc {
77 struct umassbus_softc base;
78
79 struct atapi_adapter sc_atapi_adapter;
80 #define sc_adapter sc_atapi_adapter._generic
81 struct scsipi_channel sc_channel;
82 usbd_status sc_sync_status;
83 struct scsi_request_sense sc_sense_cmd;
84 };
85
86
87 #define SHORT_INQUIRY_LENGTH 36 /* XXX */
88
89 #define UMASS_ATAPI_DRIVE 0
90
91 Static void umass_scsipi_request(struct scsipi_channel *,
92 scsipi_adapter_req_t, void *);
93 Static void umass_scsipi_minphys(struct buf *bp);
94 Static int umass_scsipi_ioctl(struct scsipi_channel *, u_long,
95 void *, int, proc_t *);
96 Static int umass_scsipi_getgeom(struct scsipi_periph *periph,
97 struct disk_parms *, u_long sectors);
98
99 Static void umass_scsipi_cb(struct umass_softc *sc, void *priv,
100 int residue, int status);
101 Static void umass_scsipi_sense_cb(struct umass_softc *sc, void *priv,
102 int residue, int status);
103
104 Static struct umass_scsipi_softc *umass_scsipi_setup(struct umass_softc *sc);
105
106 #if NATAPIBUS > 0
107 Static void umass_atapi_probe_device(struct atapibus_softc *, int);
108
109 const struct scsipi_bustype umass_atapi_bustype = {
110 SCSIPI_BUSTYPE_ATAPI,
111 atapi_scsipi_cmd,
112 atapi_interpret_sense,
113 atapi_print_addr,
114 scsi_kill_pending,
115 };
116 #endif
117
118
119 #if NSCSIBUS > 0
120 int
121 umass_scsi_attach(struct umass_softc *sc)
122 {
123 struct umass_scsipi_softc *scbus;
124
125 scbus = umass_scsipi_setup(sc);
126
127 scbus->sc_channel.chan_bustype = &scsi_bustype;
128 scbus->sc_channel.chan_ntargets = 2;
129 scbus->sc_channel.chan_nluns = sc->maxlun + 1;
130 scbus->sc_channel.chan_id = scbus->sc_channel.chan_ntargets - 1;
131 DPRINTF(UDMASS_USB, ("%s: umass_attach_bus: SCSI\n",
132 device_xname(sc->sc_dev)));
133
134 sc->sc_refcnt++;
135 scbus->base.sc_child =
136 config_found_ia(sc->sc_dev, "scsi", &scbus->sc_channel,
137 scsiprint);
138 if (--sc->sc_refcnt < 0)
139 usb_detach_wakeup(sc->sc_dev);
140
141 return (0);
142 }
143 #endif
144
145 #if NATAPIBUS > 0
146 int
147 umass_atapi_attach(struct umass_softc *sc)
148 {
149 struct umass_scsipi_softc *scbus;
150
151 scbus = umass_scsipi_setup(sc);
152 scbus->sc_atapi_adapter.atapi_probe_device = umass_atapi_probe_device;
153
154 scbus->sc_channel.chan_bustype = &umass_atapi_bustype;
155 scbus->sc_channel.chan_ntargets = 2;
156 scbus->sc_channel.chan_nluns = 1;
157
158 scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
159 DPRINTF(UDMASS_USB, ("%s: umass_attach_bus: ATAPI\n",
160 device_xname(sc->sc_dev)));
161
162 sc->sc_refcnt++;
163 scbus->base.sc_child =
164 config_found_ia(sc->sc_dev, "atapi", &scbus->sc_channel,
165 atapiprint);
166 if (--sc->sc_refcnt < 0)
167 usb_detach_wakeup(sc->sc_dev);
168
169 return (0);
170 }
171 #endif
172
173 Static struct umass_scsipi_softc *
174 umass_scsipi_setup(struct umass_softc *sc)
175 {
176 struct umass_scsipi_softc *scbus;
177
178 scbus = malloc(sizeof *scbus, M_DEVBUF, M_WAITOK | M_ZERO);
179 sc->bus = &scbus->base;
180
181 /* Only use big commands for USB SCSI devices. */
182 sc->sc_busquirks |= PQUIRK_ONLYBIG;
183
184 /* Fill in the adapter. */
185 memset(&scbus->sc_adapter, 0, sizeof(scbus->sc_adapter));
186 scbus->sc_adapter.adapt_dev = sc->sc_dev;
187 scbus->sc_adapter.adapt_nchannels = 1;
188 scbus->sc_adapter.adapt_request = umass_scsipi_request;
189 scbus->sc_adapter.adapt_minphys = umass_scsipi_minphys;
190 scbus->sc_adapter.adapt_ioctl = umass_scsipi_ioctl;
191 scbus->sc_adapter.adapt_getgeom = umass_scsipi_getgeom;
192
193 /* Fill in the channel. */
194 memset(&scbus->sc_channel, 0, sizeof(scbus->sc_channel));
195 scbus->sc_channel.chan_adapter = &scbus->sc_adapter;
196 scbus->sc_channel.chan_channel = 0;
197 scbus->sc_channel.chan_flags = SCSIPI_CHAN_OPENINGS | SCSIPI_CHAN_NOSETTLE;
198 scbus->sc_channel.chan_openings = 1;
199 scbus->sc_channel.chan_max_periph = 1;
200 scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
201
202 return (scbus);
203 }
204
205 Static void
206 umass_scsipi_request(struct scsipi_channel *chan,
207 scsipi_adapter_req_t req, void *arg)
208 {
209 struct scsipi_adapter *adapt = chan->chan_adapter;
210 struct scsipi_periph *periph;
211 struct scsipi_xfer *xs;
212 struct umass_softc *sc = device_private(adapt->adapt_dev);
213 struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
214 struct scsipi_generic *cmd;
215 int cmdlen;
216 int dir;
217 #ifdef UMASS_DEBUG
218 microtime(&sc->tv);
219 #endif
220 switch(req) {
221 case ADAPTER_REQ_RUN_XFER:
222 xs = arg;
223 periph = xs->xs_periph;
224 DIF(UDMASS_UPPER, periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS);
225
226 DPRINTF(UDMASS_CMD, ("%s: umass_scsi_cmd: at %"PRIu64".%06"PRIu64": %d:%d "
227 "xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n",
228 device_xname(sc->sc_dev), sc->tv.tv_sec, (uint64_t)sc->tv.tv_usec,
229 periph->periph_target, periph->periph_lun,
230 xs, xs->cmd->opcode, xs->datalen,
231 periph->periph_quirks, xs->xs_control & XS_CTL_POLL));
232 #if defined(USB_DEBUG) && defined(SCSIPI_DEBUG)
233 if (umassdebug & UDMASS_SCSI)
234 show_scsipi_xs(xs);
235 else if (umassdebug & ~UDMASS_CMD)
236 show_scsipi_cmd(xs);
237 #endif
238
239 if (sc->sc_dying) {
240 xs->error = XS_DRIVER_STUFFUP;
241 goto done;
242 }
243
244 #ifdef UMASS_DEBUG
245 if (chan->chan_bustype->bustype_type == SCSIPI_BUSTYPE_ATAPI ?
246 periph->periph_target != UMASS_ATAPI_DRIVE :
247 periph->periph_target == chan->chan_id) {
248 DPRINTF(UDMASS_SCSI, ("%s: wrong SCSI ID %d\n",
249 device_xname(sc->sc_dev),
250 periph->periph_target));
251 xs->error = XS_DRIVER_STUFFUP;
252 goto done;
253 }
254 #endif
255
256 cmd = xs->cmd;
257 cmdlen = xs->cmdlen;
258
259 dir = DIR_NONE;
260 if (xs->datalen) {
261 switch (xs->xs_control &
262 (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
263 case XS_CTL_DATA_IN:
264 dir = DIR_IN;
265 break;
266 case XS_CTL_DATA_OUT:
267 dir = DIR_OUT;
268 break;
269 }
270 }
271
272 if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) {
273 printf("umass_cmd: large datalen, %d\n", xs->datalen);
274 xs->error = XS_DRIVER_STUFFUP;
275 goto done;
276 }
277
278 if (xs->xs_control & XS_CTL_POLL) {
279 /* Use sync transfer. XXX Broken! */
280 DPRINTF(UDMASS_SCSI,
281 ("umass_scsi_cmd: sync dir=%d\n", dir));
282 sc->sc_xfer_flags = USBD_SYNCHRONOUS;
283 scbus->sc_sync_status = USBD_INVAL;
284 sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
285 cmdlen, xs->data,
286 xs->datalen, dir,
287 xs->timeout, 0, xs);
288 sc->sc_xfer_flags = 0;
289 DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: done err=%d\n",
290 scbus->sc_sync_status));
291 switch (scbus->sc_sync_status) {
292 case USBD_NORMAL_COMPLETION:
293 xs->error = XS_NOERROR;
294 break;
295 case USBD_TIMEOUT:
296 xs->error = XS_TIMEOUT;
297 break;
298 default:
299 xs->error = XS_DRIVER_STUFFUP;
300 break;
301 }
302 goto done;
303 } else {
304 DPRINTF(UDMASS_SCSI,
305 ("umass_scsi_cmd: async dir=%d, cmdlen=%d"
306 " datalen=%d\n",
307 dir, cmdlen, xs->datalen));
308 sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
309 cmdlen, xs->data,
310 xs->datalen, dir,
311 xs->timeout,
312 umass_scsipi_cb, xs);
313 return;
314 }
315
316 /* Return if command finishes early. */
317 done:
318 scsipi_done(xs);
319 return;
320 default:
321 /* Not supported, nothing to do. */
322 ;
323 }
324 }
325
326 Static void
327 umass_scsipi_minphys(struct buf *bp)
328 {
329 #ifdef DIAGNOSTIC
330 if (bp->b_bcount <= 0) {
331 printf("umass_scsipi_minphys count(%d) <= 0\n",
332 bp->b_bcount);
333 bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
334 }
335 #endif
336 if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE)
337 bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
338 minphys(bp);
339 }
340
341 int
342 umass_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd,
343 void *arg, int flag, proc_t *p)
344 {
345 /*struct umass_softc *sc = link->adapter_softc;*/
346 /*struct umass_scsipi_softc *scbus = sc->bus;*/
347
348 switch (cmd) {
349 #if 0
350 case SCBUSIORESET:
351 ccb->ccb_h.status = CAM_REQ_INPROG;
352 umass_reset(sc, umass_cam_cb, (void *) ccb);
353 return (0);
354 #endif
355 default:
356 return (ENOTTY);
357 }
358 }
359
360 Static int
361 umass_scsipi_getgeom(struct scsipi_periph *periph, struct disk_parms *dp,
362 u_long sectors)
363 {
364 struct umass_softc *sc =
365 device_private(periph->periph_channel->chan_adapter->adapt_dev);
366
367 /* If it's not a floppy, we don't know what to do. */
368 if (sc->sc_cmd != UMASS_CPROTO_UFI)
369 return (0);
370
371 switch (sectors) {
372 case 1440:
373 /* Most likely a single density 3.5" floppy. */
374 dp->heads = 2;
375 dp->sectors = 9;
376 dp->cyls = 80;
377 return (1);
378 case 2880:
379 /* Most likely a double density 3.5" floppy. */
380 dp->heads = 2;
381 dp->sectors = 18;
382 dp->cyls = 80;
383 return (1);
384 default:
385 return (0);
386 }
387 }
388
389 Static void
390 umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status)
391 {
392 struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
393 struct scsipi_xfer *xs = priv;
394 struct scsipi_periph *periph = xs->xs_periph;
395 int cmdlen;
396 int s;
397 #ifdef UMASS_DEBUG
398 struct timeval tv;
399 u_int delta;
400 microtime(&tv);
401 delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 + tv.tv_usec - sc->tv.tv_usec;
402 #endif
403
404 DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %"PRIu64".%06"PRIu64", delta=%u: xs=%p residue=%d"
405 " status=%d\n", tv.tv_sec, (uint64_t)tv.tv_usec, delta, xs, residue, status));
406
407 xs->resid = residue;
408
409 switch (status) {
410 case STATUS_CMD_OK:
411 xs->error = XS_NOERROR;
412 break;
413
414 case STATUS_CMD_UNKNOWN:
415 /* FALLTHROUGH */
416 case STATUS_CMD_FAILED:
417 /* fetch sense data */
418 sc->sc_sense = 1;
419 memset(&scbus->sc_sense_cmd, 0, sizeof(scbus->sc_sense_cmd));
420 scbus->sc_sense_cmd.opcode = SCSI_REQUEST_SENSE;
421 scbus->sc_sense_cmd.byte2 = periph->periph_lun <<
422 SCSI_CMD_LUN_SHIFT;
423 scbus->sc_sense_cmd.length = sizeof(xs->sense);
424
425 if (sc->sc_cmd == UMASS_CPROTO_UFI ||
426 sc->sc_cmd == UMASS_CPROTO_ATAPI)
427 cmdlen = UFI_COMMAND_LENGTH; /* XXX */
428 else
429 cmdlen = sizeof(scbus->sc_sense_cmd);
430 sc->sc_methods->wire_xfer(sc, periph->periph_lun,
431 &scbus->sc_sense_cmd, cmdlen,
432 &xs->sense, sizeof(xs->sense),
433 DIR_IN, xs->timeout,
434 umass_scsipi_sense_cb, xs);
435 return;
436
437 case STATUS_WIRE_FAILED:
438 xs->error = XS_RESET;
439 break;
440
441 default:
442 panic("%s: Unknown status %d in umass_scsipi_cb",
443 device_xname(sc->sc_dev), status);
444 }
445
446 DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %"PRIu64".%06"PRIu64": return xs->error="
447 "%d, xs->xs_status=0x%x xs->resid=%d\n",
448 tv.tv_sec, (uint64_t)tv.tv_usec,
449 xs->error, xs->xs_status, xs->resid));
450
451 s = splbio();
452 scsipi_done(xs);
453 splx(s);
454 }
455
456 /*
457 * Finalise a completed autosense operation
458 */
459 Static void
460 umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue,
461 int status)
462 {
463 struct scsipi_xfer *xs = priv;
464 int s;
465
466 DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: xs=%p residue=%d "
467 "status=%d\n", xs, residue, status));
468
469 sc->sc_sense = 0;
470 switch (status) {
471 case STATUS_CMD_OK:
472 case STATUS_CMD_UNKNOWN:
473 /* getting sense data succeeded */
474 if (residue == 0 || residue == 14)/* XXX */
475 xs->error = XS_SENSE;
476 else
477 xs->error = XS_SHORTSENSE;
478 break;
479 default:
480 DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
481 device_xname(sc->sc_dev), status));
482 xs->error = XS_DRIVER_STUFFUP;
483 break;
484 }
485
486 DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: return xs->error=%d, "
487 "xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status,
488 xs->resid));
489
490 s = splbio();
491 scsipi_done(xs);
492 splx(s);
493 }
494
495 #if NATAPIBUS > 0
496 Static void
497 umass_atapi_probe_device(struct atapibus_softc *atapi, int target)
498 {
499 struct scsipi_channel *chan = atapi->sc_channel;
500 struct scsipi_periph *periph;
501 struct scsipibus_attach_args sa;
502 char vendor[33], product[65], revision[17];
503 struct scsipi_inquiry_data inqbuf;
504
505 DPRINTF(UDMASS_SCSI,("umass_atapi_probe_device: atapi=%p target=%d\n",
506 atapi, target));
507
508 if (target != UMASS_ATAPI_DRIVE) /* only probe drive 0 */
509 return;
510
511 /* skip if already attached */
512 if (scsipi_lookup_periph(chan, target, 0) != NULL)
513 return;
514
515 periph = scsipi_alloc_periph(M_NOWAIT);
516 if (periph == NULL) {
517 aprint_error_dev(atapi->sc_dev,
518 "can't allocate link for drive %d\n", target);
519 return;
520 }
521
522 DIF(UDMASS_UPPER, periph->periph_dbflags |= 1); /* XXX 1 */
523 periph->periph_channel = chan;
524 periph->periph_switch = &atapi_probe_periphsw;
525 periph->periph_target = target;
526 periph->periph_quirks = chan->chan_defquirks;
527
528 DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: doing inquiry\n"));
529 /* Now go ask the device all about itself. */
530 memset(&inqbuf, 0, sizeof(inqbuf));
531 if (scsipi_inquire(periph, &inqbuf, XS_CTL_DISCOVERY) != 0) {
532 DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: "
533 "scsipi_inquire failed\n"));
534 free(periph, M_DEVBUF);
535 return;
536 }
537
538 scsipi_strvis(vendor, 33, inqbuf.vendor, 8);
539 scsipi_strvis(product, 65, inqbuf.product, 16);
540 scsipi_strvis(revision, 17, inqbuf.revision, 4);
541
542 sa.sa_periph = periph;
543 sa.sa_inqbuf.type = inqbuf.device;
544 sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
545 T_REMOV : T_FIXED;
546 if (sa.sa_inqbuf.removable)
547 periph->periph_flags |= PERIPH_REMOVABLE;
548 sa.sa_inqbuf.vendor = vendor;
549 sa.sa_inqbuf.product = product;
550 sa.sa_inqbuf.revision = revision;
551 sa.sa_inqptr = NULL;
552
553 DPRINTF(UDMASS_SCSI, ("umass_atapi_probedev: doing atapi_probedev on "
554 "'%s' '%s' '%s'\n", vendor, product, revision));
555 atapi_probe_device(atapi, target, periph, &sa);
556 /* atapi_probe_device() frees the periph when there is no device.*/
557 }
558 #endif
559