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