xref: /titanic_51/usr/src/uts/common/io/usb/scsa2usb/scsa2usb.c (revision 14ea4bb737263733ad80a36b4f73f681c30a6b45)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * scsa2usb bridge nexus driver:
30  *
31  * This driver supports the following wire transports:
32  * a. Bulk Only transport (see usb_ms_bulkonly.c)
33  * b. CB transport (see usb_ms_cbi.c)
34  * c. CBI transport with interrupt status completion (see usb_ms_cbi.c)
35  *
36  * It handles the following command sets:
37  * a. SCSI
38  * b. ATAPI command set (subset of SCSI command set)
39  * c. UFI command set (
40  *	http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf)
41  *
42  * For details on USB Mass Storage Class overview:
43  *	http://www.usb.org/developers/devclass_docs/usbmassover_11.pdf
44  */
45 #if defined(lint) && !defined(DEBUG)
46 #define	DEBUG	1
47 #endif
48 
49 #include <sys/usb/usba/usbai_version.h>
50 #include <sys/scsi/scsi.h>
51 #include <sys/cdio.h>
52 #include <sys/sunndi.h>
53 #include <sys/esunddi.h>
54 #include <sys/callb.h>
55 #include <sys/kobj.h>
56 #include <sys/kobj_lex.h>
57 #include <sys/strsubr.h>
58 #include <sys/sysmacros.h>
59 
60 #include <sys/usb/usba.h>
61 #include <sys/usb/usba/usba_ugen.h>
62 
63 #include <sys/usb/usba/usba_private.h>
64 #include <sys/usb/clients/mass_storage/usb_bulkonly.h>
65 #include <sys/usb/scsa2usb/scsa2usb.h>
66 
67 /*
68  * Function Prototypes
69  */
70 static int	scsa2usb_attach(dev_info_t *, ddi_attach_cmd_t);
71 static int	scsa2usb_info(dev_info_t *, ddi_info_cmd_t, void *,
72 						void **);
73 static int	scsa2usb_detach(dev_info_t *, ddi_detach_cmd_t);
74 static int	scsa2usb_cleanup(dev_info_t *, scsa2usb_state_t *);
75 static void	scsa2usb_validate_attrs(scsa2usb_state_t *);
76 static void	scsa2usb_create_luns(scsa2usb_state_t *);
77 static int	scsa2usb_is_usb(dev_info_t *);
78 static int	scsa2usb_fake_inquiry(scsa2usb_state_t *,
79 					scsa2usb_cmd_t *, uint_t);
80 static void	scsa2usb_do_inquiry(scsa2usb_state_t *,
81 						uint_t, uint_t);
82 static int	scsa2usb_do_tur(scsa2usb_state_t *, struct scsi_address *);
83 
84 /* override property handling */
85 static void	scsa2usb_override(scsa2usb_state_t *);
86 static int	scsa2usb_parse_input_str(char *, scsa2usb_ov_t *,
87 		    scsa2usb_state_t *);
88 static void	scsa2usb_override_error(char *, scsa2usb_state_t *);
89 static char	*scsa2usb_strtok_r(char *, char *, char **);
90 
91 
92 /* PANIC callback handling */
93 static void	scsa2usb_panic_callb_init(scsa2usb_state_t *);
94 static void	scsa2usb_panic_callb_fini(scsa2usb_state_t *);
95 static boolean_t scsa2usb_panic_callb(void *, int);
96 
97 /* SCSA support */
98 static int	scsa2usb_scsi_tgt_probe(struct scsi_device *, int (*)(void));
99 static int	scsa2usb_scsi_tgt_init(dev_info_t *, dev_info_t *,
100 		    scsi_hba_tran_t *, struct scsi_device *);
101 static void	scsa2usb_scsi_tgt_free(dev_info_t *, dev_info_t *,
102 		    scsi_hba_tran_t *, struct scsi_device *);
103 static struct	scsi_pkt *scsa2usb_scsi_init_pkt(struct scsi_address *,
104 		    struct scsi_pkt *, struct buf *, int, int,
105 		    int, int, int (*)(), caddr_t);
106 static void	scsa2usb_scsi_destroy_pkt(struct scsi_address *,
107 		    struct scsi_pkt *);
108 static int	scsa2usb_scsi_start(struct scsi_address *, struct scsi_pkt *);
109 static int	scsa2usb_scsi_abort(struct scsi_address *, struct scsi_pkt *);
110 static int	scsa2usb_scsi_reset(struct scsi_address *, int);
111 static int	scsa2usb_scsi_getcap(struct scsi_address *, char *, int);
112 static int	scsa2usb_scsi_setcap(struct scsi_address *, char *, int, int);
113 static int	scsa2usb_scsi_bus_config(dev_info_t *, uint_t,
114 		    ddi_bus_config_op_t, void *, dev_info_t **);
115 static int	scsa2usb_scsi_bus_unconfig(dev_info_t *, uint_t,
116 		    ddi_bus_config_op_t, void *);
117 
118 /* functions for command and transport support */
119 static void	scsa2usb_prepare_pkt(scsa2usb_state_t *, struct scsi_pkt *);
120 static int	scsa2usb_cmd_transport(scsa2usb_state_t *, scsa2usb_cmd_t *);
121 static int	scsa2usb_check_bulkonly_blacklist_attrs(scsa2usb_state_t *,
122 		    scsa2usb_cmd_t *, uchar_t);
123 static int	scsa2usb_check_ufi_blacklist_attrs(scsa2usb_state_t *, uchar_t,
124 		    scsa2usb_cmd_t *);
125 static int	scsa2usb_handle_scsi_cmd_sub_class(scsa2usb_state_t *,
126 		    scsa2usb_cmd_t *, struct scsi_pkt *);
127 static int	scsa2usb_handle_ufi_subclass_cmd(scsa2usb_state_t *,
128 		    scsa2usb_cmd_t *, struct scsi_pkt *);
129 
130 /* waitQ handling */
131 static void	scsa2usb_work_thread(void *);
132 static void	scsa2usb_transport_request(scsa2usb_state_t *, uint_t);
133 static void	scsa2usb_flush_waitQ(scsa2usb_state_t *, uint_t, uchar_t);
134 static int	scsa2usb_all_waitQs_empty(scsa2usb_state_t *);
135 
136 /* auto request sense handling */
137 static int	scsa2usb_create_arq_pkt(scsa2usb_state_t *,
138 		    struct scsi_address *);
139 static void	scsa2usb_delete_arq_pkt(scsa2usb_state_t *);
140 static void	scsa2usb_complete_arq_pkt(scsa2usb_state_t *, struct scsi_pkt *,
141 		    scsa2usb_cmd_t *, struct buf *);
142 
143 /* utility functions for any transport */
144 static int	scsa2usb_open_usb_pipes(scsa2usb_state_t *);
145 void		scsa2usb_close_usb_pipes(scsa2usb_state_t *);
146 
147 static void	scsa2usb_fill_up_cdb_len(scsa2usb_cmd_t *, int);
148 static void	scsa2usb_fill_up_cdb_lba(scsa2usb_cmd_t *, int);
149 static void	scsa2usb_fill_up_ReadCD_cdb_len(scsa2usb_cmd_t *, int, int);
150 static void	scsa2usb_fill_up_12byte_cdb_len(scsa2usb_cmd_t *, int, int);
151 static int	scsa2usb_read_cd_blk_size(uchar_t);
152 int		scsa2usb_rw_transport(scsa2usb_state_t *, struct scsi_pkt *);
153 void		scsa2usb_setup_next_xfer(scsa2usb_state_t *, scsa2usb_cmd_t *);
154 
155 static mblk_t	*scsa2usb_bp_to_mblk(scsa2usb_state_t *);
156 int		scsa2usb_handle_data_start(scsa2usb_state_t *,
157 		    scsa2usb_cmd_t *, usb_bulk_req_t *);
158 void		scsa2usb_handle_data_done(scsa2usb_state_t *,
159 		    scsa2usb_cmd_t *cmd, usb_bulk_req_t *);
160 
161 usb_bulk_req_t *scsa2usb_init_bulk_req(scsa2usb_state_t *,
162 			    size_t, uint_t, usb_req_attrs_t, usb_flags_t);
163 int		scsa2usb_bulk_timeout(int);
164 int		scsa2usb_clear_ept_stall(scsa2usb_state_t *, uint_t,
165 		    usb_pipe_handle_t, char *);
166 static void	scsa2usb_pkt_completion(scsa2usb_state_t *, struct scsi_pkt *);
167 
168 /* event handling */
169 static int	scsa2usb_reconnect_event_cb(dev_info_t *);
170 static int	scsa2usb_disconnect_event_cb(dev_info_t *);
171 static int	scsa2usb_cpr_suspend(dev_info_t *);
172 static void	scsa2usb_cpr_resume(dev_info_t *);
173 static void	scsa2usb_restore_device_state(dev_info_t *, scsa2usb_state_t *);
174 
175 /* PM handling */
176 static void	scsa2usb_create_pm_components(dev_info_t *, scsa2usb_state_t *);
177 static void	scsa2usb_raise_power(scsa2usb_state_t *);
178 static int	scsa2usb_pwrlvl0(scsa2usb_state_t *);
179 static int	scsa2usb_pwrlvl1(scsa2usb_state_t *);
180 static int	scsa2usb_pwrlvl2(scsa2usb_state_t *);
181 static int	scsa2usb_pwrlvl3(scsa2usb_state_t *);
182 static int	scsa2usb_power(dev_info_t *, int comp, int level);
183 static void	scsa2usb_pm_busy_component(scsa2usb_state_t *);
184 static void	scsa2usb_pm_idle_component(scsa2usb_state_t *);
185 
186 /* external functions for Bulk only (BO) support */
187 extern int	scsa2usb_bulk_only_transport(scsa2usb_state_t *,
188 		    scsa2usb_cmd_t *);
189 extern int	scsa2usb_bulk_only_get_max_lun(scsa2usb_state_t *);
190 
191 /* external functions for CB/CBI support */
192 extern int	scsa2usb_cbi_transport(scsa2usb_state_t *, scsa2usb_cmd_t *);
193 extern void	scsa2usb_cbi_stop_intr_polling(scsa2usb_state_t *);
194 
195 
196 /* cmd decoding */
197 static char *scsa2usb_cmds[] = {
198 	"\000tur",
199 	"\001rezero",
200 	"\003rqsense",
201 	"\004format",
202 	"\014cartprot",
203 	"\022inquiry",
204 	"\026tranlba",
205 	"\030fmtverify",
206 	"\032modesense",
207 	"\033start",
208 	"\035snddiag",
209 	"\036doorlock",
210 	"\043formatcap",
211 	"\045readcap",
212 	"\050read10",
213 	"\052write10",
214 	"\053seek10",
215 	"\056writeverify",
216 	"\057verify",
217 	"\065synchcache",
218 	"\076readlong",
219 	"\077writelong",
220 	"\102readsubchan",
221 	"\103readtoc",
222 	"\104readhdr",
223 	"\105playaudio10",
224 	"\107playaudio_msf",
225 	"\110playaudio_ti",
226 	"\111playtrk_r10",
227 	"\112geteventnotify",
228 	"\113pause_resume",
229 	"\116stop/play_scan",
230 	"\121readdiscinfo",
231 	"\122readtrkinfo",
232 	"\123reservedtrk",
233 	"\124sendopcinfo",
234 	"\125modeselect",
235 	"\132modesense",
236 	"\133closetrksession",
237 	"\135sendcuesheet",
238 	"\136prin",
239 	"\137prout",
240 	"\241blankcd",
241 	"\245playaudio12",
242 	"\250read12",
243 	"\251playtrk12",
244 	"\252write12",
245 	"\254getperf",
246 	"\271readcdmsf",
247 	"\273setcdspeed",
248 	"\275mechanism_sts",
249 	"\276readcd",
250 	NULL
251 };
252 
253 
254 /*
255  * Mass-Storage devices masquerade as "sd" disks.
256  *
257  * These devices may not support all SCSI CDBs in their
258  * entirety due to their hardware implementation limitations.
259  *
260  * As such, following is a list of some of the black-listed
261  * devices w/ the attributes that they do not support.
262  * (See scsa2usb.h for description on each attribute)
263  */
264 #define	X	((uint16_t)(-1))
265 
266 static struct blacklist {
267 	uint16_t	idVendor;	/* vendor ID			*/
268 	uint16_t	idProduct;	/* product ID			*/
269 	uint16_t	bcdDevice;	/* device release number in bcd */
270 	uint16_t	attributes;	/* attributes to blacklist	*/
271 } scsa2usb_blacklist[] = {
272 	/* Iomega Zip100 drive (prototype) with flaky bridge */
273 	{MS_IOMEGA_VID, MS_IOMEGA_PID1_ZIP100, 0,
274 	    SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_PM},
275 
276 	/* Iomega Zip100 drive (newer model) with flaky bridge */
277 	{MS_IOMEGA_VID, MS_IOMEGA_PID2_ZIP100, 0,
278 	    SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_PM},
279 
280 	/* Iomega Zip100 drive (newer model) with flaky bridge */
281 	{MS_IOMEGA_VID, MS_IOMEGA_PID3_ZIP100, 0,
282 	    SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_PM},
283 
284 	/* Iomega Zip250 drive */
285 	{MS_IOMEGA_VID, MS_IOMEGA_PID_ZIP250, 0, SCSA2USB_ATTRS_GET_LUN},
286 
287 	/* Iomega Clik! drive */
288 	{MS_IOMEGA_VID, MS_IOMEGA_PID_CLIK, 0,
289 	    SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_START_STOP},
290 
291 	/* SMSC floppy Device - and its clones */
292 	{MS_SMSC_VID, X, 0, SCSA2USB_ATTRS_START_STOP},
293 
294 	/* Hagiwara SmartMedia Device */
295 	{MS_HAGIWARA_SYS_COM_VID, MS_HAGIWARA_SYSCOM_PID1, 0,
296 	    SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_START_STOP},
297 
298 	/* Hagiwara CompactFlash Device */
299 	{MS_HAGIWARA_SYS_COM_VID, MS_HAGIWARA_SYSCOM_PID2, 0,
300 	    SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_START_STOP},
301 
302 	/* Hagiwara SmartMedia/CompactFlash Combo Device */
303 	{MS_HAGIWARA_SYS_COM_VID, MS_HAGIWARA_SYSCOM_PID3, 0,
304 	    SCSA2USB_ATTRS_START_STOP},
305 
306 	/* Hagiwara new SM Device */
307 	{MS_HAGIWARA_SYS_COM_VID, MS_HAGIWARA_SYSCOM_PID4, 0,
308 	    SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_START_STOP},
309 
310 	/* Hagiwara new CF Device */
311 	{MS_HAGIWARA_SYS_COM_VID, MS_HAGIWARA_SYSCOM_PID5, 0,
312 	    SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_START_STOP},
313 
314 	/* Mitsumi CD-RW Device(s) */
315 	{MS_MITSUMI_VID, X, X, SCSA2USB_ATTRS_BIG_TIMEOUT |
316 	    SCSA2USB_ATTRS_GET_CONF | SCSA2USB_ATTRS_GET_PERF},
317 
318 	/* Neodio Technologies Corporation SM/CF/MS/SD Combo Device */
319 	{MS_NEODIO_VID, MS_NEODIO_DEVICE_3050, 0,
320 	    SCSA2USB_ATTRS_MODE_SENSE },
321 
322 	/* dumb flash devices */
323 	{MS_SONY_FLASH_VID, MS_SONY_FLASH_PID, 0,
324 	    SCSA2USB_ATTRS_REDUCED_CMD},
325 
326 	{MS_TREK_FLASH_VID, MS_TREK_FLASH_PID, 0,
327 	    SCSA2USB_ATTRS_REDUCED_CMD},
328 
329 	{MS_PENN_FLASH_VID, MS_PENN_FLASH_PID, 0,
330 	    SCSA2USB_ATTRS_REDUCED_CMD},
331 
332 	/* SimpleTech UCF-100 CF Device */
333 	{MS_SIMPLETECH_VID, MS_SIMPLETECH_PID1, 0,
334 	    SCSA2USB_ATTRS_REDUCED_CMD},
335 
336 	{MS_ADDONICS_CARD_READER_VID, MS_ADDONICS_CARD_READER_PID,
337 	    0, SCSA2USB_ATTRS_REDUCED_CMD},
338 
339 	/* Acomdata 80GB USB/1394 Hard Disk */
340 	{MS_ACOMDATA_VID, MS_ACOMDATA_PID1, 0,
341 	    SCSA2USB_ATTRS_USE_CSW_RESIDUE},
342 
343 	/* OTi6828 Flash Disk */
344 	{MS_OTI_VID, MS_OTI_DEVICE_6828, 0,
345 	    SCSA2USB_ATTRS_USE_CSW_RESIDUE},
346 
347 	/* AMI Virtual Floppy */
348 	{MS_AMI_VID, MS_AMI_VIRTUAL_FLOPPY, 0,
349 	    SCSA2USB_ATTRS_NO_MEDIA_CHECK},
350 };
351 
352 
353 #define	N_SCSA2USB_BLACKLIST (sizeof (scsa2usb_blacklist))/ \
354 				sizeof (struct blacklist)
355 
356 /*
357  * Attribute values can be overridden by values
358  * contained in the scsa2usb.conf file.
359  * These arrays define possible user input values.
360  */
361 
362 struct scsa2usb_subclass_protocol_override {
363 	char	*name;
364 	int	value;
365 };
366 
367 static struct scsa2usb_subclass_protocol_override scsa2usb_protocol[] =  {
368 	{"CB", SCSA2USB_CB_PROTOCOL},
369 	{"CBI", SCSA2USB_CBI_PROTOCOL},
370 	{"BO", SCSA2USB_BULK_ONLY_PROTOCOL}
371 };
372 
373 static struct scsa2usb_subclass_protocol_override scsa2usb_subclass[] = {
374 	{"SCSI", SCSA2USB_SCSI_CMDSET},
375 	{"ATAPI", SCSA2USB_ATAPI_CMDSET},
376 	{"UFI", SCSA2USB_UFI_CMDSET}
377 };
378 
379 
380 #define	N_SCSA2USB_SUBC_OVERRIDE (sizeof (scsa2usb_subclass))/ \
381 			sizeof (struct scsa2usb_subclass_protocol_override)
382 
383 #define	N_SCSA2USB_PROT_OVERRIDE (sizeof (scsa2usb_protocol))/ \
384 			sizeof (struct scsa2usb_subclass_protocol_override)
385 
386 /* global variables */
387 static void *scsa2usb_statep;				/* for soft state */
388 static boolean_t scsa2usb_sync_message = B_TRUE;	/* for syncing */
389 
390 /* for debug messages */
391 uint_t	scsa2usb_errmask	= (uint_t)DPRINT_MASK_ALL;
392 uint_t	scsa2usb_errlevel	= USB_LOG_L4;
393 uint_t	scsa2usb_instance_debug = (uint_t)-1;
394 uint_t	scsa2usb_scsi_bus_config_debug = 0;
395 uint_t	scsa2usb_long_timeout	= 50 * SCSA2USB_BULK_PIPE_TIMEOUT;
396 
397 
398 /*
399  * Some devices have problems with big bulk transfers,
400  * transfers >= 128kbytes hang the device.  This tunable allows to
401  * limit the maximum bulk transfers rate.
402  */
403 uint_t	scsa2usb_max_bulk_xfer_size = SCSA2USB_MAX_BULK_XFER_SIZE;
404 
405 
406 #ifdef	SCSA2USB_BULK_ONLY_TEST
407 /*
408  * Test BO 13 cases. (See USB Mass Storage Class - Bulk Only Transport).
409  * We are not covering test cases 1, 6, and 12 as these are the "good"
410  * test cases and are tested as part of the normal drive access operations.
411  *
412  * NOTE: This is for testing only. It will be replaced by a uscsi test.
413  * Some are listed here while; other test cases are moved to usb_bulkonly.c
414  */
415 static int scsa2usb_test_case_5 = 0;
416 int scsa2usb_test_case_8 = 0;
417 int scsa2usb_test_case_10 = 0;
418 static int scsa2usb_test_case_11 = 0;
419 
420 static void	scsa2usb_test_mblk(scsa2usb_state_t *, boolean_t);
421 #endif	/* SCSA2USB_BULK_ONLY_TEST */
422 
423 static int	scsa2usb_ugen_open(dev_t *, int, int, cred_t *);
424 static int	scsa2usb_ugen_close(dev_t, int, int, cred_t *);
425 static int	scsa2usb_ugen_strategy(struct buf *);
426 static int	scsa2usb_ugen_read(dev_t, struct uio *, cred_t *);
427 static int	scsa2usb_ugen_write(dev_t, struct uio *, cred_t *);
428 static int	scsa2usb_ugen_poll(dev_t, short, int,  short *,
429 						struct pollhead **);
430 
431 /* scsa2usb cb_ops */
432 static struct cb_ops scsa2usb_cbops = {
433 	scsa2usb_ugen_open,	/* open  */
434 	scsa2usb_ugen_close,	/* close */
435 	nodev,			/* strategy */
436 	nodev,			/* print */
437 	nodev,			/* dump */
438 	scsa2usb_ugen_read,	/* read */
439 	scsa2usb_ugen_write,	/* write */
440 	NULL,			/* ioctl */
441 	nodev,			/* devmap */
442 	nodev,			/* mmap */
443 	nodev,			/* segmap */
444 	scsa2usb_ugen_poll,	/* poll */
445 	ddi_prop_op,		/* prop_op */
446 	NULL,			/* stream */
447 	D_MP,			/* cb_flag */
448 	CB_REV, 		/* rev */
449 	nodev,			/* int (*cb_aread)() */
450 	nodev			/* int (*cb_awrite)() */
451 };
452 
453 /* modloading support */
454 static struct dev_ops scsa2usb_ops = {
455 	DEVO_REV,		/* devo_rev, */
456 	0,			/* refcnt  */
457 	scsa2usb_info,		/* info */
458 	nulldev,		/* identify */
459 	nulldev,		/* probe */
460 	scsa2usb_attach,	/* attach */
461 	scsa2usb_detach,	/* detach */
462 	nodev,			/* reset */
463 	&scsa2usb_cbops,	/* driver operations */
464 	NULL,			/* bus operations */
465 	scsa2usb_power		/* power */
466 };
467 
468 static struct modldrv modldrv = {
469 	&mod_driverops,			/* Module type. This one is a driver */
470 	"SCSA to USB Driver %I%",	/* Name of the module. */
471 	&scsa2usb_ops,			/* driver ops */
472 };
473 
474 static struct modlinkage modlinkage = {
475 	MODREV_1, (void *)&modldrv, NULL
476 };
477 
478 /* event support */
479 static usb_event_t scsa2usb_events = {
480 	scsa2usb_disconnect_event_cb,
481 	scsa2usb_reconnect_event_cb,
482 	NULL, NULL
483 };
484 
485 int
486 _init(void)
487 {
488 	int rval;
489 
490 	if (((rval = ddi_soft_state_init(&scsa2usb_statep,
491 	    sizeof (scsa2usb_state_t), SCSA2USB_INITIAL_ALLOC)) != 0)) {
492 
493 		return (rval);
494 	}
495 
496 	if ((rval = scsi_hba_init(&modlinkage)) != 0) {
497 		ddi_soft_state_fini(&scsa2usb_statep);
498 
499 		return (rval);
500 	}
501 
502 	if ((rval = mod_install(&modlinkage)) != 0) {
503 		scsi_hba_fini(&modlinkage);
504 		ddi_soft_state_fini(&scsa2usb_statep);
505 
506 		return (rval);
507 	}
508 
509 	return (rval);
510 }
511 
512 
513 int
514 _fini(void)
515 {
516 	int	rval;
517 
518 	if ((rval = mod_remove(&modlinkage)) == 0) {
519 		scsi_hba_fini(&modlinkage);
520 		ddi_soft_state_fini(&scsa2usb_statep);
521 	}
522 
523 	return (rval);
524 }
525 
526 
527 int
528 _info(struct modinfo *modinfop)
529 {
530 	return (mod_info(&modlinkage, modinfop));
531 }
532 
533 
534 /*
535  * scsa2usb_info :
536  *	Get minor number, soft state structure etc.
537  */
538 /*ARGSUSED*/
539 static int
540 scsa2usb_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
541     void *arg, void **result)
542 {
543 	scsa2usb_state_t *scsa2usbp = NULL;
544 	int error = DDI_FAILURE;
545 	int instance = SCSA2USB_MINOR_TO_INSTANCE(getminor((dev_t)arg));
546 
547 	switch (infocmd) {
548 	case DDI_INFO_DEVT2DEVINFO:
549 		if (((scsa2usbp = ddi_get_soft_state(scsa2usb_statep,
550 		    instance)) != NULL) &&
551 		    scsa2usbp->scsa2usb_dip) {
552 			*result = scsa2usbp->scsa2usb_dip;
553 			error = DDI_SUCCESS;
554 		} else {
555 			*result = NULL;
556 		}
557 		break;
558 	case DDI_INFO_DEVT2INSTANCE:
559 		*result = (void *)(uintptr_t)instance;
560 		error = DDI_SUCCESS;
561 		break;
562 	default:
563 		break;
564 	}
565 
566 	return (error);
567 }
568 
569 
570 /*
571  * scsa2usb_attach:
572  *	Attach driver
573  *	Allocate a "scsi_hba_tran" - call scsi_hba_tran_alloc()
574  *	Invoke scsi_hba_attach_setup
575  *	Get the serialno of the device
576  *	Open bulk pipes
577  *	Create disk child(ren)
578  *	Register events
579  *	Create and register panic callback
580  *
581  * NOTE: Replaced CBW_DIR_OUT with USB_EP_DIR_OUT and CBW_DIR_IN with
582  * USB_EP_DIR_IN as they are the same #defines.
583  */
584 static int
585 scsa2usb_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
586 {
587 	int			instance = ddi_get_instance(dip);
588 	int			interface;
589 	uint_t			lun;
590 	boolean_t		ept_check = B_TRUE;
591 	scsi_hba_tran_t		*tran;		/* scsi transport */
592 	scsa2usb_state_t	*scsa2usbp;
593 	usb_log_handle_t	log_handle;
594 	usb_ep_data_t		*ep_data;
595 	usb_client_dev_data_t	*dev_data;
596 	usb_alt_if_data_t	*altif_data;
597 	usb_ugen_info_t 	usb_ugen_info;
598 
599 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, NULL,
600 	    "scsa2usb_attach: dip = 0x%p", dip);
601 
602 	switch (cmd) {
603 	case DDI_ATTACH:
604 		break;
605 	case DDI_RESUME:
606 		scsa2usb_cpr_resume(dip);
607 
608 		return (DDI_SUCCESS);
609 	default:
610 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, NULL,
611 		    "scsa2usb_attach: failed");
612 
613 		return (DDI_FAILURE);
614 	}
615 
616 	/* Allocate softc information */
617 	if (ddi_soft_state_zalloc(scsa2usb_statep, instance) != DDI_SUCCESS) {
618 		ddi_prop_remove_all(dip);
619 
620 		return (DDI_FAILURE);
621 	}
622 
623 	/* get soft state space and initialize */
624 	if ((scsa2usbp = ddi_get_soft_state(scsa2usb_statep,
625 	    instance)) == NULL) {
626 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, NULL,
627 		    "scsa2usb%d: bad soft state", instance);
628 		ddi_prop_remove_all(dip);
629 
630 		return (DDI_FAILURE);
631 	}
632 
633 	scsa2usbp->scsa2usb_dip 	= dip;
634 	scsa2usbp->scsa2usb_instance	= instance;
635 
636 	/* allocate a log handle for debug/error messages */
637 	scsa2usbp->scsa2usb_log_handle = log_handle =
638 	    usb_alloc_log_hdl(dip, "s2u",
639 				&scsa2usb_errlevel,
640 				&scsa2usb_errmask, &scsa2usb_instance_debug,
641 				0);
642 
643 	/* attach to USBA */
644 	if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) {
645 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
646 		    "usb_client_attach failed");
647 
648 		goto fail;
649 	}
650 	if (usb_get_dev_data(dip, &dev_data, USB_PARSE_LVL_IF, 0) !=
651 	    USB_SUCCESS) {
652 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
653 		    "usb_get_dev_data failed");
654 
655 		goto fail;
656 	}
657 
658 	/* initialize the mutex with the right cookie */
659 	mutex_init(&scsa2usbp->scsa2usb_mutex, NULL, MUTEX_DRIVER,
660 					dev_data->dev_iblock_cookie);
661 	cv_init(&scsa2usbp->scsa2usb_transport_busy_cv, NULL, CV_DRIVER, NULL);
662 
663 	for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
664 		usba_init_list(&scsa2usbp->scsa2usb_waitQ[lun], NULL,
665 					dev_data->dev_iblock_cookie);
666 	}
667 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
668 	scsa2usbp->scsa2usb_dip 	= dip;
669 	scsa2usbp->scsa2usb_instance	= instance;
670 	scsa2usbp->scsa2usb_attrs	= SCSA2USB_ALL_ATTRS;
671 	scsa2usbp->scsa2usb_dev_data	= dev_data;
672 
673 
674 	/* save the default pipe handle */
675 	scsa2usbp->scsa2usb_default_pipe = dev_data->dev_default_ph;
676 
677 	/* basic inits are done */
678 	scsa2usbp->scsa2usb_flags |= SCSA2USB_FLAGS_LOCKS_INIT;
679 
680 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, log_handle,
681 	    "curr_cfg=%d, curr_if=%d",
682 	    dev_data->dev_curr_cfg - &dev_data->dev_cfg[0],
683 	    dev_data->dev_curr_if);
684 
685 	interface = dev_data->dev_curr_if;
686 	scsa2usbp->scsa2usb_intfc_num = dev_data->dev_curr_if;
687 
688 	/* now find out relevant descriptors for alternate 0 */
689 	altif_data = &dev_data->dev_curr_cfg->cfg_if[interface].if_alt[0];
690 
691 	if (altif_data->altif_n_ep == 0) {
692 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
693 		    "invalid alt 0 for interface %d", interface);
694 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
695 
696 		goto fail;
697 	}
698 
699 	/* All CB/CBI, BO devices should have this value set */
700 	if (altif_data->altif_descr.bInterfaceClass !=
701 	    USB_CLASS_MASS_STORAGE) {
702 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
703 		    "invalid interface class (0x%x)",
704 		    altif_data->altif_descr.bInterfaceClass);
705 	}
706 	scsa2usbp->scsa2usb_intfc_descr = altif_data->altif_descr;
707 
708 	/* figure out the endpoints and copy the descr */
709 	if ((ep_data = usb_lookup_ep_data(dip, dev_data, interface, 0, 0,
710 	    USB_EP_ATTR_BULK, USB_EP_DIR_OUT)) != NULL) {
711 		scsa2usbp->scsa2usb_bulkout_ept = ep_data->ep_descr;
712 	}
713 	if ((ep_data = usb_lookup_ep_data(dip, dev_data, interface, 0, 0,
714 	    USB_EP_ATTR_BULK, USB_EP_DIR_IN)) != NULL) {
715 		scsa2usbp->scsa2usb_bulkin_ept = ep_data->ep_descr;
716 	}
717 	if ((ep_data = usb_lookup_ep_data(dip, dev_data, interface, 0, 0,
718 	    USB_EP_ATTR_INTR, USB_EP_DIR_IN)) != NULL) {
719 		scsa2usbp->scsa2usb_intr_ept = ep_data->ep_descr;
720 	}
721 
722 	/*
723 	 * check here for protocol and subclass supported by this driver
724 	 *
725 	 * first check if conf file has override values
726 	 * Note: override values are not used if supplied values are legal
727 	 */
728 	scsa2usb_override(scsa2usbp);
729 
730 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, log_handle,
731 	    "protocol=0x%x override=0x%x subclass=0x%x override=0x%x",
732 	    scsa2usbp->scsa2usb_intfc_descr.bInterfaceProtocol,
733 	    scsa2usbp->scsa2usb_protocol_override,
734 	    scsa2usbp->scsa2usb_intfc_descr.bInterfaceSubClass,
735 	    scsa2usbp->scsa2usb_subclass_override);
736 
737 	switch (scsa2usbp->scsa2usb_intfc_descr.bInterfaceProtocol) {
738 	case USB_PROTO_MS_CBI:
739 		scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_CB_PROTOCOL;
740 		break;
741 	case USB_PROTO_MS_CBI_WC:
742 		scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_CBI_PROTOCOL;
743 		break;
744 	case USB_PROTO_MS_ISD_1999_SILICN:
745 	case USB_PROTO_MS_BULK_ONLY:
746 		scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_BULK_ONLY_PROTOCOL;
747 		break;
748 	default:
749 		if (scsa2usbp->scsa2usb_protocol_override) {
750 			scsa2usbp->scsa2usb_cmd_protocol |=
751 			    scsa2usbp->scsa2usb_protocol_override;
752 			USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
753 			    "overriding protocol %x",
754 			    scsa2usbp->scsa2usb_intfc_descr.bInterfaceProtocol);
755 			break;
756 		}
757 
758 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
759 		    "unsupported protocol = %x",
760 		    scsa2usbp->scsa2usb_intfc_descr.bInterfaceProtocol);
761 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
762 
763 		goto fail;
764 	}
765 
766 	switch (scsa2usbp->scsa2usb_intfc_descr.bInterfaceSubClass) {
767 	case USB_SUBCLS_MS_SCSI:		/* transparent SCSI */
768 		scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_SCSI_CMDSET;
769 		break;
770 	case USB_SUBCLS_MS_SFF8020I:
771 	case USB_SUBCLS_MS_SFF8070I:
772 		scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_ATAPI_CMDSET;
773 		break;
774 	case USB_SUBCLS_MS_UFI:		/* UFI */
775 		scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_UFI_CMDSET;
776 		break;
777 	default:
778 		if (scsa2usbp->scsa2usb_subclass_override) {
779 			scsa2usbp->scsa2usb_cmd_protocol |=
780 			    scsa2usbp->scsa2usb_subclass_override;
781 			USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
782 			    "overriding subclass %x",
783 			    scsa2usbp->scsa2usb_intfc_descr.bInterfaceSubClass);
784 			break;
785 		}
786 
787 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
788 		    "unsupported subclass = %x",
789 		    scsa2usbp->scsa2usb_intfc_descr.bInterfaceSubClass);
790 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
791 
792 		goto fail;
793 	}
794 
795 	/* check that we have the right set of endpoint descriptors */
796 	if (SCSA2USB_IS_BULK_ONLY(scsa2usbp) || SCSA2USB_IS_CB(scsa2usbp)) {
797 		if ((scsa2usbp->scsa2usb_bulkout_ept.bLength == 0) ||
798 		    (scsa2usbp->scsa2usb_bulkin_ept.bLength == 0)) {
799 			ept_check = B_FALSE;
800 		}
801 	} else if (SCSA2USB_IS_CBI(scsa2usbp)) {
802 		if ((scsa2usbp->scsa2usb_bulkout_ept.bLength == 0) ||
803 		    (scsa2usbp->scsa2usb_bulkin_ept.bLength == 0) ||
804 		    (scsa2usbp->scsa2usb_intr_ept.bLength == 0)) {
805 			ept_check = B_FALSE;
806 		}
807 	}
808 
809 	if (ept_check == B_FALSE) {
810 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
811 		    "scsa2usb%d doesn't support minimum required endpoints",
812 		    instance);
813 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
814 
815 		goto fail;
816 	}
817 
818 	/*
819 	 * Validate the black-listed attributes
820 	 */
821 	scsa2usb_validate_attrs(scsa2usbp);
822 
823 	/* Print the serial number from the registration data */
824 	if (scsa2usbp->scsa2usb_dev_data->dev_serial) {
825 		USB_DPRINTF_L4(DPRINT_MASK_SCSA,
826 		    scsa2usbp->scsa2usb_log_handle, "Serial Number = %s",
827 		    scsa2usbp->scsa2usb_dev_data->dev_serial);
828 	}
829 
830 	/*
831 	 * Allocate a SCSA transport structure
832 	 */
833 	tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
834 	scsa2usbp->scsa2usb_tran = tran;
835 
836 	/*
837 	 * initialize transport structure
838 	 */
839 	tran->tran_hba_private		= scsa2usbp;
840 	tran->tran_tgt_private		= NULL;
841 	tran->tran_tgt_init		= scsa2usb_scsi_tgt_init;
842 	tran->tran_tgt_probe		= scsa2usb_scsi_tgt_probe;
843 	tran->tran_tgt_free		= scsa2usb_scsi_tgt_free;
844 	tran->tran_start		= scsa2usb_scsi_start;
845 	tran->tran_abort		= scsa2usb_scsi_abort;
846 	tran->tran_reset		= scsa2usb_scsi_reset;
847 	tran->tran_getcap		= scsa2usb_scsi_getcap;
848 	tran->tran_setcap		= scsa2usb_scsi_setcap;
849 	tran->tran_init_pkt		= scsa2usb_scsi_init_pkt;
850 	tran->tran_destroy_pkt		= scsa2usb_scsi_destroy_pkt;
851 	tran->tran_dmafree		= NULL;
852 	tran->tran_sync_pkt		= NULL;
853 	tran->tran_reset_notify		= NULL;
854 	tran->tran_get_bus_addr		= NULL;
855 	tran->tran_get_name		= NULL;
856 	tran->tran_quiesce		= NULL;
857 	tran->tran_unquiesce		= NULL;
858 	tran->tran_bus_reset		= NULL;
859 	tran->tran_add_eventcall	= NULL;
860 	tran->tran_get_eventcookie	= NULL;
861 	tran->tran_post_event		= NULL;
862 	tran->tran_remove_eventcall	= NULL;
863 	tran->tran_bus_config		= scsa2usb_scsi_bus_config;
864 	tran->tran_bus_unconfig		= scsa2usb_scsi_bus_unconfig;
865 
866 	/*
867 	 * register with SCSA as an HBA
868 	 * Note that the dma attributes are from parent nexus
869 	 */
870 	if (scsi_hba_attach_setup(dip, usba_get_hc_dma_attr(dip), tran, 0)) {
871 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
872 		    "scsi_hba_attach_setup failed");
873 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
874 
875 		goto fail;
876 	}
877 
878 	scsa2usbp->scsa2usb_flags |= SCSA2USB_FLAGS_HBA_ATTACH_SETUP;
879 
880 	/* create minor node */
881 	if (ddi_create_minor_node(dip, "scsa2usb", S_IFCHR,
882 	    instance << SCSA2USB_MINOR_INSTANCE_SHIFT,
883 	    DDI_NT_SCSI_NEXUS, 0) != DDI_SUCCESS) {
884 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
885 		    "scsi_attach: ddi_create_minor_node failed");
886 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
887 
888 		goto fail;
889 	}
890 
891 	/* open pipes and set scsa2usb_flags */
892 	if (scsa2usb_open_usb_pipes(scsa2usbp) == USB_FAILURE) {
893 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
894 		    "error opening pipes");
895 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
896 
897 		goto fail;
898 	}
899 
900 	/* set default block size. updated after read cap cmd */
901 	for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
902 		scsa2usbp->scsa2usb_lbasize[lun] = DEV_BSIZE;
903 	}
904 
905 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
906 
907 	/* initialize PANIC callback */
908 	scsa2usb_panic_callb_init(scsa2usbp);
909 
910 	/* finally we are all done 'initializing' the device */
911 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
912 	scsa2usbp->scsa2usb_dev_state = USB_DEV_ONLINE;
913 
914 	/* enable PM, mutex needs to be held across this */
915 	scsa2usb_create_pm_components(dip, scsa2usbp);
916 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
917 
918 	/* register for connect/disconnect events */
919 	if (usb_register_event_cbs(scsa2usbp->scsa2usb_dip, &scsa2usb_events,
920 	    0) != USB_SUCCESS) {
921 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
922 		    "error cb registering");
923 		goto fail;
924 	}
925 
926 	/* free the dev_data tree, we no longer need it */
927 	usb_free_descr_tree(dip, dev_data);
928 
929 	scsa2usb_pm_idle_component(scsa2usbp);
930 
931 	/* log the conf file override string if there is one */
932 	if (scsa2usbp->scsa2usb_override_str) {
933 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
934 		    "scsa2usb.conf override: %s",
935 		    scsa2usbp->scsa2usb_override_str);
936 	}
937 
938 	if (usb_owns_device(dip)) {
939 		/* get a ugen handle */
940 		bzero(&usb_ugen_info, sizeof (usb_ugen_info));
941 		usb_ugen_info.usb_ugen_flags = 0;
942 		usb_ugen_info.usb_ugen_minor_node_ugen_bits_mask =
943 				(dev_t)SCSA2USB_MINOR_UGEN_BITS_MASK;
944 		usb_ugen_info.usb_ugen_minor_node_instance_mask =
945 				(dev_t)~SCSA2USB_MINOR_UGEN_BITS_MASK;
946 		scsa2usbp->scsa2usb_ugen_hdl =
947 					usb_ugen_get_hdl(dip, &usb_ugen_info);
948 
949 		if (usb_ugen_attach(scsa2usbp->scsa2usb_ugen_hdl, cmd) !=
950 		    USB_SUCCESS) {
951 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
952 			    scsa2usbp->scsa2usb_log_handle,
953 			    "usb_ugen_attach failed");
954 
955 			usb_ugen_release_hdl(scsa2usbp->scsa2usb_ugen_hdl);
956 			scsa2usbp->scsa2usb_ugen_hdl = NULL;
957 		}
958 	}
959 
960 	/* report device */
961 	ddi_report_dev(dip);
962 
963 	return (DDI_SUCCESS);
964 
965 fail:
966 	if (scsa2usbp) {
967 		(void) scsa2usb_cleanup(dip, scsa2usbp);
968 	}
969 
970 	return (DDI_FAILURE);
971 }
972 
973 
974 /*
975  * scsa2usb_detach:
976  *	detach or suspend driver instance
977  */
978 static int
979 scsa2usb_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
980 {
981 	scsi_hba_tran_t	*tran;
982 	scsa2usb_state_t *scsa2usbp;
983 	int rval;
984 
985 	tran = ddi_get_driver_private(dip);
986 	ASSERT(tran != NULL);
987 
988 	scsa2usbp = (scsa2usb_state_t *)tran->tran_hba_private;
989 	ASSERT(scsa2usbp);
990 
991 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
992 	    "scsa2usb_detach: dip = 0x%p, cmd = %d", dip, cmd);
993 
994 	switch (cmd) {
995 	case DDI_DETACH:
996 
997 		if (scsa2usb_cleanup(dip, scsa2usbp) != USB_SUCCESS) {
998 
999 			return (DDI_FAILURE);
1000 		}
1001 
1002 		return (DDI_SUCCESS);
1003 	case DDI_SUSPEND:
1004 		rval = scsa2usb_cpr_suspend(dip);
1005 
1006 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
1007 	default:
1008 
1009 		return (DDI_FAILURE);
1010 	}
1011 }
1012 
1013 
1014 /*
1015  * ugen support
1016  */
1017 /*
1018  * scsa2usb_ugen_open()
1019  * (all ugen opens and pipe opens are by definition exclusive so it is OK
1020  * to count opens)
1021  */
1022 static int
1023 scsa2usb_ugen_open(dev_t *devp, int flag, int sflag, cred_t *cr)
1024 {
1025 	scsa2usb_state_t *scsa2usbp;
1026 	int		rval;
1027 
1028 	if ((scsa2usbp = ddi_get_soft_state(scsa2usb_statep,
1029 	    SCSA2USB_MINOR_TO_INSTANCE(getminor(*devp)))) == NULL) {
1030 		/* deferred detach */
1031 
1032 		return (ENXIO);
1033 	}
1034 
1035 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1036 	    "scsa2usb_ugen_open: dev_t=0x%lx", *devp);
1037 
1038 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
1039 
1040 	/* if this is the first ugen open, check on transport busy */
1041 	if (scsa2usbp->scsa2usb_ugen_open_count == 0) {
1042 		while (scsa2usbp->scsa2usb_transport_busy ||
1043 		    (scsa2usb_all_waitQs_empty(scsa2usbp) !=
1044 		    USB_SUCCESS)) {
1045 			rval = cv_wait_sig(
1046 				&scsa2usbp->scsa2usb_transport_busy_cv,
1047 				&scsa2usbp->scsa2usb_mutex);
1048 			if (rval == 0) {
1049 				mutex_exit(&scsa2usbp->scsa2usb_mutex);
1050 
1051 				return (EINTR);
1052 			}
1053 		}
1054 		scsa2usbp->scsa2usb_transport_busy++;
1055 		scsa2usbp->scsa2usb_busy_thread = curthread;
1056 	}
1057 	scsa2usbp->scsa2usb_ugen_open_count++;
1058 
1059 	scsa2usb_raise_power(scsa2usbp);
1060 
1061 	scsa2usb_close_usb_pipes(scsa2usbp);
1062 
1063 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
1064 
1065 	rval = usb_ugen_open(scsa2usbp->scsa2usb_ugen_hdl, devp, flag,
1066 		sflag, cr);
1067 
1068 	if (rval) {
1069 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
1070 
1071 		/* reopen the pipes */
1072 		if (--scsa2usbp->scsa2usb_ugen_open_count == 0) {
1073 			scsa2usbp->scsa2usb_transport_busy--;
1074 			scsa2usbp->scsa2usb_busy_thread = NULL;
1075 			cv_signal(&scsa2usbp->scsa2usb_transport_busy_cv);
1076 		}
1077 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
1078 
1079 		scsa2usb_pm_idle_component(scsa2usbp);
1080 	}
1081 
1082 	return (rval);
1083 }
1084 
1085 
1086 /*
1087  * scsa2usb_ugen_close()
1088  */
1089 static int
1090 scsa2usb_ugen_close(dev_t dev, int flag, int otype, cred_t *cr)
1091 {
1092 	int rval;
1093 
1094 	scsa2usb_state_t *scsa2usbp = ddi_get_soft_state(scsa2usb_statep,
1095 			SCSA2USB_MINOR_TO_INSTANCE(getminor(dev)));
1096 
1097 	if (scsa2usbp == NULL) {
1098 
1099 		return (ENXIO);
1100 	}
1101 
1102 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1103 	    "scsa2usb_ugen_close: dev_t=0x%lx", dev);
1104 
1105 	rval = usb_ugen_close(scsa2usbp->scsa2usb_ugen_hdl, dev, flag,
1106 		otype, cr);
1107 
1108 	if (rval == 0) {
1109 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
1110 
1111 		/* reopen the pipes */
1112 		if (--scsa2usbp->scsa2usb_ugen_open_count == 0) {
1113 			scsa2usbp->scsa2usb_transport_busy--;
1114 			scsa2usbp->scsa2usb_busy_thread = NULL;
1115 			cv_signal(&scsa2usbp->scsa2usb_transport_busy_cv);
1116 		}
1117 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
1118 
1119 		scsa2usb_pm_idle_component(scsa2usbp);
1120 	}
1121 
1122 	return (rval);
1123 }
1124 
1125 
1126 /*
1127  * scsa2usb_ugen_read/write()
1128  */
1129 /*ARGSUSED*/
1130 static int
1131 scsa2usb_ugen_read(dev_t dev, struct uio *uiop, cred_t *credp)
1132 {
1133 	scsa2usb_state_t *scsa2usbp = ddi_get_soft_state(scsa2usb_statep,
1134 			SCSA2USB_MINOR_TO_INSTANCE(getminor(dev)));
1135 
1136 	if (scsa2usbp == NULL) {
1137 
1138 		return (ENXIO);
1139 	}
1140 
1141 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1142 	    "scsa2usb_ugen_read: dev_t=0x%lx", dev);
1143 
1144 
1145 	return (usb_ugen_read(scsa2usbp->scsa2usb_ugen_hdl, dev,
1146 					uiop, credp));
1147 }
1148 
1149 
1150 /*ARGSUSED*/
1151 static int
1152 scsa2usb_ugen_write(dev_t dev, struct uio *uiop, cred_t *credp)
1153 {
1154 	scsa2usb_state_t *scsa2usbp = ddi_get_soft_state(scsa2usb_statep,
1155 			SCSA2USB_MINOR_TO_INSTANCE(getminor(dev)));
1156 
1157 	if (scsa2usbp == NULL) {
1158 
1159 		return (ENXIO);
1160 	}
1161 
1162 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1163 	    "scsa2usb_ugen_write: dev_t=0x%lx", dev);
1164 
1165 	return (usb_ugen_write(scsa2usbp->scsa2usb_ugen_hdl,
1166 					dev, uiop, credp));
1167 }
1168 
1169 
1170 /*
1171  * scsa2usb_ugen_poll
1172  */
1173 static int
1174 scsa2usb_ugen_poll(dev_t dev, short events,
1175     int anyyet,  short *reventsp, struct pollhead **phpp)
1176 {
1177 	scsa2usb_state_t *scsa2usbp = ddi_get_soft_state(scsa2usb_statep,
1178 			SCSA2USB_MINOR_TO_INSTANCE(getminor(dev)));
1179 
1180 	if (scsa2usbp == NULL) {
1181 
1182 		return (ENXIO);
1183 	}
1184 
1185 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1186 	    "scsa2usb_ugen_poll: dev_t=0x%lx", dev);
1187 
1188 	return (usb_ugen_poll(scsa2usbp->scsa2usb_ugen_hdl, dev, events,
1189 					anyyet, reventsp, phpp));
1190 }
1191 
1192 
1193 /*
1194  * scsa2usb_cleanup:
1195  *	cleanup whatever attach has setup
1196  */
1197 static int
1198 scsa2usb_cleanup(dev_info_t *dip, scsa2usb_state_t *scsa2usbp)
1199 {
1200 	int		rval, i;
1201 	scsa2usb_power_t *pm;
1202 	uint_t		lun;
1203 
1204 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1205 	    "scsa2usb_cleanup:");
1206 
1207 	/* wait till the work thread is done */
1208 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
1209 	for (i = 0; i < SCSA2USB_DRAIN_TIMEOUT; i++) {
1210 		if (scsa2usbp->scsa2usb_work_thread_id == NULL) {
1211 
1212 			break;
1213 		}
1214 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
1215 		delay(drv_usectohz(1000000));
1216 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
1217 	}
1218 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
1219 
1220 	if (i >= SCSA2USB_DRAIN_TIMEOUT) {
1221 
1222 		return (USB_FAILURE);
1223 	}
1224 
1225 	/*
1226 	 * Disable the event callbacks first, after this point, event
1227 	 * callbacks will never get called. Note we shouldn't hold
1228 	 * mutex while unregistering events because there may be a
1229 	 * competing event callback thread. Event callbacks are done
1230 	 * with ndi mutex held and this can cause a potential deadlock.
1231 	 */
1232 	usb_unregister_event_cbs(scsa2usbp->scsa2usb_dip, &scsa2usb_events);
1233 
1234 	if (scsa2usbp->scsa2usb_flags & SCSA2USB_FLAGS_LOCKS_INIT) {
1235 		/*
1236 		 * if a waitQ exists, get rid of it before destroying it
1237 		 */
1238 		for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
1239 			scsa2usb_flush_waitQ(scsa2usbp, lun, CMD_TRAN_ERR);
1240 			usba_destroy_list(&scsa2usbp->scsa2usb_waitQ[lun]);
1241 		}
1242 
1243 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
1244 		if (scsa2usbp->scsa2usb_flags &
1245 		    SCSA2USB_FLAGS_HBA_ATTACH_SETUP) {
1246 			(void) scsi_hba_detach(dip);
1247 			scsi_hba_tran_free(scsa2usbp->scsa2usb_tran);
1248 		}
1249 
1250 		if (scsa2usbp->scsa2usb_flags &
1251 		    SCSA2USB_FLAGS_PIPES_OPENED) {
1252 			scsa2usb_close_usb_pipes(scsa2usbp);
1253 		}
1254 
1255 		/* Lower the power */
1256 		pm = scsa2usbp->scsa2usb_pm;
1257 
1258 		if (pm && (scsa2usbp->scsa2usb_dev_state !=
1259 		    USB_DEV_DISCONNECTED)) {
1260 			if (pm->scsa2usb_wakeup_enabled) {
1261 				mutex_exit(&scsa2usbp->scsa2usb_mutex);
1262 				(void) pm_raise_power(dip, 0,
1263 						USB_DEV_OS_FULL_PWR);
1264 
1265 				if ((rval = usb_handle_remote_wakeup(dip,
1266 				    USB_REMOTE_WAKEUP_DISABLE)) !=
1267 				    USB_SUCCESS) {
1268 					USB_DPRINTF_L2(DPRINT_MASK_SCSA,
1269 					    scsa2usbp->scsa2usb_log_handle,
1270 					    "disable remote wakeup failed "
1271 					    "(%d)", rval);
1272 				}
1273 			} else {
1274 				mutex_exit(&scsa2usbp->scsa2usb_mutex);
1275 			}
1276 
1277 			(void) pm_lower_power(dip, 0, USB_DEV_OS_PWR_OFF);
1278 
1279 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
1280 		}
1281 
1282 		if (pm) {
1283 			kmem_free(pm, sizeof (scsa2usb_power_t));
1284 		}
1285 
1286 		if (scsa2usbp->scsa2usb_override_str) {
1287 			kmem_free(scsa2usbp->scsa2usb_override_str,
1288 			    strlen(scsa2usbp->scsa2usb_override_str) + 1);
1289 			scsa2usbp->scsa2usb_override_str = NULL;
1290 		}
1291 
1292 		/* remove the minor nodes */
1293 		ddi_remove_minor_node(dip, NULL);
1294 
1295 		/* Cancel the registered panic callback */
1296 		scsa2usb_panic_callb_fini(scsa2usbp);
1297 
1298 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
1299 
1300 		mutex_destroy(&scsa2usbp->scsa2usb_mutex);
1301 		cv_destroy(&scsa2usbp->scsa2usb_transport_busy_cv);
1302 	}
1303 
1304 	usb_client_detach(scsa2usbp->scsa2usb_dip,
1305 				scsa2usbp->scsa2usb_dev_data);
1306 
1307 	if (scsa2usbp->scsa2usb_ugen_hdl) {
1308 		(void) usb_ugen_detach(scsa2usbp->scsa2usb_ugen_hdl,
1309 							DDI_DETACH);
1310 		usb_ugen_release_hdl(scsa2usbp->scsa2usb_ugen_hdl);
1311 	}
1312 
1313 	usb_free_log_hdl(scsa2usbp->scsa2usb_log_handle);
1314 
1315 	ddi_prop_remove_all(dip);
1316 
1317 	ddi_soft_state_free(scsa2usb_statep, ddi_get_instance(dip));
1318 
1319 	return (USB_SUCCESS);
1320 }
1321 
1322 
1323 /*
1324  * scsa2usb_override:
1325  *	some devices may be attached even though their subclass or
1326  *	protocol info is not according to spec.
1327  *	these can be determined by the 'subclass-protocol-override'
1328  *	property set in the conf file.
1329  */
1330 static void
1331 scsa2usb_override(scsa2usb_state_t *scsa2usbp)
1332 {
1333 	scsa2usb_ov_t ov;
1334 	char	**override_str = NULL;
1335 	char	*override_str_cpy;
1336 	uint_t	override_str_len, override_str_cpy_len;
1337 	uint_t	i;
1338 	usb_dev_descr_t *descr = scsa2usbp->scsa2usb_dev_data->dev_descr;
1339 
1340 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
1341 
1342 	scsa2usbp->scsa2usb_subclass_override =
1343 	    scsa2usbp->scsa2usb_protocol_override = 0;
1344 
1345 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, scsa2usbp->scsa2usb_dip,
1346 	    DDI_PROP_DONTPASS, "attribute-override-list",
1347 	    &override_str, &override_str_len) != DDI_PROP_SUCCESS) {
1348 
1349 		return;
1350 	}
1351 
1352 	/* parse each string in the subclass-protocol-override property */
1353 	for (i = 0; i < override_str_len; i++) {
1354 
1355 		USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1356 		    "override_str[%d] = %s", i, override_str[i]);
1357 
1358 		/*
1359 		 * save a copy of the override string for possible
1360 		 * inclusion in soft state later
1361 		 */
1362 		override_str_cpy_len = strlen(override_str[i]) + 1;
1363 		override_str_cpy = kmem_zalloc(override_str_cpy_len, KM_SLEEP);
1364 		(void) strcpy(override_str_cpy, override_str[i]);
1365 
1366 		bzero(&ov, sizeof (scsa2usb_ov_t));
1367 
1368 		if (scsa2usb_parse_input_str(override_str[i], &ov,
1369 		    scsa2usbp) == USB_FAILURE) {
1370 			kmem_free(override_str_cpy, override_str_cpy_len);
1371 			continue;
1372 		}
1373 
1374 		/*
1375 		 * see if subclass/protocol needs to be overridden for device
1376 		 * or if device should not be power managed
1377 		 * if there'a a match, save the override string in soft state
1378 		 */
1379 		if (((descr->idVendor == (uint16_t)ov.vid) || (ov.vid == 0)) &&
1380 		    ((descr->idProduct == (uint16_t)ov.pid) || (ov.pid == 0)) &&
1381 		    ((descr->bcdDevice == (uint16_t)ov.rev) || (ov.rev == 0))) {
1382 			scsa2usbp->scsa2usb_subclass_override = ov.subclass;
1383 			scsa2usbp->scsa2usb_protocol_override = ov.protocol;
1384 
1385 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
1386 			    scsa2usbp->scsa2usb_log_handle,
1387 			    "vid=0x%x pid=0x%x rev=0x%x subclass=0x%x "
1388 			    "protocol=0x%x "
1389 			    "pmoff=%d fake_removable=%d modesense=%d "
1390 			    "reduced-cmd-support=%d",
1391 			    ov.vid, ov.pid, ov.rev, ov.subclass, ov.protocol,
1392 			    ov.pmoff, ov.fake_removable, ov.no_modesense,
1393 			    ov.reduced_cmd_support);
1394 
1395 			if (ov.pmoff) {
1396 				scsa2usbp->scsa2usb_attrs &= ~SCSA2USB_ATTRS_PM;
1397 			}
1398 			if (ov.fake_removable) {
1399 				scsa2usbp->scsa2usb_attrs &=
1400 				    ~SCSA2USB_ATTRS_RMB;
1401 			}
1402 			if (ov.no_modesense) {
1403 				scsa2usbp->scsa2usb_attrs &=
1404 				    ~SCSA2USB_ATTRS_MODE_SENSE;
1405 			}
1406 			if (ov.reduced_cmd_support) {
1407 				scsa2usbp->scsa2usb_attrs &=
1408 				    ~SCSA2USB_ATTRS_REDUCED_CMD;
1409 			}
1410 			scsa2usbp->scsa2usb_override_str = override_str_cpy;
1411 			break;
1412 		} else {
1413 			kmem_free(override_str_cpy, override_str_cpy_len);
1414 		}
1415 	}
1416 
1417 	ddi_prop_free(override_str);
1418 }
1419 
1420 
1421 /*
1422  * scsa2usb_parse_input_str:
1423  *	parse one conf file subclass-protocol-override string
1424  *	return vendor id, product id, revision, subclass, protocol
1425  *	function return is success or failure
1426  */
1427 static int
1428 scsa2usb_parse_input_str(char *str, scsa2usb_ov_t *ovp,
1429     scsa2usb_state_t *scsa2usbp)
1430 {
1431 	char		*input_field, *input_value;
1432 	char		*lasts;
1433 	uint_t		i;
1434 	u_longlong_t	value;
1435 
1436 	/* parse all the input pairs in the string */
1437 	for (input_field = scsa2usb_strtok_r(str, "=", &lasts);
1438 	    input_field != NULL;
1439 	    input_field = scsa2usb_strtok_r(lasts, "=", &lasts)) {
1440 
1441 		if ((input_value = scsa2usb_strtok_r(lasts, " ", &lasts)) ==
1442 		    NULL) {
1443 			scsa2usb_override_error("format", scsa2usbp);
1444 
1445 			return (USB_FAILURE);
1446 		}
1447 		/* if input value is a 'don't care', skip to the next pair */
1448 		if (strcmp(input_value, "*") == 0) {
1449 			continue;
1450 		}
1451 		if (strcasecmp(input_field, "vid") == 0) {
1452 			if (kobj_getvalue(input_value, &value) == -1) {
1453 				scsa2usb_override_error("vendor id", scsa2usbp);
1454 
1455 				return (USB_FAILURE);
1456 			}
1457 			ovp->vid = (int)value;
1458 		} else if (strcasecmp(input_field, "pid") == 0) {
1459 			if (kobj_getvalue(input_value, &value) == -1) {
1460 				scsa2usb_override_error("product id",
1461 				    scsa2usbp);
1462 
1463 				return (USB_FAILURE);
1464 			}
1465 			ovp->pid = (int)value;
1466 		} else if (strcasecmp(input_field, "rev") == 0) {
1467 			if (kobj_getvalue(input_value, &value) == -1) {
1468 				scsa2usb_override_error("revision id",
1469 				    scsa2usbp);
1470 
1471 				return (USB_FAILURE);
1472 			}
1473 			ovp->rev = (int)value;
1474 		} else if (strcasecmp(input_field, "subclass") == 0) {
1475 			for (i = 0; i < N_SCSA2USB_SUBC_OVERRIDE; i++) {
1476 				if (strcasecmp(input_value,
1477 				    scsa2usb_subclass[i].name) == 0) {
1478 					ovp->subclass =
1479 					    scsa2usb_subclass[i].value;
1480 					break;
1481 				}
1482 			}
1483 			if (ovp->subclass == 0) {
1484 				scsa2usb_override_error("subclass", scsa2usbp);
1485 
1486 				return (USB_FAILURE);
1487 			}
1488 		} else if (strcasecmp(input_field, "protocol") == 0) {
1489 			for (i = 0; i < N_SCSA2USB_PROT_OVERRIDE; i++) {
1490 				if (strcasecmp(input_value,
1491 				    scsa2usb_protocol[i].name) == 0) {
1492 					ovp->protocol =
1493 					    scsa2usb_protocol[i].value;
1494 					break;
1495 				}
1496 			}
1497 			if (ovp->protocol == 0) {
1498 				scsa2usb_override_error("protocol", scsa2usbp);
1499 
1500 				return (USB_FAILURE);
1501 			}
1502 		} else if (strcasecmp(input_field, "pm") == 0) {
1503 			if (strcasecmp(input_value, "off") == 0) {
1504 				ovp->pmoff = 1;
1505 				break;
1506 			} else {
1507 				scsa2usb_override_error("pm", scsa2usbp);
1508 
1509 				return (USB_FAILURE);
1510 			}
1511 		} else if (strcasecmp(input_field, "removable") == 0) {
1512 			if (strcasecmp(input_value, "true") == 0) {
1513 				ovp->fake_removable = 1;
1514 				break;
1515 			} else {
1516 				scsa2usb_override_error("removable", scsa2usbp);
1517 
1518 				return (USB_FAILURE);
1519 			}
1520 		} else if (strcasecmp(input_field, "modesense") == 0) {
1521 			if (strcasecmp(input_value, "false") == 0) {
1522 				ovp->no_modesense = 1;
1523 				break;
1524 			} else {
1525 				scsa2usb_override_error("modesense",
1526 								scsa2usbp);
1527 
1528 				return (USB_FAILURE);
1529 			}
1530 		} else if (strcasecmp(input_field,
1531 		    "reduced-cmd-support") == 0) {
1532 			if (strcasecmp(input_value, "true") == 0) {
1533 				ovp->reduced_cmd_support = 1;
1534 				break;
1535 			} else {
1536 				scsa2usb_override_error(
1537 				    "reduced-cmd-support", scsa2usbp);
1538 
1539 				return (USB_FAILURE);
1540 			}
1541 		} else {
1542 			scsa2usb_override_error(input_field, scsa2usbp);
1543 
1544 			return (USB_FAILURE);
1545 		}
1546 	}
1547 
1548 	return (USB_SUCCESS);
1549 }
1550 
1551 
1552 /*
1553  * scsa2usb_override_error:
1554  *	print an error message if conf file string is bad format
1555  */
1556 static void
1557 scsa2usb_override_error(char *input_field, scsa2usb_state_t *scsa2usbp)
1558 {
1559 	USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1560 	    "invalid %s in scsa2usb.conf file entry", input_field);
1561 }
1562 
1563 /*
1564  * scsa2usb_strtok_r:
1565  *	parse a list of tokens
1566  */
1567 static char *
1568 scsa2usb_strtok_r(char *p, char *sep, char **lasts)
1569 {
1570 	char	*e;
1571 	char	*tok = NULL;
1572 
1573 	if (p == 0 || *p == 0) {
1574 
1575 		return (NULL);
1576 	}
1577 
1578 	e = p+strlen(p);
1579 
1580 	do {
1581 		if (strchr(sep, *p) != NULL) {
1582 			if (tok != NULL) {
1583 				*p = 0;
1584 				*lasts = p+1;
1585 
1586 				return (tok);
1587 			}
1588 		} else if (tok == NULL) {
1589 			tok = p;
1590 		}
1591 	} while (++p < e);
1592 
1593 	*lasts = NULL;
1594 
1595 	return (tok);
1596 }
1597 
1598 
1599 /*
1600  * scsa2usb_validate_attrs:
1601  *	many devices have BO/CB/CBI protocol support issues.
1602  *	use vendor/product info to reset the
1603  *	individual erroneous attributes
1604  *
1605  * NOTE: we look at only device at a time (at attach time)
1606  */
1607 static void
1608 scsa2usb_validate_attrs(scsa2usb_state_t *scsa2usbp)
1609 {
1610 	int i, mask;
1611 	usb_dev_descr_t *desc = scsa2usbp->scsa2usb_dev_data->dev_descr;
1612 
1613 	if (!SCSA2USB_IS_BULK_ONLY(scsa2usbp)) {
1614 		scsa2usbp->scsa2usb_attrs &= ~SCSA2USB_ATTRS_GET_LUN;
1615 	}
1616 
1617 	/* determine if this device is on the blacklist */
1618 	for (i = 0; i < N_SCSA2USB_BLACKLIST; i++) {
1619 		if ((scsa2usb_blacklist[i].idVendor == desc->idVendor) &&
1620 		    ((scsa2usb_blacklist[i].idProduct == desc->idProduct) ||
1621 		    (scsa2usb_blacklist[i].idProduct == X))) {
1622 			scsa2usbp->scsa2usb_attrs &=
1623 				~(scsa2usb_blacklist[i].attributes);
1624 			break;
1625 		}
1626 	}
1627 
1628 	/*
1629 	 * Mitsumi's CD-RW drives subclass isn't UFI.
1630 	 * But they support UFI command-set (this code ensures that)
1631 	 * NOTE: This is a special case, and is being called out so.
1632 	 */
1633 	if (desc->idVendor == MS_MITSUMI_VID) {
1634 		mask = scsa2usbp->scsa2usb_cmd_protocol & SCSA2USB_CMDSET_MASK;
1635 		if (mask) {
1636 			scsa2usbp->scsa2usb_cmd_protocol &= ~mask;
1637 		}
1638 		scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_UFI_CMDSET;
1639 	}
1640 
1641 	if (scsa2usbp->scsa2usb_attrs != SCSA2USB_ALL_ATTRS) {
1642 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
1643 		    scsa2usbp->scsa2usb_log_handle,
1644 		    "scsa2usb attributes modified: 0x%x",
1645 		    scsa2usbp->scsa2usb_attrs);
1646 	}
1647 }
1648 
1649 
1650 /*
1651  * scsa2usb_create_luns:
1652  *	check the number of luns but continue if the check fails,
1653  *	create child nodes for each lun
1654  */
1655 static void
1656 scsa2usb_create_luns(scsa2usb_state_t *scsa2usbp)
1657 {
1658 	int		lun, rval;
1659 	char		*compatible[MAX_COMPAT_NAMES];	/* compatible names */
1660 	dev_info_t	*cdip;
1661 	uchar_t		dtype;
1662 	char		*node_name;
1663 	char		*driver_name = NULL;
1664 
1665 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1666 	    "scsa2usb_create_luns:");
1667 
1668 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
1669 
1670 	/* Set n_luns to 1 by default (for floppies and other devices) */
1671 	scsa2usbp->scsa2usb_n_luns = 1;
1672 
1673 	/*
1674 	 * Check if there are any device out there which don't
1675 	 * support the GET_MAX_LUN command. If so, don't issue
1676 	 * control request to them.
1677 	 */
1678 	if ((scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_GET_LUN) == 0) {
1679 		USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1680 		    "get_max_lun cmd not supported");
1681 	} else {
1682 		if (SCSA2USB_IS_BULK_ONLY(scsa2usbp)) {
1683 			scsa2usbp->scsa2usb_n_luns =
1684 				scsa2usb_bulk_only_get_max_lun(scsa2usbp);
1685 		}
1686 	}
1687 
1688 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1689 	    "scsa2usb_create_luns: %d luns found", scsa2usbp->scsa2usb_n_luns);
1690 
1691 	/*
1692 	 * create disk child for each lun
1693 	 */
1694 	for (lun = 0; lun < scsa2usbp->scsa2usb_n_luns; lun++) {
1695 		ASSERT(scsa2usbp->scsa2usb_lun_dip[lun] == NULL);
1696 
1697 		/* do an inquiry to get the dtype of this lun */
1698 		scsa2usb_do_inquiry(scsa2usbp, 0, lun);
1699 
1700 		dtype = scsa2usbp->scsa2usb_lun_inquiry[lun].
1701 						inq_dtype & DTYPE_MASK;
1702 
1703 		USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1704 		    "dtype[%d]=0x%x", lun, dtype);
1705 
1706 		driver_name = NULL;
1707 
1708 		switch (dtype) {
1709 		case DTYPE_DIRECT:
1710 		case DTYPE_RODIRECT:
1711 		case DTYPE_OPTICAL:
1712 			node_name = "disk";
1713 			driver_name = "sd";
1714 
1715 			break;
1716 		case DTYPE_SEQUENTIAL:
1717 			node_name = "tape";
1718 			driver_name = "st";
1719 
1720 			break;
1721 		case DTYPE_PRINTER:
1722 			node_name = "printer";
1723 
1724 			break;
1725 		case DTYPE_PROCESSOR:
1726 			node_name = "processor";
1727 
1728 			break;
1729 		case DTYPE_WORM:
1730 			node_name = "worm";
1731 
1732 			break;
1733 		case DTYPE_SCANNER:
1734 			node_name = "scanner";
1735 
1736 			break;
1737 		case DTYPE_CHANGER:
1738 			node_name = "changer";
1739 
1740 			break;
1741 		case DTYPE_COMM:
1742 			node_name = "comm";
1743 
1744 			break;
1745 		case DTYPE_ARRAY_CTRL:
1746 			node_name = "array_ctrl";
1747 
1748 			break;
1749 		case DTYPE_ESI:
1750 			node_name = "esi";
1751 			driver_name = "ses";
1752 
1753 			break;
1754 		default:
1755 			node_name = "generic";
1756 
1757 			break;
1758 		}
1759 
1760 		if (driver_name) {
1761 			compatible[0] = driver_name;
1762 		}
1763 
1764 		ndi_devi_alloc_sleep(scsa2usbp->scsa2usb_dip, node_name,
1765 		    (pnode_t)DEVI_SID_NODEID, &cdip);
1766 
1767 		/* attach target & lun properties */
1768 		rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", 0);
1769 		if (rval != DDI_PROP_SUCCESS) {
1770 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
1771 			    scsa2usbp->scsa2usb_log_handle,
1772 			    "ndi_prop_update_int target failed %d", rval);
1773 			(void) ndi_devi_free(cdip);
1774 			continue;
1775 		}
1776 
1777 		rval = ndi_prop_create_boolean(DDI_DEV_T_NONE, cdip,
1778 		    "hotpluggable");
1779 		if (rval != DDI_PROP_SUCCESS) {
1780 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
1781 			    scsa2usbp->scsa2usb_log_handle,
1782 			    "ndi_prop_create_boolean hotpluggable failed %d",
1783 			    rval);
1784 			ddi_prop_remove_all(cdip);
1785 			(void) ndi_devi_free(cdip);
1786 			continue;
1787 		}
1788 		/*
1789 		 * Some devices don't support LOG SENSE, so tells
1790 		 * sd driver not to send this command.
1791 		 */
1792 		rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
1793 		    "pm-capable", 1);
1794 		if (rval != DDI_PROP_SUCCESS) {
1795 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
1796 			    scsa2usbp->scsa2usb_log_handle,
1797 			    "ndi_prop_update_int pm-capable failed %d", rval);
1798 			ddi_prop_remove_all(cdip);
1799 			(void) ndi_devi_free(cdip);
1800 			continue;
1801 		}
1802 
1803 		rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", lun);
1804 		if (rval != DDI_PROP_SUCCESS) {
1805 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
1806 			    scsa2usbp->scsa2usb_log_handle,
1807 			    "ndi_prop_update_int lun failed %d", rval);
1808 			ddi_prop_remove_all(cdip);
1809 			(void) ndi_devi_free(cdip);
1810 			continue;
1811 		}
1812 
1813 		if (driver_name) {
1814 			rval = ndi_prop_update_string_array(DDI_DEV_T_NONE,
1815 			    cdip, "compatible", (char **)compatible,
1816 			    MAX_COMPAT_NAMES);
1817 			if (rval != DDI_PROP_SUCCESS) {
1818 				USB_DPRINTF_L2(DPRINT_MASK_SCSA,
1819 				    scsa2usbp->scsa2usb_log_handle,
1820 				    "ndi_prop_update_string_array failed %d",
1821 				    rval);
1822 				ddi_prop_remove_all(cdip);
1823 				(void) ndi_devi_free(cdip);
1824 				continue;
1825 			}
1826 		}
1827 
1828 		/*
1829 		 * add property "usb" so we always verify that it is our child
1830 		 */
1831 		rval = ndi_prop_create_boolean(DDI_DEV_T_NONE, cdip, "usb");
1832 		if (rval != DDI_PROP_SUCCESS) {
1833 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
1834 			    scsa2usbp->scsa2usb_log_handle,
1835 			    "ndi_prop_create_boolean failed %d", rval);
1836 			ddi_prop_remove_all(cdip);
1837 			(void) ndi_devi_free(cdip);
1838 			continue;
1839 		}
1840 
1841 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
1842 		(void) ddi_initchild(scsa2usbp->scsa2usb_dip, cdip);
1843 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
1844 
1845 		usba_set_usba_device(cdip,
1846 			usba_get_usba_device(scsa2usbp->scsa2usb_dip));
1847 	}
1848 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
1849 }
1850 
1851 
1852 /*
1853  * scsa2usb_is_usb:
1854  *	scsa2usb gets called for all possible sd children.
1855  *	we can only accept usb children
1856  */
1857 static int
1858 scsa2usb_is_usb(dev_info_t *dip)
1859 {
1860 	if (dip) {
1861 		return (ddi_prop_exists(DDI_DEV_T_ANY, dip,
1862 		    DDI_PROP_DONTPASS, "usb"));
1863 	}
1864 	return (0);
1865 }
1866 
1867 
1868 /*
1869  * Panic Stuff
1870  * scsa2usb_panic_callb_init:
1871  *	initialize PANIC callb and free allocated resources
1872  */
1873 static void
1874 scsa2usb_panic_callb_init(scsa2usb_state_t *scsa2usbp)
1875 {
1876 	/*
1877 	 * In case the system panics, the sync command flushes
1878 	 * dirty FS pages or buffers. This would cause a hang
1879 	 * in USB.
1880 	 * The reason for the failure is that we enter
1881 	 * polled mode (interrupts disabled) and HCD gets stuck
1882 	 * trying to execute bulk requests
1883 	 * The panic_callback registered below provides a warning
1884 	 * that a panic has occurred and from that point onwards, we
1885 	 * complete each request successfully and immediately. This
1886 	 * will fake successful syncing so at least the rest of the
1887 	 * filesystems complete syncing.
1888 	 */
1889 	scsa2usbp->scsa2usb_panic_info =
1890 		kmem_zalloc(sizeof (scsa2usb_cpr_t), KM_SLEEP);
1891 	mutex_init(&scsa2usbp->scsa2usb_panic_info->lockp,
1892 		NULL, MUTEX_DRIVER,
1893 		scsa2usbp->scsa2usb_dev_data->dev_iblock_cookie);
1894 	scsa2usbp->scsa2usb_panic_info->statep = scsa2usbp;
1895 	scsa2usbp->scsa2usb_panic_info->cpr.cc_lockp =
1896 				&scsa2usbp->scsa2usb_panic_info->lockp;
1897 	scsa2usbp->scsa2usb_panic_info->cpr.cc_id =
1898 			callb_add(scsa2usb_panic_callb,
1899 			    (void *)scsa2usbp->scsa2usb_panic_info,
1900 			    CB_CL_PANIC, "scsa2usb");
1901 }
1902 
1903 
1904 /*
1905  * scsa2usb_panic_callb_fini:
1906  *	cancel out PANIC callb and free allocated resources
1907  */
1908 static void
1909 scsa2usb_panic_callb_fini(scsa2usb_state_t *scsa2usbp)
1910 {
1911 	if (scsa2usbp->scsa2usb_panic_info) {
1912 		SCSA2USB_CANCEL_CB(scsa2usbp->scsa2usb_panic_info->cpr.cc_id);
1913 		mutex_destroy(&scsa2usbp->scsa2usb_panic_info->lockp);
1914 		scsa2usbp->scsa2usb_panic_info->statep = NULL;
1915 		kmem_free(scsa2usbp->scsa2usb_panic_info,
1916 		    sizeof (scsa2usb_cpr_t));
1917 		scsa2usbp->scsa2usb_panic_info = NULL;
1918 	}
1919 }
1920 
1921 
1922 /*
1923  * scsa2usb_panic_callb:
1924  *	This routine is called when there is a system panic.
1925  */
1926 /* ARGSUSED */
1927 static boolean_t
1928 scsa2usb_panic_callb(void *arg, int code)
1929 {
1930 	scsa2usb_cpr_t *cpr_infop;
1931 	scsa2usb_state_t *scsa2usbp;
1932 	uint_t		lun;
1933 
1934 	_NOTE(NO_COMPETING_THREADS_NOW);
1935 	cpr_infop = (scsa2usb_cpr_t *)arg;
1936 	scsa2usbp = (scsa2usb_state_t *)cpr_infop->statep;
1937 
1938 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1939 	    "scsa2usb_panic_callb: code=%d", code);
1940 
1941 	/*
1942 	 * If we return error here, "sd" prints lots of error
1943 	 * messages and could retry the same pkt over and over again.
1944 	 * The sync recovery isn't "smooth" in that case. By faking
1945 	 * a success return, instead,  we force sync to complete.
1946 	 */
1947 	if (scsa2usbp->scsa2usb_cur_pkt) {
1948 		/*
1949 		 * Do not print the "no sync" warning here. it will then be
1950 		 * displayed before we actually start syncing. Also we don't
1951 		 * replace this code with a call to scsa2usb_pkt_completion().
1952 		 * NOTE: mutexes are disabled during panic.
1953 		 */
1954 		scsa2usbp->scsa2usb_cur_pkt->pkt_reason = CMD_CMPLT;
1955 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
1956 		scsa2usb_pkt_completion(scsa2usbp, scsa2usbp->scsa2usb_cur_pkt);
1957 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
1958 	}
1959 
1960 	/* get rid of waitQ */
1961 	for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
1962 		scsa2usb_flush_waitQ(scsa2usbp, lun, CMD_CMPLT);
1963 	}
1964 
1965 #ifndef lint
1966 	_NOTE(COMPETING_THREADS_NOW);
1967 #endif
1968 
1969 	return (B_TRUE);
1970 }
1971 
1972 /*
1973  * scsa2usb_cpr_suspend
1974  *	determine if the device's state can be changed to SUSPENDED
1975  *	close pipes if there is no activity
1976  */
1977 /* ARGSUSED */
1978 static int
1979 scsa2usb_cpr_suspend(dev_info_t *dip)
1980 {
1981 	scsa2usb_state_t *scsa2usbp;
1982 	int	prev_state;
1983 	int	rval = USB_FAILURE;
1984 
1985 	scsa2usbp = ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
1986 
1987 	ASSERT(scsa2usbp != NULL);
1988 
1989 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1990 	    "scsa2usb_cpr_suspend:");
1991 
1992 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
1993 	switch (scsa2usbp->scsa2usb_dev_state) {
1994 	case USB_DEV_ONLINE:
1995 	case USB_DEV_PWRED_DOWN:
1996 	case USB_DEV_DISCONNECTED:
1997 		prev_state = scsa2usbp->scsa2usb_dev_state;
1998 		scsa2usbp->scsa2usb_dev_state = USB_DEV_SUSPENDED;
1999 
2000 		/*
2001 		 * If the device is busy, we cannot suspend
2002 		 */
2003 		if (SCSA2USB_BUSY(scsa2usbp)) {
2004 			USB_DPRINTF_L3(DPRINT_MASK_SCSA,
2005 			    scsa2usbp->scsa2usb_log_handle,
2006 			    "scsa2usb_cpr_suspend: I/O active");
2007 
2008 			/* fall back to previous state */
2009 			scsa2usbp->scsa2usb_dev_state = prev_state;
2010 		} else {
2011 			rval = USB_SUCCESS;
2012 		}
2013 
2014 		break;
2015 	case USB_DEV_SUSPENDED:
2016 	default:
2017 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2018 		    "scsa2usb_cpr_suspend: Illegal dev state: %d",
2019 		    scsa2usbp->scsa2usb_dev_state);
2020 
2021 		break;
2022 	}
2023 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2024 
2025 	if ((rval == USB_SUCCESS) && scsa2usbp->scsa2usb_ugen_hdl) {
2026 		rval = usb_ugen_detach(scsa2usbp->scsa2usb_ugen_hdl,
2027 							DDI_SUSPEND);
2028 	}
2029 
2030 	return (rval);
2031 }
2032 
2033 
2034 /*
2035  * scsa2usb_cpr_resume:
2036  *	restore device's state
2037  */
2038 static void
2039 scsa2usb_cpr_resume(dev_info_t *dip)
2040 {
2041 	scsa2usb_state_t *scsa2usbp =
2042 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
2043 
2044 	ASSERT(scsa2usbp != NULL);
2045 
2046 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2047 	    "scsa2usb_cpr_resume: dip = 0x%p", dip);
2048 
2049 	scsa2usb_restore_device_state(dip, scsa2usbp);
2050 
2051 	if (scsa2usbp->scsa2usb_ugen_hdl) {
2052 		(void) usb_ugen_attach(scsa2usbp->scsa2usb_ugen_hdl,
2053 							DDI_RESUME);
2054 	}
2055 }
2056 
2057 
2058 /*
2059  * scsa2usb_restore_device_state:
2060  *	- raise the device's power
2061  *	- reopen all the pipes
2062  */
2063 static void
2064 scsa2usb_restore_device_state(dev_info_t *dip, scsa2usb_state_t *scsa2usbp)
2065 {
2066 	uint_t	prev_state;
2067 
2068 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2069 	    "scsa2usb_restore_device_state:");
2070 
2071 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2072 	prev_state = scsa2usbp->scsa2usb_dev_state;
2073 
2074 	scsa2usb_raise_power(scsa2usbp);
2075 
2076 	ASSERT((prev_state == USB_DEV_DISCONNECTED) ||
2077 	    (prev_state == USB_DEV_SUSPENDED));
2078 
2079 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2080 
2081 	/* Check for the same device */
2082 	if (usb_check_same_device(dip, scsa2usbp->scsa2usb_log_handle,
2083 	    USB_LOG_L0, DPRINT_MASK_ALL, USB_CHK_ALL, NULL) != USB_SUCCESS) {
2084 
2085 		/* change the flags to active */
2086 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2087 		scsa2usbp->scsa2usb_dev_state = USB_DEV_DISCONNECTED;
2088 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2089 
2090 		scsa2usb_pm_idle_component(scsa2usbp);
2091 
2092 		return;
2093 	}
2094 
2095 	/*
2096 	 * if the device had remote wakeup earlier,
2097 	 * enable it again
2098 	 */
2099 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2100 	if (scsa2usbp->scsa2usb_pm &&
2101 	    scsa2usbp->scsa2usb_pm->scsa2usb_wakeup_enabled) {
2102 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2103 		(void) usb_handle_remote_wakeup(scsa2usbp->scsa2usb_dip,
2104 		    USB_REMOTE_WAKEUP_ENABLE);
2105 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2106 	}
2107 
2108 	scsa2usbp->scsa2usb_dev_state = USB_DEV_ONLINE;
2109 	scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_NONE;
2110 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2111 
2112 	scsa2usb_pm_idle_component(scsa2usbp);
2113 }
2114 
2115 
2116 /*
2117  * SCSA entry points:
2118  *
2119  * scsa2usb_scsi_tgt_probe:
2120  * scsa functions are exported by means of the transport table
2121  * Issue a probe to get the inquiry data.
2122  */
2123 /* ARGSUSED */
2124 static int
2125 scsa2usb_scsi_tgt_probe(struct scsi_device *sd, int (*waitfunc)(void))
2126 {
2127 	scsi_hba_tran_t *tran;
2128 	scsa2usb_state_t *scsa2usbp;
2129 	dev_info_t *dip = ddi_get_parent(sd->sd_dev);
2130 	int	rval;
2131 
2132 	ASSERT(dip);
2133 
2134 	tran = ddi_get_driver_private(dip);
2135 	ASSERT(tran != NULL);
2136 	scsa2usbp = (scsa2usb_state_t *)tran->tran_hba_private;
2137 	ASSERT(scsa2usbp);
2138 
2139 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2140 	    "scsa2usb_scsi_tgt_probe:");
2141 
2142 	/* if device is disconnected (ie. pipes closed), fail immediately */
2143 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2144 	if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
2145 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2146 
2147 		return (SCSIPROBE_FAILURE);
2148 	}
2149 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2150 
2151 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2152 	    "scsa2usb_scsi_tgt_probe: scsi_device = 0x%p", sd);
2153 
2154 	if ((rval = scsi_hba_probe(sd, waitfunc)) == SCSIPROBE_EXISTS) {
2155 		/*
2156 		 * respect the removable bit on all USB storage devices
2157 		 * unless overridden by a scsa2usb.conf entry
2158 		 */
2159 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2160 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_RMB)) {
2161 			_NOTE(SCHEME_PROTECTS_DATA("unshared", scsi_inquiry))
2162 			sd->sd_inq->inq_rmb = 1;
2163 		}
2164 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2165 	}
2166 
2167 	return (rval);
2168 }
2169 
2170 
2171 /*
2172  * scsa2usb_scsi_tgt_init:
2173  *	check whether we created this child ourselves
2174  */
2175 /* ARGSUSED */
2176 static int
2177 scsa2usb_scsi_tgt_init(dev_info_t *dip, dev_info_t *cdip,
2178     scsi_hba_tran_t *tran, struct scsi_device *sd)
2179 {
2180 	scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)
2181 					tran->tran_hba_private;
2182 	int lun;
2183 	int t_len = sizeof (lun);
2184 
2185 	if (ddi_prop_op(DDI_DEV_T_ANY, cdip, PROP_LEN_AND_VAL_BUF,
2186 	    DDI_PROP_DONTPASS|DDI_PROP_CANSLEEP, "lun", (caddr_t)&lun,
2187 	    &t_len) != DDI_PROP_SUCCESS) {
2188 
2189 		return (DDI_FAILURE);
2190 	}
2191 
2192 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2193 	    "scsa2usb_scsi_tgt_init: %s, lun%d", ddi_driver_name(cdip), lun);
2194 
2195 	/* is this a child we created? */
2196 	if (scsa2usb_is_usb(cdip) == 0) {
2197 
2198 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2199 		    "scsa2usb_scsi_tgt_init: new child %s%d",
2200 		    ddi_driver_name(cdip), ddi_get_instance(cdip));
2201 
2202 		/*
2203 		 * add property "usb" so we can always verify that it
2204 		 * is our child
2205 		 */
2206 		if (ndi_prop_create_boolean(DDI_DEV_T_NONE, cdip, "usb") !=
2207 		    DDI_PROP_SUCCESS) {
2208 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
2209 			    scsa2usbp->scsa2usb_log_handle,
2210 			    "ndi_prop_create_boolean failed");
2211 
2212 			return (DDI_FAILURE);
2213 		}
2214 
2215 		usba_set_usba_device(cdip,
2216 			usba_get_usba_device(scsa2usbp->scsa2usb_dip));
2217 
2218 		/*
2219 		 * we don't store this dip in scsa2usb_lun_dip, there
2220 		 * might be multiple dips for the same device
2221 		 */
2222 
2223 		return (DDI_SUCCESS);
2224 	}
2225 
2226 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2227 	if ((lun >= scsa2usbp->scsa2usb_n_luns) ||
2228 	    (scsa2usbp->scsa2usb_lun_dip[lun] != NULL)) {
2229 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2230 
2231 		return (DDI_FAILURE);
2232 	}
2233 
2234 	scsa2usbp->scsa2usb_lun_dip[lun] = cdip;
2235 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2236 
2237 	return (DDI_SUCCESS);
2238 }
2239 
2240 
2241 /*
2242  * scsa2usb_scsi_tgt_free:
2243  */
2244 /* ARGSUSED */
2245 static void
2246 scsa2usb_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *cdip,
2247 	scsi_hba_tran_t *tran, struct scsi_device *sd)
2248 {
2249 	scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)
2250 					tran->tran_hba_private;
2251 	int lun;
2252 	int t_len = sizeof (lun);
2253 
2254 	/* is this our child? */
2255 	if (scsa2usb_is_usb(cdip) == 0) {
2256 
2257 		return;
2258 	}
2259 
2260 	if (ddi_prop_op(DDI_DEV_T_ANY, cdip, PROP_LEN_AND_VAL_BUF,
2261 	    DDI_PROP_DONTPASS|DDI_PROP_CANSLEEP, "lun", (caddr_t)&lun,
2262 	    &t_len) != DDI_PROP_SUCCESS) {
2263 
2264 		return;
2265 	}
2266 
2267 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2268 	    "scsa2usb_scsi_tgt_free: %s lun%d", ddi_driver_name(cdip), lun);
2269 
2270 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2271 	if (lun < scsa2usbp->scsa2usb_n_luns) {
2272 		if (scsa2usbp->scsa2usb_lun_dip[lun] == cdip) {
2273 			scsa2usbp->scsa2usb_lun_dip[lun] = NULL;
2274 		}
2275 	}
2276 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2277 }
2278 
2279 
2280 /*
2281  * bus enumeration entry points
2282  */
2283 static int
2284 scsa2usb_scsi_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
2285     void *arg, dev_info_t **child)
2286 {
2287 	int	circ;
2288 	int	rval;
2289 
2290 	scsa2usb_state_t *scsa2usbp =
2291 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
2292 
2293 	ASSERT(scsa2usbp != NULL);
2294 
2295 	USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2296 	    "scsa2usb_scsi_bus_config: op=%d", op);
2297 
2298 	if (scsa2usb_scsi_bus_config_debug) {
2299 		flag |= NDI_DEVI_DEBUG;
2300 	}
2301 
2302 	ndi_devi_enter(dip, &circ);
2303 	/* create children if necessary */
2304 	if (DEVI(dip)->devi_child == NULL) {
2305 		scsa2usb_create_luns(scsa2usbp);
2306 	}
2307 
2308 	rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0);
2309 
2310 	ndi_devi_exit(dip, circ);
2311 
2312 	return (rval);
2313 }
2314 
2315 
2316 static int
2317 scsa2usb_scsi_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
2318     void *arg)
2319 {
2320 	scsa2usb_state_t *scsa2usbp =
2321 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
2322 
2323 	int		circular_count;
2324 	int		rval = NDI_SUCCESS;
2325 	uint_t		save_flag = flag;
2326 
2327 	ASSERT(scsa2usbp != NULL);
2328 
2329 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2330 	    "scsa2usb_scsi_bus_unconfig: op=%d", op);
2331 
2332 	if (scsa2usb_scsi_bus_config_debug) {
2333 		flag |= NDI_DEVI_DEBUG;
2334 	}
2335 
2336 	/*
2337 	 * first offline and if offlining successful, then
2338 	 * remove children
2339 	 */
2340 	if (op == BUS_UNCONFIG_ALL) {
2341 		flag &= ~(NDI_DEVI_REMOVE | NDI_UNCONFIG);
2342 	}
2343 
2344 	ndi_devi_enter(dip, &circular_count);
2345 	rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
2346 
2347 	/*
2348 	 * If unconfig is successful and not part of modunload
2349 	 * daemon, attempt to remove children.
2350 	 */
2351 	if (op == BUS_UNCONFIG_ALL && rval == NDI_SUCCESS &&
2352 	    (flag & NDI_AUTODETACH) == 0) {
2353 		flag |= NDI_DEVI_REMOVE;
2354 		rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
2355 	}
2356 	ndi_devi_exit(dip, circular_count);
2357 
2358 	if ((rval != NDI_SUCCESS) && (op == BUS_UNCONFIG_ALL) &&
2359 	    (save_flag & NDI_DEVI_REMOVE)) {
2360 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2361 		if (scsa2usbp->scsa2usb_warning_given != B_TRUE) {
2362 			USB_DPRINTF_L0(DPRINT_MASK_SCSA,
2363 			    scsa2usbp->scsa2usb_log_handle,
2364 			    "Disconnected device was busy, "
2365 			    "please reconnect.");
2366 			scsa2usbp->scsa2usb_warning_given = B_TRUE;
2367 		}
2368 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2369 	}
2370 
2371 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2372 	    "scsa2usb_scsi_bus_unconfig: rval=%d", rval);
2373 
2374 	return (rval);
2375 }
2376 
2377 
2378 /*
2379  * scsa2usb_scsi_init_pkt:
2380  *	Set up the scsi_pkt for transport. Also initialize
2381  *	scsa2usb_cmd struct for the transport.
2382  *	NOTE: We do not do any DMA setup here as USBA framework
2383  *	does that for us.
2384  */
2385 static struct scsi_pkt *
2386 scsa2usb_scsi_init_pkt(struct scsi_address *ap,
2387     struct scsi_pkt *pkt, struct buf *bp, int cmdlen, int statuslen,
2388     int tgtlen, int flags, int (*callback)(), caddr_t arg)
2389 {
2390 	scsa2usb_cmd_t	 *cmd;
2391 	scsa2usb_state_t *scsa2usbp;
2392 	struct scsi_pkt	 *in_pkt = pkt;
2393 
2394 	ASSERT(callback == NULL_FUNC || callback == SLEEP_FUNC);
2395 
2396 	scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap);
2397 
2398 	/* Print sync message */
2399 	if (ddi_in_panic()) {
2400 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2401 		SCSA2USB_PRINT_SYNC_MSG(scsa2usb_sync_message, scsa2usbp);
2402 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2403 		/* continue so caller will not hang or complain */
2404 	}
2405 
2406 	/* allocate a pkt, if none already allocated */
2407 	if (pkt == NULL) {
2408 		if (statuslen < sizeof (struct scsi_arq_status)) {
2409 			statuslen = sizeof (struct scsi_arq_status);
2410 		}
2411 
2412 		pkt = scsi_hba_pkt_alloc(scsa2usbp->scsa2usb_dip, ap, cmdlen,
2413 			statuslen, tgtlen, sizeof (scsa2usb_cmd_t),
2414 			callback, arg);
2415 		if (pkt == NULL) {
2416 
2417 			return (NULL);
2418 		}
2419 
2420 		cmd = PKT2CMD(pkt);
2421 		cmd->cmd_pkt	= pkt; /* back link to pkt */
2422 		cmd->cmd_scblen	= statuslen;
2423 		cmd->cmd_cdblen	= (uchar_t)cmdlen;
2424 
2425 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2426 		cmd->cmd_tag	= scsa2usbp->scsa2usb_tag++;
2427 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2428 
2429 		cmd->cmd_bp	= bp;
2430 		pkt->pkt_scbp	= (opaque_t)&cmd->cmd_scb;
2431 
2432 		usba_init_list(&cmd->cmd_waitQ, (usb_opaque_t)cmd,
2433 			scsa2usbp->scsa2usb_dev_data->dev_iblock_cookie);
2434 	} else {
2435 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2436 		    "scsa2usb: pkt != NULL");
2437 
2438 		/* nothing to do */
2439 	}
2440 
2441 	if (bp && (bp->b_bcount != 0)) {
2442 		if ((bp_mapin_common(bp, (callback == SLEEP_FUNC) ?
2443 		    VM_SLEEP : VM_NOSLEEP)) == NULL) {
2444 			if (pkt != in_pkt) {
2445 				scsi_hba_pkt_free(ap, pkt);
2446 			}
2447 
2448 			return (NULL);
2449 		}
2450 
2451 		USB_DPRINTF_L3(DPRINT_MASK_SCSA,
2452 		    scsa2usbp->scsa2usb_log_handle,
2453 		    "scsa2usb_scsi_init_pkt: mapped in 0x%p, addr=0x%p",
2454 		    bp, bp->b_un.b_addr);
2455 	}
2456 
2457 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2458 	    "scsa2usb_scsi_init_pkt: ap = 0x%p pkt: 0x%p\n\t"
2459 	    "bp = 0x%p cmdlen = %x stlen = 0x%x tlen = 0x%x flags = 0x%x",
2460 	    ap, pkt, bp, cmdlen, statuslen, tgtlen, flags);
2461 
2462 	return (pkt);
2463 }
2464 
2465 
2466 /*
2467  * scsa2usb_scsi_destroy_pkt:
2468  *	We are done with the packet. Get rid of it.
2469  */
2470 static void
2471 scsa2usb_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2472 {
2473 	scsa2usb_cmd_t *cmd = PKT2CMD(pkt);
2474 	scsa2usb_state_t *scsa2usbp = ADDR2SCSA2USB(ap);
2475 
2476 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2477 	    "scsa2usb_scsi_destroy_pkt: pkt=0x%p", pkt);
2478 
2479 	usba_destroy_list(&cmd->cmd_waitQ);
2480 	scsi_hba_pkt_free(ap, pkt);
2481 }
2482 
2483 
2484 /*
2485  * scsa2usb_scsi_start:
2486  *	For each command being issued, build up the CDB
2487  *	and call scsi_transport to issue the command. This
2488  *	function is based on the assumption that USB allows
2489  *	a subset of SCSI commands. Other SCSI commands we fail.
2490  */
2491 static int
2492 scsa2usb_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2493 {
2494 	scsa2usb_cmd_t		*cmd;
2495 	scsa2usb_state_t	*scsa2usbp = ADDR2SCSA2USB(ap);
2496 	uint_t			lun = ap->a_lun;
2497 
2498 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2499 
2500 	cmd = PKT2CMD(pkt);
2501 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2502 	    "scsa2usb_scsi_start:\n\t"
2503 	    "bp: 0x%p ap: 0x%p pkt: 0x%p flag: 0x%x time: 0x%x\n\tcdb0: 0x%x "
2504 	    "dev_state: 0x%x pkt_state: 0x%x flags: 0x%x pipe_state: 0x%x",
2505 	    cmd->cmd_bp, ap, pkt, pkt->pkt_flags, pkt->pkt_time,
2506 	    pkt->pkt_cdbp[0], scsa2usbp->scsa2usb_dev_state,
2507 	    scsa2usbp->scsa2usb_pkt_state, scsa2usbp->scsa2usb_flags,
2508 	    scsa2usbp->scsa2usb_pipe_state);
2509 
2510 	if (pkt->pkt_time == 0) {
2511 		USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2512 		    "pkt submitted with 0 timeout which may cause indefinite "
2513 		    "hangs");
2514 	}
2515 
2516 	/*
2517 	 * if we are in panic, we are in polled mode, so we can just
2518 	 * accept the request, drop it and return
2519 	 * if we fail this request, the rest of the file systems do not
2520 	 * get synced
2521 	 */
2522 	if (ddi_in_panic()) {
2523 		extern int do_polled_io;
2524 
2525 		ASSERT(do_polled_io);
2526 		scsa2usb_prepare_pkt(scsa2usbp, pkt);
2527 		SCSA2USB_PRINT_SYNC_MSG(scsa2usb_sync_message, scsa2usbp);
2528 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2529 
2530 		return (TRAN_ACCEPT);
2531 	}
2532 
2533 	/* we cannot do polling, this should not happen */
2534 	if (pkt->pkt_flags & FLAG_NOINTR) {
2535 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2536 		    "NOINTR packet: opcode = 0%x", pkt->pkt_cdbp[0]);
2537 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2538 
2539 		return (TRAN_BADPKT);
2540 	}
2541 
2542 	/* is there a ugen open? */
2543 	if (scsa2usbp->scsa2usb_ugen_open_count) {
2544 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2545 		    "ugen access in progress (count=%d)",
2546 		    scsa2usbp->scsa2usb_ugen_open_count);
2547 
2548 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2549 
2550 		return (TRAN_BUSY);
2551 	}
2552 
2553 	/* prepare packet */
2554 	scsa2usb_prepare_pkt(scsa2usbp, pkt);
2555 
2556 	/* just queue up the requests in the waitQ if below max */
2557 	if (usba_list_entry_count(&scsa2usbp->scsa2usb_waitQ[lun]) >
2558 	    SCSA2USB_MAX_REQ_PER_LUN) {
2559 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
2560 		    scsa2usbp->scsa2usb_log_handle,
2561 		    "scsa2usb_scsi_start: limit (%d) exceeded",
2562 		    SCSA2USB_MAX_REQ_PER_LUN);
2563 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2564 
2565 		return (TRAN_BUSY);
2566 	}
2567 
2568 	usba_add_to_list(&scsa2usbp->scsa2usb_waitQ[lun], &cmd->cmd_waitQ);
2569 
2570 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2571 	    "scsa2usb_work_thread_id=0x%x, count=%d, lun=%d",
2572 	    scsa2usbp->scsa2usb_work_thread_id,
2573 	    usba_list_entry_count(&scsa2usbp->scsa2usb_waitQ[lun]), lun);
2574 
2575 	/* fire up a thread to start executing the protocol */
2576 	if (scsa2usbp->scsa2usb_work_thread_id == 0) {
2577 		if ((usb_async_req(scsa2usbp->scsa2usb_dip,
2578 		    scsa2usb_work_thread,
2579 		    (void *)scsa2usbp, USB_FLAGS_SLEEP)) != USB_SUCCESS) {
2580 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
2581 			    scsa2usbp->scsa2usb_log_handle,
2582 			    "no work thread started");
2583 
2584 			if (usba_rm_from_list(
2585 			    &scsa2usbp->scsa2usb_waitQ[lun],
2586 			    &cmd->cmd_waitQ) == USB_SUCCESS) {
2587 				mutex_exit(&scsa2usbp->scsa2usb_mutex);
2588 
2589 				return (TRAN_BUSY);
2590 			} else {
2591 
2592 				mutex_exit(&scsa2usbp->scsa2usb_mutex);
2593 
2594 				return (TRAN_ACCEPT);
2595 			}
2596 		}
2597 		scsa2usbp->scsa2usb_work_thread_id = (kthread_t *)1;
2598 	}
2599 
2600 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2601 
2602 	return (TRAN_ACCEPT);
2603 }
2604 
2605 
2606 /*
2607  * scsa2usb_scsi_abort:
2608  *	Issue SCSI abort command. This function is a NOP.
2609  */
2610 /* ARGSUSED */
2611 static int
2612 scsa2usb_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
2613 {
2614 	scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap);
2615 
2616 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2617 	    "scsa2usb_scsi_abort: pkt = %p", pkt);
2618 
2619 	/* if device is disconnected (ie. pipes closed), fail immediately */
2620 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2621 	if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
2622 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2623 
2624 		return (0);
2625 	}
2626 
2627 	/* flush waitQ if target and lun match */
2628 	if ((ap->a_target == pkt->pkt_address.a_target) &&
2629 	    (ap->a_lun == pkt->pkt_address.a_lun)) {
2630 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2631 		scsa2usb_flush_waitQ(scsa2usbp, ap->a_lun, CMD_ABORTED);
2632 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2633 	}
2634 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2635 
2636 	return (0);
2637 }
2638 
2639 
2640 /*
2641  * scsa2usb_scsi_reset:
2642  *	device reset may turn the device into a brick and bus reset
2643  *	is not applicable.
2644  *	just flush the waitQ
2645  *	We return success, always.
2646  */
2647 /* ARGSUSED */
2648 static int
2649 scsa2usb_scsi_reset(struct scsi_address *ap, int level)
2650 {
2651 	scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap);
2652 
2653 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2654 	    "scsa2usb_scsi_reset: ap = 0x%p, level = %d", ap, level);
2655 
2656 	/* flush waitQ */
2657 	scsa2usb_flush_waitQ(scsa2usbp, ap->a_lun, CMD_RESET);
2658 
2659 	return (1);
2660 }
2661 
2662 
2663 /*
2664  * scsa2usb_scsi_getcap:
2665  *	Get SCSI capabilities.
2666  */
2667 /* ARGSUSED */
2668 static int
2669 scsa2usb_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2670 {
2671 	int rval = -1;
2672 	uint_t cidx;
2673 	size_t dev_bsize_cap;
2674 	scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap);
2675 	ASSERT(scsa2usbp);
2676 
2677 	if (cap == NULL) {
2678 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2679 		    "scsa2usb_scsi_getcap: invalid arg, "
2680 		    "cap = 0x%p whom = %d", cap, whom);
2681 
2682 		return (rval);
2683 	}
2684 
2685 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2686 	    "scsa2usb_scsi_getcap: cap = %s", cap);
2687 
2688 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2689 
2690 	/* if device is disconnected (ie. pipes closed), fail immediately */
2691 	if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
2692 
2693 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2694 
2695 		return (rval);
2696 	}
2697 
2698 	cidx =	scsi_hba_lookup_capstr(cap);
2699 	switch (cidx) {
2700 	case SCSI_CAP_GEOMETRY:
2701 		dev_bsize_cap = scsa2usbp->scsa2usb_totalsec[ap->a_lun];
2702 
2703 		if (scsa2usbp->scsa2usb_secsz[ap->a_lun] > DEV_BSIZE) {
2704 			dev_bsize_cap *=
2705 			    scsa2usbp->scsa2usb_secsz[ap->a_lun] / DEV_BSIZE;
2706 		} else if (scsa2usbp->scsa2usb_secsz[ap->a_lun] <
2707 		    DEV_BSIZE) {
2708 			dev_bsize_cap /=
2709 			    DEV_BSIZE / scsa2usbp->scsa2usb_secsz[ap->a_lun];
2710 		}
2711 
2712 		if (dev_bsize_cap < 65536 * 2 * 18) {		/* < ~1GB */
2713 			/* unlabeled floppy, 18k per cylinder */
2714 			rval = ((2 << 16) | 18);
2715 		} else if (dev_bsize_cap < 65536 * 64 * 32) {	/* < 64GB */
2716 			/* 1024k per cylinder */
2717 			rval = ((64 << 16) | 32);
2718 		} else if (dev_bsize_cap < 65536 * 255 * 63) {	/* < ~500GB */
2719 			/* ~8m per cylinder */
2720 			rval = ((255 << 16) | 63);
2721 		} else {					/* .. 8TB */
2722 			/* 64m per cylinder */
2723 			rval = ((512 << 16) | 256);
2724 		}
2725 		break;
2726 
2727 	case SCSI_CAP_DMA_MAX:
2728 		rval = scsa2usbp->scsa2usb_max_bulk_xfer_size;
2729 		break;
2730 	case SCSI_CAP_SCSI_VERSION:
2731 		rval = SCSI_VERSION_2;
2732 		break;
2733 	case SCSI_CAP_INTERCONNECT_TYPE:
2734 		rval = INTERCONNECT_USB;
2735 		break;
2736 	case SCSI_CAP_ARQ:
2737 		/* FALLTHRU */
2738 	case SCSI_CAP_UNTAGGED_QING:
2739 		rval = 1;
2740 		break;
2741 	default:
2742 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2743 		    "scsa2usb_scsi_getcap: unsupported cap = %s", cap);
2744 		break;
2745 	}
2746 
2747 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2748 	    "scsa2usb_scsi_getcap: cap = %s, returned = %d", cap, rval);
2749 
2750 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2751 
2752 	return (rval);
2753 }
2754 
2755 
2756 /*
2757  * scsa2usb_scsi_setcap:
2758  *	Set SCSI capabilities.
2759  */
2760 /* ARGSUSED */
2761 static int
2762 scsa2usb_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2763 {
2764 	int rval = -1; /* default is cap undefined */
2765 	uint_t cidx;
2766 	scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap);
2767 	ASSERT(scsa2usbp);
2768 
2769 	if (cap == NULL || whom == 0) {
2770 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2771 		    "scsa2usb_scsi_setcap: invalid arg");
2772 
2773 		return (rval);
2774 	}
2775 
2776 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2777 	/* if device is disconnected (ie. pipes closed), fail immediately */
2778 	if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
2779 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2780 
2781 		return (rval);
2782 	}
2783 
2784 	cidx =	scsi_hba_lookup_capstr(cap);
2785 	USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2786 	    "scsa2usb_scsi_setcap: ap = 0x%p value = 0x%x whom = 0x%x "
2787 	    "cidx = 0x%x", ap, value, whom, cidx);
2788 
2789 	switch (cidx) {
2790 	case SCSI_CAP_SECTOR_SIZE:
2791 		if (value) {
2792 			scsa2usbp->scsa2usb_secsz[ap->a_lun] = value;
2793 		}
2794 		break;
2795 	case SCSI_CAP_TOTAL_SECTORS:
2796 		if (value) {
2797 			scsa2usbp->scsa2usb_totalsec[ap->a_lun] = value;
2798 		}
2799 		break;
2800 	case SCSI_CAP_ARQ:
2801 		rval = 1;
2802 		break;
2803 	case SCSI_CAP_DMA_MAX:
2804 	case SCSI_CAP_SCSI_VERSION:
2805 	case SCSI_CAP_INTERCONNECT_TYPE:
2806 	case SCSI_CAP_UNTAGGED_QING:
2807 		/* supported but not settable */
2808 		rval = 0;
2809 		break;
2810 	default:
2811 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2812 		    "scsa2usb_scsi_setcap: unsupported cap = %s", cap);
2813 		break;
2814 	}
2815 
2816 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2817 
2818 	return (rval);
2819 }
2820 
2821 
2822 /*
2823  * scsa2usb - cmd and transport stuff
2824  */
2825 /*
2826  * scsa2usb_prepare_pkt:
2827  *	initialize some fields of the pkt and cmd
2828  *	(the pkt may have been resubmitted/retried)
2829  */
2830 static void
2831 scsa2usb_prepare_pkt(scsa2usb_state_t *scsa2usbp, struct scsi_pkt *pkt)
2832 {
2833 	scsa2usb_cmd_t	*cmd = PKT2CMD(pkt);
2834 
2835 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2836 	    "scsa2usb_prepare_pkt: pkt=0x%p cdb: 0x%x (%s)",
2837 	    pkt, pkt->pkt_cdbp[0],
2838 	    scsi_cname(pkt->pkt_cdbp[0], scsa2usb_cmds));
2839 
2840 	pkt->pkt_reason = CMD_CMPLT;	/* Set reason to pkt_complete */
2841 	pkt->pkt_state = 0;		/* Reset next three fields */
2842 	pkt->pkt_statistics = 0;
2843 	pkt->pkt_resid = 0;
2844 	bzero(pkt->pkt_scbp, cmd->cmd_scblen); /* Set status to good */
2845 
2846 	if (cmd) {
2847 		cmd->cmd_timeout = pkt->pkt_time;
2848 		cmd->cmd_xfercount = 0;		/* Reset the fields */
2849 		cmd->cmd_total_xfercount = 0;
2850 		cmd->cmd_lba = 0;
2851 		cmd->cmd_done = 0;
2852 		cmd->cmd_dir = 0;
2853 		cmd->cmd_offset = 0;
2854 		cmd->cmd_actual_len = cmd->cmd_cdblen;
2855 	}
2856 }
2857 
2858 
2859 /*
2860  * scsa2usb_force_invalid_request
2861  */
2862 static void
2863 scsa2usb_force_invalid_request(scsa2usb_state_t *scsa2usbp,
2864     scsa2usb_cmd_t *cmd)
2865 {
2866 	struct scsi_arq_status	*arqp;
2867 
2868 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2869 	    "scsa2usb_force_invalid_request: pkt = 0x%p", cmd->cmd_pkt);
2870 
2871 	if (cmd->cmd_scblen >= sizeof (struct scsi_arq_status)) {
2872 		arqp = (struct scsi_arq_status *)cmd->cmd_pkt->pkt_scbp;
2873 		bzero(arqp, cmd->cmd_scblen);
2874 
2875 		arqp->sts_status.sts_chk = 1;
2876 		arqp->sts_rqpkt_reason = CMD_CMPLT;
2877 		arqp->sts_rqpkt_state = STATE_XFERRED_DATA |
2878 		    STATE_GOT_BUS | STATE_GOT_STATUS;
2879 		arqp->sts_sensedata.es_valid = 1;
2880 		arqp->sts_sensedata.es_class = 7;
2881 		arqp->sts_sensedata.es_key = KEY_ILLEGAL_REQUEST;
2882 
2883 		cmd->cmd_pkt->pkt_state = STATE_ARQ_DONE |
2884 		    STATE_GOT_BUS | STATE_GOT_BUS | STATE_GOT_BUS |
2885 		    STATE_GOT_STATUS;
2886 #ifdef DEBUG
2887 		{
2888 			uchar_t *p = (uchar_t *)(&arqp->sts_sensedata);
2889 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
2890 			    scsa2usbp->scsa2usb_log_handle,
2891 			    "cdb: %x rqsense: "
2892 			    "%x %x %x %x %x %x %x %x %x %x "
2893 			    "%x %x %x %x %x %x %x %x %x %x",
2894 			    cmd->cmd_pkt->pkt_cdbp[0],
2895 			    p[0], p[1], p[2], p[3], p[4],
2896 			    p[5], p[6], p[7], p[8], p[9],
2897 			    p[10], p[11], p[12], p[13], p[14],
2898 			    p[15], p[16], p[17], p[18], p[19]);
2899 		}
2900 #endif
2901 
2902 	}
2903 }
2904 
2905 
2906 /*
2907  * scsa2usb_cmd_transport:
2908  */
2909 static int
2910 scsa2usb_cmd_transport(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd)
2911 {
2912 	int rval, transport;
2913 	struct scsi_pkt *pkt;
2914 
2915 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2916 	    "scsa2usb_cmd_transport: pkt: 0x%p, cur_pkt = 0x%p",
2917 	    cmd->cmd_pkt, scsa2usbp->scsa2usb_cur_pkt);
2918 
2919 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
2920 	ASSERT(scsa2usbp->scsa2usb_cur_pkt == NULL);
2921 
2922 	pkt = scsa2usbp->scsa2usb_cur_pkt = cmd->cmd_pkt;
2923 
2924 	/* check black-listed attrs first */
2925 	if (SCSA2USB_IS_BULK_ONLY(scsa2usbp)) {
2926 		transport = scsa2usb_check_bulkonly_blacklist_attrs(scsa2usbp,
2927 					cmd, pkt->pkt_cdbp[0]);
2928 	} else if (SCSA2USB_IS_CB(scsa2usbp) || SCSA2USB_IS_CBI(scsa2usbp)) {
2929 		transport =  scsa2usb_check_ufi_blacklist_attrs(scsa2usbp,
2930 					pkt->pkt_cdbp[0], cmd);
2931 	}
2932 
2933 	/* just accept the command */
2934 	if (transport == SCSA2USB_JUST_ACCEPT) {
2935 		SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
2936 
2937 		return (TRAN_ACCEPT);
2938 	}
2939 
2940 	/* check command set next */
2941 	if (SCSA2USB_IS_SCSI_CMDSET(scsa2usbp) ||
2942 	    SCSA2USB_IS_ATAPI_CMDSET(scsa2usbp)) {
2943 		transport =
2944 		    scsa2usb_handle_scsi_cmd_sub_class(scsa2usbp, cmd, pkt);
2945 	} else if (SCSA2USB_IS_UFI_CMDSET(scsa2usbp)) {
2946 		transport =
2947 		    scsa2usb_handle_ufi_subclass_cmd(scsa2usbp, cmd, pkt);
2948 	} else {
2949 		transport = SCSA2USB_REJECT;
2950 	}
2951 
2952 	switch (transport) {
2953 	case SCSA2USB_TRANSPORT:
2954 		if (SCSA2USB_IS_BULK_ONLY(scsa2usbp)) {
2955 			rval = scsa2usb_bulk_only_transport(scsa2usbp, cmd);
2956 		} else if (SCSA2USB_IS_CB(scsa2usbp) ||
2957 		    SCSA2USB_IS_CBI(scsa2usbp)) {
2958 			rval = scsa2usb_cbi_transport(scsa2usbp, cmd);
2959 		} else {
2960 			rval = TRAN_FATAL_ERROR;
2961 		}
2962 		break;
2963 	case SCSA2USB_JUST_ACCEPT:
2964 		SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
2965 		rval = TRAN_ACCEPT;
2966 		break;
2967 	default:
2968 		rval = TRAN_FATAL_ERROR;
2969 	}
2970 
2971 	return (rval);
2972 }
2973 
2974 
2975 /*
2976  * scsa2usb_check_bulkonly_blacklist_attrs:
2977  *	validate "scsa2usb_blacklist_attrs" (see scsa2usb.h)
2978  *	if blacklisted attrs match accept the request
2979  *	attributes checked are:-
2980  *		SCSA2USB_ATTRS_START_STOP
2981  */
2982 int
2983 scsa2usb_check_bulkonly_blacklist_attrs(scsa2usb_state_t *scsa2usbp,
2984     scsa2usb_cmd_t *cmd, uchar_t opcode)
2985 {
2986 	struct scsi_inquiry *inq =
2987 	    &scsa2usbp->scsa2usb_lun_inquiry[cmd->cmd_pkt->pkt_address.a_lun];
2988 
2989 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2990 	    "scsa2usb_check_bulkonly_blacklist_attrs: opcode = %s",
2991 	    scsi_cname(opcode, scsa2usb_cmds));
2992 
2993 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
2994 
2995 	/*
2996 	 * decode and convert the packet
2997 	 * for most cmds, we can bcopy the cdb
2998 	 */
2999 	switch (opcode) {
3000 	case SCMD_DOORLOCK:
3001 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_DOORLOCK)) {
3002 
3003 			return (SCSA2USB_JUST_ACCEPT);
3004 
3005 		/*
3006 		 * only lock the door for CD and DVD drives
3007 		 */
3008 		} else if ((inq->inq_dtype == DTYPE_RODIRECT) ||
3009 		    (inq->inq_dtype == DTYPE_OPTICAL)) {
3010 
3011 			if (inq->inq_rmb) {
3012 
3013 				break;
3014 			}
3015 		}
3016 
3017 		return (SCSA2USB_JUST_ACCEPT);
3018 
3019 	case SCMD_START_STOP:
3020 		/*
3021 		 * these devices don't have mechanics that spin the
3022 		 * media up and down. So, it doesn't make much sense
3023 		 * to issue this cmd.
3024 		 *
3025 		 * Furthermore, Hagiwara devices do not handle these
3026 		 * cmds well. just accept this command as success.
3027 		 */
3028 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_START_STOP)) {
3029 
3030 			return (SCSA2USB_JUST_ACCEPT);
3031 
3032 		} else if (cmd->cmd_pkt->pkt_cdbp[4] & LOEJECT) {
3033 			/*
3034 			 * if the device is really a removable then
3035 			 * pass it on to the device, else just accept
3036 			 */
3037 			if (inq->inq_rmb) {
3038 
3039 				break;
3040 			}
3041 
3042 			return (SCSA2USB_JUST_ACCEPT);
3043 
3044 		} else if (!scsa2usbp->scsa2usb_rcvd_not_ready) {
3045 			/*
3046 			 * if we have not received a NOT READY condition,
3047 			 * just accept since some device choke on this too.
3048 			 * we do have to let EJECT get through though
3049 			 */
3050 			return (SCSA2USB_JUST_ACCEPT);
3051 		}
3052 
3053 		break;
3054 	case SCMD_INQUIRY:
3055 		/*
3056 		 * Some devices do not handle the inquiry cmd well
3057 		 * so build an inquiry and accept this command as
3058 		 * success.
3059 		 */
3060 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_INQUIRY)) {
3061 			uchar_t evpd = 0x01;
3062 
3063 			if (cmd->cmd_cdb[1] & evpd) {
3064 
3065 				return (SCSA2USB_REJECT);
3066 			}
3067 			cmd->cmd_pkt->pkt_resid -=
3068 					scsa2usb_fake_inquiry(scsa2usbp, cmd,
3069 					cmd->cmd_pkt->pkt_address.a_lun);
3070 			cmd->cmd_pkt->pkt_state |= STATE_XFERRED_DATA;
3071 
3072 			return (SCSA2USB_JUST_ACCEPT);
3073 		}
3074 		break;
3075 
3076 	/*
3077 	 * Fake accepting the following  Opcodes
3078 	 * (as most drives don't support these)
3079 	 * These are needed by format command.
3080 	 */
3081 	case SCMD_RESERVE:
3082 	case SCMD_RELEASE:
3083 	case SCMD_PERSISTENT_RESERVE_IN:
3084 	case SCMD_PERSISTENT_RESERVE_OUT:
3085 
3086 		return (SCSA2USB_JUST_ACCEPT);
3087 
3088 	case SCMD_MODE_SENSE:
3089 	case SCMD_MODE_SELECT:
3090 	case SCMD_MODE_SENSE_G1:
3091 	case SCMD_MODE_SELECT_G1:
3092 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_MODE_SENSE)) {
3093 			if (cmd->cmd_bp) {
3094 				cmd->cmd_pkt->pkt_resid = cmd->cmd_bp->
3095 								b_bcount;
3096 			}
3097 			scsa2usb_force_invalid_request(scsa2usbp, cmd);
3098 
3099 			return (SCSA2USB_JUST_ACCEPT);
3100 		}
3101 
3102 		break;
3103 	default:
3104 
3105 		break;
3106 	}
3107 
3108 	return (SCSA2USB_TRANSPORT);
3109 }
3110 
3111 
3112 /*
3113  * scsa2usb_handle_scsi_cmd_sub_class:
3114  *	prepare a scsi cmd
3115  *	returns SCSA2USB_TRANSPORT, SCSA2USB_REJECT, SCSA2USB_JUST_ACCEPT
3116  */
3117 int
3118 scsa2usb_handle_scsi_cmd_sub_class(scsa2usb_state_t *scsa2usbp,
3119     scsa2usb_cmd_t *cmd, struct scsi_pkt *pkt)
3120 {
3121 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3122 	    "scsa2usb_handle_scsi_cmd_sub_class: cmd = 0x%p pkt = 0x%p",
3123 	    cmd, pkt);
3124 
3125 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3126 
3127 	bzero(&cmd->cmd_cdb, SCSI_CDB_SIZE);
3128 	cmd->cmd_cdb[SCSA2USB_OPCODE] = pkt->pkt_cdbp[0];   /* Set the opcode */
3129 	cmd->cmd_cdb[SCSA2USB_LUN] = pkt->pkt_cdbp[1];
3130 
3131 	/*
3132 	 * decode and convert the packet
3133 	 * for most cmds, we can bcopy the cdb
3134 	 */
3135 	switch (pkt->pkt_cdbp[0]) {
3136 	case SCMD_FORMAT:
3137 		/*
3138 		 * SCMD_FORMAT used to limit cmd->cmd_xfercount
3139 		 * to 4 bytes, but this hangs
3140 		 * formatting dvd media using cdrecord (that is,
3141 		 * a SCSI FORMAT UNIT command with a parameter list > 4 bytes)
3142 		 * (bit 4 in cdb1 is the Fmtdata bit)
3143 		 */
3144 		if ((pkt->pkt_cdbp[1] & 0x10) && cmd->cmd_bp) {
3145 			cmd->cmd_xfercount = cmd->cmd_bp->b_bcount;
3146 		} else {
3147 			cmd->cmd_xfercount = 4;
3148 		}
3149 		cmd->cmd_dir = CBW_DIR_OUT;
3150 		cmd->cmd_actual_len = CDB_GROUP0;
3151 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3152 		break;
3153 
3154 	case SCMD_INQUIRY:
3155 		cmd->cmd_dir = CBW_DIR_IN;
3156 		cmd->cmd_actual_len = CDB_GROUP0;
3157 		cmd->cmd_cdb[SCSA2USB_LBA_0] = pkt->pkt_cdbp[2];
3158 		cmd->cmd_cdb[SCSA2USB_LBA_2] = cmd->cmd_xfercount =
3159 		    min(SCSA2USB_MAX_INQ_LEN,
3160 		    cmd->cmd_bp ? cmd->cmd_bp->b_bcount : 0);
3161 		break;
3162 
3163 	case SCMD_READ_CAPACITY:
3164 		cmd->cmd_dir = CBW_DIR_IN;
3165 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3166 		cmd->cmd_xfercount = sizeof (scsa2usb_read_cap_t);
3167 		break;
3168 
3169 	/*
3170 	 * SCMD_READ/SCMD_WRITE are converted to G1 cmds
3171 	 * (as ATAPI devices don't recognize G0 commands)
3172 	 *
3173 	 * SCMD_READ_LONG/SCMD_WRITE_LONG are handled in
3174 	 * scsa2usb_rw_transport() along with other commands.
3175 	 *
3176 	 * USB Host Controllers cannot handle large (read/write)
3177 	 * xfers. We split the large request to chunks of
3178 	 * smaller ones to meet the HCD limitations.
3179 	 */
3180 	case SCMD_READ:
3181 	case SCMD_WRITE:
3182 	case SCMD_READ_G1:
3183 	case SCMD_WRITE_G1:
3184 	case SCMD_READ_G5:
3185 	case SCMD_WRITE_G5:
3186 	case SCMD_READ_LONG:
3187 	case SCMD_WRITE_LONG:
3188 	case SCMD_READ_CD:
3189 		switch (scsa2usbp->
3190 		    scsa2usb_lun_inquiry[pkt->pkt_address.a_lun].
3191 		    inq_dtype & DTYPE_MASK) {
3192 		case DTYPE_DIRECT:
3193 		case DTYPE_RODIRECT:
3194 		case DTYPE_OPTICAL:
3195 			return (scsa2usb_rw_transport(
3196 					scsa2usbp, pkt));
3197 		default:
3198 			bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3199 			if (cmd->cmd_bp) {
3200 				cmd->cmd_dir =
3201 				    (cmd->cmd_bp->b_flags & B_READ) ?
3202 				    CBW_DIR_IN : CBW_DIR_OUT;
3203 				cmd->cmd_xfercount =
3204 				    cmd->cmd_bp->b_bcount;
3205 			}
3206 			break;
3207 		}
3208 		break;
3209 
3210 	case SCMD_REQUEST_SENSE:
3211 		cmd->cmd_dir = CBW_DIR_IN;
3212 		cmd->cmd_xfercount = pkt->pkt_cdbp[4];
3213 		cmd->cmd_cdb[SCSA2USB_LBA_2] = pkt->pkt_cdbp[4];
3214 		cmd->cmd_actual_len = CDB_GROUP0;
3215 		break;
3216 
3217 	case SCMD_DOORLOCK:
3218 	case SCMD_START_STOP:
3219 	case SCMD_TEST_UNIT_READY:
3220 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3221 		break;
3222 
3223 	/*
3224 	 * Needed by zip protocol to reset the device
3225 	 */
3226 	case SCMD_SDIAG:
3227 	case SCMD_REZERO_UNIT:
3228 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3229 		cmd->cmd_actual_len = CDB_GROUP1;
3230 		break;
3231 
3232 	case SCMD_WRITE_VERIFY:
3233 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3234 		cmd->cmd_dir = CBW_DIR_OUT;
3235 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3236 		cmd->cmd_actual_len = CDB_GROUP1;
3237 		break;
3238 
3239 	/*
3240 	 * Next command does not have a SCSI equivalent as
3241 	 * it is vendor specific.
3242 	 * It was listed in the vendor's ATAPI Zip specs.
3243 	 */
3244 	case SCMD_READ_FORMAT_CAP:
3245 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3246 		cmd->cmd_dir = CBW_DIR_IN;
3247 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3248 		cmd->cmd_actual_len = CDB_GROUP1;
3249 		break;
3250 	case IOMEGA_CMD_CARTRIDGE_PROTECT:
3251 		cmd->cmd_dir = CBW_DIR_OUT;
3252 		cmd->cmd_cdb[SCSA2USB_LBA_2] = pkt->pkt_cdbp[4];
3253 		cmd->cmd_cdb[SCSA2USB_LBA_2] &= ~1;	/* Make it even */
3254 		cmd->cmd_cdb[SCSA2USB_LUN] = pkt->pkt_cdbp[1];
3255 		cmd->cmd_actual_len = CDB_GROUP0;
3256 		cmd->cmd_xfercount = pkt->pkt_cdbp[4]; /* Length of password */
3257 		break;
3258 
3259 	/*
3260 	 * Do not convert SCMD_MODE_SENSE/SELECT to G1 cmds because
3261 	 * the mode header is different as well. USB devices don't
3262 	 * support 0x03 & 0x04 mode pages, which are already obsoleted
3263 	 * by SPC-2 specification.
3264 	 */
3265 	case SCMD_MODE_SENSE:
3266 	case SCMD_MODE_SELECT:
3267 		if ((pkt->pkt_cdbp[2] == SD_MODE_SENSE_PAGE3_CODE) ||
3268 		    (pkt->pkt_cdbp[2] == SD_MODE_SENSE_PAGE4_CODE)) {
3269 			if (cmd->cmd_bp) {
3270 				cmd->cmd_pkt->pkt_resid = cmd->cmd_bp->b_bcount;
3271 			}
3272 			scsa2usb_force_invalid_request(scsa2usbp, cmd);
3273 			return (SCSA2USB_JUST_ACCEPT);
3274 		}
3275 		/* FALLTHROUGH */
3276 
3277 	default:
3278 		/*
3279 		 * an unknown command may be a uscsi cmd which we
3280 		 * should let go thru without mapping
3281 		 */
3282 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3283 		if (cmd->cmd_bp) {
3284 			cmd->cmd_dir = (cmd->cmd_bp->b_flags & B_READ) ?
3285 						CBW_DIR_IN : CBW_DIR_OUT;
3286 			cmd->cmd_xfercount = cmd->cmd_bp->b_bcount;
3287 		}
3288 
3289 		break;
3290 	} /* end of switch */
3291 
3292 	USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3293 	    "scsa2usb_handle_scsi_cmd_sub_class: opcode = 0x%x count = 0x%lx",
3294 	    pkt->pkt_cdbp[SCSA2USB_OPCODE], cmd->cmd_xfercount);
3295 
3296 	cmd->cmd_total_xfercount = cmd->cmd_xfercount;
3297 
3298 	return (SCSA2USB_TRANSPORT);
3299 }
3300 
3301 
3302 /*
3303  * scsa2usb_do_tur is performed before READ CAPACITY command is issued.
3304  * It returns media status, 0 for media ready, -1 for media not ready
3305  * or other errors.
3306  */
3307 static int
3308 scsa2usb_do_tur(scsa2usb_state_t *scsa2usbp, struct scsi_address *ap)
3309 {
3310 	struct scsi_pkt		*pkt;
3311 	scsa2usb_cmd_t		*turcmd;
3312 	int			rval = -1;
3313 
3314 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3315 	    "scsa2usb_do_tur:");
3316 
3317 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3318 
3319 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
3320 	if ((pkt = scsi_init_pkt(ap, NULL, NULL, CDB_GROUP0, 1,
3321 	    PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL)) == NULL) {
3322 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
3323 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
3324 		    scsa2usbp->scsa2usb_log_handle,
3325 		    "scsa2usb_do_tur: init pkt failed");
3326 
3327 		return (rval);
3328 	}
3329 
3330 	RQ_MAKECOM_G0(pkt, FLAG_HEAD | FLAG_NODISCON,
3331 	    (char)SCMD_TEST_UNIT_READY, 0, 0);
3332 
3333 	pkt->pkt_comp = NULL;
3334 	pkt->pkt_time = PKT_DEFAULT_TIMEOUT;
3335 	turcmd = PKT2CMD(pkt);
3336 
3337 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
3338 	scsa2usb_prepare_pkt(scsa2usbp, turcmd->cmd_pkt);
3339 
3340 	if (scsa2usb_cmd_transport(scsa2usbp, turcmd) != TRAN_ACCEPT) {
3341 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
3342 		    scsa2usbp->scsa2usb_log_handle,
3343 		    "scsa2usb_do_tur: cmd transport failed, "
3344 		    "pkt_reason=0x%x", turcmd->cmd_pkt->pkt_reason);
3345 	} else if (*(turcmd->cmd_pkt->pkt_scbp) != STATUS_GOOD) {
3346 		/*
3347 		 * Theoretically, the sense data should be retrieved and
3348 		 * sense key be checked when check condition happens. If
3349 		 * the sense key is UNIT ATTENTION, TEST UNIT READY cmd
3350 		 * needs to be sent again to clear the UNIT ATTENTION and
3351 		 * another TUR to be sent to get the real media status.
3352 		 * But the AMI virtual floppy device simply cannot recover
3353 		 * from UNIT ATTENTION by re-sending a TUR cmd, so it
3354 		 * doesn't make any difference whether to check sense key
3355 		 * or not. Just ignore sense key checking here and assume
3356 		 * the device is NOT READY.
3357 		 */
3358 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
3359 		    scsa2usbp->scsa2usb_log_handle,
3360 		    "scsa2usb_do_tur: media not ready");
3361 	} else {
3362 		rval = 0;
3363 	}
3364 
3365 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
3366 	scsi_destroy_pkt(pkt);
3367 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
3368 
3369 	return (rval);
3370 }
3371 
3372 
3373 /*
3374  * scsa2usb_check_ufi_blacklist_attrs:
3375  *	validate "scsa2usb_blacklist_attrs" (see scsa2usb.h)
3376  *	if blacklisted attrs match accept the request
3377  *	attributes checked are:-
3378  *		SCSA2USB_ATTRS_GET_CONF
3379  *		SCSA2USB_ATTRS_GET_PERF
3380  *		SCSA2USB_ATTRS_GET_START_STOP
3381  */
3382 static int
3383 scsa2usb_check_ufi_blacklist_attrs(scsa2usb_state_t *scsa2usbp, uchar_t opcode,
3384     scsa2usb_cmd_t *cmd)
3385 {
3386 	int	rval = SCSA2USB_TRANSPORT;
3387 
3388 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3389 
3390 	switch (opcode) {
3391 	case SCMD_PRIN:
3392 	case SCMD_PROUT:
3393 		rval = SCSA2USB_JUST_ACCEPT;
3394 		break;
3395 	case SCMD_MODE_SENSE:
3396 	case SCMD_MODE_SELECT:
3397 		if (cmd->cmd_bp) {
3398 			cmd->cmd_pkt->pkt_resid = cmd->cmd_bp->b_bcount;
3399 		}
3400 		scsa2usb_force_invalid_request(scsa2usbp, cmd);
3401 		rval = SCSA2USB_JUST_ACCEPT;
3402 		break;
3403 	case SCMD_GET_CONFIGURATION:
3404 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_GET_CONF)) {
3405 			rval = SCSA2USB_JUST_ACCEPT;
3406 		}
3407 		break;
3408 	case SCMD_GET_PERFORMANCE:
3409 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_GET_PERF)) {
3410 			rval = SCSA2USB_JUST_ACCEPT;
3411 		}
3412 		break;
3413 	case SCMD_START_STOP:
3414 		/*
3415 		 * some CB/CBI devices don't have mechanics that spin the
3416 		 * media up and down. So, it doesn't make much sense
3417 		 * to issue this cmd to those devices.
3418 		 */
3419 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_START_STOP)) {
3420 			rval = SCSA2USB_JUST_ACCEPT;
3421 		}
3422 		break;
3423 	case SCMD_READ_CAPACITY:
3424 		/*
3425 		 * Some devices don't support READ CAPACITY command
3426 		 * when media is not ready. Need to check media status
3427 		 * before issuing the cmd to such device.
3428 		 */
3429 		if (!(scsa2usbp->scsa2usb_attrs &
3430 		    SCSA2USB_ATTRS_NO_MEDIA_CHECK)) {
3431 			struct scsi_pkt *pkt = cmd->cmd_pkt;
3432 
3433 			ASSERT(scsa2usbp->scsa2usb_cur_pkt == pkt);
3434 			scsa2usbp->scsa2usb_cur_pkt = NULL;
3435 
3436 			if (scsa2usb_do_tur(scsa2usbp,
3437 			    &pkt->pkt_address) != 0) {
3438 				/* media not ready, force cmd invalid */
3439 				if (cmd->cmd_bp) {
3440 					cmd->cmd_pkt->pkt_resid =
3441 					    cmd->cmd_bp->b_bcount;
3442 				}
3443 				scsa2usb_force_invalid_request(scsa2usbp, cmd);
3444 				rval = SCSA2USB_JUST_ACCEPT;
3445 			}
3446 
3447 			scsa2usbp->scsa2usb_cur_pkt = pkt;
3448 		}
3449 		break;
3450 	default:
3451 		break;
3452 	}
3453 
3454 	return (rval);
3455 }
3456 
3457 
3458 /*
3459  * scsa2usb_handle_ufi_subclass_cmd:
3460  *	prepare a UFI cmd
3461  *	returns SCSA2USB_TRANSPORT, SCSA2USB_REJECT
3462  */
3463 int
3464 scsa2usb_handle_ufi_subclass_cmd(scsa2usb_state_t *scsa2usbp,
3465     scsa2usb_cmd_t *cmd, struct scsi_pkt *pkt)
3466 {
3467 	uchar_t opcode =  pkt->pkt_cdbp[0];
3468 
3469 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3470 	    "scsa2usb_handle_ufi_subclass_cmd: cmd = 0x%p pkt = 0x%p",
3471 	    cmd, pkt);
3472 
3473 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3474 
3475 	bzero(&cmd->cmd_cdb, SCSI_CDB_SIZE);
3476 	cmd->cmd_cdb[SCSA2USB_OPCODE] = opcode;   /* Set the opcode */
3477 	cmd->cmd_cdb[SCSA2USB_LUN] = pkt->pkt_cdbp[1];
3478 
3479 	/*
3480 	 * decode and convert the packet if necessary
3481 	 * for most cmds, we can bcopy the cdb
3482 	 */
3483 	switch (opcode) {
3484 	case SCMD_FORMAT:
3485 		/* if parameter list is specified */
3486 		if (pkt->pkt_cdbp[1] & 0x10) {
3487 			cmd->cmd_xfercount =
3488 				(pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3489 			cmd->cmd_dir = USB_EP_DIR_OUT;
3490 			cmd->cmd_actual_len = CDB_GROUP5;
3491 		}
3492 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3493 		break;
3494 	case SCMD_INQUIRY:
3495 		cmd->cmd_dir = USB_EP_DIR_IN;
3496 		cmd->cmd_actual_len = CDB_GROUP0;
3497 		cmd->cmd_cdb[SCSA2USB_LBA_0] = pkt->pkt_cdbp[2];
3498 		cmd->cmd_cdb[SCSA2USB_LBA_2] = cmd->cmd_xfercount =
3499 		    min(SCSA2USB_MAX_INQ_LEN,
3500 		    cmd->cmd_bp ? cmd->cmd_bp->b_bcount : 0);
3501 		break;
3502 	case SCMD_READ_CAPACITY:
3503 		cmd->cmd_dir = USB_EP_DIR_IN;
3504 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3505 		cmd->cmd_xfercount = sizeof (scsa2usb_read_cap_t);
3506 		break;
3507 	case SCMD_REQUEST_SENSE:
3508 		cmd->cmd_dir = USB_EP_DIR_IN;
3509 		cmd->cmd_xfercount = pkt->pkt_cdbp[4];
3510 		cmd->cmd_cdb[SCSA2USB_LBA_2] = pkt->pkt_cdbp[4];
3511 		cmd->cmd_actual_len = CDB_GROUP0;
3512 		break;
3513 
3514 	/*
3515 	 * do not convert SCMD_MODE_SENSE/SELECT because the
3516 	 * mode header is different as well
3517 	 */
3518 
3519 	/*
3520 	 * see usb_bulkonly.c for comments on the next set of commands
3521 	 */
3522 	case SCMD_READ:
3523 	case SCMD_WRITE:
3524 	case SCMD_READ_G1:
3525 	case SCMD_WRITE_G1:
3526 	case SCMD_READ_G5:
3527 	case SCMD_WRITE_G5:
3528 	case SCMD_READ_LONG:
3529 	case SCMD_WRITE_LONG:
3530 	case SCMD_READ_CD:
3531 
3532 		return (scsa2usb_rw_transport(scsa2usbp, pkt));
3533 
3534 	case SCMD_TEST_UNIT_READY:
3535 		/*
3536 		 * Some CB/CBI devices may not support TUR.
3537 		 */
3538 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3539 		break;
3540 	case SCMD_READ_FORMAT_CAP:
3541 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3542 		cmd->cmd_dir = USB_EP_DIR_IN;
3543 		cmd->cmd_actual_len = CDB_GROUP1;
3544 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3545 		break;
3546 	case SCMD_WRITE_VERIFY:
3547 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3548 		cmd->cmd_dir = USB_EP_DIR_OUT;
3549 		cmd->cmd_actual_len = CDB_GROUP1;
3550 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3551 		break;
3552 	case SCMD_START_STOP:
3553 		/* A larger timeout is needed for 'flaky' CD-RW devices */
3554 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_BIG_TIMEOUT)) {
3555 			cmd->cmd_timeout = max(cmd->cmd_timeout,
3556 					20 * SCSA2USB_BULK_PIPE_TIMEOUT);
3557 		}
3558 		/* FALLTHRU */
3559 	default:
3560 		/*
3561 		 * all other commands don't need special mapping
3562 		 */
3563 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3564 		if (cmd->cmd_bp) {
3565 			cmd->cmd_dir = (cmd->cmd_bp->b_flags & B_READ) ?
3566 						CBW_DIR_IN : CBW_DIR_OUT;
3567 			cmd->cmd_xfercount = cmd->cmd_bp->b_bcount;
3568 		}
3569 		break;
3570 
3571 	} /* end of switch */
3572 
3573 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3574 	    "scsa2usb_handle_ufi_subclass_cmd: opcode = 0x%x count = 0x%lx",
3575 	    opcode, cmd->cmd_xfercount);
3576 
3577 	cmd->cmd_total_xfercount = cmd->cmd_xfercount;
3578 
3579 	return (SCSA2USB_TRANSPORT);
3580 }
3581 
3582 
3583 /*
3584  * scsa2usb_rw_transport:
3585  *	Handle splitting READ and WRITE requests to the
3586  *	device to a size that the host controller allows.
3587  *
3588  *	returns TRAN_* values and not USB_SUCCESS/FAILURE
3589  *
3590  * To support CD-R/CD-RW/DVD media, we need to support a
3591  * variety of block sizes for the different types of CD
3592  * data (audio, data, video, CD-XA, yellowbook, redbook etc.)
3593  *
3594  * Some of the block sizes used are:- 512, 1k, 2k, 2056, 2336
3595  * 2340, 2352, 2368, 2448, 2646, 2647 etc.
3596  *
3597  * NOTE: the driver could be entertaining a SCSI CDB that uses
3598  * any of the above listed block sizes at a given time, and a
3599  * totally different block size at any other given time for a
3600  * different CDB.
3601  *
3602  * We need to compute block size every time and figure out
3603  * matching LBA and LEN accordingly.
3604  *
3605  * Also UHCI has a limitation that it can only xfer 32k at a
3606  * given time. So, with "odd" sized blocks and a limitation of
3607  * how much we can xfer per shot, we need to compute xfer_count
3608  * as well each time.
3609  *
3610  * The same computation is also done in the function
3611  * scsa2usb_setup_next_xfer().	To save computing block_size in
3612  * this function, I am saving block_size in "cmd" now.
3613  */
3614 int
3615 scsa2usb_rw_transport(scsa2usb_state_t *scsa2usbp, struct scsi_pkt *pkt)
3616 {
3617 	scsa2usb_cmd_t *cmd = PKT2CMD(pkt);
3618 	int lba, dir, opcode;
3619 	struct buf *bp = cmd->cmd_bp;
3620 	size_t len, xfer_count;
3621 	size_t blk_size;	/* calculate the block size to be used */
3622 	int sz;
3623 
3624 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3625 	    "scsa2usb_rw_transport:");
3626 
3627 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3628 
3629 	opcode = pkt->pkt_cdbp[0];
3630 	blk_size  = scsa2usbp->scsa2usb_lbasize[pkt->pkt_address.a_lun];
3631 						/* set to default */
3632 
3633 	switch (opcode) {
3634 	case SCMD_READ:
3635 		/*
3636 		 * Note that READ/WRITE(6) are not supported by the drive.
3637 		 * convert it into a 10 byte read/write.
3638 		 */
3639 		lba = SCSA2USB_LBA_6BYTE(pkt);
3640 		len = SCSA2USB_LEN_6BYTE(pkt);
3641 		opcode = SCMD_READ_G1;	/* Overwrite it w/ byte 10 cmd val */
3642 		dir = USB_EP_DIR_IN;
3643 		break;
3644 	case SCMD_WRITE:
3645 		lba = SCSA2USB_LBA_6BYTE(pkt);
3646 		len = SCSA2USB_LEN_6BYTE(pkt);
3647 		opcode = SCMD_WRITE_G1;	/* Overwrite it w/ byte 10 cmd val */
3648 		dir = USB_EP_DIR_OUT;
3649 		break;
3650 	case SCMD_READ_G1:
3651 	case SCMD_READ_LONG:
3652 		lba = SCSA2USB_LBA_10BYTE(pkt);
3653 		len = SCSA2USB_LEN_10BYTE(pkt);
3654 		dir = USB_EP_DIR_IN;
3655 		break;
3656 	case SCMD_WRITE_G1:
3657 	case SCMD_WRITE_LONG:
3658 		lba = SCSA2USB_LBA_10BYTE(pkt);
3659 		len = SCSA2USB_LEN_10BYTE(pkt);
3660 		dir = USB_EP_DIR_OUT;
3661 		if (len) {
3662 			sz = SCSA2USB_CDRW_BLKSZ(bp ? bp->b_bcount : 0, len);
3663 			if (SCSA2USB_VALID_CDRW_BLKSZ(sz)) {
3664 				blk_size = sz;	/* change it accordingly */
3665 			}
3666 		}
3667 		break;
3668 	case SCMD_READ_CD:
3669 		lba = SCSA2USB_LBA_10BYTE(pkt);
3670 		len = SCSA2USB_LEN_READ_CD(pkt);
3671 		dir = USB_EP_DIR_IN;
3672 
3673 		/* Figure out the block size */
3674 		blk_size = scsa2usb_read_cd_blk_size(pkt->pkt_cdbp[1] >> 2);
3675 		break;
3676 	case SCMD_READ_G5:
3677 		lba = SCSA2USB_LBA_12BYTE(pkt);
3678 		len = SCSA2USB_LEN_12BYTE(pkt);
3679 		dir = USB_EP_DIR_IN;
3680 		break;
3681 	case SCMD_WRITE_G5:
3682 		lba = SCSA2USB_LBA_12BYTE(pkt);
3683 		len = SCSA2USB_LEN_12BYTE(pkt);
3684 		dir = USB_EP_DIR_OUT;
3685 		break;
3686 	}
3687 
3688 	cmd->cmd_total_xfercount = xfer_count = len * blk_size;
3689 
3690 	/* reduce xfer count if necessary */
3691 	if (blk_size &&
3692 	    (xfer_count > scsa2usbp->scsa2usb_max_bulk_xfer_size)) {
3693 		/*
3694 		 * For CD-RW devices reduce the xfer count based
3695 		 * on the block size used by these devices. The
3696 		 * block size could change for READ_CD and WRITE
3697 		 * opcodes.
3698 		 *
3699 		 * Also as UHCI allows a max xfer of 32k at a time;
3700 		 * compute the xfer_count based on the new block_size.
3701 		 *
3702 		 * The len part of the cdb changes as a result of that.
3703 		 */
3704 		if (SCSA2USB_VALID_CDRW_BLKSZ(blk_size)) {
3705 			xfer_count = ((scsa2usbp->scsa2usb_max_bulk_xfer_size/
3706 					blk_size) * blk_size);
3707 			len = xfer_count/blk_size;
3708 			xfer_count = blk_size * len;
3709 		} else {
3710 			xfer_count = scsa2usbp->scsa2usb_max_bulk_xfer_size;
3711 			len = xfer_count/blk_size;
3712 		}
3713 	}
3714 
3715 	cmd->cmd_xfercount = xfer_count;
3716 	cmd->cmd_dir = (uchar_t)dir;
3717 	cmd->cmd_blksize = blk_size;
3718 
3719 	/*
3720 	 * Having figure out the 'partial' xfer len based on he
3721 	 * block size; fill it in to the cmd->cmd_cdb
3722 	 */
3723 	cmd->cmd_cdb[SCSA2USB_OPCODE] = (uchar_t)opcode;
3724 	switch (opcode) {
3725 	case SCMD_READ_CD:
3726 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3727 		scsa2usb_fill_up_ReadCD_cdb_len(cmd, len, CDB_GROUP5);
3728 		break;
3729 	case SCMD_WRITE_G5:
3730 	case SCMD_READ_G5:
3731 		scsa2usb_fill_up_12byte_cdb_len(cmd, len, CDB_GROUP5);
3732 		break;
3733 	default:
3734 		scsa2usb_fill_up_cdb_len(cmd, len);
3735 		cmd->cmd_actual_len = CDB_GROUP1;
3736 		break;
3737 	}
3738 
3739 	scsa2usb_fill_up_cdb_lba(cmd, lba);
3740 
3741 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3742 	    "bcount=0x%lx lba=0x%x len=0x%lx xfercount=0x%lx total=0x%lx",
3743 	    bp ? bp->b_bcount : 0, lba, len, cmd->cmd_xfercount,
3744 	    cmd->cmd_total_xfercount);
3745 
3746 	/* Set the timeout value as per command request */
3747 	if ((opcode == SCMD_WRITE_G1) && SCSA2USB_VALID_CDRW_BLKSZ(blk_size)) {
3748 		/*
3749 		 * We increase the time as CD-RW writes have two things
3750 		 * to do. After writing out the data to the media, a
3751 		 * TOC needs to be filled up at the beginning of the media
3752 		 * This is when the write gets "finalized".
3753 		 * Hence the actual write could take longer than the
3754 		 * value specified in cmd->cmd_timeout.
3755 		 */
3756 		cmd->cmd_timeout *= 4;
3757 
3758 		USB_DPRINTF_L4(DPRINT_MASK_SCSA,
3759 		    scsa2usbp->scsa2usb_log_handle,
3760 		    "new timeout value = 0x%x", cmd->cmd_timeout);
3761 	}
3762 
3763 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3764 	    "lba 0x%x len 0x%lx xfercount 0x%lx total 0x%lx",
3765 	    lba, len, cmd->cmd_xfercount, cmd->cmd_total_xfercount);
3766 
3767 	return (SCSA2USB_TRANSPORT);
3768 }
3769 
3770 
3771 /*
3772  * scsa2usb_setup_next_xfer:
3773  *	For READs and WRITEs we split up the transfer in terms of
3774  *	HCD understood units. This function handles the split transfers.
3775  *
3776  * See comments in the previous function scsa2usb_rw_transport
3777  *
3778  * The lba computation was being done based on scsa2usb_max_bulk_xfer_size
3779  * earlier. With CD-RW devices, the xfer_count and the block_size may
3780  * no longer be a multiple of scsa2usb_max_bulk_xfer_size. So compute
3781  * xfer_count all over again. Adjust lba, based on the previous requests'
3782  * len. Find out the len and add it to cmd->cmd_lba to get the new lba
3783  */
3784 void
3785 scsa2usb_setup_next_xfer(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd)
3786 {
3787 	int xfer_len = min(scsa2usbp->scsa2usb_max_bulk_xfer_size,
3788 			cmd->cmd_total_xfercount);
3789 	int cdb_len;
3790 	size_t blk_size;
3791 
3792 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3793 
3794 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3795 	    "scsa2usb_setup_next_xfer: opcode = 0x%x lba = 0x%x "
3796 	    "total count = 0x%lx", cmd->cmd_cdb[SCSA2USB_OPCODE],
3797 	    cmd->cmd_lba, cmd->cmd_total_xfercount);
3798 
3799 	ASSERT(cmd->cmd_total_xfercount > 0);
3800 	cmd->cmd_xfercount = xfer_len;
3801 	blk_size = scsa2usbp->scsa2usb_lbasize[
3802 				cmd->cmd_pkt->pkt_address.a_lun];
3803 
3804 	/*
3805 	 * For CD-RW devices reduce the xfer count based on the
3806 	 * block_size used by these devices. See changes below
3807 	 * where xfer_count is being adjusted.
3808 	 *
3809 	 * Also adjust len/lba based on the block_size and xfer_count.
3810 	 * NOTE: Always calculate lba first, as it based on previous
3811 	 * commands' values.
3812 	 */
3813 	switch (cmd->cmd_cdb[SCSA2USB_OPCODE]) {
3814 	case SCMD_READ_CD:
3815 		/* calculate lba = current_lba + len_of_prev_cmd */
3816 		cmd->cmd_lba += (cmd->cmd_cdb[6] << 16) +
3817 		    (cmd->cmd_cdb[7] << 8) + cmd->cmd_cdb[8];
3818 		cdb_len = xfer_len/cmd->cmd_blksize;
3819 		cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_2] = (uchar_t)cdb_len;
3820 		/* re-adjust xfer count */
3821 		cmd->cmd_xfercount = cdb_len * cmd->cmd_blksize;
3822 		break;
3823 	case SCMD_WRITE_G5:
3824 	case SCMD_READ_G5:
3825 		/* calculate lba = current_lba + len_of_prev_cmd */
3826 		cmd->cmd_lba += (cmd->cmd_cdb[6] << 24) +
3827 		    (cmd->cmd_cdb[7] << 16) + (cmd->cmd_cdb[8] << 8) +
3828 		    cmd->cmd_cdb[9];
3829 		if (blk_size) {
3830 			xfer_len /= blk_size;
3831 		}
3832 		scsa2usb_fill_up_12byte_cdb_len(cmd, xfer_len, CDB_GROUP5);
3833 		break;
3834 	case SCMD_WRITE_G1:
3835 	case SCMD_WRITE_LONG:
3836 		/* calculate lba = current_lba + len_of_prev_cmd */
3837 		cmd->cmd_lba += (cmd->cmd_cdb[7] << 8) + cmd->cmd_cdb[8];
3838 		if (SCSA2USB_VALID_CDRW_BLKSZ(cmd->cmd_blksize)) {
3839 			blk_size = cmd->cmd_blksize;
3840 		}
3841 		cdb_len = xfer_len/blk_size;
3842 		scsa2usb_fill_up_cdb_len(cmd, cdb_len);
3843 		/* re-adjust xfer count */
3844 		cmd->cmd_xfercount = cdb_len * blk_size;
3845 		break;
3846 	default:
3847 		if (blk_size) {
3848 			xfer_len /= blk_size;
3849 		}
3850 		scsa2usb_fill_up_cdb_len(cmd, xfer_len);
3851 		cmd->cmd_lba += scsa2usbp->scsa2usb_max_bulk_xfer_size/blk_size;
3852 	}
3853 
3854 	/* fill in the lba */
3855 	scsa2usb_fill_up_cdb_lba(cmd, cmd->cmd_lba);
3856 
3857 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3858 	    "scsa2usb_setup_next_xfer:\n\tlba = 0x%x xfer_len = 0x%x "
3859 	    "xfercount = 0x%lx total = 0x%lx", cmd->cmd_lba, xfer_len,
3860 	    cmd->cmd_xfercount, cmd->cmd_total_xfercount);
3861 }
3862 
3863 
3864 /*
3865  * take one request from the lun's waitQ and transport it
3866  */
3867 static void
3868 scsa2usb_transport_request(scsa2usb_state_t *scsa2usbp, uint_t lun)
3869 {
3870 	int			rval;
3871 	struct scsi_pkt		*pkt;
3872 	struct scsa2usb_cmd	*cmd, *arqcmd;
3873 
3874 	if ((cmd = (scsa2usb_cmd_t *)
3875 	    usba_rm_first_pvt_from_list(
3876 	    &scsa2usbp->scsa2usb_waitQ[lun])) == NULL) {
3877 
3878 		return;
3879 	}
3880 	pkt = cmd->cmd_pkt;
3881 
3882 	/*
3883 	 * if device has been disconnected, just complete it
3884 	 */
3885 	if (scsa2usbp->scsa2usb_dev_state == USB_DEV_DISCONNECTED) {
3886 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3887 		    "device not accessible");
3888 		pkt->pkt_reason = CMD_DEV_GONE;
3889 		SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
3890 		scsa2usb_pkt_completion(scsa2usbp, pkt);
3891 
3892 		return;
3893 	}
3894 
3895 	USB_DPRINTF_L4(DPRINT_MASK_SCSA,
3896 	    scsa2usbp->scsa2usb_log_handle,
3897 	    "scsa2usb_transport_request: cmd=0x%p bp=0x%p addr=0x%p",
3898 	    cmd, cmd->cmd_bp,
3899 	    (cmd->cmd_bp ? cmd->cmd_bp->b_un.b_addr : NULL));
3900 
3901 	rval = scsa2usb_cmd_transport(scsa2usbp, cmd);
3902 
3903 	USB_DPRINTF_L3(DPRINT_MASK_SCSA,
3904 	    scsa2usbp->scsa2usb_log_handle,
3905 	    "scsa2usb_transport_request: transport rval = %d",
3906 	    rval);
3907 
3908 	if (scsa2usbp->scsa2usb_cur_pkt == NULL) {
3909 
3910 		return;
3911 	}
3912 
3913 	ASSERT(pkt == scsa2usbp->scsa2usb_cur_pkt);
3914 
3915 	if (ddi_in_panic()) {
3916 		pkt->pkt_reason = CMD_CMPLT;
3917 		scsa2usb_pkt_completion(scsa2usbp, pkt);
3918 
3919 		return;
3920 	}
3921 
3922 	/*
3923 	 * start an auto-request sense iff
3924 	 * there was a check condition, we have enough
3925 	 * space in the status block, and we have not
3926 	 * faked an auto request sense
3927 	 */
3928 	if ((*(pkt->pkt_scbp) == STATUS_CHECK) &&
3929 	    (cmd->cmd_scblen >= sizeof (struct scsi_arq_status)) &&
3930 	    ((pkt->pkt_state & STATE_ARQ_DONE) == 0) &&
3931 	    (scsa2usb_create_arq_pkt(scsa2usbp,
3932 	    &pkt->pkt_address) == USB_SUCCESS)) {
3933 		arqcmd = scsa2usbp->scsa2usb_arq_cmd;
3934 
3935 		/*
3936 		 * copy the timeout from the
3937 		 * original packet
3938 		 * for lack of a better value
3939 		 */
3940 		arqcmd->cmd_pkt->pkt_time = pkt->pkt_time;
3941 		scsa2usb_prepare_pkt(scsa2usbp,
3942 			    arqcmd->cmd_pkt);
3943 
3944 		scsa2usbp->scsa2usb_cur_pkt = NULL;
3945 		if (scsa2usb_cmd_transport(
3946 		    scsa2usbp, arqcmd) == TRAN_ACCEPT) {
3947 
3948 			/* finish w/ this packet */
3949 			scsa2usb_complete_arq_pkt(
3950 			    scsa2usbp, arqcmd->cmd_pkt, cmd,
3951 			    scsa2usbp->scsa2usb_arq_bp);
3952 
3953 			/*
3954 			 * we have valid request sense
3955 			 * data so clear the pkt_reason
3956 			 */
3957 			pkt->pkt_reason = CMD_CMPLT;
3958 		}
3959 		scsa2usbp->scsa2usb_cur_pkt = pkt;
3960 		scsa2usb_delete_arq_pkt(scsa2usbp);
3961 	}
3962 
3963 	if ((rval != TRAN_ACCEPT) &&
3964 	    (pkt->pkt_reason == CMD_CMPLT)) {
3965 		pkt->pkt_reason = CMD_TRAN_ERR;
3966 	}
3967 
3968 	SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
3969 	scsa2usb_pkt_completion(scsa2usbp, pkt);
3970 
3971 	ASSERT(scsa2usbp->scsa2usb_cur_pkt == NULL);
3972 }
3973 
3974 
3975 /*
3976  * scsa2usb_work_thread:
3977  *	The taskq thread that kicks off the transport (BO and CB/CBI)
3978  */
3979 static void
3980 scsa2usb_work_thread(void *arg)
3981 {
3982 	scsa2usb_state_t	*scsa2usbp = (scsa2usb_state_t *)arg;
3983 	uint_t			lun;
3984 	uint_t			count;
3985 
3986 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
3987 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3988 	    "scsa2usb_work_thread start: thread_id=0x%x",
3989 	    scsa2usbp->scsa2usb_work_thread_id);
3990 
3991 	ASSERT(scsa2usbp->scsa2usb_work_thread_id == (kthread_t *)1);
3992 	scsa2usbp->scsa2usb_work_thread_id = curthread;
3993 
3994 	/* exclude ugen accesses */
3995 	while (scsa2usbp->scsa2usb_transport_busy) {
3996 		cv_wait(&scsa2usbp->scsa2usb_transport_busy_cv,
3997 		    &scsa2usbp->scsa2usb_mutex);
3998 	}
3999 	ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0);
4000 	scsa2usbp->scsa2usb_transport_busy++;
4001 	scsa2usbp->scsa2usb_busy_thread = curthread;
4002 
4003 	scsa2usb_raise_power(scsa2usbp);
4004 
4005 	/* reopen the pipes if necessary */
4006 	(void) scsa2usb_open_usb_pipes(scsa2usbp);
4007 
4008 	for (;;) {
4009 		ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0);
4010 		for (lun = 0; lun < scsa2usbp->scsa2usb_n_luns; lun++) {
4011 			scsa2usb_transport_request(scsa2usbp, lun);
4012 		}
4013 		count = 0;
4014 		for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
4015 			count += usba_list_entry_count(
4016 				&scsa2usbp->scsa2usb_waitQ[lun]);
4017 		}
4018 
4019 		if (count == 0) {
4020 
4021 			break;
4022 		}
4023 	}
4024 
4025 	scsa2usbp->scsa2usb_work_thread_id = 0;
4026 
4027 	ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0);
4028 
4029 	scsa2usbp->scsa2usb_transport_busy--;
4030 	scsa2usbp->scsa2usb_busy_thread = NULL;
4031 	cv_signal(&scsa2usbp->scsa2usb_transport_busy_cv);
4032 
4033 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4034 	    "scsa2usb_work_thread: exit");
4035 
4036 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4037 
4038 	scsa2usb_pm_idle_component(scsa2usbp);
4039 }
4040 
4041 
4042 /*
4043  * scsa2usb_flush_waitQ:
4044  *	empties the entire waitQ with errors asap.
4045  *
4046  * It is called from scsa2usb_scsi_reset and scsa2usb_panic_callb.
4047  * If the device is reset; we should empty the waitQ right away.
4048  * If the system has paniced; we should empty the waitQ right away.
4049  *
4050  * CPR suspend will only succeed if device is idle. No need to call
4051  * this function for CPR suspend case.
4052  */
4053 static void
4054 scsa2usb_flush_waitQ(scsa2usb_state_t *scsa2usbp, uint_t lun,
4055     uchar_t error)
4056 {
4057 	struct scsi_pkt		*pkt;
4058 	struct scsa2usb_cmd	*cmd;
4059 	usba_list_entry_t	head;
4060 
4061 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4062 
4063 	usba_move_list(&scsa2usbp->scsa2usb_waitQ[lun], &head,
4064 	    scsa2usbp->scsa2usb_dev_data->dev_iblock_cookie);
4065 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4066 
4067 	while ((cmd = (scsa2usb_cmd_t *)usba_rm_first_pvt_from_list(&head)) !=
4068 	    NULL) {
4069 		pkt = cmd->cmd_pkt;
4070 		pkt->pkt_reason = error;	/* set error */
4071 
4072 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4073 		scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_DO_COMP;
4074 		scsa2usb_pkt_completion(scsa2usbp, pkt);
4075 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4076 	} /* end of while */
4077 }
4078 
4079 
4080 /*
4081  * scsa2usb_do_inquiry is performed before INIT CHILD and we have
4082  * to fake a few things normally done by SCSA
4083  */
4084 static void
4085 scsa2usb_do_inquiry(scsa2usb_state_t *scsa2usbp, uint_t target, uint_t lun)
4086 {
4087 	struct buf	*bp;
4088 	struct scsi_pkt *pkt;
4089 	struct scsi_address ap;
4090 	int		len = SCSA2USB_MAX_INQ_LEN;
4091 
4092 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4093 	    "scsa2usb_do_inquiry: %d bytes", len);
4094 
4095 	/* is it inquiry-challenged? */
4096 	if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_INQUIRY)) {
4097 		(void) scsa2usb_fake_inquiry(scsa2usbp, NULL, lun);
4098 
4099 		return;
4100 	}
4101 
4102 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4103 
4104 	bzero(&ap, sizeof (struct scsi_address));
4105 	ap.a_hba_tran = scsa2usbp->scsa2usb_tran;
4106 	ap.a_target = target;
4107 	ap.a_lun = lun;
4108 
4109 	/* limit inquiry to 36 bytes */
4110 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4111 	if ((bp = scsi_alloc_consistent_buf(&ap, (struct buf *)NULL,
4112 	    len, B_READ, SLEEP_FUNC, NULL)) == NULL) {
4113 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4114 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4115 		    scsa2usbp->scsa2usb_log_handle,
4116 		    "scsa2usb_do_inquiry: failed");
4117 
4118 		return;
4119 	}
4120 
4121 	pkt = scsi_init_pkt(&ap, NULL, bp, CDB_GROUP0, 1,
4122 	    PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL);
4123 
4124 	RQ_MAKECOM_G0(pkt, FLAG_NOINTR, (char)SCMD_INQUIRY, 0, len);
4125 
4126 	pkt->pkt_comp = NULL;
4127 	pkt->pkt_time = 5;
4128 	bzero(bp->b_un.b_addr, len);
4129 
4130 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4131 	    "scsa2usb_do_inquiry:INQUIRY");
4132 
4133 	(void) scsi_transport(pkt);
4134 
4135 	if (pkt->pkt_reason) {
4136 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4137 		    scsa2usbp->scsa2usb_log_handle,
4138 		    "INQUIRY failed, cannot determine device type, "
4139 		    "pkt_reason=0x%x", pkt->pkt_reason);
4140 
4141 		/* not much hope for other cmds, reduce */
4142 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4143 		scsa2usbp->scsa2usb_attrs &=
4144 					~SCSA2USB_ATTRS_REDUCED_CMD;
4145 		(void) scsa2usb_fake_inquiry(scsa2usbp, NULL, lun);
4146 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4147 	}
4148 
4149 	scsi_destroy_pkt(pkt);
4150 	scsi_free_consistent_buf(bp);
4151 
4152 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4153 }
4154 
4155 
4156 /*
4157  * scsa2usb_fake_inquiry:
4158  *    build an inquiry for a given device that doesnt like inquiry
4159  *    commands.
4160  */
4161 static int
4162 scsa2usb_fake_inquiry(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd,
4163     uint_t lun)
4164 {
4165 	usb_client_dev_data_t *dev_data = scsa2usbp->scsa2usb_dev_data;
4166 	struct scsi_inquiry *inqp;
4167 	int len;
4168 
4169 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4170 	    "scsa2usb_fake_inquiry:");
4171 
4172 	if (cmd) {
4173 		inqp = (struct scsi_inquiry *)cmd->cmd_bp->b_un.b_addr;
4174 	} else {
4175 		inqp = &scsa2usbp->scsa2usb_lun_inquiry[lun];
4176 	}
4177 	bzero(inqp, sizeof (struct scsi_inquiry));
4178 	for (len = 0; len < sizeof (inqp->inq_vid); len++) {
4179 		*(inqp->inq_vid + len) = ' ';
4180 	}
4181 
4182 	for (len = 0; len < sizeof (inqp->inq_pid); len++) {
4183 		*(inqp->inq_pid + len) = ' ';
4184 	}
4185 
4186 	inqp->inq_dtype = DTYPE_DIRECT;
4187 	inqp->inq_rmb = 1;
4188 	inqp->inq_ansi = 2;
4189 	inqp->inq_rdf = RDF_SCSI2;
4190 	inqp->inq_len = sizeof (struct scsi_inquiry)-4;
4191 
4192 	/* Fill in the Vendor id/Product id strings */
4193 	if (dev_data->dev_mfg) {
4194 		if ((len = strlen(dev_data->dev_mfg)) >
4195 		    sizeof (inqp->inq_vid)) {
4196 			len = sizeof (inqp->inq_vid);
4197 		}
4198 		bcopy(dev_data->dev_mfg, inqp->inq_vid, len);
4199 	}
4200 
4201 	if (dev_data->dev_product) {
4202 		if ((len = strlen(dev_data->dev_product)) >
4203 		    sizeof (inqp->inq_pid)) {
4204 			len = sizeof (inqp->inq_pid);
4205 		}
4206 		bcopy(dev_data->dev_product, inqp->inq_pid, len);
4207 	}
4208 
4209 	/* Set the Revision to the Device */
4210 	inqp->inq_revision[0] = 0x30 +
4211 		((dev_data->dev_descr->bcdDevice>>12) & 0xF);
4212 	inqp->inq_revision[1] = 0x30 +
4213 		((dev_data->dev_descr->bcdDevice>>8) & 0xF);
4214 	inqp->inq_revision[2] = 0x30 +
4215 		((dev_data->dev_descr->bcdDevice>>4) & 0xF);
4216 	inqp->inq_revision[3] = 0x30 +
4217 		((dev_data->dev_descr->bcdDevice) & 0xF);
4218 
4219 	/* Copy inquiry data in to soft state */
4220 	bcopy(inqp, &scsa2usbp->scsa2usb_lun_inquiry[lun],
4221 	    sizeof (struct scsi_inquiry));
4222 
4223 	return (sizeof (struct scsi_inquiry));
4224 }
4225 
4226 
4227 /*
4228  * scsa2usb_create_arq_pkt:
4229  *	Create and ARQ packet to get request sense data
4230  */
4231 static int
4232 scsa2usb_create_arq_pkt(scsa2usb_state_t *scsa2usbp, struct scsi_address *ap)
4233 {
4234 	struct buf *bp;
4235 	scsa2usb_cmd_t *arq_cmd;
4236 
4237 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4238 	    "scsa2usb_create_arq_pkt: scsa2usbp: %p, ap: %p", scsa2usbp, ap);
4239 
4240 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4241 
4242 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4243 	if ((bp = scsi_alloc_consistent_buf(ap, (struct buf *)NULL,
4244 	    SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL)) == NULL) {
4245 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4246 
4247 		return (USB_FAILURE);
4248 	}
4249 
4250 	arq_cmd = PKT2CMD(scsi_init_pkt(ap, NULL, bp, CDB_GROUP0, 1,
4251 	    PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL));
4252 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4253 
4254 	RQ_MAKECOM_G0(arq_cmd->cmd_pkt,
4255 	    FLAG_SENSING | FLAG_HEAD | FLAG_NODISCON,
4256 	    (char)SCMD_REQUEST_SENSE, 0, (char)SENSE_LENGTH);
4257 
4258 	arq_cmd->cmd_pkt->pkt_ha_private = arq_cmd;
4259 	scsa2usbp->scsa2usb_arq_cmd = arq_cmd;
4260 	scsa2usbp->scsa2usb_arq_bp = bp;
4261 	arq_cmd->cmd_pkt->pkt_comp = NULL;
4262 	bzero(bp->b_un.b_addr, SENSE_LENGTH);
4263 
4264 	return (USB_SUCCESS);
4265 }
4266 
4267 
4268 /*
4269  * scsa2usb_delete_arq_pkt:
4270  *	Destroy the ARQ packet
4271  */
4272 static void
4273 scsa2usb_delete_arq_pkt(scsa2usb_state_t *scsa2usbp)
4274 {
4275 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4276 	    "scsa2usb_delete_arq_pkt: cmd: 0x%p", scsa2usbp->scsa2usb_arq_cmd);
4277 
4278 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4279 
4280 	if (scsa2usbp->scsa2usb_arq_cmd != NULL) {
4281 		scsi_destroy_pkt(scsa2usbp->scsa2usb_arq_cmd->cmd_pkt);
4282 		scsi_free_consistent_buf(scsa2usbp->scsa2usb_arq_bp);
4283 	}
4284 	scsa2usbp->scsa2usb_arq_cmd = NULL;
4285 	scsa2usbp->scsa2usb_arq_bp = NULL;
4286 }
4287 
4288 
4289 /*
4290  * scsa2usb_complete_arq_pkt:
4291  *	finish processing the arq packet
4292  */
4293 static void
4294 scsa2usb_complete_arq_pkt(scsa2usb_state_t *scsa2usbp,
4295     struct scsi_pkt *pkt, scsa2usb_cmd_t *ssp, struct buf *bp)
4296 {
4297 	scsa2usb_cmd_t		*sp = pkt->pkt_ha_private;
4298 	struct scsi_arq_status	*arqp;
4299 
4300 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4301 
4302 	arqp = (struct scsi_arq_status *)(ssp->cmd_pkt->pkt_scbp);
4303 	arqp->sts_rqpkt_status = *((struct scsi_status *)
4304 		(sp->cmd_pkt->pkt_scbp));
4305 	arqp->sts_rqpkt_reason = CMD_CMPLT;
4306 	arqp->sts_rqpkt_state |= STATE_XFERRED_DATA;
4307 	arqp->sts_rqpkt_statistics = arqp->sts_rqpkt_resid = 0;
4308 
4309 	/* is this meaningful sense data */
4310 	if (*(bp->b_un.b_addr) != 0) {
4311 		bcopy(bp->b_un.b_addr, &arqp->sts_sensedata, sp->cmd_scblen);
4312 		ssp->cmd_pkt->pkt_state |= STATE_ARQ_DONE;
4313 	}
4314 
4315 	/* we will not sense start cmd until we receive a NOT READY */
4316 	if (arqp->sts_sensedata.es_key == KEY_NOT_READY) {
4317 		scsa2usbp->scsa2usb_rcvd_not_ready = B_TRUE;
4318 	}
4319 }
4320 
4321 
4322 /*
4323  * Miscellaneous functions for any command/transport
4324  */
4325 /*
4326  * scsa2usb_open_usb_pipes:
4327  *	set up a pipe policy
4328  *	open usb bulk pipes (BO and CB/CBI)
4329  *	open usb interrupt pipe (CBI)
4330  */
4331 static int
4332 scsa2usb_open_usb_pipes(scsa2usb_state_t *scsa2usbp)
4333 {
4334 	int			rval;
4335 	usb_pipe_policy_t	policy;	/* bulk pipe policy */
4336 	size_t			sz;
4337 
4338 	ASSERT(scsa2usbp);
4339 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4340 
4341 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4342 	    "scsa2usb_open_usb_pipes: dip = 0x%p flag = 0x%x",
4343 	    scsa2usbp->scsa2usb_dip, scsa2usbp->scsa2usb_flags);
4344 
4345 	if (!(scsa2usbp->scsa2usb_flags & SCSA2USB_FLAGS_PIPES_OPENED)) {
4346 
4347 		/*
4348 		 * one pipe policy for all bulk pipes
4349 		 */
4350 		bzero(&policy, sizeof (usb_pipe_policy_t));
4351 		/* at least 2, for the normal and exceptional callbacks */
4352 		policy.pp_max_async_reqs = 1;
4353 
4354 		USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4355 		    "scsa2usb_open_usb_pipes: opening bulk pipes");
4356 
4357 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4358 
4359 		/* Open the USB bulk-in pipe */
4360 		if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip,
4361 		    &scsa2usbp->scsa2usb_bulkin_ept, &policy, USB_FLAGS_SLEEP,
4362 		    &scsa2usbp->scsa2usb_bulkin_pipe)) != USB_SUCCESS) {
4363 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4364 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4365 			    scsa2usbp->scsa2usb_log_handle,
4366 			    "scsa2usb_open_usb_pipes: bulk/in pipe open "
4367 			    " failed rval = %d", rval);
4368 
4369 			return (USB_FAILURE);
4370 		}
4371 
4372 		/* Open the bulk-out pipe  using the same policy */
4373 		if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip,
4374 		    &scsa2usbp->scsa2usb_bulkout_ept, &policy, USB_FLAGS_SLEEP,
4375 		    &scsa2usbp->scsa2usb_bulkout_pipe)) != USB_SUCCESS) {
4376 			usb_pipe_close(scsa2usbp->scsa2usb_dip,
4377 			    scsa2usbp->scsa2usb_bulkin_pipe,
4378 			    USB_FLAGS_SLEEP, NULL, NULL);
4379 
4380 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4381 			scsa2usbp->scsa2usb_bulkin_pipe = NULL;
4382 
4383 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4384 			    scsa2usbp->scsa2usb_log_handle,
4385 			    "scsa2usb_open_usb_pipes: bulk/out pipe open"
4386 			    " failed rval = %d", rval);
4387 
4388 			return (USB_FAILURE);
4389 		}
4390 
4391 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4392 
4393 		/* open interrupt pipe for CBI protocol */
4394 		if (SCSA2USB_IS_CBI(scsa2usbp)) {
4395 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4396 
4397 			if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip,
4398 			    &scsa2usbp->scsa2usb_intr_ept, &policy,
4399 			    USB_FLAGS_SLEEP, &scsa2usbp->scsa2usb_intr_pipe)) !=
4400 			    USB_SUCCESS) {
4401 				usb_pipe_close(scsa2usbp->scsa2usb_dip,
4402 				    scsa2usbp->scsa2usb_bulkin_pipe,
4403 				    USB_FLAGS_SLEEP, NULL, NULL);
4404 
4405 				usb_pipe_close(scsa2usbp->scsa2usb_dip,
4406 				    scsa2usbp->scsa2usb_bulkout_pipe,
4407 				    USB_FLAGS_SLEEP, NULL, NULL);
4408 
4409 				mutex_enter(&scsa2usbp->scsa2usb_mutex);
4410 				scsa2usbp->scsa2usb_bulkin_pipe = NULL;
4411 				scsa2usbp->scsa2usb_bulkout_pipe = NULL;
4412 
4413 				USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4414 				    scsa2usbp->scsa2usb_log_handle,
4415 				    "scsa2usb_open_usb_pipes: intr pipe open"
4416 				    " failed rval = %d", rval);
4417 
4418 				return (USB_FAILURE);
4419 			}
4420 
4421 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4422 		}
4423 
4424 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4425 
4426 		/* get the max transfer size of the bulk pipe */
4427 		if (usb_pipe_get_max_bulk_transfer_size(scsa2usbp->scsa2usb_dip,
4428 		    &sz) == USB_SUCCESS) {
4429 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4430 			scsa2usbp->scsa2usb_max_bulk_xfer_size = sz;
4431 		} else {
4432 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4433 			scsa2usbp->scsa2usb_max_bulk_xfer_size = DEV_BSIZE;
4434 		}
4435 
4436 		/* limit the xfer size */
4437 		scsa2usbp->scsa2usb_max_bulk_xfer_size = min(
4438 			scsa2usbp->scsa2usb_max_bulk_xfer_size,
4439 			scsa2usb_max_bulk_xfer_size);
4440 
4441 		USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4442 		    "scsa2usb_open_usb_pipes: max bulk transfer size = %lx",
4443 		    scsa2usbp->scsa2usb_max_bulk_xfer_size);
4444 
4445 		/* Set the pipes opened flag */
4446 		scsa2usbp->scsa2usb_flags |= SCSA2USB_FLAGS_PIPES_OPENED;
4447 
4448 		scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_NORMAL;
4449 
4450 		/* Set the state to NONE */
4451 		scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_NONE;
4452 	}
4453 
4454 	return (USB_SUCCESS);
4455 }
4456 
4457 
4458 /*
4459  * scsa2usb_close_usb_pipes:
4460  *	close all pipes synchronously
4461  */
4462 void
4463 scsa2usb_close_usb_pipes(scsa2usb_state_t *scsa2usbp)
4464 {
4465 	usb_flags_t flags = USB_FLAGS_SLEEP;
4466 
4467 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4468 	    "scsa2usb_close_usb_pipes: scsa2usb_state = 0x%p", scsa2usbp);
4469 
4470 	ASSERT(scsa2usbp);
4471 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4472 
4473 	if ((scsa2usbp->scsa2usb_flags & SCSA2USB_FLAGS_PIPES_OPENED) == 0) {
4474 
4475 		return;
4476 	}
4477 
4478 	scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_CLOSING;
4479 	/* to avoid races, reset the flag first */
4480 	scsa2usbp->scsa2usb_flags &= ~SCSA2USB_FLAGS_PIPES_OPENED;
4481 
4482 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4483 
4484 	usb_pipe_close(scsa2usbp->scsa2usb_dip,
4485 	    scsa2usbp->scsa2usb_bulkout_pipe, flags, NULL, NULL);
4486 
4487 	usb_pipe_close(scsa2usbp->scsa2usb_dip,
4488 	    scsa2usbp->scsa2usb_bulkin_pipe, flags, NULL, NULL);
4489 
4490 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4491 	if (SCSA2USB_IS_CBI(scsa2usbp)) {
4492 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4493 		usb_pipe_close(scsa2usbp->scsa2usb_dip,
4494 		    scsa2usbp->scsa2usb_intr_pipe, flags, NULL, NULL);
4495 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4496 	}
4497 	scsa2usbp->scsa2usb_bulkout_pipe = NULL;
4498 	scsa2usbp->scsa2usb_bulkin_pipe = NULL;
4499 	scsa2usbp->scsa2usb_intr_pipe = NULL;
4500 
4501 	scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_NORMAL;
4502 }
4503 
4504 
4505 /*
4506  * scsa2usb_fill_up_cdb_lba:
4507  *	fill up command CDBs' LBA part
4508  */
4509 static void
4510 scsa2usb_fill_up_cdb_lba(scsa2usb_cmd_t *cmd, int lba)
4511 {
4512 	/* zero cdb1, lba bits so they won't get copied in the new cdb */
4513 	cmd->cmd_cdb[SCSA2USB_LUN] &= 0xE0;
4514 	cmd->cmd_cdb[SCSA2USB_LBA_0] = lba >> 24;
4515 	cmd->cmd_cdb[SCSA2USB_LBA_1] = lba >> 16;
4516 	cmd->cmd_cdb[SCSA2USB_LBA_2] = lba >> 8;
4517 	cmd->cmd_cdb[SCSA2USB_LBA_3] = (uchar_t)lba;
4518 	cmd->cmd_lba = lba;
4519 }
4520 
4521 
4522 /*
4523  * scsa2usb_fill_up_ReadCD_cdb_len:
4524  *	fill up READ_CD command CDBs' len part
4525  */
4526 static void
4527 scsa2usb_fill_up_ReadCD_cdb_len(scsa2usb_cmd_t *cmd, int len, int actual_len)
4528 {
4529 	cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_0] = len >> 16;
4530 	cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_1] = len >> 8;
4531 	cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_2] = (uchar_t)len;
4532 	cmd->cmd_actual_len = (uchar_t)actual_len;
4533 }
4534 
4535 
4536 /*
4537  * scsa2usb_fill_up_12byte_cdb_len:
4538  *	fill up generic 12-byte command CDBs' len part
4539  */
4540 static void
4541 scsa2usb_fill_up_12byte_cdb_len(scsa2usb_cmd_t *cmd, int len, int actual_len)
4542 {
4543 	cmd->cmd_cdb[6] = len >> 24;
4544 	cmd->cmd_cdb[7] = len >> 16;
4545 	cmd->cmd_cdb[8] = len >> 8;
4546 	cmd->cmd_cdb[9] = (uchar_t)len;
4547 	cmd->cmd_actual_len = (uchar_t)actual_len;
4548 }
4549 
4550 
4551 /*
4552  * scsa2usb_fill_up_cdb_len:
4553  *	fill up generic 10-byte command CDBs' len part
4554  */
4555 static void
4556 scsa2usb_fill_up_cdb_len(scsa2usb_cmd_t *cmd, int len)
4557 {
4558 	cmd->cmd_cdb[SCSA2USB_LEN_0] = len >> 8;
4559 	cmd->cmd_cdb[SCSA2USB_LEN_1] = (uchar_t)len;
4560 }
4561 
4562 
4563 /*
4564  * scsa2usb_read_cd_blk_size:
4565  *	For SCMD_READ_CD opcode (0xbe). Figure out the
4566  *	block size based on expected sector type field
4567  *	definition. See MMC SCSI Specs section 6.1.15
4568  *
4569  *	Based on the value of the "expected_sector_type"
4570  *	field, the block size could be different.
4571  */
4572 static int
4573 scsa2usb_read_cd_blk_size(uchar_t expected_sector_type)
4574 {
4575 	int blk_size;
4576 
4577 	switch (expected_sector_type) {
4578 	case READ_CD_EST_CDDA:
4579 		blk_size = CDROM_BLK_2352;
4580 		break;
4581 	case READ_CD_EST_MODE2:
4582 		blk_size = CDROM_BLK_2336;
4583 		break;
4584 	case READ_CD_EST_MODE2FORM2:
4585 		blk_size = CDROM_BLK_2324;
4586 		break;
4587 	case READ_CD_EST_MODE2FORM1:
4588 	case READ_CD_EST_ALLTYPE:
4589 	case READ_CD_EST_MODE1:
4590 	default:
4591 		blk_size = CDROM_BLK_2048;
4592 	}
4593 
4594 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, NULL, "scsa2usb_read_cd_blk_size: "
4595 	    "est = 0x%x blk_size = %d", expected_sector_type, blk_size);
4596 
4597 	return (blk_size);
4598 }
4599 
4600 
4601 /* needed for esballoc_wait() */
4602 /*ARGSUSED*/
4603 static void
4604 scsa2usb_null_free(char *arg)
4605 {
4606 }
4607 
4608 static frtn_t fr = {
4609 	scsa2usb_null_free,
4610 	NULL
4611 };
4612 
4613 
4614 /*
4615  * scsa2usb_bp_to_mblk:
4616  *	Convert a bp to mblk_t. USBA framework understands mblk_t.
4617  */
4618 static mblk_t *
4619 scsa2usb_bp_to_mblk(scsa2usb_state_t *scsa2usbp)
4620 {
4621 	size_t		size;
4622 	mblk_t		*mp;
4623 	struct buf	*bp;
4624 	scsa2usb_cmd_t	*cmd = PKT2CMD(scsa2usbp->scsa2usb_cur_pkt);
4625 
4626 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4627 	    "scsa2usb_bp_to_mblk: ");
4628 
4629 	ASSERT(scsa2usbp->scsa2usb_cur_pkt);
4630 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4631 
4632 	bp = cmd->cmd_bp;
4633 
4634 	if (bp && (bp->b_bcount > 0)) {
4635 		size = ((bp->b_bcount > cmd->cmd_xfercount) ?
4636 				cmd->cmd_xfercount : bp->b_bcount);
4637 	} else {
4638 
4639 		return (NULL);
4640 	}
4641 
4642 	mp = esballoc_wait((uchar_t *)bp->b_un.b_addr + cmd->cmd_offset,
4643 						size, BPRI_LO, &fr);
4644 
4645 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4646 	    "scsa2usb_bp_to_mblk: "
4647 	    "mp=0x%p bp=0x%p pkt=0x%p off=0x%lx sz=%lu add=0x%p", mp,
4648 	    (void *)bp, scsa2usbp->scsa2usb_cur_pkt, cmd->cmd_offset,
4649 	    bp->b_bcount - cmd->cmd_offset,
4650 	    bp->b_un.b_addr);
4651 
4652 	mp->b_wptr += size;
4653 	cmd->cmd_offset += size;
4654 
4655 	return (mp);
4656 }
4657 
4658 
4659 /*
4660  * scsa2usb_handle_data_start:
4661  *	Initiate the data xfer. It could be IN/OUT direction.
4662  *
4663  *	Data IN:
4664  *		Send out the bulk-xfer request
4665  *		if rval implies STALL
4666  *			clear endpoint stall and reset bulk-in pipe
4667  *			handle data read in so far; set cmd->cmd_done
4668  *			also adjust data xfer length accordingly
4669  *		else other error
4670  *			report back to transport
4671  *			typically transport will call reset recovery
4672  *		else (no error)
4673  *			return success
4674  *
4675  *	Data OUT:
4676  *		Send out the bulk-xfer request
4677  *		if rval implies STALL
4678  *			clear endpoint stall and reset bulk-in pipe
4679  *			adjust data xfer length
4680  *		else other error
4681  *			report back to transport
4682  *			typically transport will call reset recovery
4683  *		else (no error)
4684  *			return success
4685  *
4686  *	NOTE: We call this function only if there is xfercount.
4687  */
4688 int
4689 scsa2usb_handle_data_start(scsa2usb_state_t *scsa2usbp,
4690     scsa2usb_cmd_t *cmd, usb_bulk_req_t *req)
4691 {
4692 	int		rval = USB_SUCCESS;
4693 	uint_t		ept_addr;
4694 	usb_flags_t	flags = USB_FLAGS_SLEEP;
4695 #ifdef	SCSA2USB_BULK_ONLY_TEST
4696 	usb_req_attrs_t	attrs = 0;
4697 #else
4698 	usb_req_attrs_t	attrs = USB_ATTRS_SHORT_XFER_OK;
4699 #endif
4700 
4701 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4702 	    "scsa2usb_handle_data_start: BEGIN cmd = %p, req = %p", cmd, req);
4703 
4704 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4705 
4706 	switch (cmd->cmd_dir) {
4707 	case USB_EP_DIR_IN:
4708 #ifdef	SCSA2USB_BULK_ONLY_TEST
4709 		/*
4710 		 * This case occurs when the host expects to receive
4711 		 * more data than the device actually transfers. Hi > Di
4712 		 */
4713 		if (scsa2usb_test_case_5) {
4714 			usb_bulk_req_t *req2;
4715 
4716 			req->bulk_len = cmd->cmd_xfercount - 1;
4717 			req->bulk_attributes = 0;
4718 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4719 			SCSA2USB_FREE_MSG(req->bulk_data);
4720 			req->bulk_data = allocb_wait(req->bulk_len, BPRI_LO,
4721 			    STR_NOSIG, NULL);
4722 
4723 			ASSERT(req->bulk_timeout);
4724 			rval = usb_pipe_bulk_xfer(
4725 			    scsa2usbp->scsa2usb_bulkin_pipe, req, flags);
4726 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4727 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4728 			    scsa2usbp->scsa2usb_log_handle, "rval = %x", rval);
4729 
4730 			req2 = scsa2usb_init_bulk_req(scsa2usbp,
4731 			    cmd->cmd_xfercount + 2,
4732 			    cmd->cmd_timeout, 0, flags);
4733 			req2->bulk_len = cmd->cmd_xfercount + 2;
4734 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4735 
4736 			ASSERT(req2->bulk_timeout);
4737 			rval = usb_pipe_bulk_xfer(
4738 			    scsa2usbp->scsa2usb_bulkin_pipe, req2, flags);
4739 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4740 
4741 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4742 			    scsa2usbp->scsa2usb_log_handle,
4743 			    "TEST 5: Hi > Di: rval = 0x%x", rval);
4744 			scsa2usb_test_case_5 = 0;
4745 			usb_free_bulk_req(req2);
4746 
4747 			return (rval);
4748 		}
4749 
4750 		/*
4751 		 * This happens when the host expects to send data to the
4752 		 * device while the device intends to send data to the host.
4753 		 */
4754 		if (scsa2usb_test_case_8 && (cmd->cmd_cdb[0] == SCMD_READ_G1)) {
4755 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4756 			    scsa2usbp->scsa2usb_log_handle,
4757 			    "TEST 8: Hi <> Do: Step 2");
4758 			scsa2usb_test_mblk(scsa2usbp, B_TRUE);
4759 			scsa2usb_test_case_8 = 0;
4760 
4761 			return (rval);
4762 		}
4763 #endif	/* SCSA2USB_BULK_ONLY_TEST */
4764 
4765 		ept_addr = scsa2usbp->scsa2usb_bulkin_ept.bEndpointAddress;
4766 		req->bulk_len = cmd->cmd_xfercount;
4767 		req->bulk_attributes = attrs;
4768 		SCSA2USB_FREE_MSG(req->bulk_data);
4769 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4770 
4771 		req->bulk_data = esballoc_wait(
4772 				(uchar_t *)cmd->cmd_bp->b_un.b_addr +
4773 				cmd->cmd_offset,
4774 				req->bulk_len, BPRI_LO, &fr);
4775 
4776 		ASSERT(req->bulk_timeout);
4777 		rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkin_pipe,
4778 								req, flags);
4779 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4780 
4781 		break;
4782 
4783 	case USB_EP_DIR_OUT:
4784 #ifdef	SCSA2USB_BULK_ONLY_TEST
4785 		/*
4786 		 * This happens when the host expects to receive data
4787 		 * from the device while the device intends to receive
4788 		 * data from the host.
4789 		 */
4790 		if (scsa2usb_test_case_10 &&
4791 		    (cmd->cmd_cdb[0] == SCMD_WRITE_G1)) {
4792 			req->bulk_len = CSW_LEN;
4793 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4794 
4795 			ASSERT(req->bulk_timeout);
4796 			rval = usb_pipe_bulk_xfer(
4797 			    scsa2usbp->scsa2usb_bulkin_pipe, req, flags);
4798 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4799 
4800 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4801 			    scsa2usbp->scsa2usb_log_handle,
4802 			    "TEST 10: Ho <> Di: done rval = 0x%x",  rval);
4803 			scsa2usb_test_case_10 = 0;
4804 
4805 			return (rval);
4806 		}
4807 #endif	/* SCSA2USB_BULK_ONLY_TEST */
4808 
4809 		req->bulk_data = scsa2usb_bp_to_mblk(scsa2usbp);
4810 		if (req->bulk_data == NULL) {
4811 
4812 			return (USB_FAILURE);
4813 		}
4814 
4815 #ifdef	SCSA2USB_BULK_ONLY_TEST
4816 		if (scsa2usb_test_case_11) {
4817 			/*
4818 			 * Host expects to send data to the device and
4819 			 * device doesn't expect to receive any data
4820 			 */
4821 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4822 			    scsa2usbp->scsa2usb_log_handle, "TEST 11: Ho > Do");
4823 
4824 			scsa2usb_test_mblk(scsa2usbp, B_FALSE);
4825 			scsa2usb_test_case_11 = 0;
4826 		}
4827 #endif	/* SCSA2USB_BULK_ONLY_TEST */
4828 
4829 		ept_addr = scsa2usbp->scsa2usb_bulkout_ept.bEndpointAddress;
4830 		req->bulk_len = req->bulk_data->b_wptr - req->bulk_data->b_rptr;
4831 		req->bulk_timeout = scsa2usb_bulk_timeout(cmd->cmd_timeout);
4832 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4833 
4834 		ASSERT(req->bulk_timeout);
4835 		rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkout_pipe,
4836 								req, flags);
4837 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4838 		break;
4839 	}
4840 
4841 	USB_DPRINTF_L3(DPRINT_MASK_SCSA,
4842 	    scsa2usbp->scsa2usb_log_handle,
4843 	    "scsa2usb_handle_data_start: rval=%d cr=%d", rval,
4844 	    req->bulk_completion_reason);
4845 
4846 	if (rval != USB_SUCCESS) {
4847 		/* Handle Errors now */
4848 		if (req->bulk_completion_reason == USB_CR_STALL) {
4849 			if (cmd->cmd_dir == USB_EP_DIR_IN) {
4850 				(void) scsa2usb_clear_ept_stall(
4851 				    scsa2usbp, ept_addr,
4852 				    scsa2usbp-> scsa2usb_bulkin_pipe,
4853 				    "bulk-in");
4854 			} else {
4855 				(void) scsa2usb_clear_ept_stall(
4856 				    scsa2usbp, ept_addr,
4857 				    scsa2usbp-> scsa2usb_bulkout_pipe,
4858 				    "bulk-out");
4859 			}
4860 		}
4861 
4862 		/* no more data to transfer after this */
4863 		cmd->cmd_done = 1;
4864 	}
4865 
4866 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4867 	    "scsa2usb_handle_data_start: END %s data rval = %d",
4868 	    (cmd->cmd_dir == USB_EP_DIR_IN) ? "bulk-in" : "bulk-out", rval);
4869 
4870 	return (rval);
4871 }
4872 
4873 
4874 /*
4875  * scsa2usb_handle_data_done:
4876  *	This function handles the completion of the data xfer.
4877  *	It also massages the inquiry data. This function may
4878  *	also be called after a stall.
4879  */
4880 void
4881 scsa2usb_handle_data_done(scsa2usb_state_t *scsa2usbp,
4882     scsa2usb_cmd_t *cmd, usb_bulk_req_t *req)
4883 {
4884 	struct buf	*bp = cmd->cmd_bp;
4885 	struct scsi_pkt	*pkt = scsa2usbp->scsa2usb_cur_pkt;
4886 	mblk_t		*data = req->bulk_data;
4887 	int		len = data ? (data->b_wptr - data->b_rptr) : 0;
4888 
4889 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4890 
4891 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4892 	    "scsa2usb_handle_data_done:\n\tcmd = 0x%p data = 0x%p len = 0x%x",
4893 	    cmd, data, len);
4894 
4895 	cmd->cmd_resid_xfercount = cmd->cmd_xfercount - len;
4896 
4897 	if (len)  {
4898 		uchar_t	*p;
4899 		scsa2usb_read_cap_t *cap;
4900 
4901 		switch (cmd->cmd_cdb[SCSA2USB_OPCODE]) {
4902 		case SCMD_INQUIRY:
4903 			/*
4904 			 * cache a copy of the inquiry data for our own use
4905 			 * but ensure that we have at least up to
4906 			 * inq_revision, inq_serial is not required.
4907 			 * ignore inquiry data returned for inquiry commands
4908 			 * with SCSI-3 EVPD, CmdDt bits set.
4909 			 */
4910 			if (((cmd->cmd_cdb[SCSA2USB_LUN] & 0x1f) == 0) &&
4911 			    (len >= SCSA2USB_MAX_INQ_LEN)) {
4912 				bzero(&scsa2usbp->scsa2usb_lun_inquiry
4913 				    [pkt->pkt_address.a_lun],
4914 				    sizeof (struct scsi_inquiry));
4915 				bcopy(data->b_rptr,
4916 				    &scsa2usbp->scsa2usb_lun_inquiry
4917 				    [pkt->pkt_address.a_lun], len);
4918 			}
4919 
4920 			USB_DPRINTF_L3(DPRINT_MASK_SCSA,
4921 			    scsa2usbp->scsa2usb_log_handle,
4922 			    "scsi inquiry type = 0x%x",
4923 			    scsa2usbp->scsa2usb_lun_inquiry
4924 			    [pkt->pkt_address.a_lun].inq_dtype);
4925 
4926 			cmd->cmd_done = 1;
4927 			goto handle_data;
4928 
4929 		case SCMD_READ_CAPACITY:
4930 			cap = (scsa2usb_read_cap_t *)data->b_rptr;
4931 
4932 			/* Figure out the logical block size */
4933 			if ((len >= sizeof (struct scsa2usb_read_cap)) &&
4934 			    (req->bulk_completion_reason == USB_CR_OK)) {
4935 				scsa2usbp->
4936 				    scsa2usb_lbasize[pkt->pkt_address.a_lun] =
4937 				    SCSA2USB_MK_32BIT(
4938 					    cap->scsa2usb_read_cap_blen3,
4939 					    cap->scsa2usb_read_cap_blen2,
4940 					    cap->scsa2usb_read_cap_blen1,
4941 					    cap->scsa2usb_read_cap_blen0);
4942 
4943 				USB_DPRINTF_L3(DPRINT_MASK_SCSA,
4944 				    scsa2usbp->scsa2usb_log_handle,
4945 				    "lbasize=%d", scsa2usbp->
4946 				    scsa2usb_lbasize[pkt->pkt_address.a_lun]);
4947 			}
4948 			cmd->cmd_done = 1;
4949 			goto handle_data;
4950 
4951 		case SCMD_REQUEST_SENSE:
4952 			p = data->b_rptr;
4953 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4954 			    scsa2usbp->scsa2usb_log_handle,
4955 			    "cdb: %x rqsense: "
4956 			    "%x %x %x %x %x %x %x %x %x %x\n\t"
4957 			    "%x %x %x %x %x %x %x %x %x %x",
4958 			    cmd->cmd_cdb[0],
4959 			    p[0], p[1], p[2], p[3], p[4],
4960 			    p[5], p[6], p[7], p[8], p[9],
4961 			    p[10], p[11], p[12], p[13], p[14],
4962 			    p[15], p[16], p[17], p[18], p[19]);
4963 
4964 			scsa2usbp->scsa2usb_last_cmd.status = p[2];
4965 			cmd->cmd_done = 1;
4966 			/* FALLTHROUGH */
4967 
4968 		default:
4969 handle_data:
4970 			if (bp && len && (cmd->cmd_dir == USB_EP_DIR_IN)) {
4971 				/*
4972 				 * we don't have to copy the data, the
4973 				 * data pointers for the mblk_t for
4974 				 * the bulk-in xfer points to the
4975 				 * struct buf * data.
4976 				 */
4977 				cmd->cmd_offset += len;
4978 			}
4979 
4980 			USB_DPRINTF_L3(DPRINT_MASK_SCSA,
4981 			    scsa2usbp->scsa2usb_log_handle,
4982 			    "len = 0x%x total = 0x%lx",
4983 			    len, cmd->cmd_total_xfercount);
4984 
4985 			/*
4986 			 * update total_xfercount now but it may be
4987 			 * adjusted after receiving the residue
4988 			 */
4989 			cmd->cmd_total_xfercount -= len;
4990 
4991 			if ((req->bulk_completion_reason != USB_CR_OK) ||
4992 			    (cmd->cmd_resid_xfercount != 0) ||
4993 			    (cmd->cmd_total_xfercount == 0)) {
4994 				/* set pkt_resid to total to be sure */
4995 				pkt->pkt_resid = cmd->cmd_total_xfercount;
4996 				cmd->cmd_done = 1;
4997 			}
4998 
4999 			break;
5000 		}
5001 	} else {
5002 		if (cmd->cmd_dir == USB_EP_DIR_OUT) {
5003 			if (cmd->cmd_total_xfercount == 0) {
5004 				cmd->cmd_done = 1;
5005 			}
5006 		}
5007 	}
5008 }
5009 
5010 
5011 /*
5012  * scsa2usb_init_bulk_req:
5013  *	Allocate (synchronously) and fill in a bulk-request
5014  */
5015 usb_bulk_req_t *
5016 scsa2usb_init_bulk_req(scsa2usb_state_t *scsa2usbp, size_t length,
5017     uint_t timeout, usb_req_attrs_t attrs, usb_flags_t flags)
5018 {
5019 	usb_bulk_req_t	*req;
5020 
5021 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5022 
5023 	req = usb_alloc_bulk_req(scsa2usbp->scsa2usb_dip, length,
5024 	    flags | USB_FLAGS_SLEEP);
5025 
5026 	req->bulk_len = length;			/* xfer length */
5027 	req->bulk_timeout = scsa2usb_bulk_timeout(timeout); /* xfer timeout */
5028 	req->bulk_attributes = attrs;		/* xfer attrs */
5029 	req->bulk_client_private = (usb_opaque_t)scsa2usbp; /* statep */
5030 
5031 	return (req);
5032 }
5033 
5034 
5035 /*
5036  * scsa2usb_bulk_timeout:
5037  *	ensure that bulk requests do not have infinite timeout values
5038  */
5039 int
5040 scsa2usb_bulk_timeout(int timeout)
5041 {
5042 	return ((timeout == 0) ? scsa2usb_long_timeout : timeout);
5043 }
5044 
5045 
5046 /*
5047  * scsa2usb_clear_ept_stall:
5048  *	clear endpoint stall and reset pipes
5049  */
5050 int
5051 scsa2usb_clear_ept_stall(scsa2usb_state_t *scsa2usbp, uint_t ept_addr,
5052     usb_pipe_handle_t ph, char *what)
5053 {
5054 	int rval;
5055 	dev_info_t *dip = scsa2usbp->scsa2usb_dip;
5056 
5057 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5058 	if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
5059 
5060 		return (USB_FAILURE);
5061 	}
5062 
5063 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5064 	rval = usb_clr_feature(dip, USB_DEV_REQ_RCPT_EP, 0, ept_addr,
5065 	    USB_FLAGS_SLEEP, NULL, NULL);
5066 
5067 	usb_pipe_reset(dip, ph, USB_FLAGS_SLEEP, NULL, NULL);
5068 
5069 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5070 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5071 	    "scsa2usb_clear_ept_stall: on %s: ept = 0x%x rval = %d",
5072 	    what, ept_addr, rval);
5073 
5074 	return (rval);
5075 }
5076 
5077 
5078 /*
5079  * scsa2usb_pkt_completion:
5080  *	Handle pkt completion.
5081  */
5082 static void
5083 scsa2usb_pkt_completion(scsa2usb_state_t *scsa2usbp, struct scsi_pkt *pkt)
5084 {
5085 	scsa2usb_cmd_t *cmd = PKT2CMD(pkt);
5086 
5087 	ASSERT(pkt);
5088 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5089 
5090 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5091 	    "scsa2usb_pkt_completion:\n\tscsa2usbp = 0x%p "
5092 	    "reason=%d, status=%d state=0x%x stats=0x%x resid=0x%lx",
5093 	    scsa2usbp, pkt->pkt_reason, *(pkt->pkt_scbp),
5094 	    pkt->pkt_state, pkt->pkt_statistics, pkt->pkt_resid);
5095 
5096 	if (pkt->pkt_reason == CMD_CMPLT) {
5097 		pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET |
5098 					STATE_SENT_CMD | STATE_GOT_STATUS;
5099 		if (cmd->cmd_xfercount) {
5100 			pkt->pkt_state |= STATE_XFERRED_DATA;
5101 		}
5102 	} else {
5103 		pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET |
5104 					STATE_SENT_CMD;
5105 	}
5106 
5107 	/*
5108 	 * don't zap the current state when in panic as this will
5109 	 * make debugging harder
5110 	 */
5111 	if ((scsa2usbp->scsa2usb_cur_pkt == pkt) && !ddi_in_panic()) {
5112 		SCSA2USB_RESET_CUR_PKT(scsa2usbp);
5113 
5114 		/* save the last command */
5115 		bcopy(pkt->pkt_cdbp, scsa2usbp->scsa2usb_last_cmd.cdb,
5116 		    sizeof (scsa2usbp->scsa2usb_last_cmd.cdb));
5117 
5118 		/* reset the scsa2usb_last_cmd.status value */
5119 		if ((pkt->pkt_cdbp[0] != SCMD_REQUEST_SENSE) &&
5120 		    (pkt->pkt_cdbp[0] != SCMD_INQUIRY)) {
5121 			scsa2usbp->scsa2usb_last_cmd.status = 0;
5122 		}
5123 
5124 		/*
5125 		 * set pkt state to NONE *before* calling back as the target
5126 		 * driver will immediately submit the next packet
5127 		 */
5128 		scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_NONE;
5129 	}
5130 
5131 	if (pkt->pkt_comp) {
5132 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5133 		pkt->pkt_comp(pkt);
5134 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5135 
5136 	}
5137 }
5138 
5139 
5140 /*
5141  * Even handling functions:
5142  *
5143  * scsa2usb_reconnect_event_cb:
5144  *	event handling
5145  */
5146 static int
5147 scsa2usb_reconnect_event_cb(dev_info_t *dip)
5148 {
5149 	scsa2usb_state_t *scsa2usbp =
5150 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
5151 	dev_info_t	*cdip;
5152 	int		circ;
5153 	int		rval = USB_SUCCESS;
5154 
5155 	ASSERT(scsa2usbp != NULL);
5156 
5157 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5158 	    "scsa2usb_reconnect_event_cb: dip = 0x%p", dip);
5159 
5160 	scsa2usb_restore_device_state(dip, scsa2usbp);
5161 
5162 	USB_DPRINTF_L0(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5163 	    "Reinserted device is accessible again.");
5164 
5165 	ndi_devi_enter(dip, &circ);
5166 	for (cdip = ddi_get_child(dip); cdip; ) {
5167 		dev_info_t *next = ddi_get_next_sibling(cdip);
5168 
5169 		mutex_enter(&DEVI(cdip)->devi_lock);
5170 		DEVI_SET_DEVICE_REINSERTED(cdip);
5171 		mutex_exit(&DEVI(cdip)->devi_lock);
5172 
5173 		cdip = next;
5174 	}
5175 	ndi_devi_exit(dip, circ);
5176 
5177 	/* stop suppressing warnings */
5178 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5179 	scsa2usbp->scsa2usb_warning_given = B_FALSE;
5180 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5181 
5182 	if (scsa2usbp->scsa2usb_ugen_hdl) {
5183 		rval = usb_ugen_reconnect_ev_cb(
5184 			scsa2usbp->scsa2usb_ugen_hdl);
5185 	}
5186 
5187 	return (rval);
5188 }
5189 
5190 
5191 /*
5192  * scsa2usb_all_waitQs_empty:
5193  *	check if all waitQs empty
5194  */
5195 static int
5196 scsa2usb_all_waitQs_empty(scsa2usb_state_t *scsa2usbp)
5197 {
5198 	uint_t	lun;
5199 
5200 	for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
5201 		if (usba_list_entry_count(
5202 		    &scsa2usbp->scsa2usb_waitQ[lun])) {
5203 
5204 			return (USB_FAILURE);
5205 		}
5206 	}
5207 
5208 	return (USB_SUCCESS);
5209 }
5210 
5211 
5212 /*
5213  * scsa2usb_disconnect_event_cb:
5214  *	callback for disconnect events
5215  */
5216 static int
5217 scsa2usb_disconnect_event_cb(dev_info_t *dip)
5218 {
5219 	scsa2usb_state_t *scsa2usbp =
5220 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
5221 	dev_info_t	*cdip;
5222 	int		circ, i;
5223 	int		rval = USB_SUCCESS;
5224 
5225 	ASSERT(scsa2usbp != NULL);
5226 
5227 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5228 	    "scsa2usb_disconnect_event_cb: dip = 0x%p", dip);
5229 
5230 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5231 	scsa2usbp->scsa2usb_dev_state = USB_DEV_DISCONNECTED;
5232 
5233 	/*
5234 	 * wait till the work thread is done, carry on regardless
5235 	 * if not.
5236 	 */
5237 	for (i = 0; i < SCSA2USB_DRAIN_TIMEOUT; i++) {
5238 		if ((scsa2usbp->scsa2usb_work_thread_id == NULL) &&
5239 		    (scsa2usbp->scsa2usb_cur_pkt == NULL) &&
5240 		    (scsa2usb_all_waitQs_empty(scsa2usbp) ==
5241 		    USB_SUCCESS)) {
5242 
5243 			break;
5244 		}
5245 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5246 		delay(drv_usectohz(1000000));
5247 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5248 	}
5249 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5250 
5251 	ndi_devi_enter(dip, &circ);
5252 	for (cdip = ddi_get_child(dip); cdip; ) {
5253 		dev_info_t *next = ddi_get_next_sibling(cdip);
5254 
5255 		mutex_enter(&DEVI(cdip)->devi_lock);
5256 		DEVI_SET_DEVICE_REMOVED(cdip);
5257 		mutex_exit(&DEVI(cdip)->devi_lock);
5258 
5259 		cdip = next;
5260 	}
5261 	ndi_devi_exit(dip, circ);
5262 
5263 	if (scsa2usbp->scsa2usb_ugen_hdl) {
5264 		rval = usb_ugen_disconnect_ev_cb(
5265 					scsa2usbp->scsa2usb_ugen_hdl);
5266 	}
5267 
5268 	return (rval);
5269 }
5270 
5271 
5272 /*
5273  * PM support
5274  *
5275  * scsa2usb_create_pm_components:
5276  *	create the pm components required for power management
5277  *	no mutex is need when calling USBA interfaces
5278  */
5279 static void
5280 scsa2usb_create_pm_components(dev_info_t *dip, scsa2usb_state_t *scsa2usbp)
5281 {
5282 	scsa2usb_power_t *pm;
5283 	uint_t		pwr_states;
5284 
5285 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5286 
5287 	USB_DPRINTF_L4(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5288 	    "scsa2usb_create_pm_components: dip = 0x%p, scsa2usbp = 0x%p",
5289 	    dip, scsa2usbp);
5290 
5291 	/*
5292 	 * determine if this device is on the blacklist
5293 	 * or if a conf file entry has disabled PM
5294 	 */
5295 	if ((scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_PM) == 0) {
5296 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5297 		    "device cannot be power managed");
5298 
5299 		return;
5300 	}
5301 
5302 	/* Allocate the PM state structure */
5303 	pm = kmem_zalloc(sizeof (scsa2usb_power_t), KM_SLEEP);
5304 
5305 	scsa2usbp->scsa2usb_pm = pm;
5306 	pm->scsa2usb_current_power = USB_DEV_OS_FULL_PWR;
5307 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5308 
5309 	if (usb_create_pm_components(dip, &pwr_states) ==
5310 	    USB_SUCCESS) {
5311 		if (usb_handle_remote_wakeup(dip,
5312 		    USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) {
5313 			pm->scsa2usb_wakeup_enabled = 1;
5314 		}
5315 
5316 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5317 		pm->scsa2usb_pwr_states = (uint8_t)pwr_states;
5318 		scsa2usb_raise_power(scsa2usbp);
5319 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5320 	}
5321 
5322 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5323 }
5324 
5325 
5326 /*
5327  * scsa2usb_raise_power:
5328  *	check if the device is using full power or not
5329  */
5330 static void
5331 scsa2usb_raise_power(scsa2usb_state_t *scsa2usbp)
5332 {
5333 	USB_DPRINTF_L4(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5334 	    "scsa2usb_raise_power:");
5335 
5336 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5337 
5338 	if (scsa2usbp->scsa2usb_pm) {
5339 		scsa2usb_pm_busy_component(scsa2usbp);
5340 		if (scsa2usbp->scsa2usb_pm->scsa2usb_current_power !=
5341 		    USB_DEV_OS_FULL_PWR) {
5342 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
5343 			(void) pm_raise_power(scsa2usbp->scsa2usb_dip,
5344 			    0, USB_DEV_OS_FULL_PWR);
5345 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
5346 		}
5347 	}
5348 }
5349 
5350 
5351 /*
5352  * functions to handle power transition for OS levels 0 -> 3
5353  */
5354 static int
5355 scsa2usb_pwrlvl0(scsa2usb_state_t *scsa2usbp)
5356 {
5357 	int	rval;
5358 
5359 	switch (scsa2usbp->scsa2usb_dev_state) {
5360 	case USB_DEV_ONLINE:
5361 		/* Deny the powerdown request if the device is busy */
5362 		if (scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy != 0) {
5363 
5364 			return (USB_FAILURE);
5365 		}
5366 
5367 		/*
5368 		 * stop polling on interrupt pipe
5369 		 */
5370 		scsa2usb_cbi_stop_intr_polling(scsa2usbp);
5371 
5372 		/* Issue USB D3 command to the device here */
5373 		rval = usb_set_device_pwrlvl3(scsa2usbp->scsa2usb_dip);
5374 		ASSERT(rval == USB_SUCCESS);
5375 
5376 		scsa2usbp->scsa2usb_dev_state = USB_DEV_PWRED_DOWN;
5377 
5378 		/* FALLTHRU */
5379 	case USB_DEV_DISCONNECTED:
5380 	case USB_DEV_SUSPENDED:
5381 	case USB_DEV_PWRED_DOWN:
5382 	default:
5383 		scsa2usbp->scsa2usb_pm->scsa2usb_current_power =
5384 						USB_DEV_OS_PWR_OFF;
5385 
5386 		return (USB_SUCCESS);
5387 	}
5388 }
5389 
5390 
5391 static int
5392 scsa2usb_pwrlvl1(scsa2usb_state_t *scsa2usbp)
5393 {
5394 	int	rval;
5395 
5396 	/* Issue USB D2 command to the device here */
5397 	rval = usb_set_device_pwrlvl2(scsa2usbp->scsa2usb_dip);
5398 	ASSERT(rval == USB_SUCCESS);
5399 
5400 	return (DDI_FAILURE);
5401 }
5402 
5403 
5404 static int
5405 scsa2usb_pwrlvl2(scsa2usb_state_t *scsa2usbp)
5406 {
5407 	int	rval;
5408 
5409 	/* Issue USB D1 command to the device here */
5410 	rval = usb_set_device_pwrlvl1(scsa2usbp->scsa2usb_dip);
5411 	ASSERT(rval == USB_SUCCESS);
5412 
5413 	return (DDI_FAILURE);
5414 }
5415 
5416 
5417 static int
5418 scsa2usb_pwrlvl3(scsa2usb_state_t *scsa2usbp)
5419 {
5420 	int	rval;
5421 
5422 	/*
5423 	 * PM framework tries to put us in full power
5424 	 * during system shutdown. If we are disconnected
5425 	 * return success anyways
5426 	 */
5427 	if (scsa2usbp->scsa2usb_dev_state != USB_DEV_DISCONNECTED) {
5428 		/* Issue USB D0 command to the device here */
5429 		rval = usb_set_device_pwrlvl0(scsa2usbp->scsa2usb_dip);
5430 		ASSERT(rval == USB_SUCCESS);
5431 
5432 		scsa2usbp->scsa2usb_dev_state = USB_DEV_ONLINE;
5433 	}
5434 	scsa2usbp->scsa2usb_pm->scsa2usb_current_power = USB_DEV_OS_FULL_PWR;
5435 
5436 	return (DDI_SUCCESS);
5437 }
5438 
5439 
5440 /*
5441  * scsa2usb_power:
5442  *	power entry point
5443  */
5444 /* ARGSUSED */
5445 static int
5446 scsa2usb_power(dev_info_t *dip, int comp, int level)
5447 {
5448 	scsa2usb_state_t	*scsa2usbp;
5449 	scsa2usb_power_t	*pm;
5450 	int			rval = DDI_FAILURE;
5451 
5452 	scsa2usbp = ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
5453 
5454 	USB_DPRINTF_L3(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5455 	    "scsa2usb_power: Begin scsa2usbp (%p): level = %d",
5456 	    scsa2usbp, level);
5457 
5458 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5459 	if (SCSA2USB_BUSY(scsa2usbp)) {
5460 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5461 		    "scsa2usb_power: busy");
5462 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5463 
5464 		return (rval);
5465 	}
5466 
5467 	pm = scsa2usbp->scsa2usb_pm;
5468 	if (pm == NULL) {
5469 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5470 		    "scsa2usb_power: pm NULL");
5471 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5472 
5473 		return (rval);
5474 	}
5475 
5476 	/* check if we are transitioning to a legal power level */
5477 	if (USB_DEV_PWRSTATE_OK(pm->scsa2usb_pwr_states, level)) {
5478 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5479 		    "scsa2usb_power: illegal power level = %d "
5480 		    "pwr_states: %x", level, pm->scsa2usb_pwr_states);
5481 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5482 
5483 		return (rval);
5484 	}
5485 
5486 	switch (level) {
5487 	case USB_DEV_OS_PWR_OFF :
5488 		rval = scsa2usb_pwrlvl0(scsa2usbp);
5489 		break;
5490 	case USB_DEV_OS_PWR_1 :
5491 		rval = scsa2usb_pwrlvl1(scsa2usbp);
5492 		break;
5493 	case USB_DEV_OS_PWR_2 :
5494 		rval = scsa2usb_pwrlvl2(scsa2usbp);
5495 		break;
5496 	case USB_DEV_OS_FULL_PWR :
5497 		rval = scsa2usb_pwrlvl3(scsa2usbp);
5498 		break;
5499 	}
5500 
5501 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5502 
5503 	return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
5504 }
5505 
5506 
5507 static void
5508 scsa2usb_pm_busy_component(scsa2usb_state_t *scsa2usbp)
5509 {
5510 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5511 
5512 	if (scsa2usbp->scsa2usb_pm) {
5513 		scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy++;
5514 
5515 		USB_DPRINTF_L4(DPRINT_MASK_PM,
5516 		    scsa2usbp->scsa2usb_log_handle,
5517 		    "scsa2usb_pm_busy_component: %d",
5518 		    scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy);
5519 
5520 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5521 
5522 		if (pm_busy_component(scsa2usbp->scsa2usb_dip, 0) !=
5523 		    DDI_SUCCESS) {
5524 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
5525 			ASSERT(scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy > 0);
5526 			scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy--;
5527 
5528 			USB_DPRINTF_L2(DPRINT_MASK_PM,
5529 			    scsa2usbp->scsa2usb_log_handle,
5530 			    "scsa2usb_pm_busy_component failed: %d",
5531 			    scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy);
5532 
5533 			return;
5534 		}
5535 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5536 	}
5537 }
5538 
5539 
5540 /*
5541  * scsa2usb_pm_idle_component:
5542  *	idles the device
5543  */
5544 static void
5545 scsa2usb_pm_idle_component(scsa2usb_state_t *scsa2usbp)
5546 {
5547 	ASSERT(!mutex_owned(&scsa2usbp->scsa2usb_mutex));
5548 
5549 	if (scsa2usbp->scsa2usb_pm) {
5550 		if (pm_idle_component(scsa2usbp->scsa2usb_dip, 0) ==
5551 		    DDI_SUCCESS) {
5552 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
5553 			ASSERT(scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy > 0);
5554 			scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy--;
5555 
5556 			USB_DPRINTF_L4(DPRINT_MASK_PM,
5557 			    scsa2usbp->scsa2usb_log_handle,
5558 			    "scsa2usb_pm_idle_component: %d",
5559 			    scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy);
5560 
5561 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
5562 		}
5563 	}
5564 }
5565 
5566 
5567 #ifdef	DEBUG
5568 /*
5569  * scsa2usb_print_cdb:
5570  *	prints CDB
5571  */
5572 void
5573 scsa2usb_print_cdb(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd)
5574 {
5575 	uchar_t *c = (uchar_t *)&cmd->cmd_cdb;
5576 
5577 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5578 	    "cmd = 0x%p opcode=%s "
5579 	    "cdb: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
5580 	    cmd, scsi_cname(cmd->cmd_cdb[SCSA2USB_OPCODE], scsa2usb_cmds),
5581 	    c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8],
5582 	    c[9], c[10], c[11], c[12], c[13], c[14], c[15]);
5583 }
5584 #endif	/* DEBUG */
5585 
5586 
5587 #ifdef	SCSA2USB_BULK_ONLY_TEST
5588 /*
5589  * scsa2usb_test_mblk:
5590  *	This function sends a dummy data mblk_t to simulate
5591  *	the following test cases: 5 and 11.
5592  */
5593 static void
5594 scsa2usb_test_mblk(scsa2usb_state_t *scsa2usbp, boolean_t large)
5595 {
5596 	int			i, rval;
5597 	size_t			len;
5598 	usb_flags_t		flags = USB_FLAGS_SLEEP;
5599 	usb_bulk_req_t		*req;
5600 
5601 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5602 
5603 	/* should we create a larger mblk? */
5604 	len = (large == B_TRUE) ? DEV_BSIZE : USB_BULK_CBWCMD_LEN;
5605 
5606 	req = scsa2usb_init_bulk_req(scsa2usbp, len,
5607 		SCSA2USB_BULK_PIPE_TIMEOUT, 0, flags);
5608 
5609 	/* fill up the data mblk */
5610 	for (i = 0; i < len; i++) {
5611 		*req->bulk_data->b_wptr++ = (uchar_t)i;
5612 	}
5613 
5614 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5615 	ASSERT(req->bulk_timeout);
5616 	rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkout_pipe, req, flags);
5617 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5618 
5619 	USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5620 	    "scsa2usb_test_mblk: Sent Data Out rval = 0x%x", rval);
5621 
5622 	usb_free_bulk_req(req);
5623 }
5624 #endif	/* SCSA2USB_BULK_ONLY_TEST */
5625