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