xref: /freebsd/sys/dev/usb/storage/ustorage_fs.c (revision 45dd2eaac379e5576f745380260470204c49beac)
1 /* $FreeBSD$ */
2 /*-
3  * SPDX-License-Identifier: BSD-3-Clause
4  *
5  * Copyright (C) 2003-2005 Alan Stern
6  * Copyright (C) 2008 Hans Petter Selasky
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions, and the following disclaimer,
14  *    without modification.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The names of the above-listed copyright holders may not be used
19  *    to endorse or promote products derived from this software without
20  *    specific prior written permission.
21  *
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * NOTE: Much of the SCSI statemachine handling code derives from the
38  * Linux USB gadget stack.
39  */
40 
41 #ifdef USB_GLOBAL_INCLUDE_FILE
42 #include USB_GLOBAL_INCLUDE_FILE
43 #else
44 #include <sys/stdint.h>
45 #include <sys/stddef.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/bus.h>
52 #include <sys/module.h>
53 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/sx.h>
58 #include <sys/unistd.h>
59 #include <sys/callout.h>
60 #include <sys/malloc.h>
61 #include <sys/priv.h>
62 
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
65 #include "usbdevs.h"
66 #include "usb_if.h"
67 
68 #define	USB_DEBUG_VAR ustorage_fs_debug
69 #include <dev/usb/usb_debug.h>
70 #endif			/* USB_GLOBAL_INCLUDE_FILE */
71 
72 #ifdef USB_DEBUG
73 static int ustorage_fs_debug = 0;
74 
75 SYSCTL_NODE(_hw_usb, OID_AUTO, ustorage_fs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
76     "USB ustorage_fs");
77 SYSCTL_INT(_hw_usb_ustorage_fs, OID_AUTO, debug, CTLFLAG_RWTUN,
78     &ustorage_fs_debug, 0, "ustorage_fs debug level");
79 #endif
80 
81 /* Define some limits */
82 
83 #ifndef USTORAGE_FS_BULK_SIZE
84 #define	USTORAGE_FS_BULK_SIZE	(1U << 17)	/* bytes */
85 #endif
86 
87 #ifndef	USTORAGE_FS_MAX_LUN
88 #define	USTORAGE_FS_MAX_LUN	8	/* units */
89 #endif
90 
91 #ifndef USTORAGE_QDATA_MAX
92 #define	USTORAGE_QDATA_MAX	40	/* bytes */
93 #endif
94 
95 /*
96  * The SCSI ID string must be exactly 28 characters long
97  * exluding the terminating zero.
98  */
99 #ifndef USTORAGE_FS_ID_STRING
100 #define	USTORAGE_FS_ID_STRING \
101 	"FreeBSD " /* 8 */ \
102 	"File-Stor Gadget" /* 16 */ \
103 	"0101" /* 4 */
104 #endif
105 
106 /*
107  * The following macro defines the number of
108  * sectors to be allocated for the RAM disk:
109  */
110 #ifndef USTORAGE_FS_RAM_SECT
111 #define	USTORAGE_FS_RAM_SECT (1UL << 13)
112 #endif
113 
114 static uint8_t *ustorage_fs_ramdisk;
115 
116 /* USB transfer definitions */
117 
118 #define	USTORAGE_FS_T_BBB_COMMAND     0
119 #define	USTORAGE_FS_T_BBB_DATA_DUMP   1
120 #define	USTORAGE_FS_T_BBB_DATA_READ   2
121 #define	USTORAGE_FS_T_BBB_DATA_WRITE  3
122 #define	USTORAGE_FS_T_BBB_STATUS      4
123 #define	USTORAGE_FS_T_BBB_MAX         5
124 
125 /* USB data stage direction */
126 
127 #define	DIR_NONE	0
128 #define	DIR_READ	1
129 #define	DIR_WRITE	2
130 
131 /* USB interface specific control request */
132 
133 #define	UR_BBB_RESET		0xff	/* Bulk-Only reset */
134 #define	UR_BBB_GET_MAX_LUN	0xfe	/* Get maximum lun */
135 
136 /* Command Block Wrapper */
137 typedef struct {
138 	uDWord	dCBWSignature;
139 #define	CBWSIGNATURE	0x43425355
140 	uDWord	dCBWTag;
141 	uDWord	dCBWDataTransferLength;
142 	uByte	bCBWFlags;
143 #define	CBWFLAGS_OUT	0x00
144 #define	CBWFLAGS_IN	0x80
145 	uByte	bCBWLUN;
146 	uByte	bCDBLength;
147 #define	CBWCDBLENGTH	16
148 	uByte	CBWCDB[CBWCDBLENGTH];
149 } __packed ustorage_fs_bbb_cbw_t;
150 
151 #define	USTORAGE_FS_BBB_CBW_SIZE	31
152 
153 /* Command Status Wrapper */
154 typedef struct {
155 	uDWord	dCSWSignature;
156 #define	CSWSIGNATURE	0x53425355
157 	uDWord	dCSWTag;
158 	uDWord	dCSWDataResidue;
159 	uByte	bCSWStatus;
160 #define	CSWSTATUS_GOOD	0x0
161 #define	CSWSTATUS_FAILED	0x1
162 #define	CSWSTATUS_PHASE	0x2
163 } __packed ustorage_fs_bbb_csw_t;
164 
165 #define	USTORAGE_FS_BBB_CSW_SIZE	13
166 
167 struct ustorage_fs_lun {
168 	uint8_t	*memory_image;
169 
170 	uint32_t num_sectors;
171 	uint32_t sense_data;
172 	uint32_t sense_data_info;
173 	uint32_t unit_attention_data;
174 
175 	uint8_t	read_only:1;
176 	uint8_t	prevent_medium_removal:1;
177 	uint8_t	info_valid:1;
178 	uint8_t	removable:1;
179 };
180 
181 struct ustorage_fs_softc {
182 	ustorage_fs_bbb_cbw_t *sc_cbw;	/* Command Wrapper Block */
183 	ustorage_fs_bbb_csw_t *sc_csw;	/* Command Status Block */
184 	void *sc_dma_ptr;		/* Main data buffer */
185 
186 	struct mtx sc_mtx;
187 
188 	struct ustorage_fs_lun sc_lun[USTORAGE_FS_MAX_LUN];
189 
190 	struct {
191 		uint8_t *data_ptr;
192 		struct ustorage_fs_lun *currlun;
193 
194 		uint32_t data_rem;	/* bytes, as reported by the command
195 					 * block wrapper */
196 		uint32_t offset;	/* bytes */
197 
198 		uint8_t	cbw_dir;
199 		uint8_t	cmd_dir;
200 		uint8_t	lun;
201 		uint8_t	cmd_len;
202 		uint8_t	data_short:1;
203 		uint8_t	data_error:1;
204 	}	sc_transfer;
205 
206 	device_t sc_dev;
207 	struct usb_device *sc_udev;
208 	struct usb_xfer *sc_xfer[USTORAGE_FS_T_BBB_MAX];
209 
210 	uint8_t	sc_iface_no;		/* interface number */
211 	uint8_t	sc_last_lun;
212 	uint8_t	sc_last_xfer_index;
213 	uint8_t	sc_qdata[USTORAGE_QDATA_MAX];
214 };
215 
216 /* prototypes */
217 
218 static device_probe_t ustorage_fs_probe;
219 static device_attach_t ustorage_fs_attach;
220 static device_detach_t ustorage_fs_detach;
221 static device_suspend_t ustorage_fs_suspend;
222 static device_resume_t ustorage_fs_resume;
223 static usb_handle_request_t ustorage_fs_handle_request;
224 
225 static usb_callback_t ustorage_fs_t_bbb_command_callback;
226 static usb_callback_t ustorage_fs_t_bbb_data_dump_callback;
227 static usb_callback_t ustorage_fs_t_bbb_data_read_callback;
228 static usb_callback_t ustorage_fs_t_bbb_data_write_callback;
229 static usb_callback_t ustorage_fs_t_bbb_status_callback;
230 
231 static void ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index);
232 static void ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc);
233 
234 static uint8_t ustorage_fs_verify(struct ustorage_fs_softc *sc);
235 static uint8_t ustorage_fs_inquiry(struct ustorage_fs_softc *sc);
236 static uint8_t ustorage_fs_request_sense(struct ustorage_fs_softc *sc);
237 static uint8_t ustorage_fs_read_capacity(struct ustorage_fs_softc *sc);
238 static uint8_t ustorage_fs_mode_sense(struct ustorage_fs_softc *sc);
239 static uint8_t ustorage_fs_start_stop(struct ustorage_fs_softc *sc);
240 static uint8_t ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc);
241 static uint8_t ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc);
242 static uint8_t ustorage_fs_mode_select(struct ustorage_fs_softc *sc);
243 static uint8_t ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask);
244 static uint8_t ustorage_fs_read(struct ustorage_fs_softc *sc);
245 static uint8_t ustorage_fs_write(struct ustorage_fs_softc *sc);
246 static uint8_t ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t cmd_size, uint16_t mask, uint8_t needs_medium);
247 static uint8_t ustorage_fs_do_cmd(struct ustorage_fs_softc *sc);
248 
249 static device_method_t ustorage_fs_methods[] = {
250 	/* USB interface */
251 	DEVMETHOD(usb_handle_request, ustorage_fs_handle_request),
252 
253 	/* Device interface */
254 	DEVMETHOD(device_probe, ustorage_fs_probe),
255 	DEVMETHOD(device_attach, ustorage_fs_attach),
256 	DEVMETHOD(device_detach, ustorage_fs_detach),
257 	DEVMETHOD(device_suspend, ustorage_fs_suspend),
258 	DEVMETHOD(device_resume, ustorage_fs_resume),
259 
260 	DEVMETHOD_END
261 };
262 
263 static driver_t ustorage_fs_driver = {
264 	.name = "ustorage_fs",
265 	.methods = ustorage_fs_methods,
266 	.size = sizeof(struct ustorage_fs_softc),
267 };
268 
269 static devclass_t ustorage_fs_devclass;
270 
271 DRIVER_MODULE(ustorage_fs, uhub, ustorage_fs_driver, ustorage_fs_devclass, NULL, 0);
272 MODULE_VERSION(ustorage_fs, 0);
273 MODULE_DEPEND(ustorage_fs, usb, 1, 1, 1);
274 
275 static struct usb_config ustorage_fs_bbb_config[USTORAGE_FS_T_BBB_MAX] = {
276 	[USTORAGE_FS_T_BBB_COMMAND] = {
277 		.type = UE_BULK,
278 		.endpoint = UE_ADDR_ANY,
279 		.direction = UE_DIR_OUT,
280 		.bufsize = sizeof(ustorage_fs_bbb_cbw_t),
281 		.callback = &ustorage_fs_t_bbb_command_callback,
282 		.usb_mode = USB_MODE_DEVICE,
283 	},
284 
285 	[USTORAGE_FS_T_BBB_DATA_DUMP] = {
286 		.type = UE_BULK,
287 		.endpoint = UE_ADDR_ANY,
288 		.direction = UE_DIR_OUT,
289 		.bufsize = 0,	/* use wMaxPacketSize */
290 		.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
291 		.callback = &ustorage_fs_t_bbb_data_dump_callback,
292 		.usb_mode = USB_MODE_DEVICE,
293 	},
294 
295 	[USTORAGE_FS_T_BBB_DATA_READ] = {
296 		.type = UE_BULK,
297 		.endpoint = UE_ADDR_ANY,
298 		.direction = UE_DIR_OUT,
299 		.bufsize = USTORAGE_FS_BULK_SIZE,
300 		.flags = {.proxy_buffer = 1,.short_xfer_ok = 1},
301 		.callback = &ustorage_fs_t_bbb_data_read_callback,
302 		.usb_mode = USB_MODE_DEVICE,
303 	},
304 
305 	[USTORAGE_FS_T_BBB_DATA_WRITE] = {
306 		.type = UE_BULK,
307 		.endpoint = UE_ADDR_ANY,
308 		.direction = UE_DIR_IN,
309 		.bufsize = USTORAGE_FS_BULK_SIZE,
310 		.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer = 1},
311 		.callback = &ustorage_fs_t_bbb_data_write_callback,
312 		.usb_mode = USB_MODE_DEVICE,
313 	},
314 
315 	[USTORAGE_FS_T_BBB_STATUS] = {
316 		.type = UE_BULK,
317 		.endpoint = UE_ADDR_ANY,
318 		.direction = UE_DIR_IN,
319 		.bufsize = sizeof(ustorage_fs_bbb_csw_t),
320 		.flags = {.short_xfer_ok = 1},
321 		.callback = &ustorage_fs_t_bbb_status_callback,
322 		.usb_mode = USB_MODE_DEVICE,
323 	},
324 };
325 
326 /*
327  * USB device probe/attach/detach
328  */
329 
330 static int
331 ustorage_fs_probe(device_t dev)
332 {
333 	struct usb_attach_arg *uaa = device_get_ivars(dev);
334 	struct usb_interface_descriptor *id;
335 
336 	if (uaa->usb_mode != USB_MODE_DEVICE) {
337 		return (ENXIO);
338 	}
339 	/* Check for a standards compliant device */
340 	id = usbd_get_interface_descriptor(uaa->iface);
341 	if ((id == NULL) ||
342 	    (id->bInterfaceClass != UICLASS_MASS) ||
343 	    (id->bInterfaceSubClass != UISUBCLASS_SCSI) ||
344 	    (id->bInterfaceProtocol != UIPROTO_MASS_BBB)) {
345 		return (ENXIO);
346 	}
347 	return (BUS_PROBE_GENERIC);
348 }
349 
350 static int
351 ustorage_fs_attach(device_t dev)
352 {
353 	struct ustorage_fs_softc *sc = device_get_softc(dev);
354 	struct usb_attach_arg *uaa = device_get_ivars(dev);
355 	struct usb_interface_descriptor *id;
356 	int err;
357 	int unit;
358 
359 	/*
360 	 * NOTE: the softc struct is cleared in device_set_driver.
361 	 * We can safely call ustorage_fs_detach without specifically
362 	 * initializing the struct.
363 	 */
364 
365 	sc->sc_dev = dev;
366 	sc->sc_udev = uaa->device;
367 	unit = device_get_unit(dev);
368 
369 	/* enable power saving mode */
370 	usbd_set_power_mode(uaa->device, USB_POWER_MODE_SAVE);
371 
372 	if (unit == 0) {
373 		if (ustorage_fs_ramdisk == NULL) {
374 			/*
375 			 * allocate a memory image for our ramdisk until
376 			 * further
377 			 */
378 			ustorage_fs_ramdisk =
379 			    malloc(USTORAGE_FS_RAM_SECT << 9, M_USB,
380 			    M_ZERO | M_WAITOK);
381 		}
382 		sc->sc_lun[0].memory_image = ustorage_fs_ramdisk;
383 		sc->sc_lun[0].num_sectors = USTORAGE_FS_RAM_SECT;
384 		sc->sc_lun[0].removable = 1;
385 	}
386 
387 	device_set_usb_desc(dev);
388 
389 	mtx_init(&sc->sc_mtx, "USTORAGE_FS lock",
390 	    NULL, (MTX_DEF | MTX_RECURSE));
391 
392 	/* get interface index */
393 
394 	id = usbd_get_interface_descriptor(uaa->iface);
395 	if (id == NULL) {
396 		device_printf(dev, "failed to get "
397 		    "interface number\n");
398 		goto detach;
399 	}
400 	sc->sc_iface_no = id->bInterfaceNumber;
401 
402 	err = usbd_transfer_setup(uaa->device,
403 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ustorage_fs_bbb_config,
404 	    USTORAGE_FS_T_BBB_MAX, sc, &sc->sc_mtx);
405 	if (err) {
406 		device_printf(dev, "could not setup required "
407 		    "transfers, %s\n", usbd_errstr(err));
408 		goto detach;
409 	}
410 
411 	sc->sc_cbw = usbd_xfer_get_frame_buffer(sc->sc_xfer[
412 	    USTORAGE_FS_T_BBB_COMMAND], 0);
413 	sc->sc_csw = usbd_xfer_get_frame_buffer(sc->sc_xfer[
414 	    USTORAGE_FS_T_BBB_STATUS], 0);
415  	sc->sc_dma_ptr = usbd_xfer_get_frame_buffer(sc->sc_xfer[
416 	    USTORAGE_FS_T_BBB_DATA_READ], 0);
417 
418 	/* start Mass Storage State Machine */
419 
420 	mtx_lock(&sc->sc_mtx);
421 	ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
422 	mtx_unlock(&sc->sc_mtx);
423 
424 	return (0);			/* success */
425 
426 detach:
427 	ustorage_fs_detach(dev);
428 	return (ENXIO);			/* failure */
429 }
430 
431 static int
432 ustorage_fs_detach(device_t dev)
433 {
434 	struct ustorage_fs_softc *sc = device_get_softc(dev);
435 
436 	/* teardown our statemachine */
437 
438 	usbd_transfer_unsetup(sc->sc_xfer, USTORAGE_FS_T_BBB_MAX);
439 
440 	mtx_destroy(&sc->sc_mtx);
441 
442 	return (0);			/* success */
443 }
444 
445 static int
446 ustorage_fs_suspend(device_t dev)
447 {
448 	device_printf(dev, "suspending\n");
449 	return (0);			/* success */
450 }
451 
452 static int
453 ustorage_fs_resume(device_t dev)
454 {
455 	device_printf(dev, "resuming\n");
456 	return (0);			/* success */
457 }
458 
459 /*
460  * Generic functions to handle transfers
461  */
462 
463 static void
464 ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index)
465 {
466 	if (sc->sc_xfer[xfer_index]) {
467 		sc->sc_last_xfer_index = xfer_index;
468 		usbd_transfer_start(sc->sc_xfer[xfer_index]);
469 	}
470 }
471 
472 static void
473 ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc)
474 {
475 	usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
476 	mtx_unlock(&sc->sc_mtx);
477 	usbd_transfer_drain(sc->sc_xfer[sc->sc_last_xfer_index]);
478 	mtx_lock(&sc->sc_mtx);
479 }
480 
481 static int
482 ustorage_fs_handle_request(device_t dev,
483     const void *preq, void **pptr, uint16_t *plen,
484     uint16_t offset, uint8_t *pstate)
485 {
486 	struct ustorage_fs_softc *sc = device_get_softc(dev);
487 	const struct usb_device_request *req = preq;
488 	uint8_t is_complete = *pstate;
489 
490 	if (!is_complete) {
491 		if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
492 		    (req->bRequest == UR_BBB_RESET)) {
493 			*plen = 0;
494 			mtx_lock(&sc->sc_mtx);
495 			ustorage_fs_transfer_stop(sc);
496 			sc->sc_transfer.data_error = 1;
497 			ustorage_fs_transfer_start(sc,
498 			    USTORAGE_FS_T_BBB_COMMAND);
499 			mtx_unlock(&sc->sc_mtx);
500 			return (0);
501 		} else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
502 			   (req->bRequest == UR_BBB_GET_MAX_LUN)) {
503 			if (offset == 0) {
504 				*plen = 1;
505 				*pptr = &sc->sc_last_lun;
506 			} else {
507 				*plen = 0;
508 			}
509 			return (0);
510 		}
511 	}
512 	return (ENXIO);			/* use builtin handler */
513 }
514 
515 static void
516 ustorage_fs_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
517 {
518 	struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
519 	uint32_t tag;
520 	uint8_t err = 0;
521 
522 	DPRINTF("\n");
523 
524 	switch (USB_GET_STATE(xfer)) {
525 	case USB_ST_TRANSFERRED:
526 
527 		tag = UGETDW(sc->sc_cbw->dCBWSignature);
528 
529 		if (tag != CBWSIGNATURE) {
530 			/* do nothing */
531 			DPRINTF("invalid signature 0x%08x\n", tag);
532 			break;
533 		}
534 		tag = UGETDW(sc->sc_cbw->dCBWTag);
535 
536 		/* echo back tag */
537 		USETDW(sc->sc_csw->dCSWTag, tag);
538 
539 		/* reset status */
540 		sc->sc_csw->bCSWStatus = 0;
541 
542 		/* reset data offset, data length and data remainder */
543 		sc->sc_transfer.offset = 0;
544 		sc->sc_transfer.data_rem =
545 		    UGETDW(sc->sc_cbw->dCBWDataTransferLength);
546 
547 		/* reset data flags */
548 		sc->sc_transfer.data_short = 0;
549 
550 		/* extract LUN */
551 		sc->sc_transfer.lun = sc->sc_cbw->bCBWLUN;
552 
553 		if (sc->sc_transfer.data_rem == 0) {
554 			sc->sc_transfer.cbw_dir = DIR_NONE;
555 		} else {
556 			if (sc->sc_cbw->bCBWFlags & CBWFLAGS_IN) {
557 				sc->sc_transfer.cbw_dir = DIR_WRITE;
558 			} else {
559 				sc->sc_transfer.cbw_dir = DIR_READ;
560 			}
561 		}
562 
563 		sc->sc_transfer.cmd_len = sc->sc_cbw->bCDBLength;
564 		if ((sc->sc_transfer.cmd_len > sizeof(sc->sc_cbw->CBWCDB)) ||
565 		    (sc->sc_transfer.cmd_len == 0)) {
566 			/* just halt - this is invalid */
567 			DPRINTF("invalid command length %d bytes\n",
568 			    sc->sc_transfer.cmd_len);
569 			break;
570 		}
571 
572 		err = ustorage_fs_do_cmd(sc);
573 		if (err) {
574 			/* got an error */
575 			DPRINTF("command failed\n");
576 			break;
577 		}
578 		if ((sc->sc_transfer.data_rem > 0) &&
579 		    (sc->sc_transfer.cbw_dir != sc->sc_transfer.cmd_dir)) {
580 			/* contradicting data transfer direction */
581 			err = 1;
582 			DPRINTF("data direction mismatch\n");
583 			break;
584 		}
585 		switch (sc->sc_transfer.cbw_dir) {
586 		case DIR_READ:
587 			ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_READ);
588 			break;
589 		case DIR_WRITE:
590 			ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_WRITE);
591 			break;
592 		default:
593 			ustorage_fs_transfer_start(sc,
594 			    USTORAGE_FS_T_BBB_STATUS);
595 			break;
596 		}
597 		break;
598 
599 	case USB_ST_SETUP:
600 tr_setup:
601 		if (sc->sc_transfer.data_error) {
602 			sc->sc_transfer.data_error = 0;
603 			usbd_xfer_set_stall(xfer);
604 			DPRINTF("stall pipe\n");
605 		}
606 		usbd_xfer_set_frame_len(xfer, 0,
607 		    sizeof(ustorage_fs_bbb_cbw_t));
608 		usbd_transfer_submit(xfer);
609 		break;
610 
611 	default:			/* Error */
612 		DPRINTF("error\n");
613 		if (error == USB_ERR_CANCELLED) {
614 			break;
615 		}
616 		/* If the pipe is already stalled, don't do another stall */
617 		if (!usbd_xfer_is_stalled(xfer))
618 			sc->sc_transfer.data_error = 1;
619 
620 		/* try again */
621 		goto tr_setup;
622 	}
623 	if (err) {
624 		if (sc->sc_csw->bCSWStatus == 0) {
625 			/* set some default error code */
626 			sc->sc_csw->bCSWStatus = CSWSTATUS_FAILED;
627 		}
628 		if (sc->sc_transfer.cbw_dir == DIR_READ) {
629 			/* dump all data */
630 			ustorage_fs_transfer_start(sc,
631 			    USTORAGE_FS_T_BBB_DATA_DUMP);
632 			return;
633 		}
634 		if (sc->sc_transfer.cbw_dir == DIR_WRITE) {
635 			/* need to stall before status */
636 			sc->sc_transfer.data_error = 1;
637 		}
638 		ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_STATUS);
639 	}
640 }
641 
642 static void
643 ustorage_fs_t_bbb_data_dump_callback(struct usb_xfer *xfer, usb_error_t error)
644 {
645 	struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
646 	uint32_t max_bulk = usbd_xfer_max_len(xfer);
647 	int actlen, sumlen;
648 
649 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
650 
651 	DPRINTF("\n");
652 
653 	switch (USB_GET_STATE(xfer)) {
654 	case USB_ST_TRANSFERRED:
655 		sc->sc_transfer.data_rem -= actlen;
656 		sc->sc_transfer.offset += actlen;
657 
658 		if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
659 			/* short transfer or end of data */
660 			ustorage_fs_transfer_start(sc,
661 			    USTORAGE_FS_T_BBB_STATUS);
662 			break;
663 		}
664 		/* Fallthrough */
665 
666 	case USB_ST_SETUP:
667 tr_setup:
668 		if (max_bulk > sc->sc_transfer.data_rem) {
669 			max_bulk = sc->sc_transfer.data_rem;
670 		}
671 		if (sc->sc_transfer.data_error) {
672 			sc->sc_transfer.data_error = 0;
673 			usbd_xfer_set_stall(xfer);
674 		}
675 		usbd_xfer_set_frame_len(xfer, 0, max_bulk);
676 		usbd_transfer_submit(xfer);
677 		break;
678 
679 	default:			/* Error */
680 		if (error == USB_ERR_CANCELLED) {
681 			break;
682 		}
683 		/*
684 		 * If the pipe is already stalled, don't do another stall:
685 		 */
686 		if (!usbd_xfer_is_stalled(xfer))
687 			sc->sc_transfer.data_error = 1;
688 
689 		/* try again */
690 		goto tr_setup;
691 	}
692 }
693 
694 static void
695 ustorage_fs_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
696 {
697 	struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
698 	uint32_t max_bulk = usbd_xfer_max_len(xfer);
699 	int actlen, sumlen;
700 
701 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
702 
703 	DPRINTF("\n");
704 
705 	switch (USB_GET_STATE(xfer)) {
706 	case USB_ST_TRANSFERRED:
707 		/* XXX copy data from DMA buffer */
708 		memcpy(sc->sc_transfer.data_ptr, sc->sc_dma_ptr, actlen);
709 
710 		sc->sc_transfer.data_rem -= actlen;
711 		sc->sc_transfer.data_ptr += actlen;
712 		sc->sc_transfer.offset += actlen;
713 
714 		if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
715 			/* short transfer or end of data */
716 			ustorage_fs_transfer_start(sc,
717 			    USTORAGE_FS_T_BBB_STATUS);
718 			break;
719 		}
720 		/* Fallthrough */
721 
722 	case USB_ST_SETUP:
723 tr_setup:
724 		if (max_bulk > sc->sc_transfer.data_rem) {
725 			max_bulk = sc->sc_transfer.data_rem;
726 		}
727 		if (sc->sc_transfer.data_error) {
728 			sc->sc_transfer.data_error = 0;
729 			usbd_xfer_set_stall(xfer);
730 		}
731 
732 		usbd_xfer_set_frame_data(xfer, 0, sc->sc_dma_ptr, max_bulk);
733 		usbd_transfer_submit(xfer);
734 		break;
735 
736 	default:			/* Error */
737 		if (error == USB_ERR_CANCELLED) {
738 			break;
739 		}
740 		/* If the pipe is already stalled, don't do another stall */
741 		if (!usbd_xfer_is_stalled(xfer))
742 			sc->sc_transfer.data_error = 1;
743 
744 		/* try again */
745 		goto tr_setup;
746 	}
747 }
748 
749 static void
750 ustorage_fs_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
751 {
752 	struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
753 	uint32_t max_bulk = usbd_xfer_max_len(xfer);
754 	int actlen, sumlen;
755 
756 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
757 
758 	DPRINTF("\n");
759 
760 	switch (USB_GET_STATE(xfer)) {
761 	case USB_ST_TRANSFERRED:
762 		sc->sc_transfer.data_rem -= actlen;
763 		sc->sc_transfer.data_ptr += actlen;
764 		sc->sc_transfer.offset += actlen;
765 
766 		if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
767 			/* short transfer or end of data */
768 			ustorage_fs_transfer_start(sc,
769 			    USTORAGE_FS_T_BBB_STATUS);
770 			break;
771 		}
772 	case USB_ST_SETUP:
773 tr_setup:
774 		if (max_bulk >= sc->sc_transfer.data_rem) {
775 			max_bulk = sc->sc_transfer.data_rem;
776 			if (sc->sc_transfer.data_short)
777 				usbd_xfer_set_flag(xfer, USB_FORCE_SHORT_XFER);
778 			else
779 				usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
780 		} else
781 			usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
782 
783 		if (sc->sc_transfer.data_error) {
784 			sc->sc_transfer.data_error = 0;
785 			usbd_xfer_set_stall(xfer);
786 		}
787 
788 		/* XXX copy data to DMA buffer */
789 		memcpy(sc->sc_dma_ptr, sc->sc_transfer.data_ptr, max_bulk);
790 
791 		usbd_xfer_set_frame_data(xfer, 0, sc->sc_dma_ptr, max_bulk);
792 		usbd_transfer_submit(xfer);
793 		break;
794 
795 	default:			/* Error */
796 		if (error == USB_ERR_CANCELLED) {
797 			break;
798 		}
799 		/*
800 		 * If the pipe is already stalled, don't do another
801 		 * stall
802 		 */
803 		if (!usbd_xfer_is_stalled(xfer))
804 			sc->sc_transfer.data_error = 1;
805 
806 		/* try again */
807 		goto tr_setup;
808 	}
809 }
810 
811 static void
812 ustorage_fs_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
813 {
814 	struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
815 
816 	DPRINTF("\n");
817 
818 	switch (USB_GET_STATE(xfer)) {
819 	case USB_ST_TRANSFERRED:
820 		ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
821 		break;
822 
823 	case USB_ST_SETUP:
824 tr_setup:
825 		USETDW(sc->sc_csw->dCSWSignature, CSWSIGNATURE);
826 		USETDW(sc->sc_csw->dCSWDataResidue, sc->sc_transfer.data_rem);
827 
828 		if (sc->sc_transfer.data_error) {
829 			sc->sc_transfer.data_error = 0;
830 			usbd_xfer_set_stall(xfer);
831 		}
832 		usbd_xfer_set_frame_len(xfer, 0,
833 		    sizeof(ustorage_fs_bbb_csw_t));
834 		usbd_transfer_submit(xfer);
835 		break;
836 
837 	default:
838 		if (error == USB_ERR_CANCELLED) {
839 			break;
840 		}
841 		/* If the pipe is already stalled, don't do another stall */
842 		if (!usbd_xfer_is_stalled(xfer))
843 			sc->sc_transfer.data_error = 1;
844 
845 		/* try again */
846 		goto tr_setup;
847 	}
848 }
849 
850 /* SCSI commands that we recognize */
851 #define	SC_FORMAT_UNIT			0x04
852 #define	SC_INQUIRY			0x12
853 #define	SC_MODE_SELECT_6		0x15
854 #define	SC_MODE_SELECT_10		0x55
855 #define	SC_MODE_SENSE_6			0x1a
856 #define	SC_MODE_SENSE_10		0x5a
857 #define	SC_PREVENT_ALLOW_MEDIUM_REMOVAL	0x1e
858 #define	SC_READ_6			0x08
859 #define	SC_READ_10			0x28
860 #define	SC_READ_12			0xa8
861 #define	SC_READ_CAPACITY		0x25
862 #define	SC_READ_FORMAT_CAPACITIES	0x23
863 #define	SC_RELEASE			0x17
864 #define	SC_REQUEST_SENSE		0x03
865 #define	SC_RESERVE			0x16
866 #define	SC_SEND_DIAGNOSTIC		0x1d
867 #define	SC_START_STOP_UNIT		0x1b
868 #define	SC_SYNCHRONIZE_CACHE		0x35
869 #define	SC_TEST_UNIT_READY		0x00
870 #define	SC_VERIFY			0x2f
871 #define	SC_WRITE_6			0x0a
872 #define	SC_WRITE_10			0x2a
873 #define	SC_WRITE_12			0xaa
874 
875 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
876 #define	SS_NO_SENSE				0
877 #define	SS_COMMUNICATION_FAILURE		0x040800
878 #define	SS_INVALID_COMMAND			0x052000
879 #define	SS_INVALID_FIELD_IN_CDB			0x052400
880 #define	SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE	0x052100
881 #define	SS_LOGICAL_UNIT_NOT_SUPPORTED		0x052500
882 #define	SS_MEDIUM_NOT_PRESENT			0x023a00
883 #define	SS_MEDIUM_REMOVAL_PREVENTED		0x055302
884 #define	SS_NOT_READY_TO_READY_TRANSITION	0x062800
885 #define	SS_RESET_OCCURRED			0x062900
886 #define	SS_SAVING_PARAMETERS_NOT_SUPPORTED	0x053900
887 #define	SS_UNRECOVERED_READ_ERROR		0x031100
888 #define	SS_WRITE_ERROR				0x030c02
889 #define	SS_WRITE_PROTECTED			0x072700
890 
891 #define	SK(x)		((uint8_t) ((x) >> 16))	/* Sense Key byte, etc. */
892 #define	ASC(x)		((uint8_t) ((x) >> 8))
893 #define	ASCQ(x)		((uint8_t) (x))
894 
895 /* Routines for unaligned data access */
896 
897 static uint16_t
898 get_be16(uint8_t *buf)
899 {
900 	return ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
901 }
902 
903 static uint32_t
904 get_be32(uint8_t *buf)
905 {
906 	return ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) |
907 	((uint32_t)buf[2] << 8) | ((uint32_t)buf[3]);
908 }
909 
910 static void
911 put_be16(uint8_t *buf, uint16_t val)
912 {
913 	buf[0] = val >> 8;
914 	buf[1] = val;
915 }
916 
917 static void
918 put_be32(uint8_t *buf, uint32_t val)
919 {
920 	buf[0] = val >> 24;
921 	buf[1] = val >> 16;
922 	buf[2] = val >> 8;
923 	buf[3] = val & 0xff;
924 }
925 
926 /*------------------------------------------------------------------------*
927  *	ustorage_fs_verify
928  *
929  * Returns:
930  *    0: Success
931  * Else: Failure
932  *------------------------------------------------------------------------*/
933 static uint8_t
934 ustorage_fs_verify(struct ustorage_fs_softc *sc)
935 {
936 	struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
937 	uint32_t lba;
938 	uint32_t vlen;
939 
940 	/*
941 	 * Get the starting Logical Block Address
942 	 */
943 	lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
944 
945 	/*
946 	 * We allow DPO (Disable Page Out = don't save data in the cache)
947 	 * but we don't implement it.
948 	 */
949 	if ((sc->sc_cbw->CBWCDB[1] & ~0x10) != 0) {
950 		currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
951 		return (1);
952 	}
953 	vlen = get_be16(&sc->sc_cbw->CBWCDB[7]);
954 	if (vlen == 0) {
955 		goto done;
956 	}
957 	/* No default reply */
958 
959 	/* Range check */
960 	vlen += lba;
961 
962 	if ((vlen < lba) ||
963 	    (vlen > currlun->num_sectors) ||
964 	    (lba >= currlun->num_sectors)) {
965 		currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
966 		return (1);
967 	}
968 	/* XXX TODO: verify that data is readable */
969 done:
970 	return (ustorage_fs_min_len(sc, 0, -1U));
971 }
972 
973 /*------------------------------------------------------------------------*
974  *	ustorage_fs_inquiry
975  *
976  * Returns:
977  *    0: Success
978  * Else: Failure
979  *------------------------------------------------------------------------*/
980 static uint8_t
981 ustorage_fs_inquiry(struct ustorage_fs_softc *sc)
982 {
983 	uint8_t *buf = sc->sc_transfer.data_ptr;
984 
985 	struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
986 
987 	if (!sc->sc_transfer.currlun) {
988 		/* Unsupported LUNs are okay */
989 		memset(buf, 0, 36);
990 		buf[0] = 0x7f;
991 		/* Unsupported, no device - type */
992 		return (ustorage_fs_min_len(sc, 36, -1U));
993 	}
994 	memset(buf, 0, 8);
995 	/* Non - removable, direct - access device */
996 	if (currlun->removable)
997 		buf[1] = 0x80;
998 	buf[2] = 2;
999 	/* ANSI SCSI level 2 */
1000 	buf[3] = 2;
1001 	/* SCSI - 2 INQUIRY data format */
1002 	buf[4] = 31;
1003 	/* Additional length */
1004 	/* No special options */
1005 	/* Copy in ID string */
1006 	memcpy(buf + 8, USTORAGE_FS_ID_STRING, 28);
1007 
1008 #if (USTORAGE_QDATA_MAX < 36)
1009 #error "(USTORAGE_QDATA_MAX < 36)"
1010 #endif
1011 	return (ustorage_fs_min_len(sc, 36, -1U));
1012 }
1013 
1014 /*------------------------------------------------------------------------*
1015  *	ustorage_fs_request_sense
1016  *
1017  * Returns:
1018  *    0: Success
1019  * Else: Failure
1020  *------------------------------------------------------------------------*/
1021 static uint8_t
1022 ustorage_fs_request_sense(struct ustorage_fs_softc *sc)
1023 {
1024 	uint8_t *buf = sc->sc_transfer.data_ptr;
1025 	struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1026 	uint32_t sd;
1027 	uint32_t sdinfo;
1028 	uint8_t valid;
1029 
1030 	/*
1031 	 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1032 	 *
1033 	 * If a REQUEST SENSE command is received from an initiator
1034 	 * with a pending unit attention condition (before the target
1035 	 * generates the contingent allegiance condition), then the
1036 	 * target shall either:
1037 	 *   a) report any pending sense data and preserve the unit
1038 	 *	attention condition on the logical unit, or,
1039 	 *   b) report the unit attention condition, may discard any
1040 	 *	pending sense data, and clear the unit attention
1041 	 *	condition on the logical unit for that initiator.
1042 	 *
1043 	 * FSG normally uses option a); enable this code to use option b).
1044 	 */
1045 #if 0
1046 	if (currlun && currlun->unit_attention_data != SS_NO_SENSE) {
1047 		currlun->sense_data = currlun->unit_attention_data;
1048 		currlun->unit_attention_data = SS_NO_SENSE;
1049 	}
1050 #endif
1051 
1052 	if (!currlun) {
1053 		/* Unsupported LUNs are okay */
1054 		sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1055 		sdinfo = 0;
1056 		valid = 0;
1057 	} else {
1058 		sd = currlun->sense_data;
1059 		sdinfo = currlun->sense_data_info;
1060 		valid = currlun->info_valid << 7;
1061 		currlun->sense_data = SS_NO_SENSE;
1062 		currlun->sense_data_info = 0;
1063 		currlun->info_valid = 0;
1064 	}
1065 
1066 	memset(buf, 0, 18);
1067 	buf[0] = valid | 0x70;
1068 	/* Valid, current error */
1069 	buf[2] = SK(sd);
1070 	put_be32(&buf[3], sdinfo);
1071 	/* Sense information */
1072 	buf[7] = 18 - 8;
1073 	/* Additional sense length */
1074 	buf[12] = ASC(sd);
1075 	buf[13] = ASCQ(sd);
1076 
1077 #if (USTORAGE_QDATA_MAX < 18)
1078 #error "(USTORAGE_QDATA_MAX < 18)"
1079 #endif
1080 	return (ustorage_fs_min_len(sc, 18, -1U));
1081 }
1082 
1083 /*------------------------------------------------------------------------*
1084  *	ustorage_fs_read_capacity
1085  *
1086  * Returns:
1087  *    0: Success
1088  * Else: Failure
1089  *------------------------------------------------------------------------*/
1090 static uint8_t
1091 ustorage_fs_read_capacity(struct ustorage_fs_softc *sc)
1092 {
1093 	uint8_t *buf = sc->sc_transfer.data_ptr;
1094 	struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1095 	uint32_t lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1096 	uint8_t pmi = sc->sc_cbw->CBWCDB[8];
1097 
1098 	/* Check the PMI and LBA fields */
1099 	if ((pmi > 1) || ((pmi == 0) && (lba != 0))) {
1100 		currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1101 		return (1);
1102 	}
1103 	/* Max logical block */
1104 	put_be32(&buf[0], currlun->num_sectors - 1);
1105 	/* Block length */
1106 	put_be32(&buf[4], 512);
1107 
1108 #if (USTORAGE_QDATA_MAX < 8)
1109 #error "(USTORAGE_QDATA_MAX < 8)"
1110 #endif
1111 	return (ustorage_fs_min_len(sc, 8, -1U));
1112 }
1113 
1114 /*------------------------------------------------------------------------*
1115  *	ustorage_fs_mode_sense
1116  *
1117  * Returns:
1118  *    0: Success
1119  * Else: Failure
1120  *------------------------------------------------------------------------*/
1121 static uint8_t
1122 ustorage_fs_mode_sense(struct ustorage_fs_softc *sc)
1123 {
1124 	uint8_t *buf = sc->sc_transfer.data_ptr;
1125 	struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1126 	uint8_t *buf0;
1127 	uint16_t len;
1128 	uint16_t limit;
1129 	uint8_t mscmnd = sc->sc_cbw->CBWCDB[0];
1130 	uint8_t pc;
1131 	uint8_t page_code;
1132 	uint8_t changeable_values;
1133 	uint8_t all_pages;
1134 
1135 	buf0 = buf;
1136 
1137 	if ((sc->sc_cbw->CBWCDB[1] & ~0x08) != 0) {
1138 		/* Mask away DBD */
1139 		currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1140 		return (1);
1141 	}
1142 	pc = sc->sc_cbw->CBWCDB[2] >> 6;
1143 	page_code = sc->sc_cbw->CBWCDB[2] & 0x3f;
1144 	if (pc == 3) {
1145 		currlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1146 		return (1);
1147 	}
1148 	changeable_values = (pc == 1);
1149 	all_pages = (page_code == 0x3f);
1150 
1151 	/*
1152 	 * Write the mode parameter header.  Fixed values are: default
1153 	 * medium type, no cache control (DPOFUA), and no block descriptors.
1154 	 * The only variable value is the WriteProtect bit.  We will fill in
1155 	 * the mode data length later.
1156 	 */
1157 	memset(buf, 0, 8);
1158 	if (mscmnd == SC_MODE_SENSE_6) {
1159 		buf[2] = (currlun->read_only ? 0x80 : 0x00);
1160 		/* WP, DPOFUA */
1161 		buf += 4;
1162 		limit = 255;
1163 	} else {
1164 		/* SC_MODE_SENSE_10 */
1165 		buf[3] = (currlun->read_only ? 0x80 : 0x00);
1166 		/* WP, DPOFUA */
1167 		buf += 8;
1168 		limit = 65535;
1169 		/* Should really be mod_data.buflen */
1170 	}
1171 
1172 	/* No block descriptors */
1173 
1174 	/*
1175 	 * The mode pages, in numerical order.
1176 	 */
1177 	if ((page_code == 0x08) || all_pages) {
1178 		buf[0] = 0x08;
1179 		/* Page code */
1180 		buf[1] = 10;
1181 		/* Page length */
1182 		memset(buf + 2, 0, 10);
1183 		/* None of the fields are changeable */
1184 
1185 		if (!changeable_values) {
1186 			buf[2] = 0x04;
1187 			/* Write cache enable, */
1188 			/* Read cache not disabled */
1189 			/* No cache retention priorities */
1190 			put_be16(&buf[4], 0xffff);
1191 			/* Don 't disable prefetch */
1192 			/* Minimum prefetch = 0 */
1193 			put_be16(&buf[8], 0xffff);
1194 			/* Maximum prefetch */
1195 			put_be16(&buf[10], 0xffff);
1196 			/* Maximum prefetch ceiling */
1197 		}
1198 		buf += 12;
1199 	}
1200 	/*
1201 	 * Check that a valid page was requested and the mode data length
1202 	 * isn't too long.
1203 	 */
1204 	len = buf - buf0;
1205 	if (len > limit) {
1206 		currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1207 		return (1);
1208 	}
1209 	/* Store the mode data length */
1210 	if (mscmnd == SC_MODE_SENSE_6)
1211 		buf0[0] = len - 1;
1212 	else
1213 		put_be16(buf0, len - 2);
1214 
1215 #if (USTORAGE_QDATA_MAX < 24)
1216 #error "(USTORAGE_QDATA_MAX < 24)"
1217 #endif
1218 	return (ustorage_fs_min_len(sc, len, -1U));
1219 }
1220 
1221 /*------------------------------------------------------------------------*
1222  *	ustorage_fs_start_stop
1223  *
1224  * Returns:
1225  *    0: Success
1226  * Else: Failure
1227  *------------------------------------------------------------------------*/
1228 static uint8_t
1229 ustorage_fs_start_stop(struct ustorage_fs_softc *sc)
1230 {
1231 	struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1232 	uint8_t loej;
1233 	uint8_t start;
1234 	uint8_t immed;
1235 
1236 	if (!currlun->removable) {
1237 		currlun->sense_data = SS_INVALID_COMMAND;
1238 		return (1);
1239 	}
1240 	immed = sc->sc_cbw->CBWCDB[1] & 0x01;
1241 	loej = sc->sc_cbw->CBWCDB[4] & 0x02;
1242 	start = sc->sc_cbw->CBWCDB[4] & 0x01;
1243 
1244 	if (immed || loej || start) {
1245 		/* compile fix */
1246 	}
1247 	return (0);
1248 }
1249 
1250 /*------------------------------------------------------------------------*
1251  *	ustorage_fs_prevent_allow
1252  *
1253  * Returns:
1254  *    0: Success
1255  * Else: Failure
1256  *------------------------------------------------------------------------*/
1257 static uint8_t
1258 ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc)
1259 {
1260 	struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1261 	uint8_t prevent;
1262 
1263 	if (!currlun->removable) {
1264 		currlun->sense_data = SS_INVALID_COMMAND;
1265 		return (1);
1266 	}
1267 	prevent = sc->sc_cbw->CBWCDB[4] & 0x01;
1268 	if ((sc->sc_cbw->CBWCDB[4] & ~0x01) != 0) {
1269 		/* Mask away Prevent */
1270 		currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1271 		return (1);
1272 	}
1273 	if (currlun->prevent_medium_removal && !prevent) {
1274 		//fsync_sub(currlun);
1275 	}
1276 	currlun->prevent_medium_removal = prevent;
1277 	return (0);
1278 }
1279 
1280 /*------------------------------------------------------------------------*
1281  *	ustorage_fs_read_format_capacities
1282  *
1283  * Returns:
1284  *    0: Success
1285  * Else: Failure
1286  *------------------------------------------------------------------------*/
1287 static uint8_t
1288 ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc)
1289 {
1290 	uint8_t *buf = sc->sc_transfer.data_ptr;
1291 	struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1292 
1293 	buf[0] = buf[1] = buf[2] = 0;
1294 	buf[3] = 8;
1295 	/* Only the Current / Maximum Capacity Descriptor */
1296 	buf += 4;
1297 
1298 	/* Number of blocks */
1299 	put_be32(&buf[0], currlun->num_sectors);
1300 	/* Block length */
1301 	put_be32(&buf[4], 512);
1302 	/* Current capacity */
1303 	buf[4] = 0x02;
1304 
1305 #if (USTORAGE_QDATA_MAX < 12)
1306 #error "(USTORAGE_QDATA_MAX < 12)"
1307 #endif
1308 	return (ustorage_fs_min_len(sc, 12, -1U));
1309 }
1310 
1311 /*------------------------------------------------------------------------*
1312  *	ustorage_fs_mode_select
1313  *
1314  * Return values:
1315  *    0: Success
1316  * Else: Failure
1317  *------------------------------------------------------------------------*/
1318 static uint8_t
1319 ustorage_fs_mode_select(struct ustorage_fs_softc *sc)
1320 {
1321 	struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1322 
1323 	/* We don't support MODE SELECT */
1324 	currlun->sense_data = SS_INVALID_COMMAND;
1325 	return (1);
1326 }
1327 
1328 /*------------------------------------------------------------------------*
1329  *	ustorage_fs_synchronize_cache
1330  *
1331  * Return values:
1332  *    0: Success
1333  * Else: Failure
1334  *------------------------------------------------------------------------*/
1335 static uint8_t
1336 ustorage_fs_synchronize_cache(struct ustorage_fs_softc *sc)
1337 {
1338 #if 0
1339 	struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1340 	uint8_t rc;
1341 
1342 	/*
1343 	 * We ignore the requested LBA and write out all dirty data buffers.
1344 	 */
1345 	rc = 0;
1346 	if (rc) {
1347 		currlun->sense_data = SS_WRITE_ERROR;
1348 	}
1349 #endif
1350 	return (0);
1351 }
1352 
1353 /*------------------------------------------------------------------------*
1354  *	ustorage_fs_read - read data from disk
1355  *
1356  * Return values:
1357  *    0: Success
1358  * Else: Failure
1359  *------------------------------------------------------------------------*/
1360 static uint8_t
1361 ustorage_fs_read(struct ustorage_fs_softc *sc)
1362 {
1363 	struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1364 	uint64_t file_offset;
1365 	uint32_t lba;
1366 	uint32_t len;
1367 
1368 	/*
1369 	 * Get the starting Logical Block Address and check that it's not
1370 	 * too big
1371 	 */
1372 	if (sc->sc_cbw->CBWCDB[0] == SC_READ_6) {
1373 		lba = (((uint32_t)sc->sc_cbw->CBWCDB[1]) << 16) |
1374 		    get_be16(&sc->sc_cbw->CBWCDB[2]);
1375 	} else {
1376 		lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1377 
1378 		/*
1379 		 * We allow DPO (Disable Page Out = don't save data in the
1380 		 * cache) and FUA (Force Unit Access = don't read from the
1381 		 * cache), but we don't implement them.
1382 		 */
1383 		if ((sc->sc_cbw->CBWCDB[1] & ~0x18) != 0) {
1384 			currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1385 			return (1);
1386 		}
1387 	}
1388 	len = sc->sc_transfer.data_rem >> 9;
1389 	len += lba;
1390 
1391 	if ((len < lba) ||
1392 	    (len > currlun->num_sectors) ||
1393 	    (lba >= currlun->num_sectors)) {
1394 		currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1395 		return (1);
1396 	}
1397 	file_offset = lba;
1398 	file_offset <<= 9;
1399 
1400 	sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1401 
1402 	return (0);
1403 }
1404 
1405 /*------------------------------------------------------------------------*
1406  *	ustorage_fs_write - write data to disk
1407  *
1408  * Return values:
1409  *    0: Success
1410  * Else: Failure
1411  *------------------------------------------------------------------------*/
1412 static uint8_t
1413 ustorage_fs_write(struct ustorage_fs_softc *sc)
1414 {
1415 	struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1416 	uint64_t file_offset;
1417 	uint32_t lba;
1418 	uint32_t len;
1419 
1420 	if (currlun->read_only) {
1421 		currlun->sense_data = SS_WRITE_PROTECTED;
1422 		return (1);
1423 	}
1424 	/* XXX clear SYNC */
1425 
1426 	/*
1427 	 * Get the starting Logical Block Address and check that it's not
1428 	 * too big.
1429 	 */
1430 	if (sc->sc_cbw->CBWCDB[0] == SC_WRITE_6)
1431 		lba = (((uint32_t)sc->sc_cbw->CBWCDB[1]) << 16) |
1432 		    get_be16(&sc->sc_cbw->CBWCDB[2]);
1433 	else {
1434 		lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1435 
1436 		/*
1437 		 * We allow DPO (Disable Page Out = don't save data in the
1438 		 * cache) and FUA (Force Unit Access = write directly to the
1439 		 * medium).  We don't implement DPO; we implement FUA by
1440 		 * performing synchronous output.
1441 		 */
1442 		if ((sc->sc_cbw->CBWCDB[1] & ~0x18) != 0) {
1443 			currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1444 			return (1);
1445 		}
1446 		if (sc->sc_cbw->CBWCDB[1] & 0x08) {
1447 			/* FUA */
1448 			/* XXX set SYNC flag here */
1449 		}
1450 	}
1451 
1452 	len = sc->sc_transfer.data_rem >> 9;
1453 	len += lba;
1454 
1455 	if ((len < lba) ||
1456 	    (len > currlun->num_sectors) ||
1457 	    (lba >= currlun->num_sectors)) {
1458 		currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1459 		return (1);
1460 	}
1461 	file_offset = lba;
1462 	file_offset <<= 9;
1463 
1464 	sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1465 
1466 	return (0);
1467 }
1468 
1469 /*------------------------------------------------------------------------*
1470  *	ustorage_fs_min_len
1471  *
1472  * Return values:
1473  *    0: Success
1474  * Else: Failure
1475  *------------------------------------------------------------------------*/
1476 static uint8_t
1477 ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask)
1478 {
1479 	if (len != sc->sc_transfer.data_rem) {
1480 		if (sc->sc_transfer.cbw_dir == DIR_READ) {
1481 			/*
1482 			 * there must be something wrong about this SCSI
1483 			 * command
1484 			 */
1485 			sc->sc_csw->bCSWStatus = CSWSTATUS_PHASE;
1486 			return (1);
1487 		}
1488 		/* compute the minimum length */
1489 
1490 		if (sc->sc_transfer.data_rem > len) {
1491 			/* data ends prematurely */
1492 			sc->sc_transfer.data_rem = len;
1493 			sc->sc_transfer.data_short = 1;
1494 		}
1495 		/* check length alignment */
1496 
1497 		if (sc->sc_transfer.data_rem & ~mask) {
1498 			/* data ends prematurely */
1499 			sc->sc_transfer.data_rem &= mask;
1500 			sc->sc_transfer.data_short = 1;
1501 		}
1502 	}
1503 	return (0);
1504 }
1505 
1506 /*------------------------------------------------------------------------*
1507  *	ustorage_fs_check_cmd - check command routine
1508  *
1509  * Check whether the command is properly formed and whether its data
1510  * size and direction agree with the values we already have.
1511  *
1512  * Return values:
1513  *    0: Success
1514  * Else: Failure
1515  *------------------------------------------------------------------------*/
1516 static uint8_t
1517 ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t min_cmd_size,
1518     uint16_t mask, uint8_t needs_medium)
1519 {
1520 	struct ustorage_fs_lun *currlun;
1521 	uint8_t lun = (sc->sc_cbw->CBWCDB[1] >> 5);
1522 	uint8_t i;
1523 
1524 	/* Verify the length of the command itself */
1525 	if (min_cmd_size > sc->sc_transfer.cmd_len) {
1526 		DPRINTF("%u > %u\n",
1527 		    min_cmd_size, sc->sc_transfer.cmd_len);
1528 		sc->sc_csw->bCSWStatus = CSWSTATUS_PHASE;
1529 		return (1);
1530 	}
1531 	/* Mask away the LUN */
1532 	sc->sc_cbw->CBWCDB[1] &= 0x1f;
1533 
1534 	/* Check if LUN is correct */
1535 	if (lun != sc->sc_transfer.lun) {
1536 	}
1537 	/* Check the LUN */
1538 	if (sc->sc_transfer.lun <= sc->sc_last_lun) {
1539 		sc->sc_transfer.currlun = currlun =
1540 		    sc->sc_lun + sc->sc_transfer.lun;
1541 		if (sc->sc_cbw->CBWCDB[0] != SC_REQUEST_SENSE) {
1542 			currlun->sense_data = SS_NO_SENSE;
1543 			currlun->sense_data_info = 0;
1544 			currlun->info_valid = 0;
1545 		}
1546 		/*
1547 		 * If a unit attention condition exists, only INQUIRY
1548 		 * and REQUEST SENSE commands are allowed. Anything
1549 		 * else must fail!
1550 		 */
1551 		if ((currlun->unit_attention_data != SS_NO_SENSE) &&
1552 		    (sc->sc_cbw->CBWCDB[0] != SC_INQUIRY) &&
1553 		    (sc->sc_cbw->CBWCDB[0] != SC_REQUEST_SENSE)) {
1554 			currlun->sense_data = currlun->unit_attention_data;
1555 			currlun->unit_attention_data = SS_NO_SENSE;
1556 			return (1);
1557 		}
1558 	} else {
1559 		sc->sc_transfer.currlun = currlun = NULL;
1560 
1561 		/*
1562 		 * INQUIRY and REQUEST SENSE commands are explicitly allowed
1563 		 * to use unsupported LUNs; all others may not.
1564 		 */
1565 		if ((sc->sc_cbw->CBWCDB[0] != SC_INQUIRY) &&
1566 		    (sc->sc_cbw->CBWCDB[0] != SC_REQUEST_SENSE)) {
1567 			return (1);
1568 		}
1569 	}
1570 
1571 	/*
1572 	 * Check that only command bytes listed in the mask are
1573 	 * non-zero.
1574 	 */
1575 	for (i = 0; i != min_cmd_size; i++) {
1576 		if (sc->sc_cbw->CBWCDB[i] && !(mask & (1UL << i))) {
1577 			if (currlun) {
1578 				currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1579 			}
1580 			return (1);
1581 		}
1582 	}
1583 
1584 	/*
1585 	 * If the medium isn't mounted and the command needs to access
1586 	 * it, return an error.
1587 	 */
1588 	if (currlun && (!currlun->memory_image) && needs_medium) {
1589 		currlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1590 		return (1);
1591 	}
1592 	return (0);
1593 }
1594 
1595 /*------------------------------------------------------------------------*
1596  *	ustorage_fs_do_cmd - do command
1597  *
1598  * Return values:
1599  *    0: Success
1600  * Else: Failure
1601  *------------------------------------------------------------------------*/
1602 static uint8_t
1603 ustorage_fs_do_cmd(struct ustorage_fs_softc *sc)
1604 {
1605 	uint8_t error = 1;
1606 	uint8_t i;
1607 	uint32_t temp;
1608 	const uint32_t mask9 = (0xFFFFFFFFUL >> 9) << 9;
1609 
1610 	/* set default data transfer pointer */
1611 	sc->sc_transfer.data_ptr = sc->sc_qdata;
1612 
1613 	DPRINTF("cmd_data[0]=0x%02x, data_rem=0x%08x\n",
1614 	    sc->sc_cbw->CBWCDB[0], sc->sc_transfer.data_rem);
1615 
1616 	switch (sc->sc_cbw->CBWCDB[0]) {
1617 	case SC_INQUIRY:
1618 		sc->sc_transfer.cmd_dir = DIR_WRITE;
1619 		error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1620 		if (error) {
1621 			break;
1622 		}
1623 		error = ustorage_fs_check_cmd(sc, 6,
1624 		    (1UL << 4) | 1, 0);
1625 		if (error) {
1626 			break;
1627 		}
1628 		error = ustorage_fs_inquiry(sc);
1629 
1630 		break;
1631 
1632 	case SC_MODE_SELECT_6:
1633 		sc->sc_transfer.cmd_dir = DIR_READ;
1634 		error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1635 		if (error) {
1636 			break;
1637 		}
1638 		error = ustorage_fs_check_cmd(sc, 6,
1639 		    (1UL << 1) | (1UL << 4) | 1, 0);
1640 		if (error) {
1641 			break;
1642 		}
1643 		error = ustorage_fs_mode_select(sc);
1644 
1645 		break;
1646 
1647 	case SC_MODE_SELECT_10:
1648 		sc->sc_transfer.cmd_dir = DIR_READ;
1649 		error = ustorage_fs_min_len(sc,
1650 		    get_be16(&sc->sc_cbw->CBWCDB[7]), -1U);
1651 		if (error) {
1652 			break;
1653 		}
1654 		error = ustorage_fs_check_cmd(sc, 10,
1655 		    (1UL << 1) | (3UL << 7) | 1, 0);
1656 		if (error) {
1657 			break;
1658 		}
1659 		error = ustorage_fs_mode_select(sc);
1660 
1661 		break;
1662 
1663 	case SC_MODE_SENSE_6:
1664 		sc->sc_transfer.cmd_dir = DIR_WRITE;
1665 		error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1666 		if (error) {
1667 			break;
1668 		}
1669 		error = ustorage_fs_check_cmd(sc, 6,
1670 		    (1UL << 1) | (1UL << 2) | (1UL << 4) | 1, 0);
1671 		if (error) {
1672 			break;
1673 		}
1674 		error = ustorage_fs_mode_sense(sc);
1675 
1676 		break;
1677 
1678 	case SC_MODE_SENSE_10:
1679 		sc->sc_transfer.cmd_dir = DIR_WRITE;
1680 		error = ustorage_fs_min_len(sc,
1681 		    get_be16(&sc->sc_cbw->CBWCDB[7]), -1U);
1682 		if (error) {
1683 			break;
1684 		}
1685 		error = ustorage_fs_check_cmd(sc, 10,
1686 		    (1UL << 1) | (1UL << 2) | (3UL << 7) | 1, 0);
1687 		if (error) {
1688 			break;
1689 		}
1690 		error = ustorage_fs_mode_sense(sc);
1691 
1692 		break;
1693 
1694 	case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1695 		error = ustorage_fs_min_len(sc, 0, -1U);
1696 		if (error) {
1697 			break;
1698 		}
1699 		error = ustorage_fs_check_cmd(sc, 6,
1700 		    (1UL << 4) | 1, 0);
1701 		if (error) {
1702 			break;
1703 		}
1704 		error = ustorage_fs_prevent_allow(sc);
1705 
1706 		break;
1707 
1708 	case SC_READ_6:
1709 		i = sc->sc_cbw->CBWCDB[4];
1710 		sc->sc_transfer.cmd_dir = DIR_WRITE;
1711 		temp = ((i == 0) ? 256UL : i);
1712 		error = ustorage_fs_min_len(sc, temp << 9, mask9);
1713 		if (error) {
1714 			break;
1715 		}
1716 		error = ustorage_fs_check_cmd(sc, 6,
1717 		    (7UL << 1) | (1UL << 4) | 1, 1);
1718 		if (error) {
1719 			break;
1720 		}
1721 		error = ustorage_fs_read(sc);
1722 
1723 		break;
1724 
1725 	case SC_READ_10:
1726 		sc->sc_transfer.cmd_dir = DIR_WRITE;
1727 		temp = get_be16(&sc->sc_cbw->CBWCDB[7]);
1728 		error = ustorage_fs_min_len(sc, temp << 9, mask9);
1729 		if (error) {
1730 			break;
1731 		}
1732 		error = ustorage_fs_check_cmd(sc, 10,
1733 		    (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1734 		if (error) {
1735 			break;
1736 		}
1737 		error = ustorage_fs_read(sc);
1738 
1739 		break;
1740 
1741 	case SC_READ_12:
1742 		sc->sc_transfer.cmd_dir = DIR_WRITE;
1743 		temp = get_be32(&sc->sc_cbw->CBWCDB[6]);
1744 		if (temp >= (1UL << (32 - 9))) {
1745 			/* numerical overflow */
1746 			sc->sc_csw->bCSWStatus = CSWSTATUS_FAILED;
1747 			error = 1;
1748 			break;
1749 		}
1750 		error = ustorage_fs_min_len(sc, temp << 9, mask9);
1751 		if (error) {
1752 			break;
1753 		}
1754 		error = ustorage_fs_check_cmd(sc, 12,
1755 		    (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1756 		if (error) {
1757 			break;
1758 		}
1759 		error = ustorage_fs_read(sc);
1760 
1761 		break;
1762 
1763 	case SC_READ_CAPACITY:
1764 		sc->sc_transfer.cmd_dir = DIR_WRITE;
1765 		error = ustorage_fs_check_cmd(sc, 10,
1766 		    (0xfUL << 2) | (1UL << 8) | 1, 1);
1767 		if (error) {
1768 			break;
1769 		}
1770 		error = ustorage_fs_read_capacity(sc);
1771 
1772 		break;
1773 
1774 	case SC_READ_FORMAT_CAPACITIES:
1775 		sc->sc_transfer.cmd_dir = DIR_WRITE;
1776 		error = ustorage_fs_min_len(sc,
1777 		    get_be16(&sc->sc_cbw->CBWCDB[7]), -1U);
1778 		if (error) {
1779 			break;
1780 		}
1781 		error = ustorage_fs_check_cmd(sc, 10,
1782 		    (3UL << 7) | 1, 1);
1783 		if (error) {
1784 			break;
1785 		}
1786 		error = ustorage_fs_read_format_capacities(sc);
1787 
1788 		break;
1789 
1790 	case SC_REQUEST_SENSE:
1791 		sc->sc_transfer.cmd_dir = DIR_WRITE;
1792 		error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1793 		if (error) {
1794 			break;
1795 		}
1796 		error = ustorage_fs_check_cmd(sc, 6,
1797 		    (1UL << 4) | 1, 0);
1798 		if (error) {
1799 			break;
1800 		}
1801 		error = ustorage_fs_request_sense(sc);
1802 
1803 		break;
1804 
1805 	case SC_START_STOP_UNIT:
1806 		error = ustorage_fs_min_len(sc, 0, -1U);
1807 		if (error) {
1808 			break;
1809 		}
1810 		error = ustorage_fs_check_cmd(sc, 6,
1811 		    (1UL << 1) | (1UL << 4) | 1, 0);
1812 		if (error) {
1813 			break;
1814 		}
1815 		error = ustorage_fs_start_stop(sc);
1816 
1817 		break;
1818 
1819 	case SC_SYNCHRONIZE_CACHE:
1820 		error = ustorage_fs_min_len(sc, 0, -1U);
1821 		if (error) {
1822 			break;
1823 		}
1824 		error = ustorage_fs_check_cmd(sc, 10,
1825 		    (0xfUL << 2) | (3UL << 7) | 1, 1);
1826 		if (error) {
1827 			break;
1828 		}
1829 		error = ustorage_fs_synchronize_cache(sc);
1830 
1831 		break;
1832 
1833 	case SC_TEST_UNIT_READY:
1834 		error = ustorage_fs_min_len(sc, 0, -1U);
1835 		if (error) {
1836 			break;
1837 		}
1838 		error = ustorage_fs_check_cmd(sc, 6,
1839 		    0 | 1, 1);
1840 		break;
1841 
1842 		/*
1843 		 * Although optional, this command is used by MS-Windows.
1844 		 * We support a minimal version: BytChk must be 0.
1845 		 */
1846 	case SC_VERIFY:
1847 		error = ustorage_fs_min_len(sc, 0, -1U);
1848 		if (error) {
1849 			break;
1850 		}
1851 		error = ustorage_fs_check_cmd(sc, 10,
1852 		    (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1853 		if (error) {
1854 			break;
1855 		}
1856 		error = ustorage_fs_verify(sc);
1857 
1858 		break;
1859 
1860 	case SC_WRITE_6:
1861 		i = sc->sc_cbw->CBWCDB[4];
1862 		sc->sc_transfer.cmd_dir = DIR_READ;
1863 		temp = ((i == 0) ? 256UL : i);
1864 		error = ustorage_fs_min_len(sc, temp << 9, mask9);
1865 		if (error) {
1866 			break;
1867 		}
1868 		error = ustorage_fs_check_cmd(sc, 6,
1869 		    (7UL << 1) | (1UL << 4) | 1, 1);
1870 		if (error) {
1871 			break;
1872 		}
1873 		error = ustorage_fs_write(sc);
1874 
1875 		break;
1876 
1877 	case SC_WRITE_10:
1878 		sc->sc_transfer.cmd_dir = DIR_READ;
1879 		temp = get_be16(&sc->sc_cbw->CBWCDB[7]);
1880 		error = ustorage_fs_min_len(sc, temp << 9, mask9);
1881 		if (error) {
1882 			break;
1883 		}
1884 		error = ustorage_fs_check_cmd(sc, 10,
1885 		    (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1886 		if (error) {
1887 			break;
1888 		}
1889 		error = ustorage_fs_write(sc);
1890 
1891 		break;
1892 
1893 	case SC_WRITE_12:
1894 		sc->sc_transfer.cmd_dir = DIR_READ;
1895 		temp = get_be32(&sc->sc_cbw->CBWCDB[6]);
1896 		if (temp > (mask9 >> 9)) {
1897 			/* numerical overflow */
1898 			sc->sc_csw->bCSWStatus = CSWSTATUS_FAILED;
1899 			error = 1;
1900 			break;
1901 		}
1902 		error = ustorage_fs_min_len(sc, temp << 9, mask9);
1903 		if (error) {
1904 			break;
1905 		}
1906 		error = ustorage_fs_check_cmd(sc, 12,
1907 		    (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1908 		if (error) {
1909 			break;
1910 		}
1911 		error = ustorage_fs_write(sc);
1912 
1913 		break;
1914 
1915 		/*
1916 		 * Some mandatory commands that we recognize but don't
1917 		 * implement.  They don't mean much in this setting.
1918 		 * It's left as an exercise for anyone interested to
1919 		 * implement RESERVE and RELEASE in terms of Posix
1920 		 * locks.
1921 		 */
1922 	case SC_FORMAT_UNIT:
1923 	case SC_RELEASE:
1924 	case SC_RESERVE:
1925 	case SC_SEND_DIAGNOSTIC:
1926 		/* Fallthrough */
1927 
1928 	default:
1929 		error = ustorage_fs_min_len(sc, 0, -1U);
1930 		if (error) {
1931 			break;
1932 		}
1933 		error = ustorage_fs_check_cmd(sc, sc->sc_transfer.cmd_len,
1934 		    0xff, 0);
1935 		if (error) {
1936 			break;
1937 		}
1938 		sc->sc_transfer.currlun->sense_data =
1939 		    SS_INVALID_COMMAND;
1940 		error = 1;
1941 
1942 		break;
1943 	}
1944 	return (error);
1945 }
1946