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