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