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