1/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef _ST_H
4#define _ST_H
5
6#include <linux/completion.h>
7#include <linux/mutex.h>
8#include <linux/kref.h>
9#include <scsi/scsi_cmnd.h>
10
11/* Descriptor for analyzed sense data */
12struct st_cmdstatus {
13	int midlevel_result;
14	struct scsi_sense_hdr sense_hdr;
15	int have_sense;
16	int residual;
17	u64 uremainder64;
18	u8 flags;
19	u8 remainder_valid;
20	u8 fixed_format;
21	u8 deferred;
22};
23
24struct scsi_tape;
25
26/* scsi tape command */
27struct st_request {
28	unsigned char cmd[MAX_COMMAND_SIZE];
29	unsigned char sense[SCSI_SENSE_BUFFERSIZE];
30	int result;
31	struct scsi_tape *stp;
32	struct completion *waiting;
33	struct bio *bio;
34};
35
36/* The tape buffer descriptor. */
37struct st_buffer {
38	unsigned char cleared;  /* internal buffer cleared after open? */
39	unsigned short do_dio;  /* direct i/o set up? */
40	int buffer_size;
41	int buffer_blocks;
42	int buffer_bytes;
43	int read_pointer;
44	int writing;
45	int syscall_result;
46	struct st_request *last_SRpnt;
47	struct st_cmdstatus cmdstat;
48	struct page **reserved_pages;
49	int reserved_page_order;
50	struct page **mapped_pages;
51	struct rq_map_data map_data;
52	unsigned char *b_data;
53	unsigned short use_sg;	/* zero or max number of s/g segments for this adapter */
54	unsigned short sg_segs;		/* number of segments in s/g list */
55	unsigned short frp_segs;	/* number of buffer segments */
56};
57
58/* The tape mode definition */
59struct st_modedef {
60	unsigned char defined;
61	unsigned char sysv;	/* SYS V semantics? */
62	unsigned char do_async_writes;
63	unsigned char do_buffer_writes;
64	unsigned char do_read_ahead;
65	unsigned char defaults_for_writes;
66	unsigned char default_compression;	/* 0 = don't touch, etc */
67	short default_density;	/* Forced density, -1 = no value */
68	int default_blksize;	/* Forced blocksize, -1 = no value */
69	struct scsi_tape *tape;
70	struct device *devs[2];  /* Auto-rewind and non-rewind devices */
71	struct cdev *cdevs[2];  /* Auto-rewind and non-rewind devices */
72};
73
74/* Number of modes can be changed by changing ST_NBR_MODE_BITS. The maximum
75   number of modes is 16 (ST_NBR_MODE_BITS 4) */
76#define ST_NBR_MODE_BITS 2
77#define ST_NBR_MODES (1 << ST_NBR_MODE_BITS)
78#define ST_MODE_SHIFT (7 - ST_NBR_MODE_BITS)
79#define ST_MODE_MASK ((ST_NBR_MODES - 1) << ST_MODE_SHIFT)
80
81#define ST_MAX_TAPES (1 << (20 - (ST_NBR_MODE_BITS + 1)))
82#define ST_MAX_TAPE_ENTRIES  (ST_MAX_TAPES << (ST_NBR_MODE_BITS + 1))
83
84/* The status related to each partition */
85struct st_partstat {
86	unsigned char rw;
87	unsigned char eof;
88	unsigned char at_sm;
89	unsigned char last_block_valid;
90	u32 last_block_visited;
91	int drv_block;		/* The block where the drive head is */
92	int drv_file;
93};
94
95/* Tape statistics */
96struct scsi_tape_stats {
97	atomic64_t read_byte_cnt;  /* bytes read */
98	atomic64_t write_byte_cnt; /* bytes written */
99	atomic64_t in_flight;      /* Number of I/Os in flight */
100	atomic64_t read_cnt;       /* Count of read requests */
101	atomic64_t write_cnt;      /* Count of write requests */
102	atomic64_t other_cnt;      /* Count of other requests either
103				    * implicit or from user space
104				    * ioctl. */
105	atomic64_t resid_cnt;      /* Count of resid_len > 0 */
106	atomic64_t tot_read_time;  /* ktime spent completing reads */
107	atomic64_t tot_write_time; /* ktime spent completing writes */
108	atomic64_t tot_io_time;    /* ktime spent doing any I/O */
109	ktime_t read_time;         /* holds ktime request was queued */
110	ktime_t write_time;        /* holds ktime request was queued */
111	ktime_t other_time;        /* holds ktime request was queued */
112	atomic_t last_read_size;   /* Number of bytes issued for last read */
113	atomic_t last_write_size;  /* Number of bytes issued for last write */
114};
115
116#define ST_NBR_PARTITIONS 4
117
118/* The tape drive descriptor */
119struct scsi_tape {
120	struct scsi_device *device;
121	struct mutex lock;	/* For serialization */
122	struct completion wait;	/* For SCSI commands */
123	struct st_buffer *buffer;
124	int index;
125
126	/* Drive characteristics */
127	unsigned char omit_blklims;
128	unsigned char do_auto_lock;
129	unsigned char can_bsr;
130	unsigned char can_partitions;
131	unsigned char two_fm;
132	unsigned char fast_mteom;
133	unsigned char immediate;
134	unsigned char scsi2_logical;
135	unsigned char default_drvbuffer;	/* 0xff = don't touch, value 3 bits */
136	unsigned char cln_mode;			/* 0 = none, otherwise sense byte nbr */
137	unsigned char cln_sense_value;
138	unsigned char cln_sense_mask;
139	unsigned char use_pf;			/* Set Page Format bit in all mode selects? */
140	unsigned char try_dio;			/* try direct i/o in general? */
141	unsigned char try_dio_now;		/* try direct i/o before next close? */
142	unsigned char c_algo;			/* compression algorithm */
143	unsigned char pos_unknown;			/* after reset position unknown */
144	unsigned char sili;			/* use SILI when reading in variable b mode */
145	unsigned char immediate_filemark;	/* write filemark immediately */
146	int tape_type;
147	int long_timeout;	/* timeout for commands known to take long time */
148
149	/* Mode characteristics */
150	struct st_modedef modes[ST_NBR_MODES];
151	int current_mode;
152
153	/* Status variables */
154	int partition;
155	int new_partition;
156	int nbr_partitions;	/* zero until partition support enabled */
157	struct st_partstat ps[ST_NBR_PARTITIONS];
158	unsigned char dirty;
159	unsigned char ready;
160	unsigned char write_prot;
161	unsigned char drv_write_prot;
162	unsigned char in_use;
163	unsigned char blksize_changed;
164	unsigned char density_changed;
165	unsigned char compression_changed;
166	unsigned char drv_buffer;
167	unsigned char density;
168	unsigned char door_locked;
169	unsigned char autorew_dev;   /* auto-rewind device */
170	unsigned char rew_at_close;  /* rewind necessary at close */
171	unsigned char inited;
172	unsigned char cleaning_req;  /* cleaning requested? */
173	int block_size;
174	int min_block;
175	int max_block;
176	int recover_count;     /* From tape opening */
177	int recover_reg;       /* From last status call */
178
179#if DEBUG
180	unsigned char write_pending;
181	int nbr_finished;
182	int nbr_waits;
183	int nbr_requests;
184	int nbr_dio;
185	int nbr_pages;
186	unsigned char last_cmnd[6];
187	unsigned char last_sense[16];
188#endif
189	char name[DISK_NAME_LEN];
190	struct kref     kref;
191	struct scsi_tape_stats *stats;
192};
193
194/* Bit masks for use_pf */
195#define USE_PF      1
196#define PF_TESTED   2
197
198/* Values of eof */
199#define	ST_NOEOF	0
200#define ST_FM_HIT       1
201#define ST_FM           2
202#define ST_EOM_OK       3
203#define ST_EOM_ERROR	4
204#define	ST_EOD_1        5
205#define ST_EOD_2        6
206#define ST_EOD		7
207/* EOD hit while reading => ST_EOD_1 => return zero => ST_EOD_2 =>
208   return zero => ST_EOD, return ENOSPC */
209/* When writing: ST_EOM_OK == early warning found, write OK
210		 ST_EOD_1  == allow trying new write after early warning
211		 ST_EOM_ERROR == early warning found, not able to write all */
212
213/* Values of rw */
214#define	ST_IDLE		0
215#define	ST_READING	1
216#define	ST_WRITING	2
217
218/* Values of ready state */
219#define ST_READY	0
220#define ST_NOT_READY	1
221#define ST_NO_TAPE	2
222
223/* Values for door lock state */
224#define ST_UNLOCKED	0
225#define ST_LOCKED_EXPLICIT 1
226#define ST_LOCKED_AUTO  2
227#define ST_LOCK_FAILS   3
228
229/* Positioning SCSI-commands for Tandberg, etc. drives */
230#define	QFA_REQUEST_BLOCK	0x02
231#define	QFA_SEEK_BLOCK		0x0c
232
233/* Setting the binary options */
234#define ST_DONT_TOUCH  0
235#define ST_NO          1
236#define ST_YES         2
237
238#define EXTENDED_SENSE_START  18
239
240/* Masks for some conditions in the sense data */
241#define SENSE_FMK   0x80
242#define SENSE_EOM   0x40
243#define SENSE_ILI   0x20
244
245#endif
246