xref: /linux/drivers/scsi/scsi_debug.c (revision 4f87e9068bf3aaf45f226261d5efd50bec42c12c)
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 	u8 *arr = NULL, *desc;
5894 	u8 *cmd = scp->cmnd;
5895 	struct sdeb_zone_state *zsp = NULL;
5896 	struct sdeb_store_info *sip = devip2sip(devip, false);
5897 
5898 	if (!sdebug_dev_is_zoned(devip)) {
5899 		mk_sense_invalid_opcode(scp);
5900 		return check_condition_result;
5901 	}
5902 	zs_lba = get_unaligned_be64(cmd + 2);
5903 	alloc_len = get_unaligned_be32(cmd + 10);
5904 	if (alloc_len == 0)
5905 		return 0;	/* not an error */
5906 	rep_opts = cmd[14] & 0x3f;
5907 	partial = cmd[14] & 0x80;
5908 
5909 	if (zs_lba >= sdebug_capacity) {
5910 		mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
5911 		return check_condition_result;
5912 	}
5913 
5914 	rep_max_zones = (alloc_len - 64) >> ilog2(RZONES_DESC_HD);
5915 
5916 	arr = kzalloc(alloc_len, GFP_ATOMIC | __GFP_NOWARN);
5917 	if (!arr) {
5918 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
5919 				INSUFF_RES_ASCQ);
5920 		return check_condition_result;
5921 	}
5922 
5923 	sdeb_meta_read_lock(sip);
5924 
5925 	desc = arr + 64;
5926 	for (lba = zs_lba; lba < sdebug_capacity;
5927 	     lba = zsp->z_start + zsp->z_size) {
5928 		if (WARN_ONCE(zbc_zone(devip, lba) == zsp, "lba = %llu\n", lba))
5929 			break;
5930 		zsp = zbc_zone(devip, lba);
5931 		switch (rep_opts) {
5932 		case 0x00:
5933 			/* All zones */
5934 			break;
5935 		case 0x01:
5936 			/* Empty zones */
5937 			if (zsp->z_cond != ZC1_EMPTY)
5938 				continue;
5939 			break;
5940 		case 0x02:
5941 			/* Implicit open zones */
5942 			if (zsp->z_cond != ZC2_IMPLICIT_OPEN)
5943 				continue;
5944 			break;
5945 		case 0x03:
5946 			/* Explicit open zones */
5947 			if (zsp->z_cond != ZC3_EXPLICIT_OPEN)
5948 				continue;
5949 			break;
5950 		case 0x04:
5951 			/* Closed zones */
5952 			if (zsp->z_cond != ZC4_CLOSED)
5953 				continue;
5954 			break;
5955 		case 0x05:
5956 			/* Full zones */
5957 			if (zsp->z_cond != ZC5_FULL)
5958 				continue;
5959 			break;
5960 		case 0x06:
5961 		case 0x07:
5962 		case 0x10:
5963 			/*
5964 			 * Read-only, offline, reset WP recommended are
5965 			 * not emulated: no zones to report;
5966 			 */
5967 			continue;
5968 		case 0x11:
5969 			/* non-seq-resource set */
5970 			if (!zsp->z_non_seq_resource)
5971 				continue;
5972 			break;
5973 		case 0x3e:
5974 			/* All zones except gap zones. */
5975 			if (zbc_zone_is_gap(zsp))
5976 				continue;
5977 			break;
5978 		case 0x3f:
5979 			/* Not write pointer (conventional) zones */
5980 			if (zbc_zone_is_seq(zsp))
5981 				continue;
5982 			break;
5983 		default:
5984 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
5985 					INVALID_FIELD_IN_CDB, 0);
5986 			ret = check_condition_result;
5987 			goto fini;
5988 		}
5989 
5990 		if (nrz < rep_max_zones) {
5991 			/* Fill zone descriptor */
5992 			desc[0] = zsp->z_type;
5993 			desc[1] = zsp->z_cond << 4;
5994 			if (zsp->z_non_seq_resource)
5995 				desc[1] |= 1 << 1;
5996 			put_unaligned_be64((u64)zsp->z_size, desc + 8);
5997 			put_unaligned_be64((u64)zsp->z_start, desc + 16);
5998 			put_unaligned_be64((u64)zsp->z_wp, desc + 24);
5999 			desc += 64;
6000 		}
6001 
6002 		if (partial && nrz >= rep_max_zones)
6003 			break;
6004 
6005 		nrz++;
6006 	}
6007 
6008 	/* Report header */
6009 	/* Zone list length. */
6010 	put_unaligned_be32(nrz * RZONES_DESC_HD, arr + 0);
6011 	/* Maximum LBA */
6012 	put_unaligned_be64(sdebug_capacity - 1, arr + 8);
6013 	/* Zone starting LBA granularity. */
6014 	if (devip->zcap < devip->zsize)
6015 		put_unaligned_be64(devip->zsize, arr + 16);
6016 
6017 	rep_len = (unsigned long)desc - (unsigned long)arr;
6018 	ret = fill_from_dev_buffer(scp, arr, min_t(u32, alloc_len, rep_len));
6019 
6020 fini:
6021 	sdeb_meta_read_unlock(sip);
6022 	kfree(arr);
6023 	return ret;
6024 }
6025 
6026 static int resp_atomic_write(struct scsi_cmnd *scp,
6027 			     struct sdebug_dev_info *devip)
6028 {
6029 	struct sdeb_store_info *sip;
6030 	u8 *cmd = scp->cmnd;
6031 	u16 boundary, len;
6032 	u64 lba, lba_tmp;
6033 	int ret;
6034 
6035 	if (!scsi_debug_atomic_write()) {
6036 		mk_sense_invalid_opcode(scp);
6037 		return check_condition_result;
6038 	}
6039 
6040 	sip = devip2sip(devip, true);
6041 
6042 	lba = get_unaligned_be64(cmd + 2);
6043 	boundary = get_unaligned_be16(cmd + 10);
6044 	len = get_unaligned_be16(cmd + 12);
6045 
6046 	lba_tmp = lba;
6047 	if (sdebug_atomic_wr_align &&
6048 	    do_div(lba_tmp, sdebug_atomic_wr_align)) {
6049 		/* Does not meet alignment requirement */
6050 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
6051 		return check_condition_result;
6052 	}
6053 
6054 	if (sdebug_atomic_wr_gran && len % sdebug_atomic_wr_gran) {
6055 		/* Does not meet alignment requirement */
6056 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
6057 		return check_condition_result;
6058 	}
6059 
6060 	if (boundary > 0) {
6061 		if (boundary > sdebug_atomic_wr_max_bndry) {
6062 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 12, -1);
6063 			return check_condition_result;
6064 		}
6065 
6066 		if (len > sdebug_atomic_wr_max_length_bndry) {
6067 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 12, -1);
6068 			return check_condition_result;
6069 		}
6070 	} else {
6071 		if (len > sdebug_atomic_wr_max_length) {
6072 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 12, -1);
6073 			return check_condition_result;
6074 		}
6075 	}
6076 
6077 	ret = do_device_access(sip, scp, 0, lba, len, 0, true, true);
6078 	if (unlikely(ret == -1))
6079 		return DID_ERROR << 16;
6080 	if (unlikely(ret != len * sdebug_sector_size))
6081 		return DID_ERROR << 16;
6082 	return 0;
6083 }
6084 
6085 /* Logic transplanted from tcmu-runner, file_zbc.c */
6086 static void zbc_open_all(struct sdebug_dev_info *devip)
6087 {
6088 	struct sdeb_zone_state *zsp = &devip->zstate[0];
6089 	unsigned int i;
6090 
6091 	for (i = 0; i < devip->nr_zones; i++, zsp++) {
6092 		if (zsp->z_cond == ZC4_CLOSED)
6093 			zbc_open_zone(devip, &devip->zstate[i], true);
6094 	}
6095 }
6096 
6097 static int resp_open_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
6098 {
6099 	int res = 0;
6100 	u64 z_id;
6101 	enum sdebug_z_cond zc;
6102 	u8 *cmd = scp->cmnd;
6103 	struct sdeb_zone_state *zsp;
6104 	bool all = cmd[14] & 0x01;
6105 	struct sdeb_store_info *sip = devip2sip(devip, false);
6106 
6107 	if (!sdebug_dev_is_zoned(devip)) {
6108 		mk_sense_invalid_opcode(scp);
6109 		return check_condition_result;
6110 	}
6111 	sdeb_meta_write_lock(sip);
6112 
6113 	if (all) {
6114 		/* Check if all closed zones can be open */
6115 		if (devip->max_open &&
6116 		    devip->nr_exp_open + devip->nr_closed > devip->max_open) {
6117 			mk_sense_buffer(scp, DATA_PROTECT, INSUFF_RES_ASC,
6118 					INSUFF_ZONE_ASCQ);
6119 			res = check_condition_result;
6120 			goto fini;
6121 		}
6122 		/* Open all closed zones */
6123 		zbc_open_all(devip);
6124 		goto fini;
6125 	}
6126 
6127 	/* Open the specified zone */
6128 	z_id = get_unaligned_be64(cmd + 2);
6129 	if (z_id >= sdebug_capacity) {
6130 		mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
6131 		res = check_condition_result;
6132 		goto fini;
6133 	}
6134 
6135 	zsp = zbc_zone(devip, z_id);
6136 	if (z_id != zsp->z_start) {
6137 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
6138 		res = check_condition_result;
6139 		goto fini;
6140 	}
6141 	if (zbc_zone_is_conv(zsp)) {
6142 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
6143 		res = check_condition_result;
6144 		goto fini;
6145 	}
6146 
6147 	zc = zsp->z_cond;
6148 	if (zc == ZC3_EXPLICIT_OPEN || zc == ZC5_FULL)
6149 		goto fini;
6150 
6151 	if (devip->max_open && devip->nr_exp_open >= devip->max_open) {
6152 		mk_sense_buffer(scp, DATA_PROTECT, INSUFF_RES_ASC,
6153 				INSUFF_ZONE_ASCQ);
6154 		res = check_condition_result;
6155 		goto fini;
6156 	}
6157 
6158 	zbc_open_zone(devip, zsp, true);
6159 fini:
6160 	sdeb_meta_write_unlock(sip);
6161 	return res;
6162 }
6163 
6164 static void zbc_close_all(struct sdebug_dev_info *devip)
6165 {
6166 	unsigned int i;
6167 
6168 	for (i = 0; i < devip->nr_zones; i++)
6169 		zbc_close_zone(devip, &devip->zstate[i]);
6170 }
6171 
6172 static int resp_close_zone(struct scsi_cmnd *scp,
6173 			   struct sdebug_dev_info *devip)
6174 {
6175 	int res = 0;
6176 	u64 z_id;
6177 	u8 *cmd = scp->cmnd;
6178 	struct sdeb_zone_state *zsp;
6179 	bool all = cmd[14] & 0x01;
6180 	struct sdeb_store_info *sip = devip2sip(devip, false);
6181 
6182 	if (!sdebug_dev_is_zoned(devip)) {
6183 		mk_sense_invalid_opcode(scp);
6184 		return check_condition_result;
6185 	}
6186 
6187 	sdeb_meta_write_lock(sip);
6188 
6189 	if (all) {
6190 		zbc_close_all(devip);
6191 		goto fini;
6192 	}
6193 
6194 	/* Close specified zone */
6195 	z_id = get_unaligned_be64(cmd + 2);
6196 	if (z_id >= sdebug_capacity) {
6197 		mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
6198 		res = check_condition_result;
6199 		goto fini;
6200 	}
6201 
6202 	zsp = zbc_zone(devip, z_id);
6203 	if (z_id != zsp->z_start) {
6204 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
6205 		res = check_condition_result;
6206 		goto fini;
6207 	}
6208 	if (zbc_zone_is_conv(zsp)) {
6209 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
6210 		res = check_condition_result;
6211 		goto fini;
6212 	}
6213 
6214 	zbc_close_zone(devip, zsp);
6215 fini:
6216 	sdeb_meta_write_unlock(sip);
6217 	return res;
6218 }
6219 
6220 static void zbc_finish_zone(struct sdebug_dev_info *devip,
6221 			    struct sdeb_zone_state *zsp, bool empty)
6222 {
6223 	enum sdebug_z_cond zc = zsp->z_cond;
6224 
6225 	if (zc == ZC4_CLOSED || zc == ZC2_IMPLICIT_OPEN ||
6226 	    zc == ZC3_EXPLICIT_OPEN || (empty && zc == ZC1_EMPTY)) {
6227 		if (zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN)
6228 			zbc_close_zone(devip, zsp);
6229 		if (zsp->z_cond == ZC4_CLOSED)
6230 			devip->nr_closed--;
6231 		zsp->z_wp = zsp->z_start + zsp->z_size;
6232 		zsp->z_cond = ZC5_FULL;
6233 	}
6234 }
6235 
6236 static void zbc_finish_all(struct sdebug_dev_info *devip)
6237 {
6238 	unsigned int i;
6239 
6240 	for (i = 0; i < devip->nr_zones; i++)
6241 		zbc_finish_zone(devip, &devip->zstate[i], false);
6242 }
6243 
6244 static int resp_finish_zone(struct scsi_cmnd *scp,
6245 			    struct sdebug_dev_info *devip)
6246 {
6247 	struct sdeb_zone_state *zsp;
6248 	int res = 0;
6249 	u64 z_id;
6250 	u8 *cmd = scp->cmnd;
6251 	bool all = cmd[14] & 0x01;
6252 	struct sdeb_store_info *sip = devip2sip(devip, false);
6253 
6254 	if (!sdebug_dev_is_zoned(devip)) {
6255 		mk_sense_invalid_opcode(scp);
6256 		return check_condition_result;
6257 	}
6258 
6259 	sdeb_meta_write_lock(sip);
6260 
6261 	if (all) {
6262 		zbc_finish_all(devip);
6263 		goto fini;
6264 	}
6265 
6266 	/* Finish the specified zone */
6267 	z_id = get_unaligned_be64(cmd + 2);
6268 	if (z_id >= sdebug_capacity) {
6269 		mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
6270 		res = check_condition_result;
6271 		goto fini;
6272 	}
6273 
6274 	zsp = zbc_zone(devip, z_id);
6275 	if (z_id != zsp->z_start) {
6276 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
6277 		res = check_condition_result;
6278 		goto fini;
6279 	}
6280 	if (zbc_zone_is_conv(zsp)) {
6281 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
6282 		res = check_condition_result;
6283 		goto fini;
6284 	}
6285 
6286 	zbc_finish_zone(devip, zsp, true);
6287 fini:
6288 	sdeb_meta_write_unlock(sip);
6289 	return res;
6290 }
6291 
6292 static void zbc_rwp_zone(struct sdebug_dev_info *devip,
6293 			 struct sdeb_zone_state *zsp)
6294 {
6295 	enum sdebug_z_cond zc;
6296 	struct sdeb_store_info *sip = devip2sip(devip, false);
6297 
6298 	if (!zbc_zone_is_seq(zsp))
6299 		return;
6300 
6301 	zc = zsp->z_cond;
6302 	if (zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN)
6303 		zbc_close_zone(devip, zsp);
6304 
6305 	if (zsp->z_cond == ZC4_CLOSED)
6306 		devip->nr_closed--;
6307 
6308 	if (zsp->z_wp > zsp->z_start)
6309 		memset(sip->storep + zsp->z_start * sdebug_sector_size, 0,
6310 		       (zsp->z_wp - zsp->z_start) * sdebug_sector_size);
6311 
6312 	zsp->z_non_seq_resource = false;
6313 	zsp->z_wp = zsp->z_start;
6314 	zsp->z_cond = ZC1_EMPTY;
6315 }
6316 
6317 static void zbc_rwp_all(struct sdebug_dev_info *devip)
6318 {
6319 	unsigned int i;
6320 
6321 	for (i = 0; i < devip->nr_zones; i++)
6322 		zbc_rwp_zone(devip, &devip->zstate[i]);
6323 }
6324 
6325 static int resp_rwp_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
6326 {
6327 	struct sdeb_zone_state *zsp;
6328 	int res = 0;
6329 	u64 z_id;
6330 	u8 *cmd = scp->cmnd;
6331 	bool all = cmd[14] & 0x01;
6332 	struct sdeb_store_info *sip = devip2sip(devip, false);
6333 
6334 	if (!sdebug_dev_is_zoned(devip)) {
6335 		mk_sense_invalid_opcode(scp);
6336 		return check_condition_result;
6337 	}
6338 
6339 	sdeb_meta_write_lock(sip);
6340 
6341 	if (all) {
6342 		zbc_rwp_all(devip);
6343 		goto fini;
6344 	}
6345 
6346 	z_id = get_unaligned_be64(cmd + 2);
6347 	if (z_id >= sdebug_capacity) {
6348 		mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
6349 		res = check_condition_result;
6350 		goto fini;
6351 	}
6352 
6353 	zsp = zbc_zone(devip, z_id);
6354 	if (z_id != zsp->z_start) {
6355 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
6356 		res = check_condition_result;
6357 		goto fini;
6358 	}
6359 	if (zbc_zone_is_conv(zsp)) {
6360 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
6361 		res = check_condition_result;
6362 		goto fini;
6363 	}
6364 
6365 	zbc_rwp_zone(devip, zsp);
6366 fini:
6367 	sdeb_meta_write_unlock(sip);
6368 	return res;
6369 }
6370 
6371 static u32 get_tag(struct scsi_cmnd *cmnd)
6372 {
6373 	return blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
6374 }
6375 
6376 /* Queued (deferred) command completions converge here. */
6377 static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp)
6378 {
6379 	struct sdebug_scsi_cmd *sdsc = container_of(sd_dp,
6380 					typeof(*sdsc), sd_dp);
6381 	struct scsi_cmnd *scp = (struct scsi_cmnd *)sdsc - 1;
6382 	unsigned long flags;
6383 	bool aborted;
6384 
6385 	if (sdebug_statistics) {
6386 		atomic_inc(&sdebug_completions);
6387 		if (raw_smp_processor_id() != sd_dp->issuing_cpu)
6388 			atomic_inc(&sdebug_miss_cpus);
6389 	}
6390 
6391 	spin_lock_irqsave(&sdsc->lock, flags);
6392 	aborted = sd_dp->aborted;
6393 	if (unlikely(aborted))
6394 		sd_dp->aborted = false;
6395 
6396 	spin_unlock_irqrestore(&sdsc->lock, flags);
6397 
6398 	if (aborted) {
6399 		pr_info("bypassing scsi_done() due to aborted cmd, kicking-off EH\n");
6400 		blk_abort_request(scsi_cmd_to_rq(scp));
6401 		return;
6402 	}
6403 
6404 	scsi_done(scp); /* callback to mid level */
6405 }
6406 
6407 /* When high resolution timer goes off this function is called. */
6408 static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer)
6409 {
6410 	struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer,
6411 						  hrt);
6412 	sdebug_q_cmd_complete(sd_dp);
6413 	return HRTIMER_NORESTART;
6414 }
6415 
6416 /* When work queue schedules work, it calls this function. */
6417 static void sdebug_q_cmd_wq_complete(struct work_struct *work)
6418 {
6419 	struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer,
6420 						  ew.work);
6421 	sdebug_q_cmd_complete(sd_dp);
6422 }
6423 
6424 static bool got_shared_uuid;
6425 static uuid_t shared_uuid;
6426 
6427 static int sdebug_device_create_zones(struct sdebug_dev_info *devip)
6428 {
6429 	struct sdeb_zone_state *zsp;
6430 	sector_t capacity = get_sdebug_capacity();
6431 	sector_t conv_capacity;
6432 	sector_t zstart = 0;
6433 	unsigned int i;
6434 
6435 	/*
6436 	 * Set the zone size: if sdeb_zbc_zone_size_mb is not set, figure out
6437 	 * a zone size allowing for at least 4 zones on the device. Otherwise,
6438 	 * use the specified zone size checking that at least 2 zones can be
6439 	 * created for the device.
6440 	 */
6441 	if (!sdeb_zbc_zone_size_mb) {
6442 		devip->zsize = (DEF_ZBC_ZONE_SIZE_MB * SZ_1M)
6443 			>> ilog2(sdebug_sector_size);
6444 		while (capacity < devip->zsize << 2 && devip->zsize >= 2)
6445 			devip->zsize >>= 1;
6446 		if (devip->zsize < 2) {
6447 			pr_err("Device capacity too small\n");
6448 			return -EINVAL;
6449 		}
6450 	} else {
6451 		if (!is_power_of_2(sdeb_zbc_zone_size_mb)) {
6452 			pr_err("Zone size is not a power of 2\n");
6453 			return -EINVAL;
6454 		}
6455 		devip->zsize = (sdeb_zbc_zone_size_mb * SZ_1M)
6456 			>> ilog2(sdebug_sector_size);
6457 		if (devip->zsize >= capacity) {
6458 			pr_err("Zone size too large for device capacity\n");
6459 			return -EINVAL;
6460 		}
6461 	}
6462 
6463 	devip->zsize_shift = ilog2(devip->zsize);
6464 	devip->nr_zones = (capacity + devip->zsize - 1) >> devip->zsize_shift;
6465 
6466 	if (sdeb_zbc_zone_cap_mb == 0) {
6467 		devip->zcap = devip->zsize;
6468 	} else {
6469 		devip->zcap = (sdeb_zbc_zone_cap_mb * SZ_1M) >>
6470 			      ilog2(sdebug_sector_size);
6471 		if (devip->zcap > devip->zsize) {
6472 			pr_err("Zone capacity too large\n");
6473 			return -EINVAL;
6474 		}
6475 	}
6476 
6477 	conv_capacity = (sector_t)sdeb_zbc_nr_conv << devip->zsize_shift;
6478 	if (conv_capacity >= capacity) {
6479 		pr_err("Number of conventional zones too large\n");
6480 		return -EINVAL;
6481 	}
6482 	devip->nr_conv_zones = sdeb_zbc_nr_conv;
6483 	devip->nr_seq_zones = ALIGN(capacity - conv_capacity, devip->zsize) >>
6484 			      devip->zsize_shift;
6485 	devip->nr_zones = devip->nr_conv_zones + devip->nr_seq_zones;
6486 
6487 	/* Add gap zones if zone capacity is smaller than the zone size */
6488 	if (devip->zcap < devip->zsize)
6489 		devip->nr_zones += devip->nr_seq_zones;
6490 
6491 	if (devip->zoned) {
6492 		/* zbc_max_open_zones can be 0, meaning "not reported" */
6493 		if (sdeb_zbc_max_open >= devip->nr_zones - 1)
6494 			devip->max_open = (devip->nr_zones - 1) / 2;
6495 		else
6496 			devip->max_open = sdeb_zbc_max_open;
6497 	}
6498 
6499 	devip->zstate = kzalloc_objs(struct sdeb_zone_state, devip->nr_zones);
6500 	if (!devip->zstate)
6501 		return -ENOMEM;
6502 
6503 	for (i = 0; i < devip->nr_zones; i++) {
6504 		zsp = &devip->zstate[i];
6505 
6506 		zsp->z_start = zstart;
6507 
6508 		if (i < devip->nr_conv_zones) {
6509 			zsp->z_type = ZBC_ZTYPE_CNV;
6510 			zsp->z_cond = ZBC_NOT_WRITE_POINTER;
6511 			zsp->z_wp = (sector_t)-1;
6512 			zsp->z_size =
6513 				min_t(u64, devip->zsize, capacity - zstart);
6514 		} else if ((zstart & (devip->zsize - 1)) == 0) {
6515 			if (devip->zoned)
6516 				zsp->z_type = ZBC_ZTYPE_SWR;
6517 			else
6518 				zsp->z_type = ZBC_ZTYPE_SWP;
6519 			zsp->z_cond = ZC1_EMPTY;
6520 			zsp->z_wp = zsp->z_start;
6521 			zsp->z_size =
6522 				min_t(u64, devip->zcap, capacity - zstart);
6523 		} else {
6524 			zsp->z_type = ZBC_ZTYPE_GAP;
6525 			zsp->z_cond = ZBC_NOT_WRITE_POINTER;
6526 			zsp->z_wp = (sector_t)-1;
6527 			zsp->z_size = min_t(u64, devip->zsize - devip->zcap,
6528 					    capacity - zstart);
6529 		}
6530 
6531 		WARN_ON_ONCE((int)zsp->z_size <= 0);
6532 		zstart += zsp->z_size;
6533 	}
6534 
6535 	return 0;
6536 }
6537 
6538 static struct sdebug_dev_info *sdebug_device_create(
6539 			struct sdebug_host_info *sdbg_host, gfp_t flags)
6540 {
6541 	struct sdebug_dev_info *devip;
6542 
6543 	devip = kzalloc_obj(*devip, flags);
6544 	if (devip) {
6545 		if (sdebug_uuid_ctl == 1)
6546 			uuid_gen(&devip->lu_name);
6547 		else if (sdebug_uuid_ctl == 2) {
6548 			if (got_shared_uuid)
6549 				devip->lu_name = shared_uuid;
6550 			else {
6551 				uuid_gen(&shared_uuid);
6552 				got_shared_uuid = true;
6553 				devip->lu_name = shared_uuid;
6554 			}
6555 		}
6556 		devip->sdbg_host = sdbg_host;
6557 		if (sdeb_zbc_in_use) {
6558 			devip->zoned = sdeb_zbc_model == BLK_ZONED_HM;
6559 			if (sdebug_device_create_zones(devip)) {
6560 				kfree(devip);
6561 				return NULL;
6562 			}
6563 		} else {
6564 			devip->zoned = false;
6565 		}
6566 		if (sdebug_ptype == TYPE_TAPE) {
6567 			devip->tape_density = TAPE_DEF_DENSITY;
6568 			devip->tape_blksize = TAPE_DEF_BLKSIZE;
6569 		}
6570 		devip->create_ts = ktime_get_boottime();
6571 		atomic_set(&devip->stopped, (sdeb_tur_ms_to_ready > 0 ? 2 : 0));
6572 		spin_lock_init(&devip->list_lock);
6573 		INIT_LIST_HEAD(&devip->inject_err_list);
6574 		list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list);
6575 	}
6576 	return devip;
6577 }
6578 
6579 static struct sdebug_dev_info *find_build_dev_info(struct scsi_device *sdev)
6580 {
6581 	struct sdebug_host_info *sdbg_host;
6582 	struct sdebug_dev_info *open_devip = NULL;
6583 	struct sdebug_dev_info *devip;
6584 
6585 	sdbg_host = shost_to_sdebug_host(sdev->host);
6586 
6587 	list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
6588 		if ((devip->used) && (devip->channel == sdev->channel) &&
6589 		    (devip->target == sdev->id) &&
6590 		    (devip->lun == sdev->lun))
6591 			return devip;
6592 		else {
6593 			if ((!devip->used) && (!open_devip))
6594 				open_devip = devip;
6595 		}
6596 	}
6597 	if (!open_devip) { /* try and make a new one */
6598 		open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC);
6599 		if (!open_devip) {
6600 			pr_err("out of memory at line %d\n", __LINE__);
6601 			return NULL;
6602 		}
6603 	}
6604 
6605 	open_devip->channel = sdev->channel;
6606 	open_devip->target = sdev->id;
6607 	open_devip->lun = sdev->lun;
6608 	open_devip->sdbg_host = sdbg_host;
6609 	set_bit(SDEBUG_UA_POOCCUR, open_devip->uas_bm);
6610 	open_devip->used = true;
6611 	return open_devip;
6612 }
6613 
6614 static int scsi_debug_sdev_init(struct scsi_device *sdp)
6615 {
6616 	if (sdebug_verbose)
6617 		pr_info("sdev_init <%u %u %u %llu>\n",
6618 		       sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
6619 
6620 	return 0;
6621 }
6622 
6623 static int scsi_debug_sdev_configure(struct scsi_device *sdp,
6624 				     struct queue_limits *lim)
6625 {
6626 	struct sdebug_dev_info *devip =
6627 			(struct sdebug_dev_info *)sdp->hostdata;
6628 	struct dentry *dentry;
6629 
6630 	if (sdebug_verbose)
6631 		pr_info("sdev_configure <%u %u %u %llu>\n",
6632 		       sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
6633 	if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN)
6634 		sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN;
6635 	if (devip == NULL) {
6636 		devip = find_build_dev_info(sdp);
6637 		if (devip == NULL)
6638 			return 1;  /* no resources, will be marked offline */
6639 	}
6640 	if (sdebug_ptype == TYPE_TAPE) {
6641 		if (!devip->tape_blocks[0]) {
6642 			devip->tape_blocks[0] =
6643 				kzalloc_objs(struct tape_block, TAPE_UNITS + 1);
6644 			if (!devip->tape_blocks[0])
6645 				return 1;
6646 		}
6647 		devip->tape_pending_nbr_partitions = -1;
6648 		if (partition_tape(devip, 1, TAPE_UNITS, 0) < 0) {
6649 			kfree(devip->tape_blocks[0]);
6650 			devip->tape_blocks[0] = NULL;
6651 			return 1;
6652 		}
6653 	}
6654 	sdp->hostdata = devip;
6655 	if (sdebug_no_uld)
6656 		sdp->no_uld_attach = 1;
6657 	config_cdb_len(sdp);
6658 
6659 	if (sdebug_allow_restart)
6660 		sdp->allow_restart = 1;
6661 
6662 	devip->debugfs_entry = debugfs_create_dir(dev_name(&sdp->sdev_dev),
6663 				sdebug_debugfs_root);
6664 	if (IS_ERR_OR_NULL(devip->debugfs_entry))
6665 		pr_info("failed to create debugfs directory for device %s\n",
6666 			dev_name(&sdp->sdev_gendev));
6667 
6668 	dentry = debugfs_create_file("error", 0600, devip->debugfs_entry, sdp,
6669 				&sdebug_error_fops);
6670 	if (IS_ERR_OR_NULL(dentry))
6671 		pr_info("failed to create error file for device %s\n",
6672 			dev_name(&sdp->sdev_gendev));
6673 
6674 	return 0;
6675 }
6676 
6677 static void scsi_debug_sdev_destroy(struct scsi_device *sdp)
6678 {
6679 	struct sdebug_dev_info *devip =
6680 		(struct sdebug_dev_info *)sdp->hostdata;
6681 	struct sdebug_err_inject *err;
6682 
6683 	if (sdebug_verbose)
6684 		pr_info("sdev_destroy <%u %u %u %llu>\n",
6685 		       sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
6686 
6687 	if (!devip)
6688 		return;
6689 
6690 	spin_lock(&devip->list_lock);
6691 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
6692 		list_del_rcu(&err->list);
6693 		call_rcu(&err->rcu, sdebug_err_free);
6694 	}
6695 	spin_unlock(&devip->list_lock);
6696 
6697 	debugfs_remove(devip->debugfs_entry);
6698 
6699 	if (sdp->type == TYPE_TAPE) {
6700 		kfree(devip->tape_blocks[0]);
6701 		devip->tape_blocks[0] = NULL;
6702 	}
6703 
6704 	/* make this slot available for re-use */
6705 	devip->used = false;
6706 	sdp->hostdata = NULL;
6707 }
6708 
6709 /* Returns true if cancelled or not running callback. */
6710 static bool scsi_debug_stop_cmnd(struct scsi_cmnd *cmnd)
6711 {
6712 	struct sdebug_scsi_cmd *sdsc = scsi_cmd_priv(cmnd);
6713 	struct sdebug_defer *sd_dp = &sdsc->sd_dp;
6714 	enum sdeb_defer_type defer_t = sd_dp->defer_t;
6715 
6716 	lockdep_assert_held(&sdsc->lock);
6717 
6718 	if (defer_t == SDEB_DEFER_HRT) {
6719 		int res = hrtimer_try_to_cancel(&sd_dp->hrt);
6720 
6721 		switch (res) {
6722 		case -1: /* -1 It's executing the CB */
6723 			return false;
6724 		case 0: /* Not active, it must have already run */
6725 		case 1: /* Was active, we've now cancelled */
6726 		default:
6727 			return true;
6728 		}
6729 	} else if (defer_t == SDEB_DEFER_WQ) {
6730 		/* Cancel if pending */
6731 		if (cancel_work(&sd_dp->ew.work))
6732 			return true;
6733 		/* callback may be running, so return false */
6734 		return false;
6735 	} else if (defer_t == SDEB_DEFER_POLL) {
6736 		return true;
6737 	}
6738 
6739 	return false;
6740 }
6741 
6742 struct sdebug_abort_cmd {
6743 	u32 unique_tag;
6744 };
6745 
6746 enum sdebug_internal_cmd_type {
6747 	SCSI_DEBUG_ABORT_CMD,
6748 };
6749 
6750 struct sdebug_internal_cmd {
6751 	enum sdebug_internal_cmd_type type;
6752 
6753 	union {
6754 		struct sdebug_abort_cmd abort_cmd;
6755 	};
6756 };
6757 
6758 union sdebug_priv {
6759 	struct sdebug_scsi_cmd cmd;
6760 	struct sdebug_internal_cmd internal_cmd;
6761 };
6762 
6763 /*
6764  * Abort SCSI command @cmnd. Only called from scsi_debug_abort(). Although
6765  * it would be possible to call scsi_debug_stop_cmnd() directly, an internal
6766  * command is allocated and submitted to trigger the reserved command
6767  * infrastructure.
6768  */
6769 static bool scsi_debug_abort_cmnd(struct scsi_cmnd *cmnd)
6770 {
6771 	struct Scsi_Host *shost = cmnd->device->host;
6772 	struct request *rq = scsi_cmd_to_rq(cmnd);
6773 	u32 unique_tag = blk_mq_unique_tag(rq);
6774 	struct sdebug_internal_cmd *internal_cmd;
6775 	struct scsi_cmnd *abort_cmd;
6776 	struct request *abort_rq;
6777 	blk_status_t res;
6778 
6779 	abort_cmd = scsi_get_internal_cmd(shost->pseudo_sdev, DMA_NONE,
6780 					  BLK_MQ_REQ_RESERVED);
6781 	if (!abort_cmd)
6782 		return false;
6783 	internal_cmd = scsi_cmd_priv(abort_cmd);
6784 	*internal_cmd = (struct sdebug_internal_cmd) {
6785 		.type = SCSI_DEBUG_ABORT_CMD,
6786 		.abort_cmd = {
6787 			.unique_tag = unique_tag,
6788 		},
6789 	};
6790 	abort_rq = scsi_cmd_to_rq(abort_cmd);
6791 	abort_rq->timeout = secs_to_jiffies(3);
6792 	res = blk_execute_rq(abort_rq, true);
6793 	scsi_put_internal_cmd(abort_cmd);
6794 	return res == BLK_STS_OK;
6795 }
6796 
6797 /*
6798  * All we can do is set the cmnd as internally aborted and wait for it to
6799  * finish. We cannot call scsi_done() as normal completion path may do that.
6800  */
6801 static bool sdebug_stop_cmnd(struct request *rq, void *data)
6802 {
6803 	scsi_debug_abort_cmnd(blk_mq_rq_to_pdu(rq));
6804 
6805 	return true;
6806 }
6807 
6808 /* Deletes (stops) timers or work queues of all queued commands */
6809 static void stop_all_queued(void)
6810 {
6811 	struct sdebug_host_info *sdhp;
6812 
6813 	mutex_lock(&sdebug_host_list_mutex);
6814 	list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
6815 		struct Scsi_Host *shost = sdhp->shost;
6816 
6817 		blk_mq_tagset_busy_iter(&shost->tag_set, sdebug_stop_cmnd, NULL);
6818 	}
6819 	mutex_unlock(&sdebug_host_list_mutex);
6820 }
6821 
6822 static int sdebug_fail_abort(struct scsi_cmnd *cmnd)
6823 {
6824 	struct scsi_device *sdp = cmnd->device;
6825 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
6826 	struct sdebug_err_inject *err;
6827 	unsigned char *cmd = cmnd->cmnd;
6828 	int ret = 0;
6829 
6830 	if (devip == NULL)
6831 		return 0;
6832 
6833 	rcu_read_lock();
6834 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
6835 		if (err->type == ERR_ABORT_CMD_FAILED &&
6836 		    (err->cmd == cmd[0] || err->cmd == 0xff)) {
6837 			ret = !!err->cnt;
6838 			if (err->cnt < 0)
6839 				err->cnt++;
6840 
6841 			rcu_read_unlock();
6842 			return ret;
6843 		}
6844 	}
6845 	rcu_read_unlock();
6846 
6847 	return 0;
6848 }
6849 
6850 static int scsi_debug_abort(struct scsi_cmnd *SCpnt)
6851 {
6852 	bool aborted = scsi_debug_abort_cmnd(SCpnt);
6853 	u8 *cmd = SCpnt->cmnd;
6854 	u8 opcode = cmd[0];
6855 
6856 	++num_aborts;
6857 
6858 	if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
6859 		sdev_printk(KERN_INFO, SCpnt->device,
6860 			    "command%s found\n",
6861 			    aborted ? "" : " not");
6862 
6863 
6864 	if (sdebug_fail_abort(SCpnt)) {
6865 		scmd_printk(KERN_INFO, SCpnt, "fail abort command 0x%x\n",
6866 			    opcode);
6867 		return FAILED;
6868 	}
6869 
6870 	if (aborted == false)
6871 		return FAILED;
6872 
6873 	return SUCCESS;
6874 }
6875 
6876 static bool scsi_debug_stop_all_queued_iter(struct request *rq, void *data)
6877 {
6878 	struct scsi_device *sdp = data;
6879 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
6880 
6881 	if (scmd->device == sdp)
6882 		scsi_debug_abort_cmnd(scmd);
6883 
6884 	return true;
6885 }
6886 
6887 /* Deletes (stops) timers or work queues of all queued commands per sdev */
6888 static void scsi_debug_stop_all_queued(struct scsi_device *sdp)
6889 {
6890 	struct Scsi_Host *shost = sdp->host;
6891 
6892 	blk_mq_tagset_busy_iter(&shost->tag_set,
6893 				scsi_debug_stop_all_queued_iter, sdp);
6894 }
6895 
6896 static int sdebug_fail_lun_reset(struct scsi_cmnd *cmnd)
6897 {
6898 	struct scsi_device *sdp = cmnd->device;
6899 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
6900 	struct sdebug_err_inject *err;
6901 	unsigned char *cmd = cmnd->cmnd;
6902 	int ret = 0;
6903 
6904 	if (devip == NULL)
6905 		return 0;
6906 
6907 	rcu_read_lock();
6908 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
6909 		if (err->type == ERR_LUN_RESET_FAILED &&
6910 		    (err->cmd == cmd[0] || err->cmd == 0xff)) {
6911 			ret = !!err->cnt;
6912 			if (err->cnt < 0)
6913 				err->cnt++;
6914 
6915 			rcu_read_unlock();
6916 			return ret;
6917 		}
6918 	}
6919 	rcu_read_unlock();
6920 
6921 	return 0;
6922 }
6923 
6924 static void scsi_tape_reset_clear(struct sdebug_dev_info *devip)
6925 {
6926 	int i;
6927 
6928 	devip->tape_blksize = TAPE_DEF_BLKSIZE;
6929 	devip->tape_density = TAPE_DEF_DENSITY;
6930 	devip->tape_partition = 0;
6931 	devip->tape_dce = 0;
6932 	for (i = 0; i < TAPE_MAX_PARTITIONS; i++)
6933 		devip->tape_location[i] = 0;
6934 	devip->tape_pending_nbr_partitions = -1;
6935 	/* Don't reset partitioning? */
6936 }
6937 
6938 static int scsi_debug_device_reset(struct scsi_cmnd *SCpnt)
6939 {
6940 	struct scsi_device *sdp = SCpnt->device;
6941 	struct sdebug_dev_info *devip = sdp->hostdata;
6942 	u8 *cmd = SCpnt->cmnd;
6943 	u8 opcode = cmd[0];
6944 
6945 	++num_dev_resets;
6946 
6947 	if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
6948 		sdev_printk(KERN_INFO, sdp, "doing device reset\n");
6949 
6950 	scsi_debug_stop_all_queued(sdp);
6951 	if (devip) {
6952 		set_bit(SDEBUG_UA_POR, devip->uas_bm);
6953 		if (SCpnt->device->type == TYPE_TAPE)
6954 			scsi_tape_reset_clear(devip);
6955 	}
6956 
6957 	if (sdebug_fail_lun_reset(SCpnt)) {
6958 		scmd_printk(KERN_INFO, SCpnt, "fail lun reset 0x%x\n", opcode);
6959 		return FAILED;
6960 	}
6961 
6962 	return SUCCESS;
6963 }
6964 
6965 static int sdebug_fail_target_reset(struct scsi_cmnd *cmnd)
6966 {
6967 	struct scsi_target *starget = scsi_target(cmnd->device);
6968 	struct sdebug_target_info *targetip =
6969 		(struct sdebug_target_info *)starget->hostdata;
6970 
6971 	if (targetip)
6972 		return targetip->reset_fail;
6973 
6974 	return 0;
6975 }
6976 
6977 static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt)
6978 {
6979 	struct scsi_device *sdp = SCpnt->device;
6980 	struct sdebug_host_info *sdbg_host = shost_to_sdebug_host(sdp->host);
6981 	struct sdebug_dev_info *devip;
6982 	u8 *cmd = SCpnt->cmnd;
6983 	u8 opcode = cmd[0];
6984 	int k = 0;
6985 
6986 	++num_target_resets;
6987 	if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
6988 		sdev_printk(KERN_INFO, sdp, "doing target reset\n");
6989 
6990 	list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
6991 		if (devip->target == sdp->id) {
6992 			set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
6993 			if (SCpnt->device->type == TYPE_TAPE)
6994 				scsi_tape_reset_clear(devip);
6995 			++k;
6996 		}
6997 	}
6998 
6999 	if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
7000 		sdev_printk(KERN_INFO, sdp,
7001 			    "%d device(s) found in target\n", k);
7002 
7003 	if (sdebug_fail_target_reset(SCpnt)) {
7004 		scmd_printk(KERN_INFO, SCpnt, "fail target reset 0x%x\n",
7005 			    opcode);
7006 		return FAILED;
7007 	}
7008 
7009 	return SUCCESS;
7010 }
7011 
7012 static int scsi_debug_bus_reset(struct scsi_cmnd *SCpnt)
7013 {
7014 	struct scsi_device *sdp = SCpnt->device;
7015 	struct sdebug_host_info *sdbg_host = shost_to_sdebug_host(sdp->host);
7016 	struct sdebug_dev_info *devip;
7017 	int k = 0;
7018 
7019 	++num_bus_resets;
7020 
7021 	if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
7022 		sdev_printk(KERN_INFO, sdp, "doing bus reset\n");
7023 
7024 	list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
7025 		set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
7026 		if (SCpnt->device->type == TYPE_TAPE)
7027 			scsi_tape_reset_clear(devip);
7028 		++k;
7029 	}
7030 
7031 	if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
7032 		sdev_printk(KERN_INFO, sdp,
7033 			    "%d device(s) found in host\n", k);
7034 	return SUCCESS;
7035 }
7036 
7037 static int scsi_debug_host_reset(struct scsi_cmnd *SCpnt)
7038 {
7039 	struct sdebug_host_info *sdbg_host;
7040 	struct sdebug_dev_info *devip;
7041 	int k = 0;
7042 
7043 	++num_host_resets;
7044 	if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
7045 		sdev_printk(KERN_INFO, SCpnt->device, "doing host reset\n");
7046 	mutex_lock(&sdebug_host_list_mutex);
7047 	list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
7048 		list_for_each_entry(devip, &sdbg_host->dev_info_list,
7049 				    dev_list) {
7050 			set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
7051 			if (SCpnt->device->type == TYPE_TAPE)
7052 				scsi_tape_reset_clear(devip);
7053 			++k;
7054 		}
7055 	}
7056 	mutex_unlock(&sdebug_host_list_mutex);
7057 	stop_all_queued();
7058 	if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
7059 		sdev_printk(KERN_INFO, SCpnt->device,
7060 			"%d device(s) found\n", k);
7061 	return SUCCESS;
7062 }
7063 
7064 static void sdebug_build_parts(unsigned char *ramp, unsigned long store_size)
7065 {
7066 	struct msdos_partition *pp;
7067 	int starts[SDEBUG_MAX_PARTS + 2], max_part_secs;
7068 	int sectors_per_part, num_sectors, k;
7069 	int heads_by_sects, start_sec, end_sec;
7070 
7071 	/* assume partition table already zeroed */
7072 	if ((sdebug_num_parts < 1) || (store_size < 1048576))
7073 		return;
7074 	if (sdebug_num_parts > SDEBUG_MAX_PARTS) {
7075 		sdebug_num_parts = SDEBUG_MAX_PARTS;
7076 		pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS);
7077 	}
7078 	num_sectors = (int)get_sdebug_capacity();
7079 	sectors_per_part = (num_sectors - sdebug_sectors_per)
7080 			   / sdebug_num_parts;
7081 	heads_by_sects = sdebug_heads * sdebug_sectors_per;
7082 	starts[0] = sdebug_sectors_per;
7083 	max_part_secs = sectors_per_part;
7084 	for (k = 1; k < sdebug_num_parts; ++k) {
7085 		starts[k] = ((k * sectors_per_part) / heads_by_sects)
7086 			    * heads_by_sects;
7087 		if (starts[k] - starts[k - 1] < max_part_secs)
7088 			max_part_secs = starts[k] - starts[k - 1];
7089 	}
7090 	starts[sdebug_num_parts] = num_sectors;
7091 	starts[sdebug_num_parts + 1] = 0;
7092 
7093 	ramp[510] = 0x55;	/* magic partition markings */
7094 	ramp[511] = 0xAA;
7095 	pp = (struct msdos_partition *)(ramp + 0x1be);
7096 	for (k = 0; starts[k + 1]; ++k, ++pp) {
7097 		start_sec = starts[k];
7098 		end_sec = starts[k] + max_part_secs - 1;
7099 		pp->boot_ind = 0;
7100 
7101 		pp->cyl = start_sec / heads_by_sects;
7102 		pp->head = (start_sec - (pp->cyl * heads_by_sects))
7103 			   / sdebug_sectors_per;
7104 		pp->sector = (start_sec % sdebug_sectors_per) + 1;
7105 
7106 		pp->end_cyl = end_sec / heads_by_sects;
7107 		pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
7108 			       / sdebug_sectors_per;
7109 		pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
7110 
7111 		pp->start_sect = cpu_to_le32(start_sec);
7112 		pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1);
7113 		pp->sys_ind = 0x83;	/* plain Linux partition */
7114 	}
7115 }
7116 
7117 static void block_unblock_all_queues(bool block)
7118 {
7119 	struct sdebug_host_info *sdhp;
7120 
7121 	lockdep_assert_held(&sdebug_host_list_mutex);
7122 
7123 	list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
7124 		struct Scsi_Host *shost = sdhp->shost;
7125 
7126 		if (block)
7127 			scsi_block_requests(shost);
7128 		else
7129 			scsi_unblock_requests(shost);
7130 	}
7131 }
7132 
7133 /* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1
7134  * commands will be processed normally before triggers occur.
7135  */
7136 static void tweak_cmnd_count(void)
7137 {
7138 	int count, modulo;
7139 
7140 	modulo = abs(sdebug_every_nth);
7141 	if (modulo < 2)
7142 		return;
7143 
7144 	mutex_lock(&sdebug_host_list_mutex);
7145 	block_unblock_all_queues(true);
7146 	count = atomic_read(&sdebug_cmnd_count);
7147 	atomic_set(&sdebug_cmnd_count, (count / modulo) * modulo);
7148 	block_unblock_all_queues(false);
7149 	mutex_unlock(&sdebug_host_list_mutex);
7150 }
7151 
7152 static void clear_queue_stats(void)
7153 {
7154 	atomic_set(&sdebug_cmnd_count, 0);
7155 	atomic_set(&sdebug_completions, 0);
7156 	atomic_set(&sdebug_miss_cpus, 0);
7157 	atomic_set(&sdebug_a_tsf, 0);
7158 }
7159 
7160 static bool inject_on_this_cmd(void)
7161 {
7162 	if (sdebug_every_nth == 0)
7163 		return false;
7164 	return (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) == 0;
7165 }
7166 
7167 #define INCLUSIVE_TIMING_MAX_NS 1000000		/* 1 millisecond */
7168 
7169 /* Complete the processing of the thread that queued a SCSI command to this
7170  * driver. It either completes the command by calling cmnd_done() or
7171  * schedules a hr timer or work queue then returns 0. Returns
7172  * SCSI_MLQUEUE_HOST_BUSY if temporarily out of resources.
7173  */
7174 static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip,
7175 			 int scsi_result,
7176 			 int (*pfp)(struct scsi_cmnd *,
7177 				    struct sdebug_dev_info *),
7178 			 int delta_jiff, int ndelay)
7179 {
7180 	struct request *rq = scsi_cmd_to_rq(cmnd);
7181 	bool polled = rq->cmd_flags & REQ_POLLED;
7182 	struct sdebug_scsi_cmd *sdsc = scsi_cmd_priv(cmnd);
7183 	unsigned long flags;
7184 	u64 ns_from_boot = 0;
7185 	struct scsi_device *sdp;
7186 	struct sdebug_defer *sd_dp;
7187 
7188 	if (unlikely(devip == NULL)) {
7189 		if (scsi_result == 0)
7190 			scsi_result = DID_NO_CONNECT << 16;
7191 		goto respond_in_thread;
7192 	}
7193 	sdp = cmnd->device;
7194 
7195 	if (delta_jiff == 0)
7196 		goto respond_in_thread;
7197 
7198 
7199 	if (unlikely(sdebug_every_nth && (SDEBUG_OPT_RARE_TSF & sdebug_opts) &&
7200 		     (scsi_result == 0))) {
7201 		int num_in_q = scsi_device_busy(sdp);
7202 		int qdepth = cmnd->device->queue_depth;
7203 
7204 		if ((num_in_q == qdepth) &&
7205 		    (atomic_inc_return(&sdebug_a_tsf) >=
7206 		     abs(sdebug_every_nth))) {
7207 			atomic_set(&sdebug_a_tsf, 0);
7208 			scsi_result = device_qfull_result;
7209 
7210 			if (unlikely(SDEBUG_OPT_Q_NOISE & sdebug_opts))
7211 				sdev_printk(KERN_INFO, sdp, "num_in_q=%d +1, <inject> status: TASK SET FULL\n",
7212 					    num_in_q);
7213 		}
7214 	}
7215 
7216 	sd_dp = &sdsc->sd_dp;
7217 
7218 	if (polled || (ndelay > 0 && ndelay < INCLUSIVE_TIMING_MAX_NS))
7219 		ns_from_boot = ktime_get_boottime_ns();
7220 
7221 	/* one of the resp_*() response functions is called here */
7222 	cmnd->result = pfp ? pfp(cmnd, devip) : 0;
7223 	if (cmnd->result & SDEG_RES_IMMED_MASK) {
7224 		cmnd->result &= ~SDEG_RES_IMMED_MASK;
7225 		delta_jiff = ndelay = 0;
7226 	}
7227 	if (cmnd->result == 0 && scsi_result != 0)
7228 		cmnd->result = scsi_result;
7229 	if (cmnd->result == 0 && unlikely(sdebug_opts & SDEBUG_OPT_TRANSPORT_ERR)) {
7230 		if (atomic_read(&sdeb_inject_pending)) {
7231 			mk_sense_buffer(cmnd, ABORTED_COMMAND, TRANSPORT_PROBLEM, ACK_NAK_TO);
7232 			atomic_set(&sdeb_inject_pending, 0);
7233 			cmnd->result = check_condition_result;
7234 		}
7235 	}
7236 
7237 	if (unlikely(sdebug_verbose && cmnd->result))
7238 		sdev_printk(KERN_INFO, sdp, "non-zero result=0x%x\n",
7239 			    cmnd->result);
7240 
7241 	if (delta_jiff > 0 || ndelay > 0) {
7242 		ktime_t kt;
7243 
7244 		if (delta_jiff > 0) {
7245 			u64 ns = jiffies_to_nsecs(delta_jiff);
7246 
7247 			if (sdebug_random && ns < U32_MAX) {
7248 				ns = get_random_u32_below((u32)ns);
7249 			} else if (sdebug_random) {
7250 				ns >>= 12;	/* scale to 4 usec precision */
7251 				if (ns < U32_MAX)	/* over 4 hours max */
7252 					ns = get_random_u32_below((u32)ns);
7253 				ns <<= 12;
7254 			}
7255 			kt = ns_to_ktime(ns);
7256 		} else {	/* ndelay has a 4.2 second max */
7257 			kt = sdebug_random ? get_random_u32_below((u32)ndelay) :
7258 					     (u32)ndelay;
7259 			if (ndelay < INCLUSIVE_TIMING_MAX_NS) {
7260 				u64 d = ktime_get_boottime_ns() - ns_from_boot;
7261 
7262 				if (kt <= d) {	/* elapsed duration >= kt */
7263 					/* call scsi_done() from this thread */
7264 					scsi_done(cmnd);
7265 					return 0;
7266 				}
7267 				/* otherwise reduce kt by elapsed time */
7268 				kt -= d;
7269 			}
7270 		}
7271 		if (sdebug_statistics)
7272 			sd_dp->issuing_cpu = raw_smp_processor_id();
7273 		if (polled) {
7274 			spin_lock_irqsave(&sdsc->lock, flags);
7275 			sd_dp->cmpl_ts = ktime_add(ns_to_ktime(ns_from_boot), kt);
7276 			sd_dp->defer_t = SDEB_DEFER_POLL;
7277 			spin_unlock_irqrestore(&sdsc->lock, flags);
7278 		} else {
7279 			/* schedule the invocation of scsi_done() for a later time */
7280 			spin_lock_irqsave(&sdsc->lock, flags);
7281 			sd_dp->defer_t = SDEB_DEFER_HRT;
7282 			hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL_PINNED);
7283 			/*
7284 			 * The completion handler will try to grab sqcp->lock,
7285 			 * so there is no chance that the completion handler
7286 			 * will call scsi_done() until we release the lock
7287 			 * here (so ok to keep referencing sdsc).
7288 			 */
7289 			spin_unlock_irqrestore(&sdsc->lock, flags);
7290 		}
7291 	} else {	/* jdelay < 0, use work queue */
7292 		if (unlikely((sdebug_opts & SDEBUG_OPT_CMD_ABORT) &&
7293 			     atomic_read(&sdeb_inject_pending))) {
7294 			sd_dp->aborted = true;
7295 			atomic_set(&sdeb_inject_pending, 0);
7296 			sdev_printk(KERN_INFO, sdp, "abort request tag=%#x\n",
7297 				    blk_mq_unique_tag_to_tag(get_tag(cmnd)));
7298 		}
7299 
7300 		if (sdebug_statistics)
7301 			sd_dp->issuing_cpu = raw_smp_processor_id();
7302 		if (polled) {
7303 			spin_lock_irqsave(&sdsc->lock, flags);
7304 			sd_dp->cmpl_ts = ns_to_ktime(ns_from_boot);
7305 			sd_dp->defer_t = SDEB_DEFER_POLL;
7306 			spin_unlock_irqrestore(&sdsc->lock, flags);
7307 		} else {
7308 			spin_lock_irqsave(&sdsc->lock, flags);
7309 			sd_dp->defer_t = SDEB_DEFER_WQ;
7310 			schedule_work(&sd_dp->ew.work);
7311 			spin_unlock_irqrestore(&sdsc->lock, flags);
7312 		}
7313 	}
7314 
7315 	return 0;
7316 
7317 respond_in_thread:	/* call back to mid-layer using invocation thread */
7318 	cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0;
7319 	cmnd->result &= ~SDEG_RES_IMMED_MASK;
7320 	if (cmnd->result == 0 && scsi_result != 0)
7321 		cmnd->result = scsi_result;
7322 	scsi_done(cmnd);
7323 	return 0;
7324 }
7325 
7326 /* Note: The following macros create attribute files in the
7327    /sys/module/scsi_debug/parameters directory. Unfortunately this
7328    driver is unaware of a change and cannot trigger auxiliary actions
7329    as it can when the corresponding attribute in the
7330    /sys/bus/pseudo/drivers/scsi_debug directory is changed.
7331  */
7332 module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR);
7333 module_param_named(ato, sdebug_ato, int, S_IRUGO);
7334 module_param_named(cdb_len, sdebug_cdb_len, int, 0644);
7335 module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR);
7336 module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR);
7337 module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO);
7338 module_param_named(dif, sdebug_dif, int, S_IRUGO);
7339 module_param_named(dix, sdebug_dix, int, S_IRUGO);
7340 module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR);
7341 module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR);
7342 module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR);
7343 module_param_named(guard, sdebug_guard, uint, S_IRUGO);
7344 module_param_named(host_lock, sdebug_host_lock, bool, S_IRUGO | S_IWUSR);
7345 module_param_named(host_max_queue, sdebug_host_max_queue, int, S_IRUGO);
7346 module_param_string(inq_product, sdebug_inq_product_id,
7347 		    sizeof(sdebug_inq_product_id), S_IRUGO | S_IWUSR);
7348 module_param_string(inq_rev, sdebug_inq_product_rev,
7349 		    sizeof(sdebug_inq_product_rev), S_IRUGO | S_IWUSR);
7350 module_param_string(inq_vendor, sdebug_inq_vendor_id,
7351 		    sizeof(sdebug_inq_vendor_id), S_IRUGO | S_IWUSR);
7352 module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO);
7353 module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO);
7354 module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO);
7355 module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO);
7356 module_param_named(atomic_wr, sdebug_atomic_wr, int, S_IRUGO);
7357 module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO);
7358 module_param_named(lun_format, sdebug_lun_am_i, int, S_IRUGO | S_IWUSR);
7359 module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR);
7360 module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR);
7361 module_param_named(medium_error_count, sdebug_medium_error_count, int,
7362 		   S_IRUGO | S_IWUSR);
7363 module_param_named(medium_error_start, sdebug_medium_error_start, int,
7364 		   S_IRUGO | S_IWUSR);
7365 module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR);
7366 module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR);
7367 module_param_named(no_rwlock, sdebug_no_rwlock, bool, S_IRUGO | S_IWUSR);
7368 module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO);
7369 module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO);
7370 module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR);
7371 module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO);
7372 module_param_named(opt_xferlen_exp, sdebug_opt_xferlen_exp, int, S_IRUGO);
7373 module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR);
7374 module_param_named(per_host_store, sdebug_per_host_store, bool,
7375 		   S_IRUGO | S_IWUSR);
7376 module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO);
7377 module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR);
7378 module_param_named(random, sdebug_random, bool, S_IRUGO | S_IWUSR);
7379 module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR);
7380 module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO);
7381 module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO);
7382 module_param_named(statistics, sdebug_statistics, bool, S_IRUGO | S_IWUSR);
7383 module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR);
7384 module_param_named(submit_queues, submit_queues, int, S_IRUGO);
7385 module_param_named(poll_queues, poll_queues, int, S_IRUGO);
7386 module_param_named(tur_ms_to_ready, sdeb_tur_ms_to_ready, int, S_IRUGO);
7387 module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO);
7388 module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO);
7389 module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO);
7390 module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO);
7391 module_param_named(atomic_wr_max_length, sdebug_atomic_wr_max_length, int, S_IRUGO);
7392 module_param_named(atomic_wr_align, sdebug_atomic_wr_align, int, S_IRUGO);
7393 module_param_named(atomic_wr_gran, sdebug_atomic_wr_gran, int, S_IRUGO);
7394 module_param_named(atomic_wr_max_length_bndry, sdebug_atomic_wr_max_length_bndry, int, S_IRUGO);
7395 module_param_named(atomic_wr_max_bndry, sdebug_atomic_wr_max_bndry, int, S_IRUGO);
7396 module_param_named(uuid_ctl, sdebug_uuid_ctl, int, S_IRUGO);
7397 module_param_named(virtual_gb, sdebug_virtual_gb, int, S_IRUGO | S_IWUSR);
7398 module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int,
7399 		   S_IRUGO | S_IWUSR);
7400 module_param_named(wp, sdebug_wp, bool, S_IRUGO | S_IWUSR);
7401 module_param_named(write_same_length, sdebug_write_same_length, int,
7402 		   S_IRUGO | S_IWUSR);
7403 module_param_named(zbc, sdeb_zbc_model_s, charp, S_IRUGO);
7404 module_param_named(zone_cap_mb, sdeb_zbc_zone_cap_mb, int, S_IRUGO);
7405 module_param_named(zone_max_open, sdeb_zbc_max_open, int, S_IRUGO);
7406 module_param_named(zone_nr_conv, sdeb_zbc_nr_conv, int, S_IRUGO);
7407 module_param_named(zone_size_mb, sdeb_zbc_zone_size_mb, int, S_IRUGO);
7408 module_param_named(allow_restart, sdebug_allow_restart, bool, S_IRUGO | S_IWUSR);
7409 
7410 MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
7411 MODULE_DESCRIPTION("SCSI debug adapter driver");
7412 MODULE_LICENSE("GPL");
7413 MODULE_VERSION(SDEBUG_VERSION);
7414 
7415 MODULE_PARM_DESC(add_host, "add n hosts, in sysfs if negative remove host(s) (def=1)");
7416 MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)");
7417 MODULE_PARM_DESC(cdb_len, "suggest CDB lengths to drivers (def=10)");
7418 MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)");
7419 MODULE_PARM_DESC(delay, "response delay (def=1 jiffy); 0:imm, -1,-2:tiny");
7420 MODULE_PARM_DESC(dev_size_mb, "size in MiB of ram shared by devs(def=8)");
7421 MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)");
7422 MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)");
7423 MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
7424 MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)");
7425 MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
7426 MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)");
7427 MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)");
7428 MODULE_PARM_DESC(host_max_queue,
7429 		 "host max # of queued cmds (0 to max(def) [max_queue fixed equal for !0])");
7430 MODULE_PARM_DESC(inq_product, "SCSI INQUIRY product string (def=\"scsi_debug\")");
7431 MODULE_PARM_DESC(inq_rev, "SCSI INQUIRY revision string (def=\""
7432 		 SDEBUG_VERSION "\")");
7433 MODULE_PARM_DESC(inq_vendor, "SCSI INQUIRY vendor string (def=\"Linux\")");
7434 MODULE_PARM_DESC(lbprz,
7435 		 "on read unmapped LBs return 0 when 1 (def), return 0xff when 2");
7436 MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)");
7437 MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)");
7438 MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)");
7439 MODULE_PARM_DESC(atomic_wr, "enable ATOMIC WRITE support, support WRITE ATOMIC(16) (def=0)");
7440 MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)");
7441 MODULE_PARM_DESC(lun_format, "LUN format: 0->peripheral (def); 1 --> flat address method");
7442 MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
7443 MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))");
7444 MODULE_PARM_DESC(medium_error_count, "count of sectors to return follow on MEDIUM error");
7445 MODULE_PARM_DESC(medium_error_start, "starting sector number to return MEDIUM error");
7446 MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)");
7447 MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
7448 MODULE_PARM_DESC(no_rwlock, "don't protect user data reads+writes (def=0)");
7449 MODULE_PARM_DESC(no_uld, "stop ULD (e.g. sd driver) attaching (def=0))");
7450 MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
7451 MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
7452 MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)");
7453 MODULE_PARM_DESC(opt_xferlen_exp, "optimal transfer length granularity exponent (def=physblk_exp)");
7454 MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)");
7455 MODULE_PARM_DESC(per_host_store, "If set, next positive add_host will get new store (def=0)");
7456 MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)");
7457 MODULE_PARM_DESC(poll_queues, "support for iouring iopoll queues (1 to max(submit_queues - 1))");
7458 MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
7459 MODULE_PARM_DESC(random, "If set, uniformly randomize command duration between 0 and delay_in_ns");
7460 MODULE_PARM_DESC(removable, "claim to have removable media (def=0)");
7461 MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=7[SPC-5])");
7462 MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)");
7463 MODULE_PARM_DESC(statistics, "collect statistics on commands, queues (def=0)");
7464 MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)");
7465 MODULE_PARM_DESC(submit_queues, "support for block multi-queue (def=1)");
7466 MODULE_PARM_DESC(tur_ms_to_ready, "TEST UNIT READY millisecs before initial good status (def=0)");
7467 MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)");
7468 MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)");
7469 MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)");
7470 MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)");
7471 MODULE_PARM_DESC(atomic_wr_max_length, "max # of blocks can be atomically written in one cmd (def=8192)");
7472 MODULE_PARM_DESC(atomic_wr_align, "minimum alignment of atomic write in blocks (def=2)");
7473 MODULE_PARM_DESC(atomic_wr_gran, "minimum granularity of atomic write in blocks (def=2)");
7474 MODULE_PARM_DESC(atomic_wr_max_length_bndry, "max # of blocks can be atomically written in one cmd with boundary set (def=8192)");
7475 MODULE_PARM_DESC(atomic_wr_max_bndry, "max # boundaries per atomic write (def=128)");
7476 MODULE_PARM_DESC(uuid_ctl,
7477 		 "1->use uuid for lu name, 0->don't, 2->all use same (def=0)");
7478 MODULE_PARM_DESC(virtual_gb, "virtual gigabyte (GiB) size (def=0 -> use dev_size_mb)");
7479 MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
7480 MODULE_PARM_DESC(wp, "Write Protect (def=0)");
7481 MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)");
7482 MODULE_PARM_DESC(zbc, "'none' [0]; 'aware' [1]; 'managed' [2] (def=0). Can have 'host-' prefix");
7483 MODULE_PARM_DESC(zone_cap_mb, "Zone capacity in MiB (def=zone size)");
7484 MODULE_PARM_DESC(zone_max_open, "Maximum number of open zones; [0] for no limit (def=auto)");
7485 MODULE_PARM_DESC(zone_nr_conv, "Number of conventional zones (def=1)");
7486 MODULE_PARM_DESC(zone_size_mb, "Zone size in MiB (def=auto)");
7487 MODULE_PARM_DESC(allow_restart, "Set scsi_device's allow_restart flag(def=0)");
7488 
7489 #define SDEBUG_INFO_LEN 256
7490 static char sdebug_info[SDEBUG_INFO_LEN];
7491 
7492 static const char *scsi_debug_info(struct Scsi_Host *shp)
7493 {
7494 	int k;
7495 
7496 	k = scnprintf(sdebug_info, SDEBUG_INFO_LEN, "%s: version %s [%s]\n",
7497 		      my_name, SDEBUG_VERSION, sdebug_version_date);
7498 	if (k >= (SDEBUG_INFO_LEN - 1))
7499 		return sdebug_info;
7500 	scnprintf(sdebug_info + k, SDEBUG_INFO_LEN - k,
7501 		  "  dev_size_mb=%d, opts=0x%x, submit_queues=%d, %s=%d",
7502 		  sdebug_dev_size_mb, sdebug_opts, submit_queues,
7503 		  "statistics", (int)sdebug_statistics);
7504 	return sdebug_info;
7505 }
7506 
7507 /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */
7508 static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer,
7509 				 int length)
7510 {
7511 	char arr[16];
7512 	int opts;
7513 	int minLen = length > 15 ? 15 : length;
7514 
7515 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
7516 		return -EACCES;
7517 	memcpy(arr, buffer, minLen);
7518 	arr[minLen] = '\0';
7519 	if (1 != sscanf(arr, "%d", &opts))
7520 		return -EINVAL;
7521 	sdebug_opts = opts;
7522 	sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
7523 	if (sdebug_every_nth != 0)
7524 		tweak_cmnd_count();
7525 	return length;
7526 }
7527 
7528 struct sdebug_submit_queue_data {
7529 	int *first;
7530 	int *last;
7531 	int queue_num;
7532 };
7533 
7534 static bool sdebug_submit_queue_iter(struct request *rq, void *opaque)
7535 {
7536 	struct sdebug_submit_queue_data *data = opaque;
7537 	u32 unique_tag = blk_mq_unique_tag(rq);
7538 	u16 hwq = blk_mq_unique_tag_to_hwq(unique_tag);
7539 	u16 tag = blk_mq_unique_tag_to_tag(unique_tag);
7540 	int queue_num = data->queue_num;
7541 
7542 	if (hwq != queue_num)
7543 		return true;
7544 
7545 	/* Rely on iter'ing in ascending tag order */
7546 	if (*data->first == -1)
7547 		*data->first = *data->last = tag;
7548 	else
7549 		*data->last = tag;
7550 
7551 	return true;
7552 }
7553 
7554 /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the
7555  * same for each scsi_debug host (if more than one). Some of the counters
7556  * output are not atomics so might be inaccurate in a busy system. */
7557 static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host)
7558 {
7559 	struct sdebug_host_info *sdhp;
7560 	int j;
7561 
7562 	seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n",
7563 		   SDEBUG_VERSION, sdebug_version_date);
7564 	seq_printf(m, "num_tgts=%d, %ssize=%d MB, opts=0x%x, every_nth=%d\n",
7565 		   sdebug_num_tgts, "shared (ram) ", sdebug_dev_size_mb,
7566 		   sdebug_opts, sdebug_every_nth);
7567 	seq_printf(m, "delay=%d, ndelay=%d, max_luns=%d, sector_size=%d %s\n",
7568 		   sdebug_jdelay, sdebug_ndelay, sdebug_max_luns,
7569 		   sdebug_sector_size, "bytes");
7570 	seq_printf(m, "cylinders=%d, heads=%d, sectors=%d, command aborts=%d\n",
7571 		   sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
7572 		   num_aborts);
7573 	seq_printf(m, "RESETs: device=%d, target=%d, bus=%d, host=%d\n",
7574 		   num_dev_resets, num_target_resets, num_bus_resets,
7575 		   num_host_resets);
7576 	seq_printf(m, "dix_reads=%d, dix_writes=%d, dif_errors=%d\n",
7577 		   dix_reads, dix_writes, dif_errors);
7578 	seq_printf(m, "usec_in_jiffy=%lu, statistics=%d\n", TICK_NSEC / 1000,
7579 		   sdebug_statistics);
7580 	seq_printf(m, "cmnd_count=%d, completions=%d, %s=%d, a_tsf=%d, mq_polls=%d\n",
7581 		   atomic_read(&sdebug_cmnd_count),
7582 		   atomic_read(&sdebug_completions),
7583 		   "miss_cpus", atomic_read(&sdebug_miss_cpus),
7584 		   atomic_read(&sdebug_a_tsf),
7585 		   atomic_read(&sdeb_mq_poll_count));
7586 
7587 	seq_printf(m, "submit_queues=%d\n", submit_queues);
7588 	for (j = 0; j < submit_queues; ++j) {
7589 		int f = -1, l = -1;
7590 		struct sdebug_submit_queue_data data = {
7591 			.queue_num = j,
7592 			.first = &f,
7593 			.last = &l,
7594 		};
7595 		seq_printf(m, "  queue %d:\n", j);
7596 		blk_mq_tagset_busy_iter(&host->tag_set, sdebug_submit_queue_iter,
7597 					&data);
7598 		if (f >= 0) {
7599 			seq_printf(m, "    BUSY: %s: %d,%d\n",
7600 				   "first,last bits", f, l);
7601 		}
7602 	}
7603 
7604 	seq_printf(m, "this host_no=%d\n", host->host_no);
7605 	if (!xa_empty(per_store_ap)) {
7606 		bool niu;
7607 		int idx;
7608 		unsigned long l_idx;
7609 		struct sdeb_store_info *sip;
7610 
7611 		seq_puts(m, "\nhost list:\n");
7612 		j = 0;
7613 		list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
7614 			idx = sdhp->si_idx;
7615 			seq_printf(m, "  %d: host_no=%d, si_idx=%d\n", j,
7616 				   sdhp->shost->host_no, idx);
7617 			++j;
7618 		}
7619 		seq_printf(m, "\nper_store array [most_recent_idx=%d]:\n",
7620 			   sdeb_most_recent_idx);
7621 		j = 0;
7622 		xa_for_each(per_store_ap, l_idx, sip) {
7623 			niu = xa_get_mark(per_store_ap, l_idx,
7624 					  SDEB_XA_NOT_IN_USE);
7625 			idx = (int)l_idx;
7626 			seq_printf(m, "  %d: idx=%d%s\n", j, idx,
7627 				   (niu ? "  not_in_use" : ""));
7628 			++j;
7629 		}
7630 	}
7631 	return 0;
7632 }
7633 
7634 static ssize_t delay_show(struct device_driver *ddp, char *buf)
7635 {
7636 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay);
7637 }
7638 /* Returns -EBUSY if jdelay is being changed and commands are queued. The unit
7639  * of delay is jiffies.
7640  */
7641 static ssize_t delay_store(struct device_driver *ddp, const char *buf,
7642 			   size_t count)
7643 {
7644 	int jdelay, res;
7645 
7646 	if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) {
7647 		res = count;
7648 		if (sdebug_jdelay != jdelay) {
7649 			struct sdebug_host_info *sdhp;
7650 
7651 			mutex_lock(&sdebug_host_list_mutex);
7652 			block_unblock_all_queues(true);
7653 
7654 			list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
7655 				struct Scsi_Host *shost = sdhp->shost;
7656 
7657 				if (scsi_host_busy(shost)) {
7658 					res = -EBUSY;   /* queued commands */
7659 					break;
7660 				}
7661 			}
7662 			if (res > 0) {
7663 				sdebug_jdelay = jdelay;
7664 				sdebug_ndelay = 0;
7665 			}
7666 			block_unblock_all_queues(false);
7667 			mutex_unlock(&sdebug_host_list_mutex);
7668 		}
7669 		return res;
7670 	}
7671 	return -EINVAL;
7672 }
7673 static DRIVER_ATTR_RW(delay);
7674 
7675 static ssize_t ndelay_show(struct device_driver *ddp, char *buf)
7676 {
7677 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay);
7678 }
7679 /* Returns -EBUSY if ndelay is being changed and commands are queued */
7680 /* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */
7681 static ssize_t ndelay_store(struct device_driver *ddp, const char *buf,
7682 			    size_t count)
7683 {
7684 	int ndelay, res;
7685 
7686 	if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) &&
7687 	    (ndelay >= 0) && (ndelay < (1000 * 1000 * 1000))) {
7688 		res = count;
7689 		if (sdebug_ndelay != ndelay) {
7690 			struct sdebug_host_info *sdhp;
7691 
7692 			mutex_lock(&sdebug_host_list_mutex);
7693 			block_unblock_all_queues(true);
7694 
7695 			list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
7696 				struct Scsi_Host *shost = sdhp->shost;
7697 
7698 				if (scsi_host_busy(shost)) {
7699 					res = -EBUSY;   /* queued commands */
7700 					break;
7701 				}
7702 			}
7703 
7704 			if (res > 0) {
7705 				sdebug_ndelay = ndelay;
7706 				sdebug_jdelay = ndelay  ? JDELAY_OVERRIDDEN
7707 							: DEF_JDELAY;
7708 			}
7709 			block_unblock_all_queues(false);
7710 			mutex_unlock(&sdebug_host_list_mutex);
7711 		}
7712 		return res;
7713 	}
7714 	return -EINVAL;
7715 }
7716 static DRIVER_ATTR_RW(ndelay);
7717 
7718 static ssize_t opts_show(struct device_driver *ddp, char *buf)
7719 {
7720 	return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts);
7721 }
7722 
7723 static ssize_t opts_store(struct device_driver *ddp, const char *buf,
7724 			  size_t count)
7725 {
7726 	int opts;
7727 	char work[20];
7728 
7729 	if (sscanf(buf, "%10s", work) == 1) {
7730 		if (strncasecmp(work, "0x", 2) == 0) {
7731 			if (kstrtoint(work + 2, 16, &opts) == 0)
7732 				goto opts_done;
7733 		} else {
7734 			if (kstrtoint(work, 10, &opts) == 0)
7735 				goto opts_done;
7736 		}
7737 	}
7738 	return -EINVAL;
7739 opts_done:
7740 	sdebug_opts = opts;
7741 	sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
7742 	tweak_cmnd_count();
7743 	return count;
7744 }
7745 static DRIVER_ATTR_RW(opts);
7746 
7747 static ssize_t ptype_show(struct device_driver *ddp, char *buf)
7748 {
7749 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype);
7750 }
7751 static ssize_t ptype_store(struct device_driver *ddp, const char *buf,
7752 			   size_t count)
7753 {
7754 	int n;
7755 
7756 	/* Cannot change from or to TYPE_ZBC with sysfs */
7757 	if (sdebug_ptype == TYPE_ZBC)
7758 		return -EINVAL;
7759 
7760 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
7761 		if (n == TYPE_ZBC)
7762 			return -EINVAL;
7763 		sdebug_ptype = n;
7764 		return count;
7765 	}
7766 	return -EINVAL;
7767 }
7768 static DRIVER_ATTR_RW(ptype);
7769 
7770 static ssize_t dsense_show(struct device_driver *ddp, char *buf)
7771 {
7772 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense);
7773 }
7774 static ssize_t dsense_store(struct device_driver *ddp, const char *buf,
7775 			    size_t count)
7776 {
7777 	int n;
7778 
7779 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
7780 		sdebug_dsense = n;
7781 		return count;
7782 	}
7783 	return -EINVAL;
7784 }
7785 static DRIVER_ATTR_RW(dsense);
7786 
7787 static ssize_t fake_rw_show(struct device_driver *ddp, char *buf)
7788 {
7789 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw);
7790 }
7791 static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf,
7792 			     size_t count)
7793 {
7794 	int n, idx;
7795 
7796 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
7797 		bool want_store = (n == 0);
7798 		struct sdebug_host_info *sdhp;
7799 
7800 		n = (n > 0);
7801 		sdebug_fake_rw = (sdebug_fake_rw > 0);
7802 		if (sdebug_fake_rw == n)
7803 			return count;	/* not transitioning so do nothing */
7804 
7805 		if (want_store) {	/* 1 --> 0 transition, set up store */
7806 			if (sdeb_first_idx < 0) {
7807 				idx = sdebug_add_store();
7808 				if (idx < 0)
7809 					return idx;
7810 			} else {
7811 				idx = sdeb_first_idx;
7812 				xa_clear_mark(per_store_ap, idx,
7813 					      SDEB_XA_NOT_IN_USE);
7814 			}
7815 			/* make all hosts use same store */
7816 			list_for_each_entry(sdhp, &sdebug_host_list,
7817 					    host_list) {
7818 				if (sdhp->si_idx != idx) {
7819 					xa_set_mark(per_store_ap, sdhp->si_idx,
7820 						    SDEB_XA_NOT_IN_USE);
7821 					sdhp->si_idx = idx;
7822 				}
7823 			}
7824 			sdeb_most_recent_idx = idx;
7825 		} else {	/* 0 --> 1 transition is trigger for shrink */
7826 			sdebug_erase_all_stores(true /* apart from first */);
7827 		}
7828 		sdebug_fake_rw = n;
7829 		return count;
7830 	}
7831 	return -EINVAL;
7832 }
7833 static DRIVER_ATTR_RW(fake_rw);
7834 
7835 static ssize_t no_lun_0_show(struct device_driver *ddp, char *buf)
7836 {
7837 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0);
7838 }
7839 static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf,
7840 			      size_t count)
7841 {
7842 	int n;
7843 
7844 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
7845 		sdebug_no_lun_0 = n;
7846 		return count;
7847 	}
7848 	return -EINVAL;
7849 }
7850 static DRIVER_ATTR_RW(no_lun_0);
7851 
7852 static ssize_t num_tgts_show(struct device_driver *ddp, char *buf)
7853 {
7854 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts);
7855 }
7856 static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf,
7857 			      size_t count)
7858 {
7859 	int n;
7860 
7861 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
7862 		sdebug_num_tgts = n;
7863 		sdebug_max_tgts_luns();
7864 		return count;
7865 	}
7866 	return -EINVAL;
7867 }
7868 static DRIVER_ATTR_RW(num_tgts);
7869 
7870 static ssize_t dev_size_mb_show(struct device_driver *ddp, char *buf)
7871 {
7872 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb);
7873 }
7874 static DRIVER_ATTR_RO(dev_size_mb);
7875 
7876 static ssize_t per_host_store_show(struct device_driver *ddp, char *buf)
7877 {
7878 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_per_host_store);
7879 }
7880 
7881 static ssize_t per_host_store_store(struct device_driver *ddp, const char *buf,
7882 				    size_t count)
7883 {
7884 	bool v;
7885 
7886 	if (kstrtobool(buf, &v))
7887 		return -EINVAL;
7888 
7889 	sdebug_per_host_store = v;
7890 	return count;
7891 }
7892 static DRIVER_ATTR_RW(per_host_store);
7893 
7894 static ssize_t num_parts_show(struct device_driver *ddp, char *buf)
7895 {
7896 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts);
7897 }
7898 static DRIVER_ATTR_RO(num_parts);
7899 
7900 static ssize_t every_nth_show(struct device_driver *ddp, char *buf)
7901 {
7902 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth);
7903 }
7904 static ssize_t every_nth_store(struct device_driver *ddp, const char *buf,
7905 			       size_t count)
7906 {
7907 	int nth;
7908 	char work[20];
7909 
7910 	if (sscanf(buf, "%10s", work) == 1) {
7911 		if (strncasecmp(work, "0x", 2) == 0) {
7912 			if (kstrtoint(work + 2, 16, &nth) == 0)
7913 				goto every_nth_done;
7914 		} else {
7915 			if (kstrtoint(work, 10, &nth) == 0)
7916 				goto every_nth_done;
7917 		}
7918 	}
7919 	return -EINVAL;
7920 
7921 every_nth_done:
7922 	sdebug_every_nth = nth;
7923 	if (nth && !sdebug_statistics) {
7924 		pr_info("every_nth needs statistics=1, set it\n");
7925 		sdebug_statistics = true;
7926 	}
7927 	tweak_cmnd_count();
7928 	return count;
7929 }
7930 static DRIVER_ATTR_RW(every_nth);
7931 
7932 static ssize_t lun_format_show(struct device_driver *ddp, char *buf)
7933 {
7934 	return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_lun_am);
7935 }
7936 static ssize_t lun_format_store(struct device_driver *ddp, const char *buf,
7937 				size_t count)
7938 {
7939 	int n;
7940 	bool changed;
7941 
7942 	if (kstrtoint(buf, 0, &n))
7943 		return -EINVAL;
7944 	if (n >= 0) {
7945 		if (n > (int)SAM_LUN_AM_FLAT) {
7946 			pr_warn("only LUN address methods 0 and 1 are supported\n");
7947 			return -EINVAL;
7948 		}
7949 		changed = ((int)sdebug_lun_am != n);
7950 		sdebug_lun_am = n;
7951 		if (changed && sdebug_scsi_level >= 5) {	/* >= SPC-3 */
7952 			struct sdebug_host_info *sdhp;
7953 			struct sdebug_dev_info *dp;
7954 
7955 			mutex_lock(&sdebug_host_list_mutex);
7956 			list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
7957 				list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) {
7958 					set_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm);
7959 				}
7960 			}
7961 			mutex_unlock(&sdebug_host_list_mutex);
7962 		}
7963 		return count;
7964 	}
7965 	return -EINVAL;
7966 }
7967 static DRIVER_ATTR_RW(lun_format);
7968 
7969 static ssize_t max_luns_show(struct device_driver *ddp, char *buf)
7970 {
7971 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns);
7972 }
7973 static ssize_t max_luns_store(struct device_driver *ddp, const char *buf,
7974 			      size_t count)
7975 {
7976 	int n;
7977 	bool changed;
7978 
7979 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
7980 		if (n > 256) {
7981 			pr_warn("max_luns can be no more than 256\n");
7982 			return -EINVAL;
7983 		}
7984 		changed = (sdebug_max_luns != n);
7985 		sdebug_max_luns = n;
7986 		sdebug_max_tgts_luns();
7987 		if (changed && (sdebug_scsi_level >= 5)) {	/* >= SPC-3 */
7988 			struct sdebug_host_info *sdhp;
7989 			struct sdebug_dev_info *dp;
7990 
7991 			mutex_lock(&sdebug_host_list_mutex);
7992 			list_for_each_entry(sdhp, &sdebug_host_list,
7993 					    host_list) {
7994 				list_for_each_entry(dp, &sdhp->dev_info_list,
7995 						    dev_list) {
7996 					set_bit(SDEBUG_UA_LUNS_CHANGED,
7997 						dp->uas_bm);
7998 				}
7999 			}
8000 			mutex_unlock(&sdebug_host_list_mutex);
8001 		}
8002 		return count;
8003 	}
8004 	return -EINVAL;
8005 }
8006 static DRIVER_ATTR_RW(max_luns);
8007 
8008 static ssize_t max_queue_show(struct device_driver *ddp, char *buf)
8009 {
8010 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue);
8011 }
8012 /* N.B. max_queue can be changed while there are queued commands. In flight
8013  * commands beyond the new max_queue will be completed. */
8014 static ssize_t max_queue_store(struct device_driver *ddp, const char *buf,
8015 			       size_t count)
8016 {
8017 	int n;
8018 
8019 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) &&
8020 	    (n <= SDEBUG_CANQUEUE) &&
8021 	    (sdebug_host_max_queue == 0)) {
8022 		mutex_lock(&sdebug_host_list_mutex);
8023 
8024 		/* We may only change sdebug_max_queue when we have no shosts */
8025 		if (list_empty(&sdebug_host_list))
8026 			sdebug_max_queue = n;
8027 		else
8028 			count = -EBUSY;
8029 		mutex_unlock(&sdebug_host_list_mutex);
8030 		return count;
8031 	}
8032 	return -EINVAL;
8033 }
8034 static DRIVER_ATTR_RW(max_queue);
8035 
8036 static ssize_t host_max_queue_show(struct device_driver *ddp, char *buf)
8037 {
8038 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_host_max_queue);
8039 }
8040 
8041 static ssize_t no_rwlock_show(struct device_driver *ddp, char *buf)
8042 {
8043 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_rwlock);
8044 }
8045 
8046 static ssize_t no_rwlock_store(struct device_driver *ddp, const char *buf, size_t count)
8047 {
8048 	bool v;
8049 
8050 	if (kstrtobool(buf, &v))
8051 		return -EINVAL;
8052 
8053 	sdebug_no_rwlock = v;
8054 	return count;
8055 }
8056 static DRIVER_ATTR_RW(no_rwlock);
8057 
8058 /*
8059  * Since this is used for .can_queue, and we get the hc_idx tag from the bitmap
8060  * in range [0, sdebug_host_max_queue), we can't change it.
8061  */
8062 static DRIVER_ATTR_RO(host_max_queue);
8063 
8064 static ssize_t no_uld_show(struct device_driver *ddp, char *buf)
8065 {
8066 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld);
8067 }
8068 static DRIVER_ATTR_RO(no_uld);
8069 
8070 static ssize_t scsi_level_show(struct device_driver *ddp, char *buf)
8071 {
8072 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level);
8073 }
8074 static DRIVER_ATTR_RO(scsi_level);
8075 
8076 static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf)
8077 {
8078 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb);
8079 }
8080 static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf,
8081 				size_t count)
8082 {
8083 	int n;
8084 	bool changed;
8085 
8086 	/* Ignore capacity change for ZBC drives for now */
8087 	if (sdeb_zbc_in_use)
8088 		return -ENOTSUPP;
8089 
8090 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8091 		changed = (sdebug_virtual_gb != n);
8092 		sdebug_virtual_gb = n;
8093 		sdebug_capacity = get_sdebug_capacity();
8094 		if (changed) {
8095 			struct sdebug_host_info *sdhp;
8096 			struct sdebug_dev_info *dp;
8097 
8098 			mutex_lock(&sdebug_host_list_mutex);
8099 			list_for_each_entry(sdhp, &sdebug_host_list,
8100 					    host_list) {
8101 				list_for_each_entry(dp, &sdhp->dev_info_list,
8102 						    dev_list) {
8103 					set_bit(SDEBUG_UA_CAPACITY_CHANGED,
8104 						dp->uas_bm);
8105 				}
8106 			}
8107 			mutex_unlock(&sdebug_host_list_mutex);
8108 		}
8109 		return count;
8110 	}
8111 	return -EINVAL;
8112 }
8113 static DRIVER_ATTR_RW(virtual_gb);
8114 
8115 static ssize_t add_host_show(struct device_driver *ddp, char *buf)
8116 {
8117 	/* absolute number of hosts currently active is what is shown */
8118 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_hosts);
8119 }
8120 
8121 static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
8122 			      size_t count)
8123 {
8124 	bool found;
8125 	unsigned long idx;
8126 	struct sdeb_store_info *sip;
8127 	bool want_phs = (sdebug_fake_rw == 0) && sdebug_per_host_store;
8128 	int delta_hosts;
8129 
8130 	if (sscanf(buf, "%d", &delta_hosts) != 1)
8131 		return -EINVAL;
8132 	if (delta_hosts > 0) {
8133 		do {
8134 			found = false;
8135 			if (want_phs) {
8136 				xa_for_each_marked(per_store_ap, idx, sip,
8137 						   SDEB_XA_NOT_IN_USE) {
8138 					sdeb_most_recent_idx = (int)idx;
8139 					found = true;
8140 					break;
8141 				}
8142 				if (found)	/* re-use case */
8143 					sdebug_add_host_helper((int)idx);
8144 				else
8145 					sdebug_do_add_host(true);
8146 			} else {
8147 				sdebug_do_add_host(false);
8148 			}
8149 		} while (--delta_hosts);
8150 	} else if (delta_hosts < 0) {
8151 		do {
8152 			sdebug_do_remove_host(false);
8153 		} while (++delta_hosts);
8154 	}
8155 	return count;
8156 }
8157 static DRIVER_ATTR_RW(add_host);
8158 
8159 static ssize_t vpd_use_hostno_show(struct device_driver *ddp, char *buf)
8160 {
8161 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno);
8162 }
8163 static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf,
8164 				    size_t count)
8165 {
8166 	int n;
8167 
8168 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8169 		sdebug_vpd_use_hostno = n;
8170 		return count;
8171 	}
8172 	return -EINVAL;
8173 }
8174 static DRIVER_ATTR_RW(vpd_use_hostno);
8175 
8176 static ssize_t statistics_show(struct device_driver *ddp, char *buf)
8177 {
8178 	return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_statistics);
8179 }
8180 static ssize_t statistics_store(struct device_driver *ddp, const char *buf,
8181 				size_t count)
8182 {
8183 	int n;
8184 
8185 	if ((count > 0) && (sscanf(buf, "%d", &n) == 1) && (n >= 0)) {
8186 		if (n > 0)
8187 			sdebug_statistics = true;
8188 		else {
8189 			clear_queue_stats();
8190 			sdebug_statistics = false;
8191 		}
8192 		return count;
8193 	}
8194 	return -EINVAL;
8195 }
8196 static DRIVER_ATTR_RW(statistics);
8197 
8198 static ssize_t sector_size_show(struct device_driver *ddp, char *buf)
8199 {
8200 	return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size);
8201 }
8202 static DRIVER_ATTR_RO(sector_size);
8203 
8204 static ssize_t submit_queues_show(struct device_driver *ddp, char *buf)
8205 {
8206 	return scnprintf(buf, PAGE_SIZE, "%d\n", submit_queues);
8207 }
8208 static DRIVER_ATTR_RO(submit_queues);
8209 
8210 static ssize_t dix_show(struct device_driver *ddp, char *buf)
8211 {
8212 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix);
8213 }
8214 static DRIVER_ATTR_RO(dix);
8215 
8216 static ssize_t dif_show(struct device_driver *ddp, char *buf)
8217 {
8218 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif);
8219 }
8220 static DRIVER_ATTR_RO(dif);
8221 
8222 static ssize_t guard_show(struct device_driver *ddp, char *buf)
8223 {
8224 	return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard);
8225 }
8226 static DRIVER_ATTR_RO(guard);
8227 
8228 static ssize_t ato_show(struct device_driver *ddp, char *buf)
8229 {
8230 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato);
8231 }
8232 static DRIVER_ATTR_RO(ato);
8233 
8234 static ssize_t map_show(struct device_driver *ddp, char *buf)
8235 {
8236 	ssize_t count = 0;
8237 
8238 	if (!scsi_debug_lbp())
8239 		return scnprintf(buf, PAGE_SIZE, "0-%u\n",
8240 				 sdebug_store_sectors);
8241 
8242 	if (sdebug_fake_rw == 0 && !xa_empty(per_store_ap)) {
8243 		struct sdeb_store_info *sip = xa_load(per_store_ap, 0);
8244 
8245 		if (sip)
8246 			count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
8247 					  (int)map_size, sip->map_storep);
8248 	}
8249 	buf[count++] = '\n';
8250 	buf[count] = '\0';
8251 
8252 	return count;
8253 }
8254 static DRIVER_ATTR_RO(map);
8255 
8256 static ssize_t random_show(struct device_driver *ddp, char *buf)
8257 {
8258 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_random);
8259 }
8260 
8261 static ssize_t random_store(struct device_driver *ddp, const char *buf,
8262 			    size_t count)
8263 {
8264 	bool v;
8265 
8266 	if (kstrtobool(buf, &v))
8267 		return -EINVAL;
8268 
8269 	sdebug_random = v;
8270 	return count;
8271 }
8272 static DRIVER_ATTR_RW(random);
8273 
8274 static ssize_t removable_show(struct device_driver *ddp, char *buf)
8275 {
8276 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0);
8277 }
8278 static ssize_t removable_store(struct device_driver *ddp, const char *buf,
8279 			       size_t count)
8280 {
8281 	int n;
8282 
8283 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8284 		sdebug_removable = (n > 0);
8285 		return count;
8286 	}
8287 	return -EINVAL;
8288 }
8289 static DRIVER_ATTR_RW(removable);
8290 
8291 static ssize_t host_lock_show(struct device_driver *ddp, char *buf)
8292 {
8293 	return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock);
8294 }
8295 /* N.B. sdebug_host_lock does nothing, kept for backward compatibility */
8296 static ssize_t host_lock_store(struct device_driver *ddp, const char *buf,
8297 			       size_t count)
8298 {
8299 	int n;
8300 
8301 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8302 		sdebug_host_lock = (n > 0);
8303 		return count;
8304 	}
8305 	return -EINVAL;
8306 }
8307 static DRIVER_ATTR_RW(host_lock);
8308 
8309 static ssize_t strict_show(struct device_driver *ddp, char *buf)
8310 {
8311 	return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict);
8312 }
8313 static ssize_t strict_store(struct device_driver *ddp, const char *buf,
8314 			    size_t count)
8315 {
8316 	int n;
8317 
8318 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8319 		sdebug_strict = (n > 0);
8320 		return count;
8321 	}
8322 	return -EINVAL;
8323 }
8324 static DRIVER_ATTR_RW(strict);
8325 
8326 static ssize_t uuid_ctl_show(struct device_driver *ddp, char *buf)
8327 {
8328 	return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_uuid_ctl);
8329 }
8330 static DRIVER_ATTR_RO(uuid_ctl);
8331 
8332 static ssize_t cdb_len_show(struct device_driver *ddp, char *buf)
8333 {
8334 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_cdb_len);
8335 }
8336 static ssize_t cdb_len_store(struct device_driver *ddp, const char *buf,
8337 			     size_t count)
8338 {
8339 	int ret, n;
8340 
8341 	ret = kstrtoint(buf, 0, &n);
8342 	if (ret)
8343 		return ret;
8344 	sdebug_cdb_len = n;
8345 	all_config_cdb_len();
8346 	return count;
8347 }
8348 static DRIVER_ATTR_RW(cdb_len);
8349 
8350 static const char * const zbc_model_strs_a[] = {
8351 	[BLK_ZONED_NONE] = "none",
8352 	[BLK_ZONED_HA]   = "host-aware",
8353 	[BLK_ZONED_HM]   = "host-managed",
8354 };
8355 
8356 static const char * const zbc_model_strs_b[] = {
8357 	[BLK_ZONED_NONE] = "no",
8358 	[BLK_ZONED_HA]   = "aware",
8359 	[BLK_ZONED_HM]   = "managed",
8360 };
8361 
8362 static const char * const zbc_model_strs_c[] = {
8363 	[BLK_ZONED_NONE] = "0",
8364 	[BLK_ZONED_HA]   = "1",
8365 	[BLK_ZONED_HM]   = "2",
8366 };
8367 
8368 static int sdeb_zbc_model_str(const char *cp)
8369 {
8370 	int res = sysfs_match_string(zbc_model_strs_a, cp);
8371 
8372 	if (res < 0) {
8373 		res = sysfs_match_string(zbc_model_strs_b, cp);
8374 		if (res < 0) {
8375 			res = sysfs_match_string(zbc_model_strs_c, cp);
8376 			if (res < 0)
8377 				return -EINVAL;
8378 		}
8379 	}
8380 	return res;
8381 }
8382 
8383 static ssize_t zbc_show(struct device_driver *ddp, char *buf)
8384 {
8385 	return scnprintf(buf, PAGE_SIZE, "%s\n",
8386 			 zbc_model_strs_a[sdeb_zbc_model]);
8387 }
8388 static DRIVER_ATTR_RO(zbc);
8389 
8390 static ssize_t tur_ms_to_ready_show(struct device_driver *ddp, char *buf)
8391 {
8392 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdeb_tur_ms_to_ready);
8393 }
8394 static DRIVER_ATTR_RO(tur_ms_to_ready);
8395 
8396 static ssize_t group_number_stats_show(struct device_driver *ddp, char *buf)
8397 {
8398 	char *p = buf, *end = buf + PAGE_SIZE;
8399 	int i;
8400 
8401 	for (i = 0; i < ARRAY_SIZE(writes_by_group_number); i++)
8402 		p += scnprintf(p, end - p, "%d %ld\n", i,
8403 			       atomic_long_read(&writes_by_group_number[i]));
8404 
8405 	return p - buf;
8406 }
8407 
8408 static ssize_t group_number_stats_store(struct device_driver *ddp,
8409 					const char *buf, size_t count)
8410 {
8411 	int i;
8412 
8413 	for (i = 0; i < ARRAY_SIZE(writes_by_group_number); i++)
8414 		atomic_long_set(&writes_by_group_number[i], 0);
8415 
8416 	return count;
8417 }
8418 static DRIVER_ATTR_RW(group_number_stats);
8419 
8420 /* Note: The following array creates attribute files in the
8421    /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
8422    files (over those found in the /sys/module/scsi_debug/parameters
8423    directory) is that auxiliary actions can be triggered when an attribute
8424    is changed. For example see: add_host_store() above.
8425  */
8426 
8427 static struct attribute *sdebug_drv_attrs[] = {
8428 	&driver_attr_delay.attr,
8429 	&driver_attr_opts.attr,
8430 	&driver_attr_ptype.attr,
8431 	&driver_attr_dsense.attr,
8432 	&driver_attr_fake_rw.attr,
8433 	&driver_attr_host_max_queue.attr,
8434 	&driver_attr_no_lun_0.attr,
8435 	&driver_attr_num_tgts.attr,
8436 	&driver_attr_dev_size_mb.attr,
8437 	&driver_attr_num_parts.attr,
8438 	&driver_attr_every_nth.attr,
8439 	&driver_attr_lun_format.attr,
8440 	&driver_attr_max_luns.attr,
8441 	&driver_attr_max_queue.attr,
8442 	&driver_attr_no_rwlock.attr,
8443 	&driver_attr_no_uld.attr,
8444 	&driver_attr_scsi_level.attr,
8445 	&driver_attr_virtual_gb.attr,
8446 	&driver_attr_add_host.attr,
8447 	&driver_attr_per_host_store.attr,
8448 	&driver_attr_vpd_use_hostno.attr,
8449 	&driver_attr_sector_size.attr,
8450 	&driver_attr_statistics.attr,
8451 	&driver_attr_submit_queues.attr,
8452 	&driver_attr_dix.attr,
8453 	&driver_attr_dif.attr,
8454 	&driver_attr_guard.attr,
8455 	&driver_attr_ato.attr,
8456 	&driver_attr_map.attr,
8457 	&driver_attr_random.attr,
8458 	&driver_attr_removable.attr,
8459 	&driver_attr_host_lock.attr,
8460 	&driver_attr_ndelay.attr,
8461 	&driver_attr_strict.attr,
8462 	&driver_attr_uuid_ctl.attr,
8463 	&driver_attr_cdb_len.attr,
8464 	&driver_attr_tur_ms_to_ready.attr,
8465 	&driver_attr_zbc.attr,
8466 	&driver_attr_group_number_stats.attr,
8467 	NULL,
8468 };
8469 ATTRIBUTE_GROUPS(sdebug_drv);
8470 
8471 static struct device *pseudo_primary;
8472 
8473 static int __init scsi_debug_init(void)
8474 {
8475 	bool want_store = (sdebug_fake_rw == 0);
8476 	unsigned long sz;
8477 	int k, ret, hosts_to_add;
8478 	int idx = -1;
8479 
8480 	if (sdebug_ndelay >= 1000 * 1000 * 1000) {
8481 		pr_warn("ndelay must be less than 1 second, ignored\n");
8482 		sdebug_ndelay = 0;
8483 	} else if (sdebug_ndelay > 0)
8484 		sdebug_jdelay = JDELAY_OVERRIDDEN;
8485 
8486 	switch (sdebug_sector_size) {
8487 	case  512:
8488 	case 1024:
8489 	case 2048:
8490 	case 4096:
8491 		break;
8492 	default:
8493 		pr_err("invalid sector_size %d\n", sdebug_sector_size);
8494 		return -EINVAL;
8495 	}
8496 
8497 	switch (sdebug_dif) {
8498 	case T10_PI_TYPE0_PROTECTION:
8499 		break;
8500 	case T10_PI_TYPE1_PROTECTION:
8501 	case T10_PI_TYPE2_PROTECTION:
8502 	case T10_PI_TYPE3_PROTECTION:
8503 		have_dif_prot = true;
8504 		break;
8505 
8506 	default:
8507 		pr_err("dif must be 0, 1, 2 or 3\n");
8508 		return -EINVAL;
8509 	}
8510 
8511 	if (sdebug_num_tgts < 0) {
8512 		pr_err("num_tgts must be >= 0\n");
8513 		return -EINVAL;
8514 	}
8515 
8516 	if (sdebug_guard > 1) {
8517 		pr_err("guard must be 0 or 1\n");
8518 		return -EINVAL;
8519 	}
8520 
8521 	if (sdebug_ato > 1) {
8522 		pr_err("ato must be 0 or 1\n");
8523 		return -EINVAL;
8524 	}
8525 
8526 	if (sdebug_physblk_exp > 15) {
8527 		pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp);
8528 		return -EINVAL;
8529 	}
8530 
8531 	sdebug_lun_am = sdebug_lun_am_i;
8532 	if (sdebug_lun_am > SAM_LUN_AM_FLAT) {
8533 		pr_warn("Invalid LUN format %u, using default\n", (int)sdebug_lun_am);
8534 		sdebug_lun_am = SAM_LUN_AM_PERIPHERAL;
8535 	}
8536 
8537 	if (sdebug_max_luns > 256) {
8538 		if (sdebug_max_luns > 16384) {
8539 			pr_warn("max_luns can be no more than 16384, use default\n");
8540 			sdebug_max_luns = DEF_MAX_LUNS;
8541 		}
8542 		sdebug_lun_am = SAM_LUN_AM_FLAT;
8543 	}
8544 
8545 	if (sdebug_lowest_aligned > 0x3fff) {
8546 		pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned);
8547 		return -EINVAL;
8548 	}
8549 
8550 	if (submit_queues < 1) {
8551 		pr_err("submit_queues must be 1 or more\n");
8552 		return -EINVAL;
8553 	}
8554 
8555 	if ((sdebug_max_queue > SDEBUG_CANQUEUE) || (sdebug_max_queue < 1)) {
8556 		pr_err("max_queue must be in range [1, %d]\n", SDEBUG_CANQUEUE);
8557 		return -EINVAL;
8558 	}
8559 
8560 	if ((sdebug_host_max_queue > SDEBUG_CANQUEUE) ||
8561 	    (sdebug_host_max_queue < 0)) {
8562 		pr_err("host_max_queue must be in range [0 %d]\n",
8563 		       SDEBUG_CANQUEUE);
8564 		return -EINVAL;
8565 	}
8566 
8567 	if (sdebug_host_max_queue &&
8568 	    (sdebug_max_queue != sdebug_host_max_queue)) {
8569 		sdebug_max_queue = sdebug_host_max_queue;
8570 		pr_warn("fixing max submit queue depth to host max queue depth, %d\n",
8571 			sdebug_max_queue);
8572 	}
8573 
8574 	/*
8575 	 * check for host managed zoned block device specified with
8576 	 * ptype=0x14 or zbc=XXX.
8577 	 */
8578 	if (sdebug_ptype == TYPE_ZBC) {
8579 		sdeb_zbc_model = BLK_ZONED_HM;
8580 	} else if (sdeb_zbc_model_s && *sdeb_zbc_model_s) {
8581 		k = sdeb_zbc_model_str(sdeb_zbc_model_s);
8582 		if (k < 0)
8583 			return k;
8584 		sdeb_zbc_model = k;
8585 		switch (sdeb_zbc_model) {
8586 		case BLK_ZONED_NONE:
8587 		case BLK_ZONED_HA:
8588 			sdebug_ptype = TYPE_DISK;
8589 			break;
8590 		case BLK_ZONED_HM:
8591 			sdebug_ptype = TYPE_ZBC;
8592 			break;
8593 		default:
8594 			pr_err("Invalid ZBC model\n");
8595 			return -EINVAL;
8596 		}
8597 	}
8598 	if (sdeb_zbc_model != BLK_ZONED_NONE) {
8599 		sdeb_zbc_in_use = true;
8600 		if (sdebug_dev_size_mb == DEF_DEV_SIZE_PRE_INIT)
8601 			sdebug_dev_size_mb = DEF_ZBC_DEV_SIZE_MB;
8602 	}
8603 
8604 	if (sdebug_dev_size_mb == DEF_DEV_SIZE_PRE_INIT)
8605 		sdebug_dev_size_mb = DEF_DEV_SIZE_MB;
8606 	if (sdebug_dev_size_mb < 1)
8607 		sdebug_dev_size_mb = 1;  /* force minimum 1 MB ramdisk */
8608 	sz = (unsigned long)sdebug_dev_size_mb * 1048576;
8609 	sdebug_store_sectors = sz / sdebug_sector_size;
8610 	sdebug_capacity = get_sdebug_capacity();
8611 
8612 	/* play around with geometry, don't waste too much on track 0 */
8613 	sdebug_heads = 8;
8614 	sdebug_sectors_per = 32;
8615 	if (sdebug_dev_size_mb >= 256)
8616 		sdebug_heads = 64;
8617 	else if (sdebug_dev_size_mb >= 16)
8618 		sdebug_heads = 32;
8619 	sdebug_cylinders_per = (unsigned long)sdebug_capacity /
8620 			       (sdebug_sectors_per * sdebug_heads);
8621 	if (sdebug_cylinders_per >= 1024) {
8622 		/* other LLDs do this; implies >= 1GB ram disk ... */
8623 		sdebug_heads = 255;
8624 		sdebug_sectors_per = 63;
8625 		sdebug_cylinders_per = (unsigned long)sdebug_capacity /
8626 			       (sdebug_sectors_per * sdebug_heads);
8627 	}
8628 	if (scsi_debug_lbp()) {
8629 		sdebug_unmap_max_blocks =
8630 			clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU);
8631 
8632 		sdebug_unmap_max_desc =
8633 			clamp(sdebug_unmap_max_desc, 0U, 256U);
8634 
8635 		sdebug_unmap_granularity =
8636 			clamp(sdebug_unmap_granularity, 1U, 0xffffffffU);
8637 
8638 		if (sdebug_unmap_alignment &&
8639 		    sdebug_unmap_granularity <=
8640 		    sdebug_unmap_alignment) {
8641 			pr_err("ERR: unmap_granularity <= unmap_alignment\n");
8642 			return -EINVAL;
8643 		}
8644 	}
8645 
8646 	xa_init_flags(per_store_ap, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ);
8647 	if (want_store) {
8648 		idx = sdebug_add_store();
8649 		if (idx < 0)
8650 			return idx;
8651 	}
8652 
8653 	pseudo_primary = root_device_register("pseudo_0");
8654 	if (IS_ERR(pseudo_primary)) {
8655 		pr_warn("root_device_register() error\n");
8656 		ret = PTR_ERR(pseudo_primary);
8657 		goto free_vm;
8658 	}
8659 	ret = bus_register(&pseudo_lld_bus);
8660 	if (ret < 0) {
8661 		pr_warn("bus_register error: %d\n", ret);
8662 		goto dev_unreg;
8663 	}
8664 	ret = driver_register(&sdebug_driverfs_driver);
8665 	if (ret < 0) {
8666 		pr_warn("driver_register error: %d\n", ret);
8667 		goto bus_unreg;
8668 	}
8669 
8670 	hosts_to_add = sdebug_add_host;
8671 	sdebug_add_host = 0;
8672 
8673 	sdebug_debugfs_root = debugfs_create_dir("scsi_debug", NULL);
8674 	if (IS_ERR_OR_NULL(sdebug_debugfs_root))
8675 		pr_info("failed to create initial debugfs directory\n");
8676 
8677 	for (k = 0; k < hosts_to_add; k++) {
8678 		if (want_store && k == 0) {
8679 			ret = sdebug_add_host_helper(idx);
8680 			if (ret < 0) {
8681 				pr_err("add_host_helper k=%d, error=%d\n",
8682 				       k, -ret);
8683 				break;
8684 			}
8685 		} else {
8686 			ret = sdebug_do_add_host(want_store &&
8687 						 sdebug_per_host_store);
8688 			if (ret < 0) {
8689 				pr_err("add_host k=%d error=%d\n", k, -ret);
8690 				break;
8691 			}
8692 		}
8693 	}
8694 	if (sdebug_verbose)
8695 		pr_info("built %d host(s)\n", sdebug_num_hosts);
8696 
8697 	return 0;
8698 
8699 bus_unreg:
8700 	bus_unregister(&pseudo_lld_bus);
8701 dev_unreg:
8702 	root_device_unregister(pseudo_primary);
8703 free_vm:
8704 	sdebug_erase_store(idx, NULL);
8705 	return ret;
8706 }
8707 
8708 static void __exit scsi_debug_exit(void)
8709 {
8710 	int k = sdebug_num_hosts;
8711 
8712 	for (; k; k--)
8713 		sdebug_do_remove_host(true);
8714 	driver_unregister(&sdebug_driverfs_driver);
8715 	bus_unregister(&pseudo_lld_bus);
8716 	root_device_unregister(pseudo_primary);
8717 
8718 	sdebug_erase_all_stores(false);
8719 	xa_destroy(per_store_ap);
8720 	debugfs_remove(sdebug_debugfs_root);
8721 }
8722 
8723 device_initcall(scsi_debug_init);
8724 module_exit(scsi_debug_exit);
8725 
8726 static void sdebug_release_adapter(struct device *dev)
8727 {
8728 	struct sdebug_host_info *sdbg_host;
8729 
8730 	sdbg_host = dev_to_sdebug_host(dev);
8731 	kfree(sdbg_host);
8732 }
8733 
8734 /* idx must be valid, if sip is NULL then it will be obtained using idx */
8735 static void sdebug_erase_store(int idx, struct sdeb_store_info *sip)
8736 {
8737 	if (idx < 0)
8738 		return;
8739 	if (!sip) {
8740 		if (xa_empty(per_store_ap))
8741 			return;
8742 		sip = xa_load(per_store_ap, idx);
8743 		if (!sip)
8744 			return;
8745 	}
8746 	vfree(sip->map_storep);
8747 	vfree(sip->dif_storep);
8748 	vfree(sip->storep);
8749 	xa_erase(per_store_ap, idx);
8750 	kfree(sip);
8751 }
8752 
8753 /* Assume apart_from_first==false only in shutdown case. */
8754 static void sdebug_erase_all_stores(bool apart_from_first)
8755 {
8756 	unsigned long idx;
8757 	struct sdeb_store_info *sip = NULL;
8758 
8759 	xa_for_each(per_store_ap, idx, sip) {
8760 		if (apart_from_first)
8761 			apart_from_first = false;
8762 		else
8763 			sdebug_erase_store(idx, sip);
8764 	}
8765 	if (apart_from_first)
8766 		sdeb_most_recent_idx = sdeb_first_idx;
8767 }
8768 
8769 /*
8770  * Returns store xarray new element index (idx) if >=0 else negated errno.
8771  * Limit the number of stores to 65536.
8772  */
8773 static int sdebug_add_store(void)
8774 {
8775 	int res;
8776 	u32 n_idx;
8777 	unsigned long iflags;
8778 	unsigned long sz = (unsigned long)sdebug_dev_size_mb * 1048576;
8779 	struct sdeb_store_info *sip = NULL;
8780 	struct xa_limit xal = { .max = 1 << 16, .min = 0 };
8781 
8782 	sip = kzalloc_obj(*sip);
8783 	if (!sip)
8784 		return -ENOMEM;
8785 
8786 	xa_lock_irqsave(per_store_ap, iflags);
8787 	res = __xa_alloc(per_store_ap, &n_idx, sip, xal, GFP_ATOMIC);
8788 	if (unlikely(res < 0)) {
8789 		xa_unlock_irqrestore(per_store_ap, iflags);
8790 		kfree(sip);
8791 		pr_warn("xa_alloc() errno=%d\n", -res);
8792 		return res;
8793 	}
8794 	sdeb_most_recent_idx = n_idx;
8795 	if (sdeb_first_idx < 0)
8796 		sdeb_first_idx = n_idx;
8797 	xa_unlock_irqrestore(per_store_ap, iflags);
8798 
8799 	res = -ENOMEM;
8800 	sip->storep = vzalloc(sz);
8801 	if (!sip->storep) {
8802 		pr_err("user data oom\n");
8803 		goto err;
8804 	}
8805 	if (sdebug_num_parts > 0)
8806 		sdebug_build_parts(sip->storep, sz);
8807 
8808 	/* DIF/DIX: what T10 calls Protection Information (PI) */
8809 	if (sdebug_dix) {
8810 		int dif_size;
8811 
8812 		dif_size = sdebug_store_sectors * sizeof(struct t10_pi_tuple);
8813 		sip->dif_storep = vmalloc(dif_size);
8814 
8815 		pr_info("dif_storep %u bytes @ %p\n", dif_size,
8816 			sip->dif_storep);
8817 
8818 		if (!sip->dif_storep) {
8819 			pr_err("DIX oom\n");
8820 			goto err;
8821 		}
8822 		memset(sip->dif_storep, 0xff, dif_size);
8823 	}
8824 	/* Logical Block Provisioning */
8825 	if (scsi_debug_lbp()) {
8826 		map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1;
8827 		sip->map_storep = vcalloc(BITS_TO_LONGS(map_size),
8828 					  sizeof(long));
8829 
8830 		pr_info("%lu provisioning blocks\n", map_size);
8831 
8832 		if (!sip->map_storep) {
8833 			pr_err("LBP map oom\n");
8834 			goto err;
8835 		}
8836 
8837 		/* Map first 1KB for partition table */
8838 		if (sdebug_num_parts)
8839 			map_region(sip, 0, 2);
8840 	}
8841 
8842 	rwlock_init(&sip->macc_data_lck);
8843 	rwlock_init(&sip->macc_meta_lck);
8844 	rwlock_init(&sip->macc_sector_lck);
8845 	return (int)n_idx;
8846 err:
8847 	sdebug_erase_store((int)n_idx, sip);
8848 	pr_warn("failed, errno=%d\n", -res);
8849 	return res;
8850 }
8851 
8852 static int sdebug_add_host_helper(int per_host_idx)
8853 {
8854 	int k, devs_per_host, idx;
8855 	int error = -ENOMEM;
8856 	struct sdebug_host_info *sdbg_host;
8857 	struct sdebug_dev_info *sdbg_devinfo, *tmp;
8858 
8859 	sdbg_host = kzalloc_obj(*sdbg_host);
8860 	if (!sdbg_host)
8861 		return -ENOMEM;
8862 	idx = (per_host_idx < 0) ? sdeb_first_idx : per_host_idx;
8863 	if (xa_get_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE))
8864 		xa_clear_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE);
8865 	sdbg_host->si_idx = idx;
8866 
8867 	INIT_LIST_HEAD(&sdbg_host->dev_info_list);
8868 
8869 	devs_per_host = sdebug_num_tgts * sdebug_max_luns;
8870 	for (k = 0; k < devs_per_host; k++) {
8871 		sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL);
8872 		if (!sdbg_devinfo)
8873 			goto clean;
8874 	}
8875 
8876 	mutex_lock(&sdebug_host_list_mutex);
8877 	list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
8878 	mutex_unlock(&sdebug_host_list_mutex);
8879 
8880 	sdbg_host->dev.bus = &pseudo_lld_bus;
8881 	sdbg_host->dev.parent = pseudo_primary;
8882 	sdbg_host->dev.release = &sdebug_release_adapter;
8883 	dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_num_hosts);
8884 
8885 	error = device_register(&sdbg_host->dev);
8886 	if (error) {
8887 		mutex_lock(&sdebug_host_list_mutex);
8888 		list_del(&sdbg_host->host_list);
8889 		mutex_unlock(&sdebug_host_list_mutex);
8890 		goto clean;
8891 	}
8892 
8893 	++sdebug_num_hosts;
8894 	return 0;
8895 
8896 clean:
8897 	list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
8898 				 dev_list) {
8899 		list_del(&sdbg_devinfo->dev_list);
8900 		kfree(sdbg_devinfo->zstate);
8901 		kfree(sdbg_devinfo);
8902 	}
8903 	if (sdbg_host->dev.release)
8904 		put_device(&sdbg_host->dev);
8905 	else
8906 		kfree(sdbg_host);
8907 	pr_warn("failed, errno=%d\n", -error);
8908 	return error;
8909 }
8910 
8911 static int sdebug_do_add_host(bool mk_new_store)
8912 {
8913 	int ph_idx = sdeb_most_recent_idx;
8914 
8915 	if (mk_new_store) {
8916 		ph_idx = sdebug_add_store();
8917 		if (ph_idx < 0)
8918 			return ph_idx;
8919 	}
8920 	return sdebug_add_host_helper(ph_idx);
8921 }
8922 
8923 static void sdebug_do_remove_host(bool the_end)
8924 {
8925 	int idx = -1;
8926 	struct sdebug_host_info *sdbg_host = NULL;
8927 	struct sdebug_host_info *sdbg_host2;
8928 
8929 	mutex_lock(&sdebug_host_list_mutex);
8930 	if (!list_empty(&sdebug_host_list)) {
8931 		sdbg_host = list_entry(sdebug_host_list.prev,
8932 				       struct sdebug_host_info, host_list);
8933 		idx = sdbg_host->si_idx;
8934 	}
8935 	if (!the_end && idx >= 0) {
8936 		bool unique = true;
8937 
8938 		list_for_each_entry(sdbg_host2, &sdebug_host_list, host_list) {
8939 			if (sdbg_host2 == sdbg_host)
8940 				continue;
8941 			if (idx == sdbg_host2->si_idx) {
8942 				unique = false;
8943 				break;
8944 			}
8945 		}
8946 		if (unique) {
8947 			xa_set_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE);
8948 			if (idx == sdeb_most_recent_idx)
8949 				--sdeb_most_recent_idx;
8950 		}
8951 	}
8952 	if (sdbg_host)
8953 		list_del(&sdbg_host->host_list);
8954 	mutex_unlock(&sdebug_host_list_mutex);
8955 
8956 	if (!sdbg_host)
8957 		return;
8958 
8959 	device_unregister(&sdbg_host->dev);
8960 	--sdebug_num_hosts;
8961 }
8962 
8963 static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth)
8964 {
8965 	struct sdebug_dev_info *devip = sdev->hostdata;
8966 
8967 	if (!devip)
8968 		return	-ENODEV;
8969 
8970 	mutex_lock(&sdebug_host_list_mutex);
8971 	block_unblock_all_queues(true);
8972 
8973 	if (qdepth > SDEBUG_CANQUEUE) {
8974 		qdepth = SDEBUG_CANQUEUE;
8975 		pr_warn("requested qdepth [%d] exceeds canqueue [%d], trim\n",
8976 			qdepth, SDEBUG_CANQUEUE);
8977 	}
8978 	if (qdepth < 1)
8979 		qdepth = 1;
8980 	if (qdepth != sdev->queue_depth)
8981 		scsi_change_queue_depth(sdev, qdepth);
8982 
8983 	block_unblock_all_queues(false);
8984 	mutex_unlock(&sdebug_host_list_mutex);
8985 
8986 	if (SDEBUG_OPT_Q_NOISE & sdebug_opts)
8987 		sdev_printk(KERN_INFO, sdev, "qdepth=%d\n", qdepth);
8988 
8989 	return sdev->queue_depth;
8990 }
8991 
8992 static bool fake_timeout(struct scsi_cmnd *scp)
8993 {
8994 	if (0 == (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth))) {
8995 		if (sdebug_every_nth < -1)
8996 			sdebug_every_nth = -1;
8997 		if (SDEBUG_OPT_TIMEOUT & sdebug_opts)
8998 			return true; /* ignore command causing timeout */
8999 		else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts &&
9000 			 scsi_medium_access_command(scp))
9001 			return true; /* time out reads and writes */
9002 	}
9003 	return false;
9004 }
9005 
9006 /* Response to TUR or media access command when device stopped */
9007 static int resp_not_ready(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
9008 {
9009 	int stopped_state;
9010 	u64 diff_ns = 0;
9011 	ktime_t now_ts = ktime_get_boottime();
9012 	struct scsi_device *sdp = scp->device;
9013 
9014 	stopped_state = atomic_read(&devip->stopped);
9015 	if (stopped_state == 2) {
9016 		if (ktime_to_ns(now_ts) > ktime_to_ns(devip->create_ts)) {
9017 			diff_ns = ktime_to_ns(ktime_sub(now_ts, devip->create_ts));
9018 			if (diff_ns >= ((u64)sdeb_tur_ms_to_ready * 1000000)) {
9019 				/* tur_ms_to_ready timer extinguished */
9020 				atomic_set(&devip->stopped, 0);
9021 				return 0;
9022 			}
9023 		}
9024 		mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x1);
9025 		if (sdebug_verbose)
9026 			sdev_printk(KERN_INFO, sdp,
9027 				    "%s: Not ready: in process of becoming ready\n", my_name);
9028 		if (scp->cmnd[0] == TEST_UNIT_READY) {
9029 			u64 tur_nanosecs_to_ready = (u64)sdeb_tur_ms_to_ready * 1000000;
9030 
9031 			if (diff_ns <= tur_nanosecs_to_ready)
9032 				diff_ns = tur_nanosecs_to_ready - diff_ns;
9033 			else
9034 				diff_ns = tur_nanosecs_to_ready;
9035 			/* As per 20-061r2 approved for spc6 by T10 on 20200716 */
9036 			do_div(diff_ns, 1000000);	/* diff_ns becomes milliseconds */
9037 			scsi_set_sense_information(scp->sense_buffer, SCSI_SENSE_BUFFERSIZE,
9038 						   diff_ns);
9039 			return check_condition_result;
9040 		}
9041 	}
9042 	mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2);
9043 	if (sdebug_verbose)
9044 		sdev_printk(KERN_INFO, sdp, "%s: Not ready: initializing command required\n",
9045 			    my_name);
9046 	return check_condition_result;
9047 }
9048 
9049 static void sdebug_map_queues(struct Scsi_Host *shost)
9050 {
9051 	int i, qoff;
9052 
9053 	if (shost->nr_hw_queues == 1)
9054 		return;
9055 
9056 	for (i = 0, qoff = 0; i < HCTX_MAX_TYPES; i++) {
9057 		struct blk_mq_queue_map *map = &shost->tag_set.map[i];
9058 
9059 		map->nr_queues  = 0;
9060 
9061 		if (i == HCTX_TYPE_DEFAULT)
9062 			map->nr_queues = submit_queues - poll_queues;
9063 		else if (i == HCTX_TYPE_POLL)
9064 			map->nr_queues = poll_queues;
9065 
9066 		if (!map->nr_queues) {
9067 			BUG_ON(i == HCTX_TYPE_DEFAULT);
9068 			continue;
9069 		}
9070 
9071 		map->queue_offset = qoff;
9072 		blk_mq_map_queues(map);
9073 
9074 		qoff += map->nr_queues;
9075 	}
9076 }
9077 
9078 struct sdebug_blk_mq_poll_data {
9079 	unsigned int queue_num;
9080 	int *num_entries;
9081 };
9082 
9083 /*
9084  * We don't handle aborted commands here, but it does not seem possible to have
9085  * aborted polled commands from schedule_resp()
9086  */
9087 static bool sdebug_blk_mq_poll_iter(struct request *rq, void *opaque)
9088 {
9089 	struct sdebug_blk_mq_poll_data *data = opaque;
9090 	struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
9091 	struct sdebug_scsi_cmd *sdsc = scsi_cmd_priv(cmd);
9092 	struct sdebug_defer *sd_dp;
9093 	u32 unique_tag = blk_mq_unique_tag(rq);
9094 	u16 hwq = blk_mq_unique_tag_to_hwq(unique_tag);
9095 	unsigned long flags;
9096 	int queue_num = data->queue_num;
9097 	ktime_t time;
9098 
9099 	/* We're only interested in one queue for this iteration */
9100 	if (hwq != queue_num)
9101 		return true;
9102 
9103 	/* Subsequent checks would fail if this failed, but check anyway */
9104 	if (!test_bit(SCMD_STATE_INFLIGHT, &cmd->state))
9105 		return true;
9106 
9107 	time = ktime_get_boottime();
9108 
9109 	spin_lock_irqsave(&sdsc->lock, flags);
9110 	sd_dp = &sdsc->sd_dp;
9111 	if (sd_dp->defer_t != SDEB_DEFER_POLL) {
9112 		spin_unlock_irqrestore(&sdsc->lock, flags);
9113 		return true;
9114 	}
9115 
9116 	if (time < sd_dp->cmpl_ts) {
9117 		spin_unlock_irqrestore(&sdsc->lock, flags);
9118 		return true;
9119 	}
9120 	spin_unlock_irqrestore(&sdsc->lock, flags);
9121 
9122 	if (sdebug_statistics) {
9123 		atomic_inc(&sdebug_completions);
9124 		if (raw_smp_processor_id() != sd_dp->issuing_cpu)
9125 			atomic_inc(&sdebug_miss_cpus);
9126 	}
9127 
9128 	scsi_done(cmd); /* callback to mid level */
9129 	(*data->num_entries)++;
9130 	return true;
9131 }
9132 
9133 static int sdebug_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
9134 {
9135 	int num_entries = 0;
9136 	struct sdebug_blk_mq_poll_data data = {
9137 		.queue_num = queue_num,
9138 		.num_entries = &num_entries,
9139 	};
9140 
9141 	blk_mq_tagset_busy_iter(&shost->tag_set, sdebug_blk_mq_poll_iter,
9142 				&data);
9143 
9144 	if (num_entries > 0)
9145 		atomic_add(num_entries, &sdeb_mq_poll_count);
9146 	return num_entries;
9147 }
9148 
9149 static int sdebug_timeout_cmd(struct scsi_cmnd *cmnd)
9150 {
9151 	struct scsi_device *sdp = cmnd->device;
9152 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
9153 	struct sdebug_err_inject *err;
9154 	unsigned char *cmd = cmnd->cmnd;
9155 	int ret = 0;
9156 
9157 	if (devip == NULL)
9158 		return 0;
9159 
9160 	rcu_read_lock();
9161 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
9162 		if (err->type == ERR_TMOUT_CMD &&
9163 		    (err->cmd == cmd[0] || err->cmd == 0xff)) {
9164 			ret = !!err->cnt;
9165 			if (err->cnt < 0)
9166 				err->cnt++;
9167 
9168 			rcu_read_unlock();
9169 			return ret;
9170 		}
9171 	}
9172 	rcu_read_unlock();
9173 
9174 	return 0;
9175 }
9176 
9177 static int sdebug_fail_queue_cmd(struct scsi_cmnd *cmnd)
9178 {
9179 	struct scsi_device *sdp = cmnd->device;
9180 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
9181 	struct sdebug_err_inject *err;
9182 	unsigned char *cmd = cmnd->cmnd;
9183 	int ret = 0;
9184 
9185 	if (devip == NULL)
9186 		return 0;
9187 
9188 	rcu_read_lock();
9189 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
9190 		if (err->type == ERR_FAIL_QUEUE_CMD &&
9191 		    (err->cmd == cmd[0] || err->cmd == 0xff)) {
9192 			ret = err->cnt ? err->queuecmd_ret : 0;
9193 			if (err->cnt < 0)
9194 				err->cnt++;
9195 
9196 			rcu_read_unlock();
9197 			return ret;
9198 		}
9199 	}
9200 	rcu_read_unlock();
9201 
9202 	return 0;
9203 }
9204 
9205 static int sdebug_fail_cmd(struct scsi_cmnd *cmnd, int *retval,
9206 			   struct sdebug_err_inject *info)
9207 {
9208 	struct scsi_device *sdp = cmnd->device;
9209 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
9210 	struct sdebug_err_inject *err;
9211 	unsigned char *cmd = cmnd->cmnd;
9212 	int ret = 0;
9213 	int result;
9214 
9215 	if (devip == NULL)
9216 		return 0;
9217 
9218 	rcu_read_lock();
9219 	list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
9220 		if (err->type == ERR_FAIL_CMD &&
9221 		    (err->cmd == cmd[0] || err->cmd == 0xff)) {
9222 			if (!err->cnt) {
9223 				rcu_read_unlock();
9224 				return 0;
9225 			}
9226 
9227 			ret = !!err->cnt;
9228 			rcu_read_unlock();
9229 			goto out_handle;
9230 		}
9231 	}
9232 	rcu_read_unlock();
9233 
9234 	return 0;
9235 
9236 out_handle:
9237 	if (err->cnt < 0)
9238 		err->cnt++;
9239 	mk_sense_buffer(cmnd, err->sense_key, err->asc, err->asq);
9240 	result = err->status_byte | err->host_byte << 16 | err->driver_byte << 24;
9241 	*info = *err;
9242 	*retval = schedule_resp(cmnd, devip, result, NULL, 0, 0);
9243 
9244 	return ret;
9245 }
9246 
9247 /* Process @scp, a request to abort a SCSI command by tag. */
9248 static void scsi_debug_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *scp)
9249 {
9250 	struct sdebug_internal_cmd *internal_cmd = scsi_cmd_priv(scp);
9251 	struct sdebug_abort_cmd *abort_cmd = &internal_cmd->abort_cmd;
9252 	const u32 unique_tag = abort_cmd->unique_tag;
9253 	struct scsi_cmnd *to_be_aborted_scmd =
9254 		scsi_host_find_tag(shost, unique_tag);
9255 	struct sdebug_scsi_cmd *to_be_aborted_sdsc =
9256 		scsi_cmd_priv(to_be_aborted_scmd);
9257 	bool res = false;
9258 
9259 	if (!to_be_aborted_scmd) {
9260 		pr_err("command with tag %#x not found\n", unique_tag);
9261 		return;
9262 	}
9263 
9264 	scoped_guard(spinlock_irqsave, &to_be_aborted_sdsc->lock)
9265 		res = scsi_debug_stop_cmnd(to_be_aborted_scmd);
9266 
9267 	if (res)
9268 		pr_info("aborted command with tag %#x\n", unique_tag);
9269 	else
9270 		pr_err("failed to abort command with tag %#x\n", unique_tag);
9271 
9272 	set_host_byte(scp, res ? DID_OK : DID_ERROR);
9273 }
9274 
9275 static enum scsi_qc_status
9276 scsi_debug_process_reserved_command(struct Scsi_Host *shost,
9277 				    struct scsi_cmnd *scp)
9278 {
9279 	struct sdebug_internal_cmd *internal_cmd = scsi_cmd_priv(scp);
9280 
9281 	switch (internal_cmd->type) {
9282 	case SCSI_DEBUG_ABORT_CMD:
9283 		scsi_debug_abort_cmd(shost, scp);
9284 		break;
9285 	default:
9286 		WARN_ON_ONCE(true);
9287 		set_host_byte(scp, DID_ERROR);
9288 		break;
9289 	}
9290 
9291 	scsi_done(scp);
9292 	return 0;
9293 }
9294 
9295 static enum scsi_qc_status scsi_debug_queuecommand(struct Scsi_Host *shost,
9296 						   struct scsi_cmnd *scp)
9297 {
9298 	u8 sdeb_i;
9299 	struct scsi_device *sdp = scp->device;
9300 	const struct opcode_info_t *oip;
9301 	const struct opcode_info_t *r_oip;
9302 	struct sdebug_dev_info *devip;
9303 	u8 *cmd = scp->cmnd;
9304 	int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
9305 	int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *) = NULL;
9306 	int k, na;
9307 	int errsts = 0;
9308 	u64 lun_index = sdp->lun & 0x3FFF;
9309 	u32 flags;
9310 	u16 sa;
9311 	u8 opcode = cmd[0];
9312 	u32 devsel = sdebug_get_devsel(scp->device);
9313 	bool has_wlun_rl;
9314 	bool inject_now;
9315 	int ret = 0;
9316 	struct sdebug_err_inject err;
9317 
9318 	scsi_set_resid(scp, 0);
9319 	if (sdebug_statistics) {
9320 		atomic_inc(&sdebug_cmnd_count);
9321 		inject_now = inject_on_this_cmd();
9322 	} else {
9323 		inject_now = false;
9324 	}
9325 	if (unlikely(sdebug_verbose &&
9326 		     !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) {
9327 		char b[120];
9328 		int n, len, sb;
9329 
9330 		len = scp->cmd_len;
9331 		sb = (int)sizeof(b);
9332 		if (len > 32)
9333 			strcpy(b, "too long, over 32 bytes");
9334 		else {
9335 			for (k = 0, n = 0; k < len && n < sb; ++k)
9336 				n += scnprintf(b + n, sb - n, "%02x ",
9337 					       (u32)cmd[k]);
9338 		}
9339 		sdev_printk(KERN_INFO, sdp, "%s: tag=%#x, cmd %s\n", my_name,
9340 			    blk_mq_unique_tag(scsi_cmd_to_rq(scp)), b);
9341 	}
9342 	if (unlikely(inject_now && (sdebug_opts & SDEBUG_OPT_HOST_BUSY)))
9343 		return SCSI_MLQUEUE_HOST_BUSY;
9344 	has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS);
9345 	if (unlikely(lun_index >= sdebug_max_luns && !has_wlun_rl))
9346 		goto err_out;
9347 
9348 	sdeb_i = opcode_ind_arr[opcode];	/* fully mapped */
9349 	oip = &opcode_info_arr[sdeb_i];		/* safe if table consistent */
9350 	devip = (struct sdebug_dev_info *)sdp->hostdata;
9351 	if (unlikely(!devip)) {
9352 		devip = find_build_dev_info(sdp);
9353 		if (NULL == devip)
9354 			goto err_out;
9355 	}
9356 
9357 	if (sdebug_timeout_cmd(scp)) {
9358 		scmd_printk(KERN_INFO, scp, "timeout command 0x%x\n", opcode);
9359 		return 0;
9360 	}
9361 
9362 	ret = sdebug_fail_queue_cmd(scp);
9363 	if (ret) {
9364 		scmd_printk(KERN_INFO, scp, "fail queue command 0x%x with 0x%x\n",
9365 				opcode, ret);
9366 		return ret;
9367 	}
9368 
9369 	if (sdebug_fail_cmd(scp, &ret, &err)) {
9370 		scmd_printk(KERN_INFO, scp,
9371 			"fail command 0x%x with hostbyte=0x%x, "
9372 			"driverbyte=0x%x, statusbyte=0x%x, "
9373 			"sense_key=0x%x, asc=0x%x, asq=0x%x\n",
9374 			opcode, err.host_byte, err.driver_byte,
9375 			err.status_byte, err.sense_key, err.asc, err.asq);
9376 		return ret;
9377 	}
9378 
9379 	if (unlikely(inject_now && !atomic_read(&sdeb_inject_pending)))
9380 		atomic_set(&sdeb_inject_pending, 1);
9381 
9382 	na = oip->num_attached;
9383 	r_pfp = oip->pfp;
9384 	if (na) {	/* multiple commands with this opcode */
9385 		r_oip = oip;
9386 		if (FF_SA & r_oip->flags) {
9387 			if (F_SA_LOW & oip->flags)
9388 				sa = 0x1f & cmd[1];
9389 			else
9390 				sa = get_unaligned_be16(cmd + 8);
9391 			for (k = 0; k <= na; oip = r_oip->arrp + k++) {
9392 				if (opcode == oip->opcode && sa == oip->sa &&
9393 					(devsel & oip->devsel) != 0)
9394 					break;
9395 			}
9396 		} else {   /* since no service action only check opcode */
9397 			for (k = 0; k <= na; oip = r_oip->arrp + k++) {
9398 				if (opcode == oip->opcode &&
9399 					(devsel & oip->devsel) != 0)
9400 					break;
9401 			}
9402 		}
9403 		if (k > na) {
9404 			if (F_SA_LOW & r_oip->flags)
9405 				mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4);
9406 			else if (F_SA_HIGH & r_oip->flags)
9407 				mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7);
9408 			else
9409 				mk_sense_invalid_opcode(scp);
9410 			goto check_cond;
9411 		}
9412 	}	/* else (when na==0) we assume the oip is a match */
9413 	flags = oip->flags;
9414 	if (unlikely(F_INV_OP & flags)) {
9415 		mk_sense_invalid_opcode(scp);
9416 		goto check_cond;
9417 	}
9418 	if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) {
9419 		if (sdebug_verbose)
9420 			sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n",
9421 				    my_name, opcode, " supported for wlun");
9422 		mk_sense_invalid_opcode(scp);
9423 		goto check_cond;
9424 	}
9425 	if (unlikely(sdebug_strict)) {	/* check cdb against mask */
9426 		u8 rem;
9427 		int j;
9428 
9429 		for (k = 1; k < oip->len_mask[0] && k < 16; ++k) {
9430 			rem = ~oip->len_mask[k] & cmd[k];
9431 			if (rem) {
9432 				for (j = 7; j >= 0; --j, rem <<= 1) {
9433 					if (0x80 & rem)
9434 						break;
9435 				}
9436 				mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j);
9437 				goto check_cond;
9438 			}
9439 		}
9440 	}
9441 	if (unlikely(!(F_SKIP_UA & flags) &&
9442 		     find_first_bit(devip->uas_bm,
9443 				    SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) {
9444 		errsts = make_ua(scp, devip);
9445 		if (errsts)
9446 			goto check_cond;
9447 	}
9448 	if (unlikely(((F_M_ACCESS & flags) || scp->cmnd[0] == TEST_UNIT_READY) &&
9449 		     atomic_read(&devip->stopped))) {
9450 		errsts = resp_not_ready(scp, devip);
9451 		if (errsts)
9452 			goto fini;
9453 	}
9454 	if (sdebug_fake_rw && (F_FAKE_RW & flags))
9455 		goto fini;
9456 	if (unlikely(sdebug_every_nth)) {
9457 		if (fake_timeout(scp))
9458 			return 0;	/* ignore command: make trouble */
9459 	}
9460 	if (likely(oip->pfp))
9461 		pfp = oip->pfp;	/* calls a resp_* function */
9462 	else
9463 		pfp = r_pfp;    /* if leaf function ptr NULL, try the root's */
9464 
9465 fini:
9466 	if (F_DELAY_OVERR & flags)	/* cmds like INQUIRY respond asap */
9467 		return schedule_resp(scp, devip, errsts, pfp, 0, 0);
9468 	else if ((flags & F_LONG_DELAY) && (sdebug_jdelay > 0 ||
9469 					    sdebug_ndelay > 10000)) {
9470 		/*
9471 		 * Skip long delays if ndelay <= 10 microseconds. Otherwise
9472 		 * for Start Stop Unit (SSU) want at least 1 second delay and
9473 		 * if sdebug_jdelay>1 want a long delay of that many seconds.
9474 		 * For Synchronize Cache want 1/20 of SSU's delay.
9475 		 */
9476 		int jdelay = (sdebug_jdelay < 2) ? 1 : sdebug_jdelay;
9477 		int denom = (flags & F_SYNC_DELAY) ? 20 : 1;
9478 
9479 		jdelay = mult_frac(USER_HZ * jdelay, HZ, denom * USER_HZ);
9480 		return schedule_resp(scp, devip, errsts, pfp, jdelay, 0);
9481 	} else
9482 		return schedule_resp(scp, devip, errsts, pfp, sdebug_jdelay,
9483 				     sdebug_ndelay);
9484 check_cond:
9485 	return schedule_resp(scp, devip, check_condition_result, NULL, 0, 0);
9486 err_out:
9487 	return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, NULL, 0, 0);
9488 }
9489 
9490 static int sdebug_init_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
9491 {
9492 	struct sdebug_scsi_cmd *sdsc = scsi_cmd_priv(cmd);
9493 	struct sdebug_defer *sd_dp = &sdsc->sd_dp;
9494 
9495 	if (blk_mq_is_reserved_rq(scsi_cmd_to_rq(cmd)))
9496 		return 0;
9497 
9498 	spin_lock_init(&sdsc->lock);
9499 	hrtimer_setup(&sd_dp->hrt, sdebug_q_cmd_hrt_complete, CLOCK_MONOTONIC,
9500 		      HRTIMER_MODE_REL_PINNED);
9501 	INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete);
9502 
9503 	return 0;
9504 }
9505 
9506 static const struct scsi_host_template sdebug_driver_template = {
9507 	.show_info =		scsi_debug_show_info,
9508 	.write_info =		scsi_debug_write_info,
9509 	.proc_name =		sdebug_proc_name,
9510 	.name =			"SCSI DEBUG",
9511 	.info =			scsi_debug_info,
9512 	.sdev_init =		scsi_debug_sdev_init,
9513 	.sdev_configure =	scsi_debug_sdev_configure,
9514 	.sdev_destroy =		scsi_debug_sdev_destroy,
9515 	.ioctl =		scsi_debug_ioctl,
9516 	.queuecommand =		scsi_debug_queuecommand,
9517 	.queue_reserved_command = scsi_debug_process_reserved_command,
9518 	.change_queue_depth =	sdebug_change_qdepth,
9519 	.map_queues =		sdebug_map_queues,
9520 	.mq_poll =		sdebug_blk_mq_poll,
9521 	.eh_abort_handler =	scsi_debug_abort,
9522 	.eh_device_reset_handler = scsi_debug_device_reset,
9523 	.eh_target_reset_handler = scsi_debug_target_reset,
9524 	.eh_bus_reset_handler = scsi_debug_bus_reset,
9525 	.eh_host_reset_handler = scsi_debug_host_reset,
9526 	.can_queue =		SDEBUG_CANQUEUE,
9527 	.nr_reserved_cmds =	1,
9528 	.this_id =		7,
9529 	.sg_tablesize =		SG_MAX_SEGMENTS,
9530 	.cmd_per_lun =		DEF_CMD_PER_LUN,
9531 	.max_sectors =		-1U,
9532 	.max_segment_size =	-1U,
9533 	.module =		THIS_MODULE,
9534 	.skip_settle_delay =	1,
9535 	.track_queue_depth =	1,
9536 	.cmd_size = sizeof(union sdebug_priv),
9537 	.init_cmd_priv = sdebug_init_cmd_priv,
9538 	.target_alloc =		sdebug_target_alloc,
9539 	.target_destroy =	sdebug_target_destroy,
9540 };
9541 
9542 static int sdebug_driver_probe(struct device *dev)
9543 {
9544 	int error = 0;
9545 	struct sdebug_host_info *sdbg_host;
9546 	struct Scsi_Host *hpnt;
9547 	int hprot;
9548 
9549 	sdbg_host = dev_to_sdebug_host(dev);
9550 
9551 	hpnt = scsi_host_alloc(&sdebug_driver_template, 0);
9552 	if (NULL == hpnt) {
9553 		pr_err("scsi_host_alloc failed\n");
9554 		error = -ENODEV;
9555 		return error;
9556 	}
9557 	hpnt->can_queue = sdebug_max_queue;
9558 	hpnt->cmd_per_lun = sdebug_max_queue;
9559 	if (!sdebug_clustering)
9560 		hpnt->dma_boundary = PAGE_SIZE - 1;
9561 
9562 	if (submit_queues > nr_cpu_ids) {
9563 		pr_warn("%s: trim submit_queues (was %d) to nr_cpu_ids=%u\n",
9564 			my_name, submit_queues, nr_cpu_ids);
9565 		submit_queues = nr_cpu_ids;
9566 	}
9567 	/*
9568 	 * Decide whether to tell scsi subsystem that we want mq. The
9569 	 * following should give the same answer for each host.
9570 	 */
9571 	hpnt->nr_hw_queues = submit_queues;
9572 	if (sdebug_host_max_queue)
9573 		hpnt->host_tagset = 1;
9574 
9575 	/* poll queues are possible for nr_hw_queues > 1 */
9576 	if (hpnt->nr_hw_queues == 1 || (poll_queues < 1)) {
9577 		pr_warn("%s: trim poll_queues to 0. poll_q/nr_hw = (%d/%d)\n",
9578 			 my_name, poll_queues, hpnt->nr_hw_queues);
9579 		poll_queues = 0;
9580 	}
9581 
9582 	/*
9583 	 * Poll queues don't need interrupts, but we need at least one I/O queue
9584 	 * left over for non-polled I/O.
9585 	 * If condition not met, trim poll_queues to 1 (just for simplicity).
9586 	 */
9587 	if (poll_queues >= submit_queues) {
9588 		if (submit_queues < 3)
9589 			pr_warn("%s: trim poll_queues to 1\n", my_name);
9590 		else
9591 			pr_warn("%s: trim poll_queues to 1. Perhaps try poll_queues=%d\n",
9592 				my_name, submit_queues - 1);
9593 		poll_queues = 1;
9594 	}
9595 	if (poll_queues)
9596 		hpnt->nr_maps = 3;
9597 
9598 	sdbg_host->shost = hpnt;
9599 	if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id))
9600 		hpnt->max_id = sdebug_num_tgts + 1;
9601 	else
9602 		hpnt->max_id = sdebug_num_tgts;
9603 	/* = sdebug_max_luns; */
9604 	hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
9605 
9606 	hprot = 0;
9607 
9608 	switch (sdebug_dif) {
9609 
9610 	case T10_PI_TYPE1_PROTECTION:
9611 		hprot = SHOST_DIF_TYPE1_PROTECTION;
9612 		if (sdebug_dix)
9613 			hprot |= SHOST_DIX_TYPE1_PROTECTION;
9614 		break;
9615 
9616 	case T10_PI_TYPE2_PROTECTION:
9617 		hprot = SHOST_DIF_TYPE2_PROTECTION;
9618 		if (sdebug_dix)
9619 			hprot |= SHOST_DIX_TYPE2_PROTECTION;
9620 		break;
9621 
9622 	case T10_PI_TYPE3_PROTECTION:
9623 		hprot = SHOST_DIF_TYPE3_PROTECTION;
9624 		if (sdebug_dix)
9625 			hprot |= SHOST_DIX_TYPE3_PROTECTION;
9626 		break;
9627 
9628 	default:
9629 		if (sdebug_dix)
9630 			hprot |= SHOST_DIX_TYPE0_PROTECTION;
9631 		break;
9632 	}
9633 
9634 	scsi_host_set_prot(hpnt, hprot);
9635 
9636 	if (have_dif_prot || sdebug_dix)
9637 		pr_info("host protection%s%s%s%s%s%s%s\n",
9638 			(hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
9639 			(hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
9640 			(hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
9641 			(hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
9642 			(hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
9643 			(hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
9644 			(hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
9645 
9646 	if (sdebug_guard == 1)
9647 		scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP);
9648 	else
9649 		scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC);
9650 
9651 	sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts);
9652 	if (sdebug_every_nth)	/* need stats counters for every_nth */
9653 		sdebug_statistics = true;
9654 	error = scsi_add_host(hpnt, &sdbg_host->dev);
9655 	if (error) {
9656 		pr_err("scsi_add_host failed\n");
9657 		error = -ENODEV;
9658 		scsi_host_put(hpnt);
9659 	} else {
9660 		scsi_scan_host(hpnt);
9661 	}
9662 
9663 	return error;
9664 }
9665 
9666 static void sdebug_driver_remove(struct device *dev)
9667 {
9668 	struct sdebug_host_info *sdbg_host;
9669 	struct sdebug_dev_info *sdbg_devinfo, *tmp;
9670 
9671 	sdbg_host = dev_to_sdebug_host(dev);
9672 
9673 	scsi_remove_host(sdbg_host->shost);
9674 
9675 	list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
9676 				 dev_list) {
9677 		list_del(&sdbg_devinfo->dev_list);
9678 		kfree(sdbg_devinfo->zstate);
9679 		kfree(sdbg_devinfo);
9680 	}
9681 
9682 	scsi_host_put(sdbg_host->shost);
9683 }
9684 
9685 static const struct bus_type pseudo_lld_bus = {
9686 	.name = "pseudo",
9687 	.probe = sdebug_driver_probe,
9688 	.remove = sdebug_driver_remove,
9689 	.drv_groups = sdebug_drv_groups,
9690 };
9691