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