umass_scsipi.c revision 1.55 1 /* $NetBSD: umass_scsipi.c,v 1.55 2017/10/28 00:37:12 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 2017/10/28 00:37:12 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/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
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 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 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 %#jxp: ATAPI", (uintptr_t)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
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 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
211 Static void
212 umass_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 %#jxp: %jd:%jd xs=%#jxp",
234 (uintptr_t)sc, periph->periph_target, periph->periph_lun,
235 (uintptr_t)xs);
236 DPRINTFM(UDMASS_CMD, "cmd=0x%02jx datalen=%jd (quirks=0x%jx, "
237 "poll=%jd)", xs->cmd->opcode, xs->datalen,
238 periph->periph_quirks, !!(xs->xs_control & XS_CTL_POLL));
239 #if defined(UMASS_DEBUG) && defined(SCSIPI_DEBUG)
240 if (umassdebug & UDMASS_SCSI)
241 show_scsipi_xs(xs);
242 else if (umassdebug & ~UDMASS_CMD)
243 show_scsipi_cmd(xs);
244 #endif
245
246 if (sc->sc_dying) {
247 xs->error = XS_DRIVER_STUFFUP;
248 goto done;
249 }
250
251 #ifdef UMASS_DEBUG
252 if (SCSIPI_BUSTYPE_TYPE(chan->chan_bustype->bustype_type) ==
253 SCSIPI_BUSTYPE_ATAPI ?
254 periph->periph_target != UMASS_ATAPI_DRIVE :
255 periph->periph_target == chan->chan_id) {
256 DPRINTFM(UDMASS_SCSI, "sc %#jx: wrong SCSI ID %jd",
257 (uintptr_t)sc, periph->periph_target, 0, 0);
258 xs->error = XS_DRIVER_STUFFUP;
259 goto done;
260 }
261 #endif
262
263 cmd = xs->cmd;
264 cmdlen = xs->cmdlen;
265
266 dir = DIR_NONE;
267 if (xs->datalen) {
268 switch (xs->xs_control &
269 (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
270 case XS_CTL_DATA_IN:
271 dir = DIR_IN;
272 break;
273 case XS_CTL_DATA_OUT:
274 dir = DIR_OUT;
275 break;
276 }
277 }
278
279 if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) {
280 printf("umass_cmd: large datalen, %d\n", xs->datalen);
281 xs->error = XS_DRIVER_STUFFUP;
282 goto done;
283 }
284
285 if (xs->xs_control & XS_CTL_POLL) {
286 /* Use sync transfer. XXX Broken! */
287 DPRINTFM(UDMASS_SCSI, "sync dir=%jd\n", dir, 0, 0, 0);
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 DPRINTFM(UDMASS_SCSI, "done err=%jd",
295 scbus->sc_sync_status, 0, 0, 0);
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 DPRINTFM(UDMASS_SCSI, "async dir=%jd, cmdlen=%jd"
310 " datalen=%jd", dir, cmdlen, xs->datalen, 0);
311 sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
312 cmdlen, xs->data,
313 xs->datalen, dir,
314 xs->timeout, 0,
315 umass_scsipi_cb, xs);
316 return;
317 }
318
319 /* Return if command finishes early. */
320 done:
321 scsipi_done(xs);
322 return;
323 default:
324 /* Not supported, nothing to do. */
325 ;
326 }
327 }
328
329 Static void
330 umass_scsipi_minphys(struct buf *bp)
331 {
332 #ifdef DIAGNOSTIC
333 if (bp->b_bcount <= 0) {
334 printf("umass_scsipi_minphys count(%d) <= 0\n",
335 bp->b_bcount);
336 bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
337 }
338 #endif
339 if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE)
340 bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
341 minphys(bp);
342 }
343
344 int
345 umass_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd,
346 void *arg, int flag, proc_t *p)
347 {
348 /*struct umass_softc *sc = link->adapter_softc;*/
349 /*struct umass_scsipi_softc *scbus = sc->bus;*/
350
351 switch (cmd) {
352 #if 0
353 case SCBUSIORESET:
354 ccb->ccb_h.status = CAM_REQ_INPROG;
355 umass_reset(sc, umass_cam_cb, (void *) ccb);
356 return 0;
357 #endif
358 default:
359 return ENOTTY;
360 }
361 }
362
363 Static int
364 umass_scsipi_getgeom(struct scsipi_periph *periph, struct disk_parms *dp,
365 u_long sectors)
366 {
367 struct umass_softc *sc =
368 device_private(periph->periph_channel->chan_adapter->adapt_dev);
369
370 /* If it's not a floppy, we don't know what to do. */
371 if (sc->sc_cmd != UMASS_CPROTO_UFI)
372 return 0;
373
374 switch (sectors) {
375 case 1440:
376 /* Most likely a single density 3.5" floppy. */
377 dp->heads = 2;
378 dp->sectors = 9;
379 dp->cyls = 80;
380 return 1;
381 case 2880:
382 /* Most likely a double density 3.5" floppy. */
383 dp->heads = 2;
384 dp->sectors = 18;
385 dp->cyls = 80;
386 return 1;
387 default:
388 return 0;
389 }
390 }
391
392 Static void
393 umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status)
394 {
395 UMASSHIST_FUNC(); UMASSHIST_CALLED();
396 struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
397 struct scsipi_xfer *xs = priv;
398 struct scsipi_periph *periph = xs->xs_periph;
399 int cmdlen, senselen;
400 #ifdef UMASS_DEBUG
401 struct timeval tv;
402 u_int delta;
403 microtime(&tv);
404 delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 + tv.tv_usec - sc->tv.tv_usec;
405 DPRINTFM(UDMASS_CMD, "delta=%ju: xs=%#jx residue=%jd status=%jd",
406 delta, (uintptr_t)xs, residue, status);
407 #endif
408
409
410 xs->resid = residue;
411
412 switch (status) {
413 case STATUS_CMD_OK:
414 xs->error = XS_NOERROR;
415 break;
416
417 case STATUS_CMD_UNKNOWN:
418 /* FALLTHROUGH */
419 case STATUS_CMD_FAILED:
420 /* fetch sense data */
421 sc->sc_sense = 1;
422 memset(&scbus->sc_sense_cmd, 0, sizeof(scbus->sc_sense_cmd));
423 scbus->sc_sense_cmd.opcode = SCSI_REQUEST_SENSE;
424 scbus->sc_sense_cmd.byte2 = periph->periph_lun <<
425 SCSI_CMD_LUN_SHIFT;
426
427 if (sc->sc_cmd == UMASS_CPROTO_UFI ||
428 sc->sc_cmd == UMASS_CPROTO_ATAPI)
429 cmdlen = UFI_COMMAND_LENGTH; /* XXX */
430 else
431 cmdlen = sizeof(scbus->sc_sense_cmd);
432 if (periph->periph_version < 0x05) /* SPC-3 */
433 senselen = 18;
434 else
435 senselen = sizeof(xs->sense);
436 scbus->sc_sense_cmd.length = senselen;
437 sc->sc_methods->wire_xfer(sc, periph->periph_lun,
438 &scbus->sc_sense_cmd, cmdlen,
439 &xs->sense, senselen,
440 DIR_IN, xs->timeout, 0,
441 umass_scsipi_sense_cb, xs);
442 return;
443
444 case STATUS_WIRE_FAILED:
445 xs->error = XS_RESET;
446 break;
447
448 default:
449 panic("%s: Unknown status %d in umass_scsipi_cb",
450 device_xname(sc->sc_dev), status);
451 }
452
453 DPRINTFM(UDMASS_CMD, "return xs->error=%jd, xs->xs_status=0x%jx"
454 " xs->resid=%jd", xs->error, xs->xs_status, xs->resid, 0);
455
456 scsipi_done(xs);
457 }
458
459 /*
460 * Finalise a completed autosense operation
461 */
462 Static void
463 umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue,
464 int status)
465 {
466 UMASSHIST_FUNC(); UMASSHIST_CALLED();
467 struct scsipi_xfer *xs = priv;
468
469 DPRINTFM(UDMASS_CMD, "sc %#jx: xs=%#jx residue=%jd status=%jd",
470 (uintptr_t)sc, (uintptr_t)xs, residue, status);
471
472 sc->sc_sense = 0;
473 switch (status) {
474 case STATUS_CMD_OK:
475 case STATUS_CMD_UNKNOWN:
476 /* getting sense data succeeded */
477 if (residue == 0 || residue == 14)/* XXX */
478 xs->error = XS_SENSE;
479 else
480 xs->error = XS_SHORTSENSE;
481 break;
482 default:
483 DPRINTFM(UDMASS_SCSI, "sc %#jx: Autosense failed, status %jd",
484 (uintptr_t)sc, status, 0, 0);
485 xs->error = XS_DRIVER_STUFFUP;
486 break;
487 }
488
489 DPRINTFM(UDMASS_CMD, "return xs->error=%jd, xs->xs_status=0x%jx"
490 " xs->resid=%jd", xs->error, xs->xs_status, xs->resid, 0);
491
492 scsipi_done(xs);
493 }
494
495 #if NATAPIBUS > 0
496 Static void
497 umass_atapi_probe_device(struct atapibus_softc *atapi, int target)
498 {
499 UMASSHIST_FUNC(); UMASSHIST_CALLED();
500 struct scsipi_channel *chan = atapi->sc_channel;
501 struct scsipi_periph *periph;
502 struct scsipibus_attach_args sa;
503 char vendor[33], product[65], revision[17];
504 struct scsipi_inquiry_data inqbuf;
505
506 DPRINTFM(UDMASS_SCSI, "atapi=%#jx target=%jd", (uintptr_t)atapi,
507 target, 0, 0);
508
509 if (target != UMASS_ATAPI_DRIVE) /* only probe drive 0 */
510 return;
511
512 /* skip if already attached */
513 if (scsipi_lookup_periph(chan, target, 0) != NULL) {
514 return;
515 }
516
517 periph = scsipi_alloc_periph(M_NOWAIT);
518 if (periph == NULL) {
519 aprint_error_dev(atapi->sc_dev,
520 "can't allocate link for drive %d\n", target);
521 return;
522 }
523
524 DIF(UDMASS_UPPER, periph->periph_dbflags |= 1); /* XXX 1 */
525 periph->periph_channel = chan;
526 periph->periph_switch = &atapi_probe_periphsw;
527 periph->periph_target = target;
528 periph->periph_quirks = chan->chan_defquirks;
529
530 DPRINTFM(UDMASS_SCSI, "doing inquiry", 0, 0, 0, 0);
531 /* Now go ask the device all about itself. */
532 memset(&inqbuf, 0, sizeof(inqbuf));
533 if (scsipi_inquire(periph, &inqbuf, XS_CTL_DISCOVERY) != 0) {
534 DPRINTFM(UDMASS_SCSI, "scsipi_inquire failed", 0, 0, 0, 0);
535 free(periph, M_DEVBUF);
536 return;
537 }
538
539 strnvisx(vendor, sizeof(vendor), inqbuf.vendor, 8,
540 VIS_TRIM|VIS_SAFE|VIS_OCTAL);
541 strnvisx(product, sizeof(product), inqbuf.product, 16,
542 VIS_TRIM|VIS_SAFE|VIS_OCTAL);
543 strnvisx(revision, sizeof(revision), inqbuf.revision, 4,
544 VIS_TRIM|VIS_SAFE|VIS_OCTAL);
545
546 sa.sa_periph = periph;
547 sa.sa_inqbuf.type = inqbuf.device;
548 sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
549 T_REMOV : T_FIXED;
550 if (sa.sa_inqbuf.removable)
551 periph->periph_flags |= PERIPH_REMOVABLE;
552 sa.sa_inqbuf.vendor = vendor;
553 sa.sa_inqbuf.product = product;
554 sa.sa_inqbuf.revision = revision;
555 sa.sa_inqptr = NULL;
556
557 atapi_probe_device(atapi, target, periph, &sa);
558 /* atapi_probe_device() frees the periph when there is no device.*/
559 }
560 #endif
561