xref: /linux/drivers/scsi/scsi_debug.c (revision a6c339feb830daa097ccc4c50c339c1733c12d42)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
4  *  Copyright (C) 1992  Eric Youngdale
5  *  Simulate a host adapter with 2 disks attached.  Do a lot of checking
6  *  to make sure that we are not getting blocks mixed up, and PANIC if
7  *  anything out of the ordinary is seen.
8  * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9  *
10  * Copyright (C) 2001 - 2021 Douglas Gilbert
11  *
12  *  For documentation see http://sg.danny.cz/sg/scsi_debug.html
13  */
14 
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
17 
18 #include <linux/module.h>
19 #include <linux/align.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/jiffies.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
25 #include <linux/string.h>
26 #include <linux/fs.h>
27 #include <linux/init.h>
28 #include <linux/proc_fs.h>
29 #include <linux/vmalloc.h>
30 #include <linux/moduleparam.h>
31 #include <linux/scatterlist.h>
32 #include <linux/blkdev.h>
33 #include <linux/crc-t10dif.h>
34 #include <linux/spinlock.h>
35 #include <linux/interrupt.h>
36 #include <linux/atomic.h>
37 #include <linux/hrtimer.h>
38 #include <linux/uuid.h>
39 #include <linux/t10-pi.h>
40 #include <linux/msdos_partition.h>
41 #include <linux/random.h>
42 #include <linux/xarray.h>
43 #include <linux/prefetch.h>
44 #include <linux/debugfs.h>
45 #include <linux/async.h>
46 #include <linux/cleanup.h>
47 #include <linux/parser.h>
48 
49 #include <net/checksum.h>
50 
51 #include <linux/unaligned.h>
52 
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsi_device.h>
56 #include <scsi/scsi_host.h>
57 #include <scsi/scsicam.h>
58 #include <scsi/scsi_eh.h>
59 #include <scsi/scsi_tcq.h>
60 #include <scsi/scsi_dbg.h>
61 
62 #include "sd.h"
63 #include "scsi_logging.h"
64 
65 /* make sure inq_product_rev string corresponds to this version */
66 #define SDEBUG_VERSION "0191"	/* format to fit INQUIRY revision field */
67 static const char *sdebug_version_date = "20210520";
68 
69 #define MY_NAME "scsi_debug"
70 
71 /* Default values for driver parameters */
72 #define DEF_NUM_HOST   1
73 #define DEF_NUM_TGTS   1
74 #define DEF_MAX_LUNS   1
75 /* With these defaults, this driver will make 1 host with 1 target
76  * (id 0) containing 1 logical unit (lun 0). That is 1 device.
77  */
78 #define DEF_ATO 1
79 #define DEF_CDB_LEN 10
80 #define DEF_JDELAY   0		/* if > 0 unit is a jiffy */
81 #define DEF_DEV_SIZE_PRE_INIT   0
82 #define DEF_DEV_SIZE_MB   8
83 #define DEF_ZBC_DEV_SIZE_MB   128
84 #define DEF_DIF 0
85 #define DEF_DIX 0
86 #define DEF_PER_HOST_STORE false
87 #define DEF_D_SENSE   0
88 #define DEF_EVERY_NTH   0
89 #define DEF_FAKE_RW	0
90 #define DEF_GUARD 0
91 #define DEF_HOST_LOCK 0
92 #define DEF_LBPU 0
93 #define DEF_LBPWS 0
94 #define DEF_LBPWS10 0
95 #define DEF_LBPRZ 1
96 #define DEF_LOWEST_ALIGNED 0
97 #define DEF_NDELAY   0		/* if > 0 unit is a nanosecond */
98 #define DEF_NO_LUN_0   0
99 #define DEF_NUM_PARTS   0
100 #define DEF_OPTS   0
101 #define DEF_OPT_BLKS 1024
102 #define DEF_PHYSBLK_EXP 0
103 #define DEF_OPT_XFERLEN_EXP 0
104 #define DEF_PTYPE   TYPE_DISK
105 #define DEF_RANDOM false
106 #define DEF_REMOVABLE false
107 #define DEF_CLUSTERING true
108 #define DEF_SCSI_LEVEL   7    /* INQUIRY, byte2 [6->SPC-4; 7->SPC-5] */
109 #define DEF_SECTOR_SIZE 512
110 #define DEF_UNMAP_ALIGNMENT 0
111 #define DEF_UNMAP_GRANULARITY 1
112 #define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF
113 #define DEF_UNMAP_MAX_DESC 256
114 #define DEF_VIRTUAL_GB   0
115 #define DEF_VPD_USE_HOSTNO 1
116 #define DEF_WRITESAME_LENGTH 0xFFFF
117 #define DEF_ATOMIC_WR 0
118 #define DEF_ATOMIC_WR_MAX_LENGTH 128
119 #define DEF_ATOMIC_WR_ALIGN 2
120 #define DEF_ATOMIC_WR_GRAN 2
121 #define DEF_ATOMIC_WR_MAX_LENGTH_BNDRY (DEF_ATOMIC_WR_MAX_LENGTH)
122 #define DEF_ATOMIC_WR_MAX_BNDRY 128
123 #define DEF_STRICT 0
124 #define DEF_STATISTICS false
125 #define DEF_SUBMIT_QUEUES 1
126 #define DEF_TUR_MS_TO_READY 0
127 #define DEF_UUID_CTL 0
128 #define JDELAY_OVERRIDDEN -9999
129 
130 /* Default parameters for ZBC drives */
131 #define DEF_ZBC_ZONE_SIZE_MB	128
132 #define DEF_ZBC_MAX_OPEN_ZONES	8
133 #define DEF_ZBC_NR_CONV_ZONES	1
134 
135 /* Default parameters for tape drives */
136 #define TAPE_DEF_DENSITY  0x0
137 #define TAPE_BAD_DENSITY  0x65
138 #define TAPE_DEF_BLKSIZE  0
139 #define TAPE_MIN_BLKSIZE  512
140 #define TAPE_MAX_BLKSIZE  1048576
141 #define TAPE_EW 20
142 #define TAPE_MAX_PARTITIONS 2
143 #define TAPE_UNITS 10000
144 #define TAPE_PARTITION_1_UNITS 1000
145 
146 /* The tape block data definitions */
147 #define TAPE_BLOCK_FM_FLAG   ((u32)0x1 << 30)
148 #define TAPE_BLOCK_EOD_FLAG  ((u32)0x2 << 30)
149 #define TAPE_BLOCK_MARK_MASK ((u32)0x3 << 30)
150 #define TAPE_BLOCK_SIZE_MASK (~TAPE_BLOCK_MARK_MASK)
151 #define TAPE_BLOCK_MARK(a) (a & TAPE_BLOCK_MARK_MASK)
152 #define TAPE_BLOCK_SIZE(a) (a & TAPE_BLOCK_SIZE_MASK)
153 #define IS_TAPE_BLOCK_FM(a)   ((a & TAPE_BLOCK_FM_FLAG) != 0)
154 #define IS_TAPE_BLOCK_EOD(a)  ((a & TAPE_BLOCK_EOD_FLAG) != 0)
155 
156 struct tape_block {
157 	u32 fl_size;
158 	unsigned char data[4];
159 };
160 
161 /* Flags for sense data */
162 #define SENSE_FLAG_FILEMARK  0x80
163 #define SENSE_FLAG_EOM 0x40
164 #define SENSE_FLAG_ILI 0x20
165 
166 #define SDEBUG_LUN_0_VAL 0
167 
168 /* bit mask values for sdebug_opts */
169 #define SDEBUG_OPT_NOISE		1
170 #define SDEBUG_OPT_MEDIUM_ERR		2
171 #define SDEBUG_OPT_TIMEOUT		4
172 #define SDEBUG_OPT_RECOVERED_ERR	8
173 #define SDEBUG_OPT_TRANSPORT_ERR	16
174 #define SDEBUG_OPT_DIF_ERR		32
175 #define SDEBUG_OPT_DIX_ERR		64
176 #define SDEBUG_OPT_MAC_TIMEOUT		128
177 #define SDEBUG_OPT_SHORT_TRANSFER	0x100
178 #define SDEBUG_OPT_Q_NOISE		0x200
179 #define SDEBUG_OPT_ALL_TSF		0x400	/* ignore */
180 #define SDEBUG_OPT_RARE_TSF		0x800
181 #define SDEBUG_OPT_N_WCE		0x1000
182 #define SDEBUG_OPT_RESET_NOISE		0x2000
183 #define SDEBUG_OPT_NO_CDB_NOISE		0x4000
184 #define SDEBUG_OPT_HOST_BUSY		0x8000
185 #define SDEBUG_OPT_CMD_ABORT		0x10000
186 #define SDEBUG_OPT_UNALIGNED_WRITE	0x20000
187 #define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \
188 			      SDEBUG_OPT_RESET_NOISE)
189 #define SDEBUG_OPT_RECOV_DIF_DIX (SDEBUG_OPT_RECOVERED_ERR | \
190 				  SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR)
191 
192 /* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs) are returned in
193  * priority order. In the subset implemented here lower numbers have higher
194  * priority. The UA numbers should be a sequence starting from 0 with
195  * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */
196 #define SDEBUG_UA_POR 0		/* Power on, reset, or bus device reset */
197 #define SDEBUG_UA_POOCCUR 1	/* Power on occurred */
198 #define SDEBUG_UA_BUS_RESET 2
199 #define SDEBUG_UA_MODE_CHANGED 3
200 #define SDEBUG_UA_CAPACITY_CHANGED 4
201 #define SDEBUG_UA_LUNS_CHANGED 5
202 #define SDEBUG_UA_MICROCODE_CHANGED 6	/* simulate firmware change */
203 #define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 7
204 #define SDEBUG_UA_NOT_READY_TO_READY 8
205 #define SDEBUG_NUM_UAS 9
206 
207 /* when 1==SDEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
208  * sector on read commands: */
209 #define OPT_MEDIUM_ERR_ADDR   0x1234 /* that's sector 4660 in decimal */
210 #define OPT_MEDIUM_ERR_NUM    10     /* number of consecutive medium errs */
211 
212 /* SDEBUG_CANQUEUE is the maximum number of commands that can be queued
213  * (for response) per submit queue at one time. Can be reduced by max_queue
214  * option. Command responses are not queued when jdelay=0 and ndelay=0. The
215  * per-device DEF_CMD_PER_LUN can be changed via sysfs:
216  * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth
217  * but cannot exceed SDEBUG_CANQUEUE .
218  */
219 #define SDEBUG_CANQUEUE  (4096)
220 #define DEF_CMD_PER_LUN  SDEBUG_CANQUEUE
221 
222 /* UA - Unit Attention; SA - Service Action; SSU - Start Stop Unit */
223 #define F_D_IN			1	/* Data-in command (e.g. READ) */
224 #define F_D_OUT			2	/* Data-out command (e.g. WRITE) */
225 #define F_D_OUT_MAYBE		4	/* WRITE SAME, NDOB bit */
226 #define F_D_UNKN		8
227 #define F_RL_WLUN_OK		0x10	/* allowed with REPORT LUNS W-LUN */
228 #define F_SKIP_UA		0x20	/* bypass UAs (e.g. INQUIRY command) */
229 #define F_DELAY_OVERR		0x40	/* for commands like INQUIRY */
230 #define F_SA_LOW		0x80	/* SA is in cdb byte 1, bits 4 to 0 */
231 #define F_SA_HIGH		0x100	/* SA is in cdb bytes 8 and 9 */
232 #define F_INV_OP		0x200	/* invalid opcode (not supported) */
233 #define F_FAKE_RW		0x400	/* bypass resp_*() when fake_rw set */
234 #define F_M_ACCESS		0x800	/* media access, reacts to SSU state */
235 #define F_SSU_DELAY		0x1000	/* SSU command delay (long-ish) */
236 #define F_SYNC_DELAY		0x2000	/* SYNCHRONIZE CACHE delay */
237 
238 /* Useful combinations of the above flags */
239 #define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR)
240 #define FF_MEDIA_IO (F_M_ACCESS | F_FAKE_RW)
241 #define FF_SA (F_SA_HIGH | F_SA_LOW)
242 #define F_LONG_DELAY		(F_SSU_DELAY | F_SYNC_DELAY)
243 
244 /* Device selection bit mask */
245 #define DS_ALL     0xffffffff
246 #define DS_SBC     (1 << TYPE_DISK)
247 #define DS_SSC     (1 << TYPE_TAPE)
248 #define DS_ZBC     (1 << TYPE_ZBC)
249 
250 #define DS_NO_SSC  (DS_ALL & ~DS_SSC)
251 
252 #define SDEBUG_MAX_PARTS 4
253 
254 #define SDEBUG_MAX_CMD_LEN 32
255 
256 #define SDEB_XA_NOT_IN_USE XA_MARK_1
257 
258 /* Zone types (zbcr05 table 25) */
259 enum sdebug_z_type {
260 	ZBC_ZTYPE_CNV	= 0x1,
261 	ZBC_ZTYPE_SWR	= 0x2,
262 	ZBC_ZTYPE_SWP	= 0x3,
263 	/* ZBC_ZTYPE_SOBR = 0x4, */
264 	ZBC_ZTYPE_GAP	= 0x5,
265 };
266 
267 /* enumeration names taken from table 26, zbcr05 */
268 enum sdebug_z_cond {
269 	ZBC_NOT_WRITE_POINTER	= 0x0,
270 	ZC1_EMPTY		= 0x1,
271 	ZC2_IMPLICIT_OPEN	= 0x2,
272 	ZC3_EXPLICIT_OPEN	= 0x3,
273 	ZC4_CLOSED		= 0x4,
274 	ZC6_READ_ONLY		= 0xd,
275 	ZC5_FULL		= 0xe,
276 	ZC7_OFFLINE		= 0xf,
277 };
278 
279 struct sdeb_zone_state {	/* ZBC: per zone state */
280 	enum sdebug_z_type z_type;
281 	enum sdebug_z_cond z_cond;
282 	bool z_non_seq_resource;
283 	unsigned int z_size;
284 	sector_t z_start;
285 	sector_t z_wp;
286 };
287 
288 enum sdebug_err_type {
289 	ERR_TMOUT_CMD		= 0,	/* make specific scsi command timeout */
290 	ERR_FAIL_QUEUE_CMD	= 1,	/* make specific scsi command's */
291 					/* queuecmd return failed */
292 	ERR_FAIL_CMD		= 2,	/* make specific scsi command's */
293 					/* queuecmd return succeed but */
294 					/* with errors set in scsi_cmnd */
295 	ERR_ABORT_CMD_FAILED	= 3,	/* control return FAILED from */
296 					/* scsi_debug_abort() */
297 	ERR_LUN_RESET_FAILED	= 4,	/* control return FAILED from */
298 					/* scsi_debug_device_reseLUN_RESET_FAILEDt() */
299 };
300 
301 struct sdebug_err_inject {
302 	int type;
303 	struct list_head list;
304 	int cnt;
305 	unsigned char cmd;
306 	struct rcu_head rcu;
307 
308 	union {
309 		/*
310 		 * For ERR_FAIL_QUEUE_CMD
311 		 */
312 		int queuecmd_ret;
313 
314 		/*
315 		 * For ERR_FAIL_CMD
316 		 */
317 		struct {
318 			unsigned char host_byte;
319 			unsigned char driver_byte;
320 			unsigned char status_byte;
321 			unsigned char sense_key;
322 			unsigned char asc;
323 			unsigned char asq;
324 		};
325 	};
326 };
327 
328 struct sdebug_dev_info {
329 	struct list_head dev_list;
330 	unsigned int channel;
331 	unsigned int target;
332 	u64 lun;
333 	uuid_t lu_name;
334 	struct sdebug_host_info *sdbg_host;
335 	unsigned long uas_bm[1];
336 	atomic_t stopped;	/* 1: by SSU, 2: device start */
337 	bool used;
338 
339 	/* For ZBC devices */
340 	bool zoned;
341 	unsigned int zcap;
342 	unsigned int zsize;
343 	unsigned int zsize_shift;
344 	unsigned int nr_zones;
345 	unsigned int nr_conv_zones;
346 	unsigned int nr_seq_zones;
347 	unsigned int nr_imp_open;
348 	unsigned int nr_exp_open;
349 	unsigned int nr_closed;
350 	unsigned int max_open;
351 	ktime_t create_ts;	/* time since bootup that this device was created */
352 	struct sdeb_zone_state *zstate;
353 
354 	/* For tapes */
355 	unsigned int tape_blksize;
356 	unsigned int tape_density;
357 	unsigned char tape_partition;
358 	unsigned char tape_nbr_partitions;
359 	unsigned char tape_pending_nbr_partitions;
360 	unsigned int tape_pending_part_0_size;
361 	unsigned int tape_pending_part_1_size;
362 	unsigned char tape_dce;
363 	unsigned int tape_location[TAPE_MAX_PARTITIONS];
364 	unsigned int tape_eop[TAPE_MAX_PARTITIONS];
365 	struct tape_block *tape_blocks[TAPE_MAX_PARTITIONS];
366 
367 	struct dentry *debugfs_entry;
368 	struct spinlock list_lock;
369 	struct list_head inject_err_list;
370 };
371 
372 struct sdebug_target_info {
373 	bool reset_fail;
374 	struct dentry *debugfs_entry;
375 };
376 
377 struct sdebug_host_info {
378 	struct list_head host_list;
379 	int si_idx;	/* sdeb_store_info (per host) xarray index */
380 	struct Scsi_Host *shost;
381 	struct device dev;
382 	struct list_head dev_info_list;
383 };
384 
385 /* There is an xarray of pointers to this struct's objects, one per host */
386 struct sdeb_store_info {
387 	rwlock_t macc_data_lck;	/* for media data access on this store */
388 	rwlock_t macc_meta_lck;	/* for atomic media meta access on this store */
389 	rwlock_t macc_sector_lck;	/* per-sector media data access on this store */
390 	u8 *storep;		/* user data storage (ram) */
391 	struct t10_pi_tuple *dif_storep; /* protection info */
392 	void *map_storep;	/* provisioning map */
393 };
394 
395 #define dev_to_sdebug_host(d)	\
396 	container_of(d, struct sdebug_host_info, dev)
397 
398 #define shost_to_sdebug_host(shost)	\
399 	dev_to_sdebug_host(shost->dma_dev)
400 
401 enum sdeb_defer_type {SDEB_DEFER_NONE = 0, SDEB_DEFER_HRT = 1,
402 		      SDEB_DEFER_WQ = 2, SDEB_DEFER_POLL = 3};
403 
404 struct sdebug_defer {
405 	struct hrtimer hrt;
406 	struct execute_work ew;
407 	ktime_t cmpl_ts;/* time since boot to complete this cmd */
408 	int issuing_cpu;
409 	bool aborted;	/* true when blk_abort_request() already called */
410 	enum sdeb_defer_type defer_t;
411 };
412 
413 struct sdebug_scsi_cmd {
414 	spinlock_t   lock;
415 	struct sdebug_defer sd_dp;
416 };
417 
418 static atomic_t sdebug_cmnd_count;   /* number of incoming commands */
419 static atomic_t sdebug_completions;  /* count of deferred completions */
420 static atomic_t sdebug_miss_cpus;    /* submission + completion cpus differ */
421 static atomic_t sdebug_a_tsf;	     /* 'almost task set full' counter */
422 static atomic_t sdeb_inject_pending;
423 static atomic_t sdeb_mq_poll_count;  /* bumped when mq_poll returns > 0 */
424 
425 struct opcode_info_t {
426 	u8 num_attached;	/* 0 if this is it (i.e. a leaf); use 0xff */
427 				/* for terminating element */
428 	u8 opcode;		/* if num_attached > 0, preferred */
429 	u16 sa;			/* service action */
430 	u32 devsel;		/* device type mask for this definition */
431 	u32 flags;		/* OR-ed set of SDEB_F_* */
432 	int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
433 	const struct opcode_info_t *arrp;  /* num_attached elements or NULL */
434 	u8 len_mask[16];	/* len_mask[0]-->cdb_len, then mask for cdb */
435 				/* 1 to min(cdb_len, 15); ignore cdb[15...] */
436 };
437 
438 /* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */
439 enum sdeb_opcode_index {
440 	SDEB_I_INVALID_OPCODE =	0,
441 	SDEB_I_INQUIRY = 1,
442 	SDEB_I_REPORT_LUNS = 2,
443 	SDEB_I_REQUEST_SENSE = 3,
444 	SDEB_I_TEST_UNIT_READY = 4,
445 	SDEB_I_MODE_SENSE = 5,		/* 6, 10 */
446 	SDEB_I_MODE_SELECT = 6,		/* 6, 10 */
447 	SDEB_I_LOG_SENSE = 7,
448 	SDEB_I_READ_CAPACITY = 8,	/* 10; 16 is in SA_IN(16) */
449 	SDEB_I_READ = 9,		/* 6, 10, 12, 16 */
450 	SDEB_I_WRITE = 10,		/* 6, 10, 12, 16 */
451 	SDEB_I_START_STOP = 11,
452 	SDEB_I_SERV_ACT_IN_16 = 12,	/* add ...SERV_ACT_IN_12 if needed */
453 	SDEB_I_SERV_ACT_OUT_16 = 13,	/* add ...SERV_ACT_OUT_12 if needed */
454 	SDEB_I_MAINT_IN = 14,
455 	SDEB_I_MAINT_OUT = 15,
456 	SDEB_I_VERIFY = 16,		/* VERIFY(10), VERIFY(16) */
457 	SDEB_I_VARIABLE_LEN = 17,	/* READ(32), WRITE(32), WR_SCAT(32) */
458 	SDEB_I_RESERVE = 18,		/* 6, 10 */
459 	SDEB_I_RELEASE = 19,		/* 6, 10 */
460 	SDEB_I_ALLOW_REMOVAL = 20,	/* PREVENT ALLOW MEDIUM REMOVAL */
461 	SDEB_I_REZERO_UNIT = 21,	/* REWIND in SSC */
462 	SDEB_I_ATA_PT = 22,		/* 12, 16 */
463 	SDEB_I_SEND_DIAG = 23,
464 	SDEB_I_UNMAP = 24,
465 	SDEB_I_WRITE_BUFFER = 25,
466 	SDEB_I_WRITE_SAME = 26,		/* 10, 16 */
467 	SDEB_I_SYNC_CACHE = 27,		/* 10, 16 */
468 	SDEB_I_COMP_WRITE = 28,
469 	SDEB_I_PRE_FETCH = 29,		/* 10, 16 */
470 	SDEB_I_ZONE_OUT = 30,		/* 0x94+SA; includes no data xfer */
471 	SDEB_I_ZONE_IN = 31,		/* 0x95+SA; all have data-in */
472 	SDEB_I_ATOMIC_WRITE_16 = 32,
473 	SDEB_I_READ_BLOCK_LIMITS = 33,
474 	SDEB_I_LOCATE = 34,
475 	SDEB_I_WRITE_FILEMARKS = 35,
476 	SDEB_I_SPACE = 36,
477 	SDEB_I_FORMAT_MEDIUM = 37,
478 	SDEB_I_ERASE = 38,
479 	SDEB_I_LAST_ELEM_P1 = 39,	/* keep this last (previous + 1) */
480 };
481 
482 
483 static const unsigned char opcode_ind_arr[256] = {
484 /* 0x0; 0x0->0x1f: 6 byte cdbs */
485 	SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE,
486 	    SDEB_I_FORMAT_MEDIUM, SDEB_I_READ_BLOCK_LIMITS, 0, 0,
487 	SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0,
488 	SDEB_I_WRITE_FILEMARKS, SDEB_I_SPACE, SDEB_I_INQUIRY, 0, 0,
489 	    SDEB_I_MODE_SELECT, SDEB_I_RESERVE, SDEB_I_RELEASE,
490 	0, SDEB_I_ERASE, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG,
491 	    SDEB_I_ALLOW_REMOVAL, 0,
492 /* 0x20; 0x20->0x3f: 10 byte cdbs */
493 	0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0,
494 	SDEB_I_READ, 0, SDEB_I_WRITE, SDEB_I_LOCATE, 0, 0, 0, SDEB_I_VERIFY,
495 	0, 0, 0, 0, SDEB_I_PRE_FETCH, SDEB_I_SYNC_CACHE, 0, 0,
496 	0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0,
497 /* 0x40; 0x40->0x5f: 10 byte cdbs */
498 	0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0,
499 	0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0,
500 	0, 0, 0, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
501 	    SDEB_I_RELEASE,
502 	0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0,
503 /* 0x60; 0x60->0x7d are reserved, 0x7e is "extended cdb" */
504 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
505 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
506 	0, SDEB_I_VARIABLE_LEN,
507 /* 0x80; 0x80->0x9f: 16 byte cdbs */
508 	0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0,
509 	SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0,
510 	0, 0, 0, SDEB_I_VERIFY,
511 	SDEB_I_PRE_FETCH, SDEB_I_SYNC_CACHE, 0, SDEB_I_WRITE_SAME,
512 	SDEB_I_ZONE_OUT, SDEB_I_ZONE_IN, 0, 0,
513 	0, 0, 0, 0,
514 	SDEB_I_ATOMIC_WRITE_16, 0, SDEB_I_SERV_ACT_IN_16, SDEB_I_SERV_ACT_OUT_16,
515 /* 0xa0; 0xa0->0xbf: 12 byte cdbs */
516 	SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN,
517 	     SDEB_I_MAINT_OUT, 0, 0, 0,
518 	SDEB_I_READ, 0 /* SDEB_I_SERV_ACT_OUT_12 */, SDEB_I_WRITE,
519 	     0 /* SDEB_I_SERV_ACT_IN_12 */, 0, 0, 0, 0,
520 	0, 0, 0, 0, 0, 0, 0, 0,
521 	0, 0, 0, 0, 0, 0, 0, 0,
522 /* 0xc0; 0xc0->0xff: vendor specific */
523 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
524 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
525 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
526 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
527 };
528 
529 /*
530  * The following "response" functions return the SCSI mid-level's 4 byte
531  * tuple-in-an-int. To handle commands with an IMMED bit, for a faster
532  * command completion, they can mask their return value with
533  * SDEG_RES_IMMED_MASK .
534  */
535 #define SDEG_RES_IMMED_MASK 0x40000000
536 
537 static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *);
538 static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *);
539 static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *);
540 static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
541 static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *);
542 static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
543 static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *);
544 static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
545 static int resp_read_tape(struct scsi_cmnd *, struct sdebug_dev_info *);
546 static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
547 static int resp_write_tape(struct scsi_cmnd *, struct sdebug_dev_info *);
548 static int resp_write_scat(struct scsi_cmnd *, struct sdebug_dev_info *);
549 static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *);
550 static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *);
551 static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *);
552 static int resp_get_stream_status(struct scsi_cmnd *scp,
553 				  struct sdebug_dev_info *devip);
554 static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *);
555 static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *);
556 static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *);
557 static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *);
558 static int resp_verify(struct scsi_cmnd *, struct sdebug_dev_info *);
559 static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *);
560 static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *);
561 static int resp_comp_write(struct scsi_cmnd *, struct sdebug_dev_info *);
562 static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *);
563 static int resp_sync_cache(struct scsi_cmnd *, struct sdebug_dev_info *);
564 static int resp_pre_fetch(struct scsi_cmnd *, struct sdebug_dev_info *);
565 static int resp_report_zones(struct scsi_cmnd *, struct sdebug_dev_info *);
566 static int resp_atomic_write(struct scsi_cmnd *, struct sdebug_dev_info *);
567 static int resp_open_zone(struct scsi_cmnd *, struct sdebug_dev_info *);
568 static int resp_close_zone(struct scsi_cmnd *, struct sdebug_dev_info *);
569 static int resp_finish_zone(struct scsi_cmnd *, struct sdebug_dev_info *);
570 static int resp_rwp_zone(struct scsi_cmnd *, struct sdebug_dev_info *);
571 static int resp_read_blklimits(struct scsi_cmnd *, struct sdebug_dev_info *);
572 static int resp_locate(struct scsi_cmnd *, struct sdebug_dev_info *);
573 static int resp_write_filemarks(struct scsi_cmnd *, struct sdebug_dev_info *);
574 static int resp_space(struct scsi_cmnd *, struct sdebug_dev_info *);
575 static int resp_read_position(struct scsi_cmnd *, struct sdebug_dev_info *);
576 static int resp_rewind(struct scsi_cmnd *, struct sdebug_dev_info *);
577 static int resp_format_medium(struct scsi_cmnd *, struct sdebug_dev_info *);
578 static int resp_erase(struct scsi_cmnd *, struct sdebug_dev_info *);
579 
580 static int sdebug_do_add_host(bool mk_new_store);
581 static int sdebug_add_host_helper(int per_host_idx);
582 static void sdebug_do_remove_host(bool the_end);
583 static int sdebug_add_store(void);
584 static void sdebug_erase_store(int idx, struct sdeb_store_info *sip);
585 static void sdebug_erase_all_stores(bool apart_from_first);
586 
587 /*
588  * The following are overflow arrays for cdbs that "hit" the same index in
589  * the opcode_info_arr array. The most time sensitive (or commonly used) cdb
590  * should be placed in opcode_info_arr[], the others should be placed here.
591  */
592 static const struct opcode_info_t msense_iarr[] = {
593 	{0, 0x1a, 0, DS_ALL, F_D_IN, NULL, NULL,
594 	    {6,  0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
595 };
596 
597 static const struct opcode_info_t mselect_iarr[] = {
598 	{0, 0x15, 0, DS_ALL, F_D_OUT, NULL, NULL,
599 	    {6,  0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
600 };
601 
602 static const struct opcode_info_t read_iarr[] = {
603 	{0, 0x28, 0, DS_NO_SSC, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(10) */
604 	    {10,  0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
605 	     0, 0, 0, 0} },
606 	{0, 0x8, 0, DS_NO_SSC, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL, /* READ(6) disk */
607 	    {6,  0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
608 	{0, 0x8, 0, DS_SSC, F_D_IN | FF_MEDIA_IO, resp_read_tape, NULL, /* READ(6) tape */
609 	    {6,  0x03, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
610 	{0, 0xa8, 0, DS_NO_SSC, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(12) */
611 	    {12,  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf,
612 	     0xc7, 0, 0, 0, 0} },
613 };
614 
615 static const struct opcode_info_t write_iarr[] = {
616 	{0, 0x2a, 0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO, resp_write_dt0,  /* WRITE(10) */
617 	    NULL, {10,  0xfb, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7,
618 		   0, 0, 0, 0, 0, 0} },
619 	{0, 0xa, 0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(6) disk */
620 	    NULL, {6,  0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0,
621 		   0, 0, 0} },
622 	{0, 0xa, 0, DS_SSC, F_D_OUT | FF_MEDIA_IO, resp_write_tape, /* WRITE(6) tape */
623 	    NULL, {6,  0x01, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0,
624 		   0, 0, 0} },
625 	{0, 0xaa, 0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO, resp_write_dt0,  /* WRITE(12) */
626 	    NULL, {12,  0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
627 		   0xbf, 0xc7, 0, 0, 0, 0} },
628 };
629 
630 static const struct opcode_info_t verify_iarr[] = {
631 	{0, 0x2f, 0, DS_NO_SSC, F_D_OUT_MAYBE | FF_MEDIA_IO, resp_verify,/* VERIFY(10) */
632 	    NULL, {10,  0xf7, 0xff, 0xff, 0xff, 0xff, 0xbf, 0xff, 0xff, 0xc7,
633 		   0, 0, 0, 0, 0, 0} },
634 };
635 
636 static const struct opcode_info_t sa_in_16_iarr[] = {
637 	{0, 0x9e, 0x12, DS_NO_SSC, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL,
638 	    {16,  0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
639 	     0xff, 0xff, 0xff, 0, 0xc7} },	/* GET LBA STATUS(16) */
640 	{0, 0x9e, 0x16, DS_NO_SSC, F_SA_LOW | F_D_IN, resp_get_stream_status, NULL,
641 	    {16, 0x16, 0, 0, 0xff, 0xff, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff,
642 	     0, 0} },	/* GET STREAM STATUS */
643 };
644 
645 static const struct opcode_info_t vl_iarr[] = {	/* VARIABLE LENGTH */
646 	{0, 0x7f, 0xb, DS_NO_SSC, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_dt0,
647 	    NULL, {32,  0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0xb, 0xfa,
648 		   0, 0xff, 0xff, 0xff, 0xff} },	/* WRITE(32) */
649 	{0, 0x7f, 0x11, DS_NO_SSC, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_scat,
650 	    NULL, {32,  0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x11, 0xf8,
651 		   0, 0xff, 0xff, 0x0, 0x0} },	/* WRITE SCATTERED(32) */
652 };
653 
654 static const struct opcode_info_t maint_in_iarr[] = {	/* MAINT IN */
655 	{0, 0xa3, 0xc, DS_ALL, F_SA_LOW | F_D_IN, resp_rsup_opcodes, NULL,
656 	    {12,  0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0,
657 	     0xc7, 0, 0, 0, 0} }, /* REPORT SUPPORTED OPERATION CODES */
658 	{0, 0xa3, 0xd, DS_ALL, F_SA_LOW | F_D_IN, resp_rsup_tmfs, NULL,
659 	    {12,  0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
660 	     0, 0} },	/* REPORTED SUPPORTED TASK MANAGEMENT FUNCTIONS */
661 };
662 
663 static const struct opcode_info_t write_same_iarr[] = {
664 	{0, 0x93, 0, DS_NO_SSC, F_D_OUT_MAYBE | FF_MEDIA_IO, resp_write_same_16, NULL,
665 	    {16,  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
666 	     0xff, 0xff, 0xff, 0x3f, 0xc7} },		/* WRITE SAME(16) */
667 };
668 
669 static const struct opcode_info_t reserve_iarr[] = {
670 	{0, 0x16, 0, DS_ALL, F_D_OUT, NULL, NULL,	/* RESERVE(6) */
671 	    {6,  0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
672 };
673 
674 static const struct opcode_info_t release_iarr[] = {
675 	{0, 0x17, 0, DS_ALL, F_D_OUT, NULL, NULL,	/* RELEASE(6) */
676 	    {6,  0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
677 };
678 
679 static const struct opcode_info_t sync_cache_iarr[] = {
680 	{0, 0x91, 0, DS_NO_SSC, F_SYNC_DELAY | F_M_ACCESS, resp_sync_cache, NULL,
681 	    {16,  0x6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
682 	     0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} },	/* SYNC_CACHE (16) */
683 };
684 
685 static const struct opcode_info_t pre_fetch_iarr[] = {
686 	{0, 0x90, 0, DS_NO_SSC, F_SYNC_DELAY | FF_MEDIA_IO, resp_pre_fetch, NULL,
687 	    {16,  0x2, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
688 	     0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} },	/* PRE-FETCH (16) */
689 	{0, 0x34, 0, DS_SSC, F_SYNC_DELAY | FF_MEDIA_IO, resp_read_position, NULL,
690 	    {10,  0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc7, 0, 0,
691 	     0, 0, 0, 0} },				/* READ POSITION (10) */
692 };
693 
694 static const struct opcode_info_t zone_out_iarr[] = {	/* ZONE OUT(16) */
695 	{0, 0x94, 0x1, DS_NO_SSC, F_SA_LOW | F_M_ACCESS, resp_close_zone, NULL,
696 	    {16, 0x1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
697 	     0xff, 0, 0, 0xff, 0xff, 0x1, 0xc7} },	/* CLOSE ZONE */
698 	{0, 0x94, 0x2, DS_NO_SSC, F_SA_LOW | F_M_ACCESS, resp_finish_zone, NULL,
699 	    {16, 0x2, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
700 	     0xff, 0, 0, 0xff, 0xff, 0x1, 0xc7} },	/* FINISH ZONE */
701 	{0, 0x94, 0x4, DS_NO_SSC, F_SA_LOW | F_M_ACCESS, resp_rwp_zone, NULL,
702 	    {16, 0x4, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
703 	     0xff, 0, 0, 0xff, 0xff, 0x1, 0xc7} },  /* RESET WRITE POINTER */
704 };
705 
706 static const struct opcode_info_t zone_in_iarr[] = {	/* ZONE IN(16) */
707 	{0, 0x95, 0x6, DS_NO_SSC, F_SA_LOW | F_D_IN | F_M_ACCESS, NULL, NULL,
708 	    {16, 0x6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
709 	     0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* REPORT ZONES */
710 };
711 
712 
713 /* This array is accessed via SDEB_I_* values. Make sure all are mapped,
714  * plus the terminating elements for logic that scans this table such as
715  * REPORT SUPPORTED OPERATION CODES. */
716 static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEM_P1 + 1] = {
717 /* 0 */
718 	{0, 0, 0, DS_ALL, F_INV_OP | FF_RESPOND, NULL, NULL,	/* unknown opcodes */
719 	    {0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
720 	{0, 0x12, 0, DS_ALL, FF_RESPOND | F_D_IN, resp_inquiry, NULL, /* INQUIRY */
721 	    {6,  0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
722 	{0, 0xa0, 0, DS_ALL, FF_RESPOND | F_D_IN, resp_report_luns, NULL,
723 	    {12,  0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
724 	     0, 0} },					/* REPORT LUNS */
725 	{0, 0x3, 0, DS_ALL, FF_RESPOND | F_D_IN, resp_requests, NULL,
726 	    {6,  0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
727 	{0, 0x0, 0, DS_ALL, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */
728 	    {6,  0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
729 /* 5 */
730 	{ARRAY_SIZE(msense_iarr), 0x5a, 0, DS_ALL, F_D_IN,	/* MODE SENSE(10) */
731 	    resp_mode_sense, msense_iarr, {10,  0xf8, 0xff, 0xff, 0, 0, 0,
732 		0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
733 	{ARRAY_SIZE(mselect_iarr), 0x55, 0, DS_ALL, F_D_OUT,	/* MODE SELECT(10) */
734 	    resp_mode_select, mselect_iarr, {10,  0xf1, 0, 0, 0, 0, 0, 0xff,
735 		0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
736 	{0, 0x4d, 0, DS_NO_SSC, F_D_IN, resp_log_sense, NULL,	/* LOG SENSE */
737 	    {10,  0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0,
738 	     0, 0, 0} },
739 	{0, 0x25, 0, DS_NO_SSC, F_D_IN, resp_readcap, NULL,    /* READ CAPACITY(10) */
740 	    {10,  0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0,
741 	     0, 0} },
742 	{ARRAY_SIZE(read_iarr), 0x88, 0, DS_NO_SSC, F_D_IN | FF_MEDIA_IO, /* READ(16) */
743 	    resp_read_dt0, read_iarr, {16,  0xfe, 0xff, 0xff, 0xff, 0xff,
744 	    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} },
745 /* 10 */
746 	{ARRAY_SIZE(write_iarr), 0x8a, 0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO,
747 	    resp_write_dt0, write_iarr,			/* WRITE(16) */
748 		{16,  0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
749 		 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} },
750 	{0, 0x1b, 0, DS_ALL, F_SSU_DELAY, resp_start_stop, NULL,/* START STOP UNIT */
751 	    {6,  0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
752 	{ARRAY_SIZE(sa_in_16_iarr), 0x9e, 0x10, DS_NO_SSC, F_SA_LOW | F_D_IN,
753 	    resp_readcap16, sa_in_16_iarr, /* SA_IN(16), READ CAPACITY(16) */
754 		{16,  0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
755 		 0xff, 0xff, 0xff, 0xff, 0x1, 0xc7} },
756 	{0, 0x9f, 0x12, DS_NO_SSC, F_SA_LOW | F_D_OUT | FF_MEDIA_IO, resp_write_scat,
757 	    NULL, {16,  0x12, 0xf9, 0x0, 0xff, 0xff, 0, 0, 0xff, 0xff, 0xff,
758 	    0xff, 0xff, 0xff, 0xff, 0xc7} },  /* SA_OUT(16), WRITE SCAT(16) */
759 	{ARRAY_SIZE(maint_in_iarr), 0xa3, 0xa, DS_ALL, F_SA_LOW | F_D_IN,
760 	    resp_report_tgtpgs,	/* MAINT IN, REPORT TARGET PORT GROUPS */
761 		maint_in_iarr, {12,  0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff,
762 				0xff, 0, 0xc7, 0, 0, 0, 0} },
763 /* 15 */
764 	{0, 0, 0, DS_ALL, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */
765 	    {0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
766 	{ARRAY_SIZE(verify_iarr), 0x8f, 0, DS_NO_SSC,
767 	    F_D_OUT_MAYBE | FF_MEDIA_IO, resp_verify,	/* VERIFY(16) */
768 	    verify_iarr, {16,  0xf6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
769 			  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} },
770 	{ARRAY_SIZE(vl_iarr), 0x7f, 0x9, DS_NO_SSC, F_SA_HIGH | F_D_IN | FF_MEDIA_IO,
771 	    resp_read_dt0, vl_iarr,	/* VARIABLE LENGTH, READ(32) */
772 	    {32,  0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x9, 0xfe, 0, 0xff, 0xff,
773 	     0xff, 0xff} },
774 	{ARRAY_SIZE(reserve_iarr), 0x56, 0, DS_ALL, F_D_OUT,
775 	    NULL, reserve_iarr,	/* RESERVE(10) <no response function> */
776 	    {10,  0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
777 	     0} },
778 	{ARRAY_SIZE(release_iarr), 0x57, 0, DS_ALL, F_D_OUT,
779 	    NULL, release_iarr, /* RELEASE(10) <no response function> */
780 	    {10,  0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
781 	     0} },
782 /* 20 */
783 	{0, 0x1e, 0, DS_ALL, 0, NULL, NULL, /* ALLOW REMOVAL */
784 	    {6,  0, 0, 0, 0x3, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
785 	{0, 0x1, 0, DS_SSC, 0, resp_rewind, NULL,
786 	    {6,  0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
787 	{0, 0, 0, DS_NO_SSC, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */
788 	    {0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
789 	{0, 0x1d, 0, DS_ALL, F_D_OUT, NULL, NULL,      /* SEND DIAGNOSTIC */
790 	    {6,  0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
791 	{0, 0x42, 0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO, resp_unmap, NULL, /* UNMAP */
792 	    {10,  0x1, 0, 0, 0, 0, 0x3f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
793 /* 25 */
794 	{0, 0x3b, 0, DS_NO_SSC, F_D_OUT_MAYBE, resp_write_buffer, NULL,
795 	    {10,  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0,
796 	     0, 0, 0, 0} },			/* WRITE_BUFFER */
797 	{ARRAY_SIZE(write_same_iarr), 0x41, 0, DS_NO_SSC, F_D_OUT_MAYBE | FF_MEDIA_IO,
798 	    resp_write_same_10, write_same_iarr,	/* WRITE SAME(10) */
799 		{10,  0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0,
800 		 0, 0, 0, 0, 0} },
801 	{ARRAY_SIZE(sync_cache_iarr), 0x35, 0, DS_NO_SSC, F_SYNC_DELAY | F_M_ACCESS,
802 	    resp_sync_cache, sync_cache_iarr,
803 	    {10,  0x7, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
804 	     0, 0, 0, 0} },			/* SYNC_CACHE (10) */
805 	{0, 0x89, 0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO, resp_comp_write, NULL,
806 	    {16,  0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0,
807 	     0, 0xff, 0x3f, 0xc7} },		/* COMPARE AND WRITE */
808 	{ARRAY_SIZE(pre_fetch_iarr), 0x34, 0, DS_NO_SSC, F_SYNC_DELAY | FF_MEDIA_IO,
809 	    resp_pre_fetch, pre_fetch_iarr,
810 	    {10,  0x2, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
811 	     0, 0, 0, 0} },			/* PRE-FETCH (10) */
812 						/* READ POSITION (10) */
813 
814 /* 30 */
815 	{ARRAY_SIZE(zone_out_iarr), 0x94, 0x3, DS_NO_SSC, F_SA_LOW | F_M_ACCESS,
816 	    resp_open_zone, zone_out_iarr, /* ZONE_OUT(16), OPEN ZONE) */
817 		{16,  0x3 /* SA */, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
818 		 0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x1, 0xc7} },
819 	{ARRAY_SIZE(zone_in_iarr), 0x95, 0x0, DS_NO_SSC, F_SA_LOW | F_M_ACCESS,
820 	    resp_report_zones, zone_in_iarr, /* ZONE_IN(16), REPORT ZONES) */
821 		{16,  0x0 /* SA */, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
822 		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, 0xc7} },
823 /* 32 */
824 	{0, 0x9c, 0x0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO,
825 	    resp_atomic_write, NULL, /* ATOMIC WRITE 16 */
826 		{16,  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
827 		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} },
828 	{0, 0x05, 0, DS_SSC, F_D_IN, resp_read_blklimits, NULL,    /* READ BLOCK LIMITS (6) */
829 	    {6,  0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
830 	{0, 0x2b, 0, DS_SSC, F_D_UNKN, resp_locate, NULL,	   /* LOCATE (10) */
831 	    {10,  0x07, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xff, 0xc7, 0, 0,
832 	     0, 0, 0, 0} },
833 	{0, 0x10, 0, DS_SSC, F_D_IN, resp_write_filemarks, NULL,   /* WRITE FILEMARKS (6) */
834 	    {6,  0x01, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
835 	{0, 0x11, 0, DS_SSC, F_D_IN, resp_space, NULL,    /* SPACE (6) */
836 	    {6,  0x07, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
837 	{0, 0x4, 0, DS_SSC, 0, resp_format_medium, NULL,  /* FORMAT MEDIUM (6) */
838 	    {6,  0x3, 0x7, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
839 	{0, 0x19, 0, DS_SSC, F_D_IN, resp_erase, NULL,    /* ERASE (6) */
840 	    {6,  0x03, 0x33, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
841 /* 39 */
842 /* sentinel */
843 	{0xff, 0, 0, 0, 0, NULL, NULL,		/* terminating element */
844 	    {0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
845 };
846 
847 static int sdebug_num_hosts;
848 static int sdebug_add_host = DEF_NUM_HOST;  /* in sysfs this is relative */
849 static int sdebug_ato = DEF_ATO;
850 static int sdebug_cdb_len = DEF_CDB_LEN;
851 static int sdebug_jdelay = DEF_JDELAY;
852 static int sdebug_dev_size_mb = DEF_DEV_SIZE_PRE_INIT;
853 static int sdebug_dif = DEF_DIF;
854 static int sdebug_dix = DEF_DIX;
855 static int sdebug_dsense = DEF_D_SENSE;
856 static int sdebug_every_nth = DEF_EVERY_NTH;
857 static int sdebug_fake_rw = DEF_FAKE_RW;
858 static unsigned int sdebug_guard = DEF_GUARD;
859 static int sdebug_host_max_queue;	/* per host */
860 static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED;
861 static int sdebug_max_luns = DEF_MAX_LUNS;
862 static int sdebug_max_queue = SDEBUG_CANQUEUE;	/* per submit queue */
863 static unsigned int sdebug_medium_error_start = OPT_MEDIUM_ERR_ADDR;
864 static int sdebug_medium_error_count = OPT_MEDIUM_ERR_NUM;
865 static int sdebug_ndelay = DEF_NDELAY;	/* if > 0 then unit is nanoseconds */
866 static int sdebug_no_lun_0 = DEF_NO_LUN_0;
867 static int sdebug_no_uld;
868 static int sdebug_num_parts = DEF_NUM_PARTS;
869 static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */
870 static int sdebug_opt_blks = DEF_OPT_BLKS;
871 static int sdebug_opts = DEF_OPTS;
872 static int sdebug_physblk_exp = DEF_PHYSBLK_EXP;
873 static int sdebug_opt_xferlen_exp = DEF_OPT_XFERLEN_EXP;
874 static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral device type */
875 static int sdebug_scsi_level = DEF_SCSI_LEVEL;
876 static int sdebug_sector_size = DEF_SECTOR_SIZE;
877 static int sdeb_tur_ms_to_ready = DEF_TUR_MS_TO_READY;
878 static int sdebug_virtual_gb = DEF_VIRTUAL_GB;
879 static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO;
880 static unsigned int sdebug_lbpu = DEF_LBPU;
881 static unsigned int sdebug_lbpws = DEF_LBPWS;
882 static unsigned int sdebug_lbpws10 = DEF_LBPWS10;
883 static unsigned int sdebug_lbprz = DEF_LBPRZ;
884 static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT;
885 static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY;
886 static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS;
887 static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC;
888 static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH;
889 static unsigned int sdebug_atomic_wr = DEF_ATOMIC_WR;
890 static unsigned int sdebug_atomic_wr_max_length = DEF_ATOMIC_WR_MAX_LENGTH;
891 static unsigned int sdebug_atomic_wr_align = DEF_ATOMIC_WR_ALIGN;
892 static unsigned int sdebug_atomic_wr_gran = DEF_ATOMIC_WR_GRAN;
893 static unsigned int sdebug_atomic_wr_max_length_bndry =
894 			DEF_ATOMIC_WR_MAX_LENGTH_BNDRY;
895 static unsigned int sdebug_atomic_wr_max_bndry = DEF_ATOMIC_WR_MAX_BNDRY;
896 static int sdebug_uuid_ctl = DEF_UUID_CTL;
897 static bool sdebug_random = DEF_RANDOM;
898 static bool sdebug_per_host_store = DEF_PER_HOST_STORE;
899 static bool sdebug_removable = DEF_REMOVABLE;
900 static bool sdebug_clustering = DEF_CLUSTERING;
901 static bool sdebug_host_lock = DEF_HOST_LOCK;
902 static bool sdebug_strict = DEF_STRICT;
903 static bool sdebug_no_rwlock;
904 static bool sdebug_verbose;
905 static bool have_dif_prot;
906 static bool write_since_sync;
907 static bool sdebug_statistics = DEF_STATISTICS;
908 static bool sdebug_wp;
909 static bool sdebug_allow_restart;
910 static enum {
911 	BLK_ZONED_NONE	= 0,
912 	BLK_ZONED_HA	= 1,
913 	BLK_ZONED_HM	= 2,
914 } sdeb_zbc_model = BLK_ZONED_NONE;
915 static char *sdeb_zbc_model_s;
916 
917 enum sam_lun_addr_method {SAM_LUN_AM_PERIPHERAL = 0x0,
918 			  SAM_LUN_AM_FLAT = 0x1,
919 			  SAM_LUN_AM_LOGICAL_UNIT = 0x2,
920 			  SAM_LUN_AM_EXTENDED = 0x3};
921 static enum sam_lun_addr_method sdebug_lun_am = SAM_LUN_AM_PERIPHERAL;
922 static int sdebug_lun_am_i = (int)SAM_LUN_AM_PERIPHERAL;
923 
924 static unsigned int sdebug_store_sectors;
925 static sector_t sdebug_capacity;	/* in sectors */
926 
927 /* old BIOS stuff, kernel may get rid of them but some mode sense pages
928    may still need them */
929 static int sdebug_heads;		/* heads per disk */
930 static int sdebug_cylinders_per;	/* cylinders per surface */
931 static int sdebug_sectors_per;		/* sectors per cylinder */
932 
933 static LIST_HEAD(sdebug_host_list);
934 static DEFINE_MUTEX(sdebug_host_list_mutex);
935 
936 static struct xarray per_store_arr;
937 static struct xarray *per_store_ap = &per_store_arr;
938 static int sdeb_first_idx = -1;		/* invalid index ==> none created */
939 static int sdeb_most_recent_idx = -1;
940 static DEFINE_RWLOCK(sdeb_fake_rw_lck);	/* need a RW lock when fake_rw=1 */
941 
942 static unsigned long map_size;
943 static int num_aborts;
944 static int num_dev_resets;
945 static int num_target_resets;
946 static int num_bus_resets;
947 static int num_host_resets;
948 static int dix_writes;
949 static int dix_reads;
950 static int dif_errors;
951 
952 /* ZBC global data */
953 static bool sdeb_zbc_in_use;	/* true for host-aware and host-managed disks */
954 static int sdeb_zbc_zone_cap_mb;
955 static int sdeb_zbc_zone_size_mb;
956 static int sdeb_zbc_max_open = DEF_ZBC_MAX_OPEN_ZONES;
957 static int sdeb_zbc_nr_conv = DEF_ZBC_NR_CONV_ZONES;
958 
959 static int submit_queues = DEF_SUBMIT_QUEUES;  /* > 1 for multi-queue (mq) */
960 static int poll_queues; /* iouring iopoll interface.*/
961 
962 static atomic_long_t writes_by_group_number[64];
963 
964 static char sdebug_proc_name[] = MY_NAME;
965 static const char *my_name = MY_NAME;
966 
967 static const struct bus_type pseudo_lld_bus;
968 
969 static struct device_driver sdebug_driverfs_driver = {
970 	.name 		= sdebug_proc_name,
971 	.bus		= &pseudo_lld_bus,
972 };
973 
974 static const int check_condition_result =
975 	SAM_STAT_CHECK_CONDITION;
976 
977 static const int illegal_condition_result =
978 	(DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
979 
980 static const int device_qfull_result =
981 	(DID_ABORT << 16) | SAM_STAT_TASK_SET_FULL;
982 
983 static const int condition_met_result = SAM_STAT_CONDITION_MET;
984 
985 static struct dentry *sdebug_debugfs_root;
986 static ASYNC_DOMAIN_EXCLUSIVE(sdebug_async_domain);
987 
988 static u32 sdebug_get_devsel(struct scsi_device *sdp)
989 {
990 	unsigned char devtype = sdp->type;
991 	u32 devsel;
992 
993 	if (devtype < 32)
994 		devsel = (1 << devtype);
995 	else
996 		devsel = DS_ALL;
997 
998 	return devsel;
999 }
1000 
1001 static void sdebug_err_free(struct rcu_head *head)
1002 {
1003 	struct sdebug_err_inject *inject =
1004 		container_of(head, typeof(*inject), rcu);
1005 
1006 	kfree(inject);
1007 }
1008 
1009 static void sdebug_err_add(struct scsi_device *sdev, struct sdebug_err_inject *new)
1010 {
1011 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdev->hostdata;
1012 	struct sdebug_err_inject *err;
1013 
1014 	spin_lock(&devip->list_lock);
1015 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
1016 		if (err->type == new->type && err->cmd == new->cmd) {
1017 			list_del_rcu(&err->list);
1018 			call_rcu(&err->rcu, sdebug_err_free);
1019 		}
1020 	}
1021 
1022 	list_add_tail_rcu(&new->list, &devip->inject_err_list);
1023 	spin_unlock(&devip->list_lock);
1024 }
1025 
1026 static int sdebug_err_remove(struct scsi_device *sdev, const char *buf, size_t count)
1027 {
1028 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdev->hostdata;
1029 	struct sdebug_err_inject *err;
1030 	int type;
1031 	unsigned char cmd;
1032 
1033 	if (sscanf(buf, "- %d %hhx", &type, &cmd) != 2) {
1034 		kfree(buf);
1035 		return -EINVAL;
1036 	}
1037 
1038 	spin_lock(&devip->list_lock);
1039 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
1040 		if (err->type == type && err->cmd == cmd) {
1041 			list_del_rcu(&err->list);
1042 			call_rcu(&err->rcu, sdebug_err_free);
1043 			spin_unlock(&devip->list_lock);
1044 			kfree(buf);
1045 			return count;
1046 		}
1047 	}
1048 	spin_unlock(&devip->list_lock);
1049 
1050 	kfree(buf);
1051 	return -EINVAL;
1052 }
1053 
1054 static int sdebug_error_show(struct seq_file *m, void *p)
1055 {
1056 	struct scsi_device *sdev = (struct scsi_device *)m->private;
1057 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdev->hostdata;
1058 	struct sdebug_err_inject *err;
1059 
1060 	seq_puts(m, "Type\tCount\tCommand\n");
1061 
1062 	rcu_read_lock();
1063 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
1064 		switch (err->type) {
1065 		case ERR_TMOUT_CMD:
1066 		case ERR_ABORT_CMD_FAILED:
1067 		case ERR_LUN_RESET_FAILED:
1068 			seq_printf(m, "%d\t%d\t0x%x\n", err->type, err->cnt,
1069 				err->cmd);
1070 		break;
1071 
1072 		case ERR_FAIL_QUEUE_CMD:
1073 			seq_printf(m, "%d\t%d\t0x%x\t0x%x\n", err->type,
1074 				err->cnt, err->cmd, err->queuecmd_ret);
1075 		break;
1076 
1077 		case ERR_FAIL_CMD:
1078 			seq_printf(m, "%d\t%d\t0x%x\t0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
1079 				err->type, err->cnt, err->cmd,
1080 				err->host_byte, err->driver_byte,
1081 				err->status_byte, err->sense_key,
1082 				err->asc, err->asq);
1083 		break;
1084 		}
1085 	}
1086 	rcu_read_unlock();
1087 
1088 	return 0;
1089 }
1090 
1091 static int sdebug_error_open(struct inode *inode, struct file *file)
1092 {
1093 	return single_open(file, sdebug_error_show, inode->i_private);
1094 }
1095 
1096 static ssize_t sdebug_error_write(struct file *file, const char __user *ubuf,
1097 		size_t count, loff_t *ppos)
1098 {
1099 	char *buf;
1100 	unsigned int inject_type;
1101 	struct sdebug_err_inject *inject;
1102 	struct scsi_device *sdev = (struct scsi_device *)file->f_inode->i_private;
1103 
1104 	buf = memdup_user_nul(ubuf, count);
1105 	if (IS_ERR(buf))
1106 		return PTR_ERR(buf);
1107 
1108 	if (buf[0] == '-')
1109 		return sdebug_err_remove(sdev, buf, count);
1110 
1111 	if (sscanf(buf, "%d", &inject_type) != 1) {
1112 		kfree(buf);
1113 		return -EINVAL;
1114 	}
1115 
1116 	inject = kzalloc_obj(struct sdebug_err_inject);
1117 	if (!inject) {
1118 		kfree(buf);
1119 		return -ENOMEM;
1120 	}
1121 
1122 	switch (inject_type) {
1123 	case ERR_TMOUT_CMD:
1124 	case ERR_ABORT_CMD_FAILED:
1125 	case ERR_LUN_RESET_FAILED:
1126 		if (sscanf(buf, "%d %d %hhx", &inject->type, &inject->cnt,
1127 			   &inject->cmd) != 3)
1128 			goto out_error;
1129 	break;
1130 
1131 	case ERR_FAIL_QUEUE_CMD:
1132 		if (sscanf(buf, "%d %d %hhx %x", &inject->type, &inject->cnt,
1133 			   &inject->cmd, &inject->queuecmd_ret) != 4)
1134 			goto out_error;
1135 	break;
1136 
1137 	case ERR_FAIL_CMD:
1138 		if (sscanf(buf, "%d %d %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
1139 			   &inject->type, &inject->cnt, &inject->cmd,
1140 			   &inject->host_byte, &inject->driver_byte,
1141 			   &inject->status_byte, &inject->sense_key,
1142 			   &inject->asc, &inject->asq) != 9)
1143 			goto out_error;
1144 	break;
1145 
1146 	default:
1147 		goto out_error;
1148 	break;
1149 	}
1150 
1151 	kfree(buf);
1152 	sdebug_err_add(sdev, inject);
1153 
1154 	return count;
1155 
1156 out_error:
1157 	kfree(buf);
1158 	kfree(inject);
1159 	return -EINVAL;
1160 }
1161 
1162 static const struct file_operations sdebug_error_fops = {
1163 	.open	= sdebug_error_open,
1164 	.read	= seq_read,
1165 	.write	= sdebug_error_write,
1166 	.release = single_release,
1167 };
1168 
1169 enum corrupt_options {
1170 	Opt_lba			= (1u << 0),
1171 	Opt_num			= (1u << 1),
1172 	Opt_bit_errors		= (1u << 2),
1173 	Opt_reftag_adjust	= (1u << 3),
1174 
1175 	Opt_invalid,
1176 };
1177 
1178 static const match_table_t corrupt_tokens = {
1179 	{ Opt_lba,			"lba=%u"		},
1180 	{ Opt_num,			"num=%u"		},
1181 	{ Opt_bit_errors,		"bit_errors=%u"		},
1182 	{ Opt_reftag_adjust,		"reftag_adjust=%d"	},
1183 	{ Opt_invalid,			NULL,			},
1184 };
1185 
1186 static int corrupt_lbas(struct sdebug_dev_info *devip, u64 lba, u32 num,
1187 		u32 nr_bit_errors, s32 reftag_adjust);
1188 static ssize_t sdebug_corrupt_write(struct file *file, const char __user *ubuf,
1189 		size_t count, loff_t *ppos)
1190 {
1191 	struct scsi_device *sdev = file->f_inode->i_private;
1192 	struct sdebug_dev_info *devip = sdev->hostdata;
1193 	substring_t args[MAX_OPT_ARGS];
1194 	char *buf, *options, *p;
1195 	int error = 0;
1196 	u64 lba = 0;
1197 	u32 num = 1;
1198 	u32 nr_bit_errors = 0;
1199 	s32 reftag_adjust = 0;
1200 
1201 	buf = memdup_user_nul(ubuf, count);
1202 	if (IS_ERR(buf))
1203 		return PTR_ERR(buf);
1204 
1205 	options = buf;
1206 	while ((p = strsep(&options, ",\n")) != NULL) {
1207 		if (!*p)
1208 			continue;
1209 		switch (match_token(p, corrupt_tokens, args)) {
1210 		case Opt_lba:
1211 			error = match_u64(args, &lba);
1212 			break;
1213 		case Opt_num:
1214 			error = match_uint(args, &num);
1215 			break;
1216 		case Opt_bit_errors:
1217 			error = match_uint(args, &nr_bit_errors);
1218 			break;
1219 		case Opt_reftag_adjust:
1220 			error = match_int(args, &reftag_adjust);
1221 			break;
1222 		default:
1223 			pr_warn("unknown parameter or missing value '%s'\n", p);
1224 			error = -EINVAL;
1225 		}
1226 		if (error)
1227 			goto out_free_buf;
1228 	}
1229 
1230 	if (num == 0) {
1231 		pr_warn("invalid number of logical blocks: %u\n", num);
1232 		error = -EINVAL;
1233 		goto out_free_buf;
1234 	}
1235 
1236 	if (reftag_adjust &&
1237 	    (!sdebug_dix ||
1238 	     (sdebug_dif != T10_PI_TYPE1_PROTECTION &&
1239 	      sdebug_dif != T10_PI_TYPE2_PROTECTION))) {
1240 		pr_warn("reftag adjust requires type 1 or type 2 PI with DIX\n");
1241 		error = -EINVAL;
1242 		goto out_free_buf;
1243 	}
1244 
1245 	error = corrupt_lbas(devip, lba, num, nr_bit_errors, reftag_adjust);
1246 
1247 out_free_buf:
1248 	kfree(buf);
1249 	if (error)
1250 		return error;
1251 	return count;
1252 }
1253 
1254 static const struct file_operations sdebug_corrupt_fops = {
1255 	.write		= sdebug_corrupt_write,
1256 };
1257 
1258 static int sdebug_target_reset_fail_show(struct seq_file *m, void *p)
1259 {
1260 	struct scsi_target *starget = (struct scsi_target *)m->private;
1261 	struct sdebug_target_info *targetip =
1262 		(struct sdebug_target_info *)starget->hostdata;
1263 
1264 	if (targetip)
1265 		seq_printf(m, "%c\n", targetip->reset_fail ? 'Y' : 'N');
1266 
1267 	return 0;
1268 }
1269 
1270 static int sdebug_target_reset_fail_open(struct inode *inode, struct file *file)
1271 {
1272 	return single_open(file, sdebug_target_reset_fail_show, inode->i_private);
1273 }
1274 
1275 static ssize_t sdebug_target_reset_fail_write(struct file *file,
1276 		const char __user *ubuf, size_t count, loff_t *ppos)
1277 {
1278 	int ret;
1279 	struct scsi_target *starget =
1280 		(struct scsi_target *)file->f_inode->i_private;
1281 	struct sdebug_target_info *targetip =
1282 		(struct sdebug_target_info *)starget->hostdata;
1283 
1284 	if (targetip) {
1285 		ret = kstrtobool_from_user(ubuf, count, &targetip->reset_fail);
1286 		return ret < 0 ? ret : count;
1287 	}
1288 	return -ENODEV;
1289 }
1290 
1291 static const struct file_operations sdebug_target_reset_fail_fops = {
1292 	.open	= sdebug_target_reset_fail_open,
1293 	.read	= seq_read,
1294 	.write	= sdebug_target_reset_fail_write,
1295 	.release = single_release,
1296 };
1297 
1298 static int sdebug_target_alloc(struct scsi_target *starget)
1299 {
1300 	struct sdebug_target_info *targetip;
1301 
1302 	targetip = kzalloc_obj(struct sdebug_target_info);
1303 	if (!targetip)
1304 		return -ENOMEM;
1305 
1306 	async_synchronize_full_domain(&sdebug_async_domain);
1307 
1308 	targetip->debugfs_entry = debugfs_create_dir(dev_name(&starget->dev),
1309 				sdebug_debugfs_root);
1310 
1311 	debugfs_create_file("fail_reset", 0600, targetip->debugfs_entry, starget,
1312 				&sdebug_target_reset_fail_fops);
1313 
1314 	starget->hostdata = targetip;
1315 
1316 	return 0;
1317 }
1318 
1319 static void sdebug_tartget_cleanup_async(void *data, async_cookie_t cookie)
1320 {
1321 	struct sdebug_target_info *targetip = data;
1322 
1323 	debugfs_remove(targetip->debugfs_entry);
1324 	kfree(targetip);
1325 }
1326 
1327 static void sdebug_target_destroy(struct scsi_target *starget)
1328 {
1329 	struct sdebug_target_info *targetip;
1330 
1331 	targetip = (struct sdebug_target_info *)starget->hostdata;
1332 	if (targetip) {
1333 		starget->hostdata = NULL;
1334 		async_schedule_domain(sdebug_tartget_cleanup_async, targetip,
1335 				&sdebug_async_domain);
1336 	}
1337 }
1338 
1339 /* Only do the extra work involved in logical block provisioning if one or
1340  * more of the lbpu, lbpws or lbpws10 parameters are given and we are doing
1341  * real reads and writes (i.e. not skipping them for speed).
1342  */
1343 static inline bool scsi_debug_lbp(void)
1344 {
1345 	return 0 == sdebug_fake_rw &&
1346 		(sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10);
1347 }
1348 
1349 static inline bool scsi_debug_atomic_write(void)
1350 {
1351 	return sdebug_fake_rw == 0 && sdebug_atomic_wr;
1352 }
1353 
1354 static void *lba2fake_store(struct sdeb_store_info *sip,
1355 			    unsigned long long lba)
1356 {
1357 	struct sdeb_store_info *lsip = sip;
1358 
1359 	lba = do_div(lba, sdebug_store_sectors);
1360 	if (!sip || !sip->storep) {
1361 		WARN_ON_ONCE(true);
1362 		lsip = xa_load(per_store_ap, 0);  /* should never be NULL */
1363 	}
1364 	return lsip->storep + lba * sdebug_sector_size;
1365 }
1366 
1367 static struct t10_pi_tuple *dif_store(struct sdeb_store_info *sip,
1368 				      sector_t sector)
1369 {
1370 	sector = sector_div(sector, sdebug_store_sectors);
1371 
1372 	return sip->dif_storep + sector;
1373 }
1374 
1375 static void sdebug_max_tgts_luns(void)
1376 {
1377 	struct sdebug_host_info *sdbg_host;
1378 	struct Scsi_Host *hpnt;
1379 
1380 	mutex_lock(&sdebug_host_list_mutex);
1381 	list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
1382 		hpnt = sdbg_host->shost;
1383 		if ((hpnt->this_id >= 0) &&
1384 		    (sdebug_num_tgts > hpnt->this_id))
1385 			hpnt->max_id = sdebug_num_tgts + 1;
1386 		else
1387 			hpnt->max_id = sdebug_num_tgts;
1388 		/* sdebug_max_luns; */
1389 		hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
1390 	}
1391 	mutex_unlock(&sdebug_host_list_mutex);
1392 }
1393 
1394 enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1};
1395 
1396 /* Set in_bit to -1 to indicate no bit position of invalid field */
1397 static void mk_sense_invalid_fld(struct scsi_cmnd *scp,
1398 				 enum sdeb_cmd_data c_d,
1399 				 int in_byte, int in_bit)
1400 {
1401 	unsigned char *sbuff;
1402 	u8 sks[4];
1403 	int sl, asc;
1404 
1405 	sbuff = scp->sense_buffer;
1406 	if (!sbuff) {
1407 		sdev_printk(KERN_ERR, scp->device, "sense_buffer is NULL\n");
1408 		return;
1409 	}
1410 	if (c_d)
1411 		asc = ASC_INVALID_FIELD_IN_CDB;
1412 	else
1413 		asc = ASC_INVALID_FIELD_IN_PARAMETER_LIST;
1414 	memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
1415 	scsi_set_sense(scp, sdebug_dsense, ILLEGAL_REQUEST,
1416 		       scsi_sense_code(asc, 0));
1417 	memset(sks, 0, sizeof(sks));
1418 	sks[0] = 0x80;
1419 	if (c_d)
1420 		sks[0] |= 0x40;
1421 	if (in_bit >= 0) {
1422 		sks[0] |= 0x8;
1423 		sks[0] |= 0x7 & in_bit;
1424 	}
1425 	put_unaligned_be16(in_byte, sks + 1);
1426 	if (sdebug_dsense) {
1427 		sl = sbuff[7] + 8;
1428 		sbuff[7] = sl;
1429 		sbuff[sl] = 0x2;
1430 		sbuff[sl + 1] = 0x6;
1431 		memcpy(sbuff + sl + 4, sks, 3);
1432 	} else
1433 		memcpy(sbuff + 15, sks, 3);
1434 	if (sdebug_verbose)
1435 		sdev_printk(KERN_INFO, scp->device, "%s:  [sense_key,asc,ascq"
1436 			    "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n",
1437 			    my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit);
1438 }
1439 
1440 static void mk_sense_buffer(struct scsi_cmnd *scp, u8 key, u16 code)
1441 {
1442 	if (!scp->sense_buffer) {
1443 		sdev_printk(KERN_ERR, scp->device, "sense_buffer is NULL\n");
1444 		return;
1445 	}
1446 	memset(scp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1447 
1448 	scsi_set_sense(scp, sdebug_dsense, key, code);
1449 
1450 	if (sdebug_verbose)
1451 		sdev_printk(KERN_INFO, scp->device,
1452 			    "%s:  [sense_key,sense_code]: [0x%02x,0x%04x]\n",
1453 			    my_name, key, code);
1454 }
1455 
1456 /* Sense data that has information fields for tapes */
1457 static void mk_sense_info_tape(struct scsi_cmnd *scp, u8 key, u16 code,
1458 			unsigned int information, unsigned char tape_flags)
1459 {
1460 	if (!scp->sense_buffer) {
1461 		sdev_printk(KERN_ERR, scp->device, "sense_buffer is NULL\n");
1462 		return;
1463 	}
1464 	memset(scp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1465 
1466 	scsi_set_sense(scp, /* sdebug_dsense */ 0, key, code);
1467 	/* only fixed format so far */
1468 
1469 	scp->sense_buffer[0] |= 0x80; /* valid */
1470 	scp->sense_buffer[2] |= tape_flags;
1471 	put_unaligned_be32(information, &scp->sense_buffer[3]);
1472 
1473 	if (sdebug_verbose)
1474 		sdev_printk(KERN_INFO, scp->device,
1475 			    "%s:  [sense_key,sense_code]: [0x%02x,0x%04x]\n",
1476 			    my_name, key, code);
1477 }
1478 
1479 static void mk_sense_invalid_opcode(struct scsi_cmnd *scp)
1480 {
1481 	mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_COMMAND_OP_CODE);
1482 }
1483 
1484 static int scsi_debug_ioctl(struct scsi_device *dev, unsigned int cmd,
1485 			    void __user *arg)
1486 {
1487 	if (sdebug_verbose) {
1488 		if (0x1261 == cmd)
1489 			sdev_printk(KERN_INFO, dev, "BLKFLSBUF [0x1261]\n");
1490 		else if (0x5331 == cmd)
1491 			sdev_printk(KERN_INFO, dev,
1492 				    "CDROM_GET_CAPABILITY [0x5331]\n");
1493 		else
1494 			sdev_printk(KERN_INFO, dev, "cmd=0x%x\n", cmd);
1495 	}
1496 	return -EINVAL;
1497 	/* return -ENOTTY; // correct return but upsets fdisk */
1498 }
1499 
1500 static void config_cdb_len(struct scsi_device *sdev)
1501 {
1502 	switch (sdebug_cdb_len) {
1503 	case 6:	/* suggest 6 byte READ, WRITE and MODE SENSE/SELECT */
1504 		sdev->use_10_for_rw = false;
1505 		sdev->use_16_for_rw = false;
1506 		sdev->use_10_for_ms = false;
1507 		break;
1508 	case 10: /* suggest 10 byte RWs and 6 byte MODE SENSE/SELECT */
1509 		sdev->use_10_for_rw = true;
1510 		sdev->use_16_for_rw = false;
1511 		sdev->use_10_for_ms = false;
1512 		break;
1513 	case 12: /* suggest 10 byte RWs and 10 byte MODE SENSE/SELECT */
1514 		sdev->use_10_for_rw = true;
1515 		sdev->use_16_for_rw = false;
1516 		sdev->use_10_for_ms = true;
1517 		break;
1518 	case 16:
1519 		sdev->use_10_for_rw = false;
1520 		sdev->use_16_for_rw = true;
1521 		sdev->use_10_for_ms = true;
1522 		break;
1523 	case 32: /* No knobs to suggest this so same as 16 for now */
1524 		sdev->use_10_for_rw = false;
1525 		sdev->use_16_for_rw = true;
1526 		sdev->use_10_for_ms = true;
1527 		break;
1528 	default:
1529 		pr_warn("unexpected cdb_len=%d, force to 10\n",
1530 			sdebug_cdb_len);
1531 		sdev->use_10_for_rw = true;
1532 		sdev->use_16_for_rw = false;
1533 		sdev->use_10_for_ms = false;
1534 		sdebug_cdb_len = 10;
1535 		break;
1536 	}
1537 }
1538 
1539 static void all_config_cdb_len(void)
1540 {
1541 	struct sdebug_host_info *sdbg_host;
1542 	struct Scsi_Host *shost;
1543 	struct scsi_device *sdev;
1544 
1545 	mutex_lock(&sdebug_host_list_mutex);
1546 	list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
1547 		shost = sdbg_host->shost;
1548 		shost_for_each_device(sdev, shost) {
1549 			config_cdb_len(sdev);
1550 		}
1551 	}
1552 	mutex_unlock(&sdebug_host_list_mutex);
1553 }
1554 
1555 static void clear_luns_changed_on_target(struct sdebug_dev_info *devip)
1556 {
1557 	struct sdebug_host_info *sdhp = devip->sdbg_host;
1558 	struct sdebug_dev_info *dp;
1559 
1560 	list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) {
1561 		if ((devip->sdbg_host == dp->sdbg_host) &&
1562 		    (devip->target == dp->target)) {
1563 			clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm);
1564 		}
1565 	}
1566 }
1567 
1568 static int make_ua(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
1569 {
1570 	int k;
1571 
1572 	k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS);
1573 	if (k != SDEBUG_NUM_UAS) {
1574 		const char *cp = NULL;
1575 
1576 		switch (k) {
1577 		case SDEBUG_UA_POR:
1578 			mk_sense_buffer(scp, UNIT_ATTENTION,
1579 				POWER_ON_RESET_OR_BUS_DEVICE_RESET_OCCURRED);
1580 			if (sdebug_verbose)
1581 				cp = "power on reset";
1582 			break;
1583 		case SDEBUG_UA_POOCCUR:
1584 			mk_sense_buffer(scp, UNIT_ATTENTION, POWER_ON_OCCURRED);
1585 			if (sdebug_verbose)
1586 				cp = "power on occurred";
1587 			break;
1588 		case SDEBUG_UA_BUS_RESET:
1589 			mk_sense_buffer(scp, UNIT_ATTENTION,
1590 					SCSI_BUS_RESET_OCCURRED);
1591 			if (sdebug_verbose)
1592 				cp = "bus reset";
1593 			break;
1594 		case SDEBUG_UA_MODE_CHANGED:
1595 			mk_sense_buffer(scp, UNIT_ATTENTION,
1596 					MODE_PARAMETERS_CHANGED);
1597 			if (sdebug_verbose)
1598 				cp = "mode parameters changed";
1599 			break;
1600 		case SDEBUG_UA_CAPACITY_CHANGED:
1601 			mk_sense_buffer(scp, UNIT_ATTENTION,
1602 					CAPACITY_DATA_HAS_CHANGED);
1603 			if (sdebug_verbose)
1604 				cp = "capacity data changed";
1605 			break;
1606 		case SDEBUG_UA_MICROCODE_CHANGED:
1607 			mk_sense_buffer(scp, UNIT_ATTENTION,
1608 					MICROCODE_HAS_BEEN_CHANGED);
1609 			if (sdebug_verbose)
1610 				cp = "microcode has been changed";
1611 			break;
1612 		case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET:
1613 			mk_sense_buffer(scp, UNIT_ATTENTION,
1614 				MICROCODE_HAS_BEEN_CHANGED_WITHOUT_RESET);
1615 			if (sdebug_verbose)
1616 				cp = "microcode has been changed without reset";
1617 			break;
1618 		case SDEBUG_UA_LUNS_CHANGED:
1619 			/*
1620 			 * SPC-3 behavior is to report a UNIT ATTENTION with
1621 			 * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN
1622 			 * on the target, until a REPORT LUNS command is
1623 			 * received.  SPC-4 behavior is to report it only once.
1624 			 * NOTE:  sdebug_scsi_level does not use the same
1625 			 * values as struct scsi_device->scsi_level.
1626 			 */
1627 			if (sdebug_scsi_level >= 6)	/* SPC-4 and above */
1628 				clear_luns_changed_on_target(devip);
1629 			mk_sense_buffer(scp, UNIT_ATTENTION,
1630 					REPORTED_LUNS_DATA_HAS_CHANGED);
1631 			if (sdebug_verbose)
1632 				cp = "reported luns data has changed";
1633 			break;
1634 		case SDEBUG_UA_NOT_READY_TO_READY:
1635 			mk_sense_buffer(scp, UNIT_ATTENTION,
1636 				NOT_READY_TO_READY_CHANGE_MEDIUM_MAY_HAVE_CHANGED);
1637 			if (sdebug_verbose)
1638 				cp = "not ready to ready transition/media change";
1639 			break;
1640 		default:
1641 			pr_warn("unexpected unit attention code=%d\n", k);
1642 			if (sdebug_verbose)
1643 				cp = "unknown";
1644 			break;
1645 		}
1646 		clear_bit(k, devip->uas_bm);
1647 		if (sdebug_verbose)
1648 			sdev_printk(KERN_INFO, scp->device,
1649 				   "%s reports: Unit attention: %s\n",
1650 				   my_name, cp);
1651 		return check_condition_result;
1652 	}
1653 	return 0;
1654 }
1655 
1656 /* Build SCSI "data-in" buffer. Returns 0 if ok else (DID_ERROR << 16). */
1657 static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
1658 				int arr_len)
1659 {
1660 	int act_len;
1661 	struct scsi_data_buffer *sdb = &scp->sdb;
1662 
1663 	if (!sdb->length)
1664 		return 0;
1665 	if (scp->sc_data_direction != DMA_FROM_DEVICE)
1666 		return DID_ERROR << 16;
1667 
1668 	act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents,
1669 				      arr, arr_len);
1670 	scsi_set_resid(scp, scsi_bufflen(scp) - act_len);
1671 
1672 	return 0;
1673 }
1674 
1675 /* Partial build of SCSI "data-in" buffer. Returns 0 if ok else
1676  * (DID_ERROR << 16). Can write to offset in data-in buffer. If multiple
1677  * calls, not required to write in ascending offset order. Assumes resid
1678  * set to scsi_bufflen() prior to any calls.
1679  */
1680 static int p_fill_from_dev_buffer(struct scsi_cmnd *scp, const void *arr,
1681 				  int arr_len, unsigned int off_dst)
1682 {
1683 	unsigned int act_len, n;
1684 	struct scsi_data_buffer *sdb = &scp->sdb;
1685 	off_t skip = off_dst;
1686 
1687 	if (sdb->length <= off_dst)
1688 		return 0;
1689 	if (scp->sc_data_direction != DMA_FROM_DEVICE)
1690 		return DID_ERROR << 16;
1691 
1692 	act_len = sg_pcopy_from_buffer(sdb->table.sgl, sdb->table.nents,
1693 				       arr, arr_len, skip);
1694 	pr_debug("off_dst=%u, scsi_bufflen=%u, act_len=%u, resid=%d\n",
1695 		 off_dst, scsi_bufflen(scp), act_len,
1696 		 scsi_get_resid(scp));
1697 	n = scsi_bufflen(scp) - (off_dst + act_len);
1698 	scsi_set_resid(scp, min_t(u32, scsi_get_resid(scp), n));
1699 	return 0;
1700 }
1701 
1702 /* Fetches from SCSI "data-out" buffer. Returns number of bytes fetched into
1703  * 'arr' or -1 if error.
1704  */
1705 static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
1706 			       int arr_len)
1707 {
1708 	if (!scsi_bufflen(scp))
1709 		return 0;
1710 	if (scp->sc_data_direction != DMA_TO_DEVICE)
1711 		return -1;
1712 
1713 	return scsi_sg_copy_to_buffer(scp, arr, arr_len);
1714 }
1715 
1716 
1717 static char sdebug_inq_vendor_id[9] = "Linux   ";
1718 static char sdebug_inq_product_id[17] = "scsi_debug      ";
1719 static char sdebug_inq_product_rev[5] = SDEBUG_VERSION;
1720 /* Use some locally assigned NAAs for SAS addresses. */
1721 static const u64 naa3_comp_a = 0x3222222000000000ULL;
1722 static const u64 naa3_comp_b = 0x3333333000000000ULL;
1723 static const u64 naa3_comp_c = 0x3111111000000000ULL;
1724 
1725 /* Device identification VPD page. Returns number of bytes placed in arr */
1726 static int inquiry_vpd_83(unsigned char *arr, int port_group_id,
1727 			  int target_dev_id, int dev_id_num,
1728 			  const char *dev_id_str, int dev_id_str_len,
1729 			  const uuid_t *lu_name)
1730 {
1731 	int num, port_a;
1732 	char b[32];
1733 
1734 	port_a = target_dev_id + 1;
1735 	/* T10 vendor identifier field format (faked) */
1736 	arr[0] = 0x2;	/* ASCII */
1737 	arr[1] = 0x1;
1738 	arr[2] = 0x0;
1739 	memcpy(&arr[4], sdebug_inq_vendor_id, 8);
1740 	memcpy(&arr[12], sdebug_inq_product_id, 16);
1741 	memcpy(&arr[28], dev_id_str, dev_id_str_len);
1742 	num = 8 + 16 + dev_id_str_len;
1743 	arr[3] = num;
1744 	num += 4;
1745 	if (dev_id_num >= 0) {
1746 		if (sdebug_uuid_ctl) {
1747 			/* Locally assigned UUID */
1748 			arr[num++] = 0x1;  /* binary (not necessarily sas) */
1749 			arr[num++] = 0xa;  /* PIV=0, lu, naa */
1750 			arr[num++] = 0x0;
1751 			arr[num++] = 0x12;
1752 			arr[num++] = 0x10; /* uuid type=1, locally assigned */
1753 			arr[num++] = 0x0;
1754 			memcpy(arr + num, lu_name, 16);
1755 			num += 16;
1756 		} else {
1757 			/* NAA-3, Logical unit identifier (binary) */
1758 			arr[num++] = 0x1;  /* binary (not necessarily sas) */
1759 			arr[num++] = 0x3;  /* PIV=0, lu, naa */
1760 			arr[num++] = 0x0;
1761 			arr[num++] = 0x8;
1762 			put_unaligned_be64(naa3_comp_b + dev_id_num, arr + num);
1763 			num += 8;
1764 		}
1765 		/* Target relative port number */
1766 		arr[num++] = 0x61;	/* proto=sas, binary */
1767 		arr[num++] = 0x94;	/* PIV=1, target port, rel port */
1768 		arr[num++] = 0x0;	/* reserved */
1769 		arr[num++] = 0x4;	/* length */
1770 		arr[num++] = 0x0;	/* reserved */
1771 		arr[num++] = 0x0;	/* reserved */
1772 		arr[num++] = 0x0;
1773 		arr[num++] = 0x1;	/* relative port A */
1774 	}
1775 	/* NAA-3, Target port identifier */
1776 	arr[num++] = 0x61;	/* proto=sas, binary */
1777 	arr[num++] = 0x93;	/* piv=1, target port, naa */
1778 	arr[num++] = 0x0;
1779 	arr[num++] = 0x8;
1780 	put_unaligned_be64(naa3_comp_a + port_a, arr + num);
1781 	num += 8;
1782 	/* NAA-3, Target port group identifier */
1783 	arr[num++] = 0x61;	/* proto=sas, binary */
1784 	arr[num++] = 0x95;	/* piv=1, target port group id */
1785 	arr[num++] = 0x0;
1786 	arr[num++] = 0x4;
1787 	arr[num++] = 0;
1788 	arr[num++] = 0;
1789 	put_unaligned_be16(port_group_id, arr + num);
1790 	num += 2;
1791 	/* NAA-3, Target device identifier */
1792 	arr[num++] = 0x61;	/* proto=sas, binary */
1793 	arr[num++] = 0xa3;	/* piv=1, target device, naa */
1794 	arr[num++] = 0x0;
1795 	arr[num++] = 0x8;
1796 	put_unaligned_be64(naa3_comp_a + target_dev_id, arr + num);
1797 	num += 8;
1798 	/* SCSI name string: Target device identifier */
1799 	arr[num++] = 0x63;	/* proto=sas, UTF-8 */
1800 	arr[num++] = 0xa8;	/* piv=1, target device, SCSI name string */
1801 	arr[num++] = 0x0;
1802 	arr[num++] = 24;
1803 	memcpy(arr + num, "naa.32222220", 12);
1804 	num += 12;
1805 	snprintf(b, sizeof(b), "%08X", target_dev_id);
1806 	memcpy(arr + num, b, 8);
1807 	num += 8;
1808 	memset(arr + num, 0, 4);
1809 	num += 4;
1810 	return num;
1811 }
1812 
1813 static unsigned char vpd84_data[] = {
1814 /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
1815     0x22,0x22,0x22,0x0,0xbb,0x1,
1816     0x22,0x22,0x22,0x0,0xbb,0x2,
1817 };
1818 
1819 /*  Software interface identification VPD page */
1820 static int inquiry_vpd_84(unsigned char *arr)
1821 {
1822 	memcpy(arr, vpd84_data, sizeof(vpd84_data));
1823 	return sizeof(vpd84_data);
1824 }
1825 
1826 /* Management network addresses VPD page */
1827 static int inquiry_vpd_85(unsigned char *arr)
1828 {
1829 	int num = 0;
1830 	const char *na1 = "https://www.kernel.org/config";
1831 	const char *na2 = "http://www.kernel.org/log";
1832 	int plen, olen;
1833 
1834 	arr[num++] = 0x1;	/* lu, storage config */
1835 	arr[num++] = 0x0;	/* reserved */
1836 	arr[num++] = 0x0;
1837 	olen = strlen(na1);
1838 	plen = olen + 1;
1839 	if (plen % 4)
1840 		plen = ((plen / 4) + 1) * 4;
1841 	arr[num++] = plen;	/* length, null termianted, padded */
1842 	memcpy(arr + num, na1, olen);
1843 	memset(arr + num + olen, 0, plen - olen);
1844 	num += plen;
1845 
1846 	arr[num++] = 0x4;	/* lu, logging */
1847 	arr[num++] = 0x0;	/* reserved */
1848 	arr[num++] = 0x0;
1849 	olen = strlen(na2);
1850 	plen = olen + 1;
1851 	if (plen % 4)
1852 		plen = ((plen / 4) + 1) * 4;
1853 	arr[num++] = plen;	/* length, null terminated, padded */
1854 	memcpy(arr + num, na2, olen);
1855 	memset(arr + num + olen, 0, plen - olen);
1856 	num += plen;
1857 
1858 	return num;
1859 }
1860 
1861 /* SCSI ports VPD page */
1862 static int inquiry_vpd_88(unsigned char *arr, int target_dev_id)
1863 {
1864 	int num = 0;
1865 	int port_a, port_b;
1866 
1867 	port_a = target_dev_id + 1;
1868 	port_b = port_a + 1;
1869 	arr[num++] = 0x0;	/* reserved */
1870 	arr[num++] = 0x0;	/* reserved */
1871 	arr[num++] = 0x0;
1872 	arr[num++] = 0x1;	/* relative port 1 (primary) */
1873 	memset(arr + num, 0, 6);
1874 	num += 6;
1875 	arr[num++] = 0x0;
1876 	arr[num++] = 12;	/* length tp descriptor */
1877 	/* naa-5 target port identifier (A) */
1878 	arr[num++] = 0x61;	/* proto=sas, binary */
1879 	arr[num++] = 0x93;	/* PIV=1, target port, NAA */
1880 	arr[num++] = 0x0;	/* reserved */
1881 	arr[num++] = 0x8;	/* length */
1882 	put_unaligned_be64(naa3_comp_a + port_a, arr + num);
1883 	num += 8;
1884 	arr[num++] = 0x0;	/* reserved */
1885 	arr[num++] = 0x0;	/* reserved */
1886 	arr[num++] = 0x0;
1887 	arr[num++] = 0x2;	/* relative port 2 (secondary) */
1888 	memset(arr + num, 0, 6);
1889 	num += 6;
1890 	arr[num++] = 0x0;
1891 	arr[num++] = 12;	/* length tp descriptor */
1892 	/* naa-5 target port identifier (B) */
1893 	arr[num++] = 0x61;	/* proto=sas, binary */
1894 	arr[num++] = 0x93;	/* PIV=1, target port, NAA */
1895 	arr[num++] = 0x0;	/* reserved */
1896 	arr[num++] = 0x8;	/* length */
1897 	put_unaligned_be64(naa3_comp_a + port_b, arr + num);
1898 	num += 8;
1899 
1900 	return num;
1901 }
1902 
1903 
1904 static unsigned char vpd89_data[] = {
1905 /* from 4th byte */ 0,0,0,0,
1906 'l','i','n','u','x',' ',' ',' ',
1907 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
1908 '1','2','3','4',
1909 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
1910 0xec,0,0,0,
1911 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
1912 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
1913 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
1914 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
1915 0x53,0x41,
1916 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
1917 0x20,0x20,
1918 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
1919 0x10,0x80,
1920 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
1921 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
1922 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
1923 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
1924 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
1925 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
1926 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0,
1927 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1928 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1929 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1930 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
1931 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
1932 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
1933 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0,
1934 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1935 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1936 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1937 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1938 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1939 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1940 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1941 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1942 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1943 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1944 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1945 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
1946 };
1947 
1948 /* ATA Information VPD page */
1949 static int inquiry_vpd_89(unsigned char *arr)
1950 {
1951 	memcpy(arr, vpd89_data, sizeof(vpd89_data));
1952 	return sizeof(vpd89_data);
1953 }
1954 
1955 
1956 static unsigned char vpdb0_data[] = {
1957 	/* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64,
1958 	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1959 	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1960 	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1961 };
1962 
1963 /* Block limits VPD page (SBC-3) */
1964 static int inquiry_vpd_b0(unsigned char *arr)
1965 {
1966 	unsigned int gran;
1967 
1968 	memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
1969 
1970 	/* Optimal transfer length granularity */
1971 	if (sdebug_opt_xferlen_exp != 0 &&
1972 	    sdebug_physblk_exp < sdebug_opt_xferlen_exp)
1973 		gran = 1 << sdebug_opt_xferlen_exp;
1974 	else
1975 		gran = 1 << sdebug_physblk_exp;
1976 	put_unaligned_be16(gran, arr + 2);
1977 
1978 	/* Maximum Transfer Length */
1979 	if (sdebug_store_sectors > 0x400)
1980 		put_unaligned_be32(sdebug_store_sectors, arr + 4);
1981 
1982 	/* Optimal Transfer Length */
1983 	put_unaligned_be32(sdebug_opt_blks, &arr[8]);
1984 
1985 	if (sdebug_lbpu) {
1986 		/* Maximum Unmap LBA Count */
1987 		put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]);
1988 
1989 		/* Maximum Unmap Block Descriptor Count */
1990 		put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]);
1991 	}
1992 
1993 	/* Unmap Granularity Alignment */
1994 	if (sdebug_unmap_alignment) {
1995 		put_unaligned_be32(sdebug_unmap_alignment, &arr[28]);
1996 		arr[28] |= 0x80; /* UGAVALID */
1997 	}
1998 
1999 	/* Optimal Unmap Granularity */
2000 	put_unaligned_be32(sdebug_unmap_granularity, &arr[24]);
2001 
2002 	/* Maximum WRITE SAME Length */
2003 	put_unaligned_be64(sdebug_write_same_length, &arr[32]);
2004 
2005 	if (sdebug_atomic_wr) {
2006 		put_unaligned_be32(sdebug_atomic_wr_max_length, &arr[40]);
2007 		put_unaligned_be32(sdebug_atomic_wr_align, &arr[44]);
2008 		put_unaligned_be32(sdebug_atomic_wr_gran, &arr[48]);
2009 		put_unaligned_be32(sdebug_atomic_wr_max_length_bndry, &arr[52]);
2010 		put_unaligned_be32(sdebug_atomic_wr_max_bndry, &arr[56]);
2011 	}
2012 
2013 	return 0x3c; /* Mandatory page length for Logical Block Provisioning */
2014 }
2015 
2016 /* Block device characteristics VPD page (SBC-3) */
2017 static int inquiry_vpd_b1(struct sdebug_dev_info *devip, unsigned char *arr)
2018 {
2019 	memset(arr, 0, 0x3c);
2020 	arr[0] = 0;
2021 	arr[1] = 1;	/* non rotating medium (e.g. solid state) */
2022 	arr[2] = 0;
2023 	arr[3] = 5;	/* less than 1.8" */
2024 
2025 	return 0x3c;
2026 }
2027 
2028 /* Logical block provisioning VPD page (SBC-4) */
2029 static int inquiry_vpd_b2(unsigned char *arr)
2030 {
2031 	memset(arr, 0, 0x4);
2032 	arr[0] = 0;			/* threshold exponent */
2033 	if (sdebug_lbpu)
2034 		arr[1] = 1 << 7;
2035 	if (sdebug_lbpws)
2036 		arr[1] |= 1 << 6;
2037 	if (sdebug_lbpws10)
2038 		arr[1] |= 1 << 5;
2039 	if (sdebug_lbprz && scsi_debug_lbp())
2040 		arr[1] |= (sdebug_lbprz & 0x7) << 2;  /* sbc4r07 and later */
2041 	/* anc_sup=0; dp=0 (no provisioning group descriptor) */
2042 	/* minimum_percentage=0; provisioning_type=0 (unknown) */
2043 	/* threshold_percentage=0 */
2044 	return 0x4;
2045 }
2046 
2047 /* Zoned block device characteristics VPD page (ZBC mandatory) */
2048 static int inquiry_vpd_b6(struct sdebug_dev_info *devip, unsigned char *arr)
2049 {
2050 	memset(arr, 0, 0x3c);
2051 	arr[0] = 0x1; /* set URSWRZ (unrestricted read in seq. wr req zone) */
2052 	/*
2053 	 * Set Optimal number of open sequential write preferred zones and
2054 	 * Optimal number of non-sequentially written sequential write
2055 	 * preferred zones fields to 'not reported' (0xffffffff). Leave other
2056 	 * fields set to zero, apart from Max. number of open swrz_s field.
2057 	 */
2058 	put_unaligned_be32(0xffffffff, &arr[4]);
2059 	put_unaligned_be32(0xffffffff, &arr[8]);
2060 	if (sdeb_zbc_model == BLK_ZONED_HM && devip->max_open)
2061 		put_unaligned_be32(devip->max_open, &arr[12]);
2062 	else
2063 		put_unaligned_be32(0xffffffff, &arr[12]);
2064 	if (devip->zcap < devip->zsize) {
2065 		arr[19] = ZBC_CONSTANT_ZONE_START_OFFSET;
2066 		put_unaligned_be64(devip->zsize, &arr[20]);
2067 	} else {
2068 		arr[19] = 0;
2069 	}
2070 	return 0x3c;
2071 }
2072 
2073 #define SDEBUG_BLE_LEN_AFTER_B4 28	/* thus vpage 32 bytes long */
2074 
2075 enum { MAXIMUM_NUMBER_OF_STREAMS = 6, PERMANENT_STREAM_COUNT = 5 };
2076 
2077 /* Block limits extension VPD page (SBC-4) */
2078 static int inquiry_vpd_b7(unsigned char *arrb4)
2079 {
2080 	memset(arrb4, 0, SDEBUG_BLE_LEN_AFTER_B4);
2081 	arrb4[1] = 1; /* Reduced stream control support (RSCS) */
2082 	put_unaligned_be16(MAXIMUM_NUMBER_OF_STREAMS, &arrb4[2]);
2083 	return SDEBUG_BLE_LEN_AFTER_B4;
2084 }
2085 
2086 #define SDEBUG_LONG_INQ_SZ 96
2087 #define SDEBUG_MAX_INQ_ARR_SZ 584
2088 
2089 static int resp_inquiry(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
2090 {
2091 	unsigned char pq_pdt;
2092 	unsigned char *arr;
2093 	unsigned char *cmd = scp->cmnd;
2094 	u32 alloc_len, n;
2095 	int ret;
2096 	bool have_wlun, is_disk, is_zbc, is_disk_zbc, is_tape;
2097 
2098 	alloc_len = get_unaligned_be16(cmd + 3);
2099 	arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC);
2100 	if (! arr)
2101 		return DID_REQUEUE << 16;
2102 	if (scp->device->type >= 32) {
2103 		is_disk = (sdebug_ptype == TYPE_DISK);
2104 		is_tape = (sdebug_ptype == TYPE_TAPE);
2105 	} else {
2106 		is_disk = (scp->device->type == TYPE_DISK);
2107 		is_tape = (scp->device->type == TYPE_TAPE);
2108 	}
2109 	is_zbc = devip->zoned;
2110 	is_disk_zbc = (is_disk || is_zbc);
2111 	have_wlun = scsi_is_wlun(scp->device->lun);
2112 	if (have_wlun)
2113 		pq_pdt = TYPE_WLUN;	/* present, wlun */
2114 	else if (sdebug_no_lun_0 && (devip->lun == SDEBUG_LUN_0_VAL))
2115 		pq_pdt = 0x7f;	/* not present, PQ=3, PDT=0x1f */
2116 	else
2117 		pq_pdt = ((scp->device->type >= 32 ?
2118 				sdebug_ptype : scp->device->type) & 0x1f);
2119 	arr[0] = pq_pdt;
2120 	if (0x2 & cmd[1]) {  /* CMDDT bit set */
2121 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1);
2122 		kfree(arr);
2123 		return check_condition_result;
2124 	} else if (0x1 & cmd[1]) {  /* EVPD bit set */
2125 		int lu_id_num, port_group_id, target_dev_id;
2126 		u32 len;
2127 		char lu_id_str[6];
2128 		int host_no = devip->sdbg_host->shost->host_no;
2129 
2130 		arr[1] = cmd[2];
2131 		port_group_id = (((host_no + 1) & 0x7f) << 8) +
2132 		    (devip->channel & 0x7f);
2133 		if (sdebug_vpd_use_hostno == 0)
2134 			host_no = 0;
2135 		lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) +
2136 			    (devip->target * 1000) + devip->lun);
2137 		target_dev_id = ((host_no + 1) * 2000) +
2138 				 (devip->target * 1000) - 3;
2139 		len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
2140 		if (0 == cmd[2]) { /* supported vital product data pages */
2141 			n = 4;
2142 			arr[n++] = 0x0;   /* this page */
2143 			arr[n++] = 0x80;  /* unit serial number */
2144 			arr[n++] = 0x83;  /* device identification */
2145 			arr[n++] = 0x84;  /* software interface ident. */
2146 			arr[n++] = 0x85;  /* management network addresses */
2147 			arr[n++] = 0x86;  /* extended inquiry */
2148 			arr[n++] = 0x87;  /* mode page policy */
2149 			arr[n++] = 0x88;  /* SCSI ports */
2150 			if (is_disk_zbc) {	  /* SBC or ZBC */
2151 				arr[n++] = 0x89;  /* ATA information */
2152 				arr[n++] = 0xb0;  /* Block limits */
2153 				arr[n++] = 0xb1;  /* Block characteristics */
2154 				if (is_disk)
2155 					arr[n++] = 0xb2;  /* LB Provisioning */
2156 				if (is_zbc)
2157 					arr[n++] = 0xb6;  /* ZB dev. char. */
2158 				arr[n++] = 0xb7;  /* Block limits extension */
2159 			}
2160 			arr[3] = n - 4;	  /* number of supported VPD pages */
2161 		} else if (0x80 == cmd[2]) { /* unit serial number */
2162 			arr[3] = len;
2163 			memcpy(&arr[4], lu_id_str, len);
2164 		} else if (0x83 == cmd[2]) { /* device identification */
2165 			arr[3] = inquiry_vpd_83(&arr[4], port_group_id,
2166 						target_dev_id, lu_id_num,
2167 						lu_id_str, len,
2168 						&devip->lu_name);
2169 		} else if (0x84 == cmd[2]) { /* Software interface ident. */
2170 			arr[3] = inquiry_vpd_84(&arr[4]);
2171 		} else if (0x85 == cmd[2]) { /* Management network addresses */
2172 			arr[3] = inquiry_vpd_85(&arr[4]);
2173 		} else if (0x86 == cmd[2]) { /* extended inquiry */
2174 			arr[3] = 0x3c;	/* number of following entries */
2175 			if (sdebug_dif == T10_PI_TYPE3_PROTECTION)
2176 				arr[4] = 0x4;	/* SPT: GRD_CHK:1 */
2177 			else if (have_dif_prot)
2178 				arr[4] = 0x5;   /* SPT: GRD_CHK:1, REF_CHK:1 */
2179 			else
2180 				arr[4] = 0x0;   /* no protection stuff */
2181 			/*
2182 			 * GROUP_SUP=1; HEADSUP=1 (HEAD OF QUEUE); ORDSUP=1
2183 			 * (ORDERED queuing); SIMPSUP=1 (SIMPLE queuing).
2184 			 */
2185 			arr[5] = 0x17;
2186 		} else if (0x87 == cmd[2]) { /* mode page policy */
2187 			arr[3] = 0x8;	/* number of following entries */
2188 			arr[4] = 0x2;	/* disconnect-reconnect mp */
2189 			arr[6] = 0x80;	/* mlus, shared */
2190 			arr[8] = 0x18;	 /* protocol specific lu */
2191 			arr[10] = 0x82;	 /* mlus, per initiator port */
2192 		} else if (0x88 == cmd[2]) { /* SCSI Ports */
2193 			arr[3] = inquiry_vpd_88(&arr[4], target_dev_id);
2194 		} else if (is_disk_zbc && 0x89 == cmd[2]) { /* ATA info */
2195 			n = inquiry_vpd_89(&arr[4]);
2196 			put_unaligned_be16(n, arr + 2);
2197 		} else if (is_disk_zbc && 0xb0 == cmd[2]) { /* Block limits */
2198 			arr[3] = inquiry_vpd_b0(&arr[4]);
2199 		} else if (is_disk_zbc && 0xb1 == cmd[2]) { /* Block char. */
2200 			arr[3] = inquiry_vpd_b1(devip, &arr[4]);
2201 		} else if (is_disk && 0xb2 == cmd[2]) { /* LB Prov. */
2202 			arr[3] = inquiry_vpd_b2(&arr[4]);
2203 		} else if (is_zbc && cmd[2] == 0xb6) { /* ZB dev. charact. */
2204 			arr[3] = inquiry_vpd_b6(devip, &arr[4]);
2205 		} else if (cmd[2] == 0xb7) { /* block limits extension page */
2206 			arr[3] = inquiry_vpd_b7(&arr[4]);
2207 		} else {
2208 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
2209 			kfree(arr);
2210 			return check_condition_result;
2211 		}
2212 		len = min_t(u32, get_unaligned_be16(arr + 2) + 4, alloc_len);
2213 		ret = fill_from_dev_buffer(scp, arr,
2214 			    min_t(u32, len, SDEBUG_MAX_INQ_ARR_SZ));
2215 		kfree(arr);
2216 		return ret;
2217 	}
2218 	/* drops through here for a standard inquiry */
2219 	arr[1] = sdebug_removable ? 0x80 : 0;	/* Removable disk */
2220 	arr[2] = sdebug_scsi_level;
2221 	arr[3] = 2;    /* response_data_format==2 */
2222 	arr[4] = SDEBUG_LONG_INQ_SZ - 5;
2223 	arr[5] = (int)have_dif_prot;	/* PROTECT bit */
2224 	if (sdebug_vpd_use_hostno == 0)
2225 		arr[5] |= 0x10; /* claim: implicit TPGS */
2226 	arr[6] = 0x10; /* claim: MultiP */
2227 	/* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
2228 	arr[7] = 0xa; /* claim: LINKED + CMDQUE */
2229 	memcpy(&arr[8], sdebug_inq_vendor_id, 8);
2230 	memcpy(&arr[16], sdebug_inq_product_id, 16);
2231 	memcpy(&arr[32], sdebug_inq_product_rev, 4);
2232 	/* Use Vendor Specific area to place driver date in ASCII hex */
2233 	memcpy(&arr[36], sdebug_version_date, 8);
2234 	/* version descriptors (2 bytes each) follow */
2235 	put_unaligned_be16(0xc0, arr + 58);   /* SAM-6 no version claimed */
2236 	put_unaligned_be16(0x5c0, arr + 60);  /* SPC-5 no version claimed */
2237 	n = 62;
2238 	if (is_disk) {		/* SBC-4 no version claimed */
2239 		put_unaligned_be16(0x600, arr + n);
2240 		n += 2;
2241 	} else if (is_tape) {	/* SSC-4 rev 3 */
2242 		put_unaligned_be16(0x525, arr + n);
2243 		n += 2;
2244 	} else if (is_zbc) {	/* ZBC BSR INCITS 536 revision 05 */
2245 		put_unaligned_be16(0x624, arr + n);
2246 		n += 2;
2247 	}
2248 	put_unaligned_be16(0x2100, arr + n);	/* SPL-4 no version claimed */
2249 	ret = fill_from_dev_buffer(scp, arr,
2250 			    min_t(u32, alloc_len, SDEBUG_LONG_INQ_SZ));
2251 	kfree(arr);
2252 	return ret;
2253 }
2254 
2255 /* See resp_iec_m_pg() for how this data is manipulated */
2256 static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
2257 				   0, 0, 0x0, 0x0};
2258 
2259 static int resp_requests(struct scsi_cmnd *scp,
2260 			 struct sdebug_dev_info *devip)
2261 {
2262 	unsigned char *cmd = scp->cmnd;
2263 	unsigned char arr[SCSI_SENSE_BUFFERSIZE];	/* assume >= 18 bytes */
2264 	bool dsense = !!(cmd[1] & 1);
2265 	u32 alloc_len = cmd[4];
2266 	u32 len = 18;
2267 	int stopped_state = atomic_read(&devip->stopped);
2268 
2269 	memset(arr, 0, sizeof(arr));
2270 	if (stopped_state > 0) {	/* some "pollable" data [spc6r02: 5.12.2] */
2271 		if (dsense) {
2272 			arr[0] = 0x72;
2273 			arr[1] = NOT_READY;
2274 			arr[2] = ASC_LU_NOT_READY;
2275 			arr[3] = (stopped_state == 2) ? 0x1 : 0x2;
2276 			len = 8;
2277 		} else {
2278 			arr[0] = 0x70;
2279 			arr[2] = NOT_READY;		/* NO_SENSE in sense_key */
2280 			arr[7] = 0xa;			/* 18 byte sense buffer */
2281 			arr[12] = ASC_LU_NOT_READY;
2282 			arr[13] = (stopped_state == 2) ? 0x1 : 0x2;
2283 		}
2284 	} else if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
2285 		/* Information exceptions control mode page: TEST=1, MRIE=6 */
2286 		if (dsense) {
2287 			arr[0] = 0x72;
2288 			arr[1] = 0x0;		/* NO_SENSE in sense_key */
2289 			arr[2] = ASC_FAILURE_PREDICTION_THRESHOLD_EXCEEDED;
2290 			arr[3] = 0xff;		/* Failure prediction(false) */
2291 			len = 8;
2292 		} else {
2293 			arr[0] = 0x70;
2294 			arr[2] = 0x0;		/* NO_SENSE in sense_key */
2295 			arr[7] = 0xa;   	/* 18 byte sense buffer */
2296 			arr[12] = ASC_FAILURE_PREDICTION_THRESHOLD_EXCEEDED;
2297 			arr[13] = 0xff;		/* Failure prediction(false) */
2298 		}
2299 	} else {	/* nothing to report */
2300 		if (dsense) {
2301 			len = 8;
2302 			memset(arr, 0, len);
2303 			arr[0] = 0x72;
2304 		} else {
2305 			memset(arr, 0, len);
2306 			arr[0] = 0x70;
2307 			arr[7] = 0xa;
2308 		}
2309 	}
2310 	return fill_from_dev_buffer(scp, arr, min_t(u32, len, alloc_len));
2311 }
2312 
2313 static int resp_start_stop(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
2314 {
2315 	unsigned char *cmd = scp->cmnd;
2316 	int power_cond, want_stop, stopped_state;
2317 	bool changing;
2318 
2319 	power_cond = (cmd[4] & 0xf0) >> 4;
2320 	if (power_cond) {
2321 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7);
2322 		return check_condition_result;
2323 	}
2324 	want_stop = !(cmd[4] & 1);
2325 	stopped_state = atomic_read(&devip->stopped);
2326 	if (stopped_state == 2) {
2327 		ktime_t now_ts = ktime_get_boottime();
2328 
2329 		if (ktime_to_ns(now_ts) > ktime_to_ns(devip->create_ts)) {
2330 			u64 diff_ns = ktime_to_ns(ktime_sub(now_ts, devip->create_ts));
2331 
2332 			if (diff_ns >= ((u64)sdeb_tur_ms_to_ready * 1000000)) {
2333 				/* tur_ms_to_ready timer extinguished */
2334 				atomic_set(&devip->stopped, 0);
2335 				stopped_state = 0;
2336 			}
2337 		}
2338 		if (stopped_state == 2) {
2339 			if (want_stop) {
2340 				stopped_state = 1;	/* dummy up success */
2341 			} else {	/* Disallow tur_ms_to_ready delay to be overridden */
2342 				mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 0 /* START bit */);
2343 				return check_condition_result;
2344 			}
2345 		}
2346 	}
2347 	changing = (stopped_state != want_stop);
2348 	if (changing)
2349 		atomic_xchg(&devip->stopped, want_stop);
2350 	if (scp->device->type == TYPE_TAPE && !want_stop) {
2351 		int i;
2352 
2353 		set_bit(SDEBUG_UA_NOT_READY_TO_READY, devip->uas_bm); /* not legal! */
2354 		for (i = 0; i < TAPE_MAX_PARTITIONS; i++)
2355 			devip->tape_location[i] = 0;
2356 		devip->tape_partition = 0;
2357 	}
2358 	if (!changing || (cmd[1] & 0x1))  /* state unchanged or IMMED bit set in cdb */
2359 		return SDEG_RES_IMMED_MASK;
2360 	else
2361 		return 0;
2362 }
2363 
2364 static sector_t get_sdebug_capacity(void)
2365 {
2366 	static const unsigned int gibibyte = 1073741824;
2367 
2368 	if (sdebug_virtual_gb > 0)
2369 		return (sector_t)sdebug_virtual_gb *
2370 			(gibibyte / sdebug_sector_size);
2371 	else
2372 		return sdebug_store_sectors;
2373 }
2374 
2375 #define SDEBUG_READCAP_ARR_SZ 8
2376 static int resp_readcap(struct scsi_cmnd *scp,
2377 			struct sdebug_dev_info *devip)
2378 {
2379 	unsigned char arr[SDEBUG_READCAP_ARR_SZ];
2380 	unsigned int capac;
2381 
2382 	/* following just in case virtual_gb changed */
2383 	sdebug_capacity = get_sdebug_capacity();
2384 	memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
2385 	if (sdebug_capacity < 0xffffffff) {
2386 		capac = (unsigned int)sdebug_capacity - 1;
2387 		put_unaligned_be32(capac, arr + 0);
2388 	} else
2389 		put_unaligned_be32(0xffffffff, arr + 0);
2390 	put_unaligned_be16(sdebug_sector_size, arr + 6);
2391 	return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
2392 }
2393 
2394 #define SDEBUG_READCAP16_ARR_SZ 32
2395 static int resp_readcap16(struct scsi_cmnd *scp,
2396 			  struct sdebug_dev_info *devip)
2397 {
2398 	unsigned char *cmd = scp->cmnd;
2399 	unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
2400 	u32 alloc_len;
2401 
2402 	alloc_len = get_unaligned_be32(cmd + 10);
2403 	/* following just in case virtual_gb changed */
2404 	sdebug_capacity = get_sdebug_capacity();
2405 	memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
2406 	put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0);
2407 	put_unaligned_be32(sdebug_sector_size, arr + 8);
2408 	arr[13] = sdebug_physblk_exp & 0xf;
2409 	arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f;
2410 
2411 	if (scsi_debug_lbp()) {
2412 		arr[14] |= 0x80; /* LBPME */
2413 		/* from sbc4r07, this LBPRZ field is 1 bit, but the LBPRZ in
2414 		 * the LB Provisioning VPD page is 3 bits. Note that lbprz=2
2415 		 * in the wider field maps to 0 in this field.
2416 		 */
2417 		if (sdebug_lbprz & 1)	/* precisely what the draft requires */
2418 			arr[14] |= 0x40;
2419 	}
2420 
2421 	/*
2422 	 * Since the scsi_debug READ CAPACITY implementation always reports the
2423 	 * total disk capacity, set RC BASIS = 1 for host-managed ZBC devices.
2424 	 */
2425 	if (devip->zoned)
2426 		arr[12] |= 1 << 4;
2427 
2428 	arr[15] = sdebug_lowest_aligned & 0xff;
2429 
2430 	if (have_dif_prot) {
2431 		arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */
2432 		arr[12] |= 1; /* PROT_EN */
2433 	}
2434 
2435 	return fill_from_dev_buffer(scp, arr,
2436 			    min_t(u32, alloc_len, SDEBUG_READCAP16_ARR_SZ));
2437 }
2438 
2439 #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412
2440 
2441 static int resp_report_tgtpgs(struct scsi_cmnd *scp,
2442 			      struct sdebug_dev_info *devip)
2443 {
2444 	unsigned char *cmd = scp->cmnd;
2445 	unsigned char *arr;
2446 	int host_no = devip->sdbg_host->shost->host_no;
2447 	int port_group_a, port_group_b, port_a, port_b;
2448 	u32 alen, n, rlen;
2449 	int ret;
2450 
2451 	alen = get_unaligned_be32(cmd + 6);
2452 	arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC);
2453 	if (! arr)
2454 		return DID_REQUEUE << 16;
2455 	/*
2456 	 * EVPD page 0x88 states we have two ports, one
2457 	 * real and a fake port with no device connected.
2458 	 * So we create two port groups with one port each
2459 	 * and set the group with port B to unavailable.
2460 	 */
2461 	port_a = 0x1; /* relative port A */
2462 	port_b = 0x2; /* relative port B */
2463 	port_group_a = (((host_no + 1) & 0x7f) << 8) +
2464 			(devip->channel & 0x7f);
2465 	port_group_b = (((host_no + 1) & 0x7f) << 8) +
2466 			(devip->channel & 0x7f) + 0x80;
2467 
2468 	/*
2469 	 * The asymmetric access state is cycled according to the host_id.
2470 	 */
2471 	n = 4;
2472 	if (sdebug_vpd_use_hostno == 0) {
2473 		arr[n++] = host_no % 3; /* Asymm access state */
2474 		arr[n++] = 0x0F; /* claim: all states are supported */
2475 	} else {
2476 		arr[n++] = 0x0; /* Active/Optimized path */
2477 		arr[n++] = 0x01; /* only support active/optimized paths */
2478 	}
2479 	put_unaligned_be16(port_group_a, arr + n);
2480 	n += 2;
2481 	arr[n++] = 0;    /* Reserved */
2482 	arr[n++] = 0;    /* Status code */
2483 	arr[n++] = 0;    /* Vendor unique */
2484 	arr[n++] = 0x1;  /* One port per group */
2485 	arr[n++] = 0;    /* Reserved */
2486 	arr[n++] = 0;    /* Reserved */
2487 	put_unaligned_be16(port_a, arr + n);
2488 	n += 2;
2489 	arr[n++] = 3;    /* Port unavailable */
2490 	arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */
2491 	put_unaligned_be16(port_group_b, arr + n);
2492 	n += 2;
2493 	arr[n++] = 0;    /* Reserved */
2494 	arr[n++] = 0;    /* Status code */
2495 	arr[n++] = 0;    /* Vendor unique */
2496 	arr[n++] = 0x1;  /* One port per group */
2497 	arr[n++] = 0;    /* Reserved */
2498 	arr[n++] = 0;    /* Reserved */
2499 	put_unaligned_be16(port_b, arr + n);
2500 	n += 2;
2501 
2502 	rlen = n - 4;
2503 	put_unaligned_be32(rlen, arr + 0);
2504 
2505 	/*
2506 	 * Return the smallest value of either
2507 	 * - The allocated length
2508 	 * - The constructed command length
2509 	 * - The maximum array size
2510 	 */
2511 	rlen = min(alen, n);
2512 	ret = fill_from_dev_buffer(scp, arr,
2513 			   min_t(u32, rlen, SDEBUG_MAX_TGTPGS_ARR_SZ));
2514 	kfree(arr);
2515 	return ret;
2516 }
2517 
2518 static int resp_rsup_opcodes(struct scsi_cmnd *scp,
2519 			     struct sdebug_dev_info *devip)
2520 {
2521 	bool rctd;
2522 	u8 reporting_opts, req_opcode, sdeb_i, supp;
2523 	u16 req_sa, u;
2524 	u32 alloc_len, a_len;
2525 	int k, offset, len, errsts, bump, na;
2526 	const struct opcode_info_t *oip;
2527 	const struct opcode_info_t *r_oip;
2528 	u8 *arr;
2529 	u8 *cmd = scp->cmnd;
2530 	u32 devsel = sdebug_get_devsel(scp->device);
2531 
2532 	rctd = !!(cmd[2] & 0x80);
2533 	reporting_opts = cmd[2] & 0x7;
2534 	req_opcode = cmd[3];
2535 	req_sa = get_unaligned_be16(cmd + 4);
2536 	alloc_len = get_unaligned_be32(cmd + 6);
2537 	if (alloc_len < 4 || alloc_len > 0xffff) {
2538 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
2539 		return check_condition_result;
2540 	}
2541 	if (alloc_len > 8192)
2542 		a_len = 8192;
2543 	else
2544 		a_len = alloc_len;
2545 	arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC);
2546 	if (NULL == arr) {
2547 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFFICIENT_RESOURCES);
2548 		return check_condition_result;
2549 	}
2550 	switch (reporting_opts) {
2551 	case 0:	/* all commands */
2552 		bump = rctd ? 20 : 8;
2553 		for (offset = 4, oip = opcode_info_arr;
2554 		     oip->num_attached != 0xff && offset < a_len; ++oip) {
2555 			if (F_INV_OP & oip->flags)
2556 				continue;
2557 			if ((devsel & oip->devsel) != 0) {
2558 				arr[offset] = oip->opcode;
2559 				put_unaligned_be16(oip->sa, arr + offset + 2);
2560 				if (rctd)
2561 					arr[offset + 5] |= 0x2;
2562 				if (FF_SA & oip->flags)
2563 					arr[offset + 5] |= 0x1;
2564 				put_unaligned_be16(oip->len_mask[0], arr + offset + 6);
2565 				if (rctd)
2566 					put_unaligned_be16(0xa, arr + offset + 8);
2567 				offset += bump;
2568 			}
2569 			na = oip->num_attached;
2570 			r_oip = oip;
2571 			for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) {
2572 				if (F_INV_OP & oip->flags)
2573 					continue;
2574 				if ((devsel & oip->devsel) == 0)
2575 					continue;
2576 				arr[offset] = oip->opcode;
2577 				put_unaligned_be16(oip->sa, arr + offset + 2);
2578 				if (rctd)
2579 					arr[offset + 5] |= 0x2;
2580 				if (FF_SA & oip->flags)
2581 					arr[offset + 5] |= 0x1;
2582 				put_unaligned_be16(oip->len_mask[0],
2583 						arr + offset + 6);
2584 				if (rctd)
2585 					put_unaligned_be16(0xa,
2586 							   arr + offset + 8);
2587 				offset += bump;
2588 			}
2589 			oip = r_oip;
2590 		}
2591 		put_unaligned_be32(offset - 4, arr);
2592 		break;
2593 	case 1:	/* one command: opcode only */
2594 	case 2:	/* one command: opcode plus service action */
2595 	case 3:	/* one command: if sa==0 then opcode only else opcode+sa */
2596 		sdeb_i = opcode_ind_arr[req_opcode];
2597 		oip = &opcode_info_arr[sdeb_i];
2598 		if (F_INV_OP & oip->flags) {
2599 			supp = 1;
2600 			offset = 4;
2601 		} else {
2602 			if (1 == reporting_opts) {
2603 				if (FF_SA & oip->flags) {
2604 					mk_sense_invalid_fld(scp, SDEB_IN_CDB,
2605 							     2, 2);
2606 					kfree(arr);
2607 					return check_condition_result;
2608 				}
2609 				req_sa = 0;
2610 			} else if (2 == reporting_opts &&
2611 				   0 == (FF_SA & oip->flags)) {
2612 				mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1);
2613 				kfree(arr);	/* point at requested sa */
2614 				return check_condition_result;
2615 			}
2616 			if (0 == (FF_SA & oip->flags) &&
2617 				(devsel & oip->devsel) != 0 &&
2618 				req_opcode == oip->opcode)
2619 				supp = 3;
2620 			else if (0 == (FF_SA & oip->flags)) {
2621 				na = oip->num_attached;
2622 				for (k = 0, oip = oip->arrp; k < na;
2623 				     ++k, ++oip) {
2624 					if (req_opcode == oip->opcode &&
2625 						(devsel & oip->devsel) != 0)
2626 						break;
2627 				}
2628 				supp = (k >= na) ? 1 : 3;
2629 			} else if (req_sa != oip->sa) {
2630 				na = oip->num_attached;
2631 				for (k = 0, oip = oip->arrp; k < na;
2632 				     ++k, ++oip) {
2633 					if (req_sa == oip->sa &&
2634 						(devsel & oip->devsel) != 0)
2635 						break;
2636 				}
2637 				supp = (k >= na) ? 1 : 3;
2638 			} else
2639 				supp = 3;
2640 			if (3 == supp) {
2641 				u = oip->len_mask[0];
2642 				put_unaligned_be16(u, arr + 2);
2643 				arr[4] = oip->opcode;
2644 				for (k = 1; k < u; ++k)
2645 					arr[4 + k] = (k < 16) ?
2646 						 oip->len_mask[k] : 0xff;
2647 				offset = 4 + u;
2648 			} else
2649 				offset = 4;
2650 		}
2651 		arr[1] = (rctd ? 0x80 : 0) | supp;
2652 		if (rctd) {
2653 			put_unaligned_be16(0xa, arr + offset);
2654 			offset += 12;
2655 		}
2656 		break;
2657 	default:
2658 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2);
2659 		kfree(arr);
2660 		return check_condition_result;
2661 	}
2662 	offset = (offset < a_len) ? offset : a_len;
2663 	len = (offset < alloc_len) ? offset : alloc_len;
2664 	errsts = fill_from_dev_buffer(scp, arr, len);
2665 	kfree(arr);
2666 	return errsts;
2667 }
2668 
2669 static int resp_rsup_tmfs(struct scsi_cmnd *scp,
2670 			  struct sdebug_dev_info *devip)
2671 {
2672 	bool repd;
2673 	u32 alloc_len, len;
2674 	u8 arr[16];
2675 	u8 *cmd = scp->cmnd;
2676 
2677 	memset(arr, 0, sizeof(arr));
2678 	repd = !!(cmd[2] & 0x80);
2679 	alloc_len = get_unaligned_be32(cmd + 6);
2680 	if (alloc_len < 4) {
2681 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
2682 		return check_condition_result;
2683 	}
2684 	arr[0] = 0xc8;		/* ATS | ATSS | LURS */
2685 	arr[1] = 0x1;		/* ITNRS */
2686 	if (repd) {
2687 		arr[3] = 0xc;
2688 		len = 16;
2689 	} else
2690 		len = 4;
2691 
2692 	len = (len < alloc_len) ? len : alloc_len;
2693 	return fill_from_dev_buffer(scp, arr, len);
2694 }
2695 
2696 /* <<Following mode page info copied from ST318451LW>> */
2697 
2698 static int resp_err_recov_pg(unsigned char *p, int pcontrol, int target)
2699 {	/* Read-Write Error Recovery page for mode_sense */
2700 	static const unsigned char err_recov_pg[] = {
2701 		0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
2702 		5, 0, 0xff, 0xff
2703 	};
2704 
2705 	memcpy(p, err_recov_pg, sizeof(err_recov_pg));
2706 	if (1 == pcontrol)
2707 		memset(p + 2, 0, sizeof(err_recov_pg) - 2);
2708 	return sizeof(err_recov_pg);
2709 }
2710 
2711 static int resp_disconnect_pg(unsigned char *p, int pcontrol, int target)
2712 { 	/* Disconnect-Reconnect page for mode_sense */
2713 	static const unsigned char disconnect_pg[] = {
2714 		0x2, 0xe, 128, 128, 0, 10, 0, 0,
2715 		0, 0, 0, 0, 0, 0, 0, 0
2716 	};
2717 
2718 	memcpy(p, disconnect_pg, sizeof(disconnect_pg));
2719 	if (1 == pcontrol)
2720 		memset(p + 2, 0, sizeof(disconnect_pg) - 2);
2721 	return sizeof(disconnect_pg);
2722 }
2723 
2724 static int resp_format_pg(unsigned char *p, int pcontrol, int target)
2725 {       /* Format device page for mode_sense */
2726 	static const unsigned char format_pg[] = {
2727 		0x3, 0x16, 0, 0, 0, 0, 0, 0,
2728 		0, 0, 0, 0, 0, 0, 0, 0,
2729 		0, 0, 0, 0, 0x40, 0, 0, 0
2730 	};
2731 
2732 	memcpy(p, format_pg, sizeof(format_pg));
2733 	put_unaligned_be16(sdebug_sectors_per, p + 10);
2734 	put_unaligned_be16(sdebug_sector_size, p + 12);
2735 	if (sdebug_removable)
2736 		p[20] |= 0x20; /* should agree with INQUIRY */
2737 	if (1 == pcontrol)
2738 		memset(p + 2, 0, sizeof(format_pg) - 2);
2739 	return sizeof(format_pg);
2740 }
2741 
2742 static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
2743 				     0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0,
2744 				     0, 0, 0, 0};
2745 
2746 static int resp_caching_pg(unsigned char *p, int pcontrol, int target)
2747 { 	/* Caching page for mode_sense */
2748 	static const unsigned char ch_caching_pg[] = {
2749 		/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0,
2750 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2751 	};
2752 	static const unsigned char d_caching_pg[] = {
2753 		0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
2754 		0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0
2755 	};
2756 
2757 	if (SDEBUG_OPT_N_WCE & sdebug_opts)
2758 		caching_pg[2] &= ~0x4;	/* set WCE=0 (default WCE=1) */
2759 	memcpy(p, caching_pg, sizeof(caching_pg));
2760 	if (1 == pcontrol)
2761 		memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg));
2762 	else if (2 == pcontrol)
2763 		memcpy(p, d_caching_pg, sizeof(d_caching_pg));
2764 	return sizeof(caching_pg);
2765 }
2766 
2767 static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
2768 				    0, 0, 0x2, 0x4b};
2769 
2770 static int resp_ctrl_m_pg(unsigned char *p, int pcontrol, int target)
2771 { 	/* Control mode page for mode_sense */
2772 	unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
2773 					0, 0, 0, 0};
2774 	static const unsigned char d_ctrl_m_pg[] = {
2775 		0xa, 10, 2, 0, 0, 0, 0, 0,
2776 		0, 0, 0x2, 0x4b
2777 	};
2778 
2779 	if (sdebug_dsense)
2780 		ctrl_m_pg[2] |= 0x4;
2781 	else
2782 		ctrl_m_pg[2] &= ~0x4;
2783 
2784 	if (sdebug_ato)
2785 		ctrl_m_pg[5] |= 0x80; /* ATO=1 */
2786 
2787 	memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
2788 	if (1 == pcontrol)
2789 		memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
2790 	else if (2 == pcontrol)
2791 		memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
2792 	return sizeof(ctrl_m_pg);
2793 }
2794 
2795 /* IO Advice Hints Grouping mode page */
2796 static int resp_grouping_m_pg(unsigned char *p, int pcontrol, int target)
2797 {
2798 	/* IO Advice Hints Grouping mode page */
2799 	struct grouping_m_pg {
2800 		u8 page_code;	/* OR 0x40 when subpage_code > 0 */
2801 		u8 subpage_code;
2802 		__be16 page_length;
2803 		u8 reserved[12];
2804 		struct scsi_io_group_descriptor descr[MAXIMUM_NUMBER_OF_STREAMS];
2805 	};
2806 	static const struct grouping_m_pg gr_m_pg = {
2807 		.page_code = 0xa | 0x40,
2808 		.subpage_code = 5,
2809 		.page_length = cpu_to_be16(sizeof(gr_m_pg) - 4),
2810 		.descr = {
2811 			{ .st_enble = 1 },
2812 			{ .st_enble = 1 },
2813 			{ .st_enble = 1 },
2814 			{ .st_enble = 1 },
2815 			{ .st_enble = 1 },
2816 			{ .st_enble = 0 },
2817 		}
2818 	};
2819 
2820 	BUILD_BUG_ON(sizeof(struct grouping_m_pg) !=
2821 		     16 + MAXIMUM_NUMBER_OF_STREAMS * 16);
2822 	memcpy(p, &gr_m_pg, sizeof(gr_m_pg));
2823 	if (1 == pcontrol) {
2824 		/* There are no changeable values so clear from byte 4 on. */
2825 		memset(p + 4, 0, sizeof(gr_m_pg) - 4);
2826 	}
2827 	return sizeof(gr_m_pg);
2828 }
2829 
2830 static int resp_iec_m_pg(unsigned char *p, int pcontrol, int target)
2831 {	/* Informational Exceptions control mode page for mode_sense */
2832 	static const unsigned char ch_iec_m_pg[] = {
2833 		/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
2834 		0, 0, 0x0, 0x0
2835 	};
2836 	static const unsigned char d_iec_m_pg[] = {
2837 		0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
2838 		0, 0, 0x0, 0x0
2839 	};
2840 
2841 	memcpy(p, iec_m_pg, sizeof(iec_m_pg));
2842 	if (1 == pcontrol)
2843 		memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
2844 	else if (2 == pcontrol)
2845 		memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
2846 	return sizeof(iec_m_pg);
2847 }
2848 
2849 static int resp_sas_sf_m_pg(unsigned char *p, int pcontrol, int target)
2850 {	/* SAS SSP mode page - short format for mode_sense */
2851 	static const unsigned char sas_sf_m_pg[] = {
2852 		0x19, 0x6, 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0
2853 	};
2854 
2855 	memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
2856 	if (1 == pcontrol)
2857 		memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
2858 	return sizeof(sas_sf_m_pg);
2859 }
2860 
2861 
2862 static int resp_sas_pcd_m_spg(unsigned char *p, int pcontrol, int target,
2863 			      int target_dev_id)
2864 {	/* SAS phy control and discover mode page for mode_sense */
2865 	unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
2866 		    0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
2867 		    0, 0, 0, 0, 0, 0, 0, 0,	/* insert SAS addr */
2868 		    0, 0, 0, 0, 0, 0, 0, 0,	/* insert SAS addr */
2869 		    0x2, 0, 0, 0, 0, 0, 0, 0,
2870 		    0x88, 0x99, 0, 0, 0, 0, 0, 0,
2871 		    0, 0, 0, 0, 0, 0, 0, 0,
2872 		    0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
2873 		    0, 0, 0, 0, 0, 0, 0, 0,	/* insert SAS addr */
2874 		    0, 0, 0, 0, 0, 0, 0, 0,	/* insert SAS addr */
2875 		    0x3, 0, 0, 0, 0, 0, 0, 0,
2876 		    0x88, 0x99, 0, 0, 0, 0, 0, 0,
2877 		    0, 0, 0, 0, 0, 0, 0, 0,
2878 		};
2879 	int port_a, port_b;
2880 
2881 	put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 16);
2882 	put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 24);
2883 	put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 64);
2884 	put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 72);
2885 	port_a = target_dev_id + 1;
2886 	port_b = port_a + 1;
2887 	memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
2888 	put_unaligned_be32(port_a, p + 20);
2889 	put_unaligned_be32(port_b, p + 48 + 20);
2890 	if (1 == pcontrol)
2891 		memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
2892 	return sizeof(sas_pcd_m_pg);
2893 }
2894 
2895 static int resp_sas_sha_m_spg(unsigned char *p, int pcontrol)
2896 {	/* SAS SSP shared protocol specific port mode subpage */
2897 	static const unsigned char sas_sha_m_pg[] = {
2898 		0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
2899 		0, 0, 0, 0, 0, 0, 0, 0,
2900 	};
2901 
2902 	memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
2903 	if (1 == pcontrol)
2904 		memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
2905 	return sizeof(sas_sha_m_pg);
2906 }
2907 
2908 static unsigned char partition_pg[] = {0x11, 12, 1, 0, 0x24, 3, 9, 0,
2909 	0xff, 0xff, 0x00, 0x00};
2910 
2911 static int resp_partition_m_pg(unsigned char *p, int pcontrol, int target)
2912 {	/* Partition page for mode_sense (tape) */
2913 	memcpy(p, partition_pg, sizeof(partition_pg));
2914 	if (pcontrol == 1)
2915 		memset(p + 2, 0, sizeof(partition_pg) - 2);
2916 	return sizeof(partition_pg);
2917 }
2918 
2919 static int process_medium_part_m_pg(struct sdebug_dev_info *devip,
2920 				unsigned char *new, int pg_len)
2921 {
2922 	int new_nbr, p0_size, p1_size;
2923 
2924 	if ((new[4] & 0x80) != 0) { /* FDP */
2925 		partition_pg[4] |= 0x80;
2926 		devip->tape_pending_nbr_partitions = TAPE_MAX_PARTITIONS;
2927 		devip->tape_pending_part_0_size = TAPE_UNITS - TAPE_PARTITION_1_UNITS;
2928 		devip->tape_pending_part_1_size = TAPE_PARTITION_1_UNITS;
2929 	} else {
2930 		new_nbr = new[3] + 1;
2931 		if (new_nbr > TAPE_MAX_PARTITIONS)
2932 			return 3;
2933 		if ((new[4] & 0x40) != 0) { /* SDP */
2934 			p1_size = TAPE_PARTITION_1_UNITS;
2935 			p0_size = TAPE_UNITS - p1_size;
2936 			if (p0_size < 100)
2937 				return 4;
2938 		} else if ((new[4] & 0x20) != 0) {
2939 			if (new_nbr > 1) {
2940 				p0_size = get_unaligned_be16(new + 8);
2941 				p1_size = get_unaligned_be16(new + 10);
2942 				if (p1_size == 0xFFFF)
2943 					p1_size = TAPE_UNITS - p0_size;
2944 				else if (p0_size == 0xFFFF)
2945 					p0_size = TAPE_UNITS - p1_size;
2946 				if (p0_size < 100 || p1_size < 100)
2947 					return 8;
2948 			} else {
2949 				p0_size = TAPE_UNITS;
2950 				p1_size = 0;
2951 			}
2952 		} else
2953 			return 6;
2954 		devip->tape_pending_nbr_partitions = new_nbr;
2955 		devip->tape_pending_part_0_size = p0_size;
2956 		devip->tape_pending_part_1_size = p1_size;
2957 		partition_pg[3] = new_nbr;
2958 		devip->tape_pending_nbr_partitions = new_nbr;
2959 	}
2960 
2961 	return 0;
2962 }
2963 
2964 static int resp_compression_m_pg(unsigned char *p, int pcontrol, int target,
2965 	unsigned char dce)
2966 {	/* Compression page for mode_sense (tape) */
2967 	static const unsigned char compression_pg[] = {
2968 		0x0f, 14, 0x40, 0, 0, 0, 0, 0,
2969 		0, 0, 0, 0, 0, 0
2970 	};
2971 
2972 	memcpy(p, compression_pg, sizeof(compression_pg));
2973 	if (dce)
2974 		p[2] |= 0x80;
2975 	if (pcontrol == 1)
2976 		memset(p + 2, 0, sizeof(compression_pg) - 2);
2977 	return sizeof(compression_pg);
2978 }
2979 
2980 /* PAGE_SIZE is more than necessary but provides room for future expansion. */
2981 #define SDEBUG_MAX_MSENSE_SZ PAGE_SIZE
2982 
2983 static int resp_mode_sense(struct scsi_cmnd *scp,
2984 			   struct sdebug_dev_info *devip)
2985 {
2986 	int pcontrol, pcode, subpcode, bd_len;
2987 	unsigned char dev_spec;
2988 	u32 alloc_len, offset, len;
2989 	int target_dev_id;
2990 	int target = scp->device->id;
2991 	unsigned char *ap;
2992 	unsigned char *cmd = scp->cmnd;
2993 	bool dbd, llbaa, msense_6, is_disk, is_zbc, is_tape;
2994 
2995 	unsigned char *arr __free(kfree) = kzalloc(SDEBUG_MAX_MSENSE_SZ, GFP_ATOMIC);
2996 
2997 	if (!arr)
2998 		return -ENOMEM;
2999 	dbd = !!(cmd[1] & 0x8);		/* disable block descriptors */
3000 	pcontrol = (cmd[2] & 0xc0) >> 6;
3001 	pcode = cmd[2] & 0x3f;
3002 	subpcode = cmd[3];
3003 	msense_6 = (MODE_SENSE == cmd[0]);
3004 	llbaa = msense_6 ? false : !!(cmd[1] & 0x10);
3005 	is_disk = (scp->device->type == TYPE_DISK);
3006 	is_zbc = devip->zoned;
3007 	is_tape = (scp->device->type == TYPE_TAPE);
3008 	if ((is_disk || is_zbc || is_tape) && !dbd)
3009 		bd_len = llbaa ? 16 : 8;
3010 	else
3011 		bd_len = 0;
3012 	alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7);
3013 	if (0x3 == pcontrol) {
3014 		mk_sense_buffer(scp, ILLEGAL_REQUEST,
3015 				SAVING_PARAMETERS_NOT_SUPPORTED);
3016 		return check_condition_result;
3017 	}
3018 	target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
3019 			(devip->target * 1000) - 3;
3020 	/* for disks+zbc set DPOFUA bit and clear write protect (WP) bit */
3021 	if (is_disk || is_zbc) {
3022 		dev_spec = 0x10;	/* =0x90 if WP=1 implies read-only */
3023 		if (sdebug_wp)
3024 			dev_spec |= 0x80;
3025 	} else
3026 		dev_spec = 0x0;
3027 	if (msense_6) {
3028 		arr[2] = dev_spec;
3029 		arr[3] = bd_len;
3030 		offset = 4;
3031 	} else {
3032 		arr[3] = dev_spec;
3033 		if (16 == bd_len)
3034 			arr[4] = 0x1;	/* set LONGLBA bit */
3035 		arr[7] = bd_len;	/* assume 255 or less */
3036 		offset = 8;
3037 	}
3038 	ap = arr + offset;
3039 	if ((bd_len > 0) && (!sdebug_capacity))
3040 		sdebug_capacity = get_sdebug_capacity();
3041 
3042 	if (8 == bd_len) {
3043 		if (sdebug_capacity > 0xfffffffe)
3044 			put_unaligned_be32(0xffffffff, ap + 0);
3045 		else
3046 			put_unaligned_be32(sdebug_capacity, ap + 0);
3047 		if (is_tape) {
3048 			ap[0] = devip->tape_density;
3049 			put_unaligned_be16(devip->tape_blksize, ap + 6);
3050 		} else
3051 			put_unaligned_be16(sdebug_sector_size, ap + 6);
3052 		offset += bd_len;
3053 		ap = arr + offset;
3054 	} else if (16 == bd_len) {
3055 		if (is_tape) {
3056 			mk_sense_invalid_fld(scp, SDEB_IN_DATA, 1, 4);
3057 			return check_condition_result;
3058 		}
3059 		put_unaligned_be64((u64)sdebug_capacity, ap + 0);
3060 		put_unaligned_be32(sdebug_sector_size, ap + 12);
3061 		offset += bd_len;
3062 		ap = arr + offset;
3063 	}
3064 	if (cmd[2] == 0)
3065 		goto only_bd; /* Only block descriptor requested */
3066 
3067 	/*
3068 	 * N.B. If len>0 before resp_*_pg() call, then form of that call should be:
3069 	 *        len += resp_*_pg(ap + len, pcontrol, target);
3070 	 */
3071 	switch (pcode) {
3072 	case 0x1:	/* Read-Write error recovery page, direct access */
3073 		if (subpcode > 0x0 && subpcode < 0xff)
3074 			goto bad_subpcode;
3075 		len = resp_err_recov_pg(ap, pcontrol, target);
3076 		offset += len;
3077 		break;
3078 	case 0x2:	/* Disconnect-Reconnect page, all devices */
3079 		if (subpcode > 0x0 && subpcode < 0xff)
3080 			goto bad_subpcode;
3081 		len = resp_disconnect_pg(ap, pcontrol, target);
3082 		offset += len;
3083 		break;
3084 	case 0x3:       /* Format device page, direct access */
3085 		if (subpcode > 0x0 && subpcode < 0xff)
3086 			goto bad_subpcode;
3087 		if (is_disk) {
3088 			len = resp_format_pg(ap, pcontrol, target);
3089 			offset += len;
3090 		} else {
3091 			goto bad_pcode;
3092 		}
3093 		break;
3094 	case 0x8:	/* Caching page, direct access */
3095 		if (subpcode > 0x0 && subpcode < 0xff)
3096 			goto bad_subpcode;
3097 		if (is_disk || is_zbc) {
3098 			len = resp_caching_pg(ap, pcontrol, target);
3099 			offset += len;
3100 		} else {
3101 			goto bad_pcode;
3102 		}
3103 		break;
3104 	case 0xa:	/* Control Mode page, all devices */
3105 		switch (subpcode) {
3106 		case 0:
3107 			len = resp_ctrl_m_pg(ap, pcontrol, target);
3108 			break;
3109 		case 0x05:
3110 			len = resp_grouping_m_pg(ap, pcontrol, target);
3111 			break;
3112 		case 0xff:
3113 			len = resp_ctrl_m_pg(ap, pcontrol, target);
3114 			len += resp_grouping_m_pg(ap + len, pcontrol, target);
3115 			break;
3116 		default:
3117 			goto bad_subpcode;
3118 		}
3119 		offset += len;
3120 		break;
3121 	case 0xf:	/* Compression Mode Page (tape) */
3122 		if (!is_tape)
3123 			goto bad_pcode;
3124 		len = resp_compression_m_pg(ap, pcontrol, target, devip->tape_dce);
3125 		offset += len;
3126 		break;
3127 	case 0x11:	/* Partition Mode Page (tape) */
3128 		if (!is_tape)
3129 			goto bad_pcode;
3130 		len = resp_partition_m_pg(ap, pcontrol, target);
3131 		offset += len;
3132 		break;
3133 	case 0x19:	/* if spc==1 then sas phy, control+discover */
3134 		if (subpcode > 0x2 && subpcode < 0xff)
3135 			goto bad_subpcode;
3136 		len = 0;
3137 		if ((0x0 == subpcode) || (0xff == subpcode))
3138 			len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
3139 		if ((0x1 == subpcode) || (0xff == subpcode))
3140 			len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
3141 						  target_dev_id);
3142 		if ((0x2 == subpcode) || (0xff == subpcode))
3143 			len += resp_sas_sha_m_spg(ap + len, pcontrol);
3144 		offset += len;
3145 		break;
3146 	case 0x1c:	/* Informational Exceptions Mode page, all devices */
3147 		if (subpcode > 0x0 && subpcode < 0xff)
3148 			goto bad_subpcode;
3149 		len = resp_iec_m_pg(ap, pcontrol, target);
3150 		offset += len;
3151 		break;
3152 	case 0x3f:	/* Read all Mode pages */
3153 		if (subpcode > 0x0 && subpcode < 0xff)
3154 			goto bad_subpcode;
3155 		len = resp_err_recov_pg(ap, pcontrol, target);
3156 		len += resp_disconnect_pg(ap + len, pcontrol, target);
3157 		if (is_disk) {
3158 			len += resp_format_pg(ap + len, pcontrol, target);
3159 			len += resp_caching_pg(ap + len, pcontrol, target);
3160 		} else if (is_zbc) {
3161 			len += resp_caching_pg(ap + len, pcontrol, target);
3162 		}
3163 		len += resp_ctrl_m_pg(ap + len, pcontrol, target);
3164 		if (0xff == subpcode)
3165 			len += resp_grouping_m_pg(ap + len, pcontrol, target);
3166 		len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
3167 		if (0xff == subpcode) {
3168 			len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
3169 						  target_dev_id);
3170 			len += resp_sas_sha_m_spg(ap + len, pcontrol);
3171 		}
3172 		len += resp_iec_m_pg(ap + len, pcontrol, target);
3173 		offset += len;
3174 		break;
3175 	default:
3176 		goto bad_pcode;
3177 	}
3178 only_bd:
3179 	if (msense_6)
3180 		arr[0] = offset - 1;
3181 	else
3182 		put_unaligned_be16((offset - 2), arr + 0);
3183 	return fill_from_dev_buffer(scp, arr, min_t(u32, alloc_len, offset));
3184 
3185 bad_pcode:
3186 	mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
3187 	return check_condition_result;
3188 
3189 bad_subpcode:
3190 	mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
3191 	return check_condition_result;
3192 }
3193 
3194 #define SDEBUG_MAX_MSELECT_SZ 512
3195 
3196 static int resp_mode_select(struct scsi_cmnd *scp,
3197 			    struct sdebug_dev_info *devip)
3198 {
3199 	int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
3200 	int param_len, res, mpage;
3201 	unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
3202 	unsigned char *cmd = scp->cmnd;
3203 	int mselect6 = (MODE_SELECT == cmd[0]);
3204 
3205 	memset(arr, 0, sizeof(arr));
3206 	pf = cmd[1] & 0x10;
3207 	sp = cmd[1] & 0x1;
3208 	param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7);
3209 	if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
3210 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1);
3211 		return check_condition_result;
3212 	}
3213 	res = fetch_to_dev_buffer(scp, arr, param_len);
3214 	if (-1 == res)
3215 		return DID_ERROR << 16;
3216 	else if (sdebug_verbose && (res < param_len))
3217 		sdev_printk(KERN_INFO, scp->device,
3218 			    "cdb indicated=%d, IO sent=%d bytes\n",
3219 			    param_len, res);
3220 	md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2);
3221 	bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6);
3222 	off = (mselect6 ? 4 : 8);
3223 	if (scp->device->type == TYPE_TAPE) {
3224 		int blksize;
3225 
3226 		if (bd_len != 8) {
3227 			mk_sense_invalid_fld(scp, SDEB_IN_DATA,
3228 					mselect6 ? 3 : 6, -1);
3229 			return check_condition_result;
3230 		}
3231 		if (arr[off] == TAPE_BAD_DENSITY) {
3232 			mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1);
3233 			return check_condition_result;
3234 		}
3235 		blksize = get_unaligned_be16(arr + off + 6);
3236 		if (blksize != 0 &&
3237 			(blksize < TAPE_MIN_BLKSIZE ||
3238 				blksize > TAPE_MAX_BLKSIZE ||
3239 				(blksize % 4) != 0)) {
3240 			mk_sense_invalid_fld(scp, SDEB_IN_DATA, 1, -1);
3241 			return check_condition_result;
3242 		}
3243 		devip->tape_density = arr[off];
3244 		devip->tape_blksize = blksize;
3245 	}
3246 	off += bd_len;
3247 	if (off >= res)
3248 		return 0; /* No page written, just descriptors */
3249 	if (md_len > 2) {
3250 		mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1);
3251 		return check_condition_result;
3252 	}
3253 	mpage = arr[off] & 0x3f;
3254 	ps = !!(arr[off] & 0x80);
3255 	if (ps) {
3256 		mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7);
3257 		return check_condition_result;
3258 	}
3259 	spf = !!(arr[off] & 0x40);
3260 	pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) :
3261 		       (arr[off + 1] + 2);
3262 	if ((pg_len + off) > param_len) {
3263 		mk_sense_buffer(scp, ILLEGAL_REQUEST,
3264 				PARAMETER_LIST_LENGTH_ERROR);
3265 		return check_condition_result;
3266 	}
3267 	switch (mpage) {
3268 	case 0x8:      /* Caching Mode page */
3269 		if (caching_pg[1] == arr[off + 1]) {
3270 			memcpy(caching_pg + 2, arr + off + 2,
3271 			       sizeof(caching_pg) - 2);
3272 			goto set_mode_changed_ua;
3273 		}
3274 		break;
3275 	case 0xa:      /* Control Mode page */
3276 		if (ctrl_m_pg[1] == arr[off + 1]) {
3277 			memcpy(ctrl_m_pg + 2, arr + off + 2,
3278 			       sizeof(ctrl_m_pg) - 2);
3279 			if (ctrl_m_pg[4] & 0x8)
3280 				sdebug_wp = true;
3281 			else
3282 				sdebug_wp = false;
3283 			sdebug_dsense = !!(ctrl_m_pg[2] & 0x4);
3284 			goto set_mode_changed_ua;
3285 		}
3286 		break;
3287 	case 0xf:       /* Compression mode page */
3288 		if (scp->device->type != TYPE_TAPE)
3289 			goto bad_pcode;
3290 		if ((arr[off + 2] & 0x40) != 0) {
3291 			devip->tape_dce = (arr[off + 2] & 0x80) != 0;
3292 			return 0;
3293 		}
3294 		break;
3295 	case 0x11:	/* Medium Partition Mode Page (tape) */
3296 		if (scp->device->type == TYPE_TAPE) {
3297 			int fld;
3298 
3299 			fld = process_medium_part_m_pg(devip, &arr[off], pg_len);
3300 			if (fld == 0)
3301 				return 0;
3302 			mk_sense_invalid_fld(scp, SDEB_IN_DATA, fld, -1);
3303 			return check_condition_result;
3304 		}
3305 		break;
3306 	case 0x1c:      /* Informational Exceptions Mode page */
3307 		if (iec_m_pg[1] == arr[off + 1]) {
3308 			memcpy(iec_m_pg + 2, arr + off + 2,
3309 			       sizeof(iec_m_pg) - 2);
3310 			goto set_mode_changed_ua;
3311 		}
3312 		break;
3313 	default:
3314 		break;
3315 	}
3316 	mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5);
3317 	return check_condition_result;
3318 set_mode_changed_ua:
3319 	set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm);
3320 	return 0;
3321 
3322 bad_pcode:
3323 	mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
3324 	return check_condition_result;
3325 }
3326 
3327 static int resp_temp_l_pg(unsigned char *arr)
3328 {
3329 	static const unsigned char temp_l_pg[] = {
3330 		0x0, 0x0, 0x3, 0x2, 0x0, 38,
3331 		0x0, 0x1, 0x3, 0x2, 0x0, 65,
3332 	};
3333 
3334 	memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
3335 	return sizeof(temp_l_pg);
3336 }
3337 
3338 static int resp_ie_l_pg(unsigned char *arr)
3339 {
3340 	static const unsigned char ie_l_pg[] = {
3341 		0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
3342 	};
3343 
3344 	memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
3345 	if (iec_m_pg[2] & 0x4) {	/* TEST bit set */
3346 		arr[4] = ASC_FAILURE_PREDICTION_THRESHOLD_EXCEEDED;
3347 		arr[5] = 0xff;
3348 	}
3349 	return sizeof(ie_l_pg);
3350 }
3351 
3352 static int resp_env_rep_l_spg(unsigned char *arr)
3353 {
3354 	static const unsigned char env_rep_l_spg[] = {
3355 		0x0, 0x0, 0x23, 0x8,
3356 		0x0, 40, 72, 0xff, 45, 18, 0, 0,
3357 		0x1, 0x0, 0x23, 0x8,
3358 		0x0, 55, 72, 35, 55, 45, 0, 0,
3359 	};
3360 
3361 	memcpy(arr, env_rep_l_spg, sizeof(env_rep_l_spg));
3362 	return sizeof(env_rep_l_spg);
3363 }
3364 
3365 #define SDEBUG_MAX_LSENSE_SZ 512
3366 
3367 static int resp_log_sense(struct scsi_cmnd *scp,
3368 			  struct sdebug_dev_info *devip)
3369 {
3370 	int ppc, sp, pcode, subpcode;
3371 	u32 alloc_len, len, n;
3372 	unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
3373 	unsigned char *cmd = scp->cmnd;
3374 
3375 	memset(arr, 0, sizeof(arr));
3376 	ppc = cmd[1] & 0x2;
3377 	sp = cmd[1] & 0x1;
3378 	if (ppc || sp) {
3379 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0);
3380 		return check_condition_result;
3381 	}
3382 	pcode = cmd[2] & 0x3f;
3383 	subpcode = cmd[3] & 0xff;
3384 	alloc_len = get_unaligned_be16(cmd + 7);
3385 	arr[0] = pcode;
3386 	if (0 == subpcode) {
3387 		switch (pcode) {
3388 		case 0x0:	/* Supported log pages log page */
3389 			n = 4;
3390 			arr[n++] = 0x0;		/* this page */
3391 			arr[n++] = 0xd;		/* Temperature */
3392 			arr[n++] = 0x2f;	/* Informational exceptions */
3393 			arr[3] = n - 4;
3394 			break;
3395 		case 0xd:	/* Temperature log page */
3396 			arr[3] = resp_temp_l_pg(arr + 4);
3397 			break;
3398 		case 0x2f:	/* Informational exceptions log page */
3399 			arr[3] = resp_ie_l_pg(arr + 4);
3400 			break;
3401 		default:
3402 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
3403 			return check_condition_result;
3404 		}
3405 	} else if (0xff == subpcode) {
3406 		arr[0] |= 0x40;
3407 		arr[1] = subpcode;
3408 		switch (pcode) {
3409 		case 0x0:	/* Supported log pages and subpages log page */
3410 			n = 4;
3411 			arr[n++] = 0x0;
3412 			arr[n++] = 0x0;		/* 0,0 page */
3413 			arr[n++] = 0x0;
3414 			arr[n++] = 0xff;	/* this page */
3415 			arr[n++] = 0xd;
3416 			arr[n++] = 0x0;		/* Temperature */
3417 			arr[n++] = 0xd;
3418 			arr[n++] = 0x1;		/* Environment reporting */
3419 			arr[n++] = 0xd;
3420 			arr[n++] = 0xff;	/* all 0xd subpages */
3421 			arr[n++] = 0x2f;
3422 			arr[n++] = 0x0;	/* Informational exceptions */
3423 			arr[n++] = 0x2f;
3424 			arr[n++] = 0xff;	/* all 0x2f subpages */
3425 			arr[3] = n - 4;
3426 			break;
3427 		case 0xd:	/* Temperature subpages */
3428 			n = 4;
3429 			arr[n++] = 0xd;
3430 			arr[n++] = 0x0;		/* Temperature */
3431 			arr[n++] = 0xd;
3432 			arr[n++] = 0x1;		/* Environment reporting */
3433 			arr[n++] = 0xd;
3434 			arr[n++] = 0xff;	/* these subpages */
3435 			arr[3] = n - 4;
3436 			break;
3437 		case 0x2f:	/* Informational exceptions subpages */
3438 			n = 4;
3439 			arr[n++] = 0x2f;
3440 			arr[n++] = 0x0;		/* Informational exceptions */
3441 			arr[n++] = 0x2f;
3442 			arr[n++] = 0xff;	/* these subpages */
3443 			arr[3] = n - 4;
3444 			break;
3445 		default:
3446 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
3447 			return check_condition_result;
3448 		}
3449 	} else if (subpcode > 0) {
3450 		arr[0] |= 0x40;
3451 		arr[1] = subpcode;
3452 		if (pcode == 0xd && subpcode == 1)
3453 			arr[3] = resp_env_rep_l_spg(arr + 4);
3454 		else {
3455 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
3456 			return check_condition_result;
3457 		}
3458 	} else {
3459 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
3460 		return check_condition_result;
3461 	}
3462 	len = min_t(u32, get_unaligned_be16(arr + 2) + 4, alloc_len);
3463 	return fill_from_dev_buffer(scp, arr,
3464 		    min_t(u32, len, SDEBUG_MAX_INQ_ARR_SZ));
3465 }
3466 
3467 enum {SDEBUG_READ_BLOCK_LIMITS_ARR_SZ = 6};
3468 static int resp_read_blklimits(struct scsi_cmnd *scp,
3469 			struct sdebug_dev_info *devip)
3470 {
3471 	unsigned char arr[SDEBUG_READ_BLOCK_LIMITS_ARR_SZ];
3472 
3473 	arr[0] = 4;
3474 	put_unaligned_be24(TAPE_MAX_BLKSIZE, arr + 1);
3475 	put_unaligned_be16(TAPE_MIN_BLKSIZE, arr + 4);
3476 	return fill_from_dev_buffer(scp, arr, SDEBUG_READ_BLOCK_LIMITS_ARR_SZ);
3477 }
3478 
3479 static int resp_locate(struct scsi_cmnd *scp,
3480 		struct sdebug_dev_info *devip)
3481 {
3482 	unsigned char *cmd = scp->cmnd;
3483 	unsigned int i, pos;
3484 	struct tape_block *blp;
3485 	int partition;
3486 
3487 	if ((cmd[1] & 0x02) != 0) {
3488 		if (cmd[8] >= devip->tape_nbr_partitions) {
3489 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, -1);
3490 			return check_condition_result;
3491 		}
3492 		devip->tape_partition = cmd[8];
3493 	}
3494 	pos = get_unaligned_be32(cmd + 3);
3495 	partition = devip->tape_partition;
3496 
3497 	for (i = 0, blp = devip->tape_blocks[partition];
3498 	     i < pos && i < devip->tape_eop[partition]; i++, blp++)
3499 		if (IS_TAPE_BLOCK_EOD(blp->fl_size))
3500 			break;
3501 	if (i < pos) {
3502 		devip->tape_location[partition] = i;
3503 		mk_sense_buffer(scp, BLANK_CHECK,
3504 				LU_DOES_NOT_RESPOND_TO_SELECTION);
3505 		return check_condition_result;
3506 	}
3507 	devip->tape_location[partition] = pos;
3508 
3509 	return 0;
3510 }
3511 
3512 static int resp_write_filemarks(struct scsi_cmnd *scp,
3513 		struct sdebug_dev_info *devip)
3514 {
3515 	unsigned char *cmd = scp->cmnd;
3516 	unsigned int i, count, pos;
3517 	u32 data;
3518 	int partition = devip->tape_partition;
3519 
3520 	if ((cmd[1] & 0xfe) != 0) { /* probably write setmarks, not in >= SCSI-3 */
3521 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1);
3522 		return check_condition_result;
3523 	}
3524 	count = get_unaligned_be24(cmd + 2);
3525 	data = TAPE_BLOCK_FM_FLAG;
3526 	for (i = 0, pos = devip->tape_location[partition]; i < count; i++, pos++) {
3527 		if (pos >= devip->tape_eop[partition] - 1) { /* don't overwrite EOD */
3528 			devip->tape_location[partition] = devip->tape_eop[partition] - 1;
3529 			mk_sense_info_tape(scp, VOLUME_OVERFLOW,
3530 					   END_OF_PARTITION_MEDIUM_DETECTED,
3531 					   count, SENSE_FLAG_EOM);
3532 			return check_condition_result;
3533 		}
3534 		(devip->tape_blocks[partition] + pos)->fl_size = data;
3535 	}
3536 	(devip->tape_blocks[partition] + pos)->fl_size =
3537 		TAPE_BLOCK_EOD_FLAG;
3538 	devip->tape_location[partition] = pos;
3539 
3540 	return 0;
3541 }
3542 
3543 static int resp_space(struct scsi_cmnd *scp,
3544 		struct sdebug_dev_info *devip)
3545 {
3546 	unsigned char *cmd = scp->cmnd, code;
3547 	int i = 0, pos, count;
3548 	struct tape_block *blp;
3549 	int partition = devip->tape_partition;
3550 
3551 	count = get_unaligned_be24(cmd + 2);
3552 	if ((count & 0x800000) != 0) /* extend negative to 32-bit count */
3553 		count |= 0xff000000;
3554 	code = cmd[1] & 0x0f;
3555 
3556 	pos = devip->tape_location[partition];
3557 	if (code == 0) { /* blocks */
3558 		if (count < 0) {
3559 			count = (-count);
3560 			pos -= 1;
3561 			for (i = 0, blp = devip->tape_blocks[partition] + pos; i < count;
3562 			     i++) {
3563 				if (pos < 0)
3564 					goto is_bop;
3565 				else if (IS_TAPE_BLOCK_FM(blp->fl_size))
3566 					goto is_fm;
3567 				if (i > 0) {
3568 					pos--;
3569 					blp--;
3570 				}
3571 			}
3572 		} else if (count > 0) {
3573 			for (i = 0, blp = devip->tape_blocks[partition] + pos; i < count;
3574 			     i++, pos++, blp++) {
3575 				if (IS_TAPE_BLOCK_EOD(blp->fl_size))
3576 					goto is_eod;
3577 				if (IS_TAPE_BLOCK_FM(blp->fl_size)) {
3578 					pos += 1;
3579 					goto is_fm;
3580 				}
3581 				if (pos >= devip->tape_eop[partition])
3582 					goto is_eop;
3583 			}
3584 		}
3585 	} else if (code == 1) { /* filemarks */
3586 		if (count < 0) {
3587 			count = (-count);
3588 			if (pos == 0)
3589 				goto is_bop;
3590 			else {
3591 				for (i = 0, blp = devip->tape_blocks[partition] + pos;
3592 				     i < count && pos >= 0; i++, pos--, blp--) {
3593 					for (pos--, blp-- ; !IS_TAPE_BLOCK_FM(blp->fl_size) &&
3594 						     pos >= 0; pos--, blp--)
3595 						; /* empty */
3596 					if (pos < 0)
3597 						goto is_bop;
3598 				}
3599 			}
3600 			pos += 1;
3601 		} else if (count > 0) {
3602 			for (i = 0, blp = devip->tape_blocks[partition] + pos;
3603 			     i < count; i++, pos++, blp++) {
3604 				for ( ; !IS_TAPE_BLOCK_FM(blp->fl_size) &&
3605 					      !IS_TAPE_BLOCK_EOD(blp->fl_size) &&
3606 					      pos < devip->tape_eop[partition];
3607 				      pos++, blp++)
3608 					; /* empty */
3609 				if (IS_TAPE_BLOCK_EOD(blp->fl_size))
3610 					goto is_eod;
3611 				if (pos >= devip->tape_eop[partition])
3612 					goto is_eop;
3613 			}
3614 		}
3615 	} else if (code == 3) { /* EOD */
3616 		for (blp = devip->tape_blocks[partition] + pos;
3617 		     !IS_TAPE_BLOCK_EOD(blp->fl_size) && pos < devip->tape_eop[partition];
3618 		     pos++, blp++)
3619 			; /* empty */
3620 		if (pos >= devip->tape_eop[partition])
3621 			goto is_eop;
3622 	} else {
3623 		/* sequential filemarks not supported */
3624 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, -1);
3625 		return check_condition_result;
3626 	}
3627 	devip->tape_location[partition] = pos;
3628 	return 0;
3629 
3630 is_fm:
3631 	devip->tape_location[partition] = pos;
3632 	mk_sense_info_tape(scp, NO_SENSE, FILEMARK_DETECTED,
3633 			   count - i, SENSE_FLAG_FILEMARK);
3634 	return check_condition_result;
3635 
3636 is_eod:
3637 	devip->tape_location[partition] = pos;
3638 	mk_sense_info_tape(scp, BLANK_CHECK, END_OF_DATA_DETECTED,
3639 			   count - i, 0);
3640 	return check_condition_result;
3641 
3642 is_bop:
3643 	devip->tape_location[partition] = 0;
3644 	mk_sense_info_tape(scp, NO_SENSE,
3645 			   BEGINNING_OF_PARTITION_MEDIUM_DETECTED,
3646 			   count - i, SENSE_FLAG_EOM);
3647 	devip->tape_location[partition] = 0;
3648 	return check_condition_result;
3649 
3650 is_eop:
3651 	devip->tape_location[partition] = devip->tape_eop[partition] - 1;
3652 	mk_sense_info_tape(scp, MEDIUM_ERROR, END_OF_PARTITION_MEDIUM_DETECTED,
3653 			   (unsigned int)i, SENSE_FLAG_EOM);
3654 	return check_condition_result;
3655 }
3656 
3657 enum {SDEBUG_READ_POSITION_ARR_SZ = 20};
3658 static int resp_read_position(struct scsi_cmnd *scp,
3659 			struct sdebug_dev_info *devip)
3660 {
3661 	u8 *cmd = scp->cmnd;
3662 	int all_length;
3663 	unsigned char arr[20];
3664 	unsigned int pos;
3665 
3666 	all_length = get_unaligned_be16(cmd + 7);
3667 	if ((cmd[1] & 0xfe) != 0 ||
3668 		all_length != 0) { /* only short form */
3669 		mk_sense_invalid_fld(scp, SDEB_IN_CDB,
3670 				all_length ? 7 : 1, 0);
3671 		return check_condition_result;
3672 	}
3673 	memset(arr, 0, SDEBUG_READ_POSITION_ARR_SZ);
3674 	arr[1] = devip->tape_partition;
3675 	pos = devip->tape_location[devip->tape_partition];
3676 	put_unaligned_be32(pos, arr + 4);
3677 	put_unaligned_be32(pos, arr + 8);
3678 	return fill_from_dev_buffer(scp, arr, SDEBUG_READ_POSITION_ARR_SZ);
3679 }
3680 
3681 static int resp_rewind(struct scsi_cmnd *scp,
3682 		struct sdebug_dev_info *devip)
3683 {
3684 	devip->tape_location[devip->tape_partition] = 0;
3685 
3686 	return 0;
3687 }
3688 
3689 static int partition_tape(struct sdebug_dev_info *devip, int nbr_partitions,
3690 			int part_0_size, int part_1_size)
3691 {
3692 	int i;
3693 
3694 	if (part_0_size + part_1_size > TAPE_UNITS)
3695 		return -1;
3696 	devip->tape_eop[0] = part_0_size;
3697 	devip->tape_blocks[0]->fl_size = TAPE_BLOCK_EOD_FLAG;
3698 	devip->tape_eop[1] = part_1_size;
3699 	devip->tape_blocks[1] = devip->tape_blocks[0] +
3700 			devip->tape_eop[0];
3701 	devip->tape_blocks[1]->fl_size = TAPE_BLOCK_EOD_FLAG;
3702 
3703 	for (i = 0 ; i < TAPE_MAX_PARTITIONS; i++)
3704 		devip->tape_location[i] = 0;
3705 
3706 	devip->tape_nbr_partitions = nbr_partitions;
3707 	devip->tape_partition = 0;
3708 
3709 	partition_pg[3] = nbr_partitions - 1;
3710 	put_unaligned_be16(devip->tape_eop[0], partition_pg + 8);
3711 	put_unaligned_be16(devip->tape_eop[1], partition_pg + 10);
3712 
3713 	return nbr_partitions;
3714 }
3715 
3716 static int resp_format_medium(struct scsi_cmnd *scp,
3717 			struct sdebug_dev_info *devip)
3718 {
3719 	int res = 0;
3720 	unsigned char *cmd = scp->cmnd;
3721 
3722 	if (cmd[2] > 2) {
3723 		mk_sense_invalid_fld(scp, SDEB_IN_DATA, 2, -1);
3724 		return check_condition_result;
3725 	}
3726 	if (cmd[2] != 0) {
3727 		if (devip->tape_pending_nbr_partitions > 0) {
3728 			res = partition_tape(devip,
3729 					devip->tape_pending_nbr_partitions,
3730 					devip->tape_pending_part_0_size,
3731 					devip->tape_pending_part_1_size);
3732 		} else
3733 			res = partition_tape(devip, devip->tape_nbr_partitions,
3734 					devip->tape_eop[0], devip->tape_eop[1]);
3735 	} else
3736 		res = partition_tape(devip, 1, TAPE_UNITS, 0);
3737 	if (res < 0)
3738 		return -EINVAL;
3739 
3740 	devip->tape_pending_nbr_partitions = -1;
3741 
3742 	return 0;
3743 }
3744 
3745 static int resp_erase(struct scsi_cmnd *scp,
3746 		struct sdebug_dev_info *devip)
3747 {
3748 	int partition = devip->tape_partition;
3749 	int pos = devip->tape_location[partition];
3750 	struct tape_block *blp;
3751 
3752 	blp = devip->tape_blocks[partition] + pos;
3753 	blp->fl_size = TAPE_BLOCK_EOD_FLAG;
3754 
3755 	return 0;
3756 }
3757 
3758 static inline bool sdebug_dev_is_zoned(struct sdebug_dev_info *devip)
3759 {
3760 	return devip->nr_zones != 0;
3761 }
3762 
3763 static struct sdeb_zone_state *zbc_zone(struct sdebug_dev_info *devip,
3764 					unsigned long long lba)
3765 {
3766 	u32 zno = lba >> devip->zsize_shift;
3767 	struct sdeb_zone_state *zsp;
3768 
3769 	if (devip->zcap == devip->zsize || zno < devip->nr_conv_zones)
3770 		return &devip->zstate[zno];
3771 
3772 	/*
3773 	 * If the zone capacity is less than the zone size, adjust for gap
3774 	 * zones.
3775 	 */
3776 	zno = 2 * zno - devip->nr_conv_zones;
3777 	WARN_ONCE(zno >= devip->nr_zones, "%u > %u\n", zno, devip->nr_zones);
3778 	zsp = &devip->zstate[zno];
3779 	if (lba >= zsp->z_start + zsp->z_size)
3780 		zsp++;
3781 	WARN_ON_ONCE(lba >= zsp->z_start + zsp->z_size);
3782 	return zsp;
3783 }
3784 
3785 static inline bool zbc_zone_is_conv(struct sdeb_zone_state *zsp)
3786 {
3787 	return zsp->z_type == ZBC_ZTYPE_CNV;
3788 }
3789 
3790 static inline bool zbc_zone_is_gap(struct sdeb_zone_state *zsp)
3791 {
3792 	return zsp->z_type == ZBC_ZTYPE_GAP;
3793 }
3794 
3795 static inline bool zbc_zone_is_seq(struct sdeb_zone_state *zsp)
3796 {
3797 	return !zbc_zone_is_conv(zsp) && !zbc_zone_is_gap(zsp);
3798 }
3799 
3800 static void zbc_close_zone(struct sdebug_dev_info *devip,
3801 			   struct sdeb_zone_state *zsp)
3802 {
3803 	enum sdebug_z_cond zc;
3804 
3805 	if (!zbc_zone_is_seq(zsp))
3806 		return;
3807 
3808 	zc = zsp->z_cond;
3809 	if (!(zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN))
3810 		return;
3811 
3812 	if (zc == ZC2_IMPLICIT_OPEN)
3813 		devip->nr_imp_open--;
3814 	else
3815 		devip->nr_exp_open--;
3816 
3817 	if (zsp->z_wp == zsp->z_start) {
3818 		zsp->z_cond = ZC1_EMPTY;
3819 	} else {
3820 		zsp->z_cond = ZC4_CLOSED;
3821 		devip->nr_closed++;
3822 	}
3823 }
3824 
3825 static void zbc_close_imp_open_zone(struct sdebug_dev_info *devip)
3826 {
3827 	struct sdeb_zone_state *zsp = &devip->zstate[0];
3828 	unsigned int i;
3829 
3830 	for (i = 0; i < devip->nr_zones; i++, zsp++) {
3831 		if (zsp->z_cond == ZC2_IMPLICIT_OPEN) {
3832 			zbc_close_zone(devip, zsp);
3833 			return;
3834 		}
3835 	}
3836 }
3837 
3838 static void zbc_open_zone(struct sdebug_dev_info *devip,
3839 			  struct sdeb_zone_state *zsp, bool explicit)
3840 {
3841 	enum sdebug_z_cond zc;
3842 
3843 	if (!zbc_zone_is_seq(zsp))
3844 		return;
3845 
3846 	zc = zsp->z_cond;
3847 	if ((explicit && zc == ZC3_EXPLICIT_OPEN) ||
3848 	    (!explicit && zc == ZC2_IMPLICIT_OPEN))
3849 		return;
3850 
3851 	/* Close an implicit open zone if necessary */
3852 	if (explicit && zsp->z_cond == ZC2_IMPLICIT_OPEN)
3853 		zbc_close_zone(devip, zsp);
3854 	else if (devip->max_open &&
3855 		 devip->nr_imp_open + devip->nr_exp_open >= devip->max_open)
3856 		zbc_close_imp_open_zone(devip);
3857 
3858 	if (zsp->z_cond == ZC4_CLOSED)
3859 		devip->nr_closed--;
3860 	if (explicit) {
3861 		zsp->z_cond = ZC3_EXPLICIT_OPEN;
3862 		devip->nr_exp_open++;
3863 	} else {
3864 		zsp->z_cond = ZC2_IMPLICIT_OPEN;
3865 		devip->nr_imp_open++;
3866 	}
3867 }
3868 
3869 static inline void zbc_set_zone_full(struct sdebug_dev_info *devip,
3870 				     struct sdeb_zone_state *zsp)
3871 {
3872 	switch (zsp->z_cond) {
3873 	case ZC2_IMPLICIT_OPEN:
3874 		devip->nr_imp_open--;
3875 		break;
3876 	case ZC3_EXPLICIT_OPEN:
3877 		devip->nr_exp_open--;
3878 		break;
3879 	default:
3880 		WARN_ONCE(true, "Invalid zone %llu condition %x\n",
3881 			  zsp->z_start, zsp->z_cond);
3882 		break;
3883 	}
3884 	zsp->z_cond = ZC5_FULL;
3885 }
3886 
3887 static void zbc_inc_wp(struct sdebug_dev_info *devip,
3888 		       unsigned long long lba, unsigned int num)
3889 {
3890 	struct sdeb_zone_state *zsp = zbc_zone(devip, lba);
3891 	unsigned long long n, end, zend = zsp->z_start + zsp->z_size;
3892 
3893 	if (!zbc_zone_is_seq(zsp))
3894 		return;
3895 
3896 	if (zsp->z_type == ZBC_ZTYPE_SWR) {
3897 		zsp->z_wp += num;
3898 		if (zsp->z_wp >= zend)
3899 			zbc_set_zone_full(devip, zsp);
3900 		return;
3901 	}
3902 
3903 	while (num) {
3904 		if (lba != zsp->z_wp)
3905 			zsp->z_non_seq_resource = true;
3906 
3907 		end = lba + num;
3908 		if (end >= zend) {
3909 			n = zend - lba;
3910 			zsp->z_wp = zend;
3911 		} else if (end > zsp->z_wp) {
3912 			n = num;
3913 			zsp->z_wp = end;
3914 		} else {
3915 			n = num;
3916 		}
3917 		if (zsp->z_wp >= zend)
3918 			zbc_set_zone_full(devip, zsp);
3919 
3920 		num -= n;
3921 		lba += n;
3922 		if (num) {
3923 			zsp++;
3924 			zend = zsp->z_start + zsp->z_size;
3925 		}
3926 	}
3927 }
3928 
3929 static int check_zbc_access_params(struct scsi_cmnd *scp,
3930 			unsigned long long lba, unsigned int num, bool write)
3931 {
3932 	struct scsi_device *sdp = scp->device;
3933 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
3934 	struct sdeb_zone_state *zsp = zbc_zone(devip, lba);
3935 	struct sdeb_zone_state *zsp_end = zbc_zone(devip, lba + num - 1);
3936 
3937 	if (!write) {
3938 		/* For host-managed, reads cannot cross zone types boundaries */
3939 		if (zsp->z_type != zsp_end->z_type) {
3940 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
3941 					ATTEMPT_TO_READ_INVALID_DATA);
3942 			return check_condition_result;
3943 		}
3944 		return 0;
3945 	}
3946 
3947 	/* Writing into a gap zone is not allowed */
3948 	if (zbc_zone_is_gap(zsp)) {
3949 		mk_sense_buffer(scp, ILLEGAL_REQUEST,
3950 				ATTEMPT_TO_ACCESS_GAP_ZONE);
3951 		return check_condition_result;
3952 	}
3953 
3954 	/* No restrictions for writes within conventional zones */
3955 	if (zbc_zone_is_conv(zsp)) {
3956 		if (!zbc_zone_is_conv(zsp_end)) {
3957 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
3958 					WRITE_BOUNDARY_VIOLATION);
3959 			return check_condition_result;
3960 		}
3961 		return 0;
3962 	}
3963 
3964 	if (zsp->z_type == ZBC_ZTYPE_SWR) {
3965 		/* Writes cannot cross sequential zone boundaries */
3966 		if (zsp_end != zsp) {
3967 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
3968 					WRITE_BOUNDARY_VIOLATION);
3969 			return check_condition_result;
3970 		}
3971 		/* Cannot write full zones */
3972 		if (zsp->z_cond == ZC5_FULL) {
3973 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
3974 					INVALID_FIELD_IN_CDB);
3975 			return check_condition_result;
3976 		}
3977 		/* Writes must be aligned to the zone WP */
3978 		if (lba != zsp->z_wp) {
3979 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
3980 					UNALIGNED_WRITE_COMMAND);
3981 			return check_condition_result;
3982 		}
3983 		/*
3984 		 * Writes must end on a physical block boundary, that is, the
3985 		 * transfer length must be a multiple of the physical block
3986 		 * size.
3987 		 */
3988 		if (!IS_ALIGNED(lba + num, 1U << sdebug_physblk_exp)) {
3989 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
3990 					UNALIGNED_WRITE_COMMAND);
3991 			return check_condition_result;
3992 		}
3993 	}
3994 
3995 	/* Handle implicit open of closed and empty zones */
3996 	if (zsp->z_cond == ZC1_EMPTY || zsp->z_cond == ZC4_CLOSED) {
3997 		if (devip->max_open &&
3998 		    devip->nr_exp_open >= devip->max_open) {
3999 			mk_sense_buffer(scp, DATA_PROTECT,
4000 					INSUFFICIENT_ZONE_RESOURCES);
4001 			return check_condition_result;
4002 		}
4003 		zbc_open_zone(devip, zsp, false);
4004 	}
4005 
4006 	return 0;
4007 }
4008 
4009 static inline int check_device_access_params
4010 			(struct scsi_cmnd *scp, unsigned long long lba,
4011 			 unsigned int num, bool write)
4012 {
4013 	struct scsi_device *sdp = scp->device;
4014 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
4015 
4016 	if (lba + num > sdebug_capacity) {
4017 		mk_sense_buffer(scp, ILLEGAL_REQUEST,
4018 				LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE);
4019 		return check_condition_result;
4020 	}
4021 	/* transfer length excessive (tie in to block limits VPD page) */
4022 	if (num > sdebug_store_sectors) {
4023 		/* needs work to find which cdb byte 'num' comes from */
4024 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
4025 		return check_condition_result;
4026 	}
4027 	if (write && unlikely(sdebug_wp)) {
4028 		mk_sense_buffer(scp, DATA_PROTECT, LU_SOFTWARE_WRITE_PROTECTED);
4029 		return check_condition_result;
4030 	}
4031 	if (sdebug_dev_is_zoned(devip))
4032 		return check_zbc_access_params(scp, lba, num, write);
4033 
4034 	return 0;
4035 }
4036 
4037 /*
4038  * Note: if BUG_ON() fires it usually indicates a problem with the parser
4039  * tables. Perhaps a missing F_FAKE_RW or FF_MEDIA_IO flag. Response functions
4040  * that access any of the "stores" in struct sdeb_store_info should call this
4041  * function with bug_if_fake_rw set to true.
4042  */
4043 static inline struct sdeb_store_info *devip2sip(struct sdebug_dev_info *devip,
4044 						bool bug_if_fake_rw)
4045 {
4046 	if (sdebug_fake_rw) {
4047 		BUG_ON(bug_if_fake_rw);	/* See note above */
4048 		return NULL;
4049 	}
4050 	return xa_load(per_store_ap, devip->sdbg_host->si_idx);
4051 }
4052 
4053 static inline void
4054 sdeb_read_lock(rwlock_t *lock)
4055 {
4056 	if (sdebug_no_rwlock)
4057 		__acquire(lock);
4058 	else
4059 		read_lock(lock);
4060 }
4061 
4062 static inline void
4063 sdeb_read_unlock(rwlock_t *lock)
4064 {
4065 	if (sdebug_no_rwlock)
4066 		__release(lock);
4067 	else
4068 		read_unlock(lock);
4069 }
4070 
4071 static inline void
4072 sdeb_write_lock(rwlock_t *lock)
4073 {
4074 	if (sdebug_no_rwlock)
4075 		__acquire(lock);
4076 	else
4077 		write_lock(lock);
4078 }
4079 
4080 static inline void
4081 sdeb_write_unlock(rwlock_t *lock)
4082 {
4083 	if (sdebug_no_rwlock)
4084 		__release(lock);
4085 	else
4086 		write_unlock(lock);
4087 }
4088 
4089 static inline void
4090 sdeb_data_read_lock(struct sdeb_store_info *sip)
4091 {
4092 	BUG_ON(!sip);
4093 
4094 	sdeb_read_lock(&sip->macc_data_lck);
4095 }
4096 
4097 static inline void
4098 sdeb_data_read_unlock(struct sdeb_store_info *sip)
4099 {
4100 	BUG_ON(!sip);
4101 
4102 	sdeb_read_unlock(&sip->macc_data_lck);
4103 }
4104 
4105 static inline void
4106 sdeb_data_write_lock(struct sdeb_store_info *sip)
4107 {
4108 	BUG_ON(!sip);
4109 
4110 	sdeb_write_lock(&sip->macc_data_lck);
4111 }
4112 
4113 static inline void
4114 sdeb_data_write_unlock(struct sdeb_store_info *sip)
4115 {
4116 	BUG_ON(!sip);
4117 
4118 	sdeb_write_unlock(&sip->macc_data_lck);
4119 }
4120 
4121 static inline void
4122 sdeb_data_sector_read_lock(struct sdeb_store_info *sip)
4123 {
4124 	BUG_ON(!sip);
4125 
4126 	sdeb_read_lock(&sip->macc_sector_lck);
4127 }
4128 
4129 static inline void
4130 sdeb_data_sector_read_unlock(struct sdeb_store_info *sip)
4131 {
4132 	BUG_ON(!sip);
4133 
4134 	sdeb_read_unlock(&sip->macc_sector_lck);
4135 }
4136 
4137 static inline void
4138 sdeb_data_sector_write_lock(struct sdeb_store_info *sip)
4139 {
4140 	BUG_ON(!sip);
4141 
4142 	sdeb_write_lock(&sip->macc_sector_lck);
4143 }
4144 
4145 static inline void
4146 sdeb_data_sector_write_unlock(struct sdeb_store_info *sip)
4147 {
4148 	BUG_ON(!sip);
4149 
4150 	sdeb_write_unlock(&sip->macc_sector_lck);
4151 }
4152 
4153 /*
4154  * Atomic locking:
4155  * We simplify the atomic model to allow only 1x atomic write and many non-
4156  * atomic reads or writes for all LBAs.
4157 
4158  * A RW lock has a similar bahaviour:
4159  * Only 1x writer and many readers.
4160 
4161  * So use a RW lock for per-device read and write locking:
4162  * An atomic access grabs the lock as a writer and non-atomic grabs the lock
4163  * as a reader.
4164  */
4165 
4166 static inline void
4167 sdeb_data_lock(struct sdeb_store_info *sip, bool atomic)
4168 {
4169 	if (atomic)
4170 		sdeb_data_write_lock(sip);
4171 	else
4172 		sdeb_data_read_lock(sip);
4173 }
4174 
4175 static inline void
4176 sdeb_data_unlock(struct sdeb_store_info *sip, bool atomic)
4177 {
4178 	if (atomic)
4179 		sdeb_data_write_unlock(sip);
4180 	else
4181 		sdeb_data_read_unlock(sip);
4182 }
4183 
4184 /* Allow many reads but only 1x write per sector */
4185 static inline void
4186 sdeb_data_sector_lock(struct sdeb_store_info *sip, bool do_write)
4187 {
4188 	if (do_write)
4189 		sdeb_data_sector_write_lock(sip);
4190 	else
4191 		sdeb_data_sector_read_lock(sip);
4192 }
4193 
4194 static inline void
4195 sdeb_data_sector_unlock(struct sdeb_store_info *sip, bool do_write)
4196 {
4197 	if (do_write)
4198 		sdeb_data_sector_write_unlock(sip);
4199 	else
4200 		sdeb_data_sector_read_unlock(sip);
4201 }
4202 
4203 static inline void
4204 sdeb_meta_read_lock(struct sdeb_store_info *sip)
4205 {
4206 	if (sdebug_no_rwlock) {
4207 		if (sip)
4208 			__acquire(&sip->macc_meta_lck);
4209 		else
4210 			__acquire(&sdeb_fake_rw_lck);
4211 	} else {
4212 		if (sip)
4213 			read_lock(&sip->macc_meta_lck);
4214 		else
4215 			read_lock(&sdeb_fake_rw_lck);
4216 	}
4217 }
4218 
4219 static inline void
4220 sdeb_meta_read_unlock(struct sdeb_store_info *sip)
4221 {
4222 	if (sdebug_no_rwlock) {
4223 		if (sip)
4224 			__release(&sip->macc_meta_lck);
4225 		else
4226 			__release(&sdeb_fake_rw_lck);
4227 	} else {
4228 		if (sip)
4229 			read_unlock(&sip->macc_meta_lck);
4230 		else
4231 			read_unlock(&sdeb_fake_rw_lck);
4232 	}
4233 }
4234 
4235 static inline void
4236 sdeb_meta_write_lock(struct sdeb_store_info *sip)
4237 {
4238 	if (sdebug_no_rwlock) {
4239 		if (sip)
4240 			__acquire(&sip->macc_meta_lck);
4241 		else
4242 			__acquire(&sdeb_fake_rw_lck);
4243 	} else {
4244 		if (sip)
4245 			write_lock(&sip->macc_meta_lck);
4246 		else
4247 			write_lock(&sdeb_fake_rw_lck);
4248 	}
4249 }
4250 
4251 static inline void
4252 sdeb_meta_write_unlock(struct sdeb_store_info *sip)
4253 {
4254 	if (sdebug_no_rwlock) {
4255 		if (sip)
4256 			__release(&sip->macc_meta_lck);
4257 		else
4258 			__release(&sdeb_fake_rw_lck);
4259 	} else {
4260 		if (sip)
4261 			write_unlock(&sip->macc_meta_lck);
4262 		else
4263 			write_unlock(&sdeb_fake_rw_lck);
4264 	}
4265 }
4266 
4267 /* Returns number of bytes copied or -1 if error. */
4268 static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp,
4269 			    u32 sg_skip, u64 lba, u32 num, u8 group_number,
4270 			    bool do_write, bool atomic)
4271 {
4272 	int ret;
4273 	u64 block;
4274 	enum dma_data_direction dir;
4275 	struct scsi_data_buffer *sdb = &scp->sdb;
4276 	struct scsi_device *sdp = scp->device;
4277 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
4278 	u8 *fsp;
4279 	int i, total = 0;
4280 
4281 	/*
4282 	 * Even though reads are inherently atomic (in this driver), we expect
4283 	 * the atomic flag only for writes.
4284 	 */
4285 	if (!do_write && atomic)
4286 		return -1;
4287 
4288 	if (do_write) {
4289 		dir = DMA_TO_DEVICE;
4290 		write_since_sync = true;
4291 	} else {
4292 		dir = DMA_FROM_DEVICE;
4293 	}
4294 
4295 	if (!sdb->length || !sip)
4296 		return 0;
4297 	if (scp->sc_data_direction != dir)
4298 		return -1;
4299 
4300 	if (do_write && group_number < ARRAY_SIZE(writes_by_group_number))
4301 		atomic_long_inc(&writes_by_group_number[group_number]);
4302 
4303 	fsp = sip->storep;
4304 
4305 	/*
4306 	 * A write to a sequential write required zone has to end on a physical
4307 	 * block boundary, so if the data-out buffer does not hold all of the
4308 	 * data that the command asks for, write up to the last whole physical
4309 	 * block that it does hold. What is left over is reported as part of
4310 	 * the residual.
4311 	 */
4312 	if (do_write && sdebug_dev_is_zoned(devip)) {
4313 		struct sdeb_zone_state *zsp = zbc_zone(devip, lba);
4314 
4315 		if (zsp->z_type == ZBC_ZTYPE_SWR) {
4316 			u32 avail = (sdb->length - sg_skip)
4317 				>> ilog2(sdebug_sector_size);
4318 
4319 			if (avail < num)
4320 				num = round_down(avail, 1U << sdebug_physblk_exp);
4321 		}
4322 	}
4323 
4324 	block = do_div(lba, sdebug_store_sectors);
4325 
4326 	/* Only allow 1x atomic write or multiple non-atomic writes at any given time */
4327 	sdeb_data_lock(sip, atomic);
4328 	for (i = 0; i < num; i++) {
4329 		/* We shouldn't need to lock for atomic writes, but do it anyway */
4330 		sdeb_data_sector_lock(sip, do_write);
4331 		ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
4332 		   fsp + (block * sdebug_sector_size),
4333 		   sdebug_sector_size, sg_skip, do_write);
4334 		sdeb_data_sector_unlock(sip, do_write);
4335 		total += ret;
4336 		if (ret != sdebug_sector_size)
4337 			break;
4338 		sg_skip += sdebug_sector_size;
4339 		if (++block >= sdebug_store_sectors)
4340 			block = 0;
4341 	}
4342 	sdeb_data_unlock(sip, atomic);
4343 
4344 	return total;
4345 }
4346 
4347 /* Returns number of bytes copied or -1 if error. */
4348 static int do_dout_fetch(struct scsi_cmnd *scp, u32 num, u8 *doutp)
4349 {
4350 	struct scsi_data_buffer *sdb = &scp->sdb;
4351 
4352 	if (!sdb->length)
4353 		return 0;
4354 	if (scp->sc_data_direction != DMA_TO_DEVICE)
4355 		return -1;
4356 	return sg_copy_buffer(sdb->table.sgl, sdb->table.nents, doutp,
4357 			      num * sdebug_sector_size, 0, true);
4358 }
4359 
4360 /* If sip->storep+lba compares equal to arr(num), then copy top half of
4361  * arr into sip->storep+lba and return true. If comparison fails then
4362  * return false. */
4363 static bool comp_write_worker(struct sdeb_store_info *sip, u64 lba, u32 num,
4364 			      const u8 *arr, bool compare_only)
4365 {
4366 	bool res;
4367 	u64 block, rest = 0;
4368 	u32 store_blks = sdebug_store_sectors;
4369 	u32 lb_size = sdebug_sector_size;
4370 	u8 *fsp = sip->storep;
4371 
4372 	block = do_div(lba, store_blks);
4373 	if (block + num > store_blks)
4374 		rest = block + num - store_blks;
4375 
4376 	res = !memcmp(fsp + (block * lb_size), arr, (num - rest) * lb_size);
4377 	if (!res)
4378 		return res;
4379 	if (rest)
4380 		res = !memcmp(fsp, arr + ((num - rest) * lb_size),
4381 			      rest * lb_size);
4382 	if (!res)
4383 		return res;
4384 	if (compare_only)
4385 		return true;
4386 	arr += num * lb_size;
4387 	memcpy(fsp + (block * lb_size), arr, (num - rest) * lb_size);
4388 	if (rest)
4389 		memcpy(fsp, arr + ((num - rest) * lb_size), rest * lb_size);
4390 	return res;
4391 }
4392 
4393 static __be16 dif_compute_csum(const void *buf, int len)
4394 {
4395 	__be16 csum;
4396 
4397 	if (sdebug_guard)
4398 		csum = (__force __be16)ip_compute_csum(buf, len);
4399 	else
4400 		csum = cpu_to_be16(crc_t10dif(buf, len));
4401 
4402 	return csum;
4403 }
4404 
4405 static int dif_verify(struct t10_pi_tuple *sdt, const void *data,
4406 		      sector_t sector, u32 ei_lba)
4407 {
4408 	__be16 csum = dif_compute_csum(data, sdebug_sector_size);
4409 
4410 	if (sdt->guard_tag != csum) {
4411 		pr_err("GUARD check failed on sector %lu rcvd 0x%04x, data 0x%04x\n",
4412 			(unsigned long)sector,
4413 			be16_to_cpu(sdt->guard_tag),
4414 			be16_to_cpu(csum));
4415 		return 0x01;
4416 	}
4417 	if (sdebug_dif == T10_PI_TYPE1_PROTECTION &&
4418 	    be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
4419 		pr_err("REF check failed on sector %lu\n",
4420 			(unsigned long)sector);
4421 		return 0x03;
4422 	}
4423 	if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
4424 	    be32_to_cpu(sdt->ref_tag) != ei_lba) {
4425 		pr_err("REF check failed on sector %lu\n",
4426 			(unsigned long)sector);
4427 		return 0x03;
4428 	}
4429 	return 0;
4430 }
4431 
4432 static void dif_copy_prot(struct scsi_cmnd *scp, sector_t sector,
4433 			  unsigned int sectors, bool read)
4434 {
4435 	size_t resid;
4436 	void *paddr;
4437 	struct sdeb_store_info *sip = devip2sip((struct sdebug_dev_info *)
4438 						scp->device->hostdata, true);
4439 	struct t10_pi_tuple *dif_storep = sip->dif_storep;
4440 	const void *dif_store_end = dif_storep + sdebug_store_sectors;
4441 	struct sg_mapping_iter miter;
4442 
4443 	/* Bytes of protection data to copy into sgl */
4444 	resid = sectors * sizeof(*dif_storep);
4445 
4446 	sg_miter_start(&miter, scsi_prot_sglist(scp),
4447 		       scsi_prot_sg_count(scp), SG_MITER_ATOMIC |
4448 		       (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG));
4449 
4450 	while (sg_miter_next(&miter) && resid > 0) {
4451 		size_t len = min_t(size_t, miter.length, resid);
4452 		void *start = dif_store(sip, sector);
4453 		size_t rest = 0;
4454 
4455 		if (dif_store_end < start + len)
4456 			rest = start + len - dif_store_end;
4457 
4458 		paddr = miter.addr;
4459 
4460 		if (read)
4461 			memcpy(paddr, start, len - rest);
4462 		else
4463 			memcpy(start, paddr, len - rest);
4464 
4465 		if (rest) {
4466 			if (read)
4467 				memcpy(paddr + len - rest, dif_storep, rest);
4468 			else
4469 				memcpy(dif_storep, paddr + len - rest, rest);
4470 		}
4471 
4472 		sector += len / sizeof(*dif_storep);
4473 		resid -= len;
4474 	}
4475 	sg_miter_stop(&miter);
4476 }
4477 
4478 static int prot_verify_read(struct scsi_cmnd *scp, sector_t start_sec,
4479 			    unsigned int sectors, u32 ei_lba)
4480 {
4481 	int ret = 0;
4482 	unsigned int i;
4483 	sector_t sector;
4484 	struct sdeb_store_info *sip = devip2sip((struct sdebug_dev_info *)
4485 						scp->device->hostdata, true);
4486 	struct t10_pi_tuple *sdt;
4487 
4488 	for (i = 0; i < sectors; i++, ei_lba++) {
4489 		sector = start_sec + i;
4490 		sdt = dif_store(sip, sector);
4491 
4492 		if (sdt->app_tag == cpu_to_be16(0xffff))
4493 			continue;
4494 
4495 		/*
4496 		 * Because scsi_debug acts as both initiator and
4497 		 * target we proceed to verify the PI even if
4498 		 * RDPROTECT=3. This is done so the "initiator" knows
4499 		 * which type of error to return. Otherwise we would
4500 		 * have to iterate over the PI twice.
4501 		 */
4502 		if (scp->cmnd[1] >> 5) { /* RDPROTECT */
4503 			ret = dif_verify(sdt, lba2fake_store(sip, sector),
4504 					 sector, ei_lba);
4505 			if (ret) {
4506 				dif_errors++;
4507 				break;
4508 			}
4509 		}
4510 	}
4511 
4512 	dif_copy_prot(scp, start_sec, sectors, true);
4513 	dix_reads++;
4514 
4515 	return ret;
4516 }
4517 
4518 static int resp_read_tape(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
4519 {
4520 	u32 i, num, transfer, size;
4521 	u8 *cmd = scp->cmnd;
4522 	struct scsi_data_buffer *sdb = &scp->sdb;
4523 	int partition = devip->tape_partition;
4524 	u32 pos = devip->tape_location[partition];
4525 	struct tape_block *blp;
4526 	bool fixed, sili;
4527 
4528 	if (cmd[0] != READ_6) { /* Only Read(6) supported */
4529 		mk_sense_invalid_opcode(scp);
4530 		return illegal_condition_result;
4531 	}
4532 	fixed = (cmd[1] & 0x1) != 0;
4533 	sili = (cmd[1] & 0x2) != 0;
4534 	if (fixed && sili) {
4535 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1);
4536 		return check_condition_result;
4537 	}
4538 
4539 	transfer = get_unaligned_be24(cmd + 2);
4540 	if (fixed) {
4541 		num = transfer;
4542 		size = devip->tape_blksize;
4543 	} else {
4544 		if (transfer < TAPE_MIN_BLKSIZE ||
4545 			transfer > TAPE_MAX_BLKSIZE) {
4546 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
4547 			return check_condition_result;
4548 		}
4549 		num = 1;
4550 		size = transfer;
4551 	}
4552 
4553 	for (i = 0, blp = devip->tape_blocks[partition] + pos;
4554 	     i < num && pos < devip->tape_eop[partition];
4555 	     i++, pos++, blp++) {
4556 		devip->tape_location[partition] = pos + 1;
4557 		if (IS_TAPE_BLOCK_FM(blp->fl_size)) {
4558 			mk_sense_info_tape(scp, NO_SENSE, FILEMARK_DETECTED,
4559 					   fixed ? num - i : size,
4560 					   SENSE_FLAG_FILEMARK);
4561 			scsi_set_resid(scp, (num - i) * size);
4562 			return check_condition_result;
4563 		}
4564 		/* Assume no REW */
4565 		if (IS_TAPE_BLOCK_EOD(blp->fl_size)) {
4566 			mk_sense_info_tape(scp, BLANK_CHECK,
4567 					   END_OF_DATA_DETECTED,
4568 					   fixed ? num - i : size, 0);
4569 			devip->tape_location[partition] = pos;
4570 			scsi_set_resid(scp, (num - i) * size);
4571 			return check_condition_result;
4572 		}
4573 		sg_zero_buffer(sdb->table.sgl, sdb->table.nents,
4574 			size, i * size);
4575 		sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
4576 			&(blp->data), 4, i * size, false);
4577 		if (fixed) {
4578 			if (blp->fl_size != devip->tape_blksize) {
4579 				scsi_set_resid(scp, (num - i) * size);
4580 				mk_sense_info_tape(scp, NO_SENSE,
4581 						NO_ADDITIONAL_SENSE_INFORMATION,
4582 						num - i, SENSE_FLAG_ILI);
4583 				return check_condition_result;
4584 			}
4585 		} else {
4586 			if (blp->fl_size != size) {
4587 				if (blp->fl_size < size)
4588 					scsi_set_resid(scp, size - blp->fl_size);
4589 				if (!sili) {
4590 					mk_sense_info_tape(scp, NO_SENSE,
4591 						NO_ADDITIONAL_SENSE_INFORMATION,
4592 						size - blp->fl_size,
4593 						SENSE_FLAG_ILI);
4594 					return check_condition_result;
4595 				}
4596 			}
4597 		}
4598 	}
4599 	if (pos >= devip->tape_eop[partition]) {
4600 		mk_sense_info_tape(scp, NO_SENSE,
4601 				   END_OF_PARTITION_MEDIUM_DETECTED,
4602 				   fixed ? num - i : size, SENSE_FLAG_EOM);
4603 		devip->tape_location[partition] = pos - 1;
4604 		return check_condition_result;
4605 	}
4606 	devip->tape_location[partition] = pos;
4607 
4608 	return 0;
4609 }
4610 
4611 static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
4612 {
4613 	bool check_prot;
4614 	u32 num;
4615 	u32 ei_lba;
4616 	int ret;
4617 	u64 lba;
4618 	struct sdeb_store_info *sip = devip2sip(devip, true);
4619 	u8 *cmd = scp->cmnd;
4620 	bool meta_data_locked = false;
4621 
4622 	switch (cmd[0]) {
4623 	case READ_16:
4624 		ei_lba = 0;
4625 		lba = get_unaligned_be64(cmd + 2);
4626 		num = get_unaligned_be32(cmd + 10);
4627 		check_prot = true;
4628 		break;
4629 	case READ_10:
4630 		ei_lba = 0;
4631 		lba = get_unaligned_be32(cmd + 2);
4632 		num = get_unaligned_be16(cmd + 7);
4633 		check_prot = true;
4634 		break;
4635 	case READ_6:
4636 		ei_lba = 0;
4637 		lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
4638 		      (u32)(cmd[1] & 0x1f) << 16;
4639 		num = (0 == cmd[4]) ? 256 : cmd[4];
4640 		check_prot = true;
4641 		break;
4642 	case READ_12:
4643 		ei_lba = 0;
4644 		lba = get_unaligned_be32(cmd + 2);
4645 		num = get_unaligned_be32(cmd + 6);
4646 		check_prot = true;
4647 		break;
4648 	case XDWRITEREAD_10:
4649 		ei_lba = 0;
4650 		lba = get_unaligned_be32(cmd + 2);
4651 		num = get_unaligned_be16(cmd + 7);
4652 		check_prot = false;
4653 		break;
4654 	default:	/* assume READ(32) */
4655 		lba = get_unaligned_be64(cmd + 12);
4656 		ei_lba = get_unaligned_be32(cmd + 20);
4657 		num = get_unaligned_be32(cmd + 28);
4658 		check_prot = false;
4659 		break;
4660 	}
4661 	if (unlikely(have_dif_prot && check_prot)) {
4662 		if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
4663 		    (cmd[1] & 0xe0)) {
4664 			mk_sense_invalid_opcode(scp);
4665 			return check_condition_result;
4666 		}
4667 		if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
4668 		     sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
4669 		    (cmd[1] & 0xe0) == 0)
4670 			sdev_printk(KERN_ERR, scp->device, "Unprotected RD "
4671 				    "to DIF device\n");
4672 	}
4673 	if (unlikely((sdebug_opts & SDEBUG_OPT_SHORT_TRANSFER) &&
4674 		     atomic_read(&sdeb_inject_pending))) {
4675 		num /= 2;
4676 		atomic_set(&sdeb_inject_pending, 0);
4677 	}
4678 
4679 	/*
4680 	 * When checking device access params, for reads we only check data
4681 	 * versus what is set at init time, so no need to lock.
4682 	 */
4683 	ret = check_device_access_params(scp, lba, num, false);
4684 	if (ret)
4685 		return ret;
4686 	if (unlikely((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) &&
4687 		     (lba <= (sdebug_medium_error_start + sdebug_medium_error_count - 1)) &&
4688 		     ((lba + num) > sdebug_medium_error_start))) {
4689 		mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERROR);
4690 		/* set info field and valid bit for fixed descriptor */
4691 		if (0x70 == (scp->sense_buffer[0] & 0x7f)) {
4692 			scp->sense_buffer[0] |= 0x80;	/* Valid bit */
4693 			ret = (lba < OPT_MEDIUM_ERR_ADDR)
4694 			      ? OPT_MEDIUM_ERR_ADDR : (int)lba;
4695 			put_unaligned_be32(ret, scp->sense_buffer + 3);
4696 		}
4697 		scsi_set_resid(scp, scsi_bufflen(scp));
4698 		return check_condition_result;
4699 	}
4700 
4701 	if (sdebug_dev_is_zoned(devip) ||
4702 	    (sdebug_dix && scsi_prot_sg_count(scp)))  {
4703 		sdeb_meta_read_lock(sip);
4704 		meta_data_locked = true;
4705 	}
4706 
4707 	/* DIX + T10 DIF */
4708 	if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
4709 		switch (prot_verify_read(scp, lba, num, ei_lba)) {
4710 		case 1: /* Guard tag error */
4711 			if (cmd[1] >> 5 != 3) { /* RDPROTECT != 3 */
4712 				sdeb_meta_read_unlock(sip);
4713 				mk_sense_buffer(scp, ABORTED_COMMAND,
4714 					LOGICAL_BLOCK_GUARD_CHECK_FAILED);
4715 				return check_condition_result;
4716 			} else if (scp->prot_flags & SCSI_PROT_GUARD_CHECK) {
4717 				sdeb_meta_read_unlock(sip);
4718 				mk_sense_buffer(scp, ILLEGAL_REQUEST,
4719 					LOGICAL_BLOCK_GUARD_CHECK_FAILED);
4720 				return illegal_condition_result;
4721 			}
4722 			break;
4723 		case 3: /* Reference tag error */
4724 			if (cmd[1] >> 5 != 3) { /* RDPROTECT != 3 */
4725 				sdeb_meta_read_unlock(sip);
4726 				mk_sense_buffer(scp, ABORTED_COMMAND,
4727 				      LOGICAL_BLOCK_REFERENCE_TAG_CHECK_FAILED);
4728 				return check_condition_result;
4729 			} else if (scp->prot_flags & SCSI_PROT_REF_CHECK) {
4730 				sdeb_meta_read_unlock(sip);
4731 				mk_sense_buffer(scp, ILLEGAL_REQUEST,
4732 				      LOGICAL_BLOCK_REFERENCE_TAG_CHECK_FAILED);
4733 				return illegal_condition_result;
4734 			}
4735 			break;
4736 		}
4737 	}
4738 
4739 	ret = do_device_access(sip, scp, 0, lba, num, 0, false, false);
4740 	if (meta_data_locked)
4741 		sdeb_meta_read_unlock(sip);
4742 	if (unlikely(ret == -1))
4743 		return DID_ERROR << 16;
4744 
4745 	scsi_set_resid(scp, scsi_bufflen(scp) - ret);
4746 
4747 	if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) &&
4748 		     atomic_read(&sdeb_inject_pending))) {
4749 		if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) {
4750 			mk_sense_buffer(scp, RECOVERED_ERROR,
4751 					FAILURE_PREDICTION_THRESHOLD_EXCEEDED);
4752 			atomic_set(&sdeb_inject_pending, 0);
4753 			return check_condition_result;
4754 		} else if (sdebug_opts & SDEBUG_OPT_DIF_ERR) {
4755 			mk_sense_buffer(scp, ABORTED_COMMAND,
4756 					LOGICAL_BLOCK_GUARD_CHECK_FAILED);
4757 			atomic_set(&sdeb_inject_pending, 0);
4758 			return illegal_condition_result;
4759 		} else if (SDEBUG_OPT_DIX_ERR & sdebug_opts) {
4760 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
4761 					LOGICAL_BLOCK_GUARD_CHECK_FAILED);
4762 			atomic_set(&sdeb_inject_pending, 0);
4763 			return illegal_condition_result;
4764 		}
4765 	}
4766 	return 0;
4767 }
4768 
4769 static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec,
4770 			     unsigned int sectors, u32 ei_lba)
4771 {
4772 	int ret;
4773 	struct t10_pi_tuple *sdt;
4774 	void *daddr;
4775 	sector_t sector = start_sec;
4776 	int ppage_offset;
4777 	int dpage_offset;
4778 	struct sg_mapping_iter diter;
4779 	struct sg_mapping_iter piter;
4780 
4781 	BUG_ON(scsi_sg_count(SCpnt) == 0);
4782 	BUG_ON(scsi_prot_sg_count(SCpnt) == 0);
4783 
4784 	sg_miter_start(&piter, scsi_prot_sglist(SCpnt),
4785 			scsi_prot_sg_count(SCpnt),
4786 			SG_MITER_ATOMIC | SG_MITER_FROM_SG);
4787 	sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt),
4788 			SG_MITER_ATOMIC | SG_MITER_FROM_SG);
4789 
4790 	/* For each protection page */
4791 	while (sg_miter_next(&piter)) {
4792 		dpage_offset = 0;
4793 		if (WARN_ON(!sg_miter_next(&diter))) {
4794 			ret = 0x01;
4795 			goto out;
4796 		}
4797 
4798 		for (ppage_offset = 0; ppage_offset < piter.length;
4799 		     ppage_offset += sizeof(struct t10_pi_tuple)) {
4800 			/* If we're at the end of the current
4801 			 * data page advance to the next one
4802 			 */
4803 			if (dpage_offset >= diter.length) {
4804 				if (WARN_ON(!sg_miter_next(&diter))) {
4805 					ret = 0x01;
4806 					goto out;
4807 				}
4808 				dpage_offset = 0;
4809 			}
4810 
4811 			sdt = piter.addr + ppage_offset;
4812 			daddr = diter.addr + dpage_offset;
4813 
4814 			if (SCpnt->cmnd[1] >> 5 != 3) { /* WRPROTECT */
4815 				ret = dif_verify(sdt, daddr, sector, ei_lba);
4816 				if (ret)
4817 					goto out;
4818 			}
4819 
4820 			sector++;
4821 			ei_lba++;
4822 			dpage_offset += sdebug_sector_size;
4823 		}
4824 		diter.consumed = dpage_offset;
4825 		sg_miter_stop(&diter);
4826 	}
4827 	sg_miter_stop(&piter);
4828 
4829 	dif_copy_prot(SCpnt, start_sec, sectors, false);
4830 	dix_writes++;
4831 
4832 	return 0;
4833 
4834 out:
4835 	dif_errors++;
4836 	sg_miter_stop(&diter);
4837 	sg_miter_stop(&piter);
4838 	return ret;
4839 }
4840 
4841 static unsigned long lba_to_map_index(sector_t lba)
4842 {
4843 	if (sdebug_unmap_alignment)
4844 		lba += sdebug_unmap_granularity - sdebug_unmap_alignment;
4845 	sector_div(lba, sdebug_unmap_granularity);
4846 	return lba;
4847 }
4848 
4849 static sector_t map_index_to_lba(unsigned long index)
4850 {
4851 	sector_t lba = index * sdebug_unmap_granularity;
4852 
4853 	if (sdebug_unmap_alignment)
4854 		lba -= sdebug_unmap_granularity - sdebug_unmap_alignment;
4855 	return lba;
4856 }
4857 
4858 static unsigned int map_state(struct sdeb_store_info *sip, sector_t lba,
4859 			      unsigned int *num)
4860 {
4861 	sector_t end;
4862 	unsigned int mapped;
4863 	unsigned long index;
4864 	unsigned long next;
4865 
4866 	index = lba_to_map_index(lba);
4867 	mapped = test_bit(index, sip->map_storep);
4868 
4869 	if (mapped)
4870 		next = find_next_zero_bit(sip->map_storep, map_size, index);
4871 	else
4872 		next = find_next_bit(sip->map_storep, map_size, index);
4873 
4874 	end = min_t(sector_t, sdebug_store_sectors,  map_index_to_lba(next));
4875 	*num = end - lba;
4876 	return mapped;
4877 }
4878 
4879 /*
4880  * Callers may map the whole range of a write without checking how much of
4881  * it was written. SBC-6 4.7.4.6.2 allows a deallocated LBA to become mapped
4882  * at any time, and only requires an LBA that was written to be mapped.
4883  */
4884 static void map_region(struct sdeb_store_info *sip, sector_t lba,
4885 		       unsigned int len)
4886 {
4887 	sector_t end = lba + len;
4888 
4889 	while (lba < end) {
4890 		unsigned long index = lba_to_map_index(lba);
4891 
4892 		if (index < map_size)
4893 			set_bit(index, sip->map_storep);
4894 
4895 		lba = map_index_to_lba(index + 1);
4896 	}
4897 }
4898 
4899 static void unmap_region(struct sdeb_store_info *sip, sector_t lba,
4900 			 unsigned int len)
4901 {
4902 	sector_t end = lba + len;
4903 	u8 *fsp = sip->storep;
4904 
4905 	while (lba < end) {
4906 		unsigned long index = lba_to_map_index(lba);
4907 
4908 		if (lba == map_index_to_lba(index) &&
4909 		    lba + sdebug_unmap_granularity <= end &&
4910 		    index < map_size) {
4911 			clear_bit(index, sip->map_storep);
4912 			if (sdebug_lbprz) {  /* for LBPRZ=2 return 0xff_s */
4913 				memset(fsp + lba * sdebug_sector_size,
4914 				       (sdebug_lbprz & 1) ? 0 : 0xff,
4915 				       sdebug_sector_size *
4916 				       sdebug_unmap_granularity);
4917 			}
4918 			if (sip->dif_storep) {
4919 				memset(sip->dif_storep + lba, 0xff,
4920 				       sizeof(*sip->dif_storep) *
4921 				       sdebug_unmap_granularity);
4922 			}
4923 		}
4924 		lba = map_index_to_lba(index + 1);
4925 	}
4926 }
4927 
4928 static int resp_write_tape(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
4929 {
4930 	u32 i, num, transfer, size, written = 0;
4931 	u8 *cmd = scp->cmnd;
4932 	struct scsi_data_buffer *sdb = &scp->sdb;
4933 	int partition = devip->tape_partition;
4934 	int pos = devip->tape_location[partition];
4935 	struct tape_block *blp;
4936 	bool fixed, ew;
4937 
4938 	if (cmd[0] != WRITE_6) { /* Only Write(6) supported */
4939 		mk_sense_invalid_opcode(scp);
4940 		return illegal_condition_result;
4941 	}
4942 
4943 	fixed = (cmd[1] & 1) != 0;
4944 	transfer = get_unaligned_be24(cmd + 2);
4945 	if (fixed) {
4946 		num = transfer;
4947 		size = devip->tape_blksize;
4948 	} else {
4949 		if (transfer < TAPE_MIN_BLKSIZE ||
4950 			transfer > TAPE_MAX_BLKSIZE) {
4951 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
4952 			return check_condition_result;
4953 		}
4954 		num = 1;
4955 		size = transfer;
4956 	}
4957 
4958 	scsi_set_resid(scp, num * transfer);
4959 	for (i = 0, blp = devip->tape_blocks[partition] + pos, ew = false;
4960 	     i < num && pos < devip->tape_eop[partition] - 1; i++, pos++, blp++) {
4961 		blp->fl_size = size;
4962 		sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
4963 			&(blp->data), 4, i * size, true);
4964 		written += size;
4965 		scsi_set_resid(scp, num * transfer - written);
4966 		ew |= (pos == devip->tape_eop[partition] - TAPE_EW);
4967 	}
4968 
4969 	devip->tape_location[partition] = pos;
4970 	blp->fl_size = TAPE_BLOCK_EOD_FLAG;
4971 	if (pos >= devip->tape_eop[partition] - 1) {
4972 		mk_sense_info_tape(scp, VOLUME_OVERFLOW,
4973 				   END_OF_PARTITION_MEDIUM_DETECTED,
4974 				   fixed ? num - i : transfer, SENSE_FLAG_EOM);
4975 		return check_condition_result;
4976 	}
4977 	if (ew) { /* early warning */
4978 		mk_sense_info_tape(scp, NO_SENSE,
4979 				   END_OF_PARTITION_MEDIUM_DETECTED,
4980 				   fixed ? num - i : transfer, SENSE_FLAG_EOM);
4981 		return check_condition_result;
4982 	}
4983 
4984 	return 0;
4985 }
4986 
4987 static int corrupt_lbas(struct sdebug_dev_info *devip, u64 lba, u32 num,
4988 		u32 nr_bit_errors, s32 reftag_adjust)
4989 {
4990 	struct sdeb_store_info *sip = devip2sip(devip, false);
4991 	bool meta_data_locked = false;
4992 	bool lbp = scsi_debug_lbp();
4993 	u32 block, num_mapped, b, i;
4994 	int error = 0;
4995 
4996 	if (sdebug_dev_is_zoned(devip) || sdebug_dix || lbp) {
4997 		sdeb_meta_write_lock(sip);
4998 		meta_data_locked = true;
4999 	}
5000 
5001 	if (!sip) {
5002 		pr_err("can't corrupt with fake_rw\n");
5003 		error = -EINVAL;
5004 		goto out_unlock;
5005 	}
5006 
5007 	if (num > sdebug_capacity || lba > sdebug_capacity - num) {
5008 		pr_err("logical blocks out of bounds: %llu:%u", lba, num);
5009 		error = -EINVAL;
5010 		goto out_unlock;
5011 	}
5012 
5013 	if (lbp && (!map_state(sip, lba, &num_mapped) || num > num_mapped)) {
5014 		pr_err("can't modify unmapped logical blocks: %llu:%u",
5015 			lba, num);
5016 		error = -EINVAL;
5017 		goto out_unlock;
5018 	}
5019 
5020 	/*
5021 	 * For each possible bit error, flip the same bit in each corrupted
5022 	 * block to simulate repeated patterns like errors in cables.
5023 	 */
5024 	sdeb_data_lock(sip, false);
5025 	for (b = 0; b < nr_bit_errors; b++) {
5026 		unsigned int bit;
5027 
5028 		bit = get_random_u32_below(sdebug_sector_size * BITS_PER_BYTE);
5029 		pr_info("flipping bit %u in LBAs %llu:%llu\n",
5030 				bit, lba, lba + num - 1);
5031 
5032 		div_u64_rem(lba, sdebug_store_sectors, &block);
5033 		for (i = 0; i < num; i++) {
5034 			u8 *p = sip->storep + (block * sdebug_sector_size);
5035 
5036 			sdeb_data_sector_lock(sip, true);
5037 			p[bit / BITS_PER_BYTE] ^= (1 << (bit % BITS_PER_BYTE));
5038 			sdeb_data_sector_unlock(sip, true);
5039 			if (++block >= sdebug_store_sectors)
5040 				block = 0;
5041 		}
5042 	}
5043 
5044 	if (reftag_adjust) {
5045 		pr_info("adjusting reftag by %d sectors for LBAs %llu:%llu\n",
5046 			reftag_adjust, lba, lba + num - 1);
5047 		for (i = 0; i < num; i++) {
5048 			struct t10_pi_tuple *sdt = dif_store(sip, lba + i);
5049 
5050 			be32_add_cpu(&sdt->ref_tag, reftag_adjust);
5051 		}
5052 	}
5053 	sdeb_data_unlock(sip, false);
5054 
5055 out_unlock:
5056 	if (meta_data_locked)
5057 		sdeb_meta_write_unlock(sip);
5058 	return error;
5059 }
5060 
5061 static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
5062 {
5063 	bool check_prot;
5064 	u32 num;
5065 	u8 group = 0;
5066 	u32 ei_lba;
5067 	int ret;
5068 	u64 lba;
5069 	struct sdeb_store_info *sip = devip2sip(devip, true);
5070 	u8 *cmd = scp->cmnd;
5071 	bool meta_data_locked = false;
5072 	bool lbp = scsi_debug_lbp();
5073 
5074 	if (unlikely(sdebug_opts & SDEBUG_OPT_UNALIGNED_WRITE &&
5075 		     atomic_read(&sdeb_inject_pending))) {
5076 		atomic_set(&sdeb_inject_pending, 0);
5077 		mk_sense_buffer(scp, ILLEGAL_REQUEST, UNALIGNED_WRITE_COMMAND);
5078 		return check_condition_result;
5079 	}
5080 
5081 	switch (cmd[0]) {
5082 	case WRITE_16:
5083 		ei_lba = 0;
5084 		lba = get_unaligned_be64(cmd + 2);
5085 		num = get_unaligned_be32(cmd + 10);
5086 		group = cmd[14] & 0x3f;
5087 		check_prot = true;
5088 		break;
5089 	case WRITE_10:
5090 		ei_lba = 0;
5091 		lba = get_unaligned_be32(cmd + 2);
5092 		group = cmd[6] & 0x3f;
5093 		num = get_unaligned_be16(cmd + 7);
5094 		check_prot = true;
5095 		break;
5096 	case WRITE_6:
5097 		ei_lba = 0;
5098 		lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
5099 		      (u32)(cmd[1] & 0x1f) << 16;
5100 		num = (0 == cmd[4]) ? 256 : cmd[4];
5101 		check_prot = true;
5102 		break;
5103 	case WRITE_12:
5104 		ei_lba = 0;
5105 		lba = get_unaligned_be32(cmd + 2);
5106 		num = get_unaligned_be32(cmd + 6);
5107 		group = cmd[6] & 0x3f;
5108 		check_prot = true;
5109 		break;
5110 	case 0x53:	/* XDWRITEREAD(10) */
5111 		ei_lba = 0;
5112 		lba = get_unaligned_be32(cmd + 2);
5113 		group = cmd[6] & 0x1f;
5114 		num = get_unaligned_be16(cmd + 7);
5115 		check_prot = false;
5116 		break;
5117 	default:	/* assume WRITE(32) */
5118 		group = cmd[6] & 0x3f;
5119 		lba = get_unaligned_be64(cmd + 12);
5120 		ei_lba = get_unaligned_be32(cmd + 20);
5121 		num = get_unaligned_be32(cmd + 28);
5122 		check_prot = false;
5123 		break;
5124 	}
5125 	if (unlikely(have_dif_prot && check_prot)) {
5126 		if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
5127 		    (cmd[1] & 0xe0)) {
5128 			mk_sense_invalid_opcode(scp);
5129 			return check_condition_result;
5130 		}
5131 		if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
5132 		     sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
5133 		    (cmd[1] & 0xe0) == 0)
5134 			sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
5135 				    "to DIF device\n");
5136 	}
5137 
5138 	if (sdebug_dev_is_zoned(devip) ||
5139 	    (sdebug_dix && scsi_prot_sg_count(scp)) || lbp) {
5140 		sdeb_meta_write_lock(sip);
5141 		meta_data_locked = true;
5142 	}
5143 
5144 	ret = check_device_access_params(scp, lba, num, true);
5145 	if (ret) {
5146 		if (meta_data_locked)
5147 			sdeb_meta_write_unlock(sip);
5148 		return ret;
5149 	}
5150 
5151 	/* DIX + T10 DIF */
5152 	if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
5153 		switch (prot_verify_write(scp, lba, num, ei_lba)) {
5154 		case 1: /* Guard tag error */
5155 			if (scp->prot_flags & SCSI_PROT_GUARD_CHECK) {
5156 				sdeb_meta_write_unlock(sip);
5157 				mk_sense_buffer(scp, ILLEGAL_REQUEST,
5158 					LOGICAL_BLOCK_GUARD_CHECK_FAILED);
5159 				return illegal_condition_result;
5160 			} else if (scp->cmnd[1] >> 5 != 3) { /* WRPROTECT != 3 */
5161 				sdeb_meta_write_unlock(sip);
5162 				mk_sense_buffer(scp, ABORTED_COMMAND,
5163 					LOGICAL_BLOCK_GUARD_CHECK_FAILED);
5164 				return check_condition_result;
5165 			}
5166 			break;
5167 		case 3: /* Reference tag error */
5168 			if (scp->prot_flags & SCSI_PROT_REF_CHECK) {
5169 				sdeb_meta_write_unlock(sip);
5170 				mk_sense_buffer(scp, ILLEGAL_REQUEST,
5171 					LOGICAL_BLOCK_REFERENCE_TAG_CHECK_FAILED);
5172 				return illegal_condition_result;
5173 			} else if (scp->cmnd[1] >> 5 != 3) { /* WRPROTECT != 3 */
5174 				sdeb_meta_write_unlock(sip);
5175 				mk_sense_buffer(scp, ABORTED_COMMAND,
5176 					LOGICAL_BLOCK_REFERENCE_TAG_CHECK_FAILED);
5177 				return check_condition_result;
5178 			}
5179 			break;
5180 		}
5181 	}
5182 
5183 	ret = do_device_access(sip, scp, 0, lba, num, group, true, false);
5184 	if (unlikely(lbp))
5185 		map_region(sip, lba, num);
5186 
5187 	/* If ZBC zone then bump its write pointer over the data written */
5188 	if (sdebug_dev_is_zoned(devip) && ret > 0)
5189 		zbc_inc_wp(devip, lba, ret >> ilog2(sdebug_sector_size));
5190 	if (meta_data_locked)
5191 		sdeb_meta_write_unlock(sip);
5192 
5193 	if (unlikely(-1 == ret))
5194 		return DID_ERROR << 16;
5195 	else if (unlikely(sdebug_verbose &&
5196 			  (ret < (num * sdebug_sector_size))))
5197 		sdev_printk(KERN_INFO, scp->device,
5198 			    "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
5199 			    my_name, num * sdebug_sector_size, ret);
5200 
5201 	if (ret < scsi_bufflen(scp))
5202 		scsi_set_resid(scp, scsi_bufflen(scp) - ret);
5203 
5204 	if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) &&
5205 		     atomic_read(&sdeb_inject_pending))) {
5206 		if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) {
5207 			mk_sense_buffer(scp, RECOVERED_ERROR,
5208 					FAILURE_PREDICTION_THRESHOLD_EXCEEDED);
5209 			atomic_set(&sdeb_inject_pending, 0);
5210 			return check_condition_result;
5211 		} else if (sdebug_opts & SDEBUG_OPT_DIF_ERR) {
5212 			mk_sense_buffer(scp, ABORTED_COMMAND,
5213 					LOGICAL_BLOCK_GUARD_CHECK_FAILED);
5214 			atomic_set(&sdeb_inject_pending, 0);
5215 			return illegal_condition_result;
5216 		} else if (sdebug_opts & SDEBUG_OPT_DIX_ERR) {
5217 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
5218 					LOGICAL_BLOCK_GUARD_CHECK_FAILED);
5219 			atomic_set(&sdeb_inject_pending, 0);
5220 			return illegal_condition_result;
5221 		}
5222 	}
5223 	return 0;
5224 }
5225 
5226 /*
5227  * T10 has only specified WRITE SCATTERED(16) and WRITE SCATTERED(32).
5228  * No READ GATHERED yet (requires bidi or long cdb holding gather list).
5229  */
5230 static int resp_write_scat(struct scsi_cmnd *scp,
5231 			   struct sdebug_dev_info *devip)
5232 {
5233 	u8 *cmd = scp->cmnd;
5234 	u8 *lrdp = NULL;
5235 	u8 *up;
5236 	struct sdeb_store_info *sip = devip2sip(devip, true);
5237 	u8 wrprotect;
5238 	u16 lbdof, num_lrd, k;
5239 	u32 num, bt_len, lbdof_blen, cum_lb;
5240 	u64 num_by, sg_off;
5241 	u32 lb_size = sdebug_sector_size;
5242 	u32 ei_lba;
5243 	u64 lba;
5244 	u8 group;
5245 	int ret, res;
5246 	bool is_16;
5247 	static const u32 lrd_size = 32; /* + parameter list header size */
5248 
5249 	if (cmd[0] == VARIABLE_LENGTH_CMD) {
5250 		is_16 = false;
5251 		group = cmd[6] & 0x3f;
5252 		wrprotect = (cmd[10] >> 5) & 0x7;
5253 		lbdof = get_unaligned_be16(cmd + 12);
5254 		num_lrd = get_unaligned_be16(cmd + 16);
5255 		bt_len = get_unaligned_be32(cmd + 28);
5256 	} else {        /* that leaves WRITE SCATTERED(16) */
5257 		is_16 = true;
5258 		wrprotect = (cmd[2] >> 5) & 0x7;
5259 		lbdof = get_unaligned_be16(cmd + 4);
5260 		num_lrd = get_unaligned_be16(cmd + 8);
5261 		bt_len = get_unaligned_be32(cmd + 10);
5262 		group = cmd[14] & 0x3f;
5263 		if (unlikely(have_dif_prot)) {
5264 			if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
5265 			    wrprotect) {
5266 				mk_sense_invalid_opcode(scp);
5267 				return illegal_condition_result;
5268 			}
5269 			if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
5270 			     sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
5271 			     wrprotect == 0)
5272 				sdev_printk(KERN_ERR, scp->device,
5273 					    "Unprotected WR to DIF device\n");
5274 		}
5275 	}
5276 	if (num_lrd == 0 || bt_len == 0) {
5277 		scsi_set_resid(scp, scsi_bufflen(scp));
5278 		return 0;       /* T10 says these do-nothings are not errors */
5279 	}
5280 	if (lbdof == 0) {
5281 		if (sdebug_verbose)
5282 			sdev_printk(KERN_INFO, scp->device,
5283 				"%s: LB Data Offset field bad\n", my_name);
5284 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
5285 		return illegal_condition_result;
5286 	}
5287 	lbdof_blen = lbdof * lb_size;
5288 	if ((lrd_size + (num_lrd * lrd_size)) > lbdof_blen) {
5289 		if (sdebug_verbose)
5290 			sdev_printk(KERN_INFO, scp->device,
5291 				"%s: LBA range descriptors don't fit\n", my_name);
5292 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
5293 		return illegal_condition_result;
5294 	}
5295 	lrdp = kzalloc(lbdof_blen, GFP_ATOMIC | __GFP_NOWARN);
5296 	if (lrdp == NULL)
5297 		return SCSI_MLQUEUE_HOST_BUSY;
5298 	if (sdebug_verbose)
5299 		sdev_printk(KERN_INFO, scp->device,
5300 			"%s: Fetch header+scatter_list, lbdof_blen=%u\n",
5301 			my_name, lbdof_blen);
5302 	res = fetch_to_dev_buffer(scp, lrdp, lbdof_blen);
5303 	if (res == -1) {
5304 		ret = DID_ERROR << 16;
5305 		goto err_out;
5306 	}
5307 
5308 	/* Just keep it simple and always lock for now */
5309 	sdeb_meta_write_lock(sip);
5310 	sg_off = lbdof_blen;
5311 	/* Spec says Buffer xfer Length field in number of LBs in dout */
5312 	cum_lb = 0;
5313 	for (k = 0, up = lrdp + lrd_size; k < num_lrd; ++k, up += lrd_size) {
5314 		lba = get_unaligned_be64(up + 0);
5315 		num = get_unaligned_be32(up + 8);
5316 		if (sdebug_verbose)
5317 			sdev_printk(KERN_INFO, scp->device,
5318 				"%s: k=%d  LBA=0x%llx num=%u  sg_off=%llu\n",
5319 				my_name, k, lba, num, sg_off);
5320 		if (num == 0)
5321 			continue;
5322 		ret = check_device_access_params(scp, lba, num, true);
5323 		if (ret)
5324 			goto err_out_unlock;
5325 		num_by = (u64)num * lb_size;
5326 		ei_lba = is_16 ? 0 : get_unaligned_be32(up + 12);
5327 
5328 		if ((cum_lb + num) > bt_len) {
5329 			if (sdebug_verbose)
5330 				sdev_printk(KERN_INFO, scp->device,
5331 				    "%s: sum of blocks > data provided\n",
5332 				    my_name);
5333 			mk_sense_buffer(scp, ILLEGAL_REQUEST, SSC_WRITE_ERROR);
5334 			ret = illegal_condition_result;
5335 			goto err_out_unlock;
5336 		}
5337 
5338 		/* DIX + T10 DIF */
5339 		if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
5340 			int prot_ret = prot_verify_write(scp, lba, num,
5341 							 ei_lba);
5342 
5343 			if (prot_ret) {
5344 				mk_sense_buffer(scp, ILLEGAL_REQUEST,
5345 					scsi_sense_code(ASC_ID_CRC_OR_ECC_ERROR,
5346 							prot_ret));
5347 				ret = illegal_condition_result;
5348 				goto err_out_unlock;
5349 			}
5350 		}
5351 
5352 		/*
5353 		 * Write ranges atomically to keep as close to pre-atomic
5354 		 * writes behaviour as possible.
5355 		 */
5356 		ret = do_device_access(sip, scp,
5357 				       min_t(u64, sg_off, scsi_bufflen(scp)),
5358 				       lba, num, group, true, true);
5359 		/* If ZBC zone then bump its write pointer over the data written */
5360 		if (sdebug_dev_is_zoned(devip) && ret > 0)
5361 			zbc_inc_wp(devip, lba,
5362 				   ret >> ilog2(sdebug_sector_size));
5363 		if (unlikely(scsi_debug_lbp()))
5364 			map_region(sip, lba, num);
5365 		if (unlikely(-1 == ret)) {
5366 			ret = DID_ERROR << 16;
5367 			goto err_out_unlock;
5368 		} else if (unlikely(sdebug_verbose && (ret < num_by)))
5369 			sdev_printk(KERN_INFO, scp->device,
5370 			    "%s: write: cdb indicated=%llu, IO sent=%d bytes\n",
5371 			    my_name, num_by, ret);
5372 
5373 		if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) &&
5374 			     atomic_read(&sdeb_inject_pending))) {
5375 			/* This range has been written */
5376 			sg_off += num_by;
5377 			if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) {
5378 				mk_sense_buffer(scp, RECOVERED_ERROR,
5379 					FAILURE_PREDICTION_THRESHOLD_EXCEEDED);
5380 				atomic_set(&sdeb_inject_pending, 0);
5381 				ret = check_condition_result;
5382 				goto err_out_unlock;
5383 			} else if (sdebug_opts & SDEBUG_OPT_DIF_ERR) {
5384 				mk_sense_buffer(scp, ABORTED_COMMAND,
5385 					LOGICAL_BLOCK_GUARD_CHECK_FAILED);
5386 				atomic_set(&sdeb_inject_pending, 0);
5387 				ret = illegal_condition_result;
5388 				goto err_out_unlock;
5389 			} else if (sdebug_opts & SDEBUG_OPT_DIX_ERR) {
5390 				mk_sense_buffer(scp, ILLEGAL_REQUEST,
5391 					LOGICAL_BLOCK_GUARD_CHECK_FAILED);
5392 				atomic_set(&sdeb_inject_pending, 0);
5393 				ret = illegal_condition_result;
5394 				goto err_out_unlock;
5395 			}
5396 		}
5397 		sg_off += num_by;
5398 		cum_lb += num;
5399 	}
5400 	ret = 0;
5401 err_out_unlock:
5402 	/*
5403 	 * sg_off counts what the command asked for, which can exceed the
5404 	 * buffer: lbdof is not validated against it, and a range is counted
5405 	 * in full even if do_device_access() copied less.
5406 	 */
5407 	if (scsi_bufflen(scp) > sg_off)
5408 		scsi_set_resid(scp, scsi_bufflen(scp) - sg_off);
5409 	sdeb_meta_write_unlock(sip);
5410 err_out:
5411 	kfree(lrdp);
5412 	return ret;
5413 }
5414 
5415 static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,
5416 			   u32 ei_lba, bool unmap, bool ndob)
5417 {
5418 	struct scsi_device *sdp = scp->device;
5419 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
5420 	unsigned long long i;
5421 	u64 block, lbaa;
5422 	u32 lb_size = sdebug_sector_size;
5423 	int ret;
5424 	struct sdeb_store_info *sip = devip2sip((struct sdebug_dev_info *)
5425 						scp->device->hostdata, true);
5426 	u8 *fs1p;
5427 	u8 *fsp;
5428 	bool meta_data_locked = false;
5429 	bool lbp = scsi_debug_lbp();
5430 
5431 	if (sdebug_dev_is_zoned(devip) || lbp) {
5432 		sdeb_meta_write_lock(sip);
5433 		meta_data_locked = true;
5434 	}
5435 
5436 	ret = check_device_access_params(scp, lba, num, true);
5437 	if (ret)
5438 		goto out;
5439 
5440 	if (unmap && lbp) {
5441 		unmap_region(sip, lba, num);
5442 		goto out;
5443 	}
5444 	lbaa = lba;
5445 	block = do_div(lbaa, sdebug_store_sectors);
5446 	/* if ndob then zero 1 logical block, else fetch 1 logical block */
5447 	fsp = sip->storep;
5448 	fs1p = fsp + (block * lb_size);
5449 	sdeb_data_write_lock(sip);
5450 	if (ndob) {
5451 		memset(fs1p, 0, lb_size);
5452 		ret = 0;
5453 	} else
5454 		ret = fetch_to_dev_buffer(scp, fs1p, lb_size);
5455 
5456 	if (-1 == ret) {
5457 		ret = DID_ERROR << 16;
5458 		goto out;
5459 	} else if (sdebug_verbose && !ndob && (ret < lb_size))
5460 		sdev_printk(KERN_INFO, scp->device,
5461 			    "%s: %s: lb size=%u, IO sent=%d bytes\n",
5462 			    my_name, "write same", lb_size, ret);
5463 
5464 	/* Copy first sector to remaining blocks */
5465 	for (i = 1 ; i < num ; i++) {
5466 		lbaa = lba + i;
5467 		block = do_div(lbaa, sdebug_store_sectors);
5468 		memmove(fsp + (block * lb_size), fs1p, lb_size);
5469 	}
5470 	if (lbp)
5471 		map_region(sip, lba, num);
5472 	/* If ZBC zone then bump its write pointer */
5473 	if (sdebug_dev_is_zoned(devip))
5474 		zbc_inc_wp(devip, lba, num);
5475 	sdeb_data_write_unlock(sip);
5476 	ret = 0;
5477 out:
5478 	if (meta_data_locked)
5479 		sdeb_meta_write_unlock(sip);
5480 	return ret;
5481 }
5482 
5483 static int resp_write_same_10(struct scsi_cmnd *scp,
5484 			      struct sdebug_dev_info *devip)
5485 {
5486 	u8 *cmd = scp->cmnd;
5487 	u32 lba;
5488 	u16 num;
5489 	u32 ei_lba = 0;
5490 	bool unmap = false;
5491 
5492 	if (cmd[1] & 0x8) {
5493 		if (sdebug_lbpws10 == 0) {
5494 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
5495 			return check_condition_result;
5496 		} else
5497 			unmap = true;
5498 	}
5499 	lba = get_unaligned_be32(cmd + 2);
5500 	num = get_unaligned_be16(cmd + 7);
5501 	if (num > sdebug_write_same_length) {
5502 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
5503 		return check_condition_result;
5504 	}
5505 	return resp_write_same(scp, lba, num, ei_lba, unmap, false);
5506 }
5507 
5508 static int resp_write_same_16(struct scsi_cmnd *scp,
5509 			      struct sdebug_dev_info *devip)
5510 {
5511 	u8 *cmd = scp->cmnd;
5512 	u64 lba;
5513 	u32 num;
5514 	u32 ei_lba = 0;
5515 	bool unmap = false;
5516 	bool ndob = false;
5517 
5518 	if (cmd[1] & 0x8) {	/* UNMAP */
5519 		if (sdebug_lbpws == 0) {
5520 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
5521 			return check_condition_result;
5522 		} else
5523 			unmap = true;
5524 	}
5525 	if (cmd[1] & 0x1)  /* NDOB (no data-out buffer, assumes zeroes) */
5526 		ndob = true;
5527 	lba = get_unaligned_be64(cmd + 2);
5528 	num = get_unaligned_be32(cmd + 10);
5529 	if (num > sdebug_write_same_length) {
5530 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1);
5531 		return check_condition_result;
5532 	}
5533 	return resp_write_same(scp, lba, num, ei_lba, unmap, ndob);
5534 }
5535 
5536 /* Note the mode field is in the same position as the (lower) service action
5537  * field. For the Report supported operation codes command, SPC-4 suggests
5538  * each mode of this command should be reported separately; for future. */
5539 static int resp_write_buffer(struct scsi_cmnd *scp,
5540 			     struct sdebug_dev_info *devip)
5541 {
5542 	u8 *cmd = scp->cmnd;
5543 	struct scsi_device *sdp = scp->device;
5544 	struct sdebug_dev_info *dp;
5545 	u8 mode;
5546 
5547 	mode = cmd[1] & 0x1f;
5548 	switch (mode) {
5549 	case 0x4:	/* download microcode (MC) and activate (ACT) */
5550 		/* set UAs on this device only */
5551 		set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
5552 		set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm);
5553 		break;
5554 	case 0x5:	/* download MC, save and ACT */
5555 		set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm);
5556 		break;
5557 	case 0x6:	/* download MC with offsets and ACT */
5558 		/* set UAs on most devices (LUs) in this target */
5559 		list_for_each_entry(dp,
5560 				    &devip->sdbg_host->dev_info_list,
5561 				    dev_list)
5562 			if (dp->target == sdp->id) {
5563 				set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm);
5564 				if (devip != dp)
5565 					set_bit(SDEBUG_UA_MICROCODE_CHANGED,
5566 						dp->uas_bm);
5567 			}
5568 		break;
5569 	case 0x7:	/* download MC with offsets, save, and ACT */
5570 		/* set UA on all devices (LUs) in this target */
5571 		list_for_each_entry(dp,
5572 				    &devip->sdbg_host->dev_info_list,
5573 				    dev_list)
5574 			if (dp->target == sdp->id)
5575 				set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET,
5576 					dp->uas_bm);
5577 		break;
5578 	default:
5579 		/* do nothing for this command for other mode values */
5580 		break;
5581 	}
5582 	return 0;
5583 }
5584 
5585 static int resp_comp_write(struct scsi_cmnd *scp,
5586 			   struct sdebug_dev_info *devip)
5587 {
5588 	u8 *cmd = scp->cmnd;
5589 	u8 *arr;
5590 	struct sdeb_store_info *sip = devip2sip(devip, true);
5591 	u64 lba;
5592 	u32 dnum;
5593 	u32 lb_size = sdebug_sector_size;
5594 	u8 num;
5595 	int ret;
5596 	int retval = 0;
5597 
5598 	lba = get_unaligned_be64(cmd + 2);
5599 	num = cmd[13];		/* 1 to a maximum of 255 logical blocks */
5600 	if (0 == num)
5601 		return 0;	/* degenerate case, not an error */
5602 	if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
5603 	    (cmd[1] & 0xe0)) {
5604 		mk_sense_invalid_opcode(scp);
5605 		return check_condition_result;
5606 	}
5607 	if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
5608 	     sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
5609 	    (cmd[1] & 0xe0) == 0)
5610 		sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
5611 			    "to DIF device\n");
5612 	ret = check_device_access_params(scp, lba, num, false);
5613 	if (ret)
5614 		return ret;
5615 	dnum = 2 * num;
5616 	arr = kcalloc(lb_size, dnum, GFP_ATOMIC);
5617 	if (NULL == arr) {
5618 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFFICIENT_RESOURCES);
5619 		return check_condition_result;
5620 	}
5621 
5622 	ret = do_dout_fetch(scp, dnum, arr);
5623 	if (ret == -1) {
5624 		retval = DID_ERROR << 16;
5625 		goto cleanup_free;
5626 	} else if (sdebug_verbose && (ret < (dnum * lb_size)))
5627 		sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb "
5628 			    "indicated=%u, IO sent=%d bytes\n", my_name,
5629 			    dnum * lb_size, ret);
5630 
5631 	sdeb_meta_write_lock(sip);
5632 	sdeb_data_write_lock(sip);
5633 	if (!comp_write_worker(sip, lba, num, arr, false)) {
5634 		mk_sense_buffer(scp, MISCOMPARE,
5635 				MISCOMPARE_DURING_VERIFY_OPERATION);
5636 		retval = check_condition_result;
5637 		goto cleanup_unlock;
5638 	}
5639 
5640 	/* Cover sip->map_storep (which map_region() sets) with the meta lock */
5641 	if (scsi_debug_lbp())
5642 		map_region(sip, lba, num);
5643 cleanup_unlock:
5644 	sdeb_data_write_unlock(sip);
5645 	sdeb_meta_write_unlock(sip);
5646 cleanup_free:
5647 	kfree(arr);
5648 	return retval;
5649 }
5650 
5651 struct unmap_block_desc {
5652 	__be64	lba;
5653 	__be32	blocks;
5654 	__be32	__reserved;
5655 };
5656 
5657 static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
5658 {
5659 	unsigned char *buf;
5660 	struct unmap_block_desc *desc;
5661 	struct sdeb_store_info *sip = devip2sip(devip, true);
5662 	unsigned int i, payload_len, descriptors;
5663 	int ret;
5664 
5665 	if (!scsi_debug_lbp())
5666 		return 0;	/* fib and say its done */
5667 	payload_len = get_unaligned_be16(scp->cmnd + 7);
5668 	BUG_ON(scsi_bufflen(scp) != payload_len);
5669 
5670 	descriptors = (payload_len - 8) / 16;
5671 	if (descriptors > sdebug_unmap_max_desc) {
5672 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
5673 		return check_condition_result;
5674 	}
5675 
5676 	buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC);
5677 	if (!buf) {
5678 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFFICIENT_RESOURCES);
5679 		return check_condition_result;
5680 	}
5681 
5682 	scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
5683 
5684 	BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2);
5685 	BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16);
5686 
5687 	desc = (void *)&buf[8];
5688 
5689 	sdeb_meta_write_lock(sip);
5690 
5691 	for (i = 0 ; i < descriptors ; i++) {
5692 		unsigned long long lba = get_unaligned_be64(&desc[i].lba);
5693 		unsigned int num = get_unaligned_be32(&desc[i].blocks);
5694 
5695 		ret = check_device_access_params(scp, lba, num, true);
5696 		if (ret)
5697 			goto out;
5698 
5699 		unmap_region(sip, lba, num);
5700 	}
5701 
5702 	ret = 0;
5703 
5704 out:
5705 	sdeb_meta_write_unlock(sip);
5706 	kfree(buf);
5707 
5708 	return ret;
5709 }
5710 
5711 #define SDEBUG_GET_LBA_STATUS_LEN 32
5712 
5713 static int resp_get_lba_status(struct scsi_cmnd *scp,
5714 			       struct sdebug_dev_info *devip)
5715 {
5716 	u8 *cmd = scp->cmnd;
5717 	u64 lba;
5718 	u32 alloc_len, mapped, num;
5719 	int ret;
5720 	u8 arr[SDEBUG_GET_LBA_STATUS_LEN];
5721 
5722 	lba = get_unaligned_be64(cmd + 2);
5723 	alloc_len = get_unaligned_be32(cmd + 10);
5724 
5725 	if (alloc_len < 24)
5726 		return 0;
5727 
5728 	ret = check_device_access_params(scp, lba, 1, false);
5729 	if (ret)
5730 		return ret;
5731 
5732 	if (scsi_debug_lbp()) {
5733 		struct sdeb_store_info *sip = devip2sip(devip, true);
5734 
5735 		mapped = map_state(sip, lba, &num);
5736 	} else {
5737 		mapped = 1;
5738 		/* following just in case virtual_gb changed */
5739 		sdebug_capacity = get_sdebug_capacity();
5740 		if (sdebug_capacity - lba <= 0xffffffff)
5741 			num = sdebug_capacity - lba;
5742 		else
5743 			num = 0xffffffff;
5744 	}
5745 
5746 	memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN);
5747 	put_unaligned_be32(20, arr);		/* Parameter Data Length */
5748 	put_unaligned_be64(lba, arr + 8);	/* LBA */
5749 	put_unaligned_be32(num, arr + 16);	/* Number of blocks */
5750 	arr[20] = !mapped;		/* prov_stat=0: mapped; 1: dealloc */
5751 
5752 	return fill_from_dev_buffer(scp, arr, SDEBUG_GET_LBA_STATUS_LEN);
5753 }
5754 
5755 static int resp_get_stream_status(struct scsi_cmnd *scp,
5756 				  struct sdebug_dev_info *devip)
5757 {
5758 	u16 starting_stream_id, stream_id;
5759 	const u8 *cmd = scp->cmnd;
5760 	u32 alloc_len, offset;
5761 	u8 arr[256] = {};
5762 	struct scsi_stream_status_header *h = (void *)arr;
5763 
5764 	starting_stream_id = get_unaligned_be16(cmd + 4);
5765 	alloc_len = get_unaligned_be32(cmd + 10);
5766 
5767 	if (alloc_len < 8) {
5768 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1);
5769 		return check_condition_result;
5770 	}
5771 
5772 	if (starting_stream_id >= MAXIMUM_NUMBER_OF_STREAMS) {
5773 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1);
5774 		return check_condition_result;
5775 	}
5776 
5777 	/*
5778 	 * The GET STREAM STATUS command only reports status information
5779 	 * about open streams. Treat the non-permanent stream as open.
5780 	 */
5781 	put_unaligned_be16(MAXIMUM_NUMBER_OF_STREAMS,
5782 			   &h->number_of_open_streams);
5783 
5784 	for (offset = 8, stream_id = starting_stream_id;
5785 	     offset + 8 <= min_t(u32, alloc_len, sizeof(arr)) &&
5786 		     stream_id < MAXIMUM_NUMBER_OF_STREAMS;
5787 	     offset += 8, stream_id++) {
5788 		struct scsi_stream_status *stream_status = (void *)arr + offset;
5789 
5790 		stream_status->perm = stream_id < PERMANENT_STREAM_COUNT;
5791 		put_unaligned_be16(stream_id,
5792 				   &stream_status->stream_identifier);
5793 		stream_status->rel_lifetime = stream_id + 1;
5794 	}
5795 	put_unaligned_be32(offset - 8, &h->len); /* PARAMETER DATA LENGTH */
5796 
5797 	return fill_from_dev_buffer(scp, arr, min(offset, alloc_len));
5798 }
5799 
5800 static int resp_sync_cache(struct scsi_cmnd *scp,
5801 			   struct sdebug_dev_info *devip)
5802 {
5803 	int res = 0;
5804 	u64 lba;
5805 	u32 num_blocks;
5806 	u8 *cmd = scp->cmnd;
5807 
5808 	if (cmd[0] == SYNCHRONIZE_CACHE) {	/* 10 byte cdb */
5809 		lba = get_unaligned_be32(cmd + 2);
5810 		num_blocks = get_unaligned_be16(cmd + 7);
5811 	} else {				/* SYNCHRONIZE_CACHE(16) */
5812 		lba = get_unaligned_be64(cmd + 2);
5813 		num_blocks = get_unaligned_be32(cmd + 10);
5814 	}
5815 	if (lba + num_blocks > sdebug_capacity) {
5816 		mk_sense_buffer(scp, ILLEGAL_REQUEST,
5817 				LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE);
5818 		return check_condition_result;
5819 	}
5820 	if (!write_since_sync || (cmd[1] & 0x2))
5821 		res = SDEG_RES_IMMED_MASK;
5822 	else		/* delay if write_since_sync and IMMED clear */
5823 		write_since_sync = false;
5824 	return res;
5825 }
5826 
5827 /*
5828  * Assuming the LBA+num_blocks is not out-of-range, this function will return
5829  * CONDITION MET if the specified blocks will/have fitted in the cache, and
5830  * a GOOD status otherwise. Model a disk with a big cache and yield
5831  * CONDITION MET. Actually tries to bring range in main memory into the
5832  * cache associated with the CPU(s).
5833  *
5834  * The pcode 0x34 is also used for READ POSITION by tape devices.
5835  */
5836 static int resp_pre_fetch(struct scsi_cmnd *scp,
5837 			  struct sdebug_dev_info *devip)
5838 {
5839 	int res = 0;
5840 	u64 lba;
5841 	u64 block, rest = 0;
5842 	u32 nblks;
5843 	u8 *cmd = scp->cmnd;
5844 	struct sdeb_store_info *sip = devip2sip(devip, true);
5845 	u8 *fsp = sip->storep;
5846 
5847 	if (cmd[0] == PRE_FETCH) {	/* 10 byte cdb */
5848 		lba = get_unaligned_be32(cmd + 2);
5849 		nblks = get_unaligned_be16(cmd + 7);
5850 	} else {			/* PRE-FETCH(16) */
5851 		lba = get_unaligned_be64(cmd + 2);
5852 		nblks = get_unaligned_be32(cmd + 10);
5853 	}
5854 	if (lba + nblks > sdebug_capacity) {
5855 		mk_sense_buffer(scp, ILLEGAL_REQUEST,
5856 				LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE);
5857 		return check_condition_result;
5858 	}
5859 	if (!fsp)
5860 		goto fini;
5861 	/* PRE-FETCH spec says nothing about LBP or PI so skip them */
5862 	block = do_div(lba, sdebug_store_sectors);
5863 	if (block + nblks > sdebug_store_sectors)
5864 		rest = block + nblks - sdebug_store_sectors;
5865 
5866 	/* Try to bring the PRE-FETCH range into CPU's cache */
5867 	sdeb_data_read_lock(sip);
5868 	prefetch_range(fsp + (sdebug_sector_size * block),
5869 		       (nblks - rest) * sdebug_sector_size);
5870 	if (rest)
5871 		prefetch_range(fsp, rest * sdebug_sector_size);
5872 
5873 	sdeb_data_read_unlock(sip);
5874 fini:
5875 	if (cmd[1] & 0x2)
5876 		res = SDEG_RES_IMMED_MASK;
5877 	return res | condition_met_result;
5878 }
5879 
5880 #define RL_BUCKET_ELEMS 8
5881 
5882 /* Even though each pseudo target has a REPORT LUNS "well known logical unit"
5883  * (W-LUN), the normal Linux scanning logic does not associate it with a
5884  * device (e.g. /dev/sg7). The following magic will make that association:
5885  *   "cd /sys/class/scsi_host/host<n> ; echo '- - 49409' > scan"
5886  * where <n> is a host number. If there are multiple targets in a host then
5887  * the above will associate a W-LUN to each target. To only get a W-LUN
5888  * for target 2, then use "echo '- 2 49409' > scan" .
5889  */
5890 static int resp_report_luns(struct scsi_cmnd *scp,
5891 			    struct sdebug_dev_info *devip)
5892 {
5893 	unsigned char *cmd = scp->cmnd;
5894 	unsigned int alloc_len;
5895 	unsigned char select_report;
5896 	u64 lun;
5897 	struct scsi_lun *lun_p;
5898 	u8 arr[RL_BUCKET_ELEMS * sizeof(struct scsi_lun)];
5899 	unsigned int lun_cnt;	/* normal LUN count (max: 256) */
5900 	unsigned int wlun_cnt;	/* report luns W-LUN count */
5901 	unsigned int tlun_cnt;	/* total LUN count */
5902 	unsigned int rlen;	/* response length (in bytes) */
5903 	int k, j, n, res;
5904 	unsigned int off_rsp = 0;
5905 	const int sz_lun = sizeof(struct scsi_lun);
5906 
5907 	clear_luns_changed_on_target(devip);
5908 
5909 	select_report = cmd[2];
5910 	alloc_len = get_unaligned_be32(cmd + 6);
5911 
5912 	if (alloc_len < 4) {
5913 		pr_err("alloc len too small %d\n", alloc_len);
5914 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
5915 		return check_condition_result;
5916 	}
5917 
5918 	switch (select_report) {
5919 	case 0:		/* all LUNs apart from W-LUNs */
5920 		lun_cnt = sdebug_max_luns;
5921 		wlun_cnt = 0;
5922 		break;
5923 	case 1:		/* only W-LUNs */
5924 		lun_cnt = 0;
5925 		wlun_cnt = 1;
5926 		break;
5927 	case 2:		/* all LUNs */
5928 		lun_cnt = sdebug_max_luns;
5929 		wlun_cnt = 1;
5930 		break;
5931 	case 0x10:	/* only administrative LUs */
5932 	case 0x11:	/* see SPC-5 */
5933 	case 0x12:	/* only subsiduary LUs owned by referenced LU */
5934 	default:
5935 		pr_debug("select report invalid %d\n", select_report);
5936 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
5937 		return check_condition_result;
5938 	}
5939 
5940 	if (sdebug_no_lun_0 && (lun_cnt > 0))
5941 		--lun_cnt;
5942 
5943 	tlun_cnt = lun_cnt + wlun_cnt;
5944 	rlen = tlun_cnt * sz_lun;	/* excluding 8 byte header */
5945 	scsi_set_resid(scp, scsi_bufflen(scp));
5946 	pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n",
5947 		 select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0);
5948 
5949 	/* loops rely on sizeof response header same as sizeof lun (both 8) */
5950 	lun = sdebug_no_lun_0 ? 1 : 0;
5951 	for (k = 0, j = 0, res = 0; true; ++k, j = 0) {
5952 		memset(arr, 0, sizeof(arr));
5953 		lun_p = (struct scsi_lun *)&arr[0];
5954 		if (k == 0) {
5955 			put_unaligned_be32(rlen, &arr[0]);
5956 			++lun_p;
5957 			j = 1;
5958 		}
5959 		for ( ; j < RL_BUCKET_ELEMS; ++j, ++lun_p) {
5960 			if ((k * RL_BUCKET_ELEMS) + j > lun_cnt)
5961 				break;
5962 			int_to_scsilun(lun++, lun_p);
5963 			if (lun > 1 && sdebug_lun_am == SAM_LUN_AM_FLAT)
5964 				lun_p->scsi_lun[0] |= 0x40;
5965 		}
5966 		if (j < RL_BUCKET_ELEMS)
5967 			break;
5968 		n = j * sz_lun;
5969 		res = p_fill_from_dev_buffer(scp, arr, n, off_rsp);
5970 		if (res)
5971 			return res;
5972 		off_rsp += n;
5973 	}
5974 	if (wlun_cnt) {
5975 		int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p);
5976 		++j;
5977 	}
5978 	if (j > 0)
5979 		res = p_fill_from_dev_buffer(scp, arr, j * sz_lun, off_rsp);
5980 	return res;
5981 }
5982 
5983 static int resp_verify(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
5984 {
5985 	bool is_bytchk3 = false;
5986 	u8 bytchk;
5987 	int ret, j;
5988 	u32 vnum, a_num, off;
5989 	const u32 lb_size = sdebug_sector_size;
5990 	u64 lba;
5991 	u8 *arr;
5992 	u8 *cmd = scp->cmnd;
5993 	struct sdeb_store_info *sip = devip2sip(devip, true);
5994 
5995 	bytchk = (cmd[1] >> 1) & 0x3;
5996 	if (bytchk == 0) {
5997 		return 0;	/* always claim internal verify okay */
5998 	} else if (bytchk == 2) {
5999 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2);
6000 		return check_condition_result;
6001 	} else if (bytchk == 3) {
6002 		is_bytchk3 = true;	/* 1 block sent, compared repeatedly */
6003 	}
6004 	switch (cmd[0]) {
6005 	case VERIFY_16:
6006 		lba = get_unaligned_be64(cmd + 2);
6007 		vnum = get_unaligned_be32(cmd + 10);
6008 		break;
6009 	case VERIFY:		/* is VERIFY(10) */
6010 		lba = get_unaligned_be32(cmd + 2);
6011 		vnum = get_unaligned_be16(cmd + 7);
6012 		break;
6013 	default:
6014 		mk_sense_invalid_opcode(scp);
6015 		return check_condition_result;
6016 	}
6017 	if (vnum == 0)
6018 		return 0;	/* not an error */
6019 	a_num = is_bytchk3 ? 1 : vnum;
6020 	/* Treat following check like one for read (i.e. no write) access */
6021 	ret = check_device_access_params(scp, lba, a_num, false);
6022 	if (ret)
6023 		return ret;
6024 
6025 	arr = kcalloc(lb_size, vnum, GFP_ATOMIC | __GFP_NOWARN);
6026 	if (!arr) {
6027 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFFICIENT_RESOURCES);
6028 		return check_condition_result;
6029 	}
6030 	/* Not changing store, so only need read access */
6031 	sdeb_data_read_lock(sip);
6032 
6033 	ret = do_dout_fetch(scp, a_num, arr);
6034 	if (ret == -1) {
6035 		ret = DID_ERROR << 16;
6036 		goto cleanup;
6037 	} else if (sdebug_verbose && (ret < (a_num * lb_size))) {
6038 		sdev_printk(KERN_INFO, scp->device,
6039 			    "%s: cdb indicated=%u, IO sent=%d bytes\n",
6040 			    my_name, a_num * lb_size, ret);
6041 	}
6042 	if (is_bytchk3) {
6043 		for (j = 1, off = lb_size; j < vnum; ++j, off += lb_size)
6044 			memcpy(arr + off, arr, lb_size);
6045 	}
6046 	ret = 0;
6047 	if (!comp_write_worker(sip, lba, vnum, arr, true)) {
6048 		mk_sense_buffer(scp, MISCOMPARE,
6049 				MISCOMPARE_DURING_VERIFY_OPERATION);
6050 		ret = check_condition_result;
6051 		goto cleanup;
6052 	}
6053 cleanup:
6054 	sdeb_data_read_unlock(sip);
6055 	kfree(arr);
6056 	return ret;
6057 }
6058 
6059 #define RZONES_DESC_HD 64
6060 
6061 /* Report zones depending on start LBA and reporting options */
6062 static int resp_report_zones(struct scsi_cmnd *scp,
6063 			     struct sdebug_dev_info *devip)
6064 {
6065 	unsigned int rep_max_zones, nrz = 0;
6066 	int ret = 0;
6067 	u32 alloc_len, rep_opts, rep_len;
6068 	bool partial;
6069 	u64 lba, zs_lba;
6070 	u64 arr_len;
6071 	u8 *arr = NULL, *desc;
6072 	u8 *cmd = scp->cmnd;
6073 	struct sdeb_zone_state *zsp = NULL;
6074 	struct sdeb_store_info *sip = devip2sip(devip, false);
6075 
6076 	if (!sdebug_dev_is_zoned(devip)) {
6077 		mk_sense_invalid_opcode(scp);
6078 		return check_condition_result;
6079 	}
6080 	zs_lba = get_unaligned_be64(cmd + 2);
6081 	alloc_len = get_unaligned_be32(cmd + 10);
6082 	if (alloc_len == 0)
6083 		return 0;	/* not an error */
6084 	rep_opts = cmd[14] & 0x3f;
6085 	partial = cmd[14] & 0x80;
6086 
6087 	if (zs_lba >= sdebug_capacity) {
6088 		mk_sense_buffer(scp, ILLEGAL_REQUEST,
6089 				LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE);
6090 		return check_condition_result;
6091 	}
6092 
6093 	rep_max_zones = (ALIGN((u64)alloc_len, RZONES_DESC_HD) - RZONES_DESC_HD) >>
6094 			ilog2(RZONES_DESC_HD);
6095 	rep_max_zones = min_t(unsigned int, rep_max_zones, devip->nr_zones);
6096 	arr_len = (u64)RZONES_DESC_HD * (rep_max_zones + 1);
6097 
6098 	arr = kzalloc(arr_len, GFP_ATOMIC | __GFP_NOWARN);
6099 	if (!arr) {
6100 		mk_sense_buffer(scp, ILLEGAL_REQUEST,
6101 				INSUFFICIENT_RESOURCES);
6102 		return check_condition_result;
6103 	}
6104 
6105 	sdeb_meta_read_lock(sip);
6106 
6107 	desc = arr + 64;
6108 	for (lba = zs_lba; lba < sdebug_capacity;
6109 	     lba = zsp->z_start + zsp->z_size) {
6110 		if (WARN_ONCE(zbc_zone(devip, lba) == zsp, "lba = %llu\n", lba))
6111 			break;
6112 		zsp = zbc_zone(devip, lba);
6113 		switch (rep_opts) {
6114 		case 0x00:
6115 			/* All zones */
6116 			break;
6117 		case 0x01:
6118 			/* Empty zones */
6119 			if (zsp->z_cond != ZC1_EMPTY)
6120 				continue;
6121 			break;
6122 		case 0x02:
6123 			/* Implicit open zones */
6124 			if (zsp->z_cond != ZC2_IMPLICIT_OPEN)
6125 				continue;
6126 			break;
6127 		case 0x03:
6128 			/* Explicit open zones */
6129 			if (zsp->z_cond != ZC3_EXPLICIT_OPEN)
6130 				continue;
6131 			break;
6132 		case 0x04:
6133 			/* Closed zones */
6134 			if (zsp->z_cond != ZC4_CLOSED)
6135 				continue;
6136 			break;
6137 		case 0x05:
6138 			/* Full zones */
6139 			if (zsp->z_cond != ZC5_FULL)
6140 				continue;
6141 			break;
6142 		case 0x06:
6143 		case 0x07:
6144 		case 0x10:
6145 			/*
6146 			 * Read-only, offline, reset WP recommended are
6147 			 * not emulated: no zones to report;
6148 			 */
6149 			continue;
6150 		case 0x11:
6151 			/* non-seq-resource set */
6152 			if (!zsp->z_non_seq_resource)
6153 				continue;
6154 			break;
6155 		case 0x3e:
6156 			/* All zones except gap zones. */
6157 			if (zbc_zone_is_gap(zsp))
6158 				continue;
6159 			break;
6160 		case 0x3f:
6161 			/* Not write pointer (conventional) zones */
6162 			if (zbc_zone_is_seq(zsp))
6163 				continue;
6164 			break;
6165 		default:
6166 			mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
6167 			ret = check_condition_result;
6168 			goto fini;
6169 		}
6170 
6171 		if (nrz < rep_max_zones) {
6172 			/* Fill zone descriptor */
6173 			desc[0] = zsp->z_type;
6174 			desc[1] = zsp->z_cond << 4;
6175 			if (zsp->z_non_seq_resource)
6176 				desc[1] |= 1 << 1;
6177 			put_unaligned_be64((u64)zsp->z_size, desc + 8);
6178 			put_unaligned_be64((u64)zsp->z_start, desc + 16);
6179 			put_unaligned_be64((u64)zsp->z_wp, desc + 24);
6180 			desc += 64;
6181 		}
6182 
6183 		if (partial && nrz >= rep_max_zones)
6184 			break;
6185 
6186 		nrz++;
6187 	}
6188 
6189 	/* Report header */
6190 	/* Zone list length. */
6191 	put_unaligned_be32(nrz * RZONES_DESC_HD, arr + 0);
6192 	/* Maximum LBA */
6193 	put_unaligned_be64(sdebug_capacity - 1, arr + 8);
6194 	/* Zone starting LBA granularity. */
6195 	if (devip->zcap < devip->zsize)
6196 		put_unaligned_be64(devip->zsize, arr + 16);
6197 
6198 	rep_len = (unsigned long)desc - (unsigned long)arr;
6199 	ret = fill_from_dev_buffer(scp, arr, min_t(u32, alloc_len, rep_len));
6200 
6201 fini:
6202 	sdeb_meta_read_unlock(sip);
6203 	kfree(arr);
6204 	return ret;
6205 }
6206 
6207 static int resp_atomic_write(struct scsi_cmnd *scp,
6208 			     struct sdebug_dev_info *devip)
6209 {
6210 	struct sdeb_store_info *sip;
6211 	u8 *cmd = scp->cmnd;
6212 	u16 boundary, len;
6213 	u64 lba, lba_tmp;
6214 	bool lbp = scsi_debug_lbp();
6215 	int ret;
6216 
6217 	if (!scsi_debug_atomic_write()) {
6218 		mk_sense_invalid_opcode(scp);
6219 		return check_condition_result;
6220 	}
6221 
6222 	sip = devip2sip(devip, true);
6223 
6224 	lba = get_unaligned_be64(cmd + 2);
6225 	boundary = get_unaligned_be16(cmd + 10);
6226 	len = get_unaligned_be16(cmd + 12);
6227 
6228 	lba_tmp = lba;
6229 	if (sdebug_atomic_wr_align &&
6230 	    do_div(lba_tmp, sdebug_atomic_wr_align)) {
6231 		/* Does not meet alignment requirement */
6232 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
6233 		return check_condition_result;
6234 	}
6235 
6236 	if (sdebug_atomic_wr_gran && len % sdebug_atomic_wr_gran) {
6237 		/* Does not meet alignment requirement */
6238 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
6239 		return check_condition_result;
6240 	}
6241 
6242 	if (boundary > 0) {
6243 		if (boundary > sdebug_atomic_wr_max_bndry) {
6244 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 12, -1);
6245 			return check_condition_result;
6246 		}
6247 
6248 		if (len > sdebug_atomic_wr_max_length_bndry) {
6249 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 12, -1);
6250 			return check_condition_result;
6251 		}
6252 	} else {
6253 		if (len > sdebug_atomic_wr_max_length) {
6254 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 12, -1);
6255 			return check_condition_result;
6256 		}
6257 	}
6258 
6259 	ret = check_device_access_params(scp, lba, len, true);
6260 	if (ret)
6261 		return ret;
6262 
6263 	/*
6264 	 * Short atomic writes are not allowed: SBC-6 4.28.2 requires an
6265 	 * atomic write that cannot complete to leave its LBAs unaltered, and
6266 	 * do_device_access() would already have written part of it.
6267 	 */
6268 	if (scsi_bufflen(scp) < len * sdebug_sector_size)
6269 		return DID_ERROR << 16;
6270 
6271 	if (lbp)
6272 		sdeb_meta_write_lock(sip);
6273 
6274 	ret = do_device_access(sip, scp, 0, lba, len, 0, true, true);
6275 	if (lbp) {
6276 		map_region(sip, lba, len);
6277 		sdeb_meta_write_unlock(sip);
6278 	}
6279 
6280 	if (unlikely(ret == -1))
6281 		return DID_ERROR << 16;
6282 	if (unlikely(ret != len * sdebug_sector_size))
6283 		return DID_ERROR << 16;
6284 
6285 	if (ret < scsi_bufflen(scp))
6286 		scsi_set_resid(scp, scsi_bufflen(scp) - ret);
6287 	return 0;
6288 }
6289 
6290 /* Logic transplanted from tcmu-runner, file_zbc.c */
6291 static void zbc_open_all(struct sdebug_dev_info *devip)
6292 {
6293 	struct sdeb_zone_state *zsp = &devip->zstate[0];
6294 	unsigned int i;
6295 
6296 	for (i = 0; i < devip->nr_zones; i++, zsp++) {
6297 		if (zsp->z_cond == ZC4_CLOSED)
6298 			zbc_open_zone(devip, &devip->zstate[i], true);
6299 	}
6300 }
6301 
6302 static int resp_open_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
6303 {
6304 	int res = 0;
6305 	u64 z_id;
6306 	enum sdebug_z_cond zc;
6307 	u8 *cmd = scp->cmnd;
6308 	struct sdeb_zone_state *zsp;
6309 	bool all = cmd[14] & 0x01;
6310 	struct sdeb_store_info *sip = devip2sip(devip, false);
6311 
6312 	if (!sdebug_dev_is_zoned(devip)) {
6313 		mk_sense_invalid_opcode(scp);
6314 		return check_condition_result;
6315 	}
6316 	sdeb_meta_write_lock(sip);
6317 
6318 	if (all) {
6319 		/* Check if all closed zones can be open */
6320 		if (devip->max_open &&
6321 		    devip->nr_exp_open + devip->nr_closed > devip->max_open) {
6322 			mk_sense_buffer(scp, DATA_PROTECT,
6323 					INSUFFICIENT_ZONE_RESOURCES);
6324 			res = check_condition_result;
6325 			goto fini;
6326 		}
6327 		/* Open all closed zones */
6328 		zbc_open_all(devip);
6329 		goto fini;
6330 	}
6331 
6332 	/* Open the specified zone */
6333 	z_id = get_unaligned_be64(cmd + 2);
6334 	if (z_id >= sdebug_capacity) {
6335 		mk_sense_buffer(scp, ILLEGAL_REQUEST,
6336 				LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE);
6337 		res = check_condition_result;
6338 		goto fini;
6339 	}
6340 
6341 	zsp = zbc_zone(devip, z_id);
6342 	if (z_id != zsp->z_start) {
6343 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
6344 		res = check_condition_result;
6345 		goto fini;
6346 	}
6347 	if (zbc_zone_is_conv(zsp)) {
6348 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
6349 		res = check_condition_result;
6350 		goto fini;
6351 	}
6352 
6353 	zc = zsp->z_cond;
6354 	if (zc == ZC3_EXPLICIT_OPEN || zc == ZC5_FULL)
6355 		goto fini;
6356 
6357 	if (devip->max_open && devip->nr_exp_open >= devip->max_open) {
6358 		mk_sense_buffer(scp, DATA_PROTECT, INSUFFICIENT_ZONE_RESOURCES);
6359 		res = check_condition_result;
6360 		goto fini;
6361 	}
6362 
6363 	zbc_open_zone(devip, zsp, true);
6364 fini:
6365 	sdeb_meta_write_unlock(sip);
6366 	return res;
6367 }
6368 
6369 static void zbc_close_all(struct sdebug_dev_info *devip)
6370 {
6371 	unsigned int i;
6372 
6373 	for (i = 0; i < devip->nr_zones; i++)
6374 		zbc_close_zone(devip, &devip->zstate[i]);
6375 }
6376 
6377 static int resp_close_zone(struct scsi_cmnd *scp,
6378 			   struct sdebug_dev_info *devip)
6379 {
6380 	int res = 0;
6381 	u64 z_id;
6382 	u8 *cmd = scp->cmnd;
6383 	struct sdeb_zone_state *zsp;
6384 	bool all = cmd[14] & 0x01;
6385 	struct sdeb_store_info *sip = devip2sip(devip, false);
6386 
6387 	if (!sdebug_dev_is_zoned(devip)) {
6388 		mk_sense_invalid_opcode(scp);
6389 		return check_condition_result;
6390 	}
6391 
6392 	sdeb_meta_write_lock(sip);
6393 
6394 	if (all) {
6395 		zbc_close_all(devip);
6396 		goto fini;
6397 	}
6398 
6399 	/* Close specified zone */
6400 	z_id = get_unaligned_be64(cmd + 2);
6401 	if (z_id >= sdebug_capacity) {
6402 		mk_sense_buffer(scp, ILLEGAL_REQUEST,
6403 				LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE);
6404 		res = check_condition_result;
6405 		goto fini;
6406 	}
6407 
6408 	zsp = zbc_zone(devip, z_id);
6409 	if (z_id != zsp->z_start) {
6410 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
6411 		res = check_condition_result;
6412 		goto fini;
6413 	}
6414 	if (zbc_zone_is_conv(zsp)) {
6415 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
6416 		res = check_condition_result;
6417 		goto fini;
6418 	}
6419 
6420 	zbc_close_zone(devip, zsp);
6421 fini:
6422 	sdeb_meta_write_unlock(sip);
6423 	return res;
6424 }
6425 
6426 static void zbc_finish_zone(struct sdebug_dev_info *devip,
6427 			    struct sdeb_zone_state *zsp, bool empty)
6428 {
6429 	enum sdebug_z_cond zc = zsp->z_cond;
6430 
6431 	if (zc == ZC4_CLOSED || zc == ZC2_IMPLICIT_OPEN ||
6432 	    zc == ZC3_EXPLICIT_OPEN || (empty && zc == ZC1_EMPTY)) {
6433 		if (zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN)
6434 			zbc_close_zone(devip, zsp);
6435 		if (zsp->z_cond == ZC4_CLOSED)
6436 			devip->nr_closed--;
6437 		zsp->z_wp = zsp->z_start + zsp->z_size;
6438 		zsp->z_cond = ZC5_FULL;
6439 	}
6440 }
6441 
6442 static void zbc_finish_all(struct sdebug_dev_info *devip)
6443 {
6444 	unsigned int i;
6445 
6446 	for (i = 0; i < devip->nr_zones; i++)
6447 		zbc_finish_zone(devip, &devip->zstate[i], false);
6448 }
6449 
6450 static int resp_finish_zone(struct scsi_cmnd *scp,
6451 			    struct sdebug_dev_info *devip)
6452 {
6453 	struct sdeb_zone_state *zsp;
6454 	int res = 0;
6455 	u64 z_id;
6456 	u8 *cmd = scp->cmnd;
6457 	bool all = cmd[14] & 0x01;
6458 	struct sdeb_store_info *sip = devip2sip(devip, false);
6459 
6460 	if (!sdebug_dev_is_zoned(devip)) {
6461 		mk_sense_invalid_opcode(scp);
6462 		return check_condition_result;
6463 	}
6464 
6465 	sdeb_meta_write_lock(sip);
6466 
6467 	if (all) {
6468 		zbc_finish_all(devip);
6469 		goto fini;
6470 	}
6471 
6472 	/* Finish the specified zone */
6473 	z_id = get_unaligned_be64(cmd + 2);
6474 	if (z_id >= sdebug_capacity) {
6475 		mk_sense_buffer(scp, ILLEGAL_REQUEST,
6476 				LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE);
6477 		res = check_condition_result;
6478 		goto fini;
6479 	}
6480 
6481 	zsp = zbc_zone(devip, z_id);
6482 	if (z_id != zsp->z_start) {
6483 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
6484 		res = check_condition_result;
6485 		goto fini;
6486 	}
6487 	if (zbc_zone_is_conv(zsp)) {
6488 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
6489 		res = check_condition_result;
6490 		goto fini;
6491 	}
6492 
6493 	zbc_finish_zone(devip, zsp, true);
6494 fini:
6495 	sdeb_meta_write_unlock(sip);
6496 	return res;
6497 }
6498 
6499 static void zbc_rwp_zone(struct sdebug_dev_info *devip,
6500 			 struct sdeb_zone_state *zsp)
6501 {
6502 	enum sdebug_z_cond zc;
6503 	struct sdeb_store_info *sip = devip2sip(devip, false);
6504 
6505 	if (!zbc_zone_is_seq(zsp))
6506 		return;
6507 
6508 	zc = zsp->z_cond;
6509 	if (zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN)
6510 		zbc_close_zone(devip, zsp);
6511 
6512 	if (zsp->z_cond == ZC4_CLOSED)
6513 		devip->nr_closed--;
6514 
6515 	if (zsp->z_wp > zsp->z_start)
6516 		memset(sip->storep + zsp->z_start * sdebug_sector_size, 0,
6517 		       (zsp->z_wp - zsp->z_start) * sdebug_sector_size);
6518 
6519 	zsp->z_non_seq_resource = false;
6520 	zsp->z_wp = zsp->z_start;
6521 	zsp->z_cond = ZC1_EMPTY;
6522 }
6523 
6524 static void zbc_rwp_all(struct sdebug_dev_info *devip)
6525 {
6526 	unsigned int i;
6527 
6528 	for (i = 0; i < devip->nr_zones; i++)
6529 		zbc_rwp_zone(devip, &devip->zstate[i]);
6530 }
6531 
6532 static int resp_rwp_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
6533 {
6534 	struct sdeb_zone_state *zsp;
6535 	int res = 0;
6536 	u64 z_id;
6537 	u8 *cmd = scp->cmnd;
6538 	bool all = cmd[14] & 0x01;
6539 	struct sdeb_store_info *sip = devip2sip(devip, false);
6540 
6541 	if (!sdebug_dev_is_zoned(devip)) {
6542 		mk_sense_invalid_opcode(scp);
6543 		return check_condition_result;
6544 	}
6545 
6546 	sdeb_meta_write_lock(sip);
6547 
6548 	if (all) {
6549 		zbc_rwp_all(devip);
6550 		goto fini;
6551 	}
6552 
6553 	z_id = get_unaligned_be64(cmd + 2);
6554 	if (z_id >= sdebug_capacity) {
6555 		mk_sense_buffer(scp, ILLEGAL_REQUEST,
6556 				LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE);
6557 		res = check_condition_result;
6558 		goto fini;
6559 	}
6560 
6561 	zsp = zbc_zone(devip, z_id);
6562 	if (z_id != zsp->z_start) {
6563 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
6564 		res = check_condition_result;
6565 		goto fini;
6566 	}
6567 	if (zbc_zone_is_conv(zsp)) {
6568 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB);
6569 		res = check_condition_result;
6570 		goto fini;
6571 	}
6572 
6573 	zbc_rwp_zone(devip, zsp);
6574 fini:
6575 	sdeb_meta_write_unlock(sip);
6576 	return res;
6577 }
6578 
6579 static u32 get_tag(struct scsi_cmnd *cmnd)
6580 {
6581 	return blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
6582 }
6583 
6584 /* Queued (deferred) command completions converge here. */
6585 static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp)
6586 {
6587 	struct sdebug_scsi_cmd *sdsc = container_of(sd_dp,
6588 					typeof(*sdsc), sd_dp);
6589 	struct scsi_cmnd *scp = (struct scsi_cmnd *)sdsc - 1;
6590 	unsigned long flags;
6591 	bool aborted;
6592 
6593 	if (sdebug_statistics) {
6594 		atomic_inc(&sdebug_completions);
6595 		if (raw_smp_processor_id() != sd_dp->issuing_cpu)
6596 			atomic_inc(&sdebug_miss_cpus);
6597 	}
6598 
6599 	spin_lock_irqsave(&sdsc->lock, flags);
6600 	aborted = sd_dp->aborted;
6601 	if (unlikely(aborted))
6602 		sd_dp->aborted = false;
6603 
6604 	spin_unlock_irqrestore(&sdsc->lock, flags);
6605 
6606 	if (aborted) {
6607 		pr_info("bypassing scsi_done() due to aborted cmd, kicking-off EH\n");
6608 		blk_abort_request(scsi_cmd_to_rq(scp));
6609 		return;
6610 	}
6611 
6612 	scsi_done(scp); /* callback to mid level */
6613 }
6614 
6615 /* When high resolution timer goes off this function is called. */
6616 static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer)
6617 {
6618 	struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer,
6619 						  hrt);
6620 	sdebug_q_cmd_complete(sd_dp);
6621 	return HRTIMER_NORESTART;
6622 }
6623 
6624 /* When work queue schedules work, it calls this function. */
6625 static void sdebug_q_cmd_wq_complete(struct work_struct *work)
6626 {
6627 	struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer,
6628 						  ew.work);
6629 	sdebug_q_cmd_complete(sd_dp);
6630 }
6631 
6632 static bool got_shared_uuid;
6633 static uuid_t shared_uuid;
6634 
6635 static int sdebug_device_create_zones(struct sdebug_dev_info *devip)
6636 {
6637 	struct sdeb_zone_state *zsp;
6638 	sector_t capacity = get_sdebug_capacity();
6639 	sector_t conv_capacity;
6640 	sector_t zstart = 0;
6641 	unsigned int i;
6642 
6643 	/*
6644 	 * Set the zone size: if sdeb_zbc_zone_size_mb is not set, figure out
6645 	 * a zone size allowing for at least 4 zones on the device. Otherwise,
6646 	 * use the specified zone size checking that at least 2 zones can be
6647 	 * created for the device.
6648 	 */
6649 	if (!sdeb_zbc_zone_size_mb) {
6650 		devip->zsize = (DEF_ZBC_ZONE_SIZE_MB * SZ_1M)
6651 			>> ilog2(sdebug_sector_size);
6652 		while (capacity < devip->zsize << 2 && devip->zsize >= 2)
6653 			devip->zsize >>= 1;
6654 		if (devip->zsize < 2) {
6655 			pr_err("Device capacity too small\n");
6656 			return -EINVAL;
6657 		}
6658 	} else {
6659 		if (!is_power_of_2(sdeb_zbc_zone_size_mb)) {
6660 			pr_err("Zone size is not a power of 2\n");
6661 			return -EINVAL;
6662 		}
6663 		devip->zsize = (sdeb_zbc_zone_size_mb * SZ_1M)
6664 			>> ilog2(sdebug_sector_size);
6665 		if (devip->zsize >= capacity) {
6666 			pr_err("Zone size too large for device capacity\n");
6667 			return -EINVAL;
6668 		}
6669 	}
6670 
6671 	devip->zsize_shift = ilog2(devip->zsize);
6672 	devip->nr_zones = (capacity + devip->zsize - 1) >> devip->zsize_shift;
6673 
6674 	if (sdeb_zbc_zone_cap_mb == 0) {
6675 		devip->zcap = devip->zsize;
6676 	} else {
6677 		devip->zcap = (sdeb_zbc_zone_cap_mb * SZ_1M) >>
6678 			      ilog2(sdebug_sector_size);
6679 		if (devip->zcap > devip->zsize) {
6680 			pr_err("Zone capacity too large\n");
6681 			return -EINVAL;
6682 		}
6683 	}
6684 
6685 	conv_capacity = (sector_t)sdeb_zbc_nr_conv << devip->zsize_shift;
6686 	if (conv_capacity >= capacity) {
6687 		pr_err("Number of conventional zones too large\n");
6688 		return -EINVAL;
6689 	}
6690 	devip->nr_conv_zones = sdeb_zbc_nr_conv;
6691 	devip->nr_seq_zones = ALIGN(capacity - conv_capacity, devip->zsize) >>
6692 			      devip->zsize_shift;
6693 	devip->nr_zones = devip->nr_conv_zones + devip->nr_seq_zones;
6694 
6695 	/* Add gap zones if zone capacity is smaller than the zone size */
6696 	if (devip->zcap < devip->zsize)
6697 		devip->nr_zones += devip->nr_seq_zones;
6698 
6699 	if (devip->zoned) {
6700 		/* zbc_max_open_zones can be 0, meaning "not reported" */
6701 		if (sdeb_zbc_max_open >= devip->nr_zones - 1)
6702 			devip->max_open = (devip->nr_zones - 1) / 2;
6703 		else
6704 			devip->max_open = sdeb_zbc_max_open;
6705 	}
6706 
6707 	devip->zstate = kzalloc_objs(struct sdeb_zone_state, devip->nr_zones);
6708 	if (!devip->zstate)
6709 		return -ENOMEM;
6710 
6711 	for (i = 0; i < devip->nr_zones; i++) {
6712 		zsp = &devip->zstate[i];
6713 
6714 		zsp->z_start = zstart;
6715 
6716 		if (i < devip->nr_conv_zones) {
6717 			zsp->z_type = ZBC_ZTYPE_CNV;
6718 			zsp->z_cond = ZBC_NOT_WRITE_POINTER;
6719 			zsp->z_wp = (sector_t)-1;
6720 			zsp->z_size =
6721 				min_t(u64, devip->zsize, capacity - zstart);
6722 		} else if ((zstart & (devip->zsize - 1)) == 0) {
6723 			if (devip->zoned)
6724 				zsp->z_type = ZBC_ZTYPE_SWR;
6725 			else
6726 				zsp->z_type = ZBC_ZTYPE_SWP;
6727 			zsp->z_cond = ZC1_EMPTY;
6728 			zsp->z_wp = zsp->z_start;
6729 			zsp->z_size =
6730 				min_t(u64, devip->zcap, capacity - zstart);
6731 		} else {
6732 			zsp->z_type = ZBC_ZTYPE_GAP;
6733 			zsp->z_cond = ZBC_NOT_WRITE_POINTER;
6734 			zsp->z_wp = (sector_t)-1;
6735 			zsp->z_size = min_t(u64, devip->zsize - devip->zcap,
6736 					    capacity - zstart);
6737 		}
6738 
6739 		WARN_ON_ONCE((int)zsp->z_size <= 0);
6740 		zstart += zsp->z_size;
6741 	}
6742 
6743 	return 0;
6744 }
6745 
6746 static struct sdebug_dev_info *sdebug_device_create(
6747 			struct sdebug_host_info *sdbg_host, gfp_t flags)
6748 {
6749 	struct sdebug_dev_info *devip;
6750 
6751 	devip = kzalloc_obj(*devip, flags);
6752 	if (devip) {
6753 		if (sdebug_uuid_ctl == 1)
6754 			uuid_gen(&devip->lu_name);
6755 		else if (sdebug_uuid_ctl == 2) {
6756 			if (got_shared_uuid)
6757 				devip->lu_name = shared_uuid;
6758 			else {
6759 				uuid_gen(&shared_uuid);
6760 				got_shared_uuid = true;
6761 				devip->lu_name = shared_uuid;
6762 			}
6763 		}
6764 		devip->sdbg_host = sdbg_host;
6765 		if (sdeb_zbc_in_use) {
6766 			devip->zoned = sdeb_zbc_model == BLK_ZONED_HM;
6767 			if (sdebug_device_create_zones(devip)) {
6768 				kfree(devip);
6769 				return NULL;
6770 			}
6771 		} else {
6772 			devip->zoned = false;
6773 		}
6774 		if (sdebug_ptype == TYPE_TAPE) {
6775 			devip->tape_density = TAPE_DEF_DENSITY;
6776 			devip->tape_blksize = TAPE_DEF_BLKSIZE;
6777 		}
6778 		devip->create_ts = ktime_get_boottime();
6779 		atomic_set(&devip->stopped, (sdeb_tur_ms_to_ready > 0 ? 2 : 0));
6780 		spin_lock_init(&devip->list_lock);
6781 		INIT_LIST_HEAD(&devip->inject_err_list);
6782 		list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list);
6783 	}
6784 	return devip;
6785 }
6786 
6787 static struct sdebug_dev_info *find_build_dev_info(struct scsi_device *sdev)
6788 {
6789 	struct sdebug_host_info *sdbg_host;
6790 	struct sdebug_dev_info *open_devip = NULL;
6791 	struct sdebug_dev_info *devip;
6792 
6793 	sdbg_host = shost_to_sdebug_host(sdev->host);
6794 
6795 	list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
6796 		if ((devip->used) && (devip->channel == sdev->channel) &&
6797 		    (devip->target == sdev->id) &&
6798 		    (devip->lun == sdev->lun))
6799 			return devip;
6800 		else {
6801 			if ((!devip->used) && (!open_devip))
6802 				open_devip = devip;
6803 		}
6804 	}
6805 	if (!open_devip) { /* try and make a new one */
6806 		open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC);
6807 		if (!open_devip) {
6808 			pr_err("out of memory at line %d\n", __LINE__);
6809 			return NULL;
6810 		}
6811 	}
6812 
6813 	open_devip->channel = sdev->channel;
6814 	open_devip->target = sdev->id;
6815 	open_devip->lun = sdev->lun;
6816 	open_devip->sdbg_host = sdbg_host;
6817 	set_bit(SDEBUG_UA_POOCCUR, open_devip->uas_bm);
6818 	open_devip->used = true;
6819 	return open_devip;
6820 }
6821 
6822 static int scsi_debug_sdev_init(struct scsi_device *sdp)
6823 {
6824 	if (sdebug_verbose)
6825 		pr_info("sdev_init <%u %u %u %llu>\n",
6826 		       sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
6827 
6828 	return 0;
6829 }
6830 
6831 static int scsi_debug_sdev_configure(struct scsi_device *sdp,
6832 				     struct queue_limits *lim)
6833 {
6834 	struct sdebug_dev_info *devip =
6835 			(struct sdebug_dev_info *)sdp->hostdata;
6836 	struct dentry *dentry;
6837 
6838 	if (sdebug_verbose)
6839 		pr_info("sdev_configure <%u %u %u %llu>\n",
6840 		       sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
6841 	if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN)
6842 		sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN;
6843 	if (devip == NULL) {
6844 		devip = find_build_dev_info(sdp);
6845 		if (devip == NULL)
6846 			return 1;  /* no resources, will be marked offline */
6847 	}
6848 	if (sdebug_ptype == TYPE_TAPE) {
6849 		if (!devip->tape_blocks[0]) {
6850 			devip->tape_blocks[0] =
6851 				kzalloc_objs(struct tape_block, TAPE_UNITS + 1);
6852 			if (!devip->tape_blocks[0])
6853 				return 1;
6854 		}
6855 		devip->tape_pending_nbr_partitions = -1;
6856 		if (partition_tape(devip, 1, TAPE_UNITS, 0) < 0) {
6857 			kfree(devip->tape_blocks[0]);
6858 			devip->tape_blocks[0] = NULL;
6859 			return 1;
6860 		}
6861 	}
6862 	sdp->hostdata = devip;
6863 	if (sdebug_no_uld)
6864 		sdp->no_uld_attach = 1;
6865 	config_cdb_len(sdp);
6866 
6867 	if (sdebug_allow_restart)
6868 		sdp->allow_restart = 1;
6869 
6870 	devip->debugfs_entry = debugfs_create_dir(dev_name(&sdp->sdev_dev),
6871 				sdebug_debugfs_root);
6872 	if (IS_ERR_OR_NULL(devip->debugfs_entry))
6873 		pr_info("failed to create debugfs directory for device %s\n",
6874 			dev_name(&sdp->sdev_gendev));
6875 
6876 	dentry = debugfs_create_file("error", 0600, devip->debugfs_entry, sdp,
6877 				&sdebug_error_fops);
6878 	if (IS_ERR_OR_NULL(dentry))
6879 		pr_info("failed to create error file for device %s\n",
6880 			dev_name(&sdp->sdev_gendev));
6881 	debugfs_create_file("corrupt", 0600, devip->debugfs_entry, sdp,
6882 				&sdebug_corrupt_fops);
6883 
6884 	return 0;
6885 }
6886 
6887 static void scsi_debug_sdev_destroy(struct scsi_device *sdp)
6888 {
6889 	struct sdebug_dev_info *devip =
6890 		(struct sdebug_dev_info *)sdp->hostdata;
6891 	struct sdebug_err_inject *err;
6892 
6893 	if (sdebug_verbose)
6894 		pr_info("sdev_destroy <%u %u %u %llu>\n",
6895 		       sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
6896 
6897 	if (!devip)
6898 		return;
6899 
6900 	spin_lock(&devip->list_lock);
6901 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
6902 		list_del_rcu(&err->list);
6903 		call_rcu(&err->rcu, sdebug_err_free);
6904 	}
6905 	spin_unlock(&devip->list_lock);
6906 
6907 	debugfs_remove(devip->debugfs_entry);
6908 
6909 	if (sdp->type == TYPE_TAPE) {
6910 		kfree(devip->tape_blocks[0]);
6911 		devip->tape_blocks[0] = NULL;
6912 	}
6913 
6914 	/* make this slot available for re-use */
6915 	devip->used = false;
6916 	sdp->hostdata = NULL;
6917 }
6918 
6919 /* Returns true if cancelled or not running callback. */
6920 static bool scsi_debug_stop_cmnd(struct scsi_cmnd *cmnd)
6921 {
6922 	struct sdebug_scsi_cmd *sdsc = scsi_cmd_priv(cmnd);
6923 	struct sdebug_defer *sd_dp = &sdsc->sd_dp;
6924 	enum sdeb_defer_type defer_t = sd_dp->defer_t;
6925 
6926 	lockdep_assert_held(&sdsc->lock);
6927 
6928 	if (defer_t == SDEB_DEFER_HRT) {
6929 		int res = hrtimer_try_to_cancel(&sd_dp->hrt);
6930 
6931 		switch (res) {
6932 		case -1: /* -1 It's executing the CB */
6933 			return false;
6934 		case 0: /* Not active, it must have already run */
6935 		case 1: /* Was active, we've now cancelled */
6936 		default:
6937 			return true;
6938 		}
6939 	} else if (defer_t == SDEB_DEFER_WQ) {
6940 		/* Cancel if pending */
6941 		if (cancel_work(&sd_dp->ew.work))
6942 			return true;
6943 		/* callback may be running, so return false */
6944 		return false;
6945 	} else if (defer_t == SDEB_DEFER_POLL) {
6946 		return true;
6947 	}
6948 
6949 	return false;
6950 }
6951 
6952 struct sdebug_abort_cmd {
6953 	u32 unique_tag;
6954 };
6955 
6956 enum sdebug_internal_cmd_type {
6957 	SCSI_DEBUG_ABORT_CMD,
6958 };
6959 
6960 struct sdebug_internal_cmd {
6961 	enum sdebug_internal_cmd_type type;
6962 
6963 	union {
6964 		struct sdebug_abort_cmd abort_cmd;
6965 	};
6966 };
6967 
6968 union sdebug_priv {
6969 	struct sdebug_scsi_cmd cmd;
6970 	struct sdebug_internal_cmd internal_cmd;
6971 };
6972 
6973 /*
6974  * Abort SCSI command @cmnd. Only called from scsi_debug_abort(). Although
6975  * it would be possible to call scsi_debug_stop_cmnd() directly, an internal
6976  * command is allocated and submitted to trigger the reserved command
6977  * infrastructure.
6978  */
6979 static bool scsi_debug_abort_cmnd(struct scsi_cmnd *cmnd)
6980 {
6981 	struct Scsi_Host *shost = cmnd->device->host;
6982 	struct request *rq = scsi_cmd_to_rq(cmnd);
6983 	u32 unique_tag = blk_mq_unique_tag(rq);
6984 	struct sdebug_internal_cmd *internal_cmd;
6985 	struct scsi_cmnd *abort_cmd;
6986 	struct request *abort_rq;
6987 	blk_status_t res;
6988 
6989 	abort_cmd = scsi_get_internal_cmd(shost->pseudo_sdev, DMA_NONE,
6990 					  BLK_MQ_REQ_RESERVED);
6991 	if (!abort_cmd)
6992 		return false;
6993 	internal_cmd = scsi_cmd_priv(abort_cmd);
6994 	*internal_cmd = (struct sdebug_internal_cmd) {
6995 		.type = SCSI_DEBUG_ABORT_CMD,
6996 		.abort_cmd = {
6997 			.unique_tag = unique_tag,
6998 		},
6999 	};
7000 	abort_rq = scsi_cmd_to_rq(abort_cmd);
7001 	abort_rq->timeout = secs_to_jiffies(3);
7002 	res = blk_execute_rq(abort_rq, true);
7003 	scsi_put_internal_cmd(abort_cmd);
7004 	return res == BLK_STS_OK;
7005 }
7006 
7007 /*
7008  * All we can do is set the cmnd as internally aborted and wait for it to
7009  * finish. We cannot call scsi_done() as normal completion path may do that.
7010  */
7011 static bool sdebug_stop_cmnd(struct request *rq, void *data)
7012 {
7013 	scsi_debug_abort_cmnd(blk_mq_rq_to_pdu(rq));
7014 
7015 	return true;
7016 }
7017 
7018 /* Deletes (stops) timers or work queues of all queued commands */
7019 static void stop_all_queued(void)
7020 {
7021 	struct sdebug_host_info *sdhp;
7022 
7023 	mutex_lock(&sdebug_host_list_mutex);
7024 	list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
7025 		struct Scsi_Host *shost = sdhp->shost;
7026 
7027 		blk_mq_tagset_busy_iter(&shost->tag_set, sdebug_stop_cmnd, NULL);
7028 	}
7029 	mutex_unlock(&sdebug_host_list_mutex);
7030 }
7031 
7032 static int sdebug_fail_abort(struct scsi_cmnd *cmnd)
7033 {
7034 	struct scsi_device *sdp = cmnd->device;
7035 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
7036 	struct sdebug_err_inject *err;
7037 	unsigned char *cmd = cmnd->cmnd;
7038 	int ret = 0;
7039 
7040 	if (devip == NULL)
7041 		return 0;
7042 
7043 	rcu_read_lock();
7044 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
7045 		if (err->type == ERR_ABORT_CMD_FAILED &&
7046 		    (err->cmd == cmd[0] || err->cmd == 0xff)) {
7047 			ret = !!err->cnt;
7048 			if (err->cnt < 0)
7049 				err->cnt++;
7050 
7051 			rcu_read_unlock();
7052 			return ret;
7053 		}
7054 	}
7055 	rcu_read_unlock();
7056 
7057 	return 0;
7058 }
7059 
7060 static int scsi_debug_abort(struct scsi_cmnd *SCpnt)
7061 {
7062 	bool aborted = scsi_debug_abort_cmnd(SCpnt);
7063 	u8 *cmd = SCpnt->cmnd;
7064 	u8 opcode = cmd[0];
7065 
7066 	++num_aborts;
7067 
7068 	if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
7069 		sdev_printk(KERN_INFO, SCpnt->device,
7070 			    "command%s found\n",
7071 			    aborted ? "" : " not");
7072 
7073 
7074 	if (sdebug_fail_abort(SCpnt)) {
7075 		scmd_printk(KERN_INFO, SCpnt, "fail abort command 0x%x\n",
7076 			    opcode);
7077 		return FAILED;
7078 	}
7079 
7080 	if (aborted == false)
7081 		return FAILED;
7082 
7083 	return SUCCESS;
7084 }
7085 
7086 static bool scsi_debug_stop_all_queued_iter(struct request *rq, void *data)
7087 {
7088 	struct scsi_device *sdp = data;
7089 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
7090 
7091 	if (scmd->device == sdp)
7092 		scsi_debug_abort_cmnd(scmd);
7093 
7094 	return true;
7095 }
7096 
7097 /* Deletes (stops) timers or work queues of all queued commands per sdev */
7098 static void scsi_debug_stop_all_queued(struct scsi_device *sdp)
7099 {
7100 	struct Scsi_Host *shost = sdp->host;
7101 
7102 	blk_mq_tagset_busy_iter(&shost->tag_set,
7103 				scsi_debug_stop_all_queued_iter, sdp);
7104 }
7105 
7106 static int sdebug_fail_lun_reset(struct scsi_cmnd *cmnd)
7107 {
7108 	struct scsi_device *sdp = cmnd->device;
7109 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
7110 	struct sdebug_err_inject *err;
7111 	unsigned char *cmd = cmnd->cmnd;
7112 	int ret = 0;
7113 
7114 	if (devip == NULL)
7115 		return 0;
7116 
7117 	rcu_read_lock();
7118 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
7119 		if (err->type == ERR_LUN_RESET_FAILED &&
7120 		    (err->cmd == cmd[0] || err->cmd == 0xff)) {
7121 			ret = !!err->cnt;
7122 			if (err->cnt < 0)
7123 				err->cnt++;
7124 
7125 			rcu_read_unlock();
7126 			return ret;
7127 		}
7128 	}
7129 	rcu_read_unlock();
7130 
7131 	return 0;
7132 }
7133 
7134 static void scsi_tape_reset_clear(struct sdebug_dev_info *devip)
7135 {
7136 	int i;
7137 
7138 	devip->tape_blksize = TAPE_DEF_BLKSIZE;
7139 	devip->tape_density = TAPE_DEF_DENSITY;
7140 	devip->tape_partition = 0;
7141 	devip->tape_dce = 0;
7142 	for (i = 0; i < TAPE_MAX_PARTITIONS; i++)
7143 		devip->tape_location[i] = 0;
7144 	devip->tape_pending_nbr_partitions = -1;
7145 	/* Don't reset partitioning? */
7146 }
7147 
7148 static int scsi_debug_device_reset(struct scsi_cmnd *SCpnt)
7149 {
7150 	struct scsi_device *sdp = SCpnt->device;
7151 	struct sdebug_dev_info *devip = sdp->hostdata;
7152 	u8 *cmd = SCpnt->cmnd;
7153 	u8 opcode = cmd[0];
7154 
7155 	++num_dev_resets;
7156 
7157 	if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
7158 		sdev_printk(KERN_INFO, sdp, "doing device reset\n");
7159 
7160 	scsi_debug_stop_all_queued(sdp);
7161 	if (devip) {
7162 		set_bit(SDEBUG_UA_POR, devip->uas_bm);
7163 		if (SCpnt->device->type == TYPE_TAPE)
7164 			scsi_tape_reset_clear(devip);
7165 	}
7166 
7167 	if (sdebug_fail_lun_reset(SCpnt)) {
7168 		scmd_printk(KERN_INFO, SCpnt, "fail lun reset 0x%x\n", opcode);
7169 		return FAILED;
7170 	}
7171 
7172 	return SUCCESS;
7173 }
7174 
7175 static int sdebug_fail_target_reset(struct scsi_cmnd *cmnd)
7176 {
7177 	struct scsi_target *starget = scsi_target(cmnd->device);
7178 	struct sdebug_target_info *targetip =
7179 		(struct sdebug_target_info *)starget->hostdata;
7180 
7181 	if (targetip)
7182 		return targetip->reset_fail;
7183 
7184 	return 0;
7185 }
7186 
7187 static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt)
7188 {
7189 	struct scsi_device *sdp = SCpnt->device;
7190 	struct sdebug_host_info *sdbg_host = shost_to_sdebug_host(sdp->host);
7191 	struct sdebug_dev_info *devip;
7192 	u8 *cmd = SCpnt->cmnd;
7193 	u8 opcode = cmd[0];
7194 	int k = 0;
7195 
7196 	++num_target_resets;
7197 	if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
7198 		sdev_printk(KERN_INFO, sdp, "doing target reset\n");
7199 
7200 	list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
7201 		if (devip->target == sdp->id) {
7202 			set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
7203 			if (SCpnt->device->type == TYPE_TAPE)
7204 				scsi_tape_reset_clear(devip);
7205 			++k;
7206 		}
7207 	}
7208 
7209 	if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
7210 		sdev_printk(KERN_INFO, sdp,
7211 			    "%d device(s) found in target\n", k);
7212 
7213 	if (sdebug_fail_target_reset(SCpnt)) {
7214 		scmd_printk(KERN_INFO, SCpnt, "fail target reset 0x%x\n",
7215 			    opcode);
7216 		return FAILED;
7217 	}
7218 
7219 	return SUCCESS;
7220 }
7221 
7222 static int scsi_debug_bus_reset(struct scsi_cmnd *SCpnt)
7223 {
7224 	struct scsi_device *sdp = SCpnt->device;
7225 	struct sdebug_host_info *sdbg_host = shost_to_sdebug_host(sdp->host);
7226 	struct sdebug_dev_info *devip;
7227 	int k = 0;
7228 
7229 	++num_bus_resets;
7230 
7231 	if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
7232 		sdev_printk(KERN_INFO, sdp, "doing bus reset\n");
7233 
7234 	list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
7235 		set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
7236 		if (SCpnt->device->type == TYPE_TAPE)
7237 			scsi_tape_reset_clear(devip);
7238 		++k;
7239 	}
7240 
7241 	if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
7242 		sdev_printk(KERN_INFO, sdp,
7243 			    "%d device(s) found in host\n", k);
7244 	return SUCCESS;
7245 }
7246 
7247 static int scsi_debug_host_reset(struct scsi_cmnd *SCpnt)
7248 {
7249 	struct sdebug_host_info *sdbg_host;
7250 	struct sdebug_dev_info *devip;
7251 	int k = 0;
7252 
7253 	++num_host_resets;
7254 	if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
7255 		sdev_printk(KERN_INFO, SCpnt->device, "doing host reset\n");
7256 	mutex_lock(&sdebug_host_list_mutex);
7257 	list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
7258 		list_for_each_entry(devip, &sdbg_host->dev_info_list,
7259 				    dev_list) {
7260 			set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
7261 			if (SCpnt->device->type == TYPE_TAPE)
7262 				scsi_tape_reset_clear(devip);
7263 			++k;
7264 		}
7265 	}
7266 	mutex_unlock(&sdebug_host_list_mutex);
7267 	stop_all_queued();
7268 	if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
7269 		sdev_printk(KERN_INFO, SCpnt->device,
7270 			"%d device(s) found\n", k);
7271 	return SUCCESS;
7272 }
7273 
7274 static void sdebug_build_parts(unsigned char *ramp, unsigned long store_size)
7275 {
7276 	struct msdos_partition *pp;
7277 	int starts[SDEBUG_MAX_PARTS + 2], max_part_secs;
7278 	int sectors_per_part, num_sectors, k;
7279 	int heads_by_sects, start_sec, end_sec;
7280 
7281 	/* assume partition table already zeroed */
7282 	if ((sdebug_num_parts < 1) || (store_size < 1048576))
7283 		return;
7284 	if (sdebug_num_parts > SDEBUG_MAX_PARTS) {
7285 		sdebug_num_parts = SDEBUG_MAX_PARTS;
7286 		pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS);
7287 	}
7288 	num_sectors = (int)get_sdebug_capacity();
7289 	sectors_per_part = (num_sectors - sdebug_sectors_per)
7290 			   / sdebug_num_parts;
7291 	heads_by_sects = sdebug_heads * sdebug_sectors_per;
7292 	starts[0] = sdebug_sectors_per;
7293 	max_part_secs = sectors_per_part;
7294 	for (k = 1; k < sdebug_num_parts; ++k) {
7295 		starts[k] = ((k * sectors_per_part) / heads_by_sects)
7296 			    * heads_by_sects;
7297 		if (starts[k] - starts[k - 1] < max_part_secs)
7298 			max_part_secs = starts[k] - starts[k - 1];
7299 	}
7300 	starts[sdebug_num_parts] = num_sectors;
7301 	starts[sdebug_num_parts + 1] = 0;
7302 
7303 	ramp[510] = 0x55;	/* magic partition markings */
7304 	ramp[511] = 0xAA;
7305 	pp = (struct msdos_partition *)(ramp + 0x1be);
7306 	for (k = 0; starts[k + 1]; ++k, ++pp) {
7307 		start_sec = starts[k];
7308 		end_sec = starts[k] + max_part_secs - 1;
7309 		pp->boot_ind = 0;
7310 
7311 		pp->cyl = start_sec / heads_by_sects;
7312 		pp->head = (start_sec - (pp->cyl * heads_by_sects))
7313 			   / sdebug_sectors_per;
7314 		pp->sector = (start_sec % sdebug_sectors_per) + 1;
7315 
7316 		pp->end_cyl = end_sec / heads_by_sects;
7317 		pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
7318 			       / sdebug_sectors_per;
7319 		pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
7320 
7321 		pp->start_sect = cpu_to_le32(start_sec);
7322 		pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1);
7323 		pp->sys_ind = 0x83;	/* plain Linux partition */
7324 	}
7325 }
7326 
7327 static void block_unblock_all_queues(bool block)
7328 {
7329 	struct sdebug_host_info *sdhp;
7330 
7331 	lockdep_assert_held(&sdebug_host_list_mutex);
7332 
7333 	list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
7334 		struct Scsi_Host *shost = sdhp->shost;
7335 
7336 		if (block)
7337 			scsi_block_requests(shost);
7338 		else
7339 			scsi_unblock_requests(shost);
7340 	}
7341 }
7342 
7343 /* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1
7344  * commands will be processed normally before triggers occur.
7345  */
7346 static void tweak_cmnd_count(void)
7347 {
7348 	int count, modulo;
7349 
7350 	modulo = abs(sdebug_every_nth);
7351 	if (modulo < 2)
7352 		return;
7353 
7354 	mutex_lock(&sdebug_host_list_mutex);
7355 	block_unblock_all_queues(true);
7356 	count = atomic_read(&sdebug_cmnd_count);
7357 	atomic_set(&sdebug_cmnd_count, (count / modulo) * modulo);
7358 	block_unblock_all_queues(false);
7359 	mutex_unlock(&sdebug_host_list_mutex);
7360 }
7361 
7362 static void clear_queue_stats(void)
7363 {
7364 	atomic_set(&sdebug_cmnd_count, 0);
7365 	atomic_set(&sdebug_completions, 0);
7366 	atomic_set(&sdebug_miss_cpus, 0);
7367 	atomic_set(&sdebug_a_tsf, 0);
7368 }
7369 
7370 static bool inject_on_this_cmd(void)
7371 {
7372 	if (sdebug_every_nth == 0)
7373 		return false;
7374 	return (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) == 0;
7375 }
7376 
7377 #define INCLUSIVE_TIMING_MAX_NS 1000000		/* 1 millisecond */
7378 
7379 /* Complete the processing of the thread that queued a SCSI command to this
7380  * driver. It either completes the command by calling cmnd_done() or
7381  * schedules a hr timer or work queue then returns 0. Returns
7382  * SCSI_MLQUEUE_HOST_BUSY if temporarily out of resources.
7383  */
7384 static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip,
7385 			 int scsi_result,
7386 			 int (*pfp)(struct scsi_cmnd *,
7387 				    struct sdebug_dev_info *),
7388 			 int delta_jiff, int ndelay)
7389 {
7390 	struct request *rq = scsi_cmd_to_rq(cmnd);
7391 	bool polled = rq->cmd_flags & REQ_POLLED;
7392 	struct sdebug_scsi_cmd *sdsc = scsi_cmd_priv(cmnd);
7393 	unsigned long flags;
7394 	u64 ns_from_boot = 0;
7395 	struct scsi_device *sdp;
7396 	struct sdebug_defer *sd_dp;
7397 
7398 	if (unlikely(devip == NULL)) {
7399 		if (scsi_result == 0)
7400 			scsi_result = DID_NO_CONNECT << 16;
7401 		goto respond_in_thread;
7402 	}
7403 	sdp = cmnd->device;
7404 
7405 	if (delta_jiff == 0)
7406 		goto respond_in_thread;
7407 
7408 
7409 	if (unlikely(sdebug_every_nth && (SDEBUG_OPT_RARE_TSF & sdebug_opts) &&
7410 		     (scsi_result == 0))) {
7411 		int num_in_q = scsi_device_busy(sdp);
7412 		int qdepth = cmnd->device->queue_depth;
7413 
7414 		if ((num_in_q == qdepth) &&
7415 		    (atomic_inc_return(&sdebug_a_tsf) >=
7416 		     abs(sdebug_every_nth))) {
7417 			atomic_set(&sdebug_a_tsf, 0);
7418 			scsi_result = device_qfull_result;
7419 
7420 			if (unlikely(SDEBUG_OPT_Q_NOISE & sdebug_opts))
7421 				sdev_printk(KERN_INFO, sdp, "num_in_q=%d +1, <inject> status: TASK SET FULL\n",
7422 					    num_in_q);
7423 		}
7424 	}
7425 
7426 	sd_dp = &sdsc->sd_dp;
7427 
7428 	if (polled || (ndelay > 0 && ndelay < INCLUSIVE_TIMING_MAX_NS))
7429 		ns_from_boot = ktime_get_boottime_ns();
7430 
7431 	/* one of the resp_*() response functions is called here */
7432 	cmnd->result = pfp ? pfp(cmnd, devip) : 0;
7433 	if (cmnd->result & SDEG_RES_IMMED_MASK) {
7434 		cmnd->result &= ~SDEG_RES_IMMED_MASK;
7435 		delta_jiff = ndelay = 0;
7436 	}
7437 	if (cmnd->result == 0 && scsi_result != 0)
7438 		cmnd->result = scsi_result;
7439 	if (cmnd->result == 0 && unlikely(sdebug_opts & SDEBUG_OPT_TRANSPORT_ERR)) {
7440 		if (atomic_read(&sdeb_inject_pending)) {
7441 			mk_sense_buffer(cmnd, ABORTED_COMMAND, ACK_NAK_TIMEOUT);
7442 			atomic_set(&sdeb_inject_pending, 0);
7443 			cmnd->result = check_condition_result;
7444 		}
7445 	}
7446 
7447 	if (unlikely(sdebug_verbose && cmnd->result))
7448 		sdev_printk(KERN_INFO, sdp, "non-zero result=0x%x\n",
7449 			    cmnd->result);
7450 
7451 	if (delta_jiff > 0 || ndelay > 0) {
7452 		ktime_t kt;
7453 
7454 		if (delta_jiff > 0) {
7455 			u64 ns = jiffies_to_nsecs(delta_jiff);
7456 
7457 			if (sdebug_random && ns < U32_MAX) {
7458 				ns = get_random_u32_below((u32)ns);
7459 			} else if (sdebug_random) {
7460 				ns >>= 12;	/* scale to 4 usec precision */
7461 				if (ns < U32_MAX)	/* over 4 hours max */
7462 					ns = get_random_u32_below((u32)ns);
7463 				ns <<= 12;
7464 			}
7465 			kt = ns_to_ktime(ns);
7466 		} else {	/* ndelay has a 4.2 second max */
7467 			kt = sdebug_random ? get_random_u32_below((u32)ndelay) :
7468 					     (u32)ndelay;
7469 			if (ndelay < INCLUSIVE_TIMING_MAX_NS) {
7470 				u64 d = ktime_get_boottime_ns() - ns_from_boot;
7471 
7472 				if (kt <= d) {	/* elapsed duration >= kt */
7473 					/* call scsi_done() from this thread */
7474 					scsi_done(cmnd);
7475 					return 0;
7476 				}
7477 				/* otherwise reduce kt by elapsed time */
7478 				kt -= d;
7479 			}
7480 		}
7481 		if (sdebug_statistics)
7482 			sd_dp->issuing_cpu = raw_smp_processor_id();
7483 		if (polled) {
7484 			spin_lock_irqsave(&sdsc->lock, flags);
7485 			sd_dp->cmpl_ts = ktime_add(ns_to_ktime(ns_from_boot), kt);
7486 			sd_dp->defer_t = SDEB_DEFER_POLL;
7487 			spin_unlock_irqrestore(&sdsc->lock, flags);
7488 		} else {
7489 			/* schedule the invocation of scsi_done() for a later time */
7490 			spin_lock_irqsave(&sdsc->lock, flags);
7491 			sd_dp->defer_t = SDEB_DEFER_HRT;
7492 			hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL_PINNED);
7493 			/*
7494 			 * The completion handler will try to grab sqcp->lock,
7495 			 * so there is no chance that the completion handler
7496 			 * will call scsi_done() until we release the lock
7497 			 * here (so ok to keep referencing sdsc).
7498 			 */
7499 			spin_unlock_irqrestore(&sdsc->lock, flags);
7500 		}
7501 	} else {	/* jdelay < 0, use work queue */
7502 		if (unlikely((sdebug_opts & SDEBUG_OPT_CMD_ABORT) &&
7503 			     atomic_read(&sdeb_inject_pending))) {
7504 			sd_dp->aborted = true;
7505 			atomic_set(&sdeb_inject_pending, 0);
7506 			sdev_printk(KERN_INFO, sdp, "abort request tag=%#x\n",
7507 				    blk_mq_unique_tag_to_tag(get_tag(cmnd)));
7508 		}
7509 
7510 		if (sdebug_statistics)
7511 			sd_dp->issuing_cpu = raw_smp_processor_id();
7512 		if (polled) {
7513 			spin_lock_irqsave(&sdsc->lock, flags);
7514 			sd_dp->cmpl_ts = ns_to_ktime(ns_from_boot);
7515 			sd_dp->defer_t = SDEB_DEFER_POLL;
7516 			spin_unlock_irqrestore(&sdsc->lock, flags);
7517 		} else {
7518 			spin_lock_irqsave(&sdsc->lock, flags);
7519 			sd_dp->defer_t = SDEB_DEFER_WQ;
7520 			schedule_work(&sd_dp->ew.work);
7521 			spin_unlock_irqrestore(&sdsc->lock, flags);
7522 		}
7523 	}
7524 
7525 	return 0;
7526 
7527 respond_in_thread:	/* call back to mid-layer using invocation thread */
7528 	cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0;
7529 	cmnd->result &= ~SDEG_RES_IMMED_MASK;
7530 	if (cmnd->result == 0 && scsi_result != 0)
7531 		cmnd->result = scsi_result;
7532 	scsi_done(cmnd);
7533 	return 0;
7534 }
7535 
7536 /* Note: The following macros create attribute files in the
7537    /sys/module/scsi_debug/parameters directory. Unfortunately this
7538    driver is unaware of a change and cannot trigger auxiliary actions
7539    as it can when the corresponding attribute in the
7540    /sys/bus/pseudo/drivers/scsi_debug directory is changed.
7541  */
7542 module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR);
7543 module_param_named(ato, sdebug_ato, int, S_IRUGO);
7544 module_param_named(cdb_len, sdebug_cdb_len, int, 0644);
7545 module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR);
7546 module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR);
7547 module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO);
7548 module_param_named(dif, sdebug_dif, int, S_IRUGO);
7549 module_param_named(dix, sdebug_dix, int, S_IRUGO);
7550 module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR);
7551 module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR);
7552 module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR);
7553 module_param_named(guard, sdebug_guard, uint, S_IRUGO);
7554 module_param_named(host_lock, sdebug_host_lock, bool, S_IRUGO | S_IWUSR);
7555 module_param_named(host_max_queue, sdebug_host_max_queue, int, S_IRUGO);
7556 module_param_string(inq_product, sdebug_inq_product_id,
7557 		    sizeof(sdebug_inq_product_id), S_IRUGO | S_IWUSR);
7558 module_param_string(inq_rev, sdebug_inq_product_rev,
7559 		    sizeof(sdebug_inq_product_rev), S_IRUGO | S_IWUSR);
7560 module_param_string(inq_vendor, sdebug_inq_vendor_id,
7561 		    sizeof(sdebug_inq_vendor_id), S_IRUGO | S_IWUSR);
7562 module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO);
7563 module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO);
7564 module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO);
7565 module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO);
7566 module_param_named(atomic_wr, sdebug_atomic_wr, int, S_IRUGO);
7567 module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO);
7568 module_param_named(lun_format, sdebug_lun_am_i, int, S_IRUGO | S_IWUSR);
7569 module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR);
7570 module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR);
7571 module_param_named(medium_error_count, sdebug_medium_error_count, int,
7572 		   S_IRUGO | S_IWUSR);
7573 module_param_named(medium_error_start, sdebug_medium_error_start, int,
7574 		   S_IRUGO | S_IWUSR);
7575 module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR);
7576 module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR);
7577 module_param_named(no_rwlock, sdebug_no_rwlock, bool, S_IRUGO | S_IWUSR);
7578 module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO);
7579 module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO);
7580 module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR);
7581 module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO);
7582 module_param_named(opt_xferlen_exp, sdebug_opt_xferlen_exp, int, S_IRUGO);
7583 module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR);
7584 module_param_named(per_host_store, sdebug_per_host_store, bool,
7585 		   S_IRUGO | S_IWUSR);
7586 module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO);
7587 module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR);
7588 module_param_named(random, sdebug_random, bool, S_IRUGO | S_IWUSR);
7589 module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR);
7590 module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO);
7591 module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO);
7592 module_param_named(statistics, sdebug_statistics, bool, S_IRUGO | S_IWUSR);
7593 module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR);
7594 module_param_named(submit_queues, submit_queues, int, S_IRUGO);
7595 module_param_named(poll_queues, poll_queues, int, S_IRUGO);
7596 module_param_named(tur_ms_to_ready, sdeb_tur_ms_to_ready, int, S_IRUGO);
7597 module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO);
7598 module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO);
7599 module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO);
7600 module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO);
7601 module_param_named(atomic_wr_max_length, sdebug_atomic_wr_max_length, int, S_IRUGO);
7602 module_param_named(atomic_wr_align, sdebug_atomic_wr_align, int, S_IRUGO);
7603 module_param_named(atomic_wr_gran, sdebug_atomic_wr_gran, int, S_IRUGO);
7604 module_param_named(atomic_wr_max_length_bndry, sdebug_atomic_wr_max_length_bndry, int, S_IRUGO);
7605 module_param_named(atomic_wr_max_bndry, sdebug_atomic_wr_max_bndry, int, S_IRUGO);
7606 module_param_named(uuid_ctl, sdebug_uuid_ctl, int, S_IRUGO);
7607 module_param_named(virtual_gb, sdebug_virtual_gb, int, S_IRUGO | S_IWUSR);
7608 module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int,
7609 		   S_IRUGO | S_IWUSR);
7610 module_param_named(wp, sdebug_wp, bool, S_IRUGO | S_IWUSR);
7611 module_param_named(write_same_length, sdebug_write_same_length, int,
7612 		   S_IRUGO | S_IWUSR);
7613 module_param_named(zbc, sdeb_zbc_model_s, charp, S_IRUGO);
7614 module_param_named(zone_cap_mb, sdeb_zbc_zone_cap_mb, int, S_IRUGO);
7615 module_param_named(zone_max_open, sdeb_zbc_max_open, int, S_IRUGO);
7616 module_param_named(zone_nr_conv, sdeb_zbc_nr_conv, int, S_IRUGO);
7617 module_param_named(zone_size_mb, sdeb_zbc_zone_size_mb, int, S_IRUGO);
7618 module_param_named(allow_restart, sdebug_allow_restart, bool, S_IRUGO | S_IWUSR);
7619 
7620 MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
7621 MODULE_DESCRIPTION("SCSI debug adapter driver");
7622 MODULE_LICENSE("GPL");
7623 MODULE_VERSION(SDEBUG_VERSION);
7624 
7625 MODULE_PARM_DESC(add_host, "add n hosts, in sysfs if negative remove host(s) (def=1)");
7626 MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)");
7627 MODULE_PARM_DESC(cdb_len, "suggest CDB lengths to drivers (def=10)");
7628 MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=1)");
7629 MODULE_PARM_DESC(delay, "response delay (def=0 jiffy); 0:imm, -1,-2:tiny");
7630 MODULE_PARM_DESC(dev_size_mb, "size in MiB of ram shared by devs(def=8)");
7631 MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)");
7632 MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)");
7633 MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
7634 MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)");
7635 MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
7636 MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)");
7637 MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)");
7638 MODULE_PARM_DESC(host_max_queue,
7639 		 "host max # of queued cmds (0 to max(def) [max_queue fixed equal for !0])");
7640 MODULE_PARM_DESC(inq_product, "SCSI INQUIRY product string (def=\"scsi_debug\")");
7641 MODULE_PARM_DESC(inq_rev, "SCSI INQUIRY revision string (def=\""
7642 		 SDEBUG_VERSION "\")");
7643 MODULE_PARM_DESC(inq_vendor, "SCSI INQUIRY vendor string (def=\"Linux\")");
7644 MODULE_PARM_DESC(lbprz,
7645 		 "on read unmapped LBs return 0 when 1 (def), return 0xff when 2");
7646 MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)");
7647 MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)");
7648 MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)");
7649 MODULE_PARM_DESC(atomic_wr, "enable ATOMIC WRITE support, support WRITE ATOMIC(16) (def=0)");
7650 MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)");
7651 MODULE_PARM_DESC(lun_format, "LUN format: 0->peripheral (def); 1 --> flat address method");
7652 MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
7653 MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))");
7654 MODULE_PARM_DESC(medium_error_count, "count of sectors to return follow on MEDIUM error");
7655 MODULE_PARM_DESC(medium_error_start, "starting sector number to return MEDIUM error");
7656 MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)");
7657 MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
7658 MODULE_PARM_DESC(no_rwlock, "don't protect user data reads+writes (def=0)");
7659 MODULE_PARM_DESC(no_uld, "stop ULD (e.g. sd driver) attaching (def=0))");
7660 MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
7661 MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
7662 MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)");
7663 MODULE_PARM_DESC(opt_xferlen_exp, "optimal transfer length granularity exponent (def=physblk_exp)");
7664 MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)");
7665 MODULE_PARM_DESC(per_host_store, "If set, next positive add_host will get new store (def=0)");
7666 MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)");
7667 MODULE_PARM_DESC(poll_queues, "support for iouring iopoll queues (1 to max(submit_queues - 1))");
7668 MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
7669 MODULE_PARM_DESC(random, "If set, uniformly randomize command duration between 0 and delay_in_ns");
7670 MODULE_PARM_DESC(removable, "claim to have removable media (def=0)");
7671 MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=7[SPC-5])");
7672 MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)");
7673 MODULE_PARM_DESC(statistics, "collect statistics on commands, queues (def=0)");
7674 MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)");
7675 MODULE_PARM_DESC(submit_queues, "support for block multi-queue (def=1)");
7676 MODULE_PARM_DESC(tur_ms_to_ready, "TEST UNIT READY millisecs before initial good status (def=0)");
7677 MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)");
7678 MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)");
7679 MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)");
7680 MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)");
7681 MODULE_PARM_DESC(atomic_wr_max_length, "max # of blocks can be atomically written in one cmd (def=8192)");
7682 MODULE_PARM_DESC(atomic_wr_align, "minimum alignment of atomic write in blocks (def=2)");
7683 MODULE_PARM_DESC(atomic_wr_gran, "minimum granularity of atomic write in blocks (def=2)");
7684 MODULE_PARM_DESC(atomic_wr_max_length_bndry, "max # of blocks can be atomically written in one cmd with boundary set (def=8192)");
7685 MODULE_PARM_DESC(atomic_wr_max_bndry, "max # boundaries per atomic write (def=128)");
7686 MODULE_PARM_DESC(uuid_ctl,
7687 		 "1->use uuid for lu name, 0->don't, 2->all use same (def=0)");
7688 MODULE_PARM_DESC(virtual_gb, "virtual gigabyte (GiB) size (def=0 -> use dev_size_mb)");
7689 MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
7690 MODULE_PARM_DESC(wp, "Write Protect (def=0)");
7691 MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)");
7692 MODULE_PARM_DESC(zbc, "'none' [0]; 'aware' [1]; 'managed' [2] (def=0). Can have 'host-' prefix");
7693 MODULE_PARM_DESC(zone_cap_mb, "Zone capacity in MiB (def=zone size)");
7694 MODULE_PARM_DESC(zone_max_open, "Maximum number of open zones; [0] for no limit (def=auto)");
7695 MODULE_PARM_DESC(zone_nr_conv, "Number of conventional zones (def=1)");
7696 MODULE_PARM_DESC(zone_size_mb, "Zone size in MiB (def=auto)");
7697 MODULE_PARM_DESC(allow_restart, "Set scsi_device's allow_restart flag(def=0)");
7698 
7699 #define SDEBUG_INFO_LEN 256
7700 static char sdebug_info[SDEBUG_INFO_LEN];
7701 
7702 static const char *scsi_debug_info(struct Scsi_Host *shp)
7703 {
7704 	int k;
7705 
7706 	k = scnprintf(sdebug_info, SDEBUG_INFO_LEN, "%s: version %s [%s]\n",
7707 		      my_name, SDEBUG_VERSION, sdebug_version_date);
7708 	if (k >= (SDEBUG_INFO_LEN - 1))
7709 		return sdebug_info;
7710 	scnprintf(sdebug_info + k, SDEBUG_INFO_LEN - k,
7711 		  "  dev_size_mb=%d, opts=0x%x, submit_queues=%d, %s=%d",
7712 		  sdebug_dev_size_mb, sdebug_opts, submit_queues,
7713 		  "statistics", (int)sdebug_statistics);
7714 	return sdebug_info;
7715 }
7716 
7717 /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */
7718 static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer,
7719 				 int length)
7720 {
7721 	char arr[16];
7722 	int opts;
7723 	int minLen = length > 15 ? 15 : length;
7724 
7725 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
7726 		return -EACCES;
7727 	memcpy(arr, buffer, minLen);
7728 	arr[minLen] = '\0';
7729 	if (1 != sscanf(arr, "%d", &opts))
7730 		return -EINVAL;
7731 	sdebug_opts = opts;
7732 	sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
7733 	if (sdebug_every_nth != 0)
7734 		tweak_cmnd_count();
7735 	return length;
7736 }
7737 
7738 struct sdebug_submit_queue_data {
7739 	int *first;
7740 	int *last;
7741 	int queue_num;
7742 };
7743 
7744 static bool sdebug_submit_queue_iter(struct request *rq, void *opaque)
7745 {
7746 	struct sdebug_submit_queue_data *data = opaque;
7747 	u32 unique_tag = blk_mq_unique_tag(rq);
7748 	u16 hwq = blk_mq_unique_tag_to_hwq(unique_tag);
7749 	u16 tag = blk_mq_unique_tag_to_tag(unique_tag);
7750 	int queue_num = data->queue_num;
7751 
7752 	if (hwq != queue_num)
7753 		return true;
7754 
7755 	/* Rely on iter'ing in ascending tag order */
7756 	if (*data->first == -1)
7757 		*data->first = *data->last = tag;
7758 	else
7759 		*data->last = tag;
7760 
7761 	return true;
7762 }
7763 
7764 /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the
7765  * same for each scsi_debug host (if more than one). Some of the counters
7766  * output are not atomics so might be inaccurate in a busy system. */
7767 static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host)
7768 {
7769 	struct sdebug_host_info *sdhp;
7770 	int j;
7771 
7772 	seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n",
7773 		   SDEBUG_VERSION, sdebug_version_date);
7774 	seq_printf(m, "num_tgts=%d, %ssize=%d MB, opts=0x%x, every_nth=%d\n",
7775 		   sdebug_num_tgts, "shared (ram) ", sdebug_dev_size_mb,
7776 		   sdebug_opts, sdebug_every_nth);
7777 	seq_printf(m, "delay=%d, ndelay=%d, max_luns=%d, sector_size=%d %s\n",
7778 		   sdebug_jdelay, sdebug_ndelay, sdebug_max_luns,
7779 		   sdebug_sector_size, "bytes");
7780 	seq_printf(m, "cylinders=%d, heads=%d, sectors=%d, command aborts=%d\n",
7781 		   sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
7782 		   num_aborts);
7783 	seq_printf(m, "RESETs: device=%d, target=%d, bus=%d, host=%d\n",
7784 		   num_dev_resets, num_target_resets, num_bus_resets,
7785 		   num_host_resets);
7786 	seq_printf(m, "dix_reads=%d, dix_writes=%d, dif_errors=%d\n",
7787 		   dix_reads, dix_writes, dif_errors);
7788 	seq_printf(m, "usec_in_jiffy=%lu, statistics=%d\n", TICK_NSEC / 1000,
7789 		   sdebug_statistics);
7790 	seq_printf(m, "cmnd_count=%d, completions=%d, %s=%d, a_tsf=%d, mq_polls=%d\n",
7791 		   atomic_read(&sdebug_cmnd_count),
7792 		   atomic_read(&sdebug_completions),
7793 		   "miss_cpus", atomic_read(&sdebug_miss_cpus),
7794 		   atomic_read(&sdebug_a_tsf),
7795 		   atomic_read(&sdeb_mq_poll_count));
7796 
7797 	seq_printf(m, "submit_queues=%d\n", submit_queues);
7798 	for (j = 0; j < submit_queues; ++j) {
7799 		int f = -1, l = -1;
7800 		struct sdebug_submit_queue_data data = {
7801 			.queue_num = j,
7802 			.first = &f,
7803 			.last = &l,
7804 		};
7805 		seq_printf(m, "  queue %d:\n", j);
7806 		blk_mq_tagset_busy_iter(&host->tag_set, sdebug_submit_queue_iter,
7807 					&data);
7808 		if (f >= 0) {
7809 			seq_printf(m, "    BUSY: %s: %d,%d\n",
7810 				   "first,last bits", f, l);
7811 		}
7812 	}
7813 
7814 	seq_printf(m, "this host_no=%d\n", host->host_no);
7815 	if (!xa_empty(per_store_ap)) {
7816 		bool niu;
7817 		int idx;
7818 		unsigned long l_idx;
7819 		struct sdeb_store_info *sip;
7820 
7821 		seq_puts(m, "\nhost list:\n");
7822 		j = 0;
7823 		list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
7824 			idx = sdhp->si_idx;
7825 			seq_printf(m, "  %d: host_no=%d, si_idx=%d\n", j,
7826 				   sdhp->shost->host_no, idx);
7827 			++j;
7828 		}
7829 		seq_printf(m, "\nper_store array [most_recent_idx=%d]:\n",
7830 			   sdeb_most_recent_idx);
7831 		j = 0;
7832 		xa_for_each(per_store_ap, l_idx, sip) {
7833 			niu = xa_get_mark(per_store_ap, l_idx,
7834 					  SDEB_XA_NOT_IN_USE);
7835 			idx = (int)l_idx;
7836 			seq_printf(m, "  %d: idx=%d%s\n", j, idx,
7837 				   (niu ? "  not_in_use" : ""));
7838 			++j;
7839 		}
7840 	}
7841 	return 0;
7842 }
7843 
7844 static ssize_t delay_show(struct device_driver *ddp, char *buf)
7845 {
7846 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay);
7847 }
7848 /* Returns -EBUSY if jdelay is being changed and commands are queued. The unit
7849  * of delay is jiffies.
7850  */
7851 static ssize_t delay_store(struct device_driver *ddp, const char *buf,
7852 			   size_t count)
7853 {
7854 	int jdelay, res;
7855 
7856 	if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) {
7857 		res = count;
7858 		if (sdebug_jdelay != jdelay) {
7859 			struct sdebug_host_info *sdhp;
7860 
7861 			mutex_lock(&sdebug_host_list_mutex);
7862 			block_unblock_all_queues(true);
7863 
7864 			list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
7865 				struct Scsi_Host *shost = sdhp->shost;
7866 
7867 				if (scsi_host_busy(shost)) {
7868 					res = -EBUSY;   /* queued commands */
7869 					break;
7870 				}
7871 			}
7872 			if (res > 0) {
7873 				sdebug_jdelay = jdelay;
7874 				sdebug_ndelay = 0;
7875 			}
7876 			block_unblock_all_queues(false);
7877 			mutex_unlock(&sdebug_host_list_mutex);
7878 		}
7879 		return res;
7880 	}
7881 	return -EINVAL;
7882 }
7883 static DRIVER_ATTR_RW(delay);
7884 
7885 static ssize_t ndelay_show(struct device_driver *ddp, char *buf)
7886 {
7887 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay);
7888 }
7889 /* Returns -EBUSY if ndelay is being changed and commands are queued */
7890 /* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */
7891 static ssize_t ndelay_store(struct device_driver *ddp, const char *buf,
7892 			    size_t count)
7893 {
7894 	int ndelay, res;
7895 
7896 	if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) &&
7897 	    (ndelay >= 0) && (ndelay < (1000 * 1000 * 1000))) {
7898 		res = count;
7899 		if (sdebug_ndelay != ndelay) {
7900 			struct sdebug_host_info *sdhp;
7901 
7902 			mutex_lock(&sdebug_host_list_mutex);
7903 			block_unblock_all_queues(true);
7904 
7905 			list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
7906 				struct Scsi_Host *shost = sdhp->shost;
7907 
7908 				if (scsi_host_busy(shost)) {
7909 					res = -EBUSY;   /* queued commands */
7910 					break;
7911 				}
7912 			}
7913 
7914 			if (res > 0) {
7915 				sdebug_ndelay = ndelay;
7916 				sdebug_jdelay = ndelay  ? JDELAY_OVERRIDDEN
7917 							: DEF_JDELAY;
7918 			}
7919 			block_unblock_all_queues(false);
7920 			mutex_unlock(&sdebug_host_list_mutex);
7921 		}
7922 		return res;
7923 	}
7924 	return -EINVAL;
7925 }
7926 static DRIVER_ATTR_RW(ndelay);
7927 
7928 static ssize_t opts_show(struct device_driver *ddp, char *buf)
7929 {
7930 	return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts);
7931 }
7932 
7933 static ssize_t opts_store(struct device_driver *ddp, const char *buf,
7934 			  size_t count)
7935 {
7936 	int opts;
7937 	char work[20];
7938 
7939 	if (sscanf(buf, "%10s", work) == 1) {
7940 		if (strncasecmp(work, "0x", 2) == 0) {
7941 			if (kstrtoint(work + 2, 16, &opts) == 0)
7942 				goto opts_done;
7943 		} else {
7944 			if (kstrtoint(work, 10, &opts) == 0)
7945 				goto opts_done;
7946 		}
7947 	}
7948 	return -EINVAL;
7949 opts_done:
7950 	sdebug_opts = opts;
7951 	sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
7952 	tweak_cmnd_count();
7953 	return count;
7954 }
7955 static DRIVER_ATTR_RW(opts);
7956 
7957 static ssize_t ptype_show(struct device_driver *ddp, char *buf)
7958 {
7959 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype);
7960 }
7961 static ssize_t ptype_store(struct device_driver *ddp, const char *buf,
7962 			   size_t count)
7963 {
7964 	int n;
7965 
7966 	/* Cannot change from or to TYPE_ZBC with sysfs */
7967 	if (sdebug_ptype == TYPE_ZBC)
7968 		return -EINVAL;
7969 
7970 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
7971 		if (n == TYPE_ZBC)
7972 			return -EINVAL;
7973 		sdebug_ptype = n;
7974 		return count;
7975 	}
7976 	return -EINVAL;
7977 }
7978 static DRIVER_ATTR_RW(ptype);
7979 
7980 static ssize_t dsense_show(struct device_driver *ddp, char *buf)
7981 {
7982 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense);
7983 }
7984 static ssize_t dsense_store(struct device_driver *ddp, const char *buf,
7985 			    size_t count)
7986 {
7987 	int n;
7988 
7989 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
7990 		sdebug_dsense = n;
7991 		return count;
7992 	}
7993 	return -EINVAL;
7994 }
7995 static DRIVER_ATTR_RW(dsense);
7996 
7997 static ssize_t fake_rw_show(struct device_driver *ddp, char *buf)
7998 {
7999 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw);
8000 }
8001 static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf,
8002 			     size_t count)
8003 {
8004 	int n, idx;
8005 
8006 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8007 		bool want_store = (n == 0);
8008 		struct sdebug_host_info *sdhp;
8009 
8010 		n = (n > 0);
8011 		sdebug_fake_rw = (sdebug_fake_rw > 0);
8012 		if (sdebug_fake_rw == n)
8013 			return count;	/* not transitioning so do nothing */
8014 
8015 		if (want_store) {	/* 1 --> 0 transition, set up store */
8016 			if (sdeb_first_idx < 0) {
8017 				idx = sdebug_add_store();
8018 				if (idx < 0)
8019 					return idx;
8020 			} else {
8021 				idx = sdeb_first_idx;
8022 				xa_clear_mark(per_store_ap, idx,
8023 					      SDEB_XA_NOT_IN_USE);
8024 			}
8025 			/* make all hosts use same store */
8026 			list_for_each_entry(sdhp, &sdebug_host_list,
8027 					    host_list) {
8028 				if (sdhp->si_idx != idx) {
8029 					xa_set_mark(per_store_ap, sdhp->si_idx,
8030 						    SDEB_XA_NOT_IN_USE);
8031 					sdhp->si_idx = idx;
8032 				}
8033 			}
8034 			sdeb_most_recent_idx = idx;
8035 		} else {	/* 0 --> 1 transition is trigger for shrink */
8036 			sdebug_erase_all_stores(true /* apart from first */);
8037 		}
8038 		sdebug_fake_rw = n;
8039 		return count;
8040 	}
8041 	return -EINVAL;
8042 }
8043 static DRIVER_ATTR_RW(fake_rw);
8044 
8045 static ssize_t no_lun_0_show(struct device_driver *ddp, char *buf)
8046 {
8047 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0);
8048 }
8049 static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf,
8050 			      size_t count)
8051 {
8052 	int n;
8053 
8054 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8055 		sdebug_no_lun_0 = n;
8056 		return count;
8057 	}
8058 	return -EINVAL;
8059 }
8060 static DRIVER_ATTR_RW(no_lun_0);
8061 
8062 static ssize_t num_tgts_show(struct device_driver *ddp, char *buf)
8063 {
8064 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts);
8065 }
8066 static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf,
8067 			      size_t count)
8068 {
8069 	int n;
8070 
8071 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8072 		sdebug_num_tgts = n;
8073 		sdebug_max_tgts_luns();
8074 		return count;
8075 	}
8076 	return -EINVAL;
8077 }
8078 static DRIVER_ATTR_RW(num_tgts);
8079 
8080 static ssize_t dev_size_mb_show(struct device_driver *ddp, char *buf)
8081 {
8082 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb);
8083 }
8084 static DRIVER_ATTR_RO(dev_size_mb);
8085 
8086 static ssize_t per_host_store_show(struct device_driver *ddp, char *buf)
8087 {
8088 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_per_host_store);
8089 }
8090 
8091 static ssize_t per_host_store_store(struct device_driver *ddp, const char *buf,
8092 				    size_t count)
8093 {
8094 	bool v;
8095 
8096 	if (kstrtobool(buf, &v))
8097 		return -EINVAL;
8098 
8099 	sdebug_per_host_store = v;
8100 	return count;
8101 }
8102 static DRIVER_ATTR_RW(per_host_store);
8103 
8104 static ssize_t num_parts_show(struct device_driver *ddp, char *buf)
8105 {
8106 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts);
8107 }
8108 static DRIVER_ATTR_RO(num_parts);
8109 
8110 static ssize_t every_nth_show(struct device_driver *ddp, char *buf)
8111 {
8112 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth);
8113 }
8114 static ssize_t every_nth_store(struct device_driver *ddp, const char *buf,
8115 			       size_t count)
8116 {
8117 	int nth;
8118 	char work[20];
8119 
8120 	if (sscanf(buf, "%10s", work) == 1) {
8121 		if (strncasecmp(work, "0x", 2) == 0) {
8122 			if (kstrtoint(work + 2, 16, &nth) == 0)
8123 				goto every_nth_done;
8124 		} else {
8125 			if (kstrtoint(work, 10, &nth) == 0)
8126 				goto every_nth_done;
8127 		}
8128 	}
8129 	return -EINVAL;
8130 
8131 every_nth_done:
8132 	sdebug_every_nth = nth;
8133 	if (nth && !sdebug_statistics) {
8134 		pr_info("every_nth needs statistics=1, set it\n");
8135 		sdebug_statistics = true;
8136 	}
8137 	tweak_cmnd_count();
8138 	return count;
8139 }
8140 static DRIVER_ATTR_RW(every_nth);
8141 
8142 static ssize_t lun_format_show(struct device_driver *ddp, char *buf)
8143 {
8144 	return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_lun_am);
8145 }
8146 static ssize_t lun_format_store(struct device_driver *ddp, const char *buf,
8147 				size_t count)
8148 {
8149 	int n;
8150 	bool changed;
8151 
8152 	if (kstrtoint(buf, 0, &n))
8153 		return -EINVAL;
8154 	if (n >= 0) {
8155 		if (n > (int)SAM_LUN_AM_FLAT) {
8156 			pr_warn("only LUN address methods 0 and 1 are supported\n");
8157 			return -EINVAL;
8158 		}
8159 		changed = ((int)sdebug_lun_am != n);
8160 		sdebug_lun_am = n;
8161 		if (changed && sdebug_scsi_level >= 5) {	/* >= SPC-3 */
8162 			struct sdebug_host_info *sdhp;
8163 			struct sdebug_dev_info *dp;
8164 
8165 			mutex_lock(&sdebug_host_list_mutex);
8166 			list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
8167 				list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) {
8168 					set_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm);
8169 				}
8170 			}
8171 			mutex_unlock(&sdebug_host_list_mutex);
8172 		}
8173 		return count;
8174 	}
8175 	return -EINVAL;
8176 }
8177 static DRIVER_ATTR_RW(lun_format);
8178 
8179 static ssize_t max_luns_show(struct device_driver *ddp, char *buf)
8180 {
8181 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns);
8182 }
8183 static ssize_t max_luns_store(struct device_driver *ddp, const char *buf,
8184 			      size_t count)
8185 {
8186 	int n;
8187 	bool changed;
8188 
8189 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8190 		if (n > 256) {
8191 			pr_warn("max_luns can be no more than 256\n");
8192 			return -EINVAL;
8193 		}
8194 		changed = (sdebug_max_luns != n);
8195 		sdebug_max_luns = n;
8196 		sdebug_max_tgts_luns();
8197 		if (changed && (sdebug_scsi_level >= 5)) {	/* >= SPC-3 */
8198 			struct sdebug_host_info *sdhp;
8199 			struct sdebug_dev_info *dp;
8200 
8201 			mutex_lock(&sdebug_host_list_mutex);
8202 			list_for_each_entry(sdhp, &sdebug_host_list,
8203 					    host_list) {
8204 				list_for_each_entry(dp, &sdhp->dev_info_list,
8205 						    dev_list) {
8206 					set_bit(SDEBUG_UA_LUNS_CHANGED,
8207 						dp->uas_bm);
8208 				}
8209 			}
8210 			mutex_unlock(&sdebug_host_list_mutex);
8211 		}
8212 		return count;
8213 	}
8214 	return -EINVAL;
8215 }
8216 static DRIVER_ATTR_RW(max_luns);
8217 
8218 static ssize_t max_queue_show(struct device_driver *ddp, char *buf)
8219 {
8220 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue);
8221 }
8222 /* N.B. max_queue can be changed while there are queued commands. In flight
8223  * commands beyond the new max_queue will be completed. */
8224 static ssize_t max_queue_store(struct device_driver *ddp, const char *buf,
8225 			       size_t count)
8226 {
8227 	int n;
8228 
8229 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) &&
8230 	    (n <= SDEBUG_CANQUEUE) &&
8231 	    (sdebug_host_max_queue == 0)) {
8232 		mutex_lock(&sdebug_host_list_mutex);
8233 
8234 		/* We may only change sdebug_max_queue when we have no shosts */
8235 		if (list_empty(&sdebug_host_list))
8236 			sdebug_max_queue = n;
8237 		else
8238 			count = -EBUSY;
8239 		mutex_unlock(&sdebug_host_list_mutex);
8240 		return count;
8241 	}
8242 	return -EINVAL;
8243 }
8244 static DRIVER_ATTR_RW(max_queue);
8245 
8246 static ssize_t host_max_queue_show(struct device_driver *ddp, char *buf)
8247 {
8248 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_host_max_queue);
8249 }
8250 
8251 static ssize_t no_rwlock_show(struct device_driver *ddp, char *buf)
8252 {
8253 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_rwlock);
8254 }
8255 
8256 static ssize_t no_rwlock_store(struct device_driver *ddp, const char *buf, size_t count)
8257 {
8258 	bool v;
8259 
8260 	if (kstrtobool(buf, &v))
8261 		return -EINVAL;
8262 
8263 	sdebug_no_rwlock = v;
8264 	return count;
8265 }
8266 static DRIVER_ATTR_RW(no_rwlock);
8267 
8268 /*
8269  * Since this is used for .can_queue, and we get the hc_idx tag from the bitmap
8270  * in range [0, sdebug_host_max_queue), we can't change it.
8271  */
8272 static DRIVER_ATTR_RO(host_max_queue);
8273 
8274 static ssize_t no_uld_show(struct device_driver *ddp, char *buf)
8275 {
8276 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld);
8277 }
8278 static DRIVER_ATTR_RO(no_uld);
8279 
8280 static ssize_t scsi_level_show(struct device_driver *ddp, char *buf)
8281 {
8282 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level);
8283 }
8284 static DRIVER_ATTR_RO(scsi_level);
8285 
8286 static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf)
8287 {
8288 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb);
8289 }
8290 static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf,
8291 				size_t count)
8292 {
8293 	int n;
8294 	bool changed;
8295 
8296 	/* Ignore capacity change for ZBC drives for now */
8297 	if (sdeb_zbc_in_use)
8298 		return -ENOTSUPP;
8299 
8300 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8301 		changed = (sdebug_virtual_gb != n);
8302 		sdebug_virtual_gb = n;
8303 		sdebug_capacity = get_sdebug_capacity();
8304 		if (changed) {
8305 			struct sdebug_host_info *sdhp;
8306 			struct sdebug_dev_info *dp;
8307 
8308 			mutex_lock(&sdebug_host_list_mutex);
8309 			list_for_each_entry(sdhp, &sdebug_host_list,
8310 					    host_list) {
8311 				list_for_each_entry(dp, &sdhp->dev_info_list,
8312 						    dev_list) {
8313 					set_bit(SDEBUG_UA_CAPACITY_CHANGED,
8314 						dp->uas_bm);
8315 				}
8316 			}
8317 			mutex_unlock(&sdebug_host_list_mutex);
8318 		}
8319 		return count;
8320 	}
8321 	return -EINVAL;
8322 }
8323 static DRIVER_ATTR_RW(virtual_gb);
8324 
8325 static ssize_t add_host_show(struct device_driver *ddp, char *buf)
8326 {
8327 	/* absolute number of hosts currently active is what is shown */
8328 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_hosts);
8329 }
8330 
8331 static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
8332 			      size_t count)
8333 {
8334 	bool found;
8335 	unsigned long idx;
8336 	struct sdeb_store_info *sip;
8337 	bool want_phs = (sdebug_fake_rw == 0) && sdebug_per_host_store;
8338 	int delta_hosts;
8339 
8340 	if (sscanf(buf, "%d", &delta_hosts) != 1)
8341 		return -EINVAL;
8342 	if (delta_hosts > 0) {
8343 		do {
8344 			found = false;
8345 			if (want_phs) {
8346 				xa_for_each_marked(per_store_ap, idx, sip,
8347 						   SDEB_XA_NOT_IN_USE) {
8348 					sdeb_most_recent_idx = (int)idx;
8349 					found = true;
8350 					break;
8351 				}
8352 				if (found)	/* re-use case */
8353 					sdebug_add_host_helper((int)idx);
8354 				else
8355 					sdebug_do_add_host(true);
8356 			} else {
8357 				sdebug_do_add_host(false);
8358 			}
8359 		} while (--delta_hosts);
8360 	} else if (delta_hosts < 0) {
8361 		do {
8362 			sdebug_do_remove_host(false);
8363 		} while (++delta_hosts);
8364 	}
8365 	return count;
8366 }
8367 static DRIVER_ATTR_RW(add_host);
8368 
8369 static ssize_t vpd_use_hostno_show(struct device_driver *ddp, char *buf)
8370 {
8371 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno);
8372 }
8373 static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf,
8374 				    size_t count)
8375 {
8376 	int n;
8377 
8378 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8379 		sdebug_vpd_use_hostno = n;
8380 		return count;
8381 	}
8382 	return -EINVAL;
8383 }
8384 static DRIVER_ATTR_RW(vpd_use_hostno);
8385 
8386 static ssize_t statistics_show(struct device_driver *ddp, char *buf)
8387 {
8388 	return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_statistics);
8389 }
8390 static ssize_t statistics_store(struct device_driver *ddp, const char *buf,
8391 				size_t count)
8392 {
8393 	int n;
8394 
8395 	if ((count > 0) && (sscanf(buf, "%d", &n) == 1) && (n >= 0)) {
8396 		if (n > 0)
8397 			sdebug_statistics = true;
8398 		else {
8399 			clear_queue_stats();
8400 			sdebug_statistics = false;
8401 		}
8402 		return count;
8403 	}
8404 	return -EINVAL;
8405 }
8406 static DRIVER_ATTR_RW(statistics);
8407 
8408 static ssize_t sector_size_show(struct device_driver *ddp, char *buf)
8409 {
8410 	return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size);
8411 }
8412 static DRIVER_ATTR_RO(sector_size);
8413 
8414 static ssize_t submit_queues_show(struct device_driver *ddp, char *buf)
8415 {
8416 	return scnprintf(buf, PAGE_SIZE, "%d\n", submit_queues);
8417 }
8418 static DRIVER_ATTR_RO(submit_queues);
8419 
8420 static ssize_t dix_show(struct device_driver *ddp, char *buf)
8421 {
8422 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix);
8423 }
8424 static DRIVER_ATTR_RO(dix);
8425 
8426 static ssize_t dif_show(struct device_driver *ddp, char *buf)
8427 {
8428 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif);
8429 }
8430 static DRIVER_ATTR_RO(dif);
8431 
8432 static ssize_t guard_show(struct device_driver *ddp, char *buf)
8433 {
8434 	return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard);
8435 }
8436 static DRIVER_ATTR_RO(guard);
8437 
8438 static ssize_t ato_show(struct device_driver *ddp, char *buf)
8439 {
8440 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato);
8441 }
8442 static DRIVER_ATTR_RO(ato);
8443 
8444 static ssize_t map_show(struct device_driver *ddp, char *buf)
8445 {
8446 	ssize_t count = 0;
8447 
8448 	if (!scsi_debug_lbp())
8449 		return scnprintf(buf, PAGE_SIZE, "0-%u\n",
8450 				 sdebug_store_sectors);
8451 
8452 	if (sdebug_fake_rw == 0 && !xa_empty(per_store_ap)) {
8453 		struct sdeb_store_info *sip = xa_load(per_store_ap, 0);
8454 
8455 		if (sip)
8456 			count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
8457 					  (int)map_size, sip->map_storep);
8458 	}
8459 	buf[count++] = '\n';
8460 	buf[count] = '\0';
8461 
8462 	return count;
8463 }
8464 static DRIVER_ATTR_RO(map);
8465 
8466 static ssize_t random_show(struct device_driver *ddp, char *buf)
8467 {
8468 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_random);
8469 }
8470 
8471 static ssize_t random_store(struct device_driver *ddp, const char *buf,
8472 			    size_t count)
8473 {
8474 	bool v;
8475 
8476 	if (kstrtobool(buf, &v))
8477 		return -EINVAL;
8478 
8479 	sdebug_random = v;
8480 	return count;
8481 }
8482 static DRIVER_ATTR_RW(random);
8483 
8484 static ssize_t removable_show(struct device_driver *ddp, char *buf)
8485 {
8486 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0);
8487 }
8488 static ssize_t removable_store(struct device_driver *ddp, const char *buf,
8489 			       size_t count)
8490 {
8491 	int n;
8492 
8493 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8494 		sdebug_removable = (n > 0);
8495 		return count;
8496 	}
8497 	return -EINVAL;
8498 }
8499 static DRIVER_ATTR_RW(removable);
8500 
8501 static ssize_t host_lock_show(struct device_driver *ddp, char *buf)
8502 {
8503 	return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock);
8504 }
8505 /* N.B. sdebug_host_lock does nothing, kept for backward compatibility */
8506 static ssize_t host_lock_store(struct device_driver *ddp, const char *buf,
8507 			       size_t count)
8508 {
8509 	int n;
8510 
8511 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8512 		sdebug_host_lock = (n > 0);
8513 		return count;
8514 	}
8515 	return -EINVAL;
8516 }
8517 static DRIVER_ATTR_RW(host_lock);
8518 
8519 static ssize_t strict_show(struct device_driver *ddp, char *buf)
8520 {
8521 	return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict);
8522 }
8523 static ssize_t strict_store(struct device_driver *ddp, const char *buf,
8524 			    size_t count)
8525 {
8526 	int n;
8527 
8528 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8529 		sdebug_strict = (n > 0);
8530 		return count;
8531 	}
8532 	return -EINVAL;
8533 }
8534 static DRIVER_ATTR_RW(strict);
8535 
8536 static ssize_t uuid_ctl_show(struct device_driver *ddp, char *buf)
8537 {
8538 	return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_uuid_ctl);
8539 }
8540 static DRIVER_ATTR_RO(uuid_ctl);
8541 
8542 static ssize_t cdb_len_show(struct device_driver *ddp, char *buf)
8543 {
8544 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_cdb_len);
8545 }
8546 static ssize_t cdb_len_store(struct device_driver *ddp, const char *buf,
8547 			     size_t count)
8548 {
8549 	int ret, n;
8550 
8551 	ret = kstrtoint(buf, 0, &n);
8552 	if (ret)
8553 		return ret;
8554 	sdebug_cdb_len = n;
8555 	all_config_cdb_len();
8556 	return count;
8557 }
8558 static DRIVER_ATTR_RW(cdb_len);
8559 
8560 static const char * const zbc_model_strs_a[] = {
8561 	[BLK_ZONED_NONE] = "none",
8562 	[BLK_ZONED_HA]   = "host-aware",
8563 	[BLK_ZONED_HM]   = "host-managed",
8564 };
8565 
8566 static const char * const zbc_model_strs_b[] = {
8567 	[BLK_ZONED_NONE] = "no",
8568 	[BLK_ZONED_HA]   = "aware",
8569 	[BLK_ZONED_HM]   = "managed",
8570 };
8571 
8572 static const char * const zbc_model_strs_c[] = {
8573 	[BLK_ZONED_NONE] = "0",
8574 	[BLK_ZONED_HA]   = "1",
8575 	[BLK_ZONED_HM]   = "2",
8576 };
8577 
8578 static int sdeb_zbc_model_str(const char *cp)
8579 {
8580 	int res = sysfs_match_string(zbc_model_strs_a, cp);
8581 
8582 	if (res < 0) {
8583 		res = sysfs_match_string(zbc_model_strs_b, cp);
8584 		if (res < 0) {
8585 			res = sysfs_match_string(zbc_model_strs_c, cp);
8586 			if (res < 0)
8587 				return -EINVAL;
8588 		}
8589 	}
8590 	return res;
8591 }
8592 
8593 static ssize_t zbc_show(struct device_driver *ddp, char *buf)
8594 {
8595 	return scnprintf(buf, PAGE_SIZE, "%s\n",
8596 			 zbc_model_strs_a[sdeb_zbc_model]);
8597 }
8598 static DRIVER_ATTR_RO(zbc);
8599 
8600 static ssize_t tur_ms_to_ready_show(struct device_driver *ddp, char *buf)
8601 {
8602 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdeb_tur_ms_to_ready);
8603 }
8604 static DRIVER_ATTR_RO(tur_ms_to_ready);
8605 
8606 static ssize_t group_number_stats_show(struct device_driver *ddp, char *buf)
8607 {
8608 	char *p = buf, *end = buf + PAGE_SIZE;
8609 	int i;
8610 
8611 	for (i = 0; i < ARRAY_SIZE(writes_by_group_number); i++)
8612 		p += scnprintf(p, end - p, "%d %ld\n", i,
8613 			       atomic_long_read(&writes_by_group_number[i]));
8614 
8615 	return p - buf;
8616 }
8617 
8618 static ssize_t group_number_stats_store(struct device_driver *ddp,
8619 					const char *buf, size_t count)
8620 {
8621 	int i;
8622 
8623 	for (i = 0; i < ARRAY_SIZE(writes_by_group_number); i++)
8624 		atomic_long_set(&writes_by_group_number[i], 0);
8625 
8626 	return count;
8627 }
8628 static DRIVER_ATTR_RW(group_number_stats);
8629 
8630 /* Note: The following array creates attribute files in the
8631    /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
8632    files (over those found in the /sys/module/scsi_debug/parameters
8633    directory) is that auxiliary actions can be triggered when an attribute
8634    is changed. For example see: add_host_store() above.
8635  */
8636 
8637 static struct attribute *sdebug_drv_attrs[] = {
8638 	&driver_attr_delay.attr,
8639 	&driver_attr_opts.attr,
8640 	&driver_attr_ptype.attr,
8641 	&driver_attr_dsense.attr,
8642 	&driver_attr_fake_rw.attr,
8643 	&driver_attr_host_max_queue.attr,
8644 	&driver_attr_no_lun_0.attr,
8645 	&driver_attr_num_tgts.attr,
8646 	&driver_attr_dev_size_mb.attr,
8647 	&driver_attr_num_parts.attr,
8648 	&driver_attr_every_nth.attr,
8649 	&driver_attr_lun_format.attr,
8650 	&driver_attr_max_luns.attr,
8651 	&driver_attr_max_queue.attr,
8652 	&driver_attr_no_rwlock.attr,
8653 	&driver_attr_no_uld.attr,
8654 	&driver_attr_scsi_level.attr,
8655 	&driver_attr_virtual_gb.attr,
8656 	&driver_attr_add_host.attr,
8657 	&driver_attr_per_host_store.attr,
8658 	&driver_attr_vpd_use_hostno.attr,
8659 	&driver_attr_sector_size.attr,
8660 	&driver_attr_statistics.attr,
8661 	&driver_attr_submit_queues.attr,
8662 	&driver_attr_dix.attr,
8663 	&driver_attr_dif.attr,
8664 	&driver_attr_guard.attr,
8665 	&driver_attr_ato.attr,
8666 	&driver_attr_map.attr,
8667 	&driver_attr_random.attr,
8668 	&driver_attr_removable.attr,
8669 	&driver_attr_host_lock.attr,
8670 	&driver_attr_ndelay.attr,
8671 	&driver_attr_strict.attr,
8672 	&driver_attr_uuid_ctl.attr,
8673 	&driver_attr_cdb_len.attr,
8674 	&driver_attr_tur_ms_to_ready.attr,
8675 	&driver_attr_zbc.attr,
8676 	&driver_attr_group_number_stats.attr,
8677 	NULL,
8678 };
8679 ATTRIBUTE_GROUPS(sdebug_drv);
8680 
8681 static struct device *pseudo_primary;
8682 
8683 static int __init scsi_debug_init(void)
8684 {
8685 	bool want_store = (sdebug_fake_rw == 0);
8686 	unsigned long sz;
8687 	int k, ret, hosts_to_add;
8688 	int idx = -1;
8689 
8690 	if (sdebug_ndelay >= 1000 * 1000 * 1000) {
8691 		pr_warn("ndelay must be less than 1 second, ignored\n");
8692 		sdebug_ndelay = 0;
8693 	} else if (sdebug_ndelay > 0)
8694 		sdebug_jdelay = JDELAY_OVERRIDDEN;
8695 
8696 	switch (sdebug_sector_size) {
8697 	case  512:
8698 	case 1024:
8699 	case 2048:
8700 	case 4096:
8701 		break;
8702 	default:
8703 		pr_err("invalid sector_size %d\n", sdebug_sector_size);
8704 		return -EINVAL;
8705 	}
8706 
8707 	switch (sdebug_dif) {
8708 	case T10_PI_TYPE0_PROTECTION:
8709 		break;
8710 	case T10_PI_TYPE1_PROTECTION:
8711 	case T10_PI_TYPE2_PROTECTION:
8712 	case T10_PI_TYPE3_PROTECTION:
8713 		have_dif_prot = true;
8714 		break;
8715 
8716 	default:
8717 		pr_err("dif must be 0, 1, 2 or 3\n");
8718 		return -EINVAL;
8719 	}
8720 
8721 	if (sdebug_num_tgts < 0) {
8722 		pr_err("num_tgts must be >= 0\n");
8723 		return -EINVAL;
8724 	}
8725 
8726 	if (sdebug_guard > 1) {
8727 		pr_err("guard must be 0 or 1\n");
8728 		return -EINVAL;
8729 	}
8730 
8731 	if (sdebug_ato > 1) {
8732 		pr_err("ato must be 0 or 1\n");
8733 		return -EINVAL;
8734 	}
8735 
8736 	if (sdebug_physblk_exp < 0 || sdebug_physblk_exp > 15) {
8737 		pr_err("invalid physblk_exp %d\n", sdebug_physblk_exp);
8738 		return -EINVAL;
8739 	}
8740 
8741 	sdebug_lun_am = sdebug_lun_am_i;
8742 	if (sdebug_lun_am > SAM_LUN_AM_FLAT) {
8743 		pr_warn("Invalid LUN format %u, using default\n", (int)sdebug_lun_am);
8744 		sdebug_lun_am = SAM_LUN_AM_PERIPHERAL;
8745 	}
8746 
8747 	if (sdebug_max_luns > 256) {
8748 		if (sdebug_max_luns > 16384) {
8749 			pr_warn("max_luns can be no more than 16384, use default\n");
8750 			sdebug_max_luns = DEF_MAX_LUNS;
8751 		}
8752 		sdebug_lun_am = SAM_LUN_AM_FLAT;
8753 	}
8754 
8755 	if (sdebug_lowest_aligned > 0x3fff) {
8756 		pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned);
8757 		return -EINVAL;
8758 	}
8759 
8760 	if (submit_queues < 1) {
8761 		pr_err("submit_queues must be 1 or more\n");
8762 		return -EINVAL;
8763 	}
8764 
8765 	if ((sdebug_max_queue > SDEBUG_CANQUEUE) || (sdebug_max_queue < 1)) {
8766 		pr_err("max_queue must be in range [1, %d]\n", SDEBUG_CANQUEUE);
8767 		return -EINVAL;
8768 	}
8769 
8770 	if ((sdebug_host_max_queue > SDEBUG_CANQUEUE) ||
8771 	    (sdebug_host_max_queue < 0)) {
8772 		pr_err("host_max_queue must be in range [0 %d]\n",
8773 		       SDEBUG_CANQUEUE);
8774 		return -EINVAL;
8775 	}
8776 
8777 	if (sdebug_host_max_queue &&
8778 	    (sdebug_max_queue != sdebug_host_max_queue)) {
8779 		sdebug_max_queue = sdebug_host_max_queue;
8780 		pr_warn("fixing max submit queue depth to host max queue depth, %d\n",
8781 			sdebug_max_queue);
8782 	}
8783 
8784 	/*
8785 	 * check for host managed zoned block device specified with
8786 	 * ptype=0x14 or zbc=XXX.
8787 	 */
8788 	if (sdebug_ptype == TYPE_ZBC) {
8789 		sdeb_zbc_model = BLK_ZONED_HM;
8790 	} else if (sdeb_zbc_model_s && *sdeb_zbc_model_s) {
8791 		k = sdeb_zbc_model_str(sdeb_zbc_model_s);
8792 		if (k < 0)
8793 			return k;
8794 		sdeb_zbc_model = k;
8795 		switch (sdeb_zbc_model) {
8796 		case BLK_ZONED_NONE:
8797 		case BLK_ZONED_HA:
8798 			sdebug_ptype = TYPE_DISK;
8799 			break;
8800 		case BLK_ZONED_HM:
8801 			sdebug_ptype = TYPE_ZBC;
8802 			break;
8803 		default:
8804 			pr_err("Invalid ZBC model\n");
8805 			return -EINVAL;
8806 		}
8807 	}
8808 	if (sdeb_zbc_model != BLK_ZONED_NONE) {
8809 		sdeb_zbc_in_use = true;
8810 		if (sdebug_dev_size_mb == DEF_DEV_SIZE_PRE_INIT)
8811 			sdebug_dev_size_mb = DEF_ZBC_DEV_SIZE_MB;
8812 	}
8813 
8814 	if (sdeb_zbc_in_use && sdebug_lowest_aligned) {
8815 		pr_err("lowest_aligned is not supported by a zoned device\n");
8816 		return -EINVAL;
8817 	}
8818 
8819 	if (sdeb_zbc_in_use && sdebug_atomic_wr) {
8820 		pr_err("atomic_wr is not supported by a zoned device\n");
8821 		return -EINVAL;
8822 	}
8823 
8824 	if (sdebug_dev_size_mb == DEF_DEV_SIZE_PRE_INIT)
8825 		sdebug_dev_size_mb = DEF_DEV_SIZE_MB;
8826 	if (sdebug_dev_size_mb < 1)
8827 		sdebug_dev_size_mb = 1;  /* force minimum 1 MB ramdisk */
8828 	sz = (unsigned long)sdebug_dev_size_mb * 1048576;
8829 	sdebug_store_sectors = sz / sdebug_sector_size;
8830 	sdebug_capacity = get_sdebug_capacity();
8831 
8832 	/* play around with geometry, don't waste too much on track 0 */
8833 	sdebug_heads = 8;
8834 	sdebug_sectors_per = 32;
8835 	if (sdebug_dev_size_mb >= 256)
8836 		sdebug_heads = 64;
8837 	else if (sdebug_dev_size_mb >= 16)
8838 		sdebug_heads = 32;
8839 	sdebug_cylinders_per = (unsigned long)sdebug_capacity /
8840 			       (sdebug_sectors_per * sdebug_heads);
8841 	if (sdebug_cylinders_per >= 1024) {
8842 		/* other LLDs do this; implies >= 1GB ram disk ... */
8843 		sdebug_heads = 255;
8844 		sdebug_sectors_per = 63;
8845 		sdebug_cylinders_per = (unsigned long)sdebug_capacity /
8846 			       (sdebug_sectors_per * sdebug_heads);
8847 	}
8848 	if (scsi_debug_lbp()) {
8849 		sdebug_unmap_max_blocks =
8850 			clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU);
8851 
8852 		sdebug_unmap_max_desc =
8853 			clamp(sdebug_unmap_max_desc, 0U, 256U);
8854 
8855 		sdebug_unmap_granularity =
8856 			clamp(sdebug_unmap_granularity, 1U, 0xffffffffU);
8857 
8858 		if (sdebug_unmap_alignment &&
8859 		    sdebug_unmap_granularity <=
8860 		    sdebug_unmap_alignment) {
8861 			pr_err("ERR: unmap_granularity <= unmap_alignment\n");
8862 			return -EINVAL;
8863 		}
8864 	}
8865 
8866 	xa_init_flags(per_store_ap, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ);
8867 	if (want_store) {
8868 		idx = sdebug_add_store();
8869 		if (idx < 0)
8870 			return idx;
8871 	}
8872 
8873 	pseudo_primary = root_device_register("pseudo_0");
8874 	if (IS_ERR(pseudo_primary)) {
8875 		pr_warn("root_device_register() error\n");
8876 		ret = PTR_ERR(pseudo_primary);
8877 		goto free_vm;
8878 	}
8879 	ret = bus_register(&pseudo_lld_bus);
8880 	if (ret < 0) {
8881 		pr_warn("bus_register error: %d\n", ret);
8882 		goto dev_unreg;
8883 	}
8884 	ret = driver_register(&sdebug_driverfs_driver);
8885 	if (ret < 0) {
8886 		pr_warn("driver_register error: %d\n", ret);
8887 		goto bus_unreg;
8888 	}
8889 
8890 	hosts_to_add = sdebug_add_host;
8891 	sdebug_add_host = 0;
8892 
8893 	sdebug_debugfs_root = debugfs_create_dir("scsi_debug", NULL);
8894 	if (IS_ERR_OR_NULL(sdebug_debugfs_root))
8895 		pr_info("failed to create initial debugfs directory\n");
8896 
8897 	for (k = 0; k < hosts_to_add; k++) {
8898 		if (want_store && k == 0) {
8899 			ret = sdebug_add_host_helper(idx);
8900 			if (ret < 0) {
8901 				pr_err("add_host_helper k=%d, error=%d\n",
8902 				       k, -ret);
8903 				break;
8904 			}
8905 		} else {
8906 			ret = sdebug_do_add_host(want_store &&
8907 						 sdebug_per_host_store);
8908 			if (ret < 0) {
8909 				pr_err("add_host k=%d error=%d\n", k, -ret);
8910 				break;
8911 			}
8912 		}
8913 	}
8914 	if (sdebug_verbose)
8915 		pr_info("built %d host(s)\n", sdebug_num_hosts);
8916 
8917 	return 0;
8918 
8919 bus_unreg:
8920 	bus_unregister(&pseudo_lld_bus);
8921 dev_unreg:
8922 	root_device_unregister(pseudo_primary);
8923 free_vm:
8924 	sdebug_erase_store(idx, NULL);
8925 	return ret;
8926 }
8927 
8928 static void __exit scsi_debug_exit(void)
8929 {
8930 	int k = sdebug_num_hosts;
8931 
8932 	for (; k; k--)
8933 		sdebug_do_remove_host(true);
8934 	driver_unregister(&sdebug_driverfs_driver);
8935 	bus_unregister(&pseudo_lld_bus);
8936 	root_device_unregister(pseudo_primary);
8937 
8938 	sdebug_erase_all_stores(false);
8939 	xa_destroy(per_store_ap);
8940 	debugfs_remove(sdebug_debugfs_root);
8941 }
8942 
8943 device_initcall(scsi_debug_init);
8944 module_exit(scsi_debug_exit);
8945 
8946 static void sdebug_release_adapter(struct device *dev)
8947 {
8948 	struct sdebug_host_info *sdbg_host;
8949 
8950 	sdbg_host = dev_to_sdebug_host(dev);
8951 	kfree(sdbg_host);
8952 }
8953 
8954 /* idx must be valid, if sip is NULL then it will be obtained using idx */
8955 static void sdebug_erase_store(int idx, struct sdeb_store_info *sip)
8956 {
8957 	if (idx < 0)
8958 		return;
8959 	if (!sip) {
8960 		if (xa_empty(per_store_ap))
8961 			return;
8962 		sip = xa_load(per_store_ap, idx);
8963 		if (!sip)
8964 			return;
8965 	}
8966 	vfree(sip->map_storep);
8967 	vfree(sip->dif_storep);
8968 	vfree(sip->storep);
8969 	xa_erase(per_store_ap, idx);
8970 	kfree(sip);
8971 }
8972 
8973 /* Assume apart_from_first==false only in shutdown case. */
8974 static void sdebug_erase_all_stores(bool apart_from_first)
8975 {
8976 	unsigned long idx;
8977 	struct sdeb_store_info *sip = NULL;
8978 
8979 	xa_for_each(per_store_ap, idx, sip) {
8980 		if (apart_from_first)
8981 			apart_from_first = false;
8982 		else
8983 			sdebug_erase_store(idx, sip);
8984 	}
8985 	if (apart_from_first)
8986 		sdeb_most_recent_idx = sdeb_first_idx;
8987 }
8988 
8989 /*
8990  * Returns store xarray new element index (idx) if >=0 else negated errno.
8991  * Limit the number of stores to 65536.
8992  */
8993 static int sdebug_add_store(void)
8994 {
8995 	int res;
8996 	u32 n_idx;
8997 	unsigned long iflags;
8998 	unsigned long sz = (unsigned long)sdebug_dev_size_mb * 1048576;
8999 	struct sdeb_store_info *sip = NULL;
9000 	struct xa_limit xal = { .max = 1 << 16, .min = 0 };
9001 
9002 	sip = kzalloc_obj(*sip);
9003 	if (!sip)
9004 		return -ENOMEM;
9005 
9006 	xa_lock_irqsave(per_store_ap, iflags);
9007 	res = __xa_alloc(per_store_ap, &n_idx, sip, xal, GFP_ATOMIC);
9008 	if (unlikely(res < 0)) {
9009 		xa_unlock_irqrestore(per_store_ap, iflags);
9010 		kfree(sip);
9011 		pr_warn("xa_alloc() errno=%d\n", -res);
9012 		return res;
9013 	}
9014 	sdeb_most_recent_idx = n_idx;
9015 	if (sdeb_first_idx < 0)
9016 		sdeb_first_idx = n_idx;
9017 	xa_unlock_irqrestore(per_store_ap, iflags);
9018 
9019 	res = -ENOMEM;
9020 	sip->storep = vzalloc(sz);
9021 	if (!sip->storep) {
9022 		pr_err("user data oom\n");
9023 		goto err;
9024 	}
9025 	if (sdebug_num_parts > 0)
9026 		sdebug_build_parts(sip->storep, sz);
9027 
9028 	/* DIF/DIX: what T10 calls Protection Information (PI) */
9029 	if (sdebug_dix) {
9030 		int dif_size;
9031 
9032 		dif_size = sdebug_store_sectors * sizeof(struct t10_pi_tuple);
9033 		sip->dif_storep = vmalloc(dif_size);
9034 
9035 		pr_info("dif_storep %u bytes @ %p\n", dif_size,
9036 			sip->dif_storep);
9037 
9038 		if (!sip->dif_storep) {
9039 			pr_err("DIX oom\n");
9040 			goto err;
9041 		}
9042 		memset(sip->dif_storep, 0xff, dif_size);
9043 	}
9044 	/* Logical Block Provisioning */
9045 	if (scsi_debug_lbp()) {
9046 		map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1;
9047 		sip->map_storep = vcalloc(BITS_TO_LONGS(map_size),
9048 					  sizeof(long));
9049 
9050 		pr_info("%lu provisioning blocks\n", map_size);
9051 
9052 		if (!sip->map_storep) {
9053 			pr_err("LBP map oom\n");
9054 			goto err;
9055 		}
9056 
9057 		/* Map first 1KB for partition table */
9058 		if (sdebug_num_parts)
9059 			map_region(sip, 0, 2);
9060 	}
9061 
9062 	rwlock_init(&sip->macc_data_lck);
9063 	rwlock_init(&sip->macc_meta_lck);
9064 	rwlock_init(&sip->macc_sector_lck);
9065 	return (int)n_idx;
9066 err:
9067 	sdebug_erase_store((int)n_idx, sip);
9068 	pr_warn("failed, errno=%d\n", -res);
9069 	return res;
9070 }
9071 
9072 static int sdebug_add_host_helper(int per_host_idx)
9073 {
9074 	int k, devs_per_host, idx;
9075 	int error = -ENOMEM;
9076 	struct sdebug_host_info *sdbg_host;
9077 	struct sdebug_dev_info *sdbg_devinfo, *tmp;
9078 
9079 	sdbg_host = kzalloc_obj(*sdbg_host);
9080 	if (!sdbg_host)
9081 		return -ENOMEM;
9082 	idx = (per_host_idx < 0) ? sdeb_first_idx : per_host_idx;
9083 	if (xa_get_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE))
9084 		xa_clear_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE);
9085 	sdbg_host->si_idx = idx;
9086 
9087 	INIT_LIST_HEAD(&sdbg_host->dev_info_list);
9088 
9089 	devs_per_host = sdebug_num_tgts * sdebug_max_luns;
9090 	for (k = 0; k < devs_per_host; k++) {
9091 		sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL);
9092 		if (!sdbg_devinfo)
9093 			goto clean;
9094 	}
9095 
9096 	mutex_lock(&sdebug_host_list_mutex);
9097 	list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
9098 	mutex_unlock(&sdebug_host_list_mutex);
9099 
9100 	sdbg_host->dev.bus = &pseudo_lld_bus;
9101 	sdbg_host->dev.parent = pseudo_primary;
9102 	sdbg_host->dev.release = &sdebug_release_adapter;
9103 	dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_num_hosts);
9104 
9105 	error = device_register(&sdbg_host->dev);
9106 	if (error) {
9107 		mutex_lock(&sdebug_host_list_mutex);
9108 		list_del(&sdbg_host->host_list);
9109 		mutex_unlock(&sdebug_host_list_mutex);
9110 		goto clean;
9111 	}
9112 
9113 	++sdebug_num_hosts;
9114 	return 0;
9115 
9116 clean:
9117 	list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
9118 				 dev_list) {
9119 		list_del(&sdbg_devinfo->dev_list);
9120 		kfree(sdbg_devinfo->zstate);
9121 		kfree(sdbg_devinfo);
9122 	}
9123 	if (sdbg_host->dev.release)
9124 		put_device(&sdbg_host->dev);
9125 	else
9126 		kfree(sdbg_host);
9127 	pr_warn("failed, errno=%d\n", -error);
9128 	return error;
9129 }
9130 
9131 static int sdebug_do_add_host(bool mk_new_store)
9132 {
9133 	int ph_idx = sdeb_most_recent_idx;
9134 
9135 	if (mk_new_store) {
9136 		ph_idx = sdebug_add_store();
9137 		if (ph_idx < 0)
9138 			return ph_idx;
9139 	}
9140 	return sdebug_add_host_helper(ph_idx);
9141 }
9142 
9143 static void sdebug_do_remove_host(bool the_end)
9144 {
9145 	int idx = -1;
9146 	struct sdebug_host_info *sdbg_host = NULL;
9147 	struct sdebug_host_info *sdbg_host2;
9148 
9149 	mutex_lock(&sdebug_host_list_mutex);
9150 	if (!list_empty(&sdebug_host_list)) {
9151 		sdbg_host = list_entry(sdebug_host_list.prev,
9152 				       struct sdebug_host_info, host_list);
9153 		idx = sdbg_host->si_idx;
9154 	}
9155 	if (!the_end && idx >= 0) {
9156 		bool unique = true;
9157 
9158 		list_for_each_entry(sdbg_host2, &sdebug_host_list, host_list) {
9159 			if (sdbg_host2 == sdbg_host)
9160 				continue;
9161 			if (idx == sdbg_host2->si_idx) {
9162 				unique = false;
9163 				break;
9164 			}
9165 		}
9166 		if (unique) {
9167 			xa_set_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE);
9168 			if (idx == sdeb_most_recent_idx)
9169 				--sdeb_most_recent_idx;
9170 		}
9171 	}
9172 	if (sdbg_host)
9173 		list_del(&sdbg_host->host_list);
9174 	mutex_unlock(&sdebug_host_list_mutex);
9175 
9176 	if (!sdbg_host)
9177 		return;
9178 
9179 	device_unregister(&sdbg_host->dev);
9180 	--sdebug_num_hosts;
9181 }
9182 
9183 static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth)
9184 {
9185 	struct sdebug_dev_info *devip = sdev->hostdata;
9186 
9187 	if (!devip)
9188 		return	-ENODEV;
9189 
9190 	mutex_lock(&sdebug_host_list_mutex);
9191 	block_unblock_all_queues(true);
9192 
9193 	if (qdepth > SDEBUG_CANQUEUE) {
9194 		qdepth = SDEBUG_CANQUEUE;
9195 		pr_warn("requested qdepth [%d] exceeds canqueue [%d], trim\n",
9196 			qdepth, SDEBUG_CANQUEUE);
9197 	}
9198 	if (qdepth < 1)
9199 		qdepth = 1;
9200 	if (qdepth != sdev->queue_depth)
9201 		scsi_change_queue_depth(sdev, qdepth);
9202 
9203 	block_unblock_all_queues(false);
9204 	mutex_unlock(&sdebug_host_list_mutex);
9205 
9206 	if (SDEBUG_OPT_Q_NOISE & sdebug_opts)
9207 		sdev_printk(KERN_INFO, sdev, "qdepth=%d\n", qdepth);
9208 
9209 	return sdev->queue_depth;
9210 }
9211 
9212 static bool fake_timeout(struct scsi_cmnd *scp)
9213 {
9214 	if (0 == (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth))) {
9215 		if (sdebug_every_nth < -1)
9216 			sdebug_every_nth = -1;
9217 		if (SDEBUG_OPT_TIMEOUT & sdebug_opts)
9218 			return true; /* ignore command causing timeout */
9219 		else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts &&
9220 			 scsi_medium_access_command(scp))
9221 			return true; /* time out reads and writes */
9222 	}
9223 	return false;
9224 }
9225 
9226 /* Response to TUR or media access command when device stopped */
9227 static int resp_not_ready(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
9228 {
9229 	int stopped_state;
9230 	u64 diff_ns = 0;
9231 	ktime_t now_ts = ktime_get_boottime();
9232 	struct scsi_device *sdp = scp->device;
9233 
9234 	stopped_state = atomic_read(&devip->stopped);
9235 	if (stopped_state == 2) {
9236 		if (ktime_to_ns(now_ts) > ktime_to_ns(devip->create_ts)) {
9237 			diff_ns = ktime_to_ns(ktime_sub(now_ts, devip->create_ts));
9238 			if (diff_ns >= ((u64)sdeb_tur_ms_to_ready * 1000000)) {
9239 				/* tur_ms_to_ready timer extinguished */
9240 				atomic_set(&devip->stopped, 0);
9241 				return 0;
9242 			}
9243 		}
9244 		mk_sense_buffer(scp, NOT_READY,
9245 				LU_IS_IN_PROCESS_OF_BECOMING_READY);
9246 		if (sdebug_verbose)
9247 			sdev_printk(KERN_INFO, sdp,
9248 				    "%s: Not ready: in process of becoming ready\n", my_name);
9249 		if (scp->cmnd[0] == TEST_UNIT_READY) {
9250 			u64 tur_nanosecs_to_ready = (u64)sdeb_tur_ms_to_ready * 1000000;
9251 
9252 			if (diff_ns <= tur_nanosecs_to_ready)
9253 				diff_ns = tur_nanosecs_to_ready - diff_ns;
9254 			else
9255 				diff_ns = tur_nanosecs_to_ready;
9256 			/* As per 20-061r2 approved for spc6 by T10 on 20200716 */
9257 			do_div(diff_ns, 1000000);	/* diff_ns becomes milliseconds */
9258 			scsi_set_sense_information(scp->sense_buffer, SCSI_SENSE_BUFFERSIZE,
9259 						   diff_ns);
9260 			return check_condition_result;
9261 		}
9262 	}
9263 	mk_sense_buffer(scp, NOT_READY,
9264 			LU_NOT_READY_INITIALIZING_COMMAND_REQUIRED);
9265 	if (sdebug_verbose)
9266 		sdev_printk(KERN_INFO, sdp, "%s: Not ready: initializing command required\n",
9267 			    my_name);
9268 	return check_condition_result;
9269 }
9270 
9271 static void sdebug_map_queues(struct Scsi_Host *shost)
9272 {
9273 	int i, qoff;
9274 
9275 	if (shost->nr_hw_queues == 1)
9276 		return;
9277 
9278 	for (i = 0, qoff = 0; i < HCTX_MAX_TYPES; i++) {
9279 		struct blk_mq_queue_map *map = &shost->tag_set.map[i];
9280 
9281 		map->nr_queues  = 0;
9282 
9283 		if (i == HCTX_TYPE_DEFAULT)
9284 			map->nr_queues = submit_queues - poll_queues;
9285 		else if (i == HCTX_TYPE_POLL)
9286 			map->nr_queues = poll_queues;
9287 
9288 		if (!map->nr_queues) {
9289 			BUG_ON(i == HCTX_TYPE_DEFAULT);
9290 			continue;
9291 		}
9292 
9293 		map->queue_offset = qoff;
9294 		blk_mq_map_queues(map);
9295 
9296 		qoff += map->nr_queues;
9297 	}
9298 }
9299 
9300 struct sdebug_blk_mq_poll_data {
9301 	unsigned int queue_num;
9302 	int *num_entries;
9303 };
9304 
9305 /*
9306  * We don't handle aborted commands here, but it does not seem possible to have
9307  * aborted polled commands from schedule_resp()
9308  */
9309 static bool sdebug_blk_mq_poll_iter(struct request *rq, void *opaque)
9310 {
9311 	struct sdebug_blk_mq_poll_data *data = opaque;
9312 	struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
9313 	struct sdebug_scsi_cmd *sdsc = scsi_cmd_priv(cmd);
9314 	struct sdebug_defer *sd_dp;
9315 	u32 unique_tag = blk_mq_unique_tag(rq);
9316 	u16 hwq = blk_mq_unique_tag_to_hwq(unique_tag);
9317 	unsigned long flags;
9318 	int queue_num = data->queue_num;
9319 	ktime_t time;
9320 
9321 	/* We're only interested in one queue for this iteration */
9322 	if (hwq != queue_num)
9323 		return true;
9324 
9325 	/* Subsequent checks would fail if this failed, but check anyway */
9326 	if (!test_bit(SCMD_STATE_INFLIGHT, &cmd->state))
9327 		return true;
9328 
9329 	time = ktime_get_boottime();
9330 
9331 	spin_lock_irqsave(&sdsc->lock, flags);
9332 	sd_dp = &sdsc->sd_dp;
9333 	if (sd_dp->defer_t != SDEB_DEFER_POLL) {
9334 		spin_unlock_irqrestore(&sdsc->lock, flags);
9335 		return true;
9336 	}
9337 
9338 	if (time < sd_dp->cmpl_ts) {
9339 		spin_unlock_irqrestore(&sdsc->lock, flags);
9340 		return true;
9341 	}
9342 	spin_unlock_irqrestore(&sdsc->lock, flags);
9343 
9344 	if (sdebug_statistics) {
9345 		atomic_inc(&sdebug_completions);
9346 		if (raw_smp_processor_id() != sd_dp->issuing_cpu)
9347 			atomic_inc(&sdebug_miss_cpus);
9348 	}
9349 
9350 	scsi_done(cmd); /* callback to mid level */
9351 	(*data->num_entries)++;
9352 	return true;
9353 }
9354 
9355 static int sdebug_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
9356 {
9357 	int num_entries = 0;
9358 	struct sdebug_blk_mq_poll_data data = {
9359 		.queue_num = queue_num,
9360 		.num_entries = &num_entries,
9361 	};
9362 
9363 	blk_mq_tagset_busy_iter(&shost->tag_set, sdebug_blk_mq_poll_iter,
9364 				&data);
9365 
9366 	if (num_entries > 0)
9367 		atomic_add(num_entries, &sdeb_mq_poll_count);
9368 	return num_entries;
9369 }
9370 
9371 static int sdebug_timeout_cmd(struct scsi_cmnd *cmnd)
9372 {
9373 	struct scsi_device *sdp = cmnd->device;
9374 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
9375 	struct sdebug_err_inject *err;
9376 	unsigned char *cmd = cmnd->cmnd;
9377 	int ret = 0;
9378 
9379 	if (devip == NULL)
9380 		return 0;
9381 
9382 	rcu_read_lock();
9383 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
9384 		if (err->type == ERR_TMOUT_CMD &&
9385 		    (err->cmd == cmd[0] || err->cmd == 0xff)) {
9386 			ret = !!err->cnt;
9387 			if (err->cnt < 0)
9388 				err->cnt++;
9389 
9390 			rcu_read_unlock();
9391 			return ret;
9392 		}
9393 	}
9394 	rcu_read_unlock();
9395 
9396 	return 0;
9397 }
9398 
9399 static int sdebug_fail_queue_cmd(struct scsi_cmnd *cmnd)
9400 {
9401 	struct scsi_device *sdp = cmnd->device;
9402 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
9403 	struct sdebug_err_inject *err;
9404 	unsigned char *cmd = cmnd->cmnd;
9405 	int ret = 0;
9406 
9407 	if (devip == NULL)
9408 		return 0;
9409 
9410 	rcu_read_lock();
9411 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
9412 		if (err->type == ERR_FAIL_QUEUE_CMD &&
9413 		    (err->cmd == cmd[0] || err->cmd == 0xff)) {
9414 			ret = err->cnt ? err->queuecmd_ret : 0;
9415 			if (err->cnt < 0)
9416 				err->cnt++;
9417 
9418 			rcu_read_unlock();
9419 			return ret;
9420 		}
9421 	}
9422 	rcu_read_unlock();
9423 
9424 	return 0;
9425 }
9426 
9427 static int sdebug_fail_cmd(struct scsi_cmnd *cmnd, int *retval,
9428 			   struct sdebug_err_inject *info)
9429 {
9430 	struct scsi_device *sdp = cmnd->device;
9431 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
9432 	struct sdebug_err_inject *err;
9433 	unsigned char *cmd = cmnd->cmnd;
9434 	int ret = 0;
9435 	int result;
9436 
9437 	if (devip == NULL)
9438 		return 0;
9439 
9440 	rcu_read_lock();
9441 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
9442 		if (err->type == ERR_FAIL_CMD &&
9443 		    (err->cmd == cmd[0] || err->cmd == 0xff)) {
9444 			if (!err->cnt) {
9445 				rcu_read_unlock();
9446 				return 0;
9447 			}
9448 
9449 			ret = !!err->cnt;
9450 			rcu_read_unlock();
9451 			goto out_handle;
9452 		}
9453 	}
9454 	rcu_read_unlock();
9455 
9456 	return 0;
9457 
9458 out_handle:
9459 	if (err->cnt < 0)
9460 		err->cnt++;
9461 	mk_sense_buffer(cmnd, err->sense_key,
9462 			scsi_sense_code(err->asc, err->asq));
9463 	result = err->status_byte | err->host_byte << 16 | err->driver_byte << 24;
9464 	*info = *err;
9465 	*retval = schedule_resp(cmnd, devip, result, NULL, 0, 0);
9466 
9467 	return ret;
9468 }
9469 
9470 /* Process @scp, a request to abort a SCSI command by tag. */
9471 static void scsi_debug_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *scp)
9472 {
9473 	struct sdebug_internal_cmd *internal_cmd = scsi_cmd_priv(scp);
9474 	struct sdebug_abort_cmd *abort_cmd = &internal_cmd->abort_cmd;
9475 	const u32 unique_tag = abort_cmd->unique_tag;
9476 	struct scsi_cmnd *to_be_aborted_scmd =
9477 		scsi_host_find_tag(shost, unique_tag);
9478 	struct sdebug_scsi_cmd *to_be_aborted_sdsc =
9479 		scsi_cmd_priv(to_be_aborted_scmd);
9480 	bool res = false;
9481 
9482 	if (!to_be_aborted_scmd) {
9483 		pr_err("command with tag %#x not found\n", unique_tag);
9484 		return;
9485 	}
9486 
9487 	scoped_guard(spinlock_irqsave, &to_be_aborted_sdsc->lock)
9488 		res = scsi_debug_stop_cmnd(to_be_aborted_scmd);
9489 
9490 	if (res)
9491 		pr_info("aborted command with tag %#x\n", unique_tag);
9492 	else
9493 		pr_err("failed to abort command with tag %#x\n", unique_tag);
9494 
9495 	set_host_byte(scp, res ? DID_OK : DID_ERROR);
9496 }
9497 
9498 static enum scsi_qc_status
9499 scsi_debug_process_reserved_command(struct Scsi_Host *shost,
9500 				    struct scsi_cmnd *scp)
9501 {
9502 	struct sdebug_internal_cmd *internal_cmd = scsi_cmd_priv(scp);
9503 
9504 	switch (internal_cmd->type) {
9505 	case SCSI_DEBUG_ABORT_CMD:
9506 		scsi_debug_abort_cmd(shost, scp);
9507 		break;
9508 	default:
9509 		WARN_ON_ONCE(true);
9510 		set_host_byte(scp, DID_ERROR);
9511 		break;
9512 	}
9513 
9514 	scsi_done(scp);
9515 	return 0;
9516 }
9517 
9518 static enum scsi_qc_status scsi_debug_queuecommand(struct Scsi_Host *shost,
9519 						   struct scsi_cmnd *scp)
9520 {
9521 	u8 sdeb_i;
9522 	struct scsi_device *sdp = scp->device;
9523 	const struct opcode_info_t *oip;
9524 	const struct opcode_info_t *r_oip;
9525 	struct sdebug_dev_info *devip;
9526 	u8 *cmd = scp->cmnd;
9527 	int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
9528 	int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *) = NULL;
9529 	int k, na;
9530 	int errsts = 0;
9531 	u64 lun_index = sdp->lun & 0x3FFF;
9532 	u32 flags;
9533 	u16 sa;
9534 	u8 opcode = cmd[0];
9535 	u32 devsel = sdebug_get_devsel(scp->device);
9536 	bool has_wlun_rl;
9537 	bool inject_now;
9538 	int ret = 0;
9539 	struct sdebug_err_inject err;
9540 
9541 	scsi_set_resid(scp, 0);
9542 	if (sdebug_statistics) {
9543 		atomic_inc(&sdebug_cmnd_count);
9544 		inject_now = inject_on_this_cmd();
9545 	} else {
9546 		inject_now = false;
9547 	}
9548 	if (unlikely(sdebug_verbose &&
9549 		     !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) {
9550 		char b[120];
9551 		int n, len, sb;
9552 
9553 		len = scp->cmd_len;
9554 		sb = (int)sizeof(b);
9555 		if (len > 32)
9556 			strcpy(b, "too long, over 32 bytes");
9557 		else {
9558 			for (k = 0, n = 0; k < len && n < sb; ++k)
9559 				n += scnprintf(b + n, sb - n, "%02x ",
9560 					       (u32)cmd[k]);
9561 		}
9562 		sdev_printk(KERN_INFO, sdp, "%s: tag=%#x, cmd %s\n", my_name,
9563 			    blk_mq_unique_tag(scsi_cmd_to_rq(scp)), b);
9564 	}
9565 	if (unlikely(inject_now && (sdebug_opts & SDEBUG_OPT_HOST_BUSY)))
9566 		return SCSI_MLQUEUE_HOST_BUSY;
9567 	has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS);
9568 	if (unlikely(lun_index >= sdebug_max_luns && !has_wlun_rl))
9569 		goto err_out;
9570 
9571 	sdeb_i = opcode_ind_arr[opcode];	/* fully mapped */
9572 	oip = &opcode_info_arr[sdeb_i];		/* safe if table consistent */
9573 	devip = (struct sdebug_dev_info *)sdp->hostdata;
9574 	if (unlikely(!devip)) {
9575 		devip = find_build_dev_info(sdp);
9576 		if (NULL == devip)
9577 			goto err_out;
9578 	}
9579 
9580 	if (sdebug_timeout_cmd(scp)) {
9581 		scmd_printk(KERN_INFO, scp, "timeout command 0x%x\n", opcode);
9582 		return 0;
9583 	}
9584 
9585 	ret = sdebug_fail_queue_cmd(scp);
9586 	if (ret) {
9587 		scmd_printk(KERN_INFO, scp, "fail queue command 0x%x with 0x%x\n",
9588 				opcode, ret);
9589 		return ret;
9590 	}
9591 
9592 	if (sdebug_fail_cmd(scp, &ret, &err)) {
9593 		scmd_printk(KERN_INFO, scp,
9594 			"fail command 0x%x with hostbyte=0x%x, "
9595 			"driverbyte=0x%x, statusbyte=0x%x, "
9596 			"sense_key=0x%x, asc=0x%x, asq=0x%x\n",
9597 			opcode, err.host_byte, err.driver_byte,
9598 			err.status_byte, err.sense_key, err.asc, err.asq);
9599 		return ret;
9600 	}
9601 
9602 	if (unlikely(inject_now && !atomic_read(&sdeb_inject_pending)))
9603 		atomic_set(&sdeb_inject_pending, 1);
9604 
9605 	na = oip->num_attached;
9606 	r_pfp = oip->pfp;
9607 	if (na) {	/* multiple commands with this opcode */
9608 		r_oip = oip;
9609 		if (FF_SA & r_oip->flags) {
9610 			if (F_SA_LOW & oip->flags)
9611 				sa = 0x1f & cmd[1];
9612 			else
9613 				sa = get_unaligned_be16(cmd + 8);
9614 			for (k = 0; k <= na; oip = r_oip->arrp + k++) {
9615 				if (opcode == oip->opcode && sa == oip->sa &&
9616 					(devsel & oip->devsel) != 0)
9617 					break;
9618 			}
9619 		} else {   /* since no service action only check opcode */
9620 			for (k = 0; k <= na; oip = r_oip->arrp + k++) {
9621 				if (opcode == oip->opcode &&
9622 					(devsel & oip->devsel) != 0)
9623 					break;
9624 			}
9625 		}
9626 		if (k > na) {
9627 			if (F_SA_LOW & r_oip->flags)
9628 				mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4);
9629 			else if (F_SA_HIGH & r_oip->flags)
9630 				mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7);
9631 			else
9632 				mk_sense_invalid_opcode(scp);
9633 			goto check_cond;
9634 		}
9635 	}	/* else (when na==0) we assume the oip is a match */
9636 	flags = oip->flags;
9637 	if (unlikely(F_INV_OP & flags)) {
9638 		mk_sense_invalid_opcode(scp);
9639 		goto check_cond;
9640 	}
9641 	if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) {
9642 		if (sdebug_verbose)
9643 			sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n",
9644 				    my_name, opcode, " supported for wlun");
9645 		mk_sense_invalid_opcode(scp);
9646 		goto check_cond;
9647 	}
9648 	if (unlikely(sdebug_strict)) {	/* check cdb against mask */
9649 		u8 rem;
9650 		int j;
9651 
9652 		for (k = 1; k < oip->len_mask[0] && k < 16; ++k) {
9653 			rem = ~oip->len_mask[k] & cmd[k];
9654 			if (rem) {
9655 				for (j = 7; j >= 0; --j, rem <<= 1) {
9656 					if (0x80 & rem)
9657 						break;
9658 				}
9659 				mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j);
9660 				goto check_cond;
9661 			}
9662 		}
9663 	}
9664 	if (unlikely(!(F_SKIP_UA & flags) &&
9665 		     find_first_bit(devip->uas_bm,
9666 				    SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) {
9667 		errsts = make_ua(scp, devip);
9668 		if (errsts)
9669 			goto check_cond;
9670 	}
9671 	if (unlikely(((F_M_ACCESS & flags) || scp->cmnd[0] == TEST_UNIT_READY) &&
9672 		     atomic_read(&devip->stopped))) {
9673 		errsts = resp_not_ready(scp, devip);
9674 		if (errsts)
9675 			goto fini;
9676 	}
9677 	if (sdebug_fake_rw && (F_FAKE_RW & flags))
9678 		goto fini;
9679 	if (unlikely(sdebug_every_nth)) {
9680 		if (fake_timeout(scp))
9681 			return 0;	/* ignore command: make trouble */
9682 	}
9683 	if (likely(oip->pfp))
9684 		pfp = oip->pfp;	/* calls a resp_* function */
9685 	else
9686 		pfp = r_pfp;    /* if leaf function ptr NULL, try the root's */
9687 
9688 fini:
9689 	if (F_DELAY_OVERR & flags)	/* cmds like INQUIRY respond asap */
9690 		return schedule_resp(scp, devip, errsts, pfp, 0, 0);
9691 	else if ((flags & F_LONG_DELAY) && (sdebug_jdelay > 0 ||
9692 					    sdebug_ndelay > 10000)) {
9693 		/*
9694 		 * Skip long delays if ndelay <= 10 microseconds. Otherwise
9695 		 * for Start Stop Unit (SSU) want at least 1 second delay and
9696 		 * if sdebug_jdelay>1 want a long delay of that many seconds.
9697 		 * For Synchronize Cache want 1/20 of SSU's delay.
9698 		 */
9699 		int jdelay = (sdebug_jdelay < 2) ? 1 : sdebug_jdelay;
9700 		int denom = (flags & F_SYNC_DELAY) ? 20 : 1;
9701 
9702 		jdelay = mult_frac(USER_HZ * jdelay, HZ, denom * USER_HZ);
9703 		return schedule_resp(scp, devip, errsts, pfp, jdelay, 0);
9704 	} else
9705 		return schedule_resp(scp, devip, errsts, pfp, sdebug_jdelay,
9706 				     sdebug_ndelay);
9707 check_cond:
9708 	return schedule_resp(scp, devip, check_condition_result, NULL, 0, 0);
9709 err_out:
9710 	return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, NULL, 0, 0);
9711 }
9712 
9713 static int sdebug_init_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
9714 {
9715 	struct sdebug_scsi_cmd *sdsc = scsi_cmd_priv(cmd);
9716 	struct sdebug_defer *sd_dp = &sdsc->sd_dp;
9717 
9718 	if (blk_mq_is_reserved_rq(scsi_cmd_to_rq(cmd)))
9719 		return 0;
9720 
9721 	spin_lock_init(&sdsc->lock);
9722 	hrtimer_setup(&sd_dp->hrt, sdebug_q_cmd_hrt_complete, CLOCK_MONOTONIC,
9723 		      HRTIMER_MODE_REL_PINNED);
9724 	INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete);
9725 
9726 	return 0;
9727 }
9728 
9729 static const struct scsi_host_template sdebug_driver_template = {
9730 	.show_info =		scsi_debug_show_info,
9731 	.write_info =		scsi_debug_write_info,
9732 	.proc_name =		sdebug_proc_name,
9733 	.name =			"SCSI DEBUG",
9734 	.info =			scsi_debug_info,
9735 	.sdev_init =		scsi_debug_sdev_init,
9736 	.sdev_configure =	scsi_debug_sdev_configure,
9737 	.sdev_destroy =		scsi_debug_sdev_destroy,
9738 	.ioctl =		scsi_debug_ioctl,
9739 	.queuecommand =		scsi_debug_queuecommand,
9740 	.queue_reserved_command = scsi_debug_process_reserved_command,
9741 	.change_queue_depth =	sdebug_change_qdepth,
9742 	.map_queues =		sdebug_map_queues,
9743 	.mq_poll =		sdebug_blk_mq_poll,
9744 	.eh_abort_handler =	scsi_debug_abort,
9745 	.eh_device_reset_handler = scsi_debug_device_reset,
9746 	.eh_target_reset_handler = scsi_debug_target_reset,
9747 	.eh_bus_reset_handler = scsi_debug_bus_reset,
9748 	.eh_host_reset_handler = scsi_debug_host_reset,
9749 	.can_queue =		SDEBUG_CANQUEUE,
9750 	.nr_reserved_cmds =	1,
9751 	.this_id =		7,
9752 	.sg_tablesize =		SG_MAX_SEGMENTS,
9753 	.cmd_per_lun =		DEF_CMD_PER_LUN,
9754 	.max_sectors =		-1U,
9755 	.max_segment_size =	-1U,
9756 	.module =		THIS_MODULE,
9757 	.skip_settle_delay =	1,
9758 	.track_queue_depth =	1,
9759 	.cmd_size = sizeof(union sdebug_priv),
9760 	.init_cmd_priv = sdebug_init_cmd_priv,
9761 	.target_alloc =		sdebug_target_alloc,
9762 	.target_destroy =	sdebug_target_destroy,
9763 };
9764 
9765 static int sdebug_driver_probe(struct device *dev)
9766 {
9767 	int error = 0;
9768 	struct sdebug_host_info *sdbg_host;
9769 	struct Scsi_Host *hpnt;
9770 	int hprot;
9771 
9772 	sdbg_host = dev_to_sdebug_host(dev);
9773 
9774 	hpnt = scsi_host_alloc(&sdebug_driver_template, 0);
9775 	if (NULL == hpnt) {
9776 		pr_err("scsi_host_alloc failed\n");
9777 		error = -ENODEV;
9778 		return error;
9779 	}
9780 	hpnt->can_queue = sdebug_max_queue;
9781 	hpnt->cmd_per_lun = sdebug_max_queue;
9782 	if (!sdebug_clustering)
9783 		hpnt->dma_boundary = PAGE_SIZE - 1;
9784 
9785 	if (submit_queues > nr_cpu_ids) {
9786 		pr_warn("%s: trim submit_queues (was %d) to nr_cpu_ids=%u\n",
9787 			my_name, submit_queues, nr_cpu_ids);
9788 		submit_queues = nr_cpu_ids;
9789 	}
9790 	/*
9791 	 * Decide whether to tell scsi subsystem that we want mq. The
9792 	 * following should give the same answer for each host.
9793 	 */
9794 	hpnt->nr_hw_queues = submit_queues;
9795 	if (sdebug_host_max_queue)
9796 		hpnt->host_tagset = 1;
9797 
9798 	/* poll queues are possible for nr_hw_queues > 1 */
9799 	if (hpnt->nr_hw_queues == 1 || (poll_queues < 1)) {
9800 		pr_warn("%s: trim poll_queues to 0. poll_q/nr_hw = (%d/%d)\n",
9801 			 my_name, poll_queues, hpnt->nr_hw_queues);
9802 		poll_queues = 0;
9803 	}
9804 
9805 	/*
9806 	 * Poll queues don't need interrupts, but we need at least one I/O queue
9807 	 * left over for non-polled I/O.
9808 	 * If condition not met, trim poll_queues to 1 (just for simplicity).
9809 	 */
9810 	if (poll_queues >= submit_queues) {
9811 		if (submit_queues < 3)
9812 			pr_warn("%s: trim poll_queues to 1\n", my_name);
9813 		else
9814 			pr_warn("%s: trim poll_queues to 1. Perhaps try poll_queues=%d\n",
9815 				my_name, submit_queues - 1);
9816 		poll_queues = 1;
9817 	}
9818 	if (poll_queues)
9819 		hpnt->nr_maps = 3;
9820 
9821 	sdbg_host->shost = hpnt;
9822 	if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id))
9823 		hpnt->max_id = sdebug_num_tgts + 1;
9824 	else
9825 		hpnt->max_id = sdebug_num_tgts;
9826 	/* = sdebug_max_luns; */
9827 	hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
9828 
9829 	hprot = 0;
9830 
9831 	switch (sdebug_dif) {
9832 
9833 	case T10_PI_TYPE1_PROTECTION:
9834 		hprot = SHOST_DIF_TYPE1_PROTECTION;
9835 		if (sdebug_dix)
9836 			hprot |= SHOST_DIX_TYPE1_PROTECTION;
9837 		break;
9838 
9839 	case T10_PI_TYPE2_PROTECTION:
9840 		hprot = SHOST_DIF_TYPE2_PROTECTION;
9841 		if (sdebug_dix)
9842 			hprot |= SHOST_DIX_TYPE2_PROTECTION;
9843 		break;
9844 
9845 	case T10_PI_TYPE3_PROTECTION:
9846 		hprot = SHOST_DIF_TYPE3_PROTECTION;
9847 		if (sdebug_dix)
9848 			hprot |= SHOST_DIX_TYPE3_PROTECTION;
9849 		break;
9850 
9851 	default:
9852 		if (sdebug_dix)
9853 			hprot |= SHOST_DIX_TYPE0_PROTECTION;
9854 		break;
9855 	}
9856 
9857 	scsi_host_set_prot(hpnt, hprot);
9858 
9859 	if (have_dif_prot || sdebug_dix)
9860 		pr_info("host protection%s%s%s%s%s%s%s\n",
9861 			(hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
9862 			(hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
9863 			(hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
9864 			(hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
9865 			(hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
9866 			(hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
9867 			(hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
9868 
9869 	if (sdebug_guard == 1)
9870 		scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP);
9871 	else
9872 		scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC);
9873 
9874 	sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts);
9875 	if (sdebug_every_nth)	/* need stats counters for every_nth */
9876 		sdebug_statistics = true;
9877 	error = scsi_add_host(hpnt, &sdbg_host->dev);
9878 	if (error) {
9879 		pr_err("scsi_add_host failed\n");
9880 		error = -ENODEV;
9881 		scsi_host_put(hpnt);
9882 	} else {
9883 		scsi_scan_host(hpnt);
9884 	}
9885 
9886 	return error;
9887 }
9888 
9889 static void sdebug_driver_remove(struct device *dev)
9890 {
9891 	struct sdebug_host_info *sdbg_host;
9892 	struct sdebug_dev_info *sdbg_devinfo, *tmp;
9893 
9894 	sdbg_host = dev_to_sdebug_host(dev);
9895 
9896 	scsi_remove_host(sdbg_host->shost);
9897 
9898 	list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
9899 				 dev_list) {
9900 		list_del(&sdbg_devinfo->dev_list);
9901 		kfree(sdbg_devinfo->zstate);
9902 		kfree(sdbg_devinfo);
9903 	}
9904 
9905 	scsi_host_put(sdbg_host->shost);
9906 }
9907 
9908 static const struct bus_type pseudo_lld_bus = {
9909 	.name = "pseudo",
9910 	.probe = sdebug_driver_probe,
9911 	.remove = sdebug_driver_remove,
9912 	.drv_groups = sdebug_drv_groups,
9913 };
9914