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