1/* $NetBSD: umass_scsipi.c,v 1.54 2016/11/21 08:27:30 skrll 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@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.54 2016/11/21 08:27:30 skrll Exp $");
35
36#ifdef _KERNEL_OPT
37#include "opt_usb.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#include <dev/usb/usbhist.h>
76
77#include <dev/usb/umassvar.h>
78#include <dev/usb/umass_scsipi.h>
79
80struct 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
95Static void umass_scsipi_request(struct scsipi_channel *,
96 scsipi_adapter_req_t, void *);
97Static void umass_scsipi_minphys(struct buf *);
98Static int umass_scsipi_ioctl(struct scsipi_channel *, u_long,
99 void *, int, proc_t *);
100Static int umass_scsipi_getgeom(struct scsipi_periph *,
101 struct disk_parms *, u_long);
102
103Static void umass_scsipi_cb(struct umass_softc *, void *,
104 int, int);
105Static void umass_scsipi_sense_cb(struct umass_softc *, void *,
106 int, int);
107
108Static struct umass_scsipi_softc *umass_scsipi_setup(struct umass_softc *);
109
110#if NATAPIBUS > 0
111Static void umass_atapi_probe_device(struct atapibus_softc *, int);
112
113const 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
125int
126umass_scsi_attach(struct umass_softc *sc)
127{
128 UMASSHIST_FUNC(); UMASSHIST_CALLED();
129 struct umass_scsipi_softc *scbus;
130
131 scbus = umass_scsipi_setup(sc);
132
133 scbus->sc_channel.chan_bustype = &scsi_bustype;
134 scbus->sc_channel.chan_ntargets = 2;
135 scbus->sc_channel.chan_nluns = sc->maxlun + 1;
136 scbus->sc_channel.chan_id = scbus->sc_channel.chan_ntargets - 1;
137 DPRINTFM(UDMASS_USB, "sc %p: SCSI", sc, 0, 0, 0);
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
151int
152umass_atapi_attach(struct umass_softc *sc)
153{
154 UMASSHIST_FUNC(); UMASSHIST_CALLED();
155 struct umass_scsipi_softc *scbus;
156
157 scbus = umass_scsipi_setup(sc);
158 scbus->sc_atapi_adapter.atapi_probe_device = umass_atapi_probe_device;
159
160 scbus->sc_channel.chan_bustype = &umass_atapi_bustype;
161 scbus->sc_channel.chan_ntargets = 2;
162 scbus->sc_channel.chan_nluns = 1;
163
164 scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
165 DPRINTFM(UDMASS_USB, "sc %p: ATAPI", sc, 0, 0, 0);
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
178Static struct umass_scsipi_softc *
179umass_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 scbus->sc_adapter.adapt_flags = SCSIPI_ADAPT_MPSAFE;
198
199 /* Fill in the channel. */
200 memset(&scbus->sc_channel, 0, sizeof(scbus->sc_channel));
201 scbus->sc_channel.chan_adapter = &scbus->sc_adapter;
202 scbus->sc_channel.chan_channel = 0;
203 scbus->sc_channel.chan_flags = SCSIPI_CHAN_OPENINGS | SCSIPI_CHAN_NOSETTLE;
204 scbus->sc_channel.chan_openings = 1;
205 scbus->sc_channel.chan_max_periph = 1;
206 scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
207
208 return scbus;
209}
210
211Static void
212umass_scsipi_request(struct scsipi_channel *chan,
213 scsipi_adapter_req_t req, void *arg)
214{
215 UMASSHIST_FUNC(); UMASSHIST_CALLED();
216 struct scsipi_adapter *adapt = chan->chan_adapter;
217 struct scsipi_periph *periph;
218 struct scsipi_xfer *xs;
219 struct umass_softc *sc = device_private(adapt->adapt_dev);
220 struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
221 struct scsipi_generic *cmd;
222 int cmdlen;
223 int dir;
224#ifdef UMASS_DEBUG
225 microtime(&sc->tv);
226#endif
227 switch(req) {
228 case ADAPTER_REQ_RUN_XFER:
229 xs = arg;
230 periph = xs->xs_periph;
231 DIF(UDMASS_UPPER, periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS);
232
233 DPRINTFM(UDMASS_CMD, "sc %p: %d:%d xs=%p", sc,
234 periph->periph_target, periph->periph_lun, xs);
235 DPRINTFM(UDMASS_CMD, "cmd=0x%02x datalen=%d (quirks=0x%x, "
236 "poll=%d)", xs->cmd->opcode, xs->datalen,
237 periph->periph_quirks, !!(xs->xs_control & XS_CTL_POLL));
238#if defined(UMASS_DEBUG) && defined(SCSIPI_DEBUG)
239 if (umassdebug & UDMASS_SCSI)
240 show_scsipi_xs(xs);
241 else if (umassdebug & ~UDMASS_CMD)
242 show_scsipi_cmd(xs);
243#endif
244
245 if (sc->sc_dying) {
246 xs->error = XS_DRIVER_STUFFUP;
247 goto done;
248 }
249
250#ifdef UMASS_DEBUG
251 if (SCSIPI_BUSTYPE_TYPE(chan->chan_bustype->bustype_type) ==
252 SCSIPI_BUSTYPE_ATAPI ?
253 periph->periph_target != UMASS_ATAPI_DRIVE :
254 periph->periph_target == chan->chan_id) {
255 DPRINTFM(UDMASS_SCSI, "sc %p: wrong SCSI ID %d", sc,
256 periph->periph_target, 0, 0);
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 DPRINTFM(UDMASS_SCSI, "sync dir=%d\n", dir, 0, 0, 0);
287 scbus->sc_sync_status = USBD_INVAL;
288 sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
289 cmdlen, xs->data,
290 xs->datalen, dir,
291 xs->timeout, USBD_SYNCHRONOUS,
292 0, xs);
293 DPRINTFM(UDMASS_SCSI, "done err=%d",
294 scbus->sc_sync_status, 0, 0, 0);
295 switch (scbus->sc_sync_status) {
296 case USBD_NORMAL_COMPLETION:
297 xs->error = XS_NOERROR;
298 break;
299 case USBD_TIMEOUT:
300 xs->error = XS_TIMEOUT;
301 break;
302 default:
303 xs->error = XS_DRIVER_STUFFUP;
304 break;
305 }
306 goto done;
307 } else {
308 DPRINTFM(UDMASS_SCSI, "async dir=%d, cmdlen=%d"
309 " datalen=%d", dir, cmdlen, xs->datalen, 0);
310 sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
311 cmdlen, xs->data,
312 xs->datalen, dir,
313 xs->timeout, 0,
314 umass_scsipi_cb, xs);
315 return;
316 }
317
318 /* Return if command finishes early. */
319 done:
320 scsipi_done(xs);
321 return;
322 default:
323 /* Not supported, nothing to do. */
324 ;
325 }
326}
327
328Static void
329umass_scsipi_minphys(struct buf *bp)
330{
331#ifdef DIAGNOSTIC
332 if (bp->b_bcount <= 0) {
333 printf("umass_scsipi_minphys count(%d) <= 0\n",
334 bp->b_bcount);
335 bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
336 }
337#endif
338 if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE)
339 bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
340 minphys(bp);
341}
342
343int
344umass_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd,
345 void *arg, int flag, proc_t *p)
346{
347 /*struct umass_softc *sc = link->adapter_softc;*/
348 /*struct umass_scsipi_softc *scbus = sc->bus;*/
349
350 switch (cmd) {
351#if 0
352 case SCBUSIORESET:
353 ccb->ccb_h.status = CAM_REQ_INPROG;
354 umass_reset(sc, umass_cam_cb, (void *) ccb);
355 return 0;
356#endif
357 default:
358 return ENOTTY;
359 }
360}
361
362Static int
363umass_scsipi_getgeom(struct scsipi_periph *periph, struct disk_parms *dp,
364 u_long sectors)
365{
366 struct umass_softc *sc =
367 device_private(periph->periph_channel->chan_adapter->adapt_dev);
368
369 /* If it's not a floppy, we don't know what to do. */
370 if (sc->sc_cmd != UMASS_CPROTO_UFI)
371 return 0;
372
373 switch (sectors) {
374 case 1440:
375 /* Most likely a single density 3.5" floppy. */
376 dp->heads = 2;
377 dp->sectors = 9;
378 dp->cyls = 80;
379 return 1;
380 case 2880:
381 /* Most likely a double density 3.5" floppy. */
382 dp->heads = 2;
383 dp->sectors = 18;
384 dp->cyls = 80;
385 return 1;
386 default:
387 return 0;
388 }
389}
390
391Static void
392umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status)
393{
394 UMASSHIST_FUNC(); UMASSHIST_CALLED();
395 struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
396 struct scsipi_xfer *xs = priv;
397 struct scsipi_periph *periph = xs->xs_periph;
398 int cmdlen, senselen;
399#ifdef UMASS_DEBUG
400 struct timeval tv;
401 u_int delta;
402 microtime(&tv);
403 delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 + tv.tv_usec - sc->tv.tv_usec;
404 DPRINTFM(UDMASS_CMD, "delta=%u: xs=%p residue=%d status=%d", delta, xs,
405 residue, status);
406#endif
407
408
409 xs->resid = residue;
410
411 switch (status) {
412 case STATUS_CMD_OK:
413 xs->error = XS_NOERROR;
414 break;
415
416 case STATUS_CMD_UNKNOWN:
417 /* FALLTHROUGH */
418 case STATUS_CMD_FAILED:
419 /* fetch sense data */
420 sc->sc_sense = 1;
421 memset(&scbus->sc_sense_cmd, 0, sizeof(scbus->sc_sense_cmd));
422 scbus->sc_sense_cmd.opcode = SCSI_REQUEST_SENSE;
423 scbus->sc_sense_cmd.byte2 = periph->periph_lun <<
424 SCSI_CMD_LUN_SHIFT;
425
426 if (sc->sc_cmd == UMASS_CPROTO_UFI ||
427 sc->sc_cmd == UMASS_CPROTO_ATAPI)
428 cmdlen = UFI_COMMAND_LENGTH; /* XXX */
429 else
430 cmdlen = sizeof(scbus->sc_sense_cmd);
431 if (periph->periph_version < 0x05) /* SPC-3 */
432 senselen = 18;
433 else
434 senselen = sizeof(xs->sense);
435 scbus->sc_sense_cmd.length = senselen;
436 sc->sc_methods->wire_xfer(sc, periph->periph_lun,
437 &scbus->sc_sense_cmd, cmdlen,
438 &xs->sense, senselen,
439 DIR_IN, xs->timeout, 0,
440 umass_scsipi_sense_cb, xs);
441 return;
442
443 case STATUS_WIRE_FAILED:
444 xs->error = XS_RESET;
445 break;
446
447 default:
448 panic("%s: Unknown status %d in umass_scsipi_cb",
449 device_xname(sc->sc_dev), status);
450 }
451
452 DPRINTFM(UDMASS_CMD, "return xs->error=%d, xs->xs_status=0x%x"
453 " xs->resid=%d", xs->error, xs->xs_status, xs->resid, 0);
454
455 scsipi_done(xs);
456}
457
458/*
459 * Finalise a completed autosense operation
460 */
461Static void
462umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue,
463 int status)
464{
465 UMASSHIST_FUNC(); UMASSHIST_CALLED();
466 struct scsipi_xfer *xs = priv;
467
468 DPRINTFM(UDMASS_CMD, "sc %p: xs=%p residue=%d status=%d", sc, xs,
469 residue, status);
470
471 sc->sc_sense = 0;
472 switch (status) {
473 case STATUS_CMD_OK:
474 case STATUS_CMD_UNKNOWN:
475 /* getting sense data succeeded */
476 if (residue == 0 || residue == 14)/* XXX */
477 xs->error = XS_SENSE;
478 else
479 xs->error = XS_SHORTSENSE;
480 break;
481 default:
482 DPRINTFM(UDMASS_SCSI, "sc %p: Autosense failed, status %d",
483 sc, status, 0, 0);
484 xs->error = XS_DRIVER_STUFFUP;
485 break;
486 }
487
488 DPRINTFM(UDMASS_CMD, "return xs->error=%d, xs->xs_status=0x%x"
489 " xs->resid=%d", xs->error, xs->xs_status, xs->resid, 0);
490
491 scsipi_done(xs);
492}
493
494#if NATAPIBUS > 0
495Static void
496umass_atapi_probe_device(struct atapibus_softc *atapi, int target)
497{
498 UMASSHIST_FUNC(); UMASSHIST_CALLED();
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 DPRINTFM(UDMASS_SCSI, "atapi=%p target=%d", atapi, target, 0, 0);
506
507 if (target != UMASS_ATAPI_DRIVE) /* only probe drive 0 */
508 return;
509
510 /* skip if already attached */
511 if (scsipi_lookup_periph(chan, target, 0) != NULL) {
512 return;
513 }
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 DPRINTFM(UDMASS_SCSI, "doing inquiry", 0, 0, 0, 0);
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 DPRINTFM(UDMASS_SCSI, "scsipi_inquire failed", 0, 0, 0, 0);
533 free(periph, M_DEVBUF);
534 return;
535 }
536
537 strnvisx(vendor, sizeof(vendor), inqbuf.vendor, 8,
538 VIS_TRIM|VIS_SAFE|VIS_OCTAL);
539 strnvisx(product, sizeof(product), inqbuf.product, 16,
540 VIS_TRIM|VIS_SAFE|VIS_OCTAL);
541 strnvisx(revision, sizeof(revision), inqbuf.revision, 4,
542 VIS_TRIM|VIS_SAFE|VIS_OCTAL);
543
544 sa.sa_periph = periph;
545 sa.sa_inqbuf.type = inqbuf.device;
546 sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
547 T_REMOV : T_FIXED;
548 if (sa.sa_inqbuf.removable)
549 periph->periph_flags |= PERIPH_REMOVABLE;
550 sa.sa_inqbuf.vendor = vendor;
551 sa.sa_inqbuf.product = product;
552 sa.sa_inqbuf.revision = revision;
553 sa.sa_inqptr = NULL;
554
555 atapi_probe_device(atapi, target, periph, &sa);
556 /* atapi_probe_device() frees the periph when there is no device.*/
557}
558#endif
559