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