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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_SCSI_TARGETS_STDEF_H
27 #define	_SYS_SCSI_TARGETS_STDEF_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/sunddi.h>
32 #include <sys/note.h>
33 #include <sys/condvar.h>
34 #include <sys/kstat.h>
35 #include <sys/scsi/scsi_types.h>
36 #include <sys/scsi/generic/sense.h>
37 #include <sys/mtio.h>
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 /*
44  * Defines for SCSI tape drives.
45  */
46 
47 /*
48  * Maximum variable length record size for a single request
49  */
50 #define	ST_MAXRECSIZE_VARIABLE	65535
51 
52 /*
53  * If the requested record size exceeds ST_MAXRECSIZE_VARIABLE,
54  * then the following define is used.
55  */
56 #define	ST_MAXRECSIZE_VARIABLE_LIMIT	65534
57 
58 #define	ST_MAXRECSIZE_FIXED	(63<<10)	/* maximum fixed record size */
59 #define	INF 1000000000
60 
61 /*
62  * Supported tape device types plus default type for opening.
63  * Types 10 - 13, are special (ancient too) drives - *NOT SUPPORTED*
64  * Types 14 - 1f, are 1/4-inch cartridge drives.
65  * Types 20 - 28, are 1/2-inch cartridge or reel drives.
66  * Types 28+, are rdat (vcr) drives.
67  */
68 #define	ST_TYPE_INVALID		0x00
69 
70 #define	ST_TYPE_SYSGEN1	MT_ISSYSGEN11	/* Sysgen with QIC-11 only */
71 #define	ST_TYPE_SYSGEN	MT_ISSYSGEN	/* Sysgen with QIC-24 and QIC-11 */
72 
73 #define	ST_TYPE_DEFAULT	MT_ISDEFAULT	/* Generic 1/4" or undetermined  */
74 #define	ST_TYPE_EMULEX	MT_ISMT02	/* Emulex MT-02 */
75 #define	ST_TYPE_ARCHIVE	MT_ISVIPER1	/* Archive QIC-150 */
76 #define	ST_TYPE_WANGTEK	MT_ISWANGTEK1	/* Wangtek QIC-150 */
77 
78 #define	ST_TYPE_CDC	MT_ISCDC	/* CDC - (not tested) */
79 #define	ST_TYPE_FUJI	MT_ISFUJI	/* Fujitsu - (not tested) */
80 #define	ST_TYPE_KENNEDY	MT_ISKENNEDY	/* Kennedy */
81 #define	ST_TYPE_ANRITSU	MT_ISANRITSU	/* Anritsu */
82 #define	ST_TYPE_HP	MT_ISHP		/* HP */
83 #define	ST_TYPE_HIC	MT_ISCCS23	/* Generic 1/2" Cartridge */
84 #define	ST_TYPE_REEL	MT_ISCCS24	/* Generic 1/2" Reel Tape */
85 #define	ST_TYPE_DAT	MT_ISCCS28	/* Generic DAT Tape */
86 
87 #define	ST_TYPE_EXABYTE	MT_ISEXABYTE	/* Exabyte 8200 */
88 #define	ST_TYPE_EXB8500	MT_ISEXB8500	/* Exabyte 8500 */
89 #define	ST_TYPE_WANGTHS	MT_ISWANGTHS	/* Wangtek 6130HS */
90 #define	ST_TYPE_WANGDAT	MT_ISWANGDAT	/* WangDAT */
91 #define	ST_TYPE_PYTHON  MT_ISPYTHON	/* Archive Python DAT */
92 #define	ST_TYPE_STC3490 MT_ISSTC	/* IBM STC 3490 */
93 #define	ST_TYPE_TAND25G MT_ISTAND25G	/* TANDBERG 2.5G */
94 #define	ST_TYPE_DLT	MT_ISDLT	/* DLT */
95 #define	ST_TYPE_STK9840	MT_ISSTK9840	/* StorageTek 9840, 9940, 9840B */
96 #define	ST_TYPE_BMDLT1	MT_ISBMDLT1	/* Benchmark DTL1 */
97 #define	ST_TYPE_LTO	MT_LTO		/* sun: LTO's by HP, Seagate, IBM.. */
98 #define	ST_TYPE_AIT	MT_ISAIT	/* Sony AIT I, II, III and SAIT */
99 #define	ST_LAST_TYPE	ST_TYPE_AIT	/* Add new above type and change this */
100 
101 
102 /* Internal flags */
103 #define	ST_DYNAMIC		0x2000	/* Device name has been dynamically */
104 					/* alloc'ed from the st.conf entry, */
105 					/* instead of being used from the */
106 					/* st_drivetypes array. */
107 
108 /*
109  * Defines for supported drive options
110  *
111  * WARNING : THESE OPTIONS SHOULD NEVER BE CHANGED, AS OLDER CONFIGURATIONS
112  * 		WILL DEPEND ON THE FLAG VALUES REMAINING THE SAME
113  */
114 #define	ST_VARIABLE		0x001	/* Device supports variable	*/
115 					/* length record sizes		*/
116 #define	ST_QIC			0x002	/* QIC tape device 		*/
117 #define	ST_REEL			0x004	/* 1/2-inch reel tape device	*/
118 #define	ST_BSF			0x008	/* Device supports backspace	*/
119 					/* file as in mt(1) bsf : 	*/
120 					/* backspace over EOF marks.	*/
121 					/* Devices not supporting bsf 	*/
122 					/* will fail with ENOTTY upon	*/
123 					/* use of bsf			*/
124 #define	ST_BSR			0x010	/* Device supports backspace	*/
125 					/* record as in mt(1) bsr :	*/
126 					/* backspace over records. If	*/
127 					/* the device does not support 	*/
128 					/* bsr, the st driver emulates	*/
129 					/* the action by rewinding the	*/
130 					/* tape and using forward space	*/
131 					/* file (fsf) to the correct	*/
132 					/* file and then uses forward	*/
133 					/* space record (fsr) to the	*/
134 					/* correct  record		*/
135 #define	ST_LONG_ERASE		0x020	/* Device needs a longer time	*/
136 					/* than normal to erase		*/
137 #define	ST_AUTODEN_OVERRIDE	0x040	/* Auto-Density override flag	*/
138 					/* Device can figure out the	*/
139 					/* tape density automatically,	*/
140 					/* without issuing a		*/
141 					/* mode-select/mode-sense 	*/
142 #define	ST_NOBUF		0x080	/* Don't use buffered mode.	*/
143 					/* This disables the device's	*/
144 					/* ability for buffered	writes	*/
145 					/* I.e. The device acknowledges	*/
146 					/* write completion after the	*/
147 					/* data is written to the	*/
148 					/* device's buffer, but before	*/
149 					/* all the data is actually	*/
150 					/* written to tape		*/
151 #define	ST_RESERVED_BIT1	0x100	/* resreved bit 		*/
152 					/* parity while talking to it. 	*/
153 #define	ST_KNOWS_EOD		0x200	/* Device knows when EOD (End	*/
154 					/* of Data) has been reached.	*/
155 					/* If the device knows EOD, st	*/
156 					/* uses fast file skipping.	*/
157 					/* If it does not know EOD,	*/
158 					/* file skipping happens one	*/
159 					/* file at a time. 		*/
160 #define	ST_UNLOADABLE		0x400	/* Device will not complain if	*/
161 					/* the st driver is unloaded &	*/
162 					/* loaded again; e.g. will	*/
163 					/* return the correct inquiry	*/
164 					/* string			*/
165 #define	ST_SOFT_ERROR_REPORTING 0x800	/* Do request or log sense on	*/
166 					/* close to report soft errors.	*/
167 					/* Currently only Exabyte and	*/
168 					/* DAT drives support this	*/
169 					/* feature.  			*/
170 #define	ST_LONG_TIMEOUTS	0x1000	/* Device needs 5 times longer	*/
171 					/* timeouts for normal		*/
172 					/* operation			*/
173 #define	ST_BUFFERED_WRITES	0x4000	/* The data is buffered in the	*/
174 					/* driver and pre-acked to the	*/
175 					/* application 			*/
176 #define	ST_NO_RECSIZE_LIMIT	0x8000	/* For variable record size	*/
177 					/* devices only. If flag is	*/
178 					/* set, then don't limit	*/
179 					/* record size to 64k as in	*/
180 					/* pre-Solaris 2.4 releases.	*/
181 					/* The only limit on the	*/
182 					/* record size will be the max	*/
183 					/* record size the device can	*/
184 					/* handle or the max DMA	*/
185 					/* transfer size of the		*/
186 					/* machine, which ever is	*/
187 					/* smaller. Beware of		*/
188 					/* incompatabilities with	*/
189 					/* tapes of pre-Solaris 2.4	*/
190 					/* OS's written with large	*/
191 					/* (>64k) block sizes, as	*/
192 					/* their true block size is	*/
193 					/* a max of approx 64k		*/
194 #define	ST_MODE_SEL_COMP	0x10000	/* use mode select of device	*/
195 					/* configuration page (0x10) to */
196 					/* enable/disable compression	*/
197 					/* instead of density codes for */
198 					/* the "c" and "u" devices	*/
199 #define	ST_NO_RESERVE_RELEASE	0x20000	/* For devices which do not	*/
200 					/* support RESERVE/RELEASE SCSI	*/
201 					/* command. If this is enabled	*/
202 					/* then reserve/release would	*/
203 					/* not be used during open/	*/
204 					/* close for High Availability	*/
205 #define	ST_READ_IGNORE_ILI	0x40000 /* This flag is only applicable */
206 					/* to variable block devices 	*/
207 					/* which support the SILI bit	*/
208 					/* option. It indicates that 	*/
209 					/* the SILI bit will be ignored */
210 					/* during reads  		*/
211 #define	ST_READ_IGNORE_EOFS 	0x80000 /* When this flag is set two 	*/
212 					/* EOF marks do not indicate an */
213 					/* EOM. This option is only	*/
214 					/* supported on 1/2" reel tapes */
215 #define	ST_SHORT_FILEMARKS	0x100000 /* This option applies only to */
216 					/* EXABYTE 8mm tape drives 	*/
217 					/* which support short 		*/
218 					/* filemarks. When this flag 	*/
219 					/* is set, short filemarks 	*/
220 					/* will be used for writing	*/
221 					/* filemarks. 			*/
222 #define	ST_EJECT_ON_CHANGER_FAILURE 0x200000 /* When this flag is set   */
223 					/* and the tape is trapped in   */
224 					/* the medium changer, the tape */
225 					/* is automatically ejected	*/
226 #define	ST_RETRY_ON_RECOVERED_DEFERRED_ERROR 0x400000
227 					/* This option applies only to  */
228 					/* IBM MAGSTAR 3590. If this    */
229 					/* flag is set, the st driver   */
230 					/* will retry the last cmd if   */
231 					/* the last error cause a check */
232 					/* condition with error code    */
233 					/* 0x71 and sense code  0x01 	*/
234 #define	ST_KNOWS_MEDIA		0x800000 /* Use configured media type	*/
235 					/* detected to select correct   */
236 					/* density code.		*/
237 #define	ST_WORMABLE		0x1000000
238 					/* Drive is capable of doing	*/
239 					/* Write Appends only at EOM	*/
240 					/* if WORM media type is loaded */
241 #define	ST_CLN_TYPE_1		0x10000000 /* When this flag is set,	*/
242 					/* the tape drive provides the	*/
243 					/* clean bit information in	*/
244 					/* byte 21, bitmask 0x08 of	*/
245 					/* Request Sense data		*/
246 #define	ST_CLN_TYPE_2		0x20000000 /* When this flag is set,	*/
247 					/* the tape drive provides the	*/
248 					/* clean bit information in	*/
249 					/* byte 70, bitmask 0xc0 of	*/
250 					/* Request Sense data		*/
251 #define	ST_CLN_TYPE_3		0x40000000 /* When this flag is set,	*/
252 					/* the tape drive provides the	*/
253 					/* clean bit information in	*/
254 					/* byte 18, bitmask 0x01 of	*/
255 					/* Request Sense data		*/
256 
257 #define	ST_CLN_MASK	(ST_CLN_TYPE_1 | ST_CLN_TYPE_2 | ST_CLN_TYPE_3)
258 #define	ST_VALID_OPTS	(ST_VARIABLE | ST_QIC | ST_REEL | ST_BSF | ST_BSR |\
259 	ST_LONG_ERASE | ST_AUTODEN_OVERRIDE | ST_NOBUF | ST_KNOWS_EOD |\
260 	ST_UNLOADABLE | ST_SOFT_ERROR_REPORTING | ST_LONG_TIMEOUTS |\
261 	ST_NO_RECSIZE_LIMIT | ST_MODE_SEL_COMP | ST_NO_RESERVE_RELEASE |\
262 	ST_READ_IGNORE_ILI | ST_READ_IGNORE_EOFS | ST_SHORT_FILEMARKS |\
263 	ST_EJECT_ON_CHANGER_FAILURE | ST_RETRY_ON_RECOVERED_DEFERRED_ERROR |\
264 	ST_WORMABLE | ST_CLN_TYPE_1 | ST_CLN_TYPE_2 | ST_CLN_TYPE_3)
265 
266 #define	NDENSITIES	MT_NDENSITIES
267 #define	NSPEEDS		MT_NSPEEDS
268 
269 /*
270  * defines for Log Sense Pages
271  */
272 #define	SUPPORTED_LOG_PAGES_PAGE	0x00
273 #define	TAPE_SEQUENTIAL_PAGE		0x0c
274 #define	TAPE_ALERT_PAGE			0x2e
275 
276 /*
277  * Log Page Control definitions
278  */
279 #define	CURRENT_THRESHOLD_VALUES	0x00
280 #define	CURRENT_CUMULATIVE_VALUES	0x40
281 #define	DEFAULT_THRESHOLD_VALUES	0x80
282 #define	DEFAULT_CUMULATIVE_VALUES	0xC0
283 
284 /*
285  * Tape Alert Flag definitions
286  */
287 typedef enum {
288 	TAF_READ_WARN			= 0x01,
289 	TAF_WRITE_WARN			= 0x02,
290 	TAF_HARD_ERR			= 0x03,
291 	TAF_MEDIA_ERR			= 0x04,
292 	TAF_READ_FAIL			= 0x05,
293 	TAF_WRITE_FAIL			= 0x06,
294 	TAF_MEDIA_LIFE			= 0x07,
295 	TAF_MEDIA_NOT_DATA_GRADE	= 0x08,
296 	TAF_WRITE_PROTECTED		= 0x09,
297 	TAF_NO_MEDIA_REMOVE		= 0x0A,
298 	TAF_CLEANING_MEDIA		= 0x0B,
299 	TAF_UNSUPPERTED_FORMAT		= 0x0C,
300 	TAF_RECOVERED_TAPE_BREAK	= 0x0D,
301 	TAF_TAPE_BREAK_FAUL		= 0x0E,
302 	TAF_CART_MEM_FAIL		= 0x0F,
303 	TAF_FORCED_EJECT		= 0x10,
304 	TAF_READ_ONLY_FORMAT		= 0x11,
305 	TAF_TAPE_DIR_CORRUPT		= 0x12,
306 	TAF_NEARING_MEDIA_LIFE		= 0x13,
307 	TAF_CLEAN_NOW			= 0x14,
308 	TAF_CLEAN_PERIODIC		= 0x15,
309 	TAF_EXP_CLEAN_CART		= 0x16,
310 	TAF_INVALID_CLEAN_MEDIA		= 0x17,
311 	TAF_RETENSION_REQUEST		= 0x18,
312 	TAF_DUAL_PORT_INTERFACE_ERR	= 0x19,
313 	TAF_COOLING_FAN_FAIL		= 0x1A,
314 	TAF_POWER_SUPPLY_FAIL		= 0x1B,
315 	TAF_POWER_CONSUMPTION		= 0x1C,
316 	TAF_DRIVE_MAINT_REQUEST		= 0x1D,
317 	TAF_HARDWARE_A			= 0x1E,
318 	TAF_HARDWARE_B			= 0x1F,
319 	TAF_INTERFACE			= 0x20,
320 	TAF_EJECT_MEDIA			= 0x21,
321 	TAF_DOWNLOAD_FAIL		= 0x22,
322 	TAF_DRIVE_HUMIDITY		= 0x23,
323 	TAF_DRIVE_TEMP			= 0x24,
324 	TAF_DRIVE_VOLTAGE		= 0x25,
325 	TAF_PREDICTIVE_FAIL		= 0x26,
326 	TAF_DIAG_REQUIRED		= 0x27,
327 	TAF_LOADER_HDWR_A		= 0x28,
328 	TAF_LOADER_STRAY_TAPE		= 0x29,
329 	TAF_LOADER_HDWR_B		= 0x2A,
330 	TAF_LOADER_DOOR			= 0x2B,
331 	TAF_LOADER_HDWR_C		= 0x2C,
332 	TAF_LOADER_MAGAZINE		= 0x2D,
333 	TAF_LOADER_PREDICTIVE_FAIL	= 0x2E,
334 	TAF_LOST_STATISTICS		= 0x32,
335 	TAF_TAPE_DIR_CURRUPT_UNLOAD	= 0x33,
336 	TAF_TAPE_SYS_WRT_FAIL		= 0x34,
337 	TAF_TAPE_SYS_RD_FAIL		= 0x35,
338 	TAF_NO_START_OF_DATA		= 0x36,
339 	TAF_WORM_INTEGRITY		= 0x3B,
340 	TAF_WORM_OVRWRT_ATTEMPT		= 0x3C
341 }tape_alert_flags;
342 
343 /*
344  * For ST_TYPE_STK9840 drives only. STK drive doesn't support retension
345  * so they reuse TAF_RETENSION_REQUEST.
346  */
347 #define	CLEAN_FOR_ERRORS		 0x18
348 
349 
350 #define	TAPE_ALERT_SUPPORT_UNKNOWN	0x00
351 #define	TAPE_ALERT_NOT_SUPPORTED	0x01
352 #define	TAPE_ALERT_SUPPORTED		0x02
353 #define	TAPE_ALERT_STILL_DIRTY		0x04
354 #define	TAPE_SEQUENTIAL_SUPPORTED	0x08
355 #define	TAPE_PREVIOUSLY_DIRTY		0x10
356 
357 #define	TAPE_ALERT_MAX_PARA		64
358 #define	TAPE_SEQUENTIAL_PAGE_PARA	64	/* way more then really used */
359 #define	SEQUENTIAL_NEED_CLN		0x0100
360 
361 /*
362  * Parameters
363  */
364 #define	ST_NAMESIZE	44	/* size of pretty string for vid/pid */
365 #define	VIDLEN		8	/* size of vendor identifier length */
366 #define	PIDLEN		16	/* size of product identifier length */
367 #define	VIDPIDLEN	(VIDLEN + PIDLEN)
368 
369 
370 struct st_drivetype {
371 	char	name[ST_NAMESIZE];	/* Name, for debug */
372 	char	length;			/* Length of vendor id */
373 	char	vid[VIDPIDLEN];		/* Vendor id and model (product) id */
374 	char	type;			/* Drive type for driver */
375 	int	bsize;			/* Block size */
376 	int	options;		/* Drive options */
377 	int	max_rretries;		/* Max read retries */
378 	int	max_wretries;		/* Max write retries */
379 	uchar_t	densities[NDENSITIES];	/* density codes, low->hi */
380 	uchar_t	default_density;	/* default density for this drive */
381 	uchar_t	mediatype[NDENSITIES];	/* was speed. mediatype for density. */
382 	ushort_t non_motion_timeout;	/* Inquiry type commands */
383 	ushort_t io_timeout;		/* I/O timeout in seconds */
384 	ushort_t rewind_timeout;	/* rewind timeout in seconds */
385 	ushort_t space_timeout;		/* space cmd timeout in seconds */
386 	ushort_t load_timeout;		/* load tape time in seconds */
387 	ushort_t unload_timeout;	/* unload tape time in seconds */
388 	ushort_t erase_timeout;		/* erase timeout. seconds */
389 };
390 
391 #define	MINUTES(val)	((val) * 60)
392 
393 struct comp_mode_page {
394 #if defined(_BIT_FIELDS_LTOH)
395 	uchar_t	:		6,
396 		dcc:		1,	/* Data Compression Capable */
397 		dce:		1;	/* Data Compression Enable */
398 	uchar_t	:		5,
399 		red:		2,	/* Report Exceptions on Decompress */
400 		dde:		1;	/* Data Decompression Enabled */
401 	uchar_t	comp_alg_msb;		/* Compression Algorithm */
402 	uchar_t comp_alg_high;
403 	uchar_t	comp_alg_low;
404 	uchar_t	comp_alg_lsb;
405 	uchar_t	decomp_alg_msb;		/* Decompression Algorithm */
406 	uchar_t decomp_alg_high;
407 	uchar_t	decomp_alg_low;
408 	uchar_t	decomp_alg_lsb;
409 	uchar_t	reservered0;
410 	uchar_t	reservered1;
411 	uchar_t	reservered2;
412 	uchar_t	reservered3;
413 
414 #elif defined(_BIT_FIELDS_HTOL)
415 	uchar_t	dce:		1,	/* Data Compression Enable */
416 		dcc:		1,	/* Data Compression Capable */
417 		:		6;
418 	uchar_t	dde:		1,	/* Data Decompression Enabled */
419 		red:		2,	/* Report Exceptions on Decompress */
420 		:		5;
421 	uchar_t	comp_alg_msb;		/* Compression Algorithm */
422 	uchar_t comp_alg_high;
423 	uchar_t	comp_alg_low;
424 	uchar_t	comp_alg_lsb;
425 	uchar_t	decomp_alg_msb;		/* Decompression Algorithm */
426 	uchar_t decomp_alg_high;
427 	uchar_t	decomp_alg_low;
428 	uchar_t	decomp_alg_lsb;
429 	uchar_t	reservered0;
430 	uchar_t	reservered1;
431 	uchar_t	reservered2;
432 	uchar_t	reservered3;
433 #endif
434 };
435 
436 struct dev_mode_page {
437 #if defined(_BIT_FIELDS_LTOH)
438 	uchar_t	act_format:	5,	/* active format */
439 		caf:		1,	/* Change Active Format */
440 		cap:		1,	/* Change Active Partition OBSOLETE */
441 		:		1;
442 	uchar_t	act_partition;		/* active partition */
443 	uchar_t	wrt_buf_full_ratio;	/* write buffer full ratio */
444 	uchar_t	rd_buf_full_ratio;	/* read buffer full ratio */
445 	uchar_t	wrt_delay_time_msb;	/* write delay time MSB */
446 	uchar_t	wrt_delay_time_lsb;	/* write delay time LSB */
447 	uchar_t	rew:		1,	/* Report Early Warning */
448 		robo:		1,	/* Reverse Object Buffer Order */
449 		socf:		2,	/* Stop On Consecutive Filemarks */
450 		avc:		1,	/* Automatic Velocity Control */
451 		rsmk:		1,	/* Report SetMarKs OBSOLETE */
452 		lois:		1,	/* Logical Object Identifiers Support */
453 		obr:		1;	/* Object Buffer Recovery */
454 	uchar_t	gap_size;		/* OBSOLETE */
455 	uchar_t	bam:		1,	/* Block Address Mode */
456 		bmal:		1,	/* Block Address Mode Lock */
457 		swp:		1,	/* Software Write Protection */
458 		sew:		1,	/* Sync data after Early Warning */
459 		eeg:		1,	/* Enable Early Waring */
460 		eod_defined:	3;
461 	uchar_t	buf_size_leot_msb;	/* Buffer size after early warning */
462 	uchar_t	buf_size_leot_mid;
463 	uchar_t	buf_size_leot_lsb;
464 	uchar_t	comp_alg;		/* Compression Algorithm (enable) */
465 	uchar_t	prmwp:		1,	/* PeRManent Write Protect */
466 		perswp:		1,	/* persistant write protection */
467 		asocwp:		1,	/* associated write protect */
468 		rew_on_rst:	2,	/* rewind on reset */
469 		oir:		1,	/* Only If Reserved */
470 		wtre:		2;	/* Worm Tamper Read Enable */
471 
472 #elif defined(_BIT_FIELDS_HTOL)
473 	uchar_t	:		1,
474 		cap:		1,	/* Change Active Partition OBSOLETE */
475 		caf:		1,	/* Change Active Format */
476 		act_format:	5;	/* active format */
477 	uchar_t	act_partition;		/* active partition */
478 	uchar_t	wrt_buf_full_ratio;	/* write buffer full ratio */
479 	uchar_t	rd_buf_full_ratio;	/* read buffer full ratio */
480 	uchar_t	wrt_delay_time_msb;	/* write delay time MSB */
481 	uchar_t	wrt_delay_time_lsb;	/* write delay time LSB */
482 	uchar_t	obr:		1,	/* Object Buffer Recovery */
483 		lois:		1,	/* Logical Object Identifiers Support */
484 		rsmk:		1,	/* Report SetMarKs OBSOLETE */
485 		avc:		1,	/* Automatic Velocity Control */
486 		socf:		2,	/* Stop On Consecutive Filemarks */
487 		robo:		1,	/* Reverse Object Buffer Order */
488 		rew:		1;	/* Report Early Warning */
489 	uchar_t	gap_size;		/* OBSELETE */
490 	uchar_t	eod_defined:	3,
491 		eeg:		1,	/* Enable Early Waring */
492 		sew:		1,	/* Sync data after Early Warning */
493 		swp:		1,	/* Software Write Protection */
494 		bmal:		1,	/* Block Address Mode Lock */
495 		bam:		1;	/* Block Address Mode */
496 	uchar_t	buf_size_leot_msb;	/* Buffer size after early warning */
497 	uchar_t	buf_size_leot_mid;
498 	uchar_t	buf_size_leot_lsb;
499 	uchar_t	comp_alg;		/* Compression Algorithm (enable) */
500 	uchar_t	wtre:		2,	/* Worm Tamper Read Enable */
501 		oir:		1,	/* Only If Reserved */
502 		rew_on_rst:	2,	/* rewind on reset */
503 		asocwp:		1,	/* associated write protect */
504 		perswp:		1,	/* persistant write protection */
505 		prmwp:		1;	/* PeRManent Write Protect */
506 #endif
507 };
508 
509 typedef union {
510 	struct comp_mode_page	comp;
511 	struct dev_mode_page	dev;
512 }modepage;
513 
514 /*
515  *
516  * Parameter list for the MODE_SELECT and MODE_SENSE commands.
517  * The parameter list contains a header, followed by zero or more
518  * block descriptors, followed by vendor unique parameters, if any.
519  *
520  */
521 #define	MSIZE	0x0c		/* Size without additional pages */
522 struct seq_mode {
523 #if defined(_BIT_FIELDS_LTOH)
524 	uchar_t	data_len;	/* sense data length, sense only */
525 	uchar_t	media_type;	/* medium type, sense only */
526 	uchar_t	speed	:4,	/* speed */
527 		bufm	:3,	/* buffered mode */
528 		wp	:1;	/* write protected, sense only */
529 	uchar_t	bd_len;		/* block length in bytes */
530 	uchar_t	density;	/* density code */
531 	uchar_t	high_nb;	/* number of logical blocks on the medium */
532 	uchar_t	mid_nb;		/* that are to be formatted with the density */
533 	uchar_t	low_nb;		/* code and block length in block descriptor */
534 	uchar_t	reserved;	/* reserved */
535 	uchar_t	high_bl;	/* block length */
536 	uchar_t	mid_bl;		/*   "      "   */
537 	uchar_t	low_bl;		/*   "      "   */
538 	uchar_t page_code:	6,
539 		:		1,
540 		ps:		1; /* Page Savable sense only */
541 	uchar_t	page_len;
542 	modepage page;
543 
544 #elif defined(_BIT_FIELDS_HTOL)
545 	uchar_t	data_len;	/* sense data length, sense only */
546 	uchar_t	media_type;	/* medium type, sense only */
547 	uchar_t	wp	:1,	/* write protected, sense only */
548 		bufm	:3,	/* buffered mode */
549 		speed	:4;	/* speed */
550 	uchar_t	bd_len;		/* block length in bytes */
551 	uchar_t	density;	/* density code */
552 	uchar_t	high_nb;	/* number of logical blocks on the medium */
553 	uchar_t	mid_nb;		/* that are to be formatted with the density */
554 	uchar_t	low_nb;		/* code and block length in block descriptor */
555 	uchar_t	reserved;	/* reserved */
556 	uchar_t	high_bl;	/* block length */
557 	uchar_t	mid_bl;		/*   "      "   */
558 	uchar_t	low_bl;		/*   "      "   */
559 	uchar_t	ps:		1, /* Page Savable sense only */
560 		:		1,
561 		page_code:	6;
562 	uchar_t	page_len;
563 	modepage page;
564 #else
565 #error	One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined
566 #endif	/* _BIT_FIELDS_LTOH */
567 };
568 
569 /*
570  * Data returned from the READ BLOCK LIMITS command.
571  */
572 
573 #define	RBLSIZE	(sizeof (struct read_blklim))
574 struct read_blklim {
575 #if defined(_BIT_FIELDS_HTOL)
576 	uchar_t	reserved:	3;	/* reserved */
577 	uchar_t granularity:	5;	/* Minimum Modularity */
578 #elif defined(_BIT_FIELDS_LTOH)
579 	uchar_t granularity:	5;	/* Minimum Modularity */
580 	uchar_t	reserved:	3;	/* reserved */
581 #endif
582 	uchar_t	max_hi;			/* Maximum block length, high byte */
583 	uchar_t	max_mid;		/* Maximum block length, middle byte */
584 	uchar_t	max_lo;			/* Maximum block length, low byte */
585 	uchar_t	min_hi;			/* Minimum block length, high byte */
586 	uchar_t	min_lo;			/* Minimum block length, low byte */
587 };
588 
589 /*
590  * operation codes
591  */
592 typedef enum {
593 	ST_OP_NIL,
594 	ST_OP_CTL,
595 	ST_OP_READ,
596 	ST_OP_WRITE,
597 	ST_OP_WEOF
598 }optype;
599 
600 /*
601  * eof/eot/eom codes.
602  */
603 typedef enum {
604 	ST_NO_EOF,
605 	ST_EOF_PENDING,		/* filemark pending */
606 	ST_EOF,			/* at filemark */
607 	ST_EOT_PENDING,		/* logical eot pending */
608 	ST_EOT,			/* at logical eot */
609 	ST_EOM,			/* at physical eot */
610 	ST_WRITE_AFTER_EOM	/* flag for allowing writes after EOM */
611 }pstatus;
612 
613 typedef enum { invalid, legacy, logical } posmode;
614 
615 typedef struct tapepos {
616 	uint64_t lgclblkno;
617 	int32_t fileno;
618 	int32_t blkno;
619 	int32_t partition;
620 	pstatus eof;			/* eof states */
621 	posmode	pmode;
622 	char	pad[4];
623 }tapepos_t;
624 
625 /* byte 1 of cdb for type of read position command */
626 typedef enum {
627 	SHORT_POS	= 0,
628 	LONG_POS	= 6,
629 	EXT_POS		= 8,
630 	NO_POS		= 0xff	/* Drive doesn't support read position */
631 } read_p_types;
632 
633 
634 /*
635  * Data returned from the READ POSITION command.
636  */
637 
638 typedef struct tape_position {
639 #if defined(_BIT_FIELDS_HTOL)
640 	uchar_t begin_of_part:	1;
641 	uchar_t end_of_part:	1;
642 	uchar_t blk_cnt_unkwn:	1;
643 	uchar_t byte_cnt_unkwn:	1;
644 	uchar_t reserved0:	1;
645 	uchar_t blk_posi_unkwn:	1;
646 	uchar_t posi_err:	1;
647 	uchar_t reserved1:	1;
648 #elif defined(_BIT_FIELDS_LTOH)
649 	uchar_t reserved1:	1;
650 	uchar_t posi_err:	1;
651 	uchar_t blk_posi_unkwn:	1;
652 	uchar_t reserved0:	1;
653 	uchar_t byte_cnt_unkwn:	1;
654 	uchar_t blk_cnt_unkwn:	1;
655 	uchar_t end_of_part:	1;
656 	uchar_t begin_of_part:	1;
657 #endif
658 	uchar_t partition_number;
659 	uchar_t reserved2[2];
660 	uint32_t host_block;
661 	uint32_t media_block;
662 	uchar_t reserved3;
663 	uchar_t block_in_buff[3];
664 	uint32_t byte_in_buff;
665 }tape_position_t;
666 
667 
668 typedef struct tape_position_long {
669 #if defined(_BIT_FIELDS_HTOL)
670 	uint32_t begin_of_part:	1;
671 	uint32_t end_of_part:	1;
672 	uint32_t reserved0:	2;
673 	uint32_t mrk_posi_unkwn:1;
674 	uint32_t blk_posi_unkwn:1;
675 	uint32_t reserved1:	2;
676 #elif defined(_BIT_FIELDS_LTOH)
677 	uint32_t reserved1:	2;
678 	uint32_t blk_posi_unkwn:1;
679 	uint32_t mrk_posi_unkwn:1;
680 	uint32_t reserved0:	2;
681 	uint32_t end_of_part:   1;
682 	uint32_t begin_of_part: 1;
683 #endif
684 	uint32_t reserved2:	24;
685 	uint32_t partition;
686 	uint64_t block_number;
687 	uint64_t file_number;
688 	uint64_t set_number;
689 }tape_position_long_t;
690 
691 typedef struct tape_position_ext {
692 #if defined(_BIT_FIELDS_HTOL)
693 	uint32_t begin_of_part:	1;
694 	uint32_t end_of_part:	1;
695 	uint32_t blk_cnt_unkwn:	1;
696 	uint32_t byte_cnt_unkwn:1;
697 	uint32_t mrk_posi_unkwn:1;
698 	uint32_t blk_posi_unkwn:1;
699 	uint32_t posi_err:	1;
700 	uint32_t reserved0:	1;
701 	uint32_t partition:	8;
702 	uint32_t parameter_len:	16;
703 /* start next word */
704 	uint32_t reserved1:	8;
705 	uint32_t blks_in_buf:	24;
706 #elif defined(_BIT_FIELDS_LTOH)
707 	uint32_t parameter_len:	16;
708 	uint32_t partition:	8;
709 	uint32_t reserved0:	1;
710 	uint32_t posi_err:	1;
711 	uint32_t blk_posi_unkwn:1;
712 	uint32_t mrk_posi_unkwn:1;
713 	uint32_t byte_cnt_unkwn:1;
714 	uint32_t blk_cnt_unkwn:	1;
715 	uint32_t end_of_part:	1;
716 	uint32_t begin_of_part:	1;
717 /* start next word */
718 	uint32_t blks_in_buf:	24;
719 	uint32_t reserved1:	8;
720 #endif
721 	uint64_t host_block;
722 	uint64_t media_block;
723 	uint64_t byte_in_buf;
724 }tape_position_ext_t;
725 
726 typedef union {
727 	tape_position_t srt;
728 	tape_position_ext_t ext;
729 	tape_position_long_t lng;
730 }read_pos_data_t;
731 
732 #ifdef _KERNEL
733 
734 #if defined(__i386) || defined(__amd64)
735 /* Data structure used in big block I/O on x86/x64 platform */
736 
737 /*
738  * alloc more than one contig_mem, so mutiple I/O can be
739  * on-going simultaneously
740  */
741 #define	ST_MAX_CONTIG_MEM_NUM	3
742 
743 /*
744  * 60K is used due to the limitation(size) of the intermediate buffer
745  * in DMA bind code(rootnex.c), which is 64K. If the I/O buf is page
746  * aligned, HBA can do 64K DMA, but if not, HBA can only do
747  * 64K - PAGESIZE = 60K DMA due to the copy to/from intermediate
748  * buffer will keep the page offset.
749  */
750 #define	ST_BIGBLK_XFER		60 * 1024
751 struct contig_mem {
752 	struct contig_mem *cm_next;
753 	size_t cm_len;
754 	caddr_t cm_addr;
755 	ddi_acc_handle_t cm_acc_hdl;
756 	struct buf *cm_bp;
757 	int cm_use_sbuf;
758 };
759 
760 #endif
761 
762 #endif /* _KERNEL */
763 
764 /*
765  * driver states..
766  */
767 typedef enum {
768 	ST_STATE_CLOSED,
769 	ST_STATE_OFFLINE,
770 	ST_STATE_INITIALIZING,
771 	ST_STATE_OPENING,
772 	ST_STATE_OPEN_PENDING_IO,
773 	ST_STATE_APPEND_TESTING,
774 	ST_STATE_OPEN,
775 	ST_STATE_RESOURCE_WAIT,
776 	ST_STATE_CLOSING,
777 	ST_STATE_SENSING,
778 	ST_STATE_CLOSE_PENDING_OPEN
779 }st_states;
780 
781 typedef enum { RDWR, RDONLY, WORM, RDWORM, FAILED } writablity;
782 
783 
784 /*
785  * Private info for scsi tapes. Pointed to by the un_private pointer
786  * of one of the SCSI_DEVICE chains.
787  */
788 
789 struct scsi_tape {
790 	struct scsi_device *un_sd;	/* back pointer to SCSI_DEVICE */
791 	struct scsi_pkt *un_rqs;	/* ptr to request sense command */
792 	struct scsi_pkt *un_mkr_pkt;	/* ptr to marker packet */
793 	kcondvar_t un_sbuf_cv;		/* cv on ownership of special buf */
794 	kcondvar_t un_queue_cv;		/* cv on all queued commands */
795 	struct	buf *un_sbufp;		/* for use in special io */
796 	char	*un_srqbufp;		/* sense buffer for special io */
797 	kcondvar_t un_clscv;		/* closing cv */
798 	struct	buf *un_quef;		/* head of wait queue */
799 	struct	buf *un_quel;		/* tail of wait queue */
800 	struct	buf *un_runqf;		/* head of run queue */
801 	struct	buf *un_runql;		/* tail of run queue */
802 	struct seq_mode *un_mspl;	/* ptr to mode select info */
803 	struct st_drivetype *un_dp;	/* ptr to drive table entry */
804 	uint_t	un_dp_size;		/* size of un_dp alloc'ed */
805 	caddr_t	un_tmpbuf;		/* buf for append, autodens ops */
806 	tapepos_t un_pos;		/* Current tape position */
807 	int	un_oflags;		/* open flags */
808 	tapepos_t un_err_pos;		/* block in file where err occurred */
809 	uint_t	un_err_resid;		/* resid from last error */
810 	short	un_fmneeded;		/* filemarks to be written - HP only */
811 	dev_t	un_dev;			/* unix device */
812 	uchar_t	un_attached;		/* unit known && attached */
813 	int	un_pwr_mgmt;		/* power management state */
814 	uchar_t	un_density_known;	/* density is known */
815 	uchar_t	un_curdens;		/* index into density table */
816 	optype	un_lastop;		/* last I/O was: read/write/ctl */
817 	st_states un_laststate;		/* last state */
818 	st_states un_state;		/* current state */
819 	uchar_t	un_status;		/* status from last sense */
820 	uchar_t	un_retry_ct;		/* retry count */
821 	uchar_t	un_tran_retry_ct;	/* transport retry count */
822 	writablity un_read_only;	/* RDWR, RDONLY, WORM, RDWORM */
823 	uchar_t	un_test_append;		/* check writing at end of tape */
824 	uchar_t un_arq_enabled;		/* auto request sense enabled */
825 	uchar_t un_untagged_qing;	/* hba has untagged quing */
826 	uchar_t	un_allow_large_xfer;	/* allow >64k xfers if requested */
827 	uchar_t	un_sbuf_busy;		/* sbuf busy flag */
828 	uchar_t	un_ncmds;		/* number of commands outstanding */
829 	uchar_t	un_throttle;		/* curr. max number of cmds outst. */
830 	uchar_t	un_last_throttle;	/* saved max number of cmds outst. */
831 	uchar_t	un_max_throttle;	/* max poss. number cmds outstanding */
832 	uchar_t	un_persistence;		/* 1 = persistence on, 0 off */
833 	uchar_t	un_persist_errors;	/* 1 = persistenced flagged */
834 	uchar_t	un_flush_on_errors;	/* HBA will flush all I/O's on a */
835 					/* check condidtion or error */
836 	uint_t	un_kbytes_xferred;	/* bytes (in K) counter */
837 	uint_t	un_last_resid;		/* keep last resid, for PE */
838 	uint_t	un_last_count;		/* keep last count, for PE */
839 	struct 	kstat *un_stats;	/* for I/O statistics */
840 	struct buf *un_rqs_bp;		/* bp used in rqpkt */
841 	struct	buf *un_wf;		/* head of write queue */
842 	struct	buf *un_wl;		/* tail of write queue */
843 	struct	read_blklim *un_rbl;	/* ptr to read block limit info */
844 	int	un_maxdma;		/* max dma xfer allowed by HBA */
845 	uint_t	un_bsize;		/* block size currently being used */
846 	int	un_maxbsize;		/* max block size allowed by drive */
847 	uint_t	un_minbsize;		/* min block size allowed by drive */
848 	int	un_errno;		/* errno (b_error) */
849 	kcondvar_t	un_state_cv;	/* mediastate condition variable */
850 	enum mtio_state	un_mediastate;	/* current media state */
851 	enum mtio_state	un_specified_mediastate;	/* expected state */
852 	timeout_id_t	un_delay_tid;	/* delayed cv tid */
853 	timeout_id_t	un_hib_tid;	/* handle interrupt busy tid */
854 	opaque_t	un_swr_token;	/* scsi_watch request token */
855 	uchar_t	un_comp_page;		/* compression page */
856 	uchar_t	un_rsvd_status;		/* Reservation Status */
857 	kstat_t *un_errstats;		/* for error statistics */
858 	int	un_init_options;  	/* Init time drive options */
859 	int	un_save_fileno;		/* Save here for recovery */
860 	daddr_t	un_save_blkno;		/* Save here for recovery */
861 	uchar_t un_restore_pos;		/* Indication to do recovery */
862 	tapepos_t un_suspend_pos;	/* Save blkno for SUSPEND */
863 	uchar_t	un_silent_skip;		/* to catch short reads */
864 	short	un_tids_at_suspend;	/* timeouts set at suspend */
865 	kcondvar_t un_tape_busy_cv;	/* busy cv */
866 	kcondvar_t un_suspend_cv;	/* busy cv */
867 					/* restore on close */
868 	uchar_t	un_eject_tape_on_failure; /* 1 = eject tape, 0 = don't */
869 	uchar_t	un_HeadClean; 		/* support and need head cleaning? */
870 	uchar_t	un_rqs_state;		/* see define below */
871 	struct scsi_extended_sense
872 	    *un_uscsi_rqs_buf;		/* uscsi_rqs: buffer for RQS data */
873 	uchar_t	un_data_mod;		/* Device required data mod */
874 	writablity (*un_wormable) (struct scsi_tape *un); /* worm test fuct */
875 	int un_max_cdb_sz;		/* max cdb size to use */
876 	read_p_types un_read_pos_type;
877 	read_pos_data_t *un_read_pos_data;
878 	struct mterror_entry_stack *un_error_entry_stk;
879 					/* latest sense cmd buffer */
880 
881 #if defined(__i386) || defined(__amd64)
882 	ddi_dma_handle_t un_contig_mem_hdl;
883 	struct contig_mem *un_contig_mem;
884 	int un_contig_mem_available_num;
885 	int un_contig_mem_total_num;
886 	size_t un_max_contig_mem_len;
887 	kcondvar_t un_contig_mem_cv;
888 #endif
889 };
890 
891 
892 /*
893  * device error kstats
894  */
895 struct st_errstats {
896 	struct kstat_named	st_softerrs;
897 	struct kstat_named	st_harderrs;
898 	struct kstat_named	st_transerrs;
899 	struct kstat_named	st_vid;
900 	struct kstat_named	st_pid;
901 	struct kstat_named	st_revision;
902 	struct kstat_named	st_serial;
903 };
904 
905 /*
906  * generic log page struct
907  */
908 struct log_page {
909 #if defined(_BIT_FIELDS_LTOH)
910 	uchar_t	code	:6,	/* page code number */
911 			:2;	/* reserved */
912 #elif defined(_BIT_FIELDS_HTOL)
913 	uchar_t		:2,	/* reserved */
914 		code	:6;	/* page code number */
915 #endif	/* _BIT_FIELDS_LTOH */
916 	uchar_t	reserved;	/* reserved */
917 	uchar_t	length_hi;	/* length of bytes to follow (msb) */
918 	uchar_t	length_lo;	/* length of bytes to follow (lsb) */
919 	/*
920 	 * Log parameters follow right after this...
921 	 */
922 };
923 
924 /*
925  * generic log page parameter struct
926  */
927 struct log_param {
928 	uchar_t	pc_hi;			/* parameter code (msb) */
929 	uchar_t	pc_lo;			/* parameter code (lsb) */
930 #if defined(_BIT_FIELDS_LTOH)
931 	uchar_t		lp	: 1,	/* list parameter */
932 				: 1,	/* reserved */
933 			tmc	: 2,	/* threshold met criteria */
934 			etc	: 1,	/* enable threshold comparison */
935 			tsd	: 1,	/* target save disable */
936 			ds	: 1,	/* disable save */
937 			du	: 1;	/* disable update */
938 #elif defined(_BIT_FIELDS_HTOL)
939 	uchar_t		du	: 1,	/* disable update */
940 			ds	: 1,	/* disable save */
941 			tsd	: 1,	/* target save disable */
942 			etc	: 1,	/* enable threshold comparison */
943 			tmc	: 2,	/* threshold met criteria */
944 				: 1,	/* reserved */
945 			lp	: 1;	/* list parameter */
946 #endif	/* _BIT_FIELDS_LTOH */
947 	uchar_t	length;		/* length of bytes to follow */
948 	/*
949 	 * Parameter values follow right after this...
950 	 */
951 };
952 /*
953  * TapeAlert structures
954  */
955 
956 struct st_tape_alert_parameter {
957 	struct log_param log_param;
958 	uchar_t	param_value;
959 };
960 
961 struct st_tape_alert {
962 	struct log_page log_page;
963 	struct st_tape_alert_parameter param[TAPE_ALERT_MAX_PARA];
964 };
965 
966 #define	TAPE_ALERT_PARAMETER_LENGTH \
967 	(sizeof (struct st_tape_alert_parameter)) * TAPE_ALERT_MAX_PARA
968 
969 struct log_sequential_page_parameter {
970 	struct log_param log_param;
971 	uchar_t param_value[8];
972 };
973 
974 struct log_sequential_page {
975 	struct log_page log_page;
976 	struct log_sequential_page_parameter param[TAPE_SEQUENTIAL_PAGE_PARA];
977 };
978 
979 #if !defined(__lint)
980 _NOTE(MUTEX_PROTECTS_DATA(scsi_device::sd_mutex, scsi_tape))
981 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_tape::un_dp))
982 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_tape::un_sd))
983 _NOTE(SCHEME_PROTECTS_DATA("not shared", scsi_tape::un_rqs))
984 _NOTE(SCHEME_PROTECTS_DATA("protected by cv", scsi_tape::un_sbufp))
985 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsi_tape::un_bsize))
986 _NOTE(SCHEME_PROTECTS_DATA("not shared", scsi_arq_status))
987 _NOTE(SCHEME_PROTECTS_DATA("save sharing",
988 	scsi_tape::un_allow_large_xfer
989 	scsi_tape::un_maxbsize
990 	scsi_tape::un_maxdma
991 ))
992 #if defined(__i386) || defined(__amd64)
993 _NOTE(DATA_READABLE_WITHOUT_LOCK(scsi_tape::un_contig_mem_hdl))
994 _NOTE(SCHEME_PROTECTS_DATA("not shared", contig_mem))
995 #endif
996 #endif
997 
998 
999 /*
1000  * Power management state
1001  */
1002 #define	ST_PWR_NORMAL				0
1003 #define	ST_PWR_SUSPENDED			1
1004 
1005 
1006 #define	IN_EOF(pos)	(pos.eof == ST_EOF_PENDING || pos.eof == ST_EOF)
1007 
1008 /* un_rqs_state codes */
1009 
1010 #define	ST_RQS_OVR		0x1	/* RQS data was overwritten */
1011 #define	ST_RQS_VALID		0x2	/* RQS data is valid */
1012 #define	ST_RQS_READ		0x4	/* RQS data was read */
1013 #define	ST_RQS_ERROR		0x8	/* RQS resulted in an EIO */
1014 
1015 /*
1016  * stintr codes
1017  */
1018 
1019 #define	COMMAND_DONE					0
1020 #define	COMMAND_DONE_ERROR				1
1021 #define	COMMAND_DONE_ERROR_RECOVERED			2
1022 #define	QUE_COMMAND					3
1023 #define	QUE_BUSY_COMMAND				4
1024 #define	QUE_SENSE					5
1025 #define	JUST_RETURN					6
1026 #define	COMMAND_DONE_EACCES				7
1027 #define	QUE_LAST_COMMAND				8
1028 
1029 
1030 /*
1031  *	Reservation Status
1032  *
1033  * ST_INIT_RESERVE      -Used to check if the reservation has been lost
1034  *		         in between opens and also to indicate the reservation
1035  *		         has not been done till now.
1036  * ST_RELEASE	        -Tape Unit is Released.
1037  * ST_RESERVE	        -Tape Unit is Reserved.
1038  * ST_PRESERVE_RESERVE  -Reservation is to be preserved across opens.
1039  *
1040  */
1041 #define	ST_INIT_RESERVE			0x001
1042 #define	ST_RELEASE			0x002
1043 #define	ST_RESERVE			0x004
1044 #define	ST_PRESERVE_RESERVE		0x008
1045 #define	ST_RESERVATION_CONFLICT 	0x010
1046 #define	ST_LOST_RESERVE			0x020
1047 #define	ST_APPLICATION_RESERVATIONS	0x040
1048 #define	ST_LOST_RESERVE_BETWEEN_OPENS  \
1049 		(ST_RESERVE | ST_LOST_RESERVE | ST_PRESERVE_RESERVE)
1050 
1051 /*
1052  * Service action defines for Persistant Reservation Commands
1053  */
1054 #define	ST_SA_SCSI3_REGISTER			0x00
1055 #define	ST_SA_SCSI3_RESERVE			0x01
1056 #define	ST_SA_SCSI3_RELEASE			0x02
1057 #define	ST_SA_SCSI3_CLEAR			0x03
1058 #define	ST_SA_SCSI3_PREEMPT			0x04
1059 #define	ST_SA_SCSI3_PREEMPTANDABORT		0x05
1060 #define	ST_SA_SCSI3_REGISTERANDIGNOREKEY	0x06
1061 #define	ST_SA_MASK				0x1f
1062 
1063 #define	ST_RESERVATION_DELAY		500000
1064 
1065 /*
1066  * Asynch I/O tunables
1067  */
1068 #define	ST_MAX_THROTTLE		4
1069 
1070 /*
1071  * 60 minutes seems a reasonable amount of time
1072  * to wait for tape space operations to complete.
1073  *
1074  */
1075 #define	ST_SPACE_TIME	MINUTES(60)	/* 60 minutes per space operation */
1076 #define	ST_LONG_SPACE_TIME_X	5	/* multipiler for long space ops */
1077 
1078 /*
1079  * 2 minutes seems a reasonable amount of time
1080  * to wait for tape i/o operations to complete.
1081  *
1082  */
1083 #define	ST_IO_TIME	MINUTES(2)	/* minutes per i/o */
1084 #define	ST_LONG_TIMEOUT_X	5	/* multiplier for very long timeouts */
1085 
1086 
1087 /*
1088  * 10 seconds is what we'll wait if we get a Busy Status back
1089  */
1090 #define	ST_STATUS_BUSY_TIMEOUT	10*hz	/* seconds Busy Waiting */
1091 #define	ST_TRAN_BUSY_TIMEOUT	1*hz	/* seconds retry on TRAN_BSY */
1092 #define	ST_INTERRUPT_CONTEXT	1
1093 #define	ST_START_CONTEXT	2
1094 
1095 /*
1096  * Number of times we'll retry a normal operation.
1097  *
1098  * XXX This includes retries due to transport failure as well as
1099  * XXX busy timeouts- Need to distinguish between Target and Transport
1100  * XXX failure.
1101  */
1102 
1103 #define	ST_RETRY_COUNT		20
1104 
1105 /*
1106  * Number of times to retry a failed selection
1107  */
1108 #define	ST_SEL_RETRY_COUNT		2
1109 
1110 /*
1111  * es_code value for deferred error
1112  * should be moved to sense.h
1113  */
1114 
1115 #define	ST_DEFERRED_ERROR		0x01
1116 
1117 /*
1118  * Maximum number of units (determined by minor device byte)
1119  */
1120 #define	ST_MAXUNIT	128
1121 
1122 /*
1123  * Time to wait for completion of a command before cancelling it.
1124  * For SUSPEND use only
1125  */
1126 #define	ST_WAIT_CMDS_COMPLETE		10	/* seconds */
1127 
1128 #ifndef	SECSIZE
1129 #define	SECSIZE	512
1130 #endif
1131 #ifndef	SECDIV
1132 #define	SECDIV	9
1133 #endif
1134 
1135 /*
1136  * convenient defines
1137  */
1138 #define	ST_SCSI_DEVP	(un->un_sd)
1139 #define	ST_DEVINFO		(ST_SCSI_DEVP->sd_dev)
1140 #define	ST_INQUIRY		(ST_SCSI_DEVP->sd_inq)
1141 #define	ST_RQSENSE		(ST_SCSI_DEVP->sd_sense)
1142 #define	ST_MUTEX		(&ST_SCSI_DEVP->sd_mutex)
1143 #define	ROUTE			(&ST_SCSI_DEVP->sd_address)
1144 
1145 #define	BSD_BEHAVIOR	(getminor(un->un_dev) & MT_BSD)
1146 #define	SVR4_BEHAVIOR	((getminor(un->un_dev) & MT_BSD) == 0)
1147 #define	SCBP(pkt)		((struct scsi_status *)(pkt)->pkt_scbp)
1148 #define	SCBP_C(pkt)		((*(pkt)->pkt_scbp) & STATUS_MASK)
1149 #define	CDBP(pkt)		((union scsi_cdb *)(pkt)->pkt_cdbp)
1150 #define	BP_PKT(bp)		((struct scsi_pkt *)(bp)->av_back)
1151 #define	SET_BP_PKT(bp, pkt)	((bp)->av_back = (struct buf *)(pkt))
1152 #define	BP_UCMD(bp)		((struct uscsi_cmd *)(bp)->b_back)
1153 #define	USCSI_CMD(bp)	(((bp) == un->un_sbufp) && (BP_UCMD(bp)))
1154 
1155 #define	IS_CLOSING(un)	((un)->un_state == ST_STATE_CLOSING || \
1156 	((un)->un_state == ST_STATE_SENSING && \
1157 		(un)->un_laststate == ST_STATE_CLOSING))
1158 
1159 #define	ASYNC_CMD	0
1160 #define	SYNC_CMD	1
1161 
1162 /*
1163  * Flush tape wait queue as needed.
1164  */
1165 
1166 #define	IS_PE_FLAG_SET(un) ((un)->un_persistence && (un)->un_persist_errors)
1167 
1168 #define	TURN_PE_ON(un)		st_turn_pe_on(un)
1169 #define	TURN_PE_OFF(un)		st_turn_pe_off(un)
1170 #define	SET_PE_FLAG(un)		st_set_pe_flag(un)
1171 #define	CLEAR_PE(un)		st_clear_pe(un)
1172 
1173 #define	st_bioerror(bp, error) \
1174 		{ bioerror(bp, error); \
1175 		un->un_errno = error; }
1176 
1177 /*
1178  * Macros for internal coding of count for SPACE command:
1179  *
1180  * Isfmk is 1 when spacing filemarks; 0 when spacing records:
1181  * bit 24 set indicates a space filemark command.
1182  * Fmk sets the filemark bit (24) and changes a backspace
1183  * count into a positive number with the sign bit set.
1184  * Blk changes a backspace count into a positive number with
1185  * the sign bit set.
1186  * space_cnt converts backwards counts to negative numbers.
1187  */
1188 #define	SP_BLK		0
1189 #define	SP_FLM		((1<<24))
1190 #define	SP_SQFLM	((2<<24))
1191 #define	SP_EOD		((3<<24))
1192 #define	SP_BACKSP	((1<<30))
1193 #define	SP_CMD_MASK	((7<<24))
1194 #define	SP_CNT_MASK	((1<<24)-1)
1195 
1196 /* Macros to assemble space cmds */
1197 #define	SPACE(cmd, cnt)	((cnt < 0) ? (SP_BACKSP | (-(cnt)) | cmd) : (cmd | cnt))
1198 #define	Fmk(x)		SPACE(SP_FLM, x)
1199 #define	Blk(x)		SPACE(SP_BLK, x)
1200 
1201 /* Macros to interpret space cmds */
1202 #define	SPACE_CNT(x)	(((x) & SP_BACKSP)? \
1203 	(-((x)&(SP_CNT_MASK))):(x)&(SP_CNT_MASK))
1204 #define	SPACE_TYPE(x)	((x & SP_CMD_MASK)>>24)
1205 
1206 
1207 /* Defines for byte 4 of load/unload cmd */
1208 #define	LD_UNLOAD	0
1209 #define	LD_LOAD		1
1210 #define	LD_RETEN	2
1211 #define	LD_EOT		4
1212 #define	LD_HOLD		8
1213 
1214 /* Defines for byte 4 of prevent/allow media removal */
1215 #define	MR_UNLOCK	0
1216 #define	MR_LOCK		1
1217 
1218 #define	GET_SOFT_STATE(dev)						\
1219 	register struct scsi_tape *un;					\
1220 	register int instance;						\
1221 									\
1222 	instance = MTUNIT(dev);						\
1223 	if ((un = ddi_get_soft_state(st_state, instance)) == NULL)	\
1224 		return (ENXIO);
1225 
1226 /*
1227  * Debugging turned on via conditional compilation switch -DSTDEBUG
1228  */
1229 #ifdef DEBUG
1230 #define	STDEBUG
1231 #endif
1232 
1233 #ifdef	STDEBUG
1234 #define	DEBUGGING	((scsi_options & SCSI_DEBUG_TGT) || (st_debug & 0xf))
1235 
1236 #define	ST_DARGS	st_label, SCSI_DEBUG
1237 
1238 
1239 	/* initialization */
1240 #define	ST_DEBUG1	if ((st_debug & 0xf) >= 1) scsi_log
1241 #define	ST_DEBUG	ST_DEBUG1
1242 
1243 	/* errors and UA's */
1244 #define	ST_DEBUG2	if ((st_debug & 0xf) >= 2) scsi_log
1245 
1246 	/* func calls */
1247 #define	ST_DEBUG3	if ((st_debug & 0xf) >= 3) scsi_log
1248 
1249 	/* ioctl calls */
1250 #define	ST_DEBUG4	if ((st_debug & 0xf) >= 4) scsi_log
1251 
1252 #define	ST_DEBUG5	if ((st_debug & 0xf) >= 5) scsi_log
1253 
1254 	/* full data tracking */
1255 #define	ST_DEBUG6	if ((st_debug & 0xf) >= 6) scsi_log
1256 
1257 	/* special cases */
1258 #define	ST_DEBUG_SP	if ((st_debug & 0xf) == 10) scsi_log
1259 
1260 	/* Entry Point Functions */
1261 #define	ST_ENTR(d, fn)	if (st_debug & 0x10) scsi_log(d, ST_DARGS, #fn)
1262 
1263 	/* Non-Entry Point Functions */
1264 #define	ST_FUNC(d, fn)	if (st_debug & 0x20) scsi_log(d, ST_DARGS, #fn)
1265 
1266 	/* Space Information */
1267 #define	ST_SPAC		if (st_debug & 0x40) scsi_log
1268 
1269 	/* CDB's sent */
1270 #define	ST_CDB(d, cmnt, cdb) if (st_debug & 0x180) \
1271     st_print_cdb(d, ST_DARGS, cmnt, cdb)
1272 #define	ST_SENSE(d, cmnt, sense, size) if (st_debug & 0x200) \
1273     st_clean_print(d, ST_DARGS, cmnt, sense, size)
1274 
1275 #else
1276 
1277 #define	st_debug	(0)
1278 #define	DEBUGGING	(0)
1279 #define	ST_DEBUG	if (0) scsi_log
1280 #define	ST_DEBUG1	if (0) scsi_log
1281 #define	ST_DEBUG2	if (0) scsi_log
1282 #define	ST_DEBUG3	if (0) scsi_log
1283 #define	ST_DEBUG4	if (0) scsi_log
1284 #define	ST_DEBUG5	if (0) scsi_log
1285 #define	ST_DEBUG6	if (0) scsi_log
1286 
1287 #define	ST_DEBUG_SP	if (0) scsi_log /* special cases */
1288 
1289 #define	ST_ENTR(d, fn)
1290 #define	ST_FUNC(d, fn)
1291 #define	ST_SPAC		if (0) scsi_log
1292 #define	ST_CDB(d, cmnt, cdb)
1293 #define	ST_SENSE(d, cmnt, sense, size)
1294 
1295 #endif
1296 
1297 /*
1298  * Media access values
1299  */
1300 #define	MEDIA_ACCESS_DELAY 5000000	/* usecs wait for media state change */
1301 
1302 /*
1303  * SCSI tape mode sense page information
1304  */
1305 #define	ST_DEV_CONFIG_PAGE	0x10	/* device config mode page */
1306 #define	ST_DEV_CONFIG_NO_COMP	0x00	/* use no compression */
1307 #define	ST_DEV_CONFIG_DEF_COMP	0x01	/* use default compression alg */
1308 #define	ST_COMPRESSION_DENSITY	3	/* compression minor number */
1309 
1310 /*
1311  * SCSI tape data compression Page definition.
1312  */
1313 #define	ST_DEV_DATACOMP_PAGE	0x0F	/* data compression page */
1314 
1315 
1316 
1317 /*
1318  * maxbsize values
1319  */
1320 #define	MAXBSIZE_UNKNOWN	-2	/*  not found yet */
1321 
1322 #define	ONE_MEG			(1024 * 1024)
1323 
1324 /*
1325  * generic soft error reporting
1326  *
1327  * What we are doing here is allowing a greater number of errors to occur on
1328  * smaller transfers (i.e. usually at the beginning of the tape), than on
1329  * the rest of the tape.
1330  *
1331  * A small transfer is defined as :
1332  * Transfers <= SOFT_ERROR_WARNING_THRESHOLD  allow about 1.5 times more errors
1333  *
1334  * A larget tranfer is defined as :
1335  * Transfers >  SOFT_ERROR_WARNING_THRESHOLD  allow normal amount
1336  *
1337  */
1338 #define	READ_SOFT_ERROR_WARNING_THRESHOLD    (25 * ONE_MEG)
1339 #define	WRITE_SOFT_ERROR_WARNING_THRESHOLD    (20 * ONE_MEG)
1340 
1341 /*
1342  * soft error reporting for exabyte
1343  */
1344 #define	TAPE_SENSE_LENGTH	32	/* allows for softerror info */
1345 
1346 #define	SENSE_19_BITS  \
1347 	"\20\10PF\07BPE\06FPE\05ME\04ECO\03TME\02TNP\01LBOT"
1348 #define	SENSE_20_BITS  \
1349 	"\20\10RSVD\07RSVD\06WP\05FMKE\04URE\03WE1\02SSE\01FW"
1350 #define	SENSE_21_BITS  \
1351 	"\20\10RSVD\07RSVD\06RRR\05CLND\04CLN\03PEOT\02WSEB\01WSE0"
1352 
1353 /* these are defined in percentages */
1354 #define	EXABYTE_WRITE_ERROR_THRESHOLD	6
1355 #define	EXABYTE_READ_ERROR_THRESHOLD	3
1356 /*
1357  * minumum amount of data transfer(MB) for checking soft error rate.
1358  */
1359 #define	EXABYTE_MIN_TRANSFER			(25 * ONE_MEG)
1360 
1361 #define	CLN	0x8
1362 #define	CLND	0x10
1363 
1364 /*
1365  * soft error reporting for Archive 4mm DAT
1366  */
1367 
1368 #define	LOG_SENSE_LENGTH		0xff
1369 #define	MIN_LOG_SENSE_LENGTH		0x2b
1370 #define	DAT_SMALL_WRITE_ERROR_THRESHOLD	40	/* retries per 20 mg */
1371 #define	DAT_LARGE_WRITE_ERROR_THRESHOLD	200	/* retries for more 20 mg */
1372 #define	DAT_SMALL_READ_ERROR_THRESHOLD	5	/* errors allowed */
1373 #define	DAT_LARGE_READ_ERROR_THRESHOLD	3	/* errors allowed */
1374 
1375 /*
1376  * ST timeouts that need to be cancelled for suspend
1377  */
1378 #define	ST_HIB_TID	0x01
1379 #define	ST_DELAY_TID	0x02
1380 
1381 #ifdef	__cplusplus
1382 }
1383 #endif
1384 
1385 #endif	/* _SYS_SCSI_TARGETS_STDEF_H */
1386