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