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