scsi_debug: change SCSI command parser to table driven
[deliverable/linux.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 *
9 * This version is more generic, simulating a variable number of disk
23183910
DG
10 * (or disk like devices) sharing a common amount of RAM. To be more
11 * realistic, the simulated devices have the transport attributes of
12 * SAS disks.
1da177e4
LT
13 *
14 *
78d4e5a0 15 * For documentation see http://sg.danny.cz/sg/sdebug26.html
1da177e4
LT
16 *
17 * D. Gilbert (dpg) work for Magneto-Optical device test [20010421]
18 * dpg: work for devfs large number of disks [20010809]
19 * forked for lk 2.5 series [20011216, 20020101]
20 * use vmalloc() more inquiry+mode_sense [20020302]
21 * add timers for delayed responses [20020721]
22 * Patrick Mansfield <patmans@us.ibm.com> max_luns+scsi_level [20021031]
23 * Mike Anderson <andmike@us.ibm.com> sysfs work [20021118]
24 * dpg: change style of boot options to "scsi_debug.num_tgts=2" and
25 * module options to "modprobe scsi_debug num_tgts=2" [20021221]
26 */
27
1da177e4
LT
28#include <linux/module.h>
29
30#include <linux/kernel.h>
1da177e4
LT
31#include <linux/errno.h>
32#include <linux/timer.h>
5a0e3ad6 33#include <linux/slab.h>
1da177e4
LT
34#include <linux/types.h>
35#include <linux/string.h>
36#include <linux/genhd.h>
37#include <linux/fs.h>
38#include <linux/init.h>
39#include <linux/proc_fs.h>
1da177e4
LT
40#include <linux/vmalloc.h>
41#include <linux/moduleparam.h>
852e034d 42#include <linux/scatterlist.h>
1da177e4 43#include <linux/blkdev.h>
c6a44287 44#include <linux/crc-t10dif.h>
cbf67842
DG
45#include <linux/spinlock.h>
46#include <linux/interrupt.h>
47#include <linux/atomic.h>
48#include <linux/hrtimer.h>
c6a44287
MP
49
50#include <net/checksum.h>
9ff26eef 51
44d92694
MP
52#include <asm/unaligned.h>
53
9ff26eef
FT
54#include <scsi/scsi.h>
55#include <scsi/scsi_cmnd.h>
56#include <scsi/scsi_device.h>
1da177e4
LT
57#include <scsi/scsi_host.h>
58#include <scsi/scsicam.h>
a34c4e98 59#include <scsi/scsi_eh.h>
cbf67842 60#include <scsi/scsi_tcq.h>
395cef03 61#include <scsi/scsi_dbg.h>
1da177e4 62
c6a44287 63#include "sd.h"
1da177e4 64#include "scsi_logging.h"
1da177e4 65
22017ed2
DG
66#define SCSI_DEBUG_VERSION "1.85"
67static const char *scsi_debug_version_date = "20141022";
cbf67842
DG
68
69#define MY_NAME "scsi_debug"
1da177e4 70
6f3cbf55 71/* Additional Sense Code (ASC) */
c65b1445
DG
72#define NO_ADDITIONAL_SENSE 0x0
73#define LOGICAL_UNIT_NOT_READY 0x4
c2248fc9 74#define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8
1da177e4 75#define UNRECOVERED_READ_ERR 0x11
c65b1445 76#define PARAMETER_LIST_LENGTH_ERR 0x1a
1da177e4 77#define INVALID_OPCODE 0x20
22017ed2 78#define LBA_OUT_OF_RANGE 0x21
1da177e4 79#define INVALID_FIELD_IN_CDB 0x24
c65b1445 80#define INVALID_FIELD_IN_PARAM_LIST 0x26
cbf67842
DG
81#define UA_RESET_ASC 0x29
82#define UA_CHANGED_ASC 0x2a
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
1da177e4 94
6f3cbf55
DG
95/* Additional Sense Code Qualifier (ASCQ) */
96#define ACK_NAK_TO 0x3
97
1da177e4
LT
98
99/* Default values for driver parameters */
100#define DEF_NUM_HOST 1
101#define DEF_NUM_TGTS 1
102#define DEF_MAX_LUNS 1
103/* With these defaults, this driver will make 1 host with 1 target
104 * (id 0) containing 1 logical unit (lun 0). That is 1 device.
105 */
5b94e232 106#define DEF_ATO 1
cbf67842 107#define DEF_DELAY 1 /* if > 0 unit is a jiffy */
1da177e4 108#define DEF_DEV_SIZE_MB 8
5b94e232
MP
109#define DEF_DIF 0
110#define DEF_DIX 0
1da177e4 111#define DEF_D_SENSE 0
5b94e232 112#define DEF_EVERY_NTH 0
23183910 113#define DEF_FAKE_RW 0
c6a44287 114#define DEF_GUARD 0
cbf67842 115#define DEF_HOST_LOCK 0
5b94e232
MP
116#define DEF_LBPU 0
117#define DEF_LBPWS 0
118#define DEF_LBPWS10 0
be1dd78d 119#define DEF_LBPRZ 1
ea61fca5 120#define DEF_LOWEST_ALIGNED 0
cbf67842 121#define DEF_NDELAY 0 /* if > 0 unit is a nanosecond */
5b94e232
MP
122#define DEF_NO_LUN_0 0
123#define DEF_NUM_PARTS 0
124#define DEF_OPTS 0
e308b3d1 125#define DEF_OPT_BLKS 64
5b94e232
MP
126#define DEF_PHYSBLK_EXP 0
127#define DEF_PTYPE 0
d986788b 128#define DEF_REMOVABLE false
e46b0344 129#define DEF_SCSI_LEVEL 6 /* INQUIRY, byte2 [6->SPC-4] */
5b94e232 130#define DEF_SECTOR_SIZE 512
cbf67842 131#define DEF_TAGGED_QUEUING 0 /* 0 | MSG_SIMPLE_TAG | MSG_ORDERED_TAG */
5b94e232
MP
132#define DEF_UNMAP_ALIGNMENT 0
133#define DEF_UNMAP_GRANULARITY 1
6014759c
MP
134#define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF
135#define DEF_UNMAP_MAX_DESC 256
5b94e232
MP
136#define DEF_VIRTUAL_GB 0
137#define DEF_VPD_USE_HOSTNO 1
138#define DEF_WRITESAME_LENGTH 0xFFFF
c2248fc9 139#define DEF_STRICT 0
cbf67842 140#define DELAY_OVERRIDDEN -9999
1da177e4
LT
141
142/* bit mask values for scsi_debug_opts */
143#define SCSI_DEBUG_OPT_NOISE 1
144#define SCSI_DEBUG_OPT_MEDIUM_ERR 2
145#define SCSI_DEBUG_OPT_TIMEOUT 4
146#define SCSI_DEBUG_OPT_RECOVERED_ERR 8
6f3cbf55 147#define SCSI_DEBUG_OPT_TRANSPORT_ERR 16
c6a44287
MP
148#define SCSI_DEBUG_OPT_DIF_ERR 32
149#define SCSI_DEBUG_OPT_DIX_ERR 64
18a4d0a2 150#define SCSI_DEBUG_OPT_MAC_TIMEOUT 128
cbf67842
DG
151#define SCSI_DEBUG_OPT_SHORT_TRANSFER 0x100
152#define SCSI_DEBUG_OPT_Q_NOISE 0x200
153#define SCSI_DEBUG_OPT_ALL_TSF 0x400
154#define SCSI_DEBUG_OPT_RARE_TSF 0x800
155#define SCSI_DEBUG_OPT_N_WCE 0x1000
156#define SCSI_DEBUG_OPT_RESET_NOISE 0x2000
157#define SCSI_DEBUG_OPT_NO_CDB_NOISE 0x4000
158#define SCSI_DEBUG_OPT_ALL_NOISE (0x1 | 0x200 | 0x2000)
1da177e4
LT
159/* When "every_nth" > 0 then modulo "every_nth" commands:
160 * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
161 * - a RECOVERED_ERROR is simulated on successful read and write
162 * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
6f3cbf55
DG
163 * - a TRANSPORT_ERROR is simulated on successful read and write
164 * commands if SCSI_DEBUG_OPT_TRANSPORT_ERR is set.
1da177e4
LT
165 *
166 * When "every_nth" < 0 then after "- every_nth" commands:
167 * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
168 * - a RECOVERED_ERROR is simulated on successful read and write
169 * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
6f3cbf55
DG
170 * - a TRANSPORT_ERROR is simulated on successful read and write
171 * commands if SCSI_DEBUG_OPT_TRANSPORT_ERR is set.
1da177e4
LT
172 * This will continue until some other action occurs (e.g. the user
173 * writing a new value (other than -1 or 1) to every_nth via sysfs).
174 */
175
cbf67842
DG
176/* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs)are returned in
177 * priority order. In the subset implemented here lower numbers have higher
178 * priority. The UA numbers should be a sequence starting from 0 with
179 * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */
180#define SDEBUG_UA_POR 0 /* Power on, reset, or bus device reset */
181#define SDEBUG_UA_BUS_RESET 1
182#define SDEBUG_UA_MODE_CHANGED 2
0d01c5df
DG
183#define SDEBUG_UA_CAPACITY_CHANGED 3
184#define SDEBUG_NUM_UAS 4
cbf67842
DG
185
186/* for check_readiness() */
c2248fc9
DG
187#define UAS_ONLY 1 /* check for UAs only */
188#define UAS_TUR 0 /* if no UAs then check if media access possible */
cbf67842 189
1da177e4
LT
190/* when 1==SCSI_DEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
191 * sector on read commands: */
192#define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
32f7ef73 193#define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */
1da177e4
LT
194
195/* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
196 * or "peripheral device" addressing (value 0) */
197#define SAM2_LUN_ADDRESS_METHOD 0
c65b1445 198#define SAM2_WLUN_REPORT_LUNS 0xc101
1da177e4 199
cbf67842
DG
200/* SCSI_DEBUG_CANQUEUE is the maximum number of commands that can be queued
201 * (for response) at one time. Can be reduced by max_queue option. Command
202 * responses are not queued when delay=0 and ndelay=0. The per-device
203 * DEF_CMD_PER_LUN can be changed via sysfs:
204 * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth but cannot exceed
205 * SCSI_DEBUG_CANQUEUE. */
206#define SCSI_DEBUG_CANQUEUE_WORDS 9 /* a WORD is bits in a long */
207#define SCSI_DEBUG_CANQUEUE (SCSI_DEBUG_CANQUEUE_WORDS * BITS_PER_LONG)
208#define DEF_CMD_PER_LUN 255
209
210#if DEF_CMD_PER_LUN > SCSI_DEBUG_CANQUEUE
211#warning "Expect DEF_CMD_PER_LUN <= SCSI_DEBUG_CANQUEUE"
212#endif
78d4e5a0 213
c2248fc9
DG
214/* SCSI opcodes (first byte of cdb) mapped onto these indexes */
215enum sdeb_opcode_index {
216 SDEB_I_INVALID_OPCODE = 0,
217 SDEB_I_INQUIRY = 1,
218 SDEB_I_REPORT_LUNS = 2,
219 SDEB_I_REQUEST_SENSE = 3,
220 SDEB_I_TEST_UNIT_READY = 4,
221 SDEB_I_MODE_SENSE = 5, /* 6, 10 */
222 SDEB_I_MODE_SELECT = 6, /* 6, 10 */
223 SDEB_I_LOG_SENSE = 7,
224 SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */
225 SDEB_I_READ = 9, /* 6, 10, 12, 16 */
226 SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */
227 SDEB_I_START_STOP = 11,
228 SDEB_I_SERV_ACT_IN = 12, /* 12, 16 */
229 SDEB_I_SERV_ACT_OUT = 13, /* 12, 16 */
230 SDEB_I_MAINT_IN = 14,
231 SDEB_I_MAINT_OUT = 15,
232 SDEB_I_VERIFY = 16, /* 10 only */
233 SDEB_I_VARIABLE_LEN = 17,
234 SDEB_I_RESERVE = 18, /* 6, 10 */
235 SDEB_I_RELEASE = 19, /* 6, 10 */
236 SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */
237 SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */
238 SDEB_I_ATA_PT = 22, /* 12, 16 */
239 SDEB_I_SEND_DIAG = 23,
240 SDEB_I_UNMAP = 24,
241 SDEB_I_XDWRITEREAD = 25, /* 10 only */
242 SDEB_I_WRITE_BUFFER = 26,
243 SDEB_I_WRITE_SAME = 27, /* 10, 16 */
244 SDEB_I_SYNC_CACHE = 28, /* 10 only */
245 SDEB_I_COMP_WRITE = 29,
246 SDEB_I_LAST_ELEMENT = 30, /* keep this last */
247};
248
249static const unsigned char opcode_ind_arr[256] = {
250/* 0x0; 0x0->0x1f: 6 byte cdbs */
251 SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE,
252 0, 0, 0, 0,
253 SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0,
254 0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
255 SDEB_I_RELEASE,
256 0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG,
257 SDEB_I_ALLOW_REMOVAL, 0,
258/* 0x20; 0x20->0x3f: 10 byte cdbs */
259 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0,
260 SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY,
261 0, 0, 0, 0, 0, SDEB_I_SYNC_CACHE, 0, 0,
262 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0,
263/* 0x40; 0x40->0x5f: 10 byte cdbs */
264 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0,
265 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0,
266 0, 0, 0, SDEB_I_XDWRITEREAD, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
267 SDEB_I_RELEASE,
268 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0,
269/* 0x60; 0x60->0x7d are reserved */
270 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
271 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
272 0, SDEB_I_VARIABLE_LEN,
273/* 0x80; 0x80->0x9f: 16 byte cdbs */
274 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0,
275 SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0, 0, 0, 0, 0,
276 0, 0, 0, SDEB_I_WRITE_SAME, 0, 0, 0, 0,
277 0, 0, 0, 0, 0, 0, SDEB_I_SERV_ACT_IN, SDEB_I_SERV_ACT_OUT,
278/* 0xa0; 0xa0->0xbf: 12 byte cdbs */
279 SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN,
280 SDEB_I_MAINT_OUT, 0, 0, 0,
281 SDEB_I_READ, SDEB_I_SERV_ACT_OUT, SDEB_I_WRITE, SDEB_I_SERV_ACT_IN,
282 0, 0, 0, 0,
283 0, 0, 0, 0, 0, 0, 0, 0,
284 0, 0, 0, 0, 0, 0, 0, 0,
285/* 0xc0; 0xc0->0xff: vendor specific */
286 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
287 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
288 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
289 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
290};
291
292#define F_D_IN 1
293#define F_D_OUT 2
294#define F_D_OUT_MAYBE 4 /* WRITE SAME, NDOB bit */
295#define F_D_UNKN 8
296#define F_RL_WLUN_OK 0x10
297#define F_SKIP_UA 0x20
298#define F_DELAY_OVERR 0x40
299#define F_SA_LOW 0x80 /* cdb byte 1, bits 4 to 0 */
300#define F_SA_HIGH 0x100 /* as used by variable length cdbs */
301#define F_INV_OP 0x200
302#define F_FAKE_RW 0x400
303#define F_M_ACCESS 0x800 /* media access */
304
305#define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR)
306#define FF_DIRECT_IO (F_M_ACCESS | F_FAKE_RW)
307#define FF_SA (F_SA_HIGH | F_SA_LOW)
308
309struct sdebug_dev_info;
310static int scsi_debug_queuecommand(struct scsi_cmnd *scp);
311static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *);
312static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *);
313static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *);
314static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
315static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *);
316static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
317static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *);
318static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
319static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
320static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *);
321static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *);
322static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *);
323static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *);
324static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *);
325static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *);
326static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *);
327static int resp_xdwriteread_10(struct scsi_cmnd *, struct sdebug_dev_info *);
328
329struct opcode_info_t {
330 u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff
331 * for terminating element */
332 u8 opcode; /* if num_attached > 0, preferred */
333 u16 sa; /* service action */
334 u32 flags; /* OR-ed set of SDEB_F_* */
335 int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
336 const struct opcode_info_t *arrp; /* num_attached elements or NULL */
337 u8 len_mask[16]; /* len=len_mask[0], then mask for cdb[1]... */
338 /* ignore cdb bytes after position 15 */
339};
340
341static const struct opcode_info_t msense_iarr[1] = {
342 {0, 0x1a, 0, F_D_IN, NULL, NULL,
343 {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
344};
345
346static const struct opcode_info_t mselect_iarr[1] = {
347 {0, 0x15, 0, F_D_OUT, NULL, NULL,
348 {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
349};
350
351static const struct opcode_info_t read_iarr[3] = {
352 {0, 0x28, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL,/* READ(10) */
353 {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0,
354 0, 0, 0, 0} },
355 {0, 0x8, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL, /* READ(6) */
356 {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
357 {0, 0xa8, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL,/* READ(12) */
358 {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f,
359 0xc7, 0, 0, 0, 0} },
360};
361
362static const struct opcode_info_t write_iarr[3] = {
363 {0, 0x2a, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 10 */
364 {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0,
365 0, 0, 0, 0} },
366 {0, 0xa, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 6 */
367 {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
368 {0, 0xaa, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 12 */
369 {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f,
370 0xc7, 0, 0, 0, 0} },
371};
372
373static const struct opcode_info_t sa_in_iarr[1] = {
374 {0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL,
375 {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
376 0xff, 0xff, 0xff, 0, 0xc7} },
377};
378
379static const struct opcode_info_t vl_iarr[1] = { /* VARIABLE LENGTH */
380 {0, 0x7f, 0xb, F_SA_HIGH | F_D_OUT | FF_DIRECT_IO, resp_write_dt0,
381 NULL, {32, 0xc7, 0, 0, 0, 0, 0x1f, 0x18, 0x0, 0xb, 0xfa,
382 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */
383};
384
385static const struct opcode_info_t maint_in_iarr[2] = {
386 {0, 0xa3, 0xc, F_SA_LOW | F_D_IN, NULL, NULL,
387 {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0,
388 0xc7, 0, 0, 0, 0} },
389 {0, 0xa3, 0xd, F_SA_LOW | F_D_IN, NULL, NULL,
390 {12, 0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
391 0, 0} },
392};
393
394static const struct opcode_info_t write_same_iarr[1] = {
395 {0, 0x93, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, resp_write_same_16, NULL,
396 {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
397 0xff, 0xff, 0xff, 0x1f, 0xc7} },
398};
399
400static const struct opcode_info_t reserve_iarr[1] = {
401 {0, 0x16, 0, F_D_OUT, NULL, NULL, /* RESERVE(6) */
402 {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
403};
404
405static const struct opcode_info_t release_iarr[1] = {
406 {0, 0x17, 0, F_D_OUT, NULL, NULL, /* RELEASE(6) */
407 {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
408};
409
410
411/* This array is accessed via SDEB_I_* values. Make sure all are mapped,
412 * plus the terminating elements for logic that scans this table such as
413 * REPORT SUPPORTED OPERATION CODES. */
414static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEMENT + 1] = {
415/* 0 */
416 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL,
417 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
418 {0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL,
419 {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
420 {0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL,
421 {12, 0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
422 0, 0} },
423 {0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL,
424 {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
425 {0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */
426 {6, 0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
427 {1, 0x5a, 0, F_D_IN, resp_mode_sense, msense_iarr,
428 {10, 0xf8, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
429 0} },
430 {1, 0x55, 0, F_D_OUT, resp_mode_select, mselect_iarr,
431 {10, 0xf1, 0, 0, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
432 {0, 0x4d, 0, F_D_IN, resp_log_sense, NULL,
433 {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0,
434 0, 0, 0} },
435 {0, 0x25, 0, F_D_IN, resp_readcap, NULL,
436 {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0,
437 0, 0} },
438 {3, 0x88, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, read_iarr,
439 {16, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
440 0xff, 0xff, 0xff, 0x9f, 0xc7} }, /* READ(16) */
441/* 10 */
442 {3, 0x8a, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, write_iarr,
443 {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
444 0xff, 0xff, 0xff, 0x9f, 0xc7} }, /* WRITE(16) */
445 {0, 0x1b, 0, 0, resp_start_stop, NULL, /* START STOP UNIT */
446 {6, 0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
447 {1, 0x9e, 0x10, F_SA_LOW | F_D_IN, resp_readcap16, sa_in_iarr,
448 {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
449 0xff, 0xff, 0xff, 0x1, 0xc7} }, /* READ CAPACITY(16) */
450 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* SA OUT */
451 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
452 {2, 0xa3, 0xa, F_SA_LOW | F_D_IN, resp_report_tgtpgs, maint_in_iarr,
453 {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, 0,
454 0} },
455 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */
456 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
457 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* VERIFY */
458 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
459 {1, 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_DIRECT_IO, resp_read_dt0,
460 vl_iarr, {32, 0xc7, 0, 0, 0, 0, 0x1f, 0x18, 0x0, 0x9, 0xfe, 0,
461 0xff, 0xff, 0xff, 0xff} },/* VARIABLE LENGTH, READ(32) */
462 {1, 0x56, 0, F_D_OUT, NULL, reserve_iarr, /* RESERVE(10) */
463 {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
464 0} },
465 {1, 0x57, 0, F_D_OUT, NULL, release_iarr, /* RELEASE(10) */
466 {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
467 0} },
468/* 20 */
469 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ALLOW REMOVAL */
470 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
471 {0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */
472 {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
473 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */
474 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
475 {0, 0x1d, F_D_OUT, 0, NULL, NULL, /* SEND DIAGNOSTIC */
476 {6, 0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
477 {0, 0x42, 0, F_D_OUT | FF_DIRECT_IO, resp_unmap, NULL, /* UNMAP */
478 {10, 0x1, 0, 0, 0, 0, 0x1f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
479 {0, 0x53, 0, F_D_IN | F_D_OUT | FF_DIRECT_IO, resp_xdwriteread_10,
480 NULL, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7,
481 0, 0, 0, 0, 0, 0} },
482 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* WRITE_BUFFER */
483 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
484 {1, 0x41, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, resp_write_same_10,
485 write_same_iarr, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff,
486 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
487 {0, 0x35, 0, F_DELAY_OVERR | FF_DIRECT_IO, NULL, NULL, /* SYNC_CACHE */
488 {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0,
489 0, 0, 0, 0} },
490 {0, 0x89, 0, F_D_OUT | FF_DIRECT_IO, NULL, NULL,
491 {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0,
492 0, 0xff, 0x1f, 0xc7} }, /* COMPARE AND WRITE */
493
494/* 30 */
495 {0xff, 0, 0, 0, NULL, NULL, /* terminating element */
496 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
497};
498
817fd66b
DG
499struct sdebug_scmd_extra_t {
500 bool inj_recovered;
501 bool inj_transport;
502 bool inj_dif;
503 bool inj_dix;
504 bool inj_short;
505};
506
1da177e4 507static int scsi_debug_add_host = DEF_NUM_HOST;
5b94e232 508static int scsi_debug_ato = DEF_ATO;
1da177e4
LT
509static int scsi_debug_delay = DEF_DELAY;
510static int scsi_debug_dev_size_mb = DEF_DEV_SIZE_MB;
5b94e232
MP
511static int scsi_debug_dif = DEF_DIF;
512static int scsi_debug_dix = DEF_DIX;
513static int scsi_debug_dsense = DEF_D_SENSE;
1da177e4 514static int scsi_debug_every_nth = DEF_EVERY_NTH;
5b94e232 515static int scsi_debug_fake_rw = DEF_FAKE_RW;
68aee7ba 516static unsigned int scsi_debug_guard = DEF_GUARD;
5b94e232 517static int scsi_debug_lowest_aligned = DEF_LOWEST_ALIGNED;
1da177e4 518static int scsi_debug_max_luns = DEF_MAX_LUNS;
78d4e5a0 519static int scsi_debug_max_queue = SCSI_DEBUG_CANQUEUE;
cbf67842
DG
520static atomic_t retired_max_queue; /* if > 0 then was prior max_queue */
521static int scsi_debug_ndelay = DEF_NDELAY;
5b94e232 522static int scsi_debug_no_lun_0 = DEF_NO_LUN_0;
78d4e5a0 523static int scsi_debug_no_uld = 0;
5b94e232 524static int scsi_debug_num_parts = DEF_NUM_PARTS;
1da177e4 525static int scsi_debug_num_tgts = DEF_NUM_TGTS; /* targets per host */
5b94e232 526static int scsi_debug_opt_blks = DEF_OPT_BLKS;
1da177e4 527static int scsi_debug_opts = DEF_OPTS;
5b94e232 528static int scsi_debug_physblk_exp = DEF_PHYSBLK_EXP;
1da177e4 529static int scsi_debug_ptype = DEF_PTYPE; /* SCSI peripheral type (0==disk) */
5b94e232
MP
530static int scsi_debug_scsi_level = DEF_SCSI_LEVEL;
531static int scsi_debug_sector_size = DEF_SECTOR_SIZE;
c65b1445 532static int scsi_debug_virtual_gb = DEF_VIRTUAL_GB;
23183910 533static int scsi_debug_vpd_use_hostno = DEF_VPD_USE_HOSTNO;
5b94e232
MP
534static unsigned int scsi_debug_lbpu = DEF_LBPU;
535static unsigned int scsi_debug_lbpws = DEF_LBPWS;
536static unsigned int scsi_debug_lbpws10 = DEF_LBPWS10;
be1dd78d 537static unsigned int scsi_debug_lbprz = DEF_LBPRZ;
6014759c 538static unsigned int scsi_debug_unmap_alignment = DEF_UNMAP_ALIGNMENT;
5b94e232
MP
539static unsigned int scsi_debug_unmap_granularity = DEF_UNMAP_GRANULARITY;
540static unsigned int scsi_debug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS;
541static unsigned int scsi_debug_unmap_max_desc = DEF_UNMAP_MAX_DESC;
542static unsigned int scsi_debug_write_same_length = DEF_WRITESAME_LENGTH;
d986788b 543static bool scsi_debug_removable = DEF_REMOVABLE;
0759c666 544static bool scsi_debug_clustering;
cbf67842 545static bool scsi_debug_host_lock = DEF_HOST_LOCK;
c2248fc9 546static bool scsi_debug_strict = DEF_STRICT;
817fd66b 547static bool sdebug_any_injecting_opt;
1da177e4 548
cbf67842
DG
549static atomic_t sdebug_cmnd_count;
550static atomic_t sdebug_completions;
551static atomic_t sdebug_a_tsf; /* counter of 'almost' TSFs */
1da177e4
LT
552
553#define DEV_READONLY(TGT) (0)
1da177e4 554
c65b1445 555static unsigned int sdebug_store_sectors;
1da177e4
LT
556static sector_t sdebug_capacity; /* in sectors */
557
558/* old BIOS stuff, kernel may get rid of them but some mode sense pages
559 may still need them */
560static int sdebug_heads; /* heads per disk */
561static int sdebug_cylinders_per; /* cylinders per surface */
562static int sdebug_sectors_per; /* sectors per cylinder */
563
1da177e4
LT
564#define SDEBUG_MAX_PARTS 4
565
395cef03 566#define SCSI_DEBUG_MAX_CMD_LEN 32
9e603ca0 567
5b94e232
MP
568static unsigned int scsi_debug_lbp(void)
569{
cbf67842
DG
570 return ((0 == scsi_debug_fake_rw) &&
571 (scsi_debug_lbpu | scsi_debug_lbpws | scsi_debug_lbpws10));
5b94e232
MP
572}
573
1da177e4
LT
574struct sdebug_dev_info {
575 struct list_head dev_list;
1da177e4
LT
576 unsigned int channel;
577 unsigned int target;
9cb78c16 578 u64 lun;
1da177e4 579 struct sdebug_host_info *sdbg_host;
cbf67842
DG
580 unsigned long uas_bm[1];
581 atomic_t num_in_q;
c2248fc9
DG
582 char stopped; /* TODO: should be atomic */
583 bool used;
1da177e4
LT
584};
585
586struct sdebug_host_info {
587 struct list_head host_list;
588 struct Scsi_Host *shost;
589 struct device dev;
590 struct list_head dev_info_list;
591};
592
593#define to_sdebug_host(d) \
594 container_of(d, struct sdebug_host_info, dev)
595
596static LIST_HEAD(sdebug_host_list);
597static DEFINE_SPINLOCK(sdebug_host_list_lock);
598
cbf67842
DG
599
600struct sdebug_hrtimer { /* ... is derived from hrtimer */
601 struct hrtimer hrt; /* must be first element */
602 int qa_indx;
603};
1da177e4
LT
604
605struct sdebug_queued_cmd {
cbf67842
DG
606 /* in_use flagged by a bit in queued_in_use_bm[] */
607 struct timer_list *cmnd_timerp;
608 struct tasklet_struct *tletp;
609 struct sdebug_hrtimer *sd_hrtp;
1da177e4 610 struct scsi_cmnd * a_cmnd;
1da177e4
LT
611};
612static struct sdebug_queued_cmd queued_arr[SCSI_DEBUG_CANQUEUE];
cbf67842
DG
613static unsigned long queued_in_use_bm[SCSI_DEBUG_CANQUEUE_WORDS];
614
1da177e4 615
1da177e4 616static unsigned char * fake_storep; /* ramdisk storage */
e18d8bea 617static struct sd_dif_tuple *dif_storep; /* protection info */
44d92694 618static void *map_storep; /* provisioning map */
1da177e4 619
44d92694 620static unsigned long map_size;
cbf67842
DG
621static int num_aborts;
622static int num_dev_resets;
623static int num_target_resets;
624static int num_bus_resets;
625static int num_host_resets;
c6a44287
MP
626static int dix_writes;
627static int dix_reads;
628static int dif_errors;
1da177e4
LT
629
630static DEFINE_SPINLOCK(queued_arr_lock);
631static DEFINE_RWLOCK(atomic_rw);
632
cbf67842
DG
633static char sdebug_proc_name[] = MY_NAME;
634static const char *my_name = MY_NAME;
1da177e4 635
1da177e4
LT
636static struct bus_type pseudo_lld_bus;
637
638static struct device_driver sdebug_driverfs_driver = {
639 .name = sdebug_proc_name,
640 .bus = &pseudo_lld_bus,
1da177e4
LT
641};
642
643static const int check_condition_result =
644 (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
645
c6a44287
MP
646static const int illegal_condition_result =
647 (DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
648
cbf67842
DG
649static const int device_qfull_result =
650 (DID_OK << 16) | (COMMAND_COMPLETE << 8) | SAM_STAT_TASK_SET_FULL;
651
652static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
653 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0,
654 0, 0, 0, 0};
c65b1445
DG
655static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
656 0, 0, 0x2, 0x4b};
657static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
658 0, 0, 0x0, 0x0};
659
14faa944
AM
660static void *fake_store(unsigned long long lba)
661{
662 lba = do_div(lba, sdebug_store_sectors);
663
664 return fake_storep + lba * scsi_debug_sector_size;
665}
666
667static struct sd_dif_tuple *dif_store(sector_t sector)
668{
669 sector = do_div(sector, sdebug_store_sectors);
670
671 return dif_storep + sector;
672}
673
1da177e4
LT
674static int sdebug_add_adapter(void);
675static void sdebug_remove_adapter(void);
1da177e4 676
8dea0d02
FT
677static void sdebug_max_tgts_luns(void)
678{
679 struct sdebug_host_info *sdbg_host;
680 struct Scsi_Host *hpnt;
681
682 spin_lock(&sdebug_host_list_lock);
683 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
684 hpnt = sdbg_host->shost;
685 if ((hpnt->this_id >= 0) &&
686 (scsi_debug_num_tgts > hpnt->this_id))
687 hpnt->max_id = scsi_debug_num_tgts + 1;
688 else
689 hpnt->max_id = scsi_debug_num_tgts;
690 /* scsi_debug_max_luns; */
691 hpnt->max_lun = SAM2_WLUN_REPORT_LUNS;
692 }
693 spin_unlock(&sdebug_host_list_lock);
694}
695
22017ed2
DG
696enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1};
697
698/* Set in_bit to -1 to indicate no bit position of invalid field */
699static void
700mk_sense_invalid_fld(struct scsi_cmnd *scp, enum sdeb_cmd_data c_d,
701 int in_byte, int in_bit)
702{
703 unsigned char *sbuff;
704 u8 sks[4];
705 int sl, asc;
706
707 sbuff = scp->sense_buffer;
708 if (!sbuff) {
709 sdev_printk(KERN_ERR, scp->device,
710 "%s: sense_buffer is NULL\n", __func__);
711 return;
712 }
713 asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST;
714 memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
715 scsi_build_sense_buffer(scsi_debug_dsense, sbuff, ILLEGAL_REQUEST,
716 asc, 0);
717 memset(sks, 0, sizeof(sks));
718 sks[0] = 0x80;
719 if (c_d)
720 sks[0] |= 0x40;
721 if (in_bit >= 0) {
722 sks[0] |= 0x8;
723 sks[0] |= 0x7 & in_bit;
724 }
725 put_unaligned_be16(in_byte, sks + 1);
726 if (scsi_debug_dsense) {
727 sl = sbuff[7] + 8;
728 sbuff[7] = sl;
729 sbuff[sl] = 0x2;
730 sbuff[sl + 1] = 0x6;
731 memcpy(sbuff + sl + 4, sks, 3);
732 } else
733 memcpy(sbuff + 15, sks, 3);
734 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
735 sdev_printk(KERN_INFO, scp->device, "%s: [sense_key,asc,ascq"
736 "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n",
737 my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit);
738}
739
cbf67842 740static void mk_sense_buffer(struct scsi_cmnd *scp, int key, int asc, int asq)
8dea0d02
FT
741{
742 unsigned char *sbuff;
743
cbf67842
DG
744 sbuff = scp->sense_buffer;
745 if (!sbuff) {
746 sdev_printk(KERN_ERR, scp->device,
747 "%s: sense_buffer is NULL\n", __func__);
748 return;
749 }
750 memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
8dea0d02
FT
751
752 scsi_build_sense_buffer(scsi_debug_dsense, sbuff, key, asc, asq);
753
754 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
cbf67842
DG
755 sdev_printk(KERN_INFO, scp->device,
756 "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n",
757 my_name, key, asc, asq);
8dea0d02 758}
1da177e4 759
22017ed2
DG
760static void
761mk_sense_invalid_opcode(struct scsi_cmnd *scp)
762{
763 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
764}
765
1da177e4
LT
766static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
767{
768 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
cbf67842
DG
769 if (0x1261 == cmd)
770 sdev_printk(KERN_INFO, dev,
771 "%s: BLKFLSBUF [0x1261]\n", __func__);
772 else if (0x5331 == cmd)
773 sdev_printk(KERN_INFO, dev,
774 "%s: CDROM_GET_CAPABILITY [0x5331]\n",
775 __func__);
776 else
777 sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n",
778 __func__, cmd);
1da177e4
LT
779 }
780 return -EINVAL;
781 /* return -ENOTTY; // correct return but upsets fdisk */
782}
783
cbf67842 784static int check_readiness(struct scsi_cmnd *SCpnt, int uas_only,
c65b1445 785 struct sdebug_dev_info * devip)
1da177e4 786{
cbf67842
DG
787 int k;
788 bool debug = !!(SCSI_DEBUG_OPT_NOISE & scsi_debug_opts);
789
790 k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS);
791 if (k != SDEBUG_NUM_UAS) {
792 const char *cp = NULL;
793
794 switch (k) {
795 case SDEBUG_UA_POR:
796 mk_sense_buffer(SCpnt, UNIT_ATTENTION,
797 UA_RESET_ASC, POWER_ON_RESET_ASCQ);
798 if (debug)
799 cp = "power on reset";
800 break;
801 case SDEBUG_UA_BUS_RESET:
802 mk_sense_buffer(SCpnt, UNIT_ATTENTION,
803 UA_RESET_ASC, BUS_RESET_ASCQ);
804 if (debug)
805 cp = "bus reset";
806 break;
807 case SDEBUG_UA_MODE_CHANGED:
808 mk_sense_buffer(SCpnt, UNIT_ATTENTION,
809 UA_CHANGED_ASC, MODE_CHANGED_ASCQ);
810 if (debug)
811 cp = "mode parameters changed";
812 break;
0d01c5df
DG
813 case SDEBUG_UA_CAPACITY_CHANGED:
814 mk_sense_buffer(SCpnt, UNIT_ATTENTION,
815 UA_CHANGED_ASC, CAPACITY_CHANGED_ASCQ);
816 if (debug)
817 cp = "capacity data changed";
cbf67842
DG
818 default:
819 pr_warn("%s: unexpected unit attention code=%d\n",
820 __func__, k);
821 if (debug)
822 cp = "unknown";
823 break;
824 }
825 clear_bit(k, devip->uas_bm);
826 if (debug)
827 sdev_printk(KERN_INFO, SCpnt->device,
828 "%s reports: Unit attention: %s\n",
829 my_name, cp);
1da177e4
LT
830 return check_condition_result;
831 }
cbf67842
DG
832 if ((UAS_TUR == uas_only) && devip->stopped) {
833 mk_sense_buffer(SCpnt, NOT_READY, LOGICAL_UNIT_NOT_READY,
c65b1445 834 0x2);
cbf67842
DG
835 if (debug)
836 sdev_printk(KERN_INFO, SCpnt->device,
837 "%s reports: Not ready: %s\n", my_name,
838 "initializing command required");
c65b1445
DG
839 return check_condition_result;
840 }
1da177e4
LT
841 return 0;
842}
843
844/* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */
21a61829 845static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
1da177e4
LT
846 int arr_len)
847{
21a61829 848 int act_len;
072d0bb3 849 struct scsi_data_buffer *sdb = scsi_in(scp);
1da177e4 850
072d0bb3 851 if (!sdb->length)
1da177e4 852 return 0;
072d0bb3 853 if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE))
1da177e4 854 return (DID_ERROR << 16);
21a61829
FT
855
856 act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents,
857 arr, arr_len);
a4517511 858 sdb->resid = scsi_bufflen(scp) - act_len;
21a61829 859
1da177e4
LT
860 return 0;
861}
862
863/* Returns number of bytes fetched into 'arr' or -1 if error. */
21a61829
FT
864static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
865 int arr_len)
1da177e4 866{
21a61829 867 if (!scsi_bufflen(scp))
1da177e4 868 return 0;
072d0bb3 869 if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_TO_DEVICE))
1da177e4 870 return -1;
21a61829
FT
871
872 return scsi_sg_copy_to_buffer(scp, arr, arr_len);
1da177e4
LT
873}
874
875
876static const char * inq_vendor_id = "Linux ";
877static const char * inq_product_id = "scsi_debug ";
cbf67842 878static const char *inq_product_rev = "0184"; /* version less '.' */
1da177e4 879
cbf67842 880/* Device identification VPD page. Returns number of bytes placed in arr */
5a09e398
HR
881static int inquiry_evpd_83(unsigned char * arr, int port_group_id,
882 int target_dev_id, int dev_id_num,
883 const char * dev_id_str,
c65b1445 884 int dev_id_str_len)
1da177e4 885{
c65b1445
DG
886 int num, port_a;
887 char b[32];
1da177e4 888
c65b1445 889 port_a = target_dev_id + 1;
1da177e4
LT
890 /* T10 vendor identifier field format (faked) */
891 arr[0] = 0x2; /* ASCII */
892 arr[1] = 0x1;
893 arr[2] = 0x0;
894 memcpy(&arr[4], inq_vendor_id, 8);
895 memcpy(&arr[12], inq_product_id, 16);
896 memcpy(&arr[28], dev_id_str, dev_id_str_len);
897 num = 8 + 16 + dev_id_str_len;
898 arr[3] = num;
899 num += 4;
c65b1445
DG
900 if (dev_id_num >= 0) {
901 /* NAA-5, Logical unit identifier (binary) */
902 arr[num++] = 0x1; /* binary (not necessarily sas) */
903 arr[num++] = 0x3; /* PIV=0, lu, naa */
904 arr[num++] = 0x0;
905 arr[num++] = 0x8;
906 arr[num++] = 0x53; /* naa-5 ieee company id=0x333333 (fake) */
907 arr[num++] = 0x33;
908 arr[num++] = 0x33;
909 arr[num++] = 0x30;
910 arr[num++] = (dev_id_num >> 24);
911 arr[num++] = (dev_id_num >> 16) & 0xff;
912 arr[num++] = (dev_id_num >> 8) & 0xff;
913 arr[num++] = dev_id_num & 0xff;
914 /* Target relative port number */
915 arr[num++] = 0x61; /* proto=sas, binary */
916 arr[num++] = 0x94; /* PIV=1, target port, rel port */
917 arr[num++] = 0x0; /* reserved */
918 arr[num++] = 0x4; /* length */
919 arr[num++] = 0x0; /* reserved */
920 arr[num++] = 0x0; /* reserved */
921 arr[num++] = 0x0;
922 arr[num++] = 0x1; /* relative port A */
923 }
924 /* NAA-5, Target port identifier */
925 arr[num++] = 0x61; /* proto=sas, binary */
926 arr[num++] = 0x93; /* piv=1, target port, naa */
927 arr[num++] = 0x0;
928 arr[num++] = 0x8;
929 arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
930 arr[num++] = 0x22;
931 arr[num++] = 0x22;
932 arr[num++] = 0x20;
933 arr[num++] = (port_a >> 24);
934 arr[num++] = (port_a >> 16) & 0xff;
935 arr[num++] = (port_a >> 8) & 0xff;
936 arr[num++] = port_a & 0xff;
5a09e398
HR
937 /* NAA-5, Target port group identifier */
938 arr[num++] = 0x61; /* proto=sas, binary */
939 arr[num++] = 0x95; /* piv=1, target port group id */
940 arr[num++] = 0x0;
941 arr[num++] = 0x4;
942 arr[num++] = 0;
943 arr[num++] = 0;
944 arr[num++] = (port_group_id >> 8) & 0xff;
945 arr[num++] = port_group_id & 0xff;
c65b1445
DG
946 /* NAA-5, Target device identifier */
947 arr[num++] = 0x61; /* proto=sas, binary */
948 arr[num++] = 0xa3; /* piv=1, target device, naa */
949 arr[num++] = 0x0;
950 arr[num++] = 0x8;
951 arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
952 arr[num++] = 0x22;
953 arr[num++] = 0x22;
954 arr[num++] = 0x20;
955 arr[num++] = (target_dev_id >> 24);
956 arr[num++] = (target_dev_id >> 16) & 0xff;
957 arr[num++] = (target_dev_id >> 8) & 0xff;
958 arr[num++] = target_dev_id & 0xff;
959 /* SCSI name string: Target device identifier */
960 arr[num++] = 0x63; /* proto=sas, UTF-8 */
961 arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */
962 arr[num++] = 0x0;
963 arr[num++] = 24;
964 memcpy(arr + num, "naa.52222220", 12);
965 num += 12;
966 snprintf(b, sizeof(b), "%08X", target_dev_id);
967 memcpy(arr + num, b, 8);
968 num += 8;
969 memset(arr + num, 0, 4);
970 num += 4;
971 return num;
972}
973
974
975static unsigned char vpd84_data[] = {
976/* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
977 0x22,0x22,0x22,0x0,0xbb,0x1,
978 0x22,0x22,0x22,0x0,0xbb,0x2,
979};
980
cbf67842 981/* Software interface identification VPD page */
c65b1445
DG
982static int inquiry_evpd_84(unsigned char * arr)
983{
984 memcpy(arr, vpd84_data, sizeof(vpd84_data));
985 return sizeof(vpd84_data);
986}
987
cbf67842 988/* Management network addresses VPD page */
c65b1445
DG
989static int inquiry_evpd_85(unsigned char * arr)
990{
991 int num = 0;
992 const char * na1 = "https://www.kernel.org/config";
993 const char * na2 = "http://www.kernel.org/log";
994 int plen, olen;
995
996 arr[num++] = 0x1; /* lu, storage config */
997 arr[num++] = 0x0; /* reserved */
998 arr[num++] = 0x0;
999 olen = strlen(na1);
1000 plen = olen + 1;
1001 if (plen % 4)
1002 plen = ((plen / 4) + 1) * 4;
1003 arr[num++] = plen; /* length, null termianted, padded */
1004 memcpy(arr + num, na1, olen);
1005 memset(arr + num + olen, 0, plen - olen);
1006 num += plen;
1007
1008 arr[num++] = 0x4; /* lu, logging */
1009 arr[num++] = 0x0; /* reserved */
1010 arr[num++] = 0x0;
1011 olen = strlen(na2);
1012 plen = olen + 1;
1013 if (plen % 4)
1014 plen = ((plen / 4) + 1) * 4;
1015 arr[num++] = plen; /* length, null terminated, padded */
1016 memcpy(arr + num, na2, olen);
1017 memset(arr + num + olen, 0, plen - olen);
1018 num += plen;
1019
1020 return num;
1021}
1022
1023/* SCSI ports VPD page */
1024static int inquiry_evpd_88(unsigned char * arr, int target_dev_id)
1025{
1026 int num = 0;
1027 int port_a, port_b;
1028
1029 port_a = target_dev_id + 1;
1030 port_b = port_a + 1;
1031 arr[num++] = 0x0; /* reserved */
1032 arr[num++] = 0x0; /* reserved */
1033 arr[num++] = 0x0;
1034 arr[num++] = 0x1; /* relative port 1 (primary) */
1035 memset(arr + num, 0, 6);
1036 num += 6;
1037 arr[num++] = 0x0;
1038 arr[num++] = 12; /* length tp descriptor */
1039 /* naa-5 target port identifier (A) */
1040 arr[num++] = 0x61; /* proto=sas, binary */
1041 arr[num++] = 0x93; /* PIV=1, target port, NAA */
1042 arr[num++] = 0x0; /* reserved */
1043 arr[num++] = 0x8; /* length */
1044 arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
1045 arr[num++] = 0x22;
1046 arr[num++] = 0x22;
1047 arr[num++] = 0x20;
1048 arr[num++] = (port_a >> 24);
1049 arr[num++] = (port_a >> 16) & 0xff;
1050 arr[num++] = (port_a >> 8) & 0xff;
1051 arr[num++] = port_a & 0xff;
1052
1053 arr[num++] = 0x0; /* reserved */
1054 arr[num++] = 0x0; /* reserved */
1055 arr[num++] = 0x0;
1056 arr[num++] = 0x2; /* relative port 2 (secondary) */
1057 memset(arr + num, 0, 6);
1058 num += 6;
1059 arr[num++] = 0x0;
1060 arr[num++] = 12; /* length tp descriptor */
1061 /* naa-5 target port identifier (B) */
1062 arr[num++] = 0x61; /* proto=sas, binary */
1063 arr[num++] = 0x93; /* PIV=1, target port, NAA */
1064 arr[num++] = 0x0; /* reserved */
1065 arr[num++] = 0x8; /* length */
1066 arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
1067 arr[num++] = 0x22;
1068 arr[num++] = 0x22;
1069 arr[num++] = 0x20;
1070 arr[num++] = (port_b >> 24);
1071 arr[num++] = (port_b >> 16) & 0xff;
1072 arr[num++] = (port_b >> 8) & 0xff;
1073 arr[num++] = port_b & 0xff;
1074
1075 return num;
1076}
1077
1078
1079static unsigned char vpd89_data[] = {
1080/* from 4th byte */ 0,0,0,0,
1081'l','i','n','u','x',' ',' ',' ',
1082'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
1083'1','2','3','4',
10840x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
10850xec,0,0,0,
10860x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
10870,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
10880x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
10890x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
10900x53,0x41,
10910x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
10920x20,0x20,
10930x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
10940x10,0x80,
10950,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
10960x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
10970x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
10980,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
10990x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
11000x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
11010,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0,
11020,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11030,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11040,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11050x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
11060,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
11070xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
11080,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0,
11090,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11110,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11130,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11140,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11150,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11160,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11170,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11180,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11190,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11200,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
1121};
1122
cbf67842 1123/* ATA Information VPD page */
c65b1445
DG
1124static int inquiry_evpd_89(unsigned char * arr)
1125{
1126 memcpy(arr, vpd89_data, sizeof(vpd89_data));
1127 return sizeof(vpd89_data);
1128}
1129
1130
1131static unsigned char vpdb0_data[] = {
1e49f785
DG
1132 /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64,
1133 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1134 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1135 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
c65b1445
DG
1136};
1137
cbf67842 1138/* Block limits VPD page (SBC-3) */
c65b1445
DG
1139static int inquiry_evpd_b0(unsigned char * arr)
1140{
ea61fca5
MP
1141 unsigned int gran;
1142
c65b1445 1143 memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
e308b3d1
MP
1144
1145 /* Optimal transfer length granularity */
ea61fca5
MP
1146 gran = 1 << scsi_debug_physblk_exp;
1147 arr[2] = (gran >> 8) & 0xff;
1148 arr[3] = gran & 0xff;
e308b3d1
MP
1149
1150 /* Maximum Transfer Length */
c65b1445
DG
1151 if (sdebug_store_sectors > 0x400) {
1152 arr[4] = (sdebug_store_sectors >> 24) & 0xff;
1153 arr[5] = (sdebug_store_sectors >> 16) & 0xff;
1154 arr[6] = (sdebug_store_sectors >> 8) & 0xff;
1155 arr[7] = sdebug_store_sectors & 0xff;
1156 }
44d92694 1157
e308b3d1
MP
1158 /* Optimal Transfer Length */
1159 put_unaligned_be32(scsi_debug_opt_blks, &arr[8]);
1160
5b94e232 1161 if (scsi_debug_lbpu) {
e308b3d1 1162 /* Maximum Unmap LBA Count */
6014759c 1163 put_unaligned_be32(scsi_debug_unmap_max_blocks, &arr[16]);
e308b3d1
MP
1164
1165 /* Maximum Unmap Block Descriptor Count */
44d92694
MP
1166 put_unaligned_be32(scsi_debug_unmap_max_desc, &arr[20]);
1167 }
1168
e308b3d1 1169 /* Unmap Granularity Alignment */
44d92694
MP
1170 if (scsi_debug_unmap_alignment) {
1171 put_unaligned_be32(scsi_debug_unmap_alignment, &arr[28]);
1172 arr[28] |= 0x80; /* UGAVALID */
1173 }
1174
e308b3d1 1175 /* Optimal Unmap Granularity */
6014759c
MP
1176 put_unaligned_be32(scsi_debug_unmap_granularity, &arr[24]);
1177
5b94e232
MP
1178 /* Maximum WRITE SAME Length */
1179 put_unaligned_be64(scsi_debug_write_same_length, &arr[32]);
1180
1181 return 0x3c; /* Mandatory page length for Logical Block Provisioning */
44d92694 1182
c65b1445 1183 return sizeof(vpdb0_data);
1da177e4
LT
1184}
1185
1e49f785 1186/* Block device characteristics VPD page (SBC-3) */
eac6e8e4
MW
1187static int inquiry_evpd_b1(unsigned char *arr)
1188{
1189 memset(arr, 0, 0x3c);
1190 arr[0] = 0;
1e49f785
DG
1191 arr[1] = 1; /* non rotating medium (e.g. solid state) */
1192 arr[2] = 0;
1193 arr[3] = 5; /* less than 1.8" */
eac6e8e4
MW
1194
1195 return 0x3c;
1196}
1da177e4 1197
be1dd78d 1198/* Logical block provisioning VPD page (SBC-3) */
6014759c
MP
1199static int inquiry_evpd_b2(unsigned char *arr)
1200{
3f0bc3b3 1201 memset(arr, 0, 0x4);
6014759c
MP
1202 arr[0] = 0; /* threshold exponent */
1203
5b94e232 1204 if (scsi_debug_lbpu)
6014759c
MP
1205 arr[1] = 1 << 7;
1206
5b94e232 1207 if (scsi_debug_lbpws)
6014759c
MP
1208 arr[1] |= 1 << 6;
1209
5b94e232
MP
1210 if (scsi_debug_lbpws10)
1211 arr[1] |= 1 << 5;
1212
be1dd78d
ES
1213 if (scsi_debug_lbprz)
1214 arr[1] |= 1 << 2;
1215
3f0bc3b3 1216 return 0x4;
6014759c
MP
1217}
1218
1da177e4 1219#define SDEBUG_LONG_INQ_SZ 96
c65b1445 1220#define SDEBUG_MAX_INQ_ARR_SZ 584
1da177e4 1221
c2248fc9 1222static int resp_inquiry(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
1da177e4
LT
1223{
1224 unsigned char pq_pdt;
5a09e398 1225 unsigned char * arr;
01123ef4 1226 unsigned char *cmd = scp->cmnd;
5a09e398 1227 int alloc_len, n, ret;
c2248fc9 1228 bool have_wlun;
1da177e4
LT
1229
1230 alloc_len = (cmd[3] << 8) + cmd[4];
6f3cbf55
DG
1231 arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC);
1232 if (! arr)
1233 return DID_REQUEUE << 16;
c2248fc9
DG
1234 have_wlun = (scp->device->lun == SAM2_WLUN_REPORT_LUNS);
1235 if (have_wlun)
c65b1445
DG
1236 pq_pdt = 0x1e; /* present, wlun */
1237 else if (scsi_debug_no_lun_0 && (0 == devip->lun))
1238 pq_pdt = 0x7f; /* not present, no device type */
1239 else
1240 pq_pdt = (scsi_debug_ptype & 0x1f);
1da177e4
LT
1241 arr[0] = pq_pdt;
1242 if (0x2 & cmd[1]) { /* CMDDT bit set */
22017ed2 1243 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1);
5a09e398 1244 kfree(arr);
1da177e4
LT
1245 return check_condition_result;
1246 } else if (0x1 & cmd[1]) { /* EVPD bit set */
5a09e398 1247 int lu_id_num, port_group_id, target_dev_id, len;
c65b1445
DG
1248 char lu_id_str[6];
1249 int host_no = devip->sdbg_host->shost->host_no;
1da177e4 1250
5a09e398
HR
1251 port_group_id = (((host_no + 1) & 0x7f) << 8) +
1252 (devip->channel & 0x7f);
23183910
DG
1253 if (0 == scsi_debug_vpd_use_hostno)
1254 host_no = 0;
c2248fc9 1255 lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) +
c65b1445
DG
1256 (devip->target * 1000) + devip->lun);
1257 target_dev_id = ((host_no + 1) * 2000) +
1258 (devip->target * 1000) - 3;
1259 len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
1da177e4 1260 if (0 == cmd[2]) { /* supported vital product data pages */
c65b1445
DG
1261 arr[1] = cmd[2]; /*sanity */
1262 n = 4;
1263 arr[n++] = 0x0; /* this page */
1264 arr[n++] = 0x80; /* unit serial number */
1265 arr[n++] = 0x83; /* device identification */
1266 arr[n++] = 0x84; /* software interface ident. */
1267 arr[n++] = 0x85; /* management network addresses */
1268 arr[n++] = 0x86; /* extended inquiry */
1269 arr[n++] = 0x87; /* mode page policy */
1270 arr[n++] = 0x88; /* SCSI ports */
1271 arr[n++] = 0x89; /* ATA information */
1272 arr[n++] = 0xb0; /* Block limits (SBC) */
eac6e8e4 1273 arr[n++] = 0xb1; /* Block characteristics (SBC) */
5b94e232
MP
1274 if (scsi_debug_lbp()) /* Logical Block Prov. (SBC) */
1275 arr[n++] = 0xb2;
c65b1445 1276 arr[3] = n - 4; /* number of supported VPD pages */
1da177e4 1277 } else if (0x80 == cmd[2]) { /* unit serial number */
c65b1445 1278 arr[1] = cmd[2]; /*sanity */
1da177e4 1279 arr[3] = len;
c65b1445 1280 memcpy(&arr[4], lu_id_str, len);
1da177e4 1281 } else if (0x83 == cmd[2]) { /* device identification */
c65b1445 1282 arr[1] = cmd[2]; /*sanity */
5a09e398
HR
1283 arr[3] = inquiry_evpd_83(&arr[4], port_group_id,
1284 target_dev_id, lu_id_num,
1285 lu_id_str, len);
c65b1445
DG
1286 } else if (0x84 == cmd[2]) { /* Software interface ident. */
1287 arr[1] = cmd[2]; /*sanity */
1288 arr[3] = inquiry_evpd_84(&arr[4]);
1289 } else if (0x85 == cmd[2]) { /* Management network addresses */
1290 arr[1] = cmd[2]; /*sanity */
1291 arr[3] = inquiry_evpd_85(&arr[4]);
1292 } else if (0x86 == cmd[2]) { /* extended inquiry */
1293 arr[1] = cmd[2]; /*sanity */
1294 arr[3] = 0x3c; /* number of following entries */
c6a44287
MP
1295 if (scsi_debug_dif == SD_DIF_TYPE3_PROTECTION)
1296 arr[4] = 0x4; /* SPT: GRD_CHK:1 */
1297 else if (scsi_debug_dif)
1298 arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */
1299 else
1300 arr[4] = 0x0; /* no protection stuff */
c65b1445
DG
1301 arr[5] = 0x7; /* head of q, ordered + simple q's */
1302 } else if (0x87 == cmd[2]) { /* mode page policy */
1303 arr[1] = cmd[2]; /*sanity */
1304 arr[3] = 0x8; /* number of following entries */
1305 arr[4] = 0x2; /* disconnect-reconnect mp */
1306 arr[6] = 0x80; /* mlus, shared */
1307 arr[8] = 0x18; /* protocol specific lu */
1308 arr[10] = 0x82; /* mlus, per initiator port */
1309 } else if (0x88 == cmd[2]) { /* SCSI Ports */
1310 arr[1] = cmd[2]; /*sanity */
1311 arr[3] = inquiry_evpd_88(&arr[4], target_dev_id);
1312 } else if (0x89 == cmd[2]) { /* ATA information */
1313 arr[1] = cmd[2]; /*sanity */
1314 n = inquiry_evpd_89(&arr[4]);
1315 arr[2] = (n >> 8);
1316 arr[3] = (n & 0xff);
1317 } else if (0xb0 == cmd[2]) { /* Block limits (SBC) */
1318 arr[1] = cmd[2]; /*sanity */
1319 arr[3] = inquiry_evpd_b0(&arr[4]);
eac6e8e4
MW
1320 } else if (0xb1 == cmd[2]) { /* Block characteristics (SBC) */
1321 arr[1] = cmd[2]; /*sanity */
1322 arr[3] = inquiry_evpd_b1(&arr[4]);
5b94e232 1323 } else if (0xb2 == cmd[2]) { /* Logical Block Prov. (SBC) */
6014759c
MP
1324 arr[1] = cmd[2]; /*sanity */
1325 arr[3] = inquiry_evpd_b2(&arr[4]);
1da177e4 1326 } else {
22017ed2 1327 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
5a09e398 1328 kfree(arr);
1da177e4
LT
1329 return check_condition_result;
1330 }
c65b1445 1331 len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
5a09e398 1332 ret = fill_from_dev_buffer(scp, arr,
c65b1445 1333 min(len, SDEBUG_MAX_INQ_ARR_SZ));
5a09e398
HR
1334 kfree(arr);
1335 return ret;
1da177e4
LT
1336 }
1337 /* drops through here for a standard inquiry */
d986788b 1338 arr[1] = scsi_debug_removable ? 0x80 : 0; /* Removable disk */
1da177e4
LT
1339 arr[2] = scsi_debug_scsi_level;
1340 arr[3] = 2; /* response_data_format==2 */
1341 arr[4] = SDEBUG_LONG_INQ_SZ - 5;
c6a44287 1342 arr[5] = scsi_debug_dif ? 1 : 0; /* PROTECT bit */
5a09e398
HR
1343 if (0 == scsi_debug_vpd_use_hostno)
1344 arr[5] = 0x10; /* claim: implicit TGPS */
c65b1445 1345 arr[6] = 0x10; /* claim: MultiP */
1da177e4 1346 /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
c65b1445 1347 arr[7] = 0xa; /* claim: LINKED + CMDQUE */
1da177e4
LT
1348 memcpy(&arr[8], inq_vendor_id, 8);
1349 memcpy(&arr[16], inq_product_id, 16);
1350 memcpy(&arr[32], inq_product_rev, 4);
1351 /* version descriptors (2 bytes each) follow */
e46b0344
DG
1352 arr[58] = 0x0; arr[59] = 0xa2; /* SAM-5 rev 4 */
1353 arr[60] = 0x4; arr[61] = 0x68; /* SPC-4 rev 37 */
c65b1445 1354 n = 62;
1da177e4 1355 if (scsi_debug_ptype == 0) {
e46b0344 1356 arr[n++] = 0x4; arr[n++] = 0xc5; /* SBC-4 rev 36 */
1da177e4 1357 } else if (scsi_debug_ptype == 1) {
e46b0344 1358 arr[n++] = 0x5; arr[n++] = 0x25; /* SSC-4 rev 3 */
1da177e4 1359 }
e46b0344 1360 arr[n++] = 0x20; arr[n++] = 0xe6; /* SPL-3 rev 7 */
5a09e398 1361 ret = fill_from_dev_buffer(scp, arr,
1da177e4 1362 min(alloc_len, SDEBUG_LONG_INQ_SZ));
5a09e398
HR
1363 kfree(arr);
1364 return ret;
1da177e4
LT
1365}
1366
1367static int resp_requests(struct scsi_cmnd * scp,
1368 struct sdebug_dev_info * devip)
1369{
1370 unsigned char * sbuff;
01123ef4 1371 unsigned char *cmd = scp->cmnd;
cbf67842 1372 unsigned char arr[SCSI_SENSE_BUFFERSIZE];
c2248fc9 1373 bool dsense, want_dsense;
1da177e4
LT
1374 int len = 18;
1375
c65b1445 1376 memset(arr, 0, sizeof(arr));
c2248fc9
DG
1377 dsense = !!(cmd[1] & 1);
1378 want_dsense = dsense || scsi_debug_dsense;
cbf67842 1379 sbuff = scp->sense_buffer;
c65b1445 1380 if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
c2248fc9 1381 if (dsense) {
c65b1445
DG
1382 arr[0] = 0x72;
1383 arr[1] = 0x0; /* NO_SENSE in sense_key */
1384 arr[2] = THRESHOLD_EXCEEDED;
1385 arr[3] = 0xff; /* TEST set and MRIE==6 */
c2248fc9 1386 len = 8;
c65b1445
DG
1387 } else {
1388 arr[0] = 0x70;
1389 arr[2] = 0x0; /* NO_SENSE in sense_key */
1390 arr[7] = 0xa; /* 18 byte sense buffer */
1391 arr[12] = THRESHOLD_EXCEEDED;
1392 arr[13] = 0xff; /* TEST set and MRIE==6 */
1393 }
c65b1445 1394 } else {
cbf67842 1395 memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE);
c2248fc9
DG
1396 if (arr[0] >= 0x70 && dsense == scsi_debug_dsense)
1397 ; /* have sense and formats match */
1398 else if (arr[0] <= 0x70) {
1399 if (dsense) {
1400 memset(arr, 0, 8);
1401 arr[0] = 0x72;
1402 len = 8;
1403 } else {
1404 memset(arr, 0, 18);
1405 arr[0] = 0x70;
1406 arr[7] = 0xa;
1407 }
1408 } else if (dsense) {
1409 memset(arr, 0, 8);
c65b1445
DG
1410 arr[0] = 0x72;
1411 arr[1] = sbuff[2]; /* sense key */
1412 arr[2] = sbuff[12]; /* asc */
1413 arr[3] = sbuff[13]; /* ascq */
1414 len = 8;
c2248fc9
DG
1415 } else {
1416 memset(arr, 0, 18);
1417 arr[0] = 0x70;
1418 arr[2] = sbuff[1];
1419 arr[7] = 0xa;
1420 arr[12] = sbuff[1];
1421 arr[13] = sbuff[3];
c65b1445 1422 }
c2248fc9 1423
c65b1445 1424 }
cbf67842 1425 mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0);
1da177e4
LT
1426 return fill_from_dev_buffer(scp, arr, len);
1427}
1428
c65b1445
DG
1429static int resp_start_stop(struct scsi_cmnd * scp,
1430 struct sdebug_dev_info * devip)
1431{
01123ef4 1432 unsigned char *cmd = scp->cmnd;
c2248fc9 1433 int power_cond, start;
c65b1445 1434
c65b1445
DG
1435 power_cond = (cmd[4] & 0xf0) >> 4;
1436 if (power_cond) {
22017ed2 1437 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7);
c65b1445
DG
1438 return check_condition_result;
1439 }
1440 start = cmd[4] & 1;
1441 if (start == devip->stopped)
1442 devip->stopped = !start;
1443 return 0;
1444}
1445
28898873
FT
1446static sector_t get_sdebug_capacity(void)
1447{
1448 if (scsi_debug_virtual_gb > 0)
5447ed6c
DG
1449 return (sector_t)scsi_debug_virtual_gb *
1450 (1073741824 / scsi_debug_sector_size);
28898873
FT
1451 else
1452 return sdebug_store_sectors;
1453}
1454
1da177e4
LT
1455#define SDEBUG_READCAP_ARR_SZ 8
1456static int resp_readcap(struct scsi_cmnd * scp,
1457 struct sdebug_dev_info * devip)
1458{
1459 unsigned char arr[SDEBUG_READCAP_ARR_SZ];
c65b1445 1460 unsigned int capac;
1da177e4 1461
c65b1445 1462 /* following just in case virtual_gb changed */
28898873 1463 sdebug_capacity = get_sdebug_capacity();
1da177e4 1464 memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
c65b1445
DG
1465 if (sdebug_capacity < 0xffffffff) {
1466 capac = (unsigned int)sdebug_capacity - 1;
1467 arr[0] = (capac >> 24);
1468 arr[1] = (capac >> 16) & 0xff;
1469 arr[2] = (capac >> 8) & 0xff;
1470 arr[3] = capac & 0xff;
1471 } else {
1472 arr[0] = 0xff;
1473 arr[1] = 0xff;
1474 arr[2] = 0xff;
1475 arr[3] = 0xff;
1476 }
597136ab
MP
1477 arr[6] = (scsi_debug_sector_size >> 8) & 0xff;
1478 arr[7] = scsi_debug_sector_size & 0xff;
1da177e4
LT
1479 return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
1480}
1481
c65b1445
DG
1482#define SDEBUG_READCAP16_ARR_SZ 32
1483static int resp_readcap16(struct scsi_cmnd * scp,
1484 struct sdebug_dev_info * devip)
1485{
01123ef4 1486 unsigned char *cmd = scp->cmnd;
c65b1445
DG
1487 unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
1488 unsigned long long capac;
c2248fc9 1489 int k, alloc_len;
c65b1445 1490
c65b1445
DG
1491 alloc_len = ((cmd[10] << 24) + (cmd[11] << 16) + (cmd[12] << 8)
1492 + cmd[13]);
1493 /* following just in case virtual_gb changed */
28898873 1494 sdebug_capacity = get_sdebug_capacity();
c65b1445
DG
1495 memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
1496 capac = sdebug_capacity - 1;
1497 for (k = 0; k < 8; ++k, capac >>= 8)
1498 arr[7 - k] = capac & 0xff;
597136ab
MP
1499 arr[8] = (scsi_debug_sector_size >> 24) & 0xff;
1500 arr[9] = (scsi_debug_sector_size >> 16) & 0xff;
1501 arr[10] = (scsi_debug_sector_size >> 8) & 0xff;
1502 arr[11] = scsi_debug_sector_size & 0xff;
ea61fca5
MP
1503 arr[13] = scsi_debug_physblk_exp & 0xf;
1504 arr[14] = (scsi_debug_lowest_aligned >> 8) & 0x3f;
44d92694 1505
be1dd78d 1506 if (scsi_debug_lbp()) {
5b94e232 1507 arr[14] |= 0x80; /* LBPME */
be1dd78d
ES
1508 if (scsi_debug_lbprz)
1509 arr[14] |= 0x40; /* LBPRZ */
1510 }
44d92694 1511
ea61fca5 1512 arr[15] = scsi_debug_lowest_aligned & 0xff;
c6a44287
MP
1513
1514 if (scsi_debug_dif) {
1515 arr[12] = (scsi_debug_dif - 1) << 1; /* P_TYPE */
1516 arr[12] |= 1; /* PROT_EN */
1517 }
1518
c65b1445
DG
1519 return fill_from_dev_buffer(scp, arr,
1520 min(alloc_len, SDEBUG_READCAP16_ARR_SZ));
1521}
1522
5a09e398
HR
1523#define SDEBUG_MAX_TGTPGS_ARR_SZ 1412
1524
1525static int resp_report_tgtpgs(struct scsi_cmnd * scp,
1526 struct sdebug_dev_info * devip)
1527{
01123ef4 1528 unsigned char *cmd = scp->cmnd;
5a09e398
HR
1529 unsigned char * arr;
1530 int host_no = devip->sdbg_host->shost->host_no;
1531 int n, ret, alen, rlen;
1532 int port_group_a, port_group_b, port_a, port_b;
1533
1534 alen = ((cmd[6] << 24) + (cmd[7] << 16) + (cmd[8] << 8)
1535 + cmd[9]);
1536
6f3cbf55
DG
1537 arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC);
1538 if (! arr)
1539 return DID_REQUEUE << 16;
5a09e398
HR
1540 /*
1541 * EVPD page 0x88 states we have two ports, one
1542 * real and a fake port with no device connected.
1543 * So we create two port groups with one port each
1544 * and set the group with port B to unavailable.
1545 */
1546 port_a = 0x1; /* relative port A */
1547 port_b = 0x2; /* relative port B */
1548 port_group_a = (((host_no + 1) & 0x7f) << 8) +
1549 (devip->channel & 0x7f);
1550 port_group_b = (((host_no + 1) & 0x7f) << 8) +
1551 (devip->channel & 0x7f) + 0x80;
1552
1553 /*
1554 * The asymmetric access state is cycled according to the host_id.
1555 */
1556 n = 4;
1557 if (0 == scsi_debug_vpd_use_hostno) {
1558 arr[n++] = host_no % 3; /* Asymm access state */
1559 arr[n++] = 0x0F; /* claim: all states are supported */
1560 } else {
1561 arr[n++] = 0x0; /* Active/Optimized path */
1562 arr[n++] = 0x01; /* claim: only support active/optimized paths */
1563 }
1564 arr[n++] = (port_group_a >> 8) & 0xff;
1565 arr[n++] = port_group_a & 0xff;
1566 arr[n++] = 0; /* Reserved */
1567 arr[n++] = 0; /* Status code */
1568 arr[n++] = 0; /* Vendor unique */
1569 arr[n++] = 0x1; /* One port per group */
1570 arr[n++] = 0; /* Reserved */
1571 arr[n++] = 0; /* Reserved */
1572 arr[n++] = (port_a >> 8) & 0xff;
1573 arr[n++] = port_a & 0xff;
1574 arr[n++] = 3; /* Port unavailable */
1575 arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */
1576 arr[n++] = (port_group_b >> 8) & 0xff;
1577 arr[n++] = port_group_b & 0xff;
1578 arr[n++] = 0; /* Reserved */
1579 arr[n++] = 0; /* Status code */
1580 arr[n++] = 0; /* Vendor unique */
1581 arr[n++] = 0x1; /* One port per group */
1582 arr[n++] = 0; /* Reserved */
1583 arr[n++] = 0; /* Reserved */
1584 arr[n++] = (port_b >> 8) & 0xff;
1585 arr[n++] = port_b & 0xff;
1586
1587 rlen = n - 4;
1588 arr[0] = (rlen >> 24) & 0xff;
1589 arr[1] = (rlen >> 16) & 0xff;
1590 arr[2] = (rlen >> 8) & 0xff;
1591 arr[3] = rlen & 0xff;
1592
1593 /*
1594 * Return the smallest value of either
1595 * - The allocated length
1596 * - The constructed command length
1597 * - The maximum array size
1598 */
1599 rlen = min(alen,n);
1600 ret = fill_from_dev_buffer(scp, arr,
1601 min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ));
1602 kfree(arr);
1603 return ret;
1604}
1605
1da177e4
LT
1606/* <<Following mode page info copied from ST318451LW>> */
1607
1608static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target)
1609{ /* Read-Write Error Recovery page for mode_sense */
1610 unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
1611 5, 0, 0xff, 0xff};
1612
1613 memcpy(p, err_recov_pg, sizeof(err_recov_pg));
1614 if (1 == pcontrol)
1615 memset(p + 2, 0, sizeof(err_recov_pg) - 2);
1616 return sizeof(err_recov_pg);
1617}
1618
1619static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target)
1620{ /* Disconnect-Reconnect page for mode_sense */
1621 unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
1622 0, 0, 0, 0, 0, 0, 0, 0};
1623
1624 memcpy(p, disconnect_pg, sizeof(disconnect_pg));
1625 if (1 == pcontrol)
1626 memset(p + 2, 0, sizeof(disconnect_pg) - 2);
1627 return sizeof(disconnect_pg);
1628}
1629
1630static int resp_format_pg(unsigned char * p, int pcontrol, int target)
1631{ /* Format device page for mode_sense */
597136ab
MP
1632 unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
1633 0, 0, 0, 0, 0, 0, 0, 0,
1634 0, 0, 0, 0, 0x40, 0, 0, 0};
1635
1636 memcpy(p, format_pg, sizeof(format_pg));
1637 p[10] = (sdebug_sectors_per >> 8) & 0xff;
1638 p[11] = sdebug_sectors_per & 0xff;
1639 p[12] = (scsi_debug_sector_size >> 8) & 0xff;
1640 p[13] = scsi_debug_sector_size & 0xff;
d986788b 1641 if (scsi_debug_removable)
597136ab
MP
1642 p[20] |= 0x20; /* should agree with INQUIRY */
1643 if (1 == pcontrol)
1644 memset(p + 2, 0, sizeof(format_pg) - 2);
1645 return sizeof(format_pg);
1da177e4
LT
1646}
1647
1648static int resp_caching_pg(unsigned char * p, int pcontrol, int target)
1649{ /* Caching page for mode_sense */
cbf67842
DG
1650 unsigned char ch_caching_pg[] = {/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0,
1651 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1652 unsigned char d_caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
1da177e4
LT
1653 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
1654
cbf67842
DG
1655 if (SCSI_DEBUG_OPT_N_WCE & scsi_debug_opts)
1656 caching_pg[2] &= ~0x4; /* set WCE=0 (default WCE=1) */
1da177e4
LT
1657 memcpy(p, caching_pg, sizeof(caching_pg));
1658 if (1 == pcontrol)
cbf67842
DG
1659 memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg));
1660 else if (2 == pcontrol)
1661 memcpy(p, d_caching_pg, sizeof(d_caching_pg));
1da177e4
LT
1662 return sizeof(caching_pg);
1663}
1664
1665static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target)
1666{ /* Control mode page for mode_sense */
c65b1445
DG
1667 unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
1668 0, 0, 0, 0};
1669 unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
1da177e4
LT
1670 0, 0, 0x2, 0x4b};
1671
1672 if (scsi_debug_dsense)
1673 ctrl_m_pg[2] |= 0x4;
c65b1445
DG
1674 else
1675 ctrl_m_pg[2] &= ~0x4;
c6a44287
MP
1676
1677 if (scsi_debug_ato)
1678 ctrl_m_pg[5] |= 0x80; /* ATO=1 */
1679
1da177e4
LT
1680 memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
1681 if (1 == pcontrol)
c65b1445
DG
1682 memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
1683 else if (2 == pcontrol)
1684 memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
1da177e4
LT
1685 return sizeof(ctrl_m_pg);
1686}
1687
c65b1445 1688
1da177e4
LT
1689static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target)
1690{ /* Informational Exceptions control mode page for mode_sense */
c65b1445
DG
1691 unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
1692 0, 0, 0x0, 0x0};
1693 unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
1694 0, 0, 0x0, 0x0};
1695
1da177e4
LT
1696 memcpy(p, iec_m_pg, sizeof(iec_m_pg));
1697 if (1 == pcontrol)
c65b1445
DG
1698 memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
1699 else if (2 == pcontrol)
1700 memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
1da177e4
LT
1701 return sizeof(iec_m_pg);
1702}
1703
c65b1445
DG
1704static int resp_sas_sf_m_pg(unsigned char * p, int pcontrol, int target)
1705{ /* SAS SSP mode page - short format for mode_sense */
1706 unsigned char sas_sf_m_pg[] = {0x19, 0x6,
1707 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0};
1708
1709 memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
1710 if (1 == pcontrol)
1711 memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
1712 return sizeof(sas_sf_m_pg);
1713}
1714
1715
1716static int resp_sas_pcd_m_spg(unsigned char * p, int pcontrol, int target,
1717 int target_dev_id)
1718{ /* SAS phy control and discover mode page for mode_sense */
1719 unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
1720 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
1721 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
1722 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
1723 0x2, 0, 0, 0, 0, 0, 0, 0,
1724 0x88, 0x99, 0, 0, 0, 0, 0, 0,
1725 0, 0, 0, 0, 0, 0, 0, 0,
1726 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
1727 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
1728 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
1729 0x3, 0, 0, 0, 0, 0, 0, 0,
1730 0x88, 0x99, 0, 0, 0, 0, 0, 0,
1731 0, 0, 0, 0, 0, 0, 0, 0,
1732 };
1733 int port_a, port_b;
1734
1735 port_a = target_dev_id + 1;
1736 port_b = port_a + 1;
1737 memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
1738 p[20] = (port_a >> 24);
1739 p[21] = (port_a >> 16) & 0xff;
1740 p[22] = (port_a >> 8) & 0xff;
1741 p[23] = port_a & 0xff;
1742 p[48 + 20] = (port_b >> 24);
1743 p[48 + 21] = (port_b >> 16) & 0xff;
1744 p[48 + 22] = (port_b >> 8) & 0xff;
1745 p[48 + 23] = port_b & 0xff;
1746 if (1 == pcontrol)
1747 memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
1748 return sizeof(sas_pcd_m_pg);
1749}
1750
1751static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol)
1752{ /* SAS SSP shared protocol specific port mode subpage */
1753 unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
1754 0, 0, 0, 0, 0, 0, 0, 0,
1755 };
1756
1757 memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
1758 if (1 == pcontrol)
1759 memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
1760 return sizeof(sas_sha_m_pg);
1761}
1762
1da177e4
LT
1763#define SDEBUG_MAX_MSENSE_SZ 256
1764
c2248fc9
DG
1765static int
1766resp_mode_sense(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
1da177e4 1767{
23183910
DG
1768 unsigned char dbd, llbaa;
1769 int pcontrol, pcode, subpcode, bd_len;
1da177e4 1770 unsigned char dev_spec;
c2248fc9
DG
1771 int k, alloc_len, msense_6, offset, len, target_dev_id;
1772 int target = scp->device->id;
1da177e4
LT
1773 unsigned char * ap;
1774 unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
01123ef4 1775 unsigned char *cmd = scp->cmnd;
1da177e4 1776
23183910 1777 dbd = !!(cmd[1] & 0x8);
1da177e4
LT
1778 pcontrol = (cmd[2] & 0xc0) >> 6;
1779 pcode = cmd[2] & 0x3f;
1780 subpcode = cmd[3];
1781 msense_6 = (MODE_SENSE == cmd[0]);
23183910
DG
1782 llbaa = msense_6 ? 0 : !!(cmd[1] & 0x10);
1783 if ((0 == scsi_debug_ptype) && (0 == dbd))
1784 bd_len = llbaa ? 16 : 8;
1785 else
1786 bd_len = 0;
1da177e4
LT
1787 alloc_len = msense_6 ? cmd[4] : ((cmd[7] << 8) | cmd[8]);
1788 memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
1789 if (0x3 == pcontrol) { /* Saving values not supported */
cbf67842 1790 mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0);
1da177e4
LT
1791 return check_condition_result;
1792 }
c65b1445
DG
1793 target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
1794 (devip->target * 1000) - 3;
23183910
DG
1795 /* set DPOFUA bit for disks */
1796 if (0 == scsi_debug_ptype)
1797 dev_spec = (DEV_READONLY(target) ? 0x80 : 0x0) | 0x10;
1798 else
1799 dev_spec = 0x0;
1da177e4
LT
1800 if (msense_6) {
1801 arr[2] = dev_spec;
23183910 1802 arr[3] = bd_len;
1da177e4
LT
1803 offset = 4;
1804 } else {
1805 arr[3] = dev_spec;
23183910
DG
1806 if (16 == bd_len)
1807 arr[4] = 0x1; /* set LONGLBA bit */
1808 arr[7] = bd_len; /* assume 255 or less */
1da177e4
LT
1809 offset = 8;
1810 }
1811 ap = arr + offset;
28898873
FT
1812 if ((bd_len > 0) && (!sdebug_capacity))
1813 sdebug_capacity = get_sdebug_capacity();
1814
23183910
DG
1815 if (8 == bd_len) {
1816 if (sdebug_capacity > 0xfffffffe) {
1817 ap[0] = 0xff;
1818 ap[1] = 0xff;
1819 ap[2] = 0xff;
1820 ap[3] = 0xff;
1821 } else {
1822 ap[0] = (sdebug_capacity >> 24) & 0xff;
1823 ap[1] = (sdebug_capacity >> 16) & 0xff;
1824 ap[2] = (sdebug_capacity >> 8) & 0xff;
1825 ap[3] = sdebug_capacity & 0xff;
1826 }
597136ab
MP
1827 ap[6] = (scsi_debug_sector_size >> 8) & 0xff;
1828 ap[7] = scsi_debug_sector_size & 0xff;
23183910
DG
1829 offset += bd_len;
1830 ap = arr + offset;
1831 } else if (16 == bd_len) {
1832 unsigned long long capac = sdebug_capacity;
1833
1834 for (k = 0; k < 8; ++k, capac >>= 8)
1835 ap[7 - k] = capac & 0xff;
597136ab
MP
1836 ap[12] = (scsi_debug_sector_size >> 24) & 0xff;
1837 ap[13] = (scsi_debug_sector_size >> 16) & 0xff;
1838 ap[14] = (scsi_debug_sector_size >> 8) & 0xff;
1839 ap[15] = scsi_debug_sector_size & 0xff;
23183910
DG
1840 offset += bd_len;
1841 ap = arr + offset;
1842 }
1da177e4 1843
c65b1445
DG
1844 if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) {
1845 /* TODO: Control Extension page */
22017ed2 1846 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
1da177e4
LT
1847 return check_condition_result;
1848 }
1849 switch (pcode) {
1850 case 0x1: /* Read-Write error recovery page, direct access */
1851 len = resp_err_recov_pg(ap, pcontrol, target);
1852 offset += len;
1853 break;
1854 case 0x2: /* Disconnect-Reconnect page, all devices */
1855 len = resp_disconnect_pg(ap, pcontrol, target);
1856 offset += len;
1857 break;
1858 case 0x3: /* Format device page, direct access */
1859 len = resp_format_pg(ap, pcontrol, target);
1860 offset += len;
1861 break;
1862 case 0x8: /* Caching page, direct access */
1863 len = resp_caching_pg(ap, pcontrol, target);
1864 offset += len;
1865 break;
1866 case 0xa: /* Control Mode page, all devices */
1867 len = resp_ctrl_m_pg(ap, pcontrol, target);
1868 offset += len;
1869 break;
c65b1445
DG
1870 case 0x19: /* if spc==1 then sas phy, control+discover */
1871 if ((subpcode > 0x2) && (subpcode < 0xff)) {
22017ed2 1872 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
c65b1445
DG
1873 return check_condition_result;
1874 }
1875 len = 0;
1876 if ((0x0 == subpcode) || (0xff == subpcode))
1877 len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
1878 if ((0x1 == subpcode) || (0xff == subpcode))
1879 len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
1880 target_dev_id);
1881 if ((0x2 == subpcode) || (0xff == subpcode))
1882 len += resp_sas_sha_m_spg(ap + len, pcontrol);
1883 offset += len;
1884 break;
1da177e4
LT
1885 case 0x1c: /* Informational Exceptions Mode page, all devices */
1886 len = resp_iec_m_pg(ap, pcontrol, target);
1887 offset += len;
1888 break;
1889 case 0x3f: /* Read all Mode pages */
c65b1445
DG
1890 if ((0 == subpcode) || (0xff == subpcode)) {
1891 len = resp_err_recov_pg(ap, pcontrol, target);
1892 len += resp_disconnect_pg(ap + len, pcontrol, target);
1893 len += resp_format_pg(ap + len, pcontrol, target);
1894 len += resp_caching_pg(ap + len, pcontrol, target);
1895 len += resp_ctrl_m_pg(ap + len, pcontrol, target);
1896 len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
1897 if (0xff == subpcode) {
1898 len += resp_sas_pcd_m_spg(ap + len, pcontrol,
1899 target, target_dev_id);
1900 len += resp_sas_sha_m_spg(ap + len, pcontrol);
1901 }
1902 len += resp_iec_m_pg(ap + len, pcontrol, target);
1903 } else {
22017ed2 1904 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
c65b1445
DG
1905 return check_condition_result;
1906 }
1da177e4
LT
1907 offset += len;
1908 break;
1909 default:
22017ed2 1910 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
1da177e4
LT
1911 return check_condition_result;
1912 }
1913 if (msense_6)
1914 arr[0] = offset - 1;
1915 else {
1916 arr[0] = ((offset - 2) >> 8) & 0xff;
1917 arr[1] = (offset - 2) & 0xff;
1918 }
1919 return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
1920}
1921
c65b1445
DG
1922#define SDEBUG_MAX_MSELECT_SZ 512
1923
c2248fc9
DG
1924static int
1925resp_mode_select(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
c65b1445
DG
1926{
1927 int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
c2248fc9 1928 int param_len, res, mpage;
c65b1445 1929 unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
01123ef4 1930 unsigned char *cmd = scp->cmnd;
c2248fc9 1931 int mselect6 = (MODE_SELECT == cmd[0]);
c65b1445 1932
c65b1445
DG
1933 memset(arr, 0, sizeof(arr));
1934 pf = cmd[1] & 0x10;
1935 sp = cmd[1] & 0x1;
1936 param_len = mselect6 ? cmd[4] : ((cmd[7] << 8) + cmd[8]);
1937 if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
22017ed2 1938 mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1);
c65b1445
DG
1939 return check_condition_result;
1940 }
1941 res = fetch_to_dev_buffer(scp, arr, param_len);
1942 if (-1 == res)
1943 return (DID_ERROR << 16);
1944 else if ((res < param_len) &&
1945 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
cbf67842
DG
1946 sdev_printk(KERN_INFO, scp->device,
1947 "%s: cdb indicated=%d, IO sent=%d bytes\n",
1948 __func__, param_len, res);
c65b1445
DG
1949 md_len = mselect6 ? (arr[0] + 1) : ((arr[0] << 8) + arr[1] + 2);
1950 bd_len = mselect6 ? arr[3] : ((arr[6] << 8) + arr[7]);
23183910 1951 if (md_len > 2) {
22017ed2 1952 mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1);
c65b1445
DG
1953 return check_condition_result;
1954 }
1955 off = bd_len + (mselect6 ? 4 : 8);
1956 mpage = arr[off] & 0x3f;
1957 ps = !!(arr[off] & 0x80);
1958 if (ps) {
22017ed2 1959 mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7);
c65b1445
DG
1960 return check_condition_result;
1961 }
1962 spf = !!(arr[off] & 0x40);
1963 pg_len = spf ? ((arr[off + 2] << 8) + arr[off + 3] + 4) :
1964 (arr[off + 1] + 2);
1965 if ((pg_len + off) > param_len) {
cbf67842 1966 mk_sense_buffer(scp, ILLEGAL_REQUEST,
c65b1445
DG
1967 PARAMETER_LIST_LENGTH_ERR, 0);
1968 return check_condition_result;
1969 }
1970 switch (mpage) {
cbf67842
DG
1971 case 0x8: /* Caching Mode page */
1972 if (caching_pg[1] == arr[off + 1]) {
1973 memcpy(caching_pg + 2, arr + off + 2,
1974 sizeof(caching_pg) - 2);
1975 goto set_mode_changed_ua;
1976 }
1977 break;
c65b1445
DG
1978 case 0xa: /* Control Mode page */
1979 if (ctrl_m_pg[1] == arr[off + 1]) {
1980 memcpy(ctrl_m_pg + 2, arr + off + 2,
1981 sizeof(ctrl_m_pg) - 2);
1982 scsi_debug_dsense = !!(ctrl_m_pg[2] & 0x4);
cbf67842 1983 goto set_mode_changed_ua;
c65b1445
DG
1984 }
1985 break;
1986 case 0x1c: /* Informational Exceptions Mode page */
1987 if (iec_m_pg[1] == arr[off + 1]) {
1988 memcpy(iec_m_pg + 2, arr + off + 2,
1989 sizeof(iec_m_pg) - 2);
cbf67842 1990 goto set_mode_changed_ua;
c65b1445
DG
1991 }
1992 break;
1993 default:
1994 break;
1995 }
22017ed2 1996 mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5);
c65b1445 1997 return check_condition_result;
cbf67842
DG
1998set_mode_changed_ua:
1999 set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm);
2000 return 0;
c65b1445
DG
2001}
2002
2003static int resp_temp_l_pg(unsigned char * arr)
2004{
2005 unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38,
2006 0x0, 0x1, 0x3, 0x2, 0x0, 65,
2007 };
2008
2009 memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
2010 return sizeof(temp_l_pg);
2011}
2012
2013static int resp_ie_l_pg(unsigned char * arr)
2014{
2015 unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
2016 };
2017
2018 memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
2019 if (iec_m_pg[2] & 0x4) { /* TEST bit set */
2020 arr[4] = THRESHOLD_EXCEEDED;
2021 arr[5] = 0xff;
2022 }
2023 return sizeof(ie_l_pg);
2024}
2025
2026#define SDEBUG_MAX_LSENSE_SZ 512
2027
2028static int resp_log_sense(struct scsi_cmnd * scp,
2029 struct sdebug_dev_info * devip)
2030{
c2248fc9 2031 int ppc, sp, pcontrol, pcode, subpcode, alloc_len, len, n;
c65b1445 2032 unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
01123ef4 2033 unsigned char *cmd = scp->cmnd;
c65b1445 2034
c65b1445
DG
2035 memset(arr, 0, sizeof(arr));
2036 ppc = cmd[1] & 0x2;
2037 sp = cmd[1] & 0x1;
2038 if (ppc || sp) {
22017ed2 2039 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0);
c65b1445
DG
2040 return check_condition_result;
2041 }
2042 pcontrol = (cmd[2] & 0xc0) >> 6;
2043 pcode = cmd[2] & 0x3f;
23183910 2044 subpcode = cmd[3] & 0xff;
c65b1445
DG
2045 alloc_len = (cmd[7] << 8) + cmd[8];
2046 arr[0] = pcode;
23183910
DG
2047 if (0 == subpcode) {
2048 switch (pcode) {
2049 case 0x0: /* Supported log pages log page */
2050 n = 4;
2051 arr[n++] = 0x0; /* this page */
2052 arr[n++] = 0xd; /* Temperature */
2053 arr[n++] = 0x2f; /* Informational exceptions */
2054 arr[3] = n - 4;
2055 break;
2056 case 0xd: /* Temperature log page */
2057 arr[3] = resp_temp_l_pg(arr + 4);
2058 break;
2059 case 0x2f: /* Informational exceptions log page */
2060 arr[3] = resp_ie_l_pg(arr + 4);
2061 break;
2062 default:
22017ed2 2063 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
23183910
DG
2064 return check_condition_result;
2065 }
2066 } else if (0xff == subpcode) {
2067 arr[0] |= 0x40;
2068 arr[1] = subpcode;
2069 switch (pcode) {
2070 case 0x0: /* Supported log pages and subpages log page */
2071 n = 4;
2072 arr[n++] = 0x0;
2073 arr[n++] = 0x0; /* 0,0 page */
2074 arr[n++] = 0x0;
2075 arr[n++] = 0xff; /* this page */
2076 arr[n++] = 0xd;
2077 arr[n++] = 0x0; /* Temperature */
2078 arr[n++] = 0x2f;
2079 arr[n++] = 0x0; /* Informational exceptions */
2080 arr[3] = n - 4;
2081 break;
2082 case 0xd: /* Temperature subpages */
2083 n = 4;
2084 arr[n++] = 0xd;
2085 arr[n++] = 0x0; /* Temperature */
2086 arr[3] = n - 4;
2087 break;
2088 case 0x2f: /* Informational exceptions subpages */
2089 n = 4;
2090 arr[n++] = 0x2f;
2091 arr[n++] = 0x0; /* Informational exceptions */
2092 arr[3] = n - 4;
2093 break;
2094 default:
22017ed2 2095 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
23183910
DG
2096 return check_condition_result;
2097 }
2098 } else {
22017ed2 2099 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
c65b1445
DG
2100 return check_condition_result;
2101 }
2102 len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
2103 return fill_from_dev_buffer(scp, arr,
2104 min(len, SDEBUG_MAX_INQ_ARR_SZ));
2105}
2106
cbf67842 2107static int check_device_access_params(struct scsi_cmnd *scp,
19789100 2108 unsigned long long lba, unsigned int num)
1da177e4 2109{
c65b1445 2110 if (lba + num > sdebug_capacity) {
22017ed2 2111 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
1da177e4
LT
2112 return check_condition_result;
2113 }
c65b1445
DG
2114 /* transfer length excessive (tie in to block limits VPD page) */
2115 if (num > sdebug_store_sectors) {
22017ed2 2116 /* needs work to find which cdb byte 'num' comes from */
cbf67842 2117 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
c65b1445
DG
2118 return check_condition_result;
2119 }
19789100
FT
2120 return 0;
2121}
2122
a4517511 2123/* Returns number of bytes copied or -1 if error. */
c2248fc9
DG
2124static int
2125do_device_access(struct scsi_cmnd *scmd, u64 lba, u32 num, bool do_write)
19789100
FT
2126{
2127 int ret;
c2248fc9 2128 u64 block, rest = 0;
a4517511
AM
2129 struct scsi_data_buffer *sdb;
2130 enum dma_data_direction dir;
2131 size_t (*func)(struct scatterlist *, unsigned int, void *, size_t,
2132 off_t);
2133
c2248fc9 2134 if (do_write) {
a4517511
AM
2135 sdb = scsi_out(scmd);
2136 dir = DMA_TO_DEVICE;
2137 func = sg_pcopy_to_buffer;
2138 } else {
2139 sdb = scsi_in(scmd);
2140 dir = DMA_FROM_DEVICE;
2141 func = sg_pcopy_from_buffer;
2142 }
19789100 2143
a4517511
AM
2144 if (!sdb->length)
2145 return 0;
2146 if (!(scsi_bidi_cmnd(scmd) || scmd->sc_data_direction == dir))
2147 return -1;
19789100
FT
2148
2149 block = do_div(lba, sdebug_store_sectors);
2150 if (block + num > sdebug_store_sectors)
2151 rest = block + num - sdebug_store_sectors;
2152
a4517511
AM
2153 ret = func(sdb->table.sgl, sdb->table.nents,
2154 fake_storep + (block * scsi_debug_sector_size),
2155 (num - rest) * scsi_debug_sector_size, 0);
2156 if (ret != (num - rest) * scsi_debug_sector_size)
2157 return ret;
2158
2159 if (rest) {
2160 ret += func(sdb->table.sgl, sdb->table.nents,
2161 fake_storep, rest * scsi_debug_sector_size,
2162 (num - rest) * scsi_debug_sector_size);
2163 }
19789100
FT
2164
2165 return ret;
2166}
2167
51d648af 2168static __be16 dif_compute_csum(const void *buf, int len)
beb40ea4 2169{
51d648af 2170 __be16 csum;
beb40ea4 2171
51d648af
AM
2172 if (scsi_debug_guard)
2173 csum = (__force __be16)ip_compute_csum(buf, len);
2174 else
beb40ea4 2175 csum = cpu_to_be16(crc_t10dif(buf, len));
51d648af 2176
beb40ea4
AM
2177 return csum;
2178}
2179
2180static int dif_verify(struct sd_dif_tuple *sdt, const void *data,
2181 sector_t sector, u32 ei_lba)
2182{
51d648af 2183 __be16 csum = dif_compute_csum(data, scsi_debug_sector_size);
beb40ea4
AM
2184
2185 if (sdt->guard_tag != csum) {
2186 pr_err("%s: GUARD check failed on sector %lu rcvd 0x%04x, data 0x%04x\n",
2187 __func__,
2188 (unsigned long)sector,
2189 be16_to_cpu(sdt->guard_tag),
2190 be16_to_cpu(csum));
2191 return 0x01;
2192 }
2193 if (scsi_debug_dif == SD_DIF_TYPE1_PROTECTION &&
2194 be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
2195 pr_err("%s: REF check failed on sector %lu\n",
2196 __func__, (unsigned long)sector);
2197 return 0x03;
2198 }
2199 if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION &&
2200 be32_to_cpu(sdt->ref_tag) != ei_lba) {
2201 pr_err("%s: REF check failed on sector %lu\n",
2202 __func__, (unsigned long)sector);
beb40ea4
AM
2203 return 0x03;
2204 }
2205 return 0;
2206}
2207
bb8c063c 2208static void dif_copy_prot(struct scsi_cmnd *SCpnt, sector_t sector,
65f72f2a 2209 unsigned int sectors, bool read)
c6a44287 2210{
be4e11be 2211 size_t resid;
c6a44287 2212 void *paddr;
14faa944 2213 const void *dif_store_end = dif_storep + sdebug_store_sectors;
be4e11be 2214 struct sg_mapping_iter miter;
c6a44287 2215
e18d8bea
AM
2216 /* Bytes of protection data to copy into sgl */
2217 resid = sectors * sizeof(*dif_storep);
c6a44287 2218
be4e11be
AM
2219 sg_miter_start(&miter, scsi_prot_sglist(SCpnt),
2220 scsi_prot_sg_count(SCpnt), SG_MITER_ATOMIC |
2221 (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG));
2222
2223 while (sg_miter_next(&miter) && resid > 0) {
2224 size_t len = min(miter.length, resid);
14faa944 2225 void *start = dif_store(sector);
be4e11be 2226 size_t rest = 0;
14faa944
AM
2227
2228 if (dif_store_end < start + len)
2229 rest = start + len - dif_store_end;
c6a44287 2230
be4e11be 2231 paddr = miter.addr;
14faa944 2232
65f72f2a
AM
2233 if (read)
2234 memcpy(paddr, start, len - rest);
2235 else
2236 memcpy(start, paddr, len - rest);
2237
2238 if (rest) {
2239 if (read)
2240 memcpy(paddr + len - rest, dif_storep, rest);
2241 else
2242 memcpy(dif_storep, paddr + len - rest, rest);
2243 }
c6a44287 2244
e18d8bea 2245 sector += len / sizeof(*dif_storep);
c6a44287 2246 resid -= len;
c6a44287 2247 }
be4e11be 2248 sg_miter_stop(&miter);
bb8c063c
AM
2249}
2250
2251static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec,
2252 unsigned int sectors, u32 ei_lba)
2253{
2254 unsigned int i;
2255 struct sd_dif_tuple *sdt;
2256 sector_t sector;
2257
c45eabec 2258 for (i = 0; i < sectors; i++, ei_lba++) {
bb8c063c
AM
2259 int ret;
2260
2261 sector = start_sec + i;
2262 sdt = dif_store(sector);
2263
51d648af 2264 if (sdt->app_tag == cpu_to_be16(0xffff))
bb8c063c
AM
2265 continue;
2266
2267 ret = dif_verify(sdt, fake_store(sector), sector, ei_lba);
2268 if (ret) {
2269 dif_errors++;
2270 return ret;
2271 }
bb8c063c 2272 }
c6a44287 2273
65f72f2a 2274 dif_copy_prot(SCpnt, start_sec, sectors, true);
c6a44287
MP
2275 dix_reads++;
2276
2277 return 0;
2278}
2279
c2248fc9
DG
2280static int
2281resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
19789100 2282{
c2248fc9
DG
2283 u8 *cmd = scp->cmnd;
2284 u64 lba;
2285 u32 num;
2286 u32 ei_lba;
19789100
FT
2287 unsigned long iflags;
2288 int ret;
c2248fc9 2289 bool check_prot;
19789100 2290
c2248fc9
DG
2291 switch (cmd[0]) {
2292 case READ_16:
2293 ei_lba = 0;
2294 lba = get_unaligned_be64(cmd + 2);
2295 num = get_unaligned_be32(cmd + 10);
2296 check_prot = true;
2297 break;
2298 case READ_10:
2299 ei_lba = 0;
2300 lba = get_unaligned_be32(cmd + 2);
2301 num = get_unaligned_be16(cmd + 7);
2302 check_prot = true;
2303 break;
2304 case READ_6:
2305 ei_lba = 0;
2306 lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
2307 (u32)(cmd[1] & 0x1f) << 16;
2308 num = (0 == cmd[4]) ? 256 : cmd[4];
2309 check_prot = true;
2310 break;
2311 case READ_12:
2312 ei_lba = 0;
2313 lba = get_unaligned_be32(cmd + 2);
2314 num = get_unaligned_be32(cmd + 6);
2315 check_prot = true;
2316 break;
2317 case XDWRITEREAD_10:
2318 ei_lba = 0;
2319 lba = get_unaligned_be32(cmd + 2);
2320 num = get_unaligned_be16(cmd + 7);
2321 check_prot = false;
2322 break;
2323 default: /* assume READ(32) */
2324 lba = get_unaligned_be64(cmd + 12);
2325 ei_lba = get_unaligned_be32(cmd + 20);
2326 num = get_unaligned_be32(cmd + 28);
2327 check_prot = false;
2328 break;
2329 }
2330 if (check_prot) {
2331 if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION &&
2332 (cmd[1] & 0xe0)) {
2333 mk_sense_invalid_opcode(scp);
2334 return check_condition_result;
2335 }
2336 if ((scsi_debug_dif == SD_DIF_TYPE1_PROTECTION ||
2337 scsi_debug_dif == SD_DIF_TYPE3_PROTECTION) &&
2338 (cmd[1] & 0xe0) == 0)
2339 sdev_printk(KERN_ERR, scp->device, "Unprotected RD "
2340 "to DIF device\n");
2341 }
2342 if (sdebug_any_injecting_opt) {
2343 struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp);
2344
2345 if (ep->inj_short)
2346 num /= 2;
2347 }
2348
2349 /* inline check_device_access_params() */
2350 if (lba + num > sdebug_capacity) {
2351 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
2352 return check_condition_result;
2353 }
2354 /* transfer length excessive (tie in to block limits VPD page) */
2355 if (num > sdebug_store_sectors) {
2356 /* needs work to find which cdb byte 'num' comes from */
2357 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
2358 return check_condition_result;
2359 }
19789100 2360
1da177e4 2361 if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) &&
32f7ef73 2362 (lba <= (OPT_MEDIUM_ERR_ADDR + OPT_MEDIUM_ERR_NUM - 1)) &&
c65b1445
DG
2363 ((lba + num) > OPT_MEDIUM_ERR_ADDR)) {
2364 /* claim unrecoverable read error */
c2248fc9 2365 mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0);
c65b1445 2366 /* set info field and valid bit for fixed descriptor */
c2248fc9
DG
2367 if (0x70 == (scp->sense_buffer[0] & 0x7f)) {
2368 scp->sense_buffer[0] |= 0x80; /* Valid bit */
32f7ef73
DG
2369 ret = (lba < OPT_MEDIUM_ERR_ADDR)
2370 ? OPT_MEDIUM_ERR_ADDR : (int)lba;
c2248fc9 2371 put_unaligned_be32(ret, scp->sense_buffer + 3);
c65b1445 2372 }
c2248fc9 2373 scsi_set_resid(scp, scsi_bufflen(scp));
1da177e4
LT
2374 return check_condition_result;
2375 }
c6a44287 2376
6c78cc06
AM
2377 read_lock_irqsave(&atomic_rw, iflags);
2378
c6a44287 2379 /* DIX + T10 DIF */
c2248fc9
DG
2380 if (scsi_debug_dix && scsi_prot_sg_count(scp)) {
2381 int prot_ret = prot_verify_read(scp, lba, num, ei_lba);
c6a44287
MP
2382
2383 if (prot_ret) {
6c78cc06 2384 read_unlock_irqrestore(&atomic_rw, iflags);
c2248fc9 2385 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret);
c6a44287
MP
2386 return illegal_condition_result;
2387 }
2388 }
2389
c2248fc9 2390 ret = do_device_access(scp, lba, num, false);
1da177e4 2391 read_unlock_irqrestore(&atomic_rw, iflags);
a4517511
AM
2392 if (ret == -1)
2393 return DID_ERROR << 16;
2394
c2248fc9 2395 scsi_in(scp)->resid = scsi_bufflen(scp) - ret;
a4517511 2396
c2248fc9
DG
2397 if (sdebug_any_injecting_opt) {
2398 struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp);
2399
2400 if (ep->inj_recovered) {
2401 mk_sense_buffer(scp, RECOVERED_ERROR,
2402 THRESHOLD_EXCEEDED, 0);
2403 return check_condition_result;
2404 } else if (ep->inj_transport) {
2405 mk_sense_buffer(scp, ABORTED_COMMAND,
2406 TRANSPORT_PROBLEM, ACK_NAK_TO);
2407 return check_condition_result;
2408 } else if (ep->inj_dif) {
2409 /* Logical block guard check failed */
2410 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
2411 return illegal_condition_result;
2412 } else if (ep->inj_dix) {
2413 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
2414 return illegal_condition_result;
2415 }
2416 }
a4517511 2417 return 0;
1da177e4
LT
2418}
2419
c6a44287
MP
2420void dump_sector(unsigned char *buf, int len)
2421{
cbf67842 2422 int i, j, n;
c6a44287 2423
cbf67842 2424 pr_err(">>> Sector Dump <<<\n");
c6a44287 2425 for (i = 0 ; i < len ; i += 16) {
cbf67842 2426 char b[128];
c6a44287 2427
cbf67842 2428 for (j = 0, n = 0; j < 16; j++) {
c6a44287 2429 unsigned char c = buf[i+j];
cbf67842 2430
c6a44287 2431 if (c >= 0x20 && c < 0x7e)
cbf67842
DG
2432 n += scnprintf(b + n, sizeof(b) - n,
2433 " %c ", buf[i+j]);
c6a44287 2434 else
cbf67842
DG
2435 n += scnprintf(b + n, sizeof(b) - n,
2436 "%02x ", buf[i+j]);
c6a44287 2437 }
cbf67842 2438 pr_err("%04d: %s\n", i, b);
c6a44287
MP
2439 }
2440}
2441
2442static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec,
395cef03 2443 unsigned int sectors, u32 ei_lba)
c6a44287 2444{
be4e11be 2445 int ret;
c6a44287 2446 struct sd_dif_tuple *sdt;
be4e11be 2447 void *daddr;
65f72f2a 2448 sector_t sector = start_sec;
c6a44287 2449 int ppage_offset;
be4e11be
AM
2450 int dpage_offset;
2451 struct sg_mapping_iter diter;
2452 struct sg_mapping_iter piter;
c6a44287 2453
c6a44287
MP
2454 BUG_ON(scsi_sg_count(SCpnt) == 0);
2455 BUG_ON(scsi_prot_sg_count(SCpnt) == 0);
2456
be4e11be
AM
2457 sg_miter_start(&piter, scsi_prot_sglist(SCpnt),
2458 scsi_prot_sg_count(SCpnt),
2459 SG_MITER_ATOMIC | SG_MITER_FROM_SG);
2460 sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt),
2461 SG_MITER_ATOMIC | SG_MITER_FROM_SG);
2462
2463 /* For each protection page */
2464 while (sg_miter_next(&piter)) {
2465 dpage_offset = 0;
2466 if (WARN_ON(!sg_miter_next(&diter))) {
2467 ret = 0x01;
2468 goto out;
2469 }
c6a44287 2470
be4e11be
AM
2471 for (ppage_offset = 0; ppage_offset < piter.length;
2472 ppage_offset += sizeof(struct sd_dif_tuple)) {
c6a44287 2473 /* If we're at the end of the current
be4e11be 2474 * data page advance to the next one
c6a44287 2475 */
be4e11be
AM
2476 if (dpage_offset >= diter.length) {
2477 if (WARN_ON(!sg_miter_next(&diter))) {
2478 ret = 0x01;
2479 goto out;
2480 }
2481 dpage_offset = 0;
c6a44287
MP
2482 }
2483
be4e11be
AM
2484 sdt = piter.addr + ppage_offset;
2485 daddr = diter.addr + dpage_offset;
c6a44287 2486
be4e11be 2487 ret = dif_verify(sdt, daddr, sector, ei_lba);
beb40ea4 2488 if (ret) {
be4e11be 2489 dump_sector(daddr, scsi_debug_sector_size);
395cef03
MP
2490 goto out;
2491 }
2492
c6a44287 2493 sector++;
395cef03 2494 ei_lba++;
be4e11be 2495 dpage_offset += scsi_debug_sector_size;
c6a44287 2496 }
be4e11be
AM
2497 diter.consumed = dpage_offset;
2498 sg_miter_stop(&diter);
c6a44287 2499 }
be4e11be 2500 sg_miter_stop(&piter);
c6a44287 2501
65f72f2a 2502 dif_copy_prot(SCpnt, start_sec, sectors, false);
c6a44287
MP
2503 dix_writes++;
2504
2505 return 0;
2506
2507out:
2508 dif_errors++;
be4e11be
AM
2509 sg_miter_stop(&diter);
2510 sg_miter_stop(&piter);
c6a44287
MP
2511 return ret;
2512}
2513
b90ebc3d
AM
2514static unsigned long lba_to_map_index(sector_t lba)
2515{
2516 if (scsi_debug_unmap_alignment) {
2517 lba += scsi_debug_unmap_granularity -
2518 scsi_debug_unmap_alignment;
2519 }
2520 do_div(lba, scsi_debug_unmap_granularity);
2521
2522 return lba;
2523}
2524
2525static sector_t map_index_to_lba(unsigned long index)
44d92694 2526{
a027b5b9
AM
2527 sector_t lba = index * scsi_debug_unmap_granularity;
2528
2529 if (scsi_debug_unmap_alignment) {
2530 lba -= scsi_debug_unmap_granularity -
2531 scsi_debug_unmap_alignment;
2532 }
2533
2534 return lba;
b90ebc3d 2535}
44d92694 2536
b90ebc3d
AM
2537static unsigned int map_state(sector_t lba, unsigned int *num)
2538{
2539 sector_t end;
2540 unsigned int mapped;
2541 unsigned long index;
2542 unsigned long next;
44d92694 2543
b90ebc3d
AM
2544 index = lba_to_map_index(lba);
2545 mapped = test_bit(index, map_storep);
44d92694
MP
2546
2547 if (mapped)
b90ebc3d 2548 next = find_next_zero_bit(map_storep, map_size, index);
44d92694 2549 else
b90ebc3d 2550 next = find_next_bit(map_storep, map_size, index);
44d92694 2551
b90ebc3d 2552 end = min_t(sector_t, sdebug_store_sectors, map_index_to_lba(next));
44d92694
MP
2553 *num = end - lba;
2554
2555 return mapped;
2556}
2557
2558static void map_region(sector_t lba, unsigned int len)
2559{
44d92694
MP
2560 sector_t end = lba + len;
2561
44d92694 2562 while (lba < end) {
b90ebc3d 2563 unsigned long index = lba_to_map_index(lba);
44d92694 2564
b90ebc3d
AM
2565 if (index < map_size)
2566 set_bit(index, map_storep);
44d92694 2567
b90ebc3d 2568 lba = map_index_to_lba(index + 1);
44d92694
MP
2569 }
2570}
2571
2572static void unmap_region(sector_t lba, unsigned int len)
2573{
44d92694
MP
2574 sector_t end = lba + len;
2575
44d92694 2576 while (lba < end) {
b90ebc3d 2577 unsigned long index = lba_to_map_index(lba);
44d92694 2578
b90ebc3d
AM
2579 if (lba == map_index_to_lba(index) &&
2580 lba + scsi_debug_unmap_granularity <= end &&
2581 index < map_size) {
2582 clear_bit(index, map_storep);
2583 if (scsi_debug_lbprz) {
be1dd78d 2584 memset(fake_storep +
cc34a8e6
AM
2585 lba * scsi_debug_sector_size, 0,
2586 scsi_debug_sector_size *
2587 scsi_debug_unmap_granularity);
b90ebc3d 2588 }
e9926b43
AM
2589 if (dif_storep) {
2590 memset(dif_storep + lba, 0xff,
2591 sizeof(*dif_storep) *
2592 scsi_debug_unmap_granularity);
2593 }
be1dd78d 2594 }
b90ebc3d 2595 lba = map_index_to_lba(index + 1);
44d92694
MP
2596 }
2597}
2598
c2248fc9
DG
2599static int
2600resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
1da177e4 2601{
c2248fc9
DG
2602 u8 *cmd = scp->cmnd;
2603 u64 lba;
2604 u32 num;
2605 u32 ei_lba;
1da177e4 2606 unsigned long iflags;
19789100 2607 int ret;
c2248fc9 2608 bool check_prot;
1da177e4 2609
c2248fc9
DG
2610 switch (cmd[0]) {
2611 case WRITE_16:
2612 ei_lba = 0;
2613 lba = get_unaligned_be64(cmd + 2);
2614 num = get_unaligned_be32(cmd + 10);
2615 check_prot = true;
2616 break;
2617 case WRITE_10:
2618 ei_lba = 0;
2619 lba = get_unaligned_be32(cmd + 2);
2620 num = get_unaligned_be16(cmd + 7);
2621 check_prot = true;
2622 break;
2623 case WRITE_6:
2624 ei_lba = 0;
2625 lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
2626 (u32)(cmd[1] & 0x1f) << 16;
2627 num = (0 == cmd[4]) ? 256 : cmd[4];
2628 check_prot = true;
2629 break;
2630 case WRITE_12:
2631 ei_lba = 0;
2632 lba = get_unaligned_be32(cmd + 2);
2633 num = get_unaligned_be32(cmd + 6);
2634 check_prot = true;
2635 break;
2636 case 0x53: /* XDWRITEREAD(10) */
2637 ei_lba = 0;
2638 lba = get_unaligned_be32(cmd + 2);
2639 num = get_unaligned_be16(cmd + 7);
2640 check_prot = false;
2641 break;
2642 default: /* assume WRITE(32) */
2643 lba = get_unaligned_be64(cmd + 12);
2644 ei_lba = get_unaligned_be32(cmd + 20);
2645 num = get_unaligned_be32(cmd + 28);
2646 check_prot = false;
2647 break;
2648 }
2649 if (check_prot) {
2650 if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION &&
2651 (cmd[1] & 0xe0)) {
2652 mk_sense_invalid_opcode(scp);
2653 return check_condition_result;
2654 }
2655 if ((scsi_debug_dif == SD_DIF_TYPE1_PROTECTION ||
2656 scsi_debug_dif == SD_DIF_TYPE3_PROTECTION) &&
2657 (cmd[1] & 0xe0) == 0)
2658 sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
2659 "to DIF device\n");
2660 }
2661
2662 /* inline check_device_access_params() */
2663 if (lba + num > sdebug_capacity) {
2664 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
2665 return check_condition_result;
2666 }
2667 /* transfer length excessive (tie in to block limits VPD page) */
2668 if (num > sdebug_store_sectors) {
2669 /* needs work to find which cdb byte 'num' comes from */
2670 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
2671 return check_condition_result;
2672 }
1da177e4 2673
6c78cc06
AM
2674 write_lock_irqsave(&atomic_rw, iflags);
2675
c6a44287 2676 /* DIX + T10 DIF */
c2248fc9
DG
2677 if (scsi_debug_dix && scsi_prot_sg_count(scp)) {
2678 int prot_ret = prot_verify_write(scp, lba, num, ei_lba);
c6a44287
MP
2679
2680 if (prot_ret) {
6c78cc06 2681 write_unlock_irqrestore(&atomic_rw, iflags);
c2248fc9 2682 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret);
c6a44287
MP
2683 return illegal_condition_result;
2684 }
2685 }
2686
c2248fc9 2687 ret = do_device_access(scp, lba, num, true);
9ed8d3dc 2688 if (scsi_debug_lbp())
44d92694 2689 map_region(lba, num);
1da177e4 2690 write_unlock_irqrestore(&atomic_rw, iflags);
19789100 2691 if (-1 == ret)
1da177e4 2692 return (DID_ERROR << 16);
597136ab 2693 else if ((ret < (num * scsi_debug_sector_size)) &&
1da177e4 2694 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
c2248fc9 2695 sdev_printk(KERN_INFO, scp->device,
cbf67842
DG
2696 "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
2697 my_name, num * scsi_debug_sector_size, ret);
44d92694 2698
c2248fc9
DG
2699 if (sdebug_any_injecting_opt) {
2700 struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp);
2701
2702 if (ep->inj_recovered) {
2703 mk_sense_buffer(scp, RECOVERED_ERROR,
2704 THRESHOLD_EXCEEDED, 0);
2705 return check_condition_result;
2706 } else if (ep->inj_dif) {
2707 /* Logical block guard check failed */
2708 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
2709 return illegal_condition_result;
2710 } else if (ep->inj_dix) {
2711 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
2712 return illegal_condition_result;
2713 }
2714 }
44d92694
MP
2715 return 0;
2716}
2717
c2248fc9
DG
2718static int
2719resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num, u32 ei_lba,
2720 bool unmap, bool ndob)
44d92694
MP
2721{
2722 unsigned long iflags;
2723 unsigned long long i;
2724 int ret;
2725
c2248fc9 2726 ret = check_device_access_params(scp, lba, num);
44d92694
MP
2727 if (ret)
2728 return ret;
2729
2730 write_lock_irqsave(&atomic_rw, iflags);
2731
9ed8d3dc 2732 if (unmap && scsi_debug_lbp()) {
44d92694
MP
2733 unmap_region(lba, num);
2734 goto out;
2735 }
2736
c2248fc9
DG
2737 /* if ndob then zero 1 logical block, else fetch 1 logical block */
2738 if (ndob) {
2739 memset(fake_storep + (lba * scsi_debug_sector_size), 0,
2740 scsi_debug_sector_size);
2741 ret = 0;
2742 } else
2743 ret = fetch_to_dev_buffer(scp, fake_storep +
2744 (lba * scsi_debug_sector_size),
2745 scsi_debug_sector_size);
44d92694
MP
2746
2747 if (-1 == ret) {
2748 write_unlock_irqrestore(&atomic_rw, iflags);
2749 return (DID_ERROR << 16);
2750 } else if ((ret < (num * scsi_debug_sector_size)) &&
2751 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
c2248fc9 2752 sdev_printk(KERN_INFO, scp->device,
cbf67842
DG
2753 "%s: %s: cdb indicated=%u, IO sent=%d bytes\n",
2754 my_name, "write same",
2755 num * scsi_debug_sector_size, ret);
44d92694
MP
2756
2757 /* Copy first sector to remaining blocks */
2758 for (i = 1 ; i < num ; i++)
2759 memcpy(fake_storep + ((lba + i) * scsi_debug_sector_size),
2760 fake_storep + (lba * scsi_debug_sector_size),
2761 scsi_debug_sector_size);
2762
9ed8d3dc 2763 if (scsi_debug_lbp())
44d92694
MP
2764 map_region(lba, num);
2765out:
2766 write_unlock_irqrestore(&atomic_rw, iflags);
2767
1da177e4
LT
2768 return 0;
2769}
2770
c2248fc9
DG
2771static int
2772resp_write_same_10(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
2773{
2774 u8 *cmd = scp->cmnd;
2775 u32 lba;
2776 u16 num;
2777 u32 ei_lba = 0;
2778 bool unmap = false;
2779
2780 if (cmd[1] & 0x8) {
2781 if (scsi_debug_lbpws10 == 0) {
2782 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
2783 return check_condition_result;
2784 } else
2785 unmap = true;
2786 }
2787 lba = get_unaligned_be32(cmd + 2);
2788 num = get_unaligned_be16(cmd + 7);
2789 if (num > scsi_debug_write_same_length) {
2790 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
2791 return check_condition_result;
2792 }
2793 return resp_write_same(scp, lba, num, ei_lba, unmap, false);
2794}
2795
2796static int
2797resp_write_same_16(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
2798{
2799 u8 *cmd = scp->cmnd;
2800 u64 lba;
2801 u32 num;
2802 u32 ei_lba = 0;
2803 bool unmap = false;
2804 bool ndob = false;
2805
2806 if (cmd[1] & 0x8) { /* UNMAP */
2807 if (scsi_debug_lbpws == 0) {
2808 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
2809 return check_condition_result;
2810 } else
2811 unmap = true;
2812 }
2813 if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */
2814 ndob = true;
2815 lba = get_unaligned_be64(cmd + 2);
2816 num = get_unaligned_be32(cmd + 10);
2817 if (num > scsi_debug_write_same_length) {
2818 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1);
2819 return check_condition_result;
2820 }
2821 return resp_write_same(scp, lba, num, ei_lba, unmap, ndob);
2822}
2823
44d92694
MP
2824struct unmap_block_desc {
2825 __be64 lba;
2826 __be32 blocks;
2827 __be32 __reserved;
2828};
2829
c2248fc9
DG
2830static int
2831resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
44d92694
MP
2832{
2833 unsigned char *buf;
2834 struct unmap_block_desc *desc;
2835 unsigned int i, payload_len, descriptors;
2836 int ret;
6c78cc06 2837 unsigned long iflags;
44d92694 2838
44d92694 2839
c2248fc9
DG
2840 if (!scsi_debug_lbp())
2841 return 0; /* fib and say its done */
2842 payload_len = get_unaligned_be16(scp->cmnd + 7);
2843 BUG_ON(scsi_bufflen(scp) != payload_len);
44d92694
MP
2844
2845 descriptors = (payload_len - 8) / 16;
c2248fc9
DG
2846 if (descriptors > scsi_debug_unmap_max_desc) {
2847 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
2848 return check_condition_result;
2849 }
44d92694 2850
c2248fc9
DG
2851 buf = kmalloc(scsi_bufflen(scp), GFP_ATOMIC);
2852 if (!buf) {
2853 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
2854 INSUFF_RES_ASCQ);
44d92694 2855 return check_condition_result;
c2248fc9 2856 }
44d92694 2857
c2248fc9 2858 scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
44d92694
MP
2859
2860 BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2);
2861 BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16);
2862
2863 desc = (void *)&buf[8];
2864
6c78cc06
AM
2865 write_lock_irqsave(&atomic_rw, iflags);
2866
44d92694
MP
2867 for (i = 0 ; i < descriptors ; i++) {
2868 unsigned long long lba = get_unaligned_be64(&desc[i].lba);
2869 unsigned int num = get_unaligned_be32(&desc[i].blocks);
2870
c2248fc9 2871 ret = check_device_access_params(scp, lba, num);
44d92694
MP
2872 if (ret)
2873 goto out;
2874
2875 unmap_region(lba, num);
2876 }
2877
2878 ret = 0;
2879
2880out:
6c78cc06 2881 write_unlock_irqrestore(&atomic_rw, iflags);
44d92694
MP
2882 kfree(buf);
2883
2884 return ret;
2885}
2886
2887#define SDEBUG_GET_LBA_STATUS_LEN 32
2888
c2248fc9
DG
2889static int
2890resp_get_lba_status(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
44d92694 2891{
c2248fc9
DG
2892 u8 *cmd = scp->cmnd;
2893 u64 lba;
2894 u32 alloc_len, mapped, num;
2895 u8 arr[SDEBUG_GET_LBA_STATUS_LEN];
44d92694
MP
2896 int ret;
2897
c2248fc9
DG
2898 lba = get_unaligned_be64(cmd + 2);
2899 alloc_len = get_unaligned_be32(cmd + 10);
44d92694
MP
2900
2901 if (alloc_len < 24)
2902 return 0;
2903
c2248fc9 2904 ret = check_device_access_params(scp, lba, 1);
44d92694
MP
2905 if (ret)
2906 return ret;
2907
c2248fc9
DG
2908 if (scsi_debug_lbp())
2909 mapped = map_state(lba, &num);
2910 else {
2911 mapped = 1;
2912 /* following just in case virtual_gb changed */
2913 sdebug_capacity = get_sdebug_capacity();
2914 if (sdebug_capacity - lba <= 0xffffffff)
2915 num = sdebug_capacity - lba;
2916 else
2917 num = 0xffffffff;
2918 }
44d92694
MP
2919
2920 memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN);
c2248fc9
DG
2921 put_unaligned_be32(20, arr); /* Parameter Data Length */
2922 put_unaligned_be64(lba, arr + 8); /* LBA */
2923 put_unaligned_be32(num, arr + 16); /* Number of blocks */
2924 arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */
44d92694 2925
c2248fc9 2926 return fill_from_dev_buffer(scp, arr, SDEBUG_GET_LBA_STATUS_LEN);
44d92694
MP
2927}
2928
c65b1445 2929#define SDEBUG_RLUN_ARR_SZ 256
1da177e4
LT
2930
2931static int resp_report_luns(struct scsi_cmnd * scp,
2932 struct sdebug_dev_info * devip)
2933{
2934 unsigned int alloc_len;
22017ed2
DG
2935 int lun_cnt, i, upper, num, n, want_wlun, shortish;
2936 u64 lun;
01123ef4 2937 unsigned char *cmd = scp->cmnd;
1da177e4
LT
2938 int select_report = (int)cmd[2];
2939 struct scsi_lun *one_lun;
2940 unsigned char arr[SDEBUG_RLUN_ARR_SZ];
c65b1445 2941 unsigned char * max_addr;
1da177e4
LT
2942
2943 alloc_len = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24);
22017ed2
DG
2944 shortish = (alloc_len < 4);
2945 if (shortish || (select_report > 2)) {
2946 mk_sense_invalid_fld(scp, SDEB_IN_CDB, shortish ? 6 : 2, -1);
1da177e4
LT
2947 return check_condition_result;
2948 }
2949 /* can produce response with up to 16k luns (lun 0 to lun 16383) */
2950 memset(arr, 0, SDEBUG_RLUN_ARR_SZ);
2951 lun_cnt = scsi_debug_max_luns;
c65b1445
DG
2952 if (1 == select_report)
2953 lun_cnt = 0;
2954 else if (scsi_debug_no_lun_0 && (lun_cnt > 0))
2955 --lun_cnt;
22017ed2
DG
2956 want_wlun = (select_report > 0) ? 1 : 0;
2957 num = lun_cnt + want_wlun;
c65b1445
DG
2958 arr[2] = ((sizeof(struct scsi_lun) * num) >> 8) & 0xff;
2959 arr[3] = (sizeof(struct scsi_lun) * num) & 0xff;
2960 n = min((int)((SDEBUG_RLUN_ARR_SZ - 8) /
2961 sizeof(struct scsi_lun)), num);
2962 if (n < num) {
22017ed2 2963 want_wlun = 0;
c65b1445
DG
2964 lun_cnt = n;
2965 }
1da177e4 2966 one_lun = (struct scsi_lun *) &arr[8];
c65b1445
DG
2967 max_addr = arr + SDEBUG_RLUN_ARR_SZ;
2968 for (i = 0, lun = (scsi_debug_no_lun_0 ? 1 : 0);
2969 ((i < lun_cnt) && ((unsigned char *)(one_lun + i) < max_addr));
2970 i++, lun++) {
2971 upper = (lun >> 8) & 0x3f;
1da177e4
LT
2972 if (upper)
2973 one_lun[i].scsi_lun[0] =
2974 (upper | (SAM2_LUN_ADDRESS_METHOD << 6));
c65b1445
DG
2975 one_lun[i].scsi_lun[1] = lun & 0xff;
2976 }
22017ed2 2977 if (want_wlun) {
c65b1445
DG
2978 one_lun[i].scsi_lun[0] = (SAM2_WLUN_REPORT_LUNS >> 8) & 0xff;
2979 one_lun[i].scsi_lun[1] = SAM2_WLUN_REPORT_LUNS & 0xff;
2980 i++;
1da177e4 2981 }
c65b1445 2982 alloc_len = (unsigned char *)(one_lun + i) - arr;
1da177e4
LT
2983 return fill_from_dev_buffer(scp, arr,
2984 min((int)alloc_len, SDEBUG_RLUN_ARR_SZ));
2985}
2986
c639d14e
FT
2987static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba,
2988 unsigned int num, struct sdebug_dev_info *devip)
2989{
be4e11be 2990 int j;
c639d14e
FT
2991 unsigned char *kaddr, *buf;
2992 unsigned int offset;
c639d14e 2993 struct scsi_data_buffer *sdb = scsi_in(scp);
be4e11be 2994 struct sg_mapping_iter miter;
c639d14e
FT
2995
2996 /* better not to use temporary buffer. */
2997 buf = kmalloc(scsi_bufflen(scp), GFP_ATOMIC);
c5af0db9 2998 if (!buf) {
22017ed2
DG
2999 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
3000 INSUFF_RES_ASCQ);
c5af0db9
AM
3001 return check_condition_result;
3002 }
c639d14e 3003
21a61829 3004 scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
c639d14e
FT
3005
3006 offset = 0;
be4e11be
AM
3007 sg_miter_start(&miter, sdb->table.sgl, sdb->table.nents,
3008 SG_MITER_ATOMIC | SG_MITER_TO_SG);
c639d14e 3009
be4e11be
AM
3010 while (sg_miter_next(&miter)) {
3011 kaddr = miter.addr;
3012 for (j = 0; j < miter.length; j++)
3013 *(kaddr + j) ^= *(buf + offset + j);
c639d14e 3014
be4e11be 3015 offset += miter.length;
c639d14e 3016 }
be4e11be 3017 sg_miter_stop(&miter);
c639d14e
FT
3018 kfree(buf);
3019
be4e11be 3020 return 0;
c639d14e
FT
3021}
3022
c2248fc9
DG
3023static int
3024resp_xdwriteread_10(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
3025{
3026 u8 *cmd = scp->cmnd;
3027 u64 lba;
3028 u32 num;
3029 int errsts;
3030
3031 if (!scsi_bidi_cmnd(scp)) {
3032 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
3033 INSUFF_RES_ASCQ);
3034 return check_condition_result;
3035 }
3036 errsts = resp_read_dt0(scp, devip);
3037 if (errsts)
3038 return errsts;
3039 if (!(cmd[1] & 0x4)) { /* DISABLE_WRITE is not set */
3040 errsts = resp_write_dt0(scp, devip);
3041 if (errsts)
3042 return errsts;
3043 }
3044 lba = get_unaligned_be32(cmd + 2);
3045 num = get_unaligned_be16(cmd + 7);
3046 return resp_xdwriteread(scp, lba, num, devip);
3047}
3048
cbf67842
DG
3049/* When timer or tasklet goes off this function is called. */
3050static void sdebug_q_cmd_complete(unsigned long indx)
1da177e4 3051{
cbf67842
DG
3052 int qa_indx;
3053 int retiring = 0;
1da177e4 3054 unsigned long iflags;
cbf67842
DG
3055 struct sdebug_queued_cmd *sqcp;
3056 struct scsi_cmnd *scp;
3057 struct sdebug_dev_info *devip;
1da177e4 3058
cbf67842
DG
3059 atomic_inc(&sdebug_completions);
3060 qa_indx = indx;
3061 if ((qa_indx < 0) || (qa_indx >= SCSI_DEBUG_CANQUEUE)) {
3062 pr_err("%s: wild qa_indx=%d\n", __func__, qa_indx);
1da177e4
LT
3063 return;
3064 }
3065 spin_lock_irqsave(&queued_arr_lock, iflags);
cbf67842
DG
3066 sqcp = &queued_arr[qa_indx];
3067 scp = sqcp->a_cmnd;
3068 if (NULL == scp) {
3069 spin_unlock_irqrestore(&queued_arr_lock, iflags);
3070 pr_err("%s: scp is NULL\n", __func__);
3071 return;
3072 }
3073 devip = (struct sdebug_dev_info *)scp->device->hostdata;
3074 if (devip)
3075 atomic_dec(&devip->num_in_q);
3076 else
3077 pr_err("%s: devip=NULL\n", __func__);
3078 if (atomic_read(&retired_max_queue) > 0)
3079 retiring = 1;
3080
3081 sqcp->a_cmnd = NULL;
3082 if (!test_and_clear_bit(qa_indx, queued_in_use_bm)) {
1da177e4 3083 spin_unlock_irqrestore(&queued_arr_lock, iflags);
cbf67842 3084 pr_err("%s: Unexpected completion\n", __func__);
1da177e4
LT
3085 return;
3086 }
cbf67842
DG
3087
3088 if (unlikely(retiring)) { /* user has reduced max_queue */
3089 int k, retval;
3090
3091 retval = atomic_read(&retired_max_queue);
3092 if (qa_indx >= retval) {
3093 spin_unlock_irqrestore(&queued_arr_lock, iflags);
3094 pr_err("%s: index %d too large\n", __func__, retval);
3095 return;
3096 }
3097 k = find_last_bit(queued_in_use_bm, retval);
3098 if ((k < scsi_debug_max_queue) || (k == retval))
3099 atomic_set(&retired_max_queue, 0);
3100 else
3101 atomic_set(&retired_max_queue, k + 1);
1da177e4 3102 }
1da177e4 3103 spin_unlock_irqrestore(&queued_arr_lock, iflags);
cbf67842 3104 scp->scsi_done(scp); /* callback to mid level */
1da177e4
LT
3105}
3106
cbf67842
DG
3107/* When high resolution timer goes off this function is called. */
3108static enum hrtimer_restart
3109sdebug_q_cmd_hrt_complete(struct hrtimer *timer)
3110{
3111 int qa_indx;
3112 int retiring = 0;
3113 unsigned long iflags;
3114 struct sdebug_hrtimer *sd_hrtp = (struct sdebug_hrtimer *)timer;
3115 struct sdebug_queued_cmd *sqcp;
3116 struct scsi_cmnd *scp;
3117 struct sdebug_dev_info *devip;
3118
3119 atomic_inc(&sdebug_completions);
3120 qa_indx = sd_hrtp->qa_indx;
3121 if ((qa_indx < 0) || (qa_indx >= SCSI_DEBUG_CANQUEUE)) {
3122 pr_err("%s: wild qa_indx=%d\n", __func__, qa_indx);
3123 goto the_end;
3124 }
3125 spin_lock_irqsave(&queued_arr_lock, iflags);
3126 sqcp = &queued_arr[qa_indx];
3127 scp = sqcp->a_cmnd;
3128 if (NULL == scp) {
3129 spin_unlock_irqrestore(&queued_arr_lock, iflags);
3130 pr_err("%s: scp is NULL\n", __func__);
3131 goto the_end;
3132 }
3133 devip = (struct sdebug_dev_info *)scp->device->hostdata;
3134 if (devip)
3135 atomic_dec(&devip->num_in_q);
3136 else
3137 pr_err("%s: devip=NULL\n", __func__);
3138 if (atomic_read(&retired_max_queue) > 0)
3139 retiring = 1;
3140
3141 sqcp->a_cmnd = NULL;
3142 if (!test_and_clear_bit(qa_indx, queued_in_use_bm)) {
3143 spin_unlock_irqrestore(&queued_arr_lock, iflags);
3144 pr_err("%s: Unexpected completion\n", __func__);
3145 goto the_end;
3146 }
3147
3148 if (unlikely(retiring)) { /* user has reduced max_queue */
3149 int k, retval;
3150
3151 retval = atomic_read(&retired_max_queue);
3152 if (qa_indx >= retval) {
3153 spin_unlock_irqrestore(&queued_arr_lock, iflags);
3154 pr_err("%s: index %d too large\n", __func__, retval);
3155 goto the_end;
3156 }
3157 k = find_last_bit(queued_in_use_bm, retval);
3158 if ((k < scsi_debug_max_queue) || (k == retval))
3159 atomic_set(&retired_max_queue, 0);
3160 else
3161 atomic_set(&retired_max_queue, k + 1);
3162 }
3163 spin_unlock_irqrestore(&queued_arr_lock, iflags);
3164 scp->scsi_done(scp); /* callback to mid level */
3165the_end:
3166 return HRTIMER_NORESTART;
3167}
1da177e4 3168
8dea0d02
FT
3169static struct sdebug_dev_info *
3170sdebug_device_create(struct sdebug_host_info *sdbg_host, gfp_t flags)
5cb2fc06
FT
3171{
3172 struct sdebug_dev_info *devip;
3173
3174 devip = kzalloc(sizeof(*devip), flags);
3175 if (devip) {
3176 devip->sdbg_host = sdbg_host;
3177 list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list);
3178 }
3179 return devip;
3180}
3181
1da177e4
LT
3182static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev)
3183{
3184 struct sdebug_host_info * sdbg_host;
3185 struct sdebug_dev_info * open_devip = NULL;
3186 struct sdebug_dev_info * devip =
3187 (struct sdebug_dev_info *)sdev->hostdata;
3188
3189 if (devip)
3190 return devip;
d1e4c9c5
FT
3191 sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host);
3192 if (!sdbg_host) {
cbf67842 3193 pr_err("%s: Host info NULL\n", __func__);
1da177e4
LT
3194 return NULL;
3195 }
3196 list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
3197 if ((devip->used) && (devip->channel == sdev->channel) &&
3198 (devip->target == sdev->id) &&
3199 (devip->lun == sdev->lun))
3200 return devip;
3201 else {
3202 if ((!devip->used) && (!open_devip))
3203 open_devip = devip;
3204 }
3205 }
5cb2fc06
FT
3206 if (!open_devip) { /* try and make a new one */
3207 open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC);
3208 if (!open_devip) {
1da177e4 3209 printk(KERN_ERR "%s: out of memory at line %d\n",
cadbd4a5 3210 __func__, __LINE__);
1da177e4
LT
3211 return NULL;
3212 }
1da177e4 3213 }
a75869d1
FT
3214
3215 open_devip->channel = sdev->channel;
3216 open_devip->target = sdev->id;
3217 open_devip->lun = sdev->lun;
3218 open_devip->sdbg_host = sdbg_host;
cbf67842
DG
3219 atomic_set(&open_devip->num_in_q, 0);
3220 set_bit(SDEBUG_UA_POR, open_devip->uas_bm);
c2248fc9 3221 open_devip->used = true;
a75869d1 3222 return open_devip;
1da177e4
LT
3223}
3224
8dea0d02 3225static int scsi_debug_slave_alloc(struct scsi_device *sdp)
1da177e4 3226{
8dea0d02 3227 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
9cb78c16 3228 printk(KERN_INFO "scsi_debug: slave_alloc <%u %u %u %llu>\n",
8dea0d02 3229 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
75ad23bc 3230 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, sdp->request_queue);
8dea0d02
FT
3231 return 0;
3232}
1da177e4 3233
8dea0d02
FT
3234static int scsi_debug_slave_configure(struct scsi_device *sdp)
3235{
3236 struct sdebug_dev_info *devip;
a34c4e98 3237
8dea0d02 3238 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
9cb78c16 3239 printk(KERN_INFO "scsi_debug: slave_configure <%u %u %u %llu>\n",
8dea0d02
FT
3240 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
3241 if (sdp->host->max_cmd_len != SCSI_DEBUG_MAX_CMD_LEN)
3242 sdp->host->max_cmd_len = SCSI_DEBUG_MAX_CMD_LEN;
3243 devip = devInfoReg(sdp);
3244 if (NULL == devip)
3245 return 1; /* no resources, will be marked offline */
c8b09f6f 3246 sdp->hostdata = devip;
6bb5e6e7 3247 blk_queue_max_segment_size(sdp->request_queue, -1U);
78d4e5a0
DG
3248 if (scsi_debug_no_uld)
3249 sdp->no_uld_attach = 1;
8dea0d02
FT
3250 return 0;
3251}
3252
3253static void scsi_debug_slave_destroy(struct scsi_device *sdp)
3254{
3255 struct sdebug_dev_info *devip =
3256 (struct sdebug_dev_info *)sdp->hostdata;
a34c4e98 3257
1da177e4 3258 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
9cb78c16 3259 printk(KERN_INFO "scsi_debug: slave_destroy <%u %u %u %llu>\n",
8dea0d02
FT
3260 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
3261 if (devip) {
25985edc 3262 /* make this slot available for re-use */
c2248fc9 3263 devip->used = false;
8dea0d02
FT
3264 sdp->hostdata = NULL;
3265 }
3266}
3267
cbf67842 3268/* Returns 1 if cmnd found (deletes its timer or tasklet), else returns 0 */
8dea0d02
FT
3269static int stop_queued_cmnd(struct scsi_cmnd *cmnd)
3270{
3271 unsigned long iflags;
cbf67842 3272 int k, qmax, r_qmax;
8dea0d02 3273 struct sdebug_queued_cmd *sqcp;
cbf67842 3274 struct sdebug_dev_info *devip;
8dea0d02
FT
3275
3276 spin_lock_irqsave(&queued_arr_lock, iflags);
cbf67842
DG
3277 qmax = scsi_debug_max_queue;
3278 r_qmax = atomic_read(&retired_max_queue);
3279 if (r_qmax > qmax)
3280 qmax = r_qmax;
3281 for (k = 0; k < qmax; ++k) {
3282 if (test_bit(k, queued_in_use_bm)) {
3283 sqcp = &queued_arr[k];
3284 if (cmnd == sqcp->a_cmnd) {
db525fce
DG
3285 devip = (struct sdebug_dev_info *)
3286 cmnd->device->hostdata;
3287 if (devip)
3288 atomic_dec(&devip->num_in_q);
3289 sqcp->a_cmnd = NULL;
3290 spin_unlock_irqrestore(&queued_arr_lock,
3291 iflags);
cbf67842
DG
3292 if (scsi_debug_ndelay > 0) {
3293 if (sqcp->sd_hrtp)
3294 hrtimer_cancel(
3295 &sqcp->sd_hrtp->hrt);
3296 } else if (scsi_debug_delay > 0) {
3297 if (sqcp->cmnd_timerp)
3298 del_timer_sync(
3299 sqcp->cmnd_timerp);
3300 } else if (scsi_debug_delay < 0) {
3301 if (sqcp->tletp)
3302 tasklet_kill(sqcp->tletp);
3303 }
db525fce
DG
3304 clear_bit(k, queued_in_use_bm);
3305 return 1;
cbf67842 3306 }
8dea0d02
FT
3307 }
3308 }
3309 spin_unlock_irqrestore(&queued_arr_lock, iflags);
db525fce 3310 return 0;
8dea0d02
FT
3311}
3312
cbf67842 3313/* Deletes (stops) timers or tasklets of all queued commands */
8dea0d02
FT
3314static void stop_all_queued(void)
3315{
3316 unsigned long iflags;
3317 int k;
3318 struct sdebug_queued_cmd *sqcp;
cbf67842 3319 struct sdebug_dev_info *devip;
8dea0d02
FT
3320
3321 spin_lock_irqsave(&queued_arr_lock, iflags);
cbf67842
DG
3322 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
3323 if (test_bit(k, queued_in_use_bm)) {
3324 sqcp = &queued_arr[k];
3325 if (sqcp->a_cmnd) {
db525fce
DG
3326 devip = (struct sdebug_dev_info *)
3327 sqcp->a_cmnd->device->hostdata;
3328 if (devip)
3329 atomic_dec(&devip->num_in_q);
3330 sqcp->a_cmnd = NULL;
3331 spin_unlock_irqrestore(&queued_arr_lock,
3332 iflags);
cbf67842
DG
3333 if (scsi_debug_ndelay > 0) {
3334 if (sqcp->sd_hrtp)
3335 hrtimer_cancel(
3336 &sqcp->sd_hrtp->hrt);
3337 } else if (scsi_debug_delay > 0) {
3338 if (sqcp->cmnd_timerp)
3339 del_timer_sync(
3340 sqcp->cmnd_timerp);
3341 } else if (scsi_debug_delay < 0) {
3342 if (sqcp->tletp)
3343 tasklet_kill(sqcp->tletp);
3344 }
db525fce
DG
3345 clear_bit(k, queued_in_use_bm);
3346 spin_lock_irqsave(&queued_arr_lock, iflags);
cbf67842 3347 }
8dea0d02
FT
3348 }
3349 }
3350 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1da177e4
LT
3351}
3352
cbf67842
DG
3353/* Free queued command memory on heap */
3354static void free_all_queued(void)
1da177e4 3355{
cbf67842
DG
3356 unsigned long iflags;
3357 int k;
3358 struct sdebug_queued_cmd *sqcp;
3359
3360 spin_lock_irqsave(&queued_arr_lock, iflags);
3361 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
3362 sqcp = &queued_arr[k];
3363 kfree(sqcp->cmnd_timerp);
3364 sqcp->cmnd_timerp = NULL;
3365 kfree(sqcp->tletp);
3366 sqcp->tletp = NULL;
3367 kfree(sqcp->sd_hrtp);
3368 sqcp->sd_hrtp = NULL;
3369 }
3370 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1da177e4
LT
3371}
3372
cbf67842 3373static int scsi_debug_abort(struct scsi_cmnd *SCpnt)
1da177e4 3374{
cbf67842
DG
3375 ++num_aborts;
3376 if (SCpnt) {
3377 if (SCpnt->device &&
3378 (SCSI_DEBUG_OPT_ALL_NOISE & scsi_debug_opts))
3379 sdev_printk(KERN_INFO, SCpnt->device, "%s\n",
3380 __func__);
3381 stop_queued_cmnd(SCpnt);
3382 }
3383 return SUCCESS;
1da177e4
LT
3384}
3385
3386static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt)
3387{
3388 struct sdebug_dev_info * devip;
3389
1da177e4 3390 ++num_dev_resets;
cbf67842
DG
3391 if (SCpnt && SCpnt->device) {
3392 struct scsi_device *sdp = SCpnt->device;
3393
3394 if (SCSI_DEBUG_OPT_ALL_NOISE & scsi_debug_opts)
3395 sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
3396 devip = devInfoReg(sdp);
1da177e4 3397 if (devip)
cbf67842
DG
3398 set_bit(SDEBUG_UA_POR, devip->uas_bm);
3399 }
3400 return SUCCESS;
3401}
3402
3403static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt)
3404{
3405 struct sdebug_host_info *sdbg_host;
3406 struct sdebug_dev_info *devip;
3407 struct scsi_device *sdp;
3408 struct Scsi_Host *hp;
3409 int k = 0;
3410
3411 ++num_target_resets;
3412 if (!SCpnt)
3413 goto lie;
3414 sdp = SCpnt->device;
3415 if (!sdp)
3416 goto lie;
3417 if (SCSI_DEBUG_OPT_ALL_NOISE & scsi_debug_opts)
3418 sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
3419 hp = sdp->host;
3420 if (!hp)
3421 goto lie;
3422 sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
3423 if (sdbg_host) {
3424 list_for_each_entry(devip,
3425 &sdbg_host->dev_info_list,
3426 dev_list)
3427 if (devip->target == sdp->id) {
3428 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
3429 ++k;
3430 }
1da177e4 3431 }
cbf67842
DG
3432 if (SCSI_DEBUG_OPT_RESET_NOISE & scsi_debug_opts)
3433 sdev_printk(KERN_INFO, sdp,
3434 "%s: %d device(s) found in target\n", __func__, k);
3435lie:
1da177e4
LT
3436 return SUCCESS;
3437}
3438
3439static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt)
3440{
3441 struct sdebug_host_info *sdbg_host;
cbf67842 3442 struct sdebug_dev_info *devip;
1da177e4
LT
3443 struct scsi_device * sdp;
3444 struct Scsi_Host * hp;
cbf67842 3445 int k = 0;
1da177e4 3446
1da177e4 3447 ++num_bus_resets;
cbf67842
DG
3448 if (!(SCpnt && SCpnt->device))
3449 goto lie;
3450 sdp = SCpnt->device;
3451 if (SCSI_DEBUG_OPT_ALL_NOISE & scsi_debug_opts)
3452 sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
3453 hp = sdp->host;
3454 if (hp) {
d1e4c9c5 3455 sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
1da177e4 3456 if (sdbg_host) {
cbf67842 3457 list_for_each_entry(devip,
1da177e4 3458 &sdbg_host->dev_info_list,
cbf67842
DG
3459 dev_list) {
3460 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
3461 ++k;
3462 }
1da177e4
LT
3463 }
3464 }
cbf67842
DG
3465 if (SCSI_DEBUG_OPT_RESET_NOISE & scsi_debug_opts)
3466 sdev_printk(KERN_INFO, sdp,
3467 "%s: %d device(s) found in host\n", __func__, k);
3468lie:
1da177e4
LT
3469 return SUCCESS;
3470}
3471
3472static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt)
3473{
3474 struct sdebug_host_info * sdbg_host;
cbf67842
DG
3475 struct sdebug_dev_info *devip;
3476 int k = 0;
1da177e4 3477
1da177e4 3478 ++num_host_resets;
cbf67842
DG
3479 if ((SCpnt->device) && (SCSI_DEBUG_OPT_ALL_NOISE & scsi_debug_opts))
3480 sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__);
1da177e4
LT
3481 spin_lock(&sdebug_host_list_lock);
3482 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
cbf67842
DG
3483 list_for_each_entry(devip, &sdbg_host->dev_info_list,
3484 dev_list) {
3485 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
3486 ++k;
3487 }
1da177e4
LT
3488 }
3489 spin_unlock(&sdebug_host_list_lock);
3490 stop_all_queued();
cbf67842
DG
3491 if (SCSI_DEBUG_OPT_RESET_NOISE & scsi_debug_opts)
3492 sdev_printk(KERN_INFO, SCpnt->device,
3493 "%s: %d device(s) found\n", __func__, k);
1da177e4
LT
3494 return SUCCESS;
3495}
3496
f58b0efb 3497static void __init sdebug_build_parts(unsigned char *ramp,
5f2578e5 3498 unsigned long store_size)
1da177e4
LT
3499{
3500 struct partition * pp;
3501 int starts[SDEBUG_MAX_PARTS + 2];
3502 int sectors_per_part, num_sectors, k;
3503 int heads_by_sects, start_sec, end_sec;
3504
3505 /* assume partition table already zeroed */
f58b0efb 3506 if ((scsi_debug_num_parts < 1) || (store_size < 1048576))
1da177e4
LT
3507 return;
3508 if (scsi_debug_num_parts > SDEBUG_MAX_PARTS) {
3509 scsi_debug_num_parts = SDEBUG_MAX_PARTS;
cbf67842
DG
3510 pr_warn("%s: reducing partitions to %d\n", __func__,
3511 SDEBUG_MAX_PARTS);
1da177e4 3512 }
c65b1445 3513 num_sectors = (int)sdebug_store_sectors;
1da177e4
LT
3514 sectors_per_part = (num_sectors - sdebug_sectors_per)
3515 / scsi_debug_num_parts;
3516 heads_by_sects = sdebug_heads * sdebug_sectors_per;
3517 starts[0] = sdebug_sectors_per;
3518 for (k = 1; k < scsi_debug_num_parts; ++k)
3519 starts[k] = ((k * sectors_per_part) / heads_by_sects)
3520 * heads_by_sects;
3521 starts[scsi_debug_num_parts] = num_sectors;
3522 starts[scsi_debug_num_parts + 1] = 0;
3523
3524 ramp[510] = 0x55; /* magic partition markings */
3525 ramp[511] = 0xAA;
3526 pp = (struct partition *)(ramp + 0x1be);
3527 for (k = 0; starts[k + 1]; ++k, ++pp) {
3528 start_sec = starts[k];
3529 end_sec = starts[k + 1] - 1;
3530 pp->boot_ind = 0;
3531
3532 pp->cyl = start_sec / heads_by_sects;
3533 pp->head = (start_sec - (pp->cyl * heads_by_sects))
3534 / sdebug_sectors_per;
3535 pp->sector = (start_sec % sdebug_sectors_per) + 1;
3536
3537 pp->end_cyl = end_sec / heads_by_sects;
3538 pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
3539 / sdebug_sectors_per;
3540 pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
3541
150c3544
AM
3542 pp->start_sect = cpu_to_le32(start_sec);
3543 pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1);
1da177e4
LT
3544 pp->sys_ind = 0x83; /* plain Linux partition */
3545 }
3546}
3547
cbf67842
DG
3548static int
3549schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip,
3550 int scsi_result, int delta_jiff)
1da177e4 3551{
cbf67842 3552 unsigned long iflags;
cd62b7da 3553 int k, num_in_q, qdepth, inject;
cbf67842
DG
3554 struct sdebug_queued_cmd *sqcp = NULL;
3555 struct scsi_device *sdp = cmnd->device;
3556
3557 if (NULL == cmnd || NULL == devip) {
3558 pr_warn("%s: called with NULL cmnd or devip pointer\n",
3559 __func__);
3560 /* no particularly good error to report back */
3561 return SCSI_MLQUEUE_HOST_BUSY;
3562 }
3563 if ((scsi_result) && (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
3564 sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n",
3565 __func__, scsi_result);
cd62b7da
DG
3566 if (delta_jiff == 0)
3567 goto respond_in_thread;
1da177e4 3568
cd62b7da 3569 /* schedule the response at a later time if resources permit */
cbf67842
DG
3570 spin_lock_irqsave(&queued_arr_lock, iflags);
3571 num_in_q = atomic_read(&devip->num_in_q);
3572 qdepth = cmnd->device->queue_depth;
cbf67842 3573 inject = 0;
cd62b7da
DG
3574 if ((qdepth > 0) && (num_in_q >= qdepth)) {
3575 if (scsi_result) {
3576 spin_unlock_irqrestore(&queued_arr_lock, iflags);
3577 goto respond_in_thread;
3578 } else
3579 scsi_result = device_qfull_result;
3580 } else if ((scsi_debug_every_nth != 0) &&
3581 (SCSI_DEBUG_OPT_RARE_TSF & scsi_debug_opts) &&
3582 (scsi_result == 0)) {
cbf67842
DG
3583 if ((num_in_q == (qdepth - 1)) &&
3584 (atomic_inc_return(&sdebug_a_tsf) >=
3585 abs(scsi_debug_every_nth))) {
3586 atomic_set(&sdebug_a_tsf, 0);
3587 inject = 1;
cd62b7da 3588 scsi_result = device_qfull_result;
1da177e4
LT
3589 }
3590 }
1da177e4 3591
cd62b7da 3592 k = find_first_zero_bit(queued_in_use_bm, scsi_debug_max_queue);
cbf67842 3593 if (k >= scsi_debug_max_queue) {
cbf67842 3594 spin_unlock_irqrestore(&queued_arr_lock, iflags);
cd62b7da
DG
3595 if (scsi_result)
3596 goto respond_in_thread;
3597 else if (SCSI_DEBUG_OPT_ALL_TSF & scsi_debug_opts)
3598 scsi_result = device_qfull_result;
cbf67842
DG
3599 if (SCSI_DEBUG_OPT_Q_NOISE & scsi_debug_opts)
3600 sdev_printk(KERN_INFO, sdp,
cd62b7da
DG
3601 "%s: max_queue=%d exceeded, %s\n",
3602 __func__, scsi_debug_max_queue,
3603 (scsi_result ? "status: TASK SET FULL" :
3604 "report: host busy"));
3605 if (scsi_result)
3606 goto respond_in_thread;
3607 else
cbf67842
DG
3608 return SCSI_MLQUEUE_HOST_BUSY;
3609 }
3610 __set_bit(k, queued_in_use_bm);
3611 atomic_inc(&devip->num_in_q);
3612 sqcp = &queued_arr[k];
3613 sqcp->a_cmnd = cmnd;
3614 cmnd->result = scsi_result;
3615 spin_unlock_irqrestore(&queued_arr_lock, iflags);
3616 if (delta_jiff > 0) {
3617 if (NULL == sqcp->cmnd_timerp) {
3618 sqcp->cmnd_timerp = kmalloc(sizeof(struct timer_list),
3619 GFP_ATOMIC);
3620 if (NULL == sqcp->cmnd_timerp)
3621 return SCSI_MLQUEUE_HOST_BUSY;
3622 init_timer(sqcp->cmnd_timerp);
1da177e4 3623 }
cbf67842
DG
3624 sqcp->cmnd_timerp->function = sdebug_q_cmd_complete;
3625 sqcp->cmnd_timerp->data = k;
3626 sqcp->cmnd_timerp->expires = get_jiffies_64() + delta_jiff;
3627 add_timer(sqcp->cmnd_timerp);
3628 } else if (scsi_debug_ndelay > 0) {
3629 ktime_t kt = ktime_set(0, scsi_debug_ndelay);
3630 struct sdebug_hrtimer *sd_hp = sqcp->sd_hrtp;
3631
3632 if (NULL == sd_hp) {
3633 sd_hp = kmalloc(sizeof(*sd_hp), GFP_ATOMIC);
3634 if (NULL == sd_hp)
3635 return SCSI_MLQUEUE_HOST_BUSY;
3636 sqcp->sd_hrtp = sd_hp;
3637 hrtimer_init(&sd_hp->hrt, CLOCK_MONOTONIC,
3638 HRTIMER_MODE_REL);
3639 sd_hp->hrt.function = sdebug_q_cmd_hrt_complete;
3640 sd_hp->qa_indx = k;
1da177e4 3641 }
cbf67842
DG
3642 hrtimer_start(&sd_hp->hrt, kt, HRTIMER_MODE_REL);
3643 } else { /* delay < 0 */
3644 if (NULL == sqcp->tletp) {
3645 sqcp->tletp = kmalloc(sizeof(*sqcp->tletp),
3646 GFP_ATOMIC);
3647 if (NULL == sqcp->tletp)
3648 return SCSI_MLQUEUE_HOST_BUSY;
3649 tasklet_init(sqcp->tletp,
3650 sdebug_q_cmd_complete, k);
3651 }
3652 if (-1 == delta_jiff)
3653 tasklet_hi_schedule(sqcp->tletp);
3654 else
3655 tasklet_schedule(sqcp->tletp);
1da177e4 3656 }
cd62b7da
DG
3657 if ((SCSI_DEBUG_OPT_Q_NOISE & scsi_debug_opts) &&
3658 (scsi_result == device_qfull_result))
cbf67842
DG
3659 sdev_printk(KERN_INFO, sdp,
3660 "%s: num_in_q=%d +1, %s%s\n", __func__,
3661 num_in_q, (inject ? "<inject> " : ""),
3662 "status: TASK SET FULL");
3663 return 0;
cd62b7da
DG
3664
3665respond_in_thread: /* call back to mid-layer using invocation thread */
3666 cmnd->result = scsi_result;
3667 cmnd->scsi_done(cmnd);
3668 return 0;
1da177e4 3669}
cbf67842 3670
23183910
DG
3671/* Note: The following macros create attribute files in the
3672 /sys/module/scsi_debug/parameters directory. Unfortunately this
3673 driver is unaware of a change and cannot trigger auxiliary actions
3674 as it can when the corresponding attribute in the
3675 /sys/bus/pseudo/drivers/scsi_debug directory is changed.
3676 */
c65b1445 3677module_param_named(add_host, scsi_debug_add_host, int, S_IRUGO | S_IWUSR);
5b94e232 3678module_param_named(ato, scsi_debug_ato, int, S_IRUGO);
0759c666 3679module_param_named(clustering, scsi_debug_clustering, bool, S_IRUGO | S_IWUSR);
c65b1445
DG
3680module_param_named(delay, scsi_debug_delay, int, S_IRUGO | S_IWUSR);
3681module_param_named(dev_size_mb, scsi_debug_dev_size_mb, int, S_IRUGO);
5b94e232
MP
3682module_param_named(dif, scsi_debug_dif, int, S_IRUGO);
3683module_param_named(dix, scsi_debug_dix, int, S_IRUGO);
c65b1445
DG
3684module_param_named(dsense, scsi_debug_dsense, int, S_IRUGO | S_IWUSR);
3685module_param_named(every_nth, scsi_debug_every_nth, int, S_IRUGO | S_IWUSR);
23183910 3686module_param_named(fake_rw, scsi_debug_fake_rw, int, S_IRUGO | S_IWUSR);
68aee7ba 3687module_param_named(guard, scsi_debug_guard, uint, S_IRUGO);
cbf67842 3688module_param_named(host_lock, scsi_debug_host_lock, bool, S_IRUGO | S_IWUSR);
5b94e232
MP
3689module_param_named(lbpu, scsi_debug_lbpu, int, S_IRUGO);
3690module_param_named(lbpws, scsi_debug_lbpws, int, S_IRUGO);
3691module_param_named(lbpws10, scsi_debug_lbpws10, int, S_IRUGO);
be1dd78d 3692module_param_named(lbprz, scsi_debug_lbprz, int, S_IRUGO);
5b94e232 3693module_param_named(lowest_aligned, scsi_debug_lowest_aligned, int, S_IRUGO);
c65b1445 3694module_param_named(max_luns, scsi_debug_max_luns, int, S_IRUGO | S_IWUSR);
78d4e5a0 3695module_param_named(max_queue, scsi_debug_max_queue, int, S_IRUGO | S_IWUSR);
cbf67842 3696module_param_named(ndelay, scsi_debug_ndelay, int, S_IRUGO | S_IWUSR);
c65b1445 3697module_param_named(no_lun_0, scsi_debug_no_lun_0, int, S_IRUGO | S_IWUSR);
78d4e5a0 3698module_param_named(no_uld, scsi_debug_no_uld, int, S_IRUGO);
c65b1445
DG
3699module_param_named(num_parts, scsi_debug_num_parts, int, S_IRUGO);
3700module_param_named(num_tgts, scsi_debug_num_tgts, int, S_IRUGO | S_IWUSR);
5b94e232 3701module_param_named(opt_blks, scsi_debug_opt_blks, int, S_IRUGO);
c65b1445 3702module_param_named(opts, scsi_debug_opts, int, S_IRUGO | S_IWUSR);
5b94e232 3703module_param_named(physblk_exp, scsi_debug_physblk_exp, int, S_IRUGO);
c65b1445 3704module_param_named(ptype, scsi_debug_ptype, int, S_IRUGO | S_IWUSR);
d986788b 3705module_param_named(removable, scsi_debug_removable, bool, S_IRUGO | S_IWUSR);
c65b1445 3706module_param_named(scsi_level, scsi_debug_scsi_level, int, S_IRUGO);
597136ab 3707module_param_named(sector_size, scsi_debug_sector_size, int, S_IRUGO);
c2248fc9 3708module_param_named(strict, scsi_debug_strict, bool, S_IRUGO | S_IWUSR);
5b94e232
MP
3709module_param_named(unmap_alignment, scsi_debug_unmap_alignment, int, S_IRUGO);
3710module_param_named(unmap_granularity, scsi_debug_unmap_granularity, int, S_IRUGO);
44d92694
MP
3711module_param_named(unmap_max_blocks, scsi_debug_unmap_max_blocks, int, S_IRUGO);
3712module_param_named(unmap_max_desc, scsi_debug_unmap_max_desc, int, S_IRUGO);
5b94e232
MP
3713module_param_named(virtual_gb, scsi_debug_virtual_gb, int, S_IRUGO | S_IWUSR);
3714module_param_named(vpd_use_hostno, scsi_debug_vpd_use_hostno, int,
3715 S_IRUGO | S_IWUSR);
3716module_param_named(write_same_length, scsi_debug_write_same_length, int,
3717 S_IRUGO | S_IWUSR);
1da177e4
LT
3718
3719MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
3720MODULE_DESCRIPTION("SCSI debug adapter driver");
3721MODULE_LICENSE("GPL");
3722MODULE_VERSION(SCSI_DEBUG_VERSION);
3723
3724MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
5b94e232 3725MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)");
0759c666 3726MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)");
cbf67842 3727MODULE_PARM_DESC(delay, "response delay (def=1 jiffy); 0:imm, -1,-2:tiny");
c2248fc9 3728MODULE_PARM_DESC(dev_size_mb, "size in MiB of ram shared by devs(def=8)");
5b94e232
MP
3729MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)");
3730MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)");
c65b1445 3731MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
beb87c33 3732MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)");
23183910 3733MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
5b94e232 3734MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)");
cbf67842 3735MODULE_PARM_DESC(host_lock, "use host_lock around all commands (def=0)");
5b94e232
MP
3736MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)");
3737MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)");
3738MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)");
be1dd78d 3739MODULE_PARM_DESC(lbprz, "unmapped blocks return 0 on read (def=1)");
5b94e232 3740MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)");
c65b1445 3741MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
cbf67842
DG
3742MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))");
3743MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)");
c65b1445 3744MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
78d4e5a0 3745MODULE_PARM_DESC(no_uld, "stop ULD (e.g. sd driver) attaching (def=0))");
1da177e4 3746MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
c65b1445 3747MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
5b94e232 3748MODULE_PARM_DESC(opt_blks, "optimal transfer length in block (def=64)");
6f3cbf55 3749MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)");
5b94e232 3750MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)");
1da177e4 3751MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
d986788b 3752MODULE_PARM_DESC(removable, "claim to have removable media (def=0)");
e46b0344 3753MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=6[SPC-4])");
ea61fca5 3754MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)");
c2248fc9 3755MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)");
5b94e232
MP
3756MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)");
3757MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)");
6014759c
MP
3758MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)");
3759MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)");
c2248fc9 3760MODULE_PARM_DESC(virtual_gb, "virtual gigabyte (GiB) size (def=0 -> use dev_size_mb)");
5b94e232
MP
3761MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
3762MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)");
1da177e4
LT
3763
3764static char sdebug_info[256];
3765
3766static const char * scsi_debug_info(struct Scsi_Host * shp)
3767{
3768 sprintf(sdebug_info, "scsi_debug, version %s [%s], "
3769 "dev_size_mb=%d, opts=0x%x", SCSI_DEBUG_VERSION,
3770 scsi_debug_version_date, scsi_debug_dev_size_mb,
3771 scsi_debug_opts);
3772 return sdebug_info;
3773}
3774
cbf67842 3775/* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */
c8ed555a 3776static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer, int length)
1da177e4 3777{
c8ed555a
AV
3778 char arr[16];
3779 int opts;
3780 int minLen = length > 15 ? 15 : length;
1da177e4 3781
c8ed555a
AV
3782 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3783 return -EACCES;
3784 memcpy(arr, buffer, minLen);
3785 arr[minLen] = '\0';
3786 if (1 != sscanf(arr, "%d", &opts))
3787 return -EINVAL;
3788 scsi_debug_opts = opts;
3789 if (scsi_debug_every_nth != 0)
cbf67842 3790 atomic_set(&sdebug_cmnd_count, 0);
c8ed555a
AV
3791 return length;
3792}
1da177e4 3793
cbf67842
DG
3794/* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the
3795 * same for each scsi_debug host (if more than one). Some of the counters
3796 * output are not atomics so might be inaccurate in a busy system. */
c8ed555a
AV
3797static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host)
3798{
cbf67842
DG
3799 int f, l;
3800 char b[32];
3801
3802 if (scsi_debug_every_nth > 0)
3803 snprintf(b, sizeof(b), " (curr:%d)",
3804 ((SCSI_DEBUG_OPT_RARE_TSF & scsi_debug_opts) ?
3805 atomic_read(&sdebug_a_tsf) :
3806 atomic_read(&sdebug_cmnd_count)));
3807 else
3808 b[0] = '\0';
3809
3810 seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n"
3811 "num_tgts=%d, shared (ram) size=%d MB, opts=0x%x, "
3812 "every_nth=%d%s\n"
3813 "delay=%d, ndelay=%d, max_luns=%d, q_completions=%d\n"
3814 "sector_size=%d bytes, cylinders=%d, heads=%d, sectors=%d\n"
3815 "command aborts=%d; RESETs: device=%d, target=%d, bus=%d, "
3816 "host=%d\ndix_reads=%d dix_writes=%d dif_errors=%d "
3817 "usec_in_jiffy=%lu\n",
3818 SCSI_DEBUG_VERSION, scsi_debug_version_date,
3819 scsi_debug_num_tgts, scsi_debug_dev_size_mb, scsi_debug_opts,
3820 scsi_debug_every_nth, b, scsi_debug_delay, scsi_debug_ndelay,
3821 scsi_debug_max_luns, atomic_read(&sdebug_completions),
3822 scsi_debug_sector_size, sdebug_cylinders_per, sdebug_heads,
3823 sdebug_sectors_per, num_aborts, num_dev_resets,
3824 num_target_resets, num_bus_resets, num_host_resets,
3825 dix_reads, dix_writes, dif_errors, TICK_NSEC / 1000);
3826
3827 f = find_first_bit(queued_in_use_bm, scsi_debug_max_queue);
3828 if (f != scsi_debug_max_queue) {
3829 l = find_last_bit(queued_in_use_bm, scsi_debug_max_queue);
3830 seq_printf(m, " %s BUSY: first,last bits set: %d,%d\n",
3831 "queued_in_use_bm", f, l);
3832 }
c8ed555a 3833 return 0;
1da177e4
LT
3834}
3835
82069379 3836static ssize_t delay_show(struct device_driver *ddp, char *buf)
1da177e4
LT
3837{
3838 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_delay);
3839}
cbf67842 3840/* Returns -EBUSY if delay is being changed and commands are queued */
82069379
AM
3841static ssize_t delay_store(struct device_driver *ddp, const char *buf,
3842 size_t count)
1da177e4 3843{
cbf67842
DG
3844 int delay, res;
3845
3846 if ((count > 0) && (1 == sscanf(buf, "%d", &delay))) {
3847 res = count;
3848 if (scsi_debug_delay != delay) {
3849 unsigned long iflags;
3850 int k;
3851
3852 spin_lock_irqsave(&queued_arr_lock, iflags);
3853 k = find_first_bit(queued_in_use_bm,
3854 scsi_debug_max_queue);
3855 if (k != scsi_debug_max_queue)
3856 res = -EBUSY; /* have queued commands */
3857 else {
3858 scsi_debug_delay = delay;
3859 scsi_debug_ndelay = 0;
3860 }
3861 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1da177e4 3862 }
cbf67842 3863 return res;
1da177e4
LT
3864 }
3865 return -EINVAL;
3866}
82069379 3867static DRIVER_ATTR_RW(delay);
1da177e4 3868
cbf67842
DG
3869static ssize_t ndelay_show(struct device_driver *ddp, char *buf)
3870{
3871 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ndelay);
3872}
3873/* Returns -EBUSY if ndelay is being changed and commands are queued */
3874/* If > 0 and accepted then scsi_debug_delay is set to DELAY_OVERRIDDEN */
3875static ssize_t ndelay_store(struct device_driver *ddp, const char *buf,
3876 size_t count)
3877{
3878 unsigned long iflags;
3879 int ndelay, res, k;
3880
3881 if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) &&
3882 (ndelay >= 0) && (ndelay < 1000000000)) {
3883 res = count;
3884 if (scsi_debug_ndelay != ndelay) {
3885 spin_lock_irqsave(&queued_arr_lock, iflags);
3886 k = find_first_bit(queued_in_use_bm,
3887 scsi_debug_max_queue);
3888 if (k != scsi_debug_max_queue)
3889 res = -EBUSY; /* have queued commands */
3890 else {
3891 scsi_debug_ndelay = ndelay;
3892 scsi_debug_delay = ndelay ? DELAY_OVERRIDDEN
3893 : DEF_DELAY;
3894 }
3895 spin_unlock_irqrestore(&queued_arr_lock, iflags);
3896 }
3897 return res;
3898 }
3899 return -EINVAL;
3900}
3901static DRIVER_ATTR_RW(ndelay);
3902
82069379 3903static ssize_t opts_show(struct device_driver *ddp, char *buf)
1da177e4
LT
3904{
3905 return scnprintf(buf, PAGE_SIZE, "0x%x\n", scsi_debug_opts);
3906}
3907
82069379
AM
3908static ssize_t opts_store(struct device_driver *ddp, const char *buf,
3909 size_t count)
1da177e4
LT
3910{
3911 int opts;
3912 char work[20];
3913
3914 if (1 == sscanf(buf, "%10s", work)) {
48a96876 3915 if (0 == strncasecmp(work,"0x", 2)) {
1da177e4
LT
3916 if (1 == sscanf(&work[2], "%x", &opts))
3917 goto opts_done;
3918 } else {
3919 if (1 == sscanf(work, "%d", &opts))
3920 goto opts_done;
3921 }
3922 }
3923 return -EINVAL;
3924opts_done:
3925 scsi_debug_opts = opts;
817fd66b
DG
3926 if (SCSI_DEBUG_OPT_RECOVERED_ERR & opts)
3927 sdebug_any_injecting_opt = true;
3928 else if (SCSI_DEBUG_OPT_TRANSPORT_ERR & opts)
3929 sdebug_any_injecting_opt = true;
3930 else if (SCSI_DEBUG_OPT_DIF_ERR & opts)
3931 sdebug_any_injecting_opt = true;
3932 else if (SCSI_DEBUG_OPT_DIX_ERR & opts)
3933 sdebug_any_injecting_opt = true;
3934 else if (SCSI_DEBUG_OPT_SHORT_TRANSFER & opts)
3935 sdebug_any_injecting_opt = true;
cbf67842
DG
3936 atomic_set(&sdebug_cmnd_count, 0);
3937 atomic_set(&sdebug_a_tsf, 0);
1da177e4
LT
3938 return count;
3939}
82069379 3940static DRIVER_ATTR_RW(opts);
1da177e4 3941
82069379 3942static ssize_t ptype_show(struct device_driver *ddp, char *buf)
1da177e4
LT
3943{
3944 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ptype);
3945}
82069379
AM
3946static ssize_t ptype_store(struct device_driver *ddp, const char *buf,
3947 size_t count)
1da177e4
LT
3948{
3949 int n;
3950
3951 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
3952 scsi_debug_ptype = n;
3953 return count;
3954 }
3955 return -EINVAL;
3956}
82069379 3957static DRIVER_ATTR_RW(ptype);
1da177e4 3958
82069379 3959static ssize_t dsense_show(struct device_driver *ddp, char *buf)
1da177e4
LT
3960{
3961 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dsense);
3962}
82069379
AM
3963static ssize_t dsense_store(struct device_driver *ddp, const char *buf,
3964 size_t count)
1da177e4
LT
3965{
3966 int n;
3967
3968 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
3969 scsi_debug_dsense = n;
3970 return count;
3971 }
3972 return -EINVAL;
3973}
82069379 3974static DRIVER_ATTR_RW(dsense);
1da177e4 3975
82069379 3976static ssize_t fake_rw_show(struct device_driver *ddp, char *buf)
23183910
DG
3977{
3978 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_fake_rw);
3979}
82069379
AM
3980static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf,
3981 size_t count)
23183910
DG
3982{
3983 int n;
3984
3985 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
cbf67842
DG
3986 n = (n > 0);
3987 scsi_debug_fake_rw = (scsi_debug_fake_rw > 0);
3988 if (scsi_debug_fake_rw != n) {
3989 if ((0 == n) && (NULL == fake_storep)) {
3990 unsigned long sz =
3991 (unsigned long)scsi_debug_dev_size_mb *
3992 1048576;
3993
3994 fake_storep = vmalloc(sz);
3995 if (NULL == fake_storep) {
3996 pr_err("%s: out of memory, 9\n",
3997 __func__);
3998 return -ENOMEM;
3999 }
4000 memset(fake_storep, 0, sz);
4001 }
4002 scsi_debug_fake_rw = n;
4003 }
23183910
DG
4004 return count;
4005 }
4006 return -EINVAL;
4007}
82069379 4008static DRIVER_ATTR_RW(fake_rw);
23183910 4009
82069379 4010static ssize_t no_lun_0_show(struct device_driver *ddp, char *buf)
c65b1445
DG
4011{
4012 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_no_lun_0);
4013}
82069379
AM
4014static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf,
4015 size_t count)
c65b1445
DG
4016{
4017 int n;
4018
4019 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
4020 scsi_debug_no_lun_0 = n;
4021 return count;
4022 }
4023 return -EINVAL;
4024}
82069379 4025static DRIVER_ATTR_RW(no_lun_0);
c65b1445 4026
82069379 4027static ssize_t num_tgts_show(struct device_driver *ddp, char *buf)
1da177e4
LT
4028{
4029 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_tgts);
4030}
82069379
AM
4031static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf,
4032 size_t count)
1da177e4
LT
4033{
4034 int n;
4035
4036 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
4037 scsi_debug_num_tgts = n;
4038 sdebug_max_tgts_luns();
4039 return count;
4040 }
4041 return -EINVAL;
4042}
82069379 4043static DRIVER_ATTR_RW(num_tgts);
1da177e4 4044
82069379 4045static ssize_t dev_size_mb_show(struct device_driver *ddp, char *buf)
1da177e4
LT
4046{
4047 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dev_size_mb);
4048}
82069379 4049static DRIVER_ATTR_RO(dev_size_mb);
1da177e4 4050
82069379 4051static ssize_t num_parts_show(struct device_driver *ddp, char *buf)
1da177e4
LT
4052{
4053 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_parts);
4054}
82069379 4055static DRIVER_ATTR_RO(num_parts);
1da177e4 4056
82069379 4057static ssize_t every_nth_show(struct device_driver *ddp, char *buf)
1da177e4
LT
4058{
4059 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_every_nth);
4060}
82069379
AM
4061static ssize_t every_nth_store(struct device_driver *ddp, const char *buf,
4062 size_t count)
1da177e4
LT
4063{
4064 int nth;
4065
4066 if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) {
4067 scsi_debug_every_nth = nth;
cbf67842 4068 atomic_set(&sdebug_cmnd_count, 0);
1da177e4
LT
4069 return count;
4070 }
4071 return -EINVAL;
4072}
82069379 4073static DRIVER_ATTR_RW(every_nth);
1da177e4 4074
82069379 4075static ssize_t max_luns_show(struct device_driver *ddp, char *buf)
1da177e4
LT
4076{
4077 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_max_luns);
4078}
82069379
AM
4079static ssize_t max_luns_store(struct device_driver *ddp, const char *buf,
4080 size_t count)
1da177e4
LT
4081{
4082 int n;
4083
4084 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
4085 scsi_debug_max_luns = n;
4086 sdebug_max_tgts_luns();
4087 return count;
4088 }
4089 return -EINVAL;
4090}
82069379 4091static DRIVER_ATTR_RW(max_luns);
1da177e4 4092
82069379 4093static ssize_t max_queue_show(struct device_driver *ddp, char *buf)
78d4e5a0
DG
4094{
4095 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_max_queue);
4096}
cbf67842
DG
4097/* N.B. max_queue can be changed while there are queued commands. In flight
4098 * commands beyond the new max_queue will be completed. */
82069379
AM
4099static ssize_t max_queue_store(struct device_driver *ddp, const char *buf,
4100 size_t count)
78d4e5a0 4101{
cbf67842
DG
4102 unsigned long iflags;
4103 int n, k;
78d4e5a0
DG
4104
4105 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) &&
4106 (n <= SCSI_DEBUG_CANQUEUE)) {
cbf67842
DG
4107 spin_lock_irqsave(&queued_arr_lock, iflags);
4108 k = find_last_bit(queued_in_use_bm, SCSI_DEBUG_CANQUEUE);
78d4e5a0 4109 scsi_debug_max_queue = n;
cbf67842
DG
4110 if (SCSI_DEBUG_CANQUEUE == k)
4111 atomic_set(&retired_max_queue, 0);
4112 else if (k >= n)
4113 atomic_set(&retired_max_queue, k + 1);
4114 else
4115 atomic_set(&retired_max_queue, 0);
4116 spin_unlock_irqrestore(&queued_arr_lock, iflags);
78d4e5a0
DG
4117 return count;
4118 }
4119 return -EINVAL;
4120}
82069379 4121static DRIVER_ATTR_RW(max_queue);
78d4e5a0 4122
82069379 4123static ssize_t no_uld_show(struct device_driver *ddp, char *buf)
78d4e5a0
DG
4124{
4125 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_no_uld);
4126}
82069379 4127static DRIVER_ATTR_RO(no_uld);
78d4e5a0 4128
82069379 4129static ssize_t scsi_level_show(struct device_driver *ddp, char *buf)
1da177e4
LT
4130{
4131 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_scsi_level);
4132}
82069379 4133static DRIVER_ATTR_RO(scsi_level);
1da177e4 4134
82069379 4135static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf)
c65b1445
DG
4136{
4137 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_virtual_gb);
4138}
82069379
AM
4139static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf,
4140 size_t count)
c65b1445 4141{
c2248fc9 4142 int n;
0d01c5df 4143 bool changed;
c65b1445
DG
4144
4145 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
0d01c5df 4146 changed = (scsi_debug_virtual_gb != n);
c65b1445 4147 scsi_debug_virtual_gb = n;
28898873 4148 sdebug_capacity = get_sdebug_capacity();
0d01c5df
DG
4149 if (changed) {
4150 struct sdebug_host_info *sdhp;
4151 struct sdebug_dev_info *dp;
4152
4153 list_for_each_entry(sdhp, &sdebug_host_list,
4154 host_list) {
4155 list_for_each_entry(dp, &sdhp->dev_info_list,
4156 dev_list) {
4157 set_bit(SDEBUG_UA_CAPACITY_CHANGED,
4158 dp->uas_bm);
4159 }
4160 }
4161 }
c65b1445
DG
4162 return count;
4163 }
4164 return -EINVAL;
4165}
82069379 4166static DRIVER_ATTR_RW(virtual_gb);
c65b1445 4167
82069379 4168static ssize_t add_host_show(struct device_driver *ddp, char *buf)
1da177e4
LT
4169{
4170 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_add_host);
4171}
4172
82069379
AM
4173static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
4174 size_t count)
1da177e4 4175{
f3df41cf 4176 int delta_hosts;
1da177e4 4177
f3df41cf 4178 if (sscanf(buf, "%d", &delta_hosts) != 1)
1da177e4 4179 return -EINVAL;
1da177e4
LT
4180 if (delta_hosts > 0) {
4181 do {
4182 sdebug_add_adapter();
4183 } while (--delta_hosts);
4184 } else if (delta_hosts < 0) {
4185 do {
4186 sdebug_remove_adapter();
4187 } while (++delta_hosts);
4188 }
4189 return count;
4190}
82069379 4191static DRIVER_ATTR_RW(add_host);
1da177e4 4192
82069379 4193static ssize_t vpd_use_hostno_show(struct device_driver *ddp, char *buf)
23183910
DG
4194{
4195 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_vpd_use_hostno);
4196}
82069379
AM
4197static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf,
4198 size_t count)
23183910
DG
4199{
4200 int n;
4201
4202 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
4203 scsi_debug_vpd_use_hostno = n;
4204 return count;
4205 }
4206 return -EINVAL;
4207}
82069379 4208static DRIVER_ATTR_RW(vpd_use_hostno);
23183910 4209
82069379 4210static ssize_t sector_size_show(struct device_driver *ddp, char *buf)
597136ab
MP
4211{
4212 return scnprintf(buf, PAGE_SIZE, "%u\n", scsi_debug_sector_size);
4213}
82069379 4214static DRIVER_ATTR_RO(sector_size);
597136ab 4215
82069379 4216static ssize_t dix_show(struct device_driver *ddp, char *buf)
c6a44287
MP
4217{
4218 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dix);
4219}
82069379 4220static DRIVER_ATTR_RO(dix);
c6a44287 4221
82069379 4222static ssize_t dif_show(struct device_driver *ddp, char *buf)
c6a44287
MP
4223{
4224 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dif);
4225}
82069379 4226static DRIVER_ATTR_RO(dif);
c6a44287 4227
82069379 4228static ssize_t guard_show(struct device_driver *ddp, char *buf)
c6a44287 4229{
68aee7ba 4230 return scnprintf(buf, PAGE_SIZE, "%u\n", scsi_debug_guard);
c6a44287 4231}
82069379 4232static DRIVER_ATTR_RO(guard);
c6a44287 4233
82069379 4234static ssize_t ato_show(struct device_driver *ddp, char *buf)
c6a44287
MP
4235{
4236 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ato);
4237}
82069379 4238static DRIVER_ATTR_RO(ato);
c6a44287 4239
82069379 4240static ssize_t map_show(struct device_driver *ddp, char *buf)
44d92694
MP
4241{
4242 ssize_t count;
4243
5b94e232 4244 if (!scsi_debug_lbp())
44d92694
MP
4245 return scnprintf(buf, PAGE_SIZE, "0-%u\n",
4246 sdebug_store_sectors);
4247
4248 count = bitmap_scnlistprintf(buf, PAGE_SIZE, map_storep, map_size);
4249
4250 buf[count++] = '\n';
4251 buf[count++] = 0;
4252
4253 return count;
4254}
82069379 4255static DRIVER_ATTR_RO(map);
44d92694 4256
82069379 4257static ssize_t removable_show(struct device_driver *ddp, char *buf)
d986788b
MP
4258{
4259 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_removable ? 1 : 0);
4260}
82069379
AM
4261static ssize_t removable_store(struct device_driver *ddp, const char *buf,
4262 size_t count)
d986788b
MP
4263{
4264 int n;
4265
4266 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
4267 scsi_debug_removable = (n > 0);
4268 return count;
4269 }
4270 return -EINVAL;
4271}
82069379 4272static DRIVER_ATTR_RW(removable);
d986788b 4273
cbf67842
DG
4274static ssize_t host_lock_show(struct device_driver *ddp, char *buf)
4275{
4276 return scnprintf(buf, PAGE_SIZE, "%d\n", !!scsi_debug_host_lock);
4277}
4278/* Returns -EBUSY if host_lock is being changed and commands are queued */
4279static ssize_t host_lock_store(struct device_driver *ddp, const char *buf,
4280 size_t count)
4281{
4282 int n, res;
4283
4284 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
4285 bool new_host_lock = (n > 0);
4286
4287 res = count;
4288 if (new_host_lock != scsi_debug_host_lock) {
4289 unsigned long iflags;
4290 int k;
4291
4292 spin_lock_irqsave(&queued_arr_lock, iflags);
4293 k = find_first_bit(queued_in_use_bm,
4294 scsi_debug_max_queue);
4295 if (k != scsi_debug_max_queue)
4296 res = -EBUSY; /* have queued commands */
4297 else
4298 scsi_debug_host_lock = new_host_lock;
4299 spin_unlock_irqrestore(&queued_arr_lock, iflags);
4300 }
4301 return res;
4302 }
4303 return -EINVAL;
4304}
4305static DRIVER_ATTR_RW(host_lock);
4306
c2248fc9
DG
4307static ssize_t strict_show(struct device_driver *ddp, char *buf)
4308{
4309 return scnprintf(buf, PAGE_SIZE, "%d\n", !!scsi_debug_strict);
4310}
4311static ssize_t strict_store(struct device_driver *ddp, const char *buf,
4312 size_t count)
4313{
4314 int n;
4315
4316 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
4317 scsi_debug_strict = (n > 0);
4318 return count;
4319 }
4320 return -EINVAL;
4321}
4322static DRIVER_ATTR_RW(strict);
4323
cbf67842 4324
82069379 4325/* Note: The following array creates attribute files in the
23183910
DG
4326 /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
4327 files (over those found in the /sys/module/scsi_debug/parameters
4328 directory) is that auxiliary actions can be triggered when an attribute
4329 is changed. For example see: sdebug_add_host_store() above.
4330 */
6ecaff7f 4331
82069379
AM
4332static struct attribute *sdebug_drv_attrs[] = {
4333 &driver_attr_delay.attr,
4334 &driver_attr_opts.attr,
4335 &driver_attr_ptype.attr,
4336 &driver_attr_dsense.attr,
4337 &driver_attr_fake_rw.attr,
4338 &driver_attr_no_lun_0.attr,
4339 &driver_attr_num_tgts.attr,
4340 &driver_attr_dev_size_mb.attr,
4341 &driver_attr_num_parts.attr,
4342 &driver_attr_every_nth.attr,
4343 &driver_attr_max_luns.attr,
4344 &driver_attr_max_queue.attr,
4345 &driver_attr_no_uld.attr,
4346 &driver_attr_scsi_level.attr,
4347 &driver_attr_virtual_gb.attr,
4348 &driver_attr_add_host.attr,
4349 &driver_attr_vpd_use_hostno.attr,
4350 &driver_attr_sector_size.attr,
4351 &driver_attr_dix.attr,
4352 &driver_attr_dif.attr,
4353 &driver_attr_guard.attr,
4354 &driver_attr_ato.attr,
4355 &driver_attr_map.attr,
4356 &driver_attr_removable.attr,
cbf67842
DG
4357 &driver_attr_host_lock.attr,
4358 &driver_attr_ndelay.attr,
c2248fc9 4359 &driver_attr_strict.attr,
82069379
AM
4360 NULL,
4361};
4362ATTRIBUTE_GROUPS(sdebug_drv);
1da177e4 4363
11ddceca 4364static struct device *pseudo_primary;
8dea0d02 4365
1da177e4
LT
4366static int __init scsi_debug_init(void)
4367{
5f2578e5 4368 unsigned long sz;
1da177e4
LT
4369 int host_to_add;
4370 int k;
6ecaff7f 4371 int ret;
1da177e4 4372
cbf67842
DG
4373 atomic_set(&sdebug_cmnd_count, 0);
4374 atomic_set(&sdebug_completions, 0);
4375 atomic_set(&retired_max_queue, 0);
4376
4377 if (scsi_debug_ndelay >= 1000000000) {
4378 pr_warn("%s: ndelay must be less than 1 second, ignored\n",
4379 __func__);
4380 scsi_debug_ndelay = 0;
4381 } else if (scsi_debug_ndelay > 0)
4382 scsi_debug_delay = DELAY_OVERRIDDEN;
4383
597136ab
MP
4384 switch (scsi_debug_sector_size) {
4385 case 512:
4386 case 1024:
4387 case 2048:
4388 case 4096:
4389 break;
4390 default:
cbf67842 4391 pr_err("%s: invalid sector_size %d\n", __func__,
597136ab
MP
4392 scsi_debug_sector_size);
4393 return -EINVAL;
4394 }
4395
c6a44287
MP
4396 switch (scsi_debug_dif) {
4397
4398 case SD_DIF_TYPE0_PROTECTION:
4399 case SD_DIF_TYPE1_PROTECTION:
395cef03 4400 case SD_DIF_TYPE2_PROTECTION:
c6a44287
MP
4401 case SD_DIF_TYPE3_PROTECTION:
4402 break;
4403
4404 default:
cbf67842 4405 pr_err("%s: dif must be 0, 1, 2 or 3\n", __func__);
c6a44287
MP
4406 return -EINVAL;
4407 }
4408
4409 if (scsi_debug_guard > 1) {
cbf67842 4410 pr_err("%s: guard must be 0 or 1\n", __func__);
c6a44287
MP
4411 return -EINVAL;
4412 }
4413
4414 if (scsi_debug_ato > 1) {
cbf67842 4415 pr_err("%s: ato must be 0 or 1\n", __func__);
c6a44287
MP
4416 return -EINVAL;
4417 }
4418
ea61fca5 4419 if (scsi_debug_physblk_exp > 15) {
cbf67842 4420 pr_err("%s: invalid physblk_exp %u\n", __func__,
ea61fca5
MP
4421 scsi_debug_physblk_exp);
4422 return -EINVAL;
4423 }
4424
4425 if (scsi_debug_lowest_aligned > 0x3fff) {
cbf67842 4426 pr_err("%s: lowest_aligned too big: %u\n", __func__,
ea61fca5
MP
4427 scsi_debug_lowest_aligned);
4428 return -EINVAL;
4429 }
4430
1da177e4
LT
4431 if (scsi_debug_dev_size_mb < 1)
4432 scsi_debug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
5f2578e5 4433 sz = (unsigned long)scsi_debug_dev_size_mb * 1048576;
597136ab 4434 sdebug_store_sectors = sz / scsi_debug_sector_size;
28898873 4435 sdebug_capacity = get_sdebug_capacity();
1da177e4
LT
4436
4437 /* play around with geometry, don't waste too much on track 0 */
4438 sdebug_heads = 8;
4439 sdebug_sectors_per = 32;
4440 if (scsi_debug_dev_size_mb >= 16)
4441 sdebug_heads = 32;
4442 else if (scsi_debug_dev_size_mb >= 256)
4443 sdebug_heads = 64;
4444 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
4445 (sdebug_sectors_per * sdebug_heads);
4446 if (sdebug_cylinders_per >= 1024) {
4447 /* other LLDs do this; implies >= 1GB ram disk ... */
4448 sdebug_heads = 255;
4449 sdebug_sectors_per = 63;
4450 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
4451 (sdebug_sectors_per * sdebug_heads);
4452 }
4453
cbf67842
DG
4454 if (0 == scsi_debug_fake_rw) {
4455 fake_storep = vmalloc(sz);
4456 if (NULL == fake_storep) {
4457 pr_err("%s: out of memory, 1\n", __func__);
4458 return -ENOMEM;
4459 }
4460 memset(fake_storep, 0, sz);
4461 if (scsi_debug_num_parts > 0)
4462 sdebug_build_parts(fake_storep, sz);
1da177e4 4463 }
1da177e4 4464
7cb69d03 4465 if (scsi_debug_dix) {
c6a44287
MP
4466 int dif_size;
4467
4468 dif_size = sdebug_store_sectors * sizeof(struct sd_dif_tuple);
4469 dif_storep = vmalloc(dif_size);
4470
cbf67842
DG
4471 pr_err("%s: dif_storep %u bytes @ %p\n", __func__, dif_size,
4472 dif_storep);
c6a44287
MP
4473
4474 if (dif_storep == NULL) {
cbf67842 4475 pr_err("%s: out of mem. (DIX)\n", __func__);
c6a44287
MP
4476 ret = -ENOMEM;
4477 goto free_vm;
4478 }
4479
4480 memset(dif_storep, 0xff, dif_size);
4481 }
4482
5b94e232
MP
4483 /* Logical Block Provisioning */
4484 if (scsi_debug_lbp()) {
6014759c
MP
4485 scsi_debug_unmap_max_blocks =
4486 clamp(scsi_debug_unmap_max_blocks, 0U, 0xffffffffU);
4487
4488 scsi_debug_unmap_max_desc =
4489 clamp(scsi_debug_unmap_max_desc, 0U, 256U);
4490
4491 scsi_debug_unmap_granularity =
4492 clamp(scsi_debug_unmap_granularity, 1U, 0xffffffffU);
4493
4494 if (scsi_debug_unmap_alignment &&
ac17078a
AM
4495 scsi_debug_unmap_granularity <=
4496 scsi_debug_unmap_alignment) {
cbf67842 4497 pr_err("%s: ERR: unmap_granularity <= unmap_alignment\n",
44d92694
MP
4498 __func__);
4499 return -EINVAL;
4500 }
4501
b90ebc3d
AM
4502 map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1;
4503 map_storep = vmalloc(BITS_TO_LONGS(map_size) * sizeof(long));
44d92694 4504
cbf67842 4505 pr_info("%s: %lu provisioning blocks\n", __func__, map_size);
44d92694
MP
4506
4507 if (map_storep == NULL) {
cbf67842 4508 pr_err("%s: out of mem. (MAP)\n", __func__);
44d92694
MP
4509 ret = -ENOMEM;
4510 goto free_vm;
4511 }
4512
b90ebc3d 4513 bitmap_zero(map_storep, map_size);
44d92694
MP
4514
4515 /* Map first 1KB for partition table */
4516 if (scsi_debug_num_parts)
4517 map_region(0, 2);
4518 }
4519
9b906779
NB
4520 pseudo_primary = root_device_register("pseudo_0");
4521 if (IS_ERR(pseudo_primary)) {
cbf67842 4522 pr_warn("%s: root_device_register() error\n", __func__);
9b906779 4523 ret = PTR_ERR(pseudo_primary);
6ecaff7f
RD
4524 goto free_vm;
4525 }
4526 ret = bus_register(&pseudo_lld_bus);
4527 if (ret < 0) {
cbf67842 4528 pr_warn("%s: bus_register error: %d\n", __func__, ret);
6ecaff7f
RD
4529 goto dev_unreg;
4530 }
4531 ret = driver_register(&sdebug_driverfs_driver);
4532 if (ret < 0) {
cbf67842 4533 pr_warn("%s: driver_register error: %d\n", __func__, ret);
6ecaff7f
RD
4534 goto bus_unreg;
4535 }
1da177e4 4536
1da177e4
LT
4537 host_to_add = scsi_debug_add_host;
4538 scsi_debug_add_host = 0;
4539
4540 for (k = 0; k < host_to_add; k++) {
4541 if (sdebug_add_adapter()) {
cbf67842
DG
4542 pr_err("%s: sdebug_add_adapter failed k=%d\n",
4543 __func__, k);
1da177e4
LT
4544 break;
4545 }
4546 }
4547
4548 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
cbf67842
DG
4549 pr_info("%s: built %d host(s)\n", __func__,
4550 scsi_debug_add_host);
1da177e4
LT
4551 }
4552 return 0;
6ecaff7f 4553
6ecaff7f
RD
4554bus_unreg:
4555 bus_unregister(&pseudo_lld_bus);
4556dev_unreg:
9b906779 4557 root_device_unregister(pseudo_primary);
6ecaff7f 4558free_vm:
44d92694
MP
4559 if (map_storep)
4560 vfree(map_storep);
c6a44287
MP
4561 if (dif_storep)
4562 vfree(dif_storep);
6ecaff7f
RD
4563 vfree(fake_storep);
4564
4565 return ret;
1da177e4
LT
4566}
4567
4568static void __exit scsi_debug_exit(void)
4569{
4570 int k = scsi_debug_add_host;
4571
4572 stop_all_queued();
cbf67842 4573 free_all_queued();
1da177e4
LT
4574 for (; k; k--)
4575 sdebug_remove_adapter();
1da177e4
LT
4576 driver_unregister(&sdebug_driverfs_driver);
4577 bus_unregister(&pseudo_lld_bus);
9b906779 4578 root_device_unregister(pseudo_primary);
1da177e4 4579
c6a44287
MP
4580 if (dif_storep)
4581 vfree(dif_storep);
4582
1da177e4
LT
4583 vfree(fake_storep);
4584}
4585
4586device_initcall(scsi_debug_init);
4587module_exit(scsi_debug_exit);
4588
1da177e4
LT
4589static void sdebug_release_adapter(struct device * dev)
4590{
4591 struct sdebug_host_info *sdbg_host;
4592
4593 sdbg_host = to_sdebug_host(dev);
4594 kfree(sdbg_host);
4595}
4596
4597static int sdebug_add_adapter(void)
4598{
4599 int k, devs_per_host;
4600 int error = 0;
4601 struct sdebug_host_info *sdbg_host;
8b40228f 4602 struct sdebug_dev_info *sdbg_devinfo, *tmp;
1da177e4 4603
c65b1445 4604 sdbg_host = kzalloc(sizeof(*sdbg_host),GFP_KERNEL);
1da177e4
LT
4605 if (NULL == sdbg_host) {
4606 printk(KERN_ERR "%s: out of memory at line %d\n",
cadbd4a5 4607 __func__, __LINE__);
1da177e4
LT
4608 return -ENOMEM;
4609 }
4610
1da177e4
LT
4611 INIT_LIST_HEAD(&sdbg_host->dev_info_list);
4612
4613 devs_per_host = scsi_debug_num_tgts * scsi_debug_max_luns;
4614 for (k = 0; k < devs_per_host; k++) {
5cb2fc06
FT
4615 sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL);
4616 if (!sdbg_devinfo) {
1da177e4 4617 printk(KERN_ERR "%s: out of memory at line %d\n",
cadbd4a5 4618 __func__, __LINE__);
1da177e4
LT
4619 error = -ENOMEM;
4620 goto clean;
4621 }
1da177e4
LT
4622 }
4623
4624 spin_lock(&sdebug_host_list_lock);
4625 list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
4626 spin_unlock(&sdebug_host_list_lock);
4627
4628 sdbg_host->dev.bus = &pseudo_lld_bus;
9b906779 4629 sdbg_host->dev.parent = pseudo_primary;
1da177e4 4630 sdbg_host->dev.release = &sdebug_release_adapter;
71610f55 4631 dev_set_name(&sdbg_host->dev, "adapter%d", scsi_debug_add_host);
1da177e4
LT
4632
4633 error = device_register(&sdbg_host->dev);
4634
4635 if (error)
4636 goto clean;
4637
4638 ++scsi_debug_add_host;
4639 return error;
4640
4641clean:
8b40228f
FT
4642 list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
4643 dev_list) {
1da177e4
LT
4644 list_del(&sdbg_devinfo->dev_list);
4645 kfree(sdbg_devinfo);
4646 }
4647
4648 kfree(sdbg_host);
4649 return error;
4650}
4651
4652static void sdebug_remove_adapter(void)
4653{
4654 struct sdebug_host_info * sdbg_host = NULL;
4655
4656 spin_lock(&sdebug_host_list_lock);
4657 if (!list_empty(&sdebug_host_list)) {
4658 sdbg_host = list_entry(sdebug_host_list.prev,
4659 struct sdebug_host_info, host_list);
4660 list_del(&sdbg_host->host_list);
4661 }
4662 spin_unlock(&sdebug_host_list_lock);
4663
4664 if (!sdbg_host)
4665 return;
4666
4667 device_unregister(&sdbg_host->dev);
4668 --scsi_debug_add_host;
4669}
4670
cbf67842
DG
4671static int
4672sdebug_queuecommand_lock_or_not(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
4673{
4674 if (scsi_debug_host_lock) {
4675 unsigned long iflags;
4676 int rc;
4677
4678 spin_lock_irqsave(shost->host_lock, iflags);
4679 rc = scsi_debug_queuecommand(cmd);
4680 spin_unlock_irqrestore(shost->host_lock, iflags);
4681 return rc;
4682 } else
4683 return scsi_debug_queuecommand(cmd);
4684}
4685
4686static int
db5ed4df 4687sdebug_change_qdepth(struct scsi_device *sdev, int qdepth)
cbf67842
DG
4688{
4689 int num_in_q = 0;
cbf67842
DG
4690 unsigned long iflags;
4691 struct sdebug_dev_info *devip;
4692
4693 spin_lock_irqsave(&queued_arr_lock, iflags);
4694 devip = (struct sdebug_dev_info *)sdev->hostdata;
4695 if (NULL == devip) {
4696 spin_unlock_irqrestore(&queued_arr_lock, iflags);
4697 return -ENODEV;
4698 }
4699 num_in_q = atomic_read(&devip->num_in_q);
4700 spin_unlock_irqrestore(&queued_arr_lock, iflags);
c40ecc12
CH
4701
4702 if (qdepth < 1)
4703 qdepth = 1;
4704 /* allow to exceed max host queued_arr elements for testing */
4705 if (qdepth > SCSI_DEBUG_CANQUEUE + 10)
4706 qdepth = SCSI_DEBUG_CANQUEUE + 10;
db5ed4df 4707 scsi_change_queue_depth(sdev, qdepth);
c40ecc12 4708
cbf67842 4709 if (SCSI_DEBUG_OPT_Q_NOISE & scsi_debug_opts) {
c40ecc12
CH
4710 sdev_printk(KERN_INFO, sdev,
4711 "%s: qdepth=%d, num_in_q=%d\n",
4712 __func__, qdepth, num_in_q);
cbf67842
DG
4713 }
4714 return sdev->queue_depth;
4715}
4716
4717static int
4718sdebug_change_qtype(struct scsi_device *sdev, int qtype)
4719{
a62182f3 4720 qtype = scsi_change_queue_type(sdev, qtype);
cbf67842
DG
4721 if (SCSI_DEBUG_OPT_Q_NOISE & scsi_debug_opts) {
4722 const char *cp;
4723
4724 switch (qtype) {
4725 case 0:
4726 cp = "untagged";
4727 break;
4728 case MSG_SIMPLE_TAG:
4729 cp = "simple tags";
4730 break;
4731 case MSG_ORDERED_TAG:
4732 cp = "ordered tags";
4733 break;
4734 default:
4735 cp = "unknown";
4736 break;
4737 }
4738 sdev_printk(KERN_INFO, sdev, "%s: to %s\n", __func__, cp);
4739 }
4740 return qtype;
4741}
f281233d 4742
817fd66b
DG
4743static int
4744check_inject(struct scsi_cmnd *scp)
4745{
4746 struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp);
4747
4748 memset(ep, 0, sizeof(struct sdebug_scmd_extra_t));
4749
4750 if (atomic_inc_return(&sdebug_cmnd_count) >=
4751 abs(scsi_debug_every_nth)) {
4752 atomic_set(&sdebug_cmnd_count, 0);
4753 if (scsi_debug_every_nth < -1)
4754 scsi_debug_every_nth = -1;
4755 if (SCSI_DEBUG_OPT_TIMEOUT & scsi_debug_opts)
4756 return 1; /* ignore command causing timeout */
4757 else if (SCSI_DEBUG_OPT_MAC_TIMEOUT & scsi_debug_opts &&
4758 scsi_medium_access_command(scp))
4759 return 1; /* time out reads and writes */
4760 if (sdebug_any_injecting_opt) {
4761 int opts = scsi_debug_opts;
4762
4763 if (SCSI_DEBUG_OPT_RECOVERED_ERR & opts)
4764 ep->inj_recovered = true;
4765 else if (SCSI_DEBUG_OPT_TRANSPORT_ERR & opts)
4766 ep->inj_transport = true;
4767 else if (SCSI_DEBUG_OPT_DIF_ERR & opts)
4768 ep->inj_dif = true;
4769 else if (SCSI_DEBUG_OPT_DIX_ERR & opts)
4770 ep->inj_dix = true;
4771 else if (SCSI_DEBUG_OPT_SHORT_TRANSFER & opts)
4772 ep->inj_short = true;
4773 }
4774 }
4775 return 0;
4776}
4777
c2248fc9
DG
4778static int
4779scsi_debug_queuecommand(struct scsi_cmnd *scp)
4780{
4781 u8 sdeb_i;
4782 struct scsi_device *sdp = scp->device;
4783 const struct opcode_info_t *oip;
4784 const struct opcode_info_t *r_oip;
4785 struct sdebug_dev_info *devip;
4786 u8 *cmd = scp->cmnd;
4787 int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
4788 int k, na;
4789 int errsts = 0;
4790 int errsts_no_connect = DID_NO_CONNECT << 16;
4791 u32 flags;
4792 u16 sa;
4793 u8 opcode = cmd[0];
4794 bool has_wlun_rl;
4795 bool debug = !!(SCSI_DEBUG_OPT_NOISE & scsi_debug_opts);
4796
4797 scsi_set_resid(scp, 0);
4798 if (debug && !(SCSI_DEBUG_OPT_NO_CDB_NOISE & scsi_debug_opts)) {
4799 char b[120];
4800 int n, len, sb;
4801
4802 len = scp->cmd_len;
4803 sb = (int)sizeof(b);
4804 if (len > 32)
4805 strcpy(b, "too long, over 32 bytes");
4806 else {
4807 for (k = 0, n = 0; k < len && n < sb; ++k)
4808 n += scnprintf(b + n, sb - n, "%02x ",
4809 (u32)cmd[k]);
4810 }
4811 sdev_printk(KERN_INFO, sdp, "%s: cmd %s\n", my_name, b);
4812 }
4813 has_wlun_rl = (sdp->lun == SAM2_WLUN_REPORT_LUNS);
4814 if ((sdp->lun >= scsi_debug_max_luns) && !has_wlun_rl)
4815 return schedule_resp(scp, NULL, errsts_no_connect, 0);
4816
4817 sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */
4818 oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */
4819 devip = (struct sdebug_dev_info *)sdp->hostdata;
4820 if (!devip) {
4821 devip = devInfoReg(sdp);
4822 if (NULL == devip)
4823 return schedule_resp(scp, NULL, errsts_no_connect, 0);
4824 }
4825 na = oip->num_attached;
4826 r_pfp = oip->pfp;
4827 if (na) { /* multiple commands with this opcode */
4828 r_oip = oip;
4829 if (FF_SA & r_oip->flags) {
4830 if (F_SA_LOW & oip->flags)
4831 sa = 0x1f & cmd[1];
4832 else
4833 sa = get_unaligned_be16(cmd + 8);
4834 for (k = 0; k <= na; oip = r_oip->arrp + k++) {
4835 if (opcode == oip->opcode && sa == oip->sa)
4836 break;
4837 }
4838 } else { /* since no service action only check opcode */
4839 for (k = 0; k <= na; oip = r_oip->arrp + k++) {
4840 if (opcode == oip->opcode)
4841 break;
4842 }
4843 }
4844 if (k > na) {
4845 if (F_SA_LOW & r_oip->flags)
4846 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4);
4847 else if (F_SA_HIGH & r_oip->flags)
4848 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7);
4849 else
4850 mk_sense_invalid_opcode(scp);
4851 goto check_cond;
4852 }
4853 } /* else (when na==0) we assume the oip is a match */
4854 flags = oip->flags;
4855 if (F_INV_OP & flags) {
4856 mk_sense_invalid_opcode(scp);
4857 goto check_cond;
4858 }
4859 if (has_wlun_rl && !(F_RL_WLUN_OK & flags)) {
4860 if (debug)
4861 sdev_printk(KERN_INFO, sdp, "scsi_debug: Opcode: "
4862 "0x%x not supported for wlun\n", opcode);
4863 mk_sense_invalid_opcode(scp);
4864 goto check_cond;
4865 }
4866 if (scsi_debug_strict) { /* check cdb against mask */
4867 u8 rem;
4868 int j;
4869
4870 for (k = 1; k < oip->len_mask[0] && k < 16; ++k) {
4871 rem = ~oip->len_mask[k] & cmd[k];
4872 if (rem) {
4873 for (j = 7; j >= 0; --j, rem <<= 1) {
4874 if (0x80 & rem)
4875 break;
4876 }
4877 mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j);
4878 goto check_cond;
4879 }
4880 }
4881 }
4882 if (!(F_SKIP_UA & flags) &&
4883 SDEBUG_NUM_UAS != find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS)) {
4884 errsts = check_readiness(scp, UAS_ONLY, devip);
4885 if (errsts)
4886 goto check_cond;
4887 }
4888 if ((F_M_ACCESS & flags) && devip->stopped) {
4889 mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2);
4890 if (debug)
4891 sdev_printk(KERN_INFO, sdp, "%s reports: Not ready: "
4892 "%s\n", my_name, "initializing command "
4893 "required");
4894 errsts = check_condition_result;
4895 goto fini;
4896 }
4897 if (scsi_debug_fake_rw && (F_FAKE_RW & flags))
4898 goto fini;
4899 if (scsi_debug_every_nth) {
4900 if (check_inject(scp))
4901 return 0; /* ignore command: make trouble */
4902 }
4903 if (oip->pfp) /* if this command has a resp_* function, call it */
4904 errsts = oip->pfp(scp, devip);
4905 else if (r_pfp) /* if leaf function ptr NULL, try the root's */
4906 errsts = r_pfp(scp, devip);
4907
4908fini:
4909 return schedule_resp(scp, devip, errsts,
4910 ((F_DELAY_OVERR & flags) ? 0 : scsi_debug_delay));
4911check_cond:
4912 return schedule_resp(scp, devip, check_condition_result, 0);
4913}
4914
9e603ca0 4915static struct scsi_host_template sdebug_driver_template = {
c8ed555a
AV
4916 .show_info = scsi_debug_show_info,
4917 .write_info = scsi_debug_write_info,
9e603ca0
FT
4918 .proc_name = sdebug_proc_name,
4919 .name = "SCSI DEBUG",
4920 .info = scsi_debug_info,
4921 .slave_alloc = scsi_debug_slave_alloc,
4922 .slave_configure = scsi_debug_slave_configure,
4923 .slave_destroy = scsi_debug_slave_destroy,
4924 .ioctl = scsi_debug_ioctl,
cbf67842
DG
4925 .queuecommand = sdebug_queuecommand_lock_or_not,
4926 .change_queue_depth = sdebug_change_qdepth,
4927 .change_queue_type = sdebug_change_qtype,
9e603ca0 4928 .eh_abort_handler = scsi_debug_abort,
9e603ca0 4929 .eh_device_reset_handler = scsi_debug_device_reset,
cbf67842
DG
4930 .eh_target_reset_handler = scsi_debug_target_reset,
4931 .eh_bus_reset_handler = scsi_debug_bus_reset,
9e603ca0 4932 .eh_host_reset_handler = scsi_debug_host_reset,
9e603ca0
FT
4933 .can_queue = SCSI_DEBUG_CANQUEUE,
4934 .this_id = 7,
6bb5e6e7 4935 .sg_tablesize = SCSI_MAX_SG_CHAIN_SEGMENTS,
cbf67842 4936 .cmd_per_lun = DEF_CMD_PER_LUN,
6bb5e6e7 4937 .max_sectors = -1U,
9e603ca0
FT
4938 .use_clustering = DISABLE_CLUSTERING,
4939 .module = THIS_MODULE,
c40ecc12 4940 .track_queue_depth = 1,
817fd66b 4941 .cmd_size = sizeof(struct sdebug_scmd_extra_t),
9e603ca0
FT
4942};
4943
1da177e4
LT
4944static int sdebug_driver_probe(struct device * dev)
4945{
22017ed2 4946 int error = 0;
817fd66b 4947 int opts;
22017ed2
DG
4948 struct sdebug_host_info *sdbg_host;
4949 struct Scsi_Host *hpnt;
c6a44287 4950 int host_prot;
1da177e4
LT
4951
4952 sdbg_host = to_sdebug_host(dev);
4953
78d4e5a0 4954 sdebug_driver_template.can_queue = scsi_debug_max_queue;
0759c666
AM
4955 if (scsi_debug_clustering)
4956 sdebug_driver_template.use_clustering = ENABLE_CLUSTERING;
78d4e5a0
DG
4957 hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
4958 if (NULL == hpnt) {
17c9ff52 4959 pr_err("%s: scsi_host_alloc failed\n", __func__);
78d4e5a0 4960 error = -ENODEV;
1da177e4 4961 return error;
78d4e5a0 4962 }
1da177e4
LT
4963
4964 sdbg_host->shost = hpnt;
4965 *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
4966 if ((hpnt->this_id >= 0) && (scsi_debug_num_tgts > hpnt->this_id))
4967 hpnt->max_id = scsi_debug_num_tgts + 1;
4968 else
4969 hpnt->max_id = scsi_debug_num_tgts;
c65b1445 4970 hpnt->max_lun = SAM2_WLUN_REPORT_LUNS; /* = scsi_debug_max_luns; */
1da177e4 4971
c6a44287
MP
4972 host_prot = 0;
4973
4974 switch (scsi_debug_dif) {
4975
4976 case SD_DIF_TYPE1_PROTECTION:
4977 host_prot = SHOST_DIF_TYPE1_PROTECTION;
4978 if (scsi_debug_dix)
4979 host_prot |= SHOST_DIX_TYPE1_PROTECTION;
4980 break;
4981
4982 case SD_DIF_TYPE2_PROTECTION:
4983 host_prot = SHOST_DIF_TYPE2_PROTECTION;
4984 if (scsi_debug_dix)
4985 host_prot |= SHOST_DIX_TYPE2_PROTECTION;
4986 break;
4987
4988 case SD_DIF_TYPE3_PROTECTION:
4989 host_prot = SHOST_DIF_TYPE3_PROTECTION;
4990 if (scsi_debug_dix)
4991 host_prot |= SHOST_DIX_TYPE3_PROTECTION;
4992 break;
4993
4994 default:
4995 if (scsi_debug_dix)
4996 host_prot |= SHOST_DIX_TYPE0_PROTECTION;
4997 break;
4998 }
4999
5000 scsi_host_set_prot(hpnt, host_prot);
5001
5002 printk(KERN_INFO "scsi_debug: host protection%s%s%s%s%s%s%s\n",
5003 (host_prot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
5004 (host_prot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
5005 (host_prot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
5006 (host_prot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
5007 (host_prot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
5008 (host_prot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
5009 (host_prot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
5010
5011 if (scsi_debug_guard == 1)
5012 scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP);
5013 else
5014 scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC);
5015
817fd66b
DG
5016 opts = scsi_debug_opts;
5017 if (SCSI_DEBUG_OPT_RECOVERED_ERR & opts)
5018 sdebug_any_injecting_opt = true;
5019 else if (SCSI_DEBUG_OPT_TRANSPORT_ERR & opts)
5020 sdebug_any_injecting_opt = true;
5021 else if (SCSI_DEBUG_OPT_DIF_ERR & opts)
5022 sdebug_any_injecting_opt = true;
5023 else if (SCSI_DEBUG_OPT_DIX_ERR & opts)
5024 sdebug_any_injecting_opt = true;
5025 else if (SCSI_DEBUG_OPT_SHORT_TRANSFER & opts)
5026 sdebug_any_injecting_opt = true;
5027
1da177e4
LT
5028 error = scsi_add_host(hpnt, &sdbg_host->dev);
5029 if (error) {
cadbd4a5 5030 printk(KERN_ERR "%s: scsi_add_host failed\n", __func__);
1da177e4
LT
5031 error = -ENODEV;
5032 scsi_host_put(hpnt);
5033 } else
5034 scsi_scan_host(hpnt);
5035
cbf67842 5036 return error;
1da177e4
LT
5037}
5038
5039static int sdebug_driver_remove(struct device * dev)
5040{
1da177e4 5041 struct sdebug_host_info *sdbg_host;
8b40228f 5042 struct sdebug_dev_info *sdbg_devinfo, *tmp;
1da177e4
LT
5043
5044 sdbg_host = to_sdebug_host(dev);
5045
5046 if (!sdbg_host) {
5047 printk(KERN_ERR "%s: Unable to locate host info\n",
cadbd4a5 5048 __func__);
1da177e4
LT
5049 return -ENODEV;
5050 }
5051
5052 scsi_remove_host(sdbg_host->shost);
5053
8b40228f
FT
5054 list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
5055 dev_list) {
1da177e4
LT
5056 list_del(&sdbg_devinfo->dev_list);
5057 kfree(sdbg_devinfo);
5058 }
5059
5060 scsi_host_put(sdbg_host->shost);
5061 return 0;
5062}
5063
8dea0d02
FT
5064static int pseudo_lld_bus_match(struct device *dev,
5065 struct device_driver *dev_driver)
1da177e4 5066{
8dea0d02 5067 return 1;
1da177e4 5068}
8dea0d02
FT
5069
5070static struct bus_type pseudo_lld_bus = {
5071 .name = "pseudo",
5072 .match = pseudo_lld_bus_match,
5073 .probe = sdebug_driver_probe,
5074 .remove = sdebug_driver_remove,
82069379 5075 .drv_groups = sdebug_drv_groups,
8dea0d02 5076};
This page took 1.232516 seconds and 5 git commands to generate.