Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *  Copyright (C) 1992  Eric Youngdale
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  Simulate a host adapter with 2 disks attached.  Do a lot of checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *  to make sure that we are not getting blocks mixed up, and PANIC if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *  anything out of the ordinary is seen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * Copyright (C) 2001 - 2020 Douglas Gilbert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *  For documentation see http://sg.danny.cz/sg/scsi_debug.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/genhd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/crc-t10dif.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/uuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <linux/t10-pi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/msdos_partition.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include <linux/xarray.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <linux/prefetch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include <net/checksum.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #include <scsi/scsi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #include <scsi/scsi_cmnd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #include <scsi/scsi_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #include <scsi/scsi_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #include <scsi/scsicam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #include <scsi/scsi_eh.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #include <scsi/scsi_tcq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #include <scsi/scsi_dbg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #include "sd.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) #include "scsi_logging.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) /* make sure inq_product_rev string corresponds to this version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #define SDEBUG_VERSION "0190"	/* format to fit INQUIRY revision field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static const char *sdebug_version_date = "20200710";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define MY_NAME "scsi_debug"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) /* Additional Sense Code (ASC) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) #define NO_ADDITIONAL_SENSE 0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #define LOGICAL_UNIT_NOT_READY 0x4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #define UNRECOVERED_READ_ERR 0x11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) #define PARAMETER_LIST_LENGTH_ERR 0x1a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #define INVALID_OPCODE 0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define LBA_OUT_OF_RANGE 0x21
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) #define INVALID_FIELD_IN_CDB 0x24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) #define INVALID_FIELD_IN_PARAM_LIST 0x26
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #define WRITE_PROTECTED 0x27
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #define UA_RESET_ASC 0x29
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #define UA_CHANGED_ASC 0x2a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) #define TARGET_CHANGED_ASC 0x3f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) #define LUNS_CHANGED_ASCQ 0x0e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #define INSUFF_RES_ASC 0x55
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) #define INSUFF_RES_ASCQ 0x3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) #define POWER_ON_RESET_ASCQ 0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) #define BUS_RESET_ASCQ 0x2	/* scsi bus reset occurred */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) #define MODE_CHANGED_ASCQ 0x1	/* mode parameters changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #define CAPACITY_CHANGED_ASCQ 0x9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) #define SAVING_PARAMS_UNSUP 0x39
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) #define TRANSPORT_PROBLEM 0x4b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #define THRESHOLD_EXCEEDED 0x5d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) #define LOW_POWER_COND_ON 0x5e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) #define MISCOMPARE_VERIFY_ASC 0x1d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) #define MICROCODE_CHANGED_ASCQ 0x1	/* with TARGET_CHANGED_ASC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #define MICROCODE_CHANGED_WO_RESET_ASCQ 0x16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) #define WRITE_ERROR_ASC 0xc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) #define UNALIGNED_WRITE_ASCQ 0x4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) #define WRITE_BOUNDARY_ASCQ 0x5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) #define READ_INVDATA_ASCQ 0x6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) #define READ_BOUNDARY_ASCQ 0x7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) #define INSUFF_ZONE_ASCQ 0xe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) /* Additional Sense Code Qualifier (ASCQ) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) #define ACK_NAK_TO 0x3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) /* Default values for driver parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) #define DEF_NUM_HOST   1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) #define DEF_NUM_TGTS   1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) #define DEF_MAX_LUNS   1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) /* With these defaults, this driver will make 1 host with 1 target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  * (id 0) containing 1 logical unit (lun 0). That is 1 device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) #define DEF_ATO 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) #define DEF_CDB_LEN 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) #define DEF_JDELAY   1		/* if > 0 unit is a jiffy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) #define DEF_DEV_SIZE_PRE_INIT   0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) #define DEF_DEV_SIZE_MB   8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) #define DEF_ZBC_DEV_SIZE_MB   128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) #define DEF_DIF 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) #define DEF_DIX 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) #define DEF_PER_HOST_STORE false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) #define DEF_D_SENSE   0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) #define DEF_EVERY_NTH   0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) #define DEF_FAKE_RW	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) #define DEF_GUARD 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) #define DEF_HOST_LOCK 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) #define DEF_LBPU 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) #define DEF_LBPWS 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) #define DEF_LBPWS10 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) #define DEF_LBPRZ 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) #define DEF_LOWEST_ALIGNED 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) #define DEF_NDELAY   0		/* if > 0 unit is a nanosecond */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) #define DEF_NO_LUN_0   0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) #define DEF_NUM_PARTS   0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) #define DEF_OPTS   0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) #define DEF_OPT_BLKS 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) #define DEF_PHYSBLK_EXP 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) #define DEF_OPT_XFERLEN_EXP 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) #define DEF_PTYPE   TYPE_DISK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) #define DEF_RANDOM false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) #define DEF_REMOVABLE false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) #define DEF_SCSI_LEVEL   7    /* INQUIRY, byte2 [6->SPC-4; 7->SPC-5] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) #define DEF_SECTOR_SIZE 512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) #define DEF_UNMAP_ALIGNMENT 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) #define DEF_UNMAP_GRANULARITY 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) #define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) #define DEF_UNMAP_MAX_DESC 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) #define DEF_VIRTUAL_GB   0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) #define DEF_VPD_USE_HOSTNO 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) #define DEF_WRITESAME_LENGTH 0xFFFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) #define DEF_STRICT 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) #define DEF_STATISTICS false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) #define DEF_SUBMIT_QUEUES 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) #define DEF_TUR_MS_TO_READY 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) #define DEF_UUID_CTL 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) #define JDELAY_OVERRIDDEN -9999
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) /* Default parameters for ZBC drives */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) #define DEF_ZBC_ZONE_SIZE_MB	128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) #define DEF_ZBC_MAX_OPEN_ZONES	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) #define DEF_ZBC_NR_CONV_ZONES	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) #define SDEBUG_LUN_0_VAL 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) /* bit mask values for sdebug_opts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) #define SDEBUG_OPT_NOISE		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) #define SDEBUG_OPT_MEDIUM_ERR		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) #define SDEBUG_OPT_TIMEOUT		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) #define SDEBUG_OPT_RECOVERED_ERR	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) #define SDEBUG_OPT_TRANSPORT_ERR	16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) #define SDEBUG_OPT_DIF_ERR		32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) #define SDEBUG_OPT_DIX_ERR		64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) #define SDEBUG_OPT_MAC_TIMEOUT		128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) #define SDEBUG_OPT_SHORT_TRANSFER	0x100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) #define SDEBUG_OPT_Q_NOISE		0x200
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) #define SDEBUG_OPT_ALL_TSF		0x400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) #define SDEBUG_OPT_RARE_TSF		0x800
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) #define SDEBUG_OPT_N_WCE		0x1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) #define SDEBUG_OPT_RESET_NOISE		0x2000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) #define SDEBUG_OPT_NO_CDB_NOISE		0x4000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) #define SDEBUG_OPT_HOST_BUSY		0x8000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) #define SDEBUG_OPT_CMD_ABORT		0x10000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) #define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 			      SDEBUG_OPT_RESET_NOISE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) #define SDEBUG_OPT_ALL_INJECTING (SDEBUG_OPT_RECOVERED_ERR | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 				  SDEBUG_OPT_TRANSPORT_ERR | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 				  SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 				  SDEBUG_OPT_SHORT_TRANSFER | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 				  SDEBUG_OPT_HOST_BUSY | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 				  SDEBUG_OPT_CMD_ABORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) #define SDEBUG_OPT_RECOV_DIF_DIX (SDEBUG_OPT_RECOVERED_ERR | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 				  SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) /* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs) are returned in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195)  * priority order. In the subset implemented here lower numbers have higher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196)  * priority. The UA numbers should be a sequence starting from 0 with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197)  * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) #define SDEBUG_UA_POR 0		/* Power on, reset, or bus device reset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) #define SDEBUG_UA_BUS_RESET 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) #define SDEBUG_UA_MODE_CHANGED 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) #define SDEBUG_UA_CAPACITY_CHANGED 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) #define SDEBUG_UA_LUNS_CHANGED 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) #define SDEBUG_UA_MICROCODE_CHANGED 5	/* simulate firmware change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) #define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) #define SDEBUG_NUM_UAS 7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) /* when 1==SDEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  * sector on read commands: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) #define OPT_MEDIUM_ERR_ADDR   0x1234 /* that's sector 4660 in decimal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) #define OPT_MEDIUM_ERR_NUM    10     /* number of consecutive medium errs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) /* SDEBUG_CANQUEUE is the maximum number of commands that can be queued
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  * (for response) per submit queue at one time. Can be reduced by max_queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  * option. Command responses are not queued when jdelay=0 and ndelay=0. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215)  * per-device DEF_CMD_PER_LUN can be changed via sysfs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216)  * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217)  * but cannot exceed SDEBUG_CANQUEUE .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) #define SDEBUG_CANQUEUE_WORDS  3	/* a WORD is bits in a long */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) #define SDEBUG_CANQUEUE  (SDEBUG_CANQUEUE_WORDS * BITS_PER_LONG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) #define DEF_CMD_PER_LUN  255
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) /* UA - Unit Attention; SA - Service Action; SSU - Start Stop Unit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) #define F_D_IN			1	/* Data-in command (e.g. READ) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) #define F_D_OUT			2	/* Data-out command (e.g. WRITE) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) #define F_D_OUT_MAYBE		4	/* WRITE SAME, NDOB bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) #define F_D_UNKN		8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) #define F_RL_WLUN_OK		0x10	/* allowed with REPORT LUNS W-LUN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) #define F_SKIP_UA		0x20	/* bypass UAs (e.g. INQUIRY command) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) #define F_DELAY_OVERR		0x40	/* for commands like INQUIRY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) #define F_SA_LOW		0x80	/* SA is in cdb byte 1, bits 4 to 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) #define F_SA_HIGH		0x100	/* SA is in cdb bytes 8 and 9 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) #define F_INV_OP		0x200	/* invalid opcode (not supported) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) #define F_FAKE_RW		0x400	/* bypass resp_*() when fake_rw set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) #define F_M_ACCESS		0x800	/* media access, reacts to SSU state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) #define F_SSU_DELAY		0x1000	/* SSU command delay (long-ish) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) #define F_SYNC_DELAY		0x2000	/* SYNCHRONIZE CACHE delay */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) /* Useful combinations of the above flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) #define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) #define FF_MEDIA_IO (F_M_ACCESS | F_FAKE_RW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) #define FF_SA (F_SA_HIGH | F_SA_LOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) #define F_LONG_DELAY		(F_SSU_DELAY | F_SYNC_DELAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) #define SDEBUG_MAX_PARTS 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) #define SDEBUG_MAX_CMD_LEN 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) #define SDEB_XA_NOT_IN_USE XA_MARK_1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) /* Zone types (zbcr05 table 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) enum sdebug_z_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	ZBC_ZONE_TYPE_CNV	= 0x1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	ZBC_ZONE_TYPE_SWR	= 0x2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	ZBC_ZONE_TYPE_SWP	= 0x3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) /* enumeration names taken from table 26, zbcr05 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) enum sdebug_z_cond {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	ZBC_NOT_WRITE_POINTER	= 0x0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	ZC1_EMPTY		= 0x1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	ZC2_IMPLICIT_OPEN	= 0x2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	ZC3_EXPLICIT_OPEN	= 0x3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	ZC4_CLOSED		= 0x4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	ZC6_READ_ONLY		= 0xd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	ZC5_FULL		= 0xe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	ZC7_OFFLINE		= 0xf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) struct sdeb_zone_state {	/* ZBC: per zone state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	enum sdebug_z_type z_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	enum sdebug_z_cond z_cond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	bool z_non_seq_resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	unsigned int z_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	sector_t z_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	sector_t z_wp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) struct sdebug_dev_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	struct list_head dev_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	unsigned int channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	unsigned int target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	u64 lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	uuid_t lu_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	struct sdebug_host_info *sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	unsigned long uas_bm[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	atomic_t num_in_q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	atomic_t stopped;	/* 1: by SSU, 2: device start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	bool used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	/* For ZBC devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	enum blk_zoned_model zmodel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	unsigned int zsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	unsigned int zsize_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	unsigned int nr_zones;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	unsigned int nr_conv_zones;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	unsigned int nr_imp_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	unsigned int nr_exp_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	unsigned int nr_closed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	unsigned int max_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	ktime_t create_ts;	/* time since bootup that this device was created */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	struct sdeb_zone_state *zstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) struct sdebug_host_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	struct list_head host_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	int si_idx;	/* sdeb_store_info (per host) xarray index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	struct Scsi_Host *shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	struct device dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	struct list_head dev_info_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) /* There is an xarray of pointers to this struct's objects, one per host */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) struct sdeb_store_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	rwlock_t macc_lck;	/* for atomic media access on this store */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	u8 *storep;		/* user data storage (ram) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	struct t10_pi_tuple *dif_storep; /* protection info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	void *map_storep;	/* provisioning map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) #define to_sdebug_host(d)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	container_of(d, struct sdebug_host_info, dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) enum sdeb_defer_type {SDEB_DEFER_NONE = 0, SDEB_DEFER_HRT = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		      SDEB_DEFER_WQ = 2};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) struct sdebug_defer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	struct hrtimer hrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	struct execute_work ew;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	int sqa_idx;	/* index of sdebug_queue array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	int qc_idx;	/* index of sdebug_queued_cmd array within sqa_idx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	int hc_idx;	/* hostwide tag index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	int issuing_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	bool init_hrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	bool init_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	bool aborted;	/* true when blk_abort_request() already called */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	enum sdeb_defer_type defer_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) struct sdebug_queued_cmd {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	/* corresponding bit set in in_use_bm[] in owning struct sdebug_queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	 * instance indicates this slot is in use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	struct sdebug_defer *sd_dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	struct scsi_cmnd *a_cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) struct sdebug_queue {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	struct sdebug_queued_cmd qc_arr[SDEBUG_CANQUEUE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	unsigned long in_use_bm[SDEBUG_CANQUEUE_WORDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	spinlock_t qc_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	atomic_t blocked;	/* to temporarily stop more being queued */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) static atomic_t sdebug_cmnd_count;   /* number of incoming commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) static atomic_t sdebug_completions;  /* count of deferred completions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) static atomic_t sdebug_miss_cpus;    /* submission + completion cpus differ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) static atomic_t sdebug_a_tsf;	     /* 'almost task set full' counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) static atomic_t sdeb_inject_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) struct opcode_info_t {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	u8 num_attached;	/* 0 if this is it (i.e. a leaf); use 0xff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 				/* for terminating element */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	u8 opcode;		/* if num_attached > 0, preferred */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	u16 sa;			/* service action */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	u32 flags;		/* OR-ed set of SDEB_F_* */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	const struct opcode_info_t *arrp;  /* num_attached elements or NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	u8 len_mask[16];	/* len_mask[0]-->cdb_len, then mask for cdb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 				/* 1 to min(cdb_len, 15); ignore cdb[15...] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) /* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) enum sdeb_opcode_index {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	SDEB_I_INVALID_OPCODE =	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	SDEB_I_INQUIRY = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	SDEB_I_REPORT_LUNS = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	SDEB_I_REQUEST_SENSE = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	SDEB_I_TEST_UNIT_READY = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	SDEB_I_MODE_SENSE = 5,		/* 6, 10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	SDEB_I_MODE_SELECT = 6,		/* 6, 10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	SDEB_I_LOG_SENSE = 7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	SDEB_I_READ_CAPACITY = 8,	/* 10; 16 is in SA_IN(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	SDEB_I_READ = 9,		/* 6, 10, 12, 16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	SDEB_I_WRITE = 10,		/* 6, 10, 12, 16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	SDEB_I_START_STOP = 11,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	SDEB_I_SERV_ACT_IN_16 = 12,	/* add ...SERV_ACT_IN_12 if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	SDEB_I_SERV_ACT_OUT_16 = 13,	/* add ...SERV_ACT_OUT_12 if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	SDEB_I_MAINT_IN = 14,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	SDEB_I_MAINT_OUT = 15,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	SDEB_I_VERIFY = 16,		/* VERIFY(10), VERIFY(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	SDEB_I_VARIABLE_LEN = 17,	/* READ(32), WRITE(32), WR_SCAT(32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	SDEB_I_RESERVE = 18,		/* 6, 10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	SDEB_I_RELEASE = 19,		/* 6, 10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	SDEB_I_ALLOW_REMOVAL = 20,	/* PREVENT ALLOW MEDIUM REMOVAL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	SDEB_I_REZERO_UNIT = 21,	/* REWIND in SSC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	SDEB_I_ATA_PT = 22,		/* 12, 16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	SDEB_I_SEND_DIAG = 23,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	SDEB_I_UNMAP = 24,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	SDEB_I_WRITE_BUFFER = 25,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	SDEB_I_WRITE_SAME = 26,		/* 10, 16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	SDEB_I_SYNC_CACHE = 27,		/* 10, 16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	SDEB_I_COMP_WRITE = 28,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	SDEB_I_PRE_FETCH = 29,		/* 10, 16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	SDEB_I_ZONE_OUT = 30,		/* 0x94+SA; includes no data xfer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	SDEB_I_ZONE_IN = 31,		/* 0x95+SA; all have data-in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	SDEB_I_LAST_ELEM_P1 = 32,	/* keep this last (previous + 1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) static const unsigned char opcode_ind_arr[256] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) /* 0x0; 0x0->0x1f: 6 byte cdbs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	    0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	    SDEB_I_RELEASE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	    SDEB_I_ALLOW_REMOVAL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) /* 0x20; 0x20->0x3f: 10 byte cdbs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	0, 0, 0, 0, SDEB_I_PRE_FETCH, SDEB_I_SYNC_CACHE, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) /* 0x40; 0x40->0x5f: 10 byte cdbs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	0, 0, 0, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	    SDEB_I_RELEASE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) /* 0x60; 0x60->0x7d are reserved, 0x7e is "extended cdb" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	0, SDEB_I_VARIABLE_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) /* 0x80; 0x80->0x9f: 16 byte cdbs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	0, 0, 0, SDEB_I_VERIFY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	SDEB_I_PRE_FETCH, SDEB_I_SYNC_CACHE, 0, SDEB_I_WRITE_SAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	SDEB_I_ZONE_OUT, SDEB_I_ZONE_IN, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	0, 0, 0, 0, 0, 0, SDEB_I_SERV_ACT_IN_16, SDEB_I_SERV_ACT_OUT_16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) /* 0xa0; 0xa0->0xbf: 12 byte cdbs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	     SDEB_I_MAINT_OUT, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	SDEB_I_READ, 0 /* SDEB_I_SERV_ACT_OUT_12 */, SDEB_I_WRITE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	     0 /* SDEB_I_SERV_ACT_IN_12 */, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) /* 0xc0; 0xc0->0xff: vendor specific */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457)  * The following "response" functions return the SCSI mid-level's 4 byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458)  * tuple-in-an-int. To handle commands with an IMMED bit, for a faster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459)  * command completion, they can mask their return value with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460)  * SDEG_RES_IMMED_MASK .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) #define SDEG_RES_IMMED_MASK 0x40000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) static int resp_write_scat(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) static int resp_verify(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) static int resp_comp_write(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) static int resp_sync_cache(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) static int resp_pre_fetch(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) static int resp_report_zones(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) static int resp_open_zone(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) static int resp_close_zone(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) static int resp_finish_zone(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) static int resp_rwp_zone(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) static int sdebug_do_add_host(bool mk_new_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) static int sdebug_add_host_helper(int per_host_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) static void sdebug_do_remove_host(bool the_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) static int sdebug_add_store(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) static void sdebug_erase_store(int idx, struct sdeb_store_info *sip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) static void sdebug_erase_all_stores(bool apart_from_first);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502)  * The following are overflow arrays for cdbs that "hit" the same index in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503)  * the opcode_info_arr array. The most time sensitive (or commonly used) cdb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504)  * should be placed in opcode_info_arr[], the others should be placed here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) static const struct opcode_info_t msense_iarr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	{0, 0x1a, 0, F_D_IN, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	    {6,  0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) static const struct opcode_info_t mselect_iarr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	{0, 0x15, 0, F_D_OUT, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	    {6,  0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) static const struct opcode_info_t read_iarr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	{0, 0x28, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	    {10,  0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	     0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	{0, 0x8, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL, /* READ(6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	    {6,  0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	{0, 0xa8, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	    {12,  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	     0xc7, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) static const struct opcode_info_t write_iarr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	{0, 0x2a, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0,  /* WRITE(10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	    NULL, {10,  0xfb, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		   0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	{0, 0xa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0,   /* WRITE(6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	    NULL, {6,  0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		   0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	{0, 0xaa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0,  /* WRITE(12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	    NULL, {12,  0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		   0xbf, 0xc7, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) static const struct opcode_info_t verify_iarr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	{0, 0x2f, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, resp_verify,/* VERIFY(10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	    NULL, {10,  0xf7, 0xff, 0xff, 0xff, 0xff, 0xbf, 0xff, 0xff, 0xc7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		   0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) static const struct opcode_info_t sa_in_16_iarr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	{0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	    {16,  0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	     0xff, 0xff, 0xff, 0, 0xc7} },	/* GET LBA STATUS(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) static const struct opcode_info_t vl_iarr[] = {	/* VARIABLE LENGTH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	{0, 0x7f, 0xb, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_dt0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	    NULL, {32,  0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0xb, 0xfa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		   0, 0xff, 0xff, 0xff, 0xff} },	/* WRITE(32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	{0, 0x7f, 0x11, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_scat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	    NULL, {32,  0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x11, 0xf8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		   0, 0xff, 0xff, 0x0, 0x0} },	/* WRITE SCATTERED(32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) static const struct opcode_info_t maint_in_iarr[] = {	/* MAINT IN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	{0, 0xa3, 0xc, F_SA_LOW | F_D_IN, resp_rsup_opcodes, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	    {12,  0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	     0xc7, 0, 0, 0, 0} }, /* REPORT SUPPORTED OPERATION CODES */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	{0, 0xa3, 0xd, F_SA_LOW | F_D_IN, resp_rsup_tmfs, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	    {12,  0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	     0, 0} },	/* REPORTED SUPPORTED TASK MANAGEMENT FUNCTIONS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) static const struct opcode_info_t write_same_iarr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	{0, 0x93, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, resp_write_same_16, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	    {16,  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	     0xff, 0xff, 0xff, 0x3f, 0xc7} },		/* WRITE SAME(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) static const struct opcode_info_t reserve_iarr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	{0, 0x16, 0, F_D_OUT, NULL, NULL,		/* RESERVE(6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	    {6,  0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) static const struct opcode_info_t release_iarr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	{0, 0x17, 0, F_D_OUT, NULL, NULL,		/* RELEASE(6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	    {6,  0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) static const struct opcode_info_t sync_cache_iarr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	{0, 0x91, 0, F_SYNC_DELAY | F_M_ACCESS, resp_sync_cache, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	    {16,  0x6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	     0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} },	/* SYNC_CACHE (16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) static const struct opcode_info_t pre_fetch_iarr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	{0, 0x90, 0, F_SYNC_DELAY | FF_MEDIA_IO, resp_pre_fetch, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	    {16,  0x2, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	     0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} },	/* PRE-FETCH (16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) static const struct opcode_info_t zone_out_iarr[] = {	/* ZONE OUT(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	{0, 0x94, 0x1, F_SA_LOW | F_M_ACCESS, resp_close_zone, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	    {16, 0x1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	     0xff, 0, 0, 0xff, 0xff, 0x1, 0xc7} },	/* CLOSE ZONE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	{0, 0x94, 0x2, F_SA_LOW | F_M_ACCESS, resp_finish_zone, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	    {16, 0x2, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	     0xff, 0, 0, 0xff, 0xff, 0x1, 0xc7} },	/* FINISH ZONE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	{0, 0x94, 0x4, F_SA_LOW | F_M_ACCESS, resp_rwp_zone, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	    {16, 0x4, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	     0xff, 0, 0, 0xff, 0xff, 0x1, 0xc7} },  /* RESET WRITE POINTER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) static const struct opcode_info_t zone_in_iarr[] = {	/* ZONE IN(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	{0, 0x95, 0x6, F_SA_LOW | F_D_IN | F_M_ACCESS, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	    {16, 0x6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	     0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* REPORT ZONES */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) /* This array is accessed via SDEB_I_* values. Make sure all are mapped,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617)  * plus the terminating elements for logic that scans this table such as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618)  * REPORT SUPPORTED OPERATION CODES. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEM_P1 + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) /* 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	{0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL,	/* unknown opcodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	    {0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	{0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL, /* INQUIRY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	    {6,  0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	{0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	    {12,  0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	     0, 0} },					/* REPORT LUNS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	{0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	    {6,  0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	{0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	    {6,  0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) /* 5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	{ARRAY_SIZE(msense_iarr), 0x5a, 0, F_D_IN,	/* MODE SENSE(10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	    resp_mode_sense, msense_iarr, {10,  0xf8, 0xff, 0xff, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	{ARRAY_SIZE(mselect_iarr), 0x55, 0, F_D_OUT,	/* MODE SELECT(10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	    resp_mode_select, mselect_iarr, {10,  0xf1, 0, 0, 0, 0, 0, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	{0, 0x4d, 0, F_D_IN, resp_log_sense, NULL,	/* LOG SENSE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	    {10,  0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	     0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	{0, 0x25, 0, F_D_IN, resp_readcap, NULL,    /* READ CAPACITY(10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	    {10,  0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	     0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	{ARRAY_SIZE(read_iarr), 0x88, 0, F_D_IN | FF_MEDIA_IO, /* READ(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	    resp_read_dt0, read_iarr, {16,  0xfe, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) /* 10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	{ARRAY_SIZE(write_iarr), 0x8a, 0, F_D_OUT | FF_MEDIA_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	    resp_write_dt0, write_iarr,			/* WRITE(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		{16,  0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	{0, 0x1b, 0, F_SSU_DELAY, resp_start_stop, NULL,/* START STOP UNIT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	    {6,  0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	{ARRAY_SIZE(sa_in_16_iarr), 0x9e, 0x10, F_SA_LOW | F_D_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	    resp_readcap16, sa_in_16_iarr, /* SA_IN(16), READ CAPACITY(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		{16,  0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		 0xff, 0xff, 0xff, 0xff, 0x1, 0xc7} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	{0, 0x9f, 0x12, F_SA_LOW | F_D_OUT | FF_MEDIA_IO, resp_write_scat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	    NULL, {16,  0x12, 0xf9, 0x0, 0xff, 0xff, 0, 0, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	    0xff, 0xff, 0xff, 0xff, 0xc7} },  /* SA_OUT(16), WRITE SCAT(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	{ARRAY_SIZE(maint_in_iarr), 0xa3, 0xa, F_SA_LOW | F_D_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	    resp_report_tgtpgs,	/* MAINT IN, REPORT TARGET PORT GROUPS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		maint_in_iarr, {12,  0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 				0xff, 0, 0xc7, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) /* 15 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	{0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	    {0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	{ARRAY_SIZE(verify_iarr), 0x8f, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	    F_D_OUT_MAYBE | FF_MEDIA_IO, resp_verify,	/* VERIFY(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	    verify_iarr, {16,  0xf6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 			  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	{ARRAY_SIZE(vl_iarr), 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_MEDIA_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	    resp_read_dt0, vl_iarr,	/* VARIABLE LENGTH, READ(32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	    {32,  0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x9, 0xfe, 0, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	     0xff, 0xff} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	{ARRAY_SIZE(reserve_iarr), 0x56, 0, F_D_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	    NULL, reserve_iarr,	/* RESERVE(10) <no response function> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	    {10,  0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	     0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	{ARRAY_SIZE(release_iarr), 0x57, 0, F_D_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	    NULL, release_iarr, /* RELEASE(10) <no response function> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	    {10,  0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	     0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) /* 20 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	{0, 0x1e, 0, 0, NULL, NULL, /* ALLOW REMOVAL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	    {6,  0, 0, 0, 0x3, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	{0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	    {6,  0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	{0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	    {0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	{0, 0x1d, F_D_OUT, 0, NULL, NULL,	/* SEND DIAGNOSTIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	    {6,  0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	{0, 0x42, 0, F_D_OUT | FF_MEDIA_IO, resp_unmap, NULL, /* UNMAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	    {10,  0x1, 0, 0, 0, 0, 0x3f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) /* 25 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	{0, 0x3b, 0, F_D_OUT_MAYBE, resp_write_buffer, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	    {10,  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	     0, 0, 0, 0} },			/* WRITE_BUFFER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	{ARRAY_SIZE(write_same_iarr), 0x41, 0, F_D_OUT_MAYBE | FF_MEDIA_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	    resp_write_same_10, write_same_iarr,	/* WRITE SAME(10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		{10,  0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	{ARRAY_SIZE(sync_cache_iarr), 0x35, 0, F_SYNC_DELAY | F_M_ACCESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	    resp_sync_cache, sync_cache_iarr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	    {10,  0x7, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	     0, 0, 0, 0} },			/* SYNC_CACHE (10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	{0, 0x89, 0, F_D_OUT | FF_MEDIA_IO, resp_comp_write, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	    {16,  0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	     0, 0xff, 0x3f, 0xc7} },		/* COMPARE AND WRITE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	{ARRAY_SIZE(pre_fetch_iarr), 0x34, 0, F_SYNC_DELAY | FF_MEDIA_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	    resp_pre_fetch, pre_fetch_iarr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	    {10,  0x2, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	     0, 0, 0, 0} },			/* PRE-FETCH (10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) /* 30 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	{ARRAY_SIZE(zone_out_iarr), 0x94, 0x3, F_SA_LOW | F_M_ACCESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	    resp_open_zone, zone_out_iarr, /* ZONE_OUT(16), OPEN ZONE) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		{16,  0x3 /* SA */, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		 0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x1, 0xc7} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	{ARRAY_SIZE(zone_in_iarr), 0x95, 0x0, F_SA_LOW | F_M_ACCESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	    resp_report_zones, zone_in_iarr, /* ZONE_IN(16), REPORT ZONES) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		{16,  0x0 /* SA */, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, 0xc7} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) /* sentinel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	{0xff, 0, 0, 0, NULL, NULL,		/* terminating element */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	    {0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) static int sdebug_num_hosts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) static int sdebug_add_host = DEF_NUM_HOST;  /* in sysfs this is relative */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) static int sdebug_ato = DEF_ATO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) static int sdebug_cdb_len = DEF_CDB_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) static int sdebug_jdelay = DEF_JDELAY;	/* if > 0 then unit is jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) static int sdebug_dev_size_mb = DEF_DEV_SIZE_PRE_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) static int sdebug_dif = DEF_DIF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) static int sdebug_dix = DEF_DIX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) static int sdebug_dsense = DEF_D_SENSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) static int sdebug_every_nth = DEF_EVERY_NTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) static int sdebug_fake_rw = DEF_FAKE_RW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) static unsigned int sdebug_guard = DEF_GUARD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) static int sdebug_host_max_queue;	/* per host */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) static int sdebug_max_luns = DEF_MAX_LUNS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) static int sdebug_max_queue = SDEBUG_CANQUEUE;	/* per submit queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) static unsigned int sdebug_medium_error_start = OPT_MEDIUM_ERR_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) static int sdebug_medium_error_count = OPT_MEDIUM_ERR_NUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) static atomic_t retired_max_queue;	/* if > 0 then was prior max_queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) static int sdebug_ndelay = DEF_NDELAY;	/* if > 0 then unit is nanoseconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) static int sdebug_no_lun_0 = DEF_NO_LUN_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) static int sdebug_no_uld;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) static int sdebug_num_parts = DEF_NUM_PARTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) static int sdebug_opt_blks = DEF_OPT_BLKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) static int sdebug_opts = DEF_OPTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) static int sdebug_physblk_exp = DEF_PHYSBLK_EXP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) static int sdebug_opt_xferlen_exp = DEF_OPT_XFERLEN_EXP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral device type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) static int sdebug_scsi_level = DEF_SCSI_LEVEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) static int sdebug_sector_size = DEF_SECTOR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) static int sdeb_tur_ms_to_ready = DEF_TUR_MS_TO_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) static int sdebug_virtual_gb = DEF_VIRTUAL_GB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) static unsigned int sdebug_lbpu = DEF_LBPU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) static unsigned int sdebug_lbpws = DEF_LBPWS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) static unsigned int sdebug_lbpws10 = DEF_LBPWS10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) static unsigned int sdebug_lbprz = DEF_LBPRZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) static int sdebug_uuid_ctl = DEF_UUID_CTL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) static bool sdebug_random = DEF_RANDOM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) static bool sdebug_per_host_store = DEF_PER_HOST_STORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) static bool sdebug_removable = DEF_REMOVABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) static bool sdebug_clustering;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) static bool sdebug_host_lock = DEF_HOST_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) static bool sdebug_strict = DEF_STRICT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) static bool sdebug_any_injecting_opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) static bool sdebug_verbose;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) static bool have_dif_prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) static bool write_since_sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) static bool sdebug_statistics = DEF_STATISTICS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) static bool sdebug_wp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) /* Following enum: 0: no zbc, def; 1: host aware; 2: host managed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) static enum blk_zoned_model sdeb_zbc_model = BLK_ZONED_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) static char *sdeb_zbc_model_s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) enum sam_lun_addr_method {SAM_LUN_AM_PERIPHERAL = 0x0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 			  SAM_LUN_AM_FLAT = 0x1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 			  SAM_LUN_AM_LOGICAL_UNIT = 0x2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 			  SAM_LUN_AM_EXTENDED = 0x3};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) static enum sam_lun_addr_method sdebug_lun_am = SAM_LUN_AM_PERIPHERAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) static int sdebug_lun_am_i = (int)SAM_LUN_AM_PERIPHERAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) static unsigned int sdebug_store_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) static sector_t sdebug_capacity;	/* in sectors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) /* old BIOS stuff, kernel may get rid of them but some mode sense pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801)    may still need them */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) static int sdebug_heads;		/* heads per disk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) static int sdebug_cylinders_per;	/* cylinders per surface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) static int sdebug_sectors_per;		/* sectors per cylinder */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) static LIST_HEAD(sdebug_host_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) static DEFINE_SPINLOCK(sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) static struct xarray per_store_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) static struct xarray *per_store_ap = &per_store_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) static int sdeb_first_idx = -1;		/* invalid index ==> none created */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) static int sdeb_most_recent_idx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) static DEFINE_RWLOCK(sdeb_fake_rw_lck);	/* need a RW lock when fake_rw=1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) static unsigned long map_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) static int num_aborts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) static int num_dev_resets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) static int num_target_resets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) static int num_bus_resets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) static int num_host_resets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) static int dix_writes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) static int dix_reads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) static int dif_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) /* ZBC global data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) static bool sdeb_zbc_in_use;	/* true for host-aware and host-managed disks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) static int sdeb_zbc_zone_size_mb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) static int sdeb_zbc_max_open = DEF_ZBC_MAX_OPEN_ZONES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) static int sdeb_zbc_nr_conv = DEF_ZBC_NR_CONV_ZONES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) static int submit_queues = DEF_SUBMIT_QUEUES;  /* > 1 for multi-queue (mq) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) static struct sdebug_queue *sdebug_q_arr;  /* ptr to array of submit queues */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) static DEFINE_RWLOCK(atomic_rw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) static DEFINE_RWLOCK(atomic_rw2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) static rwlock_t *ramdisk_lck_a[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) static char sdebug_proc_name[] = MY_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) static const char *my_name = MY_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) static struct bus_type pseudo_lld_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) static struct device_driver sdebug_driverfs_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	.name 		= sdebug_proc_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	.bus		= &pseudo_lld_bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) static const int check_condition_result =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		(DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) static const int illegal_condition_result =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	(DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) static const int device_qfull_result =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	(DID_OK << 16) | (COMMAND_COMPLETE << 8) | SAM_STAT_TASK_SET_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) static const int condition_met_result = SAM_STAT_CONDITION_MET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) /* Only do the extra work involved in logical block provisioning if one or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862)  * more of the lbpu, lbpws or lbpws10 parameters are given and we are doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863)  * real reads and writes (i.e. not skipping them for speed).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) static inline bool scsi_debug_lbp(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	return 0 == sdebug_fake_rw &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		(sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) static void *lba2fake_store(struct sdeb_store_info *sip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 			    unsigned long long lba)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	struct sdeb_store_info *lsip = sip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	lba = do_div(lba, sdebug_store_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	if (!sip || !sip->storep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		WARN_ON_ONCE(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		lsip = xa_load(per_store_ap, 0);  /* should never be NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	return lsip->storep + lba * sdebug_sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) static struct t10_pi_tuple *dif_store(struct sdeb_store_info *sip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 				      sector_t sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	sector = sector_div(sector, sdebug_store_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	return sip->dif_storep + sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) static void sdebug_max_tgts_luns(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	struct sdebug_host_info *sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	struct Scsi_Host *hpnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	spin_lock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		hpnt = sdbg_host->shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		if ((hpnt->this_id >= 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		    (sdebug_num_tgts > hpnt->this_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 			hpnt->max_id = sdebug_num_tgts + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 			hpnt->max_id = sdebug_num_tgts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		/* sdebug_max_luns; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	spin_unlock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) /* Set in_bit to -1 to indicate no bit position of invalid field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) static void mk_sense_invalid_fld(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 				 enum sdeb_cmd_data c_d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 				 int in_byte, int in_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	unsigned char *sbuff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	u8 sks[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	int sl, asc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	sbuff = scp->sense_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	if (!sbuff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		sdev_printk(KERN_ERR, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 			    "%s: sense_buffer is NULL\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	scsi_build_sense_buffer(sdebug_dsense, sbuff, ILLEGAL_REQUEST, asc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	memset(sks, 0, sizeof(sks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	sks[0] = 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	if (c_d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		sks[0] |= 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	if (in_bit >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		sks[0] |= 0x8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		sks[0] |= 0x7 & in_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	put_unaligned_be16(in_byte, sks + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	if (sdebug_dsense) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		sl = sbuff[7] + 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		sbuff[7] = sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		sbuff[sl] = 0x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		sbuff[sl + 1] = 0x6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		memcpy(sbuff + sl + 4, sks, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		memcpy(sbuff + 15, sks, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		sdev_printk(KERN_INFO, scp->device, "%s:  [sense_key,asc,ascq"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 			    "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			    my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) static void mk_sense_buffer(struct scsi_cmnd *scp, int key, int asc, int asq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	unsigned char *sbuff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	sbuff = scp->sense_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	if (!sbuff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		sdev_printk(KERN_ERR, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 			    "%s: sense_buffer is NULL\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	scsi_build_sense_buffer(sdebug_dsense, sbuff, key, asc, asq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		sdev_printk(KERN_INFO, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			    "%s:  [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			    my_name, key, asc, asq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) static void mk_sense_invalid_opcode(struct scsi_cmnd *scp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) static int scsi_debug_ioctl(struct scsi_device *dev, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			    void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	if (sdebug_verbose) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		if (0x1261 == cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			sdev_printk(KERN_INFO, dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 				    "%s: BLKFLSBUF [0x1261]\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		else if (0x5331 == cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 			sdev_printk(KERN_INFO, dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 				    "%s: CDROM_GET_CAPABILITY [0x5331]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 				    __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 				    __func__, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	/* return -ENOTTY; // correct return but upsets fdisk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) static void config_cdb_len(struct scsi_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	switch (sdebug_cdb_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	case 6:	/* suggest 6 byte READ, WRITE and MODE SENSE/SELECT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		sdev->use_10_for_rw = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		sdev->use_16_for_rw = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		sdev->use_10_for_ms = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	case 10: /* suggest 10 byte RWs and 6 byte MODE SENSE/SELECT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		sdev->use_10_for_rw = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		sdev->use_16_for_rw = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		sdev->use_10_for_ms = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	case 12: /* suggest 10 byte RWs and 10 byte MODE SENSE/SELECT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		sdev->use_10_for_rw = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		sdev->use_16_for_rw = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		sdev->use_10_for_ms = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	case 16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		sdev->use_10_for_rw = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		sdev->use_16_for_rw = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		sdev->use_10_for_ms = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	case 32: /* No knobs to suggest this so same as 16 for now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		sdev->use_10_for_rw = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		sdev->use_16_for_rw = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		sdev->use_10_for_ms = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		pr_warn("unexpected cdb_len=%d, force to 10\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 			sdebug_cdb_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		sdev->use_10_for_rw = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		sdev->use_16_for_rw = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		sdev->use_10_for_ms = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		sdebug_cdb_len = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) static void all_config_cdb_len(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	struct sdebug_host_info *sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	struct Scsi_Host *shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	spin_lock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		shost = sdbg_host->shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		shost_for_each_device(sdev, shost) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 			config_cdb_len(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	spin_unlock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) static void clear_luns_changed_on_target(struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	struct sdebug_host_info *sdhp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	struct sdebug_dev_info *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	spin_lock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 			if ((devip->sdbg_host == dp->sdbg_host) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 			    (devip->target == dp->target))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 				clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	spin_unlock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) static int make_ua(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	if (k != SDEBUG_NUM_UAS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		const char *cp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		switch (k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		case SDEBUG_UA_POR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 			mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 					POWER_ON_RESET_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 			if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 				cp = "power on reset";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		case SDEBUG_UA_BUS_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 			mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 					BUS_RESET_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 			if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 				cp = "bus reset";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		case SDEBUG_UA_MODE_CHANGED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 			mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 					MODE_CHANGED_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 				cp = "mode parameters changed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		case SDEBUG_UA_CAPACITY_CHANGED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 			mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 					CAPACITY_CHANGED_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 				cp = "capacity data changed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		case SDEBUG_UA_MICROCODE_CHANGED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			mk_sense_buffer(scp, UNIT_ATTENTION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 					TARGET_CHANGED_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 					MICROCODE_CHANGED_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 				cp = "microcode has been changed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 			mk_sense_buffer(scp, UNIT_ATTENTION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 					TARGET_CHANGED_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 					MICROCODE_CHANGED_WO_RESET_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 				cp = "microcode has been changed without reset";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		case SDEBUG_UA_LUNS_CHANGED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 			 * SPC-3 behavior is to report a UNIT ATTENTION with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 			 * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 			 * on the target, until a REPORT LUNS command is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 			 * received.  SPC-4 behavior is to report it only once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 			 * NOTE:  sdebug_scsi_level does not use the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 			 * values as struct scsi_device->scsi_level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 			if (sdebug_scsi_level >= 6)	/* SPC-4 and above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 				clear_luns_changed_on_target(devip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 			mk_sense_buffer(scp, UNIT_ATTENTION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 					TARGET_CHANGED_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 					LUNS_CHANGED_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 			if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 				cp = "reported luns data has changed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 			pr_warn("unexpected unit attention code=%d\n", k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 			if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 				cp = "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		clear_bit(k, devip->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 			sdev_printk(KERN_INFO, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 				   "%s reports: Unit attention: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 				   my_name, cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) /* Build SCSI "data-in" buffer. Returns 0 if ok else (DID_ERROR << 16). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 				int arr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	int act_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	struct scsi_data_buffer *sdb = &scp->sdb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	if (!sdb->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	if (scp->sc_data_direction != DMA_FROM_DEVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		return DID_ERROR << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 				      arr, arr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	scsi_set_resid(scp, scsi_bufflen(scp) - act_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) /* Partial build of SCSI "data-in" buffer. Returns 0 if ok else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)  * (DID_ERROR << 16). Can write to offset in data-in buffer. If multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170)  * calls, not required to write in ascending offset order. Assumes resid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)  * set to scsi_bufflen() prior to any calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) static int p_fill_from_dev_buffer(struct scsi_cmnd *scp, const void *arr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 				  int arr_len, unsigned int off_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	unsigned int act_len, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	struct scsi_data_buffer *sdb = &scp->sdb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	off_t skip = off_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	if (sdb->length <= off_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	if (scp->sc_data_direction != DMA_FROM_DEVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		return DID_ERROR << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	act_len = sg_pcopy_from_buffer(sdb->table.sgl, sdb->table.nents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 				       arr, arr_len, skip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	pr_debug("%s: off_dst=%u, scsi_bufflen=%u, act_len=%u, resid=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		 __func__, off_dst, scsi_bufflen(scp), act_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		 scsi_get_resid(scp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	n = scsi_bufflen(scp) - (off_dst + act_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	scsi_set_resid(scp, min_t(u32, scsi_get_resid(scp), n));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) /* Fetches from SCSI "data-out" buffer. Returns number of bytes fetched into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)  * 'arr' or -1 if error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 			       int arr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	if (!scsi_bufflen(scp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	if (scp->sc_data_direction != DMA_TO_DEVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	return scsi_sg_copy_to_buffer(scp, arr, arr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) static char sdebug_inq_vendor_id[9] = "Linux   ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) static char sdebug_inq_product_id[17] = "scsi_debug      ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) static char sdebug_inq_product_rev[5] = SDEBUG_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) /* Use some locally assigned NAAs for SAS addresses. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) static const u64 naa3_comp_a = 0x3222222000000000ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) static const u64 naa3_comp_b = 0x3333333000000000ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) static const u64 naa3_comp_c = 0x3111111000000000ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) /* Device identification VPD page. Returns number of bytes placed in arr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) static int inquiry_vpd_83(unsigned char *arr, int port_group_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 			  int target_dev_id, int dev_id_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 			  const char *dev_id_str, int dev_id_str_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 			  const uuid_t *lu_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	int num, port_a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	char b[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	port_a = target_dev_id + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	/* T10 vendor identifier field format (faked) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	arr[0] = 0x2;	/* ASCII */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	arr[1] = 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	arr[2] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	memcpy(&arr[4], sdebug_inq_vendor_id, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	memcpy(&arr[12], sdebug_inq_product_id, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	memcpy(&arr[28], dev_id_str, dev_id_str_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	num = 8 + 16 + dev_id_str_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	arr[3] = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	num += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	if (dev_id_num >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		if (sdebug_uuid_ctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 			/* Locally assigned UUID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 			arr[num++] = 0x1;  /* binary (not necessarily sas) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 			arr[num++] = 0xa;  /* PIV=0, lu, naa */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 			arr[num++] = 0x12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 			arr[num++] = 0x10; /* uuid type=1, locally assigned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 			arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 			memcpy(arr + num, lu_name, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 			num += 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 			/* NAA-3, Logical unit identifier (binary) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 			arr[num++] = 0x1;  /* binary (not necessarily sas) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 			arr[num++] = 0x3;  /* PIV=0, lu, naa */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 			arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 			arr[num++] = 0x8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 			put_unaligned_be64(naa3_comp_b + dev_id_num, arr + num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 			num += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		/* Target relative port number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		arr[num++] = 0x61;	/* proto=sas, binary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		arr[num++] = 0x94;	/* PIV=1, target port, rel port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		arr[num++] = 0x0;	/* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		arr[num++] = 0x4;	/* length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		arr[num++] = 0x0;	/* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		arr[num++] = 0x0;	/* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		arr[num++] = 0x1;	/* relative port A */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	/* NAA-3, Target port identifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	arr[num++] = 0x61;	/* proto=sas, binary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	arr[num++] = 0x93;	/* piv=1, target port, naa */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	arr[num++] = 0x8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	put_unaligned_be64(naa3_comp_a + port_a, arr + num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	num += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	/* NAA-3, Target port group identifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	arr[num++] = 0x61;	/* proto=sas, binary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	arr[num++] = 0x95;	/* piv=1, target port group id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	arr[num++] = 0x4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	arr[num++] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	arr[num++] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	put_unaligned_be16(port_group_id, arr + num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	num += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	/* NAA-3, Target device identifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	arr[num++] = 0x61;	/* proto=sas, binary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	arr[num++] = 0xa3;	/* piv=1, target device, naa */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	arr[num++] = 0x8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	put_unaligned_be64(naa3_comp_a + target_dev_id, arr + num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	num += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	/* SCSI name string: Target device identifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	arr[num++] = 0x63;	/* proto=sas, UTF-8 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	arr[num++] = 0xa8;	/* piv=1, target device, SCSI name string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	arr[num++] = 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	memcpy(arr + num, "naa.32222220", 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	num += 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	snprintf(b, sizeof(b), "%08X", target_dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	memcpy(arr + num, b, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	num += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	memset(arr + num, 0, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	num += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	return num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) static unsigned char vpd84_data[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308)     0x22,0x22,0x22,0x0,0xbb,0x1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)     0x22,0x22,0x22,0x0,0xbb,0x2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) /*  Software interface identification VPD page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) static int inquiry_vpd_84(unsigned char *arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	memcpy(arr, vpd84_data, sizeof(vpd84_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	return sizeof(vpd84_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) /* Management network addresses VPD page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) static int inquiry_vpd_85(unsigned char *arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	int num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	const char *na1 = "https://www.kernel.org/config";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	const char *na2 = "http://www.kernel.org/log";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	int plen, olen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	arr[num++] = 0x1;	/* lu, storage config */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	arr[num++] = 0x0;	/* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	olen = strlen(na1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	plen = olen + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	if (plen % 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 		plen = ((plen / 4) + 1) * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	arr[num++] = plen;	/* length, null termianted, padded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	memcpy(arr + num, na1, olen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	memset(arr + num + olen, 0, plen - olen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	num += plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	arr[num++] = 0x4;	/* lu, logging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	arr[num++] = 0x0;	/* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	olen = strlen(na2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	plen = olen + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	if (plen % 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		plen = ((plen / 4) + 1) * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	arr[num++] = plen;	/* length, null terminated, padded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	memcpy(arr + num, na2, olen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	memset(arr + num + olen, 0, plen - olen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	num += plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	return num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) /* SCSI ports VPD page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) static int inquiry_vpd_88(unsigned char *arr, int target_dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	int num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	int port_a, port_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	port_a = target_dev_id + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	port_b = port_a + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	arr[num++] = 0x0;	/* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	arr[num++] = 0x0;	/* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	arr[num++] = 0x1;	/* relative port 1 (primary) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	memset(arr + num, 0, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	num += 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	arr[num++] = 12;	/* length tp descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	/* naa-5 target port identifier (A) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	arr[num++] = 0x61;	/* proto=sas, binary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	arr[num++] = 0x93;	/* PIV=1, target port, NAA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	arr[num++] = 0x0;	/* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	arr[num++] = 0x8;	/* length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	put_unaligned_be64(naa3_comp_a + port_a, arr + num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	num += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	arr[num++] = 0x0;	/* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	arr[num++] = 0x0;	/* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	arr[num++] = 0x2;	/* relative port 2 (secondary) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	memset(arr + num, 0, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	num += 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	arr[num++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	arr[num++] = 12;	/* length tp descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	/* naa-5 target port identifier (B) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	arr[num++] = 0x61;	/* proto=sas, binary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	arr[num++] = 0x93;	/* PIV=1, target port, NAA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	arr[num++] = 0x0;	/* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	arr[num++] = 0x8;	/* length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	put_unaligned_be64(naa3_comp_a + port_b, arr + num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	num += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	return num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) static unsigned char vpd89_data[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) /* from 4th byte */ 0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 'l','i','n','u','x',' ',' ',' ',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) '1','2','3','4',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 0xec,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 0x53,0x41,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 0x20,0x20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 0x10,0x80,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) /* ATA Information VPD page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) static int inquiry_vpd_89(unsigned char *arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	memcpy(arr, vpd89_data, sizeof(vpd89_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	return sizeof(vpd89_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) static unsigned char vpdb0_data[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	/* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) /* Block limits VPD page (SBC-3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) static int inquiry_vpd_b0(unsigned char *arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	unsigned int gran;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	/* Optimal transfer length granularity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	if (sdebug_opt_xferlen_exp != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	    sdebug_physblk_exp < sdebug_opt_xferlen_exp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		gran = 1 << sdebug_opt_xferlen_exp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		gran = 1 << sdebug_physblk_exp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	put_unaligned_be16(gran, arr + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	/* Maximum Transfer Length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	if (sdebug_store_sectors > 0x400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		put_unaligned_be32(sdebug_store_sectors, arr + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	/* Optimal Transfer Length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	put_unaligned_be32(sdebug_opt_blks, &arr[8]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	if (sdebug_lbpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		/* Maximum Unmap LBA Count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		/* Maximum Unmap Block Descriptor Count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	/* Unmap Granularity Alignment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	if (sdebug_unmap_alignment) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		put_unaligned_be32(sdebug_unmap_alignment, &arr[28]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		arr[28] |= 0x80; /* UGAVALID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	/* Optimal Unmap Granularity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	put_unaligned_be32(sdebug_unmap_granularity, &arr[24]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	/* Maximum WRITE SAME Length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	put_unaligned_be64(sdebug_write_same_length, &arr[32]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	return 0x3c; /* Mandatory page length for Logical Block Provisioning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	return sizeof(vpdb0_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) /* Block device characteristics VPD page (SBC-3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) static int inquiry_vpd_b1(struct sdebug_dev_info *devip, unsigned char *arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	memset(arr, 0, 0x3c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	arr[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	arr[1] = 1;	/* non rotating medium (e.g. solid state) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	arr[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	arr[3] = 5;	/* less than 1.8" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	if (devip->zmodel == BLK_ZONED_HA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		arr[4] = 1 << 4;	/* zoned field = 01b */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	return 0x3c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) /* Logical block provisioning VPD page (SBC-4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) static int inquiry_vpd_b2(unsigned char *arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	memset(arr, 0, 0x4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	arr[0] = 0;			/* threshold exponent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	if (sdebug_lbpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		arr[1] = 1 << 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	if (sdebug_lbpws)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 		arr[1] |= 1 << 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	if (sdebug_lbpws10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		arr[1] |= 1 << 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	if (sdebug_lbprz && scsi_debug_lbp())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 		arr[1] |= (sdebug_lbprz & 0x7) << 2;  /* sbc4r07 and later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	/* anc_sup=0; dp=0 (no provisioning group descriptor) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	/* minimum_percentage=0; provisioning_type=0 (unknown) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	/* threshold_percentage=0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	return 0x4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) /* Zoned block device characteristics VPD page (ZBC mandatory) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) static int inquiry_vpd_b6(struct sdebug_dev_info *devip, unsigned char *arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	memset(arr, 0, 0x3c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	arr[0] = 0x1; /* set URSWRZ (unrestricted read in seq. wr req zone) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	 * Set Optimal number of open sequential write preferred zones and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	 * Optimal number of non-sequentially written sequential write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	 * preferred zones fields to 'not reported' (0xffffffff). Leave other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	 * fields set to zero, apart from Max. number of open swrz_s field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	put_unaligned_be32(0xffffffff, &arr[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	put_unaligned_be32(0xffffffff, &arr[8]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	if (sdeb_zbc_model == BLK_ZONED_HM && devip->max_open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		put_unaligned_be32(devip->max_open, &arr[12]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		put_unaligned_be32(0xffffffff, &arr[12]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	return 0x3c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) #define SDEBUG_LONG_INQ_SZ 96
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) #define SDEBUG_MAX_INQ_ARR_SZ 584
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) static int resp_inquiry(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	unsigned char pq_pdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	unsigned char *arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	unsigned char *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	u32 alloc_len, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	bool have_wlun, is_disk, is_zbc, is_disk_zbc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	alloc_len = get_unaligned_be16(cmd + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	if (! arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		return DID_REQUEUE << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	is_disk = (sdebug_ptype == TYPE_DISK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	is_zbc = (devip->zmodel != BLK_ZONED_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	is_disk_zbc = (is_disk || is_zbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	have_wlun = scsi_is_wlun(scp->device->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	if (have_wlun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 		pq_pdt = TYPE_WLUN;	/* present, wlun */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	else if (sdebug_no_lun_0 && (devip->lun == SDEBUG_LUN_0_VAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		pq_pdt = 0x7f;	/* not present, PQ=3, PDT=0x1f */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		pq_pdt = (sdebug_ptype & 0x1f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	arr[0] = pq_pdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	if (0x2 & cmd[1]) {  /* CMDDT bit set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 		kfree(arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	} else if (0x1 & cmd[1]) {  /* EVPD bit set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 		int lu_id_num, port_group_id, target_dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		char lu_id_str[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		int host_no = devip->sdbg_host->shost->host_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		port_group_id = (((host_no + 1) & 0x7f) << 8) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		    (devip->channel & 0x7f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		if (sdebug_vpd_use_hostno == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 			host_no = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 			    (devip->target * 1000) + devip->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		target_dev_id = ((host_no + 1) * 2000) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 				 (devip->target * 1000) - 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		if (0 == cmd[2]) { /* supported vital product data pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 			arr[1] = cmd[2];	/*sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 			n = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 			arr[n++] = 0x0;   /* this page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 			arr[n++] = 0x80;  /* unit serial number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 			arr[n++] = 0x83;  /* device identification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 			arr[n++] = 0x84;  /* software interface ident. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 			arr[n++] = 0x85;  /* management network addresses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 			arr[n++] = 0x86;  /* extended inquiry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 			arr[n++] = 0x87;  /* mode page policy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 			arr[n++] = 0x88;  /* SCSI ports */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 			if (is_disk_zbc) {	  /* SBC or ZBC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 				arr[n++] = 0x89;  /* ATA information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 				arr[n++] = 0xb0;  /* Block limits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 				arr[n++] = 0xb1;  /* Block characteristics */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 				if (is_disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 					arr[n++] = 0xb2;  /* LB Provisioning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 				if (is_zbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 					arr[n++] = 0xb6;  /* ZB dev. char. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 			arr[3] = n - 4;	  /* number of supported VPD pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		} else if (0x80 == cmd[2]) { /* unit serial number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 			arr[1] = cmd[2];	/*sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 			arr[3] = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 			memcpy(&arr[4], lu_id_str, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		} else if (0x83 == cmd[2]) { /* device identification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 			arr[1] = cmd[2];	/*sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 			arr[3] = inquiry_vpd_83(&arr[4], port_group_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 						target_dev_id, lu_id_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 						lu_id_str, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 						&devip->lu_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		} else if (0x84 == cmd[2]) { /* Software interface ident. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 			arr[1] = cmd[2];	/*sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 			arr[3] = inquiry_vpd_84(&arr[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		} else if (0x85 == cmd[2]) { /* Management network addresses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 			arr[1] = cmd[2];	/*sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 			arr[3] = inquiry_vpd_85(&arr[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		} else if (0x86 == cmd[2]) { /* extended inquiry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 			arr[1] = cmd[2];	/*sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 			arr[3] = 0x3c;	/* number of following entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 			if (sdebug_dif == T10_PI_TYPE3_PROTECTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 				arr[4] = 0x4;	/* SPT: GRD_CHK:1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 			else if (have_dif_prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 				arr[4] = 0x5;   /* SPT: GRD_CHK:1, REF_CHK:1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 				arr[4] = 0x0;   /* no protection stuff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 			arr[5] = 0x7;   /* head of q, ordered + simple q's */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 		} else if (0x87 == cmd[2]) { /* mode page policy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 			arr[1] = cmd[2];	/*sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 			arr[3] = 0x8;	/* number of following entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 			arr[4] = 0x2;	/* disconnect-reconnect mp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 			arr[6] = 0x80;	/* mlus, shared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 			arr[8] = 0x18;	 /* protocol specific lu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 			arr[10] = 0x82;	 /* mlus, per initiator port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 		} else if (0x88 == cmd[2]) { /* SCSI Ports */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 			arr[1] = cmd[2];	/*sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 			arr[3] = inquiry_vpd_88(&arr[4], target_dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		} else if (is_disk_zbc && 0x89 == cmd[2]) { /* ATA info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 			arr[1] = cmd[2];        /*sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 			n = inquiry_vpd_89(&arr[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 			put_unaligned_be16(n, arr + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 		} else if (is_disk_zbc && 0xb0 == cmd[2]) { /* Block limits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 			arr[1] = cmd[2];        /*sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 			arr[3] = inquiry_vpd_b0(&arr[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		} else if (is_disk_zbc && 0xb1 == cmd[2]) { /* Block char. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 			arr[1] = cmd[2];        /*sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 			arr[3] = inquiry_vpd_b1(devip, &arr[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		} else if (is_disk && 0xb2 == cmd[2]) { /* LB Prov. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 			arr[1] = cmd[2];        /*sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 			arr[3] = inquiry_vpd_b2(&arr[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		} else if (is_zbc && cmd[2] == 0xb6) { /* ZB dev. charact. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 			arr[1] = cmd[2];        /*sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 			arr[3] = inquiry_vpd_b6(devip, &arr[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 			kfree(arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		len = min_t(u32, get_unaligned_be16(arr + 2) + 4, alloc_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		ret = fill_from_dev_buffer(scp, arr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 			    min_t(u32, len, SDEBUG_MAX_INQ_ARR_SZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 		kfree(arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	/* drops through here for a standard inquiry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	arr[1] = sdebug_removable ? 0x80 : 0;	/* Removable disk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	arr[2] = sdebug_scsi_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	arr[3] = 2;    /* response_data_format==2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	arr[4] = SDEBUG_LONG_INQ_SZ - 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	arr[5] = (int)have_dif_prot;	/* PROTECT bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	if (sdebug_vpd_use_hostno == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		arr[5] |= 0x10; /* claim: implicit TPGS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	arr[6] = 0x10; /* claim: MultiP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	/* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	arr[7] = 0xa; /* claim: LINKED + CMDQUE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	memcpy(&arr[8], sdebug_inq_vendor_id, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	memcpy(&arr[16], sdebug_inq_product_id, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	memcpy(&arr[32], sdebug_inq_product_rev, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	/* Use Vendor Specific area to place driver date in ASCII hex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	memcpy(&arr[36], sdebug_version_date, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	/* version descriptors (2 bytes each) follow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	put_unaligned_be16(0xc0, arr + 58);   /* SAM-6 no version claimed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	put_unaligned_be16(0x5c0, arr + 60);  /* SPC-5 no version claimed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	n = 62;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	if (is_disk) {		/* SBC-4 no version claimed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 		put_unaligned_be16(0x600, arr + n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 		n += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	} else if (sdebug_ptype == TYPE_TAPE) {	/* SSC-4 rev 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		put_unaligned_be16(0x525, arr + n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		n += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	} else if (is_zbc) {	/* ZBC BSR INCITS 536 revision 05 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 		put_unaligned_be16(0x624, arr + n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 		n += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	put_unaligned_be16(0x2100, arr + n);	/* SPL-4 no version claimed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	ret = fill_from_dev_buffer(scp, arr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 			    min_t(u32, alloc_len, SDEBUG_LONG_INQ_SZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	kfree(arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) /* See resp_iec_m_pg() for how this data is manipulated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 				   0, 0, 0x0, 0x0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) static int resp_requests(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 			 struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	unsigned char *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	unsigned char arr[SCSI_SENSE_BUFFERSIZE];	/* assume >= 18 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	bool dsense = !!(cmd[1] & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	u32 alloc_len = cmd[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	u32 len = 18;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	int stopped_state = atomic_read(&devip->stopped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	memset(arr, 0, sizeof(arr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	if (stopped_state > 0) {	/* some "pollable" data [spc6r02: 5.12.2] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		if (dsense) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 			arr[0] = 0x72;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 			arr[1] = NOT_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 			arr[2] = LOGICAL_UNIT_NOT_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 			arr[3] = (stopped_state == 2) ? 0x1 : 0x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 			len = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 			arr[0] = 0x70;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 			arr[2] = NOT_READY;		/* NO_SENSE in sense_key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 			arr[7] = 0xa;			/* 18 byte sense buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 			arr[12] = LOGICAL_UNIT_NOT_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 			arr[13] = (stopped_state == 2) ? 0x1 : 0x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	} else if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 		/* Information exceptions control mode page: TEST=1, MRIE=6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 		if (dsense) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 			arr[0] = 0x72;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 			arr[1] = 0x0;		/* NO_SENSE in sense_key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 			arr[2] = THRESHOLD_EXCEEDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 			arr[3] = 0xff;		/* Failure prediction(false) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 			len = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 			arr[0] = 0x70;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 			arr[2] = 0x0;		/* NO_SENSE in sense_key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 			arr[7] = 0xa;   	/* 18 byte sense buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 			arr[12] = THRESHOLD_EXCEEDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 			arr[13] = 0xff;		/* Failure prediction(false) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	} else {	/* nothing to report */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 		if (dsense) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 			len = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 			memset(arr, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 			arr[0] = 0x72;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 			memset(arr, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 			arr[0] = 0x70;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 			arr[7] = 0xa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	return fill_from_dev_buffer(scp, arr, min_t(u32, len, alloc_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) static int resp_start_stop(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	unsigned char *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	int power_cond, want_stop, stopped_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	bool changing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	power_cond = (cmd[4] & 0xf0) >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	if (power_cond) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	want_stop = !(cmd[4] & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	stopped_state = atomic_read(&devip->stopped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	if (stopped_state == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 		ktime_t now_ts = ktime_get_boottime();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		if (ktime_to_ns(now_ts) > ktime_to_ns(devip->create_ts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 			u64 diff_ns = ktime_to_ns(ktime_sub(now_ts, devip->create_ts));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 			if (diff_ns >= ((u64)sdeb_tur_ms_to_ready * 1000000)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 				/* tur_ms_to_ready timer extinguished */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 				atomic_set(&devip->stopped, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 				stopped_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 		if (stopped_state == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 			if (want_stop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 				stopped_state = 1;	/* dummy up success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 			} else {	/* Disallow tur_ms_to_ready delay to be overridden */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 				mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 0 /* START bit */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 				return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	changing = (stopped_state != want_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	if (changing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		atomic_xchg(&devip->stopped, want_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	if (!changing || (cmd[1] & 0x1))  /* state unchanged or IMMED bit set in cdb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 		return SDEG_RES_IMMED_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) static sector_t get_sdebug_capacity(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	static const unsigned int gibibyte = 1073741824;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	if (sdebug_virtual_gb > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		return (sector_t)sdebug_virtual_gb *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 			(gibibyte / sdebug_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 		return sdebug_store_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) #define SDEBUG_READCAP_ARR_SZ 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) static int resp_readcap(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 			struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	unsigned char arr[SDEBUG_READCAP_ARR_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	unsigned int capac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	/* following just in case virtual_gb changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	sdebug_capacity = get_sdebug_capacity();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	if (sdebug_capacity < 0xffffffff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 		capac = (unsigned int)sdebug_capacity - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		put_unaligned_be32(capac, arr + 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 		put_unaligned_be32(0xffffffff, arr + 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	put_unaligned_be16(sdebug_sector_size, arr + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) #define SDEBUG_READCAP16_ARR_SZ 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) static int resp_readcap16(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 			  struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	unsigned char *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	u32 alloc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	alloc_len = get_unaligned_be32(cmd + 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	/* following just in case virtual_gb changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	sdebug_capacity = get_sdebug_capacity();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	put_unaligned_be32(sdebug_sector_size, arr + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	arr[13] = sdebug_physblk_exp & 0xf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	if (scsi_debug_lbp()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		arr[14] |= 0x80; /* LBPME */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 		/* from sbc4r07, this LBPRZ field is 1 bit, but the LBPRZ in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 		 * the LB Provisioning VPD page is 3 bits. Note that lbprz=2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 		 * in the wider field maps to 0 in this field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 		if (sdebug_lbprz & 1)	/* precisely what the draft requires */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 			arr[14] |= 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	arr[15] = sdebug_lowest_aligned & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	if (have_dif_prot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 		arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 		arr[12] |= 1; /* PROT_EN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 	return fill_from_dev_buffer(scp, arr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 			    min_t(u32, alloc_len, SDEBUG_READCAP16_ARR_SZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) static int resp_report_tgtpgs(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 			      struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 	unsigned char *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	unsigned char *arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	int host_no = devip->sdbg_host->shost->host_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	int port_group_a, port_group_b, port_a, port_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	u32 alen, n, rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	alen = get_unaligned_be32(cmd + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	if (! arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 		return DID_REQUEUE << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	 * EVPD page 0x88 states we have two ports, one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	 * real and a fake port with no device connected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	 * So we create two port groups with one port each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	 * and set the group with port B to unavailable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	port_a = 0x1; /* relative port A */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	port_b = 0x2; /* relative port B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	port_group_a = (((host_no + 1) & 0x7f) << 8) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 			(devip->channel & 0x7f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	port_group_b = (((host_no + 1) & 0x7f) << 8) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 			(devip->channel & 0x7f) + 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	 * The asymmetric access state is cycled according to the host_id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	n = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	if (sdebug_vpd_use_hostno == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 		arr[n++] = host_no % 3; /* Asymm access state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 		arr[n++] = 0x0F; /* claim: all states are supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 		arr[n++] = 0x0; /* Active/Optimized path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		arr[n++] = 0x01; /* only support active/optimized paths */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	put_unaligned_be16(port_group_a, arr + n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	n += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	arr[n++] = 0;    /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	arr[n++] = 0;    /* Status code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	arr[n++] = 0;    /* Vendor unique */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	arr[n++] = 0x1;  /* One port per group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	arr[n++] = 0;    /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	arr[n++] = 0;    /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	put_unaligned_be16(port_a, arr + n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	n += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	arr[n++] = 3;    /* Port unavailable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	put_unaligned_be16(port_group_b, arr + n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	n += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	arr[n++] = 0;    /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	arr[n++] = 0;    /* Status code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	arr[n++] = 0;    /* Vendor unique */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 	arr[n++] = 0x1;  /* One port per group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	arr[n++] = 0;    /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	arr[n++] = 0;    /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	put_unaligned_be16(port_b, arr + n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	n += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	rlen = n - 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	put_unaligned_be32(rlen, arr + 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	 * Return the smallest value of either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	 * - The allocated length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	 * - The constructed command length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	 * - The maximum array size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	rlen = min(alen, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	ret = fill_from_dev_buffer(scp, arr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 			   min_t(u32, rlen, SDEBUG_MAX_TGTPGS_ARR_SZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	kfree(arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) static int resp_rsup_opcodes(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 			     struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	bool rctd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	u8 reporting_opts, req_opcode, sdeb_i, supp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	u16 req_sa, u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	u32 alloc_len, a_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	int k, offset, len, errsts, count, bump, na;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	const struct opcode_info_t *oip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	const struct opcode_info_t *r_oip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	u8 *arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	rctd = !!(cmd[2] & 0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	reporting_opts = cmd[2] & 0x7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	req_opcode = cmd[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 	req_sa = get_unaligned_be16(cmd + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	alloc_len = get_unaligned_be32(cmd + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	if (alloc_len < 4 || alloc_len > 0xffff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	if (alloc_len > 8192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 		a_len = 8192;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		a_len = alloc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	if (NULL == arr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 				INSUFF_RES_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 	switch (reporting_opts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	case 0:	/* all commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		/* count number of commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 		for (count = 0, oip = opcode_info_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 		     oip->num_attached != 0xff; ++oip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 			if (F_INV_OP & oip->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 			count += (oip->num_attached + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 		bump = rctd ? 20 : 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 		put_unaligned_be32(count * bump, arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 		for (offset = 4, oip = opcode_info_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 		     oip->num_attached != 0xff && offset < a_len; ++oip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 			if (F_INV_OP & oip->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 			na = oip->num_attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 			arr[offset] = oip->opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 			put_unaligned_be16(oip->sa, arr + offset + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 			if (rctd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 				arr[offset + 5] |= 0x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 			if (FF_SA & oip->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 				arr[offset + 5] |= 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 			put_unaligned_be16(oip->len_mask[0], arr + offset + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 			if (rctd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 				put_unaligned_be16(0xa, arr + offset + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 			r_oip = oip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 			for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 				if (F_INV_OP & oip->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 				offset += bump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 				arr[offset] = oip->opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 				put_unaligned_be16(oip->sa, arr + offset + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 				if (rctd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 					arr[offset + 5] |= 0x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 				if (FF_SA & oip->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 					arr[offset + 5] |= 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 				put_unaligned_be16(oip->len_mask[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 						   arr + offset + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 				if (rctd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 					put_unaligned_be16(0xa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 							   arr + offset + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 			oip = r_oip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 			offset += bump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	case 1:	/* one command: opcode only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	case 2:	/* one command: opcode plus service action */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	case 3:	/* one command: if sa==0 then opcode only else opcode+sa */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 		sdeb_i = opcode_ind_arr[req_opcode];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 		oip = &opcode_info_arr[sdeb_i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 		if (F_INV_OP & oip->flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 			supp = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 			offset = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 			if (1 == reporting_opts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 				if (FF_SA & oip->flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 					mk_sense_invalid_fld(scp, SDEB_IN_CDB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 							     2, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 					kfree(arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 					return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 				req_sa = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 			} else if (2 == reporting_opts &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 				   0 == (FF_SA & oip->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 				mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 				kfree(arr);	/* point at requested sa */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 				return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 			if (0 == (FF_SA & oip->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 			    req_opcode == oip->opcode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 				supp = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 			else if (0 == (FF_SA & oip->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 				na = oip->num_attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 				for (k = 0, oip = oip->arrp; k < na;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 				     ++k, ++oip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 					if (req_opcode == oip->opcode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 						break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 				supp = (k >= na) ? 1 : 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 			} else if (req_sa != oip->sa) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 				na = oip->num_attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 				for (k = 0, oip = oip->arrp; k < na;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 				     ++k, ++oip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 					if (req_sa == oip->sa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 						break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 				supp = (k >= na) ? 1 : 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 				supp = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 			if (3 == supp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 				u = oip->len_mask[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 				put_unaligned_be16(u, arr + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 				arr[4] = oip->opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 				for (k = 1; k < u; ++k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 					arr[4 + k] = (k < 16) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 						 oip->len_mask[k] : 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 				offset = 4 + u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 				offset = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 		arr[1] = (rctd ? 0x80 : 0) | supp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 		if (rctd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 			put_unaligned_be16(0xa, arr + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 			offset += 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 		kfree(arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	offset = (offset < a_len) ? offset : a_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	len = (offset < alloc_len) ? offset : alloc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	errsts = fill_from_dev_buffer(scp, arr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	kfree(arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	return errsts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) static int resp_rsup_tmfs(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 			  struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	bool repd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	u32 alloc_len, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	u8 arr[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	memset(arr, 0, sizeof(arr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	repd = !!(cmd[2] & 0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	alloc_len = get_unaligned_be32(cmd + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	if (alloc_len < 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	arr[0] = 0xc8;		/* ATS | ATSS | LURS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 	arr[1] = 0x1;		/* ITNRS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	if (repd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 		arr[3] = 0xc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 		len = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 		len = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	len = (len < alloc_len) ? len : alloc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 	return fill_from_dev_buffer(scp, arr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) /* <<Following mode page info copied from ST318451LW>> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) static int resp_err_recov_pg(unsigned char *p, int pcontrol, int target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) {	/* Read-Write Error Recovery page for mode_sense */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 					5, 0, 0xff, 0xff};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	memcpy(p, err_recov_pg, sizeof(err_recov_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	if (1 == pcontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 		memset(p + 2, 0, sizeof(err_recov_pg) - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	return sizeof(err_recov_pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) static int resp_disconnect_pg(unsigned char *p, int pcontrol, int target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) { 	/* Disconnect-Reconnect page for mode_sense */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 	unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 					 0, 0, 0, 0, 0, 0, 0, 0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	memcpy(p, disconnect_pg, sizeof(disconnect_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	if (1 == pcontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 		memset(p + 2, 0, sizeof(disconnect_pg) - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	return sizeof(disconnect_pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) static int resp_format_pg(unsigned char *p, int pcontrol, int target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) {       /* Format device page for mode_sense */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 				     0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 				     0, 0, 0, 0, 0x40, 0, 0, 0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	memcpy(p, format_pg, sizeof(format_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 	put_unaligned_be16(sdebug_sectors_per, p + 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 	put_unaligned_be16(sdebug_sector_size, p + 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	if (sdebug_removable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 		p[20] |= 0x20; /* should agree with INQUIRY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 	if (1 == pcontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 		memset(p + 2, 0, sizeof(format_pg) - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	return sizeof(format_pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 				     0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 				     0, 0, 0, 0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) static int resp_caching_pg(unsigned char *p, int pcontrol, int target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) { 	/* Caching page for mode_sense */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	unsigned char ch_caching_pg[] = {/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	unsigned char d_caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 		0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0,     0, 0, 0, 0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	if (SDEBUG_OPT_N_WCE & sdebug_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 		caching_pg[2] &= ~0x4;	/* set WCE=0 (default WCE=1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	memcpy(p, caching_pg, sizeof(caching_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	if (1 == pcontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 		memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	else if (2 == pcontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 		memcpy(p, d_caching_pg, sizeof(d_caching_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	return sizeof(caching_pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 				    0, 0, 0x2, 0x4b};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) static int resp_ctrl_m_pg(unsigned char *p, int pcontrol, int target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) { 	/* Control mode page for mode_sense */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 	unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 					0, 0, 0, 0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 	unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 				     0, 0, 0x2, 0x4b};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	if (sdebug_dsense)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 		ctrl_m_pg[2] |= 0x4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 		ctrl_m_pg[2] &= ~0x4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	if (sdebug_ato)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 		ctrl_m_pg[5] |= 0x80; /* ATO=1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	if (1 == pcontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 		memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 	else if (2 == pcontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 		memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	return sizeof(ctrl_m_pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) static int resp_iec_m_pg(unsigned char *p, int pcontrol, int target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) {	/* Informational Exceptions control mode page for mode_sense */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 	unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 				       0, 0, 0x0, 0x0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 	unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 				      0, 0, 0x0, 0x0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	memcpy(p, iec_m_pg, sizeof(iec_m_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 	if (1 == pcontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 		memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 	else if (2 == pcontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 		memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 	return sizeof(iec_m_pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) static int resp_sas_sf_m_pg(unsigned char *p, int pcontrol, int target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) {	/* SAS SSP mode page - short format for mode_sense */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	unsigned char sas_sf_m_pg[] = {0x19, 0x6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 		0x6, 0x0, 0x7, 0xd0, 0x0, 0x0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 	memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 	if (1 == pcontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 		memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	return sizeof(sas_sf_m_pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) static int resp_sas_pcd_m_spg(unsigned char *p, int pcontrol, int target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 			      int target_dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) {	/* SAS phy control and discover mode page for mode_sense */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 	unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 		    0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 		    0, 0, 0, 0, 0, 0, 0, 0,	/* insert SAS addr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 		    0, 0, 0, 0, 0, 0, 0, 0,	/* insert SAS addr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 		    0x2, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 		    0x88, 0x99, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 		    0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 		    0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 		    0, 0, 0, 0, 0, 0, 0, 0,	/* insert SAS addr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 		    0, 0, 0, 0, 0, 0, 0, 0,	/* insert SAS addr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 		    0x3, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 		    0x88, 0x99, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 		    0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 	int port_a, port_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 	put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 	put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 72);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	port_a = target_dev_id + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 	port_b = port_a + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 	memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 	put_unaligned_be32(port_a, p + 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 	put_unaligned_be32(port_b, p + 48 + 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 	if (1 == pcontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 		memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 	return sizeof(sas_pcd_m_pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) static int resp_sas_sha_m_spg(unsigned char *p, int pcontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) {	/* SAS SSP shared protocol specific port mode subpage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 		    0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 	memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	if (1 == pcontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 		memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	return sizeof(sas_sha_m_pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) #define SDEBUG_MAX_MSENSE_SZ 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) static int resp_mode_sense(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 			   struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 	int pcontrol, pcode, subpcode, bd_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 	unsigned char dev_spec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 	u32 alloc_len, offset, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 	int target_dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	int target = scp->device->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	unsigned char *ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 	unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	unsigned char *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	bool dbd, llbaa, msense_6, is_disk, is_zbc, bad_pcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	dbd = !!(cmd[1] & 0x8);		/* disable block descriptors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	pcontrol = (cmd[2] & 0xc0) >> 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 	pcode = cmd[2] & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	subpcode = cmd[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 	msense_6 = (MODE_SENSE == cmd[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 	llbaa = msense_6 ? false : !!(cmd[1] & 0x10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 	is_disk = (sdebug_ptype == TYPE_DISK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	is_zbc = (devip->zmodel != BLK_ZONED_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 	if ((is_disk || is_zbc) && !dbd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 		bd_len = llbaa ? 16 : 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 		bd_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	if (0x3 == pcontrol) {  /* Saving values not supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 			(devip->target * 1000) - 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 	/* for disks+zbc set DPOFUA bit and clear write protect (WP) bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 	if (is_disk || is_zbc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 		dev_spec = 0x10;	/* =0x90 if WP=1 implies read-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 		if (sdebug_wp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 			dev_spec |= 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 		dev_spec = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	if (msense_6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 		arr[2] = dev_spec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 		arr[3] = bd_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 		offset = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 		arr[3] = dev_spec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 		if (16 == bd_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 			arr[4] = 0x1;	/* set LONGLBA bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 		arr[7] = bd_len;	/* assume 255 or less */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 		offset = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	ap = arr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 	if ((bd_len > 0) && (!sdebug_capacity))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 		sdebug_capacity = get_sdebug_capacity();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	if (8 == bd_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 		if (sdebug_capacity > 0xfffffffe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 			put_unaligned_be32(0xffffffff, ap + 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 			put_unaligned_be32(sdebug_capacity, ap + 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 		put_unaligned_be16(sdebug_sector_size, ap + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 		offset += bd_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 		ap = arr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 	} else if (16 == bd_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 		put_unaligned_be64((u64)sdebug_capacity, ap + 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 		put_unaligned_be32(sdebug_sector_size, ap + 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 		offset += bd_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 		ap = arr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 		/* TODO: Control Extension page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	bad_pcode = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	switch (pcode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 	case 0x1:	/* Read-Write error recovery page, direct access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 		len = resp_err_recov_pg(ap, pcontrol, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 		offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	case 0x2:	/* Disconnect-Reconnect page, all devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 		len = resp_disconnect_pg(ap, pcontrol, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 		offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 	case 0x3:       /* Format device page, direct access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 		if (is_disk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 			len = resp_format_pg(ap, pcontrol, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 			offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 			bad_pcode = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	case 0x8:	/* Caching page, direct access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 		if (is_disk || is_zbc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 			len = resp_caching_pg(ap, pcontrol, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 			offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 			bad_pcode = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 	case 0xa:	/* Control Mode page, all devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 		len = resp_ctrl_m_pg(ap, pcontrol, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 		offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 	case 0x19:	/* if spc==1 then sas phy, control+discover */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 		if ((subpcode > 0x2) && (subpcode < 0xff)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 		len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 		if ((0x0 == subpcode) || (0xff == subpcode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 			len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 		if ((0x1 == subpcode) || (0xff == subpcode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 			len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 						  target_dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 		if ((0x2 == subpcode) || (0xff == subpcode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 			len += resp_sas_sha_m_spg(ap + len, pcontrol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 		offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	case 0x1c:	/* Informational Exceptions Mode page, all devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 		len = resp_iec_m_pg(ap, pcontrol, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 		offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 	case 0x3f:	/* Read all Mode pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 		if ((0 == subpcode) || (0xff == subpcode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 			len = resp_err_recov_pg(ap, pcontrol, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 			len += resp_disconnect_pg(ap + len, pcontrol, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 			if (is_disk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 				len += resp_format_pg(ap + len, pcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 						      target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 				len += resp_caching_pg(ap + len, pcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 						       target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 			} else if (is_zbc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 				len += resp_caching_pg(ap + len, pcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 						       target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 			len += resp_ctrl_m_pg(ap + len, pcontrol, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 			len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 			if (0xff == subpcode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 				len += resp_sas_pcd_m_spg(ap + len, pcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 						  target, target_dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 				len += resp_sas_sha_m_spg(ap + len, pcontrol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 			len += resp_iec_m_pg(ap + len, pcontrol, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 			offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 		bad_pcode = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	if (bad_pcode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	if (msense_6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 		arr[0] = offset - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 		put_unaligned_be16((offset - 2), arr + 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	return fill_from_dev_buffer(scp, arr, min_t(u32, alloc_len, offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) #define SDEBUG_MAX_MSELECT_SZ 512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) static int resp_mode_select(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 			    struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 	int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 	int param_len, res, mpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	unsigned char *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	int mselect6 = (MODE_SELECT == cmd[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	memset(arr, 0, sizeof(arr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	pf = cmd[1] & 0x10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 	sp = cmd[1] & 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	res = fetch_to_dev_buffer(scp, arr, param_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	if (-1 == res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 		return DID_ERROR << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 	else if (sdebug_verbose && (res < param_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 		sdev_printk(KERN_INFO, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 			    "%s: cdb indicated=%d, IO sent=%d bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 			    __func__, param_len, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 	md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 	off = bd_len + (mselect6 ? 4 : 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 	if (md_len > 2 || off >= res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 		mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	mpage = arr[off] & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	ps = !!(arr[off] & 0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 	if (ps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 		mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 	spf = !!(arr[off] & 0x40);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 	pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 		       (arr[off + 1] + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 	if ((pg_len + off) > param_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 		mk_sense_buffer(scp, ILLEGAL_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 				PARAMETER_LIST_LENGTH_ERR, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	switch (mpage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	case 0x8:      /* Caching Mode page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 		if (caching_pg[1] == arr[off + 1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 			memcpy(caching_pg + 2, arr + off + 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 			       sizeof(caching_pg) - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 			goto set_mode_changed_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	case 0xa:      /* Control Mode page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 		if (ctrl_m_pg[1] == arr[off + 1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 			memcpy(ctrl_m_pg + 2, arr + off + 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 			       sizeof(ctrl_m_pg) - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 			if (ctrl_m_pg[4] & 0x8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 				sdebug_wp = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 				sdebug_wp = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 			sdebug_dsense = !!(ctrl_m_pg[2] & 0x4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 			goto set_mode_changed_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 	case 0x1c:      /* Informational Exceptions Mode page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 		if (iec_m_pg[1] == arr[off + 1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 			memcpy(iec_m_pg + 2, arr + off + 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 			       sizeof(iec_m_pg) - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 			goto set_mode_changed_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 	mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 	return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) set_mode_changed_ua:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 	set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) static int resp_temp_l_pg(unsigned char *arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 				     0x0, 0x1, 0x3, 0x2, 0x0, 65,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 	memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 	return sizeof(temp_l_pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) static int resp_ie_l_pg(unsigned char *arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 	unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 	memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 	if (iec_m_pg[2] & 0x4) {	/* TEST bit set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 		arr[4] = THRESHOLD_EXCEEDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 		arr[5] = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 	return sizeof(ie_l_pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) #define SDEBUG_MAX_LSENSE_SZ 512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) static int resp_log_sense(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 			  struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 	int ppc, sp, pcode, subpcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 	u32 alloc_len, len, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 	unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 	unsigned char *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 	memset(arr, 0, sizeof(arr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	ppc = cmd[1] & 0x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 	sp = cmd[1] & 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	if (ppc || sp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 	pcode = cmd[2] & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	subpcode = cmd[3] & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	alloc_len = get_unaligned_be16(cmd + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 	arr[0] = pcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 	if (0 == subpcode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 		switch (pcode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 		case 0x0:	/* Supported log pages log page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 			n = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 			arr[n++] = 0x0;		/* this page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 			arr[n++] = 0xd;		/* Temperature */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 			arr[n++] = 0x2f;	/* Informational exceptions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 			arr[3] = n - 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 		case 0xd:	/* Temperature log page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 			arr[3] = resp_temp_l_pg(arr + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 		case 0x2f:	/* Informational exceptions log page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 			arr[3] = resp_ie_l_pg(arr + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 	} else if (0xff == subpcode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 		arr[0] |= 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 		arr[1] = subpcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 		switch (pcode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 		case 0x0:	/* Supported log pages and subpages log page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 			n = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 			arr[n++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 			arr[n++] = 0x0;		/* 0,0 page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 			arr[n++] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 			arr[n++] = 0xff;	/* this page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 			arr[n++] = 0xd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 			arr[n++] = 0x0;		/* Temperature */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 			arr[n++] = 0x2f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 			arr[n++] = 0x0;	/* Informational exceptions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 			arr[3] = n - 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 		case 0xd:	/* Temperature subpages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 			n = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 			arr[n++] = 0xd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 			arr[n++] = 0x0;		/* Temperature */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 			arr[3] = n - 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 		case 0x2f:	/* Informational exceptions subpages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 			n = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 			arr[n++] = 0x2f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 			arr[n++] = 0x0;		/* Informational exceptions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 			arr[3] = n - 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 	len = min_t(u32, get_unaligned_be16(arr + 2) + 4, alloc_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 	return fill_from_dev_buffer(scp, arr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 		    min_t(u32, len, SDEBUG_MAX_INQ_ARR_SZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) static inline bool sdebug_dev_is_zoned(struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 	return devip->nr_zones != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) static struct sdeb_zone_state *zbc_zone(struct sdebug_dev_info *devip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 					unsigned long long lba)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 	return &devip->zstate[lba >> devip->zsize_shift];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) static inline bool zbc_zone_is_conv(struct sdeb_zone_state *zsp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 	return zsp->z_type == ZBC_ZONE_TYPE_CNV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) static void zbc_close_zone(struct sdebug_dev_info *devip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 			   struct sdeb_zone_state *zsp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 	enum sdebug_z_cond zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 	if (zbc_zone_is_conv(zsp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 	zc = zsp->z_cond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 	if (!(zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 	if (zc == ZC2_IMPLICIT_OPEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 		devip->nr_imp_open--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 		devip->nr_exp_open--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	if (zsp->z_wp == zsp->z_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 		zsp->z_cond = ZC1_EMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 		zsp->z_cond = ZC4_CLOSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 		devip->nr_closed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) static void zbc_close_imp_open_zone(struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 	struct sdeb_zone_state *zsp = &devip->zstate[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 	for (i = 0; i < devip->nr_zones; i++, zsp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 		if (zsp->z_cond == ZC2_IMPLICIT_OPEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 			zbc_close_zone(devip, zsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) static void zbc_open_zone(struct sdebug_dev_info *devip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 			  struct sdeb_zone_state *zsp, bool explicit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 	enum sdebug_z_cond zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 	if (zbc_zone_is_conv(zsp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 	zc = zsp->z_cond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 	if ((explicit && zc == ZC3_EXPLICIT_OPEN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 	    (!explicit && zc == ZC2_IMPLICIT_OPEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	/* Close an implicit open zone if necessary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 	if (explicit && zsp->z_cond == ZC2_IMPLICIT_OPEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 		zbc_close_zone(devip, zsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 	else if (devip->max_open &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 		 devip->nr_imp_open + devip->nr_exp_open >= devip->max_open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 		zbc_close_imp_open_zone(devip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 	if (zsp->z_cond == ZC4_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 		devip->nr_closed--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	if (explicit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 		zsp->z_cond = ZC3_EXPLICIT_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 		devip->nr_exp_open++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 		zsp->z_cond = ZC2_IMPLICIT_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 		devip->nr_imp_open++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) static void zbc_inc_wp(struct sdebug_dev_info *devip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 		       unsigned long long lba, unsigned int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 	struct sdeb_zone_state *zsp = zbc_zone(devip, lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 	unsigned long long n, end, zend = zsp->z_start + zsp->z_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 	if (zbc_zone_is_conv(zsp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 	if (zsp->z_type == ZBC_ZONE_TYPE_SWR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 		zsp->z_wp += num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 		if (zsp->z_wp >= zend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 			zsp->z_cond = ZC5_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 	while (num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 		if (lba != zsp->z_wp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 			zsp->z_non_seq_resource = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 		end = lba + num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 		if (end >= zend) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 			n = zend - lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 			zsp->z_wp = zend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 		} else if (end > zsp->z_wp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 			n = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 			zsp->z_wp = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 			n = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 		if (zsp->z_wp >= zend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 			zsp->z_cond = ZC5_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 		num -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 		lba += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 		if (num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 			zsp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 			zend = zsp->z_start + zsp->z_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) static int check_zbc_access_params(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 			unsigned long long lba, unsigned int num, bool write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 	struct scsi_device *sdp = scp->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 	struct sdeb_zone_state *zsp = zbc_zone(devip, lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 	struct sdeb_zone_state *zsp_end = zbc_zone(devip, lba + num - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 	if (!write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 		if (devip->zmodel == BLK_ZONED_HA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 		/* For host-managed, reads cannot cross zone types boundaries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 		if (zsp_end != zsp &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 		    zbc_zone_is_conv(zsp) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 		    !zbc_zone_is_conv(zsp_end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 					LBA_OUT_OF_RANGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 					READ_INVDATA_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 	/* No restrictions for writes within conventional zones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	if (zbc_zone_is_conv(zsp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 		if (!zbc_zone_is_conv(zsp_end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 					LBA_OUT_OF_RANGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 					WRITE_BOUNDARY_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 	if (zsp->z_type == ZBC_ZONE_TYPE_SWR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 		/* Writes cannot cross sequential zone boundaries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 		if (zsp_end != zsp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 					LBA_OUT_OF_RANGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 					WRITE_BOUNDARY_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 		/* Cannot write full zones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 		if (zsp->z_cond == ZC5_FULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 					INVALID_FIELD_IN_CDB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 		/* Writes must be aligned to the zone WP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 		if (lba != zsp->z_wp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 					LBA_OUT_OF_RANGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 					UNALIGNED_WRITE_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 	/* Handle implicit open of closed and empty zones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 	if (zsp->z_cond == ZC1_EMPTY || zsp->z_cond == ZC4_CLOSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 		if (devip->max_open &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 		    devip->nr_exp_open >= devip->max_open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 			mk_sense_buffer(scp, DATA_PROTECT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 					INSUFF_RES_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 					INSUFF_ZONE_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 		zbc_open_zone(devip, zsp, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) static inline int check_device_access_params
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 			(struct scsi_cmnd *scp, unsigned long long lba,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 			 unsigned int num, bool write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 	struct scsi_device *sdp = scp->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 	if (lba + num > sdebug_capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 	/* transfer length excessive (tie in to block limits VPD page) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 	if (num > sdebug_store_sectors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 		/* needs work to find which cdb byte 'num' comes from */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 	if (write && unlikely(sdebug_wp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 		mk_sense_buffer(scp, DATA_PROTECT, WRITE_PROTECTED, 0x2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 	if (sdebug_dev_is_zoned(devip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 		return check_zbc_access_params(scp, lba, num, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891)  * Note: if BUG_ON() fires it usually indicates a problem with the parser
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892)  * tables. Perhaps a missing F_FAKE_RW or FF_MEDIA_IO flag. Response functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893)  * that access any of the "stores" in struct sdeb_store_info should call this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894)  * function with bug_if_fake_rw set to true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) static inline struct sdeb_store_info *devip2sip(struct sdebug_dev_info *devip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 						bool bug_if_fake_rw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 	if (sdebug_fake_rw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 		BUG_ON(bug_if_fake_rw);	/* See note above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 	return xa_load(per_store_ap, devip->sdbg_host->si_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) /* Returns number of bytes copied or -1 if error. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 			    u32 sg_skip, u64 lba, u32 num, bool do_write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 	u64 block, rest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 	enum dma_data_direction dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 	struct scsi_data_buffer *sdb = &scp->sdb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 	u8 *fsp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 	if (do_write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 		dir = DMA_TO_DEVICE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 		write_since_sync = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 		dir = DMA_FROM_DEVICE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 	if (!sdb->length || !sip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 	if (scp->sc_data_direction != dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 	fsp = sip->storep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 	block = do_div(lba, sdebug_store_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 	if (block + num > sdebug_store_sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 		rest = block + num - sdebug_store_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 	ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 		   fsp + (block * sdebug_sector_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 		   (num - rest) * sdebug_sector_size, sg_skip, do_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 	if (ret != (num - rest) * sdebug_sector_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 	if (rest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 		ret += sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 			    fsp, rest * sdebug_sector_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 			    sg_skip + ((num - rest) * sdebug_sector_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 			    do_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) /* Returns number of bytes copied or -1 if error. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) static int do_dout_fetch(struct scsi_cmnd *scp, u32 num, u8 *doutp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 	struct scsi_data_buffer *sdb = &scp->sdb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 	if (!sdb->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 	if (scp->sc_data_direction != DMA_TO_DEVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 	return sg_copy_buffer(sdb->table.sgl, sdb->table.nents, doutp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 			      num * sdebug_sector_size, 0, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) /* If sip->storep+lba compares equal to arr(num), then copy top half of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963)  * arr into sip->storep+lba and return true. If comparison fails then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964)  * return false. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) static bool comp_write_worker(struct sdeb_store_info *sip, u64 lba, u32 num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 			      const u8 *arr, bool compare_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 	bool res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 	u64 block, rest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 	u32 store_blks = sdebug_store_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 	u32 lb_size = sdebug_sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 	u8 *fsp = sip->storep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 	block = do_div(lba, store_blks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 	if (block + num > store_blks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 		rest = block + num - store_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 	res = !memcmp(fsp + (block * lb_size), arr, (num - rest) * lb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 	if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 	if (rest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 		res = memcmp(fsp, arr + ((num - rest) * lb_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 			     rest * lb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 	if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 	if (compare_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 	arr += num * lb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 	memcpy(fsp + (block * lb_size), arr, (num - rest) * lb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 	if (rest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 		memcpy(fsp, arr + ((num - rest) * lb_size), rest * lb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) static __be16 dif_compute_csum(const void *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 	__be16 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 	if (sdebug_guard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 		csum = (__force __be16)ip_compute_csum(buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 		csum = cpu_to_be16(crc_t10dif(buf, len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 	return csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) static int dif_verify(struct t10_pi_tuple *sdt, const void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 		      sector_t sector, u32 ei_lba)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 	__be16 csum = dif_compute_csum(data, sdebug_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 	if (sdt->guard_tag != csum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 		pr_err("GUARD check failed on sector %lu rcvd 0x%04x, data 0x%04x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 			(unsigned long)sector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 			be16_to_cpu(sdt->guard_tag),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 			be16_to_cpu(csum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 		return 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 	if (sdebug_dif == T10_PI_TYPE1_PROTECTION &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 	    be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 		pr_err("REF check failed on sector %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 			(unsigned long)sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 		return 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 	if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 	    be32_to_cpu(sdt->ref_tag) != ei_lba) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 		pr_err("REF check failed on sector %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 			(unsigned long)sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 		return 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) static void dif_copy_prot(struct scsi_cmnd *scp, sector_t sector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 			  unsigned int sectors, bool read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 	size_t resid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 	void *paddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 	struct sdeb_store_info *sip = devip2sip((struct sdebug_dev_info *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 						scp->device->hostdata, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 	struct t10_pi_tuple *dif_storep = sip->dif_storep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 	const void *dif_store_end = dif_storep + sdebug_store_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 	struct sg_mapping_iter miter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 	/* Bytes of protection data to copy into sgl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 	resid = sectors * sizeof(*dif_storep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 	sg_miter_start(&miter, scsi_prot_sglist(scp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 		       scsi_prot_sg_count(scp), SG_MITER_ATOMIC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 		       (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 	while (sg_miter_next(&miter) && resid > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 		size_t len = min_t(size_t, miter.length, resid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 		void *start = dif_store(sip, sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 		size_t rest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 		if (dif_store_end < start + len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 			rest = start + len - dif_store_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 		paddr = miter.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 		if (read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 			memcpy(paddr, start, len - rest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 			memcpy(start, paddr, len - rest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 		if (rest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 			if (read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 				memcpy(paddr + len - rest, dif_storep, rest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 				memcpy(dif_storep, paddr + len - rest, rest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 		sector += len / sizeof(*dif_storep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 		resid -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 	sg_miter_stop(&miter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) static int prot_verify_read(struct scsi_cmnd *scp, sector_t start_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 			    unsigned int sectors, u32 ei_lba)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 	sector_t sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 	struct sdeb_store_info *sip = devip2sip((struct sdebug_dev_info *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 						scp->device->hostdata, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 	struct t10_pi_tuple *sdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 	for (i = 0; i < sectors; i++, ei_lba++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 		sector = start_sec + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 		sdt = dif_store(sip, sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 		if (sdt->app_tag == cpu_to_be16(0xffff))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 		ret = dif_verify(sdt, lba2fake_store(sip, sector), sector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 				 ei_lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 			dif_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 	dif_copy_prot(scp, start_sec, sectors, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 	dix_reads++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 	bool check_prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 	u32 num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 	u32 ei_lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 	u64 lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 	struct sdeb_store_info *sip = devip2sip(devip, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 	rwlock_t *macc_lckp = sip ? &sip->macc_lck : &sdeb_fake_rw_lck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 	switch (cmd[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) 	case READ_16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 		ei_lba = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) 		lba = get_unaligned_be64(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 		num = get_unaligned_be32(cmd + 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) 		check_prot = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 	case READ_10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 		ei_lba = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 		lba = get_unaligned_be32(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 		num = get_unaligned_be16(cmd + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 		check_prot = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 	case READ_6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 		ei_lba = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 		lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 		      (u32)(cmd[1] & 0x1f) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 		num = (0 == cmd[4]) ? 256 : cmd[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) 		check_prot = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 	case READ_12:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 		ei_lba = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 		lba = get_unaligned_be32(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 		num = get_unaligned_be32(cmd + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 		check_prot = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 	case XDWRITEREAD_10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 		ei_lba = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 		lba = get_unaligned_be32(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 		num = get_unaligned_be16(cmd + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 		check_prot = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 	default:	/* assume READ(32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 		lba = get_unaligned_be64(cmd + 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 		ei_lba = get_unaligned_be32(cmd + 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) 		num = get_unaligned_be32(cmd + 28);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 		check_prot = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 	if (unlikely(have_dif_prot && check_prot)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 		if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 		    (cmd[1] & 0xe0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 			mk_sense_invalid_opcode(scp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 		if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 		     sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 		    (cmd[1] & 0xe0) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) 			sdev_printk(KERN_ERR, scp->device, "Unprotected RD "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) 				    "to DIF device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) 	if (unlikely((sdebug_opts & SDEBUG_OPT_SHORT_TRANSFER) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) 		     atomic_read(&sdeb_inject_pending))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) 		num /= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) 		atomic_set(&sdeb_inject_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) 	ret = check_device_access_params(scp, lba, num, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 	if (unlikely((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) 		     (lba <= (sdebug_medium_error_start + sdebug_medium_error_count - 1)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) 		     ((lba + num) > sdebug_medium_error_start))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) 		/* claim unrecoverable read error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) 		mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) 		/* set info field and valid bit for fixed descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 		if (0x70 == (scp->sense_buffer[0] & 0x7f)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) 			scp->sense_buffer[0] |= 0x80;	/* Valid bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 			ret = (lba < OPT_MEDIUM_ERR_ADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 			      ? OPT_MEDIUM_ERR_ADDR : (int)lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 			put_unaligned_be32(ret, scp->sense_buffer + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 		scsi_set_resid(scp, scsi_bufflen(scp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 	read_lock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 	/* DIX + T10 DIF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 	if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 		int prot_ret = prot_verify_read(scp, lba, num, ei_lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 		if (prot_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 			read_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) 			mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 			return illegal_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 	ret = do_device_access(sip, scp, 0, lba, num, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 	read_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 	if (unlikely(ret == -1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 		return DID_ERROR << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 	scsi_set_resid(scp, scsi_bufflen(scp) - ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 	if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 		     atomic_read(&sdeb_inject_pending))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 		if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 			mk_sense_buffer(scp, RECOVERED_ERROR, THRESHOLD_EXCEEDED, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 			atomic_set(&sdeb_inject_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 		} else if (sdebug_opts & SDEBUG_OPT_DIF_ERR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 			/* Logical block guard check failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) 			mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 			atomic_set(&sdeb_inject_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 			return illegal_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) 		} else if (SDEBUG_OPT_DIX_ERR & sdebug_opts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 			mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 			atomic_set(&sdeb_inject_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 			return illegal_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) static void dump_sector(unsigned char *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) 	int i, j, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 	pr_err(">>> Sector Dump <<<\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 	for (i = 0 ; i < len ; i += 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 		char b[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 		for (j = 0, n = 0; j < 16; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) 			unsigned char c = buf[i+j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 			if (c >= 0x20 && c < 0x7e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 				n += scnprintf(b + n, sizeof(b) - n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 					       " %c ", buf[i+j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 				n += scnprintf(b + n, sizeof(b) - n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 					       "%02x ", buf[i+j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) 		pr_err("%04d: %s\n", i, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 			     unsigned int sectors, u32 ei_lba)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 	struct t10_pi_tuple *sdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 	void *daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 	sector_t sector = start_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 	int ppage_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 	int dpage_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 	struct sg_mapping_iter diter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 	struct sg_mapping_iter piter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) 	BUG_ON(scsi_sg_count(SCpnt) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 	BUG_ON(scsi_prot_sg_count(SCpnt) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 	sg_miter_start(&piter, scsi_prot_sglist(SCpnt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 			scsi_prot_sg_count(SCpnt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 			SG_MITER_ATOMIC | SG_MITER_FROM_SG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 	sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 			SG_MITER_ATOMIC | SG_MITER_FROM_SG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) 	/* For each protection page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) 	while (sg_miter_next(&piter)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) 		dpage_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) 		if (WARN_ON(!sg_miter_next(&diter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 			ret = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 		for (ppage_offset = 0; ppage_offset < piter.length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) 		     ppage_offset += sizeof(struct t10_pi_tuple)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) 			/* If we're at the end of the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) 			 * data page advance to the next one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) 			if (dpage_offset >= diter.length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 				if (WARN_ON(!sg_miter_next(&diter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 					ret = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 				dpage_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) 			sdt = piter.addr + ppage_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 			daddr = diter.addr + dpage_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 			ret = dif_verify(sdt, daddr, sector, ei_lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) 				dump_sector(daddr, sdebug_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) 			sector++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) 			ei_lba++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) 			dpage_offset += sdebug_sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) 		diter.consumed = dpage_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) 		sg_miter_stop(&diter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) 	sg_miter_stop(&piter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) 	dif_copy_prot(SCpnt, start_sec, sectors, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 	dix_writes++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 	dif_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 	sg_miter_stop(&diter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 	sg_miter_stop(&piter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) static unsigned long lba_to_map_index(sector_t lba)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) 	if (sdebug_unmap_alignment)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) 		lba += sdebug_unmap_granularity - sdebug_unmap_alignment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 	sector_div(lba, sdebug_unmap_granularity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) 	return lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) static sector_t map_index_to_lba(unsigned long index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) 	sector_t lba = index * sdebug_unmap_granularity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) 	if (sdebug_unmap_alignment)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 		lba -= sdebug_unmap_granularity - sdebug_unmap_alignment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) 	return lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) static unsigned int map_state(struct sdeb_store_info *sip, sector_t lba,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 			      unsigned int *num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 	sector_t end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) 	unsigned int mapped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 	unsigned long index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) 	index = lba_to_map_index(lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) 	mapped = test_bit(index, sip->map_storep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) 	if (mapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 		next = find_next_zero_bit(sip->map_storep, map_size, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 		next = find_next_bit(sip->map_storep, map_size, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 	end = min_t(sector_t, sdebug_store_sectors,  map_index_to_lba(next));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 	*num = end - lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 	return mapped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) static void map_region(struct sdeb_store_info *sip, sector_t lba,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) 		       unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) 	sector_t end = lba + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) 	while (lba < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) 		unsigned long index = lba_to_map_index(lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) 		if (index < map_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) 			set_bit(index, sip->map_storep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) 		lba = map_index_to_lba(index + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) static void unmap_region(struct sdeb_store_info *sip, sector_t lba,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) 			 unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 	sector_t end = lba + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 	u8 *fsp = sip->storep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) 	while (lba < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) 		unsigned long index = lba_to_map_index(lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 		if (lba == map_index_to_lba(index) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) 		    lba + sdebug_unmap_granularity <= end &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) 		    index < map_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) 			clear_bit(index, sip->map_storep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) 			if (sdebug_lbprz) {  /* for LBPRZ=2 return 0xff_s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) 				memset(fsp + lba * sdebug_sector_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) 				       (sdebug_lbprz & 1) ? 0 : 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) 				       sdebug_sector_size *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) 				       sdebug_unmap_granularity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) 			if (sip->dif_storep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) 				memset(sip->dif_storep + lba, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) 				       sizeof(*sip->dif_storep) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 				       sdebug_unmap_granularity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 		lba = map_index_to_lba(index + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) 	bool check_prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) 	u32 num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) 	u32 ei_lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 	u64 lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) 	struct sdeb_store_info *sip = devip2sip(devip, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 	rwlock_t *macc_lckp = &sip->macc_lck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) 	switch (cmd[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) 	case WRITE_16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) 		ei_lba = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) 		lba = get_unaligned_be64(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) 		num = get_unaligned_be32(cmd + 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) 		check_prot = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) 	case WRITE_10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) 		ei_lba = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) 		lba = get_unaligned_be32(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) 		num = get_unaligned_be16(cmd + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) 		check_prot = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) 	case WRITE_6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) 		ei_lba = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) 		lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 		      (u32)(cmd[1] & 0x1f) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) 		num = (0 == cmd[4]) ? 256 : cmd[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 		check_prot = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 	case WRITE_12:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 		ei_lba = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) 		lba = get_unaligned_be32(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) 		num = get_unaligned_be32(cmd + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) 		check_prot = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 	case 0x53:	/* XDWRITEREAD(10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) 		ei_lba = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) 		lba = get_unaligned_be32(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) 		num = get_unaligned_be16(cmd + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) 		check_prot = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) 	default:	/* assume WRITE(32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 		lba = get_unaligned_be64(cmd + 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 		ei_lba = get_unaligned_be32(cmd + 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 		num = get_unaligned_be32(cmd + 28);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 		check_prot = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 	if (unlikely(have_dif_prot && check_prot)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 		if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 		    (cmd[1] & 0xe0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 			mk_sense_invalid_opcode(scp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) 		if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 		     sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 		    (cmd[1] & 0xe0) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) 			sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 				    "to DIF device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) 	write_lock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) 	ret = check_device_access_params(scp, lba, num, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) 		write_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) 	/* DIX + T10 DIF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) 	if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 		int prot_ret = prot_verify_write(scp, lba, num, ei_lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) 		if (prot_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 			write_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) 			mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) 			return illegal_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 	ret = do_device_access(sip, scp, 0, lba, num, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) 	if (unlikely(scsi_debug_lbp()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) 		map_region(sip, lba, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) 	/* If ZBC zone then bump its write pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 	if (sdebug_dev_is_zoned(devip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) 		zbc_inc_wp(devip, lba, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) 	write_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 	if (unlikely(-1 == ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 		return DID_ERROR << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) 	else if (unlikely(sdebug_verbose &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 			  (ret < (num * sdebug_sector_size))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 		sdev_printk(KERN_INFO, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 			    "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 			    my_name, num * sdebug_sector_size, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 	if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) 		     atomic_read(&sdeb_inject_pending))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 		if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 			mk_sense_buffer(scp, RECOVERED_ERROR, THRESHOLD_EXCEEDED, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 			atomic_set(&sdeb_inject_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 		} else if (sdebug_opts & SDEBUG_OPT_DIF_ERR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 			/* Logical block guard check failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 			mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) 			atomic_set(&sdeb_inject_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) 			return illegal_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) 		} else if (sdebug_opts & SDEBUG_OPT_DIX_ERR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) 			mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) 			atomic_set(&sdeb_inject_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) 			return illegal_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532)  * T10 has only specified WRITE SCATTERED(16) and WRITE SCATTERED(32).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533)  * No READ GATHERED yet (requires bidi or long cdb holding gather list).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) static int resp_write_scat(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) 			   struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) 	u8 *lrdp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) 	u8 *up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) 	struct sdeb_store_info *sip = devip2sip(devip, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) 	rwlock_t *macc_lckp = &sip->macc_lck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) 	u8 wrprotect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 	u16 lbdof, num_lrd, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) 	u32 num, num_by, bt_len, lbdof_blen, sg_off, cum_lb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 	u32 lb_size = sdebug_sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) 	u32 ei_lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) 	u64 lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) 	int ret, res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) 	bool is_16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) 	static const u32 lrd_size = 32; /* + parameter list header size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) 	if (cmd[0] == VARIABLE_LENGTH_CMD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 		is_16 = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) 		wrprotect = (cmd[10] >> 5) & 0x7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 		lbdof = get_unaligned_be16(cmd + 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) 		num_lrd = get_unaligned_be16(cmd + 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) 		bt_len = get_unaligned_be32(cmd + 28);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 	} else {        /* that leaves WRITE SCATTERED(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 		is_16 = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) 		wrprotect = (cmd[2] >> 5) & 0x7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) 		lbdof = get_unaligned_be16(cmd + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 		num_lrd = get_unaligned_be16(cmd + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) 		bt_len = get_unaligned_be32(cmd + 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 		if (unlikely(have_dif_prot)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) 			if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 			    wrprotect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 				mk_sense_invalid_opcode(scp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) 				return illegal_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) 			if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) 			     sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) 			     wrprotect == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) 				sdev_printk(KERN_ERR, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) 					    "Unprotected WR to DIF device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) 	if ((num_lrd == 0) || (bt_len == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) 		return 0;       /* T10 says these do-nothings are not errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) 	if (lbdof == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) 		if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) 			sdev_printk(KERN_INFO, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) 				"%s: %s: LB Data Offset field bad\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) 				my_name, __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) 		return illegal_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) 	lbdof_blen = lbdof * lb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) 	if ((lrd_size + (num_lrd * lrd_size)) > lbdof_blen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) 		if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 			sdev_printk(KERN_INFO, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) 				"%s: %s: LBA range descriptors don't fit\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) 				my_name, __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) 		return illegal_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) 	lrdp = kzalloc(lbdof_blen, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) 	if (lrdp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) 		return SCSI_MLQUEUE_HOST_BUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) 	if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) 		sdev_printk(KERN_INFO, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) 			"%s: %s: Fetch header+scatter_list, lbdof_blen=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 			my_name, __func__, lbdof_blen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) 	res = fetch_to_dev_buffer(scp, lrdp, lbdof_blen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) 	if (res == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) 		ret = DID_ERROR << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) 	write_lock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) 	sg_off = lbdof_blen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) 	/* Spec says Buffer xfer Length field in number of LBs in dout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) 	cum_lb = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) 	for (k = 0, up = lrdp + lrd_size; k < num_lrd; ++k, up += lrd_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) 		lba = get_unaligned_be64(up + 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) 		num = get_unaligned_be32(up + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) 		if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) 			sdev_printk(KERN_INFO, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) 				"%s: %s: k=%d  LBA=0x%llx num=%u  sg_off=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) 				my_name, __func__, k, lba, num, sg_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) 		if (num == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) 		ret = check_device_access_params(scp, lba, num, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) 			goto err_out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) 		num_by = num * lb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) 		ei_lba = is_16 ? 0 : get_unaligned_be32(up + 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) 		if ((cum_lb + num) > bt_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) 			if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) 				sdev_printk(KERN_INFO, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) 				    "%s: %s: sum of blocks > data provided\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 				    my_name, __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 			mk_sense_buffer(scp, ILLEGAL_REQUEST, WRITE_ERROR_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) 					0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 			ret = illegal_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 			goto err_out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 		/* DIX + T10 DIF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 		if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) 			int prot_ret = prot_verify_write(scp, lba, num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 							 ei_lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 			if (prot_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 				mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 						prot_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 				ret = illegal_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) 				goto err_out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 		ret = do_device_access(sip, scp, sg_off, lba, num, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 		/* If ZBC zone then bump its write pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 		if (sdebug_dev_is_zoned(devip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 			zbc_inc_wp(devip, lba, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 		if (unlikely(scsi_debug_lbp()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 			map_region(sip, lba, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 		if (unlikely(-1 == ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 			ret = DID_ERROR << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 			goto err_out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 		} else if (unlikely(sdebug_verbose && (ret < num_by)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 			sdev_printk(KERN_INFO, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 			    "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 			    my_name, num_by, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 		if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 			     atomic_read(&sdeb_inject_pending))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 			if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 				mk_sense_buffer(scp, RECOVERED_ERROR, THRESHOLD_EXCEEDED, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 				atomic_set(&sdeb_inject_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 				ret = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 				goto err_out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 			} else if (sdebug_opts & SDEBUG_OPT_DIF_ERR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) 				/* Logical block guard check failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) 				mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) 				atomic_set(&sdeb_inject_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) 				ret = illegal_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) 				goto err_out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) 			} else if (sdebug_opts & SDEBUG_OPT_DIX_ERR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) 				mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) 				atomic_set(&sdeb_inject_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) 				ret = illegal_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) 				goto err_out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) 		sg_off += num_by;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) 		cum_lb += num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) err_out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) 	write_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) 	kfree(lrdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) 			   u32 ei_lba, bool unmap, bool ndob)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) 	struct scsi_device *sdp = scp->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 	struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) 	unsigned long long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) 	u64 block, lbaa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) 	u32 lb_size = sdebug_sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) 	struct sdeb_store_info *sip = devip2sip((struct sdebug_dev_info *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) 						scp->device->hostdata, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) 	rwlock_t *macc_lckp = &sip->macc_lck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) 	u8 *fs1p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) 	u8 *fsp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) 	write_lock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) 	ret = check_device_access_params(scp, lba, num, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) 		write_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 	if (unmap && scsi_debug_lbp()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) 		unmap_region(sip, lba, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) 	lbaa = lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) 	block = do_div(lbaa, sdebug_store_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) 	/* if ndob then zero 1 logical block, else fetch 1 logical block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) 	fsp = sip->storep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) 	fs1p = fsp + (block * lb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) 	if (ndob) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) 		memset(fs1p, 0, lb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) 		ret = fetch_to_dev_buffer(scp, fs1p, lb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) 	if (-1 == ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) 		write_unlock(&sip->macc_lck);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) 		return DID_ERROR << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) 	} else if (sdebug_verbose && !ndob && (ret < lb_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) 		sdev_printk(KERN_INFO, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) 			    "%s: %s: lb size=%u, IO sent=%d bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) 			    my_name, "write same", lb_size, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) 	/* Copy first sector to remaining blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) 	for (i = 1 ; i < num ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) 		lbaa = lba + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) 		block = do_div(lbaa, sdebug_store_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) 		memmove(fsp + (block * lb_size), fs1p, lb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) 	if (scsi_debug_lbp())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 		map_region(sip, lba, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) 	/* If ZBC zone then bump its write pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) 	if (sdebug_dev_is_zoned(devip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) 		zbc_inc_wp(devip, lba, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) 	write_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) static int resp_write_same_10(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) 			      struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) 	u32 lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) 	u16 num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) 	u32 ei_lba = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) 	bool unmap = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) 	if (cmd[1] & 0x8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) 		if (sdebug_lbpws10 == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) 			unmap = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) 	lba = get_unaligned_be32(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 	num = get_unaligned_be16(cmd + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) 	if (num > sdebug_write_same_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) 	return resp_write_same(scp, lba, num, ei_lba, unmap, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) static int resp_write_same_16(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) 			      struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) 	u64 lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) 	u32 num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) 	u32 ei_lba = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) 	bool unmap = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) 	bool ndob = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) 	if (cmd[1] & 0x8) {	/* UNMAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) 		if (sdebug_lbpws == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) 			mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) 			unmap = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) 	if (cmd[1] & 0x1)  /* NDOB (no data-out buffer, assumes zeroes) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) 		ndob = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) 	lba = get_unaligned_be64(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) 	num = get_unaligned_be32(cmd + 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) 	if (num > sdebug_write_same_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) 	return resp_write_same(scp, lba, num, ei_lba, unmap, ndob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) /* Note the mode field is in the same position as the (lower) service action
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815)  * field. For the Report supported operation codes command, SPC-4 suggests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816)  * each mode of this command should be reported separately; for future. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) static int resp_write_buffer(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) 			     struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) 	struct scsi_device *sdp = scp->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) 	struct sdebug_dev_info *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) 	u8 mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) 	mode = cmd[1] & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) 	switch (mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) 	case 0x4:	/* download microcode (MC) and activate (ACT) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) 		/* set UAs on this device only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) 		set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) 		set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) 	case 0x5:	/* download MC, save and ACT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) 		set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) 	case 0x6:	/* download MC with offsets and ACT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) 		/* set UAs on most devices (LUs) in this target */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) 		list_for_each_entry(dp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) 				    &devip->sdbg_host->dev_info_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) 				    dev_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) 			if (dp->target == sdp->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) 				set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) 				if (devip != dp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) 					set_bit(SDEBUG_UA_MICROCODE_CHANGED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) 						dp->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) 	case 0x7:	/* download MC with offsets, save, and ACT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) 		/* set UA on all devices (LUs) in this target */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) 		list_for_each_entry(dp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) 				    &devip->sdbg_host->dev_info_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) 				    dev_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) 			if (dp->target == sdp->id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) 				set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) 					dp->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) 		/* do nothing for this command for other mode values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) static int resp_comp_write(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) 			   struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) 	u8 *arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) 	struct sdeb_store_info *sip = devip2sip(devip, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) 	rwlock_t *macc_lckp = &sip->macc_lck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) 	u64 lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) 	u32 dnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) 	u32 lb_size = sdebug_sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) 	u8 num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) 	lba = get_unaligned_be64(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) 	num = cmd[13];		/* 1 to a maximum of 255 logical blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) 	if (0 == num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) 		return 0;	/* degenerate case, not an error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) 	if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) 	    (cmd[1] & 0xe0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) 		mk_sense_invalid_opcode(scp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) 	if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) 	     sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) 	    (cmd[1] & 0xe0) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) 		sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) 			    "to DIF device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) 	ret = check_device_access_params(scp, lba, num, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) 	dnum = 2 * num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) 	arr = kcalloc(lb_size, dnum, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) 	if (NULL == arr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) 				INSUFF_RES_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) 	write_lock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) 	ret = do_dout_fetch(scp, dnum, arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) 	if (ret == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) 		retval = DID_ERROR << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) 	} else if (sdebug_verbose && (ret < (dnum * lb_size)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) 		sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) 			    "indicated=%u, IO sent=%d bytes\n", my_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) 			    dnum * lb_size, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) 	if (!comp_write_worker(sip, lba, num, arr, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) 		mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) 		retval = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) 	if (scsi_debug_lbp())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) 		map_region(sip, lba, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) 	write_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) 	kfree(arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) struct unmap_block_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) 	__be64	lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) 	__be32	blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) 	__be32	__reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) 	unsigned char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) 	struct unmap_block_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) 	struct sdeb_store_info *sip = devip2sip(devip, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) 	rwlock_t *macc_lckp = &sip->macc_lck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) 	unsigned int i, payload_len, descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) 	if (!scsi_debug_lbp())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) 		return 0;	/* fib and say its done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) 	payload_len = get_unaligned_be16(scp->cmnd + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) 	BUG_ON(scsi_bufflen(scp) != payload_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) 	descriptors = (payload_len - 8) / 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) 	if (descriptors > sdebug_unmap_max_desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) 	buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) 	if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) 				INSUFF_RES_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) 	scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) 	BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) 	BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) 	desc = (void *)&buf[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) 	write_lock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) 	for (i = 0 ; i < descriptors ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) 		unsigned long long lba = get_unaligned_be64(&desc[i].lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) 		unsigned int num = get_unaligned_be32(&desc[i].blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) 		ret = check_device_access_params(scp, lba, num, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) 		unmap_region(sip, lba, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) 	write_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) #define SDEBUG_GET_LBA_STATUS_LEN 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) static int resp_get_lba_status(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) 			       struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) 	u64 lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) 	u32 alloc_len, mapped, num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) 	u8 arr[SDEBUG_GET_LBA_STATUS_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) 	lba = get_unaligned_be64(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) 	alloc_len = get_unaligned_be32(cmd + 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) 	if (alloc_len < 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) 	ret = check_device_access_params(scp, lba, 1, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) 	if (scsi_debug_lbp()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) 		struct sdeb_store_info *sip = devip2sip(devip, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) 		mapped = map_state(sip, lba, &num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) 		mapped = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) 		/* following just in case virtual_gb changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) 		sdebug_capacity = get_sdebug_capacity();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) 		if (sdebug_capacity - lba <= 0xffffffff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) 			num = sdebug_capacity - lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) 			num = 0xffffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) 	memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) 	put_unaligned_be32(20, arr);		/* Parameter Data Length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) 	put_unaligned_be64(lba, arr + 8);	/* LBA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) 	put_unaligned_be32(num, arr + 16);	/* Number of blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) 	arr[20] = !mapped;		/* prov_stat=0: mapped; 1: dealloc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) 	return fill_from_dev_buffer(scp, arr, SDEBUG_GET_LBA_STATUS_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) static int resp_sync_cache(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) 			   struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) 	int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) 	u64 lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) 	u32 num_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) 	if (cmd[0] == SYNCHRONIZE_CACHE) {	/* 10 byte cdb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) 		lba = get_unaligned_be32(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) 		num_blocks = get_unaligned_be16(cmd + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) 	} else {				/* SYNCHRONIZE_CACHE(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) 		lba = get_unaligned_be64(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) 		num_blocks = get_unaligned_be32(cmd + 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) 	if (lba + num_blocks > sdebug_capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) 	if (!write_since_sync || (cmd[1] & 0x2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) 		res = SDEG_RES_IMMED_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) 	else		/* delay if write_since_sync and IMMED clear */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) 		write_since_sync = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058)  * Assuming the LBA+num_blocks is not out-of-range, this function will return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059)  * CONDITION MET if the specified blocks will/have fitted in the cache, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060)  * a GOOD status otherwise. Model a disk with a big cache and yield
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061)  * CONDITION MET. Actually tries to bring range in main memory into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062)  * cache associated with the CPU(s).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) static int resp_pre_fetch(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) 			  struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) 	int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) 	u64 lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) 	u64 block, rest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) 	u32 nblks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) 	struct sdeb_store_info *sip = devip2sip(devip, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) 	rwlock_t *macc_lckp = &sip->macc_lck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) 	u8 *fsp = sip->storep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) 	if (cmd[0] == PRE_FETCH) {	/* 10 byte cdb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) 		lba = get_unaligned_be32(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) 		nblks = get_unaligned_be16(cmd + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) 	} else {			/* PRE-FETCH(16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) 		lba = get_unaligned_be64(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) 		nblks = get_unaligned_be32(cmd + 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) 	if (lba + nblks > sdebug_capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) 	if (!fsp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) 	/* PRE-FETCH spec says nothing about LBP or PI so skip them */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) 	block = do_div(lba, sdebug_store_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) 	if (block + nblks > sdebug_store_sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) 		rest = block + nblks - sdebug_store_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) 	/* Try to bring the PRE-FETCH range into CPU's cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) 	read_lock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) 	prefetch_range(fsp + (sdebug_sector_size * block),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) 		       (nblks - rest) * sdebug_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) 	if (rest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) 		prefetch_range(fsp, rest * sdebug_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) 	read_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) fini:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) 	if (cmd[1] & 0x2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) 		res = SDEG_RES_IMMED_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) 	return res | condition_met_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) #define RL_BUCKET_ELEMS 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) /* Even though each pseudo target has a REPORT LUNS "well known logical unit"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110)  * (W-LUN), the normal Linux scanning logic does not associate it with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111)  * device (e.g. /dev/sg7). The following magic will make that association:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112)  *   "cd /sys/class/scsi_host/host<n> ; echo '- - 49409' > scan"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113)  * where <n> is a host number. If there are multiple targets in a host then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114)  * the above will associate a W-LUN to each target. To only get a W-LUN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115)  * for target 2, then use "echo '- 2 49409' > scan" .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) static int resp_report_luns(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) 			    struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) 	unsigned char *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) 	unsigned int alloc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) 	unsigned char select_report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) 	u64 lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) 	struct scsi_lun *lun_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) 	u8 arr[RL_BUCKET_ELEMS * sizeof(struct scsi_lun)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) 	unsigned int lun_cnt;	/* normal LUN count (max: 256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) 	unsigned int wlun_cnt;	/* report luns W-LUN count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) 	unsigned int tlun_cnt;	/* total LUN count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) 	unsigned int rlen;	/* response length (in bytes) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) 	int k, j, n, res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) 	unsigned int off_rsp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) 	const int sz_lun = sizeof(struct scsi_lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) 	clear_luns_changed_on_target(devip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) 	select_report = cmd[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) 	alloc_len = get_unaligned_be32(cmd + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) 	if (alloc_len < 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) 		pr_err("alloc len too small %d\n", alloc_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) 	switch (select_report) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) 	case 0:		/* all LUNs apart from W-LUNs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) 		lun_cnt = sdebug_max_luns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) 		wlun_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) 	case 1:		/* only W-LUNs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) 		lun_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) 		wlun_cnt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) 	case 2:		/* all LUNs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) 		lun_cnt = sdebug_max_luns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) 		wlun_cnt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) 	case 0x10:	/* only administrative LUs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) 	case 0x11:	/* see SPC-5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) 	case 0x12:	/* only subsiduary LUs owned by referenced LU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) 		pr_debug("select report invalid %d\n", select_report);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) 	if (sdebug_no_lun_0 && (lun_cnt > 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) 		--lun_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) 	tlun_cnt = lun_cnt + wlun_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) 	rlen = tlun_cnt * sz_lun;	/* excluding 8 byte header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) 	scsi_set_resid(scp, scsi_bufflen(scp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) 	pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) 		 select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) 	/* loops rely on sizeof response header same as sizeof lun (both 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) 	lun = sdebug_no_lun_0 ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) 	for (k = 0, j = 0, res = 0; true; ++k, j = 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) 		memset(arr, 0, sizeof(arr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) 		lun_p = (struct scsi_lun *)&arr[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) 		if (k == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) 			put_unaligned_be32(rlen, &arr[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) 			++lun_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) 			j = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) 		for ( ; j < RL_BUCKET_ELEMS; ++j, ++lun_p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) 			if ((k * RL_BUCKET_ELEMS) + j > lun_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) 			int_to_scsilun(lun++, lun_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) 			if (lun > 1 && sdebug_lun_am == SAM_LUN_AM_FLAT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) 				lun_p->scsi_lun[0] |= 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) 		if (j < RL_BUCKET_ELEMS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) 		n = j * sz_lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) 		res = p_fill_from_dev_buffer(scp, arr, n, off_rsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) 		if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) 			return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) 		off_rsp += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) 	if (wlun_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) 		int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) 		++j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) 	if (j > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) 		res = p_fill_from_dev_buffer(scp, arr, j * sz_lun, off_rsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) static int resp_verify(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) 	bool is_bytchk3 = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) 	u8 bytchk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) 	int ret, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) 	u32 vnum, a_num, off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) 	const u32 lb_size = sdebug_sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) 	u64 lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) 	u8 *arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) 	struct sdeb_store_info *sip = devip2sip(devip, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) 	rwlock_t *macc_lckp = &sip->macc_lck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) 	bytchk = (cmd[1] >> 1) & 0x3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) 	if (bytchk == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) 		return 0;	/* always claim internal verify okay */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) 	} else if (bytchk == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) 		mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) 	} else if (bytchk == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) 		is_bytchk3 = true;	/* 1 block sent, compared repeatedly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) 	switch (cmd[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) 	case VERIFY_16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) 		lba = get_unaligned_be64(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) 		vnum = get_unaligned_be32(cmd + 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) 	case VERIFY:		/* is VERIFY(10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) 		lba = get_unaligned_be32(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) 		vnum = get_unaligned_be16(cmd + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) 		mk_sense_invalid_opcode(scp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) 	if (vnum == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) 		return 0;	/* not an error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) 	a_num = is_bytchk3 ? 1 : vnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248) 	/* Treat following check like one for read (i.e. no write) access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) 	ret = check_device_access_params(scp, lba, a_num, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253) 	arr = kcalloc(lb_size, vnum, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) 	if (!arr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) 				INSUFF_RES_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) 	/* Not changing store, so only need read access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) 	read_lock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) 	ret = do_dout_fetch(scp, a_num, arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) 	if (ret == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) 		ret = DID_ERROR << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) 	} else if (sdebug_verbose && (ret < (a_num * lb_size))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) 		sdev_printk(KERN_INFO, scp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268) 			    "%s: %s: cdb indicated=%u, IO sent=%d bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) 			    my_name, __func__, a_num * lb_size, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) 	if (is_bytchk3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) 		for (j = 1, off = lb_size; j < vnum; ++j, off += lb_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) 			memcpy(arr + off, arr, lb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) 	if (!comp_write_worker(sip, lba, vnum, arr, true)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) 		mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) 		ret = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) 	read_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) 	kfree(arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) #define RZONES_DESC_HD 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) /* Report zones depending on start LBA nad reporting options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) static int resp_report_zones(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) 			     struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) 	unsigned int i, max_zones, rep_max_zones, nrz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) 	u32 alloc_len, rep_opts, rep_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) 	bool partial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) 	u64 lba, zs_lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) 	u8 *arr = NULL, *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) 	struct sdeb_zone_state *zsp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301) 	struct sdeb_store_info *sip = devip2sip(devip, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) 	rwlock_t *macc_lckp = sip ? &sip->macc_lck : &sdeb_fake_rw_lck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) 	if (!sdebug_dev_is_zoned(devip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) 		mk_sense_invalid_opcode(scp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) 	zs_lba = get_unaligned_be64(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) 	alloc_len = get_unaligned_be32(cmd + 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) 	if (alloc_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) 		return 0;	/* not an error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) 	rep_opts = cmd[14] & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) 	partial = cmd[14] & 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) 	if (zs_lba >= sdebug_capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) 	max_zones = devip->nr_zones - (zs_lba >> devip->zsize_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) 	rep_max_zones = min((alloc_len - 64) >> ilog2(RZONES_DESC_HD),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) 			    max_zones);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) 	arr = kzalloc(alloc_len, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) 	if (!arr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) 				INSUFF_RES_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) 	read_lock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) 	desc = arr + 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) 	for (i = 0; i < max_zones; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) 		lba = zs_lba + devip->zsize * i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) 		if (lba > sdebug_capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) 		zsp = zbc_zone(devip, lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) 		switch (rep_opts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) 		case 0x00:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) 			/* All zones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) 		case 0x01:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) 			/* Empty zones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) 			if (zsp->z_cond != ZC1_EMPTY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) 		case 0x02:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) 			/* Implicit open zones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) 			if (zsp->z_cond != ZC2_IMPLICIT_OPEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) 		case 0x03:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) 			/* Explicit open zones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) 			if (zsp->z_cond != ZC3_EXPLICIT_OPEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358) 		case 0x04:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) 			/* Closed zones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) 			if (zsp->z_cond != ZC4_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) 		case 0x05:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364) 			/* Full zones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365) 			if (zsp->z_cond != ZC5_FULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368) 		case 0x06:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369) 		case 0x07:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) 		case 0x10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372) 			 * Read-only, offline, reset WP recommended are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) 			 * not emulated: no zones to report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) 		case 0x11:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) 			/* non-seq-resource set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) 			if (!zsp->z_non_seq_resource)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381) 		case 0x3f:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) 			/* Not write pointer (conventional) zones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) 			if (!zbc_zone_is_conv(zsp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) 			mk_sense_buffer(scp, ILLEGAL_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) 					INVALID_FIELD_IN_CDB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) 			ret = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) 			goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) 		if (nrz < rep_max_zones) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) 			/* Fill zone descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) 			desc[0] = zsp->z_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) 			desc[1] = zsp->z_cond << 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) 			if (zsp->z_non_seq_resource)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) 				desc[1] |= 1 << 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) 			put_unaligned_be64((u64)zsp->z_size, desc + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) 			put_unaligned_be64((u64)zsp->z_start, desc + 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) 			put_unaligned_be64((u64)zsp->z_wp, desc + 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402) 			desc += 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) 		if (partial && nrz >= rep_max_zones)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) 		nrz++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) 	/* Report header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) 	put_unaligned_be32(nrz * RZONES_DESC_HD, arr + 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) 	put_unaligned_be64(sdebug_capacity - 1, arr + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) 	rep_len = (unsigned long)desc - (unsigned long)arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) 	ret = fill_from_dev_buffer(scp, arr, min_t(u32, alloc_len, rep_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) fini:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) 	read_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) 	kfree(arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) /* Logic transplanted from tcmu-runner, file_zbc.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) static void zbc_open_all(struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) 	struct sdeb_zone_state *zsp = &devip->zstate[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) 	for (i = 0; i < devip->nr_zones; i++, zsp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) 		if (zsp->z_cond == ZC4_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) 			zbc_open_zone(devip, &devip->zstate[i], true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) static int resp_open_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) 	int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) 	u64 z_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) 	enum sdebug_z_cond zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) 	struct sdeb_zone_state *zsp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443) 	bool all = cmd[14] & 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) 	struct sdeb_store_info *sip = devip2sip(devip, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) 	rwlock_t *macc_lckp = sip ? &sip->macc_lck : &sdeb_fake_rw_lck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) 	if (!sdebug_dev_is_zoned(devip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) 		mk_sense_invalid_opcode(scp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) 	write_lock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) 	if (all) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) 		/* Check if all closed zones can be open */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) 		if (devip->max_open &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) 		    devip->nr_exp_open + devip->nr_closed > devip->max_open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) 			mk_sense_buffer(scp, DATA_PROTECT, INSUFF_RES_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) 					INSUFF_ZONE_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) 			res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) 			goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) 		/* Open all closed zones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) 		zbc_open_all(devip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468) 	/* Open the specified zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) 	z_id = get_unaligned_be64(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) 	if (z_id >= sdebug_capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) 		res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476) 	zsp = zbc_zone(devip, z_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) 	if (z_id != zsp->z_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479) 		res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) 	if (zbc_zone_is_conv(zsp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) 		res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488) 	zc = zsp->z_cond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489) 	if (zc == ZC3_EXPLICIT_OPEN || zc == ZC5_FULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) 	if (devip->max_open && devip->nr_exp_open >= devip->max_open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493) 		mk_sense_buffer(scp, DATA_PROTECT, INSUFF_RES_ASC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) 				INSUFF_ZONE_ASCQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) 		res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499) 	zbc_open_zone(devip, zsp, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500) fini:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501) 	write_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505) static void zbc_close_all(struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509) 	for (i = 0; i < devip->nr_zones; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510) 		zbc_close_zone(devip, &devip->zstate[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513) static int resp_close_zone(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) 			   struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516) 	int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517) 	u64 z_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519) 	struct sdeb_zone_state *zsp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520) 	bool all = cmd[14] & 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521) 	struct sdeb_store_info *sip = devip2sip(devip, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522) 	rwlock_t *macc_lckp = sip ? &sip->macc_lck : &sdeb_fake_rw_lck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524) 	if (!sdebug_dev_is_zoned(devip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525) 		mk_sense_invalid_opcode(scp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529) 	write_lock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531) 	if (all) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532) 		zbc_close_all(devip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536) 	/* Close specified zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4537) 	z_id = get_unaligned_be64(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4538) 	if (z_id >= sdebug_capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4539) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) 		res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544) 	zsp = zbc_zone(devip, z_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545) 	if (z_id != zsp->z_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) 		res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550) 	if (zbc_zone_is_conv(zsp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552) 		res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) 	zbc_close_zone(devip, zsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557) fini:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558) 	write_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562) static void zbc_finish_zone(struct sdebug_dev_info *devip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563) 			    struct sdeb_zone_state *zsp, bool empty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565) 	enum sdebug_z_cond zc = zsp->z_cond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4567) 	if (zc == ZC4_CLOSED || zc == ZC2_IMPLICIT_OPEN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4568) 	    zc == ZC3_EXPLICIT_OPEN || (empty && zc == ZC1_EMPTY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4569) 		if (zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) 			zbc_close_zone(devip, zsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) 		if (zsp->z_cond == ZC4_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) 			devip->nr_closed--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573) 		zsp->z_wp = zsp->z_start + zsp->z_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574) 		zsp->z_cond = ZC5_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578) static void zbc_finish_all(struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582) 	for (i = 0; i < devip->nr_zones; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583) 		zbc_finish_zone(devip, &devip->zstate[i], false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586) static int resp_finish_zone(struct scsi_cmnd *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587) 			    struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) 	struct sdeb_zone_state *zsp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) 	int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) 	u64 z_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593) 	bool all = cmd[14] & 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594) 	struct sdeb_store_info *sip = devip2sip(devip, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595) 	rwlock_t *macc_lckp = sip ? &sip->macc_lck : &sdeb_fake_rw_lck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) 	if (!sdebug_dev_is_zoned(devip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598) 		mk_sense_invalid_opcode(scp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) 	write_lock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604) 	if (all) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) 		zbc_finish_all(devip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) 	/* Finish the specified zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) 	z_id = get_unaligned_be64(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611) 	if (z_id >= sdebug_capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613) 		res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617) 	zsp = zbc_zone(devip, z_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618) 	if (z_id != zsp->z_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620) 		res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623) 	if (zbc_zone_is_conv(zsp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625) 		res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629) 	zbc_finish_zone(devip, zsp, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630) fini:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631) 	write_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635) static void zbc_rwp_zone(struct sdebug_dev_info *devip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636) 			 struct sdeb_zone_state *zsp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) 	enum sdebug_z_cond zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639) 	struct sdeb_store_info *sip = devip2sip(devip, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) 	if (zbc_zone_is_conv(zsp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) 	zc = zsp->z_cond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) 	if (zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) 		zbc_close_zone(devip, zsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648) 	if (zsp->z_cond == ZC4_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649) 		devip->nr_closed--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651) 	if (zsp->z_wp > zsp->z_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652) 		memset(sip->storep + zsp->z_start * sdebug_sector_size, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653) 		       (zsp->z_wp - zsp->z_start) * sdebug_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655) 	zsp->z_non_seq_resource = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656) 	zsp->z_wp = zsp->z_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657) 	zsp->z_cond = ZC1_EMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660) static void zbc_rwp_all(struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664) 	for (i = 0; i < devip->nr_zones; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665) 		zbc_rwp_zone(devip, &devip->zstate[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668) static int resp_rwp_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670) 	struct sdeb_zone_state *zsp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671) 	int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672) 	u64 z_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) 	bool all = cmd[14] & 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) 	struct sdeb_store_info *sip = devip2sip(devip, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676) 	rwlock_t *macc_lckp = sip ? &sip->macc_lck : &sdeb_fake_rw_lck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678) 	if (!sdebug_dev_is_zoned(devip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) 		mk_sense_invalid_opcode(scp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680) 		return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683) 	write_lock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) 	if (all) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) 		zbc_rwp_all(devip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690) 	z_id = get_unaligned_be64(cmd + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691) 	if (z_id >= sdebug_capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693) 		res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697) 	zsp = zbc_zone(devip, z_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) 	if (z_id != zsp->z_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700) 		res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4701) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4702) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4703) 	if (zbc_zone_is_conv(zsp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4704) 		mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4705) 		res = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4706) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4707) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4709) 	zbc_rwp_zone(devip, zsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4710) fini:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4711) 	write_unlock(macc_lckp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4712) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4715) static struct sdebug_queue *get_queue(struct scsi_cmnd *cmnd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4717) 	u16 hwq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4718) 	u32 tag = blk_mq_unique_tag(cmnd->request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4720) 	hwq = blk_mq_unique_tag_to_hwq(tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4722) 	pr_debug("tag=%#x, hwq=%d\n", tag, hwq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4723) 	if (WARN_ON_ONCE(hwq >= submit_queues))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4724) 		hwq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4726) 	return sdebug_q_arr + hwq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4729) static u32 get_tag(struct scsi_cmnd *cmnd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4730) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4731) 	return blk_mq_unique_tag(cmnd->request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4734) /* Queued (deferred) command completions converge here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4735) static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4737) 	bool aborted = sd_dp->aborted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4738) 	int qc_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4739) 	int retiring = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4740) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4741) 	struct sdebug_queue *sqp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4742) 	struct sdebug_queued_cmd *sqcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4743) 	struct scsi_cmnd *scp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4744) 	struct sdebug_dev_info *devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4746) 	sd_dp->defer_t = SDEB_DEFER_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4747) 	if (unlikely(aborted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4748) 		sd_dp->aborted = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4749) 	qc_idx = sd_dp->qc_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4750) 	sqp = sdebug_q_arr + sd_dp->sqa_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4751) 	if (sdebug_statistics) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4752) 		atomic_inc(&sdebug_completions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4753) 		if (raw_smp_processor_id() != sd_dp->issuing_cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4754) 			atomic_inc(&sdebug_miss_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4755) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4756) 	if (unlikely((qc_idx < 0) || (qc_idx >= SDEBUG_CANQUEUE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4757) 		pr_err("wild qc_idx=%d\n", qc_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4758) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4759) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4760) 	spin_lock_irqsave(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4761) 	sqcp = &sqp->qc_arr[qc_idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4762) 	scp = sqcp->a_cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4763) 	if (unlikely(scp == NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4764) 		spin_unlock_irqrestore(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4765) 		pr_err("scp is NULL, sqa_idx=%d, qc_idx=%d, hc_idx=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4766) 		       sd_dp->sqa_idx, qc_idx, sd_dp->hc_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4767) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4768) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4769) 	devip = (struct sdebug_dev_info *)scp->device->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4770) 	if (likely(devip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4771) 		atomic_dec(&devip->num_in_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4772) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4773) 		pr_err("devip=NULL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4774) 	if (unlikely(atomic_read(&retired_max_queue) > 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4775) 		retiring = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4777) 	sqcp->a_cmnd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4778) 	if (unlikely(!test_and_clear_bit(qc_idx, sqp->in_use_bm))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4779) 		spin_unlock_irqrestore(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4780) 		pr_err("Unexpected completion\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4781) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4782) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4784) 	if (unlikely(retiring)) {	/* user has reduced max_queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4785) 		int k, retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4787) 		retval = atomic_read(&retired_max_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4788) 		if (qc_idx >= retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4789) 			spin_unlock_irqrestore(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4790) 			pr_err("index %d too large\n", retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4791) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4792) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4793) 		k = find_last_bit(sqp->in_use_bm, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4794) 		if ((k < sdebug_max_queue) || (k == retval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4795) 			atomic_set(&retired_max_queue, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4796) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4797) 			atomic_set(&retired_max_queue, k + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4798) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4799) 	spin_unlock_irqrestore(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4800) 	if (unlikely(aborted)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4801) 		if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4802) 			pr_info("bypassing scsi_done() due to aborted cmd\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4803) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4804) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4805) 	scp->scsi_done(scp); /* callback to mid level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4808) /* When high resolution timer goes off this function is called. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4809) static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4811) 	struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4812) 						  hrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4813) 	sdebug_q_cmd_complete(sd_dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4814) 	return HRTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4817) /* When work queue schedules work, it calls this function. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4818) static void sdebug_q_cmd_wq_complete(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4819) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4820) 	struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4821) 						  ew.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4822) 	sdebug_q_cmd_complete(sd_dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4825) static bool got_shared_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4826) static uuid_t shared_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4828) static int sdebug_device_create_zones(struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4830) 	struct sdeb_zone_state *zsp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4831) 	sector_t capacity = get_sdebug_capacity();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4832) 	sector_t zstart = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4833) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4835) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4836) 	 * Set the zone size: if sdeb_zbc_zone_size_mb is not set, figure out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4837) 	 * a zone size allowing for at least 4 zones on the device. Otherwise,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4838) 	 * use the specified zone size checking that at least 2 zones can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4839) 	 * created for the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4840) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4841) 	if (!sdeb_zbc_zone_size_mb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4842) 		devip->zsize = (DEF_ZBC_ZONE_SIZE_MB * SZ_1M)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4843) 			>> ilog2(sdebug_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4844) 		while (capacity < devip->zsize << 2 && devip->zsize >= 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4845) 			devip->zsize >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4846) 		if (devip->zsize < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4847) 			pr_err("Device capacity too small\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4848) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4849) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4850) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4851) 		if (!is_power_of_2(sdeb_zbc_zone_size_mb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4852) 			pr_err("Zone size is not a power of 2\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4853) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4854) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4855) 		devip->zsize = (sdeb_zbc_zone_size_mb * SZ_1M)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4856) 			>> ilog2(sdebug_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4857) 		if (devip->zsize >= capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4858) 			pr_err("Zone size too large for device capacity\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4859) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4860) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4861) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4863) 	devip->zsize_shift = ilog2(devip->zsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4864) 	devip->nr_zones = (capacity + devip->zsize - 1) >> devip->zsize_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4866) 	if (sdeb_zbc_nr_conv >= devip->nr_zones) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4867) 		pr_err("Number of conventional zones too large\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4868) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4869) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4870) 	devip->nr_conv_zones = sdeb_zbc_nr_conv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4872) 	if (devip->zmodel == BLK_ZONED_HM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4873) 		/* zbc_max_open_zones can be 0, meaning "not reported" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4874) 		if (sdeb_zbc_max_open >= devip->nr_zones - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4875) 			devip->max_open = (devip->nr_zones - 1) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4876) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4877) 			devip->max_open = sdeb_zbc_max_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4878) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4880) 	devip->zstate = kcalloc(devip->nr_zones,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4881) 				sizeof(struct sdeb_zone_state), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4882) 	if (!devip->zstate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4883) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4885) 	for (i = 0; i < devip->nr_zones; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4886) 		zsp = &devip->zstate[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4888) 		zsp->z_start = zstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4890) 		if (i < devip->nr_conv_zones) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4891) 			zsp->z_type = ZBC_ZONE_TYPE_CNV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4892) 			zsp->z_cond = ZBC_NOT_WRITE_POINTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4893) 			zsp->z_wp = (sector_t)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4894) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4895) 			if (devip->zmodel == BLK_ZONED_HM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4896) 				zsp->z_type = ZBC_ZONE_TYPE_SWR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4897) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4898) 				zsp->z_type = ZBC_ZONE_TYPE_SWP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4899) 			zsp->z_cond = ZC1_EMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4900) 			zsp->z_wp = zsp->z_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4901) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4903) 		if (zsp->z_start + devip->zsize < capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4904) 			zsp->z_size = devip->zsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4905) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4906) 			zsp->z_size = capacity - zsp->z_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4908) 		zstart += zsp->z_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4909) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4911) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4914) static struct sdebug_dev_info *sdebug_device_create(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4915) 			struct sdebug_host_info *sdbg_host, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4917) 	struct sdebug_dev_info *devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4919) 	devip = kzalloc(sizeof(*devip), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4920) 	if (devip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4921) 		if (sdebug_uuid_ctl == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4922) 			uuid_gen(&devip->lu_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4923) 		else if (sdebug_uuid_ctl == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4924) 			if (got_shared_uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4925) 				devip->lu_name = shared_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4926) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4927) 				uuid_gen(&shared_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4928) 				got_shared_uuid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4929) 				devip->lu_name = shared_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4930) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4931) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4932) 		devip->sdbg_host = sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4933) 		if (sdeb_zbc_in_use) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4934) 			devip->zmodel = sdeb_zbc_model;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4935) 			if (sdebug_device_create_zones(devip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4936) 				kfree(devip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4937) 				return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4938) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4939) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4940) 			devip->zmodel = BLK_ZONED_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4941) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4942) 		devip->sdbg_host = sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4943) 		devip->create_ts = ktime_get_boottime();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4944) 		atomic_set(&devip->stopped, (sdeb_tur_ms_to_ready > 0 ? 2 : 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4945) 		list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4946) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4947) 	return devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4950) static struct sdebug_dev_info *find_build_dev_info(struct scsi_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4951) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4952) 	struct sdebug_host_info *sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4953) 	struct sdebug_dev_info *open_devip = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4954) 	struct sdebug_dev_info *devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4956) 	sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4957) 	if (!sdbg_host) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4958) 		pr_err("Host info NULL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4959) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4960) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4962) 	list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4963) 		if ((devip->used) && (devip->channel == sdev->channel) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4964) 		    (devip->target == sdev->id) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4965) 		    (devip->lun == sdev->lun))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4966) 			return devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4967) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4968) 			if ((!devip->used) && (!open_devip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4969) 				open_devip = devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4970) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4971) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4972) 	if (!open_devip) { /* try and make a new one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4973) 		open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4974) 		if (!open_devip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4975) 			pr_err("out of memory at line %d\n", __LINE__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4976) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4977) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4978) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4980) 	open_devip->channel = sdev->channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4981) 	open_devip->target = sdev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4982) 	open_devip->lun = sdev->lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4983) 	open_devip->sdbg_host = sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4984) 	atomic_set(&open_devip->num_in_q, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4985) 	set_bit(SDEBUG_UA_POR, open_devip->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4986) 	open_devip->used = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4987) 	return open_devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4988) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4990) static int scsi_debug_slave_alloc(struct scsi_device *sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4992) 	if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4993) 		pr_info("slave_alloc <%u %u %u %llu>\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4994) 		       sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4995) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4998) static int scsi_debug_slave_configure(struct scsi_device *sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5000) 	struct sdebug_dev_info *devip =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5001) 			(struct sdebug_dev_info *)sdp->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5003) 	if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5004) 		pr_info("slave_configure <%u %u %u %llu>\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5005) 		       sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5006) 	if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5007) 		sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5008) 	if (devip == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5009) 		devip = find_build_dev_info(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5010) 		if (devip == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5011) 			return 1;  /* no resources, will be marked offline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5012) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5013) 	sdp->hostdata = devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5014) 	if (sdebug_no_uld)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5015) 		sdp->no_uld_attach = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5016) 	config_cdb_len(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5017) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5018) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5020) static void scsi_debug_slave_destroy(struct scsi_device *sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5021) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5022) 	struct sdebug_dev_info *devip =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5023) 		(struct sdebug_dev_info *)sdp->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5025) 	if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5026) 		pr_info("slave_destroy <%u %u %u %llu>\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5027) 		       sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5028) 	if (devip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5029) 		/* make this slot available for re-use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5030) 		devip->used = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5031) 		sdp->hostdata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5032) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5035) static void stop_qc_helper(struct sdebug_defer *sd_dp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5036) 			   enum sdeb_defer_type defer_t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5037) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5038) 	if (!sd_dp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5039) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5040) 	if (defer_t == SDEB_DEFER_HRT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5041) 		hrtimer_cancel(&sd_dp->hrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5042) 	else if (defer_t == SDEB_DEFER_WQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5043) 		cancel_work_sync(&sd_dp->ew.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5046) /* If @cmnd found deletes its timer or work queue and returns true; else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5047)    returns false */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5048) static bool stop_queued_cmnd(struct scsi_cmnd *cmnd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5049) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5050) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5051) 	int j, k, qmax, r_qmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5052) 	enum sdeb_defer_type l_defer_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5053) 	struct sdebug_queue *sqp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5054) 	struct sdebug_queued_cmd *sqcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5055) 	struct sdebug_dev_info *devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5056) 	struct sdebug_defer *sd_dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5058) 	for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5059) 		spin_lock_irqsave(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5060) 		qmax = sdebug_max_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5061) 		r_qmax = atomic_read(&retired_max_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5062) 		if (r_qmax > qmax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5063) 			qmax = r_qmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5064) 		for (k = 0; k < qmax; ++k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5065) 			if (test_bit(k, sqp->in_use_bm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5066) 				sqcp = &sqp->qc_arr[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5067) 				if (cmnd != sqcp->a_cmnd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5068) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5069) 				/* found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5070) 				devip = (struct sdebug_dev_info *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5071) 						cmnd->device->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5072) 				if (devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5073) 					atomic_dec(&devip->num_in_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5074) 				sqcp->a_cmnd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5075) 				sd_dp = sqcp->sd_dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5076) 				if (sd_dp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5077) 					l_defer_t = sd_dp->defer_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5078) 					sd_dp->defer_t = SDEB_DEFER_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5079) 				} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5080) 					l_defer_t = SDEB_DEFER_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5081) 				spin_unlock_irqrestore(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5082) 				stop_qc_helper(sd_dp, l_defer_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5083) 				clear_bit(k, sqp->in_use_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5084) 				return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5085) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5086) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5087) 		spin_unlock_irqrestore(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5088) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5089) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5092) /* Deletes (stops) timers or work queues of all queued commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5093) static void stop_all_queued(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5094) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5095) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5096) 	int j, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5097) 	enum sdeb_defer_type l_defer_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5098) 	struct sdebug_queue *sqp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5099) 	struct sdebug_queued_cmd *sqcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5100) 	struct sdebug_dev_info *devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5101) 	struct sdebug_defer *sd_dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5103) 	for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5104) 		spin_lock_irqsave(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5105) 		for (k = 0; k < SDEBUG_CANQUEUE; ++k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5106) 			if (test_bit(k, sqp->in_use_bm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5107) 				sqcp = &sqp->qc_arr[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5108) 				if (sqcp->a_cmnd == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5109) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5110) 				devip = (struct sdebug_dev_info *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5111) 					sqcp->a_cmnd->device->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5112) 				if (devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5113) 					atomic_dec(&devip->num_in_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5114) 				sqcp->a_cmnd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5115) 				sd_dp = sqcp->sd_dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5116) 				if (sd_dp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5117) 					l_defer_t = sd_dp->defer_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5118) 					sd_dp->defer_t = SDEB_DEFER_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5119) 				} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5120) 					l_defer_t = SDEB_DEFER_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5121) 				spin_unlock_irqrestore(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5122) 				stop_qc_helper(sd_dp, l_defer_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5123) 				clear_bit(k, sqp->in_use_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5124) 				spin_lock_irqsave(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5125) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5126) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5127) 		spin_unlock_irqrestore(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5131) /* Free queued command memory on heap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5132) static void free_all_queued(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5134) 	int j, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5135) 	struct sdebug_queue *sqp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5136) 	struct sdebug_queued_cmd *sqcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5138) 	for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5139) 		for (k = 0; k < SDEBUG_CANQUEUE; ++k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5140) 			sqcp = &sqp->qc_arr[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5141) 			kfree(sqcp->sd_dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5142) 			sqcp->sd_dp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5143) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5147) static int scsi_debug_abort(struct scsi_cmnd *SCpnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5149) 	bool ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5151) 	++num_aborts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5152) 	if (SCpnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5153) 		ok = stop_queued_cmnd(SCpnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5154) 		if (SCpnt->device && (SDEBUG_OPT_ALL_NOISE & sdebug_opts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5155) 			sdev_printk(KERN_INFO, SCpnt->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5156) 				    "%s: command%s found\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5157) 				    ok ? "" : " not");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5159) 	return SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5162) static int scsi_debug_device_reset(struct scsi_cmnd *SCpnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5164) 	++num_dev_resets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5165) 	if (SCpnt && SCpnt->device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5166) 		struct scsi_device *sdp = SCpnt->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5167) 		struct sdebug_dev_info *devip =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5168) 				(struct sdebug_dev_info *)sdp->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5170) 		if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5171) 			sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5172) 		if (devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5173) 			set_bit(SDEBUG_UA_POR, devip->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5175) 	return SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5178) static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5180) 	struct sdebug_host_info *sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5181) 	struct sdebug_dev_info *devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5182) 	struct scsi_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5183) 	struct Scsi_Host *hp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5184) 	int k = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5186) 	++num_target_resets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5187) 	if (!SCpnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5188) 		goto lie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5189) 	sdp = SCpnt->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5190) 	if (!sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5191) 		goto lie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5192) 	if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5193) 		sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5194) 	hp = sdp->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5195) 	if (!hp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5196) 		goto lie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5197) 	sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5198) 	if (sdbg_host) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5199) 		list_for_each_entry(devip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5200) 				    &sdbg_host->dev_info_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5201) 				    dev_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5202) 			if (devip->target == sdp->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5203) 				set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5204) 				++k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5205) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5207) 	if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5208) 		sdev_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5209) 			    "%s: %d device(s) found in target\n", __func__, k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5210) lie:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5211) 	return SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5214) static int scsi_debug_bus_reset(struct scsi_cmnd *SCpnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5216) 	struct sdebug_host_info *sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5217) 	struct sdebug_dev_info *devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5218) 	struct scsi_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5219) 	struct Scsi_Host *hp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5220) 	int k = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5222) 	++num_bus_resets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5223) 	if (!(SCpnt && SCpnt->device))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5224) 		goto lie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5225) 	sdp = SCpnt->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5226) 	if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5227) 		sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5228) 	hp = sdp->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5229) 	if (hp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5230) 		sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5231) 		if (sdbg_host) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5232) 			list_for_each_entry(devip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5233) 					    &sdbg_host->dev_info_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5234) 					    dev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5235) 				set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5236) 				++k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5237) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5238) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5240) 	if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5241) 		sdev_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5242) 			    "%s: %d device(s) found in host\n", __func__, k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5243) lie:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5244) 	return SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5247) static int scsi_debug_host_reset(struct scsi_cmnd *SCpnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5249) 	struct sdebug_host_info *sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5250) 	struct sdebug_dev_info *devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5251) 	int k = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5253) 	++num_host_resets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5254) 	if ((SCpnt->device) && (SDEBUG_OPT_ALL_NOISE & sdebug_opts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5255) 		sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5256) 	spin_lock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5257) 	list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5258) 		list_for_each_entry(devip, &sdbg_host->dev_info_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5259) 				    dev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5260) 			set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5261) 			++k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5262) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5264) 	spin_unlock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5265) 	stop_all_queued();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5266) 	if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5267) 		sdev_printk(KERN_INFO, SCpnt->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5268) 			    "%s: %d device(s) found\n", __func__, k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5269) 	return SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5272) static void sdebug_build_parts(unsigned char *ramp, unsigned long store_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5274) 	struct msdos_partition *pp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5275) 	int starts[SDEBUG_MAX_PARTS + 2], max_part_secs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5276) 	int sectors_per_part, num_sectors, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5277) 	int heads_by_sects, start_sec, end_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5279) 	/* assume partition table already zeroed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5280) 	if ((sdebug_num_parts < 1) || (store_size < 1048576))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5281) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5282) 	if (sdebug_num_parts > SDEBUG_MAX_PARTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5283) 		sdebug_num_parts = SDEBUG_MAX_PARTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5284) 		pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5285) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5286) 	num_sectors = (int)get_sdebug_capacity();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5287) 	sectors_per_part = (num_sectors - sdebug_sectors_per)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5288) 			   / sdebug_num_parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5289) 	heads_by_sects = sdebug_heads * sdebug_sectors_per;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5290) 	starts[0] = sdebug_sectors_per;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5291) 	max_part_secs = sectors_per_part;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5292) 	for (k = 1; k < sdebug_num_parts; ++k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5293) 		starts[k] = ((k * sectors_per_part) / heads_by_sects)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5294) 			    * heads_by_sects;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5295) 		if (starts[k] - starts[k - 1] < max_part_secs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5296) 			max_part_secs = starts[k] - starts[k - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5298) 	starts[sdebug_num_parts] = num_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5299) 	starts[sdebug_num_parts + 1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5301) 	ramp[510] = 0x55;	/* magic partition markings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5302) 	ramp[511] = 0xAA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5303) 	pp = (struct msdos_partition *)(ramp + 0x1be);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5304) 	for (k = 0; starts[k + 1]; ++k, ++pp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5305) 		start_sec = starts[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5306) 		end_sec = starts[k] + max_part_secs - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5307) 		pp->boot_ind = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5309) 		pp->cyl = start_sec / heads_by_sects;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5310) 		pp->head = (start_sec - (pp->cyl * heads_by_sects))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5311) 			   / sdebug_sectors_per;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5312) 		pp->sector = (start_sec % sdebug_sectors_per) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5314) 		pp->end_cyl = end_sec / heads_by_sects;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5315) 		pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5316) 			       / sdebug_sectors_per;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5317) 		pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5319) 		pp->start_sect = cpu_to_le32(start_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5320) 		pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5321) 		pp->sys_ind = 0x83;	/* plain Linux partition */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5325) static void block_unblock_all_queues(bool block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5327) 	int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5328) 	struct sdebug_queue *sqp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5330) 	for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5331) 		atomic_set(&sqp->blocked, (int)block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5334) /* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5335)  * commands will be processed normally before triggers occur.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5336)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5337) static void tweak_cmnd_count(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5339) 	int count, modulo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5341) 	modulo = abs(sdebug_every_nth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5342) 	if (modulo < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5343) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5344) 	block_unblock_all_queues(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5345) 	count = atomic_read(&sdebug_cmnd_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5346) 	atomic_set(&sdebug_cmnd_count, (count / modulo) * modulo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5347) 	block_unblock_all_queues(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5350) static void clear_queue_stats(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5352) 	atomic_set(&sdebug_cmnd_count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5353) 	atomic_set(&sdebug_completions, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5354) 	atomic_set(&sdebug_miss_cpus, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5355) 	atomic_set(&sdebug_a_tsf, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5358) static bool inject_on_this_cmd(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5360) 	if (sdebug_every_nth == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5361) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5362) 	return (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5365) #define INCLUSIVE_TIMING_MAX_NS 1000000		/* 1 millisecond */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5367) /* Complete the processing of the thread that queued a SCSI command to this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5368)  * driver. It either completes the command by calling cmnd_done() or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5369)  * schedules a hr timer or work queue then returns 0. Returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5370)  * SCSI_MLQUEUE_HOST_BUSY if temporarily out of resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5371)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5372) static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5373) 			 int scsi_result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5374) 			 int (*pfp)(struct scsi_cmnd *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5375) 				    struct sdebug_dev_info *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5376) 			 int delta_jiff, int ndelay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5378) 	bool new_sd_dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5379) 	bool inject = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5380) 	int k, num_in_q, qdepth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5381) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5382) 	u64 ns_from_boot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5383) 	struct sdebug_queue *sqp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5384) 	struct sdebug_queued_cmd *sqcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5385) 	struct scsi_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5386) 	struct sdebug_defer *sd_dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5388) 	if (unlikely(devip == NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5389) 		if (scsi_result == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5390) 			scsi_result = DID_NO_CONNECT << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5391) 		goto respond_in_thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5393) 	sdp = cmnd->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5395) 	if (delta_jiff == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5396) 		goto respond_in_thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5398) 	sqp = get_queue(cmnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5399) 	spin_lock_irqsave(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5400) 	if (unlikely(atomic_read(&sqp->blocked))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5401) 		spin_unlock_irqrestore(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5402) 		return SCSI_MLQUEUE_HOST_BUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5404) 	num_in_q = atomic_read(&devip->num_in_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5405) 	qdepth = cmnd->device->queue_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5406) 	if (unlikely((qdepth > 0) && (num_in_q >= qdepth))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5407) 		if (scsi_result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5408) 			spin_unlock_irqrestore(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5409) 			goto respond_in_thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5410) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5411) 			scsi_result = device_qfull_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5412) 	} else if (unlikely(sdebug_every_nth &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5413) 			    (SDEBUG_OPT_RARE_TSF & sdebug_opts) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5414) 			    (scsi_result == 0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5415) 		if ((num_in_q == (qdepth - 1)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5416) 		    (atomic_inc_return(&sdebug_a_tsf) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5417) 		     abs(sdebug_every_nth))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5418) 			atomic_set(&sdebug_a_tsf, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5419) 			inject = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5420) 			scsi_result = device_qfull_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5421) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5424) 	k = find_first_zero_bit(sqp->in_use_bm, sdebug_max_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5425) 	if (unlikely(k >= sdebug_max_queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5426) 		spin_unlock_irqrestore(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5427) 		if (scsi_result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5428) 			goto respond_in_thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5429) 		else if (SDEBUG_OPT_ALL_TSF & sdebug_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5430) 			scsi_result = device_qfull_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5431) 		if (SDEBUG_OPT_Q_NOISE & sdebug_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5432) 			sdev_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5433) 				    "%s: max_queue=%d exceeded, %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5434) 				    __func__, sdebug_max_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5435) 				    (scsi_result ?  "status: TASK SET FULL" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5436) 						    "report: host busy"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5437) 		if (scsi_result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5438) 			goto respond_in_thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5439) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5440) 			return SCSI_MLQUEUE_HOST_BUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5441) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5442) 	set_bit(k, sqp->in_use_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5443) 	atomic_inc(&devip->num_in_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5444) 	sqcp = &sqp->qc_arr[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5445) 	sqcp->a_cmnd = cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5446) 	cmnd->host_scribble = (unsigned char *)sqcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5447) 	sd_dp = sqcp->sd_dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5448) 	spin_unlock_irqrestore(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5449) 	if (!sd_dp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5450) 		sd_dp = kzalloc(sizeof(*sd_dp), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5451) 		if (!sd_dp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5452) 			atomic_dec(&devip->num_in_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5453) 			clear_bit(k, sqp->in_use_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5454) 			return SCSI_MLQUEUE_HOST_BUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5455) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5456) 		new_sd_dp = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5457) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5458) 		new_sd_dp = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5461) 	/* Set the hostwide tag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5462) 	if (sdebug_host_max_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5463) 		sd_dp->hc_idx = get_tag(cmnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5465) 	if (ndelay > 0 && ndelay < INCLUSIVE_TIMING_MAX_NS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5466) 		ns_from_boot = ktime_get_boottime_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5468) 	/* one of the resp_*() response functions is called here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5469) 	cmnd->result = pfp ? pfp(cmnd, devip) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5470) 	if (cmnd->result & SDEG_RES_IMMED_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5471) 		cmnd->result &= ~SDEG_RES_IMMED_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5472) 		delta_jiff = ndelay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5473) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5474) 	if (cmnd->result == 0 && scsi_result != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5475) 		cmnd->result = scsi_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5476) 	if (cmnd->result == 0 && unlikely(sdebug_opts & SDEBUG_OPT_TRANSPORT_ERR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5477) 		if (atomic_read(&sdeb_inject_pending)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5478) 			mk_sense_buffer(cmnd, ABORTED_COMMAND, TRANSPORT_PROBLEM, ACK_NAK_TO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5479) 			atomic_set(&sdeb_inject_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5480) 			cmnd->result = check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5481) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5482) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5484) 	if (unlikely(sdebug_verbose && cmnd->result))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5485) 		sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5486) 			    __func__, cmnd->result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5488) 	if (delta_jiff > 0 || ndelay > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5489) 		ktime_t kt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5491) 		if (delta_jiff > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5492) 			u64 ns = jiffies_to_nsecs(delta_jiff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5494) 			if (sdebug_random && ns < U32_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5495) 				ns = prandom_u32_max((u32)ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5496) 			} else if (sdebug_random) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5497) 				ns >>= 12;	/* scale to 4 usec precision */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5498) 				if (ns < U32_MAX)	/* over 4 hours max */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5499) 					ns = prandom_u32_max((u32)ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5500) 				ns <<= 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5501) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5502) 			kt = ns_to_ktime(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5503) 		} else {	/* ndelay has a 4.2 second max */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5504) 			kt = sdebug_random ? prandom_u32_max((u32)ndelay) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5505) 					     (u32)ndelay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5506) 			if (ndelay < INCLUSIVE_TIMING_MAX_NS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5507) 				u64 d = ktime_get_boottime_ns() - ns_from_boot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5509) 				if (kt <= d) {	/* elapsed duration >= kt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5510) 					spin_lock_irqsave(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5511) 					sqcp->a_cmnd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5512) 					atomic_dec(&devip->num_in_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5513) 					clear_bit(k, sqp->in_use_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5514) 					spin_unlock_irqrestore(&sqp->qc_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5515) 					if (new_sd_dp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5516) 						kfree(sd_dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5517) 					/* call scsi_done() from this thread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5518) 					cmnd->scsi_done(cmnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5519) 					return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5520) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5521) 				/* otherwise reduce kt by elapsed time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5522) 				kt -= d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5523) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5524) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5525) 		if (!sd_dp->init_hrt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5526) 			sd_dp->init_hrt = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5527) 			sqcp->sd_dp = sd_dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5528) 			hrtimer_init(&sd_dp->hrt, CLOCK_MONOTONIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5529) 				     HRTIMER_MODE_REL_PINNED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5530) 			sd_dp->hrt.function = sdebug_q_cmd_hrt_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5531) 			sd_dp->sqa_idx = sqp - sdebug_q_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5532) 			sd_dp->qc_idx = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5533) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5534) 		if (sdebug_statistics)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5535) 			sd_dp->issuing_cpu = raw_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5536) 		sd_dp->defer_t = SDEB_DEFER_HRT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5537) 		/* schedule the invocation of scsi_done() for a later time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5538) 		hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL_PINNED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5539) 	} else {	/* jdelay < 0, use work queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5540) 		if (!sd_dp->init_wq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5541) 			sd_dp->init_wq = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5542) 			sqcp->sd_dp = sd_dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5543) 			sd_dp->sqa_idx = sqp - sdebug_q_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5544) 			sd_dp->qc_idx = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5545) 			INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5546) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5547) 		if (sdebug_statistics)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5548) 			sd_dp->issuing_cpu = raw_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5549) 		sd_dp->defer_t = SDEB_DEFER_WQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5550) 		if (unlikely((sdebug_opts & SDEBUG_OPT_CMD_ABORT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5551) 			     atomic_read(&sdeb_inject_pending)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5552) 			sd_dp->aborted = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5553) 		schedule_work(&sd_dp->ew.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5554) 		if (unlikely((sdebug_opts & SDEBUG_OPT_CMD_ABORT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5555) 			     atomic_read(&sdeb_inject_pending))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5556) 			sdev_printk(KERN_INFO, sdp, "abort request tag %d\n", cmnd->request->tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5557) 			blk_abort_request(cmnd->request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5558) 			atomic_set(&sdeb_inject_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5559) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5561) 	if (unlikely((SDEBUG_OPT_Q_NOISE & sdebug_opts) && scsi_result == device_qfull_result))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5562) 		sdev_printk(KERN_INFO, sdp, "%s: num_in_q=%d +1, %s%s\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5563) 			    num_in_q, (inject ? "<inject> " : ""), "status: TASK SET FULL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5564) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5566) respond_in_thread:	/* call back to mid-layer using invocation thread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5567) 	cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5568) 	cmnd->result &= ~SDEG_RES_IMMED_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5569) 	if (cmnd->result == 0 && scsi_result != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5570) 		cmnd->result = scsi_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5571) 	cmnd->scsi_done(cmnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5572) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5575) /* Note: The following macros create attribute files in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5576)    /sys/module/scsi_debug/parameters directory. Unfortunately this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5577)    driver is unaware of a change and cannot trigger auxiliary actions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5578)    as it can when the corresponding attribute in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5579)    /sys/bus/pseudo/drivers/scsi_debug directory is changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5580)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5581) module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5582) module_param_named(ato, sdebug_ato, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5583) module_param_named(cdb_len, sdebug_cdb_len, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5584) module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5585) module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5586) module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5587) module_param_named(dif, sdebug_dif, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5588) module_param_named(dix, sdebug_dix, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5589) module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5590) module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5591) module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5592) module_param_named(guard, sdebug_guard, uint, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5593) module_param_named(host_lock, sdebug_host_lock, bool, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5594) module_param_named(host_max_queue, sdebug_host_max_queue, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5595) module_param_string(inq_product, sdebug_inq_product_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5596) 		    sizeof(sdebug_inq_product_id), S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5597) module_param_string(inq_rev, sdebug_inq_product_rev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5598) 		    sizeof(sdebug_inq_product_rev), S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5599) module_param_string(inq_vendor, sdebug_inq_vendor_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5600) 		    sizeof(sdebug_inq_vendor_id), S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5601) module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5602) module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5603) module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5604) module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5605) module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5606) module_param_named(lun_format, sdebug_lun_am_i, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5607) module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5608) module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5609) module_param_named(medium_error_count, sdebug_medium_error_count, int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5610) 		   S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5611) module_param_named(medium_error_start, sdebug_medium_error_start, int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5612) 		   S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5613) module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5614) module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5615) module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5616) module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5617) module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5618) module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5619) module_param_named(opt_xferlen_exp, sdebug_opt_xferlen_exp, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5620) module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5621) module_param_named(per_host_store, sdebug_per_host_store, bool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5622) 		   S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5623) module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5624) module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5625) module_param_named(random, sdebug_random, bool, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5626) module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5627) module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5628) module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5629) module_param_named(statistics, sdebug_statistics, bool, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5630) module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5631) module_param_named(submit_queues, submit_queues, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5632) module_param_named(tur_ms_to_ready, sdeb_tur_ms_to_ready, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5633) module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5634) module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5635) module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5636) module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5637) module_param_named(uuid_ctl, sdebug_uuid_ctl, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5638) module_param_named(virtual_gb, sdebug_virtual_gb, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5639) module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5640) 		   S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5641) module_param_named(wp, sdebug_wp, bool, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5642) module_param_named(write_same_length, sdebug_write_same_length, int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5643) 		   S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5644) module_param_named(zbc, sdeb_zbc_model_s, charp, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5645) module_param_named(zone_max_open, sdeb_zbc_max_open, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5646) module_param_named(zone_nr_conv, sdeb_zbc_nr_conv, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5647) module_param_named(zone_size_mb, sdeb_zbc_zone_size_mb, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5649) MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5650) MODULE_DESCRIPTION("SCSI debug adapter driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5651) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5652) MODULE_VERSION(SDEBUG_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5654) MODULE_PARM_DESC(add_host, "add n hosts, in sysfs if negative remove host(s) (def=1)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5655) MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5656) MODULE_PARM_DESC(cdb_len, "suggest CDB lengths to drivers (def=10)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5657) MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5658) MODULE_PARM_DESC(delay, "response delay (def=1 jiffy); 0:imm, -1,-2:tiny");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5659) MODULE_PARM_DESC(dev_size_mb, "size in MiB of ram shared by devs(def=8)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5660) MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5661) MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5662) MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5663) MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5664) MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5665) MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5666) MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5667) MODULE_PARM_DESC(host_max_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5668) 		 "host max # of queued cmds (0 to max(def) [max_queue fixed equal for !0])");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5669) MODULE_PARM_DESC(inq_product, "SCSI INQUIRY product string (def=\"scsi_debug\")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5670) MODULE_PARM_DESC(inq_rev, "SCSI INQUIRY revision string (def=\""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5671) 		 SDEBUG_VERSION "\")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5672) MODULE_PARM_DESC(inq_vendor, "SCSI INQUIRY vendor string (def=\"Linux\")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5673) MODULE_PARM_DESC(lbprz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5674) 		 "on read unmapped LBs return 0 when 1 (def), return 0xff when 2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5675) MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5676) MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5677) MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5678) MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5679) MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5680) MODULE_PARM_DESC(lun_format, "LUN format: 0->peripheral (def); 1 --> flat address method");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5681) MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5682) MODULE_PARM_DESC(medium_error_count, "count of sectors to return follow on MEDIUM error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5683) MODULE_PARM_DESC(medium_error_start, "starting sector number to return MEDIUM error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5684) MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5685) MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5686) MODULE_PARM_DESC(no_uld, "stop ULD (e.g. sd driver) attaching (def=0))");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5687) MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5688) MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5689) MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5690) MODULE_PARM_DESC(opt_xferlen_exp, "optimal transfer length granularity exponent (def=physblk_exp)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5691) MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5692) MODULE_PARM_DESC(per_host_store, "If set, next positive add_host will get new store (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5693) MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5694) MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5695) MODULE_PARM_DESC(random, "If set, uniformly randomize command duration between 0 and delay_in_ns");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5696) MODULE_PARM_DESC(removable, "claim to have removable media (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5697) MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=7[SPC-5])");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5698) MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5699) MODULE_PARM_DESC(statistics, "collect statistics on commands, queues (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5700) MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5701) MODULE_PARM_DESC(submit_queues, "support for block multi-queue (def=1)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5702) MODULE_PARM_DESC(tur_ms_to_ready, "TEST UNIT READY millisecs before initial good status (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5703) MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5704) MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5705) MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5706) MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5707) MODULE_PARM_DESC(uuid_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5708) 		 "1->use uuid for lu name, 0->don't, 2->all use same (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5709) MODULE_PARM_DESC(virtual_gb, "virtual gigabyte (GiB) size (def=0 -> use dev_size_mb)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5710) MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5711) MODULE_PARM_DESC(wp, "Write Protect (def=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5712) MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5713) MODULE_PARM_DESC(zbc, "'none' [0]; 'aware' [1]; 'managed' [2] (def=0). Can have 'host-' prefix");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5714) MODULE_PARM_DESC(zone_max_open, "Maximum number of open zones; [0] for no limit (def=auto)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5715) MODULE_PARM_DESC(zone_nr_conv, "Number of conventional zones (def=1)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5716) MODULE_PARM_DESC(zone_size_mb, "Zone size in MiB (def=auto)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5718) #define SDEBUG_INFO_LEN 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5719) static char sdebug_info[SDEBUG_INFO_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5721) static const char *scsi_debug_info(struct Scsi_Host *shp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5723) 	int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5725) 	k = scnprintf(sdebug_info, SDEBUG_INFO_LEN, "%s: version %s [%s]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5726) 		      my_name, SDEBUG_VERSION, sdebug_version_date);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5727) 	if (k >= (SDEBUG_INFO_LEN - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5728) 		return sdebug_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5729) 	scnprintf(sdebug_info + k, SDEBUG_INFO_LEN - k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5730) 		  "  dev_size_mb=%d, opts=0x%x, submit_queues=%d, %s=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5731) 		  sdebug_dev_size_mb, sdebug_opts, submit_queues,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5732) 		  "statistics", (int)sdebug_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5733) 	return sdebug_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5736) /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5737) static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5738) 				 int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5739) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5740) 	char arr[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5741) 	int opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5742) 	int minLen = length > 15 ? 15 : length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5744) 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5745) 		return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5746) 	memcpy(arr, buffer, minLen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5747) 	arr[minLen] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5748) 	if (1 != sscanf(arr, "%d", &opts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5749) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5750) 	sdebug_opts = opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5751) 	sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5752) 	sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5753) 	if (sdebug_every_nth != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5754) 		tweak_cmnd_count();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5755) 	return length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5758) /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5759)  * same for each scsi_debug host (if more than one). Some of the counters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5760)  * output are not atomics so might be inaccurate in a busy system. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5761) static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5762) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5763) 	int f, j, l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5764) 	struct sdebug_queue *sqp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5765) 	struct sdebug_host_info *sdhp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5767) 	seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5768) 		   SDEBUG_VERSION, sdebug_version_date);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5769) 	seq_printf(m, "num_tgts=%d, %ssize=%d MB, opts=0x%x, every_nth=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5770) 		   sdebug_num_tgts, "shared (ram) ", sdebug_dev_size_mb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5771) 		   sdebug_opts, sdebug_every_nth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5772) 	seq_printf(m, "delay=%d, ndelay=%d, max_luns=%d, sector_size=%d %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5773) 		   sdebug_jdelay, sdebug_ndelay, sdebug_max_luns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5774) 		   sdebug_sector_size, "bytes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5775) 	seq_printf(m, "cylinders=%d, heads=%d, sectors=%d, command aborts=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5776) 		   sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5777) 		   num_aborts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5778) 	seq_printf(m, "RESETs: device=%d, target=%d, bus=%d, host=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5779) 		   num_dev_resets, num_target_resets, num_bus_resets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5780) 		   num_host_resets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5781) 	seq_printf(m, "dix_reads=%d, dix_writes=%d, dif_errors=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5782) 		   dix_reads, dix_writes, dif_errors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5783) 	seq_printf(m, "usec_in_jiffy=%lu, statistics=%d\n", TICK_NSEC / 1000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5784) 		   sdebug_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5785) 	seq_printf(m, "cmnd_count=%d, completions=%d, %s=%d, a_tsf=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5786) 		   atomic_read(&sdebug_cmnd_count),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5787) 		   atomic_read(&sdebug_completions),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5788) 		   "miss_cpus", atomic_read(&sdebug_miss_cpus),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5789) 		   atomic_read(&sdebug_a_tsf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5791) 	seq_printf(m, "submit_queues=%d\n", submit_queues);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5792) 	for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5793) 		seq_printf(m, "  queue %d:\n", j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5794) 		f = find_first_bit(sqp->in_use_bm, sdebug_max_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5795) 		if (f != sdebug_max_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5796) 			l = find_last_bit(sqp->in_use_bm, sdebug_max_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5797) 			seq_printf(m, "    in_use_bm BUSY: %s: %d,%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5798) 				   "first,last bits", f, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5799) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5800) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5802) 	seq_printf(m, "this host_no=%d\n", host->host_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5803) 	if (!xa_empty(per_store_ap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5804) 		bool niu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5805) 		int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5806) 		unsigned long l_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5807) 		struct sdeb_store_info *sip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5809) 		seq_puts(m, "\nhost list:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5810) 		j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5811) 		list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5812) 			idx = sdhp->si_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5813) 			seq_printf(m, "  %d: host_no=%d, si_idx=%d\n", j,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5814) 				   sdhp->shost->host_no, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5815) 			++j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5816) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5817) 		seq_printf(m, "\nper_store array [most_recent_idx=%d]:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5818) 			   sdeb_most_recent_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5819) 		j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5820) 		xa_for_each(per_store_ap, l_idx, sip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5821) 			niu = xa_get_mark(per_store_ap, l_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5822) 					  SDEB_XA_NOT_IN_USE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5823) 			idx = (int)l_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5824) 			seq_printf(m, "  %d: idx=%d%s\n", j, idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5825) 				   (niu ? "  not_in_use" : ""));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5826) 			++j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5827) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5828) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5829) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5832) static ssize_t delay_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5833) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5834) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5836) /* Returns -EBUSY if jdelay is being changed and commands are queued. The unit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5837)  * of delay is jiffies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5838)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5839) static ssize_t delay_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5840) 			   size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5842) 	int jdelay, res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5844) 	if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5845) 		res = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5846) 		if (sdebug_jdelay != jdelay) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5847) 			int j, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5848) 			struct sdebug_queue *sqp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5850) 			block_unblock_all_queues(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5851) 			for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5852) 			     ++j, ++sqp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5853) 				k = find_first_bit(sqp->in_use_bm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5854) 						   sdebug_max_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5855) 				if (k != sdebug_max_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5856) 					res = -EBUSY;   /* queued commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5857) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5858) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5859) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5860) 			if (res > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5861) 				sdebug_jdelay = jdelay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5862) 				sdebug_ndelay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5863) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5864) 			block_unblock_all_queues(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5865) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5866) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5867) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5868) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5870) static DRIVER_ATTR_RW(delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5872) static ssize_t ndelay_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5874) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5876) /* Returns -EBUSY if ndelay is being changed and commands are queued */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5877) /* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5878) static ssize_t ndelay_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5879) 			    size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5880) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5881) 	int ndelay, res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5883) 	if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5884) 	    (ndelay >= 0) && (ndelay < (1000 * 1000 * 1000))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5885) 		res = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5886) 		if (sdebug_ndelay != ndelay) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5887) 			int j, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5888) 			struct sdebug_queue *sqp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5890) 			block_unblock_all_queues(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5891) 			for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5892) 			     ++j, ++sqp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5893) 				k = find_first_bit(sqp->in_use_bm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5894) 						   sdebug_max_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5895) 				if (k != sdebug_max_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5896) 					res = -EBUSY;   /* queued commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5897) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5898) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5899) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5900) 			if (res > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5901) 				sdebug_ndelay = ndelay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5902) 				sdebug_jdelay = ndelay  ? JDELAY_OVERRIDDEN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5903) 							: DEF_JDELAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5904) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5905) 			block_unblock_all_queues(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5906) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5907) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5908) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5909) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5910) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5911) static DRIVER_ATTR_RW(ndelay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5913) static ssize_t opts_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5915) 	return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5918) static ssize_t opts_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5919) 			  size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5921) 	int opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5922) 	char work[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5924) 	if (sscanf(buf, "%10s", work) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5925) 		if (strncasecmp(work, "0x", 2) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5926) 			if (kstrtoint(work + 2, 16, &opts) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5927) 				goto opts_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5928) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5929) 			if (kstrtoint(work, 10, &opts) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5930) 				goto opts_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5931) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5932) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5933) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5934) opts_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5935) 	sdebug_opts = opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5936) 	sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5937) 	sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5938) 	tweak_cmnd_count();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5939) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5941) static DRIVER_ATTR_RW(opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5943) static ssize_t ptype_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5944) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5945) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5947) static ssize_t ptype_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5948) 			   size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5949) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5950) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5952) 	/* Cannot change from or to TYPE_ZBC with sysfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5953) 	if (sdebug_ptype == TYPE_ZBC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5954) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5956) 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5957) 		if (n == TYPE_ZBC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5958) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5959) 		sdebug_ptype = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5960) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5961) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5962) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5963) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5964) static DRIVER_ATTR_RW(ptype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5966) static ssize_t dsense_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5968) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5970) static ssize_t dsense_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5971) 			    size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5972) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5973) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5975) 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5976) 		sdebug_dsense = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5977) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5978) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5979) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5981) static DRIVER_ATTR_RW(dsense);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5983) static ssize_t fake_rw_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5984) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5985) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5986) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5987) static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5988) 			     size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5989) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5990) 	int n, idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5992) 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5993) 		bool want_store = (n == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5994) 		struct sdebug_host_info *sdhp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5996) 		n = (n > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5997) 		sdebug_fake_rw = (sdebug_fake_rw > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5998) 		if (sdebug_fake_rw == n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5999) 			return count;	/* not transitioning so do nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6001) 		if (want_store) {	/* 1 --> 0 transition, set up store */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6002) 			if (sdeb_first_idx < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6003) 				idx = sdebug_add_store();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6004) 				if (idx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6005) 					return idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6006) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6007) 				idx = sdeb_first_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6008) 				xa_clear_mark(per_store_ap, idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6009) 					      SDEB_XA_NOT_IN_USE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6010) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6011) 			/* make all hosts use same store */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6012) 			list_for_each_entry(sdhp, &sdebug_host_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6013) 					    host_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6014) 				if (sdhp->si_idx != idx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6015) 					xa_set_mark(per_store_ap, sdhp->si_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6016) 						    SDEB_XA_NOT_IN_USE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6017) 					sdhp->si_idx = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6018) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6019) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6020) 			sdeb_most_recent_idx = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6021) 		} else {	/* 0 --> 1 transition is trigger for shrink */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6022) 			sdebug_erase_all_stores(true /* apart from first */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6023) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6024) 		sdebug_fake_rw = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6025) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6026) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6027) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6029) static DRIVER_ATTR_RW(fake_rw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6031) static ssize_t no_lun_0_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6033) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6035) static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6036) 			      size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6037) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6038) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6040) 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6041) 		sdebug_no_lun_0 = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6042) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6043) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6044) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6046) static DRIVER_ATTR_RW(no_lun_0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6048) static ssize_t num_tgts_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6049) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6050) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6051) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6052) static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6053) 			      size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6054) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6055) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6057) 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6058) 		sdebug_num_tgts = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6059) 		sdebug_max_tgts_luns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6060) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6061) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6062) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6063) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6064) static DRIVER_ATTR_RW(num_tgts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6066) static ssize_t dev_size_mb_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6067) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6068) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6070) static DRIVER_ATTR_RO(dev_size_mb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6072) static ssize_t per_host_store_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6073) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6074) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_per_host_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6077) static ssize_t per_host_store_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6078) 				    size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6079) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6080) 	bool v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6082) 	if (kstrtobool(buf, &v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6083) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6085) 	sdebug_per_host_store = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6086) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6088) static DRIVER_ATTR_RW(per_host_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6090) static ssize_t num_parts_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6091) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6092) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6094) static DRIVER_ATTR_RO(num_parts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6096) static ssize_t every_nth_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6097) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6098) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6099) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6100) static ssize_t every_nth_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6101) 			       size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6103) 	int nth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6104) 	char work[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6106) 	if (sscanf(buf, "%10s", work) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6107) 		if (strncasecmp(work, "0x", 2) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6108) 			if (kstrtoint(work + 2, 16, &nth) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6109) 				goto every_nth_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6110) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6111) 			if (kstrtoint(work, 10, &nth) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6112) 				goto every_nth_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6113) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6115) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6117) every_nth_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6118) 	sdebug_every_nth = nth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6119) 	if (nth && !sdebug_statistics) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6120) 		pr_info("every_nth needs statistics=1, set it\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6121) 		sdebug_statistics = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6123) 	tweak_cmnd_count();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6124) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6126) static DRIVER_ATTR_RW(every_nth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6128) static ssize_t lun_format_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6130) 	return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_lun_am);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6132) static ssize_t lun_format_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6133) 				size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6135) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6136) 	bool changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6138) 	if (kstrtoint(buf, 0, &n))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6139) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6140) 	if (n >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6141) 		if (n > (int)SAM_LUN_AM_FLAT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6142) 			pr_warn("only LUN address methods 0 and 1 are supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6143) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6144) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6145) 		changed = ((int)sdebug_lun_am != n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6146) 		sdebug_lun_am = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6147) 		if (changed && sdebug_scsi_level >= 5) {	/* >= SPC-3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6148) 			struct sdebug_host_info *sdhp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6149) 			struct sdebug_dev_info *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6151) 			spin_lock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6152) 			list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6153) 				list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6154) 					set_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6155) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6156) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6157) 			spin_unlock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6158) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6159) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6161) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6163) static DRIVER_ATTR_RW(lun_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6165) static ssize_t max_luns_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6167) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6169) static ssize_t max_luns_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6170) 			      size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6172) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6173) 	bool changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6175) 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6176) 		if (n > 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6177) 			pr_warn("max_luns can be no more than 256\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6178) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6179) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6180) 		changed = (sdebug_max_luns != n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6181) 		sdebug_max_luns = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6182) 		sdebug_max_tgts_luns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6183) 		if (changed && (sdebug_scsi_level >= 5)) {	/* >= SPC-3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6184) 			struct sdebug_host_info *sdhp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6185) 			struct sdebug_dev_info *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6187) 			spin_lock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6188) 			list_for_each_entry(sdhp, &sdebug_host_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6189) 					    host_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6190) 				list_for_each_entry(dp, &sdhp->dev_info_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6191) 						    dev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6192) 					set_bit(SDEBUG_UA_LUNS_CHANGED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6193) 						dp->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6194) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6195) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6196) 			spin_unlock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6197) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6198) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6200) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6202) static DRIVER_ATTR_RW(max_luns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6204) static ssize_t max_queue_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6206) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6208) /* N.B. max_queue can be changed while there are queued commands. In flight
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6209)  * commands beyond the new max_queue will be completed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6210) static ssize_t max_queue_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6211) 			       size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6213) 	int j, n, k, a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6214) 	struct sdebug_queue *sqp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6216) 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6217) 	    (n <= SDEBUG_CANQUEUE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6218) 	    (sdebug_host_max_queue == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6219) 		block_unblock_all_queues(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6220) 		k = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6221) 		for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6222) 		     ++j, ++sqp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6223) 			a = find_last_bit(sqp->in_use_bm, SDEBUG_CANQUEUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6224) 			if (a > k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6225) 				k = a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6226) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6227) 		sdebug_max_queue = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6228) 		if (k == SDEBUG_CANQUEUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6229) 			atomic_set(&retired_max_queue, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6230) 		else if (k >= n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6231) 			atomic_set(&retired_max_queue, k + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6232) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6233) 			atomic_set(&retired_max_queue, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6234) 		block_unblock_all_queues(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6235) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6237) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6239) static DRIVER_ATTR_RW(max_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6241) static ssize_t host_max_queue_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6243) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_host_max_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6246) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6247)  * Since this is used for .can_queue, and we get the hc_idx tag from the bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6248)  * in range [0, sdebug_host_max_queue), we can't change it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6249)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6250) static DRIVER_ATTR_RO(host_max_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6252) static ssize_t no_uld_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6254) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6256) static DRIVER_ATTR_RO(no_uld);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6258) static ssize_t scsi_level_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6260) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6262) static DRIVER_ATTR_RO(scsi_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6264) static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6266) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6268) static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6269) 				size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6271) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6272) 	bool changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6274) 	/* Ignore capacity change for ZBC drives for now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6275) 	if (sdeb_zbc_in_use)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6276) 		return -ENOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6278) 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6279) 		changed = (sdebug_virtual_gb != n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6280) 		sdebug_virtual_gb = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6281) 		sdebug_capacity = get_sdebug_capacity();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6282) 		if (changed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6283) 			struct sdebug_host_info *sdhp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6284) 			struct sdebug_dev_info *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6286) 			spin_lock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6287) 			list_for_each_entry(sdhp, &sdebug_host_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6288) 					    host_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6289) 				list_for_each_entry(dp, &sdhp->dev_info_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6290) 						    dev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6291) 					set_bit(SDEBUG_UA_CAPACITY_CHANGED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6292) 						dp->uas_bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6293) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6294) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6295) 			spin_unlock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6296) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6297) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6299) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6301) static DRIVER_ATTR_RW(virtual_gb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6303) static ssize_t add_host_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6305) 	/* absolute number of hosts currently active is what is shown */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6306) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_hosts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6309) static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6310) 			      size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6312) 	bool found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6313) 	unsigned long idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6314) 	struct sdeb_store_info *sip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6315) 	bool want_phs = (sdebug_fake_rw == 0) && sdebug_per_host_store;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6316) 	int delta_hosts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6318) 	if (sscanf(buf, "%d", &delta_hosts) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6319) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6320) 	if (delta_hosts > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6321) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6322) 			found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6323) 			if (want_phs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6324) 				xa_for_each_marked(per_store_ap, idx, sip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6325) 						   SDEB_XA_NOT_IN_USE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6326) 					sdeb_most_recent_idx = (int)idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6327) 					found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6328) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6329) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6330) 				if (found)	/* re-use case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6331) 					sdebug_add_host_helper((int)idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6332) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6333) 					sdebug_do_add_host(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6334) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6335) 				sdebug_do_add_host(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6336) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6337) 		} while (--delta_hosts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6338) 	} else if (delta_hosts < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6339) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6340) 			sdebug_do_remove_host(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6341) 		} while (++delta_hosts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6342) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6343) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6345) static DRIVER_ATTR_RW(add_host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6347) static ssize_t vpd_use_hostno_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6349) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6351) static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6352) 				    size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6354) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6356) 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6357) 		sdebug_vpd_use_hostno = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6358) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6360) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6362) static DRIVER_ATTR_RW(vpd_use_hostno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6364) static ssize_t statistics_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6366) 	return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6368) static ssize_t statistics_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6369) 				size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6371) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6373) 	if ((count > 0) && (sscanf(buf, "%d", &n) == 1) && (n >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6374) 		if (n > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6375) 			sdebug_statistics = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6376) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6377) 			clear_queue_stats();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6378) 			sdebug_statistics = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6379) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6380) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6381) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6382) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6384) static DRIVER_ATTR_RW(statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6386) static ssize_t sector_size_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6388) 	return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6390) static DRIVER_ATTR_RO(sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6392) static ssize_t submit_queues_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6394) 	return scnprintf(buf, PAGE_SIZE, "%d\n", submit_queues);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6396) static DRIVER_ATTR_RO(submit_queues);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6398) static ssize_t dix_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6400) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6402) static DRIVER_ATTR_RO(dix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6404) static ssize_t dif_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6406) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6408) static DRIVER_ATTR_RO(dif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6410) static ssize_t guard_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6412) 	return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6414) static DRIVER_ATTR_RO(guard);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6416) static ssize_t ato_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6418) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6420) static DRIVER_ATTR_RO(ato);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6422) static ssize_t map_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6424) 	ssize_t count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6426) 	if (!scsi_debug_lbp())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6427) 		return scnprintf(buf, PAGE_SIZE, "0-%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6428) 				 sdebug_store_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6430) 	if (sdebug_fake_rw == 0 && !xa_empty(per_store_ap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6431) 		struct sdeb_store_info *sip = xa_load(per_store_ap, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6433) 		if (sip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6434) 			count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6435) 					  (int)map_size, sip->map_storep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6436) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6437) 	buf[count++] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6438) 	buf[count] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6440) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6442) static DRIVER_ATTR_RO(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6444) static ssize_t random_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6446) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_random);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6449) static ssize_t random_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6450) 			    size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6452) 	bool v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6454) 	if (kstrtobool(buf, &v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6455) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6457) 	sdebug_random = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6458) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6460) static DRIVER_ATTR_RW(random);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6462) static ssize_t removable_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6464) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6466) static ssize_t removable_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6467) 			       size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6469) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6471) 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6472) 		sdebug_removable = (n > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6473) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6475) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6477) static DRIVER_ATTR_RW(removable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6479) static ssize_t host_lock_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6481) 	return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6483) /* N.B. sdebug_host_lock does nothing, kept for backward compatibility */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6484) static ssize_t host_lock_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6485) 			       size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6487) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6489) 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6490) 		sdebug_host_lock = (n > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6491) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6492) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6493) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6495) static DRIVER_ATTR_RW(host_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6497) static ssize_t strict_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6499) 	return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6501) static ssize_t strict_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6502) 			    size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6504) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6506) 	if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6507) 		sdebug_strict = (n > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6508) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6510) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6512) static DRIVER_ATTR_RW(strict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6514) static ssize_t uuid_ctl_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6516) 	return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_uuid_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6518) static DRIVER_ATTR_RO(uuid_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6520) static ssize_t cdb_len_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6522) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_cdb_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6524) static ssize_t cdb_len_store(struct device_driver *ddp, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6525) 			     size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6527) 	int ret, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6529) 	ret = kstrtoint(buf, 0, &n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6530) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6531) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6532) 	sdebug_cdb_len = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6533) 	all_config_cdb_len();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6534) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6536) static DRIVER_ATTR_RW(cdb_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6538) static const char * const zbc_model_strs_a[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6539) 	[BLK_ZONED_NONE] = "none",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6540) 	[BLK_ZONED_HA]   = "host-aware",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6541) 	[BLK_ZONED_HM]   = "host-managed",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6542) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6544) static const char * const zbc_model_strs_b[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6545) 	[BLK_ZONED_NONE] = "no",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6546) 	[BLK_ZONED_HA]   = "aware",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6547) 	[BLK_ZONED_HM]   = "managed",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6548) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6550) static const char * const zbc_model_strs_c[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6551) 	[BLK_ZONED_NONE] = "0",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6552) 	[BLK_ZONED_HA]   = "1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6553) 	[BLK_ZONED_HM]   = "2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6554) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6556) static int sdeb_zbc_model_str(const char *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6558) 	int res = sysfs_match_string(zbc_model_strs_a, cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6560) 	if (res < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6561) 		res = sysfs_match_string(zbc_model_strs_b, cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6562) 		if (res < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6563) 			res = sysfs_match_string(zbc_model_strs_c, cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6564) 			if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6565) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6566) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6567) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6568) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6571) static ssize_t zbc_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6573) 	return scnprintf(buf, PAGE_SIZE, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6574) 			 zbc_model_strs_a[sdeb_zbc_model]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6576) static DRIVER_ATTR_RO(zbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6578) static ssize_t tur_ms_to_ready_show(struct device_driver *ddp, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6580) 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdeb_tur_ms_to_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6582) static DRIVER_ATTR_RO(tur_ms_to_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6584) /* Note: The following array creates attribute files in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6585)    /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6586)    files (over those found in the /sys/module/scsi_debug/parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6587)    directory) is that auxiliary actions can be triggered when an attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6588)    is changed. For example see: add_host_store() above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6589)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6591) static struct attribute *sdebug_drv_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6592) 	&driver_attr_delay.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6593) 	&driver_attr_opts.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6594) 	&driver_attr_ptype.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6595) 	&driver_attr_dsense.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6596) 	&driver_attr_fake_rw.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6597) 	&driver_attr_host_max_queue.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6598) 	&driver_attr_no_lun_0.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6599) 	&driver_attr_num_tgts.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6600) 	&driver_attr_dev_size_mb.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6601) 	&driver_attr_num_parts.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6602) 	&driver_attr_every_nth.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6603) 	&driver_attr_lun_format.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6604) 	&driver_attr_max_luns.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6605) 	&driver_attr_max_queue.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6606) 	&driver_attr_no_uld.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6607) 	&driver_attr_scsi_level.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6608) 	&driver_attr_virtual_gb.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6609) 	&driver_attr_add_host.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6610) 	&driver_attr_per_host_store.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6611) 	&driver_attr_vpd_use_hostno.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6612) 	&driver_attr_sector_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6613) 	&driver_attr_statistics.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6614) 	&driver_attr_submit_queues.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6615) 	&driver_attr_dix.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6616) 	&driver_attr_dif.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6617) 	&driver_attr_guard.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6618) 	&driver_attr_ato.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6619) 	&driver_attr_map.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6620) 	&driver_attr_random.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6621) 	&driver_attr_removable.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6622) 	&driver_attr_host_lock.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6623) 	&driver_attr_ndelay.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6624) 	&driver_attr_strict.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6625) 	&driver_attr_uuid_ctl.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6626) 	&driver_attr_cdb_len.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6627) 	&driver_attr_tur_ms_to_ready.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6628) 	&driver_attr_zbc.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6629) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6630) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6631) ATTRIBUTE_GROUPS(sdebug_drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6633) static struct device *pseudo_primary;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6635) static int __init scsi_debug_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6637) 	bool want_store = (sdebug_fake_rw == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6638) 	unsigned long sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6639) 	int k, ret, hosts_to_add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6640) 	int idx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6642) 	ramdisk_lck_a[0] = &atomic_rw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6643) 	ramdisk_lck_a[1] = &atomic_rw2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6644) 	atomic_set(&retired_max_queue, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6646) 	if (sdebug_ndelay >= 1000 * 1000 * 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6647) 		pr_warn("ndelay must be less than 1 second, ignored\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6648) 		sdebug_ndelay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6649) 	} else if (sdebug_ndelay > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6650) 		sdebug_jdelay = JDELAY_OVERRIDDEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6652) 	switch (sdebug_sector_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6653) 	case  512:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6654) 	case 1024:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6655) 	case 2048:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6656) 	case 4096:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6657) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6658) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6659) 		pr_err("invalid sector_size %d\n", sdebug_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6660) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6661) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6663) 	switch (sdebug_dif) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6664) 	case T10_PI_TYPE0_PROTECTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6665) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6666) 	case T10_PI_TYPE1_PROTECTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6667) 	case T10_PI_TYPE2_PROTECTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6668) 	case T10_PI_TYPE3_PROTECTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6669) 		have_dif_prot = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6670) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6672) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6673) 		pr_err("dif must be 0, 1, 2 or 3\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6674) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6677) 	if (sdebug_num_tgts < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6678) 		pr_err("num_tgts must be >= 0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6679) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6680) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6682) 	if (sdebug_guard > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6683) 		pr_err("guard must be 0 or 1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6684) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6685) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6687) 	if (sdebug_ato > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6688) 		pr_err("ato must be 0 or 1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6689) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6690) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6692) 	if (sdebug_physblk_exp > 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6693) 		pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6694) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6697) 	sdebug_lun_am = sdebug_lun_am_i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6698) 	if (sdebug_lun_am > SAM_LUN_AM_FLAT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6699) 		pr_warn("Invalid LUN format %u, using default\n", (int)sdebug_lun_am);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6700) 		sdebug_lun_am = SAM_LUN_AM_PERIPHERAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6701) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6703) 	if (sdebug_max_luns > 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6704) 		if (sdebug_max_luns > 16384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6705) 			pr_warn("max_luns can be no more than 16384, use default\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6706) 			sdebug_max_luns = DEF_MAX_LUNS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6707) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6708) 		sdebug_lun_am = SAM_LUN_AM_FLAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6709) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6711) 	if (sdebug_lowest_aligned > 0x3fff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6712) 		pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6713) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6714) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6716) 	if (submit_queues < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6717) 		pr_err("submit_queues must be 1 or more\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6718) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6719) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6721) 	if ((sdebug_max_queue > SDEBUG_CANQUEUE) || (sdebug_max_queue < 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6722) 		pr_err("max_queue must be in range [1, %d]\n", SDEBUG_CANQUEUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6723) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6724) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6726) 	if ((sdebug_host_max_queue > SDEBUG_CANQUEUE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6727) 	    (sdebug_host_max_queue < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6728) 		pr_err("host_max_queue must be in range [0 %d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6729) 		       SDEBUG_CANQUEUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6730) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6733) 	if (sdebug_host_max_queue &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6734) 	    (sdebug_max_queue != sdebug_host_max_queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6735) 		sdebug_max_queue = sdebug_host_max_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6736) 		pr_warn("fixing max submit queue depth to host max queue depth, %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6737) 			sdebug_max_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6740) 	sdebug_q_arr = kcalloc(submit_queues, sizeof(struct sdebug_queue),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6741) 			       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6742) 	if (sdebug_q_arr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6743) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6744) 	for (k = 0; k < submit_queues; ++k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6745) 		spin_lock_init(&sdebug_q_arr[k].qc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6747) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6748) 	 * check for host managed zoned block device specified with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6749) 	 * ptype=0x14 or zbc=XXX.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6750) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6751) 	if (sdebug_ptype == TYPE_ZBC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6752) 		sdeb_zbc_model = BLK_ZONED_HM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6753) 	} else if (sdeb_zbc_model_s && *sdeb_zbc_model_s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6754) 		k = sdeb_zbc_model_str(sdeb_zbc_model_s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6755) 		if (k < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6756) 			ret = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6757) 			goto free_q_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6758) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6759) 		sdeb_zbc_model = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6760) 		switch (sdeb_zbc_model) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6761) 		case BLK_ZONED_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6762) 		case BLK_ZONED_HA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6763) 			sdebug_ptype = TYPE_DISK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6764) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6765) 		case BLK_ZONED_HM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6766) 			sdebug_ptype = TYPE_ZBC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6767) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6768) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6769) 			pr_err("Invalid ZBC model\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6770) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6771) 			goto free_q_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6772) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6773) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6774) 	if (sdeb_zbc_model != BLK_ZONED_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6775) 		sdeb_zbc_in_use = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6776) 		if (sdebug_dev_size_mb == DEF_DEV_SIZE_PRE_INIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6777) 			sdebug_dev_size_mb = DEF_ZBC_DEV_SIZE_MB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6778) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6780) 	if (sdebug_dev_size_mb == DEF_DEV_SIZE_PRE_INIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6781) 		sdebug_dev_size_mb = DEF_DEV_SIZE_MB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6782) 	if (sdebug_dev_size_mb < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6783) 		sdebug_dev_size_mb = 1;  /* force minimum 1 MB ramdisk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6784) 	sz = (unsigned long)sdebug_dev_size_mb * 1048576;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6785) 	sdebug_store_sectors = sz / sdebug_sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6786) 	sdebug_capacity = get_sdebug_capacity();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6788) 	/* play around with geometry, don't waste too much on track 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6789) 	sdebug_heads = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6790) 	sdebug_sectors_per = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6791) 	if (sdebug_dev_size_mb >= 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6792) 		sdebug_heads = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6793) 	else if (sdebug_dev_size_mb >= 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6794) 		sdebug_heads = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6795) 	sdebug_cylinders_per = (unsigned long)sdebug_capacity /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6796) 			       (sdebug_sectors_per * sdebug_heads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6797) 	if (sdebug_cylinders_per >= 1024) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6798) 		/* other LLDs do this; implies >= 1GB ram disk ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6799) 		sdebug_heads = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6800) 		sdebug_sectors_per = 63;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6801) 		sdebug_cylinders_per = (unsigned long)sdebug_capacity /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6802) 			       (sdebug_sectors_per * sdebug_heads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6803) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6804) 	if (scsi_debug_lbp()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6805) 		sdebug_unmap_max_blocks =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6806) 			clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6808) 		sdebug_unmap_max_desc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6809) 			clamp(sdebug_unmap_max_desc, 0U, 256U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6811) 		sdebug_unmap_granularity =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6812) 			clamp(sdebug_unmap_granularity, 1U, 0xffffffffU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6814) 		if (sdebug_unmap_alignment &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6815) 		    sdebug_unmap_granularity <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6816) 		    sdebug_unmap_alignment) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6817) 			pr_err("ERR: unmap_granularity <= unmap_alignment\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6818) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6819) 			goto free_q_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6820) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6821) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6822) 	xa_init_flags(per_store_ap, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6823) 	if (want_store) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6824) 		idx = sdebug_add_store();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6825) 		if (idx < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6826) 			ret = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6827) 			goto free_q_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6828) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6829) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6831) 	pseudo_primary = root_device_register("pseudo_0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6832) 	if (IS_ERR(pseudo_primary)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6833) 		pr_warn("root_device_register() error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6834) 		ret = PTR_ERR(pseudo_primary);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6835) 		goto free_vm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6836) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6837) 	ret = bus_register(&pseudo_lld_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6838) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6839) 		pr_warn("bus_register error: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6840) 		goto dev_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6841) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6842) 	ret = driver_register(&sdebug_driverfs_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6843) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6844) 		pr_warn("driver_register error: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6845) 		goto bus_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6846) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6848) 	hosts_to_add = sdebug_add_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6849) 	sdebug_add_host = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6851) 	for (k = 0; k < hosts_to_add; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6852) 		if (want_store && k == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6853) 			ret = sdebug_add_host_helper(idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6854) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6855) 				pr_err("add_host_helper k=%d, error=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6856) 				       k, -ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6857) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6858) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6859) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6860) 			ret = sdebug_do_add_host(want_store &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6861) 						 sdebug_per_host_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6862) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6863) 				pr_err("add_host k=%d error=%d\n", k, -ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6864) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6865) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6866) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6867) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6868) 	if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6869) 		pr_info("built %d host(s)\n", sdebug_num_hosts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6871) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6873) bus_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6874) 	bus_unregister(&pseudo_lld_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6875) dev_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6876) 	root_device_unregister(pseudo_primary);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6877) free_vm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6878) 	sdebug_erase_store(idx, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6879) free_q_arr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6880) 	kfree(sdebug_q_arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6881) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6884) static void __exit scsi_debug_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6885) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6886) 	int k = sdebug_num_hosts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6888) 	stop_all_queued();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6889) 	for (; k; k--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6890) 		sdebug_do_remove_host(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6891) 	free_all_queued();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6892) 	driver_unregister(&sdebug_driverfs_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6893) 	bus_unregister(&pseudo_lld_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6894) 	root_device_unregister(pseudo_primary);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6896) 	sdebug_erase_all_stores(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6897) 	xa_destroy(per_store_ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6898) 	kfree(sdebug_q_arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6901) device_initcall(scsi_debug_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6902) module_exit(scsi_debug_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6904) static void sdebug_release_adapter(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6906) 	struct sdebug_host_info *sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6908) 	sdbg_host = to_sdebug_host(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6909) 	kfree(sdbg_host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6910) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6912) /* idx must be valid, if sip is NULL then it will be obtained using idx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6913) static void sdebug_erase_store(int idx, struct sdeb_store_info *sip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6915) 	if (idx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6916) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6917) 	if (!sip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6918) 		if (xa_empty(per_store_ap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6919) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6920) 		sip = xa_load(per_store_ap, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6921) 		if (!sip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6922) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6923) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6924) 	vfree(sip->map_storep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6925) 	vfree(sip->dif_storep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6926) 	vfree(sip->storep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6927) 	xa_erase(per_store_ap, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6928) 	kfree(sip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6929) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6931) /* Assume apart_from_first==false only in shutdown case. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6932) static void sdebug_erase_all_stores(bool apart_from_first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6933) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6934) 	unsigned long idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6935) 	struct sdeb_store_info *sip = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6937) 	xa_for_each(per_store_ap, idx, sip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6938) 		if (apart_from_first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6939) 			apart_from_first = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6940) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6941) 			sdebug_erase_store(idx, sip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6942) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6943) 	if (apart_from_first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6944) 		sdeb_most_recent_idx = sdeb_first_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6947) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6948)  * Returns store xarray new element index (idx) if >=0 else negated errno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6949)  * Limit the number of stores to 65536.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6950)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6951) static int sdebug_add_store(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6953) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6954) 	u32 n_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6955) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6956) 	unsigned long sz = (unsigned long)sdebug_dev_size_mb * 1048576;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6957) 	struct sdeb_store_info *sip = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6958) 	struct xa_limit xal = { .max = 1 << 16, .min = 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6960) 	sip = kzalloc(sizeof(*sip), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6961) 	if (!sip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6962) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6964) 	xa_lock_irqsave(per_store_ap, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6965) 	res = __xa_alloc(per_store_ap, &n_idx, sip, xal, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6966) 	if (unlikely(res < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6967) 		xa_unlock_irqrestore(per_store_ap, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6968) 		kfree(sip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6969) 		pr_warn("%s: xa_alloc() errno=%d\n", __func__, -res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6970) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6971) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6972) 	sdeb_most_recent_idx = n_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6973) 	if (sdeb_first_idx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6974) 		sdeb_first_idx = n_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6975) 	xa_unlock_irqrestore(per_store_ap, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6977) 	res = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6978) 	sip->storep = vzalloc(sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6979) 	if (!sip->storep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6980) 		pr_err("user data oom\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6981) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6982) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6983) 	if (sdebug_num_parts > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6984) 		sdebug_build_parts(sip->storep, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6986) 	/* DIF/DIX: what T10 calls Protection Information (PI) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6987) 	if (sdebug_dix) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6988) 		int dif_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6990) 		dif_size = sdebug_store_sectors * sizeof(struct t10_pi_tuple);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6991) 		sip->dif_storep = vmalloc(dif_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6993) 		pr_info("dif_storep %u bytes @ %pK\n", dif_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6994) 			sip->dif_storep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6996) 		if (!sip->dif_storep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6997) 			pr_err("DIX oom\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6998) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6999) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7000) 		memset(sip->dif_storep, 0xff, dif_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7001) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7002) 	/* Logical Block Provisioning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7003) 	if (scsi_debug_lbp()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7004) 		map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7005) 		sip->map_storep = vmalloc(array_size(sizeof(long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7006) 						     BITS_TO_LONGS(map_size)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7008) 		pr_info("%lu provisioning blocks\n", map_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7010) 		if (!sip->map_storep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7011) 			pr_err("LBP map oom\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7012) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7013) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7015) 		bitmap_zero(sip->map_storep, map_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7017) 		/* Map first 1KB for partition table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7018) 		if (sdebug_num_parts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7019) 			map_region(sip, 0, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7020) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7022) 	rwlock_init(&sip->macc_lck);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7023) 	return (int)n_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7024) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7025) 	sdebug_erase_store((int)n_idx, sip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7026) 	pr_warn("%s: failed, errno=%d\n", __func__, -res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7027) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7030) static int sdebug_add_host_helper(int per_host_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7032) 	int k, devs_per_host, idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7033) 	int error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7034) 	struct sdebug_host_info *sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7035) 	struct sdebug_dev_info *sdbg_devinfo, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7037) 	sdbg_host = kzalloc(sizeof(*sdbg_host), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7038) 	if (!sdbg_host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7039) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7040) 	idx = (per_host_idx < 0) ? sdeb_first_idx : per_host_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7041) 	if (xa_get_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7042) 		xa_clear_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7043) 	sdbg_host->si_idx = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7045) 	INIT_LIST_HEAD(&sdbg_host->dev_info_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7047) 	devs_per_host = sdebug_num_tgts * sdebug_max_luns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7048) 	for (k = 0; k < devs_per_host; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7049) 		sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7050) 		if (!sdbg_devinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7051) 			goto clean;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7052) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7054) 	spin_lock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7055) 	list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7056) 	spin_unlock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7058) 	sdbg_host->dev.bus = &pseudo_lld_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7059) 	sdbg_host->dev.parent = pseudo_primary;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7060) 	sdbg_host->dev.release = &sdebug_release_adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7061) 	dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_num_hosts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7063) 	error = device_register(&sdbg_host->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7064) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7065) 		goto clean;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7067) 	++sdebug_num_hosts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7068) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7070) clean:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7071) 	list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7072) 				 dev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7073) 		list_del(&sdbg_devinfo->dev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7074) 		kfree(sdbg_devinfo->zstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7075) 		kfree(sdbg_devinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7076) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7077) 	kfree(sdbg_host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7078) 	pr_warn("%s: failed, errno=%d\n", __func__, -error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7079) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7082) static int sdebug_do_add_host(bool mk_new_store)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7083) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7084) 	int ph_idx = sdeb_most_recent_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7086) 	if (mk_new_store) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7087) 		ph_idx = sdebug_add_store();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7088) 		if (ph_idx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7089) 			return ph_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7090) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7091) 	return sdebug_add_host_helper(ph_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7094) static void sdebug_do_remove_host(bool the_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7095) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7096) 	int idx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7097) 	struct sdebug_host_info *sdbg_host = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7098) 	struct sdebug_host_info *sdbg_host2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7100) 	spin_lock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7101) 	if (!list_empty(&sdebug_host_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7102) 		sdbg_host = list_entry(sdebug_host_list.prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7103) 				       struct sdebug_host_info, host_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7104) 		idx = sdbg_host->si_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7106) 	if (!the_end && idx >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7107) 		bool unique = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7109) 		list_for_each_entry(sdbg_host2, &sdebug_host_list, host_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7110) 			if (sdbg_host2 == sdbg_host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7111) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7112) 			if (idx == sdbg_host2->si_idx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7113) 				unique = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7114) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7115) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7116) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7117) 		if (unique) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7118) 			xa_set_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7119) 			if (idx == sdeb_most_recent_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7120) 				--sdeb_most_recent_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7121) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7123) 	if (sdbg_host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7124) 		list_del(&sdbg_host->host_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7125) 	spin_unlock(&sdebug_host_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7127) 	if (!sdbg_host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7128) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7130) 	device_unregister(&sdbg_host->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7131) 	--sdebug_num_hosts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7134) static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7136) 	int num_in_q = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7137) 	struct sdebug_dev_info *devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7139) 	block_unblock_all_queues(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7140) 	devip = (struct sdebug_dev_info *)sdev->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7141) 	if (NULL == devip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7142) 		block_unblock_all_queues(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7143) 		return	-ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7145) 	num_in_q = atomic_read(&devip->num_in_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7147) 	if (qdepth < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7148) 		qdepth = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7149) 	/* allow to exceed max host qc_arr elements for testing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7150) 	if (qdepth > SDEBUG_CANQUEUE + 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7151) 		qdepth = SDEBUG_CANQUEUE + 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7152) 	scsi_change_queue_depth(sdev, qdepth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7154) 	if (SDEBUG_OPT_Q_NOISE & sdebug_opts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7155) 		sdev_printk(KERN_INFO, sdev, "%s: qdepth=%d, num_in_q=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7156) 			    __func__, qdepth, num_in_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7158) 	block_unblock_all_queues(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7159) 	return sdev->queue_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7162) static bool fake_timeout(struct scsi_cmnd *scp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7164) 	if (0 == (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7165) 		if (sdebug_every_nth < -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7166) 			sdebug_every_nth = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7167) 		if (SDEBUG_OPT_TIMEOUT & sdebug_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7168) 			return true; /* ignore command causing timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7169) 		else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7170) 			 scsi_medium_access_command(scp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7171) 			return true; /* time out reads and writes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7173) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7176) /* Response to TUR or media access command when device stopped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7177) static int resp_not_ready(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7179) 	int stopped_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7180) 	u64 diff_ns = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7181) 	ktime_t now_ts = ktime_get_boottime();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7182) 	struct scsi_device *sdp = scp->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7184) 	stopped_state = atomic_read(&devip->stopped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7185) 	if (stopped_state == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7186) 		if (ktime_to_ns(now_ts) > ktime_to_ns(devip->create_ts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7187) 			diff_ns = ktime_to_ns(ktime_sub(now_ts, devip->create_ts));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7188) 			if (diff_ns >= ((u64)sdeb_tur_ms_to_ready * 1000000)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7189) 				/* tur_ms_to_ready timer extinguished */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7190) 				atomic_set(&devip->stopped, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7191) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7192) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7193) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7194) 		mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7195) 		if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7196) 			sdev_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7197) 				    "%s: Not ready: in process of becoming ready\n", my_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7198) 		if (scp->cmnd[0] == TEST_UNIT_READY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7199) 			u64 tur_nanosecs_to_ready = (u64)sdeb_tur_ms_to_ready * 1000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7201) 			if (diff_ns <= tur_nanosecs_to_ready)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7202) 				diff_ns = tur_nanosecs_to_ready - diff_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7203) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7204) 				diff_ns = tur_nanosecs_to_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7205) 			/* As per 20-061r2 approved for spc6 by T10 on 20200716 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7206) 			do_div(diff_ns, 1000000);	/* diff_ns becomes milliseconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7207) 			scsi_set_sense_information(scp->sense_buffer, SCSI_SENSE_BUFFERSIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7208) 						   diff_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7209) 			return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7210) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7212) 	mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7213) 	if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7214) 		sdev_printk(KERN_INFO, sdp, "%s: Not ready: initializing command required\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7215) 			    my_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7216) 	return check_condition_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7219) static int scsi_debug_queuecommand(struct Scsi_Host *shost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7220) 				   struct scsi_cmnd *scp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7222) 	u8 sdeb_i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7223) 	struct scsi_device *sdp = scp->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7224) 	const struct opcode_info_t *oip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7225) 	const struct opcode_info_t *r_oip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7226) 	struct sdebug_dev_info *devip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7227) 	u8 *cmd = scp->cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7228) 	int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7229) 	int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7230) 	int k, na;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7231) 	int errsts = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7232) 	u64 lun_index = sdp->lun & 0x3FFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7233) 	u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7234) 	u16 sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7235) 	u8 opcode = cmd[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7236) 	bool has_wlun_rl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7237) 	bool inject_now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7239) 	scsi_set_resid(scp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7240) 	if (sdebug_statistics) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7241) 		atomic_inc(&sdebug_cmnd_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7242) 		inject_now = inject_on_this_cmd();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7243) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7244) 		inject_now = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7246) 	if (unlikely(sdebug_verbose &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7247) 		     !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7248) 		char b[120];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7249) 		int n, len, sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7251) 		len = scp->cmd_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7252) 		sb = (int)sizeof(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7253) 		if (len > 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7254) 			strcpy(b, "too long, over 32 bytes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7255) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7256) 			for (k = 0, n = 0; k < len && n < sb; ++k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7257) 				n += scnprintf(b + n, sb - n, "%02x ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7258) 					       (u32)cmd[k]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7259) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7260) 		sdev_printk(KERN_INFO, sdp, "%s: tag=%#x, cmd %s\n", my_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7261) 			    blk_mq_unique_tag(scp->request), b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7262) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7263) 	if (unlikely(inject_now && (sdebug_opts & SDEBUG_OPT_HOST_BUSY)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7264) 		return SCSI_MLQUEUE_HOST_BUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7265) 	has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7266) 	if (unlikely(lun_index >= sdebug_max_luns && !has_wlun_rl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7267) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7269) 	sdeb_i = opcode_ind_arr[opcode];	/* fully mapped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7270) 	oip = &opcode_info_arr[sdeb_i];		/* safe if table consistent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7271) 	devip = (struct sdebug_dev_info *)sdp->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7272) 	if (unlikely(!devip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7273) 		devip = find_build_dev_info(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7274) 		if (NULL == devip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7275) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7277) 	if (unlikely(inject_now && !atomic_read(&sdeb_inject_pending)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7278) 		atomic_set(&sdeb_inject_pending, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7280) 	na = oip->num_attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7281) 	r_pfp = oip->pfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7282) 	if (na) {	/* multiple commands with this opcode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7283) 		r_oip = oip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7284) 		if (FF_SA & r_oip->flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7285) 			if (F_SA_LOW & oip->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7286) 				sa = 0x1f & cmd[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7287) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7288) 				sa = get_unaligned_be16(cmd + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7289) 			for (k = 0; k <= na; oip = r_oip->arrp + k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7290) 				if (opcode == oip->opcode && sa == oip->sa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7291) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7292) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7293) 		} else {   /* since no service action only check opcode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7294) 			for (k = 0; k <= na; oip = r_oip->arrp + k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7295) 				if (opcode == oip->opcode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7296) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7297) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7298) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7299) 		if (k > na) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7300) 			if (F_SA_LOW & r_oip->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7301) 				mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7302) 			else if (F_SA_HIGH & r_oip->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7303) 				mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7304) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7305) 				mk_sense_invalid_opcode(scp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7306) 			goto check_cond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7307) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7308) 	}	/* else (when na==0) we assume the oip is a match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7309) 	flags = oip->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7310) 	if (unlikely(F_INV_OP & flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7311) 		mk_sense_invalid_opcode(scp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7312) 		goto check_cond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7314) 	if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7315) 		if (sdebug_verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7316) 			sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7317) 				    my_name, opcode, " supported for wlun");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7318) 		mk_sense_invalid_opcode(scp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7319) 		goto check_cond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7321) 	if (unlikely(sdebug_strict)) {	/* check cdb against mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7322) 		u8 rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7323) 		int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7325) 		for (k = 1; k < oip->len_mask[0] && k < 16; ++k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7326) 			rem = ~oip->len_mask[k] & cmd[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7327) 			if (rem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7328) 				for (j = 7; j >= 0; --j, rem <<= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7329) 					if (0x80 & rem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7330) 						break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7331) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7332) 				mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7333) 				goto check_cond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7334) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7335) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7336) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7337) 	if (unlikely(!(F_SKIP_UA & flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7338) 		     find_first_bit(devip->uas_bm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7339) 				    SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7340) 		errsts = make_ua(scp, devip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7341) 		if (errsts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7342) 			goto check_cond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7344) 	if (unlikely(((F_M_ACCESS & flags) || scp->cmnd[0] == TEST_UNIT_READY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7345) 		     atomic_read(&devip->stopped))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7346) 		errsts = resp_not_ready(scp, devip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7347) 		if (errsts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7348) 			goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7350) 	if (sdebug_fake_rw && (F_FAKE_RW & flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7351) 		goto fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7352) 	if (unlikely(sdebug_every_nth)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7353) 		if (fake_timeout(scp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7354) 			return 0;	/* ignore command: make trouble */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7356) 	if (likely(oip->pfp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7357) 		pfp = oip->pfp;	/* calls a resp_* function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7358) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7359) 		pfp = r_pfp;    /* if leaf function ptr NULL, try the root's */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7361) fini:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7362) 	if (F_DELAY_OVERR & flags)	/* cmds like INQUIRY respond asap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7363) 		return schedule_resp(scp, devip, errsts, pfp, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7364) 	else if ((flags & F_LONG_DELAY) && (sdebug_jdelay > 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7365) 					    sdebug_ndelay > 10000)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7366) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7367) 		 * Skip long delays if ndelay <= 10 microseconds. Otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7368) 		 * for Start Stop Unit (SSU) want at least 1 second delay and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7369) 		 * if sdebug_jdelay>1 want a long delay of that many seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7370) 		 * For Synchronize Cache want 1/20 of SSU's delay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7371) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7372) 		int jdelay = (sdebug_jdelay < 2) ? 1 : sdebug_jdelay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7373) 		int denom = (flags & F_SYNC_DELAY) ? 20 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7375) 		jdelay = mult_frac(USER_HZ * jdelay, HZ, denom * USER_HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7376) 		return schedule_resp(scp, devip, errsts, pfp, jdelay, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7377) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7378) 		return schedule_resp(scp, devip, errsts, pfp, sdebug_jdelay,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7379) 				     sdebug_ndelay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7380) check_cond:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7381) 	return schedule_resp(scp, devip, check_condition_result, NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7382) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7383) 	return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7386) static struct scsi_host_template sdebug_driver_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7387) 	.show_info =		scsi_debug_show_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7388) 	.write_info =		scsi_debug_write_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7389) 	.proc_name =		sdebug_proc_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7390) 	.name =			"SCSI DEBUG",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7391) 	.info =			scsi_debug_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7392) 	.slave_alloc =		scsi_debug_slave_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7393) 	.slave_configure =	scsi_debug_slave_configure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7394) 	.slave_destroy =	scsi_debug_slave_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7395) 	.ioctl =		scsi_debug_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7396) 	.queuecommand =		scsi_debug_queuecommand,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7397) 	.change_queue_depth =	sdebug_change_qdepth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7398) 	.eh_abort_handler =	scsi_debug_abort,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7399) 	.eh_device_reset_handler = scsi_debug_device_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7400) 	.eh_target_reset_handler = scsi_debug_target_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7401) 	.eh_bus_reset_handler = scsi_debug_bus_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7402) 	.eh_host_reset_handler = scsi_debug_host_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7403) 	.can_queue =		SDEBUG_CANQUEUE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7404) 	.this_id =		7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7405) 	.sg_tablesize =		SG_MAX_SEGMENTS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7406) 	.cmd_per_lun =		DEF_CMD_PER_LUN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7407) 	.max_sectors =		-1U,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7408) 	.max_segment_size =	-1U,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7409) 	.module =		THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7410) 	.track_queue_depth =	1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7411) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7413) static int sdebug_driver_probe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7415) 	int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7416) 	struct sdebug_host_info *sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7417) 	struct Scsi_Host *hpnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7418) 	int hprot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7420) 	sdbg_host = to_sdebug_host(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7422) 	sdebug_driver_template.can_queue = sdebug_max_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7423) 	if (!sdebug_clustering)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7424) 		sdebug_driver_template.dma_boundary = PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7426) 	hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7427) 	if (NULL == hpnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7428) 		pr_err("scsi_host_alloc failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7429) 		error = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7430) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7432) 	if (submit_queues > nr_cpu_ids) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7433) 		pr_warn("%s: trim submit_queues (was %d) to nr_cpu_ids=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7434) 			my_name, submit_queues, nr_cpu_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7435) 		submit_queues = nr_cpu_ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7436) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7437) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7438) 	 * Decide whether to tell scsi subsystem that we want mq. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7439) 	 * following should give the same answer for each host.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7440) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7441) 	hpnt->nr_hw_queues = submit_queues;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7442) 	if (sdebug_host_max_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7443) 		hpnt->host_tagset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7445) 	sdbg_host->shost = hpnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7446) 	*((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7447) 	if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7448) 		hpnt->max_id = sdebug_num_tgts + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7449) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7450) 		hpnt->max_id = sdebug_num_tgts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7451) 	/* = sdebug_max_luns; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7452) 	hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7454) 	hprot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7456) 	switch (sdebug_dif) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7458) 	case T10_PI_TYPE1_PROTECTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7459) 		hprot = SHOST_DIF_TYPE1_PROTECTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7460) 		if (sdebug_dix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7461) 			hprot |= SHOST_DIX_TYPE1_PROTECTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7462) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7464) 	case T10_PI_TYPE2_PROTECTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7465) 		hprot = SHOST_DIF_TYPE2_PROTECTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7466) 		if (sdebug_dix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7467) 			hprot |= SHOST_DIX_TYPE2_PROTECTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7468) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7470) 	case T10_PI_TYPE3_PROTECTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7471) 		hprot = SHOST_DIF_TYPE3_PROTECTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7472) 		if (sdebug_dix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7473) 			hprot |= SHOST_DIX_TYPE3_PROTECTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7474) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7476) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7477) 		if (sdebug_dix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7478) 			hprot |= SHOST_DIX_TYPE0_PROTECTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7479) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7482) 	scsi_host_set_prot(hpnt, hprot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7484) 	if (have_dif_prot || sdebug_dix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7485) 		pr_info("host protection%s%s%s%s%s%s%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7486) 			(hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7487) 			(hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7488) 			(hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7489) 			(hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7490) 			(hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7491) 			(hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7492) 			(hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7494) 	if (sdebug_guard == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7495) 		scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7496) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7497) 		scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7499) 	sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7500) 	sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & sdebug_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7501) 	if (sdebug_every_nth)	/* need stats counters for every_nth */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7502) 		sdebug_statistics = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7503) 	error = scsi_add_host(hpnt, &sdbg_host->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7504) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7505) 		pr_err("scsi_add_host failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7506) 		error = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7507) 		scsi_host_put(hpnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7508) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7509) 		scsi_scan_host(hpnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7510) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7512) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7515) static int sdebug_driver_remove(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7517) 	struct sdebug_host_info *sdbg_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7518) 	struct sdebug_dev_info *sdbg_devinfo, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7520) 	sdbg_host = to_sdebug_host(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7522) 	if (!sdbg_host) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7523) 		pr_err("Unable to locate host info\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7524) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7525) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7527) 	scsi_remove_host(sdbg_host->shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7529) 	list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7530) 				 dev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7531) 		list_del(&sdbg_devinfo->dev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7532) 		kfree(sdbg_devinfo->zstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7533) 		kfree(sdbg_devinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7536) 	scsi_host_put(sdbg_host->shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7537) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7540) static int pseudo_lld_bus_match(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7541) 				struct device_driver *dev_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7543) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7546) static struct bus_type pseudo_lld_bus = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7547) 	.name = "pseudo",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7548) 	.match = pseudo_lld_bus_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7549) 	.probe = sdebug_driver_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7550) 	.remove = sdebug_driver_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7551) 	.drv_groups = sdebug_drv_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7552) };