xref: /titanic_41/usr/src/uts/common/io/usb/scsa2usb/scsa2usb.c (revision 4bac220845f606f60663ed6f3a2b88caa00ae87e)
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 	_NOTE(COMPETING_THREADS_NOW);
1966 
1967 	return (B_TRUE);
1968 }
1969 
1970 /*
1971  * scsa2usb_cpr_suspend
1972  *	determine if the device's state can be changed to SUSPENDED
1973  *	close pipes if there is no activity
1974  */
1975 /* ARGSUSED */
1976 static int
1977 scsa2usb_cpr_suspend(dev_info_t *dip)
1978 {
1979 	scsa2usb_state_t *scsa2usbp;
1980 	int	prev_state;
1981 	int	rval = USB_FAILURE;
1982 
1983 	scsa2usbp = ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
1984 
1985 	ASSERT(scsa2usbp != NULL);
1986 
1987 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1988 	    "scsa2usb_cpr_suspend:");
1989 
1990 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
1991 	switch (scsa2usbp->scsa2usb_dev_state) {
1992 	case USB_DEV_ONLINE:
1993 	case USB_DEV_PWRED_DOWN:
1994 	case USB_DEV_DISCONNECTED:
1995 		prev_state = scsa2usbp->scsa2usb_dev_state;
1996 		scsa2usbp->scsa2usb_dev_state = USB_DEV_SUSPENDED;
1997 
1998 		/*
1999 		 * If the device is busy, we cannot suspend
2000 		 */
2001 		if (SCSA2USB_BUSY(scsa2usbp)) {
2002 			USB_DPRINTF_L3(DPRINT_MASK_SCSA,
2003 			    scsa2usbp->scsa2usb_log_handle,
2004 			    "scsa2usb_cpr_suspend: I/O active");
2005 
2006 			/* fall back to previous state */
2007 			scsa2usbp->scsa2usb_dev_state = prev_state;
2008 		} else {
2009 			rval = USB_SUCCESS;
2010 		}
2011 
2012 		break;
2013 	case USB_DEV_SUSPENDED:
2014 	default:
2015 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2016 		    "scsa2usb_cpr_suspend: Illegal dev state: %d",
2017 		    scsa2usbp->scsa2usb_dev_state);
2018 
2019 		break;
2020 	}
2021 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2022 
2023 	if ((rval == USB_SUCCESS) && scsa2usbp->scsa2usb_ugen_hdl) {
2024 		rval = usb_ugen_detach(scsa2usbp->scsa2usb_ugen_hdl,
2025 							DDI_SUSPEND);
2026 	}
2027 
2028 	return (rval);
2029 }
2030 
2031 
2032 /*
2033  * scsa2usb_cpr_resume:
2034  *	restore device's state
2035  */
2036 static void
2037 scsa2usb_cpr_resume(dev_info_t *dip)
2038 {
2039 	scsa2usb_state_t *scsa2usbp =
2040 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
2041 
2042 	ASSERT(scsa2usbp != NULL);
2043 
2044 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2045 	    "scsa2usb_cpr_resume: dip = 0x%p", dip);
2046 
2047 	scsa2usb_restore_device_state(dip, scsa2usbp);
2048 
2049 	if (scsa2usbp->scsa2usb_ugen_hdl) {
2050 		(void) usb_ugen_attach(scsa2usbp->scsa2usb_ugen_hdl,
2051 							DDI_RESUME);
2052 	}
2053 }
2054 
2055 
2056 /*
2057  * scsa2usb_restore_device_state:
2058  *	- raise the device's power
2059  *	- reopen all the pipes
2060  */
2061 static void
2062 scsa2usb_restore_device_state(dev_info_t *dip, scsa2usb_state_t *scsa2usbp)
2063 {
2064 	uint_t	prev_state;
2065 
2066 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2067 	    "scsa2usb_restore_device_state:");
2068 
2069 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2070 	prev_state = scsa2usbp->scsa2usb_dev_state;
2071 
2072 	scsa2usb_raise_power(scsa2usbp);
2073 
2074 	ASSERT((prev_state == USB_DEV_DISCONNECTED) ||
2075 	    (prev_state == USB_DEV_SUSPENDED));
2076 
2077 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2078 
2079 	/* Check for the same device */
2080 	if (usb_check_same_device(dip, scsa2usbp->scsa2usb_log_handle,
2081 	    USB_LOG_L0, DPRINT_MASK_ALL, USB_CHK_ALL, NULL) != USB_SUCCESS) {
2082 
2083 		/* change the flags to active */
2084 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2085 		scsa2usbp->scsa2usb_dev_state = USB_DEV_DISCONNECTED;
2086 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2087 
2088 		scsa2usb_pm_idle_component(scsa2usbp);
2089 
2090 		return;
2091 	}
2092 
2093 	/*
2094 	 * if the device had remote wakeup earlier,
2095 	 * enable it again
2096 	 */
2097 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2098 	if (scsa2usbp->scsa2usb_pm &&
2099 	    scsa2usbp->scsa2usb_pm->scsa2usb_wakeup_enabled) {
2100 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2101 		(void) usb_handle_remote_wakeup(scsa2usbp->scsa2usb_dip,
2102 		    USB_REMOTE_WAKEUP_ENABLE);
2103 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2104 	}
2105 
2106 	scsa2usbp->scsa2usb_dev_state = USB_DEV_ONLINE;
2107 	scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_NONE;
2108 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2109 
2110 	scsa2usb_pm_idle_component(scsa2usbp);
2111 }
2112 
2113 
2114 /*
2115  * SCSA entry points:
2116  *
2117  * scsa2usb_scsi_tgt_probe:
2118  * scsa functions are exported by means of the transport table
2119  * Issue a probe to get the inquiry data.
2120  */
2121 /* ARGSUSED */
2122 static int
2123 scsa2usb_scsi_tgt_probe(struct scsi_device *sd, int (*waitfunc)(void))
2124 {
2125 	scsi_hba_tran_t *tran;
2126 	scsa2usb_state_t *scsa2usbp;
2127 	dev_info_t *dip = ddi_get_parent(sd->sd_dev);
2128 	int	rval;
2129 
2130 	ASSERT(dip);
2131 
2132 	tran = ddi_get_driver_private(dip);
2133 	ASSERT(tran != NULL);
2134 	scsa2usbp = (scsa2usb_state_t *)tran->tran_hba_private;
2135 	ASSERT(scsa2usbp);
2136 
2137 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2138 	    "scsa2usb_scsi_tgt_probe:");
2139 
2140 	/* if device is disconnected (ie. pipes closed), fail immediately */
2141 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2142 	if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
2143 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2144 
2145 		return (SCSIPROBE_FAILURE);
2146 	}
2147 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2148 
2149 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2150 	    "scsa2usb_scsi_tgt_probe: scsi_device = 0x%p", sd);
2151 
2152 	if ((rval = scsi_hba_probe(sd, waitfunc)) == SCSIPROBE_EXISTS) {
2153 		/*
2154 		 * respect the removable bit on all USB storage devices
2155 		 * unless overridden by a scsa2usb.conf entry
2156 		 */
2157 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2158 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_RMB)) {
2159 			_NOTE(SCHEME_PROTECTS_DATA("unshared", scsi_inquiry))
2160 			sd->sd_inq->inq_rmb = 1;
2161 		}
2162 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2163 	}
2164 
2165 	return (rval);
2166 }
2167 
2168 
2169 /*
2170  * scsa2usb_scsi_tgt_init:
2171  *	check whether we created this child ourselves
2172  */
2173 /* ARGSUSED */
2174 static int
2175 scsa2usb_scsi_tgt_init(dev_info_t *dip, dev_info_t *cdip,
2176     scsi_hba_tran_t *tran, struct scsi_device *sd)
2177 {
2178 	scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)
2179 					tran->tran_hba_private;
2180 	int lun;
2181 	int t_len = sizeof (lun);
2182 
2183 	if (ddi_prop_op(DDI_DEV_T_ANY, cdip, PROP_LEN_AND_VAL_BUF,
2184 	    DDI_PROP_DONTPASS|DDI_PROP_CANSLEEP, "lun", (caddr_t)&lun,
2185 	    &t_len) != DDI_PROP_SUCCESS) {
2186 
2187 		return (DDI_FAILURE);
2188 	}
2189 
2190 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2191 	    "scsa2usb_scsi_tgt_init: %s, lun%d", ddi_driver_name(cdip), lun);
2192 
2193 	/* is this a child we created? */
2194 	if (scsa2usb_is_usb(cdip) == 0) {
2195 
2196 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2197 		    "scsa2usb_scsi_tgt_init: new child %s%d",
2198 		    ddi_driver_name(cdip), ddi_get_instance(cdip));
2199 
2200 		/*
2201 		 * add property "usb" so we can always verify that it
2202 		 * is our child
2203 		 */
2204 		if (ndi_prop_create_boolean(DDI_DEV_T_NONE, cdip, "usb") !=
2205 		    DDI_PROP_SUCCESS) {
2206 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
2207 			    scsa2usbp->scsa2usb_log_handle,
2208 			    "ndi_prop_create_boolean failed");
2209 
2210 			return (DDI_FAILURE);
2211 		}
2212 
2213 		usba_set_usba_device(cdip,
2214 			usba_get_usba_device(scsa2usbp->scsa2usb_dip));
2215 
2216 		/*
2217 		 * we don't store this dip in scsa2usb_lun_dip, there
2218 		 * might be multiple dips for the same device
2219 		 */
2220 
2221 		return (DDI_SUCCESS);
2222 	}
2223 
2224 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2225 	if ((lun >= scsa2usbp->scsa2usb_n_luns) ||
2226 	    (scsa2usbp->scsa2usb_lun_dip[lun] != NULL)) {
2227 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2228 
2229 		return (DDI_FAILURE);
2230 	}
2231 
2232 	scsa2usbp->scsa2usb_lun_dip[lun] = cdip;
2233 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2234 
2235 	return (DDI_SUCCESS);
2236 }
2237 
2238 
2239 /*
2240  * scsa2usb_scsi_tgt_free:
2241  */
2242 /* ARGSUSED */
2243 static void
2244 scsa2usb_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *cdip,
2245 	scsi_hba_tran_t *tran, struct scsi_device *sd)
2246 {
2247 	scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)
2248 					tran->tran_hba_private;
2249 	int lun;
2250 	int t_len = sizeof (lun);
2251 
2252 	/* is this our child? */
2253 	if (scsa2usb_is_usb(cdip) == 0) {
2254 
2255 		return;
2256 	}
2257 
2258 	if (ddi_prop_op(DDI_DEV_T_ANY, cdip, PROP_LEN_AND_VAL_BUF,
2259 	    DDI_PROP_DONTPASS|DDI_PROP_CANSLEEP, "lun", (caddr_t)&lun,
2260 	    &t_len) != DDI_PROP_SUCCESS) {
2261 
2262 		return;
2263 	}
2264 
2265 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2266 	    "scsa2usb_scsi_tgt_free: %s lun%d", ddi_driver_name(cdip), lun);
2267 
2268 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2269 	if (lun < scsa2usbp->scsa2usb_n_luns) {
2270 		if (scsa2usbp->scsa2usb_lun_dip[lun] == cdip) {
2271 			scsa2usbp->scsa2usb_lun_dip[lun] = NULL;
2272 		}
2273 	}
2274 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2275 }
2276 
2277 
2278 /*
2279  * bus enumeration entry points
2280  */
2281 static int
2282 scsa2usb_scsi_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
2283     void *arg, dev_info_t **child)
2284 {
2285 	int	circ;
2286 	int	rval;
2287 
2288 	scsa2usb_state_t *scsa2usbp =
2289 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
2290 
2291 	ASSERT(scsa2usbp != NULL);
2292 
2293 	USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2294 	    "scsa2usb_scsi_bus_config: op=%d", op);
2295 
2296 	if (scsa2usb_scsi_bus_config_debug) {
2297 		flag |= NDI_DEVI_DEBUG;
2298 	}
2299 
2300 	ndi_devi_enter(dip, &circ);
2301 	/* create children if necessary */
2302 	if (DEVI(dip)->devi_child == NULL) {
2303 		scsa2usb_create_luns(scsa2usbp);
2304 	}
2305 
2306 	rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0);
2307 
2308 	ndi_devi_exit(dip, circ);
2309 
2310 	return (rval);
2311 }
2312 
2313 
2314 static int
2315 scsa2usb_scsi_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
2316     void *arg)
2317 {
2318 	scsa2usb_state_t *scsa2usbp =
2319 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
2320 
2321 	int		circular_count;
2322 	int		rval = NDI_SUCCESS;
2323 	uint_t		save_flag = flag;
2324 
2325 	ASSERT(scsa2usbp != NULL);
2326 
2327 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2328 	    "scsa2usb_scsi_bus_unconfig: op=%d", op);
2329 
2330 	if (scsa2usb_scsi_bus_config_debug) {
2331 		flag |= NDI_DEVI_DEBUG;
2332 	}
2333 
2334 	/*
2335 	 * first offline and if offlining successful, then
2336 	 * remove children
2337 	 */
2338 	if (op == BUS_UNCONFIG_ALL) {
2339 		flag &= ~(NDI_DEVI_REMOVE | NDI_UNCONFIG);
2340 	}
2341 
2342 	ndi_devi_enter(dip, &circular_count);
2343 	rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
2344 
2345 	/*
2346 	 * If unconfig is successful and not part of modunload
2347 	 * daemon, attempt to remove children.
2348 	 */
2349 	if (op == BUS_UNCONFIG_ALL && rval == NDI_SUCCESS &&
2350 	    (flag & NDI_AUTODETACH) == 0) {
2351 		flag |= NDI_DEVI_REMOVE;
2352 		rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
2353 	}
2354 	ndi_devi_exit(dip, circular_count);
2355 
2356 	if ((rval != NDI_SUCCESS) && (op == BUS_UNCONFIG_ALL) &&
2357 	    (save_flag & NDI_DEVI_REMOVE)) {
2358 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2359 		if (scsa2usbp->scsa2usb_warning_given != B_TRUE) {
2360 			USB_DPRINTF_L0(DPRINT_MASK_SCSA,
2361 			    scsa2usbp->scsa2usb_log_handle,
2362 			    "Disconnected device was busy, "
2363 			    "please reconnect.");
2364 			scsa2usbp->scsa2usb_warning_given = B_TRUE;
2365 		}
2366 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2367 	}
2368 
2369 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2370 	    "scsa2usb_scsi_bus_unconfig: rval=%d", rval);
2371 
2372 	return (rval);
2373 }
2374 
2375 
2376 /*
2377  * scsa2usb_scsi_init_pkt:
2378  *	Set up the scsi_pkt for transport. Also initialize
2379  *	scsa2usb_cmd struct for the transport.
2380  *	NOTE: We do not do any DMA setup here as USBA framework
2381  *	does that for us.
2382  */
2383 static struct scsi_pkt *
2384 scsa2usb_scsi_init_pkt(struct scsi_address *ap,
2385     struct scsi_pkt *pkt, struct buf *bp, int cmdlen, int statuslen,
2386     int tgtlen, int flags, int (*callback)(), caddr_t arg)
2387 {
2388 	scsa2usb_cmd_t	 *cmd;
2389 	scsa2usb_state_t *scsa2usbp;
2390 	struct scsi_pkt	 *in_pkt = pkt;
2391 
2392 	ASSERT(callback == NULL_FUNC || callback == SLEEP_FUNC);
2393 
2394 	scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap);
2395 
2396 	/* Print sync message */
2397 	if (ddi_in_panic()) {
2398 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2399 		SCSA2USB_PRINT_SYNC_MSG(scsa2usb_sync_message, scsa2usbp);
2400 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2401 		/* continue so caller will not hang or complain */
2402 	}
2403 
2404 	/* allocate a pkt, if none already allocated */
2405 	if (pkt == NULL) {
2406 		if (statuslen < sizeof (struct scsi_arq_status)) {
2407 			statuslen = sizeof (struct scsi_arq_status);
2408 		}
2409 
2410 		pkt = scsi_hba_pkt_alloc(scsa2usbp->scsa2usb_dip, ap, cmdlen,
2411 			statuslen, tgtlen, sizeof (scsa2usb_cmd_t),
2412 			callback, arg);
2413 		if (pkt == NULL) {
2414 
2415 			return (NULL);
2416 		}
2417 
2418 		cmd = PKT2CMD(pkt);
2419 		cmd->cmd_pkt	= pkt; /* back link to pkt */
2420 		cmd->cmd_scblen	= statuslen;
2421 		cmd->cmd_cdblen	= (uchar_t)cmdlen;
2422 
2423 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2424 		cmd->cmd_tag	= scsa2usbp->scsa2usb_tag++;
2425 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2426 
2427 		cmd->cmd_bp	= bp;
2428 		pkt->pkt_scbp	= (opaque_t)&cmd->cmd_scb;
2429 
2430 		usba_init_list(&cmd->cmd_waitQ, (usb_opaque_t)cmd,
2431 			scsa2usbp->scsa2usb_dev_data->dev_iblock_cookie);
2432 	} else {
2433 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2434 		    "scsa2usb: pkt != NULL");
2435 
2436 		/* nothing to do */
2437 	}
2438 
2439 	if (bp && (bp->b_bcount != 0)) {
2440 		if ((bp_mapin_common(bp, (callback == SLEEP_FUNC) ?
2441 		    VM_SLEEP : VM_NOSLEEP)) == NULL) {
2442 			if (pkt != in_pkt) {
2443 				scsi_hba_pkt_free(ap, pkt);
2444 			}
2445 
2446 			return (NULL);
2447 		}
2448 
2449 		USB_DPRINTF_L3(DPRINT_MASK_SCSA,
2450 		    scsa2usbp->scsa2usb_log_handle,
2451 		    "scsa2usb_scsi_init_pkt: mapped in 0x%p, addr=0x%p",
2452 		    bp, bp->b_un.b_addr);
2453 	}
2454 
2455 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2456 	    "scsa2usb_scsi_init_pkt: ap = 0x%p pkt: 0x%p\n\t"
2457 	    "bp = 0x%p cmdlen = %x stlen = 0x%x tlen = 0x%x flags = 0x%x",
2458 	    ap, pkt, bp, cmdlen, statuslen, tgtlen, flags);
2459 
2460 	return (pkt);
2461 }
2462 
2463 
2464 /*
2465  * scsa2usb_scsi_destroy_pkt:
2466  *	We are done with the packet. Get rid of it.
2467  */
2468 static void
2469 scsa2usb_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2470 {
2471 	scsa2usb_cmd_t *cmd = PKT2CMD(pkt);
2472 	scsa2usb_state_t *scsa2usbp = ADDR2SCSA2USB(ap);
2473 
2474 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2475 	    "scsa2usb_scsi_destroy_pkt: pkt=0x%p", pkt);
2476 
2477 	usba_destroy_list(&cmd->cmd_waitQ);
2478 	scsi_hba_pkt_free(ap, pkt);
2479 }
2480 
2481 
2482 /*
2483  * scsa2usb_scsi_start:
2484  *	For each command being issued, build up the CDB
2485  *	and call scsi_transport to issue the command. This
2486  *	function is based on the assumption that USB allows
2487  *	a subset of SCSI commands. Other SCSI commands we fail.
2488  */
2489 static int
2490 scsa2usb_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2491 {
2492 	scsa2usb_cmd_t		*cmd;
2493 	scsa2usb_state_t	*scsa2usbp = ADDR2SCSA2USB(ap);
2494 	uint_t			lun = ap->a_lun;
2495 
2496 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2497 
2498 	cmd = PKT2CMD(pkt);
2499 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2500 	    "scsa2usb_scsi_start:\n\t"
2501 	    "bp: 0x%p ap: 0x%p pkt: 0x%p flag: 0x%x time: 0x%x\n\tcdb0: 0x%x "
2502 	    "dev_state: 0x%x pkt_state: 0x%x flags: 0x%x pipe_state: 0x%x",
2503 	    cmd->cmd_bp, ap, pkt, pkt->pkt_flags, pkt->pkt_time,
2504 	    pkt->pkt_cdbp[0], scsa2usbp->scsa2usb_dev_state,
2505 	    scsa2usbp->scsa2usb_pkt_state, scsa2usbp->scsa2usb_flags,
2506 	    scsa2usbp->scsa2usb_pipe_state);
2507 
2508 	if (pkt->pkt_time == 0) {
2509 		USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2510 		    "pkt submitted with 0 timeout which may cause indefinite "
2511 		    "hangs");
2512 	}
2513 
2514 	/*
2515 	 * if we are in panic, we are in polled mode, so we can just
2516 	 * accept the request, drop it and return
2517 	 * if we fail this request, the rest of the file systems do not
2518 	 * get synced
2519 	 */
2520 	if (ddi_in_panic()) {
2521 		extern int do_polled_io;
2522 
2523 		ASSERT(do_polled_io);
2524 		scsa2usb_prepare_pkt(scsa2usbp, pkt);
2525 		SCSA2USB_PRINT_SYNC_MSG(scsa2usb_sync_message, scsa2usbp);
2526 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2527 
2528 		return (TRAN_ACCEPT);
2529 	}
2530 
2531 	/* we cannot do polling, this should not happen */
2532 	if (pkt->pkt_flags & FLAG_NOINTR) {
2533 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2534 		    "NOINTR packet: opcode = 0%x", pkt->pkt_cdbp[0]);
2535 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2536 
2537 		return (TRAN_BADPKT);
2538 	}
2539 
2540 	/* is there a ugen open? */
2541 	if (scsa2usbp->scsa2usb_ugen_open_count) {
2542 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2543 		    "ugen access in progress (count=%d)",
2544 		    scsa2usbp->scsa2usb_ugen_open_count);
2545 
2546 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2547 
2548 		return (TRAN_BUSY);
2549 	}
2550 
2551 	/* prepare packet */
2552 	scsa2usb_prepare_pkt(scsa2usbp, pkt);
2553 
2554 	/* just queue up the requests in the waitQ if below max */
2555 	if (usba_list_entry_count(&scsa2usbp->scsa2usb_waitQ[lun]) >
2556 	    SCSA2USB_MAX_REQ_PER_LUN) {
2557 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
2558 		    scsa2usbp->scsa2usb_log_handle,
2559 		    "scsa2usb_scsi_start: limit (%d) exceeded",
2560 		    SCSA2USB_MAX_REQ_PER_LUN);
2561 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2562 
2563 		return (TRAN_BUSY);
2564 	}
2565 
2566 	usba_add_to_list(&scsa2usbp->scsa2usb_waitQ[lun], &cmd->cmd_waitQ);
2567 
2568 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2569 	    "scsa2usb_work_thread_id=0x%x, count=%d, lun=%d",
2570 	    scsa2usbp->scsa2usb_work_thread_id,
2571 	    usba_list_entry_count(&scsa2usbp->scsa2usb_waitQ[lun]), lun);
2572 
2573 	/* fire up a thread to start executing the protocol */
2574 	if (scsa2usbp->scsa2usb_work_thread_id == 0) {
2575 		if ((usb_async_req(scsa2usbp->scsa2usb_dip,
2576 		    scsa2usb_work_thread,
2577 		    (void *)scsa2usbp, USB_FLAGS_SLEEP)) != USB_SUCCESS) {
2578 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
2579 			    scsa2usbp->scsa2usb_log_handle,
2580 			    "no work thread started");
2581 
2582 			if (usba_rm_from_list(
2583 			    &scsa2usbp->scsa2usb_waitQ[lun],
2584 			    &cmd->cmd_waitQ) == USB_SUCCESS) {
2585 				mutex_exit(&scsa2usbp->scsa2usb_mutex);
2586 
2587 				return (TRAN_BUSY);
2588 			} else {
2589 
2590 				mutex_exit(&scsa2usbp->scsa2usb_mutex);
2591 
2592 				return (TRAN_ACCEPT);
2593 			}
2594 		}
2595 		scsa2usbp->scsa2usb_work_thread_id = (kthread_t *)1;
2596 	}
2597 
2598 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2599 
2600 	return (TRAN_ACCEPT);
2601 }
2602 
2603 
2604 /*
2605  * scsa2usb_scsi_abort:
2606  *	Issue SCSI abort command. This function is a NOP.
2607  */
2608 /* ARGSUSED */
2609 static int
2610 scsa2usb_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
2611 {
2612 	scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap);
2613 
2614 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2615 	    "scsa2usb_scsi_abort: pkt = %p", pkt);
2616 
2617 	/* if device is disconnected (ie. pipes closed), fail immediately */
2618 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2619 	if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
2620 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2621 
2622 		return (0);
2623 	}
2624 
2625 	/* flush waitQ if target and lun match */
2626 	if ((ap->a_target == pkt->pkt_address.a_target) &&
2627 	    (ap->a_lun == pkt->pkt_address.a_lun)) {
2628 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2629 		scsa2usb_flush_waitQ(scsa2usbp, ap->a_lun, CMD_ABORTED);
2630 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
2631 	}
2632 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2633 
2634 	return (0);
2635 }
2636 
2637 
2638 /*
2639  * scsa2usb_scsi_reset:
2640  *	device reset may turn the device into a brick and bus reset
2641  *	is not applicable.
2642  *	just flush the waitQ
2643  *	We return success, always.
2644  */
2645 /* ARGSUSED */
2646 static int
2647 scsa2usb_scsi_reset(struct scsi_address *ap, int level)
2648 {
2649 	scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap);
2650 
2651 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2652 	    "scsa2usb_scsi_reset: ap = 0x%p, level = %d", ap, level);
2653 
2654 	/* flush waitQ */
2655 	scsa2usb_flush_waitQ(scsa2usbp, ap->a_lun, CMD_RESET);
2656 
2657 	return (1);
2658 }
2659 
2660 
2661 /*
2662  * scsa2usb_scsi_getcap:
2663  *	Get SCSI capabilities.
2664  */
2665 /* ARGSUSED */
2666 static int
2667 scsa2usb_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2668 {
2669 	int rval = -1;
2670 	uint_t cidx;
2671 	size_t dev_bsize_cap;
2672 	scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap);
2673 	ASSERT(scsa2usbp);
2674 
2675 	if (cap == NULL) {
2676 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2677 		    "scsa2usb_scsi_getcap: invalid arg, "
2678 		    "cap = 0x%p whom = %d", cap, whom);
2679 
2680 		return (rval);
2681 	}
2682 
2683 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2684 	    "scsa2usb_scsi_getcap: cap = %s", cap);
2685 
2686 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2687 
2688 	/* if device is disconnected (ie. pipes closed), fail immediately */
2689 	if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
2690 
2691 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2692 
2693 		return (rval);
2694 	}
2695 
2696 	cidx =	scsi_hba_lookup_capstr(cap);
2697 	switch (cidx) {
2698 	case SCSI_CAP_GEOMETRY:
2699 		dev_bsize_cap = scsa2usbp->scsa2usb_totalsec[ap->a_lun];
2700 
2701 		if (scsa2usbp->scsa2usb_secsz[ap->a_lun] > DEV_BSIZE) {
2702 			dev_bsize_cap *=
2703 			    scsa2usbp->scsa2usb_secsz[ap->a_lun] / DEV_BSIZE;
2704 		} else if (scsa2usbp->scsa2usb_secsz[ap->a_lun] <
2705 		    DEV_BSIZE) {
2706 			dev_bsize_cap /=
2707 			    DEV_BSIZE / scsa2usbp->scsa2usb_secsz[ap->a_lun];
2708 		}
2709 
2710 		if (dev_bsize_cap < 65536 * 2 * 18) {		/* < ~1GB */
2711 			/* unlabeled floppy, 18k per cylinder */
2712 			rval = ((2 << 16) | 18);
2713 		} else if (dev_bsize_cap < 65536 * 64 * 32) {	/* < 64GB */
2714 			/* 1024k per cylinder */
2715 			rval = ((64 << 16) | 32);
2716 		} else if (dev_bsize_cap < 65536 * 255 * 63) {	/* < ~500GB */
2717 			/* ~8m per cylinder */
2718 			rval = ((255 << 16) | 63);
2719 		} else {					/* .. 8TB */
2720 			/* 64m per cylinder */
2721 			rval = ((512 << 16) | 256);
2722 		}
2723 		break;
2724 
2725 	case SCSI_CAP_DMA_MAX:
2726 		rval = scsa2usbp->scsa2usb_max_bulk_xfer_size;
2727 		break;
2728 	case SCSI_CAP_SCSI_VERSION:
2729 		rval = SCSI_VERSION_2;
2730 		break;
2731 	case SCSI_CAP_INTERCONNECT_TYPE:
2732 		rval = INTERCONNECT_USB;
2733 		break;
2734 	case SCSI_CAP_ARQ:
2735 		/* FALLTHRU */
2736 	case SCSI_CAP_UNTAGGED_QING:
2737 		rval = 1;
2738 		break;
2739 	default:
2740 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2741 		    "scsa2usb_scsi_getcap: unsupported cap = %s", cap);
2742 		break;
2743 	}
2744 
2745 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2746 	    "scsa2usb_scsi_getcap: cap = %s, returned = %d", cap, rval);
2747 
2748 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2749 
2750 	return (rval);
2751 }
2752 
2753 
2754 /*
2755  * scsa2usb_scsi_setcap:
2756  *	Set SCSI capabilities.
2757  */
2758 /* ARGSUSED */
2759 static int
2760 scsa2usb_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2761 {
2762 	int rval = -1; /* default is cap undefined */
2763 	uint_t cidx;
2764 	scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap);
2765 	ASSERT(scsa2usbp);
2766 
2767 	if (cap == NULL || whom == 0) {
2768 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2769 		    "scsa2usb_scsi_setcap: invalid arg");
2770 
2771 		return (rval);
2772 	}
2773 
2774 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
2775 	/* if device is disconnected (ie. pipes closed), fail immediately */
2776 	if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
2777 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
2778 
2779 		return (rval);
2780 	}
2781 
2782 	cidx =	scsi_hba_lookup_capstr(cap);
2783 	USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2784 	    "scsa2usb_scsi_setcap: ap = 0x%p value = 0x%x whom = 0x%x "
2785 	    "cidx = 0x%x", ap, value, whom, cidx);
2786 
2787 	switch (cidx) {
2788 	case SCSI_CAP_SECTOR_SIZE:
2789 		if (value) {
2790 			scsa2usbp->scsa2usb_secsz[ap->a_lun] = value;
2791 		}
2792 		break;
2793 	case SCSI_CAP_TOTAL_SECTORS:
2794 		if (value) {
2795 			scsa2usbp->scsa2usb_totalsec[ap->a_lun] = value;
2796 		}
2797 		break;
2798 	case SCSI_CAP_ARQ:
2799 		rval = 1;
2800 		break;
2801 	case SCSI_CAP_DMA_MAX:
2802 	case SCSI_CAP_SCSI_VERSION:
2803 	case SCSI_CAP_INTERCONNECT_TYPE:
2804 	case SCSI_CAP_UNTAGGED_QING:
2805 		/* supported but not settable */
2806 		rval = 0;
2807 		break;
2808 	default:
2809 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2810 		    "scsa2usb_scsi_setcap: unsupported cap = %s", cap);
2811 		break;
2812 	}
2813 
2814 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
2815 
2816 	return (rval);
2817 }
2818 
2819 
2820 /*
2821  * scsa2usb - cmd and transport stuff
2822  */
2823 /*
2824  * scsa2usb_prepare_pkt:
2825  *	initialize some fields of the pkt and cmd
2826  *	(the pkt may have been resubmitted/retried)
2827  */
2828 static void
2829 scsa2usb_prepare_pkt(scsa2usb_state_t *scsa2usbp, struct scsi_pkt *pkt)
2830 {
2831 	scsa2usb_cmd_t	*cmd = PKT2CMD(pkt);
2832 
2833 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2834 	    "scsa2usb_prepare_pkt: pkt=0x%p cdb: 0x%x (%s)",
2835 	    pkt, pkt->pkt_cdbp[0],
2836 	    scsi_cname(pkt->pkt_cdbp[0], scsa2usb_cmds));
2837 
2838 	pkt->pkt_reason = CMD_CMPLT;	/* Set reason to pkt_complete */
2839 	pkt->pkt_state = 0;		/* Reset next three fields */
2840 	pkt->pkt_statistics = 0;
2841 	pkt->pkt_resid = 0;
2842 	bzero(pkt->pkt_scbp, cmd->cmd_scblen); /* Set status to good */
2843 
2844 	if (cmd) {
2845 		cmd->cmd_timeout = pkt->pkt_time;
2846 		cmd->cmd_xfercount = 0;		/* Reset the fields */
2847 		cmd->cmd_total_xfercount = 0;
2848 		cmd->cmd_lba = 0;
2849 		cmd->cmd_done = 0;
2850 		cmd->cmd_dir = 0;
2851 		cmd->cmd_offset = 0;
2852 		cmd->cmd_actual_len = cmd->cmd_cdblen;
2853 	}
2854 }
2855 
2856 
2857 /*
2858  * scsa2usb_force_invalid_request
2859  */
2860 static void
2861 scsa2usb_force_invalid_request(scsa2usb_state_t *scsa2usbp,
2862     scsa2usb_cmd_t *cmd)
2863 {
2864 	struct scsi_arq_status	*arqp;
2865 
2866 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2867 	    "scsa2usb_force_invalid_request: pkt = 0x%p", cmd->cmd_pkt);
2868 
2869 	if (cmd->cmd_scblen >= sizeof (struct scsi_arq_status)) {
2870 		arqp = (struct scsi_arq_status *)cmd->cmd_pkt->pkt_scbp;
2871 		bzero(arqp, cmd->cmd_scblen);
2872 
2873 		arqp->sts_status.sts_chk = 1;
2874 		arqp->sts_rqpkt_reason = CMD_CMPLT;
2875 		arqp->sts_rqpkt_state = STATE_XFERRED_DATA |
2876 		    STATE_GOT_BUS | STATE_GOT_STATUS;
2877 		arqp->sts_sensedata.es_valid = 1;
2878 		arqp->sts_sensedata.es_class = 7;
2879 		arqp->sts_sensedata.es_key = KEY_ILLEGAL_REQUEST;
2880 
2881 		cmd->cmd_pkt->pkt_state = STATE_ARQ_DONE |
2882 		    STATE_GOT_BUS | STATE_GOT_BUS | STATE_GOT_BUS |
2883 		    STATE_GOT_STATUS;
2884 #ifdef DEBUG
2885 		{
2886 			uchar_t *p = (uchar_t *)(&arqp->sts_sensedata);
2887 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
2888 			    scsa2usbp->scsa2usb_log_handle,
2889 			    "cdb: %x rqsense: "
2890 			    "%x %x %x %x %x %x %x %x %x %x "
2891 			    "%x %x %x %x %x %x %x %x %x %x",
2892 			    cmd->cmd_pkt->pkt_cdbp[0],
2893 			    p[0], p[1], p[2], p[3], p[4],
2894 			    p[5], p[6], p[7], p[8], p[9],
2895 			    p[10], p[11], p[12], p[13], p[14],
2896 			    p[15], p[16], p[17], p[18], p[19]);
2897 		}
2898 #endif
2899 
2900 	}
2901 }
2902 
2903 
2904 /*
2905  * scsa2usb_cmd_transport:
2906  */
2907 static int
2908 scsa2usb_cmd_transport(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd)
2909 {
2910 	int rval, transport;
2911 	struct scsi_pkt *pkt;
2912 
2913 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2914 	    "scsa2usb_cmd_transport: pkt: 0x%p, cur_pkt = 0x%p",
2915 	    cmd->cmd_pkt, scsa2usbp->scsa2usb_cur_pkt);
2916 
2917 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
2918 	ASSERT(scsa2usbp->scsa2usb_cur_pkt == NULL);
2919 
2920 	pkt = scsa2usbp->scsa2usb_cur_pkt = cmd->cmd_pkt;
2921 
2922 	/* check black-listed attrs first */
2923 	if (SCSA2USB_IS_BULK_ONLY(scsa2usbp)) {
2924 		transport = scsa2usb_check_bulkonly_blacklist_attrs(scsa2usbp,
2925 					cmd, pkt->pkt_cdbp[0]);
2926 	} else if (SCSA2USB_IS_CB(scsa2usbp) || SCSA2USB_IS_CBI(scsa2usbp)) {
2927 		transport =  scsa2usb_check_ufi_blacklist_attrs(scsa2usbp,
2928 					pkt->pkt_cdbp[0], cmd);
2929 	}
2930 
2931 	/* just accept the command */
2932 	if (transport == SCSA2USB_JUST_ACCEPT) {
2933 		SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
2934 
2935 		return (TRAN_ACCEPT);
2936 	}
2937 
2938 	/* check command set next */
2939 	if (SCSA2USB_IS_SCSI_CMDSET(scsa2usbp) ||
2940 	    SCSA2USB_IS_ATAPI_CMDSET(scsa2usbp)) {
2941 		transport =
2942 		    scsa2usb_handle_scsi_cmd_sub_class(scsa2usbp, cmd, pkt);
2943 	} else if (SCSA2USB_IS_UFI_CMDSET(scsa2usbp)) {
2944 		transport =
2945 		    scsa2usb_handle_ufi_subclass_cmd(scsa2usbp, cmd, pkt);
2946 	} else {
2947 		transport = SCSA2USB_REJECT;
2948 	}
2949 
2950 	switch (transport) {
2951 	case SCSA2USB_TRANSPORT:
2952 		if (SCSA2USB_IS_BULK_ONLY(scsa2usbp)) {
2953 			rval = scsa2usb_bulk_only_transport(scsa2usbp, cmd);
2954 		} else if (SCSA2USB_IS_CB(scsa2usbp) ||
2955 		    SCSA2USB_IS_CBI(scsa2usbp)) {
2956 			rval = scsa2usb_cbi_transport(scsa2usbp, cmd);
2957 		} else {
2958 			rval = TRAN_FATAL_ERROR;
2959 		}
2960 		break;
2961 	case SCSA2USB_JUST_ACCEPT:
2962 		SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
2963 		rval = TRAN_ACCEPT;
2964 		break;
2965 	default:
2966 		rval = TRAN_FATAL_ERROR;
2967 	}
2968 
2969 	return (rval);
2970 }
2971 
2972 
2973 /*
2974  * scsa2usb_check_bulkonly_blacklist_attrs:
2975  *	validate "scsa2usb_blacklist_attrs" (see scsa2usb.h)
2976  *	if blacklisted attrs match accept the request
2977  *	attributes checked are:-
2978  *		SCSA2USB_ATTRS_START_STOP
2979  */
2980 int
2981 scsa2usb_check_bulkonly_blacklist_attrs(scsa2usb_state_t *scsa2usbp,
2982     scsa2usb_cmd_t *cmd, uchar_t opcode)
2983 {
2984 	struct scsi_inquiry *inq =
2985 	    &scsa2usbp->scsa2usb_lun_inquiry[cmd->cmd_pkt->pkt_address.a_lun];
2986 
2987 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2988 	    "scsa2usb_check_bulkonly_blacklist_attrs: opcode = %s",
2989 	    scsi_cname(opcode, scsa2usb_cmds));
2990 
2991 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
2992 
2993 	/*
2994 	 * decode and convert the packet
2995 	 * for most cmds, we can bcopy the cdb
2996 	 */
2997 	switch (opcode) {
2998 	case SCMD_DOORLOCK:
2999 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_DOORLOCK)) {
3000 
3001 			return (SCSA2USB_JUST_ACCEPT);
3002 
3003 		/*
3004 		 * only lock the door for CD and DVD drives
3005 		 */
3006 		} else if ((inq->inq_dtype == DTYPE_RODIRECT) ||
3007 		    (inq->inq_dtype == DTYPE_OPTICAL)) {
3008 
3009 			if (inq->inq_rmb) {
3010 
3011 				break;
3012 			}
3013 		}
3014 
3015 		return (SCSA2USB_JUST_ACCEPT);
3016 
3017 	case SCMD_START_STOP:
3018 		/*
3019 		 * these devices don't have mechanics that spin the
3020 		 * media up and down. So, it doesn't make much sense
3021 		 * to issue this cmd.
3022 		 *
3023 		 * Furthermore, Hagiwara devices do not handle these
3024 		 * cmds well. just accept this command as success.
3025 		 */
3026 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_START_STOP)) {
3027 
3028 			return (SCSA2USB_JUST_ACCEPT);
3029 
3030 		} else if (cmd->cmd_pkt->pkt_cdbp[4] & LOEJECT) {
3031 			/*
3032 			 * if the device is really a removable then
3033 			 * pass it on to the device, else just accept
3034 			 */
3035 			if (inq->inq_rmb) {
3036 
3037 				break;
3038 			}
3039 
3040 			return (SCSA2USB_JUST_ACCEPT);
3041 
3042 		} else if (!scsa2usbp->scsa2usb_rcvd_not_ready) {
3043 			/*
3044 			 * if we have not received a NOT READY condition,
3045 			 * just accept since some device choke on this too.
3046 			 * we do have to let EJECT get through though
3047 			 */
3048 			return (SCSA2USB_JUST_ACCEPT);
3049 		}
3050 
3051 		break;
3052 	case SCMD_INQUIRY:
3053 		/*
3054 		 * Some devices do not handle the inquiry cmd well
3055 		 * so build an inquiry and accept this command as
3056 		 * success.
3057 		 */
3058 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_INQUIRY)) {
3059 			uchar_t evpd = 0x01;
3060 
3061 			if (cmd->cmd_cdb[1] & evpd) {
3062 
3063 				return (SCSA2USB_REJECT);
3064 			}
3065 			cmd->cmd_pkt->pkt_resid -=
3066 					scsa2usb_fake_inquiry(scsa2usbp, cmd,
3067 					cmd->cmd_pkt->pkt_address.a_lun);
3068 			cmd->cmd_pkt->pkt_state |= STATE_XFERRED_DATA;
3069 
3070 			return (SCSA2USB_JUST_ACCEPT);
3071 		}
3072 		break;
3073 
3074 	/*
3075 	 * Fake accepting the following  Opcodes
3076 	 * (as most drives don't support these)
3077 	 * These are needed by format command.
3078 	 */
3079 	case SCMD_RESERVE:
3080 	case SCMD_RELEASE:
3081 	case SCMD_PERSISTENT_RESERVE_IN:
3082 	case SCMD_PERSISTENT_RESERVE_OUT:
3083 
3084 		return (SCSA2USB_JUST_ACCEPT);
3085 
3086 	case SCMD_MODE_SENSE:
3087 	case SCMD_MODE_SELECT:
3088 	case SCMD_MODE_SENSE_G1:
3089 	case SCMD_MODE_SELECT_G1:
3090 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_MODE_SENSE)) {
3091 			if (cmd->cmd_bp) {
3092 				cmd->cmd_pkt->pkt_resid = cmd->cmd_bp->
3093 								b_bcount;
3094 			}
3095 			scsa2usb_force_invalid_request(scsa2usbp, cmd);
3096 
3097 			return (SCSA2USB_JUST_ACCEPT);
3098 		}
3099 
3100 		break;
3101 	default:
3102 
3103 		break;
3104 	}
3105 
3106 	return (SCSA2USB_TRANSPORT);
3107 }
3108 
3109 
3110 /*
3111  * scsa2usb_handle_scsi_cmd_sub_class:
3112  *	prepare a scsi cmd
3113  *	returns SCSA2USB_TRANSPORT, SCSA2USB_REJECT, SCSA2USB_JUST_ACCEPT
3114  */
3115 int
3116 scsa2usb_handle_scsi_cmd_sub_class(scsa2usb_state_t *scsa2usbp,
3117     scsa2usb_cmd_t *cmd, struct scsi_pkt *pkt)
3118 {
3119 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3120 	    "scsa2usb_handle_scsi_cmd_sub_class: cmd = 0x%p pkt = 0x%p",
3121 	    cmd, pkt);
3122 
3123 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3124 
3125 	bzero(&cmd->cmd_cdb, SCSI_CDB_SIZE);
3126 	cmd->cmd_cdb[SCSA2USB_OPCODE] = pkt->pkt_cdbp[0];   /* Set the opcode */
3127 	cmd->cmd_cdb[SCSA2USB_LUN] = pkt->pkt_cdbp[1];
3128 
3129 	/*
3130 	 * decode and convert the packet
3131 	 * for most cmds, we can bcopy the cdb
3132 	 */
3133 	switch (pkt->pkt_cdbp[0]) {
3134 	case SCMD_FORMAT:
3135 		/*
3136 		 * SCMD_FORMAT used to limit cmd->cmd_xfercount
3137 		 * to 4 bytes, but this hangs
3138 		 * formatting dvd media using cdrecord (that is,
3139 		 * a SCSI FORMAT UNIT command with a parameter list > 4 bytes)
3140 		 * (bit 4 in cdb1 is the Fmtdata bit)
3141 		 */
3142 		if ((pkt->pkt_cdbp[1] & 0x10) && cmd->cmd_bp) {
3143 			cmd->cmd_xfercount = cmd->cmd_bp->b_bcount;
3144 		} else {
3145 			cmd->cmd_xfercount = 4;
3146 		}
3147 		cmd->cmd_dir = CBW_DIR_OUT;
3148 		cmd->cmd_actual_len = CDB_GROUP0;
3149 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3150 		break;
3151 
3152 	case SCMD_INQUIRY:
3153 		cmd->cmd_dir = CBW_DIR_IN;
3154 		cmd->cmd_actual_len = CDB_GROUP0;
3155 		cmd->cmd_cdb[SCSA2USB_LBA_0] = pkt->pkt_cdbp[2];
3156 		cmd->cmd_cdb[SCSA2USB_LBA_2] = cmd->cmd_xfercount =
3157 		    min(SCSA2USB_MAX_INQ_LEN,
3158 		    cmd->cmd_bp ? cmd->cmd_bp->b_bcount : 0);
3159 		break;
3160 
3161 	case SCMD_READ_CAPACITY:
3162 		cmd->cmd_dir = CBW_DIR_IN;
3163 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3164 		cmd->cmd_xfercount = sizeof (scsa2usb_read_cap_t);
3165 		break;
3166 
3167 	/*
3168 	 * SCMD_READ/SCMD_WRITE are converted to G1 cmds
3169 	 * (as ATAPI devices don't recognize G0 commands)
3170 	 *
3171 	 * SCMD_READ_LONG/SCMD_WRITE_LONG are handled in
3172 	 * scsa2usb_rw_transport() along with other commands.
3173 	 *
3174 	 * USB Host Controllers cannot handle large (read/write)
3175 	 * xfers. We split the large request to chunks of
3176 	 * smaller ones to meet the HCD limitations.
3177 	 */
3178 	case SCMD_READ:
3179 	case SCMD_WRITE:
3180 	case SCMD_READ_G1:
3181 	case SCMD_WRITE_G1:
3182 	case SCMD_READ_G5:
3183 	case SCMD_WRITE_G5:
3184 	case SCMD_READ_LONG:
3185 	case SCMD_WRITE_LONG:
3186 	case SCMD_READ_CD:
3187 		switch (scsa2usbp->
3188 		    scsa2usb_lun_inquiry[pkt->pkt_address.a_lun].
3189 		    inq_dtype & DTYPE_MASK) {
3190 		case DTYPE_DIRECT:
3191 		case DTYPE_RODIRECT:
3192 		case DTYPE_OPTICAL:
3193 			return (scsa2usb_rw_transport(
3194 					scsa2usbp, pkt));
3195 		default:
3196 			bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3197 			if (cmd->cmd_bp) {
3198 				cmd->cmd_dir =
3199 				    (cmd->cmd_bp->b_flags & B_READ) ?
3200 				    CBW_DIR_IN : CBW_DIR_OUT;
3201 				cmd->cmd_xfercount =
3202 				    cmd->cmd_bp->b_bcount;
3203 			}
3204 			break;
3205 		}
3206 		break;
3207 
3208 	case SCMD_REQUEST_SENSE:
3209 		cmd->cmd_dir = CBW_DIR_IN;
3210 		cmd->cmd_xfercount = pkt->pkt_cdbp[4];
3211 		cmd->cmd_cdb[SCSA2USB_LBA_2] = pkt->pkt_cdbp[4];
3212 		cmd->cmd_actual_len = CDB_GROUP0;
3213 		break;
3214 
3215 	case SCMD_DOORLOCK:
3216 	case SCMD_START_STOP:
3217 	case SCMD_TEST_UNIT_READY:
3218 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3219 		break;
3220 
3221 	/*
3222 	 * Needed by zip protocol to reset the device
3223 	 */
3224 	case SCMD_SDIAG:
3225 	case SCMD_REZERO_UNIT:
3226 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3227 		cmd->cmd_actual_len = CDB_GROUP1;
3228 		break;
3229 
3230 	case SCMD_WRITE_VERIFY:
3231 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3232 		cmd->cmd_dir = CBW_DIR_OUT;
3233 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3234 		cmd->cmd_actual_len = CDB_GROUP1;
3235 		break;
3236 
3237 	/*
3238 	 * Next command does not have a SCSI equivalent as
3239 	 * it is vendor specific.
3240 	 * It was listed in the vendor's ATAPI Zip specs.
3241 	 */
3242 	case SCMD_READ_FORMAT_CAP:
3243 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3244 		cmd->cmd_dir = CBW_DIR_IN;
3245 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3246 		cmd->cmd_actual_len = CDB_GROUP1;
3247 		break;
3248 	case IOMEGA_CMD_CARTRIDGE_PROTECT:
3249 		cmd->cmd_dir = CBW_DIR_OUT;
3250 		cmd->cmd_cdb[SCSA2USB_LBA_2] = pkt->pkt_cdbp[4];
3251 		cmd->cmd_cdb[SCSA2USB_LBA_2] &= ~1;	/* Make it even */
3252 		cmd->cmd_cdb[SCSA2USB_LUN] = pkt->pkt_cdbp[1];
3253 		cmd->cmd_actual_len = CDB_GROUP0;
3254 		cmd->cmd_xfercount = pkt->pkt_cdbp[4]; /* Length of password */
3255 		break;
3256 
3257 	/*
3258 	 * Do not convert SCMD_MODE_SENSE/SELECT to G1 cmds because
3259 	 * the mode header is different as well. USB devices don't
3260 	 * support 0x03 & 0x04 mode pages, which are already obsoleted
3261 	 * by SPC-2 specification.
3262 	 */
3263 	case SCMD_MODE_SENSE:
3264 	case SCMD_MODE_SELECT:
3265 		if ((pkt->pkt_cdbp[2] == SD_MODE_SENSE_PAGE3_CODE) ||
3266 		    (pkt->pkt_cdbp[2] == SD_MODE_SENSE_PAGE4_CODE)) {
3267 			if (cmd->cmd_bp) {
3268 				cmd->cmd_pkt->pkt_resid = cmd->cmd_bp->b_bcount;
3269 			}
3270 			scsa2usb_force_invalid_request(scsa2usbp, cmd);
3271 			return (SCSA2USB_JUST_ACCEPT);
3272 		}
3273 		/* FALLTHROUGH */
3274 
3275 	default:
3276 		/*
3277 		 * an unknown command may be a uscsi cmd which we
3278 		 * should let go thru without mapping
3279 		 */
3280 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3281 		if (cmd->cmd_bp) {
3282 			cmd->cmd_dir = (cmd->cmd_bp->b_flags & B_READ) ?
3283 						CBW_DIR_IN : CBW_DIR_OUT;
3284 			cmd->cmd_xfercount = cmd->cmd_bp->b_bcount;
3285 		}
3286 
3287 		break;
3288 	} /* end of switch */
3289 
3290 	USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3291 	    "scsa2usb_handle_scsi_cmd_sub_class: opcode = 0x%x count = 0x%lx",
3292 	    pkt->pkt_cdbp[SCSA2USB_OPCODE], cmd->cmd_xfercount);
3293 
3294 	cmd->cmd_total_xfercount = cmd->cmd_xfercount;
3295 
3296 	return (SCSA2USB_TRANSPORT);
3297 }
3298 
3299 
3300 /*
3301  * scsa2usb_do_tur is performed before READ CAPACITY command is issued.
3302  * It returns media status, 0 for media ready, -1 for media not ready
3303  * or other errors.
3304  */
3305 static int
3306 scsa2usb_do_tur(scsa2usb_state_t *scsa2usbp, struct scsi_address *ap)
3307 {
3308 	struct scsi_pkt		*pkt;
3309 	scsa2usb_cmd_t		*turcmd;
3310 	int			rval = -1;
3311 
3312 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3313 	    "scsa2usb_do_tur:");
3314 
3315 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3316 
3317 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
3318 	if ((pkt = scsi_init_pkt(ap, NULL, NULL, CDB_GROUP0, 1,
3319 	    PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL)) == NULL) {
3320 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
3321 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
3322 		    scsa2usbp->scsa2usb_log_handle,
3323 		    "scsa2usb_do_tur: init pkt failed");
3324 
3325 		return (rval);
3326 	}
3327 
3328 	RQ_MAKECOM_G0(pkt, FLAG_HEAD | FLAG_NODISCON,
3329 	    (char)SCMD_TEST_UNIT_READY, 0, 0);
3330 
3331 	pkt->pkt_comp = NULL;
3332 	pkt->pkt_time = PKT_DEFAULT_TIMEOUT;
3333 	turcmd = PKT2CMD(pkt);
3334 
3335 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
3336 	scsa2usb_prepare_pkt(scsa2usbp, turcmd->cmd_pkt);
3337 
3338 	if (scsa2usb_cmd_transport(scsa2usbp, turcmd) != TRAN_ACCEPT) {
3339 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
3340 		    scsa2usbp->scsa2usb_log_handle,
3341 		    "scsa2usb_do_tur: cmd transport failed, "
3342 		    "pkt_reason=0x%x", turcmd->cmd_pkt->pkt_reason);
3343 	} else if (*(turcmd->cmd_pkt->pkt_scbp) != STATUS_GOOD) {
3344 		/*
3345 		 * Theoretically, the sense data should be retrieved and
3346 		 * sense key be checked when check condition happens. If
3347 		 * the sense key is UNIT ATTENTION, TEST UNIT READY cmd
3348 		 * needs to be sent again to clear the UNIT ATTENTION and
3349 		 * another TUR to be sent to get the real media status.
3350 		 * But the AMI virtual floppy device simply cannot recover
3351 		 * from UNIT ATTENTION by re-sending a TUR cmd, so it
3352 		 * doesn't make any difference whether to check sense key
3353 		 * or not. Just ignore sense key checking here and assume
3354 		 * the device is NOT READY.
3355 		 */
3356 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
3357 		    scsa2usbp->scsa2usb_log_handle,
3358 		    "scsa2usb_do_tur: media not ready");
3359 	} else {
3360 		rval = 0;
3361 	}
3362 
3363 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
3364 	scsi_destroy_pkt(pkt);
3365 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
3366 
3367 	return (rval);
3368 }
3369 
3370 
3371 /*
3372  * scsa2usb_check_ufi_blacklist_attrs:
3373  *	validate "scsa2usb_blacklist_attrs" (see scsa2usb.h)
3374  *	if blacklisted attrs match accept the request
3375  *	attributes checked are:-
3376  *		SCSA2USB_ATTRS_GET_CONF
3377  *		SCSA2USB_ATTRS_GET_PERF
3378  *		SCSA2USB_ATTRS_GET_START_STOP
3379  */
3380 static int
3381 scsa2usb_check_ufi_blacklist_attrs(scsa2usb_state_t *scsa2usbp, uchar_t opcode,
3382     scsa2usb_cmd_t *cmd)
3383 {
3384 	int	rval = SCSA2USB_TRANSPORT;
3385 
3386 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3387 
3388 	switch (opcode) {
3389 	case SCMD_PRIN:
3390 	case SCMD_PROUT:
3391 		rval = SCSA2USB_JUST_ACCEPT;
3392 		break;
3393 	case SCMD_MODE_SENSE:
3394 	case SCMD_MODE_SELECT:
3395 		if (cmd->cmd_bp) {
3396 			cmd->cmd_pkt->pkt_resid = cmd->cmd_bp->b_bcount;
3397 		}
3398 		scsa2usb_force_invalid_request(scsa2usbp, cmd);
3399 		rval = SCSA2USB_JUST_ACCEPT;
3400 		break;
3401 	case SCMD_GET_CONFIGURATION:
3402 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_GET_CONF)) {
3403 			rval = SCSA2USB_JUST_ACCEPT;
3404 		}
3405 		break;
3406 	case SCMD_GET_PERFORMANCE:
3407 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_GET_PERF)) {
3408 			rval = SCSA2USB_JUST_ACCEPT;
3409 		}
3410 		break;
3411 	case SCMD_START_STOP:
3412 		/*
3413 		 * some CB/CBI devices don't have mechanics that spin the
3414 		 * media up and down. So, it doesn't make much sense
3415 		 * to issue this cmd to those devices.
3416 		 */
3417 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_START_STOP)) {
3418 			rval = SCSA2USB_JUST_ACCEPT;
3419 		}
3420 		break;
3421 	case SCMD_READ_CAPACITY:
3422 		/*
3423 		 * Some devices don't support READ CAPACITY command
3424 		 * when media is not ready. Need to check media status
3425 		 * before issuing the cmd to such device.
3426 		 */
3427 		if (!(scsa2usbp->scsa2usb_attrs &
3428 		    SCSA2USB_ATTRS_NO_MEDIA_CHECK)) {
3429 			struct scsi_pkt *pkt = cmd->cmd_pkt;
3430 
3431 			ASSERT(scsa2usbp->scsa2usb_cur_pkt == pkt);
3432 			scsa2usbp->scsa2usb_cur_pkt = NULL;
3433 
3434 			if (scsa2usb_do_tur(scsa2usbp,
3435 			    &pkt->pkt_address) != 0) {
3436 				/* media not ready, force cmd invalid */
3437 				if (cmd->cmd_bp) {
3438 					cmd->cmd_pkt->pkt_resid =
3439 					    cmd->cmd_bp->b_bcount;
3440 				}
3441 				scsa2usb_force_invalid_request(scsa2usbp, cmd);
3442 				rval = SCSA2USB_JUST_ACCEPT;
3443 			}
3444 
3445 			scsa2usbp->scsa2usb_cur_pkt = pkt;
3446 		}
3447 		break;
3448 	default:
3449 		break;
3450 	}
3451 
3452 	return (rval);
3453 }
3454 
3455 
3456 /*
3457  * scsa2usb_handle_ufi_subclass_cmd:
3458  *	prepare a UFI cmd
3459  *	returns SCSA2USB_TRANSPORT, SCSA2USB_REJECT
3460  */
3461 int
3462 scsa2usb_handle_ufi_subclass_cmd(scsa2usb_state_t *scsa2usbp,
3463     scsa2usb_cmd_t *cmd, struct scsi_pkt *pkt)
3464 {
3465 	uchar_t opcode =  pkt->pkt_cdbp[0];
3466 
3467 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3468 	    "scsa2usb_handle_ufi_subclass_cmd: cmd = 0x%p pkt = 0x%p",
3469 	    cmd, pkt);
3470 
3471 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3472 
3473 	bzero(&cmd->cmd_cdb, SCSI_CDB_SIZE);
3474 	cmd->cmd_cdb[SCSA2USB_OPCODE] = opcode;   /* Set the opcode */
3475 	cmd->cmd_cdb[SCSA2USB_LUN] = pkt->pkt_cdbp[1];
3476 
3477 	/*
3478 	 * decode and convert the packet if necessary
3479 	 * for most cmds, we can bcopy the cdb
3480 	 */
3481 	switch (opcode) {
3482 	case SCMD_FORMAT:
3483 		/* if parameter list is specified */
3484 		if (pkt->pkt_cdbp[1] & 0x10) {
3485 			cmd->cmd_xfercount =
3486 				(pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3487 			cmd->cmd_dir = USB_EP_DIR_OUT;
3488 			cmd->cmd_actual_len = CDB_GROUP5;
3489 		}
3490 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3491 		break;
3492 	case SCMD_INQUIRY:
3493 		cmd->cmd_dir = USB_EP_DIR_IN;
3494 		cmd->cmd_actual_len = CDB_GROUP0;
3495 		cmd->cmd_cdb[SCSA2USB_LBA_0] = pkt->pkt_cdbp[2];
3496 		cmd->cmd_cdb[SCSA2USB_LBA_2] = cmd->cmd_xfercount =
3497 		    min(SCSA2USB_MAX_INQ_LEN,
3498 		    cmd->cmd_bp ? cmd->cmd_bp->b_bcount : 0);
3499 		break;
3500 	case SCMD_READ_CAPACITY:
3501 		cmd->cmd_dir = USB_EP_DIR_IN;
3502 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3503 		cmd->cmd_xfercount = sizeof (scsa2usb_read_cap_t);
3504 		break;
3505 	case SCMD_REQUEST_SENSE:
3506 		cmd->cmd_dir = USB_EP_DIR_IN;
3507 		cmd->cmd_xfercount = pkt->pkt_cdbp[4];
3508 		cmd->cmd_cdb[SCSA2USB_LBA_2] = pkt->pkt_cdbp[4];
3509 		cmd->cmd_actual_len = CDB_GROUP0;
3510 		break;
3511 
3512 	/*
3513 	 * do not convert SCMD_MODE_SENSE/SELECT because the
3514 	 * mode header is different as well
3515 	 */
3516 
3517 	/*
3518 	 * see usb_bulkonly.c for comments on the next set of commands
3519 	 */
3520 	case SCMD_READ:
3521 	case SCMD_WRITE:
3522 	case SCMD_READ_G1:
3523 	case SCMD_WRITE_G1:
3524 	case SCMD_READ_G5:
3525 	case SCMD_WRITE_G5:
3526 	case SCMD_READ_LONG:
3527 	case SCMD_WRITE_LONG:
3528 	case SCMD_READ_CD:
3529 
3530 		return (scsa2usb_rw_transport(scsa2usbp, pkt));
3531 
3532 	case SCMD_TEST_UNIT_READY:
3533 		/*
3534 		 * Some CB/CBI devices may not support TUR.
3535 		 */
3536 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3537 		break;
3538 	case SCMD_READ_FORMAT_CAP:
3539 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3540 		cmd->cmd_dir = USB_EP_DIR_IN;
3541 		cmd->cmd_actual_len = CDB_GROUP1;
3542 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3543 		break;
3544 	case SCMD_WRITE_VERIFY:
3545 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3546 		cmd->cmd_dir = USB_EP_DIR_OUT;
3547 		cmd->cmd_actual_len = CDB_GROUP1;
3548 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3549 		break;
3550 	case SCMD_START_STOP:
3551 		/* A larger timeout is needed for 'flaky' CD-RW devices */
3552 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_BIG_TIMEOUT)) {
3553 			cmd->cmd_timeout = max(cmd->cmd_timeout,
3554 					20 * SCSA2USB_BULK_PIPE_TIMEOUT);
3555 		}
3556 		/* FALLTHRU */
3557 	default:
3558 		/*
3559 		 * all other commands don't need special mapping
3560 		 */
3561 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3562 		if (cmd->cmd_bp) {
3563 			cmd->cmd_dir = (cmd->cmd_bp->b_flags & B_READ) ?
3564 						CBW_DIR_IN : CBW_DIR_OUT;
3565 			cmd->cmd_xfercount = cmd->cmd_bp->b_bcount;
3566 		}
3567 		break;
3568 
3569 	} /* end of switch */
3570 
3571 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3572 	    "scsa2usb_handle_ufi_subclass_cmd: opcode = 0x%x count = 0x%lx",
3573 	    opcode, cmd->cmd_xfercount);
3574 
3575 	cmd->cmd_total_xfercount = cmd->cmd_xfercount;
3576 
3577 	return (SCSA2USB_TRANSPORT);
3578 }
3579 
3580 
3581 /*
3582  * scsa2usb_rw_transport:
3583  *	Handle splitting READ and WRITE requests to the
3584  *	device to a size that the host controller allows.
3585  *
3586  *	returns TRAN_* values and not USB_SUCCESS/FAILURE
3587  *
3588  * To support CD-R/CD-RW/DVD media, we need to support a
3589  * variety of block sizes for the different types of CD
3590  * data (audio, data, video, CD-XA, yellowbook, redbook etc.)
3591  *
3592  * Some of the block sizes used are:- 512, 1k, 2k, 2056, 2336
3593  * 2340, 2352, 2368, 2448, 2646, 2647 etc.
3594  *
3595  * NOTE: the driver could be entertaining a SCSI CDB that uses
3596  * any of the above listed block sizes at a given time, and a
3597  * totally different block size at any other given time for a
3598  * different CDB.
3599  *
3600  * We need to compute block size every time and figure out
3601  * matching LBA and LEN accordingly.
3602  *
3603  * Also UHCI has a limitation that it can only xfer 32k at a
3604  * given time. So, with "odd" sized blocks and a limitation of
3605  * how much we can xfer per shot, we need to compute xfer_count
3606  * as well each time.
3607  *
3608  * The same computation is also done in the function
3609  * scsa2usb_setup_next_xfer().	To save computing block_size in
3610  * this function, I am saving block_size in "cmd" now.
3611  */
3612 int
3613 scsa2usb_rw_transport(scsa2usb_state_t *scsa2usbp, struct scsi_pkt *pkt)
3614 {
3615 	scsa2usb_cmd_t *cmd = PKT2CMD(pkt);
3616 	int lba, dir, opcode;
3617 	struct buf *bp = cmd->cmd_bp;
3618 	size_t len, xfer_count;
3619 	size_t blk_size;	/* calculate the block size to be used */
3620 	int sz;
3621 
3622 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3623 	    "scsa2usb_rw_transport:");
3624 
3625 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3626 
3627 	opcode = pkt->pkt_cdbp[0];
3628 	blk_size  = scsa2usbp->scsa2usb_lbasize[pkt->pkt_address.a_lun];
3629 						/* set to default */
3630 
3631 	switch (opcode) {
3632 	case SCMD_READ:
3633 		/*
3634 		 * Note that READ/WRITE(6) are not supported by the drive.
3635 		 * convert it into a 10 byte read/write.
3636 		 */
3637 		lba = SCSA2USB_LBA_6BYTE(pkt);
3638 		len = SCSA2USB_LEN_6BYTE(pkt);
3639 		opcode = SCMD_READ_G1;	/* Overwrite it w/ byte 10 cmd val */
3640 		dir = USB_EP_DIR_IN;
3641 		break;
3642 	case SCMD_WRITE:
3643 		lba = SCSA2USB_LBA_6BYTE(pkt);
3644 		len = SCSA2USB_LEN_6BYTE(pkt);
3645 		opcode = SCMD_WRITE_G1;	/* Overwrite it w/ byte 10 cmd val */
3646 		dir = USB_EP_DIR_OUT;
3647 		break;
3648 	case SCMD_READ_G1:
3649 	case SCMD_READ_LONG:
3650 		lba = SCSA2USB_LBA_10BYTE(pkt);
3651 		len = SCSA2USB_LEN_10BYTE(pkt);
3652 		dir = USB_EP_DIR_IN;
3653 		break;
3654 	case SCMD_WRITE_G1:
3655 	case SCMD_WRITE_LONG:
3656 		lba = SCSA2USB_LBA_10BYTE(pkt);
3657 		len = SCSA2USB_LEN_10BYTE(pkt);
3658 		dir = USB_EP_DIR_OUT;
3659 		if (len) {
3660 			sz = SCSA2USB_CDRW_BLKSZ(bp ? bp->b_bcount : 0, len);
3661 			if (SCSA2USB_VALID_CDRW_BLKSZ(sz)) {
3662 				blk_size = sz;	/* change it accordingly */
3663 			}
3664 		}
3665 		break;
3666 	case SCMD_READ_CD:
3667 		lba = SCSA2USB_LBA_10BYTE(pkt);
3668 		len = SCSA2USB_LEN_READ_CD(pkt);
3669 		dir = USB_EP_DIR_IN;
3670 
3671 		/* Figure out the block size */
3672 		blk_size = scsa2usb_read_cd_blk_size(pkt->pkt_cdbp[1] >> 2);
3673 		break;
3674 	case SCMD_READ_G5:
3675 		lba = SCSA2USB_LBA_12BYTE(pkt);
3676 		len = SCSA2USB_LEN_12BYTE(pkt);
3677 		dir = USB_EP_DIR_IN;
3678 		break;
3679 	case SCMD_WRITE_G5:
3680 		lba = SCSA2USB_LBA_12BYTE(pkt);
3681 		len = SCSA2USB_LEN_12BYTE(pkt);
3682 		dir = USB_EP_DIR_OUT;
3683 		break;
3684 	}
3685 
3686 	cmd->cmd_total_xfercount = xfer_count = len * blk_size;
3687 
3688 	/* reduce xfer count if necessary */
3689 	if (blk_size &&
3690 	    (xfer_count > scsa2usbp->scsa2usb_max_bulk_xfer_size)) {
3691 		/*
3692 		 * For CD-RW devices reduce the xfer count based
3693 		 * on the block size used by these devices. The
3694 		 * block size could change for READ_CD and WRITE
3695 		 * opcodes.
3696 		 *
3697 		 * Also as UHCI allows a max xfer of 32k at a time;
3698 		 * compute the xfer_count based on the new block_size.
3699 		 *
3700 		 * The len part of the cdb changes as a result of that.
3701 		 */
3702 		if (SCSA2USB_VALID_CDRW_BLKSZ(blk_size)) {
3703 			xfer_count = ((scsa2usbp->scsa2usb_max_bulk_xfer_size/
3704 					blk_size) * blk_size);
3705 			len = xfer_count/blk_size;
3706 			xfer_count = blk_size * len;
3707 		} else {
3708 			xfer_count = scsa2usbp->scsa2usb_max_bulk_xfer_size;
3709 			len = xfer_count/blk_size;
3710 		}
3711 	}
3712 
3713 	cmd->cmd_xfercount = xfer_count;
3714 	cmd->cmd_dir = (uchar_t)dir;
3715 	cmd->cmd_blksize = blk_size;
3716 
3717 	/*
3718 	 * Having figure out the 'partial' xfer len based on he
3719 	 * block size; fill it in to the cmd->cmd_cdb
3720 	 */
3721 	cmd->cmd_cdb[SCSA2USB_OPCODE] = (uchar_t)opcode;
3722 	switch (opcode) {
3723 	case SCMD_READ_CD:
3724 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3725 		scsa2usb_fill_up_ReadCD_cdb_len(cmd, len, CDB_GROUP5);
3726 		break;
3727 	case SCMD_WRITE_G5:
3728 	case SCMD_READ_G5:
3729 		scsa2usb_fill_up_12byte_cdb_len(cmd, len, CDB_GROUP5);
3730 		break;
3731 	default:
3732 		scsa2usb_fill_up_cdb_len(cmd, len);
3733 		cmd->cmd_actual_len = CDB_GROUP1;
3734 		break;
3735 	}
3736 
3737 	scsa2usb_fill_up_cdb_lba(cmd, lba);
3738 
3739 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3740 	    "bcount=0x%lx lba=0x%x len=0x%lx xfercount=0x%lx total=0x%lx",
3741 	    bp ? bp->b_bcount : 0, lba, len, cmd->cmd_xfercount,
3742 	    cmd->cmd_total_xfercount);
3743 
3744 	/* Set the timeout value as per command request */
3745 	if ((opcode == SCMD_WRITE_G1) && SCSA2USB_VALID_CDRW_BLKSZ(blk_size)) {
3746 		/*
3747 		 * We increase the time as CD-RW writes have two things
3748 		 * to do. After writing out the data to the media, a
3749 		 * TOC needs to be filled up at the beginning of the media
3750 		 * This is when the write gets "finalized".
3751 		 * Hence the actual write could take longer than the
3752 		 * value specified in cmd->cmd_timeout.
3753 		 */
3754 		cmd->cmd_timeout *= 4;
3755 
3756 		USB_DPRINTF_L4(DPRINT_MASK_SCSA,
3757 		    scsa2usbp->scsa2usb_log_handle,
3758 		    "new timeout value = 0x%x", cmd->cmd_timeout);
3759 	}
3760 
3761 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3762 	    "lba 0x%x len 0x%lx xfercount 0x%lx total 0x%lx",
3763 	    lba, len, cmd->cmd_xfercount, cmd->cmd_total_xfercount);
3764 
3765 	return (SCSA2USB_TRANSPORT);
3766 }
3767 
3768 
3769 /*
3770  * scsa2usb_setup_next_xfer:
3771  *	For READs and WRITEs we split up the transfer in terms of
3772  *	HCD understood units. This function handles the split transfers.
3773  *
3774  * See comments in the previous function scsa2usb_rw_transport
3775  *
3776  * The lba computation was being done based on scsa2usb_max_bulk_xfer_size
3777  * earlier. With CD-RW devices, the xfer_count and the block_size may
3778  * no longer be a multiple of scsa2usb_max_bulk_xfer_size. So compute
3779  * xfer_count all over again. Adjust lba, based on the previous requests'
3780  * len. Find out the len and add it to cmd->cmd_lba to get the new lba
3781  */
3782 void
3783 scsa2usb_setup_next_xfer(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd)
3784 {
3785 	int xfer_len = min(scsa2usbp->scsa2usb_max_bulk_xfer_size,
3786 			cmd->cmd_total_xfercount);
3787 	int cdb_len;
3788 	size_t blk_size;
3789 
3790 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3791 
3792 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3793 	    "scsa2usb_setup_next_xfer: opcode = 0x%x lba = 0x%x "
3794 	    "total count = 0x%lx", cmd->cmd_cdb[SCSA2USB_OPCODE],
3795 	    cmd->cmd_lba, cmd->cmd_total_xfercount);
3796 
3797 	ASSERT(cmd->cmd_total_xfercount > 0);
3798 	cmd->cmd_xfercount = xfer_len;
3799 	blk_size = scsa2usbp->scsa2usb_lbasize[
3800 				cmd->cmd_pkt->pkt_address.a_lun];
3801 
3802 	/*
3803 	 * For CD-RW devices reduce the xfer count based on the
3804 	 * block_size used by these devices. See changes below
3805 	 * where xfer_count is being adjusted.
3806 	 *
3807 	 * Also adjust len/lba based on the block_size and xfer_count.
3808 	 * NOTE: Always calculate lba first, as it based on previous
3809 	 * commands' values.
3810 	 */
3811 	switch (cmd->cmd_cdb[SCSA2USB_OPCODE]) {
3812 	case SCMD_READ_CD:
3813 		/* calculate lba = current_lba + len_of_prev_cmd */
3814 		cmd->cmd_lba += (cmd->cmd_cdb[6] << 16) +
3815 		    (cmd->cmd_cdb[7] << 8) + cmd->cmd_cdb[8];
3816 		cdb_len = xfer_len/cmd->cmd_blksize;
3817 		cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_2] = (uchar_t)cdb_len;
3818 		/* re-adjust xfer count */
3819 		cmd->cmd_xfercount = cdb_len * cmd->cmd_blksize;
3820 		break;
3821 	case SCMD_WRITE_G5:
3822 	case SCMD_READ_G5:
3823 		/* calculate lba = current_lba + len_of_prev_cmd */
3824 		cmd->cmd_lba += (cmd->cmd_cdb[6] << 24) +
3825 		    (cmd->cmd_cdb[7] << 16) + (cmd->cmd_cdb[8] << 8) +
3826 		    cmd->cmd_cdb[9];
3827 		if (blk_size) {
3828 			xfer_len /= blk_size;
3829 		}
3830 		scsa2usb_fill_up_12byte_cdb_len(cmd, xfer_len, CDB_GROUP5);
3831 		break;
3832 	case SCMD_WRITE_G1:
3833 	case SCMD_WRITE_LONG:
3834 		/* calculate lba = current_lba + len_of_prev_cmd */
3835 		cmd->cmd_lba += (cmd->cmd_cdb[7] << 8) + cmd->cmd_cdb[8];
3836 		if (SCSA2USB_VALID_CDRW_BLKSZ(cmd->cmd_blksize)) {
3837 			blk_size = cmd->cmd_blksize;
3838 		}
3839 		cdb_len = xfer_len/blk_size;
3840 		scsa2usb_fill_up_cdb_len(cmd, cdb_len);
3841 		/* re-adjust xfer count */
3842 		cmd->cmd_xfercount = cdb_len * blk_size;
3843 		break;
3844 	default:
3845 		if (blk_size) {
3846 			xfer_len /= blk_size;
3847 		}
3848 		scsa2usb_fill_up_cdb_len(cmd, xfer_len);
3849 		cmd->cmd_lba += scsa2usbp->scsa2usb_max_bulk_xfer_size/blk_size;
3850 	}
3851 
3852 	/* fill in the lba */
3853 	scsa2usb_fill_up_cdb_lba(cmd, cmd->cmd_lba);
3854 
3855 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3856 	    "scsa2usb_setup_next_xfer:\n\tlba = 0x%x xfer_len = 0x%x "
3857 	    "xfercount = 0x%lx total = 0x%lx", cmd->cmd_lba, xfer_len,
3858 	    cmd->cmd_xfercount, cmd->cmd_total_xfercount);
3859 }
3860 
3861 
3862 /*
3863  * take one request from the lun's waitQ and transport it
3864  */
3865 static void
3866 scsa2usb_transport_request(scsa2usb_state_t *scsa2usbp, uint_t lun)
3867 {
3868 	int			rval;
3869 	struct scsi_pkt		*pkt;
3870 	struct scsa2usb_cmd	*cmd, *arqcmd;
3871 
3872 	if ((cmd = (scsa2usb_cmd_t *)
3873 	    usba_rm_first_pvt_from_list(
3874 	    &scsa2usbp->scsa2usb_waitQ[lun])) == NULL) {
3875 
3876 		return;
3877 	}
3878 	pkt = cmd->cmd_pkt;
3879 
3880 	/*
3881 	 * if device has been disconnected, just complete it
3882 	 */
3883 	if (scsa2usbp->scsa2usb_dev_state == USB_DEV_DISCONNECTED) {
3884 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3885 		    "device not accessible");
3886 		pkt->pkt_reason = CMD_DEV_GONE;
3887 		SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
3888 		scsa2usb_pkt_completion(scsa2usbp, pkt);
3889 
3890 		return;
3891 	}
3892 
3893 	USB_DPRINTF_L4(DPRINT_MASK_SCSA,
3894 	    scsa2usbp->scsa2usb_log_handle,
3895 	    "scsa2usb_transport_request: cmd=0x%p bp=0x%p addr=0x%p",
3896 	    cmd, cmd->cmd_bp,
3897 	    (cmd->cmd_bp ? cmd->cmd_bp->b_un.b_addr : NULL));
3898 
3899 	rval = scsa2usb_cmd_transport(scsa2usbp, cmd);
3900 
3901 	USB_DPRINTF_L3(DPRINT_MASK_SCSA,
3902 	    scsa2usbp->scsa2usb_log_handle,
3903 	    "scsa2usb_transport_request: transport rval = %d",
3904 	    rval);
3905 
3906 	if (scsa2usbp->scsa2usb_cur_pkt == NULL) {
3907 
3908 		return;
3909 	}
3910 
3911 	ASSERT(pkt == scsa2usbp->scsa2usb_cur_pkt);
3912 
3913 	if (ddi_in_panic()) {
3914 		pkt->pkt_reason = CMD_CMPLT;
3915 		scsa2usb_pkt_completion(scsa2usbp, pkt);
3916 
3917 		return;
3918 	}
3919 
3920 	/*
3921 	 * start an auto-request sense iff
3922 	 * there was a check condition, we have enough
3923 	 * space in the status block, and we have not
3924 	 * faked an auto request sense
3925 	 */
3926 	if ((*(pkt->pkt_scbp) == STATUS_CHECK) &&
3927 	    (cmd->cmd_scblen >= sizeof (struct scsi_arq_status)) &&
3928 	    ((pkt->pkt_state & STATE_ARQ_DONE) == 0) &&
3929 	    (scsa2usb_create_arq_pkt(scsa2usbp,
3930 	    &pkt->pkt_address) == USB_SUCCESS)) {
3931 		arqcmd = scsa2usbp->scsa2usb_arq_cmd;
3932 
3933 		/*
3934 		 * copy the timeout from the
3935 		 * original packet
3936 		 * for lack of a better value
3937 		 */
3938 		arqcmd->cmd_pkt->pkt_time = pkt->pkt_time;
3939 		scsa2usb_prepare_pkt(scsa2usbp,
3940 			    arqcmd->cmd_pkt);
3941 
3942 		scsa2usbp->scsa2usb_cur_pkt = NULL;
3943 		if (scsa2usb_cmd_transport(
3944 		    scsa2usbp, arqcmd) == TRAN_ACCEPT) {
3945 
3946 			/* finish w/ this packet */
3947 			scsa2usb_complete_arq_pkt(
3948 			    scsa2usbp, arqcmd->cmd_pkt, cmd,
3949 			    scsa2usbp->scsa2usb_arq_bp);
3950 
3951 			/*
3952 			 * we have valid request sense
3953 			 * data so clear the pkt_reason
3954 			 */
3955 			pkt->pkt_reason = CMD_CMPLT;
3956 		}
3957 		scsa2usbp->scsa2usb_cur_pkt = pkt;
3958 		scsa2usb_delete_arq_pkt(scsa2usbp);
3959 	}
3960 
3961 	if ((rval != TRAN_ACCEPT) &&
3962 	    (pkt->pkt_reason == CMD_CMPLT)) {
3963 		pkt->pkt_reason = CMD_TRAN_ERR;
3964 	}
3965 
3966 	SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
3967 	scsa2usb_pkt_completion(scsa2usbp, pkt);
3968 
3969 	ASSERT(scsa2usbp->scsa2usb_cur_pkt == NULL);
3970 }
3971 
3972 
3973 /*
3974  * scsa2usb_work_thread:
3975  *	The taskq thread that kicks off the transport (BO and CB/CBI)
3976  */
3977 static void
3978 scsa2usb_work_thread(void *arg)
3979 {
3980 	scsa2usb_state_t	*scsa2usbp = (scsa2usb_state_t *)arg;
3981 	uint_t			lun;
3982 	uint_t			count;
3983 
3984 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
3985 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3986 	    "scsa2usb_work_thread start: thread_id=0x%x",
3987 	    scsa2usbp->scsa2usb_work_thread_id);
3988 
3989 	ASSERT(scsa2usbp->scsa2usb_work_thread_id == (kthread_t *)1);
3990 	scsa2usbp->scsa2usb_work_thread_id = curthread;
3991 
3992 	/* exclude ugen accesses */
3993 	while (scsa2usbp->scsa2usb_transport_busy) {
3994 		cv_wait(&scsa2usbp->scsa2usb_transport_busy_cv,
3995 		    &scsa2usbp->scsa2usb_mutex);
3996 	}
3997 	ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0);
3998 	scsa2usbp->scsa2usb_transport_busy++;
3999 	scsa2usbp->scsa2usb_busy_thread = curthread;
4000 
4001 	scsa2usb_raise_power(scsa2usbp);
4002 
4003 	/* reopen the pipes if necessary */
4004 	(void) scsa2usb_open_usb_pipes(scsa2usbp);
4005 
4006 	for (;;) {
4007 		ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0);
4008 		for (lun = 0; lun < scsa2usbp->scsa2usb_n_luns; lun++) {
4009 			scsa2usb_transport_request(scsa2usbp, lun);
4010 		}
4011 		count = 0;
4012 		for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
4013 			count += usba_list_entry_count(
4014 				&scsa2usbp->scsa2usb_waitQ[lun]);
4015 		}
4016 
4017 		if (count == 0) {
4018 
4019 			break;
4020 		}
4021 	}
4022 
4023 	scsa2usbp->scsa2usb_work_thread_id = 0;
4024 
4025 	ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0);
4026 
4027 	scsa2usbp->scsa2usb_transport_busy--;
4028 	scsa2usbp->scsa2usb_busy_thread = NULL;
4029 	cv_signal(&scsa2usbp->scsa2usb_transport_busy_cv);
4030 
4031 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4032 	    "scsa2usb_work_thread: exit");
4033 
4034 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4035 
4036 	scsa2usb_pm_idle_component(scsa2usbp);
4037 }
4038 
4039 
4040 /*
4041  * scsa2usb_flush_waitQ:
4042  *	empties the entire waitQ with errors asap.
4043  *
4044  * It is called from scsa2usb_scsi_reset and scsa2usb_panic_callb.
4045  * If the device is reset; we should empty the waitQ right away.
4046  * If the system has paniced; we should empty the waitQ right away.
4047  *
4048  * CPR suspend will only succeed if device is idle. No need to call
4049  * this function for CPR suspend case.
4050  */
4051 static void
4052 scsa2usb_flush_waitQ(scsa2usb_state_t *scsa2usbp, uint_t lun,
4053     uchar_t error)
4054 {
4055 	struct scsi_pkt		*pkt;
4056 	struct scsa2usb_cmd	*cmd;
4057 	usba_list_entry_t	head;
4058 
4059 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4060 
4061 	usba_move_list(&scsa2usbp->scsa2usb_waitQ[lun], &head,
4062 	    scsa2usbp->scsa2usb_dev_data->dev_iblock_cookie);
4063 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4064 
4065 	while ((cmd = (scsa2usb_cmd_t *)usba_rm_first_pvt_from_list(&head)) !=
4066 	    NULL) {
4067 		pkt = cmd->cmd_pkt;
4068 		pkt->pkt_reason = error;	/* set error */
4069 
4070 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4071 		scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_DO_COMP;
4072 		scsa2usb_pkt_completion(scsa2usbp, pkt);
4073 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4074 	} /* end of while */
4075 }
4076 
4077 
4078 /*
4079  * scsa2usb_do_inquiry is performed before INIT CHILD and we have
4080  * to fake a few things normally done by SCSA
4081  */
4082 static void
4083 scsa2usb_do_inquiry(scsa2usb_state_t *scsa2usbp, uint_t target, uint_t lun)
4084 {
4085 	struct buf	*bp;
4086 	struct scsi_pkt *pkt;
4087 	struct scsi_address ap;
4088 	int		len = SCSA2USB_MAX_INQ_LEN;
4089 
4090 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4091 	    "scsa2usb_do_inquiry: %d bytes", len);
4092 
4093 	/* is it inquiry-challenged? */
4094 	if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_INQUIRY)) {
4095 		(void) scsa2usb_fake_inquiry(scsa2usbp, NULL, lun);
4096 
4097 		return;
4098 	}
4099 
4100 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4101 
4102 	bzero(&ap, sizeof (struct scsi_address));
4103 	ap.a_hba_tran = scsa2usbp->scsa2usb_tran;
4104 	ap.a_target = target;
4105 	ap.a_lun = lun;
4106 
4107 	/* limit inquiry to 36 bytes */
4108 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4109 	if ((bp = scsi_alloc_consistent_buf(&ap, (struct buf *)NULL,
4110 	    len, B_READ, SLEEP_FUNC, NULL)) == NULL) {
4111 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4112 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4113 		    scsa2usbp->scsa2usb_log_handle,
4114 		    "scsa2usb_do_inquiry: failed");
4115 
4116 		return;
4117 	}
4118 
4119 	pkt = scsi_init_pkt(&ap, NULL, bp, CDB_GROUP0, 1,
4120 	    PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL);
4121 
4122 	RQ_MAKECOM_G0(pkt, FLAG_NOINTR, (char)SCMD_INQUIRY, 0, len);
4123 
4124 	pkt->pkt_comp = NULL;
4125 	pkt->pkt_time = 5;
4126 	bzero(bp->b_un.b_addr, len);
4127 
4128 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4129 	    "scsa2usb_do_inquiry:INQUIRY");
4130 
4131 	(void) scsi_transport(pkt);
4132 
4133 	if (pkt->pkt_reason) {
4134 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4135 		    scsa2usbp->scsa2usb_log_handle,
4136 		    "INQUIRY failed, cannot determine device type, "
4137 		    "pkt_reason=0x%x", pkt->pkt_reason);
4138 
4139 		/* not much hope for other cmds, reduce */
4140 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4141 		scsa2usbp->scsa2usb_attrs &=
4142 					~SCSA2USB_ATTRS_REDUCED_CMD;
4143 		(void) scsa2usb_fake_inquiry(scsa2usbp, NULL, lun);
4144 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4145 	}
4146 
4147 	scsi_destroy_pkt(pkt);
4148 	scsi_free_consistent_buf(bp);
4149 
4150 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4151 }
4152 
4153 
4154 /*
4155  * scsa2usb_fake_inquiry:
4156  *    build an inquiry for a given device that doesnt like inquiry
4157  *    commands.
4158  */
4159 static int
4160 scsa2usb_fake_inquiry(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd,
4161     uint_t lun)
4162 {
4163 	usb_client_dev_data_t *dev_data = scsa2usbp->scsa2usb_dev_data;
4164 	struct scsi_inquiry *inqp;
4165 	int len;
4166 
4167 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4168 	    "scsa2usb_fake_inquiry:");
4169 
4170 	if (cmd) {
4171 		inqp = (struct scsi_inquiry *)cmd->cmd_bp->b_un.b_addr;
4172 	} else {
4173 		inqp = &scsa2usbp->scsa2usb_lun_inquiry[lun];
4174 	}
4175 	bzero(inqp, sizeof (struct scsi_inquiry));
4176 	for (len = 0; len < sizeof (inqp->inq_vid); len++) {
4177 		*(inqp->inq_vid + len) = ' ';
4178 	}
4179 
4180 	for (len = 0; len < sizeof (inqp->inq_pid); len++) {
4181 		*(inqp->inq_pid + len) = ' ';
4182 	}
4183 
4184 	inqp->inq_dtype = DTYPE_DIRECT;
4185 	inqp->inq_rmb = 1;
4186 	inqp->inq_ansi = 2;
4187 	inqp->inq_rdf = RDF_SCSI2;
4188 	inqp->inq_len = sizeof (struct scsi_inquiry)-4;
4189 
4190 	/* Fill in the Vendor id/Product id strings */
4191 	if (dev_data->dev_mfg) {
4192 		if ((len = strlen(dev_data->dev_mfg)) >
4193 		    sizeof (inqp->inq_vid)) {
4194 			len = sizeof (inqp->inq_vid);
4195 		}
4196 		bcopy(dev_data->dev_mfg, inqp->inq_vid, len);
4197 	}
4198 
4199 	if (dev_data->dev_product) {
4200 		if ((len = strlen(dev_data->dev_product)) >
4201 		    sizeof (inqp->inq_pid)) {
4202 			len = sizeof (inqp->inq_pid);
4203 		}
4204 		bcopy(dev_data->dev_product, inqp->inq_pid, len);
4205 	}
4206 
4207 	/* Set the Revision to the Device */
4208 	inqp->inq_revision[0] = 0x30 +
4209 		((dev_data->dev_descr->bcdDevice>>12) & 0xF);
4210 	inqp->inq_revision[1] = 0x30 +
4211 		((dev_data->dev_descr->bcdDevice>>8) & 0xF);
4212 	inqp->inq_revision[2] = 0x30 +
4213 		((dev_data->dev_descr->bcdDevice>>4) & 0xF);
4214 	inqp->inq_revision[3] = 0x30 +
4215 		((dev_data->dev_descr->bcdDevice) & 0xF);
4216 
4217 	/* Copy inquiry data in to soft state */
4218 	bcopy(inqp, &scsa2usbp->scsa2usb_lun_inquiry[lun],
4219 	    sizeof (struct scsi_inquiry));
4220 
4221 	return (sizeof (struct scsi_inquiry));
4222 }
4223 
4224 
4225 /*
4226  * scsa2usb_create_arq_pkt:
4227  *	Create and ARQ packet to get request sense data
4228  */
4229 static int
4230 scsa2usb_create_arq_pkt(scsa2usb_state_t *scsa2usbp, struct scsi_address *ap)
4231 {
4232 	struct buf *bp;
4233 	scsa2usb_cmd_t *arq_cmd;
4234 
4235 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4236 	    "scsa2usb_create_arq_pkt: scsa2usbp: %p, ap: %p", scsa2usbp, ap);
4237 
4238 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4239 
4240 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4241 	if ((bp = scsi_alloc_consistent_buf(ap, (struct buf *)NULL,
4242 	    SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL)) == NULL) {
4243 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4244 
4245 		return (USB_FAILURE);
4246 	}
4247 
4248 	arq_cmd = PKT2CMD(scsi_init_pkt(ap, NULL, bp, CDB_GROUP0, 1,
4249 	    PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL));
4250 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4251 
4252 	RQ_MAKECOM_G0(arq_cmd->cmd_pkt,
4253 	    FLAG_SENSING | FLAG_HEAD | FLAG_NODISCON,
4254 	    (char)SCMD_REQUEST_SENSE, 0, (char)SENSE_LENGTH);
4255 
4256 	arq_cmd->cmd_pkt->pkt_ha_private = arq_cmd;
4257 	scsa2usbp->scsa2usb_arq_cmd = arq_cmd;
4258 	scsa2usbp->scsa2usb_arq_bp = bp;
4259 	arq_cmd->cmd_pkt->pkt_comp = NULL;
4260 	bzero(bp->b_un.b_addr, SENSE_LENGTH);
4261 
4262 	return (USB_SUCCESS);
4263 }
4264 
4265 
4266 /*
4267  * scsa2usb_delete_arq_pkt:
4268  *	Destroy the ARQ packet
4269  */
4270 static void
4271 scsa2usb_delete_arq_pkt(scsa2usb_state_t *scsa2usbp)
4272 {
4273 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4274 	    "scsa2usb_delete_arq_pkt: cmd: 0x%p", scsa2usbp->scsa2usb_arq_cmd);
4275 
4276 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4277 
4278 	if (scsa2usbp->scsa2usb_arq_cmd != NULL) {
4279 		scsi_destroy_pkt(scsa2usbp->scsa2usb_arq_cmd->cmd_pkt);
4280 		scsi_free_consistent_buf(scsa2usbp->scsa2usb_arq_bp);
4281 	}
4282 	scsa2usbp->scsa2usb_arq_cmd = NULL;
4283 	scsa2usbp->scsa2usb_arq_bp = NULL;
4284 }
4285 
4286 
4287 /*
4288  * scsa2usb_complete_arq_pkt:
4289  *	finish processing the arq packet
4290  */
4291 static void
4292 scsa2usb_complete_arq_pkt(scsa2usb_state_t *scsa2usbp,
4293     struct scsi_pkt *pkt, scsa2usb_cmd_t *ssp, struct buf *bp)
4294 {
4295 	scsa2usb_cmd_t		*sp = pkt->pkt_ha_private;
4296 	struct scsi_arq_status	*arqp;
4297 
4298 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4299 
4300 	arqp = (struct scsi_arq_status *)(ssp->cmd_pkt->pkt_scbp);
4301 	arqp->sts_rqpkt_status = *((struct scsi_status *)
4302 		(sp->cmd_pkt->pkt_scbp));
4303 	arqp->sts_rqpkt_reason = CMD_CMPLT;
4304 	arqp->sts_rqpkt_state |= STATE_XFERRED_DATA;
4305 	arqp->sts_rqpkt_statistics = arqp->sts_rqpkt_resid = 0;
4306 
4307 	/* is this meaningful sense data */
4308 	if (*(bp->b_un.b_addr) != 0) {
4309 		bcopy(bp->b_un.b_addr, &arqp->sts_sensedata, sp->cmd_scblen);
4310 		ssp->cmd_pkt->pkt_state |= STATE_ARQ_DONE;
4311 	}
4312 
4313 	/* we will not sense start cmd until we receive a NOT READY */
4314 	if (arqp->sts_sensedata.es_key == KEY_NOT_READY) {
4315 		scsa2usbp->scsa2usb_rcvd_not_ready = B_TRUE;
4316 	}
4317 }
4318 
4319 
4320 /*
4321  * Miscellaneous functions for any command/transport
4322  */
4323 /*
4324  * scsa2usb_open_usb_pipes:
4325  *	set up a pipe policy
4326  *	open usb bulk pipes (BO and CB/CBI)
4327  *	open usb interrupt pipe (CBI)
4328  */
4329 static int
4330 scsa2usb_open_usb_pipes(scsa2usb_state_t *scsa2usbp)
4331 {
4332 	int			rval;
4333 	usb_pipe_policy_t	policy;	/* bulk pipe policy */
4334 	size_t			sz;
4335 
4336 	ASSERT(scsa2usbp);
4337 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4338 
4339 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4340 	    "scsa2usb_open_usb_pipes: dip = 0x%p flag = 0x%x",
4341 	    scsa2usbp->scsa2usb_dip, scsa2usbp->scsa2usb_flags);
4342 
4343 	if (!(scsa2usbp->scsa2usb_flags & SCSA2USB_FLAGS_PIPES_OPENED)) {
4344 
4345 		/*
4346 		 * one pipe policy for all bulk pipes
4347 		 */
4348 		bzero(&policy, sizeof (usb_pipe_policy_t));
4349 		/* at least 2, for the normal and exceptional callbacks */
4350 		policy.pp_max_async_reqs = 1;
4351 
4352 		USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4353 		    "scsa2usb_open_usb_pipes: opening bulk pipes");
4354 
4355 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4356 
4357 		/* Open the USB bulk-in pipe */
4358 		if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip,
4359 		    &scsa2usbp->scsa2usb_bulkin_ept, &policy, USB_FLAGS_SLEEP,
4360 		    &scsa2usbp->scsa2usb_bulkin_pipe)) != USB_SUCCESS) {
4361 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4362 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4363 			    scsa2usbp->scsa2usb_log_handle,
4364 			    "scsa2usb_open_usb_pipes: bulk/in pipe open "
4365 			    " failed rval = %d", rval);
4366 
4367 			return (USB_FAILURE);
4368 		}
4369 
4370 		/* Open the bulk-out pipe  using the same policy */
4371 		if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip,
4372 		    &scsa2usbp->scsa2usb_bulkout_ept, &policy, USB_FLAGS_SLEEP,
4373 		    &scsa2usbp->scsa2usb_bulkout_pipe)) != USB_SUCCESS) {
4374 			usb_pipe_close(scsa2usbp->scsa2usb_dip,
4375 			    scsa2usbp->scsa2usb_bulkin_pipe,
4376 			    USB_FLAGS_SLEEP, NULL, NULL);
4377 
4378 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4379 			scsa2usbp->scsa2usb_bulkin_pipe = NULL;
4380 
4381 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4382 			    scsa2usbp->scsa2usb_log_handle,
4383 			    "scsa2usb_open_usb_pipes: bulk/out pipe open"
4384 			    " failed rval = %d", rval);
4385 
4386 			return (USB_FAILURE);
4387 		}
4388 
4389 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4390 
4391 		/* open interrupt pipe for CBI protocol */
4392 		if (SCSA2USB_IS_CBI(scsa2usbp)) {
4393 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4394 
4395 			if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip,
4396 			    &scsa2usbp->scsa2usb_intr_ept, &policy,
4397 			    USB_FLAGS_SLEEP, &scsa2usbp->scsa2usb_intr_pipe)) !=
4398 			    USB_SUCCESS) {
4399 				usb_pipe_close(scsa2usbp->scsa2usb_dip,
4400 				    scsa2usbp->scsa2usb_bulkin_pipe,
4401 				    USB_FLAGS_SLEEP, NULL, NULL);
4402 
4403 				usb_pipe_close(scsa2usbp->scsa2usb_dip,
4404 				    scsa2usbp->scsa2usb_bulkout_pipe,
4405 				    USB_FLAGS_SLEEP, NULL, NULL);
4406 
4407 				mutex_enter(&scsa2usbp->scsa2usb_mutex);
4408 				scsa2usbp->scsa2usb_bulkin_pipe = NULL;
4409 				scsa2usbp->scsa2usb_bulkout_pipe = NULL;
4410 
4411 				USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4412 				    scsa2usbp->scsa2usb_log_handle,
4413 				    "scsa2usb_open_usb_pipes: intr pipe open"
4414 				    " failed rval = %d", rval);
4415 
4416 				return (USB_FAILURE);
4417 			}
4418 
4419 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4420 		}
4421 
4422 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4423 
4424 		/* get the max transfer size of the bulk pipe */
4425 		if (usb_pipe_get_max_bulk_transfer_size(scsa2usbp->scsa2usb_dip,
4426 		    &sz) == USB_SUCCESS) {
4427 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4428 			scsa2usbp->scsa2usb_max_bulk_xfer_size = sz;
4429 		} else {
4430 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4431 			scsa2usbp->scsa2usb_max_bulk_xfer_size = DEV_BSIZE;
4432 		}
4433 
4434 		/* limit the xfer size */
4435 		scsa2usbp->scsa2usb_max_bulk_xfer_size = min(
4436 			scsa2usbp->scsa2usb_max_bulk_xfer_size,
4437 			scsa2usb_max_bulk_xfer_size);
4438 
4439 		USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4440 		    "scsa2usb_open_usb_pipes: max bulk transfer size = %lx",
4441 		    scsa2usbp->scsa2usb_max_bulk_xfer_size);
4442 
4443 		/* Set the pipes opened flag */
4444 		scsa2usbp->scsa2usb_flags |= SCSA2USB_FLAGS_PIPES_OPENED;
4445 
4446 		scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_NORMAL;
4447 
4448 		/* Set the state to NONE */
4449 		scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_NONE;
4450 	}
4451 
4452 	return (USB_SUCCESS);
4453 }
4454 
4455 
4456 /*
4457  * scsa2usb_close_usb_pipes:
4458  *	close all pipes synchronously
4459  */
4460 void
4461 scsa2usb_close_usb_pipes(scsa2usb_state_t *scsa2usbp)
4462 {
4463 	usb_flags_t flags = USB_FLAGS_SLEEP;
4464 
4465 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4466 	    "scsa2usb_close_usb_pipes: scsa2usb_state = 0x%p", scsa2usbp);
4467 
4468 	ASSERT(scsa2usbp);
4469 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4470 
4471 	if ((scsa2usbp->scsa2usb_flags & SCSA2USB_FLAGS_PIPES_OPENED) == 0) {
4472 
4473 		return;
4474 	}
4475 
4476 	scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_CLOSING;
4477 	/* to avoid races, reset the flag first */
4478 	scsa2usbp->scsa2usb_flags &= ~SCSA2USB_FLAGS_PIPES_OPENED;
4479 
4480 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4481 
4482 	usb_pipe_close(scsa2usbp->scsa2usb_dip,
4483 	    scsa2usbp->scsa2usb_bulkout_pipe, flags, NULL, NULL);
4484 
4485 	usb_pipe_close(scsa2usbp->scsa2usb_dip,
4486 	    scsa2usbp->scsa2usb_bulkin_pipe, flags, NULL, NULL);
4487 
4488 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4489 	if (SCSA2USB_IS_CBI(scsa2usbp)) {
4490 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4491 		usb_pipe_close(scsa2usbp->scsa2usb_dip,
4492 		    scsa2usbp->scsa2usb_intr_pipe, flags, NULL, NULL);
4493 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4494 	}
4495 	scsa2usbp->scsa2usb_bulkout_pipe = NULL;
4496 	scsa2usbp->scsa2usb_bulkin_pipe = NULL;
4497 	scsa2usbp->scsa2usb_intr_pipe = NULL;
4498 
4499 	scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_NORMAL;
4500 }
4501 
4502 
4503 /*
4504  * scsa2usb_fill_up_cdb_lba:
4505  *	fill up command CDBs' LBA part
4506  */
4507 static void
4508 scsa2usb_fill_up_cdb_lba(scsa2usb_cmd_t *cmd, int lba)
4509 {
4510 	/* zero cdb1, lba bits so they won't get copied in the new cdb */
4511 	cmd->cmd_cdb[SCSA2USB_LUN] &= 0xE0;
4512 	cmd->cmd_cdb[SCSA2USB_LBA_0] = lba >> 24;
4513 	cmd->cmd_cdb[SCSA2USB_LBA_1] = lba >> 16;
4514 	cmd->cmd_cdb[SCSA2USB_LBA_2] = lba >> 8;
4515 	cmd->cmd_cdb[SCSA2USB_LBA_3] = (uchar_t)lba;
4516 	cmd->cmd_lba = lba;
4517 }
4518 
4519 
4520 /*
4521  * scsa2usb_fill_up_ReadCD_cdb_len:
4522  *	fill up READ_CD command CDBs' len part
4523  */
4524 static void
4525 scsa2usb_fill_up_ReadCD_cdb_len(scsa2usb_cmd_t *cmd, int len, int actual_len)
4526 {
4527 	cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_0] = len >> 16;
4528 	cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_1] = len >> 8;
4529 	cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_2] = (uchar_t)len;
4530 	cmd->cmd_actual_len = (uchar_t)actual_len;
4531 }
4532 
4533 
4534 /*
4535  * scsa2usb_fill_up_12byte_cdb_len:
4536  *	fill up generic 12-byte command CDBs' len part
4537  */
4538 static void
4539 scsa2usb_fill_up_12byte_cdb_len(scsa2usb_cmd_t *cmd, int len, int actual_len)
4540 {
4541 	cmd->cmd_cdb[6] = len >> 24;
4542 	cmd->cmd_cdb[7] = len >> 16;
4543 	cmd->cmd_cdb[8] = len >> 8;
4544 	cmd->cmd_cdb[9] = (uchar_t)len;
4545 	cmd->cmd_actual_len = (uchar_t)actual_len;
4546 }
4547 
4548 
4549 /*
4550  * scsa2usb_fill_up_cdb_len:
4551  *	fill up generic 10-byte command CDBs' len part
4552  */
4553 static void
4554 scsa2usb_fill_up_cdb_len(scsa2usb_cmd_t *cmd, int len)
4555 {
4556 	cmd->cmd_cdb[SCSA2USB_LEN_0] = len >> 8;
4557 	cmd->cmd_cdb[SCSA2USB_LEN_1] = (uchar_t)len;
4558 }
4559 
4560 
4561 /*
4562  * scsa2usb_read_cd_blk_size:
4563  *	For SCMD_READ_CD opcode (0xbe). Figure out the
4564  *	block size based on expected sector type field
4565  *	definition. See MMC SCSI Specs section 6.1.15
4566  *
4567  *	Based on the value of the "expected_sector_type"
4568  *	field, the block size could be different.
4569  */
4570 static int
4571 scsa2usb_read_cd_blk_size(uchar_t expected_sector_type)
4572 {
4573 	int blk_size;
4574 
4575 	switch (expected_sector_type) {
4576 	case READ_CD_EST_CDDA:
4577 		blk_size = CDROM_BLK_2352;
4578 		break;
4579 	case READ_CD_EST_MODE2:
4580 		blk_size = CDROM_BLK_2336;
4581 		break;
4582 	case READ_CD_EST_MODE2FORM2:
4583 		blk_size = CDROM_BLK_2324;
4584 		break;
4585 	case READ_CD_EST_MODE2FORM1:
4586 	case READ_CD_EST_ALLTYPE:
4587 	case READ_CD_EST_MODE1:
4588 	default:
4589 		blk_size = CDROM_BLK_2048;
4590 	}
4591 
4592 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, NULL, "scsa2usb_read_cd_blk_size: "
4593 	    "est = 0x%x blk_size = %d", expected_sector_type, blk_size);
4594 
4595 	return (blk_size);
4596 }
4597 
4598 
4599 /* needed for esballoc_wait() */
4600 /*ARGSUSED*/
4601 static void
4602 scsa2usb_null_free(char *arg)
4603 {
4604 }
4605 
4606 static frtn_t fr = {
4607 	scsa2usb_null_free,
4608 	NULL
4609 };
4610 
4611 
4612 /*
4613  * scsa2usb_bp_to_mblk:
4614  *	Convert a bp to mblk_t. USBA framework understands mblk_t.
4615  */
4616 static mblk_t *
4617 scsa2usb_bp_to_mblk(scsa2usb_state_t *scsa2usbp)
4618 {
4619 	size_t		size;
4620 	mblk_t		*mp;
4621 	struct buf	*bp;
4622 	scsa2usb_cmd_t	*cmd = PKT2CMD(scsa2usbp->scsa2usb_cur_pkt);
4623 
4624 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4625 	    "scsa2usb_bp_to_mblk: ");
4626 
4627 	ASSERT(scsa2usbp->scsa2usb_cur_pkt);
4628 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4629 
4630 	bp = cmd->cmd_bp;
4631 
4632 	if (bp && (bp->b_bcount > 0)) {
4633 		size = ((bp->b_bcount > cmd->cmd_xfercount) ?
4634 				cmd->cmd_xfercount : bp->b_bcount);
4635 	} else {
4636 
4637 		return (NULL);
4638 	}
4639 
4640 	mp = esballoc_wait((uchar_t *)bp->b_un.b_addr + cmd->cmd_offset,
4641 						size, BPRI_LO, &fr);
4642 
4643 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4644 	    "scsa2usb_bp_to_mblk: "
4645 	    "mp=0x%p bp=0x%p pkt=0x%p off=0x%lx sz=%lu add=0x%p", mp,
4646 	    (void *)bp, scsa2usbp->scsa2usb_cur_pkt, cmd->cmd_offset,
4647 	    bp->b_bcount - cmd->cmd_offset,
4648 	    bp->b_un.b_addr);
4649 
4650 	mp->b_wptr += size;
4651 	cmd->cmd_offset += size;
4652 
4653 	return (mp);
4654 }
4655 
4656 
4657 /*
4658  * scsa2usb_handle_data_start:
4659  *	Initiate the data xfer. It could be IN/OUT direction.
4660  *
4661  *	Data IN:
4662  *		Send out the bulk-xfer request
4663  *		if rval implies STALL
4664  *			clear endpoint stall and reset bulk-in pipe
4665  *			handle data read in so far; set cmd->cmd_done
4666  *			also adjust data xfer length accordingly
4667  *		else other error
4668  *			report back to transport
4669  *			typically transport will call reset recovery
4670  *		else (no error)
4671  *			return success
4672  *
4673  *	Data OUT:
4674  *		Send out the bulk-xfer request
4675  *		if rval implies STALL
4676  *			clear endpoint stall and reset bulk-in pipe
4677  *			adjust data xfer length
4678  *		else other error
4679  *			report back to transport
4680  *			typically transport will call reset recovery
4681  *		else (no error)
4682  *			return success
4683  *
4684  *	NOTE: We call this function only if there is xfercount.
4685  */
4686 int
4687 scsa2usb_handle_data_start(scsa2usb_state_t *scsa2usbp,
4688     scsa2usb_cmd_t *cmd, usb_bulk_req_t *req)
4689 {
4690 	int		rval = USB_SUCCESS;
4691 	uint_t		ept_addr;
4692 	usb_flags_t	flags = USB_FLAGS_SLEEP;
4693 #ifdef	SCSA2USB_BULK_ONLY_TEST
4694 	usb_req_attrs_t	attrs = 0;
4695 #else
4696 	usb_req_attrs_t	attrs = USB_ATTRS_SHORT_XFER_OK;
4697 #endif
4698 
4699 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4700 	    "scsa2usb_handle_data_start: BEGIN cmd = %p, req = %p", cmd, req);
4701 
4702 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4703 
4704 	switch (cmd->cmd_dir) {
4705 	case USB_EP_DIR_IN:
4706 #ifdef	SCSA2USB_BULK_ONLY_TEST
4707 		/*
4708 		 * This case occurs when the host expects to receive
4709 		 * more data than the device actually transfers. Hi > Di
4710 		 */
4711 		if (scsa2usb_test_case_5) {
4712 			usb_bulk_req_t *req2;
4713 
4714 			req->bulk_len = cmd->cmd_xfercount - 1;
4715 			req->bulk_attributes = 0;
4716 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4717 			SCSA2USB_FREE_MSG(req->bulk_data);
4718 			req->bulk_data = allocb_wait(req->bulk_len, BPRI_LO,
4719 			    STR_NOSIG, NULL);
4720 
4721 			ASSERT(req->bulk_timeout);
4722 			rval = usb_pipe_bulk_xfer(
4723 			    scsa2usbp->scsa2usb_bulkin_pipe, req, flags);
4724 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4725 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4726 			    scsa2usbp->scsa2usb_log_handle, "rval = %x", rval);
4727 
4728 			req2 = scsa2usb_init_bulk_req(scsa2usbp,
4729 			    cmd->cmd_xfercount + 2,
4730 			    cmd->cmd_timeout, 0, flags);
4731 			req2->bulk_len = cmd->cmd_xfercount + 2;
4732 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4733 
4734 			ASSERT(req2->bulk_timeout);
4735 			rval = usb_pipe_bulk_xfer(
4736 			    scsa2usbp->scsa2usb_bulkin_pipe, req2, flags);
4737 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4738 
4739 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4740 			    scsa2usbp->scsa2usb_log_handle,
4741 			    "TEST 5: Hi > Di: rval = 0x%x", rval);
4742 			scsa2usb_test_case_5 = 0;
4743 			usb_free_bulk_req(req2);
4744 
4745 			return (rval);
4746 		}
4747 
4748 		/*
4749 		 * This happens when the host expects to send data to the
4750 		 * device while the device intends to send data to the host.
4751 		 */
4752 		if (scsa2usb_test_case_8 && (cmd->cmd_cdb[0] == SCMD_READ_G1)) {
4753 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4754 			    scsa2usbp->scsa2usb_log_handle,
4755 			    "TEST 8: Hi <> Do: Step 2");
4756 			scsa2usb_test_mblk(scsa2usbp, B_TRUE);
4757 			scsa2usb_test_case_8 = 0;
4758 
4759 			return (rval);
4760 		}
4761 #endif	/* SCSA2USB_BULK_ONLY_TEST */
4762 
4763 		ept_addr = scsa2usbp->scsa2usb_bulkin_ept.bEndpointAddress;
4764 		req->bulk_len = cmd->cmd_xfercount;
4765 		req->bulk_attributes = attrs;
4766 		SCSA2USB_FREE_MSG(req->bulk_data);
4767 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4768 
4769 		req->bulk_data = esballoc_wait(
4770 				(uchar_t *)cmd->cmd_bp->b_un.b_addr +
4771 				cmd->cmd_offset,
4772 				req->bulk_len, BPRI_LO, &fr);
4773 
4774 		ASSERT(req->bulk_timeout);
4775 		rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkin_pipe,
4776 								req, flags);
4777 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4778 
4779 		break;
4780 
4781 	case USB_EP_DIR_OUT:
4782 #ifdef	SCSA2USB_BULK_ONLY_TEST
4783 		/*
4784 		 * This happens when the host expects to receive data
4785 		 * from the device while the device intends to receive
4786 		 * data from the host.
4787 		 */
4788 		if (scsa2usb_test_case_10 &&
4789 		    (cmd->cmd_cdb[0] == SCMD_WRITE_G1)) {
4790 			req->bulk_len = CSW_LEN;
4791 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4792 
4793 			ASSERT(req->bulk_timeout);
4794 			rval = usb_pipe_bulk_xfer(
4795 			    scsa2usbp->scsa2usb_bulkin_pipe, req, flags);
4796 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4797 
4798 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4799 			    scsa2usbp->scsa2usb_log_handle,
4800 			    "TEST 10: Ho <> Di: done rval = 0x%x",  rval);
4801 			scsa2usb_test_case_10 = 0;
4802 
4803 			return (rval);
4804 		}
4805 #endif	/* SCSA2USB_BULK_ONLY_TEST */
4806 
4807 		req->bulk_data = scsa2usb_bp_to_mblk(scsa2usbp);
4808 		if (req->bulk_data == NULL) {
4809 
4810 			return (USB_FAILURE);
4811 		}
4812 
4813 #ifdef	SCSA2USB_BULK_ONLY_TEST
4814 		if (scsa2usb_test_case_11) {
4815 			/*
4816 			 * Host expects to send data to the device and
4817 			 * device doesn't expect to receive any data
4818 			 */
4819 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4820 			    scsa2usbp->scsa2usb_log_handle, "TEST 11: Ho > Do");
4821 
4822 			scsa2usb_test_mblk(scsa2usbp, B_FALSE);
4823 			scsa2usb_test_case_11 = 0;
4824 		}
4825 #endif	/* SCSA2USB_BULK_ONLY_TEST */
4826 
4827 		ept_addr = scsa2usbp->scsa2usb_bulkout_ept.bEndpointAddress;
4828 		req->bulk_len = req->bulk_data->b_wptr - req->bulk_data->b_rptr;
4829 		req->bulk_timeout = scsa2usb_bulk_timeout(cmd->cmd_timeout);
4830 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4831 
4832 		ASSERT(req->bulk_timeout);
4833 		rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkout_pipe,
4834 								req, flags);
4835 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4836 		break;
4837 	}
4838 
4839 	USB_DPRINTF_L3(DPRINT_MASK_SCSA,
4840 	    scsa2usbp->scsa2usb_log_handle,
4841 	    "scsa2usb_handle_data_start: rval=%d cr=%d", rval,
4842 	    req->bulk_completion_reason);
4843 
4844 	if (rval != USB_SUCCESS) {
4845 		/* Handle Errors now */
4846 		if (req->bulk_completion_reason == USB_CR_STALL) {
4847 			if (cmd->cmd_dir == USB_EP_DIR_IN) {
4848 				(void) scsa2usb_clear_ept_stall(
4849 				    scsa2usbp, ept_addr,
4850 				    scsa2usbp-> scsa2usb_bulkin_pipe,
4851 				    "bulk-in");
4852 			} else {
4853 				(void) scsa2usb_clear_ept_stall(
4854 				    scsa2usbp, ept_addr,
4855 				    scsa2usbp-> scsa2usb_bulkout_pipe,
4856 				    "bulk-out");
4857 			}
4858 		}
4859 
4860 		/* no more data to transfer after this */
4861 		cmd->cmd_done = 1;
4862 	}
4863 
4864 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4865 	    "scsa2usb_handle_data_start: END %s data rval = %d",
4866 	    (cmd->cmd_dir == USB_EP_DIR_IN) ? "bulk-in" : "bulk-out", rval);
4867 
4868 	return (rval);
4869 }
4870 
4871 
4872 /*
4873  * scsa2usb_handle_data_done:
4874  *	This function handles the completion of the data xfer.
4875  *	It also massages the inquiry data. This function may
4876  *	also be called after a stall.
4877  */
4878 void
4879 scsa2usb_handle_data_done(scsa2usb_state_t *scsa2usbp,
4880     scsa2usb_cmd_t *cmd, usb_bulk_req_t *req)
4881 {
4882 	struct buf	*bp = cmd->cmd_bp;
4883 	struct scsi_pkt	*pkt = scsa2usbp->scsa2usb_cur_pkt;
4884 	mblk_t		*data = req->bulk_data;
4885 	int		len = data ? (data->b_wptr - data->b_rptr) : 0;
4886 
4887 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4888 
4889 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4890 	    "scsa2usb_handle_data_done:\n\tcmd = 0x%p data = 0x%p len = 0x%x",
4891 	    cmd, data, len);
4892 
4893 	cmd->cmd_resid_xfercount = cmd->cmd_xfercount - len;
4894 
4895 	if (len)  {
4896 		uchar_t	*p;
4897 		scsa2usb_read_cap_t *cap;
4898 
4899 		switch (cmd->cmd_cdb[SCSA2USB_OPCODE]) {
4900 		case SCMD_INQUIRY:
4901 			/*
4902 			 * cache a copy of the inquiry data for our own use
4903 			 * but ensure that we have at least up to
4904 			 * inq_revision, inq_serial is not required.
4905 			 * ignore inquiry data returned for inquiry commands
4906 			 * with SCSI-3 EVPD, CmdDt bits set.
4907 			 */
4908 			if (((cmd->cmd_cdb[SCSA2USB_LUN] & 0x1f) == 0) &&
4909 			    (len >= SCSA2USB_MAX_INQ_LEN)) {
4910 				bzero(&scsa2usbp->scsa2usb_lun_inquiry
4911 				    [pkt->pkt_address.a_lun],
4912 				    sizeof (struct scsi_inquiry));
4913 				bcopy(data->b_rptr,
4914 				    &scsa2usbp->scsa2usb_lun_inquiry
4915 				    [pkt->pkt_address.a_lun], len);
4916 			}
4917 
4918 			USB_DPRINTF_L3(DPRINT_MASK_SCSA,
4919 			    scsa2usbp->scsa2usb_log_handle,
4920 			    "scsi inquiry type = 0x%x",
4921 			    scsa2usbp->scsa2usb_lun_inquiry
4922 			    [pkt->pkt_address.a_lun].inq_dtype);
4923 
4924 			cmd->cmd_done = 1;
4925 			goto handle_data;
4926 
4927 		case SCMD_READ_CAPACITY:
4928 			cap = (scsa2usb_read_cap_t *)data->b_rptr;
4929 
4930 			/* Figure out the logical block size */
4931 			if ((len >= sizeof (struct scsa2usb_read_cap)) &&
4932 			    (req->bulk_completion_reason == USB_CR_OK)) {
4933 				scsa2usbp->
4934 				    scsa2usb_lbasize[pkt->pkt_address.a_lun] =
4935 				    SCSA2USB_MK_32BIT(
4936 					    cap->scsa2usb_read_cap_blen3,
4937 					    cap->scsa2usb_read_cap_blen2,
4938 					    cap->scsa2usb_read_cap_blen1,
4939 					    cap->scsa2usb_read_cap_blen0);
4940 
4941 				USB_DPRINTF_L3(DPRINT_MASK_SCSA,
4942 				    scsa2usbp->scsa2usb_log_handle,
4943 				    "lbasize=%d", scsa2usbp->
4944 				    scsa2usb_lbasize[pkt->pkt_address.a_lun]);
4945 			}
4946 			cmd->cmd_done = 1;
4947 			goto handle_data;
4948 
4949 		case SCMD_REQUEST_SENSE:
4950 			p = data->b_rptr;
4951 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4952 			    scsa2usbp->scsa2usb_log_handle,
4953 			    "cdb: %x rqsense: "
4954 			    "%x %x %x %x %x %x %x %x %x %x\n\t"
4955 			    "%x %x %x %x %x %x %x %x %x %x",
4956 			    cmd->cmd_cdb[0],
4957 			    p[0], p[1], p[2], p[3], p[4],
4958 			    p[5], p[6], p[7], p[8], p[9],
4959 			    p[10], p[11], p[12], p[13], p[14],
4960 			    p[15], p[16], p[17], p[18], p[19]);
4961 
4962 			scsa2usbp->scsa2usb_last_cmd.status = p[2];
4963 			cmd->cmd_done = 1;
4964 			/* FALLTHROUGH */
4965 
4966 		default:
4967 handle_data:
4968 			if (bp && len && (cmd->cmd_dir == USB_EP_DIR_IN)) {
4969 				/*
4970 				 * we don't have to copy the data, the
4971 				 * data pointers for the mblk_t for
4972 				 * the bulk-in xfer points to the
4973 				 * struct buf * data.
4974 				 */
4975 				cmd->cmd_offset += len;
4976 			}
4977 
4978 			USB_DPRINTF_L3(DPRINT_MASK_SCSA,
4979 			    scsa2usbp->scsa2usb_log_handle,
4980 			    "len = 0x%x total = 0x%lx",
4981 			    len, cmd->cmd_total_xfercount);
4982 
4983 			/*
4984 			 * update total_xfercount now but it may be
4985 			 * adjusted after receiving the residue
4986 			 */
4987 			cmd->cmd_total_xfercount -= len;
4988 
4989 			if ((req->bulk_completion_reason != USB_CR_OK) ||
4990 			    (cmd->cmd_resid_xfercount != 0) ||
4991 			    (cmd->cmd_total_xfercount == 0)) {
4992 				/* set pkt_resid to total to be sure */
4993 				pkt->pkt_resid = cmd->cmd_total_xfercount;
4994 				cmd->cmd_done = 1;
4995 			}
4996 
4997 			break;
4998 		}
4999 	} else {
5000 		if (cmd->cmd_dir == USB_EP_DIR_OUT) {
5001 			if (cmd->cmd_total_xfercount == 0) {
5002 				cmd->cmd_done = 1;
5003 			}
5004 		}
5005 	}
5006 }
5007 
5008 
5009 /*
5010  * scsa2usb_init_bulk_req:
5011  *	Allocate (synchronously) and fill in a bulk-request
5012  */
5013 usb_bulk_req_t *
5014 scsa2usb_init_bulk_req(scsa2usb_state_t *scsa2usbp, size_t length,
5015     uint_t timeout, usb_req_attrs_t attrs, usb_flags_t flags)
5016 {
5017 	usb_bulk_req_t	*req;
5018 
5019 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5020 
5021 	req = usb_alloc_bulk_req(scsa2usbp->scsa2usb_dip, length,
5022 	    flags | USB_FLAGS_SLEEP);
5023 
5024 	req->bulk_len = length;			/* xfer length */
5025 	req->bulk_timeout = scsa2usb_bulk_timeout(timeout); /* xfer timeout */
5026 	req->bulk_attributes = attrs;		/* xfer attrs */
5027 	req->bulk_client_private = (usb_opaque_t)scsa2usbp; /* statep */
5028 
5029 	return (req);
5030 }
5031 
5032 
5033 /*
5034  * scsa2usb_bulk_timeout:
5035  *	ensure that bulk requests do not have infinite timeout values
5036  */
5037 int
5038 scsa2usb_bulk_timeout(int timeout)
5039 {
5040 	return ((timeout == 0) ? scsa2usb_long_timeout : timeout);
5041 }
5042 
5043 
5044 /*
5045  * scsa2usb_clear_ept_stall:
5046  *	clear endpoint stall and reset pipes
5047  */
5048 int
5049 scsa2usb_clear_ept_stall(scsa2usb_state_t *scsa2usbp, uint_t ept_addr,
5050     usb_pipe_handle_t ph, char *what)
5051 {
5052 	int rval;
5053 	dev_info_t *dip = scsa2usbp->scsa2usb_dip;
5054 
5055 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5056 	if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
5057 
5058 		return (USB_FAILURE);
5059 	}
5060 
5061 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5062 	rval = usb_clr_feature(dip, USB_DEV_REQ_RCPT_EP, 0, ept_addr,
5063 	    USB_FLAGS_SLEEP, NULL, NULL);
5064 
5065 	usb_pipe_reset(dip, ph, USB_FLAGS_SLEEP, NULL, NULL);
5066 
5067 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5068 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5069 	    "scsa2usb_clear_ept_stall: on %s: ept = 0x%x rval = %d",
5070 	    what, ept_addr, rval);
5071 
5072 	return (rval);
5073 }
5074 
5075 
5076 /*
5077  * scsa2usb_pkt_completion:
5078  *	Handle pkt completion.
5079  */
5080 static void
5081 scsa2usb_pkt_completion(scsa2usb_state_t *scsa2usbp, struct scsi_pkt *pkt)
5082 {
5083 	scsa2usb_cmd_t *cmd = PKT2CMD(pkt);
5084 
5085 	ASSERT(pkt);
5086 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5087 
5088 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5089 	    "scsa2usb_pkt_completion:\n\tscsa2usbp = 0x%p "
5090 	    "reason=%d, status=%d state=0x%x stats=0x%x resid=0x%lx",
5091 	    scsa2usbp, pkt->pkt_reason, *(pkt->pkt_scbp),
5092 	    pkt->pkt_state, pkt->pkt_statistics, pkt->pkt_resid);
5093 
5094 	if (pkt->pkt_reason == CMD_CMPLT) {
5095 		pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET |
5096 					STATE_SENT_CMD | STATE_GOT_STATUS;
5097 		if (cmd->cmd_xfercount) {
5098 			pkt->pkt_state |= STATE_XFERRED_DATA;
5099 		}
5100 	} else {
5101 		pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET |
5102 					STATE_SENT_CMD;
5103 	}
5104 
5105 	/*
5106 	 * don't zap the current state when in panic as this will
5107 	 * make debugging harder
5108 	 */
5109 	if ((scsa2usbp->scsa2usb_cur_pkt == pkt) && !ddi_in_panic()) {
5110 		SCSA2USB_RESET_CUR_PKT(scsa2usbp);
5111 
5112 		/* save the last command */
5113 		bcopy(pkt->pkt_cdbp, scsa2usbp->scsa2usb_last_cmd.cdb,
5114 		    sizeof (scsa2usbp->scsa2usb_last_cmd.cdb));
5115 
5116 		/* reset the scsa2usb_last_cmd.status value */
5117 		if ((pkt->pkt_cdbp[0] != SCMD_REQUEST_SENSE) &&
5118 		    (pkt->pkt_cdbp[0] != SCMD_INQUIRY)) {
5119 			scsa2usbp->scsa2usb_last_cmd.status = 0;
5120 		}
5121 
5122 		/*
5123 		 * set pkt state to NONE *before* calling back as the target
5124 		 * driver will immediately submit the next packet
5125 		 */
5126 		scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_NONE;
5127 	}
5128 
5129 	if (pkt->pkt_comp) {
5130 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5131 		pkt->pkt_comp(pkt);
5132 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5133 
5134 	}
5135 }
5136 
5137 
5138 /*
5139  * Even handling functions:
5140  *
5141  * scsa2usb_reconnect_event_cb:
5142  *	event handling
5143  */
5144 static int
5145 scsa2usb_reconnect_event_cb(dev_info_t *dip)
5146 {
5147 	scsa2usb_state_t *scsa2usbp =
5148 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
5149 	dev_info_t	*cdip;
5150 	int		circ;
5151 	int		rval = USB_SUCCESS;
5152 
5153 	ASSERT(scsa2usbp != NULL);
5154 
5155 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5156 	    "scsa2usb_reconnect_event_cb: dip = 0x%p", dip);
5157 
5158 	scsa2usb_restore_device_state(dip, scsa2usbp);
5159 
5160 	USB_DPRINTF_L0(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5161 	    "Reinserted device is accessible again.");
5162 
5163 	ndi_devi_enter(dip, &circ);
5164 	for (cdip = ddi_get_child(dip); cdip; ) {
5165 		dev_info_t *next = ddi_get_next_sibling(cdip);
5166 
5167 		mutex_enter(&DEVI(cdip)->devi_lock);
5168 		DEVI_SET_DEVICE_REINSERTED(cdip);
5169 		mutex_exit(&DEVI(cdip)->devi_lock);
5170 
5171 		cdip = next;
5172 	}
5173 	ndi_devi_exit(dip, circ);
5174 
5175 	/* stop suppressing warnings */
5176 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5177 	scsa2usbp->scsa2usb_warning_given = B_FALSE;
5178 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5179 
5180 	if (scsa2usbp->scsa2usb_ugen_hdl) {
5181 		rval = usb_ugen_reconnect_ev_cb(
5182 			scsa2usbp->scsa2usb_ugen_hdl);
5183 	}
5184 
5185 	return (rval);
5186 }
5187 
5188 
5189 /*
5190  * scsa2usb_all_waitQs_empty:
5191  *	check if all waitQs empty
5192  */
5193 static int
5194 scsa2usb_all_waitQs_empty(scsa2usb_state_t *scsa2usbp)
5195 {
5196 	uint_t	lun;
5197 
5198 	for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
5199 		if (usba_list_entry_count(
5200 		    &scsa2usbp->scsa2usb_waitQ[lun])) {
5201 
5202 			return (USB_FAILURE);
5203 		}
5204 	}
5205 
5206 	return (USB_SUCCESS);
5207 }
5208 
5209 
5210 /*
5211  * scsa2usb_disconnect_event_cb:
5212  *	callback for disconnect events
5213  */
5214 static int
5215 scsa2usb_disconnect_event_cb(dev_info_t *dip)
5216 {
5217 	scsa2usb_state_t *scsa2usbp =
5218 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
5219 	dev_info_t	*cdip;
5220 	int		circ, i;
5221 	int		rval = USB_SUCCESS;
5222 
5223 	ASSERT(scsa2usbp != NULL);
5224 
5225 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5226 	    "scsa2usb_disconnect_event_cb: dip = 0x%p", dip);
5227 
5228 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5229 	scsa2usbp->scsa2usb_dev_state = USB_DEV_DISCONNECTED;
5230 
5231 	/*
5232 	 * wait till the work thread is done, carry on regardless
5233 	 * if not.
5234 	 */
5235 	for (i = 0; i < SCSA2USB_DRAIN_TIMEOUT; i++) {
5236 		if ((scsa2usbp->scsa2usb_work_thread_id == NULL) &&
5237 		    (scsa2usbp->scsa2usb_cur_pkt == NULL) &&
5238 		    (scsa2usb_all_waitQs_empty(scsa2usbp) ==
5239 		    USB_SUCCESS)) {
5240 
5241 			break;
5242 		}
5243 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5244 		delay(drv_usectohz(1000000));
5245 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5246 	}
5247 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5248 
5249 	ndi_devi_enter(dip, &circ);
5250 	for (cdip = ddi_get_child(dip); cdip; ) {
5251 		dev_info_t *next = ddi_get_next_sibling(cdip);
5252 
5253 		mutex_enter(&DEVI(cdip)->devi_lock);
5254 		DEVI_SET_DEVICE_REMOVED(cdip);
5255 		mutex_exit(&DEVI(cdip)->devi_lock);
5256 
5257 		cdip = next;
5258 	}
5259 	ndi_devi_exit(dip, circ);
5260 
5261 	if (scsa2usbp->scsa2usb_ugen_hdl) {
5262 		rval = usb_ugen_disconnect_ev_cb(
5263 					scsa2usbp->scsa2usb_ugen_hdl);
5264 	}
5265 
5266 	return (rval);
5267 }
5268 
5269 
5270 /*
5271  * PM support
5272  *
5273  * scsa2usb_create_pm_components:
5274  *	create the pm components required for power management
5275  *	no mutex is need when calling USBA interfaces
5276  */
5277 static void
5278 scsa2usb_create_pm_components(dev_info_t *dip, scsa2usb_state_t *scsa2usbp)
5279 {
5280 	scsa2usb_power_t *pm;
5281 	uint_t		pwr_states;
5282 
5283 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5284 
5285 	USB_DPRINTF_L4(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5286 	    "scsa2usb_create_pm_components: dip = 0x%p, scsa2usbp = 0x%p",
5287 	    dip, scsa2usbp);
5288 
5289 	/*
5290 	 * determine if this device is on the blacklist
5291 	 * or if a conf file entry has disabled PM
5292 	 */
5293 	if ((scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_PM) == 0) {
5294 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5295 		    "device cannot be power managed");
5296 
5297 		return;
5298 	}
5299 
5300 	/* Allocate the PM state structure */
5301 	pm = kmem_zalloc(sizeof (scsa2usb_power_t), KM_SLEEP);
5302 
5303 	scsa2usbp->scsa2usb_pm = pm;
5304 	pm->scsa2usb_current_power = USB_DEV_OS_FULL_PWR;
5305 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5306 
5307 	if (usb_create_pm_components(dip, &pwr_states) ==
5308 	    USB_SUCCESS) {
5309 		if (usb_handle_remote_wakeup(dip,
5310 		    USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) {
5311 			pm->scsa2usb_wakeup_enabled = 1;
5312 		}
5313 
5314 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5315 		pm->scsa2usb_pwr_states = (uint8_t)pwr_states;
5316 		scsa2usb_raise_power(scsa2usbp);
5317 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5318 	}
5319 
5320 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5321 }
5322 
5323 
5324 /*
5325  * scsa2usb_raise_power:
5326  *	check if the device is using full power or not
5327  */
5328 static void
5329 scsa2usb_raise_power(scsa2usb_state_t *scsa2usbp)
5330 {
5331 	USB_DPRINTF_L4(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5332 	    "scsa2usb_raise_power:");
5333 
5334 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5335 
5336 	if (scsa2usbp->scsa2usb_pm) {
5337 		scsa2usb_pm_busy_component(scsa2usbp);
5338 		if (scsa2usbp->scsa2usb_pm->scsa2usb_current_power !=
5339 		    USB_DEV_OS_FULL_PWR) {
5340 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
5341 			(void) pm_raise_power(scsa2usbp->scsa2usb_dip,
5342 			    0, USB_DEV_OS_FULL_PWR);
5343 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
5344 		}
5345 	}
5346 }
5347 
5348 
5349 /*
5350  * functions to handle power transition for OS levels 0 -> 3
5351  */
5352 static int
5353 scsa2usb_pwrlvl0(scsa2usb_state_t *scsa2usbp)
5354 {
5355 	int	rval;
5356 
5357 	switch (scsa2usbp->scsa2usb_dev_state) {
5358 	case USB_DEV_ONLINE:
5359 		/* Deny the powerdown request if the device is busy */
5360 		if (scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy != 0) {
5361 
5362 			return (USB_FAILURE);
5363 		}
5364 
5365 		/*
5366 		 * stop polling on interrupt pipe
5367 		 */
5368 		scsa2usb_cbi_stop_intr_polling(scsa2usbp);
5369 
5370 		/* Issue USB D3 command to the device here */
5371 		rval = usb_set_device_pwrlvl3(scsa2usbp->scsa2usb_dip);
5372 		ASSERT(rval == USB_SUCCESS);
5373 
5374 		scsa2usbp->scsa2usb_dev_state = USB_DEV_PWRED_DOWN;
5375 
5376 		/* FALLTHRU */
5377 	case USB_DEV_DISCONNECTED:
5378 	case USB_DEV_SUSPENDED:
5379 	case USB_DEV_PWRED_DOWN:
5380 	default:
5381 		scsa2usbp->scsa2usb_pm->scsa2usb_current_power =
5382 						USB_DEV_OS_PWR_OFF;
5383 
5384 		return (USB_SUCCESS);
5385 	}
5386 }
5387 
5388 
5389 static int
5390 scsa2usb_pwrlvl1(scsa2usb_state_t *scsa2usbp)
5391 {
5392 	int	rval;
5393 
5394 	/* Issue USB D2 command to the device here */
5395 	rval = usb_set_device_pwrlvl2(scsa2usbp->scsa2usb_dip);
5396 	ASSERT(rval == USB_SUCCESS);
5397 
5398 	return (DDI_FAILURE);
5399 }
5400 
5401 
5402 static int
5403 scsa2usb_pwrlvl2(scsa2usb_state_t *scsa2usbp)
5404 {
5405 	int	rval;
5406 
5407 	/* Issue USB D1 command to the device here */
5408 	rval = usb_set_device_pwrlvl1(scsa2usbp->scsa2usb_dip);
5409 	ASSERT(rval == USB_SUCCESS);
5410 
5411 	return (DDI_FAILURE);
5412 }
5413 
5414 
5415 static int
5416 scsa2usb_pwrlvl3(scsa2usb_state_t *scsa2usbp)
5417 {
5418 	int	rval;
5419 
5420 	/*
5421 	 * PM framework tries to put us in full power
5422 	 * during system shutdown. If we are disconnected
5423 	 * return success anyways
5424 	 */
5425 	if (scsa2usbp->scsa2usb_dev_state != USB_DEV_DISCONNECTED) {
5426 		/* Issue USB D0 command to the device here */
5427 		rval = usb_set_device_pwrlvl0(scsa2usbp->scsa2usb_dip);
5428 		ASSERT(rval == USB_SUCCESS);
5429 
5430 		scsa2usbp->scsa2usb_dev_state = USB_DEV_ONLINE;
5431 	}
5432 	scsa2usbp->scsa2usb_pm->scsa2usb_current_power = USB_DEV_OS_FULL_PWR;
5433 
5434 	return (DDI_SUCCESS);
5435 }
5436 
5437 
5438 /*
5439  * scsa2usb_power:
5440  *	power entry point
5441  */
5442 /* ARGSUSED */
5443 static int
5444 scsa2usb_power(dev_info_t *dip, int comp, int level)
5445 {
5446 	scsa2usb_state_t	*scsa2usbp;
5447 	scsa2usb_power_t	*pm;
5448 	int			rval = DDI_FAILURE;
5449 
5450 	scsa2usbp = ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
5451 
5452 	USB_DPRINTF_L3(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5453 	    "scsa2usb_power: Begin scsa2usbp (%p): level = %d",
5454 	    scsa2usbp, level);
5455 
5456 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5457 	if (SCSA2USB_BUSY(scsa2usbp)) {
5458 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5459 		    "scsa2usb_power: busy");
5460 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5461 
5462 		return (rval);
5463 	}
5464 
5465 	pm = scsa2usbp->scsa2usb_pm;
5466 	if (pm == NULL) {
5467 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5468 		    "scsa2usb_power: pm NULL");
5469 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5470 
5471 		return (rval);
5472 	}
5473 
5474 	/* check if we are transitioning to a legal power level */
5475 	if (USB_DEV_PWRSTATE_OK(pm->scsa2usb_pwr_states, level)) {
5476 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5477 		    "scsa2usb_power: illegal power level = %d "
5478 		    "pwr_states: %x", level, pm->scsa2usb_pwr_states);
5479 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5480 
5481 		return (rval);
5482 	}
5483 
5484 	switch (level) {
5485 	case USB_DEV_OS_PWR_OFF :
5486 		rval = scsa2usb_pwrlvl0(scsa2usbp);
5487 		break;
5488 	case USB_DEV_OS_PWR_1 :
5489 		rval = scsa2usb_pwrlvl1(scsa2usbp);
5490 		break;
5491 	case USB_DEV_OS_PWR_2 :
5492 		rval = scsa2usb_pwrlvl2(scsa2usbp);
5493 		break;
5494 	case USB_DEV_OS_FULL_PWR :
5495 		rval = scsa2usb_pwrlvl3(scsa2usbp);
5496 		break;
5497 	}
5498 
5499 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5500 
5501 	return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
5502 }
5503 
5504 
5505 static void
5506 scsa2usb_pm_busy_component(scsa2usb_state_t *scsa2usbp)
5507 {
5508 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5509 
5510 	if (scsa2usbp->scsa2usb_pm) {
5511 		scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy++;
5512 
5513 		USB_DPRINTF_L4(DPRINT_MASK_PM,
5514 		    scsa2usbp->scsa2usb_log_handle,
5515 		    "scsa2usb_pm_busy_component: %d",
5516 		    scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy);
5517 
5518 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5519 
5520 		if (pm_busy_component(scsa2usbp->scsa2usb_dip, 0) !=
5521 		    DDI_SUCCESS) {
5522 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
5523 			ASSERT(scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy > 0);
5524 			scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy--;
5525 
5526 			USB_DPRINTF_L2(DPRINT_MASK_PM,
5527 			    scsa2usbp->scsa2usb_log_handle,
5528 			    "scsa2usb_pm_busy_component failed: %d",
5529 			    scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy);
5530 
5531 			return;
5532 		}
5533 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5534 	}
5535 }
5536 
5537 
5538 /*
5539  * scsa2usb_pm_idle_component:
5540  *	idles the device
5541  */
5542 static void
5543 scsa2usb_pm_idle_component(scsa2usb_state_t *scsa2usbp)
5544 {
5545 	ASSERT(!mutex_owned(&scsa2usbp->scsa2usb_mutex));
5546 
5547 	if (scsa2usbp->scsa2usb_pm) {
5548 		if (pm_idle_component(scsa2usbp->scsa2usb_dip, 0) ==
5549 		    DDI_SUCCESS) {
5550 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
5551 			ASSERT(scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy > 0);
5552 			scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy--;
5553 
5554 			USB_DPRINTF_L4(DPRINT_MASK_PM,
5555 			    scsa2usbp->scsa2usb_log_handle,
5556 			    "scsa2usb_pm_idle_component: %d",
5557 			    scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy);
5558 
5559 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
5560 		}
5561 	}
5562 }
5563 
5564 
5565 #ifdef	DEBUG
5566 /*
5567  * scsa2usb_print_cdb:
5568  *	prints CDB
5569  */
5570 void
5571 scsa2usb_print_cdb(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd)
5572 {
5573 	uchar_t *c = (uchar_t *)&cmd->cmd_cdb;
5574 
5575 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5576 	    "cmd = 0x%p opcode=%s "
5577 	    "cdb: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
5578 	    cmd, scsi_cname(cmd->cmd_cdb[SCSA2USB_OPCODE], scsa2usb_cmds),
5579 	    c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8],
5580 	    c[9], c[10], c[11], c[12], c[13], c[14], c[15]);
5581 }
5582 #endif	/* DEBUG */
5583 
5584 
5585 #ifdef	SCSA2USB_BULK_ONLY_TEST
5586 /*
5587  * scsa2usb_test_mblk:
5588  *	This function sends a dummy data mblk_t to simulate
5589  *	the following test cases: 5 and 11.
5590  */
5591 static void
5592 scsa2usb_test_mblk(scsa2usb_state_t *scsa2usbp, boolean_t large)
5593 {
5594 	int			i, rval;
5595 	size_t			len;
5596 	usb_flags_t		flags = USB_FLAGS_SLEEP;
5597 	usb_bulk_req_t		*req;
5598 
5599 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5600 
5601 	/* should we create a larger mblk? */
5602 	len = (large == B_TRUE) ? DEV_BSIZE : USB_BULK_CBWCMD_LEN;
5603 
5604 	req = scsa2usb_init_bulk_req(scsa2usbp, len,
5605 		SCSA2USB_BULK_PIPE_TIMEOUT, 0, flags);
5606 
5607 	/* fill up the data mblk */
5608 	for (i = 0; i < len; i++) {
5609 		*req->bulk_data->b_wptr++ = (uchar_t)i;
5610 	}
5611 
5612 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5613 	ASSERT(req->bulk_timeout);
5614 	rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkout_pipe, req, flags);
5615 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5616 
5617 	USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5618 	    "scsa2usb_test_mblk: Sent Data Out rval = 0x%x", rval);
5619 
5620 	usb_free_bulk_req(req);
5621 }
5622 #endif	/* SCSA2USB_BULK_ONLY_TEST */
5623