]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/scsi/scsi_debug.c
Merge tag 'seccomp-v5.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees...
[mirror_ubuntu-jammy-kernel.git] / drivers / scsi / scsi_debug.c
CommitLineData
8d7c56d0 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
1da177e4
LT
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 *
80c49563 10 * Copyright (C) 2001 - 2018 Douglas Gilbert
1da177e4 11 *
78d4e5a0 12 * For documentation see http://sg.danny.cz/sg/sdebug26.html
1da177e4
LT
13 */
14
c1287970
TW
15
16#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
17
1da177e4
LT
18#include <linux/module.h>
19
20#include <linux/kernel.h>
1da177e4 21#include <linux/errno.h>
b333a819 22#include <linux/jiffies.h>
5a0e3ad6 23#include <linux/slab.h>
1da177e4
LT
24#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/genhd.h>
27#include <linux/fs.h>
28#include <linux/init.h>
29#include <linux/proc_fs.h>
1da177e4
LT
30#include <linux/vmalloc.h>
31#include <linux/moduleparam.h>
852e034d 32#include <linux/scatterlist.h>
1da177e4 33#include <linux/blkdev.h>
c6a44287 34#include <linux/crc-t10dif.h>
cbf67842
DG
35#include <linux/spinlock.h>
36#include <linux/interrupt.h>
37#include <linux/atomic.h>
38#include <linux/hrtimer.h>
09ba24c1 39#include <linux/uuid.h>
6ebf105c 40#include <linux/t10-pi.h>
1442f76d 41#include <linux/msdos_partition.h>
c6a44287
MP
42
43#include <net/checksum.h>
9ff26eef 44
44d92694
MP
45#include <asm/unaligned.h>
46
9ff26eef
FT
47#include <scsi/scsi.h>
48#include <scsi/scsi_cmnd.h>
49#include <scsi/scsi_device.h>
1da177e4
LT
50#include <scsi/scsi_host.h>
51#include <scsi/scsicam.h>
a34c4e98 52#include <scsi/scsi_eh.h>
cbf67842 53#include <scsi/scsi_tcq.h>
395cef03 54#include <scsi/scsi_dbg.h>
1da177e4 55
c6a44287 56#include "sd.h"
1da177e4 57#include "scsi_logging.h"
1da177e4 58
773642d9 59/* make sure inq_product_rev string corresponds to this version */
80c49563 60#define SDEBUG_VERSION "0188" /* format to fit INQUIRY revision field */
40d07b52 61static const char *sdebug_version_date = "20190125";
cbf67842
DG
62
63#define MY_NAME "scsi_debug"
1da177e4 64
6f3cbf55 65/* Additional Sense Code (ASC) */
c65b1445
DG
66#define NO_ADDITIONAL_SENSE 0x0
67#define LOGICAL_UNIT_NOT_READY 0x4
c2248fc9 68#define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8
1da177e4 69#define UNRECOVERED_READ_ERR 0x11
c65b1445 70#define PARAMETER_LIST_LENGTH_ERR 0x1a
1da177e4 71#define INVALID_OPCODE 0x20
22017ed2 72#define LBA_OUT_OF_RANGE 0x21
1da177e4 73#define INVALID_FIELD_IN_CDB 0x24
c65b1445 74#define INVALID_FIELD_IN_PARAM_LIST 0x26
9447b6ce 75#define WRITE_PROTECTED 0x27
cbf67842
DG
76#define UA_RESET_ASC 0x29
77#define UA_CHANGED_ASC 0x2a
19c8ead7
EM
78#define TARGET_CHANGED_ASC 0x3f
79#define LUNS_CHANGED_ASCQ 0x0e
22017ed2
DG
80#define INSUFF_RES_ASC 0x55
81#define INSUFF_RES_ASCQ 0x3
cbf67842
DG
82#define POWER_ON_RESET_ASCQ 0x0
83#define BUS_RESET_ASCQ 0x2 /* scsi bus reset occurred */
84#define MODE_CHANGED_ASCQ 0x1 /* mode parameters changed */
22017ed2 85#define CAPACITY_CHANGED_ASCQ 0x9
1da177e4 86#define SAVING_PARAMS_UNSUP 0x39
6f3cbf55 87#define TRANSPORT_PROBLEM 0x4b
c65b1445
DG
88#define THRESHOLD_EXCEEDED 0x5d
89#define LOW_POWER_COND_ON 0x5e
22017ed2 90#define MISCOMPARE_VERIFY_ASC 0x1d
acafd0b9
EM
91#define MICROCODE_CHANGED_ASCQ 0x1 /* with TARGET_CHANGED_ASC */
92#define MICROCODE_CHANGED_WO_RESET_ASCQ 0x16
481b5e5c 93#define WRITE_ERROR_ASC 0xc
1da177e4 94
6f3cbf55
DG
95/* Additional Sense Code Qualifier (ASCQ) */
96#define ACK_NAK_TO 0x3
97
1da177e4
LT
98/* Default values for driver parameters */
99#define DEF_NUM_HOST 1
100#define DEF_NUM_TGTS 1
101#define DEF_MAX_LUNS 1
102/* With these defaults, this driver will make 1 host with 1 target
103 * (id 0) containing 1 logical unit (lun 0). That is 1 device.
104 */
5b94e232 105#define DEF_ATO 1
9b760fd8 106#define DEF_CDB_LEN 10
c2206098 107#define DEF_JDELAY 1 /* if > 0 unit is a jiffy */
1da177e4 108#define DEF_DEV_SIZE_MB 8
5b94e232
MP
109#define DEF_DIF 0
110#define DEF_DIX 0
1da177e4 111#define DEF_D_SENSE 0
5b94e232 112#define DEF_EVERY_NTH 0
23183910 113#define DEF_FAKE_RW 0
c6a44287 114#define DEF_GUARD 0
cbf67842 115#define DEF_HOST_LOCK 0
5b94e232
MP
116#define DEF_LBPU 0
117#define DEF_LBPWS 0
118#define DEF_LBPWS10 0
be1dd78d 119#define DEF_LBPRZ 1
ea61fca5 120#define DEF_LOWEST_ALIGNED 0
cbf67842 121#define DEF_NDELAY 0 /* if > 0 unit is a nanosecond */
5b94e232
MP
122#define DEF_NO_LUN_0 0
123#define DEF_NUM_PARTS 0
124#define DEF_OPTS 0
32c5844a 125#define DEF_OPT_BLKS 1024
5b94e232 126#define DEF_PHYSBLK_EXP 0
86e6828a 127#define DEF_OPT_XFERLEN_EXP 0
b01f6f83 128#define DEF_PTYPE TYPE_DISK
d986788b 129#define DEF_REMOVABLE false
760f3b03 130#define DEF_SCSI_LEVEL 7 /* INQUIRY, byte2 [6->SPC-4; 7->SPC-5] */
5b94e232
MP
131#define DEF_SECTOR_SIZE 512
132#define DEF_UNMAP_ALIGNMENT 0
133#define DEF_UNMAP_GRANULARITY 1
6014759c
MP
134#define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF
135#define DEF_UNMAP_MAX_DESC 256
5b94e232
MP
136#define DEF_VIRTUAL_GB 0
137#define DEF_VPD_USE_HOSTNO 1
138#define DEF_WRITESAME_LENGTH 0xFFFF
c2248fc9 139#define DEF_STRICT 0
c4837394
DG
140#define DEF_STATISTICS false
141#define DEF_SUBMIT_QUEUES 1
09ba24c1 142#define DEF_UUID_CTL 0
c2206098 143#define JDELAY_OVERRIDDEN -9999
1da177e4 144
b01f6f83
DG
145#define SDEBUG_LUN_0_VAL 0
146
773642d9
DG
147/* bit mask values for sdebug_opts */
148#define SDEBUG_OPT_NOISE 1
149#define SDEBUG_OPT_MEDIUM_ERR 2
150#define SDEBUG_OPT_TIMEOUT 4
151#define SDEBUG_OPT_RECOVERED_ERR 8
152#define SDEBUG_OPT_TRANSPORT_ERR 16
153#define SDEBUG_OPT_DIF_ERR 32
154#define SDEBUG_OPT_DIX_ERR 64
155#define SDEBUG_OPT_MAC_TIMEOUT 128
156#define SDEBUG_OPT_SHORT_TRANSFER 0x100
157#define SDEBUG_OPT_Q_NOISE 0x200
158#define SDEBUG_OPT_ALL_TSF 0x400
159#define SDEBUG_OPT_RARE_TSF 0x800
160#define SDEBUG_OPT_N_WCE 0x1000
161#define SDEBUG_OPT_RESET_NOISE 0x2000
162#define SDEBUG_OPT_NO_CDB_NOISE 0x4000
7ee6d1b4 163#define SDEBUG_OPT_HOST_BUSY 0x8000
7382f9d8 164#define SDEBUG_OPT_CMD_ABORT 0x10000
773642d9
DG
165#define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \
166 SDEBUG_OPT_RESET_NOISE)
167#define SDEBUG_OPT_ALL_INJECTING (SDEBUG_OPT_RECOVERED_ERR | \
168 SDEBUG_OPT_TRANSPORT_ERR | \
169 SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR | \
7ee6d1b4 170 SDEBUG_OPT_SHORT_TRANSFER | \
7382f9d8
DG
171 SDEBUG_OPT_HOST_BUSY | \
172 SDEBUG_OPT_CMD_ABORT)
1da177e4 173/* When "every_nth" > 0 then modulo "every_nth" commands:
fd32119b 174 * - a missing response is simulated if SDEBUG_OPT_TIMEOUT is set
1da177e4 175 * - a RECOVERED_ERROR is simulated on successful read and write
773642d9 176 * commands if SDEBUG_OPT_RECOVERED_ERR is set.
6f3cbf55 177 * - a TRANSPORT_ERROR is simulated on successful read and write
773642d9 178 * commands if SDEBUG_OPT_TRANSPORT_ERR is set.
7382f9d8
DG
179 * - similarly for DIF_ERR, DIX_ERR, SHORT_TRANSFER, HOST_BUSY and
180 * CMD_ABORT
1da177e4 181 *
7382f9d8
DG
182 * When "every_nth" < 0 then after "- every_nth" commands the selected
183 * error will be injected. The error will be injected on every subsequent
184 * command until some other action occurs; for example, the user writing
185 * a new value (other than -1 or 1) to every_nth:
186 * echo 0 > /sys/bus/pseudo/drivers/scsi_debug/every_nth
1da177e4
LT
187 */
188
fd32119b 189/* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs) are returned in
cbf67842
DG
190 * priority order. In the subset implemented here lower numbers have higher
191 * priority. The UA numbers should be a sequence starting from 0 with
192 * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */
193#define SDEBUG_UA_POR 0 /* Power on, reset, or bus device reset */
194#define SDEBUG_UA_BUS_RESET 1
195#define SDEBUG_UA_MODE_CHANGED 2
0d01c5df 196#define SDEBUG_UA_CAPACITY_CHANGED 3
19c8ead7 197#define SDEBUG_UA_LUNS_CHANGED 4
acafd0b9
EM
198#define SDEBUG_UA_MICROCODE_CHANGED 5 /* simulate firmware change */
199#define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 6
200#define SDEBUG_NUM_UAS 7
cbf67842 201
773642d9 202/* when 1==SDEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
1da177e4
LT
203 * sector on read commands: */
204#define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
32f7ef73 205#define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */
1da177e4
LT
206
207/* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
208 * or "peripheral device" addressing (value 0) */
209#define SAM2_LUN_ADDRESS_METHOD 0
210
c4837394
DG
211/* SDEBUG_CANQUEUE is the maximum number of commands that can be queued
212 * (for response) per submit queue at one time. Can be reduced by max_queue
213 * option. Command responses are not queued when jdelay=0 and ndelay=0. The
214 * per-device DEF_CMD_PER_LUN can be changed via sysfs:
215 * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth
216 * but cannot exceed SDEBUG_CANQUEUE .
217 */
218#define SDEBUG_CANQUEUE_WORDS 3 /* a WORD is bits in a long */
219#define SDEBUG_CANQUEUE (SDEBUG_CANQUEUE_WORDS * BITS_PER_LONG)
cbf67842
DG
220#define DEF_CMD_PER_LUN 255
221
fd32119b
DG
222#define F_D_IN 1
223#define F_D_OUT 2
224#define F_D_OUT_MAYBE 4 /* WRITE SAME, NDOB bit */
225#define F_D_UNKN 8
226#define F_RL_WLUN_OK 0x10
227#define F_SKIP_UA 0x20
228#define F_DELAY_OVERR 0x40
229#define F_SA_LOW 0x80 /* cdb byte 1, bits 4 to 0 */
230#define F_SA_HIGH 0x100 /* as used by variable length cdbs */
231#define F_INV_OP 0x200
232#define F_FAKE_RW 0x400
233#define F_M_ACCESS 0x800 /* media access */
4f2c8bf6
DG
234#define F_SSU_DELAY 0x1000
235#define F_SYNC_DELAY 0x2000
fd32119b
DG
236
237#define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR)
46f64e70 238#define FF_MEDIA_IO (F_M_ACCESS | F_FAKE_RW)
fd32119b 239#define FF_SA (F_SA_HIGH | F_SA_LOW)
4f2c8bf6 240#define F_LONG_DELAY (F_SSU_DELAY | F_SYNC_DELAY)
fd32119b
DG
241
242#define SDEBUG_MAX_PARTS 4
243
b01f6f83 244#define SDEBUG_MAX_CMD_LEN 32
fd32119b
DG
245
246
247struct sdebug_dev_info {
248 struct list_head dev_list;
249 unsigned int channel;
250 unsigned int target;
251 u64 lun;
bf476433 252 uuid_t lu_name;
fd32119b
DG
253 struct sdebug_host_info *sdbg_host;
254 unsigned long uas_bm[1];
255 atomic_t num_in_q;
c4837394 256 atomic_t stopped;
fd32119b
DG
257 bool used;
258};
259
260struct sdebug_host_info {
261 struct list_head host_list;
262 struct Scsi_Host *shost;
263 struct device dev;
264 struct list_head dev_info_list;
265};
266
267#define to_sdebug_host(d) \
268 container_of(d, struct sdebug_host_info, dev)
269
10bde980
DG
270enum sdeb_defer_type {SDEB_DEFER_NONE = 0, SDEB_DEFER_HRT = 1,
271 SDEB_DEFER_WQ = 2};
272
fd32119b
DG
273struct sdebug_defer {
274 struct hrtimer hrt;
275 struct execute_work ew;
c4837394
DG
276 int sqa_idx; /* index of sdebug_queue array */
277 int qc_idx; /* index of sdebug_queued_cmd array within sqa_idx */
278 int issuing_cpu;
10bde980
DG
279 bool init_hrt;
280 bool init_wq;
7382f9d8 281 bool aborted; /* true when blk_abort_request() already called */
10bde980 282 enum sdeb_defer_type defer_t;
fd32119b
DG
283};
284
285struct sdebug_queued_cmd {
c4837394
DG
286 /* corresponding bit set in in_use_bm[] in owning struct sdebug_queue
287 * instance indicates this slot is in use.
288 */
fd32119b
DG
289 struct sdebug_defer *sd_dp;
290 struct scsi_cmnd *a_cmnd;
c4837394
DG
291 unsigned int inj_recovered:1;
292 unsigned int inj_transport:1;
293 unsigned int inj_dif:1;
294 unsigned int inj_dix:1;
295 unsigned int inj_short:1;
7ee6d1b4 296 unsigned int inj_host_busy:1;
7382f9d8 297 unsigned int inj_cmd_abort:1;
fd32119b
DG
298};
299
c4837394
DG
300struct sdebug_queue {
301 struct sdebug_queued_cmd qc_arr[SDEBUG_CANQUEUE];
302 unsigned long in_use_bm[SDEBUG_CANQUEUE_WORDS];
303 spinlock_t qc_lock;
304 atomic_t blocked; /* to temporarily stop more being queued */
fd32119b
DG
305};
306
c4837394
DG
307static atomic_t sdebug_cmnd_count; /* number of incoming commands */
308static atomic_t sdebug_completions; /* count of deferred completions */
309static atomic_t sdebug_miss_cpus; /* submission + completion cpus differ */
310static atomic_t sdebug_a_tsf; /* 'almost task set full' counter */
311
fd32119b 312struct opcode_info_t {
b01f6f83
DG
313 u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff */
314 /* for terminating element */
fd32119b
DG
315 u8 opcode; /* if num_attached > 0, preferred */
316 u16 sa; /* service action */
317 u32 flags; /* OR-ed set of SDEB_F_* */
318 int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
319 const struct opcode_info_t *arrp; /* num_attached elements or NULL */
9a051019
DG
320 u8 len_mask[16]; /* len_mask[0]-->cdb_len, then mask for cdb */
321 /* 1 to min(cdb_len, 15); ignore cdb[15...] */
fd32119b
DG
322};
323
324/* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */
c2248fc9
DG
325enum sdeb_opcode_index {
326 SDEB_I_INVALID_OPCODE = 0,
327 SDEB_I_INQUIRY = 1,
328 SDEB_I_REPORT_LUNS = 2,
329 SDEB_I_REQUEST_SENSE = 3,
330 SDEB_I_TEST_UNIT_READY = 4,
331 SDEB_I_MODE_SENSE = 5, /* 6, 10 */
332 SDEB_I_MODE_SELECT = 6, /* 6, 10 */
333 SDEB_I_LOG_SENSE = 7,
334 SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */
335 SDEB_I_READ = 9, /* 6, 10, 12, 16 */
336 SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */
337 SDEB_I_START_STOP = 11,
46f64e70
DG
338 SDEB_I_SERV_ACT_IN_16 = 12, /* add ...SERV_ACT_IN_12 if needed */
339 SDEB_I_SERV_ACT_OUT_16 = 13, /* add ...SERV_ACT_OUT_12 if needed */
c2248fc9
DG
340 SDEB_I_MAINT_IN = 14,
341 SDEB_I_MAINT_OUT = 15,
342 SDEB_I_VERIFY = 16, /* 10 only */
481b5e5c 343 SDEB_I_VARIABLE_LEN = 17, /* READ(32), WRITE(32), WR_SCAT(32) */
c2248fc9
DG
344 SDEB_I_RESERVE = 18, /* 6, 10 */
345 SDEB_I_RELEASE = 19, /* 6, 10 */
346 SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */
347 SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */
348 SDEB_I_ATA_PT = 22, /* 12, 16 */
349 SDEB_I_SEND_DIAG = 23,
350 SDEB_I_UNMAP = 24,
c208556a
BVA
351 SDEB_I_WRITE_BUFFER = 25,
352 SDEB_I_WRITE_SAME = 26, /* 10, 16 */
353 SDEB_I_SYNC_CACHE = 27, /* 10, 16 */
354 SDEB_I_COMP_WRITE = 28,
355 SDEB_I_LAST_ELEMENT = 29, /* keep this last (previous + 1) */
c2248fc9
DG
356};
357
c4837394 358
c2248fc9
DG
359static const unsigned char opcode_ind_arr[256] = {
360/* 0x0; 0x0->0x1f: 6 byte cdbs */
361 SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE,
362 0, 0, 0, 0,
363 SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0,
364 0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
365 SDEB_I_RELEASE,
366 0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG,
367 SDEB_I_ALLOW_REMOVAL, 0,
368/* 0x20; 0x20->0x3f: 10 byte cdbs */
369 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0,
370 SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY,
371 0, 0, 0, 0, 0, SDEB_I_SYNC_CACHE, 0, 0,
372 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0,
373/* 0x40; 0x40->0x5f: 10 byte cdbs */
374 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0,
375 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0,
c208556a 376 0, 0, 0, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
c2248fc9
DG
377 SDEB_I_RELEASE,
378 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0,
fd32119b 379/* 0x60; 0x60->0x7d are reserved, 0x7e is "extended cdb" */
c2248fc9
DG
380 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
381 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
382 0, SDEB_I_VARIABLE_LEN,
383/* 0x80; 0x80->0x9f: 16 byte cdbs */
384 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0,
385 SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0, 0, 0, 0, 0,
80c49563 386 0, SDEB_I_SYNC_CACHE, 0, SDEB_I_WRITE_SAME, 0, 0, 0, 0,
46f64e70 387 0, 0, 0, 0, 0, 0, SDEB_I_SERV_ACT_IN_16, SDEB_I_SERV_ACT_OUT_16,
c2248fc9
DG
388/* 0xa0; 0xa0->0xbf: 12 byte cdbs */
389 SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN,
390 SDEB_I_MAINT_OUT, 0, 0, 0,
46f64e70
DG
391 SDEB_I_READ, 0 /* SDEB_I_SERV_ACT_OUT_12 */, SDEB_I_WRITE,
392 0 /* SDEB_I_SERV_ACT_IN_12 */, 0, 0, 0, 0,
c2248fc9
DG
393 0, 0, 0, 0, 0, 0, 0, 0,
394 0, 0, 0, 0, 0, 0, 0, 0,
395/* 0xc0; 0xc0->0xff: vendor specific */
396 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
397 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
398 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
399 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
400};
401
80c49563
DG
402/*
403 * The following "response" functions return the SCSI mid-level's 4 byte
404 * tuple-in-an-int. To handle commands with an IMMED bit, for a faster
405 * command completion, they can mask their return value with
406 * SDEG_RES_IMMED_MASK .
407 */
408#define SDEG_RES_IMMED_MASK 0x40000000
409
c2248fc9
DG
410static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *);
411static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *);
412static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *);
413static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
414static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *);
415static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
416static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *);
417static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
418static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
481b5e5c 419static int resp_write_scat(struct scsi_cmnd *, struct sdebug_dev_info *);
c2248fc9
DG
420static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *);
421static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *);
422static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *);
423static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *);
424static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *);
38d5c833
DG
425static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *);
426static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *);
c2248fc9
DG
427static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *);
428static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *);
38d5c833 429static int resp_comp_write(struct scsi_cmnd *, struct sdebug_dev_info *);
acafd0b9 430static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *);
80c49563 431static int resp_sync_cache(struct scsi_cmnd *, struct sdebug_dev_info *);
c2248fc9 432
46f64e70
DG
433/*
434 * The following are overflow arrays for cdbs that "hit" the same index in
435 * the opcode_info_arr array. The most time sensitive (or commonly used) cdb
436 * should be placed in opcode_info_arr[], the others should be placed here.
437 */
438static const struct opcode_info_t msense_iarr[] = {
c2248fc9
DG
439 {0, 0x1a, 0, F_D_IN, NULL, NULL,
440 {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
441};
442
46f64e70 443static const struct opcode_info_t mselect_iarr[] = {
c2248fc9
DG
444 {0, 0x15, 0, F_D_OUT, NULL, NULL,
445 {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
446};
447
46f64e70
DG
448static const struct opcode_info_t read_iarr[] = {
449 {0, 0x28, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(10) */
b7e24581 450 {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
c2248fc9 451 0, 0, 0, 0} },
46f64e70 452 {0, 0x8, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL, /* READ(6) */
c2248fc9 453 {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
46f64e70 454 {0, 0xa8, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(12) */
b7e24581 455 {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf,
c2248fc9
DG
456 0xc7, 0, 0, 0, 0} },
457};
458
46f64e70
DG
459static const struct opcode_info_t write_iarr[] = {
460 {0, 0x2a, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(10) */
461 NULL, {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7,
462 0, 0, 0, 0, 0, 0} },
463 {0, 0xa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(6) */
464 NULL, {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0,
465 0, 0, 0} },
466 {0, 0xaa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(12) */
467 NULL, {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
468 0xbf, 0xc7, 0, 0, 0, 0} },
c2248fc9
DG
469};
470
46f64e70 471static const struct opcode_info_t sa_in_16_iarr[] = {
c2248fc9
DG
472 {0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL,
473 {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
46f64e70 474 0xff, 0xff, 0xff, 0, 0xc7} }, /* GET LBA STATUS(16) */
c2248fc9
DG
475};
476
46f64e70
DG
477static const struct opcode_info_t vl_iarr[] = { /* VARIABLE LENGTH */
478 {0, 0x7f, 0xb, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_dt0,
b7e24581 479 NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0xb, 0xfa,
c2248fc9 480 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */
481b5e5c
DG
481 {0, 0x7f, 0x11, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_scat,
482 NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x11, 0xf8,
483 0, 0xff, 0xff, 0x0, 0x0} }, /* WRITE SCATTERED(32) */
c2248fc9
DG
484};
485
46f64e70 486static const struct opcode_info_t maint_in_iarr[] = { /* MAINT IN */
38d5c833 487 {0, 0xa3, 0xc, F_SA_LOW | F_D_IN, resp_rsup_opcodes, NULL,
c2248fc9 488 {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0,
46f64e70 489 0xc7, 0, 0, 0, 0} }, /* REPORT SUPPORTED OPERATION CODES */
38d5c833 490 {0, 0xa3, 0xd, F_SA_LOW | F_D_IN, resp_rsup_tmfs, NULL,
c2248fc9 491 {12, 0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
46f64e70 492 0, 0} }, /* REPORTED SUPPORTED TASK MANAGEMENT FUNCTIONS */
c2248fc9
DG
493};
494
46f64e70
DG
495static const struct opcode_info_t write_same_iarr[] = {
496 {0, 0x93, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, resp_write_same_16, NULL,
c2248fc9 497 {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
46f64e70 498 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* WRITE SAME(16) */
c2248fc9
DG
499};
500
46f64e70
DG
501static const struct opcode_info_t reserve_iarr[] = {
502 {0, 0x16, 0, F_D_OUT, NULL, NULL, /* RESERVE(6) */
c2248fc9
DG
503 {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
504};
505
46f64e70
DG
506static const struct opcode_info_t release_iarr[] = {
507 {0, 0x17, 0, F_D_OUT, NULL, NULL, /* RELEASE(6) */
c2248fc9
DG
508 {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
509};
510
80c49563 511static const struct opcode_info_t sync_cache_iarr[] = {
4f2c8bf6 512 {0, 0x91, 0, F_SYNC_DELAY | F_M_ACCESS, resp_sync_cache, NULL,
80c49563
DG
513 {16, 0x6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
514 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* SYNC_CACHE (16) */
515};
516
c2248fc9
DG
517
518/* This array is accessed via SDEB_I_* values. Make sure all are mapped,
519 * plus the terminating elements for logic that scans this table such as
520 * REPORT SUPPORTED OPERATION CODES. */
521static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEMENT + 1] = {
522/* 0 */
46f64e70 523 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* unknown opcodes */
c2248fc9 524 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
46f64e70 525 {0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL, /* INQUIRY */
c2248fc9
DG
526 {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
527 {0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL,
528 {12, 0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
46f64e70 529 0, 0} }, /* REPORT LUNS */
c2248fc9
DG
530 {0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL,
531 {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
532 {0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */
533 {6, 0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
46f64e70
DG
534/* 5 */
535 {ARRAY_SIZE(msense_iarr), 0x5a, 0, F_D_IN, /* MODE SENSE(10) */
536 resp_mode_sense, msense_iarr, {10, 0xf8, 0xff, 0xff, 0, 0, 0,
537 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
538 {ARRAY_SIZE(mselect_iarr), 0x55, 0, F_D_OUT, /* MODE SELECT(10) */
539 resp_mode_select, mselect_iarr, {10, 0xf1, 0, 0, 0, 0, 0, 0xff,
540 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
541 {0, 0x4d, 0, F_D_IN, resp_log_sense, NULL, /* LOG SENSE */
c2248fc9
DG
542 {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0,
543 0, 0, 0} },
46f64e70 544 {0, 0x25, 0, F_D_IN, resp_readcap, NULL, /* READ CAPACITY(10) */
c2248fc9
DG
545 {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0,
546 0, 0} },
46f64e70
DG
547 {ARRAY_SIZE(read_iarr), 0x88, 0, F_D_IN | FF_MEDIA_IO, /* READ(16) */
548 resp_read_dt0, read_iarr, {16, 0xfe, 0xff, 0xff, 0xff, 0xff,
549 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} },
c2248fc9 550/* 10 */
46f64e70
DG
551 {ARRAY_SIZE(write_iarr), 0x8a, 0, F_D_OUT | FF_MEDIA_IO,
552 resp_write_dt0, write_iarr, /* WRITE(16) */
553 {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
80c49563 554 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} },
4f2c8bf6 555 {0, 0x1b, 0, F_SSU_DELAY, resp_start_stop, NULL,/* START STOP UNIT */
c2248fc9 556 {6, 0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
46f64e70
DG
557 {ARRAY_SIZE(sa_in_16_iarr), 0x9e, 0x10, F_SA_LOW | F_D_IN,
558 resp_readcap16, sa_in_16_iarr, /* SA_IN(16), READ CAPACITY(16) */
559 {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
560 0xff, 0xff, 0xff, 0xff, 0x1, 0xc7} },
481b5e5c
DG
561 {0, 0x9f, 0x12, F_SA_LOW | F_D_OUT | FF_MEDIA_IO, resp_write_scat,
562 NULL, {16, 0x12, 0xf9, 0x0, 0xff, 0xff, 0, 0, 0xff, 0xff, 0xff,
563 0xff, 0xff, 0xff, 0xff, 0xc7} }, /* SA_OUT(16), WRITE SCAT(16) */
46f64e70
DG
564 {ARRAY_SIZE(maint_in_iarr), 0xa3, 0xa, F_SA_LOW | F_D_IN,
565 resp_report_tgtpgs, /* MAINT IN, REPORT TARGET PORT GROUPS */
566 maint_in_iarr, {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff,
567 0xff, 0, 0xc7, 0, 0, 0, 0} },
568/* 15 */
c2248fc9
DG
569 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */
570 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
46f64e70 571 {0, 0x2f, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, NULL, NULL, /* VERIFY(10) */
f7f9f26b
DG
572 {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7,
573 0, 0, 0, 0, 0, 0} },
46f64e70
DG
574 {ARRAY_SIZE(vl_iarr), 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_MEDIA_IO,
575 resp_read_dt0, vl_iarr, /* VARIABLE LENGTH, READ(32) */
576 {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x9, 0xfe, 0, 0xff, 0xff,
577 0xff, 0xff} },
578 {ARRAY_SIZE(reserve_iarr), 0x56, 0, F_D_OUT,
579 NULL, reserve_iarr, /* RESERVE(10) <no response function> */
c2248fc9
DG
580 {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
581 0} },
46f64e70
DG
582 {ARRAY_SIZE(release_iarr), 0x57, 0, F_D_OUT,
583 NULL, release_iarr, /* RELEASE(10) <no response function> */
c2248fc9
DG
584 {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
585 0} },
586/* 20 */
f7f9f26b
DG
587 {0, 0x1e, 0, 0, NULL, NULL, /* ALLOW REMOVAL */
588 {6, 0, 0, 0, 0x3, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
c2248fc9
DG
589 {0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */
590 {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
591 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */
592 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
593 {0, 0x1d, F_D_OUT, 0, NULL, NULL, /* SEND DIAGNOSTIC */
594 {6, 0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
46f64e70 595 {0, 0x42, 0, F_D_OUT | FF_MEDIA_IO, resp_unmap, NULL, /* UNMAP */
b7e24581 596 {10, 0x1, 0, 0, 0, 0, 0x3f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
46f64e70 597/* 25 */
acafd0b9
EM
598 {0, 0x3b, 0, F_D_OUT_MAYBE, resp_write_buffer, NULL,
599 {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0,
600 0, 0, 0, 0} }, /* WRITE_BUFFER */
46f64e70
DG
601 {ARRAY_SIZE(write_same_iarr), 0x41, 0, F_D_OUT_MAYBE | FF_MEDIA_IO,
602 resp_write_same_10, write_same_iarr, /* WRITE SAME(10) */
603 {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0,
604 0, 0, 0, 0, 0} },
4f2c8bf6 605 {ARRAY_SIZE(sync_cache_iarr), 0x35, 0, F_SYNC_DELAY | F_M_ACCESS,
80c49563 606 resp_sync_cache, sync_cache_iarr,
b7e24581 607 {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
80c49563 608 0, 0, 0, 0} }, /* SYNC_CACHE (10) */
46f64e70 609 {0, 0x89, 0, F_D_OUT | FF_MEDIA_IO, resp_comp_write, NULL,
c2248fc9 610 {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0,
b7e24581 611 0, 0xff, 0x3f, 0xc7} }, /* COMPARE AND WRITE */
c2248fc9 612
c208556a 613/* 29 */
c2248fc9
DG
614 {0xff, 0, 0, 0, NULL, NULL, /* terminating element */
615 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
616};
617
773642d9
DG
618static int sdebug_add_host = DEF_NUM_HOST;
619static int sdebug_ato = DEF_ATO;
9b760fd8 620static int sdebug_cdb_len = DEF_CDB_LEN;
c2206098 621static int sdebug_jdelay = DEF_JDELAY; /* if > 0 then unit is jiffies */
773642d9
DG
622static int sdebug_dev_size_mb = DEF_DEV_SIZE_MB;
623static int sdebug_dif = DEF_DIF;
624static int sdebug_dix = DEF_DIX;
625static int sdebug_dsense = DEF_D_SENSE;
626static int sdebug_every_nth = DEF_EVERY_NTH;
627static int sdebug_fake_rw = DEF_FAKE_RW;
628static unsigned int sdebug_guard = DEF_GUARD;
629static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED;
630static int sdebug_max_luns = DEF_MAX_LUNS;
c4837394 631static int sdebug_max_queue = SDEBUG_CANQUEUE; /* per submit queue */
d9da891a
LO
632static unsigned int sdebug_medium_error_start = OPT_MEDIUM_ERR_ADDR;
633static int sdebug_medium_error_count = OPT_MEDIUM_ERR_NUM;
cbf67842 634static atomic_t retired_max_queue; /* if > 0 then was prior max_queue */
c2206098 635static int sdebug_ndelay = DEF_NDELAY; /* if > 0 then unit is nanoseconds */
773642d9
DG
636static int sdebug_no_lun_0 = DEF_NO_LUN_0;
637static int sdebug_no_uld;
638static int sdebug_num_parts = DEF_NUM_PARTS;
639static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */
640static int sdebug_opt_blks = DEF_OPT_BLKS;
641static int sdebug_opts = DEF_OPTS;
642static int sdebug_physblk_exp = DEF_PHYSBLK_EXP;
86e6828a 643static int sdebug_opt_xferlen_exp = DEF_OPT_XFERLEN_EXP;
b01f6f83 644static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral device type */
773642d9
DG
645static int sdebug_scsi_level = DEF_SCSI_LEVEL;
646static int sdebug_sector_size = DEF_SECTOR_SIZE;
647static int sdebug_virtual_gb = DEF_VIRTUAL_GB;
648static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO;
649static unsigned int sdebug_lbpu = DEF_LBPU;
650static unsigned int sdebug_lbpws = DEF_LBPWS;
651static unsigned int sdebug_lbpws10 = DEF_LBPWS10;
652static unsigned int sdebug_lbprz = DEF_LBPRZ;
653static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT;
654static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY;
655static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS;
656static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC;
657static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH;
09ba24c1 658static int sdebug_uuid_ctl = DEF_UUID_CTL;
773642d9
DG
659static bool sdebug_removable = DEF_REMOVABLE;
660static bool sdebug_clustering;
661static bool sdebug_host_lock = DEF_HOST_LOCK;
662static bool sdebug_strict = DEF_STRICT;
817fd66b 663static bool sdebug_any_injecting_opt;
773642d9 664static bool sdebug_verbose;
f46eb0e9 665static bool have_dif_prot;
4f2c8bf6 666static bool write_since_sync;
c4837394 667static bool sdebug_statistics = DEF_STATISTICS;
9447b6ce 668static bool sdebug_wp;
1da177e4 669
c65b1445 670static unsigned int sdebug_store_sectors;
1da177e4
LT
671static sector_t sdebug_capacity; /* in sectors */
672
673/* old BIOS stuff, kernel may get rid of them but some mode sense pages
674 may still need them */
675static int sdebug_heads; /* heads per disk */
676static int sdebug_cylinders_per; /* cylinders per surface */
677static int sdebug_sectors_per; /* sectors per cylinder */
678
1da177e4
LT
679static LIST_HEAD(sdebug_host_list);
680static DEFINE_SPINLOCK(sdebug_host_list_lock);
681
fd32119b 682static unsigned char *fake_storep; /* ramdisk storage */
6ebf105c 683static struct t10_pi_tuple *dif_storep; /* protection info */
44d92694 684static void *map_storep; /* provisioning map */
1da177e4 685
44d92694 686static unsigned long map_size;
cbf67842
DG
687static int num_aborts;
688static int num_dev_resets;
689static int num_target_resets;
690static int num_bus_resets;
691static int num_host_resets;
c6a44287
MP
692static int dix_writes;
693static int dix_reads;
694static int dif_errors;
1da177e4 695
c4837394
DG
696static int submit_queues = DEF_SUBMIT_QUEUES; /* > 1 for multi-queue (mq) */
697static struct sdebug_queue *sdebug_q_arr; /* ptr to array of submit queues */
fd32119b 698
1da177e4
LT
699static DEFINE_RWLOCK(atomic_rw);
700
cbf67842
DG
701static char sdebug_proc_name[] = MY_NAME;
702static const char *my_name = MY_NAME;
1da177e4 703
1da177e4
LT
704static struct bus_type pseudo_lld_bus;
705
706static struct device_driver sdebug_driverfs_driver = {
707 .name = sdebug_proc_name,
708 .bus = &pseudo_lld_bus,
1da177e4
LT
709};
710
711static const int check_condition_result =
712 (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
713
c6a44287
MP
714static const int illegal_condition_result =
715 (DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
716
cbf67842
DG
717static const int device_qfull_result =
718 (DID_OK << 16) | (COMMAND_COMPLETE << 8) | SAM_STAT_TASK_SET_FULL;
719
fd32119b 720
760f3b03
DG
721/* Only do the extra work involved in logical block provisioning if one or
722 * more of the lbpu, lbpws or lbpws10 parameters are given and we are doing
723 * real reads and writes (i.e. not skipping them for speed).
724 */
725static inline bool scsi_debug_lbp(void)
fd32119b
DG
726{
727 return 0 == sdebug_fake_rw &&
728 (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10);
729}
c65b1445 730
40d07b52 731static void *lba2fake_store(unsigned long long lba)
14faa944
AM
732{
733 lba = do_div(lba, sdebug_store_sectors);
734
773642d9 735 return fake_storep + lba * sdebug_sector_size;
14faa944
AM
736}
737
6ebf105c 738static struct t10_pi_tuple *dif_store(sector_t sector)
14faa944 739{
49413112 740 sector = sector_div(sector, sdebug_store_sectors);
14faa944
AM
741
742 return dif_storep + sector;
743}
744
8dea0d02
FT
745static void sdebug_max_tgts_luns(void)
746{
747 struct sdebug_host_info *sdbg_host;
748 struct Scsi_Host *hpnt;
749
750 spin_lock(&sdebug_host_list_lock);
751 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
752 hpnt = sdbg_host->shost;
753 if ((hpnt->this_id >= 0) &&
773642d9
DG
754 (sdebug_num_tgts > hpnt->this_id))
755 hpnt->max_id = sdebug_num_tgts + 1;
8dea0d02 756 else
773642d9
DG
757 hpnt->max_id = sdebug_num_tgts;
758 /* sdebug_max_luns; */
f2d3fd29 759 hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
8dea0d02
FT
760 }
761 spin_unlock(&sdebug_host_list_lock);
762}
763
22017ed2
DG
764enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1};
765
766/* Set in_bit to -1 to indicate no bit position of invalid field */
fd32119b
DG
767static void mk_sense_invalid_fld(struct scsi_cmnd *scp,
768 enum sdeb_cmd_data c_d,
769 int in_byte, int in_bit)
22017ed2
DG
770{
771 unsigned char *sbuff;
772 u8 sks[4];
773 int sl, asc;
774
775 sbuff = scp->sense_buffer;
776 if (!sbuff) {
777 sdev_printk(KERN_ERR, scp->device,
778 "%s: sense_buffer is NULL\n", __func__);
779 return;
780 }
781 asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST;
782 memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
773642d9 783 scsi_build_sense_buffer(sdebug_dsense, sbuff, ILLEGAL_REQUEST, asc, 0);
22017ed2
DG
784 memset(sks, 0, sizeof(sks));
785 sks[0] = 0x80;
786 if (c_d)
787 sks[0] |= 0x40;
788 if (in_bit >= 0) {
789 sks[0] |= 0x8;
790 sks[0] |= 0x7 & in_bit;
791 }
792 put_unaligned_be16(in_byte, sks + 1);
773642d9 793 if (sdebug_dsense) {
22017ed2
DG
794 sl = sbuff[7] + 8;
795 sbuff[7] = sl;
796 sbuff[sl] = 0x2;
797 sbuff[sl + 1] = 0x6;
798 memcpy(sbuff + sl + 4, sks, 3);
799 } else
800 memcpy(sbuff + 15, sks, 3);
773642d9 801 if (sdebug_verbose)
22017ed2
DG
802 sdev_printk(KERN_INFO, scp->device, "%s: [sense_key,asc,ascq"
803 "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n",
804 my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit);
805}
806
cbf67842 807static void mk_sense_buffer(struct scsi_cmnd *scp, int key, int asc, int asq)
8dea0d02
FT
808{
809 unsigned char *sbuff;
810
cbf67842
DG
811 sbuff = scp->sense_buffer;
812 if (!sbuff) {
813 sdev_printk(KERN_ERR, scp->device,
814 "%s: sense_buffer is NULL\n", __func__);
815 return;
816 }
817 memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
8dea0d02 818
773642d9 819 scsi_build_sense_buffer(sdebug_dsense, sbuff, key, asc, asq);
8dea0d02 820
773642d9 821 if (sdebug_verbose)
cbf67842
DG
822 sdev_printk(KERN_INFO, scp->device,
823 "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n",
824 my_name, key, asc, asq);
8dea0d02 825}
1da177e4 826
fd32119b 827static void mk_sense_invalid_opcode(struct scsi_cmnd *scp)
22017ed2
DG
828{
829 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
830}
831
6f4e626f
NC
832static int scsi_debug_ioctl(struct scsi_device *dev, unsigned int cmd,
833 void __user *arg)
1da177e4 834{
773642d9 835 if (sdebug_verbose) {
cbf67842
DG
836 if (0x1261 == cmd)
837 sdev_printk(KERN_INFO, dev,
838 "%s: BLKFLSBUF [0x1261]\n", __func__);
839 else if (0x5331 == cmd)
840 sdev_printk(KERN_INFO, dev,
841 "%s: CDROM_GET_CAPABILITY [0x5331]\n",
842 __func__);
843 else
844 sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n",
845 __func__, cmd);
1da177e4
LT
846 }
847 return -EINVAL;
848 /* return -ENOTTY; // correct return but upsets fdisk */
849}
850
9b760fd8
DG
851static void config_cdb_len(struct scsi_device *sdev)
852{
853 switch (sdebug_cdb_len) {
854 case 6: /* suggest 6 byte READ, WRITE and MODE SENSE/SELECT */
855 sdev->use_10_for_rw = false;
856 sdev->use_16_for_rw = false;
857 sdev->use_10_for_ms = false;
858 break;
859 case 10: /* suggest 10 byte RWs and 6 byte MODE SENSE/SELECT */
860 sdev->use_10_for_rw = true;
861 sdev->use_16_for_rw = false;
862 sdev->use_10_for_ms = false;
863 break;
864 case 12: /* suggest 10 byte RWs and 10 byte MODE SENSE/SELECT */
865 sdev->use_10_for_rw = true;
866 sdev->use_16_for_rw = false;
867 sdev->use_10_for_ms = true;
868 break;
869 case 16:
870 sdev->use_10_for_rw = false;
871 sdev->use_16_for_rw = true;
872 sdev->use_10_for_ms = true;
873 break;
874 case 32: /* No knobs to suggest this so same as 16 for now */
875 sdev->use_10_for_rw = false;
876 sdev->use_16_for_rw = true;
877 sdev->use_10_for_ms = true;
878 break;
879 default:
880 pr_warn("unexpected cdb_len=%d, force to 10\n",
881 sdebug_cdb_len);
882 sdev->use_10_for_rw = true;
883 sdev->use_16_for_rw = false;
884 sdev->use_10_for_ms = false;
885 sdebug_cdb_len = 10;
886 break;
887 }
888}
889
890static void all_config_cdb_len(void)
891{
892 struct sdebug_host_info *sdbg_host;
893 struct Scsi_Host *shost;
894 struct scsi_device *sdev;
895
896 spin_lock(&sdebug_host_list_lock);
897 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
898 shost = sdbg_host->shost;
899 shost_for_each_device(sdev, shost) {
900 config_cdb_len(sdev);
901 }
902 }
903 spin_unlock(&sdebug_host_list_lock);
904}
905
19c8ead7
EM
906static void clear_luns_changed_on_target(struct sdebug_dev_info *devip)
907{
908 struct sdebug_host_info *sdhp;
909 struct sdebug_dev_info *dp;
910
911 spin_lock(&sdebug_host_list_lock);
912 list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
913 list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) {
914 if ((devip->sdbg_host == dp->sdbg_host) &&
915 (devip->target == dp->target))
916 clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm);
917 }
918 }
919 spin_unlock(&sdebug_host_list_lock);
920}
921
f46eb0e9 922static int make_ua(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
1da177e4 923{
cbf67842 924 int k;
cbf67842
DG
925
926 k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS);
927 if (k != SDEBUG_NUM_UAS) {
928 const char *cp = NULL;
929
930 switch (k) {
931 case SDEBUG_UA_POR:
f46eb0e9
DG
932 mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC,
933 POWER_ON_RESET_ASCQ);
773642d9 934 if (sdebug_verbose)
cbf67842
DG
935 cp = "power on reset";
936 break;
937 case SDEBUG_UA_BUS_RESET:
f46eb0e9
DG
938 mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC,
939 BUS_RESET_ASCQ);
773642d9 940 if (sdebug_verbose)
cbf67842
DG
941 cp = "bus reset";
942 break;
943 case SDEBUG_UA_MODE_CHANGED:
f46eb0e9
DG
944 mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC,
945 MODE_CHANGED_ASCQ);
773642d9 946 if (sdebug_verbose)
cbf67842
DG
947 cp = "mode parameters changed";
948 break;
0d01c5df 949 case SDEBUG_UA_CAPACITY_CHANGED:
f46eb0e9
DG
950 mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC,
951 CAPACITY_CHANGED_ASCQ);
773642d9 952 if (sdebug_verbose)
0d01c5df 953 cp = "capacity data changed";
f49accf1 954 break;
acafd0b9 955 case SDEBUG_UA_MICROCODE_CHANGED:
f46eb0e9 956 mk_sense_buffer(scp, UNIT_ATTENTION,
b01f6f83
DG
957 TARGET_CHANGED_ASC,
958 MICROCODE_CHANGED_ASCQ);
773642d9 959 if (sdebug_verbose)
acafd0b9
EM
960 cp = "microcode has been changed";
961 break;
962 case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET:
f46eb0e9 963 mk_sense_buffer(scp, UNIT_ATTENTION,
acafd0b9
EM
964 TARGET_CHANGED_ASC,
965 MICROCODE_CHANGED_WO_RESET_ASCQ);
773642d9 966 if (sdebug_verbose)
acafd0b9
EM
967 cp = "microcode has been changed without reset";
968 break;
19c8ead7
EM
969 case SDEBUG_UA_LUNS_CHANGED:
970 /*
971 * SPC-3 behavior is to report a UNIT ATTENTION with
972 * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN
973 * on the target, until a REPORT LUNS command is
974 * received. SPC-4 behavior is to report it only once.
773642d9 975 * NOTE: sdebug_scsi_level does not use the same
19c8ead7
EM
976 * values as struct scsi_device->scsi_level.
977 */
773642d9 978 if (sdebug_scsi_level >= 6) /* SPC-4 and above */
19c8ead7 979 clear_luns_changed_on_target(devip);
f46eb0e9 980 mk_sense_buffer(scp, UNIT_ATTENTION,
19c8ead7
EM
981 TARGET_CHANGED_ASC,
982 LUNS_CHANGED_ASCQ);
773642d9 983 if (sdebug_verbose)
19c8ead7
EM
984 cp = "reported luns data has changed";
985 break;
cbf67842 986 default:
773642d9
DG
987 pr_warn("unexpected unit attention code=%d\n", k);
988 if (sdebug_verbose)
cbf67842
DG
989 cp = "unknown";
990 break;
991 }
992 clear_bit(k, devip->uas_bm);
773642d9 993 if (sdebug_verbose)
f46eb0e9 994 sdev_printk(KERN_INFO, scp->device,
cbf67842
DG
995 "%s reports: Unit attention: %s\n",
996 my_name, cp);
1da177e4
LT
997 return check_condition_result;
998 }
999 return 0;
1000}
1001
fb0cc8d1 1002/* Build SCSI "data-in" buffer. Returns 0 if ok else (DID_ERROR << 16). */
21a61829 1003static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
1da177e4
LT
1004 int arr_len)
1005{
21a61829 1006 int act_len;
ae3d56d8 1007 struct scsi_data_buffer *sdb = &scp->sdb;
1da177e4 1008
072d0bb3 1009 if (!sdb->length)
1da177e4 1010 return 0;
ae3d56d8 1011 if (scp->sc_data_direction != DMA_FROM_DEVICE)
773642d9 1012 return DID_ERROR << 16;
21a61829
FT
1013
1014 act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents,
1015 arr, arr_len);
42d387be 1016 scsi_set_resid(scp, scsi_bufflen(scp) - act_len);
21a61829 1017
1da177e4
LT
1018 return 0;
1019}
1020
fb0cc8d1
DG
1021/* Partial build of SCSI "data-in" buffer. Returns 0 if ok else
1022 * (DID_ERROR << 16). Can write to offset in data-in buffer. If multiple
1023 * calls, not required to write in ascending offset order. Assumes resid
1024 * set to scsi_bufflen() prior to any calls.
1025 */
1026static int p_fill_from_dev_buffer(struct scsi_cmnd *scp, const void *arr,
1027 int arr_len, unsigned int off_dst)
1028{
9237f04e 1029 unsigned int act_len, n;
ae3d56d8 1030 struct scsi_data_buffer *sdb = &scp->sdb;
fb0cc8d1
DG
1031 off_t skip = off_dst;
1032
1033 if (sdb->length <= off_dst)
1034 return 0;
ae3d56d8 1035 if (scp->sc_data_direction != DMA_FROM_DEVICE)
fb0cc8d1
DG
1036 return DID_ERROR << 16;
1037
1038 act_len = sg_pcopy_from_buffer(sdb->table.sgl, sdb->table.nents,
1039 arr, arr_len, skip);
1040 pr_debug("%s: off_dst=%u, scsi_bufflen=%u, act_len=%u, resid=%d\n",
42d387be
BVA
1041 __func__, off_dst, scsi_bufflen(scp), act_len,
1042 scsi_get_resid(scp));
9237f04e 1043 n = scsi_bufflen(scp) - (off_dst + act_len);
42d387be 1044 scsi_set_resid(scp, min(scsi_get_resid(scp), n));
fb0cc8d1
DG
1045 return 0;
1046}
1047
1048/* Fetches from SCSI "data-out" buffer. Returns number of bytes fetched into
1049 * 'arr' or -1 if error.
1050 */
21a61829
FT
1051static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
1052 int arr_len)
1da177e4 1053{
21a61829 1054 if (!scsi_bufflen(scp))
1da177e4 1055 return 0;
ae3d56d8 1056 if (scp->sc_data_direction != DMA_TO_DEVICE)
1da177e4 1057 return -1;
21a61829
FT
1058
1059 return scsi_sg_copy_to_buffer(scp, arr, arr_len);
1da177e4
LT
1060}
1061
1062
e5203cf0
HR
1063static char sdebug_inq_vendor_id[9] = "Linux ";
1064static char sdebug_inq_product_id[17] = "scsi_debug ";
9b760fd8 1065static char sdebug_inq_product_rev[5] = SDEBUG_VERSION;
1b37bd60
DG
1066/* Use some locally assigned NAAs for SAS addresses. */
1067static const u64 naa3_comp_a = 0x3222222000000000ULL;
1068static const u64 naa3_comp_b = 0x3333333000000000ULL;
1069static const u64 naa3_comp_c = 0x3111111000000000ULL;
1da177e4 1070
cbf67842 1071/* Device identification VPD page. Returns number of bytes placed in arr */
760f3b03
DG
1072static int inquiry_vpd_83(unsigned char *arr, int port_group_id,
1073 int target_dev_id, int dev_id_num,
09ba24c1 1074 const char *dev_id_str, int dev_id_str_len,
bf476433 1075 const uuid_t *lu_name)
1da177e4 1076{
c65b1445
DG
1077 int num, port_a;
1078 char b[32];
1da177e4 1079
c65b1445 1080 port_a = target_dev_id + 1;
1da177e4
LT
1081 /* T10 vendor identifier field format (faked) */
1082 arr[0] = 0x2; /* ASCII */
1083 arr[1] = 0x1;
1084 arr[2] = 0x0;
e5203cf0
HR
1085 memcpy(&arr[4], sdebug_inq_vendor_id, 8);
1086 memcpy(&arr[12], sdebug_inq_product_id, 16);
1da177e4
LT
1087 memcpy(&arr[28], dev_id_str, dev_id_str_len);
1088 num = 8 + 16 + dev_id_str_len;
1089 arr[3] = num;
1090 num += 4;
c65b1445 1091 if (dev_id_num >= 0) {
09ba24c1
DG
1092 if (sdebug_uuid_ctl) {
1093 /* Locally assigned UUID */
1094 arr[num++] = 0x1; /* binary (not necessarily sas) */
1095 arr[num++] = 0xa; /* PIV=0, lu, naa */
1096 arr[num++] = 0x0;
1097 arr[num++] = 0x12;
1098 arr[num++] = 0x10; /* uuid type=1, locally assigned */
1099 arr[num++] = 0x0;
1100 memcpy(arr + num, lu_name, 16);
1101 num += 16;
1102 } else {
1b37bd60 1103 /* NAA-3, Logical unit identifier (binary) */
09ba24c1
DG
1104 arr[num++] = 0x1; /* binary (not necessarily sas) */
1105 arr[num++] = 0x3; /* PIV=0, lu, naa */
1106 arr[num++] = 0x0;
1107 arr[num++] = 0x8;
1b37bd60 1108 put_unaligned_be64(naa3_comp_b + dev_id_num, arr + num);
09ba24c1
DG
1109 num += 8;
1110 }
c65b1445
DG
1111 /* Target relative port number */
1112 arr[num++] = 0x61; /* proto=sas, binary */
1113 arr[num++] = 0x94; /* PIV=1, target port, rel port */
1114 arr[num++] = 0x0; /* reserved */
1115 arr[num++] = 0x4; /* length */
1116 arr[num++] = 0x0; /* reserved */
1117 arr[num++] = 0x0; /* reserved */
1118 arr[num++] = 0x0;
1119 arr[num++] = 0x1; /* relative port A */
1120 }
1b37bd60 1121 /* NAA-3, Target port identifier */
c65b1445
DG
1122 arr[num++] = 0x61; /* proto=sas, binary */
1123 arr[num++] = 0x93; /* piv=1, target port, naa */
1124 arr[num++] = 0x0;
1125 arr[num++] = 0x8;
1b37bd60 1126 put_unaligned_be64(naa3_comp_a + port_a, arr + num);
773642d9 1127 num += 8;
1b37bd60 1128 /* NAA-3, Target port group identifier */
5a09e398
HR
1129 arr[num++] = 0x61; /* proto=sas, binary */
1130 arr[num++] = 0x95; /* piv=1, target port group id */
1131 arr[num++] = 0x0;
1132 arr[num++] = 0x4;
1133 arr[num++] = 0;
1134 arr[num++] = 0;
773642d9
DG
1135 put_unaligned_be16(port_group_id, arr + num);
1136 num += 2;
1b37bd60 1137 /* NAA-3, Target device identifier */
c65b1445
DG
1138 arr[num++] = 0x61; /* proto=sas, binary */
1139 arr[num++] = 0xa3; /* piv=1, target device, naa */
1140 arr[num++] = 0x0;
1141 arr[num++] = 0x8;
1b37bd60 1142 put_unaligned_be64(naa3_comp_a + target_dev_id, arr + num);
773642d9 1143 num += 8;
c65b1445
DG
1144 /* SCSI name string: Target device identifier */
1145 arr[num++] = 0x63; /* proto=sas, UTF-8 */
1146 arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */
1147 arr[num++] = 0x0;
1148 arr[num++] = 24;
1b37bd60 1149 memcpy(arr + num, "naa.32222220", 12);
c65b1445
DG
1150 num += 12;
1151 snprintf(b, sizeof(b), "%08X", target_dev_id);
1152 memcpy(arr + num, b, 8);
1153 num += 8;
1154 memset(arr + num, 0, 4);
1155 num += 4;
1156 return num;
1157}
1158
c65b1445
DG
1159static unsigned char vpd84_data[] = {
1160/* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
1161 0x22,0x22,0x22,0x0,0xbb,0x1,
1162 0x22,0x22,0x22,0x0,0xbb,0x2,
1163};
1164
cbf67842 1165/* Software interface identification VPD page */
760f3b03 1166static int inquiry_vpd_84(unsigned char *arr)
c65b1445
DG
1167{
1168 memcpy(arr, vpd84_data, sizeof(vpd84_data));
1169 return sizeof(vpd84_data);
1170}
1171
cbf67842 1172/* Management network addresses VPD page */
760f3b03 1173static int inquiry_vpd_85(unsigned char *arr)
c65b1445
DG
1174{
1175 int num = 0;
91d4c752
JP
1176 const char *na1 = "https://www.kernel.org/config";
1177 const char *na2 = "http://www.kernel.org/log";
c65b1445
DG
1178 int plen, olen;
1179
1180 arr[num++] = 0x1; /* lu, storage config */
1181 arr[num++] = 0x0; /* reserved */
1182 arr[num++] = 0x0;
1183 olen = strlen(na1);
1184 plen = olen + 1;
1185 if (plen % 4)
1186 plen = ((plen / 4) + 1) * 4;
1187 arr[num++] = plen; /* length, null termianted, padded */
1188 memcpy(arr + num, na1, olen);
1189 memset(arr + num + olen, 0, plen - olen);
1190 num += plen;
1191
1192 arr[num++] = 0x4; /* lu, logging */
1193 arr[num++] = 0x0; /* reserved */
1194 arr[num++] = 0x0;
1195 olen = strlen(na2);
1196 plen = olen + 1;
1197 if (plen % 4)
1198 plen = ((plen / 4) + 1) * 4;
1199 arr[num++] = plen; /* length, null terminated, padded */
1200 memcpy(arr + num, na2, olen);
1201 memset(arr + num + olen, 0, plen - olen);
1202 num += plen;
1203
1204 return num;
1205}
1206
1207/* SCSI ports VPD page */
760f3b03 1208static int inquiry_vpd_88(unsigned char *arr, int target_dev_id)
c65b1445
DG
1209{
1210 int num = 0;
1211 int port_a, port_b;
1212
1213 port_a = target_dev_id + 1;
1214 port_b = port_a + 1;
1215 arr[num++] = 0x0; /* reserved */
1216 arr[num++] = 0x0; /* reserved */
1217 arr[num++] = 0x0;
1218 arr[num++] = 0x1; /* relative port 1 (primary) */
1219 memset(arr + num, 0, 6);
1220 num += 6;
1221 arr[num++] = 0x0;
1222 arr[num++] = 12; /* length tp descriptor */
1223 /* naa-5 target port identifier (A) */
1224 arr[num++] = 0x61; /* proto=sas, binary */
1225 arr[num++] = 0x93; /* PIV=1, target port, NAA */
1226 arr[num++] = 0x0; /* reserved */
1227 arr[num++] = 0x8; /* length */
1b37bd60 1228 put_unaligned_be64(naa3_comp_a + port_a, arr + num);
773642d9 1229 num += 8;
c65b1445
DG
1230 arr[num++] = 0x0; /* reserved */
1231 arr[num++] = 0x0; /* reserved */
1232 arr[num++] = 0x0;
1233 arr[num++] = 0x2; /* relative port 2 (secondary) */
1234 memset(arr + num, 0, 6);
1235 num += 6;
1236 arr[num++] = 0x0;
1237 arr[num++] = 12; /* length tp descriptor */
1238 /* naa-5 target port identifier (B) */
1239 arr[num++] = 0x61; /* proto=sas, binary */
1240 arr[num++] = 0x93; /* PIV=1, target port, NAA */
1241 arr[num++] = 0x0; /* reserved */
1242 arr[num++] = 0x8; /* length */
1b37bd60 1243 put_unaligned_be64(naa3_comp_a + port_b, arr + num);
773642d9 1244 num += 8;
c65b1445
DG
1245
1246 return num;
1247}
1248
1249
1250static unsigned char vpd89_data[] = {
1251/* from 4th byte */ 0,0,0,0,
1252'l','i','n','u','x',' ',' ',' ',
1253'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
1254'1','2','3','4',
12550x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
12560xec,0,0,0,
12570x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
12580,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
12590x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
12600x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
12610x53,0x41,
12620x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
12630x20,0x20,
12640x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
12650x10,0x80,
12660,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
12670x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
12680x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
12690,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
12700x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
12710x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
12720,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0,
12730,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12740,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12750,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12760x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
12770,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
12780xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
12790,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0,
12800,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12810,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12820,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12830,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12840,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12850,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12860,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12870,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12880,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12890,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12900,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
12910,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
1292};
1293
cbf67842 1294/* ATA Information VPD page */
760f3b03 1295static int inquiry_vpd_89(unsigned char *arr)
c65b1445
DG
1296{
1297 memcpy(arr, vpd89_data, sizeof(vpd89_data));
1298 return sizeof(vpd89_data);
1299}
1300
1301
1302static unsigned char vpdb0_data[] = {
1e49f785
DG
1303 /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64,
1304 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1305 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1306 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
c65b1445
DG
1307};
1308
cbf67842 1309/* Block limits VPD page (SBC-3) */
760f3b03 1310static int inquiry_vpd_b0(unsigned char *arr)
c65b1445 1311{
ea61fca5
MP
1312 unsigned int gran;
1313
c65b1445 1314 memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
e308b3d1
MP
1315
1316 /* Optimal transfer length granularity */
86e6828a
LH
1317 if (sdebug_opt_xferlen_exp != 0 &&
1318 sdebug_physblk_exp < sdebug_opt_xferlen_exp)
1319 gran = 1 << sdebug_opt_xferlen_exp;
1320 else
1321 gran = 1 << sdebug_physblk_exp;
773642d9 1322 put_unaligned_be16(gran, arr + 2);
e308b3d1
MP
1323
1324 /* Maximum Transfer Length */
773642d9
DG
1325 if (sdebug_store_sectors > 0x400)
1326 put_unaligned_be32(sdebug_store_sectors, arr + 4);
44d92694 1327
e308b3d1 1328 /* Optimal Transfer Length */
773642d9 1329 put_unaligned_be32(sdebug_opt_blks, &arr[8]);
e308b3d1 1330
773642d9 1331 if (sdebug_lbpu) {
e308b3d1 1332 /* Maximum Unmap LBA Count */
773642d9 1333 put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]);
e308b3d1
MP
1334
1335 /* Maximum Unmap Block Descriptor Count */
773642d9 1336 put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]);
44d92694
MP
1337 }
1338
e308b3d1 1339 /* Unmap Granularity Alignment */
773642d9
DG
1340 if (sdebug_unmap_alignment) {
1341 put_unaligned_be32(sdebug_unmap_alignment, &arr[28]);
44d92694
MP
1342 arr[28] |= 0x80; /* UGAVALID */
1343 }
1344
e308b3d1 1345 /* Optimal Unmap Granularity */
773642d9 1346 put_unaligned_be32(sdebug_unmap_granularity, &arr[24]);
6014759c 1347
5b94e232 1348 /* Maximum WRITE SAME Length */
773642d9 1349 put_unaligned_be64(sdebug_write_same_length, &arr[32]);
5b94e232
MP
1350
1351 return 0x3c; /* Mandatory page length for Logical Block Provisioning */
44d92694 1352
c65b1445 1353 return sizeof(vpdb0_data);
1da177e4
LT
1354}
1355
1e49f785 1356/* Block device characteristics VPD page (SBC-3) */
760f3b03 1357static int inquiry_vpd_b1(unsigned char *arr)
eac6e8e4
MW
1358{
1359 memset(arr, 0, 0x3c);
1360 arr[0] = 0;
1e49f785
DG
1361 arr[1] = 1; /* non rotating medium (e.g. solid state) */
1362 arr[2] = 0;
1363 arr[3] = 5; /* less than 1.8" */
eac6e8e4
MW
1364
1365 return 0x3c;
1366}
1da177e4 1367
760f3b03
DG
1368/* Logical block provisioning VPD page (SBC-4) */
1369static int inquiry_vpd_b2(unsigned char *arr)
6014759c 1370{
3f0bc3b3 1371 memset(arr, 0, 0x4);
6014759c 1372 arr[0] = 0; /* threshold exponent */
773642d9 1373 if (sdebug_lbpu)
6014759c 1374 arr[1] = 1 << 7;
773642d9 1375 if (sdebug_lbpws)
6014759c 1376 arr[1] |= 1 << 6;
773642d9 1377 if (sdebug_lbpws10)
5b94e232 1378 arr[1] |= 1 << 5;
760f3b03
DG
1379 if (sdebug_lbprz && scsi_debug_lbp())
1380 arr[1] |= (sdebug_lbprz & 0x7) << 2; /* sbc4r07 and later */
1381 /* anc_sup=0; dp=0 (no provisioning group descriptor) */
1382 /* minimum_percentage=0; provisioning_type=0 (unknown) */
1383 /* threshold_percentage=0 */
3f0bc3b3 1384 return 0x4;
6014759c
MP
1385}
1386
1da177e4 1387#define SDEBUG_LONG_INQ_SZ 96
c65b1445 1388#define SDEBUG_MAX_INQ_ARR_SZ 584
1da177e4 1389
c2248fc9 1390static int resp_inquiry(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
1da177e4
LT
1391{
1392 unsigned char pq_pdt;
91d4c752 1393 unsigned char *arr;
01123ef4 1394 unsigned char *cmd = scp->cmnd;
5a09e398 1395 int alloc_len, n, ret;
760f3b03 1396 bool have_wlun, is_disk;
1da177e4 1397
773642d9 1398 alloc_len = get_unaligned_be16(cmd + 3);
6f3cbf55
DG
1399 arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC);
1400 if (! arr)
1401 return DID_REQUEUE << 16;
760f3b03 1402 is_disk = (sdebug_ptype == TYPE_DISK);
b01f6f83 1403 have_wlun = scsi_is_wlun(scp->device->lun);
c2248fc9 1404 if (have_wlun)
b01f6f83
DG
1405 pq_pdt = TYPE_WLUN; /* present, wlun */
1406 else if (sdebug_no_lun_0 && (devip->lun == SDEBUG_LUN_0_VAL))
1407 pq_pdt = 0x7f; /* not present, PQ=3, PDT=0x1f */
c65b1445 1408 else
773642d9 1409 pq_pdt = (sdebug_ptype & 0x1f);
1da177e4
LT
1410 arr[0] = pq_pdt;
1411 if (0x2 & cmd[1]) { /* CMDDT bit set */
22017ed2 1412 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1);
5a09e398 1413 kfree(arr);
1da177e4
LT
1414 return check_condition_result;
1415 } else if (0x1 & cmd[1]) { /* EVPD bit set */
5a09e398 1416 int lu_id_num, port_group_id, target_dev_id, len;
c65b1445
DG
1417 char lu_id_str[6];
1418 int host_no = devip->sdbg_host->shost->host_no;
1da177e4 1419
5a09e398
HR
1420 port_group_id = (((host_no + 1) & 0x7f) << 8) +
1421 (devip->channel & 0x7f);
b01f6f83 1422 if (sdebug_vpd_use_hostno == 0)
23183910 1423 host_no = 0;
c2248fc9 1424 lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) +
c65b1445
DG
1425 (devip->target * 1000) + devip->lun);
1426 target_dev_id = ((host_no + 1) * 2000) +
1427 (devip->target * 1000) - 3;
1428 len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
1da177e4 1429 if (0 == cmd[2]) { /* supported vital product data pages */
c65b1445
DG
1430 arr[1] = cmd[2]; /*sanity */
1431 n = 4;
1432 arr[n++] = 0x0; /* this page */
1433 arr[n++] = 0x80; /* unit serial number */
1434 arr[n++] = 0x83; /* device identification */
1435 arr[n++] = 0x84; /* software interface ident. */
1436 arr[n++] = 0x85; /* management network addresses */
1437 arr[n++] = 0x86; /* extended inquiry */
1438 arr[n++] = 0x87; /* mode page policy */
1439 arr[n++] = 0x88; /* SCSI ports */
760f3b03
DG
1440 if (is_disk) { /* SBC only */
1441 arr[n++] = 0x89; /* ATA information */
1442 arr[n++] = 0xb0; /* Block limits */
1443 arr[n++] = 0xb1; /* Block characteristics */
1444 arr[n++] = 0xb2; /* Logical Block Prov */
1445 }
c65b1445 1446 arr[3] = n - 4; /* number of supported VPD pages */
1da177e4 1447 } else if (0x80 == cmd[2]) { /* unit serial number */
c65b1445 1448 arr[1] = cmd[2]; /*sanity */
1da177e4 1449 arr[3] = len;
c65b1445 1450 memcpy(&arr[4], lu_id_str, len);
1da177e4 1451 } else if (0x83 == cmd[2]) { /* device identification */
c65b1445 1452 arr[1] = cmd[2]; /*sanity */
760f3b03
DG
1453 arr[3] = inquiry_vpd_83(&arr[4], port_group_id,
1454 target_dev_id, lu_id_num,
09ba24c1
DG
1455 lu_id_str, len,
1456 &devip->lu_name);
c65b1445
DG
1457 } else if (0x84 == cmd[2]) { /* Software interface ident. */
1458 arr[1] = cmd[2]; /*sanity */
760f3b03 1459 arr[3] = inquiry_vpd_84(&arr[4]);
c65b1445
DG
1460 } else if (0x85 == cmd[2]) { /* Management network addresses */
1461 arr[1] = cmd[2]; /*sanity */
760f3b03 1462 arr[3] = inquiry_vpd_85(&arr[4]);
c65b1445
DG
1463 } else if (0x86 == cmd[2]) { /* extended inquiry */
1464 arr[1] = cmd[2]; /*sanity */
1465 arr[3] = 0x3c; /* number of following entries */
8475c811 1466 if (sdebug_dif == T10_PI_TYPE3_PROTECTION)
c6a44287 1467 arr[4] = 0x4; /* SPT: GRD_CHK:1 */
760f3b03 1468 else if (have_dif_prot)
c6a44287
MP
1469 arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */
1470 else
1471 arr[4] = 0x0; /* no protection stuff */
c65b1445
DG
1472 arr[5] = 0x7; /* head of q, ordered + simple q's */
1473 } else if (0x87 == cmd[2]) { /* mode page policy */
1474 arr[1] = cmd[2]; /*sanity */
1475 arr[3] = 0x8; /* number of following entries */
1476 arr[4] = 0x2; /* disconnect-reconnect mp */
1477 arr[6] = 0x80; /* mlus, shared */
1478 arr[8] = 0x18; /* protocol specific lu */
1479 arr[10] = 0x82; /* mlus, per initiator port */
1480 } else if (0x88 == cmd[2]) { /* SCSI Ports */
1481 arr[1] = cmd[2]; /*sanity */
760f3b03
DG
1482 arr[3] = inquiry_vpd_88(&arr[4], target_dev_id);
1483 } else if (is_disk && 0x89 == cmd[2]) { /* ATA information */
c65b1445 1484 arr[1] = cmd[2]; /*sanity */
760f3b03 1485 n = inquiry_vpd_89(&arr[4]);
773642d9 1486 put_unaligned_be16(n, arr + 2);
760f3b03 1487 } else if (is_disk && 0xb0 == cmd[2]) { /* Block limits */
c65b1445 1488 arr[1] = cmd[2]; /*sanity */
760f3b03
DG
1489 arr[3] = inquiry_vpd_b0(&arr[4]);
1490 } else if (is_disk && 0xb1 == cmd[2]) { /* Block char. */
eac6e8e4 1491 arr[1] = cmd[2]; /*sanity */
760f3b03
DG
1492 arr[3] = inquiry_vpd_b1(&arr[4]);
1493 } else if (is_disk && 0xb2 == cmd[2]) { /* LB Prov. */
6014759c 1494 arr[1] = cmd[2]; /*sanity */
760f3b03 1495 arr[3] = inquiry_vpd_b2(&arr[4]);
1da177e4 1496 } else {
22017ed2 1497 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
5a09e398 1498 kfree(arr);
1da177e4
LT
1499 return check_condition_result;
1500 }
773642d9 1501 len = min(get_unaligned_be16(arr + 2) + 4, alloc_len);
5a09e398 1502 ret = fill_from_dev_buffer(scp, arr,
c65b1445 1503 min(len, SDEBUG_MAX_INQ_ARR_SZ));
5a09e398
HR
1504 kfree(arr);
1505 return ret;
1da177e4
LT
1506 }
1507 /* drops through here for a standard inquiry */
773642d9
DG
1508 arr[1] = sdebug_removable ? 0x80 : 0; /* Removable disk */
1509 arr[2] = sdebug_scsi_level;
1da177e4
LT
1510 arr[3] = 2; /* response_data_format==2 */
1511 arr[4] = SDEBUG_LONG_INQ_SZ - 5;
f46eb0e9 1512 arr[5] = (int)have_dif_prot; /* PROTECT bit */
b01f6f83 1513 if (sdebug_vpd_use_hostno == 0)
70bdf202 1514 arr[5] |= 0x10; /* claim: implicit TPGS */
c65b1445 1515 arr[6] = 0x10; /* claim: MultiP */
1da177e4 1516 /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
c65b1445 1517 arr[7] = 0xa; /* claim: LINKED + CMDQUE */
e5203cf0
HR
1518 memcpy(&arr[8], sdebug_inq_vendor_id, 8);
1519 memcpy(&arr[16], sdebug_inq_product_id, 16);
1520 memcpy(&arr[32], sdebug_inq_product_rev, 4);
9b760fd8
DG
1521 /* Use Vendor Specific area to place driver date in ASCII hex */
1522 memcpy(&arr[36], sdebug_version_date, 8);
1da177e4 1523 /* version descriptors (2 bytes each) follow */
760f3b03
DG
1524 put_unaligned_be16(0xc0, arr + 58); /* SAM-6 no version claimed */
1525 put_unaligned_be16(0x5c0, arr + 60); /* SPC-5 no version claimed */
c65b1445 1526 n = 62;
760f3b03
DG
1527 if (is_disk) { /* SBC-4 no version claimed */
1528 put_unaligned_be16(0x600, arr + n);
1529 n += 2;
1530 } else if (sdebug_ptype == TYPE_TAPE) { /* SSC-4 rev 3 */
1531 put_unaligned_be16(0x525, arr + n);
1532 n += 2;
1da177e4 1533 }
760f3b03 1534 put_unaligned_be16(0x2100, arr + n); /* SPL-4 no version claimed */
5a09e398 1535 ret = fill_from_dev_buffer(scp, arr,
1da177e4 1536 min(alloc_len, SDEBUG_LONG_INQ_SZ));
5a09e398
HR
1537 kfree(arr);
1538 return ret;
1da177e4
LT
1539}
1540
fd32119b
DG
1541static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
1542 0, 0, 0x0, 0x0};
1543
91d4c752
JP
1544static int resp_requests(struct scsi_cmnd *scp,
1545 struct sdebug_dev_info *devip)
1da177e4 1546{
91d4c752 1547 unsigned char *sbuff;
01123ef4 1548 unsigned char *cmd = scp->cmnd;
cbf67842 1549 unsigned char arr[SCSI_SENSE_BUFFERSIZE];
2492fc09 1550 bool dsense;
1da177e4
LT
1551 int len = 18;
1552
c65b1445 1553 memset(arr, 0, sizeof(arr));
c2248fc9 1554 dsense = !!(cmd[1] & 1);
cbf67842 1555 sbuff = scp->sense_buffer;
c65b1445 1556 if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
c2248fc9 1557 if (dsense) {
c65b1445
DG
1558 arr[0] = 0x72;
1559 arr[1] = 0x0; /* NO_SENSE in sense_key */
1560 arr[2] = THRESHOLD_EXCEEDED;
1561 arr[3] = 0xff; /* TEST set and MRIE==6 */
c2248fc9 1562 len = 8;
c65b1445
DG
1563 } else {
1564 arr[0] = 0x70;
1565 arr[2] = 0x0; /* NO_SENSE in sense_key */
1566 arr[7] = 0xa; /* 18 byte sense buffer */
1567 arr[12] = THRESHOLD_EXCEEDED;
1568 arr[13] = 0xff; /* TEST set and MRIE==6 */
1569 }
c65b1445 1570 } else {
cbf67842 1571 memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE);
773642d9 1572 if (arr[0] >= 0x70 && dsense == sdebug_dsense)
c2248fc9
DG
1573 ; /* have sense and formats match */
1574 else if (arr[0] <= 0x70) {
1575 if (dsense) {
1576 memset(arr, 0, 8);
1577 arr[0] = 0x72;
1578 len = 8;
1579 } else {
1580 memset(arr, 0, 18);
1581 arr[0] = 0x70;
1582 arr[7] = 0xa;
1583 }
1584 } else if (dsense) {
1585 memset(arr, 0, 8);
c65b1445
DG
1586 arr[0] = 0x72;
1587 arr[1] = sbuff[2]; /* sense key */
1588 arr[2] = sbuff[12]; /* asc */
1589 arr[3] = sbuff[13]; /* ascq */
1590 len = 8;
c2248fc9
DG
1591 } else {
1592 memset(arr, 0, 18);
1593 arr[0] = 0x70;
1594 arr[2] = sbuff[1];
1595 arr[7] = 0xa;
1596 arr[12] = sbuff[1];
1597 arr[13] = sbuff[3];
c65b1445 1598 }
c2248fc9 1599
c65b1445 1600 }
cbf67842 1601 mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0);
1da177e4
LT
1602 return fill_from_dev_buffer(scp, arr, len);
1603}
1604
91d4c752
JP
1605static int resp_start_stop(struct scsi_cmnd *scp,
1606 struct sdebug_dev_info *devip)
c65b1445 1607{
01123ef4 1608 unsigned char *cmd = scp->cmnd;
c4837394 1609 int power_cond, stop;
4f2c8bf6 1610 bool changing;
c65b1445 1611
c65b1445
DG
1612 power_cond = (cmd[4] & 0xf0) >> 4;
1613 if (power_cond) {
22017ed2 1614 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7);
c65b1445
DG
1615 return check_condition_result;
1616 }
c4837394 1617 stop = !(cmd[4] & 1);
4f2c8bf6 1618 changing = atomic_read(&devip->stopped) == !stop;
c4837394 1619 atomic_xchg(&devip->stopped, stop);
4f2c8bf6
DG
1620 if (!changing || cmd[1] & 0x1) /* state unchanged or IMMED set */
1621 return SDEG_RES_IMMED_MASK;
1622 else
1623 return 0;
c65b1445
DG
1624}
1625
28898873
FT
1626static sector_t get_sdebug_capacity(void)
1627{
773642d9
DG
1628 static const unsigned int gibibyte = 1073741824;
1629
1630 if (sdebug_virtual_gb > 0)
1631 return (sector_t)sdebug_virtual_gb *
1632 (gibibyte / sdebug_sector_size);
28898873
FT
1633 else
1634 return sdebug_store_sectors;
1635}
1636
1da177e4 1637#define SDEBUG_READCAP_ARR_SZ 8
91d4c752
JP
1638static int resp_readcap(struct scsi_cmnd *scp,
1639 struct sdebug_dev_info *devip)
1da177e4
LT
1640{
1641 unsigned char arr[SDEBUG_READCAP_ARR_SZ];
c65b1445 1642 unsigned int capac;
1da177e4 1643
c65b1445 1644 /* following just in case virtual_gb changed */
28898873 1645 sdebug_capacity = get_sdebug_capacity();
1da177e4 1646 memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
c65b1445
DG
1647 if (sdebug_capacity < 0xffffffff) {
1648 capac = (unsigned int)sdebug_capacity - 1;
773642d9
DG
1649 put_unaligned_be32(capac, arr + 0);
1650 } else
1651 put_unaligned_be32(0xffffffff, arr + 0);
1652 put_unaligned_be16(sdebug_sector_size, arr + 6);
1da177e4
LT
1653 return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
1654}
1655
c65b1445 1656#define SDEBUG_READCAP16_ARR_SZ 32
91d4c752
JP
1657static int resp_readcap16(struct scsi_cmnd *scp,
1658 struct sdebug_dev_info *devip)
c65b1445 1659{
01123ef4 1660 unsigned char *cmd = scp->cmnd;
c65b1445 1661 unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
773642d9 1662 int alloc_len;
c65b1445 1663
773642d9 1664 alloc_len = get_unaligned_be32(cmd + 10);
c65b1445 1665 /* following just in case virtual_gb changed */
28898873 1666 sdebug_capacity = get_sdebug_capacity();
c65b1445 1667 memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
773642d9
DG
1668 put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0);
1669 put_unaligned_be32(sdebug_sector_size, arr + 8);
1670 arr[13] = sdebug_physblk_exp & 0xf;
1671 arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f;
44d92694 1672
be1dd78d 1673 if (scsi_debug_lbp()) {
5b94e232 1674 arr[14] |= 0x80; /* LBPME */
760f3b03
DG
1675 /* from sbc4r07, this LBPRZ field is 1 bit, but the LBPRZ in
1676 * the LB Provisioning VPD page is 3 bits. Note that lbprz=2
1677 * in the wider field maps to 0 in this field.
1678 */
1679 if (sdebug_lbprz & 1) /* precisely what the draft requires */
1680 arr[14] |= 0x40;
be1dd78d 1681 }
44d92694 1682
773642d9 1683 arr[15] = sdebug_lowest_aligned & 0xff;
c6a44287 1684
760f3b03 1685 if (have_dif_prot) {
773642d9 1686 arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */
c6a44287
MP
1687 arr[12] |= 1; /* PROT_EN */
1688 }
1689
c65b1445
DG
1690 return fill_from_dev_buffer(scp, arr,
1691 min(alloc_len, SDEBUG_READCAP16_ARR_SZ));
1692}
1693
5a09e398
HR
1694#define SDEBUG_MAX_TGTPGS_ARR_SZ 1412
1695
91d4c752
JP
1696static int resp_report_tgtpgs(struct scsi_cmnd *scp,
1697 struct sdebug_dev_info *devip)
5a09e398 1698{
01123ef4 1699 unsigned char *cmd = scp->cmnd;
91d4c752 1700 unsigned char *arr;
5a09e398
HR
1701 int host_no = devip->sdbg_host->shost->host_no;
1702 int n, ret, alen, rlen;
1703 int port_group_a, port_group_b, port_a, port_b;
1704
773642d9 1705 alen = get_unaligned_be32(cmd + 6);
6f3cbf55
DG
1706 arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC);
1707 if (! arr)
1708 return DID_REQUEUE << 16;
5a09e398
HR
1709 /*
1710 * EVPD page 0x88 states we have two ports, one
1711 * real and a fake port with no device connected.
1712 * So we create two port groups with one port each
1713 * and set the group with port B to unavailable.
1714 */
1715 port_a = 0x1; /* relative port A */
1716 port_b = 0x2; /* relative port B */
1717 port_group_a = (((host_no + 1) & 0x7f) << 8) +
773642d9 1718 (devip->channel & 0x7f);
5a09e398 1719 port_group_b = (((host_no + 1) & 0x7f) << 8) +
773642d9 1720 (devip->channel & 0x7f) + 0x80;
5a09e398
HR
1721
1722 /*
1723 * The asymmetric access state is cycled according to the host_id.
1724 */
1725 n = 4;
b01f6f83 1726 if (sdebug_vpd_use_hostno == 0) {
773642d9
DG
1727 arr[n++] = host_no % 3; /* Asymm access state */
1728 arr[n++] = 0x0F; /* claim: all states are supported */
5a09e398 1729 } else {
773642d9
DG
1730 arr[n++] = 0x0; /* Active/Optimized path */
1731 arr[n++] = 0x01; /* only support active/optimized paths */
5a09e398 1732 }
773642d9
DG
1733 put_unaligned_be16(port_group_a, arr + n);
1734 n += 2;
5a09e398
HR
1735 arr[n++] = 0; /* Reserved */
1736 arr[n++] = 0; /* Status code */
1737 arr[n++] = 0; /* Vendor unique */
1738 arr[n++] = 0x1; /* One port per group */
1739 arr[n++] = 0; /* Reserved */
1740 arr[n++] = 0; /* Reserved */
773642d9
DG
1741 put_unaligned_be16(port_a, arr + n);
1742 n += 2;
5a09e398
HR
1743 arr[n++] = 3; /* Port unavailable */
1744 arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */
773642d9
DG
1745 put_unaligned_be16(port_group_b, arr + n);
1746 n += 2;
5a09e398
HR
1747 arr[n++] = 0; /* Reserved */
1748 arr[n++] = 0; /* Status code */
1749 arr[n++] = 0; /* Vendor unique */
1750 arr[n++] = 0x1; /* One port per group */
1751 arr[n++] = 0; /* Reserved */
1752 arr[n++] = 0; /* Reserved */
773642d9
DG
1753 put_unaligned_be16(port_b, arr + n);
1754 n += 2;
5a09e398
HR
1755
1756 rlen = n - 4;
773642d9 1757 put_unaligned_be32(rlen, arr + 0);
5a09e398
HR
1758
1759 /*
1760 * Return the smallest value of either
1761 * - The allocated length
1762 * - The constructed command length
1763 * - The maximum array size
1764 */
1765 rlen = min(alen,n);
1766 ret = fill_from_dev_buffer(scp, arr,
1767 min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ));
1768 kfree(arr);
1769 return ret;
1770}
1771
fd32119b
DG
1772static int resp_rsup_opcodes(struct scsi_cmnd *scp,
1773 struct sdebug_dev_info *devip)
38d5c833
DG
1774{
1775 bool rctd;
1776 u8 reporting_opts, req_opcode, sdeb_i, supp;
1777 u16 req_sa, u;
1778 u32 alloc_len, a_len;
1779 int k, offset, len, errsts, count, bump, na;
1780 const struct opcode_info_t *oip;
1781 const struct opcode_info_t *r_oip;
1782 u8 *arr;
1783 u8 *cmd = scp->cmnd;
1784
1785 rctd = !!(cmd[2] & 0x80);
1786 reporting_opts = cmd[2] & 0x7;
1787 req_opcode = cmd[3];
1788 req_sa = get_unaligned_be16(cmd + 4);
1789 alloc_len = get_unaligned_be32(cmd + 6);
6d310dfb 1790 if (alloc_len < 4 || alloc_len > 0xffff) {
38d5c833
DG
1791 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
1792 return check_condition_result;
1793 }
1794 if (alloc_len > 8192)
1795 a_len = 8192;
1796 else
1797 a_len = alloc_len;
99531e60 1798 arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC);
38d5c833
DG
1799 if (NULL == arr) {
1800 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
1801 INSUFF_RES_ASCQ);
1802 return check_condition_result;
1803 }
1804 switch (reporting_opts) {
1805 case 0: /* all commands */
1806 /* count number of commands */
1807 for (count = 0, oip = opcode_info_arr;
1808 oip->num_attached != 0xff; ++oip) {
1809 if (F_INV_OP & oip->flags)
1810 continue;
1811 count += (oip->num_attached + 1);
1812 }
1813 bump = rctd ? 20 : 8;
1814 put_unaligned_be32(count * bump, arr);
1815 for (offset = 4, oip = opcode_info_arr;
1816 oip->num_attached != 0xff && offset < a_len; ++oip) {
1817 if (F_INV_OP & oip->flags)
1818 continue;
1819 na = oip->num_attached;
1820 arr[offset] = oip->opcode;
1821 put_unaligned_be16(oip->sa, arr + offset + 2);
1822 if (rctd)
1823 arr[offset + 5] |= 0x2;
1824 if (FF_SA & oip->flags)
1825 arr[offset + 5] |= 0x1;
1826 put_unaligned_be16(oip->len_mask[0], arr + offset + 6);
1827 if (rctd)
1828 put_unaligned_be16(0xa, arr + offset + 8);
1829 r_oip = oip;
1830 for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) {
1831 if (F_INV_OP & oip->flags)
1832 continue;
1833 offset += bump;
1834 arr[offset] = oip->opcode;
1835 put_unaligned_be16(oip->sa, arr + offset + 2);
1836 if (rctd)
1837 arr[offset + 5] |= 0x2;
1838 if (FF_SA & oip->flags)
1839 arr[offset + 5] |= 0x1;
1840 put_unaligned_be16(oip->len_mask[0],
1841 arr + offset + 6);
1842 if (rctd)
1843 put_unaligned_be16(0xa,
1844 arr + offset + 8);
1845 }
1846 oip = r_oip;
1847 offset += bump;
1848 }
1849 break;
1850 case 1: /* one command: opcode only */
1851 case 2: /* one command: opcode plus service action */
1852 case 3: /* one command: if sa==0 then opcode only else opcode+sa */
1853 sdeb_i = opcode_ind_arr[req_opcode];
1854 oip = &opcode_info_arr[sdeb_i];
1855 if (F_INV_OP & oip->flags) {
1856 supp = 1;
1857 offset = 4;
1858 } else {
1859 if (1 == reporting_opts) {
1860 if (FF_SA & oip->flags) {
1861 mk_sense_invalid_fld(scp, SDEB_IN_CDB,
1862 2, 2);
1863 kfree(arr);
1864 return check_condition_result;
1865 }
1866 req_sa = 0;
1867 } else if (2 == reporting_opts &&
1868 0 == (FF_SA & oip->flags)) {
1869 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1);
1870 kfree(arr); /* point at requested sa */
1871 return check_condition_result;
1872 }
1873 if (0 == (FF_SA & oip->flags) &&
1874 req_opcode == oip->opcode)
1875 supp = 3;
1876 else if (0 == (FF_SA & oip->flags)) {
1877 na = oip->num_attached;
1878 for (k = 0, oip = oip->arrp; k < na;
1879 ++k, ++oip) {
1880 if (req_opcode == oip->opcode)
1881 break;
1882 }
1883 supp = (k >= na) ? 1 : 3;
1884 } else if (req_sa != oip->sa) {
1885 na = oip->num_attached;
1886 for (k = 0, oip = oip->arrp; k < na;
1887 ++k, ++oip) {
1888 if (req_sa == oip->sa)
1889 break;
1890 }
1891 supp = (k >= na) ? 1 : 3;
1892 } else
1893 supp = 3;
1894 if (3 == supp) {
1895 u = oip->len_mask[0];
1896 put_unaligned_be16(u, arr + 2);
1897 arr[4] = oip->opcode;
1898 for (k = 1; k < u; ++k)
1899 arr[4 + k] = (k < 16) ?
1900 oip->len_mask[k] : 0xff;
1901 offset = 4 + u;
1902 } else
1903 offset = 4;
1904 }
1905 arr[1] = (rctd ? 0x80 : 0) | supp;
1906 if (rctd) {
1907 put_unaligned_be16(0xa, arr + offset);
1908 offset += 12;
1909 }
1910 break;
1911 default:
1912 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2);
1913 kfree(arr);
1914 return check_condition_result;
1915 }
1916 offset = (offset < a_len) ? offset : a_len;
1917 len = (offset < alloc_len) ? offset : alloc_len;
1918 errsts = fill_from_dev_buffer(scp, arr, len);
1919 kfree(arr);
1920 return errsts;
1921}
1922
fd32119b
DG
1923static int resp_rsup_tmfs(struct scsi_cmnd *scp,
1924 struct sdebug_dev_info *devip)
38d5c833
DG
1925{
1926 bool repd;
1927 u32 alloc_len, len;
1928 u8 arr[16];
1929 u8 *cmd = scp->cmnd;
1930
1931 memset(arr, 0, sizeof(arr));
1932 repd = !!(cmd[2] & 0x80);
1933 alloc_len = get_unaligned_be32(cmd + 6);
1934 if (alloc_len < 4) {
1935 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
1936 return check_condition_result;
1937 }
1938 arr[0] = 0xc8; /* ATS | ATSS | LURS */
1939 arr[1] = 0x1; /* ITNRS */
1940 if (repd) {
1941 arr[3] = 0xc;
1942 len = 16;
1943 } else
1944 len = 4;
1945
1946 len = (len < alloc_len) ? len : alloc_len;
1947 return fill_from_dev_buffer(scp, arr, len);
1948}
1949
1da177e4
LT
1950/* <<Following mode page info copied from ST318451LW>> */
1951
91d4c752 1952static int resp_err_recov_pg(unsigned char *p, int pcontrol, int target)
1da177e4
LT
1953{ /* Read-Write Error Recovery page for mode_sense */
1954 unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
1955 5, 0, 0xff, 0xff};
1956
1957 memcpy(p, err_recov_pg, sizeof(err_recov_pg));
1958 if (1 == pcontrol)
1959 memset(p + 2, 0, sizeof(err_recov_pg) - 2);
1960 return sizeof(err_recov_pg);
1961}
1962
91d4c752 1963static int resp_disconnect_pg(unsigned char *p, int pcontrol, int target)
1da177e4
LT
1964{ /* Disconnect-Reconnect page for mode_sense */
1965 unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
1966 0, 0, 0, 0, 0, 0, 0, 0};
1967
1968 memcpy(p, disconnect_pg, sizeof(disconnect_pg));
1969 if (1 == pcontrol)
1970 memset(p + 2, 0, sizeof(disconnect_pg) - 2);
1971 return sizeof(disconnect_pg);
1972}
1973
91d4c752 1974static int resp_format_pg(unsigned char *p, int pcontrol, int target)
1da177e4 1975{ /* Format device page for mode_sense */
597136ab
MP
1976 unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
1977 0, 0, 0, 0, 0, 0, 0, 0,
1978 0, 0, 0, 0, 0x40, 0, 0, 0};
1979
1980 memcpy(p, format_pg, sizeof(format_pg));
773642d9
DG
1981 put_unaligned_be16(sdebug_sectors_per, p + 10);
1982 put_unaligned_be16(sdebug_sector_size, p + 12);
1983 if (sdebug_removable)
597136ab
MP
1984 p[20] |= 0x20; /* should agree with INQUIRY */
1985 if (1 == pcontrol)
1986 memset(p + 2, 0, sizeof(format_pg) - 2);
1987 return sizeof(format_pg);
1da177e4
LT
1988}
1989
fd32119b
DG
1990static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
1991 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0,
1992 0, 0, 0, 0};
1993
91d4c752 1994static int resp_caching_pg(unsigned char *p, int pcontrol, int target)
1da177e4 1995{ /* Caching page for mode_sense */
cbf67842
DG
1996 unsigned char ch_caching_pg[] = {/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0,
1997 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1998 unsigned char d_caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
1da177e4
LT
1999 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
2000
773642d9 2001 if (SDEBUG_OPT_N_WCE & sdebug_opts)
cbf67842 2002 caching_pg[2] &= ~0x4; /* set WCE=0 (default WCE=1) */
1da177e4
LT
2003 memcpy(p, caching_pg, sizeof(caching_pg));
2004 if (1 == pcontrol)
cbf67842
DG
2005 memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg));
2006 else if (2 == pcontrol)
2007 memcpy(p, d_caching_pg, sizeof(d_caching_pg));
1da177e4
LT
2008 return sizeof(caching_pg);
2009}
2010
fd32119b
DG
2011static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
2012 0, 0, 0x2, 0x4b};
2013
91d4c752 2014static int resp_ctrl_m_pg(unsigned char *p, int pcontrol, int target)
1da177e4 2015{ /* Control mode page for mode_sense */
c65b1445 2016 unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
9a051019 2017 0, 0, 0, 0};
c65b1445 2018 unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
1da177e4
LT
2019 0, 0, 0x2, 0x4b};
2020
773642d9 2021 if (sdebug_dsense)
1da177e4 2022 ctrl_m_pg[2] |= 0x4;
c65b1445
DG
2023 else
2024 ctrl_m_pg[2] &= ~0x4;
c6a44287 2025
773642d9 2026 if (sdebug_ato)
c6a44287
MP
2027 ctrl_m_pg[5] |= 0x80; /* ATO=1 */
2028
1da177e4
LT
2029 memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
2030 if (1 == pcontrol)
c65b1445
DG
2031 memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
2032 else if (2 == pcontrol)
2033 memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
1da177e4
LT
2034 return sizeof(ctrl_m_pg);
2035}
2036
c65b1445 2037
91d4c752 2038static int resp_iec_m_pg(unsigned char *p, int pcontrol, int target)
1da177e4 2039{ /* Informational Exceptions control mode page for mode_sense */
c65b1445
DG
2040 unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
2041 0, 0, 0x0, 0x0};
2042 unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
2043 0, 0, 0x0, 0x0};
2044
1da177e4
LT
2045 memcpy(p, iec_m_pg, sizeof(iec_m_pg));
2046 if (1 == pcontrol)
c65b1445
DG
2047 memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
2048 else if (2 == pcontrol)
2049 memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
1da177e4
LT
2050 return sizeof(iec_m_pg);
2051}
2052
91d4c752 2053static int resp_sas_sf_m_pg(unsigned char *p, int pcontrol, int target)
c65b1445
DG
2054{ /* SAS SSP mode page - short format for mode_sense */
2055 unsigned char sas_sf_m_pg[] = {0x19, 0x6,
2056 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0};
2057
2058 memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
2059 if (1 == pcontrol)
2060 memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
2061 return sizeof(sas_sf_m_pg);
2062}
2063
2064
91d4c752 2065static int resp_sas_pcd_m_spg(unsigned char *p, int pcontrol, int target,
c65b1445
DG
2066 int target_dev_id)
2067{ /* SAS phy control and discover mode page for mode_sense */
2068 unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
2069 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
773642d9
DG
2070 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
2071 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
c65b1445
DG
2072 0x2, 0, 0, 0, 0, 0, 0, 0,
2073 0x88, 0x99, 0, 0, 0, 0, 0, 0,
2074 0, 0, 0, 0, 0, 0, 0, 0,
2075 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
773642d9
DG
2076 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
2077 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
c65b1445
DG
2078 0x3, 0, 0, 0, 0, 0, 0, 0,
2079 0x88, 0x99, 0, 0, 0, 0, 0, 0,
2080 0, 0, 0, 0, 0, 0, 0, 0,
2081 };
2082 int port_a, port_b;
2083
1b37bd60
DG
2084 put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 16);
2085 put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 24);
2086 put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 64);
2087 put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 72);
c65b1445
DG
2088 port_a = target_dev_id + 1;
2089 port_b = port_a + 1;
2090 memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
773642d9
DG
2091 put_unaligned_be32(port_a, p + 20);
2092 put_unaligned_be32(port_b, p + 48 + 20);
c65b1445
DG
2093 if (1 == pcontrol)
2094 memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
2095 return sizeof(sas_pcd_m_pg);
2096}
2097
91d4c752 2098static int resp_sas_sha_m_spg(unsigned char *p, int pcontrol)
c65b1445
DG
2099{ /* SAS SSP shared protocol specific port mode subpage */
2100 unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
2101 0, 0, 0, 0, 0, 0, 0, 0,
2102 };
2103
2104 memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
2105 if (1 == pcontrol)
2106 memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
2107 return sizeof(sas_sha_m_pg);
2108}
2109
1da177e4
LT
2110#define SDEBUG_MAX_MSENSE_SZ 256
2111
fd32119b
DG
2112static int resp_mode_sense(struct scsi_cmnd *scp,
2113 struct sdebug_dev_info *devip)
1da177e4 2114{
23183910 2115 int pcontrol, pcode, subpcode, bd_len;
1da177e4 2116 unsigned char dev_spec;
760f3b03 2117 int alloc_len, offset, len, target_dev_id;
c2248fc9 2118 int target = scp->device->id;
91d4c752 2119 unsigned char *ap;
1da177e4 2120 unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
01123ef4 2121 unsigned char *cmd = scp->cmnd;
760f3b03 2122 bool dbd, llbaa, msense_6, is_disk, bad_pcode;
1da177e4 2123
760f3b03 2124 dbd = !!(cmd[1] & 0x8); /* disable block descriptors */
1da177e4
LT
2125 pcontrol = (cmd[2] & 0xc0) >> 6;
2126 pcode = cmd[2] & 0x3f;
2127 subpcode = cmd[3];
2128 msense_6 = (MODE_SENSE == cmd[0]);
760f3b03
DG
2129 llbaa = msense_6 ? false : !!(cmd[1] & 0x10);
2130 is_disk = (sdebug_ptype == TYPE_DISK);
2131 if (is_disk && !dbd)
23183910
DG
2132 bd_len = llbaa ? 16 : 8;
2133 else
2134 bd_len = 0;
773642d9 2135 alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7);
1da177e4
LT
2136 memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
2137 if (0x3 == pcontrol) { /* Saving values not supported */
cbf67842 2138 mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0);
1da177e4
LT
2139 return check_condition_result;
2140 }
c65b1445
DG
2141 target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
2142 (devip->target * 1000) - 3;
b01f6f83 2143 /* for disks set DPOFUA bit and clear write protect (WP) bit */
9447b6ce 2144 if (is_disk) {
b01f6f83 2145 dev_spec = 0x10; /* =0x90 if WP=1 implies read-only */
9447b6ce
MP
2146 if (sdebug_wp)
2147 dev_spec |= 0x80;
2148 } else
23183910 2149 dev_spec = 0x0;
1da177e4
LT
2150 if (msense_6) {
2151 arr[2] = dev_spec;
23183910 2152 arr[3] = bd_len;
1da177e4
LT
2153 offset = 4;
2154 } else {
2155 arr[3] = dev_spec;
23183910
DG
2156 if (16 == bd_len)
2157 arr[4] = 0x1; /* set LONGLBA bit */
2158 arr[7] = bd_len; /* assume 255 or less */
1da177e4
LT
2159 offset = 8;
2160 }
2161 ap = arr + offset;
28898873
FT
2162 if ((bd_len > 0) && (!sdebug_capacity))
2163 sdebug_capacity = get_sdebug_capacity();
2164
23183910 2165 if (8 == bd_len) {
773642d9
DG
2166 if (sdebug_capacity > 0xfffffffe)
2167 put_unaligned_be32(0xffffffff, ap + 0);
2168 else
2169 put_unaligned_be32(sdebug_capacity, ap + 0);
2170 put_unaligned_be16(sdebug_sector_size, ap + 6);
23183910
DG
2171 offset += bd_len;
2172 ap = arr + offset;
2173 } else if (16 == bd_len) {
773642d9
DG
2174 put_unaligned_be64((u64)sdebug_capacity, ap + 0);
2175 put_unaligned_be32(sdebug_sector_size, ap + 12);
23183910
DG
2176 offset += bd_len;
2177 ap = arr + offset;
2178 }
1da177e4 2179
c65b1445
DG
2180 if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) {
2181 /* TODO: Control Extension page */
22017ed2 2182 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
1da177e4
LT
2183 return check_condition_result;
2184 }
760f3b03
DG
2185 bad_pcode = false;
2186
1da177e4
LT
2187 switch (pcode) {
2188 case 0x1: /* Read-Write error recovery page, direct access */
2189 len = resp_err_recov_pg(ap, pcontrol, target);
2190 offset += len;
2191 break;
2192 case 0x2: /* Disconnect-Reconnect page, all devices */
2193 len = resp_disconnect_pg(ap, pcontrol, target);
2194 offset += len;
2195 break;
9a051019 2196 case 0x3: /* Format device page, direct access */
760f3b03
DG
2197 if (is_disk) {
2198 len = resp_format_pg(ap, pcontrol, target);
2199 offset += len;
2200 } else
2201 bad_pcode = true;
9a051019 2202 break;
1da177e4 2203 case 0x8: /* Caching page, direct access */
760f3b03
DG
2204 if (is_disk) {
2205 len = resp_caching_pg(ap, pcontrol, target);
2206 offset += len;
2207 } else
2208 bad_pcode = true;
1da177e4
LT
2209 break;
2210 case 0xa: /* Control Mode page, all devices */
2211 len = resp_ctrl_m_pg(ap, pcontrol, target);
2212 offset += len;
2213 break;
c65b1445
DG
2214 case 0x19: /* if spc==1 then sas phy, control+discover */
2215 if ((subpcode > 0x2) && (subpcode < 0xff)) {
22017ed2 2216 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
c65b1445 2217 return check_condition_result;
9a051019 2218 }
c65b1445
DG
2219 len = 0;
2220 if ((0x0 == subpcode) || (0xff == subpcode))
2221 len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
2222 if ((0x1 == subpcode) || (0xff == subpcode))
2223 len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
2224 target_dev_id);
2225 if ((0x2 == subpcode) || (0xff == subpcode))
2226 len += resp_sas_sha_m_spg(ap + len, pcontrol);
2227 offset += len;
2228 break;
1da177e4
LT
2229 case 0x1c: /* Informational Exceptions Mode page, all devices */
2230 len = resp_iec_m_pg(ap, pcontrol, target);
2231 offset += len;
2232 break;
2233 case 0x3f: /* Read all Mode pages */
c65b1445
DG
2234 if ((0 == subpcode) || (0xff == subpcode)) {
2235 len = resp_err_recov_pg(ap, pcontrol, target);
2236 len += resp_disconnect_pg(ap + len, pcontrol, target);
760f3b03
DG
2237 if (is_disk) {
2238 len += resp_format_pg(ap + len, pcontrol,
2239 target);
2240 len += resp_caching_pg(ap + len, pcontrol,
2241 target);
2242 }
c65b1445
DG
2243 len += resp_ctrl_m_pg(ap + len, pcontrol, target);
2244 len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
2245 if (0xff == subpcode) {
2246 len += resp_sas_pcd_m_spg(ap + len, pcontrol,
2247 target, target_dev_id);
2248 len += resp_sas_sha_m_spg(ap + len, pcontrol);
2249 }
2250 len += resp_iec_m_pg(ap + len, pcontrol, target);
760f3b03 2251 offset += len;
c65b1445 2252 } else {
22017ed2 2253 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
c65b1445 2254 return check_condition_result;
9a051019 2255 }
1da177e4
LT
2256 break;
2257 default:
760f3b03
DG
2258 bad_pcode = true;
2259 break;
2260 }
2261 if (bad_pcode) {
22017ed2 2262 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
1da177e4
LT
2263 return check_condition_result;
2264 }
2265 if (msense_6)
2266 arr[0] = offset - 1;
773642d9
DG
2267 else
2268 put_unaligned_be16((offset - 2), arr + 0);
1da177e4
LT
2269 return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
2270}
2271
c65b1445
DG
2272#define SDEBUG_MAX_MSELECT_SZ 512
2273
fd32119b
DG
2274static int resp_mode_select(struct scsi_cmnd *scp,
2275 struct sdebug_dev_info *devip)
c65b1445
DG
2276{
2277 int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
c2248fc9 2278 int param_len, res, mpage;
c65b1445 2279 unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
01123ef4 2280 unsigned char *cmd = scp->cmnd;
c2248fc9 2281 int mselect6 = (MODE_SELECT == cmd[0]);
c65b1445 2282
c65b1445
DG
2283 memset(arr, 0, sizeof(arr));
2284 pf = cmd[1] & 0x10;
2285 sp = cmd[1] & 0x1;
773642d9 2286 param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7);
c65b1445 2287 if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
22017ed2 2288 mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1);
c65b1445
DG
2289 return check_condition_result;
2290 }
9a051019
DG
2291 res = fetch_to_dev_buffer(scp, arr, param_len);
2292 if (-1 == res)
773642d9
DG
2293 return DID_ERROR << 16;
2294 else if (sdebug_verbose && (res < param_len))
cbf67842
DG
2295 sdev_printk(KERN_INFO, scp->device,
2296 "%s: cdb indicated=%d, IO sent=%d bytes\n",
2297 __func__, param_len, res);
773642d9
DG
2298 md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2);
2299 bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6);
23183910 2300 if (md_len > 2) {
22017ed2 2301 mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1);
c65b1445
DG
2302 return check_condition_result;
2303 }
2304 off = bd_len + (mselect6 ? 4 : 8);
2305 mpage = arr[off] & 0x3f;
2306 ps = !!(arr[off] & 0x80);
2307 if (ps) {
22017ed2 2308 mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7);
c65b1445
DG
2309 return check_condition_result;
2310 }
2311 spf = !!(arr[off] & 0x40);
773642d9 2312 pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) :
c65b1445
DG
2313 (arr[off + 1] + 2);
2314 if ((pg_len + off) > param_len) {
cbf67842 2315 mk_sense_buffer(scp, ILLEGAL_REQUEST,
c65b1445
DG
2316 PARAMETER_LIST_LENGTH_ERR, 0);
2317 return check_condition_result;
2318 }
2319 switch (mpage) {
cbf67842
DG
2320 case 0x8: /* Caching Mode page */
2321 if (caching_pg[1] == arr[off + 1]) {
2322 memcpy(caching_pg + 2, arr + off + 2,
2323 sizeof(caching_pg) - 2);
2324 goto set_mode_changed_ua;
2325 }
2326 break;
c65b1445
DG
2327 case 0xa: /* Control Mode page */
2328 if (ctrl_m_pg[1] == arr[off + 1]) {
2329 memcpy(ctrl_m_pg + 2, arr + off + 2,
2330 sizeof(ctrl_m_pg) - 2);
9447b6ce
MP
2331 if (ctrl_m_pg[4] & 0x8)
2332 sdebug_wp = true;
2333 else
2334 sdebug_wp = false;
773642d9 2335 sdebug_dsense = !!(ctrl_m_pg[2] & 0x4);
cbf67842 2336 goto set_mode_changed_ua;
c65b1445
DG
2337 }
2338 break;
2339 case 0x1c: /* Informational Exceptions Mode page */
2340 if (iec_m_pg[1] == arr[off + 1]) {
2341 memcpy(iec_m_pg + 2, arr + off + 2,
2342 sizeof(iec_m_pg) - 2);
cbf67842 2343 goto set_mode_changed_ua;
c65b1445
DG
2344 }
2345 break;
2346 default:
2347 break;
2348 }
22017ed2 2349 mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5);
c65b1445 2350 return check_condition_result;
cbf67842
DG
2351set_mode_changed_ua:
2352 set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm);
2353 return 0;
c65b1445
DG
2354}
2355
91d4c752 2356static int resp_temp_l_pg(unsigned char *arr)
c65b1445
DG
2357{
2358 unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38,
2359 0x0, 0x1, 0x3, 0x2, 0x0, 65,
2360 };
2361
9a051019
DG
2362 memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
2363 return sizeof(temp_l_pg);
c65b1445
DG
2364}
2365
91d4c752 2366static int resp_ie_l_pg(unsigned char *arr)
c65b1445
DG
2367{
2368 unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
2369 };
2370
9a051019 2371 memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
c65b1445
DG
2372 if (iec_m_pg[2] & 0x4) { /* TEST bit set */
2373 arr[4] = THRESHOLD_EXCEEDED;
2374 arr[5] = 0xff;
2375 }
9a051019 2376 return sizeof(ie_l_pg);
c65b1445
DG
2377}
2378
2379#define SDEBUG_MAX_LSENSE_SZ 512
2380
9a051019
DG
2381static int resp_log_sense(struct scsi_cmnd *scp,
2382 struct sdebug_dev_info *devip)
c65b1445 2383{
ab17241c 2384 int ppc, sp, pcode, subpcode, alloc_len, len, n;
c65b1445 2385 unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
01123ef4 2386 unsigned char *cmd = scp->cmnd;
c65b1445 2387
c65b1445
DG
2388 memset(arr, 0, sizeof(arr));
2389 ppc = cmd[1] & 0x2;
2390 sp = cmd[1] & 0x1;
2391 if (ppc || sp) {
22017ed2 2392 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0);
c65b1445
DG
2393 return check_condition_result;
2394 }
c65b1445 2395 pcode = cmd[2] & 0x3f;
23183910 2396 subpcode = cmd[3] & 0xff;
773642d9 2397 alloc_len = get_unaligned_be16(cmd + 7);
c65b1445 2398 arr[0] = pcode;
23183910
DG
2399 if (0 == subpcode) {
2400 switch (pcode) {
2401 case 0x0: /* Supported log pages log page */
2402 n = 4;
2403 arr[n++] = 0x0; /* this page */
2404 arr[n++] = 0xd; /* Temperature */
2405 arr[n++] = 0x2f; /* Informational exceptions */
2406 arr[3] = n - 4;
2407 break;
2408 case 0xd: /* Temperature log page */
2409 arr[3] = resp_temp_l_pg(arr + 4);
2410 break;
2411 case 0x2f: /* Informational exceptions log page */
2412 arr[3] = resp_ie_l_pg(arr + 4);
2413 break;
2414 default:
22017ed2 2415 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
23183910
DG
2416 return check_condition_result;
2417 }
2418 } else if (0xff == subpcode) {
2419 arr[0] |= 0x40;
2420 arr[1] = subpcode;
2421 switch (pcode) {
2422 case 0x0: /* Supported log pages and subpages log page */
2423 n = 4;
2424 arr[n++] = 0x0;
2425 arr[n++] = 0x0; /* 0,0 page */
2426 arr[n++] = 0x0;
2427 arr[n++] = 0xff; /* this page */
2428 arr[n++] = 0xd;
2429 arr[n++] = 0x0; /* Temperature */
2430 arr[n++] = 0x2f;
2431 arr[n++] = 0x0; /* Informational exceptions */
2432 arr[3] = n - 4;
2433 break;
2434 case 0xd: /* Temperature subpages */
2435 n = 4;
2436 arr[n++] = 0xd;
2437 arr[n++] = 0x0; /* Temperature */
2438 arr[3] = n - 4;
2439 break;
2440 case 0x2f: /* Informational exceptions subpages */
2441 n = 4;
2442 arr[n++] = 0x2f;
2443 arr[n++] = 0x0; /* Informational exceptions */
2444 arr[3] = n - 4;
2445 break;
2446 default:
22017ed2 2447 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
23183910
DG
2448 return check_condition_result;
2449 }
2450 } else {
22017ed2 2451 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
c65b1445
DG
2452 return check_condition_result;
2453 }
773642d9 2454 len = min(get_unaligned_be16(arr + 2) + 4, alloc_len);
c65b1445
DG
2455 return fill_from_dev_buffer(scp, arr,
2456 min(len, SDEBUG_MAX_INQ_ARR_SZ));
2457}
2458
9447b6ce
MP
2459static inline int check_device_access_params(struct scsi_cmnd *scp,
2460 unsigned long long lba, unsigned int num, bool write)
1da177e4 2461{
c65b1445 2462 if (lba + num > sdebug_capacity) {
22017ed2 2463 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
1da177e4
LT
2464 return check_condition_result;
2465 }
c65b1445
DG
2466 /* transfer length excessive (tie in to block limits VPD page) */
2467 if (num > sdebug_store_sectors) {
22017ed2 2468 /* needs work to find which cdb byte 'num' comes from */
cbf67842 2469 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
c65b1445
DG
2470 return check_condition_result;
2471 }
9447b6ce
MP
2472 if (write && unlikely(sdebug_wp)) {
2473 mk_sense_buffer(scp, DATA_PROTECT, WRITE_PROTECTED, 0x2);
2474 return check_condition_result;
2475 }
19789100
FT
2476 return 0;
2477}
2478
a4517511 2479/* Returns number of bytes copied or -1 if error. */
0a7e69c7
DG
2480static int do_device_access(struct scsi_cmnd *scmd, u32 sg_skip, u64 lba,
2481 u32 num, bool do_write)
19789100
FT
2482{
2483 int ret;
c2248fc9 2484 u64 block, rest = 0;
ae3d56d8 2485 struct scsi_data_buffer *sdb = &scmd->sdb;
a4517511 2486 enum dma_data_direction dir;
a4517511 2487
c2248fc9 2488 if (do_write) {
a4517511 2489 dir = DMA_TO_DEVICE;
4f2c8bf6 2490 write_since_sync = true;
a4517511 2491 } else {
a4517511 2492 dir = DMA_FROM_DEVICE;
a4517511 2493 }
19789100 2494
a4517511
AM
2495 if (!sdb->length)
2496 return 0;
ae3d56d8 2497 if (scmd->sc_data_direction != dir)
a4517511 2498 return -1;
19789100
FT
2499
2500 block = do_div(lba, sdebug_store_sectors);
2501 if (block + num > sdebug_store_sectors)
2502 rest = block + num - sdebug_store_sectors;
2503
386ecb12 2504 ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
773642d9 2505 fake_storep + (block * sdebug_sector_size),
0a7e69c7 2506 (num - rest) * sdebug_sector_size, sg_skip, do_write);
773642d9 2507 if (ret != (num - rest) * sdebug_sector_size)
a4517511
AM
2508 return ret;
2509
2510 if (rest) {
386ecb12 2511 ret += sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
773642d9 2512 fake_storep, rest * sdebug_sector_size,
0a7e69c7
DG
2513 sg_skip + ((num - rest) * sdebug_sector_size),
2514 do_write);
a4517511 2515 }
19789100
FT
2516
2517 return ret;
2518}
2519
40d07b52
DG
2520/* If lba2fake_store(lba,num) compares equal to arr(num), then copy top half of
2521 * arr into lba2fake_store(lba,num) and return true. If comparison fails then
38d5c833 2522 * return false. */
fd32119b 2523static bool comp_write_worker(u64 lba, u32 num, const u8 *arr)
38d5c833
DG
2524{
2525 bool res;
2526 u64 block, rest = 0;
2527 u32 store_blks = sdebug_store_sectors;
773642d9 2528 u32 lb_size = sdebug_sector_size;
38d5c833
DG
2529
2530 block = do_div(lba, store_blks);
2531 if (block + num > store_blks)
2532 rest = block + num - store_blks;
2533
2534 res = !memcmp(fake_storep + (block * lb_size), arr,
2535 (num - rest) * lb_size);
2536 if (!res)
2537 return res;
2538 if (rest)
2539 res = memcmp(fake_storep, arr + ((num - rest) * lb_size),
2540 rest * lb_size);
2541 if (!res)
2542 return res;
2543 arr += num * lb_size;
2544 memcpy(fake_storep + (block * lb_size), arr, (num - rest) * lb_size);
2545 if (rest)
2546 memcpy(fake_storep, arr + ((num - rest) * lb_size),
2547 rest * lb_size);
2548 return res;
2549}
2550
51d648af 2551static __be16 dif_compute_csum(const void *buf, int len)
beb40ea4 2552{
51d648af 2553 __be16 csum;
beb40ea4 2554
773642d9 2555 if (sdebug_guard)
51d648af
AM
2556 csum = (__force __be16)ip_compute_csum(buf, len);
2557 else
beb40ea4 2558 csum = cpu_to_be16(crc_t10dif(buf, len));
51d648af 2559
beb40ea4
AM
2560 return csum;
2561}
2562
6ebf105c 2563static int dif_verify(struct t10_pi_tuple *sdt, const void *data,
beb40ea4
AM
2564 sector_t sector, u32 ei_lba)
2565{
773642d9 2566 __be16 csum = dif_compute_csum(data, sdebug_sector_size);
beb40ea4
AM
2567
2568 if (sdt->guard_tag != csum) {
c1287970 2569 pr_err("GUARD check failed on sector %lu rcvd 0x%04x, data 0x%04x\n",
beb40ea4
AM
2570 (unsigned long)sector,
2571 be16_to_cpu(sdt->guard_tag),
2572 be16_to_cpu(csum));
2573 return 0x01;
2574 }
8475c811 2575 if (sdebug_dif == T10_PI_TYPE1_PROTECTION &&
beb40ea4 2576 be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
c1287970
TW
2577 pr_err("REF check failed on sector %lu\n",
2578 (unsigned long)sector);
beb40ea4
AM
2579 return 0x03;
2580 }
8475c811 2581 if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
beb40ea4 2582 be32_to_cpu(sdt->ref_tag) != ei_lba) {
c1287970
TW
2583 pr_err("REF check failed on sector %lu\n",
2584 (unsigned long)sector);
beb40ea4
AM
2585 return 0x03;
2586 }
2587 return 0;
2588}
2589
bb8c063c 2590static void dif_copy_prot(struct scsi_cmnd *SCpnt, sector_t sector,
65f72f2a 2591 unsigned int sectors, bool read)
c6a44287 2592{
be4e11be 2593 size_t resid;
c6a44287 2594 void *paddr;
14faa944 2595 const void *dif_store_end = dif_storep + sdebug_store_sectors;
be4e11be 2596 struct sg_mapping_iter miter;
c6a44287 2597
e18d8bea
AM
2598 /* Bytes of protection data to copy into sgl */
2599 resid = sectors * sizeof(*dif_storep);
c6a44287 2600
be4e11be
AM
2601 sg_miter_start(&miter, scsi_prot_sglist(SCpnt),
2602 scsi_prot_sg_count(SCpnt), SG_MITER_ATOMIC |
2603 (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG));
2604
2605 while (sg_miter_next(&miter) && resid > 0) {
2606 size_t len = min(miter.length, resid);
14faa944 2607 void *start = dif_store(sector);
be4e11be 2608 size_t rest = 0;
14faa944
AM
2609
2610 if (dif_store_end < start + len)
2611 rest = start + len - dif_store_end;
c6a44287 2612
be4e11be 2613 paddr = miter.addr;
14faa944 2614
65f72f2a
AM
2615 if (read)
2616 memcpy(paddr, start, len - rest);
2617 else
2618 memcpy(start, paddr, len - rest);
2619
2620 if (rest) {
2621 if (read)
2622 memcpy(paddr + len - rest, dif_storep, rest);
2623 else
2624 memcpy(dif_storep, paddr + len - rest, rest);
2625 }
c6a44287 2626
e18d8bea 2627 sector += len / sizeof(*dif_storep);
c6a44287 2628 resid -= len;
c6a44287 2629 }
be4e11be 2630 sg_miter_stop(&miter);
bb8c063c
AM
2631}
2632
2633static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec,
2634 unsigned int sectors, u32 ei_lba)
2635{
2636 unsigned int i;
6ebf105c 2637 struct t10_pi_tuple *sdt;
bb8c063c
AM
2638 sector_t sector;
2639
c45eabec 2640 for (i = 0; i < sectors; i++, ei_lba++) {
bb8c063c
AM
2641 int ret;
2642
2643 sector = start_sec + i;
2644 sdt = dif_store(sector);
2645
51d648af 2646 if (sdt->app_tag == cpu_to_be16(0xffff))
bb8c063c
AM
2647 continue;
2648
40d07b52 2649 ret = dif_verify(sdt, lba2fake_store(sector), sector, ei_lba);
bb8c063c
AM
2650 if (ret) {
2651 dif_errors++;
2652 return ret;
2653 }
bb8c063c 2654 }
c6a44287 2655
65f72f2a 2656 dif_copy_prot(SCpnt, start_sec, sectors, true);
c6a44287
MP
2657 dix_reads++;
2658
2659 return 0;
2660}
2661
fd32119b 2662static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
19789100 2663{
c2248fc9 2664 u8 *cmd = scp->cmnd;
c4837394 2665 struct sdebug_queued_cmd *sqcp;
c2248fc9
DG
2666 u64 lba;
2667 u32 num;
2668 u32 ei_lba;
19789100
FT
2669 unsigned long iflags;
2670 int ret;
c2248fc9 2671 bool check_prot;
19789100 2672
c2248fc9
DG
2673 switch (cmd[0]) {
2674 case READ_16:
2675 ei_lba = 0;
2676 lba = get_unaligned_be64(cmd + 2);
2677 num = get_unaligned_be32(cmd + 10);
2678 check_prot = true;
2679 break;
2680 case READ_10:
2681 ei_lba = 0;
2682 lba = get_unaligned_be32(cmd + 2);
2683 num = get_unaligned_be16(cmd + 7);
2684 check_prot = true;
2685 break;
2686 case READ_6:
2687 ei_lba = 0;
2688 lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
2689 (u32)(cmd[1] & 0x1f) << 16;
2690 num = (0 == cmd[4]) ? 256 : cmd[4];
2691 check_prot = true;
2692 break;
2693 case READ_12:
2694 ei_lba = 0;
2695 lba = get_unaligned_be32(cmd + 2);
2696 num = get_unaligned_be32(cmd + 6);
2697 check_prot = true;
2698 break;
2699 case XDWRITEREAD_10:
2700 ei_lba = 0;
2701 lba = get_unaligned_be32(cmd + 2);
2702 num = get_unaligned_be16(cmd + 7);
2703 check_prot = false;
2704 break;
2705 default: /* assume READ(32) */
2706 lba = get_unaligned_be64(cmd + 12);
2707 ei_lba = get_unaligned_be32(cmd + 20);
2708 num = get_unaligned_be32(cmd + 28);
2709 check_prot = false;
2710 break;
2711 }
f46eb0e9 2712 if (unlikely(have_dif_prot && check_prot)) {
8475c811 2713 if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
c2248fc9
DG
2714 (cmd[1] & 0xe0)) {
2715 mk_sense_invalid_opcode(scp);
2716 return check_condition_result;
2717 }
8475c811
CH
2718 if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
2719 sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
c2248fc9
DG
2720 (cmd[1] & 0xe0) == 0)
2721 sdev_printk(KERN_ERR, scp->device, "Unprotected RD "
2722 "to DIF device\n");
2723 }
f46eb0e9 2724 if (unlikely(sdebug_any_injecting_opt)) {
c4837394 2725 sqcp = (struct sdebug_queued_cmd *)scp->host_scribble;
c2248fc9 2726
c4837394
DG
2727 if (sqcp) {
2728 if (sqcp->inj_short)
2729 num /= 2;
2730 }
2731 } else
2732 sqcp = NULL;
c2248fc9 2733
9447b6ce
MP
2734 ret = check_device_access_params(scp, lba, num, false);
2735 if (ret)
2736 return ret;
f46eb0e9 2737 if (unlikely((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) &&
d9da891a
LO
2738 (lba <= (sdebug_medium_error_start + sdebug_medium_error_count - 1)) &&
2739 ((lba + num) > sdebug_medium_error_start))) {
c65b1445 2740 /* claim unrecoverable read error */
c2248fc9 2741 mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0);
c65b1445 2742 /* set info field and valid bit for fixed descriptor */
c2248fc9
DG
2743 if (0x70 == (scp->sense_buffer[0] & 0x7f)) {
2744 scp->sense_buffer[0] |= 0x80; /* Valid bit */
32f7ef73
DG
2745 ret = (lba < OPT_MEDIUM_ERR_ADDR)
2746 ? OPT_MEDIUM_ERR_ADDR : (int)lba;
c2248fc9 2747 put_unaligned_be32(ret, scp->sense_buffer + 3);
c65b1445 2748 }
c2248fc9 2749 scsi_set_resid(scp, scsi_bufflen(scp));
1da177e4
LT
2750 return check_condition_result;
2751 }
c6a44287 2752
6c78cc06
AM
2753 read_lock_irqsave(&atomic_rw, iflags);
2754
c6a44287 2755 /* DIX + T10 DIF */
f46eb0e9 2756 if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
c2248fc9 2757 int prot_ret = prot_verify_read(scp, lba, num, ei_lba);
c6a44287
MP
2758
2759 if (prot_ret) {
6c78cc06 2760 read_unlock_irqrestore(&atomic_rw, iflags);
c2248fc9 2761 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret);
c6a44287
MP
2762 return illegal_condition_result;
2763 }
2764 }
2765
0a7e69c7 2766 ret = do_device_access(scp, 0, lba, num, false);
1da177e4 2767 read_unlock_irqrestore(&atomic_rw, iflags);
f46eb0e9 2768 if (unlikely(ret == -1))
a4517511
AM
2769 return DID_ERROR << 16;
2770
42d387be 2771 scsi_set_resid(scp, scsi_bufflen(scp) - ret);
a4517511 2772
c4837394
DG
2773 if (unlikely(sqcp)) {
2774 if (sqcp->inj_recovered) {
c2248fc9
DG
2775 mk_sense_buffer(scp, RECOVERED_ERROR,
2776 THRESHOLD_EXCEEDED, 0);
2777 return check_condition_result;
c4837394 2778 } else if (sqcp->inj_transport) {
c2248fc9
DG
2779 mk_sense_buffer(scp, ABORTED_COMMAND,
2780 TRANSPORT_PROBLEM, ACK_NAK_TO);
2781 return check_condition_result;
c4837394 2782 } else if (sqcp->inj_dif) {
c2248fc9
DG
2783 /* Logical block guard check failed */
2784 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
2785 return illegal_condition_result;
c4837394 2786 } else if (sqcp->inj_dix) {
c2248fc9
DG
2787 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
2788 return illegal_condition_result;
2789 }
2790 }
a4517511 2791 return 0;
1da177e4
LT
2792}
2793
58a8635d 2794static void dump_sector(unsigned char *buf, int len)
c6a44287 2795{
cbf67842 2796 int i, j, n;
c6a44287 2797
cbf67842 2798 pr_err(">>> Sector Dump <<<\n");
c6a44287 2799 for (i = 0 ; i < len ; i += 16) {
cbf67842 2800 char b[128];
c6a44287 2801
cbf67842 2802 for (j = 0, n = 0; j < 16; j++) {
c6a44287 2803 unsigned char c = buf[i+j];
cbf67842 2804
c6a44287 2805 if (c >= 0x20 && c < 0x7e)
cbf67842
DG
2806 n += scnprintf(b + n, sizeof(b) - n,
2807 " %c ", buf[i+j]);
c6a44287 2808 else
cbf67842
DG
2809 n += scnprintf(b + n, sizeof(b) - n,
2810 "%02x ", buf[i+j]);
c6a44287 2811 }
cbf67842 2812 pr_err("%04d: %s\n", i, b);
c6a44287
MP
2813 }
2814}
2815
2816static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec,
395cef03 2817 unsigned int sectors, u32 ei_lba)
c6a44287 2818{
be4e11be 2819 int ret;
6ebf105c 2820 struct t10_pi_tuple *sdt;
be4e11be 2821 void *daddr;
65f72f2a 2822 sector_t sector = start_sec;
c6a44287 2823 int ppage_offset;
be4e11be
AM
2824 int dpage_offset;
2825 struct sg_mapping_iter diter;
2826 struct sg_mapping_iter piter;
c6a44287 2827
c6a44287
MP
2828 BUG_ON(scsi_sg_count(SCpnt) == 0);
2829 BUG_ON(scsi_prot_sg_count(SCpnt) == 0);
2830
be4e11be
AM
2831 sg_miter_start(&piter, scsi_prot_sglist(SCpnt),
2832 scsi_prot_sg_count(SCpnt),
2833 SG_MITER_ATOMIC | SG_MITER_FROM_SG);
2834 sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt),
2835 SG_MITER_ATOMIC | SG_MITER_FROM_SG);
2836
2837 /* For each protection page */
2838 while (sg_miter_next(&piter)) {
2839 dpage_offset = 0;
2840 if (WARN_ON(!sg_miter_next(&diter))) {
2841 ret = 0x01;
2842 goto out;
2843 }
c6a44287 2844
be4e11be 2845 for (ppage_offset = 0; ppage_offset < piter.length;
6ebf105c 2846 ppage_offset += sizeof(struct t10_pi_tuple)) {
c6a44287 2847 /* If we're at the end of the current
be4e11be 2848 * data page advance to the next one
c6a44287 2849 */
be4e11be
AM
2850 if (dpage_offset >= diter.length) {
2851 if (WARN_ON(!sg_miter_next(&diter))) {
2852 ret = 0x01;
2853 goto out;
2854 }
2855 dpage_offset = 0;
c6a44287
MP
2856 }
2857
be4e11be
AM
2858 sdt = piter.addr + ppage_offset;
2859 daddr = diter.addr + dpage_offset;
c6a44287 2860
be4e11be 2861 ret = dif_verify(sdt, daddr, sector, ei_lba);
beb40ea4 2862 if (ret) {
773642d9 2863 dump_sector(daddr, sdebug_sector_size);
395cef03
MP
2864 goto out;
2865 }
2866
c6a44287 2867 sector++;
395cef03 2868 ei_lba++;
773642d9 2869 dpage_offset += sdebug_sector_size;
c6a44287 2870 }
be4e11be
AM
2871 diter.consumed = dpage_offset;
2872 sg_miter_stop(&diter);
c6a44287 2873 }
be4e11be 2874 sg_miter_stop(&piter);
c6a44287 2875
65f72f2a 2876 dif_copy_prot(SCpnt, start_sec, sectors, false);
c6a44287
MP
2877 dix_writes++;
2878
2879 return 0;
2880
2881out:
2882 dif_errors++;
be4e11be
AM
2883 sg_miter_stop(&diter);
2884 sg_miter_stop(&piter);
c6a44287
MP
2885 return ret;
2886}
2887
b90ebc3d
AM
2888static unsigned long lba_to_map_index(sector_t lba)
2889{
773642d9
DG
2890 if (sdebug_unmap_alignment)
2891 lba += sdebug_unmap_granularity - sdebug_unmap_alignment;
2892 sector_div(lba, sdebug_unmap_granularity);
b90ebc3d
AM
2893 return lba;
2894}
2895
2896static sector_t map_index_to_lba(unsigned long index)
44d92694 2897{
773642d9 2898 sector_t lba = index * sdebug_unmap_granularity;
a027b5b9 2899
773642d9
DG
2900 if (sdebug_unmap_alignment)
2901 lba -= sdebug_unmap_granularity - sdebug_unmap_alignment;
a027b5b9 2902 return lba;
b90ebc3d 2903}
44d92694 2904
b90ebc3d
AM
2905static unsigned int map_state(sector_t lba, unsigned int *num)
2906{
2907 sector_t end;
2908 unsigned int mapped;
2909 unsigned long index;
2910 unsigned long next;
44d92694 2911
b90ebc3d
AM
2912 index = lba_to_map_index(lba);
2913 mapped = test_bit(index, map_storep);
44d92694
MP
2914
2915 if (mapped)
b90ebc3d 2916 next = find_next_zero_bit(map_storep, map_size, index);
44d92694 2917 else
b90ebc3d 2918 next = find_next_bit(map_storep, map_size, index);
44d92694 2919
b90ebc3d 2920 end = min_t(sector_t, sdebug_store_sectors, map_index_to_lba(next));
44d92694 2921 *num = end - lba;
44d92694
MP
2922 return mapped;
2923}
2924
2925static void map_region(sector_t lba, unsigned int len)
2926{
44d92694
MP
2927 sector_t end = lba + len;
2928
44d92694 2929 while (lba < end) {
b90ebc3d 2930 unsigned long index = lba_to_map_index(lba);
44d92694 2931
b90ebc3d
AM
2932 if (index < map_size)
2933 set_bit(index, map_storep);
44d92694 2934
b90ebc3d 2935 lba = map_index_to_lba(index + 1);
44d92694
MP
2936 }
2937}
2938
2939static void unmap_region(sector_t lba, unsigned int len)
2940{
44d92694
MP
2941 sector_t end = lba + len;
2942
44d92694 2943 while (lba < end) {
b90ebc3d 2944 unsigned long index = lba_to_map_index(lba);
44d92694 2945
b90ebc3d 2946 if (lba == map_index_to_lba(index) &&
773642d9 2947 lba + sdebug_unmap_granularity <= end &&
b90ebc3d
AM
2948 index < map_size) {
2949 clear_bit(index, map_storep);
760f3b03 2950 if (sdebug_lbprz) { /* for LBPRZ=2 return 0xff_s */
be1dd78d 2951 memset(fake_storep +
760f3b03
DG
2952 lba * sdebug_sector_size,
2953 (sdebug_lbprz & 1) ? 0 : 0xff,
773642d9
DG
2954 sdebug_sector_size *
2955 sdebug_unmap_granularity);
b90ebc3d 2956 }
e9926b43
AM
2957 if (dif_storep) {
2958 memset(dif_storep + lba, 0xff,
2959 sizeof(*dif_storep) *
773642d9 2960 sdebug_unmap_granularity);
e9926b43 2961 }
be1dd78d 2962 }
b90ebc3d 2963 lba = map_index_to_lba(index + 1);
44d92694
MP
2964 }
2965}
2966
fd32119b 2967static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
1da177e4 2968{
c2248fc9
DG
2969 u8 *cmd = scp->cmnd;
2970 u64 lba;
2971 u32 num;
2972 u32 ei_lba;
1da177e4 2973 unsigned long iflags;
19789100 2974 int ret;
c2248fc9 2975 bool check_prot;
1da177e4 2976
c2248fc9
DG
2977 switch (cmd[0]) {
2978 case WRITE_16:
2979 ei_lba = 0;
2980 lba = get_unaligned_be64(cmd + 2);
2981 num = get_unaligned_be32(cmd + 10);
2982 check_prot = true;
2983 break;
2984 case WRITE_10:
2985 ei_lba = 0;
2986 lba = get_unaligned_be32(cmd + 2);
2987 num = get_unaligned_be16(cmd + 7);
2988 check_prot = true;
2989 break;
2990 case WRITE_6:
2991 ei_lba = 0;
2992 lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
2993 (u32)(cmd[1] & 0x1f) << 16;
2994 num = (0 == cmd[4]) ? 256 : cmd[4];
2995 check_prot = true;
2996 break;
2997 case WRITE_12:
2998 ei_lba = 0;
2999 lba = get_unaligned_be32(cmd + 2);
3000 num = get_unaligned_be32(cmd + 6);
3001 check_prot = true;
3002 break;
3003 case 0x53: /* XDWRITEREAD(10) */
3004 ei_lba = 0;
3005 lba = get_unaligned_be32(cmd + 2);
3006 num = get_unaligned_be16(cmd + 7);
3007 check_prot = false;
3008 break;
3009 default: /* assume WRITE(32) */
3010 lba = get_unaligned_be64(cmd + 12);
3011 ei_lba = get_unaligned_be32(cmd + 20);
3012 num = get_unaligned_be32(cmd + 28);
3013 check_prot = false;
3014 break;
3015 }
f46eb0e9 3016 if (unlikely(have_dif_prot && check_prot)) {
8475c811 3017 if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
c2248fc9
DG
3018 (cmd[1] & 0xe0)) {
3019 mk_sense_invalid_opcode(scp);
3020 return check_condition_result;
3021 }
8475c811
CH
3022 if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
3023 sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
c2248fc9
DG
3024 (cmd[1] & 0xe0) == 0)
3025 sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
3026 "to DIF device\n");
3027 }
9447b6ce
MP
3028 ret = check_device_access_params(scp, lba, num, true);
3029 if (ret)
3030 return ret;
6c78cc06
AM
3031 write_lock_irqsave(&atomic_rw, iflags);
3032
c6a44287 3033 /* DIX + T10 DIF */
f46eb0e9 3034 if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
c2248fc9 3035 int prot_ret = prot_verify_write(scp, lba, num, ei_lba);
c6a44287
MP
3036
3037 if (prot_ret) {
6c78cc06 3038 write_unlock_irqrestore(&atomic_rw, iflags);
c2248fc9 3039 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret);
c6a44287
MP
3040 return illegal_condition_result;
3041 }
3042 }
3043
0a7e69c7 3044 ret = do_device_access(scp, 0, lba, num, true);
f46eb0e9 3045 if (unlikely(scsi_debug_lbp()))
44d92694 3046 map_region(lba, num);
1da177e4 3047 write_unlock_irqrestore(&atomic_rw, iflags);
f46eb0e9 3048 if (unlikely(-1 == ret))
773642d9 3049 return DID_ERROR << 16;
c4837394
DG
3050 else if (unlikely(sdebug_verbose &&
3051 (ret < (num * sdebug_sector_size))))
c2248fc9 3052 sdev_printk(KERN_INFO, scp->device,
cbf67842 3053 "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
773642d9 3054 my_name, num * sdebug_sector_size, ret);
44d92694 3055
f46eb0e9 3056 if (unlikely(sdebug_any_injecting_opt)) {
c4837394
DG
3057 struct sdebug_queued_cmd *sqcp =
3058 (struct sdebug_queued_cmd *)scp->host_scribble;
c2248fc9 3059
c4837394
DG
3060 if (sqcp) {
3061 if (sqcp->inj_recovered) {
3062 mk_sense_buffer(scp, RECOVERED_ERROR,
3063 THRESHOLD_EXCEEDED, 0);
3064 return check_condition_result;
3065 } else if (sqcp->inj_dif) {
3066 /* Logical block guard check failed */
3067 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
3068 return illegal_condition_result;
3069 } else if (sqcp->inj_dix) {
3070 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
3071 return illegal_condition_result;
3072 }
c2248fc9
DG
3073 }
3074 }
44d92694
MP
3075 return 0;
3076}
3077
481b5e5c
DG
3078/*
3079 * T10 has only specified WRITE SCATTERED(16) and WRITE SCATTERED(32).
3080 * No READ GATHERED yet (requires bidi or long cdb holding gather list).
3081 */
3082static int resp_write_scat(struct scsi_cmnd *scp,
3083 struct sdebug_dev_info *devip)
3084{
3085 u8 *cmd = scp->cmnd;
3086 u8 *lrdp = NULL;
3087 u8 *up;
3088 u8 wrprotect;
3089 u16 lbdof, num_lrd, k;
3090 u32 num, num_by, bt_len, lbdof_blen, sg_off, cum_lb;
3091 u32 lb_size = sdebug_sector_size;
3092 u32 ei_lba;
3093 u64 lba;
3094 unsigned long iflags;
3095 int ret, res;
3096 bool is_16;
3097 static const u32 lrd_size = 32; /* + parameter list header size */
3098
3099 if (cmd[0] == VARIABLE_LENGTH_CMD) {
3100 is_16 = false;
3101 wrprotect = (cmd[10] >> 5) & 0x7;
3102 lbdof = get_unaligned_be16(cmd + 12);
3103 num_lrd = get_unaligned_be16(cmd + 16);
3104 bt_len = get_unaligned_be32(cmd + 28);
3105 } else { /* that leaves WRITE SCATTERED(16) */
3106 is_16 = true;
3107 wrprotect = (cmd[2] >> 5) & 0x7;
3108 lbdof = get_unaligned_be16(cmd + 4);
3109 num_lrd = get_unaligned_be16(cmd + 8);
3110 bt_len = get_unaligned_be32(cmd + 10);
3111 if (unlikely(have_dif_prot)) {
3112 if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
3113 wrprotect) {
3114 mk_sense_invalid_opcode(scp);
3115 return illegal_condition_result;
3116 }
3117 if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
3118 sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
3119 wrprotect == 0)
3120 sdev_printk(KERN_ERR, scp->device,
3121 "Unprotected WR to DIF device\n");
3122 }
3123 }
3124 if ((num_lrd == 0) || (bt_len == 0))
3125 return 0; /* T10 says these do-nothings are not errors */
3126 if (lbdof == 0) {
3127 if (sdebug_verbose)
3128 sdev_printk(KERN_INFO, scp->device,
3129 "%s: %s: LB Data Offset field bad\n",
3130 my_name, __func__);
3131 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
3132 return illegal_condition_result;
3133 }
3134 lbdof_blen = lbdof * lb_size;
3135 if ((lrd_size + (num_lrd * lrd_size)) > lbdof_blen) {
3136 if (sdebug_verbose)
3137 sdev_printk(KERN_INFO, scp->device,
3138 "%s: %s: LBA range descriptors don't fit\n",
3139 my_name, __func__);
3140 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
3141 return illegal_condition_result;
3142 }
3143 lrdp = kzalloc(lbdof_blen, GFP_ATOMIC);
3144 if (lrdp == NULL)
3145 return SCSI_MLQUEUE_HOST_BUSY;
3146 if (sdebug_verbose)
3147 sdev_printk(KERN_INFO, scp->device,
3148 "%s: %s: Fetch header+scatter_list, lbdof_blen=%u\n",
3149 my_name, __func__, lbdof_blen);
3150 res = fetch_to_dev_buffer(scp, lrdp, lbdof_blen);
3151 if (res == -1) {
3152 ret = DID_ERROR << 16;
3153 goto err_out;
3154 }
3155
3156 write_lock_irqsave(&atomic_rw, iflags);
3157 sg_off = lbdof_blen;
3158 /* Spec says Buffer xfer Length field in number of LBs in dout */
3159 cum_lb = 0;
3160 for (k = 0, up = lrdp + lrd_size; k < num_lrd; ++k, up += lrd_size) {
3161 lba = get_unaligned_be64(up + 0);
3162 num = get_unaligned_be32(up + 8);
3163 if (sdebug_verbose)
3164 sdev_printk(KERN_INFO, scp->device,
3165 "%s: %s: k=%d LBA=0x%llx num=%u sg_off=%u\n",
3166 my_name, __func__, k, lba, num, sg_off);
3167 if (num == 0)
3168 continue;
9447b6ce 3169 ret = check_device_access_params(scp, lba, num, true);
481b5e5c
DG
3170 if (ret)
3171 goto err_out_unlock;
3172 num_by = num * lb_size;
3173 ei_lba = is_16 ? 0 : get_unaligned_be32(up + 12);
3174
3175 if ((cum_lb + num) > bt_len) {
3176 if (sdebug_verbose)
3177 sdev_printk(KERN_INFO, scp->device,
3178 "%s: %s: sum of blocks > data provided\n",
3179 my_name, __func__);
3180 mk_sense_buffer(scp, ILLEGAL_REQUEST, WRITE_ERROR_ASC,
3181 0);
3182 ret = illegal_condition_result;
3183 goto err_out_unlock;
3184 }
3185
3186 /* DIX + T10 DIF */
3187 if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
3188 int prot_ret = prot_verify_write(scp, lba, num,
3189 ei_lba);
3190
3191 if (prot_ret) {
3192 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10,
3193 prot_ret);
3194 ret = illegal_condition_result;
3195 goto err_out_unlock;
3196 }
3197 }
3198
3199 ret = do_device_access(scp, sg_off, lba, num, true);
3200 if (unlikely(scsi_debug_lbp()))
3201 map_region(lba, num);
3202 if (unlikely(-1 == ret)) {
3203 ret = DID_ERROR << 16;
3204 goto err_out_unlock;
3205 } else if (unlikely(sdebug_verbose && (ret < num_by)))
3206 sdev_printk(KERN_INFO, scp->device,
3207 "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
3208 my_name, num_by, ret);
3209
3210 if (unlikely(sdebug_any_injecting_opt)) {
3211 struct sdebug_queued_cmd *sqcp =
3212 (struct sdebug_queued_cmd *)scp->host_scribble;
3213
3214 if (sqcp) {
3215 if (sqcp->inj_recovered) {
3216 mk_sense_buffer(scp, RECOVERED_ERROR,
3217 THRESHOLD_EXCEEDED, 0);
3218 ret = illegal_condition_result;
3219 goto err_out_unlock;
3220 } else if (sqcp->inj_dif) {
3221 /* Logical block guard check failed */
3222 mk_sense_buffer(scp, ABORTED_COMMAND,
3223 0x10, 1);
3224 ret = illegal_condition_result;
3225 goto err_out_unlock;
3226 } else if (sqcp->inj_dix) {
3227 mk_sense_buffer(scp, ILLEGAL_REQUEST,
3228 0x10, 1);
3229 ret = illegal_condition_result;
3230 goto err_out_unlock;
3231 }
3232 }
3233 }
3234 sg_off += num_by;
3235 cum_lb += num;
3236 }
3237 ret = 0;
3238err_out_unlock:
3239 write_unlock_irqrestore(&atomic_rw, iflags);
3240err_out:
3241 kfree(lrdp);
3242 return ret;
3243}
3244
fd32119b
DG
3245static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,
3246 u32 ei_lba, bool unmap, bool ndob)
44d92694 3247{
40d07b52 3248 int ret;
44d92694
MP
3249 unsigned long iflags;
3250 unsigned long long i;
40d07b52
DG
3251 u32 lb_size = sdebug_sector_size;
3252 u64 block, lbaa;
3253 u8 *fs1p;
44d92694 3254
9447b6ce 3255 ret = check_device_access_params(scp, lba, num, true);
44d92694
MP
3256 if (ret)
3257 return ret;
3258
3259 write_lock_irqsave(&atomic_rw, iflags);
3260
9ed8d3dc 3261 if (unmap && scsi_debug_lbp()) {
44d92694
MP
3262 unmap_region(lba, num);
3263 goto out;
3264 }
40d07b52
DG
3265 lbaa = lba;
3266 block = do_div(lbaa, sdebug_store_sectors);
c2248fc9 3267 /* if ndob then zero 1 logical block, else fetch 1 logical block */
40d07b52 3268 fs1p = fake_storep + (block * lb_size);
c2248fc9 3269 if (ndob) {
40d07b52 3270 memset(fs1p, 0, lb_size);
c2248fc9
DG
3271 ret = 0;
3272 } else
40d07b52 3273 ret = fetch_to_dev_buffer(scp, fs1p, lb_size);
44d92694
MP
3274
3275 if (-1 == ret) {
3276 write_unlock_irqrestore(&atomic_rw, iflags);
773642d9 3277 return DID_ERROR << 16;
40d07b52 3278 } else if (sdebug_verbose && !ndob && (ret < lb_size))
c2248fc9 3279 sdev_printk(KERN_INFO, scp->device,
e33d7c56 3280 "%s: %s: lb size=%u, IO sent=%d bytes\n",
40d07b52 3281 my_name, "write same", lb_size, ret);
44d92694
MP
3282
3283 /* Copy first sector to remaining blocks */
40d07b52
DG
3284 for (i = 1 ; i < num ; i++) {
3285 lbaa = lba + i;
3286 block = do_div(lbaa, sdebug_store_sectors);
3287 memmove(fake_storep + (block * lb_size), fs1p, lb_size);
3288 }
9ed8d3dc 3289 if (scsi_debug_lbp())
44d92694
MP
3290 map_region(lba, num);
3291out:
3292 write_unlock_irqrestore(&atomic_rw, iflags);
3293
1da177e4
LT
3294 return 0;
3295}
3296
fd32119b
DG
3297static int resp_write_same_10(struct scsi_cmnd *scp,
3298 struct sdebug_dev_info *devip)
c2248fc9
DG
3299{
3300 u8 *cmd = scp->cmnd;
3301 u32 lba;
3302 u16 num;
3303 u32 ei_lba = 0;
3304 bool unmap = false;
3305
3306 if (cmd[1] & 0x8) {
773642d9 3307 if (sdebug_lbpws10 == 0) {
c2248fc9
DG
3308 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
3309 return check_condition_result;
3310 } else
3311 unmap = true;
3312 }
3313 lba = get_unaligned_be32(cmd + 2);
3314 num = get_unaligned_be16(cmd + 7);
773642d9 3315 if (num > sdebug_write_same_length) {
c2248fc9
DG
3316 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
3317 return check_condition_result;
3318 }
3319 return resp_write_same(scp, lba, num, ei_lba, unmap, false);
3320}
3321
fd32119b
DG
3322static int resp_write_same_16(struct scsi_cmnd *scp,
3323 struct sdebug_dev_info *devip)
c2248fc9
DG
3324{
3325 u8 *cmd = scp->cmnd;
3326 u64 lba;
3327 u32 num;
3328 u32 ei_lba = 0;
3329 bool unmap = false;
3330 bool ndob = false;
3331
3332 if (cmd[1] & 0x8) { /* UNMAP */
773642d9 3333 if (sdebug_lbpws == 0) {
c2248fc9
DG
3334 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
3335 return check_condition_result;
3336 } else
3337 unmap = true;
3338 }
3339 if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */
3340 ndob = true;
3341 lba = get_unaligned_be64(cmd + 2);
3342 num = get_unaligned_be32(cmd + 10);
773642d9 3343 if (num > sdebug_write_same_length) {
c2248fc9
DG
3344 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1);
3345 return check_condition_result;
3346 }
3347 return resp_write_same(scp, lba, num, ei_lba, unmap, ndob);
3348}
3349
acafd0b9
EM
3350/* Note the mode field is in the same position as the (lower) service action
3351 * field. For the Report supported operation codes command, SPC-4 suggests
3352 * each mode of this command should be reported separately; for future. */
fd32119b
DG
3353static int resp_write_buffer(struct scsi_cmnd *scp,
3354 struct sdebug_dev_info *devip)
acafd0b9
EM
3355{
3356 u8 *cmd = scp->cmnd;
3357 struct scsi_device *sdp = scp->device;
3358 struct sdebug_dev_info *dp;
3359 u8 mode;
3360
3361 mode = cmd[1] & 0x1f;
3362 switch (mode) {
3363 case 0x4: /* download microcode (MC) and activate (ACT) */
3364 /* set UAs on this device only */
3365 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
3366 set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm);
3367 break;
3368 case 0x5: /* download MC, save and ACT */
3369 set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm);
3370 break;
3371 case 0x6: /* download MC with offsets and ACT */
3372 /* set UAs on most devices (LUs) in this target */
3373 list_for_each_entry(dp,
3374 &devip->sdbg_host->dev_info_list,
3375 dev_list)
3376 if (dp->target == sdp->id) {
3377 set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm);
3378 if (devip != dp)
3379 set_bit(SDEBUG_UA_MICROCODE_CHANGED,
3380 dp->uas_bm);
3381 }
3382 break;
3383 case 0x7: /* download MC with offsets, save, and ACT */
3384 /* set UA on all devices (LUs) in this target */
3385 list_for_each_entry(dp,
3386 &devip->sdbg_host->dev_info_list,
3387 dev_list)
3388 if (dp->target == sdp->id)
3389 set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET,
3390 dp->uas_bm);
3391 break;
3392 default:
3393 /* do nothing for this command for other mode values */
3394 break;
3395 }
3396 return 0;
3397}
3398
fd32119b
DG
3399static int resp_comp_write(struct scsi_cmnd *scp,
3400 struct sdebug_dev_info *devip)
38d5c833
DG
3401{
3402 u8 *cmd = scp->cmnd;
3403 u8 *arr;
3404 u8 *fake_storep_hold;
3405 u64 lba;
3406 u32 dnum;
773642d9 3407 u32 lb_size = sdebug_sector_size;
38d5c833
DG
3408 u8 num;
3409 unsigned long iflags;
3410 int ret;
d467d31f 3411 int retval = 0;
38d5c833 3412
d467d31f 3413 lba = get_unaligned_be64(cmd + 2);
38d5c833
DG
3414 num = cmd[13]; /* 1 to a maximum of 255 logical blocks */
3415 if (0 == num)
3416 return 0; /* degenerate case, not an error */
8475c811 3417 if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
38d5c833
DG
3418 (cmd[1] & 0xe0)) {
3419 mk_sense_invalid_opcode(scp);
3420 return check_condition_result;
3421 }
8475c811
CH
3422 if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
3423 sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
38d5c833
DG
3424 (cmd[1] & 0xe0) == 0)
3425 sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
3426 "to DIF device\n");
9447b6ce
MP
3427 ret = check_device_access_params(scp, lba, num, false);
3428 if (ret)
3429 return ret;
d467d31f 3430 dnum = 2 * num;
6396bb22 3431 arr = kcalloc(lb_size, dnum, GFP_ATOMIC);
d467d31f
DG
3432 if (NULL == arr) {
3433 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
3434 INSUFF_RES_ASCQ);
3435 return check_condition_result;
3436 }
38d5c833
DG
3437
3438 write_lock_irqsave(&atomic_rw, iflags);
3439
3440 /* trick do_device_access() to fetch both compare and write buffers
3441 * from data-in into arr. Safe (atomic) since write_lock held. */
3442 fake_storep_hold = fake_storep;
3443 fake_storep = arr;
0a7e69c7 3444 ret = do_device_access(scp, 0, 0, dnum, true);
38d5c833
DG
3445 fake_storep = fake_storep_hold;
3446 if (ret == -1) {
d467d31f
DG
3447 retval = DID_ERROR << 16;
3448 goto cleanup;
773642d9 3449 } else if (sdebug_verbose && (ret < (dnum * lb_size)))
38d5c833
DG
3450 sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb "
3451 "indicated=%u, IO sent=%d bytes\n", my_name,
3452 dnum * lb_size, ret);
3453 if (!comp_write_worker(lba, num, arr)) {
38d5c833 3454 mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0);
d467d31f
DG
3455 retval = check_condition_result;
3456 goto cleanup;
38d5c833
DG
3457 }
3458 if (scsi_debug_lbp())
3459 map_region(lba, num);
d467d31f 3460cleanup:
38d5c833 3461 write_unlock_irqrestore(&atomic_rw, iflags);
d467d31f
DG
3462 kfree(arr);
3463 return retval;
38d5c833
DG
3464}
3465
44d92694
MP
3466struct unmap_block_desc {
3467 __be64 lba;
3468 __be32 blocks;
3469 __be32 __reserved;
3470};
3471
fd32119b 3472static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
44d92694
MP
3473{
3474 unsigned char *buf;
3475 struct unmap_block_desc *desc;
3476 unsigned int i, payload_len, descriptors;
3477 int ret;
6c78cc06 3478 unsigned long iflags;
44d92694 3479
44d92694 3480
c2248fc9
DG
3481 if (!scsi_debug_lbp())
3482 return 0; /* fib and say its done */
3483 payload_len = get_unaligned_be16(scp->cmnd + 7);
3484 BUG_ON(scsi_bufflen(scp) != payload_len);
44d92694
MP
3485
3486 descriptors = (payload_len - 8) / 16;
773642d9 3487 if (descriptors > sdebug_unmap_max_desc) {
c2248fc9
DG
3488 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
3489 return check_condition_result;
3490 }
44d92694 3491
b333a819 3492 buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC);
c2248fc9
DG
3493 if (!buf) {
3494 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
3495 INSUFF_RES_ASCQ);
44d92694 3496 return check_condition_result;
c2248fc9 3497 }
44d92694 3498
c2248fc9 3499 scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
44d92694
MP
3500
3501 BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2);
3502 BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16);
3503
3504 desc = (void *)&buf[8];
3505
6c78cc06
AM
3506 write_lock_irqsave(&atomic_rw, iflags);
3507
44d92694
MP
3508 for (i = 0 ; i < descriptors ; i++) {
3509 unsigned long long lba = get_unaligned_be64(&desc[i].lba);
3510 unsigned int num = get_unaligned_be32(&desc[i].blocks);
3511
9447b6ce 3512 ret = check_device_access_params(scp, lba, num, true);
44d92694
MP
3513 if (ret)
3514 goto out;
3515
3516 unmap_region(lba, num);
3517 }
3518
3519 ret = 0;
3520
3521out:
6c78cc06 3522 write_unlock_irqrestore(&atomic_rw, iflags);
44d92694
MP
3523 kfree(buf);
3524
3525 return ret;
3526}
3527
3528#define SDEBUG_GET_LBA_STATUS_LEN 32
3529
fd32119b
DG
3530static int resp_get_lba_status(struct scsi_cmnd *scp,
3531 struct sdebug_dev_info *devip)
44d92694 3532{
c2248fc9
DG
3533 u8 *cmd = scp->cmnd;
3534 u64 lba;
3535 u32 alloc_len, mapped, num;
3536 u8 arr[SDEBUG_GET_LBA_STATUS_LEN];
44d92694
MP
3537 int ret;
3538
c2248fc9
DG
3539 lba = get_unaligned_be64(cmd + 2);
3540 alloc_len = get_unaligned_be32(cmd + 10);
44d92694
MP
3541
3542 if (alloc_len < 24)
3543 return 0;
3544
9447b6ce 3545 ret = check_device_access_params(scp, lba, 1, false);
44d92694
MP
3546 if (ret)
3547 return ret;
3548
c2248fc9
DG
3549 if (scsi_debug_lbp())
3550 mapped = map_state(lba, &num);
3551 else {
3552 mapped = 1;
3553 /* following just in case virtual_gb changed */
3554 sdebug_capacity = get_sdebug_capacity();
3555 if (sdebug_capacity - lba <= 0xffffffff)
3556 num = sdebug_capacity - lba;
3557 else
3558 num = 0xffffffff;
3559 }
44d92694
MP
3560
3561 memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN);
c2248fc9
DG
3562 put_unaligned_be32(20, arr); /* Parameter Data Length */
3563 put_unaligned_be64(lba, arr + 8); /* LBA */
3564 put_unaligned_be32(num, arr + 16); /* Number of blocks */
3565 arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */
44d92694 3566
c2248fc9 3567 return fill_from_dev_buffer(scp, arr, SDEBUG_GET_LBA_STATUS_LEN);
44d92694
MP
3568}
3569
80c49563
DG
3570static int resp_sync_cache(struct scsi_cmnd *scp,
3571 struct sdebug_dev_info *devip)
3572{
4f2c8bf6 3573 int res = 0;
80c49563
DG
3574 u64 lba;
3575 u32 num_blocks;
3576 u8 *cmd = scp->cmnd;
3577
3578 if (cmd[0] == SYNCHRONIZE_CACHE) { /* 10 byte cdb */
3579 lba = get_unaligned_be32(cmd + 2);
3580 num_blocks = get_unaligned_be16(cmd + 7);
3581 } else { /* SYNCHRONIZE_CACHE(16) */
3582 lba = get_unaligned_be64(cmd + 2);
3583 num_blocks = get_unaligned_be32(cmd + 10);
3584 }
3585 if (lba + num_blocks > sdebug_capacity) {
3586 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
3587 return check_condition_result;
3588 }
4f2c8bf6
DG
3589 if (!write_since_sync || cmd[1] & 0x2)
3590 res = SDEG_RES_IMMED_MASK;
3591 else /* delay if write_since_sync and IMMED clear */
3592 write_since_sync = false;
3593 return res;
80c49563
DG
3594}
3595
fb0cc8d1
DG
3596#define RL_BUCKET_ELEMS 8
3597
8d039e22
DG
3598/* Even though each pseudo target has a REPORT LUNS "well known logical unit"
3599 * (W-LUN), the normal Linux scanning logic does not associate it with a
3600 * device (e.g. /dev/sg7). The following magic will make that association:
3601 * "cd /sys/class/scsi_host/host<n> ; echo '- - 49409' > scan"
3602 * where <n> is a host number. If there are multiple targets in a host then
3603 * the above will associate a W-LUN to each target. To only get a W-LUN
3604 * for target 2, then use "echo '- 2 49409' > scan" .
3605 */
3606static int resp_report_luns(struct scsi_cmnd *scp,
3607 struct sdebug_dev_info *devip)
1da177e4 3608{
8d039e22 3609 unsigned char *cmd = scp->cmnd;
1da177e4 3610 unsigned int alloc_len;
8d039e22 3611 unsigned char select_report;
22017ed2 3612 u64 lun;
8d039e22 3613 struct scsi_lun *lun_p;
fb0cc8d1 3614 u8 arr[RL_BUCKET_ELEMS * sizeof(struct scsi_lun)];
8d039e22
DG
3615 unsigned int lun_cnt; /* normal LUN count (max: 256) */
3616 unsigned int wlun_cnt; /* report luns W-LUN count */
3617 unsigned int tlun_cnt; /* total LUN count */
3618 unsigned int rlen; /* response length (in bytes) */
fb0cc8d1
DG
3619 int k, j, n, res;
3620 unsigned int off_rsp = 0;
3621 const int sz_lun = sizeof(struct scsi_lun);
1da177e4 3622
19c8ead7 3623 clear_luns_changed_on_target(devip);
8d039e22
DG
3624
3625 select_report = cmd[2];
3626 alloc_len = get_unaligned_be32(cmd + 6);
3627
3628 if (alloc_len < 4) {
3629 pr_err("alloc len too small %d\n", alloc_len);
3630 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
1da177e4
LT
3631 return check_condition_result;
3632 }
8d039e22
DG
3633
3634 switch (select_report) {
3635 case 0: /* all LUNs apart from W-LUNs */
3636 lun_cnt = sdebug_max_luns;
3637 wlun_cnt = 0;
3638 break;
3639 case 1: /* only W-LUNs */
c65b1445 3640 lun_cnt = 0;
8d039e22
DG
3641 wlun_cnt = 1;
3642 break;
3643 case 2: /* all LUNs */
3644 lun_cnt = sdebug_max_luns;
3645 wlun_cnt = 1;
3646 break;
3647 case 0x10: /* only administrative LUs */
3648 case 0x11: /* see SPC-5 */
3649 case 0x12: /* only subsiduary LUs owned by referenced LU */
3650 default:
3651 pr_debug("select report invalid %d\n", select_report);
3652 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
3653 return check_condition_result;
3654 }
3655
3656 if (sdebug_no_lun_0 && (lun_cnt > 0))
c65b1445 3657 --lun_cnt;
8d039e22
DG
3658
3659 tlun_cnt = lun_cnt + wlun_cnt;
fb0cc8d1
DG
3660 rlen = tlun_cnt * sz_lun; /* excluding 8 byte header */
3661 scsi_set_resid(scp, scsi_bufflen(scp));
8d039e22
DG
3662 pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n",
3663 select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0);
3664
fb0cc8d1 3665 /* loops rely on sizeof response header same as sizeof lun (both 8) */
8d039e22 3666 lun = sdebug_no_lun_0 ? 1 : 0;
fb0cc8d1
DG
3667 for (k = 0, j = 0, res = 0; true; ++k, j = 0) {
3668 memset(arr, 0, sizeof(arr));
3669 lun_p = (struct scsi_lun *)&arr[0];
3670 if (k == 0) {
3671 put_unaligned_be32(rlen, &arr[0]);
3672 ++lun_p;
3673 j = 1;
3674 }
3675 for ( ; j < RL_BUCKET_ELEMS; ++j, ++lun_p) {
3676 if ((k * RL_BUCKET_ELEMS) + j > lun_cnt)
3677 break;
3678 int_to_scsilun(lun++, lun_p);
3679 }
3680 if (j < RL_BUCKET_ELEMS)
3681 break;
3682 n = j * sz_lun;
3683 res = p_fill_from_dev_buffer(scp, arr, n, off_rsp);
3684 if (res)
3685 return res;
3686 off_rsp += n;
3687 }
3688 if (wlun_cnt) {
3689 int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p);
3690 ++j;
3691 }
3692 if (j > 0)
3693 res = p_fill_from_dev_buffer(scp, arr, j * sz_lun, off_rsp);
8d039e22 3694 return res;
1da177e4
LT
3695}
3696
c4837394
DG
3697static struct sdebug_queue *get_queue(struct scsi_cmnd *cmnd)
3698{
458df78b
BVA
3699 u32 tag = blk_mq_unique_tag(cmnd->request);
3700 u16 hwq = blk_mq_unique_tag_to_hwq(tag);
c4837394 3701
458df78b
BVA
3702 pr_debug("tag=%#x, hwq=%d\n", tag, hwq);
3703 if (WARN_ON_ONCE(hwq >= submit_queues))
3704 hwq = 0;
3705 return sdebug_q_arr + hwq;
c4837394
DG
3706}
3707
3708/* Queued (deferred) command completions converge here. */
fd32119b 3709static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp)
1da177e4 3710{
7382f9d8 3711 bool aborted = sd_dp->aborted;
c4837394 3712 int qc_idx;
cbf67842 3713 int retiring = 0;
1da177e4 3714 unsigned long iflags;
c4837394 3715 struct sdebug_queue *sqp;
cbf67842
DG
3716 struct sdebug_queued_cmd *sqcp;
3717 struct scsi_cmnd *scp;
3718 struct sdebug_dev_info *devip;
1da177e4 3719
10bde980 3720 sd_dp->defer_t = SDEB_DEFER_NONE;
7382f9d8
DG
3721 if (unlikely(aborted))
3722 sd_dp->aborted = false;
c4837394
DG
3723 qc_idx = sd_dp->qc_idx;
3724 sqp = sdebug_q_arr + sd_dp->sqa_idx;
3725 if (sdebug_statistics) {
3726 atomic_inc(&sdebug_completions);
3727 if (raw_smp_processor_id() != sd_dp->issuing_cpu)
3728 atomic_inc(&sdebug_miss_cpus);
3729 }
3730 if (unlikely((qc_idx < 0) || (qc_idx >= SDEBUG_CANQUEUE))) {
3731 pr_err("wild qc_idx=%d\n", qc_idx);
1da177e4
LT
3732 return;
3733 }
c4837394
DG
3734 spin_lock_irqsave(&sqp->qc_lock, iflags);
3735 sqcp = &sqp->qc_arr[qc_idx];
cbf67842 3736 scp = sqcp->a_cmnd;
b01f6f83 3737 if (unlikely(scp == NULL)) {
c4837394
DG
3738 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
3739 pr_err("scp is NULL, sqa_idx=%d, qc_idx=%d\n",
3740 sd_dp->sqa_idx, qc_idx);
cbf67842
DG
3741 return;
3742 }
3743 devip = (struct sdebug_dev_info *)scp->device->hostdata;
f46eb0e9 3744 if (likely(devip))
cbf67842
DG
3745 atomic_dec(&devip->num_in_q);
3746 else
c1287970 3747 pr_err("devip=NULL\n");
f46eb0e9 3748 if (unlikely(atomic_read(&retired_max_queue) > 0))
cbf67842
DG
3749 retiring = 1;
3750
3751 sqcp->a_cmnd = NULL;
c4837394
DG
3752 if (unlikely(!test_and_clear_bit(qc_idx, sqp->in_use_bm))) {
3753 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
c1287970 3754 pr_err("Unexpected completion\n");
1da177e4
LT
3755 return;
3756 }
cbf67842
DG
3757
3758 if (unlikely(retiring)) { /* user has reduced max_queue */
3759 int k, retval;
3760
3761 retval = atomic_read(&retired_max_queue);
c4837394
DG
3762 if (qc_idx >= retval) {
3763 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
c1287970 3764 pr_err("index %d too large\n", retval);
cbf67842
DG
3765 return;
3766 }
c4837394 3767 k = find_last_bit(sqp->in_use_bm, retval);
773642d9 3768 if ((k < sdebug_max_queue) || (k == retval))
cbf67842
DG
3769 atomic_set(&retired_max_queue, 0);
3770 else
3771 atomic_set(&retired_max_queue, k + 1);
1da177e4 3772 }
c4837394 3773 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
7382f9d8
DG
3774 if (unlikely(aborted)) {
3775 if (sdebug_verbose)
3776 pr_info("bypassing scsi_done() due to aborted cmd\n");
3777 return;
3778 }
cbf67842 3779 scp->scsi_done(scp); /* callback to mid level */
1da177e4
LT
3780}
3781
cbf67842 3782/* When high resolution timer goes off this function is called. */
fd32119b 3783static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer)
cbf67842 3784{
a10bc12a
DG
3785 struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer,
3786 hrt);
3787 sdebug_q_cmd_complete(sd_dp);
cbf67842
DG
3788 return HRTIMER_NORESTART;
3789}
1da177e4 3790
a10bc12a 3791/* When work queue schedules work, it calls this function. */
fd32119b 3792static void sdebug_q_cmd_wq_complete(struct work_struct *work)
a10bc12a
DG
3793{
3794 struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer,
3795 ew.work);
3796 sdebug_q_cmd_complete(sd_dp);
3797}
3798
09ba24c1 3799static bool got_shared_uuid;
bf476433 3800static uuid_t shared_uuid;
09ba24c1 3801
fd32119b
DG
3802static struct sdebug_dev_info *sdebug_device_create(
3803 struct sdebug_host_info *sdbg_host, gfp_t flags)
5cb2fc06
FT
3804{
3805 struct sdebug_dev_info *devip;
3806
3807 devip = kzalloc(sizeof(*devip), flags);
3808 if (devip) {
09ba24c1 3809 if (sdebug_uuid_ctl == 1)
bf476433 3810 uuid_gen(&devip->lu_name);
09ba24c1
DG
3811 else if (sdebug_uuid_ctl == 2) {
3812 if (got_shared_uuid)
3813 devip->lu_name = shared_uuid;
3814 else {
bf476433 3815 uuid_gen(&shared_uuid);
09ba24c1
DG
3816 got_shared_uuid = true;
3817 devip->lu_name = shared_uuid;
3818 }
3819 }
5cb2fc06
FT
3820 devip->sdbg_host = sdbg_host;
3821 list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list);
3822 }
3823 return devip;
3824}
3825
f46eb0e9 3826static struct sdebug_dev_info *find_build_dev_info(struct scsi_device *sdev)
1da177e4 3827{
f46eb0e9
DG
3828 struct sdebug_host_info *sdbg_host;
3829 struct sdebug_dev_info *open_devip = NULL;
3830 struct sdebug_dev_info *devip;
1da177e4 3831
d1e4c9c5
FT
3832 sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host);
3833 if (!sdbg_host) {
c1287970 3834 pr_err("Host info NULL\n");
1da177e4 3835 return NULL;
9a051019 3836 }
1da177e4
LT
3837 list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
3838 if ((devip->used) && (devip->channel == sdev->channel) &&
9a051019
DG
3839 (devip->target == sdev->id) &&
3840 (devip->lun == sdev->lun))
3841 return devip;
1da177e4
LT
3842 else {
3843 if ((!devip->used) && (!open_devip))
3844 open_devip = devip;
3845 }
3846 }
5cb2fc06
FT
3847 if (!open_devip) { /* try and make a new one */
3848 open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC);
3849 if (!open_devip) {
c1287970 3850 pr_err("out of memory at line %d\n", __LINE__);
1da177e4
LT
3851 return NULL;
3852 }
1da177e4 3853 }
a75869d1
FT
3854
3855 open_devip->channel = sdev->channel;
3856 open_devip->target = sdev->id;
3857 open_devip->lun = sdev->lun;
3858 open_devip->sdbg_host = sdbg_host;
cbf67842
DG
3859 atomic_set(&open_devip->num_in_q, 0);
3860 set_bit(SDEBUG_UA_POR, open_devip->uas_bm);
c2248fc9 3861 open_devip->used = true;
a75869d1 3862 return open_devip;
1da177e4
LT
3863}
3864
8dea0d02 3865static int scsi_debug_slave_alloc(struct scsi_device *sdp)
1da177e4 3866{
773642d9 3867 if (sdebug_verbose)
c1287970 3868 pr_info("slave_alloc <%u %u %u %llu>\n",
8dea0d02 3869 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
8dea0d02
FT
3870 return 0;
3871}
1da177e4 3872
8dea0d02
FT
3873static int scsi_debug_slave_configure(struct scsi_device *sdp)
3874{
f46eb0e9
DG
3875 struct sdebug_dev_info *devip =
3876 (struct sdebug_dev_info *)sdp->hostdata;
a34c4e98 3877
773642d9 3878 if (sdebug_verbose)
c1287970 3879 pr_info("slave_configure <%u %u %u %llu>\n",
8dea0d02 3880 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
b01f6f83
DG
3881 if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN)
3882 sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN;
3883 if (devip == NULL) {
f46eb0e9 3884 devip = find_build_dev_info(sdp);
b01f6f83 3885 if (devip == NULL)
f46eb0e9
DG
3886 return 1; /* no resources, will be marked offline */
3887 }
c8b09f6f 3888 sdp->hostdata = devip;
773642d9 3889 if (sdebug_no_uld)
78d4e5a0 3890 sdp->no_uld_attach = 1;
9b760fd8 3891 config_cdb_len(sdp);
8dea0d02
FT
3892 return 0;
3893}
3894
3895static void scsi_debug_slave_destroy(struct scsi_device *sdp)
3896{
3897 struct sdebug_dev_info *devip =
3898 (struct sdebug_dev_info *)sdp->hostdata;
a34c4e98 3899
773642d9 3900 if (sdebug_verbose)
c1287970 3901 pr_info("slave_destroy <%u %u %u %llu>\n",
8dea0d02
FT
3902 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
3903 if (devip) {
25985edc 3904 /* make this slot available for re-use */
c2248fc9 3905 devip->used = false;
8dea0d02
FT
3906 sdp->hostdata = NULL;
3907 }
3908}
3909
10bde980
DG
3910static void stop_qc_helper(struct sdebug_defer *sd_dp,
3911 enum sdeb_defer_type defer_t)
c4837394
DG
3912{
3913 if (!sd_dp)
3914 return;
10bde980 3915 if (defer_t == SDEB_DEFER_HRT)
c4837394 3916 hrtimer_cancel(&sd_dp->hrt);
10bde980 3917 else if (defer_t == SDEB_DEFER_WQ)
c4837394
DG
3918 cancel_work_sync(&sd_dp->ew.work);
3919}
3920
a10bc12a
DG
3921/* If @cmnd found deletes its timer or work queue and returns true; else
3922 returns false */
3923static bool stop_queued_cmnd(struct scsi_cmnd *cmnd)
8dea0d02
FT
3924{
3925 unsigned long iflags;
c4837394 3926 int j, k, qmax, r_qmax;
10bde980 3927 enum sdeb_defer_type l_defer_t;
c4837394 3928 struct sdebug_queue *sqp;
8dea0d02 3929 struct sdebug_queued_cmd *sqcp;
cbf67842 3930 struct sdebug_dev_info *devip;
a10bc12a 3931 struct sdebug_defer *sd_dp;
8dea0d02 3932
c4837394
DG
3933 for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
3934 spin_lock_irqsave(&sqp->qc_lock, iflags);
3935 qmax = sdebug_max_queue;
3936 r_qmax = atomic_read(&retired_max_queue);
3937 if (r_qmax > qmax)
3938 qmax = r_qmax;
3939 for (k = 0; k < qmax; ++k) {
3940 if (test_bit(k, sqp->in_use_bm)) {
3941 sqcp = &sqp->qc_arr[k];
3942 if (cmnd != sqcp->a_cmnd)
3943 continue;
3944 /* found */
3945 devip = (struct sdebug_dev_info *)
3946 cmnd->device->hostdata;
3947 if (devip)
3948 atomic_dec(&devip->num_in_q);
3949 sqcp->a_cmnd = NULL;
3950 sd_dp = sqcp->sd_dp;
10bde980
DG
3951 if (sd_dp) {
3952 l_defer_t = sd_dp->defer_t;
3953 sd_dp->defer_t = SDEB_DEFER_NONE;
3954 } else
3955 l_defer_t = SDEB_DEFER_NONE;
c4837394 3956 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
10bde980 3957 stop_qc_helper(sd_dp, l_defer_t);
c4837394
DG
3958 clear_bit(k, sqp->in_use_bm);
3959 return true;
cbf67842 3960 }
8dea0d02 3961 }
c4837394 3962 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
8dea0d02 3963 }
a10bc12a 3964 return false;
8dea0d02
FT
3965}
3966
a10bc12a 3967/* Deletes (stops) timers or work queues of all queued commands */
8dea0d02
FT
3968static void stop_all_queued(void)
3969{
3970 unsigned long iflags;
c4837394 3971 int j, k;
10bde980 3972 enum sdeb_defer_type l_defer_t;
c4837394 3973 struct sdebug_queue *sqp;
8dea0d02 3974 struct sdebug_queued_cmd *sqcp;
cbf67842 3975 struct sdebug_dev_info *devip;
a10bc12a 3976 struct sdebug_defer *sd_dp;
8dea0d02 3977
c4837394
DG
3978 for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
3979 spin_lock_irqsave(&sqp->qc_lock, iflags);
3980 for (k = 0; k < SDEBUG_CANQUEUE; ++k) {
3981 if (test_bit(k, sqp->in_use_bm)) {
3982 sqcp = &sqp->qc_arr[k];
3983 if (sqcp->a_cmnd == NULL)
3984 continue;
3985 devip = (struct sdebug_dev_info *)
3986 sqcp->a_cmnd->device->hostdata;
3987 if (devip)
3988 atomic_dec(&devip->num_in_q);
3989 sqcp->a_cmnd = NULL;
3990 sd_dp = sqcp->sd_dp;
10bde980
DG
3991 if (sd_dp) {
3992 l_defer_t = sd_dp->defer_t;
3993 sd_dp->defer_t = SDEB_DEFER_NONE;
3994 } else
3995 l_defer_t = SDEB_DEFER_NONE;
c4837394 3996 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
10bde980 3997 stop_qc_helper(sd_dp, l_defer_t);
c4837394
DG
3998 clear_bit(k, sqp->in_use_bm);
3999 spin_lock_irqsave(&sqp->qc_lock, iflags);
cbf67842 4000 }
8dea0d02 4001 }
c4837394 4002 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
8dea0d02 4003 }
1da177e4
LT
4004}
4005
cbf67842
DG
4006/* Free queued command memory on heap */
4007static void free_all_queued(void)
1da177e4 4008{
c4837394
DG
4009 int j, k;
4010 struct sdebug_queue *sqp;
cbf67842
DG
4011 struct sdebug_queued_cmd *sqcp;
4012
c4837394
DG
4013 for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
4014 for (k = 0; k < SDEBUG_CANQUEUE; ++k) {
4015 sqcp = &sqp->qc_arr[k];
4016 kfree(sqcp->sd_dp);
4017 sqcp->sd_dp = NULL;
4018 }
cbf67842 4019 }
1da177e4
LT
4020}
4021
cbf67842 4022static int scsi_debug_abort(struct scsi_cmnd *SCpnt)
1da177e4 4023{
a10bc12a
DG
4024 bool ok;
4025
cbf67842
DG
4026 ++num_aborts;
4027 if (SCpnt) {
a10bc12a
DG
4028 ok = stop_queued_cmnd(SCpnt);
4029 if (SCpnt->device && (SDEBUG_OPT_ALL_NOISE & sdebug_opts))
4030 sdev_printk(KERN_INFO, SCpnt->device,
4031 "%s: command%s found\n", __func__,
4032 ok ? "" : " not");
cbf67842
DG
4033 }
4034 return SUCCESS;
1da177e4
LT
4035}
4036
91d4c752 4037static int scsi_debug_device_reset(struct scsi_cmnd *SCpnt)
1da177e4 4038{
1da177e4 4039 ++num_dev_resets;
cbf67842
DG
4040 if (SCpnt && SCpnt->device) {
4041 struct scsi_device *sdp = SCpnt->device;
f46eb0e9
DG
4042 struct sdebug_dev_info *devip =
4043 (struct sdebug_dev_info *)sdp->hostdata;
cbf67842 4044
773642d9 4045 if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
cbf67842 4046 sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
1da177e4 4047 if (devip)
cbf67842
DG
4048 set_bit(SDEBUG_UA_POR, devip->uas_bm);
4049 }
4050 return SUCCESS;
4051}
4052
4053static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt)
4054{
4055 struct sdebug_host_info *sdbg_host;
4056 struct sdebug_dev_info *devip;
4057 struct scsi_device *sdp;
4058 struct Scsi_Host *hp;
4059 int k = 0;
4060
4061 ++num_target_resets;
4062 if (!SCpnt)
4063 goto lie;
4064 sdp = SCpnt->device;
4065 if (!sdp)
4066 goto lie;
773642d9 4067 if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
cbf67842
DG
4068 sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
4069 hp = sdp->host;
4070 if (!hp)
4071 goto lie;
4072 sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
4073 if (sdbg_host) {
4074 list_for_each_entry(devip,
4075 &sdbg_host->dev_info_list,
4076 dev_list)
4077 if (devip->target == sdp->id) {
4078 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
4079 ++k;
4080 }
1da177e4 4081 }
773642d9 4082 if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
cbf67842
DG
4083 sdev_printk(KERN_INFO, sdp,
4084 "%s: %d device(s) found in target\n", __func__, k);
4085lie:
1da177e4
LT
4086 return SUCCESS;
4087}
4088
91d4c752 4089static int scsi_debug_bus_reset(struct scsi_cmnd *SCpnt)
1da177e4
LT
4090{
4091 struct sdebug_host_info *sdbg_host;
cbf67842 4092 struct sdebug_dev_info *devip;
9a051019
DG
4093 struct scsi_device *sdp;
4094 struct Scsi_Host *hp;
cbf67842 4095 int k = 0;
1da177e4 4096
1da177e4 4097 ++num_bus_resets;
cbf67842
DG
4098 if (!(SCpnt && SCpnt->device))
4099 goto lie;
4100 sdp = SCpnt->device;
773642d9 4101 if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
cbf67842
DG
4102 sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
4103 hp = sdp->host;
4104 if (hp) {
d1e4c9c5 4105 sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
1da177e4 4106 if (sdbg_host) {
cbf67842 4107 list_for_each_entry(devip,
9a051019 4108 &sdbg_host->dev_info_list,
cbf67842
DG
4109 dev_list) {
4110 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
4111 ++k;
4112 }
1da177e4
LT
4113 }
4114 }
773642d9 4115 if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
cbf67842
DG
4116 sdev_printk(KERN_INFO, sdp,
4117 "%s: %d device(s) found in host\n", __func__, k);
4118lie:
1da177e4
LT
4119 return SUCCESS;
4120}
4121
91d4c752 4122static int scsi_debug_host_reset(struct scsi_cmnd *SCpnt)
1da177e4 4123{
91d4c752 4124 struct sdebug_host_info *sdbg_host;
cbf67842
DG
4125 struct sdebug_dev_info *devip;
4126 int k = 0;
1da177e4 4127
1da177e4 4128 ++num_host_resets;
773642d9 4129 if ((SCpnt->device) && (SDEBUG_OPT_ALL_NOISE & sdebug_opts))
cbf67842 4130 sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__);
9a051019
DG
4131 spin_lock(&sdebug_host_list_lock);
4132 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
cbf67842
DG
4133 list_for_each_entry(devip, &sdbg_host->dev_info_list,
4134 dev_list) {
4135 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
4136 ++k;
4137 }
9a051019
DG
4138 }
4139 spin_unlock(&sdebug_host_list_lock);
1da177e4 4140 stop_all_queued();
773642d9 4141 if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
cbf67842
DG
4142 sdev_printk(KERN_INFO, SCpnt->device,
4143 "%s: %d device(s) found\n", __func__, k);
1da177e4
LT
4144 return SUCCESS;
4145}
4146
f58b0efb 4147static void __init sdebug_build_parts(unsigned char *ramp,
5f2578e5 4148 unsigned long store_size)
1da177e4 4149{
1442f76d 4150 struct msdos_partition *pp;
1da177e4
LT
4151 int starts[SDEBUG_MAX_PARTS + 2];
4152 int sectors_per_part, num_sectors, k;
4153 int heads_by_sects, start_sec, end_sec;
4154
4155 /* assume partition table already zeroed */
773642d9 4156 if ((sdebug_num_parts < 1) || (store_size < 1048576))
1da177e4 4157 return;
773642d9
DG
4158 if (sdebug_num_parts > SDEBUG_MAX_PARTS) {
4159 sdebug_num_parts = SDEBUG_MAX_PARTS;
c1287970 4160 pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS);
1da177e4 4161 }
c65b1445 4162 num_sectors = (int)sdebug_store_sectors;
1da177e4 4163 sectors_per_part = (num_sectors - sdebug_sectors_per)
773642d9 4164 / sdebug_num_parts;
1da177e4 4165 heads_by_sects = sdebug_heads * sdebug_sectors_per;
9a051019 4166 starts[0] = sdebug_sectors_per;
773642d9 4167 for (k = 1; k < sdebug_num_parts; ++k)
1da177e4
LT
4168 starts[k] = ((k * sectors_per_part) / heads_by_sects)
4169 * heads_by_sects;
773642d9
DG
4170 starts[sdebug_num_parts] = num_sectors;
4171 starts[sdebug_num_parts + 1] = 0;
1da177e4
LT
4172
4173 ramp[510] = 0x55; /* magic partition markings */
4174 ramp[511] = 0xAA;
1442f76d 4175 pp = (struct msdos_partition *)(ramp + 0x1be);
1da177e4
LT
4176 for (k = 0; starts[k + 1]; ++k, ++pp) {
4177 start_sec = starts[k];
4178 end_sec = starts[k + 1] - 1;
4179 pp->boot_ind = 0;
4180
4181 pp->cyl = start_sec / heads_by_sects;
4182 pp->head = (start_sec - (pp->cyl * heads_by_sects))
4183 / sdebug_sectors_per;
4184 pp->sector = (start_sec % sdebug_sectors_per) + 1;
4185
4186 pp->end_cyl = end_sec / heads_by_sects;
4187 pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
4188 / sdebug_sectors_per;
4189 pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
4190
150c3544
AM
4191 pp->start_sect = cpu_to_le32(start_sec);
4192 pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1);
1da177e4
LT
4193 pp->sys_ind = 0x83; /* plain Linux partition */
4194 }
4195}
4196
c4837394
DG
4197static void block_unblock_all_queues(bool block)
4198{
4199 int j;
4200 struct sdebug_queue *sqp;
4201
4202 for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp)
4203 atomic_set(&sqp->blocked, (int)block);
4204}
4205
4206/* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1
4207 * commands will be processed normally before triggers occur.
4208 */
4209static void tweak_cmnd_count(void)
4210{
4211 int count, modulo;
4212
4213 modulo = abs(sdebug_every_nth);
4214 if (modulo < 2)
4215 return;
4216 block_unblock_all_queues(true);
4217 count = atomic_read(&sdebug_cmnd_count);
4218 atomic_set(&sdebug_cmnd_count, (count / modulo) * modulo);
4219 block_unblock_all_queues(false);
4220}
4221
4222static void clear_queue_stats(void)
4223{
4224 atomic_set(&sdebug_cmnd_count, 0);
4225 atomic_set(&sdebug_completions, 0);
4226 atomic_set(&sdebug_miss_cpus, 0);
4227 atomic_set(&sdebug_a_tsf, 0);
4228}
4229
4230static void setup_inject(struct sdebug_queue *sqp,
4231 struct sdebug_queued_cmd *sqcp)
4232{
f9ba7af8
MW
4233 if ((atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) > 0) {
4234 if (sdebug_every_nth > 0)
4235 sqcp->inj_recovered = sqcp->inj_transport
4236 = sqcp->inj_dif
7382f9d8
DG
4237 = sqcp->inj_dix = sqcp->inj_short
4238 = sqcp->inj_host_busy = sqcp->inj_cmd_abort = 0;
c4837394 4239 return;
f9ba7af8 4240 }
c4837394
DG
4241 sqcp->inj_recovered = !!(SDEBUG_OPT_RECOVERED_ERR & sdebug_opts);
4242 sqcp->inj_transport = !!(SDEBUG_OPT_TRANSPORT_ERR & sdebug_opts);
4243 sqcp->inj_dif = !!(SDEBUG_OPT_DIF_ERR & sdebug_opts);
4244 sqcp->inj_dix = !!(SDEBUG_OPT_DIX_ERR & sdebug_opts);
4245 sqcp->inj_short = !!(SDEBUG_OPT_SHORT_TRANSFER & sdebug_opts);
7ee6d1b4 4246 sqcp->inj_host_busy = !!(SDEBUG_OPT_HOST_BUSY & sdebug_opts);
7382f9d8 4247 sqcp->inj_cmd_abort = !!(SDEBUG_OPT_CMD_ABORT & sdebug_opts);
c4837394
DG
4248}
4249
4250/* Complete the processing of the thread that queued a SCSI command to this
4251 * driver. It either completes the command by calling cmnd_done() or
4252 * schedules a hr timer or work queue then returns 0. Returns
4253 * SCSI_MLQUEUE_HOST_BUSY if temporarily out of resources.
4254 */
fd32119b 4255static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip,
f66b8517
MW
4256 int scsi_result,
4257 int (*pfp)(struct scsi_cmnd *,
4258 struct sdebug_dev_info *),
4259 int delta_jiff, int ndelay)
1da177e4 4260{
cbf67842 4261 unsigned long iflags;
cd62b7da 4262 int k, num_in_q, qdepth, inject;
c4837394
DG
4263 struct sdebug_queue *sqp;
4264 struct sdebug_queued_cmd *sqcp;
299b6c07 4265 struct scsi_device *sdp;
a10bc12a 4266 struct sdebug_defer *sd_dp;
299b6c07 4267
b01f6f83
DG
4268 if (unlikely(devip == NULL)) {
4269 if (scsi_result == 0)
f46eb0e9
DG
4270 scsi_result = DID_NO_CONNECT << 16;
4271 goto respond_in_thread;
cbf67842 4272 }
299b6c07
TW
4273 sdp = cmnd->device;
4274
cd62b7da
DG
4275 if (delta_jiff == 0)
4276 goto respond_in_thread;
1da177e4 4277
cd62b7da 4278 /* schedule the response at a later time if resources permit */
c4837394
DG
4279 sqp = get_queue(cmnd);
4280 spin_lock_irqsave(&sqp->qc_lock, iflags);
4281 if (unlikely(atomic_read(&sqp->blocked))) {
4282 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
4283 return SCSI_MLQUEUE_HOST_BUSY;
4284 }
cbf67842
DG
4285 num_in_q = atomic_read(&devip->num_in_q);
4286 qdepth = cmnd->device->queue_depth;
cbf67842 4287 inject = 0;
f46eb0e9 4288 if (unlikely((qdepth > 0) && (num_in_q >= qdepth))) {
cd62b7da 4289 if (scsi_result) {
c4837394 4290 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
cd62b7da
DG
4291 goto respond_in_thread;
4292 } else
4293 scsi_result = device_qfull_result;
c4837394 4294 } else if (unlikely(sdebug_every_nth &&
f46eb0e9
DG
4295 (SDEBUG_OPT_RARE_TSF & sdebug_opts) &&
4296 (scsi_result == 0))) {
cbf67842
DG
4297 if ((num_in_q == (qdepth - 1)) &&
4298 (atomic_inc_return(&sdebug_a_tsf) >=
773642d9 4299 abs(sdebug_every_nth))) {
cbf67842
DG
4300 atomic_set(&sdebug_a_tsf, 0);
4301 inject = 1;
cd62b7da 4302 scsi_result = device_qfull_result;
1da177e4
LT
4303 }
4304 }
1da177e4 4305
c4837394 4306 k = find_first_zero_bit(sqp->in_use_bm, sdebug_max_queue);
f46eb0e9 4307 if (unlikely(k >= sdebug_max_queue)) {
c4837394 4308 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
cd62b7da
DG
4309 if (scsi_result)
4310 goto respond_in_thread;
773642d9 4311 else if (SDEBUG_OPT_ALL_TSF & sdebug_opts)
cd62b7da 4312 scsi_result = device_qfull_result;
773642d9 4313 if (SDEBUG_OPT_Q_NOISE & sdebug_opts)
cbf67842 4314 sdev_printk(KERN_INFO, sdp,
cd62b7da 4315 "%s: max_queue=%d exceeded, %s\n",
773642d9 4316 __func__, sdebug_max_queue,
cd62b7da
DG
4317 (scsi_result ? "status: TASK SET FULL" :
4318 "report: host busy"));
4319 if (scsi_result)
4320 goto respond_in_thread;
4321 else
cbf67842
DG
4322 return SCSI_MLQUEUE_HOST_BUSY;
4323 }
c4837394 4324 __set_bit(k, sqp->in_use_bm);
cbf67842 4325 atomic_inc(&devip->num_in_q);
c4837394 4326 sqcp = &sqp->qc_arr[k];
cbf67842 4327 sqcp->a_cmnd = cmnd;
c4837394 4328 cmnd->host_scribble = (unsigned char *)sqcp;
a10bc12a 4329 sd_dp = sqcp->sd_dp;
c4837394
DG
4330 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
4331 if (unlikely(sdebug_every_nth && sdebug_any_injecting_opt))
4332 setup_inject(sqp, sqcp);
10bde980
DG
4333 if (sd_dp == NULL) {
4334 sd_dp = kzalloc(sizeof(*sd_dp), GFP_ATOMIC);
4335 if (sd_dp == NULL)
4336 return SCSI_MLQUEUE_HOST_BUSY;
4337 }
f66b8517
MW
4338
4339 cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0;
4340 if (cmnd->result & SDEG_RES_IMMED_MASK) {
4341 /*
4342 * This is the F_DELAY_OVERR case. No delay.
4343 */
4344 cmnd->result &= ~SDEG_RES_IMMED_MASK;
4345 delta_jiff = ndelay = 0;
4346 }
4347 if (cmnd->result == 0 && scsi_result != 0)
4348 cmnd->result = scsi_result;
4349
4350 if (unlikely(sdebug_verbose && cmnd->result))
4351 sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n",
4352 __func__, cmnd->result);
4353
10bde980 4354 if (delta_jiff > 0 || ndelay > 0) {
b333a819 4355 ktime_t kt;
cbf67842 4356
b333a819 4357 if (delta_jiff > 0) {
13f6b610 4358 kt = ns_to_ktime((u64)delta_jiff * (NSEC_PER_SEC / HZ));
b333a819 4359 } else
10bde980
DG
4360 kt = ndelay;
4361 if (!sd_dp->init_hrt) {
4362 sd_dp->init_hrt = true;
a10bc12a
DG
4363 sqcp->sd_dp = sd_dp;
4364 hrtimer_init(&sd_dp->hrt, CLOCK_MONOTONIC,
c4837394 4365 HRTIMER_MODE_REL_PINNED);
a10bc12a 4366 sd_dp->hrt.function = sdebug_q_cmd_hrt_complete;
c4837394
DG
4367 sd_dp->sqa_idx = sqp - sdebug_q_arr;
4368 sd_dp->qc_idx = k;
1da177e4 4369 }
c4837394
DG
4370 if (sdebug_statistics)
4371 sd_dp->issuing_cpu = raw_smp_processor_id();
10bde980 4372 sd_dp->defer_t = SDEB_DEFER_HRT;
c4837394
DG
4373 hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL_PINNED);
4374 } else { /* jdelay < 0, use work queue */
10bde980
DG
4375 if (!sd_dp->init_wq) {
4376 sd_dp->init_wq = true;
a10bc12a 4377 sqcp->sd_dp = sd_dp;
c4837394
DG
4378 sd_dp->sqa_idx = sqp - sdebug_q_arr;
4379 sd_dp->qc_idx = k;
a10bc12a 4380 INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete);
cbf67842 4381 }
c4837394
DG
4382 if (sdebug_statistics)
4383 sd_dp->issuing_cpu = raw_smp_processor_id();
10bde980 4384 sd_dp->defer_t = SDEB_DEFER_WQ;
7382f9d8
DG
4385 if (unlikely(sqcp->inj_cmd_abort))
4386 sd_dp->aborted = true;
a10bc12a 4387 schedule_work(&sd_dp->ew.work);
7382f9d8
DG
4388 if (unlikely(sqcp->inj_cmd_abort)) {
4389 sdev_printk(KERN_INFO, sdp, "abort request tag %d\n",
4390 cmnd->request->tag);
4391 blk_abort_request(cmnd->request);
4392 }
1da177e4 4393 }
f46eb0e9
DG
4394 if (unlikely((SDEBUG_OPT_Q_NOISE & sdebug_opts) &&
4395 (scsi_result == device_qfull_result)))
cbf67842
DG
4396 sdev_printk(KERN_INFO, sdp,
4397 "%s: num_in_q=%d +1, %s%s\n", __func__,
4398 num_in_q, (inject ? "<inject> " : ""),
4399 "status: TASK SET FULL");
4400 return 0;
cd62b7da
DG
4401
4402respond_in_thread: /* call back to mid-layer using invocation thread */
f66b8517
MW
4403 cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0;
4404 cmnd->result &= ~SDEG_RES_IMMED_MASK;
4405 if (cmnd->result == 0 && scsi_result != 0)
4406 cmnd->result = scsi_result;
cd62b7da
DG
4407 cmnd->scsi_done(cmnd);
4408 return 0;
1da177e4 4409}
cbf67842 4410
23183910
DG
4411/* Note: The following macros create attribute files in the
4412 /sys/module/scsi_debug/parameters directory. Unfortunately this
4413 driver is unaware of a change and cannot trigger auxiliary actions
4414 as it can when the corresponding attribute in the
4415 /sys/bus/pseudo/drivers/scsi_debug directory is changed.
4416 */
773642d9
DG
4417module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR);
4418module_param_named(ato, sdebug_ato, int, S_IRUGO);
9b760fd8 4419module_param_named(cdb_len, sdebug_cdb_len, int, 0644);
773642d9 4420module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR);
c2206098 4421module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR);
773642d9
DG
4422module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO);
4423module_param_named(dif, sdebug_dif, int, S_IRUGO);
4424module_param_named(dix, sdebug_dix, int, S_IRUGO);
4425module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR);
4426module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR);
4427module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR);
4428module_param_named(guard, sdebug_guard, uint, S_IRUGO);
4429module_param_named(host_lock, sdebug_host_lock, bool, S_IRUGO | S_IWUSR);
e5203cf0
HR
4430module_param_string(inq_vendor, sdebug_inq_vendor_id,
4431 sizeof(sdebug_inq_vendor_id), S_IRUGO|S_IWUSR);
4432module_param_string(inq_product, sdebug_inq_product_id,
4433 sizeof(sdebug_inq_product_id), S_IRUGO|S_IWUSR);
4434module_param_string(inq_rev, sdebug_inq_product_rev,
4435 sizeof(sdebug_inq_product_rev), S_IRUGO|S_IWUSR);
773642d9
DG
4436module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO);
4437module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO);
4438module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO);
4439module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO);
4440module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO);
4441module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR);
4442module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR);
d9da891a
LO
4443module_param_named(medium_error_start, sdebug_medium_error_start, int, S_IRUGO | S_IWUSR);
4444module_param_named(medium_error_count, sdebug_medium_error_count, int, S_IRUGO | S_IWUSR);
773642d9
DG
4445module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR);
4446module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR);
4447module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO);
4448module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO);
4449module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR);
4450module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO);
4451module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR);
4452module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO);
86e6828a 4453module_param_named(opt_xferlen_exp, sdebug_opt_xferlen_exp, int, S_IRUGO);
773642d9
DG
4454module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR);
4455module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR);
4456module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO);
4457module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO);
c4837394 4458module_param_named(statistics, sdebug_statistics, bool, S_IRUGO | S_IWUSR);
773642d9 4459module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR);
c4837394 4460module_param_named(submit_queues, submit_queues, int, S_IRUGO);
773642d9
DG
4461module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO);
4462module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO);
4463module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO);
4464module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO);
4465module_param_named(virtual_gb, sdebug_virtual_gb, int, S_IRUGO | S_IWUSR);
09ba24c1 4466module_param_named(uuid_ctl, sdebug_uuid_ctl, int, S_IRUGO);
773642d9 4467module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int,
5b94e232 4468 S_IRUGO | S_IWUSR);
9447b6ce 4469module_param_named(wp, sdebug_wp, bool, S_IRUGO | S_IWUSR);
773642d9 4470module_param_named(write_same_length, sdebug_write_same_length, int,
5b94e232 4471 S_IRUGO | S_IWUSR);
1da177e4
LT
4472
4473MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
4474MODULE_DESCRIPTION("SCSI debug adapter driver");
4475MODULE_LICENSE("GPL");
b01f6f83 4476MODULE_VERSION(SDEBUG_VERSION);
1da177e4
LT
4477
4478MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
5b94e232 4479MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)");
9b760fd8 4480MODULE_PARM_DESC(cdb_len, "suggest CDB lengths to drivers (def=10)");
0759c666 4481MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)");
cbf67842 4482MODULE_PARM_DESC(delay, "response delay (def=1 jiffy); 0:imm, -1,-2:tiny");
c2248fc9 4483MODULE_PARM_DESC(dev_size_mb, "size in MiB of ram shared by devs(def=8)");
5b94e232
MP
4484MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)");
4485MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)");
c65b1445 4486MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
beb87c33 4487MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)");
23183910 4488MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
5b94e232 4489MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)");
185dd232 4490MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)");
e5203cf0
HR
4491MODULE_PARM_DESC(inq_vendor, "SCSI INQUIRY vendor string (def=\"Linux\")");
4492MODULE_PARM_DESC(inq_product, "SCSI INQUIRY product string (def=\"scsi_debug\")");
9b760fd8
DG
4493MODULE_PARM_DESC(inq_rev, "SCSI INQUIRY revision string (def=\""
4494 SDEBUG_VERSION "\")");
5b94e232
MP
4495MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)");
4496MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)");
4497MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)");
760f3b03
DG
4498MODULE_PARM_DESC(lbprz,
4499 "on read unmapped LBs return 0 when 1 (def), return 0xff when 2");
5b94e232 4500MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)");
c65b1445 4501MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
cbf67842 4502MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))");
d9da891a
LO
4503MODULE_PARM_DESC(medium_error_start, "starting sector number to return MEDIUM error");
4504MODULE_PARM_DESC(medium_error_count, "count of sectors to return follow on MEDIUM error");
cbf67842 4505MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)");
c65b1445 4506MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
78d4e5a0 4507MODULE_PARM_DESC(no_uld, "stop ULD (e.g. sd driver) attaching (def=0))");
1da177e4 4508MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
c65b1445 4509MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
32c5844a 4510MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)");
6f3cbf55 4511MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)");
5b94e232 4512MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)");
86e6828a 4513MODULE_PARM_DESC(opt_xferlen_exp, "optimal transfer length granularity exponent (def=physblk_exp)");
1da177e4 4514MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
d986788b 4515MODULE_PARM_DESC(removable, "claim to have removable media (def=0)");
760f3b03 4516MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=7[SPC-5])");
ea61fca5 4517MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)");
c4837394 4518MODULE_PARM_DESC(statistics, "collect statistics on commands, queues (def=0)");
c2248fc9 4519MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)");
c4837394 4520MODULE_PARM_DESC(submit_queues, "support for block multi-queue (def=1)");
5b94e232
MP
4521MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)");
4522MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)");
6014759c
MP
4523MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)");
4524MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)");
09ba24c1
DG
4525MODULE_PARM_DESC(uuid_ctl,
4526 "1->use uuid for lu name, 0->don't, 2->all use same (def=0)");
c2248fc9 4527MODULE_PARM_DESC(virtual_gb, "virtual gigabyte (GiB) size (def=0 -> use dev_size_mb)");
5b94e232 4528MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
9447b6ce 4529MODULE_PARM_DESC(wp, "Write Protect (def=0)");
5b94e232 4530MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)");
1da177e4 4531
760f3b03
DG
4532#define SDEBUG_INFO_LEN 256
4533static char sdebug_info[SDEBUG_INFO_LEN];
1da177e4 4534
91d4c752 4535static const char *scsi_debug_info(struct Scsi_Host *shp)
1da177e4 4536{
c4837394
DG
4537 int k;
4538
760f3b03
DG
4539 k = scnprintf(sdebug_info, SDEBUG_INFO_LEN, "%s: version %s [%s]\n",
4540 my_name, SDEBUG_VERSION, sdebug_version_date);
4541 if (k >= (SDEBUG_INFO_LEN - 1))
c4837394 4542 return sdebug_info;
760f3b03
DG
4543 scnprintf(sdebug_info + k, SDEBUG_INFO_LEN - k,
4544 " dev_size_mb=%d, opts=0x%x, submit_queues=%d, %s=%d",
4545 sdebug_dev_size_mb, sdebug_opts, submit_queues,
4546 "statistics", (int)sdebug_statistics);
1da177e4
LT
4547 return sdebug_info;
4548}
4549
cbf67842 4550/* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */
fd32119b
DG
4551static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer,
4552 int length)
1da177e4 4553{
c8ed555a
AV
4554 char arr[16];
4555 int opts;
4556 int minLen = length > 15 ? 15 : length;
1da177e4 4557
c8ed555a
AV
4558 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
4559 return -EACCES;
4560 memcpy(arr, buffer, minLen);
4561 arr[minLen] = '\0';
4562 if (1 != sscanf(arr, "%d", &opts))
4563 return -EINVAL;
773642d9
DG
4564 sdebug_opts = opts;
4565 sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
4566 sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts);
4567 if (sdebug_every_nth != 0)
c4837394 4568 tweak_cmnd_count();
c8ed555a
AV
4569 return length;
4570}
1da177e4 4571
cbf67842
DG
4572/* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the
4573 * same for each scsi_debug host (if more than one). Some of the counters
4574 * output are not atomics so might be inaccurate in a busy system. */
c8ed555a
AV
4575static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host)
4576{
c4837394
DG
4577 int f, j, l;
4578 struct sdebug_queue *sqp;
4579
4580 seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n",
4581 SDEBUG_VERSION, sdebug_version_date);
4582 seq_printf(m, "num_tgts=%d, %ssize=%d MB, opts=0x%x, every_nth=%d\n",
4583 sdebug_num_tgts, "shared (ram) ", sdebug_dev_size_mb,
4584 sdebug_opts, sdebug_every_nth);
4585 seq_printf(m, "delay=%d, ndelay=%d, max_luns=%d, sector_size=%d %s\n",
4586 sdebug_jdelay, sdebug_ndelay, sdebug_max_luns,
4587 sdebug_sector_size, "bytes");
4588 seq_printf(m, "cylinders=%d, heads=%d, sectors=%d, command aborts=%d\n",
4589 sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
4590 num_aborts);
4591 seq_printf(m, "RESETs: device=%d, target=%d, bus=%d, host=%d\n",
4592 num_dev_resets, num_target_resets, num_bus_resets,
4593 num_host_resets);
4594 seq_printf(m, "dix_reads=%d, dix_writes=%d, dif_errors=%d\n",
4595 dix_reads, dix_writes, dif_errors);
458df78b
BVA
4596 seq_printf(m, "usec_in_jiffy=%lu, statistics=%d\n", TICK_NSEC / 1000,
4597 sdebug_statistics);
c4837394
DG
4598 seq_printf(m, "cmnd_count=%d, completions=%d, %s=%d, a_tsf=%d\n",
4599 atomic_read(&sdebug_cmnd_count),
4600 atomic_read(&sdebug_completions),
4601 "miss_cpus", atomic_read(&sdebug_miss_cpus),
4602 atomic_read(&sdebug_a_tsf));
4603
4604 seq_printf(m, "submit_queues=%d\n", submit_queues);
4605 for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
4606 seq_printf(m, " queue %d:\n", j);
4607 f = find_first_bit(sqp->in_use_bm, sdebug_max_queue);
4608 if (f != sdebug_max_queue) {
4609 l = find_last_bit(sqp->in_use_bm, sdebug_max_queue);
4610 seq_printf(m, " in_use_bm BUSY: %s: %d,%d\n",
4611 "first,last bits", f, l);
4612 }
cbf67842 4613 }
c8ed555a 4614 return 0;
1da177e4
LT
4615}
4616
82069379 4617static ssize_t delay_show(struct device_driver *ddp, char *buf)
1da177e4 4618{
c2206098 4619 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay);
1da177e4 4620}
c4837394
DG
4621/* Returns -EBUSY if jdelay is being changed and commands are queued. The unit
4622 * of delay is jiffies.
4623 */
82069379
AM
4624static ssize_t delay_store(struct device_driver *ddp, const char *buf,
4625 size_t count)
1da177e4 4626{
c2206098 4627 int jdelay, res;
cbf67842 4628
b01f6f83 4629 if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) {
cbf67842 4630 res = count;
c2206098 4631 if (sdebug_jdelay != jdelay) {
c4837394
DG
4632 int j, k;
4633 struct sdebug_queue *sqp;
4634
4635 block_unblock_all_queues(true);
4636 for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
4637 ++j, ++sqp) {
4638 k = find_first_bit(sqp->in_use_bm,
4639 sdebug_max_queue);
4640 if (k != sdebug_max_queue) {
4641 res = -EBUSY; /* queued commands */
4642 break;
4643 }
4644 }
4645 if (res > 0) {
c2206098 4646 sdebug_jdelay = jdelay;
773642d9 4647 sdebug_ndelay = 0;
cbf67842 4648 }
c4837394 4649 block_unblock_all_queues(false);
1da177e4 4650 }
cbf67842 4651 return res;
1da177e4
LT
4652 }
4653 return -EINVAL;
4654}
82069379 4655static DRIVER_ATTR_RW(delay);
1da177e4 4656
cbf67842
DG
4657static ssize_t ndelay_show(struct device_driver *ddp, char *buf)
4658{
773642d9 4659 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay);
cbf67842
DG
4660}
4661/* Returns -EBUSY if ndelay is being changed and commands are queued */
c2206098 4662/* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */
cbf67842 4663static ssize_t ndelay_store(struct device_driver *ddp, const char *buf,
fd32119b 4664 size_t count)
cbf67842 4665{
c4837394 4666 int ndelay, res;
cbf67842
DG
4667
4668 if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) &&
c4837394 4669 (ndelay >= 0) && (ndelay < (1000 * 1000 * 1000))) {
cbf67842 4670 res = count;
773642d9 4671 if (sdebug_ndelay != ndelay) {
c4837394
DG
4672 int j, k;
4673 struct sdebug_queue *sqp;
4674
4675 block_unblock_all_queues(true);
4676 for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
4677 ++j, ++sqp) {
4678 k = find_first_bit(sqp->in_use_bm,
4679 sdebug_max_queue);
4680 if (k != sdebug_max_queue) {
4681 res = -EBUSY; /* queued commands */
4682 break;
4683 }
4684 }
4685 if (res > 0) {
773642d9 4686 sdebug_ndelay = ndelay;
c2206098
DG
4687 sdebug_jdelay = ndelay ? JDELAY_OVERRIDDEN
4688 : DEF_JDELAY;
cbf67842 4689 }
c4837394 4690 block_unblock_all_queues(false);
cbf67842
DG
4691 }
4692 return res;
4693 }
4694 return -EINVAL;
4695}
4696static DRIVER_ATTR_RW(ndelay);
4697
82069379 4698static ssize_t opts_show(struct device_driver *ddp, char *buf)
1da177e4 4699{
773642d9 4700 return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts);
1da177e4
LT
4701}
4702
82069379
AM
4703static ssize_t opts_store(struct device_driver *ddp, const char *buf,
4704 size_t count)
1da177e4 4705{
9a051019 4706 int opts;
1da177e4
LT
4707 char work[20];
4708
9a051019
DG
4709 if (sscanf(buf, "%10s", work) == 1) {
4710 if (strncasecmp(work, "0x", 2) == 0) {
4711 if (kstrtoint(work + 2, 16, &opts) == 0)
1da177e4
LT
4712 goto opts_done;
4713 } else {
9a051019 4714 if (kstrtoint(work, 10, &opts) == 0)
1da177e4
LT
4715 goto opts_done;
4716 }
4717 }
4718 return -EINVAL;
4719opts_done:
773642d9
DG
4720 sdebug_opts = opts;
4721 sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
4722 sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts);
c4837394 4723 tweak_cmnd_count();
1da177e4
LT
4724 return count;
4725}
82069379 4726static DRIVER_ATTR_RW(opts);
1da177e4 4727
82069379 4728static ssize_t ptype_show(struct device_driver *ddp, char *buf)
1da177e4 4729{
773642d9 4730 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype);
1da177e4 4731}
82069379
AM
4732static ssize_t ptype_store(struct device_driver *ddp, const char *buf,
4733 size_t count)
1da177e4 4734{
9a051019 4735 int n;
1da177e4
LT
4736
4737 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
773642d9 4738 sdebug_ptype = n;
1da177e4
LT
4739 return count;
4740 }
4741 return -EINVAL;
4742}
82069379 4743static DRIVER_ATTR_RW(ptype);
1da177e4 4744
82069379 4745static ssize_t dsense_show(struct device_driver *ddp, char *buf)
1da177e4 4746{
773642d9 4747 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense);
1da177e4 4748}
82069379
AM
4749static ssize_t dsense_store(struct device_driver *ddp, const char *buf,
4750 size_t count)
1da177e4 4751{
9a051019 4752 int n;
1da177e4
LT
4753
4754 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
773642d9 4755 sdebug_dsense = n;
1da177e4
LT
4756 return count;
4757 }
4758 return -EINVAL;
4759}
82069379 4760static DRIVER_ATTR_RW(dsense);
1da177e4 4761
82069379 4762static ssize_t fake_rw_show(struct device_driver *ddp, char *buf)
23183910 4763{
773642d9 4764 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw);
23183910 4765}
82069379
AM
4766static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf,
4767 size_t count)
23183910 4768{
9a051019 4769 int n;
23183910
DG
4770
4771 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
cbf67842 4772 n = (n > 0);
773642d9
DG
4773 sdebug_fake_rw = (sdebug_fake_rw > 0);
4774 if (sdebug_fake_rw != n) {
cbf67842
DG
4775 if ((0 == n) && (NULL == fake_storep)) {
4776 unsigned long sz =
773642d9 4777 (unsigned long)sdebug_dev_size_mb *
cbf67842
DG
4778 1048576;
4779
7382f9d8 4780 fake_storep = vzalloc(sz);
cbf67842 4781 if (NULL == fake_storep) {
c1287970 4782 pr_err("out of memory, 9\n");
cbf67842
DG
4783 return -ENOMEM;
4784 }
cbf67842 4785 }
773642d9 4786 sdebug_fake_rw = n;
cbf67842 4787 }
23183910
DG
4788 return count;
4789 }
4790 return -EINVAL;
4791}
82069379 4792static DRIVER_ATTR_RW(fake_rw);
23183910 4793
82069379 4794static ssize_t no_lun_0_show(struct device_driver *ddp, char *buf)
c65b1445 4795{
773642d9 4796 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0);
c65b1445 4797}
82069379
AM
4798static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf,
4799 size_t count)
c65b1445 4800{
9a051019 4801 int n;
c65b1445
DG
4802
4803 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
773642d9 4804 sdebug_no_lun_0 = n;
c65b1445
DG
4805 return count;
4806 }
4807 return -EINVAL;
4808}
82069379 4809static DRIVER_ATTR_RW(no_lun_0);
c65b1445 4810
82069379 4811static ssize_t num_tgts_show(struct device_driver *ddp, char *buf)
1da177e4 4812{
773642d9 4813 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts);
1da177e4 4814}
82069379
AM
4815static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf,
4816 size_t count)
1da177e4 4817{
9a051019 4818 int n;
1da177e4
LT
4819
4820 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
773642d9 4821 sdebug_num_tgts = n;
1da177e4
LT
4822 sdebug_max_tgts_luns();
4823 return count;
4824 }
4825 return -EINVAL;
4826}
82069379 4827static DRIVER_ATTR_RW(num_tgts);
1da177e4 4828
82069379 4829static ssize_t dev_size_mb_show(struct device_driver *ddp, char *buf)
1da177e4 4830{
773642d9 4831 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb);
1da177e4 4832}
82069379 4833static DRIVER_ATTR_RO(dev_size_mb);
1da177e4 4834
82069379 4835static ssize_t num_parts_show(struct device_driver *ddp, char *buf)
1da177e4 4836{
773642d9 4837 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts);
1da177e4 4838}
82069379 4839static DRIVER_ATTR_RO(num_parts);
1da177e4 4840
82069379 4841static ssize_t every_nth_show(struct device_driver *ddp, char *buf)
1da177e4 4842{
773642d9 4843 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth);
1da177e4 4844}
82069379
AM
4845static ssize_t every_nth_store(struct device_driver *ddp, const char *buf,
4846 size_t count)
1da177e4 4847{
9a051019 4848 int nth;
1da177e4
LT
4849
4850 if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) {
773642d9 4851 sdebug_every_nth = nth;
c4837394
DG
4852 if (nth && !sdebug_statistics) {
4853 pr_info("every_nth needs statistics=1, set it\n");
4854 sdebug_statistics = true;
4855 }
4856 tweak_cmnd_count();
1da177e4
LT
4857 return count;
4858 }
4859 return -EINVAL;
4860}
82069379 4861static DRIVER_ATTR_RW(every_nth);
1da177e4 4862
82069379 4863static ssize_t max_luns_show(struct device_driver *ddp, char *buf)
1da177e4 4864{
773642d9 4865 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns);
1da177e4 4866}
82069379
AM
4867static ssize_t max_luns_store(struct device_driver *ddp, const char *buf,
4868 size_t count)
1da177e4 4869{
9a051019 4870 int n;
19c8ead7 4871 bool changed;
1da177e4
LT
4872
4873 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
8d039e22
DG
4874 if (n > 256) {
4875 pr_warn("max_luns can be no more than 256\n");
4876 return -EINVAL;
4877 }
773642d9
DG
4878 changed = (sdebug_max_luns != n);
4879 sdebug_max_luns = n;
1da177e4 4880 sdebug_max_tgts_luns();
773642d9 4881 if (changed && (sdebug_scsi_level >= 5)) { /* >= SPC-3 */
19c8ead7
EM
4882 struct sdebug_host_info *sdhp;
4883 struct sdebug_dev_info *dp;
4884
4885 spin_lock(&sdebug_host_list_lock);
4886 list_for_each_entry(sdhp, &sdebug_host_list,
4887 host_list) {
4888 list_for_each_entry(dp, &sdhp->dev_info_list,
4889 dev_list) {
4890 set_bit(SDEBUG_UA_LUNS_CHANGED,
4891 dp->uas_bm);
4892 }
4893 }
4894 spin_unlock(&sdebug_host_list_lock);
4895 }
1da177e4
LT
4896 return count;
4897 }
4898 return -EINVAL;
4899}
82069379 4900static DRIVER_ATTR_RW(max_luns);
1da177e4 4901
82069379 4902static ssize_t max_queue_show(struct device_driver *ddp, char *buf)
78d4e5a0 4903{
773642d9 4904 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue);
78d4e5a0 4905}
cbf67842
DG
4906/* N.B. max_queue can be changed while there are queued commands. In flight
4907 * commands beyond the new max_queue will be completed. */
82069379
AM
4908static ssize_t max_queue_store(struct device_driver *ddp, const char *buf,
4909 size_t count)
78d4e5a0 4910{
c4837394
DG
4911 int j, n, k, a;
4912 struct sdebug_queue *sqp;
78d4e5a0
DG
4913
4914 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) &&
c4837394
DG
4915 (n <= SDEBUG_CANQUEUE)) {
4916 block_unblock_all_queues(true);
4917 k = 0;
4918 for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
4919 ++j, ++sqp) {
4920 a = find_last_bit(sqp->in_use_bm, SDEBUG_CANQUEUE);
4921 if (a > k)
4922 k = a;
4923 }
773642d9 4924 sdebug_max_queue = n;
c4837394 4925 if (k == SDEBUG_CANQUEUE)
cbf67842
DG
4926 atomic_set(&retired_max_queue, 0);
4927 else if (k >= n)
4928 atomic_set(&retired_max_queue, k + 1);
4929 else
4930 atomic_set(&retired_max_queue, 0);
c4837394 4931 block_unblock_all_queues(false);
78d4e5a0
DG
4932 return count;
4933 }
4934 return -EINVAL;
4935}
82069379 4936static DRIVER_ATTR_RW(max_queue);
78d4e5a0 4937
82069379 4938static ssize_t no_uld_show(struct device_driver *ddp, char *buf)
78d4e5a0 4939{
773642d9 4940 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld);
78d4e5a0 4941}
82069379 4942static DRIVER_ATTR_RO(no_uld);
78d4e5a0 4943
82069379 4944static ssize_t scsi_level_show(struct device_driver *ddp, char *buf)
1da177e4 4945{
773642d9 4946 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level);
1da177e4 4947}
82069379 4948static DRIVER_ATTR_RO(scsi_level);
1da177e4 4949
82069379 4950static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf)
c65b1445 4951{
773642d9 4952 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb);
c65b1445 4953}
82069379
AM
4954static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf,
4955 size_t count)
c65b1445 4956{
9a051019 4957 int n;
0d01c5df 4958 bool changed;
c65b1445
DG
4959
4960 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
773642d9
DG
4961 changed = (sdebug_virtual_gb != n);
4962 sdebug_virtual_gb = n;
28898873 4963 sdebug_capacity = get_sdebug_capacity();
0d01c5df
DG
4964 if (changed) {
4965 struct sdebug_host_info *sdhp;
4966 struct sdebug_dev_info *dp;
4967
4bc6b634 4968 spin_lock(&sdebug_host_list_lock);
0d01c5df
DG
4969 list_for_each_entry(sdhp, &sdebug_host_list,
4970 host_list) {
4971 list_for_each_entry(dp, &sdhp->dev_info_list,
4972 dev_list) {
4973 set_bit(SDEBUG_UA_CAPACITY_CHANGED,
4974 dp->uas_bm);
4975 }
4976 }
4bc6b634 4977 spin_unlock(&sdebug_host_list_lock);
0d01c5df 4978 }
c65b1445
DG
4979 return count;
4980 }
4981 return -EINVAL;
4982}
82069379 4983static DRIVER_ATTR_RW(virtual_gb);
c65b1445 4984
82069379 4985static ssize_t add_host_show(struct device_driver *ddp, char *buf)
1da177e4 4986{
773642d9 4987 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_add_host);
1da177e4
LT
4988}
4989
fd32119b
DG
4990static int sdebug_add_adapter(void);
4991static void sdebug_remove_adapter(void);
4992
82069379
AM
4993static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
4994 size_t count)
1da177e4 4995{
f3df41cf 4996 int delta_hosts;
1da177e4 4997
f3df41cf 4998 if (sscanf(buf, "%d", &delta_hosts) != 1)
1da177e4 4999 return -EINVAL;
1da177e4
LT
5000 if (delta_hosts > 0) {
5001 do {
5002 sdebug_add_adapter();
5003 } while (--delta_hosts);
5004 } else if (delta_hosts < 0) {
5005 do {
5006 sdebug_remove_adapter();
5007 } while (++delta_hosts);
5008 }
5009 return count;
5010}
82069379 5011static DRIVER_ATTR_RW(add_host);
1da177e4 5012
82069379 5013static ssize_t vpd_use_hostno_show(struct device_driver *ddp, char *buf)
23183910 5014{
773642d9 5015 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno);
23183910 5016}
82069379
AM
5017static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf,
5018 size_t count)
23183910
DG
5019{
5020 int n;
5021
5022 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
773642d9 5023 sdebug_vpd_use_hostno = n;
23183910
DG
5024 return count;
5025 }
5026 return -EINVAL;
5027}
82069379 5028static DRIVER_ATTR_RW(vpd_use_hostno);
23183910 5029
c4837394
DG
5030static ssize_t statistics_show(struct device_driver *ddp, char *buf)
5031{
5032 return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_statistics);
5033}
5034static ssize_t statistics_store(struct device_driver *ddp, const char *buf,
5035 size_t count)
5036{
5037 int n;
5038
5039 if ((count > 0) && (sscanf(buf, "%d", &n) == 1) && (n >= 0)) {
5040 if (n > 0)
5041 sdebug_statistics = true;
5042 else {
5043 clear_queue_stats();
5044 sdebug_statistics = false;
5045 }
5046 return count;
5047 }
5048 return -EINVAL;
5049}
5050static DRIVER_ATTR_RW(statistics);
5051
82069379 5052static ssize_t sector_size_show(struct device_driver *ddp, char *buf)
597136ab 5053{
773642d9 5054 return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size);
597136ab 5055}
82069379 5056static DRIVER_ATTR_RO(sector_size);
597136ab 5057
c4837394
DG
5058static ssize_t submit_queues_show(struct device_driver *ddp, char *buf)
5059{
5060 return scnprintf(buf, PAGE_SIZE, "%d\n", submit_queues);
5061}
5062static DRIVER_ATTR_RO(submit_queues);
5063
82069379 5064static ssize_t dix_show(struct device_driver *ddp, char *buf)
c6a44287 5065{
773642d9 5066 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix);
c6a44287 5067}
82069379 5068static DRIVER_ATTR_RO(dix);
c6a44287 5069
82069379 5070static ssize_t dif_show(struct device_driver *ddp, char *buf)
c6a44287 5071{
773642d9 5072 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif);
c6a44287 5073}
82069379 5074static DRIVER_ATTR_RO(dif);
c6a44287 5075
82069379 5076static ssize_t guard_show(struct device_driver *ddp, char *buf)
c6a44287 5077{
773642d9 5078 return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard);
c6a44287 5079}
82069379 5080static DRIVER_ATTR_RO(guard);
c6a44287 5081
82069379 5082static ssize_t ato_show(struct device_driver *ddp, char *buf)
c6a44287 5083{
773642d9 5084 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato);
c6a44287 5085}
82069379 5086static DRIVER_ATTR_RO(ato);
c6a44287 5087
82069379 5088static ssize_t map_show(struct device_driver *ddp, char *buf)
44d92694
MP
5089{
5090 ssize_t count;
5091
5b94e232 5092 if (!scsi_debug_lbp())
44d92694
MP
5093 return scnprintf(buf, PAGE_SIZE, "0-%u\n",
5094 sdebug_store_sectors);
5095
c7badc90
TH
5096 count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
5097 (int)map_size, map_storep);
44d92694 5098 buf[count++] = '\n';
c7badc90 5099 buf[count] = '\0';
44d92694
MP
5100
5101 return count;
5102}
82069379 5103static DRIVER_ATTR_RO(map);
44d92694 5104
82069379 5105static ssize_t removable_show(struct device_driver *ddp, char *buf)
d986788b 5106{
773642d9 5107 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0);
d986788b 5108}
82069379
AM
5109static ssize_t removable_store(struct device_driver *ddp, const char *buf,
5110 size_t count)
d986788b
MP
5111{
5112 int n;
5113
5114 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
773642d9 5115 sdebug_removable = (n > 0);
d986788b
MP
5116 return count;
5117 }
5118 return -EINVAL;
5119}
82069379 5120static DRIVER_ATTR_RW(removable);
d986788b 5121
cbf67842
DG
5122static ssize_t host_lock_show(struct device_driver *ddp, char *buf)
5123{
773642d9 5124 return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock);
cbf67842 5125}
185dd232 5126/* N.B. sdebug_host_lock does nothing, kept for backward compatibility */
cbf67842
DG
5127static ssize_t host_lock_store(struct device_driver *ddp, const char *buf,
5128 size_t count)
5129{
185dd232 5130 int n;
cbf67842
DG
5131
5132 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
185dd232
DG
5133 sdebug_host_lock = (n > 0);
5134 return count;
cbf67842
DG
5135 }
5136 return -EINVAL;
5137}
5138static DRIVER_ATTR_RW(host_lock);
5139
c2248fc9
DG
5140static ssize_t strict_show(struct device_driver *ddp, char *buf)
5141{
773642d9 5142 return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict);
c2248fc9
DG
5143}
5144static ssize_t strict_store(struct device_driver *ddp, const char *buf,
5145 size_t count)
5146{
5147 int n;
5148
5149 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
773642d9 5150 sdebug_strict = (n > 0);
c2248fc9
DG
5151 return count;
5152 }
5153 return -EINVAL;
5154}
5155static DRIVER_ATTR_RW(strict);
5156
09ba24c1
DG
5157static ssize_t uuid_ctl_show(struct device_driver *ddp, char *buf)
5158{
5159 return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_uuid_ctl);
5160}
5161static DRIVER_ATTR_RO(uuid_ctl);
5162
9b760fd8
DG
5163static ssize_t cdb_len_show(struct device_driver *ddp, char *buf)
5164{
5165 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_cdb_len);
5166}
5167static ssize_t cdb_len_store(struct device_driver *ddp, const char *buf,
5168 size_t count)
5169{
5170 int ret, n;
5171
5172 ret = kstrtoint(buf, 0, &n);
5173 if (ret)
5174 return ret;
5175 sdebug_cdb_len = n;
5176 all_config_cdb_len();
5177 return count;
5178}
5179static DRIVER_ATTR_RW(cdb_len);
5180
cbf67842 5181
82069379 5182/* Note: The following array creates attribute files in the
23183910
DG
5183 /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
5184 files (over those found in the /sys/module/scsi_debug/parameters
5185 directory) is that auxiliary actions can be triggered when an attribute
5186 is changed. For example see: sdebug_add_host_store() above.
5187 */
6ecaff7f 5188
82069379
AM
5189static struct attribute *sdebug_drv_attrs[] = {
5190 &driver_attr_delay.attr,
5191 &driver_attr_opts.attr,
5192 &driver_attr_ptype.attr,
5193 &driver_attr_dsense.attr,
5194 &driver_attr_fake_rw.attr,
5195 &driver_attr_no_lun_0.attr,
5196 &driver_attr_num_tgts.attr,
5197 &driver_attr_dev_size_mb.attr,
5198 &driver_attr_num_parts.attr,
5199 &driver_attr_every_nth.attr,
5200 &driver_attr_max_luns.attr,
5201 &driver_attr_max_queue.attr,
5202 &driver_attr_no_uld.attr,
5203 &driver_attr_scsi_level.attr,
5204 &driver_attr_virtual_gb.attr,
5205 &driver_attr_add_host.attr,
5206 &driver_attr_vpd_use_hostno.attr,
5207 &driver_attr_sector_size.attr,
c4837394
DG
5208 &driver_attr_statistics.attr,
5209 &driver_attr_submit_queues.attr,
82069379
AM
5210 &driver_attr_dix.attr,
5211 &driver_attr_dif.attr,
5212 &driver_attr_guard.attr,
5213 &driver_attr_ato.attr,
5214 &driver_attr_map.attr,
5215 &driver_attr_removable.attr,
cbf67842
DG
5216 &driver_attr_host_lock.attr,
5217 &driver_attr_ndelay.attr,
c2248fc9 5218 &driver_attr_strict.attr,
09ba24c1 5219 &driver_attr_uuid_ctl.attr,
9b760fd8 5220 &driver_attr_cdb_len.attr,
82069379
AM
5221 NULL,
5222};
5223ATTRIBUTE_GROUPS(sdebug_drv);
1da177e4 5224
11ddceca 5225static struct device *pseudo_primary;
8dea0d02 5226
1da177e4
LT
5227static int __init scsi_debug_init(void)
5228{
5f2578e5 5229 unsigned long sz;
1da177e4
LT
5230 int host_to_add;
5231 int k;
6ecaff7f 5232 int ret;
1da177e4 5233
cbf67842
DG
5234 atomic_set(&retired_max_queue, 0);
5235
773642d9 5236 if (sdebug_ndelay >= 1000 * 1000 * 1000) {
c1287970 5237 pr_warn("ndelay must be less than 1 second, ignored\n");
773642d9
DG
5238 sdebug_ndelay = 0;
5239 } else if (sdebug_ndelay > 0)
c2206098 5240 sdebug_jdelay = JDELAY_OVERRIDDEN;
cbf67842 5241
773642d9 5242 switch (sdebug_sector_size) {
597136ab
MP
5243 case 512:
5244 case 1024:
5245 case 2048:
5246 case 4096:
5247 break;
5248 default:
773642d9 5249 pr_err("invalid sector_size %d\n", sdebug_sector_size);
597136ab
MP
5250 return -EINVAL;
5251 }
5252
773642d9 5253 switch (sdebug_dif) {
8475c811 5254 case T10_PI_TYPE0_PROTECTION:
f46eb0e9 5255 break;
8475c811
CH
5256 case T10_PI_TYPE1_PROTECTION:
5257 case T10_PI_TYPE2_PROTECTION:
5258 case T10_PI_TYPE3_PROTECTION:
f46eb0e9 5259 have_dif_prot = true;
c6a44287
MP
5260 break;
5261
5262 default:
c1287970 5263 pr_err("dif must be 0, 1, 2 or 3\n");
c6a44287
MP
5264 return -EINVAL;
5265 }
5266
aa5334c4
ML
5267 if (sdebug_num_tgts < 0) {
5268 pr_err("num_tgts must be >= 0\n");
5269 return -EINVAL;
5270 }
5271
773642d9 5272 if (sdebug_guard > 1) {
c1287970 5273 pr_err("guard must be 0 or 1\n");
c6a44287
MP
5274 return -EINVAL;
5275 }
5276
773642d9 5277 if (sdebug_ato > 1) {
c1287970 5278 pr_err("ato must be 0 or 1\n");
c6a44287
MP
5279 return -EINVAL;
5280 }
5281
773642d9
DG
5282 if (sdebug_physblk_exp > 15) {
5283 pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp);
ea61fca5
MP
5284 return -EINVAL;
5285 }
8d039e22
DG
5286 if (sdebug_max_luns > 256) {
5287 pr_warn("max_luns can be no more than 256, use default\n");
5288 sdebug_max_luns = DEF_MAX_LUNS;
5289 }
ea61fca5 5290
773642d9
DG
5291 if (sdebug_lowest_aligned > 0x3fff) {
5292 pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned);
ea61fca5
MP
5293 return -EINVAL;
5294 }
5295
c4837394
DG
5296 if (submit_queues < 1) {
5297 pr_err("submit_queues must be 1 or more\n");
5298 return -EINVAL;
5299 }
5300 sdebug_q_arr = kcalloc(submit_queues, sizeof(struct sdebug_queue),
5301 GFP_KERNEL);
5302 if (sdebug_q_arr == NULL)
5303 return -ENOMEM;
5304 for (k = 0; k < submit_queues; ++k)
5305 spin_lock_init(&sdebug_q_arr[k].qc_lock);
5306
773642d9
DG
5307 if (sdebug_dev_size_mb < 1)
5308 sdebug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
5309 sz = (unsigned long)sdebug_dev_size_mb * 1048576;
5310 sdebug_store_sectors = sz / sdebug_sector_size;
28898873 5311 sdebug_capacity = get_sdebug_capacity();
1da177e4
LT
5312
5313 /* play around with geometry, don't waste too much on track 0 */
5314 sdebug_heads = 8;
5315 sdebug_sectors_per = 32;
773642d9 5316 if (sdebug_dev_size_mb >= 256)
1da177e4 5317 sdebug_heads = 64;
773642d9 5318 else if (sdebug_dev_size_mb >= 16)
fa785f0a 5319 sdebug_heads = 32;
1da177e4
LT
5320 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
5321 (sdebug_sectors_per * sdebug_heads);
5322 if (sdebug_cylinders_per >= 1024) {
5323 /* other LLDs do this; implies >= 1GB ram disk ... */
5324 sdebug_heads = 255;
5325 sdebug_sectors_per = 63;
5326 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
5327 (sdebug_sectors_per * sdebug_heads);
5328 }
5329
b01f6f83 5330 if (sdebug_fake_rw == 0) {
7382f9d8 5331 fake_storep = vzalloc(sz);
cbf67842 5332 if (NULL == fake_storep) {
c1287970 5333 pr_err("out of memory, 1\n");
c4837394
DG
5334 ret = -ENOMEM;
5335 goto free_q_arr;
cbf67842 5336 }
773642d9 5337 if (sdebug_num_parts > 0)
cbf67842 5338 sdebug_build_parts(fake_storep, sz);
1da177e4 5339 }
1da177e4 5340
773642d9 5341 if (sdebug_dix) {
c6a44287
MP
5342 int dif_size;
5343
6ebf105c 5344 dif_size = sdebug_store_sectors * sizeof(struct t10_pi_tuple);
c6a44287
MP
5345 dif_storep = vmalloc(dif_size);
5346
c1287970 5347 pr_err("dif_storep %u bytes @ %p\n", dif_size, dif_storep);
c6a44287
MP
5348
5349 if (dif_storep == NULL) {
c1287970 5350 pr_err("out of mem. (DIX)\n");
c6a44287
MP
5351 ret = -ENOMEM;
5352 goto free_vm;
5353 }
5354
5355 memset(dif_storep, 0xff, dif_size);
5356 }
5357
5b94e232
MP
5358 /* Logical Block Provisioning */
5359 if (scsi_debug_lbp()) {
773642d9
DG
5360 sdebug_unmap_max_blocks =
5361 clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU);
6014759c 5362
773642d9
DG
5363 sdebug_unmap_max_desc =
5364 clamp(sdebug_unmap_max_desc, 0U, 256U);
6014759c 5365
773642d9
DG
5366 sdebug_unmap_granularity =
5367 clamp(sdebug_unmap_granularity, 1U, 0xffffffffU);
6014759c 5368
773642d9
DG
5369 if (sdebug_unmap_alignment &&
5370 sdebug_unmap_granularity <=
5371 sdebug_unmap_alignment) {
c1287970 5372 pr_err("ERR: unmap_granularity <= unmap_alignment\n");
c4837394
DG
5373 ret = -EINVAL;
5374 goto free_vm;
44d92694
MP
5375 }
5376
b90ebc3d 5377 map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1;
42bc47b3
KC
5378 map_storep = vmalloc(array_size(sizeof(long),
5379 BITS_TO_LONGS(map_size)));
44d92694 5380
c1287970 5381 pr_info("%lu provisioning blocks\n", map_size);
44d92694
MP
5382
5383 if (map_storep == NULL) {
c1287970 5384 pr_err("out of mem. (MAP)\n");
44d92694
MP
5385 ret = -ENOMEM;
5386 goto free_vm;
5387 }
5388
b90ebc3d 5389 bitmap_zero(map_storep, map_size);
44d92694
MP
5390
5391 /* Map first 1KB for partition table */
773642d9 5392 if (sdebug_num_parts)
44d92694
MP
5393 map_region(0, 2);
5394 }
5395
9b906779
NB
5396 pseudo_primary = root_device_register("pseudo_0");
5397 if (IS_ERR(pseudo_primary)) {
c1287970 5398 pr_warn("root_device_register() error\n");
9b906779 5399 ret = PTR_ERR(pseudo_primary);
6ecaff7f
RD
5400 goto free_vm;
5401 }
5402 ret = bus_register(&pseudo_lld_bus);
5403 if (ret < 0) {
c1287970 5404 pr_warn("bus_register error: %d\n", ret);
6ecaff7f
RD
5405 goto dev_unreg;
5406 }
5407 ret = driver_register(&sdebug_driverfs_driver);
5408 if (ret < 0) {
c1287970 5409 pr_warn("driver_register error: %d\n", ret);
6ecaff7f
RD
5410 goto bus_unreg;
5411 }
1da177e4 5412
773642d9
DG
5413 host_to_add = sdebug_add_host;
5414 sdebug_add_host = 0;
1da177e4 5415
9a051019
DG
5416 for (k = 0; k < host_to_add; k++) {
5417 if (sdebug_add_adapter()) {
c1287970 5418 pr_err("sdebug_add_adapter failed k=%d\n", k);
9a051019
DG
5419 break;
5420 }
5421 }
1da177e4 5422
773642d9
DG
5423 if (sdebug_verbose)
5424 pr_info("built %d host(s)\n", sdebug_add_host);
c1287970 5425
1da177e4 5426 return 0;
6ecaff7f 5427
6ecaff7f
RD
5428bus_unreg:
5429 bus_unregister(&pseudo_lld_bus);
5430dev_unreg:
9b906779 5431 root_device_unregister(pseudo_primary);
6ecaff7f 5432free_vm:
de232af6
TW
5433 vfree(map_storep);
5434 vfree(dif_storep);
6ecaff7f 5435 vfree(fake_storep);
c4837394
DG
5436free_q_arr:
5437 kfree(sdebug_q_arr);
6ecaff7f 5438 return ret;
1da177e4
LT
5439}
5440
5441static void __exit scsi_debug_exit(void)
5442{
773642d9 5443 int k = sdebug_add_host;
1da177e4
LT
5444
5445 stop_all_queued();
5446 for (; k; k--)
5447 sdebug_remove_adapter();
52ab9768 5448 free_all_queued();
1da177e4
LT
5449 driver_unregister(&sdebug_driverfs_driver);
5450 bus_unregister(&pseudo_lld_bus);
9b906779 5451 root_device_unregister(pseudo_primary);
1da177e4 5452
4d2b496f 5453 vfree(map_storep);
de232af6 5454 vfree(dif_storep);
1da177e4 5455 vfree(fake_storep);
c4837394 5456 kfree(sdebug_q_arr);
1da177e4
LT
5457}
5458
5459device_initcall(scsi_debug_init);
5460module_exit(scsi_debug_exit);
5461
91d4c752 5462static void sdebug_release_adapter(struct device *dev)
1da177e4 5463{
9a051019 5464 struct sdebug_host_info *sdbg_host;
1da177e4
LT
5465
5466 sdbg_host = to_sdebug_host(dev);
9a051019 5467 kfree(sdbg_host);
1da177e4
LT
5468}
5469
5470static int sdebug_add_adapter(void)
5471{
5472 int k, devs_per_host;
9a051019
DG
5473 int error = 0;
5474 struct sdebug_host_info *sdbg_host;
8b40228f 5475 struct sdebug_dev_info *sdbg_devinfo, *tmp;
1da177e4 5476
9a051019
DG
5477 sdbg_host = kzalloc(sizeof(*sdbg_host), GFP_KERNEL);
5478 if (sdbg_host == NULL) {
c1287970 5479 pr_err("out of memory at line %d\n", __LINE__);
9a051019
DG
5480 return -ENOMEM;
5481 }
1da177e4 5482
9a051019 5483 INIT_LIST_HEAD(&sdbg_host->dev_info_list);
1da177e4 5484
773642d9 5485 devs_per_host = sdebug_num_tgts * sdebug_max_luns;
9a051019 5486 for (k = 0; k < devs_per_host; k++) {
5cb2fc06
FT
5487 sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL);
5488 if (!sdbg_devinfo) {
c1287970 5489 pr_err("out of memory at line %d\n", __LINE__);
9a051019 5490 error = -ENOMEM;
1da177e4 5491 goto clean;
9a051019
DG
5492 }
5493 }
1da177e4 5494
9a051019
DG
5495 spin_lock(&sdebug_host_list_lock);
5496 list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
5497 spin_unlock(&sdebug_host_list_lock);
1da177e4 5498
9a051019
DG
5499 sdbg_host->dev.bus = &pseudo_lld_bus;
5500 sdbg_host->dev.parent = pseudo_primary;
5501 sdbg_host->dev.release = &sdebug_release_adapter;
773642d9 5502 dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_add_host);
1da177e4 5503
9a051019 5504 error = device_register(&sdbg_host->dev);
1da177e4 5505
9a051019 5506 if (error)
1da177e4
LT
5507 goto clean;
5508
773642d9 5509 ++sdebug_add_host;
9a051019 5510 return error;
1da177e4
LT
5511
5512clean:
8b40228f
FT
5513 list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
5514 dev_list) {
1da177e4
LT
5515 list_del(&sdbg_devinfo->dev_list);
5516 kfree(sdbg_devinfo);
5517 }
5518
5519 kfree(sdbg_host);
9a051019 5520 return error;
1da177e4
LT
5521}
5522
5523static void sdebug_remove_adapter(void)
5524{
9a051019 5525 struct sdebug_host_info *sdbg_host = NULL;
1da177e4 5526
9a051019
DG
5527 spin_lock(&sdebug_host_list_lock);
5528 if (!list_empty(&sdebug_host_list)) {
5529 sdbg_host = list_entry(sdebug_host_list.prev,
5530 struct sdebug_host_info, host_list);
1da177e4
LT
5531 list_del(&sdbg_host->host_list);
5532 }
9a051019 5533 spin_unlock(&sdebug_host_list_lock);
1da177e4
LT
5534
5535 if (!sdbg_host)
5536 return;
5537
773642d9
DG
5538 device_unregister(&sdbg_host->dev);
5539 --sdebug_add_host;
1da177e4
LT
5540}
5541
fd32119b 5542static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth)
cbf67842
DG
5543{
5544 int num_in_q = 0;
cbf67842
DG
5545 struct sdebug_dev_info *devip;
5546
c4837394 5547 block_unblock_all_queues(true);
cbf67842
DG
5548 devip = (struct sdebug_dev_info *)sdev->hostdata;
5549 if (NULL == devip) {
c4837394 5550 block_unblock_all_queues(false);
cbf67842
DG
5551 return -ENODEV;
5552 }
5553 num_in_q = atomic_read(&devip->num_in_q);
c40ecc12
CH
5554
5555 if (qdepth < 1)
5556 qdepth = 1;
c4837394
DG
5557 /* allow to exceed max host qc_arr elements for testing */
5558 if (qdepth > SDEBUG_CANQUEUE + 10)
5559 qdepth = SDEBUG_CANQUEUE + 10;
db5ed4df 5560 scsi_change_queue_depth(sdev, qdepth);
c40ecc12 5561
773642d9 5562 if (SDEBUG_OPT_Q_NOISE & sdebug_opts) {
c4837394 5563 sdev_printk(KERN_INFO, sdev, "%s: qdepth=%d, num_in_q=%d\n",
c40ecc12 5564 __func__, qdepth, num_in_q);
cbf67842 5565 }
c4837394 5566 block_unblock_all_queues(false);
cbf67842
DG
5567 return sdev->queue_depth;
5568}
5569
c4837394 5570static bool fake_timeout(struct scsi_cmnd *scp)
817fd66b 5571{
c4837394 5572 if (0 == (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth))) {
773642d9
DG
5573 if (sdebug_every_nth < -1)
5574 sdebug_every_nth = -1;
5575 if (SDEBUG_OPT_TIMEOUT & sdebug_opts)
c4837394 5576 return true; /* ignore command causing timeout */
773642d9 5577 else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts &&
817fd66b 5578 scsi_medium_access_command(scp))
c4837394 5579 return true; /* time out reads and writes */
817fd66b 5580 }
c4837394 5581 return false;
817fd66b
DG
5582}
5583
7ee6d1b4
BVA
5584static bool fake_host_busy(struct scsi_cmnd *scp)
5585{
5586 return (sdebug_opts & SDEBUG_OPT_HOST_BUSY) &&
5587 (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) == 0;
5588}
5589
fd32119b
DG
5590static int scsi_debug_queuecommand(struct Scsi_Host *shost,
5591 struct scsi_cmnd *scp)
c2248fc9
DG
5592{
5593 u8 sdeb_i;
5594 struct scsi_device *sdp = scp->device;
5595 const struct opcode_info_t *oip;
5596 const struct opcode_info_t *r_oip;
5597 struct sdebug_dev_info *devip;
5598 u8 *cmd = scp->cmnd;
5599 int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
f66b8517 5600 int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *) = NULL;
c2248fc9
DG
5601 int k, na;
5602 int errsts = 0;
c2248fc9
DG
5603 u32 flags;
5604 u16 sa;
5605 u8 opcode = cmd[0];
5606 bool has_wlun_rl;
c2248fc9
DG
5607
5608 scsi_set_resid(scp, 0);
c4837394
DG
5609 if (sdebug_statistics)
5610 atomic_inc(&sdebug_cmnd_count);
f46eb0e9
DG
5611 if (unlikely(sdebug_verbose &&
5612 !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) {
c2248fc9
DG
5613 char b[120];
5614 int n, len, sb;
5615
5616 len = scp->cmd_len;
5617 sb = (int)sizeof(b);
5618 if (len > 32)
5619 strcpy(b, "too long, over 32 bytes");
5620 else {
5621 for (k = 0, n = 0; k < len && n < sb; ++k)
5622 n += scnprintf(b + n, sb - n, "%02x ",
5623 (u32)cmd[k]);
5624 }
458df78b
BVA
5625 sdev_printk(KERN_INFO, sdp, "%s: tag=%#x, cmd %s\n", my_name,
5626 blk_mq_unique_tag(scp->request), b);
c2248fc9 5627 }
7ee6d1b4
BVA
5628 if (fake_host_busy(scp))
5629 return SCSI_MLQUEUE_HOST_BUSY;
34d55434 5630 has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS);
f46eb0e9
DG
5631 if (unlikely((sdp->lun >= sdebug_max_luns) && !has_wlun_rl))
5632 goto err_out;
c2248fc9
DG
5633
5634 sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */
5635 oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */
5636 devip = (struct sdebug_dev_info *)sdp->hostdata;
f46eb0e9
DG
5637 if (unlikely(!devip)) {
5638 devip = find_build_dev_info(sdp);
c2248fc9 5639 if (NULL == devip)
f46eb0e9 5640 goto err_out;
c2248fc9
DG
5641 }
5642 na = oip->num_attached;
5643 r_pfp = oip->pfp;
5644 if (na) { /* multiple commands with this opcode */
5645 r_oip = oip;
5646 if (FF_SA & r_oip->flags) {
5647 if (F_SA_LOW & oip->flags)
5648 sa = 0x1f & cmd[1];
5649 else
5650 sa = get_unaligned_be16(cmd + 8);
5651 for (k = 0; k <= na; oip = r_oip->arrp + k++) {
5652 if (opcode == oip->opcode && sa == oip->sa)
5653 break;
5654 }
5655 } else { /* since no service action only check opcode */
5656 for (k = 0; k <= na; oip = r_oip->arrp + k++) {
5657 if (opcode == oip->opcode)
5658 break;
5659 }
5660 }
5661 if (k > na) {
5662 if (F_SA_LOW & r_oip->flags)
5663 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4);
5664 else if (F_SA_HIGH & r_oip->flags)
5665 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7);
5666 else
5667 mk_sense_invalid_opcode(scp);
5668 goto check_cond;
5669 }
5670 } /* else (when na==0) we assume the oip is a match */
5671 flags = oip->flags;
f46eb0e9 5672 if (unlikely(F_INV_OP & flags)) {
c2248fc9
DG
5673 mk_sense_invalid_opcode(scp);
5674 goto check_cond;
5675 }
f46eb0e9 5676 if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) {
773642d9
DG
5677 if (sdebug_verbose)
5678 sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n",
5679 my_name, opcode, " supported for wlun");
c2248fc9
DG
5680 mk_sense_invalid_opcode(scp);
5681 goto check_cond;
5682 }
f46eb0e9 5683 if (unlikely(sdebug_strict)) { /* check cdb against mask */
c2248fc9
DG
5684 u8 rem;
5685 int j;
5686
5687 for (k = 1; k < oip->len_mask[0] && k < 16; ++k) {
5688 rem = ~oip->len_mask[k] & cmd[k];
5689 if (rem) {
5690 for (j = 7; j >= 0; --j, rem <<= 1) {
5691 if (0x80 & rem)
5692 break;
5693 }
5694 mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j);
5695 goto check_cond;
5696 }
5697 }
5698 }
f46eb0e9 5699 if (unlikely(!(F_SKIP_UA & flags) &&
b01f6f83
DG
5700 find_first_bit(devip->uas_bm,
5701 SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) {
f46eb0e9 5702 errsts = make_ua(scp, devip);
c2248fc9
DG
5703 if (errsts)
5704 goto check_cond;
5705 }
c4837394 5706 if (unlikely((F_M_ACCESS & flags) && atomic_read(&devip->stopped))) {
c2248fc9 5707 mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2);
773642d9 5708 if (sdebug_verbose)
c2248fc9
DG
5709 sdev_printk(KERN_INFO, sdp, "%s reports: Not ready: "
5710 "%s\n", my_name, "initializing command "
5711 "required");
5712 errsts = check_condition_result;
5713 goto fini;
5714 }
773642d9 5715 if (sdebug_fake_rw && (F_FAKE_RW & flags))
c2248fc9 5716 goto fini;
f46eb0e9 5717 if (unlikely(sdebug_every_nth)) {
c4837394 5718 if (fake_timeout(scp))
c2248fc9
DG
5719 return 0; /* ignore command: make trouble */
5720 }
f46eb0e9 5721 if (likely(oip->pfp))
f66b8517
MW
5722 pfp = oip->pfp; /* calls a resp_* function */
5723 else
5724 pfp = r_pfp; /* if leaf function ptr NULL, try the root's */
c2248fc9
DG
5725
5726fini:
10bde980 5727 if (F_DELAY_OVERR & flags)
f66b8517 5728 return schedule_resp(scp, devip, errsts, pfp, 0, 0);
75aa3209
DG
5729 else if ((flags & F_LONG_DELAY) && (sdebug_jdelay > 0 ||
5730 sdebug_ndelay > 10000)) {
80c49563 5731 /*
75aa3209
DG
5732 * Skip long delays if ndelay <= 10 microseconds. Otherwise
5733 * for Start Stop Unit (SSU) want at least 1 second delay and
5734 * if sdebug_jdelay>1 want a long delay of that many seconds.
5735 * For Synchronize Cache want 1/20 of SSU's delay.
80c49563
DG
5736 */
5737 int jdelay = (sdebug_jdelay < 2) ? 1 : sdebug_jdelay;
4f2c8bf6 5738 int denom = (flags & F_SYNC_DELAY) ? 20 : 1;
80c49563 5739
4f2c8bf6 5740 jdelay = mult_frac(USER_HZ * jdelay, HZ, denom * USER_HZ);
f66b8517 5741 return schedule_resp(scp, devip, errsts, pfp, jdelay, 0);
80c49563 5742 } else
f66b8517 5743 return schedule_resp(scp, devip, errsts, pfp, sdebug_jdelay,
10bde980 5744 sdebug_ndelay);
c2248fc9 5745check_cond:
f66b8517 5746 return schedule_resp(scp, devip, check_condition_result, NULL, 0, 0);
f46eb0e9 5747err_out:
f66b8517 5748 return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, NULL, 0, 0);
c2248fc9
DG
5749}
5750
9e603ca0 5751static struct scsi_host_template sdebug_driver_template = {
c8ed555a
AV
5752 .show_info = scsi_debug_show_info,
5753 .write_info = scsi_debug_write_info,
9e603ca0
FT
5754 .proc_name = sdebug_proc_name,
5755 .name = "SCSI DEBUG",
5756 .info = scsi_debug_info,
5757 .slave_alloc = scsi_debug_slave_alloc,
5758 .slave_configure = scsi_debug_slave_configure,
5759 .slave_destroy = scsi_debug_slave_destroy,
5760 .ioctl = scsi_debug_ioctl,
185dd232 5761 .queuecommand = scsi_debug_queuecommand,
cbf67842 5762 .change_queue_depth = sdebug_change_qdepth,
9e603ca0 5763 .eh_abort_handler = scsi_debug_abort,
9e603ca0 5764 .eh_device_reset_handler = scsi_debug_device_reset,
cbf67842
DG
5765 .eh_target_reset_handler = scsi_debug_target_reset,
5766 .eh_bus_reset_handler = scsi_debug_bus_reset,
9e603ca0 5767 .eh_host_reset_handler = scsi_debug_host_reset,
c4837394 5768 .can_queue = SDEBUG_CANQUEUE,
9e603ca0 5769 .this_id = 7,
65e8617f 5770 .sg_tablesize = SG_MAX_SEGMENTS,
cbf67842 5771 .cmd_per_lun = DEF_CMD_PER_LUN,
6bb5e6e7 5772 .max_sectors = -1U,
50c2e910 5773 .max_segment_size = -1U,
9e603ca0 5774 .module = THIS_MODULE,
c40ecc12 5775 .track_queue_depth = 1,
9e603ca0
FT
5776};
5777
91d4c752 5778static int sdebug_driver_probe(struct device *dev)
1da177e4 5779{
22017ed2
DG
5780 int error = 0;
5781 struct sdebug_host_info *sdbg_host;
5782 struct Scsi_Host *hpnt;
f46eb0e9 5783 int hprot;
1da177e4
LT
5784
5785 sdbg_host = to_sdebug_host(dev);
5786
773642d9 5787 sdebug_driver_template.can_queue = sdebug_max_queue;
2a3d4eb8 5788 if (!sdebug_clustering)
4af14d11
CH
5789 sdebug_driver_template.dma_boundary = PAGE_SIZE - 1;
5790
78d4e5a0
DG
5791 hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
5792 if (NULL == hpnt) {
c1287970 5793 pr_err("scsi_host_alloc failed\n");
78d4e5a0 5794 error = -ENODEV;
1da177e4 5795 return error;
78d4e5a0 5796 }
c4837394 5797 if (submit_queues > nr_cpu_ids) {
9b130ad5 5798 pr_warn("%s: trim submit_queues (was %d) to nr_cpu_ids=%u\n",
c4837394
DG
5799 my_name, submit_queues, nr_cpu_ids);
5800 submit_queues = nr_cpu_ids;
5801 }
5802 /* Decide whether to tell scsi subsystem that we want mq */
5803 /* Following should give the same answer for each host */
f664a3cc 5804 hpnt->nr_hw_queues = submit_queues;
1da177e4 5805
9a051019 5806 sdbg_host->shost = hpnt;
1da177e4 5807 *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
773642d9
DG
5808 if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id))
5809 hpnt->max_id = sdebug_num_tgts + 1;
1da177e4 5810 else
773642d9
DG
5811 hpnt->max_id = sdebug_num_tgts;
5812 /* = sdebug_max_luns; */
f2d3fd29 5813 hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
1da177e4 5814
f46eb0e9 5815 hprot = 0;
c6a44287 5816
773642d9 5817 switch (sdebug_dif) {
c6a44287 5818
8475c811 5819 case T10_PI_TYPE1_PROTECTION:
f46eb0e9 5820 hprot = SHOST_DIF_TYPE1_PROTECTION;
773642d9 5821 if (sdebug_dix)
f46eb0e9 5822 hprot |= SHOST_DIX_TYPE1_PROTECTION;
c6a44287
MP
5823 break;
5824
8475c811 5825 case T10_PI_TYPE2_PROTECTION:
f46eb0e9 5826 hprot = SHOST_DIF_TYPE2_PROTECTION;
773642d9 5827 if (sdebug_dix)
f46eb0e9 5828 hprot |= SHOST_DIX_TYPE2_PROTECTION;
c6a44287
MP
5829 break;
5830
8475c811 5831 case T10_PI_TYPE3_PROTECTION:
f46eb0e9 5832 hprot = SHOST_DIF_TYPE3_PROTECTION;
773642d9 5833 if (sdebug_dix)
f46eb0e9 5834 hprot |= SHOST_DIX_TYPE3_PROTECTION;
c6a44287
MP
5835 break;
5836
5837 default:
773642d9 5838 if (sdebug_dix)
f46eb0e9 5839 hprot |= SHOST_DIX_TYPE0_PROTECTION;
c6a44287
MP
5840 break;
5841 }
5842
f46eb0e9 5843 scsi_host_set_prot(hpnt, hprot);
c6a44287 5844
f46eb0e9
DG
5845 if (have_dif_prot || sdebug_dix)
5846 pr_info("host protection%s%s%s%s%s%s%s\n",
5847 (hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
5848 (hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
5849 (hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
5850 (hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
5851 (hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
5852 (hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
5853 (hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
c6a44287 5854
773642d9 5855 if (sdebug_guard == 1)
c6a44287
MP
5856 scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP);
5857 else
5858 scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC);
5859
773642d9
DG
5860 sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts);
5861 sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & sdebug_opts);
c4837394
DG
5862 if (sdebug_every_nth) /* need stats counters for every_nth */
5863 sdebug_statistics = true;
9a051019
DG
5864 error = scsi_add_host(hpnt, &sdbg_host->dev);
5865 if (error) {
c1287970 5866 pr_err("scsi_add_host failed\n");
9a051019 5867 error = -ENODEV;
1da177e4 5868 scsi_host_put(hpnt);
9a051019 5869 } else
1da177e4
LT
5870 scsi_scan_host(hpnt);
5871
cbf67842 5872 return error;
1da177e4
LT
5873}
5874
91d4c752 5875static int sdebug_driver_remove(struct device *dev)
1da177e4 5876{
9a051019 5877 struct sdebug_host_info *sdbg_host;
8b40228f 5878 struct sdebug_dev_info *sdbg_devinfo, *tmp;
1da177e4
LT
5879
5880 sdbg_host = to_sdebug_host(dev);
5881
5882 if (!sdbg_host) {
c1287970 5883 pr_err("Unable to locate host info\n");
1da177e4
LT
5884 return -ENODEV;
5885 }
5886
9a051019 5887 scsi_remove_host(sdbg_host->shost);
1da177e4 5888
8b40228f
FT
5889 list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
5890 dev_list) {
9a051019
DG
5891 list_del(&sdbg_devinfo->dev_list);
5892 kfree(sdbg_devinfo);
5893 }
1da177e4 5894
9a051019
DG
5895 scsi_host_put(sdbg_host->shost);
5896 return 0;
1da177e4
LT
5897}
5898
8dea0d02
FT
5899static int pseudo_lld_bus_match(struct device *dev,
5900 struct device_driver *dev_driver)
1da177e4 5901{
8dea0d02 5902 return 1;
1da177e4 5903}
8dea0d02
FT
5904
5905static struct bus_type pseudo_lld_bus = {
5906 .name = "pseudo",
5907 .match = pseudo_lld_bus_match,
5908 .probe = sdebug_driver_probe,
5909 .remove = sdebug_driver_remove,
82069379 5910 .drv_groups = sdebug_drv_groups,
8dea0d02 5911};