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