Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / scsi / scsi_debug.c
blobaa0c26fc91061f16a0af7a65179177355c4bb5cb
1 /*
2 * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
3 * Copyright (C) 1992 Eric Youngdale
4 * Simulate a host adapter with 2 disks attached. Do a lot of checking
5 * to make sure that we are not getting blocks mixed up, and PANIC if
6 * anything out of the ordinary is seen.
7 * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9 * This version is more generic, simulating a variable number of disk
10 * (or disk like devices) sharing a common amount of RAM. To be more
11 * realistic, the simulated devices have the transport attributes of
12 * SAS disks.
15 * For documentation see http://www.torque.net/sg/sdebug26.html
17 * D. Gilbert (dpg) work for Magneto-Optical device test [20010421]
18 * dpg: work for devfs large number of disks [20010809]
19 * forked for lk 2.5 series [20011216, 20020101]
20 * use vmalloc() more inquiry+mode_sense [20020302]
21 * add timers for delayed responses [20020721]
22 * Patrick Mansfield <patmans@us.ibm.com> max_luns+scsi_level [20021031]
23 * Mike Anderson <andmike@us.ibm.com> sysfs work [20021118]
24 * dpg: change style of boot options to "scsi_debug.num_tgts=2" and
25 * module options to "modprobe scsi_debug num_tgts=2" [20021221]
28 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/errno.h>
32 #include <linux/timer.h>
33 #include <linux/types.h>
34 #include <linux/string.h>
35 #include <linux/genhd.h>
36 #include <linux/fs.h>
37 #include <linux/init.h>
38 #include <linux/proc_fs.h>
39 #include <linux/vmalloc.h>
40 #include <linux/moduleparam.h>
41 #include <linux/scatterlist.h>
43 #include <linux/blkdev.h>
44 #include "scsi.h"
45 #include <scsi/scsi_host.h>
46 #include <scsi/scsicam.h>
48 #include <linux/stat.h>
50 #include "scsi_logging.h"
51 #include "scsi_debug.h"
53 #define SCSI_DEBUG_VERSION "1.81"
54 static const char * scsi_debug_version_date = "20070104";
56 /* Additional Sense Code (ASC) */
57 #define NO_ADDITIONAL_SENSE 0x0
58 #define LOGICAL_UNIT_NOT_READY 0x4
59 #define UNRECOVERED_READ_ERR 0x11
60 #define PARAMETER_LIST_LENGTH_ERR 0x1a
61 #define INVALID_OPCODE 0x20
62 #define ADDR_OUT_OF_RANGE 0x21
63 #define INVALID_FIELD_IN_CDB 0x24
64 #define INVALID_FIELD_IN_PARAM_LIST 0x26
65 #define POWERON_RESET 0x29
66 #define SAVING_PARAMS_UNSUP 0x39
67 #define TRANSPORT_PROBLEM 0x4b
68 #define THRESHOLD_EXCEEDED 0x5d
69 #define LOW_POWER_COND_ON 0x5e
71 /* Additional Sense Code Qualifier (ASCQ) */
72 #define ACK_NAK_TO 0x3
74 #define SDEBUG_TAGGED_QUEUING 0 /* 0 | MSG_SIMPLE_TAG | MSG_ORDERED_TAG */
76 /* Default values for driver parameters */
77 #define DEF_NUM_HOST 1
78 #define DEF_NUM_TGTS 1
79 #define DEF_MAX_LUNS 1
80 /* With these defaults, this driver will make 1 host with 1 target
81 * (id 0) containing 1 logical unit (lun 0). That is 1 device.
83 #define DEF_DELAY 1
84 #define DEF_DEV_SIZE_MB 8
85 #define DEF_EVERY_NTH 0
86 #define DEF_NUM_PARTS 0
87 #define DEF_OPTS 0
88 #define DEF_SCSI_LEVEL 5 /* INQUIRY, byte2 [5->SPC-3] */
89 #define DEF_PTYPE 0
90 #define DEF_D_SENSE 0
91 #define DEF_NO_LUN_0 0
92 #define DEF_VIRTUAL_GB 0
93 #define DEF_FAKE_RW 0
94 #define DEF_VPD_USE_HOSTNO 1
96 /* bit mask values for scsi_debug_opts */
97 #define SCSI_DEBUG_OPT_NOISE 1
98 #define SCSI_DEBUG_OPT_MEDIUM_ERR 2
99 #define SCSI_DEBUG_OPT_TIMEOUT 4
100 #define SCSI_DEBUG_OPT_RECOVERED_ERR 8
101 #define SCSI_DEBUG_OPT_TRANSPORT_ERR 16
102 /* When "every_nth" > 0 then modulo "every_nth" commands:
103 * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
104 * - a RECOVERED_ERROR is simulated on successful read and write
105 * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
106 * - a TRANSPORT_ERROR is simulated on successful read and write
107 * commands if SCSI_DEBUG_OPT_TRANSPORT_ERR is set.
109 * When "every_nth" < 0 then after "- every_nth" commands:
110 * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
111 * - a RECOVERED_ERROR is simulated on successful read and write
112 * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
113 * - a TRANSPORT_ERROR is simulated on successful read and write
114 * commands if SCSI_DEBUG_OPT_TRANSPORT_ERR is set.
115 * This will continue until some other action occurs (e.g. the user
116 * writing a new value (other than -1 or 1) to every_nth via sysfs).
119 /* when 1==SCSI_DEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
120 * sector on read commands: */
121 #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
123 /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
124 * or "peripheral device" addressing (value 0) */
125 #define SAM2_LUN_ADDRESS_METHOD 0
126 #define SAM2_WLUN_REPORT_LUNS 0xc101
128 static int scsi_debug_add_host = DEF_NUM_HOST;
129 static int scsi_debug_delay = DEF_DELAY;
130 static int scsi_debug_dev_size_mb = DEF_DEV_SIZE_MB;
131 static int scsi_debug_every_nth = DEF_EVERY_NTH;
132 static int scsi_debug_max_luns = DEF_MAX_LUNS;
133 static int scsi_debug_num_parts = DEF_NUM_PARTS;
134 static int scsi_debug_num_tgts = DEF_NUM_TGTS; /* targets per host */
135 static int scsi_debug_opts = DEF_OPTS;
136 static int scsi_debug_scsi_level = DEF_SCSI_LEVEL;
137 static int scsi_debug_ptype = DEF_PTYPE; /* SCSI peripheral type (0==disk) */
138 static int scsi_debug_dsense = DEF_D_SENSE;
139 static int scsi_debug_no_lun_0 = DEF_NO_LUN_0;
140 static int scsi_debug_virtual_gb = DEF_VIRTUAL_GB;
141 static int scsi_debug_fake_rw = DEF_FAKE_RW;
142 static int scsi_debug_vpd_use_hostno = DEF_VPD_USE_HOSTNO;
144 static int scsi_debug_cmnd_count = 0;
146 #define DEV_READONLY(TGT) (0)
147 #define DEV_REMOVEABLE(TGT) (0)
149 static unsigned int sdebug_store_size; /* in bytes */
150 static unsigned int sdebug_store_sectors;
151 static sector_t sdebug_capacity; /* in sectors */
153 /* old BIOS stuff, kernel may get rid of them but some mode sense pages
154 may still need them */
155 static int sdebug_heads; /* heads per disk */
156 static int sdebug_cylinders_per; /* cylinders per surface */
157 static int sdebug_sectors_per; /* sectors per cylinder */
159 /* default sector size is 512 bytes, 2**9 bytes */
160 #define POW2_SECT_SIZE 9
161 #define SECT_SIZE (1 << POW2_SECT_SIZE)
162 #define SECT_SIZE_PER(TGT) SECT_SIZE
164 #define SDEBUG_MAX_PARTS 4
166 #define SDEBUG_SENSE_LEN 32
168 struct sdebug_dev_info {
169 struct list_head dev_list;
170 unsigned char sense_buff[SDEBUG_SENSE_LEN]; /* weak nexus */
171 unsigned int channel;
172 unsigned int target;
173 unsigned int lun;
174 struct sdebug_host_info *sdbg_host;
175 unsigned int wlun;
176 char reset;
177 char stopped;
178 char used;
181 struct sdebug_host_info {
182 struct list_head host_list;
183 struct Scsi_Host *shost;
184 struct device dev;
185 struct list_head dev_info_list;
188 #define to_sdebug_host(d) \
189 container_of(d, struct sdebug_host_info, dev)
191 static LIST_HEAD(sdebug_host_list);
192 static DEFINE_SPINLOCK(sdebug_host_list_lock);
194 typedef void (* done_funct_t) (struct scsi_cmnd *);
196 struct sdebug_queued_cmd {
197 int in_use;
198 struct timer_list cmnd_timer;
199 done_funct_t done_funct;
200 struct scsi_cmnd * a_cmnd;
201 int scsi_result;
203 static struct sdebug_queued_cmd queued_arr[SCSI_DEBUG_CANQUEUE];
205 static struct scsi_host_template sdebug_driver_template = {
206 .proc_info = scsi_debug_proc_info,
207 .name = "SCSI DEBUG",
208 .info = scsi_debug_info,
209 .slave_alloc = scsi_debug_slave_alloc,
210 .slave_configure = scsi_debug_slave_configure,
211 .slave_destroy = scsi_debug_slave_destroy,
212 .ioctl = scsi_debug_ioctl,
213 .queuecommand = scsi_debug_queuecommand,
214 .eh_abort_handler = scsi_debug_abort,
215 .eh_bus_reset_handler = scsi_debug_bus_reset,
216 .eh_device_reset_handler = scsi_debug_device_reset,
217 .eh_host_reset_handler = scsi_debug_host_reset,
218 .bios_param = scsi_debug_biosparam,
219 .can_queue = SCSI_DEBUG_CANQUEUE,
220 .this_id = 7,
221 .sg_tablesize = 256,
222 .cmd_per_lun = 16,
223 .max_sectors = 0xffff,
224 .unchecked_isa_dma = 0,
225 <<<<<<< HEAD:drivers/scsi/scsi_debug.c
226 .use_clustering = ENABLE_CLUSTERING,
227 =======
228 .use_clustering = DISABLE_CLUSTERING,
229 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_debug.c
230 .module = THIS_MODULE,
233 static unsigned char * fake_storep; /* ramdisk storage */
235 static int num_aborts = 0;
236 static int num_dev_resets = 0;
237 static int num_bus_resets = 0;
238 static int num_host_resets = 0;
240 static DEFINE_SPINLOCK(queued_arr_lock);
241 static DEFINE_RWLOCK(atomic_rw);
243 static char sdebug_proc_name[] = "scsi_debug";
245 static int sdebug_driver_probe(struct device *);
246 static int sdebug_driver_remove(struct device *);
247 static struct bus_type pseudo_lld_bus;
249 static struct device_driver sdebug_driverfs_driver = {
250 .name = sdebug_proc_name,
251 .bus = &pseudo_lld_bus,
254 static const int check_condition_result =
255 (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
257 static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
258 0, 0, 0x2, 0x4b};
259 static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
260 0, 0, 0x0, 0x0};
262 /* function declarations */
263 static int resp_inquiry(struct scsi_cmnd * SCpnt, int target,
264 struct sdebug_dev_info * devip);
265 static int resp_requests(struct scsi_cmnd * SCpnt,
266 struct sdebug_dev_info * devip);
267 static int resp_start_stop(struct scsi_cmnd * scp,
268 struct sdebug_dev_info * devip);
269 static int resp_report_tgtpgs(struct scsi_cmnd * scp,
270 struct sdebug_dev_info * devip);
271 static int resp_readcap(struct scsi_cmnd * SCpnt,
272 struct sdebug_dev_info * devip);
273 static int resp_readcap16(struct scsi_cmnd * SCpnt,
274 struct sdebug_dev_info * devip);
275 static int resp_mode_sense(struct scsi_cmnd * scp, int target,
276 struct sdebug_dev_info * devip);
277 static int resp_mode_select(struct scsi_cmnd * scp, int mselect6,
278 struct sdebug_dev_info * devip);
279 static int resp_log_sense(struct scsi_cmnd * scp,
280 struct sdebug_dev_info * devip);
281 static int resp_read(struct scsi_cmnd * SCpnt, unsigned long long lba,
282 unsigned int num, struct sdebug_dev_info * devip);
283 static int resp_write(struct scsi_cmnd * SCpnt, unsigned long long lba,
284 unsigned int num, struct sdebug_dev_info * devip);
285 static int resp_report_luns(struct scsi_cmnd * SCpnt,
286 struct sdebug_dev_info * devip);
287 static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba,
288 unsigned int num, struct sdebug_dev_info *devip);
289 static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
290 int arr_len);
291 static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
292 int max_arr_len);
293 static void timer_intr_handler(unsigned long);
294 static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev);
295 static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
296 int asc, int asq);
297 static int check_readiness(struct scsi_cmnd * SCpnt, int reset_only,
298 struct sdebug_dev_info * devip);
299 static int schedule_resp(struct scsi_cmnd * cmnd,
300 struct sdebug_dev_info * devip,
301 done_funct_t done, int scsi_result, int delta_jiff);
302 static void __init sdebug_build_parts(unsigned char * ramp);
303 static void __init init_all_queued(void);
304 static void stop_all_queued(void);
305 static int stop_queued_cmnd(struct scsi_cmnd * cmnd);
306 static int inquiry_evpd_83(unsigned char * arr, int port_group_id,
307 int target_dev_id, int dev_id_num,
308 const char * dev_id_str, int dev_id_str_len);
309 static int inquiry_evpd_88(unsigned char * arr, int target_dev_id);
310 static int do_create_driverfs_files(void);
311 static void do_remove_driverfs_files(void);
313 static int sdebug_add_adapter(void);
314 static void sdebug_remove_adapter(void);
315 static void sdebug_max_tgts_luns(void);
317 static struct device pseudo_primary;
318 static struct bus_type pseudo_lld_bus;
320 static void get_data_transfer_info(unsigned char *cmd,
321 unsigned long long *lba, unsigned int *num)
323 int i;
325 switch (*cmd) {
326 case WRITE_16:
327 case READ_16:
328 for (*lba = 0, i = 0; i < 8; ++i) {
329 if (i > 0)
330 *lba <<= 8;
331 *lba += cmd[2 + i];
333 *num = cmd[13] + (cmd[12] << 8) +
334 (cmd[11] << 16) + (cmd[10] << 24);
335 break;
336 case WRITE_12:
337 case READ_12:
338 *lba = cmd[5] + (cmd[4] << 8) + (cmd[3] << 16) + (cmd[2] << 24);
339 *num = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24);
340 break;
341 case WRITE_10:
342 case READ_10:
343 case XDWRITEREAD_10:
344 *lba = cmd[5] + (cmd[4] << 8) + (cmd[3] << 16) + (cmd[2] << 24);
345 *num = cmd[8] + (cmd[7] << 8);
346 break;
347 case WRITE_6:
348 case READ_6:
349 *lba = cmd[3] + (cmd[2] << 8) + ((cmd[1] & 0x1f) << 16);
350 *num = (0 == cmd[4]) ? 256 : cmd[4];
351 break;
352 default:
353 break;
357 static
358 int scsi_debug_queuecommand(struct scsi_cmnd * SCpnt, done_funct_t done)
360 unsigned char *cmd = (unsigned char *) SCpnt->cmnd;
361 int len, k;
362 unsigned int num;
363 unsigned long long lba;
364 int errsts = 0;
365 int target = SCpnt->device->id;
366 struct sdebug_dev_info * devip = NULL;
367 int inj_recovered = 0;
368 int inj_transport = 0;
369 int delay_override = 0;
371 if (done == NULL)
372 return 0; /* assume mid level reprocessing command */
374 scsi_set_resid(SCpnt, 0);
375 if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmd) {
376 printk(KERN_INFO "scsi_debug: cmd ");
377 for (k = 0, len = SCpnt->cmd_len; k < len; ++k)
378 printk("%02x ", (int)cmd[k]);
379 printk("\n");
381 if(target == sdebug_driver_template.this_id) {
382 printk(KERN_INFO "scsi_debug: initiator's id used as "
383 "target!\n");
384 return schedule_resp(SCpnt, NULL, done,
385 DID_NO_CONNECT << 16, 0);
388 if ((SCpnt->device->lun >= scsi_debug_max_luns) &&
389 (SCpnt->device->lun != SAM2_WLUN_REPORT_LUNS))
390 return schedule_resp(SCpnt, NULL, done,
391 DID_NO_CONNECT << 16, 0);
392 devip = devInfoReg(SCpnt->device);
393 if (NULL == devip)
394 return schedule_resp(SCpnt, NULL, done,
395 DID_NO_CONNECT << 16, 0);
397 if ((scsi_debug_every_nth != 0) &&
398 (++scsi_debug_cmnd_count >= abs(scsi_debug_every_nth))) {
399 scsi_debug_cmnd_count = 0;
400 if (scsi_debug_every_nth < -1)
401 scsi_debug_every_nth = -1;
402 if (SCSI_DEBUG_OPT_TIMEOUT & scsi_debug_opts)
403 return 0; /* ignore command causing timeout */
404 else if (SCSI_DEBUG_OPT_RECOVERED_ERR & scsi_debug_opts)
405 inj_recovered = 1; /* to reads and writes below */
406 else if (SCSI_DEBUG_OPT_TRANSPORT_ERR & scsi_debug_opts)
407 inj_transport = 1; /* to reads and writes below */
410 if (devip->wlun) {
411 switch (*cmd) {
412 case INQUIRY:
413 case REQUEST_SENSE:
414 case TEST_UNIT_READY:
415 case REPORT_LUNS:
416 break; /* only allowable wlun commands */
417 default:
418 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
419 printk(KERN_INFO "scsi_debug: Opcode: 0x%x "
420 "not supported for wlun\n", *cmd);
421 mk_sense_buffer(devip, ILLEGAL_REQUEST,
422 INVALID_OPCODE, 0);
423 errsts = check_condition_result;
424 return schedule_resp(SCpnt, devip, done, errsts,
429 switch (*cmd) {
430 case INQUIRY: /* mandatory, ignore unit attention */
431 delay_override = 1;
432 errsts = resp_inquiry(SCpnt, target, devip);
433 break;
434 case REQUEST_SENSE: /* mandatory, ignore unit attention */
435 delay_override = 1;
436 errsts = resp_requests(SCpnt, devip);
437 break;
438 case REZERO_UNIT: /* actually this is REWIND for SSC */
439 case START_STOP:
440 errsts = resp_start_stop(SCpnt, devip);
441 break;
442 case ALLOW_MEDIUM_REMOVAL:
443 if ((errsts = check_readiness(SCpnt, 1, devip)))
444 break;
445 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
446 printk(KERN_INFO "scsi_debug: Medium removal %s\n",
447 cmd[4] ? "inhibited" : "enabled");
448 break;
449 case SEND_DIAGNOSTIC: /* mandatory */
450 errsts = check_readiness(SCpnt, 1, devip);
451 break;
452 case TEST_UNIT_READY: /* mandatory */
453 delay_override = 1;
454 errsts = check_readiness(SCpnt, 0, devip);
455 break;
456 case RESERVE:
457 errsts = check_readiness(SCpnt, 1, devip);
458 break;
459 case RESERVE_10:
460 errsts = check_readiness(SCpnt, 1, devip);
461 break;
462 case RELEASE:
463 errsts = check_readiness(SCpnt, 1, devip);
464 break;
465 case RELEASE_10:
466 errsts = check_readiness(SCpnt, 1, devip);
467 break;
468 case READ_CAPACITY:
469 errsts = resp_readcap(SCpnt, devip);
470 break;
471 case SERVICE_ACTION_IN:
472 if (SAI_READ_CAPACITY_16 != cmd[1]) {
473 mk_sense_buffer(devip, ILLEGAL_REQUEST,
474 INVALID_OPCODE, 0);
475 errsts = check_condition_result;
476 break;
478 errsts = resp_readcap16(SCpnt, devip);
479 break;
480 case MAINTENANCE_IN:
481 if (MI_REPORT_TARGET_PGS != cmd[1]) {
482 mk_sense_buffer(devip, ILLEGAL_REQUEST,
483 INVALID_OPCODE, 0);
484 errsts = check_condition_result;
485 break;
487 errsts = resp_report_tgtpgs(SCpnt, devip);
488 break;
489 case READ_16:
490 case READ_12:
491 case READ_10:
492 case READ_6:
493 if ((errsts = check_readiness(SCpnt, 0, devip)))
494 break;
495 if (scsi_debug_fake_rw)
496 break;
497 get_data_transfer_info(cmd, &lba, &num);
498 errsts = resp_read(SCpnt, lba, num, devip);
499 if (inj_recovered && (0 == errsts)) {
500 mk_sense_buffer(devip, RECOVERED_ERROR,
501 THRESHOLD_EXCEEDED, 0);
502 errsts = check_condition_result;
503 } else if (inj_transport && (0 == errsts)) {
504 mk_sense_buffer(devip, ABORTED_COMMAND,
505 TRANSPORT_PROBLEM, ACK_NAK_TO);
506 errsts = check_condition_result;
508 break;
509 case REPORT_LUNS: /* mandatory, ignore unit attention */
510 delay_override = 1;
511 errsts = resp_report_luns(SCpnt, devip);
512 break;
513 case VERIFY: /* 10 byte SBC-2 command */
514 errsts = check_readiness(SCpnt, 0, devip);
515 break;
516 case WRITE_16:
517 case WRITE_12:
518 case WRITE_10:
519 case WRITE_6:
520 if ((errsts = check_readiness(SCpnt, 0, devip)))
521 break;
522 if (scsi_debug_fake_rw)
523 break;
524 get_data_transfer_info(cmd, &lba, &num);
525 errsts = resp_write(SCpnt, lba, num, devip);
526 if (inj_recovered && (0 == errsts)) {
527 mk_sense_buffer(devip, RECOVERED_ERROR,
528 THRESHOLD_EXCEEDED, 0);
529 errsts = check_condition_result;
531 break;
532 case MODE_SENSE:
533 case MODE_SENSE_10:
534 errsts = resp_mode_sense(SCpnt, target, devip);
535 break;
536 case MODE_SELECT:
537 errsts = resp_mode_select(SCpnt, 1, devip);
538 break;
539 case MODE_SELECT_10:
540 errsts = resp_mode_select(SCpnt, 0, devip);
541 break;
542 case LOG_SENSE:
543 errsts = resp_log_sense(SCpnt, devip);
544 break;
545 case SYNCHRONIZE_CACHE:
546 delay_override = 1;
547 errsts = check_readiness(SCpnt, 0, devip);
548 break;
549 case WRITE_BUFFER:
550 errsts = check_readiness(SCpnt, 1, devip);
551 break;
552 case XDWRITEREAD_10:
553 if (!scsi_bidi_cmnd(SCpnt)) {
554 mk_sense_buffer(devip, ILLEGAL_REQUEST,
555 INVALID_FIELD_IN_CDB, 0);
556 errsts = check_condition_result;
557 break;
560 errsts = check_readiness(SCpnt, 0, devip);
561 if (errsts)
562 break;
563 if (scsi_debug_fake_rw)
564 break;
565 get_data_transfer_info(cmd, &lba, &num);
566 errsts = resp_read(SCpnt, lba, num, devip);
567 if (errsts)
568 break;
569 errsts = resp_write(SCpnt, lba, num, devip);
570 if (errsts)
571 break;
572 errsts = resp_xdwriteread(SCpnt, lba, num, devip);
573 break;
574 default:
575 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
576 printk(KERN_INFO "scsi_debug: Opcode: 0x%x not "
577 "supported\n", *cmd);
578 if ((errsts = check_readiness(SCpnt, 1, devip)))
579 break; /* Unit attention takes precedence */
580 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
581 errsts = check_condition_result;
582 break;
584 return schedule_resp(SCpnt, devip, done, errsts,
585 (delay_override ? 0 : scsi_debug_delay));
588 static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
590 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
591 printk(KERN_INFO "scsi_debug: ioctl: cmd=0x%x\n", cmd);
593 return -EINVAL;
594 /* return -ENOTTY; // correct return but upsets fdisk */
597 static int check_readiness(struct scsi_cmnd * SCpnt, int reset_only,
598 struct sdebug_dev_info * devip)
600 if (devip->reset) {
601 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
602 printk(KERN_INFO "scsi_debug: Reporting Unit "
603 "attention: power on reset\n");
604 devip->reset = 0;
605 mk_sense_buffer(devip, UNIT_ATTENTION, POWERON_RESET, 0);
606 return check_condition_result;
608 if ((0 == reset_only) && devip->stopped) {
609 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
610 printk(KERN_INFO "scsi_debug: Reporting Not "
611 "ready: initializing command required\n");
612 mk_sense_buffer(devip, NOT_READY, LOGICAL_UNIT_NOT_READY,
613 0x2);
614 return check_condition_result;
616 return 0;
619 /* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */
620 static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
621 int arr_len)
623 int k, req_len, act_len, len, active;
624 void * kaddr;
625 void * kaddr_off;
626 struct scatterlist *sg;
627 struct scsi_data_buffer *sdb = scsi_in(scp);
629 if (!sdb->length)
630 return 0;
631 if (!sdb->table.sgl)
632 return (DID_ERROR << 16);
633 if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE))
634 return (DID_ERROR << 16);
635 active = 1;
636 req_len = act_len = 0;
637 for_each_sg(sdb->table.sgl, sg, sdb->table.nents, k) {
638 if (active) {
639 kaddr = (unsigned char *)
640 kmap_atomic(sg_page(sg), KM_USER0);
641 if (NULL == kaddr)
642 return (DID_ERROR << 16);
643 kaddr_off = (unsigned char *)kaddr + sg->offset;
644 len = sg->length;
645 if ((req_len + len) > arr_len) {
646 active = 0;
647 len = arr_len - req_len;
649 memcpy(kaddr_off, arr + req_len, len);
650 kunmap_atomic(kaddr, KM_USER0);
651 act_len += len;
653 req_len += sg->length;
655 if (sdb->resid)
656 sdb->resid -= act_len;
657 else
658 sdb->resid = req_len - act_len;
659 return 0;
662 /* Returns number of bytes fetched into 'arr' or -1 if error. */
663 static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
664 int max_arr_len)
666 int k, req_len, len, fin;
667 void * kaddr;
668 void * kaddr_off;
669 struct scatterlist * sg;
671 if (0 == scsi_bufflen(scp))
672 return 0;
673 if (NULL == scsi_sglist(scp))
674 return -1;
675 if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_TO_DEVICE))
676 return -1;
677 req_len = fin = 0;
678 scsi_for_each_sg(scp, sg, scsi_sg_count(scp), k) {
679 kaddr = (unsigned char *)kmap_atomic(sg_page(sg), KM_USER0);
680 if (NULL == kaddr)
681 return -1;
682 kaddr_off = (unsigned char *)kaddr + sg->offset;
683 len = sg->length;
684 if ((req_len + len) > max_arr_len) {
685 len = max_arr_len - req_len;
686 fin = 1;
688 memcpy(arr + req_len, kaddr_off, len);
689 kunmap_atomic(kaddr, KM_USER0);
690 if (fin)
691 return req_len + len;
692 req_len += sg->length;
694 return req_len;
698 static const char * inq_vendor_id = "Linux ";
699 static const char * inq_product_id = "scsi_debug ";
700 static const char * inq_product_rev = "0004";
702 static int inquiry_evpd_83(unsigned char * arr, int port_group_id,
703 int target_dev_id, int dev_id_num,
704 const char * dev_id_str,
705 int dev_id_str_len)
707 int num, port_a;
708 char b[32];
710 port_a = target_dev_id + 1;
711 /* T10 vendor identifier field format (faked) */
712 arr[0] = 0x2; /* ASCII */
713 arr[1] = 0x1;
714 arr[2] = 0x0;
715 memcpy(&arr[4], inq_vendor_id, 8);
716 memcpy(&arr[12], inq_product_id, 16);
717 memcpy(&arr[28], dev_id_str, dev_id_str_len);
718 num = 8 + 16 + dev_id_str_len;
719 arr[3] = num;
720 num += 4;
721 if (dev_id_num >= 0) {
722 /* NAA-5, Logical unit identifier (binary) */
723 arr[num++] = 0x1; /* binary (not necessarily sas) */
724 arr[num++] = 0x3; /* PIV=0, lu, naa */
725 arr[num++] = 0x0;
726 arr[num++] = 0x8;
727 arr[num++] = 0x53; /* naa-5 ieee company id=0x333333 (fake) */
728 arr[num++] = 0x33;
729 arr[num++] = 0x33;
730 arr[num++] = 0x30;
731 arr[num++] = (dev_id_num >> 24);
732 arr[num++] = (dev_id_num >> 16) & 0xff;
733 arr[num++] = (dev_id_num >> 8) & 0xff;
734 arr[num++] = dev_id_num & 0xff;
735 /* Target relative port number */
736 arr[num++] = 0x61; /* proto=sas, binary */
737 arr[num++] = 0x94; /* PIV=1, target port, rel port */
738 arr[num++] = 0x0; /* reserved */
739 arr[num++] = 0x4; /* length */
740 arr[num++] = 0x0; /* reserved */
741 arr[num++] = 0x0; /* reserved */
742 arr[num++] = 0x0;
743 arr[num++] = 0x1; /* relative port A */
745 /* NAA-5, Target port identifier */
746 arr[num++] = 0x61; /* proto=sas, binary */
747 arr[num++] = 0x93; /* piv=1, target port, naa */
748 arr[num++] = 0x0;
749 arr[num++] = 0x8;
750 arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
751 arr[num++] = 0x22;
752 arr[num++] = 0x22;
753 arr[num++] = 0x20;
754 arr[num++] = (port_a >> 24);
755 arr[num++] = (port_a >> 16) & 0xff;
756 arr[num++] = (port_a >> 8) & 0xff;
757 arr[num++] = port_a & 0xff;
758 /* NAA-5, Target port group identifier */
759 arr[num++] = 0x61; /* proto=sas, binary */
760 arr[num++] = 0x95; /* piv=1, target port group id */
761 arr[num++] = 0x0;
762 arr[num++] = 0x4;
763 arr[num++] = 0;
764 arr[num++] = 0;
765 arr[num++] = (port_group_id >> 8) & 0xff;
766 arr[num++] = port_group_id & 0xff;
767 /* NAA-5, Target device identifier */
768 arr[num++] = 0x61; /* proto=sas, binary */
769 arr[num++] = 0xa3; /* piv=1, target device, naa */
770 arr[num++] = 0x0;
771 arr[num++] = 0x8;
772 arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
773 arr[num++] = 0x22;
774 arr[num++] = 0x22;
775 arr[num++] = 0x20;
776 arr[num++] = (target_dev_id >> 24);
777 arr[num++] = (target_dev_id >> 16) & 0xff;
778 arr[num++] = (target_dev_id >> 8) & 0xff;
779 arr[num++] = target_dev_id & 0xff;
780 /* SCSI name string: Target device identifier */
781 arr[num++] = 0x63; /* proto=sas, UTF-8 */
782 arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */
783 arr[num++] = 0x0;
784 arr[num++] = 24;
785 memcpy(arr + num, "naa.52222220", 12);
786 num += 12;
787 snprintf(b, sizeof(b), "%08X", target_dev_id);
788 memcpy(arr + num, b, 8);
789 num += 8;
790 memset(arr + num, 0, 4);
791 num += 4;
792 return num;
796 static unsigned char vpd84_data[] = {
797 /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
798 0x22,0x22,0x22,0x0,0xbb,0x1,
799 0x22,0x22,0x22,0x0,0xbb,0x2,
802 static int inquiry_evpd_84(unsigned char * arr)
804 memcpy(arr, vpd84_data, sizeof(vpd84_data));
805 return sizeof(vpd84_data);
808 static int inquiry_evpd_85(unsigned char * arr)
810 int num = 0;
811 const char * na1 = "https://www.kernel.org/config";
812 const char * na2 = "http://www.kernel.org/log";
813 int plen, olen;
815 arr[num++] = 0x1; /* lu, storage config */
816 arr[num++] = 0x0; /* reserved */
817 arr[num++] = 0x0;
818 olen = strlen(na1);
819 plen = olen + 1;
820 if (plen % 4)
821 plen = ((plen / 4) + 1) * 4;
822 arr[num++] = plen; /* length, null termianted, padded */
823 memcpy(arr + num, na1, olen);
824 memset(arr + num + olen, 0, plen - olen);
825 num += plen;
827 arr[num++] = 0x4; /* lu, logging */
828 arr[num++] = 0x0; /* reserved */
829 arr[num++] = 0x0;
830 olen = strlen(na2);
831 plen = olen + 1;
832 if (plen % 4)
833 plen = ((plen / 4) + 1) * 4;
834 arr[num++] = plen; /* length, null terminated, padded */
835 memcpy(arr + num, na2, olen);
836 memset(arr + num + olen, 0, plen - olen);
837 num += plen;
839 return num;
842 /* SCSI ports VPD page */
843 static int inquiry_evpd_88(unsigned char * arr, int target_dev_id)
845 int num = 0;
846 int port_a, port_b;
848 port_a = target_dev_id + 1;
849 port_b = port_a + 1;
850 arr[num++] = 0x0; /* reserved */
851 arr[num++] = 0x0; /* reserved */
852 arr[num++] = 0x0;
853 arr[num++] = 0x1; /* relative port 1 (primary) */
854 memset(arr + num, 0, 6);
855 num += 6;
856 arr[num++] = 0x0;
857 arr[num++] = 12; /* length tp descriptor */
858 /* naa-5 target port identifier (A) */
859 arr[num++] = 0x61; /* proto=sas, binary */
860 arr[num++] = 0x93; /* PIV=1, target port, NAA */
861 arr[num++] = 0x0; /* reserved */
862 arr[num++] = 0x8; /* length */
863 arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
864 arr[num++] = 0x22;
865 arr[num++] = 0x22;
866 arr[num++] = 0x20;
867 arr[num++] = (port_a >> 24);
868 arr[num++] = (port_a >> 16) & 0xff;
869 arr[num++] = (port_a >> 8) & 0xff;
870 arr[num++] = port_a & 0xff;
872 arr[num++] = 0x0; /* reserved */
873 arr[num++] = 0x0; /* reserved */
874 arr[num++] = 0x0;
875 arr[num++] = 0x2; /* relative port 2 (secondary) */
876 memset(arr + num, 0, 6);
877 num += 6;
878 arr[num++] = 0x0;
879 arr[num++] = 12; /* length tp descriptor */
880 /* naa-5 target port identifier (B) */
881 arr[num++] = 0x61; /* proto=sas, binary */
882 arr[num++] = 0x93; /* PIV=1, target port, NAA */
883 arr[num++] = 0x0; /* reserved */
884 arr[num++] = 0x8; /* length */
885 arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
886 arr[num++] = 0x22;
887 arr[num++] = 0x22;
888 arr[num++] = 0x20;
889 arr[num++] = (port_b >> 24);
890 arr[num++] = (port_b >> 16) & 0xff;
891 arr[num++] = (port_b >> 8) & 0xff;
892 arr[num++] = port_b & 0xff;
894 return num;
898 static unsigned char vpd89_data[] = {
899 /* from 4th byte */ 0,0,0,0,
900 'l','i','n','u','x',' ',' ',' ',
901 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
902 '1','2','3','4',
903 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
904 0xec,0,0,0,
905 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
906 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
907 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
908 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
909 0x53,0x41,
910 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
911 0x20,0x20,
912 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
913 0x10,0x80,
914 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
915 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
916 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
917 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
918 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
919 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
920 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0,
921 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
922 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
923 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
924 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
925 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
926 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
927 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0,
928 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
929 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
930 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
931 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
932 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
933 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
934 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
935 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
936 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
937 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
938 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
939 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
942 static int inquiry_evpd_89(unsigned char * arr)
944 memcpy(arr, vpd89_data, sizeof(vpd89_data));
945 return sizeof(vpd89_data);
949 static unsigned char vpdb0_data[] = {
950 /* from 4th byte */ 0,0,0,4,
951 0,0,0x4,0,
952 0,0,0,64,
955 static int inquiry_evpd_b0(unsigned char * arr)
957 memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
958 if (sdebug_store_sectors > 0x400) {
959 arr[4] = (sdebug_store_sectors >> 24) & 0xff;
960 arr[5] = (sdebug_store_sectors >> 16) & 0xff;
961 arr[6] = (sdebug_store_sectors >> 8) & 0xff;
962 arr[7] = sdebug_store_sectors & 0xff;
964 return sizeof(vpdb0_data);
968 #define SDEBUG_LONG_INQ_SZ 96
969 #define SDEBUG_MAX_INQ_ARR_SZ 584
971 static int resp_inquiry(struct scsi_cmnd * scp, int target,
972 struct sdebug_dev_info * devip)
974 unsigned char pq_pdt;
975 unsigned char * arr;
976 unsigned char *cmd = (unsigned char *)scp->cmnd;
977 int alloc_len, n, ret;
979 alloc_len = (cmd[3] << 8) + cmd[4];
980 arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC);
981 if (! arr)
982 return DID_REQUEUE << 16;
983 if (devip->wlun)
984 pq_pdt = 0x1e; /* present, wlun */
985 else if (scsi_debug_no_lun_0 && (0 == devip->lun))
986 pq_pdt = 0x7f; /* not present, no device type */
987 else
988 pq_pdt = (scsi_debug_ptype & 0x1f);
989 arr[0] = pq_pdt;
990 if (0x2 & cmd[1]) { /* CMDDT bit set */
991 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
993 kfree(arr);
994 return check_condition_result;
995 } else if (0x1 & cmd[1]) { /* EVPD bit set */
996 int lu_id_num, port_group_id, target_dev_id, len;
997 char lu_id_str[6];
998 int host_no = devip->sdbg_host->shost->host_no;
1000 port_group_id = (((host_no + 1) & 0x7f) << 8) +
1001 (devip->channel & 0x7f);
1002 if (0 == scsi_debug_vpd_use_hostno)
1003 host_no = 0;
1004 lu_id_num = devip->wlun ? -1 : (((host_no + 1) * 2000) +
1005 (devip->target * 1000) + devip->lun);
1006 target_dev_id = ((host_no + 1) * 2000) +
1007 (devip->target * 1000) - 3;
1008 len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
1009 if (0 == cmd[2]) { /* supported vital product data pages */
1010 arr[1] = cmd[2]; /*sanity */
1011 n = 4;
1012 arr[n++] = 0x0; /* this page */
1013 arr[n++] = 0x80; /* unit serial number */
1014 arr[n++] = 0x83; /* device identification */
1015 arr[n++] = 0x84; /* software interface ident. */
1016 arr[n++] = 0x85; /* management network addresses */
1017 arr[n++] = 0x86; /* extended inquiry */
1018 arr[n++] = 0x87; /* mode page policy */
1019 arr[n++] = 0x88; /* SCSI ports */
1020 arr[n++] = 0x89; /* ATA information */
1021 arr[n++] = 0xb0; /* Block limits (SBC) */
1022 arr[3] = n - 4; /* number of supported VPD pages */
1023 } else if (0x80 == cmd[2]) { /* unit serial number */
1024 arr[1] = cmd[2]; /*sanity */
1025 arr[3] = len;
1026 memcpy(&arr[4], lu_id_str, len);
1027 } else if (0x83 == cmd[2]) { /* device identification */
1028 arr[1] = cmd[2]; /*sanity */
1029 arr[3] = inquiry_evpd_83(&arr[4], port_group_id,
1030 target_dev_id, lu_id_num,
1031 lu_id_str, len);
1032 } else if (0x84 == cmd[2]) { /* Software interface ident. */
1033 arr[1] = cmd[2]; /*sanity */
1034 arr[3] = inquiry_evpd_84(&arr[4]);
1035 } else if (0x85 == cmd[2]) { /* Management network addresses */
1036 arr[1] = cmd[2]; /*sanity */
1037 arr[3] = inquiry_evpd_85(&arr[4]);
1038 } else if (0x86 == cmd[2]) { /* extended inquiry */
1039 arr[1] = cmd[2]; /*sanity */
1040 arr[3] = 0x3c; /* number of following entries */
1041 arr[4] = 0x0; /* no protection stuff */
1042 arr[5] = 0x7; /* head of q, ordered + simple q's */
1043 } else if (0x87 == cmd[2]) { /* mode page policy */
1044 arr[1] = cmd[2]; /*sanity */
1045 arr[3] = 0x8; /* number of following entries */
1046 arr[4] = 0x2; /* disconnect-reconnect mp */
1047 arr[6] = 0x80; /* mlus, shared */
1048 arr[8] = 0x18; /* protocol specific lu */
1049 arr[10] = 0x82; /* mlus, per initiator port */
1050 } else if (0x88 == cmd[2]) { /* SCSI Ports */
1051 arr[1] = cmd[2]; /*sanity */
1052 arr[3] = inquiry_evpd_88(&arr[4], target_dev_id);
1053 } else if (0x89 == cmd[2]) { /* ATA information */
1054 arr[1] = cmd[2]; /*sanity */
1055 n = inquiry_evpd_89(&arr[4]);
1056 arr[2] = (n >> 8);
1057 arr[3] = (n & 0xff);
1058 } else if (0xb0 == cmd[2]) { /* Block limits (SBC) */
1059 arr[1] = cmd[2]; /*sanity */
1060 arr[3] = inquiry_evpd_b0(&arr[4]);
1061 } else {
1062 /* Illegal request, invalid field in cdb */
1063 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1064 INVALID_FIELD_IN_CDB, 0);
1065 kfree(arr);
1066 return check_condition_result;
1068 len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
1069 ret = fill_from_dev_buffer(scp, arr,
1070 min(len, SDEBUG_MAX_INQ_ARR_SZ));
1071 kfree(arr);
1072 return ret;
1074 /* drops through here for a standard inquiry */
1075 arr[1] = DEV_REMOVEABLE(target) ? 0x80 : 0; /* Removable disk */
1076 arr[2] = scsi_debug_scsi_level;
1077 arr[3] = 2; /* response_data_format==2 */
1078 arr[4] = SDEBUG_LONG_INQ_SZ - 5;
1079 if (0 == scsi_debug_vpd_use_hostno)
1080 arr[5] = 0x10; /* claim: implicit TGPS */
1081 arr[6] = 0x10; /* claim: MultiP */
1082 /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
1083 arr[7] = 0xa; /* claim: LINKED + CMDQUE */
1084 memcpy(&arr[8], inq_vendor_id, 8);
1085 memcpy(&arr[16], inq_product_id, 16);
1086 memcpy(&arr[32], inq_product_rev, 4);
1087 /* version descriptors (2 bytes each) follow */
1088 arr[58] = 0x0; arr[59] = 0x77; /* SAM-3 ANSI */
1089 arr[60] = 0x3; arr[61] = 0x14; /* SPC-3 ANSI */
1090 n = 62;
1091 if (scsi_debug_ptype == 0) {
1092 arr[n++] = 0x3; arr[n++] = 0x3d; /* SBC-2 ANSI */
1093 } else if (scsi_debug_ptype == 1) {
1094 arr[n++] = 0x3; arr[n++] = 0x60; /* SSC-2 no version */
1096 arr[n++] = 0xc; arr[n++] = 0xf; /* SAS-1.1 rev 10 */
1097 ret = fill_from_dev_buffer(scp, arr,
1098 min(alloc_len, SDEBUG_LONG_INQ_SZ));
1099 kfree(arr);
1100 return ret;
1103 static int resp_requests(struct scsi_cmnd * scp,
1104 struct sdebug_dev_info * devip)
1106 unsigned char * sbuff;
1107 unsigned char *cmd = (unsigned char *)scp->cmnd;
1108 unsigned char arr[SDEBUG_SENSE_LEN];
1109 int want_dsense;
1110 int len = 18;
1112 memset(arr, 0, sizeof(arr));
1113 if (devip->reset == 1)
1114 mk_sense_buffer(devip, 0, NO_ADDITIONAL_SENSE, 0);
1115 want_dsense = !!(cmd[1] & 1) || scsi_debug_dsense;
1116 sbuff = devip->sense_buff;
1117 if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
1118 if (want_dsense) {
1119 arr[0] = 0x72;
1120 arr[1] = 0x0; /* NO_SENSE in sense_key */
1121 arr[2] = THRESHOLD_EXCEEDED;
1122 arr[3] = 0xff; /* TEST set and MRIE==6 */
1123 } else {
1124 arr[0] = 0x70;
1125 arr[2] = 0x0; /* NO_SENSE in sense_key */
1126 arr[7] = 0xa; /* 18 byte sense buffer */
1127 arr[12] = THRESHOLD_EXCEEDED;
1128 arr[13] = 0xff; /* TEST set and MRIE==6 */
1130 } else {
1131 memcpy(arr, sbuff, SDEBUG_SENSE_LEN);
1132 if ((cmd[1] & 1) && (! scsi_debug_dsense)) {
1133 /* DESC bit set and sense_buff in fixed format */
1134 memset(arr, 0, sizeof(arr));
1135 arr[0] = 0x72;
1136 arr[1] = sbuff[2]; /* sense key */
1137 arr[2] = sbuff[12]; /* asc */
1138 arr[3] = sbuff[13]; /* ascq */
1139 len = 8;
1142 mk_sense_buffer(devip, 0, NO_ADDITIONAL_SENSE, 0);
1143 return fill_from_dev_buffer(scp, arr, len);
1146 static int resp_start_stop(struct scsi_cmnd * scp,
1147 struct sdebug_dev_info * devip)
1149 unsigned char *cmd = (unsigned char *)scp->cmnd;
1150 int power_cond, errsts, start;
1152 if ((errsts = check_readiness(scp, 1, devip)))
1153 return errsts;
1154 power_cond = (cmd[4] & 0xf0) >> 4;
1155 if (power_cond) {
1156 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
1158 return check_condition_result;
1160 start = cmd[4] & 1;
1161 if (start == devip->stopped)
1162 devip->stopped = !start;
1163 return 0;
1166 #define SDEBUG_READCAP_ARR_SZ 8
1167 static int resp_readcap(struct scsi_cmnd * scp,
1168 struct sdebug_dev_info * devip)
1170 unsigned char arr[SDEBUG_READCAP_ARR_SZ];
1171 unsigned int capac;
1172 int errsts;
1174 if ((errsts = check_readiness(scp, 1, devip)))
1175 return errsts;
1176 /* following just in case virtual_gb changed */
1177 if (scsi_debug_virtual_gb > 0) {
1178 sdebug_capacity = 2048 * 1024;
1179 sdebug_capacity *= scsi_debug_virtual_gb;
1180 } else
1181 sdebug_capacity = sdebug_store_sectors;
1182 memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
1183 if (sdebug_capacity < 0xffffffff) {
1184 capac = (unsigned int)sdebug_capacity - 1;
1185 arr[0] = (capac >> 24);
1186 arr[1] = (capac >> 16) & 0xff;
1187 arr[2] = (capac >> 8) & 0xff;
1188 arr[3] = capac & 0xff;
1189 } else {
1190 arr[0] = 0xff;
1191 arr[1] = 0xff;
1192 arr[2] = 0xff;
1193 arr[3] = 0xff;
1195 arr[6] = (SECT_SIZE_PER(target) >> 8) & 0xff;
1196 arr[7] = SECT_SIZE_PER(target) & 0xff;
1197 return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
1200 #define SDEBUG_READCAP16_ARR_SZ 32
1201 static int resp_readcap16(struct scsi_cmnd * scp,
1202 struct sdebug_dev_info * devip)
1204 unsigned char *cmd = (unsigned char *)scp->cmnd;
1205 unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
1206 unsigned long long capac;
1207 int errsts, k, alloc_len;
1209 if ((errsts = check_readiness(scp, 1, devip)))
1210 return errsts;
1211 alloc_len = ((cmd[10] << 24) + (cmd[11] << 16) + (cmd[12] << 8)
1212 + cmd[13]);
1213 /* following just in case virtual_gb changed */
1214 if (scsi_debug_virtual_gb > 0) {
1215 sdebug_capacity = 2048 * 1024;
1216 sdebug_capacity *= scsi_debug_virtual_gb;
1217 } else
1218 sdebug_capacity = sdebug_store_sectors;
1219 memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
1220 capac = sdebug_capacity - 1;
1221 for (k = 0; k < 8; ++k, capac >>= 8)
1222 arr[7 - k] = capac & 0xff;
1223 arr[8] = (SECT_SIZE_PER(target) >> 24) & 0xff;
1224 arr[9] = (SECT_SIZE_PER(target) >> 16) & 0xff;
1225 arr[10] = (SECT_SIZE_PER(target) >> 8) & 0xff;
1226 arr[11] = SECT_SIZE_PER(target) & 0xff;
1227 return fill_from_dev_buffer(scp, arr,
1228 min(alloc_len, SDEBUG_READCAP16_ARR_SZ));
1231 #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412
1233 static int resp_report_tgtpgs(struct scsi_cmnd * scp,
1234 struct sdebug_dev_info * devip)
1236 unsigned char *cmd = (unsigned char *)scp->cmnd;
1237 unsigned char * arr;
1238 int host_no = devip->sdbg_host->shost->host_no;
1239 int n, ret, alen, rlen;
1240 int port_group_a, port_group_b, port_a, port_b;
1242 alen = ((cmd[6] << 24) + (cmd[7] << 16) + (cmd[8] << 8)
1243 + cmd[9]);
1245 arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC);
1246 if (! arr)
1247 return DID_REQUEUE << 16;
1249 * EVPD page 0x88 states we have two ports, one
1250 * real and a fake port with no device connected.
1251 * So we create two port groups with one port each
1252 * and set the group with port B to unavailable.
1254 port_a = 0x1; /* relative port A */
1255 port_b = 0x2; /* relative port B */
1256 port_group_a = (((host_no + 1) & 0x7f) << 8) +
1257 (devip->channel & 0x7f);
1258 port_group_b = (((host_no + 1) & 0x7f) << 8) +
1259 (devip->channel & 0x7f) + 0x80;
1262 * The asymmetric access state is cycled according to the host_id.
1264 n = 4;
1265 if (0 == scsi_debug_vpd_use_hostno) {
1266 arr[n++] = host_no % 3; /* Asymm access state */
1267 arr[n++] = 0x0F; /* claim: all states are supported */
1268 } else {
1269 arr[n++] = 0x0; /* Active/Optimized path */
1270 arr[n++] = 0x01; /* claim: only support active/optimized paths */
1272 arr[n++] = (port_group_a >> 8) & 0xff;
1273 arr[n++] = port_group_a & 0xff;
1274 arr[n++] = 0; /* Reserved */
1275 arr[n++] = 0; /* Status code */
1276 arr[n++] = 0; /* Vendor unique */
1277 arr[n++] = 0x1; /* One port per group */
1278 arr[n++] = 0; /* Reserved */
1279 arr[n++] = 0; /* Reserved */
1280 arr[n++] = (port_a >> 8) & 0xff;
1281 arr[n++] = port_a & 0xff;
1282 arr[n++] = 3; /* Port unavailable */
1283 arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */
1284 arr[n++] = (port_group_b >> 8) & 0xff;
1285 arr[n++] = port_group_b & 0xff;
1286 arr[n++] = 0; /* Reserved */
1287 arr[n++] = 0; /* Status code */
1288 arr[n++] = 0; /* Vendor unique */
1289 arr[n++] = 0x1; /* One port per group */
1290 arr[n++] = 0; /* Reserved */
1291 arr[n++] = 0; /* Reserved */
1292 arr[n++] = (port_b >> 8) & 0xff;
1293 arr[n++] = port_b & 0xff;
1295 rlen = n - 4;
1296 arr[0] = (rlen >> 24) & 0xff;
1297 arr[1] = (rlen >> 16) & 0xff;
1298 arr[2] = (rlen >> 8) & 0xff;
1299 arr[3] = rlen & 0xff;
1302 * Return the smallest value of either
1303 * - The allocated length
1304 * - The constructed command length
1305 * - The maximum array size
1307 rlen = min(alen,n);
1308 ret = fill_from_dev_buffer(scp, arr,
1309 min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ));
1310 kfree(arr);
1311 return ret;
1314 /* <<Following mode page info copied from ST318451LW>> */
1316 static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target)
1317 { /* Read-Write Error Recovery page for mode_sense */
1318 unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
1319 5, 0, 0xff, 0xff};
1321 memcpy(p, err_recov_pg, sizeof(err_recov_pg));
1322 if (1 == pcontrol)
1323 memset(p + 2, 0, sizeof(err_recov_pg) - 2);
1324 return sizeof(err_recov_pg);
1327 static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target)
1328 { /* Disconnect-Reconnect page for mode_sense */
1329 unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
1330 0, 0, 0, 0, 0, 0, 0, 0};
1332 memcpy(p, disconnect_pg, sizeof(disconnect_pg));
1333 if (1 == pcontrol)
1334 memset(p + 2, 0, sizeof(disconnect_pg) - 2);
1335 return sizeof(disconnect_pg);
1338 static int resp_format_pg(unsigned char * p, int pcontrol, int target)
1339 { /* Format device page for mode_sense */
1340 unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
1341 0, 0, 0, 0, 0, 0, 0, 0,
1342 0, 0, 0, 0, 0x40, 0, 0, 0};
1344 memcpy(p, format_pg, sizeof(format_pg));
1345 p[10] = (sdebug_sectors_per >> 8) & 0xff;
1346 p[11] = sdebug_sectors_per & 0xff;
1347 p[12] = (SECT_SIZE >> 8) & 0xff;
1348 p[13] = SECT_SIZE & 0xff;
1349 if (DEV_REMOVEABLE(target))
1350 p[20] |= 0x20; /* should agree with INQUIRY */
1351 if (1 == pcontrol)
1352 memset(p + 2, 0, sizeof(format_pg) - 2);
1353 return sizeof(format_pg);
1356 static int resp_caching_pg(unsigned char * p, int pcontrol, int target)
1357 { /* Caching page for mode_sense */
1358 unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
1359 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
1361 memcpy(p, caching_pg, sizeof(caching_pg));
1362 if (1 == pcontrol)
1363 memset(p + 2, 0, sizeof(caching_pg) - 2);
1364 return sizeof(caching_pg);
1367 static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target)
1368 { /* Control mode page for mode_sense */
1369 unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
1370 0, 0, 0, 0};
1371 unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
1372 0, 0, 0x2, 0x4b};
1374 if (scsi_debug_dsense)
1375 ctrl_m_pg[2] |= 0x4;
1376 else
1377 ctrl_m_pg[2] &= ~0x4;
1378 memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
1379 if (1 == pcontrol)
1380 memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
1381 else if (2 == pcontrol)
1382 memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
1383 return sizeof(ctrl_m_pg);
1387 static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target)
1388 { /* Informational Exceptions control mode page for mode_sense */
1389 unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
1390 0, 0, 0x0, 0x0};
1391 unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
1392 0, 0, 0x0, 0x0};
1394 memcpy(p, iec_m_pg, sizeof(iec_m_pg));
1395 if (1 == pcontrol)
1396 memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
1397 else if (2 == pcontrol)
1398 memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
1399 return sizeof(iec_m_pg);
1402 static int resp_sas_sf_m_pg(unsigned char * p, int pcontrol, int target)
1403 { /* SAS SSP mode page - short format for mode_sense */
1404 unsigned char sas_sf_m_pg[] = {0x19, 0x6,
1405 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0};
1407 memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
1408 if (1 == pcontrol)
1409 memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
1410 return sizeof(sas_sf_m_pg);
1414 static int resp_sas_pcd_m_spg(unsigned char * p, int pcontrol, int target,
1415 int target_dev_id)
1416 { /* SAS phy control and discover mode page for mode_sense */
1417 unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
1418 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
1419 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
1420 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
1421 0x2, 0, 0, 0, 0, 0, 0, 0,
1422 0x88, 0x99, 0, 0, 0, 0, 0, 0,
1423 0, 0, 0, 0, 0, 0, 0, 0,
1424 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
1425 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
1426 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
1427 0x3, 0, 0, 0, 0, 0, 0, 0,
1428 0x88, 0x99, 0, 0, 0, 0, 0, 0,
1429 0, 0, 0, 0, 0, 0, 0, 0,
1431 int port_a, port_b;
1433 port_a = target_dev_id + 1;
1434 port_b = port_a + 1;
1435 memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
1436 p[20] = (port_a >> 24);
1437 p[21] = (port_a >> 16) & 0xff;
1438 p[22] = (port_a >> 8) & 0xff;
1439 p[23] = port_a & 0xff;
1440 p[48 + 20] = (port_b >> 24);
1441 p[48 + 21] = (port_b >> 16) & 0xff;
1442 p[48 + 22] = (port_b >> 8) & 0xff;
1443 p[48 + 23] = port_b & 0xff;
1444 if (1 == pcontrol)
1445 memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
1446 return sizeof(sas_pcd_m_pg);
1449 static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol)
1450 { /* SAS SSP shared protocol specific port mode subpage */
1451 unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
1452 0, 0, 0, 0, 0, 0, 0, 0,
1455 memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
1456 if (1 == pcontrol)
1457 memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
1458 return sizeof(sas_sha_m_pg);
1461 #define SDEBUG_MAX_MSENSE_SZ 256
1463 static int resp_mode_sense(struct scsi_cmnd * scp, int target,
1464 struct sdebug_dev_info * devip)
1466 unsigned char dbd, llbaa;
1467 int pcontrol, pcode, subpcode, bd_len;
1468 unsigned char dev_spec;
1469 int k, alloc_len, msense_6, offset, len, errsts, target_dev_id;
1470 unsigned char * ap;
1471 unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
1472 unsigned char *cmd = (unsigned char *)scp->cmnd;
1474 if ((errsts = check_readiness(scp, 1, devip)))
1475 return errsts;
1476 dbd = !!(cmd[1] & 0x8);
1477 pcontrol = (cmd[2] & 0xc0) >> 6;
1478 pcode = cmd[2] & 0x3f;
1479 subpcode = cmd[3];
1480 msense_6 = (MODE_SENSE == cmd[0]);
1481 llbaa = msense_6 ? 0 : !!(cmd[1] & 0x10);
1482 if ((0 == scsi_debug_ptype) && (0 == dbd))
1483 bd_len = llbaa ? 16 : 8;
1484 else
1485 bd_len = 0;
1486 alloc_len = msense_6 ? cmd[4] : ((cmd[7] << 8) | cmd[8]);
1487 memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
1488 if (0x3 == pcontrol) { /* Saving values not supported */
1489 mk_sense_buffer(devip, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP,
1491 return check_condition_result;
1493 target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
1494 (devip->target * 1000) - 3;
1495 /* set DPOFUA bit for disks */
1496 if (0 == scsi_debug_ptype)
1497 dev_spec = (DEV_READONLY(target) ? 0x80 : 0x0) | 0x10;
1498 else
1499 dev_spec = 0x0;
1500 if (msense_6) {
1501 arr[2] = dev_spec;
1502 arr[3] = bd_len;
1503 offset = 4;
1504 } else {
1505 arr[3] = dev_spec;
1506 if (16 == bd_len)
1507 arr[4] = 0x1; /* set LONGLBA bit */
1508 arr[7] = bd_len; /* assume 255 or less */
1509 offset = 8;
1511 ap = arr + offset;
1512 if ((bd_len > 0) && (0 == sdebug_capacity)) {
1513 if (scsi_debug_virtual_gb > 0) {
1514 sdebug_capacity = 2048 * 1024;
1515 sdebug_capacity *= scsi_debug_virtual_gb;
1516 } else
1517 sdebug_capacity = sdebug_store_sectors;
1519 if (8 == bd_len) {
1520 if (sdebug_capacity > 0xfffffffe) {
1521 ap[0] = 0xff;
1522 ap[1] = 0xff;
1523 ap[2] = 0xff;
1524 ap[3] = 0xff;
1525 } else {
1526 ap[0] = (sdebug_capacity >> 24) & 0xff;
1527 ap[1] = (sdebug_capacity >> 16) & 0xff;
1528 ap[2] = (sdebug_capacity >> 8) & 0xff;
1529 ap[3] = sdebug_capacity & 0xff;
1531 ap[6] = (SECT_SIZE_PER(target) >> 8) & 0xff;
1532 ap[7] = SECT_SIZE_PER(target) & 0xff;
1533 offset += bd_len;
1534 ap = arr + offset;
1535 } else if (16 == bd_len) {
1536 unsigned long long capac = sdebug_capacity;
1538 for (k = 0; k < 8; ++k, capac >>= 8)
1539 ap[7 - k] = capac & 0xff;
1540 ap[12] = (SECT_SIZE_PER(target) >> 24) & 0xff;
1541 ap[13] = (SECT_SIZE_PER(target) >> 16) & 0xff;
1542 ap[14] = (SECT_SIZE_PER(target) >> 8) & 0xff;
1543 ap[15] = SECT_SIZE_PER(target) & 0xff;
1544 offset += bd_len;
1545 ap = arr + offset;
1548 if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) {
1549 /* TODO: Control Extension page */
1550 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
1552 return check_condition_result;
1554 switch (pcode) {
1555 case 0x1: /* Read-Write error recovery page, direct access */
1556 len = resp_err_recov_pg(ap, pcontrol, target);
1557 offset += len;
1558 break;
1559 case 0x2: /* Disconnect-Reconnect page, all devices */
1560 len = resp_disconnect_pg(ap, pcontrol, target);
1561 offset += len;
1562 break;
1563 case 0x3: /* Format device page, direct access */
1564 len = resp_format_pg(ap, pcontrol, target);
1565 offset += len;
1566 break;
1567 case 0x8: /* Caching page, direct access */
1568 len = resp_caching_pg(ap, pcontrol, target);
1569 offset += len;
1570 break;
1571 case 0xa: /* Control Mode page, all devices */
1572 len = resp_ctrl_m_pg(ap, pcontrol, target);
1573 offset += len;
1574 break;
1575 case 0x19: /* if spc==1 then sas phy, control+discover */
1576 if ((subpcode > 0x2) && (subpcode < 0xff)) {
1577 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1578 INVALID_FIELD_IN_CDB, 0);
1579 return check_condition_result;
1581 len = 0;
1582 if ((0x0 == subpcode) || (0xff == subpcode))
1583 len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
1584 if ((0x1 == subpcode) || (0xff == subpcode))
1585 len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
1586 target_dev_id);
1587 if ((0x2 == subpcode) || (0xff == subpcode))
1588 len += resp_sas_sha_m_spg(ap + len, pcontrol);
1589 offset += len;
1590 break;
1591 case 0x1c: /* Informational Exceptions Mode page, all devices */
1592 len = resp_iec_m_pg(ap, pcontrol, target);
1593 offset += len;
1594 break;
1595 case 0x3f: /* Read all Mode pages */
1596 if ((0 == subpcode) || (0xff == subpcode)) {
1597 len = resp_err_recov_pg(ap, pcontrol, target);
1598 len += resp_disconnect_pg(ap + len, pcontrol, target);
1599 len += resp_format_pg(ap + len, pcontrol, target);
1600 len += resp_caching_pg(ap + len, pcontrol, target);
1601 len += resp_ctrl_m_pg(ap + len, pcontrol, target);
1602 len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
1603 if (0xff == subpcode) {
1604 len += resp_sas_pcd_m_spg(ap + len, pcontrol,
1605 target, target_dev_id);
1606 len += resp_sas_sha_m_spg(ap + len, pcontrol);
1608 len += resp_iec_m_pg(ap + len, pcontrol, target);
1609 } else {
1610 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1611 INVALID_FIELD_IN_CDB, 0);
1612 return check_condition_result;
1614 offset += len;
1615 break;
1616 default:
1617 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
1619 return check_condition_result;
1621 if (msense_6)
1622 arr[0] = offset - 1;
1623 else {
1624 arr[0] = ((offset - 2) >> 8) & 0xff;
1625 arr[1] = (offset - 2) & 0xff;
1627 return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
1630 #define SDEBUG_MAX_MSELECT_SZ 512
1632 static int resp_mode_select(struct scsi_cmnd * scp, int mselect6,
1633 struct sdebug_dev_info * devip)
1635 int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
1636 int param_len, res, errsts, mpage;
1637 unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
1638 unsigned char *cmd = (unsigned char *)scp->cmnd;
1640 if ((errsts = check_readiness(scp, 1, devip)))
1641 return errsts;
1642 memset(arr, 0, sizeof(arr));
1643 pf = cmd[1] & 0x10;
1644 sp = cmd[1] & 0x1;
1645 param_len = mselect6 ? cmd[4] : ((cmd[7] << 8) + cmd[8]);
1646 if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
1647 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1648 INVALID_FIELD_IN_CDB, 0);
1649 return check_condition_result;
1651 res = fetch_to_dev_buffer(scp, arr, param_len);
1652 if (-1 == res)
1653 return (DID_ERROR << 16);
1654 else if ((res < param_len) &&
1655 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
1656 printk(KERN_INFO "scsi_debug: mode_select: cdb indicated=%d, "
1657 " IO sent=%d bytes\n", param_len, res);
1658 md_len = mselect6 ? (arr[0] + 1) : ((arr[0] << 8) + arr[1] + 2);
1659 bd_len = mselect6 ? arr[3] : ((arr[6] << 8) + arr[7]);
1660 if (md_len > 2) {
1661 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1662 INVALID_FIELD_IN_PARAM_LIST, 0);
1663 return check_condition_result;
1665 off = bd_len + (mselect6 ? 4 : 8);
1666 mpage = arr[off] & 0x3f;
1667 ps = !!(arr[off] & 0x80);
1668 if (ps) {
1669 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1670 INVALID_FIELD_IN_PARAM_LIST, 0);
1671 return check_condition_result;
1673 spf = !!(arr[off] & 0x40);
1674 pg_len = spf ? ((arr[off + 2] << 8) + arr[off + 3] + 4) :
1675 (arr[off + 1] + 2);
1676 if ((pg_len + off) > param_len) {
1677 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1678 PARAMETER_LIST_LENGTH_ERR, 0);
1679 return check_condition_result;
1681 switch (mpage) {
1682 case 0xa: /* Control Mode page */
1683 if (ctrl_m_pg[1] == arr[off + 1]) {
1684 memcpy(ctrl_m_pg + 2, arr + off + 2,
1685 sizeof(ctrl_m_pg) - 2);
1686 scsi_debug_dsense = !!(ctrl_m_pg[2] & 0x4);
1687 return 0;
1689 break;
1690 case 0x1c: /* Informational Exceptions Mode page */
1691 if (iec_m_pg[1] == arr[off + 1]) {
1692 memcpy(iec_m_pg + 2, arr + off + 2,
1693 sizeof(iec_m_pg) - 2);
1694 return 0;
1696 break;
1697 default:
1698 break;
1700 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1701 INVALID_FIELD_IN_PARAM_LIST, 0);
1702 return check_condition_result;
1705 static int resp_temp_l_pg(unsigned char * arr)
1707 unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38,
1708 0x0, 0x1, 0x3, 0x2, 0x0, 65,
1711 memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
1712 return sizeof(temp_l_pg);
1715 static int resp_ie_l_pg(unsigned char * arr)
1717 unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
1720 memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
1721 if (iec_m_pg[2] & 0x4) { /* TEST bit set */
1722 arr[4] = THRESHOLD_EXCEEDED;
1723 arr[5] = 0xff;
1725 return sizeof(ie_l_pg);
1728 #define SDEBUG_MAX_LSENSE_SZ 512
1730 static int resp_log_sense(struct scsi_cmnd * scp,
1731 struct sdebug_dev_info * devip)
1733 int ppc, sp, pcontrol, pcode, subpcode, alloc_len, errsts, len, n;
1734 unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
1735 unsigned char *cmd = (unsigned char *)scp->cmnd;
1737 if ((errsts = check_readiness(scp, 1, devip)))
1738 return errsts;
1739 memset(arr, 0, sizeof(arr));
1740 ppc = cmd[1] & 0x2;
1741 sp = cmd[1] & 0x1;
1742 if (ppc || sp) {
1743 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1744 INVALID_FIELD_IN_CDB, 0);
1745 return check_condition_result;
1747 pcontrol = (cmd[2] & 0xc0) >> 6;
1748 pcode = cmd[2] & 0x3f;
1749 subpcode = cmd[3] & 0xff;
1750 alloc_len = (cmd[7] << 8) + cmd[8];
1751 arr[0] = pcode;
1752 if (0 == subpcode) {
1753 switch (pcode) {
1754 case 0x0: /* Supported log pages log page */
1755 n = 4;
1756 arr[n++] = 0x0; /* this page */
1757 arr[n++] = 0xd; /* Temperature */
1758 arr[n++] = 0x2f; /* Informational exceptions */
1759 arr[3] = n - 4;
1760 break;
1761 case 0xd: /* Temperature log page */
1762 arr[3] = resp_temp_l_pg(arr + 4);
1763 break;
1764 case 0x2f: /* Informational exceptions log page */
1765 arr[3] = resp_ie_l_pg(arr + 4);
1766 break;
1767 default:
1768 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1769 INVALID_FIELD_IN_CDB, 0);
1770 return check_condition_result;
1772 } else if (0xff == subpcode) {
1773 arr[0] |= 0x40;
1774 arr[1] = subpcode;
1775 switch (pcode) {
1776 case 0x0: /* Supported log pages and subpages log page */
1777 n = 4;
1778 arr[n++] = 0x0;
1779 arr[n++] = 0x0; /* 0,0 page */
1780 arr[n++] = 0x0;
1781 arr[n++] = 0xff; /* this page */
1782 arr[n++] = 0xd;
1783 arr[n++] = 0x0; /* Temperature */
1784 arr[n++] = 0x2f;
1785 arr[n++] = 0x0; /* Informational exceptions */
1786 arr[3] = n - 4;
1787 break;
1788 case 0xd: /* Temperature subpages */
1789 n = 4;
1790 arr[n++] = 0xd;
1791 arr[n++] = 0x0; /* Temperature */
1792 arr[3] = n - 4;
1793 break;
1794 case 0x2f: /* Informational exceptions subpages */
1795 n = 4;
1796 arr[n++] = 0x2f;
1797 arr[n++] = 0x0; /* Informational exceptions */
1798 arr[3] = n - 4;
1799 break;
1800 default:
1801 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1802 INVALID_FIELD_IN_CDB, 0);
1803 return check_condition_result;
1805 } else {
1806 mk_sense_buffer(devip, ILLEGAL_REQUEST,
1807 INVALID_FIELD_IN_CDB, 0);
1808 return check_condition_result;
1810 len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
1811 return fill_from_dev_buffer(scp, arr,
1812 min(len, SDEBUG_MAX_INQ_ARR_SZ));
1815 static int resp_read(struct scsi_cmnd * SCpnt, unsigned long long lba,
1816 unsigned int num, struct sdebug_dev_info * devip)
1818 unsigned long iflags;
1819 unsigned int block, from_bottom;
1820 unsigned long long u;
1821 int ret;
1823 if (lba + num > sdebug_capacity) {
1824 mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
1826 return check_condition_result;
1828 /* transfer length excessive (tie in to block limits VPD page) */
1829 if (num > sdebug_store_sectors) {
1830 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
1832 return check_condition_result;
1834 if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) &&
1835 (lba <= OPT_MEDIUM_ERR_ADDR) &&
1836 ((lba + num) > OPT_MEDIUM_ERR_ADDR)) {
1837 /* claim unrecoverable read error */
1838 mk_sense_buffer(devip, MEDIUM_ERROR, UNRECOVERED_READ_ERR,
1840 /* set info field and valid bit for fixed descriptor */
1841 if (0x70 == (devip->sense_buff[0] & 0x7f)) {
1842 devip->sense_buff[0] |= 0x80; /* Valid bit */
1843 ret = OPT_MEDIUM_ERR_ADDR;
1844 devip->sense_buff[3] = (ret >> 24) & 0xff;
1845 devip->sense_buff[4] = (ret >> 16) & 0xff;
1846 devip->sense_buff[5] = (ret >> 8) & 0xff;
1847 devip->sense_buff[6] = ret & 0xff;
1849 return check_condition_result;
1851 read_lock_irqsave(&atomic_rw, iflags);
1852 if ((lba + num) <= sdebug_store_sectors)
1853 ret = fill_from_dev_buffer(SCpnt,
1854 fake_storep + (lba * SECT_SIZE),
1855 num * SECT_SIZE);
1856 else {
1857 /* modulo when one arg is 64 bits needs do_div() */
1858 u = lba;
1859 block = do_div(u, sdebug_store_sectors);
1860 from_bottom = 0;
1861 if ((block + num) > sdebug_store_sectors)
1862 from_bottom = (block + num) - sdebug_store_sectors;
1863 ret = fill_from_dev_buffer(SCpnt,
1864 fake_storep + (block * SECT_SIZE),
1865 (num - from_bottom) * SECT_SIZE);
1866 if ((0 == ret) && (from_bottom > 0))
1867 ret = fill_from_dev_buffer(SCpnt, fake_storep,
1868 from_bottom * SECT_SIZE);
1870 read_unlock_irqrestore(&atomic_rw, iflags);
1871 return ret;
1874 static int resp_write(struct scsi_cmnd * SCpnt, unsigned long long lba,
1875 unsigned int num, struct sdebug_dev_info * devip)
1877 unsigned long iflags;
1878 unsigned int block, to_bottom;
1879 unsigned long long u;
1880 int res;
1882 if (lba + num > sdebug_capacity) {
1883 mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
1885 return check_condition_result;
1887 /* transfer length excessive (tie in to block limits VPD page) */
1888 if (num > sdebug_store_sectors) {
1889 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
1891 return check_condition_result;
1894 write_lock_irqsave(&atomic_rw, iflags);
1895 if ((lba + num) <= sdebug_store_sectors)
1896 res = fetch_to_dev_buffer(SCpnt,
1897 fake_storep + (lba * SECT_SIZE),
1898 num * SECT_SIZE);
1899 else {
1900 /* modulo when one arg is 64 bits needs do_div() */
1901 u = lba;
1902 block = do_div(u, sdebug_store_sectors);
1903 to_bottom = 0;
1904 if ((block + num) > sdebug_store_sectors)
1905 to_bottom = (block + num) - sdebug_store_sectors;
1906 res = fetch_to_dev_buffer(SCpnt,
1907 fake_storep + (block * SECT_SIZE),
1908 (num - to_bottom) * SECT_SIZE);
1909 if ((0 == res) && (to_bottom > 0))
1910 res = fetch_to_dev_buffer(SCpnt, fake_storep,
1911 to_bottom * SECT_SIZE);
1913 write_unlock_irqrestore(&atomic_rw, iflags);
1914 if (-1 == res)
1915 return (DID_ERROR << 16);
1916 else if ((res < (num * SECT_SIZE)) &&
1917 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
1918 printk(KERN_INFO "scsi_debug: write: cdb indicated=%u, "
1919 " IO sent=%d bytes\n", num * SECT_SIZE, res);
1920 return 0;
1923 #define SDEBUG_RLUN_ARR_SZ 256
1925 static int resp_report_luns(struct scsi_cmnd * scp,
1926 struct sdebug_dev_info * devip)
1928 unsigned int alloc_len;
1929 int lun_cnt, i, upper, num, n, wlun, lun;
1930 unsigned char *cmd = (unsigned char *)scp->cmnd;
1931 int select_report = (int)cmd[2];
1932 struct scsi_lun *one_lun;
1933 unsigned char arr[SDEBUG_RLUN_ARR_SZ];
1934 unsigned char * max_addr;
1936 alloc_len = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24);
1937 if ((alloc_len < 4) || (select_report > 2)) {
1938 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
1940 return check_condition_result;
1942 /* can produce response with up to 16k luns (lun 0 to lun 16383) */
1943 memset(arr, 0, SDEBUG_RLUN_ARR_SZ);
1944 lun_cnt = scsi_debug_max_luns;
1945 if (1 == select_report)
1946 lun_cnt = 0;
1947 else if (scsi_debug_no_lun_0 && (lun_cnt > 0))
1948 --lun_cnt;
1949 wlun = (select_report > 0) ? 1 : 0;
1950 num = lun_cnt + wlun;
1951 arr[2] = ((sizeof(struct scsi_lun) * num) >> 8) & 0xff;
1952 arr[3] = (sizeof(struct scsi_lun) * num) & 0xff;
1953 n = min((int)((SDEBUG_RLUN_ARR_SZ - 8) /
1954 sizeof(struct scsi_lun)), num);
1955 if (n < num) {
1956 wlun = 0;
1957 lun_cnt = n;
1959 one_lun = (struct scsi_lun *) &arr[8];
1960 max_addr = arr + SDEBUG_RLUN_ARR_SZ;
1961 for (i = 0, lun = (scsi_debug_no_lun_0 ? 1 : 0);
1962 ((i < lun_cnt) && ((unsigned char *)(one_lun + i) < max_addr));
1963 i++, lun++) {
1964 upper = (lun >> 8) & 0x3f;
1965 if (upper)
1966 one_lun[i].scsi_lun[0] =
1967 (upper | (SAM2_LUN_ADDRESS_METHOD << 6));
1968 one_lun[i].scsi_lun[1] = lun & 0xff;
1970 if (wlun) {
1971 one_lun[i].scsi_lun[0] = (SAM2_WLUN_REPORT_LUNS >> 8) & 0xff;
1972 one_lun[i].scsi_lun[1] = SAM2_WLUN_REPORT_LUNS & 0xff;
1973 i++;
1975 alloc_len = (unsigned char *)(one_lun + i) - arr;
1976 return fill_from_dev_buffer(scp, arr,
1977 min((int)alloc_len, SDEBUG_RLUN_ARR_SZ));
1980 static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba,
1981 unsigned int num, struct sdebug_dev_info *devip)
1983 int i, j, ret = -1;
1984 unsigned char *kaddr, *buf;
1985 unsigned int offset;
1986 struct scatterlist *sg;
1987 struct scsi_data_buffer *sdb = scsi_in(scp);
1989 /* better not to use temporary buffer. */
1990 buf = kmalloc(scsi_bufflen(scp), GFP_ATOMIC);
1991 if (!buf)
1992 return ret;
1994 offset = 0;
1995 scsi_for_each_sg(scp, sg, scsi_sg_count(scp), i) {
1996 kaddr = (unsigned char *)kmap_atomic(sg_page(sg), KM_USER0);
1997 if (!kaddr)
1998 goto out;
2000 memcpy(buf + offset, kaddr + sg->offset, sg->length);
2001 offset += sg->length;
2002 kunmap_atomic(kaddr, KM_USER0);
2005 offset = 0;
2006 for_each_sg(sdb->table.sgl, sg, sdb->table.nents, i) {
2007 kaddr = (unsigned char *)kmap_atomic(sg_page(sg), KM_USER0);
2008 if (!kaddr)
2009 goto out;
2011 for (j = 0; j < sg->length; j++)
2012 *(kaddr + sg->offset + j) ^= *(buf + offset + j);
2014 offset += sg->length;
2015 kunmap_atomic(kaddr, KM_USER0);
2017 ret = 0;
2018 out:
2019 kfree(buf);
2021 return ret;
2024 /* When timer goes off this function is called. */
2025 static void timer_intr_handler(unsigned long indx)
2027 struct sdebug_queued_cmd * sqcp;
2028 unsigned long iflags;
2030 if (indx >= SCSI_DEBUG_CANQUEUE) {
2031 printk(KERN_ERR "scsi_debug:timer_intr_handler: indx too "
2032 "large\n");
2033 return;
2035 spin_lock_irqsave(&queued_arr_lock, iflags);
2036 sqcp = &queued_arr[(int)indx];
2037 if (! sqcp->in_use) {
2038 printk(KERN_ERR "scsi_debug:timer_intr_handler: Unexpected "
2039 "interrupt\n");
2040 spin_unlock_irqrestore(&queued_arr_lock, iflags);
2041 return;
2043 sqcp->in_use = 0;
2044 if (sqcp->done_funct) {
2045 sqcp->a_cmnd->result = sqcp->scsi_result;
2046 sqcp->done_funct(sqcp->a_cmnd); /* callback to mid level */
2048 sqcp->done_funct = NULL;
2049 spin_unlock_irqrestore(&queued_arr_lock, iflags);
2052 static int scsi_debug_slave_alloc(struct scsi_device * sdp)
2054 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
2055 printk(KERN_INFO "scsi_debug: slave_alloc <%u %u %u %u>\n",
2056 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
2057 set_bit(QUEUE_FLAG_BIDI, &sdp->request_queue->queue_flags);
2058 return 0;
2061 static int scsi_debug_slave_configure(struct scsi_device * sdp)
2063 struct sdebug_dev_info * devip;
2065 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
2066 printk(KERN_INFO "scsi_debug: slave_configure <%u %u %u %u>\n",
2067 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
2068 if (sdp->host->max_cmd_len != SCSI_DEBUG_MAX_CMD_LEN)
2069 sdp->host->max_cmd_len = SCSI_DEBUG_MAX_CMD_LEN;
2070 devip = devInfoReg(sdp);
2071 if (NULL == devip)
2072 return 1; /* no resources, will be marked offline */
2073 sdp->hostdata = devip;
2074 if (sdp->host->cmd_per_lun)
2075 scsi_adjust_queue_depth(sdp, SDEBUG_TAGGED_QUEUING,
2076 sdp->host->cmd_per_lun);
2077 blk_queue_max_segment_size(sdp->request_queue, 256 * 1024);
2078 return 0;
2081 static void scsi_debug_slave_destroy(struct scsi_device * sdp)
2083 struct sdebug_dev_info * devip =
2084 (struct sdebug_dev_info *)sdp->hostdata;
2086 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
2087 printk(KERN_INFO "scsi_debug: slave_destroy <%u %u %u %u>\n",
2088 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
2089 if (devip) {
2090 /* make this slot avaliable for re-use */
2091 devip->used = 0;
2092 sdp->hostdata = NULL;
2096 static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev)
2098 struct sdebug_host_info * sdbg_host;
2099 struct sdebug_dev_info * open_devip = NULL;
2100 struct sdebug_dev_info * devip =
2101 (struct sdebug_dev_info *)sdev->hostdata;
2103 if (devip)
2104 return devip;
2105 sdbg_host = *(struct sdebug_host_info **) sdev->host->hostdata;
2106 if(! sdbg_host) {
2107 printk(KERN_ERR "Host info NULL\n");
2108 return NULL;
2110 list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
2111 if ((devip->used) && (devip->channel == sdev->channel) &&
2112 (devip->target == sdev->id) &&
2113 (devip->lun == sdev->lun))
2114 return devip;
2115 else {
2116 if ((!devip->used) && (!open_devip))
2117 open_devip = devip;
2120 if (NULL == open_devip) { /* try and make a new one */
2121 open_devip = kzalloc(sizeof(*open_devip),GFP_ATOMIC);
2122 if (NULL == open_devip) {
2123 printk(KERN_ERR "%s: out of memory at line %d\n",
2124 __FUNCTION__, __LINE__);
2125 return NULL;
2127 open_devip->sdbg_host = sdbg_host;
2128 list_add_tail(&open_devip->dev_list,
2129 &sdbg_host->dev_info_list);
2131 if (open_devip) {
2132 open_devip->channel = sdev->channel;
2133 open_devip->target = sdev->id;
2134 open_devip->lun = sdev->lun;
2135 open_devip->sdbg_host = sdbg_host;
2136 open_devip->reset = 1;
2137 open_devip->used = 1;
2138 memset(open_devip->sense_buff, 0, SDEBUG_SENSE_LEN);
2139 if (scsi_debug_dsense)
2140 open_devip->sense_buff[0] = 0x72;
2141 else {
2142 open_devip->sense_buff[0] = 0x70;
2143 open_devip->sense_buff[7] = 0xa;
2145 if (sdev->lun == SAM2_WLUN_REPORT_LUNS)
2146 open_devip->wlun = SAM2_WLUN_REPORT_LUNS & 0xff;
2147 return open_devip;
2149 return NULL;
2152 static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
2153 int asc, int asq)
2155 unsigned char * sbuff;
2157 sbuff = devip->sense_buff;
2158 memset(sbuff, 0, SDEBUG_SENSE_LEN);
2159 if (scsi_debug_dsense) {
2160 sbuff[0] = 0x72; /* descriptor, current */
2161 sbuff[1] = key;
2162 sbuff[2] = asc;
2163 sbuff[3] = asq;
2164 } else {
2165 sbuff[0] = 0x70; /* fixed, current */
2166 sbuff[2] = key;
2167 sbuff[7] = 0xa; /* implies 18 byte sense buffer */
2168 sbuff[12] = asc;
2169 sbuff[13] = asq;
2171 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
2172 printk(KERN_INFO "scsi_debug: [sense_key,asc,ascq]: "
2173 "[0x%x,0x%x,0x%x]\n", key, asc, asq);
2176 static int scsi_debug_abort(struct scsi_cmnd * SCpnt)
2178 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
2179 printk(KERN_INFO "scsi_debug: abort\n");
2180 ++num_aborts;
2181 stop_queued_cmnd(SCpnt);
2182 return SUCCESS;
2185 static int scsi_debug_biosparam(struct scsi_device *sdev,
2186 struct block_device * bdev, sector_t capacity, int *info)
2188 int res;
2189 unsigned char *buf;
2191 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
2192 printk(KERN_INFO "scsi_debug: biosparam\n");
2193 buf = scsi_bios_ptable(bdev);
2194 if (buf) {
2195 res = scsi_partsize(buf, capacity,
2196 &info[2], &info[0], &info[1]);
2197 kfree(buf);
2198 if (! res)
2199 return res;
2201 info[0] = sdebug_heads;
2202 info[1] = sdebug_sectors_per;
2203 info[2] = sdebug_cylinders_per;
2204 return 0;
2207 static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt)
2209 struct sdebug_dev_info * devip;
2211 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
2212 printk(KERN_INFO "scsi_debug: device_reset\n");
2213 ++num_dev_resets;
2214 if (SCpnt) {
2215 devip = devInfoReg(SCpnt->device);
2216 if (devip)
2217 devip->reset = 1;
2219 return SUCCESS;
2222 static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt)
2224 struct sdebug_host_info *sdbg_host;
2225 struct sdebug_dev_info * dev_info;
2226 struct scsi_device * sdp;
2227 struct Scsi_Host * hp;
2229 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
2230 printk(KERN_INFO "scsi_debug: bus_reset\n");
2231 ++num_bus_resets;
2232 if (SCpnt && ((sdp = SCpnt->device)) && ((hp = sdp->host))) {
2233 sdbg_host = *(struct sdebug_host_info **) hp->hostdata;
2234 if (sdbg_host) {
2235 list_for_each_entry(dev_info,
2236 &sdbg_host->dev_info_list,
2237 dev_list)
2238 dev_info->reset = 1;
2241 return SUCCESS;
2244 static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt)
2246 struct sdebug_host_info * sdbg_host;
2247 struct sdebug_dev_info * dev_info;
2249 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
2250 printk(KERN_INFO "scsi_debug: host_reset\n");
2251 ++num_host_resets;
2252 spin_lock(&sdebug_host_list_lock);
2253 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
2254 list_for_each_entry(dev_info, &sdbg_host->dev_info_list,
2255 dev_list)
2256 dev_info->reset = 1;
2258 spin_unlock(&sdebug_host_list_lock);
2259 stop_all_queued();
2260 return SUCCESS;
2263 /* Returns 1 if found 'cmnd' and deleted its timer. else returns 0 */
2264 static int stop_queued_cmnd(struct scsi_cmnd * cmnd)
2266 unsigned long iflags;
2267 int k;
2268 struct sdebug_queued_cmd * sqcp;
2270 spin_lock_irqsave(&queued_arr_lock, iflags);
2271 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
2272 sqcp = &queued_arr[k];
2273 if (sqcp->in_use && (cmnd == sqcp->a_cmnd)) {
2274 del_timer_sync(&sqcp->cmnd_timer);
2275 sqcp->in_use = 0;
2276 sqcp->a_cmnd = NULL;
2277 break;
2280 spin_unlock_irqrestore(&queued_arr_lock, iflags);
2281 return (k < SCSI_DEBUG_CANQUEUE) ? 1 : 0;
2284 /* Deletes (stops) timers of all queued commands */
2285 static void stop_all_queued(void)
2287 unsigned long iflags;
2288 int k;
2289 struct sdebug_queued_cmd * sqcp;
2291 spin_lock_irqsave(&queued_arr_lock, iflags);
2292 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
2293 sqcp = &queued_arr[k];
2294 if (sqcp->in_use && sqcp->a_cmnd) {
2295 del_timer_sync(&sqcp->cmnd_timer);
2296 sqcp->in_use = 0;
2297 sqcp->a_cmnd = NULL;
2300 spin_unlock_irqrestore(&queued_arr_lock, iflags);
2303 /* Initializes timers in queued array */
2304 static void __init init_all_queued(void)
2306 unsigned long iflags;
2307 int k;
2308 struct sdebug_queued_cmd * sqcp;
2310 spin_lock_irqsave(&queued_arr_lock, iflags);
2311 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
2312 sqcp = &queued_arr[k];
2313 init_timer(&sqcp->cmnd_timer);
2314 sqcp->in_use = 0;
2315 sqcp->a_cmnd = NULL;
2317 spin_unlock_irqrestore(&queued_arr_lock, iflags);
2320 static void __init sdebug_build_parts(unsigned char * ramp)
2322 struct partition * pp;
2323 int starts[SDEBUG_MAX_PARTS + 2];
2324 int sectors_per_part, num_sectors, k;
2325 int heads_by_sects, start_sec, end_sec;
2327 /* assume partition table already zeroed */
2328 if ((scsi_debug_num_parts < 1) || (sdebug_store_size < 1048576))
2329 return;
2330 if (scsi_debug_num_parts > SDEBUG_MAX_PARTS) {
2331 scsi_debug_num_parts = SDEBUG_MAX_PARTS;
2332 printk(KERN_WARNING "scsi_debug:build_parts: reducing "
2333 "partitions to %d\n", SDEBUG_MAX_PARTS);
2335 num_sectors = (int)sdebug_store_sectors;
2336 sectors_per_part = (num_sectors - sdebug_sectors_per)
2337 / scsi_debug_num_parts;
2338 heads_by_sects = sdebug_heads * sdebug_sectors_per;
2339 starts[0] = sdebug_sectors_per;
2340 for (k = 1; k < scsi_debug_num_parts; ++k)
2341 starts[k] = ((k * sectors_per_part) / heads_by_sects)
2342 * heads_by_sects;
2343 starts[scsi_debug_num_parts] = num_sectors;
2344 starts[scsi_debug_num_parts + 1] = 0;
2346 ramp[510] = 0x55; /* magic partition markings */
2347 ramp[511] = 0xAA;
2348 pp = (struct partition *)(ramp + 0x1be);
2349 for (k = 0; starts[k + 1]; ++k, ++pp) {
2350 start_sec = starts[k];
2351 end_sec = starts[k + 1] - 1;
2352 pp->boot_ind = 0;
2354 pp->cyl = start_sec / heads_by_sects;
2355 pp->head = (start_sec - (pp->cyl * heads_by_sects))
2356 / sdebug_sectors_per;
2357 pp->sector = (start_sec % sdebug_sectors_per) + 1;
2359 pp->end_cyl = end_sec / heads_by_sects;
2360 pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
2361 / sdebug_sectors_per;
2362 pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
2364 pp->start_sect = start_sec;
2365 pp->nr_sects = end_sec - start_sec + 1;
2366 pp->sys_ind = 0x83; /* plain Linux partition */
2370 static int schedule_resp(struct scsi_cmnd * cmnd,
2371 struct sdebug_dev_info * devip,
2372 done_funct_t done, int scsi_result, int delta_jiff)
2374 if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmnd) {
2375 if (scsi_result) {
2376 struct scsi_device * sdp = cmnd->device;
2378 printk(KERN_INFO "scsi_debug: <%u %u %u %u> "
2379 "non-zero result=0x%x\n", sdp->host->host_no,
2380 sdp->channel, sdp->id, sdp->lun, scsi_result);
2383 if (cmnd && devip) {
2384 /* simulate autosense by this driver */
2385 if (SAM_STAT_CHECK_CONDITION == (scsi_result & 0xff))
2386 memcpy(cmnd->sense_buffer, devip->sense_buff,
2387 (SCSI_SENSE_BUFFERSIZE > SDEBUG_SENSE_LEN) ?
2388 SDEBUG_SENSE_LEN : SCSI_SENSE_BUFFERSIZE);
2390 if (delta_jiff <= 0) {
2391 if (cmnd)
2392 cmnd->result = scsi_result;
2393 if (done)
2394 done(cmnd);
2395 return 0;
2396 } else {
2397 unsigned long iflags;
2398 int k;
2399 struct sdebug_queued_cmd * sqcp = NULL;
2401 spin_lock_irqsave(&queued_arr_lock, iflags);
2402 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
2403 sqcp = &queued_arr[k];
2404 if (! sqcp->in_use)
2405 break;
2407 if (k >= SCSI_DEBUG_CANQUEUE) {
2408 spin_unlock_irqrestore(&queued_arr_lock, iflags);
2409 printk(KERN_WARNING "scsi_debug: can_queue exceeded\n");
2410 return 1; /* report busy to mid level */
2412 sqcp->in_use = 1;
2413 sqcp->a_cmnd = cmnd;
2414 sqcp->scsi_result = scsi_result;
2415 sqcp->done_funct = done;
2416 sqcp->cmnd_timer.function = timer_intr_handler;
2417 sqcp->cmnd_timer.data = k;
2418 sqcp->cmnd_timer.expires = jiffies + delta_jiff;
2419 add_timer(&sqcp->cmnd_timer);
2420 spin_unlock_irqrestore(&queued_arr_lock, iflags);
2421 if (cmnd)
2422 cmnd->result = 0;
2423 return 0;
2427 /* Note: The following macros create attribute files in the
2428 /sys/module/scsi_debug/parameters directory. Unfortunately this
2429 driver is unaware of a change and cannot trigger auxiliary actions
2430 as it can when the corresponding attribute in the
2431 /sys/bus/pseudo/drivers/scsi_debug directory is changed.
2433 module_param_named(add_host, scsi_debug_add_host, int, S_IRUGO | S_IWUSR);
2434 module_param_named(delay, scsi_debug_delay, int, S_IRUGO | S_IWUSR);
2435 module_param_named(dev_size_mb, scsi_debug_dev_size_mb, int, S_IRUGO);
2436 module_param_named(dsense, scsi_debug_dsense, int, S_IRUGO | S_IWUSR);
2437 module_param_named(every_nth, scsi_debug_every_nth, int, S_IRUGO | S_IWUSR);
2438 module_param_named(fake_rw, scsi_debug_fake_rw, int, S_IRUGO | S_IWUSR);
2439 module_param_named(max_luns, scsi_debug_max_luns, int, S_IRUGO | S_IWUSR);
2440 module_param_named(no_lun_0, scsi_debug_no_lun_0, int, S_IRUGO | S_IWUSR);
2441 module_param_named(num_parts, scsi_debug_num_parts, int, S_IRUGO);
2442 module_param_named(num_tgts, scsi_debug_num_tgts, int, S_IRUGO | S_IWUSR);
2443 module_param_named(opts, scsi_debug_opts, int, S_IRUGO | S_IWUSR);
2444 module_param_named(ptype, scsi_debug_ptype, int, S_IRUGO | S_IWUSR);
2445 module_param_named(scsi_level, scsi_debug_scsi_level, int, S_IRUGO);
2446 module_param_named(virtual_gb, scsi_debug_virtual_gb, int, S_IRUGO | S_IWUSR);
2447 module_param_named(vpd_use_hostno, scsi_debug_vpd_use_hostno, int,
2448 S_IRUGO | S_IWUSR);
2450 MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
2451 MODULE_DESCRIPTION("SCSI debug adapter driver");
2452 MODULE_LICENSE("GPL");
2453 MODULE_VERSION(SCSI_DEBUG_VERSION);
2455 MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
2456 MODULE_PARM_DESC(delay, "# of jiffies to delay response(def=1)");
2457 MODULE_PARM_DESC(dev_size_mb, "size in MB of ram shared by devs(def=8)");
2458 MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
2459 MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)");
2460 MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
2461 MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
2462 MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
2463 MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
2464 MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
2465 MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)");
2466 MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
2467 MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=5[SPC-3])");
2468 MODULE_PARM_DESC(virtual_gb, "virtual gigabyte size (def=0 -> use dev_size_mb)");
2469 MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
2472 static char sdebug_info[256];
2474 static const char * scsi_debug_info(struct Scsi_Host * shp)
2476 sprintf(sdebug_info, "scsi_debug, version %s [%s], "
2477 "dev_size_mb=%d, opts=0x%x", SCSI_DEBUG_VERSION,
2478 scsi_debug_version_date, scsi_debug_dev_size_mb,
2479 scsi_debug_opts);
2480 return sdebug_info;
2483 /* scsi_debug_proc_info
2484 * Used if the driver currently has no own support for /proc/scsi
2486 static int scsi_debug_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
2487 int length, int inout)
2489 int len, pos, begin;
2490 int orig_length;
2492 orig_length = length;
2494 if (inout == 1) {
2495 char arr[16];
2496 int minLen = length > 15 ? 15 : length;
2498 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2499 return -EACCES;
2500 memcpy(arr, buffer, minLen);
2501 arr[minLen] = '\0';
2502 if (1 != sscanf(arr, "%d", &pos))
2503 return -EINVAL;
2504 scsi_debug_opts = pos;
2505 if (scsi_debug_every_nth != 0)
2506 scsi_debug_cmnd_count = 0;
2507 return length;
2509 begin = 0;
2510 pos = len = sprintf(buffer, "scsi_debug adapter driver, version "
2511 "%s [%s]\n"
2512 "num_tgts=%d, shared (ram) size=%d MB, opts=0x%x, "
2513 "every_nth=%d(curr:%d)\n"
2514 "delay=%d, max_luns=%d, scsi_level=%d\n"
2515 "sector_size=%d bytes, cylinders=%d, heads=%d, sectors=%d\n"
2516 "number of aborts=%d, device_reset=%d, bus_resets=%d, "
2517 "host_resets=%d\n",
2518 SCSI_DEBUG_VERSION, scsi_debug_version_date, scsi_debug_num_tgts,
2519 scsi_debug_dev_size_mb, scsi_debug_opts, scsi_debug_every_nth,
2520 scsi_debug_cmnd_count, scsi_debug_delay,
2521 scsi_debug_max_luns, scsi_debug_scsi_level,
2522 SECT_SIZE, sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
2523 num_aborts, num_dev_resets, num_bus_resets, num_host_resets);
2524 if (pos < offset) {
2525 len = 0;
2526 begin = pos;
2528 *start = buffer + (offset - begin); /* Start of wanted data */
2529 len -= (offset - begin);
2530 if (len > length)
2531 len = length;
2532 return len;
2535 static ssize_t sdebug_delay_show(struct device_driver * ddp, char * buf)
2537 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_delay);
2540 static ssize_t sdebug_delay_store(struct device_driver * ddp,
2541 const char * buf, size_t count)
2543 int delay;
2544 char work[20];
2546 if (1 == sscanf(buf, "%10s", work)) {
2547 if ((1 == sscanf(work, "%d", &delay)) && (delay >= 0)) {
2548 scsi_debug_delay = delay;
2549 return count;
2552 return -EINVAL;
2554 DRIVER_ATTR(delay, S_IRUGO | S_IWUSR, sdebug_delay_show,
2555 sdebug_delay_store);
2557 static ssize_t sdebug_opts_show(struct device_driver * ddp, char * buf)
2559 return scnprintf(buf, PAGE_SIZE, "0x%x\n", scsi_debug_opts);
2562 static ssize_t sdebug_opts_store(struct device_driver * ddp,
2563 const char * buf, size_t count)
2565 int opts;
2566 char work[20];
2568 if (1 == sscanf(buf, "%10s", work)) {
2569 if (0 == strnicmp(work,"0x", 2)) {
2570 if (1 == sscanf(&work[2], "%x", &opts))
2571 goto opts_done;
2572 } else {
2573 if (1 == sscanf(work, "%d", &opts))
2574 goto opts_done;
2577 return -EINVAL;
2578 opts_done:
2579 scsi_debug_opts = opts;
2580 scsi_debug_cmnd_count = 0;
2581 return count;
2583 DRIVER_ATTR(opts, S_IRUGO | S_IWUSR, sdebug_opts_show,
2584 sdebug_opts_store);
2586 static ssize_t sdebug_ptype_show(struct device_driver * ddp, char * buf)
2588 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ptype);
2590 static ssize_t sdebug_ptype_store(struct device_driver * ddp,
2591 const char * buf, size_t count)
2593 int n;
2595 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
2596 scsi_debug_ptype = n;
2597 return count;
2599 return -EINVAL;
2601 DRIVER_ATTR(ptype, S_IRUGO | S_IWUSR, sdebug_ptype_show, sdebug_ptype_store);
2603 static ssize_t sdebug_dsense_show(struct device_driver * ddp, char * buf)
2605 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dsense);
2607 static ssize_t sdebug_dsense_store(struct device_driver * ddp,
2608 const char * buf, size_t count)
2610 int n;
2612 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
2613 scsi_debug_dsense = n;
2614 return count;
2616 return -EINVAL;
2618 DRIVER_ATTR(dsense, S_IRUGO | S_IWUSR, sdebug_dsense_show,
2619 sdebug_dsense_store);
2621 static ssize_t sdebug_fake_rw_show(struct device_driver * ddp, char * buf)
2623 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_fake_rw);
2625 static ssize_t sdebug_fake_rw_store(struct device_driver * ddp,
2626 const char * buf, size_t count)
2628 int n;
2630 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
2631 scsi_debug_fake_rw = n;
2632 return count;
2634 return -EINVAL;
2636 DRIVER_ATTR(fake_rw, S_IRUGO | S_IWUSR, sdebug_fake_rw_show,
2637 sdebug_fake_rw_store);
2639 static ssize_t sdebug_no_lun_0_show(struct device_driver * ddp, char * buf)
2641 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_no_lun_0);
2643 static ssize_t sdebug_no_lun_0_store(struct device_driver * ddp,
2644 const char * buf, size_t count)
2646 int n;
2648 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
2649 scsi_debug_no_lun_0 = n;
2650 return count;
2652 return -EINVAL;
2654 DRIVER_ATTR(no_lun_0, S_IRUGO | S_IWUSR, sdebug_no_lun_0_show,
2655 sdebug_no_lun_0_store);
2657 static ssize_t sdebug_num_tgts_show(struct device_driver * ddp, char * buf)
2659 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_tgts);
2661 static ssize_t sdebug_num_tgts_store(struct device_driver * ddp,
2662 const char * buf, size_t count)
2664 int n;
2666 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
2667 scsi_debug_num_tgts = n;
2668 sdebug_max_tgts_luns();
2669 return count;
2671 return -EINVAL;
2673 DRIVER_ATTR(num_tgts, S_IRUGO | S_IWUSR, sdebug_num_tgts_show,
2674 sdebug_num_tgts_store);
2676 static ssize_t sdebug_dev_size_mb_show(struct device_driver * ddp, char * buf)
2678 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dev_size_mb);
2680 DRIVER_ATTR(dev_size_mb, S_IRUGO, sdebug_dev_size_mb_show, NULL);
2682 static ssize_t sdebug_num_parts_show(struct device_driver * ddp, char * buf)
2684 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_parts);
2686 DRIVER_ATTR(num_parts, S_IRUGO, sdebug_num_parts_show, NULL);
2688 static ssize_t sdebug_every_nth_show(struct device_driver * ddp, char * buf)
2690 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_every_nth);
2692 static ssize_t sdebug_every_nth_store(struct device_driver * ddp,
2693 const char * buf, size_t count)
2695 int nth;
2697 if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) {
2698 scsi_debug_every_nth = nth;
2699 scsi_debug_cmnd_count = 0;
2700 return count;
2702 return -EINVAL;
2704 DRIVER_ATTR(every_nth, S_IRUGO | S_IWUSR, sdebug_every_nth_show,
2705 sdebug_every_nth_store);
2707 static ssize_t sdebug_max_luns_show(struct device_driver * ddp, char * buf)
2709 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_max_luns);
2711 static ssize_t sdebug_max_luns_store(struct device_driver * ddp,
2712 const char * buf, size_t count)
2714 int n;
2716 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
2717 scsi_debug_max_luns = n;
2718 sdebug_max_tgts_luns();
2719 return count;
2721 return -EINVAL;
2723 DRIVER_ATTR(max_luns, S_IRUGO | S_IWUSR, sdebug_max_luns_show,
2724 sdebug_max_luns_store);
2726 static ssize_t sdebug_scsi_level_show(struct device_driver * ddp, char * buf)
2728 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_scsi_level);
2730 DRIVER_ATTR(scsi_level, S_IRUGO, sdebug_scsi_level_show, NULL);
2732 static ssize_t sdebug_virtual_gb_show(struct device_driver * ddp, char * buf)
2734 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_virtual_gb);
2736 static ssize_t sdebug_virtual_gb_store(struct device_driver * ddp,
2737 const char * buf, size_t count)
2739 int n;
2741 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
2742 scsi_debug_virtual_gb = n;
2743 if (scsi_debug_virtual_gb > 0) {
2744 sdebug_capacity = 2048 * 1024;
2745 sdebug_capacity *= scsi_debug_virtual_gb;
2746 } else
2747 sdebug_capacity = sdebug_store_sectors;
2748 return count;
2750 return -EINVAL;
2752 DRIVER_ATTR(virtual_gb, S_IRUGO | S_IWUSR, sdebug_virtual_gb_show,
2753 sdebug_virtual_gb_store);
2755 static ssize_t sdebug_add_host_show(struct device_driver * ddp, char * buf)
2757 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_add_host);
2760 static ssize_t sdebug_add_host_store(struct device_driver * ddp,
2761 const char * buf, size_t count)
2763 int delta_hosts;
2764 char work[20];
2766 if (1 != sscanf(buf, "%10s", work))
2767 return -EINVAL;
2768 { /* temporary hack around sscanf() problem with -ve nums */
2769 int neg = 0;
2771 if ('-' == *work)
2772 neg = 1;
2773 if (1 != sscanf(work + neg, "%d", &delta_hosts))
2774 return -EINVAL;
2775 if (neg)
2776 delta_hosts = -delta_hosts;
2778 if (delta_hosts > 0) {
2779 do {
2780 sdebug_add_adapter();
2781 } while (--delta_hosts);
2782 } else if (delta_hosts < 0) {
2783 do {
2784 sdebug_remove_adapter();
2785 } while (++delta_hosts);
2787 return count;
2789 DRIVER_ATTR(add_host, S_IRUGO | S_IWUSR, sdebug_add_host_show,
2790 sdebug_add_host_store);
2792 static ssize_t sdebug_vpd_use_hostno_show(struct device_driver * ddp,
2793 char * buf)
2795 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_vpd_use_hostno);
2797 static ssize_t sdebug_vpd_use_hostno_store(struct device_driver * ddp,
2798 const char * buf, size_t count)
2800 int n;
2802 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
2803 scsi_debug_vpd_use_hostno = n;
2804 return count;
2806 return -EINVAL;
2808 DRIVER_ATTR(vpd_use_hostno, S_IRUGO | S_IWUSR, sdebug_vpd_use_hostno_show,
2809 sdebug_vpd_use_hostno_store);
2811 /* Note: The following function creates attribute files in the
2812 /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
2813 files (over those found in the /sys/module/scsi_debug/parameters
2814 directory) is that auxiliary actions can be triggered when an attribute
2815 is changed. For example see: sdebug_add_host_store() above.
2817 static int do_create_driverfs_files(void)
2819 int ret;
2821 ret = driver_create_file(&sdebug_driverfs_driver, &driver_attr_add_host);
2822 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_delay);
2823 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
2824 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_dsense);
2825 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
2826 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_fake_rw);
2827 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
2828 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_no_lun_0);
2829 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_num_parts);
2830 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
2831 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_ptype);
2832 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_opts);
2833 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
2834 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_virtual_gb);
2835 ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_vpd_use_hostno);
2836 return ret;
2839 static void do_remove_driverfs_files(void)
2841 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_vpd_use_hostno);
2842 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_virtual_gb);
2843 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
2844 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_opts);
2845 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_ptype);
2846 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
2847 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_num_parts);
2848 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_no_lun_0);
2849 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
2850 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_fake_rw);
2851 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
2852 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_dsense);
2853 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
2854 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_delay);
2855 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_add_host);
2858 static int __init scsi_debug_init(void)
2860 unsigned int sz;
2861 int host_to_add;
2862 int k;
2863 int ret;
2865 if (scsi_debug_dev_size_mb < 1)
2866 scsi_debug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
2867 sdebug_store_size = (unsigned int)scsi_debug_dev_size_mb * 1048576;
2868 sdebug_store_sectors = sdebug_store_size / SECT_SIZE;
2869 if (scsi_debug_virtual_gb > 0) {
2870 sdebug_capacity = 2048 * 1024;
2871 sdebug_capacity *= scsi_debug_virtual_gb;
2872 } else
2873 sdebug_capacity = sdebug_store_sectors;
2875 /* play around with geometry, don't waste too much on track 0 */
2876 sdebug_heads = 8;
2877 sdebug_sectors_per = 32;
2878 if (scsi_debug_dev_size_mb >= 16)
2879 sdebug_heads = 32;
2880 else if (scsi_debug_dev_size_mb >= 256)
2881 sdebug_heads = 64;
2882 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
2883 (sdebug_sectors_per * sdebug_heads);
2884 if (sdebug_cylinders_per >= 1024) {
2885 /* other LLDs do this; implies >= 1GB ram disk ... */
2886 sdebug_heads = 255;
2887 sdebug_sectors_per = 63;
2888 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
2889 (sdebug_sectors_per * sdebug_heads);
2892 sz = sdebug_store_size;
2893 fake_storep = vmalloc(sz);
2894 if (NULL == fake_storep) {
2895 printk(KERN_ERR "scsi_debug_init: out of memory, 1\n");
2896 return -ENOMEM;
2898 memset(fake_storep, 0, sz);
2899 if (scsi_debug_num_parts > 0)
2900 sdebug_build_parts(fake_storep);
2902 ret = device_register(&pseudo_primary);
2903 if (ret < 0) {
2904 printk(KERN_WARNING "scsi_debug: device_register error: %d\n",
2905 ret);
2906 goto free_vm;
2908 ret = bus_register(&pseudo_lld_bus);
2909 if (ret < 0) {
2910 printk(KERN_WARNING "scsi_debug: bus_register error: %d\n",
2911 ret);
2912 goto dev_unreg;
2914 ret = driver_register(&sdebug_driverfs_driver);
2915 if (ret < 0) {
2916 printk(KERN_WARNING "scsi_debug: driver_register error: %d\n",
2917 ret);
2918 goto bus_unreg;
2920 ret = do_create_driverfs_files();
2921 if (ret < 0) {
2922 printk(KERN_WARNING "scsi_debug: driver_create_file error: %d\n",
2923 ret);
2924 goto del_files;
2927 init_all_queued();
2929 sdebug_driver_template.proc_name = sdebug_proc_name;
2931 host_to_add = scsi_debug_add_host;
2932 scsi_debug_add_host = 0;
2934 for (k = 0; k < host_to_add; k++) {
2935 if (sdebug_add_adapter()) {
2936 printk(KERN_ERR "scsi_debug_init: "
2937 "sdebug_add_adapter failed k=%d\n", k);
2938 break;
2942 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
2943 printk(KERN_INFO "scsi_debug_init: built %d host(s)\n",
2944 scsi_debug_add_host);
2946 return 0;
2948 del_files:
2949 do_remove_driverfs_files();
2950 driver_unregister(&sdebug_driverfs_driver);
2951 bus_unreg:
2952 bus_unregister(&pseudo_lld_bus);
2953 dev_unreg:
2954 device_unregister(&pseudo_primary);
2955 free_vm:
2956 vfree(fake_storep);
2958 return ret;
2961 static void __exit scsi_debug_exit(void)
2963 int k = scsi_debug_add_host;
2965 stop_all_queued();
2966 for (; k; k--)
2967 sdebug_remove_adapter();
2968 do_remove_driverfs_files();
2969 driver_unregister(&sdebug_driverfs_driver);
2970 bus_unregister(&pseudo_lld_bus);
2971 device_unregister(&pseudo_primary);
2973 vfree(fake_storep);
2976 device_initcall(scsi_debug_init);
2977 module_exit(scsi_debug_exit);
2979 static void pseudo_0_release(struct device * dev)
2981 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
2982 printk(KERN_INFO "scsi_debug: pseudo_0_release() called\n");
2985 static struct device pseudo_primary = {
2986 .bus_id = "pseudo_0",
2987 .release = pseudo_0_release,
2990 static int pseudo_lld_bus_match(struct device *dev,
2991 struct device_driver *dev_driver)
2993 return 1;
2996 static struct bus_type pseudo_lld_bus = {
2997 .name = "pseudo",
2998 .match = pseudo_lld_bus_match,
2999 .probe = sdebug_driver_probe,
3000 .remove = sdebug_driver_remove,
3003 static void sdebug_release_adapter(struct device * dev)
3005 struct sdebug_host_info *sdbg_host;
3007 sdbg_host = to_sdebug_host(dev);
3008 kfree(sdbg_host);
3011 static int sdebug_add_adapter(void)
3013 int k, devs_per_host;
3014 int error = 0;
3015 struct sdebug_host_info *sdbg_host;
3016 struct sdebug_dev_info *sdbg_devinfo;
3017 struct list_head *lh, *lh_sf;
3019 sdbg_host = kzalloc(sizeof(*sdbg_host),GFP_KERNEL);
3020 if (NULL == sdbg_host) {
3021 printk(KERN_ERR "%s: out of memory at line %d\n",
3022 __FUNCTION__, __LINE__);
3023 return -ENOMEM;
3026 INIT_LIST_HEAD(&sdbg_host->dev_info_list);
3028 devs_per_host = scsi_debug_num_tgts * scsi_debug_max_luns;
3029 for (k = 0; k < devs_per_host; k++) {
3030 sdbg_devinfo = kzalloc(sizeof(*sdbg_devinfo),GFP_KERNEL);
3031 if (NULL == sdbg_devinfo) {
3032 printk(KERN_ERR "%s: out of memory at line %d\n",
3033 __FUNCTION__, __LINE__);
3034 error = -ENOMEM;
3035 goto clean;
3037 sdbg_devinfo->sdbg_host = sdbg_host;
3038 list_add_tail(&sdbg_devinfo->dev_list,
3039 &sdbg_host->dev_info_list);
3042 spin_lock(&sdebug_host_list_lock);
3043 list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
3044 spin_unlock(&sdebug_host_list_lock);
3046 sdbg_host->dev.bus = &pseudo_lld_bus;
3047 sdbg_host->dev.parent = &pseudo_primary;
3048 sdbg_host->dev.release = &sdebug_release_adapter;
3049 sprintf(sdbg_host->dev.bus_id, "adapter%d", scsi_debug_add_host);
3051 error = device_register(&sdbg_host->dev);
3053 if (error)
3054 goto clean;
3056 ++scsi_debug_add_host;
3057 return error;
3059 clean:
3060 list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
3061 sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
3062 dev_list);
3063 list_del(&sdbg_devinfo->dev_list);
3064 kfree(sdbg_devinfo);
3067 kfree(sdbg_host);
3068 return error;
3071 static void sdebug_remove_adapter(void)
3073 struct sdebug_host_info * sdbg_host = NULL;
3075 spin_lock(&sdebug_host_list_lock);
3076 if (!list_empty(&sdebug_host_list)) {
3077 sdbg_host = list_entry(sdebug_host_list.prev,
3078 struct sdebug_host_info, host_list);
3079 list_del(&sdbg_host->host_list);
3081 spin_unlock(&sdebug_host_list_lock);
3083 if (!sdbg_host)
3084 return;
3086 device_unregister(&sdbg_host->dev);
3087 --scsi_debug_add_host;
3090 static int sdebug_driver_probe(struct device * dev)
3092 int error = 0;
3093 struct sdebug_host_info *sdbg_host;
3094 struct Scsi_Host *hpnt;
3096 sdbg_host = to_sdebug_host(dev);
3098 hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
3099 if (NULL == hpnt) {
3100 printk(KERN_ERR "%s: scsi_register failed\n", __FUNCTION__);
3101 error = -ENODEV;
3102 return error;
3105 sdbg_host->shost = hpnt;
3106 *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
3107 if ((hpnt->this_id >= 0) && (scsi_debug_num_tgts > hpnt->this_id))
3108 hpnt->max_id = scsi_debug_num_tgts + 1;
3109 else
3110 hpnt->max_id = scsi_debug_num_tgts;
3111 hpnt->max_lun = SAM2_WLUN_REPORT_LUNS; /* = scsi_debug_max_luns; */
3113 error = scsi_add_host(hpnt, &sdbg_host->dev);
3114 if (error) {
3115 printk(KERN_ERR "%s: scsi_add_host failed\n", __FUNCTION__);
3116 error = -ENODEV;
3117 scsi_host_put(hpnt);
3118 } else
3119 scsi_scan_host(hpnt);
3122 return error;
3125 static int sdebug_driver_remove(struct device * dev)
3127 struct list_head *lh, *lh_sf;
3128 struct sdebug_host_info *sdbg_host;
3129 struct sdebug_dev_info *sdbg_devinfo;
3131 sdbg_host = to_sdebug_host(dev);
3133 if (!sdbg_host) {
3134 printk(KERN_ERR "%s: Unable to locate host info\n",
3135 __FUNCTION__);
3136 return -ENODEV;
3139 scsi_remove_host(sdbg_host->shost);
3141 list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
3142 sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
3143 dev_list);
3144 list_del(&sdbg_devinfo->dev_list);
3145 kfree(sdbg_devinfo);
3148 scsi_host_put(sdbg_host->shost);
3149 return 0;
3152 static void sdebug_max_tgts_luns(void)
3154 struct sdebug_host_info * sdbg_host;
3155 struct Scsi_Host *hpnt;
3157 spin_lock(&sdebug_host_list_lock);
3158 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
3159 hpnt = sdbg_host->shost;
3160 if ((hpnt->this_id >= 0) &&
3161 (scsi_debug_num_tgts > hpnt->this_id))
3162 hpnt->max_id = scsi_debug_num_tgts + 1;
3163 else
3164 hpnt->max_id = scsi_debug_num_tgts;
3165 hpnt->max_lun = SAM2_WLUN_REPORT_LUNS; /* scsi_debug_max_luns; */
3167 spin_unlock(&sdebug_host_list_lock);