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