aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi
diff options
context:
space:
mode:
authorDouglas Gilbert <dgilbert@interlog.com>2014-11-24 20:46:29 -0500
committerChristoph Hellwig <hch@lst.de>2014-11-25 09:42:57 -0500
commitc2248fc974df7be55a5f6db6b6f99a90b749581b (patch)
tree73117721a14603545743fecc6294ef65321958a5 /drivers/scsi
parent0d01c5df5cd470515a88a454ba69126f4b7abdab (diff)
scsi_debug: change SCSI command parser to table driven
The existing 'big switch' parser in queuecommand() is changed to a table driven parser. The old and new queuecommand() were moved in the source so diff would not shuffle them. Apart from the new tables most other changes are refactoring existing response code to be more easily called out of the table parser. The 'strict' parameter is added so that cdb_s can be checked for non-zero values in parts of the cdb that are reserved. Some other changes include: tweak request sense response when D_SENSE differs; support NDOB in Write Same(16); and fix crash in Get LBA Status when LBP was inactive. Signed-off-by: Douglas Gilbert <dgilbert@interlog.com> Signed-off-by: Christoph Hellwig <hch@lst.de>
Diffstat (limited to 'drivers/scsi')
-rw-r--r--drivers/scsi/scsi_debug.c1391
1 files changed, 833 insertions, 558 deletions
diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c
index ee99aca92bca..2181427a1ea5 100644
--- a/drivers/scsi/scsi_debug.c
+++ b/drivers/scsi/scsi_debug.c
@@ -71,10 +71,10 @@ static const char *scsi_debug_version_date = "20141022";
71/* Additional Sense Code (ASC) */ 71/* Additional Sense Code (ASC) */
72#define NO_ADDITIONAL_SENSE 0x0 72#define NO_ADDITIONAL_SENSE 0x0
73#define LOGICAL_UNIT_NOT_READY 0x4 73#define LOGICAL_UNIT_NOT_READY 0x4
74#define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8
74#define UNRECOVERED_READ_ERR 0x11 75#define UNRECOVERED_READ_ERR 0x11
75#define PARAMETER_LIST_LENGTH_ERR 0x1a 76#define PARAMETER_LIST_LENGTH_ERR 0x1a
76#define INVALID_OPCODE 0x20 77#define INVALID_OPCODE 0x20
77#define INVALID_COMMAND_OPCODE 0x20
78#define LBA_OUT_OF_RANGE 0x21 78#define LBA_OUT_OF_RANGE 0x21
79#define INVALID_FIELD_IN_CDB 0x24 79#define INVALID_FIELD_IN_CDB 0x24
80#define INVALID_FIELD_IN_PARAM_LIST 0x26 80#define INVALID_FIELD_IN_PARAM_LIST 0x26
@@ -136,6 +136,7 @@ static const char *scsi_debug_version_date = "20141022";
136#define DEF_VIRTUAL_GB 0 136#define DEF_VIRTUAL_GB 0
137#define DEF_VPD_USE_HOSTNO 1 137#define DEF_VPD_USE_HOSTNO 1
138#define DEF_WRITESAME_LENGTH 0xFFFF 138#define DEF_WRITESAME_LENGTH 0xFFFF
139#define DEF_STRICT 0
139#define DELAY_OVERRIDDEN -9999 140#define DELAY_OVERRIDDEN -9999
140 141
141/* bit mask values for scsi_debug_opts */ 142/* bit mask values for scsi_debug_opts */
@@ -183,8 +184,8 @@ static const char *scsi_debug_version_date = "20141022";
183#define SDEBUG_NUM_UAS 4 184#define SDEBUG_NUM_UAS 4
184 185
185/* for check_readiness() */ 186/* for check_readiness() */
186#define UAS_ONLY 1 187#define UAS_ONLY 1 /* check for UAs only */
187#define UAS_TUR 0 188#define UAS_TUR 0 /* if no UAs then check if media access possible */
188 189
189/* when 1==SCSI_DEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this 190/* when 1==SCSI_DEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
190 * sector on read commands: */ 191 * sector on read commands: */
@@ -210,6 +211,291 @@ static const char *scsi_debug_version_date = "20141022";
210#warning "Expect DEF_CMD_PER_LUN <= SCSI_DEBUG_CANQUEUE" 211#warning "Expect DEF_CMD_PER_LUN <= SCSI_DEBUG_CANQUEUE"
211#endif 212#endif
212 213
214/* SCSI opcodes (first byte of cdb) mapped onto these indexes */
215enum sdeb_opcode_index {
216 SDEB_I_INVALID_OPCODE = 0,
217 SDEB_I_INQUIRY = 1,
218 SDEB_I_REPORT_LUNS = 2,
219 SDEB_I_REQUEST_SENSE = 3,
220 SDEB_I_TEST_UNIT_READY = 4,
221 SDEB_I_MODE_SENSE = 5, /* 6, 10 */
222 SDEB_I_MODE_SELECT = 6, /* 6, 10 */
223 SDEB_I_LOG_SENSE = 7,
224 SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */
225 SDEB_I_READ = 9, /* 6, 10, 12, 16 */
226 SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */
227 SDEB_I_START_STOP = 11,
228 SDEB_I_SERV_ACT_IN = 12, /* 12, 16 */
229 SDEB_I_SERV_ACT_OUT = 13, /* 12, 16 */
230 SDEB_I_MAINT_IN = 14,
231 SDEB_I_MAINT_OUT = 15,
232 SDEB_I_VERIFY = 16, /* 10 only */
233 SDEB_I_VARIABLE_LEN = 17,
234 SDEB_I_RESERVE = 18, /* 6, 10 */
235 SDEB_I_RELEASE = 19, /* 6, 10 */
236 SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */
237 SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */
238 SDEB_I_ATA_PT = 22, /* 12, 16 */
239 SDEB_I_SEND_DIAG = 23,
240 SDEB_I_UNMAP = 24,
241 SDEB_I_XDWRITEREAD = 25, /* 10 only */
242 SDEB_I_WRITE_BUFFER = 26,
243 SDEB_I_WRITE_SAME = 27, /* 10, 16 */
244 SDEB_I_SYNC_CACHE = 28, /* 10 only */
245 SDEB_I_COMP_WRITE = 29,
246 SDEB_I_LAST_ELEMENT = 30, /* keep this last */
247};
248
249static const unsigned char opcode_ind_arr[256] = {
250/* 0x0; 0x0->0x1f: 6 byte cdbs */
251 SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE,
252 0, 0, 0, 0,
253 SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0,
254 0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
255 SDEB_I_RELEASE,
256 0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG,
257 SDEB_I_ALLOW_REMOVAL, 0,
258/* 0x20; 0x20->0x3f: 10 byte cdbs */
259 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0,
260 SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY,
261 0, 0, 0, 0, 0, SDEB_I_SYNC_CACHE, 0, 0,
262 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0,
263/* 0x40; 0x40->0x5f: 10 byte cdbs */
264 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0,
265 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0,
266 0, 0, 0, SDEB_I_XDWRITEREAD, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
267 SDEB_I_RELEASE,
268 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0,
269/* 0x60; 0x60->0x7d are reserved */
270 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
271 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
272 0, SDEB_I_VARIABLE_LEN,
273/* 0x80; 0x80->0x9f: 16 byte cdbs */
274 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0,
275 SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0, 0, 0, 0, 0,
276 0, 0, 0, SDEB_I_WRITE_SAME, 0, 0, 0, 0,
277 0, 0, 0, 0, 0, 0, SDEB_I_SERV_ACT_IN, SDEB_I_SERV_ACT_OUT,
278/* 0xa0; 0xa0->0xbf: 12 byte cdbs */
279 SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN,
280 SDEB_I_MAINT_OUT, 0, 0, 0,
281 SDEB_I_READ, SDEB_I_SERV_ACT_OUT, SDEB_I_WRITE, SDEB_I_SERV_ACT_IN,
282 0, 0, 0, 0,
283 0, 0, 0, 0, 0, 0, 0, 0,
284 0, 0, 0, 0, 0, 0, 0, 0,
285/* 0xc0; 0xc0->0xff: vendor specific */
286 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
287 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
288 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
289 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
290};
291
292#define F_D_IN 1
293#define F_D_OUT 2
294#define F_D_OUT_MAYBE 4 /* WRITE SAME, NDOB bit */
295#define F_D_UNKN 8
296#define F_RL_WLUN_OK 0x10
297#define F_SKIP_UA 0x20
298#define F_DELAY_OVERR 0x40
299#define F_SA_LOW 0x80 /* cdb byte 1, bits 4 to 0 */
300#define F_SA_HIGH 0x100 /* as used by variable length cdbs */
301#define F_INV_OP 0x200
302#define F_FAKE_RW 0x400
303#define F_M_ACCESS 0x800 /* media access */
304
305#define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR)
306#define FF_DIRECT_IO (F_M_ACCESS | F_FAKE_RW)
307#define FF_SA (F_SA_HIGH | F_SA_LOW)
308
309struct sdebug_dev_info;
310static int scsi_debug_queuecommand(struct scsi_cmnd *scp);
311static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *);
312static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *);
313static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *);
314static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
315static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *);
316static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
317static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *);
318static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
319static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
320static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *);
321static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *);
322static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *);
323static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *);
324static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *);
325static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *);
326static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *);
327static int resp_xdwriteread_10(struct scsi_cmnd *, struct sdebug_dev_info *);
328
329struct opcode_info_t {
330 u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff
331 * for terminating element */
332 u8 opcode; /* if num_attached > 0, preferred */
333 u16 sa; /* service action */
334 u32 flags; /* OR-ed set of SDEB_F_* */
335 int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
336 const struct opcode_info_t *arrp; /* num_attached elements or NULL */
337 u8 len_mask[16]; /* len=len_mask[0], then mask for cdb[1]... */
338 /* ignore cdb bytes after position 15 */
339};
340
341static const struct opcode_info_t msense_iarr[1] = {
342 {0, 0x1a, 0, F_D_IN, NULL, NULL,
343 {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
344};
345
346static const struct opcode_info_t mselect_iarr[1] = {
347 {0, 0x15, 0, F_D_OUT, NULL, NULL,
348 {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
349};
350
351static const struct opcode_info_t read_iarr[3] = {
352 {0, 0x28, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL,/* READ(10) */
353 {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0,
354 0, 0, 0, 0} },
355 {0, 0x8, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL, /* READ(6) */
356 {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
357 {0, 0xa8, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL,/* READ(12) */
358 {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f,
359 0xc7, 0, 0, 0, 0} },
360};
361
362static const struct opcode_info_t write_iarr[3] = {
363 {0, 0x2a, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 10 */
364 {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0,
365 0, 0, 0, 0} },
366 {0, 0xa, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 6 */
367 {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
368 {0, 0xaa, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 12 */
369 {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f,
370 0xc7, 0, 0, 0, 0} },
371};
372
373static const struct opcode_info_t sa_in_iarr[1] = {
374 {0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL,
375 {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
376 0xff, 0xff, 0xff, 0, 0xc7} },
377};
378
379static const struct opcode_info_t vl_iarr[1] = { /* VARIABLE LENGTH */
380 {0, 0x7f, 0xb, F_SA_HIGH | F_D_OUT | FF_DIRECT_IO, resp_write_dt0,
381 NULL, {32, 0xc7, 0, 0, 0, 0, 0x1f, 0x18, 0x0, 0xb, 0xfa,
382 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */
383};
384
385static const struct opcode_info_t maint_in_iarr[2] = {
386 {0, 0xa3, 0xc, F_SA_LOW | F_D_IN, NULL, NULL,
387 {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0,
388 0xc7, 0, 0, 0, 0} },
389 {0, 0xa3, 0xd, F_SA_LOW | F_D_IN, NULL, NULL,
390 {12, 0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
391 0, 0} },
392};
393
394static const struct opcode_info_t write_same_iarr[1] = {
395 {0, 0x93, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, resp_write_same_16, NULL,
396 {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
397 0xff, 0xff, 0xff, 0x1f, 0xc7} },
398};
399
400static const struct opcode_info_t reserve_iarr[1] = {
401 {0, 0x16, 0, F_D_OUT, NULL, NULL, /* RESERVE(6) */
402 {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
403};
404
405static const struct opcode_info_t release_iarr[1] = {
406 {0, 0x17, 0, F_D_OUT, NULL, NULL, /* RELEASE(6) */
407 {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
408};
409
410
411/* This array is accessed via SDEB_I_* values. Make sure all are mapped,
412 * plus the terminating elements for logic that scans this table such as
413 * REPORT SUPPORTED OPERATION CODES. */
414static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEMENT + 1] = {
415/* 0 */
416 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL,
417 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
418 {0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL,
419 {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
420 {0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL,
421 {12, 0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
422 0, 0} },
423 {0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL,
424 {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
425 {0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */
426 {6, 0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
427 {1, 0x5a, 0, F_D_IN, resp_mode_sense, msense_iarr,
428 {10, 0xf8, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
429 0} },
430 {1, 0x55, 0, F_D_OUT, resp_mode_select, mselect_iarr,
431 {10, 0xf1, 0, 0, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
432 {0, 0x4d, 0, F_D_IN, resp_log_sense, NULL,
433 {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0,
434 0, 0, 0} },
435 {0, 0x25, 0, F_D_IN, resp_readcap, NULL,
436 {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0,
437 0, 0} },
438 {3, 0x88, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, read_iarr,
439 {16, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
440 0xff, 0xff, 0xff, 0x9f, 0xc7} }, /* READ(16) */
441/* 10 */
442 {3, 0x8a, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, write_iarr,
443 {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
444 0xff, 0xff, 0xff, 0x9f, 0xc7} }, /* WRITE(16) */
445 {0, 0x1b, 0, 0, resp_start_stop, NULL, /* START STOP UNIT */
446 {6, 0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
447 {1, 0x9e, 0x10, F_SA_LOW | F_D_IN, resp_readcap16, sa_in_iarr,
448 {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
449 0xff, 0xff, 0xff, 0x1, 0xc7} }, /* READ CAPACITY(16) */
450 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* SA OUT */
451 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
452 {2, 0xa3, 0xa, F_SA_LOW | F_D_IN, resp_report_tgtpgs, maint_in_iarr,
453 {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, 0,
454 0} },
455 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */
456 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
457 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* VERIFY */
458 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
459 {1, 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_DIRECT_IO, resp_read_dt0,
460 vl_iarr, {32, 0xc7, 0, 0, 0, 0, 0x1f, 0x18, 0x0, 0x9, 0xfe, 0,
461 0xff, 0xff, 0xff, 0xff} },/* VARIABLE LENGTH, READ(32) */
462 {1, 0x56, 0, F_D_OUT, NULL, reserve_iarr, /* RESERVE(10) */
463 {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
464 0} },
465 {1, 0x57, 0, F_D_OUT, NULL, release_iarr, /* RELEASE(10) */
466 {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
467 0} },
468/* 20 */
469 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ALLOW REMOVAL */
470 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
471 {0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */
472 {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
473 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */
474 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
475 {0, 0x1d, F_D_OUT, 0, NULL, NULL, /* SEND DIAGNOSTIC */
476 {6, 0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
477 {0, 0x42, 0, F_D_OUT | FF_DIRECT_IO, resp_unmap, NULL, /* UNMAP */
478 {10, 0x1, 0, 0, 0, 0, 0x1f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
479 {0, 0x53, 0, F_D_IN | F_D_OUT | FF_DIRECT_IO, resp_xdwriteread_10,
480 NULL, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7,
481 0, 0, 0, 0, 0, 0} },
482 {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* WRITE_BUFFER */
483 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
484 {1, 0x41, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, resp_write_same_10,
485 write_same_iarr, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff,
486 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
487 {0, 0x35, 0, F_DELAY_OVERR | FF_DIRECT_IO, NULL, NULL, /* SYNC_CACHE */
488 {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0,
489 0, 0, 0, 0} },
490 {0, 0x89, 0, F_D_OUT | FF_DIRECT_IO, NULL, NULL,
491 {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0,
492 0, 0xff, 0x1f, 0xc7} }, /* COMPARE AND WRITE */
493
494/* 30 */
495 {0xff, 0, 0, 0, NULL, NULL, /* terminating element */
496 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
497};
498
213struct sdebug_scmd_extra_t { 499struct sdebug_scmd_extra_t {
214 bool inj_recovered; 500 bool inj_recovered;
215 bool inj_transport; 501 bool inj_transport;
@@ -257,6 +543,7 @@ static unsigned int scsi_debug_write_same_length = DEF_WRITESAME_LENGTH;
257static bool scsi_debug_removable = DEF_REMOVABLE; 543static bool scsi_debug_removable = DEF_REMOVABLE;
258static bool scsi_debug_clustering; 544static bool scsi_debug_clustering;
259static bool scsi_debug_host_lock = DEF_HOST_LOCK; 545static bool scsi_debug_host_lock = DEF_HOST_LOCK;
546static bool scsi_debug_strict = DEF_STRICT;
260static bool sdebug_any_injecting_opt; 547static bool sdebug_any_injecting_opt;
261 548
262static atomic_t sdebug_cmnd_count; 549static atomic_t sdebug_cmnd_count;
@@ -290,11 +577,10 @@ struct sdebug_dev_info {
290 unsigned int target; 577 unsigned int target;
291 u64 lun; 578 u64 lun;
292 struct sdebug_host_info *sdbg_host; 579 struct sdebug_host_info *sdbg_host;
293 u64 wlun;
294 unsigned long uas_bm[1]; 580 unsigned long uas_bm[1];
295 atomic_t num_in_q; 581 atomic_t num_in_q;
296 char stopped; 582 char stopped; /* TODO: should be atomic */
297 char used; 583 bool used;
298}; 584};
299 585
300struct sdebug_host_info { 586struct sdebug_host_info {
@@ -477,65 +763,6 @@ mk_sense_invalid_opcode(struct scsi_cmnd *scp)
477 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0); 763 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
478} 764}
479 765
480static void get_data_transfer_info(unsigned char *cmd,
481 unsigned long long *lba, unsigned int *num,
482 u32 *ei_lba)
483{
484 *ei_lba = 0;
485
486 switch (*cmd) {
487 case VARIABLE_LENGTH_CMD:
488 *lba = (u64)cmd[19] | (u64)cmd[18] << 8 |
489 (u64)cmd[17] << 16 | (u64)cmd[16] << 24 |
490 (u64)cmd[15] << 32 | (u64)cmd[14] << 40 |
491 (u64)cmd[13] << 48 | (u64)cmd[12] << 56;
492
493 *ei_lba = (u32)cmd[23] | (u32)cmd[22] << 8 |
494 (u32)cmd[21] << 16 | (u32)cmd[20] << 24;
495
496 *num = (u32)cmd[31] | (u32)cmd[30] << 8 | (u32)cmd[29] << 16 |
497 (u32)cmd[28] << 24;
498 break;
499
500 case WRITE_SAME_16:
501 case WRITE_16:
502 case READ_16:
503 *lba = (u64)cmd[9] | (u64)cmd[8] << 8 |
504 (u64)cmd[7] << 16 | (u64)cmd[6] << 24 |
505 (u64)cmd[5] << 32 | (u64)cmd[4] << 40 |
506 (u64)cmd[3] << 48 | (u64)cmd[2] << 56;
507
508 *num = (u32)cmd[13] | (u32)cmd[12] << 8 | (u32)cmd[11] << 16 |
509 (u32)cmd[10] << 24;
510 break;
511 case WRITE_12:
512 case READ_12:
513 *lba = (u32)cmd[5] | (u32)cmd[4] << 8 | (u32)cmd[3] << 16 |
514 (u32)cmd[2] << 24;
515
516 *num = (u32)cmd[9] | (u32)cmd[8] << 8 | (u32)cmd[7] << 16 |
517 (u32)cmd[6] << 24;
518 break;
519 case WRITE_SAME:
520 case WRITE_10:
521 case READ_10:
522 case XDWRITEREAD_10:
523 *lba = (u32)cmd[5] | (u32)cmd[4] << 8 | (u32)cmd[3] << 16 |
524 (u32)cmd[2] << 24;
525
526 *num = (u32)cmd[8] | (u32)cmd[7] << 8;
527 break;
528 case WRITE_6:
529 case READ_6:
530 *lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
531 (u32)(cmd[1] & 0x1f) << 16;
532 *num = (0 == cmd[4]) ? 256 : cmd[4];
533 break;
534 default:
535 break;
536 }
537}
538
539static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg) 766static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
540{ 767{
541 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) { 768 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
@@ -992,19 +1219,20 @@ static int inquiry_evpd_b2(unsigned char *arr)
992#define SDEBUG_LONG_INQ_SZ 96 1219#define SDEBUG_LONG_INQ_SZ 96
993#define SDEBUG_MAX_INQ_ARR_SZ 584 1220#define SDEBUG_MAX_INQ_ARR_SZ 584
994 1221
995static int resp_inquiry(struct scsi_cmnd *scp, int target, 1222static int resp_inquiry(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
996 struct sdebug_dev_info * devip)
997{ 1223{
998 unsigned char pq_pdt; 1224 unsigned char pq_pdt;
999 unsigned char * arr; 1225 unsigned char * arr;
1000 unsigned char *cmd = scp->cmnd; 1226 unsigned char *cmd = scp->cmnd;
1001 int alloc_len, n, ret; 1227 int alloc_len, n, ret;
1228 bool have_wlun;
1002 1229
1003 alloc_len = (cmd[3] << 8) + cmd[4]; 1230 alloc_len = (cmd[3] << 8) + cmd[4];
1004 arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC); 1231 arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC);
1005 if (! arr) 1232 if (! arr)
1006 return DID_REQUEUE << 16; 1233 return DID_REQUEUE << 16;
1007 if (devip->wlun) 1234 have_wlun = (scp->device->lun == SAM2_WLUN_REPORT_LUNS);
1235 if (have_wlun)
1008 pq_pdt = 0x1e; /* present, wlun */ 1236 pq_pdt = 0x1e; /* present, wlun */
1009 else if (scsi_debug_no_lun_0 && (0 == devip->lun)) 1237 else if (scsi_debug_no_lun_0 && (0 == devip->lun))
1010 pq_pdt = 0x7f; /* not present, no device type */ 1238 pq_pdt = 0x7f; /* not present, no device type */
@@ -1024,7 +1252,7 @@ static int resp_inquiry(struct scsi_cmnd *scp, int target,
1024 (devip->channel & 0x7f); 1252 (devip->channel & 0x7f);
1025 if (0 == scsi_debug_vpd_use_hostno) 1253 if (0 == scsi_debug_vpd_use_hostno)
1026 host_no = 0; 1254 host_no = 0;
1027 lu_id_num = devip->wlun ? -1 : (((host_no + 1) * 2000) + 1255 lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) +
1028 (devip->target * 1000) + devip->lun); 1256 (devip->target * 1000) + devip->lun);
1029 target_dev_id = ((host_no + 1) * 2000) + 1257 target_dev_id = ((host_no + 1) * 2000) +
1030 (devip->target * 1000) - 3; 1258 (devip->target * 1000) - 3;
@@ -1142,18 +1370,20 @@ static int resp_requests(struct scsi_cmnd * scp,
1142 unsigned char * sbuff; 1370 unsigned char * sbuff;
1143 unsigned char *cmd = scp->cmnd; 1371 unsigned char *cmd = scp->cmnd;
1144 unsigned char arr[SCSI_SENSE_BUFFERSIZE]; 1372 unsigned char arr[SCSI_SENSE_BUFFERSIZE];
1145 int want_dsense; 1373 bool dsense, want_dsense;
1146 int len = 18; 1374 int len = 18;
1147 1375
1148 memset(arr, 0, sizeof(arr)); 1376 memset(arr, 0, sizeof(arr));
1149 want_dsense = !!(cmd[1] & 1) || scsi_debug_dsense; 1377 dsense = !!(cmd[1] & 1);
1378 want_dsense = dsense || scsi_debug_dsense;
1150 sbuff = scp->sense_buffer; 1379 sbuff = scp->sense_buffer;
1151 if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) { 1380 if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
1152 if (want_dsense) { 1381 if (dsense) {
1153 arr[0] = 0x72; 1382 arr[0] = 0x72;
1154 arr[1] = 0x0; /* NO_SENSE in sense_key */ 1383 arr[1] = 0x0; /* NO_SENSE in sense_key */
1155 arr[2] = THRESHOLD_EXCEEDED; 1384 arr[2] = THRESHOLD_EXCEEDED;
1156 arr[3] = 0xff; /* TEST set and MRIE==6 */ 1385 arr[3] = 0xff; /* TEST set and MRIE==6 */
1386 len = 8;
1157 } else { 1387 } else {
1158 arr[0] = 0x70; 1388 arr[0] = 0x70;
1159 arr[2] = 0x0; /* NO_SENSE in sense_key */ 1389 arr[2] = 0x0; /* NO_SENSE in sense_key */
@@ -1163,15 +1393,34 @@ static int resp_requests(struct scsi_cmnd * scp,
1163 } 1393 }
1164 } else { 1394 } else {
1165 memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE); 1395 memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE);
1166 if ((cmd[1] & 1) && (! scsi_debug_dsense)) { 1396 if (arr[0] >= 0x70 && dsense == scsi_debug_dsense)
1167 /* DESC bit set and sense_buff in fixed format */ 1397 ; /* have sense and formats match */
1168 memset(arr, 0, sizeof(arr)); 1398 else if (arr[0] <= 0x70) {
1399 if (dsense) {
1400 memset(arr, 0, 8);
1401 arr[0] = 0x72;
1402 len = 8;
1403 } else {
1404 memset(arr, 0, 18);
1405 arr[0] = 0x70;
1406 arr[7] = 0xa;
1407 }
1408 } else if (dsense) {
1409 memset(arr, 0, 8);
1169 arr[0] = 0x72; 1410 arr[0] = 0x72;
1170 arr[1] = sbuff[2]; /* sense key */ 1411 arr[1] = sbuff[2]; /* sense key */
1171 arr[2] = sbuff[12]; /* asc */ 1412 arr[2] = sbuff[12]; /* asc */
1172 arr[3] = sbuff[13]; /* ascq */ 1413 arr[3] = sbuff[13]; /* ascq */
1173 len = 8; 1414 len = 8;
1415 } else {
1416 memset(arr, 0, 18);
1417 arr[0] = 0x70;
1418 arr[2] = sbuff[1];
1419 arr[7] = 0xa;
1420 arr[12] = sbuff[1];
1421 arr[13] = sbuff[3];
1174 } 1422 }
1423
1175 } 1424 }
1176 mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0); 1425 mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0);
1177 return fill_from_dev_buffer(scp, arr, len); 1426 return fill_from_dev_buffer(scp, arr, len);
@@ -1181,11 +1430,8 @@ static int resp_start_stop(struct scsi_cmnd * scp,
1181 struct sdebug_dev_info * devip) 1430 struct sdebug_dev_info * devip)
1182{ 1431{
1183 unsigned char *cmd = scp->cmnd; 1432 unsigned char *cmd = scp->cmnd;
1184 int power_cond, errsts, start; 1433 int power_cond, start;
1185 1434
1186 errsts = check_readiness(scp, UAS_ONLY, devip);
1187 if (errsts)
1188 return errsts;
1189 power_cond = (cmd[4] & 0xf0) >> 4; 1435 power_cond = (cmd[4] & 0xf0) >> 4;
1190 if (power_cond) { 1436 if (power_cond) {
1191 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7); 1437 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7);
@@ -1212,11 +1458,7 @@ static int resp_readcap(struct scsi_cmnd * scp,
1212{ 1458{
1213 unsigned char arr[SDEBUG_READCAP_ARR_SZ]; 1459 unsigned char arr[SDEBUG_READCAP_ARR_SZ];
1214 unsigned int capac; 1460 unsigned int capac;
1215 int errsts;
1216 1461
1217 errsts = check_readiness(scp, UAS_ONLY, devip);
1218 if (errsts)
1219 return errsts;
1220 /* following just in case virtual_gb changed */ 1462 /* following just in case virtual_gb changed */
1221 sdebug_capacity = get_sdebug_capacity(); 1463 sdebug_capacity = get_sdebug_capacity();
1222 memset(arr, 0, SDEBUG_READCAP_ARR_SZ); 1464 memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
@@ -1244,11 +1486,8 @@ static int resp_readcap16(struct scsi_cmnd * scp,
1244 unsigned char *cmd = scp->cmnd; 1486 unsigned char *cmd = scp->cmnd;
1245 unsigned char arr[SDEBUG_READCAP16_ARR_SZ]; 1487 unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
1246 unsigned long long capac; 1488 unsigned long long capac;
1247 int errsts, k, alloc_len; 1489 int k, alloc_len;
1248 1490
1249 errsts = check_readiness(scp, UAS_ONLY, devip);
1250 if (errsts)
1251 return errsts;
1252 alloc_len = ((cmd[10] << 24) + (cmd[11] << 16) + (cmd[12] << 8) 1491 alloc_len = ((cmd[10] << 24) + (cmd[11] << 16) + (cmd[12] << 8)
1253 + cmd[13]); 1492 + cmd[13]);
1254 /* following just in case virtual_gb changed */ 1493 /* following just in case virtual_gb changed */
@@ -1523,20 +1762,18 @@ static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol)
1523 1762
1524#define SDEBUG_MAX_MSENSE_SZ 256 1763#define SDEBUG_MAX_MSENSE_SZ 256
1525 1764
1526static int resp_mode_sense(struct scsi_cmnd * scp, int target, 1765static int
1527 struct sdebug_dev_info * devip) 1766resp_mode_sense(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
1528{ 1767{
1529 unsigned char dbd, llbaa; 1768 unsigned char dbd, llbaa;
1530 int pcontrol, pcode, subpcode, bd_len; 1769 int pcontrol, pcode, subpcode, bd_len;
1531 unsigned char dev_spec; 1770 unsigned char dev_spec;
1532 int k, alloc_len, msense_6, offset, len, errsts, target_dev_id; 1771 int k, alloc_len, msense_6, offset, len, target_dev_id;
1772 int target = scp->device->id;
1533 unsigned char * ap; 1773 unsigned char * ap;
1534 unsigned char arr[SDEBUG_MAX_MSENSE_SZ]; 1774 unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
1535 unsigned char *cmd = scp->cmnd; 1775 unsigned char *cmd = scp->cmnd;
1536 1776
1537 errsts = check_readiness(scp, UAS_ONLY, devip);
1538 if (errsts)
1539 return errsts;
1540 dbd = !!(cmd[1] & 0x8); 1777 dbd = !!(cmd[1] & 0x8);
1541 pcontrol = (cmd[2] & 0xc0) >> 6; 1778 pcontrol = (cmd[2] & 0xc0) >> 6;
1542 pcode = cmd[2] & 0x3f; 1779 pcode = cmd[2] & 0x3f;
@@ -1684,17 +1921,15 @@ static int resp_mode_sense(struct scsi_cmnd * scp, int target,
1684 1921
1685#define SDEBUG_MAX_MSELECT_SZ 512 1922#define SDEBUG_MAX_MSELECT_SZ 512
1686 1923
1687static int resp_mode_select(struct scsi_cmnd * scp, int mselect6, 1924static int
1688 struct sdebug_dev_info * devip) 1925resp_mode_select(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
1689{ 1926{
1690 int pf, sp, ps, md_len, bd_len, off, spf, pg_len; 1927 int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
1691 int param_len, res, errsts, mpage; 1928 int param_len, res, mpage;
1692 unsigned char arr[SDEBUG_MAX_MSELECT_SZ]; 1929 unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
1693 unsigned char *cmd = scp->cmnd; 1930 unsigned char *cmd = scp->cmnd;
1931 int mselect6 = (MODE_SELECT == cmd[0]);
1694 1932
1695 errsts = check_readiness(scp, UAS_ONLY, devip);
1696 if (errsts)
1697 return errsts;
1698 memset(arr, 0, sizeof(arr)); 1933 memset(arr, 0, sizeof(arr));
1699 pf = cmd[1] & 0x10; 1934 pf = cmd[1] & 0x10;
1700 sp = cmd[1] & 0x1; 1935 sp = cmd[1] & 0x1;
@@ -1793,13 +2028,10 @@ static int resp_ie_l_pg(unsigned char * arr)
1793static int resp_log_sense(struct scsi_cmnd * scp, 2028static int resp_log_sense(struct scsi_cmnd * scp,
1794 struct sdebug_dev_info * devip) 2029 struct sdebug_dev_info * devip)
1795{ 2030{
1796 int ppc, sp, pcontrol, pcode, subpcode, alloc_len, errsts, len, n; 2031 int ppc, sp, pcontrol, pcode, subpcode, alloc_len, len, n;
1797 unsigned char arr[SDEBUG_MAX_LSENSE_SZ]; 2032 unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
1798 unsigned char *cmd = scp->cmnd; 2033 unsigned char *cmd = scp->cmnd;
1799 2034
1800 errsts = check_readiness(scp, UAS_ONLY, devip);
1801 if (errsts)
1802 return errsts;
1803 memset(arr, 0, sizeof(arr)); 2035 memset(arr, 0, sizeof(arr));
1804 ppc = cmd[1] & 0x2; 2036 ppc = cmd[1] & 0x2;
1805 sp = cmd[1] & 0x1; 2037 sp = cmd[1] & 0x1;
@@ -1889,17 +2121,17 @@ static int check_device_access_params(struct scsi_cmnd *scp,
1889} 2121}
1890 2122
1891/* Returns number of bytes copied or -1 if error. */ 2123/* Returns number of bytes copied or -1 if error. */
1892static int do_device_access(struct scsi_cmnd *scmd, 2124static int
1893 unsigned long long lba, unsigned int num, int write) 2125do_device_access(struct scsi_cmnd *scmd, u64 lba, u32 num, bool do_write)
1894{ 2126{
1895 int ret; 2127 int ret;
1896 unsigned long long block, rest = 0; 2128 u64 block, rest = 0;
1897 struct scsi_data_buffer *sdb; 2129 struct scsi_data_buffer *sdb;
1898 enum dma_data_direction dir; 2130 enum dma_data_direction dir;
1899 size_t (*func)(struct scatterlist *, unsigned int, void *, size_t, 2131 size_t (*func)(struct scatterlist *, unsigned int, void *, size_t,
1900 off_t); 2132 off_t);
1901 2133
1902 if (write) { 2134 if (do_write) {
1903 sdb = scsi_out(scmd); 2135 sdb = scsi_out(scmd);
1904 dir = DMA_TO_DEVICE; 2136 dir = DMA_TO_DEVICE;
1905 func = sg_pcopy_to_buffer; 2137 func = sg_pcopy_to_buffer;
@@ -2045,55 +2277,143 @@ static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec,
2045 return 0; 2277 return 0;
2046} 2278}
2047 2279
2048static int resp_read(struct scsi_cmnd *SCpnt, unsigned long long lba, 2280static int
2049 unsigned int num, u32 ei_lba) 2281resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
2050{ 2282{
2283 u8 *cmd = scp->cmnd;
2284 u64 lba;
2285 u32 num;
2286 u32 ei_lba;
2051 unsigned long iflags; 2287 unsigned long iflags;
2052 int ret; 2288 int ret;
2289 bool check_prot;
2053 2290
2054 ret = check_device_access_params(SCpnt, lba, num); 2291 switch (cmd[0]) {
2055 if (ret) 2292 case READ_16:
2056 return ret; 2293 ei_lba = 0;
2294 lba = get_unaligned_be64(cmd + 2);
2295 num = get_unaligned_be32(cmd + 10);
2296 check_prot = true;
2297 break;
2298 case READ_10:
2299 ei_lba = 0;
2300 lba = get_unaligned_be32(cmd + 2);
2301 num = get_unaligned_be16(cmd + 7);
2302 check_prot = true;
2303 break;
2304 case READ_6:
2305 ei_lba = 0;
2306 lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
2307 (u32)(cmd[1] & 0x1f) << 16;
2308 num = (0 == cmd[4]) ? 256 : cmd[4];
2309 check_prot = true;
2310 break;
2311 case READ_12:
2312 ei_lba = 0;
2313 lba = get_unaligned_be32(cmd + 2);
2314 num = get_unaligned_be32(cmd + 6);
2315 check_prot = true;
2316 break;
2317 case XDWRITEREAD_10:
2318 ei_lba = 0;
2319 lba = get_unaligned_be32(cmd + 2);
2320 num = get_unaligned_be16(cmd + 7);
2321 check_prot = false;
2322 break;
2323 default: /* assume READ(32) */
2324 lba = get_unaligned_be64(cmd + 12);
2325 ei_lba = get_unaligned_be32(cmd + 20);
2326 num = get_unaligned_be32(cmd + 28);
2327 check_prot = false;
2328 break;
2329 }
2330 if (check_prot) {
2331 if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION &&
2332 (cmd[1] & 0xe0)) {
2333 mk_sense_invalid_opcode(scp);
2334 return check_condition_result;
2335 }
2336 if ((scsi_debug_dif == SD_DIF_TYPE1_PROTECTION ||
2337 scsi_debug_dif == SD_DIF_TYPE3_PROTECTION) &&
2338 (cmd[1] & 0xe0) == 0)
2339 sdev_printk(KERN_ERR, scp->device, "Unprotected RD "
2340 "to DIF device\n");
2341 }
2342 if (sdebug_any_injecting_opt) {
2343 struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp);
2344
2345 if (ep->inj_short)
2346 num /= 2;
2347 }
2348
2349 /* inline check_device_access_params() */
2350 if (lba + num > sdebug_capacity) {
2351 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
2352 return check_condition_result;
2353 }
2354 /* transfer length excessive (tie in to block limits VPD page) */
2355 if (num > sdebug_store_sectors) {
2356 /* needs work to find which cdb byte 'num' comes from */
2357 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
2358 return check_condition_result;
2359 }
2057 2360
2058 if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) && 2361 if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) &&
2059 (lba <= (OPT_MEDIUM_ERR_ADDR + OPT_MEDIUM_ERR_NUM - 1)) && 2362 (lba <= (OPT_MEDIUM_ERR_ADDR + OPT_MEDIUM_ERR_NUM - 1)) &&
2060 ((lba + num) > OPT_MEDIUM_ERR_ADDR)) { 2363 ((lba + num) > OPT_MEDIUM_ERR_ADDR)) {
2061 /* claim unrecoverable read error */ 2364 /* claim unrecoverable read error */
2062 mk_sense_buffer(SCpnt, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0); 2365 mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0);
2063 /* set info field and valid bit for fixed descriptor */ 2366 /* set info field and valid bit for fixed descriptor */
2064 if (0x70 == (SCpnt->sense_buffer[0] & 0x7f)) { 2367 if (0x70 == (scp->sense_buffer[0] & 0x7f)) {
2065 SCpnt->sense_buffer[0] |= 0x80; /* Valid bit */ 2368 scp->sense_buffer[0] |= 0x80; /* Valid bit */
2066 ret = (lba < OPT_MEDIUM_ERR_ADDR) 2369 ret = (lba < OPT_MEDIUM_ERR_ADDR)
2067 ? OPT_MEDIUM_ERR_ADDR : (int)lba; 2370 ? OPT_MEDIUM_ERR_ADDR : (int)lba;
2068 SCpnt->sense_buffer[3] = (ret >> 24) & 0xff; 2371 put_unaligned_be32(ret, scp->sense_buffer + 3);
2069 SCpnt->sense_buffer[4] = (ret >> 16) & 0xff;
2070 SCpnt->sense_buffer[5] = (ret >> 8) & 0xff;
2071 SCpnt->sense_buffer[6] = ret & 0xff;
2072 } 2372 }
2073 scsi_set_resid(SCpnt, scsi_bufflen(SCpnt)); 2373 scsi_set_resid(scp, scsi_bufflen(scp));
2074 return check_condition_result; 2374 return check_condition_result;
2075 } 2375 }
2076 2376
2077 read_lock_irqsave(&atomic_rw, iflags); 2377 read_lock_irqsave(&atomic_rw, iflags);
2078 2378
2079 /* DIX + T10 DIF */ 2379 /* DIX + T10 DIF */
2080 if (scsi_debug_dix && scsi_prot_sg_count(SCpnt)) { 2380 if (scsi_debug_dix && scsi_prot_sg_count(scp)) {
2081 int prot_ret = prot_verify_read(SCpnt, lba, num, ei_lba); 2381 int prot_ret = prot_verify_read(scp, lba, num, ei_lba);
2082 2382
2083 if (prot_ret) { 2383 if (prot_ret) {
2084 read_unlock_irqrestore(&atomic_rw, iflags); 2384 read_unlock_irqrestore(&atomic_rw, iflags);
2085 mk_sense_buffer(SCpnt, ABORTED_COMMAND, 0x10, prot_ret); 2385 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret);
2086 return illegal_condition_result; 2386 return illegal_condition_result;
2087 } 2387 }
2088 } 2388 }
2089 2389
2090 ret = do_device_access(SCpnt, lba, num, 0); 2390 ret = do_device_access(scp, lba, num, false);
2091 read_unlock_irqrestore(&atomic_rw, iflags); 2391 read_unlock_irqrestore(&atomic_rw, iflags);
2092 if (ret == -1) 2392 if (ret == -1)
2093 return DID_ERROR << 16; 2393 return DID_ERROR << 16;
2094 2394
2095 scsi_in(SCpnt)->resid = scsi_bufflen(SCpnt) - ret; 2395 scsi_in(scp)->resid = scsi_bufflen(scp) - ret;
2396
2397 if (sdebug_any_injecting_opt) {
2398 struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp);
2096 2399
2400 if (ep->inj_recovered) {
2401 mk_sense_buffer(scp, RECOVERED_ERROR,
2402 THRESHOLD_EXCEEDED, 0);
2403 return check_condition_result;
2404 } else if (ep->inj_transport) {
2405 mk_sense_buffer(scp, ABORTED_COMMAND,
2406 TRANSPORT_PROBLEM, ACK_NAK_TO);
2407 return check_condition_result;
2408 } else if (ep->inj_dif) {
2409 /* Logical block guard check failed */
2410 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
2411 return illegal_condition_result;
2412 } else if (ep->inj_dix) {
2413 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
2414 return illegal_condition_result;
2415 }
2416 }
2097 return 0; 2417 return 0;
2098} 2418}
2099 2419
@@ -2276,31 +2596,95 @@ static void unmap_region(sector_t lba, unsigned int len)
2276 } 2596 }
2277} 2597}
2278 2598
2279static int resp_write(struct scsi_cmnd *SCpnt, unsigned long long lba, 2599static int
2280 unsigned int num, u32 ei_lba) 2600resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
2281{ 2601{
2602 u8 *cmd = scp->cmnd;
2603 u64 lba;
2604 u32 num;
2605 u32 ei_lba;
2282 unsigned long iflags; 2606 unsigned long iflags;
2283 int ret; 2607 int ret;
2608 bool check_prot;
2284 2609
2285 ret = check_device_access_params(SCpnt, lba, num); 2610 switch (cmd[0]) {
2286 if (ret) 2611 case WRITE_16:
2287 return ret; 2612 ei_lba = 0;
2613 lba = get_unaligned_be64(cmd + 2);
2614 num = get_unaligned_be32(cmd + 10);
2615 check_prot = true;
2616 break;
2617 case WRITE_10:
2618 ei_lba = 0;
2619 lba = get_unaligned_be32(cmd + 2);
2620 num = get_unaligned_be16(cmd + 7);
2621 check_prot = true;
2622 break;
2623 case WRITE_6:
2624 ei_lba = 0;
2625 lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
2626 (u32)(cmd[1] & 0x1f) << 16;
2627 num = (0 == cmd[4]) ? 256 : cmd[4];
2628 check_prot = true;
2629 break;
2630 case WRITE_12:
2631 ei_lba = 0;
2632 lba = get_unaligned_be32(cmd + 2);
2633 num = get_unaligned_be32(cmd + 6);
2634 check_prot = true;
2635 break;
2636 case 0x53: /* XDWRITEREAD(10) */
2637 ei_lba = 0;
2638 lba = get_unaligned_be32(cmd + 2);
2639 num = get_unaligned_be16(cmd + 7);
2640 check_prot = false;
2641 break;
2642 default: /* assume WRITE(32) */
2643 lba = get_unaligned_be64(cmd + 12);
2644 ei_lba = get_unaligned_be32(cmd + 20);
2645 num = get_unaligned_be32(cmd + 28);
2646 check_prot = false;
2647 break;
2648 }
2649 if (check_prot) {
2650 if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION &&
2651 (cmd[1] & 0xe0)) {
2652 mk_sense_invalid_opcode(scp);
2653 return check_condition_result;
2654 }
2655 if ((scsi_debug_dif == SD_DIF_TYPE1_PROTECTION ||
2656 scsi_debug_dif == SD_DIF_TYPE3_PROTECTION) &&
2657 (cmd[1] & 0xe0) == 0)
2658 sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
2659 "to DIF device\n");
2660 }
2661
2662 /* inline check_device_access_params() */
2663 if (lba + num > sdebug_capacity) {
2664 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
2665 return check_condition_result;
2666 }
2667 /* transfer length excessive (tie in to block limits VPD page) */
2668 if (num > sdebug_store_sectors) {
2669 /* needs work to find which cdb byte 'num' comes from */
2670 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
2671 return check_condition_result;
2672 }
2288 2673
2289 write_lock_irqsave(&atomic_rw, iflags); 2674 write_lock_irqsave(&atomic_rw, iflags);
2290 2675
2291 /* DIX + T10 DIF */ 2676 /* DIX + T10 DIF */
2292 if (scsi_debug_dix && scsi_prot_sg_count(SCpnt)) { 2677 if (scsi_debug_dix && scsi_prot_sg_count(scp)) {
2293 int prot_ret = prot_verify_write(SCpnt, lba, num, ei_lba); 2678 int prot_ret = prot_verify_write(scp, lba, num, ei_lba);
2294 2679
2295 if (prot_ret) { 2680 if (prot_ret) {
2296 write_unlock_irqrestore(&atomic_rw, iflags); 2681 write_unlock_irqrestore(&atomic_rw, iflags);
2297 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST, 0x10, 2682 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret);
2298 prot_ret);
2299 return illegal_condition_result; 2683 return illegal_condition_result;
2300 } 2684 }
2301 } 2685 }
2302 2686
2303 ret = do_device_access(SCpnt, lba, num, 1); 2687 ret = do_device_access(scp, lba, num, true);
2304 if (scsi_debug_lbp()) 2688 if (scsi_debug_lbp())
2305 map_region(lba, num); 2689 map_region(lba, num);
2306 write_unlock_irqrestore(&atomic_rw, iflags); 2690 write_unlock_irqrestore(&atomic_rw, iflags);
@@ -2308,30 +2692,41 @@ static int resp_write(struct scsi_cmnd *SCpnt, unsigned long long lba,
2308 return (DID_ERROR << 16); 2692 return (DID_ERROR << 16);
2309 else if ((ret < (num * scsi_debug_sector_size)) && 2693 else if ((ret < (num * scsi_debug_sector_size)) &&
2310 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)) 2694 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
2311 sdev_printk(KERN_INFO, SCpnt->device, 2695 sdev_printk(KERN_INFO, scp->device,
2312 "%s: write: cdb indicated=%u, IO sent=%d bytes\n", 2696 "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
2313 my_name, num * scsi_debug_sector_size, ret); 2697 my_name, num * scsi_debug_sector_size, ret);
2314 2698
2699 if (sdebug_any_injecting_opt) {
2700 struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp);
2701
2702 if (ep->inj_recovered) {
2703 mk_sense_buffer(scp, RECOVERED_ERROR,
2704 THRESHOLD_EXCEEDED, 0);
2705 return check_condition_result;
2706 } else if (ep->inj_dif) {
2707 /* Logical block guard check failed */
2708 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
2709 return illegal_condition_result;
2710 } else if (ep->inj_dix) {
2711 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
2712 return illegal_condition_result;
2713 }
2714 }
2315 return 0; 2715 return 0;
2316} 2716}
2317 2717
2318static int resp_write_same(struct scsi_cmnd *scmd, unsigned long long lba, 2718static int
2319 unsigned int num, u32 ei_lba, unsigned int unmap) 2719resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num, u32 ei_lba,
2720 bool unmap, bool ndob)
2320{ 2721{
2321 unsigned long iflags; 2722 unsigned long iflags;
2322 unsigned long long i; 2723 unsigned long long i;
2323 int ret; 2724 int ret;
2324 2725
2325 ret = check_device_access_params(scmd, lba, num); 2726 ret = check_device_access_params(scp, lba, num);
2326 if (ret) 2727 if (ret)
2327 return ret; 2728 return ret;
2328 2729
2329 if (num > scsi_debug_write_same_length) {
2330 mk_sense_buffer(scmd, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
2331 0);
2332 return check_condition_result;
2333 }
2334
2335 write_lock_irqsave(&atomic_rw, iflags); 2730 write_lock_irqsave(&atomic_rw, iflags);
2336 2731
2337 if (unmap && scsi_debug_lbp()) { 2732 if (unmap && scsi_debug_lbp()) {
@@ -2339,17 +2734,22 @@ static int resp_write_same(struct scsi_cmnd *scmd, unsigned long long lba,
2339 goto out; 2734 goto out;
2340 } 2735 }
2341 2736
2342 /* Else fetch one logical block */ 2737 /* if ndob then zero 1 logical block, else fetch 1 logical block */
2343 ret = fetch_to_dev_buffer(scmd, 2738 if (ndob) {
2344 fake_storep + (lba * scsi_debug_sector_size), 2739 memset(fake_storep + (lba * scsi_debug_sector_size), 0,
2345 scsi_debug_sector_size); 2740 scsi_debug_sector_size);
2741 ret = 0;
2742 } else
2743 ret = fetch_to_dev_buffer(scp, fake_storep +
2744 (lba * scsi_debug_sector_size),
2745 scsi_debug_sector_size);
2346 2746
2347 if (-1 == ret) { 2747 if (-1 == ret) {
2348 write_unlock_irqrestore(&atomic_rw, iflags); 2748 write_unlock_irqrestore(&atomic_rw, iflags);
2349 return (DID_ERROR << 16); 2749 return (DID_ERROR << 16);
2350 } else if ((ret < (num * scsi_debug_sector_size)) && 2750 } else if ((ret < (num * scsi_debug_sector_size)) &&
2351 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)) 2751 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
2352 sdev_printk(KERN_INFO, scmd->device, 2752 sdev_printk(KERN_INFO, scp->device,
2353 "%s: %s: cdb indicated=%u, IO sent=%d bytes\n", 2753 "%s: %s: cdb indicated=%u, IO sent=%d bytes\n",
2354 my_name, "write same", 2754 my_name, "write same",
2355 num * scsi_debug_sector_size, ret); 2755 num * scsi_debug_sector_size, ret);
@@ -2368,13 +2768,67 @@ out:
2368 return 0; 2768 return 0;
2369} 2769}
2370 2770
2771static int
2772resp_write_same_10(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
2773{
2774 u8 *cmd = scp->cmnd;
2775 u32 lba;
2776 u16 num;
2777 u32 ei_lba = 0;
2778 bool unmap = false;
2779
2780 if (cmd[1] & 0x8) {
2781 if (scsi_debug_lbpws10 == 0) {
2782 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
2783 return check_condition_result;
2784 } else
2785 unmap = true;
2786 }
2787 lba = get_unaligned_be32(cmd + 2);
2788 num = get_unaligned_be16(cmd + 7);
2789 if (num > scsi_debug_write_same_length) {
2790 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
2791 return check_condition_result;
2792 }
2793 return resp_write_same(scp, lba, num, ei_lba, unmap, false);
2794}
2795
2796static int
2797resp_write_same_16(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
2798{
2799 u8 *cmd = scp->cmnd;
2800 u64 lba;
2801 u32 num;
2802 u32 ei_lba = 0;
2803 bool unmap = false;
2804 bool ndob = false;
2805
2806 if (cmd[1] & 0x8) { /* UNMAP */
2807 if (scsi_debug_lbpws == 0) {
2808 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
2809 return check_condition_result;
2810 } else
2811 unmap = true;
2812 }
2813 if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */
2814 ndob = true;
2815 lba = get_unaligned_be64(cmd + 2);
2816 num = get_unaligned_be32(cmd + 10);
2817 if (num > scsi_debug_write_same_length) {
2818 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1);
2819 return check_condition_result;
2820 }
2821 return resp_write_same(scp, lba, num, ei_lba, unmap, ndob);
2822}
2823
2371struct unmap_block_desc { 2824struct unmap_block_desc {
2372 __be64 lba; 2825 __be64 lba;
2373 __be32 blocks; 2826 __be32 blocks;
2374 __be32 __reserved; 2827 __be32 __reserved;
2375}; 2828};
2376 2829
2377static int resp_unmap(struct scsi_cmnd * scmd, struct sdebug_dev_info * devip) 2830static int
2831resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
2378{ 2832{
2379 unsigned char *buf; 2833 unsigned char *buf;
2380 struct unmap_block_desc *desc; 2834 struct unmap_block_desc *desc;
@@ -2382,20 +2836,26 @@ static int resp_unmap(struct scsi_cmnd * scmd, struct sdebug_dev_info * devip)
2382 int ret; 2836 int ret;
2383 unsigned long iflags; 2837 unsigned long iflags;
2384 2838
2385 ret = check_readiness(scmd, UAS_ONLY, devip);
2386 if (ret)
2387 return ret;
2388 2839
2389 payload_len = get_unaligned_be16(&scmd->cmnd[7]); 2840 if (!scsi_debug_lbp())
2390 BUG_ON(scsi_bufflen(scmd) != payload_len); 2841 return 0; /* fib and say its done */
2842 payload_len = get_unaligned_be16(scp->cmnd + 7);
2843 BUG_ON(scsi_bufflen(scp) != payload_len);
2391 2844
2392 descriptors = (payload_len - 8) / 16; 2845 descriptors = (payload_len - 8) / 16;
2846 if (descriptors > scsi_debug_unmap_max_desc) {
2847 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
2848 return check_condition_result;
2849 }
2393 2850
2394 buf = kmalloc(scsi_bufflen(scmd), GFP_ATOMIC); 2851 buf = kmalloc(scsi_bufflen(scp), GFP_ATOMIC);
2395 if (!buf) 2852 if (!buf) {
2853 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
2854 INSUFF_RES_ASCQ);
2396 return check_condition_result; 2855 return check_condition_result;
2856 }
2397 2857
2398 scsi_sg_copy_to_buffer(scmd, buf, scsi_bufflen(scmd)); 2858 scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
2399 2859
2400 BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2); 2860 BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2);
2401 BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16); 2861 BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16);
@@ -2408,7 +2868,7 @@ static int resp_unmap(struct scsi_cmnd * scmd, struct sdebug_dev_info * devip)
2408 unsigned long long lba = get_unaligned_be64(&desc[i].lba); 2868 unsigned long long lba = get_unaligned_be64(&desc[i].lba);
2409 unsigned int num = get_unaligned_be32(&desc[i].blocks); 2869 unsigned int num = get_unaligned_be32(&desc[i].blocks);
2410 2870
2411 ret = check_device_access_params(scmd, lba, num); 2871 ret = check_device_access_params(scp, lba, num);
2412 if (ret) 2872 if (ret)
2413 goto out; 2873 goto out;
2414 2874
@@ -2426,37 +2886,44 @@ out:
2426 2886
2427#define SDEBUG_GET_LBA_STATUS_LEN 32 2887#define SDEBUG_GET_LBA_STATUS_LEN 32
2428 2888
2429static int resp_get_lba_status(struct scsi_cmnd * scmd, 2889static int
2430 struct sdebug_dev_info * devip) 2890resp_get_lba_status(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
2431{ 2891{
2432 unsigned long long lba; 2892 u8 *cmd = scp->cmnd;
2433 unsigned int alloc_len, mapped, num; 2893 u64 lba;
2434 unsigned char arr[SDEBUG_GET_LBA_STATUS_LEN]; 2894 u32 alloc_len, mapped, num;
2895 u8 arr[SDEBUG_GET_LBA_STATUS_LEN];
2435 int ret; 2896 int ret;
2436 2897
2437 ret = check_readiness(scmd, UAS_ONLY, devip); 2898 lba = get_unaligned_be64(cmd + 2);
2438 if (ret) 2899 alloc_len = get_unaligned_be32(cmd + 10);
2439 return ret;
2440
2441 lba = get_unaligned_be64(&scmd->cmnd[2]);
2442 alloc_len = get_unaligned_be32(&scmd->cmnd[10]);
2443 2900
2444 if (alloc_len < 24) 2901 if (alloc_len < 24)
2445 return 0; 2902 return 0;
2446 2903
2447 ret = check_device_access_params(scmd, lba, 1); 2904 ret = check_device_access_params(scp, lba, 1);
2448 if (ret) 2905 if (ret)
2449 return ret; 2906 return ret;
2450 2907
2451 mapped = map_state(lba, &num); 2908 if (scsi_debug_lbp())
2909 mapped = map_state(lba, &num);
2910 else {
2911 mapped = 1;
2912 /* following just in case virtual_gb changed */
2913 sdebug_capacity = get_sdebug_capacity();
2914 if (sdebug_capacity - lba <= 0xffffffff)
2915 num = sdebug_capacity - lba;
2916 else
2917 num = 0xffffffff;
2918 }
2452 2919
2453 memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN); 2920 memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN);
2454 put_unaligned_be32(20, &arr[0]); /* Parameter Data Length */ 2921 put_unaligned_be32(20, arr); /* Parameter Data Length */
2455 put_unaligned_be64(lba, &arr[8]); /* LBA */ 2922 put_unaligned_be64(lba, arr + 8); /* LBA */
2456 put_unaligned_be32(num, &arr[16]); /* Number of blocks */ 2923 put_unaligned_be32(num, arr + 16); /* Number of blocks */
2457 arr[20] = !mapped; /* mapped = 0, unmapped = 1 */ 2924 arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */
2458 2925
2459 return fill_from_dev_buffer(scmd, arr, SDEBUG_GET_LBA_STATUS_LEN); 2926 return fill_from_dev_buffer(scp, arr, SDEBUG_GET_LBA_STATUS_LEN);
2460} 2927}
2461 2928
2462#define SDEBUG_RLUN_ARR_SZ 256 2929#define SDEBUG_RLUN_ARR_SZ 256
@@ -2553,6 +3020,32 @@ static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba,
2553 return 0; 3020 return 0;
2554} 3021}
2555 3022
3023static int
3024resp_xdwriteread_10(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
3025{
3026 u8 *cmd = scp->cmnd;
3027 u64 lba;
3028 u32 num;
3029 int errsts;
3030
3031 if (!scsi_bidi_cmnd(scp)) {
3032 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
3033 INSUFF_RES_ASCQ);
3034 return check_condition_result;
3035 }
3036 errsts = resp_read_dt0(scp, devip);
3037 if (errsts)
3038 return errsts;
3039 if (!(cmd[1] & 0x4)) { /* DISABLE_WRITE is not set */
3040 errsts = resp_write_dt0(scp, devip);
3041 if (errsts)
3042 return errsts;
3043 }
3044 lba = get_unaligned_be32(cmd + 2);
3045 num = get_unaligned_be16(cmd + 7);
3046 return resp_xdwriteread(scp, lba, num, devip);
3047}
3048
2556/* When timer or tasklet goes off this function is called. */ 3049/* When timer or tasklet goes off this function is called. */
2557static void sdebug_q_cmd_complete(unsigned long indx) 3050static void sdebug_q_cmd_complete(unsigned long indx)
2558{ 3051{
@@ -2725,10 +3218,7 @@ static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev)
2725 open_devip->sdbg_host = sdbg_host; 3218 open_devip->sdbg_host = sdbg_host;
2726 atomic_set(&open_devip->num_in_q, 0); 3219 atomic_set(&open_devip->num_in_q, 0);
2727 set_bit(SDEBUG_UA_POR, open_devip->uas_bm); 3220 set_bit(SDEBUG_UA_POR, open_devip->uas_bm);
2728 open_devip->used = 1; 3221 open_devip->used = true;
2729 if (sdev->lun == SAM2_WLUN_REPORT_LUNS)
2730 open_devip->wlun = SAM2_WLUN_REPORT_LUNS & 0xff;
2731
2732 return open_devip; 3222 return open_devip;
2733} 3223}
2734 3224
@@ -2770,7 +3260,7 @@ static void scsi_debug_slave_destroy(struct scsi_device *sdp)
2770 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); 3260 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
2771 if (devip) { 3261 if (devip) {
2772 /* make this slot available for re-use */ 3262 /* make this slot available for re-use */
2773 devip->used = 0; 3263 devip->used = false;
2774 sdp->hostdata = NULL; 3264 sdp->hostdata = NULL;
2775 } 3265 }
2776} 3266}
@@ -3215,6 +3705,7 @@ module_param_named(ptype, scsi_debug_ptype, int, S_IRUGO | S_IWUSR);
3215module_param_named(removable, scsi_debug_removable, bool, S_IRUGO | S_IWUSR); 3705module_param_named(removable, scsi_debug_removable, bool, S_IRUGO | S_IWUSR);
3216module_param_named(scsi_level, scsi_debug_scsi_level, int, S_IRUGO); 3706module_param_named(scsi_level, scsi_debug_scsi_level, int, S_IRUGO);
3217module_param_named(sector_size, scsi_debug_sector_size, int, S_IRUGO); 3707module_param_named(sector_size, scsi_debug_sector_size, int, S_IRUGO);
3708module_param_named(strict, scsi_debug_strict, bool, S_IRUGO | S_IWUSR);
3218module_param_named(unmap_alignment, scsi_debug_unmap_alignment, int, S_IRUGO); 3709module_param_named(unmap_alignment, scsi_debug_unmap_alignment, int, S_IRUGO);
3219module_param_named(unmap_granularity, scsi_debug_unmap_granularity, int, S_IRUGO); 3710module_param_named(unmap_granularity, scsi_debug_unmap_granularity, int, S_IRUGO);
3220module_param_named(unmap_max_blocks, scsi_debug_unmap_max_blocks, int, S_IRUGO); 3711module_param_named(unmap_max_blocks, scsi_debug_unmap_max_blocks, int, S_IRUGO);
@@ -3234,7 +3725,7 @@ MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
3234MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)"); 3725MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)");
3235MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)"); 3726MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)");
3236MODULE_PARM_DESC(delay, "response delay (def=1 jiffy); 0:imm, -1,-2:tiny"); 3727MODULE_PARM_DESC(delay, "response delay (def=1 jiffy); 0:imm, -1,-2:tiny");
3237MODULE_PARM_DESC(dev_size_mb, "size in MB of ram shared by devs(def=8)"); 3728MODULE_PARM_DESC(dev_size_mb, "size in MiB of ram shared by devs(def=8)");
3238MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)"); 3729MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)");
3239MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)"); 3730MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)");
3240MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)"); 3731MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
@@ -3261,11 +3752,12 @@ MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
3261MODULE_PARM_DESC(removable, "claim to have removable media (def=0)"); 3752MODULE_PARM_DESC(removable, "claim to have removable media (def=0)");
3262MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=6[SPC-4])"); 3753MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=6[SPC-4])");
3263MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)"); 3754MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)");
3755MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)");
3264MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)"); 3756MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)");
3265MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)"); 3757MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)");
3266MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)"); 3758MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)");
3267MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)"); 3759MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)");
3268MODULE_PARM_DESC(virtual_gb, "virtual gigabyte size (def=0 -> use dev_size_mb)"); 3760MODULE_PARM_DESC(virtual_gb, "virtual gigabyte (GiB) size (def=0 -> use dev_size_mb)");
3269MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)"); 3761MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
3270MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)"); 3762MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)");
3271 3763
@@ -3644,11 +4136,10 @@ static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf)
3644{ 4136{
3645 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_virtual_gb); 4137 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_virtual_gb);
3646} 4138}
3647
3648static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf, 4139static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf,
3649 size_t count) 4140 size_t count)
3650{ 4141{
3651 int n; 4142 int n;
3652 bool changed; 4143 bool changed;
3653 4144
3654 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { 4145 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
@@ -3813,6 +4304,23 @@ static ssize_t host_lock_store(struct device_driver *ddp, const char *buf,
3813} 4304}
3814static DRIVER_ATTR_RW(host_lock); 4305static DRIVER_ATTR_RW(host_lock);
3815 4306
4307static ssize_t strict_show(struct device_driver *ddp, char *buf)
4308{
4309 return scnprintf(buf, PAGE_SIZE, "%d\n", !!scsi_debug_strict);
4310}
4311static ssize_t strict_store(struct device_driver *ddp, const char *buf,
4312 size_t count)
4313{
4314 int n;
4315
4316 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
4317 scsi_debug_strict = (n > 0);
4318 return count;
4319 }
4320 return -EINVAL;
4321}
4322static DRIVER_ATTR_RW(strict);
4323
3816 4324
3817/* Note: The following array creates attribute files in the 4325/* Note: The following array creates attribute files in the
3818 /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these 4326 /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
@@ -3848,6 +4356,7 @@ static struct attribute *sdebug_drv_attrs[] = {
3848 &driver_attr_removable.attr, 4356 &driver_attr_removable.attr,
3849 &driver_attr_host_lock.attr, 4357 &driver_attr_host_lock.attr,
3850 &driver_attr_ndelay.attr, 4358 &driver_attr_ndelay.attr,
4359 &driver_attr_strict.attr,
3851 NULL, 4360 NULL,
3852}; 4361};
3853ATTRIBUTE_GROUPS(sdebug_drv); 4362ATTRIBUTE_GROUPS(sdebug_drv);
@@ -4160,377 +4669,6 @@ static void sdebug_remove_adapter(void)
4160} 4669}
4161 4670
4162static int 4671static int
4163scsi_debug_queuecommand(struct scsi_cmnd *SCpnt)
4164{
4165 unsigned char *cmd = SCpnt->cmnd;
4166 int len, k;
4167 unsigned int num;
4168 unsigned long long lba;
4169 u32 ei_lba;
4170 int errsts = 0;
4171 int target = SCpnt->device->id;
4172 struct sdebug_dev_info *devip = NULL;
4173 int inj_recovered = 0;
4174 int inj_transport = 0;
4175 int inj_dif = 0;
4176 int inj_dix = 0;
4177 int inj_short = 0;
4178 int delay_override = 0;
4179 int unmap = 0;
4180
4181 scsi_set_resid(SCpnt, 0);
4182 if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) &&
4183 !(SCSI_DEBUG_OPT_NO_CDB_NOISE & scsi_debug_opts)) {
4184 char b[120];
4185 int n;
4186
4187 len = SCpnt->cmd_len;
4188 if (len > 32)
4189 strcpy(b, "too long, over 32 bytes");
4190 else {
4191 for (k = 0, n = 0; k < len; ++k)
4192 n += scnprintf(b + n, sizeof(b) - n, "%02x ",
4193 (unsigned int)cmd[k]);
4194 }
4195 sdev_printk(KERN_INFO, SCpnt->device, "%s: cmd %s\n", my_name,
4196 b);
4197 }
4198
4199 if ((SCpnt->device->lun >= scsi_debug_max_luns) &&
4200 (SCpnt->device->lun != SAM2_WLUN_REPORT_LUNS))
4201 return schedule_resp(SCpnt, NULL, DID_NO_CONNECT << 16, 0);
4202 devip = devInfoReg(SCpnt->device);
4203 if (NULL == devip)
4204 return schedule_resp(SCpnt, NULL, DID_NO_CONNECT << 16, 0);
4205
4206 if ((scsi_debug_every_nth != 0) &&
4207 (atomic_inc_return(&sdebug_cmnd_count) >=
4208 abs(scsi_debug_every_nth))) {
4209 atomic_set(&sdebug_cmnd_count, 0);
4210 if (scsi_debug_every_nth < -1)
4211 scsi_debug_every_nth = -1;
4212 if (SCSI_DEBUG_OPT_TIMEOUT & scsi_debug_opts)
4213 return 0; /* ignore command causing timeout */
4214 else if (SCSI_DEBUG_OPT_MAC_TIMEOUT & scsi_debug_opts &&
4215 scsi_medium_access_command(SCpnt))
4216 return 0; /* time out reads and writes */
4217 else if (SCSI_DEBUG_OPT_RECOVERED_ERR & scsi_debug_opts)
4218 inj_recovered = 1; /* to reads and writes below */
4219 else if (SCSI_DEBUG_OPT_TRANSPORT_ERR & scsi_debug_opts)
4220 inj_transport = 1; /* to reads and writes below */
4221 else if (SCSI_DEBUG_OPT_DIF_ERR & scsi_debug_opts)
4222 inj_dif = 1; /* to reads and writes below */
4223 else if (SCSI_DEBUG_OPT_DIX_ERR & scsi_debug_opts)
4224 inj_dix = 1; /* to reads and writes below */
4225 else if (SCSI_DEBUG_OPT_SHORT_TRANSFER & scsi_debug_opts)
4226 inj_short = 1;
4227 }
4228
4229 if (devip->wlun) {
4230 switch (*cmd) {
4231 case INQUIRY:
4232 case REQUEST_SENSE:
4233 case TEST_UNIT_READY:
4234 case REPORT_LUNS:
4235 break; /* only allowable wlun commands */
4236 default:
4237 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
4238 printk(KERN_INFO "scsi_debug: Opcode: 0x%x "
4239 "not supported for wlun\n", *cmd);
4240 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
4241 INVALID_OPCODE, 0);
4242 errsts = check_condition_result;
4243 return schedule_resp(SCpnt, devip, errsts, 0);
4244 }
4245 }
4246
4247 switch (*cmd) {
4248 case INQUIRY: /* mandatory, ignore unit attention */
4249 delay_override = 1;
4250 errsts = resp_inquiry(SCpnt, target, devip);
4251 break;
4252 case REQUEST_SENSE: /* mandatory, ignore unit attention */
4253 delay_override = 1;
4254 errsts = resp_requests(SCpnt, devip);
4255 break;
4256 case REZERO_UNIT: /* actually this is REWIND for SSC */
4257 case START_STOP:
4258 errsts = resp_start_stop(SCpnt, devip);
4259 break;
4260 case ALLOW_MEDIUM_REMOVAL:
4261 errsts = check_readiness(SCpnt, UAS_ONLY, devip);
4262 if (errsts)
4263 break;
4264 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
4265 printk(KERN_INFO "scsi_debug: Medium removal %s\n",
4266 cmd[4] ? "inhibited" : "enabled");
4267 break;
4268 case SEND_DIAGNOSTIC: /* mandatory */
4269 errsts = check_readiness(SCpnt, UAS_ONLY, devip);
4270 break;
4271 case TEST_UNIT_READY: /* mandatory */
4272 /* delay_override = 1; */
4273 errsts = check_readiness(SCpnt, UAS_TUR, devip);
4274 break;
4275 case RESERVE:
4276 errsts = check_readiness(SCpnt, UAS_ONLY, devip);
4277 break;
4278 case RESERVE_10:
4279 errsts = check_readiness(SCpnt, UAS_ONLY, devip);
4280 break;
4281 case RELEASE:
4282 errsts = check_readiness(SCpnt, UAS_ONLY, devip);
4283 break;
4284 case RELEASE_10:
4285 errsts = check_readiness(SCpnt, UAS_ONLY, devip);
4286 break;
4287 case READ_CAPACITY:
4288 errsts = resp_readcap(SCpnt, devip);
4289 break;
4290 case SERVICE_ACTION_IN:
4291 if (cmd[1] == SAI_READ_CAPACITY_16)
4292 errsts = resp_readcap16(SCpnt, devip);
4293 else if (cmd[1] == SAI_GET_LBA_STATUS) {
4294
4295 if (scsi_debug_lbp() == 0) {
4296 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
4297 INVALID_COMMAND_OPCODE, 0);
4298 errsts = check_condition_result;
4299 } else
4300 errsts = resp_get_lba_status(SCpnt, devip);
4301 } else {
4302 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
4303 INVALID_OPCODE, 0);
4304 errsts = check_condition_result;
4305 }
4306 break;
4307 case MAINTENANCE_IN:
4308 if (MI_REPORT_TARGET_PGS != cmd[1]) {
4309 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
4310 INVALID_OPCODE, 0);
4311 errsts = check_condition_result;
4312 break;
4313 }
4314 errsts = resp_report_tgtpgs(SCpnt, devip);
4315 break;
4316 case READ_16:
4317 case READ_12:
4318 case READ_10:
4319 /* READ{10,12,16} and DIF Type 2 are natural enemies */
4320 if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION &&
4321 cmd[1] & 0xe0) {
4322 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
4323 INVALID_COMMAND_OPCODE, 0);
4324 errsts = check_condition_result;
4325 break;
4326 }
4327
4328 if ((scsi_debug_dif == SD_DIF_TYPE1_PROTECTION ||
4329 scsi_debug_dif == SD_DIF_TYPE3_PROTECTION) &&
4330 (cmd[1] & 0xe0) == 0)
4331 printk(KERN_ERR "Unprotected RD/WR to DIF device\n");
4332
4333 /* fall through */
4334 case READ_6:
4335read:
4336 errsts = check_readiness(SCpnt, UAS_TUR, devip);
4337 if (errsts)
4338 break;
4339 if (scsi_debug_fake_rw)
4340 break;
4341 get_data_transfer_info(cmd, &lba, &num, &ei_lba);
4342
4343 if (inj_short)
4344 num /= 2;
4345
4346 errsts = resp_read(SCpnt, lba, num, ei_lba);
4347 if (inj_recovered && (0 == errsts)) {
4348 mk_sense_buffer(SCpnt, RECOVERED_ERROR,
4349 THRESHOLD_EXCEEDED, 0);
4350 errsts = check_condition_result;
4351 } else if (inj_transport && (0 == errsts)) {
4352 mk_sense_buffer(SCpnt, ABORTED_COMMAND,
4353 TRANSPORT_PROBLEM, ACK_NAK_TO);
4354 errsts = check_condition_result;
4355 } else if (inj_dif && (0 == errsts)) {
4356 /* Logical block guard check failed */
4357 mk_sense_buffer(SCpnt, ABORTED_COMMAND, 0x10, 1);
4358 errsts = illegal_condition_result;
4359 } else if (inj_dix && (0 == errsts)) {
4360 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST, 0x10, 1);
4361 errsts = illegal_condition_result;
4362 }
4363 break;
4364 case REPORT_LUNS: /* mandatory, ignore unit attention */
4365 delay_override = 1;
4366 errsts = resp_report_luns(SCpnt, devip);
4367 break;
4368 case VERIFY: /* 10 byte SBC-2 command */
4369 errsts = check_readiness(SCpnt, UAS_TUR, devip);
4370 break;
4371 case WRITE_16:
4372 case WRITE_12:
4373 case WRITE_10:
4374 /* WRITE{10,12,16} and DIF Type 2 are natural enemies */
4375 if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION &&
4376 cmd[1] & 0xe0) {
4377 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
4378 INVALID_COMMAND_OPCODE, 0);
4379 errsts = check_condition_result;
4380 break;
4381 }
4382
4383 if ((scsi_debug_dif == SD_DIF_TYPE1_PROTECTION ||
4384 scsi_debug_dif == SD_DIF_TYPE3_PROTECTION) &&
4385 (cmd[1] & 0xe0) == 0)
4386 printk(KERN_ERR "Unprotected RD/WR to DIF device\n");
4387
4388 /* fall through */
4389 case WRITE_6:
4390write:
4391 errsts = check_readiness(SCpnt, UAS_TUR, devip);
4392 if (errsts)
4393 break;
4394 if (scsi_debug_fake_rw)
4395 break;
4396 get_data_transfer_info(cmd, &lba, &num, &ei_lba);
4397 errsts = resp_write(SCpnt, lba, num, ei_lba);
4398 if (inj_recovered && (0 == errsts)) {
4399 mk_sense_buffer(SCpnt, RECOVERED_ERROR,
4400 THRESHOLD_EXCEEDED, 0);
4401 errsts = check_condition_result;
4402 } else if (inj_dif && (0 == errsts)) {
4403 mk_sense_buffer(SCpnt, ABORTED_COMMAND, 0x10, 1);
4404 errsts = illegal_condition_result;
4405 } else if (inj_dix && (0 == errsts)) {
4406 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST, 0x10, 1);
4407 errsts = illegal_condition_result;
4408 }
4409 break;
4410 case WRITE_SAME_16:
4411 case WRITE_SAME:
4412 if (cmd[1] & 0x8) {
4413 if ((*cmd == WRITE_SAME_16 && scsi_debug_lbpws == 0) ||
4414 (*cmd == WRITE_SAME && scsi_debug_lbpws10 == 0)) {
4415 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
4416 INVALID_FIELD_IN_CDB, 0);
4417 errsts = check_condition_result;
4418 } else
4419 unmap = 1;
4420 }
4421 if (errsts)
4422 break;
4423 errsts = check_readiness(SCpnt, UAS_TUR, devip);
4424 if (errsts)
4425 break;
4426 if (scsi_debug_fake_rw)
4427 break;
4428 get_data_transfer_info(cmd, &lba, &num, &ei_lba);
4429 errsts = resp_write_same(SCpnt, lba, num, ei_lba, unmap);
4430 break;
4431 case UNMAP:
4432 errsts = check_readiness(SCpnt, UAS_TUR, devip);
4433 if (errsts)
4434 break;
4435 if (scsi_debug_fake_rw)
4436 break;
4437
4438 if (scsi_debug_unmap_max_desc == 0 || scsi_debug_lbpu == 0) {
4439 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
4440 INVALID_COMMAND_OPCODE, 0);
4441 errsts = check_condition_result;
4442 } else
4443 errsts = resp_unmap(SCpnt, devip);
4444 break;
4445 case MODE_SENSE:
4446 case MODE_SENSE_10:
4447 errsts = resp_mode_sense(SCpnt, target, devip);
4448 break;
4449 case MODE_SELECT:
4450 errsts = resp_mode_select(SCpnt, 1, devip);
4451 break;
4452 case MODE_SELECT_10:
4453 errsts = resp_mode_select(SCpnt, 0, devip);
4454 break;
4455 case LOG_SENSE:
4456 errsts = resp_log_sense(SCpnt, devip);
4457 break;
4458 case SYNCHRONIZE_CACHE:
4459 delay_override = 1;
4460 errsts = check_readiness(SCpnt, UAS_TUR, devip);
4461 break;
4462 case WRITE_BUFFER:
4463 errsts = check_readiness(SCpnt, UAS_ONLY, devip);
4464 break;
4465 case XDWRITEREAD_10:
4466 if (!scsi_bidi_cmnd(SCpnt)) {
4467 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
4468 INVALID_FIELD_IN_CDB, 0);
4469 errsts = check_condition_result;
4470 break;
4471 }
4472
4473 errsts = check_readiness(SCpnt, UAS_TUR, devip);
4474 if (errsts)
4475 break;
4476 if (scsi_debug_fake_rw)
4477 break;
4478 get_data_transfer_info(cmd, &lba, &num, &ei_lba);
4479 errsts = resp_read(SCpnt, lba, num, ei_lba);
4480 if (errsts)
4481 break;
4482 errsts = resp_write(SCpnt, lba, num, ei_lba);
4483 if (errsts)
4484 break;
4485 errsts = resp_xdwriteread(SCpnt, lba, num, devip);
4486 break;
4487 case VARIABLE_LENGTH_CMD:
4488 if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION) {
4489
4490 if ((cmd[10] & 0xe0) == 0)
4491 printk(KERN_ERR
4492 "Unprotected RD/WR to DIF device\n");
4493
4494 if (cmd[9] == READ_32) {
4495 BUG_ON(SCpnt->cmd_len < 32);
4496 goto read;
4497 }
4498
4499 if (cmd[9] == WRITE_32) {
4500 BUG_ON(SCpnt->cmd_len < 32);
4501 goto write;
4502 }
4503 }
4504
4505 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
4506 INVALID_FIELD_IN_CDB, 0);
4507 errsts = check_condition_result;
4508 break;
4509 case 0x85:
4510 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
4511 sdev_printk(KERN_INFO, SCpnt->device,
4512 "%s: ATA PASS-THROUGH(16) not supported\n", my_name);
4513 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
4514 INVALID_OPCODE, 0);
4515 errsts = check_condition_result;
4516 break;
4517 default:
4518 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
4519 sdev_printk(KERN_INFO, SCpnt->device,
4520 "%s: Opcode: 0x%x not supported\n",
4521 my_name, *cmd);
4522 errsts = check_readiness(SCpnt, UAS_ONLY, devip);
4523 if (errsts)
4524 break; /* Unit attention takes precedence */
4525 mk_sense_buffer(SCpnt, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
4526 errsts = check_condition_result;
4527 break;
4528 }
4529 return schedule_resp(SCpnt, devip, errsts,
4530 (delay_override ? 0 : scsi_debug_delay));
4531}
4532
4533static int
4534sdebug_queuecommand_lock_or_not(struct Scsi_Host *shost, struct scsi_cmnd *cmd) 4672sdebug_queuecommand_lock_or_not(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
4535{ 4673{
4536 if (scsi_debug_host_lock) { 4674 if (scsi_debug_host_lock) {
@@ -4637,6 +4775,143 @@ check_inject(struct scsi_cmnd *scp)
4637 return 0; 4775 return 0;
4638} 4776}
4639 4777
4778static int
4779scsi_debug_queuecommand(struct scsi_cmnd *scp)
4780{
4781 u8 sdeb_i;
4782 struct scsi_device *sdp = scp->device;
4783 const struct opcode_info_t *oip;
4784 const struct opcode_info_t *r_oip;
4785 struct sdebug_dev_info *devip;
4786 u8 *cmd = scp->cmnd;
4787 int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
4788 int k, na;
4789 int errsts = 0;
4790 int errsts_no_connect = DID_NO_CONNECT << 16;
4791 u32 flags;
4792 u16 sa;
4793 u8 opcode = cmd[0];
4794 bool has_wlun_rl;
4795 bool debug = !!(SCSI_DEBUG_OPT_NOISE & scsi_debug_opts);
4796
4797 scsi_set_resid(scp, 0);
4798 if (debug && !(SCSI_DEBUG_OPT_NO_CDB_NOISE & scsi_debug_opts)) {
4799 char b[120];
4800 int n, len, sb;
4801
4802 len = scp->cmd_len;
4803 sb = (int)sizeof(b);
4804 if (len > 32)
4805 strcpy(b, "too long, over 32 bytes");
4806 else {
4807 for (k = 0, n = 0; k < len && n < sb; ++k)
4808 n += scnprintf(b + n, sb - n, "%02x ",
4809 (u32)cmd[k]);
4810 }
4811 sdev_printk(KERN_INFO, sdp, "%s: cmd %s\n", my_name, b);
4812 }
4813 has_wlun_rl = (sdp->lun == SAM2_WLUN_REPORT_LUNS);
4814 if ((sdp->lun >= scsi_debug_max_luns) && !has_wlun_rl)
4815 return schedule_resp(scp, NULL, errsts_no_connect, 0);
4816
4817 sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */
4818 oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */
4819 devip = (struct sdebug_dev_info *)sdp->hostdata;
4820 if (!devip) {
4821 devip = devInfoReg(sdp);
4822 if (NULL == devip)
4823 return schedule_resp(scp, NULL, errsts_no_connect, 0);
4824 }
4825 na = oip->num_attached;
4826 r_pfp = oip->pfp;
4827 if (na) { /* multiple commands with this opcode */
4828 r_oip = oip;
4829 if (FF_SA & r_oip->flags) {
4830 if (F_SA_LOW & oip->flags)
4831 sa = 0x1f & cmd[1];
4832 else
4833 sa = get_unaligned_be16(cmd + 8);
4834 for (k = 0; k <= na; oip = r_oip->arrp + k++) {
4835 if (opcode == oip->opcode && sa == oip->sa)
4836 break;
4837 }
4838 } else { /* since no service action only check opcode */
4839 for (k = 0; k <= na; oip = r_oip->arrp + k++) {
4840 if (opcode == oip->opcode)
4841 break;
4842 }
4843 }
4844 if (k > na) {
4845 if (F_SA_LOW & r_oip->flags)
4846 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4);
4847 else if (F_SA_HIGH & r_oip->flags)
4848 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7);
4849 else
4850 mk_sense_invalid_opcode(scp);
4851 goto check_cond;
4852 }
4853 } /* else (when na==0) we assume the oip is a match */
4854 flags = oip->flags;
4855 if (F_INV_OP & flags) {
4856 mk_sense_invalid_opcode(scp);
4857 goto check_cond;
4858 }
4859 if (has_wlun_rl && !(F_RL_WLUN_OK & flags)) {
4860 if (debug)
4861 sdev_printk(KERN_INFO, sdp, "scsi_debug: Opcode: "
4862 "0x%x not supported for wlun\n", opcode);
4863 mk_sense_invalid_opcode(scp);
4864 goto check_cond;
4865 }
4866 if (scsi_debug_strict) { /* check cdb against mask */
4867 u8 rem;
4868 int j;
4869
4870 for (k = 1; k < oip->len_mask[0] && k < 16; ++k) {
4871 rem = ~oip->len_mask[k] & cmd[k];
4872 if (rem) {
4873 for (j = 7; j >= 0; --j, rem <<= 1) {
4874 if (0x80 & rem)
4875 break;
4876 }
4877 mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j);
4878 goto check_cond;
4879 }
4880 }
4881 }
4882 if (!(F_SKIP_UA & flags) &&
4883 SDEBUG_NUM_UAS != find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS)) {
4884 errsts = check_readiness(scp, UAS_ONLY, devip);
4885 if (errsts)
4886 goto check_cond;
4887 }
4888 if ((F_M_ACCESS & flags) && devip->stopped) {
4889 mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2);
4890 if (debug)
4891 sdev_printk(KERN_INFO, sdp, "%s reports: Not ready: "
4892 "%s\n", my_name, "initializing command "
4893 "required");
4894 errsts = check_condition_result;
4895 goto fini;
4896 }
4897 if (scsi_debug_fake_rw && (F_FAKE_RW & flags))
4898 goto fini;
4899 if (scsi_debug_every_nth) {
4900 if (check_inject(scp))
4901 return 0; /* ignore command: make trouble */
4902 }
4903 if (oip->pfp) /* if this command has a resp_* function, call it */
4904 errsts = oip->pfp(scp, devip);
4905 else if (r_pfp) /* if leaf function ptr NULL, try the root's */
4906 errsts = r_pfp(scp, devip);
4907
4908fini:
4909 return schedule_resp(scp, devip, errsts,
4910 ((F_DELAY_OVERR & flags) ? 0 : scsi_debug_delay));
4911check_cond:
4912 return schedule_resp(scp, devip, check_condition_result, 0);
4913}
4914
4640static struct scsi_host_template sdebug_driver_template = { 4915static struct scsi_host_template sdebug_driver_template = {
4641 .show_info = scsi_debug_show_info, 4916 .show_info = scsi_debug_show_info,
4642 .write_info = scsi_debug_write_info, 4917 .write_info = scsi_debug_write_info,