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/sdebug26.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>
46 #include <scsi/scsi_host.h>
47 #include <scsi/scsicam.h>
49 #include <linux/stat.h>
51 #include "scsi_logging.h"
52 #include "scsi_debug.h"
54 #define SCSI_DEBUG_VERSION "1.75"
55 static const char * scsi_debug_version_date
= "20050113";
57 /* Additional Sense Code (ASC) used */
58 #define NO_ADDED_SENSE 0x0
59 #define UNRECOVERED_READ_ERR 0x11
60 #define INVALID_OPCODE 0x20
61 #define ADDR_OUT_OF_RANGE 0x21
62 #define INVALID_FIELD_IN_CDB 0x24
63 #define POWERON_RESET 0x29
64 #define SAVING_PARAMS_UNSUP 0x39
65 #define THRESHHOLD_EXCEEDED 0x5d
67 #define SDEBUG_TAGGED_QUEUING 0 /* 0 | MSG_SIMPLE_TAG | MSG_ORDERED_TAG */
69 /* Default values for driver parameters */
70 #define DEF_NUM_HOST 1
71 #define DEF_NUM_TGTS 1
72 #define DEF_MAX_LUNS 1
73 /* With these defaults, this driver will make 1 host with 1 target
74 * (id 0) containing 1 logical unit (lun 0). That is 1 device.
77 #define DEF_DEV_SIZE_MB 8
78 #define DEF_EVERY_NTH 0
79 #define DEF_NUM_PARTS 0
81 #define DEF_SCSI_LEVEL 5 /* INQUIRY, byte2 [5->SPC-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.
95 * When "every_nth" < 0 then after "- every_nth" commands:
96 * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
97 * - a RECOVERED_ERROR is simulated on successful read and write
98 * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
99 * This will continue until some other action occurs (e.g. the user
100 * writing a new value (other than -1 or 1) to every_nth via sysfs).
103 /* when 1==SCSI_DEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
104 * sector on read commands: */
105 #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
107 /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
108 * or "peripheral device" addressing (value 0) */
109 #define SAM2_LUN_ADDRESS_METHOD 0
111 static int scsi_debug_add_host
= DEF_NUM_HOST
;
112 static int scsi_debug_delay
= DEF_DELAY
;
113 static int scsi_debug_dev_size_mb
= DEF_DEV_SIZE_MB
;
114 static int scsi_debug_every_nth
= DEF_EVERY_NTH
;
115 static int scsi_debug_max_luns
= DEF_MAX_LUNS
;
116 static int scsi_debug_num_parts
= DEF_NUM_PARTS
;
117 static int scsi_debug_num_tgts
= DEF_NUM_TGTS
; /* targets per host */
118 static int scsi_debug_opts
= DEF_OPTS
;
119 static int scsi_debug_scsi_level
= DEF_SCSI_LEVEL
;
120 static int scsi_debug_ptype
= DEF_PTYPE
; /* SCSI peripheral type (0==disk) */
121 static int scsi_debug_dsense
= DEF_D_SENSE
;
123 static int scsi_debug_cmnd_count
= 0;
125 #define DEV_READONLY(TGT) (0)
126 #define DEV_REMOVEABLE(TGT) (0)
128 static unsigned long sdebug_store_size
; /* in bytes */
129 static sector_t sdebug_capacity
; /* in sectors */
131 /* old BIOS stuff, kernel may get rid of them but some mode sense pages
132 may still need them */
133 static int sdebug_heads
; /* heads per disk */
134 static int sdebug_cylinders_per
; /* cylinders per surface */
135 static int sdebug_sectors_per
; /* sectors per cylinder */
137 /* default sector size is 512 bytes, 2**9 bytes */
138 #define POW2_SECT_SIZE 9
139 #define SECT_SIZE (1 << POW2_SECT_SIZE)
140 #define SECT_SIZE_PER(TGT) SECT_SIZE
142 #define SDEBUG_MAX_PARTS 4
144 #define SDEBUG_SENSE_LEN 32
146 struct sdebug_dev_info
{
147 struct list_head dev_list
;
148 unsigned char sense_buff
[SDEBUG_SENSE_LEN
]; /* weak nexus */
149 unsigned int channel
;
152 struct sdebug_host_info
*sdbg_host
;
157 struct sdebug_host_info
{
158 struct list_head host_list
;
159 struct Scsi_Host
*shost
;
161 struct list_head dev_info_list
;
164 #define to_sdebug_host(d) \
165 container_of(d, struct sdebug_host_info, dev)
167 static LIST_HEAD(sdebug_host_list
);
168 static DEFINE_SPINLOCK(sdebug_host_list_lock
);
170 typedef void (* done_funct_t
) (struct scsi_cmnd
*);
172 struct sdebug_queued_cmd
{
174 struct timer_list cmnd_timer
;
175 done_funct_t done_funct
;
176 struct scsi_cmnd
* a_cmnd
;
179 static struct sdebug_queued_cmd queued_arr
[SCSI_DEBUG_CANQUEUE
];
181 static struct scsi_host_template sdebug_driver_template
= {
182 .proc_info
= scsi_debug_proc_info
,
183 .name
= "SCSI DEBUG",
184 .info
= scsi_debug_info
,
185 .slave_alloc
= scsi_debug_slave_alloc
,
186 .slave_configure
= scsi_debug_slave_configure
,
187 .slave_destroy
= scsi_debug_slave_destroy
,
188 .ioctl
= scsi_debug_ioctl
,
189 .queuecommand
= scsi_debug_queuecommand
,
190 .eh_abort_handler
= scsi_debug_abort
,
191 .eh_bus_reset_handler
= scsi_debug_bus_reset
,
192 .eh_device_reset_handler
= scsi_debug_device_reset
,
193 .eh_host_reset_handler
= scsi_debug_host_reset
,
194 .bios_param
= scsi_debug_biosparam
,
195 .can_queue
= SCSI_DEBUG_CANQUEUE
,
200 .unchecked_isa_dma
= 0,
201 .use_clustering
= DISABLE_CLUSTERING
,
202 .module
= THIS_MODULE
,
205 static unsigned char * fake_storep
; /* ramdisk storage */
207 static int num_aborts
= 0;
208 static int num_dev_resets
= 0;
209 static int num_bus_resets
= 0;
210 static int num_host_resets
= 0;
212 static DEFINE_SPINLOCK(queued_arr_lock
);
213 static DEFINE_RWLOCK(atomic_rw
);
215 static char sdebug_proc_name
[] = "scsi_debug";
217 static int sdebug_driver_probe(struct device
*);
218 static int sdebug_driver_remove(struct device
*);
219 static struct bus_type pseudo_lld_bus
;
221 static struct device_driver sdebug_driverfs_driver
= {
222 .name
= sdebug_proc_name
,
223 .bus
= &pseudo_lld_bus
,
224 .probe
= sdebug_driver_probe
,
225 .remove
= sdebug_driver_remove
,
228 static const int check_condition_result
=
229 (DRIVER_SENSE
<< 24) | SAM_STAT_CHECK_CONDITION
;
231 /* function declarations */
232 static int resp_inquiry(struct scsi_cmnd
* SCpnt
, int target
,
233 struct sdebug_dev_info
* devip
);
234 static int resp_requests(struct scsi_cmnd
* SCpnt
,
235 struct sdebug_dev_info
* devip
);
236 static int resp_readcap(struct scsi_cmnd
* SCpnt
,
237 struct sdebug_dev_info
* devip
);
238 static int resp_mode_sense(struct scsi_cmnd
* SCpnt
, int target
,
239 struct sdebug_dev_info
* devip
);
240 static int resp_read(struct scsi_cmnd
* SCpnt
, int upper_blk
, int block
,
241 int num
, struct sdebug_dev_info
* devip
);
242 static int resp_write(struct scsi_cmnd
* SCpnt
, int upper_blk
, int block
,
243 int num
, struct sdebug_dev_info
* devip
);
244 static int resp_report_luns(struct scsi_cmnd
* SCpnt
,
245 struct sdebug_dev_info
* devip
);
246 static int fill_from_dev_buffer(struct scsi_cmnd
* scp
, unsigned char * arr
,
248 static int fetch_to_dev_buffer(struct scsi_cmnd
* scp
, unsigned char * arr
,
250 static void timer_intr_handler(unsigned long);
251 static struct sdebug_dev_info
* devInfoReg(struct scsi_device
* sdev
);
252 static void mk_sense_buffer(struct sdebug_dev_info
* devip
, int key
,
254 static int check_reset(struct scsi_cmnd
* SCpnt
,
255 struct sdebug_dev_info
* devip
);
256 static int schedule_resp(struct scsi_cmnd
* cmnd
,
257 struct sdebug_dev_info
* devip
,
258 done_funct_t done
, int scsi_result
, int delta_jiff
);
259 static void __init
sdebug_build_parts(unsigned char * ramp
);
260 static void __init
init_all_queued(void);
261 static void stop_all_queued(void);
262 static int stop_queued_cmnd(struct scsi_cmnd
* cmnd
);
263 static int inquiry_evpd_83(unsigned char * arr
, int dev_id_num
,
264 const char * dev_id_str
, int dev_id_str_len
);
265 static void do_create_driverfs_files(void);
266 static void do_remove_driverfs_files(void);
268 static int sdebug_add_adapter(void);
269 static void sdebug_remove_adapter(void);
270 static void sdebug_max_tgts_luns(void);
272 static struct device pseudo_primary
;
273 static struct bus_type pseudo_lld_bus
;
277 int scsi_debug_queuecommand(struct scsi_cmnd
* SCpnt
, done_funct_t done
)
279 unsigned char *cmd
= (unsigned char *) SCpnt
->cmnd
;
280 int block
, upper_blk
, num
, k
;
282 int target
= scmd_id(SCpnt
);
283 struct sdebug_dev_info
* devip
= NULL
;
284 int inj_recovered
= 0;
287 return 0; /* assume mid level reprocessing command */
289 if ((SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
) && cmd
) {
290 printk(KERN_INFO
"scsi_debug: cmd ");
291 for (k
= 0, num
= SCpnt
->cmd_len
; k
< num
; ++k
)
292 printk("%02x ", (int)cmd
[k
]);
295 if(target
== sdebug_driver_template
.this_id
) {
296 printk(KERN_INFO
"scsi_debug: initiator's id used as "
298 return schedule_resp(SCpnt
, NULL
, done
,
299 DID_NO_CONNECT
<< 16, 0);
302 if (SCpnt
->device
->lun
>= scsi_debug_max_luns
)
303 return schedule_resp(SCpnt
, NULL
, done
,
304 DID_NO_CONNECT
<< 16, 0);
305 devip
= devInfoReg(SCpnt
->device
);
307 return schedule_resp(SCpnt
, NULL
, done
,
308 DID_NO_CONNECT
<< 16, 0);
310 if ((scsi_debug_every_nth
!= 0) &&
311 (++scsi_debug_cmnd_count
>= abs(scsi_debug_every_nth
))) {
312 scsi_debug_cmnd_count
= 0;
313 if (scsi_debug_every_nth
< -1)
314 scsi_debug_every_nth
= -1;
315 if (SCSI_DEBUG_OPT_TIMEOUT
& scsi_debug_opts
)
316 return 0; /* ignore command causing timeout */
317 else if (SCSI_DEBUG_OPT_RECOVERED_ERR
& scsi_debug_opts
)
318 inj_recovered
= 1; /* to reads and writes below */
322 case INQUIRY
: /* mandatory, ignore unit attention */
323 errsts
= resp_inquiry(SCpnt
, target
, devip
);
325 case REQUEST_SENSE
: /* mandatory, ignore unit attention */
326 errsts
= resp_requests(SCpnt
, devip
);
328 case REZERO_UNIT
: /* actually this is REWIND for SSC */
330 errsts
= check_reset(SCpnt
, devip
);
332 case ALLOW_MEDIUM_REMOVAL
:
333 if ((errsts
= check_reset(SCpnt
, devip
)))
335 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
336 printk(KERN_INFO
"scsi_debug: Medium removal %s\n",
337 cmd
[4] ? "inhibited" : "enabled");
339 case SEND_DIAGNOSTIC
: /* mandatory */
340 errsts
= check_reset(SCpnt
, devip
);
342 case TEST_UNIT_READY
: /* mandatory */
343 errsts
= check_reset(SCpnt
, devip
);
346 errsts
= check_reset(SCpnt
, devip
);
349 errsts
= check_reset(SCpnt
, devip
);
352 errsts
= check_reset(SCpnt
, devip
);
355 errsts
= check_reset(SCpnt
, devip
);
358 errsts
= resp_readcap(SCpnt
, devip
);
364 if ((errsts
= check_reset(SCpnt
, devip
)))
367 if ((*cmd
) == READ_16
) {
368 upper_blk
= cmd
[5] + (cmd
[4] << 8) +
369 (cmd
[3] << 16) + (cmd
[2] << 24);
370 block
= cmd
[9] + (cmd
[8] << 8) +
371 (cmd
[7] << 16) + (cmd
[6] << 24);
372 num
= cmd
[13] + (cmd
[12] << 8) +
373 (cmd
[11] << 16) + (cmd
[10] << 24);
374 } else if ((*cmd
) == READ_12
) {
375 block
= cmd
[5] + (cmd
[4] << 8) +
376 (cmd
[3] << 16) + (cmd
[2] << 24);
377 num
= cmd
[9] + (cmd
[8] << 8) +
378 (cmd
[7] << 16) + (cmd
[6] << 24);
379 } else if ((*cmd
) == READ_10
) {
380 block
= cmd
[5] + (cmd
[4] << 8) +
381 (cmd
[3] << 16) + (cmd
[2] << 24);
382 num
= cmd
[8] + (cmd
[7] << 8);
384 block
= cmd
[3] + (cmd
[2] << 8) +
385 ((cmd
[1] & 0x1f) << 16);
388 errsts
= resp_read(SCpnt
, upper_blk
, block
, num
, devip
);
389 if (inj_recovered
&& (0 == errsts
)) {
390 mk_sense_buffer(devip
, RECOVERED_ERROR
,
391 THRESHHOLD_EXCEEDED
, 0);
392 errsts
= check_condition_result
;
395 case REPORT_LUNS
: /* mandatory, ignore unit attention */
396 errsts
= resp_report_luns(SCpnt
, devip
);
398 case VERIFY
: /* 10 byte SBC-2 command */
399 errsts
= check_reset(SCpnt
, devip
);
405 if ((errsts
= check_reset(SCpnt
, devip
)))
408 if ((*cmd
) == WRITE_16
) {
409 upper_blk
= cmd
[5] + (cmd
[4] << 8) +
410 (cmd
[3] << 16) + (cmd
[2] << 24);
411 block
= cmd
[9] + (cmd
[8] << 8) +
412 (cmd
[7] << 16) + (cmd
[6] << 24);
413 num
= cmd
[13] + (cmd
[12] << 8) +
414 (cmd
[11] << 16) + (cmd
[10] << 24);
415 } else if ((*cmd
) == WRITE_12
) {
416 block
= cmd
[5] + (cmd
[4] << 8) +
417 (cmd
[3] << 16) + (cmd
[2] << 24);
418 num
= cmd
[9] + (cmd
[8] << 8) +
419 (cmd
[7] << 16) + (cmd
[6] << 24);
420 } else if ((*cmd
) == WRITE_10
) {
421 block
= cmd
[5] + (cmd
[4] << 8) +
422 (cmd
[3] << 16) + (cmd
[2] << 24);
423 num
= cmd
[8] + (cmd
[7] << 8);
425 block
= cmd
[3] + (cmd
[2] << 8) +
426 ((cmd
[1] & 0x1f) << 16);
429 errsts
= resp_write(SCpnt
, upper_blk
, block
, num
, devip
);
430 if (inj_recovered
&& (0 == errsts
)) {
431 mk_sense_buffer(devip
, RECOVERED_ERROR
,
432 THRESHHOLD_EXCEEDED
, 0);
433 errsts
= check_condition_result
;
438 errsts
= resp_mode_sense(SCpnt
, target
, devip
);
440 case SYNCHRONIZE_CACHE
:
441 errsts
= check_reset(SCpnt
, devip
);
444 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
445 printk(KERN_INFO
"scsi_debug: Opcode: 0x%x not "
446 "supported\n", *cmd
);
447 if ((errsts
= check_reset(SCpnt
, devip
)))
448 break; /* Unit attention takes precedence */
449 mk_sense_buffer(devip
, ILLEGAL_REQUEST
, INVALID_OPCODE
, 0);
450 errsts
= check_condition_result
;
453 return schedule_resp(SCpnt
, devip
, done
, errsts
, scsi_debug_delay
);
456 static int scsi_debug_ioctl(struct scsi_device
*dev
, int cmd
, void __user
*arg
)
458 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
) {
459 printk(KERN_INFO
"scsi_debug: ioctl: cmd=0x%x\n", cmd
);
462 /* return -ENOTTY; // correct return but upsets fdisk */
465 static int check_reset(struct scsi_cmnd
* SCpnt
, struct sdebug_dev_info
* devip
)
468 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
469 printk(KERN_INFO
"scsi_debug: Reporting Unit "
470 "attention: power on reset\n");
472 mk_sense_buffer(devip
, UNIT_ATTENTION
, POWERON_RESET
, 0);
473 return check_condition_result
;
478 /* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */
479 static int fill_from_dev_buffer(struct scsi_cmnd
* scp
, unsigned char * arr
,
482 int k
, req_len
, act_len
, len
, active
;
485 struct scatterlist
* sgpnt
;
487 if (0 == scp
->request_bufflen
)
489 if (NULL
== scp
->request_buffer
)
490 return (DID_ERROR
<< 16);
491 if (! ((scp
->sc_data_direction
== DMA_BIDIRECTIONAL
) ||
492 (scp
->sc_data_direction
== DMA_FROM_DEVICE
)))
493 return (DID_ERROR
<< 16);
494 if (0 == scp
->use_sg
) {
495 req_len
= scp
->request_bufflen
;
496 act_len
= (req_len
< arr_len
) ? req_len
: arr_len
;
497 memcpy(scp
->request_buffer
, arr
, act_len
);
498 scp
->resid
= req_len
- act_len
;
501 sgpnt
= (struct scatterlist
*)scp
->request_buffer
;
503 for (k
= 0, req_len
= 0, act_len
= 0; k
< scp
->use_sg
; ++k
, ++sgpnt
) {
505 kaddr
= (unsigned char *)
506 kmap_atomic(sgpnt
->page
, KM_USER0
);
508 return (DID_ERROR
<< 16);
509 kaddr_off
= (unsigned char *)kaddr
+ sgpnt
->offset
;
511 if ((req_len
+ len
) > arr_len
) {
513 len
= arr_len
- req_len
;
515 memcpy(kaddr_off
, arr
+ req_len
, len
);
516 kunmap_atomic(kaddr
, KM_USER0
);
519 req_len
+= sgpnt
->length
;
521 scp
->resid
= req_len
- act_len
;
525 /* Returns number of bytes fetched into 'arr' or -1 if error. */
526 static int fetch_to_dev_buffer(struct scsi_cmnd
* scp
, unsigned char * arr
,
529 int k
, req_len
, len
, fin
;
532 struct scatterlist
* sgpnt
;
534 if (0 == scp
->request_bufflen
)
536 if (NULL
== scp
->request_buffer
)
538 if (! ((scp
->sc_data_direction
== DMA_BIDIRECTIONAL
) ||
539 (scp
->sc_data_direction
== DMA_TO_DEVICE
)))
541 if (0 == scp
->use_sg
) {
542 req_len
= scp
->request_bufflen
;
543 len
= (req_len
< max_arr_len
) ? req_len
: max_arr_len
;
544 memcpy(arr
, scp
->request_buffer
, len
);
547 sgpnt
= (struct scatterlist
*)scp
->request_buffer
;
548 for (k
= 0, req_len
= 0, fin
= 0; k
< scp
->use_sg
; ++k
, ++sgpnt
) {
549 kaddr
= (unsigned char *)kmap_atomic(sgpnt
->page
, KM_USER0
);
552 kaddr_off
= (unsigned char *)kaddr
+ sgpnt
->offset
;
554 if ((req_len
+ len
) > max_arr_len
) {
555 len
= max_arr_len
- req_len
;
558 memcpy(arr
+ req_len
, kaddr_off
, len
);
559 kunmap_atomic(kaddr
, KM_USER0
);
561 return req_len
+ len
;
562 req_len
+= sgpnt
->length
;
568 static const char * inq_vendor_id
= "Linux ";
569 static const char * inq_product_id
= "scsi_debug ";
570 static const char * inq_product_rev
= "0004";
572 static int inquiry_evpd_83(unsigned char * arr
, int dev_id_num
,
573 const char * dev_id_str
, int dev_id_str_len
)
577 /* Two identification descriptors: */
578 /* T10 vendor identifier field format (faked) */
579 arr
[0] = 0x2; /* ASCII */
582 memcpy(&arr
[4], inq_vendor_id
, 8);
583 memcpy(&arr
[12], inq_product_id
, 16);
584 memcpy(&arr
[28], dev_id_str
, dev_id_str_len
);
585 num
= 8 + 16 + dev_id_str_len
;
588 /* NAA IEEE registered identifier (faked) */
589 arr
[num
] = 0x1; /* binary */
593 arr
[num
+ 4] = 0x51; /* ieee company id=0x123456 (faked) */
597 arr
[num
+ 8] = (dev_id_num
>> 24);
598 arr
[num
+ 9] = (dev_id_num
>> 16) & 0xff;
599 arr
[num
+ 10] = (dev_id_num
>> 8) & 0xff;
600 arr
[num
+ 11] = dev_id_num
& 0xff;
605 #define SDEBUG_LONG_INQ_SZ 96
606 #define SDEBUG_MAX_INQ_ARR_SZ 128
608 static int resp_inquiry(struct scsi_cmnd
* scp
, int target
,
609 struct sdebug_dev_info
* devip
)
611 unsigned char pq_pdt
;
612 unsigned char arr
[SDEBUG_MAX_INQ_ARR_SZ
];
613 unsigned char *cmd
= (unsigned char *)scp
->cmnd
;
616 alloc_len
= (cmd
[3] << 8) + cmd
[4];
617 memset(arr
, 0, SDEBUG_MAX_INQ_ARR_SZ
);
618 pq_pdt
= (scsi_debug_ptype
& 0x1f);
620 if (0x2 & cmd
[1]) { /* CMDDT bit set */
621 mk_sense_buffer(devip
, ILLEGAL_REQUEST
, INVALID_FIELD_IN_CDB
,
623 return check_condition_result
;
624 } else if (0x1 & cmd
[1]) { /* EVPD bit set */
628 dev_id_num
= ((devip
->sdbg_host
->shost
->host_no
+ 1) * 2000) +
629 (devip
->target
* 1000) + devip
->lun
;
630 len
= scnprintf(dev_id_str
, 6, "%d", dev_id_num
);
631 if (0 == cmd
[2]) { /* supported vital product data pages */
633 arr
[4] = 0x0; /* this page */
634 arr
[5] = 0x80; /* unit serial number */
635 arr
[6] = 0x83; /* device identification */
636 } else if (0x80 == cmd
[2]) { /* unit serial number */
639 memcpy(&arr
[4], dev_id_str
, len
);
640 } else if (0x83 == cmd
[2]) { /* device identification */
642 arr
[3] = inquiry_evpd_83(&arr
[4], dev_id_num
,
645 /* Illegal request, invalid field in cdb */
646 mk_sense_buffer(devip
, ILLEGAL_REQUEST
,
647 INVALID_FIELD_IN_CDB
, 0);
648 return check_condition_result
;
650 return fill_from_dev_buffer(scp
, arr
,
651 min(alloc_len
, SDEBUG_MAX_INQ_ARR_SZ
));
653 /* drops through here for a standard inquiry */
654 arr
[1] = DEV_REMOVEABLE(target
) ? 0x80 : 0; /* Removable disk */
655 arr
[2] = scsi_debug_scsi_level
;
656 arr
[3] = 2; /* response_data_format==2 */
657 arr
[4] = SDEBUG_LONG_INQ_SZ
- 5;
658 arr
[6] = 0x1; /* claim: ADDR16 */
659 /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
660 arr
[7] = 0x3a; /* claim: WBUS16, SYNC, LINKED + CMDQUE */
661 memcpy(&arr
[8], inq_vendor_id
, 8);
662 memcpy(&arr
[16], inq_product_id
, 16);
663 memcpy(&arr
[32], inq_product_rev
, 4);
664 /* version descriptors (2 bytes each) follow */
665 arr
[58] = 0x0; arr
[59] = 0x40; /* SAM-2 */
666 arr
[60] = 0x3; arr
[61] = 0x0; /* SPC-3 */
667 if (scsi_debug_ptype
== 0) {
668 arr
[62] = 0x1; arr
[63] = 0x80; /* SBC */
669 } else if (scsi_debug_ptype
== 1) {
670 arr
[62] = 0x2; arr
[63] = 0x00; /* SSC */
672 return fill_from_dev_buffer(scp
, arr
,
673 min(alloc_len
, SDEBUG_LONG_INQ_SZ
));
676 static int resp_requests(struct scsi_cmnd
* scp
,
677 struct sdebug_dev_info
* devip
)
679 unsigned char * sbuff
;
680 unsigned char *cmd
= (unsigned char *)scp
->cmnd
;
681 unsigned char arr
[SDEBUG_SENSE_LEN
];
684 memset(arr
, 0, SDEBUG_SENSE_LEN
);
685 if (devip
->reset
== 1)
686 mk_sense_buffer(devip
, 0, NO_ADDED_SENSE
, 0);
687 sbuff
= devip
->sense_buff
;
688 if ((cmd
[1] & 1) && (! scsi_debug_dsense
)) {
689 /* DESC bit set and sense_buff in fixed format */
691 arr
[1] = sbuff
[2]; /* sense key */
692 arr
[2] = sbuff
[12]; /* asc */
693 arr
[3] = sbuff
[13]; /* ascq */
696 memcpy(arr
, sbuff
, SDEBUG_SENSE_LEN
);
697 mk_sense_buffer(devip
, 0, NO_ADDED_SENSE
, 0);
698 return fill_from_dev_buffer(scp
, arr
, len
);
701 #define SDEBUG_READCAP_ARR_SZ 8
702 static int resp_readcap(struct scsi_cmnd
* scp
,
703 struct sdebug_dev_info
* devip
)
705 unsigned char arr
[SDEBUG_READCAP_ARR_SZ
];
709 if ((errsts
= check_reset(scp
, devip
)))
711 memset(arr
, 0, SDEBUG_READCAP_ARR_SZ
);
712 capac
= (unsigned long)sdebug_capacity
- 1;
713 arr
[0] = (capac
>> 24);
714 arr
[1] = (capac
>> 16) & 0xff;
715 arr
[2] = (capac
>> 8) & 0xff;
716 arr
[3] = capac
& 0xff;
717 arr
[6] = (SECT_SIZE_PER(target
) >> 8) & 0xff;
718 arr
[7] = SECT_SIZE_PER(target
) & 0xff;
719 return fill_from_dev_buffer(scp
, arr
, SDEBUG_READCAP_ARR_SZ
);
722 /* <<Following mode page info copied from ST318451LW>> */
724 static int resp_err_recov_pg(unsigned char * p
, int pcontrol
, int target
)
725 { /* Read-Write Error Recovery page for mode_sense */
726 unsigned char err_recov_pg
[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
729 memcpy(p
, err_recov_pg
, sizeof(err_recov_pg
));
731 memset(p
+ 2, 0, sizeof(err_recov_pg
) - 2);
732 return sizeof(err_recov_pg
);
735 static int resp_disconnect_pg(unsigned char * p
, int pcontrol
, int target
)
736 { /* Disconnect-Reconnect page for mode_sense */
737 unsigned char disconnect_pg
[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
738 0, 0, 0, 0, 0, 0, 0, 0};
740 memcpy(p
, disconnect_pg
, sizeof(disconnect_pg
));
742 memset(p
+ 2, 0, sizeof(disconnect_pg
) - 2);
743 return sizeof(disconnect_pg
);
746 static int resp_format_pg(unsigned char * p
, int pcontrol
, int target
)
747 { /* Format device page for mode_sense */
748 unsigned char format_pg
[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
749 0, 0, 0, 0, 0, 0, 0, 0,
750 0, 0, 0, 0, 0x40, 0, 0, 0};
752 memcpy(p
, format_pg
, sizeof(format_pg
));
753 p
[10] = (sdebug_sectors_per
>> 8) & 0xff;
754 p
[11] = sdebug_sectors_per
& 0xff;
755 p
[12] = (SECT_SIZE
>> 8) & 0xff;
756 p
[13] = SECT_SIZE
& 0xff;
757 if (DEV_REMOVEABLE(target
))
758 p
[20] |= 0x20; /* should agree with INQUIRY */
760 memset(p
+ 2, 0, sizeof(format_pg
) - 2);
761 return sizeof(format_pg
);
764 static int resp_caching_pg(unsigned char * p
, int pcontrol
, int target
)
765 { /* Caching page for mode_sense */
766 unsigned char caching_pg
[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
767 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
769 memcpy(p
, caching_pg
, sizeof(caching_pg
));
771 memset(p
+ 2, 0, sizeof(caching_pg
) - 2);
772 return sizeof(caching_pg
);
775 static int resp_ctrl_m_pg(unsigned char * p
, int pcontrol
, int target
)
776 { /* Control mode page for mode_sense */
777 unsigned char ctrl_m_pg
[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
780 if (scsi_debug_dsense
)
782 memcpy(p
, ctrl_m_pg
, sizeof(ctrl_m_pg
));
784 memset(p
+ 2, 0, sizeof(ctrl_m_pg
) - 2);
785 return sizeof(ctrl_m_pg
);
788 static int resp_iec_m_pg(unsigned char * p
, int pcontrol
, int target
)
789 { /* Informational Exceptions control mode page for mode_sense */
790 unsigned char iec_m_pg
[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
792 memcpy(p
, iec_m_pg
, sizeof(iec_m_pg
));
794 memset(p
+ 2, 0, sizeof(iec_m_pg
) - 2);
795 return sizeof(iec_m_pg
);
798 #define SDEBUG_MAX_MSENSE_SZ 256
800 static int resp_mode_sense(struct scsi_cmnd
* scp
, int target
,
801 struct sdebug_dev_info
* devip
)
804 int pcontrol
, pcode
, subpcode
;
805 unsigned char dev_spec
;
806 int alloc_len
, msense_6
, offset
, len
, errsts
;
808 unsigned char arr
[SDEBUG_MAX_MSENSE_SZ
];
809 unsigned char *cmd
= (unsigned char *)scp
->cmnd
;
811 if ((errsts
= check_reset(scp
, devip
)))
814 pcontrol
= (cmd
[2] & 0xc0) >> 6;
815 pcode
= cmd
[2] & 0x3f;
817 msense_6
= (MODE_SENSE
== cmd
[0]);
818 alloc_len
= msense_6
? cmd
[4] : ((cmd
[7] << 8) | cmd
[8]);
819 memset(arr
, 0, SDEBUG_MAX_MSENSE_SZ
);
820 if (0x3 == pcontrol
) { /* Saving values not supported */
821 mk_sense_buffer(devip
, ILLEGAL_REQUEST
, SAVING_PARAMS_UNSUP
,
823 return check_condition_result
;
825 dev_spec
= DEV_READONLY(target
) ? 0x80 : 0x0;
835 if (0 != subpcode
) { /* TODO: Control Extension page */
836 mk_sense_buffer(devip
, ILLEGAL_REQUEST
, INVALID_FIELD_IN_CDB
,
838 return check_condition_result
;
841 case 0x1: /* Read-Write error recovery page, direct access */
842 len
= resp_err_recov_pg(ap
, pcontrol
, target
);
845 case 0x2: /* Disconnect-Reconnect page, all devices */
846 len
= resp_disconnect_pg(ap
, pcontrol
, target
);
849 case 0x3: /* Format device page, direct access */
850 len
= resp_format_pg(ap
, pcontrol
, target
);
853 case 0x8: /* Caching page, direct access */
854 len
= resp_caching_pg(ap
, pcontrol
, target
);
857 case 0xa: /* Control Mode page, all devices */
858 len
= resp_ctrl_m_pg(ap
, pcontrol
, target
);
861 case 0x1c: /* Informational Exceptions Mode page, all devices */
862 len
= resp_iec_m_pg(ap
, pcontrol
, target
);
865 case 0x3f: /* Read all Mode pages */
866 len
= resp_err_recov_pg(ap
, pcontrol
, target
);
867 len
+= resp_disconnect_pg(ap
+ len
, pcontrol
, target
);
868 len
+= resp_format_pg(ap
+ len
, pcontrol
, target
);
869 len
+= resp_caching_pg(ap
+ len
, pcontrol
, target
);
870 len
+= resp_ctrl_m_pg(ap
+ len
, pcontrol
, target
);
871 len
+= resp_iec_m_pg(ap
+ len
, pcontrol
, target
);
875 mk_sense_buffer(devip
, ILLEGAL_REQUEST
, INVALID_FIELD_IN_CDB
,
877 return check_condition_result
;
882 arr
[0] = ((offset
- 2) >> 8) & 0xff;
883 arr
[1] = (offset
- 2) & 0xff;
885 return fill_from_dev_buffer(scp
, arr
, min(alloc_len
, offset
));
888 static int resp_read(struct scsi_cmnd
* SCpnt
, int upper_blk
, int block
,
889 int num
, struct sdebug_dev_info
* devip
)
891 unsigned long iflags
;
894 if (upper_blk
|| (block
+ num
> sdebug_capacity
)) {
895 mk_sense_buffer(devip
, ILLEGAL_REQUEST
, ADDR_OUT_OF_RANGE
,
897 return check_condition_result
;
899 if ((SCSI_DEBUG_OPT_MEDIUM_ERR
& scsi_debug_opts
) &&
900 (block
<= OPT_MEDIUM_ERR_ADDR
) &&
901 ((block
+ num
) > OPT_MEDIUM_ERR_ADDR
)) {
902 mk_sense_buffer(devip
, MEDIUM_ERROR
, UNRECOVERED_READ_ERR
,
904 /* claim unrecoverable read error */
905 return check_condition_result
;
907 read_lock_irqsave(&atomic_rw
, iflags
);
908 ret
= fill_from_dev_buffer(SCpnt
, fake_storep
+ (block
* SECT_SIZE
),
910 read_unlock_irqrestore(&atomic_rw
, iflags
);
914 static int resp_write(struct scsi_cmnd
* SCpnt
, int upper_blk
, int block
,
915 int num
, struct sdebug_dev_info
* devip
)
917 unsigned long iflags
;
920 if (upper_blk
|| (block
+ num
> sdebug_capacity
)) {
921 mk_sense_buffer(devip
, ILLEGAL_REQUEST
, ADDR_OUT_OF_RANGE
,
923 return check_condition_result
;
926 write_lock_irqsave(&atomic_rw
, iflags
);
927 res
= fetch_to_dev_buffer(SCpnt
, fake_storep
+ (block
* SECT_SIZE
),
929 write_unlock_irqrestore(&atomic_rw
, iflags
);
931 return (DID_ERROR
<< 16);
932 else if ((res
< (num
* SECT_SIZE
)) &&
933 (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
))
934 printk(KERN_INFO
"scsi_debug: write: cdb indicated=%d, "
935 " IO sent=%d bytes\n", num
* SECT_SIZE
, res
);
939 #define SDEBUG_RLUN_ARR_SZ 128
941 static int resp_report_luns(struct scsi_cmnd
* scp
,
942 struct sdebug_dev_info
* devip
)
944 unsigned int alloc_len
;
945 int lun_cnt
, i
, upper
;
946 unsigned char *cmd
= (unsigned char *)scp
->cmnd
;
947 int select_report
= (int)cmd
[2];
948 struct scsi_lun
*one_lun
;
949 unsigned char arr
[SDEBUG_RLUN_ARR_SZ
];
951 alloc_len
= cmd
[9] + (cmd
[8] << 8) + (cmd
[7] << 16) + (cmd
[6] << 24);
952 if ((alloc_len
< 16) || (select_report
> 2)) {
953 mk_sense_buffer(devip
, ILLEGAL_REQUEST
, INVALID_FIELD_IN_CDB
,
955 return check_condition_result
;
957 /* can produce response with up to 16k luns (lun 0 to lun 16383) */
958 memset(arr
, 0, SDEBUG_RLUN_ARR_SZ
);
959 lun_cnt
= scsi_debug_max_luns
;
960 arr
[2] = ((sizeof(struct scsi_lun
) * lun_cnt
) >> 8) & 0xff;
961 arr
[3] = (sizeof(struct scsi_lun
) * lun_cnt
) & 0xff;
962 lun_cnt
= min((int)((SDEBUG_RLUN_ARR_SZ
- 8) /
963 sizeof(struct scsi_lun
)), lun_cnt
);
964 one_lun
= (struct scsi_lun
*) &arr
[8];
965 for (i
= 0; i
< lun_cnt
; i
++) {
966 upper
= (i
>> 8) & 0x3f;
968 one_lun
[i
].scsi_lun
[0] =
969 (upper
| (SAM2_LUN_ADDRESS_METHOD
<< 6));
970 one_lun
[i
].scsi_lun
[1] = i
& 0xff;
972 return fill_from_dev_buffer(scp
, arr
,
973 min((int)alloc_len
, SDEBUG_RLUN_ARR_SZ
));
976 /* When timer goes off this function is called. */
977 static void timer_intr_handler(unsigned long indx
)
979 struct sdebug_queued_cmd
* sqcp
;
980 unsigned long iflags
;
982 if (indx
>= SCSI_DEBUG_CANQUEUE
) {
983 printk(KERN_ERR
"scsi_debug:timer_intr_handler: indx too "
987 spin_lock_irqsave(&queued_arr_lock
, iflags
);
988 sqcp
= &queued_arr
[(int)indx
];
989 if (! sqcp
->in_use
) {
990 printk(KERN_ERR
"scsi_debug:timer_intr_handler: Unexpected "
992 spin_unlock_irqrestore(&queued_arr_lock
, iflags
);
996 if (sqcp
->done_funct
) {
997 sqcp
->a_cmnd
->result
= sqcp
->scsi_result
;
998 sqcp
->done_funct(sqcp
->a_cmnd
); /* callback to mid level */
1000 sqcp
->done_funct
= NULL
;
1001 spin_unlock_irqrestore(&queued_arr_lock
, iflags
);
1004 static int scsi_debug_slave_alloc(struct scsi_device
* sdp
)
1006 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
1007 sdev_printk(KERN_INFO
, sdp
, "scsi_debug: slave_alloc\n");
1011 static int scsi_debug_slave_configure(struct scsi_device
* sdp
)
1013 struct sdebug_dev_info
* devip
;
1015 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
1016 sdev_printk(KERN_INFO
, sdp
, "scsi_debug: slave_configure\n");
1017 if (sdp
->host
->max_cmd_len
!= SCSI_DEBUG_MAX_CMD_LEN
)
1018 sdp
->host
->max_cmd_len
= SCSI_DEBUG_MAX_CMD_LEN
;
1019 devip
= devInfoReg(sdp
);
1020 sdp
->hostdata
= devip
;
1021 if (sdp
->host
->cmd_per_lun
)
1022 scsi_adjust_queue_depth(sdp
, SDEBUG_TAGGED_QUEUING
,
1023 sdp
->host
->cmd_per_lun
);
1027 static void scsi_debug_slave_destroy(struct scsi_device
* sdp
)
1029 struct sdebug_dev_info
* devip
=
1030 (struct sdebug_dev_info
*)sdp
->hostdata
;
1032 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
1033 sdev_printk(KERN_INFO
, sdp
, "scsi_debug: slave_destroy\n");
1035 /* make this slot avaliable for re-use */
1037 sdp
->hostdata
= NULL
;
1041 static struct sdebug_dev_info
* devInfoReg(struct scsi_device
* sdev
)
1043 struct sdebug_host_info
* sdbg_host
;
1044 struct sdebug_dev_info
* open_devip
= NULL
;
1045 struct sdebug_dev_info
* devip
=
1046 (struct sdebug_dev_info
*)sdev
->hostdata
;
1050 sdbg_host
= *(struct sdebug_host_info
**) sdev
->host
->hostdata
;
1052 printk(KERN_ERR
"Host info NULL\n");
1055 list_for_each_entry(devip
, &sdbg_host
->dev_info_list
, dev_list
) {
1056 if ((devip
->used
) && (devip
->channel
== sdev
->channel
) &&
1057 (devip
->target
== sdev
->id
) &&
1058 (devip
->lun
== sdev
->lun
))
1061 if ((!devip
->used
) && (!open_devip
))
1065 if (NULL
== open_devip
) { /* try and make a new one */
1066 open_devip
= kmalloc(sizeof(*open_devip
),GFP_KERNEL
);
1067 if (NULL
== open_devip
) {
1068 printk(KERN_ERR
"%s: out of memory at line %d\n",
1069 __FUNCTION__
, __LINE__
);
1072 memset(open_devip
, 0, sizeof(*open_devip
));
1073 open_devip
->sdbg_host
= sdbg_host
;
1074 list_add_tail(&open_devip
->dev_list
,
1075 &sdbg_host
->dev_info_list
);
1078 open_devip
->channel
= sdev
->channel
;
1079 open_devip
->target
= sdev
->id
;
1080 open_devip
->lun
= sdev
->lun
;
1081 open_devip
->sdbg_host
= sdbg_host
;
1082 open_devip
->reset
= 1;
1083 open_devip
->used
= 1;
1084 memset(open_devip
->sense_buff
, 0, SDEBUG_SENSE_LEN
);
1085 if (scsi_debug_dsense
)
1086 open_devip
->sense_buff
[0] = 0x72;
1088 open_devip
->sense_buff
[0] = 0x70;
1089 open_devip
->sense_buff
[7] = 0xa;
1096 static void mk_sense_buffer(struct sdebug_dev_info
* devip
, int key
,
1099 unsigned char * sbuff
;
1101 sbuff
= devip
->sense_buff
;
1102 memset(sbuff
, 0, SDEBUG_SENSE_LEN
);
1103 if (scsi_debug_dsense
) {
1104 sbuff
[0] = 0x72; /* descriptor, current */
1109 sbuff
[0] = 0x70; /* fixed, current */
1111 sbuff
[7] = 0xa; /* implies 18 byte sense buffer */
1115 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
1116 printk(KERN_INFO
"scsi_debug: [sense_key,asc,ascq]: "
1117 "[0x%x,0x%x,0x%x]\n", key
, asc
, asq
);
1120 static int scsi_debug_abort(struct scsi_cmnd
* SCpnt
)
1122 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
1123 printk(KERN_INFO
"scsi_debug: abort\n");
1125 stop_queued_cmnd(SCpnt
);
1129 static int scsi_debug_biosparam(struct scsi_device
*sdev
,
1130 struct block_device
* bdev
, sector_t capacity
, int *info
)
1135 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
1136 printk(KERN_INFO
"scsi_debug: biosparam\n");
1137 buf
= scsi_bios_ptable(bdev
);
1139 res
= scsi_partsize(buf
, capacity
,
1140 &info
[2], &info
[0], &info
[1]);
1145 info
[0] = sdebug_heads
;
1146 info
[1] = sdebug_sectors_per
;
1147 info
[2] = sdebug_cylinders_per
;
1151 static int scsi_debug_device_reset(struct scsi_cmnd
* SCpnt
)
1153 struct sdebug_dev_info
* devip
;
1155 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
1156 printk(KERN_INFO
"scsi_debug: device_reset\n");
1159 devip
= devInfoReg(SCpnt
->device
);
1166 static int scsi_debug_bus_reset(struct scsi_cmnd
* SCpnt
)
1168 struct sdebug_host_info
*sdbg_host
;
1169 struct sdebug_dev_info
* dev_info
;
1170 struct scsi_device
* sdp
;
1171 struct Scsi_Host
* hp
;
1173 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
1174 printk(KERN_INFO
"scsi_debug: bus_reset\n");
1176 if (SCpnt
&& ((sdp
= SCpnt
->device
)) && ((hp
= sdp
->host
))) {
1177 sdbg_host
= *(struct sdebug_host_info
**) hp
->hostdata
;
1179 list_for_each_entry(dev_info
,
1180 &sdbg_host
->dev_info_list
,
1182 dev_info
->reset
= 1;
1188 static int scsi_debug_host_reset(struct scsi_cmnd
* SCpnt
)
1190 struct sdebug_host_info
* sdbg_host
;
1191 struct sdebug_dev_info
* dev_info
;
1193 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
1194 printk(KERN_INFO
"scsi_debug: host_reset\n");
1196 spin_lock(&sdebug_host_list_lock
);
1197 list_for_each_entry(sdbg_host
, &sdebug_host_list
, host_list
) {
1198 list_for_each_entry(dev_info
, &sdbg_host
->dev_info_list
,
1200 dev_info
->reset
= 1;
1202 spin_unlock(&sdebug_host_list_lock
);
1207 /* Returns 1 if found 'cmnd' and deleted its timer. else returns 0 */
1208 static int stop_queued_cmnd(struct scsi_cmnd
* cmnd
)
1210 unsigned long iflags
;
1212 struct sdebug_queued_cmd
* sqcp
;
1214 spin_lock_irqsave(&queued_arr_lock
, iflags
);
1215 for (k
= 0; k
< SCSI_DEBUG_CANQUEUE
; ++k
) {
1216 sqcp
= &queued_arr
[k
];
1217 if (sqcp
->in_use
&& (cmnd
== sqcp
->a_cmnd
)) {
1218 del_timer_sync(&sqcp
->cmnd_timer
);
1220 sqcp
->a_cmnd
= NULL
;
1224 spin_unlock_irqrestore(&queued_arr_lock
, iflags
);
1225 return (k
< SCSI_DEBUG_CANQUEUE
) ? 1 : 0;
1228 /* Deletes (stops) timers of all queued commands */
1229 static void stop_all_queued(void)
1231 unsigned long iflags
;
1233 struct sdebug_queued_cmd
* sqcp
;
1235 spin_lock_irqsave(&queued_arr_lock
, iflags
);
1236 for (k
= 0; k
< SCSI_DEBUG_CANQUEUE
; ++k
) {
1237 sqcp
= &queued_arr
[k
];
1238 if (sqcp
->in_use
&& sqcp
->a_cmnd
) {
1239 del_timer_sync(&sqcp
->cmnd_timer
);
1241 sqcp
->a_cmnd
= NULL
;
1244 spin_unlock_irqrestore(&queued_arr_lock
, iflags
);
1247 /* Initializes timers in queued array */
1248 static void __init
init_all_queued(void)
1250 unsigned long iflags
;
1252 struct sdebug_queued_cmd
* sqcp
;
1254 spin_lock_irqsave(&queued_arr_lock
, iflags
);
1255 for (k
= 0; k
< SCSI_DEBUG_CANQUEUE
; ++k
) {
1256 sqcp
= &queued_arr
[k
];
1257 init_timer(&sqcp
->cmnd_timer
);
1259 sqcp
->a_cmnd
= NULL
;
1261 spin_unlock_irqrestore(&queued_arr_lock
, iflags
);
1264 static void __init
sdebug_build_parts(unsigned char * ramp
)
1266 struct partition
* pp
;
1267 int starts
[SDEBUG_MAX_PARTS
+ 2];
1268 int sectors_per_part
, num_sectors
, k
;
1269 int heads_by_sects
, start_sec
, end_sec
;
1271 /* assume partition table already zeroed */
1272 if ((scsi_debug_num_parts
< 1) || (sdebug_store_size
< 1048576))
1274 if (scsi_debug_num_parts
> SDEBUG_MAX_PARTS
) {
1275 scsi_debug_num_parts
= SDEBUG_MAX_PARTS
;
1276 printk(KERN_WARNING
"scsi_debug:build_parts: reducing "
1277 "partitions to %d\n", SDEBUG_MAX_PARTS
);
1279 num_sectors
= (int)(sdebug_store_size
/ SECT_SIZE
);
1280 sectors_per_part
= (num_sectors
- sdebug_sectors_per
)
1281 / scsi_debug_num_parts
;
1282 heads_by_sects
= sdebug_heads
* sdebug_sectors_per
;
1283 starts
[0] = sdebug_sectors_per
;
1284 for (k
= 1; k
< scsi_debug_num_parts
; ++k
)
1285 starts
[k
] = ((k
* sectors_per_part
) / heads_by_sects
)
1287 starts
[scsi_debug_num_parts
] = num_sectors
;
1288 starts
[scsi_debug_num_parts
+ 1] = 0;
1290 ramp
[510] = 0x55; /* magic partition markings */
1292 pp
= (struct partition
*)(ramp
+ 0x1be);
1293 for (k
= 0; starts
[k
+ 1]; ++k
, ++pp
) {
1294 start_sec
= starts
[k
];
1295 end_sec
= starts
[k
+ 1] - 1;
1298 pp
->cyl
= start_sec
/ heads_by_sects
;
1299 pp
->head
= (start_sec
- (pp
->cyl
* heads_by_sects
))
1300 / sdebug_sectors_per
;
1301 pp
->sector
= (start_sec
% sdebug_sectors_per
) + 1;
1303 pp
->end_cyl
= end_sec
/ heads_by_sects
;
1304 pp
->end_head
= (end_sec
- (pp
->end_cyl
* heads_by_sects
))
1305 / sdebug_sectors_per
;
1306 pp
->end_sector
= (end_sec
% sdebug_sectors_per
) + 1;
1308 pp
->start_sect
= start_sec
;
1309 pp
->nr_sects
= end_sec
- start_sec
+ 1;
1310 pp
->sys_ind
= 0x83; /* plain Linux partition */
1314 static int schedule_resp(struct scsi_cmnd
* cmnd
,
1315 struct sdebug_dev_info
* devip
,
1316 done_funct_t done
, int scsi_result
, int delta_jiff
)
1318 if ((SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
) && cmnd
) {
1320 struct scsi_device
* sdp
= cmnd
->device
;
1322 sdev_printk(KERN_INFO
, sdp
,
1323 "non-zero result=0x%x\n",
1327 if (cmnd
&& devip
) {
1328 /* simulate autosense by this driver */
1329 if (SAM_STAT_CHECK_CONDITION
== (scsi_result
& 0xff))
1330 memcpy(cmnd
->sense_buffer
, devip
->sense_buff
,
1331 (SCSI_SENSE_BUFFERSIZE
> SDEBUG_SENSE_LEN
) ?
1332 SDEBUG_SENSE_LEN
: SCSI_SENSE_BUFFERSIZE
);
1334 if (delta_jiff
<= 0) {
1336 cmnd
->result
= scsi_result
;
1341 unsigned long iflags
;
1343 struct sdebug_queued_cmd
* sqcp
= NULL
;
1345 spin_lock_irqsave(&queued_arr_lock
, iflags
);
1346 for (k
= 0; k
< SCSI_DEBUG_CANQUEUE
; ++k
) {
1347 sqcp
= &queued_arr
[k
];
1351 if (k
>= SCSI_DEBUG_CANQUEUE
) {
1352 spin_unlock_irqrestore(&queued_arr_lock
, iflags
);
1353 printk(KERN_WARNING
"scsi_debug: can_queue exceeded\n");
1354 return 1; /* report busy to mid level */
1357 sqcp
->a_cmnd
= cmnd
;
1358 sqcp
->scsi_result
= scsi_result
;
1359 sqcp
->done_funct
= done
;
1360 sqcp
->cmnd_timer
.function
= timer_intr_handler
;
1361 sqcp
->cmnd_timer
.data
= k
;
1362 sqcp
->cmnd_timer
.expires
= jiffies
+ delta_jiff
;
1363 add_timer(&sqcp
->cmnd_timer
);
1364 spin_unlock_irqrestore(&queued_arr_lock
, iflags
);
1371 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
1372 * of sysfs parameters (which module_param doesn't yet support).
1373 * Sysfs parameters defined explicitly below.
1375 module_param_named(add_host
, scsi_debug_add_host
, int, 0); /* perm=0644 */
1376 module_param_named(delay
, scsi_debug_delay
, int, 0); /* perm=0644 */
1377 module_param_named(dev_size_mb
, scsi_debug_dev_size_mb
, int, 0);
1378 module_param_named(dsense
, scsi_debug_dsense
, int, 0);
1379 module_param_named(every_nth
, scsi_debug_every_nth
, int, 0);
1380 module_param_named(max_luns
, scsi_debug_max_luns
, int, 0);
1381 module_param_named(num_parts
, scsi_debug_num_parts
, int, 0);
1382 module_param_named(num_tgts
, scsi_debug_num_tgts
, int, 0);
1383 module_param_named(opts
, scsi_debug_opts
, int, 0); /* perm=0644 */
1384 module_param_named(ptype
, scsi_debug_ptype
, int, 0);
1385 module_param_named(scsi_level
, scsi_debug_scsi_level
, int, 0);
1387 MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
1388 MODULE_DESCRIPTION("SCSI debug adapter driver");
1389 MODULE_LICENSE("GPL");
1390 MODULE_VERSION(SCSI_DEBUG_VERSION
);
1392 MODULE_PARM_DESC(add_host
, "0..127 hosts allowed(def=1)");
1393 MODULE_PARM_DESC(delay
, "# of jiffies to delay response(def=1)");
1394 MODULE_PARM_DESC(dev_size_mb
, "size in MB of ram shared by devs");
1395 MODULE_PARM_DESC(dsense
, "use descriptor sense format(def: fixed)");
1396 MODULE_PARM_DESC(every_nth
, "timeout every nth command(def=100)");
1397 MODULE_PARM_DESC(max_luns
, "number of SCSI LUNs per target to simulate");
1398 MODULE_PARM_DESC(num_parts
, "number of partitions(def=0)");
1399 MODULE_PARM_DESC(num_tgts
, "number of SCSI targets per host to simulate");
1400 MODULE_PARM_DESC(opts
, "1->noise, 2->medium_error, 4->...");
1401 MODULE_PARM_DESC(ptype
, "SCSI peripheral type(def=0[disk])");
1402 MODULE_PARM_DESC(scsi_level
, "SCSI level to simulate(def=5[SPC-3])");
1405 static char sdebug_info
[256];
1407 static const char * scsi_debug_info(struct Scsi_Host
* shp
)
1409 sprintf(sdebug_info
, "scsi_debug, version %s [%s], "
1410 "dev_size_mb=%d, opts=0x%x", SCSI_DEBUG_VERSION
,
1411 scsi_debug_version_date
, scsi_debug_dev_size_mb
,
1416 /* scsi_debug_proc_info
1417 * Used if the driver currently has no own support for /proc/scsi
1419 static int scsi_debug_proc_info(struct Scsi_Host
*host
, char *buffer
, char **start
, off_t offset
,
1420 int length
, int inout
)
1422 int len
, pos
, begin
;
1425 orig_length
= length
;
1429 int minLen
= length
> 15 ? 15 : length
;
1431 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
1433 memcpy(arr
, buffer
, minLen
);
1435 if (1 != sscanf(arr
, "%d", &pos
))
1437 scsi_debug_opts
= pos
;
1438 if (scsi_debug_every_nth
!= 0)
1439 scsi_debug_cmnd_count
= 0;
1443 pos
= len
= sprintf(buffer
, "scsi_debug adapter driver, version "
1445 "num_tgts=%d, shared (ram) size=%d MB, opts=0x%x, "
1446 "every_nth=%d(curr:%d)\n"
1447 "delay=%d, max_luns=%d, scsi_level=%d\n"
1448 "sector_size=%d bytes, cylinders=%d, heads=%d, sectors=%d\n"
1449 "number of aborts=%d, device_reset=%d, bus_resets=%d, "
1451 SCSI_DEBUG_VERSION
, scsi_debug_version_date
, scsi_debug_num_tgts
,
1452 scsi_debug_dev_size_mb
, scsi_debug_opts
, scsi_debug_every_nth
,
1453 scsi_debug_cmnd_count
, scsi_debug_delay
,
1454 scsi_debug_max_luns
, scsi_debug_scsi_level
,
1455 SECT_SIZE
, sdebug_cylinders_per
, sdebug_heads
, sdebug_sectors_per
,
1456 num_aborts
, num_dev_resets
, num_bus_resets
, num_host_resets
);
1461 *start
= buffer
+ (offset
- begin
); /* Start of wanted data */
1462 len
-= (offset
- begin
);
1468 static ssize_t
sdebug_delay_show(struct device_driver
* ddp
, char * buf
)
1470 return scnprintf(buf
, PAGE_SIZE
, "%d\n", scsi_debug_delay
);
1473 static ssize_t
sdebug_delay_store(struct device_driver
* ddp
,
1474 const char * buf
, size_t count
)
1479 if (1 == sscanf(buf
, "%10s", work
)) {
1480 if ((1 == sscanf(work
, "%d", &delay
)) && (delay
>= 0)) {
1481 scsi_debug_delay
= delay
;
1487 DRIVER_ATTR(delay
, S_IRUGO
| S_IWUSR
, sdebug_delay_show
,
1488 sdebug_delay_store
);
1490 static ssize_t
sdebug_opts_show(struct device_driver
* ddp
, char * buf
)
1492 return scnprintf(buf
, PAGE_SIZE
, "0x%x\n", scsi_debug_opts
);
1495 static ssize_t
sdebug_opts_store(struct device_driver
* ddp
,
1496 const char * buf
, size_t count
)
1501 if (1 == sscanf(buf
, "%10s", work
)) {
1502 if (0 == strnicmp(work
,"0x", 2)) {
1503 if (1 == sscanf(&work
[2], "%x", &opts
))
1506 if (1 == sscanf(work
, "%d", &opts
))
1512 scsi_debug_opts
= opts
;
1513 scsi_debug_cmnd_count
= 0;
1516 DRIVER_ATTR(opts
, S_IRUGO
| S_IWUSR
, sdebug_opts_show
,
1519 static ssize_t
sdebug_ptype_show(struct device_driver
* ddp
, char * buf
)
1521 return scnprintf(buf
, PAGE_SIZE
, "%d\n", scsi_debug_ptype
);
1523 static ssize_t
sdebug_ptype_store(struct device_driver
* ddp
,
1524 const char * buf
, size_t count
)
1528 if ((count
> 0) && (1 == sscanf(buf
, "%d", &n
)) && (n
>= 0)) {
1529 scsi_debug_ptype
= n
;
1534 DRIVER_ATTR(ptype
, S_IRUGO
| S_IWUSR
, sdebug_ptype_show
, sdebug_ptype_store
);
1536 static ssize_t
sdebug_dsense_show(struct device_driver
* ddp
, char * buf
)
1538 return scnprintf(buf
, PAGE_SIZE
, "%d\n", scsi_debug_dsense
);
1540 static ssize_t
sdebug_dsense_store(struct device_driver
* ddp
,
1541 const char * buf
, size_t count
)
1545 if ((count
> 0) && (1 == sscanf(buf
, "%d", &n
)) && (n
>= 0)) {
1546 scsi_debug_dsense
= n
;
1551 DRIVER_ATTR(dsense
, S_IRUGO
| S_IWUSR
, sdebug_dsense_show
,
1552 sdebug_dsense_store
);
1554 static ssize_t
sdebug_num_tgts_show(struct device_driver
* ddp
, char * buf
)
1556 return scnprintf(buf
, PAGE_SIZE
, "%d\n", scsi_debug_num_tgts
);
1558 static ssize_t
sdebug_num_tgts_store(struct device_driver
* ddp
,
1559 const char * buf
, size_t count
)
1563 if ((count
> 0) && (1 == sscanf(buf
, "%d", &n
)) && (n
>= 0)) {
1564 scsi_debug_num_tgts
= n
;
1565 sdebug_max_tgts_luns();
1570 DRIVER_ATTR(num_tgts
, S_IRUGO
| S_IWUSR
, sdebug_num_tgts_show
,
1571 sdebug_num_tgts_store
);
1573 static ssize_t
sdebug_dev_size_mb_show(struct device_driver
* ddp
, char * buf
)
1575 return scnprintf(buf
, PAGE_SIZE
, "%d\n", scsi_debug_dev_size_mb
);
1577 DRIVER_ATTR(dev_size_mb
, S_IRUGO
, sdebug_dev_size_mb_show
, NULL
);
1579 static ssize_t
sdebug_num_parts_show(struct device_driver
* ddp
, char * buf
)
1581 return scnprintf(buf
, PAGE_SIZE
, "%d\n", scsi_debug_num_parts
);
1583 DRIVER_ATTR(num_parts
, S_IRUGO
, sdebug_num_parts_show
, NULL
);
1585 static ssize_t
sdebug_every_nth_show(struct device_driver
* ddp
, char * buf
)
1587 return scnprintf(buf
, PAGE_SIZE
, "%d\n", scsi_debug_every_nth
);
1589 static ssize_t
sdebug_every_nth_store(struct device_driver
* ddp
,
1590 const char * buf
, size_t count
)
1594 if ((count
> 0) && (1 == sscanf(buf
, "%d", &nth
))) {
1595 scsi_debug_every_nth
= nth
;
1596 scsi_debug_cmnd_count
= 0;
1601 DRIVER_ATTR(every_nth
, S_IRUGO
| S_IWUSR
, sdebug_every_nth_show
,
1602 sdebug_every_nth_store
);
1604 static ssize_t
sdebug_max_luns_show(struct device_driver
* ddp
, char * buf
)
1606 return scnprintf(buf
, PAGE_SIZE
, "%d\n", scsi_debug_max_luns
);
1608 static ssize_t
sdebug_max_luns_store(struct device_driver
* ddp
,
1609 const char * buf
, size_t count
)
1613 if ((count
> 0) && (1 == sscanf(buf
, "%d", &n
)) && (n
>= 0)) {
1614 scsi_debug_max_luns
= n
;
1615 sdebug_max_tgts_luns();
1620 DRIVER_ATTR(max_luns
, S_IRUGO
| S_IWUSR
, sdebug_max_luns_show
,
1621 sdebug_max_luns_store
);
1623 static ssize_t
sdebug_scsi_level_show(struct device_driver
* ddp
, char * buf
)
1625 return scnprintf(buf
, PAGE_SIZE
, "%d\n", scsi_debug_scsi_level
);
1627 DRIVER_ATTR(scsi_level
, S_IRUGO
, sdebug_scsi_level_show
, NULL
);
1629 static ssize_t
sdebug_add_host_show(struct device_driver
* ddp
, char * buf
)
1631 return scnprintf(buf
, PAGE_SIZE
, "%d\n", scsi_debug_add_host
);
1634 static ssize_t
sdebug_add_host_store(struct device_driver
* ddp
,
1635 const char * buf
, size_t count
)
1640 if (1 != sscanf(buf
, "%10s", work
))
1642 { /* temporary hack around sscanf() problem with -ve nums */
1647 if (1 != sscanf(work
+ neg
, "%d", &delta_hosts
))
1650 delta_hosts
= -delta_hosts
;
1652 if (delta_hosts
> 0) {
1654 sdebug_add_adapter();
1655 } while (--delta_hosts
);
1656 } else if (delta_hosts
< 0) {
1658 sdebug_remove_adapter();
1659 } while (++delta_hosts
);
1663 DRIVER_ATTR(add_host
, S_IRUGO
| S_IWUSR
, sdebug_add_host_show
,
1664 sdebug_add_host_store
);
1666 static void do_create_driverfs_files(void)
1668 driver_create_file(&sdebug_driverfs_driver
, &driver_attr_add_host
);
1669 driver_create_file(&sdebug_driverfs_driver
, &driver_attr_delay
);
1670 driver_create_file(&sdebug_driverfs_driver
, &driver_attr_dev_size_mb
);
1671 driver_create_file(&sdebug_driverfs_driver
, &driver_attr_dsense
);
1672 driver_create_file(&sdebug_driverfs_driver
, &driver_attr_every_nth
);
1673 driver_create_file(&sdebug_driverfs_driver
, &driver_attr_max_luns
);
1674 driver_create_file(&sdebug_driverfs_driver
, &driver_attr_num_tgts
);
1675 driver_create_file(&sdebug_driverfs_driver
, &driver_attr_num_parts
);
1676 driver_create_file(&sdebug_driverfs_driver
, &driver_attr_ptype
);
1677 driver_create_file(&sdebug_driverfs_driver
, &driver_attr_opts
);
1678 driver_create_file(&sdebug_driverfs_driver
, &driver_attr_scsi_level
);
1681 static void do_remove_driverfs_files(void)
1683 driver_remove_file(&sdebug_driverfs_driver
, &driver_attr_scsi_level
);
1684 driver_remove_file(&sdebug_driverfs_driver
, &driver_attr_opts
);
1685 driver_remove_file(&sdebug_driverfs_driver
, &driver_attr_ptype
);
1686 driver_remove_file(&sdebug_driverfs_driver
, &driver_attr_num_parts
);
1687 driver_remove_file(&sdebug_driverfs_driver
, &driver_attr_num_tgts
);
1688 driver_remove_file(&sdebug_driverfs_driver
, &driver_attr_max_luns
);
1689 driver_remove_file(&sdebug_driverfs_driver
, &driver_attr_every_nth
);
1690 driver_remove_file(&sdebug_driverfs_driver
, &driver_attr_dsense
);
1691 driver_remove_file(&sdebug_driverfs_driver
, &driver_attr_dev_size_mb
);
1692 driver_remove_file(&sdebug_driverfs_driver
, &driver_attr_delay
);
1693 driver_remove_file(&sdebug_driverfs_driver
, &driver_attr_add_host
);
1696 static int __init
scsi_debug_init(void)
1702 if (scsi_debug_dev_size_mb
< 1)
1703 scsi_debug_dev_size_mb
= 1; /* force minimum 1 MB ramdisk */
1704 sdebug_store_size
= (unsigned long)scsi_debug_dev_size_mb
* 1048576;
1705 sdebug_capacity
= sdebug_store_size
/ SECT_SIZE
;
1707 /* play around with geometry, don't waste too much on track 0 */
1709 sdebug_sectors_per
= 32;
1710 if (scsi_debug_dev_size_mb
>= 16)
1712 else if (scsi_debug_dev_size_mb
>= 256)
1714 sdebug_cylinders_per
= (unsigned long)sdebug_capacity
/
1715 (sdebug_sectors_per
* sdebug_heads
);
1716 if (sdebug_cylinders_per
>= 1024) {
1717 /* other LLDs do this; implies >= 1GB ram disk ... */
1719 sdebug_sectors_per
= 63;
1720 sdebug_cylinders_per
= (unsigned long)sdebug_capacity
/
1721 (sdebug_sectors_per
* sdebug_heads
);
1724 sz
= sdebug_store_size
;
1725 fake_storep
= vmalloc(sz
);
1726 if (NULL
== fake_storep
) {
1727 printk(KERN_ERR
"scsi_debug_init: out of memory, 1\n");
1730 memset(fake_storep
, 0, sz
);
1731 if (scsi_debug_num_parts
> 0)
1732 sdebug_build_parts(fake_storep
);
1736 device_register(&pseudo_primary
);
1737 bus_register(&pseudo_lld_bus
);
1738 driver_register(&sdebug_driverfs_driver
);
1739 do_create_driverfs_files();
1741 sdebug_driver_template
.proc_name
= (char *)sdebug_proc_name
;
1743 host_to_add
= scsi_debug_add_host
;
1744 scsi_debug_add_host
= 0;
1746 for (k
= 0; k
< host_to_add
; k
++) {
1747 if (sdebug_add_adapter()) {
1748 printk(KERN_ERR
"scsi_debug_init: "
1749 "sdebug_add_adapter failed k=%d\n", k
);
1754 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
) {
1755 printk(KERN_INFO
"scsi_debug_init: built %d host(s)\n",
1756 scsi_debug_add_host
);
1761 static void __exit
scsi_debug_exit(void)
1763 int k
= scsi_debug_add_host
;
1767 sdebug_remove_adapter();
1768 do_remove_driverfs_files();
1769 driver_unregister(&sdebug_driverfs_driver
);
1770 bus_unregister(&pseudo_lld_bus
);
1771 device_unregister(&pseudo_primary
);
1776 device_initcall(scsi_debug_init
);
1777 module_exit(scsi_debug_exit
);
1779 static void pseudo_0_release(struct device
* dev
)
1781 if (SCSI_DEBUG_OPT_NOISE
& scsi_debug_opts
)
1782 printk(KERN_INFO
"scsi_debug: pseudo_0_release() called\n");
1785 static struct device pseudo_primary
= {
1786 .bus_id
= "pseudo_0",
1787 .release
= pseudo_0_release
,
1790 static int pseudo_lld_bus_match(struct device
*dev
,
1791 struct device_driver
*dev_driver
)
1796 static struct bus_type pseudo_lld_bus
= {
1798 .match
= pseudo_lld_bus_match
,
1801 static void sdebug_release_adapter(struct device
* dev
)
1803 struct sdebug_host_info
*sdbg_host
;
1805 sdbg_host
= to_sdebug_host(dev
);
1809 static int sdebug_add_adapter(void)
1811 int k
, devs_per_host
;
1813 struct sdebug_host_info
*sdbg_host
;
1814 struct sdebug_dev_info
*sdbg_devinfo
;
1815 struct list_head
*lh
, *lh_sf
;
1817 sdbg_host
= kmalloc(sizeof(*sdbg_host
),GFP_KERNEL
);
1819 if (NULL
== sdbg_host
) {
1820 printk(KERN_ERR
"%s: out of memory at line %d\n",
1821 __FUNCTION__
, __LINE__
);
1825 memset(sdbg_host
, 0, sizeof(*sdbg_host
));
1826 INIT_LIST_HEAD(&sdbg_host
->dev_info_list
);
1828 devs_per_host
= scsi_debug_num_tgts
* scsi_debug_max_luns
;
1829 for (k
= 0; k
< devs_per_host
; k
++) {
1830 sdbg_devinfo
= kmalloc(sizeof(*sdbg_devinfo
),GFP_KERNEL
);
1831 if (NULL
== sdbg_devinfo
) {
1832 printk(KERN_ERR
"%s: out of memory at line %d\n",
1833 __FUNCTION__
, __LINE__
);
1837 memset(sdbg_devinfo
, 0, sizeof(*sdbg_devinfo
));
1838 sdbg_devinfo
->sdbg_host
= sdbg_host
;
1839 list_add_tail(&sdbg_devinfo
->dev_list
,
1840 &sdbg_host
->dev_info_list
);
1843 spin_lock(&sdebug_host_list_lock
);
1844 list_add_tail(&sdbg_host
->host_list
, &sdebug_host_list
);
1845 spin_unlock(&sdebug_host_list_lock
);
1847 sdbg_host
->dev
.bus
= &pseudo_lld_bus
;
1848 sdbg_host
->dev
.parent
= &pseudo_primary
;
1849 sdbg_host
->dev
.release
= &sdebug_release_adapter
;
1850 sprintf(sdbg_host
->dev
.bus_id
, "adapter%d", scsi_debug_add_host
);
1852 error
= device_register(&sdbg_host
->dev
);
1857 ++scsi_debug_add_host
;
1861 list_for_each_safe(lh
, lh_sf
, &sdbg_host
->dev_info_list
) {
1862 sdbg_devinfo
= list_entry(lh
, struct sdebug_dev_info
,
1864 list_del(&sdbg_devinfo
->dev_list
);
1865 kfree(sdbg_devinfo
);
1872 static void sdebug_remove_adapter(void)
1874 struct sdebug_host_info
* sdbg_host
= NULL
;
1876 spin_lock(&sdebug_host_list_lock
);
1877 if (!list_empty(&sdebug_host_list
)) {
1878 sdbg_host
= list_entry(sdebug_host_list
.prev
,
1879 struct sdebug_host_info
, host_list
);
1880 list_del(&sdbg_host
->host_list
);
1882 spin_unlock(&sdebug_host_list_lock
);
1887 device_unregister(&sdbg_host
->dev
);
1888 --scsi_debug_add_host
;
1891 static int sdebug_driver_probe(struct device
* dev
)
1894 struct sdebug_host_info
*sdbg_host
;
1895 struct Scsi_Host
*hpnt
;
1897 sdbg_host
= to_sdebug_host(dev
);
1899 hpnt
= scsi_host_alloc(&sdebug_driver_template
, sizeof(sdbg_host
));
1901 printk(KERN_ERR
"%s: scsi_register failed\n", __FUNCTION__
);
1906 sdbg_host
->shost
= hpnt
;
1907 *((struct sdebug_host_info
**)hpnt
->hostdata
) = sdbg_host
;
1908 if ((hpnt
->this_id
>= 0) && (scsi_debug_num_tgts
> hpnt
->this_id
))
1909 hpnt
->max_id
= scsi_debug_num_tgts
+ 1;
1911 hpnt
->max_id
= scsi_debug_num_tgts
;
1912 hpnt
->max_lun
= scsi_debug_max_luns
;
1914 error
= scsi_add_host(hpnt
, &sdbg_host
->dev
);
1916 printk(KERN_ERR
"%s: scsi_add_host failed\n", __FUNCTION__
);
1918 scsi_host_put(hpnt
);
1920 scsi_scan_host(hpnt
);
1926 static int sdebug_driver_remove(struct device
* dev
)
1928 struct list_head
*lh
, *lh_sf
;
1929 struct sdebug_host_info
*sdbg_host
;
1930 struct sdebug_dev_info
*sdbg_devinfo
;
1932 sdbg_host
= to_sdebug_host(dev
);
1935 printk(KERN_ERR
"%s: Unable to locate host info\n",
1940 scsi_remove_host(sdbg_host
->shost
);
1942 list_for_each_safe(lh
, lh_sf
, &sdbg_host
->dev_info_list
) {
1943 sdbg_devinfo
= list_entry(lh
, struct sdebug_dev_info
,
1945 list_del(&sdbg_devinfo
->dev_list
);
1946 kfree(sdbg_devinfo
);
1949 scsi_host_put(sdbg_host
->shost
);
1953 static void sdebug_max_tgts_luns(void)
1955 struct sdebug_host_info
* sdbg_host
;
1956 struct Scsi_Host
*hpnt
;
1958 spin_lock(&sdebug_host_list_lock
);
1959 list_for_each_entry(sdbg_host
, &sdebug_host_list
, host_list
) {
1960 hpnt
= sdbg_host
->shost
;
1961 if ((hpnt
->this_id
>= 0) &&
1962 (scsi_debug_num_tgts
> hpnt
->this_id
))
1963 hpnt
->max_id
= scsi_debug_num_tgts
+ 1;
1965 hpnt
->max_id
= scsi_debug_num_tgts
;
1966 hpnt
->max_lun
= scsi_debug_max_luns
;
1968 spin_unlock(&sdebug_host_list_lock
);