18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci#ifndef _ST_H
48c2ecf20Sopenharmony_ci#define _ST_H
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/completion.h>
78c2ecf20Sopenharmony_ci#include <linux/mutex.h>
88c2ecf20Sopenharmony_ci#include <linux/kref.h>
98c2ecf20Sopenharmony_ci#include <scsi/scsi_cmnd.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/* Descriptor for analyzed sense data */
128c2ecf20Sopenharmony_cistruct st_cmdstatus {
138c2ecf20Sopenharmony_ci	int midlevel_result;
148c2ecf20Sopenharmony_ci	struct scsi_sense_hdr sense_hdr;
158c2ecf20Sopenharmony_ci	int have_sense;
168c2ecf20Sopenharmony_ci	int residual;
178c2ecf20Sopenharmony_ci	u64 uremainder64;
188c2ecf20Sopenharmony_ci	u8 flags;
198c2ecf20Sopenharmony_ci	u8 remainder_valid;
208c2ecf20Sopenharmony_ci	u8 fixed_format;
218c2ecf20Sopenharmony_ci	u8 deferred;
228c2ecf20Sopenharmony_ci};
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistruct scsi_tape;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* scsi tape command */
278c2ecf20Sopenharmony_cistruct st_request {
288c2ecf20Sopenharmony_ci	unsigned char cmd[MAX_COMMAND_SIZE];
298c2ecf20Sopenharmony_ci	unsigned char sense[SCSI_SENSE_BUFFERSIZE];
308c2ecf20Sopenharmony_ci	int result;
318c2ecf20Sopenharmony_ci	struct scsi_tape *stp;
328c2ecf20Sopenharmony_ci	struct completion *waiting;
338c2ecf20Sopenharmony_ci	struct bio *bio;
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* The tape buffer descriptor. */
378c2ecf20Sopenharmony_cistruct st_buffer {
388c2ecf20Sopenharmony_ci	unsigned char dma;	/* DMA-able buffer */
398c2ecf20Sopenharmony_ci	unsigned char cleared;  /* internal buffer cleared after open? */
408c2ecf20Sopenharmony_ci	unsigned short do_dio;  /* direct i/o set up? */
418c2ecf20Sopenharmony_ci	int buffer_size;
428c2ecf20Sopenharmony_ci	int buffer_blocks;
438c2ecf20Sopenharmony_ci	int buffer_bytes;
448c2ecf20Sopenharmony_ci	int read_pointer;
458c2ecf20Sopenharmony_ci	int writing;
468c2ecf20Sopenharmony_ci	int syscall_result;
478c2ecf20Sopenharmony_ci	struct st_request *last_SRpnt;
488c2ecf20Sopenharmony_ci	struct st_cmdstatus cmdstat;
498c2ecf20Sopenharmony_ci	struct page **reserved_pages;
508c2ecf20Sopenharmony_ci	int reserved_page_order;
518c2ecf20Sopenharmony_ci	struct page **mapped_pages;
528c2ecf20Sopenharmony_ci	struct rq_map_data map_data;
538c2ecf20Sopenharmony_ci	unsigned char *b_data;
548c2ecf20Sopenharmony_ci	unsigned short use_sg;	/* zero or max number of s/g segments for this adapter */
558c2ecf20Sopenharmony_ci	unsigned short sg_segs;		/* number of segments in s/g list */
568c2ecf20Sopenharmony_ci	unsigned short frp_segs;	/* number of buffer segments */
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/* The tape mode definition */
608c2ecf20Sopenharmony_cistruct st_modedef {
618c2ecf20Sopenharmony_ci	unsigned char defined;
628c2ecf20Sopenharmony_ci	unsigned char sysv;	/* SYS V semantics? */
638c2ecf20Sopenharmony_ci	unsigned char do_async_writes;
648c2ecf20Sopenharmony_ci	unsigned char do_buffer_writes;
658c2ecf20Sopenharmony_ci	unsigned char do_read_ahead;
668c2ecf20Sopenharmony_ci	unsigned char defaults_for_writes;
678c2ecf20Sopenharmony_ci	unsigned char default_compression;	/* 0 = don't touch, etc */
688c2ecf20Sopenharmony_ci	short default_density;	/* Forced density, -1 = no value */
698c2ecf20Sopenharmony_ci	int default_blksize;	/* Forced blocksize, -1 = no value */
708c2ecf20Sopenharmony_ci	struct scsi_tape *tape;
718c2ecf20Sopenharmony_ci	struct device *devs[2];  /* Auto-rewind and non-rewind devices */
728c2ecf20Sopenharmony_ci	struct cdev *cdevs[2];  /* Auto-rewind and non-rewind devices */
738c2ecf20Sopenharmony_ci};
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/* Number of modes can be changed by changing ST_NBR_MODE_BITS. The maximum
768c2ecf20Sopenharmony_ci   number of modes is 16 (ST_NBR_MODE_BITS 4) */
778c2ecf20Sopenharmony_ci#define ST_NBR_MODE_BITS 2
788c2ecf20Sopenharmony_ci#define ST_NBR_MODES (1 << ST_NBR_MODE_BITS)
798c2ecf20Sopenharmony_ci#define ST_MODE_SHIFT (7 - ST_NBR_MODE_BITS)
808c2ecf20Sopenharmony_ci#define ST_MODE_MASK ((ST_NBR_MODES - 1) << ST_MODE_SHIFT)
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define ST_MAX_TAPES (1 << (20 - (ST_NBR_MODE_BITS + 1)))
838c2ecf20Sopenharmony_ci#define ST_MAX_TAPE_ENTRIES  (ST_MAX_TAPES << (ST_NBR_MODE_BITS + 1))
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/* The status related to each partition */
868c2ecf20Sopenharmony_cistruct st_partstat {
878c2ecf20Sopenharmony_ci	unsigned char rw;
888c2ecf20Sopenharmony_ci	unsigned char eof;
898c2ecf20Sopenharmony_ci	unsigned char at_sm;
908c2ecf20Sopenharmony_ci	unsigned char last_block_valid;
918c2ecf20Sopenharmony_ci	u32 last_block_visited;
928c2ecf20Sopenharmony_ci	int drv_block;		/* The block where the drive head is */
938c2ecf20Sopenharmony_ci	int drv_file;
948c2ecf20Sopenharmony_ci};
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/* Tape statistics */
978c2ecf20Sopenharmony_cistruct scsi_tape_stats {
988c2ecf20Sopenharmony_ci	atomic64_t read_byte_cnt;  /* bytes read */
998c2ecf20Sopenharmony_ci	atomic64_t write_byte_cnt; /* bytes written */
1008c2ecf20Sopenharmony_ci	atomic64_t in_flight;      /* Number of I/Os in flight */
1018c2ecf20Sopenharmony_ci	atomic64_t read_cnt;       /* Count of read requests */
1028c2ecf20Sopenharmony_ci	atomic64_t write_cnt;      /* Count of write requests */
1038c2ecf20Sopenharmony_ci	atomic64_t other_cnt;      /* Count of other requests either
1048c2ecf20Sopenharmony_ci				    * implicit or from user space
1058c2ecf20Sopenharmony_ci				    * ioctl. */
1068c2ecf20Sopenharmony_ci	atomic64_t resid_cnt;      /* Count of resid_len > 0 */
1078c2ecf20Sopenharmony_ci	atomic64_t tot_read_time;  /* ktime spent completing reads */
1088c2ecf20Sopenharmony_ci	atomic64_t tot_write_time; /* ktime spent completing writes */
1098c2ecf20Sopenharmony_ci	atomic64_t tot_io_time;    /* ktime spent doing any I/O */
1108c2ecf20Sopenharmony_ci	ktime_t read_time;         /* holds ktime request was queued */
1118c2ecf20Sopenharmony_ci	ktime_t write_time;        /* holds ktime request was queued */
1128c2ecf20Sopenharmony_ci	ktime_t other_time;        /* holds ktime request was queued */
1138c2ecf20Sopenharmony_ci	atomic_t last_read_size;   /* Number of bytes issued for last read */
1148c2ecf20Sopenharmony_ci	atomic_t last_write_size;  /* Number of bytes issued for last write */
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define ST_NBR_PARTITIONS 4
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/* The tape drive descriptor */
1208c2ecf20Sopenharmony_cistruct scsi_tape {
1218c2ecf20Sopenharmony_ci	struct scsi_driver *driver;
1228c2ecf20Sopenharmony_ci	struct scsi_device *device;
1238c2ecf20Sopenharmony_ci	struct mutex lock;	/* For serialization */
1248c2ecf20Sopenharmony_ci	struct completion wait;	/* For SCSI commands */
1258c2ecf20Sopenharmony_ci	struct st_buffer *buffer;
1268c2ecf20Sopenharmony_ci	int index;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	/* Drive characteristics */
1298c2ecf20Sopenharmony_ci	unsigned char omit_blklims;
1308c2ecf20Sopenharmony_ci	unsigned char do_auto_lock;
1318c2ecf20Sopenharmony_ci	unsigned char can_bsr;
1328c2ecf20Sopenharmony_ci	unsigned char can_partitions;
1338c2ecf20Sopenharmony_ci	unsigned char two_fm;
1348c2ecf20Sopenharmony_ci	unsigned char fast_mteom;
1358c2ecf20Sopenharmony_ci	unsigned char immediate;
1368c2ecf20Sopenharmony_ci	unsigned char restr_dma;
1378c2ecf20Sopenharmony_ci	unsigned char scsi2_logical;
1388c2ecf20Sopenharmony_ci	unsigned char default_drvbuffer;	/* 0xff = don't touch, value 3 bits */
1398c2ecf20Sopenharmony_ci	unsigned char cln_mode;			/* 0 = none, otherwise sense byte nbr */
1408c2ecf20Sopenharmony_ci	unsigned char cln_sense_value;
1418c2ecf20Sopenharmony_ci	unsigned char cln_sense_mask;
1428c2ecf20Sopenharmony_ci	unsigned char use_pf;			/* Set Page Format bit in all mode selects? */
1438c2ecf20Sopenharmony_ci	unsigned char try_dio;			/* try direct i/o in general? */
1448c2ecf20Sopenharmony_ci	unsigned char try_dio_now;		/* try direct i/o before next close? */
1458c2ecf20Sopenharmony_ci	unsigned char c_algo;			/* compression algorithm */
1468c2ecf20Sopenharmony_ci	unsigned char pos_unknown;			/* after reset position unknown */
1478c2ecf20Sopenharmony_ci	unsigned char sili;			/* use SILI when reading in variable b mode */
1488c2ecf20Sopenharmony_ci	unsigned char immediate_filemark;	/* write filemark immediately */
1498c2ecf20Sopenharmony_ci	int tape_type;
1508c2ecf20Sopenharmony_ci	int long_timeout;	/* timeout for commands known to take long time */
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	/* Mode characteristics */
1538c2ecf20Sopenharmony_ci	struct st_modedef modes[ST_NBR_MODES];
1548c2ecf20Sopenharmony_ci	int current_mode;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/* Status variables */
1578c2ecf20Sopenharmony_ci	int partition;
1588c2ecf20Sopenharmony_ci	int new_partition;
1598c2ecf20Sopenharmony_ci	int nbr_partitions;	/* zero until partition support enabled */
1608c2ecf20Sopenharmony_ci	struct st_partstat ps[ST_NBR_PARTITIONS];
1618c2ecf20Sopenharmony_ci	unsigned char dirty;
1628c2ecf20Sopenharmony_ci	unsigned char ready;
1638c2ecf20Sopenharmony_ci	unsigned char write_prot;
1648c2ecf20Sopenharmony_ci	unsigned char drv_write_prot;
1658c2ecf20Sopenharmony_ci	unsigned char in_use;
1668c2ecf20Sopenharmony_ci	unsigned char blksize_changed;
1678c2ecf20Sopenharmony_ci	unsigned char density_changed;
1688c2ecf20Sopenharmony_ci	unsigned char compression_changed;
1698c2ecf20Sopenharmony_ci	unsigned char drv_buffer;
1708c2ecf20Sopenharmony_ci	unsigned char density;
1718c2ecf20Sopenharmony_ci	unsigned char door_locked;
1728c2ecf20Sopenharmony_ci	unsigned char autorew_dev;   /* auto-rewind device */
1738c2ecf20Sopenharmony_ci	unsigned char rew_at_close;  /* rewind necessary at close */
1748c2ecf20Sopenharmony_ci	unsigned char inited;
1758c2ecf20Sopenharmony_ci	unsigned char cleaning_req;  /* cleaning requested? */
1768c2ecf20Sopenharmony_ci	int block_size;
1778c2ecf20Sopenharmony_ci	int min_block;
1788c2ecf20Sopenharmony_ci	int max_block;
1798c2ecf20Sopenharmony_ci	int recover_count;     /* From tape opening */
1808c2ecf20Sopenharmony_ci	int recover_reg;       /* From last status call */
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci#if DEBUG
1838c2ecf20Sopenharmony_ci	unsigned char write_pending;
1848c2ecf20Sopenharmony_ci	int nbr_finished;
1858c2ecf20Sopenharmony_ci	int nbr_waits;
1868c2ecf20Sopenharmony_ci	int nbr_requests;
1878c2ecf20Sopenharmony_ci	int nbr_dio;
1888c2ecf20Sopenharmony_ci	int nbr_pages;
1898c2ecf20Sopenharmony_ci	unsigned char last_cmnd[6];
1908c2ecf20Sopenharmony_ci	unsigned char last_sense[16];
1918c2ecf20Sopenharmony_ci#endif
1928c2ecf20Sopenharmony_ci	struct gendisk *disk;
1938c2ecf20Sopenharmony_ci	struct kref     kref;
1948c2ecf20Sopenharmony_ci	struct scsi_tape_stats *stats;
1958c2ecf20Sopenharmony_ci};
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/* Bit masks for use_pf */
1988c2ecf20Sopenharmony_ci#define USE_PF      1
1998c2ecf20Sopenharmony_ci#define PF_TESTED   2
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci/* Values of eof */
2028c2ecf20Sopenharmony_ci#define	ST_NOEOF	0
2038c2ecf20Sopenharmony_ci#define ST_FM_HIT       1
2048c2ecf20Sopenharmony_ci#define ST_FM           2
2058c2ecf20Sopenharmony_ci#define ST_EOM_OK       3
2068c2ecf20Sopenharmony_ci#define ST_EOM_ERROR	4
2078c2ecf20Sopenharmony_ci#define	ST_EOD_1        5
2088c2ecf20Sopenharmony_ci#define ST_EOD_2        6
2098c2ecf20Sopenharmony_ci#define ST_EOD		7
2108c2ecf20Sopenharmony_ci/* EOD hit while reading => ST_EOD_1 => return zero => ST_EOD_2 =>
2118c2ecf20Sopenharmony_ci   return zero => ST_EOD, return ENOSPC */
2128c2ecf20Sopenharmony_ci/* When writing: ST_EOM_OK == early warning found, write OK
2138c2ecf20Sopenharmony_ci		 ST_EOD_1  == allow trying new write after early warning
2148c2ecf20Sopenharmony_ci		 ST_EOM_ERROR == early warning found, not able to write all */
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci/* Values of rw */
2178c2ecf20Sopenharmony_ci#define	ST_IDLE		0
2188c2ecf20Sopenharmony_ci#define	ST_READING	1
2198c2ecf20Sopenharmony_ci#define	ST_WRITING	2
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci/* Values of ready state */
2228c2ecf20Sopenharmony_ci#define ST_READY	0
2238c2ecf20Sopenharmony_ci#define ST_NOT_READY	1
2248c2ecf20Sopenharmony_ci#define ST_NO_TAPE	2
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/* Values for door lock state */
2278c2ecf20Sopenharmony_ci#define ST_UNLOCKED	0
2288c2ecf20Sopenharmony_ci#define ST_LOCKED_EXPLICIT 1
2298c2ecf20Sopenharmony_ci#define ST_LOCKED_AUTO  2
2308c2ecf20Sopenharmony_ci#define ST_LOCK_FAILS   3
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci/* Positioning SCSI-commands for Tandberg, etc. drives */
2338c2ecf20Sopenharmony_ci#define	QFA_REQUEST_BLOCK	0x02
2348c2ecf20Sopenharmony_ci#define	QFA_SEEK_BLOCK		0x0c
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci/* Setting the binary options */
2378c2ecf20Sopenharmony_ci#define ST_DONT_TOUCH  0
2388c2ecf20Sopenharmony_ci#define ST_NO          1
2398c2ecf20Sopenharmony_ci#define ST_YES         2
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci#define EXTENDED_SENSE_START  18
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci/* Masks for some conditions in the sense data */
2448c2ecf20Sopenharmony_ci#define SENSE_FMK   0x80
2458c2ecf20Sopenharmony_ci#define SENSE_EOM   0x40
2468c2ecf20Sopenharmony_ci#define SENSE_ILI   0x20
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci#endif
249