umass_scsipi.c revision 1.48.16.1 1 /* $NetBSD: umass_scsipi.c,v 1.48.16.1 2016/09/06 20:33:09 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.48.16.1 2016/09/06 20:33:09 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
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 %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
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 %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
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 UMASSHIST_FUNC(); UMASSHIST_CALLED();
215 struct scsipi_adapter *adapt = chan->chan_adapter;
216 struct scsipi_periph *periph;
217 struct scsipi_xfer *xs;
218 struct umass_softc *sc = device_private(adapt->adapt_dev);
219 struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
220 struct scsipi_generic *cmd;
221 int cmdlen;
222 int dir;
223 #ifdef UMASS_DEBUG
224 microtime(&sc->tv);
225 #endif
226 switch(req) {
227 case ADAPTER_REQ_RUN_XFER:
228 xs = arg;
229 periph = xs->xs_periph;
230 DIF(UDMASS_UPPER, periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS);
231
232 DPRINTFM(UDMASS_CMD, "sc %p: %d:%d xs=%p", sc,
233 periph->periph_target, periph->periph_lun, xs);
234 DPRINTFM(UDMASS_CMD, "cmd=0x%02x datalen=%d (quirks=0x%x, "
235 "poll=%d)", xs->cmd->opcode, xs->datalen,
236 periph->periph_quirks, !!(xs->xs_control & XS_CTL_POLL));
237 #if defined(UMASS_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 DPRINTFM(UDMASS_SCSI, "sc %p: wrong SCSI ID %d", sc,
255 periph->periph_target, 0, 0);
256 xs->error = XS_DRIVER_STUFFUP;
257 goto done;
258 }
259 #endif
260
261 cmd = xs->cmd;
262 cmdlen = xs->cmdlen;
263
264 dir = DIR_NONE;
265 if (xs->datalen) {
266 switch (xs->xs_control &
267 (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
268 case XS_CTL_DATA_IN:
269 dir = DIR_IN;
270 break;
271 case XS_CTL_DATA_OUT:
272 dir = DIR_OUT;
273 break;
274 }
275 }
276
277 if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) {
278 printf("umass_cmd: large datalen, %d\n", xs->datalen);
279 xs->error = XS_DRIVER_STUFFUP;
280 goto done;
281 }
282
283 if (xs->xs_control & XS_CTL_POLL) {
284 /* Use sync transfer. XXX Broken! */
285 DPRINTFM(UDMASS_SCSI, "sync dir=%d\n", dir, 0, 0, 0);
286 scbus->sc_sync_status = USBD_INVAL;
287 sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
288 cmdlen, xs->data,
289 xs->datalen, dir,
290 xs->timeout, USBD_SYNCHRONOUS,
291 0, xs);
292 DPRINTFM(UDMASS_SCSI, "done err=%d",
293 scbus->sc_sync_status, 0, 0, 0);
294 switch (scbus->sc_sync_status) {
295 case USBD_NORMAL_COMPLETION:
296 xs->error = XS_NOERROR;
297 break;
298 case USBD_TIMEOUT:
299 xs->error = XS_TIMEOUT;
300 break;
301 default:
302 xs->error = XS_DRIVER_STUFFUP;
303 break;
304 }
305 goto done;
306 } else {
307 DPRINTFM(UDMASS_SCSI, "async dir=%d, cmdlen=%d"
308 " datalen=%d", dir, cmdlen, xs->datalen, 0);
309 sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
310 cmdlen, xs->data,
311 xs->datalen, dir,
312 xs->timeout, 0,
313 umass_scsipi_cb, xs);
314 return;
315 }
316
317 /* Return if command finishes early. */
318 done:
319 KERNEL_LOCK(1, curlwp);
320 scsipi_done(xs);
321 KERNEL_UNLOCK_ONE(curlwp);
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 int s;
401 #ifdef UMASS_DEBUG
402 struct timeval tv;
403 u_int delta;
404 microtime(&tv);
405 delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 + tv.tv_usec - sc->tv.tv_usec;
406 DPRINTFM(UDMASS_CMD, "delta=%u: xs=%p residue=%d status=%d", delta, xs,
407 residue, status);
408 #endif
409
410
411 xs->resid = residue;
412
413 switch (status) {
414 case STATUS_CMD_OK:
415 xs->error = XS_NOERROR;
416 break;
417
418 case STATUS_CMD_UNKNOWN:
419 /* FALLTHROUGH */
420 case STATUS_CMD_FAILED:
421 /* fetch sense data */
422 sc->sc_sense = 1;
423 memset(&scbus->sc_sense_cmd, 0, sizeof(scbus->sc_sense_cmd));
424 scbus->sc_sense_cmd.opcode = SCSI_REQUEST_SENSE;
425 scbus->sc_sense_cmd.byte2 = periph->periph_lun <<
426 SCSI_CMD_LUN_SHIFT;
427
428 if (sc->sc_cmd == UMASS_CPROTO_UFI ||
429 sc->sc_cmd == UMASS_CPROTO_ATAPI)
430 cmdlen = UFI_COMMAND_LENGTH; /* XXX */
431 else
432 cmdlen = sizeof(scbus->sc_sense_cmd);
433 if (periph->periph_version < 0x05) /* SPC-3 */
434 senselen = 18;
435 else
436 senselen = sizeof(xs->sense);
437 scbus->sc_sense_cmd.length = senselen;
438 sc->sc_methods->wire_xfer(sc, periph->periph_lun,
439 &scbus->sc_sense_cmd, cmdlen,
440 &xs->sense, senselen,
441 DIR_IN, xs->timeout, 0,
442 umass_scsipi_sense_cb, xs);
443 return;
444
445 case STATUS_WIRE_FAILED:
446 xs->error = XS_RESET;
447 break;
448
449 default:
450 panic("%s: Unknown status %d in umass_scsipi_cb",
451 device_xname(sc->sc_dev), status);
452 }
453
454 DPRINTFM(UDMASS_CMD, "return xs->error=%d, xs->xs_status=0x%x"
455 " xs->resid=%d", xs->error, xs->xs_status, xs->resid, 0);
456
457 s = splbio();
458 KERNEL_LOCK(1, curlwp);
459 scsipi_done(xs);
460 KERNEL_UNLOCK_ONE(curlwp);
461 splx(s);
462 }
463
464 /*
465 * Finalise a completed autosense operation
466 */
467 Static void
468 umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue,
469 int status)
470 {
471 UMASSHIST_FUNC(); UMASSHIST_CALLED();
472 struct scsipi_xfer *xs = priv;
473 int s;
474
475 DPRINTFM(UDMASS_CMD, "sc %p: xs=%p residue=%d status=%d", sc, xs,
476 residue, status);
477
478 sc->sc_sense = 0;
479 switch (status) {
480 case STATUS_CMD_OK:
481 case STATUS_CMD_UNKNOWN:
482 /* getting sense data succeeded */
483 if (residue == 0 || residue == 14)/* XXX */
484 xs->error = XS_SENSE;
485 else
486 xs->error = XS_SHORTSENSE;
487 break;
488 default:
489 DPRINTFM(UDMASS_SCSI, "sc %p: Autosense failed, status %d",
490 sc, status, 0, 0);
491 xs->error = XS_DRIVER_STUFFUP;
492 break;
493 }
494
495 DPRINTFM(UDMASS_CMD, "return xs->error=%d, xs->xs_status=0x%x"
496 " xs->resid=%d", xs->error, xs->xs_status, xs->resid, 0);
497
498 s = splbio();
499 KERNEL_LOCK(1, curlwp);
500 scsipi_done(xs);
501 KERNEL_UNLOCK_ONE(curlwp);
502 splx(s);
503 }
504
505 #if NATAPIBUS > 0
506 Static void
507 umass_atapi_probe_device(struct atapibus_softc *atapi, int target)
508 {
509 UMASSHIST_FUNC(); UMASSHIST_CALLED();
510 struct scsipi_channel *chan = atapi->sc_channel;
511 struct scsipi_periph *periph;
512 struct scsipibus_attach_args sa;
513 char vendor[33], product[65], revision[17];
514 struct scsipi_inquiry_data inqbuf;
515
516 DPRINTFM(UDMASS_SCSI, "atapi=%p target=%d", atapi, target, 0, 0);
517
518 if (target != UMASS_ATAPI_DRIVE) /* only probe drive 0 */
519 return;
520
521 KERNEL_LOCK(1, curlwp);
522
523 /* skip if already attached */
524 if (scsipi_lookup_periph(chan, target, 0) != NULL) {
525 KERNEL_UNLOCK_ONE(curlwp);
526 return;
527 }
528
529 periph = scsipi_alloc_periph(M_NOWAIT);
530 if (periph == NULL) {
531 KERNEL_UNLOCK_ONE(curlwp);
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 KERNEL_UNLOCK_ONE(curlwp);
548 DPRINTFM(UDMASS_SCSI, "scsipi_inquire failed", 0, 0, 0, 0);
549 free(periph, M_DEVBUF);
550 return;
551 }
552
553 scsipi_strvis(vendor, 33, inqbuf.vendor, 8);
554 scsipi_strvis(product, 65, inqbuf.product, 16);
555 scsipi_strvis(revision, 17, inqbuf.revision, 4);
556
557 sa.sa_periph = periph;
558 sa.sa_inqbuf.type = inqbuf.device;
559 sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
560 T_REMOV : T_FIXED;
561 if (sa.sa_inqbuf.removable)
562 periph->periph_flags |= PERIPH_REMOVABLE;
563 sa.sa_inqbuf.vendor = vendor;
564 sa.sa_inqbuf.product = product;
565 sa.sa_inqbuf.revision = revision;
566 sa.sa_inqptr = NULL;
567
568 atapi_probe_device(atapi, target, periph, &sa);
569 /* atapi_probe_device() frees the periph when there is no device.*/
570
571 KERNEL_UNLOCK_ONE(curlwp);
572 }
573 #endif
574