9648 lines · 273.6 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv *  Copyright (C) 1992  Eric Youngdale *  Simulate a host adapter with 2 disks attached.  Do a lot of checking *  to make sure that we are not getting blocks mixed up, and PANIC if *  anything out of the ordinary is seen. * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * * Copyright (C) 2001 - 2021 Douglas Gilbert * *  For documentation see http://sg.danny.cz/sg/scsi_debug.html */  #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ #include <linux/module.h>#include <linux/align.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/jiffies.h>#include <linux/slab.h>#include <linux/types.h>#include <linux/string.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/proc_fs.h>#include <linux/vmalloc.h>#include <linux/moduleparam.h>#include <linux/scatterlist.h>#include <linux/blkdev.h>#include <linux/crc-t10dif.h>#include <linux/spinlock.h>#include <linux/interrupt.h>#include <linux/atomic.h>#include <linux/hrtimer.h>#include <linux/uuid.h>#include <linux/t10-pi.h>#include <linux/msdos_partition.h>#include <linux/random.h>#include <linux/xarray.h>#include <linux/prefetch.h>#include <linux/debugfs.h>#include <linux/async.h>#include <linux/cleanup.h> #include <net/checksum.h> #include <linux/unaligned.h> #include <scsi/scsi.h>#include <scsi/scsi_cmnd.h>#include <scsi/scsi_device.h>#include <scsi/scsi_host.h>#include <scsi/scsicam.h>#include <scsi/scsi_eh.h>#include <scsi/scsi_tcq.h>#include <scsi/scsi_dbg.h> #include "sd.h"#include "scsi_logging.h" /* make sure inq_product_rev string corresponds to this version */#define SDEBUG_VERSION "0191"	/* format to fit INQUIRY revision field */static const char *sdebug_version_date = "20210520"; #define MY_NAME "scsi_debug" /* Additional Sense Code Qualifier (ASCQ) */#define ACK_NAK_TO 0x3 /* Default values for driver parameters */#define DEF_NUM_HOST   1#define DEF_NUM_TGTS   1#define DEF_MAX_LUNS   1/* With these defaults, this driver will make 1 host with 1 target * (id 0) containing 1 logical unit (lun 0). That is 1 device. */#define DEF_ATO 1#define DEF_CDB_LEN 10#define DEF_JDELAY   1		/* if > 0 unit is a jiffy */#define DEF_DEV_SIZE_PRE_INIT   0#define DEF_DEV_SIZE_MB   8#define DEF_ZBC_DEV_SIZE_MB   128#define DEF_DIF 0#define DEF_DIX 0#define DEF_PER_HOST_STORE false#define DEF_D_SENSE   0#define DEF_EVERY_NTH   0#define DEF_FAKE_RW	0#define DEF_GUARD 0#define DEF_HOST_LOCK 0#define DEF_LBPU 0#define DEF_LBPWS 0#define DEF_LBPWS10 0#define DEF_LBPRZ 1#define DEF_LOWEST_ALIGNED 0#define DEF_NDELAY   0		/* if > 0 unit is a nanosecond */#define DEF_NO_LUN_0   0#define DEF_NUM_PARTS   0#define DEF_OPTS   0#define DEF_OPT_BLKS 1024#define DEF_PHYSBLK_EXP 0#define DEF_OPT_XFERLEN_EXP 0#define DEF_PTYPE   TYPE_DISK#define DEF_RANDOM false#define DEF_REMOVABLE false#define DEF_SCSI_LEVEL   7    /* INQUIRY, byte2 [6->SPC-4; 7->SPC-5] */#define DEF_SECTOR_SIZE 512#define DEF_UNMAP_ALIGNMENT 0#define DEF_UNMAP_GRANULARITY 1#define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF#define DEF_UNMAP_MAX_DESC 256#define DEF_VIRTUAL_GB   0#define DEF_VPD_USE_HOSTNO 1#define DEF_WRITESAME_LENGTH 0xFFFF#define DEF_ATOMIC_WR 0#define DEF_ATOMIC_WR_MAX_LENGTH 128#define DEF_ATOMIC_WR_ALIGN 2#define DEF_ATOMIC_WR_GRAN 2#define DEF_ATOMIC_WR_MAX_LENGTH_BNDRY (DEF_ATOMIC_WR_MAX_LENGTH)#define DEF_ATOMIC_WR_MAX_BNDRY 128#define DEF_STRICT 0#define DEF_STATISTICS false#define DEF_SUBMIT_QUEUES 1#define DEF_TUR_MS_TO_READY 0#define DEF_UUID_CTL 0#define JDELAY_OVERRIDDEN -9999 /* Default parameters for ZBC drives */#define DEF_ZBC_ZONE_SIZE_MB	128#define DEF_ZBC_MAX_OPEN_ZONES	8#define DEF_ZBC_NR_CONV_ZONES	1 /* Default parameters for tape drives */#define TAPE_DEF_DENSITY  0x0#define TAPE_BAD_DENSITY  0x65#define TAPE_DEF_BLKSIZE  0#define TAPE_MIN_BLKSIZE  512#define TAPE_MAX_BLKSIZE  1048576#define TAPE_EW 20#define TAPE_MAX_PARTITIONS 2#define TAPE_UNITS 10000#define TAPE_PARTITION_1_UNITS 1000 /* The tape block data definitions */#define TAPE_BLOCK_FM_FLAG   ((u32)0x1 << 30)#define TAPE_BLOCK_EOD_FLAG  ((u32)0x2 << 30)#define TAPE_BLOCK_MARK_MASK ((u32)0x3 << 30)#define TAPE_BLOCK_SIZE_MASK (~TAPE_BLOCK_MARK_MASK)#define TAPE_BLOCK_MARK(a) (a & TAPE_BLOCK_MARK_MASK)#define TAPE_BLOCK_SIZE(a) (a & TAPE_BLOCK_SIZE_MASK)#define IS_TAPE_BLOCK_FM(a)   ((a & TAPE_BLOCK_FM_FLAG) != 0)#define IS_TAPE_BLOCK_EOD(a)  ((a & TAPE_BLOCK_EOD_FLAG) != 0) struct tape_block {	u32 fl_size;	unsigned char data[4];}; /* Flags for sense data */#define SENSE_FLAG_FILEMARK  0x80#define SENSE_FLAG_EOM 0x40#define SENSE_FLAG_ILI 0x20 #define SDEBUG_LUN_0_VAL 0 /* bit mask values for sdebug_opts */#define SDEBUG_OPT_NOISE		1#define SDEBUG_OPT_MEDIUM_ERR		2#define SDEBUG_OPT_TIMEOUT		4#define SDEBUG_OPT_RECOVERED_ERR	8#define SDEBUG_OPT_TRANSPORT_ERR	16#define SDEBUG_OPT_DIF_ERR		32#define SDEBUG_OPT_DIX_ERR		64#define SDEBUG_OPT_MAC_TIMEOUT		128#define SDEBUG_OPT_SHORT_TRANSFER	0x100#define SDEBUG_OPT_Q_NOISE		0x200#define SDEBUG_OPT_ALL_TSF		0x400	/* ignore */#define SDEBUG_OPT_RARE_TSF		0x800#define SDEBUG_OPT_N_WCE		0x1000#define SDEBUG_OPT_RESET_NOISE		0x2000#define SDEBUG_OPT_NO_CDB_NOISE		0x4000#define SDEBUG_OPT_HOST_BUSY		0x8000#define SDEBUG_OPT_CMD_ABORT		0x10000#define SDEBUG_OPT_UNALIGNED_WRITE	0x20000#define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \			      SDEBUG_OPT_RESET_NOISE)#define SDEBUG_OPT_RECOV_DIF_DIX (SDEBUG_OPT_RECOVERED_ERR | \				  SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR) /* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs) are returned in * priority order. In the subset implemented here lower numbers have higher * priority. The UA numbers should be a sequence starting from 0 with * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */#define SDEBUG_UA_POR 0		/* Power on, reset, or bus device reset */#define SDEBUG_UA_POOCCUR 1	/* Power on occurred */#define SDEBUG_UA_BUS_RESET 2#define SDEBUG_UA_MODE_CHANGED 3#define SDEBUG_UA_CAPACITY_CHANGED 4#define SDEBUG_UA_LUNS_CHANGED 5#define SDEBUG_UA_MICROCODE_CHANGED 6	/* simulate firmware change */#define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 7#define SDEBUG_UA_NOT_READY_TO_READY 8#define SDEBUG_NUM_UAS 9 /* when 1==SDEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this * sector on read commands: */#define OPT_MEDIUM_ERR_ADDR   0x1234 /* that's sector 4660 in decimal */#define OPT_MEDIUM_ERR_NUM    10     /* number of consecutive medium errs */ /* SDEBUG_CANQUEUE is the maximum number of commands that can be queued * (for response) per submit queue at one time. Can be reduced by max_queue * option. Command responses are not queued when jdelay=0 and ndelay=0. The * per-device DEF_CMD_PER_LUN can be changed via sysfs: * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth * but cannot exceed SDEBUG_CANQUEUE . */#define SDEBUG_CANQUEUE_WORDS  3	/* a WORD is bits in a long */#define SDEBUG_CANQUEUE  (SDEBUG_CANQUEUE_WORDS * BITS_PER_LONG)#define DEF_CMD_PER_LUN  SDEBUG_CANQUEUE /* UA - Unit Attention; SA - Service Action; SSU - Start Stop Unit */#define F_D_IN			1	/* Data-in command (e.g. READ) */#define F_D_OUT			2	/* Data-out command (e.g. WRITE) */#define F_D_OUT_MAYBE		4	/* WRITE SAME, NDOB bit */#define F_D_UNKN		8#define F_RL_WLUN_OK		0x10	/* allowed with REPORT LUNS W-LUN */#define F_SKIP_UA		0x20	/* bypass UAs (e.g. INQUIRY command) */#define F_DELAY_OVERR		0x40	/* for commands like INQUIRY */#define F_SA_LOW		0x80	/* SA is in cdb byte 1, bits 4 to 0 */#define F_SA_HIGH		0x100	/* SA is in cdb bytes 8 and 9 */#define F_INV_OP		0x200	/* invalid opcode (not supported) */#define F_FAKE_RW		0x400	/* bypass resp_*() when fake_rw set */#define F_M_ACCESS		0x800	/* media access, reacts to SSU state */#define F_SSU_DELAY		0x1000	/* SSU command delay (long-ish) */#define F_SYNC_DELAY		0x2000	/* SYNCHRONIZE CACHE delay */ /* Useful combinations of the above flags */#define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR)#define FF_MEDIA_IO (F_M_ACCESS | F_FAKE_RW)#define FF_SA (F_SA_HIGH | F_SA_LOW)#define F_LONG_DELAY		(F_SSU_DELAY | F_SYNC_DELAY) /* Device selection bit mask */#define DS_ALL     0xffffffff#define DS_SBC     (1 << TYPE_DISK)#define DS_SSC     (1 << TYPE_TAPE)#define DS_ZBC     (1 << TYPE_ZBC) #define DS_NO_SSC  (DS_ALL & ~DS_SSC) #define SDEBUG_MAX_PARTS 4 #define SDEBUG_MAX_CMD_LEN 32 #define SDEB_XA_NOT_IN_USE XA_MARK_1 /* Zone types (zbcr05 table 25) */enum sdebug_z_type {	ZBC_ZTYPE_CNV	= 0x1,	ZBC_ZTYPE_SWR	= 0x2,	ZBC_ZTYPE_SWP	= 0x3,	/* ZBC_ZTYPE_SOBR = 0x4, */	ZBC_ZTYPE_GAP	= 0x5,}; /* enumeration names taken from table 26, zbcr05 */enum sdebug_z_cond {	ZBC_NOT_WRITE_POINTER	= 0x0,	ZC1_EMPTY		= 0x1,	ZC2_IMPLICIT_OPEN	= 0x2,	ZC3_EXPLICIT_OPEN	= 0x3,	ZC4_CLOSED		= 0x4,	ZC6_READ_ONLY		= 0xd,	ZC5_FULL		= 0xe,	ZC7_OFFLINE		= 0xf,}; struct sdeb_zone_state {	/* ZBC: per zone state */	enum sdebug_z_type z_type;	enum sdebug_z_cond z_cond;	bool z_non_seq_resource;	unsigned int z_size;	sector_t z_start;	sector_t z_wp;}; enum sdebug_err_type {	ERR_TMOUT_CMD		= 0,	/* make specific scsi command timeout */	ERR_FAIL_QUEUE_CMD	= 1,	/* make specific scsi command's */					/* queuecmd return failed */	ERR_FAIL_CMD		= 2,	/* make specific scsi command's */					/* queuecmd return succeed but */					/* with errors set in scsi_cmnd */	ERR_ABORT_CMD_FAILED	= 3,	/* control return FAILED from */					/* scsi_debug_abort() */	ERR_LUN_RESET_FAILED	= 4,	/* control return FAILED from */					/* scsi_debug_device_reseLUN_RESET_FAILEDt() */}; struct sdebug_err_inject {	int type;	struct list_head list;	int cnt;	unsigned char cmd;	struct rcu_head rcu; 	union {		/*		 * For ERR_FAIL_QUEUE_CMD		 */		int queuecmd_ret; 		/*		 * For ERR_FAIL_CMD		 */		struct {			unsigned char host_byte;			unsigned char driver_byte;			unsigned char status_byte;			unsigned char sense_key;			unsigned char asc;			unsigned char asq;		};	};}; struct sdebug_dev_info {	struct list_head dev_list;	unsigned int channel;	unsigned int target;	u64 lun;	uuid_t lu_name;	struct sdebug_host_info *sdbg_host;	unsigned long uas_bm[1];	atomic_t stopped;	/* 1: by SSU, 2: device start */	bool used; 	/* For ZBC devices */	bool zoned;	unsigned int zcap;	unsigned int zsize;	unsigned int zsize_shift;	unsigned int nr_zones;	unsigned int nr_conv_zones;	unsigned int nr_seq_zones;	unsigned int nr_imp_open;	unsigned int nr_exp_open;	unsigned int nr_closed;	unsigned int max_open;	ktime_t create_ts;	/* time since bootup that this device was created */	struct sdeb_zone_state *zstate; 	/* For tapes */	unsigned int tape_blksize;	unsigned int tape_density;	unsigned char tape_partition;	unsigned char tape_nbr_partitions;	unsigned char tape_pending_nbr_partitions;	unsigned int tape_pending_part_0_size;	unsigned int tape_pending_part_1_size;	unsigned char tape_dce;	unsigned int tape_location[TAPE_MAX_PARTITIONS];	unsigned int tape_eop[TAPE_MAX_PARTITIONS];	struct tape_block *tape_blocks[TAPE_MAX_PARTITIONS]; 	struct dentry *debugfs_entry;	struct spinlock list_lock;	struct list_head inject_err_list;}; struct sdebug_target_info {	bool reset_fail;	struct dentry *debugfs_entry;}; struct sdebug_host_info {	struct list_head host_list;	int si_idx;	/* sdeb_store_info (per host) xarray index */	struct Scsi_Host *shost;	struct device dev;	struct list_head dev_info_list;}; /* There is an xarray of pointers to this struct's objects, one per host */struct sdeb_store_info {	rwlock_t macc_data_lck;	/* for media data access on this store */	rwlock_t macc_meta_lck;	/* for atomic media meta access on this store */	rwlock_t macc_sector_lck;	/* per-sector media data access on this store */	u8 *storep;		/* user data storage (ram) */	struct t10_pi_tuple *dif_storep; /* protection info */	void *map_storep;	/* provisioning map */}; #define dev_to_sdebug_host(d)	\	container_of(d, struct sdebug_host_info, dev) #define shost_to_sdebug_host(shost)	\	dev_to_sdebug_host(shost->dma_dev) enum sdeb_defer_type {SDEB_DEFER_NONE = 0, SDEB_DEFER_HRT = 1,		      SDEB_DEFER_WQ = 2, SDEB_DEFER_POLL = 3}; struct sdebug_defer {	struct hrtimer hrt;	struct execute_work ew;	ktime_t cmpl_ts;/* time since boot to complete this cmd */	int issuing_cpu;	bool aborted;	/* true when blk_abort_request() already called */	enum sdeb_defer_type defer_t;}; struct sdebug_scsi_cmd {	spinlock_t   lock;	struct sdebug_defer sd_dp;}; static atomic_t sdebug_cmnd_count;   /* number of incoming commands */static atomic_t sdebug_completions;  /* count of deferred completions */static atomic_t sdebug_miss_cpus;    /* submission + completion cpus differ */static atomic_t sdebug_a_tsf;	     /* 'almost task set full' counter */static atomic_t sdeb_inject_pending;static atomic_t sdeb_mq_poll_count;  /* bumped when mq_poll returns > 0 */ struct opcode_info_t {	u8 num_attached;	/* 0 if this is it (i.e. a leaf); use 0xff */				/* for terminating element */	u8 opcode;		/* if num_attached > 0, preferred */	u16 sa;			/* service action */	u32 devsel;		/* device type mask for this definition */	u32 flags;		/* OR-ed set of SDEB_F_* */	int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);	const struct opcode_info_t *arrp;  /* num_attached elements or NULL */	u8 len_mask[16];	/* len_mask[0]-->cdb_len, then mask for cdb */				/* 1 to min(cdb_len, 15); ignore cdb[15...] */}; /* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */enum sdeb_opcode_index {	SDEB_I_INVALID_OPCODE =	0,	SDEB_I_INQUIRY = 1,	SDEB_I_REPORT_LUNS = 2,	SDEB_I_REQUEST_SENSE = 3,	SDEB_I_TEST_UNIT_READY = 4,	SDEB_I_MODE_SENSE = 5,		/* 6, 10 */	SDEB_I_MODE_SELECT = 6,		/* 6, 10 */	SDEB_I_LOG_SENSE = 7,	SDEB_I_READ_CAPACITY = 8,	/* 10; 16 is in SA_IN(16) */	SDEB_I_READ = 9,		/* 6, 10, 12, 16 */	SDEB_I_WRITE = 10,		/* 6, 10, 12, 16 */	SDEB_I_START_STOP = 11,	SDEB_I_SERV_ACT_IN_16 = 12,	/* add ...SERV_ACT_IN_12 if needed */	SDEB_I_SERV_ACT_OUT_16 = 13,	/* add ...SERV_ACT_OUT_12 if needed */	SDEB_I_MAINT_IN = 14,	SDEB_I_MAINT_OUT = 15,	SDEB_I_VERIFY = 16,		/* VERIFY(10), VERIFY(16) */	SDEB_I_VARIABLE_LEN = 17,	/* READ(32), WRITE(32), WR_SCAT(32) */	SDEB_I_RESERVE = 18,		/* 6, 10 */	SDEB_I_RELEASE = 19,		/* 6, 10 */	SDEB_I_ALLOW_REMOVAL = 20,	/* PREVENT ALLOW MEDIUM REMOVAL */	SDEB_I_REZERO_UNIT = 21,	/* REWIND in SSC */	SDEB_I_ATA_PT = 22,		/* 12, 16 */	SDEB_I_SEND_DIAG = 23,	SDEB_I_UNMAP = 24,	SDEB_I_WRITE_BUFFER = 25,	SDEB_I_WRITE_SAME = 26,		/* 10, 16 */	SDEB_I_SYNC_CACHE = 27,		/* 10, 16 */	SDEB_I_COMP_WRITE = 28,	SDEB_I_PRE_FETCH = 29,		/* 10, 16 */	SDEB_I_ZONE_OUT = 30,		/* 0x94+SA; includes no data xfer */	SDEB_I_ZONE_IN = 31,		/* 0x95+SA; all have data-in */	SDEB_I_ATOMIC_WRITE_16 = 32,	SDEB_I_READ_BLOCK_LIMITS = 33,	SDEB_I_LOCATE = 34,	SDEB_I_WRITE_FILEMARKS = 35,	SDEB_I_SPACE = 36,	SDEB_I_FORMAT_MEDIUM = 37,	SDEB_I_ERASE = 38,	SDEB_I_LAST_ELEM_P1 = 39,	/* keep this last (previous + 1) */};  static const unsigned char opcode_ind_arr[256] = {/* 0x0; 0x0->0x1f: 6 byte cdbs */	SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE,	    SDEB_I_FORMAT_MEDIUM, SDEB_I_READ_BLOCK_LIMITS, 0, 0,	SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0,	SDEB_I_WRITE_FILEMARKS, SDEB_I_SPACE, SDEB_I_INQUIRY, 0, 0,	    SDEB_I_MODE_SELECT, SDEB_I_RESERVE, SDEB_I_RELEASE,	0, SDEB_I_ERASE, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG,	    SDEB_I_ALLOW_REMOVAL, 0,/* 0x20; 0x20->0x3f: 10 byte cdbs */	0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0,	SDEB_I_READ, 0, SDEB_I_WRITE, SDEB_I_LOCATE, 0, 0, 0, SDEB_I_VERIFY,	0, 0, 0, 0, SDEB_I_PRE_FETCH, SDEB_I_SYNC_CACHE, 0, 0,	0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0,/* 0x40; 0x40->0x5f: 10 byte cdbs */	0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0,

Lines 1–500 of 9648.