1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv 4 * Copyright (C) 1992 Eric Youngdale 5 * Simulate a host adapter with 2 disks attached. Do a lot of checking 6 * to make sure that we are not getting blocks mixed up, and PANIC if 7 * anything out of the ordinary is seen. 8 * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 9 * 10 * Copyright (C) 2001 - 2021 Douglas Gilbert 11 * 12 * For documentation see http://sg.danny.cz/sg/scsi_debug.html 13 */ 14 15 16 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ 17 18 #include <linux/module.h> 19 #include <linux/align.h> 20 #include <linux/kernel.h> 21 #include <linux/errno.h> 22 #include <linux/jiffies.h> 23 #include <linux/slab.h> 24 #include <linux/types.h> 25 #include <linux/string.h> 26 #include <linux/fs.h> 27 #include <linux/init.h> 28 #include <linux/proc_fs.h> 29 #include <linux/vmalloc.h> 30 #include <linux/moduleparam.h> 31 #include <linux/scatterlist.h> 32 #include <linux/blkdev.h> 33 #include <linux/crc-t10dif.h> 34 #include <linux/spinlock.h> 35 #include <linux/interrupt.h> 36 #include <linux/atomic.h> 37 #include <linux/hrtimer.h> 38 #include <linux/uuid.h> 39 #include <linux/t10-pi.h> 40 #include <linux/msdos_partition.h> 41 #include <linux/random.h> 42 #include <linux/xarray.h> 43 #include <linux/prefetch.h> 44 #include <linux/debugfs.h> 45 #include <linux/async.h> 46 #include <linux/cleanup.h> 47 48 #include <net/checksum.h> 49 50 #include <linux/unaligned.h> 51 52 #include <scsi/scsi.h> 53 #include <scsi/scsi_cmnd.h> 54 #include <scsi/scsi_device.h> 55 #include <scsi/scsi_host.h> 56 #include <scsi/scsicam.h> 57 #include <scsi/scsi_eh.h> 58 #include <scsi/scsi_tcq.h> 59 #include <scsi/scsi_dbg.h> 60 61 #include "sd.h" 62 #include "scsi_logging.h" 63 64 /* make sure inq_product_rev string corresponds to this version */ 65 #define SDEBUG_VERSION "0191" /* format to fit INQUIRY revision field */ 66 static const char *sdebug_version_date = "20210520"; 67 68 #define MY_NAME "scsi_debug" 69 70 /* Additional Sense Code Qualifier (ASCQ) */ 71 #define ACK_NAK_TO 0x3 72 73 /* Default values for driver parameters */ 74 #define DEF_NUM_HOST 1 75 #define DEF_NUM_TGTS 1 76 #define DEF_MAX_LUNS 1 77 /* With these defaults, this driver will make 1 host with 1 target 78 * (id 0) containing 1 logical unit (lun 0). That is 1 device. 79 */ 80 #define DEF_ATO 1 81 #define DEF_CDB_LEN 10 82 #define DEF_JDELAY 1 /* if > 0 unit is a jiffy */ 83 #define DEF_DEV_SIZE_PRE_INIT 0 84 #define DEF_DEV_SIZE_MB 8 85 #define DEF_ZBC_DEV_SIZE_MB 128 86 #define DEF_DIF 0 87 #define DEF_DIX 0 88 #define DEF_PER_HOST_STORE false 89 #define DEF_D_SENSE 0 90 #define DEF_EVERY_NTH 0 91 #define DEF_FAKE_RW 0 92 #define DEF_GUARD 0 93 #define DEF_HOST_LOCK 0 94 #define DEF_LBPU 0 95 #define DEF_LBPWS 0 96 #define DEF_LBPWS10 0 97 #define DEF_LBPRZ 1 98 #define DEF_LOWEST_ALIGNED 0 99 #define DEF_NDELAY 0 /* if > 0 unit is a nanosecond */ 100 #define DEF_NO_LUN_0 0 101 #define DEF_NUM_PARTS 0 102 #define DEF_OPTS 0 103 #define DEF_OPT_BLKS 1024 104 #define DEF_PHYSBLK_EXP 0 105 #define DEF_OPT_XFERLEN_EXP 0 106 #define DEF_PTYPE TYPE_DISK 107 #define DEF_RANDOM false 108 #define DEF_REMOVABLE false 109 #define DEF_SCSI_LEVEL 7 /* INQUIRY, byte2 [6->SPC-4; 7->SPC-5] */ 110 #define DEF_SECTOR_SIZE 512 111 #define DEF_UNMAP_ALIGNMENT 0 112 #define DEF_UNMAP_GRANULARITY 1 113 #define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF 114 #define DEF_UNMAP_MAX_DESC 256 115 #define DEF_VIRTUAL_GB 0 116 #define DEF_VPD_USE_HOSTNO 1 117 #define DEF_WRITESAME_LENGTH 0xFFFF 118 #define DEF_ATOMIC_WR 0 119 #define DEF_ATOMIC_WR_MAX_LENGTH 128 120 #define DEF_ATOMIC_WR_ALIGN 2 121 #define DEF_ATOMIC_WR_GRAN 2 122 #define DEF_ATOMIC_WR_MAX_LENGTH_BNDRY (DEF_ATOMIC_WR_MAX_LENGTH) 123 #define DEF_ATOMIC_WR_MAX_BNDRY 128 124 #define DEF_STRICT 0 125 #define DEF_STATISTICS false 126 #define DEF_SUBMIT_QUEUES 1 127 #define DEF_TUR_MS_TO_READY 0 128 #define DEF_UUID_CTL 0 129 #define JDELAY_OVERRIDDEN -9999 130 131 /* Default parameters for ZBC drives */ 132 #define DEF_ZBC_ZONE_SIZE_MB 128 133 #define DEF_ZBC_MAX_OPEN_ZONES 8 134 #define DEF_ZBC_NR_CONV_ZONES 1 135 136 /* Default parameters for tape drives */ 137 #define TAPE_DEF_DENSITY 0x0 138 #define TAPE_BAD_DENSITY 0x65 139 #define TAPE_DEF_BLKSIZE 0 140 #define TAPE_MIN_BLKSIZE 512 141 #define TAPE_MAX_BLKSIZE 1048576 142 #define TAPE_EW 20 143 #define TAPE_MAX_PARTITIONS 2 144 #define TAPE_UNITS 10000 145 #define TAPE_PARTITION_1_UNITS 1000 146 147 /* The tape block data definitions */ 148 #define TAPE_BLOCK_FM_FLAG ((u32)0x1 << 30) 149 #define TAPE_BLOCK_EOD_FLAG ((u32)0x2 << 30) 150 #define TAPE_BLOCK_MARK_MASK ((u32)0x3 << 30) 151 #define TAPE_BLOCK_SIZE_MASK (~TAPE_BLOCK_MARK_MASK) 152 #define TAPE_BLOCK_MARK(a) (a & TAPE_BLOCK_MARK_MASK) 153 #define TAPE_BLOCK_SIZE(a) (a & TAPE_BLOCK_SIZE_MASK) 154 #define IS_TAPE_BLOCK_FM(a) ((a & TAPE_BLOCK_FM_FLAG) != 0) 155 #define IS_TAPE_BLOCK_EOD(a) ((a & TAPE_BLOCK_EOD_FLAG) != 0) 156 157 struct tape_block { 158 u32 fl_size; 159 unsigned char data[4]; 160 }; 161 162 /* Flags for sense data */ 163 #define SENSE_FLAG_FILEMARK 0x80 164 #define SENSE_FLAG_EOM 0x40 165 #define SENSE_FLAG_ILI 0x20 166 167 #define SDEBUG_LUN_0_VAL 0 168 169 /* bit mask values for sdebug_opts */ 170 #define SDEBUG_OPT_NOISE 1 171 #define SDEBUG_OPT_MEDIUM_ERR 2 172 #define SDEBUG_OPT_TIMEOUT 4 173 #define SDEBUG_OPT_RECOVERED_ERR 8 174 #define SDEBUG_OPT_TRANSPORT_ERR 16 175 #define SDEBUG_OPT_DIF_ERR 32 176 #define SDEBUG_OPT_DIX_ERR 64 177 #define SDEBUG_OPT_MAC_TIMEOUT 128 178 #define SDEBUG_OPT_SHORT_TRANSFER 0x100 179 #define SDEBUG_OPT_Q_NOISE 0x200 180 #define SDEBUG_OPT_ALL_TSF 0x400 /* ignore */ 181 #define SDEBUG_OPT_RARE_TSF 0x800 182 #define SDEBUG_OPT_N_WCE 0x1000 183 #define SDEBUG_OPT_RESET_NOISE 0x2000 184 #define SDEBUG_OPT_NO_CDB_NOISE 0x4000 185 #define SDEBUG_OPT_HOST_BUSY 0x8000 186 #define SDEBUG_OPT_CMD_ABORT 0x10000 187 #define SDEBUG_OPT_UNALIGNED_WRITE 0x20000 188 #define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \ 189 SDEBUG_OPT_RESET_NOISE) 190 #define SDEBUG_OPT_RECOV_DIF_DIX (SDEBUG_OPT_RECOVERED_ERR | \ 191 SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR) 192 193 /* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs) are returned in 194 * priority order. In the subset implemented here lower numbers have higher 195 * priority. The UA numbers should be a sequence starting from 0 with 196 * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */ 197 #define SDEBUG_UA_POR 0 /* Power on, reset, or bus device reset */ 198 #define SDEBUG_UA_POOCCUR 1 /* Power on occurred */ 199 #define SDEBUG_UA_BUS_RESET 2 200 #define SDEBUG_UA_MODE_CHANGED 3 201 #define SDEBUG_UA_CAPACITY_CHANGED 4 202 #define SDEBUG_UA_LUNS_CHANGED 5 203 #define SDEBUG_UA_MICROCODE_CHANGED 6 /* simulate firmware change */ 204 #define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 7 205 #define SDEBUG_UA_NOT_READY_TO_READY 8 206 #define SDEBUG_NUM_UAS 9 207 208 /* when 1==SDEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this 209 * sector on read commands: */ 210 #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */ 211 #define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */ 212 213 /* SDEBUG_CANQUEUE is the maximum number of commands that can be queued 214 * (for response) per submit queue at one time. Can be reduced by max_queue 215 * option. Command responses are not queued when jdelay=0 and ndelay=0. The 216 * per-device DEF_CMD_PER_LUN can be changed via sysfs: 217 * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth 218 * but cannot exceed SDEBUG_CANQUEUE . 219 */ 220 #define SDEBUG_CANQUEUE_WORDS 3 /* a WORD is bits in a long */ 221 #define SDEBUG_CANQUEUE (SDEBUG_CANQUEUE_WORDS * BITS_PER_LONG) 222 #define DEF_CMD_PER_LUN SDEBUG_CANQUEUE 223 224 /* UA - Unit Attention; SA - Service Action; SSU - Start Stop Unit */ 225 #define F_D_IN 1 /* Data-in command (e.g. READ) */ 226 #define F_D_OUT 2 /* Data-out command (e.g. WRITE) */ 227 #define F_D_OUT_MAYBE 4 /* WRITE SAME, NDOB bit */ 228 #define F_D_UNKN 8 229 #define F_RL_WLUN_OK 0x10 /* allowed with REPORT LUNS W-LUN */ 230 #define F_SKIP_UA 0x20 /* bypass UAs (e.g. INQUIRY command) */ 231 #define F_DELAY_OVERR 0x40 /* for commands like INQUIRY */ 232 #define F_SA_LOW 0x80 /* SA is in cdb byte 1, bits 4 to 0 */ 233 #define F_SA_HIGH 0x100 /* SA is in cdb bytes 8 and 9 */ 234 #define F_INV_OP 0x200 /* invalid opcode (not supported) */ 235 #define F_FAKE_RW 0x400 /* bypass resp_*() when fake_rw set */ 236 #define F_M_ACCESS 0x800 /* media access, reacts to SSU state */ 237 #define F_SSU_DELAY 0x1000 /* SSU command delay (long-ish) */ 238 #define F_SYNC_DELAY 0x2000 /* SYNCHRONIZE CACHE delay */ 239 240 /* Useful combinations of the above flags */ 241 #define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR) 242 #define FF_MEDIA_IO (F_M_ACCESS | F_FAKE_RW) 243 #define FF_SA (F_SA_HIGH | F_SA_LOW) 244 #define F_LONG_DELAY (F_SSU_DELAY | F_SYNC_DELAY) 245 246 /* Device selection bit mask */ 247 #define DS_ALL 0xffffffff 248 #define DS_SBC (1 << TYPE_DISK) 249 #define DS_SSC (1 << TYPE_TAPE) 250 #define DS_ZBC (1 << TYPE_ZBC) 251 252 #define DS_NO_SSC (DS_ALL & ~DS_SSC) 253 254 #define SDEBUG_MAX_PARTS 4 255 256 #define SDEBUG_MAX_CMD_LEN 32 257 258 #define SDEB_XA_NOT_IN_USE XA_MARK_1 259 260 /* Zone types (zbcr05 table 25) */ 261 enum sdebug_z_type { 262 ZBC_ZTYPE_CNV = 0x1, 263 ZBC_ZTYPE_SWR = 0x2, 264 ZBC_ZTYPE_SWP = 0x3, 265 /* ZBC_ZTYPE_SOBR = 0x4, */ 266 ZBC_ZTYPE_GAP = 0x5, 267 }; 268 269 /* enumeration names taken from table 26, zbcr05 */ 270 enum sdebug_z_cond { 271 ZBC_NOT_WRITE_POINTER = 0x0, 272 ZC1_EMPTY = 0x1, 273 ZC2_IMPLICIT_OPEN = 0x2, 274 ZC3_EXPLICIT_OPEN = 0x3, 275 ZC4_CLOSED = 0x4, 276 ZC6_READ_ONLY = 0xd, 277 ZC5_FULL = 0xe, 278 ZC7_OFFLINE = 0xf, 279 }; 280 281 struct sdeb_zone_state { /* ZBC: per zone state */ 282 enum sdebug_z_type z_type; 283 enum sdebug_z_cond z_cond; 284 bool z_non_seq_resource; 285 unsigned int z_size; 286 sector_t z_start; 287 sector_t z_wp; 288 }; 289 290 enum sdebug_err_type { 291 ERR_TMOUT_CMD = 0, /* make specific scsi command timeout */ 292 ERR_FAIL_QUEUE_CMD = 1, /* make specific scsi command's */ 293 /* queuecmd return failed */ 294 ERR_FAIL_CMD = 2, /* make specific scsi command's */ 295 /* queuecmd return succeed but */ 296 /* with errors set in scsi_cmnd */ 297 ERR_ABORT_CMD_FAILED = 3, /* control return FAILED from */ 298 /* scsi_debug_abort() */ 299 ERR_LUN_RESET_FAILED = 4, /* control return FAILED from */ 300 /* scsi_debug_device_reseLUN_RESET_FAILEDt() */ 301 }; 302 303 struct sdebug_err_inject { 304 int type; 305 struct list_head list; 306 int cnt; 307 unsigned char cmd; 308 struct rcu_head rcu; 309 310 union { 311 /* 312 * For ERR_FAIL_QUEUE_CMD 313 */ 314 int queuecmd_ret; 315 316 /* 317 * For ERR_FAIL_CMD 318 */ 319 struct { 320 unsigned char host_byte; 321 unsigned char driver_byte; 322 unsigned char status_byte; 323 unsigned char sense_key; 324 unsigned char asc; 325 unsigned char asq; 326 }; 327 }; 328 }; 329 330 struct sdebug_dev_info { 331 struct list_head dev_list; 332 unsigned int channel; 333 unsigned int target; 334 u64 lun; 335 uuid_t lu_name; 336 struct sdebug_host_info *sdbg_host; 337 unsigned long uas_bm[1]; 338 atomic_t stopped; /* 1: by SSU, 2: device start */ 339 bool used; 340 341 /* For ZBC devices */ 342 bool zoned; 343 unsigned int zcap; 344 unsigned int zsize; 345 unsigned int zsize_shift; 346 unsigned int nr_zones; 347 unsigned int nr_conv_zones; 348 unsigned int nr_seq_zones; 349 unsigned int nr_imp_open; 350 unsigned int nr_exp_open; 351 unsigned int nr_closed; 352 unsigned int max_open; 353 ktime_t create_ts; /* time since bootup that this device was created */ 354 struct sdeb_zone_state *zstate; 355 356 /* For tapes */ 357 unsigned int tape_blksize; 358 unsigned int tape_density; 359 unsigned char tape_partition; 360 unsigned char tape_nbr_partitions; 361 unsigned char tape_pending_nbr_partitions; 362 unsigned int tape_pending_part_0_size; 363 unsigned int tape_pending_part_1_size; 364 unsigned char tape_dce; 365 unsigned int tape_location[TAPE_MAX_PARTITIONS]; 366 unsigned int tape_eop[TAPE_MAX_PARTITIONS]; 367 struct tape_block *tape_blocks[TAPE_MAX_PARTITIONS]; 368 369 struct dentry *debugfs_entry; 370 struct spinlock list_lock; 371 struct list_head inject_err_list; 372 }; 373 374 struct sdebug_target_info { 375 bool reset_fail; 376 struct dentry *debugfs_entry; 377 }; 378 379 struct sdebug_host_info { 380 struct list_head host_list; 381 int si_idx; /* sdeb_store_info (per host) xarray index */ 382 struct Scsi_Host *shost; 383 struct device dev; 384 struct list_head dev_info_list; 385 }; 386 387 /* There is an xarray of pointers to this struct's objects, one per host */ 388 struct sdeb_store_info { 389 rwlock_t macc_data_lck; /* for media data access on this store */ 390 rwlock_t macc_meta_lck; /* for atomic media meta access on this store */ 391 rwlock_t macc_sector_lck; /* per-sector media data access on this store */ 392 u8 *storep; /* user data storage (ram) */ 393 struct t10_pi_tuple *dif_storep; /* protection info */ 394 void *map_storep; /* provisioning map */ 395 }; 396 397 #define dev_to_sdebug_host(d) \ 398 container_of(d, struct sdebug_host_info, dev) 399 400 #define shost_to_sdebug_host(shost) \ 401 dev_to_sdebug_host(shost->dma_dev) 402 403 enum sdeb_defer_type {SDEB_DEFER_NONE = 0, SDEB_DEFER_HRT = 1, 404 SDEB_DEFER_WQ = 2, SDEB_DEFER_POLL = 3}; 405 406 struct sdebug_defer { 407 struct hrtimer hrt; 408 struct execute_work ew; 409 ktime_t cmpl_ts;/* time since boot to complete this cmd */ 410 int issuing_cpu; 411 bool aborted; /* true when blk_abort_request() already called */ 412 enum sdeb_defer_type defer_t; 413 }; 414 415 struct sdebug_scsi_cmd { 416 spinlock_t lock; 417 struct sdebug_defer sd_dp; 418 }; 419 420 static atomic_t sdebug_cmnd_count; /* number of incoming commands */ 421 static atomic_t sdebug_completions; /* count of deferred completions */ 422 static atomic_t sdebug_miss_cpus; /* submission + completion cpus differ */ 423 static atomic_t sdebug_a_tsf; /* 'almost task set full' counter */ 424 static atomic_t sdeb_inject_pending; 425 static atomic_t sdeb_mq_poll_count; /* bumped when mq_poll returns > 0 */ 426 427 struct opcode_info_t { 428 u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff */ 429 /* for terminating element */ 430 u8 opcode; /* if num_attached > 0, preferred */ 431 u16 sa; /* service action */ 432 u32 devsel; /* device type mask for this definition */ 433 u32 flags; /* OR-ed set of SDEB_F_* */ 434 int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); 435 const struct opcode_info_t *arrp; /* num_attached elements or NULL */ 436 u8 len_mask[16]; /* len_mask[0]-->cdb_len, then mask for cdb */ 437 /* 1 to min(cdb_len, 15); ignore cdb[15...] */ 438 }; 439 440 /* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */ 441 enum sdeb_opcode_index { 442 SDEB_I_INVALID_OPCODE = 0, 443 SDEB_I_INQUIRY = 1, 444 SDEB_I_REPORT_LUNS = 2, 445 SDEB_I_REQUEST_SENSE = 3, 446 SDEB_I_TEST_UNIT_READY = 4, 447 SDEB_I_MODE_SENSE = 5, /* 6, 10 */ 448 SDEB_I_MODE_SELECT = 6, /* 6, 10 */ 449 SDEB_I_LOG_SENSE = 7, 450 SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */ 451 SDEB_I_READ = 9, /* 6, 10, 12, 16 */ 452 SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */ 453 SDEB_I_START_STOP = 11, 454 SDEB_I_SERV_ACT_IN_16 = 12, /* add ...SERV_ACT_IN_12 if needed */ 455 SDEB_I_SERV_ACT_OUT_16 = 13, /* add ...SERV_ACT_OUT_12 if needed */ 456 SDEB_I_MAINT_IN = 14, 457 SDEB_I_MAINT_OUT = 15, 458 SDEB_I_VERIFY = 16, /* VERIFY(10), VERIFY(16) */ 459 SDEB_I_VARIABLE_LEN = 17, /* READ(32), WRITE(32), WR_SCAT(32) */ 460 SDEB_I_RESERVE = 18, /* 6, 10 */ 461 SDEB_I_RELEASE = 19, /* 6, 10 */ 462 SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */ 463 SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */ 464 SDEB_I_ATA_PT = 22, /* 12, 16 */ 465 SDEB_I_SEND_DIAG = 23, 466 SDEB_I_UNMAP = 24, 467 SDEB_I_WRITE_BUFFER = 25, 468 SDEB_I_WRITE_SAME = 26, /* 10, 16 */ 469 SDEB_I_SYNC_CACHE = 27, /* 10, 16 */ 470 SDEB_I_COMP_WRITE = 28, 471 SDEB_I_PRE_FETCH = 29, /* 10, 16 */ 472 SDEB_I_ZONE_OUT = 30, /* 0x94+SA; includes no data xfer */ 473 SDEB_I_ZONE_IN = 31, /* 0x95+SA; all have data-in */ 474 SDEB_I_ATOMIC_WRITE_16 = 32, 475 SDEB_I_READ_BLOCK_LIMITS = 33, 476 SDEB_I_LOCATE = 34, 477 SDEB_I_WRITE_FILEMARKS = 35, 478 SDEB_I_SPACE = 36, 479 SDEB_I_FORMAT_MEDIUM = 37, 480 SDEB_I_ERASE = 38, 481 SDEB_I_LAST_ELEM_P1 = 39, /* keep this last (previous + 1) */ 482 }; 483 484 485 static const unsigned char opcode_ind_arr[256] = { 486 /* 0x0; 0x0->0x1f: 6 byte cdbs */ 487 SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE, 488 SDEB_I_FORMAT_MEDIUM, SDEB_I_READ_BLOCK_LIMITS, 0, 0, 489 SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0, 490 SDEB_I_WRITE_FILEMARKS, SDEB_I_SPACE, SDEB_I_INQUIRY, 0, 0, 491 SDEB_I_MODE_SELECT, SDEB_I_RESERVE, SDEB_I_RELEASE, 492 0, SDEB_I_ERASE, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG, 493 SDEB_I_ALLOW_REMOVAL, 0, 494 /* 0x20; 0x20->0x3f: 10 byte cdbs */ 495 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0, 496 SDEB_I_READ, 0, SDEB_I_WRITE, SDEB_I_LOCATE, 0, 0, 0, SDEB_I_VERIFY, 497 0, 0, 0, 0, SDEB_I_PRE_FETCH, SDEB_I_SYNC_CACHE, 0, 0, 498 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0, 499 /* 0x40; 0x40->0x5f: 10 byte cdbs */ 500 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0, 501 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0, 502 0, 0, 0, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE, 503 SDEB_I_RELEASE, 504 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0, 505 /* 0x60; 0x60->0x7d are reserved, 0x7e is "extended cdb" */ 506 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 507 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 508 0, SDEB_I_VARIABLE_LEN, 509 /* 0x80; 0x80->0x9f: 16 byte cdbs */ 510 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0, 511 SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0, 512 0, 0, 0, SDEB_I_VERIFY, 513 SDEB_I_PRE_FETCH, SDEB_I_SYNC_CACHE, 0, SDEB_I_WRITE_SAME, 514 SDEB_I_ZONE_OUT, SDEB_I_ZONE_IN, 0, 0, 515 0, 0, 0, 0, 516 SDEB_I_ATOMIC_WRITE_16, 0, SDEB_I_SERV_ACT_IN_16, SDEB_I_SERV_ACT_OUT_16, 517 /* 0xa0; 0xa0->0xbf: 12 byte cdbs */ 518 SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN, 519 SDEB_I_MAINT_OUT, 0, 0, 0, 520 SDEB_I_READ, 0 /* SDEB_I_SERV_ACT_OUT_12 */, SDEB_I_WRITE, 521 0 /* SDEB_I_SERV_ACT_IN_12 */, 0, 0, 0, 0, 522 0, 0, 0, 0, 0, 0, 0, 0, 523 0, 0, 0, 0, 0, 0, 0, 0, 524 /* 0xc0; 0xc0->0xff: vendor specific */ 525 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 526 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 527 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 528 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 529 }; 530 531 /* 532 * The following "response" functions return the SCSI mid-level's 4 byte 533 * tuple-in-an-int. To handle commands with an IMMED bit, for a faster 534 * command completion, they can mask their return value with 535 * SDEG_RES_IMMED_MASK . 536 */ 537 #define SDEG_RES_IMMED_MASK 0x40000000 538 539 static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *); 540 static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *); 541 static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *); 542 static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *); 543 static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *); 544 static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *); 545 static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *); 546 static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); 547 static int resp_read_tape(struct scsi_cmnd *, struct sdebug_dev_info *); 548 static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); 549 static int resp_write_tape(struct scsi_cmnd *, struct sdebug_dev_info *); 550 static int resp_write_scat(struct scsi_cmnd *, struct sdebug_dev_info *); 551 static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *); 552 static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *); 553 static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *); 554 static int resp_get_stream_status(struct scsi_cmnd *scp, 555 struct sdebug_dev_info *devip); 556 static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *); 557 static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *); 558 static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *); 559 static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *); 560 static int resp_verify(struct scsi_cmnd *, struct sdebug_dev_info *); 561 static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *); 562 static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *); 563 static int resp_comp_write(struct scsi_cmnd *, struct sdebug_dev_info *); 564 static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *); 565 static int resp_sync_cache(struct scsi_cmnd *, struct sdebug_dev_info *); 566 static int resp_pre_fetch(struct scsi_cmnd *, struct sdebug_dev_info *); 567 static int resp_report_zones(struct scsi_cmnd *, struct sdebug_dev_info *); 568 static int resp_atomic_write(struct scsi_cmnd *, struct sdebug_dev_info *); 569 static int resp_open_zone(struct scsi_cmnd *, struct sdebug_dev_info *); 570 static int resp_close_zone(struct scsi_cmnd *, struct sdebug_dev_info *); 571 static int resp_finish_zone(struct scsi_cmnd *, struct sdebug_dev_info *); 572 static int resp_rwp_zone(struct scsi_cmnd *, struct sdebug_dev_info *); 573 static int resp_read_blklimits(struct scsi_cmnd *, struct sdebug_dev_info *); 574 static int resp_locate(struct scsi_cmnd *, struct sdebug_dev_info *); 575 static int resp_write_filemarks(struct scsi_cmnd *, struct sdebug_dev_info *); 576 static int resp_space(struct scsi_cmnd *, struct sdebug_dev_info *); 577 static int resp_read_position(struct scsi_cmnd *, struct sdebug_dev_info *); 578 static int resp_rewind(struct scsi_cmnd *, struct sdebug_dev_info *); 579 static int resp_format_medium(struct scsi_cmnd *, struct sdebug_dev_info *); 580 static int resp_erase(struct scsi_cmnd *, struct sdebug_dev_info *); 581 582 static int sdebug_do_add_host(bool mk_new_store); 583 static int sdebug_add_host_helper(int per_host_idx); 584 static void sdebug_do_remove_host(bool the_end); 585 static int sdebug_add_store(void); 586 static void sdebug_erase_store(int idx, struct sdeb_store_info *sip); 587 static void sdebug_erase_all_stores(bool apart_from_first); 588 589 /* 590 * The following are overflow arrays for cdbs that "hit" the same index in 591 * the opcode_info_arr array. The most time sensitive (or commonly used) cdb 592 * should be placed in opcode_info_arr[], the others should be placed here. 593 */ 594 static const struct opcode_info_t msense_iarr[] = { 595 {0, 0x1a, 0, DS_ALL, F_D_IN, NULL, NULL, 596 {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 597 }; 598 599 static const struct opcode_info_t mselect_iarr[] = { 600 {0, 0x15, 0, DS_ALL, F_D_OUT, NULL, NULL, 601 {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 602 }; 603 604 static const struct opcode_info_t read_iarr[] = { 605 {0, 0x28, 0, DS_NO_SSC, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(10) */ 606 {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0, 607 0, 0, 0, 0} }, 608 {0, 0x8, 0, DS_NO_SSC, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL, /* READ(6) disk */ 609 {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 610 {0, 0x8, 0, DS_SSC, F_D_IN | FF_MEDIA_IO, resp_read_tape, NULL, /* READ(6) tape */ 611 {6, 0x03, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 612 {0, 0xa8, 0, DS_NO_SSC, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(12) */ 613 {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, 614 0xc7, 0, 0, 0, 0} }, 615 }; 616 617 static const struct opcode_info_t write_iarr[] = { 618 {0, 0x2a, 0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(10) */ 619 NULL, {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 620 0, 0, 0, 0, 0, 0} }, 621 {0, 0xa, 0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(6) disk */ 622 NULL, {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 623 0, 0, 0} }, 624 {0, 0xa, 0, DS_SSC, F_D_OUT | FF_MEDIA_IO, resp_write_tape, /* WRITE(6) tape */ 625 NULL, {6, 0x01, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 626 0, 0, 0} }, 627 {0, 0xaa, 0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(12) */ 628 NULL, {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 629 0xbf, 0xc7, 0, 0, 0, 0} }, 630 }; 631 632 static const struct opcode_info_t verify_iarr[] = { 633 {0, 0x2f, 0, DS_NO_SSC, F_D_OUT_MAYBE | FF_MEDIA_IO, resp_verify,/* VERIFY(10) */ 634 NULL, {10, 0xf7, 0xff, 0xff, 0xff, 0xff, 0xbf, 0xff, 0xff, 0xc7, 635 0, 0, 0, 0, 0, 0} }, 636 }; 637 638 static const struct opcode_info_t sa_in_16_iarr[] = { 639 {0, 0x9e, 0x12, DS_NO_SSC, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL, 640 {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 641 0xff, 0xff, 0xff, 0, 0xc7} }, /* GET LBA STATUS(16) */ 642 {0, 0x9e, 0x16, DS_NO_SSC, F_SA_LOW | F_D_IN, resp_get_stream_status, NULL, 643 {16, 0x16, 0, 0, 0xff, 0xff, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 644 0, 0} }, /* GET STREAM STATUS */ 645 }; 646 647 static const struct opcode_info_t vl_iarr[] = { /* VARIABLE LENGTH */ 648 {0, 0x7f, 0xb, DS_NO_SSC, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_dt0, 649 NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0xb, 0xfa, 650 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */ 651 {0, 0x7f, 0x11, DS_NO_SSC, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_scat, 652 NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x11, 0xf8, 653 0, 0xff, 0xff, 0x0, 0x0} }, /* WRITE SCATTERED(32) */ 654 }; 655 656 static const struct opcode_info_t maint_in_iarr[] = { /* MAINT IN */ 657 {0, 0xa3, 0xc, DS_ALL, F_SA_LOW | F_D_IN, resp_rsup_opcodes, NULL, 658 {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 659 0xc7, 0, 0, 0, 0} }, /* REPORT SUPPORTED OPERATION CODES */ 660 {0, 0xa3, 0xd, DS_ALL, F_SA_LOW | F_D_IN, resp_rsup_tmfs, NULL, 661 {12, 0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, 662 0, 0} }, /* REPORTED SUPPORTED TASK MANAGEMENT FUNCTIONS */ 663 }; 664 665 static const struct opcode_info_t write_same_iarr[] = { 666 {0, 0x93, 0, DS_NO_SSC, F_D_OUT_MAYBE | FF_MEDIA_IO, resp_write_same_16, NULL, 667 {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 668 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* WRITE SAME(16) */ 669 }; 670 671 static const struct opcode_info_t reserve_iarr[] = { 672 {0, 0x16, 0, DS_ALL, F_D_OUT, NULL, NULL, /* RESERVE(6) */ 673 {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 674 }; 675 676 static const struct opcode_info_t release_iarr[] = { 677 {0, 0x17, 0, DS_ALL, F_D_OUT, NULL, NULL, /* RELEASE(6) */ 678 {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 679 }; 680 681 static const struct opcode_info_t sync_cache_iarr[] = { 682 {0, 0x91, 0, DS_NO_SSC, F_SYNC_DELAY | F_M_ACCESS, resp_sync_cache, NULL, 683 {16, 0x6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 684 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* SYNC_CACHE (16) */ 685 }; 686 687 static const struct opcode_info_t pre_fetch_iarr[] = { 688 {0, 0x90, 0, DS_NO_SSC, F_SYNC_DELAY | FF_MEDIA_IO, resp_pre_fetch, NULL, 689 {16, 0x2, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 690 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* PRE-FETCH (16) */ 691 {0, 0x34, 0, DS_SSC, F_SYNC_DELAY | FF_MEDIA_IO, resp_read_position, NULL, 692 {10, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc7, 0, 0, 693 0, 0, 0, 0} }, /* READ POSITION (10) */ 694 }; 695 696 static const struct opcode_info_t zone_out_iarr[] = { /* ZONE OUT(16) */ 697 {0, 0x94, 0x1, DS_NO_SSC, F_SA_LOW | F_M_ACCESS, resp_close_zone, NULL, 698 {16, 0x1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 699 0xff, 0, 0, 0xff, 0xff, 0x1, 0xc7} }, /* CLOSE ZONE */ 700 {0, 0x94, 0x2, DS_NO_SSC, F_SA_LOW | F_M_ACCESS, resp_finish_zone, NULL, 701 {16, 0x2, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 702 0xff, 0, 0, 0xff, 0xff, 0x1, 0xc7} }, /* FINISH ZONE */ 703 {0, 0x94, 0x4, DS_NO_SSC, F_SA_LOW | F_M_ACCESS, resp_rwp_zone, NULL, 704 {16, 0x4, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 705 0xff, 0, 0, 0xff, 0xff, 0x1, 0xc7} }, /* RESET WRITE POINTER */ 706 }; 707 708 static const struct opcode_info_t zone_in_iarr[] = { /* ZONE IN(16) */ 709 {0, 0x95, 0x6, DS_NO_SSC, F_SA_LOW | F_D_IN | F_M_ACCESS, NULL, NULL, 710 {16, 0x6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 711 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* REPORT ZONES */ 712 }; 713 714 715 /* This array is accessed via SDEB_I_* values. Make sure all are mapped, 716 * plus the terminating elements for logic that scans this table such as 717 * REPORT SUPPORTED OPERATION CODES. */ 718 static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEM_P1 + 1] = { 719 /* 0 */ 720 {0, 0, 0, DS_ALL, F_INV_OP | FF_RESPOND, NULL, NULL, /* unknown opcodes */ 721 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 722 {0, 0x12, 0, DS_ALL, FF_RESPOND | F_D_IN, resp_inquiry, NULL, /* INQUIRY */ 723 {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 724 {0, 0xa0, 0, DS_ALL, FF_RESPOND | F_D_IN, resp_report_luns, NULL, 725 {12, 0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, 726 0, 0} }, /* REPORT LUNS */ 727 {0, 0x3, 0, DS_ALL, FF_RESPOND | F_D_IN, resp_requests, NULL, 728 {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 729 {0, 0x0, 0, DS_ALL, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */ 730 {6, 0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 731 /* 5 */ 732 {ARRAY_SIZE(msense_iarr), 0x5a, 0, DS_ALL, F_D_IN, /* MODE SENSE(10) */ 733 resp_mode_sense, msense_iarr, {10, 0xf8, 0xff, 0xff, 0, 0, 0, 734 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, 735 {ARRAY_SIZE(mselect_iarr), 0x55, 0, DS_ALL, F_D_OUT, /* MODE SELECT(10) */ 736 resp_mode_select, mselect_iarr, {10, 0xf1, 0, 0, 0, 0, 0, 0xff, 737 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, 738 {0, 0x4d, 0, DS_NO_SSC, F_D_IN, resp_log_sense, NULL, /* LOG SENSE */ 739 {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 740 0, 0, 0} }, 741 {0, 0x25, 0, DS_NO_SSC, F_D_IN, resp_readcap, NULL, /* READ CAPACITY(10) */ 742 {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0, 743 0, 0} }, 744 {ARRAY_SIZE(read_iarr), 0x88, 0, DS_NO_SSC, F_D_IN | FF_MEDIA_IO, /* READ(16) */ 745 resp_read_dt0, read_iarr, {16, 0xfe, 0xff, 0xff, 0xff, 0xff, 746 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} }, 747 /* 10 */ 748 {ARRAY_SIZE(write_iarr), 0x8a, 0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO, 749 resp_write_dt0, write_iarr, /* WRITE(16) */ 750 {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 751 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} }, 752 {0, 0x1b, 0, DS_ALL, F_SSU_DELAY, resp_start_stop, NULL,/* START STOP UNIT */ 753 {6, 0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 754 {ARRAY_SIZE(sa_in_16_iarr), 0x9e, 0x10, DS_NO_SSC, F_SA_LOW | F_D_IN, 755 resp_readcap16, sa_in_16_iarr, /* SA_IN(16), READ CAPACITY(16) */ 756 {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 757 0xff, 0xff, 0xff, 0xff, 0x1, 0xc7} }, 758 {0, 0x9f, 0x12, DS_NO_SSC, F_SA_LOW | F_D_OUT | FF_MEDIA_IO, resp_write_scat, 759 NULL, {16, 0x12, 0xf9, 0x0, 0xff, 0xff, 0, 0, 0xff, 0xff, 0xff, 760 0xff, 0xff, 0xff, 0xff, 0xc7} }, /* SA_OUT(16), WRITE SCAT(16) */ 761 {ARRAY_SIZE(maint_in_iarr), 0xa3, 0xa, DS_ALL, F_SA_LOW | F_D_IN, 762 resp_report_tgtpgs, /* MAINT IN, REPORT TARGET PORT GROUPS */ 763 maint_in_iarr, {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff, 764 0xff, 0, 0xc7, 0, 0, 0, 0} }, 765 /* 15 */ 766 {0, 0, 0, DS_ALL, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */ 767 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 768 {ARRAY_SIZE(verify_iarr), 0x8f, 0, DS_NO_SSC, 769 F_D_OUT_MAYBE | FF_MEDIA_IO, resp_verify, /* VERIFY(16) */ 770 verify_iarr, {16, 0xf6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 771 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} }, 772 {ARRAY_SIZE(vl_iarr), 0x7f, 0x9, DS_NO_SSC, F_SA_HIGH | F_D_IN | FF_MEDIA_IO, 773 resp_read_dt0, vl_iarr, /* VARIABLE LENGTH, READ(32) */ 774 {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x9, 0xfe, 0, 0xff, 0xff, 775 0xff, 0xff} }, 776 {ARRAY_SIZE(reserve_iarr), 0x56, 0, DS_ALL, F_D_OUT, 777 NULL, reserve_iarr, /* RESERVE(10) <no response function> */ 778 {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 779 0} }, 780 {ARRAY_SIZE(release_iarr), 0x57, 0, DS_ALL, F_D_OUT, 781 NULL, release_iarr, /* RELEASE(10) <no response function> */ 782 {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 783 0} }, 784 /* 20 */ 785 {0, 0x1e, 0, DS_ALL, 0, NULL, NULL, /* ALLOW REMOVAL */ 786 {6, 0, 0, 0, 0x3, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 787 {0, 0x1, 0, DS_SSC, 0, resp_rewind, NULL, 788 {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 789 {0, 0, 0, DS_NO_SSC, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */ 790 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 791 {0, 0x1d, 0, DS_ALL, F_D_OUT, NULL, NULL, /* SEND DIAGNOSTIC */ 792 {6, 0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 793 {0, 0x42, 0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO, resp_unmap, NULL, /* UNMAP */ 794 {10, 0x1, 0, 0, 0, 0, 0x3f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, 795 /* 25 */ 796 {0, 0x3b, 0, DS_NO_SSC, F_D_OUT_MAYBE, resp_write_buffer, NULL, 797 {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 798 0, 0, 0, 0} }, /* WRITE_BUFFER */ 799 {ARRAY_SIZE(write_same_iarr), 0x41, 0, DS_NO_SSC, F_D_OUT_MAYBE | FF_MEDIA_IO, 800 resp_write_same_10, write_same_iarr, /* WRITE SAME(10) */ 801 {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 802 0, 0, 0, 0, 0} }, 803 {ARRAY_SIZE(sync_cache_iarr), 0x35, 0, DS_NO_SSC, F_SYNC_DELAY | F_M_ACCESS, 804 resp_sync_cache, sync_cache_iarr, 805 {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0, 806 0, 0, 0, 0} }, /* SYNC_CACHE (10) */ 807 {0, 0x89, 0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO, resp_comp_write, NULL, 808 {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 809 0, 0xff, 0x3f, 0xc7} }, /* COMPARE AND WRITE */ 810 {ARRAY_SIZE(pre_fetch_iarr), 0x34, 0, DS_NO_SSC, F_SYNC_DELAY | FF_MEDIA_IO, 811 resp_pre_fetch, pre_fetch_iarr, 812 {10, 0x2, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0, 813 0, 0, 0, 0} }, /* PRE-FETCH (10) */ 814 /* READ POSITION (10) */ 815 816 /* 30 */ 817 {ARRAY_SIZE(zone_out_iarr), 0x94, 0x3, DS_NO_SSC, F_SA_LOW | F_M_ACCESS, 818 resp_open_zone, zone_out_iarr, /* ZONE_OUT(16), OPEN ZONE) */ 819 {16, 0x3 /* SA */, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 820 0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x1, 0xc7} }, 821 {ARRAY_SIZE(zone_in_iarr), 0x95, 0x0, DS_NO_SSC, F_SA_LOW | F_M_ACCESS, 822 resp_report_zones, zone_in_iarr, /* ZONE_IN(16), REPORT ZONES) */ 823 {16, 0x0 /* SA */, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 824 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, 0xc7} }, 825 /* 32 */ 826 {0, 0x9c, 0x0, DS_NO_SSC, F_D_OUT | FF_MEDIA_IO, 827 resp_atomic_write, NULL, /* ATOMIC WRITE 16 */ 828 {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 829 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} }, 830 {0, 0x05, 0, DS_SSC, F_D_IN, resp_read_blklimits, NULL, /* READ BLOCK LIMITS (6) */ 831 {6, 0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 832 {0, 0x2b, 0, DS_SSC, F_D_UNKN, resp_locate, NULL, /* LOCATE (10) */ 833 {10, 0x07, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xff, 0xc7, 0, 0, 834 0, 0, 0, 0} }, 835 {0, 0x10, 0, DS_SSC, F_D_IN, resp_write_filemarks, NULL, /* WRITE FILEMARKS (6) */ 836 {6, 0x01, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 837 {0, 0x11, 0, DS_SSC, F_D_IN, resp_space, NULL, /* SPACE (6) */ 838 {6, 0x07, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 839 {0, 0x4, 0, DS_SSC, 0, resp_format_medium, NULL, /* FORMAT MEDIUM (6) */ 840 {6, 0x3, 0x7, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 841 {0, 0x19, 0, DS_SSC, F_D_IN, resp_erase, NULL, /* ERASE (6) */ 842 {6, 0x03, 0x33, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 843 /* 39 */ 844 /* sentinel */ 845 {0xff, 0, 0, 0, 0, NULL, NULL, /* terminating element */ 846 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, 847 }; 848 849 static int sdebug_num_hosts; 850 static int sdebug_add_host = DEF_NUM_HOST; /* in sysfs this is relative */ 851 static int sdebug_ato = DEF_ATO; 852 static int sdebug_cdb_len = DEF_CDB_LEN; 853 static int sdebug_jdelay = DEF_JDELAY; /* if > 0 then unit is jiffies */ 854 static int sdebug_dev_size_mb = DEF_DEV_SIZE_PRE_INIT; 855 static int sdebug_dif = DEF_DIF; 856 static int sdebug_dix = DEF_DIX; 857 static int sdebug_dsense = DEF_D_SENSE; 858 static int sdebug_every_nth = DEF_EVERY_NTH; 859 static int sdebug_fake_rw = DEF_FAKE_RW; 860 static unsigned int sdebug_guard = DEF_GUARD; 861 static int sdebug_host_max_queue; /* per host */ 862 static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED; 863 static int sdebug_max_luns = DEF_MAX_LUNS; 864 static int sdebug_max_queue = SDEBUG_CANQUEUE; /* per submit queue */ 865 static unsigned int sdebug_medium_error_start = OPT_MEDIUM_ERR_ADDR; 866 static int sdebug_medium_error_count = OPT_MEDIUM_ERR_NUM; 867 static int sdebug_ndelay = DEF_NDELAY; /* if > 0 then unit is nanoseconds */ 868 static int sdebug_no_lun_0 = DEF_NO_LUN_0; 869 static int sdebug_no_uld; 870 static int sdebug_num_parts = DEF_NUM_PARTS; 871 static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */ 872 static int sdebug_opt_blks = DEF_OPT_BLKS; 873 static int sdebug_opts = DEF_OPTS; 874 static int sdebug_physblk_exp = DEF_PHYSBLK_EXP; 875 static int sdebug_opt_xferlen_exp = DEF_OPT_XFERLEN_EXP; 876 static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral device type */ 877 static int sdebug_scsi_level = DEF_SCSI_LEVEL; 878 static int sdebug_sector_size = DEF_SECTOR_SIZE; 879 static int sdeb_tur_ms_to_ready = DEF_TUR_MS_TO_READY; 880 static int sdebug_virtual_gb = DEF_VIRTUAL_GB; 881 static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO; 882 static unsigned int sdebug_lbpu = DEF_LBPU; 883 static unsigned int sdebug_lbpws = DEF_LBPWS; 884 static unsigned int sdebug_lbpws10 = DEF_LBPWS10; 885 static unsigned int sdebug_lbprz = DEF_LBPRZ; 886 static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT; 887 static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY; 888 static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS; 889 static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC; 890 static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH; 891 static unsigned int sdebug_atomic_wr = DEF_ATOMIC_WR; 892 static unsigned int sdebug_atomic_wr_max_length = DEF_ATOMIC_WR_MAX_LENGTH; 893 static unsigned int sdebug_atomic_wr_align = DEF_ATOMIC_WR_ALIGN; 894 static unsigned int sdebug_atomic_wr_gran = DEF_ATOMIC_WR_GRAN; 895 static unsigned int sdebug_atomic_wr_max_length_bndry = 896 DEF_ATOMIC_WR_MAX_LENGTH_BNDRY; 897 static unsigned int sdebug_atomic_wr_max_bndry = DEF_ATOMIC_WR_MAX_BNDRY; 898 static int sdebug_uuid_ctl = DEF_UUID_CTL; 899 static bool sdebug_random = DEF_RANDOM; 900 static bool sdebug_per_host_store = DEF_PER_HOST_STORE; 901 static bool sdebug_removable = DEF_REMOVABLE; 902 static bool sdebug_clustering; 903 static bool sdebug_host_lock = DEF_HOST_LOCK; 904 static bool sdebug_strict = DEF_STRICT; 905 static bool sdebug_no_rwlock; 906 static bool sdebug_verbose; 907 static bool have_dif_prot; 908 static bool write_since_sync; 909 static bool sdebug_statistics = DEF_STATISTICS; 910 static bool sdebug_wp; 911 static bool sdebug_allow_restart; 912 static enum { 913 BLK_ZONED_NONE = 0, 914 BLK_ZONED_HA = 1, 915 BLK_ZONED_HM = 2, 916 } sdeb_zbc_model = BLK_ZONED_NONE; 917 static char *sdeb_zbc_model_s; 918 919 enum sam_lun_addr_method {SAM_LUN_AM_PERIPHERAL = 0x0, 920 SAM_LUN_AM_FLAT = 0x1, 921 SAM_LUN_AM_LOGICAL_UNIT = 0x2, 922 SAM_LUN_AM_EXTENDED = 0x3}; 923 static enum sam_lun_addr_method sdebug_lun_am = SAM_LUN_AM_PERIPHERAL; 924 static int sdebug_lun_am_i = (int)SAM_LUN_AM_PERIPHERAL; 925 926 static unsigned int sdebug_store_sectors; 927 static sector_t sdebug_capacity; /* in sectors */ 928 929 /* old BIOS stuff, kernel may get rid of them but some mode sense pages 930 may still need them */ 931 static int sdebug_heads; /* heads per disk */ 932 static int sdebug_cylinders_per; /* cylinders per surface */ 933 static int sdebug_sectors_per; /* sectors per cylinder */ 934 935 static LIST_HEAD(sdebug_host_list); 936 static DEFINE_MUTEX(sdebug_host_list_mutex); 937 938 static struct xarray per_store_arr; 939 static struct xarray *per_store_ap = &per_store_arr; 940 static int sdeb_first_idx = -1; /* invalid index ==> none created */ 941 static int sdeb_most_recent_idx = -1; 942 static DEFINE_RWLOCK(sdeb_fake_rw_lck); /* need a RW lock when fake_rw=1 */ 943 944 static unsigned long map_size; 945 static int num_aborts; 946 static int num_dev_resets; 947 static int num_target_resets; 948 static int num_bus_resets; 949 static int num_host_resets; 950 static int dix_writes; 951 static int dix_reads; 952 static int dif_errors; 953 954 /* ZBC global data */ 955 static bool sdeb_zbc_in_use; /* true for host-aware and host-managed disks */ 956 static int sdeb_zbc_zone_cap_mb; 957 static int sdeb_zbc_zone_size_mb; 958 static int sdeb_zbc_max_open = DEF_ZBC_MAX_OPEN_ZONES; 959 static int sdeb_zbc_nr_conv = DEF_ZBC_NR_CONV_ZONES; 960 961 static int submit_queues = DEF_SUBMIT_QUEUES; /* > 1 for multi-queue (mq) */ 962 static int poll_queues; /* iouring iopoll interface.*/ 963 964 static atomic_long_t writes_by_group_number[64]; 965 966 static char sdebug_proc_name[] = MY_NAME; 967 static const char *my_name = MY_NAME; 968 969 static const struct bus_type pseudo_lld_bus; 970 971 static struct device_driver sdebug_driverfs_driver = { 972 .name = sdebug_proc_name, 973 .bus = &pseudo_lld_bus, 974 }; 975 976 static const int check_condition_result = 977 SAM_STAT_CHECK_CONDITION; 978 979 static const int illegal_condition_result = 980 (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION; 981 982 static const int device_qfull_result = 983 (DID_ABORT << 16) | SAM_STAT_TASK_SET_FULL; 984 985 static const int condition_met_result = SAM_STAT_CONDITION_MET; 986 987 static struct dentry *sdebug_debugfs_root; 988 static ASYNC_DOMAIN_EXCLUSIVE(sdebug_async_domain); 989 990 static u32 sdebug_get_devsel(struct scsi_device *sdp) 991 { 992 unsigned char devtype = sdp->type; 993 u32 devsel; 994 995 if (devtype < 32) 996 devsel = (1 << devtype); 997 else 998 devsel = DS_ALL; 999 1000 return devsel; 1001 } 1002 1003 static void sdebug_err_free(struct rcu_head *head) 1004 { 1005 struct sdebug_err_inject *inject = 1006 container_of(head, typeof(*inject), rcu); 1007 1008 kfree(inject); 1009 } 1010 1011 static void sdebug_err_add(struct scsi_device *sdev, struct sdebug_err_inject *new) 1012 { 1013 struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdev->hostdata; 1014 struct sdebug_err_inject *err; 1015 1016 spin_lock(&devip->list_lock); 1017 list_for_each_entry_rcu(err, &devip->inject_err_list, list) { 1018 if (err->type == new->type && err->cmd == new->cmd) { 1019 list_del_rcu(&err->list); 1020 call_rcu(&err->rcu, sdebug_err_free); 1021 } 1022 } 1023 1024 list_add_tail_rcu(&new->list, &devip->inject_err_list); 1025 spin_unlock(&devip->list_lock); 1026 } 1027 1028 static int sdebug_err_remove(struct scsi_device *sdev, const char *buf, size_t count) 1029 { 1030 struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdev->hostdata; 1031 struct sdebug_err_inject *err; 1032 int type; 1033 unsigned char cmd; 1034 1035 if (sscanf(buf, "- %d %hhx", &type, &cmd) != 2) { 1036 kfree(buf); 1037 return -EINVAL; 1038 } 1039 1040 spin_lock(&devip->list_lock); 1041 list_for_each_entry_rcu(err, &devip->inject_err_list, list) { 1042 if (err->type == type && err->cmd == cmd) { 1043 list_del_rcu(&err->list); 1044 call_rcu(&err->rcu, sdebug_err_free); 1045 spin_unlock(&devip->list_lock); 1046 kfree(buf); 1047 return count; 1048 } 1049 } 1050 spin_unlock(&devip->list_lock); 1051 1052 kfree(buf); 1053 return -EINVAL; 1054 } 1055 1056 static int sdebug_error_show(struct seq_file *m, void *p) 1057 { 1058 struct scsi_device *sdev = (struct scsi_device *)m->private; 1059 struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdev->hostdata; 1060 struct sdebug_err_inject *err; 1061 1062 seq_puts(m, "Type\tCount\tCommand\n"); 1063 1064 rcu_read_lock(); 1065 list_for_each_entry_rcu(err, &devip->inject_err_list, list) { 1066 switch (err->type) { 1067 case ERR_TMOUT_CMD: 1068 case ERR_ABORT_CMD_FAILED: 1069 case ERR_LUN_RESET_FAILED: 1070 seq_printf(m, "%d\t%d\t0x%x\n", err->type, err->cnt, 1071 err->cmd); 1072 break; 1073 1074 case ERR_FAIL_QUEUE_CMD: 1075 seq_printf(m, "%d\t%d\t0x%x\t0x%x\n", err->type, 1076 err->cnt, err->cmd, err->queuecmd_ret); 1077 break; 1078 1079 case ERR_FAIL_CMD: 1080 seq_printf(m, "%d\t%d\t0x%x\t0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", 1081 err->type, err->cnt, err->cmd, 1082 err->host_byte, err->driver_byte, 1083 err->status_byte, err->sense_key, 1084 err->asc, err->asq); 1085 break; 1086 } 1087 } 1088 rcu_read_unlock(); 1089 1090 return 0; 1091 } 1092 1093 static int sdebug_error_open(struct inode *inode, struct file *file) 1094 { 1095 return single_open(file, sdebug_error_show, inode->i_private); 1096 } 1097 1098 static ssize_t sdebug_error_write(struct file *file, const char __user *ubuf, 1099 size_t count, loff_t *ppos) 1100 { 1101 char *buf; 1102 unsigned int inject_type; 1103 struct sdebug_err_inject *inject; 1104 struct scsi_device *sdev = (struct scsi_device *)file->f_inode->i_private; 1105 1106 buf = memdup_user_nul(ubuf, count); 1107 if (IS_ERR(buf)) 1108 return PTR_ERR(buf); 1109 1110 if (buf[0] == '-') 1111 return sdebug_err_remove(sdev, buf, count); 1112 1113 if (sscanf(buf, "%d", &inject_type) != 1) { 1114 kfree(buf); 1115 return -EINVAL; 1116 } 1117 1118 inject = kzalloc_obj(struct sdebug_err_inject); 1119 if (!inject) { 1120 kfree(buf); 1121 return -ENOMEM; 1122 } 1123 1124 switch (inject_type) { 1125 case ERR_TMOUT_CMD: 1126 case ERR_ABORT_CMD_FAILED: 1127 case ERR_LUN_RESET_FAILED: 1128 if (sscanf(buf, "%d %d %hhx", &inject->type, &inject->cnt, 1129 &inject->cmd) != 3) 1130 goto out_error; 1131 break; 1132 1133 case ERR_FAIL_QUEUE_CMD: 1134 if (sscanf(buf, "%d %d %hhx %x", &inject->type, &inject->cnt, 1135 &inject->cmd, &inject->queuecmd_ret) != 4) 1136 goto out_error; 1137 break; 1138 1139 case ERR_FAIL_CMD: 1140 if (sscanf(buf, "%d %d %hhx %hhx %hhx %hhx %hhx %hhx %hhx", 1141 &inject->type, &inject->cnt, &inject->cmd, 1142 &inject->host_byte, &inject->driver_byte, 1143 &inject->status_byte, &inject->sense_key, 1144 &inject->asc, &inject->asq) != 9) 1145 goto out_error; 1146 break; 1147 1148 default: 1149 goto out_error; 1150 break; 1151 } 1152 1153 kfree(buf); 1154 sdebug_err_add(sdev, inject); 1155 1156 return count; 1157 1158 out_error: 1159 kfree(buf); 1160 kfree(inject); 1161 return -EINVAL; 1162 } 1163 1164 static const struct file_operations sdebug_error_fops = { 1165 .open = sdebug_error_open, 1166 .read = seq_read, 1167 .write = sdebug_error_write, 1168 .release = single_release, 1169 }; 1170 1171 static int sdebug_target_reset_fail_show(struct seq_file *m, void *p) 1172 { 1173 struct scsi_target *starget = (struct scsi_target *)m->private; 1174 struct sdebug_target_info *targetip = 1175 (struct sdebug_target_info *)starget->hostdata; 1176 1177 if (targetip) 1178 seq_printf(m, "%c\n", targetip->reset_fail ? 'Y' : 'N'); 1179 1180 return 0; 1181 } 1182 1183 static int sdebug_target_reset_fail_open(struct inode *inode, struct file *file) 1184 { 1185 return single_open(file, sdebug_target_reset_fail_show, inode->i_private); 1186 } 1187 1188 static ssize_t sdebug_target_reset_fail_write(struct file *file, 1189 const char __user *ubuf, size_t count, loff_t *ppos) 1190 { 1191 int ret; 1192 struct scsi_target *starget = 1193 (struct scsi_target *)file->f_inode->i_private; 1194 struct sdebug_target_info *targetip = 1195 (struct sdebug_target_info *)starget->hostdata; 1196 1197 if (targetip) { 1198 ret = kstrtobool_from_user(ubuf, count, &targetip->reset_fail); 1199 return ret < 0 ? ret : count; 1200 } 1201 return -ENODEV; 1202 } 1203 1204 static const struct file_operations sdebug_target_reset_fail_fops = { 1205 .open = sdebug_target_reset_fail_open, 1206 .read = seq_read, 1207 .write = sdebug_target_reset_fail_write, 1208 .release = single_release, 1209 }; 1210 1211 static int sdebug_target_alloc(struct scsi_target *starget) 1212 { 1213 struct sdebug_target_info *targetip; 1214 1215 targetip = kzalloc_obj(struct sdebug_target_info); 1216 if (!targetip) 1217 return -ENOMEM; 1218 1219 async_synchronize_full_domain(&sdebug_async_domain); 1220 1221 targetip->debugfs_entry = debugfs_create_dir(dev_name(&starget->dev), 1222 sdebug_debugfs_root); 1223 1224 debugfs_create_file("fail_reset", 0600, targetip->debugfs_entry, starget, 1225 &sdebug_target_reset_fail_fops); 1226 1227 starget->hostdata = targetip; 1228 1229 return 0; 1230 } 1231 1232 static void sdebug_tartget_cleanup_async(void *data, async_cookie_t cookie) 1233 { 1234 struct sdebug_target_info *targetip = data; 1235 1236 debugfs_remove(targetip->debugfs_entry); 1237 kfree(targetip); 1238 } 1239 1240 static void sdebug_target_destroy(struct scsi_target *starget) 1241 { 1242 struct sdebug_target_info *targetip; 1243 1244 targetip = (struct sdebug_target_info *)starget->hostdata; 1245 if (targetip) { 1246 starget->hostdata = NULL; 1247 async_schedule_domain(sdebug_tartget_cleanup_async, targetip, 1248 &sdebug_async_domain); 1249 } 1250 } 1251 1252 /* Only do the extra work involved in logical block provisioning if one or 1253 * more of the lbpu, lbpws or lbpws10 parameters are given and we are doing 1254 * real reads and writes (i.e. not skipping them for speed). 1255 */ 1256 static inline bool scsi_debug_lbp(void) 1257 { 1258 return 0 == sdebug_fake_rw && 1259 (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10); 1260 } 1261 1262 static inline bool scsi_debug_atomic_write(void) 1263 { 1264 return sdebug_fake_rw == 0 && sdebug_atomic_wr; 1265 } 1266 1267 static void *lba2fake_store(struct sdeb_store_info *sip, 1268 unsigned long long lba) 1269 { 1270 struct sdeb_store_info *lsip = sip; 1271 1272 lba = do_div(lba, sdebug_store_sectors); 1273 if (!sip || !sip->storep) { 1274 WARN_ON_ONCE(true); 1275 lsip = xa_load(per_store_ap, 0); /* should never be NULL */ 1276 } 1277 return lsip->storep + lba * sdebug_sector_size; 1278 } 1279 1280 static struct t10_pi_tuple *dif_store(struct sdeb_store_info *sip, 1281 sector_t sector) 1282 { 1283 sector = sector_div(sector, sdebug_store_sectors); 1284 1285 return sip->dif_storep + sector; 1286 } 1287 1288 static void sdebug_max_tgts_luns(void) 1289 { 1290 struct sdebug_host_info *sdbg_host; 1291 struct Scsi_Host *hpnt; 1292 1293 mutex_lock(&sdebug_host_list_mutex); 1294 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { 1295 hpnt = sdbg_host->shost; 1296 if ((hpnt->this_id >= 0) && 1297 (sdebug_num_tgts > hpnt->this_id)) 1298 hpnt->max_id = sdebug_num_tgts + 1; 1299 else 1300 hpnt->max_id = sdebug_num_tgts; 1301 /* sdebug_max_luns; */ 1302 hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; 1303 } 1304 mutex_unlock(&sdebug_host_list_mutex); 1305 } 1306 1307 enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1}; 1308 1309 /* Set in_bit to -1 to indicate no bit position of invalid field */ 1310 static void mk_sense_invalid_fld(struct scsi_cmnd *scp, 1311 enum sdeb_cmd_data c_d, 1312 int in_byte, int in_bit) 1313 { 1314 unsigned char *sbuff; 1315 u8 sks[4]; 1316 int sl, asc; 1317 1318 sbuff = scp->sense_buffer; 1319 if (!sbuff) { 1320 sdev_printk(KERN_ERR, scp->device, "sense_buffer is NULL\n"); 1321 return; 1322 } 1323 asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST; 1324 memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE); 1325 scsi_build_sense(scp, sdebug_dsense, ILLEGAL_REQUEST, asc, 0); 1326 memset(sks, 0, sizeof(sks)); 1327 sks[0] = 0x80; 1328 if (c_d) 1329 sks[0] |= 0x40; 1330 if (in_bit >= 0) { 1331 sks[0] |= 0x8; 1332 sks[0] |= 0x7 & in_bit; 1333 } 1334 put_unaligned_be16(in_byte, sks + 1); 1335 if (sdebug_dsense) { 1336 sl = sbuff[7] + 8; 1337 sbuff[7] = sl; 1338 sbuff[sl] = 0x2; 1339 sbuff[sl + 1] = 0x6; 1340 memcpy(sbuff + sl + 4, sks, 3); 1341 } else 1342 memcpy(sbuff + 15, sks, 3); 1343 if (sdebug_verbose) 1344 sdev_printk(KERN_INFO, scp->device, "%s: [sense_key,asc,ascq" 1345 "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n", 1346 my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit); 1347 } 1348 1349 static void mk_sense_buffer(struct scsi_cmnd *scp, int key, int asc, int asq) 1350 { 1351 if (!scp->sense_buffer) { 1352 sdev_printk(KERN_ERR, scp->device, "sense_buffer is NULL\n"); 1353 return; 1354 } 1355 memset(scp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); 1356 1357 scsi_build_sense(scp, sdebug_dsense, key, asc, asq); 1358 1359 if (sdebug_verbose) 1360 sdev_printk(KERN_INFO, scp->device, 1361 "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n", 1362 my_name, key, asc, asq); 1363 } 1364 1365 /* Sense data that has information fields for tapes */ 1366 static void mk_sense_info_tape(struct scsi_cmnd *scp, int key, int asc, int asq, 1367 unsigned int information, unsigned char tape_flags) 1368 { 1369 if (!scp->sense_buffer) { 1370 sdev_printk(KERN_ERR, scp->device, "sense_buffer is NULL\n"); 1371 return; 1372 } 1373 memset(scp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); 1374 1375 scsi_build_sense(scp, /* sdebug_dsense */ 0, key, asc, asq); 1376 /* only fixed format so far */ 1377 1378 scp->sense_buffer[0] |= 0x80; /* valid */ 1379 scp->sense_buffer[2] |= tape_flags; 1380 put_unaligned_be32(information, &scp->sense_buffer[3]); 1381 1382 if (sdebug_verbose) 1383 sdev_printk(KERN_INFO, scp->device, 1384 "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n", 1385 my_name, key, asc, asq); 1386 } 1387 1388 static void mk_sense_invalid_opcode(struct scsi_cmnd *scp) 1389 { 1390 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0); 1391 } 1392 1393 static int scsi_debug_ioctl(struct scsi_device *dev, unsigned int cmd, 1394 void __user *arg) 1395 { 1396 if (sdebug_verbose) { 1397 if (0x1261 == cmd) 1398 sdev_printk(KERN_INFO, dev, "BLKFLSBUF [0x1261]\n"); 1399 else if (0x5331 == cmd) 1400 sdev_printk(KERN_INFO, dev, 1401 "CDROM_GET_CAPABILITY [0x5331]\n"); 1402 else 1403 sdev_printk(KERN_INFO, dev, "cmd=0x%x\n", cmd); 1404 } 1405 return -EINVAL; 1406 /* return -ENOTTY; // correct return but upsets fdisk */ 1407 } 1408 1409 static void config_cdb_len(struct scsi_device *sdev) 1410 { 1411 switch (sdebug_cdb_len) { 1412 case 6: /* suggest 6 byte READ, WRITE and MODE SENSE/SELECT */ 1413 sdev->use_10_for_rw = false; 1414 sdev->use_16_for_rw = false; 1415 sdev->use_10_for_ms = false; 1416 break; 1417 case 10: /* suggest 10 byte RWs and 6 byte MODE SENSE/SELECT */ 1418 sdev->use_10_for_rw = true; 1419 sdev->use_16_for_rw = false; 1420 sdev->use_10_for_ms = false; 1421 break; 1422 case 12: /* suggest 10 byte RWs and 10 byte MODE SENSE/SELECT */ 1423 sdev->use_10_for_rw = true; 1424 sdev->use_16_for_rw = false; 1425 sdev->use_10_for_ms = true; 1426 break; 1427 case 16: 1428 sdev->use_10_for_rw = false; 1429 sdev->use_16_for_rw = true; 1430 sdev->use_10_for_ms = true; 1431 break; 1432 case 32: /* No knobs to suggest this so same as 16 for now */ 1433 sdev->use_10_for_rw = false; 1434 sdev->use_16_for_rw = true; 1435 sdev->use_10_for_ms = true; 1436 break; 1437 default: 1438 pr_warn("unexpected cdb_len=%d, force to 10\n", 1439 sdebug_cdb_len); 1440 sdev->use_10_for_rw = true; 1441 sdev->use_16_for_rw = false; 1442 sdev->use_10_for_ms = false; 1443 sdebug_cdb_len = 10; 1444 break; 1445 } 1446 } 1447 1448 static void all_config_cdb_len(void) 1449 { 1450 struct sdebug_host_info *sdbg_host; 1451 struct Scsi_Host *shost; 1452 struct scsi_device *sdev; 1453 1454 mutex_lock(&sdebug_host_list_mutex); 1455 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { 1456 shost = sdbg_host->shost; 1457 shost_for_each_device(sdev, shost) { 1458 config_cdb_len(sdev); 1459 } 1460 } 1461 mutex_unlock(&sdebug_host_list_mutex); 1462 } 1463 1464 static void clear_luns_changed_on_target(struct sdebug_dev_info *devip) 1465 { 1466 struct sdebug_host_info *sdhp = devip->sdbg_host; 1467 struct sdebug_dev_info *dp; 1468 1469 list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) { 1470 if ((devip->sdbg_host == dp->sdbg_host) && 1471 (devip->target == dp->target)) { 1472 clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm); 1473 } 1474 } 1475 } 1476 1477 static int make_ua(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) 1478 { 1479 int k; 1480 1481 k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS); 1482 if (k != SDEBUG_NUM_UAS) { 1483 const char *cp = NULL; 1484 1485 switch (k) { 1486 case SDEBUG_UA_POR: 1487 mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC, 1488 POWER_ON_RESET_ASCQ); 1489 if (sdebug_verbose) 1490 cp = "power on reset"; 1491 break; 1492 case SDEBUG_UA_POOCCUR: 1493 mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC, 1494 POWER_ON_OCCURRED_ASCQ); 1495 if (sdebug_verbose) 1496 cp = "power on occurred"; 1497 break; 1498 case SDEBUG_UA_BUS_RESET: 1499 mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC, 1500 BUS_RESET_ASCQ); 1501 if (sdebug_verbose) 1502 cp = "bus reset"; 1503 break; 1504 case SDEBUG_UA_MODE_CHANGED: 1505 mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC, 1506 MODE_CHANGED_ASCQ); 1507 if (sdebug_verbose) 1508 cp = "mode parameters changed"; 1509 break; 1510 case SDEBUG_UA_CAPACITY_CHANGED: 1511 mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC, 1512 CAPACITY_CHANGED_ASCQ); 1513 if (sdebug_verbose) 1514 cp = "capacity data changed"; 1515 break; 1516 case SDEBUG_UA_MICROCODE_CHANGED: 1517 mk_sense_buffer(scp, UNIT_ATTENTION, 1518 TARGET_CHANGED_ASC, 1519 MICROCODE_CHANGED_ASCQ); 1520 if (sdebug_verbose) 1521 cp = "microcode has been changed"; 1522 break; 1523 case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET: 1524 mk_sense_buffer(scp, UNIT_ATTENTION, 1525 TARGET_CHANGED_ASC, 1526 MICROCODE_CHANGED_WO_RESET_ASCQ); 1527 if (sdebug_verbose) 1528 cp = "microcode has been changed without reset"; 1529 break; 1530 case SDEBUG_UA_LUNS_CHANGED: 1531 /* 1532 * SPC-3 behavior is to report a UNIT ATTENTION with 1533 * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN 1534 * on the target, until a REPORT LUNS command is 1535 * received. SPC-4 behavior is to report it only once. 1536 * NOTE: sdebug_scsi_level does not use the same 1537 * values as struct scsi_device->scsi_level. 1538 */ 1539 if (sdebug_scsi_level >= 6) /* SPC-4 and above */ 1540 clear_luns_changed_on_target(devip); 1541 mk_sense_buffer(scp, UNIT_ATTENTION, 1542 TARGET_CHANGED_ASC, 1543 LUNS_CHANGED_ASCQ); 1544 if (sdebug_verbose) 1545 cp = "reported luns data has changed"; 1546 break; 1547 case SDEBUG_UA_NOT_READY_TO_READY: 1548 mk_sense_buffer(scp, UNIT_ATTENTION, UA_READY_ASC, 1549 0); 1550 if (sdebug_verbose) 1551 cp = "not ready to ready transition/media change"; 1552 break; 1553 default: 1554 pr_warn("unexpected unit attention code=%d\n", k); 1555 if (sdebug_verbose) 1556 cp = "unknown"; 1557 break; 1558 } 1559 clear_bit(k, devip->uas_bm); 1560 if (sdebug_verbose) 1561 sdev_printk(KERN_INFO, scp->device, 1562 "%s reports: Unit attention: %s\n", 1563 my_name, cp); 1564 return check_condition_result; 1565 } 1566 return 0; 1567 } 1568 1569 /* Build SCSI "data-in" buffer. Returns 0 if ok else (DID_ERROR << 16). */ 1570 static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr, 1571 int arr_len) 1572 { 1573 int act_len; 1574 struct scsi_data_buffer *sdb = &scp->sdb; 1575 1576 if (!sdb->length) 1577 return 0; 1578 if (scp->sc_data_direction != DMA_FROM_DEVICE) 1579 return DID_ERROR << 16; 1580 1581 act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents, 1582 arr, arr_len); 1583 scsi_set_resid(scp, scsi_bufflen(scp) - act_len); 1584 1585 return 0; 1586 } 1587 1588 /* Partial build of SCSI "data-in" buffer. Returns 0 if ok else 1589 * (DID_ERROR << 16). Can write to offset in data-in buffer. If multiple 1590 * calls, not required to write in ascending offset order. Assumes resid 1591 * set to scsi_bufflen() prior to any calls. 1592 */ 1593 static int p_fill_from_dev_buffer(struct scsi_cmnd *scp, const void *arr, 1594 int arr_len, unsigned int off_dst) 1595 { 1596 unsigned int act_len, n; 1597 struct scsi_data_buffer *sdb = &scp->sdb; 1598 off_t skip = off_dst; 1599 1600 if (sdb->length <= off_dst) 1601 return 0; 1602 if (scp->sc_data_direction != DMA_FROM_DEVICE) 1603 return DID_ERROR << 16; 1604 1605 act_len = sg_pcopy_from_buffer(sdb->table.sgl, sdb->table.nents, 1606 arr, arr_len, skip); 1607 pr_debug("off_dst=%u, scsi_bufflen=%u, act_len=%u, resid=%d\n", 1608 off_dst, scsi_bufflen(scp), act_len, 1609 scsi_get_resid(scp)); 1610 n = scsi_bufflen(scp) - (off_dst + act_len); 1611 scsi_set_resid(scp, min_t(u32, scsi_get_resid(scp), n)); 1612 return 0; 1613 } 1614 1615 /* Fetches from SCSI "data-out" buffer. Returns number of bytes fetched into 1616 * 'arr' or -1 if error. 1617 */ 1618 static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr, 1619 int arr_len) 1620 { 1621 if (!scsi_bufflen(scp)) 1622 return 0; 1623 if (scp->sc_data_direction != DMA_TO_DEVICE) 1624 return -1; 1625 1626 return scsi_sg_copy_to_buffer(scp, arr, arr_len); 1627 } 1628 1629 1630 static char sdebug_inq_vendor_id[9] = "Linux "; 1631 static char sdebug_inq_product_id[17] = "scsi_debug "; 1632 static char sdebug_inq_product_rev[5] = SDEBUG_VERSION; 1633 /* Use some locally assigned NAAs for SAS addresses. */ 1634 static const u64 naa3_comp_a = 0x3222222000000000ULL; 1635 static const u64 naa3_comp_b = 0x3333333000000000ULL; 1636 static const u64 naa3_comp_c = 0x3111111000000000ULL; 1637 1638 /* Device identification VPD page. Returns number of bytes placed in arr */ 1639 static int inquiry_vpd_83(unsigned char *arr, int port_group_id, 1640 int target_dev_id, int dev_id_num, 1641 const char *dev_id_str, int dev_id_str_len, 1642 const uuid_t *lu_name) 1643 { 1644 int num, port_a; 1645 char b[32]; 1646 1647 port_a = target_dev_id + 1; 1648 /* T10 vendor identifier field format (faked) */ 1649 arr[0] = 0x2; /* ASCII */ 1650 arr[1] = 0x1; 1651 arr[2] = 0x0; 1652 memcpy(&arr[4], sdebug_inq_vendor_id, 8); 1653 memcpy(&arr[12], sdebug_inq_product_id, 16); 1654 memcpy(&arr[28], dev_id_str, dev_id_str_len); 1655 num = 8 + 16 + dev_id_str_len; 1656 arr[3] = num; 1657 num += 4; 1658 if (dev_id_num >= 0) { 1659 if (sdebug_uuid_ctl) { 1660 /* Locally assigned UUID */ 1661 arr[num++] = 0x1; /* binary (not necessarily sas) */ 1662 arr[num++] = 0xa; /* PIV=0, lu, naa */ 1663 arr[num++] = 0x0; 1664 arr[num++] = 0x12; 1665 arr[num++] = 0x10; /* uuid type=1, locally assigned */ 1666 arr[num++] = 0x0; 1667 memcpy(arr + num, lu_name, 16); 1668 num += 16; 1669 } else { 1670 /* NAA-3, Logical unit identifier (binary) */ 1671 arr[num++] = 0x1; /* binary (not necessarily sas) */ 1672 arr[num++] = 0x3; /* PIV=0, lu, naa */ 1673 arr[num++] = 0x0; 1674 arr[num++] = 0x8; 1675 put_unaligned_be64(naa3_comp_b + dev_id_num, arr + num); 1676 num += 8; 1677 } 1678 /* Target relative port number */ 1679 arr[num++] = 0x61; /* proto=sas, binary */ 1680 arr[num++] = 0x94; /* PIV=1, target port, rel port */ 1681 arr[num++] = 0x0; /* reserved */ 1682 arr[num++] = 0x4; /* length */ 1683 arr[num++] = 0x0; /* reserved */ 1684 arr[num++] = 0x0; /* reserved */ 1685 arr[num++] = 0x0; 1686 arr[num++] = 0x1; /* relative port A */ 1687 } 1688 /* NAA-3, Target port identifier */ 1689 arr[num++] = 0x61; /* proto=sas, binary */ 1690 arr[num++] = 0x93; /* piv=1, target port, naa */ 1691 arr[num++] = 0x0; 1692 arr[num++] = 0x8; 1693 put_unaligned_be64(naa3_comp_a + port_a, arr + num); 1694 num += 8; 1695 /* NAA-3, Target port group identifier */ 1696 arr[num++] = 0x61; /* proto=sas, binary */ 1697 arr[num++] = 0x95; /* piv=1, target port group id */ 1698 arr[num++] = 0x0; 1699 arr[num++] = 0x4; 1700 arr[num++] = 0; 1701 arr[num++] = 0; 1702 put_unaligned_be16(port_group_id, arr + num); 1703 num += 2; 1704 /* NAA-3, Target device identifier */ 1705 arr[num++] = 0x61; /* proto=sas, binary */ 1706 arr[num++] = 0xa3; /* piv=1, target device, naa */ 1707 arr[num++] = 0x0; 1708 arr[num++] = 0x8; 1709 put_unaligned_be64(naa3_comp_a + target_dev_id, arr + num); 1710 num += 8; 1711 /* SCSI name string: Target device identifier */ 1712 arr[num++] = 0x63; /* proto=sas, UTF-8 */ 1713 arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */ 1714 arr[num++] = 0x0; 1715 arr[num++] = 24; 1716 memcpy(arr + num, "naa.32222220", 12); 1717 num += 12; 1718 snprintf(b, sizeof(b), "%08X", target_dev_id); 1719 memcpy(arr + num, b, 8); 1720 num += 8; 1721 memset(arr + num, 0, 4); 1722 num += 4; 1723 return num; 1724 } 1725 1726 static unsigned char vpd84_data[] = { 1727 /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0, 1728 0x22,0x22,0x22,0x0,0xbb,0x1, 1729 0x22,0x22,0x22,0x0,0xbb,0x2, 1730 }; 1731 1732 /* Software interface identification VPD page */ 1733 static int inquiry_vpd_84(unsigned char *arr) 1734 { 1735 memcpy(arr, vpd84_data, sizeof(vpd84_data)); 1736 return sizeof(vpd84_data); 1737 } 1738 1739 /* Management network addresses VPD page */ 1740 static int inquiry_vpd_85(unsigned char *arr) 1741 { 1742 int num = 0; 1743 const char *na1 = "https://www.kernel.org/config"; 1744 const char *na2 = "http://www.kernel.org/log"; 1745 int plen, olen; 1746 1747 arr[num++] = 0x1; /* lu, storage config */ 1748 arr[num++] = 0x0; /* reserved */ 1749 arr[num++] = 0x0; 1750 olen = strlen(na1); 1751 plen = olen + 1; 1752 if (plen % 4) 1753 plen = ((plen / 4) + 1) * 4; 1754 arr[num++] = plen; /* length, null termianted, padded */ 1755 memcpy(arr + num, na1, olen); 1756 memset(arr + num + olen, 0, plen - olen); 1757 num += plen; 1758 1759 arr[num++] = 0x4; /* lu, logging */ 1760 arr[num++] = 0x0; /* reserved */ 1761 arr[num++] = 0x0; 1762 olen = strlen(na2); 1763 plen = olen + 1; 1764 if (plen % 4) 1765 plen = ((plen / 4) + 1) * 4; 1766 arr[num++] = plen; /* length, null terminated, padded */ 1767 memcpy(arr + num, na2, olen); 1768 memset(arr + num + olen, 0, plen - olen); 1769 num += plen; 1770 1771 return num; 1772 } 1773 1774 /* SCSI ports VPD page */ 1775 static int inquiry_vpd_88(unsigned char *arr, int target_dev_id) 1776 { 1777 int num = 0; 1778 int port_a, port_b; 1779 1780 port_a = target_dev_id + 1; 1781 port_b = port_a + 1; 1782 arr[num++] = 0x0; /* reserved */ 1783 arr[num++] = 0x0; /* reserved */ 1784 arr[num++] = 0x0; 1785 arr[num++] = 0x1; /* relative port 1 (primary) */ 1786 memset(arr + num, 0, 6); 1787 num += 6; 1788 arr[num++] = 0x0; 1789 arr[num++] = 12; /* length tp descriptor */ 1790 /* naa-5 target port identifier (A) */ 1791 arr[num++] = 0x61; /* proto=sas, binary */ 1792 arr[num++] = 0x93; /* PIV=1, target port, NAA */ 1793 arr[num++] = 0x0; /* reserved */ 1794 arr[num++] = 0x8; /* length */ 1795 put_unaligned_be64(naa3_comp_a + port_a, arr + num); 1796 num += 8; 1797 arr[num++] = 0x0; /* reserved */ 1798 arr[num++] = 0x0; /* reserved */ 1799 arr[num++] = 0x0; 1800 arr[num++] = 0x2; /* relative port 2 (secondary) */ 1801 memset(arr + num, 0, 6); 1802 num += 6; 1803 arr[num++] = 0x0; 1804 arr[num++] = 12; /* length tp descriptor */ 1805 /* naa-5 target port identifier (B) */ 1806 arr[num++] = 0x61; /* proto=sas, binary */ 1807 arr[num++] = 0x93; /* PIV=1, target port, NAA */ 1808 arr[num++] = 0x0; /* reserved */ 1809 arr[num++] = 0x8; /* length */ 1810 put_unaligned_be64(naa3_comp_a + port_b, arr + num); 1811 num += 8; 1812 1813 return num; 1814 } 1815 1816 1817 static unsigned char vpd89_data[] = { 1818 /* from 4th byte */ 0,0,0,0, 1819 'l','i','n','u','x',' ',' ',' ', 1820 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ', 1821 '1','2','3','4', 1822 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0, 1823 0xec,0,0,0, 1824 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0, 1825 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20, 1826 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33, 1827 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31, 1828 0x53,0x41, 1829 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 1830 0x20,0x20, 1831 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 1832 0x10,0x80, 1833 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0, 1834 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0, 1835 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0, 1836 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0, 1837 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40, 1838 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0, 1839 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0, 1840 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1841 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1842 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1843 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42, 1844 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8, 1845 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe, 1846 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0, 1847 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1848 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1849 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1850 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1851 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1852 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1853 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1854 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1855 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1856 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1857 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1858 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51, 1859 }; 1860 1861 /* ATA Information VPD page */ 1862 static int inquiry_vpd_89(unsigned char *arr) 1863 { 1864 memcpy(arr, vpd89_data, sizeof(vpd89_data)); 1865 return sizeof(vpd89_data); 1866 } 1867 1868 1869 static unsigned char vpdb0_data[] = { 1870 /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64, 1871 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1872 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1873 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1874 }; 1875 1876 /* Block limits VPD page (SBC-3) */ 1877 static int inquiry_vpd_b0(unsigned char *arr) 1878 { 1879 unsigned int gran; 1880 1881 memcpy(arr, vpdb0_data, sizeof(vpdb0_data)); 1882 1883 /* Optimal transfer length granularity */ 1884 if (sdebug_opt_xferlen_exp != 0 && 1885 sdebug_physblk_exp < sdebug_opt_xferlen_exp) 1886 gran = 1 << sdebug_opt_xferlen_exp; 1887 else 1888 gran = 1 << sdebug_physblk_exp; 1889 put_unaligned_be16(gran, arr + 2); 1890 1891 /* Maximum Transfer Length */ 1892 if (sdebug_store_sectors > 0x400) 1893 put_unaligned_be32(sdebug_store_sectors, arr + 4); 1894 1895 /* Optimal Transfer Length */ 1896 put_unaligned_be32(sdebug_opt_blks, &arr[8]); 1897 1898 if (sdebug_lbpu) { 1899 /* Maximum Unmap LBA Count */ 1900 put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]); 1901 1902 /* Maximum Unmap Block Descriptor Count */ 1903 put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]); 1904 } 1905 1906 /* Unmap Granularity Alignment */ 1907 if (sdebug_unmap_alignment) { 1908 put_unaligned_be32(sdebug_unmap_alignment, &arr[28]); 1909 arr[28] |= 0x80; /* UGAVALID */ 1910 } 1911 1912 /* Optimal Unmap Granularity */ 1913 put_unaligned_be32(sdebug_unmap_granularity, &arr[24]); 1914 1915 /* Maximum WRITE SAME Length */ 1916 put_unaligned_be64(sdebug_write_same_length, &arr[32]); 1917 1918 if (sdebug_atomic_wr) { 1919 put_unaligned_be32(sdebug_atomic_wr_max_length, &arr[40]); 1920 put_unaligned_be32(sdebug_atomic_wr_align, &arr[44]); 1921 put_unaligned_be32(sdebug_atomic_wr_gran, &arr[48]); 1922 put_unaligned_be32(sdebug_atomic_wr_max_length_bndry, &arr[52]); 1923 put_unaligned_be32(sdebug_atomic_wr_max_bndry, &arr[56]); 1924 } 1925 1926 return 0x3c; /* Mandatory page length for Logical Block Provisioning */ 1927 } 1928 1929 /* Block device characteristics VPD page (SBC-3) */ 1930 static int inquiry_vpd_b1(struct sdebug_dev_info *devip, unsigned char *arr) 1931 { 1932 memset(arr, 0, 0x3c); 1933 arr[0] = 0; 1934 arr[1] = 1; /* non rotating medium (e.g. solid state) */ 1935 arr[2] = 0; 1936 arr[3] = 5; /* less than 1.8" */ 1937 1938 return 0x3c; 1939 } 1940 1941 /* Logical block provisioning VPD page (SBC-4) */ 1942 static int inquiry_vpd_b2(unsigned char *arr) 1943 { 1944 memset(arr, 0, 0x4); 1945 arr[0] = 0; /* threshold exponent */ 1946 if (sdebug_lbpu) 1947 arr[1] = 1 << 7; 1948 if (sdebug_lbpws) 1949 arr[1] |= 1 << 6; 1950 if (sdebug_lbpws10) 1951 arr[1] |= 1 << 5; 1952 if (sdebug_lbprz && scsi_debug_lbp()) 1953 arr[1] |= (sdebug_lbprz & 0x7) << 2; /* sbc4r07 and later */ 1954 /* anc_sup=0; dp=0 (no provisioning group descriptor) */ 1955 /* minimum_percentage=0; provisioning_type=0 (unknown) */ 1956 /* threshold_percentage=0 */ 1957 return 0x4; 1958 } 1959 1960 /* Zoned block device characteristics VPD page (ZBC mandatory) */ 1961 static int inquiry_vpd_b6(struct sdebug_dev_info *devip, unsigned char *arr) 1962 { 1963 memset(arr, 0, 0x3c); 1964 arr[0] = 0x1; /* set URSWRZ (unrestricted read in seq. wr req zone) */ 1965 /* 1966 * Set Optimal number of open sequential write preferred zones and 1967 * Optimal number of non-sequentially written sequential write 1968 * preferred zones fields to 'not reported' (0xffffffff). Leave other 1969 * fields set to zero, apart from Max. number of open swrz_s field. 1970 */ 1971 put_unaligned_be32(0xffffffff, &arr[4]); 1972 put_unaligned_be32(0xffffffff, &arr[8]); 1973 if (sdeb_zbc_model == BLK_ZONED_HM && devip->max_open) 1974 put_unaligned_be32(devip->max_open, &arr[12]); 1975 else 1976 put_unaligned_be32(0xffffffff, &arr[12]); 1977 if (devip->zcap < devip->zsize) { 1978 arr[19] = ZBC_CONSTANT_ZONE_START_OFFSET; 1979 put_unaligned_be64(devip->zsize, &arr[20]); 1980 } else { 1981 arr[19] = 0; 1982 } 1983 return 0x3c; 1984 } 1985 1986 #define SDEBUG_BLE_LEN_AFTER_B4 28 /* thus vpage 32 bytes long */ 1987 1988 enum { MAXIMUM_NUMBER_OF_STREAMS = 6, PERMANENT_STREAM_COUNT = 5 }; 1989 1990 /* Block limits extension VPD page (SBC-4) */ 1991 static int inquiry_vpd_b7(unsigned char *arrb4) 1992 { 1993 memset(arrb4, 0, SDEBUG_BLE_LEN_AFTER_B4); 1994 arrb4[1] = 1; /* Reduced stream control support (RSCS) */ 1995 put_unaligned_be16(MAXIMUM_NUMBER_OF_STREAMS, &arrb4[2]); 1996 return SDEBUG_BLE_LEN_AFTER_B4; 1997 } 1998 1999 #define SDEBUG_LONG_INQ_SZ 96 2000 #define SDEBUG_MAX_INQ_ARR_SZ 584 2001 2002 static int resp_inquiry(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) 2003 { 2004 unsigned char pq_pdt; 2005 unsigned char *arr; 2006 unsigned char *cmd = scp->cmnd; 2007 u32 alloc_len, n; 2008 int ret; 2009 bool have_wlun, is_disk, is_zbc, is_disk_zbc, is_tape; 2010 2011 alloc_len = get_unaligned_be16(cmd + 3); 2012 arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC); 2013 if (! arr) 2014 return DID_REQUEUE << 16; 2015 if (scp->device->type >= 32) { 2016 is_disk = (sdebug_ptype == TYPE_DISK); 2017 is_tape = (sdebug_ptype == TYPE_TAPE); 2018 } else { 2019 is_disk = (scp->device->type == TYPE_DISK); 2020 is_tape = (scp->device->type == TYPE_TAPE); 2021 } 2022 is_zbc = devip->zoned; 2023 is_disk_zbc = (is_disk || is_zbc); 2024 have_wlun = scsi_is_wlun(scp->device->lun); 2025 if (have_wlun) 2026 pq_pdt = TYPE_WLUN; /* present, wlun */ 2027 else if (sdebug_no_lun_0 && (devip->lun == SDEBUG_LUN_0_VAL)) 2028 pq_pdt = 0x7f; /* not present, PQ=3, PDT=0x1f */ 2029 else 2030 pq_pdt = ((scp->device->type >= 32 ? 2031 sdebug_ptype : scp->device->type) & 0x1f); 2032 arr[0] = pq_pdt; 2033 if (0x2 & cmd[1]) { /* CMDDT bit set */ 2034 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1); 2035 kfree(arr); 2036 return check_condition_result; 2037 } else if (0x1 & cmd[1]) { /* EVPD bit set */ 2038 int lu_id_num, port_group_id, target_dev_id; 2039 u32 len; 2040 char lu_id_str[6]; 2041 int host_no = devip->sdbg_host->shost->host_no; 2042 2043 arr[1] = cmd[2]; 2044 port_group_id = (((host_no + 1) & 0x7f) << 8) + 2045 (devip->channel & 0x7f); 2046 if (sdebug_vpd_use_hostno == 0) 2047 host_no = 0; 2048 lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) + 2049 (devip->target * 1000) + devip->lun); 2050 target_dev_id = ((host_no + 1) * 2000) + 2051 (devip->target * 1000) - 3; 2052 len = scnprintf(lu_id_str, 6, "%d", lu_id_num); 2053 if (0 == cmd[2]) { /* supported vital product data pages */ 2054 n = 4; 2055 arr[n++] = 0x0; /* this page */ 2056 arr[n++] = 0x80; /* unit serial number */ 2057 arr[n++] = 0x83; /* device identification */ 2058 arr[n++] = 0x84; /* software interface ident. */ 2059 arr[n++] = 0x85; /* management network addresses */ 2060 arr[n++] = 0x86; /* extended inquiry */ 2061 arr[n++] = 0x87; /* mode page policy */ 2062 arr[n++] = 0x88; /* SCSI ports */ 2063 if (is_disk_zbc) { /* SBC or ZBC */ 2064 arr[n++] = 0x89; /* ATA information */ 2065 arr[n++] = 0xb0; /* Block limits */ 2066 arr[n++] = 0xb1; /* Block characteristics */ 2067 if (is_disk) 2068 arr[n++] = 0xb2; /* LB Provisioning */ 2069 if (is_zbc) 2070 arr[n++] = 0xb6; /* ZB dev. char. */ 2071 arr[n++] = 0xb7; /* Block limits extension */ 2072 } 2073 arr[3] = n - 4; /* number of supported VPD pages */ 2074 } else if (0x80 == cmd[2]) { /* unit serial number */ 2075 arr[3] = len; 2076 memcpy(&arr[4], lu_id_str, len); 2077 } else if (0x83 == cmd[2]) { /* device identification */ 2078 arr[3] = inquiry_vpd_83(&arr[4], port_group_id, 2079 target_dev_id, lu_id_num, 2080 lu_id_str, len, 2081 &devip->lu_name); 2082 } else if (0x84 == cmd[2]) { /* Software interface ident. */ 2083 arr[3] = inquiry_vpd_84(&arr[4]); 2084 } else if (0x85 == cmd[2]) { /* Management network addresses */ 2085 arr[3] = inquiry_vpd_85(&arr[4]); 2086 } else if (0x86 == cmd[2]) { /* extended inquiry */ 2087 arr[3] = 0x3c; /* number of following entries */ 2088 if (sdebug_dif == T10_PI_TYPE3_PROTECTION) 2089 arr[4] = 0x4; /* SPT: GRD_CHK:1 */ 2090 else if (have_dif_prot) 2091 arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */ 2092 else 2093 arr[4] = 0x0; /* no protection stuff */ 2094 /* 2095 * GROUP_SUP=1; HEADSUP=1 (HEAD OF QUEUE); ORDSUP=1 2096 * (ORDERED queuing); SIMPSUP=1 (SIMPLE queuing). 2097 */ 2098 arr[5] = 0x17; 2099 } else if (0x87 == cmd[2]) { /* mode page policy */ 2100 arr[3] = 0x8; /* number of following entries */ 2101 arr[4] = 0x2; /* disconnect-reconnect mp */ 2102 arr[6] = 0x80; /* mlus, shared */ 2103 arr[8] = 0x18; /* protocol specific lu */ 2104 arr[10] = 0x82; /* mlus, per initiator port */ 2105 } else if (0x88 == cmd[2]) { /* SCSI Ports */ 2106 arr[3] = inquiry_vpd_88(&arr[4], target_dev_id); 2107 } else if (is_disk_zbc && 0x89 == cmd[2]) { /* ATA info */ 2108 n = inquiry_vpd_89(&arr[4]); 2109 put_unaligned_be16(n, arr + 2); 2110 } else if (is_disk_zbc && 0xb0 == cmd[2]) { /* Block limits */ 2111 arr[3] = inquiry_vpd_b0(&arr[4]); 2112 } else if (is_disk_zbc && 0xb1 == cmd[2]) { /* Block char. */ 2113 arr[3] = inquiry_vpd_b1(devip, &arr[4]); 2114 } else if (is_disk && 0xb2 == cmd[2]) { /* LB Prov. */ 2115 arr[3] = inquiry_vpd_b2(&arr[4]); 2116 } else if (is_zbc && cmd[2] == 0xb6) { /* ZB dev. charact. */ 2117 arr[3] = inquiry_vpd_b6(devip, &arr[4]); 2118 } else if (cmd[2] == 0xb7) { /* block limits extension page */ 2119 arr[3] = inquiry_vpd_b7(&arr[4]); 2120 } else { 2121 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); 2122 kfree(arr); 2123 return check_condition_result; 2124 } 2125 len = min_t(u32, get_unaligned_be16(arr + 2) + 4, alloc_len); 2126 ret = fill_from_dev_buffer(scp, arr, 2127 min_t(u32, len, SDEBUG_MAX_INQ_ARR_SZ)); 2128 kfree(arr); 2129 return ret; 2130 } 2131 /* drops through here for a standard inquiry */ 2132 arr[1] = sdebug_removable ? 0x80 : 0; /* Removable disk */ 2133 arr[2] = sdebug_scsi_level; 2134 arr[3] = 2; /* response_data_format==2 */ 2135 arr[4] = SDEBUG_LONG_INQ_SZ - 5; 2136 arr[5] = (int)have_dif_prot; /* PROTECT bit */ 2137 if (sdebug_vpd_use_hostno == 0) 2138 arr[5] |= 0x10; /* claim: implicit TPGS */ 2139 arr[6] = 0x10; /* claim: MultiP */ 2140 /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */ 2141 arr[7] = 0xa; /* claim: LINKED + CMDQUE */ 2142 memcpy(&arr[8], sdebug_inq_vendor_id, 8); 2143 memcpy(&arr[16], sdebug_inq_product_id, 16); 2144 memcpy(&arr[32], sdebug_inq_product_rev, 4); 2145 /* Use Vendor Specific area to place driver date in ASCII hex */ 2146 memcpy(&arr[36], sdebug_version_date, 8); 2147 /* version descriptors (2 bytes each) follow */ 2148 put_unaligned_be16(0xc0, arr + 58); /* SAM-6 no version claimed */ 2149 put_unaligned_be16(0x5c0, arr + 60); /* SPC-5 no version claimed */ 2150 n = 62; 2151 if (is_disk) { /* SBC-4 no version claimed */ 2152 put_unaligned_be16(0x600, arr + n); 2153 n += 2; 2154 } else if (is_tape) { /* SSC-4 rev 3 */ 2155 put_unaligned_be16(0x525, arr + n); 2156 n += 2; 2157 } else if (is_zbc) { /* ZBC BSR INCITS 536 revision 05 */ 2158 put_unaligned_be16(0x624, arr + n); 2159 n += 2; 2160 } 2161 put_unaligned_be16(0x2100, arr + n); /* SPL-4 no version claimed */ 2162 ret = fill_from_dev_buffer(scp, arr, 2163 min_t(u32, alloc_len, SDEBUG_LONG_INQ_SZ)); 2164 kfree(arr); 2165 return ret; 2166 } 2167 2168 /* See resp_iec_m_pg() for how this data is manipulated */ 2169 static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, 2170 0, 0, 0x0, 0x0}; 2171 2172 static int resp_requests(struct scsi_cmnd *scp, 2173 struct sdebug_dev_info *devip) 2174 { 2175 unsigned char *cmd = scp->cmnd; 2176 unsigned char arr[SCSI_SENSE_BUFFERSIZE]; /* assume >= 18 bytes */ 2177 bool dsense = !!(cmd[1] & 1); 2178 u32 alloc_len = cmd[4]; 2179 u32 len = 18; 2180 int stopped_state = atomic_read(&devip->stopped); 2181 2182 memset(arr, 0, sizeof(arr)); 2183 if (stopped_state > 0) { /* some "pollable" data [spc6r02: 5.12.2] */ 2184 if (dsense) { 2185 arr[0] = 0x72; 2186 arr[1] = NOT_READY; 2187 arr[2] = LOGICAL_UNIT_NOT_READY; 2188 arr[3] = (stopped_state == 2) ? 0x1 : 0x2; 2189 len = 8; 2190 } else { 2191 arr[0] = 0x70; 2192 arr[2] = NOT_READY; /* NO_SENSE in sense_key */ 2193 arr[7] = 0xa; /* 18 byte sense buffer */ 2194 arr[12] = LOGICAL_UNIT_NOT_READY; 2195 arr[13] = (stopped_state == 2) ? 0x1 : 0x2; 2196 } 2197 } else if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) { 2198 /* Information exceptions control mode page: TEST=1, MRIE=6 */ 2199 if (dsense) { 2200 arr[0] = 0x72; 2201 arr[1] = 0x0; /* NO_SENSE in sense_key */ 2202 arr[2] = THRESHOLD_EXCEEDED; 2203 arr[3] = 0xff; /* Failure prediction(false) */ 2204 len = 8; 2205 } else { 2206 arr[0] = 0x70; 2207 arr[2] = 0x0; /* NO_SENSE in sense_key */ 2208 arr[7] = 0xa; /* 18 byte sense buffer */ 2209 arr[12] = THRESHOLD_EXCEEDED; 2210 arr[13] = 0xff; /* Failure prediction(false) */ 2211 } 2212 } else { /* nothing to report */ 2213 if (dsense) { 2214 len = 8; 2215 memset(arr, 0, len); 2216 arr[0] = 0x72; 2217 } else { 2218 memset(arr, 0, len); 2219 arr[0] = 0x70; 2220 arr[7] = 0xa; 2221 } 2222 } 2223 return fill_from_dev_buffer(scp, arr, min_t(u32, len, alloc_len)); 2224 } 2225 2226 static int resp_start_stop(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) 2227 { 2228 unsigned char *cmd = scp->cmnd; 2229 int power_cond, want_stop, stopped_state; 2230 bool changing; 2231 2232 power_cond = (cmd[4] & 0xf0) >> 4; 2233 if (power_cond) { 2234 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7); 2235 return check_condition_result; 2236 } 2237 want_stop = !(cmd[4] & 1); 2238 stopped_state = atomic_read(&devip->stopped); 2239 if (stopped_state == 2) { 2240 ktime_t now_ts = ktime_get_boottime(); 2241 2242 if (ktime_to_ns(now_ts) > ktime_to_ns(devip->create_ts)) { 2243 u64 diff_ns = ktime_to_ns(ktime_sub(now_ts, devip->create_ts)); 2244 2245 if (diff_ns >= ((u64)sdeb_tur_ms_to_ready * 1000000)) { 2246 /* tur_ms_to_ready timer extinguished */ 2247 atomic_set(&devip->stopped, 0); 2248 stopped_state = 0; 2249 } 2250 } 2251 if (stopped_state == 2) { 2252 if (want_stop) { 2253 stopped_state = 1; /* dummy up success */ 2254 } else { /* Disallow tur_ms_to_ready delay to be overridden */ 2255 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 0 /* START bit */); 2256 return check_condition_result; 2257 } 2258 } 2259 } 2260 changing = (stopped_state != want_stop); 2261 if (changing) 2262 atomic_xchg(&devip->stopped, want_stop); 2263 if (scp->device->type == TYPE_TAPE && !want_stop) { 2264 int i; 2265 2266 set_bit(SDEBUG_UA_NOT_READY_TO_READY, devip->uas_bm); /* not legal! */ 2267 for (i = 0; i < TAPE_MAX_PARTITIONS; i++) 2268 devip->tape_location[i] = 0; 2269 devip->tape_partition = 0; 2270 } 2271 if (!changing || (cmd[1] & 0x1)) /* state unchanged or IMMED bit set in cdb */ 2272 return SDEG_RES_IMMED_MASK; 2273 else 2274 return 0; 2275 } 2276 2277 static sector_t get_sdebug_capacity(void) 2278 { 2279 static const unsigned int gibibyte = 1073741824; 2280 2281 if (sdebug_virtual_gb > 0) 2282 return (sector_t)sdebug_virtual_gb * 2283 (gibibyte / sdebug_sector_size); 2284 else 2285 return sdebug_store_sectors; 2286 } 2287 2288 #define SDEBUG_READCAP_ARR_SZ 8 2289 static int resp_readcap(struct scsi_cmnd *scp, 2290 struct sdebug_dev_info *devip) 2291 { 2292 unsigned char arr[SDEBUG_READCAP_ARR_SZ]; 2293 unsigned int capac; 2294 2295 /* following just in case virtual_gb changed */ 2296 sdebug_capacity = get_sdebug_capacity(); 2297 memset(arr, 0, SDEBUG_READCAP_ARR_SZ); 2298 if (sdebug_capacity < 0xffffffff) { 2299 capac = (unsigned int)sdebug_capacity - 1; 2300 put_unaligned_be32(capac, arr + 0); 2301 } else 2302 put_unaligned_be32(0xffffffff, arr + 0); 2303 put_unaligned_be16(sdebug_sector_size, arr + 6); 2304 return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ); 2305 } 2306 2307 #define SDEBUG_READCAP16_ARR_SZ 32 2308 static int resp_readcap16(struct scsi_cmnd *scp, 2309 struct sdebug_dev_info *devip) 2310 { 2311 unsigned char *cmd = scp->cmnd; 2312 unsigned char arr[SDEBUG_READCAP16_ARR_SZ]; 2313 u32 alloc_len; 2314 2315 alloc_len = get_unaligned_be32(cmd + 10); 2316 /* following just in case virtual_gb changed */ 2317 sdebug_capacity = get_sdebug_capacity(); 2318 memset(arr, 0, SDEBUG_READCAP16_ARR_SZ); 2319 put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0); 2320 put_unaligned_be32(sdebug_sector_size, arr + 8); 2321 arr[13] = sdebug_physblk_exp & 0xf; 2322 arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f; 2323 2324 if (scsi_debug_lbp()) { 2325 arr[14] |= 0x80; /* LBPME */ 2326 /* from sbc4r07, this LBPRZ field is 1 bit, but the LBPRZ in 2327 * the LB Provisioning VPD page is 3 bits. Note that lbprz=2 2328 * in the wider field maps to 0 in this field. 2329 */ 2330 if (sdebug_lbprz & 1) /* precisely what the draft requires */ 2331 arr[14] |= 0x40; 2332 } 2333 2334 /* 2335 * Since the scsi_debug READ CAPACITY implementation always reports the 2336 * total disk capacity, set RC BASIS = 1 for host-managed ZBC devices. 2337 */ 2338 if (devip->zoned) 2339 arr[12] |= 1 << 4; 2340 2341 arr[15] = sdebug_lowest_aligned & 0xff; 2342 2343 if (have_dif_prot) { 2344 arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */ 2345 arr[12] |= 1; /* PROT_EN */ 2346 } 2347 2348 return fill_from_dev_buffer(scp, arr, 2349 min_t(u32, alloc_len, SDEBUG_READCAP16_ARR_SZ)); 2350 } 2351 2352 #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412 2353 2354 static int resp_report_tgtpgs(struct scsi_cmnd *scp, 2355 struct sdebug_dev_info *devip) 2356 { 2357 unsigned char *cmd = scp->cmnd; 2358 unsigned char *arr; 2359 int host_no = devip->sdbg_host->shost->host_no; 2360 int port_group_a, port_group_b, port_a, port_b; 2361 u32 alen, n, rlen; 2362 int ret; 2363 2364 alen = get_unaligned_be32(cmd + 6); 2365 arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC); 2366 if (! arr) 2367 return DID_REQUEUE << 16; 2368 /* 2369 * EVPD page 0x88 states we have two ports, one 2370 * real and a fake port with no device connected. 2371 * So we create two port groups with one port each 2372 * and set the group with port B to unavailable. 2373 */ 2374 port_a = 0x1; /* relative port A */ 2375 port_b = 0x2; /* relative port B */ 2376 port_group_a = (((host_no + 1) & 0x7f) << 8) + 2377 (devip->channel & 0x7f); 2378 port_group_b = (((host_no + 1) & 0x7f) << 8) + 2379 (devip->channel & 0x7f) + 0x80; 2380 2381 /* 2382 * The asymmetric access state is cycled according to the host_id. 2383 */ 2384 n = 4; 2385 if (sdebug_vpd_use_hostno == 0) { 2386 arr[n++] = host_no % 3; /* Asymm access state */ 2387 arr[n++] = 0x0F; /* claim: all states are supported */ 2388 } else { 2389 arr[n++] = 0x0; /* Active/Optimized path */ 2390 arr[n++] = 0x01; /* only support active/optimized paths */ 2391 } 2392 put_unaligned_be16(port_group_a, arr + n); 2393 n += 2; 2394 arr[n++] = 0; /* Reserved */ 2395 arr[n++] = 0; /* Status code */ 2396 arr[n++] = 0; /* Vendor unique */ 2397 arr[n++] = 0x1; /* One port per group */ 2398 arr[n++] = 0; /* Reserved */ 2399 arr[n++] = 0; /* Reserved */ 2400 put_unaligned_be16(port_a, arr + n); 2401 n += 2; 2402 arr[n++] = 3; /* Port unavailable */ 2403 arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */ 2404 put_unaligned_be16(port_group_b, arr + n); 2405 n += 2; 2406 arr[n++] = 0; /* Reserved */ 2407 arr[n++] = 0; /* Status code */ 2408 arr[n++] = 0; /* Vendor unique */ 2409 arr[n++] = 0x1; /* One port per group */ 2410 arr[n++] = 0; /* Reserved */ 2411 arr[n++] = 0; /* Reserved */ 2412 put_unaligned_be16(port_b, arr + n); 2413 n += 2; 2414 2415 rlen = n - 4; 2416 put_unaligned_be32(rlen, arr + 0); 2417 2418 /* 2419 * Return the smallest value of either 2420 * - The allocated length 2421 * - The constructed command length 2422 * - The maximum array size 2423 */ 2424 rlen = min(alen, n); 2425 ret = fill_from_dev_buffer(scp, arr, 2426 min_t(u32, rlen, SDEBUG_MAX_TGTPGS_ARR_SZ)); 2427 kfree(arr); 2428 return ret; 2429 } 2430 2431 static int resp_rsup_opcodes(struct scsi_cmnd *scp, 2432 struct sdebug_dev_info *devip) 2433 { 2434 bool rctd; 2435 u8 reporting_opts, req_opcode, sdeb_i, supp; 2436 u16 req_sa, u; 2437 u32 alloc_len, a_len; 2438 int k, offset, len, errsts, bump, na; 2439 const struct opcode_info_t *oip; 2440 const struct opcode_info_t *r_oip; 2441 u8 *arr; 2442 u8 *cmd = scp->cmnd; 2443 u32 devsel = sdebug_get_devsel(scp->device); 2444 2445 rctd = !!(cmd[2] & 0x80); 2446 reporting_opts = cmd[2] & 0x7; 2447 req_opcode = cmd[3]; 2448 req_sa = get_unaligned_be16(cmd + 4); 2449 alloc_len = get_unaligned_be32(cmd + 6); 2450 if (alloc_len < 4 || alloc_len > 0xffff) { 2451 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); 2452 return check_condition_result; 2453 } 2454 if (alloc_len > 8192) 2455 a_len = 8192; 2456 else 2457 a_len = alloc_len; 2458 arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC); 2459 if (NULL == arr) { 2460 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, 2461 INSUFF_RES_ASCQ); 2462 return check_condition_result; 2463 } 2464 switch (reporting_opts) { 2465 case 0: /* all commands */ 2466 bump = rctd ? 20 : 8; 2467 for (offset = 4, oip = opcode_info_arr; 2468 oip->num_attached != 0xff && offset < a_len; ++oip) { 2469 if (F_INV_OP & oip->flags) 2470 continue; 2471 if ((devsel & oip->devsel) != 0) { 2472 arr[offset] = oip->opcode; 2473 put_unaligned_be16(oip->sa, arr + offset + 2); 2474 if (rctd) 2475 arr[offset + 5] |= 0x2; 2476 if (FF_SA & oip->flags) 2477 arr[offset + 5] |= 0x1; 2478 put_unaligned_be16(oip->len_mask[0], arr + offset + 6); 2479 if (rctd) 2480 put_unaligned_be16(0xa, arr + offset + 8); 2481 offset += bump; 2482 } 2483 na = oip->num_attached; 2484 r_oip = oip; 2485 for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) { 2486 if (F_INV_OP & oip->flags) 2487 continue; 2488 if ((devsel & oip->devsel) == 0) 2489 continue; 2490 arr[offset] = oip->opcode; 2491 put_unaligned_be16(oip->sa, arr + offset + 2); 2492 if (rctd) 2493 arr[offset + 5] |= 0x2; 2494 if (FF_SA & oip->flags) 2495 arr[offset + 5] |= 0x1; 2496 put_unaligned_be16(oip->len_mask[0], 2497 arr + offset + 6); 2498 if (rctd) 2499 put_unaligned_be16(0xa, 2500 arr + offset + 8); 2501 offset += bump; 2502 } 2503 oip = r_oip; 2504 } 2505 put_unaligned_be32(offset - 4, arr); 2506 break; 2507 case 1: /* one command: opcode only */ 2508 case 2: /* one command: opcode plus service action */ 2509 case 3: /* one command: if sa==0 then opcode only else opcode+sa */ 2510 sdeb_i = opcode_ind_arr[req_opcode]; 2511 oip = &opcode_info_arr[sdeb_i]; 2512 if (F_INV_OP & oip->flags) { 2513 supp = 1; 2514 offset = 4; 2515 } else { 2516 if (1 == reporting_opts) { 2517 if (FF_SA & oip->flags) { 2518 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2519 2, 2); 2520 kfree(arr); 2521 return check_condition_result; 2522 } 2523 req_sa = 0; 2524 } else if (2 == reporting_opts && 2525 0 == (FF_SA & oip->flags)) { 2526 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1); 2527 kfree(arr); /* point at requested sa */ 2528 return check_condition_result; 2529 } 2530 if (0 == (FF_SA & oip->flags) && 2531 (devsel & oip->devsel) != 0 && 2532 req_opcode == oip->opcode) 2533 supp = 3; 2534 else if (0 == (FF_SA & oip->flags)) { 2535 na = oip->num_attached; 2536 for (k = 0, oip = oip->arrp; k < na; 2537 ++k, ++oip) { 2538 if (req_opcode == oip->opcode && 2539 (devsel & oip->devsel) != 0) 2540 break; 2541 } 2542 supp = (k >= na) ? 1 : 3; 2543 } else if (req_sa != oip->sa) { 2544 na = oip->num_attached; 2545 for (k = 0, oip = oip->arrp; k < na; 2546 ++k, ++oip) { 2547 if (req_sa == oip->sa && 2548 (devsel & oip->devsel) != 0) 2549 break; 2550 } 2551 supp = (k >= na) ? 1 : 3; 2552 } else 2553 supp = 3; 2554 if (3 == supp) { 2555 u = oip->len_mask[0]; 2556 put_unaligned_be16(u, arr + 2); 2557 arr[4] = oip->opcode; 2558 for (k = 1; k < u; ++k) 2559 arr[4 + k] = (k < 16) ? 2560 oip->len_mask[k] : 0xff; 2561 offset = 4 + u; 2562 } else 2563 offset = 4; 2564 } 2565 arr[1] = (rctd ? 0x80 : 0) | supp; 2566 if (rctd) { 2567 put_unaligned_be16(0xa, arr + offset); 2568 offset += 12; 2569 } 2570 break; 2571 default: 2572 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2); 2573 kfree(arr); 2574 return check_condition_result; 2575 } 2576 offset = (offset < a_len) ? offset : a_len; 2577 len = (offset < alloc_len) ? offset : alloc_len; 2578 errsts = fill_from_dev_buffer(scp, arr, len); 2579 kfree(arr); 2580 return errsts; 2581 } 2582 2583 static int resp_rsup_tmfs(struct scsi_cmnd *scp, 2584 struct sdebug_dev_info *devip) 2585 { 2586 bool repd; 2587 u32 alloc_len, len; 2588 u8 arr[16]; 2589 u8 *cmd = scp->cmnd; 2590 2591 memset(arr, 0, sizeof(arr)); 2592 repd = !!(cmd[2] & 0x80); 2593 alloc_len = get_unaligned_be32(cmd + 6); 2594 if (alloc_len < 4) { 2595 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); 2596 return check_condition_result; 2597 } 2598 arr[0] = 0xc8; /* ATS | ATSS | LURS */ 2599 arr[1] = 0x1; /* ITNRS */ 2600 if (repd) { 2601 arr[3] = 0xc; 2602 len = 16; 2603 } else 2604 len = 4; 2605 2606 len = (len < alloc_len) ? len : alloc_len; 2607 return fill_from_dev_buffer(scp, arr, len); 2608 } 2609 2610 /* <<Following mode page info copied from ST318451LW>> */ 2611 2612 static int resp_err_recov_pg(unsigned char *p, int pcontrol, int target) 2613 { /* Read-Write Error Recovery page for mode_sense */ 2614 static const unsigned char err_recov_pg[] = { 2615 0x1, 0xa, 0xc0, 11, 240, 0, 0, 0, 2616 5, 0, 0xff, 0xff 2617 }; 2618 2619 memcpy(p, err_recov_pg, sizeof(err_recov_pg)); 2620 if (1 == pcontrol) 2621 memset(p + 2, 0, sizeof(err_recov_pg) - 2); 2622 return sizeof(err_recov_pg); 2623 } 2624 2625 static int resp_disconnect_pg(unsigned char *p, int pcontrol, int target) 2626 { /* Disconnect-Reconnect page for mode_sense */ 2627 static const unsigned char disconnect_pg[] = { 2628 0x2, 0xe, 128, 128, 0, 10, 0, 0, 2629 0, 0, 0, 0, 0, 0, 0, 0 2630 }; 2631 2632 memcpy(p, disconnect_pg, sizeof(disconnect_pg)); 2633 if (1 == pcontrol) 2634 memset(p + 2, 0, sizeof(disconnect_pg) - 2); 2635 return sizeof(disconnect_pg); 2636 } 2637 2638 static int resp_format_pg(unsigned char *p, int pcontrol, int target) 2639 { /* Format device page for mode_sense */ 2640 static const unsigned char format_pg[] = { 2641 0x3, 0x16, 0, 0, 0, 0, 0, 0, 2642 0, 0, 0, 0, 0, 0, 0, 0, 2643 0, 0, 0, 0, 0x40, 0, 0, 0 2644 }; 2645 2646 memcpy(p, format_pg, sizeof(format_pg)); 2647 put_unaligned_be16(sdebug_sectors_per, p + 10); 2648 put_unaligned_be16(sdebug_sector_size, p + 12); 2649 if (sdebug_removable) 2650 p[20] |= 0x20; /* should agree with INQUIRY */ 2651 if (1 == pcontrol) 2652 memset(p + 2, 0, sizeof(format_pg) - 2); 2653 return sizeof(format_pg); 2654 } 2655 2656 static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0, 2657 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 2658 0, 0, 0, 0}; 2659 2660 static int resp_caching_pg(unsigned char *p, int pcontrol, int target) 2661 { /* Caching page for mode_sense */ 2662 static const unsigned char ch_caching_pg[] = { 2663 /* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0, 2664 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 2665 }; 2666 static const unsigned char d_caching_pg[] = { 2667 0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0, 2668 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0 2669 }; 2670 2671 if (SDEBUG_OPT_N_WCE & sdebug_opts) 2672 caching_pg[2] &= ~0x4; /* set WCE=0 (default WCE=1) */ 2673 memcpy(p, caching_pg, sizeof(caching_pg)); 2674 if (1 == pcontrol) 2675 memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg)); 2676 else if (2 == pcontrol) 2677 memcpy(p, d_caching_pg, sizeof(d_caching_pg)); 2678 return sizeof(caching_pg); 2679 } 2680 2681 static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0, 2682 0, 0, 0x2, 0x4b}; 2683 2684 static int resp_ctrl_m_pg(unsigned char *p, int pcontrol, int target) 2685 { /* Control mode page for mode_sense */ 2686 unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0, 2687 0, 0, 0, 0}; 2688 static const unsigned char d_ctrl_m_pg[] = { 2689 0xa, 10, 2, 0, 0, 0, 0, 0, 2690 0, 0, 0x2, 0x4b 2691 }; 2692 2693 if (sdebug_dsense) 2694 ctrl_m_pg[2] |= 0x4; 2695 else 2696 ctrl_m_pg[2] &= ~0x4; 2697 2698 if (sdebug_ato) 2699 ctrl_m_pg[5] |= 0x80; /* ATO=1 */ 2700 2701 memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg)); 2702 if (1 == pcontrol) 2703 memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg)); 2704 else if (2 == pcontrol) 2705 memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg)); 2706 return sizeof(ctrl_m_pg); 2707 } 2708 2709 /* IO Advice Hints Grouping mode page */ 2710 static int resp_grouping_m_pg(unsigned char *p, int pcontrol, int target) 2711 { 2712 /* IO Advice Hints Grouping mode page */ 2713 struct grouping_m_pg { 2714 u8 page_code; /* OR 0x40 when subpage_code > 0 */ 2715 u8 subpage_code; 2716 __be16 page_length; 2717 u8 reserved[12]; 2718 struct scsi_io_group_descriptor descr[MAXIMUM_NUMBER_OF_STREAMS]; 2719 }; 2720 static const struct grouping_m_pg gr_m_pg = { 2721 .page_code = 0xa | 0x40, 2722 .subpage_code = 5, 2723 .page_length = cpu_to_be16(sizeof(gr_m_pg) - 4), 2724 .descr = { 2725 { .st_enble = 1 }, 2726 { .st_enble = 1 }, 2727 { .st_enble = 1 }, 2728 { .st_enble = 1 }, 2729 { .st_enble = 1 }, 2730 { .st_enble = 0 }, 2731 } 2732 }; 2733 2734 BUILD_BUG_ON(sizeof(struct grouping_m_pg) != 2735 16 + MAXIMUM_NUMBER_OF_STREAMS * 16); 2736 memcpy(p, &gr_m_pg, sizeof(gr_m_pg)); 2737 if (1 == pcontrol) { 2738 /* There are no changeable values so clear from byte 4 on. */ 2739 memset(p + 4, 0, sizeof(gr_m_pg) - 4); 2740 } 2741 return sizeof(gr_m_pg); 2742 } 2743 2744 static int resp_iec_m_pg(unsigned char *p, int pcontrol, int target) 2745 { /* Informational Exceptions control mode page for mode_sense */ 2746 static const unsigned char ch_iec_m_pg[] = { 2747 /* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0, 2748 0, 0, 0x0, 0x0 2749 }; 2750 static const unsigned char d_iec_m_pg[] = { 2751 0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, 2752 0, 0, 0x0, 0x0 2753 }; 2754 2755 memcpy(p, iec_m_pg, sizeof(iec_m_pg)); 2756 if (1 == pcontrol) 2757 memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg)); 2758 else if (2 == pcontrol) 2759 memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg)); 2760 return sizeof(iec_m_pg); 2761 } 2762 2763 static int resp_sas_sf_m_pg(unsigned char *p, int pcontrol, int target) 2764 { /* SAS SSP mode page - short format for mode_sense */ 2765 static const unsigned char sas_sf_m_pg[] = { 2766 0x19, 0x6, 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0 2767 }; 2768 2769 memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg)); 2770 if (1 == pcontrol) 2771 memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2); 2772 return sizeof(sas_sf_m_pg); 2773 } 2774 2775 2776 static int resp_sas_pcd_m_spg(unsigned char *p, int pcontrol, int target, 2777 int target_dev_id) 2778 { /* SAS phy control and discover mode page for mode_sense */ 2779 unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2, 2780 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0, 2781 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ 2782 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ 2783 0x2, 0, 0, 0, 0, 0, 0, 0, 2784 0x88, 0x99, 0, 0, 0, 0, 0, 0, 2785 0, 0, 0, 0, 0, 0, 0, 0, 2786 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0, 2787 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ 2788 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ 2789 0x3, 0, 0, 0, 0, 0, 0, 0, 2790 0x88, 0x99, 0, 0, 0, 0, 0, 0, 2791 0, 0, 0, 0, 0, 0, 0, 0, 2792 }; 2793 int port_a, port_b; 2794 2795 put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 16); 2796 put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 24); 2797 put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 64); 2798 put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 72); 2799 port_a = target_dev_id + 1; 2800 port_b = port_a + 1; 2801 memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg)); 2802 put_unaligned_be32(port_a, p + 20); 2803 put_unaligned_be32(port_b, p + 48 + 20); 2804 if (1 == pcontrol) 2805 memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4); 2806 return sizeof(sas_pcd_m_pg); 2807 } 2808 2809 static int resp_sas_sha_m_spg(unsigned char *p, int pcontrol) 2810 { /* SAS SSP shared protocol specific port mode subpage */ 2811 static const unsigned char sas_sha_m_pg[] = { 2812 0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0, 2813 0, 0, 0, 0, 0, 0, 0, 0, 2814 }; 2815 2816 memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg)); 2817 if (1 == pcontrol) 2818 memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4); 2819 return sizeof(sas_sha_m_pg); 2820 } 2821 2822 static unsigned char partition_pg[] = {0x11, 12, 1, 0, 0x24, 3, 9, 0, 2823 0xff, 0xff, 0x00, 0x00}; 2824 2825 static int resp_partition_m_pg(unsigned char *p, int pcontrol, int target) 2826 { /* Partition page for mode_sense (tape) */ 2827 memcpy(p, partition_pg, sizeof(partition_pg)); 2828 if (pcontrol == 1) 2829 memset(p + 2, 0, sizeof(partition_pg) - 2); 2830 return sizeof(partition_pg); 2831 } 2832 2833 static int process_medium_part_m_pg(struct sdebug_dev_info *devip, 2834 unsigned char *new, int pg_len) 2835 { 2836 int new_nbr, p0_size, p1_size; 2837 2838 if ((new[4] & 0x80) != 0) { /* FDP */ 2839 partition_pg[4] |= 0x80; 2840 devip->tape_pending_nbr_partitions = TAPE_MAX_PARTITIONS; 2841 devip->tape_pending_part_0_size = TAPE_UNITS - TAPE_PARTITION_1_UNITS; 2842 devip->tape_pending_part_1_size = TAPE_PARTITION_1_UNITS; 2843 } else { 2844 new_nbr = new[3] + 1; 2845 if (new_nbr > TAPE_MAX_PARTITIONS) 2846 return 3; 2847 if ((new[4] & 0x40) != 0) { /* SDP */ 2848 p1_size = TAPE_PARTITION_1_UNITS; 2849 p0_size = TAPE_UNITS - p1_size; 2850 if (p0_size < 100) 2851 return 4; 2852 } else if ((new[4] & 0x20) != 0) { 2853 if (new_nbr > 1) { 2854 p0_size = get_unaligned_be16(new + 8); 2855 p1_size = get_unaligned_be16(new + 10); 2856 if (p1_size == 0xFFFF) 2857 p1_size = TAPE_UNITS - p0_size; 2858 else if (p0_size == 0xFFFF) 2859 p0_size = TAPE_UNITS - p1_size; 2860 if (p0_size < 100 || p1_size < 100) 2861 return 8; 2862 } else { 2863 p0_size = TAPE_UNITS; 2864 p1_size = 0; 2865 } 2866 } else 2867 return 6; 2868 devip->tape_pending_nbr_partitions = new_nbr; 2869 devip->tape_pending_part_0_size = p0_size; 2870 devip->tape_pending_part_1_size = p1_size; 2871 partition_pg[3] = new_nbr; 2872 devip->tape_pending_nbr_partitions = new_nbr; 2873 } 2874 2875 return 0; 2876 } 2877 2878 static int resp_compression_m_pg(unsigned char *p, int pcontrol, int target, 2879 unsigned char dce) 2880 { /* Compression page for mode_sense (tape) */ 2881 static const unsigned char compression_pg[] = { 2882 0x0f, 14, 0x40, 0, 0, 0, 0, 0, 2883 0, 0, 0, 0, 0, 0 2884 }; 2885 2886 memcpy(p, compression_pg, sizeof(compression_pg)); 2887 if (dce) 2888 p[2] |= 0x80; 2889 if (pcontrol == 1) 2890 memset(p + 2, 0, sizeof(compression_pg) - 2); 2891 return sizeof(compression_pg); 2892 } 2893 2894 /* PAGE_SIZE is more than necessary but provides room for future expansion. */ 2895 #define SDEBUG_MAX_MSENSE_SZ PAGE_SIZE 2896 2897 static int resp_mode_sense(struct scsi_cmnd *scp, 2898 struct sdebug_dev_info *devip) 2899 { 2900 int pcontrol, pcode, subpcode, bd_len; 2901 unsigned char dev_spec; 2902 u32 alloc_len, offset, len; 2903 int target_dev_id; 2904 int target = scp->device->id; 2905 unsigned char *ap; 2906 unsigned char *cmd = scp->cmnd; 2907 bool dbd, llbaa, msense_6, is_disk, is_zbc, is_tape; 2908 2909 unsigned char *arr __free(kfree) = kzalloc(SDEBUG_MAX_MSENSE_SZ, GFP_ATOMIC); 2910 2911 if (!arr) 2912 return -ENOMEM; 2913 dbd = !!(cmd[1] & 0x8); /* disable block descriptors */ 2914 pcontrol = (cmd[2] & 0xc0) >> 6; 2915 pcode = cmd[2] & 0x3f; 2916 subpcode = cmd[3]; 2917 msense_6 = (MODE_SENSE == cmd[0]); 2918 llbaa = msense_6 ? false : !!(cmd[1] & 0x10); 2919 is_disk = (scp->device->type == TYPE_DISK); 2920 is_zbc = devip->zoned; 2921 is_tape = (scp->device->type == TYPE_TAPE); 2922 if ((is_disk || is_zbc || is_tape) && !dbd) 2923 bd_len = llbaa ? 16 : 8; 2924 else 2925 bd_len = 0; 2926 alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7); 2927 if (0x3 == pcontrol) { /* Saving values not supported */ 2928 mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0); 2929 return check_condition_result; 2930 } 2931 target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) + 2932 (devip->target * 1000) - 3; 2933 /* for disks+zbc set DPOFUA bit and clear write protect (WP) bit */ 2934 if (is_disk || is_zbc) { 2935 dev_spec = 0x10; /* =0x90 if WP=1 implies read-only */ 2936 if (sdebug_wp) 2937 dev_spec |= 0x80; 2938 } else 2939 dev_spec = 0x0; 2940 if (msense_6) { 2941 arr[2] = dev_spec; 2942 arr[3] = bd_len; 2943 offset = 4; 2944 } else { 2945 arr[3] = dev_spec; 2946 if (16 == bd_len) 2947 arr[4] = 0x1; /* set LONGLBA bit */ 2948 arr[7] = bd_len; /* assume 255 or less */ 2949 offset = 8; 2950 } 2951 ap = arr + offset; 2952 if ((bd_len > 0) && (!sdebug_capacity)) 2953 sdebug_capacity = get_sdebug_capacity(); 2954 2955 if (8 == bd_len) { 2956 if (sdebug_capacity > 0xfffffffe) 2957 put_unaligned_be32(0xffffffff, ap + 0); 2958 else 2959 put_unaligned_be32(sdebug_capacity, ap + 0); 2960 if (is_tape) { 2961 ap[0] = devip->tape_density; 2962 put_unaligned_be16(devip->tape_blksize, ap + 6); 2963 } else 2964 put_unaligned_be16(sdebug_sector_size, ap + 6); 2965 offset += bd_len; 2966 ap = arr + offset; 2967 } else if (16 == bd_len) { 2968 if (is_tape) { 2969 mk_sense_invalid_fld(scp, SDEB_IN_DATA, 1, 4); 2970 return check_condition_result; 2971 } 2972 put_unaligned_be64((u64)sdebug_capacity, ap + 0); 2973 put_unaligned_be32(sdebug_sector_size, ap + 12); 2974 offset += bd_len; 2975 ap = arr + offset; 2976 } 2977 if (cmd[2] == 0) 2978 goto only_bd; /* Only block descriptor requested */ 2979 2980 /* 2981 * N.B. If len>0 before resp_*_pg() call, then form of that call should be: 2982 * len += resp_*_pg(ap + len, pcontrol, target); 2983 */ 2984 switch (pcode) { 2985 case 0x1: /* Read-Write error recovery page, direct access */ 2986 if (subpcode > 0x0 && subpcode < 0xff) 2987 goto bad_subpcode; 2988 len = resp_err_recov_pg(ap, pcontrol, target); 2989 offset += len; 2990 break; 2991 case 0x2: /* Disconnect-Reconnect page, all devices */ 2992 if (subpcode > 0x0 && subpcode < 0xff) 2993 goto bad_subpcode; 2994 len = resp_disconnect_pg(ap, pcontrol, target); 2995 offset += len; 2996 break; 2997 case 0x3: /* Format device page, direct access */ 2998 if (subpcode > 0x0 && subpcode < 0xff) 2999 goto bad_subpcode; 3000 if (is_disk) { 3001 len = resp_format_pg(ap, pcontrol, target); 3002 offset += len; 3003 } else { 3004 goto bad_pcode; 3005 } 3006 break; 3007 case 0x8: /* Caching page, direct access */ 3008 if (subpcode > 0x0 && subpcode < 0xff) 3009 goto bad_subpcode; 3010 if (is_disk || is_zbc) { 3011 len = resp_caching_pg(ap, pcontrol, target); 3012 offset += len; 3013 } else { 3014 goto bad_pcode; 3015 } 3016 break; 3017 case 0xa: /* Control Mode page, all devices */ 3018 switch (subpcode) { 3019 case 0: 3020 len = resp_ctrl_m_pg(ap, pcontrol, target); 3021 break; 3022 case 0x05: 3023 len = resp_grouping_m_pg(ap, pcontrol, target); 3024 break; 3025 case 0xff: 3026 len = resp_ctrl_m_pg(ap, pcontrol, target); 3027 len += resp_grouping_m_pg(ap + len, pcontrol, target); 3028 break; 3029 default: 3030 goto bad_subpcode; 3031 } 3032 offset += len; 3033 break; 3034 case 0xf: /* Compression Mode Page (tape) */ 3035 if (!is_tape) 3036 goto bad_pcode; 3037 len = resp_compression_m_pg(ap, pcontrol, target, devip->tape_dce); 3038 offset += len; 3039 break; 3040 case 0x11: /* Partition Mode Page (tape) */ 3041 if (!is_tape) 3042 goto bad_pcode; 3043 len = resp_partition_m_pg(ap, pcontrol, target); 3044 offset += len; 3045 break; 3046 case 0x19: /* if spc==1 then sas phy, control+discover */ 3047 if (subpcode > 0x2 && subpcode < 0xff) 3048 goto bad_subpcode; 3049 len = 0; 3050 if ((0x0 == subpcode) || (0xff == subpcode)) 3051 len += resp_sas_sf_m_pg(ap + len, pcontrol, target); 3052 if ((0x1 == subpcode) || (0xff == subpcode)) 3053 len += resp_sas_pcd_m_spg(ap + len, pcontrol, target, 3054 target_dev_id); 3055 if ((0x2 == subpcode) || (0xff == subpcode)) 3056 len += resp_sas_sha_m_spg(ap + len, pcontrol); 3057 offset += len; 3058 break; 3059 case 0x1c: /* Informational Exceptions Mode page, all devices */ 3060 if (subpcode > 0x0 && subpcode < 0xff) 3061 goto bad_subpcode; 3062 len = resp_iec_m_pg(ap, pcontrol, target); 3063 offset += len; 3064 break; 3065 case 0x3f: /* Read all Mode pages */ 3066 if (subpcode > 0x0 && subpcode < 0xff) 3067 goto bad_subpcode; 3068 len = resp_err_recov_pg(ap, pcontrol, target); 3069 len += resp_disconnect_pg(ap + len, pcontrol, target); 3070 if (is_disk) { 3071 len += resp_format_pg(ap + len, pcontrol, target); 3072 len += resp_caching_pg(ap + len, pcontrol, target); 3073 } else if (is_zbc) { 3074 len += resp_caching_pg(ap + len, pcontrol, target); 3075 } 3076 len += resp_ctrl_m_pg(ap + len, pcontrol, target); 3077 if (0xff == subpcode) 3078 len += resp_grouping_m_pg(ap + len, pcontrol, target); 3079 len += resp_sas_sf_m_pg(ap + len, pcontrol, target); 3080 if (0xff == subpcode) { 3081 len += resp_sas_pcd_m_spg(ap + len, pcontrol, target, 3082 target_dev_id); 3083 len += resp_sas_sha_m_spg(ap + len, pcontrol); 3084 } 3085 len += resp_iec_m_pg(ap + len, pcontrol, target); 3086 offset += len; 3087 break; 3088 default: 3089 goto bad_pcode; 3090 } 3091 only_bd: 3092 if (msense_6) 3093 arr[0] = offset - 1; 3094 else 3095 put_unaligned_be16((offset - 2), arr + 0); 3096 return fill_from_dev_buffer(scp, arr, min_t(u32, alloc_len, offset)); 3097 3098 bad_pcode: 3099 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); 3100 return check_condition_result; 3101 3102 bad_subpcode: 3103 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); 3104 return check_condition_result; 3105 } 3106 3107 #define SDEBUG_MAX_MSELECT_SZ 512 3108 3109 static int resp_mode_select(struct scsi_cmnd *scp, 3110 struct sdebug_dev_info *devip) 3111 { 3112 int pf, sp, ps, md_len, bd_len, off, spf, pg_len; 3113 int param_len, res, mpage; 3114 unsigned char arr[SDEBUG_MAX_MSELECT_SZ]; 3115 unsigned char *cmd = scp->cmnd; 3116 int mselect6 = (MODE_SELECT == cmd[0]); 3117 3118 memset(arr, 0, sizeof(arr)); 3119 pf = cmd[1] & 0x10; 3120 sp = cmd[1] & 0x1; 3121 param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7); 3122 if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) { 3123 mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1); 3124 return check_condition_result; 3125 } 3126 res = fetch_to_dev_buffer(scp, arr, param_len); 3127 if (-1 == res) 3128 return DID_ERROR << 16; 3129 else if (sdebug_verbose && (res < param_len)) 3130 sdev_printk(KERN_INFO, scp->device, 3131 "cdb indicated=%d, IO sent=%d bytes\n", 3132 param_len, res); 3133 md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2); 3134 bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6); 3135 off = (mselect6 ? 4 : 8); 3136 if (scp->device->type == TYPE_TAPE) { 3137 int blksize; 3138 3139 if (bd_len != 8) { 3140 mk_sense_invalid_fld(scp, SDEB_IN_DATA, 3141 mselect6 ? 3 : 6, -1); 3142 return check_condition_result; 3143 } 3144 if (arr[off] == TAPE_BAD_DENSITY) { 3145 mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1); 3146 return check_condition_result; 3147 } 3148 blksize = get_unaligned_be16(arr + off + 6); 3149 if (blksize != 0 && 3150 (blksize < TAPE_MIN_BLKSIZE || 3151 blksize > TAPE_MAX_BLKSIZE || 3152 (blksize % 4) != 0)) { 3153 mk_sense_invalid_fld(scp, SDEB_IN_DATA, 1, -1); 3154 return check_condition_result; 3155 } 3156 devip->tape_density = arr[off]; 3157 devip->tape_blksize = blksize; 3158 } 3159 off += bd_len; 3160 if (off >= res) 3161 return 0; /* No page written, just descriptors */ 3162 if (md_len > 2) { 3163 mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1); 3164 return check_condition_result; 3165 } 3166 mpage = arr[off] & 0x3f; 3167 ps = !!(arr[off] & 0x80); 3168 if (ps) { 3169 mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7); 3170 return check_condition_result; 3171 } 3172 spf = !!(arr[off] & 0x40); 3173 pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) : 3174 (arr[off + 1] + 2); 3175 if ((pg_len + off) > param_len) { 3176 mk_sense_buffer(scp, ILLEGAL_REQUEST, 3177 PARAMETER_LIST_LENGTH_ERR, 0); 3178 return check_condition_result; 3179 } 3180 switch (mpage) { 3181 case 0x8: /* Caching Mode page */ 3182 if (caching_pg[1] == arr[off + 1]) { 3183 memcpy(caching_pg + 2, arr + off + 2, 3184 sizeof(caching_pg) - 2); 3185 goto set_mode_changed_ua; 3186 } 3187 break; 3188 case 0xa: /* Control Mode page */ 3189 if (ctrl_m_pg[1] == arr[off + 1]) { 3190 memcpy(ctrl_m_pg + 2, arr + off + 2, 3191 sizeof(ctrl_m_pg) - 2); 3192 if (ctrl_m_pg[4] & 0x8) 3193 sdebug_wp = true; 3194 else 3195 sdebug_wp = false; 3196 sdebug_dsense = !!(ctrl_m_pg[2] & 0x4); 3197 goto set_mode_changed_ua; 3198 } 3199 break; 3200 case 0xf: /* Compression mode page */ 3201 if (scp->device->type != TYPE_TAPE) 3202 goto bad_pcode; 3203 if ((arr[off + 2] & 0x40) != 0) { 3204 devip->tape_dce = (arr[off + 2] & 0x80) != 0; 3205 return 0; 3206 } 3207 break; 3208 case 0x11: /* Medium Partition Mode Page (tape) */ 3209 if (scp->device->type == TYPE_TAPE) { 3210 int fld; 3211 3212 fld = process_medium_part_m_pg(devip, &arr[off], pg_len); 3213 if (fld == 0) 3214 return 0; 3215 mk_sense_invalid_fld(scp, SDEB_IN_DATA, fld, -1); 3216 return check_condition_result; 3217 } 3218 break; 3219 case 0x1c: /* Informational Exceptions Mode page */ 3220 if (iec_m_pg[1] == arr[off + 1]) { 3221 memcpy(iec_m_pg + 2, arr + off + 2, 3222 sizeof(iec_m_pg) - 2); 3223 goto set_mode_changed_ua; 3224 } 3225 break; 3226 default: 3227 break; 3228 } 3229 mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5); 3230 return check_condition_result; 3231 set_mode_changed_ua: 3232 set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm); 3233 return 0; 3234 3235 bad_pcode: 3236 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); 3237 return check_condition_result; 3238 } 3239 3240 static int resp_temp_l_pg(unsigned char *arr) 3241 { 3242 static const unsigned char temp_l_pg[] = { 3243 0x0, 0x0, 0x3, 0x2, 0x0, 38, 3244 0x0, 0x1, 0x3, 0x2, 0x0, 65, 3245 }; 3246 3247 memcpy(arr, temp_l_pg, sizeof(temp_l_pg)); 3248 return sizeof(temp_l_pg); 3249 } 3250 3251 static int resp_ie_l_pg(unsigned char *arr) 3252 { 3253 static const unsigned char ie_l_pg[] = { 3254 0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38, 3255 }; 3256 3257 memcpy(arr, ie_l_pg, sizeof(ie_l_pg)); 3258 if (iec_m_pg[2] & 0x4) { /* TEST bit set */ 3259 arr[4] = THRESHOLD_EXCEEDED; 3260 arr[5] = 0xff; 3261 } 3262 return sizeof(ie_l_pg); 3263 } 3264 3265 static int resp_env_rep_l_spg(unsigned char *arr) 3266 { 3267 static const unsigned char env_rep_l_spg[] = { 3268 0x0, 0x0, 0x23, 0x8, 3269 0x0, 40, 72, 0xff, 45, 18, 0, 0, 3270 0x1, 0x0, 0x23, 0x8, 3271 0x0, 55, 72, 35, 55, 45, 0, 0, 3272 }; 3273 3274 memcpy(arr, env_rep_l_spg, sizeof(env_rep_l_spg)); 3275 return sizeof(env_rep_l_spg); 3276 } 3277 3278 #define SDEBUG_MAX_LSENSE_SZ 512 3279 3280 static int resp_log_sense(struct scsi_cmnd *scp, 3281 struct sdebug_dev_info *devip) 3282 { 3283 int ppc, sp, pcode, subpcode; 3284 u32 alloc_len, len, n; 3285 unsigned char arr[SDEBUG_MAX_LSENSE_SZ]; 3286 unsigned char *cmd = scp->cmnd; 3287 3288 memset(arr, 0, sizeof(arr)); 3289 ppc = cmd[1] & 0x2; 3290 sp = cmd[1] & 0x1; 3291 if (ppc || sp) { 3292 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0); 3293 return check_condition_result; 3294 } 3295 pcode = cmd[2] & 0x3f; 3296 subpcode = cmd[3] & 0xff; 3297 alloc_len = get_unaligned_be16(cmd + 7); 3298 arr[0] = pcode; 3299 if (0 == subpcode) { 3300 switch (pcode) { 3301 case 0x0: /* Supported log pages log page */ 3302 n = 4; 3303 arr[n++] = 0x0; /* this page */ 3304 arr[n++] = 0xd; /* Temperature */ 3305 arr[n++] = 0x2f; /* Informational exceptions */ 3306 arr[3] = n - 4; 3307 break; 3308 case 0xd: /* Temperature log page */ 3309 arr[3] = resp_temp_l_pg(arr + 4); 3310 break; 3311 case 0x2f: /* Informational exceptions log page */ 3312 arr[3] = resp_ie_l_pg(arr + 4); 3313 break; 3314 default: 3315 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); 3316 return check_condition_result; 3317 } 3318 } else if (0xff == subpcode) { 3319 arr[0] |= 0x40; 3320 arr[1] = subpcode; 3321 switch (pcode) { 3322 case 0x0: /* Supported log pages and subpages log page */ 3323 n = 4; 3324 arr[n++] = 0x0; 3325 arr[n++] = 0x0; /* 0,0 page */ 3326 arr[n++] = 0x0; 3327 arr[n++] = 0xff; /* this page */ 3328 arr[n++] = 0xd; 3329 arr[n++] = 0x0; /* Temperature */ 3330 arr[n++] = 0xd; 3331 arr[n++] = 0x1; /* Environment reporting */ 3332 arr[n++] = 0xd; 3333 arr[n++] = 0xff; /* all 0xd subpages */ 3334 arr[n++] = 0x2f; 3335 arr[n++] = 0x0; /* Informational exceptions */ 3336 arr[n++] = 0x2f; 3337 arr[n++] = 0xff; /* all 0x2f subpages */ 3338 arr[3] = n - 4; 3339 break; 3340 case 0xd: /* Temperature subpages */ 3341 n = 4; 3342 arr[n++] = 0xd; 3343 arr[n++] = 0x0; /* Temperature */ 3344 arr[n++] = 0xd; 3345 arr[n++] = 0x1; /* Environment reporting */ 3346 arr[n++] = 0xd; 3347 arr[n++] = 0xff; /* these subpages */ 3348 arr[3] = n - 4; 3349 break; 3350 case 0x2f: /* Informational exceptions subpages */ 3351 n = 4; 3352 arr[n++] = 0x2f; 3353 arr[n++] = 0x0; /* Informational exceptions */ 3354 arr[n++] = 0x2f; 3355 arr[n++] = 0xff; /* these subpages */ 3356 arr[3] = n - 4; 3357 break; 3358 default: 3359 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); 3360 return check_condition_result; 3361 } 3362 } else if (subpcode > 0) { 3363 arr[0] |= 0x40; 3364 arr[1] = subpcode; 3365 if (pcode == 0xd && subpcode == 1) 3366 arr[3] = resp_env_rep_l_spg(arr + 4); 3367 else { 3368 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); 3369 return check_condition_result; 3370 } 3371 } else { 3372 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); 3373 return check_condition_result; 3374 } 3375 len = min_t(u32, get_unaligned_be16(arr + 2) + 4, alloc_len); 3376 return fill_from_dev_buffer(scp, arr, 3377 min_t(u32, len, SDEBUG_MAX_INQ_ARR_SZ)); 3378 } 3379 3380 enum {SDEBUG_READ_BLOCK_LIMITS_ARR_SZ = 6}; 3381 static int resp_read_blklimits(struct scsi_cmnd *scp, 3382 struct sdebug_dev_info *devip) 3383 { 3384 unsigned char arr[SDEBUG_READ_BLOCK_LIMITS_ARR_SZ]; 3385 3386 arr[0] = 4; 3387 put_unaligned_be24(TAPE_MAX_BLKSIZE, arr + 1); 3388 put_unaligned_be16(TAPE_MIN_BLKSIZE, arr + 4); 3389 return fill_from_dev_buffer(scp, arr, SDEBUG_READ_BLOCK_LIMITS_ARR_SZ); 3390 } 3391 3392 static int resp_locate(struct scsi_cmnd *scp, 3393 struct sdebug_dev_info *devip) 3394 { 3395 unsigned char *cmd = scp->cmnd; 3396 unsigned int i, pos; 3397 struct tape_block *blp; 3398 int partition; 3399 3400 if ((cmd[1] & 0x02) != 0) { 3401 if (cmd[8] >= devip->tape_nbr_partitions) { 3402 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, -1); 3403 return check_condition_result; 3404 } 3405 devip->tape_partition = cmd[8]; 3406 } 3407 pos = get_unaligned_be32(cmd + 3); 3408 partition = devip->tape_partition; 3409 3410 for (i = 0, blp = devip->tape_blocks[partition]; 3411 i < pos && i < devip->tape_eop[partition]; i++, blp++) 3412 if (IS_TAPE_BLOCK_EOD(blp->fl_size)) 3413 break; 3414 if (i < pos) { 3415 devip->tape_location[partition] = i; 3416 mk_sense_buffer(scp, BLANK_CHECK, 0x05, 0); 3417 return check_condition_result; 3418 } 3419 devip->tape_location[partition] = pos; 3420 3421 return 0; 3422 } 3423 3424 static int resp_write_filemarks(struct scsi_cmnd *scp, 3425 struct sdebug_dev_info *devip) 3426 { 3427 unsigned char *cmd = scp->cmnd; 3428 unsigned int i, count, pos; 3429 u32 data; 3430 int partition = devip->tape_partition; 3431 3432 if ((cmd[1] & 0xfe) != 0) { /* probably write setmarks, not in >= SCSI-3 */ 3433 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1); 3434 return check_condition_result; 3435 } 3436 count = get_unaligned_be24(cmd + 2); 3437 data = TAPE_BLOCK_FM_FLAG; 3438 for (i = 0, pos = devip->tape_location[partition]; i < count; i++, pos++) { 3439 if (pos >= devip->tape_eop[partition] - 1) { /* don't overwrite EOD */ 3440 devip->tape_location[partition] = devip->tape_eop[partition] - 1; 3441 mk_sense_info_tape(scp, VOLUME_OVERFLOW, NO_ADDITIONAL_SENSE, 3442 EOP_EOM_DETECTED_ASCQ, count, SENSE_FLAG_EOM); 3443 return check_condition_result; 3444 } 3445 (devip->tape_blocks[partition] + pos)->fl_size = data; 3446 } 3447 (devip->tape_blocks[partition] + pos)->fl_size = 3448 TAPE_BLOCK_EOD_FLAG; 3449 devip->tape_location[partition] = pos; 3450 3451 return 0; 3452 } 3453 3454 static int resp_space(struct scsi_cmnd *scp, 3455 struct sdebug_dev_info *devip) 3456 { 3457 unsigned char *cmd = scp->cmnd, code; 3458 int i = 0, pos, count; 3459 struct tape_block *blp; 3460 int partition = devip->tape_partition; 3461 3462 count = get_unaligned_be24(cmd + 2); 3463 if ((count & 0x800000) != 0) /* extend negative to 32-bit count */ 3464 count |= 0xff000000; 3465 code = cmd[1] & 0x0f; 3466 3467 pos = devip->tape_location[partition]; 3468 if (code == 0) { /* blocks */ 3469 if (count < 0) { 3470 count = (-count); 3471 pos -= 1; 3472 for (i = 0, blp = devip->tape_blocks[partition] + pos; i < count; 3473 i++) { 3474 if (pos < 0) 3475 goto is_bop; 3476 else if (IS_TAPE_BLOCK_FM(blp->fl_size)) 3477 goto is_fm; 3478 if (i > 0) { 3479 pos--; 3480 blp--; 3481 } 3482 } 3483 } else if (count > 0) { 3484 for (i = 0, blp = devip->tape_blocks[partition] + pos; i < count; 3485 i++, pos++, blp++) { 3486 if (IS_TAPE_BLOCK_EOD(blp->fl_size)) 3487 goto is_eod; 3488 if (IS_TAPE_BLOCK_FM(blp->fl_size)) { 3489 pos += 1; 3490 goto is_fm; 3491 } 3492 if (pos >= devip->tape_eop[partition]) 3493 goto is_eop; 3494 } 3495 } 3496 } else if (code == 1) { /* filemarks */ 3497 if (count < 0) { 3498 count = (-count); 3499 if (pos == 0) 3500 goto is_bop; 3501 else { 3502 for (i = 0, blp = devip->tape_blocks[partition] + pos; 3503 i < count && pos >= 0; i++, pos--, blp--) { 3504 for (pos--, blp-- ; !IS_TAPE_BLOCK_FM(blp->fl_size) && 3505 pos >= 0; pos--, blp--) 3506 ; /* empty */ 3507 if (pos < 0) 3508 goto is_bop; 3509 } 3510 } 3511 pos += 1; 3512 } else if (count > 0) { 3513 for (i = 0, blp = devip->tape_blocks[partition] + pos; 3514 i < count; i++, pos++, blp++) { 3515 for ( ; !IS_TAPE_BLOCK_FM(blp->fl_size) && 3516 !IS_TAPE_BLOCK_EOD(blp->fl_size) && 3517 pos < devip->tape_eop[partition]; 3518 pos++, blp++) 3519 ; /* empty */ 3520 if (IS_TAPE_BLOCK_EOD(blp->fl_size)) 3521 goto is_eod; 3522 if (pos >= devip->tape_eop[partition]) 3523 goto is_eop; 3524 } 3525 } 3526 } else if (code == 3) { /* EOD */ 3527 for (blp = devip->tape_blocks[partition] + pos; 3528 !IS_TAPE_BLOCK_EOD(blp->fl_size) && pos < devip->tape_eop[partition]; 3529 pos++, blp++) 3530 ; /* empty */ 3531 if (pos >= devip->tape_eop[partition]) 3532 goto is_eop; 3533 } else { 3534 /* sequential filemarks not supported */ 3535 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, -1); 3536 return check_condition_result; 3537 } 3538 devip->tape_location[partition] = pos; 3539 return 0; 3540 3541 is_fm: 3542 devip->tape_location[partition] = pos; 3543 mk_sense_info_tape(scp, NO_SENSE, NO_ADDITIONAL_SENSE, 3544 FILEMARK_DETECTED_ASCQ, count - i, 3545 SENSE_FLAG_FILEMARK); 3546 return check_condition_result; 3547 3548 is_eod: 3549 devip->tape_location[partition] = pos; 3550 mk_sense_info_tape(scp, BLANK_CHECK, NO_ADDITIONAL_SENSE, 3551 EOD_DETECTED_ASCQ, count - i, 3552 0); 3553 return check_condition_result; 3554 3555 is_bop: 3556 devip->tape_location[partition] = 0; 3557 mk_sense_info_tape(scp, NO_SENSE, NO_ADDITIONAL_SENSE, 3558 BEGINNING_OF_P_M_DETECTED_ASCQ, count - i, 3559 SENSE_FLAG_EOM); 3560 devip->tape_location[partition] = 0; 3561 return check_condition_result; 3562 3563 is_eop: 3564 devip->tape_location[partition] = devip->tape_eop[partition] - 1; 3565 mk_sense_info_tape(scp, MEDIUM_ERROR, NO_ADDITIONAL_SENSE, 3566 EOP_EOM_DETECTED_ASCQ, (unsigned int)i, 3567 SENSE_FLAG_EOM); 3568 return check_condition_result; 3569 } 3570 3571 enum {SDEBUG_READ_POSITION_ARR_SZ = 20}; 3572 static int resp_read_position(struct scsi_cmnd *scp, 3573 struct sdebug_dev_info *devip) 3574 { 3575 u8 *cmd = scp->cmnd; 3576 int all_length; 3577 unsigned char arr[20]; 3578 unsigned int pos; 3579 3580 all_length = get_unaligned_be16(cmd + 7); 3581 if ((cmd[1] & 0xfe) != 0 || 3582 all_length != 0) { /* only short form */ 3583 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3584 all_length ? 7 : 1, 0); 3585 return check_condition_result; 3586 } 3587 memset(arr, 0, SDEBUG_READ_POSITION_ARR_SZ); 3588 arr[1] = devip->tape_partition; 3589 pos = devip->tape_location[devip->tape_partition]; 3590 put_unaligned_be32(pos, arr + 4); 3591 put_unaligned_be32(pos, arr + 8); 3592 return fill_from_dev_buffer(scp, arr, SDEBUG_READ_POSITION_ARR_SZ); 3593 } 3594 3595 static int resp_rewind(struct scsi_cmnd *scp, 3596 struct sdebug_dev_info *devip) 3597 { 3598 devip->tape_location[devip->tape_partition] = 0; 3599 3600 return 0; 3601 } 3602 3603 static int partition_tape(struct sdebug_dev_info *devip, int nbr_partitions, 3604 int part_0_size, int part_1_size) 3605 { 3606 int i; 3607 3608 if (part_0_size + part_1_size > TAPE_UNITS) 3609 return -1; 3610 devip->tape_eop[0] = part_0_size; 3611 devip->tape_blocks[0]->fl_size = TAPE_BLOCK_EOD_FLAG; 3612 devip->tape_eop[1] = part_1_size; 3613 devip->tape_blocks[1] = devip->tape_blocks[0] + 3614 devip->tape_eop[0]; 3615 devip->tape_blocks[1]->fl_size = TAPE_BLOCK_EOD_FLAG; 3616 3617 for (i = 0 ; i < TAPE_MAX_PARTITIONS; i++) 3618 devip->tape_location[i] = 0; 3619 3620 devip->tape_nbr_partitions = nbr_partitions; 3621 devip->tape_partition = 0; 3622 3623 partition_pg[3] = nbr_partitions - 1; 3624 put_unaligned_be16(devip->tape_eop[0], partition_pg + 8); 3625 put_unaligned_be16(devip->tape_eop[1], partition_pg + 10); 3626 3627 return nbr_partitions; 3628 } 3629 3630 static int resp_format_medium(struct scsi_cmnd *scp, 3631 struct sdebug_dev_info *devip) 3632 { 3633 int res = 0; 3634 unsigned char *cmd = scp->cmnd; 3635 3636 if (cmd[2] > 2) { 3637 mk_sense_invalid_fld(scp, SDEB_IN_DATA, 2, -1); 3638 return check_condition_result; 3639 } 3640 if (cmd[2] != 0) { 3641 if (devip->tape_pending_nbr_partitions > 0) { 3642 res = partition_tape(devip, 3643 devip->tape_pending_nbr_partitions, 3644 devip->tape_pending_part_0_size, 3645 devip->tape_pending_part_1_size); 3646 } else 3647 res = partition_tape(devip, devip->tape_nbr_partitions, 3648 devip->tape_eop[0], devip->tape_eop[1]); 3649 } else 3650 res = partition_tape(devip, 1, TAPE_UNITS, 0); 3651 if (res < 0) 3652 return -EINVAL; 3653 3654 devip->tape_pending_nbr_partitions = -1; 3655 3656 return 0; 3657 } 3658 3659 static int resp_erase(struct scsi_cmnd *scp, 3660 struct sdebug_dev_info *devip) 3661 { 3662 int partition = devip->tape_partition; 3663 int pos = devip->tape_location[partition]; 3664 struct tape_block *blp; 3665 3666 blp = devip->tape_blocks[partition] + pos; 3667 blp->fl_size = TAPE_BLOCK_EOD_FLAG; 3668 3669 return 0; 3670 } 3671 3672 static inline bool sdebug_dev_is_zoned(struct sdebug_dev_info *devip) 3673 { 3674 return devip->nr_zones != 0; 3675 } 3676 3677 static struct sdeb_zone_state *zbc_zone(struct sdebug_dev_info *devip, 3678 unsigned long long lba) 3679 { 3680 u32 zno = lba >> devip->zsize_shift; 3681 struct sdeb_zone_state *zsp; 3682 3683 if (devip->zcap == devip->zsize || zno < devip->nr_conv_zones) 3684 return &devip->zstate[zno]; 3685 3686 /* 3687 * If the zone capacity is less than the zone size, adjust for gap 3688 * zones. 3689 */ 3690 zno = 2 * zno - devip->nr_conv_zones; 3691 WARN_ONCE(zno >= devip->nr_zones, "%u > %u\n", zno, devip->nr_zones); 3692 zsp = &devip->zstate[zno]; 3693 if (lba >= zsp->z_start + zsp->z_size) 3694 zsp++; 3695 WARN_ON_ONCE(lba >= zsp->z_start + zsp->z_size); 3696 return zsp; 3697 } 3698 3699 static inline bool zbc_zone_is_conv(struct sdeb_zone_state *zsp) 3700 { 3701 return zsp->z_type == ZBC_ZTYPE_CNV; 3702 } 3703 3704 static inline bool zbc_zone_is_gap(struct sdeb_zone_state *zsp) 3705 { 3706 return zsp->z_type == ZBC_ZTYPE_GAP; 3707 } 3708 3709 static inline bool zbc_zone_is_seq(struct sdeb_zone_state *zsp) 3710 { 3711 return !zbc_zone_is_conv(zsp) && !zbc_zone_is_gap(zsp); 3712 } 3713 3714 static void zbc_close_zone(struct sdebug_dev_info *devip, 3715 struct sdeb_zone_state *zsp) 3716 { 3717 enum sdebug_z_cond zc; 3718 3719 if (!zbc_zone_is_seq(zsp)) 3720 return; 3721 3722 zc = zsp->z_cond; 3723 if (!(zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN)) 3724 return; 3725 3726 if (zc == ZC2_IMPLICIT_OPEN) 3727 devip->nr_imp_open--; 3728 else 3729 devip->nr_exp_open--; 3730 3731 if (zsp->z_wp == zsp->z_start) { 3732 zsp->z_cond = ZC1_EMPTY; 3733 } else { 3734 zsp->z_cond = ZC4_CLOSED; 3735 devip->nr_closed++; 3736 } 3737 } 3738 3739 static void zbc_close_imp_open_zone(struct sdebug_dev_info *devip) 3740 { 3741 struct sdeb_zone_state *zsp = &devip->zstate[0]; 3742 unsigned int i; 3743 3744 for (i = 0; i < devip->nr_zones; i++, zsp++) { 3745 if (zsp->z_cond == ZC2_IMPLICIT_OPEN) { 3746 zbc_close_zone(devip, zsp); 3747 return; 3748 } 3749 } 3750 } 3751 3752 static void zbc_open_zone(struct sdebug_dev_info *devip, 3753 struct sdeb_zone_state *zsp, bool explicit) 3754 { 3755 enum sdebug_z_cond zc; 3756 3757 if (!zbc_zone_is_seq(zsp)) 3758 return; 3759 3760 zc = zsp->z_cond; 3761 if ((explicit && zc == ZC3_EXPLICIT_OPEN) || 3762 (!explicit && zc == ZC2_IMPLICIT_OPEN)) 3763 return; 3764 3765 /* Close an implicit open zone if necessary */ 3766 if (explicit && zsp->z_cond == ZC2_IMPLICIT_OPEN) 3767 zbc_close_zone(devip, zsp); 3768 else if (devip->max_open && 3769 devip->nr_imp_open + devip->nr_exp_open >= devip->max_open) 3770 zbc_close_imp_open_zone(devip); 3771 3772 if (zsp->z_cond == ZC4_CLOSED) 3773 devip->nr_closed--; 3774 if (explicit) { 3775 zsp->z_cond = ZC3_EXPLICIT_OPEN; 3776 devip->nr_exp_open++; 3777 } else { 3778 zsp->z_cond = ZC2_IMPLICIT_OPEN; 3779 devip->nr_imp_open++; 3780 } 3781 } 3782 3783 static inline void zbc_set_zone_full(struct sdebug_dev_info *devip, 3784 struct sdeb_zone_state *zsp) 3785 { 3786 switch (zsp->z_cond) { 3787 case ZC2_IMPLICIT_OPEN: 3788 devip->nr_imp_open--; 3789 break; 3790 case ZC3_EXPLICIT_OPEN: 3791 devip->nr_exp_open--; 3792 break; 3793 default: 3794 WARN_ONCE(true, "Invalid zone %llu condition %x\n", 3795 zsp->z_start, zsp->z_cond); 3796 break; 3797 } 3798 zsp->z_cond = ZC5_FULL; 3799 } 3800 3801 static void zbc_inc_wp(struct sdebug_dev_info *devip, 3802 unsigned long long lba, unsigned int num) 3803 { 3804 struct sdeb_zone_state *zsp = zbc_zone(devip, lba); 3805 unsigned long long n, end, zend = zsp->z_start + zsp->z_size; 3806 3807 if (!zbc_zone_is_seq(zsp)) 3808 return; 3809 3810 if (zsp->z_type == ZBC_ZTYPE_SWR) { 3811 zsp->z_wp += num; 3812 if (zsp->z_wp >= zend) 3813 zbc_set_zone_full(devip, zsp); 3814 return; 3815 } 3816 3817 while (num) { 3818 if (lba != zsp->z_wp) 3819 zsp->z_non_seq_resource = true; 3820 3821 end = lba + num; 3822 if (end >= zend) { 3823 n = zend - lba; 3824 zsp->z_wp = zend; 3825 } else if (end > zsp->z_wp) { 3826 n = num; 3827 zsp->z_wp = end; 3828 } else { 3829 n = num; 3830 } 3831 if (zsp->z_wp >= zend) 3832 zbc_set_zone_full(devip, zsp); 3833 3834 num -= n; 3835 lba += n; 3836 if (num) { 3837 zsp++; 3838 zend = zsp->z_start + zsp->z_size; 3839 } 3840 } 3841 } 3842 3843 static int check_zbc_access_params(struct scsi_cmnd *scp, 3844 unsigned long long lba, unsigned int num, bool write) 3845 { 3846 struct scsi_device *sdp = scp->device; 3847 struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata; 3848 struct sdeb_zone_state *zsp = zbc_zone(devip, lba); 3849 struct sdeb_zone_state *zsp_end = zbc_zone(devip, lba + num - 1); 3850 3851 if (!write) { 3852 /* For host-managed, reads cannot cross zone types boundaries */ 3853 if (zsp->z_type != zsp_end->z_type) { 3854 mk_sense_buffer(scp, ILLEGAL_REQUEST, 3855 LBA_OUT_OF_RANGE, 3856 READ_INVDATA_ASCQ); 3857 return check_condition_result; 3858 } 3859 return 0; 3860 } 3861 3862 /* Writing into a gap zone is not allowed */ 3863 if (zbc_zone_is_gap(zsp)) { 3864 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 3865 ATTEMPT_ACCESS_GAP); 3866 return check_condition_result; 3867 } 3868 3869 /* No restrictions for writes within conventional zones */ 3870 if (zbc_zone_is_conv(zsp)) { 3871 if (!zbc_zone_is_conv(zsp_end)) { 3872 mk_sense_buffer(scp, ILLEGAL_REQUEST, 3873 LBA_OUT_OF_RANGE, 3874 WRITE_BOUNDARY_ASCQ); 3875 return check_condition_result; 3876 } 3877 return 0; 3878 } 3879 3880 if (zsp->z_type == ZBC_ZTYPE_SWR) { 3881 /* Writes cannot cross sequential zone boundaries */ 3882 if (zsp_end != zsp) { 3883 mk_sense_buffer(scp, ILLEGAL_REQUEST, 3884 LBA_OUT_OF_RANGE, 3885 WRITE_BOUNDARY_ASCQ); 3886 return check_condition_result; 3887 } 3888 /* Cannot write full zones */ 3889 if (zsp->z_cond == ZC5_FULL) { 3890 mk_sense_buffer(scp, ILLEGAL_REQUEST, 3891 INVALID_FIELD_IN_CDB, 0); 3892 return check_condition_result; 3893 } 3894 /* Writes must be aligned to the zone WP */ 3895 if (lba != zsp->z_wp) { 3896 mk_sense_buffer(scp, ILLEGAL_REQUEST, 3897 LBA_OUT_OF_RANGE, 3898 UNALIGNED_WRITE_ASCQ); 3899 return check_condition_result; 3900 } 3901 } 3902 3903 /* Handle implicit open of closed and empty zones */ 3904 if (zsp->z_cond == ZC1_EMPTY || zsp->z_cond == ZC4_CLOSED) { 3905 if (devip->max_open && 3906 devip->nr_exp_open >= devip->max_open) { 3907 mk_sense_buffer(scp, DATA_PROTECT, 3908 INSUFF_RES_ASC, 3909 INSUFF_ZONE_ASCQ); 3910 return check_condition_result; 3911 } 3912 zbc_open_zone(devip, zsp, false); 3913 } 3914 3915 return 0; 3916 } 3917 3918 static inline int check_device_access_params 3919 (struct scsi_cmnd *scp, unsigned long long lba, 3920 unsigned int num, bool write) 3921 { 3922 struct scsi_device *sdp = scp->device; 3923 struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata; 3924 3925 if (lba + num > sdebug_capacity) { 3926 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); 3927 return check_condition_result; 3928 } 3929 /* transfer length excessive (tie in to block limits VPD page) */ 3930 if (num > sdebug_store_sectors) { 3931 /* needs work to find which cdb byte 'num' comes from */ 3932 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); 3933 return check_condition_result; 3934 } 3935 if (write && unlikely(sdebug_wp)) { 3936 mk_sense_buffer(scp, DATA_PROTECT, WRITE_PROTECTED, 0x2); 3937 return check_condition_result; 3938 } 3939 if (sdebug_dev_is_zoned(devip)) 3940 return check_zbc_access_params(scp, lba, num, write); 3941 3942 return 0; 3943 } 3944 3945 /* 3946 * Note: if BUG_ON() fires it usually indicates a problem with the parser 3947 * tables. Perhaps a missing F_FAKE_RW or FF_MEDIA_IO flag. Response functions 3948 * that access any of the "stores" in struct sdeb_store_info should call this 3949 * function with bug_if_fake_rw set to true. 3950 */ 3951 static inline struct sdeb_store_info *devip2sip(struct sdebug_dev_info *devip, 3952 bool bug_if_fake_rw) 3953 { 3954 if (sdebug_fake_rw) { 3955 BUG_ON(bug_if_fake_rw); /* See note above */ 3956 return NULL; 3957 } 3958 return xa_load(per_store_ap, devip->sdbg_host->si_idx); 3959 } 3960 3961 static inline void 3962 sdeb_read_lock(rwlock_t *lock) 3963 { 3964 if (sdebug_no_rwlock) 3965 __acquire(lock); 3966 else 3967 read_lock(lock); 3968 } 3969 3970 static inline void 3971 sdeb_read_unlock(rwlock_t *lock) 3972 { 3973 if (sdebug_no_rwlock) 3974 __release(lock); 3975 else 3976 read_unlock(lock); 3977 } 3978 3979 static inline void 3980 sdeb_write_lock(rwlock_t *lock) 3981 { 3982 if (sdebug_no_rwlock) 3983 __acquire(lock); 3984 else 3985 write_lock(lock); 3986 } 3987 3988 static inline void 3989 sdeb_write_unlock(rwlock_t *lock) 3990 { 3991 if (sdebug_no_rwlock) 3992 __release(lock); 3993 else 3994 write_unlock(lock); 3995 } 3996 3997 static inline void 3998 sdeb_data_read_lock(struct sdeb_store_info *sip) 3999 { 4000 BUG_ON(!sip); 4001 4002 sdeb_read_lock(&sip->macc_data_lck); 4003 } 4004 4005 static inline void 4006 sdeb_data_read_unlock(struct sdeb_store_info *sip) 4007 { 4008 BUG_ON(!sip); 4009 4010 sdeb_read_unlock(&sip->macc_data_lck); 4011 } 4012 4013 static inline void 4014 sdeb_data_write_lock(struct sdeb_store_info *sip) 4015 { 4016 BUG_ON(!sip); 4017 4018 sdeb_write_lock(&sip->macc_data_lck); 4019 } 4020 4021 static inline void 4022 sdeb_data_write_unlock(struct sdeb_store_info *sip) 4023 { 4024 BUG_ON(!sip); 4025 4026 sdeb_write_unlock(&sip->macc_data_lck); 4027 } 4028 4029 static inline void 4030 sdeb_data_sector_read_lock(struct sdeb_store_info *sip) 4031 { 4032 BUG_ON(!sip); 4033 4034 sdeb_read_lock(&sip->macc_sector_lck); 4035 } 4036 4037 static inline void 4038 sdeb_data_sector_read_unlock(struct sdeb_store_info *sip) 4039 { 4040 BUG_ON(!sip); 4041 4042 sdeb_read_unlock(&sip->macc_sector_lck); 4043 } 4044 4045 static inline void 4046 sdeb_data_sector_write_lock(struct sdeb_store_info *sip) 4047 { 4048 BUG_ON(!sip); 4049 4050 sdeb_write_lock(&sip->macc_sector_lck); 4051 } 4052 4053 static inline void 4054 sdeb_data_sector_write_unlock(struct sdeb_store_info *sip) 4055 { 4056 BUG_ON(!sip); 4057 4058 sdeb_write_unlock(&sip->macc_sector_lck); 4059 } 4060 4061 /* 4062 * Atomic locking: 4063 * We simplify the atomic model to allow only 1x atomic write and many non- 4064 * atomic reads or writes for all LBAs. 4065 4066 * A RW lock has a similar bahaviour: 4067 * Only 1x writer and many readers. 4068 4069 * So use a RW lock for per-device read and write locking: 4070 * An atomic access grabs the lock as a writer and non-atomic grabs the lock 4071 * as a reader. 4072 */ 4073 4074 static inline void 4075 sdeb_data_lock(struct sdeb_store_info *sip, bool atomic) 4076 { 4077 if (atomic) 4078 sdeb_data_write_lock(sip); 4079 else 4080 sdeb_data_read_lock(sip); 4081 } 4082 4083 static inline void 4084 sdeb_data_unlock(struct sdeb_store_info *sip, bool atomic) 4085 { 4086 if (atomic) 4087 sdeb_data_write_unlock(sip); 4088 else 4089 sdeb_data_read_unlock(sip); 4090 } 4091 4092 /* Allow many reads but only 1x write per sector */ 4093 static inline void 4094 sdeb_data_sector_lock(struct sdeb_store_info *sip, bool do_write) 4095 { 4096 if (do_write) 4097 sdeb_data_sector_write_lock(sip); 4098 else 4099 sdeb_data_sector_read_lock(sip); 4100 } 4101 4102 static inline void 4103 sdeb_data_sector_unlock(struct sdeb_store_info *sip, bool do_write) 4104 { 4105 if (do_write) 4106 sdeb_data_sector_write_unlock(sip); 4107 else 4108 sdeb_data_sector_read_unlock(sip); 4109 } 4110 4111 static inline void 4112 sdeb_meta_read_lock(struct sdeb_store_info *sip) 4113 { 4114 if (sdebug_no_rwlock) { 4115 if (sip) 4116 __acquire(&sip->macc_meta_lck); 4117 else 4118 __acquire(&sdeb_fake_rw_lck); 4119 } else { 4120 if (sip) 4121 read_lock(&sip->macc_meta_lck); 4122 else 4123 read_lock(&sdeb_fake_rw_lck); 4124 } 4125 } 4126 4127 static inline void 4128 sdeb_meta_read_unlock(struct sdeb_store_info *sip) 4129 { 4130 if (sdebug_no_rwlock) { 4131 if (sip) 4132 __release(&sip->macc_meta_lck); 4133 else 4134 __release(&sdeb_fake_rw_lck); 4135 } else { 4136 if (sip) 4137 read_unlock(&sip->macc_meta_lck); 4138 else 4139 read_unlock(&sdeb_fake_rw_lck); 4140 } 4141 } 4142 4143 static inline void 4144 sdeb_meta_write_lock(struct sdeb_store_info *sip) 4145 { 4146 if (sdebug_no_rwlock) { 4147 if (sip) 4148 __acquire(&sip->macc_meta_lck); 4149 else 4150 __acquire(&sdeb_fake_rw_lck); 4151 } else { 4152 if (sip) 4153 write_lock(&sip->macc_meta_lck); 4154 else 4155 write_lock(&sdeb_fake_rw_lck); 4156 } 4157 } 4158 4159 static inline void 4160 sdeb_meta_write_unlock(struct sdeb_store_info *sip) 4161 { 4162 if (sdebug_no_rwlock) { 4163 if (sip) 4164 __release(&sip->macc_meta_lck); 4165 else 4166 __release(&sdeb_fake_rw_lck); 4167 } else { 4168 if (sip) 4169 write_unlock(&sip->macc_meta_lck); 4170 else 4171 write_unlock(&sdeb_fake_rw_lck); 4172 } 4173 } 4174 4175 /* Returns number of bytes copied or -1 if error. */ 4176 static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp, 4177 u32 sg_skip, u64 lba, u32 num, u8 group_number, 4178 bool do_write, bool atomic) 4179 { 4180 int ret; 4181 u64 block; 4182 enum dma_data_direction dir; 4183 struct scsi_data_buffer *sdb = &scp->sdb; 4184 u8 *fsp; 4185 int i, total = 0; 4186 4187 /* 4188 * Even though reads are inherently atomic (in this driver), we expect 4189 * the atomic flag only for writes. 4190 */ 4191 if (!do_write && atomic) 4192 return -1; 4193 4194 if (do_write) { 4195 dir = DMA_TO_DEVICE; 4196 write_since_sync = true; 4197 } else { 4198 dir = DMA_FROM_DEVICE; 4199 } 4200 4201 if (!sdb->length || !sip) 4202 return 0; 4203 if (scp->sc_data_direction != dir) 4204 return -1; 4205 4206 if (do_write && group_number < ARRAY_SIZE(writes_by_group_number)) 4207 atomic_long_inc(&writes_by_group_number[group_number]); 4208 4209 fsp = sip->storep; 4210 4211 block = do_div(lba, sdebug_store_sectors); 4212 4213 /* Only allow 1x atomic write or multiple non-atomic writes at any given time */ 4214 sdeb_data_lock(sip, atomic); 4215 for (i = 0; i < num; i++) { 4216 /* We shouldn't need to lock for atomic writes, but do it anyway */ 4217 sdeb_data_sector_lock(sip, do_write); 4218 ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents, 4219 fsp + (block * sdebug_sector_size), 4220 sdebug_sector_size, sg_skip, do_write); 4221 sdeb_data_sector_unlock(sip, do_write); 4222 total += ret; 4223 if (ret != sdebug_sector_size) 4224 break; 4225 sg_skip += sdebug_sector_size; 4226 if (++block >= sdebug_store_sectors) 4227 block = 0; 4228 } 4229 sdeb_data_unlock(sip, atomic); 4230 4231 return total; 4232 } 4233 4234 /* Returns number of bytes copied or -1 if error. */ 4235 static int do_dout_fetch(struct scsi_cmnd *scp, u32 num, u8 *doutp) 4236 { 4237 struct scsi_data_buffer *sdb = &scp->sdb; 4238 4239 if (!sdb->length) 4240 return 0; 4241 if (scp->sc_data_direction != DMA_TO_DEVICE) 4242 return -1; 4243 return sg_copy_buffer(sdb->table.sgl, sdb->table.nents, doutp, 4244 num * sdebug_sector_size, 0, true); 4245 } 4246 4247 /* If sip->storep+lba compares equal to arr(num), then copy top half of 4248 * arr into sip->storep+lba and return true. If comparison fails then 4249 * return false. */ 4250 static bool comp_write_worker(struct sdeb_store_info *sip, u64 lba, u32 num, 4251 const u8 *arr, bool compare_only) 4252 { 4253 bool res; 4254 u64 block, rest = 0; 4255 u32 store_blks = sdebug_store_sectors; 4256 u32 lb_size = sdebug_sector_size; 4257 u8 *fsp = sip->storep; 4258 4259 block = do_div(lba, store_blks); 4260 if (block + num > store_blks) 4261 rest = block + num - store_blks; 4262 4263 res = !memcmp(fsp + (block * lb_size), arr, (num - rest) * lb_size); 4264 if (!res) 4265 return res; 4266 if (rest) 4267 res = !memcmp(fsp, arr + ((num - rest) * lb_size), 4268 rest * lb_size); 4269 if (!res) 4270 return res; 4271 if (compare_only) 4272 return true; 4273 arr += num * lb_size; 4274 memcpy(fsp + (block * lb_size), arr, (num - rest) * lb_size); 4275 if (rest) 4276 memcpy(fsp, arr + ((num - rest) * lb_size), rest * lb_size); 4277 return res; 4278 } 4279 4280 static __be16 dif_compute_csum(const void *buf, int len) 4281 { 4282 __be16 csum; 4283 4284 if (sdebug_guard) 4285 csum = (__force __be16)ip_compute_csum(buf, len); 4286 else 4287 csum = cpu_to_be16(crc_t10dif(buf, len)); 4288 4289 return csum; 4290 } 4291 4292 static int dif_verify(struct t10_pi_tuple *sdt, const void *data, 4293 sector_t sector, u32 ei_lba) 4294 { 4295 __be16 csum = dif_compute_csum(data, sdebug_sector_size); 4296 4297 if (sdt->guard_tag != csum) { 4298 pr_err("GUARD check failed on sector %lu rcvd 0x%04x, data 0x%04x\n", 4299 (unsigned long)sector, 4300 be16_to_cpu(sdt->guard_tag), 4301 be16_to_cpu(csum)); 4302 return 0x01; 4303 } 4304 if (sdebug_dif == T10_PI_TYPE1_PROTECTION && 4305 be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) { 4306 pr_err("REF check failed on sector %lu\n", 4307 (unsigned long)sector); 4308 return 0x03; 4309 } 4310 if (sdebug_dif == T10_PI_TYPE2_PROTECTION && 4311 be32_to_cpu(sdt->ref_tag) != ei_lba) { 4312 pr_err("REF check failed on sector %lu\n", 4313 (unsigned long)sector); 4314 return 0x03; 4315 } 4316 return 0; 4317 } 4318 4319 static void dif_copy_prot(struct scsi_cmnd *scp, sector_t sector, 4320 unsigned int sectors, bool read) 4321 { 4322 size_t resid; 4323 void *paddr; 4324 struct sdeb_store_info *sip = devip2sip((struct sdebug_dev_info *) 4325 scp->device->hostdata, true); 4326 struct t10_pi_tuple *dif_storep = sip->dif_storep; 4327 const void *dif_store_end = dif_storep + sdebug_store_sectors; 4328 struct sg_mapping_iter miter; 4329 4330 /* Bytes of protection data to copy into sgl */ 4331 resid = sectors * sizeof(*dif_storep); 4332 4333 sg_miter_start(&miter, scsi_prot_sglist(scp), 4334 scsi_prot_sg_count(scp), SG_MITER_ATOMIC | 4335 (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG)); 4336 4337 while (sg_miter_next(&miter) && resid > 0) { 4338 size_t len = min_t(size_t, miter.length, resid); 4339 void *start = dif_store(sip, sector); 4340 size_t rest = 0; 4341 4342 if (dif_store_end < start + len) 4343 rest = start + len - dif_store_end; 4344 4345 paddr = miter.addr; 4346 4347 if (read) 4348 memcpy(paddr, start, len - rest); 4349 else 4350 memcpy(start, paddr, len - rest); 4351 4352 if (rest) { 4353 if (read) 4354 memcpy(paddr + len - rest, dif_storep, rest); 4355 else 4356 memcpy(dif_storep, paddr + len - rest, rest); 4357 } 4358 4359 sector += len / sizeof(*dif_storep); 4360 resid -= len; 4361 } 4362 sg_miter_stop(&miter); 4363 } 4364 4365 static int prot_verify_read(struct scsi_cmnd *scp, sector_t start_sec, 4366 unsigned int sectors, u32 ei_lba) 4367 { 4368 int ret = 0; 4369 unsigned int i; 4370 sector_t sector; 4371 struct sdeb_store_info *sip = devip2sip((struct sdebug_dev_info *) 4372 scp->device->hostdata, true); 4373 struct t10_pi_tuple *sdt; 4374 4375 for (i = 0; i < sectors; i++, ei_lba++) { 4376 sector = start_sec + i; 4377 sdt = dif_store(sip, sector); 4378 4379 if (sdt->app_tag == cpu_to_be16(0xffff)) 4380 continue; 4381 4382 /* 4383 * Because scsi_debug acts as both initiator and 4384 * target we proceed to verify the PI even if 4385 * RDPROTECT=3. This is done so the "initiator" knows 4386 * which type of error to return. Otherwise we would 4387 * have to iterate over the PI twice. 4388 */ 4389 if (scp->cmnd[1] >> 5) { /* RDPROTECT */ 4390 ret = dif_verify(sdt, lba2fake_store(sip, sector), 4391 sector, ei_lba); 4392 if (ret) { 4393 dif_errors++; 4394 break; 4395 } 4396 } 4397 } 4398 4399 dif_copy_prot(scp, start_sec, sectors, true); 4400 dix_reads++; 4401 4402 return ret; 4403 } 4404 4405 static int resp_read_tape(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) 4406 { 4407 u32 i, num, transfer, size; 4408 u8 *cmd = scp->cmnd; 4409 struct scsi_data_buffer *sdb = &scp->sdb; 4410 int partition = devip->tape_partition; 4411 u32 pos = devip->tape_location[partition]; 4412 struct tape_block *blp; 4413 bool fixed, sili; 4414 4415 if (cmd[0] != READ_6) { /* Only Read(6) supported */ 4416 mk_sense_invalid_opcode(scp); 4417 return illegal_condition_result; 4418 } 4419 fixed = (cmd[1] & 0x1) != 0; 4420 sili = (cmd[1] & 0x2) != 0; 4421 if (fixed && sili) { 4422 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1); 4423 return check_condition_result; 4424 } 4425 4426 transfer = get_unaligned_be24(cmd + 2); 4427 if (fixed) { 4428 num = transfer; 4429 size = devip->tape_blksize; 4430 } else { 4431 if (transfer < TAPE_MIN_BLKSIZE || 4432 transfer > TAPE_MAX_BLKSIZE) { 4433 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); 4434 return check_condition_result; 4435 } 4436 num = 1; 4437 size = transfer; 4438 } 4439 4440 for (i = 0, blp = devip->tape_blocks[partition] + pos; 4441 i < num && pos < devip->tape_eop[partition]; 4442 i++, pos++, blp++) { 4443 devip->tape_location[partition] = pos + 1; 4444 if (IS_TAPE_BLOCK_FM(blp->fl_size)) { 4445 mk_sense_info_tape(scp, NO_SENSE, NO_ADDITIONAL_SENSE, 4446 FILEMARK_DETECTED_ASCQ, fixed ? num - i : size, 4447 SENSE_FLAG_FILEMARK); 4448 scsi_set_resid(scp, (num - i) * size); 4449 return check_condition_result; 4450 } 4451 /* Assume no REW */ 4452 if (IS_TAPE_BLOCK_EOD(blp->fl_size)) { 4453 mk_sense_info_tape(scp, BLANK_CHECK, NO_ADDITIONAL_SENSE, 4454 EOD_DETECTED_ASCQ, fixed ? num - i : size, 4455 0); 4456 devip->tape_location[partition] = pos; 4457 scsi_set_resid(scp, (num - i) * size); 4458 return check_condition_result; 4459 } 4460 sg_zero_buffer(sdb->table.sgl, sdb->table.nents, 4461 size, i * size); 4462 sg_copy_buffer(sdb->table.sgl, sdb->table.nents, 4463 &(blp->data), 4, i * size, false); 4464 if (fixed) { 4465 if (blp->fl_size != devip->tape_blksize) { 4466 scsi_set_resid(scp, (num - i) * size); 4467 mk_sense_info_tape(scp, NO_SENSE, NO_ADDITIONAL_SENSE, 4468 0, num - i, 4469 SENSE_FLAG_ILI); 4470 return check_condition_result; 4471 } 4472 } else { 4473 if (blp->fl_size != size) { 4474 if (blp->fl_size < size) 4475 scsi_set_resid(scp, size - blp->fl_size); 4476 if (!sili) { 4477 mk_sense_info_tape(scp, NO_SENSE, NO_ADDITIONAL_SENSE, 4478 0, size - blp->fl_size, 4479 SENSE_FLAG_ILI); 4480 return check_condition_result; 4481 } 4482 } 4483 } 4484 } 4485 if (pos >= devip->tape_eop[partition]) { 4486 mk_sense_info_tape(scp, NO_SENSE, NO_ADDITIONAL_SENSE, 4487 EOP_EOM_DETECTED_ASCQ, fixed ? num - i : size, 4488 SENSE_FLAG_EOM); 4489 devip->tape_location[partition] = pos - 1; 4490 return check_condition_result; 4491 } 4492 devip->tape_location[partition] = pos; 4493 4494 return 0; 4495 } 4496 4497 static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) 4498 { 4499 bool check_prot; 4500 u32 num; 4501 u32 ei_lba; 4502 int ret; 4503 u64 lba; 4504 struct sdeb_store_info *sip = devip2sip(devip, true); 4505 u8 *cmd = scp->cmnd; 4506 bool meta_data_locked = false; 4507 4508 switch (cmd[0]) { 4509 case READ_16: 4510 ei_lba = 0; 4511 lba = get_unaligned_be64(cmd + 2); 4512 num = get_unaligned_be32(cmd + 10); 4513 check_prot = true; 4514 break; 4515 case READ_10: 4516 ei_lba = 0; 4517 lba = get_unaligned_be32(cmd + 2); 4518 num = get_unaligned_be16(cmd + 7); 4519 check_prot = true; 4520 break; 4521 case READ_6: 4522 ei_lba = 0; 4523 lba = (u32)cmd[3] | (u32)cmd[2] << 8 | 4524 (u32)(cmd[1] & 0x1f) << 16; 4525 num = (0 == cmd[4]) ? 256 : cmd[4]; 4526 check_prot = true; 4527 break; 4528 case READ_12: 4529 ei_lba = 0; 4530 lba = get_unaligned_be32(cmd + 2); 4531 num = get_unaligned_be32(cmd + 6); 4532 check_prot = true; 4533 break; 4534 case XDWRITEREAD_10: 4535 ei_lba = 0; 4536 lba = get_unaligned_be32(cmd + 2); 4537 num = get_unaligned_be16(cmd + 7); 4538 check_prot = false; 4539 break; 4540 default: /* assume READ(32) */ 4541 lba = get_unaligned_be64(cmd + 12); 4542 ei_lba = get_unaligned_be32(cmd + 20); 4543 num = get_unaligned_be32(cmd + 28); 4544 check_prot = false; 4545 break; 4546 } 4547 if (unlikely(have_dif_prot && check_prot)) { 4548 if (sdebug_dif == T10_PI_TYPE2_PROTECTION && 4549 (cmd[1] & 0xe0)) { 4550 mk_sense_invalid_opcode(scp); 4551 return check_condition_result; 4552 } 4553 if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || 4554 sdebug_dif == T10_PI_TYPE3_PROTECTION) && 4555 (cmd[1] & 0xe0) == 0) 4556 sdev_printk(KERN_ERR, scp->device, "Unprotected RD " 4557 "to DIF device\n"); 4558 } 4559 if (unlikely((sdebug_opts & SDEBUG_OPT_SHORT_TRANSFER) && 4560 atomic_read(&sdeb_inject_pending))) { 4561 num /= 2; 4562 atomic_set(&sdeb_inject_pending, 0); 4563 } 4564 4565 /* 4566 * When checking device access params, for reads we only check data 4567 * versus what is set at init time, so no need to lock. 4568 */ 4569 ret = check_device_access_params(scp, lba, num, false); 4570 if (ret) 4571 return ret; 4572 if (unlikely((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) && 4573 (lba <= (sdebug_medium_error_start + sdebug_medium_error_count - 1)) && 4574 ((lba + num) > sdebug_medium_error_start))) { 4575 /* claim unrecoverable read error */ 4576 mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0); 4577 /* set info field and valid bit for fixed descriptor */ 4578 if (0x70 == (scp->sense_buffer[0] & 0x7f)) { 4579 scp->sense_buffer[0] |= 0x80; /* Valid bit */ 4580 ret = (lba < OPT_MEDIUM_ERR_ADDR) 4581 ? OPT_MEDIUM_ERR_ADDR : (int)lba; 4582 put_unaligned_be32(ret, scp->sense_buffer + 3); 4583 } 4584 scsi_set_resid(scp, scsi_bufflen(scp)); 4585 return check_condition_result; 4586 } 4587 4588 if (sdebug_dev_is_zoned(devip) || 4589 (sdebug_dix && scsi_prot_sg_count(scp))) { 4590 sdeb_meta_read_lock(sip); 4591 meta_data_locked = true; 4592 } 4593 4594 /* DIX + T10 DIF */ 4595 if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { 4596 switch (prot_verify_read(scp, lba, num, ei_lba)) { 4597 case 1: /* Guard tag error */ 4598 if (cmd[1] >> 5 != 3) { /* RDPROTECT != 3 */ 4599 sdeb_meta_read_unlock(sip); 4600 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); 4601 return check_condition_result; 4602 } else if (scp->prot_flags & SCSI_PROT_GUARD_CHECK) { 4603 sdeb_meta_read_unlock(sip); 4604 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); 4605 return illegal_condition_result; 4606 } 4607 break; 4608 case 3: /* Reference tag error */ 4609 if (cmd[1] >> 5 != 3) { /* RDPROTECT != 3 */ 4610 sdeb_meta_read_unlock(sip); 4611 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 3); 4612 return check_condition_result; 4613 } else if (scp->prot_flags & SCSI_PROT_REF_CHECK) { 4614 sdeb_meta_read_unlock(sip); 4615 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 3); 4616 return illegal_condition_result; 4617 } 4618 break; 4619 } 4620 } 4621 4622 ret = do_device_access(sip, scp, 0, lba, num, 0, false, false); 4623 if (meta_data_locked) 4624 sdeb_meta_read_unlock(sip); 4625 if (unlikely(ret == -1)) 4626 return DID_ERROR << 16; 4627 4628 scsi_set_resid(scp, scsi_bufflen(scp) - ret); 4629 4630 if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) && 4631 atomic_read(&sdeb_inject_pending))) { 4632 if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) { 4633 mk_sense_buffer(scp, RECOVERED_ERROR, THRESHOLD_EXCEEDED, 0); 4634 atomic_set(&sdeb_inject_pending, 0); 4635 return check_condition_result; 4636 } else if (sdebug_opts & SDEBUG_OPT_DIF_ERR) { 4637 /* Logical block guard check failed */ 4638 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); 4639 atomic_set(&sdeb_inject_pending, 0); 4640 return illegal_condition_result; 4641 } else if (SDEBUG_OPT_DIX_ERR & sdebug_opts) { 4642 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); 4643 atomic_set(&sdeb_inject_pending, 0); 4644 return illegal_condition_result; 4645 } 4646 } 4647 return 0; 4648 } 4649 4650 static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec, 4651 unsigned int sectors, u32 ei_lba) 4652 { 4653 int ret; 4654 struct t10_pi_tuple *sdt; 4655 void *daddr; 4656 sector_t sector = start_sec; 4657 int ppage_offset; 4658 int dpage_offset; 4659 struct sg_mapping_iter diter; 4660 struct sg_mapping_iter piter; 4661 4662 BUG_ON(scsi_sg_count(SCpnt) == 0); 4663 BUG_ON(scsi_prot_sg_count(SCpnt) == 0); 4664 4665 sg_miter_start(&piter, scsi_prot_sglist(SCpnt), 4666 scsi_prot_sg_count(SCpnt), 4667 SG_MITER_ATOMIC | SG_MITER_FROM_SG); 4668 sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt), 4669 SG_MITER_ATOMIC | SG_MITER_FROM_SG); 4670 4671 /* For each protection page */ 4672 while (sg_miter_next(&piter)) { 4673 dpage_offset = 0; 4674 if (WARN_ON(!sg_miter_next(&diter))) { 4675 ret = 0x01; 4676 goto out; 4677 } 4678 4679 for (ppage_offset = 0; ppage_offset < piter.length; 4680 ppage_offset += sizeof(struct t10_pi_tuple)) { 4681 /* If we're at the end of the current 4682 * data page advance to the next one 4683 */ 4684 if (dpage_offset >= diter.length) { 4685 if (WARN_ON(!sg_miter_next(&diter))) { 4686 ret = 0x01; 4687 goto out; 4688 } 4689 dpage_offset = 0; 4690 } 4691 4692 sdt = piter.addr + ppage_offset; 4693 daddr = diter.addr + dpage_offset; 4694 4695 if (SCpnt->cmnd[1] >> 5 != 3) { /* WRPROTECT */ 4696 ret = dif_verify(sdt, daddr, sector, ei_lba); 4697 if (ret) 4698 goto out; 4699 } 4700 4701 sector++; 4702 ei_lba++; 4703 dpage_offset += sdebug_sector_size; 4704 } 4705 diter.consumed = dpage_offset; 4706 sg_miter_stop(&diter); 4707 } 4708 sg_miter_stop(&piter); 4709 4710 dif_copy_prot(SCpnt, start_sec, sectors, false); 4711 dix_writes++; 4712 4713 return 0; 4714 4715 out: 4716 dif_errors++; 4717 sg_miter_stop(&diter); 4718 sg_miter_stop(&piter); 4719 return ret; 4720 } 4721 4722 static unsigned long lba_to_map_index(sector_t lba) 4723 { 4724 if (sdebug_unmap_alignment) 4725 lba += sdebug_unmap_granularity - sdebug_unmap_alignment; 4726 sector_div(lba, sdebug_unmap_granularity); 4727 return lba; 4728 } 4729 4730 static sector_t map_index_to_lba(unsigned long index) 4731 { 4732 sector_t lba = index * sdebug_unmap_granularity; 4733 4734 if (sdebug_unmap_alignment) 4735 lba -= sdebug_unmap_granularity - sdebug_unmap_alignment; 4736 return lba; 4737 } 4738 4739 static unsigned int map_state(struct sdeb_store_info *sip, sector_t lba, 4740 unsigned int *num) 4741 { 4742 sector_t end; 4743 unsigned int mapped; 4744 unsigned long index; 4745 unsigned long next; 4746 4747 index = lba_to_map_index(lba); 4748 mapped = test_bit(index, sip->map_storep); 4749 4750 if (mapped) 4751 next = find_next_zero_bit(sip->map_storep, map_size, index); 4752 else 4753 next = find_next_bit(sip->map_storep, map_size, index); 4754 4755 end = min_t(sector_t, sdebug_store_sectors, map_index_to_lba(next)); 4756 *num = end - lba; 4757 return mapped; 4758 } 4759 4760 static void map_region(struct sdeb_store_info *sip, sector_t lba, 4761 unsigned int len) 4762 { 4763 sector_t end = lba + len; 4764 4765 while (lba < end) { 4766 unsigned long index = lba_to_map_index(lba); 4767 4768 if (index < map_size) 4769 set_bit(index, sip->map_storep); 4770 4771 lba = map_index_to_lba(index + 1); 4772 } 4773 } 4774 4775 static void unmap_region(struct sdeb_store_info *sip, sector_t lba, 4776 unsigned int len) 4777 { 4778 sector_t end = lba + len; 4779 u8 *fsp = sip->storep; 4780 4781 while (lba < end) { 4782 unsigned long index = lba_to_map_index(lba); 4783 4784 if (lba == map_index_to_lba(index) && 4785 lba + sdebug_unmap_granularity <= end && 4786 index < map_size) { 4787 clear_bit(index, sip->map_storep); 4788 if (sdebug_lbprz) { /* for LBPRZ=2 return 0xff_s */ 4789 memset(fsp + lba * sdebug_sector_size, 4790 (sdebug_lbprz & 1) ? 0 : 0xff, 4791 sdebug_sector_size * 4792 sdebug_unmap_granularity); 4793 } 4794 if (sip->dif_storep) { 4795 memset(sip->dif_storep + lba, 0xff, 4796 sizeof(*sip->dif_storep) * 4797 sdebug_unmap_granularity); 4798 } 4799 } 4800 lba = map_index_to_lba(index + 1); 4801 } 4802 } 4803 4804 static int resp_write_tape(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) 4805 { 4806 u32 i, num, transfer, size, written = 0; 4807 u8 *cmd = scp->cmnd; 4808 struct scsi_data_buffer *sdb = &scp->sdb; 4809 int partition = devip->tape_partition; 4810 int pos = devip->tape_location[partition]; 4811 struct tape_block *blp; 4812 bool fixed, ew; 4813 4814 if (cmd[0] != WRITE_6) { /* Only Write(6) supported */ 4815 mk_sense_invalid_opcode(scp); 4816 return illegal_condition_result; 4817 } 4818 4819 fixed = (cmd[1] & 1) != 0; 4820 transfer = get_unaligned_be24(cmd + 2); 4821 if (fixed) { 4822 num = transfer; 4823 size = devip->tape_blksize; 4824 } else { 4825 if (transfer < TAPE_MIN_BLKSIZE || 4826 transfer > TAPE_MAX_BLKSIZE) { 4827 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); 4828 return check_condition_result; 4829 } 4830 num = 1; 4831 size = transfer; 4832 } 4833 4834 scsi_set_resid(scp, num * transfer); 4835 for (i = 0, blp = devip->tape_blocks[partition] + pos, ew = false; 4836 i < num && pos < devip->tape_eop[partition] - 1; i++, pos++, blp++) { 4837 blp->fl_size = size; 4838 sg_copy_buffer(sdb->table.sgl, sdb->table.nents, 4839 &(blp->data), 4, i * size, true); 4840 written += size; 4841 scsi_set_resid(scp, num * transfer - written); 4842 ew |= (pos == devip->tape_eop[partition] - TAPE_EW); 4843 } 4844 4845 devip->tape_location[partition] = pos; 4846 blp->fl_size = TAPE_BLOCK_EOD_FLAG; 4847 if (pos >= devip->tape_eop[partition] - 1) { 4848 mk_sense_info_tape(scp, VOLUME_OVERFLOW, 4849 NO_ADDITIONAL_SENSE, EOP_EOM_DETECTED_ASCQ, 4850 fixed ? num - i : transfer, 4851 SENSE_FLAG_EOM); 4852 return check_condition_result; 4853 } 4854 if (ew) { /* early warning */ 4855 mk_sense_info_tape(scp, NO_SENSE, 4856 NO_ADDITIONAL_SENSE, EOP_EOM_DETECTED_ASCQ, 4857 fixed ? num - i : transfer, 4858 SENSE_FLAG_EOM); 4859 return check_condition_result; 4860 } 4861 4862 return 0; 4863 } 4864 4865 static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) 4866 { 4867 bool check_prot; 4868 u32 num; 4869 u8 group = 0; 4870 u32 ei_lba; 4871 int ret; 4872 u64 lba; 4873 struct sdeb_store_info *sip = devip2sip(devip, true); 4874 u8 *cmd = scp->cmnd; 4875 bool meta_data_locked = false; 4876 4877 if (unlikely(sdebug_opts & SDEBUG_OPT_UNALIGNED_WRITE && 4878 atomic_read(&sdeb_inject_pending))) { 4879 atomic_set(&sdeb_inject_pending, 0); 4880 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 4881 UNALIGNED_WRITE_ASCQ); 4882 return check_condition_result; 4883 } 4884 4885 switch (cmd[0]) { 4886 case WRITE_16: 4887 ei_lba = 0; 4888 lba = get_unaligned_be64(cmd + 2); 4889 num = get_unaligned_be32(cmd + 10); 4890 group = cmd[14] & 0x3f; 4891 check_prot = true; 4892 break; 4893 case WRITE_10: 4894 ei_lba = 0; 4895 lba = get_unaligned_be32(cmd + 2); 4896 group = cmd[6] & 0x3f; 4897 num = get_unaligned_be16(cmd + 7); 4898 check_prot = true; 4899 break; 4900 case WRITE_6: 4901 ei_lba = 0; 4902 lba = (u32)cmd[3] | (u32)cmd[2] << 8 | 4903 (u32)(cmd[1] & 0x1f) << 16; 4904 num = (0 == cmd[4]) ? 256 : cmd[4]; 4905 check_prot = true; 4906 break; 4907 case WRITE_12: 4908 ei_lba = 0; 4909 lba = get_unaligned_be32(cmd + 2); 4910 num = get_unaligned_be32(cmd + 6); 4911 group = cmd[6] & 0x3f; 4912 check_prot = true; 4913 break; 4914 case 0x53: /* XDWRITEREAD(10) */ 4915 ei_lba = 0; 4916 lba = get_unaligned_be32(cmd + 2); 4917 group = cmd[6] & 0x1f; 4918 num = get_unaligned_be16(cmd + 7); 4919 check_prot = false; 4920 break; 4921 default: /* assume WRITE(32) */ 4922 group = cmd[6] & 0x3f; 4923 lba = get_unaligned_be64(cmd + 12); 4924 ei_lba = get_unaligned_be32(cmd + 20); 4925 num = get_unaligned_be32(cmd + 28); 4926 check_prot = false; 4927 break; 4928 } 4929 if (unlikely(have_dif_prot && check_prot)) { 4930 if (sdebug_dif == T10_PI_TYPE2_PROTECTION && 4931 (cmd[1] & 0xe0)) { 4932 mk_sense_invalid_opcode(scp); 4933 return check_condition_result; 4934 } 4935 if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || 4936 sdebug_dif == T10_PI_TYPE3_PROTECTION) && 4937 (cmd[1] & 0xe0) == 0) 4938 sdev_printk(KERN_ERR, scp->device, "Unprotected WR " 4939 "to DIF device\n"); 4940 } 4941 4942 if (sdebug_dev_is_zoned(devip) || 4943 (sdebug_dix && scsi_prot_sg_count(scp)) || 4944 scsi_debug_lbp()) { 4945 sdeb_meta_write_lock(sip); 4946 meta_data_locked = true; 4947 } 4948 4949 ret = check_device_access_params(scp, lba, num, true); 4950 if (ret) { 4951 if (meta_data_locked) 4952 sdeb_meta_write_unlock(sip); 4953 return ret; 4954 } 4955 4956 /* DIX + T10 DIF */ 4957 if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { 4958 switch (prot_verify_write(scp, lba, num, ei_lba)) { 4959 case 1: /* Guard tag error */ 4960 if (scp->prot_flags & SCSI_PROT_GUARD_CHECK) { 4961 sdeb_meta_write_unlock(sip); 4962 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); 4963 return illegal_condition_result; 4964 } else if (scp->cmnd[1] >> 5 != 3) { /* WRPROTECT != 3 */ 4965 sdeb_meta_write_unlock(sip); 4966 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); 4967 return check_condition_result; 4968 } 4969 break; 4970 case 3: /* Reference tag error */ 4971 if (scp->prot_flags & SCSI_PROT_REF_CHECK) { 4972 sdeb_meta_write_unlock(sip); 4973 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 3); 4974 return illegal_condition_result; 4975 } else if (scp->cmnd[1] >> 5 != 3) { /* WRPROTECT != 3 */ 4976 sdeb_meta_write_unlock(sip); 4977 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 3); 4978 return check_condition_result; 4979 } 4980 break; 4981 } 4982 } 4983 4984 ret = do_device_access(sip, scp, 0, lba, num, group, true, false); 4985 if (unlikely(scsi_debug_lbp())) 4986 map_region(sip, lba, num); 4987 4988 /* If ZBC zone then bump its write pointer */ 4989 if (sdebug_dev_is_zoned(devip)) 4990 zbc_inc_wp(devip, lba, num); 4991 if (meta_data_locked) 4992 sdeb_meta_write_unlock(sip); 4993 4994 if (unlikely(-1 == ret)) 4995 return DID_ERROR << 16; 4996 else if (unlikely(sdebug_verbose && 4997 (ret < (num * sdebug_sector_size)))) 4998 sdev_printk(KERN_INFO, scp->device, 4999 "%s: write: cdb indicated=%u, IO sent=%d bytes\n", 5000 my_name, num * sdebug_sector_size, ret); 5001 5002 if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) && 5003 atomic_read(&sdeb_inject_pending))) { 5004 if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) { 5005 mk_sense_buffer(scp, RECOVERED_ERROR, THRESHOLD_EXCEEDED, 0); 5006 atomic_set(&sdeb_inject_pending, 0); 5007 return check_condition_result; 5008 } else if (sdebug_opts & SDEBUG_OPT_DIF_ERR) { 5009 /* Logical block guard check failed */ 5010 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); 5011 atomic_set(&sdeb_inject_pending, 0); 5012 return illegal_condition_result; 5013 } else if (sdebug_opts & SDEBUG_OPT_DIX_ERR) { 5014 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); 5015 atomic_set(&sdeb_inject_pending, 0); 5016 return illegal_condition_result; 5017 } 5018 } 5019 return 0; 5020 } 5021 5022 /* 5023 * T10 has only specified WRITE SCATTERED(16) and WRITE SCATTERED(32). 5024 * No READ GATHERED yet (requires bidi or long cdb holding gather list). 5025 */ 5026 static int resp_write_scat(struct scsi_cmnd *scp, 5027 struct sdebug_dev_info *devip) 5028 { 5029 u8 *cmd = scp->cmnd; 5030 u8 *lrdp = NULL; 5031 u8 *up; 5032 struct sdeb_store_info *sip = devip2sip(devip, true); 5033 u8 wrprotect; 5034 u16 lbdof, num_lrd, k; 5035 u32 num, num_by, bt_len, lbdof_blen, sg_off, cum_lb; 5036 u32 lb_size = sdebug_sector_size; 5037 u32 ei_lba; 5038 u64 lba; 5039 u8 group; 5040 int ret, res; 5041 bool is_16; 5042 static const u32 lrd_size = 32; /* + parameter list header size */ 5043 5044 if (cmd[0] == VARIABLE_LENGTH_CMD) { 5045 is_16 = false; 5046 group = cmd[6] & 0x3f; 5047 wrprotect = (cmd[10] >> 5) & 0x7; 5048 lbdof = get_unaligned_be16(cmd + 12); 5049 num_lrd = get_unaligned_be16(cmd + 16); 5050 bt_len = get_unaligned_be32(cmd + 28); 5051 } else { /* that leaves WRITE SCATTERED(16) */ 5052 is_16 = true; 5053 wrprotect = (cmd[2] >> 5) & 0x7; 5054 lbdof = get_unaligned_be16(cmd + 4); 5055 num_lrd = get_unaligned_be16(cmd + 8); 5056 bt_len = get_unaligned_be32(cmd + 10); 5057 group = cmd[14] & 0x3f; 5058 if (unlikely(have_dif_prot)) { 5059 if (sdebug_dif == T10_PI_TYPE2_PROTECTION && 5060 wrprotect) { 5061 mk_sense_invalid_opcode(scp); 5062 return illegal_condition_result; 5063 } 5064 if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || 5065 sdebug_dif == T10_PI_TYPE3_PROTECTION) && 5066 wrprotect == 0) 5067 sdev_printk(KERN_ERR, scp->device, 5068 "Unprotected WR to DIF device\n"); 5069 } 5070 } 5071 if ((num_lrd == 0) || (bt_len == 0)) 5072 return 0; /* T10 says these do-nothings are not errors */ 5073 if (lbdof == 0) { 5074 if (sdebug_verbose) 5075 sdev_printk(KERN_INFO, scp->device, 5076 "%s: LB Data Offset field bad\n", my_name); 5077 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); 5078 return illegal_condition_result; 5079 } 5080 lbdof_blen = lbdof * lb_size; 5081 if ((lrd_size + (num_lrd * lrd_size)) > lbdof_blen) { 5082 if (sdebug_verbose) 5083 sdev_printk(KERN_INFO, scp->device, 5084 "%s: LBA range descriptors don't fit\n", my_name); 5085 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); 5086 return illegal_condition_result; 5087 } 5088 lrdp = kzalloc(lbdof_blen, GFP_ATOMIC | __GFP_NOWARN); 5089 if (lrdp == NULL) 5090 return SCSI_MLQUEUE_HOST_BUSY; 5091 if (sdebug_verbose) 5092 sdev_printk(KERN_INFO, scp->device, 5093 "%s: Fetch header+scatter_list, lbdof_blen=%u\n", 5094 my_name, lbdof_blen); 5095 res = fetch_to_dev_buffer(scp, lrdp, lbdof_blen); 5096 if (res == -1) { 5097 ret = DID_ERROR << 16; 5098 goto err_out; 5099 } 5100 5101 /* Just keep it simple and always lock for now */ 5102 sdeb_meta_write_lock(sip); 5103 sg_off = lbdof_blen; 5104 /* Spec says Buffer xfer Length field in number of LBs in dout */ 5105 cum_lb = 0; 5106 for (k = 0, up = lrdp + lrd_size; k < num_lrd; ++k, up += lrd_size) { 5107 lba = get_unaligned_be64(up + 0); 5108 num = get_unaligned_be32(up + 8); 5109 if (sdebug_verbose) 5110 sdev_printk(KERN_INFO, scp->device, 5111 "%s: k=%d LBA=0x%llx num=%u sg_off=%u\n", 5112 my_name, k, lba, num, sg_off); 5113 if (num == 0) 5114 continue; 5115 ret = check_device_access_params(scp, lba, num, true); 5116 if (ret) 5117 goto err_out_unlock; 5118 num_by = num * lb_size; 5119 ei_lba = is_16 ? 0 : get_unaligned_be32(up + 12); 5120 5121 if ((cum_lb + num) > bt_len) { 5122 if (sdebug_verbose) 5123 sdev_printk(KERN_INFO, scp->device, 5124 "%s: sum of blocks > data provided\n", 5125 my_name); 5126 mk_sense_buffer(scp, ILLEGAL_REQUEST, WRITE_ERROR_ASC, 5127 0); 5128 ret = illegal_condition_result; 5129 goto err_out_unlock; 5130 } 5131 5132 /* DIX + T10 DIF */ 5133 if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { 5134 int prot_ret = prot_verify_write(scp, lba, num, 5135 ei_lba); 5136 5137 if (prot_ret) { 5138 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 5139 prot_ret); 5140 ret = illegal_condition_result; 5141 goto err_out_unlock; 5142 } 5143 } 5144 5145 /* 5146 * Write ranges atomically to keep as close to pre-atomic 5147 * writes behaviour as possible. 5148 */ 5149 ret = do_device_access(sip, scp, sg_off, lba, num, group, true, true); 5150 /* If ZBC zone then bump its write pointer */ 5151 if (sdebug_dev_is_zoned(devip)) 5152 zbc_inc_wp(devip, lba, num); 5153 if (unlikely(scsi_debug_lbp())) 5154 map_region(sip, lba, num); 5155 if (unlikely(-1 == ret)) { 5156 ret = DID_ERROR << 16; 5157 goto err_out_unlock; 5158 } else if (unlikely(sdebug_verbose && (ret < num_by))) 5159 sdev_printk(KERN_INFO, scp->device, 5160 "%s: write: cdb indicated=%u, IO sent=%d bytes\n", 5161 my_name, num_by, ret); 5162 5163 if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) && 5164 atomic_read(&sdeb_inject_pending))) { 5165 if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) { 5166 mk_sense_buffer(scp, RECOVERED_ERROR, THRESHOLD_EXCEEDED, 0); 5167 atomic_set(&sdeb_inject_pending, 0); 5168 ret = check_condition_result; 5169 goto err_out_unlock; 5170 } else if (sdebug_opts & SDEBUG_OPT_DIF_ERR) { 5171 /* Logical block guard check failed */ 5172 mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); 5173 atomic_set(&sdeb_inject_pending, 0); 5174 ret = illegal_condition_result; 5175 goto err_out_unlock; 5176 } else if (sdebug_opts & SDEBUG_OPT_DIX_ERR) { 5177 mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); 5178 atomic_set(&sdeb_inject_pending, 0); 5179 ret = illegal_condition_result; 5180 goto err_out_unlock; 5181 } 5182 } 5183 sg_off += num_by; 5184 cum_lb += num; 5185 } 5186 ret = 0; 5187 err_out_unlock: 5188 sdeb_meta_write_unlock(sip); 5189 err_out: 5190 kfree(lrdp); 5191 return ret; 5192 } 5193 5194 static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num, 5195 u32 ei_lba, bool unmap, bool ndob) 5196 { 5197 struct scsi_device *sdp = scp->device; 5198 struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata; 5199 unsigned long long i; 5200 u64 block, lbaa; 5201 u32 lb_size = sdebug_sector_size; 5202 int ret; 5203 struct sdeb_store_info *sip = devip2sip((struct sdebug_dev_info *) 5204 scp->device->hostdata, true); 5205 u8 *fs1p; 5206 u8 *fsp; 5207 bool meta_data_locked = false; 5208 5209 if (sdebug_dev_is_zoned(devip) || scsi_debug_lbp()) { 5210 sdeb_meta_write_lock(sip); 5211 meta_data_locked = true; 5212 } 5213 5214 ret = check_device_access_params(scp, lba, num, true); 5215 if (ret) 5216 goto out; 5217 5218 if (unmap && scsi_debug_lbp()) { 5219 unmap_region(sip, lba, num); 5220 goto out; 5221 } 5222 lbaa = lba; 5223 block = do_div(lbaa, sdebug_store_sectors); 5224 /* if ndob then zero 1 logical block, else fetch 1 logical block */ 5225 fsp = sip->storep; 5226 fs1p = fsp + (block * lb_size); 5227 sdeb_data_write_lock(sip); 5228 if (ndob) { 5229 memset(fs1p, 0, lb_size); 5230 ret = 0; 5231 } else 5232 ret = fetch_to_dev_buffer(scp, fs1p, lb_size); 5233 5234 if (-1 == ret) { 5235 ret = DID_ERROR << 16; 5236 goto out; 5237 } else if (sdebug_verbose && !ndob && (ret < lb_size)) 5238 sdev_printk(KERN_INFO, scp->device, 5239 "%s: %s: lb size=%u, IO sent=%d bytes\n", 5240 my_name, "write same", lb_size, ret); 5241 5242 /* Copy first sector to remaining blocks */ 5243 for (i = 1 ; i < num ; i++) { 5244 lbaa = lba + i; 5245 block = do_div(lbaa, sdebug_store_sectors); 5246 memmove(fsp + (block * lb_size), fs1p, lb_size); 5247 } 5248 if (scsi_debug_lbp()) 5249 map_region(sip, lba, num); 5250 /* If ZBC zone then bump its write pointer */ 5251 if (sdebug_dev_is_zoned(devip)) 5252 zbc_inc_wp(devip, lba, num); 5253 sdeb_data_write_unlock(sip); 5254 ret = 0; 5255 out: 5256 if (meta_data_locked) 5257 sdeb_meta_write_unlock(sip); 5258 return ret; 5259 } 5260 5261 static int resp_write_same_10(struct scsi_cmnd *scp, 5262 struct sdebug_dev_info *devip) 5263 { 5264 u8 *cmd = scp->cmnd; 5265 u32 lba; 5266 u16 num; 5267 u32 ei_lba = 0; 5268 bool unmap = false; 5269 5270 if (cmd[1] & 0x8) { 5271 if (sdebug_lbpws10 == 0) { 5272 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); 5273 return check_condition_result; 5274 } else 5275 unmap = true; 5276 } 5277 lba = get_unaligned_be32(cmd + 2); 5278 num = get_unaligned_be16(cmd + 7); 5279 if (num > sdebug_write_same_length) { 5280 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); 5281 return check_condition_result; 5282 } 5283 return resp_write_same(scp, lba, num, ei_lba, unmap, false); 5284 } 5285 5286 static int resp_write_same_16(struct scsi_cmnd *scp, 5287 struct sdebug_dev_info *devip) 5288 { 5289 u8 *cmd = scp->cmnd; 5290 u64 lba; 5291 u32 num; 5292 u32 ei_lba = 0; 5293 bool unmap = false; 5294 bool ndob = false; 5295 5296 if (cmd[1] & 0x8) { /* UNMAP */ 5297 if (sdebug_lbpws == 0) { 5298 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); 5299 return check_condition_result; 5300 } else 5301 unmap = true; 5302 } 5303 if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */ 5304 ndob = true; 5305 lba = get_unaligned_be64(cmd + 2); 5306 num = get_unaligned_be32(cmd + 10); 5307 if (num > sdebug_write_same_length) { 5308 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1); 5309 return check_condition_result; 5310 } 5311 return resp_write_same(scp, lba, num, ei_lba, unmap, ndob); 5312 } 5313 5314 /* Note the mode field is in the same position as the (lower) service action 5315 * field. For the Report supported operation codes command, SPC-4 suggests 5316 * each mode of this command should be reported separately; for future. */ 5317 static int resp_write_buffer(struct scsi_cmnd *scp, 5318 struct sdebug_dev_info *devip) 5319 { 5320 u8 *cmd = scp->cmnd; 5321 struct scsi_device *sdp = scp->device; 5322 struct sdebug_dev_info *dp; 5323 u8 mode; 5324 5325 mode = cmd[1] & 0x1f; 5326 switch (mode) { 5327 case 0x4: /* download microcode (MC) and activate (ACT) */ 5328 /* set UAs on this device only */ 5329 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); 5330 set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm); 5331 break; 5332 case 0x5: /* download MC, save and ACT */ 5333 set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm); 5334 break; 5335 case 0x6: /* download MC with offsets and ACT */ 5336 /* set UAs on most devices (LUs) in this target */ 5337 list_for_each_entry(dp, 5338 &devip->sdbg_host->dev_info_list, 5339 dev_list) 5340 if (dp->target == sdp->id) { 5341 set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm); 5342 if (devip != dp) 5343 set_bit(SDEBUG_UA_MICROCODE_CHANGED, 5344 dp->uas_bm); 5345 } 5346 break; 5347 case 0x7: /* download MC with offsets, save, and ACT */ 5348 /* set UA on all devices (LUs) in this target */ 5349 list_for_each_entry(dp, 5350 &devip->sdbg_host->dev_info_list, 5351 dev_list) 5352 if (dp->target == sdp->id) 5353 set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, 5354 dp->uas_bm); 5355 break; 5356 default: 5357 /* do nothing for this command for other mode values */ 5358 break; 5359 } 5360 return 0; 5361 } 5362 5363 static int resp_comp_write(struct scsi_cmnd *scp, 5364 struct sdebug_dev_info *devip) 5365 { 5366 u8 *cmd = scp->cmnd; 5367 u8 *arr; 5368 struct sdeb_store_info *sip = devip2sip(devip, true); 5369 u64 lba; 5370 u32 dnum; 5371 u32 lb_size = sdebug_sector_size; 5372 u8 num; 5373 int ret; 5374 int retval = 0; 5375 5376 lba = get_unaligned_be64(cmd + 2); 5377 num = cmd[13]; /* 1 to a maximum of 255 logical blocks */ 5378 if (0 == num) 5379 return 0; /* degenerate case, not an error */ 5380 if (sdebug_dif == T10_PI_TYPE2_PROTECTION && 5381 (cmd[1] & 0xe0)) { 5382 mk_sense_invalid_opcode(scp); 5383 return check_condition_result; 5384 } 5385 if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || 5386 sdebug_dif == T10_PI_TYPE3_PROTECTION) && 5387 (cmd[1] & 0xe0) == 0) 5388 sdev_printk(KERN_ERR, scp->device, "Unprotected WR " 5389 "to DIF device\n"); 5390 ret = check_device_access_params(scp, lba, num, false); 5391 if (ret) 5392 return ret; 5393 dnum = 2 * num; 5394 arr = kcalloc(lb_size, dnum, GFP_ATOMIC); 5395 if (NULL == arr) { 5396 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, 5397 INSUFF_RES_ASCQ); 5398 return check_condition_result; 5399 } 5400 5401 ret = do_dout_fetch(scp, dnum, arr); 5402 if (ret == -1) { 5403 retval = DID_ERROR << 16; 5404 goto cleanup_free; 5405 } else if (sdebug_verbose && (ret < (dnum * lb_size))) 5406 sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb " 5407 "indicated=%u, IO sent=%d bytes\n", my_name, 5408 dnum * lb_size, ret); 5409 5410 sdeb_data_write_lock(sip); 5411 sdeb_meta_write_lock(sip); 5412 if (!comp_write_worker(sip, lba, num, arr, false)) { 5413 mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0); 5414 retval = check_condition_result; 5415 goto cleanup_unlock; 5416 } 5417 5418 /* Cover sip->map_storep (which map_region()) sets with data lock */ 5419 if (scsi_debug_lbp()) 5420 map_region(sip, lba, num); 5421 cleanup_unlock: 5422 sdeb_meta_write_unlock(sip); 5423 sdeb_data_write_unlock(sip); 5424 cleanup_free: 5425 kfree(arr); 5426 return retval; 5427 } 5428 5429 struct unmap_block_desc { 5430 __be64 lba; 5431 __be32 blocks; 5432 __be32 __reserved; 5433 }; 5434 5435 static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) 5436 { 5437 unsigned char *buf; 5438 struct unmap_block_desc *desc; 5439 struct sdeb_store_info *sip = devip2sip(devip, true); 5440 unsigned int i, payload_len, descriptors; 5441 int ret; 5442 5443 if (!scsi_debug_lbp()) 5444 return 0; /* fib and say its done */ 5445 payload_len = get_unaligned_be16(scp->cmnd + 7); 5446 BUG_ON(scsi_bufflen(scp) != payload_len); 5447 5448 descriptors = (payload_len - 8) / 16; 5449 if (descriptors > sdebug_unmap_max_desc) { 5450 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); 5451 return check_condition_result; 5452 } 5453 5454 buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC); 5455 if (!buf) { 5456 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, 5457 INSUFF_RES_ASCQ); 5458 return check_condition_result; 5459 } 5460 5461 scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp)); 5462 5463 BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2); 5464 BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16); 5465 5466 desc = (void *)&buf[8]; 5467 5468 sdeb_meta_write_lock(sip); 5469 5470 for (i = 0 ; i < descriptors ; i++) { 5471 unsigned long long lba = get_unaligned_be64(&desc[i].lba); 5472 unsigned int num = get_unaligned_be32(&desc[i].blocks); 5473 5474 ret = check_device_access_params(scp, lba, num, true); 5475 if (ret) 5476 goto out; 5477 5478 unmap_region(sip, lba, num); 5479 } 5480 5481 ret = 0; 5482 5483 out: 5484 sdeb_meta_write_unlock(sip); 5485 kfree(buf); 5486 5487 return ret; 5488 } 5489 5490 #define SDEBUG_GET_LBA_STATUS_LEN 32 5491 5492 static int resp_get_lba_status(struct scsi_cmnd *scp, 5493 struct sdebug_dev_info *devip) 5494 { 5495 u8 *cmd = scp->cmnd; 5496 u64 lba; 5497 u32 alloc_len, mapped, num; 5498 int ret; 5499 u8 arr[SDEBUG_GET_LBA_STATUS_LEN]; 5500 5501 lba = get_unaligned_be64(cmd + 2); 5502 alloc_len = get_unaligned_be32(cmd + 10); 5503 5504 if (alloc_len < 24) 5505 return 0; 5506 5507 ret = check_device_access_params(scp, lba, 1, false); 5508 if (ret) 5509 return ret; 5510 5511 if (scsi_debug_lbp()) { 5512 struct sdeb_store_info *sip = devip2sip(devip, true); 5513 5514 mapped = map_state(sip, lba, &num); 5515 } else { 5516 mapped = 1; 5517 /* following just in case virtual_gb changed */ 5518 sdebug_capacity = get_sdebug_capacity(); 5519 if (sdebug_capacity - lba <= 0xffffffff) 5520 num = sdebug_capacity - lba; 5521 else 5522 num = 0xffffffff; 5523 } 5524 5525 memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN); 5526 put_unaligned_be32(20, arr); /* Parameter Data Length */ 5527 put_unaligned_be64(lba, arr + 8); /* LBA */ 5528 put_unaligned_be32(num, arr + 16); /* Number of blocks */ 5529 arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */ 5530 5531 return fill_from_dev_buffer(scp, arr, SDEBUG_GET_LBA_STATUS_LEN); 5532 } 5533 5534 static int resp_get_stream_status(struct scsi_cmnd *scp, 5535 struct sdebug_dev_info *devip) 5536 { 5537 u16 starting_stream_id, stream_id; 5538 const u8 *cmd = scp->cmnd; 5539 u32 alloc_len, offset; 5540 u8 arr[256] = {}; 5541 struct scsi_stream_status_header *h = (void *)arr; 5542 5543 starting_stream_id = get_unaligned_be16(cmd + 4); 5544 alloc_len = get_unaligned_be32(cmd + 10); 5545 5546 if (alloc_len < 8) { 5547 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1); 5548 return check_condition_result; 5549 } 5550 5551 if (starting_stream_id >= MAXIMUM_NUMBER_OF_STREAMS) { 5552 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1); 5553 return check_condition_result; 5554 } 5555 5556 /* 5557 * The GET STREAM STATUS command only reports status information 5558 * about open streams. Treat the non-permanent stream as open. 5559 */ 5560 put_unaligned_be16(MAXIMUM_NUMBER_OF_STREAMS, 5561 &h->number_of_open_streams); 5562 5563 for (offset = 8, stream_id = starting_stream_id; 5564 offset + 8 <= min_t(u32, alloc_len, sizeof(arr)) && 5565 stream_id < MAXIMUM_NUMBER_OF_STREAMS; 5566 offset += 8, stream_id++) { 5567 struct scsi_stream_status *stream_status = (void *)arr + offset; 5568 5569 stream_status->perm = stream_id < PERMANENT_STREAM_COUNT; 5570 put_unaligned_be16(stream_id, 5571 &stream_status->stream_identifier); 5572 stream_status->rel_lifetime = stream_id + 1; 5573 } 5574 put_unaligned_be32(offset - 8, &h->len); /* PARAMETER DATA LENGTH */ 5575 5576 return fill_from_dev_buffer(scp, arr, min(offset, alloc_len)); 5577 } 5578 5579 static int resp_sync_cache(struct scsi_cmnd *scp, 5580 struct sdebug_dev_info *devip) 5581 { 5582 int res = 0; 5583 u64 lba; 5584 u32 num_blocks; 5585 u8 *cmd = scp->cmnd; 5586 5587 if (cmd[0] == SYNCHRONIZE_CACHE) { /* 10 byte cdb */ 5588 lba = get_unaligned_be32(cmd + 2); 5589 num_blocks = get_unaligned_be16(cmd + 7); 5590 } else { /* SYNCHRONIZE_CACHE(16) */ 5591 lba = get_unaligned_be64(cmd + 2); 5592 num_blocks = get_unaligned_be32(cmd + 10); 5593 } 5594 if (lba + num_blocks > sdebug_capacity) { 5595 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); 5596 return check_condition_result; 5597 } 5598 if (!write_since_sync || (cmd[1] & 0x2)) 5599 res = SDEG_RES_IMMED_MASK; 5600 else /* delay if write_since_sync and IMMED clear */ 5601 write_since_sync = false; 5602 return res; 5603 } 5604 5605 /* 5606 * Assuming the LBA+num_blocks is not out-of-range, this function will return 5607 * CONDITION MET if the specified blocks will/have fitted in the cache, and 5608 * a GOOD status otherwise. Model a disk with a big cache and yield 5609 * CONDITION MET. Actually tries to bring range in main memory into the 5610 * cache associated with the CPU(s). 5611 * 5612 * The pcode 0x34 is also used for READ POSITION by tape devices. 5613 */ 5614 static int resp_pre_fetch(struct scsi_cmnd *scp, 5615 struct sdebug_dev_info *devip) 5616 { 5617 int res = 0; 5618 u64 lba; 5619 u64 block, rest = 0; 5620 u32 nblks; 5621 u8 *cmd = scp->cmnd; 5622 struct sdeb_store_info *sip = devip2sip(devip, true); 5623 u8 *fsp = sip->storep; 5624 5625 if (cmd[0] == PRE_FETCH) { /* 10 byte cdb */ 5626 lba = get_unaligned_be32(cmd + 2); 5627 nblks = get_unaligned_be16(cmd + 7); 5628 } else { /* PRE-FETCH(16) */ 5629 lba = get_unaligned_be64(cmd + 2); 5630 nblks = get_unaligned_be32(cmd + 10); 5631 } 5632 if (lba + nblks > sdebug_capacity) { 5633 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); 5634 return check_condition_result; 5635 } 5636 if (!fsp) 5637 goto fini; 5638 /* PRE-FETCH spec says nothing about LBP or PI so skip them */ 5639 block = do_div(lba, sdebug_store_sectors); 5640 if (block + nblks > sdebug_store_sectors) 5641 rest = block + nblks - sdebug_store_sectors; 5642 5643 /* Try to bring the PRE-FETCH range into CPU's cache */ 5644 sdeb_data_read_lock(sip); 5645 prefetch_range(fsp + (sdebug_sector_size * block), 5646 (nblks - rest) * sdebug_sector_size); 5647 if (rest) 5648 prefetch_range(fsp, rest * sdebug_sector_size); 5649 5650 sdeb_data_read_unlock(sip); 5651 fini: 5652 if (cmd[1] & 0x2) 5653 res = SDEG_RES_IMMED_MASK; 5654 return res | condition_met_result; 5655 } 5656 5657 #define RL_BUCKET_ELEMS 8 5658 5659 /* Even though each pseudo target has a REPORT LUNS "well known logical unit" 5660 * (W-LUN), the normal Linux scanning logic does not associate it with a 5661 * device (e.g. /dev/sg7). The following magic will make that association: 5662 * "cd /sys/class/scsi_host/host<n> ; echo '- - 49409' > scan" 5663 * where <n> is a host number. If there are multiple targets in a host then 5664 * the above will associate a W-LUN to each target. To only get a W-LUN 5665 * for target 2, then use "echo '- 2 49409' > scan" . 5666 */ 5667 static int resp_report_luns(struct scsi_cmnd *scp, 5668 struct sdebug_dev_info *devip) 5669 { 5670 unsigned char *cmd = scp->cmnd; 5671 unsigned int alloc_len; 5672 unsigned char select_report; 5673 u64 lun; 5674 struct scsi_lun *lun_p; 5675 u8 arr[RL_BUCKET_ELEMS * sizeof(struct scsi_lun)]; 5676 unsigned int lun_cnt; /* normal LUN count (max: 256) */ 5677 unsigned int wlun_cnt; /* report luns W-LUN count */ 5678 unsigned int tlun_cnt; /* total LUN count */ 5679 unsigned int rlen; /* response length (in bytes) */ 5680 int k, j, n, res; 5681 unsigned int off_rsp = 0; 5682 const int sz_lun = sizeof(struct scsi_lun); 5683 5684 clear_luns_changed_on_target(devip); 5685 5686 select_report = cmd[2]; 5687 alloc_len = get_unaligned_be32(cmd + 6); 5688 5689 if (alloc_len < 4) { 5690 pr_err("alloc len too small %d\n", alloc_len); 5691 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); 5692 return check_condition_result; 5693 } 5694 5695 switch (select_report) { 5696 case 0: /* all LUNs apart from W-LUNs */ 5697 lun_cnt = sdebug_max_luns; 5698 wlun_cnt = 0; 5699 break; 5700 case 1: /* only W-LUNs */ 5701 lun_cnt = 0; 5702 wlun_cnt = 1; 5703 break; 5704 case 2: /* all LUNs */ 5705 lun_cnt = sdebug_max_luns; 5706 wlun_cnt = 1; 5707 break; 5708 case 0x10: /* only administrative LUs */ 5709 case 0x11: /* see SPC-5 */ 5710 case 0x12: /* only subsiduary LUs owned by referenced LU */ 5711 default: 5712 pr_debug("select report invalid %d\n", select_report); 5713 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); 5714 return check_condition_result; 5715 } 5716 5717 if (sdebug_no_lun_0 && (lun_cnt > 0)) 5718 --lun_cnt; 5719 5720 tlun_cnt = lun_cnt + wlun_cnt; 5721 rlen = tlun_cnt * sz_lun; /* excluding 8 byte header */ 5722 scsi_set_resid(scp, scsi_bufflen(scp)); 5723 pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n", 5724 select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0); 5725 5726 /* loops rely on sizeof response header same as sizeof lun (both 8) */ 5727 lun = sdebug_no_lun_0 ? 1 : 0; 5728 for (k = 0, j = 0, res = 0; true; ++k, j = 0) { 5729 memset(arr, 0, sizeof(arr)); 5730 lun_p = (struct scsi_lun *)&arr[0]; 5731 if (k == 0) { 5732 put_unaligned_be32(rlen, &arr[0]); 5733 ++lun_p; 5734 j = 1; 5735 } 5736 for ( ; j < RL_BUCKET_ELEMS; ++j, ++lun_p) { 5737 if ((k * RL_BUCKET_ELEMS) + j > lun_cnt) 5738 break; 5739 int_to_scsilun(lun++, lun_p); 5740 if (lun > 1 && sdebug_lun_am == SAM_LUN_AM_FLAT) 5741 lun_p->scsi_lun[0] |= 0x40; 5742 } 5743 if (j < RL_BUCKET_ELEMS) 5744 break; 5745 n = j * sz_lun; 5746 res = p_fill_from_dev_buffer(scp, arr, n, off_rsp); 5747 if (res) 5748 return res; 5749 off_rsp += n; 5750 } 5751 if (wlun_cnt) { 5752 int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p); 5753 ++j; 5754 } 5755 if (j > 0) 5756 res = p_fill_from_dev_buffer(scp, arr, j * sz_lun, off_rsp); 5757 return res; 5758 } 5759 5760 static int resp_verify(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) 5761 { 5762 bool is_bytchk3 = false; 5763 u8 bytchk; 5764 int ret, j; 5765 u32 vnum, a_num, off; 5766 const u32 lb_size = sdebug_sector_size; 5767 u64 lba; 5768 u8 *arr; 5769 u8 *cmd = scp->cmnd; 5770 struct sdeb_store_info *sip = devip2sip(devip, true); 5771 5772 bytchk = (cmd[1] >> 1) & 0x3; 5773 if (bytchk == 0) { 5774 return 0; /* always claim internal verify okay */ 5775 } else if (bytchk == 2) { 5776 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2); 5777 return check_condition_result; 5778 } else if (bytchk == 3) { 5779 is_bytchk3 = true; /* 1 block sent, compared repeatedly */ 5780 } 5781 switch (cmd[0]) { 5782 case VERIFY_16: 5783 lba = get_unaligned_be64(cmd + 2); 5784 vnum = get_unaligned_be32(cmd + 10); 5785 break; 5786 case VERIFY: /* is VERIFY(10) */ 5787 lba = get_unaligned_be32(cmd + 2); 5788 vnum = get_unaligned_be16(cmd + 7); 5789 break; 5790 default: 5791 mk_sense_invalid_opcode(scp); 5792 return check_condition_result; 5793 } 5794 if (vnum == 0) 5795 return 0; /* not an error */ 5796 a_num = is_bytchk3 ? 1 : vnum; 5797 /* Treat following check like one for read (i.e. no write) access */ 5798 ret = check_device_access_params(scp, lba, a_num, false); 5799 if (ret) 5800 return ret; 5801 5802 arr = kcalloc(lb_size, vnum, GFP_ATOMIC | __GFP_NOWARN); 5803 if (!arr) { 5804 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, 5805 INSUFF_RES_ASCQ); 5806 return check_condition_result; 5807 } 5808 /* Not changing store, so only need read access */ 5809 sdeb_data_read_lock(sip); 5810 5811 ret = do_dout_fetch(scp, a_num, arr); 5812 if (ret == -1) { 5813 ret = DID_ERROR << 16; 5814 goto cleanup; 5815 } else if (sdebug_verbose && (ret < (a_num * lb_size))) { 5816 sdev_printk(KERN_INFO, scp->device, 5817 "%s: cdb indicated=%u, IO sent=%d bytes\n", 5818 my_name, a_num * lb_size, ret); 5819 } 5820 if (is_bytchk3) { 5821 for (j = 1, off = lb_size; j < vnum; ++j, off += lb_size) 5822 memcpy(arr + off, arr, lb_size); 5823 } 5824 ret = 0; 5825 if (!comp_write_worker(sip, lba, vnum, arr, true)) { 5826 mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0); 5827 ret = check_condition_result; 5828 goto cleanup; 5829 } 5830 cleanup: 5831 sdeb_data_read_unlock(sip); 5832 kfree(arr); 5833 return ret; 5834 } 5835 5836 #define RZONES_DESC_HD 64 5837 5838 /* Report zones depending on start LBA and reporting options */ 5839 static int resp_report_zones(struct scsi_cmnd *scp, 5840 struct sdebug_dev_info *devip) 5841 { 5842 unsigned int rep_max_zones, nrz = 0; 5843 int ret = 0; 5844 u32 alloc_len, rep_opts, rep_len; 5845 bool partial; 5846 u64 lba, zs_lba; 5847 u64 arr_len; 5848 u8 *arr = NULL, *desc; 5849 u8 *cmd = scp->cmnd; 5850 struct sdeb_zone_state *zsp = NULL; 5851 struct sdeb_store_info *sip = devip2sip(devip, false); 5852 5853 if (!sdebug_dev_is_zoned(devip)) { 5854 mk_sense_invalid_opcode(scp); 5855 return check_condition_result; 5856 } 5857 zs_lba = get_unaligned_be64(cmd + 2); 5858 alloc_len = get_unaligned_be32(cmd + 10); 5859 if (alloc_len == 0) 5860 return 0; /* not an error */ 5861 rep_opts = cmd[14] & 0x3f; 5862 partial = cmd[14] & 0x80; 5863 5864 if (zs_lba >= sdebug_capacity) { 5865 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); 5866 return check_condition_result; 5867 } 5868 5869 rep_max_zones = (ALIGN((u64)alloc_len, RZONES_DESC_HD) - RZONES_DESC_HD) >> 5870 ilog2(RZONES_DESC_HD); 5871 rep_max_zones = min_t(unsigned int, rep_max_zones, devip->nr_zones); 5872 arr_len = (u64)RZONES_DESC_HD * (rep_max_zones + 1); 5873 5874 arr = kzalloc(arr_len, GFP_ATOMIC | __GFP_NOWARN); 5875 if (!arr) { 5876 mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, 5877 INSUFF_RES_ASCQ); 5878 return check_condition_result; 5879 } 5880 5881 sdeb_meta_read_lock(sip); 5882 5883 desc = arr + 64; 5884 for (lba = zs_lba; lba < sdebug_capacity; 5885 lba = zsp->z_start + zsp->z_size) { 5886 if (WARN_ONCE(zbc_zone(devip, lba) == zsp, "lba = %llu\n", lba)) 5887 break; 5888 zsp = zbc_zone(devip, lba); 5889 switch (rep_opts) { 5890 case 0x00: 5891 /* All zones */ 5892 break; 5893 case 0x01: 5894 /* Empty zones */ 5895 if (zsp->z_cond != ZC1_EMPTY) 5896 continue; 5897 break; 5898 case 0x02: 5899 /* Implicit open zones */ 5900 if (zsp->z_cond != ZC2_IMPLICIT_OPEN) 5901 continue; 5902 break; 5903 case 0x03: 5904 /* Explicit open zones */ 5905 if (zsp->z_cond != ZC3_EXPLICIT_OPEN) 5906 continue; 5907 break; 5908 case 0x04: 5909 /* Closed zones */ 5910 if (zsp->z_cond != ZC4_CLOSED) 5911 continue; 5912 break; 5913 case 0x05: 5914 /* Full zones */ 5915 if (zsp->z_cond != ZC5_FULL) 5916 continue; 5917 break; 5918 case 0x06: 5919 case 0x07: 5920 case 0x10: 5921 /* 5922 * Read-only, offline, reset WP recommended are 5923 * not emulated: no zones to report; 5924 */ 5925 continue; 5926 case 0x11: 5927 /* non-seq-resource set */ 5928 if (!zsp->z_non_seq_resource) 5929 continue; 5930 break; 5931 case 0x3e: 5932 /* All zones except gap zones. */ 5933 if (zbc_zone_is_gap(zsp)) 5934 continue; 5935 break; 5936 case 0x3f: 5937 /* Not write pointer (conventional) zones */ 5938 if (zbc_zone_is_seq(zsp)) 5939 continue; 5940 break; 5941 default: 5942 mk_sense_buffer(scp, ILLEGAL_REQUEST, 5943 INVALID_FIELD_IN_CDB, 0); 5944 ret = check_condition_result; 5945 goto fini; 5946 } 5947 5948 if (nrz < rep_max_zones) { 5949 /* Fill zone descriptor */ 5950 desc[0] = zsp->z_type; 5951 desc[1] = zsp->z_cond << 4; 5952 if (zsp->z_non_seq_resource) 5953 desc[1] |= 1 << 1; 5954 put_unaligned_be64((u64)zsp->z_size, desc + 8); 5955 put_unaligned_be64((u64)zsp->z_start, desc + 16); 5956 put_unaligned_be64((u64)zsp->z_wp, desc + 24); 5957 desc += 64; 5958 } 5959 5960 if (partial && nrz >= rep_max_zones) 5961 break; 5962 5963 nrz++; 5964 } 5965 5966 /* Report header */ 5967 /* Zone list length. */ 5968 put_unaligned_be32(nrz * RZONES_DESC_HD, arr + 0); 5969 /* Maximum LBA */ 5970 put_unaligned_be64(sdebug_capacity - 1, arr + 8); 5971 /* Zone starting LBA granularity. */ 5972 if (devip->zcap < devip->zsize) 5973 put_unaligned_be64(devip->zsize, arr + 16); 5974 5975 rep_len = (unsigned long)desc - (unsigned long)arr; 5976 ret = fill_from_dev_buffer(scp, arr, min_t(u32, alloc_len, rep_len)); 5977 5978 fini: 5979 sdeb_meta_read_unlock(sip); 5980 kfree(arr); 5981 return ret; 5982 } 5983 5984 static int resp_atomic_write(struct scsi_cmnd *scp, 5985 struct sdebug_dev_info *devip) 5986 { 5987 struct sdeb_store_info *sip; 5988 u8 *cmd = scp->cmnd; 5989 u16 boundary, len; 5990 u64 lba, lba_tmp; 5991 int ret; 5992 5993 if (!scsi_debug_atomic_write()) { 5994 mk_sense_invalid_opcode(scp); 5995 return check_condition_result; 5996 } 5997 5998 sip = devip2sip(devip, true); 5999 6000 lba = get_unaligned_be64(cmd + 2); 6001 boundary = get_unaligned_be16(cmd + 10); 6002 len = get_unaligned_be16(cmd + 12); 6003 6004 lba_tmp = lba; 6005 if (sdebug_atomic_wr_align && 6006 do_div(lba_tmp, sdebug_atomic_wr_align)) { 6007 /* Does not meet alignment requirement */ 6008 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); 6009 return check_condition_result; 6010 } 6011 6012 if (sdebug_atomic_wr_gran && len % sdebug_atomic_wr_gran) { 6013 /* Does not meet alignment requirement */ 6014 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); 6015 return check_condition_result; 6016 } 6017 6018 if (boundary > 0) { 6019 if (boundary > sdebug_atomic_wr_max_bndry) { 6020 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 12, -1); 6021 return check_condition_result; 6022 } 6023 6024 if (len > sdebug_atomic_wr_max_length_bndry) { 6025 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 12, -1); 6026 return check_condition_result; 6027 } 6028 } else { 6029 if (len > sdebug_atomic_wr_max_length) { 6030 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 12, -1); 6031 return check_condition_result; 6032 } 6033 } 6034 6035 ret = do_device_access(sip, scp, 0, lba, len, 0, true, true); 6036 if (unlikely(ret == -1)) 6037 return DID_ERROR << 16; 6038 if (unlikely(ret != len * sdebug_sector_size)) 6039 return DID_ERROR << 16; 6040 return 0; 6041 } 6042 6043 /* Logic transplanted from tcmu-runner, file_zbc.c */ 6044 static void zbc_open_all(struct sdebug_dev_info *devip) 6045 { 6046 struct sdeb_zone_state *zsp = &devip->zstate[0]; 6047 unsigned int i; 6048 6049 for (i = 0; i < devip->nr_zones; i++, zsp++) { 6050 if (zsp->z_cond == ZC4_CLOSED) 6051 zbc_open_zone(devip, &devip->zstate[i], true); 6052 } 6053 } 6054 6055 static int resp_open_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) 6056 { 6057 int res = 0; 6058 u64 z_id; 6059 enum sdebug_z_cond zc; 6060 u8 *cmd = scp->cmnd; 6061 struct sdeb_zone_state *zsp; 6062 bool all = cmd[14] & 0x01; 6063 struct sdeb_store_info *sip = devip2sip(devip, false); 6064 6065 if (!sdebug_dev_is_zoned(devip)) { 6066 mk_sense_invalid_opcode(scp); 6067 return check_condition_result; 6068 } 6069 sdeb_meta_write_lock(sip); 6070 6071 if (all) { 6072 /* Check if all closed zones can be open */ 6073 if (devip->max_open && 6074 devip->nr_exp_open + devip->nr_closed > devip->max_open) { 6075 mk_sense_buffer(scp, DATA_PROTECT, INSUFF_RES_ASC, 6076 INSUFF_ZONE_ASCQ); 6077 res = check_condition_result; 6078 goto fini; 6079 } 6080 /* Open all closed zones */ 6081 zbc_open_all(devip); 6082 goto fini; 6083 } 6084 6085 /* Open the specified zone */ 6086 z_id = get_unaligned_be64(cmd + 2); 6087 if (z_id >= sdebug_capacity) { 6088 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); 6089 res = check_condition_result; 6090 goto fini; 6091 } 6092 6093 zsp = zbc_zone(devip, z_id); 6094 if (z_id != zsp->z_start) { 6095 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); 6096 res = check_condition_result; 6097 goto fini; 6098 } 6099 if (zbc_zone_is_conv(zsp)) { 6100 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); 6101 res = check_condition_result; 6102 goto fini; 6103 } 6104 6105 zc = zsp->z_cond; 6106 if (zc == ZC3_EXPLICIT_OPEN || zc == ZC5_FULL) 6107 goto fini; 6108 6109 if (devip->max_open && devip->nr_exp_open >= devip->max_open) { 6110 mk_sense_buffer(scp, DATA_PROTECT, INSUFF_RES_ASC, 6111 INSUFF_ZONE_ASCQ); 6112 res = check_condition_result; 6113 goto fini; 6114 } 6115 6116 zbc_open_zone(devip, zsp, true); 6117 fini: 6118 sdeb_meta_write_unlock(sip); 6119 return res; 6120 } 6121 6122 static void zbc_close_all(struct sdebug_dev_info *devip) 6123 { 6124 unsigned int i; 6125 6126 for (i = 0; i < devip->nr_zones; i++) 6127 zbc_close_zone(devip, &devip->zstate[i]); 6128 } 6129 6130 static int resp_close_zone(struct scsi_cmnd *scp, 6131 struct sdebug_dev_info *devip) 6132 { 6133 int res = 0; 6134 u64 z_id; 6135 u8 *cmd = scp->cmnd; 6136 struct sdeb_zone_state *zsp; 6137 bool all = cmd[14] & 0x01; 6138 struct sdeb_store_info *sip = devip2sip(devip, false); 6139 6140 if (!sdebug_dev_is_zoned(devip)) { 6141 mk_sense_invalid_opcode(scp); 6142 return check_condition_result; 6143 } 6144 6145 sdeb_meta_write_lock(sip); 6146 6147 if (all) { 6148 zbc_close_all(devip); 6149 goto fini; 6150 } 6151 6152 /* Close specified zone */ 6153 z_id = get_unaligned_be64(cmd + 2); 6154 if (z_id >= sdebug_capacity) { 6155 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); 6156 res = check_condition_result; 6157 goto fini; 6158 } 6159 6160 zsp = zbc_zone(devip, z_id); 6161 if (z_id != zsp->z_start) { 6162 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); 6163 res = check_condition_result; 6164 goto fini; 6165 } 6166 if (zbc_zone_is_conv(zsp)) { 6167 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); 6168 res = check_condition_result; 6169 goto fini; 6170 } 6171 6172 zbc_close_zone(devip, zsp); 6173 fini: 6174 sdeb_meta_write_unlock(sip); 6175 return res; 6176 } 6177 6178 static void zbc_finish_zone(struct sdebug_dev_info *devip, 6179 struct sdeb_zone_state *zsp, bool empty) 6180 { 6181 enum sdebug_z_cond zc = zsp->z_cond; 6182 6183 if (zc == ZC4_CLOSED || zc == ZC2_IMPLICIT_OPEN || 6184 zc == ZC3_EXPLICIT_OPEN || (empty && zc == ZC1_EMPTY)) { 6185 if (zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN) 6186 zbc_close_zone(devip, zsp); 6187 if (zsp->z_cond == ZC4_CLOSED) 6188 devip->nr_closed--; 6189 zsp->z_wp = zsp->z_start + zsp->z_size; 6190 zsp->z_cond = ZC5_FULL; 6191 } 6192 } 6193 6194 static void zbc_finish_all(struct sdebug_dev_info *devip) 6195 { 6196 unsigned int i; 6197 6198 for (i = 0; i < devip->nr_zones; i++) 6199 zbc_finish_zone(devip, &devip->zstate[i], false); 6200 } 6201 6202 static int resp_finish_zone(struct scsi_cmnd *scp, 6203 struct sdebug_dev_info *devip) 6204 { 6205 struct sdeb_zone_state *zsp; 6206 int res = 0; 6207 u64 z_id; 6208 u8 *cmd = scp->cmnd; 6209 bool all = cmd[14] & 0x01; 6210 struct sdeb_store_info *sip = devip2sip(devip, false); 6211 6212 if (!sdebug_dev_is_zoned(devip)) { 6213 mk_sense_invalid_opcode(scp); 6214 return check_condition_result; 6215 } 6216 6217 sdeb_meta_write_lock(sip); 6218 6219 if (all) { 6220 zbc_finish_all(devip); 6221 goto fini; 6222 } 6223 6224 /* Finish the specified zone */ 6225 z_id = get_unaligned_be64(cmd + 2); 6226 if (z_id >= sdebug_capacity) { 6227 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); 6228 res = check_condition_result; 6229 goto fini; 6230 } 6231 6232 zsp = zbc_zone(devip, z_id); 6233 if (z_id != zsp->z_start) { 6234 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); 6235 res = check_condition_result; 6236 goto fini; 6237 } 6238 if (zbc_zone_is_conv(zsp)) { 6239 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); 6240 res = check_condition_result; 6241 goto fini; 6242 } 6243 6244 zbc_finish_zone(devip, zsp, true); 6245 fini: 6246 sdeb_meta_write_unlock(sip); 6247 return res; 6248 } 6249 6250 static void zbc_rwp_zone(struct sdebug_dev_info *devip, 6251 struct sdeb_zone_state *zsp) 6252 { 6253 enum sdebug_z_cond zc; 6254 struct sdeb_store_info *sip = devip2sip(devip, false); 6255 6256 if (!zbc_zone_is_seq(zsp)) 6257 return; 6258 6259 zc = zsp->z_cond; 6260 if (zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN) 6261 zbc_close_zone(devip, zsp); 6262 6263 if (zsp->z_cond == ZC4_CLOSED) 6264 devip->nr_closed--; 6265 6266 if (zsp->z_wp > zsp->z_start) 6267 memset(sip->storep + zsp->z_start * sdebug_sector_size, 0, 6268 (zsp->z_wp - zsp->z_start) * sdebug_sector_size); 6269 6270 zsp->z_non_seq_resource = false; 6271 zsp->z_wp = zsp->z_start; 6272 zsp->z_cond = ZC1_EMPTY; 6273 } 6274 6275 static void zbc_rwp_all(struct sdebug_dev_info *devip) 6276 { 6277 unsigned int i; 6278 6279 for (i = 0; i < devip->nr_zones; i++) 6280 zbc_rwp_zone(devip, &devip->zstate[i]); 6281 } 6282 6283 static int resp_rwp_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) 6284 { 6285 struct sdeb_zone_state *zsp; 6286 int res = 0; 6287 u64 z_id; 6288 u8 *cmd = scp->cmnd; 6289 bool all = cmd[14] & 0x01; 6290 struct sdeb_store_info *sip = devip2sip(devip, false); 6291 6292 if (!sdebug_dev_is_zoned(devip)) { 6293 mk_sense_invalid_opcode(scp); 6294 return check_condition_result; 6295 } 6296 6297 sdeb_meta_write_lock(sip); 6298 6299 if (all) { 6300 zbc_rwp_all(devip); 6301 goto fini; 6302 } 6303 6304 z_id = get_unaligned_be64(cmd + 2); 6305 if (z_id >= sdebug_capacity) { 6306 mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); 6307 res = check_condition_result; 6308 goto fini; 6309 } 6310 6311 zsp = zbc_zone(devip, z_id); 6312 if (z_id != zsp->z_start) { 6313 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); 6314 res = check_condition_result; 6315 goto fini; 6316 } 6317 if (zbc_zone_is_conv(zsp)) { 6318 mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); 6319 res = check_condition_result; 6320 goto fini; 6321 } 6322 6323 zbc_rwp_zone(devip, zsp); 6324 fini: 6325 sdeb_meta_write_unlock(sip); 6326 return res; 6327 } 6328 6329 static u32 get_tag(struct scsi_cmnd *cmnd) 6330 { 6331 return blk_mq_unique_tag(scsi_cmd_to_rq(cmnd)); 6332 } 6333 6334 /* Queued (deferred) command completions converge here. */ 6335 static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp) 6336 { 6337 struct sdebug_scsi_cmd *sdsc = container_of(sd_dp, 6338 typeof(*sdsc), sd_dp); 6339 struct scsi_cmnd *scp = (struct scsi_cmnd *)sdsc - 1; 6340 unsigned long flags; 6341 bool aborted; 6342 6343 if (sdebug_statistics) { 6344 atomic_inc(&sdebug_completions); 6345 if (raw_smp_processor_id() != sd_dp->issuing_cpu) 6346 atomic_inc(&sdebug_miss_cpus); 6347 } 6348 6349 spin_lock_irqsave(&sdsc->lock, flags); 6350 aborted = sd_dp->aborted; 6351 if (unlikely(aborted)) 6352 sd_dp->aborted = false; 6353 6354 spin_unlock_irqrestore(&sdsc->lock, flags); 6355 6356 if (aborted) { 6357 pr_info("bypassing scsi_done() due to aborted cmd, kicking-off EH\n"); 6358 blk_abort_request(scsi_cmd_to_rq(scp)); 6359 return; 6360 } 6361 6362 scsi_done(scp); /* callback to mid level */ 6363 } 6364 6365 /* When high resolution timer goes off this function is called. */ 6366 static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer) 6367 { 6368 struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer, 6369 hrt); 6370 sdebug_q_cmd_complete(sd_dp); 6371 return HRTIMER_NORESTART; 6372 } 6373 6374 /* When work queue schedules work, it calls this function. */ 6375 static void sdebug_q_cmd_wq_complete(struct work_struct *work) 6376 { 6377 struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer, 6378 ew.work); 6379 sdebug_q_cmd_complete(sd_dp); 6380 } 6381 6382 static bool got_shared_uuid; 6383 static uuid_t shared_uuid; 6384 6385 static int sdebug_device_create_zones(struct sdebug_dev_info *devip) 6386 { 6387 struct sdeb_zone_state *zsp; 6388 sector_t capacity = get_sdebug_capacity(); 6389 sector_t conv_capacity; 6390 sector_t zstart = 0; 6391 unsigned int i; 6392 6393 /* 6394 * Set the zone size: if sdeb_zbc_zone_size_mb is not set, figure out 6395 * a zone size allowing for at least 4 zones on the device. Otherwise, 6396 * use the specified zone size checking that at least 2 zones can be 6397 * created for the device. 6398 */ 6399 if (!sdeb_zbc_zone_size_mb) { 6400 devip->zsize = (DEF_ZBC_ZONE_SIZE_MB * SZ_1M) 6401 >> ilog2(sdebug_sector_size); 6402 while (capacity < devip->zsize << 2 && devip->zsize >= 2) 6403 devip->zsize >>= 1; 6404 if (devip->zsize < 2) { 6405 pr_err("Device capacity too small\n"); 6406 return -EINVAL; 6407 } 6408 } else { 6409 if (!is_power_of_2(sdeb_zbc_zone_size_mb)) { 6410 pr_err("Zone size is not a power of 2\n"); 6411 return -EINVAL; 6412 } 6413 devip->zsize = (sdeb_zbc_zone_size_mb * SZ_1M) 6414 >> ilog2(sdebug_sector_size); 6415 if (devip->zsize >= capacity) { 6416 pr_err("Zone size too large for device capacity\n"); 6417 return -EINVAL; 6418 } 6419 } 6420 6421 devip->zsize_shift = ilog2(devip->zsize); 6422 devip->nr_zones = (capacity + devip->zsize - 1) >> devip->zsize_shift; 6423 6424 if (sdeb_zbc_zone_cap_mb == 0) { 6425 devip->zcap = devip->zsize; 6426 } else { 6427 devip->zcap = (sdeb_zbc_zone_cap_mb * SZ_1M) >> 6428 ilog2(sdebug_sector_size); 6429 if (devip->zcap > devip->zsize) { 6430 pr_err("Zone capacity too large\n"); 6431 return -EINVAL; 6432 } 6433 } 6434 6435 conv_capacity = (sector_t)sdeb_zbc_nr_conv << devip->zsize_shift; 6436 if (conv_capacity >= capacity) { 6437 pr_err("Number of conventional zones too large\n"); 6438 return -EINVAL; 6439 } 6440 devip->nr_conv_zones = sdeb_zbc_nr_conv; 6441 devip->nr_seq_zones = ALIGN(capacity - conv_capacity, devip->zsize) >> 6442 devip->zsize_shift; 6443 devip->nr_zones = devip->nr_conv_zones + devip->nr_seq_zones; 6444 6445 /* Add gap zones if zone capacity is smaller than the zone size */ 6446 if (devip->zcap < devip->zsize) 6447 devip->nr_zones += devip->nr_seq_zones; 6448 6449 if (devip->zoned) { 6450 /* zbc_max_open_zones can be 0, meaning "not reported" */ 6451 if (sdeb_zbc_max_open >= devip->nr_zones - 1) 6452 devip->max_open = (devip->nr_zones - 1) / 2; 6453 else 6454 devip->max_open = sdeb_zbc_max_open; 6455 } 6456 6457 devip->zstate = kzalloc_objs(struct sdeb_zone_state, devip->nr_zones); 6458 if (!devip->zstate) 6459 return -ENOMEM; 6460 6461 for (i = 0; i < devip->nr_zones; i++) { 6462 zsp = &devip->zstate[i]; 6463 6464 zsp->z_start = zstart; 6465 6466 if (i < devip->nr_conv_zones) { 6467 zsp->z_type = ZBC_ZTYPE_CNV; 6468 zsp->z_cond = ZBC_NOT_WRITE_POINTER; 6469 zsp->z_wp = (sector_t)-1; 6470 zsp->z_size = 6471 min_t(u64, devip->zsize, capacity - zstart); 6472 } else if ((zstart & (devip->zsize - 1)) == 0) { 6473 if (devip->zoned) 6474 zsp->z_type = ZBC_ZTYPE_SWR; 6475 else 6476 zsp->z_type = ZBC_ZTYPE_SWP; 6477 zsp->z_cond = ZC1_EMPTY; 6478 zsp->z_wp = zsp->z_start; 6479 zsp->z_size = 6480 min_t(u64, devip->zcap, capacity - zstart); 6481 } else { 6482 zsp->z_type = ZBC_ZTYPE_GAP; 6483 zsp->z_cond = ZBC_NOT_WRITE_POINTER; 6484 zsp->z_wp = (sector_t)-1; 6485 zsp->z_size = min_t(u64, devip->zsize - devip->zcap, 6486 capacity - zstart); 6487 } 6488 6489 WARN_ON_ONCE((int)zsp->z_size <= 0); 6490 zstart += zsp->z_size; 6491 } 6492 6493 return 0; 6494 } 6495 6496 static struct sdebug_dev_info *sdebug_device_create( 6497 struct sdebug_host_info *sdbg_host, gfp_t flags) 6498 { 6499 struct sdebug_dev_info *devip; 6500 6501 devip = kzalloc_obj(*devip, flags); 6502 if (devip) { 6503 if (sdebug_uuid_ctl == 1) 6504 uuid_gen(&devip->lu_name); 6505 else if (sdebug_uuid_ctl == 2) { 6506 if (got_shared_uuid) 6507 devip->lu_name = shared_uuid; 6508 else { 6509 uuid_gen(&shared_uuid); 6510 got_shared_uuid = true; 6511 devip->lu_name = shared_uuid; 6512 } 6513 } 6514 devip->sdbg_host = sdbg_host; 6515 if (sdeb_zbc_in_use) { 6516 devip->zoned = sdeb_zbc_model == BLK_ZONED_HM; 6517 if (sdebug_device_create_zones(devip)) { 6518 kfree(devip); 6519 return NULL; 6520 } 6521 } else { 6522 devip->zoned = false; 6523 } 6524 if (sdebug_ptype == TYPE_TAPE) { 6525 devip->tape_density = TAPE_DEF_DENSITY; 6526 devip->tape_blksize = TAPE_DEF_BLKSIZE; 6527 } 6528 devip->create_ts = ktime_get_boottime(); 6529 atomic_set(&devip->stopped, (sdeb_tur_ms_to_ready > 0 ? 2 : 0)); 6530 spin_lock_init(&devip->list_lock); 6531 INIT_LIST_HEAD(&devip->inject_err_list); 6532 list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list); 6533 } 6534 return devip; 6535 } 6536 6537 static struct sdebug_dev_info *find_build_dev_info(struct scsi_device *sdev) 6538 { 6539 struct sdebug_host_info *sdbg_host; 6540 struct sdebug_dev_info *open_devip = NULL; 6541 struct sdebug_dev_info *devip; 6542 6543 sdbg_host = shost_to_sdebug_host(sdev->host); 6544 6545 list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) { 6546 if ((devip->used) && (devip->channel == sdev->channel) && 6547 (devip->target == sdev->id) && 6548 (devip->lun == sdev->lun)) 6549 return devip; 6550 else { 6551 if ((!devip->used) && (!open_devip)) 6552 open_devip = devip; 6553 } 6554 } 6555 if (!open_devip) { /* try and make a new one */ 6556 open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC); 6557 if (!open_devip) { 6558 pr_err("out of memory at line %d\n", __LINE__); 6559 return NULL; 6560 } 6561 } 6562 6563 open_devip->channel = sdev->channel; 6564 open_devip->target = sdev->id; 6565 open_devip->lun = sdev->lun; 6566 open_devip->sdbg_host = sdbg_host; 6567 set_bit(SDEBUG_UA_POOCCUR, open_devip->uas_bm); 6568 open_devip->used = true; 6569 return open_devip; 6570 } 6571 6572 static int scsi_debug_sdev_init(struct scsi_device *sdp) 6573 { 6574 if (sdebug_verbose) 6575 pr_info("sdev_init <%u %u %u %llu>\n", 6576 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); 6577 6578 return 0; 6579 } 6580 6581 static int scsi_debug_sdev_configure(struct scsi_device *sdp, 6582 struct queue_limits *lim) 6583 { 6584 struct sdebug_dev_info *devip = 6585 (struct sdebug_dev_info *)sdp->hostdata; 6586 struct dentry *dentry; 6587 6588 if (sdebug_verbose) 6589 pr_info("sdev_configure <%u %u %u %llu>\n", 6590 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); 6591 if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN) 6592 sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN; 6593 if (devip == NULL) { 6594 devip = find_build_dev_info(sdp); 6595 if (devip == NULL) 6596 return 1; /* no resources, will be marked offline */ 6597 } 6598 if (sdebug_ptype == TYPE_TAPE) { 6599 if (!devip->tape_blocks[0]) { 6600 devip->tape_blocks[0] = 6601 kzalloc_objs(struct tape_block, TAPE_UNITS + 1); 6602 if (!devip->tape_blocks[0]) 6603 return 1; 6604 } 6605 devip->tape_pending_nbr_partitions = -1; 6606 if (partition_tape(devip, 1, TAPE_UNITS, 0) < 0) { 6607 kfree(devip->tape_blocks[0]); 6608 devip->tape_blocks[0] = NULL; 6609 return 1; 6610 } 6611 } 6612 sdp->hostdata = devip; 6613 if (sdebug_no_uld) 6614 sdp->no_uld_attach = 1; 6615 config_cdb_len(sdp); 6616 6617 if (sdebug_allow_restart) 6618 sdp->allow_restart = 1; 6619 6620 devip->debugfs_entry = debugfs_create_dir(dev_name(&sdp->sdev_dev), 6621 sdebug_debugfs_root); 6622 if (IS_ERR_OR_NULL(devip->debugfs_entry)) 6623 pr_info("failed to create debugfs directory for device %s\n", 6624 dev_name(&sdp->sdev_gendev)); 6625 6626 dentry = debugfs_create_file("error", 0600, devip->debugfs_entry, sdp, 6627 &sdebug_error_fops); 6628 if (IS_ERR_OR_NULL(dentry)) 6629 pr_info("failed to create error file for device %s\n", 6630 dev_name(&sdp->sdev_gendev)); 6631 6632 return 0; 6633 } 6634 6635 static void scsi_debug_sdev_destroy(struct scsi_device *sdp) 6636 { 6637 struct sdebug_dev_info *devip = 6638 (struct sdebug_dev_info *)sdp->hostdata; 6639 struct sdebug_err_inject *err; 6640 6641 if (sdebug_verbose) 6642 pr_info("sdev_destroy <%u %u %u %llu>\n", 6643 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); 6644 6645 if (!devip) 6646 return; 6647 6648 spin_lock(&devip->list_lock); 6649 list_for_each_entry_rcu(err, &devip->inject_err_list, list) { 6650 list_del_rcu(&err->list); 6651 call_rcu(&err->rcu, sdebug_err_free); 6652 } 6653 spin_unlock(&devip->list_lock); 6654 6655 debugfs_remove(devip->debugfs_entry); 6656 6657 if (sdp->type == TYPE_TAPE) { 6658 kfree(devip->tape_blocks[0]); 6659 devip->tape_blocks[0] = NULL; 6660 } 6661 6662 /* make this slot available for re-use */ 6663 devip->used = false; 6664 sdp->hostdata = NULL; 6665 } 6666 6667 /* Returns true if cancelled or not running callback. */ 6668 static bool scsi_debug_stop_cmnd(struct scsi_cmnd *cmnd) 6669 { 6670 struct sdebug_scsi_cmd *sdsc = scsi_cmd_priv(cmnd); 6671 struct sdebug_defer *sd_dp = &sdsc->sd_dp; 6672 enum sdeb_defer_type defer_t = sd_dp->defer_t; 6673 6674 lockdep_assert_held(&sdsc->lock); 6675 6676 if (defer_t == SDEB_DEFER_HRT) { 6677 int res = hrtimer_try_to_cancel(&sd_dp->hrt); 6678 6679 switch (res) { 6680 case -1: /* -1 It's executing the CB */ 6681 return false; 6682 case 0: /* Not active, it must have already run */ 6683 case 1: /* Was active, we've now cancelled */ 6684 default: 6685 return true; 6686 } 6687 } else if (defer_t == SDEB_DEFER_WQ) { 6688 /* Cancel if pending */ 6689 if (cancel_work(&sd_dp->ew.work)) 6690 return true; 6691 /* callback may be running, so return false */ 6692 return false; 6693 } else if (defer_t == SDEB_DEFER_POLL) { 6694 return true; 6695 } 6696 6697 return false; 6698 } 6699 6700 struct sdebug_abort_cmd { 6701 u32 unique_tag; 6702 }; 6703 6704 enum sdebug_internal_cmd_type { 6705 SCSI_DEBUG_ABORT_CMD, 6706 }; 6707 6708 struct sdebug_internal_cmd { 6709 enum sdebug_internal_cmd_type type; 6710 6711 union { 6712 struct sdebug_abort_cmd abort_cmd; 6713 }; 6714 }; 6715 6716 union sdebug_priv { 6717 struct sdebug_scsi_cmd cmd; 6718 struct sdebug_internal_cmd internal_cmd; 6719 }; 6720 6721 /* 6722 * Abort SCSI command @cmnd. Only called from scsi_debug_abort(). Although 6723 * it would be possible to call scsi_debug_stop_cmnd() directly, an internal 6724 * command is allocated and submitted to trigger the reserved command 6725 * infrastructure. 6726 */ 6727 static bool scsi_debug_abort_cmnd(struct scsi_cmnd *cmnd) 6728 { 6729 struct Scsi_Host *shost = cmnd->device->host; 6730 struct request *rq = scsi_cmd_to_rq(cmnd); 6731 u32 unique_tag = blk_mq_unique_tag(rq); 6732 struct sdebug_internal_cmd *internal_cmd; 6733 struct scsi_cmnd *abort_cmd; 6734 struct request *abort_rq; 6735 blk_status_t res; 6736 6737 abort_cmd = scsi_get_internal_cmd(shost->pseudo_sdev, DMA_NONE, 6738 BLK_MQ_REQ_RESERVED); 6739 if (!abort_cmd) 6740 return false; 6741 internal_cmd = scsi_cmd_priv(abort_cmd); 6742 *internal_cmd = (struct sdebug_internal_cmd) { 6743 .type = SCSI_DEBUG_ABORT_CMD, 6744 .abort_cmd = { 6745 .unique_tag = unique_tag, 6746 }, 6747 }; 6748 abort_rq = scsi_cmd_to_rq(abort_cmd); 6749 abort_rq->timeout = secs_to_jiffies(3); 6750 res = blk_execute_rq(abort_rq, true); 6751 scsi_put_internal_cmd(abort_cmd); 6752 return res == BLK_STS_OK; 6753 } 6754 6755 /* 6756 * All we can do is set the cmnd as internally aborted and wait for it to 6757 * finish. We cannot call scsi_done() as normal completion path may do that. 6758 */ 6759 static bool sdebug_stop_cmnd(struct request *rq, void *data) 6760 { 6761 scsi_debug_abort_cmnd(blk_mq_rq_to_pdu(rq)); 6762 6763 return true; 6764 } 6765 6766 /* Deletes (stops) timers or work queues of all queued commands */ 6767 static void stop_all_queued(void) 6768 { 6769 struct sdebug_host_info *sdhp; 6770 6771 mutex_lock(&sdebug_host_list_mutex); 6772 list_for_each_entry(sdhp, &sdebug_host_list, host_list) { 6773 struct Scsi_Host *shost = sdhp->shost; 6774 6775 blk_mq_tagset_busy_iter(&shost->tag_set, sdebug_stop_cmnd, NULL); 6776 } 6777 mutex_unlock(&sdebug_host_list_mutex); 6778 } 6779 6780 static int sdebug_fail_abort(struct scsi_cmnd *cmnd) 6781 { 6782 struct scsi_device *sdp = cmnd->device; 6783 struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata; 6784 struct sdebug_err_inject *err; 6785 unsigned char *cmd = cmnd->cmnd; 6786 int ret = 0; 6787 6788 if (devip == NULL) 6789 return 0; 6790 6791 rcu_read_lock(); 6792 list_for_each_entry_rcu(err, &devip->inject_err_list, list) { 6793 if (err->type == ERR_ABORT_CMD_FAILED && 6794 (err->cmd == cmd[0] || err->cmd == 0xff)) { 6795 ret = !!err->cnt; 6796 if (err->cnt < 0) 6797 err->cnt++; 6798 6799 rcu_read_unlock(); 6800 return ret; 6801 } 6802 } 6803 rcu_read_unlock(); 6804 6805 return 0; 6806 } 6807 6808 static int scsi_debug_abort(struct scsi_cmnd *SCpnt) 6809 { 6810 bool aborted = scsi_debug_abort_cmnd(SCpnt); 6811 u8 *cmd = SCpnt->cmnd; 6812 u8 opcode = cmd[0]; 6813 6814 ++num_aborts; 6815 6816 if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) 6817 sdev_printk(KERN_INFO, SCpnt->device, 6818 "command%s found\n", 6819 aborted ? "" : " not"); 6820 6821 6822 if (sdebug_fail_abort(SCpnt)) { 6823 scmd_printk(KERN_INFO, SCpnt, "fail abort command 0x%x\n", 6824 opcode); 6825 return FAILED; 6826 } 6827 6828 if (aborted == false) 6829 return FAILED; 6830 6831 return SUCCESS; 6832 } 6833 6834 static bool scsi_debug_stop_all_queued_iter(struct request *rq, void *data) 6835 { 6836 struct scsi_device *sdp = data; 6837 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq); 6838 6839 if (scmd->device == sdp) 6840 scsi_debug_abort_cmnd(scmd); 6841 6842 return true; 6843 } 6844 6845 /* Deletes (stops) timers or work queues of all queued commands per sdev */ 6846 static void scsi_debug_stop_all_queued(struct scsi_device *sdp) 6847 { 6848 struct Scsi_Host *shost = sdp->host; 6849 6850 blk_mq_tagset_busy_iter(&shost->tag_set, 6851 scsi_debug_stop_all_queued_iter, sdp); 6852 } 6853 6854 static int sdebug_fail_lun_reset(struct scsi_cmnd *cmnd) 6855 { 6856 struct scsi_device *sdp = cmnd->device; 6857 struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata; 6858 struct sdebug_err_inject *err; 6859 unsigned char *cmd = cmnd->cmnd; 6860 int ret = 0; 6861 6862 if (devip == NULL) 6863 return 0; 6864 6865 rcu_read_lock(); 6866 list_for_each_entry_rcu(err, &devip->inject_err_list, list) { 6867 if (err->type == ERR_LUN_RESET_FAILED && 6868 (err->cmd == cmd[0] || err->cmd == 0xff)) { 6869 ret = !!err->cnt; 6870 if (err->cnt < 0) 6871 err->cnt++; 6872 6873 rcu_read_unlock(); 6874 return ret; 6875 } 6876 } 6877 rcu_read_unlock(); 6878 6879 return 0; 6880 } 6881 6882 static void scsi_tape_reset_clear(struct sdebug_dev_info *devip) 6883 { 6884 int i; 6885 6886 devip->tape_blksize = TAPE_DEF_BLKSIZE; 6887 devip->tape_density = TAPE_DEF_DENSITY; 6888 devip->tape_partition = 0; 6889 devip->tape_dce = 0; 6890 for (i = 0; i < TAPE_MAX_PARTITIONS; i++) 6891 devip->tape_location[i] = 0; 6892 devip->tape_pending_nbr_partitions = -1; 6893 /* Don't reset partitioning? */ 6894 } 6895 6896 static int scsi_debug_device_reset(struct scsi_cmnd *SCpnt) 6897 { 6898 struct scsi_device *sdp = SCpnt->device; 6899 struct sdebug_dev_info *devip = sdp->hostdata; 6900 u8 *cmd = SCpnt->cmnd; 6901 u8 opcode = cmd[0]; 6902 6903 ++num_dev_resets; 6904 6905 if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) 6906 sdev_printk(KERN_INFO, sdp, "doing device reset\n"); 6907 6908 scsi_debug_stop_all_queued(sdp); 6909 if (devip) { 6910 set_bit(SDEBUG_UA_POR, devip->uas_bm); 6911 if (SCpnt->device->type == TYPE_TAPE) 6912 scsi_tape_reset_clear(devip); 6913 } 6914 6915 if (sdebug_fail_lun_reset(SCpnt)) { 6916 scmd_printk(KERN_INFO, SCpnt, "fail lun reset 0x%x\n", opcode); 6917 return FAILED; 6918 } 6919 6920 return SUCCESS; 6921 } 6922 6923 static int sdebug_fail_target_reset(struct scsi_cmnd *cmnd) 6924 { 6925 struct scsi_target *starget = scsi_target(cmnd->device); 6926 struct sdebug_target_info *targetip = 6927 (struct sdebug_target_info *)starget->hostdata; 6928 6929 if (targetip) 6930 return targetip->reset_fail; 6931 6932 return 0; 6933 } 6934 6935 static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt) 6936 { 6937 struct scsi_device *sdp = SCpnt->device; 6938 struct sdebug_host_info *sdbg_host = shost_to_sdebug_host(sdp->host); 6939 struct sdebug_dev_info *devip; 6940 u8 *cmd = SCpnt->cmnd; 6941 u8 opcode = cmd[0]; 6942 int k = 0; 6943 6944 ++num_target_resets; 6945 if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) 6946 sdev_printk(KERN_INFO, sdp, "doing target reset\n"); 6947 6948 list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) { 6949 if (devip->target == sdp->id) { 6950 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); 6951 if (SCpnt->device->type == TYPE_TAPE) 6952 scsi_tape_reset_clear(devip); 6953 ++k; 6954 } 6955 } 6956 6957 if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) 6958 sdev_printk(KERN_INFO, sdp, 6959 "%d device(s) found in target\n", k); 6960 6961 if (sdebug_fail_target_reset(SCpnt)) { 6962 scmd_printk(KERN_INFO, SCpnt, "fail target reset 0x%x\n", 6963 opcode); 6964 return FAILED; 6965 } 6966 6967 return SUCCESS; 6968 } 6969 6970 static int scsi_debug_bus_reset(struct scsi_cmnd *SCpnt) 6971 { 6972 struct scsi_device *sdp = SCpnt->device; 6973 struct sdebug_host_info *sdbg_host = shost_to_sdebug_host(sdp->host); 6974 struct sdebug_dev_info *devip; 6975 int k = 0; 6976 6977 ++num_bus_resets; 6978 6979 if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) 6980 sdev_printk(KERN_INFO, sdp, "doing bus reset\n"); 6981 6982 list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) { 6983 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); 6984 if (SCpnt->device->type == TYPE_TAPE) 6985 scsi_tape_reset_clear(devip); 6986 ++k; 6987 } 6988 6989 if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) 6990 sdev_printk(KERN_INFO, sdp, 6991 "%d device(s) found in host\n", k); 6992 return SUCCESS; 6993 } 6994 6995 static int scsi_debug_host_reset(struct scsi_cmnd *SCpnt) 6996 { 6997 struct sdebug_host_info *sdbg_host; 6998 struct sdebug_dev_info *devip; 6999 int k = 0; 7000 7001 ++num_host_resets; 7002 if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) 7003 sdev_printk(KERN_INFO, SCpnt->device, "doing host reset\n"); 7004 mutex_lock(&sdebug_host_list_mutex); 7005 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { 7006 list_for_each_entry(devip, &sdbg_host->dev_info_list, 7007 dev_list) { 7008 set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); 7009 if (SCpnt->device->type == TYPE_TAPE) 7010 scsi_tape_reset_clear(devip); 7011 ++k; 7012 } 7013 } 7014 mutex_unlock(&sdebug_host_list_mutex); 7015 stop_all_queued(); 7016 if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) 7017 sdev_printk(KERN_INFO, SCpnt->device, 7018 "%d device(s) found\n", k); 7019 return SUCCESS; 7020 } 7021 7022 static void sdebug_build_parts(unsigned char *ramp, unsigned long store_size) 7023 { 7024 struct msdos_partition *pp; 7025 int starts[SDEBUG_MAX_PARTS + 2], max_part_secs; 7026 int sectors_per_part, num_sectors, k; 7027 int heads_by_sects, start_sec, end_sec; 7028 7029 /* assume partition table already zeroed */ 7030 if ((sdebug_num_parts < 1) || (store_size < 1048576)) 7031 return; 7032 if (sdebug_num_parts > SDEBUG_MAX_PARTS) { 7033 sdebug_num_parts = SDEBUG_MAX_PARTS; 7034 pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS); 7035 } 7036 num_sectors = (int)get_sdebug_capacity(); 7037 sectors_per_part = (num_sectors - sdebug_sectors_per) 7038 / sdebug_num_parts; 7039 heads_by_sects = sdebug_heads * sdebug_sectors_per; 7040 starts[0] = sdebug_sectors_per; 7041 max_part_secs = sectors_per_part; 7042 for (k = 1; k < sdebug_num_parts; ++k) { 7043 starts[k] = ((k * sectors_per_part) / heads_by_sects) 7044 * heads_by_sects; 7045 if (starts[k] - starts[k - 1] < max_part_secs) 7046 max_part_secs = starts[k] - starts[k - 1]; 7047 } 7048 starts[sdebug_num_parts] = num_sectors; 7049 starts[sdebug_num_parts + 1] = 0; 7050 7051 ramp[510] = 0x55; /* magic partition markings */ 7052 ramp[511] = 0xAA; 7053 pp = (struct msdos_partition *)(ramp + 0x1be); 7054 for (k = 0; starts[k + 1]; ++k, ++pp) { 7055 start_sec = starts[k]; 7056 end_sec = starts[k] + max_part_secs - 1; 7057 pp->boot_ind = 0; 7058 7059 pp->cyl = start_sec / heads_by_sects; 7060 pp->head = (start_sec - (pp->cyl * heads_by_sects)) 7061 / sdebug_sectors_per; 7062 pp->sector = (start_sec % sdebug_sectors_per) + 1; 7063 7064 pp->end_cyl = end_sec / heads_by_sects; 7065 pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects)) 7066 / sdebug_sectors_per; 7067 pp->end_sector = (end_sec % sdebug_sectors_per) + 1; 7068 7069 pp->start_sect = cpu_to_le32(start_sec); 7070 pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1); 7071 pp->sys_ind = 0x83; /* plain Linux partition */ 7072 } 7073 } 7074 7075 static void block_unblock_all_queues(bool block) 7076 { 7077 struct sdebug_host_info *sdhp; 7078 7079 lockdep_assert_held(&sdebug_host_list_mutex); 7080 7081 list_for_each_entry(sdhp, &sdebug_host_list, host_list) { 7082 struct Scsi_Host *shost = sdhp->shost; 7083 7084 if (block) 7085 scsi_block_requests(shost); 7086 else 7087 scsi_unblock_requests(shost); 7088 } 7089 } 7090 7091 /* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1 7092 * commands will be processed normally before triggers occur. 7093 */ 7094 static void tweak_cmnd_count(void) 7095 { 7096 int count, modulo; 7097 7098 modulo = abs(sdebug_every_nth); 7099 if (modulo < 2) 7100 return; 7101 7102 mutex_lock(&sdebug_host_list_mutex); 7103 block_unblock_all_queues(true); 7104 count = atomic_read(&sdebug_cmnd_count); 7105 atomic_set(&sdebug_cmnd_count, (count / modulo) * modulo); 7106 block_unblock_all_queues(false); 7107 mutex_unlock(&sdebug_host_list_mutex); 7108 } 7109 7110 static void clear_queue_stats(void) 7111 { 7112 atomic_set(&sdebug_cmnd_count, 0); 7113 atomic_set(&sdebug_completions, 0); 7114 atomic_set(&sdebug_miss_cpus, 0); 7115 atomic_set(&sdebug_a_tsf, 0); 7116 } 7117 7118 static bool inject_on_this_cmd(void) 7119 { 7120 if (sdebug_every_nth == 0) 7121 return false; 7122 return (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) == 0; 7123 } 7124 7125 #define INCLUSIVE_TIMING_MAX_NS 1000000 /* 1 millisecond */ 7126 7127 /* Complete the processing of the thread that queued a SCSI command to this 7128 * driver. It either completes the command by calling cmnd_done() or 7129 * schedules a hr timer or work queue then returns 0. Returns 7130 * SCSI_MLQUEUE_HOST_BUSY if temporarily out of resources. 7131 */ 7132 static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip, 7133 int scsi_result, 7134 int (*pfp)(struct scsi_cmnd *, 7135 struct sdebug_dev_info *), 7136 int delta_jiff, int ndelay) 7137 { 7138 struct request *rq = scsi_cmd_to_rq(cmnd); 7139 bool polled = rq->cmd_flags & REQ_POLLED; 7140 struct sdebug_scsi_cmd *sdsc = scsi_cmd_priv(cmnd); 7141 unsigned long flags; 7142 u64 ns_from_boot = 0; 7143 struct scsi_device *sdp; 7144 struct sdebug_defer *sd_dp; 7145 7146 if (unlikely(devip == NULL)) { 7147 if (scsi_result == 0) 7148 scsi_result = DID_NO_CONNECT << 16; 7149 goto respond_in_thread; 7150 } 7151 sdp = cmnd->device; 7152 7153 if (delta_jiff == 0) 7154 goto respond_in_thread; 7155 7156 7157 if (unlikely(sdebug_every_nth && (SDEBUG_OPT_RARE_TSF & sdebug_opts) && 7158 (scsi_result == 0))) { 7159 int num_in_q = scsi_device_busy(sdp); 7160 int qdepth = cmnd->device->queue_depth; 7161 7162 if ((num_in_q == qdepth) && 7163 (atomic_inc_return(&sdebug_a_tsf) >= 7164 abs(sdebug_every_nth))) { 7165 atomic_set(&sdebug_a_tsf, 0); 7166 scsi_result = device_qfull_result; 7167 7168 if (unlikely(SDEBUG_OPT_Q_NOISE & sdebug_opts)) 7169 sdev_printk(KERN_INFO, sdp, "num_in_q=%d +1, <inject> status: TASK SET FULL\n", 7170 num_in_q); 7171 } 7172 } 7173 7174 sd_dp = &sdsc->sd_dp; 7175 7176 if (polled || (ndelay > 0 && ndelay < INCLUSIVE_TIMING_MAX_NS)) 7177 ns_from_boot = ktime_get_boottime_ns(); 7178 7179 /* one of the resp_*() response functions is called here */ 7180 cmnd->result = pfp ? pfp(cmnd, devip) : 0; 7181 if (cmnd->result & SDEG_RES_IMMED_MASK) { 7182 cmnd->result &= ~SDEG_RES_IMMED_MASK; 7183 delta_jiff = ndelay = 0; 7184 } 7185 if (cmnd->result == 0 && scsi_result != 0) 7186 cmnd->result = scsi_result; 7187 if (cmnd->result == 0 && unlikely(sdebug_opts & SDEBUG_OPT_TRANSPORT_ERR)) { 7188 if (atomic_read(&sdeb_inject_pending)) { 7189 mk_sense_buffer(cmnd, ABORTED_COMMAND, TRANSPORT_PROBLEM, ACK_NAK_TO); 7190 atomic_set(&sdeb_inject_pending, 0); 7191 cmnd->result = check_condition_result; 7192 } 7193 } 7194 7195 if (unlikely(sdebug_verbose && cmnd->result)) 7196 sdev_printk(KERN_INFO, sdp, "non-zero result=0x%x\n", 7197 cmnd->result); 7198 7199 if (delta_jiff > 0 || ndelay > 0) { 7200 ktime_t kt; 7201 7202 if (delta_jiff > 0) { 7203 u64 ns = jiffies_to_nsecs(delta_jiff); 7204 7205 if (sdebug_random && ns < U32_MAX) { 7206 ns = get_random_u32_below((u32)ns); 7207 } else if (sdebug_random) { 7208 ns >>= 12; /* scale to 4 usec precision */ 7209 if (ns < U32_MAX) /* over 4 hours max */ 7210 ns = get_random_u32_below((u32)ns); 7211 ns <<= 12; 7212 } 7213 kt = ns_to_ktime(ns); 7214 } else { /* ndelay has a 4.2 second max */ 7215 kt = sdebug_random ? get_random_u32_below((u32)ndelay) : 7216 (u32)ndelay; 7217 if (ndelay < INCLUSIVE_TIMING_MAX_NS) { 7218 u64 d = ktime_get_boottime_ns() - ns_from_boot; 7219 7220 if (kt <= d) { /* elapsed duration >= kt */ 7221 /* call scsi_done() from this thread */ 7222 scsi_done(cmnd); 7223 return 0; 7224 } 7225 /* otherwise reduce kt by elapsed time */ 7226 kt -= d; 7227 } 7228 } 7229 if (sdebug_statistics) 7230 sd_dp->issuing_cpu = raw_smp_processor_id(); 7231 if (polled) { 7232 spin_lock_irqsave(&sdsc->lock, flags); 7233 sd_dp->cmpl_ts = ktime_add(ns_to_ktime(ns_from_boot), kt); 7234 sd_dp->defer_t = SDEB_DEFER_POLL; 7235 spin_unlock_irqrestore(&sdsc->lock, flags); 7236 } else { 7237 /* schedule the invocation of scsi_done() for a later time */ 7238 spin_lock_irqsave(&sdsc->lock, flags); 7239 sd_dp->defer_t = SDEB_DEFER_HRT; 7240 hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL_PINNED); 7241 /* 7242 * The completion handler will try to grab sqcp->lock, 7243 * so there is no chance that the completion handler 7244 * will call scsi_done() until we release the lock 7245 * here (so ok to keep referencing sdsc). 7246 */ 7247 spin_unlock_irqrestore(&sdsc->lock, flags); 7248 } 7249 } else { /* jdelay < 0, use work queue */ 7250 if (unlikely((sdebug_opts & SDEBUG_OPT_CMD_ABORT) && 7251 atomic_read(&sdeb_inject_pending))) { 7252 sd_dp->aborted = true; 7253 atomic_set(&sdeb_inject_pending, 0); 7254 sdev_printk(KERN_INFO, sdp, "abort request tag=%#x\n", 7255 blk_mq_unique_tag_to_tag(get_tag(cmnd))); 7256 } 7257 7258 if (sdebug_statistics) 7259 sd_dp->issuing_cpu = raw_smp_processor_id(); 7260 if (polled) { 7261 spin_lock_irqsave(&sdsc->lock, flags); 7262 sd_dp->cmpl_ts = ns_to_ktime(ns_from_boot); 7263 sd_dp->defer_t = SDEB_DEFER_POLL; 7264 spin_unlock_irqrestore(&sdsc->lock, flags); 7265 } else { 7266 spin_lock_irqsave(&sdsc->lock, flags); 7267 sd_dp->defer_t = SDEB_DEFER_WQ; 7268 schedule_work(&sd_dp->ew.work); 7269 spin_unlock_irqrestore(&sdsc->lock, flags); 7270 } 7271 } 7272 7273 return 0; 7274 7275 respond_in_thread: /* call back to mid-layer using invocation thread */ 7276 cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0; 7277 cmnd->result &= ~SDEG_RES_IMMED_MASK; 7278 if (cmnd->result == 0 && scsi_result != 0) 7279 cmnd->result = scsi_result; 7280 scsi_done(cmnd); 7281 return 0; 7282 } 7283 7284 /* Note: The following macros create attribute files in the 7285 /sys/module/scsi_debug/parameters directory. Unfortunately this 7286 driver is unaware of a change and cannot trigger auxiliary actions 7287 as it can when the corresponding attribute in the 7288 /sys/bus/pseudo/drivers/scsi_debug directory is changed. 7289 */ 7290 module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR); 7291 module_param_named(ato, sdebug_ato, int, S_IRUGO); 7292 module_param_named(cdb_len, sdebug_cdb_len, int, 0644); 7293 module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR); 7294 module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR); 7295 module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO); 7296 module_param_named(dif, sdebug_dif, int, S_IRUGO); 7297 module_param_named(dix, sdebug_dix, int, S_IRUGO); 7298 module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR); 7299 module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR); 7300 module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR); 7301 module_param_named(guard, sdebug_guard, uint, S_IRUGO); 7302 module_param_named(host_lock, sdebug_host_lock, bool, S_IRUGO | S_IWUSR); 7303 module_param_named(host_max_queue, sdebug_host_max_queue, int, S_IRUGO); 7304 module_param_string(inq_product, sdebug_inq_product_id, 7305 sizeof(sdebug_inq_product_id), S_IRUGO | S_IWUSR); 7306 module_param_string(inq_rev, sdebug_inq_product_rev, 7307 sizeof(sdebug_inq_product_rev), S_IRUGO | S_IWUSR); 7308 module_param_string(inq_vendor, sdebug_inq_vendor_id, 7309 sizeof(sdebug_inq_vendor_id), S_IRUGO | S_IWUSR); 7310 module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO); 7311 module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO); 7312 module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO); 7313 module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO); 7314 module_param_named(atomic_wr, sdebug_atomic_wr, int, S_IRUGO); 7315 module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO); 7316 module_param_named(lun_format, sdebug_lun_am_i, int, S_IRUGO | S_IWUSR); 7317 module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR); 7318 module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR); 7319 module_param_named(medium_error_count, sdebug_medium_error_count, int, 7320 S_IRUGO | S_IWUSR); 7321 module_param_named(medium_error_start, sdebug_medium_error_start, int, 7322 S_IRUGO | S_IWUSR); 7323 module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR); 7324 module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR); 7325 module_param_named(no_rwlock, sdebug_no_rwlock, bool, S_IRUGO | S_IWUSR); 7326 module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO); 7327 module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO); 7328 module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR); 7329 module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO); 7330 module_param_named(opt_xferlen_exp, sdebug_opt_xferlen_exp, int, S_IRUGO); 7331 module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR); 7332 module_param_named(per_host_store, sdebug_per_host_store, bool, 7333 S_IRUGO | S_IWUSR); 7334 module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO); 7335 module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR); 7336 module_param_named(random, sdebug_random, bool, S_IRUGO | S_IWUSR); 7337 module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR); 7338 module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO); 7339 module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO); 7340 module_param_named(statistics, sdebug_statistics, bool, S_IRUGO | S_IWUSR); 7341 module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR); 7342 module_param_named(submit_queues, submit_queues, int, S_IRUGO); 7343 module_param_named(poll_queues, poll_queues, int, S_IRUGO); 7344 module_param_named(tur_ms_to_ready, sdeb_tur_ms_to_ready, int, S_IRUGO); 7345 module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO); 7346 module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO); 7347 module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO); 7348 module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO); 7349 module_param_named(atomic_wr_max_length, sdebug_atomic_wr_max_length, int, S_IRUGO); 7350 module_param_named(atomic_wr_align, sdebug_atomic_wr_align, int, S_IRUGO); 7351 module_param_named(atomic_wr_gran, sdebug_atomic_wr_gran, int, S_IRUGO); 7352 module_param_named(atomic_wr_max_length_bndry, sdebug_atomic_wr_max_length_bndry, int, S_IRUGO); 7353 module_param_named(atomic_wr_max_bndry, sdebug_atomic_wr_max_bndry, int, S_IRUGO); 7354 module_param_named(uuid_ctl, sdebug_uuid_ctl, int, S_IRUGO); 7355 module_param_named(virtual_gb, sdebug_virtual_gb, int, S_IRUGO | S_IWUSR); 7356 module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int, 7357 S_IRUGO | S_IWUSR); 7358 module_param_named(wp, sdebug_wp, bool, S_IRUGO | S_IWUSR); 7359 module_param_named(write_same_length, sdebug_write_same_length, int, 7360 S_IRUGO | S_IWUSR); 7361 module_param_named(zbc, sdeb_zbc_model_s, charp, S_IRUGO); 7362 module_param_named(zone_cap_mb, sdeb_zbc_zone_cap_mb, int, S_IRUGO); 7363 module_param_named(zone_max_open, sdeb_zbc_max_open, int, S_IRUGO); 7364 module_param_named(zone_nr_conv, sdeb_zbc_nr_conv, int, S_IRUGO); 7365 module_param_named(zone_size_mb, sdeb_zbc_zone_size_mb, int, S_IRUGO); 7366 module_param_named(allow_restart, sdebug_allow_restart, bool, S_IRUGO | S_IWUSR); 7367 7368 MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert"); 7369 MODULE_DESCRIPTION("SCSI debug adapter driver"); 7370 MODULE_LICENSE("GPL"); 7371 MODULE_VERSION(SDEBUG_VERSION); 7372 7373 MODULE_PARM_DESC(add_host, "add n hosts, in sysfs if negative remove host(s) (def=1)"); 7374 MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)"); 7375 MODULE_PARM_DESC(cdb_len, "suggest CDB lengths to drivers (def=10)"); 7376 MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)"); 7377 MODULE_PARM_DESC(delay, "response delay (def=1 jiffy); 0:imm, -1,-2:tiny"); 7378 MODULE_PARM_DESC(dev_size_mb, "size in MiB of ram shared by devs(def=8)"); 7379 MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)"); 7380 MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)"); 7381 MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)"); 7382 MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)"); 7383 MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)"); 7384 MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)"); 7385 MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)"); 7386 MODULE_PARM_DESC(host_max_queue, 7387 "host max # of queued cmds (0 to max(def) [max_queue fixed equal for !0])"); 7388 MODULE_PARM_DESC(inq_product, "SCSI INQUIRY product string (def=\"scsi_debug\")"); 7389 MODULE_PARM_DESC(inq_rev, "SCSI INQUIRY revision string (def=\"" 7390 SDEBUG_VERSION "\")"); 7391 MODULE_PARM_DESC(inq_vendor, "SCSI INQUIRY vendor string (def=\"Linux\")"); 7392 MODULE_PARM_DESC(lbprz, 7393 "on read unmapped LBs return 0 when 1 (def), return 0xff when 2"); 7394 MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)"); 7395 MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)"); 7396 MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)"); 7397 MODULE_PARM_DESC(atomic_wr, "enable ATOMIC WRITE support, support WRITE ATOMIC(16) (def=0)"); 7398 MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)"); 7399 MODULE_PARM_DESC(lun_format, "LUN format: 0->peripheral (def); 1 --> flat address method"); 7400 MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)"); 7401 MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))"); 7402 MODULE_PARM_DESC(medium_error_count, "count of sectors to return follow on MEDIUM error"); 7403 MODULE_PARM_DESC(medium_error_start, "starting sector number to return MEDIUM error"); 7404 MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)"); 7405 MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)"); 7406 MODULE_PARM_DESC(no_rwlock, "don't protect user data reads+writes (def=0)"); 7407 MODULE_PARM_DESC(no_uld, "stop ULD (e.g. sd driver) attaching (def=0))"); 7408 MODULE_PARM_DESC(num_parts, "number of partitions(def=0)"); 7409 MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)"); 7410 MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)"); 7411 MODULE_PARM_DESC(opt_xferlen_exp, "optimal transfer length granularity exponent (def=physblk_exp)"); 7412 MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)"); 7413 MODULE_PARM_DESC(per_host_store, "If set, next positive add_host will get new store (def=0)"); 7414 MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)"); 7415 MODULE_PARM_DESC(poll_queues, "support for iouring iopoll queues (1 to max(submit_queues - 1))"); 7416 MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])"); 7417 MODULE_PARM_DESC(random, "If set, uniformly randomize command duration between 0 and delay_in_ns"); 7418 MODULE_PARM_DESC(removable, "claim to have removable media (def=0)"); 7419 MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=7[SPC-5])"); 7420 MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)"); 7421 MODULE_PARM_DESC(statistics, "collect statistics on commands, queues (def=0)"); 7422 MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)"); 7423 MODULE_PARM_DESC(submit_queues, "support for block multi-queue (def=1)"); 7424 MODULE_PARM_DESC(tur_ms_to_ready, "TEST UNIT READY millisecs before initial good status (def=0)"); 7425 MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)"); 7426 MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)"); 7427 MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)"); 7428 MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)"); 7429 MODULE_PARM_DESC(atomic_wr_max_length, "max # of blocks can be atomically written in one cmd (def=8192)"); 7430 MODULE_PARM_DESC(atomic_wr_align, "minimum alignment of atomic write in blocks (def=2)"); 7431 MODULE_PARM_DESC(atomic_wr_gran, "minimum granularity of atomic write in blocks (def=2)"); 7432 MODULE_PARM_DESC(atomic_wr_max_length_bndry, "max # of blocks can be atomically written in one cmd with boundary set (def=8192)"); 7433 MODULE_PARM_DESC(atomic_wr_max_bndry, "max # boundaries per atomic write (def=128)"); 7434 MODULE_PARM_DESC(uuid_ctl, 7435 "1->use uuid for lu name, 0->don't, 2->all use same (def=0)"); 7436 MODULE_PARM_DESC(virtual_gb, "virtual gigabyte (GiB) size (def=0 -> use dev_size_mb)"); 7437 MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)"); 7438 MODULE_PARM_DESC(wp, "Write Protect (def=0)"); 7439 MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)"); 7440 MODULE_PARM_DESC(zbc, "'none' [0]; 'aware' [1]; 'managed' [2] (def=0). Can have 'host-' prefix"); 7441 MODULE_PARM_DESC(zone_cap_mb, "Zone capacity in MiB (def=zone size)"); 7442 MODULE_PARM_DESC(zone_max_open, "Maximum number of open zones; [0] for no limit (def=auto)"); 7443 MODULE_PARM_DESC(zone_nr_conv, "Number of conventional zones (def=1)"); 7444 MODULE_PARM_DESC(zone_size_mb, "Zone size in MiB (def=auto)"); 7445 MODULE_PARM_DESC(allow_restart, "Set scsi_device's allow_restart flag(def=0)"); 7446 7447 #define SDEBUG_INFO_LEN 256 7448 static char sdebug_info[SDEBUG_INFO_LEN]; 7449 7450 static const char *scsi_debug_info(struct Scsi_Host *shp) 7451 { 7452 int k; 7453 7454 k = scnprintf(sdebug_info, SDEBUG_INFO_LEN, "%s: version %s [%s]\n", 7455 my_name, SDEBUG_VERSION, sdebug_version_date); 7456 if (k >= (SDEBUG_INFO_LEN - 1)) 7457 return sdebug_info; 7458 scnprintf(sdebug_info + k, SDEBUG_INFO_LEN - k, 7459 " dev_size_mb=%d, opts=0x%x, submit_queues=%d, %s=%d", 7460 sdebug_dev_size_mb, sdebug_opts, submit_queues, 7461 "statistics", (int)sdebug_statistics); 7462 return sdebug_info; 7463 } 7464 7465 /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */ 7466 static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer, 7467 int length) 7468 { 7469 char arr[16]; 7470 int opts; 7471 int minLen = length > 15 ? 15 : length; 7472 7473 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) 7474 return -EACCES; 7475 memcpy(arr, buffer, minLen); 7476 arr[minLen] = '\0'; 7477 if (1 != sscanf(arr, "%d", &opts)) 7478 return -EINVAL; 7479 sdebug_opts = opts; 7480 sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); 7481 if (sdebug_every_nth != 0) 7482 tweak_cmnd_count(); 7483 return length; 7484 } 7485 7486 struct sdebug_submit_queue_data { 7487 int *first; 7488 int *last; 7489 int queue_num; 7490 }; 7491 7492 static bool sdebug_submit_queue_iter(struct request *rq, void *opaque) 7493 { 7494 struct sdebug_submit_queue_data *data = opaque; 7495 u32 unique_tag = blk_mq_unique_tag(rq); 7496 u16 hwq = blk_mq_unique_tag_to_hwq(unique_tag); 7497 u16 tag = blk_mq_unique_tag_to_tag(unique_tag); 7498 int queue_num = data->queue_num; 7499 7500 if (hwq != queue_num) 7501 return true; 7502 7503 /* Rely on iter'ing in ascending tag order */ 7504 if (*data->first == -1) 7505 *data->first = *data->last = tag; 7506 else 7507 *data->last = tag; 7508 7509 return true; 7510 } 7511 7512 /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the 7513 * same for each scsi_debug host (if more than one). Some of the counters 7514 * output are not atomics so might be inaccurate in a busy system. */ 7515 static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host) 7516 { 7517 struct sdebug_host_info *sdhp; 7518 int j; 7519 7520 seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n", 7521 SDEBUG_VERSION, sdebug_version_date); 7522 seq_printf(m, "num_tgts=%d, %ssize=%d MB, opts=0x%x, every_nth=%d\n", 7523 sdebug_num_tgts, "shared (ram) ", sdebug_dev_size_mb, 7524 sdebug_opts, sdebug_every_nth); 7525 seq_printf(m, "delay=%d, ndelay=%d, max_luns=%d, sector_size=%d %s\n", 7526 sdebug_jdelay, sdebug_ndelay, sdebug_max_luns, 7527 sdebug_sector_size, "bytes"); 7528 seq_printf(m, "cylinders=%d, heads=%d, sectors=%d, command aborts=%d\n", 7529 sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per, 7530 num_aborts); 7531 seq_printf(m, "RESETs: device=%d, target=%d, bus=%d, host=%d\n", 7532 num_dev_resets, num_target_resets, num_bus_resets, 7533 num_host_resets); 7534 seq_printf(m, "dix_reads=%d, dix_writes=%d, dif_errors=%d\n", 7535 dix_reads, dix_writes, dif_errors); 7536 seq_printf(m, "usec_in_jiffy=%lu, statistics=%d\n", TICK_NSEC / 1000, 7537 sdebug_statistics); 7538 seq_printf(m, "cmnd_count=%d, completions=%d, %s=%d, a_tsf=%d, mq_polls=%d\n", 7539 atomic_read(&sdebug_cmnd_count), 7540 atomic_read(&sdebug_completions), 7541 "miss_cpus", atomic_read(&sdebug_miss_cpus), 7542 atomic_read(&sdebug_a_tsf), 7543 atomic_read(&sdeb_mq_poll_count)); 7544 7545 seq_printf(m, "submit_queues=%d\n", submit_queues); 7546 for (j = 0; j < submit_queues; ++j) { 7547 int f = -1, l = -1; 7548 struct sdebug_submit_queue_data data = { 7549 .queue_num = j, 7550 .first = &f, 7551 .last = &l, 7552 }; 7553 seq_printf(m, " queue %d:\n", j); 7554 blk_mq_tagset_busy_iter(&host->tag_set, sdebug_submit_queue_iter, 7555 &data); 7556 if (f >= 0) { 7557 seq_printf(m, " BUSY: %s: %d,%d\n", 7558 "first,last bits", f, l); 7559 } 7560 } 7561 7562 seq_printf(m, "this host_no=%d\n", host->host_no); 7563 if (!xa_empty(per_store_ap)) { 7564 bool niu; 7565 int idx; 7566 unsigned long l_idx; 7567 struct sdeb_store_info *sip; 7568 7569 seq_puts(m, "\nhost list:\n"); 7570 j = 0; 7571 list_for_each_entry(sdhp, &sdebug_host_list, host_list) { 7572 idx = sdhp->si_idx; 7573 seq_printf(m, " %d: host_no=%d, si_idx=%d\n", j, 7574 sdhp->shost->host_no, idx); 7575 ++j; 7576 } 7577 seq_printf(m, "\nper_store array [most_recent_idx=%d]:\n", 7578 sdeb_most_recent_idx); 7579 j = 0; 7580 xa_for_each(per_store_ap, l_idx, sip) { 7581 niu = xa_get_mark(per_store_ap, l_idx, 7582 SDEB_XA_NOT_IN_USE); 7583 idx = (int)l_idx; 7584 seq_printf(m, " %d: idx=%d%s\n", j, idx, 7585 (niu ? " not_in_use" : "")); 7586 ++j; 7587 } 7588 } 7589 return 0; 7590 } 7591 7592 static ssize_t delay_show(struct device_driver *ddp, char *buf) 7593 { 7594 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay); 7595 } 7596 /* Returns -EBUSY if jdelay is being changed and commands are queued. The unit 7597 * of delay is jiffies. 7598 */ 7599 static ssize_t delay_store(struct device_driver *ddp, const char *buf, 7600 size_t count) 7601 { 7602 int jdelay, res; 7603 7604 if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) { 7605 res = count; 7606 if (sdebug_jdelay != jdelay) { 7607 struct sdebug_host_info *sdhp; 7608 7609 mutex_lock(&sdebug_host_list_mutex); 7610 block_unblock_all_queues(true); 7611 7612 list_for_each_entry(sdhp, &sdebug_host_list, host_list) { 7613 struct Scsi_Host *shost = sdhp->shost; 7614 7615 if (scsi_host_busy(shost)) { 7616 res = -EBUSY; /* queued commands */ 7617 break; 7618 } 7619 } 7620 if (res > 0) { 7621 sdebug_jdelay = jdelay; 7622 sdebug_ndelay = 0; 7623 } 7624 block_unblock_all_queues(false); 7625 mutex_unlock(&sdebug_host_list_mutex); 7626 } 7627 return res; 7628 } 7629 return -EINVAL; 7630 } 7631 static DRIVER_ATTR_RW(delay); 7632 7633 static ssize_t ndelay_show(struct device_driver *ddp, char *buf) 7634 { 7635 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay); 7636 } 7637 /* Returns -EBUSY if ndelay is being changed and commands are queued */ 7638 /* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */ 7639 static ssize_t ndelay_store(struct device_driver *ddp, const char *buf, 7640 size_t count) 7641 { 7642 int ndelay, res; 7643 7644 if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) && 7645 (ndelay >= 0) && (ndelay < (1000 * 1000 * 1000))) { 7646 res = count; 7647 if (sdebug_ndelay != ndelay) { 7648 struct sdebug_host_info *sdhp; 7649 7650 mutex_lock(&sdebug_host_list_mutex); 7651 block_unblock_all_queues(true); 7652 7653 list_for_each_entry(sdhp, &sdebug_host_list, host_list) { 7654 struct Scsi_Host *shost = sdhp->shost; 7655 7656 if (scsi_host_busy(shost)) { 7657 res = -EBUSY; /* queued commands */ 7658 break; 7659 } 7660 } 7661 7662 if (res > 0) { 7663 sdebug_ndelay = ndelay; 7664 sdebug_jdelay = ndelay ? JDELAY_OVERRIDDEN 7665 : DEF_JDELAY; 7666 } 7667 block_unblock_all_queues(false); 7668 mutex_unlock(&sdebug_host_list_mutex); 7669 } 7670 return res; 7671 } 7672 return -EINVAL; 7673 } 7674 static DRIVER_ATTR_RW(ndelay); 7675 7676 static ssize_t opts_show(struct device_driver *ddp, char *buf) 7677 { 7678 return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts); 7679 } 7680 7681 static ssize_t opts_store(struct device_driver *ddp, const char *buf, 7682 size_t count) 7683 { 7684 int opts; 7685 char work[20]; 7686 7687 if (sscanf(buf, "%10s", work) == 1) { 7688 if (strncasecmp(work, "0x", 2) == 0) { 7689 if (kstrtoint(work + 2, 16, &opts) == 0) 7690 goto opts_done; 7691 } else { 7692 if (kstrtoint(work, 10, &opts) == 0) 7693 goto opts_done; 7694 } 7695 } 7696 return -EINVAL; 7697 opts_done: 7698 sdebug_opts = opts; 7699 sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); 7700 tweak_cmnd_count(); 7701 return count; 7702 } 7703 static DRIVER_ATTR_RW(opts); 7704 7705 static ssize_t ptype_show(struct device_driver *ddp, char *buf) 7706 { 7707 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype); 7708 } 7709 static ssize_t ptype_store(struct device_driver *ddp, const char *buf, 7710 size_t count) 7711 { 7712 int n; 7713 7714 /* Cannot change from or to TYPE_ZBC with sysfs */ 7715 if (sdebug_ptype == TYPE_ZBC) 7716 return -EINVAL; 7717 7718 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { 7719 if (n == TYPE_ZBC) 7720 return -EINVAL; 7721 sdebug_ptype = n; 7722 return count; 7723 } 7724 return -EINVAL; 7725 } 7726 static DRIVER_ATTR_RW(ptype); 7727 7728 static ssize_t dsense_show(struct device_driver *ddp, char *buf) 7729 { 7730 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense); 7731 } 7732 static ssize_t dsense_store(struct device_driver *ddp, const char *buf, 7733 size_t count) 7734 { 7735 int n; 7736 7737 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { 7738 sdebug_dsense = n; 7739 return count; 7740 } 7741 return -EINVAL; 7742 } 7743 static DRIVER_ATTR_RW(dsense); 7744 7745 static ssize_t fake_rw_show(struct device_driver *ddp, char *buf) 7746 { 7747 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw); 7748 } 7749 static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf, 7750 size_t count) 7751 { 7752 int n, idx; 7753 7754 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { 7755 bool want_store = (n == 0); 7756 struct sdebug_host_info *sdhp; 7757 7758 n = (n > 0); 7759 sdebug_fake_rw = (sdebug_fake_rw > 0); 7760 if (sdebug_fake_rw == n) 7761 return count; /* not transitioning so do nothing */ 7762 7763 if (want_store) { /* 1 --> 0 transition, set up store */ 7764 if (sdeb_first_idx < 0) { 7765 idx = sdebug_add_store(); 7766 if (idx < 0) 7767 return idx; 7768 } else { 7769 idx = sdeb_first_idx; 7770 xa_clear_mark(per_store_ap, idx, 7771 SDEB_XA_NOT_IN_USE); 7772 } 7773 /* make all hosts use same store */ 7774 list_for_each_entry(sdhp, &sdebug_host_list, 7775 host_list) { 7776 if (sdhp->si_idx != idx) { 7777 xa_set_mark(per_store_ap, sdhp->si_idx, 7778 SDEB_XA_NOT_IN_USE); 7779 sdhp->si_idx = idx; 7780 } 7781 } 7782 sdeb_most_recent_idx = idx; 7783 } else { /* 0 --> 1 transition is trigger for shrink */ 7784 sdebug_erase_all_stores(true /* apart from first */); 7785 } 7786 sdebug_fake_rw = n; 7787 return count; 7788 } 7789 return -EINVAL; 7790 } 7791 static DRIVER_ATTR_RW(fake_rw); 7792 7793 static ssize_t no_lun_0_show(struct device_driver *ddp, char *buf) 7794 { 7795 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0); 7796 } 7797 static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf, 7798 size_t count) 7799 { 7800 int n; 7801 7802 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { 7803 sdebug_no_lun_0 = n; 7804 return count; 7805 } 7806 return -EINVAL; 7807 } 7808 static DRIVER_ATTR_RW(no_lun_0); 7809 7810 static ssize_t num_tgts_show(struct device_driver *ddp, char *buf) 7811 { 7812 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts); 7813 } 7814 static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf, 7815 size_t count) 7816 { 7817 int n; 7818 7819 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { 7820 sdebug_num_tgts = n; 7821 sdebug_max_tgts_luns(); 7822 return count; 7823 } 7824 return -EINVAL; 7825 } 7826 static DRIVER_ATTR_RW(num_tgts); 7827 7828 static ssize_t dev_size_mb_show(struct device_driver *ddp, char *buf) 7829 { 7830 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb); 7831 } 7832 static DRIVER_ATTR_RO(dev_size_mb); 7833 7834 static ssize_t per_host_store_show(struct device_driver *ddp, char *buf) 7835 { 7836 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_per_host_store); 7837 } 7838 7839 static ssize_t per_host_store_store(struct device_driver *ddp, const char *buf, 7840 size_t count) 7841 { 7842 bool v; 7843 7844 if (kstrtobool(buf, &v)) 7845 return -EINVAL; 7846 7847 sdebug_per_host_store = v; 7848 return count; 7849 } 7850 static DRIVER_ATTR_RW(per_host_store); 7851 7852 static ssize_t num_parts_show(struct device_driver *ddp, char *buf) 7853 { 7854 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts); 7855 } 7856 static DRIVER_ATTR_RO(num_parts); 7857 7858 static ssize_t every_nth_show(struct device_driver *ddp, char *buf) 7859 { 7860 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth); 7861 } 7862 static ssize_t every_nth_store(struct device_driver *ddp, const char *buf, 7863 size_t count) 7864 { 7865 int nth; 7866 char work[20]; 7867 7868 if (sscanf(buf, "%10s", work) == 1) { 7869 if (strncasecmp(work, "0x", 2) == 0) { 7870 if (kstrtoint(work + 2, 16, &nth) == 0) 7871 goto every_nth_done; 7872 } else { 7873 if (kstrtoint(work, 10, &nth) == 0) 7874 goto every_nth_done; 7875 } 7876 } 7877 return -EINVAL; 7878 7879 every_nth_done: 7880 sdebug_every_nth = nth; 7881 if (nth && !sdebug_statistics) { 7882 pr_info("every_nth needs statistics=1, set it\n"); 7883 sdebug_statistics = true; 7884 } 7885 tweak_cmnd_count(); 7886 return count; 7887 } 7888 static DRIVER_ATTR_RW(every_nth); 7889 7890 static ssize_t lun_format_show(struct device_driver *ddp, char *buf) 7891 { 7892 return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_lun_am); 7893 } 7894 static ssize_t lun_format_store(struct device_driver *ddp, const char *buf, 7895 size_t count) 7896 { 7897 int n; 7898 bool changed; 7899 7900 if (kstrtoint(buf, 0, &n)) 7901 return -EINVAL; 7902 if (n >= 0) { 7903 if (n > (int)SAM_LUN_AM_FLAT) { 7904 pr_warn("only LUN address methods 0 and 1 are supported\n"); 7905 return -EINVAL; 7906 } 7907 changed = ((int)sdebug_lun_am != n); 7908 sdebug_lun_am = n; 7909 if (changed && sdebug_scsi_level >= 5) { /* >= SPC-3 */ 7910 struct sdebug_host_info *sdhp; 7911 struct sdebug_dev_info *dp; 7912 7913 mutex_lock(&sdebug_host_list_mutex); 7914 list_for_each_entry(sdhp, &sdebug_host_list, host_list) { 7915 list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) { 7916 set_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm); 7917 } 7918 } 7919 mutex_unlock(&sdebug_host_list_mutex); 7920 } 7921 return count; 7922 } 7923 return -EINVAL; 7924 } 7925 static DRIVER_ATTR_RW(lun_format); 7926 7927 static ssize_t max_luns_show(struct device_driver *ddp, char *buf) 7928 { 7929 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns); 7930 } 7931 static ssize_t max_luns_store(struct device_driver *ddp, const char *buf, 7932 size_t count) 7933 { 7934 int n; 7935 bool changed; 7936 7937 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { 7938 if (n > 256) { 7939 pr_warn("max_luns can be no more than 256\n"); 7940 return -EINVAL; 7941 } 7942 changed = (sdebug_max_luns != n); 7943 sdebug_max_luns = n; 7944 sdebug_max_tgts_luns(); 7945 if (changed && (sdebug_scsi_level >= 5)) { /* >= SPC-3 */ 7946 struct sdebug_host_info *sdhp; 7947 struct sdebug_dev_info *dp; 7948 7949 mutex_lock(&sdebug_host_list_mutex); 7950 list_for_each_entry(sdhp, &sdebug_host_list, 7951 host_list) { 7952 list_for_each_entry(dp, &sdhp->dev_info_list, 7953 dev_list) { 7954 set_bit(SDEBUG_UA_LUNS_CHANGED, 7955 dp->uas_bm); 7956 } 7957 } 7958 mutex_unlock(&sdebug_host_list_mutex); 7959 } 7960 return count; 7961 } 7962 return -EINVAL; 7963 } 7964 static DRIVER_ATTR_RW(max_luns); 7965 7966 static ssize_t max_queue_show(struct device_driver *ddp, char *buf) 7967 { 7968 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue); 7969 } 7970 /* N.B. max_queue can be changed while there are queued commands. In flight 7971 * commands beyond the new max_queue will be completed. */ 7972 static ssize_t max_queue_store(struct device_driver *ddp, const char *buf, 7973 size_t count) 7974 { 7975 int n; 7976 7977 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) && 7978 (n <= SDEBUG_CANQUEUE) && 7979 (sdebug_host_max_queue == 0)) { 7980 mutex_lock(&sdebug_host_list_mutex); 7981 7982 /* We may only change sdebug_max_queue when we have no shosts */ 7983 if (list_empty(&sdebug_host_list)) 7984 sdebug_max_queue = n; 7985 else 7986 count = -EBUSY; 7987 mutex_unlock(&sdebug_host_list_mutex); 7988 return count; 7989 } 7990 return -EINVAL; 7991 } 7992 static DRIVER_ATTR_RW(max_queue); 7993 7994 static ssize_t host_max_queue_show(struct device_driver *ddp, char *buf) 7995 { 7996 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_host_max_queue); 7997 } 7998 7999 static ssize_t no_rwlock_show(struct device_driver *ddp, char *buf) 8000 { 8001 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_rwlock); 8002 } 8003 8004 static ssize_t no_rwlock_store(struct device_driver *ddp, const char *buf, size_t count) 8005 { 8006 bool v; 8007 8008 if (kstrtobool(buf, &v)) 8009 return -EINVAL; 8010 8011 sdebug_no_rwlock = v; 8012 return count; 8013 } 8014 static DRIVER_ATTR_RW(no_rwlock); 8015 8016 /* 8017 * Since this is used for .can_queue, and we get the hc_idx tag from the bitmap 8018 * in range [0, sdebug_host_max_queue), we can't change it. 8019 */ 8020 static DRIVER_ATTR_RO(host_max_queue); 8021 8022 static ssize_t no_uld_show(struct device_driver *ddp, char *buf) 8023 { 8024 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld); 8025 } 8026 static DRIVER_ATTR_RO(no_uld); 8027 8028 static ssize_t scsi_level_show(struct device_driver *ddp, char *buf) 8029 { 8030 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level); 8031 } 8032 static DRIVER_ATTR_RO(scsi_level); 8033 8034 static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf) 8035 { 8036 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb); 8037 } 8038 static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf, 8039 size_t count) 8040 { 8041 int n; 8042 bool changed; 8043 8044 /* Ignore capacity change for ZBC drives for now */ 8045 if (sdeb_zbc_in_use) 8046 return -ENOTSUPP; 8047 8048 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { 8049 changed = (sdebug_virtual_gb != n); 8050 sdebug_virtual_gb = n; 8051 sdebug_capacity = get_sdebug_capacity(); 8052 if (changed) { 8053 struct sdebug_host_info *sdhp; 8054 struct sdebug_dev_info *dp; 8055 8056 mutex_lock(&sdebug_host_list_mutex); 8057 list_for_each_entry(sdhp, &sdebug_host_list, 8058 host_list) { 8059 list_for_each_entry(dp, &sdhp->dev_info_list, 8060 dev_list) { 8061 set_bit(SDEBUG_UA_CAPACITY_CHANGED, 8062 dp->uas_bm); 8063 } 8064 } 8065 mutex_unlock(&sdebug_host_list_mutex); 8066 } 8067 return count; 8068 } 8069 return -EINVAL; 8070 } 8071 static DRIVER_ATTR_RW(virtual_gb); 8072 8073 static ssize_t add_host_show(struct device_driver *ddp, char *buf) 8074 { 8075 /* absolute number of hosts currently active is what is shown */ 8076 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_hosts); 8077 } 8078 8079 static ssize_t add_host_store(struct device_driver *ddp, const char *buf, 8080 size_t count) 8081 { 8082 bool found; 8083 unsigned long idx; 8084 struct sdeb_store_info *sip; 8085 bool want_phs = (sdebug_fake_rw == 0) && sdebug_per_host_store; 8086 int delta_hosts; 8087 8088 if (sscanf(buf, "%d", &delta_hosts) != 1) 8089 return -EINVAL; 8090 if (delta_hosts > 0) { 8091 do { 8092 found = false; 8093 if (want_phs) { 8094 xa_for_each_marked(per_store_ap, idx, sip, 8095 SDEB_XA_NOT_IN_USE) { 8096 sdeb_most_recent_idx = (int)idx; 8097 found = true; 8098 break; 8099 } 8100 if (found) /* re-use case */ 8101 sdebug_add_host_helper((int)idx); 8102 else 8103 sdebug_do_add_host(true); 8104 } else { 8105 sdebug_do_add_host(false); 8106 } 8107 } while (--delta_hosts); 8108 } else if (delta_hosts < 0) { 8109 do { 8110 sdebug_do_remove_host(false); 8111 } while (++delta_hosts); 8112 } 8113 return count; 8114 } 8115 static DRIVER_ATTR_RW(add_host); 8116 8117 static ssize_t vpd_use_hostno_show(struct device_driver *ddp, char *buf) 8118 { 8119 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno); 8120 } 8121 static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf, 8122 size_t count) 8123 { 8124 int n; 8125 8126 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { 8127 sdebug_vpd_use_hostno = n; 8128 return count; 8129 } 8130 return -EINVAL; 8131 } 8132 static DRIVER_ATTR_RW(vpd_use_hostno); 8133 8134 static ssize_t statistics_show(struct device_driver *ddp, char *buf) 8135 { 8136 return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_statistics); 8137 } 8138 static ssize_t statistics_store(struct device_driver *ddp, const char *buf, 8139 size_t count) 8140 { 8141 int n; 8142 8143 if ((count > 0) && (sscanf(buf, "%d", &n) == 1) && (n >= 0)) { 8144 if (n > 0) 8145 sdebug_statistics = true; 8146 else { 8147 clear_queue_stats(); 8148 sdebug_statistics = false; 8149 } 8150 return count; 8151 } 8152 return -EINVAL; 8153 } 8154 static DRIVER_ATTR_RW(statistics); 8155 8156 static ssize_t sector_size_show(struct device_driver *ddp, char *buf) 8157 { 8158 return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size); 8159 } 8160 static DRIVER_ATTR_RO(sector_size); 8161 8162 static ssize_t submit_queues_show(struct device_driver *ddp, char *buf) 8163 { 8164 return scnprintf(buf, PAGE_SIZE, "%d\n", submit_queues); 8165 } 8166 static DRIVER_ATTR_RO(submit_queues); 8167 8168 static ssize_t dix_show(struct device_driver *ddp, char *buf) 8169 { 8170 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix); 8171 } 8172 static DRIVER_ATTR_RO(dix); 8173 8174 static ssize_t dif_show(struct device_driver *ddp, char *buf) 8175 { 8176 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif); 8177 } 8178 static DRIVER_ATTR_RO(dif); 8179 8180 static ssize_t guard_show(struct device_driver *ddp, char *buf) 8181 { 8182 return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard); 8183 } 8184 static DRIVER_ATTR_RO(guard); 8185 8186 static ssize_t ato_show(struct device_driver *ddp, char *buf) 8187 { 8188 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato); 8189 } 8190 static DRIVER_ATTR_RO(ato); 8191 8192 static ssize_t map_show(struct device_driver *ddp, char *buf) 8193 { 8194 ssize_t count = 0; 8195 8196 if (!scsi_debug_lbp()) 8197 return scnprintf(buf, PAGE_SIZE, "0-%u\n", 8198 sdebug_store_sectors); 8199 8200 if (sdebug_fake_rw == 0 && !xa_empty(per_store_ap)) { 8201 struct sdeb_store_info *sip = xa_load(per_store_ap, 0); 8202 8203 if (sip) 8204 count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", 8205 (int)map_size, sip->map_storep); 8206 } 8207 buf[count++] = '\n'; 8208 buf[count] = '\0'; 8209 8210 return count; 8211 } 8212 static DRIVER_ATTR_RO(map); 8213 8214 static ssize_t random_show(struct device_driver *ddp, char *buf) 8215 { 8216 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_random); 8217 } 8218 8219 static ssize_t random_store(struct device_driver *ddp, const char *buf, 8220 size_t count) 8221 { 8222 bool v; 8223 8224 if (kstrtobool(buf, &v)) 8225 return -EINVAL; 8226 8227 sdebug_random = v; 8228 return count; 8229 } 8230 static DRIVER_ATTR_RW(random); 8231 8232 static ssize_t removable_show(struct device_driver *ddp, char *buf) 8233 { 8234 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0); 8235 } 8236 static ssize_t removable_store(struct device_driver *ddp, const char *buf, 8237 size_t count) 8238 { 8239 int n; 8240 8241 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { 8242 sdebug_removable = (n > 0); 8243 return count; 8244 } 8245 return -EINVAL; 8246 } 8247 static DRIVER_ATTR_RW(removable); 8248 8249 static ssize_t host_lock_show(struct device_driver *ddp, char *buf) 8250 { 8251 return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock); 8252 } 8253 /* N.B. sdebug_host_lock does nothing, kept for backward compatibility */ 8254 static ssize_t host_lock_store(struct device_driver *ddp, const char *buf, 8255 size_t count) 8256 { 8257 int n; 8258 8259 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { 8260 sdebug_host_lock = (n > 0); 8261 return count; 8262 } 8263 return -EINVAL; 8264 } 8265 static DRIVER_ATTR_RW(host_lock); 8266 8267 static ssize_t strict_show(struct device_driver *ddp, char *buf) 8268 { 8269 return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict); 8270 } 8271 static ssize_t strict_store(struct device_driver *ddp, const char *buf, 8272 size_t count) 8273 { 8274 int n; 8275 8276 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { 8277 sdebug_strict = (n > 0); 8278 return count; 8279 } 8280 return -EINVAL; 8281 } 8282 static DRIVER_ATTR_RW(strict); 8283 8284 static ssize_t uuid_ctl_show(struct device_driver *ddp, char *buf) 8285 { 8286 return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_uuid_ctl); 8287 } 8288 static DRIVER_ATTR_RO(uuid_ctl); 8289 8290 static ssize_t cdb_len_show(struct device_driver *ddp, char *buf) 8291 { 8292 return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_cdb_len); 8293 } 8294 static ssize_t cdb_len_store(struct device_driver *ddp, const char *buf, 8295 size_t count) 8296 { 8297 int ret, n; 8298 8299 ret = kstrtoint(buf, 0, &n); 8300 if (ret) 8301 return ret; 8302 sdebug_cdb_len = n; 8303 all_config_cdb_len(); 8304 return count; 8305 } 8306 static DRIVER_ATTR_RW(cdb_len); 8307 8308 static const char * const zbc_model_strs_a[] = { 8309 [BLK_ZONED_NONE] = "none", 8310 [BLK_ZONED_HA] = "host-aware", 8311 [BLK_ZONED_HM] = "host-managed", 8312 }; 8313 8314 static const char * const zbc_model_strs_b[] = { 8315 [BLK_ZONED_NONE] = "no", 8316 [BLK_ZONED_HA] = "aware", 8317 [BLK_ZONED_HM] = "managed", 8318 }; 8319 8320 static const char * const zbc_model_strs_c[] = { 8321 [BLK_ZONED_NONE] = "0", 8322 [BLK_ZONED_HA] = "1", 8323 [BLK_ZONED_HM] = "2", 8324 }; 8325 8326 static int sdeb_zbc_model_str(const char *cp) 8327 { 8328 int res = sysfs_match_string(zbc_model_strs_a, cp); 8329 8330 if (res < 0) { 8331 res = sysfs_match_string(zbc_model_strs_b, cp); 8332 if (res < 0) { 8333 res = sysfs_match_string(zbc_model_strs_c, cp); 8334 if (res < 0) 8335 return -EINVAL; 8336 } 8337 } 8338 return res; 8339 } 8340 8341 static ssize_t zbc_show(struct device_driver *ddp, char *buf) 8342 { 8343 return scnprintf(buf, PAGE_SIZE, "%s\n", 8344 zbc_model_strs_a[sdeb_zbc_model]); 8345 } 8346 static DRIVER_ATTR_RO(zbc); 8347 8348 static ssize_t tur_ms_to_ready_show(struct device_driver *ddp, char *buf) 8349 { 8350 return scnprintf(buf, PAGE_SIZE, "%d\n", sdeb_tur_ms_to_ready); 8351 } 8352 static DRIVER_ATTR_RO(tur_ms_to_ready); 8353 8354 static ssize_t group_number_stats_show(struct device_driver *ddp, char *buf) 8355 { 8356 char *p = buf, *end = buf + PAGE_SIZE; 8357 int i; 8358 8359 for (i = 0; i < ARRAY_SIZE(writes_by_group_number); i++) 8360 p += scnprintf(p, end - p, "%d %ld\n", i, 8361 atomic_long_read(&writes_by_group_number[i])); 8362 8363 return p - buf; 8364 } 8365 8366 static ssize_t group_number_stats_store(struct device_driver *ddp, 8367 const char *buf, size_t count) 8368 { 8369 int i; 8370 8371 for (i = 0; i < ARRAY_SIZE(writes_by_group_number); i++) 8372 atomic_long_set(&writes_by_group_number[i], 0); 8373 8374 return count; 8375 } 8376 static DRIVER_ATTR_RW(group_number_stats); 8377 8378 /* Note: The following array creates attribute files in the 8379 /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these 8380 files (over those found in the /sys/module/scsi_debug/parameters 8381 directory) is that auxiliary actions can be triggered when an attribute 8382 is changed. For example see: add_host_store() above. 8383 */ 8384 8385 static struct attribute *sdebug_drv_attrs[] = { 8386 &driver_attr_delay.attr, 8387 &driver_attr_opts.attr, 8388 &driver_attr_ptype.attr, 8389 &driver_attr_dsense.attr, 8390 &driver_attr_fake_rw.attr, 8391 &driver_attr_host_max_queue.attr, 8392 &driver_attr_no_lun_0.attr, 8393 &driver_attr_num_tgts.attr, 8394 &driver_attr_dev_size_mb.attr, 8395 &driver_attr_num_parts.attr, 8396 &driver_attr_every_nth.attr, 8397 &driver_attr_lun_format.attr, 8398 &driver_attr_max_luns.attr, 8399 &driver_attr_max_queue.attr, 8400 &driver_attr_no_rwlock.attr, 8401 &driver_attr_no_uld.attr, 8402 &driver_attr_scsi_level.attr, 8403 &driver_attr_virtual_gb.attr, 8404 &driver_attr_add_host.attr, 8405 &driver_attr_per_host_store.attr, 8406 &driver_attr_vpd_use_hostno.attr, 8407 &driver_attr_sector_size.attr, 8408 &driver_attr_statistics.attr, 8409 &driver_attr_submit_queues.attr, 8410 &driver_attr_dix.attr, 8411 &driver_attr_dif.attr, 8412 &driver_attr_guard.attr, 8413 &driver_attr_ato.attr, 8414 &driver_attr_map.attr, 8415 &driver_attr_random.attr, 8416 &driver_attr_removable.attr, 8417 &driver_attr_host_lock.attr, 8418 &driver_attr_ndelay.attr, 8419 &driver_attr_strict.attr, 8420 &driver_attr_uuid_ctl.attr, 8421 &driver_attr_cdb_len.attr, 8422 &driver_attr_tur_ms_to_ready.attr, 8423 &driver_attr_zbc.attr, 8424 &driver_attr_group_number_stats.attr, 8425 NULL, 8426 }; 8427 ATTRIBUTE_GROUPS(sdebug_drv); 8428 8429 static struct device *pseudo_primary; 8430 8431 static int __init scsi_debug_init(void) 8432 { 8433 bool want_store = (sdebug_fake_rw == 0); 8434 unsigned long sz; 8435 int k, ret, hosts_to_add; 8436 int idx = -1; 8437 8438 if (sdebug_ndelay >= 1000 * 1000 * 1000) { 8439 pr_warn("ndelay must be less than 1 second, ignored\n"); 8440 sdebug_ndelay = 0; 8441 } else if (sdebug_ndelay > 0) 8442 sdebug_jdelay = JDELAY_OVERRIDDEN; 8443 8444 switch (sdebug_sector_size) { 8445 case 512: 8446 case 1024: 8447 case 2048: 8448 case 4096: 8449 break; 8450 default: 8451 pr_err("invalid sector_size %d\n", sdebug_sector_size); 8452 return -EINVAL; 8453 } 8454 8455 switch (sdebug_dif) { 8456 case T10_PI_TYPE0_PROTECTION: 8457 break; 8458 case T10_PI_TYPE1_PROTECTION: 8459 case T10_PI_TYPE2_PROTECTION: 8460 case T10_PI_TYPE3_PROTECTION: 8461 have_dif_prot = true; 8462 break; 8463 8464 default: 8465 pr_err("dif must be 0, 1, 2 or 3\n"); 8466 return -EINVAL; 8467 } 8468 8469 if (sdebug_num_tgts < 0) { 8470 pr_err("num_tgts must be >= 0\n"); 8471 return -EINVAL; 8472 } 8473 8474 if (sdebug_guard > 1) { 8475 pr_err("guard must be 0 or 1\n"); 8476 return -EINVAL; 8477 } 8478 8479 if (sdebug_ato > 1) { 8480 pr_err("ato must be 0 or 1\n"); 8481 return -EINVAL; 8482 } 8483 8484 if (sdebug_physblk_exp > 15) { 8485 pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp); 8486 return -EINVAL; 8487 } 8488 8489 sdebug_lun_am = sdebug_lun_am_i; 8490 if (sdebug_lun_am > SAM_LUN_AM_FLAT) { 8491 pr_warn("Invalid LUN format %u, using default\n", (int)sdebug_lun_am); 8492 sdebug_lun_am = SAM_LUN_AM_PERIPHERAL; 8493 } 8494 8495 if (sdebug_max_luns > 256) { 8496 if (sdebug_max_luns > 16384) { 8497 pr_warn("max_luns can be no more than 16384, use default\n"); 8498 sdebug_max_luns = DEF_MAX_LUNS; 8499 } 8500 sdebug_lun_am = SAM_LUN_AM_FLAT; 8501 } 8502 8503 if (sdebug_lowest_aligned > 0x3fff) { 8504 pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned); 8505 return -EINVAL; 8506 } 8507 8508 if (submit_queues < 1) { 8509 pr_err("submit_queues must be 1 or more\n"); 8510 return -EINVAL; 8511 } 8512 8513 if ((sdebug_max_queue > SDEBUG_CANQUEUE) || (sdebug_max_queue < 1)) { 8514 pr_err("max_queue must be in range [1, %d]\n", SDEBUG_CANQUEUE); 8515 return -EINVAL; 8516 } 8517 8518 if ((sdebug_host_max_queue > SDEBUG_CANQUEUE) || 8519 (sdebug_host_max_queue < 0)) { 8520 pr_err("host_max_queue must be in range [0 %d]\n", 8521 SDEBUG_CANQUEUE); 8522 return -EINVAL; 8523 } 8524 8525 if (sdebug_host_max_queue && 8526 (sdebug_max_queue != sdebug_host_max_queue)) { 8527 sdebug_max_queue = sdebug_host_max_queue; 8528 pr_warn("fixing max submit queue depth to host max queue depth, %d\n", 8529 sdebug_max_queue); 8530 } 8531 8532 /* 8533 * check for host managed zoned block device specified with 8534 * ptype=0x14 or zbc=XXX. 8535 */ 8536 if (sdebug_ptype == TYPE_ZBC) { 8537 sdeb_zbc_model = BLK_ZONED_HM; 8538 } else if (sdeb_zbc_model_s && *sdeb_zbc_model_s) { 8539 k = sdeb_zbc_model_str(sdeb_zbc_model_s); 8540 if (k < 0) 8541 return k; 8542 sdeb_zbc_model = k; 8543 switch (sdeb_zbc_model) { 8544 case BLK_ZONED_NONE: 8545 case BLK_ZONED_HA: 8546 sdebug_ptype = TYPE_DISK; 8547 break; 8548 case BLK_ZONED_HM: 8549 sdebug_ptype = TYPE_ZBC; 8550 break; 8551 default: 8552 pr_err("Invalid ZBC model\n"); 8553 return -EINVAL; 8554 } 8555 } 8556 if (sdeb_zbc_model != BLK_ZONED_NONE) { 8557 sdeb_zbc_in_use = true; 8558 if (sdebug_dev_size_mb == DEF_DEV_SIZE_PRE_INIT) 8559 sdebug_dev_size_mb = DEF_ZBC_DEV_SIZE_MB; 8560 } 8561 8562 if (sdebug_dev_size_mb == DEF_DEV_SIZE_PRE_INIT) 8563 sdebug_dev_size_mb = DEF_DEV_SIZE_MB; 8564 if (sdebug_dev_size_mb < 1) 8565 sdebug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */ 8566 sz = (unsigned long)sdebug_dev_size_mb * 1048576; 8567 sdebug_store_sectors = sz / sdebug_sector_size; 8568 sdebug_capacity = get_sdebug_capacity(); 8569 8570 /* play around with geometry, don't waste too much on track 0 */ 8571 sdebug_heads = 8; 8572 sdebug_sectors_per = 32; 8573 if (sdebug_dev_size_mb >= 256) 8574 sdebug_heads = 64; 8575 else if (sdebug_dev_size_mb >= 16) 8576 sdebug_heads = 32; 8577 sdebug_cylinders_per = (unsigned long)sdebug_capacity / 8578 (sdebug_sectors_per * sdebug_heads); 8579 if (sdebug_cylinders_per >= 1024) { 8580 /* other LLDs do this; implies >= 1GB ram disk ... */ 8581 sdebug_heads = 255; 8582 sdebug_sectors_per = 63; 8583 sdebug_cylinders_per = (unsigned long)sdebug_capacity / 8584 (sdebug_sectors_per * sdebug_heads); 8585 } 8586 if (scsi_debug_lbp()) { 8587 sdebug_unmap_max_blocks = 8588 clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU); 8589 8590 sdebug_unmap_max_desc = 8591 clamp(sdebug_unmap_max_desc, 0U, 256U); 8592 8593 sdebug_unmap_granularity = 8594 clamp(sdebug_unmap_granularity, 1U, 0xffffffffU); 8595 8596 if (sdebug_unmap_alignment && 8597 sdebug_unmap_granularity <= 8598 sdebug_unmap_alignment) { 8599 pr_err("ERR: unmap_granularity <= unmap_alignment\n"); 8600 return -EINVAL; 8601 } 8602 } 8603 8604 xa_init_flags(per_store_ap, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ); 8605 if (want_store) { 8606 idx = sdebug_add_store(); 8607 if (idx < 0) 8608 return idx; 8609 } 8610 8611 pseudo_primary = root_device_register("pseudo_0"); 8612 if (IS_ERR(pseudo_primary)) { 8613 pr_warn("root_device_register() error\n"); 8614 ret = PTR_ERR(pseudo_primary); 8615 goto free_vm; 8616 } 8617 ret = bus_register(&pseudo_lld_bus); 8618 if (ret < 0) { 8619 pr_warn("bus_register error: %d\n", ret); 8620 goto dev_unreg; 8621 } 8622 ret = driver_register(&sdebug_driverfs_driver); 8623 if (ret < 0) { 8624 pr_warn("driver_register error: %d\n", ret); 8625 goto bus_unreg; 8626 } 8627 8628 hosts_to_add = sdebug_add_host; 8629 sdebug_add_host = 0; 8630 8631 sdebug_debugfs_root = debugfs_create_dir("scsi_debug", NULL); 8632 if (IS_ERR_OR_NULL(sdebug_debugfs_root)) 8633 pr_info("failed to create initial debugfs directory\n"); 8634 8635 for (k = 0; k < hosts_to_add; k++) { 8636 if (want_store && k == 0) { 8637 ret = sdebug_add_host_helper(idx); 8638 if (ret < 0) { 8639 pr_err("add_host_helper k=%d, error=%d\n", 8640 k, -ret); 8641 break; 8642 } 8643 } else { 8644 ret = sdebug_do_add_host(want_store && 8645 sdebug_per_host_store); 8646 if (ret < 0) { 8647 pr_err("add_host k=%d error=%d\n", k, -ret); 8648 break; 8649 } 8650 } 8651 } 8652 if (sdebug_verbose) 8653 pr_info("built %d host(s)\n", sdebug_num_hosts); 8654 8655 return 0; 8656 8657 bus_unreg: 8658 bus_unregister(&pseudo_lld_bus); 8659 dev_unreg: 8660 root_device_unregister(pseudo_primary); 8661 free_vm: 8662 sdebug_erase_store(idx, NULL); 8663 return ret; 8664 } 8665 8666 static void __exit scsi_debug_exit(void) 8667 { 8668 int k = sdebug_num_hosts; 8669 8670 for (; k; k--) 8671 sdebug_do_remove_host(true); 8672 driver_unregister(&sdebug_driverfs_driver); 8673 bus_unregister(&pseudo_lld_bus); 8674 root_device_unregister(pseudo_primary); 8675 8676 sdebug_erase_all_stores(false); 8677 xa_destroy(per_store_ap); 8678 debugfs_remove(sdebug_debugfs_root); 8679 } 8680 8681 device_initcall(scsi_debug_init); 8682 module_exit(scsi_debug_exit); 8683 8684 static void sdebug_release_adapter(struct device *dev) 8685 { 8686 struct sdebug_host_info *sdbg_host; 8687 8688 sdbg_host = dev_to_sdebug_host(dev); 8689 kfree(sdbg_host); 8690 } 8691 8692 /* idx must be valid, if sip is NULL then it will be obtained using idx */ 8693 static void sdebug_erase_store(int idx, struct sdeb_store_info *sip) 8694 { 8695 if (idx < 0) 8696 return; 8697 if (!sip) { 8698 if (xa_empty(per_store_ap)) 8699 return; 8700 sip = xa_load(per_store_ap, idx); 8701 if (!sip) 8702 return; 8703 } 8704 vfree(sip->map_storep); 8705 vfree(sip->dif_storep); 8706 vfree(sip->storep); 8707 xa_erase(per_store_ap, idx); 8708 kfree(sip); 8709 } 8710 8711 /* Assume apart_from_first==false only in shutdown case. */ 8712 static void sdebug_erase_all_stores(bool apart_from_first) 8713 { 8714 unsigned long idx; 8715 struct sdeb_store_info *sip = NULL; 8716 8717 xa_for_each(per_store_ap, idx, sip) { 8718 if (apart_from_first) 8719 apart_from_first = false; 8720 else 8721 sdebug_erase_store(idx, sip); 8722 } 8723 if (apart_from_first) 8724 sdeb_most_recent_idx = sdeb_first_idx; 8725 } 8726 8727 /* 8728 * Returns store xarray new element index (idx) if >=0 else negated errno. 8729 * Limit the number of stores to 65536. 8730 */ 8731 static int sdebug_add_store(void) 8732 { 8733 int res; 8734 u32 n_idx; 8735 unsigned long iflags; 8736 unsigned long sz = (unsigned long)sdebug_dev_size_mb * 1048576; 8737 struct sdeb_store_info *sip = NULL; 8738 struct xa_limit xal = { .max = 1 << 16, .min = 0 }; 8739 8740 sip = kzalloc_obj(*sip); 8741 if (!sip) 8742 return -ENOMEM; 8743 8744 xa_lock_irqsave(per_store_ap, iflags); 8745 res = __xa_alloc(per_store_ap, &n_idx, sip, xal, GFP_ATOMIC); 8746 if (unlikely(res < 0)) { 8747 xa_unlock_irqrestore(per_store_ap, iflags); 8748 kfree(sip); 8749 pr_warn("xa_alloc() errno=%d\n", -res); 8750 return res; 8751 } 8752 sdeb_most_recent_idx = n_idx; 8753 if (sdeb_first_idx < 0) 8754 sdeb_first_idx = n_idx; 8755 xa_unlock_irqrestore(per_store_ap, iflags); 8756 8757 res = -ENOMEM; 8758 sip->storep = vzalloc(sz); 8759 if (!sip->storep) { 8760 pr_err("user data oom\n"); 8761 goto err; 8762 } 8763 if (sdebug_num_parts > 0) 8764 sdebug_build_parts(sip->storep, sz); 8765 8766 /* DIF/DIX: what T10 calls Protection Information (PI) */ 8767 if (sdebug_dix) { 8768 int dif_size; 8769 8770 dif_size = sdebug_store_sectors * sizeof(struct t10_pi_tuple); 8771 sip->dif_storep = vmalloc(dif_size); 8772 8773 pr_info("dif_storep %u bytes @ %p\n", dif_size, 8774 sip->dif_storep); 8775 8776 if (!sip->dif_storep) { 8777 pr_err("DIX oom\n"); 8778 goto err; 8779 } 8780 memset(sip->dif_storep, 0xff, dif_size); 8781 } 8782 /* Logical Block Provisioning */ 8783 if (scsi_debug_lbp()) { 8784 map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1; 8785 sip->map_storep = vcalloc(BITS_TO_LONGS(map_size), 8786 sizeof(long)); 8787 8788 pr_info("%lu provisioning blocks\n", map_size); 8789 8790 if (!sip->map_storep) { 8791 pr_err("LBP map oom\n"); 8792 goto err; 8793 } 8794 8795 /* Map first 1KB for partition table */ 8796 if (sdebug_num_parts) 8797 map_region(sip, 0, 2); 8798 } 8799 8800 rwlock_init(&sip->macc_data_lck); 8801 rwlock_init(&sip->macc_meta_lck); 8802 rwlock_init(&sip->macc_sector_lck); 8803 return (int)n_idx; 8804 err: 8805 sdebug_erase_store((int)n_idx, sip); 8806 pr_warn("failed, errno=%d\n", -res); 8807 return res; 8808 } 8809 8810 static int sdebug_add_host_helper(int per_host_idx) 8811 { 8812 int k, devs_per_host, idx; 8813 int error = -ENOMEM; 8814 struct sdebug_host_info *sdbg_host; 8815 struct sdebug_dev_info *sdbg_devinfo, *tmp; 8816 8817 sdbg_host = kzalloc_obj(*sdbg_host); 8818 if (!sdbg_host) 8819 return -ENOMEM; 8820 idx = (per_host_idx < 0) ? sdeb_first_idx : per_host_idx; 8821 if (xa_get_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE)) 8822 xa_clear_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE); 8823 sdbg_host->si_idx = idx; 8824 8825 INIT_LIST_HEAD(&sdbg_host->dev_info_list); 8826 8827 devs_per_host = sdebug_num_tgts * sdebug_max_luns; 8828 for (k = 0; k < devs_per_host; k++) { 8829 sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL); 8830 if (!sdbg_devinfo) 8831 goto clean; 8832 } 8833 8834 mutex_lock(&sdebug_host_list_mutex); 8835 list_add_tail(&sdbg_host->host_list, &sdebug_host_list); 8836 mutex_unlock(&sdebug_host_list_mutex); 8837 8838 sdbg_host->dev.bus = &pseudo_lld_bus; 8839 sdbg_host->dev.parent = pseudo_primary; 8840 sdbg_host->dev.release = &sdebug_release_adapter; 8841 dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_num_hosts); 8842 8843 error = device_register(&sdbg_host->dev); 8844 if (error) { 8845 mutex_lock(&sdebug_host_list_mutex); 8846 list_del(&sdbg_host->host_list); 8847 mutex_unlock(&sdebug_host_list_mutex); 8848 goto clean; 8849 } 8850 8851 ++sdebug_num_hosts; 8852 return 0; 8853 8854 clean: 8855 list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, 8856 dev_list) { 8857 list_del(&sdbg_devinfo->dev_list); 8858 kfree(sdbg_devinfo->zstate); 8859 kfree(sdbg_devinfo); 8860 } 8861 if (sdbg_host->dev.release) 8862 put_device(&sdbg_host->dev); 8863 else 8864 kfree(sdbg_host); 8865 pr_warn("failed, errno=%d\n", -error); 8866 return error; 8867 } 8868 8869 static int sdebug_do_add_host(bool mk_new_store) 8870 { 8871 int ph_idx = sdeb_most_recent_idx; 8872 8873 if (mk_new_store) { 8874 ph_idx = sdebug_add_store(); 8875 if (ph_idx < 0) 8876 return ph_idx; 8877 } 8878 return sdebug_add_host_helper(ph_idx); 8879 } 8880 8881 static void sdebug_do_remove_host(bool the_end) 8882 { 8883 int idx = -1; 8884 struct sdebug_host_info *sdbg_host = NULL; 8885 struct sdebug_host_info *sdbg_host2; 8886 8887 mutex_lock(&sdebug_host_list_mutex); 8888 if (!list_empty(&sdebug_host_list)) { 8889 sdbg_host = list_entry(sdebug_host_list.prev, 8890 struct sdebug_host_info, host_list); 8891 idx = sdbg_host->si_idx; 8892 } 8893 if (!the_end && idx >= 0) { 8894 bool unique = true; 8895 8896 list_for_each_entry(sdbg_host2, &sdebug_host_list, host_list) { 8897 if (sdbg_host2 == sdbg_host) 8898 continue; 8899 if (idx == sdbg_host2->si_idx) { 8900 unique = false; 8901 break; 8902 } 8903 } 8904 if (unique) { 8905 xa_set_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE); 8906 if (idx == sdeb_most_recent_idx) 8907 --sdeb_most_recent_idx; 8908 } 8909 } 8910 if (sdbg_host) 8911 list_del(&sdbg_host->host_list); 8912 mutex_unlock(&sdebug_host_list_mutex); 8913 8914 if (!sdbg_host) 8915 return; 8916 8917 device_unregister(&sdbg_host->dev); 8918 --sdebug_num_hosts; 8919 } 8920 8921 static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth) 8922 { 8923 struct sdebug_dev_info *devip = sdev->hostdata; 8924 8925 if (!devip) 8926 return -ENODEV; 8927 8928 mutex_lock(&sdebug_host_list_mutex); 8929 block_unblock_all_queues(true); 8930 8931 if (qdepth > SDEBUG_CANQUEUE) { 8932 qdepth = SDEBUG_CANQUEUE; 8933 pr_warn("requested qdepth [%d] exceeds canqueue [%d], trim\n", 8934 qdepth, SDEBUG_CANQUEUE); 8935 } 8936 if (qdepth < 1) 8937 qdepth = 1; 8938 if (qdepth != sdev->queue_depth) 8939 scsi_change_queue_depth(sdev, qdepth); 8940 8941 block_unblock_all_queues(false); 8942 mutex_unlock(&sdebug_host_list_mutex); 8943 8944 if (SDEBUG_OPT_Q_NOISE & sdebug_opts) 8945 sdev_printk(KERN_INFO, sdev, "qdepth=%d\n", qdepth); 8946 8947 return sdev->queue_depth; 8948 } 8949 8950 static bool fake_timeout(struct scsi_cmnd *scp) 8951 { 8952 if (0 == (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth))) { 8953 if (sdebug_every_nth < -1) 8954 sdebug_every_nth = -1; 8955 if (SDEBUG_OPT_TIMEOUT & sdebug_opts) 8956 return true; /* ignore command causing timeout */ 8957 else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts && 8958 scsi_medium_access_command(scp)) 8959 return true; /* time out reads and writes */ 8960 } 8961 return false; 8962 } 8963 8964 /* Response to TUR or media access command when device stopped */ 8965 static int resp_not_ready(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) 8966 { 8967 int stopped_state; 8968 u64 diff_ns = 0; 8969 ktime_t now_ts = ktime_get_boottime(); 8970 struct scsi_device *sdp = scp->device; 8971 8972 stopped_state = atomic_read(&devip->stopped); 8973 if (stopped_state == 2) { 8974 if (ktime_to_ns(now_ts) > ktime_to_ns(devip->create_ts)) { 8975 diff_ns = ktime_to_ns(ktime_sub(now_ts, devip->create_ts)); 8976 if (diff_ns >= ((u64)sdeb_tur_ms_to_ready * 1000000)) { 8977 /* tur_ms_to_ready timer extinguished */ 8978 atomic_set(&devip->stopped, 0); 8979 return 0; 8980 } 8981 } 8982 mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x1); 8983 if (sdebug_verbose) 8984 sdev_printk(KERN_INFO, sdp, 8985 "%s: Not ready: in process of becoming ready\n", my_name); 8986 if (scp->cmnd[0] == TEST_UNIT_READY) { 8987 u64 tur_nanosecs_to_ready = (u64)sdeb_tur_ms_to_ready * 1000000; 8988 8989 if (diff_ns <= tur_nanosecs_to_ready) 8990 diff_ns = tur_nanosecs_to_ready - diff_ns; 8991 else 8992 diff_ns = tur_nanosecs_to_ready; 8993 /* As per 20-061r2 approved for spc6 by T10 on 20200716 */ 8994 do_div(diff_ns, 1000000); /* diff_ns becomes milliseconds */ 8995 scsi_set_sense_information(scp->sense_buffer, SCSI_SENSE_BUFFERSIZE, 8996 diff_ns); 8997 return check_condition_result; 8998 } 8999 } 9000 mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2); 9001 if (sdebug_verbose) 9002 sdev_printk(KERN_INFO, sdp, "%s: Not ready: initializing command required\n", 9003 my_name); 9004 return check_condition_result; 9005 } 9006 9007 static void sdebug_map_queues(struct Scsi_Host *shost) 9008 { 9009 int i, qoff; 9010 9011 if (shost->nr_hw_queues == 1) 9012 return; 9013 9014 for (i = 0, qoff = 0; i < HCTX_MAX_TYPES; i++) { 9015 struct blk_mq_queue_map *map = &shost->tag_set.map[i]; 9016 9017 map->nr_queues = 0; 9018 9019 if (i == HCTX_TYPE_DEFAULT) 9020 map->nr_queues = submit_queues - poll_queues; 9021 else if (i == HCTX_TYPE_POLL) 9022 map->nr_queues = poll_queues; 9023 9024 if (!map->nr_queues) { 9025 BUG_ON(i == HCTX_TYPE_DEFAULT); 9026 continue; 9027 } 9028 9029 map->queue_offset = qoff; 9030 blk_mq_map_queues(map); 9031 9032 qoff += map->nr_queues; 9033 } 9034 } 9035 9036 struct sdebug_blk_mq_poll_data { 9037 unsigned int queue_num; 9038 int *num_entries; 9039 }; 9040 9041 /* 9042 * We don't handle aborted commands here, but it does not seem possible to have 9043 * aborted polled commands from schedule_resp() 9044 */ 9045 static bool sdebug_blk_mq_poll_iter(struct request *rq, void *opaque) 9046 { 9047 struct sdebug_blk_mq_poll_data *data = opaque; 9048 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); 9049 struct sdebug_scsi_cmd *sdsc = scsi_cmd_priv(cmd); 9050 struct sdebug_defer *sd_dp; 9051 u32 unique_tag = blk_mq_unique_tag(rq); 9052 u16 hwq = blk_mq_unique_tag_to_hwq(unique_tag); 9053 unsigned long flags; 9054 int queue_num = data->queue_num; 9055 ktime_t time; 9056 9057 /* We're only interested in one queue for this iteration */ 9058 if (hwq != queue_num) 9059 return true; 9060 9061 /* Subsequent checks would fail if this failed, but check anyway */ 9062 if (!test_bit(SCMD_STATE_INFLIGHT, &cmd->state)) 9063 return true; 9064 9065 time = ktime_get_boottime(); 9066 9067 spin_lock_irqsave(&sdsc->lock, flags); 9068 sd_dp = &sdsc->sd_dp; 9069 if (sd_dp->defer_t != SDEB_DEFER_POLL) { 9070 spin_unlock_irqrestore(&sdsc->lock, flags); 9071 return true; 9072 } 9073 9074 if (time < sd_dp->cmpl_ts) { 9075 spin_unlock_irqrestore(&sdsc->lock, flags); 9076 return true; 9077 } 9078 spin_unlock_irqrestore(&sdsc->lock, flags); 9079 9080 if (sdebug_statistics) { 9081 atomic_inc(&sdebug_completions); 9082 if (raw_smp_processor_id() != sd_dp->issuing_cpu) 9083 atomic_inc(&sdebug_miss_cpus); 9084 } 9085 9086 scsi_done(cmd); /* callback to mid level */ 9087 (*data->num_entries)++; 9088 return true; 9089 } 9090 9091 static int sdebug_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num) 9092 { 9093 int num_entries = 0; 9094 struct sdebug_blk_mq_poll_data data = { 9095 .queue_num = queue_num, 9096 .num_entries = &num_entries, 9097 }; 9098 9099 blk_mq_tagset_busy_iter(&shost->tag_set, sdebug_blk_mq_poll_iter, 9100 &data); 9101 9102 if (num_entries > 0) 9103 atomic_add(num_entries, &sdeb_mq_poll_count); 9104 return num_entries; 9105 } 9106 9107 static int sdebug_timeout_cmd(struct scsi_cmnd *cmnd) 9108 { 9109 struct scsi_device *sdp = cmnd->device; 9110 struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata; 9111 struct sdebug_err_inject *err; 9112 unsigned char *cmd = cmnd->cmnd; 9113 int ret = 0; 9114 9115 if (devip == NULL) 9116 return 0; 9117 9118 rcu_read_lock(); 9119 list_for_each_entry_rcu(err, &devip->inject_err_list, list) { 9120 if (err->type == ERR_TMOUT_CMD && 9121 (err->cmd == cmd[0] || err->cmd == 0xff)) { 9122 ret = !!err->cnt; 9123 if (err->cnt < 0) 9124 err->cnt++; 9125 9126 rcu_read_unlock(); 9127 return ret; 9128 } 9129 } 9130 rcu_read_unlock(); 9131 9132 return 0; 9133 } 9134 9135 static int sdebug_fail_queue_cmd(struct scsi_cmnd *cmnd) 9136 { 9137 struct scsi_device *sdp = cmnd->device; 9138 struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata; 9139 struct sdebug_err_inject *err; 9140 unsigned char *cmd = cmnd->cmnd; 9141 int ret = 0; 9142 9143 if (devip == NULL) 9144 return 0; 9145 9146 rcu_read_lock(); 9147 list_for_each_entry_rcu(err, &devip->inject_err_list, list) { 9148 if (err->type == ERR_FAIL_QUEUE_CMD && 9149 (err->cmd == cmd[0] || err->cmd == 0xff)) { 9150 ret = err->cnt ? err->queuecmd_ret : 0; 9151 if (err->cnt < 0) 9152 err->cnt++; 9153 9154 rcu_read_unlock(); 9155 return ret; 9156 } 9157 } 9158 rcu_read_unlock(); 9159 9160 return 0; 9161 } 9162 9163 static int sdebug_fail_cmd(struct scsi_cmnd *cmnd, int *retval, 9164 struct sdebug_err_inject *info) 9165 { 9166 struct scsi_device *sdp = cmnd->device; 9167 struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata; 9168 struct sdebug_err_inject *err; 9169 unsigned char *cmd = cmnd->cmnd; 9170 int ret = 0; 9171 int result; 9172 9173 if (devip == NULL) 9174 return 0; 9175 9176 rcu_read_lock(); 9177 list_for_each_entry_rcu(err, &devip->inject_err_list, list) { 9178 if (err->type == ERR_FAIL_CMD && 9179 (err->cmd == cmd[0] || err->cmd == 0xff)) { 9180 if (!err->cnt) { 9181 rcu_read_unlock(); 9182 return 0; 9183 } 9184 9185 ret = !!err->cnt; 9186 rcu_read_unlock(); 9187 goto out_handle; 9188 } 9189 } 9190 rcu_read_unlock(); 9191 9192 return 0; 9193 9194 out_handle: 9195 if (err->cnt < 0) 9196 err->cnt++; 9197 mk_sense_buffer(cmnd, err->sense_key, err->asc, err->asq); 9198 result = err->status_byte | err->host_byte << 16 | err->driver_byte << 24; 9199 *info = *err; 9200 *retval = schedule_resp(cmnd, devip, result, NULL, 0, 0); 9201 9202 return ret; 9203 } 9204 9205 /* Process @scp, a request to abort a SCSI command by tag. */ 9206 static void scsi_debug_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *scp) 9207 { 9208 struct sdebug_internal_cmd *internal_cmd = scsi_cmd_priv(scp); 9209 struct sdebug_abort_cmd *abort_cmd = &internal_cmd->abort_cmd; 9210 const u32 unique_tag = abort_cmd->unique_tag; 9211 struct scsi_cmnd *to_be_aborted_scmd = 9212 scsi_host_find_tag(shost, unique_tag); 9213 struct sdebug_scsi_cmd *to_be_aborted_sdsc = 9214 scsi_cmd_priv(to_be_aborted_scmd); 9215 bool res = false; 9216 9217 if (!to_be_aborted_scmd) { 9218 pr_err("command with tag %#x not found\n", unique_tag); 9219 return; 9220 } 9221 9222 scoped_guard(spinlock_irqsave, &to_be_aborted_sdsc->lock) 9223 res = scsi_debug_stop_cmnd(to_be_aborted_scmd); 9224 9225 if (res) 9226 pr_info("aborted command with tag %#x\n", unique_tag); 9227 else 9228 pr_err("failed to abort command with tag %#x\n", unique_tag); 9229 9230 set_host_byte(scp, res ? DID_OK : DID_ERROR); 9231 } 9232 9233 static enum scsi_qc_status 9234 scsi_debug_process_reserved_command(struct Scsi_Host *shost, 9235 struct scsi_cmnd *scp) 9236 { 9237 struct sdebug_internal_cmd *internal_cmd = scsi_cmd_priv(scp); 9238 9239 switch (internal_cmd->type) { 9240 case SCSI_DEBUG_ABORT_CMD: 9241 scsi_debug_abort_cmd(shost, scp); 9242 break; 9243 default: 9244 WARN_ON_ONCE(true); 9245 set_host_byte(scp, DID_ERROR); 9246 break; 9247 } 9248 9249 scsi_done(scp); 9250 return 0; 9251 } 9252 9253 static enum scsi_qc_status scsi_debug_queuecommand(struct Scsi_Host *shost, 9254 struct scsi_cmnd *scp) 9255 { 9256 u8 sdeb_i; 9257 struct scsi_device *sdp = scp->device; 9258 const struct opcode_info_t *oip; 9259 const struct opcode_info_t *r_oip; 9260 struct sdebug_dev_info *devip; 9261 u8 *cmd = scp->cmnd; 9262 int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); 9263 int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *) = NULL; 9264 int k, na; 9265 int errsts = 0; 9266 u64 lun_index = sdp->lun & 0x3FFF; 9267 u32 flags; 9268 u16 sa; 9269 u8 opcode = cmd[0]; 9270 u32 devsel = sdebug_get_devsel(scp->device); 9271 bool has_wlun_rl; 9272 bool inject_now; 9273 int ret = 0; 9274 struct sdebug_err_inject err; 9275 9276 scsi_set_resid(scp, 0); 9277 if (sdebug_statistics) { 9278 atomic_inc(&sdebug_cmnd_count); 9279 inject_now = inject_on_this_cmd(); 9280 } else { 9281 inject_now = false; 9282 } 9283 if (unlikely(sdebug_verbose && 9284 !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) { 9285 char b[120]; 9286 int n, len, sb; 9287 9288 len = scp->cmd_len; 9289 sb = (int)sizeof(b); 9290 if (len > 32) 9291 strcpy(b, "too long, over 32 bytes"); 9292 else { 9293 for (k = 0, n = 0; k < len && n < sb; ++k) 9294 n += scnprintf(b + n, sb - n, "%02x ", 9295 (u32)cmd[k]); 9296 } 9297 sdev_printk(KERN_INFO, sdp, "%s: tag=%#x, cmd %s\n", my_name, 9298 blk_mq_unique_tag(scsi_cmd_to_rq(scp)), b); 9299 } 9300 if (unlikely(inject_now && (sdebug_opts & SDEBUG_OPT_HOST_BUSY))) 9301 return SCSI_MLQUEUE_HOST_BUSY; 9302 has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS); 9303 if (unlikely(lun_index >= sdebug_max_luns && !has_wlun_rl)) 9304 goto err_out; 9305 9306 sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */ 9307 oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */ 9308 devip = (struct sdebug_dev_info *)sdp->hostdata; 9309 if (unlikely(!devip)) { 9310 devip = find_build_dev_info(sdp); 9311 if (NULL == devip) 9312 goto err_out; 9313 } 9314 9315 if (sdebug_timeout_cmd(scp)) { 9316 scmd_printk(KERN_INFO, scp, "timeout command 0x%x\n", opcode); 9317 return 0; 9318 } 9319 9320 ret = sdebug_fail_queue_cmd(scp); 9321 if (ret) { 9322 scmd_printk(KERN_INFO, scp, "fail queue command 0x%x with 0x%x\n", 9323 opcode, ret); 9324 return ret; 9325 } 9326 9327 if (sdebug_fail_cmd(scp, &ret, &err)) { 9328 scmd_printk(KERN_INFO, scp, 9329 "fail command 0x%x with hostbyte=0x%x, " 9330 "driverbyte=0x%x, statusbyte=0x%x, " 9331 "sense_key=0x%x, asc=0x%x, asq=0x%x\n", 9332 opcode, err.host_byte, err.driver_byte, 9333 err.status_byte, err.sense_key, err.asc, err.asq); 9334 return ret; 9335 } 9336 9337 if (unlikely(inject_now && !atomic_read(&sdeb_inject_pending))) 9338 atomic_set(&sdeb_inject_pending, 1); 9339 9340 na = oip->num_attached; 9341 r_pfp = oip->pfp; 9342 if (na) { /* multiple commands with this opcode */ 9343 r_oip = oip; 9344 if (FF_SA & r_oip->flags) { 9345 if (F_SA_LOW & oip->flags) 9346 sa = 0x1f & cmd[1]; 9347 else 9348 sa = get_unaligned_be16(cmd + 8); 9349 for (k = 0; k <= na; oip = r_oip->arrp + k++) { 9350 if (opcode == oip->opcode && sa == oip->sa && 9351 (devsel & oip->devsel) != 0) 9352 break; 9353 } 9354 } else { /* since no service action only check opcode */ 9355 for (k = 0; k <= na; oip = r_oip->arrp + k++) { 9356 if (opcode == oip->opcode && 9357 (devsel & oip->devsel) != 0) 9358 break; 9359 } 9360 } 9361 if (k > na) { 9362 if (F_SA_LOW & r_oip->flags) 9363 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4); 9364 else if (F_SA_HIGH & r_oip->flags) 9365 mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7); 9366 else 9367 mk_sense_invalid_opcode(scp); 9368 goto check_cond; 9369 } 9370 } /* else (when na==0) we assume the oip is a match */ 9371 flags = oip->flags; 9372 if (unlikely(F_INV_OP & flags)) { 9373 mk_sense_invalid_opcode(scp); 9374 goto check_cond; 9375 } 9376 if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) { 9377 if (sdebug_verbose) 9378 sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n", 9379 my_name, opcode, " supported for wlun"); 9380 mk_sense_invalid_opcode(scp); 9381 goto check_cond; 9382 } 9383 if (unlikely(sdebug_strict)) { /* check cdb against mask */ 9384 u8 rem; 9385 int j; 9386 9387 for (k = 1; k < oip->len_mask[0] && k < 16; ++k) { 9388 rem = ~oip->len_mask[k] & cmd[k]; 9389 if (rem) { 9390 for (j = 7; j >= 0; --j, rem <<= 1) { 9391 if (0x80 & rem) 9392 break; 9393 } 9394 mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j); 9395 goto check_cond; 9396 } 9397 } 9398 } 9399 if (unlikely(!(F_SKIP_UA & flags) && 9400 find_first_bit(devip->uas_bm, 9401 SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) { 9402 errsts = make_ua(scp, devip); 9403 if (errsts) 9404 goto check_cond; 9405 } 9406 if (unlikely(((F_M_ACCESS & flags) || scp->cmnd[0] == TEST_UNIT_READY) && 9407 atomic_read(&devip->stopped))) { 9408 errsts = resp_not_ready(scp, devip); 9409 if (errsts) 9410 goto fini; 9411 } 9412 if (sdebug_fake_rw && (F_FAKE_RW & flags)) 9413 goto fini; 9414 if (unlikely(sdebug_every_nth)) { 9415 if (fake_timeout(scp)) 9416 return 0; /* ignore command: make trouble */ 9417 } 9418 if (likely(oip->pfp)) 9419 pfp = oip->pfp; /* calls a resp_* function */ 9420 else 9421 pfp = r_pfp; /* if leaf function ptr NULL, try the root's */ 9422 9423 fini: 9424 if (F_DELAY_OVERR & flags) /* cmds like INQUIRY respond asap */ 9425 return schedule_resp(scp, devip, errsts, pfp, 0, 0); 9426 else if ((flags & F_LONG_DELAY) && (sdebug_jdelay > 0 || 9427 sdebug_ndelay > 10000)) { 9428 /* 9429 * Skip long delays if ndelay <= 10 microseconds. Otherwise 9430 * for Start Stop Unit (SSU) want at least 1 second delay and 9431 * if sdebug_jdelay>1 want a long delay of that many seconds. 9432 * For Synchronize Cache want 1/20 of SSU's delay. 9433 */ 9434 int jdelay = (sdebug_jdelay < 2) ? 1 : sdebug_jdelay; 9435 int denom = (flags & F_SYNC_DELAY) ? 20 : 1; 9436 9437 jdelay = mult_frac(USER_HZ * jdelay, HZ, denom * USER_HZ); 9438 return schedule_resp(scp, devip, errsts, pfp, jdelay, 0); 9439 } else 9440 return schedule_resp(scp, devip, errsts, pfp, sdebug_jdelay, 9441 sdebug_ndelay); 9442 check_cond: 9443 return schedule_resp(scp, devip, check_condition_result, NULL, 0, 0); 9444 err_out: 9445 return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, NULL, 0, 0); 9446 } 9447 9448 static int sdebug_init_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd) 9449 { 9450 struct sdebug_scsi_cmd *sdsc = scsi_cmd_priv(cmd); 9451 struct sdebug_defer *sd_dp = &sdsc->sd_dp; 9452 9453 if (blk_mq_is_reserved_rq(scsi_cmd_to_rq(cmd))) 9454 return 0; 9455 9456 spin_lock_init(&sdsc->lock); 9457 hrtimer_setup(&sd_dp->hrt, sdebug_q_cmd_hrt_complete, CLOCK_MONOTONIC, 9458 HRTIMER_MODE_REL_PINNED); 9459 INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete); 9460 9461 return 0; 9462 } 9463 9464 static const struct scsi_host_template sdebug_driver_template = { 9465 .show_info = scsi_debug_show_info, 9466 .write_info = scsi_debug_write_info, 9467 .proc_name = sdebug_proc_name, 9468 .name = "SCSI DEBUG", 9469 .info = scsi_debug_info, 9470 .sdev_init = scsi_debug_sdev_init, 9471 .sdev_configure = scsi_debug_sdev_configure, 9472 .sdev_destroy = scsi_debug_sdev_destroy, 9473 .ioctl = scsi_debug_ioctl, 9474 .queuecommand = scsi_debug_queuecommand, 9475 .queue_reserved_command = scsi_debug_process_reserved_command, 9476 .change_queue_depth = sdebug_change_qdepth, 9477 .map_queues = sdebug_map_queues, 9478 .mq_poll = sdebug_blk_mq_poll, 9479 .eh_abort_handler = scsi_debug_abort, 9480 .eh_device_reset_handler = scsi_debug_device_reset, 9481 .eh_target_reset_handler = scsi_debug_target_reset, 9482 .eh_bus_reset_handler = scsi_debug_bus_reset, 9483 .eh_host_reset_handler = scsi_debug_host_reset, 9484 .can_queue = SDEBUG_CANQUEUE, 9485 .nr_reserved_cmds = 1, 9486 .this_id = 7, 9487 .sg_tablesize = SG_MAX_SEGMENTS, 9488 .cmd_per_lun = DEF_CMD_PER_LUN, 9489 .max_sectors = -1U, 9490 .max_segment_size = -1U, 9491 .module = THIS_MODULE, 9492 .skip_settle_delay = 1, 9493 .track_queue_depth = 1, 9494 .cmd_size = sizeof(union sdebug_priv), 9495 .init_cmd_priv = sdebug_init_cmd_priv, 9496 .target_alloc = sdebug_target_alloc, 9497 .target_destroy = sdebug_target_destroy, 9498 }; 9499 9500 static int sdebug_driver_probe(struct device *dev) 9501 { 9502 int error = 0; 9503 struct sdebug_host_info *sdbg_host; 9504 struct Scsi_Host *hpnt; 9505 int hprot; 9506 9507 sdbg_host = dev_to_sdebug_host(dev); 9508 9509 hpnt = scsi_host_alloc(&sdebug_driver_template, 0); 9510 if (NULL == hpnt) { 9511 pr_err("scsi_host_alloc failed\n"); 9512 error = -ENODEV; 9513 return error; 9514 } 9515 hpnt->can_queue = sdebug_max_queue; 9516 hpnt->cmd_per_lun = sdebug_max_queue; 9517 if (!sdebug_clustering) 9518 hpnt->dma_boundary = PAGE_SIZE - 1; 9519 9520 if (submit_queues > nr_cpu_ids) { 9521 pr_warn("%s: trim submit_queues (was %d) to nr_cpu_ids=%u\n", 9522 my_name, submit_queues, nr_cpu_ids); 9523 submit_queues = nr_cpu_ids; 9524 } 9525 /* 9526 * Decide whether to tell scsi subsystem that we want mq. The 9527 * following should give the same answer for each host. 9528 */ 9529 hpnt->nr_hw_queues = submit_queues; 9530 if (sdebug_host_max_queue) 9531 hpnt->host_tagset = 1; 9532 9533 /* poll queues are possible for nr_hw_queues > 1 */ 9534 if (hpnt->nr_hw_queues == 1 || (poll_queues < 1)) { 9535 pr_warn("%s: trim poll_queues to 0. poll_q/nr_hw = (%d/%d)\n", 9536 my_name, poll_queues, hpnt->nr_hw_queues); 9537 poll_queues = 0; 9538 } 9539 9540 /* 9541 * Poll queues don't need interrupts, but we need at least one I/O queue 9542 * left over for non-polled I/O. 9543 * If condition not met, trim poll_queues to 1 (just for simplicity). 9544 */ 9545 if (poll_queues >= submit_queues) { 9546 if (submit_queues < 3) 9547 pr_warn("%s: trim poll_queues to 1\n", my_name); 9548 else 9549 pr_warn("%s: trim poll_queues to 1. Perhaps try poll_queues=%d\n", 9550 my_name, submit_queues - 1); 9551 poll_queues = 1; 9552 } 9553 if (poll_queues) 9554 hpnt->nr_maps = 3; 9555 9556 sdbg_host->shost = hpnt; 9557 if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id)) 9558 hpnt->max_id = sdebug_num_tgts + 1; 9559 else 9560 hpnt->max_id = sdebug_num_tgts; 9561 /* = sdebug_max_luns; */ 9562 hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; 9563 9564 hprot = 0; 9565 9566 switch (sdebug_dif) { 9567 9568 case T10_PI_TYPE1_PROTECTION: 9569 hprot = SHOST_DIF_TYPE1_PROTECTION; 9570 if (sdebug_dix) 9571 hprot |= SHOST_DIX_TYPE1_PROTECTION; 9572 break; 9573 9574 case T10_PI_TYPE2_PROTECTION: 9575 hprot = SHOST_DIF_TYPE2_PROTECTION; 9576 if (sdebug_dix) 9577 hprot |= SHOST_DIX_TYPE2_PROTECTION; 9578 break; 9579 9580 case T10_PI_TYPE3_PROTECTION: 9581 hprot = SHOST_DIF_TYPE3_PROTECTION; 9582 if (sdebug_dix) 9583 hprot |= SHOST_DIX_TYPE3_PROTECTION; 9584 break; 9585 9586 default: 9587 if (sdebug_dix) 9588 hprot |= SHOST_DIX_TYPE0_PROTECTION; 9589 break; 9590 } 9591 9592 scsi_host_set_prot(hpnt, hprot); 9593 9594 if (have_dif_prot || sdebug_dix) 9595 pr_info("host protection%s%s%s%s%s%s%s\n", 9596 (hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "", 9597 (hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "", 9598 (hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "", 9599 (hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "", 9600 (hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "", 9601 (hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "", 9602 (hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : ""); 9603 9604 if (sdebug_guard == 1) 9605 scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP); 9606 else 9607 scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC); 9608 9609 sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts); 9610 if (sdebug_every_nth) /* need stats counters for every_nth */ 9611 sdebug_statistics = true; 9612 error = scsi_add_host(hpnt, &sdbg_host->dev); 9613 if (error) { 9614 pr_err("scsi_add_host failed\n"); 9615 error = -ENODEV; 9616 scsi_host_put(hpnt); 9617 } else { 9618 scsi_scan_host(hpnt); 9619 } 9620 9621 return error; 9622 } 9623 9624 static void sdebug_driver_remove(struct device *dev) 9625 { 9626 struct sdebug_host_info *sdbg_host; 9627 struct sdebug_dev_info *sdbg_devinfo, *tmp; 9628 9629 sdbg_host = dev_to_sdebug_host(dev); 9630 9631 scsi_remove_host(sdbg_host->shost); 9632 9633 list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, 9634 dev_list) { 9635 list_del(&sdbg_devinfo->dev_list); 9636 kfree(sdbg_devinfo->zstate); 9637 kfree(sdbg_devinfo); 9638 } 9639 9640 scsi_host_put(sdbg_host->shost); 9641 } 9642 9643 static const struct bus_type pseudo_lld_bus = { 9644 .name = "pseudo", 9645 .probe = sdebug_driver_probe, 9646 .remove = sdebug_driver_remove, 9647 .drv_groups = sdebug_drv_groups, 9648 }; 9649