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