blob: 2ee55d54d1254250aa4fd3452747183355d9aee1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 *
Douglas Gilbert773642d2016-04-25 12:16:28 -04009 * Copyright (C) 2001 - 2016 Douglas Gilbert
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
Douglas Gilbert773642d2016-04-25 12:16:28 -040011 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -040016 * For documentation see http://sg.danny.cz/sg/sdebug26.html
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Tomas Winklerc12879702015-07-28 16:54:20 +030020
21#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24
25#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/errno.h>
Douglas Gilbertb333a812016-04-25 12:16:30 -040027#include <linux/jiffies.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/vmalloc.h>
36#include <linux/moduleparam.h>
Jens Axboe852e0342007-07-16 10:19:24 +020037#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/blkdev.h>
Martin K. Petersenc6a44282009-01-04 03:08:19 -050039#include <linux/crc-t10dif.h>
Douglas Gilbertcbf67842014-07-26 11:55:35 -040040#include <linux/spinlock.h>
41#include <linux/interrupt.h>
42#include <linux/atomic.h>
43#include <linux/hrtimer.h>
Martin K. Petersenc6a44282009-01-04 03:08:19 -050044
45#include <net/checksum.h>
FUJITA Tomonori9ff26ee2008-03-02 18:30:15 +090046
Martin K. Petersen44d92692009-10-15 14:45:27 -040047#include <asm/unaligned.h>
48
FUJITA Tomonori9ff26ee2008-03-02 18:30:15 +090049#include <scsi/scsi.h>
50#include <scsi/scsi_cmnd.h>
51#include <scsi/scsi_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <scsi/scsi_host.h>
53#include <scsi/scsicam.h>
FUJITA Tomonoria34c4e92008-03-25 09:26:50 +090054#include <scsi/scsi_eh.h>
Douglas Gilbertcbf67842014-07-26 11:55:35 -040055#include <scsi/scsi_tcq.h>
Martin K. Petersen395cef02009-09-18 17:33:03 -040056#include <scsi/scsi_dbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Martin K. Petersenc6a44282009-01-04 03:08:19 -050058#include "sd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "scsi_logging.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Douglas Gilbert773642d2016-04-25 12:16:28 -040061/* make sure inq_product_rev string corresponds to this version */
Douglas Gilbertb01f6f82016-04-30 22:44:42 -040062#define SDEBUG_VERSION "1.86"
63static const char *sdebug_version_date = "20160430";
Douglas Gilbertcbf67842014-07-26 11:55:35 -040064
65#define MY_NAME "scsi_debug"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Douglas Gilbert6f3cbf52007-01-05 00:05:25 -050067/* Additional Sense Code (ASC) */
Douglas Gilbertc65b1442006-06-06 00:11:24 -040068#define NO_ADDITIONAL_SENSE 0x0
69#define LOGICAL_UNIT_NOT_READY 0x4
Douglas Gilbertc2248fc2014-11-24 20:46:29 -050070#define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define UNRECOVERED_READ_ERR 0x11
Douglas Gilbertc65b1442006-06-06 00:11:24 -040072#define PARAMETER_LIST_LENGTH_ERR 0x1a
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define INVALID_OPCODE 0x20
Douglas Gilbert22017ed2014-11-24 23:04:47 -050074#define LBA_OUT_OF_RANGE 0x21
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define INVALID_FIELD_IN_CDB 0x24
Douglas Gilbertc65b1442006-06-06 00:11:24 -040076#define INVALID_FIELD_IN_PARAM_LIST 0x26
Douglas Gilbertcbf67842014-07-26 11:55:35 -040077#define UA_RESET_ASC 0x29
78#define UA_CHANGED_ASC 0x2a
Ewan D. Milne19c8ead2014-12-04 11:49:27 -050079#define TARGET_CHANGED_ASC 0x3f
80#define LUNS_CHANGED_ASCQ 0x0e
Douglas Gilbert22017ed2014-11-24 23:04:47 -050081#define INSUFF_RES_ASC 0x55
82#define INSUFF_RES_ASCQ 0x3
Douglas Gilbertcbf67842014-07-26 11:55:35 -040083#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 */
Douglas Gilbert22017ed2014-11-24 23:04:47 -050086#define CAPACITY_CHANGED_ASCQ 0x9
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define SAVING_PARAMS_UNSUP 0x39
Douglas Gilbert6f3cbf52007-01-05 00:05:25 -050088#define TRANSPORT_PROBLEM 0x4b
Douglas Gilbertc65b1442006-06-06 00:11:24 -040089#define THRESHOLD_EXCEEDED 0x5d
90#define LOW_POWER_COND_ON 0x5e
Douglas Gilbert22017ed2014-11-24 23:04:47 -050091#define MISCOMPARE_VERIFY_ASC 0x1d
Ewan D. Milneacafd0b2014-12-04 11:49:28 -050092#define MICROCODE_CHANGED_ASCQ 0x1 /* with TARGET_CHANGED_ASC */
93#define MICROCODE_CHANGED_WO_RESET_ASCQ 0x16
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Douglas Gilbert6f3cbf52007-01-05 00:05:25 -050095/* Additional Sense Code Qualifier (ASCQ) */
96#define ACK_NAK_TO 0x3
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* 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 */
Martin K. Petersen5b94e232011-03-08 02:08:11 -0500105#define DEF_ATO 1
Douglas Gilbertc2206092016-04-25 12:16:31 -0400106#define DEF_JDELAY 1 /* if > 0 unit is a jiffy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define DEF_DEV_SIZE_MB 8
Martin K. Petersen5b94e232011-03-08 02:08:11 -0500108#define DEF_DIF 0
109#define DEF_DIX 0
110#define DEF_D_SENSE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define DEF_EVERY_NTH 0
Martin K. Petersen5b94e232011-03-08 02:08:11 -0500112#define DEF_FAKE_RW 0
113#define DEF_GUARD 0
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400114#define DEF_HOST_LOCK 0
Martin K. Petersen5b94e232011-03-08 02:08:11 -0500115#define DEF_LBPU 0
116#define DEF_LBPWS 0
117#define DEF_LBPWS10 0
Eric Sandeenbe1dd782012-03-08 00:03:59 -0600118#define DEF_LBPRZ 1
Martin K. Petersen5b94e232011-03-08 02:08:11 -0500119#define DEF_LOWEST_ALIGNED 0
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400120#define DEF_NDELAY 0 /* if > 0 unit is a nanosecond */
Martin K. Petersen5b94e232011-03-08 02:08:11 -0500121#define DEF_NO_LUN_0 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define DEF_NUM_PARTS 0
123#define DEF_OPTS 0
Martin K. Petersen32c58442015-12-16 17:53:51 -0500124#define DEF_OPT_BLKS 1024
Martin K. Petersen5b94e232011-03-08 02:08:11 -0500125#define DEF_PHYSBLK_EXP 0
Douglas Gilbertb01f6f82016-04-30 22:44:42 -0400126#define DEF_PTYPE TYPE_DISK
Martin Pittd9867882012-09-06 12:04:33 +0200127#define DEF_REMOVABLE false
Douglas Gilberte46b0342014-08-05 12:21:53 +0200128#define DEF_SCSI_LEVEL 6 /* INQUIRY, byte2 [6->SPC-4] */
Martin K. Petersen5b94e232011-03-08 02:08:11 -0500129#define DEF_SECTOR_SIZE 512
130#define DEF_UNMAP_ALIGNMENT 0
131#define DEF_UNMAP_GRANULARITY 1
Martin K. Petersen60147592010-08-19 11:49:00 -0400132#define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF
133#define DEF_UNMAP_MAX_DESC 256
Martin K. Petersen5b94e232011-03-08 02:08:11 -0500134#define DEF_VIRTUAL_GB 0
135#define DEF_VPD_USE_HOSTNO 1
136#define DEF_WRITESAME_LENGTH 0xFFFF
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500137#define DEF_STRICT 0
Douglas Gilbertc4837392016-05-06 00:40:26 -0400138#define DEF_STATISTICS false
139#define DEF_SUBMIT_QUEUES 1
Douglas Gilbertc2206092016-04-25 12:16:31 -0400140#define JDELAY_OVERRIDDEN -9999
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Douglas Gilbertb01f6f82016-04-30 22:44:42 -0400142#define SDEBUG_LUN_0_VAL 0
143
Douglas Gilbert773642d2016-04-25 12:16:28 -0400144/* bit mask values for sdebug_opts */
145#define SDEBUG_OPT_NOISE 1
146#define SDEBUG_OPT_MEDIUM_ERR 2
147#define SDEBUG_OPT_TIMEOUT 4
148#define SDEBUG_OPT_RECOVERED_ERR 8
149#define SDEBUG_OPT_TRANSPORT_ERR 16
150#define SDEBUG_OPT_DIF_ERR 32
151#define SDEBUG_OPT_DIX_ERR 64
152#define SDEBUG_OPT_MAC_TIMEOUT 128
153#define SDEBUG_OPT_SHORT_TRANSFER 0x100
154#define SDEBUG_OPT_Q_NOISE 0x200
155#define SDEBUG_OPT_ALL_TSF 0x400
156#define SDEBUG_OPT_RARE_TSF 0x800
157#define SDEBUG_OPT_N_WCE 0x1000
158#define SDEBUG_OPT_RESET_NOISE 0x2000
159#define SDEBUG_OPT_NO_CDB_NOISE 0x4000
160#define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \
161 SDEBUG_OPT_RESET_NOISE)
162#define SDEBUG_OPT_ALL_INJECTING (SDEBUG_OPT_RECOVERED_ERR | \
163 SDEBUG_OPT_TRANSPORT_ERR | \
164 SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR | \
165 SDEBUG_OPT_SHORT_TRANSFER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* When "every_nth" > 0 then modulo "every_nth" commands:
Douglas Gilbertfd321192016-04-25 12:16:33 -0400167 * - a missing response is simulated if SDEBUG_OPT_TIMEOUT is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * - a RECOVERED_ERROR is simulated on successful read and write
Douglas Gilbert773642d2016-04-25 12:16:28 -0400169 * commands if SDEBUG_OPT_RECOVERED_ERR is set.
Douglas Gilbert6f3cbf52007-01-05 00:05:25 -0500170 * - a TRANSPORT_ERROR is simulated on successful read and write
Douglas Gilbert773642d2016-04-25 12:16:28 -0400171 * commands if SDEBUG_OPT_TRANSPORT_ERR is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *
173 * When "every_nth" < 0 then after "- every_nth" commands:
Douglas Gilbertfd321192016-04-25 12:16:33 -0400174 * - a missing response is simulated if SDEBUG_OPT_TIMEOUT is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * - a RECOVERED_ERROR is simulated on successful read and write
Douglas Gilbert773642d2016-04-25 12:16:28 -0400176 * commands if SDEBUG_OPT_RECOVERED_ERR is set.
Douglas Gilbert6f3cbf52007-01-05 00:05:25 -0500177 * - a TRANSPORT_ERROR is simulated on successful read and write
Douglas Gilbert773642d2016-04-25 12:16:28 -0400178 * commands if _DEBUG_OPT_TRANSPORT_ERR is set.
179 * This will continue on every subsequent command until some other action
180 * occurs (e.g. the user * writing a new value (other than -1 or 1) to
181 * every_nth via sysfs).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
183
Douglas Gilbertfd321192016-04-25 12:16:33 -0400184/* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs) are returned in
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400185 * priority order. In the subset implemented here lower numbers have higher
186 * priority. The UA numbers should be a sequence starting from 0 with
187 * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */
188#define SDEBUG_UA_POR 0 /* Power on, reset, or bus device reset */
189#define SDEBUG_UA_BUS_RESET 1
190#define SDEBUG_UA_MODE_CHANGED 2
Douglas Gilbert0d01c5d2014-11-24 20:27:51 -0500191#define SDEBUG_UA_CAPACITY_CHANGED 3
Ewan D. Milne19c8ead2014-12-04 11:49:27 -0500192#define SDEBUG_UA_LUNS_CHANGED 4
Ewan D. Milneacafd0b2014-12-04 11:49:28 -0500193#define SDEBUG_UA_MICROCODE_CHANGED 5 /* simulate firmware change */
194#define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 6
195#define SDEBUG_NUM_UAS 7
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400196
Douglas Gilbert773642d2016-04-25 12:16:28 -0400197/* when 1==SDEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * sector on read commands: */
199#define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
Douglas Gilbert32f7ef72011-03-11 10:43:35 -0500200#define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
203 * or "peripheral device" addressing (value 0) */
204#define SAM2_LUN_ADDRESS_METHOD 0
205
Douglas Gilbertc4837392016-05-06 00:40:26 -0400206/* SDEBUG_CANQUEUE is the maximum number of commands that can be queued
207 * (for response) per submit queue at one time. Can be reduced by max_queue
208 * option. Command responses are not queued when jdelay=0 and ndelay=0. The
209 * per-device DEF_CMD_PER_LUN can be changed via sysfs:
210 * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth
211 * but cannot exceed SDEBUG_CANQUEUE .
212 */
213#define SDEBUG_CANQUEUE_WORDS 3 /* a WORD is bits in a long */
214#define SDEBUG_CANQUEUE (SDEBUG_CANQUEUE_WORDS * BITS_PER_LONG)
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400215#define DEF_CMD_PER_LUN 255
216
Douglas Gilbertfd321192016-04-25 12:16:33 -0400217#define F_D_IN 1
218#define F_D_OUT 2
219#define F_D_OUT_MAYBE 4 /* WRITE SAME, NDOB bit */
220#define F_D_UNKN 8
221#define F_RL_WLUN_OK 0x10
222#define F_SKIP_UA 0x20
223#define F_DELAY_OVERR 0x40
224#define F_SA_LOW 0x80 /* cdb byte 1, bits 4 to 0 */
225#define F_SA_HIGH 0x100 /* as used by variable length cdbs */
226#define F_INV_OP 0x200
227#define F_FAKE_RW 0x400
228#define F_M_ACCESS 0x800 /* media access */
229
230#define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR)
231#define FF_DIRECT_IO (F_M_ACCESS | F_FAKE_RW)
232#define FF_SA (F_SA_HIGH | F_SA_LOW)
233
234#define SDEBUG_MAX_PARTS 4
235
Douglas Gilbertb01f6f82016-04-30 22:44:42 -0400236#define SDEBUG_MAX_CMD_LEN 32
Douglas Gilbertfd321192016-04-25 12:16:33 -0400237
238
239struct sdebug_dev_info {
240 struct list_head dev_list;
241 unsigned int channel;
242 unsigned int target;
243 u64 lun;
244 struct sdebug_host_info *sdbg_host;
245 unsigned long uas_bm[1];
246 atomic_t num_in_q;
Douglas Gilbertc4837392016-05-06 00:40:26 -0400247 atomic_t stopped;
Douglas Gilbertfd321192016-04-25 12:16:33 -0400248 bool used;
249};
250
251struct sdebug_host_info {
252 struct list_head host_list;
253 struct Scsi_Host *shost;
254 struct device dev;
255 struct list_head dev_info_list;
256};
257
258#define to_sdebug_host(d) \
259 container_of(d, struct sdebug_host_info, dev)
260
261struct sdebug_defer {
262 struct hrtimer hrt;
263 struct execute_work ew;
Douglas Gilbertc4837392016-05-06 00:40:26 -0400264 int sqa_idx; /* index of sdebug_queue array */
265 int qc_idx; /* index of sdebug_queued_cmd array within sqa_idx */
266 int issuing_cpu;
Douglas Gilbertfd321192016-04-25 12:16:33 -0400267};
268
269struct sdebug_queued_cmd {
Douglas Gilbertc4837392016-05-06 00:40:26 -0400270 /* corresponding bit set in in_use_bm[] in owning struct sdebug_queue
271 * instance indicates this slot is in use.
272 */
Douglas Gilbertfd321192016-04-25 12:16:33 -0400273 struct sdebug_defer *sd_dp;
274 struct scsi_cmnd *a_cmnd;
Douglas Gilbertc4837392016-05-06 00:40:26 -0400275 unsigned int inj_recovered:1;
276 unsigned int inj_transport:1;
277 unsigned int inj_dif:1;
278 unsigned int inj_dix:1;
279 unsigned int inj_short:1;
Douglas Gilbertfd321192016-04-25 12:16:33 -0400280};
281
Douglas Gilbertc4837392016-05-06 00:40:26 -0400282struct sdebug_queue {
283 struct sdebug_queued_cmd qc_arr[SDEBUG_CANQUEUE];
284 unsigned long in_use_bm[SDEBUG_CANQUEUE_WORDS];
285 spinlock_t qc_lock;
286 atomic_t blocked; /* to temporarily stop more being queued */
Douglas Gilbertfd321192016-04-25 12:16:33 -0400287};
288
Douglas Gilbertc4837392016-05-06 00:40:26 -0400289static atomic_t sdebug_cmnd_count; /* number of incoming commands */
290static atomic_t sdebug_completions; /* count of deferred completions */
291static atomic_t sdebug_miss_cpus; /* submission + completion cpus differ */
292static atomic_t sdebug_a_tsf; /* 'almost task set full' counter */
293
Douglas Gilbertfd321192016-04-25 12:16:33 -0400294struct opcode_info_t {
Douglas Gilbertb01f6f82016-04-30 22:44:42 -0400295 u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff */
296 /* for terminating element */
Douglas Gilbertfd321192016-04-25 12:16:33 -0400297 u8 opcode; /* if num_attached > 0, preferred */
298 u16 sa; /* service action */
299 u32 flags; /* OR-ed set of SDEB_F_* */
300 int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
301 const struct opcode_info_t *arrp; /* num_attached elements or NULL */
302 u8 len_mask[16]; /* len=len_mask[0], then mask for cdb[1]... */
303 /* ignore cdb bytes after position 15 */
304};
305
306/* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500307enum sdeb_opcode_index {
308 SDEB_I_INVALID_OPCODE = 0,
309 SDEB_I_INQUIRY = 1,
310 SDEB_I_REPORT_LUNS = 2,
311 SDEB_I_REQUEST_SENSE = 3,
312 SDEB_I_TEST_UNIT_READY = 4,
313 SDEB_I_MODE_SENSE = 5, /* 6, 10 */
314 SDEB_I_MODE_SELECT = 6, /* 6, 10 */
315 SDEB_I_LOG_SENSE = 7,
316 SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */
317 SDEB_I_READ = 9, /* 6, 10, 12, 16 */
318 SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */
319 SDEB_I_START_STOP = 11,
320 SDEB_I_SERV_ACT_IN = 12, /* 12, 16 */
321 SDEB_I_SERV_ACT_OUT = 13, /* 12, 16 */
322 SDEB_I_MAINT_IN = 14,
323 SDEB_I_MAINT_OUT = 15,
324 SDEB_I_VERIFY = 16, /* 10 only */
325 SDEB_I_VARIABLE_LEN = 17,
326 SDEB_I_RESERVE = 18, /* 6, 10 */
327 SDEB_I_RELEASE = 19, /* 6, 10 */
328 SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */
329 SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */
330 SDEB_I_ATA_PT = 22, /* 12, 16 */
331 SDEB_I_SEND_DIAG = 23,
332 SDEB_I_UNMAP = 24,
333 SDEB_I_XDWRITEREAD = 25, /* 10 only */
334 SDEB_I_WRITE_BUFFER = 26,
335 SDEB_I_WRITE_SAME = 27, /* 10, 16 */
336 SDEB_I_SYNC_CACHE = 28, /* 10 only */
337 SDEB_I_COMP_WRITE = 29,
338 SDEB_I_LAST_ELEMENT = 30, /* keep this last */
339};
340
Douglas Gilbertc4837392016-05-06 00:40:26 -0400341
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500342static const unsigned char opcode_ind_arr[256] = {
343/* 0x0; 0x0->0x1f: 6 byte cdbs */
344 SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE,
345 0, 0, 0, 0,
346 SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0,
347 0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
348 SDEB_I_RELEASE,
349 0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG,
350 SDEB_I_ALLOW_REMOVAL, 0,
351/* 0x20; 0x20->0x3f: 10 byte cdbs */
352 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0,
353 SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY,
354 0, 0, 0, 0, 0, SDEB_I_SYNC_CACHE, 0, 0,
355 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0,
356/* 0x40; 0x40->0x5f: 10 byte cdbs */
357 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0,
358 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0,
359 0, 0, 0, SDEB_I_XDWRITEREAD, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
360 SDEB_I_RELEASE,
361 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0,
Douglas Gilbertfd321192016-04-25 12:16:33 -0400362/* 0x60; 0x60->0x7d are reserved, 0x7e is "extended cdb" */
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500363 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
364 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
365 0, SDEB_I_VARIABLE_LEN,
366/* 0x80; 0x80->0x9f: 16 byte cdbs */
367 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0,
368 SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0, 0, 0, 0, 0,
369 0, 0, 0, SDEB_I_WRITE_SAME, 0, 0, 0, 0,
370 0, 0, 0, 0, 0, 0, SDEB_I_SERV_ACT_IN, SDEB_I_SERV_ACT_OUT,
371/* 0xa0; 0xa0->0xbf: 12 byte cdbs */
372 SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN,
373 SDEB_I_MAINT_OUT, 0, 0, 0,
374 SDEB_I_READ, SDEB_I_SERV_ACT_OUT, SDEB_I_WRITE, SDEB_I_SERV_ACT_IN,
375 0, 0, 0, 0,
376 0, 0, 0, 0, 0, 0, 0, 0,
377 0, 0, 0, 0, 0, 0, 0, 0,
378/* 0xc0; 0xc0->0xff: vendor specific */
379 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
380 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
381 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
382 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
383};
384
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500385static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *);
386static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *);
387static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *);
388static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
389static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *);
390static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
391static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *);
392static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
393static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
394static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *);
395static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *);
396static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *);
397static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *);
398static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *);
Douglas Gilbert38d5c832014-11-24 21:27:12 -0500399static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *);
400static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500401static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *);
402static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *);
403static int resp_xdwriteread_10(struct scsi_cmnd *, struct sdebug_dev_info *);
Douglas Gilbert38d5c832014-11-24 21:27:12 -0500404static int resp_comp_write(struct scsi_cmnd *, struct sdebug_dev_info *);
Ewan D. Milneacafd0b2014-12-04 11:49:28 -0500405static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500406
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500407static const struct opcode_info_t msense_iarr[1] = {
408 {0, 0x1a, 0, F_D_IN, NULL, NULL,
409 {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
410};
411
412static const struct opcode_info_t mselect_iarr[1] = {
413 {0, 0x15, 0, F_D_OUT, NULL, NULL,
414 {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
415};
416
417static const struct opcode_info_t read_iarr[3] = {
418 {0, 0x28, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL,/* READ(10) */
419 {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0,
420 0, 0, 0, 0} },
421 {0, 0x8, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL, /* READ(6) */
422 {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
423 {0, 0xa8, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL,/* READ(12) */
424 {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f,
425 0xc7, 0, 0, 0, 0} },
426};
427
428static const struct opcode_info_t write_iarr[3] = {
429 {0, 0x2a, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 10 */
430 {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0,
431 0, 0, 0, 0} },
432 {0, 0xa, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 6 */
433 {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
434 {0, 0xaa, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 12 */
435 {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f,
436 0xc7, 0, 0, 0, 0} },
437};
438
439static const struct opcode_info_t sa_in_iarr[1] = {
440 {0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL,
441 {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
442 0xff, 0xff, 0xff, 0, 0xc7} },
443};
444
445static const struct opcode_info_t vl_iarr[1] = { /* VARIABLE LENGTH */
446 {0, 0x7f, 0xb, F_SA_HIGH | F_D_OUT | FF_DIRECT_IO, resp_write_dt0,
447 NULL, {32, 0xc7, 0, 0, 0, 0, 0x1f, 0x18, 0x0, 0xb, 0xfa,
448 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */
449};
450
451static const struct opcode_info_t maint_in_iarr[2] = {
Douglas Gilbert38d5c832014-11-24 21:27:12 -0500452 {0, 0xa3, 0xc, F_SA_LOW | F_D_IN, resp_rsup_opcodes, NULL,
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500453 {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0,
454 0xc7, 0, 0, 0, 0} },
Douglas Gilbert38d5c832014-11-24 21:27:12 -0500455 {0, 0xa3, 0xd, F_SA_LOW | F_D_IN, resp_rsup_tmfs, NULL,
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500456 {12, 0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
457 0, 0} },
458};
459
460static const struct opcode_info_t write_same_iarr[1] = {
461 {0, 0x93, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, resp_write_same_16, NULL,
462 {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
463 0xff, 0xff, 0xff, 0x1f, 0xc7} },
464};
465
466static const struct opcode_info_t reserve_iarr[1] = {
467 {0, 0x16, 0, F_D_OUT, NULL, NULL, /* RESERVE(6) */
468 {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
469};
470
471static const struct opcode_info_t release_iarr[1] = {
472 {0, 0x17, 0, F_D_OUT, NULL, NULL, /* RELEASE(6) */
473 {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
474};
475
476
477/* This array is accessed via SDEB_I_* values. Make sure all are mapped,
478 * plus the terminating elements for logic that scans this table such as
479 * REPORT SUPPORTED OPERATION CODES. */
480static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEMENT + 1] = {
481/* 0 */
482 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL,
483 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
484 {0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL,
485 {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
486 {0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL,
487 {12, 0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
488 0, 0} },
489 {0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL,
490 {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
491 {0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */
492 {6, 0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
493 {1, 0x5a, 0, F_D_IN, resp_mode_sense, msense_iarr,
494 {10, 0xf8, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
495 0} },
496 {1, 0x55, 0, F_D_OUT, resp_mode_select, mselect_iarr,
497 {10, 0xf1, 0, 0, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
498 {0, 0x4d, 0, F_D_IN, resp_log_sense, NULL,
499 {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0,
500 0, 0, 0} },
501 {0, 0x25, 0, F_D_IN, resp_readcap, NULL,
502 {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0,
503 0, 0} },
504 {3, 0x88, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, read_iarr,
505 {16, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
506 0xff, 0xff, 0xff, 0x9f, 0xc7} }, /* READ(16) */
507/* 10 */
508 {3, 0x8a, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, write_iarr,
509 {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
510 0xff, 0xff, 0xff, 0x9f, 0xc7} }, /* WRITE(16) */
511 {0, 0x1b, 0, 0, resp_start_stop, NULL, /* START STOP UNIT */
512 {6, 0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
513 {1, 0x9e, 0x10, F_SA_LOW | F_D_IN, resp_readcap16, sa_in_iarr,
514 {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
515 0xff, 0xff, 0xff, 0x1, 0xc7} }, /* READ CAPACITY(16) */
516 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* SA OUT */
517 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
518 {2, 0xa3, 0xa, F_SA_LOW | F_D_IN, resp_report_tgtpgs, maint_in_iarr,
519 {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, 0,
520 0} },
521 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */
522 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
Douglas Gilbertf7f9f262015-11-22 12:11:28 -0500523 {0, 0x2f, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, NULL, NULL, /* VERIFY(10) */
524 {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7,
525 0, 0, 0, 0, 0, 0} },
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500526 {1, 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_DIRECT_IO, resp_read_dt0,
527 vl_iarr, {32, 0xc7, 0, 0, 0, 0, 0x1f, 0x18, 0x0, 0x9, 0xfe, 0,
528 0xff, 0xff, 0xff, 0xff} },/* VARIABLE LENGTH, READ(32) */
529 {1, 0x56, 0, F_D_OUT, NULL, reserve_iarr, /* RESERVE(10) */
530 {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
531 0} },
532 {1, 0x57, 0, F_D_OUT, NULL, release_iarr, /* RELEASE(10) */
533 {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
534 0} },
535/* 20 */
Douglas Gilbertf7f9f262015-11-22 12:11:28 -0500536 {0, 0x1e, 0, 0, NULL, NULL, /* ALLOW REMOVAL */
537 {6, 0, 0, 0, 0x3, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500538 {0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */
539 {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
540 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */
541 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
542 {0, 0x1d, F_D_OUT, 0, NULL, NULL, /* SEND DIAGNOSTIC */
543 {6, 0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
544 {0, 0x42, 0, F_D_OUT | FF_DIRECT_IO, resp_unmap, NULL, /* UNMAP */
545 {10, 0x1, 0, 0, 0, 0, 0x1f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
546 {0, 0x53, 0, F_D_IN | F_D_OUT | FF_DIRECT_IO, resp_xdwriteread_10,
547 NULL, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7,
548 0, 0, 0, 0, 0, 0} },
Ewan D. Milneacafd0b2014-12-04 11:49:28 -0500549 {0, 0x3b, 0, F_D_OUT_MAYBE, resp_write_buffer, NULL,
550 {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0,
551 0, 0, 0, 0} }, /* WRITE_BUFFER */
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500552 {1, 0x41, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, resp_write_same_10,
553 write_same_iarr, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff,
554 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
555 {0, 0x35, 0, F_DELAY_OVERR | FF_DIRECT_IO, NULL, NULL, /* SYNC_CACHE */
556 {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0,
557 0, 0, 0, 0} },
Douglas Gilbert38d5c832014-11-24 21:27:12 -0500558 {0, 0x89, 0, F_D_OUT | FF_DIRECT_IO, resp_comp_write, NULL,
Douglas Gilbertc2248fc2014-11-24 20:46:29 -0500559 {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0,
560 0, 0xff, 0x1f, 0xc7} }, /* COMPARE AND WRITE */
561
562/* 30 */
563 {0xff, 0, 0, 0, NULL, NULL, /* terminating element */
564 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
565};
566
Douglas Gilbert773642d2016-04-25 12:16:28 -0400567static int sdebug_add_host = DEF_NUM_HOST;
568static int sdebug_ato = DEF_ATO;
Douglas Gilbertc2206092016-04-25 12:16:31 -0400569static int sdebug_jdelay = DEF_JDELAY; /* if > 0 then unit is jiffies */
Douglas Gilbert773642d2016-04-25 12:16:28 -0400570static int sdebug_dev_size_mb = DEF_DEV_SIZE_MB;
571static int sdebug_dif = DEF_DIF;
572static int sdebug_dix = DEF_DIX;
573static int sdebug_dsense = DEF_D_SENSE;
574static int sdebug_every_nth = DEF_EVERY_NTH;
575static int sdebug_fake_rw = DEF_FAKE_RW;
576static unsigned int sdebug_guard = DEF_GUARD;
577static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED;
578static int sdebug_max_luns = DEF_MAX_LUNS;
Douglas Gilbertc4837392016-05-06 00:40:26 -0400579static int sdebug_max_queue = SDEBUG_CANQUEUE; /* per submit queue */
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400580static atomic_t retired_max_queue; /* if > 0 then was prior max_queue */
Douglas Gilbertc2206092016-04-25 12:16:31 -0400581static int sdebug_ndelay = DEF_NDELAY; /* if > 0 then unit is nanoseconds */
Douglas Gilbert773642d2016-04-25 12:16:28 -0400582static int sdebug_no_lun_0 = DEF_NO_LUN_0;
583static int sdebug_no_uld;
584static int sdebug_num_parts = DEF_NUM_PARTS;
585static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */
586static int sdebug_opt_blks = DEF_OPT_BLKS;
587static int sdebug_opts = DEF_OPTS;
588static int sdebug_physblk_exp = DEF_PHYSBLK_EXP;
Douglas Gilbertb01f6f82016-04-30 22:44:42 -0400589static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral device type */
Douglas Gilbert773642d2016-04-25 12:16:28 -0400590static int sdebug_scsi_level = DEF_SCSI_LEVEL;
591static int sdebug_sector_size = DEF_SECTOR_SIZE;
592static int sdebug_virtual_gb = DEF_VIRTUAL_GB;
593static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO;
594static unsigned int sdebug_lbpu = DEF_LBPU;
595static unsigned int sdebug_lbpws = DEF_LBPWS;
596static unsigned int sdebug_lbpws10 = DEF_LBPWS10;
597static unsigned int sdebug_lbprz = DEF_LBPRZ;
598static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT;
599static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY;
600static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS;
601static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC;
602static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH;
603static bool sdebug_removable = DEF_REMOVABLE;
604static bool sdebug_clustering;
605static bool sdebug_host_lock = DEF_HOST_LOCK;
606static bool sdebug_strict = DEF_STRICT;
Douglas Gilbert817fd662014-11-24 20:18:02 -0500607static bool sdebug_any_injecting_opt;
Douglas Gilbert773642d2016-04-25 12:16:28 -0400608static bool sdebug_verbose;
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -0400609static bool have_dif_prot;
Douglas Gilbertc4837392016-05-06 00:40:26 -0400610static bool sdebug_statistics = DEF_STATISTICS;
611static bool sdebug_mq_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Douglas Gilbertc65b1442006-06-06 00:11:24 -0400613static unsigned int sdebug_store_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614static sector_t sdebug_capacity; /* in sectors */
615
616/* old BIOS stuff, kernel may get rid of them but some mode sense pages
617 may still need them */
618static int sdebug_heads; /* heads per disk */
619static int sdebug_cylinders_per; /* cylinders per surface */
620static int sdebug_sectors_per; /* sectors per cylinder */
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622static LIST_HEAD(sdebug_host_list);
623static DEFINE_SPINLOCK(sdebug_host_list_lock);
624
Douglas Gilbertfd321192016-04-25 12:16:33 -0400625static unsigned char *fake_storep; /* ramdisk storage */
Akinobu Mitae18d8be2013-06-29 17:59:18 +0900626static struct sd_dif_tuple *dif_storep; /* protection info */
Martin K. Petersen44d92692009-10-15 14:45:27 -0400627static void *map_storep; /* provisioning map */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Martin K. Petersen44d92692009-10-15 14:45:27 -0400629static unsigned long map_size;
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400630static int num_aborts;
631static int num_dev_resets;
632static int num_target_resets;
633static int num_bus_resets;
634static int num_host_resets;
Martin K. Petersenc6a44282009-01-04 03:08:19 -0500635static int dix_writes;
636static int dix_reads;
637static int dif_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Douglas Gilbertc4837392016-05-06 00:40:26 -0400639static int submit_queues = DEF_SUBMIT_QUEUES; /* > 1 for multi-queue (mq) */
640static struct sdebug_queue *sdebug_q_arr; /* ptr to array of submit queues */
Douglas Gilbertfd321192016-04-25 12:16:33 -0400641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642static DEFINE_RWLOCK(atomic_rw);
643
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400644static char sdebug_proc_name[] = MY_NAME;
645static const char *my_name = MY_NAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647static struct bus_type pseudo_lld_bus;
648
649static struct device_driver sdebug_driverfs_driver = {
650 .name = sdebug_proc_name,
651 .bus = &pseudo_lld_bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652};
653
654static const int check_condition_result =
655 (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
656
Martin K. Petersenc6a44282009-01-04 03:08:19 -0500657static const int illegal_condition_result =
658 (DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
659
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400660static const int device_qfull_result =
661 (DID_OK << 16) | (COMMAND_COMPLETE << 8) | SAM_STAT_TASK_SET_FULL;
662
Douglas Gilbertfd321192016-04-25 12:16:33 -0400663
Douglas Gilbertb01f6f82016-04-30 22:44:42 -0400664static inline unsigned int scsi_debug_lbp(void)
Douglas Gilbertfd321192016-04-25 12:16:33 -0400665{
666 return 0 == sdebug_fake_rw &&
667 (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10);
668}
Douglas Gilbertc65b1442006-06-06 00:11:24 -0400669
Akinobu Mita14faa942013-09-18 21:27:24 +0900670static void *fake_store(unsigned long long lba)
671{
672 lba = do_div(lba, sdebug_store_sectors);
673
Douglas Gilbert773642d2016-04-25 12:16:28 -0400674 return fake_storep + lba * sdebug_sector_size;
Akinobu Mita14faa942013-09-18 21:27:24 +0900675}
676
677static struct sd_dif_tuple *dif_store(sector_t sector)
678{
Arnd Bergmann49413112015-11-20 17:38:28 +0100679 sector = sector_div(sector, sdebug_store_sectors);
Akinobu Mita14faa942013-09-18 21:27:24 +0900680
681 return dif_storep + sector;
682}
683
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +0900684static void sdebug_max_tgts_luns(void)
685{
686 struct sdebug_host_info *sdbg_host;
687 struct Scsi_Host *hpnt;
688
689 spin_lock(&sdebug_host_list_lock);
690 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
691 hpnt = sdbg_host->shost;
692 if ((hpnt->this_id >= 0) &&
Douglas Gilbert773642d2016-04-25 12:16:28 -0400693 (sdebug_num_tgts > hpnt->this_id))
694 hpnt->max_id = sdebug_num_tgts + 1;
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +0900695 else
Douglas Gilbert773642d2016-04-25 12:16:28 -0400696 hpnt->max_id = sdebug_num_tgts;
697 /* sdebug_max_luns; */
Tomas Winklerf2d3fd22015-07-28 16:54:25 +0300698 hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +0900699 }
700 spin_unlock(&sdebug_host_list_lock);
701}
702
Douglas Gilbert22017ed2014-11-24 23:04:47 -0500703enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1};
704
705/* Set in_bit to -1 to indicate no bit position of invalid field */
Douglas Gilbertfd321192016-04-25 12:16:33 -0400706static void mk_sense_invalid_fld(struct scsi_cmnd *scp,
707 enum sdeb_cmd_data c_d,
708 int in_byte, int in_bit)
Douglas Gilbert22017ed2014-11-24 23:04:47 -0500709{
710 unsigned char *sbuff;
711 u8 sks[4];
712 int sl, asc;
713
714 sbuff = scp->sense_buffer;
715 if (!sbuff) {
716 sdev_printk(KERN_ERR, scp->device,
717 "%s: sense_buffer is NULL\n", __func__);
718 return;
719 }
720 asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST;
721 memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
Douglas Gilbert773642d2016-04-25 12:16:28 -0400722 scsi_build_sense_buffer(sdebug_dsense, sbuff, ILLEGAL_REQUEST, asc, 0);
Douglas Gilbert22017ed2014-11-24 23:04:47 -0500723 memset(sks, 0, sizeof(sks));
724 sks[0] = 0x80;
725 if (c_d)
726 sks[0] |= 0x40;
727 if (in_bit >= 0) {
728 sks[0] |= 0x8;
729 sks[0] |= 0x7 & in_bit;
730 }
731 put_unaligned_be16(in_byte, sks + 1);
Douglas Gilbert773642d2016-04-25 12:16:28 -0400732 if (sdebug_dsense) {
Douglas Gilbert22017ed2014-11-24 23:04:47 -0500733 sl = sbuff[7] + 8;
734 sbuff[7] = sl;
735 sbuff[sl] = 0x2;
736 sbuff[sl + 1] = 0x6;
737 memcpy(sbuff + sl + 4, sks, 3);
738 } else
739 memcpy(sbuff + 15, sks, 3);
Douglas Gilbert773642d2016-04-25 12:16:28 -0400740 if (sdebug_verbose)
Douglas Gilbert22017ed2014-11-24 23:04:47 -0500741 sdev_printk(KERN_INFO, scp->device, "%s: [sense_key,asc,ascq"
742 "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n",
743 my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit);
744}
745
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400746static void mk_sense_buffer(struct scsi_cmnd *scp, int key, int asc, int asq)
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +0900747{
748 unsigned char *sbuff;
749
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400750 sbuff = scp->sense_buffer;
751 if (!sbuff) {
752 sdev_printk(KERN_ERR, scp->device,
753 "%s: sense_buffer is NULL\n", __func__);
754 return;
755 }
756 memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +0900757
Douglas Gilbert773642d2016-04-25 12:16:28 -0400758 scsi_build_sense_buffer(sdebug_dsense, sbuff, key, asc, asq);
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +0900759
Douglas Gilbert773642d2016-04-25 12:16:28 -0400760 if (sdebug_verbose)
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400761 sdev_printk(KERN_INFO, scp->device,
762 "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n",
763 my_name, key, asc, asq);
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +0900764}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Douglas Gilbertfd321192016-04-25 12:16:33 -0400766static void mk_sense_invalid_opcode(struct scsi_cmnd *scp)
Douglas Gilbert22017ed2014-11-24 23:04:47 -0500767{
768 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
772{
Douglas Gilbert773642d2016-04-25 12:16:28 -0400773 if (sdebug_verbose) {
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400774 if (0x1261 == cmd)
775 sdev_printk(KERN_INFO, dev,
776 "%s: BLKFLSBUF [0x1261]\n", __func__);
777 else if (0x5331 == cmd)
778 sdev_printk(KERN_INFO, dev,
779 "%s: CDROM_GET_CAPABILITY [0x5331]\n",
780 __func__);
781 else
782 sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n",
783 __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785 return -EINVAL;
786 /* return -ENOTTY; // correct return but upsets fdisk */
787}
788
Ewan D. Milne19c8ead2014-12-04 11:49:27 -0500789static void clear_luns_changed_on_target(struct sdebug_dev_info *devip)
790{
791 struct sdebug_host_info *sdhp;
792 struct sdebug_dev_info *dp;
793
794 spin_lock(&sdebug_host_list_lock);
795 list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
796 list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) {
797 if ((devip->sdbg_host == dp->sdbg_host) &&
798 (devip->target == dp->target))
799 clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm);
800 }
801 }
802 spin_unlock(&sdebug_host_list_lock);
803}
804
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -0400805static int make_ua(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400807 int k;
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400808
809 k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS);
810 if (k != SDEBUG_NUM_UAS) {
811 const char *cp = NULL;
812
813 switch (k) {
814 case SDEBUG_UA_POR:
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -0400815 mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC,
816 POWER_ON_RESET_ASCQ);
Douglas Gilbert773642d2016-04-25 12:16:28 -0400817 if (sdebug_verbose)
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400818 cp = "power on reset";
819 break;
820 case SDEBUG_UA_BUS_RESET:
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -0400821 mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC,
822 BUS_RESET_ASCQ);
Douglas Gilbert773642d2016-04-25 12:16:28 -0400823 if (sdebug_verbose)
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400824 cp = "bus reset";
825 break;
826 case SDEBUG_UA_MODE_CHANGED:
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -0400827 mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC,
828 MODE_CHANGED_ASCQ);
Douglas Gilbert773642d2016-04-25 12:16:28 -0400829 if (sdebug_verbose)
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400830 cp = "mode parameters changed";
831 break;
Douglas Gilbert0d01c5d2014-11-24 20:27:51 -0500832 case SDEBUG_UA_CAPACITY_CHANGED:
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -0400833 mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC,
834 CAPACITY_CHANGED_ASCQ);
Douglas Gilbert773642d2016-04-25 12:16:28 -0400835 if (sdebug_verbose)
Douglas Gilbert0d01c5d2014-11-24 20:27:51 -0500836 cp = "capacity data changed";
Ewan D. Milnef49accf2014-12-04 11:49:25 -0500837 break;
Ewan D. Milneacafd0b2014-12-04 11:49:28 -0500838 case SDEBUG_UA_MICROCODE_CHANGED:
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -0400839 mk_sense_buffer(scp, UNIT_ATTENTION,
Douglas Gilbertb01f6f82016-04-30 22:44:42 -0400840 TARGET_CHANGED_ASC,
841 MICROCODE_CHANGED_ASCQ);
Douglas Gilbert773642d2016-04-25 12:16:28 -0400842 if (sdebug_verbose)
Ewan D. Milneacafd0b2014-12-04 11:49:28 -0500843 cp = "microcode has been changed";
844 break;
845 case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET:
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -0400846 mk_sense_buffer(scp, UNIT_ATTENTION,
Ewan D. Milneacafd0b2014-12-04 11:49:28 -0500847 TARGET_CHANGED_ASC,
848 MICROCODE_CHANGED_WO_RESET_ASCQ);
Douglas Gilbert773642d2016-04-25 12:16:28 -0400849 if (sdebug_verbose)
Ewan D. Milneacafd0b2014-12-04 11:49:28 -0500850 cp = "microcode has been changed without reset";
851 break;
Ewan D. Milne19c8ead2014-12-04 11:49:27 -0500852 case SDEBUG_UA_LUNS_CHANGED:
853 /*
854 * SPC-3 behavior is to report a UNIT ATTENTION with
855 * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN
856 * on the target, until a REPORT LUNS command is
857 * received. SPC-4 behavior is to report it only once.
Douglas Gilbert773642d2016-04-25 12:16:28 -0400858 * NOTE: sdebug_scsi_level does not use the same
Ewan D. Milne19c8ead2014-12-04 11:49:27 -0500859 * values as struct scsi_device->scsi_level.
860 */
Douglas Gilbert773642d2016-04-25 12:16:28 -0400861 if (sdebug_scsi_level >= 6) /* SPC-4 and above */
Ewan D. Milne19c8ead2014-12-04 11:49:27 -0500862 clear_luns_changed_on_target(devip);
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -0400863 mk_sense_buffer(scp, UNIT_ATTENTION,
Ewan D. Milne19c8ead2014-12-04 11:49:27 -0500864 TARGET_CHANGED_ASC,
865 LUNS_CHANGED_ASCQ);
Douglas Gilbert773642d2016-04-25 12:16:28 -0400866 if (sdebug_verbose)
Ewan D. Milne19c8ead2014-12-04 11:49:27 -0500867 cp = "reported luns data has changed";
868 break;
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400869 default:
Douglas Gilbert773642d2016-04-25 12:16:28 -0400870 pr_warn("unexpected unit attention code=%d\n", k);
871 if (sdebug_verbose)
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400872 cp = "unknown";
873 break;
874 }
875 clear_bit(k, devip->uas_bm);
Douglas Gilbert773642d2016-04-25 12:16:28 -0400876 if (sdebug_verbose)
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -0400877 sdev_printk(KERN_INFO, scp->device,
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400878 "%s reports: Unit attention: %s\n",
879 my_name, cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return check_condition_result;
881 }
882 return 0;
883}
884
885/* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */
FUJITA Tomonori21a61822008-03-09 13:44:30 +0900886static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 int arr_len)
888{
FUJITA Tomonori21a61822008-03-09 13:44:30 +0900889 int act_len;
FUJITA Tomonori072d0bb2008-01-23 01:32:00 +0900890 struct scsi_data_buffer *sdb = scsi_in(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
FUJITA Tomonori072d0bb2008-01-23 01:32:00 +0900892 if (!sdb->length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return 0;
FUJITA Tomonori072d0bb2008-01-23 01:32:00 +0900894 if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE))
Douglas Gilbert773642d2016-04-25 12:16:28 -0400895 return DID_ERROR << 16;
FUJITA Tomonori21a61822008-03-09 13:44:30 +0900896
897 act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents,
898 arr, arr_len);
Akinobu Mitaa4517512013-07-08 16:01:57 -0700899 sdb->resid = scsi_bufflen(scp) - act_len;
FUJITA Tomonori21a61822008-03-09 13:44:30 +0900900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return 0;
902}
903
904/* Returns number of bytes fetched into 'arr' or -1 if error. */
FUJITA Tomonori21a61822008-03-09 13:44:30 +0900905static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
906 int arr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
FUJITA Tomonori21a61822008-03-09 13:44:30 +0900908 if (!scsi_bufflen(scp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return 0;
FUJITA Tomonori072d0bb2008-01-23 01:32:00 +0900910 if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_TO_DEVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -1;
FUJITA Tomonori21a61822008-03-09 13:44:30 +0900912
913 return scsi_sg_copy_to_buffer(scp, arr, arr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916
917static const char * inq_vendor_id = "Linux ";
918static const char * inq_product_id = "scsi_debug ";
Douglas Gilbert773642d2016-04-25 12:16:28 -0400919static const char *inq_product_rev = "0186"; /* version less '.' */
920static const u64 naa5_comp_a = 0x5222222000000000ULL;
921static const u64 naa5_comp_b = 0x5333333000000000ULL;
922static const u64 naa5_comp_c = 0x5111111000000000ULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Douglas Gilbertcbf67842014-07-26 11:55:35 -0400924/* Device identification VPD page. Returns number of bytes placed in arr */
Hannes Reinecke5a09e392006-10-20 09:58:47 +0200925static int inquiry_evpd_83(unsigned char * arr, int port_group_id,
926 int target_dev_id, int dev_id_num,
927 const char * dev_id_str,
Douglas Gilbertc65b1442006-06-06 00:11:24 -0400928 int dev_id_str_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Douglas Gilbertc65b1442006-06-06 00:11:24 -0400930 int num, port_a;
931 char b[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Douglas Gilbertc65b1442006-06-06 00:11:24 -0400933 port_a = target_dev_id + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /* T10 vendor identifier field format (faked) */
935 arr[0] = 0x2; /* ASCII */
936 arr[1] = 0x1;
937 arr[2] = 0x0;
938 memcpy(&arr[4], inq_vendor_id, 8);
939 memcpy(&arr[12], inq_product_id, 16);
940 memcpy(&arr[28], dev_id_str, dev_id_str_len);
941 num = 8 + 16 + dev_id_str_len;
942 arr[3] = num;
943 num += 4;
Douglas Gilbertc65b1442006-06-06 00:11:24 -0400944 if (dev_id_num >= 0) {
945 /* NAA-5, Logical unit identifier (binary) */
946 arr[num++] = 0x1; /* binary (not necessarily sas) */
947 arr[num++] = 0x3; /* PIV=0, lu, naa */
948 arr[num++] = 0x0;
949 arr[num++] = 0x8;
Douglas Gilbert773642d2016-04-25 12:16:28 -0400950 put_unaligned_be64(naa5_comp_b + dev_id_num, arr + num);
951 num += 8;
Douglas Gilbertc65b1442006-06-06 00:11:24 -0400952 /* Target relative port number */
953 arr[num++] = 0x61; /* proto=sas, binary */
954 arr[num++] = 0x94; /* PIV=1, target port, rel port */
955 arr[num++] = 0x0; /* reserved */
956 arr[num++] = 0x4; /* length */
957 arr[num++] = 0x0; /* reserved */
958 arr[num++] = 0x0; /* reserved */
959 arr[num++] = 0x0;
960 arr[num++] = 0x1; /* relative port A */
961 }
962 /* NAA-5, Target port identifier */
963 arr[num++] = 0x61; /* proto=sas, binary */
964 arr[num++] = 0x93; /* piv=1, target port, naa */
965 arr[num++] = 0x0;
966 arr[num++] = 0x8;
Douglas Gilbert773642d2016-04-25 12:16:28 -0400967 put_unaligned_be64(naa5_comp_a + port_a, arr + num);
968 num += 8;
Hannes Reinecke5a09e392006-10-20 09:58:47 +0200969 /* NAA-5, Target port group identifier */
970 arr[num++] = 0x61; /* proto=sas, binary */
971 arr[num++] = 0x95; /* piv=1, target port group id */
972 arr[num++] = 0x0;
973 arr[num++] = 0x4;
974 arr[num++] = 0;
975 arr[num++] = 0;
Douglas Gilbert773642d2016-04-25 12:16:28 -0400976 put_unaligned_be16(port_group_id, arr + num);
977 num += 2;
Douglas Gilbertc65b1442006-06-06 00:11:24 -0400978 /* NAA-5, Target device identifier */
979 arr[num++] = 0x61; /* proto=sas, binary */
980 arr[num++] = 0xa3; /* piv=1, target device, naa */
981 arr[num++] = 0x0;
982 arr[num++] = 0x8;
Douglas Gilbert773642d2016-04-25 12:16:28 -0400983 put_unaligned_be64(naa5_comp_a + target_dev_id, arr + num);
984 num += 8;
Douglas Gilbertc65b1442006-06-06 00:11:24 -0400985 /* SCSI name string: Target device identifier */
986 arr[num++] = 0x63; /* proto=sas, UTF-8 */
987 arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */
988 arr[num++] = 0x0;
989 arr[num++] = 24;
990 memcpy(arr + num, "naa.52222220", 12);
991 num += 12;
992 snprintf(b, sizeof(b), "%08X", target_dev_id);
993 memcpy(arr + num, b, 8);
994 num += 8;
995 memset(arr + num, 0, 4);
996 num += 4;
997 return num;
998}
999
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001000static unsigned char vpd84_data[] = {
1001/* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
1002 0x22,0x22,0x22,0x0,0xbb,0x1,
1003 0x22,0x22,0x22,0x0,0xbb,0x2,
1004};
1005
Douglas Gilbertcbf67842014-07-26 11:55:35 -04001006/* Software interface identification VPD page */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001007static int inquiry_evpd_84(unsigned char * arr)
1008{
1009 memcpy(arr, vpd84_data, sizeof(vpd84_data));
1010 return sizeof(vpd84_data);
1011}
1012
Douglas Gilbertcbf67842014-07-26 11:55:35 -04001013/* Management network addresses VPD page */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001014static int inquiry_evpd_85(unsigned char * arr)
1015{
1016 int num = 0;
1017 const char * na1 = "https://www.kernel.org/config";
1018 const char * na2 = "http://www.kernel.org/log";
1019 int plen, olen;
1020
1021 arr[num++] = 0x1; /* lu, storage config */
1022 arr[num++] = 0x0; /* reserved */
1023 arr[num++] = 0x0;
1024 olen = strlen(na1);
1025 plen = olen + 1;
1026 if (plen % 4)
1027 plen = ((plen / 4) + 1) * 4;
1028 arr[num++] = plen; /* length, null termianted, padded */
1029 memcpy(arr + num, na1, olen);
1030 memset(arr + num + olen, 0, plen - olen);
1031 num += plen;
1032
1033 arr[num++] = 0x4; /* lu, logging */
1034 arr[num++] = 0x0; /* reserved */
1035 arr[num++] = 0x0;
1036 olen = strlen(na2);
1037 plen = olen + 1;
1038 if (plen % 4)
1039 plen = ((plen / 4) + 1) * 4;
1040 arr[num++] = plen; /* length, null terminated, padded */
1041 memcpy(arr + num, na2, olen);
1042 memset(arr + num + olen, 0, plen - olen);
1043 num += plen;
1044
1045 return num;
1046}
1047
1048/* SCSI ports VPD page */
1049static int inquiry_evpd_88(unsigned char * arr, int target_dev_id)
1050{
1051 int num = 0;
1052 int port_a, port_b;
1053
1054 port_a = target_dev_id + 1;
1055 port_b = port_a + 1;
1056 arr[num++] = 0x0; /* reserved */
1057 arr[num++] = 0x0; /* reserved */
1058 arr[num++] = 0x0;
1059 arr[num++] = 0x1; /* relative port 1 (primary) */
1060 memset(arr + num, 0, 6);
1061 num += 6;
1062 arr[num++] = 0x0;
1063 arr[num++] = 12; /* length tp descriptor */
1064 /* naa-5 target port identifier (A) */
1065 arr[num++] = 0x61; /* proto=sas, binary */
1066 arr[num++] = 0x93; /* PIV=1, target port, NAA */
1067 arr[num++] = 0x0; /* reserved */
1068 arr[num++] = 0x8; /* length */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001069 put_unaligned_be64(naa5_comp_a + port_a, arr + num);
1070 num += 8;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001071 arr[num++] = 0x0; /* reserved */
1072 arr[num++] = 0x0; /* reserved */
1073 arr[num++] = 0x0;
1074 arr[num++] = 0x2; /* relative port 2 (secondary) */
1075 memset(arr + num, 0, 6);
1076 num += 6;
1077 arr[num++] = 0x0;
1078 arr[num++] = 12; /* length tp descriptor */
1079 /* naa-5 target port identifier (B) */
1080 arr[num++] = 0x61; /* proto=sas, binary */
1081 arr[num++] = 0x93; /* PIV=1, target port, NAA */
1082 arr[num++] = 0x0; /* reserved */
1083 arr[num++] = 0x8; /* length */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001084 put_unaligned_be64(naa5_comp_a + port_b, arr + num);
1085 num += 8;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001086
1087 return num;
1088}
1089
1090
1091static unsigned char vpd89_data[] = {
1092/* from 4th byte */ 0,0,0,0,
1093'l','i','n','u','x',' ',' ',' ',
1094'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
1095'1','2','3','4',
10960x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
10970xec,0,0,0,
10980x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
10990,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
11000x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
11010x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
11020x53,0x41,
11030x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
11040x20,0x20,
11050x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
11060x10,0x80,
11070,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
11080x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
11090x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
11100,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
11110x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
11120x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
11130,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,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,
11170x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
11180,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
11190xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
11200,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,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,0,0,
11230,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11240,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11250,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11260,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11270,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11280,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11290,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11300,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11310,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
11320,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
1133};
1134
Douglas Gilbertcbf67842014-07-26 11:55:35 -04001135/* ATA Information VPD page */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001136static int inquiry_evpd_89(unsigned char * arr)
1137{
1138 memcpy(arr, vpd89_data, sizeof(vpd89_data));
1139 return sizeof(vpd89_data);
1140}
1141
1142
1143static unsigned char vpdb0_data[] = {
Douglas Gilbert1e49f782009-10-29 01:48:31 -04001144 /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64,
1145 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1146 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1147 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001148};
1149
Douglas Gilbertcbf67842014-07-26 11:55:35 -04001150/* Block limits VPD page (SBC-3) */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001151static int inquiry_evpd_b0(unsigned char * arr)
1152{
Martin K. Petersenea61fca2009-05-15 00:40:33 -04001153 unsigned int gran;
1154
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001155 memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
Martin K. Petersene308b3d2010-03-23 01:12:27 -04001156
1157 /* Optimal transfer length granularity */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001158 gran = 1 << sdebug_physblk_exp;
1159 put_unaligned_be16(gran, arr + 2);
Martin K. Petersene308b3d2010-03-23 01:12:27 -04001160
1161 /* Maximum Transfer Length */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001162 if (sdebug_store_sectors > 0x400)
1163 put_unaligned_be32(sdebug_store_sectors, arr + 4);
Martin K. Petersen44d92692009-10-15 14:45:27 -04001164
Martin K. Petersene308b3d2010-03-23 01:12:27 -04001165 /* Optimal Transfer Length */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001166 put_unaligned_be32(sdebug_opt_blks, &arr[8]);
Martin K. Petersene308b3d2010-03-23 01:12:27 -04001167
Douglas Gilbert773642d2016-04-25 12:16:28 -04001168 if (sdebug_lbpu) {
Martin K. Petersene308b3d2010-03-23 01:12:27 -04001169 /* Maximum Unmap LBA Count */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001170 put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]);
Martin K. Petersene308b3d2010-03-23 01:12:27 -04001171
1172 /* Maximum Unmap Block Descriptor Count */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001173 put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]);
Martin K. Petersen44d92692009-10-15 14:45:27 -04001174 }
1175
Martin K. Petersene308b3d2010-03-23 01:12:27 -04001176 /* Unmap Granularity Alignment */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001177 if (sdebug_unmap_alignment) {
1178 put_unaligned_be32(sdebug_unmap_alignment, &arr[28]);
Martin K. Petersen44d92692009-10-15 14:45:27 -04001179 arr[28] |= 0x80; /* UGAVALID */
1180 }
1181
Martin K. Petersene308b3d2010-03-23 01:12:27 -04001182 /* Optimal Unmap Granularity */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001183 put_unaligned_be32(sdebug_unmap_granularity, &arr[24]);
Martin K. Petersen60147592010-08-19 11:49:00 -04001184
Martin K. Petersen5b94e232011-03-08 02:08:11 -05001185 /* Maximum WRITE SAME Length */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001186 put_unaligned_be64(sdebug_write_same_length, &arr[32]);
Martin K. Petersen5b94e232011-03-08 02:08:11 -05001187
1188 return 0x3c; /* Mandatory page length for Logical Block Provisioning */
Martin K. Petersen44d92692009-10-15 14:45:27 -04001189
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001190 return sizeof(vpdb0_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
Douglas Gilbert1e49f782009-10-29 01:48:31 -04001193/* Block device characteristics VPD page (SBC-3) */
Matthew Wilcoxeac6e8e42008-06-19 10:02:58 -06001194static int inquiry_evpd_b1(unsigned char *arr)
1195{
1196 memset(arr, 0, 0x3c);
1197 arr[0] = 0;
Douglas Gilbert1e49f782009-10-29 01:48:31 -04001198 arr[1] = 1; /* non rotating medium (e.g. solid state) */
1199 arr[2] = 0;
1200 arr[3] = 5; /* less than 1.8" */
Matthew Wilcoxeac6e8e42008-06-19 10:02:58 -06001201
1202 return 0x3c;
1203}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Eric Sandeenbe1dd782012-03-08 00:03:59 -06001205/* Logical block provisioning VPD page (SBC-3) */
Martin K. Petersen60147592010-08-19 11:49:00 -04001206static int inquiry_evpd_b2(unsigned char *arr)
1207{
Martin K. Petersen3f0bc3b2012-03-08 10:48:29 -05001208 memset(arr, 0, 0x4);
Martin K. Petersen60147592010-08-19 11:49:00 -04001209 arr[0] = 0; /* threshold exponent */
1210
Douglas Gilbert773642d2016-04-25 12:16:28 -04001211 if (sdebug_lbpu)
Martin K. Petersen60147592010-08-19 11:49:00 -04001212 arr[1] = 1 << 7;
1213
Douglas Gilbert773642d2016-04-25 12:16:28 -04001214 if (sdebug_lbpws)
Martin K. Petersen60147592010-08-19 11:49:00 -04001215 arr[1] |= 1 << 6;
1216
Douglas Gilbert773642d2016-04-25 12:16:28 -04001217 if (sdebug_lbpws10)
Martin K. Petersen5b94e232011-03-08 02:08:11 -05001218 arr[1] |= 1 << 5;
1219
Douglas Gilbert773642d2016-04-25 12:16:28 -04001220 if (sdebug_lbprz)
Eric Sandeenbe1dd782012-03-08 00:03:59 -06001221 arr[1] |= 1 << 2;
1222
Martin K. Petersen3f0bc3b2012-03-08 10:48:29 -05001223 return 0x4;
Martin K. Petersen60147592010-08-19 11:49:00 -04001224}
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226#define SDEBUG_LONG_INQ_SZ 96
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001227#define SDEBUG_MAX_INQ_ARR_SZ 584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05001229static int resp_inquiry(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 unsigned char pq_pdt;
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001232 unsigned char * arr;
Douglas Gilbert01123ef2014-08-05 12:20:02 +02001233 unsigned char *cmd = scp->cmnd;
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001234 int alloc_len, n, ret;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05001235 bool have_wlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Douglas Gilbert773642d2016-04-25 12:16:28 -04001237 alloc_len = get_unaligned_be16(cmd + 3);
Douglas Gilbert6f3cbf52007-01-05 00:05:25 -05001238 arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC);
1239 if (! arr)
1240 return DID_REQUEUE << 16;
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04001241 have_wlun = scsi_is_wlun(scp->device->lun);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05001242 if (have_wlun)
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04001243 pq_pdt = TYPE_WLUN; /* present, wlun */
1244 else if (sdebug_no_lun_0 && (devip->lun == SDEBUG_LUN_0_VAL))
1245 pq_pdt = 0x7f; /* not present, PQ=3, PDT=0x1f */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001246 else
Douglas Gilbert773642d2016-04-25 12:16:28 -04001247 pq_pdt = (sdebug_ptype & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 arr[0] = pq_pdt;
1249 if (0x2 & cmd[1]) { /* CMDDT bit set */
Douglas Gilbert22017ed2014-11-24 23:04:47 -05001250 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1);
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001251 kfree(arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return check_condition_result;
1253 } else if (0x1 & cmd[1]) { /* EVPD bit set */
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001254 int lu_id_num, port_group_id, target_dev_id, len;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001255 char lu_id_str[6];
1256 int host_no = devip->sdbg_host->shost->host_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001258 port_group_id = (((host_no + 1) & 0x7f) << 8) +
1259 (devip->channel & 0x7f);
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04001260 if (sdebug_vpd_use_hostno == 0)
Douglas Gilbert23183912006-09-16 20:30:47 -04001261 host_no = 0;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05001262 lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) +
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001263 (devip->target * 1000) + devip->lun);
1264 target_dev_id = ((host_no + 1) * 2000) +
1265 (devip->target * 1000) - 3;
1266 len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (0 == cmd[2]) { /* supported vital product data pages */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001268 arr[1] = cmd[2]; /*sanity */
1269 n = 4;
1270 arr[n++] = 0x0; /* this page */
1271 arr[n++] = 0x80; /* unit serial number */
1272 arr[n++] = 0x83; /* device identification */
1273 arr[n++] = 0x84; /* software interface ident. */
1274 arr[n++] = 0x85; /* management network addresses */
1275 arr[n++] = 0x86; /* extended inquiry */
1276 arr[n++] = 0x87; /* mode page policy */
1277 arr[n++] = 0x88; /* SCSI ports */
1278 arr[n++] = 0x89; /* ATA information */
1279 arr[n++] = 0xb0; /* Block limits (SBC) */
Matthew Wilcoxeac6e8e42008-06-19 10:02:58 -06001280 arr[n++] = 0xb1; /* Block characteristics (SBC) */
Martin K. Petersen5b94e232011-03-08 02:08:11 -05001281 if (scsi_debug_lbp()) /* Logical Block Prov. (SBC) */
1282 arr[n++] = 0xb2;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001283 arr[3] = n - 4; /* number of supported VPD pages */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 } else if (0x80 == cmd[2]) { /* unit serial number */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001285 arr[1] = cmd[2]; /*sanity */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 arr[3] = len;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001287 memcpy(&arr[4], lu_id_str, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 } else if (0x83 == cmd[2]) { /* device identification */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001289 arr[1] = cmd[2]; /*sanity */
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001290 arr[3] = inquiry_evpd_83(&arr[4], port_group_id,
1291 target_dev_id, lu_id_num,
1292 lu_id_str, len);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001293 } else if (0x84 == cmd[2]) { /* Software interface ident. */
1294 arr[1] = cmd[2]; /*sanity */
1295 arr[3] = inquiry_evpd_84(&arr[4]);
1296 } else if (0x85 == cmd[2]) { /* Management network addresses */
1297 arr[1] = cmd[2]; /*sanity */
1298 arr[3] = inquiry_evpd_85(&arr[4]);
1299 } else if (0x86 == cmd[2]) { /* extended inquiry */
1300 arr[1] = cmd[2]; /*sanity */
1301 arr[3] = 0x3c; /* number of following entries */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001302 if (sdebug_dif == SD_DIF_TYPE3_PROTECTION)
Martin K. Petersenc6a44282009-01-04 03:08:19 -05001303 arr[4] = 0x4; /* SPT: GRD_CHK:1 */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001304 else if (sdebug_dif)
Martin K. Petersenc6a44282009-01-04 03:08:19 -05001305 arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */
1306 else
1307 arr[4] = 0x0; /* no protection stuff */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001308 arr[5] = 0x7; /* head of q, ordered + simple q's */
1309 } else if (0x87 == cmd[2]) { /* mode page policy */
1310 arr[1] = cmd[2]; /*sanity */
1311 arr[3] = 0x8; /* number of following entries */
1312 arr[4] = 0x2; /* disconnect-reconnect mp */
1313 arr[6] = 0x80; /* mlus, shared */
1314 arr[8] = 0x18; /* protocol specific lu */
1315 arr[10] = 0x82; /* mlus, per initiator port */
1316 } else if (0x88 == cmd[2]) { /* SCSI Ports */
1317 arr[1] = cmd[2]; /*sanity */
1318 arr[3] = inquiry_evpd_88(&arr[4], target_dev_id);
1319 } else if (0x89 == cmd[2]) { /* ATA information */
1320 arr[1] = cmd[2]; /*sanity */
1321 n = inquiry_evpd_89(&arr[4]);
Douglas Gilbert773642d2016-04-25 12:16:28 -04001322 put_unaligned_be16(n, arr + 2);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001323 } else if (0xb0 == cmd[2]) { /* Block limits (SBC) */
1324 arr[1] = cmd[2]; /*sanity */
1325 arr[3] = inquiry_evpd_b0(&arr[4]);
Matthew Wilcoxeac6e8e42008-06-19 10:02:58 -06001326 } else if (0xb1 == cmd[2]) { /* Block characteristics (SBC) */
1327 arr[1] = cmd[2]; /*sanity */
1328 arr[3] = inquiry_evpd_b1(&arr[4]);
Martin K. Petersen5b94e232011-03-08 02:08:11 -05001329 } else if (0xb2 == cmd[2]) { /* Logical Block Prov. (SBC) */
Martin K. Petersen60147592010-08-19 11:49:00 -04001330 arr[1] = cmd[2]; /*sanity */
1331 arr[3] = inquiry_evpd_b2(&arr[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 } else {
Douglas Gilbert22017ed2014-11-24 23:04:47 -05001333 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001334 kfree(arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return check_condition_result;
1336 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04001337 len = min(get_unaligned_be16(arr + 2) + 4, alloc_len);
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001338 ret = fill_from_dev_buffer(scp, arr,
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001339 min(len, SDEBUG_MAX_INQ_ARR_SZ));
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001340 kfree(arr);
1341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343 /* drops through here for a standard inquiry */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001344 arr[1] = sdebug_removable ? 0x80 : 0; /* Removable disk */
1345 arr[2] = sdebug_scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 arr[3] = 2; /* response_data_format==2 */
1347 arr[4] = SDEBUG_LONG_INQ_SZ - 5;
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04001348 arr[5] = (int)have_dif_prot; /* PROTECT bit */
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04001349 if (sdebug_vpd_use_hostno == 0)
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001350 arr[5] = 0x10; /* claim: implicit TGPS */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001351 arr[6] = 0x10; /* claim: MultiP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001353 arr[7] = 0xa; /* claim: LINKED + CMDQUE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 memcpy(&arr[8], inq_vendor_id, 8);
1355 memcpy(&arr[16], inq_product_id, 16);
1356 memcpy(&arr[32], inq_product_rev, 4);
1357 /* version descriptors (2 bytes each) follow */
Douglas Gilberte46b0342014-08-05 12:21:53 +02001358 arr[58] = 0x0; arr[59] = 0xa2; /* SAM-5 rev 4 */
1359 arr[60] = 0x4; arr[61] = 0x68; /* SPC-4 rev 37 */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001360 n = 62;
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04001361 if (sdebug_ptype == TYPE_DISK) {
Douglas Gilberte46b0342014-08-05 12:21:53 +02001362 arr[n++] = 0x4; arr[n++] = 0xc5; /* SBC-4 rev 36 */
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04001363 } else if (sdebug_ptype == TYPE_TAPE) {
Douglas Gilberte46b0342014-08-05 12:21:53 +02001364 arr[n++] = 0x5; arr[n++] = 0x25; /* SSC-4 rev 3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Douglas Gilberte46b0342014-08-05 12:21:53 +02001366 arr[n++] = 0x20; arr[n++] = 0xe6; /* SPL-3 rev 7 */
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001367 ret = fill_from_dev_buffer(scp, arr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 min(alloc_len, SDEBUG_LONG_INQ_SZ));
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001369 kfree(arr);
1370 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Douglas Gilbertfd321192016-04-25 12:16:33 -04001373static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
1374 0, 0, 0x0, 0x0};
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376static int resp_requests(struct scsi_cmnd * scp,
1377 struct sdebug_dev_info * devip)
1378{
1379 unsigned char * sbuff;
Douglas Gilbert01123ef2014-08-05 12:20:02 +02001380 unsigned char *cmd = scp->cmnd;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04001381 unsigned char arr[SCSI_SENSE_BUFFERSIZE];
Tomas Winkler2492fc02015-07-28 16:54:26 +03001382 bool dsense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 int len = 18;
1384
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001385 memset(arr, 0, sizeof(arr));
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05001386 dsense = !!(cmd[1] & 1);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04001387 sbuff = scp->sense_buffer;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001388 if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05001389 if (dsense) {
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001390 arr[0] = 0x72;
1391 arr[1] = 0x0; /* NO_SENSE in sense_key */
1392 arr[2] = THRESHOLD_EXCEEDED;
1393 arr[3] = 0xff; /* TEST set and MRIE==6 */
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05001394 len = 8;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001395 } else {
1396 arr[0] = 0x70;
1397 arr[2] = 0x0; /* NO_SENSE in sense_key */
1398 arr[7] = 0xa; /* 18 byte sense buffer */
1399 arr[12] = THRESHOLD_EXCEEDED;
1400 arr[13] = 0xff; /* TEST set and MRIE==6 */
1401 }
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001402 } else {
Douglas Gilbertcbf67842014-07-26 11:55:35 -04001403 memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE);
Douglas Gilbert773642d2016-04-25 12:16:28 -04001404 if (arr[0] >= 0x70 && dsense == sdebug_dsense)
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05001405 ; /* have sense and formats match */
1406 else if (arr[0] <= 0x70) {
1407 if (dsense) {
1408 memset(arr, 0, 8);
1409 arr[0] = 0x72;
1410 len = 8;
1411 } else {
1412 memset(arr, 0, 18);
1413 arr[0] = 0x70;
1414 arr[7] = 0xa;
1415 }
1416 } else if (dsense) {
1417 memset(arr, 0, 8);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001418 arr[0] = 0x72;
1419 arr[1] = sbuff[2]; /* sense key */
1420 arr[2] = sbuff[12]; /* asc */
1421 arr[3] = sbuff[13]; /* ascq */
1422 len = 8;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05001423 } else {
1424 memset(arr, 0, 18);
1425 arr[0] = 0x70;
1426 arr[2] = sbuff[1];
1427 arr[7] = 0xa;
1428 arr[12] = sbuff[1];
1429 arr[13] = sbuff[3];
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001430 }
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05001431
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001432 }
Douglas Gilbertcbf67842014-07-26 11:55:35 -04001433 mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return fill_from_dev_buffer(scp, arr, len);
1435}
1436
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001437static int resp_start_stop(struct scsi_cmnd * scp,
1438 struct sdebug_dev_info * devip)
1439{
Douglas Gilbert01123ef2014-08-05 12:20:02 +02001440 unsigned char *cmd = scp->cmnd;
Douglas Gilbertc4837392016-05-06 00:40:26 -04001441 int power_cond, stop;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001442
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001443 power_cond = (cmd[4] & 0xf0) >> 4;
1444 if (power_cond) {
Douglas Gilbert22017ed2014-11-24 23:04:47 -05001445 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001446 return check_condition_result;
1447 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04001448 stop = !(cmd[4] & 1);
1449 atomic_xchg(&devip->stopped, stop);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001450 return 0;
1451}
1452
FUJITA Tomonori28898872008-03-30 00:59:55 +09001453static sector_t get_sdebug_capacity(void)
1454{
Douglas Gilbert773642d2016-04-25 12:16:28 -04001455 static const unsigned int gibibyte = 1073741824;
1456
1457 if (sdebug_virtual_gb > 0)
1458 return (sector_t)sdebug_virtual_gb *
1459 (gibibyte / sdebug_sector_size);
FUJITA Tomonori28898872008-03-30 00:59:55 +09001460 else
1461 return sdebug_store_sectors;
1462}
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464#define SDEBUG_READCAP_ARR_SZ 8
1465static int resp_readcap(struct scsi_cmnd * scp,
1466 struct sdebug_dev_info * devip)
1467{
1468 unsigned char arr[SDEBUG_READCAP_ARR_SZ];
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001469 unsigned int capac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001471 /* following just in case virtual_gb changed */
FUJITA Tomonori28898872008-03-30 00:59:55 +09001472 sdebug_capacity = get_sdebug_capacity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001474 if (sdebug_capacity < 0xffffffff) {
1475 capac = (unsigned int)sdebug_capacity - 1;
Douglas Gilbert773642d2016-04-25 12:16:28 -04001476 put_unaligned_be32(capac, arr + 0);
1477 } else
1478 put_unaligned_be32(0xffffffff, arr + 0);
1479 put_unaligned_be16(sdebug_sector_size, arr + 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
1481}
1482
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001483#define SDEBUG_READCAP16_ARR_SZ 32
1484static int resp_readcap16(struct scsi_cmnd * scp,
1485 struct sdebug_dev_info * devip)
1486{
Douglas Gilbert01123ef2014-08-05 12:20:02 +02001487 unsigned char *cmd = scp->cmnd;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001488 unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
Douglas Gilbert773642d2016-04-25 12:16:28 -04001489 int alloc_len;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001490
Douglas Gilbert773642d2016-04-25 12:16:28 -04001491 alloc_len = get_unaligned_be32(cmd + 10);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001492 /* following just in case virtual_gb changed */
FUJITA Tomonori28898872008-03-30 00:59:55 +09001493 sdebug_capacity = get_sdebug_capacity();
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001494 memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
Douglas Gilbert773642d2016-04-25 12:16:28 -04001495 put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0);
1496 put_unaligned_be32(sdebug_sector_size, arr + 8);
1497 arr[13] = sdebug_physblk_exp & 0xf;
1498 arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f;
Martin K. Petersen44d92692009-10-15 14:45:27 -04001499
Eric Sandeenbe1dd782012-03-08 00:03:59 -06001500 if (scsi_debug_lbp()) {
Martin K. Petersen5b94e232011-03-08 02:08:11 -05001501 arr[14] |= 0x80; /* LBPME */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001502 if (sdebug_lbprz)
Eric Sandeenbe1dd782012-03-08 00:03:59 -06001503 arr[14] |= 0x40; /* LBPRZ */
1504 }
Martin K. Petersen44d92692009-10-15 14:45:27 -04001505
Douglas Gilbert773642d2016-04-25 12:16:28 -04001506 arr[15] = sdebug_lowest_aligned & 0xff;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05001507
Douglas Gilbert773642d2016-04-25 12:16:28 -04001508 if (sdebug_dif) {
1509 arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */
Martin K. Petersenc6a44282009-01-04 03:08:19 -05001510 arr[12] |= 1; /* PROT_EN */
1511 }
1512
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001513 return fill_from_dev_buffer(scp, arr,
1514 min(alloc_len, SDEBUG_READCAP16_ARR_SZ));
1515}
1516
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001517#define SDEBUG_MAX_TGTPGS_ARR_SZ 1412
1518
1519static int resp_report_tgtpgs(struct scsi_cmnd * scp,
1520 struct sdebug_dev_info * devip)
1521{
Douglas Gilbert01123ef2014-08-05 12:20:02 +02001522 unsigned char *cmd = scp->cmnd;
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001523 unsigned char * arr;
1524 int host_no = devip->sdbg_host->shost->host_no;
1525 int n, ret, alen, rlen;
1526 int port_group_a, port_group_b, port_a, port_b;
1527
Douglas Gilbert773642d2016-04-25 12:16:28 -04001528 alen = get_unaligned_be32(cmd + 6);
Douglas Gilbert6f3cbf52007-01-05 00:05:25 -05001529 arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC);
1530 if (! arr)
1531 return DID_REQUEUE << 16;
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001532 /*
1533 * EVPD page 0x88 states we have two ports, one
1534 * real and a fake port with no device connected.
1535 * So we create two port groups with one port each
1536 * and set the group with port B to unavailable.
1537 */
1538 port_a = 0x1; /* relative port A */
1539 port_b = 0x2; /* relative port B */
1540 port_group_a = (((host_no + 1) & 0x7f) << 8) +
Douglas Gilbert773642d2016-04-25 12:16:28 -04001541 (devip->channel & 0x7f);
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001542 port_group_b = (((host_no + 1) & 0x7f) << 8) +
Douglas Gilbert773642d2016-04-25 12:16:28 -04001543 (devip->channel & 0x7f) + 0x80;
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001544
1545 /*
1546 * The asymmetric access state is cycled according to the host_id.
1547 */
1548 n = 4;
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04001549 if (sdebug_vpd_use_hostno == 0) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04001550 arr[n++] = host_no % 3; /* Asymm access state */
1551 arr[n++] = 0x0F; /* claim: all states are supported */
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001552 } else {
Douglas Gilbert773642d2016-04-25 12:16:28 -04001553 arr[n++] = 0x0; /* Active/Optimized path */
1554 arr[n++] = 0x01; /* only support active/optimized paths */
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001555 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04001556 put_unaligned_be16(port_group_a, arr + n);
1557 n += 2;
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001558 arr[n++] = 0; /* Reserved */
1559 arr[n++] = 0; /* Status code */
1560 arr[n++] = 0; /* Vendor unique */
1561 arr[n++] = 0x1; /* One port per group */
1562 arr[n++] = 0; /* Reserved */
1563 arr[n++] = 0; /* Reserved */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001564 put_unaligned_be16(port_a, arr + n);
1565 n += 2;
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001566 arr[n++] = 3; /* Port unavailable */
1567 arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001568 put_unaligned_be16(port_group_b, arr + n);
1569 n += 2;
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001570 arr[n++] = 0; /* Reserved */
1571 arr[n++] = 0; /* Status code */
1572 arr[n++] = 0; /* Vendor unique */
1573 arr[n++] = 0x1; /* One port per group */
1574 arr[n++] = 0; /* Reserved */
1575 arr[n++] = 0; /* Reserved */
Douglas Gilbert773642d2016-04-25 12:16:28 -04001576 put_unaligned_be16(port_b, arr + n);
1577 n += 2;
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001578
1579 rlen = n - 4;
Douglas Gilbert773642d2016-04-25 12:16:28 -04001580 put_unaligned_be32(rlen, arr + 0);
Hannes Reinecke5a09e392006-10-20 09:58:47 +02001581
1582 /*
1583 * Return the smallest value of either
1584 * - The allocated length
1585 * - The constructed command length
1586 * - The maximum array size
1587 */
1588 rlen = min(alen,n);
1589 ret = fill_from_dev_buffer(scp, arr,
1590 min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ));
1591 kfree(arr);
1592 return ret;
1593}
1594
Douglas Gilbertfd321192016-04-25 12:16:33 -04001595static int resp_rsup_opcodes(struct scsi_cmnd *scp,
1596 struct sdebug_dev_info *devip)
Douglas Gilbert38d5c832014-11-24 21:27:12 -05001597{
1598 bool rctd;
1599 u8 reporting_opts, req_opcode, sdeb_i, supp;
1600 u16 req_sa, u;
1601 u32 alloc_len, a_len;
1602 int k, offset, len, errsts, count, bump, na;
1603 const struct opcode_info_t *oip;
1604 const struct opcode_info_t *r_oip;
1605 u8 *arr;
1606 u8 *cmd = scp->cmnd;
1607
1608 rctd = !!(cmd[2] & 0x80);
1609 reporting_opts = cmd[2] & 0x7;
1610 req_opcode = cmd[3];
1611 req_sa = get_unaligned_be16(cmd + 4);
1612 alloc_len = get_unaligned_be32(cmd + 6);
Colin Ian King6d310df2015-01-22 11:20:40 +00001613 if (alloc_len < 4 || alloc_len > 0xffff) {
Douglas Gilbert38d5c832014-11-24 21:27:12 -05001614 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
1615 return check_condition_result;
1616 }
1617 if (alloc_len > 8192)
1618 a_len = 8192;
1619 else
1620 a_len = alloc_len;
Sasha Levin99531e62015-01-17 17:47:37 -05001621 arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC);
Douglas Gilbert38d5c832014-11-24 21:27:12 -05001622 if (NULL == arr) {
1623 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
1624 INSUFF_RES_ASCQ);
1625 return check_condition_result;
1626 }
1627 switch (reporting_opts) {
1628 case 0: /* all commands */
1629 /* count number of commands */
1630 for (count = 0, oip = opcode_info_arr;
1631 oip->num_attached != 0xff; ++oip) {
1632 if (F_INV_OP & oip->flags)
1633 continue;
1634 count += (oip->num_attached + 1);
1635 }
1636 bump = rctd ? 20 : 8;
1637 put_unaligned_be32(count * bump, arr);
1638 for (offset = 4, oip = opcode_info_arr;
1639 oip->num_attached != 0xff && offset < a_len; ++oip) {
1640 if (F_INV_OP & oip->flags)
1641 continue;
1642 na = oip->num_attached;
1643 arr[offset] = oip->opcode;
1644 put_unaligned_be16(oip->sa, arr + offset + 2);
1645 if (rctd)
1646 arr[offset + 5] |= 0x2;
1647 if (FF_SA & oip->flags)
1648 arr[offset + 5] |= 0x1;
1649 put_unaligned_be16(oip->len_mask[0], arr + offset + 6);
1650 if (rctd)
1651 put_unaligned_be16(0xa, arr + offset + 8);
1652 r_oip = oip;
1653 for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) {
1654 if (F_INV_OP & oip->flags)
1655 continue;
1656 offset += bump;
1657 arr[offset] = oip->opcode;
1658 put_unaligned_be16(oip->sa, arr + offset + 2);
1659 if (rctd)
1660 arr[offset + 5] |= 0x2;
1661 if (FF_SA & oip->flags)
1662 arr[offset + 5] |= 0x1;
1663 put_unaligned_be16(oip->len_mask[0],
1664 arr + offset + 6);
1665 if (rctd)
1666 put_unaligned_be16(0xa,
1667 arr + offset + 8);
1668 }
1669 oip = r_oip;
1670 offset += bump;
1671 }
1672 break;
1673 case 1: /* one command: opcode only */
1674 case 2: /* one command: opcode plus service action */
1675 case 3: /* one command: if sa==0 then opcode only else opcode+sa */
1676 sdeb_i = opcode_ind_arr[req_opcode];
1677 oip = &opcode_info_arr[sdeb_i];
1678 if (F_INV_OP & oip->flags) {
1679 supp = 1;
1680 offset = 4;
1681 } else {
1682 if (1 == reporting_opts) {
1683 if (FF_SA & oip->flags) {
1684 mk_sense_invalid_fld(scp, SDEB_IN_CDB,
1685 2, 2);
1686 kfree(arr);
1687 return check_condition_result;
1688 }
1689 req_sa = 0;
1690 } else if (2 == reporting_opts &&
1691 0 == (FF_SA & oip->flags)) {
1692 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1);
1693 kfree(arr); /* point at requested sa */
1694 return check_condition_result;
1695 }
1696 if (0 == (FF_SA & oip->flags) &&
1697 req_opcode == oip->opcode)
1698 supp = 3;
1699 else if (0 == (FF_SA & oip->flags)) {
1700 na = oip->num_attached;
1701 for (k = 0, oip = oip->arrp; k < na;
1702 ++k, ++oip) {
1703 if (req_opcode == oip->opcode)
1704 break;
1705 }
1706 supp = (k >= na) ? 1 : 3;
1707 } else if (req_sa != oip->sa) {
1708 na = oip->num_attached;
1709 for (k = 0, oip = oip->arrp; k < na;
1710 ++k, ++oip) {
1711 if (req_sa == oip->sa)
1712 break;
1713 }
1714 supp = (k >= na) ? 1 : 3;
1715 } else
1716 supp = 3;
1717 if (3 == supp) {
1718 u = oip->len_mask[0];
1719 put_unaligned_be16(u, arr + 2);
1720 arr[4] = oip->opcode;
1721 for (k = 1; k < u; ++k)
1722 arr[4 + k] = (k < 16) ?
1723 oip->len_mask[k] : 0xff;
1724 offset = 4 + u;
1725 } else
1726 offset = 4;
1727 }
1728 arr[1] = (rctd ? 0x80 : 0) | supp;
1729 if (rctd) {
1730 put_unaligned_be16(0xa, arr + offset);
1731 offset += 12;
1732 }
1733 break;
1734 default:
1735 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2);
1736 kfree(arr);
1737 return check_condition_result;
1738 }
1739 offset = (offset < a_len) ? offset : a_len;
1740 len = (offset < alloc_len) ? offset : alloc_len;
1741 errsts = fill_from_dev_buffer(scp, arr, len);
1742 kfree(arr);
1743 return errsts;
1744}
1745
Douglas Gilbertfd321192016-04-25 12:16:33 -04001746static int resp_rsup_tmfs(struct scsi_cmnd *scp,
1747 struct sdebug_dev_info *devip)
Douglas Gilbert38d5c832014-11-24 21:27:12 -05001748{
1749 bool repd;
1750 u32 alloc_len, len;
1751 u8 arr[16];
1752 u8 *cmd = scp->cmnd;
1753
1754 memset(arr, 0, sizeof(arr));
1755 repd = !!(cmd[2] & 0x80);
1756 alloc_len = get_unaligned_be32(cmd + 6);
1757 if (alloc_len < 4) {
1758 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
1759 return check_condition_result;
1760 }
1761 arr[0] = 0xc8; /* ATS | ATSS | LURS */
1762 arr[1] = 0x1; /* ITNRS */
1763 if (repd) {
1764 arr[3] = 0xc;
1765 len = 16;
1766 } else
1767 len = 4;
1768
1769 len = (len < alloc_len) ? len : alloc_len;
1770 return fill_from_dev_buffer(scp, arr, len);
1771}
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773/* <<Following mode page info copied from ST318451LW>> */
1774
1775static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target)
1776{ /* Read-Write Error Recovery page for mode_sense */
1777 unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
1778 5, 0, 0xff, 0xff};
1779
1780 memcpy(p, err_recov_pg, sizeof(err_recov_pg));
1781 if (1 == pcontrol)
1782 memset(p + 2, 0, sizeof(err_recov_pg) - 2);
1783 return sizeof(err_recov_pg);
1784}
1785
1786static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target)
1787{ /* Disconnect-Reconnect page for mode_sense */
1788 unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
1789 0, 0, 0, 0, 0, 0, 0, 0};
1790
1791 memcpy(p, disconnect_pg, sizeof(disconnect_pg));
1792 if (1 == pcontrol)
1793 memset(p + 2, 0, sizeof(disconnect_pg) - 2);
1794 return sizeof(disconnect_pg);
1795}
1796
1797static int resp_format_pg(unsigned char * p, int pcontrol, int target)
1798{ /* Format device page for mode_sense */
Martin K. Petersen597136a2008-06-05 00:12:59 -04001799 unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
1800 0, 0, 0, 0, 0, 0, 0, 0,
1801 0, 0, 0, 0, 0x40, 0, 0, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Martin K. Petersen597136a2008-06-05 00:12:59 -04001803 memcpy(p, format_pg, sizeof(format_pg));
Douglas Gilbert773642d2016-04-25 12:16:28 -04001804 put_unaligned_be16(sdebug_sectors_per, p + 10);
1805 put_unaligned_be16(sdebug_sector_size, p + 12);
1806 if (sdebug_removable)
Martin K. Petersen597136a2008-06-05 00:12:59 -04001807 p[20] |= 0x20; /* should agree with INQUIRY */
1808 if (1 == pcontrol)
1809 memset(p + 2, 0, sizeof(format_pg) - 2);
1810 return sizeof(format_pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Douglas Gilbertfd321192016-04-25 12:16:33 -04001813static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
1814 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0,
1815 0, 0, 0, 0};
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817static int resp_caching_pg(unsigned char * p, int pcontrol, int target)
1818{ /* Caching page for mode_sense */
Douglas Gilbertcbf67842014-07-26 11:55:35 -04001819 unsigned char ch_caching_pg[] = {/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0,
1820 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1821 unsigned char d_caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
1823
Douglas Gilbert773642d2016-04-25 12:16:28 -04001824 if (SDEBUG_OPT_N_WCE & sdebug_opts)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04001825 caching_pg[2] &= ~0x4; /* set WCE=0 (default WCE=1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 memcpy(p, caching_pg, sizeof(caching_pg));
1827 if (1 == pcontrol)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04001828 memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg));
1829 else if (2 == pcontrol)
1830 memcpy(p, d_caching_pg, sizeof(d_caching_pg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return sizeof(caching_pg);
1832}
1833
Douglas Gilbertfd321192016-04-25 12:16:33 -04001834static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
1835 0, 0, 0x2, 0x4b};
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target)
1838{ /* Control mode page for mode_sense */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001839 unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
1840 0, 0, 0, 0};
1841 unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 0, 0, 0x2, 0x4b};
1843
Douglas Gilbert773642d2016-04-25 12:16:28 -04001844 if (sdebug_dsense)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 ctrl_m_pg[2] |= 0x4;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001846 else
1847 ctrl_m_pg[2] &= ~0x4;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05001848
Douglas Gilbert773642d2016-04-25 12:16:28 -04001849 if (sdebug_ato)
Martin K. Petersenc6a44282009-01-04 03:08:19 -05001850 ctrl_m_pg[5] |= 0x80; /* ATO=1 */
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
1853 if (1 == pcontrol)
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001854 memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
1855 else if (2 == pcontrol)
1856 memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return sizeof(ctrl_m_pg);
1858}
1859
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target)
1862{ /* Informational Exceptions control mode page for mode_sense */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001863 unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
1864 0, 0, 0x0, 0x0};
1865 unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
1866 0, 0, 0x0, 0x0};
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 memcpy(p, iec_m_pg, sizeof(iec_m_pg));
1869 if (1 == pcontrol)
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001870 memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
1871 else if (2 == pcontrol)
1872 memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return sizeof(iec_m_pg);
1874}
1875
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001876static int resp_sas_sf_m_pg(unsigned char * p, int pcontrol, int target)
1877{ /* SAS SSP mode page - short format for mode_sense */
1878 unsigned char sas_sf_m_pg[] = {0x19, 0x6,
1879 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0};
1880
1881 memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
1882 if (1 == pcontrol)
1883 memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
1884 return sizeof(sas_sf_m_pg);
1885}
1886
1887
1888static int resp_sas_pcd_m_spg(unsigned char * p, int pcontrol, int target,
1889 int target_dev_id)
1890{ /* SAS phy control and discover mode page for mode_sense */
1891 unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
1892 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
Douglas Gilbert773642d2016-04-25 12:16:28 -04001893 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
1894 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001895 0x2, 0, 0, 0, 0, 0, 0, 0,
1896 0x88, 0x99, 0, 0, 0, 0, 0, 0,
1897 0, 0, 0, 0, 0, 0, 0, 0,
1898 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
Douglas Gilbert773642d2016-04-25 12:16:28 -04001899 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
1900 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001901 0x3, 0, 0, 0, 0, 0, 0, 0,
1902 0x88, 0x99, 0, 0, 0, 0, 0, 0,
1903 0, 0, 0, 0, 0, 0, 0, 0,
1904 };
1905 int port_a, port_b;
1906
Douglas Gilbert773642d2016-04-25 12:16:28 -04001907 put_unaligned_be64(naa5_comp_a, sas_pcd_m_pg + 16);
1908 put_unaligned_be64(naa5_comp_c + 1, sas_pcd_m_pg + 24);
1909 put_unaligned_be64(naa5_comp_a, sas_pcd_m_pg + 64);
1910 put_unaligned_be64(naa5_comp_c + 1, sas_pcd_m_pg + 72);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001911 port_a = target_dev_id + 1;
1912 port_b = port_a + 1;
1913 memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
Douglas Gilbert773642d2016-04-25 12:16:28 -04001914 put_unaligned_be32(port_a, p + 20);
1915 put_unaligned_be32(port_b, p + 48 + 20);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001916 if (1 == pcontrol)
1917 memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
1918 return sizeof(sas_pcd_m_pg);
1919}
1920
1921static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol)
1922{ /* SAS SSP shared protocol specific port mode subpage */
1923 unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
1924 0, 0, 0, 0, 0, 0, 0, 0,
1925 };
1926
1927 memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
1928 if (1 == pcontrol)
1929 memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
1930 return sizeof(sas_sha_m_pg);
1931}
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933#define SDEBUG_MAX_MSENSE_SZ 256
1934
Douglas Gilbertfd321192016-04-25 12:16:33 -04001935static int resp_mode_sense(struct scsi_cmnd *scp,
1936 struct sdebug_dev_info *devip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Douglas Gilbert23183912006-09-16 20:30:47 -04001938 unsigned char dbd, llbaa;
1939 int pcontrol, pcode, subpcode, bd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 unsigned char dev_spec;
Douglas Gilbert773642d2016-04-25 12:16:28 -04001941 int alloc_len, msense_6, offset, len, target_dev_id;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05001942 int target = scp->device->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 unsigned char * ap;
1944 unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
Douglas Gilbert01123ef2014-08-05 12:20:02 +02001945 unsigned char *cmd = scp->cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Douglas Gilbert23183912006-09-16 20:30:47 -04001947 dbd = !!(cmd[1] & 0x8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 pcontrol = (cmd[2] & 0xc0) >> 6;
1949 pcode = cmd[2] & 0x3f;
1950 subpcode = cmd[3];
1951 msense_6 = (MODE_SENSE == cmd[0]);
Douglas Gilbert23183912006-09-16 20:30:47 -04001952 llbaa = msense_6 ? 0 : !!(cmd[1] & 0x10);
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04001953 if ((sdebug_ptype == TYPE_DISK) && (dbd == 0))
Douglas Gilbert23183912006-09-16 20:30:47 -04001954 bd_len = llbaa ? 16 : 8;
1955 else
1956 bd_len = 0;
Douglas Gilbert773642d2016-04-25 12:16:28 -04001957 alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
1959 if (0x3 == pcontrol) { /* Saving values not supported */
Douglas Gilbertcbf67842014-07-26 11:55:35 -04001960 mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return check_condition_result;
1962 }
Douglas Gilbertc65b1442006-06-06 00:11:24 -04001963 target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
1964 (devip->target * 1000) - 3;
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04001965 /* for disks set DPOFUA bit and clear write protect (WP) bit */
1966 if (sdebug_ptype == TYPE_DISK)
1967 dev_spec = 0x10; /* =0x90 if WP=1 implies read-only */
Douglas Gilbert23183912006-09-16 20:30:47 -04001968 else
1969 dev_spec = 0x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (msense_6) {
1971 arr[2] = dev_spec;
Douglas Gilbert23183912006-09-16 20:30:47 -04001972 arr[3] = bd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 offset = 4;
1974 } else {
1975 arr[3] = dev_spec;
Douglas Gilbert23183912006-09-16 20:30:47 -04001976 if (16 == bd_len)
1977 arr[4] = 0x1; /* set LONGLBA bit */
1978 arr[7] = bd_len; /* assume 255 or less */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 offset = 8;
1980 }
1981 ap = arr + offset;
FUJITA Tomonori28898872008-03-30 00:59:55 +09001982 if ((bd_len > 0) && (!sdebug_capacity))
1983 sdebug_capacity = get_sdebug_capacity();
1984
Douglas Gilbert23183912006-09-16 20:30:47 -04001985 if (8 == bd_len) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04001986 if (sdebug_capacity > 0xfffffffe)
1987 put_unaligned_be32(0xffffffff, ap + 0);
1988 else
1989 put_unaligned_be32(sdebug_capacity, ap + 0);
1990 put_unaligned_be16(sdebug_sector_size, ap + 6);
Douglas Gilbert23183912006-09-16 20:30:47 -04001991 offset += bd_len;
1992 ap = arr + offset;
1993 } else if (16 == bd_len) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04001994 put_unaligned_be64((u64)sdebug_capacity, ap + 0);
1995 put_unaligned_be32(sdebug_sector_size, ap + 12);
Douglas Gilbert23183912006-09-16 20:30:47 -04001996 offset += bd_len;
1997 ap = arr + offset;
1998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002000 if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) {
2001 /* TODO: Control Extension page */
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002002 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return check_condition_result;
2004 }
2005 switch (pcode) {
2006 case 0x1: /* Read-Write error recovery page, direct access */
2007 len = resp_err_recov_pg(ap, pcontrol, target);
2008 offset += len;
2009 break;
2010 case 0x2: /* Disconnect-Reconnect page, all devices */
2011 len = resp_disconnect_pg(ap, pcontrol, target);
2012 offset += len;
2013 break;
2014 case 0x3: /* Format device page, direct access */
2015 len = resp_format_pg(ap, pcontrol, target);
2016 offset += len;
2017 break;
2018 case 0x8: /* Caching page, direct access */
2019 len = resp_caching_pg(ap, pcontrol, target);
2020 offset += len;
2021 break;
2022 case 0xa: /* Control Mode page, all devices */
2023 len = resp_ctrl_m_pg(ap, pcontrol, target);
2024 offset += len;
2025 break;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002026 case 0x19: /* if spc==1 then sas phy, control+discover */
2027 if ((subpcode > 0x2) && (subpcode < 0xff)) {
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002028 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002029 return check_condition_result;
2030 }
2031 len = 0;
2032 if ((0x0 == subpcode) || (0xff == subpcode))
2033 len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
2034 if ((0x1 == subpcode) || (0xff == subpcode))
2035 len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
2036 target_dev_id);
2037 if ((0x2 == subpcode) || (0xff == subpcode))
2038 len += resp_sas_sha_m_spg(ap + len, pcontrol);
2039 offset += len;
2040 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 case 0x1c: /* Informational Exceptions Mode page, all devices */
2042 len = resp_iec_m_pg(ap, pcontrol, target);
2043 offset += len;
2044 break;
2045 case 0x3f: /* Read all Mode pages */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002046 if ((0 == subpcode) || (0xff == subpcode)) {
2047 len = resp_err_recov_pg(ap, pcontrol, target);
2048 len += resp_disconnect_pg(ap + len, pcontrol, target);
2049 len += resp_format_pg(ap + len, pcontrol, target);
2050 len += resp_caching_pg(ap + len, pcontrol, target);
2051 len += resp_ctrl_m_pg(ap + len, pcontrol, target);
2052 len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
2053 if (0xff == subpcode) {
2054 len += resp_sas_pcd_m_spg(ap + len, pcontrol,
2055 target, target_dev_id);
2056 len += resp_sas_sha_m_spg(ap + len, pcontrol);
2057 }
2058 len += resp_iec_m_pg(ap + len, pcontrol, target);
2059 } else {
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002060 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002061 return check_condition_result;
2062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 offset += len;
2064 break;
2065 default:
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002066 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return check_condition_result;
2068 }
2069 if (msense_6)
2070 arr[0] = offset - 1;
Douglas Gilbert773642d2016-04-25 12:16:28 -04002071 else
2072 put_unaligned_be16((offset - 2), arr + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
2074}
2075
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002076#define SDEBUG_MAX_MSELECT_SZ 512
2077
Douglas Gilbertfd321192016-04-25 12:16:33 -04002078static int resp_mode_select(struct scsi_cmnd *scp,
2079 struct sdebug_dev_info *devip)
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002080{
2081 int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002082 int param_len, res, mpage;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002083 unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
Douglas Gilbert01123ef2014-08-05 12:20:02 +02002084 unsigned char *cmd = scp->cmnd;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002085 int mselect6 = (MODE_SELECT == cmd[0]);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002086
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002087 memset(arr, 0, sizeof(arr));
2088 pf = cmd[1] & 0x10;
2089 sp = cmd[1] & 0x1;
Douglas Gilbert773642d2016-04-25 12:16:28 -04002090 param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002091 if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002092 mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002093 return check_condition_result;
2094 }
2095 res = fetch_to_dev_buffer(scp, arr, param_len);
2096 if (-1 == res)
Douglas Gilbert773642d2016-04-25 12:16:28 -04002097 return DID_ERROR << 16;
2098 else if (sdebug_verbose && (res < param_len))
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002099 sdev_printk(KERN_INFO, scp->device,
2100 "%s: cdb indicated=%d, IO sent=%d bytes\n",
2101 __func__, param_len, res);
Douglas Gilbert773642d2016-04-25 12:16:28 -04002102 md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2);
2103 bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6);
Douglas Gilbert23183912006-09-16 20:30:47 -04002104 if (md_len > 2) {
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002105 mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002106 return check_condition_result;
2107 }
2108 off = bd_len + (mselect6 ? 4 : 8);
2109 mpage = arr[off] & 0x3f;
2110 ps = !!(arr[off] & 0x80);
2111 if (ps) {
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002112 mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002113 return check_condition_result;
2114 }
2115 spf = !!(arr[off] & 0x40);
Douglas Gilbert773642d2016-04-25 12:16:28 -04002116 pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) :
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002117 (arr[off + 1] + 2);
2118 if ((pg_len + off) > param_len) {
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002119 mk_sense_buffer(scp, ILLEGAL_REQUEST,
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002120 PARAMETER_LIST_LENGTH_ERR, 0);
2121 return check_condition_result;
2122 }
2123 switch (mpage) {
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002124 case 0x8: /* Caching Mode page */
2125 if (caching_pg[1] == arr[off + 1]) {
2126 memcpy(caching_pg + 2, arr + off + 2,
2127 sizeof(caching_pg) - 2);
2128 goto set_mode_changed_ua;
2129 }
2130 break;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002131 case 0xa: /* Control Mode page */
2132 if (ctrl_m_pg[1] == arr[off + 1]) {
2133 memcpy(ctrl_m_pg + 2, arr + off + 2,
2134 sizeof(ctrl_m_pg) - 2);
Douglas Gilbert773642d2016-04-25 12:16:28 -04002135 sdebug_dsense = !!(ctrl_m_pg[2] & 0x4);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002136 goto set_mode_changed_ua;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002137 }
2138 break;
2139 case 0x1c: /* Informational Exceptions Mode page */
2140 if (iec_m_pg[1] == arr[off + 1]) {
2141 memcpy(iec_m_pg + 2, arr + off + 2,
2142 sizeof(iec_m_pg) - 2);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002143 goto set_mode_changed_ua;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002144 }
2145 break;
2146 default:
2147 break;
2148 }
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002149 mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002150 return check_condition_result;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002151set_mode_changed_ua:
2152 set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm);
2153 return 0;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002154}
2155
2156static int resp_temp_l_pg(unsigned char * arr)
2157{
2158 unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38,
2159 0x0, 0x1, 0x3, 0x2, 0x0, 65,
2160 };
2161
2162 memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
2163 return sizeof(temp_l_pg);
2164}
2165
2166static int resp_ie_l_pg(unsigned char * arr)
2167{
2168 unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
2169 };
2170
2171 memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
2172 if (iec_m_pg[2] & 0x4) { /* TEST bit set */
2173 arr[4] = THRESHOLD_EXCEEDED;
2174 arr[5] = 0xff;
2175 }
2176 return sizeof(ie_l_pg);
2177}
2178
2179#define SDEBUG_MAX_LSENSE_SZ 512
2180
2181static int resp_log_sense(struct scsi_cmnd * scp,
2182 struct sdebug_dev_info * devip)
2183{
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002184 int ppc, sp, pcontrol, pcode, subpcode, alloc_len, len, n;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002185 unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
Douglas Gilbert01123ef2014-08-05 12:20:02 +02002186 unsigned char *cmd = scp->cmnd;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002187
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002188 memset(arr, 0, sizeof(arr));
2189 ppc = cmd[1] & 0x2;
2190 sp = cmd[1] & 0x1;
2191 if (ppc || sp) {
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002192 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002193 return check_condition_result;
2194 }
2195 pcontrol = (cmd[2] & 0xc0) >> 6;
2196 pcode = cmd[2] & 0x3f;
Douglas Gilbert23183912006-09-16 20:30:47 -04002197 subpcode = cmd[3] & 0xff;
Douglas Gilbert773642d2016-04-25 12:16:28 -04002198 alloc_len = get_unaligned_be16(cmd + 7);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002199 arr[0] = pcode;
Douglas Gilbert23183912006-09-16 20:30:47 -04002200 if (0 == subpcode) {
2201 switch (pcode) {
2202 case 0x0: /* Supported log pages log page */
2203 n = 4;
2204 arr[n++] = 0x0; /* this page */
2205 arr[n++] = 0xd; /* Temperature */
2206 arr[n++] = 0x2f; /* Informational exceptions */
2207 arr[3] = n - 4;
2208 break;
2209 case 0xd: /* Temperature log page */
2210 arr[3] = resp_temp_l_pg(arr + 4);
2211 break;
2212 case 0x2f: /* Informational exceptions log page */
2213 arr[3] = resp_ie_l_pg(arr + 4);
2214 break;
2215 default:
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002216 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
Douglas Gilbert23183912006-09-16 20:30:47 -04002217 return check_condition_result;
2218 }
2219 } else if (0xff == subpcode) {
2220 arr[0] |= 0x40;
2221 arr[1] = subpcode;
2222 switch (pcode) {
2223 case 0x0: /* Supported log pages and subpages log page */
2224 n = 4;
2225 arr[n++] = 0x0;
2226 arr[n++] = 0x0; /* 0,0 page */
2227 arr[n++] = 0x0;
2228 arr[n++] = 0xff; /* this page */
2229 arr[n++] = 0xd;
2230 arr[n++] = 0x0; /* Temperature */
2231 arr[n++] = 0x2f;
2232 arr[n++] = 0x0; /* Informational exceptions */
2233 arr[3] = n - 4;
2234 break;
2235 case 0xd: /* Temperature subpages */
2236 n = 4;
2237 arr[n++] = 0xd;
2238 arr[n++] = 0x0; /* Temperature */
2239 arr[3] = n - 4;
2240 break;
2241 case 0x2f: /* Informational exceptions subpages */
2242 n = 4;
2243 arr[n++] = 0x2f;
2244 arr[n++] = 0x0; /* Informational exceptions */
2245 arr[3] = n - 4;
2246 break;
2247 default:
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002248 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
Douglas Gilbert23183912006-09-16 20:30:47 -04002249 return check_condition_result;
2250 }
2251 } else {
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002252 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002253 return check_condition_result;
2254 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04002255 len = min(get_unaligned_be16(arr + 2) + 4, alloc_len);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002256 return fill_from_dev_buffer(scp, arr,
2257 min(len, SDEBUG_MAX_INQ_ARR_SZ));
2258}
2259
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002260static int check_device_access_params(struct scsi_cmnd *scp,
FUJITA Tomonori19789102008-03-30 00:59:56 +09002261 unsigned long long lba, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002263 if (lba + num > sdebug_capacity) {
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002264 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return check_condition_result;
2266 }
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002267 /* transfer length excessive (tie in to block limits VPD page) */
2268 if (num > sdebug_store_sectors) {
Douglas Gilbert22017ed2014-11-24 23:04:47 -05002269 /* needs work to find which cdb byte 'num' comes from */
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002270 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002271 return check_condition_result;
2272 }
FUJITA Tomonori19789102008-03-30 00:59:56 +09002273 return 0;
2274}
2275
Akinobu Mitaa4517512013-07-08 16:01:57 -07002276/* Returns number of bytes copied or -1 if error. */
Douglas Gilbertfd321192016-04-25 12:16:33 -04002277static int do_device_access(struct scsi_cmnd *scmd, u64 lba, u32 num,
2278 bool do_write)
FUJITA Tomonori19789102008-03-30 00:59:56 +09002279{
2280 int ret;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002281 u64 block, rest = 0;
Akinobu Mitaa4517512013-07-08 16:01:57 -07002282 struct scsi_data_buffer *sdb;
2283 enum dma_data_direction dir;
FUJITA Tomonori19789102008-03-30 00:59:56 +09002284
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002285 if (do_write) {
Akinobu Mitaa4517512013-07-08 16:01:57 -07002286 sdb = scsi_out(scmd);
2287 dir = DMA_TO_DEVICE;
Akinobu Mitaa4517512013-07-08 16:01:57 -07002288 } else {
2289 sdb = scsi_in(scmd);
2290 dir = DMA_FROM_DEVICE;
Akinobu Mitaa4517512013-07-08 16:01:57 -07002291 }
2292
2293 if (!sdb->length)
2294 return 0;
2295 if (!(scsi_bidi_cmnd(scmd) || scmd->sc_data_direction == dir))
2296 return -1;
FUJITA Tomonori19789102008-03-30 00:59:56 +09002297
2298 block = do_div(lba, sdebug_store_sectors);
2299 if (block + num > sdebug_store_sectors)
2300 rest = block + num - sdebug_store_sectors;
2301
Dave Gordon386ecb12015-06-30 14:58:57 -07002302 ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
Douglas Gilbert773642d2016-04-25 12:16:28 -04002303 fake_storep + (block * sdebug_sector_size),
2304 (num - rest) * sdebug_sector_size, 0, do_write);
2305 if (ret != (num - rest) * sdebug_sector_size)
Akinobu Mitaa4517512013-07-08 16:01:57 -07002306 return ret;
2307
2308 if (rest) {
Dave Gordon386ecb12015-06-30 14:58:57 -07002309 ret += sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
Douglas Gilbert773642d2016-04-25 12:16:28 -04002310 fake_storep, rest * sdebug_sector_size,
2311 (num - rest) * sdebug_sector_size, do_write);
Akinobu Mitaa4517512013-07-08 16:01:57 -07002312 }
FUJITA Tomonori19789102008-03-30 00:59:56 +09002313
2314 return ret;
2315}
2316
Douglas Gilbert38d5c832014-11-24 21:27:12 -05002317/* If fake_store(lba,num) compares equal to arr(num), then copy top half of
2318 * arr into fake_store(lba,num) and return true. If comparison fails then
2319 * return false. */
Douglas Gilbertfd321192016-04-25 12:16:33 -04002320static bool comp_write_worker(u64 lba, u32 num, const u8 *arr)
Douglas Gilbert38d5c832014-11-24 21:27:12 -05002321{
2322 bool res;
2323 u64 block, rest = 0;
2324 u32 store_blks = sdebug_store_sectors;
Douglas Gilbert773642d2016-04-25 12:16:28 -04002325 u32 lb_size = sdebug_sector_size;
Douglas Gilbert38d5c832014-11-24 21:27:12 -05002326
2327 block = do_div(lba, store_blks);
2328 if (block + num > store_blks)
2329 rest = block + num - store_blks;
2330
2331 res = !memcmp(fake_storep + (block * lb_size), arr,
2332 (num - rest) * lb_size);
2333 if (!res)
2334 return res;
2335 if (rest)
2336 res = memcmp(fake_storep, arr + ((num - rest) * lb_size),
2337 rest * lb_size);
2338 if (!res)
2339 return res;
2340 arr += num * lb_size;
2341 memcpy(fake_storep + (block * lb_size), arr, (num - rest) * lb_size);
2342 if (rest)
2343 memcpy(fake_storep, arr + ((num - rest) * lb_size),
2344 rest * lb_size);
2345 return res;
2346}
2347
Akinobu Mita51d648a2013-09-18 21:27:28 +09002348static __be16 dif_compute_csum(const void *buf, int len)
Akinobu Mitabeb40ea2013-06-29 17:59:19 +09002349{
Akinobu Mita51d648a2013-09-18 21:27:28 +09002350 __be16 csum;
Akinobu Mitabeb40ea2013-06-29 17:59:19 +09002351
Douglas Gilbert773642d2016-04-25 12:16:28 -04002352 if (sdebug_guard)
Akinobu Mita51d648a2013-09-18 21:27:28 +09002353 csum = (__force __be16)ip_compute_csum(buf, len);
2354 else
Akinobu Mitabeb40ea2013-06-29 17:59:19 +09002355 csum = cpu_to_be16(crc_t10dif(buf, len));
Akinobu Mita51d648a2013-09-18 21:27:28 +09002356
Akinobu Mitabeb40ea2013-06-29 17:59:19 +09002357 return csum;
2358}
2359
2360static int dif_verify(struct sd_dif_tuple *sdt, const void *data,
2361 sector_t sector, u32 ei_lba)
2362{
Douglas Gilbert773642d2016-04-25 12:16:28 -04002363 __be16 csum = dif_compute_csum(data, sdebug_sector_size);
Akinobu Mitabeb40ea2013-06-29 17:59:19 +09002364
2365 if (sdt->guard_tag != csum) {
Tomas Winklerc12879702015-07-28 16:54:20 +03002366 pr_err("GUARD check failed on sector %lu rcvd 0x%04x, data 0x%04x\n",
Akinobu Mitabeb40ea2013-06-29 17:59:19 +09002367 (unsigned long)sector,
2368 be16_to_cpu(sdt->guard_tag),
2369 be16_to_cpu(csum));
2370 return 0x01;
2371 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04002372 if (sdebug_dif == SD_DIF_TYPE1_PROTECTION &&
Akinobu Mitabeb40ea2013-06-29 17:59:19 +09002373 be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
Tomas Winklerc12879702015-07-28 16:54:20 +03002374 pr_err("REF check failed on sector %lu\n",
2375 (unsigned long)sector);
Akinobu Mitabeb40ea2013-06-29 17:59:19 +09002376 return 0x03;
2377 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04002378 if (sdebug_dif == SD_DIF_TYPE2_PROTECTION &&
Akinobu Mitabeb40ea2013-06-29 17:59:19 +09002379 be32_to_cpu(sdt->ref_tag) != ei_lba) {
Tomas Winklerc12879702015-07-28 16:54:20 +03002380 pr_err("REF check failed on sector %lu\n",
2381 (unsigned long)sector);
Akinobu Mitabeb40ea2013-06-29 17:59:19 +09002382 return 0x03;
2383 }
2384 return 0;
2385}
2386
Akinobu Mitabb8c0632013-09-18 21:27:25 +09002387static void dif_copy_prot(struct scsi_cmnd *SCpnt, sector_t sector,
Akinobu Mita65f72f2a2013-09-18 21:27:26 +09002388 unsigned int sectors, bool read)
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002389{
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002390 size_t resid;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002391 void *paddr;
Akinobu Mita14faa942013-09-18 21:27:24 +09002392 const void *dif_store_end = dif_storep + sdebug_store_sectors;
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002393 struct sg_mapping_iter miter;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002394
Akinobu Mitae18d8be2013-06-29 17:59:18 +09002395 /* Bytes of protection data to copy into sgl */
2396 resid = sectors * sizeof(*dif_storep);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002397
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002398 sg_miter_start(&miter, scsi_prot_sglist(SCpnt),
2399 scsi_prot_sg_count(SCpnt), SG_MITER_ATOMIC |
2400 (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG));
2401
2402 while (sg_miter_next(&miter) && resid > 0) {
2403 size_t len = min(miter.length, resid);
Akinobu Mita14faa942013-09-18 21:27:24 +09002404 void *start = dif_store(sector);
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002405 size_t rest = 0;
Akinobu Mita14faa942013-09-18 21:27:24 +09002406
2407 if (dif_store_end < start + len)
2408 rest = start + len - dif_store_end;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002409
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002410 paddr = miter.addr;
Akinobu Mita14faa942013-09-18 21:27:24 +09002411
Akinobu Mita65f72f2a2013-09-18 21:27:26 +09002412 if (read)
2413 memcpy(paddr, start, len - rest);
2414 else
2415 memcpy(start, paddr, len - rest);
2416
2417 if (rest) {
2418 if (read)
2419 memcpy(paddr + len - rest, dif_storep, rest);
2420 else
2421 memcpy(dif_storep, paddr + len - rest, rest);
2422 }
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002423
Akinobu Mitae18d8be2013-06-29 17:59:18 +09002424 sector += len / sizeof(*dif_storep);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002425 resid -= len;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002426 }
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002427 sg_miter_stop(&miter);
Akinobu Mitabb8c0632013-09-18 21:27:25 +09002428}
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002429
Akinobu Mitabb8c0632013-09-18 21:27:25 +09002430static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec,
2431 unsigned int sectors, u32 ei_lba)
2432{
2433 unsigned int i;
2434 struct sd_dif_tuple *sdt;
2435 sector_t sector;
2436
Akinobu Mitac45eabec2014-02-26 22:56:58 +09002437 for (i = 0; i < sectors; i++, ei_lba++) {
Akinobu Mitabb8c0632013-09-18 21:27:25 +09002438 int ret;
2439
2440 sector = start_sec + i;
2441 sdt = dif_store(sector);
2442
Akinobu Mita51d648a2013-09-18 21:27:28 +09002443 if (sdt->app_tag == cpu_to_be16(0xffff))
Akinobu Mitabb8c0632013-09-18 21:27:25 +09002444 continue;
2445
2446 ret = dif_verify(sdt, fake_store(sector), sector, ei_lba);
2447 if (ret) {
2448 dif_errors++;
2449 return ret;
2450 }
Akinobu Mitabb8c0632013-09-18 21:27:25 +09002451 }
2452
Akinobu Mita65f72f2a2013-09-18 21:27:26 +09002453 dif_copy_prot(SCpnt, start_sec, sectors, true);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002454 dix_reads++;
2455
2456 return 0;
2457}
2458
Douglas Gilbertfd321192016-04-25 12:16:33 -04002459static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
FUJITA Tomonori19789102008-03-30 00:59:56 +09002460{
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002461 u8 *cmd = scp->cmnd;
Douglas Gilbertc4837392016-05-06 00:40:26 -04002462 struct sdebug_queued_cmd *sqcp;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002463 u64 lba;
2464 u32 num;
2465 u32 ei_lba;
FUJITA Tomonori19789102008-03-30 00:59:56 +09002466 unsigned long iflags;
2467 int ret;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002468 bool check_prot;
FUJITA Tomonori19789102008-03-30 00:59:56 +09002469
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002470 switch (cmd[0]) {
2471 case READ_16:
2472 ei_lba = 0;
2473 lba = get_unaligned_be64(cmd + 2);
2474 num = get_unaligned_be32(cmd + 10);
2475 check_prot = true;
2476 break;
2477 case READ_10:
2478 ei_lba = 0;
2479 lba = get_unaligned_be32(cmd + 2);
2480 num = get_unaligned_be16(cmd + 7);
2481 check_prot = true;
2482 break;
2483 case READ_6:
2484 ei_lba = 0;
2485 lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
2486 (u32)(cmd[1] & 0x1f) << 16;
2487 num = (0 == cmd[4]) ? 256 : cmd[4];
2488 check_prot = true;
2489 break;
2490 case READ_12:
2491 ei_lba = 0;
2492 lba = get_unaligned_be32(cmd + 2);
2493 num = get_unaligned_be32(cmd + 6);
2494 check_prot = true;
2495 break;
2496 case XDWRITEREAD_10:
2497 ei_lba = 0;
2498 lba = get_unaligned_be32(cmd + 2);
2499 num = get_unaligned_be16(cmd + 7);
2500 check_prot = false;
2501 break;
2502 default: /* assume READ(32) */
2503 lba = get_unaligned_be64(cmd + 12);
2504 ei_lba = get_unaligned_be32(cmd + 20);
2505 num = get_unaligned_be32(cmd + 28);
2506 check_prot = false;
2507 break;
2508 }
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002509 if (unlikely(have_dif_prot && check_prot)) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04002510 if (sdebug_dif == SD_DIF_TYPE2_PROTECTION &&
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002511 (cmd[1] & 0xe0)) {
2512 mk_sense_invalid_opcode(scp);
2513 return check_condition_result;
2514 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04002515 if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION ||
2516 sdebug_dif == SD_DIF_TYPE3_PROTECTION) &&
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002517 (cmd[1] & 0xe0) == 0)
2518 sdev_printk(KERN_ERR, scp->device, "Unprotected RD "
2519 "to DIF device\n");
2520 }
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002521 if (unlikely(sdebug_any_injecting_opt)) {
Douglas Gilbertc4837392016-05-06 00:40:26 -04002522 sqcp = (struct sdebug_queued_cmd *)scp->host_scribble;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002523
Douglas Gilbertc4837392016-05-06 00:40:26 -04002524 if (sqcp) {
2525 if (sqcp->inj_short)
2526 num /= 2;
2527 }
2528 } else
2529 sqcp = NULL;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002530
2531 /* inline check_device_access_params() */
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002532 if (unlikely(lba + num > sdebug_capacity)) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002533 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
2534 return check_condition_result;
2535 }
2536 /* transfer length excessive (tie in to block limits VPD page) */
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002537 if (unlikely(num > sdebug_store_sectors)) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002538 /* needs work to find which cdb byte 'num' comes from */
2539 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
2540 return check_condition_result;
2541 }
FUJITA Tomonori19789102008-03-30 00:59:56 +09002542
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002543 if (unlikely((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) &&
2544 (lba <= (OPT_MEDIUM_ERR_ADDR + OPT_MEDIUM_ERR_NUM - 1)) &&
2545 ((lba + num) > OPT_MEDIUM_ERR_ADDR))) {
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002546 /* claim unrecoverable read error */
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002547 mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002548 /* set info field and valid bit for fixed descriptor */
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002549 if (0x70 == (scp->sense_buffer[0] & 0x7f)) {
2550 scp->sense_buffer[0] |= 0x80; /* Valid bit */
Douglas Gilbert32f7ef72011-03-11 10:43:35 -05002551 ret = (lba < OPT_MEDIUM_ERR_ADDR)
2552 ? OPT_MEDIUM_ERR_ADDR : (int)lba;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002553 put_unaligned_be32(ret, scp->sense_buffer + 3);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04002554 }
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002555 scsi_set_resid(scp, scsi_bufflen(scp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 return check_condition_result;
2557 }
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002558
Akinobu Mita6c78cc02014-02-26 22:57:03 +09002559 read_lock_irqsave(&atomic_rw, iflags);
2560
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002561 /* DIX + T10 DIF */
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002562 if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002563 int prot_ret = prot_verify_read(scp, lba, num, ei_lba);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002564
2565 if (prot_ret) {
Akinobu Mita6c78cc02014-02-26 22:57:03 +09002566 read_unlock_irqrestore(&atomic_rw, iflags);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002567 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002568 return illegal_condition_result;
2569 }
2570 }
2571
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002572 ret = do_device_access(scp, lba, num, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 read_unlock_irqrestore(&atomic_rw, iflags);
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002574 if (unlikely(ret == -1))
Akinobu Mitaa4517512013-07-08 16:01:57 -07002575 return DID_ERROR << 16;
2576
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002577 scsi_in(scp)->resid = scsi_bufflen(scp) - ret;
Akinobu Mitaa4517512013-07-08 16:01:57 -07002578
Douglas Gilbertc4837392016-05-06 00:40:26 -04002579 if (unlikely(sqcp)) {
2580 if (sqcp->inj_recovered) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002581 mk_sense_buffer(scp, RECOVERED_ERROR,
2582 THRESHOLD_EXCEEDED, 0);
2583 return check_condition_result;
Douglas Gilbertc4837392016-05-06 00:40:26 -04002584 } else if (sqcp->inj_transport) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002585 mk_sense_buffer(scp, ABORTED_COMMAND,
2586 TRANSPORT_PROBLEM, ACK_NAK_TO);
2587 return check_condition_result;
Douglas Gilbertc4837392016-05-06 00:40:26 -04002588 } else if (sqcp->inj_dif) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002589 /* Logical block guard check failed */
2590 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
2591 return illegal_condition_result;
Douglas Gilbertc4837392016-05-06 00:40:26 -04002592 } else if (sqcp->inj_dix) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002593 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
2594 return illegal_condition_result;
2595 }
2596 }
Akinobu Mitaa4517512013-07-08 16:01:57 -07002597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
Tomas Winkler58a86352015-07-28 16:54:23 +03002600static void dump_sector(unsigned char *buf, int len)
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002601{
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002602 int i, j, n;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002603
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002604 pr_err(">>> Sector Dump <<<\n");
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002605 for (i = 0 ; i < len ; i += 16) {
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002606 char b[128];
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002607
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002608 for (j = 0, n = 0; j < 16; j++) {
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002609 unsigned char c = buf[i+j];
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002610
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002611 if (c >= 0x20 && c < 0x7e)
2612 n += scnprintf(b + n, sizeof(b) - n,
2613 " %c ", buf[i+j]);
2614 else
2615 n += scnprintf(b + n, sizeof(b) - n,
2616 "%02x ", buf[i+j]);
2617 }
2618 pr_err("%04d: %s\n", i, b);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002619 }
2620}
2621
2622static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec,
Martin K. Petersen395cef02009-09-18 17:33:03 -04002623 unsigned int sectors, u32 ei_lba)
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002624{
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002625 int ret;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002626 struct sd_dif_tuple *sdt;
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002627 void *daddr;
Akinobu Mita65f72f2a2013-09-18 21:27:26 +09002628 sector_t sector = start_sec;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002629 int ppage_offset;
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002630 int dpage_offset;
2631 struct sg_mapping_iter diter;
2632 struct sg_mapping_iter piter;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002633
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002634 BUG_ON(scsi_sg_count(SCpnt) == 0);
2635 BUG_ON(scsi_prot_sg_count(SCpnt) == 0);
2636
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002637 sg_miter_start(&piter, scsi_prot_sglist(SCpnt),
2638 scsi_prot_sg_count(SCpnt),
2639 SG_MITER_ATOMIC | SG_MITER_FROM_SG);
2640 sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt),
2641 SG_MITER_ATOMIC | SG_MITER_FROM_SG);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002642
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002643 /* For each protection page */
2644 while (sg_miter_next(&piter)) {
2645 dpage_offset = 0;
2646 if (WARN_ON(!sg_miter_next(&diter))) {
2647 ret = 0x01;
2648 goto out;
2649 }
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002650
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002651 for (ppage_offset = 0; ppage_offset < piter.length;
2652 ppage_offset += sizeof(struct sd_dif_tuple)) {
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002653 /* If we're at the end of the current
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002654 * data page advance to the next one
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002655 */
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002656 if (dpage_offset >= diter.length) {
2657 if (WARN_ON(!sg_miter_next(&diter))) {
2658 ret = 0x01;
2659 goto out;
2660 }
2661 dpage_offset = 0;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002662 }
2663
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002664 sdt = piter.addr + ppage_offset;
2665 daddr = diter.addr + dpage_offset;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002666
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002667 ret = dif_verify(sdt, daddr, sector, ei_lba);
Akinobu Mitabeb40ea2013-06-29 17:59:19 +09002668 if (ret) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04002669 dump_sector(daddr, sdebug_sector_size);
Martin K. Petersen395cef02009-09-18 17:33:03 -04002670 goto out;
2671 }
2672
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002673 sector++;
Martin K. Petersen395cef02009-09-18 17:33:03 -04002674 ei_lba++;
Douglas Gilbert773642d2016-04-25 12:16:28 -04002675 dpage_offset += sdebug_sector_size;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002676 }
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002677 diter.consumed = dpage_offset;
2678 sg_miter_stop(&diter);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002679 }
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002680 sg_miter_stop(&piter);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002681
Akinobu Mita65f72f2a2013-09-18 21:27:26 +09002682 dif_copy_prot(SCpnt, start_sec, sectors, false);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002683 dix_writes++;
2684
2685 return 0;
2686
2687out:
2688 dif_errors++;
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09002689 sg_miter_stop(&diter);
2690 sg_miter_stop(&piter);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002691 return ret;
2692}
2693
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002694static unsigned long lba_to_map_index(sector_t lba)
2695{
Douglas Gilbert773642d2016-04-25 12:16:28 -04002696 if (sdebug_unmap_alignment)
2697 lba += sdebug_unmap_granularity - sdebug_unmap_alignment;
2698 sector_div(lba, sdebug_unmap_granularity);
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002699 return lba;
2700}
2701
2702static sector_t map_index_to_lba(unsigned long index)
2703{
Douglas Gilbert773642d2016-04-25 12:16:28 -04002704 sector_t lba = index * sdebug_unmap_granularity;
Akinobu Mitaa027b5b2013-08-26 22:08:41 +09002705
Douglas Gilbert773642d2016-04-25 12:16:28 -04002706 if (sdebug_unmap_alignment)
2707 lba -= sdebug_unmap_granularity - sdebug_unmap_alignment;
Akinobu Mitaa027b5b2013-08-26 22:08:41 +09002708 return lba;
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002709}
2710
Martin K. Petersen44d92692009-10-15 14:45:27 -04002711static unsigned int map_state(sector_t lba, unsigned int *num)
2712{
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002713 sector_t end;
2714 unsigned int mapped;
2715 unsigned long index;
2716 unsigned long next;
Martin K. Petersen44d92692009-10-15 14:45:27 -04002717
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002718 index = lba_to_map_index(lba);
2719 mapped = test_bit(index, map_storep);
Martin K. Petersen44d92692009-10-15 14:45:27 -04002720
2721 if (mapped)
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002722 next = find_next_zero_bit(map_storep, map_size, index);
Martin K. Petersen44d92692009-10-15 14:45:27 -04002723 else
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002724 next = find_next_bit(map_storep, map_size, index);
Martin K. Petersen44d92692009-10-15 14:45:27 -04002725
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002726 end = min_t(sector_t, sdebug_store_sectors, map_index_to_lba(next));
Martin K. Petersen44d92692009-10-15 14:45:27 -04002727 *num = end - lba;
Martin K. Petersen44d92692009-10-15 14:45:27 -04002728 return mapped;
2729}
2730
2731static void map_region(sector_t lba, unsigned int len)
2732{
Martin K. Petersen44d92692009-10-15 14:45:27 -04002733 sector_t end = lba + len;
2734
Martin K. Petersen44d92692009-10-15 14:45:27 -04002735 while (lba < end) {
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002736 unsigned long index = lba_to_map_index(lba);
Martin K. Petersen44d92692009-10-15 14:45:27 -04002737
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002738 if (index < map_size)
2739 set_bit(index, map_storep);
Martin K. Petersen44d92692009-10-15 14:45:27 -04002740
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002741 lba = map_index_to_lba(index + 1);
Martin K. Petersen44d92692009-10-15 14:45:27 -04002742 }
2743}
2744
2745static void unmap_region(sector_t lba, unsigned int len)
2746{
Martin K. Petersen44d92692009-10-15 14:45:27 -04002747 sector_t end = lba + len;
2748
Martin K. Petersen44d92692009-10-15 14:45:27 -04002749 while (lba < end) {
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002750 unsigned long index = lba_to_map_index(lba);
Martin K. Petersen44d92692009-10-15 14:45:27 -04002751
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002752 if (lba == map_index_to_lba(index) &&
Douglas Gilbert773642d2016-04-25 12:16:28 -04002753 lba + sdebug_unmap_granularity <= end &&
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002754 index < map_size) {
2755 clear_bit(index, map_storep);
Douglas Gilbert773642d2016-04-25 12:16:28 -04002756 if (sdebug_lbprz) {
Eric Sandeenbe1dd782012-03-08 00:03:59 -06002757 memset(fake_storep +
Douglas Gilbert773642d2016-04-25 12:16:28 -04002758 lba * sdebug_sector_size, 0,
2759 sdebug_sector_size *
2760 sdebug_unmap_granularity);
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002761 }
Akinobu Mitae9926b42013-06-29 17:59:17 +09002762 if (dif_storep) {
2763 memset(dif_storep + lba, 0xff,
2764 sizeof(*dif_storep) *
Douglas Gilbert773642d2016-04-25 12:16:28 -04002765 sdebug_unmap_granularity);
Akinobu Mitae9926b42013-06-29 17:59:17 +09002766 }
Eric Sandeenbe1dd782012-03-08 00:03:59 -06002767 }
Akinobu Mitab90ebc32013-04-16 22:11:58 +09002768 lba = map_index_to_lba(index + 1);
Martin K. Petersen44d92692009-10-15 14:45:27 -04002769 }
2770}
2771
Douglas Gilbertfd321192016-04-25 12:16:33 -04002772static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002774 u8 *cmd = scp->cmnd;
2775 u64 lba;
2776 u32 num;
2777 u32 ei_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 unsigned long iflags;
FUJITA Tomonori19789102008-03-30 00:59:56 +09002779 int ret;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002780 bool check_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002782 switch (cmd[0]) {
2783 case WRITE_16:
2784 ei_lba = 0;
2785 lba = get_unaligned_be64(cmd + 2);
2786 num = get_unaligned_be32(cmd + 10);
2787 check_prot = true;
2788 break;
2789 case WRITE_10:
2790 ei_lba = 0;
2791 lba = get_unaligned_be32(cmd + 2);
2792 num = get_unaligned_be16(cmd + 7);
2793 check_prot = true;
2794 break;
2795 case WRITE_6:
2796 ei_lba = 0;
2797 lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
2798 (u32)(cmd[1] & 0x1f) << 16;
2799 num = (0 == cmd[4]) ? 256 : cmd[4];
2800 check_prot = true;
2801 break;
2802 case WRITE_12:
2803 ei_lba = 0;
2804 lba = get_unaligned_be32(cmd + 2);
2805 num = get_unaligned_be32(cmd + 6);
2806 check_prot = true;
2807 break;
2808 case 0x53: /* XDWRITEREAD(10) */
2809 ei_lba = 0;
2810 lba = get_unaligned_be32(cmd + 2);
2811 num = get_unaligned_be16(cmd + 7);
2812 check_prot = false;
2813 break;
2814 default: /* assume WRITE(32) */
2815 lba = get_unaligned_be64(cmd + 12);
2816 ei_lba = get_unaligned_be32(cmd + 20);
2817 num = get_unaligned_be32(cmd + 28);
2818 check_prot = false;
2819 break;
2820 }
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002821 if (unlikely(have_dif_prot && check_prot)) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04002822 if (sdebug_dif == SD_DIF_TYPE2_PROTECTION &&
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002823 (cmd[1] & 0xe0)) {
2824 mk_sense_invalid_opcode(scp);
2825 return check_condition_result;
2826 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04002827 if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION ||
2828 sdebug_dif == SD_DIF_TYPE3_PROTECTION) &&
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002829 (cmd[1] & 0xe0) == 0)
2830 sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
2831 "to DIF device\n");
2832 }
2833
2834 /* inline check_device_access_params() */
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002835 if (unlikely(lba + num > sdebug_capacity)) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002836 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
2837 return check_condition_result;
2838 }
2839 /* transfer length excessive (tie in to block limits VPD page) */
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002840 if (unlikely(num > sdebug_store_sectors)) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002841 /* needs work to find which cdb byte 'num' comes from */
2842 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
2843 return check_condition_result;
2844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Akinobu Mita6c78cc02014-02-26 22:57:03 +09002846 write_lock_irqsave(&atomic_rw, iflags);
2847
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002848 /* DIX + T10 DIF */
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002849 if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002850 int prot_ret = prot_verify_write(scp, lba, num, ei_lba);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002851
2852 if (prot_ret) {
Akinobu Mita6c78cc02014-02-26 22:57:03 +09002853 write_unlock_irqrestore(&atomic_rw, iflags);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002854 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05002855 return illegal_condition_result;
2856 }
2857 }
2858
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002859 ret = do_device_access(scp, lba, num, true);
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002860 if (unlikely(scsi_debug_lbp()))
Martin K. Petersen44d92692009-10-15 14:45:27 -04002861 map_region(lba, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 write_unlock_irqrestore(&atomic_rw, iflags);
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002863 if (unlikely(-1 == ret))
Douglas Gilbert773642d2016-04-25 12:16:28 -04002864 return DID_ERROR << 16;
Douglas Gilbertc4837392016-05-06 00:40:26 -04002865 else if (unlikely(sdebug_verbose &&
2866 (ret < (num * sdebug_sector_size))))
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002867 sdev_printk(KERN_INFO, scp->device,
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002868 "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
Douglas Gilbert773642d2016-04-25 12:16:28 -04002869 my_name, num * sdebug_sector_size, ret);
Martin K. Petersen44d92692009-10-15 14:45:27 -04002870
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04002871 if (unlikely(sdebug_any_injecting_opt)) {
Douglas Gilbertc4837392016-05-06 00:40:26 -04002872 struct sdebug_queued_cmd *sqcp =
2873 (struct sdebug_queued_cmd *)scp->host_scribble;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002874
Douglas Gilbertc4837392016-05-06 00:40:26 -04002875 if (sqcp) {
2876 if (sqcp->inj_recovered) {
2877 mk_sense_buffer(scp, RECOVERED_ERROR,
2878 THRESHOLD_EXCEEDED, 0);
2879 return check_condition_result;
2880 } else if (sqcp->inj_dif) {
2881 /* Logical block guard check failed */
2882 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
2883 return illegal_condition_result;
2884 } else if (sqcp->inj_dix) {
2885 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
2886 return illegal_condition_result;
2887 }
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002888 }
2889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return 0;
2891}
2892
Douglas Gilbertfd321192016-04-25 12:16:33 -04002893static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,
2894 u32 ei_lba, bool unmap, bool ndob)
Martin K. Petersen44d92692009-10-15 14:45:27 -04002895{
2896 unsigned long iflags;
2897 unsigned long long i;
2898 int ret;
Douglas Gilbert773642d2016-04-25 12:16:28 -04002899 u64 lba_off;
Martin K. Petersen44d92692009-10-15 14:45:27 -04002900
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002901 ret = check_device_access_params(scp, lba, num);
Martin K. Petersen44d92692009-10-15 14:45:27 -04002902 if (ret)
2903 return ret;
2904
2905 write_lock_irqsave(&atomic_rw, iflags);
2906
Akinobu Mita9ed8d3d2013-04-16 22:11:55 +09002907 if (unmap && scsi_debug_lbp()) {
Martin K. Petersen44d92692009-10-15 14:45:27 -04002908 unmap_region(lba, num);
2909 goto out;
2910 }
2911
Douglas Gilbert773642d2016-04-25 12:16:28 -04002912 lba_off = lba * sdebug_sector_size;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002913 /* if ndob then zero 1 logical block, else fetch 1 logical block */
2914 if (ndob) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04002915 memset(fake_storep + lba_off, 0, sdebug_sector_size);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002916 ret = 0;
2917 } else
Douglas Gilbert773642d2016-04-25 12:16:28 -04002918 ret = fetch_to_dev_buffer(scp, fake_storep + lba_off,
2919 sdebug_sector_size);
Martin K. Petersen44d92692009-10-15 14:45:27 -04002920
2921 if (-1 == ret) {
2922 write_unlock_irqrestore(&atomic_rw, iflags);
Douglas Gilbert773642d2016-04-25 12:16:28 -04002923 return DID_ERROR << 16;
2924 } else if (sdebug_verbose && (ret < (num * sdebug_sector_size)))
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002925 sdev_printk(KERN_INFO, scp->device,
Douglas Gilbertcbf67842014-07-26 11:55:35 -04002926 "%s: %s: cdb indicated=%u, IO sent=%d bytes\n",
2927 my_name, "write same",
Douglas Gilbert773642d2016-04-25 12:16:28 -04002928 num * sdebug_sector_size, ret);
Martin K. Petersen44d92692009-10-15 14:45:27 -04002929
2930 /* Copy first sector to remaining blocks */
2931 for (i = 1 ; i < num ; i++)
Douglas Gilbert773642d2016-04-25 12:16:28 -04002932 memcpy(fake_storep + ((lba + i) * sdebug_sector_size),
2933 fake_storep + lba_off,
2934 sdebug_sector_size);
Martin K. Petersen44d92692009-10-15 14:45:27 -04002935
Akinobu Mita9ed8d3d2013-04-16 22:11:55 +09002936 if (scsi_debug_lbp())
Martin K. Petersen44d92692009-10-15 14:45:27 -04002937 map_region(lba, num);
2938out:
2939 write_unlock_irqrestore(&atomic_rw, iflags);
2940
2941 return 0;
2942}
2943
Douglas Gilbertfd321192016-04-25 12:16:33 -04002944static int resp_write_same_10(struct scsi_cmnd *scp,
2945 struct sdebug_dev_info *devip)
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002946{
2947 u8 *cmd = scp->cmnd;
2948 u32 lba;
2949 u16 num;
2950 u32 ei_lba = 0;
2951 bool unmap = false;
2952
2953 if (cmd[1] & 0x8) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04002954 if (sdebug_lbpws10 == 0) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002955 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
2956 return check_condition_result;
2957 } else
2958 unmap = true;
2959 }
2960 lba = get_unaligned_be32(cmd + 2);
2961 num = get_unaligned_be16(cmd + 7);
Douglas Gilbert773642d2016-04-25 12:16:28 -04002962 if (num > sdebug_write_same_length) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002963 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
2964 return check_condition_result;
2965 }
2966 return resp_write_same(scp, lba, num, ei_lba, unmap, false);
2967}
2968
Douglas Gilbertfd321192016-04-25 12:16:33 -04002969static int resp_write_same_16(struct scsi_cmnd *scp,
2970 struct sdebug_dev_info *devip)
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002971{
2972 u8 *cmd = scp->cmnd;
2973 u64 lba;
2974 u32 num;
2975 u32 ei_lba = 0;
2976 bool unmap = false;
2977 bool ndob = false;
2978
2979 if (cmd[1] & 0x8) { /* UNMAP */
Douglas Gilbert773642d2016-04-25 12:16:28 -04002980 if (sdebug_lbpws == 0) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002981 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
2982 return check_condition_result;
2983 } else
2984 unmap = true;
2985 }
2986 if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */
2987 ndob = true;
2988 lba = get_unaligned_be64(cmd + 2);
2989 num = get_unaligned_be32(cmd + 10);
Douglas Gilbert773642d2016-04-25 12:16:28 -04002990 if (num > sdebug_write_same_length) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05002991 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1);
2992 return check_condition_result;
2993 }
2994 return resp_write_same(scp, lba, num, ei_lba, unmap, ndob);
2995}
2996
Ewan D. Milneacafd0b2014-12-04 11:49:28 -05002997/* Note the mode field is in the same position as the (lower) service action
2998 * field. For the Report supported operation codes command, SPC-4 suggests
2999 * each mode of this command should be reported separately; for future. */
Douglas Gilbertfd321192016-04-25 12:16:33 -04003000static int resp_write_buffer(struct scsi_cmnd *scp,
3001 struct sdebug_dev_info *devip)
Ewan D. Milneacafd0b2014-12-04 11:49:28 -05003002{
3003 u8 *cmd = scp->cmnd;
3004 struct scsi_device *sdp = scp->device;
3005 struct sdebug_dev_info *dp;
3006 u8 mode;
3007
3008 mode = cmd[1] & 0x1f;
3009 switch (mode) {
3010 case 0x4: /* download microcode (MC) and activate (ACT) */
3011 /* set UAs on this device only */
3012 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
3013 set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm);
3014 break;
3015 case 0x5: /* download MC, save and ACT */
3016 set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm);
3017 break;
3018 case 0x6: /* download MC with offsets and ACT */
3019 /* set UAs on most devices (LUs) in this target */
3020 list_for_each_entry(dp,
3021 &devip->sdbg_host->dev_info_list,
3022 dev_list)
3023 if (dp->target == sdp->id) {
3024 set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm);
3025 if (devip != dp)
3026 set_bit(SDEBUG_UA_MICROCODE_CHANGED,
3027 dp->uas_bm);
3028 }
3029 break;
3030 case 0x7: /* download MC with offsets, save, and ACT */
3031 /* set UA on all devices (LUs) in this target */
3032 list_for_each_entry(dp,
3033 &devip->sdbg_host->dev_info_list,
3034 dev_list)
3035 if (dp->target == sdp->id)
3036 set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET,
3037 dp->uas_bm);
3038 break;
3039 default:
3040 /* do nothing for this command for other mode values */
3041 break;
3042 }
3043 return 0;
3044}
3045
Douglas Gilbertfd321192016-04-25 12:16:33 -04003046static int resp_comp_write(struct scsi_cmnd *scp,
3047 struct sdebug_dev_info *devip)
Douglas Gilbert38d5c832014-11-24 21:27:12 -05003048{
3049 u8 *cmd = scp->cmnd;
3050 u8 *arr;
3051 u8 *fake_storep_hold;
3052 u64 lba;
3053 u32 dnum;
Douglas Gilbert773642d2016-04-25 12:16:28 -04003054 u32 lb_size = sdebug_sector_size;
Douglas Gilbert38d5c832014-11-24 21:27:12 -05003055 u8 num;
3056 unsigned long iflags;
3057 int ret;
Douglas Gilbertd467d312014-11-26 12:33:48 -05003058 int retval = 0;
Douglas Gilbert38d5c832014-11-24 21:27:12 -05003059
Douglas Gilbertd467d312014-11-26 12:33:48 -05003060 lba = get_unaligned_be64(cmd + 2);
Douglas Gilbert38d5c832014-11-24 21:27:12 -05003061 num = cmd[13]; /* 1 to a maximum of 255 logical blocks */
3062 if (0 == num)
3063 return 0; /* degenerate case, not an error */
Douglas Gilbert773642d2016-04-25 12:16:28 -04003064 if (sdebug_dif == SD_DIF_TYPE2_PROTECTION &&
Douglas Gilbert38d5c832014-11-24 21:27:12 -05003065 (cmd[1] & 0xe0)) {
3066 mk_sense_invalid_opcode(scp);
3067 return check_condition_result;
3068 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04003069 if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION ||
3070 sdebug_dif == SD_DIF_TYPE3_PROTECTION) &&
Douglas Gilbert38d5c832014-11-24 21:27:12 -05003071 (cmd[1] & 0xe0) == 0)
3072 sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
3073 "to DIF device\n");
3074
3075 /* inline check_device_access_params() */
3076 if (lba + num > sdebug_capacity) {
3077 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
3078 return check_condition_result;
3079 }
3080 /* transfer length excessive (tie in to block limits VPD page) */
3081 if (num > sdebug_store_sectors) {
3082 /* needs work to find which cdb byte 'num' comes from */
3083 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
3084 return check_condition_result;
3085 }
Douglas Gilbertd467d312014-11-26 12:33:48 -05003086 dnum = 2 * num;
3087 arr = kzalloc(dnum * lb_size, GFP_ATOMIC);
3088 if (NULL == arr) {
3089 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
3090 INSUFF_RES_ASCQ);
3091 return check_condition_result;
3092 }
Douglas Gilbert38d5c832014-11-24 21:27:12 -05003093
3094 write_lock_irqsave(&atomic_rw, iflags);
3095
3096 /* trick do_device_access() to fetch both compare and write buffers
3097 * from data-in into arr. Safe (atomic) since write_lock held. */
3098 fake_storep_hold = fake_storep;
3099 fake_storep = arr;
3100 ret = do_device_access(scp, 0, dnum, true);
3101 fake_storep = fake_storep_hold;
3102 if (ret == -1) {
Douglas Gilbertd467d312014-11-26 12:33:48 -05003103 retval = DID_ERROR << 16;
3104 goto cleanup;
Douglas Gilbert773642d2016-04-25 12:16:28 -04003105 } else if (sdebug_verbose && (ret < (dnum * lb_size)))
Douglas Gilbert38d5c832014-11-24 21:27:12 -05003106 sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb "
3107 "indicated=%u, IO sent=%d bytes\n", my_name,
3108 dnum * lb_size, ret);
3109 if (!comp_write_worker(lba, num, arr)) {
Douglas Gilbert38d5c832014-11-24 21:27:12 -05003110 mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0);
Douglas Gilbertd467d312014-11-26 12:33:48 -05003111 retval = check_condition_result;
3112 goto cleanup;
Douglas Gilbert38d5c832014-11-24 21:27:12 -05003113 }
3114 if (scsi_debug_lbp())
3115 map_region(lba, num);
Douglas Gilbertd467d312014-11-26 12:33:48 -05003116cleanup:
Douglas Gilbert38d5c832014-11-24 21:27:12 -05003117 write_unlock_irqrestore(&atomic_rw, iflags);
Douglas Gilbertd467d312014-11-26 12:33:48 -05003118 kfree(arr);
3119 return retval;
Douglas Gilbert38d5c832014-11-24 21:27:12 -05003120}
3121
Martin K. Petersen44d92692009-10-15 14:45:27 -04003122struct unmap_block_desc {
3123 __be64 lba;
3124 __be32 blocks;
3125 __be32 __reserved;
3126};
3127
Douglas Gilbertfd321192016-04-25 12:16:33 -04003128static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
Martin K. Petersen44d92692009-10-15 14:45:27 -04003129{
3130 unsigned char *buf;
3131 struct unmap_block_desc *desc;
3132 unsigned int i, payload_len, descriptors;
3133 int ret;
Akinobu Mita6c78cc02014-02-26 22:57:03 +09003134 unsigned long iflags;
Martin K. Petersen44d92692009-10-15 14:45:27 -04003135
Martin K. Petersen44d92692009-10-15 14:45:27 -04003136
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003137 if (!scsi_debug_lbp())
3138 return 0; /* fib and say its done */
3139 payload_len = get_unaligned_be16(scp->cmnd + 7);
3140 BUG_ON(scsi_bufflen(scp) != payload_len);
Martin K. Petersen44d92692009-10-15 14:45:27 -04003141
3142 descriptors = (payload_len - 8) / 16;
Douglas Gilbert773642d2016-04-25 12:16:28 -04003143 if (descriptors > sdebug_unmap_max_desc) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003144 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
Martin K. Petersen44d92692009-10-15 14:45:27 -04003145 return check_condition_result;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003146 }
Martin K. Petersen44d92692009-10-15 14:45:27 -04003147
Douglas Gilbertb333a812016-04-25 12:16:30 -04003148 buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003149 if (!buf) {
3150 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
3151 INSUFF_RES_ASCQ);
3152 return check_condition_result;
3153 }
3154
3155 scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
Martin K. Petersen44d92692009-10-15 14:45:27 -04003156
3157 BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2);
3158 BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16);
3159
3160 desc = (void *)&buf[8];
3161
Akinobu Mita6c78cc02014-02-26 22:57:03 +09003162 write_lock_irqsave(&atomic_rw, iflags);
3163
Martin K. Petersen44d92692009-10-15 14:45:27 -04003164 for (i = 0 ; i < descriptors ; i++) {
3165 unsigned long long lba = get_unaligned_be64(&desc[i].lba);
3166 unsigned int num = get_unaligned_be32(&desc[i].blocks);
3167
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003168 ret = check_device_access_params(scp, lba, num);
Martin K. Petersen44d92692009-10-15 14:45:27 -04003169 if (ret)
3170 goto out;
3171
3172 unmap_region(lba, num);
3173 }
3174
3175 ret = 0;
3176
3177out:
Akinobu Mita6c78cc02014-02-26 22:57:03 +09003178 write_unlock_irqrestore(&atomic_rw, iflags);
Martin K. Petersen44d92692009-10-15 14:45:27 -04003179 kfree(buf);
3180
3181 return ret;
3182}
3183
3184#define SDEBUG_GET_LBA_STATUS_LEN 32
3185
Douglas Gilbertfd321192016-04-25 12:16:33 -04003186static int resp_get_lba_status(struct scsi_cmnd *scp,
3187 struct sdebug_dev_info *devip)
Martin K. Petersen44d92692009-10-15 14:45:27 -04003188{
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003189 u8 *cmd = scp->cmnd;
3190 u64 lba;
3191 u32 alloc_len, mapped, num;
3192 u8 arr[SDEBUG_GET_LBA_STATUS_LEN];
Martin K. Petersen44d92692009-10-15 14:45:27 -04003193 int ret;
3194
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003195 lba = get_unaligned_be64(cmd + 2);
3196 alloc_len = get_unaligned_be32(cmd + 10);
Martin K. Petersen44d92692009-10-15 14:45:27 -04003197
3198 if (alloc_len < 24)
3199 return 0;
3200
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003201 ret = check_device_access_params(scp, lba, 1);
Martin K. Petersen44d92692009-10-15 14:45:27 -04003202 if (ret)
3203 return ret;
3204
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003205 if (scsi_debug_lbp())
3206 mapped = map_state(lba, &num);
3207 else {
3208 mapped = 1;
3209 /* following just in case virtual_gb changed */
3210 sdebug_capacity = get_sdebug_capacity();
3211 if (sdebug_capacity - lba <= 0xffffffff)
3212 num = sdebug_capacity - lba;
3213 else
3214 num = 0xffffffff;
3215 }
Martin K. Petersen44d92692009-10-15 14:45:27 -04003216
3217 memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003218 put_unaligned_be32(20, arr); /* Parameter Data Length */
3219 put_unaligned_be64(lba, arr + 8); /* LBA */
3220 put_unaligned_be32(num, arr + 16); /* Number of blocks */
3221 arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */
Martin K. Petersen44d92692009-10-15 14:45:27 -04003222
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003223 return fill_from_dev_buffer(scp, arr, SDEBUG_GET_LBA_STATUS_LEN);
Martin K. Petersen44d92692009-10-15 14:45:27 -04003224}
3225
Douglas Gilbert8d039e22016-04-30 22:44:43 -04003226/* Even though each pseudo target has a REPORT LUNS "well known logical unit"
3227 * (W-LUN), the normal Linux scanning logic does not associate it with a
3228 * device (e.g. /dev/sg7). The following magic will make that association:
3229 * "cd /sys/class/scsi_host/host<n> ; echo '- - 49409' > scan"
3230 * where <n> is a host number. If there are multiple targets in a host then
3231 * the above will associate a W-LUN to each target. To only get a W-LUN
3232 * for target 2, then use "echo '- 2 49409' > scan" .
3233 */
3234static int resp_report_luns(struct scsi_cmnd *scp,
3235 struct sdebug_dev_info *devip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
Douglas Gilbert01123ef2014-08-05 12:20:02 +02003237 unsigned char *cmd = scp->cmnd;
Douglas Gilbert8d039e22016-04-30 22:44:43 -04003238 unsigned int alloc_len;
3239 unsigned char select_report;
3240 u64 lun;
3241 struct scsi_lun *lun_p;
3242 u8 *arr;
3243 unsigned int lun_cnt; /* normal LUN count (max: 256) */
3244 unsigned int wlun_cnt; /* report luns W-LUN count */
3245 unsigned int tlun_cnt; /* total LUN count */
3246 unsigned int rlen; /* response length (in bytes) */
3247 int i, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Ewan D. Milne19c8ead2014-12-04 11:49:27 -05003249 clear_luns_changed_on_target(devip);
Douglas Gilbert8d039e22016-04-30 22:44:43 -04003250
3251 select_report = cmd[2];
3252 alloc_len = get_unaligned_be32(cmd + 6);
3253
3254 if (alloc_len < 4) {
3255 pr_err("alloc len too small %d\n", alloc_len);
3256 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return check_condition_result;
3258 }
Douglas Gilbert8d039e22016-04-30 22:44:43 -04003259
3260 switch (select_report) {
3261 case 0: /* all LUNs apart from W-LUNs */
3262 lun_cnt = sdebug_max_luns;
3263 wlun_cnt = 0;
3264 break;
3265 case 1: /* only W-LUNs */
Douglas Gilbertc65b1442006-06-06 00:11:24 -04003266 lun_cnt = 0;
Douglas Gilbert8d039e22016-04-30 22:44:43 -04003267 wlun_cnt = 1;
3268 break;
3269 case 2: /* all LUNs */
3270 lun_cnt = sdebug_max_luns;
3271 wlun_cnt = 1;
3272 break;
3273 case 0x10: /* only administrative LUs */
3274 case 0x11: /* see SPC-5 */
3275 case 0x12: /* only subsiduary LUs owned by referenced LU */
3276 default:
3277 pr_debug("select report invalid %d\n", select_report);
3278 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
3279 return check_condition_result;
3280 }
3281
3282 if (sdebug_no_lun_0 && (lun_cnt > 0))
Douglas Gilbertc65b1442006-06-06 00:11:24 -04003283 --lun_cnt;
Douglas Gilbert8d039e22016-04-30 22:44:43 -04003284
3285 tlun_cnt = lun_cnt + wlun_cnt;
3286
3287 rlen = (tlun_cnt * sizeof(struct scsi_lun)) + 8;
3288 arr = vmalloc(rlen);
3289 if (!arr) {
3290 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
3291 INSUFF_RES_ASCQ);
3292 return check_condition_result;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04003293 }
Douglas Gilbert8d039e22016-04-30 22:44:43 -04003294 memset(arr, 0, rlen);
3295 pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n",
3296 select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0);
3297
3298 /* luns start at byte 8 in response following the header */
3299 lun_p = (struct scsi_lun *)&arr[8];
3300
3301 /* LUNs use single level peripheral device addressing method */
3302 lun = sdebug_no_lun_0 ? 1 : 0;
3303 for (i = 0; i < lun_cnt; i++)
3304 int_to_scsilun(lun++, lun_p++);
3305
3306 if (wlun_cnt)
3307 int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p++);
3308
3309 put_unaligned_be32(rlen - 8, &arr[0]);
3310
3311 res = fill_from_dev_buffer(scp, arr, rlen);
3312 vfree(arr);
3313 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
FUJITA Tomonoric639d142008-01-23 01:32:01 +09003316static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba,
3317 unsigned int num, struct sdebug_dev_info *devip)
3318{
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09003319 int j;
FUJITA Tomonoric639d142008-01-23 01:32:01 +09003320 unsigned char *kaddr, *buf;
3321 unsigned int offset;
FUJITA Tomonoric639d142008-01-23 01:32:01 +09003322 struct scsi_data_buffer *sdb = scsi_in(scp);
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09003323 struct sg_mapping_iter miter;
FUJITA Tomonoric639d142008-01-23 01:32:01 +09003324
3325 /* better not to use temporary buffer. */
Douglas Gilbertb333a812016-04-25 12:16:30 -04003326 buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC);
Akinobu Mitac5af0db2014-02-26 22:57:01 +09003327 if (!buf) {
Douglas Gilbert22017ed2014-11-24 23:04:47 -05003328 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
3329 INSUFF_RES_ASCQ);
Akinobu Mitac5af0db2014-02-26 22:57:01 +09003330 return check_condition_result;
3331 }
FUJITA Tomonoric639d142008-01-23 01:32:01 +09003332
FUJITA Tomonori21a61822008-03-09 13:44:30 +09003333 scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
FUJITA Tomonoric639d142008-01-23 01:32:01 +09003334
3335 offset = 0;
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09003336 sg_miter_start(&miter, sdb->table.sgl, sdb->table.nents,
3337 SG_MITER_ATOMIC | SG_MITER_TO_SG);
FUJITA Tomonoric639d142008-01-23 01:32:01 +09003338
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09003339 while (sg_miter_next(&miter)) {
3340 kaddr = miter.addr;
3341 for (j = 0; j < miter.length; j++)
3342 *(kaddr + j) ^= *(buf + offset + j);
FUJITA Tomonoric639d142008-01-23 01:32:01 +09003343
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09003344 offset += miter.length;
FUJITA Tomonoric639d142008-01-23 01:32:01 +09003345 }
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09003346 sg_miter_stop(&miter);
FUJITA Tomonoric639d142008-01-23 01:32:01 +09003347 kfree(buf);
3348
Akinobu Mitabe4e11b2014-02-26 22:57:02 +09003349 return 0;
FUJITA Tomonoric639d142008-01-23 01:32:01 +09003350}
3351
Douglas Gilbertfd321192016-04-25 12:16:33 -04003352static int resp_xdwriteread_10(struct scsi_cmnd *scp,
3353 struct sdebug_dev_info *devip)
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003354{
3355 u8 *cmd = scp->cmnd;
3356 u64 lba;
3357 u32 num;
3358 int errsts;
3359
3360 if (!scsi_bidi_cmnd(scp)) {
3361 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
3362 INSUFF_RES_ASCQ);
3363 return check_condition_result;
3364 }
3365 errsts = resp_read_dt0(scp, devip);
3366 if (errsts)
3367 return errsts;
3368 if (!(cmd[1] & 0x4)) { /* DISABLE_WRITE is not set */
3369 errsts = resp_write_dt0(scp, devip);
3370 if (errsts)
3371 return errsts;
3372 }
3373 lba = get_unaligned_be32(cmd + 2);
3374 num = get_unaligned_be16(cmd + 7);
3375 return resp_xdwriteread(scp, lba, num, devip);
3376}
3377
Douglas Gilbertc4837392016-05-06 00:40:26 -04003378static struct sdebug_queue *get_queue(struct scsi_cmnd *cmnd)
3379{
3380 struct sdebug_queue *sqp = sdebug_q_arr;
3381
3382 if (sdebug_mq_active) {
3383 u32 tag = blk_mq_unique_tag(cmnd->request);
3384 u16 hwq = blk_mq_unique_tag_to_hwq(tag);
3385
3386 if (unlikely(hwq >= submit_queues)) {
3387 pr_warn("Unexpected hwq=%d, apply modulo\n", hwq);
3388 hwq %= submit_queues;
3389 }
3390 pr_debug("tag=%u, hwq=%d\n", tag, hwq);
3391 return sqp + hwq;
3392 } else
3393 return sqp;
3394}
3395
3396/* Queued (deferred) command completions converge here. */
Douglas Gilbertfd321192016-04-25 12:16:33 -04003397static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398{
Douglas Gilbertc4837392016-05-06 00:40:26 -04003399 int qc_idx;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003400 int retiring = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 unsigned long iflags;
Douglas Gilbertc4837392016-05-06 00:40:26 -04003402 struct sdebug_queue *sqp;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003403 struct sdebug_queued_cmd *sqcp;
3404 struct scsi_cmnd *scp;
3405 struct sdebug_dev_info *devip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Douglas Gilbertc4837392016-05-06 00:40:26 -04003407 qc_idx = sd_dp->qc_idx;
3408 sqp = sdebug_q_arr + sd_dp->sqa_idx;
3409 if (sdebug_statistics) {
3410 atomic_inc(&sdebug_completions);
3411 if (raw_smp_processor_id() != sd_dp->issuing_cpu)
3412 atomic_inc(&sdebug_miss_cpus);
3413 }
3414 if (unlikely((qc_idx < 0) || (qc_idx >= SDEBUG_CANQUEUE))) {
3415 pr_err("wild qc_idx=%d\n", qc_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 return;
3417 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04003418 spin_lock_irqsave(&sqp->qc_lock, iflags);
3419 sqcp = &sqp->qc_arr[qc_idx];
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003420 scp = sqcp->a_cmnd;
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04003421 if (unlikely(scp == NULL)) {
Douglas Gilbertc4837392016-05-06 00:40:26 -04003422 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
3423 pr_err("scp is NULL, sqa_idx=%d, qc_idx=%d\n",
3424 sd_dp->sqa_idx, qc_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return;
3426 }
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003427 devip = (struct sdebug_dev_info *)scp->device->hostdata;
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04003428 if (likely(devip))
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003429 atomic_dec(&devip->num_in_q);
3430 else
Tomas Winklerc12879702015-07-28 16:54:20 +03003431 pr_err("devip=NULL\n");
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04003432 if (unlikely(atomic_read(&retired_max_queue) > 0))
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003433 retiring = 1;
3434
3435 sqcp->a_cmnd = NULL;
Douglas Gilbertc4837392016-05-06 00:40:26 -04003436 if (unlikely(!test_and_clear_bit(qc_idx, sqp->in_use_bm))) {
3437 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
Tomas Winklerc12879702015-07-28 16:54:20 +03003438 pr_err("Unexpected completion\n");
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003439 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 }
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003441
3442 if (unlikely(retiring)) { /* user has reduced max_queue */
3443 int k, retval;
3444
3445 retval = atomic_read(&retired_max_queue);
Douglas Gilbertc4837392016-05-06 00:40:26 -04003446 if (qc_idx >= retval) {
3447 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
Tomas Winklerc12879702015-07-28 16:54:20 +03003448 pr_err("index %d too large\n", retval);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003449 return;
3450 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04003451 k = find_last_bit(sqp->in_use_bm, retval);
Douglas Gilbert773642d2016-04-25 12:16:28 -04003452 if ((k < sdebug_max_queue) || (k == retval))
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003453 atomic_set(&retired_max_queue, 0);
3454 else
3455 atomic_set(&retired_max_queue, k + 1);
3456 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04003457 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003458 scp->scsi_done(scp); /* callback to mid level */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459}
3460
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003461/* When high resolution timer goes off this function is called. */
Douglas Gilbertfd321192016-04-25 12:16:33 -04003462static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003463{
Douglas Gilberta10bc122016-04-25 12:16:32 -04003464 struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer,
3465 hrt);
3466 sdebug_q_cmd_complete(sd_dp);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003467 return HRTIMER_NORESTART;
3468}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Douglas Gilberta10bc122016-04-25 12:16:32 -04003470/* When work queue schedules work, it calls this function. */
Douglas Gilbertfd321192016-04-25 12:16:33 -04003471static void sdebug_q_cmd_wq_complete(struct work_struct *work)
Douglas Gilberta10bc122016-04-25 12:16:32 -04003472{
3473 struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer,
3474 ew.work);
3475 sdebug_q_cmd_complete(sd_dp);
3476}
3477
Douglas Gilbertfd321192016-04-25 12:16:33 -04003478static struct sdebug_dev_info *sdebug_device_create(
3479 struct sdebug_host_info *sdbg_host, gfp_t flags)
FUJITA Tomonori5cb2fc02008-03-20 11:09:16 +09003480{
3481 struct sdebug_dev_info *devip;
3482
3483 devip = kzalloc(sizeof(*devip), flags);
3484 if (devip) {
3485 devip->sdbg_host = sdbg_host;
3486 list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list);
3487 }
3488 return devip;
3489}
3490
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04003491static struct sdebug_dev_info *find_build_dev_info(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04003493 struct sdebug_host_info *sdbg_host;
3494 struct sdebug_dev_info *open_devip = NULL;
3495 struct sdebug_dev_info *devip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
FUJITA Tomonorid1e4c9c2008-03-02 18:30:18 +09003497 sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host);
3498 if (!sdbg_host) {
Tomas Winklerc12879702015-07-28 16:54:20 +03003499 pr_err("Host info NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 return NULL;
3501 }
3502 list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
3503 if ((devip->used) && (devip->channel == sdev->channel) &&
3504 (devip->target == sdev->id) &&
3505 (devip->lun == sdev->lun))
3506 return devip;
3507 else {
3508 if ((!devip->used) && (!open_devip))
3509 open_devip = devip;
3510 }
3511 }
FUJITA Tomonori5cb2fc02008-03-20 11:09:16 +09003512 if (!open_devip) { /* try and make a new one */
3513 open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC);
3514 if (!open_devip) {
Tomas Winklerc12879702015-07-28 16:54:20 +03003515 pr_err("out of memory at line %d\n", __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 return NULL;
3517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 }
FUJITA Tomonoria75869d2008-03-20 11:09:17 +09003519
3520 open_devip->channel = sdev->channel;
3521 open_devip->target = sdev->id;
3522 open_devip->lun = sdev->lun;
3523 open_devip->sdbg_host = sdbg_host;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003524 atomic_set(&open_devip->num_in_q, 0);
3525 set_bit(SDEBUG_UA_POR, open_devip->uas_bm);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003526 open_devip->used = true;
FUJITA Tomonoria75869d2008-03-20 11:09:17 +09003527 return open_devip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528}
3529
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003530static int scsi_debug_slave_alloc(struct scsi_device *sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531{
Douglas Gilbert773642d2016-04-25 12:16:28 -04003532 if (sdebug_verbose)
Tomas Winklerc12879702015-07-28 16:54:20 +03003533 pr_info("slave_alloc <%u %u %u %llu>\n",
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003534 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
Nick Piggin75ad23b2008-04-29 14:48:33 +02003535 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, sdp->request_queue);
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003536 return 0;
3537}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003539static int scsi_debug_slave_configure(struct scsi_device *sdp)
3540{
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04003541 struct sdebug_dev_info *devip =
3542 (struct sdebug_dev_info *)sdp->hostdata;
FUJITA Tomonoria34c4e92008-03-25 09:26:50 +09003543
Douglas Gilbert773642d2016-04-25 12:16:28 -04003544 if (sdebug_verbose)
Tomas Winklerc12879702015-07-28 16:54:20 +03003545 pr_info("slave_configure <%u %u %u %llu>\n",
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003546 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04003547 if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN)
3548 sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN;
3549 if (devip == NULL) {
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04003550 devip = find_build_dev_info(sdp);
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04003551 if (devip == NULL)
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04003552 return 1; /* no resources, will be marked offline */
3553 }
Christoph Hellwigc8b09f62014-11-03 20:15:14 +01003554 sdp->hostdata = devip;
Akinobu Mita6bb5e6e2014-06-02 22:56:49 +09003555 blk_queue_max_segment_size(sdp->request_queue, -1U);
Douglas Gilbert773642d2016-04-25 12:16:28 -04003556 if (sdebug_no_uld)
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04003557 sdp->no_uld_attach = 1;
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003558 return 0;
3559}
3560
3561static void scsi_debug_slave_destroy(struct scsi_device *sdp)
3562{
3563 struct sdebug_dev_info *devip =
3564 (struct sdebug_dev_info *)sdp->hostdata;
3565
Douglas Gilbert773642d2016-04-25 12:16:28 -04003566 if (sdebug_verbose)
Tomas Winklerc12879702015-07-28 16:54:20 +03003567 pr_info("slave_destroy <%u %u %u %llu>\n",
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003568 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
3569 if (devip) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003570 /* make this slot available for re-use */
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05003571 devip->used = false;
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003572 sdp->hostdata = NULL;
3573 }
3574}
3575
Douglas Gilbertc4837392016-05-06 00:40:26 -04003576static void stop_qc_helper(struct sdebug_defer *sd_dp)
3577{
3578 if (!sd_dp)
3579 return;
3580 if ((sdebug_jdelay > 0) || (sdebug_ndelay > 0))
3581 hrtimer_cancel(&sd_dp->hrt);
3582 else if (sdebug_jdelay < 0)
3583 cancel_work_sync(&sd_dp->ew.work);
3584}
3585
Douglas Gilberta10bc122016-04-25 12:16:32 -04003586/* If @cmnd found deletes its timer or work queue and returns true; else
3587 returns false */
3588static bool stop_queued_cmnd(struct scsi_cmnd *cmnd)
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003589{
3590 unsigned long iflags;
Douglas Gilbertc4837392016-05-06 00:40:26 -04003591 int j, k, qmax, r_qmax;
3592 struct sdebug_queue *sqp;
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003593 struct sdebug_queued_cmd *sqcp;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003594 struct sdebug_dev_info *devip;
Douglas Gilberta10bc122016-04-25 12:16:32 -04003595 struct sdebug_defer *sd_dp;
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003596
Douglas Gilbertc4837392016-05-06 00:40:26 -04003597 for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
3598 spin_lock_irqsave(&sqp->qc_lock, iflags);
3599 qmax = sdebug_max_queue;
3600 r_qmax = atomic_read(&retired_max_queue);
3601 if (r_qmax > qmax)
3602 qmax = r_qmax;
3603 for (k = 0; k < qmax; ++k) {
3604 if (test_bit(k, sqp->in_use_bm)) {
3605 sqcp = &sqp->qc_arr[k];
3606 if (cmnd != sqcp->a_cmnd)
3607 continue;
3608 /* found */
3609 devip = (struct sdebug_dev_info *)
3610 cmnd->device->hostdata;
3611 if (devip)
3612 atomic_dec(&devip->num_in_q);
3613 sqcp->a_cmnd = NULL;
3614 sd_dp = sqcp->sd_dp;
3615 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
3616 stop_qc_helper(sd_dp);
3617 clear_bit(k, sqp->in_use_bm);
3618 return true;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003619 }
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003620 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04003621 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003622 }
Douglas Gilberta10bc122016-04-25 12:16:32 -04003623 return false;
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003624}
3625
Douglas Gilberta10bc122016-04-25 12:16:32 -04003626/* Deletes (stops) timers or work queues of all queued commands */
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003627static void stop_all_queued(void)
3628{
3629 unsigned long iflags;
Douglas Gilbertc4837392016-05-06 00:40:26 -04003630 int j, k;
3631 struct sdebug_queue *sqp;
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003632 struct sdebug_queued_cmd *sqcp;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003633 struct sdebug_dev_info *devip;
Douglas Gilberta10bc122016-04-25 12:16:32 -04003634 struct sdebug_defer *sd_dp;
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003635
Douglas Gilbertc4837392016-05-06 00:40:26 -04003636 for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
3637 spin_lock_irqsave(&sqp->qc_lock, iflags);
3638 for (k = 0; k < SDEBUG_CANQUEUE; ++k) {
3639 if (test_bit(k, sqp->in_use_bm)) {
3640 sqcp = &sqp->qc_arr[k];
3641 if (sqcp->a_cmnd == NULL)
3642 continue;
3643 devip = (struct sdebug_dev_info *)
3644 sqcp->a_cmnd->device->hostdata;
3645 if (devip)
3646 atomic_dec(&devip->num_in_q);
3647 sqcp->a_cmnd = NULL;
3648 sd_dp = sqcp->sd_dp;
3649 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
3650 stop_qc_helper(sd_dp);
3651 clear_bit(k, sqp->in_use_bm);
3652 spin_lock_irqsave(&sqp->qc_lock, iflags);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003653 }
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003654 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04003655 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09003656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657}
3658
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003659/* Free queued command memory on heap */
3660static void free_all_queued(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661{
Douglas Gilbertc4837392016-05-06 00:40:26 -04003662 int j, k;
3663 struct sdebug_queue *sqp;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003664 struct sdebug_queued_cmd *sqcp;
3665
Douglas Gilbertc4837392016-05-06 00:40:26 -04003666 for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
3667 for (k = 0; k < SDEBUG_CANQUEUE; ++k) {
3668 sqcp = &sqp->qc_arr[k];
3669 kfree(sqcp->sd_dp);
3670 sqcp->sd_dp = NULL;
3671 }
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673}
3674
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003675static int scsi_debug_abort(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676{
Douglas Gilberta10bc122016-04-25 12:16:32 -04003677 bool ok;
3678
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003679 ++num_aborts;
3680 if (SCpnt) {
Douglas Gilberta10bc122016-04-25 12:16:32 -04003681 ok = stop_queued_cmnd(SCpnt);
3682 if (SCpnt->device && (SDEBUG_OPT_ALL_NOISE & sdebug_opts))
3683 sdev_printk(KERN_INFO, SCpnt->device,
3684 "%s: command%s found\n", __func__,
3685 ok ? "" : " not");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 }
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003687 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688}
3689
3690static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt)
3691{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 ++num_dev_resets;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003693 if (SCpnt && SCpnt->device) {
3694 struct scsi_device *sdp = SCpnt->device;
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04003695 struct sdebug_dev_info *devip =
3696 (struct sdebug_dev_info *)sdp->hostdata;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003697
Douglas Gilbert773642d2016-04-25 12:16:28 -04003698 if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003699 sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 if (devip)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003701 set_bit(SDEBUG_UA_POR, devip->uas_bm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 }
3703 return SUCCESS;
3704}
3705
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003706static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt)
3707{
3708 struct sdebug_host_info *sdbg_host;
3709 struct sdebug_dev_info *devip;
3710 struct scsi_device *sdp;
3711 struct Scsi_Host *hp;
3712 int k = 0;
3713
3714 ++num_target_resets;
3715 if (!SCpnt)
3716 goto lie;
3717 sdp = SCpnt->device;
3718 if (!sdp)
3719 goto lie;
Douglas Gilbert773642d2016-04-25 12:16:28 -04003720 if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003721 sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
3722 hp = sdp->host;
3723 if (!hp)
3724 goto lie;
3725 sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
3726 if (sdbg_host) {
3727 list_for_each_entry(devip,
3728 &sdbg_host->dev_info_list,
3729 dev_list)
3730 if (devip->target == sdp->id) {
3731 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
3732 ++k;
3733 }
3734 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04003735 if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003736 sdev_printk(KERN_INFO, sdp,
3737 "%s: %d device(s) found in target\n", __func__, k);
3738lie:
3739 return SUCCESS;
3740}
3741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt)
3743{
3744 struct sdebug_host_info *sdbg_host;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003745 struct sdebug_dev_info *devip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 struct scsi_device * sdp;
3747 struct Scsi_Host * hp;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003748 int k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 ++num_bus_resets;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003751 if (!(SCpnt && SCpnt->device))
3752 goto lie;
3753 sdp = SCpnt->device;
Douglas Gilbert773642d2016-04-25 12:16:28 -04003754 if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003755 sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
3756 hp = sdp->host;
3757 if (hp) {
FUJITA Tomonorid1e4c9c2008-03-02 18:30:18 +09003758 sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 if (sdbg_host) {
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003760 list_for_each_entry(devip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 &sdbg_host->dev_info_list,
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003762 dev_list) {
3763 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
3764 ++k;
3765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 }
3767 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04003768 if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003769 sdev_printk(KERN_INFO, sdp,
3770 "%s: %d device(s) found in host\n", __func__, k);
3771lie:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 return SUCCESS;
3773}
3774
3775static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt)
3776{
3777 struct sdebug_host_info * sdbg_host;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003778 struct sdebug_dev_info *devip;
3779 int k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 ++num_host_resets;
Douglas Gilbert773642d2016-04-25 12:16:28 -04003782 if ((SCpnt->device) && (SDEBUG_OPT_ALL_NOISE & sdebug_opts))
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003783 sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 spin_lock(&sdebug_host_list_lock);
3785 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003786 list_for_each_entry(devip, &sdbg_host->dev_info_list,
3787 dev_list) {
3788 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
3789 ++k;
3790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 }
3792 spin_unlock(&sdebug_host_list_lock);
3793 stop_all_queued();
Douglas Gilbert773642d2016-04-25 12:16:28 -04003794 if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003795 sdev_printk(KERN_INFO, SCpnt->device,
3796 "%s: %d device(s) found\n", __func__, k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 return SUCCESS;
3798}
3799
FUJITA Tomonorif58b0ef2008-03-30 00:59:54 +09003800static void __init sdebug_build_parts(unsigned char *ramp,
FUJITA Tomonori5f2578e2008-03-30 00:59:57 +09003801 unsigned long store_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802{
3803 struct partition * pp;
3804 int starts[SDEBUG_MAX_PARTS + 2];
3805 int sectors_per_part, num_sectors, k;
3806 int heads_by_sects, start_sec, end_sec;
3807
3808 /* assume partition table already zeroed */
Douglas Gilbert773642d2016-04-25 12:16:28 -04003809 if ((sdebug_num_parts < 1) || (store_size < 1048576))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 return;
Douglas Gilbert773642d2016-04-25 12:16:28 -04003811 if (sdebug_num_parts > SDEBUG_MAX_PARTS) {
3812 sdebug_num_parts = SDEBUG_MAX_PARTS;
Tomas Winklerc12879702015-07-28 16:54:20 +03003813 pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 }
Douglas Gilbertc65b1442006-06-06 00:11:24 -04003815 num_sectors = (int)sdebug_store_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 sectors_per_part = (num_sectors - sdebug_sectors_per)
Douglas Gilbert773642d2016-04-25 12:16:28 -04003817 / sdebug_num_parts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 heads_by_sects = sdebug_heads * sdebug_sectors_per;
3819 starts[0] = sdebug_sectors_per;
Douglas Gilbert773642d2016-04-25 12:16:28 -04003820 for (k = 1; k < sdebug_num_parts; ++k)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 starts[k] = ((k * sectors_per_part) / heads_by_sects)
3822 * heads_by_sects;
Douglas Gilbert773642d2016-04-25 12:16:28 -04003823 starts[sdebug_num_parts] = num_sectors;
3824 starts[sdebug_num_parts + 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
3826 ramp[510] = 0x55; /* magic partition markings */
3827 ramp[511] = 0xAA;
3828 pp = (struct partition *)(ramp + 0x1be);
3829 for (k = 0; starts[k + 1]; ++k, ++pp) {
3830 start_sec = starts[k];
3831 end_sec = starts[k + 1] - 1;
3832 pp->boot_ind = 0;
3833
3834 pp->cyl = start_sec / heads_by_sects;
3835 pp->head = (start_sec - (pp->cyl * heads_by_sects))
3836 / sdebug_sectors_per;
3837 pp->sector = (start_sec % sdebug_sectors_per) + 1;
3838
3839 pp->end_cyl = end_sec / heads_by_sects;
3840 pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
3841 / sdebug_sectors_per;
3842 pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
3843
Akinobu Mita150c3542013-08-26 22:08:40 +09003844 pp->start_sect = cpu_to_le32(start_sec);
3845 pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 pp->sys_ind = 0x83; /* plain Linux partition */
3847 }
3848}
3849
Douglas Gilbertc4837392016-05-06 00:40:26 -04003850static void block_unblock_all_queues(bool block)
3851{
3852 int j;
3853 struct sdebug_queue *sqp;
3854
3855 for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp)
3856 atomic_set(&sqp->blocked, (int)block);
3857}
3858
3859/* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1
3860 * commands will be processed normally before triggers occur.
3861 */
3862static void tweak_cmnd_count(void)
3863{
3864 int count, modulo;
3865
3866 modulo = abs(sdebug_every_nth);
3867 if (modulo < 2)
3868 return;
3869 block_unblock_all_queues(true);
3870 count = atomic_read(&sdebug_cmnd_count);
3871 atomic_set(&sdebug_cmnd_count, (count / modulo) * modulo);
3872 block_unblock_all_queues(false);
3873}
3874
3875static void clear_queue_stats(void)
3876{
3877 atomic_set(&sdebug_cmnd_count, 0);
3878 atomic_set(&sdebug_completions, 0);
3879 atomic_set(&sdebug_miss_cpus, 0);
3880 atomic_set(&sdebug_a_tsf, 0);
3881}
3882
3883static void setup_inject(struct sdebug_queue *sqp,
3884 struct sdebug_queued_cmd *sqcp)
3885{
3886 if ((atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) > 0)
3887 return;
3888 sqcp->inj_recovered = !!(SDEBUG_OPT_RECOVERED_ERR & sdebug_opts);
3889 sqcp->inj_transport = !!(SDEBUG_OPT_TRANSPORT_ERR & sdebug_opts);
3890 sqcp->inj_dif = !!(SDEBUG_OPT_DIF_ERR & sdebug_opts);
3891 sqcp->inj_dix = !!(SDEBUG_OPT_DIX_ERR & sdebug_opts);
3892 sqcp->inj_short = !!(SDEBUG_OPT_SHORT_TRANSFER & sdebug_opts);
3893}
3894
3895/* Complete the processing of the thread that queued a SCSI command to this
3896 * driver. It either completes the command by calling cmnd_done() or
3897 * schedules a hr timer or work queue then returns 0. Returns
3898 * SCSI_MLQUEUE_HOST_BUSY if temporarily out of resources.
3899 */
Douglas Gilbertfd321192016-04-25 12:16:33 -04003900static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip,
3901 int scsi_result, int delta_jiff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902{
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003903 unsigned long iflags;
Douglas Gilbertcd62b7d2014-08-05 12:20:46 +02003904 int k, num_in_q, qdepth, inject;
Douglas Gilbertc4837392016-05-06 00:40:26 -04003905 struct sdebug_queue *sqp;
3906 struct sdebug_queued_cmd *sqcp;
Tomas Winkler299b6c02015-07-28 16:54:24 +03003907 struct scsi_device *sdp;
Douglas Gilberta10bc122016-04-25 12:16:32 -04003908 struct sdebug_defer *sd_dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04003910 if (unlikely(devip == NULL)) {
3911 if (scsi_result == 0)
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04003912 scsi_result = DID_NO_CONNECT << 16;
3913 goto respond_in_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 }
Tomas Winkler299b6c02015-07-28 16:54:24 +03003915 sdp = cmnd->device;
3916
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04003917 if (unlikely(sdebug_verbose && scsi_result))
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003918 sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n",
3919 __func__, scsi_result);
Douglas Gilbertcd62b7d2014-08-05 12:20:46 +02003920 if (delta_jiff == 0)
3921 goto respond_in_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Douglas Gilbertcd62b7d2014-08-05 12:20:46 +02003923 /* schedule the response at a later time if resources permit */
Douglas Gilbertc4837392016-05-06 00:40:26 -04003924 sqp = get_queue(cmnd);
3925 spin_lock_irqsave(&sqp->qc_lock, iflags);
3926 if (unlikely(atomic_read(&sqp->blocked))) {
3927 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
3928 return SCSI_MLQUEUE_HOST_BUSY;
3929 }
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003930 num_in_q = atomic_read(&devip->num_in_q);
3931 qdepth = cmnd->device->queue_depth;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003932 inject = 0;
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04003933 if (unlikely((qdepth > 0) && (num_in_q >= qdepth))) {
Douglas Gilbertcd62b7d2014-08-05 12:20:46 +02003934 if (scsi_result) {
Douglas Gilbertc4837392016-05-06 00:40:26 -04003935 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
Douglas Gilbertcd62b7d2014-08-05 12:20:46 +02003936 goto respond_in_thread;
3937 } else
3938 scsi_result = device_qfull_result;
Douglas Gilbertc4837392016-05-06 00:40:26 -04003939 } else if (unlikely(sdebug_every_nth &&
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04003940 (SDEBUG_OPT_RARE_TSF & sdebug_opts) &&
3941 (scsi_result == 0))) {
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003942 if ((num_in_q == (qdepth - 1)) &&
3943 (atomic_inc_return(&sdebug_a_tsf) >=
Douglas Gilbert773642d2016-04-25 12:16:28 -04003944 abs(sdebug_every_nth))) {
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003945 atomic_set(&sdebug_a_tsf, 0);
3946 inject = 1;
Douglas Gilbertcd62b7d2014-08-05 12:20:46 +02003947 scsi_result = device_qfull_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 }
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003949 }
3950
Douglas Gilbertc4837392016-05-06 00:40:26 -04003951 k = find_first_zero_bit(sqp->in_use_bm, sdebug_max_queue);
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04003952 if (unlikely(k >= sdebug_max_queue)) {
Douglas Gilbertc4837392016-05-06 00:40:26 -04003953 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
Douglas Gilbertcd62b7d2014-08-05 12:20:46 +02003954 if (scsi_result)
3955 goto respond_in_thread;
Douglas Gilbert773642d2016-04-25 12:16:28 -04003956 else if (SDEBUG_OPT_ALL_TSF & sdebug_opts)
Douglas Gilbertcd62b7d2014-08-05 12:20:46 +02003957 scsi_result = device_qfull_result;
Douglas Gilbert773642d2016-04-25 12:16:28 -04003958 if (SDEBUG_OPT_Q_NOISE & sdebug_opts)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003959 sdev_printk(KERN_INFO, sdp,
Douglas Gilbertcd62b7d2014-08-05 12:20:46 +02003960 "%s: max_queue=%d exceeded, %s\n",
Douglas Gilbert773642d2016-04-25 12:16:28 -04003961 __func__, sdebug_max_queue,
Douglas Gilbertcd62b7d2014-08-05 12:20:46 +02003962 (scsi_result ? "status: TASK SET FULL" :
3963 "report: host busy"));
3964 if (scsi_result)
3965 goto respond_in_thread;
3966 else
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003967 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04003969 __set_bit(k, sqp->in_use_bm);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003970 atomic_inc(&devip->num_in_q);
Douglas Gilbertc4837392016-05-06 00:40:26 -04003971 sqcp = &sqp->qc_arr[k];
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003972 sqcp->a_cmnd = cmnd;
Douglas Gilbertc4837392016-05-06 00:40:26 -04003973 cmnd->host_scribble = (unsigned char *)sqcp;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003974 cmnd->result = scsi_result;
Douglas Gilberta10bc122016-04-25 12:16:32 -04003975 sd_dp = sqcp->sd_dp;
Douglas Gilbertc4837392016-05-06 00:40:26 -04003976 spin_unlock_irqrestore(&sqp->qc_lock, iflags);
3977 if (unlikely(sdebug_every_nth && sdebug_any_injecting_opt))
3978 setup_inject(sqp, sqcp);
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04003979 if (delta_jiff > 0 || sdebug_ndelay > 0) {
Douglas Gilbertb333a812016-04-25 12:16:30 -04003980 ktime_t kt;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003981
Douglas Gilbertb333a812016-04-25 12:16:30 -04003982 if (delta_jiff > 0) {
3983 struct timespec ts;
3984
3985 jiffies_to_timespec(delta_jiff, &ts);
3986 kt = ktime_set(ts.tv_sec, ts.tv_nsec);
3987 } else
3988 kt = ktime_set(0, sdebug_ndelay);
Douglas Gilberta10bc122016-04-25 12:16:32 -04003989 if (NULL == sd_dp) {
3990 sd_dp = kzalloc(sizeof(*sd_dp), GFP_ATOMIC);
3991 if (NULL == sd_dp)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003992 return SCSI_MLQUEUE_HOST_BUSY;
Douglas Gilberta10bc122016-04-25 12:16:32 -04003993 sqcp->sd_dp = sd_dp;
3994 hrtimer_init(&sd_dp->hrt, CLOCK_MONOTONIC,
Douglas Gilbertc4837392016-05-06 00:40:26 -04003995 HRTIMER_MODE_REL_PINNED);
Douglas Gilberta10bc122016-04-25 12:16:32 -04003996 sd_dp->hrt.function = sdebug_q_cmd_hrt_complete;
Douglas Gilbertc4837392016-05-06 00:40:26 -04003997 sd_dp->sqa_idx = sqp - sdebug_q_arr;
3998 sd_dp->qc_idx = k;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04003999 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04004000 if (sdebug_statistics)
4001 sd_dp->issuing_cpu = raw_smp_processor_id();
4002 hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL_PINNED);
4003 } else { /* jdelay < 0, use work queue */
Douglas Gilberta10bc122016-04-25 12:16:32 -04004004 if (NULL == sd_dp) {
4005 sd_dp = kzalloc(sizeof(*sqcp->sd_dp), GFP_ATOMIC);
4006 if (NULL == sd_dp)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004007 return SCSI_MLQUEUE_HOST_BUSY;
Douglas Gilberta10bc122016-04-25 12:16:32 -04004008 sqcp->sd_dp = sd_dp;
Douglas Gilbertc4837392016-05-06 00:40:26 -04004009 sd_dp->sqa_idx = sqp - sdebug_q_arr;
4010 sd_dp->qc_idx = k;
Douglas Gilberta10bc122016-04-25 12:16:32 -04004011 INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004012 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04004013 if (sdebug_statistics)
4014 sd_dp->issuing_cpu = raw_smp_processor_id();
Douglas Gilberta10bc122016-04-25 12:16:32 -04004015 schedule_work(&sd_dp->ew.work);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004016 }
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04004017 if (unlikely((SDEBUG_OPT_Q_NOISE & sdebug_opts) &&
4018 (scsi_result == device_qfull_result)))
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004019 sdev_printk(KERN_INFO, sdp,
4020 "%s: num_in_q=%d +1, %s%s\n", __func__,
4021 num_in_q, (inject ? "<inject> " : ""),
4022 "status: TASK SET FULL");
4023 return 0;
Douglas Gilbertcd62b7d2014-08-05 12:20:46 +02004024
4025respond_in_thread: /* call back to mid-layer using invocation thread */
4026 cmnd->result = scsi_result;
4027 cmnd->scsi_done(cmnd);
4028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029}
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004030
Douglas Gilbert23183912006-09-16 20:30:47 -04004031/* Note: The following macros create attribute files in the
4032 /sys/module/scsi_debug/parameters directory. Unfortunately this
4033 driver is unaware of a change and cannot trigger auxiliary actions
4034 as it can when the corresponding attribute in the
4035 /sys/bus/pseudo/drivers/scsi_debug directory is changed.
4036 */
Douglas Gilbert773642d2016-04-25 12:16:28 -04004037module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR);
4038module_param_named(ato, sdebug_ato, int, S_IRUGO);
4039module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR);
Douglas Gilbertc2206092016-04-25 12:16:31 -04004040module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR);
Douglas Gilbert773642d2016-04-25 12:16:28 -04004041module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO);
4042module_param_named(dif, sdebug_dif, int, S_IRUGO);
4043module_param_named(dix, sdebug_dix, int, S_IRUGO);
4044module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR);
4045module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR);
4046module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR);
4047module_param_named(guard, sdebug_guard, uint, S_IRUGO);
4048module_param_named(host_lock, sdebug_host_lock, bool, S_IRUGO | S_IWUSR);
4049module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO);
4050module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO);
4051module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO);
4052module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO);
4053module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO);
4054module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR);
4055module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR);
4056module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR);
4057module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR);
4058module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO);
4059module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO);
4060module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR);
4061module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO);
4062module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR);
4063module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO);
4064module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR);
4065module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR);
4066module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO);
4067module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO);
Douglas Gilbertc4837392016-05-06 00:40:26 -04004068module_param_named(statistics, sdebug_statistics, bool, S_IRUGO | S_IWUSR);
Douglas Gilbert773642d2016-04-25 12:16:28 -04004069module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR);
Douglas Gilbertc4837392016-05-06 00:40:26 -04004070module_param_named(submit_queues, submit_queues, int, S_IRUGO);
Douglas Gilbert773642d2016-04-25 12:16:28 -04004071module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO);
4072module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO);
4073module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO);
4074module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO);
4075module_param_named(virtual_gb, sdebug_virtual_gb, int, S_IRUGO | S_IWUSR);
4076module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int,
Douglas Gilbert23183912006-09-16 20:30:47 -04004077 S_IRUGO | S_IWUSR);
Douglas Gilbert773642d2016-04-25 12:16:28 -04004078module_param_named(write_same_length, sdebug_write_same_length, int,
Martin K. Petersen5b94e232011-03-08 02:08:11 -05004079 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
4081MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
4082MODULE_DESCRIPTION("SCSI debug adapter driver");
4083MODULE_LICENSE("GPL");
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04004084MODULE_VERSION(SDEBUG_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
4086MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
Martin K. Petersen5b94e232011-03-08 02:08:11 -05004087MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)");
Akinobu Mita0759c662014-02-26 22:57:04 +09004088MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)");
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004089MODULE_PARM_DESC(delay, "response delay (def=1 jiffy); 0:imm, -1,-2:tiny");
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05004090MODULE_PARM_DESC(dev_size_mb, "size in MiB of ram shared by devs(def=8)");
Martin K. Petersen5b94e232011-03-08 02:08:11 -05004091MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)");
4092MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)");
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004093MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
Randy Dunlapbeb87c32007-06-11 11:36:40 -07004094MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)");
Douglas Gilbert23183912006-09-16 20:30:47 -04004095MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
Martin K. Petersen5b94e232011-03-08 02:08:11 -05004096MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)");
Douglas Gilbert185dd232016-04-25 12:16:29 -04004097MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)");
Martin K. Petersen5b94e232011-03-08 02:08:11 -05004098MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)");
4099MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)");
4100MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)");
Eric Sandeenbe1dd782012-03-08 00:03:59 -06004101MODULE_PARM_DESC(lbprz, "unmapped blocks return 0 on read (def=1)");
Martin K. Petersen5b94e232011-03-08 02:08:11 -05004102MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)");
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004103MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004104MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))");
4105MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)");
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004106MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04004107MODULE_PARM_DESC(no_uld, "stop ULD (e.g. sd driver) attaching (def=0))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004109MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
Martin K. Petersen32c58442015-12-16 17:53:51 -05004110MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)");
Douglas Gilbert6f3cbf52007-01-05 00:05:25 -05004111MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)");
Martin K. Petersen5b94e232011-03-08 02:08:11 -05004112MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
Martin Pittd9867882012-09-06 12:04:33 +02004114MODULE_PARM_DESC(removable, "claim to have removable media (def=0)");
Douglas Gilberte46b0342014-08-05 12:21:53 +02004115MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=6[SPC-4])");
Martin K. Petersenea61fca2009-05-15 00:40:33 -04004116MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)");
Douglas Gilbertc4837392016-05-06 00:40:26 -04004117MODULE_PARM_DESC(statistics, "collect statistics on commands, queues (def=0)");
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05004118MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)");
Douglas Gilbertc4837392016-05-06 00:40:26 -04004119MODULE_PARM_DESC(submit_queues, "support for block multi-queue (def=1)");
Martin K. Petersen5b94e232011-03-08 02:08:11 -05004120MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)");
4121MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)");
Martin K. Petersen60147592010-08-19 11:49:00 -04004122MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)");
4123MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)");
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05004124MODULE_PARM_DESC(virtual_gb, "virtual gigabyte (GiB) size (def=0 -> use dev_size_mb)");
Martin K. Petersen5b94e232011-03-08 02:08:11 -05004125MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
4126MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
4128static char sdebug_info[256];
4129
4130static const char * scsi_debug_info(struct Scsi_Host * shp)
4131{
Douglas Gilbertc4837392016-05-06 00:40:26 -04004132 int k;
4133
4134 k = scnprintf(sdebug_info, sizeof(sdebug_info),
4135 "%s: version %s [%s], dev_size_mb=%d, opts=0x%x\n",
4136 my_name, SDEBUG_VERSION, sdebug_version_date,
4137 sdebug_dev_size_mb, sdebug_opts);
4138 if (k >= (sizeof(sdebug_info) - 1))
4139 return sdebug_info;
4140 scnprintf(sdebug_info + k, sizeof(sdebug_info) - k,
4141 "%s: submit_queues=%d, statistics=%d\n", my_name,
4142 submit_queues, (int)sdebug_statistics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 return sdebug_info;
4144}
4145
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004146/* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */
Douglas Gilbertfd321192016-04-25 12:16:33 -04004147static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer,
4148 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149{
Al Viroc8ed5552013-03-31 01:46:06 -04004150 char arr[16];
4151 int opts;
4152 int minLen = length > 15 ? 15 : length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
Al Viroc8ed5552013-03-31 01:46:06 -04004154 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
4155 return -EACCES;
4156 memcpy(arr, buffer, minLen);
4157 arr[minLen] = '\0';
4158 if (1 != sscanf(arr, "%d", &opts))
4159 return -EINVAL;
Douglas Gilbert773642d2016-04-25 12:16:28 -04004160 sdebug_opts = opts;
4161 sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
4162 sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts);
4163 if (sdebug_every_nth != 0)
Douglas Gilbertc4837392016-05-06 00:40:26 -04004164 tweak_cmnd_count();
Al Viroc8ed5552013-03-31 01:46:06 -04004165 return length;
4166}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004168/* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the
4169 * same for each scsi_debug host (if more than one). Some of the counters
4170 * output are not atomics so might be inaccurate in a busy system. */
Al Viroc8ed5552013-03-31 01:46:06 -04004171static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host)
4172{
Douglas Gilbertc4837392016-05-06 00:40:26 -04004173 int f, j, l;
4174 struct sdebug_queue *sqp;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004175
Douglas Gilbertc4837392016-05-06 00:40:26 -04004176 seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n",
4177 SDEBUG_VERSION, sdebug_version_date);
4178 seq_printf(m, "num_tgts=%d, %ssize=%d MB, opts=0x%x, every_nth=%d\n",
4179 sdebug_num_tgts, "shared (ram) ", sdebug_dev_size_mb,
4180 sdebug_opts, sdebug_every_nth);
4181 seq_printf(m, "delay=%d, ndelay=%d, max_luns=%d, sector_size=%d %s\n",
4182 sdebug_jdelay, sdebug_ndelay, sdebug_max_luns,
4183 sdebug_sector_size, "bytes");
4184 seq_printf(m, "cylinders=%d, heads=%d, sectors=%d, command aborts=%d\n",
4185 sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
4186 num_aborts);
4187 seq_printf(m, "RESETs: device=%d, target=%d, bus=%d, host=%d\n",
4188 num_dev_resets, num_target_resets, num_bus_resets,
4189 num_host_resets);
4190 seq_printf(m, "dix_reads=%d, dix_writes=%d, dif_errors=%d\n",
4191 dix_reads, dix_writes, dif_errors);
4192 seq_printf(m, "usec_in_jiffy=%lu, %s=%d, mq_active=%d\n",
4193 TICK_NSEC / 1000, "statistics", sdebug_statistics,
4194 sdebug_mq_active);
4195 seq_printf(m, "cmnd_count=%d, completions=%d, %s=%d, a_tsf=%d\n",
4196 atomic_read(&sdebug_cmnd_count),
4197 atomic_read(&sdebug_completions),
4198 "miss_cpus", atomic_read(&sdebug_miss_cpus),
4199 atomic_read(&sdebug_a_tsf));
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004200
Douglas Gilbertc4837392016-05-06 00:40:26 -04004201 seq_printf(m, "submit_queues=%d\n", submit_queues);
4202 for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
4203 seq_printf(m, " queue %d:\n", j);
4204 f = find_first_bit(sqp->in_use_bm, sdebug_max_queue);
4205 if (f != sdebug_max_queue) {
4206 l = find_last_bit(sqp->in_use_bm, sdebug_max_queue);
4207 seq_printf(m, " in_use_bm BUSY: %s: %d,%d\n",
4208 "first,last bits", f, l);
4209 }
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004210 }
Al Viroc8ed5552013-03-31 01:46:06 -04004211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212}
4213
Akinobu Mita82069372013-10-14 22:48:04 +09004214static ssize_t delay_show(struct device_driver *ddp, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215{
Douglas Gilbertc2206092016-04-25 12:16:31 -04004216 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217}
Douglas Gilbertc4837392016-05-06 00:40:26 -04004218/* Returns -EBUSY if jdelay is being changed and commands are queued. The unit
4219 * of delay is jiffies.
4220 */
Akinobu Mita82069372013-10-14 22:48:04 +09004221static ssize_t delay_store(struct device_driver *ddp, const char *buf,
4222 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223{
Douglas Gilbertc2206092016-04-25 12:16:31 -04004224 int jdelay, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04004226 if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) {
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004227 res = count;
Douglas Gilbertc2206092016-04-25 12:16:31 -04004228 if (sdebug_jdelay != jdelay) {
Douglas Gilbertc4837392016-05-06 00:40:26 -04004229 int j, k;
4230 struct sdebug_queue *sqp;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004231
Douglas Gilbertc4837392016-05-06 00:40:26 -04004232 block_unblock_all_queues(true);
4233 for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
4234 ++j, ++sqp) {
4235 k = find_first_bit(sqp->in_use_bm,
4236 sdebug_max_queue);
4237 if (k != sdebug_max_queue) {
4238 res = -EBUSY; /* queued commands */
4239 break;
4240 }
4241 }
4242 if (res > 0) {
Douglas Gilberta10bc122016-04-25 12:16:32 -04004243 /* make sure sdebug_defer instances get
4244 * re-allocated for new delay variant */
4245 free_all_queued();
Douglas Gilbertc2206092016-04-25 12:16:31 -04004246 sdebug_jdelay = jdelay;
Douglas Gilbert773642d2016-04-25 12:16:28 -04004247 sdebug_ndelay = 0;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004248 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04004249 block_unblock_all_queues(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 }
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004251 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 }
4253 return -EINVAL;
4254}
Akinobu Mita82069372013-10-14 22:48:04 +09004255static DRIVER_ATTR_RW(delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004257static ssize_t ndelay_show(struct device_driver *ddp, char *buf)
4258{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004259 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004260}
4261/* Returns -EBUSY if ndelay is being changed and commands are queued */
Douglas Gilbertc2206092016-04-25 12:16:31 -04004262/* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004263static ssize_t ndelay_store(struct device_driver *ddp, const char *buf,
Douglas Gilbertfd321192016-04-25 12:16:33 -04004264 size_t count)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004265{
Douglas Gilbertc4837392016-05-06 00:40:26 -04004266 int ndelay, res;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004267
4268 if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) &&
Douglas Gilbertc4837392016-05-06 00:40:26 -04004269 (ndelay >= 0) && (ndelay < (1000 * 1000 * 1000))) {
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004270 res = count;
Douglas Gilbert773642d2016-04-25 12:16:28 -04004271 if (sdebug_ndelay != ndelay) {
Douglas Gilbertc4837392016-05-06 00:40:26 -04004272 int j, k;
4273 struct sdebug_queue *sqp;
4274
4275 block_unblock_all_queues(true);
4276 for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
4277 ++j, ++sqp) {
4278 k = find_first_bit(sqp->in_use_bm,
4279 sdebug_max_queue);
4280 if (k != sdebug_max_queue) {
4281 res = -EBUSY; /* queued commands */
4282 break;
4283 }
4284 }
4285 if (res > 0) {
Douglas Gilberta10bc122016-04-25 12:16:32 -04004286 /* make sure sdebug_defer instances get
4287 * re-allocated for new delay variant */
4288 free_all_queued();
Douglas Gilbert773642d2016-04-25 12:16:28 -04004289 sdebug_ndelay = ndelay;
Douglas Gilbertc2206092016-04-25 12:16:31 -04004290 sdebug_jdelay = ndelay ? JDELAY_OVERRIDDEN
4291 : DEF_JDELAY;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004292 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04004293 block_unblock_all_queues(false);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004294 }
4295 return res;
4296 }
4297 return -EINVAL;
4298}
4299static DRIVER_ATTR_RW(ndelay);
4300
Akinobu Mita82069372013-10-14 22:48:04 +09004301static ssize_t opts_show(struct device_driver *ddp, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004303 return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304}
4305
Akinobu Mita82069372013-10-14 22:48:04 +09004306static ssize_t opts_store(struct device_driver *ddp, const char *buf,
4307 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
4309 int opts;
4310 char work[20];
4311
4312 if (1 == sscanf(buf, "%10s", work)) {
Rasmus Villemoes48a96872014-10-13 15:54:44 -07004313 if (0 == strncasecmp(work,"0x", 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 if (1 == sscanf(&work[2], "%x", &opts))
4315 goto opts_done;
4316 } else {
4317 if (1 == sscanf(work, "%d", &opts))
4318 goto opts_done;
4319 }
4320 }
4321 return -EINVAL;
4322opts_done:
Douglas Gilbert773642d2016-04-25 12:16:28 -04004323 sdebug_opts = opts;
4324 sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
4325 sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts);
Douglas Gilbertc4837392016-05-06 00:40:26 -04004326 tweak_cmnd_count();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 return count;
4328}
Akinobu Mita82069372013-10-14 22:48:04 +09004329static DRIVER_ATTR_RW(opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Akinobu Mita82069372013-10-14 22:48:04 +09004331static ssize_t ptype_show(struct device_driver *ddp, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004333 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334}
Akinobu Mita82069372013-10-14 22:48:04 +09004335static ssize_t ptype_store(struct device_driver *ddp, const char *buf,
4336 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
4338 int n;
4339
4340 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04004341 sdebug_ptype = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 return count;
4343 }
4344 return -EINVAL;
4345}
Akinobu Mita82069372013-10-14 22:48:04 +09004346static DRIVER_ATTR_RW(ptype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Akinobu Mita82069372013-10-14 22:48:04 +09004348static ssize_t dsense_show(struct device_driver *ddp, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004350 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351}
Akinobu Mita82069372013-10-14 22:48:04 +09004352static ssize_t dsense_store(struct device_driver *ddp, const char *buf,
4353 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354{
4355 int n;
4356
4357 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04004358 sdebug_dsense = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 return count;
4360 }
4361 return -EINVAL;
4362}
Akinobu Mita82069372013-10-14 22:48:04 +09004363static DRIVER_ATTR_RW(dsense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Akinobu Mita82069372013-10-14 22:48:04 +09004365static ssize_t fake_rw_show(struct device_driver *ddp, char *buf)
Douglas Gilbert23183912006-09-16 20:30:47 -04004366{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004367 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw);
Douglas Gilbert23183912006-09-16 20:30:47 -04004368}
Akinobu Mita82069372013-10-14 22:48:04 +09004369static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf,
4370 size_t count)
Douglas Gilbert23183912006-09-16 20:30:47 -04004371{
4372 int n;
4373
4374 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004375 n = (n > 0);
Douglas Gilbert773642d2016-04-25 12:16:28 -04004376 sdebug_fake_rw = (sdebug_fake_rw > 0);
4377 if (sdebug_fake_rw != n) {
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004378 if ((0 == n) && (NULL == fake_storep)) {
4379 unsigned long sz =
Douglas Gilbert773642d2016-04-25 12:16:28 -04004380 (unsigned long)sdebug_dev_size_mb *
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004381 1048576;
4382
4383 fake_storep = vmalloc(sz);
4384 if (NULL == fake_storep) {
Tomas Winklerc12879702015-07-28 16:54:20 +03004385 pr_err("out of memory, 9\n");
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004386 return -ENOMEM;
4387 }
4388 memset(fake_storep, 0, sz);
4389 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04004390 sdebug_fake_rw = n;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004391 }
Douglas Gilbert23183912006-09-16 20:30:47 -04004392 return count;
4393 }
4394 return -EINVAL;
4395}
Akinobu Mita82069372013-10-14 22:48:04 +09004396static DRIVER_ATTR_RW(fake_rw);
Douglas Gilbert23183912006-09-16 20:30:47 -04004397
Akinobu Mita82069372013-10-14 22:48:04 +09004398static ssize_t no_lun_0_show(struct device_driver *ddp, char *buf)
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004399{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004400 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004401}
Akinobu Mita82069372013-10-14 22:48:04 +09004402static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf,
4403 size_t count)
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004404{
4405 int n;
4406
4407 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04004408 sdebug_no_lun_0 = n;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004409 return count;
4410 }
4411 return -EINVAL;
4412}
Akinobu Mita82069372013-10-14 22:48:04 +09004413static DRIVER_ATTR_RW(no_lun_0);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004414
Akinobu Mita82069372013-10-14 22:48:04 +09004415static ssize_t num_tgts_show(struct device_driver *ddp, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004417 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418}
Akinobu Mita82069372013-10-14 22:48:04 +09004419static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf,
4420 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421{
4422 int n;
4423
4424 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04004425 sdebug_num_tgts = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 sdebug_max_tgts_luns();
4427 return count;
4428 }
4429 return -EINVAL;
4430}
Akinobu Mita82069372013-10-14 22:48:04 +09004431static DRIVER_ATTR_RW(num_tgts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Akinobu Mita82069372013-10-14 22:48:04 +09004433static ssize_t dev_size_mb_show(struct device_driver *ddp, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004435 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436}
Akinobu Mita82069372013-10-14 22:48:04 +09004437static DRIVER_ATTR_RO(dev_size_mb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
Akinobu Mita82069372013-10-14 22:48:04 +09004439static ssize_t num_parts_show(struct device_driver *ddp, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004441 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
Akinobu Mita82069372013-10-14 22:48:04 +09004443static DRIVER_ATTR_RO(num_parts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Akinobu Mita82069372013-10-14 22:48:04 +09004445static ssize_t every_nth_show(struct device_driver *ddp, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004447 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448}
Akinobu Mita82069372013-10-14 22:48:04 +09004449static ssize_t every_nth_store(struct device_driver *ddp, const char *buf,
4450 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
4452 int nth;
4453
4454 if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04004455 sdebug_every_nth = nth;
Douglas Gilbertc4837392016-05-06 00:40:26 -04004456 if (nth && !sdebug_statistics) {
4457 pr_info("every_nth needs statistics=1, set it\n");
4458 sdebug_statistics = true;
4459 }
4460 tweak_cmnd_count();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 return count;
4462 }
4463 return -EINVAL;
4464}
Akinobu Mita82069372013-10-14 22:48:04 +09004465static DRIVER_ATTR_RW(every_nth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Akinobu Mita82069372013-10-14 22:48:04 +09004467static ssize_t max_luns_show(struct device_driver *ddp, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004469 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470}
Akinobu Mita82069372013-10-14 22:48:04 +09004471static ssize_t max_luns_store(struct device_driver *ddp, const char *buf,
4472 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473{
4474 int n;
Ewan D. Milne19c8ead2014-12-04 11:49:27 -05004475 bool changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
4477 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
Douglas Gilbert8d039e22016-04-30 22:44:43 -04004478 if (n > 256) {
4479 pr_warn("max_luns can be no more than 256\n");
4480 return -EINVAL;
4481 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04004482 changed = (sdebug_max_luns != n);
4483 sdebug_max_luns = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 sdebug_max_tgts_luns();
Douglas Gilbert773642d2016-04-25 12:16:28 -04004485 if (changed && (sdebug_scsi_level >= 5)) { /* >= SPC-3 */
Ewan D. Milne19c8ead2014-12-04 11:49:27 -05004486 struct sdebug_host_info *sdhp;
4487 struct sdebug_dev_info *dp;
4488
4489 spin_lock(&sdebug_host_list_lock);
4490 list_for_each_entry(sdhp, &sdebug_host_list,
4491 host_list) {
4492 list_for_each_entry(dp, &sdhp->dev_info_list,
4493 dev_list) {
4494 set_bit(SDEBUG_UA_LUNS_CHANGED,
4495 dp->uas_bm);
4496 }
4497 }
4498 spin_unlock(&sdebug_host_list_lock);
4499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 return count;
4501 }
4502 return -EINVAL;
4503}
Akinobu Mita82069372013-10-14 22:48:04 +09004504static DRIVER_ATTR_RW(max_luns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
Akinobu Mita82069372013-10-14 22:48:04 +09004506static ssize_t max_queue_show(struct device_driver *ddp, char *buf)
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04004507{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004508 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue);
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04004509}
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004510/* N.B. max_queue can be changed while there are queued commands. In flight
4511 * commands beyond the new max_queue will be completed. */
Akinobu Mita82069372013-10-14 22:48:04 +09004512static ssize_t max_queue_store(struct device_driver *ddp, const char *buf,
4513 size_t count)
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04004514{
Douglas Gilbertc4837392016-05-06 00:40:26 -04004515 int j, n, k, a;
4516 struct sdebug_queue *sqp;
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04004517
4518 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) &&
Douglas Gilbertc4837392016-05-06 00:40:26 -04004519 (n <= SDEBUG_CANQUEUE)) {
4520 block_unblock_all_queues(true);
4521 k = 0;
4522 for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
4523 ++j, ++sqp) {
4524 a = find_last_bit(sqp->in_use_bm, SDEBUG_CANQUEUE);
4525 if (a > k)
4526 k = a;
4527 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04004528 sdebug_max_queue = n;
Douglas Gilbertc4837392016-05-06 00:40:26 -04004529 if (k == SDEBUG_CANQUEUE)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004530 atomic_set(&retired_max_queue, 0);
4531 else if (k >= n)
4532 atomic_set(&retired_max_queue, k + 1);
4533 else
4534 atomic_set(&retired_max_queue, 0);
Douglas Gilbertc4837392016-05-06 00:40:26 -04004535 block_unblock_all_queues(false);
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04004536 return count;
4537 }
4538 return -EINVAL;
4539}
Akinobu Mita82069372013-10-14 22:48:04 +09004540static DRIVER_ATTR_RW(max_queue);
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04004541
Akinobu Mita82069372013-10-14 22:48:04 +09004542static ssize_t no_uld_show(struct device_driver *ddp, char *buf)
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04004543{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004544 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld);
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04004545}
Akinobu Mita82069372013-10-14 22:48:04 +09004546static DRIVER_ATTR_RO(no_uld);
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04004547
Akinobu Mita82069372013-10-14 22:48:04 +09004548static ssize_t scsi_level_show(struct device_driver *ddp, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004550 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551}
Akinobu Mita82069372013-10-14 22:48:04 +09004552static DRIVER_ATTR_RO(scsi_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
Akinobu Mita82069372013-10-14 22:48:04 +09004554static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf)
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004555{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004556 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004557}
Akinobu Mita82069372013-10-14 22:48:04 +09004558static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf,
4559 size_t count)
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004560{
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05004561 int n;
Douglas Gilbert0d01c5d2014-11-24 20:27:51 -05004562 bool changed;
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004563
4564 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04004565 changed = (sdebug_virtual_gb != n);
4566 sdebug_virtual_gb = n;
FUJITA Tomonori28898872008-03-30 00:59:55 +09004567 sdebug_capacity = get_sdebug_capacity();
Douglas Gilbert0d01c5d2014-11-24 20:27:51 -05004568 if (changed) {
4569 struct sdebug_host_info *sdhp;
4570 struct sdebug_dev_info *dp;
FUJITA Tomonori28898872008-03-30 00:59:55 +09004571
Ewan D. Milne4bc6b632014-12-04 11:49:26 -05004572 spin_lock(&sdebug_host_list_lock);
Douglas Gilbert0d01c5d2014-11-24 20:27:51 -05004573 list_for_each_entry(sdhp, &sdebug_host_list,
4574 host_list) {
4575 list_for_each_entry(dp, &sdhp->dev_info_list,
4576 dev_list) {
4577 set_bit(SDEBUG_UA_CAPACITY_CHANGED,
4578 dp->uas_bm);
4579 }
4580 }
Ewan D. Milne4bc6b632014-12-04 11:49:26 -05004581 spin_unlock(&sdebug_host_list_lock);
Douglas Gilbert0d01c5d2014-11-24 20:27:51 -05004582 }
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004583 return count;
4584 }
4585 return -EINVAL;
4586}
Akinobu Mita82069372013-10-14 22:48:04 +09004587static DRIVER_ATTR_RW(virtual_gb);
Douglas Gilbertc65b1442006-06-06 00:11:24 -04004588
Akinobu Mita82069372013-10-14 22:48:04 +09004589static ssize_t add_host_show(struct device_driver *ddp, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004591 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_add_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592}
4593
Douglas Gilbertfd321192016-04-25 12:16:33 -04004594static int sdebug_add_adapter(void);
4595static void sdebug_remove_adapter(void);
4596
Akinobu Mita82069372013-10-14 22:48:04 +09004597static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
4598 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599{
FUJITA Tomonorif3df41c2008-03-20 11:09:15 +09004600 int delta_hosts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
FUJITA Tomonorif3df41c2008-03-20 11:09:15 +09004602 if (sscanf(buf, "%d", &delta_hosts) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 if (delta_hosts > 0) {
4605 do {
4606 sdebug_add_adapter();
4607 } while (--delta_hosts);
4608 } else if (delta_hosts < 0) {
4609 do {
4610 sdebug_remove_adapter();
4611 } while (++delta_hosts);
4612 }
4613 return count;
4614}
Akinobu Mita82069372013-10-14 22:48:04 +09004615static DRIVER_ATTR_RW(add_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
Akinobu Mita82069372013-10-14 22:48:04 +09004617static ssize_t vpd_use_hostno_show(struct device_driver *ddp, char *buf)
Douglas Gilbert23183912006-09-16 20:30:47 -04004618{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004619 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno);
Douglas Gilbert23183912006-09-16 20:30:47 -04004620}
Akinobu Mita82069372013-10-14 22:48:04 +09004621static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf,
4622 size_t count)
Douglas Gilbert23183912006-09-16 20:30:47 -04004623{
4624 int n;
4625
4626 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04004627 sdebug_vpd_use_hostno = n;
Douglas Gilbert23183912006-09-16 20:30:47 -04004628 return count;
4629 }
4630 return -EINVAL;
4631}
Akinobu Mita82069372013-10-14 22:48:04 +09004632static DRIVER_ATTR_RW(vpd_use_hostno);
Douglas Gilbert23183912006-09-16 20:30:47 -04004633
Douglas Gilbertc4837392016-05-06 00:40:26 -04004634static ssize_t statistics_show(struct device_driver *ddp, char *buf)
4635{
4636 return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_statistics);
4637}
4638static ssize_t statistics_store(struct device_driver *ddp, const char *buf,
4639 size_t count)
4640{
4641 int n;
4642
4643 if ((count > 0) && (sscanf(buf, "%d", &n) == 1) && (n >= 0)) {
4644 if (n > 0)
4645 sdebug_statistics = true;
4646 else {
4647 clear_queue_stats();
4648 sdebug_statistics = false;
4649 }
4650 return count;
4651 }
4652 return -EINVAL;
4653}
4654static DRIVER_ATTR_RW(statistics);
4655
Akinobu Mita82069372013-10-14 22:48:04 +09004656static ssize_t sector_size_show(struct device_driver *ddp, char *buf)
Martin K. Petersen597136a2008-06-05 00:12:59 -04004657{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004658 return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size);
Martin K. Petersen597136a2008-06-05 00:12:59 -04004659}
Akinobu Mita82069372013-10-14 22:48:04 +09004660static DRIVER_ATTR_RO(sector_size);
Martin K. Petersen597136a2008-06-05 00:12:59 -04004661
Douglas Gilbertc4837392016-05-06 00:40:26 -04004662static ssize_t submit_queues_show(struct device_driver *ddp, char *buf)
4663{
4664 return scnprintf(buf, PAGE_SIZE, "%d\n", submit_queues);
4665}
4666static DRIVER_ATTR_RO(submit_queues);
4667
Akinobu Mita82069372013-10-14 22:48:04 +09004668static ssize_t dix_show(struct device_driver *ddp, char *buf)
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004669{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004670 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004671}
Akinobu Mita82069372013-10-14 22:48:04 +09004672static DRIVER_ATTR_RO(dix);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004673
Akinobu Mita82069372013-10-14 22:48:04 +09004674static ssize_t dif_show(struct device_driver *ddp, char *buf)
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004675{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004676 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004677}
Akinobu Mita82069372013-10-14 22:48:04 +09004678static DRIVER_ATTR_RO(dif);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004679
Akinobu Mita82069372013-10-14 22:48:04 +09004680static ssize_t guard_show(struct device_driver *ddp, char *buf)
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004681{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004682 return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004683}
Akinobu Mita82069372013-10-14 22:48:04 +09004684static DRIVER_ATTR_RO(guard);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004685
Akinobu Mita82069372013-10-14 22:48:04 +09004686static ssize_t ato_show(struct device_driver *ddp, char *buf)
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004687{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004688 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004689}
Akinobu Mita82069372013-10-14 22:48:04 +09004690static DRIVER_ATTR_RO(ato);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004691
Akinobu Mita82069372013-10-14 22:48:04 +09004692static ssize_t map_show(struct device_driver *ddp, char *buf)
Martin K. Petersen44d92692009-10-15 14:45:27 -04004693{
4694 ssize_t count;
4695
Martin K. Petersen5b94e232011-03-08 02:08:11 -05004696 if (!scsi_debug_lbp())
Martin K. Petersen44d92692009-10-15 14:45:27 -04004697 return scnprintf(buf, PAGE_SIZE, "0-%u\n",
4698 sdebug_store_sectors);
4699
Tejun Heoc7badc92015-02-13 14:37:51 -08004700 count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
4701 (int)map_size, map_storep);
Martin K. Petersen44d92692009-10-15 14:45:27 -04004702 buf[count++] = '\n';
Tejun Heoc7badc92015-02-13 14:37:51 -08004703 buf[count] = '\0';
Martin K. Petersen44d92692009-10-15 14:45:27 -04004704
4705 return count;
4706}
Akinobu Mita82069372013-10-14 22:48:04 +09004707static DRIVER_ATTR_RO(map);
Martin K. Petersen44d92692009-10-15 14:45:27 -04004708
Akinobu Mita82069372013-10-14 22:48:04 +09004709static ssize_t removable_show(struct device_driver *ddp, char *buf)
Martin Pittd9867882012-09-06 12:04:33 +02004710{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004711 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0);
Martin Pittd9867882012-09-06 12:04:33 +02004712}
Akinobu Mita82069372013-10-14 22:48:04 +09004713static ssize_t removable_store(struct device_driver *ddp, const char *buf,
4714 size_t count)
Martin Pittd9867882012-09-06 12:04:33 +02004715{
4716 int n;
4717
4718 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04004719 sdebug_removable = (n > 0);
Martin Pittd9867882012-09-06 12:04:33 +02004720 return count;
4721 }
4722 return -EINVAL;
4723}
Akinobu Mita82069372013-10-14 22:48:04 +09004724static DRIVER_ATTR_RW(removable);
Martin Pittd9867882012-09-06 12:04:33 +02004725
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004726static ssize_t host_lock_show(struct device_driver *ddp, char *buf)
4727{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004728 return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004729}
Douglas Gilbert185dd232016-04-25 12:16:29 -04004730/* N.B. sdebug_host_lock does nothing, kept for backward compatibility */
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004731static ssize_t host_lock_store(struct device_driver *ddp, const char *buf,
4732 size_t count)
4733{
Douglas Gilbert185dd232016-04-25 12:16:29 -04004734 int n;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004735
4736 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
Douglas Gilbert185dd232016-04-25 12:16:29 -04004737 sdebug_host_lock = (n > 0);
4738 return count;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004739 }
4740 return -EINVAL;
4741}
4742static DRIVER_ATTR_RW(host_lock);
4743
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05004744static ssize_t strict_show(struct device_driver *ddp, char *buf)
4745{
Douglas Gilbert773642d2016-04-25 12:16:28 -04004746 return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05004747}
4748static ssize_t strict_store(struct device_driver *ddp, const char *buf,
4749 size_t count)
4750{
4751 int n;
4752
4753 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04004754 sdebug_strict = (n > 0);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05004755 return count;
4756 }
4757 return -EINVAL;
4758}
4759static DRIVER_ATTR_RW(strict);
4760
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004761
Akinobu Mita82069372013-10-14 22:48:04 +09004762/* Note: The following array creates attribute files in the
Douglas Gilbert23183912006-09-16 20:30:47 -04004763 /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
4764 files (over those found in the /sys/module/scsi_debug/parameters
4765 directory) is that auxiliary actions can be triggered when an attribute
4766 is changed. For example see: sdebug_add_host_store() above.
4767 */
Randy Dunlap6ecaff72006-07-11 20:53:22 -07004768
Akinobu Mita82069372013-10-14 22:48:04 +09004769static struct attribute *sdebug_drv_attrs[] = {
4770 &driver_attr_delay.attr,
4771 &driver_attr_opts.attr,
4772 &driver_attr_ptype.attr,
4773 &driver_attr_dsense.attr,
4774 &driver_attr_fake_rw.attr,
4775 &driver_attr_no_lun_0.attr,
4776 &driver_attr_num_tgts.attr,
4777 &driver_attr_dev_size_mb.attr,
4778 &driver_attr_num_parts.attr,
4779 &driver_attr_every_nth.attr,
4780 &driver_attr_max_luns.attr,
4781 &driver_attr_max_queue.attr,
4782 &driver_attr_no_uld.attr,
4783 &driver_attr_scsi_level.attr,
4784 &driver_attr_virtual_gb.attr,
4785 &driver_attr_add_host.attr,
4786 &driver_attr_vpd_use_hostno.attr,
4787 &driver_attr_sector_size.attr,
Douglas Gilbertc4837392016-05-06 00:40:26 -04004788 &driver_attr_statistics.attr,
4789 &driver_attr_submit_queues.attr,
Akinobu Mita82069372013-10-14 22:48:04 +09004790 &driver_attr_dix.attr,
4791 &driver_attr_dif.attr,
4792 &driver_attr_guard.attr,
4793 &driver_attr_ato.attr,
4794 &driver_attr_map.attr,
4795 &driver_attr_removable.attr,
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004796 &driver_attr_host_lock.attr,
4797 &driver_attr_ndelay.attr,
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05004798 &driver_attr_strict.attr,
Akinobu Mita82069372013-10-14 22:48:04 +09004799 NULL,
4800};
4801ATTRIBUTE_GROUPS(sdebug_drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Akinobu Mita11ddcec2014-02-26 22:56:59 +09004803static struct device *pseudo_primary;
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09004804
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805static int __init scsi_debug_init(void)
4806{
FUJITA Tomonori5f2578e2008-03-30 00:59:57 +09004807 unsigned long sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 int host_to_add;
4809 int k;
Randy Dunlap6ecaff72006-07-11 20:53:22 -07004810 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004812 atomic_set(&retired_max_queue, 0);
4813
Douglas Gilbert773642d2016-04-25 12:16:28 -04004814 if (sdebug_ndelay >= 1000 * 1000 * 1000) {
Tomas Winklerc12879702015-07-28 16:54:20 +03004815 pr_warn("ndelay must be less than 1 second, ignored\n");
Douglas Gilbert773642d2016-04-25 12:16:28 -04004816 sdebug_ndelay = 0;
4817 } else if (sdebug_ndelay > 0)
Douglas Gilbertc2206092016-04-25 12:16:31 -04004818 sdebug_jdelay = JDELAY_OVERRIDDEN;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004819
Douglas Gilbert773642d2016-04-25 12:16:28 -04004820 switch (sdebug_sector_size) {
Martin K. Petersen597136a2008-06-05 00:12:59 -04004821 case 512:
4822 case 1024:
4823 case 2048:
4824 case 4096:
4825 break;
4826 default:
Douglas Gilbert773642d2016-04-25 12:16:28 -04004827 pr_err("invalid sector_size %d\n", sdebug_sector_size);
Martin K. Petersen597136a2008-06-05 00:12:59 -04004828 return -EINVAL;
4829 }
4830
Douglas Gilbert773642d2016-04-25 12:16:28 -04004831 switch (sdebug_dif) {
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004832
4833 case SD_DIF_TYPE0_PROTECTION:
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04004834 break;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004835 case SD_DIF_TYPE1_PROTECTION:
Martin K. Petersen395cef02009-09-18 17:33:03 -04004836 case SD_DIF_TYPE2_PROTECTION:
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004837 case SD_DIF_TYPE3_PROTECTION:
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04004838 have_dif_prot = true;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004839 break;
4840
4841 default:
Tomas Winklerc12879702015-07-28 16:54:20 +03004842 pr_err("dif must be 0, 1, 2 or 3\n");
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004843 return -EINVAL;
4844 }
4845
Douglas Gilbert773642d2016-04-25 12:16:28 -04004846 if (sdebug_guard > 1) {
Tomas Winklerc12879702015-07-28 16:54:20 +03004847 pr_err("guard must be 0 or 1\n");
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004848 return -EINVAL;
4849 }
4850
Douglas Gilbert773642d2016-04-25 12:16:28 -04004851 if (sdebug_ato > 1) {
Tomas Winklerc12879702015-07-28 16:54:20 +03004852 pr_err("ato must be 0 or 1\n");
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004853 return -EINVAL;
4854 }
4855
Douglas Gilbert773642d2016-04-25 12:16:28 -04004856 if (sdebug_physblk_exp > 15) {
4857 pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp);
Martin K. Petersenea61fca2009-05-15 00:40:33 -04004858 return -EINVAL;
4859 }
Douglas Gilbert8d039e22016-04-30 22:44:43 -04004860 if (sdebug_max_luns > 256) {
4861 pr_warn("max_luns can be no more than 256, use default\n");
4862 sdebug_max_luns = DEF_MAX_LUNS;
4863 }
Martin K. Petersenea61fca2009-05-15 00:40:33 -04004864
Douglas Gilbert773642d2016-04-25 12:16:28 -04004865 if (sdebug_lowest_aligned > 0x3fff) {
4866 pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned);
Martin K. Petersenea61fca2009-05-15 00:40:33 -04004867 return -EINVAL;
4868 }
4869
Douglas Gilbertc4837392016-05-06 00:40:26 -04004870 if (submit_queues < 1) {
4871 pr_err("submit_queues must be 1 or more\n");
4872 return -EINVAL;
4873 }
4874 sdebug_q_arr = kcalloc(submit_queues, sizeof(struct sdebug_queue),
4875 GFP_KERNEL);
4876 if (sdebug_q_arr == NULL)
4877 return -ENOMEM;
4878 for (k = 0; k < submit_queues; ++k)
4879 spin_lock_init(&sdebug_q_arr[k].qc_lock);
4880
Douglas Gilbert773642d2016-04-25 12:16:28 -04004881 if (sdebug_dev_size_mb < 1)
4882 sdebug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
4883 sz = (unsigned long)sdebug_dev_size_mb * 1048576;
4884 sdebug_store_sectors = sz / sdebug_sector_size;
FUJITA Tomonori28898872008-03-30 00:59:55 +09004885 sdebug_capacity = get_sdebug_capacity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
4887 /* play around with geometry, don't waste too much on track 0 */
4888 sdebug_heads = 8;
4889 sdebug_sectors_per = 32;
Douglas Gilbert773642d2016-04-25 12:16:28 -04004890 if (sdebug_dev_size_mb >= 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 sdebug_heads = 64;
Douglas Gilbert773642d2016-04-25 12:16:28 -04004892 else if (sdebug_dev_size_mb >= 16)
Andy Shevchenkofa785f02015-11-26 20:22:50 +02004893 sdebug_heads = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
4895 (sdebug_sectors_per * sdebug_heads);
4896 if (sdebug_cylinders_per >= 1024) {
4897 /* other LLDs do this; implies >= 1GB ram disk ... */
4898 sdebug_heads = 255;
4899 sdebug_sectors_per = 63;
4900 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
4901 (sdebug_sectors_per * sdebug_heads);
4902 }
4903
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04004904 if (sdebug_fake_rw == 0) {
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004905 fake_storep = vmalloc(sz);
4906 if (NULL == fake_storep) {
Tomas Winklerc12879702015-07-28 16:54:20 +03004907 pr_err("out of memory, 1\n");
Douglas Gilbertc4837392016-05-06 00:40:26 -04004908 ret = -ENOMEM;
4909 goto free_q_arr;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004910 }
4911 memset(fake_storep, 0, sz);
Douglas Gilbert773642d2016-04-25 12:16:28 -04004912 if (sdebug_num_parts > 0)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04004913 sdebug_build_parts(fake_storep, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
Douglas Gilbert773642d2016-04-25 12:16:28 -04004916 if (sdebug_dix) {
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004917 int dif_size;
4918
4919 dif_size = sdebug_store_sectors * sizeof(struct sd_dif_tuple);
4920 dif_storep = vmalloc(dif_size);
4921
Tomas Winklerc12879702015-07-28 16:54:20 +03004922 pr_err("dif_storep %u bytes @ %p\n", dif_size, dif_storep);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004923
4924 if (dif_storep == NULL) {
Tomas Winklerc12879702015-07-28 16:54:20 +03004925 pr_err("out of mem. (DIX)\n");
Martin K. Petersenc6a44282009-01-04 03:08:19 -05004926 ret = -ENOMEM;
4927 goto free_vm;
4928 }
4929
4930 memset(dif_storep, 0xff, dif_size);
4931 }
4932
Martin K. Petersen5b94e232011-03-08 02:08:11 -05004933 /* Logical Block Provisioning */
4934 if (scsi_debug_lbp()) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04004935 sdebug_unmap_max_blocks =
4936 clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU);
Martin K. Petersen60147592010-08-19 11:49:00 -04004937
Douglas Gilbert773642d2016-04-25 12:16:28 -04004938 sdebug_unmap_max_desc =
4939 clamp(sdebug_unmap_max_desc, 0U, 256U);
Martin K. Petersen60147592010-08-19 11:49:00 -04004940
Douglas Gilbert773642d2016-04-25 12:16:28 -04004941 sdebug_unmap_granularity =
4942 clamp(sdebug_unmap_granularity, 1U, 0xffffffffU);
Martin K. Petersen60147592010-08-19 11:49:00 -04004943
Douglas Gilbert773642d2016-04-25 12:16:28 -04004944 if (sdebug_unmap_alignment &&
4945 sdebug_unmap_granularity <=
4946 sdebug_unmap_alignment) {
Tomas Winklerc12879702015-07-28 16:54:20 +03004947 pr_err("ERR: unmap_granularity <= unmap_alignment\n");
Douglas Gilbertc4837392016-05-06 00:40:26 -04004948 ret = -EINVAL;
4949 goto free_vm;
Martin K. Petersen44d92692009-10-15 14:45:27 -04004950 }
4951
Akinobu Mitab90ebc32013-04-16 22:11:58 +09004952 map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1;
4953 map_storep = vmalloc(BITS_TO_LONGS(map_size) * sizeof(long));
Martin K. Petersen44d92692009-10-15 14:45:27 -04004954
Tomas Winklerc12879702015-07-28 16:54:20 +03004955 pr_info("%lu provisioning blocks\n", map_size);
Martin K. Petersen44d92692009-10-15 14:45:27 -04004956
4957 if (map_storep == NULL) {
Tomas Winklerc12879702015-07-28 16:54:20 +03004958 pr_err("out of mem. (MAP)\n");
Martin K. Petersen44d92692009-10-15 14:45:27 -04004959 ret = -ENOMEM;
4960 goto free_vm;
4961 }
4962
Akinobu Mitab90ebc32013-04-16 22:11:58 +09004963 bitmap_zero(map_storep, map_size);
Martin K. Petersen44d92692009-10-15 14:45:27 -04004964
4965 /* Map first 1KB for partition table */
Douglas Gilbert773642d2016-04-25 12:16:28 -04004966 if (sdebug_num_parts)
Martin K. Petersen44d92692009-10-15 14:45:27 -04004967 map_region(0, 2);
4968 }
4969
Nicholas Bellinger9b906772010-09-06 17:24:28 -07004970 pseudo_primary = root_device_register("pseudo_0");
4971 if (IS_ERR(pseudo_primary)) {
Tomas Winklerc12879702015-07-28 16:54:20 +03004972 pr_warn("root_device_register() error\n");
Nicholas Bellinger9b906772010-09-06 17:24:28 -07004973 ret = PTR_ERR(pseudo_primary);
Randy Dunlap6ecaff72006-07-11 20:53:22 -07004974 goto free_vm;
4975 }
4976 ret = bus_register(&pseudo_lld_bus);
4977 if (ret < 0) {
Tomas Winklerc12879702015-07-28 16:54:20 +03004978 pr_warn("bus_register error: %d\n", ret);
Randy Dunlap6ecaff72006-07-11 20:53:22 -07004979 goto dev_unreg;
4980 }
4981 ret = driver_register(&sdebug_driverfs_driver);
4982 if (ret < 0) {
Tomas Winklerc12879702015-07-28 16:54:20 +03004983 pr_warn("driver_register error: %d\n", ret);
Randy Dunlap6ecaff72006-07-11 20:53:22 -07004984 goto bus_unreg;
4985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
Douglas Gilbert773642d2016-04-25 12:16:28 -04004987 host_to_add = sdebug_add_host;
4988 sdebug_add_host = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
4990 for (k = 0; k < host_to_add; k++) {
4991 if (sdebug_add_adapter()) {
Tomas Winklerc12879702015-07-28 16:54:20 +03004992 pr_err("sdebug_add_adapter failed k=%d\n", k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 break;
4994 }
4995 }
4996
Douglas Gilbert773642d2016-04-25 12:16:28 -04004997 if (sdebug_verbose)
4998 pr_info("built %d host(s)\n", sdebug_add_host);
Tomas Winklerc12879702015-07-28 16:54:20 +03004999
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 return 0;
Randy Dunlap6ecaff72006-07-11 20:53:22 -07005001
Randy Dunlap6ecaff72006-07-11 20:53:22 -07005002bus_unreg:
5003 bus_unregister(&pseudo_lld_bus);
5004dev_unreg:
Nicholas Bellinger9b906772010-09-06 17:24:28 -07005005 root_device_unregister(pseudo_primary);
Randy Dunlap6ecaff72006-07-11 20:53:22 -07005006free_vm:
Tomas Winklerde232af2015-07-28 16:54:22 +03005007 vfree(map_storep);
5008 vfree(dif_storep);
Randy Dunlap6ecaff72006-07-11 20:53:22 -07005009 vfree(fake_storep);
Douglas Gilbertc4837392016-05-06 00:40:26 -04005010free_q_arr:
5011 kfree(sdebug_q_arr);
Randy Dunlap6ecaff72006-07-11 20:53:22 -07005012 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013}
5014
5015static void __exit scsi_debug_exit(void)
5016{
Douglas Gilbert773642d2016-04-25 12:16:28 -04005017 int k = sdebug_add_host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
5019 stop_all_queued();
Douglas Gilbertcbf67842014-07-26 11:55:35 -04005020 free_all_queued();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 for (; k; k--)
5022 sdebug_remove_adapter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 driver_unregister(&sdebug_driverfs_driver);
5024 bus_unregister(&pseudo_lld_bus);
Nicholas Bellinger9b906772010-09-06 17:24:28 -07005025 root_device_unregister(pseudo_primary);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Tomas Winklerde232af2015-07-28 16:54:22 +03005027 vfree(dif_storep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 vfree(fake_storep);
Douglas Gilbertc4837392016-05-06 00:40:26 -04005029 kfree(sdebug_q_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030}
5031
5032device_initcall(scsi_debug_init);
5033module_exit(scsi_debug_exit);
5034
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035static void sdebug_release_adapter(struct device * dev)
5036{
5037 struct sdebug_host_info *sdbg_host;
5038
5039 sdbg_host = to_sdebug_host(dev);
5040 kfree(sdbg_host);
5041}
5042
5043static int sdebug_add_adapter(void)
5044{
5045 int k, devs_per_host;
5046 int error = 0;
5047 struct sdebug_host_info *sdbg_host;
FUJITA Tomonori8b40228f2008-03-20 11:09:18 +09005048 struct sdebug_dev_info *sdbg_devinfo, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049
Douglas Gilbertc65b1442006-06-06 00:11:24 -04005050 sdbg_host = kzalloc(sizeof(*sdbg_host),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 if (NULL == sdbg_host) {
Tomas Winklerc12879702015-07-28 16:54:20 +03005052 pr_err("out of memory at line %d\n", __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 return -ENOMEM;
5054 }
5055
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 INIT_LIST_HEAD(&sdbg_host->dev_info_list);
5057
Douglas Gilbert773642d2016-04-25 12:16:28 -04005058 devs_per_host = sdebug_num_tgts * sdebug_max_luns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 for (k = 0; k < devs_per_host; k++) {
FUJITA Tomonori5cb2fc02008-03-20 11:09:16 +09005060 sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL);
5061 if (!sdbg_devinfo) {
Tomas Winklerc12879702015-07-28 16:54:20 +03005062 pr_err("out of memory at line %d\n", __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 error = -ENOMEM;
5064 goto clean;
5065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 }
5067
5068 spin_lock(&sdebug_host_list_lock);
5069 list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
5070 spin_unlock(&sdebug_host_list_lock);
5071
5072 sdbg_host->dev.bus = &pseudo_lld_bus;
Nicholas Bellinger9b906772010-09-06 17:24:28 -07005073 sdbg_host->dev.parent = pseudo_primary;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 sdbg_host->dev.release = &sdebug_release_adapter;
Douglas Gilbert773642d2016-04-25 12:16:28 -04005075 dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_add_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
5077 error = device_register(&sdbg_host->dev);
5078
5079 if (error)
5080 goto clean;
5081
Douglas Gilbert773642d2016-04-25 12:16:28 -04005082 ++sdebug_add_host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 return error;
5084
5085clean:
FUJITA Tomonori8b40228f2008-03-20 11:09:18 +09005086 list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
5087 dev_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 list_del(&sdbg_devinfo->dev_list);
5089 kfree(sdbg_devinfo);
5090 }
5091
5092 kfree(sdbg_host);
5093 return error;
5094}
5095
5096static void sdebug_remove_adapter(void)
5097{
5098 struct sdebug_host_info * sdbg_host = NULL;
5099
5100 spin_lock(&sdebug_host_list_lock);
5101 if (!list_empty(&sdebug_host_list)) {
5102 sdbg_host = list_entry(sdebug_host_list.prev,
5103 struct sdebug_host_info, host_list);
5104 list_del(&sdbg_host->host_list);
5105 }
5106 spin_unlock(&sdebug_host_list_lock);
5107
5108 if (!sdbg_host)
5109 return;
5110
Douglas Gilbert773642d2016-04-25 12:16:28 -04005111 device_unregister(&sdbg_host->dev);
5112 --sdebug_add_host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113}
5114
Douglas Gilbertfd321192016-04-25 12:16:33 -04005115static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth)
Douglas Gilbertcbf67842014-07-26 11:55:35 -04005116{
5117 int num_in_q = 0;
Douglas Gilbertcbf67842014-07-26 11:55:35 -04005118 struct sdebug_dev_info *devip;
5119
Douglas Gilbertc4837392016-05-06 00:40:26 -04005120 block_unblock_all_queues(true);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04005121 devip = (struct sdebug_dev_info *)sdev->hostdata;
5122 if (NULL == devip) {
Douglas Gilbertc4837392016-05-06 00:40:26 -04005123 block_unblock_all_queues(false);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04005124 return -ENODEV;
5125 }
5126 num_in_q = atomic_read(&devip->num_in_q);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04005127
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005128 if (qdepth < 1)
5129 qdepth = 1;
Douglas Gilbertc4837392016-05-06 00:40:26 -04005130 /* allow to exceed max host qc_arr elements for testing */
5131 if (qdepth > SDEBUG_CANQUEUE + 10)
5132 qdepth = SDEBUG_CANQUEUE + 10;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005133 scsi_change_queue_depth(sdev, qdepth);
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005134
Douglas Gilbert773642d2016-04-25 12:16:28 -04005135 if (SDEBUG_OPT_Q_NOISE & sdebug_opts) {
Douglas Gilbertc4837392016-05-06 00:40:26 -04005136 sdev_printk(KERN_INFO, sdev, "%s: qdepth=%d, num_in_q=%d\n",
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005137 __func__, qdepth, num_in_q);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04005138 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04005139 block_unblock_all_queues(false);
Douglas Gilbertcbf67842014-07-26 11:55:35 -04005140 return sdev->queue_depth;
5141}
5142
Douglas Gilbertc4837392016-05-06 00:40:26 -04005143static bool fake_timeout(struct scsi_cmnd *scp)
Douglas Gilbert817fd662014-11-24 20:18:02 -05005144{
Douglas Gilbertc4837392016-05-06 00:40:26 -04005145 if (0 == (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth))) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04005146 if (sdebug_every_nth < -1)
5147 sdebug_every_nth = -1;
5148 if (SDEBUG_OPT_TIMEOUT & sdebug_opts)
Douglas Gilbertc4837392016-05-06 00:40:26 -04005149 return true; /* ignore command causing timeout */
Douglas Gilbert773642d2016-04-25 12:16:28 -04005150 else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts &&
Douglas Gilbert817fd662014-11-24 20:18:02 -05005151 scsi_medium_access_command(scp))
Douglas Gilbertc4837392016-05-06 00:40:26 -04005152 return true; /* time out reads and writes */
Douglas Gilbert817fd662014-11-24 20:18:02 -05005153 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04005154 return false;
Douglas Gilbert817fd662014-11-24 20:18:02 -05005155}
5156
Douglas Gilbertfd321192016-04-25 12:16:33 -04005157static int scsi_debug_queuecommand(struct Scsi_Host *shost,
5158 struct scsi_cmnd *scp)
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005159{
5160 u8 sdeb_i;
5161 struct scsi_device *sdp = scp->device;
5162 const struct opcode_info_t *oip;
5163 const struct opcode_info_t *r_oip;
5164 struct sdebug_dev_info *devip;
5165 u8 *cmd = scp->cmnd;
5166 int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
5167 int k, na;
5168 int errsts = 0;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005169 u32 flags;
5170 u16 sa;
5171 u8 opcode = cmd[0];
5172 bool has_wlun_rl;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005173
5174 scsi_set_resid(scp, 0);
Douglas Gilbertc4837392016-05-06 00:40:26 -04005175 if (sdebug_statistics)
5176 atomic_inc(&sdebug_cmnd_count);
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005177 if (unlikely(sdebug_verbose &&
5178 !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005179 char b[120];
5180 int n, len, sb;
5181
5182 len = scp->cmd_len;
5183 sb = (int)sizeof(b);
5184 if (len > 32)
5185 strcpy(b, "too long, over 32 bytes");
5186 else {
5187 for (k = 0, n = 0; k < len && n < sb; ++k)
5188 n += scnprintf(b + n, sb - n, "%02x ",
5189 (u32)cmd[k]);
5190 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04005191 if (sdebug_mq_active)
5192 sdev_printk(KERN_INFO, sdp, "%s: tag=%u, cmd %s\n",
5193 my_name, blk_mq_unique_tag(scp->request),
5194 b);
5195 else
5196 sdev_printk(KERN_INFO, sdp, "%s: cmd %s\n", my_name,
5197 b);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005198 }
Tomas Winkler34d55432015-07-28 16:54:21 +03005199 has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS);
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005200 if (unlikely((sdp->lun >= sdebug_max_luns) && !has_wlun_rl))
5201 goto err_out;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005202
5203 sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */
5204 oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */
5205 devip = (struct sdebug_dev_info *)sdp->hostdata;
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005206 if (unlikely(!devip)) {
5207 devip = find_build_dev_info(sdp);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005208 if (NULL == devip)
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005209 goto err_out;
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005210 }
5211 na = oip->num_attached;
5212 r_pfp = oip->pfp;
5213 if (na) { /* multiple commands with this opcode */
5214 r_oip = oip;
5215 if (FF_SA & r_oip->flags) {
5216 if (F_SA_LOW & oip->flags)
5217 sa = 0x1f & cmd[1];
5218 else
5219 sa = get_unaligned_be16(cmd + 8);
5220 for (k = 0; k <= na; oip = r_oip->arrp + k++) {
5221 if (opcode == oip->opcode && sa == oip->sa)
5222 break;
5223 }
5224 } else { /* since no service action only check opcode */
5225 for (k = 0; k <= na; oip = r_oip->arrp + k++) {
5226 if (opcode == oip->opcode)
5227 break;
5228 }
5229 }
5230 if (k > na) {
5231 if (F_SA_LOW & r_oip->flags)
5232 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4);
5233 else if (F_SA_HIGH & r_oip->flags)
5234 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7);
5235 else
5236 mk_sense_invalid_opcode(scp);
5237 goto check_cond;
5238 }
5239 } /* else (when na==0) we assume the oip is a match */
5240 flags = oip->flags;
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005241 if (unlikely(F_INV_OP & flags)) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005242 mk_sense_invalid_opcode(scp);
5243 goto check_cond;
5244 }
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005245 if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) {
Douglas Gilbert773642d2016-04-25 12:16:28 -04005246 if (sdebug_verbose)
5247 sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n",
5248 my_name, opcode, " supported for wlun");
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005249 mk_sense_invalid_opcode(scp);
5250 goto check_cond;
5251 }
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005252 if (unlikely(sdebug_strict)) { /* check cdb against mask */
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005253 u8 rem;
5254 int j;
5255
5256 for (k = 1; k < oip->len_mask[0] && k < 16; ++k) {
5257 rem = ~oip->len_mask[k] & cmd[k];
5258 if (rem) {
5259 for (j = 7; j >= 0; --j, rem <<= 1) {
5260 if (0x80 & rem)
5261 break;
5262 }
5263 mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j);
5264 goto check_cond;
5265 }
5266 }
5267 }
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005268 if (unlikely(!(F_SKIP_UA & flags) &&
Douglas Gilbertb01f6f82016-04-30 22:44:42 -04005269 find_first_bit(devip->uas_bm,
5270 SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) {
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005271 errsts = make_ua(scp, devip);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005272 if (errsts)
5273 goto check_cond;
5274 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04005275 if (unlikely((F_M_ACCESS & flags) && atomic_read(&devip->stopped))) {
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005276 mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2);
Douglas Gilbert773642d2016-04-25 12:16:28 -04005277 if (sdebug_verbose)
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005278 sdev_printk(KERN_INFO, sdp, "%s reports: Not ready: "
5279 "%s\n", my_name, "initializing command "
5280 "required");
5281 errsts = check_condition_result;
5282 goto fini;
5283 }
Douglas Gilbert773642d2016-04-25 12:16:28 -04005284 if (sdebug_fake_rw && (F_FAKE_RW & flags))
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005285 goto fini;
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005286 if (unlikely(sdebug_every_nth)) {
Douglas Gilbertc4837392016-05-06 00:40:26 -04005287 if (fake_timeout(scp))
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005288 return 0; /* ignore command: make trouble */
5289 }
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005290 if (likely(oip->pfp))
5291 errsts = oip->pfp(scp, devip); /* calls a resp_* function */
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005292 else if (r_pfp) /* if leaf function ptr NULL, try the root's */
5293 errsts = r_pfp(scp, devip);
5294
5295fini:
5296 return schedule_resp(scp, devip, errsts,
Douglas Gilbertc2206092016-04-25 12:16:31 -04005297 ((F_DELAY_OVERR & flags) ? 0 : sdebug_jdelay));
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005298check_cond:
5299 return schedule_resp(scp, devip, check_condition_result, 0);
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005300err_out:
5301 return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, 0);
Douglas Gilbertc2248fc2014-11-24 20:46:29 -05005302}
5303
FUJITA Tomonori9e603ca2008-03-02 18:30:16 +09005304static struct scsi_host_template sdebug_driver_template = {
Al Viroc8ed5552013-03-31 01:46:06 -04005305 .show_info = scsi_debug_show_info,
5306 .write_info = scsi_debug_write_info,
FUJITA Tomonori9e603ca2008-03-02 18:30:16 +09005307 .proc_name = sdebug_proc_name,
5308 .name = "SCSI DEBUG",
5309 .info = scsi_debug_info,
5310 .slave_alloc = scsi_debug_slave_alloc,
5311 .slave_configure = scsi_debug_slave_configure,
5312 .slave_destroy = scsi_debug_slave_destroy,
5313 .ioctl = scsi_debug_ioctl,
Douglas Gilbert185dd232016-04-25 12:16:29 -04005314 .queuecommand = scsi_debug_queuecommand,
Douglas Gilbertcbf67842014-07-26 11:55:35 -04005315 .change_queue_depth = sdebug_change_qdepth,
FUJITA Tomonori9e603ca2008-03-02 18:30:16 +09005316 .eh_abort_handler = scsi_debug_abort,
FUJITA Tomonori9e603ca2008-03-02 18:30:16 +09005317 .eh_device_reset_handler = scsi_debug_device_reset,
Douglas Gilbertcbf67842014-07-26 11:55:35 -04005318 .eh_target_reset_handler = scsi_debug_target_reset,
5319 .eh_bus_reset_handler = scsi_debug_bus_reset,
FUJITA Tomonori9e603ca2008-03-02 18:30:16 +09005320 .eh_host_reset_handler = scsi_debug_host_reset,
Douglas Gilbertc4837392016-05-06 00:40:26 -04005321 .can_queue = SDEBUG_CANQUEUE,
FUJITA Tomonori9e603ca2008-03-02 18:30:16 +09005322 .this_id = 7,
Ming Lin65e86172016-04-04 14:48:10 -07005323 .sg_tablesize = SG_MAX_SEGMENTS,
Douglas Gilbertcbf67842014-07-26 11:55:35 -04005324 .cmd_per_lun = DEF_CMD_PER_LUN,
Akinobu Mita6bb5e6e2014-06-02 22:56:49 +09005325 .max_sectors = -1U,
FUJITA Tomonori9e603ca2008-03-02 18:30:16 +09005326 .use_clustering = DISABLE_CLUSTERING,
5327 .module = THIS_MODULE,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005328 .track_queue_depth = 1,
FUJITA Tomonori9e603ca2008-03-02 18:30:16 +09005329};
5330
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331static int sdebug_driver_probe(struct device * dev)
5332{
Douglas Gilbert22017ed2014-11-24 23:04:47 -05005333 int error = 0;
5334 struct sdebug_host_info *sdbg_host;
5335 struct Scsi_Host *hpnt;
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005336 int hprot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
5338 sdbg_host = to_sdebug_host(dev);
5339
Douglas Gilbert773642d2016-04-25 12:16:28 -04005340 sdebug_driver_template.can_queue = sdebug_max_queue;
5341 if (sdebug_clustering)
Akinobu Mita0759c662014-02-26 22:57:04 +09005342 sdebug_driver_template.use_clustering = ENABLE_CLUSTERING;
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04005343 hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
5344 if (NULL == hpnt) {
Tomas Winklerc12879702015-07-28 16:54:20 +03005345 pr_err("scsi_host_alloc failed\n");
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04005346 error = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 return error;
Douglas Gilbert78d4e5a2010-03-25 17:29:05 -04005348 }
Douglas Gilbertc4837392016-05-06 00:40:26 -04005349 if (submit_queues > nr_cpu_ids) {
5350 pr_warn("%s: trim submit_queues (was %d) to nr_cpu_ids=%d\n",
5351 my_name, submit_queues, nr_cpu_ids);
5352 submit_queues = nr_cpu_ids;
5353 }
5354 /* Decide whether to tell scsi subsystem that we want mq */
5355 /* Following should give the same answer for each host */
5356 sdebug_mq_active = shost_use_blk_mq(hpnt) && (submit_queues > 1);
5357 if (sdebug_mq_active)
5358 hpnt->nr_hw_queues = submit_queues;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
5360 sdbg_host->shost = hpnt;
5361 *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
Douglas Gilbert773642d2016-04-25 12:16:28 -04005362 if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id))
5363 hpnt->max_id = sdebug_num_tgts + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 else
Douglas Gilbert773642d2016-04-25 12:16:28 -04005365 hpnt->max_id = sdebug_num_tgts;
5366 /* = sdebug_max_luns; */
Tomas Winklerf2d3fd22015-07-28 16:54:25 +03005367 hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005369 hprot = 0;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05005370
Douglas Gilbert773642d2016-04-25 12:16:28 -04005371 switch (sdebug_dif) {
Martin K. Petersenc6a44282009-01-04 03:08:19 -05005372
5373 case SD_DIF_TYPE1_PROTECTION:
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005374 hprot = SHOST_DIF_TYPE1_PROTECTION;
Douglas Gilbert773642d2016-04-25 12:16:28 -04005375 if (sdebug_dix)
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005376 hprot |= SHOST_DIX_TYPE1_PROTECTION;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05005377 break;
5378
5379 case SD_DIF_TYPE2_PROTECTION:
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005380 hprot = SHOST_DIF_TYPE2_PROTECTION;
Douglas Gilbert773642d2016-04-25 12:16:28 -04005381 if (sdebug_dix)
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005382 hprot |= SHOST_DIX_TYPE2_PROTECTION;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05005383 break;
5384
5385 case SD_DIF_TYPE3_PROTECTION:
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005386 hprot = SHOST_DIF_TYPE3_PROTECTION;
Douglas Gilbert773642d2016-04-25 12:16:28 -04005387 if (sdebug_dix)
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005388 hprot |= SHOST_DIX_TYPE3_PROTECTION;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05005389 break;
5390
5391 default:
Douglas Gilbert773642d2016-04-25 12:16:28 -04005392 if (sdebug_dix)
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005393 hprot |= SHOST_DIX_TYPE0_PROTECTION;
Martin K. Petersenc6a44282009-01-04 03:08:19 -05005394 break;
5395 }
5396
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005397 scsi_host_set_prot(hpnt, hprot);
Martin K. Petersenc6a44282009-01-04 03:08:19 -05005398
Douglas Gilbertf46eb0e2016-04-25 12:16:34 -04005399 if (have_dif_prot || sdebug_dix)
5400 pr_info("host protection%s%s%s%s%s%s%s\n",
5401 (hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
5402 (hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
5403 (hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
5404 (hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
5405 (hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
5406 (hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
5407 (hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
Martin K. Petersenc6a44282009-01-04 03:08:19 -05005408
Douglas Gilbert773642d2016-04-25 12:16:28 -04005409 if (sdebug_guard == 1)
Martin K. Petersenc6a44282009-01-04 03:08:19 -05005410 scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP);
5411 else
5412 scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC);
5413
Douglas Gilbert773642d2016-04-25 12:16:28 -04005414 sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts);
5415 sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & sdebug_opts);
Douglas Gilbertc4837392016-05-06 00:40:26 -04005416 if (sdebug_every_nth) /* need stats counters for every_nth */
5417 sdebug_statistics = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 error = scsi_add_host(hpnt, &sdbg_host->dev);
5419 if (error) {
Tomas Winklerc12879702015-07-28 16:54:20 +03005420 pr_err("scsi_add_host failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 error = -ENODEV;
5422 scsi_host_put(hpnt);
5423 } else
5424 scsi_scan_host(hpnt);
5425
Douglas Gilbertcbf67842014-07-26 11:55:35 -04005426 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427}
5428
5429static int sdebug_driver_remove(struct device * dev)
5430{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 struct sdebug_host_info *sdbg_host;
FUJITA Tomonori8b40228f2008-03-20 11:09:18 +09005432 struct sdebug_dev_info *sdbg_devinfo, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433
5434 sdbg_host = to_sdebug_host(dev);
5435
5436 if (!sdbg_host) {
Tomas Winklerc12879702015-07-28 16:54:20 +03005437 pr_err("Unable to locate host info\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 return -ENODEV;
5439 }
5440
5441 scsi_remove_host(sdbg_host->shost);
5442
FUJITA Tomonori8b40228f2008-03-20 11:09:18 +09005443 list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
5444 dev_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 list_del(&sdbg_devinfo->dev_list);
5446 kfree(sdbg_devinfo);
5447 }
5448
5449 scsi_host_put(sdbg_host->shost);
5450 return 0;
5451}
5452
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09005453static int pseudo_lld_bus_match(struct device *dev,
5454 struct device_driver *dev_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455{
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09005456 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457}
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09005458
5459static struct bus_type pseudo_lld_bus = {
5460 .name = "pseudo",
5461 .match = pseudo_lld_bus_match,
5462 .probe = sdebug_driver_probe,
5463 .remove = sdebug_driver_remove,
Akinobu Mita82069372013-10-14 22:48:04 +09005464 .drv_groups = sdebug_drv_groups,
FUJITA Tomonori8dea0d02008-03-30 00:59:58 +09005465};