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