aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/scsi_debug.c
diff options
context:
space:
mode:
authorDouglas Gilbert <dougg@torque.net>2006-06-06 00:11:24 -0400
committerJames Bottomley <jejb@mulgrave.il.steeleye.com>2006-06-26 10:12:42 -0400
commitc65b1445d153a66ca91b00c1f10187e495c17918 (patch)
tree33957363da428cd53c4fc372c0f8253d7a5927f1 /drivers/scsi/scsi_debug.c
parent4311fa60b0d1cb5a2f62f646978294bca7b46cbb (diff)
[SCSI] scsi_debug version 1.79
- add 'virtual_gb' parameter to simulate large storage (by wrapping in dev_size_mb megabytes of actual ram) - add 'no_lun_0' parameter to skip lun 0 on each target (but still respond as required to INQUIRY + REPORT LUNS) - add well know lu support - add MODE SELECT commands support [pages: 0xa and 0x1c] - add LOG SENSE command support [pages: 0xd and 0x2f] - add READ CAPACITY (16) support - increase number of mode pages supported (to read), mainly transport specific (SAS) mode (sub)pages - add more VPD pages and extend others, including ATA information VPD page - START STOP UNIT now maintains a state machine - READ (16) and WRITE (16) cope with lbas larger than 32 bits (needed for the 'virtual_gb' parameter) - allow single command transfers up to 32 MB - more precise error (sense data) messages Signed-off-by: Douglas Gilbert <dougg@torque.net> Signed-off-by: James Bottomley <James.Bottomley@SteelEye.com>
Diffstat (limited to 'drivers/scsi/scsi_debug.c')
-rw-r--r--drivers/scsi/scsi_debug.c1223
1 files changed, 1028 insertions, 195 deletions
diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c
index 5a5d2af8ee43..1da8ccde6d14 100644
--- a/drivers/scsi/scsi_debug.c
+++ b/drivers/scsi/scsi_debug.c
@@ -51,18 +51,22 @@
51#include "scsi_logging.h" 51#include "scsi_logging.h"
52#include "scsi_debug.h" 52#include "scsi_debug.h"
53 53
54#define SCSI_DEBUG_VERSION "1.75" 54#define SCSI_DEBUG_VERSION "1.79"
55static const char * scsi_debug_version_date = "20050113"; 55static const char * scsi_debug_version_date = "20060604";
56 56
57/* Additional Sense Code (ASC) used */ 57/* Additional Sense Code (ASC) used */
58#define NO_ADDED_SENSE 0x0 58#define NO_ADDITIONAL_SENSE 0x0
59#define LOGICAL_UNIT_NOT_READY 0x4
59#define UNRECOVERED_READ_ERR 0x11 60#define UNRECOVERED_READ_ERR 0x11
61#define PARAMETER_LIST_LENGTH_ERR 0x1a
60#define INVALID_OPCODE 0x20 62#define INVALID_OPCODE 0x20
61#define ADDR_OUT_OF_RANGE 0x21 63#define ADDR_OUT_OF_RANGE 0x21
62#define INVALID_FIELD_IN_CDB 0x24 64#define INVALID_FIELD_IN_CDB 0x24
65#define INVALID_FIELD_IN_PARAM_LIST 0x26
63#define POWERON_RESET 0x29 66#define POWERON_RESET 0x29
64#define SAVING_PARAMS_UNSUP 0x39 67#define SAVING_PARAMS_UNSUP 0x39
65#define THRESHHOLD_EXCEEDED 0x5d 68#define THRESHOLD_EXCEEDED 0x5d
69#define LOW_POWER_COND_ON 0x5e
66 70
67#define SDEBUG_TAGGED_QUEUING 0 /* 0 | MSG_SIMPLE_TAG | MSG_ORDERED_TAG */ 71#define SDEBUG_TAGGED_QUEUING 0 /* 0 | MSG_SIMPLE_TAG | MSG_ORDERED_TAG */
68 72
@@ -81,6 +85,8 @@ static const char * scsi_debug_version_date = "20050113";
81#define DEF_SCSI_LEVEL 5 /* INQUIRY, byte2 [5->SPC-3] */ 85#define DEF_SCSI_LEVEL 5 /* INQUIRY, byte2 [5->SPC-3] */
82#define DEF_PTYPE 0 86#define DEF_PTYPE 0
83#define DEF_D_SENSE 0 87#define DEF_D_SENSE 0
88#define DEF_NO_LUN_0 0
89#define DEF_VIRTUAL_GB 0
84 90
85/* bit mask values for scsi_debug_opts */ 91/* bit mask values for scsi_debug_opts */
86#define SCSI_DEBUG_OPT_NOISE 1 92#define SCSI_DEBUG_OPT_NOISE 1
@@ -107,6 +113,7 @@ static const char * scsi_debug_version_date = "20050113";
107/* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1) 113/* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
108 * or "peripheral device" addressing (value 0) */ 114 * or "peripheral device" addressing (value 0) */
109#define SAM2_LUN_ADDRESS_METHOD 0 115#define SAM2_LUN_ADDRESS_METHOD 0
116#define SAM2_WLUN_REPORT_LUNS 0xc101
110 117
111static int scsi_debug_add_host = DEF_NUM_HOST; 118static int scsi_debug_add_host = DEF_NUM_HOST;
112static int scsi_debug_delay = DEF_DELAY; 119static int scsi_debug_delay = DEF_DELAY;
@@ -119,13 +126,16 @@ static int scsi_debug_opts = DEF_OPTS;
119static int scsi_debug_scsi_level = DEF_SCSI_LEVEL; 126static int scsi_debug_scsi_level = DEF_SCSI_LEVEL;
120static int scsi_debug_ptype = DEF_PTYPE; /* SCSI peripheral type (0==disk) */ 127static int scsi_debug_ptype = DEF_PTYPE; /* SCSI peripheral type (0==disk) */
121static int scsi_debug_dsense = DEF_D_SENSE; 128static int scsi_debug_dsense = DEF_D_SENSE;
129static int scsi_debug_no_lun_0 = DEF_NO_LUN_0;
130static int scsi_debug_virtual_gb = DEF_VIRTUAL_GB;
122 131
123static int scsi_debug_cmnd_count = 0; 132static int scsi_debug_cmnd_count = 0;
124 133
125#define DEV_READONLY(TGT) (0) 134#define DEV_READONLY(TGT) (0)
126#define DEV_REMOVEABLE(TGT) (0) 135#define DEV_REMOVEABLE(TGT) (0)
127 136
128static unsigned long sdebug_store_size; /* in bytes */ 137static unsigned int sdebug_store_size; /* in bytes */
138static unsigned int sdebug_store_sectors;
129static sector_t sdebug_capacity; /* in sectors */ 139static sector_t sdebug_capacity; /* in sectors */
130 140
131/* old BIOS stuff, kernel may get rid of them but some mode sense pages 141/* old BIOS stuff, kernel may get rid of them but some mode sense pages
@@ -150,7 +160,9 @@ struct sdebug_dev_info {
150 unsigned int target; 160 unsigned int target;
151 unsigned int lun; 161 unsigned int lun;
152 struct sdebug_host_info *sdbg_host; 162 struct sdebug_host_info *sdbg_host;
163 unsigned int wlun;
153 char reset; 164 char reset;
165 char stopped;
154 char used; 166 char used;
155}; 167};
156 168
@@ -194,11 +206,11 @@ static struct scsi_host_template sdebug_driver_template = {
194 .bios_param = scsi_debug_biosparam, 206 .bios_param = scsi_debug_biosparam,
195 .can_queue = SCSI_DEBUG_CANQUEUE, 207 .can_queue = SCSI_DEBUG_CANQUEUE,
196 .this_id = 7, 208 .this_id = 7,
197 .sg_tablesize = 64, 209 .sg_tablesize = 256,
198 .cmd_per_lun = 3, 210 .cmd_per_lun = 16,
199 .max_sectors = 4096, 211 .max_sectors = 0xffff,
200 .unchecked_isa_dma = 0, 212 .unchecked_isa_dma = 0,
201 .use_clustering = DISABLE_CLUSTERING, 213 .use_clustering = ENABLE_CLUSTERING,
202 .module = THIS_MODULE, 214 .module = THIS_MODULE,
203}; 215};
204 216
@@ -226,19 +238,32 @@ static struct device_driver sdebug_driverfs_driver = {
226static const int check_condition_result = 238static const int check_condition_result =
227 (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; 239 (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
228 240
241static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
242 0, 0, 0x2, 0x4b};
243static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
244 0, 0, 0x0, 0x0};
245
229/* function declarations */ 246/* function declarations */
230static int resp_inquiry(struct scsi_cmnd * SCpnt, int target, 247static int resp_inquiry(struct scsi_cmnd * SCpnt, int target,
231 struct sdebug_dev_info * devip); 248 struct sdebug_dev_info * devip);
232static int resp_requests(struct scsi_cmnd * SCpnt, 249static int resp_requests(struct scsi_cmnd * SCpnt,
233 struct sdebug_dev_info * devip); 250 struct sdebug_dev_info * devip);
251static int resp_start_stop(struct scsi_cmnd * scp,
252 struct sdebug_dev_info * devip);
234static int resp_readcap(struct scsi_cmnd * SCpnt, 253static int resp_readcap(struct scsi_cmnd * SCpnt,
235 struct sdebug_dev_info * devip); 254 struct sdebug_dev_info * devip);
236static int resp_mode_sense(struct scsi_cmnd * SCpnt, int target, 255static int resp_readcap16(struct scsi_cmnd * SCpnt,
256 struct sdebug_dev_info * devip);
257static int resp_mode_sense(struct scsi_cmnd * scp, int target,
237 struct sdebug_dev_info * devip); 258 struct sdebug_dev_info * devip);
238static int resp_read(struct scsi_cmnd * SCpnt, int upper_blk, int block, 259static int resp_mode_select(struct scsi_cmnd * scp, int mselect6,
239 int num, struct sdebug_dev_info * devip); 260 struct sdebug_dev_info * devip);
240static int resp_write(struct scsi_cmnd * SCpnt, int upper_blk, int block, 261static int resp_log_sense(struct scsi_cmnd * scp,
241 int num, struct sdebug_dev_info * devip); 262 struct sdebug_dev_info * devip);
263static int resp_read(struct scsi_cmnd * SCpnt, unsigned long long lba,
264 unsigned int num, struct sdebug_dev_info * devip);
265static int resp_write(struct scsi_cmnd * SCpnt, unsigned long long lba,
266 unsigned int num, struct sdebug_dev_info * devip);
242static int resp_report_luns(struct scsi_cmnd * SCpnt, 267static int resp_report_luns(struct scsi_cmnd * SCpnt,
243 struct sdebug_dev_info * devip); 268 struct sdebug_dev_info * devip);
244static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr, 269static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
@@ -249,8 +274,8 @@ static void timer_intr_handler(unsigned long);
249static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev); 274static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev);
250static void mk_sense_buffer(struct sdebug_dev_info * devip, int key, 275static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
251 int asc, int asq); 276 int asc, int asq);
252static int check_reset(struct scsi_cmnd * SCpnt, 277static int check_readiness(struct scsi_cmnd * SCpnt, int reset_only,
253 struct sdebug_dev_info * devip); 278 struct sdebug_dev_info * devip);
254static int schedule_resp(struct scsi_cmnd * cmnd, 279static int schedule_resp(struct scsi_cmnd * cmnd,
255 struct sdebug_dev_info * devip, 280 struct sdebug_dev_info * devip,
256 done_funct_t done, int scsi_result, int delta_jiff); 281 done_funct_t done, int scsi_result, int delta_jiff);
@@ -258,8 +283,10 @@ static void __init sdebug_build_parts(unsigned char * ramp);
258static void __init init_all_queued(void); 283static void __init init_all_queued(void);
259static void stop_all_queued(void); 284static void stop_all_queued(void);
260static int stop_queued_cmnd(struct scsi_cmnd * cmnd); 285static int stop_queued_cmnd(struct scsi_cmnd * cmnd);
261static int inquiry_evpd_83(unsigned char * arr, int dev_id_num, 286static int inquiry_evpd_83(unsigned char * arr, int target_dev_id,
262 const char * dev_id_str, int dev_id_str_len); 287 int dev_id_num, const char * dev_id_str,
288 int dev_id_str_len);
289static int inquiry_evpd_88(unsigned char * arr, int target_dev_id);
263static void do_create_driverfs_files(void); 290static void do_create_driverfs_files(void);
264static void do_remove_driverfs_files(void); 291static void do_remove_driverfs_files(void);
265 292
@@ -275,18 +302,22 @@ static
275int scsi_debug_queuecommand(struct scsi_cmnd * SCpnt, done_funct_t done) 302int scsi_debug_queuecommand(struct scsi_cmnd * SCpnt, done_funct_t done)
276{ 303{
277 unsigned char *cmd = (unsigned char *) SCpnt->cmnd; 304 unsigned char *cmd = (unsigned char *) SCpnt->cmnd;
278 int block, upper_blk, num, k; 305 int len, k, j;
306 unsigned int num;
307 unsigned long long lba;
279 int errsts = 0; 308 int errsts = 0;
280 int target = scmd_id(SCpnt); 309 int target = SCpnt->device->id;
281 struct sdebug_dev_info * devip = NULL; 310 struct sdebug_dev_info * devip = NULL;
282 int inj_recovered = 0; 311 int inj_recovered = 0;
312 int delay_override = 0;
283 313
284 if (done == NULL) 314 if (done == NULL)
285 return 0; /* assume mid level reprocessing command */ 315 return 0; /* assume mid level reprocessing command */
286 316
317 SCpnt->resid = 0;
287 if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmd) { 318 if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmd) {
288 printk(KERN_INFO "scsi_debug: cmd "); 319 printk(KERN_INFO "scsi_debug: cmd ");
289 for (k = 0, num = SCpnt->cmd_len; k < num; ++k) 320 for (k = 0, len = SCpnt->cmd_len; k < len; ++k)
290 printk("%02x ", (int)cmd[k]); 321 printk("%02x ", (int)cmd[k]);
291 printk("\n"); 322 printk("\n");
292 } 323 }
@@ -297,7 +328,8 @@ int scsi_debug_queuecommand(struct scsi_cmnd * SCpnt, done_funct_t done)
297 DID_NO_CONNECT << 16, 0); 328 DID_NO_CONNECT << 16, 0);
298 } 329 }
299 330
300 if (SCpnt->device->lun >= scsi_debug_max_luns) 331 if ((SCpnt->device->lun >= scsi_debug_max_luns) &&
332 (SCpnt->device->lun != SAM2_WLUN_REPORT_LUNS))
301 return schedule_resp(SCpnt, NULL, done, 333 return schedule_resp(SCpnt, NULL, done,
302 DID_NO_CONNECT << 16, 0); 334 DID_NO_CONNECT << 16, 0);
303 devip = devInfoReg(SCpnt->device); 335 devip = devInfoReg(SCpnt->device);
@@ -316,118 +348,150 @@ int scsi_debug_queuecommand(struct scsi_cmnd * SCpnt, done_funct_t done)
316 inj_recovered = 1; /* to reads and writes below */ 348 inj_recovered = 1; /* to reads and writes below */
317 } 349 }
318 350
351 if (devip->wlun) {
352 switch (*cmd) {
353 case INQUIRY:
354 case REQUEST_SENSE:
355 case TEST_UNIT_READY:
356 case REPORT_LUNS:
357 break; /* only allowable wlun commands */
358 default:
359 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
360 printk(KERN_INFO "scsi_debug: Opcode: 0x%x "
361 "not supported for wlun\n", *cmd);
362 mk_sense_buffer(devip, ILLEGAL_REQUEST,
363 INVALID_OPCODE, 0);
364 errsts = check_condition_result;
365 return schedule_resp(SCpnt, devip, done, errsts,
366 0);
367 }
368 }
369
319 switch (*cmd) { 370 switch (*cmd) {
320 case INQUIRY: /* mandatory, ignore unit attention */ 371 case INQUIRY: /* mandatory, ignore unit attention */
372 delay_override = 1;
321 errsts = resp_inquiry(SCpnt, target, devip); 373 errsts = resp_inquiry(SCpnt, target, devip);
322 break; 374 break;
323 case REQUEST_SENSE: /* mandatory, ignore unit attention */ 375 case REQUEST_SENSE: /* mandatory, ignore unit attention */
376 delay_override = 1;
324 errsts = resp_requests(SCpnt, devip); 377 errsts = resp_requests(SCpnt, devip);
325 break; 378 break;
326 case REZERO_UNIT: /* actually this is REWIND for SSC */ 379 case REZERO_UNIT: /* actually this is REWIND for SSC */
327 case START_STOP: 380 case START_STOP:
328 errsts = check_reset(SCpnt, devip); 381 errsts = resp_start_stop(SCpnt, devip);
329 break; 382 break;
330 case ALLOW_MEDIUM_REMOVAL: 383 case ALLOW_MEDIUM_REMOVAL:
331 if ((errsts = check_reset(SCpnt, devip))) 384 if ((errsts = check_readiness(SCpnt, 1, devip)))
332 break; 385 break;
333 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) 386 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
334 printk(KERN_INFO "scsi_debug: Medium removal %s\n", 387 printk(KERN_INFO "scsi_debug: Medium removal %s\n",
335 cmd[4] ? "inhibited" : "enabled"); 388 cmd[4] ? "inhibited" : "enabled");
336 break; 389 break;
337 case SEND_DIAGNOSTIC: /* mandatory */ 390 case SEND_DIAGNOSTIC: /* mandatory */
338 errsts = check_reset(SCpnt, devip); 391 errsts = check_readiness(SCpnt, 1, devip);
339 break; 392 break;
340 case TEST_UNIT_READY: /* mandatory */ 393 case TEST_UNIT_READY: /* mandatory */
341 errsts = check_reset(SCpnt, devip); 394 delay_override = 1;
395 errsts = check_readiness(SCpnt, 0, devip);
342 break; 396 break;
343 case RESERVE: 397 case RESERVE:
344 errsts = check_reset(SCpnt, devip); 398 errsts = check_readiness(SCpnt, 1, devip);
345 break; 399 break;
346 case RESERVE_10: 400 case RESERVE_10:
347 errsts = check_reset(SCpnt, devip); 401 errsts = check_readiness(SCpnt, 1, devip);
348 break; 402 break;
349 case RELEASE: 403 case RELEASE:
350 errsts = check_reset(SCpnt, devip); 404 errsts = check_readiness(SCpnt, 1, devip);
351 break; 405 break;
352 case RELEASE_10: 406 case RELEASE_10:
353 errsts = check_reset(SCpnt, devip); 407 errsts = check_readiness(SCpnt, 1, devip);
354 break; 408 break;
355 case READ_CAPACITY: 409 case READ_CAPACITY:
356 errsts = resp_readcap(SCpnt, devip); 410 errsts = resp_readcap(SCpnt, devip);
357 break; 411 break;
412 case SERVICE_ACTION_IN:
413 if (SAI_READ_CAPACITY_16 != cmd[1]) {
414 mk_sense_buffer(devip, ILLEGAL_REQUEST,
415 INVALID_OPCODE, 0);
416 errsts = check_condition_result;
417 break;
418 }
419 errsts = resp_readcap16(SCpnt, devip);
420 break;
358 case READ_16: 421 case READ_16:
359 case READ_12: 422 case READ_12:
360 case READ_10: 423 case READ_10:
361 case READ_6: 424 case READ_6:
362 if ((errsts = check_reset(SCpnt, devip))) 425 if ((errsts = check_readiness(SCpnt, 0, devip)))
363 break; 426 break;
364 upper_blk = 0;
365 if ((*cmd) == READ_16) { 427 if ((*cmd) == READ_16) {
366 upper_blk = cmd[5] + (cmd[4] << 8) + 428 for (lba = 0, j = 0; j < 8; ++j) {
367 (cmd[3] << 16) + (cmd[2] << 24); 429 if (j > 0)
368 block = cmd[9] + (cmd[8] << 8) + 430 lba <<= 8;
369 (cmd[7] << 16) + (cmd[6] << 24); 431 lba += cmd[2 + j];
432 }
370 num = cmd[13] + (cmd[12] << 8) + 433 num = cmd[13] + (cmd[12] << 8) +
371 (cmd[11] << 16) + (cmd[10] << 24); 434 (cmd[11] << 16) + (cmd[10] << 24);
372 } else if ((*cmd) == READ_12) { 435 } else if ((*cmd) == READ_12) {
373 block = cmd[5] + (cmd[4] << 8) + 436 lba = cmd[5] + (cmd[4] << 8) +
374 (cmd[3] << 16) + (cmd[2] << 24); 437 (cmd[3] << 16) + (cmd[2] << 24);
375 num = cmd[9] + (cmd[8] << 8) + 438 num = cmd[9] + (cmd[8] << 8) +
376 (cmd[7] << 16) + (cmd[6] << 24); 439 (cmd[7] << 16) + (cmd[6] << 24);
377 } else if ((*cmd) == READ_10) { 440 } else if ((*cmd) == READ_10) {
378 block = cmd[5] + (cmd[4] << 8) + 441 lba = cmd[5] + (cmd[4] << 8) +
379 (cmd[3] << 16) + (cmd[2] << 24); 442 (cmd[3] << 16) + (cmd[2] << 24);
380 num = cmd[8] + (cmd[7] << 8); 443 num = cmd[8] + (cmd[7] << 8);
381 } else { 444 } else { /* READ (6) */
382 block = cmd[3] + (cmd[2] << 8) + 445 lba = cmd[3] + (cmd[2] << 8) +
383 ((cmd[1] & 0x1f) << 16); 446 ((cmd[1] & 0x1f) << 16);
384 num = cmd[4]; 447 num = (0 == cmd[4]) ? 256 : cmd[4];
385 } 448 }
386 errsts = resp_read(SCpnt, upper_blk, block, num, devip); 449 errsts = resp_read(SCpnt, lba, num, devip);
387 if (inj_recovered && (0 == errsts)) { 450 if (inj_recovered && (0 == errsts)) {
388 mk_sense_buffer(devip, RECOVERED_ERROR, 451 mk_sense_buffer(devip, RECOVERED_ERROR,
389 THRESHHOLD_EXCEEDED, 0); 452 THRESHOLD_EXCEEDED, 0);
390 errsts = check_condition_result; 453 errsts = check_condition_result;
391 } 454 }
392 break; 455 break;
393 case REPORT_LUNS: /* mandatory, ignore unit attention */ 456 case REPORT_LUNS: /* mandatory, ignore unit attention */
457 delay_override = 1;
394 errsts = resp_report_luns(SCpnt, devip); 458 errsts = resp_report_luns(SCpnt, devip);
395 break; 459 break;
396 case VERIFY: /* 10 byte SBC-2 command */ 460 case VERIFY: /* 10 byte SBC-2 command */
397 errsts = check_reset(SCpnt, devip); 461 errsts = check_readiness(SCpnt, 0, devip);
398 break; 462 break;
399 case WRITE_16: 463 case WRITE_16:
400 case WRITE_12: 464 case WRITE_12:
401 case WRITE_10: 465 case WRITE_10:
402 case WRITE_6: 466 case WRITE_6:
403 if ((errsts = check_reset(SCpnt, devip))) 467 if ((errsts = check_readiness(SCpnt, 0, devip)))
404 break; 468 break;
405 upper_blk = 0;
406 if ((*cmd) == WRITE_16) { 469 if ((*cmd) == WRITE_16) {
407 upper_blk = cmd[5] + (cmd[4] << 8) + 470 for (lba = 0, j = 0; j < 8; ++j) {
408 (cmd[3] << 16) + (cmd[2] << 24); 471 if (j > 0)
409 block = cmd[9] + (cmd[8] << 8) + 472 lba <<= 8;
410 (cmd[7] << 16) + (cmd[6] << 24); 473 lba += cmd[2 + j];
474 }
411 num = cmd[13] + (cmd[12] << 8) + 475 num = cmd[13] + (cmd[12] << 8) +
412 (cmd[11] << 16) + (cmd[10] << 24); 476 (cmd[11] << 16) + (cmd[10] << 24);
413 } else if ((*cmd) == WRITE_12) { 477 } else if ((*cmd) == WRITE_12) {
414 block = cmd[5] + (cmd[4] << 8) + 478 lba = cmd[5] + (cmd[4] << 8) +
415 (cmd[3] << 16) + (cmd[2] << 24); 479 (cmd[3] << 16) + (cmd[2] << 24);
416 num = cmd[9] + (cmd[8] << 8) + 480 num = cmd[9] + (cmd[8] << 8) +
417 (cmd[7] << 16) + (cmd[6] << 24); 481 (cmd[7] << 16) + (cmd[6] << 24);
418 } else if ((*cmd) == WRITE_10) { 482 } else if ((*cmd) == WRITE_10) {
419 block = cmd[5] + (cmd[4] << 8) + 483 lba = cmd[5] + (cmd[4] << 8) +
420 (cmd[3] << 16) + (cmd[2] << 24); 484 (cmd[3] << 16) + (cmd[2] << 24);
421 num = cmd[8] + (cmd[7] << 8); 485 num = cmd[8] + (cmd[7] << 8);
422 } else { 486 } else { /* WRITE (6) */
423 block = cmd[3] + (cmd[2] << 8) + 487 lba = cmd[3] + (cmd[2] << 8) +
424 ((cmd[1] & 0x1f) << 16); 488 ((cmd[1] & 0x1f) << 16);
425 num = cmd[4]; 489 num = (0 == cmd[4]) ? 256 : cmd[4];
426 } 490 }
427 errsts = resp_write(SCpnt, upper_blk, block, num, devip); 491 errsts = resp_write(SCpnt, lba, num, devip);
428 if (inj_recovered && (0 == errsts)) { 492 if (inj_recovered && (0 == errsts)) {
429 mk_sense_buffer(devip, RECOVERED_ERROR, 493 mk_sense_buffer(devip, RECOVERED_ERROR,
430 THRESHHOLD_EXCEEDED, 0); 494 THRESHOLD_EXCEEDED, 0);
431 errsts = check_condition_result; 495 errsts = check_condition_result;
432 } 496 }
433 break; 497 break;
@@ -435,20 +499,31 @@ int scsi_debug_queuecommand(struct scsi_cmnd * SCpnt, done_funct_t done)
435 case MODE_SENSE_10: 499 case MODE_SENSE_10:
436 errsts = resp_mode_sense(SCpnt, target, devip); 500 errsts = resp_mode_sense(SCpnt, target, devip);
437 break; 501 break;
502 case MODE_SELECT:
503 errsts = resp_mode_select(SCpnt, 1, devip);
504 break;
505 case MODE_SELECT_10:
506 errsts = resp_mode_select(SCpnt, 0, devip);
507 break;
508 case LOG_SENSE:
509 errsts = resp_log_sense(SCpnt, devip);
510 break;
438 case SYNCHRONIZE_CACHE: 511 case SYNCHRONIZE_CACHE:
439 errsts = check_reset(SCpnt, devip); 512 delay_override = 1;
513 errsts = check_readiness(SCpnt, 0, devip);
440 break; 514 break;
441 default: 515 default:
442 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) 516 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
443 printk(KERN_INFO "scsi_debug: Opcode: 0x%x not " 517 printk(KERN_INFO "scsi_debug: Opcode: 0x%x not "
444 "supported\n", *cmd); 518 "supported\n", *cmd);
445 if ((errsts = check_reset(SCpnt, devip))) 519 if ((errsts = check_readiness(SCpnt, 1, devip)))
446 break; /* Unit attention takes precedence */ 520 break; /* Unit attention takes precedence */
447 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_OPCODE, 0); 521 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
448 errsts = check_condition_result; 522 errsts = check_condition_result;
449 break; 523 break;
450 } 524 }
451 return schedule_resp(SCpnt, devip, done, errsts, scsi_debug_delay); 525 return schedule_resp(SCpnt, devip, done, errsts,
526 (delay_override ? 0 : scsi_debug_delay));
452} 527}
453 528
454static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg) 529static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
@@ -460,7 +535,8 @@ static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
460 /* return -ENOTTY; // correct return but upsets fdisk */ 535 /* return -ENOTTY; // correct return but upsets fdisk */
461} 536}
462 537
463static int check_reset(struct scsi_cmnd * SCpnt, struct sdebug_dev_info * devip) 538static int check_readiness(struct scsi_cmnd * SCpnt, int reset_only,
539 struct sdebug_dev_info * devip)
464{ 540{
465 if (devip->reset) { 541 if (devip->reset) {
466 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) 542 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
@@ -470,6 +546,14 @@ static int check_reset(struct scsi_cmnd * SCpnt, struct sdebug_dev_info * devip)
470 mk_sense_buffer(devip, UNIT_ATTENTION, POWERON_RESET, 0); 546 mk_sense_buffer(devip, UNIT_ATTENTION, POWERON_RESET, 0);
471 return check_condition_result; 547 return check_condition_result;
472 } 548 }
549 if ((0 == reset_only) && devip->stopped) {
550 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
551 printk(KERN_INFO "scsi_debug: Reporting Not "
552 "ready: initializing command required\n");
553 mk_sense_buffer(devip, NOT_READY, LOGICAL_UNIT_NOT_READY,
554 0x2);
555 return check_condition_result;
556 }
473 return 0; 557 return 0;
474} 558}
475 559
@@ -493,7 +577,10 @@ static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
493 req_len = scp->request_bufflen; 577 req_len = scp->request_bufflen;
494 act_len = (req_len < arr_len) ? req_len : arr_len; 578 act_len = (req_len < arr_len) ? req_len : arr_len;
495 memcpy(scp->request_buffer, arr, act_len); 579 memcpy(scp->request_buffer, arr, act_len);
496 scp->resid = req_len - act_len; 580 if (scp->resid)
581 scp->resid -= act_len;
582 else
583 scp->resid = req_len - act_len;
497 return 0; 584 return 0;
498 } 585 }
499 sgpnt = (struct scatterlist *)scp->request_buffer; 586 sgpnt = (struct scatterlist *)scp->request_buffer;
@@ -516,7 +603,10 @@ static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
516 } 603 }
517 req_len += sgpnt->length; 604 req_len += sgpnt->length;
518 } 605 }
519 scp->resid = req_len - act_len; 606 if (scp->resid)
607 scp->resid -= act_len;
608 else
609 scp->resid = req_len - act_len;
520 return 0; 610 return 0;
521} 611}
522 612
@@ -567,12 +657,14 @@ static const char * inq_vendor_id = "Linux ";
567static const char * inq_product_id = "scsi_debug "; 657static const char * inq_product_id = "scsi_debug ";
568static const char * inq_product_rev = "0004"; 658static const char * inq_product_rev = "0004";
569 659
570static int inquiry_evpd_83(unsigned char * arr, int dev_id_num, 660static int inquiry_evpd_83(unsigned char * arr, int target_dev_id,
571 const char * dev_id_str, int dev_id_str_len) 661 int dev_id_num, const char * dev_id_str,
662 int dev_id_str_len)
572{ 663{
573 int num; 664 int num, port_a;
665 char b[32];
574 666
575 /* Two identification descriptors: */ 667 port_a = target_dev_id + 1;
576 /* T10 vendor identifier field format (faked) */ 668 /* T10 vendor identifier field format (faked) */
577 arr[0] = 0x2; /* ASCII */ 669 arr[0] = 0x2; /* ASCII */
578 arr[1] = 0x1; 670 arr[1] = 0x1;
@@ -583,25 +675,246 @@ static int inquiry_evpd_83(unsigned char * arr, int dev_id_num,
583 num = 8 + 16 + dev_id_str_len; 675 num = 8 + 16 + dev_id_str_len;
584 arr[3] = num; 676 arr[3] = num;
585 num += 4; 677 num += 4;
586 /* NAA IEEE registered identifier (faked) */ 678 if (dev_id_num >= 0) {
587 arr[num] = 0x1; /* binary */ 679 /* NAA-5, Logical unit identifier (binary) */
588 arr[num + 1] = 0x3; 680 arr[num++] = 0x1; /* binary (not necessarily sas) */
589 arr[num + 2] = 0x0; 681 arr[num++] = 0x3; /* PIV=0, lu, naa */
590 arr[num + 3] = 0x8; 682 arr[num++] = 0x0;
591 arr[num + 4] = 0x51; /* ieee company id=0x123456 (faked) */ 683 arr[num++] = 0x8;
592 arr[num + 5] = 0x23; 684 arr[num++] = 0x53; /* naa-5 ieee company id=0x333333 (fake) */
593 arr[num + 6] = 0x45; 685 arr[num++] = 0x33;
594 arr[num + 7] = 0x60; 686 arr[num++] = 0x33;
595 arr[num + 8] = (dev_id_num >> 24); 687 arr[num++] = 0x30;
596 arr[num + 9] = (dev_id_num >> 16) & 0xff; 688 arr[num++] = (dev_id_num >> 24);
597 arr[num + 10] = (dev_id_num >> 8) & 0xff; 689 arr[num++] = (dev_id_num >> 16) & 0xff;
598 arr[num + 11] = dev_id_num & 0xff; 690 arr[num++] = (dev_id_num >> 8) & 0xff;
599 return num + 12; 691 arr[num++] = dev_id_num & 0xff;
692 /* Target relative port number */
693 arr[num++] = 0x61; /* proto=sas, binary */
694 arr[num++] = 0x94; /* PIV=1, target port, rel port */
695 arr[num++] = 0x0; /* reserved */
696 arr[num++] = 0x4; /* length */
697 arr[num++] = 0x0; /* reserved */
698 arr[num++] = 0x0; /* reserved */
699 arr[num++] = 0x0;
700 arr[num++] = 0x1; /* relative port A */
701 }
702 /* NAA-5, Target port identifier */
703 arr[num++] = 0x61; /* proto=sas, binary */
704 arr[num++] = 0x93; /* piv=1, target port, naa */
705 arr[num++] = 0x0;
706 arr[num++] = 0x8;
707 arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
708 arr[num++] = 0x22;
709 arr[num++] = 0x22;
710 arr[num++] = 0x20;
711 arr[num++] = (port_a >> 24);
712 arr[num++] = (port_a >> 16) & 0xff;
713 arr[num++] = (port_a >> 8) & 0xff;
714 arr[num++] = port_a & 0xff;
715 /* NAA-5, Target device identifier */
716 arr[num++] = 0x61; /* proto=sas, binary */
717 arr[num++] = 0xa3; /* piv=1, target device, naa */
718 arr[num++] = 0x0;
719 arr[num++] = 0x8;
720 arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
721 arr[num++] = 0x22;
722 arr[num++] = 0x22;
723 arr[num++] = 0x20;
724 arr[num++] = (target_dev_id >> 24);
725 arr[num++] = (target_dev_id >> 16) & 0xff;
726 arr[num++] = (target_dev_id >> 8) & 0xff;
727 arr[num++] = target_dev_id & 0xff;
728 /* SCSI name string: Target device identifier */
729 arr[num++] = 0x63; /* proto=sas, UTF-8 */
730 arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */
731 arr[num++] = 0x0;
732 arr[num++] = 24;
733 memcpy(arr + num, "naa.52222220", 12);
734 num += 12;
735 snprintf(b, sizeof(b), "%08X", target_dev_id);
736 memcpy(arr + num, b, 8);
737 num += 8;
738 memset(arr + num, 0, 4);
739 num += 4;
740 return num;
741}
742
743
744static unsigned char vpd84_data[] = {
745/* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
746 0x22,0x22,0x22,0x0,0xbb,0x1,
747 0x22,0x22,0x22,0x0,0xbb,0x2,
748};
749
750static int inquiry_evpd_84(unsigned char * arr)
751{
752 memcpy(arr, vpd84_data, sizeof(vpd84_data));
753 return sizeof(vpd84_data);
754}
755
756static int inquiry_evpd_85(unsigned char * arr)
757{
758 int num = 0;
759 const char * na1 = "https://www.kernel.org/config";
760 const char * na2 = "http://www.kernel.org/log";
761 int plen, olen;
762
763 arr[num++] = 0x1; /* lu, storage config */
764 arr[num++] = 0x0; /* reserved */
765 arr[num++] = 0x0;
766 olen = strlen(na1);
767 plen = olen + 1;
768 if (plen % 4)
769 plen = ((plen / 4) + 1) * 4;
770 arr[num++] = plen; /* length, null termianted, padded */
771 memcpy(arr + num, na1, olen);
772 memset(arr + num + olen, 0, plen - olen);
773 num += plen;
774
775 arr[num++] = 0x4; /* lu, logging */
776 arr[num++] = 0x0; /* reserved */
777 arr[num++] = 0x0;
778 olen = strlen(na2);
779 plen = olen + 1;
780 if (plen % 4)
781 plen = ((plen / 4) + 1) * 4;
782 arr[num++] = plen; /* length, null terminated, padded */
783 memcpy(arr + num, na2, olen);
784 memset(arr + num + olen, 0, plen - olen);
785 num += plen;
786
787 return num;
788}
789
790/* SCSI ports VPD page */
791static int inquiry_evpd_88(unsigned char * arr, int target_dev_id)
792{
793 int num = 0;
794 int port_a, port_b;
795
796 port_a = target_dev_id + 1;
797 port_b = port_a + 1;
798 arr[num++] = 0x0; /* reserved */
799 arr[num++] = 0x0; /* reserved */
800 arr[num++] = 0x0;
801 arr[num++] = 0x1; /* relative port 1 (primary) */
802 memset(arr + num, 0, 6);
803 num += 6;
804 arr[num++] = 0x0;
805 arr[num++] = 12; /* length tp descriptor */
806 /* naa-5 target port identifier (A) */
807 arr[num++] = 0x61; /* proto=sas, binary */
808 arr[num++] = 0x93; /* PIV=1, target port, NAA */
809 arr[num++] = 0x0; /* reserved */
810 arr[num++] = 0x8; /* length */
811 arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
812 arr[num++] = 0x22;
813 arr[num++] = 0x22;
814 arr[num++] = 0x20;
815 arr[num++] = (port_a >> 24);
816 arr[num++] = (port_a >> 16) & 0xff;
817 arr[num++] = (port_a >> 8) & 0xff;
818 arr[num++] = port_a & 0xff;
819
820 arr[num++] = 0x0; /* reserved */
821 arr[num++] = 0x0; /* reserved */
822 arr[num++] = 0x0;
823 arr[num++] = 0x2; /* relative port 2 (secondary) */
824 memset(arr + num, 0, 6);
825 num += 6;
826 arr[num++] = 0x0;
827 arr[num++] = 12; /* length tp descriptor */
828 /* naa-5 target port identifier (B) */
829 arr[num++] = 0x61; /* proto=sas, binary */
830 arr[num++] = 0x93; /* PIV=1, target port, NAA */
831 arr[num++] = 0x0; /* reserved */
832 arr[num++] = 0x8; /* length */
833 arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
834 arr[num++] = 0x22;
835 arr[num++] = 0x22;
836 arr[num++] = 0x20;
837 arr[num++] = (port_b >> 24);
838 arr[num++] = (port_b >> 16) & 0xff;
839 arr[num++] = (port_b >> 8) & 0xff;
840 arr[num++] = port_b & 0xff;
841
842 return num;
843}
844
845
846static unsigned char vpd89_data[] = {
847/* from 4th byte */ 0,0,0,0,
848'l','i','n','u','x',' ',' ',' ',
849'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
850'1','2','3','4',
8510x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
8520xec,0,0,0,
8530x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
8540,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
8550x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
8560x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
8570x53,0x41,
8580x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
8590x20,0x20,
8600x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
8610x10,0x80,
8620,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
8630x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
8640x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
8650,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
8660x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
8670x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
8680,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0,
8690,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8700,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8710,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8720x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
8730,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
8740xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
8750,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0,
8760,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8770,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8780,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8790,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8800,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8810,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8820,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8830,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8840,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8850,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8860,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8870,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
888};
889
890static int inquiry_evpd_89(unsigned char * arr)
891{
892 memcpy(arr, vpd89_data, sizeof(vpd89_data));
893 return sizeof(vpd89_data);
894}
895
896
897static unsigned char vpdb0_data[] = {
898 /* from 4th byte */ 0,0,0,4,
899 0,0,0x4,0,
900 0,0,0,64,
901};
902
903static int inquiry_evpd_b0(unsigned char * arr)
904{
905 memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
906 if (sdebug_store_sectors > 0x400) {
907 arr[4] = (sdebug_store_sectors >> 24) & 0xff;
908 arr[5] = (sdebug_store_sectors >> 16) & 0xff;
909 arr[6] = (sdebug_store_sectors >> 8) & 0xff;
910 arr[7] = sdebug_store_sectors & 0xff;
911 }
912 return sizeof(vpdb0_data);
600} 913}
601 914
602 915
603#define SDEBUG_LONG_INQ_SZ 96 916#define SDEBUG_LONG_INQ_SZ 96
604#define SDEBUG_MAX_INQ_ARR_SZ 128 917#define SDEBUG_MAX_INQ_ARR_SZ 584
605 918
606static int resp_inquiry(struct scsi_cmnd * scp, int target, 919static int resp_inquiry(struct scsi_cmnd * scp, int target,
607 struct sdebug_dev_info * devip) 920 struct sdebug_dev_info * devip)
@@ -609,64 +922,113 @@ static int resp_inquiry(struct scsi_cmnd * scp, int target,
609 unsigned char pq_pdt; 922 unsigned char pq_pdt;
610 unsigned char arr[SDEBUG_MAX_INQ_ARR_SZ]; 923 unsigned char arr[SDEBUG_MAX_INQ_ARR_SZ];
611 unsigned char *cmd = (unsigned char *)scp->cmnd; 924 unsigned char *cmd = (unsigned char *)scp->cmnd;
612 int alloc_len; 925 int alloc_len, n;
613 926
614 alloc_len = (cmd[3] << 8) + cmd[4]; 927 alloc_len = (cmd[3] << 8) + cmd[4];
615 memset(arr, 0, SDEBUG_MAX_INQ_ARR_SZ); 928 memset(arr, 0, SDEBUG_MAX_INQ_ARR_SZ);
616 pq_pdt = (scsi_debug_ptype & 0x1f); 929 if (devip->wlun)
930 pq_pdt = 0x1e; /* present, wlun */
931 else if (scsi_debug_no_lun_0 && (0 == devip->lun))
932 pq_pdt = 0x7f; /* not present, no device type */
933 else
934 pq_pdt = (scsi_debug_ptype & 0x1f);
617 arr[0] = pq_pdt; 935 arr[0] = pq_pdt;
618 if (0x2 & cmd[1]) { /* CMDDT bit set */ 936 if (0x2 & cmd[1]) { /* CMDDT bit set */
619 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 937 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
620 0); 938 0);
621 return check_condition_result; 939 return check_condition_result;
622 } else if (0x1 & cmd[1]) { /* EVPD bit set */ 940 } else if (0x1 & cmd[1]) { /* EVPD bit set */
623 int dev_id_num, len; 941 int lu_id_num, target_dev_id, len;
624 char dev_id_str[6]; 942 char lu_id_str[6];
943 int host_no = devip->sdbg_host->shost->host_no;
625 944
626 dev_id_num = ((devip->sdbg_host->shost->host_no + 1) * 2000) + 945 lu_id_num = devip->wlun ? -1 : (((host_no + 1) * 2000) +
627 (devip->target * 1000) + devip->lun; 946 (devip->target * 1000) + devip->lun);
628 len = scnprintf(dev_id_str, 6, "%d", dev_id_num); 947 target_dev_id = ((host_no + 1) * 2000) +
948 (devip->target * 1000) - 3;
949 len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
629 if (0 == cmd[2]) { /* supported vital product data pages */ 950 if (0 == cmd[2]) { /* supported vital product data pages */
630 arr[3] = 3; 951 arr[1] = cmd[2]; /*sanity */
631 arr[4] = 0x0; /* this page */ 952 n = 4;
632 arr[5] = 0x80; /* unit serial number */ 953 arr[n++] = 0x0; /* this page */
633 arr[6] = 0x83; /* device identification */ 954 arr[n++] = 0x80; /* unit serial number */
955 arr[n++] = 0x83; /* device identification */
956 arr[n++] = 0x84; /* software interface ident. */
957 arr[n++] = 0x85; /* management network addresses */
958 arr[n++] = 0x86; /* extended inquiry */
959 arr[n++] = 0x87; /* mode page policy */
960 arr[n++] = 0x88; /* SCSI ports */
961 arr[n++] = 0x89; /* ATA information */
962 arr[n++] = 0xb0; /* Block limits (SBC) */
963 arr[3] = n - 4; /* number of supported VPD pages */
634 } else if (0x80 == cmd[2]) { /* unit serial number */ 964 } else if (0x80 == cmd[2]) { /* unit serial number */
635 arr[1] = 0x80; 965 arr[1] = cmd[2]; /*sanity */
636 arr[3] = len; 966 arr[3] = len;
637 memcpy(&arr[4], dev_id_str, len); 967 memcpy(&arr[4], lu_id_str, len);
638 } else if (0x83 == cmd[2]) { /* device identification */ 968 } else if (0x83 == cmd[2]) { /* device identification */
639 arr[1] = 0x83; 969 arr[1] = cmd[2]; /*sanity */
640 arr[3] = inquiry_evpd_83(&arr[4], dev_id_num, 970 arr[3] = inquiry_evpd_83(&arr[4], target_dev_id,
641 dev_id_str, len); 971 lu_id_num, lu_id_str, len);
972 } else if (0x84 == cmd[2]) { /* Software interface ident. */
973 arr[1] = cmd[2]; /*sanity */
974 arr[3] = inquiry_evpd_84(&arr[4]);
975 } else if (0x85 == cmd[2]) { /* Management network addresses */
976 arr[1] = cmd[2]; /*sanity */
977 arr[3] = inquiry_evpd_85(&arr[4]);
978 } else if (0x86 == cmd[2]) { /* extended inquiry */
979 arr[1] = cmd[2]; /*sanity */
980 arr[3] = 0x3c; /* number of following entries */
981 arr[4] = 0x0; /* no protection stuff */
982 arr[5] = 0x7; /* head of q, ordered + simple q's */
983 } else if (0x87 == cmd[2]) { /* mode page policy */
984 arr[1] = cmd[2]; /*sanity */
985 arr[3] = 0x8; /* number of following entries */
986 arr[4] = 0x2; /* disconnect-reconnect mp */
987 arr[6] = 0x80; /* mlus, shared */
988 arr[8] = 0x18; /* protocol specific lu */
989 arr[10] = 0x82; /* mlus, per initiator port */
990 } else if (0x88 == cmd[2]) { /* SCSI Ports */
991 arr[1] = cmd[2]; /*sanity */
992 arr[3] = inquiry_evpd_88(&arr[4], target_dev_id);
993 } else if (0x89 == cmd[2]) { /* ATA information */
994 arr[1] = cmd[2]; /*sanity */
995 n = inquiry_evpd_89(&arr[4]);
996 arr[2] = (n >> 8);
997 arr[3] = (n & 0xff);
998 } else if (0xb0 == cmd[2]) { /* Block limits (SBC) */
999 arr[1] = cmd[2]; /*sanity */
1000 arr[3] = inquiry_evpd_b0(&arr[4]);
642 } else { 1001 } else {
643 /* Illegal request, invalid field in cdb */ 1002 /* Illegal request, invalid field in cdb */
644 mk_sense_buffer(devip, ILLEGAL_REQUEST, 1003 mk_sense_buffer(devip, ILLEGAL_REQUEST,
645 INVALID_FIELD_IN_CDB, 0); 1004 INVALID_FIELD_IN_CDB, 0);
646 return check_condition_result; 1005 return check_condition_result;
647 } 1006 }
1007 len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
648 return fill_from_dev_buffer(scp, arr, 1008 return fill_from_dev_buffer(scp, arr,
649 min(alloc_len, SDEBUG_MAX_INQ_ARR_SZ)); 1009 min(len, SDEBUG_MAX_INQ_ARR_SZ));
650 } 1010 }
651 /* drops through here for a standard inquiry */ 1011 /* drops through here for a standard inquiry */
652 arr[1] = DEV_REMOVEABLE(target) ? 0x80 : 0; /* Removable disk */ 1012 arr[1] = DEV_REMOVEABLE(target) ? 0x80 : 0; /* Removable disk */
653 arr[2] = scsi_debug_scsi_level; 1013 arr[2] = scsi_debug_scsi_level;
654 arr[3] = 2; /* response_data_format==2 */ 1014 arr[3] = 2; /* response_data_format==2 */
655 arr[4] = SDEBUG_LONG_INQ_SZ - 5; 1015 arr[4] = SDEBUG_LONG_INQ_SZ - 5;
656 arr[6] = 0x1; /* claim: ADDR16 */ 1016 arr[6] = 0x10; /* claim: MultiP */
657 /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */ 1017 /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
658 arr[7] = 0x3a; /* claim: WBUS16, SYNC, LINKED + CMDQUE */ 1018 arr[7] = 0xa; /* claim: LINKED + CMDQUE */
659 memcpy(&arr[8], inq_vendor_id, 8); 1019 memcpy(&arr[8], inq_vendor_id, 8);
660 memcpy(&arr[16], inq_product_id, 16); 1020 memcpy(&arr[16], inq_product_id, 16);
661 memcpy(&arr[32], inq_product_rev, 4); 1021 memcpy(&arr[32], inq_product_rev, 4);
662 /* version descriptors (2 bytes each) follow */ 1022 /* version descriptors (2 bytes each) follow */
663 arr[58] = 0x0; arr[59] = 0x40; /* SAM-2 */ 1023 arr[58] = 0x0; arr[59] = 0x77; /* SAM-3 ANSI */
664 arr[60] = 0x3; arr[61] = 0x0; /* SPC-3 */ 1024 arr[60] = 0x3; arr[61] = 0x14; /* SPC-3 ANSI */
1025 n = 62;
665 if (scsi_debug_ptype == 0) { 1026 if (scsi_debug_ptype == 0) {
666 arr[62] = 0x1; arr[63] = 0x80; /* SBC */ 1027 arr[n++] = 0x3; arr[n++] = 0x3d; /* SBC-2 ANSI */
667 } else if (scsi_debug_ptype == 1) { 1028 } else if (scsi_debug_ptype == 1) {
668 arr[62] = 0x2; arr[63] = 0x00; /* SSC */ 1029 arr[n++] = 0x3; arr[n++] = 0x60; /* SSC-2 no version */
669 } 1030 }
1031 arr[n++] = 0xc; arr[n++] = 0xf; /* SAS-1.1 rev 10 */
670 return fill_from_dev_buffer(scp, arr, 1032 return fill_from_dev_buffer(scp, arr,
671 min(alloc_len, SDEBUG_LONG_INQ_SZ)); 1033 min(alloc_len, SDEBUG_LONG_INQ_SZ));
672} 1034}
@@ -677,46 +1039,141 @@ static int resp_requests(struct scsi_cmnd * scp,
677 unsigned char * sbuff; 1039 unsigned char * sbuff;
678 unsigned char *cmd = (unsigned char *)scp->cmnd; 1040 unsigned char *cmd = (unsigned char *)scp->cmnd;
679 unsigned char arr[SDEBUG_SENSE_LEN]; 1041 unsigned char arr[SDEBUG_SENSE_LEN];
1042 int want_dsense;
680 int len = 18; 1043 int len = 18;
681 1044
682 memset(arr, 0, SDEBUG_SENSE_LEN); 1045 memset(arr, 0, sizeof(arr));
683 if (devip->reset == 1) 1046 if (devip->reset == 1)
684 mk_sense_buffer(devip, 0, NO_ADDED_SENSE, 0); 1047 mk_sense_buffer(devip, 0, NO_ADDITIONAL_SENSE, 0);
1048 want_dsense = !!(cmd[1] & 1) || scsi_debug_dsense;
685 sbuff = devip->sense_buff; 1049 sbuff = devip->sense_buff;
686 if ((cmd[1] & 1) && (! scsi_debug_dsense)) { 1050 if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
687 /* DESC bit set and sense_buff in fixed format */ 1051 if (want_dsense) {
688 arr[0] = 0x72; 1052 arr[0] = 0x72;
689 arr[1] = sbuff[2]; /* sense key */ 1053 arr[1] = 0x0; /* NO_SENSE in sense_key */
690 arr[2] = sbuff[12]; /* asc */ 1054 arr[2] = THRESHOLD_EXCEEDED;
691 arr[3] = sbuff[13]; /* ascq */ 1055 arr[3] = 0xff; /* TEST set and MRIE==6 */
692 len = 8; 1056 } else {
693 } else 1057 arr[0] = 0x70;
1058 arr[2] = 0x0; /* NO_SENSE in sense_key */
1059 arr[7] = 0xa; /* 18 byte sense buffer */
1060 arr[12] = THRESHOLD_EXCEEDED;
1061 arr[13] = 0xff; /* TEST set and MRIE==6 */
1062 }
1063 } else if (devip->stopped) {
1064 if (want_dsense) {
1065 arr[0] = 0x72;
1066 arr[1] = 0x0; /* NO_SENSE in sense_key */
1067 arr[2] = LOW_POWER_COND_ON;
1068 arr[3] = 0x0; /* TEST set and MRIE==6 */
1069 } else {
1070 arr[0] = 0x70;
1071 arr[2] = 0x0; /* NO_SENSE in sense_key */
1072 arr[7] = 0xa; /* 18 byte sense buffer */
1073 arr[12] = LOW_POWER_COND_ON;
1074 arr[13] = 0x0; /* TEST set and MRIE==6 */
1075 }
1076 } else {
694 memcpy(arr, sbuff, SDEBUG_SENSE_LEN); 1077 memcpy(arr, sbuff, SDEBUG_SENSE_LEN);
695 mk_sense_buffer(devip, 0, NO_ADDED_SENSE, 0); 1078 if ((cmd[1] & 1) && (! scsi_debug_dsense)) {
1079 /* DESC bit set and sense_buff in fixed format */
1080 memset(arr, 0, sizeof(arr));
1081 arr[0] = 0x72;
1082 arr[1] = sbuff[2]; /* sense key */
1083 arr[2] = sbuff[12]; /* asc */
1084 arr[3] = sbuff[13]; /* ascq */
1085 len = 8;
1086 }
1087 }
1088 mk_sense_buffer(devip, 0, NO_ADDITIONAL_SENSE, 0);
696 return fill_from_dev_buffer(scp, arr, len); 1089 return fill_from_dev_buffer(scp, arr, len);
697} 1090}
698 1091
1092static int resp_start_stop(struct scsi_cmnd * scp,
1093 struct sdebug_dev_info * devip)
1094{
1095 unsigned char *cmd = (unsigned char *)scp->cmnd;
1096 int power_cond, errsts, start;
1097
1098 if ((errsts = check_readiness(scp, 1, devip)))
1099 return errsts;
1100 power_cond = (cmd[4] & 0xf0) >> 4;
1101 if (power_cond) {
1102 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
1103 0);
1104 return check_condition_result;
1105 }
1106 start = cmd[4] & 1;
1107 if (start == devip->stopped)
1108 devip->stopped = !start;
1109 return 0;
1110}
1111
699#define SDEBUG_READCAP_ARR_SZ 8 1112#define SDEBUG_READCAP_ARR_SZ 8
700static int resp_readcap(struct scsi_cmnd * scp, 1113static int resp_readcap(struct scsi_cmnd * scp,
701 struct sdebug_dev_info * devip) 1114 struct sdebug_dev_info * devip)
702{ 1115{
703 unsigned char arr[SDEBUG_READCAP_ARR_SZ]; 1116 unsigned char arr[SDEBUG_READCAP_ARR_SZ];
704 unsigned long capac; 1117 unsigned int capac;
705 int errsts; 1118 int errsts;
706 1119
707 if ((errsts = check_reset(scp, devip))) 1120 if ((errsts = check_readiness(scp, 1, devip)))
708 return errsts; 1121 return errsts;
1122 /* following just in case virtual_gb changed */
1123 if (scsi_debug_virtual_gb > 0) {
1124 sdebug_capacity = 2048 * 1024;
1125 sdebug_capacity *= scsi_debug_virtual_gb;
1126 } else
1127 sdebug_capacity = sdebug_store_sectors;
709 memset(arr, 0, SDEBUG_READCAP_ARR_SZ); 1128 memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
710 capac = (unsigned long)sdebug_capacity - 1; 1129 if (sdebug_capacity < 0xffffffff) {
711 arr[0] = (capac >> 24); 1130 capac = (unsigned int)sdebug_capacity - 1;
712 arr[1] = (capac >> 16) & 0xff; 1131 arr[0] = (capac >> 24);
713 arr[2] = (capac >> 8) & 0xff; 1132 arr[1] = (capac >> 16) & 0xff;
714 arr[3] = capac & 0xff; 1133 arr[2] = (capac >> 8) & 0xff;
1134 arr[3] = capac & 0xff;
1135 } else {
1136 arr[0] = 0xff;
1137 arr[1] = 0xff;
1138 arr[2] = 0xff;
1139 arr[3] = 0xff;
1140 }
715 arr[6] = (SECT_SIZE_PER(target) >> 8) & 0xff; 1141 arr[6] = (SECT_SIZE_PER(target) >> 8) & 0xff;
716 arr[7] = SECT_SIZE_PER(target) & 0xff; 1142 arr[7] = SECT_SIZE_PER(target) & 0xff;
717 return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ); 1143 return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
718} 1144}
719 1145
1146#define SDEBUG_READCAP16_ARR_SZ 32
1147static int resp_readcap16(struct scsi_cmnd * scp,
1148 struct sdebug_dev_info * devip)
1149{
1150 unsigned char *cmd = (unsigned char *)scp->cmnd;
1151 unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
1152 unsigned long long capac;
1153 int errsts, k, alloc_len;
1154
1155 if ((errsts = check_readiness(scp, 1, devip)))
1156 return errsts;
1157 alloc_len = ((cmd[10] << 24) + (cmd[11] << 16) + (cmd[12] << 8)
1158 + cmd[13]);
1159 /* following just in case virtual_gb changed */
1160 if (scsi_debug_virtual_gb > 0) {
1161 sdebug_capacity = 2048 * 1024;
1162 sdebug_capacity *= scsi_debug_virtual_gb;
1163 } else
1164 sdebug_capacity = sdebug_store_sectors;
1165 memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
1166 capac = sdebug_capacity - 1;
1167 for (k = 0; k < 8; ++k, capac >>= 8)
1168 arr[7 - k] = capac & 0xff;
1169 arr[8] = (SECT_SIZE_PER(target) >> 24) & 0xff;
1170 arr[9] = (SECT_SIZE_PER(target) >> 16) & 0xff;
1171 arr[10] = (SECT_SIZE_PER(target) >> 8) & 0xff;
1172 arr[11] = SECT_SIZE_PER(target) & 0xff;
1173 return fill_from_dev_buffer(scp, arr,
1174 min(alloc_len, SDEBUG_READCAP16_ARR_SZ));
1175}
1176
720/* <<Following mode page info copied from ST318451LW>> */ 1177/* <<Following mode page info copied from ST318451LW>> */
721 1178
722static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target) 1179static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target)
@@ -772,27 +1229,98 @@ static int resp_caching_pg(unsigned char * p, int pcontrol, int target)
772 1229
773static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target) 1230static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target)
774{ /* Control mode page for mode_sense */ 1231{ /* Control mode page for mode_sense */
775 unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0, 1232 unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
1233 0, 0, 0, 0};
1234 unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
776 0, 0, 0x2, 0x4b}; 1235 0, 0, 0x2, 0x4b};
777 1236
778 if (scsi_debug_dsense) 1237 if (scsi_debug_dsense)
779 ctrl_m_pg[2] |= 0x4; 1238 ctrl_m_pg[2] |= 0x4;
1239 else
1240 ctrl_m_pg[2] &= ~0x4;
780 memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg)); 1241 memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
781 if (1 == pcontrol) 1242 if (1 == pcontrol)
782 memset(p + 2, 0, sizeof(ctrl_m_pg) - 2); 1243 memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
1244 else if (2 == pcontrol)
1245 memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
783 return sizeof(ctrl_m_pg); 1246 return sizeof(ctrl_m_pg);
784} 1247}
785 1248
1249
786static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target) 1250static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target)
787{ /* Informational Exceptions control mode page for mode_sense */ 1251{ /* Informational Exceptions control mode page for mode_sense */
788 unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, 1252 unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
789 0, 0, 0x0, 0x0}; 1253 0, 0, 0x0, 0x0};
1254 unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
1255 0, 0, 0x0, 0x0};
1256
790 memcpy(p, iec_m_pg, sizeof(iec_m_pg)); 1257 memcpy(p, iec_m_pg, sizeof(iec_m_pg));
791 if (1 == pcontrol) 1258 if (1 == pcontrol)
792 memset(p + 2, 0, sizeof(iec_m_pg) - 2); 1259 memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
1260 else if (2 == pcontrol)
1261 memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
793 return sizeof(iec_m_pg); 1262 return sizeof(iec_m_pg);
794} 1263}
795 1264
1265static int resp_sas_sf_m_pg(unsigned char * p, int pcontrol, int target)
1266{ /* SAS SSP mode page - short format for mode_sense */
1267 unsigned char sas_sf_m_pg[] = {0x19, 0x6,
1268 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0};
1269
1270 memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
1271 if (1 == pcontrol)
1272 memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
1273 return sizeof(sas_sf_m_pg);
1274}
1275
1276
1277static int resp_sas_pcd_m_spg(unsigned char * p, int pcontrol, int target,
1278 int target_dev_id)
1279{ /* SAS phy control and discover mode page for mode_sense */
1280 unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
1281 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
1282 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
1283 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
1284 0x2, 0, 0, 0, 0, 0, 0, 0,
1285 0x88, 0x99, 0, 0, 0, 0, 0, 0,
1286 0, 0, 0, 0, 0, 0, 0, 0,
1287 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
1288 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
1289 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
1290 0x3, 0, 0, 0, 0, 0, 0, 0,
1291 0x88, 0x99, 0, 0, 0, 0, 0, 0,
1292 0, 0, 0, 0, 0, 0, 0, 0,
1293 };
1294 int port_a, port_b;
1295
1296 port_a = target_dev_id + 1;
1297 port_b = port_a + 1;
1298 memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
1299 p[20] = (port_a >> 24);
1300 p[21] = (port_a >> 16) & 0xff;
1301 p[22] = (port_a >> 8) & 0xff;
1302 p[23] = port_a & 0xff;
1303 p[48 + 20] = (port_b >> 24);
1304 p[48 + 21] = (port_b >> 16) & 0xff;
1305 p[48 + 22] = (port_b >> 8) & 0xff;
1306 p[48 + 23] = port_b & 0xff;
1307 if (1 == pcontrol)
1308 memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
1309 return sizeof(sas_pcd_m_pg);
1310}
1311
1312static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol)
1313{ /* SAS SSP shared protocol specific port mode subpage */
1314 unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
1315 0, 0, 0, 0, 0, 0, 0, 0,
1316 };
1317
1318 memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
1319 if (1 == pcontrol)
1320 memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
1321 return sizeof(sas_sha_m_pg);
1322}
1323
796#define SDEBUG_MAX_MSENSE_SZ 256 1324#define SDEBUG_MAX_MSENSE_SZ 256
797 1325
798static int resp_mode_sense(struct scsi_cmnd * scp, int target, 1326static int resp_mode_sense(struct scsi_cmnd * scp, int target,
@@ -801,12 +1329,12 @@ static int resp_mode_sense(struct scsi_cmnd * scp, int target,
801 unsigned char dbd; 1329 unsigned char dbd;
802 int pcontrol, pcode, subpcode; 1330 int pcontrol, pcode, subpcode;
803 unsigned char dev_spec; 1331 unsigned char dev_spec;
804 int alloc_len, msense_6, offset, len, errsts; 1332 int alloc_len, msense_6, offset, len, errsts, target_dev_id;
805 unsigned char * ap; 1333 unsigned char * ap;
806 unsigned char arr[SDEBUG_MAX_MSENSE_SZ]; 1334 unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
807 unsigned char *cmd = (unsigned char *)scp->cmnd; 1335 unsigned char *cmd = (unsigned char *)scp->cmnd;
808 1336
809 if ((errsts = check_reset(scp, devip))) 1337 if ((errsts = check_readiness(scp, 1, devip)))
810 return errsts; 1338 return errsts;
811 dbd = cmd[1] & 0x8; 1339 dbd = cmd[1] & 0x8;
812 pcontrol = (cmd[2] & 0xc0) >> 6; 1340 pcontrol = (cmd[2] & 0xc0) >> 6;
@@ -820,6 +1348,8 @@ static int resp_mode_sense(struct scsi_cmnd * scp, int target,
820 0); 1348 0);
821 return check_condition_result; 1349 return check_condition_result;
822 } 1350 }
1351 target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
1352 (devip->target * 1000) - 3;
823 dev_spec = DEV_READONLY(target) ? 0x80 : 0x0; 1353 dev_spec = DEV_READONLY(target) ? 0x80 : 0x0;
824 if (msense_6) { 1354 if (msense_6) {
825 arr[2] = dev_spec; 1355 arr[2] = dev_spec;
@@ -830,7 +1360,8 @@ static int resp_mode_sense(struct scsi_cmnd * scp, int target,
830 } 1360 }
831 ap = arr + offset; 1361 ap = arr + offset;
832 1362
833 if (0 != subpcode) { /* TODO: Control Extension page */ 1363 if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) {
1364 /* TODO: Control Extension page */
834 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 1365 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
835 0); 1366 0);
836 return check_condition_result; 1367 return check_condition_result;
@@ -856,17 +1387,45 @@ static int resp_mode_sense(struct scsi_cmnd * scp, int target,
856 len = resp_ctrl_m_pg(ap, pcontrol, target); 1387 len = resp_ctrl_m_pg(ap, pcontrol, target);
857 offset += len; 1388 offset += len;
858 break; 1389 break;
1390 case 0x19: /* if spc==1 then sas phy, control+discover */
1391 if ((subpcode > 0x2) && (subpcode < 0xff)) {
1392 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1393 INVALID_FIELD_IN_CDB, 0);
1394 return check_condition_result;
1395 }
1396 len = 0;
1397 if ((0x0 == subpcode) || (0xff == subpcode))
1398 len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
1399 if ((0x1 == subpcode) || (0xff == subpcode))
1400 len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
1401 target_dev_id);
1402 if ((0x2 == subpcode) || (0xff == subpcode))
1403 len += resp_sas_sha_m_spg(ap + len, pcontrol);
1404 offset += len;
1405 break;
859 case 0x1c: /* Informational Exceptions Mode page, all devices */ 1406 case 0x1c: /* Informational Exceptions Mode page, all devices */
860 len = resp_iec_m_pg(ap, pcontrol, target); 1407 len = resp_iec_m_pg(ap, pcontrol, target);
861 offset += len; 1408 offset += len;
862 break; 1409 break;
863 case 0x3f: /* Read all Mode pages */ 1410 case 0x3f: /* Read all Mode pages */
864 len = resp_err_recov_pg(ap, pcontrol, target); 1411 if ((0 == subpcode) || (0xff == subpcode)) {
865 len += resp_disconnect_pg(ap + len, pcontrol, target); 1412 len = resp_err_recov_pg(ap, pcontrol, target);
866 len += resp_format_pg(ap + len, pcontrol, target); 1413 len += resp_disconnect_pg(ap + len, pcontrol, target);
867 len += resp_caching_pg(ap + len, pcontrol, target); 1414 len += resp_format_pg(ap + len, pcontrol, target);
868 len += resp_ctrl_m_pg(ap + len, pcontrol, target); 1415 len += resp_caching_pg(ap + len, pcontrol, target);
869 len += resp_iec_m_pg(ap + len, pcontrol, target); 1416 len += resp_ctrl_m_pg(ap + len, pcontrol, target);
1417 len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
1418 if (0xff == subpcode) {
1419 len += resp_sas_pcd_m_spg(ap + len, pcontrol,
1420 target, target_dev_id);
1421 len += resp_sas_sha_m_spg(ap + len, pcontrol);
1422 }
1423 len += resp_iec_m_pg(ap + len, pcontrol, target);
1424 } else {
1425 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1426 INVALID_FIELD_IN_CDB, 0);
1427 return check_condition_result;
1428 }
870 offset += len; 1429 offset += len;
871 break; 1430 break;
872 default: 1431 default:
@@ -883,71 +1442,274 @@ static int resp_mode_sense(struct scsi_cmnd * scp, int target,
883 return fill_from_dev_buffer(scp, arr, min(alloc_len, offset)); 1442 return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
884} 1443}
885 1444
886static int resp_read(struct scsi_cmnd * SCpnt, int upper_blk, int block, 1445#define SDEBUG_MAX_MSELECT_SZ 512
887 int num, struct sdebug_dev_info * devip) 1446
1447static int resp_mode_select(struct scsi_cmnd * scp, int mselect6,
1448 struct sdebug_dev_info * devip)
1449{
1450 int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
1451 int param_len, res, errsts, mpage;
1452 unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
1453 unsigned char *cmd = (unsigned char *)scp->cmnd;
1454
1455 if ((errsts = check_readiness(scp, 1, devip)))
1456 return errsts;
1457 memset(arr, 0, sizeof(arr));
1458 pf = cmd[1] & 0x10;
1459 sp = cmd[1] & 0x1;
1460 param_len = mselect6 ? cmd[4] : ((cmd[7] << 8) + cmd[8]);
1461 if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
1462 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1463 INVALID_FIELD_IN_CDB, 0);
1464 return check_condition_result;
1465 }
1466 res = fetch_to_dev_buffer(scp, arr, param_len);
1467 if (-1 == res)
1468 return (DID_ERROR << 16);
1469 else if ((res < param_len) &&
1470 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
1471 printk(KERN_INFO "scsi_debug: mode_select: cdb indicated=%d, "
1472 " IO sent=%d bytes\n", param_len, res);
1473 md_len = mselect6 ? (arr[0] + 1) : ((arr[0] << 8) + arr[1] + 2);
1474 bd_len = mselect6 ? arr[3] : ((arr[6] << 8) + arr[7]);
1475 if ((md_len > 2) || (0 != bd_len)) {
1476 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1477 INVALID_FIELD_IN_PARAM_LIST, 0);
1478 return check_condition_result;
1479 }
1480 off = bd_len + (mselect6 ? 4 : 8);
1481 mpage = arr[off] & 0x3f;
1482 ps = !!(arr[off] & 0x80);
1483 if (ps) {
1484 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1485 INVALID_FIELD_IN_PARAM_LIST, 0);
1486 return check_condition_result;
1487 }
1488 spf = !!(arr[off] & 0x40);
1489 pg_len = spf ? ((arr[off + 2] << 8) + arr[off + 3] + 4) :
1490 (arr[off + 1] + 2);
1491 if ((pg_len + off) > param_len) {
1492 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1493 PARAMETER_LIST_LENGTH_ERR, 0);
1494 return check_condition_result;
1495 }
1496 switch (mpage) {
1497 case 0xa: /* Control Mode page */
1498 if (ctrl_m_pg[1] == arr[off + 1]) {
1499 memcpy(ctrl_m_pg + 2, arr + off + 2,
1500 sizeof(ctrl_m_pg) - 2);
1501 scsi_debug_dsense = !!(ctrl_m_pg[2] & 0x4);
1502 return 0;
1503 }
1504 break;
1505 case 0x1c: /* Informational Exceptions Mode page */
1506 if (iec_m_pg[1] == arr[off + 1]) {
1507 memcpy(iec_m_pg + 2, arr + off + 2,
1508 sizeof(iec_m_pg) - 2);
1509 return 0;
1510 }
1511 break;
1512 default:
1513 break;
1514 }
1515 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1516 INVALID_FIELD_IN_PARAM_LIST, 0);
1517 return check_condition_result;
1518}
1519
1520static int resp_temp_l_pg(unsigned char * arr)
1521{
1522 unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38,
1523 0x0, 0x1, 0x3, 0x2, 0x0, 65,
1524 };
1525
1526 memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
1527 return sizeof(temp_l_pg);
1528}
1529
1530static int resp_ie_l_pg(unsigned char * arr)
1531{
1532 unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
1533 };
1534
1535 memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
1536 if (iec_m_pg[2] & 0x4) { /* TEST bit set */
1537 arr[4] = THRESHOLD_EXCEEDED;
1538 arr[5] = 0xff;
1539 }
1540 return sizeof(ie_l_pg);
1541}
1542
1543#define SDEBUG_MAX_LSENSE_SZ 512
1544
1545static int resp_log_sense(struct scsi_cmnd * scp,
1546 struct sdebug_dev_info * devip)
1547{
1548 int ppc, sp, pcontrol, pcode, alloc_len, errsts, len, n;
1549 unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
1550 unsigned char *cmd = (unsigned char *)scp->cmnd;
1551
1552 if ((errsts = check_readiness(scp, 1, devip)))
1553 return errsts;
1554 memset(arr, 0, sizeof(arr));
1555 ppc = cmd[1] & 0x2;
1556 sp = cmd[1] & 0x1;
1557 if (ppc || sp) {
1558 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1559 INVALID_FIELD_IN_CDB, 0);
1560 return check_condition_result;
1561 }
1562 pcontrol = (cmd[2] & 0xc0) >> 6;
1563 pcode = cmd[2] & 0x3f;
1564 alloc_len = (cmd[7] << 8) + cmd[8];
1565 arr[0] = pcode;
1566 switch (pcode) {
1567 case 0x0: /* Supported log pages log page */
1568 n = 4;
1569 arr[n++] = 0x0; /* this page */
1570 arr[n++] = 0xd; /* Temperature */
1571 arr[n++] = 0x2f; /* Informational exceptions */
1572 arr[3] = n - 4;
1573 break;
1574 case 0xd: /* Temperature log page */
1575 arr[3] = resp_temp_l_pg(arr + 4);
1576 break;
1577 case 0x2f: /* Informational exceptions log page */
1578 arr[3] = resp_ie_l_pg(arr + 4);
1579 break;
1580 default:
1581 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1582 INVALID_FIELD_IN_CDB, 0);
1583 return check_condition_result;
1584 }
1585 len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
1586 return fill_from_dev_buffer(scp, arr,
1587 min(len, SDEBUG_MAX_INQ_ARR_SZ));
1588}
1589
1590static int resp_read(struct scsi_cmnd * SCpnt, unsigned long long lba,
1591 unsigned int num, struct sdebug_dev_info * devip)
888{ 1592{
889 unsigned long iflags; 1593 unsigned long iflags;
1594 unsigned int block, from_bottom;
1595 unsigned long long u;
890 int ret; 1596 int ret;
891 1597
892 if (upper_blk || (block + num > sdebug_capacity)) { 1598 if (lba + num > sdebug_capacity) {
893 mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE, 1599 mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
894 0); 1600 0);
895 return check_condition_result; 1601 return check_condition_result;
896 } 1602 }
1603 /* transfer length excessive (tie in to block limits VPD page) */
1604 if (num > sdebug_store_sectors) {
1605 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
1606 0);
1607 return check_condition_result;
1608 }
897 if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) && 1609 if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) &&
898 (block <= OPT_MEDIUM_ERR_ADDR) && 1610 (lba <= OPT_MEDIUM_ERR_ADDR) &&
899 ((block + num) > OPT_MEDIUM_ERR_ADDR)) { 1611 ((lba + num) > OPT_MEDIUM_ERR_ADDR)) {
1612 /* claim unrecoverable read error */
900 mk_sense_buffer(devip, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 1613 mk_sense_buffer(devip, MEDIUM_ERROR, UNRECOVERED_READ_ERR,
901 0); 1614 0);
902 /* claim unrecoverable read error */ 1615 /* set info field and valid bit for fixed descriptor */
1616 if (0x70 == (devip->sense_buff[0] & 0x7f)) {
1617 devip->sense_buff[0] |= 0x80; /* Valid bit */
1618 ret = OPT_MEDIUM_ERR_ADDR;
1619 devip->sense_buff[3] = (ret >> 24) & 0xff;
1620 devip->sense_buff[4] = (ret >> 16) & 0xff;
1621 devip->sense_buff[5] = (ret >> 8) & 0xff;
1622 devip->sense_buff[6] = ret & 0xff;
1623 }
903 return check_condition_result; 1624 return check_condition_result;
904 } 1625 }
905 read_lock_irqsave(&atomic_rw, iflags); 1626 read_lock_irqsave(&atomic_rw, iflags);
906 ret = fill_from_dev_buffer(SCpnt, fake_storep + (block * SECT_SIZE), 1627 if ((lba + num) <= sdebug_store_sectors)
907 num * SECT_SIZE); 1628 ret = fill_from_dev_buffer(SCpnt,
1629 fake_storep + (lba * SECT_SIZE),
1630 num * SECT_SIZE);
1631 else {
1632 /* modulo when one arg is 64 bits needs do_div() */
1633 u = lba;
1634 block = do_div(u, sdebug_store_sectors);
1635 from_bottom = 0;
1636 if ((block + num) > sdebug_store_sectors)
1637 from_bottom = (block + num) - sdebug_store_sectors;
1638 ret = fill_from_dev_buffer(SCpnt,
1639 fake_storep + (block * SECT_SIZE),
1640 (num - from_bottom) * SECT_SIZE);
1641 if ((0 == ret) && (from_bottom > 0))
1642 ret = fill_from_dev_buffer(SCpnt, fake_storep,
1643 from_bottom * SECT_SIZE);
1644 }
908 read_unlock_irqrestore(&atomic_rw, iflags); 1645 read_unlock_irqrestore(&atomic_rw, iflags);
909 return ret; 1646 return ret;
910} 1647}
911 1648
912static int resp_write(struct scsi_cmnd * SCpnt, int upper_blk, int block, 1649static int resp_write(struct scsi_cmnd * SCpnt, unsigned long long lba,
913 int num, struct sdebug_dev_info * devip) 1650 unsigned int num, struct sdebug_dev_info * devip)
914{ 1651{
915 unsigned long iflags; 1652 unsigned long iflags;
1653 unsigned int block, to_bottom;
1654 unsigned long long u;
916 int res; 1655 int res;
917 1656
918 if (upper_blk || (block + num > sdebug_capacity)) { 1657 if (lba + num > sdebug_capacity) {
919 mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE, 1658 mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
920 0); 1659 0);
921 return check_condition_result; 1660 return check_condition_result;
922 } 1661 }
1662 /* transfer length excessive (tie in to block limits VPD page) */
1663 if (num > sdebug_store_sectors) {
1664 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
1665 0);
1666 return check_condition_result;
1667 }
923 1668
924 write_lock_irqsave(&atomic_rw, iflags); 1669 write_lock_irqsave(&atomic_rw, iflags);
925 res = fetch_to_dev_buffer(SCpnt, fake_storep + (block * SECT_SIZE), 1670 if ((lba + num) <= sdebug_store_sectors)
926 num * SECT_SIZE); 1671 res = fetch_to_dev_buffer(SCpnt,
1672 fake_storep + (lba * SECT_SIZE),
1673 num * SECT_SIZE);
1674 else {
1675 /* modulo when one arg is 64 bits needs do_div() */
1676 u = lba;
1677 block = do_div(u, sdebug_store_sectors);
1678 to_bottom = 0;
1679 if ((block + num) > sdebug_store_sectors)
1680 to_bottom = (block + num) - sdebug_store_sectors;
1681 res = fetch_to_dev_buffer(SCpnt,
1682 fake_storep + (block * SECT_SIZE),
1683 (num - to_bottom) * SECT_SIZE);
1684 if ((0 == res) && (to_bottom > 0))
1685 res = fetch_to_dev_buffer(SCpnt, fake_storep,
1686 to_bottom * SECT_SIZE);
1687 }
927 write_unlock_irqrestore(&atomic_rw, iflags); 1688 write_unlock_irqrestore(&atomic_rw, iflags);
928 if (-1 == res) 1689 if (-1 == res)
929 return (DID_ERROR << 16); 1690 return (DID_ERROR << 16);
930 else if ((res < (num * SECT_SIZE)) && 1691 else if ((res < (num * SECT_SIZE)) &&
931 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)) 1692 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
932 printk(KERN_INFO "scsi_debug: write: cdb indicated=%d, " 1693 printk(KERN_INFO "scsi_debug: write: cdb indicated=%u, "
933 " IO sent=%d bytes\n", num * SECT_SIZE, res); 1694 " IO sent=%d bytes\n", num * SECT_SIZE, res);
934 return 0; 1695 return 0;
935} 1696}
936 1697
937#define SDEBUG_RLUN_ARR_SZ 128 1698#define SDEBUG_RLUN_ARR_SZ 256
938 1699
939static int resp_report_luns(struct scsi_cmnd * scp, 1700static int resp_report_luns(struct scsi_cmnd * scp,
940 struct sdebug_dev_info * devip) 1701 struct sdebug_dev_info * devip)
941{ 1702{
942 unsigned int alloc_len; 1703 unsigned int alloc_len;
943 int lun_cnt, i, upper; 1704 int lun_cnt, i, upper, num, n, wlun, lun;
944 unsigned char *cmd = (unsigned char *)scp->cmnd; 1705 unsigned char *cmd = (unsigned char *)scp->cmnd;
945 int select_report = (int)cmd[2]; 1706 int select_report = (int)cmd[2];
946 struct scsi_lun *one_lun; 1707 struct scsi_lun *one_lun;
947 unsigned char arr[SDEBUG_RLUN_ARR_SZ]; 1708 unsigned char arr[SDEBUG_RLUN_ARR_SZ];
1709 unsigned char * max_addr;
948 1710
949 alloc_len = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24); 1711 alloc_len = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24);
950 if ((alloc_len < 16) || (select_report > 2)) { 1712 if ((alloc_len < 4) || (select_report > 2)) {
951 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 1713 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
952 0); 1714 0);
953 return check_condition_result; 1715 return check_condition_result;
@@ -955,18 +1717,37 @@ static int resp_report_luns(struct scsi_cmnd * scp,
955 /* can produce response with up to 16k luns (lun 0 to lun 16383) */ 1717 /* can produce response with up to 16k luns (lun 0 to lun 16383) */
956 memset(arr, 0, SDEBUG_RLUN_ARR_SZ); 1718 memset(arr, 0, SDEBUG_RLUN_ARR_SZ);
957 lun_cnt = scsi_debug_max_luns; 1719 lun_cnt = scsi_debug_max_luns;
958 arr[2] = ((sizeof(struct scsi_lun) * lun_cnt) >> 8) & 0xff; 1720 if (1 == select_report)
959 arr[3] = (sizeof(struct scsi_lun) * lun_cnt) & 0xff; 1721 lun_cnt = 0;
960 lun_cnt = min((int)((SDEBUG_RLUN_ARR_SZ - 8) / 1722 else if (scsi_debug_no_lun_0 && (lun_cnt > 0))
961 sizeof(struct scsi_lun)), lun_cnt); 1723 --lun_cnt;
1724 wlun = (select_report > 0) ? 1 : 0;
1725 num = lun_cnt + wlun;
1726 arr[2] = ((sizeof(struct scsi_lun) * num) >> 8) & 0xff;
1727 arr[3] = (sizeof(struct scsi_lun) * num) & 0xff;
1728 n = min((int)((SDEBUG_RLUN_ARR_SZ - 8) /
1729 sizeof(struct scsi_lun)), num);
1730 if (n < num) {
1731 wlun = 0;
1732 lun_cnt = n;
1733 }
962 one_lun = (struct scsi_lun *) &arr[8]; 1734 one_lun = (struct scsi_lun *) &arr[8];
963 for (i = 0; i < lun_cnt; i++) { 1735 max_addr = arr + SDEBUG_RLUN_ARR_SZ;
964 upper = (i >> 8) & 0x3f; 1736 for (i = 0, lun = (scsi_debug_no_lun_0 ? 1 : 0);
1737 ((i < lun_cnt) && ((unsigned char *)(one_lun + i) < max_addr));
1738 i++, lun++) {
1739 upper = (lun >> 8) & 0x3f;
965 if (upper) 1740 if (upper)
966 one_lun[i].scsi_lun[0] = 1741 one_lun[i].scsi_lun[0] =
967 (upper | (SAM2_LUN_ADDRESS_METHOD << 6)); 1742 (upper | (SAM2_LUN_ADDRESS_METHOD << 6));
968 one_lun[i].scsi_lun[1] = i & 0xff; 1743 one_lun[i].scsi_lun[1] = lun & 0xff;
1744 }
1745 if (wlun) {
1746 one_lun[i].scsi_lun[0] = (SAM2_WLUN_REPORT_LUNS >> 8) & 0xff;
1747 one_lun[i].scsi_lun[1] = SAM2_WLUN_REPORT_LUNS & 0xff;
1748 i++;
969 } 1749 }
1750 alloc_len = (unsigned char *)(one_lun + i) - arr;
970 return fill_from_dev_buffer(scp, arr, 1751 return fill_from_dev_buffer(scp, arr,
971 min((int)alloc_len, SDEBUG_RLUN_ARR_SZ)); 1752 min((int)alloc_len, SDEBUG_RLUN_ARR_SZ));
972} 1753}
@@ -1002,7 +1783,8 @@ static void timer_intr_handler(unsigned long indx)
1002static int scsi_debug_slave_alloc(struct scsi_device * sdp) 1783static int scsi_debug_slave_alloc(struct scsi_device * sdp)
1003{ 1784{
1004 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) 1785 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1005 sdev_printk(KERN_INFO, sdp, "scsi_debug: slave_alloc\n"); 1786 printk(KERN_INFO "scsi_debug: slave_alloc <%u %u %u %u>\n",
1787 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
1006 return 0; 1788 return 0;
1007} 1789}
1008 1790
@@ -1011,7 +1793,8 @@ static int scsi_debug_slave_configure(struct scsi_device * sdp)
1011 struct sdebug_dev_info * devip; 1793 struct sdebug_dev_info * devip;
1012 1794
1013 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) 1795 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1014 sdev_printk(KERN_INFO, sdp, "scsi_debug: slave_configure\n"); 1796 printk(KERN_INFO "scsi_debug: slave_configure <%u %u %u %u>\n",
1797 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
1015 if (sdp->host->max_cmd_len != SCSI_DEBUG_MAX_CMD_LEN) 1798 if (sdp->host->max_cmd_len != SCSI_DEBUG_MAX_CMD_LEN)
1016 sdp->host->max_cmd_len = SCSI_DEBUG_MAX_CMD_LEN; 1799 sdp->host->max_cmd_len = SCSI_DEBUG_MAX_CMD_LEN;
1017 devip = devInfoReg(sdp); 1800 devip = devInfoReg(sdp);
@@ -1019,6 +1802,7 @@ static int scsi_debug_slave_configure(struct scsi_device * sdp)
1019 if (sdp->host->cmd_per_lun) 1802 if (sdp->host->cmd_per_lun)
1020 scsi_adjust_queue_depth(sdp, SDEBUG_TAGGED_QUEUING, 1803 scsi_adjust_queue_depth(sdp, SDEBUG_TAGGED_QUEUING,
1021 sdp->host->cmd_per_lun); 1804 sdp->host->cmd_per_lun);
1805 blk_queue_max_segment_size(sdp->request_queue, 256 * 1024);
1022 return 0; 1806 return 0;
1023} 1807}
1024 1808
@@ -1028,7 +1812,8 @@ static void scsi_debug_slave_destroy(struct scsi_device * sdp)
1028 (struct sdebug_dev_info *)sdp->hostdata; 1812 (struct sdebug_dev_info *)sdp->hostdata;
1029 1813
1030 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) 1814 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1031 sdev_printk(KERN_INFO, sdp, "scsi_debug: slave_destroy\n"); 1815 printk(KERN_INFO "scsi_debug: slave_destroy <%u %u %u %u>\n",
1816 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
1032 if (devip) { 1817 if (devip) {
1033 /* make this slot avaliable for re-use */ 1818 /* make this slot avaliable for re-use */
1034 devip->used = 0; 1819 devip->used = 0;
@@ -1085,6 +1870,8 @@ static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev)
1085 open_devip->sense_buff[0] = 0x70; 1870 open_devip->sense_buff[0] = 0x70;
1086 open_devip->sense_buff[7] = 0xa; 1871 open_devip->sense_buff[7] = 0xa;
1087 } 1872 }
1873 if (sdev->lun == SAM2_WLUN_REPORT_LUNS)
1874 open_devip->wlun = SAM2_WLUN_REPORT_LUNS & 0xff;
1088 return open_devip; 1875 return open_devip;
1089 } 1876 }
1090 return NULL; 1877 return NULL;
@@ -1273,7 +2060,7 @@ static void __init sdebug_build_parts(unsigned char * ramp)
1273 printk(KERN_WARNING "scsi_debug:build_parts: reducing " 2060 printk(KERN_WARNING "scsi_debug:build_parts: reducing "
1274 "partitions to %d\n", SDEBUG_MAX_PARTS); 2061 "partitions to %d\n", SDEBUG_MAX_PARTS);
1275 } 2062 }
1276 num_sectors = (int)(sdebug_store_size / SECT_SIZE); 2063 num_sectors = (int)sdebug_store_sectors;
1277 sectors_per_part = (num_sectors - sdebug_sectors_per) 2064 sectors_per_part = (num_sectors - sdebug_sectors_per)
1278 / scsi_debug_num_parts; 2065 / scsi_debug_num_parts;
1279 heads_by_sects = sdebug_heads * sdebug_sectors_per; 2066 heads_by_sects = sdebug_heads * sdebug_sectors_per;
@@ -1316,9 +2103,9 @@ static int schedule_resp(struct scsi_cmnd * cmnd,
1316 if (scsi_result) { 2103 if (scsi_result) {
1317 struct scsi_device * sdp = cmnd->device; 2104 struct scsi_device * sdp = cmnd->device;
1318 2105
1319 sdev_printk(KERN_INFO, sdp, 2106 printk(KERN_INFO "scsi_debug: <%u %u %u %u> "
1320 "non-zero result=0x%x\n", 2107 "non-zero result=0x%x\n", sdp->host->host_no,
1321 scsi_result); 2108 sdp->channel, sdp->id, sdp->lun, scsi_result);
1322 } 2109 }
1323 } 2110 }
1324 if (cmnd && devip) { 2111 if (cmnd && devip) {
@@ -1365,21 +2152,19 @@ static int schedule_resp(struct scsi_cmnd * cmnd,
1365 } 2152 }
1366} 2153}
1367 2154
1368/* Set 'perm' (4th argument) to 0 to disable module_param's definition 2155module_param_named(add_host, scsi_debug_add_host, int, S_IRUGO | S_IWUSR);
1369 * of sysfs parameters (which module_param doesn't yet support). 2156module_param_named(delay, scsi_debug_delay, int, S_IRUGO | S_IWUSR);
1370 * Sysfs parameters defined explicitly below. 2157module_param_named(dev_size_mb, scsi_debug_dev_size_mb, int, S_IRUGO);
1371 */ 2158module_param_named(dsense, scsi_debug_dsense, int, S_IRUGO | S_IWUSR);
1372module_param_named(add_host, scsi_debug_add_host, int, 0); /* perm=0644 */ 2159module_param_named(every_nth, scsi_debug_every_nth, int, S_IRUGO | S_IWUSR);
1373module_param_named(delay, scsi_debug_delay, int, 0); /* perm=0644 */ 2160module_param_named(max_luns, scsi_debug_max_luns, int, S_IRUGO | S_IWUSR);
1374module_param_named(dev_size_mb, scsi_debug_dev_size_mb, int, 0); 2161module_param_named(no_lun_0, scsi_debug_no_lun_0, int, S_IRUGO | S_IWUSR);
1375module_param_named(dsense, scsi_debug_dsense, int, 0); 2162module_param_named(num_parts, scsi_debug_num_parts, int, S_IRUGO);
1376module_param_named(every_nth, scsi_debug_every_nth, int, 0); 2163module_param_named(num_tgts, scsi_debug_num_tgts, int, S_IRUGO | S_IWUSR);
1377module_param_named(max_luns, scsi_debug_max_luns, int, 0); 2164module_param_named(opts, scsi_debug_opts, int, S_IRUGO | S_IWUSR);
1378module_param_named(num_parts, scsi_debug_num_parts, int, 0); 2165module_param_named(ptype, scsi_debug_ptype, int, S_IRUGO | S_IWUSR);
1379module_param_named(num_tgts, scsi_debug_num_tgts, int, 0); 2166module_param_named(scsi_level, scsi_debug_scsi_level, int, S_IRUGO);
1380module_param_named(opts, scsi_debug_opts, int, 0); /* perm=0644 */ 2167module_param_named(virtual_gb, scsi_debug_virtual_gb, int, S_IRUGO | S_IWUSR);
1381module_param_named(ptype, scsi_debug_ptype, int, 0);
1382module_param_named(scsi_level, scsi_debug_scsi_level, int, 0);
1383 2168
1384MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert"); 2169MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
1385MODULE_DESCRIPTION("SCSI debug adapter driver"); 2170MODULE_DESCRIPTION("SCSI debug adapter driver");
@@ -1388,15 +2173,17 @@ MODULE_VERSION(SCSI_DEBUG_VERSION);
1388 2173
1389MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)"); 2174MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
1390MODULE_PARM_DESC(delay, "# of jiffies to delay response(def=1)"); 2175MODULE_PARM_DESC(delay, "# of jiffies to delay response(def=1)");
1391MODULE_PARM_DESC(dev_size_mb, "size in MB of ram shared by devs"); 2176MODULE_PARM_DESC(dev_size_mb, "size in MB of ram shared by devs(def=8)");
1392MODULE_PARM_DESC(dsense, "use descriptor sense format(def: fixed)"); 2177MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
1393MODULE_PARM_DESC(every_nth, "timeout every nth command(def=100)"); 2178MODULE_PARM_DESC(every_nth, "timeout every nth command(def=100)");
1394MODULE_PARM_DESC(max_luns, "number of SCSI LUNs per target to simulate"); 2179MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
2180MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
1395MODULE_PARM_DESC(num_parts, "number of partitions(def=0)"); 2181MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
1396MODULE_PARM_DESC(num_tgts, "number of SCSI targets per host to simulate"); 2182MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
1397MODULE_PARM_DESC(opts, "1->noise, 2->medium_error, 4->..."); 2183MODULE_PARM_DESC(opts, "1->noise, 2->medium_error, 4->... (def=0)");
1398MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])"); 2184MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
1399MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=5[SPC-3])"); 2185MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=5[SPC-3])");
2186MODULE_PARM_DESC(virtual_gb, "virtual gigabyte size (def=0 -> use dev_size_mb)");
1400 2187
1401 2188
1402static char sdebug_info[256]; 2189static char sdebug_info[256];
@@ -1548,6 +2335,24 @@ static ssize_t sdebug_dsense_store(struct device_driver * ddp,
1548DRIVER_ATTR(dsense, S_IRUGO | S_IWUSR, sdebug_dsense_show, 2335DRIVER_ATTR(dsense, S_IRUGO | S_IWUSR, sdebug_dsense_show,
1549 sdebug_dsense_store); 2336 sdebug_dsense_store);
1550 2337
2338static ssize_t sdebug_no_lun_0_show(struct device_driver * ddp, char * buf)
2339{
2340 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_no_lun_0);
2341}
2342static ssize_t sdebug_no_lun_0_store(struct device_driver * ddp,
2343 const char * buf, size_t count)
2344{
2345 int n;
2346
2347 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
2348 scsi_debug_no_lun_0 = n;
2349 return count;
2350 }
2351 return -EINVAL;
2352}
2353DRIVER_ATTR(no_lun_0, S_IRUGO | S_IWUSR, sdebug_no_lun_0_show,
2354 sdebug_no_lun_0_store);
2355
1551static ssize_t sdebug_num_tgts_show(struct device_driver * ddp, char * buf) 2356static ssize_t sdebug_num_tgts_show(struct device_driver * ddp, char * buf)
1552{ 2357{
1553 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_tgts); 2358 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_tgts);
@@ -1623,6 +2428,29 @@ static ssize_t sdebug_scsi_level_show(struct device_driver * ddp, char * buf)
1623} 2428}
1624DRIVER_ATTR(scsi_level, S_IRUGO, sdebug_scsi_level_show, NULL); 2429DRIVER_ATTR(scsi_level, S_IRUGO, sdebug_scsi_level_show, NULL);
1625 2430
2431static ssize_t sdebug_virtual_gb_show(struct device_driver * ddp, char * buf)
2432{
2433 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_virtual_gb);
2434}
2435static ssize_t sdebug_virtual_gb_store(struct device_driver * ddp,
2436 const char * buf, size_t count)
2437{
2438 int n;
2439
2440 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
2441 scsi_debug_virtual_gb = n;
2442 if (scsi_debug_virtual_gb > 0) {
2443 sdebug_capacity = 2048 * 1024;
2444 sdebug_capacity *= scsi_debug_virtual_gb;
2445 } else
2446 sdebug_capacity = sdebug_store_sectors;
2447 return count;
2448 }
2449 return -EINVAL;
2450}
2451DRIVER_ATTR(virtual_gb, S_IRUGO | S_IWUSR, sdebug_virtual_gb_show,
2452 sdebug_virtual_gb_store);
2453
1626static ssize_t sdebug_add_host_show(struct device_driver * ddp, char * buf) 2454static ssize_t sdebug_add_host_show(struct device_driver * ddp, char * buf)
1627{ 2455{
1628 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_add_host); 2456 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_add_host);
@@ -1692,14 +2520,19 @@ static void do_remove_driverfs_files(void)
1692 2520
1693static int __init scsi_debug_init(void) 2521static int __init scsi_debug_init(void)
1694{ 2522{
1695 unsigned long sz; 2523 unsigned int sz;
1696 int host_to_add; 2524 int host_to_add;
1697 int k; 2525 int k;
1698 2526
1699 if (scsi_debug_dev_size_mb < 1) 2527 if (scsi_debug_dev_size_mb < 1)
1700 scsi_debug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */ 2528 scsi_debug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
1701 sdebug_store_size = (unsigned long)scsi_debug_dev_size_mb * 1048576; 2529 sdebug_store_size = (unsigned int)scsi_debug_dev_size_mb * 1048576;
1702 sdebug_capacity = sdebug_store_size / SECT_SIZE; 2530 sdebug_store_sectors = sdebug_store_size / SECT_SIZE;
2531 if (scsi_debug_virtual_gb > 0) {
2532 sdebug_capacity = 2048 * 1024;
2533 sdebug_capacity *= scsi_debug_virtual_gb;
2534 } else
2535 sdebug_capacity = sdebug_store_sectors;
1703 2536
1704 /* play around with geometry, don't waste too much on track 0 */ 2537 /* play around with geometry, don't waste too much on track 0 */
1705 sdebug_heads = 8; 2538 sdebug_heads = 8;
@@ -1813,7 +2646,7 @@ static int sdebug_add_adapter(void)
1813 struct sdebug_dev_info *sdbg_devinfo; 2646 struct sdebug_dev_info *sdbg_devinfo;
1814 struct list_head *lh, *lh_sf; 2647 struct list_head *lh, *lh_sf;
1815 2648
1816 sdbg_host = kzalloc(sizeof(*sdbg_host), GFP_KERNEL); 2649 sdbg_host = kzalloc(sizeof(*sdbg_host),GFP_KERNEL);
1817 2650
1818 if (NULL == sdbg_host) { 2651 if (NULL == sdbg_host) {
1819 printk(KERN_ERR "%s: out of memory at line %d\n", 2652 printk(KERN_ERR "%s: out of memory at line %d\n",
@@ -1825,7 +2658,7 @@ static int sdebug_add_adapter(void)
1825 2658
1826 devs_per_host = scsi_debug_num_tgts * scsi_debug_max_luns; 2659 devs_per_host = scsi_debug_num_tgts * scsi_debug_max_luns;
1827 for (k = 0; k < devs_per_host; k++) { 2660 for (k = 0; k < devs_per_host; k++) {
1828 sdbg_devinfo = kzalloc(sizeof(*sdbg_devinfo), GFP_KERNEL); 2661 sdbg_devinfo = kzalloc(sizeof(*sdbg_devinfo),GFP_KERNEL);
1829 if (NULL == sdbg_devinfo) { 2662 if (NULL == sdbg_devinfo) {
1830 printk(KERN_ERR "%s: out of memory at line %d\n", 2663 printk(KERN_ERR "%s: out of memory at line %d\n",
1831 __FUNCTION__, __LINE__); 2664 __FUNCTION__, __LINE__);
@@ -1906,7 +2739,7 @@ static int sdebug_driver_probe(struct device * dev)
1906 hpnt->max_id = scsi_debug_num_tgts + 1; 2739 hpnt->max_id = scsi_debug_num_tgts + 1;
1907 else 2740 else
1908 hpnt->max_id = scsi_debug_num_tgts; 2741 hpnt->max_id = scsi_debug_num_tgts;
1909 hpnt->max_lun = scsi_debug_max_luns; 2742 hpnt->max_lun = SAM2_WLUN_REPORT_LUNS; /* = scsi_debug_max_luns; */
1910 2743
1911 error = scsi_add_host(hpnt, &sdbg_host->dev); 2744 error = scsi_add_host(hpnt, &sdbg_host->dev);
1912 if (error) { 2745 if (error) {
@@ -1960,7 +2793,7 @@ static void sdebug_max_tgts_luns(void)
1960 hpnt->max_id = scsi_debug_num_tgts + 1; 2793 hpnt->max_id = scsi_debug_num_tgts + 1;
1961 else 2794 else
1962 hpnt->max_id = scsi_debug_num_tgts; 2795 hpnt->max_id = scsi_debug_num_tgts;
1963 hpnt->max_lun = scsi_debug_max_luns; 2796 hpnt->max_lun = SAM2_WLUN_REPORT_LUNS; /* scsi_debug_max_luns; */
1964 } 2797 }
1965 spin_unlock(&sdebug_host_list_lock); 2798 spin_unlock(&sdebug_host_list_lock);
1966} 2799}