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