xref: /titanic_44/usr/src/uts/common/io/usb/scsa2usb/scsa2usb.c (revision f53eecf557986dac6ededb388fedd6ca63be0350)
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_PAGE3_CODE) ||
3319 		    (pkt->pkt_cdbp[2] == SD_MODE_SENSE_PAGE4_CODE)) {
3320 			if (cmd->cmd_bp) {
3321 				cmd->cmd_pkt->pkt_resid = cmd->cmd_bp->b_bcount;
3322 			}
3323 			scsa2usb_force_invalid_request(scsa2usbp, cmd);
3324 			return (SCSA2USB_JUST_ACCEPT);
3325 		}
3326 		/* FALLTHROUGH */
3327 
3328 	default:
3329 		/*
3330 		 * an unknown command may be a uscsi cmd which we
3331 		 * should let go thru without mapping
3332 		 */
3333 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3334 		if (cmd->cmd_bp) {
3335 			cmd->cmd_dir = (cmd->cmd_bp->b_flags & B_READ) ?
3336 			    CBW_DIR_IN : CBW_DIR_OUT;
3337 			cmd->cmd_xfercount = cmd->cmd_bp->b_bcount;
3338 		}
3339 
3340 		break;
3341 	} /* end of switch */
3342 
3343 	USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3344 	    "scsa2usb_handle_scsi_cmd_sub_class: opcode = 0x%x count = 0x%lx",
3345 	    pkt->pkt_cdbp[SCSA2USB_OPCODE], cmd->cmd_xfercount);
3346 
3347 	cmd->cmd_total_xfercount = cmd->cmd_xfercount;
3348 
3349 	return (SCSA2USB_TRANSPORT);
3350 }
3351 
3352 
3353 /*
3354  * scsa2usb_do_tur is performed before READ CAPACITY command is issued.
3355  * It returns media status, 0 for media ready, -1 for media not ready
3356  * or other errors.
3357  */
3358 static int
3359 scsa2usb_do_tur(scsa2usb_state_t *scsa2usbp, struct scsi_address *ap)
3360 {
3361 	struct scsi_pkt		*pkt;
3362 	scsa2usb_cmd_t		*turcmd;
3363 	int			rval = -1;
3364 
3365 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3366 	    "scsa2usb_do_tur:");
3367 
3368 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3369 
3370 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
3371 	if ((pkt = scsi_init_pkt(ap, NULL, NULL, CDB_GROUP0, 1,
3372 	    PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL)) == NULL) {
3373 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
3374 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
3375 		    scsa2usbp->scsa2usb_log_handle,
3376 		    "scsa2usb_do_tur: init pkt failed");
3377 
3378 		return (rval);
3379 	}
3380 
3381 	RQ_MAKECOM_G0(pkt, FLAG_HEAD | FLAG_NODISCON,
3382 	    (char)SCMD_TEST_UNIT_READY, 0, 0);
3383 
3384 	pkt->pkt_comp = NULL;
3385 	pkt->pkt_time = PKT_DEFAULT_TIMEOUT;
3386 	turcmd = PKT2CMD(pkt);
3387 
3388 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
3389 	scsa2usb_prepare_pkt(scsa2usbp, turcmd->cmd_pkt);
3390 
3391 	if (scsa2usb_cmd_transport(scsa2usbp, turcmd) != TRAN_ACCEPT) {
3392 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
3393 		    scsa2usbp->scsa2usb_log_handle,
3394 		    "scsa2usb_do_tur: cmd transport failed, "
3395 		    "pkt_reason=0x%x", turcmd->cmd_pkt->pkt_reason);
3396 	} else if (*(turcmd->cmd_pkt->pkt_scbp) != STATUS_GOOD) {
3397 		/*
3398 		 * Theoretically, the sense data should be retrieved and
3399 		 * sense key be checked when check condition happens. If
3400 		 * the sense key is UNIT ATTENTION, TEST UNIT READY cmd
3401 		 * needs to be sent again to clear the UNIT ATTENTION and
3402 		 * another TUR to be sent to get the real media status.
3403 		 * But the AMI virtual floppy device simply cannot recover
3404 		 * from UNIT ATTENTION by re-sending a TUR cmd, so it
3405 		 * doesn't make any difference whether to check sense key
3406 		 * or not. Just ignore sense key checking here and assume
3407 		 * the device is NOT READY.
3408 		 */
3409 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
3410 		    scsa2usbp->scsa2usb_log_handle,
3411 		    "scsa2usb_do_tur: media not ready");
3412 	} else {
3413 		rval = 0;
3414 	}
3415 
3416 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
3417 	scsi_destroy_pkt(pkt);
3418 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
3419 
3420 	return (rval);
3421 }
3422 
3423 
3424 /*
3425  * scsa2usb_check_ufi_blacklist_attrs:
3426  *	validate "scsa2usb_blacklist_attrs" (see scsa2usb.h)
3427  *	if blacklisted attrs match accept the request
3428  *	attributes checked are:-
3429  *		SCSA2USB_ATTRS_GET_CONF
3430  *		SCSA2USB_ATTRS_GET_PERF
3431  *		SCSA2USB_ATTRS_GET_START_STOP
3432  */
3433 static int
3434 scsa2usb_check_ufi_blacklist_attrs(scsa2usb_state_t *scsa2usbp, uchar_t opcode,
3435     scsa2usb_cmd_t *cmd)
3436 {
3437 	int	rval = SCSA2USB_TRANSPORT;
3438 
3439 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3440 
3441 	switch (opcode) {
3442 	case SCMD_PRIN:
3443 	case SCMD_PROUT:
3444 		rval = SCSA2USB_JUST_ACCEPT;
3445 		break;
3446 	case SCMD_MODE_SENSE:
3447 	case SCMD_MODE_SELECT:
3448 		if (cmd->cmd_bp) {
3449 			cmd->cmd_pkt->pkt_resid = cmd->cmd_bp->b_bcount;
3450 		}
3451 		scsa2usb_force_invalid_request(scsa2usbp, cmd);
3452 		rval = SCSA2USB_JUST_ACCEPT;
3453 		break;
3454 	case SCMD_GET_CONFIGURATION:
3455 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_GET_CONF)) {
3456 			rval = SCSA2USB_JUST_ACCEPT;
3457 		}
3458 		break;
3459 	case SCMD_GET_PERFORMANCE:
3460 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_GET_PERF)) {
3461 			rval = SCSA2USB_JUST_ACCEPT;
3462 		}
3463 		break;
3464 	case SCMD_START_STOP:
3465 		/*
3466 		 * some CB/CBI devices don't have mechanics that spin the
3467 		 * media up and down. So, it doesn't make much sense
3468 		 * to issue this cmd to those devices.
3469 		 */
3470 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_START_STOP)) {
3471 			rval = SCSA2USB_JUST_ACCEPT;
3472 		}
3473 		break;
3474 	case SCMD_READ_CAPACITY:
3475 		/*
3476 		 * Some devices don't support READ CAPACITY command
3477 		 * when media is not ready. Need to check media status
3478 		 * before issuing the cmd to such device.
3479 		 */
3480 		if (!(scsa2usbp->scsa2usb_attrs &
3481 		    SCSA2USB_ATTRS_NO_MEDIA_CHECK)) {
3482 			struct scsi_pkt *pkt = cmd->cmd_pkt;
3483 
3484 			ASSERT(scsa2usbp->scsa2usb_cur_pkt == pkt);
3485 			scsa2usbp->scsa2usb_cur_pkt = NULL;
3486 
3487 			if (scsa2usb_do_tur(scsa2usbp,
3488 			    &pkt->pkt_address) != 0) {
3489 				/* media not ready, force cmd invalid */
3490 				if (cmd->cmd_bp) {
3491 					cmd->cmd_pkt->pkt_resid =
3492 					    cmd->cmd_bp->b_bcount;
3493 				}
3494 				scsa2usb_force_invalid_request(scsa2usbp, cmd);
3495 				rval = SCSA2USB_JUST_ACCEPT;
3496 			}
3497 
3498 			scsa2usbp->scsa2usb_cur_pkt = pkt;
3499 		}
3500 		break;
3501 	default:
3502 		break;
3503 	}
3504 
3505 	return (rval);
3506 }
3507 
3508 
3509 /*
3510  * scsa2usb_handle_ufi_subclass_cmd:
3511  *	prepare a UFI cmd
3512  *	returns SCSA2USB_TRANSPORT, SCSA2USB_REJECT
3513  */
3514 int
3515 scsa2usb_handle_ufi_subclass_cmd(scsa2usb_state_t *scsa2usbp,
3516     scsa2usb_cmd_t *cmd, struct scsi_pkt *pkt)
3517 {
3518 	uchar_t opcode =  pkt->pkt_cdbp[0];
3519 
3520 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3521 	    "scsa2usb_handle_ufi_subclass_cmd: cmd = 0x%p pkt = 0x%p",
3522 	    (void *)cmd, (void *)pkt);
3523 
3524 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3525 
3526 	bzero(&cmd->cmd_cdb, SCSI_CDB_SIZE);
3527 	cmd->cmd_cdb[SCSA2USB_OPCODE] = opcode;   /* Set the opcode */
3528 	cmd->cmd_cdb[SCSA2USB_LUN] = pkt->pkt_cdbp[1];
3529 
3530 	/*
3531 	 * decode and convert the packet if necessary
3532 	 * for most cmds, we can bcopy the cdb
3533 	 */
3534 	switch (opcode) {
3535 	case SCMD_FORMAT:
3536 		/* if parameter list is specified */
3537 		if (pkt->pkt_cdbp[1] & 0x10) {
3538 			cmd->cmd_xfercount =
3539 			    (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3540 			cmd->cmd_dir = USB_EP_DIR_OUT;
3541 			cmd->cmd_actual_len = CDB_GROUP5;
3542 		}
3543 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3544 		break;
3545 	case SCMD_INQUIRY:
3546 		cmd->cmd_dir = USB_EP_DIR_IN;
3547 		cmd->cmd_actual_len = CDB_GROUP0;
3548 		cmd->cmd_cdb[SCSA2USB_LBA_0] = pkt->pkt_cdbp[2];
3549 		cmd->cmd_cdb[SCSA2USB_LBA_2] = cmd->cmd_xfercount =
3550 		    min(SCSA2USB_MAX_INQ_LEN,
3551 		    cmd->cmd_bp ? cmd->cmd_bp->b_bcount : 0);
3552 		break;
3553 	case SCMD_READ_CAPACITY:
3554 		cmd->cmd_dir = USB_EP_DIR_IN;
3555 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3556 		cmd->cmd_xfercount = sizeof (scsa2usb_read_cap_t);
3557 		break;
3558 	case SCMD_REQUEST_SENSE:
3559 		cmd->cmd_dir = USB_EP_DIR_IN;
3560 		cmd->cmd_xfercount = pkt->pkt_cdbp[4];
3561 		cmd->cmd_cdb[SCSA2USB_LBA_2] = pkt->pkt_cdbp[4];
3562 		cmd->cmd_actual_len = CDB_GROUP0;
3563 		break;
3564 
3565 	/*
3566 	 * do not convert SCMD_MODE_SENSE/SELECT because the
3567 	 * mode header is different as well
3568 	 */
3569 
3570 	/*
3571 	 * see usb_bulkonly.c for comments on the next set of commands
3572 	 */
3573 	case SCMD_READ:
3574 	case SCMD_WRITE:
3575 	case SCMD_READ_G1:
3576 	case SCMD_WRITE_G1:
3577 	case SCMD_READ_G5:
3578 	case SCMD_WRITE_G5:
3579 	case SCMD_READ_LONG:
3580 	case SCMD_WRITE_LONG:
3581 	case SCMD_READ_CD:
3582 
3583 		return (scsa2usb_rw_transport(scsa2usbp, pkt));
3584 
3585 	case SCMD_TEST_UNIT_READY:
3586 		/*
3587 		 * Some CB/CBI devices may not support TUR.
3588 		 */
3589 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3590 		break;
3591 	case SCMD_READ_FORMAT_CAP:
3592 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3593 		cmd->cmd_dir = USB_EP_DIR_IN;
3594 		cmd->cmd_actual_len = CDB_GROUP1;
3595 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3596 		break;
3597 	case SCMD_WRITE_VERIFY:
3598 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3599 		cmd->cmd_dir = USB_EP_DIR_OUT;
3600 		cmd->cmd_actual_len = CDB_GROUP1;
3601 		cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8];
3602 		break;
3603 	case SCMD_START_STOP:
3604 		/* A larger timeout is needed for 'flaky' CD-RW devices */
3605 		if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_BIG_TIMEOUT)) {
3606 			cmd->cmd_timeout = max(cmd->cmd_timeout,
3607 			    20 * SCSA2USB_BULK_PIPE_TIMEOUT);
3608 		}
3609 		/* FALLTHRU */
3610 	default:
3611 		/*
3612 		 * all other commands don't need special mapping
3613 		 */
3614 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3615 		if (cmd->cmd_bp) {
3616 			cmd->cmd_dir = (cmd->cmd_bp->b_flags & B_READ) ?
3617 			    CBW_DIR_IN : CBW_DIR_OUT;
3618 			cmd->cmd_xfercount = cmd->cmd_bp->b_bcount;
3619 		}
3620 		break;
3621 
3622 	} /* end of switch */
3623 
3624 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3625 	    "scsa2usb_handle_ufi_subclass_cmd: opcode = 0x%x count = 0x%lx",
3626 	    opcode, cmd->cmd_xfercount);
3627 
3628 	cmd->cmd_total_xfercount = cmd->cmd_xfercount;
3629 
3630 	return (SCSA2USB_TRANSPORT);
3631 }
3632 
3633 
3634 /*
3635  * scsa2usb_rw_transport:
3636  *	Handle splitting READ and WRITE requests to the
3637  *	device to a size that the host controller allows.
3638  *
3639  *	returns TRAN_* values and not USB_SUCCESS/FAILURE
3640  *
3641  * To support CD-R/CD-RW/DVD media, we need to support a
3642  * variety of block sizes for the different types of CD
3643  * data (audio, data, video, CD-XA, yellowbook, redbook etc.)
3644  *
3645  * Some of the block sizes used are:- 512, 1k, 2k, 2056, 2336
3646  * 2340, 2352, 2368, 2448, 2646, 2647 etc.
3647  *
3648  * NOTE: the driver could be entertaining a SCSI CDB that uses
3649  * any of the above listed block sizes at a given time, and a
3650  * totally different block size at any other given time for a
3651  * different CDB.
3652  *
3653  * We need to compute block size every time and figure out
3654  * matching LBA and LEN accordingly.
3655  *
3656  * Also UHCI has a limitation that it can only xfer 32k at a
3657  * given time. So, with "odd" sized blocks and a limitation of
3658  * how much we can xfer per shot, we need to compute xfer_count
3659  * as well each time.
3660  *
3661  * The same computation is also done in the function
3662  * scsa2usb_setup_next_xfer().	To save computing block_size in
3663  * this function, I am saving block_size in "cmd" now.
3664  */
3665 int
3666 scsa2usb_rw_transport(scsa2usb_state_t *scsa2usbp, struct scsi_pkt *pkt)
3667 {
3668 	scsa2usb_cmd_t *cmd = PKT2CMD(pkt);
3669 	int lba, dir, opcode;
3670 	struct buf *bp = cmd->cmd_bp;
3671 	size_t len, xfer_count;
3672 	size_t blk_size;	/* calculate the block size to be used */
3673 	int sz;
3674 
3675 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3676 	    "scsa2usb_rw_transport:");
3677 
3678 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3679 
3680 	opcode = pkt->pkt_cdbp[0];
3681 	blk_size  = scsa2usbp->scsa2usb_lbasize[pkt->pkt_address.a_lun];
3682 						/* set to default */
3683 
3684 	switch (opcode) {
3685 	case SCMD_READ:
3686 		/*
3687 		 * Note that READ/WRITE(6) are not supported by the drive.
3688 		 * convert it into a 10 byte read/write.
3689 		 */
3690 		lba = SCSA2USB_LBA_6BYTE(pkt);
3691 		len = SCSA2USB_LEN_6BYTE(pkt);
3692 		opcode = SCMD_READ_G1;	/* Overwrite it w/ byte 10 cmd val */
3693 		dir = USB_EP_DIR_IN;
3694 		break;
3695 	case SCMD_WRITE:
3696 		lba = SCSA2USB_LBA_6BYTE(pkt);
3697 		len = SCSA2USB_LEN_6BYTE(pkt);
3698 		opcode = SCMD_WRITE_G1;	/* Overwrite it w/ byte 10 cmd val */
3699 		dir = USB_EP_DIR_OUT;
3700 		break;
3701 	case SCMD_READ_G1:
3702 	case SCMD_READ_LONG:
3703 		lba = SCSA2USB_LBA_10BYTE(pkt);
3704 		len = SCSA2USB_LEN_10BYTE(pkt);
3705 		dir = USB_EP_DIR_IN;
3706 		break;
3707 	case SCMD_WRITE_G1:
3708 	case SCMD_WRITE_LONG:
3709 		lba = SCSA2USB_LBA_10BYTE(pkt);
3710 		len = SCSA2USB_LEN_10BYTE(pkt);
3711 		dir = USB_EP_DIR_OUT;
3712 		if (len) {
3713 			sz = SCSA2USB_CDRW_BLKSZ(bp ? bp->b_bcount : 0, len);
3714 			if (SCSA2USB_VALID_CDRW_BLKSZ(sz)) {
3715 				blk_size = sz;	/* change it accordingly */
3716 			}
3717 		}
3718 		break;
3719 	case SCMD_READ_CD:
3720 		lba = SCSA2USB_LBA_10BYTE(pkt);
3721 		len = SCSA2USB_LEN_READ_CD(pkt);
3722 		dir = USB_EP_DIR_IN;
3723 
3724 		/* Figure out the block size */
3725 		blk_size = scsa2usb_read_cd_blk_size(pkt->pkt_cdbp[1] >> 2);
3726 		break;
3727 	case SCMD_READ_G5:
3728 		lba = SCSA2USB_LBA_12BYTE(pkt);
3729 		len = SCSA2USB_LEN_12BYTE(pkt);
3730 		dir = USB_EP_DIR_IN;
3731 		break;
3732 	case SCMD_WRITE_G5:
3733 		lba = SCSA2USB_LBA_12BYTE(pkt);
3734 		len = SCSA2USB_LEN_12BYTE(pkt);
3735 		dir = USB_EP_DIR_OUT;
3736 		break;
3737 	}
3738 
3739 	cmd->cmd_total_xfercount = xfer_count = len * blk_size;
3740 
3741 	/* reduce xfer count if necessary */
3742 	if (blk_size &&
3743 	    (xfer_count > scsa2usbp->scsa2usb_max_bulk_xfer_size)) {
3744 		/*
3745 		 * For CD-RW devices reduce the xfer count based
3746 		 * on the block size used by these devices. The
3747 		 * block size could change for READ_CD and WRITE
3748 		 * opcodes.
3749 		 *
3750 		 * Also as UHCI allows a max xfer of 32k at a time;
3751 		 * compute the xfer_count based on the new block_size.
3752 		 *
3753 		 * The len part of the cdb changes as a result of that.
3754 		 */
3755 		if (SCSA2USB_VALID_CDRW_BLKSZ(blk_size)) {
3756 			xfer_count = ((scsa2usbp->scsa2usb_max_bulk_xfer_size/
3757 			    blk_size) * blk_size);
3758 			len = xfer_count/blk_size;
3759 			xfer_count = blk_size * len;
3760 		} else {
3761 			xfer_count = scsa2usbp->scsa2usb_max_bulk_xfer_size;
3762 			len = xfer_count/blk_size;
3763 		}
3764 	}
3765 
3766 	cmd->cmd_xfercount = xfer_count;
3767 	cmd->cmd_dir = (uchar_t)dir;
3768 	cmd->cmd_blksize = (int)blk_size;
3769 
3770 	/*
3771 	 * Having figure out the 'partial' xfer len based on he
3772 	 * block size; fill it in to the cmd->cmd_cdb
3773 	 */
3774 	cmd->cmd_cdb[SCSA2USB_OPCODE] = (uchar_t)opcode;
3775 	switch (opcode) {
3776 	case SCMD_READ_CD:
3777 		bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen);
3778 		scsa2usb_fill_up_ReadCD_cdb_len(cmd, len, CDB_GROUP5);
3779 		break;
3780 	case SCMD_WRITE_G5:
3781 	case SCMD_READ_G5:
3782 		scsa2usb_fill_up_12byte_cdb_len(cmd, len, CDB_GROUP5);
3783 		break;
3784 	default:
3785 		scsa2usb_fill_up_cdb_len(cmd, len);
3786 		cmd->cmd_actual_len = CDB_GROUP1;
3787 		break;
3788 	}
3789 
3790 	scsa2usb_fill_up_cdb_lba(cmd, lba);
3791 
3792 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3793 	    "bcount=0x%lx lba=0x%x len=0x%lx xfercount=0x%lx total=0x%lx",
3794 	    bp ? bp->b_bcount : 0, lba, len, cmd->cmd_xfercount,
3795 	    cmd->cmd_total_xfercount);
3796 
3797 	/* Set the timeout value as per command request */
3798 	if ((opcode == SCMD_WRITE_G1) && SCSA2USB_VALID_CDRW_BLKSZ(blk_size)) {
3799 		/*
3800 		 * We increase the time as CD-RW writes have two things
3801 		 * to do. After writing out the data to the media, a
3802 		 * TOC needs to be filled up at the beginning of the media
3803 		 * This is when the write gets "finalized".
3804 		 * Hence the actual write could take longer than the
3805 		 * value specified in cmd->cmd_timeout.
3806 		 */
3807 		cmd->cmd_timeout *= 4;
3808 
3809 		USB_DPRINTF_L4(DPRINT_MASK_SCSA,
3810 		    scsa2usbp->scsa2usb_log_handle,
3811 		    "new timeout value = 0x%x", cmd->cmd_timeout);
3812 	}
3813 
3814 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3815 	    "lba 0x%x len 0x%lx xfercount 0x%lx total 0x%lx",
3816 	    lba, len, cmd->cmd_xfercount, cmd->cmd_total_xfercount);
3817 
3818 	return (SCSA2USB_TRANSPORT);
3819 }
3820 
3821 
3822 /*
3823  * scsa2usb_setup_next_xfer:
3824  *	For READs and WRITEs we split up the transfer in terms of
3825  *	HCD understood units. This function handles the split transfers.
3826  *
3827  * See comments in the previous function scsa2usb_rw_transport
3828  *
3829  * The lba computation was being done based on scsa2usb_max_bulk_xfer_size
3830  * earlier. With CD-RW devices, the xfer_count and the block_size may
3831  * no longer be a multiple of scsa2usb_max_bulk_xfer_size. So compute
3832  * xfer_count all over again. Adjust lba, based on the previous requests'
3833  * len. Find out the len and add it to cmd->cmd_lba to get the new lba
3834  */
3835 void
3836 scsa2usb_setup_next_xfer(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd)
3837 {
3838 	int xfer_len = min(scsa2usbp->scsa2usb_max_bulk_xfer_size,
3839 	    cmd->cmd_total_xfercount);
3840 	int cdb_len;
3841 	size_t blk_size;
3842 
3843 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
3844 
3845 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3846 	    "scsa2usb_setup_next_xfer: opcode = 0x%x lba = 0x%x "
3847 	    "total count = 0x%lx", cmd->cmd_cdb[SCSA2USB_OPCODE],
3848 	    cmd->cmd_lba, cmd->cmd_total_xfercount);
3849 
3850 	ASSERT(cmd->cmd_total_xfercount > 0);
3851 	cmd->cmd_xfercount = xfer_len;
3852 	blk_size = scsa2usbp->scsa2usb_lbasize[
3853 	    cmd->cmd_pkt->pkt_address.a_lun];
3854 
3855 	/*
3856 	 * For CD-RW devices reduce the xfer count based on the
3857 	 * block_size used by these devices. See changes below
3858 	 * where xfer_count is being adjusted.
3859 	 *
3860 	 * Also adjust len/lba based on the block_size and xfer_count.
3861 	 * NOTE: Always calculate lba first, as it based on previous
3862 	 * commands' values.
3863 	 */
3864 	switch (cmd->cmd_cdb[SCSA2USB_OPCODE]) {
3865 	case SCMD_READ_CD:
3866 		/* calculate lba = current_lba + len_of_prev_cmd */
3867 		cmd->cmd_lba += (cmd->cmd_cdb[6] << 16) +
3868 		    (cmd->cmd_cdb[7] << 8) + cmd->cmd_cdb[8];
3869 		cdb_len = xfer_len/cmd->cmd_blksize;
3870 		cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_2] = (uchar_t)cdb_len;
3871 		/* re-adjust xfer count */
3872 		cmd->cmd_xfercount = cdb_len * cmd->cmd_blksize;
3873 		break;
3874 	case SCMD_WRITE_G5:
3875 	case SCMD_READ_G5:
3876 		/* calculate lba = current_lba + len_of_prev_cmd */
3877 		cmd->cmd_lba += (cmd->cmd_cdb[6] << 24) +
3878 		    (cmd->cmd_cdb[7] << 16) + (cmd->cmd_cdb[8] << 8) +
3879 		    cmd->cmd_cdb[9];
3880 		if (blk_size) {
3881 			xfer_len /= blk_size;
3882 		}
3883 		scsa2usb_fill_up_12byte_cdb_len(cmd, xfer_len, CDB_GROUP5);
3884 		break;
3885 	case SCMD_WRITE_G1:
3886 	case SCMD_WRITE_LONG:
3887 		/* calculate lba = current_lba + len_of_prev_cmd */
3888 		cmd->cmd_lba += (cmd->cmd_cdb[7] << 8) + cmd->cmd_cdb[8];
3889 		if (SCSA2USB_VALID_CDRW_BLKSZ(cmd->cmd_blksize)) {
3890 			blk_size = cmd->cmd_blksize;
3891 		}
3892 		cdb_len = xfer_len/blk_size;
3893 		scsa2usb_fill_up_cdb_len(cmd, cdb_len);
3894 		/* re-adjust xfer count */
3895 		cmd->cmd_xfercount = cdb_len * blk_size;
3896 		break;
3897 	default:
3898 		if (blk_size) {
3899 			xfer_len /= blk_size;
3900 		}
3901 		scsa2usb_fill_up_cdb_len(cmd, xfer_len);
3902 		cmd->cmd_lba += scsa2usbp->scsa2usb_max_bulk_xfer_size/blk_size;
3903 	}
3904 
3905 	/* fill in the lba */
3906 	scsa2usb_fill_up_cdb_lba(cmd, cmd->cmd_lba);
3907 
3908 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3909 	    "scsa2usb_setup_next_xfer:\n\tlba = 0x%x xfer_len = 0x%x "
3910 	    "xfercount = 0x%lx total = 0x%lx", cmd->cmd_lba, xfer_len,
3911 	    cmd->cmd_xfercount, cmd->cmd_total_xfercount);
3912 }
3913 
3914 
3915 /*
3916  * take one request from the lun's waitQ and transport it
3917  */
3918 static void
3919 scsa2usb_transport_request(scsa2usb_state_t *scsa2usbp, uint_t lun)
3920 {
3921 	int			rval;
3922 	struct scsi_pkt		*pkt;
3923 	struct scsa2usb_cmd	*cmd, *arqcmd;
3924 
3925 	if ((cmd = (scsa2usb_cmd_t *)
3926 	    usba_rm_first_pvt_from_list(
3927 	    &scsa2usbp->scsa2usb_waitQ[lun])) == NULL) {
3928 
3929 		return;
3930 	}
3931 	pkt = cmd->cmd_pkt;
3932 
3933 	/*
3934 	 * if device has been disconnected, just complete it
3935 	 */
3936 	if (scsa2usbp->scsa2usb_dev_state == USB_DEV_DISCONNECTED) {
3937 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3938 		    "device not accessible");
3939 		pkt->pkt_reason = CMD_DEV_GONE;
3940 		SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
3941 		scsa2usb_pkt_completion(scsa2usbp, pkt);
3942 
3943 		return;
3944 	}
3945 
3946 	USB_DPRINTF_L4(DPRINT_MASK_SCSA,
3947 	    scsa2usbp->scsa2usb_log_handle,
3948 	    "scsa2usb_transport_request: cmd=0x%p bp=0x%p addr=0x%p",
3949 	    (void *)cmd, (void *)cmd->cmd_bp,
3950 	    (void *)(cmd->cmd_bp ? cmd->cmd_bp->b_un.b_addr : NULL));
3951 
3952 	rval = scsa2usb_cmd_transport(scsa2usbp, cmd);
3953 
3954 	USB_DPRINTF_L3(DPRINT_MASK_SCSA,
3955 	    scsa2usbp->scsa2usb_log_handle,
3956 	    "scsa2usb_transport_request: transport rval = %d",
3957 	    rval);
3958 
3959 	if (scsa2usbp->scsa2usb_cur_pkt == NULL) {
3960 
3961 		return;
3962 	}
3963 
3964 	ASSERT(pkt == scsa2usbp->scsa2usb_cur_pkt);
3965 
3966 	if (ddi_in_panic()) {
3967 		pkt->pkt_reason = CMD_CMPLT;
3968 		scsa2usb_pkt_completion(scsa2usbp, pkt);
3969 
3970 		return;
3971 	}
3972 
3973 	/*
3974 	 * start an auto-request sense iff
3975 	 * there was a check condition, we have enough
3976 	 * space in the status block, and we have not
3977 	 * faked an auto request sense
3978 	 */
3979 	if ((*(pkt->pkt_scbp) == STATUS_CHECK) &&
3980 	    (cmd->cmd_scblen >= sizeof (struct scsi_arq_status)) &&
3981 	    ((pkt->pkt_state & STATE_ARQ_DONE) == 0) &&
3982 	    (scsa2usb_create_arq_pkt(scsa2usbp,
3983 	    &pkt->pkt_address) == USB_SUCCESS)) {
3984 		arqcmd = scsa2usbp->scsa2usb_arq_cmd;
3985 
3986 		/*
3987 		 * copy the timeout from the
3988 		 * original packet
3989 		 * for lack of a better value
3990 		 */
3991 		arqcmd->cmd_pkt->pkt_time = pkt->pkt_time;
3992 		scsa2usb_prepare_pkt(scsa2usbp,
3993 		    arqcmd->cmd_pkt);
3994 
3995 		scsa2usbp->scsa2usb_cur_pkt = NULL;
3996 		if (scsa2usb_cmd_transport(
3997 		    scsa2usbp, arqcmd) == TRAN_ACCEPT) {
3998 
3999 			/* finish w/ this packet */
4000 			scsa2usb_complete_arq_pkt(
4001 			    scsa2usbp, arqcmd->cmd_pkt, cmd,
4002 			    scsa2usbp->scsa2usb_arq_bp);
4003 
4004 			/*
4005 			 * we have valid request sense
4006 			 * data so clear the pkt_reason
4007 			 */
4008 			pkt->pkt_reason = CMD_CMPLT;
4009 		}
4010 		scsa2usbp->scsa2usb_cur_pkt = pkt;
4011 		scsa2usb_delete_arq_pkt(scsa2usbp);
4012 	}
4013 
4014 	if ((rval != TRAN_ACCEPT) &&
4015 	    (pkt->pkt_reason == CMD_CMPLT)) {
4016 		pkt->pkt_reason = CMD_TRAN_ERR;
4017 	}
4018 
4019 	SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
4020 	scsa2usb_pkt_completion(scsa2usbp, pkt);
4021 
4022 	ASSERT(scsa2usbp->scsa2usb_cur_pkt == NULL);
4023 }
4024 
4025 
4026 /*
4027  * scsa2usb_work_thread:
4028  *	The taskq thread that kicks off the transport (BO and CB/CBI)
4029  */
4030 static void
4031 scsa2usb_work_thread(void *arg)
4032 {
4033 	scsa2usb_state_t	*scsa2usbp = (scsa2usb_state_t *)arg;
4034 	uint_t			lun;
4035 	uint_t			count;
4036 
4037 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4038 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4039 	    "scsa2usb_work_thread start: thread_id=0x%p",
4040 	    (void *)scsa2usbp->scsa2usb_work_thread_id);
4041 
4042 	ASSERT(scsa2usbp->scsa2usb_work_thread_id == (kthread_t *)1);
4043 	scsa2usbp->scsa2usb_work_thread_id = curthread;
4044 
4045 	/* exclude ugen accesses */
4046 	while (scsa2usbp->scsa2usb_transport_busy) {
4047 		cv_wait(&scsa2usbp->scsa2usb_transport_busy_cv,
4048 		    &scsa2usbp->scsa2usb_mutex);
4049 	}
4050 	ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0);
4051 	scsa2usbp->scsa2usb_transport_busy++;
4052 	scsa2usbp->scsa2usb_busy_thread = curthread;
4053 
4054 	scsa2usb_raise_power(scsa2usbp);
4055 
4056 	/* reopen the pipes if necessary */
4057 	(void) scsa2usb_open_usb_pipes(scsa2usbp);
4058 
4059 	for (;;) {
4060 		ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0);
4061 		for (lun = 0; lun < scsa2usbp->scsa2usb_n_luns; lun++) {
4062 			scsa2usb_transport_request(scsa2usbp, lun);
4063 		}
4064 		count = 0;
4065 		for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
4066 			count += usba_list_entry_count(
4067 			    &scsa2usbp->scsa2usb_waitQ[lun]);
4068 		}
4069 
4070 		if (count == 0) {
4071 
4072 			break;
4073 		}
4074 	}
4075 
4076 	scsa2usbp->scsa2usb_work_thread_id = 0;
4077 
4078 	ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0);
4079 
4080 	scsa2usbp->scsa2usb_transport_busy--;
4081 	scsa2usbp->scsa2usb_busy_thread = NULL;
4082 	cv_signal(&scsa2usbp->scsa2usb_transport_busy_cv);
4083 
4084 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4085 	    "scsa2usb_work_thread: exit");
4086 
4087 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4088 
4089 	scsa2usb_pm_idle_component(scsa2usbp);
4090 }
4091 
4092 
4093 /*
4094  * scsa2usb_flush_waitQ:
4095  *	empties the entire waitQ with errors asap.
4096  *
4097  * It is called from scsa2usb_scsi_reset and scsa2usb_panic_callb.
4098  * If the device is reset; we should empty the waitQ right away.
4099  * If the system has paniced; we should empty the waitQ right away.
4100  *
4101  * CPR suspend will only succeed if device is idle. No need to call
4102  * this function for CPR suspend case.
4103  */
4104 static void
4105 scsa2usb_flush_waitQ(scsa2usb_state_t *scsa2usbp, uint_t lun,
4106     uchar_t error)
4107 {
4108 	struct scsi_pkt		*pkt;
4109 	struct scsa2usb_cmd	*cmd;
4110 	usba_list_entry_t	head;
4111 
4112 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4113 
4114 	usba_move_list(&scsa2usbp->scsa2usb_waitQ[lun], &head,
4115 	    scsa2usbp->scsa2usb_dev_data->dev_iblock_cookie);
4116 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4117 
4118 	while ((cmd = (scsa2usb_cmd_t *)usba_rm_first_pvt_from_list(&head)) !=
4119 	    NULL) {
4120 		pkt = cmd->cmd_pkt;
4121 		pkt->pkt_reason = error;	/* set error */
4122 
4123 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4124 		scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_DO_COMP;
4125 		scsa2usb_pkt_completion(scsa2usbp, pkt);
4126 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4127 	} /* end of while */
4128 }
4129 
4130 
4131 /*
4132  * scsa2usb_do_inquiry is performed before INIT CHILD and we have
4133  * to fake a few things normally done by SCSA
4134  */
4135 static void
4136 scsa2usb_do_inquiry(scsa2usb_state_t *scsa2usbp, uint_t target, uint_t lun)
4137 {
4138 	struct buf	*bp;
4139 	struct scsi_pkt *pkt;
4140 	struct scsi_address ap;
4141 	int		len = SCSA2USB_MAX_INQ_LEN;
4142 
4143 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4144 	    "scsa2usb_do_inquiry: %d bytes", len);
4145 
4146 	/* is it inquiry-challenged? */
4147 	if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_INQUIRY)) {
4148 		scsa2usb_fake_inquiry(scsa2usbp,
4149 		    &scsa2usbp->scsa2usb_lun_inquiry[lun]);
4150 		return;
4151 	}
4152 
4153 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4154 
4155 	bzero(&ap, sizeof (struct scsi_address));
4156 	ap.a_hba_tran = scsa2usbp->scsa2usb_tran;
4157 	ap.a_target = (ushort_t)target;
4158 	ap.a_lun = (uchar_t)lun;
4159 
4160 	/* limit inquiry to 36 bytes */
4161 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4162 	if ((bp = scsi_alloc_consistent_buf(&ap, (struct buf *)NULL,
4163 	    len, B_READ, SLEEP_FUNC, NULL)) == NULL) {
4164 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4165 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4166 		    scsa2usbp->scsa2usb_log_handle,
4167 		    "scsa2usb_do_inquiry: failed");
4168 
4169 		return;
4170 	}
4171 
4172 	pkt = scsi_init_pkt(&ap, NULL, bp, CDB_GROUP0, 1,
4173 	    PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL);
4174 
4175 	RQ_MAKECOM_G0(pkt, FLAG_NOINTR, (char)SCMD_INQUIRY, 0, (char)len);
4176 
4177 	pkt->pkt_comp = NULL;
4178 	pkt->pkt_time = 5;
4179 	bzero(bp->b_un.b_addr, len);
4180 
4181 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4182 	    "scsa2usb_do_inquiry:INQUIRY");
4183 
4184 	(void) scsi_transport(pkt);
4185 
4186 	if (pkt->pkt_reason) {
4187 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4188 		    scsa2usbp->scsa2usb_log_handle,
4189 		    "INQUIRY failed, cannot determine device type, "
4190 		    "pkt_reason=0x%x", pkt->pkt_reason);
4191 
4192 		/* not much hope for other cmds, reduce */
4193 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4194 		scsa2usbp->scsa2usb_attrs &=
4195 		    ~SCSA2USB_ATTRS_REDUCED_CMD;
4196 		scsa2usb_fake_inquiry(scsa2usbp,
4197 		    &scsa2usbp->scsa2usb_lun_inquiry[lun]);
4198 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4199 	}
4200 
4201 	scsi_destroy_pkt(pkt);
4202 	scsi_free_consistent_buf(bp);
4203 
4204 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4205 }
4206 
4207 
4208 /*
4209  * scsa2usb_fake_inquiry:
4210  *    build an inquiry for a given device that doesnt like inquiry
4211  *    commands.
4212  */
4213 static void
4214 scsa2usb_fake_inquiry(scsa2usb_state_t *scsa2usbp, struct scsi_inquiry *inqp)
4215 {
4216 	usb_client_dev_data_t *dev_data = scsa2usbp->scsa2usb_dev_data;
4217 	int len;
4218 
4219 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4220 	    "scsa2usb_fake_inquiry:");
4221 
4222 	bzero(inqp, sizeof (struct scsi_inquiry));
4223 	for (len = 0; len < sizeof (inqp->inq_vid); len++) {
4224 		*(inqp->inq_vid + len) = ' ';
4225 	}
4226 
4227 	for (len = 0; len < sizeof (inqp->inq_pid); len++) {
4228 		*(inqp->inq_pid + len) = ' ';
4229 	}
4230 
4231 	inqp->inq_dtype = DTYPE_DIRECT;
4232 	inqp->inq_rmb = 1;
4233 	inqp->inq_ansi = 2;
4234 	inqp->inq_rdf = RDF_SCSI2;
4235 	inqp->inq_len = sizeof (struct scsi_inquiry)-4;
4236 
4237 	/* Fill in the Vendor id/Product id strings */
4238 	if (dev_data->dev_mfg) {
4239 		if ((len = strlen(dev_data->dev_mfg)) >
4240 		    sizeof (inqp->inq_vid)) {
4241 			len = sizeof (inqp->inq_vid);
4242 		}
4243 		bcopy(dev_data->dev_mfg, inqp->inq_vid, len);
4244 	}
4245 
4246 	if (dev_data->dev_product) {
4247 		if ((len = strlen(dev_data->dev_product)) >
4248 		    sizeof (inqp->inq_pid)) {
4249 			len = sizeof (inqp->inq_pid);
4250 		}
4251 		bcopy(dev_data->dev_product, inqp->inq_pid, len);
4252 	}
4253 
4254 	/* Set the Revision to the Device */
4255 	inqp->inq_revision[0] = 0x30 +
4256 	    ((dev_data->dev_descr->bcdDevice>>12) & 0xF);
4257 	inqp->inq_revision[1] = 0x30 +
4258 	    ((dev_data->dev_descr->bcdDevice>>8) & 0xF);
4259 	inqp->inq_revision[2] = 0x30 +
4260 	    ((dev_data->dev_descr->bcdDevice>>4) & 0xF);
4261 	inqp->inq_revision[3] = 0x30 +
4262 	    ((dev_data->dev_descr->bcdDevice) & 0xF);
4263 }
4264 
4265 
4266 /*
4267  * scsa2usb_create_arq_pkt:
4268  *	Create and ARQ packet to get request sense data
4269  */
4270 static int
4271 scsa2usb_create_arq_pkt(scsa2usb_state_t *scsa2usbp, struct scsi_address *ap)
4272 {
4273 	struct buf *bp;
4274 	scsa2usb_cmd_t *arq_cmd;
4275 
4276 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4277 	    "scsa2usb_create_arq_pkt: scsa2usbp: %p, ap: %p",
4278 	    (void *)scsa2usbp, (void *)ap);
4279 
4280 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4281 
4282 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4283 	if ((bp = scsi_alloc_consistent_buf(ap, (struct buf *)NULL,
4284 	    SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL)) == NULL) {
4285 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4286 
4287 		return (USB_FAILURE);
4288 	}
4289 
4290 	arq_cmd = PKT2CMD(scsi_init_pkt(ap, NULL, bp, CDB_GROUP0, 1,
4291 	    PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL));
4292 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4293 
4294 	RQ_MAKECOM_G0(arq_cmd->cmd_pkt,
4295 	    FLAG_SENSING | FLAG_HEAD | FLAG_NODISCON,
4296 	    (char)SCMD_REQUEST_SENSE, 0, (char)SENSE_LENGTH);
4297 
4298 	arq_cmd->cmd_pkt->pkt_ha_private = arq_cmd;
4299 	scsa2usbp->scsa2usb_arq_cmd = arq_cmd;
4300 	scsa2usbp->scsa2usb_arq_bp = bp;
4301 	arq_cmd->cmd_pkt->pkt_comp = NULL;
4302 	bzero(bp->b_un.b_addr, SENSE_LENGTH);
4303 
4304 	return (USB_SUCCESS);
4305 }
4306 
4307 
4308 /*
4309  * scsa2usb_delete_arq_pkt:
4310  *	Destroy the ARQ packet
4311  */
4312 static void
4313 scsa2usb_delete_arq_pkt(scsa2usb_state_t *scsa2usbp)
4314 {
4315 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4316 	    "scsa2usb_delete_arq_pkt: cmd: 0x%p",
4317 	    (void *)scsa2usbp->scsa2usb_arq_cmd);
4318 
4319 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4320 
4321 	if (scsa2usbp->scsa2usb_arq_cmd != NULL) {
4322 		scsi_destroy_pkt(scsa2usbp->scsa2usb_arq_cmd->cmd_pkt);
4323 		scsi_free_consistent_buf(scsa2usbp->scsa2usb_arq_bp);
4324 	}
4325 	scsa2usbp->scsa2usb_arq_cmd = NULL;
4326 	scsa2usbp->scsa2usb_arq_bp = NULL;
4327 }
4328 
4329 
4330 /*
4331  * scsa2usb_complete_arq_pkt:
4332  *	finish processing the arq packet
4333  */
4334 static void
4335 scsa2usb_complete_arq_pkt(scsa2usb_state_t *scsa2usbp,
4336     struct scsi_pkt *pkt, scsa2usb_cmd_t *ssp, struct buf *bp)
4337 {
4338 	scsa2usb_cmd_t		*sp = pkt->pkt_ha_private;
4339 	struct scsi_arq_status	*arqp;
4340 
4341 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4342 
4343 	arqp = (struct scsi_arq_status *)(ssp->cmd_pkt->pkt_scbp);
4344 	arqp->sts_rqpkt_status = *((struct scsi_status *)
4345 	    (sp->cmd_pkt->pkt_scbp));
4346 	arqp->sts_rqpkt_reason = CMD_CMPLT;
4347 	arqp->sts_rqpkt_state |= STATE_XFERRED_DATA;
4348 	arqp->sts_rqpkt_statistics = arqp->sts_rqpkt_resid = 0;
4349 
4350 	/* is this meaningful sense data */
4351 	if (*(bp->b_un.b_addr) != 0) {
4352 		bcopy(bp->b_un.b_addr, &arqp->sts_sensedata, SENSE_LENGTH);
4353 		ssp->cmd_pkt->pkt_state |= STATE_ARQ_DONE;
4354 	}
4355 
4356 	/* we will not sense start cmd until we receive a NOT READY */
4357 	if (arqp->sts_sensedata.es_key == KEY_NOT_READY) {
4358 		scsa2usbp->scsa2usb_rcvd_not_ready = B_TRUE;
4359 	}
4360 }
4361 
4362 
4363 /*
4364  * Miscellaneous functions for any command/transport
4365  */
4366 /*
4367  * scsa2usb_open_usb_pipes:
4368  *	set up a pipe policy
4369  *	open usb bulk pipes (BO and CB/CBI)
4370  *	open usb interrupt pipe (CBI)
4371  */
4372 static int
4373 scsa2usb_open_usb_pipes(scsa2usb_state_t *scsa2usbp)
4374 {
4375 	int			rval;
4376 	usb_pipe_policy_t	policy;	/* bulk pipe policy */
4377 	size_t			sz;
4378 
4379 	ASSERT(scsa2usbp);
4380 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4381 
4382 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4383 	    "scsa2usb_open_usb_pipes: dip = 0x%p flag = 0x%x",
4384 	    (void *)scsa2usbp->scsa2usb_dip, scsa2usbp->scsa2usb_flags);
4385 
4386 	if (!(scsa2usbp->scsa2usb_flags & SCSA2USB_FLAGS_PIPES_OPENED)) {
4387 
4388 		/*
4389 		 * one pipe policy for all bulk pipes
4390 		 */
4391 		bzero(&policy, sizeof (usb_pipe_policy_t));
4392 		/* at least 2, for the normal and exceptional callbacks */
4393 		policy.pp_max_async_reqs = 1;
4394 
4395 		USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4396 		    "scsa2usb_open_usb_pipes: opening bulk pipes");
4397 
4398 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4399 
4400 		/* Open the USB bulk-in pipe */
4401 		if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip,
4402 		    &scsa2usbp->scsa2usb_bulkin_ept, &policy, USB_FLAGS_SLEEP,
4403 		    &scsa2usbp->scsa2usb_bulkin_pipe)) != USB_SUCCESS) {
4404 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4405 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4406 			    scsa2usbp->scsa2usb_log_handle,
4407 			    "scsa2usb_open_usb_pipes: bulk/in pipe open "
4408 			    " failed rval = %d", rval);
4409 
4410 			return (USB_FAILURE);
4411 		}
4412 
4413 		/* Open the bulk-out pipe  using the same policy */
4414 		if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip,
4415 		    &scsa2usbp->scsa2usb_bulkout_ept, &policy, USB_FLAGS_SLEEP,
4416 		    &scsa2usbp->scsa2usb_bulkout_pipe)) != USB_SUCCESS) {
4417 			usb_pipe_close(scsa2usbp->scsa2usb_dip,
4418 			    scsa2usbp->scsa2usb_bulkin_pipe,
4419 			    USB_FLAGS_SLEEP, NULL, NULL);
4420 
4421 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4422 			scsa2usbp->scsa2usb_bulkin_pipe = NULL;
4423 
4424 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4425 			    scsa2usbp->scsa2usb_log_handle,
4426 			    "scsa2usb_open_usb_pipes: bulk/out pipe open"
4427 			    " failed rval = %d", rval);
4428 
4429 			return (USB_FAILURE);
4430 		}
4431 
4432 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4433 
4434 		/* open interrupt pipe for CBI protocol */
4435 		if (SCSA2USB_IS_CBI(scsa2usbp)) {
4436 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4437 
4438 			if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip,
4439 			    &scsa2usbp->scsa2usb_intr_ept, &policy,
4440 			    USB_FLAGS_SLEEP, &scsa2usbp->scsa2usb_intr_pipe)) !=
4441 			    USB_SUCCESS) {
4442 				usb_pipe_close(scsa2usbp->scsa2usb_dip,
4443 				    scsa2usbp->scsa2usb_bulkin_pipe,
4444 				    USB_FLAGS_SLEEP, NULL, NULL);
4445 
4446 				usb_pipe_close(scsa2usbp->scsa2usb_dip,
4447 				    scsa2usbp->scsa2usb_bulkout_pipe,
4448 				    USB_FLAGS_SLEEP, NULL, NULL);
4449 
4450 				mutex_enter(&scsa2usbp->scsa2usb_mutex);
4451 				scsa2usbp->scsa2usb_bulkin_pipe = NULL;
4452 				scsa2usbp->scsa2usb_bulkout_pipe = NULL;
4453 
4454 				USB_DPRINTF_L2(DPRINT_MASK_SCSA,
4455 				    scsa2usbp->scsa2usb_log_handle,
4456 				    "scsa2usb_open_usb_pipes: intr pipe open"
4457 				    " failed rval = %d", rval);
4458 
4459 				return (USB_FAILURE);
4460 			}
4461 
4462 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4463 		}
4464 
4465 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4466 
4467 		/* get the max transfer size of the bulk pipe */
4468 		if (usb_pipe_get_max_bulk_transfer_size(scsa2usbp->scsa2usb_dip,
4469 		    &sz) == USB_SUCCESS) {
4470 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4471 			scsa2usbp->scsa2usb_max_bulk_xfer_size = sz;
4472 		} else {
4473 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4474 			scsa2usbp->scsa2usb_max_bulk_xfer_size = DEV_BSIZE;
4475 		}
4476 
4477 		/* limit the xfer size */
4478 		scsa2usbp->scsa2usb_max_bulk_xfer_size = min(
4479 		    scsa2usbp->scsa2usb_max_bulk_xfer_size,
4480 		    scsa2usb_max_bulk_xfer_size);
4481 
4482 		USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4483 		    "scsa2usb_open_usb_pipes: max bulk transfer size = %lx",
4484 		    scsa2usbp->scsa2usb_max_bulk_xfer_size);
4485 
4486 		/* Set the pipes opened flag */
4487 		scsa2usbp->scsa2usb_flags |= SCSA2USB_FLAGS_PIPES_OPENED;
4488 
4489 		scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_NORMAL;
4490 
4491 		/* Set the state to NONE */
4492 		scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_NONE;
4493 	}
4494 
4495 	return (USB_SUCCESS);
4496 }
4497 
4498 
4499 /*
4500  * scsa2usb_close_usb_pipes:
4501  *	close all pipes synchronously
4502  */
4503 void
4504 scsa2usb_close_usb_pipes(scsa2usb_state_t *scsa2usbp)
4505 {
4506 	usb_flags_t flags = USB_FLAGS_SLEEP;
4507 
4508 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4509 	    "scsa2usb_close_usb_pipes: scsa2usb_state = 0x%p",
4510 	    (void *)scsa2usbp);
4511 
4512 	ASSERT(scsa2usbp);
4513 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4514 
4515 	if ((scsa2usbp->scsa2usb_flags & SCSA2USB_FLAGS_PIPES_OPENED) == 0) {
4516 
4517 		return;
4518 	}
4519 
4520 	scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_CLOSING;
4521 	/* to avoid races, reset the flag first */
4522 	scsa2usbp->scsa2usb_flags &= ~SCSA2USB_FLAGS_PIPES_OPENED;
4523 
4524 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
4525 
4526 	usb_pipe_close(scsa2usbp->scsa2usb_dip,
4527 	    scsa2usbp->scsa2usb_bulkout_pipe, flags, NULL, NULL);
4528 
4529 	usb_pipe_close(scsa2usbp->scsa2usb_dip,
4530 	    scsa2usbp->scsa2usb_bulkin_pipe, flags, NULL, NULL);
4531 
4532 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
4533 	if (SCSA2USB_IS_CBI(scsa2usbp)) {
4534 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4535 		usb_pipe_close(scsa2usbp->scsa2usb_dip,
4536 		    scsa2usbp->scsa2usb_intr_pipe, flags, NULL, NULL);
4537 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4538 	}
4539 	scsa2usbp->scsa2usb_bulkout_pipe = NULL;
4540 	scsa2usbp->scsa2usb_bulkin_pipe = NULL;
4541 	scsa2usbp->scsa2usb_intr_pipe = NULL;
4542 
4543 	scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_NORMAL;
4544 }
4545 
4546 
4547 /*
4548  * scsa2usb_fill_up_cdb_lba:
4549  *	fill up command CDBs' LBA part
4550  */
4551 static void
4552 scsa2usb_fill_up_cdb_lba(scsa2usb_cmd_t *cmd, int lba)
4553 {
4554 	/* zero cdb1, lba bits so they won't get copied in the new cdb */
4555 	cmd->cmd_cdb[SCSA2USB_LUN] &= 0xE0;
4556 	cmd->cmd_cdb[SCSA2USB_LBA_0] = lba >> 24;
4557 	cmd->cmd_cdb[SCSA2USB_LBA_1] = lba >> 16;
4558 	cmd->cmd_cdb[SCSA2USB_LBA_2] = lba >> 8;
4559 	cmd->cmd_cdb[SCSA2USB_LBA_3] = (uchar_t)lba;
4560 	cmd->cmd_lba = lba;
4561 }
4562 
4563 
4564 /*
4565  * scsa2usb_fill_up_ReadCD_cdb_len:
4566  *	fill up READ_CD command CDBs' len part
4567  */
4568 static void
4569 scsa2usb_fill_up_ReadCD_cdb_len(scsa2usb_cmd_t *cmd, int len, int actual_len)
4570 {
4571 	cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_0] = len >> 16;
4572 	cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_1] = len >> 8;
4573 	cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_2] = (uchar_t)len;
4574 	cmd->cmd_actual_len = (uchar_t)actual_len;
4575 }
4576 
4577 
4578 /*
4579  * scsa2usb_fill_up_12byte_cdb_len:
4580  *	fill up generic 12-byte command CDBs' len part
4581  */
4582 static void
4583 scsa2usb_fill_up_12byte_cdb_len(scsa2usb_cmd_t *cmd, int len, int actual_len)
4584 {
4585 	cmd->cmd_cdb[6] = len >> 24;
4586 	cmd->cmd_cdb[7] = len >> 16;
4587 	cmd->cmd_cdb[8] = len >> 8;
4588 	cmd->cmd_cdb[9] = (uchar_t)len;
4589 	cmd->cmd_actual_len = (uchar_t)actual_len;
4590 }
4591 
4592 
4593 /*
4594  * scsa2usb_fill_up_cdb_len:
4595  *	fill up generic 10-byte command CDBs' len part
4596  */
4597 static void
4598 scsa2usb_fill_up_cdb_len(scsa2usb_cmd_t *cmd, int len)
4599 {
4600 	cmd->cmd_cdb[SCSA2USB_LEN_0] = len >> 8;
4601 	cmd->cmd_cdb[SCSA2USB_LEN_1] = (uchar_t)len;
4602 }
4603 
4604 
4605 /*
4606  * scsa2usb_read_cd_blk_size:
4607  *	For SCMD_READ_CD opcode (0xbe). Figure out the
4608  *	block size based on expected sector type field
4609  *	definition. See MMC SCSI Specs section 6.1.15
4610  *
4611  *	Based on the value of the "expected_sector_type"
4612  *	field, the block size could be different.
4613  */
4614 static int
4615 scsa2usb_read_cd_blk_size(uchar_t expected_sector_type)
4616 {
4617 	int blk_size;
4618 
4619 	switch (expected_sector_type) {
4620 	case READ_CD_EST_CDDA:
4621 		blk_size = CDROM_BLK_2352;
4622 		break;
4623 	case READ_CD_EST_MODE2:
4624 		blk_size = CDROM_BLK_2336;
4625 		break;
4626 	case READ_CD_EST_MODE2FORM2:
4627 		blk_size = CDROM_BLK_2324;
4628 		break;
4629 	case READ_CD_EST_MODE2FORM1:
4630 	case READ_CD_EST_ALLTYPE:
4631 	case READ_CD_EST_MODE1:
4632 	default:
4633 		blk_size = CDROM_BLK_2048;
4634 	}
4635 
4636 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, NULL, "scsa2usb_read_cd_blk_size: "
4637 	    "est = 0x%x blk_size = %d", expected_sector_type, blk_size);
4638 
4639 	return (blk_size);
4640 }
4641 
4642 
4643 /*
4644  * scsa2usb_bp_to_mblk:
4645  *	Convert a bp to mblk_t. USBA framework understands mblk_t.
4646  */
4647 static mblk_t *
4648 scsa2usb_bp_to_mblk(scsa2usb_state_t *scsa2usbp)
4649 {
4650 	size_t		size;
4651 	mblk_t		*mp;
4652 	struct buf	*bp;
4653 	scsa2usb_cmd_t	*cmd = PKT2CMD(scsa2usbp->scsa2usb_cur_pkt);
4654 
4655 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4656 	    "scsa2usb_bp_to_mblk: ");
4657 
4658 	ASSERT(scsa2usbp->scsa2usb_cur_pkt);
4659 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4660 
4661 	bp = cmd->cmd_bp;
4662 
4663 	if (bp && (bp->b_bcount > 0)) {
4664 		size = ((bp->b_bcount > cmd->cmd_xfercount) ?
4665 		    cmd->cmd_xfercount : bp->b_bcount);
4666 	} else {
4667 
4668 		return (NULL);
4669 	}
4670 
4671 	mp = esballoc_wait((uchar_t *)bp->b_un.b_addr + cmd->cmd_offset,
4672 	    size, BPRI_LO, &frnop);
4673 
4674 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4675 	    "scsa2usb_bp_to_mblk: "
4676 	    "mp=0x%p bp=0x%p pkt=0x%p off=0x%lx sz=%lu add=0x%p",
4677 	    (void *)mp, (void *)bp, (void *)scsa2usbp->scsa2usb_cur_pkt,
4678 	    cmd->cmd_offset, bp->b_bcount - cmd->cmd_offset,
4679 	    (void *)bp->b_un.b_addr);
4680 
4681 	mp->b_wptr += size;
4682 	cmd->cmd_offset += size;
4683 
4684 	return (mp);
4685 }
4686 
4687 
4688 /*
4689  * scsa2usb_handle_data_start:
4690  *	Initiate the data xfer. It could be IN/OUT direction.
4691  *
4692  *	Data IN:
4693  *		Send out the bulk-xfer request
4694  *		if rval implies STALL
4695  *			clear endpoint stall and reset bulk-in pipe
4696  *			handle data read in so far; set cmd->cmd_done
4697  *			also adjust data xfer length accordingly
4698  *		else other error
4699  *			report back to transport
4700  *			typically transport will call reset recovery
4701  *		else (no error)
4702  *			return success
4703  *
4704  *	Data OUT:
4705  *		Send out the bulk-xfer request
4706  *		if rval implies STALL
4707  *			clear endpoint stall and reset bulk-in pipe
4708  *			adjust data xfer length
4709  *		else other error
4710  *			report back to transport
4711  *			typically transport will call reset recovery
4712  *		else (no error)
4713  *			return success
4714  *
4715  *	NOTE: We call this function only if there is xfercount.
4716  */
4717 int
4718 scsa2usb_handle_data_start(scsa2usb_state_t *scsa2usbp,
4719     scsa2usb_cmd_t *cmd, usb_bulk_req_t *req)
4720 {
4721 	int		rval = USB_SUCCESS;
4722 	uint_t		ept_addr;
4723 	usb_flags_t	flags = USB_FLAGS_SLEEP;
4724 #ifdef	SCSA2USB_BULK_ONLY_TEST
4725 	usb_req_attrs_t	attrs = 0;
4726 #else
4727 	usb_req_attrs_t	attrs = USB_ATTRS_SHORT_XFER_OK;
4728 #endif
4729 
4730 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4731 	    "scsa2usb_handle_data_start: BEGIN cmd = %p, req = %p",
4732 	    (void *)cmd, (void *)req);
4733 
4734 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4735 
4736 	switch (cmd->cmd_dir) {
4737 	case USB_EP_DIR_IN:
4738 #ifdef	SCSA2USB_BULK_ONLY_TEST
4739 		/*
4740 		 * This case occurs when the host expects to receive
4741 		 * more data than the device actually transfers. Hi > Di
4742 		 */
4743 		if (scsa2usb_test_case_5) {
4744 			usb_bulk_req_t *req2;
4745 
4746 			req->bulk_len = cmd->cmd_xfercount - 1;
4747 			req->bulk_attributes = 0;
4748 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4749 			SCSA2USB_FREE_MSG(req->bulk_data);
4750 			req->bulk_data = allocb_wait(req->bulk_len, BPRI_LO,
4751 			    STR_NOSIG, NULL);
4752 
4753 			ASSERT(req->bulk_timeout);
4754 			rval = usb_pipe_bulk_xfer(
4755 			    scsa2usbp->scsa2usb_bulkin_pipe, req, flags);
4756 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4757 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4758 			    scsa2usbp->scsa2usb_log_handle, "rval = %x", rval);
4759 
4760 			req2 = scsa2usb_init_bulk_req(scsa2usbp,
4761 			    cmd->cmd_xfercount + 2,
4762 			    cmd->cmd_timeout, 0, flags);
4763 			req2->bulk_len = cmd->cmd_xfercount + 2;
4764 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4765 
4766 			ASSERT(req2->bulk_timeout);
4767 			rval = usb_pipe_bulk_xfer(
4768 			    scsa2usbp->scsa2usb_bulkin_pipe, req2, flags);
4769 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4770 
4771 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4772 			    scsa2usbp->scsa2usb_log_handle,
4773 			    "TEST 5: Hi > Di: rval = 0x%x", rval);
4774 			scsa2usb_test_case_5 = 0;
4775 			usb_free_bulk_req(req2);
4776 
4777 			return (rval);
4778 		}
4779 
4780 		/*
4781 		 * This happens when the host expects to send data to the
4782 		 * device while the device intends to send data to the host.
4783 		 */
4784 		if (scsa2usb_test_case_8 && (cmd->cmd_cdb[0] == SCMD_READ_G1)) {
4785 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4786 			    scsa2usbp->scsa2usb_log_handle,
4787 			    "TEST 8: Hi <> Do: Step 2");
4788 			scsa2usb_test_mblk(scsa2usbp, B_TRUE);
4789 			scsa2usb_test_case_8 = 0;
4790 
4791 			return (rval);
4792 		}
4793 #endif	/* SCSA2USB_BULK_ONLY_TEST */
4794 
4795 		ept_addr = scsa2usbp->scsa2usb_bulkin_ept.bEndpointAddress;
4796 		req->bulk_len = cmd->cmd_xfercount;
4797 		req->bulk_attributes = attrs;
4798 		SCSA2USB_FREE_MSG(req->bulk_data);
4799 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4800 
4801 		req->bulk_data = esballoc_wait(
4802 		    (uchar_t *)cmd->cmd_bp->b_un.b_addr +
4803 		    cmd->cmd_offset,
4804 		    req->bulk_len, BPRI_LO, &frnop);
4805 
4806 		ASSERT(req->bulk_timeout);
4807 		rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkin_pipe,
4808 		    req, flags);
4809 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4810 
4811 		break;
4812 
4813 	case USB_EP_DIR_OUT:
4814 #ifdef	SCSA2USB_BULK_ONLY_TEST
4815 		/*
4816 		 * This happens when the host expects to receive data
4817 		 * from the device while the device intends to receive
4818 		 * data from the host.
4819 		 */
4820 		if (scsa2usb_test_case_10 &&
4821 		    (cmd->cmd_cdb[0] == SCMD_WRITE_G1)) {
4822 			req->bulk_len = CSW_LEN;
4823 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
4824 
4825 			ASSERT(req->bulk_timeout);
4826 			rval = usb_pipe_bulk_xfer(
4827 			    scsa2usbp->scsa2usb_bulkin_pipe, req, flags);
4828 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
4829 
4830 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4831 			    scsa2usbp->scsa2usb_log_handle,
4832 			    "TEST 10: Ho <> Di: done rval = 0x%x",  rval);
4833 			scsa2usb_test_case_10 = 0;
4834 
4835 			return (rval);
4836 		}
4837 #endif	/* SCSA2USB_BULK_ONLY_TEST */
4838 
4839 		req->bulk_data = scsa2usb_bp_to_mblk(scsa2usbp);
4840 		if (req->bulk_data == NULL) {
4841 
4842 			return (USB_FAILURE);
4843 		}
4844 
4845 #ifdef	SCSA2USB_BULK_ONLY_TEST
4846 		if (scsa2usb_test_case_11) {
4847 			/*
4848 			 * Host expects to send data to the device and
4849 			 * device doesn't expect to receive any data
4850 			 */
4851 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
4852 			    scsa2usbp->scsa2usb_log_handle, "TEST 11: Ho > Do");
4853 
4854 			scsa2usb_test_mblk(scsa2usbp, B_FALSE);
4855 			scsa2usb_test_case_11 = 0;
4856 		}
4857 #endif	/* SCSA2USB_BULK_ONLY_TEST */
4858 
4859 		ept_addr = scsa2usbp->scsa2usb_bulkout_ept.bEndpointAddress;
4860 		req->bulk_len = MBLKL(req->bulk_data);
4861 		req->bulk_timeout = scsa2usb_bulk_timeout(cmd->cmd_timeout);
4862 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
4863 
4864 		ASSERT(req->bulk_timeout);
4865 		rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkout_pipe,
4866 		    req, flags);
4867 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
4868 		break;
4869 	}
4870 
4871 	USB_DPRINTF_L3(DPRINT_MASK_SCSA,
4872 	    scsa2usbp->scsa2usb_log_handle,
4873 	    "scsa2usb_handle_data_start: rval=%d cr=%d", rval,
4874 	    req->bulk_completion_reason);
4875 
4876 	if (rval != USB_SUCCESS) {
4877 		/* Handle Errors now */
4878 		if (req->bulk_completion_reason == USB_CR_STALL) {
4879 			if (cmd->cmd_dir == USB_EP_DIR_IN) {
4880 				(void) scsa2usb_clear_ept_stall(
4881 				    scsa2usbp, ept_addr,
4882 				    scsa2usbp-> scsa2usb_bulkin_pipe,
4883 				    "bulk-in");
4884 			} else {
4885 				(void) scsa2usb_clear_ept_stall(
4886 				    scsa2usbp, ept_addr,
4887 				    scsa2usbp-> scsa2usb_bulkout_pipe,
4888 				    "bulk-out");
4889 			}
4890 		}
4891 
4892 		/* no more data to transfer after this */
4893 		cmd->cmd_done = 1;
4894 	}
4895 
4896 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4897 	    "scsa2usb_handle_data_start: END %s data rval = %d",
4898 	    (cmd->cmd_dir == USB_EP_DIR_IN) ? "bulk-in" : "bulk-out", rval);
4899 
4900 	return (rval);
4901 }
4902 
4903 
4904 /*
4905  * scsa2usb_handle_data_done:
4906  *	This function handles the completion of the data xfer.
4907  *	It also massages the inquiry data. This function may
4908  *	also be called after a stall.
4909  */
4910 void
4911 scsa2usb_handle_data_done(scsa2usb_state_t *scsa2usbp,
4912     scsa2usb_cmd_t *cmd, usb_bulk_req_t *req)
4913 {
4914 	struct buf	*bp = cmd->cmd_bp;
4915 	struct scsi_pkt	*pkt = scsa2usbp->scsa2usb_cur_pkt;
4916 	mblk_t		*data = req->bulk_data;
4917 	int		len = data ? MBLKL(data) : 0;
4918 	uint32_t	max_lba;
4919 
4920 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4921 
4922 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4923 	    "scsa2usb_handle_data_done:\n\tcmd = 0x%p data = 0x%p len = 0x%x",
4924 	    (void *)cmd, (void *)data, len);
4925 
4926 	cmd->cmd_resid_xfercount = cmd->cmd_xfercount - len;
4927 
4928 	if (len)  {
4929 		uchar_t	*p;
4930 		uchar_t dtype;
4931 		scsa2usb_read_cap_t *cap;
4932 		struct scsi_inquiry *inq;
4933 
4934 		switch (cmd->cmd_cdb[SCSA2USB_OPCODE]) {
4935 		case SCMD_INQUIRY:
4936 			/*
4937 			 * cache a copy of the inquiry data for our own use
4938 			 * but ensure that we have at least up to
4939 			 * inq_revision, inq_serial is not required.
4940 			 * ignore inquiry data returned for inquiry commands
4941 			 * with SCSI-3 EVPD, CmdDt bits set.
4942 			 */
4943 			if (((cmd->cmd_cdb[SCSA2USB_LUN] & 0x1f) == 0) &&
4944 			    (len >= SCSA2USB_MAX_INQ_LEN)) {
4945 				inq = (struct scsi_inquiry *)data->b_rptr;
4946 				dtype = inq->inq_dtype & DTYPE_MASK;
4947 				/*
4948 				 * scsi framework sends zero byte write(10) cmd
4949 				 * to (Simplified) direct-access devices with
4950 				 * inquiry version > 2 for reservation changes.
4951 				 * But some USB devices don't support zero byte
4952 				 * write(10) even though they have inquiry
4953 				 * version > 2. Considering scsa2usb driver
4954 				 * doesn't support reservation and all the
4955 				 * reservation cmds are being faked, we fake
4956 				 * the inquiry version to 0 to make scsi
4957 				 * framework send test unit ready cmd which is
4958 				 * supported by all the usb devices.
4959 				 */
4960 				if (((dtype == DTYPE_DIRECT) ||
4961 				    (dtype == DTYPE_RBC)) &&
4962 				    (inq->inq_ansi > 2)) {
4963 					inq->inq_ansi = 0;
4964 				}
4965 
4966 				bzero(&scsa2usbp->scsa2usb_lun_inquiry
4967 				    [pkt->pkt_address.a_lun],
4968 				    sizeof (struct scsi_inquiry));
4969 				bcopy(data->b_rptr,
4970 				    &scsa2usbp->scsa2usb_lun_inquiry
4971 				    [pkt->pkt_address.a_lun], len);
4972 			}
4973 
4974 			USB_DPRINTF_L3(DPRINT_MASK_SCSA,
4975 			    scsa2usbp->scsa2usb_log_handle,
4976 			    "scsi inquiry type = 0x%x",
4977 			    scsa2usbp->scsa2usb_lun_inquiry
4978 			    [pkt->pkt_address.a_lun].inq_dtype);
4979 
4980 			cmd->cmd_done = 1;
4981 			goto handle_data;
4982 
4983 		case SCMD_READ_CAPACITY:
4984 			cap = (scsa2usb_read_cap_t *)data->b_rptr;
4985 
4986 			/* Figure out the logical block size */
4987 			if ((len >= sizeof (struct scsa2usb_read_cap)) &&
4988 			    (req->bulk_completion_reason == USB_CR_OK)) {
4989 				scsa2usbp->
4990 				    scsa2usb_lbasize[pkt->pkt_address.a_lun] =
4991 				    SCSA2USB_MK_32BIT(
4992 				    cap->scsa2usb_read_cap_blen3,
4993 				    cap->scsa2usb_read_cap_blen2,
4994 				    cap->scsa2usb_read_cap_blen1,
4995 				    cap->scsa2usb_read_cap_blen0);
4996 
4997 				max_lba = SCSA2USB_MK_32BIT(
4998 				    cap->scsa2usb_read_cap_lba3,
4999 				    cap->scsa2usb_read_cap_lba2,
5000 				    cap->scsa2usb_read_cap_lba1,
5001 				    cap->scsa2usb_read_cap_lba0);
5002 
5003 				/*
5004 				 * Some devices return total logical block
5005 				 * number instead of highest logical block
5006 				 * address. Adjust the value by minus 1.
5007 				 */
5008 				if (max_lba > 0 && (scsa2usbp->scsa2usb_attrs &
5009 				    SCSA2USB_ATTRS_NO_CAP_ADJUST) == 0) {
5010 					max_lba -= 1;
5011 					cap->scsa2usb_read_cap_lba0 =
5012 					    (uchar_t)(max_lba & 0xFF);
5013 					cap->scsa2usb_read_cap_lba1 =
5014 					    (uchar_t)(max_lba >> 8 & 0xFF);
5015 					cap->scsa2usb_read_cap_lba2 =
5016 					    (uchar_t)(max_lba >> 16 & 0xFF);
5017 					cap->scsa2usb_read_cap_lba3 =
5018 					    (uchar_t)(max_lba >> 24 & 0xFF);
5019 				}
5020 
5021 				USB_DPRINTF_L2(DPRINT_MASK_SCSA,
5022 				    scsa2usbp->scsa2usb_log_handle,
5023 				    "bytes in each logical block=0x%lx,"
5024 				    "number of total logical blocks=0x%x",
5025 				    scsa2usbp->
5026 				    scsa2usb_lbasize[pkt->pkt_address.a_lun],
5027 				    max_lba + 1);
5028 			}
5029 			cmd->cmd_done = 1;
5030 			goto handle_data;
5031 
5032 		case SCMD_REQUEST_SENSE:
5033 			p = data->b_rptr;
5034 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
5035 			    scsa2usbp->scsa2usb_log_handle,
5036 			    "cdb: %x rqsense: "
5037 			    "%x %x %x %x %x %x %x %x %x %x\n\t"
5038 			    "%x %x %x %x %x %x %x %x %x %x",
5039 			    cmd->cmd_cdb[0],
5040 			    p[0], p[1], p[2], p[3], p[4],
5041 			    p[5], p[6], p[7], p[8], p[9],
5042 			    p[10], p[11], p[12], p[13], p[14],
5043 			    p[15], p[16], p[17], p[18], p[19]);
5044 
5045 			scsa2usbp->scsa2usb_last_cmd.status = p[2];
5046 			cmd->cmd_done = 1;
5047 			/* FALLTHROUGH */
5048 
5049 		default:
5050 handle_data:
5051 			if (bp && len && (cmd->cmd_dir == USB_EP_DIR_IN)) {
5052 				/*
5053 				 * we don't have to copy the data, the
5054 				 * data pointers for the mblk_t for
5055 				 * the bulk-in xfer points to the
5056 				 * struct buf * data.
5057 				 */
5058 				cmd->cmd_offset += len;
5059 			}
5060 
5061 			USB_DPRINTF_L3(DPRINT_MASK_SCSA,
5062 			    scsa2usbp->scsa2usb_log_handle,
5063 			    "len = 0x%x total = 0x%lx offset = 0x%lx",
5064 			    len, cmd->cmd_total_xfercount, cmd->cmd_offset);
5065 
5066 			/*
5067 			 * update total_xfercount now but it may be
5068 			 * adjusted after receiving the residue
5069 			 */
5070 			cmd->cmd_total_xfercount -= len;
5071 
5072 			if ((req->bulk_completion_reason != USB_CR_OK) ||
5073 			    (cmd->cmd_resid_xfercount != 0) ||
5074 			    (cmd->cmd_total_xfercount == 0)) {
5075 				/* set pkt_resid to total to be sure */
5076 				pkt->pkt_resid = cmd->cmd_total_xfercount;
5077 				cmd->cmd_done = 1;
5078 			}
5079 
5080 			break;
5081 		}
5082 	} else {
5083 		if (cmd->cmd_dir == USB_EP_DIR_OUT) {
5084 			if (cmd->cmd_total_xfercount == 0) {
5085 				cmd->cmd_done = 1;
5086 			}
5087 		}
5088 	}
5089 }
5090 
5091 
5092 /*
5093  * scsa2usb_init_bulk_req:
5094  *	Allocate (synchronously) and fill in a bulk-request
5095  */
5096 usb_bulk_req_t *
5097 scsa2usb_init_bulk_req(scsa2usb_state_t *scsa2usbp, size_t length,
5098     uint_t timeout, usb_req_attrs_t attrs, usb_flags_t flags)
5099 {
5100 	usb_bulk_req_t	*req;
5101 
5102 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5103 
5104 	req = usb_alloc_bulk_req(scsa2usbp->scsa2usb_dip, length,
5105 	    flags | USB_FLAGS_SLEEP);
5106 
5107 	req->bulk_len = (uint_t)length;			/* xfer length */
5108 	req->bulk_timeout = scsa2usb_bulk_timeout(timeout); /* xfer timeout */
5109 	req->bulk_attributes = attrs;		/* xfer attrs */
5110 	req->bulk_client_private = (usb_opaque_t)scsa2usbp; /* statep */
5111 
5112 	return (req);
5113 }
5114 
5115 
5116 /*
5117  * scsa2usb_bulk_timeout:
5118  *	ensure that bulk requests do not have infinite timeout values
5119  */
5120 int
5121 scsa2usb_bulk_timeout(int timeout)
5122 {
5123 	return ((timeout == 0) ? scsa2usb_long_timeout : timeout);
5124 }
5125 
5126 
5127 /*
5128  * scsa2usb_clear_ept_stall:
5129  *	clear endpoint stall and reset pipes
5130  */
5131 int
5132 scsa2usb_clear_ept_stall(scsa2usb_state_t *scsa2usbp, uint_t ept_addr,
5133     usb_pipe_handle_t ph, char *what)
5134 {
5135 	int rval;
5136 	dev_info_t *dip = scsa2usbp->scsa2usb_dip;
5137 
5138 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5139 	if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
5140 
5141 		return (USB_FAILURE);
5142 	}
5143 
5144 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5145 	rval = usb_clr_feature(dip, USB_DEV_REQ_RCPT_EP, 0, ept_addr,
5146 	    USB_FLAGS_SLEEP, NULL, NULL);
5147 
5148 	usb_pipe_reset(dip, ph, USB_FLAGS_SLEEP, NULL, NULL);
5149 
5150 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5151 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5152 	    "scsa2usb_clear_ept_stall: on %s: ept = 0x%x rval = %d",
5153 	    what, ept_addr, rval);
5154 
5155 	return (rval);
5156 }
5157 
5158 
5159 /*
5160  * scsa2usb_pkt_completion:
5161  *	Handle pkt completion.
5162  */
5163 static void
5164 scsa2usb_pkt_completion(scsa2usb_state_t *scsa2usbp, struct scsi_pkt *pkt)
5165 {
5166 	scsa2usb_cmd_t *cmd = PKT2CMD(pkt);
5167 	size_t len;
5168 
5169 	ASSERT(pkt);
5170 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5171 
5172 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5173 	    "scsa2usb_pkt_completion:\n\tscsa2usbp = 0x%p "
5174 	    "reason=%d, status=%d state=0x%x stats=0x%x resid=0x%lx",
5175 	    (void *)scsa2usbp, pkt->pkt_reason, *(pkt->pkt_scbp),
5176 	    pkt->pkt_state, pkt->pkt_statistics, pkt->pkt_resid);
5177 
5178 	if (pkt->pkt_reason == CMD_CMPLT) {
5179 		pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET |
5180 		    STATE_SENT_CMD | STATE_GOT_STATUS;
5181 		if (cmd->cmd_xfercount) {
5182 			pkt->pkt_state |= STATE_XFERRED_DATA;
5183 		}
5184 	} else {
5185 		pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET |
5186 		    STATE_SENT_CMD;
5187 	}
5188 
5189 	/*
5190 	 * don't zap the current state when in panic as this will
5191 	 * make debugging harder
5192 	 */
5193 	if ((scsa2usbp->scsa2usb_cur_pkt == pkt) && !ddi_in_panic()) {
5194 		SCSA2USB_RESET_CUR_PKT(scsa2usbp);
5195 
5196 		len = sizeof (scsa2usbp->scsa2usb_last_cmd.cdb);
5197 		bzero(scsa2usbp->scsa2usb_last_cmd.cdb, len);
5198 
5199 		len = (len < cmd->cmd_cdblen) ? len : cmd->cmd_cdblen;
5200 		USB_DPRINTF_L3(DPRINT_MASK_SCSA,
5201 		    scsa2usbp->scsa2usb_log_handle,
5202 		    "scsa2usb_pkt_completion: save last cmd, len=%ld", len);
5203 
5204 		/* save the last command */
5205 		bcopy(pkt->pkt_cdbp, scsa2usbp->scsa2usb_last_cmd.cdb, len);
5206 
5207 		/* reset the scsa2usb_last_cmd.status value */
5208 		if ((pkt->pkt_cdbp[0] != SCMD_REQUEST_SENSE) &&
5209 		    (pkt->pkt_cdbp[0] != SCMD_INQUIRY)) {
5210 			scsa2usbp->scsa2usb_last_cmd.status = 0;
5211 		}
5212 
5213 		/*
5214 		 * set pkt state to NONE *before* calling back as the target
5215 		 * driver will immediately submit the next packet
5216 		 */
5217 		scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_NONE;
5218 	}
5219 
5220 	if (pkt->pkt_comp) {
5221 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5222 		scsi_hba_pkt_comp(pkt);
5223 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5224 
5225 	}
5226 }
5227 
5228 
5229 /*
5230  * Even handling functions:
5231  *
5232  * scsa2usb_reconnect_event_cb:
5233  *	event handling
5234  */
5235 static int
5236 scsa2usb_reconnect_event_cb(dev_info_t *dip)
5237 {
5238 	scsa2usb_state_t *scsa2usbp =
5239 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
5240 	dev_info_t	*cdip;
5241 	int		circ;
5242 	int		rval = USB_SUCCESS;
5243 
5244 	ASSERT(scsa2usbp != NULL);
5245 
5246 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5247 	    "scsa2usb_reconnect_event_cb: dip = 0x%p", (void *)dip);
5248 
5249 	scsa2usb_restore_device_state(dip, scsa2usbp);
5250 
5251 	USB_DPRINTF_L0(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5252 	    "Reinserted device is accessible again.");
5253 
5254 	ndi_devi_enter(dip, &circ);
5255 	for (cdip = ddi_get_child(dip); cdip; ) {
5256 		dev_info_t *next = ddi_get_next_sibling(cdip);
5257 
5258 		mutex_enter(&DEVI(cdip)->devi_lock);
5259 		DEVI_SET_DEVICE_REINSERTED(cdip);
5260 		mutex_exit(&DEVI(cdip)->devi_lock);
5261 
5262 		cdip = next;
5263 	}
5264 	ndi_devi_exit(dip, circ);
5265 
5266 	/* stop suppressing warnings */
5267 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5268 	scsa2usbp->scsa2usb_warning_given = B_FALSE;
5269 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5270 
5271 	if (scsa2usbp->scsa2usb_ugen_hdl) {
5272 		rval = usb_ugen_reconnect_ev_cb(
5273 		    scsa2usbp->scsa2usb_ugen_hdl);
5274 	}
5275 
5276 	return (rval);
5277 }
5278 
5279 
5280 /*
5281  * scsa2usb_all_waitQs_empty:
5282  *	check if all waitQs empty
5283  */
5284 static int
5285 scsa2usb_all_waitQs_empty(scsa2usb_state_t *scsa2usbp)
5286 {
5287 	uint_t	lun;
5288 
5289 	for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) {
5290 		if (usba_list_entry_count(
5291 		    &scsa2usbp->scsa2usb_waitQ[lun])) {
5292 
5293 			return (USB_FAILURE);
5294 		}
5295 	}
5296 
5297 	return (USB_SUCCESS);
5298 }
5299 
5300 
5301 /*
5302  * scsa2usb_disconnect_event_cb:
5303  *	callback for disconnect events
5304  */
5305 static int
5306 scsa2usb_disconnect_event_cb(dev_info_t *dip)
5307 {
5308 	scsa2usb_state_t *scsa2usbp =
5309 	    ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
5310 	dev_info_t	*cdip;
5311 	int		circ, i;
5312 	int		rval = USB_SUCCESS;
5313 
5314 	ASSERT(scsa2usbp != NULL);
5315 
5316 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5317 	    "scsa2usb_disconnect_event_cb: dip = 0x%p", (void *)dip);
5318 
5319 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5320 	scsa2usbp->scsa2usb_dev_state = USB_DEV_DISCONNECTED;
5321 
5322 	/*
5323 	 * wait till the work thread is done, carry on regardless
5324 	 * if not.
5325 	 */
5326 	for (i = 0; i < SCSA2USB_DRAIN_TIMEOUT; i++) {
5327 		if ((scsa2usbp->scsa2usb_work_thread_id == NULL) &&
5328 		    (scsa2usbp->scsa2usb_cur_pkt == NULL) &&
5329 		    (scsa2usb_all_waitQs_empty(scsa2usbp) ==
5330 		    USB_SUCCESS)) {
5331 
5332 			break;
5333 		}
5334 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5335 		delay(drv_usectohz(1000000));
5336 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5337 	}
5338 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5339 
5340 	ndi_devi_enter(dip, &circ);
5341 	for (cdip = ddi_get_child(dip); cdip; ) {
5342 		dev_info_t *next = ddi_get_next_sibling(cdip);
5343 
5344 		mutex_enter(&DEVI(cdip)->devi_lock);
5345 		DEVI_SET_DEVICE_REMOVED(cdip);
5346 		mutex_exit(&DEVI(cdip)->devi_lock);
5347 
5348 		cdip = next;
5349 	}
5350 	ndi_devi_exit(dip, circ);
5351 
5352 	if (scsa2usbp->scsa2usb_ugen_hdl) {
5353 		rval = usb_ugen_disconnect_ev_cb(
5354 		    scsa2usbp->scsa2usb_ugen_hdl);
5355 	}
5356 
5357 	return (rval);
5358 }
5359 
5360 
5361 /*
5362  * PM support
5363  *
5364  * scsa2usb_create_pm_components:
5365  *	create the pm components required for power management
5366  *	no mutex is need when calling USBA interfaces
5367  */
5368 static void
5369 scsa2usb_create_pm_components(dev_info_t *dip, scsa2usb_state_t *scsa2usbp)
5370 {
5371 	scsa2usb_power_t *pm;
5372 	uint_t		pwr_states;
5373 
5374 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5375 
5376 	USB_DPRINTF_L4(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5377 	    "scsa2usb_create_pm_components: dip = 0x%p, scsa2usbp = 0x%p",
5378 	    (void *)dip, (void *)scsa2usbp);
5379 
5380 	/*
5381 	 * determine if this device is on the blacklist
5382 	 * or if a conf file entry has disabled PM
5383 	 */
5384 	if ((scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_PM) == 0) {
5385 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5386 		    "device cannot be power managed");
5387 
5388 		return;
5389 	}
5390 
5391 	/* Allocate the PM state structure */
5392 	pm = kmem_zalloc(sizeof (scsa2usb_power_t), KM_SLEEP);
5393 
5394 	scsa2usbp->scsa2usb_pm = pm;
5395 	pm->scsa2usb_current_power = USB_DEV_OS_FULL_PWR;
5396 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5397 
5398 	if (usb_create_pm_components(dip, &pwr_states) ==
5399 	    USB_SUCCESS) {
5400 		if (usb_handle_remote_wakeup(dip,
5401 		    USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) {
5402 			pm->scsa2usb_wakeup_enabled = 1;
5403 		}
5404 
5405 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5406 		pm->scsa2usb_pwr_states = (uint8_t)pwr_states;
5407 		scsa2usb_raise_power(scsa2usbp);
5408 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5409 	}
5410 
5411 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5412 }
5413 
5414 
5415 /*
5416  * scsa2usb_raise_power:
5417  *	check if the device is using full power or not
5418  */
5419 static void
5420 scsa2usb_raise_power(scsa2usb_state_t *scsa2usbp)
5421 {
5422 	USB_DPRINTF_L4(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5423 	    "scsa2usb_raise_power:");
5424 
5425 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5426 
5427 	if (scsa2usbp->scsa2usb_pm) {
5428 		scsa2usb_pm_busy_component(scsa2usbp);
5429 		if (scsa2usbp->scsa2usb_pm->scsa2usb_current_power !=
5430 		    USB_DEV_OS_FULL_PWR) {
5431 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
5432 			(void) pm_raise_power(scsa2usbp->scsa2usb_dip,
5433 			    0, USB_DEV_OS_FULL_PWR);
5434 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
5435 		}
5436 	}
5437 }
5438 
5439 
5440 /*
5441  * functions to handle power transition for OS levels 0 -> 3
5442  */
5443 static int
5444 scsa2usb_pwrlvl0(scsa2usb_state_t *scsa2usbp)
5445 {
5446 	int	rval;
5447 
5448 	switch (scsa2usbp->scsa2usb_dev_state) {
5449 	case USB_DEV_ONLINE:
5450 		/* Deny the powerdown request if the device is busy */
5451 		if (scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy != 0) {
5452 
5453 			return (USB_FAILURE);
5454 		}
5455 
5456 		/*
5457 		 * stop polling on interrupt pipe
5458 		 */
5459 		scsa2usb_cbi_stop_intr_polling(scsa2usbp);
5460 
5461 		/* Issue USB D3 command to the device here */
5462 		rval = usb_set_device_pwrlvl3(scsa2usbp->scsa2usb_dip);
5463 		ASSERT(rval == USB_SUCCESS);
5464 
5465 		scsa2usbp->scsa2usb_dev_state = USB_DEV_PWRED_DOWN;
5466 
5467 		/* FALLTHRU */
5468 	case USB_DEV_DISCONNECTED:
5469 	case USB_DEV_SUSPENDED:
5470 	case USB_DEV_PWRED_DOWN:
5471 	default:
5472 		scsa2usbp->scsa2usb_pm->scsa2usb_current_power =
5473 		    USB_DEV_OS_PWR_OFF;
5474 
5475 		return (USB_SUCCESS);
5476 	}
5477 }
5478 
5479 
5480 static int
5481 scsa2usb_pwrlvl1(scsa2usb_state_t *scsa2usbp)
5482 {
5483 	int	rval;
5484 
5485 	/* Issue USB D2 command to the device here */
5486 	rval = usb_set_device_pwrlvl2(scsa2usbp->scsa2usb_dip);
5487 	ASSERT(rval == USB_SUCCESS);
5488 
5489 	return (DDI_FAILURE);
5490 }
5491 
5492 
5493 static int
5494 scsa2usb_pwrlvl2(scsa2usb_state_t *scsa2usbp)
5495 {
5496 	int	rval;
5497 
5498 	/* Issue USB D1 command to the device here */
5499 	rval = usb_set_device_pwrlvl1(scsa2usbp->scsa2usb_dip);
5500 	ASSERT(rval == USB_SUCCESS);
5501 
5502 	return (DDI_FAILURE);
5503 }
5504 
5505 
5506 static int
5507 scsa2usb_pwrlvl3(scsa2usb_state_t *scsa2usbp)
5508 {
5509 	int	rval;
5510 
5511 	/*
5512 	 * PM framework tries to put us in full power
5513 	 * during system shutdown. If we are disconnected
5514 	 * return success anyways
5515 	 */
5516 	if (scsa2usbp->scsa2usb_dev_state != USB_DEV_DISCONNECTED) {
5517 		/* Issue USB D0 command to the device here */
5518 		rval = usb_set_device_pwrlvl0(scsa2usbp->scsa2usb_dip);
5519 		ASSERT(rval == USB_SUCCESS);
5520 
5521 		scsa2usbp->scsa2usb_dev_state = USB_DEV_ONLINE;
5522 	}
5523 	scsa2usbp->scsa2usb_pm->scsa2usb_current_power = USB_DEV_OS_FULL_PWR;
5524 
5525 	return (DDI_SUCCESS);
5526 }
5527 
5528 
5529 /*
5530  * scsa2usb_power:
5531  *	power entry point
5532  */
5533 /* ARGSUSED */
5534 static int
5535 scsa2usb_power(dev_info_t *dip, int comp, int level)
5536 {
5537 	scsa2usb_state_t	*scsa2usbp;
5538 	scsa2usb_power_t	*pm;
5539 	int			rval = DDI_FAILURE;
5540 
5541 	scsa2usbp = ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip));
5542 
5543 	USB_DPRINTF_L3(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5544 	    "scsa2usb_power: Begin scsa2usbp (%p): level = %d",
5545 	    (void *)scsa2usbp, level);
5546 
5547 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5548 	if (SCSA2USB_BUSY(scsa2usbp)) {
5549 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5550 		    "scsa2usb_power: busy");
5551 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5552 
5553 		return (rval);
5554 	}
5555 
5556 	pm = scsa2usbp->scsa2usb_pm;
5557 	if (pm == NULL) {
5558 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5559 		    "scsa2usb_power: pm NULL");
5560 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5561 
5562 		return (rval);
5563 	}
5564 
5565 	/* check if we are transitioning to a legal power level */
5566 	if (USB_DEV_PWRSTATE_OK(pm->scsa2usb_pwr_states, level)) {
5567 		USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle,
5568 		    "scsa2usb_power: illegal power level = %d "
5569 		    "pwr_states: %x", level, pm->scsa2usb_pwr_states);
5570 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5571 
5572 		return (rval);
5573 	}
5574 
5575 	switch (level) {
5576 	case USB_DEV_OS_PWR_OFF :
5577 		rval = scsa2usb_pwrlvl0(scsa2usbp);
5578 		break;
5579 	case USB_DEV_OS_PWR_1 :
5580 		rval = scsa2usb_pwrlvl1(scsa2usbp);
5581 		break;
5582 	case USB_DEV_OS_PWR_2 :
5583 		rval = scsa2usb_pwrlvl2(scsa2usbp);
5584 		break;
5585 	case USB_DEV_OS_FULL_PWR :
5586 		rval = scsa2usb_pwrlvl3(scsa2usbp);
5587 		break;
5588 	}
5589 
5590 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5591 
5592 	return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
5593 }
5594 
5595 
5596 static void
5597 scsa2usb_pm_busy_component(scsa2usb_state_t *scsa2usbp)
5598 {
5599 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5600 
5601 	if (scsa2usbp->scsa2usb_pm) {
5602 		scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy++;
5603 
5604 		USB_DPRINTF_L4(DPRINT_MASK_PM,
5605 		    scsa2usbp->scsa2usb_log_handle,
5606 		    "scsa2usb_pm_busy_component: %d",
5607 		    scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy);
5608 
5609 		mutex_exit(&scsa2usbp->scsa2usb_mutex);
5610 
5611 		if (pm_busy_component(scsa2usbp->scsa2usb_dip, 0) !=
5612 		    DDI_SUCCESS) {
5613 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
5614 			ASSERT(scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy > 0);
5615 			scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy--;
5616 
5617 			USB_DPRINTF_L2(DPRINT_MASK_PM,
5618 			    scsa2usbp->scsa2usb_log_handle,
5619 			    "scsa2usb_pm_busy_component failed: %d",
5620 			    scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy);
5621 
5622 			return;
5623 		}
5624 		mutex_enter(&scsa2usbp->scsa2usb_mutex);
5625 	}
5626 }
5627 
5628 
5629 /*
5630  * scsa2usb_pm_idle_component:
5631  *	idles the device
5632  */
5633 static void
5634 scsa2usb_pm_idle_component(scsa2usb_state_t *scsa2usbp)
5635 {
5636 	ASSERT(!mutex_owned(&scsa2usbp->scsa2usb_mutex));
5637 
5638 	if (scsa2usbp->scsa2usb_pm) {
5639 		if (pm_idle_component(scsa2usbp->scsa2usb_dip, 0) ==
5640 		    DDI_SUCCESS) {
5641 			mutex_enter(&scsa2usbp->scsa2usb_mutex);
5642 			ASSERT(scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy > 0);
5643 			scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy--;
5644 
5645 			USB_DPRINTF_L4(DPRINT_MASK_PM,
5646 			    scsa2usbp->scsa2usb_log_handle,
5647 			    "scsa2usb_pm_idle_component: %d",
5648 			    scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy);
5649 
5650 			mutex_exit(&scsa2usbp->scsa2usb_mutex);
5651 		}
5652 	}
5653 }
5654 
5655 
5656 #ifdef	DEBUG
5657 /*
5658  * scsa2usb_print_cdb:
5659  *	prints CDB
5660  */
5661 void
5662 scsa2usb_print_cdb(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd)
5663 {
5664 	uchar_t *c = (uchar_t *)&cmd->cmd_cdb;
5665 
5666 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5667 	    "cmd = 0x%p opcode=%s "
5668 	    "cdb: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
5669 	    (void *)cmd,
5670 	    scsi_cname(cmd->cmd_cdb[SCSA2USB_OPCODE], scsa2usb_cmds),
5671 	    c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8],
5672 	    c[9], c[10], c[11], c[12], c[13], c[14], c[15]);
5673 }
5674 #endif	/* DEBUG */
5675 
5676 
5677 #ifdef	SCSA2USB_BULK_ONLY_TEST
5678 /*
5679  * scsa2usb_test_mblk:
5680  *	This function sends a dummy data mblk_t to simulate
5681  *	the following test cases: 5 and 11.
5682  */
5683 static void
5684 scsa2usb_test_mblk(scsa2usb_state_t *scsa2usbp, boolean_t large)
5685 {
5686 	int			i, rval;
5687 	size_t			len;
5688 	usb_flags_t		flags = USB_FLAGS_SLEEP;
5689 	usb_bulk_req_t		*req;
5690 
5691 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5692 
5693 	/* should we create a larger mblk? */
5694 	len = (large == B_TRUE) ? DEV_BSIZE : USB_BULK_CBWCMD_LEN;
5695 
5696 	req = scsa2usb_init_bulk_req(scsa2usbp, len,
5697 	    SCSA2USB_BULK_PIPE_TIMEOUT, 0, flags);
5698 
5699 	/* fill up the data mblk */
5700 	for (i = 0; i < len; i++) {
5701 		*req->bulk_data->b_wptr++ = (uchar_t)i;
5702 	}
5703 
5704 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
5705 	ASSERT(req->bulk_timeout);
5706 	rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkout_pipe, req, flags);
5707 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
5708 
5709 	USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5710 	    "scsa2usb_test_mblk: Sent Data Out rval = 0x%x", rval);
5711 
5712 	usb_free_bulk_req(req);
5713 }
5714 #endif	/* SCSA2USB_BULK_ONLY_TEST */
5715