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