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