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