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