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>
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>
47 #include <scsi/scsicam.h>
49 #include <linux/stat.h>
51 #ifndef LINUX_VERSION_CODE
52 #include <linux/version.h>
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
82 #define DEF_SCSI_LEVEL 3
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
;
140 struct sdebug_host_info
*sdbg_host
;
145 struct sdebug_host_info
{
146 struct list_head host_list
;
147 struct Scsi_Host
*shost
;
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
{
162 struct timer_list cmnd_timer
;
163 done_funct_t done_funct
;
164 struct scsi_cmnd
* a_cmnd
;
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
,
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
)
260 return (unsigned char *)page_address(sclp
->page
) +
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
;
273 int target
= SCpnt
->device
->id
;
274 int bufflen
= SCpnt
->request_bufflen
;
276 struct sdebug_dev_info
* devip
= NULL
;
277 unsigned char * sbuff
;
278 int inj_recovered
= 0;
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 */
292 buff
= (unsigned char *) SCpnt
->request_buffer
;
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 "
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
);
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 */
324 case INQUIRY
: /* mandatory */
325 errsts
= resp_inquiry(cmd
, target
, buff
, bufflen
, devip
);
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 */
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);
338 memset(buff
, 0, bufflen
);
343 errsts
= check_reset(SCpnt
, devip
);
345 case ALLOW_MEDIUM_REMOVAL
:
346 if ((errsts
= check_reset(SCpnt
, devip
)))
348 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
349 printk("\tMedium removal %s\n",
350 cmd
[4] ? "inhibited" : "enabled");
352 case SEND_DIAGNOSTIC
: /* mandatory */
353 memset(buff
, 0, bufflen
);
355 case TEST_UNIT_READY
: /* mandatory */
356 memset(buff
, 0, bufflen
);
359 errsts
= check_reset(SCpnt
, devip
);
360 memset(buff
, 0, bufflen
);
363 errsts
= check_reset(SCpnt
, devip
);
364 memset(buff
, 0, bufflen
);
367 errsts
= check_reset(SCpnt
, devip
);
368 memset(buff
, 0, bufflen
);
371 errsts
= check_reset(SCpnt
, devip
);
372 memset(buff
, 0, bufflen
);
375 errsts
= check_reset(SCpnt
, devip
);
376 memset(buff
, 0, bufflen
);
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;
391 if ((errsts
= check_reset(SCpnt
, devip
)))
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);
411 block
= cmd
[3] + (cmd
[2] << 8) +
412 ((cmd
[1] & 0x1f) << 16);
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
;
423 errsts
= resp_report_luns(cmd
, buff
, bufflen
, devip
);
429 if ((errsts
= check_reset(SCpnt
, devip
)))
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);
449 block
= cmd
[3] + (cmd
[2] << 8) +
450 ((cmd
[1] & 0x1f) << 16);
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
;
462 errsts
= resp_mode_sense(cmd
, target
, buff
, bufflen
, devip
);
464 case SYNCHRONIZE_CACHE
:
465 memset(buff
, 0, bufflen
);
468 if ((errsts
= check_reset(SCpnt
, devip
)))
470 mk_sense_buffer(devip
, ILLEGAL_REQUEST
, INVALID_OPCODE
, 0, 18);
471 errsts
= check_condition_result
;
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
);
483 /* return -ENOTTY; // correct return but upsets fdisk */
486 static int check_reset(struct scsi_cmnd
* SCpnt
, struct sdebug_dev_info
* devip
)
490 mk_sense_buffer(devip
, UNIT_ATTENTION
, POWERON_RESET
, 0, 18);
491 return check_condition_result
;
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
)
508 /* Two identification descriptors: */
509 /* T10 vendor identifier field format (faked) */
510 arr
[0] = 0x2; /* ASCII */
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
;
519 /* NAA IEEE registered identifier (faked) */
520 arr
[num
] = 0x1; /* binary */
524 arr
[num
+ 4] = 0x51; /* ieee company id=0x123456 (faked) */
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;
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
);
550 if (0x2 & cmd
[1]) { /* CMDDT bit set */
551 mk_sense_buffer(devip
, ILLEGAL_REQUEST
, INVALID_FIELD_IN_CDB
,
553 return check_condition_result
;
554 } else if (0x1 & cmd
[1]) { /* EVPD bit set */
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 */
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 */
570 memcpy(&arr
[4], dev_id_str
, len
);
571 } else if (0x83 == cmd
[2]) { /* device identification */
573 arr
[3] = inquiry_evpd_83(&arr
[4], dev_id_num
,
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
);
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
);
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,
603 memcpy(p
, err_recov_pg
, sizeof(err_recov_pg
));
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
));
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 */
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
));
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,
654 memcpy(p
, ctrl_m_pg
, sizeof(ctrl_m_pg
));
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,
664 memcpy(p
, iec_m_pg
, sizeof(iec_m_pg
));
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
)
678 unsigned char dev_spec
;
679 int alloc_len
, msense_6
, offset
, len
;
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
));
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
,
702 return check_condition_result
;
704 dev_spec
= DEV_READONLY(target
) ? 0x80 : 0x0;
715 case 0x1: /* Read-Write error recovery page, direct access */
716 len
= resp_err_recov_pg(ap
, pcontrol
, target
);
719 case 0x2: /* Disconnect-Reconnect page, all devices */
720 len
= resp_disconnect_pg(ap
, pcontrol
, target
);
723 case 0x3: /* Format device page, direct access */
724 len
= resp_format_pg(ap
, pcontrol
, target
);
727 case 0x8: /* Caching page, direct access */
728 len
= resp_caching_pg(ap
, pcontrol
, target
);
731 case 0xa: /* Control Mode page, all devices */
732 len
= resp_ctrl_m_pg(ap
, pcontrol
, target
);
735 case 0x1c: /* Informational Exceptions Mode page, all devices */
736 len
= resp_iec_m_pg(ap
, pcontrol
, target
);
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
);
749 mk_sense_buffer(devip
, ILLEGAL_REQUEST
, INVALID_FIELD_IN_CDB
,
751 return check_condition_result
;
757 arr
[0] = (offset
>> 8) & 0xff;
758 arr
[1] = offset
& 0xff;
760 memcpy(buff
, arr
, min_len
);
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
;
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
,
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
,
783 /* claim unrecoverable read error */
784 return check_condition_result
;
786 read_lock_irqsave(&atomic_rw
, iflags
);
789 /* printk(KERN_INFO "scsi_debug_read: block=%d, tot_bufflen=%d\n",
793 sgpnt
= (struct scatterlist
*) buff
;
794 buff
= scatg2virt(&sgpnt
[sgcount
]);
795 bufflen
= sgpnt
[sgcount
].length
;
798 memcpy(buff
, fake_storep
+ (block
* SECT_SIZE
), bufflen
);
801 block
+= bufflen
>> POW2_SECT_SIZE
;
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
);
811 read_unlock_irqrestore(&atomic_rw
, iflags
);
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
;
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
,
827 return check_condition_result
;
830 write_lock_irqsave(&atomic_rw
, iflags
);
835 sgpnt
= (struct scatterlist
*) buff
;
836 buff
= scatg2virt(&sgpnt
[sgcount
]);
837 bufflen
= sgpnt
[sgcount
].length
;
840 memcpy(fake_storep
+ (block
* SECT_SIZE
), buff
, bufflen
);
844 block
+= bufflen
>> POW2_SECT_SIZE
;
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
);
854 write_unlock_irqrestore(&atomic_rw
, iflags
);
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
,
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
)),
880 one_lun
= (struct scsi_lun
*) &buff
[8];
881 for (i
= 0; i
< lun_cnt
; i
++) {
882 upper
= (i
>> 8) & 0x3f;
884 one_lun
[i
].scsi_lun
[0] =
885 (upper
| (SAM2_LUN_ADDRESS_METHOD
<< 6));
886 one_lun
[i
].scsi_lun
[1] = i
& 0xff;
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 "
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 "
908 spin_unlock_irqrestore(&queued_arr_lock
, iflags
);
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
);
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
);
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
);
954 /* make this slot avaliable for re-use */
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
;
969 sdbg_host
= *(struct sdebug_host_info
**) sdev
->host
->hostdata
;
971 printk(KERN_ERR
"Host info NULL\n");
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
))
980 if ((!devip
->used
) && (!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;
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
;
1009 sbuff
[7] = (inbandLen
> 7) ? (inbandLen
- 8) : 0;
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");
1019 stop_queued_cmnd(SCpnt
);
1023 static int scsi_debug_biosparam(struct scsi_device
*sdev
,
1024 struct block_device
* bdev
, sector_t capacity
, int *info
)
1029 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
1030 printk(KERN_INFO
"scsi_debug: biosparam\n");
1031 buf
= scsi_bios_ptable(bdev
);
1033 res
= scsi_partsize(buf
, capacity
,
1034 &info
[2], &info
[0], &info
[1]);
1039 info
[0] = sdebug_heads
;
1040 info
[1] = sdebug_sectors_per
;
1041 info
[2] = sdebug_cylinders_per
;
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");
1053 devip
= devInfoReg(SCpnt
->device
);
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");
1070 if (SCpnt
&& ((sdp
= SCpnt
->device
)) && ((hp
= sdp
->host
))) {
1071 sdbg_host
= *(struct sdebug_host_info
**) hp
->hostdata
;
1073 list_for_each_entry(dev_info
,
1074 &sdbg_host
->dev_info_list
,
1076 dev_info
->reset
= 1;
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");
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
,
1094 dev_info
->reset
= 1;
1096 spin_unlock(&sdebug_host_list_lock
);
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
;
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
);
1114 sqcp
->a_cmnd
= NULL
;
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
;
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
);
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
;
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
);
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
)
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
]);
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) {
1186 cmnd
->result
= scsi_result
;
1191 unsigned long iflags
;
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
];
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 */
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
);
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
,
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
;
1268 orig_length
= length
;
1272 int minLen
= length
> 15 ? 15 : length
;
1274 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
1276 memcpy(arr
, buffer
, minLen
);
1278 if (1 != sscanf(arr
, "%d", &pos
))
1280 scsi_debug_opts
= pos
;
1281 if (scsi_debug_every_nth
> 0)
1282 scsi_debug_cmnd_count
= 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, "
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
);
1303 *start
= buffer
+ (offset
- begin
); /* Start of wanted data */
1304 len
-= (offset
- begin
);
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
)
1321 if (1 == sscanf(buf
, "%10s", work
)) {
1322 if ((1 == sscanf(work
, "%d", &delay
)) && (delay
>= 0)) {
1323 scsi_debug_delay
= delay
;
1329 DRIVER_ATTR(delay
, S_IRUGO
| S_IWUSR
, sdebug_delay_show
,
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
)
1343 if (1 == sscanf(buf
, "%10s", work
)) {
1344 if (0 == strnicmp(work
,"0x", 2)) {
1345 if (1 == sscanf(&work
[2], "%x", &opts
))
1348 if (1 == sscanf(work
, "%d", &opts
))
1354 scsi_debug_opts
= opts
;
1355 scsi_debug_cmnd_count
= 0;
1358 DRIVER_ATTR(opts
, S_IRUGO
| S_IWUSR
, sdebug_opts_show
,
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
)
1370 if ((count
> 0) && (1 == sscanf(buf
, "%d", &n
)) && (n
>= 0)) {
1371 scsi_debug_num_tgts
= n
;
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
)
1394 if ((count
> 0) && (1 == sscanf(buf
, "%d", &nth
)) && (nth
>= 0)) {
1395 scsi_debug_every_nth
= nth
;
1396 scsi_debug_cmnd_count
= 0;
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
)
1413 if ((count
> 0) && (1 == sscanf(buf
, "%d", &n
)) && (n
>= 0)) {
1414 scsi_debug_max_luns
= n
;
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
)
1439 if (1 != sscanf(buf
, "%10s", work
))
1441 { /* temporary hack around sscanf() problem with -ve nums */
1446 if (1 != sscanf(work
+ neg
, "%d", &delta_hosts
))
1449 delta_hosts
= -delta_hosts
;
1451 if (delta_hosts
> 0) {
1453 sdebug_add_adapter();
1454 } while (--delta_hosts
);
1455 } else if (delta_hosts
< 0) {
1457 sdebug_remove_adapter();
1458 } while (++delta_hosts
);
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)
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 */
1500 sdebug_sectors_per
= 32;
1501 if (scsi_debug_dev_size_mb
>= 16)
1503 else if (scsi_debug_dev_size_mb
>= 256)
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 ... */
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");
1521 memset(fake_storep
, 0, sz
);
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
);
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
);
1550 static void __exit
scsi_debug_exit(void)
1552 int k
= scsi_debug_add_host
;
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
);
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
)
1578 static struct bus_type pseudo_lld_bus
= {
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
);
1591 static int sdebug_add_adapter()
1593 int k
, devs_per_host
;
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__
);
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__
);
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
);
1639 ++scsi_debug_add_host
;
1643 list_for_each_safe(lh
, lh_sf
, &sdbg_host
->dev_info_list
) {
1644 sdbg_devinfo
= list_entry(lh
, struct sdebug_dev_info
,
1646 list_del(&sdbg_devinfo
->dev_list
);
1647 kfree(sdbg_devinfo
);
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
);
1669 device_unregister(&sdbg_host
->dev
);
1670 --scsi_debug_add_host
;
1673 static int sdebug_driver_probe(struct device
* dev
)
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
));
1683 printk(KERN_ERR
"%s: scsi_register failed\n", __FUNCTION__
);
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;
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
);
1698 printk(KERN_ERR
"%s: scsi_add_host failed\n", __FUNCTION__
);
1700 scsi_host_put(hpnt
);
1702 scsi_scan_host(hpnt
);
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
);
1717 printk(KERN_ERR
"%s: Unable to locate host info\n",
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
,
1727 list_del(&sdbg_devinfo
->dev_list
);
1728 kfree(sdbg_devinfo
);
1731 scsi_host_put(sdbg_host
->shost
);