1#include <sys/cdefs.h>
2__FBSDID("$FreeBSD$");
3
4/*-
5 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 *
7 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
8 *		      Nick Hibma <n_hibma@FreeBSD.org>
9 * All rights reserved.
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	$FreeBSD$
33 *	$NetBSD: umass.c,v 1.28 2000/04/02 23:46:53 augustss Exp $
34 */
35
36/* Also already merged from NetBSD:
37 *	$NetBSD: umass.c,v 1.67 2001/11/25 19:05:22 augustss Exp $
38 *	$NetBSD: umass.c,v 1.90 2002/11/04 19:17:33 pooka Exp $
39 *	$NetBSD: umass.c,v 1.108 2003/11/07 17:03:25 wiz Exp $
40 *	$NetBSD: umass.c,v 1.109 2003/12/04 13:57:31 keihan Exp $
41 */
42
43/*
44 * Universal Serial Bus Mass Storage Class specs:
45 * http://www.usb.org/developers/devclass_docs/usb_msc_overview_1.2.pdf
46 * http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf
47 * http://www.usb.org/developers/devclass_docs/usb_msc_cbi_1.1.pdf
48 * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf
49 */
50
51/*
52 * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>.
53 * Parts of the code written by Jason R. Thorpe <thorpej@shagadelic.org>.
54 */
55
56/*
57 * The driver handles 3 Wire Protocols
58 * - Command/Bulk/Interrupt (CBI)
59 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
60 * - Mass Storage Bulk-Only (BBB)
61 *   (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
62 *
63 * Over these wire protocols it handles the following command protocols
64 * - SCSI
65 * - UFI (floppy command set)
66 * - 8070i (ATAPI)
67 *
68 * UFI and 8070i (ATAPI) are transformed versions of the SCSI command set. The
69 * sc->sc_transform method is used to convert the commands into the appropriate
70 * format (if at all necessary). For example, UFI requires all commands to be
71 * 12 bytes in length amongst other things.
72 *
73 * The source code below is marked and can be split into a number of pieces
74 * (in this order):
75 *
76 * - probe/attach/detach
77 * - generic transfer routines
78 * - BBB
79 * - CBI
80 * - CBI_I (in addition to functions from CBI)
81 * - CAM (Common Access Method)
82 * - SCSI
83 * - UFI
84 * - 8070i (ATAPI)
85 *
86 * The protocols are implemented using a state machine, for the transfers as
87 * well as for the resets. The state machine is contained in umass_t_*_callback.
88 * The state machine is started through either umass_command_start() or
89 * umass_reset().
90 *
91 * The reason for doing this is a) CAM performs a lot better this way and b) it
92 * avoids using tsleep from interrupt context (for example after a failed
93 * transfer).
94 */
95
96/*
97 * The SCSI related part of this driver has been derived from the
98 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@FreeBSD.org).
99 *
100 * The CAM layer uses so called actions which are messages sent to the host
101 * adapter for completion. The actions come in through umass_cam_action. The
102 * appropriate block of routines is called depending on the transport protocol
103 * in use. When the transfer has finished, these routines call
104 * umass_cam_cb again to complete the CAM command.
105 */
106
107#include <sys/stdint.h>
108#include <sys/stddef.h>
109#include <sys/param.h>
110#include <sys/queue.h>
111#include <sys/types.h>
112#include <sys/systm.h>
113#include <sys/kernel.h>
114#include <sys/bus.h>
115#include <sys/module.h>
116#include <sys/lock.h>
117#include <sys/mutex.h>
118#include <sys/condvar.h>
119#include <sys/sysctl.h>
120#include <sys/sx.h>
121#include <sys/unistd.h>
122#include <sys/callout.h>
123#include <sys/malloc.h>
124#include <sys/priv.h>
125
126#include <dev/usb/usb.h>
127#include <dev/usb/usbdi.h>
128#include <dev/usb/usbdi_util.h>
129#include "usbdevs.h"
130
131#include <dev/usb/quirk/usb_quirk.h>
132
133#include <cam/cam.h>
134#include <cam/cam_ccb.h>
135#include <cam/cam_sim.h>
136#include <cam/cam_xpt_sim.h>
137#include <cam/scsi/scsi_all.h>
138#include <cam/scsi/scsi_da.h>
139
140#include <cam/cam_periph.h>
141
142#ifdef USB_DEBUG
143#define	DIF(m, x)				\
144  do {						\
145    if (umass_debug & (m)) { x ; }		\
146  } while (0)
147
148#define	DPRINTF(sc, m, fmt, ...)			\
149  do {							\
150    if (umass_debug & (m)) {				\
151        printf("%s:%s: " fmt,				\
152	       (sc) ? (const char *)(sc)->sc_name :	\
153	       (const char *)"umassX",			\
154		__FUNCTION__ ,## __VA_ARGS__);		\
155    }							\
156  } while (0)
157
158#define	UDMASS_GEN	0x00010000	/* general */
159#define	UDMASS_SCSI	0x00020000	/* scsi */
160#define	UDMASS_UFI	0x00040000	/* ufi command set */
161#define	UDMASS_ATAPI	0x00080000	/* 8070i command set */
162#define	UDMASS_CMD	(UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
163#define	UDMASS_USB	0x00100000	/* USB general */
164#define	UDMASS_BBB	0x00200000	/* Bulk-Only transfers */
165#define	UDMASS_CBI	0x00400000	/* CBI transfers */
166#define	UDMASS_WIRE	(UDMASS_BBB|UDMASS_CBI)
167#define	UDMASS_ALL	0xffff0000	/* all of the above */
168static int umass_debug;
169static int umass_throttle;
170
171static SYSCTL_NODE(_hw_usb, OID_AUTO, umass, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
172    "USB umass");
173SYSCTL_INT(_hw_usb_umass, OID_AUTO, debug, CTLFLAG_RWTUN,
174    &umass_debug, 0, "umass debug level");
175SYSCTL_INT(_hw_usb_umass, OID_AUTO, throttle, CTLFLAG_RWTUN,
176    &umass_throttle, 0, "Forced delay between commands in milliseconds");
177#else
178#define	DIF(...) do { } while (0)
179#define	DPRINTF(...) do { } while (0)
180#endif
181
182#define	UMASS_BULK_SIZE (1 << 17)
183#define	UMASS_CBI_DIAGNOSTIC_CMDLEN 12	/* bytes */
184#define	UMASS_MAX_CMDLEN MAX(12, CAM_MAX_CDBLEN)	/* bytes */
185
186/* USB transfer definitions */
187
188#define	UMASS_T_BBB_RESET1      0	/* Bulk-Only */
189#define	UMASS_T_BBB_RESET2      1
190#define	UMASS_T_BBB_RESET3      2
191#define	UMASS_T_BBB_COMMAND     3
192#define	UMASS_T_BBB_DATA_READ   4
193#define	UMASS_T_BBB_DATA_RD_CS  5
194#define	UMASS_T_BBB_DATA_WRITE  6
195#define	UMASS_T_BBB_DATA_WR_CS  7
196#define	UMASS_T_BBB_STATUS      8
197#define	UMASS_T_BBB_MAX         9
198
199#define	UMASS_T_CBI_RESET1      0	/* CBI */
200#define	UMASS_T_CBI_RESET2      1
201#define	UMASS_T_CBI_RESET3      2
202#define	UMASS_T_CBI_COMMAND     3
203#define	UMASS_T_CBI_DATA_READ   4
204#define	UMASS_T_CBI_DATA_RD_CS  5
205#define	UMASS_T_CBI_DATA_WRITE  6
206#define	UMASS_T_CBI_DATA_WR_CS  7
207#define	UMASS_T_CBI_STATUS      8
208#define	UMASS_T_CBI_RESET4      9
209#define	UMASS_T_CBI_MAX        10
210
211#define	UMASS_T_MAX MAX(UMASS_T_CBI_MAX, UMASS_T_BBB_MAX)
212
213/* Generic definitions */
214
215/* Direction for transfer */
216#define	DIR_NONE	0
217#define	DIR_IN		1
218#define	DIR_OUT		2
219
220/* device name */
221#define	DEVNAME		"umass"
222#define	DEVNAME_SIM	"umass-sim"
223
224/* Approximate maximum transfer speeds (assumes 33% overhead). */
225#define	UMASS_FULL_TRANSFER_SPEED	1000
226#define	UMASS_HIGH_TRANSFER_SPEED	40000
227#define	UMASS_SUPER_TRANSFER_SPEED	400000
228#define	UMASS_FLOPPY_TRANSFER_SPEED	20
229
230#define	UMASS_TIMEOUT			5000	/* ms */
231
232/* CAM specific definitions */
233
234#define	UMASS_SCSIID_MAX	1	/* maximum number of drives expected */
235#define	UMASS_SCSIID_HOST	UMASS_SCSIID_MAX
236
237/* Bulk-Only features */
238
239#define	UR_BBB_RESET		0xff	/* Bulk-Only reset */
240#define	UR_BBB_GET_MAX_LUN	0xfe	/* Get maximum lun */
241
242/* Command Block Wrapper */
243typedef struct {
244	uDWord	dCBWSignature;
245#define	CBWSIGNATURE	0x43425355
246	uDWord	dCBWTag;
247	uDWord	dCBWDataTransferLength;
248	uByte	bCBWFlags;
249#define	CBWFLAGS_OUT	0x00
250#define	CBWFLAGS_IN	0x80
251	uByte	bCBWLUN;
252	uByte	bCDBLength;
253#define	CBWCDBLENGTH	16
254	uByte	CBWCDB[CBWCDBLENGTH];
255} __packed umass_bbb_cbw_t;
256
257#define	UMASS_BBB_CBW_SIZE	31
258
259/* Command Status Wrapper */
260typedef struct {
261	uDWord	dCSWSignature;
262#define	CSWSIGNATURE	0x53425355
263#define	CSWSIGNATURE_IMAGINATION_DBX1	0x43425355
264#define	CSWSIGNATURE_OLYMPUS_C1	0x55425355
265	uDWord	dCSWTag;
266	uDWord	dCSWDataResidue;
267	uByte	bCSWStatus;
268#define	CSWSTATUS_GOOD	0x0
269#define	CSWSTATUS_FAILED	0x1
270#define	CSWSTATUS_PHASE	0x2
271} __packed umass_bbb_csw_t;
272
273#define	UMASS_BBB_CSW_SIZE	13
274
275/* CBI features */
276
277#define	UR_CBI_ADSC	0x00
278
279typedef union {
280	struct {
281		uint8_t	type;
282#define	IDB_TYPE_CCI		0x00
283		uint8_t	value;
284#define	IDB_VALUE_PASS		0x00
285#define	IDB_VALUE_FAIL		0x01
286#define	IDB_VALUE_PHASE		0x02
287#define	IDB_VALUE_PERSISTENT	0x03
288#define	IDB_VALUE_STATUS_MASK	0x03
289	} __packed common;
290
291	struct {
292		uint8_t	asc;
293		uint8_t	ascq;
294	} __packed ufi;
295} __packed umass_cbi_sbl_t;
296
297struct umass_softc;			/* see below */
298
299typedef void (umass_callback_t)(struct umass_softc *sc, union ccb *ccb,
300    	uint32_t residue, uint8_t status);
301
302#define	STATUS_CMD_OK		0	/* everything ok */
303#define	STATUS_CMD_UNKNOWN	1	/* will have to fetch sense */
304#define	STATUS_CMD_FAILED	2	/* transfer was ok, command failed */
305#define	STATUS_WIRE_FAILED	3	/* couldn't even get command across */
306
307typedef uint8_t (umass_transform_t)(struct umass_softc *sc, uint8_t *cmd_ptr,
308    	uint8_t cmd_len);
309
310/* Wire and command protocol */
311#define	UMASS_PROTO_BBB		0x0001	/* USB wire protocol */
312#define	UMASS_PROTO_CBI		0x0002
313#define	UMASS_PROTO_CBI_I	0x0004
314#define	UMASS_PROTO_WIRE	0x00ff	/* USB wire protocol mask */
315#define	UMASS_PROTO_SCSI	0x0100	/* command protocol */
316#define	UMASS_PROTO_ATAPI	0x0200
317#define	UMASS_PROTO_UFI		0x0400
318#define	UMASS_PROTO_RBC		0x0800
319#define	UMASS_PROTO_COMMAND	0xff00	/* command protocol mask */
320
321/* Device specific quirks */
322#define	NO_QUIRKS		0x0000
323	/*
324	 * The drive does not support Test Unit Ready. Convert to Start Unit
325	 */
326#define	NO_TEST_UNIT_READY	0x0001
327	/*
328	 * The drive does not reset the Unit Attention state after REQUEST
329	 * SENSE has been sent. The INQUIRY command does not reset the UA
330	 * either, and so CAM runs in circles trying to retrieve the initial
331	 * INQUIRY data.
332	 */
333#define	RS_NO_CLEAR_UA		0x0002
334	/* The drive does not support START STOP.  */
335#define	NO_START_STOP		0x0004
336	/* Don't ask for full inquiry data (255b).  */
337#define	FORCE_SHORT_INQUIRY	0x0008
338	/* Needs to be initialised the Shuttle way */
339#define	SHUTTLE_INIT		0x0010
340	/* Drive needs to be switched to alternate iface 1 */
341#define	ALT_IFACE_1		0x0020
342	/* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
343#define	FLOPPY_SPEED		0x0040
344	/* The device can't count and gets the residue of transfers wrong */
345#define	IGNORE_RESIDUE		0x0080
346	/* No GetMaxLun call */
347#define	NO_GETMAXLUN		0x0100
348	/* The device uses a weird CSWSIGNATURE. */
349#define	WRONG_CSWSIG		0x0200
350	/* Device cannot handle INQUIRY so fake a generic response */
351#define	NO_INQUIRY		0x0400
352	/* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
353#define	NO_INQUIRY_EVPD		0x0800
354	/* Pad all RBC requests to 12 bytes. */
355#define	RBC_PAD_TO_12		0x1000
356	/*
357	 * Device reports number of sectors from READ_CAPACITY, not max
358	 * sector number.
359	 */
360#define	READ_CAPACITY_OFFBY1	0x2000
361	/*
362	 * Device cannot handle a SCSI synchronize cache command.  Normally
363	 * this quirk would be handled in the cam layer, but for IDE bridges
364	 * we need to associate the quirk with the bridge and not the
365	 * underlying disk device.  This is handled by faking a success
366	 * result.
367	 */
368#define	NO_SYNCHRONIZE_CACHE	0x4000
369	/* Device does not support 'PREVENT/ALLOW MEDIUM REMOVAL'. */
370#define	NO_PREVENT_ALLOW	0x8000
371
372struct umass_softc {
373	struct scsi_sense cam_scsi_sense;
374	struct scsi_test_unit_ready cam_scsi_test_unit_ready;
375	struct mtx sc_mtx;
376	struct {
377		uint8_t *data_ptr;
378		union ccb *ccb;
379		umass_callback_t *callback;
380
381		uint32_t data_len;	/* bytes */
382		uint32_t data_rem;	/* bytes */
383		uint32_t data_timeout;	/* ms */
384		uint32_t actlen;	/* bytes */
385
386		uint8_t	cmd_data[UMASS_MAX_CMDLEN];
387		uint8_t	cmd_len;	/* bytes */
388		uint8_t	dir;
389		uint8_t	lun;
390	}	sc_transfer;
391
392	/* Bulk specific variables for transfers in progress */
393	umass_bbb_cbw_t cbw;		/* command block wrapper */
394	umass_bbb_csw_t csw;		/* command status wrapper */
395
396	/* CBI specific variables for transfers in progress */
397	umass_cbi_sbl_t sbl;		/* status block */
398
399	device_t sc_dev;
400	struct usb_device *sc_udev;
401	struct cam_sim *sc_sim;		/* SCSI Interface Module */
402	struct usb_xfer *sc_xfer[UMASS_T_MAX];
403
404	/*
405	 * The command transform function is used to convert the SCSI
406	 * commands into their derivatives, like UFI, ATAPI, and friends.
407	 */
408	umass_transform_t *sc_transform;
409
410	uint32_t sc_unit;
411	uint32_t sc_quirks;		/* they got it almost right */
412	uint32_t sc_proto;		/* wire and cmd protocol */
413
414	uint8_t	sc_name[16];
415	uint8_t	sc_iface_no;		/* interface number */
416	uint8_t	sc_maxlun;		/* maximum LUN number, inclusive */
417	uint8_t	sc_last_xfer_index;
418	uint8_t	sc_status_try;
419};
420
421struct umass_probe_proto {
422	uint32_t quirks;
423	uint32_t proto;
424
425	int	error;
426};
427
428/* prototypes */
429
430static device_probe_t umass_probe;
431static device_attach_t umass_attach;
432static device_detach_t umass_detach;
433
434static usb_callback_t umass_tr_error;
435static usb_callback_t umass_t_bbb_reset1_callback;
436static usb_callback_t umass_t_bbb_reset2_callback;
437static usb_callback_t umass_t_bbb_reset3_callback;
438static usb_callback_t umass_t_bbb_command_callback;
439static usb_callback_t umass_t_bbb_data_read_callback;
440static usb_callback_t umass_t_bbb_data_rd_cs_callback;
441static usb_callback_t umass_t_bbb_data_write_callback;
442static usb_callback_t umass_t_bbb_data_wr_cs_callback;
443static usb_callback_t umass_t_bbb_status_callback;
444static usb_callback_t umass_t_cbi_reset1_callback;
445static usb_callback_t umass_t_cbi_reset2_callback;
446static usb_callback_t umass_t_cbi_reset3_callback;
447static usb_callback_t umass_t_cbi_reset4_callback;
448static usb_callback_t umass_t_cbi_command_callback;
449static usb_callback_t umass_t_cbi_data_read_callback;
450static usb_callback_t umass_t_cbi_data_rd_cs_callback;
451static usb_callback_t umass_t_cbi_data_write_callback;
452static usb_callback_t umass_t_cbi_data_wr_cs_callback;
453static usb_callback_t umass_t_cbi_status_callback;
454
455static void	umass_cancel_ccb(struct umass_softc *);
456static void	umass_init_shuttle(struct umass_softc *);
457static void	umass_reset(struct umass_softc *);
458static void	umass_t_bbb_data_clear_stall_callback(struct usb_xfer *,
459		    uint8_t, uint8_t, usb_error_t);
460static void	umass_command_start(struct umass_softc *, uint8_t, void *,
461		    uint32_t, uint32_t, umass_callback_t *, union ccb *);
462static uint8_t	umass_bbb_get_max_lun(struct umass_softc *);
463static void	umass_cbi_start_status(struct umass_softc *);
464static void	umass_t_cbi_data_clear_stall_callback(struct usb_xfer *,
465		    uint8_t, uint8_t, usb_error_t);
466static int	umass_cam_attach_sim(struct umass_softc *);
467static void	umass_cam_attach(struct umass_softc *);
468static void	umass_cam_detach_sim(struct umass_softc *);
469static void	umass_cam_action(struct cam_sim *, union ccb *);
470static void	umass_cam_poll(struct cam_sim *);
471static void	umass_cam_cb(struct umass_softc *, union ccb *, uint32_t,
472		    uint8_t);
473static void	umass_cam_sense_cb(struct umass_softc *, union ccb *, uint32_t,
474		    uint8_t);
475static void	umass_cam_quirk_cb(struct umass_softc *, union ccb *, uint32_t,
476		    uint8_t);
477static uint8_t	umass_scsi_transform(struct umass_softc *, uint8_t *, uint8_t);
478static uint8_t	umass_rbc_transform(struct umass_softc *, uint8_t *, uint8_t);
479static uint8_t	umass_ufi_transform(struct umass_softc *, uint8_t *, uint8_t);
480static uint8_t	umass_atapi_transform(struct umass_softc *, uint8_t *,
481		    uint8_t);
482static uint8_t	umass_no_transform(struct umass_softc *, uint8_t *, uint8_t);
483static uint8_t	umass_std_transform(struct umass_softc *, union ccb *, uint8_t
484		    *, uint8_t);
485
486#ifdef USB_DEBUG
487static void	umass_bbb_dump_cbw(struct umass_softc *, umass_bbb_cbw_t *);
488static void	umass_bbb_dump_csw(struct umass_softc *, umass_bbb_csw_t *);
489static void	umass_cbi_dump_cmd(struct umass_softc *, void *, uint8_t);
490static void	umass_dump_buffer(struct umass_softc *, uint8_t *, uint32_t,
491		    uint32_t);
492#endif
493
494static struct usb_config umass_bbb_config[UMASS_T_BBB_MAX] = {
495	[UMASS_T_BBB_RESET1] = {
496		.type = UE_CONTROL,
497		.endpoint = 0x00,	/* Control pipe */
498		.direction = UE_DIR_ANY,
499		.bufsize = sizeof(struct usb_device_request),
500		.callback = &umass_t_bbb_reset1_callback,
501		.timeout = 5000,	/* 5 seconds */
502		.interval = 500,	/* 500 milliseconds */
503	},
504
505	[UMASS_T_BBB_RESET2] = {
506		.type = UE_CONTROL,
507		.endpoint = 0x00,	/* Control pipe */
508		.direction = UE_DIR_ANY,
509		.bufsize = sizeof(struct usb_device_request),
510		.callback = &umass_t_bbb_reset2_callback,
511		.timeout = 5000,	/* 5 seconds */
512		.interval = 50,	/* 50 milliseconds */
513	},
514
515	[UMASS_T_BBB_RESET3] = {
516		.type = UE_CONTROL,
517		.endpoint = 0x00,	/* Control pipe */
518		.direction = UE_DIR_ANY,
519		.bufsize = sizeof(struct usb_device_request),
520		.callback = &umass_t_bbb_reset3_callback,
521		.timeout = 5000,	/* 5 seconds */
522		.interval = 50,	/* 50 milliseconds */
523	},
524
525	[UMASS_T_BBB_COMMAND] = {
526		.type = UE_BULK,
527		.endpoint = UE_ADDR_ANY,
528		.direction = UE_DIR_OUT,
529		.bufsize = sizeof(umass_bbb_cbw_t),
530		.callback = &umass_t_bbb_command_callback,
531		.timeout = 5000,	/* 5 seconds */
532	},
533
534	[UMASS_T_BBB_DATA_READ] = {
535		.type = UE_BULK,
536		.endpoint = UE_ADDR_ANY,
537		.direction = UE_DIR_IN,
538		.bufsize = UMASS_BULK_SIZE,
539		.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer=1,},
540		.callback = &umass_t_bbb_data_read_callback,
541		.timeout = 0,	/* overwritten later */
542	},
543
544	[UMASS_T_BBB_DATA_RD_CS] = {
545		.type = UE_CONTROL,
546		.endpoint = 0x00,	/* Control pipe */
547		.direction = UE_DIR_ANY,
548		.bufsize = sizeof(struct usb_device_request),
549		.callback = &umass_t_bbb_data_rd_cs_callback,
550		.timeout = 5000,	/* 5 seconds */
551	},
552
553	[UMASS_T_BBB_DATA_WRITE] = {
554		.type = UE_BULK,
555		.endpoint = UE_ADDR_ANY,
556		.direction = UE_DIR_OUT,
557		.bufsize = UMASS_BULK_SIZE,
558		.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer=1,},
559		.callback = &umass_t_bbb_data_write_callback,
560		.timeout = 0,	/* overwritten later */
561	},
562
563	[UMASS_T_BBB_DATA_WR_CS] = {
564		.type = UE_CONTROL,
565		.endpoint = 0x00,	/* Control pipe */
566		.direction = UE_DIR_ANY,
567		.bufsize = sizeof(struct usb_device_request),
568		.callback = &umass_t_bbb_data_wr_cs_callback,
569		.timeout = 5000,	/* 5 seconds */
570	},
571
572	[UMASS_T_BBB_STATUS] = {
573		.type = UE_BULK,
574		.endpoint = UE_ADDR_ANY,
575		.direction = UE_DIR_IN,
576		.bufsize = sizeof(umass_bbb_csw_t),
577		.flags = {.short_xfer_ok = 1,},
578		.callback = &umass_t_bbb_status_callback,
579		.timeout = 5000,	/* ms */
580	},
581};
582
583static struct usb_config umass_cbi_config[UMASS_T_CBI_MAX] = {
584	[UMASS_T_CBI_RESET1] = {
585		.type = UE_CONTROL,
586		.endpoint = 0x00,	/* Control pipe */
587		.direction = UE_DIR_ANY,
588		.bufsize = (sizeof(struct usb_device_request) +
589		    UMASS_CBI_DIAGNOSTIC_CMDLEN),
590		.callback = &umass_t_cbi_reset1_callback,
591		.timeout = 5000,	/* 5 seconds */
592		.interval = 500,	/* 500 milliseconds */
593	},
594
595	[UMASS_T_CBI_RESET2] = {
596		.type = UE_CONTROL,
597		.endpoint = 0x00,	/* Control pipe */
598		.direction = UE_DIR_ANY,
599		.bufsize = sizeof(struct usb_device_request),
600		.callback = &umass_t_cbi_reset2_callback,
601		.timeout = 5000,	/* 5 seconds */
602		.interval = 50,	/* 50 milliseconds */
603	},
604
605	[UMASS_T_CBI_RESET3] = {
606		.type = UE_CONTROL,
607		.endpoint = 0x00,	/* Control pipe */
608		.direction = UE_DIR_ANY,
609		.bufsize = sizeof(struct usb_device_request),
610		.callback = &umass_t_cbi_reset3_callback,
611		.timeout = 5000,	/* 5 seconds */
612		.interval = 50,	/* 50 milliseconds */
613	},
614
615	[UMASS_T_CBI_COMMAND] = {
616		.type = UE_CONTROL,
617		.endpoint = 0x00,	/* Control pipe */
618		.direction = UE_DIR_ANY,
619		.bufsize = (sizeof(struct usb_device_request) +
620		    UMASS_MAX_CMDLEN),
621		.callback = &umass_t_cbi_command_callback,
622		.timeout = 5000,	/* 5 seconds */
623	},
624
625	[UMASS_T_CBI_DATA_READ] = {
626		.type = UE_BULK,
627		.endpoint = UE_ADDR_ANY,
628		.direction = UE_DIR_IN,
629		.bufsize = UMASS_BULK_SIZE,
630		.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer=1,},
631		.callback = &umass_t_cbi_data_read_callback,
632		.timeout = 0,	/* overwritten later */
633	},
634
635	[UMASS_T_CBI_DATA_RD_CS] = {
636		.type = UE_CONTROL,
637		.endpoint = 0x00,	/* Control pipe */
638		.direction = UE_DIR_ANY,
639		.bufsize = sizeof(struct usb_device_request),
640		.callback = &umass_t_cbi_data_rd_cs_callback,
641		.timeout = 5000,	/* 5 seconds */
642	},
643
644	[UMASS_T_CBI_DATA_WRITE] = {
645		.type = UE_BULK,
646		.endpoint = UE_ADDR_ANY,
647		.direction = UE_DIR_OUT,
648		.bufsize = UMASS_BULK_SIZE,
649		.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer=1,},
650		.callback = &umass_t_cbi_data_write_callback,
651		.timeout = 0,	/* overwritten later */
652	},
653
654	[UMASS_T_CBI_DATA_WR_CS] = {
655		.type = UE_CONTROL,
656		.endpoint = 0x00,	/* Control pipe */
657		.direction = UE_DIR_ANY,
658		.bufsize = sizeof(struct usb_device_request),
659		.callback = &umass_t_cbi_data_wr_cs_callback,
660		.timeout = 5000,	/* 5 seconds */
661	},
662
663	[UMASS_T_CBI_STATUS] = {
664		.type = UE_INTERRUPT,
665		.endpoint = UE_ADDR_ANY,
666		.direction = UE_DIR_IN,
667		.flags = {.short_xfer_ok = 1,.no_pipe_ok = 1,},
668		.bufsize = sizeof(umass_cbi_sbl_t),
669		.callback = &umass_t_cbi_status_callback,
670		.timeout = 5000,	/* ms */
671	},
672
673	[UMASS_T_CBI_RESET4] = {
674		.type = UE_CONTROL,
675		.endpoint = 0x00,	/* Control pipe */
676		.direction = UE_DIR_ANY,
677		.bufsize = sizeof(struct usb_device_request),
678		.callback = &umass_t_cbi_reset4_callback,
679		.timeout = 5000,	/* ms */
680	},
681};
682
683/* If device cannot return valid inquiry data, fake it */
684static const uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
685	0, /* removable */ 0x80, SCSI_REV_2, SCSI_REV_2,
686	 /* additional_length */ 31, 0, 0, 0
687};
688
689#define	UFI_COMMAND_LENGTH	12	/* UFI commands are always 12 bytes */
690#define	ATAPI_COMMAND_LENGTH	12	/* ATAPI commands are always 12 bytes */
691
692static devclass_t umass_devclass;
693
694static device_method_t umass_methods[] = {
695	/* Device interface */
696	DEVMETHOD(device_probe, umass_probe),
697	DEVMETHOD(device_attach, umass_attach),
698	DEVMETHOD(device_detach, umass_detach),
699
700	DEVMETHOD_END
701};
702
703static driver_t umass_driver = {
704	.name = "umass",
705	.methods = umass_methods,
706	.size = sizeof(struct umass_softc),
707};
708
709static const STRUCT_USB_HOST_ID __used umass_devs[] = {
710	/* generic mass storage class */
711	{USB_IFACE_CLASS(UICLASS_MASS),},
712};
713
714DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, NULL, 0);
715MODULE_DEPEND(umass, usb, 1, 1, 1);
716MODULE_DEPEND(umass, cam, 1, 1, 1);
717MODULE_VERSION(umass, 1);
718USB_PNP_HOST_INFO(umass_devs);
719
720/*
721 * USB device probe/attach/detach
722 */
723
724static uint16_t
725umass_get_proto(struct usb_interface *iface)
726{
727	struct usb_interface_descriptor *id;
728	uint16_t retval;
729
730	retval = 0;
731
732	/* Check for a standards compliant device */
733	id = usbd_get_interface_descriptor(iface);
734	if ((id == NULL) ||
735	    (id->bInterfaceClass != UICLASS_MASS)) {
736		goto done;
737	}
738	switch (id->bInterfaceSubClass) {
739	case UISUBCLASS_SCSI:
740		retval |= UMASS_PROTO_SCSI;
741		break;
742	case UISUBCLASS_UFI:
743		retval |= UMASS_PROTO_UFI;
744		break;
745	case UISUBCLASS_RBC:
746		retval |= UMASS_PROTO_RBC;
747		break;
748	case UISUBCLASS_SFF8020I:
749	case UISUBCLASS_SFF8070I:
750		retval |= UMASS_PROTO_ATAPI;
751		break;
752	default:
753		goto done;
754	}
755
756	switch (id->bInterfaceProtocol) {
757	case UIPROTO_MASS_CBI:
758		retval |= UMASS_PROTO_CBI;
759		break;
760	case UIPROTO_MASS_CBI_I:
761		retval |= UMASS_PROTO_CBI_I;
762		break;
763	case UIPROTO_MASS_BBB_OLD:
764	case UIPROTO_MASS_BBB:
765		retval |= UMASS_PROTO_BBB;
766		break;
767	default:
768		goto done;
769	}
770done:
771	return (retval);
772}
773
774/*
775 * Match the device we are seeing with the devices supported.
776 */
777static struct umass_probe_proto
778umass_probe_proto(device_t dev, struct usb_attach_arg *uaa)
779{
780	struct umass_probe_proto ret;
781	uint32_t quirks = NO_QUIRKS;
782	uint32_t proto = umass_get_proto(uaa->iface);
783
784	memset(&ret, 0, sizeof(ret));
785	ret.error = BUS_PROBE_GENERIC;
786
787	/* Check if we should deny probing. */
788	if (usb_test_quirk(uaa, UQ_MSC_IGNORE)) {
789		ret.error = ENXIO;
790		goto done;
791	}
792
793	/* Search for protocol enforcement */
794
795	if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_BBB)) {
796		proto &= ~UMASS_PROTO_WIRE;
797		proto |= UMASS_PROTO_BBB;
798	} else if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_CBI)) {
799		proto &= ~UMASS_PROTO_WIRE;
800		proto |= UMASS_PROTO_CBI;
801	} else if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_CBI_I)) {
802		proto &= ~UMASS_PROTO_WIRE;
803		proto |= UMASS_PROTO_CBI_I;
804	}
805
806	if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_SCSI)) {
807		proto &= ~UMASS_PROTO_COMMAND;
808		proto |= UMASS_PROTO_SCSI;
809	} else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_ATAPI)) {
810		proto &= ~UMASS_PROTO_COMMAND;
811		proto |= UMASS_PROTO_ATAPI;
812	} else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_UFI)) {
813		proto &= ~UMASS_PROTO_COMMAND;
814		proto |= UMASS_PROTO_UFI;
815	} else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_RBC)) {
816		proto &= ~UMASS_PROTO_COMMAND;
817		proto |= UMASS_PROTO_RBC;
818	}
819
820	/* Check if the protocol is invalid */
821
822	if ((proto & UMASS_PROTO_COMMAND) == 0) {
823		ret.error = ENXIO;
824		goto done;
825	}
826
827	if ((proto & UMASS_PROTO_WIRE) == 0) {
828		ret.error = ENXIO;
829		goto done;
830	}
831
832	/* Search for quirks */
833
834	if (usb_test_quirk(uaa, UQ_MSC_NO_TEST_UNIT_READY))
835		quirks |= NO_TEST_UNIT_READY;
836	if (usb_test_quirk(uaa, UQ_MSC_NO_RS_CLEAR_UA))
837		quirks |= RS_NO_CLEAR_UA;
838	if (usb_test_quirk(uaa, UQ_MSC_NO_START_STOP))
839		quirks |= NO_START_STOP;
840	if (usb_test_quirk(uaa, UQ_MSC_NO_GETMAXLUN))
841		quirks |= NO_GETMAXLUN;
842	if (usb_test_quirk(uaa, UQ_MSC_NO_INQUIRY))
843		quirks |= NO_INQUIRY;
844	if (usb_test_quirk(uaa, UQ_MSC_NO_INQUIRY_EVPD))
845		quirks |= NO_INQUIRY_EVPD;
846	if (usb_test_quirk(uaa, UQ_MSC_NO_PREVENT_ALLOW))
847		quirks |= NO_PREVENT_ALLOW;
848	if (usb_test_quirk(uaa, UQ_MSC_NO_SYNC_CACHE))
849		quirks |= NO_SYNCHRONIZE_CACHE;
850	if (usb_test_quirk(uaa, UQ_MSC_SHUTTLE_INIT))
851		quirks |= SHUTTLE_INIT;
852	if (usb_test_quirk(uaa, UQ_MSC_ALT_IFACE_1))
853		quirks |= ALT_IFACE_1;
854	if (usb_test_quirk(uaa, UQ_MSC_FLOPPY_SPEED))
855		quirks |= FLOPPY_SPEED;
856	if (usb_test_quirk(uaa, UQ_MSC_IGNORE_RESIDUE))
857		quirks |= IGNORE_RESIDUE;
858	if (usb_test_quirk(uaa, UQ_MSC_WRONG_CSWSIG))
859		quirks |= WRONG_CSWSIG;
860	if (usb_test_quirk(uaa, UQ_MSC_RBC_PAD_TO_12))
861		quirks |= RBC_PAD_TO_12;
862	if (usb_test_quirk(uaa, UQ_MSC_READ_CAP_OFFBY1))
863		quirks |= READ_CAPACITY_OFFBY1;
864	if (usb_test_quirk(uaa, UQ_MSC_FORCE_SHORT_INQ))
865		quirks |= FORCE_SHORT_INQUIRY;
866
867done:
868	ret.quirks = quirks;
869	ret.proto = proto;
870	return (ret);
871}
872
873static int
874umass_probe(device_t dev)
875{
876	struct usb_attach_arg *uaa = device_get_ivars(dev);
877	struct umass_probe_proto temp;
878
879	if (uaa->usb_mode != USB_MODE_HOST) {
880		return (ENXIO);
881	}
882	temp = umass_probe_proto(dev, uaa);
883
884	return (temp.error);
885}
886
887static int
888umass_attach(device_t dev)
889{
890	struct umass_softc *sc = device_get_softc(dev);
891	struct usb_attach_arg *uaa = device_get_ivars(dev);
892	struct umass_probe_proto temp = umass_probe_proto(dev, uaa);
893	struct usb_interface_descriptor *id;
894	int err;
895
896	/*
897	 * NOTE: the softc struct is cleared in device_set_driver.
898	 * We can safely call umass_detach without specifically
899	 * initializing the struct.
900	 */
901
902	sc->sc_dev = dev;
903	sc->sc_udev = uaa->device;
904	sc->sc_proto = temp.proto;
905	sc->sc_quirks = temp.quirks;
906	sc->sc_unit = device_get_unit(dev);
907
908	snprintf(sc->sc_name, sizeof(sc->sc_name),
909	    "%s", device_get_nameunit(dev));
910
911	device_set_usb_desc(dev);
912
913        mtx_init(&sc->sc_mtx, device_get_nameunit(dev),
914	    NULL, MTX_DEF | MTX_RECURSE);
915
916	/* get interface index */
917
918	id = usbd_get_interface_descriptor(uaa->iface);
919	if (id == NULL) {
920		device_printf(dev, "failed to get "
921		    "interface number\n");
922		goto detach;
923	}
924	sc->sc_iface_no = id->bInterfaceNumber;
925
926#ifdef USB_DEBUG
927	device_printf(dev, " ");
928
929	switch (sc->sc_proto & UMASS_PROTO_COMMAND) {
930	case UMASS_PROTO_SCSI:
931		printf("SCSI");
932		break;
933	case UMASS_PROTO_ATAPI:
934		printf("8070i (ATAPI)");
935		break;
936	case UMASS_PROTO_UFI:
937		printf("UFI");
938		break;
939	case UMASS_PROTO_RBC:
940		printf("RBC");
941		break;
942	default:
943		printf("(unknown 0x%02x)",
944		    sc->sc_proto & UMASS_PROTO_COMMAND);
945		break;
946	}
947
948	printf(" over ");
949
950	switch (sc->sc_proto & UMASS_PROTO_WIRE) {
951	case UMASS_PROTO_BBB:
952		printf("Bulk-Only");
953		break;
954	case UMASS_PROTO_CBI:		/* uses Comand/Bulk pipes */
955		printf("CBI");
956		break;
957	case UMASS_PROTO_CBI_I:	/* uses Comand/Bulk/Interrupt pipes */
958		printf("CBI with CCI");
959		break;
960	default:
961		printf("(unknown 0x%02x)",
962		    sc->sc_proto & UMASS_PROTO_WIRE);
963	}
964
965	printf("; quirks = 0x%04x\n", sc->sc_quirks);
966#endif
967
968	if (sc->sc_quirks & ALT_IFACE_1) {
969		err = usbd_set_alt_interface_index
970		    (uaa->device, uaa->info.bIfaceIndex, 1);
971
972		if (err) {
973			DPRINTF(sc, UDMASS_USB, "could not switch to "
974			    "Alt Interface 1\n");
975			goto detach;
976		}
977	}
978	/* allocate all required USB transfers */
979
980	if (sc->sc_proto & UMASS_PROTO_BBB) {
981		err = usbd_transfer_setup(uaa->device,
982		    &uaa->info.bIfaceIndex, sc->sc_xfer, umass_bbb_config,
983		    UMASS_T_BBB_MAX, sc, &sc->sc_mtx);
984
985		/* skip reset first time */
986		sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
987
988	} else if (sc->sc_proto & (UMASS_PROTO_CBI | UMASS_PROTO_CBI_I)) {
989		err = usbd_transfer_setup(uaa->device,
990		    &uaa->info.bIfaceIndex, sc->sc_xfer, umass_cbi_config,
991		    UMASS_T_CBI_MAX, sc, &sc->sc_mtx);
992
993		/* skip reset first time */
994		sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
995
996	} else {
997		err = USB_ERR_INVAL;
998	}
999
1000	if (err) {
1001		device_printf(dev, "could not setup required "
1002		    "transfers, %s\n", usbd_errstr(err));
1003		goto detach;
1004	}
1005#ifdef USB_DEBUG
1006	if (umass_throttle > 0) {
1007		uint8_t x;
1008		int iv;
1009
1010		iv = umass_throttle;
1011
1012		if (iv < 1)
1013			iv = 1;
1014		else if (iv > 8000)
1015			iv = 8000;
1016
1017		for (x = 0; x != UMASS_T_MAX; x++) {
1018			if (sc->sc_xfer[x] != NULL)
1019				usbd_xfer_set_interval(sc->sc_xfer[x], iv);
1020		}
1021	}
1022#endif
1023	sc->sc_transform =
1024	    (sc->sc_proto & UMASS_PROTO_SCSI) ? &umass_scsi_transform :
1025	    (sc->sc_proto & UMASS_PROTO_UFI) ? &umass_ufi_transform :
1026	    (sc->sc_proto & UMASS_PROTO_ATAPI) ? &umass_atapi_transform :
1027	    (sc->sc_proto & UMASS_PROTO_RBC) ? &umass_rbc_transform :
1028	    &umass_no_transform;
1029
1030	/* from here onwards the device can be used. */
1031
1032	if (sc->sc_quirks & SHUTTLE_INIT) {
1033		umass_init_shuttle(sc);
1034	}
1035	/* get the maximum LUN supported by the device */
1036
1037	if (((sc->sc_proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) &&
1038	    !(sc->sc_quirks & NO_GETMAXLUN))
1039		sc->sc_maxlun = umass_bbb_get_max_lun(sc);
1040	else
1041		sc->sc_maxlun = 0;
1042
1043	/* Prepare the SCSI command block */
1044	sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1045	sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1046
1047	/* register the SIM */
1048	err = umass_cam_attach_sim(sc);
1049	if (err) {
1050		goto detach;
1051	}
1052	/* scan the SIM */
1053	umass_cam_attach(sc);
1054
1055	DPRINTF(sc, UDMASS_GEN, "Attach finished\n");
1056
1057	return (0);			/* success */
1058
1059detach:
1060	umass_detach(dev);
1061	return (ENXIO);			/* failure */
1062}
1063
1064static int
1065umass_detach(device_t dev)
1066{
1067	struct umass_softc *sc = device_get_softc(dev);
1068
1069	DPRINTF(sc, UDMASS_USB, "\n");
1070
1071	/* teardown our statemachine */
1072
1073	usbd_transfer_unsetup(sc->sc_xfer, UMASS_T_MAX);
1074
1075	mtx_lock(&sc->sc_mtx);
1076
1077	/* cancel any leftover CCB's */
1078
1079	umass_cancel_ccb(sc);
1080
1081	umass_cam_detach_sim(sc);
1082
1083	mtx_unlock(&sc->sc_mtx);
1084
1085	mtx_destroy(&sc->sc_mtx);
1086
1087	return (0);			/* success */
1088}
1089
1090static void
1091umass_init_shuttle(struct umass_softc *sc)
1092{
1093	struct usb_device_request req;
1094	uint8_t status[2] = {0, 0};
1095
1096	/*
1097	 * The Linux driver does this, but no one can tell us what the
1098	 * command does.
1099	 */
1100	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1101	req.bRequest = 1;		/* XXX unknown command */
1102	USETW(req.wValue, 0);
1103	req.wIndex[0] = sc->sc_iface_no;
1104	req.wIndex[1] = 0;
1105	USETW(req.wLength, sizeof(status));
1106	usbd_do_request(sc->sc_udev, NULL, &req, &status);
1107
1108	DPRINTF(sc, UDMASS_GEN, "Shuttle init returned 0x%02x%02x\n",
1109	    status[0], status[1]);
1110}
1111
1112/*
1113 * Generic functions to handle transfers
1114 */
1115
1116static void
1117umass_transfer_start(struct umass_softc *sc, uint8_t xfer_index)
1118{
1119	DPRINTF(sc, UDMASS_GEN, "transfer index = "
1120	    "%d\n", xfer_index);
1121
1122	if (sc->sc_xfer[xfer_index]) {
1123		sc->sc_last_xfer_index = xfer_index;
1124		usbd_transfer_start(sc->sc_xfer[xfer_index]);
1125	} else {
1126		umass_cancel_ccb(sc);
1127	}
1128}
1129
1130static void
1131umass_reset(struct umass_softc *sc)
1132{
1133	DPRINTF(sc, UDMASS_GEN, "resetting device\n");
1134
1135	/*
1136	 * stop the last transfer, if not already stopped:
1137	 */
1138	usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
1139	umass_transfer_start(sc, 0);
1140}
1141
1142static void
1143umass_cancel_ccb(struct umass_softc *sc)
1144{
1145	union ccb *ccb;
1146
1147	USB_MTX_ASSERT(&sc->sc_mtx, MA_OWNED);
1148
1149	ccb = sc->sc_transfer.ccb;
1150	sc->sc_transfer.ccb = NULL;
1151	sc->sc_last_xfer_index = 0;
1152
1153	if (ccb) {
1154		(sc->sc_transfer.callback)
1155		    (sc, ccb, (sc->sc_transfer.data_len -
1156		    sc->sc_transfer.actlen), STATUS_WIRE_FAILED);
1157	}
1158}
1159
1160static void
1161umass_tr_error(struct usb_xfer *xfer, usb_error_t error)
1162{
1163	struct umass_softc *sc = usbd_xfer_softc(xfer);
1164
1165	if (error != USB_ERR_CANCELLED) {
1166		DPRINTF(sc, UDMASS_GEN, "transfer error, %s -> "
1167		    "reset\n", usbd_errstr(error));
1168	}
1169	umass_cancel_ccb(sc);
1170}
1171
1172/*
1173 * BBB protocol specific functions
1174 */
1175
1176static void
1177umass_t_bbb_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
1178{
1179	struct umass_softc *sc = usbd_xfer_softc(xfer);
1180	struct usb_device_request req;
1181	struct usb_page_cache *pc;
1182
1183	switch (USB_GET_STATE(xfer)) {
1184	case USB_ST_TRANSFERRED:
1185		umass_transfer_start(sc, UMASS_T_BBB_RESET2);
1186		return;
1187
1188	case USB_ST_SETUP:
1189		/*
1190		 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1191		 *
1192		 * For Reset Recovery the host shall issue in the following order:
1193		 * a) a Bulk-Only Mass Storage Reset
1194		 * b) a Clear Feature HALT to the Bulk-In endpoint
1195		 * c) a Clear Feature HALT to the Bulk-Out endpoint
1196		 *
1197		 * This is done in 3 steps, using 3 transfers:
1198		 * UMASS_T_BBB_RESET1
1199		 * UMASS_T_BBB_RESET2
1200		 * UMASS_T_BBB_RESET3
1201		 */
1202
1203		DPRINTF(sc, UDMASS_BBB, "BBB reset!\n");
1204
1205		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1206		req.bRequest = UR_BBB_RESET;	/* bulk only reset */
1207		USETW(req.wValue, 0);
1208		req.wIndex[0] = sc->sc_iface_no;
1209		req.wIndex[1] = 0;
1210		USETW(req.wLength, 0);
1211
1212		pc = usbd_xfer_get_frame(xfer, 0);
1213		usbd_copy_in(pc, 0, &req, sizeof(req));
1214
1215		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1216		usbd_xfer_set_frames(xfer, 1);
1217		usbd_transfer_submit(xfer);
1218		return;
1219
1220	default:			/* Error */
1221		umass_tr_error(xfer, error);
1222		return;
1223	}
1224}
1225
1226static void
1227umass_t_bbb_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
1228{
1229	umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_RESET3,
1230	    UMASS_T_BBB_DATA_READ, error);
1231}
1232
1233static void
1234umass_t_bbb_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
1235{
1236	umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_COMMAND,
1237	    UMASS_T_BBB_DATA_WRITE, error);
1238}
1239
1240static void
1241umass_t_bbb_data_clear_stall_callback(struct usb_xfer *xfer,
1242    uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
1243{
1244	struct umass_softc *sc = usbd_xfer_softc(xfer);
1245
1246	switch (USB_GET_STATE(xfer)) {
1247	case USB_ST_TRANSFERRED:
1248tr_transferred:
1249		umass_transfer_start(sc, next_xfer);
1250		return;
1251
1252	case USB_ST_SETUP:
1253		if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1254			goto tr_transferred;
1255		}
1256		return;
1257
1258	default:			/* Error */
1259		umass_tr_error(xfer, error);
1260		return;
1261	}
1262}
1263
1264static void
1265umass_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
1266{
1267	struct umass_softc *sc = usbd_xfer_softc(xfer);
1268	union ccb *ccb = sc->sc_transfer.ccb;
1269	struct usb_page_cache *pc;
1270	uint32_t tag;
1271
1272	switch (USB_GET_STATE(xfer)) {
1273	case USB_ST_TRANSFERRED:
1274		umass_transfer_start
1275		    (sc, ((sc->sc_transfer.dir == DIR_IN) ? UMASS_T_BBB_DATA_READ :
1276		    (sc->sc_transfer.dir == DIR_OUT) ? UMASS_T_BBB_DATA_WRITE :
1277		    UMASS_T_BBB_STATUS));
1278		return;
1279
1280	case USB_ST_SETUP:
1281
1282		sc->sc_status_try = 0;
1283
1284		if (ccb) {
1285			/*
1286		         * the initial value is not important,
1287		         * as long as the values are unique:
1288		         */
1289			tag = UGETDW(sc->cbw.dCBWTag) + 1;
1290
1291			USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1292			USETDW(sc->cbw.dCBWTag, tag);
1293
1294			/*
1295		         * dCBWDataTransferLength:
1296		         *   This field indicates the number of bytes of data that the host
1297		         *   intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1298		         *   the Direction bit) during the execution of this command. If this
1299		         *   field is set to 0, the device will expect that no data will be
1300		         *   transferred IN or OUT during this command, regardless of the value
1301		         *   of the Direction bit defined in dCBWFlags.
1302		         */
1303			USETDW(sc->cbw.dCBWDataTransferLength, sc->sc_transfer.data_len);
1304
1305			/*
1306		         * dCBWFlags:
1307		         *   The bits of the Flags field are defined as follows:
1308		         *     Bits 0-6  reserved
1309		         *     Bit  7    Direction - this bit shall be ignored if the
1310		         *                           dCBWDataTransferLength field is zero.
1311		         *               0 = data Out from host to device
1312		         *               1 = data In from device to host
1313		         */
1314			sc->cbw.bCBWFlags = ((sc->sc_transfer.dir == DIR_IN) ?
1315			    CBWFLAGS_IN : CBWFLAGS_OUT);
1316			sc->cbw.bCBWLUN = sc->sc_transfer.lun;
1317
1318			if (sc->sc_transfer.cmd_len > sizeof(sc->cbw.CBWCDB)) {
1319				sc->sc_transfer.cmd_len = sizeof(sc->cbw.CBWCDB);
1320				DPRINTF(sc, UDMASS_BBB, "Truncating long command!\n");
1321			}
1322			sc->cbw.bCDBLength = sc->sc_transfer.cmd_len;
1323
1324			/* copy SCSI command data */
1325			memcpy(sc->cbw.CBWCDB, sc->sc_transfer.cmd_data,
1326			    sc->sc_transfer.cmd_len);
1327
1328			/* clear remaining command area */
1329			memset(sc->cbw.CBWCDB +
1330			    sc->sc_transfer.cmd_len, 0,
1331			    sizeof(sc->cbw.CBWCDB) -
1332			    sc->sc_transfer.cmd_len);
1333
1334			DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1335
1336			pc = usbd_xfer_get_frame(xfer, 0);
1337			usbd_copy_in(pc, 0, &sc->cbw, sizeof(sc->cbw));
1338			usbd_xfer_set_frame_len(xfer, 0, sizeof(sc->cbw));
1339
1340			usbd_transfer_submit(xfer);
1341		}
1342		return;
1343
1344	default:			/* Error */
1345		umass_tr_error(xfer, error);
1346		return;
1347	}
1348}
1349
1350static void
1351umass_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
1352{
1353	struct umass_softc *sc = usbd_xfer_softc(xfer);
1354	uint32_t max_bulk = usbd_xfer_max_len(xfer);
1355	int actlen, sumlen;
1356
1357	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1358
1359	switch (USB_GET_STATE(xfer)) {
1360	case USB_ST_TRANSFERRED:
1361		sc->sc_transfer.data_rem -= actlen;
1362		sc->sc_transfer.data_ptr += actlen;
1363		sc->sc_transfer.actlen += actlen;
1364
1365		if (actlen < sumlen) {
1366			/* short transfer */
1367			sc->sc_transfer.data_rem = 0;
1368		}
1369	case USB_ST_SETUP:
1370		DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1371		    max_bulk, sc->sc_transfer.data_rem);
1372
1373		if (sc->sc_transfer.data_rem == 0) {
1374			umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1375			return;
1376		}
1377		if (max_bulk > sc->sc_transfer.data_rem) {
1378			max_bulk = sc->sc_transfer.data_rem;
1379		}
1380		usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1381
1382		usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1383		    max_bulk);
1384
1385		usbd_transfer_submit(xfer);
1386		return;
1387
1388	default:			/* Error */
1389		if (error == USB_ERR_CANCELLED) {
1390			umass_tr_error(xfer, error);
1391		} else {
1392			umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1393		}
1394		return;
1395	}
1396}
1397
1398static void
1399umass_t_bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1400{
1401	umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1402	    UMASS_T_BBB_DATA_READ, error);
1403}
1404
1405static void
1406umass_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
1407{
1408	struct umass_softc *sc = usbd_xfer_softc(xfer);
1409	uint32_t max_bulk = usbd_xfer_max_len(xfer);
1410	int actlen, sumlen;
1411
1412	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1413
1414	switch (USB_GET_STATE(xfer)) {
1415	case USB_ST_TRANSFERRED:
1416		sc->sc_transfer.data_rem -= actlen;
1417		sc->sc_transfer.data_ptr += actlen;
1418		sc->sc_transfer.actlen += actlen;
1419
1420		if (actlen < sumlen) {
1421			/* short transfer */
1422			sc->sc_transfer.data_rem = 0;
1423		}
1424	case USB_ST_SETUP:
1425		DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1426		    max_bulk, sc->sc_transfer.data_rem);
1427
1428		if (sc->sc_transfer.data_rem == 0) {
1429			umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1430			return;
1431		}
1432		if (max_bulk > sc->sc_transfer.data_rem) {
1433			max_bulk = sc->sc_transfer.data_rem;
1434		}
1435		usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1436
1437		usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1438		    max_bulk);
1439
1440		usbd_transfer_submit(xfer);
1441		return;
1442
1443	default:			/* Error */
1444		if (error == USB_ERR_CANCELLED) {
1445			umass_tr_error(xfer, error);
1446		} else {
1447			umass_transfer_start(sc, UMASS_T_BBB_DATA_WR_CS);
1448		}
1449		return;
1450	}
1451}
1452
1453static void
1454umass_t_bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1455{
1456	umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1457	    UMASS_T_BBB_DATA_WRITE, error);
1458}
1459
1460static void
1461umass_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
1462{
1463	struct umass_softc *sc = usbd_xfer_softc(xfer);
1464	union ccb *ccb = sc->sc_transfer.ccb;
1465	struct usb_page_cache *pc;
1466	uint32_t residue;
1467	int actlen;
1468
1469	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1470
1471	switch (USB_GET_STATE(xfer)) {
1472	case USB_ST_TRANSFERRED:
1473
1474		/*
1475		 * Do a full reset if there is something wrong with the CSW:
1476		 */
1477		sc->sc_status_try = 1;
1478
1479		/* Zero missing parts of the CSW: */
1480
1481		if (actlen < (int)sizeof(sc->csw))
1482			memset(&sc->csw, 0, sizeof(sc->csw));
1483
1484		pc = usbd_xfer_get_frame(xfer, 0);
1485		usbd_copy_out(pc, 0, &sc->csw, actlen);
1486
1487		DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
1488
1489		residue = UGETDW(sc->csw.dCSWDataResidue);
1490
1491		if ((!residue) || (sc->sc_quirks & IGNORE_RESIDUE)) {
1492			residue = (sc->sc_transfer.data_len -
1493			    sc->sc_transfer.actlen);
1494		}
1495		if (residue > sc->sc_transfer.data_len) {
1496			DPRINTF(sc, UDMASS_BBB, "truncating residue from %d "
1497			    "to %d bytes\n", residue, sc->sc_transfer.data_len);
1498			residue = sc->sc_transfer.data_len;
1499		}
1500		/* translate weird command-status signatures: */
1501		if (sc->sc_quirks & WRONG_CSWSIG) {
1502			uint32_t temp = UGETDW(sc->csw.dCSWSignature);
1503
1504			if ((temp == CSWSIGNATURE_OLYMPUS_C1) ||
1505			    (temp == CSWSIGNATURE_IMAGINATION_DBX1)) {
1506				USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
1507			}
1508		}
1509		/* check CSW and handle eventual error */
1510		if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1511			DPRINTF(sc, UDMASS_BBB, "bad CSW signature 0x%08x != 0x%08x\n",
1512			    UGETDW(sc->csw.dCSWSignature), CSWSIGNATURE);
1513			/*
1514			 * Invalid CSW: Wrong signature or wrong tag might
1515			 * indicate that we lost synchronization. Reset the
1516			 * device.
1517			 */
1518			goto tr_error;
1519		} else if (UGETDW(sc->csw.dCSWTag) != UGETDW(sc->cbw.dCBWTag)) {
1520			DPRINTF(sc, UDMASS_BBB, "Invalid CSW: tag 0x%08x should be "
1521			    "0x%08x\n", UGETDW(sc->csw.dCSWTag),
1522			    UGETDW(sc->cbw.dCBWTag));
1523			goto tr_error;
1524		} else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1525			DPRINTF(sc, UDMASS_BBB, "Invalid CSW: status %d > %d\n",
1526			    sc->csw.bCSWStatus, CSWSTATUS_PHASE);
1527			goto tr_error;
1528		} else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1529			DPRINTF(sc, UDMASS_BBB, "Phase error, residue = "
1530			    "%d\n", residue);
1531			goto tr_error;
1532		} else if (sc->sc_transfer.actlen > sc->sc_transfer.data_len) {
1533			DPRINTF(sc, UDMASS_BBB, "Buffer overrun %d > %d\n",
1534			    sc->sc_transfer.actlen, sc->sc_transfer.data_len);
1535			goto tr_error;
1536		} else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1537			DPRINTF(sc, UDMASS_BBB, "Command failed, residue = "
1538			    "%d\n", residue);
1539
1540			sc->sc_transfer.ccb = NULL;
1541
1542			sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
1543
1544			(sc->sc_transfer.callback)
1545			    (sc, ccb, residue, STATUS_CMD_FAILED);
1546		} else {
1547			sc->sc_transfer.ccb = NULL;
1548
1549			sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
1550
1551			(sc->sc_transfer.callback)
1552			    (sc, ccb, residue, STATUS_CMD_OK);
1553		}
1554		return;
1555
1556	case USB_ST_SETUP:
1557		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1558		usbd_transfer_submit(xfer);
1559		return;
1560
1561	default:
1562tr_error:
1563		DPRINTF(sc, UDMASS_BBB, "Failed to read CSW: %s, try %d\n",
1564		    usbd_errstr(error), sc->sc_status_try);
1565
1566		if ((error == USB_ERR_CANCELLED) ||
1567		    (sc->sc_status_try)) {
1568			umass_tr_error(xfer, error);
1569		} else {
1570			sc->sc_status_try = 1;
1571			umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1572		}
1573		return;
1574	}
1575}
1576
1577static void
1578umass_command_start(struct umass_softc *sc, uint8_t dir,
1579    void *data_ptr, uint32_t data_len,
1580    uint32_t data_timeout, umass_callback_t *callback,
1581    union ccb *ccb)
1582{
1583	sc->sc_transfer.lun = ccb->ccb_h.target_lun;
1584
1585	/*
1586	 * NOTE: assumes that "sc->sc_transfer.cmd_data" and
1587	 * "sc->sc_transfer.cmd_len" has been properly
1588	 * initialized.
1589	 */
1590
1591	sc->sc_transfer.dir = data_len ? dir : DIR_NONE;
1592	sc->sc_transfer.data_ptr = data_ptr;
1593	sc->sc_transfer.data_len = data_len;
1594	sc->sc_transfer.data_rem = data_len;
1595	sc->sc_transfer.data_timeout = (data_timeout + UMASS_TIMEOUT);
1596
1597	sc->sc_transfer.actlen = 0;
1598	sc->sc_transfer.callback = callback;
1599	sc->sc_transfer.ccb = ccb;
1600
1601	if (sc->sc_xfer[sc->sc_last_xfer_index]) {
1602		usbd_transfer_start(sc->sc_xfer[sc->sc_last_xfer_index]);
1603	} else {
1604		umass_cancel_ccb(sc);
1605	}
1606}
1607
1608static uint8_t
1609umass_bbb_get_max_lun(struct umass_softc *sc)
1610{
1611	struct usb_device_request req;
1612	usb_error_t err;
1613	uint8_t buf = 0;
1614
1615	/* The Get Max Lun command is a class-specific request. */
1616	req.bmRequestType = UT_READ_CLASS_INTERFACE;
1617	req.bRequest = UR_BBB_GET_MAX_LUN;
1618	USETW(req.wValue, 0);
1619	req.wIndex[0] = sc->sc_iface_no;
1620	req.wIndex[1] = 0;
1621	USETW(req.wLength, 1);
1622
1623	err = usbd_do_request(sc->sc_udev, NULL, &req, &buf);
1624	if (err) {
1625		buf = 0;
1626
1627		/* Device doesn't support Get Max Lun request. */
1628		printf("%s: Get Max Lun not supported (%s)\n",
1629		    sc->sc_name, usbd_errstr(err));
1630	}
1631	return (buf);
1632}
1633
1634/*
1635 * Command/Bulk/Interrupt (CBI) specific functions
1636 */
1637
1638static void
1639umass_cbi_start_status(struct umass_softc *sc)
1640{
1641	if (sc->sc_xfer[UMASS_T_CBI_STATUS]) {
1642		umass_transfer_start(sc, UMASS_T_CBI_STATUS);
1643	} else {
1644		union ccb *ccb = sc->sc_transfer.ccb;
1645
1646		sc->sc_transfer.ccb = NULL;
1647
1648		sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1649
1650		(sc->sc_transfer.callback)
1651		    (sc, ccb, (sc->sc_transfer.data_len -
1652		    sc->sc_transfer.actlen), STATUS_CMD_UNKNOWN);
1653	}
1654}
1655
1656static void
1657umass_t_cbi_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
1658{
1659	struct umass_softc *sc = usbd_xfer_softc(xfer);
1660	struct usb_device_request req;
1661	struct usb_page_cache *pc;
1662	uint8_t buf[UMASS_CBI_DIAGNOSTIC_CMDLEN];
1663
1664	uint8_t i;
1665
1666	switch (USB_GET_STATE(xfer)) {
1667	case USB_ST_TRANSFERRED:
1668		umass_transfer_start(sc, UMASS_T_CBI_RESET2);
1669		break;
1670
1671	case USB_ST_SETUP:
1672		/*
1673		 * Command Block Reset Protocol
1674		 *
1675		 * First send a reset request to the device. Then clear
1676		 * any possibly stalled bulk endpoints.
1677		 *
1678		 * This is done in 3 steps, using 3 transfers:
1679		 * UMASS_T_CBI_RESET1
1680		 * UMASS_T_CBI_RESET2
1681		 * UMASS_T_CBI_RESET3
1682		 * UMASS_T_CBI_RESET4 (only if there is an interrupt endpoint)
1683		 */
1684
1685		DPRINTF(sc, UDMASS_CBI, "CBI reset!\n");
1686
1687		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1688		req.bRequest = UR_CBI_ADSC;
1689		USETW(req.wValue, 0);
1690		req.wIndex[0] = sc->sc_iface_no;
1691		req.wIndex[1] = 0;
1692		USETW(req.wLength, UMASS_CBI_DIAGNOSTIC_CMDLEN);
1693
1694		/*
1695		 * The 0x1d code is the SEND DIAGNOSTIC command. To
1696		 * distinguish between the two, the last 10 bytes of the CBL
1697		 * is filled with 0xff (section 2.2 of the CBI
1698		 * specification)
1699		 */
1700		buf[0] = 0x1d;		/* Command Block Reset */
1701		buf[1] = 0x04;
1702
1703		for (i = 2; i < UMASS_CBI_DIAGNOSTIC_CMDLEN; i++) {
1704			buf[i] = 0xff;
1705		}
1706
1707		pc = usbd_xfer_get_frame(xfer, 0);
1708		usbd_copy_in(pc, 0, &req, sizeof(req));
1709		pc = usbd_xfer_get_frame(xfer, 1);
1710		usbd_copy_in(pc, 0, buf, sizeof(buf));
1711
1712		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1713		usbd_xfer_set_frame_len(xfer, 1, sizeof(buf));
1714		usbd_xfer_set_frames(xfer, 2);
1715		usbd_transfer_submit(xfer);
1716		break;
1717
1718	default:			/* Error */
1719		if (error == USB_ERR_CANCELLED)
1720			umass_tr_error(xfer, error);
1721		else
1722			umass_transfer_start(sc, UMASS_T_CBI_RESET2);
1723		break;
1724	}
1725}
1726
1727static void
1728umass_t_cbi_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
1729{
1730	umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_RESET3,
1731	    UMASS_T_CBI_DATA_READ, error);
1732}
1733
1734static void
1735umass_t_cbi_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
1736{
1737	struct umass_softc *sc = usbd_xfer_softc(xfer);
1738
1739	umass_t_cbi_data_clear_stall_callback
1740	    (xfer, (sc->sc_xfer[UMASS_T_CBI_RESET4] &&
1741	    sc->sc_xfer[UMASS_T_CBI_STATUS]) ?
1742	    UMASS_T_CBI_RESET4 : UMASS_T_CBI_COMMAND,
1743	    UMASS_T_CBI_DATA_WRITE, error);
1744}
1745
1746static void
1747umass_t_cbi_reset4_callback(struct usb_xfer *xfer, usb_error_t error)
1748{
1749	umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_COMMAND,
1750	    UMASS_T_CBI_STATUS, error);
1751}
1752
1753static void
1754umass_t_cbi_data_clear_stall_callback(struct usb_xfer *xfer,
1755    uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
1756{
1757	struct umass_softc *sc = usbd_xfer_softc(xfer);
1758
1759	switch (USB_GET_STATE(xfer)) {
1760	case USB_ST_TRANSFERRED:
1761tr_transferred:
1762		if (next_xfer == UMASS_T_CBI_STATUS) {
1763			umass_cbi_start_status(sc);
1764		} else {
1765			umass_transfer_start(sc, next_xfer);
1766		}
1767		break;
1768
1769	case USB_ST_SETUP:
1770		if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1771			goto tr_transferred;	/* should not happen */
1772		}
1773		break;
1774
1775	default:			/* Error */
1776		umass_tr_error(xfer, error);
1777		break;
1778	}
1779}
1780
1781static void
1782umass_t_cbi_command_callback(struct usb_xfer *xfer, usb_error_t error)
1783{
1784	struct umass_softc *sc = usbd_xfer_softc(xfer);
1785	union ccb *ccb = sc->sc_transfer.ccb;
1786	struct usb_device_request req;
1787	struct usb_page_cache *pc;
1788
1789	switch (USB_GET_STATE(xfer)) {
1790	case USB_ST_TRANSFERRED:
1791
1792		if (sc->sc_transfer.dir == DIR_NONE) {
1793			umass_cbi_start_status(sc);
1794		} else {
1795			umass_transfer_start
1796			    (sc, (sc->sc_transfer.dir == DIR_IN) ?
1797			    UMASS_T_CBI_DATA_READ : UMASS_T_CBI_DATA_WRITE);
1798		}
1799		break;
1800
1801	case USB_ST_SETUP:
1802
1803		if (ccb) {
1804			/*
1805		         * do a CBI transfer with cmd_len bytes from
1806		         * cmd_data, possibly a data phase of data_len
1807		         * bytes from/to the device and finally a status
1808		         * read phase.
1809		         */
1810
1811			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1812			req.bRequest = UR_CBI_ADSC;
1813			USETW(req.wValue, 0);
1814			req.wIndex[0] = sc->sc_iface_no;
1815			req.wIndex[1] = 0;
1816			req.wLength[0] = sc->sc_transfer.cmd_len;
1817			req.wLength[1] = 0;
1818
1819			pc = usbd_xfer_get_frame(xfer, 0);
1820			usbd_copy_in(pc, 0, &req, sizeof(req));
1821			pc = usbd_xfer_get_frame(xfer, 1);
1822			usbd_copy_in(pc, 0, sc->sc_transfer.cmd_data,
1823			    sc->sc_transfer.cmd_len);
1824
1825			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1826			usbd_xfer_set_frame_len(xfer, 1, sc->sc_transfer.cmd_len);
1827			usbd_xfer_set_frames(xfer,
1828			    sc->sc_transfer.cmd_len ? 2 : 1);
1829
1830			DIF(UDMASS_CBI,
1831			    umass_cbi_dump_cmd(sc,
1832			    sc->sc_transfer.cmd_data,
1833			    sc->sc_transfer.cmd_len));
1834
1835			usbd_transfer_submit(xfer);
1836		}
1837		break;
1838
1839	default:			/* Error */
1840		/*
1841		 * STALL on the control pipe can be result of the command error.
1842		 * Attempt to clear this STALL same as for bulk pipe also
1843		 * results in command completion interrupt, but ASC/ASCQ there
1844		 * look like not always valid, so don't bother about it.
1845		 */
1846		if ((error == USB_ERR_STALLED) ||
1847		    (sc->sc_transfer.callback == &umass_cam_cb)) {
1848			sc->sc_transfer.ccb = NULL;
1849			(sc->sc_transfer.callback)
1850			    (sc, ccb, sc->sc_transfer.data_len,
1851			    STATUS_CMD_UNKNOWN);
1852		} else {
1853			umass_tr_error(xfer, error);
1854			/* skip reset */
1855			sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1856		}
1857		break;
1858	}
1859}
1860
1861static void
1862umass_t_cbi_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
1863{
1864	struct umass_softc *sc = usbd_xfer_softc(xfer);
1865	uint32_t max_bulk = usbd_xfer_max_len(xfer);
1866	int actlen, sumlen;
1867
1868	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1869
1870	switch (USB_GET_STATE(xfer)) {
1871	case USB_ST_TRANSFERRED:
1872		sc->sc_transfer.data_rem -= actlen;
1873		sc->sc_transfer.data_ptr += actlen;
1874		sc->sc_transfer.actlen += actlen;
1875
1876		if (actlen < sumlen) {
1877			/* short transfer */
1878			sc->sc_transfer.data_rem = 0;
1879		}
1880	case USB_ST_SETUP:
1881		DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
1882		    max_bulk, sc->sc_transfer.data_rem);
1883
1884		if (sc->sc_transfer.data_rem == 0) {
1885			umass_cbi_start_status(sc);
1886			break;
1887		}
1888		if (max_bulk > sc->sc_transfer.data_rem) {
1889			max_bulk = sc->sc_transfer.data_rem;
1890		}
1891		usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1892
1893		usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1894		    max_bulk);
1895
1896		usbd_transfer_submit(xfer);
1897		break;
1898
1899	default:			/* Error */
1900		if ((error == USB_ERR_CANCELLED) ||
1901		    (sc->sc_transfer.callback != &umass_cam_cb)) {
1902			umass_tr_error(xfer, error);
1903		} else {
1904			umass_transfer_start(sc, UMASS_T_CBI_DATA_RD_CS);
1905		}
1906		break;
1907	}
1908}
1909
1910static void
1911umass_t_cbi_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1912{
1913	umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
1914	    UMASS_T_CBI_DATA_READ, error);
1915}
1916
1917static void
1918umass_t_cbi_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
1919{
1920	struct umass_softc *sc = usbd_xfer_softc(xfer);
1921	uint32_t max_bulk = usbd_xfer_max_len(xfer);
1922	int actlen, sumlen;
1923
1924	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1925
1926	switch (USB_GET_STATE(xfer)) {
1927	case USB_ST_TRANSFERRED:
1928		sc->sc_transfer.data_rem -= actlen;
1929		sc->sc_transfer.data_ptr += actlen;
1930		sc->sc_transfer.actlen += actlen;
1931
1932		if (actlen < sumlen) {
1933			/* short transfer */
1934			sc->sc_transfer.data_rem = 0;
1935		}
1936	case USB_ST_SETUP:
1937		DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
1938		    max_bulk, sc->sc_transfer.data_rem);
1939
1940		if (sc->sc_transfer.data_rem == 0) {
1941			umass_cbi_start_status(sc);
1942			break;
1943		}
1944		if (max_bulk > sc->sc_transfer.data_rem) {
1945			max_bulk = sc->sc_transfer.data_rem;
1946		}
1947		usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1948
1949		usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1950		    max_bulk);
1951
1952		usbd_transfer_submit(xfer);
1953		break;
1954
1955	default:			/* Error */
1956		if ((error == USB_ERR_CANCELLED) ||
1957		    (sc->sc_transfer.callback != &umass_cam_cb)) {
1958			umass_tr_error(xfer, error);
1959		} else {
1960			umass_transfer_start(sc, UMASS_T_CBI_DATA_WR_CS);
1961		}
1962		break;
1963	}
1964}
1965
1966static void
1967umass_t_cbi_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1968{
1969	umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
1970	    UMASS_T_CBI_DATA_WRITE, error);
1971}
1972
1973static void
1974umass_t_cbi_status_callback(struct usb_xfer *xfer, usb_error_t error)
1975{
1976	struct umass_softc *sc = usbd_xfer_softc(xfer);
1977	union ccb *ccb = sc->sc_transfer.ccb;
1978	struct usb_page_cache *pc;
1979	uint32_t residue;
1980	uint8_t status;
1981	int actlen;
1982
1983	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1984
1985	switch (USB_GET_STATE(xfer)) {
1986	case USB_ST_TRANSFERRED:
1987
1988		if (actlen < (int)sizeof(sc->sbl)) {
1989			goto tr_setup;
1990		}
1991		pc = usbd_xfer_get_frame(xfer, 0);
1992		usbd_copy_out(pc, 0, &sc->sbl, sizeof(sc->sbl));
1993
1994		residue = (sc->sc_transfer.data_len -
1995		    sc->sc_transfer.actlen);
1996
1997		/* dissect the information in the buffer */
1998
1999		if (sc->sc_proto & UMASS_PROTO_UFI) {
2000			/*
2001			 * Section 3.4.3.1.3 specifies that the UFI command
2002			 * protocol returns an ASC and ASCQ in the interrupt
2003			 * data block.
2004			 */
2005
2006			DPRINTF(sc, UDMASS_CBI, "UFI CCI, ASC = 0x%02x, "
2007			    "ASCQ = 0x%02x\n", sc->sbl.ufi.asc,
2008			    sc->sbl.ufi.ascq);
2009
2010			status = (((sc->sbl.ufi.asc == 0) &&
2011			    (sc->sbl.ufi.ascq == 0)) ?
2012			    STATUS_CMD_OK : STATUS_CMD_FAILED);
2013
2014			sc->sc_transfer.ccb = NULL;
2015
2016			sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2017
2018			(sc->sc_transfer.callback)
2019			    (sc, ccb, residue, status);
2020
2021			break;
2022
2023		} else {
2024			/* Command Interrupt Data Block */
2025
2026			DPRINTF(sc, UDMASS_CBI, "type=0x%02x, value=0x%02x\n",
2027			    sc->sbl.common.type, sc->sbl.common.value);
2028
2029			if (sc->sbl.common.type == IDB_TYPE_CCI) {
2030				status = (sc->sbl.common.value & IDB_VALUE_STATUS_MASK);
2031
2032				status = ((status == IDB_VALUE_PASS) ? STATUS_CMD_OK :
2033				    (status == IDB_VALUE_FAIL) ? STATUS_CMD_FAILED :
2034				    (status == IDB_VALUE_PERSISTENT) ? STATUS_CMD_FAILED :
2035				    STATUS_WIRE_FAILED);
2036
2037				sc->sc_transfer.ccb = NULL;
2038
2039				sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2040
2041				(sc->sc_transfer.callback)
2042				    (sc, ccb, residue, status);
2043
2044				break;
2045			}
2046		}
2047
2048		/* fallthrough */
2049
2050	case USB_ST_SETUP:
2051tr_setup:
2052		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2053		usbd_transfer_submit(xfer);
2054		break;
2055
2056	default:			/* Error */
2057		DPRINTF(sc, UDMASS_CBI, "Failed to read CSW: %s\n",
2058		    usbd_errstr(error));
2059		umass_tr_error(xfer, error);
2060		break;
2061	}
2062}
2063
2064/*
2065 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2066 */
2067
2068static int
2069umass_cam_attach_sim(struct umass_softc *sc)
2070{
2071	struct cam_devq *devq;		/* Per device Queue */
2072	cam_status status;
2073
2074	/*
2075	 * A HBA is attached to the CAM layer.
2076	 *
2077	 * The CAM layer will then after a while start probing for devices on
2078	 * the bus. The number of SIMs is limited to one.
2079	 */
2080
2081	devq = cam_simq_alloc(1 /* maximum openings */ );
2082	if (devq == NULL) {
2083		return (ENOMEM);
2084	}
2085	sc->sc_sim = cam_sim_alloc
2086	    (&umass_cam_action, &umass_cam_poll,
2087	    DEVNAME_SIM,
2088	    sc /* priv */ ,
2089	    sc->sc_unit /* unit number */ ,
2090	    &sc->sc_mtx /* mutex */ ,
2091	    1 /* maximum device openings */ ,
2092	    0 /* maximum tagged device openings */ ,
2093	    devq);
2094
2095	if (sc->sc_sim == NULL) {
2096		cam_simq_free(devq);
2097		return (ENOMEM);
2098	}
2099
2100	mtx_lock(&sc->sc_mtx);
2101	status = xpt_bus_register(sc->sc_sim, sc->sc_dev, sc->sc_unit);
2102	if (status != CAM_SUCCESS) {
2103		cam_sim_free(sc->sc_sim, /* free_devq */ TRUE);
2104		mtx_unlock(&sc->sc_mtx);
2105		printf("%s: xpt_bus_register failed with status %#x\n",
2106		    __func__, status);
2107		return (ENOMEM);
2108	}
2109	mtx_unlock(&sc->sc_mtx);
2110
2111	return (0);
2112}
2113
2114static void
2115umass_cam_attach(struct umass_softc *sc)
2116{
2117#ifndef USB_DEBUG
2118	if (bootverbose)
2119#endif
2120		printf("%s:%d:%d: Attached to scbus%d\n",
2121		    sc->sc_name, cam_sim_path(sc->sc_sim),
2122		    sc->sc_unit, cam_sim_path(sc->sc_sim));
2123}
2124
2125/* umass_cam_detach
2126 *	detach from the CAM layer
2127 */
2128
2129static void
2130umass_cam_detach_sim(struct umass_softc *sc)
2131{
2132	cam_status status;
2133
2134	if (sc->sc_sim != NULL) {
2135		status = xpt_bus_deregister(cam_sim_path(sc->sc_sim));
2136		if (status == CAM_REQ_CMP) {
2137			/* accessing the softc is not possible after this */
2138			sc->sc_sim->softc = NULL;
2139			DPRINTF(sc, UDMASS_SCSI, "%s: %s:%d:%d caling "
2140			    "cam_sim_free sim %p refc %u mtx %p\n",
2141			    __func__, sc->sc_name, cam_sim_path(sc->sc_sim),
2142			    sc->sc_unit, sc->sc_sim,
2143			    sc->sc_sim->refcount, sc->sc_sim->mtx);
2144			cam_sim_free(sc->sc_sim, /* free_devq */ TRUE);
2145		} else {
2146			panic("%s: %s: CAM layer is busy: %#x\n",
2147			    __func__, sc->sc_name, status);
2148		}
2149		sc->sc_sim = NULL;
2150	}
2151}
2152
2153/* umass_cam_action
2154 * 	CAM requests for action come through here
2155 */
2156
2157static void
2158umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2159{
2160	struct umass_softc *sc = (struct umass_softc *)sim->softc;
2161
2162	if (sc == NULL) {
2163		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2164		xpt_done(ccb);
2165		return;
2166	}
2167
2168	/* Perform the requested action */
2169	switch (ccb->ccb_h.func_code) {
2170	case XPT_SCSI_IO:
2171		{
2172			uint8_t *cmd;
2173			uint8_t dir;
2174
2175			if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2176				cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2177			} else {
2178				cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2179			}
2180
2181			DPRINTF(sc, UDMASS_SCSI, "%d:%d:%jx:XPT_SCSI_IO: "
2182			    "cmd: 0x%02x, flags: 0x%02x, "
2183			    "%db cmd/%db data/%db sense\n",
2184			    cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2185			    (uintmax_t)ccb->ccb_h.target_lun, cmd[0],
2186			    ccb->ccb_h.flags & CAM_DIR_MASK, ccb->csio.cdb_len,
2187			    ccb->csio.dxfer_len, ccb->csio.sense_len);
2188
2189			if (sc->sc_transfer.ccb) {
2190				DPRINTF(sc, UDMASS_SCSI, "%d:%d:%jx:XPT_SCSI_IO: "
2191				    "I/O in progress, deferring\n",
2192				    cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2193				    (uintmax_t)ccb->ccb_h.target_lun);
2194				ccb->ccb_h.status = CAM_SCSI_BUSY;
2195				xpt_done(ccb);
2196				goto done;
2197			}
2198			switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
2199			case CAM_DIR_IN:
2200				dir = DIR_IN;
2201				break;
2202			case CAM_DIR_OUT:
2203				dir = DIR_OUT;
2204				DIF(UDMASS_SCSI,
2205				    umass_dump_buffer(sc, ccb->csio.data_ptr,
2206				    ccb->csio.dxfer_len, 48));
2207				break;
2208			default:
2209				dir = DIR_NONE;
2210			}
2211
2212			ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2213
2214			/*
2215			 * sc->sc_transform will convert the command to the
2216			 * command format needed by the specific command set
2217			 * and return the converted command in
2218			 * "sc->sc_transfer.cmd_data"
2219			 */
2220			if (umass_std_transform(sc, ccb, cmd, ccb->csio.cdb_len)) {
2221				if (sc->sc_transfer.cmd_data[0] == INQUIRY) {
2222					const char *pserial;
2223
2224					pserial = usb_get_serial(sc->sc_udev);
2225
2226					/*
2227					 * Umass devices don't generally report their serial numbers
2228					 * in the usual SCSI way.  Emulate it here.
2229					 */
2230					if ((sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
2231					    (sc->sc_transfer.cmd_data[2] == SVPD_UNIT_SERIAL_NUMBER) &&
2232					    (pserial[0] != '\0')) {
2233						struct scsi_vpd_unit_serial_number *vpd_serial;
2234
2235						vpd_serial = (struct scsi_vpd_unit_serial_number *)ccb->csio.data_ptr;
2236						vpd_serial->length = strlen(pserial);
2237						if (vpd_serial->length > sizeof(vpd_serial->serial_num))
2238							vpd_serial->length = sizeof(vpd_serial->serial_num);
2239						memcpy(vpd_serial->serial_num, pserial, vpd_serial->length);
2240						ccb->csio.scsi_status = SCSI_STATUS_OK;
2241						ccb->ccb_h.status = CAM_REQ_CMP;
2242						xpt_done(ccb);
2243						goto done;
2244					}
2245
2246					/*
2247					 * Handle EVPD inquiry for broken devices first
2248					 * NO_INQUIRY also implies NO_INQUIRY_EVPD
2249					 */
2250					if ((sc->sc_quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
2251					    (sc->sc_transfer.cmd_data[1] & SI_EVPD)) {
2252						scsi_set_sense_data(&ccb->csio.sense_data,
2253							/*sense_format*/ SSD_TYPE_NONE,
2254							/*current_error*/ 1,
2255							/*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
2256							/*asc*/ 0x24,
2257							/*ascq*/ 0x00,
2258							/*extra args*/ SSD_ELEM_NONE);
2259						ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2260						ccb->ccb_h.status =
2261						    CAM_SCSI_STATUS_ERROR |
2262						    CAM_AUTOSNS_VALID |
2263						    CAM_DEV_QFRZN;
2264						xpt_freeze_devq(ccb->ccb_h.path, 1);
2265						xpt_done(ccb);
2266						goto done;
2267					}
2268					/*
2269					 * Return fake inquiry data for
2270					 * broken devices
2271					 */
2272					if (sc->sc_quirks & NO_INQUIRY) {
2273						memcpy(ccb->csio.data_ptr, &fake_inq_data,
2274						    sizeof(fake_inq_data));
2275						ccb->csio.scsi_status = SCSI_STATUS_OK;
2276						ccb->ccb_h.status = CAM_REQ_CMP;
2277						xpt_done(ccb);
2278						goto done;
2279					}
2280					if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2281						ccb->csio.dxfer_len = SHORT_INQUIRY_LENGTH;
2282					}
2283				} else if (sc->sc_transfer.cmd_data[0] == PREVENT_ALLOW) {
2284					if (sc->sc_quirks & NO_PREVENT_ALLOW) {
2285						ccb->csio.scsi_status = SCSI_STATUS_OK;
2286						ccb->ccb_h.status = CAM_REQ_CMP;
2287						xpt_done(ccb);
2288						goto done;
2289					}
2290				} else if (sc->sc_transfer.cmd_data[0] == SYNCHRONIZE_CACHE) {
2291					if (sc->sc_quirks & NO_SYNCHRONIZE_CACHE) {
2292						ccb->csio.scsi_status = SCSI_STATUS_OK;
2293						ccb->ccb_h.status = CAM_REQ_CMP;
2294						xpt_done(ccb);
2295						goto done;
2296					}
2297				}
2298				umass_command_start(sc, dir, ccb->csio.data_ptr,
2299				    ccb->csio.dxfer_len,
2300				    ccb->ccb_h.timeout,
2301				    &umass_cam_cb, ccb);
2302			}
2303			break;
2304		}
2305	case XPT_PATH_INQ:
2306		{
2307			struct ccb_pathinq *cpi = &ccb->cpi;
2308
2309			DPRINTF(sc, UDMASS_SCSI, "%d:%d:%jx:XPT_PATH_INQ:.\n",
2310			    sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2311			    (uintmax_t)ccb->ccb_h.target_lun);
2312
2313			/* host specific information */
2314			cpi->version_num = 1;
2315			cpi->hba_inquiry = 0;
2316			cpi->target_sprt = 0;
2317			cpi->hba_misc = PIM_NO_6_BYTE;
2318			cpi->hba_eng_cnt = 0;
2319			cpi->max_target = UMASS_SCSIID_MAX;	/* one target */
2320			cpi->initiator_id = UMASS_SCSIID_HOST;
2321			strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2322			strlcpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
2323			strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2324			cpi->unit_number = cam_sim_unit(sim);
2325			cpi->bus_id = sc->sc_unit;
2326			cpi->protocol = PROTO_SCSI;
2327			cpi->protocol_version = SCSI_REV_2;
2328			cpi->transport = XPORT_USB;
2329			cpi->transport_version = 0;
2330
2331			if (sc == NULL) {
2332				cpi->base_transfer_speed = 0;
2333				cpi->max_lun = 0;
2334			} else {
2335				if (sc->sc_quirks & FLOPPY_SPEED) {
2336					cpi->base_transfer_speed =
2337					    UMASS_FLOPPY_TRANSFER_SPEED;
2338				} else {
2339					switch (usbd_get_speed(sc->sc_udev)) {
2340					case USB_SPEED_SUPER:
2341						cpi->base_transfer_speed =
2342						    UMASS_SUPER_TRANSFER_SPEED;
2343						cpi->maxio = maxphys;
2344						break;
2345					case USB_SPEED_HIGH:
2346						cpi->base_transfer_speed =
2347						    UMASS_HIGH_TRANSFER_SPEED;
2348						break;
2349					default:
2350						cpi->base_transfer_speed =
2351						    UMASS_FULL_TRANSFER_SPEED;
2352						break;
2353					}
2354				}
2355				cpi->max_lun = sc->sc_maxlun;
2356			}
2357
2358			cpi->ccb_h.status = CAM_REQ_CMP;
2359			xpt_done(ccb);
2360			break;
2361		}
2362	case XPT_RESET_DEV:
2363		{
2364			DPRINTF(sc, UDMASS_SCSI, "%d:%d:%jx:XPT_RESET_DEV:.\n",
2365			    cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2366			    (uintmax_t)ccb->ccb_h.target_lun);
2367
2368			umass_reset(sc);
2369
2370			ccb->ccb_h.status = CAM_REQ_CMP;
2371			xpt_done(ccb);
2372			break;
2373		}
2374	case XPT_GET_TRAN_SETTINGS:
2375		{
2376			struct ccb_trans_settings *cts = &ccb->cts;
2377
2378			DPRINTF(sc, UDMASS_SCSI, "%d:%d:%jx:XPT_GET_TRAN_SETTINGS:.\n",
2379			    cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2380			    (uintmax_t)ccb->ccb_h.target_lun);
2381
2382			cts->protocol = PROTO_SCSI;
2383			cts->protocol_version = SCSI_REV_2;
2384			cts->transport = XPORT_USB;
2385			cts->transport_version = 0;
2386			cts->xport_specific.valid = 0;
2387
2388			ccb->ccb_h.status = CAM_REQ_CMP;
2389			xpt_done(ccb);
2390			break;
2391		}
2392	case XPT_SET_TRAN_SETTINGS:
2393		{
2394			DPRINTF(sc, UDMASS_SCSI, "%d:%d:%jx:XPT_SET_TRAN_SETTINGS:.\n",
2395			    cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2396			    (uintmax_t)ccb->ccb_h.target_lun);
2397
2398			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2399			xpt_done(ccb);
2400			break;
2401		}
2402	case XPT_CALC_GEOMETRY:
2403		{
2404			cam_calc_geometry(&ccb->ccg, /* extended */ 1);
2405			xpt_done(ccb);
2406			break;
2407		}
2408	case XPT_NOOP:
2409		{
2410			DPRINTF(sc, UDMASS_SCSI, "%d:%d:%jx:XPT_NOOP:.\n",
2411			    sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2412			    (uintmax_t)ccb->ccb_h.target_lun);
2413
2414			ccb->ccb_h.status = CAM_REQ_CMP;
2415			xpt_done(ccb);
2416			break;
2417		}
2418	default:
2419		DPRINTF(sc, UDMASS_SCSI, "%d:%d:%jx:func_code 0x%04x: "
2420		    "Not implemented\n",
2421		    sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2422		    (uintmax_t)ccb->ccb_h.target_lun, ccb->ccb_h.func_code);
2423
2424		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2425		xpt_done(ccb);
2426		break;
2427	}
2428
2429done:
2430	return;
2431}
2432
2433static void
2434umass_cam_poll(struct cam_sim *sim)
2435{
2436	struct umass_softc *sc = (struct umass_softc *)sim->softc;
2437
2438	if (sc == NULL)
2439		return;
2440
2441	DPRINTF(sc, UDMASS_SCSI, "CAM poll\n");
2442
2443	usbd_transfer_poll(sc->sc_xfer, UMASS_T_MAX);
2444}
2445
2446/* umass_cam_cb
2447 *	finalise a completed CAM command
2448 */
2449
2450static void
2451umass_cam_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2452    uint8_t status)
2453{
2454	ccb->csio.resid = residue;
2455
2456	switch (status) {
2457	case STATUS_CMD_OK:
2458		ccb->ccb_h.status = CAM_REQ_CMP;
2459		if ((sc->sc_quirks & READ_CAPACITY_OFFBY1) &&
2460		    (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
2461		    (ccb->csio.cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
2462			struct scsi_read_capacity_data *rcap;
2463			uint32_t maxsector;
2464
2465			rcap = (void *)(ccb->csio.data_ptr);
2466			maxsector = scsi_4btoul(rcap->addr) - 1;
2467			scsi_ulto4b(maxsector, rcap->addr);
2468		}
2469		/*
2470		 * We have to add SVPD_UNIT_SERIAL_NUMBER to the list
2471		 * of pages supported by the device - otherwise, CAM
2472		 * will never ask us for the serial number if the
2473		 * device cannot handle that by itself.
2474		 */
2475		if (ccb->ccb_h.func_code == XPT_SCSI_IO &&
2476		    sc->sc_transfer.cmd_data[0] == INQUIRY &&
2477		    (sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
2478		    sc->sc_transfer.cmd_data[2] == SVPD_SUPPORTED_PAGE_LIST &&
2479		    (usb_get_serial(sc->sc_udev)[0] != '\0')) {
2480			struct ccb_scsiio *csio;
2481			struct scsi_vpd_supported_page_list *page_list;
2482
2483			csio = &ccb->csio;
2484			page_list = (struct scsi_vpd_supported_page_list *)csio->data_ptr;
2485			if (page_list->length + 1 < SVPD_SUPPORTED_PAGES_SIZE) {
2486				page_list->list[page_list->length] = SVPD_UNIT_SERIAL_NUMBER;
2487				page_list->length++;
2488			}
2489		}
2490		xpt_done(ccb);
2491		break;
2492
2493	case STATUS_CMD_UNKNOWN:
2494	case STATUS_CMD_FAILED:
2495
2496		/* fetch sense data */
2497
2498		/* the rest of the command was filled in at attach */
2499		sc->cam_scsi_sense.length = ccb->csio.sense_len;
2500
2501		DPRINTF(sc, UDMASS_SCSI, "Fetching %d bytes of "
2502		    "sense data\n", ccb->csio.sense_len);
2503
2504		if (umass_std_transform(sc, ccb, &sc->cam_scsi_sense.opcode,
2505		    sizeof(sc->cam_scsi_sense))) {
2506			if ((sc->sc_quirks & FORCE_SHORT_INQUIRY) &&
2507			    (sc->sc_transfer.cmd_data[0] == INQUIRY)) {
2508				ccb->csio.sense_len = SHORT_INQUIRY_LENGTH;
2509			}
2510			umass_command_start(sc, DIR_IN, &ccb->csio.sense_data.error_code,
2511			    ccb->csio.sense_len, ccb->ccb_h.timeout,
2512			    &umass_cam_sense_cb, ccb);
2513		}
2514		break;
2515
2516	default:
2517		/*
2518		 * The wire protocol failed and will hopefully have
2519		 * recovered. We return an error to CAM and let CAM
2520		 * retry the command if necessary.
2521		 */
2522		xpt_freeze_devq(ccb->ccb_h.path, 1);
2523		ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
2524		xpt_done(ccb);
2525		break;
2526	}
2527}
2528
2529/*
2530 * Finalise a completed autosense operation
2531 */
2532static void
2533umass_cam_sense_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2534    uint8_t status)
2535{
2536	uint8_t *cmd;
2537
2538	switch (status) {
2539	case STATUS_CMD_OK:
2540	case STATUS_CMD_UNKNOWN:
2541	case STATUS_CMD_FAILED: {
2542		int key, sense_len;
2543
2544		ccb->csio.sense_resid = residue;
2545		sense_len = ccb->csio.sense_len - ccb->csio.sense_resid;
2546		key = scsi_get_sense_key(&ccb->csio.sense_data, sense_len,
2547					 /*show_errors*/ 1);
2548
2549		if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2550			cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2551		} else {
2552			cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2553		}
2554
2555		/*
2556		 * Getting sense data always succeeds (apart from wire
2557		 * failures):
2558		 */
2559		if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
2560		    (cmd[0] == INQUIRY) &&
2561		    (key == SSD_KEY_UNIT_ATTENTION)) {
2562			/*
2563			 * Ignore unit attention errors in the case where
2564			 * the Unit Attention state is not cleared on
2565			 * REQUEST SENSE. They will appear again at the next
2566			 * command.
2567			 */
2568			ccb->ccb_h.status = CAM_REQ_CMP;
2569		} else if (key == SSD_KEY_NO_SENSE) {
2570			/*
2571			 * No problem after all (in the case of CBI without
2572			 * CCI)
2573			 */
2574			ccb->ccb_h.status = CAM_REQ_CMP;
2575		} else if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
2576			    (cmd[0] == READ_CAPACITY) &&
2577		    (key == SSD_KEY_UNIT_ATTENTION)) {
2578			/*
2579			 * Some devices do not clear the unit attention error
2580			 * on request sense. We insert a test unit ready
2581			 * command to make sure we clear the unit attention
2582			 * condition, then allow the retry to proceed as
2583			 * usual.
2584			 */
2585
2586			xpt_freeze_devq(ccb->ccb_h.path, 1);
2587			ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2588			    | CAM_AUTOSNS_VALID | CAM_DEV_QFRZN;
2589			ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2590
2591#if 0
2592			DELAY(300000);
2593#endif
2594			DPRINTF(sc, UDMASS_SCSI, "Doing a sneaky"
2595			    "TEST_UNIT_READY\n");
2596
2597			/* the rest of the command was filled in at attach */
2598
2599			if ((sc->sc_transform)(sc,
2600			    &sc->cam_scsi_test_unit_ready.opcode,
2601			    sizeof(sc->cam_scsi_test_unit_ready)) == 1) {
2602				umass_command_start(sc, DIR_NONE, NULL, 0,
2603				    ccb->ccb_h.timeout,
2604				    &umass_cam_quirk_cb, ccb);
2605				break;
2606			}
2607		} else {
2608			xpt_freeze_devq(ccb->ccb_h.path, 1);
2609			if (key >= 0) {
2610				ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2611				    | CAM_AUTOSNS_VALID | CAM_DEV_QFRZN;
2612				ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2613			} else
2614				ccb->ccb_h.status = CAM_AUTOSENSE_FAIL
2615				    | CAM_DEV_QFRZN;
2616		}
2617		xpt_done(ccb);
2618		break;
2619	}
2620	default:
2621		DPRINTF(sc, UDMASS_SCSI, "Autosense failed, "
2622		    "status %d\n", status);
2623		xpt_freeze_devq(ccb->ccb_h.path, 1);
2624		ccb->ccb_h.status = CAM_AUTOSENSE_FAIL | CAM_DEV_QFRZN;
2625		xpt_done(ccb);
2626	}
2627}
2628
2629/*
2630 * This completion code just handles the fact that we sent a test-unit-ready
2631 * after having previously failed a READ CAPACITY with CHECK_COND.  The CCB
2632 * status for CAM is already set earlier.
2633 */
2634static void
2635umass_cam_quirk_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2636    uint8_t status)
2637{
2638	DPRINTF(sc, UDMASS_SCSI, "Test unit ready "
2639	    "returned status %d\n", status);
2640
2641	xpt_done(ccb);
2642}
2643
2644/*
2645 * SCSI specific functions
2646 */
2647
2648static uint8_t
2649umass_scsi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2650    uint8_t cmd_len)
2651{
2652	if ((cmd_len == 0) ||
2653	    (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2654		DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2655		    "length: %d bytes\n", cmd_len);
2656		return (0);		/* failure */
2657	}
2658	sc->sc_transfer.cmd_len = cmd_len;
2659
2660	switch (cmd_ptr[0]) {
2661	case TEST_UNIT_READY:
2662		if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2663			DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
2664			    "to START_UNIT\n");
2665			memset(sc->sc_transfer.cmd_data, 0, cmd_len);
2666			sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2667			sc->sc_transfer.cmd_data[4] = SSS_START;
2668			return (1);
2669		}
2670		break;
2671
2672	case INQUIRY:
2673		/*
2674		 * some drives wedge when asked for full inquiry
2675		 * information.
2676		 */
2677		if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2678			memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2679			sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
2680			return (1);
2681		}
2682		break;
2683	}
2684
2685	memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2686	return (1);
2687}
2688
2689static uint8_t
2690umass_rbc_transform(struct umass_softc *sc, uint8_t *cmd_ptr, uint8_t cmd_len)
2691{
2692	if ((cmd_len == 0) ||
2693	    (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2694		DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2695		    "length: %d bytes\n", cmd_len);
2696		return (0);		/* failure */
2697	}
2698	switch (cmd_ptr[0]) {
2699		/* these commands are defined in RBC: */
2700	case READ_10:
2701	case READ_CAPACITY:
2702	case START_STOP_UNIT:
2703	case SYNCHRONIZE_CACHE:
2704	case WRITE_10:
2705	case VERIFY_10:
2706	case INQUIRY:
2707	case MODE_SELECT_10:
2708	case MODE_SENSE_10:
2709	case TEST_UNIT_READY:
2710	case WRITE_BUFFER:
2711		/*
2712		 * The following commands are not listed in my copy of the
2713		 * RBC specs. CAM however seems to want those, and at least
2714		 * the Sony DSC device appears to support those as well
2715		 */
2716	case REQUEST_SENSE:
2717	case PREVENT_ALLOW:
2718
2719		memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2720
2721		if ((sc->sc_quirks & RBC_PAD_TO_12) && (cmd_len < 12)) {
2722			memset(sc->sc_transfer.cmd_data + cmd_len,
2723			    0, 12 - cmd_len);
2724			cmd_len = 12;
2725		}
2726		sc->sc_transfer.cmd_len = cmd_len;
2727		return (1);		/* success */
2728
2729		/* All other commands are not legal in RBC */
2730	default:
2731		DPRINTF(sc, UDMASS_SCSI, "Unsupported RBC "
2732		    "command 0x%02x\n", cmd_ptr[0]);
2733		return (0);		/* failure */
2734	}
2735}
2736
2737static uint8_t
2738umass_ufi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2739    uint8_t cmd_len)
2740{
2741	if ((cmd_len == 0) ||
2742	    (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2743		DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2744		    "length: %d bytes\n", cmd_len);
2745		return (0);		/* failure */
2746	}
2747	/* An UFI command is always 12 bytes in length */
2748	sc->sc_transfer.cmd_len = UFI_COMMAND_LENGTH;
2749
2750	/* Zero the command data */
2751	memset(sc->sc_transfer.cmd_data, 0, UFI_COMMAND_LENGTH);
2752
2753	switch (cmd_ptr[0]) {
2754		/*
2755		 * Commands of which the format has been verified. They
2756		 * should work. Copy the command into the (zeroed out)
2757		 * destination buffer.
2758		 */
2759	case TEST_UNIT_READY:
2760		if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2761			/*
2762			 * Some devices do not support this command. Start
2763			 * Stop Unit should give the same results
2764			 */
2765			DPRINTF(sc, UDMASS_UFI, "Converted TEST_UNIT_READY "
2766			    "to START_UNIT\n");
2767
2768			sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2769			sc->sc_transfer.cmd_data[4] = SSS_START;
2770			return (1);
2771		}
2772		break;
2773
2774	case REZERO_UNIT:
2775	case REQUEST_SENSE:
2776	case FORMAT_UNIT:
2777	case INQUIRY:
2778	case START_STOP_UNIT:
2779	case SEND_DIAGNOSTIC:
2780	case PREVENT_ALLOW:
2781	case READ_CAPACITY:
2782	case READ_10:
2783	case WRITE_10:
2784	case POSITION_TO_ELEMENT:	/* SEEK_10 */
2785	case WRITE_AND_VERIFY:
2786	case VERIFY:
2787	case MODE_SELECT_10:
2788	case MODE_SENSE_10:
2789	case READ_12:
2790	case WRITE_12:
2791	case READ_FORMAT_CAPACITIES:
2792		break;
2793
2794		/*
2795		 * SYNCHRONIZE_CACHE isn't supported by UFI, nor should it be
2796		 * required for UFI devices, so it is appropriate to fake
2797		 * success.
2798		 */
2799	case SYNCHRONIZE_CACHE:
2800		return (2);
2801
2802	default:
2803		DPRINTF(sc, UDMASS_SCSI, "Unsupported UFI "
2804		    "command 0x%02x\n", cmd_ptr[0]);
2805		return (0);		/* failure */
2806	}
2807
2808	memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2809	return (1);			/* success */
2810}
2811
2812/*
2813 * 8070i (ATAPI) specific functions
2814 */
2815static uint8_t
2816umass_atapi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2817    uint8_t cmd_len)
2818{
2819	if ((cmd_len == 0) ||
2820	    (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2821		DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2822		    "length: %d bytes\n", cmd_len);
2823		return (0);		/* failure */
2824	}
2825	/* An ATAPI command is always 12 bytes in length. */
2826	sc->sc_transfer.cmd_len = ATAPI_COMMAND_LENGTH;
2827
2828	/* Zero the command data */
2829	memset(sc->sc_transfer.cmd_data, 0, ATAPI_COMMAND_LENGTH);
2830
2831	switch (cmd_ptr[0]) {
2832		/*
2833		 * Commands of which the format has been verified. They
2834		 * should work. Copy the command into the destination
2835		 * buffer.
2836		 */
2837	case INQUIRY:
2838		/*
2839		 * some drives wedge when asked for full inquiry
2840		 * information.
2841		 */
2842		if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2843			memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2844
2845			sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
2846			return (1);
2847		}
2848		break;
2849
2850	case TEST_UNIT_READY:
2851		if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2852			DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
2853			    "to START_UNIT\n");
2854			sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2855			sc->sc_transfer.cmd_data[4] = SSS_START;
2856			return (1);
2857		}
2858		break;
2859
2860	case REZERO_UNIT:
2861	case REQUEST_SENSE:
2862	case START_STOP_UNIT:
2863	case SEND_DIAGNOSTIC:
2864	case PREVENT_ALLOW:
2865	case READ_CAPACITY:
2866	case READ_10:
2867	case WRITE_10:
2868	case POSITION_TO_ELEMENT:	/* SEEK_10 */
2869	case SYNCHRONIZE_CACHE:
2870	case MODE_SELECT_10:
2871	case MODE_SENSE_10:
2872	case READ_BUFFER:
2873	case 0x42:			/* READ_SUBCHANNEL */
2874	case 0x43:			/* READ_TOC */
2875	case 0x44:			/* READ_HEADER */
2876	case 0x47:			/* PLAY_MSF (Play Minute/Second/Frame) */
2877	case 0x48:			/* PLAY_TRACK */
2878	case 0x49:			/* PLAY_TRACK_REL */
2879	case 0x4b:			/* PAUSE */
2880	case 0x51:			/* READ_DISK_INFO */
2881	case 0x52:			/* READ_TRACK_INFO */
2882	case 0x54:			/* SEND_OPC */
2883	case 0x59:			/* READ_MASTER_CUE */
2884	case 0x5b:			/* CLOSE_TR_SESSION */
2885	case 0x5c:			/* READ_BUFFER_CAP */
2886	case 0x5d:			/* SEND_CUE_SHEET */
2887	case 0xa1:			/* BLANK */
2888	case 0xa5:			/* PLAY_12 */
2889	case 0xa6:			/* EXCHANGE_MEDIUM */
2890	case 0xad:			/* READ_DVD_STRUCTURE */
2891	case 0xbb:			/* SET_CD_SPEED */
2892	case 0xe5:			/* READ_TRACK_INFO_PHILIPS */
2893		break;
2894
2895	case READ_12:
2896	case WRITE_12:
2897	default:
2898		DPRINTF(sc, UDMASS_SCSI, "Unsupported ATAPI "
2899		    "command 0x%02x - trying anyway\n",
2900		    cmd_ptr[0]);
2901		break;
2902	}
2903
2904	memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2905	return (1);			/* success */
2906}
2907
2908static uint8_t
2909umass_no_transform(struct umass_softc *sc, uint8_t *cmd,
2910    uint8_t cmdlen)
2911{
2912	return (0);			/* failure */
2913}
2914
2915static uint8_t
2916umass_std_transform(struct umass_softc *sc, union ccb *ccb,
2917    uint8_t *cmd, uint8_t cmdlen)
2918{
2919	uint8_t retval;
2920
2921	retval = (sc->sc_transform) (sc, cmd, cmdlen);
2922
2923	if (retval == 2) {
2924		ccb->ccb_h.status = CAM_REQ_CMP;
2925		xpt_done(ccb);
2926		return (0);
2927	} else if (retval == 0) {
2928		xpt_freeze_devq(ccb->ccb_h.path, 1);
2929		ccb->ccb_h.status = CAM_REQ_INVALID | CAM_DEV_QFRZN;
2930		xpt_done(ccb);
2931		return (0);
2932	}
2933	/* Command should be executed */
2934	return (1);
2935}
2936
2937#ifdef USB_DEBUG
2938static void
2939umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
2940{
2941	uint8_t *c = cbw->CBWCDB;
2942
2943	uint32_t dlen = UGETDW(cbw->dCBWDataTransferLength);
2944	uint32_t tag = UGETDW(cbw->dCBWTag);
2945
2946	uint8_t clen = cbw->bCDBLength;
2947	uint8_t flags = cbw->bCBWFlags;
2948	uint8_t lun = cbw->bCBWLUN;
2949
2950	DPRINTF(sc, UDMASS_BBB, "CBW %d: cmd = %db "
2951	    "(0x%02x%02x%02x%02x%02x%02x%s), "
2952	    "data = %db, lun = %d, dir = %s\n",
2953	    tag, clen,
2954	    c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6 ? "..." : ""),
2955	    dlen, lun, (flags == CBWFLAGS_IN ? "in" :
2956	    (flags == CBWFLAGS_OUT ? "out" : "<invalid>")));
2957}
2958
2959static void
2960umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
2961{
2962	uint32_t sig = UGETDW(csw->dCSWSignature);
2963	uint32_t tag = UGETDW(csw->dCSWTag);
2964	uint32_t res = UGETDW(csw->dCSWDataResidue);
2965	uint8_t status = csw->bCSWStatus;
2966
2967	DPRINTF(sc, UDMASS_BBB, "CSW %d: sig = 0x%08x (%s), tag = 0x%08x, "
2968	    "res = %d, status = 0x%02x (%s)\n",
2969	    tag, sig, (sig == CSWSIGNATURE ? "valid" : "invalid"),
2970	    tag, res,
2971	    status, (status == CSWSTATUS_GOOD ? "good" :
2972	    (status == CSWSTATUS_FAILED ? "failed" :
2973	    (status == CSWSTATUS_PHASE ? "phase" : "<invalid>"))));
2974}
2975
2976static void
2977umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, uint8_t cmdlen)
2978{
2979	uint8_t *c = cmd;
2980	uint8_t dir = sc->sc_transfer.dir;
2981
2982	DPRINTF(sc, UDMASS_BBB, "cmd = %db "
2983	    "(0x%02x%02x%02x%02x%02x%02x%s), "
2984	    "data = %db, dir = %s\n",
2985	    cmdlen,
2986	    c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6 ? "..." : ""),
2987	    sc->sc_transfer.data_len,
2988	    (dir == DIR_IN ? "in" :
2989	    (dir == DIR_OUT ? "out" :
2990	    (dir == DIR_NONE ? "no data phase" : "<invalid>"))));
2991}
2992
2993static void
2994umass_dump_buffer(struct umass_softc *sc, uint8_t *buffer, uint32_t buflen,
2995    uint32_t printlen)
2996{
2997	uint32_t i, j;
2998	char s1[40];
2999	char s2[40];
3000	char s3[5];
3001
3002	s1[0] = '\0';
3003	s3[0] = '\0';
3004
3005	sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3006	for (i = 0; (i < buflen) && (i < printlen); i++) {
3007		j = i % 16;
3008		if (j == 0 && i != 0) {
3009			DPRINTF(sc, UDMASS_GEN, "0x %s%s\n",
3010			    s1, s2);
3011			s2[0] = '\0';
3012		}
3013		sprintf(&s1[j * 2], "%02x", buffer[i] & 0xff);
3014	}
3015	if (buflen > printlen)
3016		sprintf(s3, " ...");
3017	DPRINTF(sc, UDMASS_GEN, "0x %s%s%s\n",
3018	    s1, s2, s3);
3019}
3020
3021#endif
3022