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