xref: /titanic_44/usr/src/uts/common/sys/usb/scsa2usb/scsa2usb.h (revision 870ad75a2b67a92c3449d93b4fef8a0baa982b4a)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
22  * Use is subject to license terms.
23  */
24 
25 #ifndef _SYS_USB_SCSA2USB_H
26 #define	_SYS_USB_SCSA2USB_H
27 
28 
29 #ifdef	__cplusplus
30 extern "C" {
31 #endif
32 
33 #include <sys/usb/usba/usbai_private.h>
34 
35 /*
36  * SCSA2USB: This header file contains the internal structures
37  * and variable definitions used in USB mass storage disk driver.
38  */
39 
40 
41 #define	SCSA2USB_INITIAL_ALLOC	4	/* initial soft space alloc */
42 
43 #define	MAX_COMPAT_NAMES	1	/* max compatible names for children */
44 #define	SERIAL_NUM_LEN		64	/* for reading string descriptor */
45 #define	SCSA2USB_SERIAL_LEN	12	/* len of serial no in scsi_inquiry */
46 
47 #define	SCSA2USB_MAX_LUNS	0x10	/* maximum luns supported. */
48 
49 /*
50  * limit the max transfer size to under <= 64K. Some devices
51  * have problems with large transfers
52  */
53 #define	SCSA2USB_MAX_BULK_XFER_SIZE	(64 * 1024)
54 
55 /* Blacklist some vendors whose devices could cause problems */
56 #define	MS_HAGIWARA_SYS_COM_VID	0x693	/* VendorId of Hagiwara Sys-Com */
57 #define	MS_HAGIWARA_SYSCOM_PID1	0x1	/* PID for SmartMedia(SM) device */
58 #define	MS_HAGIWARA_SYSCOM_PID2	0x3	/* PID for CompactFlash(CF) device */
59 #define	MS_HAGIWARA_SYSCOM_PID3	0x5	/* PID for SM/CF Combo device */
60 #define	MS_HAGIWARA_SYSCOM_PID4	0x2	/* PID for new SM device */
61 #define	MS_HAGIWARA_SYSCOM_PID5	0x4	/* PID for new CF device */
62 
63 #define	MS_IOMEGA_VID		0x59b	/* VendorId of Iomega */
64 #define	MS_IOMEGA_PID1_ZIP100	0x1	/* PID of an Older Iomega Zip100 */
65 #define	MS_IOMEGA_PID2_ZIP100	0x2	/* PID of Newer Iomega Zip100 */
66 #define	MS_IOMEGA_PID3_ZIP100	0x31	/* PID of Newer Iomega Zip100 */
67 #define	MS_IOMEGA_PID_ZIP250	0x30	/* PID of Newer Iomega Zip250 */
68 #define	MS_IOMEGA_PID_CLIK	0x60	/* PID of Iomega Clik! drive */
69 
70 #define	MS_MITSUMI_VID		0x3ee	/* VendorId of Mitsumi Inc */
71 #define	MS_MITSUMI_DEVICE_242	0x242	/* bcdDevice of Mitsumi CR-4804TU */
72 #define	MS_MITSUMI_DEVICE_24	0x24	/* bcdDevice of Mitsumi CR-4802TU */
73 
74 #define	MS_YEDATA_VID		0x57b	/* VendorId of Y-E Data Corp */
75 #define	MS_SMSC_VID		0x424	/* Vendor Id of SMSC */
76 #define	MS_SMSC_PID0		0xfdc	/* floppy from SMSC */
77 
78 #define	MS_NEODIO_VID		0xaec	/* Neodio Technologies Corporation */
79 #define	MS_NEODIO_DEVICE_3050	0x3050	/* PID of ND3050/Soyo BayOne */
80 					/* SM/CF/MS/SD */
81 #define	MS_SONY_FLASH_VID	0x54c	/* sony flash device */
82 #define	MS_SONY_FLASH_PID	0x8b
83 
84 #define	MS_TREK_FLASH_VID	0xa16	/* Trek flash device */
85 #define	MS_TREK_FLASH_PID	0x9988
86 
87 #define	MS_PENN_FLASH_VID	0xd7d	/* Penn flash device */
88 #define	MS_PENN_FLASH_PID	0x1320
89 
90 #define	MS_SIMPLETECH_VID	0x7c4	/* VendorId of Simpltech */
91 #define	MS_SIMPLETECH_PID1	0xa400	/* PID for UCF-100 device */
92 
93 #define	MS_ADDONICS_CARD_READER_VID 0x7cc /* addonics */
94 #define	MS_ADDONICS_CARD_READER_PID 0x320
95 
96 #define	MS_ACOMDATA_VID		0xc0b	/* VendorId of DMI (Acomdata) */
97 #define	MS_ACOMDATA_PID1	0x5fab	/* PID for 80GB USB/1394 disk */
98 
99 #define	MS_OTI_VID		0xea0	/* VendorID of OTI */
100 #define	MS_OTI_DEVICE_6828	0x6828	/* PID for 6828 flash disk */
101 
102 #define	MS_SCANLOGIC_VID	0x04ce	/* VendorID of ScanLogic */
103 #define	MS_SCANLOGIC_PID1	0x0002	/* SL USB Storage Device */
104 
105 #define	MS_SUPERTOP_VID		0x14cd	/* Super Top USB 2.0 IDE enclosure */
106 #define	MS_SUPERTOP_DEVICE_6600	0x6600
107 
108 #define	MS_AIGO_VID		0xed1	/* VendorID of Aigo */
109 #define	MS_AIGO_DEVICE_6981	0x6981	/* Aigo Miniking Device NEHFSP14 */
110 
111 #define	MS_ALCOR_VID	0x58f	/* Vendor ID of Alcor Micro Corp */
112 #define	MS_ALCOR_PID0	0x6387	/* PID for 6387 flash disk */
113 
114 #define	MS_TOSHIBA_VID	0x930	/* Vendor ID of Toshiba Corp */
115 #define	MS_TOSHIBA_PID0	0x6545	/* Kingston DataTraveler / PNY Attache Stick */
116 
117 #define	MS_PNY_VID	0x154b	/* Vendor ID of PNY Corp */
118 #define	MS_PNY_PID0	0x16	/* PNY floppy drive */
119 
120 /*
121  * The AMI virtual floppy device is not a real USB storage device, but
122  * emulated by the SP firmware shipped together with important Sun x86
123  * products such as Galaxy and Thumper platforms. The device causes
124  * very long delay in boot process of these platforms which is a big
125  * performance issue. Improvement in firmware may solve the issue, but
126  * before the firmware is fixed, it needs to be taken care of by software
127  * to avoid the huge impact on user experience.
128  *
129  * The long boot delay is caused by timeouts and retries of READ CAPACITY
130  * command issued to the device. The device is a USB ufi subclass device
131  * using CBI protocol. When READ CAPACITY command is issued, the device
132  * returns STALL on the bulk endpoint during the data stage, however, it
133  * doesn't return status on the intr pipe during status stage, so the intr
134  * pipe can only fail with timeout.
135  *
136  * Reducing timeout value to 1 second can help a little bit, but the delay
137  * is still noticeable, because the target driver would make many retries
138  * for this command. It is not desirable to mess with the target driver
139  * for a broken USB device. So adding the device to the scsa2usb blacklist
140  * is the best choice we have.
141  *
142  * It is found that the READ CAPACITY failure only happens when there is
143  * no media in the floppy drive. When there is a media, the device works
144  * just fine. So READ CAPACITY command cannot be arbitrarily disabled.
145  * Media status needs to be checked before issuing the command by sending
146  * an additional TEST UNIT READY command. If TEST UNIT READY command
147  * return STATUS_GOOD, it means the media is ready and then READ CAPACITY
148  * can be issued.
149  *
150  * SCSA2USB_ATTRS_NO_MEDIA_CHECK is added below for this purpose. It is
151  * overrided in scsa2usb.c for the AMI virtual floppy device to take care
152  * of the special need.
153  */
154 #define	MS_AMI_VID		0x46b	/* VendorId of AMI */
155 #define	MS_AMI_VIRTUAL_FLOPPY	0xff40	/* PID for AMI virtual floppy */
156 
157 /*
158  * List the attributes that need special case in the driver
159  * SCSA2USB_ATTRS_GET_LUN: Bulk Only Transport Get_Max_Lun class specific
160  *		command is not implemented by these devices
161  * SCSA2USB_ATTRS_PM: Some devices don't like being power managed.
162  * SCSA2USB_ATTRS_START_STOP: Some devices don't do anything with
163  *		SCMD_START_STOP opcode (for e.g. SmartMedia/CompactFlash/
164  *		Clik!/MemoryStick/MMC USB readers/writers.
165  * SCSA2USB_ATTRS_GET_CONF: SCMD_GET_CONFIGURATION is not supported
166  * SCMD_TEST_UNIT_READY: for floppies this needs to be converted to
167  *		SCMD_START_STOP as floppies don't support this
168  * SCSA2USB_ATTRS_GET_PERF: SCMD_GET_PERFORMANCE not supported by
169  *		Mitsumi's CD-RW devices.
170  * SCSA2USB_ATTRS_BIG_TIMEOUT: Mitsumi's CD-RW devices need large
171  *		timeout with SCMD_START_STOP cmd
172  * SCSA2USB_ATTRS_RMB: Pay attention to the device's RMB setting,
173  *		instead of automatically treating it as removable
174  * SCSA2USB_ATTRS_USE_CSW_RESIDUE: Some devices report false residue in
175  *		the CSW of bulk-only transfer status stage though data
176  *		was successfully transfered, so need to ignore residue.
177  * SCSA2USB_ATTRS_NO_MEDIA_CHECK: AMI Virtual Floppy devices need to
178  *		check if media is ready before issuing READ CAPACITY.
179  * SCSA2USB_ATTRS_NO_CAP_ADJUST: Some devices return total logical block number
180  * 		instead of highest logical block address on READ_CAPACITY cmd.
181  *
182  * NOTE: If a device simply STALLs the GET_MAX_LUN BO class-specific command
183  * and recovers then it will not be added to the scsa2usb_blacklist[] table
184  * in scsa2usb.c. The other attributes will not be taken of the table unless
185  * their inclusion causes a recovery and retries (thus seriously affecting
186  * the driver performance).
187  */
188 #define	SCSA2USB_ATTRS_GET_LUN		0x01	/* GET_MAX_LUN (Bulk Only) */
189 #define	SCSA2USB_ATTRS_PM		0x02	/* Some don't support PM */
190 #define	SCSA2USB_ATTRS_START_STOP	0x04	/* SCMD_START_STOP */
191 #define	SCSA2USB_ATTRS_GET_CONF		0x08	/* SCMD_GET_CONFIGURATION */
192 #define	SCSA2USB_ATTRS_GET_PERF		0x10	/* SCMD_GET_PERFORMANCE */
193 #define	SCSA2USB_ATTRS_BIG_TIMEOUT	0x40	/* for SCMD_START_STOP */
194 #define	SCSA2USB_ATTRS_DOORLOCK		0x80	/* for SCMD_DOORLOCK */
195 #define	SCSA2USB_ATTRS_RMB		0x100	/* Pay attention to RMB */
196 #define	SCSA2USB_ATTRS_MODE_SENSE	0x200	/* SCMD_MODE_SENSE */
197 #define	SCSA2USB_ATTRS_INQUIRY		0x400	/* SCMD_INQUIRY */
198 #define	SCSA2USB_ATTRS_USE_CSW_RESIDUE	0x800	/* for residue checking */
199 #define	SCSA2USB_ATTRS_NO_MEDIA_CHECK	0x1000	/* for media checking */
200 #define	SCSA2USB_ATTRS_NO_CAP_ADJUST	0x2000	/* for CAPACITY adjusting */
201 #define	SCSA2USB_ATTRS_REDUCED_CMD	\
202 	(SCSA2USB_ATTRS_DOORLOCK|SCSA2USB_ATTRS_MODE_SENSE| \
203 	SCSA2USB_ATTRS_START_STOP|SCSA2USB_ATTRS_INQUIRY| \
204 	SCSA2USB_ATTRS_USE_CSW_RESIDUE)
205 
206 #define	SCSA2USB_ALL_ATTRS		0xFFFF	/* All of the above */
207 
208 /* max inquiry length */
209 #define	SCSA2USB_MAX_INQ_LEN (offsetof(struct scsi_inquiry, inq_serial))
210 
211 /* page code of scsi mode page */
212 #ifndef SD_MODE_SENSE_PAGE3_CODE
213 #define	SD_MODE_SENSE_PAGE3_CODE	0x03
214 #endif
215 
216 #ifndef SD_MODE_SENSE_PAGE4_CODE
217 #define	SD_MODE_SENSE_PAGE4_CODE	0x04
218 #endif
219 
220 /*
221  * PM support
222  */
223 typedef struct scsa2usb_power  {
224 	/* device busy accounting */
225 	int		scsa2usb_pm_busy;
226 	/* this is the bit mask of the power states that device has */
227 	uint8_t		scsa2usb_pwr_states;
228 
229 	uint8_t		scsa2usb_wakeup_enabled;
230 
231 	/* current power level the device is in */
232 	uint8_t		scsa2usb_current_power;
233 } scsa2usb_power_t;
234 
235 /*
236  * CPR support:
237  *	keep track of the last command issued to the drive. If it
238  *	was TUR or EJECT then allow issuing a CPR suspend.
239  */
240 #define	LOEJECT	2		/* eject bit in start/stop cmd */
241 
242 typedef struct scsa2usb_last_cmd {
243 	/* this is the cdb of the last command issued */
244 	uchar_t		cdb[SCSI_CDB_SIZE];
245 
246 	/* this is the status of the last command issued */
247 	uint_t		status;
248 } scsa2usb_last_cmd_t;
249 
250 /*
251  * override values
252  *	These values may be set in scsa2usb.conf for particular devices
253  */
254 typedef struct scsa2usb_ov {
255 	int	vid;		/* vendor id */
256 	int	pid;		/* product id */
257 	int	rev;		/* revision */
258 	int	subclass;	/* subclass override */
259 	int	protocol;	/* protocol override */
260 	int	pmoff;		/* power management override */
261 	int	fake_removable;	/* removable device override */
262 	int	no_modesense;	/* no mode sense */
263 				/* no modesense, doorlock, PM, start/stop */
264 	int	reduced_cmd_support;
265 } scsa2usb_ov_t;
266 
267 
268 /*
269  * Per bulk device "state" data structure.
270  */
271 typedef struct scsa2usb_state {
272 	int			scsa2usb_instance;	/* Instance number    */
273 	int			scsa2usb_dev_state;	/* USB device state   */
274 	int			scsa2usb_flags; 	/* Per instance flags */
275 	int			scsa2usb_intfc_num;	/* Interface number   */
276 	dev_info_t		*scsa2usb_dip;		/* Per device. info   */
277 	scsa2usb_power_t	*scsa2usb_pm;		/* PM state info */
278 
279 	int			scsa2usb_transport_busy; /* ugen/sd traffic */
280 	int			scsa2usb_ugen_open_count;
281 	kcondvar_t		scsa2usb_transport_busy_cv;
282 	kthread_t		*scsa2usb_busy_thread;
283 
284 	kmutex_t		scsa2usb_mutex;		/* Per instance lock  */
285 
286 	struct scsi_hba_tran	*scsa2usb_tran;		/* SCSI transport ptr */
287 	struct scsi_pkt		*scsa2usb_cur_pkt;	/* SCSI packet ptr    */
288 
289 	usba_list_entry_t	scsa2usb_waitQ[SCSA2USB_MAX_LUNS];
290 							/* waitQ list */
291 	struct scsa2usb_cmd	*scsa2usb_arq_cmd;	/* ARQ cmd */
292 	struct buf		*scsa2usb_arq_bp;	/* ARQ buf */
293 
294 	dev_info_t		*scsa2usb_lun_dip[SCSA2USB_MAX_LUNS];
295 						/* store devinfo per LUN  */
296 	struct scsi_inquiry	scsa2usb_lun_inquiry[SCSA2USB_MAX_LUNS];
297 						/* store inquiry per LUN  */
298 	usb_if_descr_t		scsa2usb_intfc_descr;	/* Interface descr    */
299 	usb_ep_descr_t		scsa2usb_bulkin_ept;	/* Bulk In descriptor */
300 	usb_ep_descr_t		scsa2usb_bulkout_ept;	/* Bulkout descriptor */
301 	usb_ep_descr_t		scsa2usb_intr_ept;	/* Intr ept descr */
302 
303 	usb_pipe_handle_t	scsa2usb_default_pipe;	/* Default pipe	Hndle */
304 	usb_pipe_handle_t	scsa2usb_intr_pipe;	/* Intr polling Hndle */
305 	usb_pipe_handle_t	scsa2usb_bulkin_pipe;	/* Bulk Inpipe Handle */
306 	usb_pipe_handle_t	scsa2usb_bulkout_pipe;	/* Bulk Outpipe Hndle */
307 
308 	uint_t			scsa2usb_pipe_state;	/* resetting state */
309 	uint_t			scsa2usb_tag;		/* current tag */
310 	uint_t			scsa2usb_pkt_state;	/* packet state */
311 	uint_t			scsa2usb_n_luns;	/* number of luns */
312 
313 	usb_log_handle_t	scsa2usb_log_handle;	/* log handle */
314 	struct scsa2usb_cpr	*scsa2usb_panic_info;	/* for cpr info */
315 
316 	size_t			scsa2usb_lbasize[SCSA2USB_MAX_LUNS];
317 							/* sector size */
318 	size_t			scsa2usb_totalsec[SCSA2USB_MAX_LUNS];
319 							/* total sectors */
320 	size_t			scsa2usb_secsz[SCSA2USB_MAX_LUNS];
321 							/* sector size */
322 	size_t			scsa2usb_max_bulk_xfer_size; /* from HCD */
323 
324 	usb_client_dev_data_t	*scsa2usb_dev_data;	/* USB registration */
325 	scsa2usb_last_cmd_t	scsa2usb_last_cmd;	/* last/prev command */
326 
327 	uint_t			scsa2usb_attrs;		/* for bad devices */
328 	uint_t			scsa2usb_cmd_protocol;	/* CMD protocol used */
329 	kthread_t		*scsa2usb_work_thread_id; /* handle commands */
330 
331 				/* conf file override values */
332 	uint_t			scsa2usb_subclass_override;
333 	uint_t			scsa2usb_protocol_override;
334 	char			*scsa2usb_override_str;
335 
336 				/* suppress repetitive disconnect warnings */
337 	boolean_t		scsa2usb_warning_given;
338 
339 	boolean_t		scsa2usb_rcvd_not_ready; /* received NOT */
340 							/* READY sense data */
341 
342 	usb_ugen_hdl_t		scsa2usb_ugen_hdl;	/* ugen support */
343 } scsa2usb_state_t;
344 
345 
346 /* for warlock */
347 _NOTE(MUTEX_PROTECTS_DATA(scsa2usb_state::scsa2usb_mutex, scsa2usb_state))
348 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_instance))
349 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_dip))
350 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_arq_cmd))
351 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_arq_bp))
352 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_bulkin_ept))
353 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_bulkout_ept))
354 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_intr_ept))
355 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_default_pipe))
356 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_intr_pipe))
357 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_bulkin_pipe))
358 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_bulkout_pipe))
359 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_log_handle))
360 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_intfc_num))
361 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_dev_data))
362 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_ugen_hdl))
363 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_state::scsa2usb_pm))
364 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsa2usb_power_t))
365 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_hba_tran_t))
366 _NOTE(SCHEME_PROTECTS_DATA("unshared data", usb_bulk_req_t))
367 
368 /* scsa2usb_pipe_state values */
369 #define	SCSA2USB_PIPE_NORMAL		0x00	/* no reset or clearing	*/
370 #define	SCSA2USB_PIPE_CLOSING		0x01	/* closing all pipes */
371 #define	SCSA2USB_PIPE_DEV_RESET		0x02	/* device specific reset */
372 
373 /* pkt xfer state machine */
374 #define	SCSA2USB_PKT_NONE		0	/* device is idle */
375 #define	SCSA2USB_PKT_PROCESS_CSW	1	/* device doing status again */
376 #define	SCSA2USB_PKT_DO_COMP		2	/* device is done xfer */
377 
378 /* scsa2usb_flags values */
379 #define	SCSA2USB_FLAGS_PIPES_OPENED	0x001	/* usb pipes are open */
380 #define	SCSA2USB_FLAGS_HBA_ATTACH_SETUP	0x002	/* scsi hba setup done */
381 #define	SCSA2USB_FLAGS_LOCKS_INIT	0x004	/* basic inits done */
382 
383 /* scsa2usb_cmd_protocol values */
384 #define	SCSA2USB_UNKNOWN_PROTOCOL	0x0000	/* unknown wire protocol */
385 #define	SCSA2USB_CB_PROTOCOL		0x0001	/* CBI wire protocol */
386 #define	SCSA2USB_CBI_PROTOCOL		0x0002	/* CBI w/ intr wire protocol */
387 #define	SCSA2USB_BULK_ONLY_PROTOCOL	0x0004	/* Bulk Only wire protocol */
388 
389 #define	SCSA2USB_SCSI_CMDSET		0x1000	/* SCSI command set followed */
390 #define	SCSA2USB_ATAPI_CMDSET		0x2000	/* ATAPI command set followed */
391 #define	SCSA2USB_UFI_CMDSET		0x4000	/* UFI command set followed */
392 #define	SCSA2USB_CMDSET_MASK		0x7000	/* OR al the above */
393 
394 #define	SCSA2USB_IS_UFI_CMDSET(s) \
395 	(((s)->scsa2usb_cmd_protocol & SCSA2USB_UFI_CMDSET))
396 #define	SCSA2USB_IS_SCSI_CMDSET(s) \
397 	(((s)->scsa2usb_cmd_protocol & SCSA2USB_SCSI_CMDSET))
398 #define	SCSA2USB_IS_ATAPI_CMDSET(s) \
399 	(((s)->scsa2usb_cmd_protocol & SCSA2USB_ATAPI_CMDSET))
400 
401 #define	SCSA2USB_IS_CB(s) \
402 	(((s)->scsa2usb_cmd_protocol & SCSA2USB_CB_PROTOCOL))
403 
404 #define	SCSA2USB_IS_CBI(s) \
405 	(((s)->scsa2usb_cmd_protocol & SCSA2USB_CBI_PROTOCOL))
406 
407 #define	SCSA2USB_IS_BULK_ONLY(s) \
408 	(((s)->scsa2usb_cmd_protocol & SCSA2USB_BULK_ONLY_PROTOCOL))
409 
410 /* check if it is ok to access the device and send command to it */
411 #define	SCSA2USB_DEVICE_ACCESS_OK(s) \
412 	((s)->scsa2usb_dev_state == USB_DEV_ONLINE)
413 
414 /* check if we are in any reset */
415 #define	SCSA2USB_IN_RESET(s) \
416 	(((s)->scsa2usb_pipe_state & SCSA2USB_PIPE_DEV_RESET) != 0)
417 
418 /* check if the device is busy */
419 #define	SCSA2USB_BUSY(s) \
420 	(((s)->scsa2usb_cur_pkt) || \
421 	((s)->scsa2usb_pipe_state != SCSA2USB_PIPE_NORMAL) || \
422 	((s)->scsa2usb_pkt_state != SCSA2USB_PKT_NONE))
423 
424 /* check if we're doing cpr */
425 #define	SCSA2USB_CHK_CPR(s) \
426 	(((s)->scsa2usb_dev_state == USB_DEV_SUSPENDED))
427 
428 /* check if we're either paniced or in cpr state */
429 #define	SCSA2USB_CHK_PANIC_CPR(s) \
430 	(ddi_in_panic() || SCSA2USB_CHK_CPR(s))
431 
432 /* reset scsa2usb state after pkt_comp is called */
433 #define	SCSA2USB_RESET_CUR_PKT(s) \
434 	(s)->scsa2usb_cur_pkt = NULL; \
435 	(s)->scsa2usb_pkt_state = SCSA2USB_PKT_NONE;
436 
437 /* print a panic sync message to the console */
438 #define	SCSA2USB_PRINT_SYNC_MSG(m, s) \
439 	if ((m) == B_TRUE) { \
440 		USB_DPRINTF_L1(DPRINT_MASK_SCSA, (s)->scsa2usb_log_handle, \
441 		    "syncing not supported"); \
442 		(m) = B_FALSE; \
443 	}
444 
445 /* Cancel callbacks registered during attach time */
446 #define	SCSA2USB_CANCEL_CB(id) \
447 	if ((id)) { \
448 		(void) callb_delete((id)); \
449 		(id) = 0; \
450 	}
451 
452 /* Set SCSA2USB_PKT_DO_COMP state if there is active I/O */
453 #define	SCSA2USB_SET_PKT_DO_COMP_STATE(s) \
454 	if ((s)->scsa2usb_cur_pkt) { \
455 		(s)->scsa2usb_pkt_state = SCSA2USB_PKT_DO_COMP; \
456 	}
457 
458 #define	SCSA2USB_FREE_MSG(data) \
459 	if ((data)) { \
460 		freemsg((data)); \
461 	}
462 
463 #define	SCSA2USB_FREE_BULK_REQ(req) \
464 	if ((req)) { \
465 		usb_free_bulk_req((req));	/* Free request */ \
466 	}
467 
468 
469 /* SCSA related */
470 #define	ADDR2TRAN(ap)		((ap)->a_hba_tran)
471 #define	TRAN2SCSA2USB(tran)	((scsa2usb_state_t *)(tran)->tran_hba_private)
472 #define	ADDR2SCSA2USB(ap)	(TRAN2SCSA2USB(ADDR2TRAN(ap)))
473 
474 #define	PKT_PRIV_LEN		16
475 
476 #define	PKT_DEFAULT_TIMEOUT	5
477 
478 /*
479  * auto request sense
480  */
481 #define	RQ_MAKECOM_COMMON(pktp, flag, cmd) \
482 	(pktp)->pkt_flags = (flag), \
483 	((union scsi_cdb *)(pktp)->pkt_cdbp)->scc_cmd = (cmd), \
484 	((union scsi_cdb *)(pktp)->pkt_cdbp)->scc_lun = \
485 	    (pktp)->pkt_address.a_lun
486 
487 #define	RQ_MAKECOM_G0(pktp, flag, cmd, addr, cnt) \
488 	RQ_MAKECOM_COMMON((pktp), (flag), (cmd)), \
489 	FORMG0ADDR(((union scsi_cdb *)(pktp)->pkt_cdbp), (addr)), \
490 	FORMG0COUNT(((union scsi_cdb *)(pktp)->pkt_cdbp), (cnt))
491 
492 
493 /* transport related */
494 #define	SCSA2USB_JUST_ACCEPT	0
495 #define	SCSA2USB_TRANSPORT	1
496 #define	SCSA2USB_REJECT		-1
497 
498 /*
499  * The scsa2usb_cpr_info data structure is used for cpr related
500  * callbacks. It is used for panic callbacks as well.
501  */
502 typedef struct scsa2usb_cpr {
503 	callb_cpr_t		cpr;		/* for cpr related info */
504 	struct scsa2usb_state	*statep;	/* for scsa2usb state info */
505 	kmutex_t		lockp;		/* mutex used by cpr_info_t */
506 } scsa2usb_cpr_t;
507 
508 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_cpr_t::cpr))
509 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsa2usb_cpr_t::statep))
510 
511 /*
512  * The scsa2usb_cmd data structure is defined here. It gets
513  * initialized per command that is sent to the device.
514  */
515 typedef struct scsa2usb_cmd {
516 	struct scsi_pkt		*cmd_pkt;		/* copy of pkt ptr */
517 	struct	buf		*cmd_bp;		/* copy of bp ptr */
518 	size_t			cmd_xfercount;		/* current xfer count */
519 	size_t			cmd_resid_xfercount;	/* last xfer resid */
520 	int			cmd_scblen;		/* status length */
521 	int			cmd_tag;		/* tag */
522 	int			cmd_timeout;		/* copy of pkt_time */
523 	uchar_t			cmd_cdb[SCSI_CDB_SIZE];	/* CDB */
524 	uchar_t			cmd_dir;		/* direction */
525 	uchar_t			cmd_actual_len; 	/* cdb len */
526 	uchar_t			cmd_cdblen;		/* requested  cdb len */
527 	struct scsi_arq_status	cmd_scb;		/* status, w/ arq */
528 
529 	/* used in multiple xfers */
530 	size_t			cmd_total_xfercount;	/* total xfer val */
531 	size_t			cmd_offset;		/* offset into buf */
532 	int			cmd_lba;		/* current xfer lba */
533 	int			cmd_done;		/* command done? */
534 	int			cmd_blksize;		/* block size */
535 	usba_list_entry_t	cmd_waitQ;		/* waitQ element */
536 } scsa2usb_cmd_t;
537 
538 /* for warlock */
539 _NOTE(SCHEME_PROTECTS_DATA("unique per packet or safe sharing",
540     scsi_cdb scsi_status scsi_pkt buf scsa2usb_cmd scsi_arq_status))
541 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_device scsi_address))
542 
543 /* scsa2usb_cdb position of fields in CDB */
544 #define	SCSA2USB_OPCODE		0		/* Opcode field */
545 #define	SCSA2USB_LUN		1		/* LUN field */
546 #define	SCSA2USB_LBA_0		2		/* LBA[0] field */
547 #define	SCSA2USB_LBA_1		3		/* LBA[1] field */
548 #define	SCSA2USB_LBA_2		4		/* LBA[2] field */
549 #define	SCSA2USB_LBA_3		5		/* LBA[3] field */
550 #define	SCSA2USB_LEN_0		7		/* LEN[0] field */
551 #define	SCSA2USB_LEN_1		8		/* LEN[1] field */
552 
553 /* macros to calculate LBA for 6/10/12-byte commands */
554 #define	SCSA2USB_LBA_6BYTE(pkt) \
555 	(((pkt)->pkt_cdbp[1] & 0x1f) << 16) + \
556 	((pkt)->pkt_cdbp[2] << 8) + (pkt)->pkt_cdbp[3]
557 #define	SCSA2USB_LEN_6BYTE(pkt)		(pkt)->pkt_cdbp[4]
558 
559 #define	SCSA2USB_LEN_10BYTE(pkt) \
560 	((pkt)->pkt_cdbp[7] << 8) + (pkt)->pkt_cdbp[8]
561 #define	SCSA2USB_LBA_10BYTE(pkt) \
562 	((pkt)->pkt_cdbp[2] << 24) + ((pkt)->pkt_cdbp[3] << 16) + \
563 	    ((pkt)->pkt_cdbp[4] << 8) +  (pkt)->pkt_cdbp[5]
564 
565 #define	SCSA2USB_LEN_12BYTE(pkt) \
566 	((pkt)->pkt_cdbp[6] << 24) + ((pkt)->pkt_cdbp[7] << 16) + \
567 	    ((pkt)->pkt_cdbp[8] << 8) +  (pkt)->pkt_cdbp[9]
568 #define	SCSA2USB_LBA_12BYTE(pkt) \
569 	((pkt)->pkt_cdbp[2] << 24) + ((pkt)->pkt_cdbp[3] << 16) + \
570 	    ((pkt)->pkt_cdbp[4] << 8) +  (pkt)->pkt_cdbp[5]
571 
572 /* macros to convert a pkt to cmd and vice-versa */
573 #define	PKT2CMD(pkt)		((scsa2usb_cmd_t *)(pkt)->pkt_ha_private)
574 #define	CMD2PKT(sp)		((sp)->cmd_pkt
575 
576 /* bulk pipe default timeout value - how long the command to be tried? */
577 #define	SCSA2USB_BULK_PIPE_TIMEOUT	(2 * USB_PIPE_TIMEOUT)
578 
579 /* drain timeout in seconds on the work thread */
580 #define	SCSA2USB_DRAIN_TIMEOUT		60
581 
582 /* scsa2usb pkt xfer status phase retry times */
583 #define	SCSA2USB_STATUS_RETRIES		3
584 
585 /*
586  * limit on the number of requests that can be queued per LUN:
587  * 3 for untagged queueing, 1 for scsiwatch and a margin of 2
588  */
589 #define	SCSA2USB_MAX_REQ_PER_LUN	6
590 
591 /*
592  * The following data structure is used to save the values returned
593  * by the READ_CAPACITY command. lba is the max allowed logical block
594  * address and blen is max allowed block size.
595  */
596 typedef struct scsa2usb_read_cap {
597 	uchar_t	scsa2usb_read_cap_lba3;		/* Max lba supported */
598 	uchar_t	scsa2usb_read_cap_lba2;
599 	uchar_t	scsa2usb_read_cap_lba1;
600 	uchar_t	scsa2usb_read_cap_lba0;
601 	uchar_t	scsa2usb_read_cap_blen3;	/* Max block size supported */
602 	uchar_t	scsa2usb_read_cap_blen2;
603 	uchar_t	scsa2usb_read_cap_blen1;
604 	uchar_t	scsa2usb_read_cap_blen0;
605 } scsa2usb_read_cap_t;
606 
607 #define	SCSA2USB_MK_32BIT(a, b, c, d) \
608 		(((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
609 
610 /* position of fields for SCMD_READ_CD CDB */
611 #define	SCSA2USB_READ_CD_LEN_0	6	/* LEN[0] of SCMD_READ_CD */
612 #define	SCSA2USB_READ_CD_LEN_1	7	/* LEN[1] of SCMD_READ_CD */
613 #define	SCSA2USB_READ_CD_LEN_2	8	/* LEN[2] of SCMD_READ_CD */
614 
615 /* macro to calculate LEN for SCMD_READ_CD command */
616 #define	SCSA2USB_LEN_READ_CD(pkt) \
617 	(((pkt)->pkt_cdbp[SCSA2USB_READ_CD_LEN_0] << 16) +\
618 	    ((pkt)->pkt_cdbp[SCSA2USB_READ_CD_LEN_1] << 8) +\
619 	    (pkt)->pkt_cdbp[SCSA2USB_READ_CD_LEN_2])
620 
621 /* Figure out Block Size before issuing a WRITE to CD-RW device */
622 #define	SCSA2USB_CDRW_BLKSZ(bcount, len)	((bcount) / (len));
623 #define	SCSA2USB_VALID_CDRW_BLKSZ(blksz) \
624 	(((blksz) == CDROM_BLK_2048) || ((blksz) == CDROM_BLK_2352) || \
625 	((blksz) == CDROM_BLK_2336) || ((blksz) == CDROM_BLK_2324) || \
626 	((blksz) == 0))
627 
628 /* debug and error msg logging */
629 #define	DPRINT_MASK_SCSA	0x0001		/* for SCSA */
630 #define	DPRINT_MASK_ATTA	0x0002		/* for ATTA */
631 #define	DPRINT_MASK_EVENTS	0x0004		/* for event handling */
632 #define	DPRINT_MASK_CALLBACKS	0x0008		/* for callbacks  */
633 #define	DPRINT_MASK_TIMEOUT	0x0010		/* for timeouts */
634 #define	DPRINT_MASK_DUMPING	0x0020		/* for dumping */
635 #define	DPRINT_MASK_PM		0x0040		/* for pwr mgmt */
636 #define	DPRINT_MASK_ALL		0xffffffff	/* for everything */
637 
638 #ifdef	DEBUG
639 #define	SCSA2USB_PRINT_CDB	scsa2usb_print_cdb
640 #else
641 #define	SCSA2USB_PRINT_CDB	0 &&
642 #endif
643 
644 /* ugen support */
645 #define	SCSA2USB_MINOR_UGEN_BITS_MASK	0xff
646 #define	SCSA2USB_MINOR_INSTANCE_MASK	~SCSA2USB_MINOR_UGEN_BITS_MASK
647 #define	SCSA2USB_MINOR_INSTANCE_SHIFT	8
648 
649 #define	SCSA2USB_MINOR_TO_INSTANCE(minor)	\
650 		(((minor) & SCSA2USB_MINOR_INSTANCE_MASK) >> \
651 		SCSA2USB_MINOR_INSTANCE_SHIFT)
652 
653 #ifdef __cplusplus
654 }
655 #endif
656 
657 #endif	/* _SYS_USB_SCSA2USB_H */
658