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