xref: /illumos-gate/usr/src/uts/common/io/usb/scsa2usb/scsa2usb.c (revision 1b8adde7ba7d5e04395c141c5400dc2cffd7d809)
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 void	scsa2usb_fake_inquiry(scsa2usb_state_t *,
79 		    struct scsi_inquiry *);
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 	ddi_quiesce_not_needed,		/* quiesce */
478 };
479 
480 static struct modldrv modldrv = {
481 	&mod_driverops,			/* Module type. This one is a driver */
482 	"SCSA to USB Driver",	/* Name of the module. */
483 	&scsa2usb_ops,			/* driver ops */
484 };
485 
486 static struct modlinkage modlinkage = {
487 	MODREV_1, (void *)&modldrv, NULL
488 };
489 
490 /* event support */
491 static usb_event_t scsa2usb_events = {
492 	scsa2usb_disconnect_event_cb,
493 	scsa2usb_reconnect_event_cb,
494 	NULL, NULL
495 };
496 
497 int
498 _init(void)
499 {
500 	int rval;
501 
502 	if (((rval = ddi_soft_state_init(&scsa2usb_statep,
503 	    sizeof (scsa2usb_state_t), SCSA2USB_INITIAL_ALLOC)) != 0)) {
504 
505 		return (rval);
506 	}
507 
508 	if ((rval = scsi_hba_init(&modlinkage)) != 0) {
509 		ddi_soft_state_fini(&scsa2usb_statep);
510 
511 		return (rval);
512 	}
513 
514 	if ((rval = mod_install(&modlinkage)) != 0) {
515 		scsi_hba_fini(&modlinkage);
516 		ddi_soft_state_fini(&scsa2usb_statep);
517 
518 		return (rval);
519 	}
520 
521 	return (rval);
522 }
523 
524 
525 int
526 _fini(void)
527 {
528 	int	rval;
529 
530 	if ((rval = mod_remove(&modlinkage)) == 0) {
531 		scsi_hba_fini(&modlinkage);
532 		ddi_soft_state_fini(&scsa2usb_statep);
533 	}
534 
535 	return (rval);
536 }
537 
538 
539 int
540 _info(struct modinfo *modinfop)
541 {
542 	return (mod_info(&modlinkage, modinfop));
543 }
544 
545 
546 /*
547  * scsa2usb_info :
548  *	Get minor number, soft state structure etc.
549  */
550 /*ARGSUSED*/
551 static int
552 scsa2usb_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
553     void *arg, void **result)
554 {
555 	scsa2usb_state_t *scsa2usbp = NULL;
556 	int error = DDI_FAILURE;
557 	int instance = SCSA2USB_MINOR_TO_INSTANCE(getminor((dev_t)arg));
558 
559 	switch (infocmd) {
560 	case DDI_INFO_DEVT2DEVINFO:
561 		if (((scsa2usbp = ddi_get_soft_state(scsa2usb_statep,
562 		    instance)) != NULL) &&
563 		    scsa2usbp->scsa2usb_dip) {
564 			*result = scsa2usbp->scsa2usb_dip;
565 			error = DDI_SUCCESS;
566 		} else {
567 			*result = NULL;
568 		}
569 		break;
570 	case DDI_INFO_DEVT2INSTANCE:
571 		*result = (void *)(uintptr_t)instance;
572 		error = DDI_SUCCESS;
573 		break;
574 	default:
575 		break;
576 	}
577 
578 	return (error);
579 }
580 
581 
582 /*
583  * scsa2usb_attach:
584  *	Attach driver
585  *	Allocate a "scsi_hba_tran" - call scsi_hba_tran_alloc()
586  *	Invoke scsi_hba_attach_setup
587  *	Get the serialno of the device
588  *	Open bulk pipes
589  *	Create disk child(ren)
590  *	Register events
591  *	Create and register panic callback
592  *
593  * NOTE: Replaced CBW_DIR_OUT with USB_EP_DIR_OUT and CBW_DIR_IN with
594  * USB_EP_DIR_IN as they are the same #defines.
595  */
596 static int
597 scsa2usb_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
598 {
599 	int			instance = ddi_get_instance(dip);
600 	int			interface;
601 	uint_t			lun;
602 	boolean_t		ept_check = B_TRUE;
603 	scsi_hba_tran_t		*tran;		/* scsi transport */
604 	scsa2usb_state_t	*scsa2usbp;
605 	usb_log_handle_t	log_handle;
606 	usb_ep_data_t		*ep_data;
607 	usb_client_dev_data_t	*dev_data;
608 	usb_alt_if_data_t	*altif_data;
609 	usb_ugen_info_t 	usb_ugen_info;
610 
611 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, NULL,
612 	    "scsa2usb_attach: dip = 0x%p", (void *)dip);
613 
614 	switch (cmd) {
615 	case DDI_ATTACH:
616 		break;
617 	case DDI_RESUME:
618 		scsa2usb_cpr_resume(dip);
619 
620 		return (DDI_SUCCESS);
621 	default:
622 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, NULL,
623 		    "scsa2usb_attach: failed");
624 
625 		return (DDI_FAILURE);
626 	}
627 
628 	/* Allocate softc information */
629 	if (ddi_soft_state_zalloc(scsa2usb_statep, instance) != DDI_SUCCESS) {
630 		ddi_prop_remove_all(dip);
631 
632 		return (DDI_FAILURE);
633 	}
634 
635 	/* get soft state space and initialize */
636 	if ((scsa2usbp = ddi_get_soft_state(scsa2usb_statep,
637 	    instance)) == NULL) {
638 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, NULL,
639 		    "scsa2usb%d: bad soft state", instance);
640 		ddi_prop_remove_all(dip);
641 
642 		return (DDI_FAILURE);
643 	}
644 
645 	scsa2usbp->scsa2usb_dip 	= dip;
646 	scsa2usbp->scsa2usb_instance	= instance;
647 
648 	/* allocate a log handle for debug/error messages */
649 	scsa2usbp->scsa2usb_log_handle = log_handle =
650 	    usb_alloc_log_hdl(dip, "s2u",
651 	    &scsa2usb_errlevel,
652 	    &scsa2usb_errmask, &scsa2usb_instance_debug,
653 	    0);
654 
655 	/* attach to USBA */
656 	if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) {
657 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
658 		    "usb_client_attach failed");
659 
660 		goto fail;
661 	}
662 	if (usb_get_dev_data(dip, &dev_data, USB_PARSE_LVL_IF, 0) !=
663 	    USB_SUCCESS) {
664 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
665 		    "usb_get_dev_data failed");
666 
667 		goto fail;
668 	}
669 
670 	/* initialize the mutex with the right cookie */
671 	mutex_init(&scsa2usbp->scsa2usb_mutex, NULL, MUTEX_DRIVER,
672 	    dev_data->dev_iblock_cookie);
673 	cv_init(&scsa2usbp->scsa2usb_transport_busy_cv, NULL, CV_DRIVER, NULL);
674 
675 	for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
676 		usba_init_list(&scsa2usbp->scsa2usb_waitQ[lun], NULL,
677 		    dev_data->dev_iblock_cookie);
678 	}
679 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
680 	scsa2usbp->scsa2usb_dip 	= dip;
681 	scsa2usbp->scsa2usb_instance	= instance;
682 	scsa2usbp->scsa2usb_attrs	= SCSA2USB_ALL_ATTRS;
683 	scsa2usbp->scsa2usb_dev_data	= dev_data;
684 
685 
686 	/* save the default pipe handle */
687 	scsa2usbp->scsa2usb_default_pipe = dev_data->dev_default_ph;
688 
689 	/* basic inits are done */
690 	scsa2usbp->scsa2usb_flags |= SCSA2USB_FLAGS_LOCKS_INIT;
691 
692 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, log_handle,
693 	    "curr_cfg=%ld, curr_if=%d",
694 	    (long)(dev_data->dev_curr_cfg - &dev_data->dev_cfg[0]),
695 	    dev_data->dev_curr_if);
696 
697 	interface = dev_data->dev_curr_if;
698 	scsa2usbp->scsa2usb_intfc_num = dev_data->dev_curr_if;
699 
700 	/* now find out relevant descriptors for alternate 0 */
701 	altif_data = &dev_data->dev_curr_cfg->cfg_if[interface].if_alt[0];
702 
703 	if (altif_data->altif_n_ep == 0) {
704 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
705 		    "invalid alt 0 for interface %d", interface);
706 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
707 
708 		goto fail;
709 	}
710 
711 	/* All CB/CBI, BO devices should have this value set */
712 	if (altif_data->altif_descr.bInterfaceClass !=
713 	    USB_CLASS_MASS_STORAGE) {
714 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
715 		    "invalid interface class (0x%x)",
716 		    altif_data->altif_descr.bInterfaceClass);
717 	}
718 	scsa2usbp->scsa2usb_intfc_descr = altif_data->altif_descr;
719 
720 	/* figure out the endpoints and copy the descr */
721 	if ((ep_data = usb_lookup_ep_data(dip, dev_data, interface, 0, 0,
722 	    USB_EP_ATTR_BULK, USB_EP_DIR_OUT)) != NULL) {
723 		scsa2usbp->scsa2usb_bulkout_ept = ep_data->ep_descr;
724 	}
725 	if ((ep_data = usb_lookup_ep_data(dip, dev_data, interface, 0, 0,
726 	    USB_EP_ATTR_BULK, USB_EP_DIR_IN)) != NULL) {
727 		scsa2usbp->scsa2usb_bulkin_ept = ep_data->ep_descr;
728 	}
729 	if ((ep_data = usb_lookup_ep_data(dip, dev_data, interface, 0, 0,
730 	    USB_EP_ATTR_INTR, USB_EP_DIR_IN)) != NULL) {
731 		scsa2usbp->scsa2usb_intr_ept = ep_data->ep_descr;
732 	}
733 
734 	/*
735 	 * check here for protocol and subclass supported by this driver
736 	 *
737 	 * first check if conf file has override values
738 	 * Note: override values are not used if supplied values are legal
739 	 */
740 	scsa2usb_override(scsa2usbp);
741 
742 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, log_handle,
743 	    "protocol=0x%x override=0x%x subclass=0x%x override=0x%x",
744 	    scsa2usbp->scsa2usb_intfc_descr.bInterfaceProtocol,
745 	    scsa2usbp->scsa2usb_protocol_override,
746 	    scsa2usbp->scsa2usb_intfc_descr.bInterfaceSubClass,
747 	    scsa2usbp->scsa2usb_subclass_override);
748 
749 	switch (scsa2usbp->scsa2usb_intfc_descr.bInterfaceProtocol) {
750 	case USB_PROTO_MS_CBI:
751 		scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_CB_PROTOCOL;
752 		break;
753 	case USB_PROTO_MS_CBI_WC:
754 		scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_CBI_PROTOCOL;
755 		break;
756 	case USB_PROTO_MS_ISD_1999_SILICN:
757 	case USB_PROTO_MS_BULK_ONLY:
758 		scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_BULK_ONLY_PROTOCOL;
759 		break;
760 	default:
761 		if (scsa2usbp->scsa2usb_protocol_override) {
762 			scsa2usbp->scsa2usb_cmd_protocol |=
763 			    scsa2usbp->scsa2usb_protocol_override;
764 			USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
765 			    "overriding protocol %x",
766 			    scsa2usbp->scsa2usb_intfc_descr.bInterfaceProtocol);
767 			break;
768 		}
769 
770 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
771 		    "unsupported protocol = %x",
772 		    scsa2usbp->scsa2usb_intfc_descr.bInterfaceProtocol);
773 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
774 
775 		goto fail;
776 	}
777 
778 	switch (scsa2usbp->scsa2usb_intfc_descr.bInterfaceSubClass) {
779 	case USB_SUBCLS_MS_SCSI:		/* transparent SCSI */
780 		scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_SCSI_CMDSET;
781 		break;
782 	case USB_SUBCLS_MS_SFF8020I:
783 	case USB_SUBCLS_MS_SFF8070I:
784 		scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_ATAPI_CMDSET;
785 		break;
786 	case USB_SUBCLS_MS_UFI:		/* UFI */
787 		scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_UFI_CMDSET;
788 		break;
789 	default:
790 		if (scsa2usbp->scsa2usb_subclass_override) {
791 			scsa2usbp->scsa2usb_cmd_protocol |=
792 			    scsa2usbp->scsa2usb_subclass_override;
793 			USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
794 			    "overriding subclass %x",
795 			    scsa2usbp->scsa2usb_intfc_descr.bInterfaceSubClass);
796 			break;
797 		}
798 
799 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
800 		    "unsupported subclass = %x",
801 		    scsa2usbp->scsa2usb_intfc_descr.bInterfaceSubClass);
802 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
803 
804 		goto fail;
805 	}
806 
807 	/* check that we have the right set of endpoint descriptors */
808 	if (SCSA2USB_IS_BULK_ONLY(scsa2usbp) || SCSA2USB_IS_CB(scsa2usbp)) {
809 		if ((scsa2usbp->scsa2usb_bulkout_ept.bLength == 0) ||
810 		    (scsa2usbp->scsa2usb_bulkin_ept.bLength == 0)) {
811 			ept_check = B_FALSE;
812 		}
813 	} else if (SCSA2USB_IS_CBI(scsa2usbp)) {
814 		if ((scsa2usbp->scsa2usb_bulkout_ept.bLength == 0) ||
815 		    (scsa2usbp->scsa2usb_bulkin_ept.bLength == 0) ||
816 		    (scsa2usbp->scsa2usb_intr_ept.bLength == 0)) {
817 			ept_check = B_FALSE;
818 		}
819 	}
820 
821 	if (ept_check == B_FALSE) {
822 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
823 		    "scsa2usb%d doesn't support minimum required endpoints",
824 		    instance);
825 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
826 
827 		goto fail;
828 	}
829 
830 	/*
831 	 * Validate the black-listed attributes
832 	 */
833 	scsa2usb_validate_attrs(scsa2usbp);
834 
835 	/* Print the serial number from the registration data */
836 	if (scsa2usbp->scsa2usb_dev_data->dev_serial) {
837 		USB_DPRINTF_L4(DPRINT_MASK_SCSA,
838 		    scsa2usbp->scsa2usb_log_handle, "Serial Number = %s",
839 		    scsa2usbp->scsa2usb_dev_data->dev_serial);
840 	}
841 
842 	/*
843 	 * Allocate a SCSA transport structure
844 	 */
845 	tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
846 	scsa2usbp->scsa2usb_tran = tran;
847 
848 	/*
849 	 * initialize transport structure
850 	 */
851 	tran->tran_hba_private		= scsa2usbp;
852 	tran->tran_tgt_private		= NULL;
853 	tran->tran_tgt_init		= scsa2usb_scsi_tgt_init;
854 	tran->tran_tgt_probe		= scsa2usb_scsi_tgt_probe;
855 	tran->tran_tgt_free		= scsa2usb_scsi_tgt_free;
856 	tran->tran_start		= scsa2usb_scsi_start;
857 	tran->tran_abort		= scsa2usb_scsi_abort;
858 	tran->tran_reset		= scsa2usb_scsi_reset;
859 	tran->tran_getcap		= scsa2usb_scsi_getcap;
860 	tran->tran_setcap		= scsa2usb_scsi_setcap;
861 	tran->tran_init_pkt		= scsa2usb_scsi_init_pkt;
862 	tran->tran_destroy_pkt		= scsa2usb_scsi_destroy_pkt;
863 	tran->tran_dmafree		= NULL;
864 	tran->tran_sync_pkt		= NULL;
865 	tran->tran_reset_notify		= NULL;
866 	tran->tran_get_bus_addr		= NULL;
867 	tran->tran_get_name		= NULL;
868 	tran->tran_quiesce		= NULL;
869 	tran->tran_unquiesce		= NULL;
870 	tran->tran_bus_reset		= NULL;
871 	tran->tran_add_eventcall	= NULL;
872 	tran->tran_get_eventcookie	= NULL;
873 	tran->tran_post_event		= NULL;
874 	tran->tran_remove_eventcall	= NULL;
875 	tran->tran_bus_config		= scsa2usb_scsi_bus_config;
876 	tran->tran_bus_unconfig		= scsa2usb_scsi_bus_unconfig;
877 
878 	/*
879 	 * register with SCSA as an HBA
880 	 * Note that the dma attributes are from parent nexus
881 	 */
882 	if (scsi_hba_attach_setup(dip, usba_get_hc_dma_attr(dip), tran, 0)) {
883 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
884 		    "scsi_hba_attach_setup failed");
885 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
886 
887 		goto fail;
888 	}
889 
890 	scsa2usbp->scsa2usb_flags |= SCSA2USB_FLAGS_HBA_ATTACH_SETUP;
891 
892 	/* create minor node */
893 	if (ddi_create_minor_node(dip, "scsa2usb", S_IFCHR,
894 	    instance << SCSA2USB_MINOR_INSTANCE_SHIFT,
895 	    DDI_NT_SCSI_NEXUS, 0) != DDI_SUCCESS) {
896 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
897 		    "scsi_attach: ddi_create_minor_node failed");
898 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
899 
900 		goto fail;
901 	}
902 
903 	/* open pipes and set scsa2usb_flags */
904 	if (scsa2usb_open_usb_pipes(scsa2usbp) == USB_FAILURE) {
905 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
906 		    "error opening pipes");
907 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
908 
909 		goto fail;
910 	}
911 
912 	/* set default block size. updated after read cap cmd */
913 	for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
914 		scsa2usbp->scsa2usb_lbasize[lun] = DEV_BSIZE;
915 	}
916 
917 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
918 
919 	/* initialize PANIC callback */
920 	scsa2usb_panic_callb_init(scsa2usbp);
921 
922 	/* finally we are all done 'initializing' the device */
923 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
924 	scsa2usbp->scsa2usb_dev_state = USB_DEV_ONLINE;
925 
926 	/* enable PM, mutex needs to be held across this */
927 	scsa2usb_create_pm_components(dip, scsa2usbp);
928 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
929 
930 	/* register for connect/disconnect events */
931 	if (usb_register_event_cbs(scsa2usbp->scsa2usb_dip, &scsa2usb_events,
932 	    0) != USB_SUCCESS) {
933 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle,
934 		    "error cb registering");
935 		goto fail;
936 	}
937 
938 	/* free the dev_data tree, we no longer need it */
939 	usb_free_descr_tree(dip, dev_data);
940 
941 	scsa2usb_pm_idle_component(scsa2usbp);
942 
943 	/* log the conf file override string if there is one */
944 	if (scsa2usbp->scsa2usb_override_str) {
945 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
946 		    "scsa2usb.conf override: %s",
947 		    scsa2usbp->scsa2usb_override_str);
948 	}
949 
950 	if (usb_owns_device(dip)) {
951 		/* get a ugen handle */
952 		bzero(&usb_ugen_info, sizeof (usb_ugen_info));
953 		usb_ugen_info.usb_ugen_flags = 0;
954 		usb_ugen_info.usb_ugen_minor_node_ugen_bits_mask =
955 		    (dev_t)SCSA2USB_MINOR_UGEN_BITS_MASK;
956 		usb_ugen_info.usb_ugen_minor_node_instance_mask =
957 		    (dev_t)~SCSA2USB_MINOR_UGEN_BITS_MASK;
958 		scsa2usbp->scsa2usb_ugen_hdl =
959 		    usb_ugen_get_hdl(dip, &usb_ugen_info);
960 
961 		if (usb_ugen_attach(scsa2usbp->scsa2usb_ugen_hdl, cmd) !=
962 		    USB_SUCCESS) {
963 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
964 			    scsa2usbp->scsa2usb_log_handle,
965 			    "usb_ugen_attach failed");
966 
967 			usb_ugen_release_hdl(scsa2usbp->scsa2usb_ugen_hdl);
968 			scsa2usbp->scsa2usb_ugen_hdl = NULL;
969 		}
970 	}
971 
972 	/* report device */
973 	ddi_report_dev(dip);
974 
975 	return (DDI_SUCCESS);
976 
977 fail:
978 	if (scsa2usbp) {
979 		(void) scsa2usb_cleanup(dip, scsa2usbp);
980 	}
981 
982 	return (DDI_FAILURE);
983 }
984 
985 
986 /*
987  * scsa2usb_detach:
988  *	detach or suspend driver instance
989  */
990 static int
991 scsa2usb_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
992 {
993 	scsi_hba_tran_t	*tran;
994 	scsa2usb_state_t *scsa2usbp;
995 	int rval;
996 
997 	tran = ddi_get_driver_private(dip);
998 	ASSERT(tran != NULL);
999 
1000 	scsa2usbp = (scsa2usb_state_t *)tran->tran_hba_private;
1001 	ASSERT(scsa2usbp);
1002 
1003 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1004 	    "scsa2usb_detach: dip = 0x%p, cmd = %d", (void *)dip, cmd);
1005 
1006 	switch (cmd) {
1007 	case DDI_DETACH:
1008 
1009 		if (scsa2usb_cleanup(dip, scsa2usbp) != USB_SUCCESS) {
1010 
1011 			return (DDI_FAILURE);
1012 		}
1013 
1014 		return (DDI_SUCCESS);
1015 	case DDI_SUSPEND:
1016 		rval = scsa2usb_cpr_suspend(dip);
1017 
1018 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
1019 	default:
1020 
1021 		return (DDI_FAILURE);
1022 	}
1023 }
1024 
1025 /*
1026  * 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 			unsigned int bufsize;
3093 			int count;
3094 
3095 			if (cmd->cmd_cdb[1] & evpd)
3096 				return (SCSA2USB_REJECT);
3097 
3098 			scsa2usb_fake_inquiry(scsa2usbp, inq);
3099 
3100 			/* Copy no more than requested */
3101 			count = MIN(cmd->cmd_bp->b_bcount,
3102 			    sizeof (struct scsi_inquiry));
3103 			bufsize = cmd->cmd_pkt->pkt_cdbp[4];
3104 			count = MIN(count, bufsize);
3105 			bcopy(inq, cmd->cmd_bp->b_un.b_addr, count);
3106 
3107 			cmd->cmd_pkt->pkt_resid = bufsize - count;
3108 			cmd->cmd_pkt->pkt_state |= STATE_XFERRED_DATA;
3109 
3110 			return (SCSA2USB_JUST_ACCEPT);
3111 		}
3112 		break;
3113 
3114 	/*
3115 	 * Fake accepting the following  Opcodes
3116 	 * (as most drives don't support these)
3117 	 * These are needed by format command.
3118 	 */
3119 	case SCMD_RESERVE:
3120 	case SCMD_RELEASE:
3121 	case SCMD_PERSISTENT_RESERVE_IN:
3122 	case SCMD_PERSISTENT_RESERVE_OUT:
3123 
3124 		return (SCSA2USB_JUST_ACCEPT);
3125 
3126 	case SCMD_MODE_SENSE:
3127 	case SCMD_MODE_SELECT:
3128 	case SCMD_MODE_SENSE_G1:
3129 	case SCMD_MODE_SELECT_G1:
3130 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_MODE_SENSE)) {
3131 			if (cmd->cmd_bp) {
3132 				cmd->cmd_pkt->pkt_resid = cmd->cmd_bp->
3133 				    b_bcount;
3134 			}
3135 			scsa2usb_force_invalid_request(scsa2usbp, cmd);
3136 
3137 			return (SCSA2USB_JUST_ACCEPT);
3138 		}
3139 
3140 		break;
3141 	default:
3142 
3143 		break;
3144 	}
3145 
3146 	return (SCSA2USB_TRANSPORT);
3147 }
3148 
3149 
3150 /*
3151  * scsa2usb_handle_scsi_cmd_sub_class:
3152  *	prepare a scsi cmd
3153  *	returns SCSA2USB_TRANSPORT, SCSA2USB_REJECT, SCSA2USB_JUST_ACCEPT
3154  */
3155 int
3156 scsa2usb_handle_scsi_cmd_sub_class(scsa2usb_state_t *scsa2usbp,
3157     scsa2usb_cmd_t *cmd, struct scsi_pkt *pkt)
3158 {
3159 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3160 	    "scsa2usb_handle_scsi_cmd_sub_class: cmd = 0x%p pkt = 0x%p",
3161 	    (void *)cmd, (void *)pkt);
3162 
3163 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3164 
3165 	bzero(&cmd->cmd_cdb, SCSI_CDB_SIZE);
3166 	cmd->cmd_cdb[SCSA2USB_OPCODE] = pkt->pkt_cdbp[0];   /* Set the opcode */
3167 	cmd->cmd_cdb[SCSA2USB_LUN] = pkt->pkt_cdbp[1];
3168 
3169 	/*
3170 	 * decode and convert the packet
3171 	 * for most cmds, we can bcopy the cdb
3172 	 */
3173 	switch (pkt->pkt_cdbp[0]) {
3174 	case SCMD_FORMAT:
3175 		/*
3176 		 * SCMD_FORMAT used to limit cmd->cmd_xfercount
3177 		 * to 4 bytes, but this hangs
3178 		 * formatting dvd media using cdrecord (that is,
3179 		 * a SCSI FORMAT UNIT command with a parameter list > 4 bytes)
3180 		 * (bit 4 in cdb1 is the Fmtdata bit)
3181 		 */
3182 		if ((pkt->pkt_cdbp[1] & 0x10) && cmd->cmd_bp) {
3183 			cmd->cmd_xfercount = cmd->cmd_bp->b_bcount;
3184 		} else {
3185 			cmd->cmd_xfercount = 4;
3186 		}
3187 		cmd->cmd_dir = CBW_DIR_OUT;
3188 		cmd->cmd_actual_len = CDB_GROUP0;
3189 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3190 		break;
3191 
3192 	case SCMD_INQUIRY:
3193 		cmd->cmd_dir = CBW_DIR_IN;
3194 		cmd->cmd_actual_len = CDB_GROUP0;
3195 		cmd->cmd_cdb[SCSA2USB_LBA_0] = pkt->pkt_cdbp[2];
3196 		cmd->cmd_cdb[SCSA2USB_LBA_2] = cmd->cmd_xfercount =
3197 		    min(SCSA2USB_MAX_INQ_LEN,
3198 		    cmd->cmd_bp ? cmd->cmd_bp->b_bcount : 0);
3199 		break;
3200 
3201 	case SCMD_READ_CAPACITY:
3202 		cmd->cmd_dir = CBW_DIR_IN;
3203 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3204 		cmd->cmd_xfercount = sizeof (scsa2usb_read_cap_t);
3205 		break;
3206 
3207 	/*
3208 	 * SCMD_READ/SCMD_WRITE are converted to G1 cmds
3209 	 * (as ATAPI devices don't recognize G0 commands)
3210 	 *
3211 	 * SCMD_READ_LONG/SCMD_WRITE_LONG are handled in
3212 	 * scsa2usb_rw_transport() along with other commands.
3213 	 *
3214 	 * USB Host Controllers cannot handle large (read/write)
3215 	 * xfers. We split the large request to chunks of
3216 	 * smaller ones to meet the HCD limitations.
3217 	 */
3218 	case SCMD_READ:
3219 	case SCMD_WRITE:
3220 	case SCMD_READ_G1:
3221 	case SCMD_WRITE_G1:
3222 	case SCMD_READ_G5:
3223 	case SCMD_WRITE_G5:
3224 	case SCMD_READ_LONG:
3225 	case SCMD_WRITE_LONG:
3226 	case SCMD_READ_CD:
3227 		switch (scsa2usbp->
3228 		    scsa2usb_lun_inquiry[pkt->pkt_address.a_lun].
3229 		    inq_dtype & DTYPE_MASK) {
3230 		case DTYPE_DIRECT:
3231 		case DTYPE_RODIRECT:
3232 		case DTYPE_OPTICAL:
3233 			return (scsa2usb_rw_transport(
3234 			    scsa2usbp, pkt));
3235 		default:
3236 			bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3237 			if (cmd->cmd_bp) {
3238 				cmd->cmd_dir =
3239 				    (cmd->cmd_bp->b_flags & B_READ) ?
3240 				    CBW_DIR_IN : CBW_DIR_OUT;
3241 				cmd->cmd_xfercount =
3242 				    cmd->cmd_bp->b_bcount;
3243 			}
3244 			break;
3245 		}
3246 		break;
3247 
3248 	case SCMD_REQUEST_SENSE:
3249 		cmd->cmd_dir = CBW_DIR_IN;
3250 		cmd->cmd_xfercount = pkt->pkt_cdbp[4];
3251 		cmd->cmd_cdb[SCSA2USB_LBA_2] = pkt->pkt_cdbp[4];
3252 		cmd->cmd_actual_len = CDB_GROUP0;
3253 		break;
3254 
3255 	case SCMD_DOORLOCK:
3256 	case SCMD_START_STOP:
3257 	case SCMD_TEST_UNIT_READY:
3258 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3259 		break;
3260 
3261 	/*
3262 	 * Needed by zip protocol to reset the device
3263 	 */
3264 	case SCMD_SDIAG:
3265 	case SCMD_REZERO_UNIT:
3266 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3267 		cmd->cmd_actual_len = CDB_GROUP1;
3268 		break;
3269 
3270 	case SCMD_WRITE_VERIFY:
3271 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3272 		cmd->cmd_dir = CBW_DIR_OUT;
3273 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3274 		cmd->cmd_actual_len = CDB_GROUP1;
3275 		break;
3276 
3277 	/*
3278 	 * Next command does not have a SCSI equivalent as
3279 	 * it is vendor specific.
3280 	 * It was listed in the vendor's ATAPI Zip specs.
3281 	 */
3282 	case SCMD_READ_FORMAT_CAP:
3283 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3284 		cmd->cmd_dir = CBW_DIR_IN;
3285 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3286 		cmd->cmd_actual_len = CDB_GROUP1;
3287 		break;
3288 	case IOMEGA_CMD_CARTRIDGE_PROTECT:
3289 		cmd->cmd_dir = CBW_DIR_OUT;
3290 		cmd->cmd_cdb[SCSA2USB_LBA_2] = pkt->pkt_cdbp[4];
3291 		cmd->cmd_cdb[SCSA2USB_LBA_2] &= ~1;	/* Make it even */
3292 		cmd->cmd_cdb[SCSA2USB_LUN] = pkt->pkt_cdbp[1];
3293 		cmd->cmd_actual_len = CDB_GROUP0;
3294 		cmd->cmd_xfercount = pkt->pkt_cdbp[4]; /* Length of password */
3295 		break;
3296 
3297 	/*
3298 	 * Do not convert SCMD_MODE_SENSE/SELECT to G1 cmds because
3299 	 * the mode header is different as well. USB devices don't
3300 	 * support 0x03 & 0x04 mode pages, which are already obsoleted
3301 	 * by SPC-2 specification.
3302 	 */
3303 	case SCMD_MODE_SENSE:
3304 	case SCMD_MODE_SELECT:
3305 		if ((pkt->pkt_cdbp[2] == SD_MODE_SENSE_PAGE3_CODE) ||
3306 		    (pkt->pkt_cdbp[2] == SD_MODE_SENSE_PAGE4_CODE)) {
3307 			if (cmd->cmd_bp) {
3308 				cmd->cmd_pkt->pkt_resid = cmd->cmd_bp->b_bcount;
3309 			}
3310 			scsa2usb_force_invalid_request(scsa2usbp, cmd);
3311 			return (SCSA2USB_JUST_ACCEPT);
3312 		}
3313 		/* FALLTHROUGH */
3314 
3315 	default:
3316 		/*
3317 		 * an unknown command may be a uscsi cmd which we
3318 		 * should let go thru without mapping
3319 		 */
3320 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3321 		if (cmd->cmd_bp) {
3322 			cmd->cmd_dir = (cmd->cmd_bp->b_flags & B_READ) ?
3323 			    CBW_DIR_IN : CBW_DIR_OUT;
3324 			cmd->cmd_xfercount = cmd->cmd_bp->b_bcount;
3325 		}
3326 
3327 		break;
3328 	} /* end of switch */
3329 
3330 	USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3331 	    "scsa2usb_handle_scsi_cmd_sub_class: opcode = 0x%x count = 0x%lx",
3332 	    pkt->pkt_cdbp[SCSA2USB_OPCODE], cmd->cmd_xfercount);
3333 
3334 	cmd->cmd_total_xfercount = cmd->cmd_xfercount;
3335 
3336 	return (SCSA2USB_TRANSPORT);
3337 }
3338 
3339 
3340 /*
3341  * scsa2usb_do_tur is performed before READ CAPACITY command is issued.
3342  * It returns media status, 0 for media ready, -1 for media not ready
3343  * or other errors.
3344  */
3345 static int
3346 scsa2usb_do_tur(scsa2usb_state_t *scsa2usbp, struct scsi_address *ap)
3347 {
3348 	struct scsi_pkt		*pkt;
3349 	scsa2usb_cmd_t		*turcmd;
3350 	int			rval = -1;
3351 
3352 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3353 	    "scsa2usb_do_tur:");
3354 
3355 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3356 
3357 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
3358 	if ((pkt = scsi_init_pkt(ap, NULL, NULL, CDB_GROUP0, 1,
3359 	    PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL)) == NULL) {
3360 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
3361 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
3362 		    scsa2usbp->scsa2usb_log_handle,
3363 		    "scsa2usb_do_tur: init pkt failed");
3364 
3365 		return (rval);
3366 	}
3367 
3368 	RQ_MAKECOM_G0(pkt, FLAG_HEAD | FLAG_NODISCON,
3369 	    (char)SCMD_TEST_UNIT_READY, 0, 0);
3370 
3371 	pkt->pkt_comp = NULL;
3372 	pkt->pkt_time = PKT_DEFAULT_TIMEOUT;
3373 	turcmd = PKT2CMD(pkt);
3374 
3375 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
3376 	scsa2usb_prepare_pkt(scsa2usbp, turcmd->cmd_pkt);
3377 
3378 	if (scsa2usb_cmd_transport(scsa2usbp, turcmd) != TRAN_ACCEPT) {
3379 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
3380 		    scsa2usbp->scsa2usb_log_handle,
3381 		    "scsa2usb_do_tur: cmd transport failed, "
3382 		    "pkt_reason=0x%x", turcmd->cmd_pkt->pkt_reason);
3383 	} else if (*(turcmd->cmd_pkt->pkt_scbp) != STATUS_GOOD) {
3384 		/*
3385 		 * Theoretically, the sense data should be retrieved and
3386 		 * sense key be checked when check condition happens. If
3387 		 * the sense key is UNIT ATTENTION, TEST UNIT READY cmd
3388 		 * needs to be sent again to clear the UNIT ATTENTION and
3389 		 * another TUR to be sent to get the real media status.
3390 		 * But the AMI virtual floppy device simply cannot recover
3391 		 * from UNIT ATTENTION by re-sending a TUR cmd, so it
3392 		 * doesn't make any difference whether to check sense key
3393 		 * or not. Just ignore sense key checking here and assume
3394 		 * the device is NOT READY.
3395 		 */
3396 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
3397 		    scsa2usbp->scsa2usb_log_handle,
3398 		    "scsa2usb_do_tur: media not ready");
3399 	} else {
3400 		rval = 0;
3401 	}
3402 
3403 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
3404 	scsi_destroy_pkt(pkt);
3405 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
3406 
3407 	return (rval);
3408 }
3409 
3410 
3411 /*
3412  * scsa2usb_check_ufi_blacklist_attrs:
3413  *	validate "scsa2usb_blacklist_attrs" (see scsa2usb.h)
3414  *	if blacklisted attrs match accept the request
3415  *	attributes checked are:-
3416  *		SCSA2USB_ATTRS_GET_CONF
3417  *		SCSA2USB_ATTRS_GET_PERF
3418  *		SCSA2USB_ATTRS_GET_START_STOP
3419  */
3420 static int
3421 scsa2usb_check_ufi_blacklist_attrs(scsa2usb_state_t *scsa2usbp, uchar_t opcode,
3422     scsa2usb_cmd_t *cmd)
3423 {
3424 	int	rval = SCSA2USB_TRANSPORT;
3425 
3426 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3427 
3428 	switch (opcode) {
3429 	case SCMD_PRIN:
3430 	case SCMD_PROUT:
3431 		rval = SCSA2USB_JUST_ACCEPT;
3432 		break;
3433 	case SCMD_MODE_SENSE:
3434 	case SCMD_MODE_SELECT:
3435 		if (cmd->cmd_bp) {
3436 			cmd->cmd_pkt->pkt_resid = cmd->cmd_bp->b_bcount;
3437 		}
3438 		scsa2usb_force_invalid_request(scsa2usbp, cmd);
3439 		rval = SCSA2USB_JUST_ACCEPT;
3440 		break;
3441 	case SCMD_GET_CONFIGURATION:
3442 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_GET_CONF)) {
3443 			rval = SCSA2USB_JUST_ACCEPT;
3444 		}
3445 		break;
3446 	case SCMD_GET_PERFORMANCE:
3447 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_GET_PERF)) {
3448 			rval = SCSA2USB_JUST_ACCEPT;
3449 		}
3450 		break;
3451 	case SCMD_START_STOP:
3452 		/*
3453 		 * some CB/CBI devices don't have mechanics that spin the
3454 		 * media up and down. So, it doesn't make much sense
3455 		 * to issue this cmd to those devices.
3456 		 */
3457 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_START_STOP)) {
3458 			rval = SCSA2USB_JUST_ACCEPT;
3459 		}
3460 		break;
3461 	case SCMD_READ_CAPACITY:
3462 		/*
3463 		 * Some devices don't support READ CAPACITY command
3464 		 * when media is not ready. Need to check media status
3465 		 * before issuing the cmd to such device.
3466 		 */
3467 		if (!(scsa2usbp->scsa2usb_attrs &
3468 		    SCSA2USB_ATTRS_NO_MEDIA_CHECK)) {
3469 			struct scsi_pkt *pkt = cmd->cmd_pkt;
3470 
3471 			ASSERT(scsa2usbp->scsa2usb_cur_pkt == pkt);
3472 			scsa2usbp->scsa2usb_cur_pkt = NULL;
3473 
3474 			if (scsa2usb_do_tur(scsa2usbp,
3475 			    &pkt->pkt_address) != 0) {
3476 				/* media not ready, force cmd invalid */
3477 				if (cmd->cmd_bp) {
3478 					cmd->cmd_pkt->pkt_resid =
3479 					    cmd->cmd_bp->b_bcount;
3480 				}
3481 				scsa2usb_force_invalid_request(scsa2usbp, cmd);
3482 				rval = SCSA2USB_JUST_ACCEPT;
3483 			}
3484 
3485 			scsa2usbp->scsa2usb_cur_pkt = pkt;
3486 		}
3487 		break;
3488 	default:
3489 		break;
3490 	}
3491 
3492 	return (rval);
3493 }
3494 
3495 
3496 /*
3497  * scsa2usb_handle_ufi_subclass_cmd:
3498  *	prepare a UFI cmd
3499  *	returns SCSA2USB_TRANSPORT, SCSA2USB_REJECT
3500  */
3501 int
3502 scsa2usb_handle_ufi_subclass_cmd(scsa2usb_state_t *scsa2usbp,
3503     scsa2usb_cmd_t *cmd, struct scsi_pkt *pkt)
3504 {
3505 	uchar_t opcode =  pkt->pkt_cdbp[0];
3506 
3507 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3508 	    "scsa2usb_handle_ufi_subclass_cmd: cmd = 0x%p pkt = 0x%p",
3509 	    (void *)cmd, (void *)pkt);
3510 
3511 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3512 
3513 	bzero(&cmd->cmd_cdb, SCSI_CDB_SIZE);
3514 	cmd->cmd_cdb[SCSA2USB_OPCODE] = opcode;   /* Set the opcode */
3515 	cmd->cmd_cdb[SCSA2USB_LUN] = pkt->pkt_cdbp[1];
3516 
3517 	/*
3518 	 * decode and convert the packet if necessary
3519 	 * for most cmds, we can bcopy the cdb
3520 	 */
3521 	switch (opcode) {
3522 	case SCMD_FORMAT:
3523 		/* if parameter list is specified */
3524 		if (pkt->pkt_cdbp[1] & 0x10) {
3525 			cmd->cmd_xfercount =
3526 			    (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3527 			cmd->cmd_dir = USB_EP_DIR_OUT;
3528 			cmd->cmd_actual_len = CDB_GROUP5;
3529 		}
3530 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3531 		break;
3532 	case SCMD_INQUIRY:
3533 		cmd->cmd_dir = USB_EP_DIR_IN;
3534 		cmd->cmd_actual_len = CDB_GROUP0;
3535 		cmd->cmd_cdb[SCSA2USB_LBA_0] = pkt->pkt_cdbp[2];
3536 		cmd->cmd_cdb[SCSA2USB_LBA_2] = cmd->cmd_xfercount =
3537 		    min(SCSA2USB_MAX_INQ_LEN,
3538 		    cmd->cmd_bp ? cmd->cmd_bp->b_bcount : 0);
3539 		break;
3540 	case SCMD_READ_CAPACITY:
3541 		cmd->cmd_dir = USB_EP_DIR_IN;
3542 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3543 		cmd->cmd_xfercount = sizeof (scsa2usb_read_cap_t);
3544 		break;
3545 	case SCMD_REQUEST_SENSE:
3546 		cmd->cmd_dir = USB_EP_DIR_IN;
3547 		cmd->cmd_xfercount = pkt->pkt_cdbp[4];
3548 		cmd->cmd_cdb[SCSA2USB_LBA_2] = pkt->pkt_cdbp[4];
3549 		cmd->cmd_actual_len = CDB_GROUP0;
3550 		break;
3551 
3552 	/*
3553 	 * do not convert SCMD_MODE_SENSE/SELECT because the
3554 	 * mode header is different as well
3555 	 */
3556 
3557 	/*
3558 	 * see usb_bulkonly.c for comments on the next set of commands
3559 	 */
3560 	case SCMD_READ:
3561 	case SCMD_WRITE:
3562 	case SCMD_READ_G1:
3563 	case SCMD_WRITE_G1:
3564 	case SCMD_READ_G5:
3565 	case SCMD_WRITE_G5:
3566 	case SCMD_READ_LONG:
3567 	case SCMD_WRITE_LONG:
3568 	case SCMD_READ_CD:
3569 
3570 		return (scsa2usb_rw_transport(scsa2usbp, pkt));
3571 
3572 	case SCMD_TEST_UNIT_READY:
3573 		/*
3574 		 * Some CB/CBI devices may not support TUR.
3575 		 */
3576 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3577 		break;
3578 	case SCMD_READ_FORMAT_CAP:
3579 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3580 		cmd->cmd_dir = USB_EP_DIR_IN;
3581 		cmd->cmd_actual_len = CDB_GROUP1;
3582 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3583 		break;
3584 	case SCMD_WRITE_VERIFY:
3585 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3586 		cmd->cmd_dir = USB_EP_DIR_OUT;
3587 		cmd->cmd_actual_len = CDB_GROUP1;
3588 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3589 		break;
3590 	case SCMD_START_STOP:
3591 		/* A larger timeout is needed for 'flaky' CD-RW devices */
3592 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_BIG_TIMEOUT)) {
3593 			cmd->cmd_timeout = max(cmd->cmd_timeout,
3594 			    20 * SCSA2USB_BULK_PIPE_TIMEOUT);
3595 		}
3596 		/* FALLTHRU */
3597 	default:
3598 		/*
3599 		 * all other commands don't need special mapping
3600 		 */
3601 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3602 		if (cmd->cmd_bp) {
3603 			cmd->cmd_dir = (cmd->cmd_bp->b_flags & B_READ) ?
3604 			    CBW_DIR_IN : CBW_DIR_OUT;
3605 			cmd->cmd_xfercount = cmd->cmd_bp->b_bcount;
3606 		}
3607 		break;
3608 
3609 	} /* end of switch */
3610 
3611 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3612 	    "scsa2usb_handle_ufi_subclass_cmd: opcode = 0x%x count = 0x%lx",
3613 	    opcode, cmd->cmd_xfercount);
3614 
3615 	cmd->cmd_total_xfercount = cmd->cmd_xfercount;
3616 
3617 	return (SCSA2USB_TRANSPORT);
3618 }
3619 
3620 
3621 /*
3622  * scsa2usb_rw_transport:
3623  *	Handle splitting READ and WRITE requests to the
3624  *	device to a size that the host controller allows.
3625  *
3626  *	returns TRAN_* values and not USB_SUCCESS/FAILURE
3627  *
3628  * To support CD-R/CD-RW/DVD media, we need to support a
3629  * variety of block sizes for the different types of CD
3630  * data (audio, data, video, CD-XA, yellowbook, redbook etc.)
3631  *
3632  * Some of the block sizes used are:- 512, 1k, 2k, 2056, 2336
3633  * 2340, 2352, 2368, 2448, 2646, 2647 etc.
3634  *
3635  * NOTE: the driver could be entertaining a SCSI CDB that uses
3636  * any of the above listed block sizes at a given time, and a
3637  * totally different block size at any other given time for a
3638  * different CDB.
3639  *
3640  * We need to compute block size every time and figure out
3641  * matching LBA and LEN accordingly.
3642  *
3643  * Also UHCI has a limitation that it can only xfer 32k at a
3644  * given time. So, with "odd" sized blocks and a limitation of
3645  * how much we can xfer per shot, we need to compute xfer_count
3646  * as well each time.
3647  *
3648  * The same computation is also done in the function
3649  * scsa2usb_setup_next_xfer().	To save computing block_size in
3650  * this function, I am saving block_size in "cmd" now.
3651  */
3652 int
3653 scsa2usb_rw_transport(scsa2usb_state_t *scsa2usbp, struct scsi_pkt *pkt)
3654 {
3655 	scsa2usb_cmd_t *cmd = PKT2CMD(pkt);
3656 	int lba, dir, opcode;
3657 	struct buf *bp = cmd->cmd_bp;
3658 	size_t len, xfer_count;
3659 	size_t blk_size;	/* calculate the block size to be used */
3660 	int sz;
3661 
3662 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3663 	    "scsa2usb_rw_transport:");
3664 
3665 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3666 
3667 	opcode = pkt->pkt_cdbp[0];
3668 	blk_size  = scsa2usbp->scsa2usb_lbasize[pkt->pkt_address.a_lun];
3669 						/* set to default */
3670 
3671 	switch (opcode) {
3672 	case SCMD_READ:
3673 		/*
3674 		 * Note that READ/WRITE(6) are not supported by the drive.
3675 		 * convert it into a 10 byte read/write.
3676 		 */
3677 		lba = SCSA2USB_LBA_6BYTE(pkt);
3678 		len = SCSA2USB_LEN_6BYTE(pkt);
3679 		opcode = SCMD_READ_G1;	/* Overwrite it w/ byte 10 cmd val */
3680 		dir = USB_EP_DIR_IN;
3681 		break;
3682 	case SCMD_WRITE:
3683 		lba = SCSA2USB_LBA_6BYTE(pkt);
3684 		len = SCSA2USB_LEN_6BYTE(pkt);
3685 		opcode = SCMD_WRITE_G1;	/* Overwrite it w/ byte 10 cmd val */
3686 		dir = USB_EP_DIR_OUT;
3687 		break;
3688 	case SCMD_READ_G1:
3689 	case SCMD_READ_LONG:
3690 		lba = SCSA2USB_LBA_10BYTE(pkt);
3691 		len = SCSA2USB_LEN_10BYTE(pkt);
3692 		dir = USB_EP_DIR_IN;
3693 		break;
3694 	case SCMD_WRITE_G1:
3695 	case SCMD_WRITE_LONG:
3696 		lba = SCSA2USB_LBA_10BYTE(pkt);
3697 		len = SCSA2USB_LEN_10BYTE(pkt);
3698 		dir = USB_EP_DIR_OUT;
3699 		if (len) {
3700 			sz = SCSA2USB_CDRW_BLKSZ(bp ? bp->b_bcount : 0, len);
3701 			if (SCSA2USB_VALID_CDRW_BLKSZ(sz)) {
3702 				blk_size = sz;	/* change it accordingly */
3703 			}
3704 		}
3705 		break;
3706 	case SCMD_READ_CD:
3707 		lba = SCSA2USB_LBA_10BYTE(pkt);
3708 		len = SCSA2USB_LEN_READ_CD(pkt);
3709 		dir = USB_EP_DIR_IN;
3710 
3711 		/* Figure out the block size */
3712 		blk_size = scsa2usb_read_cd_blk_size(pkt->pkt_cdbp[1] >> 2);
3713 		break;
3714 	case SCMD_READ_G5:
3715 		lba = SCSA2USB_LBA_12BYTE(pkt);
3716 		len = SCSA2USB_LEN_12BYTE(pkt);
3717 		dir = USB_EP_DIR_IN;
3718 		break;
3719 	case SCMD_WRITE_G5:
3720 		lba = SCSA2USB_LBA_12BYTE(pkt);
3721 		len = SCSA2USB_LEN_12BYTE(pkt);
3722 		dir = USB_EP_DIR_OUT;
3723 		break;
3724 	}
3725 
3726 	cmd->cmd_total_xfercount = xfer_count = len * blk_size;
3727 
3728 	/* reduce xfer count if necessary */
3729 	if (blk_size &&
3730 	    (xfer_count > scsa2usbp->scsa2usb_max_bulk_xfer_size)) {
3731 		/*
3732 		 * For CD-RW devices reduce the xfer count based
3733 		 * on the block size used by these devices. The
3734 		 * block size could change for READ_CD and WRITE
3735 		 * opcodes.
3736 		 *
3737 		 * Also as UHCI allows a max xfer of 32k at a time;
3738 		 * compute the xfer_count based on the new block_size.
3739 		 *
3740 		 * The len part of the cdb changes as a result of that.
3741 		 */
3742 		if (SCSA2USB_VALID_CDRW_BLKSZ(blk_size)) {
3743 			xfer_count = ((scsa2usbp->scsa2usb_max_bulk_xfer_size/
3744 			    blk_size) * blk_size);
3745 			len = xfer_count/blk_size;
3746 			xfer_count = blk_size * len;
3747 		} else {
3748 			xfer_count = scsa2usbp->scsa2usb_max_bulk_xfer_size;
3749 			len = xfer_count/blk_size;
3750 		}
3751 	}
3752 
3753 	cmd->cmd_xfercount = xfer_count;
3754 	cmd->cmd_dir = (uchar_t)dir;
3755 	cmd->cmd_blksize = (int)blk_size;
3756 
3757 	/*
3758 	 * Having figure out the 'partial' xfer len based on he
3759 	 * block size; fill it in to the cmd->cmd_cdb
3760 	 */
3761 	cmd->cmd_cdb[SCSA2USB_OPCODE] = (uchar_t)opcode;
3762 	switch (opcode) {
3763 	case SCMD_READ_CD:
3764 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3765 		scsa2usb_fill_up_ReadCD_cdb_len(cmd, len, CDB_GROUP5);
3766 		break;
3767 	case SCMD_WRITE_G5:
3768 	case SCMD_READ_G5:
3769 		scsa2usb_fill_up_12byte_cdb_len(cmd, len, CDB_GROUP5);
3770 		break;
3771 	default:
3772 		scsa2usb_fill_up_cdb_len(cmd, len);
3773 		cmd->cmd_actual_len = CDB_GROUP1;
3774 		break;
3775 	}
3776 
3777 	scsa2usb_fill_up_cdb_lba(cmd, lba);
3778 
3779 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3780 	    "bcount=0x%lx lba=0x%x len=0x%lx xfercount=0x%lx total=0x%lx",
3781 	    bp ? bp->b_bcount : 0, lba, len, cmd->cmd_xfercount,
3782 	    cmd->cmd_total_xfercount);
3783 
3784 	/* Set the timeout value as per command request */
3785 	if ((opcode == SCMD_WRITE_G1) && SCSA2USB_VALID_CDRW_BLKSZ(blk_size)) {
3786 		/*
3787 		 * We increase the time as CD-RW writes have two things
3788 		 * to do. After writing out the data to the media, a
3789 		 * TOC needs to be filled up at the beginning of the media
3790 		 * This is when the write gets "finalized".
3791 		 * Hence the actual write could take longer than the
3792 		 * value specified in cmd->cmd_timeout.
3793 		 */
3794 		cmd->cmd_timeout *= 4;
3795 
3796 		USB_DPRINTF_L4(DPRINT_MASK_SCSA,
3797 		    scsa2usbp->scsa2usb_log_handle,
3798 		    "new timeout value = 0x%x", cmd->cmd_timeout);
3799 	}
3800 
3801 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3802 	    "lba 0x%x len 0x%lx xfercount 0x%lx total 0x%lx",
3803 	    lba, len, cmd->cmd_xfercount, cmd->cmd_total_xfercount);
3804 
3805 	return (SCSA2USB_TRANSPORT);
3806 }
3807 
3808 
3809 /*
3810  * scsa2usb_setup_next_xfer:
3811  *	For READs and WRITEs we split up the transfer in terms of
3812  *	HCD understood units. This function handles the split transfers.
3813  *
3814  * See comments in the previous function scsa2usb_rw_transport
3815  *
3816  * The lba computation was being done based on scsa2usb_max_bulk_xfer_size
3817  * earlier. With CD-RW devices, the xfer_count and the block_size may
3818  * no longer be a multiple of scsa2usb_max_bulk_xfer_size. So compute
3819  * xfer_count all over again. Adjust lba, based on the previous requests'
3820  * len. Find out the len and add it to cmd->cmd_lba to get the new lba
3821  */
3822 void
3823 scsa2usb_setup_next_xfer(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd)
3824 {
3825 	int xfer_len = min(scsa2usbp->scsa2usb_max_bulk_xfer_size,
3826 	    cmd->cmd_total_xfercount);
3827 	int cdb_len;
3828 	size_t blk_size;
3829 
3830 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3831 
3832 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3833 	    "scsa2usb_setup_next_xfer: opcode = 0x%x lba = 0x%x "
3834 	    "total count = 0x%lx", cmd->cmd_cdb[SCSA2USB_OPCODE],
3835 	    cmd->cmd_lba, cmd->cmd_total_xfercount);
3836 
3837 	ASSERT(cmd->cmd_total_xfercount > 0);
3838 	cmd->cmd_xfercount = xfer_len;
3839 	blk_size = scsa2usbp->scsa2usb_lbasize[
3840 	    cmd->cmd_pkt->pkt_address.a_lun];
3841 
3842 	/*
3843 	 * For CD-RW devices reduce the xfer count based on the
3844 	 * block_size used by these devices. See changes below
3845 	 * where xfer_count is being adjusted.
3846 	 *
3847 	 * Also adjust len/lba based on the block_size and xfer_count.
3848 	 * NOTE: Always calculate lba first, as it based on previous
3849 	 * commands' values.
3850 	 */
3851 	switch (cmd->cmd_cdb[SCSA2USB_OPCODE]) {
3852 	case SCMD_READ_CD:
3853 		/* calculate lba = current_lba + len_of_prev_cmd */
3854 		cmd->cmd_lba += (cmd->cmd_cdb[6] << 16) +
3855 		    (cmd->cmd_cdb[7] << 8) + cmd->cmd_cdb[8];
3856 		cdb_len = xfer_len/cmd->cmd_blksize;
3857 		cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_2] = (uchar_t)cdb_len;
3858 		/* re-adjust xfer count */
3859 		cmd->cmd_xfercount = cdb_len * cmd->cmd_blksize;
3860 		break;
3861 	case SCMD_WRITE_G5:
3862 	case SCMD_READ_G5:
3863 		/* calculate lba = current_lba + len_of_prev_cmd */
3864 		cmd->cmd_lba += (cmd->cmd_cdb[6] << 24) +
3865 		    (cmd->cmd_cdb[7] << 16) + (cmd->cmd_cdb[8] << 8) +
3866 		    cmd->cmd_cdb[9];
3867 		if (blk_size) {
3868 			xfer_len /= blk_size;
3869 		}
3870 		scsa2usb_fill_up_12byte_cdb_len(cmd, xfer_len, CDB_GROUP5);
3871 		break;
3872 	case SCMD_WRITE_G1:
3873 	case SCMD_WRITE_LONG:
3874 		/* calculate lba = current_lba + len_of_prev_cmd */
3875 		cmd->cmd_lba += (cmd->cmd_cdb[7] << 8) + cmd->cmd_cdb[8];
3876 		if (SCSA2USB_VALID_CDRW_BLKSZ(cmd->cmd_blksize)) {
3877 			blk_size = cmd->cmd_blksize;
3878 		}
3879 		cdb_len = xfer_len/blk_size;
3880 		scsa2usb_fill_up_cdb_len(cmd, cdb_len);
3881 		/* re-adjust xfer count */
3882 		cmd->cmd_xfercount = cdb_len * blk_size;
3883 		break;
3884 	default:
3885 		if (blk_size) {
3886 			xfer_len /= blk_size;
3887 		}
3888 		scsa2usb_fill_up_cdb_len(cmd, xfer_len);
3889 		cmd->cmd_lba += scsa2usbp->scsa2usb_max_bulk_xfer_size/blk_size;
3890 	}
3891 
3892 	/* fill in the lba */
3893 	scsa2usb_fill_up_cdb_lba(cmd, cmd->cmd_lba);
3894 
3895 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3896 	    "scsa2usb_setup_next_xfer:\n\tlba = 0x%x xfer_len = 0x%x "
3897 	    "xfercount = 0x%lx total = 0x%lx", cmd->cmd_lba, xfer_len,
3898 	    cmd->cmd_xfercount, cmd->cmd_total_xfercount);
3899 }
3900 
3901 
3902 /*
3903  * take one request from the lun's waitQ and transport it
3904  */
3905 static void
3906 scsa2usb_transport_request(scsa2usb_state_t *scsa2usbp, uint_t lun)
3907 {
3908 	int			rval;
3909 	struct scsi_pkt		*pkt;
3910 	struct scsa2usb_cmd	*cmd, *arqcmd;
3911 
3912 	if ((cmd = (scsa2usb_cmd_t *)
3913 	    usba_rm_first_pvt_from_list(
3914 	    &scsa2usbp->scsa2usb_waitQ[lun])) == NULL) {
3915 
3916 		return;
3917 	}
3918 	pkt = cmd->cmd_pkt;
3919 
3920 	/*
3921 	 * if device has been disconnected, just complete it
3922 	 */
3923 	if (scsa2usbp->scsa2usb_dev_state == USB_DEV_DISCONNECTED) {
3924 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3925 		    "device not accessible");
3926 		pkt->pkt_reason = CMD_DEV_GONE;
3927 		SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
3928 		scsa2usb_pkt_completion(scsa2usbp, pkt);
3929 
3930 		return;
3931 	}
3932 
3933 	USB_DPRINTF_L4(DPRINT_MASK_SCSA,
3934 	    scsa2usbp->scsa2usb_log_handle,
3935 	    "scsa2usb_transport_request: cmd=0x%p bp=0x%p addr=0x%p",
3936 	    (void *)cmd, (void *)cmd->cmd_bp,
3937 	    (void *)(cmd->cmd_bp ? cmd->cmd_bp->b_un.b_addr : NULL));
3938 
3939 	rval = scsa2usb_cmd_transport(scsa2usbp, cmd);
3940 
3941 	USB_DPRINTF_L3(DPRINT_MASK_SCSA,
3942 	    scsa2usbp->scsa2usb_log_handle,
3943 	    "scsa2usb_transport_request: transport rval = %d",
3944 	    rval);
3945 
3946 	if (scsa2usbp->scsa2usb_cur_pkt == NULL) {
3947 
3948 		return;
3949 	}
3950 
3951 	ASSERT(pkt == scsa2usbp->scsa2usb_cur_pkt);
3952 
3953 	if (ddi_in_panic()) {
3954 		pkt->pkt_reason = CMD_CMPLT;
3955 		scsa2usb_pkt_completion(scsa2usbp, pkt);
3956 
3957 		return;
3958 	}
3959 
3960 	/*
3961 	 * start an auto-request sense iff
3962 	 * there was a check condition, we have enough
3963 	 * space in the status block, and we have not
3964 	 * faked an auto request sense
3965 	 */
3966 	if ((*(pkt->pkt_scbp) == STATUS_CHECK) &&
3967 	    (cmd->cmd_scblen >= sizeof (struct scsi_arq_status)) &&
3968 	    ((pkt->pkt_state & STATE_ARQ_DONE) == 0) &&
3969 	    (scsa2usb_create_arq_pkt(scsa2usbp,
3970 	    &pkt->pkt_address) == USB_SUCCESS)) {
3971 		arqcmd = scsa2usbp->scsa2usb_arq_cmd;
3972 
3973 		/*
3974 		 * copy the timeout from the
3975 		 * original packet
3976 		 * for lack of a better value
3977 		 */
3978 		arqcmd->cmd_pkt->pkt_time = pkt->pkt_time;
3979 		scsa2usb_prepare_pkt(scsa2usbp,
3980 		    arqcmd->cmd_pkt);
3981 
3982 		scsa2usbp->scsa2usb_cur_pkt = NULL;
3983 		if (scsa2usb_cmd_transport(
3984 		    scsa2usbp, arqcmd) == TRAN_ACCEPT) {
3985 
3986 			/* finish w/ this packet */
3987 			scsa2usb_complete_arq_pkt(
3988 			    scsa2usbp, arqcmd->cmd_pkt, cmd,
3989 			    scsa2usbp->scsa2usb_arq_bp);
3990 
3991 			/*
3992 			 * we have valid request sense
3993 			 * data so clear the pkt_reason
3994 			 */
3995 			pkt->pkt_reason = CMD_CMPLT;
3996 		}
3997 		scsa2usbp->scsa2usb_cur_pkt = pkt;
3998 		scsa2usb_delete_arq_pkt(scsa2usbp);
3999 	}
4000 
4001 	if ((rval != TRAN_ACCEPT) &&
4002 	    (pkt->pkt_reason == CMD_CMPLT)) {
4003 		pkt->pkt_reason = CMD_TRAN_ERR;
4004 	}
4005 
4006 	SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
4007 	scsa2usb_pkt_completion(scsa2usbp, pkt);
4008 
4009 	ASSERT(scsa2usbp->scsa2usb_cur_pkt == NULL);
4010 }
4011 
4012 
4013 /*
4014  * scsa2usb_work_thread:
4015  *	The taskq thread that kicks off the transport (BO and CB/CBI)
4016  */
4017 static void
4018 scsa2usb_work_thread(void *arg)
4019 {
4020 	scsa2usb_state_t	*scsa2usbp = (scsa2usb_state_t *)arg;
4021 	uint_t			lun;
4022 	uint_t			count;
4023 
4024 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4025 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4026 	    "scsa2usb_work_thread start: thread_id=0x%p",
4027 	    (void *)scsa2usbp->scsa2usb_work_thread_id);
4028 
4029 	ASSERT(scsa2usbp->scsa2usb_work_thread_id == (kthread_t *)1);
4030 	scsa2usbp->scsa2usb_work_thread_id = curthread;
4031 
4032 	/* exclude ugen accesses */
4033 	while (scsa2usbp->scsa2usb_transport_busy) {
4034 		cv_wait(&scsa2usbp->scsa2usb_transport_busy_cv,
4035 		    &scsa2usbp->scsa2usb_mutex);
4036 	}
4037 	ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0);
4038 	scsa2usbp->scsa2usb_transport_busy++;
4039 	scsa2usbp->scsa2usb_busy_thread = curthread;
4040 
4041 	scsa2usb_raise_power(scsa2usbp);
4042 
4043 	/* reopen the pipes if necessary */
4044 	(void) scsa2usb_open_usb_pipes(scsa2usbp);
4045 
4046 	for (;;) {
4047 		ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0);
4048 		for (lun = 0; lun < scsa2usbp->scsa2usb_n_luns; lun++) {
4049 			scsa2usb_transport_request(scsa2usbp, lun);
4050 		}
4051 		count = 0;
4052 		for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
4053 			count += usba_list_entry_count(
4054 			    &scsa2usbp->scsa2usb_waitQ[lun]);
4055 		}
4056 
4057 		if (count == 0) {
4058 
4059 			break;
4060 		}
4061 	}
4062 
4063 	scsa2usbp->scsa2usb_work_thread_id = 0;
4064 
4065 	ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0);
4066 
4067 	scsa2usbp->scsa2usb_transport_busy--;
4068 	scsa2usbp->scsa2usb_busy_thread = NULL;
4069 	cv_signal(&scsa2usbp->scsa2usb_transport_busy_cv);
4070 
4071 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4072 	    "scsa2usb_work_thread: exit");
4073 
4074 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4075 
4076 	scsa2usb_pm_idle_component(scsa2usbp);
4077 }
4078 
4079 
4080 /*
4081  * scsa2usb_flush_waitQ:
4082  *	empties the entire waitQ with errors asap.
4083  *
4084  * It is called from scsa2usb_scsi_reset and scsa2usb_panic_callb.
4085  * If the device is reset; we should empty the waitQ right away.
4086  * If the system has paniced; we should empty the waitQ right away.
4087  *
4088  * CPR suspend will only succeed if device is idle. No need to call
4089  * this function for CPR suspend case.
4090  */
4091 static void
4092 scsa2usb_flush_waitQ(scsa2usb_state_t *scsa2usbp, uint_t lun,
4093     uchar_t error)
4094 {
4095 	struct scsi_pkt		*pkt;
4096 	struct scsa2usb_cmd	*cmd;
4097 	usba_list_entry_t	head;
4098 
4099 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4100 
4101 	usba_move_list(&scsa2usbp->scsa2usb_waitQ[lun], &head,
4102 	    scsa2usbp->scsa2usb_dev_data->dev_iblock_cookie);
4103 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4104 
4105 	while ((cmd = (scsa2usb_cmd_t *)usba_rm_first_pvt_from_list(&head)) !=
4106 	    NULL) {
4107 		pkt = cmd->cmd_pkt;
4108 		pkt->pkt_reason = error;	/* set error */
4109 
4110 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4111 		scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_DO_COMP;
4112 		scsa2usb_pkt_completion(scsa2usbp, pkt);
4113 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4114 	} /* end of while */
4115 }
4116 
4117 
4118 /*
4119  * scsa2usb_do_inquiry is performed before INIT CHILD and we have
4120  * to fake a few things normally done by SCSA
4121  */
4122 static void
4123 scsa2usb_do_inquiry(scsa2usb_state_t *scsa2usbp, uint_t target, uint_t lun)
4124 {
4125 	struct buf	*bp;
4126 	struct scsi_pkt *pkt;
4127 	struct scsi_address ap;
4128 	int		len = SCSA2USB_MAX_INQ_LEN;
4129 
4130 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4131 	    "scsa2usb_do_inquiry: %d bytes", len);
4132 
4133 	/* is it inquiry-challenged? */
4134 	if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_INQUIRY)) {
4135 		scsa2usb_fake_inquiry(scsa2usbp,
4136 		    &scsa2usbp->scsa2usb_lun_inquiry[lun]);
4137 		return;
4138 	}
4139 
4140 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4141 
4142 	bzero(&ap, sizeof (struct scsi_address));
4143 	ap.a_hba_tran = scsa2usbp->scsa2usb_tran;
4144 	ap.a_target = (ushort_t)target;
4145 	ap.a_lun = (uchar_t)lun;
4146 
4147 	/* limit inquiry to 36 bytes */
4148 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4149 	if ((bp = scsi_alloc_consistent_buf(&ap, (struct buf *)NULL,
4150 	    len, B_READ, SLEEP_FUNC, NULL)) == NULL) {
4151 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4152 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4153 		    scsa2usbp->scsa2usb_log_handle,
4154 		    "scsa2usb_do_inquiry: failed");
4155 
4156 		return;
4157 	}
4158 
4159 	pkt = scsi_init_pkt(&ap, NULL, bp, CDB_GROUP0, 1,
4160 	    PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL);
4161 
4162 	RQ_MAKECOM_G0(pkt, FLAG_NOINTR, (char)SCMD_INQUIRY, 0, (char)len);
4163 
4164 	pkt->pkt_comp = NULL;
4165 	pkt->pkt_time = 5;
4166 	bzero(bp->b_un.b_addr, len);
4167 
4168 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4169 	    "scsa2usb_do_inquiry:INQUIRY");
4170 
4171 	(void) scsi_transport(pkt);
4172 
4173 	if (pkt->pkt_reason) {
4174 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4175 		    scsa2usbp->scsa2usb_log_handle,
4176 		    "INQUIRY failed, cannot determine device type, "
4177 		    "pkt_reason=0x%x", pkt->pkt_reason);
4178 
4179 		/* not much hope for other cmds, reduce */
4180 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4181 		scsa2usbp->scsa2usb_attrs &=
4182 		    ~SCSA2USB_ATTRS_REDUCED_CMD;
4183 		scsa2usb_fake_inquiry(scsa2usbp,
4184 		    &scsa2usbp->scsa2usb_lun_inquiry[lun]);
4185 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4186 	}
4187 
4188 	scsi_destroy_pkt(pkt);
4189 	scsi_free_consistent_buf(bp);
4190 
4191 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4192 }
4193 
4194 
4195 /*
4196  * scsa2usb_fake_inquiry:
4197  *    build an inquiry for a given device that doesnt like inquiry
4198  *    commands.
4199  */
4200 static void
4201 scsa2usb_fake_inquiry(scsa2usb_state_t *scsa2usbp, struct scsi_inquiry *inqp)
4202 {
4203 	usb_client_dev_data_t *dev_data = scsa2usbp->scsa2usb_dev_data;
4204 	int len;
4205 
4206 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4207 	    "scsa2usb_fake_inquiry:");
4208 
4209 	bzero(inqp, sizeof (struct scsi_inquiry));
4210 	for (len = 0; len < sizeof (inqp->inq_vid); len++) {
4211 		*(inqp->inq_vid + len) = ' ';
4212 	}
4213 
4214 	for (len = 0; len < sizeof (inqp->inq_pid); len++) {
4215 		*(inqp->inq_pid + len) = ' ';
4216 	}
4217 
4218 	inqp->inq_dtype = DTYPE_DIRECT;
4219 	inqp->inq_rmb = 1;
4220 	inqp->inq_ansi = 2;
4221 	inqp->inq_rdf = RDF_SCSI2;
4222 	inqp->inq_len = sizeof (struct scsi_inquiry)-4;
4223 
4224 	/* Fill in the Vendor id/Product id strings */
4225 	if (dev_data->dev_mfg) {
4226 		if ((len = strlen(dev_data->dev_mfg)) >
4227 		    sizeof (inqp->inq_vid)) {
4228 			len = sizeof (inqp->inq_vid);
4229 		}
4230 		bcopy(dev_data->dev_mfg, inqp->inq_vid, len);
4231 	}
4232 
4233 	if (dev_data->dev_product) {
4234 		if ((len = strlen(dev_data->dev_product)) >
4235 		    sizeof (inqp->inq_pid)) {
4236 			len = sizeof (inqp->inq_pid);
4237 		}
4238 		bcopy(dev_data->dev_product, inqp->inq_pid, len);
4239 	}
4240 
4241 	/* Set the Revision to the Device */
4242 	inqp->inq_revision[0] = 0x30 +
4243 	    ((dev_data->dev_descr->bcdDevice>>12) & 0xF);
4244 	inqp->inq_revision[1] = 0x30 +
4245 	    ((dev_data->dev_descr->bcdDevice>>8) & 0xF);
4246 	inqp->inq_revision[2] = 0x30 +
4247 	    ((dev_data->dev_descr->bcdDevice>>4) & 0xF);
4248 	inqp->inq_revision[3] = 0x30 +
4249 	    ((dev_data->dev_descr->bcdDevice) & 0xF);
4250 }
4251 
4252 
4253 /*
4254  * scsa2usb_create_arq_pkt:
4255  *	Create and ARQ packet to get request sense data
4256  */
4257 static int
4258 scsa2usb_create_arq_pkt(scsa2usb_state_t *scsa2usbp, struct scsi_address *ap)
4259 {
4260 	struct buf *bp;
4261 	scsa2usb_cmd_t *arq_cmd;
4262 
4263 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4264 	    "scsa2usb_create_arq_pkt: scsa2usbp: %p, ap: %p",
4265 	    (void *)scsa2usbp, (void *)ap);
4266 
4267 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4268 
4269 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4270 	if ((bp = scsi_alloc_consistent_buf(ap, (struct buf *)NULL,
4271 	    SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL)) == NULL) {
4272 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4273 
4274 		return (USB_FAILURE);
4275 	}
4276 
4277 	arq_cmd = PKT2CMD(scsi_init_pkt(ap, NULL, bp, CDB_GROUP0, 1,
4278 	    PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL));
4279 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4280 
4281 	RQ_MAKECOM_G0(arq_cmd->cmd_pkt,
4282 	    FLAG_SENSING | FLAG_HEAD | FLAG_NODISCON,
4283 	    (char)SCMD_REQUEST_SENSE, 0, (char)SENSE_LENGTH);
4284 
4285 	arq_cmd->cmd_pkt->pkt_ha_private = arq_cmd;
4286 	scsa2usbp->scsa2usb_arq_cmd = arq_cmd;
4287 	scsa2usbp->scsa2usb_arq_bp = bp;
4288 	arq_cmd->cmd_pkt->pkt_comp = NULL;
4289 	bzero(bp->b_un.b_addr, SENSE_LENGTH);
4290 
4291 	return (USB_SUCCESS);
4292 }
4293 
4294 
4295 /*
4296  * scsa2usb_delete_arq_pkt:
4297  *	Destroy the ARQ packet
4298  */
4299 static void
4300 scsa2usb_delete_arq_pkt(scsa2usb_state_t *scsa2usbp)
4301 {
4302 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4303 	    "scsa2usb_delete_arq_pkt: cmd: 0x%p",
4304 	    (void *)scsa2usbp->scsa2usb_arq_cmd);
4305 
4306 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4307 
4308 	if (scsa2usbp->scsa2usb_arq_cmd != NULL) {
4309 		scsi_destroy_pkt(scsa2usbp->scsa2usb_arq_cmd->cmd_pkt);
4310 		scsi_free_consistent_buf(scsa2usbp->scsa2usb_arq_bp);
4311 	}
4312 	scsa2usbp->scsa2usb_arq_cmd = NULL;
4313 	scsa2usbp->scsa2usb_arq_bp = NULL;
4314 }
4315 
4316 
4317 /*
4318  * scsa2usb_complete_arq_pkt:
4319  *	finish processing the arq packet
4320  */
4321 static void
4322 scsa2usb_complete_arq_pkt(scsa2usb_state_t *scsa2usbp,
4323     struct scsi_pkt *pkt, scsa2usb_cmd_t *ssp, struct buf *bp)
4324 {
4325 	scsa2usb_cmd_t		*sp = pkt->pkt_ha_private;
4326 	struct scsi_arq_status	*arqp;
4327 
4328 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4329 
4330 	arqp = (struct scsi_arq_status *)(ssp->cmd_pkt->pkt_scbp);
4331 	arqp->sts_rqpkt_status = *((struct scsi_status *)
4332 	    (sp->cmd_pkt->pkt_scbp));
4333 	arqp->sts_rqpkt_reason = CMD_CMPLT;
4334 	arqp->sts_rqpkt_state |= STATE_XFERRED_DATA;
4335 	arqp->sts_rqpkt_statistics = arqp->sts_rqpkt_resid = 0;
4336 
4337 	/* is this meaningful sense data */
4338 	if (*(bp->b_un.b_addr) != 0) {
4339 		bcopy(bp->b_un.b_addr, &arqp->sts_sensedata, SENSE_LENGTH);
4340 		ssp->cmd_pkt->pkt_state |= STATE_ARQ_DONE;
4341 	}
4342 
4343 	/* we will not sense start cmd until we receive a NOT READY */
4344 	if (arqp->sts_sensedata.es_key == KEY_NOT_READY) {
4345 		scsa2usbp->scsa2usb_rcvd_not_ready = B_TRUE;
4346 	}
4347 }
4348 
4349 
4350 /*
4351  * Miscellaneous functions for any command/transport
4352  */
4353 /*
4354  * scsa2usb_open_usb_pipes:
4355  *	set up a pipe policy
4356  *	open usb bulk pipes (BO and CB/CBI)
4357  *	open usb interrupt pipe (CBI)
4358  */
4359 static int
4360 scsa2usb_open_usb_pipes(scsa2usb_state_t *scsa2usbp)
4361 {
4362 	int			rval;
4363 	usb_pipe_policy_t	policy;	/* bulk pipe policy */
4364 	size_t			sz;
4365 
4366 	ASSERT(scsa2usbp);
4367 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4368 
4369 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4370 	    "scsa2usb_open_usb_pipes: dip = 0x%p flag = 0x%x",
4371 	    (void *)scsa2usbp->scsa2usb_dip, scsa2usbp->scsa2usb_flags);
4372 
4373 	if (!(scsa2usbp->scsa2usb_flags & SCSA2USB_FLAGS_PIPES_OPENED)) {
4374 
4375 		/*
4376 		 * one pipe policy for all bulk pipes
4377 		 */
4378 		bzero(&policy, sizeof (usb_pipe_policy_t));
4379 		/* at least 2, for the normal and exceptional callbacks */
4380 		policy.pp_max_async_reqs = 1;
4381 
4382 		USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4383 		    "scsa2usb_open_usb_pipes: opening bulk pipes");
4384 
4385 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4386 
4387 		/* Open the USB bulk-in pipe */
4388 		if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip,
4389 		    &scsa2usbp->scsa2usb_bulkin_ept, &policy, USB_FLAGS_SLEEP,
4390 		    &scsa2usbp->scsa2usb_bulkin_pipe)) != USB_SUCCESS) {
4391 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4392 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4393 			    scsa2usbp->scsa2usb_log_handle,
4394 			    "scsa2usb_open_usb_pipes: bulk/in pipe open "
4395 			    " failed rval = %d", rval);
4396 
4397 			return (USB_FAILURE);
4398 		}
4399 
4400 		/* Open the bulk-out pipe  using the same policy */
4401 		if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip,
4402 		    &scsa2usbp->scsa2usb_bulkout_ept, &policy, USB_FLAGS_SLEEP,
4403 		    &scsa2usbp->scsa2usb_bulkout_pipe)) != USB_SUCCESS) {
4404 			usb_pipe_close(scsa2usbp->scsa2usb_dip,
4405 			    scsa2usbp->scsa2usb_bulkin_pipe,
4406 			    USB_FLAGS_SLEEP, NULL, NULL);
4407 
4408 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4409 			scsa2usbp->scsa2usb_bulkin_pipe = NULL;
4410 
4411 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4412 			    scsa2usbp->scsa2usb_log_handle,
4413 			    "scsa2usb_open_usb_pipes: bulk/out pipe open"
4414 			    " failed rval = %d", rval);
4415 
4416 			return (USB_FAILURE);
4417 		}
4418 
4419 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4420 
4421 		/* open interrupt pipe for CBI protocol */
4422 		if (SCSA2USB_IS_CBI(scsa2usbp)) {
4423 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4424 
4425 			if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip,
4426 			    &scsa2usbp->scsa2usb_intr_ept, &policy,
4427 			    USB_FLAGS_SLEEP, &scsa2usbp->scsa2usb_intr_pipe)) !=
4428 			    USB_SUCCESS) {
4429 				usb_pipe_close(scsa2usbp->scsa2usb_dip,
4430 				    scsa2usbp->scsa2usb_bulkin_pipe,
4431 				    USB_FLAGS_SLEEP, NULL, NULL);
4432 
4433 				usb_pipe_close(scsa2usbp->scsa2usb_dip,
4434 				    scsa2usbp->scsa2usb_bulkout_pipe,
4435 				    USB_FLAGS_SLEEP, NULL, NULL);
4436 
4437 				mutex_enter(&scsa2usbp->scsa2usb_mutex);
4438 				scsa2usbp->scsa2usb_bulkin_pipe = NULL;
4439 				scsa2usbp->scsa2usb_bulkout_pipe = NULL;
4440 
4441 				USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4442 				    scsa2usbp->scsa2usb_log_handle,
4443 				    "scsa2usb_open_usb_pipes: intr pipe open"
4444 				    " failed rval = %d", rval);
4445 
4446 				return (USB_FAILURE);
4447 			}
4448 
4449 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4450 		}
4451 
4452 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4453 
4454 		/* get the max transfer size of the bulk pipe */
4455 		if (usb_pipe_get_max_bulk_transfer_size(scsa2usbp->scsa2usb_dip,
4456 		    &sz) == USB_SUCCESS) {
4457 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4458 			scsa2usbp->scsa2usb_max_bulk_xfer_size = sz;
4459 		} else {
4460 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4461 			scsa2usbp->scsa2usb_max_bulk_xfer_size = DEV_BSIZE;
4462 		}
4463 
4464 		/* limit the xfer size */
4465 		scsa2usbp->scsa2usb_max_bulk_xfer_size = min(
4466 		    scsa2usbp->scsa2usb_max_bulk_xfer_size,
4467 		    scsa2usb_max_bulk_xfer_size);
4468 
4469 		USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4470 		    "scsa2usb_open_usb_pipes: max bulk transfer size = %lx",
4471 		    scsa2usbp->scsa2usb_max_bulk_xfer_size);
4472 
4473 		/* Set the pipes opened flag */
4474 		scsa2usbp->scsa2usb_flags |= SCSA2USB_FLAGS_PIPES_OPENED;
4475 
4476 		scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_NORMAL;
4477 
4478 		/* Set the state to NONE */
4479 		scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_NONE;
4480 	}
4481 
4482 	return (USB_SUCCESS);
4483 }
4484 
4485 
4486 /*
4487  * scsa2usb_close_usb_pipes:
4488  *	close all pipes synchronously
4489  */
4490 void
4491 scsa2usb_close_usb_pipes(scsa2usb_state_t *scsa2usbp)
4492 {
4493 	usb_flags_t flags = USB_FLAGS_SLEEP;
4494 
4495 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4496 	    "scsa2usb_close_usb_pipes: scsa2usb_state = 0x%p",
4497 	    (void *)scsa2usbp);
4498 
4499 	ASSERT(scsa2usbp);
4500 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4501 
4502 	if ((scsa2usbp->scsa2usb_flags & SCSA2USB_FLAGS_PIPES_OPENED) == 0) {
4503 
4504 		return;
4505 	}
4506 
4507 	scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_CLOSING;
4508 	/* to avoid races, reset the flag first */
4509 	scsa2usbp->scsa2usb_flags &= ~SCSA2USB_FLAGS_PIPES_OPENED;
4510 
4511 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4512 
4513 	usb_pipe_close(scsa2usbp->scsa2usb_dip,
4514 	    scsa2usbp->scsa2usb_bulkout_pipe, flags, NULL, NULL);
4515 
4516 	usb_pipe_close(scsa2usbp->scsa2usb_dip,
4517 	    scsa2usbp->scsa2usb_bulkin_pipe, flags, NULL, NULL);
4518 
4519 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4520 	if (SCSA2USB_IS_CBI(scsa2usbp)) {
4521 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4522 		usb_pipe_close(scsa2usbp->scsa2usb_dip,
4523 		    scsa2usbp->scsa2usb_intr_pipe, flags, NULL, NULL);
4524 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4525 	}
4526 	scsa2usbp->scsa2usb_bulkout_pipe = NULL;
4527 	scsa2usbp->scsa2usb_bulkin_pipe = NULL;
4528 	scsa2usbp->scsa2usb_intr_pipe = NULL;
4529 
4530 	scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_NORMAL;
4531 }
4532 
4533 
4534 /*
4535  * scsa2usb_fill_up_cdb_lba:
4536  *	fill up command CDBs' LBA part
4537  */
4538 static void
4539 scsa2usb_fill_up_cdb_lba(scsa2usb_cmd_t *cmd, int lba)
4540 {
4541 	/* zero cdb1, lba bits so they won't get copied in the new cdb */
4542 	cmd->cmd_cdb[SCSA2USB_LUN] &= 0xE0;
4543 	cmd->cmd_cdb[SCSA2USB_LBA_0] = lba >> 24;
4544 	cmd->cmd_cdb[SCSA2USB_LBA_1] = lba >> 16;
4545 	cmd->cmd_cdb[SCSA2USB_LBA_2] = lba >> 8;
4546 	cmd->cmd_cdb[SCSA2USB_LBA_3] = (uchar_t)lba;
4547 	cmd->cmd_lba = lba;
4548 }
4549 
4550 
4551 /*
4552  * scsa2usb_fill_up_ReadCD_cdb_len:
4553  *	fill up READ_CD command CDBs' len part
4554  */
4555 static void
4556 scsa2usb_fill_up_ReadCD_cdb_len(scsa2usb_cmd_t *cmd, int len, int actual_len)
4557 {
4558 	cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_0] = len >> 16;
4559 	cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_1] = len >> 8;
4560 	cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_2] = (uchar_t)len;
4561 	cmd->cmd_actual_len = (uchar_t)actual_len;
4562 }
4563 
4564 
4565 /*
4566  * scsa2usb_fill_up_12byte_cdb_len:
4567  *	fill up generic 12-byte command CDBs' len part
4568  */
4569 static void
4570 scsa2usb_fill_up_12byte_cdb_len(scsa2usb_cmd_t *cmd, int len, int actual_len)
4571 {
4572 	cmd->cmd_cdb[6] = len >> 24;
4573 	cmd->cmd_cdb[7] = len >> 16;
4574 	cmd->cmd_cdb[8] = len >> 8;
4575 	cmd->cmd_cdb[9] = (uchar_t)len;
4576 	cmd->cmd_actual_len = (uchar_t)actual_len;
4577 }
4578 
4579 
4580 /*
4581  * scsa2usb_fill_up_cdb_len:
4582  *	fill up generic 10-byte command CDBs' len part
4583  */
4584 static void
4585 scsa2usb_fill_up_cdb_len(scsa2usb_cmd_t *cmd, int len)
4586 {
4587 	cmd->cmd_cdb[SCSA2USB_LEN_0] = len >> 8;
4588 	cmd->cmd_cdb[SCSA2USB_LEN_1] = (uchar_t)len;
4589 }
4590 
4591 
4592 /*
4593  * scsa2usb_read_cd_blk_size:
4594  *	For SCMD_READ_CD opcode (0xbe). Figure out the
4595  *	block size based on expected sector type field
4596  *	definition. See MMC SCSI Specs section 6.1.15
4597  *
4598  *	Based on the value of the "expected_sector_type"
4599  *	field, the block size could be different.
4600  */
4601 static int
4602 scsa2usb_read_cd_blk_size(uchar_t expected_sector_type)
4603 {
4604 	int blk_size;
4605 
4606 	switch (expected_sector_type) {
4607 	case READ_CD_EST_CDDA:
4608 		blk_size = CDROM_BLK_2352;
4609 		break;
4610 	case READ_CD_EST_MODE2:
4611 		blk_size = CDROM_BLK_2336;
4612 		break;
4613 	case READ_CD_EST_MODE2FORM2:
4614 		blk_size = CDROM_BLK_2324;
4615 		break;
4616 	case READ_CD_EST_MODE2FORM1:
4617 	case READ_CD_EST_ALLTYPE:
4618 	case READ_CD_EST_MODE1:
4619 	default:
4620 		blk_size = CDROM_BLK_2048;
4621 	}
4622 
4623 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, NULL, "scsa2usb_read_cd_blk_size: "
4624 	    "est = 0x%x blk_size = %d", expected_sector_type, blk_size);
4625 
4626 	return (blk_size);
4627 }
4628 
4629 
4630 /*
4631  * scsa2usb_bp_to_mblk:
4632  *	Convert a bp to mblk_t. USBA framework understands mblk_t.
4633  */
4634 static mblk_t *
4635 scsa2usb_bp_to_mblk(scsa2usb_state_t *scsa2usbp)
4636 {
4637 	size_t		size;
4638 	mblk_t		*mp;
4639 	struct buf	*bp;
4640 	scsa2usb_cmd_t	*cmd = PKT2CMD(scsa2usbp->scsa2usb_cur_pkt);
4641 
4642 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4643 	    "scsa2usb_bp_to_mblk: ");
4644 
4645 	ASSERT(scsa2usbp->scsa2usb_cur_pkt);
4646 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4647 
4648 	bp = cmd->cmd_bp;
4649 
4650 	if (bp && (bp->b_bcount > 0)) {
4651 		size = ((bp->b_bcount > cmd->cmd_xfercount) ?
4652 		    cmd->cmd_xfercount : bp->b_bcount);
4653 	} else {
4654 
4655 		return (NULL);
4656 	}
4657 
4658 	mp = esballoc_wait((uchar_t *)bp->b_un.b_addr + cmd->cmd_offset,
4659 	    size, BPRI_LO, &frnop);
4660 
4661 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4662 	    "scsa2usb_bp_to_mblk: "
4663 	    "mp=0x%p bp=0x%p pkt=0x%p off=0x%lx sz=%lu add=0x%p",
4664 	    (void *)mp, (void *)bp, (void *)scsa2usbp->scsa2usb_cur_pkt,
4665 	    cmd->cmd_offset, bp->b_bcount - cmd->cmd_offset,
4666 	    (void *)bp->b_un.b_addr);
4667 
4668 	mp->b_wptr += size;
4669 	cmd->cmd_offset += size;
4670 
4671 	return (mp);
4672 }
4673 
4674 
4675 /*
4676  * scsa2usb_handle_data_start:
4677  *	Initiate the data xfer. It could be IN/OUT direction.
4678  *
4679  *	Data IN:
4680  *		Send out the bulk-xfer request
4681  *		if rval implies STALL
4682  *			clear endpoint stall and reset bulk-in pipe
4683  *			handle data read in so far; set cmd->cmd_done
4684  *			also adjust data xfer length accordingly
4685  *		else other error
4686  *			report back to transport
4687  *			typically transport will call reset recovery
4688  *		else (no error)
4689  *			return success
4690  *
4691  *	Data OUT:
4692  *		Send out the bulk-xfer request
4693  *		if rval implies STALL
4694  *			clear endpoint stall and reset bulk-in pipe
4695  *			adjust data xfer length
4696  *		else other error
4697  *			report back to transport
4698  *			typically transport will call reset recovery
4699  *		else (no error)
4700  *			return success
4701  *
4702  *	NOTE: We call this function only if there is xfercount.
4703  */
4704 int
4705 scsa2usb_handle_data_start(scsa2usb_state_t *scsa2usbp,
4706     scsa2usb_cmd_t *cmd, usb_bulk_req_t *req)
4707 {
4708 	int		rval = USB_SUCCESS;
4709 	uint_t		ept_addr;
4710 	usb_flags_t	flags = USB_FLAGS_SLEEP;
4711 #ifdef	SCSA2USB_BULK_ONLY_TEST
4712 	usb_req_attrs_t	attrs = 0;
4713 #else
4714 	usb_req_attrs_t	attrs = USB_ATTRS_SHORT_XFER_OK;
4715 #endif
4716 
4717 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4718 	    "scsa2usb_handle_data_start: BEGIN cmd = %p, req = %p",
4719 	    (void *)cmd, (void *)req);
4720 
4721 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4722 
4723 	switch (cmd->cmd_dir) {
4724 	case USB_EP_DIR_IN:
4725 #ifdef	SCSA2USB_BULK_ONLY_TEST
4726 		/*
4727 		 * This case occurs when the host expects to receive
4728 		 * more data than the device actually transfers. Hi > Di
4729 		 */
4730 		if (scsa2usb_test_case_5) {
4731 			usb_bulk_req_t *req2;
4732 
4733 			req->bulk_len = cmd->cmd_xfercount - 1;
4734 			req->bulk_attributes = 0;
4735 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4736 			SCSA2USB_FREE_MSG(req->bulk_data);
4737 			req->bulk_data = allocb_wait(req->bulk_len, BPRI_LO,
4738 			    STR_NOSIG, NULL);
4739 
4740 			ASSERT(req->bulk_timeout);
4741 			rval = usb_pipe_bulk_xfer(
4742 			    scsa2usbp->scsa2usb_bulkin_pipe, req, flags);
4743 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4744 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4745 			    scsa2usbp->scsa2usb_log_handle, "rval = %x", rval);
4746 
4747 			req2 = scsa2usb_init_bulk_req(scsa2usbp,
4748 			    cmd->cmd_xfercount + 2,
4749 			    cmd->cmd_timeout, 0, flags);
4750 			req2->bulk_len = cmd->cmd_xfercount + 2;
4751 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4752 
4753 			ASSERT(req2->bulk_timeout);
4754 			rval = usb_pipe_bulk_xfer(
4755 			    scsa2usbp->scsa2usb_bulkin_pipe, req2, flags);
4756 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4757 
4758 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4759 			    scsa2usbp->scsa2usb_log_handle,
4760 			    "TEST 5: Hi > Di: rval = 0x%x", rval);
4761 			scsa2usb_test_case_5 = 0;
4762 			usb_free_bulk_req(req2);
4763 
4764 			return (rval);
4765 		}
4766 
4767 		/*
4768 		 * This happens when the host expects to send data to the
4769 		 * device while the device intends to send data to the host.
4770 		 */
4771 		if (scsa2usb_test_case_8 && (cmd->cmd_cdb[0] == SCMD_READ_G1)) {
4772 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4773 			    scsa2usbp->scsa2usb_log_handle,
4774 			    "TEST 8: Hi <> Do: Step 2");
4775 			scsa2usb_test_mblk(scsa2usbp, B_TRUE);
4776 			scsa2usb_test_case_8 = 0;
4777 
4778 			return (rval);
4779 		}
4780 #endif	/* SCSA2USB_BULK_ONLY_TEST */
4781 
4782 		ept_addr = scsa2usbp->scsa2usb_bulkin_ept.bEndpointAddress;
4783 		req->bulk_len = cmd->cmd_xfercount;
4784 		req->bulk_attributes = attrs;
4785 		SCSA2USB_FREE_MSG(req->bulk_data);
4786 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4787 
4788 		req->bulk_data = esballoc_wait(
4789 		    (uchar_t *)cmd->cmd_bp->b_un.b_addr +
4790 		    cmd->cmd_offset,
4791 		    req->bulk_len, BPRI_LO, &frnop);
4792 
4793 		ASSERT(req->bulk_timeout);
4794 		rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkin_pipe,
4795 		    req, flags);
4796 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4797 
4798 		break;
4799 
4800 	case USB_EP_DIR_OUT:
4801 #ifdef	SCSA2USB_BULK_ONLY_TEST
4802 		/*
4803 		 * This happens when the host expects to receive data
4804 		 * from the device while the device intends to receive
4805 		 * data from the host.
4806 		 */
4807 		if (scsa2usb_test_case_10 &&
4808 		    (cmd->cmd_cdb[0] == SCMD_WRITE_G1)) {
4809 			req->bulk_len = CSW_LEN;
4810 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4811 
4812 			ASSERT(req->bulk_timeout);
4813 			rval = usb_pipe_bulk_xfer(
4814 			    scsa2usbp->scsa2usb_bulkin_pipe, req, flags);
4815 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4816 
4817 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4818 			    scsa2usbp->scsa2usb_log_handle,
4819 			    "TEST 10: Ho <> Di: done rval = 0x%x",  rval);
4820 			scsa2usb_test_case_10 = 0;
4821 
4822 			return (rval);
4823 		}
4824 #endif	/* SCSA2USB_BULK_ONLY_TEST */
4825 
4826 		req->bulk_data = scsa2usb_bp_to_mblk(scsa2usbp);
4827 		if (req->bulk_data == NULL) {
4828 
4829 			return (USB_FAILURE);
4830 		}
4831 
4832 #ifdef	SCSA2USB_BULK_ONLY_TEST
4833 		if (scsa2usb_test_case_11) {
4834 			/*
4835 			 * Host expects to send data to the device and
4836 			 * device doesn't expect to receive any data
4837 			 */
4838 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4839 			    scsa2usbp->scsa2usb_log_handle, "TEST 11: Ho > Do");
4840 
4841 			scsa2usb_test_mblk(scsa2usbp, B_FALSE);
4842 			scsa2usb_test_case_11 = 0;
4843 		}
4844 #endif	/* SCSA2USB_BULK_ONLY_TEST */
4845 
4846 		ept_addr = scsa2usbp->scsa2usb_bulkout_ept.bEndpointAddress;
4847 		req->bulk_len = MBLKL(req->bulk_data);
4848 		req->bulk_timeout = scsa2usb_bulk_timeout(cmd->cmd_timeout);
4849 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4850 
4851 		ASSERT(req->bulk_timeout);
4852 		rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkout_pipe,
4853 		    req, flags);
4854 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4855 		break;
4856 	}
4857 
4858 	USB_DPRINTF_L3(DPRINT_MASK_SCSA,
4859 	    scsa2usbp->scsa2usb_log_handle,
4860 	    "scsa2usb_handle_data_start: rval=%d cr=%d", rval,
4861 	    req->bulk_completion_reason);
4862 
4863 	if (rval != USB_SUCCESS) {
4864 		/* Handle Errors now */
4865 		if (req->bulk_completion_reason == USB_CR_STALL) {
4866 			if (cmd->cmd_dir == USB_EP_DIR_IN) {
4867 				(void) scsa2usb_clear_ept_stall(
4868 				    scsa2usbp, ept_addr,
4869 				    scsa2usbp-> scsa2usb_bulkin_pipe,
4870 				    "bulk-in");
4871 			} else {
4872 				(void) scsa2usb_clear_ept_stall(
4873 				    scsa2usbp, ept_addr,
4874 				    scsa2usbp-> scsa2usb_bulkout_pipe,
4875 				    "bulk-out");
4876 			}
4877 		}
4878 
4879 		/* no more data to transfer after this */
4880 		cmd->cmd_done = 1;
4881 	}
4882 
4883 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4884 	    "scsa2usb_handle_data_start: END %s data rval = %d",
4885 	    (cmd->cmd_dir == USB_EP_DIR_IN) ? "bulk-in" : "bulk-out", rval);
4886 
4887 	return (rval);
4888 }
4889 
4890 
4891 /*
4892  * scsa2usb_handle_data_done:
4893  *	This function handles the completion of the data xfer.
4894  *	It also massages the inquiry data. This function may
4895  *	also be called after a stall.
4896  */
4897 void
4898 scsa2usb_handle_data_done(scsa2usb_state_t *scsa2usbp,
4899     scsa2usb_cmd_t *cmd, usb_bulk_req_t *req)
4900 {
4901 	struct buf	*bp = cmd->cmd_bp;
4902 	struct scsi_pkt	*pkt = scsa2usbp->scsa2usb_cur_pkt;
4903 	mblk_t		*data = req->bulk_data;
4904 	int		len = data ? MBLKL(data) : 0;
4905 	uint32_t	max_lba;
4906 
4907 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4908 
4909 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4910 	    "scsa2usb_handle_data_done:\n\tcmd = 0x%p data = 0x%p len = 0x%x",
4911 	    (void *)cmd, (void *)data, len);
4912 
4913 	cmd->cmd_resid_xfercount = cmd->cmd_xfercount - len;
4914 
4915 	if (len)  {
4916 		uchar_t	*p;
4917 		uchar_t dtype;
4918 		scsa2usb_read_cap_t *cap;
4919 		struct scsi_inquiry *inq;
4920 
4921 		switch (cmd->cmd_cdb[SCSA2USB_OPCODE]) {
4922 		case SCMD_INQUIRY:
4923 			/*
4924 			 * cache a copy of the inquiry data for our own use
4925 			 * but ensure that we have at least up to
4926 			 * inq_revision, inq_serial is not required.
4927 			 * ignore inquiry data returned for inquiry commands
4928 			 * with SCSI-3 EVPD, CmdDt bits set.
4929 			 */
4930 			if (((cmd->cmd_cdb[SCSA2USB_LUN] & 0x1f) == 0) &&
4931 			    (len >= SCSA2USB_MAX_INQ_LEN)) {
4932 				inq = (struct scsi_inquiry *)data->b_rptr;
4933 				dtype = inq->inq_dtype & DTYPE_MASK;
4934 				/*
4935 				 * scsi framework sends zero byte write(10) cmd
4936 				 * to (Simplified) direct-access devices with
4937 				 * inquiry version > 2 for reservation changes.
4938 				 * But some USB devices don't support zero byte
4939 				 * write(10) even though they have inquiry
4940 				 * version > 2. Considering scsa2usb driver
4941 				 * doesn't support reservation and all the
4942 				 * reservation cmds are being faked, we fake
4943 				 * the inquiry version to 0 to make scsi
4944 				 * framework send test unit ready cmd which is
4945 				 * supported by all the usb devices.
4946 				 */
4947 				if (((dtype == DTYPE_DIRECT) ||
4948 				    (dtype == DTYPE_RBC)) &&
4949 				    (inq->inq_ansi > 2)) {
4950 					inq->inq_ansi = 0;
4951 				}
4952 
4953 				bzero(&scsa2usbp->scsa2usb_lun_inquiry
4954 				    [pkt->pkt_address.a_lun],
4955 				    sizeof (struct scsi_inquiry));
4956 				bcopy(data->b_rptr,
4957 				    &scsa2usbp->scsa2usb_lun_inquiry
4958 				    [pkt->pkt_address.a_lun], len);
4959 			}
4960 
4961 			USB_DPRINTF_L3(DPRINT_MASK_SCSA,
4962 			    scsa2usbp->scsa2usb_log_handle,
4963 			    "scsi inquiry type = 0x%x",
4964 			    scsa2usbp->scsa2usb_lun_inquiry
4965 			    [pkt->pkt_address.a_lun].inq_dtype);
4966 
4967 			cmd->cmd_done = 1;
4968 			goto handle_data;
4969 
4970 		case SCMD_READ_CAPACITY:
4971 			cap = (scsa2usb_read_cap_t *)data->b_rptr;
4972 
4973 			/* Figure out the logical block size */
4974 			if ((len >= sizeof (struct scsa2usb_read_cap)) &&
4975 			    (req->bulk_completion_reason == USB_CR_OK)) {
4976 				scsa2usbp->
4977 				    scsa2usb_lbasize[pkt->pkt_address.a_lun] =
4978 				    SCSA2USB_MK_32BIT(
4979 				    cap->scsa2usb_read_cap_blen3,
4980 				    cap->scsa2usb_read_cap_blen2,
4981 				    cap->scsa2usb_read_cap_blen1,
4982 				    cap->scsa2usb_read_cap_blen0);
4983 
4984 				max_lba = SCSA2USB_MK_32BIT(
4985 				    cap->scsa2usb_read_cap_lba3,
4986 				    cap->scsa2usb_read_cap_lba2,
4987 				    cap->scsa2usb_read_cap_lba1,
4988 				    cap->scsa2usb_read_cap_lba0);
4989 
4990 				/*
4991 				 * Some devices return total logical block
4992 				 * number instead of highest logical block
4993 				 * address. Adjust the value by minus 1.
4994 				 */
4995 				if (max_lba > 0 && (scsa2usbp->scsa2usb_attrs &
4996 				    SCSA2USB_ATTRS_NO_CAP_ADJUST) == 0) {
4997 					max_lba -= 1;
4998 					cap->scsa2usb_read_cap_lba0 =
4999 					    (uchar_t)(max_lba & 0xFF);
5000 					cap->scsa2usb_read_cap_lba1 =
5001 					    (uchar_t)(max_lba >> 8 & 0xFF);
5002 					cap->scsa2usb_read_cap_lba2 =
5003 					    (uchar_t)(max_lba >> 16 & 0xFF);
5004 					cap->scsa2usb_read_cap_lba3 =
5005 					    (uchar_t)(max_lba >> 24 & 0xFF);
5006 				}
5007 
5008 				USB_DPRINTF_L2(DPRINT_MASK_SCSA,
5009 				    scsa2usbp->scsa2usb_log_handle,
5010 				    "bytes in each logical block=0x%lx,"
5011 				    "number of total logical blocks=0x%x",
5012 				    scsa2usbp->
5013 				    scsa2usb_lbasize[pkt->pkt_address.a_lun],
5014 				    max_lba + 1);
5015 			}
5016 			cmd->cmd_done = 1;
5017 			goto handle_data;
5018 
5019 		case SCMD_REQUEST_SENSE:
5020 			p = data->b_rptr;
5021 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
5022 			    scsa2usbp->scsa2usb_log_handle,
5023 			    "cdb: %x rqsense: "
5024 			    "%x %x %x %x %x %x %x %x %x %x\n\t"
5025 			    "%x %x %x %x %x %x %x %x %x %x",
5026 			    cmd->cmd_cdb[0],
5027 			    p[0], p[1], p[2], p[3], p[4],
5028 			    p[5], p[6], p[7], p[8], p[9],
5029 			    p[10], p[11], p[12], p[13], p[14],
5030 			    p[15], p[16], p[17], p[18], p[19]);
5031 
5032 			scsa2usbp->scsa2usb_last_cmd.status = p[2];
5033 			cmd->cmd_done = 1;
5034 			/* FALLTHROUGH */
5035 
5036 		default:
5037 handle_data:
5038 			if (bp && len && (cmd->cmd_dir == USB_EP_DIR_IN)) {
5039 				/*
5040 				 * we don't have to copy the data, the
5041 				 * data pointers for the mblk_t for
5042 				 * the bulk-in xfer points to the
5043 				 * struct buf * data.
5044 				 */
5045 				cmd->cmd_offset += len;
5046 			}
5047 
5048 			USB_DPRINTF_L3(DPRINT_MASK_SCSA,
5049 			    scsa2usbp->scsa2usb_log_handle,
5050 			    "len = 0x%x total = 0x%lx offset = 0x%lx",
5051 			    len, cmd->cmd_total_xfercount, cmd->cmd_offset);
5052 
5053 			/*
5054 			 * update total_xfercount now but it may be
5055 			 * adjusted after receiving the residue
5056 			 */
5057 			cmd->cmd_total_xfercount -= len;
5058 
5059 			if ((req->bulk_completion_reason != USB_CR_OK) ||
5060 			    (cmd->cmd_resid_xfercount != 0) ||
5061 			    (cmd->cmd_total_xfercount == 0)) {
5062 				/* set pkt_resid to total to be sure */
5063 				pkt->pkt_resid = cmd->cmd_total_xfercount;
5064 				cmd->cmd_done = 1;
5065 			}
5066 
5067 			break;
5068 		}
5069 	} else {
5070 		if (cmd->cmd_dir == USB_EP_DIR_OUT) {
5071 			if (cmd->cmd_total_xfercount == 0) {
5072 				cmd->cmd_done = 1;
5073 			}
5074 		}
5075 	}
5076 }
5077 
5078 
5079 /*
5080  * scsa2usb_init_bulk_req:
5081  *	Allocate (synchronously) and fill in a bulk-request
5082  */
5083 usb_bulk_req_t *
5084 scsa2usb_init_bulk_req(scsa2usb_state_t *scsa2usbp, size_t length,
5085     uint_t timeout, usb_req_attrs_t attrs, usb_flags_t flags)
5086 {
5087 	usb_bulk_req_t	*req;
5088 
5089 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5090 
5091 	req = usb_alloc_bulk_req(scsa2usbp->scsa2usb_dip, length,
5092 	    flags | USB_FLAGS_SLEEP);
5093 
5094 	req->bulk_len = (uint_t)length;			/* xfer length */
5095 	req->bulk_timeout = scsa2usb_bulk_timeout(timeout); /* xfer timeout */
5096 	req->bulk_attributes = attrs;		/* xfer attrs */
5097 	req->bulk_client_private = (usb_opaque_t)scsa2usbp; /* statep */
5098 
5099 	return (req);
5100 }
5101 
5102 
5103 /*
5104  * scsa2usb_bulk_timeout:
5105  *	ensure that bulk requests do not have infinite timeout values
5106  */
5107 int
5108 scsa2usb_bulk_timeout(int timeout)
5109 {
5110 	return ((timeout == 0) ? scsa2usb_long_timeout : timeout);
5111 }
5112 
5113 
5114 /*
5115  * scsa2usb_clear_ept_stall:
5116  *	clear endpoint stall and reset pipes
5117  */
5118 int
5119 scsa2usb_clear_ept_stall(scsa2usb_state_t *scsa2usbp, uint_t ept_addr,
5120     usb_pipe_handle_t ph, char *what)
5121 {
5122 	int rval;
5123 	dev_info_t *dip = scsa2usbp->scsa2usb_dip;
5124 
5125 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5126 	if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
5127 
5128 		return (USB_FAILURE);
5129 	}
5130 
5131 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5132 	rval = usb_clr_feature(dip, USB_DEV_REQ_RCPT_EP, 0, ept_addr,
5133 	    USB_FLAGS_SLEEP, NULL, NULL);
5134 
5135 	usb_pipe_reset(dip, ph, USB_FLAGS_SLEEP, NULL, NULL);
5136 
5137 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5138 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5139 	    "scsa2usb_clear_ept_stall: on %s: ept = 0x%x rval = %d",
5140 	    what, ept_addr, rval);
5141 
5142 	return (rval);
5143 }
5144 
5145 
5146 /*
5147  * scsa2usb_pkt_completion:
5148  *	Handle pkt completion.
5149  */
5150 static void
5151 scsa2usb_pkt_completion(scsa2usb_state_t *scsa2usbp, struct scsi_pkt *pkt)
5152 {
5153 	scsa2usb_cmd_t *cmd = PKT2CMD(pkt);
5154 	size_t len;
5155 
5156 	ASSERT(pkt);
5157 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5158 
5159 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5160 	    "scsa2usb_pkt_completion:\n\tscsa2usbp = 0x%p "
5161 	    "reason=%d, status=%d state=0x%x stats=0x%x resid=0x%lx",
5162 	    (void *)scsa2usbp, pkt->pkt_reason, *(pkt->pkt_scbp),
5163 	    pkt->pkt_state, pkt->pkt_statistics, pkt->pkt_resid);
5164 
5165 	if (pkt->pkt_reason == CMD_CMPLT) {
5166 		pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET |
5167 		    STATE_SENT_CMD | STATE_GOT_STATUS;
5168 		if (cmd->cmd_xfercount) {
5169 			pkt->pkt_state |= STATE_XFERRED_DATA;
5170 		}
5171 	} else {
5172 		pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET |
5173 		    STATE_SENT_CMD;
5174 	}
5175 
5176 	/*
5177 	 * don't zap the current state when in panic as this will
5178 	 * make debugging harder
5179 	 */
5180 	if ((scsa2usbp->scsa2usb_cur_pkt == pkt) && !ddi_in_panic()) {
5181 		SCSA2USB_RESET_CUR_PKT(scsa2usbp);
5182 
5183 		len = sizeof (scsa2usbp->scsa2usb_last_cmd.cdb);
5184 		bzero(scsa2usbp->scsa2usb_last_cmd.cdb, len);
5185 
5186 		len = (len < cmd->cmd_cdblen) ? len : cmd->cmd_cdblen;
5187 		USB_DPRINTF_L3(DPRINT_MASK_SCSA,
5188 		    scsa2usbp->scsa2usb_log_handle,
5189 		    "scsa2usb_pkt_completion: save last cmd, len=%ld", len);
5190 
5191 		/* save the last command */
5192 		bcopy(pkt->pkt_cdbp, scsa2usbp->scsa2usb_last_cmd.cdb, len);
5193 
5194 		/* reset the scsa2usb_last_cmd.status value */
5195 		if ((pkt->pkt_cdbp[0] != SCMD_REQUEST_SENSE) &&
5196 		    (pkt->pkt_cdbp[0] != SCMD_INQUIRY)) {
5197 			scsa2usbp->scsa2usb_last_cmd.status = 0;
5198 		}
5199 
5200 		/*
5201 		 * set pkt state to NONE *before* calling back as the target
5202 		 * driver will immediately submit the next packet
5203 		 */
5204 		scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_NONE;
5205 	}
5206 
5207 	if (pkt->pkt_comp) {
5208 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5209 		pkt->pkt_comp(pkt);
5210 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5211 
5212 	}
5213 }
5214 
5215 
5216 /*
5217  * Even handling functions:
5218  *
5219  * scsa2usb_reconnect_event_cb:
5220  *	event handling
5221  */
5222 static int
5223 scsa2usb_reconnect_event_cb(dev_info_t *dip)
5224 {
5225 	scsa2usb_state_t *scsa2usbp =
5226 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
5227 	dev_info_t	*cdip;
5228 	int		circ;
5229 	int		rval = USB_SUCCESS;
5230 
5231 	ASSERT(scsa2usbp != NULL);
5232 
5233 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5234 	    "scsa2usb_reconnect_event_cb: dip = 0x%p", (void *)dip);
5235 
5236 	scsa2usb_restore_device_state(dip, scsa2usbp);
5237 
5238 	USB_DPRINTF_L0(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5239 	    "Reinserted device is accessible again.");
5240 
5241 	ndi_devi_enter(dip, &circ);
5242 	for (cdip = ddi_get_child(dip); cdip; ) {
5243 		dev_info_t *next = ddi_get_next_sibling(cdip);
5244 
5245 		mutex_enter(&DEVI(cdip)->devi_lock);
5246 		DEVI_SET_DEVICE_REINSERTED(cdip);
5247 		mutex_exit(&DEVI(cdip)->devi_lock);
5248 
5249 		cdip = next;
5250 	}
5251 	ndi_devi_exit(dip, circ);
5252 
5253 	/* stop suppressing warnings */
5254 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5255 	scsa2usbp->scsa2usb_warning_given = B_FALSE;
5256 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5257 
5258 	if (scsa2usbp->scsa2usb_ugen_hdl) {
5259 		rval = usb_ugen_reconnect_ev_cb(
5260 		    scsa2usbp->scsa2usb_ugen_hdl);
5261 	}
5262 
5263 	return (rval);
5264 }
5265 
5266 
5267 /*
5268  * scsa2usb_all_waitQs_empty:
5269  *	check if all waitQs empty
5270  */
5271 static int
5272 scsa2usb_all_waitQs_empty(scsa2usb_state_t *scsa2usbp)
5273 {
5274 	uint_t	lun;
5275 
5276 	for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
5277 		if (usba_list_entry_count(
5278 		    &scsa2usbp->scsa2usb_waitQ[lun])) {
5279 
5280 			return (USB_FAILURE);
5281 		}
5282 	}
5283 
5284 	return (USB_SUCCESS);
5285 }
5286 
5287 
5288 /*
5289  * scsa2usb_disconnect_event_cb:
5290  *	callback for disconnect events
5291  */
5292 static int
5293 scsa2usb_disconnect_event_cb(dev_info_t *dip)
5294 {
5295 	scsa2usb_state_t *scsa2usbp =
5296 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
5297 	dev_info_t	*cdip;
5298 	int		circ, i;
5299 	int		rval = USB_SUCCESS;
5300 
5301 	ASSERT(scsa2usbp != NULL);
5302 
5303 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5304 	    "scsa2usb_disconnect_event_cb: dip = 0x%p", (void *)dip);
5305 
5306 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5307 	scsa2usbp->scsa2usb_dev_state = USB_DEV_DISCONNECTED;
5308 
5309 	/*
5310 	 * wait till the work thread is done, carry on regardless
5311 	 * if not.
5312 	 */
5313 	for (i = 0; i < SCSA2USB_DRAIN_TIMEOUT; i++) {
5314 		if ((scsa2usbp->scsa2usb_work_thread_id == NULL) &&
5315 		    (scsa2usbp->scsa2usb_cur_pkt == NULL) &&
5316 		    (scsa2usb_all_waitQs_empty(scsa2usbp) ==
5317 		    USB_SUCCESS)) {
5318 
5319 			break;
5320 		}
5321 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5322 		delay(drv_usectohz(1000000));
5323 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5324 	}
5325 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5326 
5327 	ndi_devi_enter(dip, &circ);
5328 	for (cdip = ddi_get_child(dip); cdip; ) {
5329 		dev_info_t *next = ddi_get_next_sibling(cdip);
5330 
5331 		mutex_enter(&DEVI(cdip)->devi_lock);
5332 		DEVI_SET_DEVICE_REMOVED(cdip);
5333 		mutex_exit(&DEVI(cdip)->devi_lock);
5334 
5335 		cdip = next;
5336 	}
5337 	ndi_devi_exit(dip, circ);
5338 
5339 	if (scsa2usbp->scsa2usb_ugen_hdl) {
5340 		rval = usb_ugen_disconnect_ev_cb(
5341 		    scsa2usbp->scsa2usb_ugen_hdl);
5342 	}
5343 
5344 	return (rval);
5345 }
5346 
5347 
5348 /*
5349  * PM support
5350  *
5351  * scsa2usb_create_pm_components:
5352  *	create the pm components required for power management
5353  *	no mutex is need when calling USBA interfaces
5354  */
5355 static void
5356 scsa2usb_create_pm_components(dev_info_t *dip, scsa2usb_state_t *scsa2usbp)
5357 {
5358 	scsa2usb_power_t *pm;
5359 	uint_t		pwr_states;
5360 
5361 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5362 
5363 	USB_DPRINTF_L4(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5364 	    "scsa2usb_create_pm_components: dip = 0x%p, scsa2usbp = 0x%p",
5365 	    (void *)dip, (void *)scsa2usbp);
5366 
5367 	/*
5368 	 * determine if this device is on the blacklist
5369 	 * or if a conf file entry has disabled PM
5370 	 */
5371 	if ((scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_PM) == 0) {
5372 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5373 		    "device cannot be power managed");
5374 
5375 		return;
5376 	}
5377 
5378 	/* Allocate the PM state structure */
5379 	pm = kmem_zalloc(sizeof (scsa2usb_power_t), KM_SLEEP);
5380 
5381 	scsa2usbp->scsa2usb_pm = pm;
5382 	pm->scsa2usb_current_power = USB_DEV_OS_FULL_PWR;
5383 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5384 
5385 	if (usb_create_pm_components(dip, &pwr_states) ==
5386 	    USB_SUCCESS) {
5387 		if (usb_handle_remote_wakeup(dip,
5388 		    USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) {
5389 			pm->scsa2usb_wakeup_enabled = 1;
5390 		}
5391 
5392 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5393 		pm->scsa2usb_pwr_states = (uint8_t)pwr_states;
5394 		scsa2usb_raise_power(scsa2usbp);
5395 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5396 	}
5397 
5398 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5399 }
5400 
5401 
5402 /*
5403  * scsa2usb_raise_power:
5404  *	check if the device is using full power or not
5405  */
5406 static void
5407 scsa2usb_raise_power(scsa2usb_state_t *scsa2usbp)
5408 {
5409 	USB_DPRINTF_L4(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5410 	    "scsa2usb_raise_power:");
5411 
5412 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5413 
5414 	if (scsa2usbp->scsa2usb_pm) {
5415 		scsa2usb_pm_busy_component(scsa2usbp);
5416 		if (scsa2usbp->scsa2usb_pm->scsa2usb_current_power !=
5417 		    USB_DEV_OS_FULL_PWR) {
5418 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
5419 			(void) pm_raise_power(scsa2usbp->scsa2usb_dip,
5420 			    0, USB_DEV_OS_FULL_PWR);
5421 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
5422 		}
5423 	}
5424 }
5425 
5426 
5427 /*
5428  * functions to handle power transition for OS levels 0 -> 3
5429  */
5430 static int
5431 scsa2usb_pwrlvl0(scsa2usb_state_t *scsa2usbp)
5432 {
5433 	int	rval;
5434 
5435 	switch (scsa2usbp->scsa2usb_dev_state) {
5436 	case USB_DEV_ONLINE:
5437 		/* Deny the powerdown request if the device is busy */
5438 		if (scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy != 0) {
5439 
5440 			return (USB_FAILURE);
5441 		}
5442 
5443 		/*
5444 		 * stop polling on interrupt pipe
5445 		 */
5446 		scsa2usb_cbi_stop_intr_polling(scsa2usbp);
5447 
5448 		/* Issue USB D3 command to the device here */
5449 		rval = usb_set_device_pwrlvl3(scsa2usbp->scsa2usb_dip);
5450 		ASSERT(rval == USB_SUCCESS);
5451 
5452 		scsa2usbp->scsa2usb_dev_state = USB_DEV_PWRED_DOWN;
5453 
5454 		/* FALLTHRU */
5455 	case USB_DEV_DISCONNECTED:
5456 	case USB_DEV_SUSPENDED:
5457 	case USB_DEV_PWRED_DOWN:
5458 	default:
5459 		scsa2usbp->scsa2usb_pm->scsa2usb_current_power =
5460 		    USB_DEV_OS_PWR_OFF;
5461 
5462 		return (USB_SUCCESS);
5463 	}
5464 }
5465 
5466 
5467 static int
5468 scsa2usb_pwrlvl1(scsa2usb_state_t *scsa2usbp)
5469 {
5470 	int	rval;
5471 
5472 	/* Issue USB D2 command to the device here */
5473 	rval = usb_set_device_pwrlvl2(scsa2usbp->scsa2usb_dip);
5474 	ASSERT(rval == USB_SUCCESS);
5475 
5476 	return (DDI_FAILURE);
5477 }
5478 
5479 
5480 static int
5481 scsa2usb_pwrlvl2(scsa2usb_state_t *scsa2usbp)
5482 {
5483 	int	rval;
5484 
5485 	/* Issue USB D1 command to the device here */
5486 	rval = usb_set_device_pwrlvl1(scsa2usbp->scsa2usb_dip);
5487 	ASSERT(rval == USB_SUCCESS);
5488 
5489 	return (DDI_FAILURE);
5490 }
5491 
5492 
5493 static int
5494 scsa2usb_pwrlvl3(scsa2usb_state_t *scsa2usbp)
5495 {
5496 	int	rval;
5497 
5498 	/*
5499 	 * PM framework tries to put us in full power
5500 	 * during system shutdown. If we are disconnected
5501 	 * return success anyways
5502 	 */
5503 	if (scsa2usbp->scsa2usb_dev_state != USB_DEV_DISCONNECTED) {
5504 		/* Issue USB D0 command to the device here */
5505 		rval = usb_set_device_pwrlvl0(scsa2usbp->scsa2usb_dip);
5506 		ASSERT(rval == USB_SUCCESS);
5507 
5508 		scsa2usbp->scsa2usb_dev_state = USB_DEV_ONLINE;
5509 	}
5510 	scsa2usbp->scsa2usb_pm->scsa2usb_current_power = USB_DEV_OS_FULL_PWR;
5511 
5512 	return (DDI_SUCCESS);
5513 }
5514 
5515 
5516 /*
5517  * scsa2usb_power:
5518  *	power entry point
5519  */
5520 /* ARGSUSED */
5521 static int
5522 scsa2usb_power(dev_info_t *dip, int comp, int level)
5523 {
5524 	scsa2usb_state_t	*scsa2usbp;
5525 	scsa2usb_power_t	*pm;
5526 	int			rval = DDI_FAILURE;
5527 
5528 	scsa2usbp = ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
5529 
5530 	USB_DPRINTF_L3(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5531 	    "scsa2usb_power: Begin scsa2usbp (%p): level = %d",
5532 	    (void *)scsa2usbp, level);
5533 
5534 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5535 	if (SCSA2USB_BUSY(scsa2usbp)) {
5536 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5537 		    "scsa2usb_power: busy");
5538 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5539 
5540 		return (rval);
5541 	}
5542 
5543 	pm = scsa2usbp->scsa2usb_pm;
5544 	if (pm == NULL) {
5545 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5546 		    "scsa2usb_power: pm NULL");
5547 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5548 
5549 		return (rval);
5550 	}
5551 
5552 	/* check if we are transitioning to a legal power level */
5553 	if (USB_DEV_PWRSTATE_OK(pm->scsa2usb_pwr_states, level)) {
5554 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5555 		    "scsa2usb_power: illegal power level = %d "
5556 		    "pwr_states: %x", level, pm->scsa2usb_pwr_states);
5557 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5558 
5559 		return (rval);
5560 	}
5561 
5562 	switch (level) {
5563 	case USB_DEV_OS_PWR_OFF :
5564 		rval = scsa2usb_pwrlvl0(scsa2usbp);
5565 		break;
5566 	case USB_DEV_OS_PWR_1 :
5567 		rval = scsa2usb_pwrlvl1(scsa2usbp);
5568 		break;
5569 	case USB_DEV_OS_PWR_2 :
5570 		rval = scsa2usb_pwrlvl2(scsa2usbp);
5571 		break;
5572 	case USB_DEV_OS_FULL_PWR :
5573 		rval = scsa2usb_pwrlvl3(scsa2usbp);
5574 		break;
5575 	}
5576 
5577 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5578 
5579 	return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
5580 }
5581 
5582 
5583 static void
5584 scsa2usb_pm_busy_component(scsa2usb_state_t *scsa2usbp)
5585 {
5586 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5587 
5588 	if (scsa2usbp->scsa2usb_pm) {
5589 		scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy++;
5590 
5591 		USB_DPRINTF_L4(DPRINT_MASK_PM,
5592 		    scsa2usbp->scsa2usb_log_handle,
5593 		    "scsa2usb_pm_busy_component: %d",
5594 		    scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy);
5595 
5596 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5597 
5598 		if (pm_busy_component(scsa2usbp->scsa2usb_dip, 0) !=
5599 		    DDI_SUCCESS) {
5600 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
5601 			ASSERT(scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy > 0);
5602 			scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy--;
5603 
5604 			USB_DPRINTF_L2(DPRINT_MASK_PM,
5605 			    scsa2usbp->scsa2usb_log_handle,
5606 			    "scsa2usb_pm_busy_component failed: %d",
5607 			    scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy);
5608 
5609 			return;
5610 		}
5611 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5612 	}
5613 }
5614 
5615 
5616 /*
5617  * scsa2usb_pm_idle_component:
5618  *	idles the device
5619  */
5620 static void
5621 scsa2usb_pm_idle_component(scsa2usb_state_t *scsa2usbp)
5622 {
5623 	ASSERT(!mutex_owned(&scsa2usbp->scsa2usb_mutex));
5624 
5625 	if (scsa2usbp->scsa2usb_pm) {
5626 		if (pm_idle_component(scsa2usbp->scsa2usb_dip, 0) ==
5627 		    DDI_SUCCESS) {
5628 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
5629 			ASSERT(scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy > 0);
5630 			scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy--;
5631 
5632 			USB_DPRINTF_L4(DPRINT_MASK_PM,
5633 			    scsa2usbp->scsa2usb_log_handle,
5634 			    "scsa2usb_pm_idle_component: %d",
5635 			    scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy);
5636 
5637 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
5638 		}
5639 	}
5640 }
5641 
5642 
5643 #ifdef	DEBUG
5644 /*
5645  * scsa2usb_print_cdb:
5646  *	prints CDB
5647  */
5648 void
5649 scsa2usb_print_cdb(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd)
5650 {
5651 	uchar_t *c = (uchar_t *)&cmd->cmd_cdb;
5652 
5653 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5654 	    "cmd = 0x%p opcode=%s "
5655 	    "cdb: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
5656 	    (void *)cmd,
5657 	    scsi_cname(cmd->cmd_cdb[SCSA2USB_OPCODE], scsa2usb_cmds),
5658 	    c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8],
5659 	    c[9], c[10], c[11], c[12], c[13], c[14], c[15]);
5660 }
5661 #endif	/* DEBUG */
5662 
5663 
5664 #ifdef	SCSA2USB_BULK_ONLY_TEST
5665 /*
5666  * scsa2usb_test_mblk:
5667  *	This function sends a dummy data mblk_t to simulate
5668  *	the following test cases: 5 and 11.
5669  */
5670 static void
5671 scsa2usb_test_mblk(scsa2usb_state_t *scsa2usbp, boolean_t large)
5672 {
5673 	int			i, rval;
5674 	size_t			len;
5675 	usb_flags_t		flags = USB_FLAGS_SLEEP;
5676 	usb_bulk_req_t		*req;
5677 
5678 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5679 
5680 	/* should we create a larger mblk? */
5681 	len = (large == B_TRUE) ? DEV_BSIZE : USB_BULK_CBWCMD_LEN;
5682 
5683 	req = scsa2usb_init_bulk_req(scsa2usbp, len,
5684 	    SCSA2USB_BULK_PIPE_TIMEOUT, 0, flags);
5685 
5686 	/* fill up the data mblk */
5687 	for (i = 0; i < len; i++) {
5688 		*req->bulk_data->b_wptr++ = (uchar_t)i;
5689 	}
5690 
5691 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5692 	ASSERT(req->bulk_timeout);
5693 	rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkout_pipe, req, flags);
5694 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5695 
5696 	USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5697 	    "scsa2usb_test_mblk: Sent Data Out rval = 0x%x", rval);
5698 
5699 	usb_free_bulk_req(req);
5700 }
5701 #endif	/* SCSA2USB_BULK_ONLY_TEST */
5702