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