[PATCH] update copyright and licensing
[linux-2.6/history.git] / drivers / scsi / scsi_debug.c
blob870808c3f89c868ecb26dcd7efee2f62f0e26ace
1 /*
2 * linux/kernel/scsi_debug.c
3 * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
4 * Copyright (C) 1992 Eric Youngdale
5 * Simulate a host adapter with 2 disks attached. Do a lot of checking
6 * to make sure that we are not getting blocks mixed up, and PANIC if
7 * anything out of the ordinary is seen.
8 * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10 * This version is more generic, simulating a variable number of disk
11 * (or disk like devices) sharing a common amount of RAM
14 * For documentation see http://www.torque.net/sg/sdebug25.html
16 * D. Gilbert (dpg) work for Magneto-Optical device test [20010421]
17 * dpg: work for devfs large number of disks [20010809]
18 * forked for lk 2.5 series [20011216, 20020101]
19 * use vmalloc() more inquiry+mode_sense [20020302]
20 * add timers for delayed responses [20020721]
21 * Patrick Mansfield <patmans@us.ibm.com> max_luns+scsi_level [20021031]
22 * Mike Anderson <andmike@us.ibm.com> sysfs work [20021118]
23 * dpg: change style of boot options to "scsi_debug.num_tgts=2" and
24 * module options to "modprobe scsi_debug num_tgts=2" [20021221]
27 #include <linux/config.h>
28 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/errno.h>
33 #include <linux/timer.h>
34 #include <linux/types.h>
35 #include <linux/string.h>
36 #include <linux/genhd.h>
37 #include <linux/fs.h>
38 #include <linux/init.h>
39 #include <linux/proc_fs.h>
40 #include <linux/smp_lock.h>
41 #include <linux/vmalloc.h>
42 #include <linux/moduleparam.h>
44 #include <linux/blkdev.h>
45 #include "scsi.h"
46 #include "hosts.h"
47 #include <scsi/scsicam.h>
49 #include <linux/stat.h>
51 #ifndef LINUX_VERSION_CODE
52 #include <linux/version.h>
53 #endif
55 #include "scsi_logging.h"
56 #include "scsi_debug.h"
58 static const char * scsi_debug_version_str = "Version: 1.70 (20030507)";
60 /* Additional Sense Code (ASC) used */
61 #define NO_ADDED_SENSE 0x0
62 #define UNRECOVERED_READ_ERR 0x11
63 #define INVALID_OPCODE 0x20
64 #define ADDR_OUT_OF_RANGE 0x21
65 #define INVALID_FIELD_IN_CDB 0x24
66 #define POWERON_RESET 0x29
67 #define SAVING_PARAMS_UNSUP 0x39
68 #define THRESHHOLD_EXCEEDED 0x5d
70 #define SDEBUG_TAGGED_QUEUING 0 /* 0 | MSG_SIMPLE_TAG | MSG_ORDERED_TAG */
72 /* Default values for driver parameters */
73 #define DEF_NUM_HOST 1
74 #define DEF_NUM_TGTS 1
75 #define DEF_MAX_LUNS 1
76 /* With these defaults, this driver will make 1 host with 1 target
77 * (id 0) containing 1 logical unit (lun 0). That is 1 device.
79 #define DEF_DEV_SIZE_MB 8
80 #define DEF_EVERY_NTH 0
81 #define DEF_DELAY 1
82 #define DEF_SCSI_LEVEL 3
83 #define DEF_OPTS 0
85 /* bit mask values for scsi_debug_opts */
86 #define SCSI_DEBUG_OPT_NOISE 1
87 #define SCSI_DEBUG_OPT_MEDIUM_ERR 2
88 #define SCSI_DEBUG_OPT_TIMEOUT 4
89 #define SCSI_DEBUG_OPT_RECOVERED_ERR 8
90 /* When "every_nth" > 0 then modulo "every_nth" commands:
91 * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
92 * - a RECOVERED_ERROR is simulated on successful read and write
93 * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
96 /* when 1==SCSI_DEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
97 * sector on read commands: */
98 #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
100 /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
101 * or "peripheral device" addressing (value 0) */
102 #define SAM2_LUN_ADDRESS_METHOD 0
104 static int scsi_debug_dev_size_mb = DEF_DEV_SIZE_MB;
105 static int scsi_debug_num_tgts = DEF_NUM_TGTS; /* targets per host */
106 static int scsi_debug_opts = DEF_OPTS;
107 static int scsi_debug_every_nth = DEF_EVERY_NTH;
108 static int scsi_debug_cmnd_count = 0;
109 static int scsi_debug_delay = DEF_DELAY;
110 static int scsi_debug_max_luns = DEF_MAX_LUNS;
111 static int scsi_debug_scsi_level = DEF_SCSI_LEVEL;
112 static int scsi_debug_add_host = DEF_NUM_HOST;
114 #define DEV_READONLY(TGT) (0)
115 #define DEV_REMOVEABLE(TGT) (0)
116 #define PERIPH_DEVICE_TYPE(TGT) (TYPE_DISK);
118 static unsigned long sdebug_store_size; /* in bytes */
119 static sector_t sdebug_capacity; /* in sectors */
121 /* old BIOS stuff, kernel may get rid of them but some mode sense pages
122 may still need them */
123 static int sdebug_heads; /* heads per disk */
124 static int sdebug_cylinders_per; /* cylinders per surface */
125 static int sdebug_sectors_per; /* sectors per cylinder */
127 /* default sector size is 512 bytes, 2**9 bytes */
128 #define POW2_SECT_SIZE 9
129 #define SECT_SIZE (1 << POW2_SECT_SIZE)
130 #define SECT_SIZE_PER(TGT) SECT_SIZE
132 #define SDEBUG_SENSE_LEN 32
134 struct sdebug_dev_info {
135 struct list_head dev_list;
136 unsigned char sense_buff[SDEBUG_SENSE_LEN]; /* weak nexus */
137 unsigned int channel;
138 unsigned int target;
139 unsigned int lun;
140 struct sdebug_host_info *sdbg_host;
141 char reset;
142 char used;
145 struct sdebug_host_info {
146 struct list_head host_list;
147 struct Scsi_Host *shost;
148 struct device dev;
149 struct list_head dev_info_list;
152 #define to_sdebug_host(d) \
153 container_of(d, struct sdebug_host_info, dev)
155 static LIST_HEAD(sdebug_host_list);
156 static spinlock_t sdebug_host_list_lock = SPIN_LOCK_UNLOCKED;
158 typedef void (* done_funct_t) (struct scsi_cmnd *);
160 struct sdebug_queued_cmd {
161 int in_use;
162 struct timer_list cmnd_timer;
163 done_funct_t done_funct;
164 struct scsi_cmnd * a_cmnd;
165 int scsi_result;
167 static struct sdebug_queued_cmd queued_arr[SCSI_DEBUG_CANQUEUE];
169 static Scsi_Host_Template sdebug_driver_template = {
170 .proc_info = scsi_debug_proc_info,
171 .name = "SCSI DEBUG",
172 .info = scsi_debug_info,
173 .slave_alloc = scsi_debug_slave_alloc,
174 .slave_configure = scsi_debug_slave_configure,
175 .slave_destroy = scsi_debug_slave_destroy,
176 .ioctl = scsi_debug_ioctl,
177 .queuecommand = scsi_debug_queuecommand,
178 .eh_abort_handler = scsi_debug_abort,
179 .eh_bus_reset_handler = scsi_debug_bus_reset,
180 .eh_device_reset_handler = scsi_debug_device_reset,
181 .eh_host_reset_handler = scsi_debug_host_reset,
182 .bios_param = scsi_debug_biosparam,
183 .can_queue = SCSI_DEBUG_CANQUEUE,
184 .this_id = 7,
185 .sg_tablesize = 64,
186 .cmd_per_lun = 3,
187 .max_sectors = 4096,
188 .unchecked_isa_dma = 0,
189 .use_clustering = ENABLE_CLUSTERING,
190 .module = THIS_MODULE,
193 static unsigned char * fake_storep; /* ramdisk storage */
195 static unsigned char spare_buff[SDEBUG_SENSE_LEN];
197 static int num_aborts = 0;
198 static int num_dev_resets = 0;
199 static int num_bus_resets = 0;
200 static int num_host_resets = 0;
202 static spinlock_t queued_arr_lock = SPIN_LOCK_UNLOCKED;
203 static rwlock_t atomic_rw = RW_LOCK_UNLOCKED;
205 static char sdebug_proc_name[] = "scsi_debug";
207 static int sdebug_driver_probe(struct device *);
208 static int sdebug_driver_remove(struct device *);
209 static struct bus_type pseudo_lld_bus;
211 static struct device_driver sdebug_driverfs_driver = {
212 .name = sdebug_proc_name,
213 .bus = &pseudo_lld_bus,
214 .probe = sdebug_driver_probe,
215 .remove = sdebug_driver_remove,
218 static const int check_condition_result =
219 (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
221 /* function declarations */
222 static int resp_inquiry(unsigned char * cmd, int target, unsigned char * buff,
223 int bufflen, struct sdebug_dev_info * devip);
224 static int resp_mode_sense(unsigned char * cmd, int target,
225 unsigned char * buff, int bufflen,
226 struct sdebug_dev_info * devip);
227 static int resp_read(struct scsi_cmnd * SCpnt, int upper_blk, int block,
228 int num, struct sdebug_dev_info * devip);
229 static int resp_write(struct scsi_cmnd * SCpnt, int upper_blk, int block,
230 int num, struct sdebug_dev_info * devip);
231 static int resp_report_luns(unsigned char * cmd, unsigned char * buff,
232 int bufflen, struct sdebug_dev_info * devip);
233 static void timer_intr_handler(unsigned long);
234 static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev);
235 static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
236 int asc, int asq, int inbandLen);
237 static int check_reset(struct scsi_cmnd * SCpnt,
238 struct sdebug_dev_info * devip);
239 static int schedule_resp(struct scsi_cmnd * cmnd,
240 struct sdebug_dev_info * devip,
241 done_funct_t done, int scsi_result, int delta_jiff);
242 static void init_all_queued(void);
243 static void stop_all_queued(void);
244 static int stop_queued_cmnd(struct scsi_cmnd * cmnd);
245 static int inquiry_evpd_83(unsigned char * arr, int dev_id_num,
246 const char * dev_id_str, int dev_id_str_len);
247 static void do_create_driverfs_files(void);
248 static void do_remove_driverfs_files(void);
250 static int sdebug_add_adapter(void);
251 static void sdebug_remove_adapter(void);
252 static struct device pseudo_primary;
253 static struct bus_type pseudo_lld_bus;
255 static unsigned char * scatg2virt(const struct scatterlist * sclp)
257 if (NULL == sclp)
258 return NULL;
259 else if (sclp->page)
260 return (unsigned char *)page_address(sclp->page) +
261 sclp->offset;
262 else
263 return NULL;
266 static
267 int scsi_debug_queuecommand(struct scsi_cmnd * SCpnt, done_funct_t done)
269 unsigned char *cmd = (unsigned char *) SCpnt->cmnd;
270 int block, upper_blk, num;
271 unsigned char *buff;
272 int errsts = 0;
273 int target = SCpnt->device->id;
274 int bufflen = SCpnt->request_bufflen;
275 unsigned long capac;
276 struct sdebug_dev_info * devip = NULL;
277 unsigned char * sbuff;
278 int inj_recovered = 0;
280 if (done == NULL)
281 return 0; /* assume mid level reprocessing command */
283 if (SCpnt->use_sg) { /* just use first element */
284 struct scatterlist *sgpnt = (struct scatterlist *)
285 SCpnt->request_buffer;
287 buff = scatg2virt(&sgpnt[0]);
288 bufflen = sgpnt[0].length;
289 /* READ and WRITE process scatterlist themselves */
291 else
292 buff = (unsigned char *) SCpnt->request_buffer;
293 if (NULL == buff) {
294 buff = spare_buff; /* assume cmd moves no data */
295 bufflen = SDEBUG_SENSE_LEN;
299 if(target == sdebug_driver_template.this_id) {
300 printk(KERN_INFO "scsi_debug: initiator's id used as "
301 "target!\n");
302 return schedule_resp(SCpnt, NULL, done,
303 DID_NO_CONNECT << 16, 0);
306 if (SCpnt->device->lun >= scsi_debug_max_luns)
307 return schedule_resp(SCpnt, NULL, done,
308 DID_NO_CONNECT << 16, 0);
309 devip = devInfoReg(SCpnt->device);
310 if (NULL == devip)
311 return schedule_resp(SCpnt, NULL, done,
312 DID_NO_CONNECT << 16, 0);
314 if ((scsi_debug_every_nth > 0) &&
315 (++scsi_debug_cmnd_count >= scsi_debug_every_nth)) {
316 scsi_debug_cmnd_count =0;
317 if (SCSI_DEBUG_OPT_TIMEOUT & scsi_debug_opts)
318 return 0; /* ignore command causing timeout */
319 else if (SCSI_DEBUG_OPT_RECOVERED_ERR & scsi_debug_opts)
320 inj_recovered = 1; /* to reads and writes below */
323 switch (*cmd) {
324 case INQUIRY: /* mandatory */
325 errsts = resp_inquiry(cmd, target, buff, bufflen, devip);
326 break;
327 case REQUEST_SENSE: /* mandatory */
328 /* Since this driver indicates autosense by placing the
329 * sense buffer in the scsi_cmnd structure in the response
330 * (when SAM_STAT_CHECK_CONDITION is set), the mid level
331 * shouldn't need to call REQUEST_SENSE */
332 if (devip) {
333 sbuff = devip->sense_buff;
334 memcpy(buff, sbuff, (bufflen < SDEBUG_SENSE_LEN) ?
335 bufflen : SDEBUG_SENSE_LEN);
336 mk_sense_buffer(devip, 0, NO_ADDED_SENSE, 0, 7);
337 } else {
338 memset(buff, 0, bufflen);
339 buff[0] = 0x70;
341 break;
342 case START_STOP:
343 errsts = check_reset(SCpnt, devip);
344 break;
345 case ALLOW_MEDIUM_REMOVAL:
346 if ((errsts = check_reset(SCpnt, devip)))
347 break;
348 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
349 printk("\tMedium removal %s\n",
350 cmd[4] ? "inhibited" : "enabled");
351 break;
352 case SEND_DIAGNOSTIC: /* mandatory */
353 memset(buff, 0, bufflen);
354 break;
355 case TEST_UNIT_READY: /* mandatory */
356 memset(buff, 0, bufflen);
357 break;
358 case RESERVE:
359 errsts = check_reset(SCpnt, devip);
360 memset(buff, 0, bufflen);
361 break;
362 case RESERVE_10:
363 errsts = check_reset(SCpnt, devip);
364 memset(buff, 0, bufflen);
365 break;
366 case RELEASE:
367 errsts = check_reset(SCpnt, devip);
368 memset(buff, 0, bufflen);
369 break;
370 case RELEASE_10:
371 errsts = check_reset(SCpnt, devip);
372 memset(buff, 0, bufflen);
373 break;
374 case READ_CAPACITY:
375 errsts = check_reset(SCpnt, devip);
376 memset(buff, 0, bufflen);
377 if (bufflen > 7) {
378 capac = (unsigned long)sdebug_capacity - 1;
379 buff[0] = (capac >> 24);
380 buff[1] = (capac >> 16) & 0xff;
381 buff[2] = (capac >> 8) & 0xff;
382 buff[3] = capac & 0xff;
383 buff[6] = (SECT_SIZE_PER(target) >> 8) & 0xff;
384 buff[7] = SECT_SIZE_PER(target) & 0xff;
386 break;
387 case READ_16:
388 case READ_12:
389 case READ_10:
390 case READ_6:
391 if ((errsts = check_reset(SCpnt, devip)))
392 break;
393 upper_blk = 0;
394 if ((*cmd) == READ_16) {
395 upper_blk = cmd[5] + (cmd[4] << 8) +
396 (cmd[3] << 16) + (cmd[2] << 24);
397 block = cmd[9] + (cmd[8] << 8) +
398 (cmd[7] << 16) + (cmd[6] << 24);
399 num = cmd[13] + (cmd[12] << 8) +
400 (cmd[11] << 16) + (cmd[10] << 24);
401 } else if ((*cmd) == READ_12) {
402 block = cmd[5] + (cmd[4] << 8) +
403 (cmd[3] << 16) + (cmd[2] << 24);
404 num = cmd[9] + (cmd[8] << 8) +
405 (cmd[7] << 16) + (cmd[6] << 24);
406 } else if ((*cmd) == READ_10) {
407 block = cmd[5] + (cmd[4] << 8) +
408 (cmd[3] << 16) + (cmd[2] << 24);
409 num = cmd[8] + (cmd[7] << 8);
410 } else {
411 block = cmd[3] + (cmd[2] << 8) +
412 ((cmd[1] & 0x1f) << 16);
413 num = cmd[4];
415 errsts = resp_read(SCpnt, upper_blk, block, num, devip);
416 if (inj_recovered && (0 == errsts)) {
417 mk_sense_buffer(devip, RECOVERED_ERROR,
418 THRESHHOLD_EXCEEDED, 0, 18);
419 errsts = check_condition_result;
421 break;
422 case REPORT_LUNS:
423 errsts = resp_report_luns(cmd, buff, bufflen, devip);
424 break;
425 case WRITE_16:
426 case WRITE_12:
427 case WRITE_10:
428 case WRITE_6:
429 if ((errsts = check_reset(SCpnt, devip)))
430 break;
431 upper_blk = 0;
432 if ((*cmd) == WRITE_16) {
433 upper_blk = cmd[5] + (cmd[4] << 8) +
434 (cmd[3] << 16) + (cmd[2] << 24);
435 block = cmd[9] + (cmd[8] << 8) +
436 (cmd[7] << 16) + (cmd[6] << 24);
437 num = cmd[13] + (cmd[12] << 8) +
438 (cmd[11] << 16) + (cmd[10] << 24);
439 } else if ((*cmd) == WRITE_12) {
440 block = cmd[5] + (cmd[4] << 8) +
441 (cmd[3] << 16) + (cmd[2] << 24);
442 num = cmd[9] + (cmd[8] << 8) +
443 (cmd[7] << 16) + (cmd[6] << 24);
444 } else if ((*cmd) == WRITE_10) {
445 block = cmd[5] + (cmd[4] << 8) +
446 (cmd[3] << 16) + (cmd[2] << 24);
447 num = cmd[8] + (cmd[7] << 8);
448 } else {
449 block = cmd[3] + (cmd[2] << 8) +
450 ((cmd[1] & 0x1f) << 16);
451 num = cmd[4];
453 errsts = resp_write(SCpnt, upper_blk, block, num, devip);
454 if (inj_recovered && (0 == errsts)) {
455 mk_sense_buffer(devip, RECOVERED_ERROR,
456 THRESHHOLD_EXCEEDED, 0, 18);
457 errsts = check_condition_result;
459 break;
460 case MODE_SENSE:
461 case MODE_SENSE_10:
462 errsts = resp_mode_sense(cmd, target, buff, bufflen, devip);
463 break;
464 case SYNCHRONIZE_CACHE:
465 memset(buff, 0, bufflen);
466 break;
467 default:
468 if ((errsts = check_reset(SCpnt, devip)))
469 break;
470 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_OPCODE, 0, 18);
471 errsts = check_condition_result;
472 break;
474 return schedule_resp(SCpnt, devip, done, errsts, scsi_debug_delay);
477 static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void *arg)
479 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
480 printk(KERN_INFO "scsi_debug: ioctl: cmd=0x%x\n", cmd);
482 return -EINVAL;
483 /* return -ENOTTY; // correct return but upsets fdisk */
486 static int check_reset(struct scsi_cmnd * SCpnt, struct sdebug_dev_info * devip)
488 if (devip->reset) {
489 devip->reset = 0;
490 mk_sense_buffer(devip, UNIT_ATTENTION, POWERON_RESET, 0, 18);
491 return check_condition_result;
493 return 0;
496 #define SDEBUG_LONG_INQ_SZ 58
497 #define SDEBUG_MAX_INQ_ARR_SZ 128
499 static const char * vendor_id = "Linux ";
500 static const char * product_id = "scsi_debug ";
501 static const char * product_rev = "0004";
503 static int inquiry_evpd_83(unsigned char * arr, int dev_id_num,
504 const char * dev_id_str, int dev_id_str_len)
506 int num;
508 /* Two identification descriptors: */
509 /* T10 vendor identifier field format (faked) */
510 arr[0] = 0x2; /* ASCII */
511 arr[1] = 0x1;
512 arr[2] = 0x0;
513 memcpy(&arr[4], vendor_id, 8);
514 memcpy(&arr[12], product_id, 16);
515 memcpy(&arr[28], dev_id_str, dev_id_str_len);
516 num = 8 + 16 + dev_id_str_len;
517 arr[3] = num;
518 num += 4;
519 /* NAA IEEE registered identifier (faked) */
520 arr[num] = 0x1; /* binary */
521 arr[num + 1] = 0x3;
522 arr[num + 2] = 0x0;
523 arr[num + 3] = 0x8;
524 arr[num + 4] = 0x51; /* ieee company id=0x123456 (faked) */
525 arr[num + 5] = 0x23;
526 arr[num + 6] = 0x45;
527 arr[num + 7] = 0x60;
528 arr[num + 8] = (dev_id_num >> 24);
529 arr[num + 9] = (dev_id_num >> 16) & 0xff;
530 arr[num + 10] = (dev_id_num >> 8) & 0xff;
531 arr[num + 11] = dev_id_num & 0xff;
532 return num + 12;
535 static int resp_inquiry(unsigned char * cmd, int target, unsigned char * buff,
536 int bufflen, struct sdebug_dev_info * devip)
538 unsigned char pq_pdt;
539 unsigned char arr[SDEBUG_MAX_INQ_ARR_SZ];
540 int min_len = bufflen > SDEBUG_MAX_INQ_ARR_SZ ?
541 SDEBUG_MAX_INQ_ARR_SZ : bufflen;
543 if (bufflen < cmd[4])
544 printk(KERN_INFO "scsi_debug: inquiry: bufflen=%d "
545 "< alloc_length=%d\n", bufflen, (int)cmd[4]);
546 memset(buff, 0, bufflen);
547 memset(arr, 0, SDEBUG_MAX_INQ_ARR_SZ);
548 pq_pdt = PERIPH_DEVICE_TYPE(target);
549 arr[0] = pq_pdt;
550 if (0x2 & cmd[1]) { /* CMDDT bit set */
551 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
552 0, 18);
553 return check_condition_result;
554 } else if (0x1 & cmd[1]) { /* EVPD bit set */
555 int dev_id_num, len;
556 char dev_id_str[6];
558 dev_id_num = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
559 (devip->target * 1000) + devip->lun;
560 len = snprintf(dev_id_str, 6, "%d", dev_id_num);
561 len = (len > 6) ? 6 : len;
562 if (0 == cmd[2]) { /* supported vital product data pages */
563 arr[3] = 3;
564 arr[4] = 0x0; /* this page */
565 arr[5] = 0x80; /* unit serial number */
566 arr[6] = 0x83; /* device identification */
567 } else if (0x80 == cmd[2]) { /* unit serial number */
568 arr[1] = 0x80;
569 arr[3] = len;
570 memcpy(&arr[4], dev_id_str, len);
571 } else if (0x83 == cmd[2]) { /* device identification */
572 arr[1] = 0x83;
573 arr[3] = inquiry_evpd_83(&arr[4], dev_id_num,
574 dev_id_str, len);
575 } else {
576 /* Illegal request, invalid field in cdb */
577 mk_sense_buffer(devip, ILLEGAL_REQUEST,
578 INVALID_FIELD_IN_CDB, 0, 18);
579 return check_condition_result;
581 memcpy(buff, arr, min_len);
582 return 0;
584 /* drops through here for a standard inquiry */
585 arr[1] = DEV_REMOVEABLE(target) ? 0x80 : 0; /* Removable disk */
586 arr[2] = scsi_debug_scsi_level;
587 arr[4] = SDEBUG_LONG_INQ_SZ - 5;
588 arr[7] = 0x3a; /* claim: WBUS16, SYNC, LINKED + CMDQUE */
589 memcpy(&arr[8], vendor_id, 8);
590 memcpy(&arr[16], product_id, 16);
591 memcpy(&arr[32], product_rev, 4);
592 memcpy(buff, arr, min_len);
593 return 0;
596 /* <<Following mode page info copied from ST318451LW>> */
598 static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target)
599 { /* Read-Write Error Recovery page for mode_sense */
600 unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
601 5, 0, 0xff, 0xff};
603 memcpy(p, err_recov_pg, sizeof(err_recov_pg));
604 if (1 == pcontrol)
605 memset(p + 2, 0, sizeof(err_recov_pg) - 2);
606 return sizeof(err_recov_pg);
609 static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target)
610 { /* Disconnect-Reconnect page for mode_sense */
611 unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
612 0, 0, 0, 0, 0, 0, 0, 0};
614 memcpy(p, disconnect_pg, sizeof(disconnect_pg));
615 if (1 == pcontrol)
616 memset(p + 2, 0, sizeof(disconnect_pg) - 2);
617 return sizeof(disconnect_pg);
620 static int resp_format_pg(unsigned char * p, int pcontrol, int target)
621 { /* Format device page for mode_sense */
622 unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
623 0, 0, 0, 0, 0, 0, 0, 0,
624 0, 0, 0, 0, 0x40, 0, 0, 0};
626 memcpy(p, format_pg, sizeof(format_pg));
627 p[10] = (sdebug_sectors_per >> 8) & 0xff;
628 p[11] = sdebug_sectors_per & 0xff;
629 p[12] = (SECT_SIZE >> 8) & 0xff;
630 p[13] = SECT_SIZE & 0xff;
631 if (DEV_REMOVEABLE(target))
632 p[20] |= 0x20; /* should agree with INQUIRY */
633 if (1 == pcontrol)
634 memset(p + 2, 0, sizeof(format_pg) - 2);
635 return sizeof(format_pg);
638 static int resp_caching_pg(unsigned char * p, int pcontrol, int target)
639 { /* Caching page for mode_sense */
640 unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
641 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
643 memcpy(p, caching_pg, sizeof(caching_pg));
644 if (1 == pcontrol)
645 memset(p + 2, 0, sizeof(caching_pg) - 2);
646 return sizeof(caching_pg);
649 static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target)
650 { /* Control mode page for mode_sense */
651 unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
652 0, 0, 0x2, 0x4b};
654 memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
655 if (1 == pcontrol)
656 memset(p + 2, 0, sizeof(ctrl_m_pg) - 2);
657 return sizeof(ctrl_m_pg);
660 static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target)
661 { /* Informational Exceptions control mode page for mode_sense */
662 unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
663 0, 0, 0x0, 0x0};
664 memcpy(p, iec_m_pg, sizeof(iec_m_pg));
665 if (1 == pcontrol)
666 memset(p + 2, 0, sizeof(iec_m_pg) - 2);
667 return sizeof(iec_m_pg);
670 #define SDEBUG_MAX_MSENSE_SZ 256
672 static int resp_mode_sense(unsigned char * cmd, int target,
673 unsigned char * buff, int bufflen,
674 struct sdebug_dev_info * devip)
676 unsigned char dbd;
677 int pcontrol, pcode;
678 unsigned char dev_spec;
679 int alloc_len, msense_6, offset, len;
680 unsigned char * ap;
681 unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
682 int min_len = bufflen > SDEBUG_MAX_MSENSE_SZ ?
683 SDEBUG_MAX_MSENSE_SZ : bufflen;
685 SCSI_LOG_LLQUEUE(3, printk("Mode sense ...(%p %d)\n", buff, bufflen));
686 dbd = cmd[1] & 0x8;
687 pcontrol = (cmd[2] & 0xc0) >> 6;
688 pcode = cmd[2] & 0x3f;
689 msense_6 = (MODE_SENSE == cmd[0]);
690 alloc_len = msense_6 ? cmd[4] : ((cmd[7] << 8) | cmd[8]);
691 /* printk(KERN_INFO "msense: dbd=%d pcontrol=%d pcode=%d "
692 "msense_6=%d alloc_len=%d\n", dbd, pcontrol, pcode, "
693 "msense_6, alloc_len); */
694 if (bufflen < alloc_len)
695 printk(KERN_INFO "scsi_debug: mode_sense: bufflen=%d "
696 "< alloc_length=%d\n", bufflen, alloc_len);
697 memset(buff, 0, bufflen);
698 memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
699 if (0x3 == pcontrol) { /* Saving values not supported */
700 mk_sense_buffer(devip, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP,
701 0, 18);
702 return check_condition_result;
704 dev_spec = DEV_READONLY(target) ? 0x80 : 0x0;
705 if (msense_6) {
706 arr[2] = dev_spec;
707 offset = 4;
708 } else {
709 arr[3] = dev_spec;
710 offset = 8;
712 ap = arr + offset;
714 switch (pcode) {
715 case 0x1: /* Read-Write error recovery page, direct access */
716 len = resp_err_recov_pg(ap, pcontrol, target);
717 offset += len;
718 break;
719 case 0x2: /* Disconnect-Reconnect page, all devices */
720 len = resp_disconnect_pg(ap, pcontrol, target);
721 offset += len;
722 break;
723 case 0x3: /* Format device page, direct access */
724 len = resp_format_pg(ap, pcontrol, target);
725 offset += len;
726 break;
727 case 0x8: /* Caching page, direct access */
728 len = resp_caching_pg(ap, pcontrol, target);
729 offset += len;
730 break;
731 case 0xa: /* Control Mode page, all devices */
732 len = resp_ctrl_m_pg(ap, pcontrol, target);
733 offset += len;
734 break;
735 case 0x1c: /* Informational Exceptions Mode page, all devices */
736 len = resp_iec_m_pg(ap, pcontrol, target);
737 offset += len;
738 break;
739 case 0x3f: /* Read all Mode pages */
740 len = resp_err_recov_pg(ap, pcontrol, target);
741 len += resp_disconnect_pg(ap + len, pcontrol, target);
742 len += resp_format_pg(ap + len, pcontrol, target);
743 len += resp_caching_pg(ap + len, pcontrol, target);
744 len += resp_ctrl_m_pg(ap + len, pcontrol, target);
745 len += resp_iec_m_pg(ap + len, pcontrol, target);
746 offset += len;
747 break;
748 default:
749 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
750 0, 18);
751 return check_condition_result;
753 if (msense_6)
754 arr[0] = offset - 1;
755 else {
756 offset -= 2;
757 arr[0] = (offset >> 8) & 0xff;
758 arr[1] = offset & 0xff;
760 memcpy(buff, arr, min_len);
761 return 0;
764 static int resp_read(struct scsi_cmnd * SCpnt, int upper_blk, int block,
765 int num, struct sdebug_dev_info * devip)
767 unsigned char *buff = (unsigned char *) SCpnt->request_buffer;
768 int nbytes, sgcount;
769 struct scatterlist *sgpnt = NULL;
770 int bufflen = SCpnt->request_bufflen;
771 unsigned long iflags;
773 if (upper_blk || (block + num > sdebug_capacity)) {
774 mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
775 0, 18);
776 return check_condition_result;
778 if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) &&
779 (block <= OPT_MEDIUM_ERR_ADDR) &&
780 ((block + num) > OPT_MEDIUM_ERR_ADDR)) {
781 mk_sense_buffer(devip, MEDIUM_ERROR, UNRECOVERED_READ_ERR,
782 0, 18);
783 /* claim unrecoverable read error */
784 return check_condition_result;
786 read_lock_irqsave(&atomic_rw, iflags);
787 sgcount = 0;
788 nbytes = bufflen;
789 /* printk(KERN_INFO "scsi_debug_read: block=%d, tot_bufflen=%d\n",
790 block, bufflen); */
791 if (SCpnt->use_sg) {
792 sgcount = 0;
793 sgpnt = (struct scatterlist *) buff;
794 buff = scatg2virt(&sgpnt[sgcount]);
795 bufflen = sgpnt[sgcount].length;
797 do {
798 memcpy(buff, fake_storep + (block * SECT_SIZE), bufflen);
799 nbytes -= bufflen;
800 if (SCpnt->use_sg) {
801 block += bufflen >> POW2_SECT_SIZE;
802 sgcount++;
803 if (nbytes) {
804 buff = scatg2virt(&sgpnt[sgcount]);
805 bufflen = sgpnt[sgcount].length;
807 } else if (nbytes > 0)
808 printk(KERN_WARNING "scsi_debug:resp_read: unexpected "
809 "nbytes=%d\n", nbytes);
810 } while (nbytes);
811 read_unlock_irqrestore(&atomic_rw, iflags);
812 return 0;
815 static int resp_write(struct scsi_cmnd * SCpnt, int upper_blk, int block,
816 int num, struct sdebug_dev_info * devip)
818 unsigned char *buff = (unsigned char *) SCpnt->request_buffer;
819 int nbytes, sgcount;
820 struct scatterlist *sgpnt = NULL;
821 int bufflen = SCpnt->request_bufflen;
822 unsigned long iflags;
824 if (upper_blk || (block + num > sdebug_capacity)) {
825 mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
826 0, 18);
827 return check_condition_result;
830 write_lock_irqsave(&atomic_rw, iflags);
831 sgcount = 0;
832 nbytes = bufflen;
833 if (SCpnt->use_sg) {
834 sgcount = 0;
835 sgpnt = (struct scatterlist *) buff;
836 buff = scatg2virt(&sgpnt[sgcount]);
837 bufflen = sgpnt[sgcount].length;
839 do {
840 memcpy(fake_storep + (block * SECT_SIZE), buff, bufflen);
842 nbytes -= bufflen;
843 if (SCpnt->use_sg) {
844 block += bufflen >> POW2_SECT_SIZE;
845 sgcount++;
846 if (nbytes) {
847 buff = scatg2virt(&sgpnt[sgcount]);
848 bufflen = sgpnt[sgcount].length;
850 } else if (nbytes > 0)
851 printk(KERN_WARNING "scsi_debug:resp_write: "
852 "unexpected nbytes=%d\n", nbytes);
853 } while (nbytes);
854 write_unlock_irqrestore(&atomic_rw, iflags);
855 return 0;
858 static int resp_report_luns(unsigned char * cmd, unsigned char * buff,
859 int bufflen, struct sdebug_dev_info * devip)
861 unsigned int alloc_len;
862 int lun_cnt, i, upper;
863 int select_report = (int)cmd[2];
864 struct scsi_lun *one_lun;
866 alloc_len = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24);
867 if ((alloc_len < 16) || (select_report > 2)) {
868 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
869 0, 18);
870 return check_condition_result;
872 if (bufflen > 8) { /* can produce response with up to 16k luns
873 (lun 0 to lun 16383) */
874 memset(buff, 0, bufflen);
875 lun_cnt = scsi_debug_max_luns;
876 buff[2] = ((sizeof(struct scsi_lun) * lun_cnt) >> 8) & 0xff;
877 buff[3] = (sizeof(struct scsi_lun) * lun_cnt) & 0xff;
878 lun_cnt = min((int)((bufflen - 8) / sizeof(struct scsi_lun)),
879 lun_cnt);
880 one_lun = (struct scsi_lun *) &buff[8];
881 for (i = 0; i < lun_cnt; i++) {
882 upper = (i >> 8) & 0x3f;
883 if (upper)
884 one_lun[i].scsi_lun[0] =
885 (upper | (SAM2_LUN_ADDRESS_METHOD << 6));
886 one_lun[i].scsi_lun[1] = i & 0xff;
889 return 0;
892 /* When timer goes off this function is called. */
893 static void timer_intr_handler(unsigned long indx)
895 struct sdebug_queued_cmd * sqcp;
896 unsigned long iflags;
898 if (indx >= SCSI_DEBUG_CANQUEUE) {
899 printk(KERN_ERR "scsi_debug:timer_intr_handler: indx too "
900 "large\n");
901 return;
903 spin_lock_irqsave(&queued_arr_lock, iflags);
904 sqcp = &queued_arr[(int)indx];
905 if (! sqcp->in_use) {
906 printk(KERN_ERR "scsi_debug:timer_intr_handler: Unexpected "
907 "interrupt\n");
908 spin_unlock_irqrestore(&queued_arr_lock, iflags);
909 return;
911 sqcp->in_use = 0;
912 if (sqcp->done_funct) {
913 sqcp->a_cmnd->result = sqcp->scsi_result;
914 sqcp->done_funct(sqcp->a_cmnd); /* callback to mid level */
916 sqcp->done_funct = NULL;
917 spin_unlock_irqrestore(&queued_arr_lock, iflags);
920 static int scsi_debug_slave_alloc(struct scsi_device * sdp)
922 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
923 printk(KERN_INFO "scsi_debug: slave_alloc <%u %u %u %u>\n",
924 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
925 return 0;
928 static int scsi_debug_slave_configure(struct scsi_device * sdp)
930 struct sdebug_dev_info * devip;
932 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
933 printk(KERN_INFO "scsi_debug: slave_configure <%u %u %u %u>\n",
934 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
935 if (sdp->host->max_cmd_len != SCSI_DEBUG_MAX_CMD_LEN)
936 sdp->host->max_cmd_len = SCSI_DEBUG_MAX_CMD_LEN;
937 devip = devInfoReg(sdp);
938 sdp->hostdata = devip;
939 if (sdp->host->cmd_per_lun)
940 scsi_adjust_queue_depth(sdp, SDEBUG_TAGGED_QUEUING,
941 sdp->host->cmd_per_lun);
942 return 0;
945 static void scsi_debug_slave_destroy(struct scsi_device * sdp)
947 struct sdebug_dev_info * devip =
948 (struct sdebug_dev_info *)sdp->hostdata;
950 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
951 printk(KERN_INFO "scsi_debug: slave_destroy <%u %u %u %u>\n",
952 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
953 if (devip) {
954 /* make this slot avaliable for re-use */
955 devip->used = 0;
956 sdp->hostdata = NULL;
960 static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev)
962 struct sdebug_host_info * sdbg_host;
963 struct sdebug_dev_info * open_devip = NULL;
964 struct sdebug_dev_info * devip =
965 (struct sdebug_dev_info *)sdev->hostdata;
967 if (devip)
968 return devip;
969 sdbg_host = *(struct sdebug_host_info **) sdev->host->hostdata;
970 if(! sdbg_host) {
971 printk(KERN_ERR "Host info NULL\n");
972 return NULL;
974 list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
975 if ((devip->used) && (devip->channel == sdev->channel) &&
976 (devip->target == sdev->id) &&
977 (devip->lun == sdev->lun))
978 return devip;
979 else {
980 if ((!devip->used) && (!open_devip))
981 open_devip = devip;
984 if (open_devip) {
985 open_devip->channel = sdev->channel;
986 open_devip->target = sdev->id;
987 open_devip->lun = sdev->lun;
988 open_devip->sdbg_host = sdbg_host;
989 open_devip->reset = 1;
990 open_devip->used = 1;
991 memset(open_devip->sense_buff, 0, SDEBUG_SENSE_LEN);
992 open_devip->sense_buff[0] = 0x70;
993 return open_devip;
995 return NULL;
998 static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
999 int asc, int asq, int inbandLen)
1001 unsigned char * sbuff;
1003 sbuff = devip->sense_buff;
1004 memset(sbuff, 0, SDEBUG_SENSE_LEN);
1005 if (inbandLen > SDEBUG_SENSE_LEN)
1006 inbandLen = SDEBUG_SENSE_LEN;
1007 sbuff[0] = 0x70;
1008 sbuff[2] = key;
1009 sbuff[7] = (inbandLen > 7) ? (inbandLen - 8) : 0;
1010 sbuff[12] = asc;
1011 sbuff[13] = asq;
1014 static int scsi_debug_abort(struct scsi_cmnd * SCpnt)
1016 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1017 printk(KERN_INFO "scsi_debug: abort\n");
1018 ++num_aborts;
1019 stop_queued_cmnd(SCpnt);
1020 return SUCCESS;
1023 static int scsi_debug_biosparam(struct scsi_device *sdev,
1024 struct block_device * bdev, sector_t capacity, int *info)
1026 int res;
1027 unsigned char *buf;
1029 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1030 printk(KERN_INFO "scsi_debug: biosparam\n");
1031 buf = scsi_bios_ptable(bdev);
1032 if (buf) {
1033 res = scsi_partsize(buf, capacity,
1034 &info[2], &info[0], &info[1]);
1035 kfree(buf);
1036 if (! res)
1037 return res;
1039 info[0] = sdebug_heads;
1040 info[1] = sdebug_sectors_per;
1041 info[2] = sdebug_cylinders_per;
1042 return 0;
1045 static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt)
1047 struct sdebug_dev_info * devip;
1049 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1050 printk(KERN_INFO "scsi_debug: device_reset\n");
1051 ++num_dev_resets;
1052 if (SCpnt) {
1053 devip = devInfoReg(SCpnt->device);
1054 if (devip)
1055 devip->reset = 1;
1057 return SUCCESS;
1060 static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt)
1062 struct sdebug_host_info *sdbg_host;
1063 struct sdebug_dev_info * dev_info;
1064 struct scsi_device * sdp;
1065 struct Scsi_Host * hp;
1067 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1068 printk(KERN_INFO "scsi_debug: bus_reset\n");
1069 ++num_bus_resets;
1070 if (SCpnt && ((sdp = SCpnt->device)) && ((hp = sdp->host))) {
1071 sdbg_host = *(struct sdebug_host_info **) hp->hostdata;
1072 if (sdbg_host) {
1073 list_for_each_entry(dev_info,
1074 &sdbg_host->dev_info_list,
1075 dev_list)
1076 dev_info->reset = 1;
1079 return SUCCESS;
1082 static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt)
1084 struct sdebug_host_info * sdbg_host;
1085 struct sdebug_dev_info * dev_info;
1087 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1088 printk(KERN_INFO "scsi_debug: host_reset\n");
1089 ++num_host_resets;
1090 spin_lock(&sdebug_host_list_lock);
1091 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
1092 list_for_each_entry(dev_info, &sdbg_host->dev_info_list,
1093 dev_list)
1094 dev_info->reset = 1;
1096 spin_unlock(&sdebug_host_list_lock);
1097 stop_all_queued();
1098 return SUCCESS;
1101 /* Returns 1 if found 'cmnd' and deleted its timer. else returns 0 */
1102 static int stop_queued_cmnd(struct scsi_cmnd * cmnd)
1104 unsigned long iflags;
1105 int k;
1106 struct sdebug_queued_cmd * sqcp;
1108 spin_lock_irqsave(&queued_arr_lock, iflags);
1109 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
1110 sqcp = &queued_arr[k];
1111 if (sqcp->in_use && (cmnd == sqcp->a_cmnd)) {
1112 del_timer_sync(&sqcp->cmnd_timer);
1113 sqcp->in_use = 0;
1114 sqcp->a_cmnd = NULL;
1115 break;
1118 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1119 return (k < SCSI_DEBUG_CANQUEUE) ? 1 : 0;
1122 /* Deletes (stops) timers of all queued commands */
1123 static void stop_all_queued(void)
1125 unsigned long iflags;
1126 int k;
1127 struct sdebug_queued_cmd * sqcp;
1129 spin_lock_irqsave(&queued_arr_lock, iflags);
1130 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
1131 sqcp = &queued_arr[k];
1132 if (sqcp->in_use && sqcp->a_cmnd) {
1133 del_timer_sync(&sqcp->cmnd_timer);
1134 sqcp->in_use = 0;
1135 sqcp->a_cmnd = NULL;
1138 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1141 /* Initializes timers in queued array */
1142 static void init_all_queued(void)
1144 unsigned long iflags;
1145 int k;
1146 struct sdebug_queued_cmd * sqcp;
1148 spin_lock_irqsave(&queued_arr_lock, iflags);
1149 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
1150 sqcp = &queued_arr[k];
1151 init_timer(&sqcp->cmnd_timer);
1152 sqcp->in_use = 0;
1153 sqcp->a_cmnd = NULL;
1155 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1158 static int schedule_resp(struct scsi_cmnd * cmnd,
1159 struct sdebug_dev_info * devip,
1160 done_funct_t done, int scsi_result, int delta_jiff)
1162 int k, num;
1164 if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmnd) {
1165 printk(KERN_INFO "scsi_debug: cmd ");
1166 for (k = 0, num = cmnd->cmd_len; k < num; ++k)
1167 printk("%02x ", (int)cmnd->cmnd[k]);
1168 printk("\n");
1169 if (scsi_result) {
1170 struct scsi_device * sdp = cmnd->device;
1172 printk(KERN_INFO "scsi_debug: ... <%u %u %u %u> "
1173 "non-zero result=0x%x\n", sdp->host->host_no,
1174 sdp->channel, sdp->id, sdp->lun, scsi_result);
1177 if (cmnd && devip) {
1178 /* simulate autosense by this driver */
1179 if (SAM_STAT_CHECK_CONDITION == (scsi_result & 0xff))
1180 memcpy(cmnd->sense_buffer, devip->sense_buff,
1181 (SCSI_SENSE_BUFFERSIZE > SDEBUG_SENSE_LEN) ?
1182 SDEBUG_SENSE_LEN : SCSI_SENSE_BUFFERSIZE);
1184 if (delta_jiff <= 0) {
1185 if (cmnd)
1186 cmnd->result = scsi_result;
1187 if (done)
1188 done(cmnd);
1189 return 0;
1190 } else {
1191 unsigned long iflags;
1192 int k;
1193 struct sdebug_queued_cmd * sqcp = NULL;
1195 spin_lock_irqsave(&queued_arr_lock, iflags);
1196 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
1197 sqcp = &queued_arr[k];
1198 if (! sqcp->in_use)
1199 break;
1201 if (k >= SCSI_DEBUG_CANQUEUE) {
1202 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1203 printk(KERN_WARNING "scsi_debug: can_queue exceeded\n");
1204 return 1; /* report busy to mid level */
1206 sqcp->in_use = 1;
1207 sqcp->a_cmnd = cmnd;
1208 sqcp->scsi_result = scsi_result;
1209 sqcp->done_funct = done;
1210 sqcp->cmnd_timer.function = timer_intr_handler;
1211 sqcp->cmnd_timer.data = k;
1212 sqcp->cmnd_timer.expires = jiffies + delta_jiff;
1213 add_timer(&sqcp->cmnd_timer);
1214 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1215 if (cmnd)
1216 cmnd->result = 0;
1217 return 0;
1221 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
1222 * of sysfs parameters (which module_param doesn't yet support).
1223 * Sysfs parameters defined explicitly below.
1225 module_param_named(num_tgts, scsi_debug_num_tgts, int, 0);
1226 module_param_named(max_luns, scsi_debug_max_luns, int, 0);
1227 module_param_named(scsi_level, scsi_debug_scsi_level, int, 0);
1228 module_param_named(dev_size_mb, scsi_debug_dev_size_mb, int, 0);
1229 module_param_named(opts, scsi_debug_opts, int, 0); /* perm=0644 */
1230 module_param_named(every_nth, scsi_debug_every_nth, int, 0);
1231 module_param_named(delay, scsi_debug_delay, int, 0); /* perm=0644 */
1232 module_param_named(add_host, scsi_debug_add_host, int, 0); /* perm=0644 */
1234 MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
1235 MODULE_DESCRIPTION("SCSI debug adapter driver");
1236 MODULE_LICENSE("GPL");
1238 MODULE_PARM_DESC(num_tgts, "number of SCSI targets per host to simulate");
1239 MODULE_PARM_DESC(max_luns, "number of SCSI LUNs per target to simulate");
1240 MODULE_PARM_DESC(scsi_level, "SCSI level to simulate");
1241 MODULE_PARM_DESC(dev_size_mb, "size in MB of ram shared by devs");
1242 MODULE_PARM_DESC(opts, "1->noise, 2->medium_error, 4->...");
1243 MODULE_PARM_DESC(every_nth, "timeout every nth command(def=100)");
1244 MODULE_PARM_DESC(delay, "# of jiffies to delay response(def=1)");
1245 MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
1248 static char sdebug_info[256];
1250 static const char * scsi_debug_info(struct Scsi_Host * shp)
1252 sprintf(sdebug_info, "scsi_debug, %s, num_tgts=%d, "
1253 "dev_size_mb=%d, opts=0x%x", scsi_debug_version_str,
1254 scsi_debug_num_tgts, scsi_debug_dev_size_mb,
1255 scsi_debug_opts);
1256 return sdebug_info;
1259 /* scsi_debug_proc_info
1260 * Used if the driver currently has no own support for /proc/scsi
1262 static int scsi_debug_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
1263 int length, int inout)
1265 int len, pos, begin;
1266 int orig_length;
1268 orig_length = length;
1270 if (inout == 1) {
1271 char arr[16];
1272 int minLen = length > 15 ? 15 : length;
1274 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1275 return -EACCES;
1276 memcpy(arr, buffer, minLen);
1277 arr[minLen] = '\0';
1278 if (1 != sscanf(arr, "%d", &pos))
1279 return -EINVAL;
1280 scsi_debug_opts = pos;
1281 if (scsi_debug_every_nth > 0)
1282 scsi_debug_cmnd_count = 0;
1283 return length;
1285 begin = 0;
1286 pos = len = sprintf(buffer, "scsi_debug adapter driver, %s\n"
1287 "num_tgts=%d, shared (ram) size=%d MB, opts=0x%x, "
1288 "every_nth=%d(curr:%d)\n"
1289 "delay=%d, max_luns=%d, scsi_level=%d\n"
1290 "sector_size=%d bytes, cylinders=%d, heads=%d, sectors=%d\n"
1291 "number of aborts=%d, device_reset=%d, bus_resets=%d, "
1292 "host_resets=%d\n",
1293 scsi_debug_version_str, scsi_debug_num_tgts,
1294 scsi_debug_dev_size_mb, scsi_debug_opts, scsi_debug_every_nth,
1295 scsi_debug_cmnd_count, scsi_debug_delay,
1296 scsi_debug_max_luns, scsi_debug_scsi_level,
1297 SECT_SIZE, sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
1298 num_aborts, num_dev_resets, num_bus_resets, num_host_resets);
1299 if (pos < offset) {
1300 len = 0;
1301 begin = pos;
1303 *start = buffer + (offset - begin); /* Start of wanted data */
1304 len -= (offset - begin);
1305 if (len > length)
1306 len = length;
1307 return len;
1310 static ssize_t sdebug_delay_show(struct device_driver * ddp, char * buf)
1312 return snprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_delay);
1315 static ssize_t sdebug_delay_store(struct device_driver * ddp,
1316 const char * buf, size_t count)
1318 int delay;
1319 char work[20];
1321 if (1 == sscanf(buf, "%10s", work)) {
1322 if ((1 == sscanf(work, "%d", &delay)) && (delay >= 0)) {
1323 scsi_debug_delay = delay;
1324 return count;
1327 return -EINVAL;
1329 DRIVER_ATTR(delay, S_IRUGO | S_IWUSR, sdebug_delay_show,
1330 sdebug_delay_store)
1332 static ssize_t sdebug_opts_show(struct device_driver * ddp, char * buf)
1334 return snprintf(buf, PAGE_SIZE, "0x%x\n", scsi_debug_opts);
1337 static ssize_t sdebug_opts_store(struct device_driver * ddp,
1338 const char * buf, size_t count)
1340 int opts;
1341 char work[20];
1343 if (1 == sscanf(buf, "%10s", work)) {
1344 if (0 == strnicmp(work,"0x", 2)) {
1345 if (1 == sscanf(&work[2], "%x", &opts))
1346 goto opts_done;
1347 } else {
1348 if (1 == sscanf(work, "%d", &opts))
1349 goto opts_done;
1352 return -EINVAL;
1353 opts_done:
1354 scsi_debug_opts = opts;
1355 scsi_debug_cmnd_count = 0;
1356 return count;
1358 DRIVER_ATTR(opts, S_IRUGO | S_IWUSR, sdebug_opts_show,
1359 sdebug_opts_store)
1361 static ssize_t sdebug_num_tgts_show(struct device_driver * ddp, char * buf)
1363 return snprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_tgts);
1365 static ssize_t sdebug_num_tgts_store(struct device_driver * ddp,
1366 const char * buf, size_t count)
1368 int n;
1370 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
1371 scsi_debug_num_tgts = n;
1372 return count;
1374 return -EINVAL;
1376 DRIVER_ATTR(num_tgts, S_IRUGO | S_IWUSR, sdebug_num_tgts_show,
1377 sdebug_num_tgts_store)
1379 static ssize_t sdebug_dev_size_mb_show(struct device_driver * ddp, char * buf)
1381 return snprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dev_size_mb);
1383 DRIVER_ATTR(dev_size_mb, S_IRUGO, sdebug_dev_size_mb_show, NULL)
1385 static ssize_t sdebug_every_nth_show(struct device_driver * ddp, char * buf)
1387 return snprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_every_nth);
1389 static ssize_t sdebug_every_nth_store(struct device_driver * ddp,
1390 const char * buf, size_t count)
1392 int nth;
1394 if ((count > 0) && (1 == sscanf(buf, "%d", &nth)) && (nth >= 0)) {
1395 scsi_debug_every_nth = nth;
1396 scsi_debug_cmnd_count = 0;
1397 return count;
1399 return -EINVAL;
1401 DRIVER_ATTR(every_nth, S_IRUGO | S_IWUSR, sdebug_every_nth_show,
1402 sdebug_every_nth_store)
1404 static ssize_t sdebug_max_luns_show(struct device_driver * ddp, char * buf)
1406 return snprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_max_luns);
1408 static ssize_t sdebug_max_luns_store(struct device_driver * ddp,
1409 const char * buf, size_t count)
1411 int n;
1413 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
1414 scsi_debug_max_luns = n;
1415 return count;
1417 return -EINVAL;
1419 DRIVER_ATTR(max_luns, S_IRUGO | S_IWUSR, sdebug_max_luns_show,
1420 sdebug_max_luns_store)
1422 static ssize_t sdebug_scsi_level_show(struct device_driver * ddp, char * buf)
1424 return snprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_scsi_level);
1426 DRIVER_ATTR(scsi_level, S_IRUGO, sdebug_scsi_level_show, NULL)
1428 static ssize_t sdebug_add_host_show(struct device_driver * ddp, char * buf)
1430 return snprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_add_host);
1433 static ssize_t sdebug_add_host_store(struct device_driver * ddp,
1434 const char * buf, size_t count)
1436 int delta_hosts;
1437 char work[20];
1439 if (1 != sscanf(buf, "%10s", work))
1440 return -EINVAL;
1441 { /* temporary hack around sscanf() problem with -ve nums */
1442 int neg = 0;
1444 if ('-' == *work)
1445 neg = 1;
1446 if (1 != sscanf(work + neg, "%d", &delta_hosts))
1447 return -EINVAL;
1448 if (neg)
1449 delta_hosts = -delta_hosts;
1451 if (delta_hosts > 0) {
1452 do {
1453 sdebug_add_adapter();
1454 } while (--delta_hosts);
1455 } else if (delta_hosts < 0) {
1456 do {
1457 sdebug_remove_adapter();
1458 } while (++delta_hosts);
1460 return count;
1462 DRIVER_ATTR(add_host, S_IRUGO | S_IWUSR, sdebug_add_host_show,
1463 sdebug_add_host_store)
1465 static void do_create_driverfs_files()
1467 driver_create_file(&sdebug_driverfs_driver, &driver_attr_delay);
1468 driver_create_file(&sdebug_driverfs_driver, &driver_attr_opts);
1469 driver_create_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
1470 driver_create_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
1471 driver_create_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
1472 driver_create_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
1473 driver_create_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
1474 driver_create_file(&sdebug_driverfs_driver, &driver_attr_add_host);
1477 static void do_remove_driverfs_files()
1479 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_add_host);
1480 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
1481 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
1482 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
1483 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
1484 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
1485 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_opts);
1486 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_delay);
1489 static int __init scsi_debug_init(void)
1491 unsigned long sz;
1492 int host_to_add;
1493 int k;
1495 sdebug_store_size = (unsigned long)scsi_debug_dev_size_mb * 1048576;
1496 sdebug_capacity = sdebug_store_size / SECT_SIZE;
1498 /* play around with geometry, don't waste too much on track 0 */
1499 sdebug_heads = 8;
1500 sdebug_sectors_per = 32;
1501 if (scsi_debug_dev_size_mb >= 16)
1502 sdebug_heads = 32;
1503 else if (scsi_debug_dev_size_mb >= 256)
1504 sdebug_heads = 64;
1505 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
1506 (sdebug_sectors_per * sdebug_heads);
1507 if (sdebug_cylinders_per >= 1024) {
1508 /* other LLDs do this; implies >= 1GB ram disk ... */
1509 sdebug_heads = 255;
1510 sdebug_sectors_per = 63;
1511 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
1512 (sdebug_sectors_per * sdebug_heads);
1515 sz = sdebug_store_size;
1516 fake_storep = vmalloc(sz);
1517 if (NULL == fake_storep) {
1518 printk(KERN_ERR "scsi_debug_init: out of memory, 1\n");
1519 return -ENOMEM;
1521 memset(fake_storep, 0, sz);
1523 init_all_queued();
1525 device_register(&pseudo_primary);
1526 bus_register(&pseudo_lld_bus);
1527 driver_register(&sdebug_driverfs_driver);
1528 do_create_driverfs_files();
1530 sdebug_driver_template.proc_name = (char *)sdebug_proc_name;
1532 host_to_add = scsi_debug_add_host;
1533 scsi_debug_add_host = 0;
1535 for (k = 0; k < host_to_add; k++) {
1536 if (sdebug_add_adapter()) {
1537 printk(KERN_ERR "scsi_debug_init: "
1538 "sdebug_add_adapter failed k=%d\n", k);
1539 break;
1543 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
1544 printk(KERN_INFO "scsi_debug: ... built %d host(s)\n",
1545 scsi_debug_add_host);
1547 return 0;
1550 static void __exit scsi_debug_exit(void)
1552 int k = scsi_debug_add_host;
1554 stop_all_queued();
1555 for (; k; k--)
1556 sdebug_remove_adapter();
1557 do_remove_driverfs_files();
1558 driver_unregister(&sdebug_driverfs_driver);
1559 bus_unregister(&pseudo_lld_bus);
1560 device_unregister(&pseudo_primary);
1562 vfree(fake_storep);
1565 device_initcall(scsi_debug_init);
1566 module_exit(scsi_debug_exit);
1568 static struct device pseudo_primary = {
1569 .bus_id = "pseudo_0",
1572 static int pseudo_lld_bus_match(struct device *dev,
1573 struct device_driver *dev_driver)
1575 return 1;
1578 static struct bus_type pseudo_lld_bus = {
1579 .name = "pseudo",
1580 .match = pseudo_lld_bus_match,
1583 static void sdebug_release_adapter(struct device * dev)
1585 struct sdebug_host_info *sdbg_host;
1587 sdbg_host = to_sdebug_host(dev);
1588 kfree(sdbg_host);
1591 static int sdebug_add_adapter()
1593 int k, devs_per_host;
1594 int error = 0;
1595 struct sdebug_host_info *sdbg_host;
1596 struct sdebug_dev_info *sdbg_devinfo;
1597 struct list_head *lh, *lh_sf;
1599 sdbg_host = kmalloc(sizeof(*sdbg_host),GFP_KERNEL);
1601 if (NULL == sdbg_host) {
1602 printk(KERN_ERR "%s: out of memory at line %d\n",
1603 __FUNCTION__, __LINE__);
1604 return -ENOMEM;
1607 memset(sdbg_host, 0, sizeof(*sdbg_host));
1608 INIT_LIST_HEAD(&sdbg_host->dev_info_list);
1610 devs_per_host = scsi_debug_num_tgts * scsi_debug_max_luns;
1611 for (k = 0; k < devs_per_host; k++) {
1612 sdbg_devinfo = kmalloc(sizeof(*sdbg_devinfo),GFP_KERNEL);
1613 if (NULL == sdbg_devinfo) {
1614 printk(KERN_ERR "%s: out of memory at line %d\n",
1615 __FUNCTION__, __LINE__);
1616 error = -ENOMEM;
1617 goto clean;
1619 memset(sdbg_devinfo, 0, sizeof(*sdbg_devinfo));
1620 sdbg_devinfo->sdbg_host = sdbg_host;
1621 list_add_tail(&sdbg_devinfo->dev_list,
1622 &sdbg_host->dev_info_list);
1625 spin_lock(&sdebug_host_list_lock);
1626 list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
1627 spin_unlock(&sdebug_host_list_lock);
1629 sdbg_host->dev.bus = &pseudo_lld_bus;
1630 sdbg_host->dev.parent = &pseudo_primary;
1631 sdbg_host->dev.release = &sdebug_release_adapter;
1632 sprintf(sdbg_host->dev.bus_id, "adapter%d", scsi_debug_add_host);
1634 error = device_register(&sdbg_host->dev);
1636 if (error)
1637 goto clean;
1639 ++scsi_debug_add_host;
1640 return error;
1642 clean:
1643 list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
1644 sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
1645 dev_list);
1646 list_del(&sdbg_devinfo->dev_list);
1647 kfree(sdbg_devinfo);
1650 kfree(sdbg_host);
1651 return error;
1654 static void sdebug_remove_adapter()
1656 struct sdebug_host_info * sdbg_host = NULL;
1658 spin_lock(&sdebug_host_list_lock);
1659 if (!list_empty(&sdebug_host_list)) {
1660 sdbg_host = list_entry(sdebug_host_list.prev,
1661 struct sdebug_host_info, host_list);
1662 list_del(&sdbg_host->host_list);
1664 spin_unlock(&sdebug_host_list_lock);
1666 if (!sdbg_host)
1667 return;
1669 device_unregister(&sdbg_host->dev);
1670 --scsi_debug_add_host;
1673 static int sdebug_driver_probe(struct device * dev)
1675 int error = 0;
1676 struct sdebug_host_info *sdbg_host;
1677 struct Scsi_Host *hpnt;
1679 sdbg_host = to_sdebug_host(dev);
1681 hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
1682 if (NULL == hpnt) {
1683 printk(KERN_ERR "%s: scsi_register failed\n", __FUNCTION__);
1684 error = -ENODEV;
1685 return error;
1688 sdbg_host->shost = hpnt;
1689 *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
1690 if ((hpnt->this_id >= 0) && (scsi_debug_num_tgts > hpnt->this_id))
1691 hpnt->max_id = scsi_debug_num_tgts + 1;
1692 else
1693 hpnt->max_id = scsi_debug_num_tgts;
1694 hpnt->max_lun = scsi_debug_max_luns;
1696 error = scsi_add_host(hpnt, &sdbg_host->dev);
1697 if (error) {
1698 printk(KERN_ERR "%s: scsi_add_host failed\n", __FUNCTION__);
1699 error = -ENODEV;
1700 scsi_host_put(hpnt);
1701 } else
1702 scsi_scan_host(hpnt);
1705 return error;
1708 static int sdebug_driver_remove(struct device * dev)
1710 struct list_head *lh, *lh_sf;
1711 struct sdebug_host_info *sdbg_host;
1712 struct sdebug_dev_info *sdbg_devinfo;
1714 sdbg_host = to_sdebug_host(dev);
1716 if (!sdbg_host) {
1717 printk(KERN_ERR "%s: Unable to locate host info\n",
1718 __FUNCTION__);
1719 return -ENODEV;
1722 scsi_remove_host(sdbg_host->shost);
1724 list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
1725 sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
1726 dev_list);
1727 list_del(&sdbg_devinfo->dev_list);
1728 kfree(sdbg_devinfo);
1731 scsi_host_put(sdbg_host->shost);
1732 return 0;