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