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