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