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