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