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