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