Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv |
| 3 | * Copyright (C) 1992 Eric Youngdale |
| 4 | * Simulate a host adapter with 2 disks attached. Do a lot of checking |
| 5 | * to make sure that we are not getting blocks mixed up, and PANIC if |
| 6 | * anything out of the ordinary is seen. |
| 7 | * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 8 | * |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 9 | * Copyright (C) 2001 - 2018 Douglas Gilbert |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2, or (at your option) |
| 14 | * any later version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 16 | * For documentation see http://sg.danny.cz/sg/sdebug26.html |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 20 | |
| 21 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | |
| 25 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/errno.h> |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 27 | #include <linux/jiffies.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/types.h> |
| 30 | #include <linux/string.h> |
| 31 | #include <linux/genhd.h> |
| 32 | #include <linux/fs.h> |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/proc_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/vmalloc.h> |
| 36 | #include <linux/moduleparam.h> |
Jens Axboe | 852e034 | 2007-07-16 10:19:24 +0200 | [diff] [blame] | 37 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/blkdev.h> |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 39 | #include <linux/crc-t10dif.h> |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 40 | #include <linux/spinlock.h> |
| 41 | #include <linux/interrupt.h> |
| 42 | #include <linux/atomic.h> |
| 43 | #include <linux/hrtimer.h> |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 44 | #include <linux/uuid.h> |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 45 | #include <linux/t10-pi.h> |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 46 | |
| 47 | #include <net/checksum.h> |
FUJITA Tomonori | 9ff26ee | 2008-03-02 18:30:15 +0900 | [diff] [blame] | 48 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 49 | #include <asm/unaligned.h> |
| 50 | |
FUJITA Tomonori | 9ff26ee | 2008-03-02 18:30:15 +0900 | [diff] [blame] | 51 | #include <scsi/scsi.h> |
| 52 | #include <scsi/scsi_cmnd.h> |
| 53 | #include <scsi/scsi_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include <scsi/scsi_host.h> |
| 55 | #include <scsi/scsicam.h> |
FUJITA Tomonori | a34c4e9 | 2008-03-25 09:26:50 +0900 | [diff] [blame] | 56 | #include <scsi/scsi_eh.h> |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 57 | #include <scsi/scsi_tcq.h> |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 58 | #include <scsi/scsi_dbg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 60 | #include "sd.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include "scsi_logging.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 63 | /* make sure inq_product_rev string corresponds to this version */ |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 64 | #define SDEBUG_VERSION "0188" /* format to fit INQUIRY revision field */ |
| 65 | static const char *sdebug_version_date = "20180128"; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 66 | |
| 67 | #define MY_NAME "scsi_debug" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 69 | /* Additional Sense Code (ASC) */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 70 | #define NO_ADDITIONAL_SENSE 0x0 |
| 71 | #define LOGICAL_UNIT_NOT_READY 0x4 |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 72 | #define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | #define UNRECOVERED_READ_ERR 0x11 |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 74 | #define PARAMETER_LIST_LENGTH_ERR 0x1a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #define INVALID_OPCODE 0x20 |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 76 | #define LBA_OUT_OF_RANGE 0x21 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | #define INVALID_FIELD_IN_CDB 0x24 |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 78 | #define INVALID_FIELD_IN_PARAM_LIST 0x26 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 79 | #define UA_RESET_ASC 0x29 |
| 80 | #define UA_CHANGED_ASC 0x2a |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 81 | #define TARGET_CHANGED_ASC 0x3f |
| 82 | #define LUNS_CHANGED_ASCQ 0x0e |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 83 | #define INSUFF_RES_ASC 0x55 |
| 84 | #define INSUFF_RES_ASCQ 0x3 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 85 | #define POWER_ON_RESET_ASCQ 0x0 |
| 86 | #define BUS_RESET_ASCQ 0x2 /* scsi bus reset occurred */ |
| 87 | #define MODE_CHANGED_ASCQ 0x1 /* mode parameters changed */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 88 | #define CAPACITY_CHANGED_ASCQ 0x9 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | #define SAVING_PARAMS_UNSUP 0x39 |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 90 | #define TRANSPORT_PROBLEM 0x4b |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 91 | #define THRESHOLD_EXCEEDED 0x5d |
| 92 | #define LOW_POWER_COND_ON 0x5e |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 93 | #define MISCOMPARE_VERIFY_ASC 0x1d |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 94 | #define MICROCODE_CHANGED_ASCQ 0x1 /* with TARGET_CHANGED_ASC */ |
| 95 | #define MICROCODE_CHANGED_WO_RESET_ASCQ 0x16 |
Douglas Gilbert | 481b5e5 | 2017-12-23 12:48:14 -0500 | [diff] [blame] | 96 | #define WRITE_ERROR_ASC 0xc |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 98 | /* Additional Sense Code Qualifier (ASCQ) */ |
| 99 | #define ACK_NAK_TO 0x3 |
| 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | /* Default values for driver parameters */ |
| 102 | #define DEF_NUM_HOST 1 |
| 103 | #define DEF_NUM_TGTS 1 |
| 104 | #define DEF_MAX_LUNS 1 |
| 105 | /* With these defaults, this driver will make 1 host with 1 target |
| 106 | * (id 0) containing 1 logical unit (lun 0). That is 1 device. |
| 107 | */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 108 | #define DEF_ATO 1 |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 109 | #define DEF_CDB_LEN 10 |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 110 | #define DEF_JDELAY 1 /* if > 0 unit is a jiffy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | #define DEF_DEV_SIZE_MB 8 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 112 | #define DEF_DIF 0 |
| 113 | #define DEF_DIX 0 |
| 114 | #define DEF_D_SENSE 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | #define DEF_EVERY_NTH 0 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 116 | #define DEF_FAKE_RW 0 |
| 117 | #define DEF_GUARD 0 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 118 | #define DEF_HOST_LOCK 0 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 119 | #define DEF_LBPU 0 |
| 120 | #define DEF_LBPWS 0 |
| 121 | #define DEF_LBPWS10 0 |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 122 | #define DEF_LBPRZ 1 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 123 | #define DEF_LOWEST_ALIGNED 0 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 124 | #define DEF_NDELAY 0 /* if > 0 unit is a nanosecond */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 125 | #define DEF_NO_LUN_0 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | #define DEF_NUM_PARTS 0 |
| 127 | #define DEF_OPTS 0 |
Martin K. Petersen | 32c5844 | 2015-12-16 17:53:51 -0500 | [diff] [blame] | 128 | #define DEF_OPT_BLKS 1024 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 129 | #define DEF_PHYSBLK_EXP 0 |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 130 | #define DEF_OPT_XFERLEN_EXP 0 |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 131 | #define DEF_PTYPE TYPE_DISK |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 132 | #define DEF_REMOVABLE false |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 133 | #define DEF_SCSI_LEVEL 7 /* INQUIRY, byte2 [6->SPC-4; 7->SPC-5] */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 134 | #define DEF_SECTOR_SIZE 512 |
| 135 | #define DEF_UNMAP_ALIGNMENT 0 |
| 136 | #define DEF_UNMAP_GRANULARITY 1 |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 137 | #define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF |
| 138 | #define DEF_UNMAP_MAX_DESC 256 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 139 | #define DEF_VIRTUAL_GB 0 |
| 140 | #define DEF_VPD_USE_HOSTNO 1 |
| 141 | #define DEF_WRITESAME_LENGTH 0xFFFF |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 142 | #define DEF_STRICT 0 |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 143 | #define DEF_STATISTICS false |
| 144 | #define DEF_SUBMIT_QUEUES 1 |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 145 | #define DEF_UUID_CTL 0 |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 146 | #define JDELAY_OVERRIDDEN -9999 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 148 | #define SDEBUG_LUN_0_VAL 0 |
| 149 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 150 | /* bit mask values for sdebug_opts */ |
| 151 | #define SDEBUG_OPT_NOISE 1 |
| 152 | #define SDEBUG_OPT_MEDIUM_ERR 2 |
| 153 | #define SDEBUG_OPT_TIMEOUT 4 |
| 154 | #define SDEBUG_OPT_RECOVERED_ERR 8 |
| 155 | #define SDEBUG_OPT_TRANSPORT_ERR 16 |
| 156 | #define SDEBUG_OPT_DIF_ERR 32 |
| 157 | #define SDEBUG_OPT_DIX_ERR 64 |
| 158 | #define SDEBUG_OPT_MAC_TIMEOUT 128 |
| 159 | #define SDEBUG_OPT_SHORT_TRANSFER 0x100 |
| 160 | #define SDEBUG_OPT_Q_NOISE 0x200 |
| 161 | #define SDEBUG_OPT_ALL_TSF 0x400 |
| 162 | #define SDEBUG_OPT_RARE_TSF 0x800 |
| 163 | #define SDEBUG_OPT_N_WCE 0x1000 |
| 164 | #define SDEBUG_OPT_RESET_NOISE 0x2000 |
| 165 | #define SDEBUG_OPT_NO_CDB_NOISE 0x4000 |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 166 | #define SDEBUG_OPT_HOST_BUSY 0x8000 |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 167 | #define SDEBUG_OPT_CMD_ABORT 0x10000 |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 168 | #define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \ |
| 169 | SDEBUG_OPT_RESET_NOISE) |
| 170 | #define SDEBUG_OPT_ALL_INJECTING (SDEBUG_OPT_RECOVERED_ERR | \ |
| 171 | SDEBUG_OPT_TRANSPORT_ERR | \ |
| 172 | SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR | \ |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 173 | SDEBUG_OPT_SHORT_TRANSFER | \ |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 174 | SDEBUG_OPT_HOST_BUSY | \ |
| 175 | SDEBUG_OPT_CMD_ABORT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | /* When "every_nth" > 0 then modulo "every_nth" commands: |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 177 | * - a missing response is simulated if SDEBUG_OPT_TIMEOUT is set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | * - a RECOVERED_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 179 | * commands if SDEBUG_OPT_RECOVERED_ERR is set. |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 180 | * - a TRANSPORT_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 181 | * commands if SDEBUG_OPT_TRANSPORT_ERR is set. |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 182 | * - similarly for DIF_ERR, DIX_ERR, SHORT_TRANSFER, HOST_BUSY and |
| 183 | * CMD_ABORT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | * |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 185 | * When "every_nth" < 0 then after "- every_nth" commands the selected |
| 186 | * error will be injected. The error will be injected on every subsequent |
| 187 | * command until some other action occurs; for example, the user writing |
| 188 | * a new value (other than -1 or 1) to every_nth: |
| 189 | * echo 0 > /sys/bus/pseudo/drivers/scsi_debug/every_nth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | */ |
| 191 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 192 | /* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs) are returned in |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 193 | * priority order. In the subset implemented here lower numbers have higher |
| 194 | * priority. The UA numbers should be a sequence starting from 0 with |
| 195 | * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */ |
| 196 | #define SDEBUG_UA_POR 0 /* Power on, reset, or bus device reset */ |
| 197 | #define SDEBUG_UA_BUS_RESET 1 |
| 198 | #define SDEBUG_UA_MODE_CHANGED 2 |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 199 | #define SDEBUG_UA_CAPACITY_CHANGED 3 |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 200 | #define SDEBUG_UA_LUNS_CHANGED 4 |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 201 | #define SDEBUG_UA_MICROCODE_CHANGED 5 /* simulate firmware change */ |
| 202 | #define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 6 |
| 203 | #define SDEBUG_NUM_UAS 7 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 204 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 205 | /* when 1==SDEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | * sector on read commands: */ |
| 207 | #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */ |
Douglas Gilbert | 32f7ef7 | 2011-03-11 10:43:35 -0500 | [diff] [blame] | 208 | #define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1) |
| 211 | * or "peripheral device" addressing (value 0) */ |
| 212 | #define SAM2_LUN_ADDRESS_METHOD 0 |
| 213 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 214 | /* SDEBUG_CANQUEUE is the maximum number of commands that can be queued |
| 215 | * (for response) per submit queue at one time. Can be reduced by max_queue |
| 216 | * option. Command responses are not queued when jdelay=0 and ndelay=0. The |
| 217 | * per-device DEF_CMD_PER_LUN can be changed via sysfs: |
| 218 | * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth |
| 219 | * but cannot exceed SDEBUG_CANQUEUE . |
| 220 | */ |
| 221 | #define SDEBUG_CANQUEUE_WORDS 3 /* a WORD is bits in a long */ |
| 222 | #define SDEBUG_CANQUEUE (SDEBUG_CANQUEUE_WORDS * BITS_PER_LONG) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 223 | #define DEF_CMD_PER_LUN 255 |
| 224 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 225 | #define F_D_IN 1 |
| 226 | #define F_D_OUT 2 |
| 227 | #define F_D_OUT_MAYBE 4 /* WRITE SAME, NDOB bit */ |
| 228 | #define F_D_UNKN 8 |
| 229 | #define F_RL_WLUN_OK 0x10 |
| 230 | #define F_SKIP_UA 0x20 |
| 231 | #define F_DELAY_OVERR 0x40 |
| 232 | #define F_SA_LOW 0x80 /* cdb byte 1, bits 4 to 0 */ |
| 233 | #define F_SA_HIGH 0x100 /* as used by variable length cdbs */ |
| 234 | #define F_INV_OP 0x200 |
| 235 | #define F_FAKE_RW 0x400 |
| 236 | #define F_M_ACCESS 0x800 /* media access */ |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 237 | #define F_SSU_DELAY 0x1000 |
| 238 | #define F_SYNC_DELAY 0x2000 |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 239 | |
| 240 | #define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR) |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 241 | #define FF_MEDIA_IO (F_M_ACCESS | F_FAKE_RW) |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 242 | #define FF_SA (F_SA_HIGH | F_SA_LOW) |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 243 | #define F_LONG_DELAY (F_SSU_DELAY | F_SYNC_DELAY) |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 244 | |
| 245 | #define SDEBUG_MAX_PARTS 4 |
| 246 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 247 | #define SDEBUG_MAX_CMD_LEN 32 |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 248 | |
| 249 | |
| 250 | struct sdebug_dev_info { |
| 251 | struct list_head dev_list; |
| 252 | unsigned int channel; |
| 253 | unsigned int target; |
| 254 | u64 lun; |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 255 | uuid_t lu_name; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 256 | struct sdebug_host_info *sdbg_host; |
| 257 | unsigned long uas_bm[1]; |
| 258 | atomic_t num_in_q; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 259 | atomic_t stopped; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 260 | bool used; |
| 261 | }; |
| 262 | |
| 263 | struct sdebug_host_info { |
| 264 | struct list_head host_list; |
| 265 | struct Scsi_Host *shost; |
| 266 | struct device dev; |
| 267 | struct list_head dev_info_list; |
| 268 | }; |
| 269 | |
| 270 | #define to_sdebug_host(d) \ |
| 271 | container_of(d, struct sdebug_host_info, dev) |
| 272 | |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 273 | enum sdeb_defer_type {SDEB_DEFER_NONE = 0, SDEB_DEFER_HRT = 1, |
| 274 | SDEB_DEFER_WQ = 2}; |
| 275 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 276 | struct sdebug_defer { |
| 277 | struct hrtimer hrt; |
| 278 | struct execute_work ew; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 279 | int sqa_idx; /* index of sdebug_queue array */ |
| 280 | int qc_idx; /* index of sdebug_queued_cmd array within sqa_idx */ |
| 281 | int issuing_cpu; |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 282 | bool init_hrt; |
| 283 | bool init_wq; |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 284 | bool aborted; /* true when blk_abort_request() already called */ |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 285 | enum sdeb_defer_type defer_t; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | struct sdebug_queued_cmd { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 289 | /* corresponding bit set in in_use_bm[] in owning struct sdebug_queue |
| 290 | * instance indicates this slot is in use. |
| 291 | */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 292 | struct sdebug_defer *sd_dp; |
| 293 | struct scsi_cmnd *a_cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 294 | unsigned int inj_recovered:1; |
| 295 | unsigned int inj_transport:1; |
| 296 | unsigned int inj_dif:1; |
| 297 | unsigned int inj_dix:1; |
| 298 | unsigned int inj_short:1; |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 299 | unsigned int inj_host_busy:1; |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 300 | unsigned int inj_cmd_abort:1; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 301 | }; |
| 302 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 303 | struct sdebug_queue { |
| 304 | struct sdebug_queued_cmd qc_arr[SDEBUG_CANQUEUE]; |
| 305 | unsigned long in_use_bm[SDEBUG_CANQUEUE_WORDS]; |
| 306 | spinlock_t qc_lock; |
| 307 | atomic_t blocked; /* to temporarily stop more being queued */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 308 | }; |
| 309 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 310 | static atomic_t sdebug_cmnd_count; /* number of incoming commands */ |
| 311 | static atomic_t sdebug_completions; /* count of deferred completions */ |
| 312 | static atomic_t sdebug_miss_cpus; /* submission + completion cpus differ */ |
| 313 | static atomic_t sdebug_a_tsf; /* 'almost task set full' counter */ |
| 314 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 315 | struct opcode_info_t { |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 316 | u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff */ |
| 317 | /* for terminating element */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 318 | u8 opcode; /* if num_attached > 0, preferred */ |
| 319 | u16 sa; /* service action */ |
| 320 | u32 flags; /* OR-ed set of SDEB_F_* */ |
| 321 | int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 322 | const struct opcode_info_t *arrp; /* num_attached elements or NULL */ |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 323 | u8 len_mask[16]; /* len_mask[0]-->cdb_len, then mask for cdb */ |
| 324 | /* 1 to min(cdb_len, 15); ignore cdb[15...] */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | /* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 328 | enum sdeb_opcode_index { |
| 329 | SDEB_I_INVALID_OPCODE = 0, |
| 330 | SDEB_I_INQUIRY = 1, |
| 331 | SDEB_I_REPORT_LUNS = 2, |
| 332 | SDEB_I_REQUEST_SENSE = 3, |
| 333 | SDEB_I_TEST_UNIT_READY = 4, |
| 334 | SDEB_I_MODE_SENSE = 5, /* 6, 10 */ |
| 335 | SDEB_I_MODE_SELECT = 6, /* 6, 10 */ |
| 336 | SDEB_I_LOG_SENSE = 7, |
| 337 | SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */ |
| 338 | SDEB_I_READ = 9, /* 6, 10, 12, 16 */ |
| 339 | SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */ |
| 340 | SDEB_I_START_STOP = 11, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 341 | SDEB_I_SERV_ACT_IN_16 = 12, /* add ...SERV_ACT_IN_12 if needed */ |
| 342 | SDEB_I_SERV_ACT_OUT_16 = 13, /* add ...SERV_ACT_OUT_12 if needed */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 343 | SDEB_I_MAINT_IN = 14, |
| 344 | SDEB_I_MAINT_OUT = 15, |
| 345 | SDEB_I_VERIFY = 16, /* 10 only */ |
Douglas Gilbert | 481b5e5 | 2017-12-23 12:48:14 -0500 | [diff] [blame] | 346 | SDEB_I_VARIABLE_LEN = 17, /* READ(32), WRITE(32), WR_SCAT(32) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 347 | SDEB_I_RESERVE = 18, /* 6, 10 */ |
| 348 | SDEB_I_RELEASE = 19, /* 6, 10 */ |
| 349 | SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */ |
| 350 | SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */ |
| 351 | SDEB_I_ATA_PT = 22, /* 12, 16 */ |
| 352 | SDEB_I_SEND_DIAG = 23, |
| 353 | SDEB_I_UNMAP = 24, |
Bart Van Assche | c208556 | 2019-02-08 13:21:27 -0800 | [diff] [blame] | 354 | SDEB_I_WRITE_BUFFER = 25, |
| 355 | SDEB_I_WRITE_SAME = 26, /* 10, 16 */ |
| 356 | SDEB_I_SYNC_CACHE = 27, /* 10, 16 */ |
| 357 | SDEB_I_COMP_WRITE = 28, |
| 358 | SDEB_I_LAST_ELEMENT = 29, /* keep this last (previous + 1) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 359 | }; |
| 360 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 361 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 362 | static const unsigned char opcode_ind_arr[256] = { |
| 363 | /* 0x0; 0x0->0x1f: 6 byte cdbs */ |
| 364 | SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE, |
| 365 | 0, 0, 0, 0, |
| 366 | SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0, |
| 367 | 0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE, |
| 368 | SDEB_I_RELEASE, |
| 369 | 0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG, |
| 370 | SDEB_I_ALLOW_REMOVAL, 0, |
| 371 | /* 0x20; 0x20->0x3f: 10 byte cdbs */ |
| 372 | 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0, |
| 373 | SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY, |
| 374 | 0, 0, 0, 0, 0, SDEB_I_SYNC_CACHE, 0, 0, |
| 375 | 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0, |
| 376 | /* 0x40; 0x40->0x5f: 10 byte cdbs */ |
| 377 | 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0, |
| 378 | 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0, |
Bart Van Assche | c208556 | 2019-02-08 13:21:27 -0800 | [diff] [blame] | 379 | 0, 0, 0, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 380 | SDEB_I_RELEASE, |
| 381 | 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0, |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 382 | /* 0x60; 0x60->0x7d are reserved, 0x7e is "extended cdb" */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 383 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 384 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 385 | 0, SDEB_I_VARIABLE_LEN, |
| 386 | /* 0x80; 0x80->0x9f: 16 byte cdbs */ |
| 387 | 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0, |
| 388 | SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0, 0, 0, 0, 0, |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 389 | 0, SDEB_I_SYNC_CACHE, 0, SDEB_I_WRITE_SAME, 0, 0, 0, 0, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 390 | 0, 0, 0, 0, 0, 0, SDEB_I_SERV_ACT_IN_16, SDEB_I_SERV_ACT_OUT_16, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 391 | /* 0xa0; 0xa0->0xbf: 12 byte cdbs */ |
| 392 | SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN, |
| 393 | SDEB_I_MAINT_OUT, 0, 0, 0, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 394 | SDEB_I_READ, 0 /* SDEB_I_SERV_ACT_OUT_12 */, SDEB_I_WRITE, |
| 395 | 0 /* SDEB_I_SERV_ACT_IN_12 */, 0, 0, 0, 0, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 396 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 397 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 398 | /* 0xc0; 0xc0->0xff: vendor specific */ |
| 399 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 400 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 401 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 402 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 403 | }; |
| 404 | |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 405 | /* |
| 406 | * The following "response" functions return the SCSI mid-level's 4 byte |
| 407 | * tuple-in-an-int. To handle commands with an IMMED bit, for a faster |
| 408 | * command completion, they can mask their return value with |
| 409 | * SDEG_RES_IMMED_MASK . |
| 410 | */ |
| 411 | #define SDEG_RES_IMMED_MASK 0x40000000 |
| 412 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 413 | static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 414 | static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 415 | static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 416 | static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 417 | static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 418 | static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 419 | static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 420 | static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 421 | static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 481b5e5 | 2017-12-23 12:48:14 -0500 | [diff] [blame] | 422 | static int resp_write_scat(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 423 | static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 424 | static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 425 | static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 426 | static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 427 | static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 428 | static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 429 | static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 430 | static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 431 | static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 432 | static int resp_comp_write(struct scsi_cmnd *, struct sdebug_dev_info *); |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 433 | static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 434 | static int resp_sync_cache(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 435 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 436 | /* |
| 437 | * The following are overflow arrays for cdbs that "hit" the same index in |
| 438 | * the opcode_info_arr array. The most time sensitive (or commonly used) cdb |
| 439 | * should be placed in opcode_info_arr[], the others should be placed here. |
| 440 | */ |
| 441 | static const struct opcode_info_t msense_iarr[] = { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 442 | {0, 0x1a, 0, F_D_IN, NULL, NULL, |
| 443 | {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 444 | }; |
| 445 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 446 | static const struct opcode_info_t mselect_iarr[] = { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 447 | {0, 0x15, 0, F_D_OUT, NULL, NULL, |
| 448 | {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 449 | }; |
| 450 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 451 | static const struct opcode_info_t read_iarr[] = { |
| 452 | {0, 0x28, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(10) */ |
Douglas Gilbert | b7e2458 | 2017-12-23 12:48:11 -0500 | [diff] [blame] | 453 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 454 | 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 455 | {0, 0x8, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL, /* READ(6) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 456 | {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 457 | {0, 0xa8, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(12) */ |
Douglas Gilbert | b7e2458 | 2017-12-23 12:48:11 -0500 | [diff] [blame] | 458 | {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 459 | 0xc7, 0, 0, 0, 0} }, |
| 460 | }; |
| 461 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 462 | static const struct opcode_info_t write_iarr[] = { |
| 463 | {0, 0x2a, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(10) */ |
| 464 | NULL, {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, |
| 465 | 0, 0, 0, 0, 0, 0} }, |
| 466 | {0, 0xa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(6) */ |
| 467 | NULL, {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, |
| 468 | 0, 0, 0} }, |
| 469 | {0, 0xaa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(12) */ |
| 470 | NULL, {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 471 | 0xbf, 0xc7, 0, 0, 0, 0} }, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 472 | }; |
| 473 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 474 | static const struct opcode_info_t sa_in_16_iarr[] = { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 475 | {0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL, |
| 476 | {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 477 | 0xff, 0xff, 0xff, 0, 0xc7} }, /* GET LBA STATUS(16) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 478 | }; |
| 479 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 480 | static const struct opcode_info_t vl_iarr[] = { /* VARIABLE LENGTH */ |
| 481 | {0, 0x7f, 0xb, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_dt0, |
Douglas Gilbert | b7e2458 | 2017-12-23 12:48:11 -0500 | [diff] [blame] | 482 | NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0xb, 0xfa, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 483 | 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */ |
Douglas Gilbert | 481b5e5 | 2017-12-23 12:48:14 -0500 | [diff] [blame] | 484 | {0, 0x7f, 0x11, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_scat, |
| 485 | NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x11, 0xf8, |
| 486 | 0, 0xff, 0xff, 0x0, 0x0} }, /* WRITE SCATTERED(32) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 487 | }; |
| 488 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 489 | static const struct opcode_info_t maint_in_iarr[] = { /* MAINT IN */ |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 490 | {0, 0xa3, 0xc, F_SA_LOW | F_D_IN, resp_rsup_opcodes, NULL, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 491 | {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 492 | 0xc7, 0, 0, 0, 0} }, /* REPORT SUPPORTED OPERATION CODES */ |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 493 | {0, 0xa3, 0xd, F_SA_LOW | F_D_IN, resp_rsup_tmfs, NULL, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 494 | {12, 0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 495 | 0, 0} }, /* REPORTED SUPPORTED TASK MANAGEMENT FUNCTIONS */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 496 | }; |
| 497 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 498 | static const struct opcode_info_t write_same_iarr[] = { |
| 499 | {0, 0x93, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, resp_write_same_16, NULL, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 500 | {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 501 | 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* WRITE SAME(16) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 502 | }; |
| 503 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 504 | static const struct opcode_info_t reserve_iarr[] = { |
| 505 | {0, 0x16, 0, F_D_OUT, NULL, NULL, /* RESERVE(6) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 506 | {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 507 | }; |
| 508 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 509 | static const struct opcode_info_t release_iarr[] = { |
| 510 | {0, 0x17, 0, F_D_OUT, NULL, NULL, /* RELEASE(6) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 511 | {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 512 | }; |
| 513 | |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 514 | static const struct opcode_info_t sync_cache_iarr[] = { |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 515 | {0, 0x91, 0, F_SYNC_DELAY | F_M_ACCESS, resp_sync_cache, NULL, |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 516 | {16, 0x6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 517 | 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* SYNC_CACHE (16) */ |
| 518 | }; |
| 519 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 520 | |
| 521 | /* This array is accessed via SDEB_I_* values. Make sure all are mapped, |
| 522 | * plus the terminating elements for logic that scans this table such as |
| 523 | * REPORT SUPPORTED OPERATION CODES. */ |
| 524 | static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEMENT + 1] = { |
| 525 | /* 0 */ |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 526 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* unknown opcodes */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 527 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 528 | {0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL, /* INQUIRY */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 529 | {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 530 | {0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL, |
| 531 | {12, 0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 532 | 0, 0} }, /* REPORT LUNS */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 533 | {0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL, |
| 534 | {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 535 | {0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */ |
| 536 | {6, 0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 537 | /* 5 */ |
| 538 | {ARRAY_SIZE(msense_iarr), 0x5a, 0, F_D_IN, /* MODE SENSE(10) */ |
| 539 | resp_mode_sense, msense_iarr, {10, 0xf8, 0xff, 0xff, 0, 0, 0, |
| 540 | 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 541 | {ARRAY_SIZE(mselect_iarr), 0x55, 0, F_D_OUT, /* MODE SELECT(10) */ |
| 542 | resp_mode_select, mselect_iarr, {10, 0xf1, 0, 0, 0, 0, 0, 0xff, |
| 543 | 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 544 | {0, 0x4d, 0, F_D_IN, resp_log_sense, NULL, /* LOG SENSE */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 545 | {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, |
| 546 | 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 547 | {0, 0x25, 0, F_D_IN, resp_readcap, NULL, /* READ CAPACITY(10) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 548 | {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0, |
| 549 | 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 550 | {ARRAY_SIZE(read_iarr), 0x88, 0, F_D_IN | FF_MEDIA_IO, /* READ(16) */ |
| 551 | resp_read_dt0, read_iarr, {16, 0xfe, 0xff, 0xff, 0xff, 0xff, |
| 552 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} }, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 553 | /* 10 */ |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 554 | {ARRAY_SIZE(write_iarr), 0x8a, 0, F_D_OUT | FF_MEDIA_IO, |
| 555 | resp_write_dt0, write_iarr, /* WRITE(16) */ |
| 556 | {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 557 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} }, |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 558 | {0, 0x1b, 0, F_SSU_DELAY, resp_start_stop, NULL,/* START STOP UNIT */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 559 | {6, 0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 560 | {ARRAY_SIZE(sa_in_16_iarr), 0x9e, 0x10, F_SA_LOW | F_D_IN, |
| 561 | resp_readcap16, sa_in_16_iarr, /* SA_IN(16), READ CAPACITY(16) */ |
| 562 | {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 563 | 0xff, 0xff, 0xff, 0xff, 0x1, 0xc7} }, |
Douglas Gilbert | 481b5e5 | 2017-12-23 12:48:14 -0500 | [diff] [blame] | 564 | {0, 0x9f, 0x12, F_SA_LOW | F_D_OUT | FF_MEDIA_IO, resp_write_scat, |
| 565 | NULL, {16, 0x12, 0xf9, 0x0, 0xff, 0xff, 0, 0, 0xff, 0xff, 0xff, |
| 566 | 0xff, 0xff, 0xff, 0xff, 0xc7} }, /* SA_OUT(16), WRITE SCAT(16) */ |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 567 | {ARRAY_SIZE(maint_in_iarr), 0xa3, 0xa, F_SA_LOW | F_D_IN, |
| 568 | resp_report_tgtpgs, /* MAINT IN, REPORT TARGET PORT GROUPS */ |
| 569 | maint_in_iarr, {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff, |
| 570 | 0xff, 0, 0xc7, 0, 0, 0, 0} }, |
| 571 | /* 15 */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 572 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */ |
| 573 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 574 | {0, 0x2f, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, NULL, NULL, /* VERIFY(10) */ |
Douglas Gilbert | f7f9f26 | 2015-11-22 12:11:28 -0500 | [diff] [blame] | 575 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, |
| 576 | 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 577 | {ARRAY_SIZE(vl_iarr), 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_MEDIA_IO, |
| 578 | resp_read_dt0, vl_iarr, /* VARIABLE LENGTH, READ(32) */ |
| 579 | {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x9, 0xfe, 0, 0xff, 0xff, |
| 580 | 0xff, 0xff} }, |
| 581 | {ARRAY_SIZE(reserve_iarr), 0x56, 0, F_D_OUT, |
| 582 | NULL, reserve_iarr, /* RESERVE(10) <no response function> */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 583 | {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 584 | 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 585 | {ARRAY_SIZE(release_iarr), 0x57, 0, F_D_OUT, |
| 586 | NULL, release_iarr, /* RELEASE(10) <no response function> */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 587 | {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 588 | 0} }, |
| 589 | /* 20 */ |
Douglas Gilbert | f7f9f26 | 2015-11-22 12:11:28 -0500 | [diff] [blame] | 590 | {0, 0x1e, 0, 0, NULL, NULL, /* ALLOW REMOVAL */ |
| 591 | {6, 0, 0, 0, 0x3, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 592 | {0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */ |
| 593 | {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 594 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */ |
| 595 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 596 | {0, 0x1d, F_D_OUT, 0, NULL, NULL, /* SEND DIAGNOSTIC */ |
| 597 | {6, 0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 598 | {0, 0x42, 0, F_D_OUT | FF_MEDIA_IO, resp_unmap, NULL, /* UNMAP */ |
Douglas Gilbert | b7e2458 | 2017-12-23 12:48:11 -0500 | [diff] [blame] | 599 | {10, 0x1, 0, 0, 0, 0, 0x3f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 600 | /* 25 */ |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 601 | {0, 0x3b, 0, F_D_OUT_MAYBE, resp_write_buffer, NULL, |
| 602 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, |
| 603 | 0, 0, 0, 0} }, /* WRITE_BUFFER */ |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 604 | {ARRAY_SIZE(write_same_iarr), 0x41, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, |
| 605 | resp_write_same_10, write_same_iarr, /* WRITE SAME(10) */ |
| 606 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, |
| 607 | 0, 0, 0, 0, 0} }, |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 608 | {ARRAY_SIZE(sync_cache_iarr), 0x35, 0, F_SYNC_DELAY | F_M_ACCESS, |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 609 | resp_sync_cache, sync_cache_iarr, |
Douglas Gilbert | b7e2458 | 2017-12-23 12:48:11 -0500 | [diff] [blame] | 610 | {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0, |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 611 | 0, 0, 0, 0} }, /* SYNC_CACHE (10) */ |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 612 | {0, 0x89, 0, F_D_OUT | FF_MEDIA_IO, resp_comp_write, NULL, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 613 | {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, |
Douglas Gilbert | b7e2458 | 2017-12-23 12:48:11 -0500 | [diff] [blame] | 614 | 0, 0xff, 0x3f, 0xc7} }, /* COMPARE AND WRITE */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 615 | |
Bart Van Assche | c208556 | 2019-02-08 13:21:27 -0800 | [diff] [blame] | 616 | /* 29 */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 617 | {0xff, 0, 0, 0, NULL, NULL, /* terminating element */ |
| 618 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 619 | }; |
| 620 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 621 | static int sdebug_add_host = DEF_NUM_HOST; |
| 622 | static int sdebug_ato = DEF_ATO; |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 623 | static int sdebug_cdb_len = DEF_CDB_LEN; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 624 | static int sdebug_jdelay = DEF_JDELAY; /* if > 0 then unit is jiffies */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 625 | static int sdebug_dev_size_mb = DEF_DEV_SIZE_MB; |
| 626 | static int sdebug_dif = DEF_DIF; |
| 627 | static int sdebug_dix = DEF_DIX; |
| 628 | static int sdebug_dsense = DEF_D_SENSE; |
| 629 | static int sdebug_every_nth = DEF_EVERY_NTH; |
| 630 | static int sdebug_fake_rw = DEF_FAKE_RW; |
| 631 | static unsigned int sdebug_guard = DEF_GUARD; |
| 632 | static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED; |
| 633 | static int sdebug_max_luns = DEF_MAX_LUNS; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 634 | static int sdebug_max_queue = SDEBUG_CANQUEUE; /* per submit queue */ |
Laurence Oberman | d9da891 | 2018-02-03 13:38:35 -0500 | [diff] [blame] | 635 | static unsigned int sdebug_medium_error_start = OPT_MEDIUM_ERR_ADDR; |
| 636 | static int sdebug_medium_error_count = OPT_MEDIUM_ERR_NUM; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 637 | static atomic_t retired_max_queue; /* if > 0 then was prior max_queue */ |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 638 | static int sdebug_ndelay = DEF_NDELAY; /* if > 0 then unit is nanoseconds */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 639 | static int sdebug_no_lun_0 = DEF_NO_LUN_0; |
| 640 | static int sdebug_no_uld; |
| 641 | static int sdebug_num_parts = DEF_NUM_PARTS; |
| 642 | static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */ |
| 643 | static int sdebug_opt_blks = DEF_OPT_BLKS; |
| 644 | static int sdebug_opts = DEF_OPTS; |
| 645 | static int sdebug_physblk_exp = DEF_PHYSBLK_EXP; |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 646 | static int sdebug_opt_xferlen_exp = DEF_OPT_XFERLEN_EXP; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 647 | static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral device type */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 648 | static int sdebug_scsi_level = DEF_SCSI_LEVEL; |
| 649 | static int sdebug_sector_size = DEF_SECTOR_SIZE; |
| 650 | static int sdebug_virtual_gb = DEF_VIRTUAL_GB; |
| 651 | static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO; |
| 652 | static unsigned int sdebug_lbpu = DEF_LBPU; |
| 653 | static unsigned int sdebug_lbpws = DEF_LBPWS; |
| 654 | static unsigned int sdebug_lbpws10 = DEF_LBPWS10; |
| 655 | static unsigned int sdebug_lbprz = DEF_LBPRZ; |
| 656 | static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT; |
| 657 | static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY; |
| 658 | static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS; |
| 659 | static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC; |
| 660 | static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH; |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 661 | static int sdebug_uuid_ctl = DEF_UUID_CTL; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 662 | static bool sdebug_removable = DEF_REMOVABLE; |
| 663 | static bool sdebug_clustering; |
| 664 | static bool sdebug_host_lock = DEF_HOST_LOCK; |
| 665 | static bool sdebug_strict = DEF_STRICT; |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 666 | static bool sdebug_any_injecting_opt; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 667 | static bool sdebug_verbose; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 668 | static bool have_dif_prot; |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 669 | static bool write_since_sync; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 670 | static bool sdebug_statistics = DEF_STATISTICS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 672 | static unsigned int sdebug_store_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | static sector_t sdebug_capacity; /* in sectors */ |
| 674 | |
| 675 | /* old BIOS stuff, kernel may get rid of them but some mode sense pages |
| 676 | may still need them */ |
| 677 | static int sdebug_heads; /* heads per disk */ |
| 678 | static int sdebug_cylinders_per; /* cylinders per surface */ |
| 679 | static int sdebug_sectors_per; /* sectors per cylinder */ |
| 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | static LIST_HEAD(sdebug_host_list); |
| 682 | static DEFINE_SPINLOCK(sdebug_host_list_lock); |
| 683 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 684 | static unsigned char *fake_storep; /* ramdisk storage */ |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 685 | static struct t10_pi_tuple *dif_storep; /* protection info */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 686 | static void *map_storep; /* provisioning map */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 688 | static unsigned long map_size; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 689 | static int num_aborts; |
| 690 | static int num_dev_resets; |
| 691 | static int num_target_resets; |
| 692 | static int num_bus_resets; |
| 693 | static int num_host_resets; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 694 | static int dix_writes; |
| 695 | static int dix_reads; |
| 696 | static int dif_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 698 | static int submit_queues = DEF_SUBMIT_QUEUES; /* > 1 for multi-queue (mq) */ |
| 699 | static struct sdebug_queue *sdebug_q_arr; /* ptr to array of submit queues */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | static DEFINE_RWLOCK(atomic_rw); |
| 702 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 703 | static char sdebug_proc_name[] = MY_NAME; |
| 704 | static const char *my_name = MY_NAME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | static struct bus_type pseudo_lld_bus; |
| 707 | |
| 708 | static struct device_driver sdebug_driverfs_driver = { |
| 709 | .name = sdebug_proc_name, |
| 710 | .bus = &pseudo_lld_bus, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | }; |
| 712 | |
| 713 | static const int check_condition_result = |
| 714 | (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
| 715 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 716 | static const int illegal_condition_result = |
| 717 | (DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION; |
| 718 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 719 | static const int device_qfull_result = |
| 720 | (DID_OK << 16) | (COMMAND_COMPLETE << 8) | SAM_STAT_TASK_SET_FULL; |
| 721 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 722 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 723 | /* Only do the extra work involved in logical block provisioning if one or |
| 724 | * more of the lbpu, lbpws or lbpws10 parameters are given and we are doing |
| 725 | * real reads and writes (i.e. not skipping them for speed). |
| 726 | */ |
| 727 | static inline bool scsi_debug_lbp(void) |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 728 | { |
| 729 | return 0 == sdebug_fake_rw && |
| 730 | (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10); |
| 731 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 732 | |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 733 | static void *fake_store(unsigned long long lba) |
| 734 | { |
| 735 | lba = do_div(lba, sdebug_store_sectors); |
| 736 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 737 | return fake_storep + lba * sdebug_sector_size; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 738 | } |
| 739 | |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 740 | static struct t10_pi_tuple *dif_store(sector_t sector) |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 741 | { |
Arnd Bergmann | 4941311 | 2015-11-20 17:38:28 +0100 | [diff] [blame] | 742 | sector = sector_div(sector, sdebug_store_sectors); |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 743 | |
| 744 | return dif_storep + sector; |
| 745 | } |
| 746 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 747 | static void sdebug_max_tgts_luns(void) |
| 748 | { |
| 749 | struct sdebug_host_info *sdbg_host; |
| 750 | struct Scsi_Host *hpnt; |
| 751 | |
| 752 | spin_lock(&sdebug_host_list_lock); |
| 753 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
| 754 | hpnt = sdbg_host->shost; |
| 755 | if ((hpnt->this_id >= 0) && |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 756 | (sdebug_num_tgts > hpnt->this_id)) |
| 757 | hpnt->max_id = sdebug_num_tgts + 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 758 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 759 | hpnt->max_id = sdebug_num_tgts; |
| 760 | /* sdebug_max_luns; */ |
Tomas Winkler | f2d3fd2 | 2015-07-28 16:54:25 +0300 | [diff] [blame] | 761 | hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 762 | } |
| 763 | spin_unlock(&sdebug_host_list_lock); |
| 764 | } |
| 765 | |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 766 | enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1}; |
| 767 | |
| 768 | /* Set in_bit to -1 to indicate no bit position of invalid field */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 769 | static void mk_sense_invalid_fld(struct scsi_cmnd *scp, |
| 770 | enum sdeb_cmd_data c_d, |
| 771 | int in_byte, int in_bit) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 772 | { |
| 773 | unsigned char *sbuff; |
| 774 | u8 sks[4]; |
| 775 | int sl, asc; |
| 776 | |
| 777 | sbuff = scp->sense_buffer; |
| 778 | if (!sbuff) { |
| 779 | sdev_printk(KERN_ERR, scp->device, |
| 780 | "%s: sense_buffer is NULL\n", __func__); |
| 781 | return; |
| 782 | } |
| 783 | asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST; |
| 784 | memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 785 | scsi_build_sense_buffer(sdebug_dsense, sbuff, ILLEGAL_REQUEST, asc, 0); |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 786 | memset(sks, 0, sizeof(sks)); |
| 787 | sks[0] = 0x80; |
| 788 | if (c_d) |
| 789 | sks[0] |= 0x40; |
| 790 | if (in_bit >= 0) { |
| 791 | sks[0] |= 0x8; |
| 792 | sks[0] |= 0x7 & in_bit; |
| 793 | } |
| 794 | put_unaligned_be16(in_byte, sks + 1); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 795 | if (sdebug_dsense) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 796 | sl = sbuff[7] + 8; |
| 797 | sbuff[7] = sl; |
| 798 | sbuff[sl] = 0x2; |
| 799 | sbuff[sl + 1] = 0x6; |
| 800 | memcpy(sbuff + sl + 4, sks, 3); |
| 801 | } else |
| 802 | memcpy(sbuff + 15, sks, 3); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 803 | if (sdebug_verbose) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 804 | sdev_printk(KERN_INFO, scp->device, "%s: [sense_key,asc,ascq" |
| 805 | "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n", |
| 806 | my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit); |
| 807 | } |
| 808 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 809 | static void mk_sense_buffer(struct scsi_cmnd *scp, int key, int asc, int asq) |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 810 | { |
| 811 | unsigned char *sbuff; |
| 812 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 813 | sbuff = scp->sense_buffer; |
| 814 | if (!sbuff) { |
| 815 | sdev_printk(KERN_ERR, scp->device, |
| 816 | "%s: sense_buffer is NULL\n", __func__); |
| 817 | return; |
| 818 | } |
| 819 | memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 820 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 821 | scsi_build_sense_buffer(sdebug_dsense, sbuff, key, asc, asq); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 822 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 823 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 824 | sdev_printk(KERN_INFO, scp->device, |
| 825 | "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n", |
| 826 | my_name, key, asc, asq); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 827 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 829 | static void mk_sense_invalid_opcode(struct scsi_cmnd *scp) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 830 | { |
| 831 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0); |
| 832 | } |
| 833 | |
Nathan Chancellor | 6f4e626 | 2019-02-07 09:07:20 -0700 | [diff] [blame] | 834 | static int scsi_debug_ioctl(struct scsi_device *dev, unsigned int cmd, |
| 835 | void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 837 | if (sdebug_verbose) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 838 | if (0x1261 == cmd) |
| 839 | sdev_printk(KERN_INFO, dev, |
| 840 | "%s: BLKFLSBUF [0x1261]\n", __func__); |
| 841 | else if (0x5331 == cmd) |
| 842 | sdev_printk(KERN_INFO, dev, |
| 843 | "%s: CDROM_GET_CAPABILITY [0x5331]\n", |
| 844 | __func__); |
| 845 | else |
| 846 | sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n", |
| 847 | __func__, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | } |
| 849 | return -EINVAL; |
| 850 | /* return -ENOTTY; // correct return but upsets fdisk */ |
| 851 | } |
| 852 | |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 853 | static void config_cdb_len(struct scsi_device *sdev) |
| 854 | { |
| 855 | switch (sdebug_cdb_len) { |
| 856 | case 6: /* suggest 6 byte READ, WRITE and MODE SENSE/SELECT */ |
| 857 | sdev->use_10_for_rw = false; |
| 858 | sdev->use_16_for_rw = false; |
| 859 | sdev->use_10_for_ms = false; |
| 860 | break; |
| 861 | case 10: /* suggest 10 byte RWs and 6 byte MODE SENSE/SELECT */ |
| 862 | sdev->use_10_for_rw = true; |
| 863 | sdev->use_16_for_rw = false; |
| 864 | sdev->use_10_for_ms = false; |
| 865 | break; |
| 866 | case 12: /* suggest 10 byte RWs and 10 byte MODE SENSE/SELECT */ |
| 867 | sdev->use_10_for_rw = true; |
| 868 | sdev->use_16_for_rw = false; |
| 869 | sdev->use_10_for_ms = true; |
| 870 | break; |
| 871 | case 16: |
| 872 | sdev->use_10_for_rw = false; |
| 873 | sdev->use_16_for_rw = true; |
| 874 | sdev->use_10_for_ms = true; |
| 875 | break; |
| 876 | case 32: /* No knobs to suggest this so same as 16 for now */ |
| 877 | sdev->use_10_for_rw = false; |
| 878 | sdev->use_16_for_rw = true; |
| 879 | sdev->use_10_for_ms = true; |
| 880 | break; |
| 881 | default: |
| 882 | pr_warn("unexpected cdb_len=%d, force to 10\n", |
| 883 | sdebug_cdb_len); |
| 884 | sdev->use_10_for_rw = true; |
| 885 | sdev->use_16_for_rw = false; |
| 886 | sdev->use_10_for_ms = false; |
| 887 | sdebug_cdb_len = 10; |
| 888 | break; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | static void all_config_cdb_len(void) |
| 893 | { |
| 894 | struct sdebug_host_info *sdbg_host; |
| 895 | struct Scsi_Host *shost; |
| 896 | struct scsi_device *sdev; |
| 897 | |
| 898 | spin_lock(&sdebug_host_list_lock); |
| 899 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
| 900 | shost = sdbg_host->shost; |
| 901 | shost_for_each_device(sdev, shost) { |
| 902 | config_cdb_len(sdev); |
| 903 | } |
| 904 | } |
| 905 | spin_unlock(&sdebug_host_list_lock); |
| 906 | } |
| 907 | |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 908 | static void clear_luns_changed_on_target(struct sdebug_dev_info *devip) |
| 909 | { |
| 910 | struct sdebug_host_info *sdhp; |
| 911 | struct sdebug_dev_info *dp; |
| 912 | |
| 913 | spin_lock(&sdebug_host_list_lock); |
| 914 | list_for_each_entry(sdhp, &sdebug_host_list, host_list) { |
| 915 | list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) { |
| 916 | if ((devip->sdbg_host == dp->sdbg_host) && |
| 917 | (devip->target == dp->target)) |
| 918 | clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm); |
| 919 | } |
| 920 | } |
| 921 | spin_unlock(&sdebug_host_list_lock); |
| 922 | } |
| 923 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 924 | static int make_ua(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 926 | int k; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 927 | |
| 928 | k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS); |
| 929 | if (k != SDEBUG_NUM_UAS) { |
| 930 | const char *cp = NULL; |
| 931 | |
| 932 | switch (k) { |
| 933 | case SDEBUG_UA_POR: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 934 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC, |
| 935 | POWER_ON_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 936 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 937 | cp = "power on reset"; |
| 938 | break; |
| 939 | case SDEBUG_UA_BUS_RESET: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 940 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC, |
| 941 | BUS_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 942 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 943 | cp = "bus reset"; |
| 944 | break; |
| 945 | case SDEBUG_UA_MODE_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 946 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC, |
| 947 | MODE_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 948 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 949 | cp = "mode parameters changed"; |
| 950 | break; |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 951 | case SDEBUG_UA_CAPACITY_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 952 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC, |
| 953 | CAPACITY_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 954 | if (sdebug_verbose) |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 955 | cp = "capacity data changed"; |
Ewan D. Milne | f49accf | 2014-12-04 11:49:25 -0500 | [diff] [blame] | 956 | break; |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 957 | case SDEBUG_UA_MICROCODE_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 958 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 959 | TARGET_CHANGED_ASC, |
| 960 | MICROCODE_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 961 | if (sdebug_verbose) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 962 | cp = "microcode has been changed"; |
| 963 | break; |
| 964 | case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 965 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 966 | TARGET_CHANGED_ASC, |
| 967 | MICROCODE_CHANGED_WO_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 968 | if (sdebug_verbose) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 969 | cp = "microcode has been changed without reset"; |
| 970 | break; |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 971 | case SDEBUG_UA_LUNS_CHANGED: |
| 972 | /* |
| 973 | * SPC-3 behavior is to report a UNIT ATTENTION with |
| 974 | * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN |
| 975 | * on the target, until a REPORT LUNS command is |
| 976 | * received. SPC-4 behavior is to report it only once. |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 977 | * NOTE: sdebug_scsi_level does not use the same |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 978 | * values as struct scsi_device->scsi_level. |
| 979 | */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 980 | if (sdebug_scsi_level >= 6) /* SPC-4 and above */ |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 981 | clear_luns_changed_on_target(devip); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 982 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 983 | TARGET_CHANGED_ASC, |
| 984 | LUNS_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 985 | if (sdebug_verbose) |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 986 | cp = "reported luns data has changed"; |
| 987 | break; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 988 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 989 | pr_warn("unexpected unit attention code=%d\n", k); |
| 990 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 991 | cp = "unknown"; |
| 992 | break; |
| 993 | } |
| 994 | clear_bit(k, devip->uas_bm); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 995 | if (sdebug_verbose) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 996 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 997 | "%s reports: Unit attention: %s\n", |
| 998 | my_name, cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | return check_condition_result; |
| 1000 | } |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 1004 | /* Build SCSI "data-in" buffer. Returns 0 if ok else (DID_ERROR << 16). */ |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 1005 | static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | int arr_len) |
| 1007 | { |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 1008 | int act_len; |
Christoph Hellwig | ae3d56d | 2019-01-29 09:33:07 +0100 | [diff] [blame] | 1009 | struct scsi_data_buffer *sdb = &scp->sdb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 1011 | if (!sdb->length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | return 0; |
Christoph Hellwig | ae3d56d | 2019-01-29 09:33:07 +0100 | [diff] [blame] | 1013 | if (scp->sc_data_direction != DMA_FROM_DEVICE) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1014 | return DID_ERROR << 16; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 1015 | |
| 1016 | act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents, |
| 1017 | arr, arr_len); |
Bart Van Assche | 42d387b | 2019-02-08 13:25:00 -0800 | [diff] [blame^] | 1018 | scsi_set_resid(scp, scsi_bufflen(scp) - act_len); |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 1019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | return 0; |
| 1021 | } |
| 1022 | |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 1023 | /* Partial build of SCSI "data-in" buffer. Returns 0 if ok else |
| 1024 | * (DID_ERROR << 16). Can write to offset in data-in buffer. If multiple |
| 1025 | * calls, not required to write in ascending offset order. Assumes resid |
| 1026 | * set to scsi_bufflen() prior to any calls. |
| 1027 | */ |
| 1028 | static int p_fill_from_dev_buffer(struct scsi_cmnd *scp, const void *arr, |
| 1029 | int arr_len, unsigned int off_dst) |
| 1030 | { |
| 1031 | int act_len, n; |
Christoph Hellwig | ae3d56d | 2019-01-29 09:33:07 +0100 | [diff] [blame] | 1032 | struct scsi_data_buffer *sdb = &scp->sdb; |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 1033 | off_t skip = off_dst; |
| 1034 | |
| 1035 | if (sdb->length <= off_dst) |
| 1036 | return 0; |
Christoph Hellwig | ae3d56d | 2019-01-29 09:33:07 +0100 | [diff] [blame] | 1037 | if (scp->sc_data_direction != DMA_FROM_DEVICE) |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 1038 | return DID_ERROR << 16; |
| 1039 | |
| 1040 | act_len = sg_pcopy_from_buffer(sdb->table.sgl, sdb->table.nents, |
| 1041 | arr, arr_len, skip); |
| 1042 | pr_debug("%s: off_dst=%u, scsi_bufflen=%u, act_len=%u, resid=%d\n", |
Bart Van Assche | 42d387b | 2019-02-08 13:25:00 -0800 | [diff] [blame^] | 1043 | __func__, off_dst, scsi_bufflen(scp), act_len, |
| 1044 | scsi_get_resid(scp)); |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 1045 | n = (int)scsi_bufflen(scp) - ((int)off_dst + act_len); |
Bart Van Assche | 42d387b | 2019-02-08 13:25:00 -0800 | [diff] [blame^] | 1046 | scsi_set_resid(scp, min(scsi_get_resid(scp), n)); |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 1047 | return 0; |
| 1048 | } |
| 1049 | |
| 1050 | /* Fetches from SCSI "data-out" buffer. Returns number of bytes fetched into |
| 1051 | * 'arr' or -1 if error. |
| 1052 | */ |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 1053 | static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr, |
| 1054 | int arr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | { |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 1056 | if (!scsi_bufflen(scp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | return 0; |
Christoph Hellwig | ae3d56d | 2019-01-29 09:33:07 +0100 | [diff] [blame] | 1058 | if (scp->sc_data_direction != DMA_TO_DEVICE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | return -1; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 1060 | |
| 1061 | return scsi_sg_copy_to_buffer(scp, arr, arr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | |
Hannes Reinecke | e5203cf | 2017-10-02 16:26:33 +0200 | [diff] [blame] | 1065 | static char sdebug_inq_vendor_id[9] = "Linux "; |
| 1066 | static char sdebug_inq_product_id[17] = "scsi_debug "; |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 1067 | static char sdebug_inq_product_rev[5] = SDEBUG_VERSION; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1068 | /* Use some locally assigned NAAs for SAS addresses. */ |
| 1069 | static const u64 naa3_comp_a = 0x3222222000000000ULL; |
| 1070 | static const u64 naa3_comp_b = 0x3333333000000000ULL; |
| 1071 | static const u64 naa3_comp_c = 0x3111111000000000ULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1073 | /* Device identification VPD page. Returns number of bytes placed in arr */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1074 | static int inquiry_vpd_83(unsigned char *arr, int port_group_id, |
| 1075 | int target_dev_id, int dev_id_num, |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1076 | const char *dev_id_str, int dev_id_str_len, |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 1077 | const uuid_t *lu_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1079 | int num, port_a; |
| 1080 | char b[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1082 | port_a = target_dev_id + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | /* T10 vendor identifier field format (faked) */ |
| 1084 | arr[0] = 0x2; /* ASCII */ |
| 1085 | arr[1] = 0x1; |
| 1086 | arr[2] = 0x0; |
Hannes Reinecke | e5203cf | 2017-10-02 16:26:33 +0200 | [diff] [blame] | 1087 | memcpy(&arr[4], sdebug_inq_vendor_id, 8); |
| 1088 | memcpy(&arr[12], sdebug_inq_product_id, 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | memcpy(&arr[28], dev_id_str, dev_id_str_len); |
| 1090 | num = 8 + 16 + dev_id_str_len; |
| 1091 | arr[3] = num; |
| 1092 | num += 4; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1093 | if (dev_id_num >= 0) { |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1094 | if (sdebug_uuid_ctl) { |
| 1095 | /* Locally assigned UUID */ |
| 1096 | arr[num++] = 0x1; /* binary (not necessarily sas) */ |
| 1097 | arr[num++] = 0xa; /* PIV=0, lu, naa */ |
| 1098 | arr[num++] = 0x0; |
| 1099 | arr[num++] = 0x12; |
| 1100 | arr[num++] = 0x10; /* uuid type=1, locally assigned */ |
| 1101 | arr[num++] = 0x0; |
| 1102 | memcpy(arr + num, lu_name, 16); |
| 1103 | num += 16; |
| 1104 | } else { |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1105 | /* NAA-3, Logical unit identifier (binary) */ |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1106 | arr[num++] = 0x1; /* binary (not necessarily sas) */ |
| 1107 | arr[num++] = 0x3; /* PIV=0, lu, naa */ |
| 1108 | arr[num++] = 0x0; |
| 1109 | arr[num++] = 0x8; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1110 | put_unaligned_be64(naa3_comp_b + dev_id_num, arr + num); |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1111 | num += 8; |
| 1112 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1113 | /* Target relative port number */ |
| 1114 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1115 | arr[num++] = 0x94; /* PIV=1, target port, rel port */ |
| 1116 | arr[num++] = 0x0; /* reserved */ |
| 1117 | arr[num++] = 0x4; /* length */ |
| 1118 | arr[num++] = 0x0; /* reserved */ |
| 1119 | arr[num++] = 0x0; /* reserved */ |
| 1120 | arr[num++] = 0x0; |
| 1121 | arr[num++] = 0x1; /* relative port A */ |
| 1122 | } |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1123 | /* NAA-3, Target port identifier */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1124 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1125 | arr[num++] = 0x93; /* piv=1, target port, naa */ |
| 1126 | arr[num++] = 0x0; |
| 1127 | arr[num++] = 0x8; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1128 | put_unaligned_be64(naa3_comp_a + port_a, arr + num); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1129 | num += 8; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1130 | /* NAA-3, Target port group identifier */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1131 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1132 | arr[num++] = 0x95; /* piv=1, target port group id */ |
| 1133 | arr[num++] = 0x0; |
| 1134 | arr[num++] = 0x4; |
| 1135 | arr[num++] = 0; |
| 1136 | arr[num++] = 0; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1137 | put_unaligned_be16(port_group_id, arr + num); |
| 1138 | num += 2; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1139 | /* NAA-3, Target device identifier */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1140 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1141 | arr[num++] = 0xa3; /* piv=1, target device, naa */ |
| 1142 | arr[num++] = 0x0; |
| 1143 | arr[num++] = 0x8; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1144 | put_unaligned_be64(naa3_comp_a + target_dev_id, arr + num); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1145 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1146 | /* SCSI name string: Target device identifier */ |
| 1147 | arr[num++] = 0x63; /* proto=sas, UTF-8 */ |
| 1148 | arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */ |
| 1149 | arr[num++] = 0x0; |
| 1150 | arr[num++] = 24; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1151 | memcpy(arr + num, "naa.32222220", 12); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1152 | num += 12; |
| 1153 | snprintf(b, sizeof(b), "%08X", target_dev_id); |
| 1154 | memcpy(arr + num, b, 8); |
| 1155 | num += 8; |
| 1156 | memset(arr + num, 0, 4); |
| 1157 | num += 4; |
| 1158 | return num; |
| 1159 | } |
| 1160 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1161 | static unsigned char vpd84_data[] = { |
| 1162 | /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0, |
| 1163 | 0x22,0x22,0x22,0x0,0xbb,0x1, |
| 1164 | 0x22,0x22,0x22,0x0,0xbb,0x2, |
| 1165 | }; |
| 1166 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1167 | /* Software interface identification VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1168 | static int inquiry_vpd_84(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1169 | { |
| 1170 | memcpy(arr, vpd84_data, sizeof(vpd84_data)); |
| 1171 | return sizeof(vpd84_data); |
| 1172 | } |
| 1173 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1174 | /* Management network addresses VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1175 | static int inquiry_vpd_85(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1176 | { |
| 1177 | int num = 0; |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 1178 | const char *na1 = "https://www.kernel.org/config"; |
| 1179 | const char *na2 = "http://www.kernel.org/log"; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1180 | int plen, olen; |
| 1181 | |
| 1182 | arr[num++] = 0x1; /* lu, storage config */ |
| 1183 | arr[num++] = 0x0; /* reserved */ |
| 1184 | arr[num++] = 0x0; |
| 1185 | olen = strlen(na1); |
| 1186 | plen = olen + 1; |
| 1187 | if (plen % 4) |
| 1188 | plen = ((plen / 4) + 1) * 4; |
| 1189 | arr[num++] = plen; /* length, null termianted, padded */ |
| 1190 | memcpy(arr + num, na1, olen); |
| 1191 | memset(arr + num + olen, 0, plen - olen); |
| 1192 | num += plen; |
| 1193 | |
| 1194 | arr[num++] = 0x4; /* lu, logging */ |
| 1195 | arr[num++] = 0x0; /* reserved */ |
| 1196 | arr[num++] = 0x0; |
| 1197 | olen = strlen(na2); |
| 1198 | plen = olen + 1; |
| 1199 | if (plen % 4) |
| 1200 | plen = ((plen / 4) + 1) * 4; |
| 1201 | arr[num++] = plen; /* length, null terminated, padded */ |
| 1202 | memcpy(arr + num, na2, olen); |
| 1203 | memset(arr + num + olen, 0, plen - olen); |
| 1204 | num += plen; |
| 1205 | |
| 1206 | return num; |
| 1207 | } |
| 1208 | |
| 1209 | /* SCSI ports VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1210 | static int inquiry_vpd_88(unsigned char *arr, int target_dev_id) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1211 | { |
| 1212 | int num = 0; |
| 1213 | int port_a, port_b; |
| 1214 | |
| 1215 | port_a = target_dev_id + 1; |
| 1216 | port_b = port_a + 1; |
| 1217 | arr[num++] = 0x0; /* reserved */ |
| 1218 | arr[num++] = 0x0; /* reserved */ |
| 1219 | arr[num++] = 0x0; |
| 1220 | arr[num++] = 0x1; /* relative port 1 (primary) */ |
| 1221 | memset(arr + num, 0, 6); |
| 1222 | num += 6; |
| 1223 | arr[num++] = 0x0; |
| 1224 | arr[num++] = 12; /* length tp descriptor */ |
| 1225 | /* naa-5 target port identifier (A) */ |
| 1226 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1227 | arr[num++] = 0x93; /* PIV=1, target port, NAA */ |
| 1228 | arr[num++] = 0x0; /* reserved */ |
| 1229 | arr[num++] = 0x8; /* length */ |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1230 | put_unaligned_be64(naa3_comp_a + port_a, arr + num); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1231 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1232 | arr[num++] = 0x0; /* reserved */ |
| 1233 | arr[num++] = 0x0; /* reserved */ |
| 1234 | arr[num++] = 0x0; |
| 1235 | arr[num++] = 0x2; /* relative port 2 (secondary) */ |
| 1236 | memset(arr + num, 0, 6); |
| 1237 | num += 6; |
| 1238 | arr[num++] = 0x0; |
| 1239 | arr[num++] = 12; /* length tp descriptor */ |
| 1240 | /* naa-5 target port identifier (B) */ |
| 1241 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1242 | arr[num++] = 0x93; /* PIV=1, target port, NAA */ |
| 1243 | arr[num++] = 0x0; /* reserved */ |
| 1244 | arr[num++] = 0x8; /* length */ |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1245 | put_unaligned_be64(naa3_comp_a + port_b, arr + num); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1246 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1247 | |
| 1248 | return num; |
| 1249 | } |
| 1250 | |
| 1251 | |
| 1252 | static unsigned char vpd89_data[] = { |
| 1253 | /* from 4th byte */ 0,0,0,0, |
| 1254 | 'l','i','n','u','x',' ',' ',' ', |
| 1255 | 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ', |
| 1256 | '1','2','3','4', |
| 1257 | 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0, |
| 1258 | 0xec,0,0,0, |
| 1259 | 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0, |
| 1260 | 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20, |
| 1261 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33, |
| 1262 | 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31, |
| 1263 | 0x53,0x41, |
| 1264 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 1265 | 0x20,0x20, |
| 1266 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 1267 | 0x10,0x80, |
| 1268 | 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0, |
| 1269 | 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0, |
| 1270 | 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0, |
| 1271 | 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0, |
| 1272 | 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40, |
| 1273 | 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0, |
| 1274 | 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0, |
| 1275 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1276 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1277 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1278 | 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42, |
| 1279 | 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8, |
| 1280 | 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe, |
| 1281 | 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0, |
| 1282 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1283 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1284 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1285 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1286 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1287 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1288 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1289 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1290 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1291 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1292 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1293 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51, |
| 1294 | }; |
| 1295 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1296 | /* ATA Information VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1297 | static int inquiry_vpd_89(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1298 | { |
| 1299 | memcpy(arr, vpd89_data, sizeof(vpd89_data)); |
| 1300 | return sizeof(vpd89_data); |
| 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | static unsigned char vpdb0_data[] = { |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1305 | /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64, |
| 1306 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1307 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1308 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1309 | }; |
| 1310 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1311 | /* Block limits VPD page (SBC-3) */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1312 | static int inquiry_vpd_b0(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1313 | { |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 1314 | unsigned int gran; |
| 1315 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1316 | memcpy(arr, vpdb0_data, sizeof(vpdb0_data)); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1317 | |
| 1318 | /* Optimal transfer length granularity */ |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 1319 | if (sdebug_opt_xferlen_exp != 0 && |
| 1320 | sdebug_physblk_exp < sdebug_opt_xferlen_exp) |
| 1321 | gran = 1 << sdebug_opt_xferlen_exp; |
| 1322 | else |
| 1323 | gran = 1 << sdebug_physblk_exp; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1324 | put_unaligned_be16(gran, arr + 2); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1325 | |
| 1326 | /* Maximum Transfer Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1327 | if (sdebug_store_sectors > 0x400) |
| 1328 | put_unaligned_be32(sdebug_store_sectors, arr + 4); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1329 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1330 | /* Optimal Transfer Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1331 | put_unaligned_be32(sdebug_opt_blks, &arr[8]); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1332 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1333 | if (sdebug_lbpu) { |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1334 | /* Maximum Unmap LBA Count */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1335 | put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1336 | |
| 1337 | /* Maximum Unmap Block Descriptor Count */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1338 | put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1339 | } |
| 1340 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1341 | /* Unmap Granularity Alignment */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1342 | if (sdebug_unmap_alignment) { |
| 1343 | put_unaligned_be32(sdebug_unmap_alignment, &arr[28]); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1344 | arr[28] |= 0x80; /* UGAVALID */ |
| 1345 | } |
| 1346 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1347 | /* Optimal Unmap Granularity */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1348 | put_unaligned_be32(sdebug_unmap_granularity, &arr[24]); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1349 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1350 | /* Maximum WRITE SAME Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1351 | put_unaligned_be64(sdebug_write_same_length, &arr[32]); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1352 | |
| 1353 | return 0x3c; /* Mandatory page length for Logical Block Provisioning */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1354 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1355 | return sizeof(vpdb0_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1358 | /* Block device characteristics VPD page (SBC-3) */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1359 | static int inquiry_vpd_b1(unsigned char *arr) |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1360 | { |
| 1361 | memset(arr, 0, 0x3c); |
| 1362 | arr[0] = 0; |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1363 | arr[1] = 1; /* non rotating medium (e.g. solid state) */ |
| 1364 | arr[2] = 0; |
| 1365 | arr[3] = 5; /* less than 1.8" */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1366 | |
| 1367 | return 0x3c; |
| 1368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1370 | /* Logical block provisioning VPD page (SBC-4) */ |
| 1371 | static int inquiry_vpd_b2(unsigned char *arr) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1372 | { |
Martin K. Petersen | 3f0bc3b | 2012-03-08 10:48:29 -0500 | [diff] [blame] | 1373 | memset(arr, 0, 0x4); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1374 | arr[0] = 0; /* threshold exponent */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1375 | if (sdebug_lbpu) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1376 | arr[1] = 1 << 7; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1377 | if (sdebug_lbpws) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1378 | arr[1] |= 1 << 6; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1379 | if (sdebug_lbpws10) |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1380 | arr[1] |= 1 << 5; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1381 | if (sdebug_lbprz && scsi_debug_lbp()) |
| 1382 | arr[1] |= (sdebug_lbprz & 0x7) << 2; /* sbc4r07 and later */ |
| 1383 | /* anc_sup=0; dp=0 (no provisioning group descriptor) */ |
| 1384 | /* minimum_percentage=0; provisioning_type=0 (unknown) */ |
| 1385 | /* threshold_percentage=0 */ |
Martin K. Petersen | 3f0bc3b | 2012-03-08 10:48:29 -0500 | [diff] [blame] | 1386 | return 0x4; |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1387 | } |
| 1388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | #define SDEBUG_LONG_INQ_SZ 96 |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1390 | #define SDEBUG_MAX_INQ_ARR_SZ 584 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1392 | static int resp_inquiry(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | { |
| 1394 | unsigned char pq_pdt; |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 1395 | unsigned char *arr; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1396 | unsigned char *cmd = scp->cmnd; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1397 | int alloc_len, n, ret; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1398 | bool have_wlun, is_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1400 | alloc_len = get_unaligned_be16(cmd + 3); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 1401 | arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC); |
| 1402 | if (! arr) |
| 1403 | return DID_REQUEUE << 16; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1404 | is_disk = (sdebug_ptype == TYPE_DISK); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1405 | have_wlun = scsi_is_wlun(scp->device->lun); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1406 | if (have_wlun) |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1407 | pq_pdt = TYPE_WLUN; /* present, wlun */ |
| 1408 | else if (sdebug_no_lun_0 && (devip->lun == SDEBUG_LUN_0_VAL)) |
| 1409 | pq_pdt = 0x7f; /* not present, PQ=3, PDT=0x1f */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1410 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1411 | pq_pdt = (sdebug_ptype & 0x1f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | arr[0] = pq_pdt; |
| 1413 | if (0x2 & cmd[1]) { /* CMDDT bit set */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1414 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1415 | kfree(arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | return check_condition_result; |
| 1417 | } else if (0x1 & cmd[1]) { /* EVPD bit set */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1418 | int lu_id_num, port_group_id, target_dev_id, len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1419 | char lu_id_str[6]; |
| 1420 | int host_no = devip->sdbg_host->shost->host_no; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1422 | port_group_id = (((host_no + 1) & 0x7f) << 8) + |
| 1423 | (devip->channel & 0x7f); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1424 | if (sdebug_vpd_use_hostno == 0) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1425 | host_no = 0; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1426 | lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) + |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1427 | (devip->target * 1000) + devip->lun); |
| 1428 | target_dev_id = ((host_no + 1) * 2000) + |
| 1429 | (devip->target * 1000) - 3; |
| 1430 | len = scnprintf(lu_id_str, 6, "%d", lu_id_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | if (0 == cmd[2]) { /* supported vital product data pages */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1432 | arr[1] = cmd[2]; /*sanity */ |
| 1433 | n = 4; |
| 1434 | arr[n++] = 0x0; /* this page */ |
| 1435 | arr[n++] = 0x80; /* unit serial number */ |
| 1436 | arr[n++] = 0x83; /* device identification */ |
| 1437 | arr[n++] = 0x84; /* software interface ident. */ |
| 1438 | arr[n++] = 0x85; /* management network addresses */ |
| 1439 | arr[n++] = 0x86; /* extended inquiry */ |
| 1440 | arr[n++] = 0x87; /* mode page policy */ |
| 1441 | arr[n++] = 0x88; /* SCSI ports */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1442 | if (is_disk) { /* SBC only */ |
| 1443 | arr[n++] = 0x89; /* ATA information */ |
| 1444 | arr[n++] = 0xb0; /* Block limits */ |
| 1445 | arr[n++] = 0xb1; /* Block characteristics */ |
| 1446 | arr[n++] = 0xb2; /* Logical Block Prov */ |
| 1447 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1448 | arr[3] = n - 4; /* number of supported VPD pages */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | } else if (0x80 == cmd[2]) { /* unit serial number */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1450 | arr[1] = cmd[2]; /*sanity */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | arr[3] = len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1452 | memcpy(&arr[4], lu_id_str, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | } else if (0x83 == cmd[2]) { /* device identification */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1454 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1455 | arr[3] = inquiry_vpd_83(&arr[4], port_group_id, |
| 1456 | target_dev_id, lu_id_num, |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1457 | lu_id_str, len, |
| 1458 | &devip->lu_name); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1459 | } else if (0x84 == cmd[2]) { /* Software interface ident. */ |
| 1460 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1461 | arr[3] = inquiry_vpd_84(&arr[4]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1462 | } else if (0x85 == cmd[2]) { /* Management network addresses */ |
| 1463 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1464 | arr[3] = inquiry_vpd_85(&arr[4]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1465 | } else if (0x86 == cmd[2]) { /* extended inquiry */ |
| 1466 | arr[1] = cmd[2]; /*sanity */ |
| 1467 | arr[3] = 0x3c; /* number of following entries */ |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 1468 | if (sdebug_dif == T10_PI_TYPE3_PROTECTION) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1469 | arr[4] = 0x4; /* SPT: GRD_CHK:1 */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1470 | else if (have_dif_prot) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1471 | arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */ |
| 1472 | else |
| 1473 | arr[4] = 0x0; /* no protection stuff */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1474 | arr[5] = 0x7; /* head of q, ordered + simple q's */ |
| 1475 | } else if (0x87 == cmd[2]) { /* mode page policy */ |
| 1476 | arr[1] = cmd[2]; /*sanity */ |
| 1477 | arr[3] = 0x8; /* number of following entries */ |
| 1478 | arr[4] = 0x2; /* disconnect-reconnect mp */ |
| 1479 | arr[6] = 0x80; /* mlus, shared */ |
| 1480 | arr[8] = 0x18; /* protocol specific lu */ |
| 1481 | arr[10] = 0x82; /* mlus, per initiator port */ |
| 1482 | } else if (0x88 == cmd[2]) { /* SCSI Ports */ |
| 1483 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1484 | arr[3] = inquiry_vpd_88(&arr[4], target_dev_id); |
| 1485 | } else if (is_disk && 0x89 == cmd[2]) { /* ATA information */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1486 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1487 | n = inquiry_vpd_89(&arr[4]); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1488 | put_unaligned_be16(n, arr + 2); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1489 | } else if (is_disk && 0xb0 == cmd[2]) { /* Block limits */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1490 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1491 | arr[3] = inquiry_vpd_b0(&arr[4]); |
| 1492 | } else if (is_disk && 0xb1 == cmd[2]) { /* Block char. */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1493 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1494 | arr[3] = inquiry_vpd_b1(&arr[4]); |
| 1495 | } else if (is_disk && 0xb2 == cmd[2]) { /* LB Prov. */ |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1496 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1497 | arr[3] = inquiry_vpd_b2(&arr[4]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1499 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1500 | kfree(arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | return check_condition_result; |
| 1502 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1503 | len = min(get_unaligned_be16(arr + 2) + 4, alloc_len); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1504 | ret = fill_from_dev_buffer(scp, arr, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1505 | min(len, SDEBUG_MAX_INQ_ARR_SZ)); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1506 | kfree(arr); |
| 1507 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | } |
| 1509 | /* drops through here for a standard inquiry */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1510 | arr[1] = sdebug_removable ? 0x80 : 0; /* Removable disk */ |
| 1511 | arr[2] = sdebug_scsi_level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | arr[3] = 2; /* response_data_format==2 */ |
| 1513 | arr[4] = SDEBUG_LONG_INQ_SZ - 5; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 1514 | arr[5] = (int)have_dif_prot; /* PROTECT bit */ |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1515 | if (sdebug_vpd_use_hostno == 0) |
Martin K. Petersen | 70bdf20 | 2017-05-19 12:39:36 -0400 | [diff] [blame] | 1516 | arr[5] |= 0x10; /* claim: implicit TPGS */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1517 | arr[6] = 0x10; /* claim: MultiP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1519 | arr[7] = 0xa; /* claim: LINKED + CMDQUE */ |
Hannes Reinecke | e5203cf | 2017-10-02 16:26:33 +0200 | [diff] [blame] | 1520 | memcpy(&arr[8], sdebug_inq_vendor_id, 8); |
| 1521 | memcpy(&arr[16], sdebug_inq_product_id, 16); |
| 1522 | memcpy(&arr[32], sdebug_inq_product_rev, 4); |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 1523 | /* Use Vendor Specific area to place driver date in ASCII hex */ |
| 1524 | memcpy(&arr[36], sdebug_version_date, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | /* version descriptors (2 bytes each) follow */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1526 | put_unaligned_be16(0xc0, arr + 58); /* SAM-6 no version claimed */ |
| 1527 | put_unaligned_be16(0x5c0, arr + 60); /* SPC-5 no version claimed */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1528 | n = 62; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1529 | if (is_disk) { /* SBC-4 no version claimed */ |
| 1530 | put_unaligned_be16(0x600, arr + n); |
| 1531 | n += 2; |
| 1532 | } else if (sdebug_ptype == TYPE_TAPE) { /* SSC-4 rev 3 */ |
| 1533 | put_unaligned_be16(0x525, arr + n); |
| 1534 | n += 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | } |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1536 | put_unaligned_be16(0x2100, arr + n); /* SPL-4 no version claimed */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1537 | ret = fill_from_dev_buffer(scp, arr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | min(alloc_len, SDEBUG_LONG_INQ_SZ)); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1539 | kfree(arr); |
| 1540 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1543 | static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, |
| 1544 | 0, 0, 0x0, 0x0}; |
| 1545 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 1546 | static int resp_requests(struct scsi_cmnd *scp, |
| 1547 | struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | { |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 1549 | unsigned char *sbuff; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1550 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1551 | unsigned char arr[SCSI_SENSE_BUFFERSIZE]; |
Tomas Winkler | 2492fc0 | 2015-07-28 16:54:26 +0300 | [diff] [blame] | 1552 | bool dsense; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | int len = 18; |
| 1554 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1555 | memset(arr, 0, sizeof(arr)); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1556 | dsense = !!(cmd[1] & 1); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1557 | sbuff = scp->sense_buffer; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1558 | if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1559 | if (dsense) { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1560 | arr[0] = 0x72; |
| 1561 | arr[1] = 0x0; /* NO_SENSE in sense_key */ |
| 1562 | arr[2] = THRESHOLD_EXCEEDED; |
| 1563 | arr[3] = 0xff; /* TEST set and MRIE==6 */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1564 | len = 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1565 | } else { |
| 1566 | arr[0] = 0x70; |
| 1567 | arr[2] = 0x0; /* NO_SENSE in sense_key */ |
| 1568 | arr[7] = 0xa; /* 18 byte sense buffer */ |
| 1569 | arr[12] = THRESHOLD_EXCEEDED; |
| 1570 | arr[13] = 0xff; /* TEST set and MRIE==6 */ |
| 1571 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1572 | } else { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1573 | memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1574 | if (arr[0] >= 0x70 && dsense == sdebug_dsense) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1575 | ; /* have sense and formats match */ |
| 1576 | else if (arr[0] <= 0x70) { |
| 1577 | if (dsense) { |
| 1578 | memset(arr, 0, 8); |
| 1579 | arr[0] = 0x72; |
| 1580 | len = 8; |
| 1581 | } else { |
| 1582 | memset(arr, 0, 18); |
| 1583 | arr[0] = 0x70; |
| 1584 | arr[7] = 0xa; |
| 1585 | } |
| 1586 | } else if (dsense) { |
| 1587 | memset(arr, 0, 8); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1588 | arr[0] = 0x72; |
| 1589 | arr[1] = sbuff[2]; /* sense key */ |
| 1590 | arr[2] = sbuff[12]; /* asc */ |
| 1591 | arr[3] = sbuff[13]; /* ascq */ |
| 1592 | len = 8; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1593 | } else { |
| 1594 | memset(arr, 0, 18); |
| 1595 | arr[0] = 0x70; |
| 1596 | arr[2] = sbuff[1]; |
| 1597 | arr[7] = 0xa; |
| 1598 | arr[12] = sbuff[1]; |
| 1599 | arr[13] = sbuff[3]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1600 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1601 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1602 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1603 | mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | return fill_from_dev_buffer(scp, arr, len); |
| 1605 | } |
| 1606 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 1607 | static int resp_start_stop(struct scsi_cmnd *scp, |
| 1608 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1609 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1610 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 1611 | int power_cond, stop; |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 1612 | bool changing; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1613 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1614 | power_cond = (cmd[4] & 0xf0) >> 4; |
| 1615 | if (power_cond) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1616 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1617 | return check_condition_result; |
| 1618 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 1619 | stop = !(cmd[4] & 1); |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 1620 | changing = atomic_read(&devip->stopped) == !stop; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 1621 | atomic_xchg(&devip->stopped, stop); |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 1622 | if (!changing || cmd[1] & 0x1) /* state unchanged or IMMED set */ |
| 1623 | return SDEG_RES_IMMED_MASK; |
| 1624 | else |
| 1625 | return 0; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1626 | } |
| 1627 | |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1628 | static sector_t get_sdebug_capacity(void) |
| 1629 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1630 | static const unsigned int gibibyte = 1073741824; |
| 1631 | |
| 1632 | if (sdebug_virtual_gb > 0) |
| 1633 | return (sector_t)sdebug_virtual_gb * |
| 1634 | (gibibyte / sdebug_sector_size); |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1635 | else |
| 1636 | return sdebug_store_sectors; |
| 1637 | } |
| 1638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | #define SDEBUG_READCAP_ARR_SZ 8 |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 1640 | static int resp_readcap(struct scsi_cmnd *scp, |
| 1641 | struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | { |
| 1643 | unsigned char arr[SDEBUG_READCAP_ARR_SZ]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1644 | unsigned int capac; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1646 | /* following just in case virtual_gb changed */ |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1647 | sdebug_capacity = get_sdebug_capacity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | memset(arr, 0, SDEBUG_READCAP_ARR_SZ); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1649 | if (sdebug_capacity < 0xffffffff) { |
| 1650 | capac = (unsigned int)sdebug_capacity - 1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1651 | put_unaligned_be32(capac, arr + 0); |
| 1652 | } else |
| 1653 | put_unaligned_be32(0xffffffff, arr + 0); |
| 1654 | put_unaligned_be16(sdebug_sector_size, arr + 6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ); |
| 1656 | } |
| 1657 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1658 | #define SDEBUG_READCAP16_ARR_SZ 32 |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 1659 | static int resp_readcap16(struct scsi_cmnd *scp, |
| 1660 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1661 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1662 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1663 | unsigned char arr[SDEBUG_READCAP16_ARR_SZ]; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1664 | int alloc_len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1665 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1666 | alloc_len = get_unaligned_be32(cmd + 10); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1667 | /* following just in case virtual_gb changed */ |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1668 | sdebug_capacity = get_sdebug_capacity(); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1669 | memset(arr, 0, SDEBUG_READCAP16_ARR_SZ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1670 | put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0); |
| 1671 | put_unaligned_be32(sdebug_sector_size, arr + 8); |
| 1672 | arr[13] = sdebug_physblk_exp & 0xf; |
| 1673 | arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1674 | |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1675 | if (scsi_debug_lbp()) { |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1676 | arr[14] |= 0x80; /* LBPME */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1677 | /* from sbc4r07, this LBPRZ field is 1 bit, but the LBPRZ in |
| 1678 | * the LB Provisioning VPD page is 3 bits. Note that lbprz=2 |
| 1679 | * in the wider field maps to 0 in this field. |
| 1680 | */ |
| 1681 | if (sdebug_lbprz & 1) /* precisely what the draft requires */ |
| 1682 | arr[14] |= 0x40; |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1683 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1684 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1685 | arr[15] = sdebug_lowest_aligned & 0xff; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1686 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1687 | if (have_dif_prot) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1688 | arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */ |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1689 | arr[12] |= 1; /* PROT_EN */ |
| 1690 | } |
| 1691 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1692 | return fill_from_dev_buffer(scp, arr, |
| 1693 | min(alloc_len, SDEBUG_READCAP16_ARR_SZ)); |
| 1694 | } |
| 1695 | |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1696 | #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412 |
| 1697 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 1698 | static int resp_report_tgtpgs(struct scsi_cmnd *scp, |
| 1699 | struct sdebug_dev_info *devip) |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1700 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1701 | unsigned char *cmd = scp->cmnd; |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 1702 | unsigned char *arr; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1703 | int host_no = devip->sdbg_host->shost->host_no; |
| 1704 | int n, ret, alen, rlen; |
| 1705 | int port_group_a, port_group_b, port_a, port_b; |
| 1706 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1707 | alen = get_unaligned_be32(cmd + 6); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 1708 | arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC); |
| 1709 | if (! arr) |
| 1710 | return DID_REQUEUE << 16; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1711 | /* |
| 1712 | * EVPD page 0x88 states we have two ports, one |
| 1713 | * real and a fake port with no device connected. |
| 1714 | * So we create two port groups with one port each |
| 1715 | * and set the group with port B to unavailable. |
| 1716 | */ |
| 1717 | port_a = 0x1; /* relative port A */ |
| 1718 | port_b = 0x2; /* relative port B */ |
| 1719 | port_group_a = (((host_no + 1) & 0x7f) << 8) + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1720 | (devip->channel & 0x7f); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1721 | port_group_b = (((host_no + 1) & 0x7f) << 8) + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1722 | (devip->channel & 0x7f) + 0x80; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1723 | |
| 1724 | /* |
| 1725 | * The asymmetric access state is cycled according to the host_id. |
| 1726 | */ |
| 1727 | n = 4; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1728 | if (sdebug_vpd_use_hostno == 0) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1729 | arr[n++] = host_no % 3; /* Asymm access state */ |
| 1730 | arr[n++] = 0x0F; /* claim: all states are supported */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1731 | } else { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1732 | arr[n++] = 0x0; /* Active/Optimized path */ |
| 1733 | arr[n++] = 0x01; /* only support active/optimized paths */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1734 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1735 | put_unaligned_be16(port_group_a, arr + n); |
| 1736 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1737 | arr[n++] = 0; /* Reserved */ |
| 1738 | arr[n++] = 0; /* Status code */ |
| 1739 | arr[n++] = 0; /* Vendor unique */ |
| 1740 | arr[n++] = 0x1; /* One port per group */ |
| 1741 | arr[n++] = 0; /* Reserved */ |
| 1742 | arr[n++] = 0; /* Reserved */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1743 | put_unaligned_be16(port_a, arr + n); |
| 1744 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1745 | arr[n++] = 3; /* Port unavailable */ |
| 1746 | arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1747 | put_unaligned_be16(port_group_b, arr + n); |
| 1748 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1749 | arr[n++] = 0; /* Reserved */ |
| 1750 | arr[n++] = 0; /* Status code */ |
| 1751 | arr[n++] = 0; /* Vendor unique */ |
| 1752 | arr[n++] = 0x1; /* One port per group */ |
| 1753 | arr[n++] = 0; /* Reserved */ |
| 1754 | arr[n++] = 0; /* Reserved */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1755 | put_unaligned_be16(port_b, arr + n); |
| 1756 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1757 | |
| 1758 | rlen = n - 4; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1759 | put_unaligned_be32(rlen, arr + 0); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1760 | |
| 1761 | /* |
| 1762 | * Return the smallest value of either |
| 1763 | * - The allocated length |
| 1764 | * - The constructed command length |
| 1765 | * - The maximum array size |
| 1766 | */ |
| 1767 | rlen = min(alen,n); |
| 1768 | ret = fill_from_dev_buffer(scp, arr, |
| 1769 | min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ)); |
| 1770 | kfree(arr); |
| 1771 | return ret; |
| 1772 | } |
| 1773 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1774 | static int resp_rsup_opcodes(struct scsi_cmnd *scp, |
| 1775 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1776 | { |
| 1777 | bool rctd; |
| 1778 | u8 reporting_opts, req_opcode, sdeb_i, supp; |
| 1779 | u16 req_sa, u; |
| 1780 | u32 alloc_len, a_len; |
| 1781 | int k, offset, len, errsts, count, bump, na; |
| 1782 | const struct opcode_info_t *oip; |
| 1783 | const struct opcode_info_t *r_oip; |
| 1784 | u8 *arr; |
| 1785 | u8 *cmd = scp->cmnd; |
| 1786 | |
| 1787 | rctd = !!(cmd[2] & 0x80); |
| 1788 | reporting_opts = cmd[2] & 0x7; |
| 1789 | req_opcode = cmd[3]; |
| 1790 | req_sa = get_unaligned_be16(cmd + 4); |
| 1791 | alloc_len = get_unaligned_be32(cmd + 6); |
Colin Ian King | 6d310df | 2015-01-22 11:20:40 +0000 | [diff] [blame] | 1792 | if (alloc_len < 4 || alloc_len > 0xffff) { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1793 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
| 1794 | return check_condition_result; |
| 1795 | } |
| 1796 | if (alloc_len > 8192) |
| 1797 | a_len = 8192; |
| 1798 | else |
| 1799 | a_len = alloc_len; |
Sasha Levin | 99531e6 | 2015-01-17 17:47:37 -0500 | [diff] [blame] | 1800 | arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1801 | if (NULL == arr) { |
| 1802 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 1803 | INSUFF_RES_ASCQ); |
| 1804 | return check_condition_result; |
| 1805 | } |
| 1806 | switch (reporting_opts) { |
| 1807 | case 0: /* all commands */ |
| 1808 | /* count number of commands */ |
| 1809 | for (count = 0, oip = opcode_info_arr; |
| 1810 | oip->num_attached != 0xff; ++oip) { |
| 1811 | if (F_INV_OP & oip->flags) |
| 1812 | continue; |
| 1813 | count += (oip->num_attached + 1); |
| 1814 | } |
| 1815 | bump = rctd ? 20 : 8; |
| 1816 | put_unaligned_be32(count * bump, arr); |
| 1817 | for (offset = 4, oip = opcode_info_arr; |
| 1818 | oip->num_attached != 0xff && offset < a_len; ++oip) { |
| 1819 | if (F_INV_OP & oip->flags) |
| 1820 | continue; |
| 1821 | na = oip->num_attached; |
| 1822 | arr[offset] = oip->opcode; |
| 1823 | put_unaligned_be16(oip->sa, arr + offset + 2); |
| 1824 | if (rctd) |
| 1825 | arr[offset + 5] |= 0x2; |
| 1826 | if (FF_SA & oip->flags) |
| 1827 | arr[offset + 5] |= 0x1; |
| 1828 | put_unaligned_be16(oip->len_mask[0], arr + offset + 6); |
| 1829 | if (rctd) |
| 1830 | put_unaligned_be16(0xa, arr + offset + 8); |
| 1831 | r_oip = oip; |
| 1832 | for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) { |
| 1833 | if (F_INV_OP & oip->flags) |
| 1834 | continue; |
| 1835 | offset += bump; |
| 1836 | arr[offset] = oip->opcode; |
| 1837 | put_unaligned_be16(oip->sa, arr + offset + 2); |
| 1838 | if (rctd) |
| 1839 | arr[offset + 5] |= 0x2; |
| 1840 | if (FF_SA & oip->flags) |
| 1841 | arr[offset + 5] |= 0x1; |
| 1842 | put_unaligned_be16(oip->len_mask[0], |
| 1843 | arr + offset + 6); |
| 1844 | if (rctd) |
| 1845 | put_unaligned_be16(0xa, |
| 1846 | arr + offset + 8); |
| 1847 | } |
| 1848 | oip = r_oip; |
| 1849 | offset += bump; |
| 1850 | } |
| 1851 | break; |
| 1852 | case 1: /* one command: opcode only */ |
| 1853 | case 2: /* one command: opcode plus service action */ |
| 1854 | case 3: /* one command: if sa==0 then opcode only else opcode+sa */ |
| 1855 | sdeb_i = opcode_ind_arr[req_opcode]; |
| 1856 | oip = &opcode_info_arr[sdeb_i]; |
| 1857 | if (F_INV_OP & oip->flags) { |
| 1858 | supp = 1; |
| 1859 | offset = 4; |
| 1860 | } else { |
| 1861 | if (1 == reporting_opts) { |
| 1862 | if (FF_SA & oip->flags) { |
| 1863 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, |
| 1864 | 2, 2); |
| 1865 | kfree(arr); |
| 1866 | return check_condition_result; |
| 1867 | } |
| 1868 | req_sa = 0; |
| 1869 | } else if (2 == reporting_opts && |
| 1870 | 0 == (FF_SA & oip->flags)) { |
| 1871 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1); |
| 1872 | kfree(arr); /* point at requested sa */ |
| 1873 | return check_condition_result; |
| 1874 | } |
| 1875 | if (0 == (FF_SA & oip->flags) && |
| 1876 | req_opcode == oip->opcode) |
| 1877 | supp = 3; |
| 1878 | else if (0 == (FF_SA & oip->flags)) { |
| 1879 | na = oip->num_attached; |
| 1880 | for (k = 0, oip = oip->arrp; k < na; |
| 1881 | ++k, ++oip) { |
| 1882 | if (req_opcode == oip->opcode) |
| 1883 | break; |
| 1884 | } |
| 1885 | supp = (k >= na) ? 1 : 3; |
| 1886 | } else if (req_sa != oip->sa) { |
| 1887 | na = oip->num_attached; |
| 1888 | for (k = 0, oip = oip->arrp; k < na; |
| 1889 | ++k, ++oip) { |
| 1890 | if (req_sa == oip->sa) |
| 1891 | break; |
| 1892 | } |
| 1893 | supp = (k >= na) ? 1 : 3; |
| 1894 | } else |
| 1895 | supp = 3; |
| 1896 | if (3 == supp) { |
| 1897 | u = oip->len_mask[0]; |
| 1898 | put_unaligned_be16(u, arr + 2); |
| 1899 | arr[4] = oip->opcode; |
| 1900 | for (k = 1; k < u; ++k) |
| 1901 | arr[4 + k] = (k < 16) ? |
| 1902 | oip->len_mask[k] : 0xff; |
| 1903 | offset = 4 + u; |
| 1904 | } else |
| 1905 | offset = 4; |
| 1906 | } |
| 1907 | arr[1] = (rctd ? 0x80 : 0) | supp; |
| 1908 | if (rctd) { |
| 1909 | put_unaligned_be16(0xa, arr + offset); |
| 1910 | offset += 12; |
| 1911 | } |
| 1912 | break; |
| 1913 | default: |
| 1914 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2); |
| 1915 | kfree(arr); |
| 1916 | return check_condition_result; |
| 1917 | } |
| 1918 | offset = (offset < a_len) ? offset : a_len; |
| 1919 | len = (offset < alloc_len) ? offset : alloc_len; |
| 1920 | errsts = fill_from_dev_buffer(scp, arr, len); |
| 1921 | kfree(arr); |
| 1922 | return errsts; |
| 1923 | } |
| 1924 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1925 | static int resp_rsup_tmfs(struct scsi_cmnd *scp, |
| 1926 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1927 | { |
| 1928 | bool repd; |
| 1929 | u32 alloc_len, len; |
| 1930 | u8 arr[16]; |
| 1931 | u8 *cmd = scp->cmnd; |
| 1932 | |
| 1933 | memset(arr, 0, sizeof(arr)); |
| 1934 | repd = !!(cmd[2] & 0x80); |
| 1935 | alloc_len = get_unaligned_be32(cmd + 6); |
| 1936 | if (alloc_len < 4) { |
| 1937 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
| 1938 | return check_condition_result; |
| 1939 | } |
| 1940 | arr[0] = 0xc8; /* ATS | ATSS | LURS */ |
| 1941 | arr[1] = 0x1; /* ITNRS */ |
| 1942 | if (repd) { |
| 1943 | arr[3] = 0xc; |
| 1944 | len = 16; |
| 1945 | } else |
| 1946 | len = 4; |
| 1947 | |
| 1948 | len = (len < alloc_len) ? len : alloc_len; |
| 1949 | return fill_from_dev_buffer(scp, arr, len); |
| 1950 | } |
| 1951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | /* <<Following mode page info copied from ST318451LW>> */ |
| 1953 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 1954 | static int resp_err_recov_pg(unsigned char *p, int pcontrol, int target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | { /* Read-Write Error Recovery page for mode_sense */ |
| 1956 | unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0, |
| 1957 | 5, 0, 0xff, 0xff}; |
| 1958 | |
| 1959 | memcpy(p, err_recov_pg, sizeof(err_recov_pg)); |
| 1960 | if (1 == pcontrol) |
| 1961 | memset(p + 2, 0, sizeof(err_recov_pg) - 2); |
| 1962 | return sizeof(err_recov_pg); |
| 1963 | } |
| 1964 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 1965 | static int resp_disconnect_pg(unsigned char *p, int pcontrol, int target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | { /* Disconnect-Reconnect page for mode_sense */ |
| 1967 | unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0, |
| 1968 | 0, 0, 0, 0, 0, 0, 0, 0}; |
| 1969 | |
| 1970 | memcpy(p, disconnect_pg, sizeof(disconnect_pg)); |
| 1971 | if (1 == pcontrol) |
| 1972 | memset(p + 2, 0, sizeof(disconnect_pg) - 2); |
| 1973 | return sizeof(disconnect_pg); |
| 1974 | } |
| 1975 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 1976 | static int resp_format_pg(unsigned char *p, int pcontrol, int target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | { /* Format device page for mode_sense */ |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1978 | unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0, |
| 1979 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1980 | 0, 0, 0, 0, 0x40, 0, 0, 0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1982 | memcpy(p, format_pg, sizeof(format_pg)); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1983 | put_unaligned_be16(sdebug_sectors_per, p + 10); |
| 1984 | put_unaligned_be16(sdebug_sector_size, p + 12); |
| 1985 | if (sdebug_removable) |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1986 | p[20] |= 0x20; /* should agree with INQUIRY */ |
| 1987 | if (1 == pcontrol) |
| 1988 | memset(p + 2, 0, sizeof(format_pg) - 2); |
| 1989 | return sizeof(format_pg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1992 | static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0, |
| 1993 | 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, |
| 1994 | 0, 0, 0, 0}; |
| 1995 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 1996 | static int resp_caching_pg(unsigned char *p, int pcontrol, int target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | { /* Caching page for mode_sense */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1998 | unsigned char ch_caching_pg[] = {/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0, |
| 1999 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 2000 | unsigned char d_caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0}; |
| 2002 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2003 | if (SDEBUG_OPT_N_WCE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2004 | caching_pg[2] &= ~0x4; /* set WCE=0 (default WCE=1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | memcpy(p, caching_pg, sizeof(caching_pg)); |
| 2006 | if (1 == pcontrol) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2007 | memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg)); |
| 2008 | else if (2 == pcontrol) |
| 2009 | memcpy(p, d_caching_pg, sizeof(d_caching_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | return sizeof(caching_pg); |
| 2011 | } |
| 2012 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2013 | static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0, |
| 2014 | 0, 0, 0x2, 0x4b}; |
| 2015 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 2016 | static int resp_ctrl_m_pg(unsigned char *p, int pcontrol, int target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | { /* Control mode page for mode_sense */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2018 | unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0, |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2019 | 0, 0, 0, 0}; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2020 | unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | 0, 0, 0x2, 0x4b}; |
| 2022 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2023 | if (sdebug_dsense) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | ctrl_m_pg[2] |= 0x4; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2025 | else |
| 2026 | ctrl_m_pg[2] &= ~0x4; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2027 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2028 | if (sdebug_ato) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2029 | ctrl_m_pg[5] |= 0x80; /* ATO=1 */ |
| 2030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg)); |
| 2032 | if (1 == pcontrol) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2033 | memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg)); |
| 2034 | else if (2 == pcontrol) |
| 2035 | memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | return sizeof(ctrl_m_pg); |
| 2037 | } |
| 2038 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2039 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 2040 | static int resp_iec_m_pg(unsigned char *p, int pcontrol, int target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | { /* Informational Exceptions control mode page for mode_sense */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2042 | unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0, |
| 2043 | 0, 0, 0x0, 0x0}; |
| 2044 | unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, |
| 2045 | 0, 0, 0x0, 0x0}; |
| 2046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | memcpy(p, iec_m_pg, sizeof(iec_m_pg)); |
| 2048 | if (1 == pcontrol) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2049 | memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg)); |
| 2050 | else if (2 == pcontrol) |
| 2051 | memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | return sizeof(iec_m_pg); |
| 2053 | } |
| 2054 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 2055 | static int resp_sas_sf_m_pg(unsigned char *p, int pcontrol, int target) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2056 | { /* SAS SSP mode page - short format for mode_sense */ |
| 2057 | unsigned char sas_sf_m_pg[] = {0x19, 0x6, |
| 2058 | 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0}; |
| 2059 | |
| 2060 | memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg)); |
| 2061 | if (1 == pcontrol) |
| 2062 | memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2); |
| 2063 | return sizeof(sas_sf_m_pg); |
| 2064 | } |
| 2065 | |
| 2066 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 2067 | static int resp_sas_pcd_m_spg(unsigned char *p, int pcontrol, int target, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2068 | int target_dev_id) |
| 2069 | { /* SAS phy control and discover mode page for mode_sense */ |
| 2070 | unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2, |
| 2071 | 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2072 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
| 2073 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2074 | 0x2, 0, 0, 0, 0, 0, 0, 0, |
| 2075 | 0x88, 0x99, 0, 0, 0, 0, 0, 0, |
| 2076 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 2077 | 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2078 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
| 2079 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2080 | 0x3, 0, 0, 0, 0, 0, 0, 0, |
| 2081 | 0x88, 0x99, 0, 0, 0, 0, 0, 0, |
| 2082 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 2083 | }; |
| 2084 | int port_a, port_b; |
| 2085 | |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 2086 | put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 16); |
| 2087 | put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 24); |
| 2088 | put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 64); |
| 2089 | put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 72); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2090 | port_a = target_dev_id + 1; |
| 2091 | port_b = port_a + 1; |
| 2092 | memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg)); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2093 | put_unaligned_be32(port_a, p + 20); |
| 2094 | put_unaligned_be32(port_b, p + 48 + 20); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2095 | if (1 == pcontrol) |
| 2096 | memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4); |
| 2097 | return sizeof(sas_pcd_m_pg); |
| 2098 | } |
| 2099 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 2100 | static int resp_sas_sha_m_spg(unsigned char *p, int pcontrol) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2101 | { /* SAS SSP shared protocol specific port mode subpage */ |
| 2102 | unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0, |
| 2103 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 2104 | }; |
| 2105 | |
| 2106 | memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg)); |
| 2107 | if (1 == pcontrol) |
| 2108 | memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4); |
| 2109 | return sizeof(sas_sha_m_pg); |
| 2110 | } |
| 2111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | #define SDEBUG_MAX_MSENSE_SZ 256 |
| 2113 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2114 | static int resp_mode_sense(struct scsi_cmnd *scp, |
| 2115 | struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | { |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2117 | int pcontrol, pcode, subpcode, bd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | unsigned char dev_spec; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2119 | int alloc_len, offset, len, target_dev_id; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2120 | int target = scp->device->id; |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 2121 | unsigned char *ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | unsigned char arr[SDEBUG_MAX_MSENSE_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2123 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2124 | bool dbd, llbaa, msense_6, is_disk, bad_pcode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2126 | dbd = !!(cmd[1] & 0x8); /* disable block descriptors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | pcontrol = (cmd[2] & 0xc0) >> 6; |
| 2128 | pcode = cmd[2] & 0x3f; |
| 2129 | subpcode = cmd[3]; |
| 2130 | msense_6 = (MODE_SENSE == cmd[0]); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2131 | llbaa = msense_6 ? false : !!(cmd[1] & 0x10); |
| 2132 | is_disk = (sdebug_ptype == TYPE_DISK); |
| 2133 | if (is_disk && !dbd) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2134 | bd_len = llbaa ? 16 : 8; |
| 2135 | else |
| 2136 | bd_len = 0; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2137 | alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | memset(arr, 0, SDEBUG_MAX_MSENSE_SZ); |
| 2139 | if (0x3 == pcontrol) { /* Saving values not supported */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2140 | mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | return check_condition_result; |
| 2142 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2143 | target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) + |
| 2144 | (devip->target * 1000) - 3; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 2145 | /* for disks set DPOFUA bit and clear write protect (WP) bit */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2146 | if (is_disk) |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 2147 | dev_spec = 0x10; /* =0x90 if WP=1 implies read-only */ |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2148 | else |
| 2149 | dev_spec = 0x0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | if (msense_6) { |
| 2151 | arr[2] = dev_spec; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2152 | arr[3] = bd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | offset = 4; |
| 2154 | } else { |
| 2155 | arr[3] = dev_spec; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2156 | if (16 == bd_len) |
| 2157 | arr[4] = 0x1; /* set LONGLBA bit */ |
| 2158 | arr[7] = bd_len; /* assume 255 or less */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | offset = 8; |
| 2160 | } |
| 2161 | ap = arr + offset; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 2162 | if ((bd_len > 0) && (!sdebug_capacity)) |
| 2163 | sdebug_capacity = get_sdebug_capacity(); |
| 2164 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2165 | if (8 == bd_len) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2166 | if (sdebug_capacity > 0xfffffffe) |
| 2167 | put_unaligned_be32(0xffffffff, ap + 0); |
| 2168 | else |
| 2169 | put_unaligned_be32(sdebug_capacity, ap + 0); |
| 2170 | put_unaligned_be16(sdebug_sector_size, ap + 6); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2171 | offset += bd_len; |
| 2172 | ap = arr + offset; |
| 2173 | } else if (16 == bd_len) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2174 | put_unaligned_be64((u64)sdebug_capacity, ap + 0); |
| 2175 | put_unaligned_be32(sdebug_sector_size, ap + 12); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2176 | offset += bd_len; |
| 2177 | ap = arr + offset; |
| 2178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2180 | if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) { |
| 2181 | /* TODO: Control Extension page */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2182 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | return check_condition_result; |
| 2184 | } |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2185 | bad_pcode = false; |
| 2186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | switch (pcode) { |
| 2188 | case 0x1: /* Read-Write error recovery page, direct access */ |
| 2189 | len = resp_err_recov_pg(ap, pcontrol, target); |
| 2190 | offset += len; |
| 2191 | break; |
| 2192 | case 0x2: /* Disconnect-Reconnect page, all devices */ |
| 2193 | len = resp_disconnect_pg(ap, pcontrol, target); |
| 2194 | offset += len; |
| 2195 | break; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2196 | case 0x3: /* Format device page, direct access */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2197 | if (is_disk) { |
| 2198 | len = resp_format_pg(ap, pcontrol, target); |
| 2199 | offset += len; |
| 2200 | } else |
| 2201 | bad_pcode = true; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2202 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | case 0x8: /* Caching page, direct access */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2204 | if (is_disk) { |
| 2205 | len = resp_caching_pg(ap, pcontrol, target); |
| 2206 | offset += len; |
| 2207 | } else |
| 2208 | bad_pcode = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | break; |
| 2210 | case 0xa: /* Control Mode page, all devices */ |
| 2211 | len = resp_ctrl_m_pg(ap, pcontrol, target); |
| 2212 | offset += len; |
| 2213 | break; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2214 | case 0x19: /* if spc==1 then sas phy, control+discover */ |
| 2215 | if ((subpcode > 0x2) && (subpcode < 0xff)) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2216 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2217 | return check_condition_result; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2218 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2219 | len = 0; |
| 2220 | if ((0x0 == subpcode) || (0xff == subpcode)) |
| 2221 | len += resp_sas_sf_m_pg(ap + len, pcontrol, target); |
| 2222 | if ((0x1 == subpcode) || (0xff == subpcode)) |
| 2223 | len += resp_sas_pcd_m_spg(ap + len, pcontrol, target, |
| 2224 | target_dev_id); |
| 2225 | if ((0x2 == subpcode) || (0xff == subpcode)) |
| 2226 | len += resp_sas_sha_m_spg(ap + len, pcontrol); |
| 2227 | offset += len; |
| 2228 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | case 0x1c: /* Informational Exceptions Mode page, all devices */ |
| 2230 | len = resp_iec_m_pg(ap, pcontrol, target); |
| 2231 | offset += len; |
| 2232 | break; |
| 2233 | case 0x3f: /* Read all Mode pages */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2234 | if ((0 == subpcode) || (0xff == subpcode)) { |
| 2235 | len = resp_err_recov_pg(ap, pcontrol, target); |
| 2236 | len += resp_disconnect_pg(ap + len, pcontrol, target); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2237 | if (is_disk) { |
| 2238 | len += resp_format_pg(ap + len, pcontrol, |
| 2239 | target); |
| 2240 | len += resp_caching_pg(ap + len, pcontrol, |
| 2241 | target); |
| 2242 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2243 | len += resp_ctrl_m_pg(ap + len, pcontrol, target); |
| 2244 | len += resp_sas_sf_m_pg(ap + len, pcontrol, target); |
| 2245 | if (0xff == subpcode) { |
| 2246 | len += resp_sas_pcd_m_spg(ap + len, pcontrol, |
| 2247 | target, target_dev_id); |
| 2248 | len += resp_sas_sha_m_spg(ap + len, pcontrol); |
| 2249 | } |
| 2250 | len += resp_iec_m_pg(ap + len, pcontrol, target); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2251 | offset += len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2252 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2253 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2254 | return check_condition_result; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | break; |
| 2257 | default: |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2258 | bad_pcode = true; |
| 2259 | break; |
| 2260 | } |
| 2261 | if (bad_pcode) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2262 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | return check_condition_result; |
| 2264 | } |
| 2265 | if (msense_6) |
| 2266 | arr[0] = offset - 1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2267 | else |
| 2268 | put_unaligned_be16((offset - 2), arr + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | return fill_from_dev_buffer(scp, arr, min(alloc_len, offset)); |
| 2270 | } |
| 2271 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2272 | #define SDEBUG_MAX_MSELECT_SZ 512 |
| 2273 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2274 | static int resp_mode_select(struct scsi_cmnd *scp, |
| 2275 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2276 | { |
| 2277 | int pf, sp, ps, md_len, bd_len, off, spf, pg_len; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2278 | int param_len, res, mpage; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2279 | unsigned char arr[SDEBUG_MAX_MSELECT_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2280 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2281 | int mselect6 = (MODE_SELECT == cmd[0]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2282 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2283 | memset(arr, 0, sizeof(arr)); |
| 2284 | pf = cmd[1] & 0x10; |
| 2285 | sp = cmd[1] & 0x1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2286 | param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2287 | if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2288 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2289 | return check_condition_result; |
| 2290 | } |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2291 | res = fetch_to_dev_buffer(scp, arr, param_len); |
| 2292 | if (-1 == res) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2293 | return DID_ERROR << 16; |
| 2294 | else if (sdebug_verbose && (res < param_len)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2295 | sdev_printk(KERN_INFO, scp->device, |
| 2296 | "%s: cdb indicated=%d, IO sent=%d bytes\n", |
| 2297 | __func__, param_len, res); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2298 | md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2); |
| 2299 | bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2300 | if (md_len > 2) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2301 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2302 | return check_condition_result; |
| 2303 | } |
| 2304 | off = bd_len + (mselect6 ? 4 : 8); |
| 2305 | mpage = arr[off] & 0x3f; |
| 2306 | ps = !!(arr[off] & 0x80); |
| 2307 | if (ps) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2308 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2309 | return check_condition_result; |
| 2310 | } |
| 2311 | spf = !!(arr[off] & 0x40); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2312 | pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) : |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2313 | (arr[off + 1] + 2); |
| 2314 | if ((pg_len + off) > param_len) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2315 | mk_sense_buffer(scp, ILLEGAL_REQUEST, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2316 | PARAMETER_LIST_LENGTH_ERR, 0); |
| 2317 | return check_condition_result; |
| 2318 | } |
| 2319 | switch (mpage) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2320 | case 0x8: /* Caching Mode page */ |
| 2321 | if (caching_pg[1] == arr[off + 1]) { |
| 2322 | memcpy(caching_pg + 2, arr + off + 2, |
| 2323 | sizeof(caching_pg) - 2); |
| 2324 | goto set_mode_changed_ua; |
| 2325 | } |
| 2326 | break; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2327 | case 0xa: /* Control Mode page */ |
| 2328 | if (ctrl_m_pg[1] == arr[off + 1]) { |
| 2329 | memcpy(ctrl_m_pg + 2, arr + off + 2, |
| 2330 | sizeof(ctrl_m_pg) - 2); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2331 | sdebug_dsense = !!(ctrl_m_pg[2] & 0x4); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2332 | goto set_mode_changed_ua; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2333 | } |
| 2334 | break; |
| 2335 | case 0x1c: /* Informational Exceptions Mode page */ |
| 2336 | if (iec_m_pg[1] == arr[off + 1]) { |
| 2337 | memcpy(iec_m_pg + 2, arr + off + 2, |
| 2338 | sizeof(iec_m_pg) - 2); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2339 | goto set_mode_changed_ua; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2340 | } |
| 2341 | break; |
| 2342 | default: |
| 2343 | break; |
| 2344 | } |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2345 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2346 | return check_condition_result; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2347 | set_mode_changed_ua: |
| 2348 | set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm); |
| 2349 | return 0; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2350 | } |
| 2351 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 2352 | static int resp_temp_l_pg(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2353 | { |
| 2354 | unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38, |
| 2355 | 0x0, 0x1, 0x3, 0x2, 0x0, 65, |
| 2356 | }; |
| 2357 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2358 | memcpy(arr, temp_l_pg, sizeof(temp_l_pg)); |
| 2359 | return sizeof(temp_l_pg); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2360 | } |
| 2361 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 2362 | static int resp_ie_l_pg(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2363 | { |
| 2364 | unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38, |
| 2365 | }; |
| 2366 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2367 | memcpy(arr, ie_l_pg, sizeof(ie_l_pg)); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2368 | if (iec_m_pg[2] & 0x4) { /* TEST bit set */ |
| 2369 | arr[4] = THRESHOLD_EXCEEDED; |
| 2370 | arr[5] = 0xff; |
| 2371 | } |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2372 | return sizeof(ie_l_pg); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | #define SDEBUG_MAX_LSENSE_SZ 512 |
| 2376 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2377 | static int resp_log_sense(struct scsi_cmnd *scp, |
| 2378 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2379 | { |
Bart Van Assche | ab17241 | 2017-08-25 13:46:42 -0700 | [diff] [blame] | 2380 | int ppc, sp, pcode, subpcode, alloc_len, len, n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2381 | unsigned char arr[SDEBUG_MAX_LSENSE_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2382 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2383 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2384 | memset(arr, 0, sizeof(arr)); |
| 2385 | ppc = cmd[1] & 0x2; |
| 2386 | sp = cmd[1] & 0x1; |
| 2387 | if (ppc || sp) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2388 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2389 | return check_condition_result; |
| 2390 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2391 | pcode = cmd[2] & 0x3f; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2392 | subpcode = cmd[3] & 0xff; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2393 | alloc_len = get_unaligned_be16(cmd + 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2394 | arr[0] = pcode; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2395 | if (0 == subpcode) { |
| 2396 | switch (pcode) { |
| 2397 | case 0x0: /* Supported log pages log page */ |
| 2398 | n = 4; |
| 2399 | arr[n++] = 0x0; /* this page */ |
| 2400 | arr[n++] = 0xd; /* Temperature */ |
| 2401 | arr[n++] = 0x2f; /* Informational exceptions */ |
| 2402 | arr[3] = n - 4; |
| 2403 | break; |
| 2404 | case 0xd: /* Temperature log page */ |
| 2405 | arr[3] = resp_temp_l_pg(arr + 4); |
| 2406 | break; |
| 2407 | case 0x2f: /* Informational exceptions log page */ |
| 2408 | arr[3] = resp_ie_l_pg(arr + 4); |
| 2409 | break; |
| 2410 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2411 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2412 | return check_condition_result; |
| 2413 | } |
| 2414 | } else if (0xff == subpcode) { |
| 2415 | arr[0] |= 0x40; |
| 2416 | arr[1] = subpcode; |
| 2417 | switch (pcode) { |
| 2418 | case 0x0: /* Supported log pages and subpages log page */ |
| 2419 | n = 4; |
| 2420 | arr[n++] = 0x0; |
| 2421 | arr[n++] = 0x0; /* 0,0 page */ |
| 2422 | arr[n++] = 0x0; |
| 2423 | arr[n++] = 0xff; /* this page */ |
| 2424 | arr[n++] = 0xd; |
| 2425 | arr[n++] = 0x0; /* Temperature */ |
| 2426 | arr[n++] = 0x2f; |
| 2427 | arr[n++] = 0x0; /* Informational exceptions */ |
| 2428 | arr[3] = n - 4; |
| 2429 | break; |
| 2430 | case 0xd: /* Temperature subpages */ |
| 2431 | n = 4; |
| 2432 | arr[n++] = 0xd; |
| 2433 | arr[n++] = 0x0; /* Temperature */ |
| 2434 | arr[3] = n - 4; |
| 2435 | break; |
| 2436 | case 0x2f: /* Informational exceptions subpages */ |
| 2437 | n = 4; |
| 2438 | arr[n++] = 0x2f; |
| 2439 | arr[n++] = 0x0; /* Informational exceptions */ |
| 2440 | arr[3] = n - 4; |
| 2441 | break; |
| 2442 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2443 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2444 | return check_condition_result; |
| 2445 | } |
| 2446 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2447 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2448 | return check_condition_result; |
| 2449 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2450 | len = min(get_unaligned_be16(arr + 2) + 4, alloc_len); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2451 | return fill_from_dev_buffer(scp, arr, |
| 2452 | min(len, SDEBUG_MAX_INQ_ARR_SZ)); |
| 2453 | } |
| 2454 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2455 | static int check_device_access_params(struct scsi_cmnd *scp, |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2456 | unsigned long long lba, unsigned int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2458 | if (lba + num > sdebug_capacity) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2459 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | return check_condition_result; |
| 2461 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2462 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 2463 | if (num > sdebug_store_sectors) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2464 | /* needs work to find which cdb byte 'num' comes from */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2465 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2466 | return check_condition_result; |
| 2467 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2468 | return 0; |
| 2469 | } |
| 2470 | |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2471 | /* Returns number of bytes copied or -1 if error. */ |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 2472 | static int do_device_access(struct scsi_cmnd *scmd, u32 sg_skip, u64 lba, |
| 2473 | u32 num, bool do_write) |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2474 | { |
| 2475 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2476 | u64 block, rest = 0; |
Christoph Hellwig | ae3d56d | 2019-01-29 09:33:07 +0100 | [diff] [blame] | 2477 | struct scsi_data_buffer *sdb = &scmd->sdb; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2478 | enum dma_data_direction dir; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2479 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2480 | if (do_write) { |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2481 | dir = DMA_TO_DEVICE; |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 2482 | write_since_sync = true; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2483 | } else { |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2484 | dir = DMA_FROM_DEVICE; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | if (!sdb->length) |
| 2488 | return 0; |
Christoph Hellwig | ae3d56d | 2019-01-29 09:33:07 +0100 | [diff] [blame] | 2489 | if (scmd->sc_data_direction != dir) |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2490 | return -1; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2491 | |
| 2492 | block = do_div(lba, sdebug_store_sectors); |
| 2493 | if (block + num > sdebug_store_sectors) |
| 2494 | rest = block + num - sdebug_store_sectors; |
| 2495 | |
Dave Gordon | 386ecb1 | 2015-06-30 14:58:57 -0700 | [diff] [blame] | 2496 | ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2497 | fake_storep + (block * sdebug_sector_size), |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 2498 | (num - rest) * sdebug_sector_size, sg_skip, do_write); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2499 | if (ret != (num - rest) * sdebug_sector_size) |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2500 | return ret; |
| 2501 | |
| 2502 | if (rest) { |
Dave Gordon | 386ecb1 | 2015-06-30 14:58:57 -0700 | [diff] [blame] | 2503 | ret += sg_copy_buffer(sdb->table.sgl, sdb->table.nents, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2504 | fake_storep, rest * sdebug_sector_size, |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 2505 | sg_skip + ((num - rest) * sdebug_sector_size), |
| 2506 | do_write); |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2507 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2508 | |
| 2509 | return ret; |
| 2510 | } |
| 2511 | |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2512 | /* If fake_store(lba,num) compares equal to arr(num), then copy top half of |
| 2513 | * arr into fake_store(lba,num) and return true. If comparison fails then |
| 2514 | * return false. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2515 | static bool comp_write_worker(u64 lba, u32 num, const u8 *arr) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2516 | { |
| 2517 | bool res; |
| 2518 | u64 block, rest = 0; |
| 2519 | u32 store_blks = sdebug_store_sectors; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2520 | u32 lb_size = sdebug_sector_size; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2521 | |
| 2522 | block = do_div(lba, store_blks); |
| 2523 | if (block + num > store_blks) |
| 2524 | rest = block + num - store_blks; |
| 2525 | |
| 2526 | res = !memcmp(fake_storep + (block * lb_size), arr, |
| 2527 | (num - rest) * lb_size); |
| 2528 | if (!res) |
| 2529 | return res; |
| 2530 | if (rest) |
| 2531 | res = memcmp(fake_storep, arr + ((num - rest) * lb_size), |
| 2532 | rest * lb_size); |
| 2533 | if (!res) |
| 2534 | return res; |
| 2535 | arr += num * lb_size; |
| 2536 | memcpy(fake_storep + (block * lb_size), arr, (num - rest) * lb_size); |
| 2537 | if (rest) |
| 2538 | memcpy(fake_storep, arr + ((num - rest) * lb_size), |
| 2539 | rest * lb_size); |
| 2540 | return res; |
| 2541 | } |
| 2542 | |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2543 | static __be16 dif_compute_csum(const void *buf, int len) |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2544 | { |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2545 | __be16 csum; |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2546 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2547 | if (sdebug_guard) |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2548 | csum = (__force __be16)ip_compute_csum(buf, len); |
| 2549 | else |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2550 | csum = cpu_to_be16(crc_t10dif(buf, len)); |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2551 | |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2552 | return csum; |
| 2553 | } |
| 2554 | |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 2555 | static int dif_verify(struct t10_pi_tuple *sdt, const void *data, |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2556 | sector_t sector, u32 ei_lba) |
| 2557 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2558 | __be16 csum = dif_compute_csum(data, sdebug_sector_size); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2559 | |
| 2560 | if (sdt->guard_tag != csum) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2561 | pr_err("GUARD check failed on sector %lu rcvd 0x%04x, data 0x%04x\n", |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2562 | (unsigned long)sector, |
| 2563 | be16_to_cpu(sdt->guard_tag), |
| 2564 | be16_to_cpu(csum)); |
| 2565 | return 0x01; |
| 2566 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2567 | if (sdebug_dif == T10_PI_TYPE1_PROTECTION && |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2568 | be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2569 | pr_err("REF check failed on sector %lu\n", |
| 2570 | (unsigned long)sector); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2571 | return 0x03; |
| 2572 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2573 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2574 | be32_to_cpu(sdt->ref_tag) != ei_lba) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2575 | pr_err("REF check failed on sector %lu\n", |
| 2576 | (unsigned long)sector); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2577 | return 0x03; |
| 2578 | } |
| 2579 | return 0; |
| 2580 | } |
| 2581 | |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2582 | static void dif_copy_prot(struct scsi_cmnd *SCpnt, sector_t sector, |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2583 | unsigned int sectors, bool read) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2584 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2585 | size_t resid; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2586 | void *paddr; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2587 | const void *dif_store_end = dif_storep + sdebug_store_sectors; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2588 | struct sg_mapping_iter miter; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2589 | |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 2590 | /* Bytes of protection data to copy into sgl */ |
| 2591 | resid = sectors * sizeof(*dif_storep); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2592 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2593 | sg_miter_start(&miter, scsi_prot_sglist(SCpnt), |
| 2594 | scsi_prot_sg_count(SCpnt), SG_MITER_ATOMIC | |
| 2595 | (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG)); |
| 2596 | |
| 2597 | while (sg_miter_next(&miter) && resid > 0) { |
| 2598 | size_t len = min(miter.length, resid); |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2599 | void *start = dif_store(sector); |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2600 | size_t rest = 0; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2601 | |
| 2602 | if (dif_store_end < start + len) |
| 2603 | rest = start + len - dif_store_end; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2604 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2605 | paddr = miter.addr; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2606 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2607 | if (read) |
| 2608 | memcpy(paddr, start, len - rest); |
| 2609 | else |
| 2610 | memcpy(start, paddr, len - rest); |
| 2611 | |
| 2612 | if (rest) { |
| 2613 | if (read) |
| 2614 | memcpy(paddr + len - rest, dif_storep, rest); |
| 2615 | else |
| 2616 | memcpy(dif_storep, paddr + len - rest, rest); |
| 2617 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2618 | |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 2619 | sector += len / sizeof(*dif_storep); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2620 | resid -= len; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2621 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2622 | sg_miter_stop(&miter); |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2623 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2624 | |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2625 | static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec, |
| 2626 | unsigned int sectors, u32 ei_lba) |
| 2627 | { |
| 2628 | unsigned int i; |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 2629 | struct t10_pi_tuple *sdt; |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2630 | sector_t sector; |
| 2631 | |
Akinobu Mita | c45eabec | 2014-02-26 22:56:58 +0900 | [diff] [blame] | 2632 | for (i = 0; i < sectors; i++, ei_lba++) { |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2633 | int ret; |
| 2634 | |
| 2635 | sector = start_sec + i; |
| 2636 | sdt = dif_store(sector); |
| 2637 | |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2638 | if (sdt->app_tag == cpu_to_be16(0xffff)) |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2639 | continue; |
| 2640 | |
| 2641 | ret = dif_verify(sdt, fake_store(sector), sector, ei_lba); |
| 2642 | if (ret) { |
| 2643 | dif_errors++; |
| 2644 | return ret; |
| 2645 | } |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2646 | } |
| 2647 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2648 | dif_copy_prot(SCpnt, start_sec, sectors, true); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2649 | dix_reads++; |
| 2650 | |
| 2651 | return 0; |
| 2652 | } |
| 2653 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2654 | static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2655 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2656 | u8 *cmd = scp->cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2657 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2658 | u64 lba; |
| 2659 | u32 num; |
| 2660 | u32 ei_lba; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2661 | unsigned long iflags; |
| 2662 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2663 | bool check_prot; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2664 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2665 | switch (cmd[0]) { |
| 2666 | case READ_16: |
| 2667 | ei_lba = 0; |
| 2668 | lba = get_unaligned_be64(cmd + 2); |
| 2669 | num = get_unaligned_be32(cmd + 10); |
| 2670 | check_prot = true; |
| 2671 | break; |
| 2672 | case READ_10: |
| 2673 | ei_lba = 0; |
| 2674 | lba = get_unaligned_be32(cmd + 2); |
| 2675 | num = get_unaligned_be16(cmd + 7); |
| 2676 | check_prot = true; |
| 2677 | break; |
| 2678 | case READ_6: |
| 2679 | ei_lba = 0; |
| 2680 | lba = (u32)cmd[3] | (u32)cmd[2] << 8 | |
| 2681 | (u32)(cmd[1] & 0x1f) << 16; |
| 2682 | num = (0 == cmd[4]) ? 256 : cmd[4]; |
| 2683 | check_prot = true; |
| 2684 | break; |
| 2685 | case READ_12: |
| 2686 | ei_lba = 0; |
| 2687 | lba = get_unaligned_be32(cmd + 2); |
| 2688 | num = get_unaligned_be32(cmd + 6); |
| 2689 | check_prot = true; |
| 2690 | break; |
| 2691 | case XDWRITEREAD_10: |
| 2692 | ei_lba = 0; |
| 2693 | lba = get_unaligned_be32(cmd + 2); |
| 2694 | num = get_unaligned_be16(cmd + 7); |
| 2695 | check_prot = false; |
| 2696 | break; |
| 2697 | default: /* assume READ(32) */ |
| 2698 | lba = get_unaligned_be64(cmd + 12); |
| 2699 | ei_lba = get_unaligned_be32(cmd + 20); |
| 2700 | num = get_unaligned_be32(cmd + 28); |
| 2701 | check_prot = false; |
| 2702 | break; |
| 2703 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2704 | if (unlikely(have_dif_prot && check_prot)) { |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2705 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2706 | (cmd[1] & 0xe0)) { |
| 2707 | mk_sense_invalid_opcode(scp); |
| 2708 | return check_condition_result; |
| 2709 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2710 | if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || |
| 2711 | sdebug_dif == T10_PI_TYPE3_PROTECTION) && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2712 | (cmd[1] & 0xe0) == 0) |
| 2713 | sdev_printk(KERN_ERR, scp->device, "Unprotected RD " |
| 2714 | "to DIF device\n"); |
| 2715 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2716 | if (unlikely(sdebug_any_injecting_opt)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2717 | sqcp = (struct sdebug_queued_cmd *)scp->host_scribble; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2718 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2719 | if (sqcp) { |
| 2720 | if (sqcp->inj_short) |
| 2721 | num /= 2; |
| 2722 | } |
| 2723 | } else |
| 2724 | sqcp = NULL; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2725 | |
| 2726 | /* inline check_device_access_params() */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2727 | if (unlikely(lba + num > sdebug_capacity)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2728 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 2729 | return check_condition_result; |
| 2730 | } |
| 2731 | /* transfer length excessive (tie in to block limits VPD page) */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2732 | if (unlikely(num > sdebug_store_sectors)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2733 | /* needs work to find which cdb byte 'num' comes from */ |
| 2734 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 2735 | return check_condition_result; |
| 2736 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2737 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2738 | if (unlikely((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) && |
Laurence Oberman | d9da891 | 2018-02-03 13:38:35 -0500 | [diff] [blame] | 2739 | (lba <= (sdebug_medium_error_start + sdebug_medium_error_count - 1)) && |
| 2740 | ((lba + num) > sdebug_medium_error_start))) { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2741 | /* claim unrecoverable read error */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2742 | mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2743 | /* set info field and valid bit for fixed descriptor */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2744 | if (0x70 == (scp->sense_buffer[0] & 0x7f)) { |
| 2745 | scp->sense_buffer[0] |= 0x80; /* Valid bit */ |
Douglas Gilbert | 32f7ef7 | 2011-03-11 10:43:35 -0500 | [diff] [blame] | 2746 | ret = (lba < OPT_MEDIUM_ERR_ADDR) |
| 2747 | ? OPT_MEDIUM_ERR_ADDR : (int)lba; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2748 | put_unaligned_be32(ret, scp->sense_buffer + 3); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2749 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2750 | scsi_set_resid(scp, scsi_bufflen(scp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | return check_condition_result; |
| 2752 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2753 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2754 | read_lock_irqsave(&atomic_rw, iflags); |
| 2755 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2756 | /* DIX + T10 DIF */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2757 | if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2758 | int prot_ret = prot_verify_read(scp, lba, num, ei_lba); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2759 | |
| 2760 | if (prot_ret) { |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2761 | read_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2762 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2763 | return illegal_condition_result; |
| 2764 | } |
| 2765 | } |
| 2766 | |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 2767 | ret = do_device_access(scp, 0, lba, num, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | read_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2769 | if (unlikely(ret == -1)) |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2770 | return DID_ERROR << 16; |
| 2771 | |
Bart Van Assche | 42d387b | 2019-02-08 13:25:00 -0800 | [diff] [blame^] | 2772 | scsi_set_resid(scp, scsi_bufflen(scp) - ret); |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2773 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2774 | if (unlikely(sqcp)) { |
| 2775 | if (sqcp->inj_recovered) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2776 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 2777 | THRESHOLD_EXCEEDED, 0); |
| 2778 | return check_condition_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2779 | } else if (sqcp->inj_transport) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2780 | mk_sense_buffer(scp, ABORTED_COMMAND, |
| 2781 | TRANSPORT_PROBLEM, ACK_NAK_TO); |
| 2782 | return check_condition_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2783 | } else if (sqcp->inj_dif) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2784 | /* Logical block guard check failed */ |
| 2785 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); |
| 2786 | return illegal_condition_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2787 | } else if (sqcp->inj_dix) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2788 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); |
| 2789 | return illegal_condition_result; |
| 2790 | } |
| 2791 | } |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2792 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | } |
| 2794 | |
Tomas Winkler | 58a8635 | 2015-07-28 16:54:23 +0300 | [diff] [blame] | 2795 | static void dump_sector(unsigned char *buf, int len) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2796 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2797 | int i, j, n; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2798 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2799 | pr_err(">>> Sector Dump <<<\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2800 | for (i = 0 ; i < len ; i += 16) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2801 | char b[128]; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2802 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2803 | for (j = 0, n = 0; j < 16; j++) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2804 | unsigned char c = buf[i+j]; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2805 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2806 | if (c >= 0x20 && c < 0x7e) |
| 2807 | n += scnprintf(b + n, sizeof(b) - n, |
| 2808 | " %c ", buf[i+j]); |
| 2809 | else |
| 2810 | n += scnprintf(b + n, sizeof(b) - n, |
| 2811 | "%02x ", buf[i+j]); |
| 2812 | } |
| 2813 | pr_err("%04d: %s\n", i, b); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2814 | } |
| 2815 | } |
| 2816 | |
| 2817 | static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec, |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 2818 | unsigned int sectors, u32 ei_lba) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2819 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2820 | int ret; |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 2821 | struct t10_pi_tuple *sdt; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2822 | void *daddr; |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2823 | sector_t sector = start_sec; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2824 | int ppage_offset; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2825 | int dpage_offset; |
| 2826 | struct sg_mapping_iter diter; |
| 2827 | struct sg_mapping_iter piter; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2828 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2829 | BUG_ON(scsi_sg_count(SCpnt) == 0); |
| 2830 | BUG_ON(scsi_prot_sg_count(SCpnt) == 0); |
| 2831 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2832 | sg_miter_start(&piter, scsi_prot_sglist(SCpnt), |
| 2833 | scsi_prot_sg_count(SCpnt), |
| 2834 | SG_MITER_ATOMIC | SG_MITER_FROM_SG); |
| 2835 | sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt), |
| 2836 | SG_MITER_ATOMIC | SG_MITER_FROM_SG); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2837 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2838 | /* For each protection page */ |
| 2839 | while (sg_miter_next(&piter)) { |
| 2840 | dpage_offset = 0; |
| 2841 | if (WARN_ON(!sg_miter_next(&diter))) { |
| 2842 | ret = 0x01; |
| 2843 | goto out; |
| 2844 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2845 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2846 | for (ppage_offset = 0; ppage_offset < piter.length; |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 2847 | ppage_offset += sizeof(struct t10_pi_tuple)) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2848 | /* If we're at the end of the current |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2849 | * data page advance to the next one |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2850 | */ |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2851 | if (dpage_offset >= diter.length) { |
| 2852 | if (WARN_ON(!sg_miter_next(&diter))) { |
| 2853 | ret = 0x01; |
| 2854 | goto out; |
| 2855 | } |
| 2856 | dpage_offset = 0; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2857 | } |
| 2858 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2859 | sdt = piter.addr + ppage_offset; |
| 2860 | daddr = diter.addr + dpage_offset; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2861 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2862 | ret = dif_verify(sdt, daddr, sector, ei_lba); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2863 | if (ret) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2864 | dump_sector(daddr, sdebug_sector_size); |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 2865 | goto out; |
| 2866 | } |
| 2867 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2868 | sector++; |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 2869 | ei_lba++; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2870 | dpage_offset += sdebug_sector_size; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2871 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2872 | diter.consumed = dpage_offset; |
| 2873 | sg_miter_stop(&diter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2874 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2875 | sg_miter_stop(&piter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2876 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2877 | dif_copy_prot(SCpnt, start_sec, sectors, false); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2878 | dix_writes++; |
| 2879 | |
| 2880 | return 0; |
| 2881 | |
| 2882 | out: |
| 2883 | dif_errors++; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2884 | sg_miter_stop(&diter); |
| 2885 | sg_miter_stop(&piter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2886 | return ret; |
| 2887 | } |
| 2888 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2889 | static unsigned long lba_to_map_index(sector_t lba) |
| 2890 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2891 | if (sdebug_unmap_alignment) |
| 2892 | lba += sdebug_unmap_granularity - sdebug_unmap_alignment; |
| 2893 | sector_div(lba, sdebug_unmap_granularity); |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2894 | return lba; |
| 2895 | } |
| 2896 | |
| 2897 | static sector_t map_index_to_lba(unsigned long index) |
| 2898 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2899 | sector_t lba = index * sdebug_unmap_granularity; |
Akinobu Mita | a027b5b | 2013-08-26 22:08:41 +0900 | [diff] [blame] | 2900 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2901 | if (sdebug_unmap_alignment) |
| 2902 | lba -= sdebug_unmap_granularity - sdebug_unmap_alignment; |
Akinobu Mita | a027b5b | 2013-08-26 22:08:41 +0900 | [diff] [blame] | 2903 | return lba; |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2904 | } |
| 2905 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2906 | static unsigned int map_state(sector_t lba, unsigned int *num) |
| 2907 | { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2908 | sector_t end; |
| 2909 | unsigned int mapped; |
| 2910 | unsigned long index; |
| 2911 | unsigned long next; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2912 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2913 | index = lba_to_map_index(lba); |
| 2914 | mapped = test_bit(index, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2915 | |
| 2916 | if (mapped) |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2917 | next = find_next_zero_bit(map_storep, map_size, index); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2918 | else |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2919 | next = find_next_bit(map_storep, map_size, index); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2920 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2921 | end = min_t(sector_t, sdebug_store_sectors, map_index_to_lba(next)); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2922 | *num = end - lba; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2923 | return mapped; |
| 2924 | } |
| 2925 | |
| 2926 | static void map_region(sector_t lba, unsigned int len) |
| 2927 | { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2928 | sector_t end = lba + len; |
| 2929 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2930 | while (lba < end) { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2931 | unsigned long index = lba_to_map_index(lba); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2932 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2933 | if (index < map_size) |
| 2934 | set_bit(index, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2935 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2936 | lba = map_index_to_lba(index + 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2937 | } |
| 2938 | } |
| 2939 | |
| 2940 | static void unmap_region(sector_t lba, unsigned int len) |
| 2941 | { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2942 | sector_t end = lba + len; |
| 2943 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2944 | while (lba < end) { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2945 | unsigned long index = lba_to_map_index(lba); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2946 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2947 | if (lba == map_index_to_lba(index) && |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2948 | lba + sdebug_unmap_granularity <= end && |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2949 | index < map_size) { |
| 2950 | clear_bit(index, map_storep); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2951 | if (sdebug_lbprz) { /* for LBPRZ=2 return 0xff_s */ |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 2952 | memset(fake_storep + |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2953 | lba * sdebug_sector_size, |
| 2954 | (sdebug_lbprz & 1) ? 0 : 0xff, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2955 | sdebug_sector_size * |
| 2956 | sdebug_unmap_granularity); |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2957 | } |
Akinobu Mita | e9926b4 | 2013-06-29 17:59:17 +0900 | [diff] [blame] | 2958 | if (dif_storep) { |
| 2959 | memset(dif_storep + lba, 0xff, |
| 2960 | sizeof(*dif_storep) * |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2961 | sdebug_unmap_granularity); |
Akinobu Mita | e9926b4 | 2013-06-29 17:59:17 +0900 | [diff] [blame] | 2962 | } |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 2963 | } |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2964 | lba = map_index_to_lba(index + 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2965 | } |
| 2966 | } |
| 2967 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2968 | static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2970 | u8 *cmd = scp->cmnd; |
| 2971 | u64 lba; |
| 2972 | u32 num; |
| 2973 | u32 ei_lba; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | unsigned long iflags; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2975 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2976 | bool check_prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2978 | switch (cmd[0]) { |
| 2979 | case WRITE_16: |
| 2980 | ei_lba = 0; |
| 2981 | lba = get_unaligned_be64(cmd + 2); |
| 2982 | num = get_unaligned_be32(cmd + 10); |
| 2983 | check_prot = true; |
| 2984 | break; |
| 2985 | case WRITE_10: |
| 2986 | ei_lba = 0; |
| 2987 | lba = get_unaligned_be32(cmd + 2); |
| 2988 | num = get_unaligned_be16(cmd + 7); |
| 2989 | check_prot = true; |
| 2990 | break; |
| 2991 | case WRITE_6: |
| 2992 | ei_lba = 0; |
| 2993 | lba = (u32)cmd[3] | (u32)cmd[2] << 8 | |
| 2994 | (u32)(cmd[1] & 0x1f) << 16; |
| 2995 | num = (0 == cmd[4]) ? 256 : cmd[4]; |
| 2996 | check_prot = true; |
| 2997 | break; |
| 2998 | case WRITE_12: |
| 2999 | ei_lba = 0; |
| 3000 | lba = get_unaligned_be32(cmd + 2); |
| 3001 | num = get_unaligned_be32(cmd + 6); |
| 3002 | check_prot = true; |
| 3003 | break; |
| 3004 | case 0x53: /* XDWRITEREAD(10) */ |
| 3005 | ei_lba = 0; |
| 3006 | lba = get_unaligned_be32(cmd + 2); |
| 3007 | num = get_unaligned_be16(cmd + 7); |
| 3008 | check_prot = false; |
| 3009 | break; |
| 3010 | default: /* assume WRITE(32) */ |
| 3011 | lba = get_unaligned_be64(cmd + 12); |
| 3012 | ei_lba = get_unaligned_be32(cmd + 20); |
| 3013 | num = get_unaligned_be32(cmd + 28); |
| 3014 | check_prot = false; |
| 3015 | break; |
| 3016 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3017 | if (unlikely(have_dif_prot && check_prot)) { |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 3018 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3019 | (cmd[1] & 0xe0)) { |
| 3020 | mk_sense_invalid_opcode(scp); |
| 3021 | return check_condition_result; |
| 3022 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 3023 | if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || |
| 3024 | sdebug_dif == T10_PI_TYPE3_PROTECTION) && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3025 | (cmd[1] & 0xe0) == 0) |
| 3026 | sdev_printk(KERN_ERR, scp->device, "Unprotected WR " |
| 3027 | "to DIF device\n"); |
| 3028 | } |
| 3029 | |
| 3030 | /* inline check_device_access_params() */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3031 | if (unlikely(lba + num > sdebug_capacity)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3032 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 3033 | return check_condition_result; |
| 3034 | } |
| 3035 | /* transfer length excessive (tie in to block limits VPD page) */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3036 | if (unlikely(num > sdebug_store_sectors)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3037 | /* needs work to find which cdb byte 'num' comes from */ |
| 3038 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 3039 | return check_condition_result; |
| 3040 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3042 | write_lock_irqsave(&atomic_rw, iflags); |
| 3043 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 3044 | /* DIX + T10 DIF */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3045 | if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3046 | int prot_ret = prot_verify_write(scp, lba, num, ei_lba); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 3047 | |
| 3048 | if (prot_ret) { |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3049 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3050 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 3051 | return illegal_condition_result; |
| 3052 | } |
| 3053 | } |
| 3054 | |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 3055 | ret = do_device_access(scp, 0, lba, num, true); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3056 | if (unlikely(scsi_debug_lbp())) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3057 | map_region(lba, num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3059 | if (unlikely(-1 == ret)) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3060 | return DID_ERROR << 16; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3061 | else if (unlikely(sdebug_verbose && |
| 3062 | (ret < (num * sdebug_sector_size)))) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3063 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3064 | "%s: write: cdb indicated=%u, IO sent=%d bytes\n", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3065 | my_name, num * sdebug_sector_size, ret); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3066 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3067 | if (unlikely(sdebug_any_injecting_opt)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3068 | struct sdebug_queued_cmd *sqcp = |
| 3069 | (struct sdebug_queued_cmd *)scp->host_scribble; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3070 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3071 | if (sqcp) { |
| 3072 | if (sqcp->inj_recovered) { |
| 3073 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 3074 | THRESHOLD_EXCEEDED, 0); |
| 3075 | return check_condition_result; |
| 3076 | } else if (sqcp->inj_dif) { |
| 3077 | /* Logical block guard check failed */ |
| 3078 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); |
| 3079 | return illegal_condition_result; |
| 3080 | } else if (sqcp->inj_dix) { |
| 3081 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); |
| 3082 | return illegal_condition_result; |
| 3083 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3084 | } |
| 3085 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | return 0; |
| 3087 | } |
| 3088 | |
Douglas Gilbert | 481b5e5 | 2017-12-23 12:48:14 -0500 | [diff] [blame] | 3089 | /* |
| 3090 | * T10 has only specified WRITE SCATTERED(16) and WRITE SCATTERED(32). |
| 3091 | * No READ GATHERED yet (requires bidi or long cdb holding gather list). |
| 3092 | */ |
| 3093 | static int resp_write_scat(struct scsi_cmnd *scp, |
| 3094 | struct sdebug_dev_info *devip) |
| 3095 | { |
| 3096 | u8 *cmd = scp->cmnd; |
| 3097 | u8 *lrdp = NULL; |
| 3098 | u8 *up; |
| 3099 | u8 wrprotect; |
| 3100 | u16 lbdof, num_lrd, k; |
| 3101 | u32 num, num_by, bt_len, lbdof_blen, sg_off, cum_lb; |
| 3102 | u32 lb_size = sdebug_sector_size; |
| 3103 | u32 ei_lba; |
| 3104 | u64 lba; |
| 3105 | unsigned long iflags; |
| 3106 | int ret, res; |
| 3107 | bool is_16; |
| 3108 | static const u32 lrd_size = 32; /* + parameter list header size */ |
| 3109 | |
| 3110 | if (cmd[0] == VARIABLE_LENGTH_CMD) { |
| 3111 | is_16 = false; |
| 3112 | wrprotect = (cmd[10] >> 5) & 0x7; |
| 3113 | lbdof = get_unaligned_be16(cmd + 12); |
| 3114 | num_lrd = get_unaligned_be16(cmd + 16); |
| 3115 | bt_len = get_unaligned_be32(cmd + 28); |
| 3116 | } else { /* that leaves WRITE SCATTERED(16) */ |
| 3117 | is_16 = true; |
| 3118 | wrprotect = (cmd[2] >> 5) & 0x7; |
| 3119 | lbdof = get_unaligned_be16(cmd + 4); |
| 3120 | num_lrd = get_unaligned_be16(cmd + 8); |
| 3121 | bt_len = get_unaligned_be32(cmd + 10); |
| 3122 | if (unlikely(have_dif_prot)) { |
| 3123 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
| 3124 | wrprotect) { |
| 3125 | mk_sense_invalid_opcode(scp); |
| 3126 | return illegal_condition_result; |
| 3127 | } |
| 3128 | if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || |
| 3129 | sdebug_dif == T10_PI_TYPE3_PROTECTION) && |
| 3130 | wrprotect == 0) |
| 3131 | sdev_printk(KERN_ERR, scp->device, |
| 3132 | "Unprotected WR to DIF device\n"); |
| 3133 | } |
| 3134 | } |
| 3135 | if ((num_lrd == 0) || (bt_len == 0)) |
| 3136 | return 0; /* T10 says these do-nothings are not errors */ |
| 3137 | if (lbdof == 0) { |
| 3138 | if (sdebug_verbose) |
| 3139 | sdev_printk(KERN_INFO, scp->device, |
| 3140 | "%s: %s: LB Data Offset field bad\n", |
| 3141 | my_name, __func__); |
| 3142 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 3143 | return illegal_condition_result; |
| 3144 | } |
| 3145 | lbdof_blen = lbdof * lb_size; |
| 3146 | if ((lrd_size + (num_lrd * lrd_size)) > lbdof_blen) { |
| 3147 | if (sdebug_verbose) |
| 3148 | sdev_printk(KERN_INFO, scp->device, |
| 3149 | "%s: %s: LBA range descriptors don't fit\n", |
| 3150 | my_name, __func__); |
| 3151 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 3152 | return illegal_condition_result; |
| 3153 | } |
| 3154 | lrdp = kzalloc(lbdof_blen, GFP_ATOMIC); |
| 3155 | if (lrdp == NULL) |
| 3156 | return SCSI_MLQUEUE_HOST_BUSY; |
| 3157 | if (sdebug_verbose) |
| 3158 | sdev_printk(KERN_INFO, scp->device, |
| 3159 | "%s: %s: Fetch header+scatter_list, lbdof_blen=%u\n", |
| 3160 | my_name, __func__, lbdof_blen); |
| 3161 | res = fetch_to_dev_buffer(scp, lrdp, lbdof_blen); |
| 3162 | if (res == -1) { |
| 3163 | ret = DID_ERROR << 16; |
| 3164 | goto err_out; |
| 3165 | } |
| 3166 | |
| 3167 | write_lock_irqsave(&atomic_rw, iflags); |
| 3168 | sg_off = lbdof_blen; |
| 3169 | /* Spec says Buffer xfer Length field in number of LBs in dout */ |
| 3170 | cum_lb = 0; |
| 3171 | for (k = 0, up = lrdp + lrd_size; k < num_lrd; ++k, up += lrd_size) { |
| 3172 | lba = get_unaligned_be64(up + 0); |
| 3173 | num = get_unaligned_be32(up + 8); |
| 3174 | if (sdebug_verbose) |
| 3175 | sdev_printk(KERN_INFO, scp->device, |
| 3176 | "%s: %s: k=%d LBA=0x%llx num=%u sg_off=%u\n", |
| 3177 | my_name, __func__, k, lba, num, sg_off); |
| 3178 | if (num == 0) |
| 3179 | continue; |
| 3180 | ret = check_device_access_params(scp, lba, num); |
| 3181 | if (ret) |
| 3182 | goto err_out_unlock; |
| 3183 | num_by = num * lb_size; |
| 3184 | ei_lba = is_16 ? 0 : get_unaligned_be32(up + 12); |
| 3185 | |
| 3186 | if ((cum_lb + num) > bt_len) { |
| 3187 | if (sdebug_verbose) |
| 3188 | sdev_printk(KERN_INFO, scp->device, |
| 3189 | "%s: %s: sum of blocks > data provided\n", |
| 3190 | my_name, __func__); |
| 3191 | mk_sense_buffer(scp, ILLEGAL_REQUEST, WRITE_ERROR_ASC, |
| 3192 | 0); |
| 3193 | ret = illegal_condition_result; |
| 3194 | goto err_out_unlock; |
| 3195 | } |
| 3196 | |
| 3197 | /* DIX + T10 DIF */ |
| 3198 | if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { |
| 3199 | int prot_ret = prot_verify_write(scp, lba, num, |
| 3200 | ei_lba); |
| 3201 | |
| 3202 | if (prot_ret) { |
| 3203 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, |
| 3204 | prot_ret); |
| 3205 | ret = illegal_condition_result; |
| 3206 | goto err_out_unlock; |
| 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | ret = do_device_access(scp, sg_off, lba, num, true); |
| 3211 | if (unlikely(scsi_debug_lbp())) |
| 3212 | map_region(lba, num); |
| 3213 | if (unlikely(-1 == ret)) { |
| 3214 | ret = DID_ERROR << 16; |
| 3215 | goto err_out_unlock; |
| 3216 | } else if (unlikely(sdebug_verbose && (ret < num_by))) |
| 3217 | sdev_printk(KERN_INFO, scp->device, |
| 3218 | "%s: write: cdb indicated=%u, IO sent=%d bytes\n", |
| 3219 | my_name, num_by, ret); |
| 3220 | |
| 3221 | if (unlikely(sdebug_any_injecting_opt)) { |
| 3222 | struct sdebug_queued_cmd *sqcp = |
| 3223 | (struct sdebug_queued_cmd *)scp->host_scribble; |
| 3224 | |
| 3225 | if (sqcp) { |
| 3226 | if (sqcp->inj_recovered) { |
| 3227 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 3228 | THRESHOLD_EXCEEDED, 0); |
| 3229 | ret = illegal_condition_result; |
| 3230 | goto err_out_unlock; |
| 3231 | } else if (sqcp->inj_dif) { |
| 3232 | /* Logical block guard check failed */ |
| 3233 | mk_sense_buffer(scp, ABORTED_COMMAND, |
| 3234 | 0x10, 1); |
| 3235 | ret = illegal_condition_result; |
| 3236 | goto err_out_unlock; |
| 3237 | } else if (sqcp->inj_dix) { |
| 3238 | mk_sense_buffer(scp, ILLEGAL_REQUEST, |
| 3239 | 0x10, 1); |
| 3240 | ret = illegal_condition_result; |
| 3241 | goto err_out_unlock; |
| 3242 | } |
| 3243 | } |
| 3244 | } |
| 3245 | sg_off += num_by; |
| 3246 | cum_lb += num; |
| 3247 | } |
| 3248 | ret = 0; |
| 3249 | err_out_unlock: |
| 3250 | write_unlock_irqrestore(&atomic_rw, iflags); |
| 3251 | err_out: |
| 3252 | kfree(lrdp); |
| 3253 | return ret; |
| 3254 | } |
| 3255 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3256 | static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num, |
| 3257 | u32 ei_lba, bool unmap, bool ndob) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3258 | { |
| 3259 | unsigned long iflags; |
| 3260 | unsigned long long i; |
| 3261 | int ret; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3262 | u64 lba_off; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3263 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3264 | ret = check_device_access_params(scp, lba, num); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3265 | if (ret) |
| 3266 | return ret; |
| 3267 | |
| 3268 | write_lock_irqsave(&atomic_rw, iflags); |
| 3269 | |
Akinobu Mita | 9ed8d3d | 2013-04-16 22:11:55 +0900 | [diff] [blame] | 3270 | if (unmap && scsi_debug_lbp()) { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3271 | unmap_region(lba, num); |
| 3272 | goto out; |
| 3273 | } |
| 3274 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3275 | lba_off = lba * sdebug_sector_size; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3276 | /* if ndob then zero 1 logical block, else fetch 1 logical block */ |
| 3277 | if (ndob) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3278 | memset(fake_storep + lba_off, 0, sdebug_sector_size); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3279 | ret = 0; |
| 3280 | } else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3281 | ret = fetch_to_dev_buffer(scp, fake_storep + lba_off, |
| 3282 | sdebug_sector_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3283 | |
| 3284 | if (-1 == ret) { |
| 3285 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3286 | return DID_ERROR << 16; |
Douglas Gilbert | e33d7c5 | 2017-10-29 10:47:19 -0400 | [diff] [blame] | 3287 | } else if (sdebug_verbose && !ndob && (ret < sdebug_sector_size)) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3288 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | e33d7c5 | 2017-10-29 10:47:19 -0400 | [diff] [blame] | 3289 | "%s: %s: lb size=%u, IO sent=%d bytes\n", |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3290 | my_name, "write same", |
Douglas Gilbert | e33d7c5 | 2017-10-29 10:47:19 -0400 | [diff] [blame] | 3291 | sdebug_sector_size, ret); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3292 | |
| 3293 | /* Copy first sector to remaining blocks */ |
| 3294 | for (i = 1 ; i < num ; i++) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3295 | memcpy(fake_storep + ((lba + i) * sdebug_sector_size), |
| 3296 | fake_storep + lba_off, |
| 3297 | sdebug_sector_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3298 | |
Akinobu Mita | 9ed8d3d | 2013-04-16 22:11:55 +0900 | [diff] [blame] | 3299 | if (scsi_debug_lbp()) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3300 | map_region(lba, num); |
| 3301 | out: |
| 3302 | write_unlock_irqrestore(&atomic_rw, iflags); |
| 3303 | |
| 3304 | return 0; |
| 3305 | } |
| 3306 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3307 | static int resp_write_same_10(struct scsi_cmnd *scp, |
| 3308 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3309 | { |
| 3310 | u8 *cmd = scp->cmnd; |
| 3311 | u32 lba; |
| 3312 | u16 num; |
| 3313 | u32 ei_lba = 0; |
| 3314 | bool unmap = false; |
| 3315 | |
| 3316 | if (cmd[1] & 0x8) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3317 | if (sdebug_lbpws10 == 0) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3318 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); |
| 3319 | return check_condition_result; |
| 3320 | } else |
| 3321 | unmap = true; |
| 3322 | } |
| 3323 | lba = get_unaligned_be32(cmd + 2); |
| 3324 | num = get_unaligned_be16(cmd + 7); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3325 | if (num > sdebug_write_same_length) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3326 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); |
| 3327 | return check_condition_result; |
| 3328 | } |
| 3329 | return resp_write_same(scp, lba, num, ei_lba, unmap, false); |
| 3330 | } |
| 3331 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3332 | static int resp_write_same_16(struct scsi_cmnd *scp, |
| 3333 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3334 | { |
| 3335 | u8 *cmd = scp->cmnd; |
| 3336 | u64 lba; |
| 3337 | u32 num; |
| 3338 | u32 ei_lba = 0; |
| 3339 | bool unmap = false; |
| 3340 | bool ndob = false; |
| 3341 | |
| 3342 | if (cmd[1] & 0x8) { /* UNMAP */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3343 | if (sdebug_lbpws == 0) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3344 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); |
| 3345 | return check_condition_result; |
| 3346 | } else |
| 3347 | unmap = true; |
| 3348 | } |
| 3349 | if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */ |
| 3350 | ndob = true; |
| 3351 | lba = get_unaligned_be64(cmd + 2); |
| 3352 | num = get_unaligned_be32(cmd + 10); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3353 | if (num > sdebug_write_same_length) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3354 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1); |
| 3355 | return check_condition_result; |
| 3356 | } |
| 3357 | return resp_write_same(scp, lba, num, ei_lba, unmap, ndob); |
| 3358 | } |
| 3359 | |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 3360 | /* Note the mode field is in the same position as the (lower) service action |
| 3361 | * field. For the Report supported operation codes command, SPC-4 suggests |
| 3362 | * each mode of this command should be reported separately; for future. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3363 | static int resp_write_buffer(struct scsi_cmnd *scp, |
| 3364 | struct sdebug_dev_info *devip) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 3365 | { |
| 3366 | u8 *cmd = scp->cmnd; |
| 3367 | struct scsi_device *sdp = scp->device; |
| 3368 | struct sdebug_dev_info *dp; |
| 3369 | u8 mode; |
| 3370 | |
| 3371 | mode = cmd[1] & 0x1f; |
| 3372 | switch (mode) { |
| 3373 | case 0x4: /* download microcode (MC) and activate (ACT) */ |
| 3374 | /* set UAs on this device only */ |
| 3375 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3376 | set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm); |
| 3377 | break; |
| 3378 | case 0x5: /* download MC, save and ACT */ |
| 3379 | set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm); |
| 3380 | break; |
| 3381 | case 0x6: /* download MC with offsets and ACT */ |
| 3382 | /* set UAs on most devices (LUs) in this target */ |
| 3383 | list_for_each_entry(dp, |
| 3384 | &devip->sdbg_host->dev_info_list, |
| 3385 | dev_list) |
| 3386 | if (dp->target == sdp->id) { |
| 3387 | set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm); |
| 3388 | if (devip != dp) |
| 3389 | set_bit(SDEBUG_UA_MICROCODE_CHANGED, |
| 3390 | dp->uas_bm); |
| 3391 | } |
| 3392 | break; |
| 3393 | case 0x7: /* download MC with offsets, save, and ACT */ |
| 3394 | /* set UA on all devices (LUs) in this target */ |
| 3395 | list_for_each_entry(dp, |
| 3396 | &devip->sdbg_host->dev_info_list, |
| 3397 | dev_list) |
| 3398 | if (dp->target == sdp->id) |
| 3399 | set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, |
| 3400 | dp->uas_bm); |
| 3401 | break; |
| 3402 | default: |
| 3403 | /* do nothing for this command for other mode values */ |
| 3404 | break; |
| 3405 | } |
| 3406 | return 0; |
| 3407 | } |
| 3408 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3409 | static int resp_comp_write(struct scsi_cmnd *scp, |
| 3410 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3411 | { |
| 3412 | u8 *cmd = scp->cmnd; |
| 3413 | u8 *arr; |
| 3414 | u8 *fake_storep_hold; |
| 3415 | u64 lba; |
| 3416 | u32 dnum; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3417 | u32 lb_size = sdebug_sector_size; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3418 | u8 num; |
| 3419 | unsigned long iflags; |
| 3420 | int ret; |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3421 | int retval = 0; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3422 | |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3423 | lba = get_unaligned_be64(cmd + 2); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3424 | num = cmd[13]; /* 1 to a maximum of 255 logical blocks */ |
| 3425 | if (0 == num) |
| 3426 | return 0; /* degenerate case, not an error */ |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 3427 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3428 | (cmd[1] & 0xe0)) { |
| 3429 | mk_sense_invalid_opcode(scp); |
| 3430 | return check_condition_result; |
| 3431 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 3432 | if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || |
| 3433 | sdebug_dif == T10_PI_TYPE3_PROTECTION) && |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3434 | (cmd[1] & 0xe0) == 0) |
| 3435 | sdev_printk(KERN_ERR, scp->device, "Unprotected WR " |
| 3436 | "to DIF device\n"); |
| 3437 | |
| 3438 | /* inline check_device_access_params() */ |
| 3439 | if (lba + num > sdebug_capacity) { |
| 3440 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 3441 | return check_condition_result; |
| 3442 | } |
| 3443 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 3444 | if (num > sdebug_store_sectors) { |
| 3445 | /* needs work to find which cdb byte 'num' comes from */ |
| 3446 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 3447 | return check_condition_result; |
| 3448 | } |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3449 | dnum = 2 * num; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 3450 | arr = kcalloc(lb_size, dnum, GFP_ATOMIC); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3451 | if (NULL == arr) { |
| 3452 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3453 | INSUFF_RES_ASCQ); |
| 3454 | return check_condition_result; |
| 3455 | } |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3456 | |
| 3457 | write_lock_irqsave(&atomic_rw, iflags); |
| 3458 | |
| 3459 | /* trick do_device_access() to fetch both compare and write buffers |
| 3460 | * from data-in into arr. Safe (atomic) since write_lock held. */ |
| 3461 | fake_storep_hold = fake_storep; |
| 3462 | fake_storep = arr; |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 3463 | ret = do_device_access(scp, 0, 0, dnum, true); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3464 | fake_storep = fake_storep_hold; |
| 3465 | if (ret == -1) { |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3466 | retval = DID_ERROR << 16; |
| 3467 | goto cleanup; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3468 | } else if (sdebug_verbose && (ret < (dnum * lb_size))) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3469 | sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb " |
| 3470 | "indicated=%u, IO sent=%d bytes\n", my_name, |
| 3471 | dnum * lb_size, ret); |
| 3472 | if (!comp_write_worker(lba, num, arr)) { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3473 | mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3474 | retval = check_condition_result; |
| 3475 | goto cleanup; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3476 | } |
| 3477 | if (scsi_debug_lbp()) |
| 3478 | map_region(lba, num); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3479 | cleanup: |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3480 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3481 | kfree(arr); |
| 3482 | return retval; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3483 | } |
| 3484 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3485 | struct unmap_block_desc { |
| 3486 | __be64 lba; |
| 3487 | __be32 blocks; |
| 3488 | __be32 __reserved; |
| 3489 | }; |
| 3490 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3491 | static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3492 | { |
| 3493 | unsigned char *buf; |
| 3494 | struct unmap_block_desc *desc; |
| 3495 | unsigned int i, payload_len, descriptors; |
| 3496 | int ret; |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3497 | unsigned long iflags; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3498 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3499 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3500 | if (!scsi_debug_lbp()) |
| 3501 | return 0; /* fib and say its done */ |
| 3502 | payload_len = get_unaligned_be16(scp->cmnd + 7); |
| 3503 | BUG_ON(scsi_bufflen(scp) != payload_len); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3504 | |
| 3505 | descriptors = (payload_len - 8) / 16; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3506 | if (descriptors > sdebug_unmap_max_desc) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3507 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3508 | return check_condition_result; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3509 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3510 | |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 3511 | buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3512 | if (!buf) { |
| 3513 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3514 | INSUFF_RES_ASCQ); |
| 3515 | return check_condition_result; |
| 3516 | } |
| 3517 | |
| 3518 | scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp)); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3519 | |
| 3520 | BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2); |
| 3521 | BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16); |
| 3522 | |
| 3523 | desc = (void *)&buf[8]; |
| 3524 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3525 | write_lock_irqsave(&atomic_rw, iflags); |
| 3526 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3527 | for (i = 0 ; i < descriptors ; i++) { |
| 3528 | unsigned long long lba = get_unaligned_be64(&desc[i].lba); |
| 3529 | unsigned int num = get_unaligned_be32(&desc[i].blocks); |
| 3530 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3531 | ret = check_device_access_params(scp, lba, num); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3532 | if (ret) |
| 3533 | goto out; |
| 3534 | |
| 3535 | unmap_region(lba, num); |
| 3536 | } |
| 3537 | |
| 3538 | ret = 0; |
| 3539 | |
| 3540 | out: |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3541 | write_unlock_irqrestore(&atomic_rw, iflags); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3542 | kfree(buf); |
| 3543 | |
| 3544 | return ret; |
| 3545 | } |
| 3546 | |
| 3547 | #define SDEBUG_GET_LBA_STATUS_LEN 32 |
| 3548 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3549 | static int resp_get_lba_status(struct scsi_cmnd *scp, |
| 3550 | struct sdebug_dev_info *devip) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3551 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3552 | u8 *cmd = scp->cmnd; |
| 3553 | u64 lba; |
| 3554 | u32 alloc_len, mapped, num; |
| 3555 | u8 arr[SDEBUG_GET_LBA_STATUS_LEN]; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3556 | int ret; |
| 3557 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3558 | lba = get_unaligned_be64(cmd + 2); |
| 3559 | alloc_len = get_unaligned_be32(cmd + 10); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3560 | |
| 3561 | if (alloc_len < 24) |
| 3562 | return 0; |
| 3563 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3564 | ret = check_device_access_params(scp, lba, 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3565 | if (ret) |
| 3566 | return ret; |
| 3567 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3568 | if (scsi_debug_lbp()) |
| 3569 | mapped = map_state(lba, &num); |
| 3570 | else { |
| 3571 | mapped = 1; |
| 3572 | /* following just in case virtual_gb changed */ |
| 3573 | sdebug_capacity = get_sdebug_capacity(); |
| 3574 | if (sdebug_capacity - lba <= 0xffffffff) |
| 3575 | num = sdebug_capacity - lba; |
| 3576 | else |
| 3577 | num = 0xffffffff; |
| 3578 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3579 | |
| 3580 | memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3581 | put_unaligned_be32(20, arr); /* Parameter Data Length */ |
| 3582 | put_unaligned_be64(lba, arr + 8); /* LBA */ |
| 3583 | put_unaligned_be32(num, arr + 16); /* Number of blocks */ |
| 3584 | arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3585 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3586 | return fill_from_dev_buffer(scp, arr, SDEBUG_GET_LBA_STATUS_LEN); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3587 | } |
| 3588 | |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 3589 | static int resp_sync_cache(struct scsi_cmnd *scp, |
| 3590 | struct sdebug_dev_info *devip) |
| 3591 | { |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 3592 | int res = 0; |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 3593 | u64 lba; |
| 3594 | u32 num_blocks; |
| 3595 | u8 *cmd = scp->cmnd; |
| 3596 | |
| 3597 | if (cmd[0] == SYNCHRONIZE_CACHE) { /* 10 byte cdb */ |
| 3598 | lba = get_unaligned_be32(cmd + 2); |
| 3599 | num_blocks = get_unaligned_be16(cmd + 7); |
| 3600 | } else { /* SYNCHRONIZE_CACHE(16) */ |
| 3601 | lba = get_unaligned_be64(cmd + 2); |
| 3602 | num_blocks = get_unaligned_be32(cmd + 10); |
| 3603 | } |
| 3604 | if (lba + num_blocks > sdebug_capacity) { |
| 3605 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 3606 | return check_condition_result; |
| 3607 | } |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 3608 | if (!write_since_sync || cmd[1] & 0x2) |
| 3609 | res = SDEG_RES_IMMED_MASK; |
| 3610 | else /* delay if write_since_sync and IMMED clear */ |
| 3611 | write_since_sync = false; |
| 3612 | return res; |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 3613 | } |
| 3614 | |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3615 | #define RL_BUCKET_ELEMS 8 |
| 3616 | |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3617 | /* Even though each pseudo target has a REPORT LUNS "well known logical unit" |
| 3618 | * (W-LUN), the normal Linux scanning logic does not associate it with a |
| 3619 | * device (e.g. /dev/sg7). The following magic will make that association: |
| 3620 | * "cd /sys/class/scsi_host/host<n> ; echo '- - 49409' > scan" |
| 3621 | * where <n> is a host number. If there are multiple targets in a host then |
| 3622 | * the above will associate a W-LUN to each target. To only get a W-LUN |
| 3623 | * for target 2, then use "echo '- 2 49409' > scan" . |
| 3624 | */ |
| 3625 | static int resp_report_luns(struct scsi_cmnd *scp, |
| 3626 | struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 3628 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3629 | unsigned int alloc_len; |
| 3630 | unsigned char select_report; |
| 3631 | u64 lun; |
| 3632 | struct scsi_lun *lun_p; |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3633 | u8 arr[RL_BUCKET_ELEMS * sizeof(struct scsi_lun)]; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3634 | unsigned int lun_cnt; /* normal LUN count (max: 256) */ |
| 3635 | unsigned int wlun_cnt; /* report luns W-LUN count */ |
| 3636 | unsigned int tlun_cnt; /* total LUN count */ |
| 3637 | unsigned int rlen; /* response length (in bytes) */ |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3638 | int k, j, n, res; |
| 3639 | unsigned int off_rsp = 0; |
| 3640 | const int sz_lun = sizeof(struct scsi_lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3641 | |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 3642 | clear_luns_changed_on_target(devip); |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3643 | |
| 3644 | select_report = cmd[2]; |
| 3645 | alloc_len = get_unaligned_be32(cmd + 6); |
| 3646 | |
| 3647 | if (alloc_len < 4) { |
| 3648 | pr_err("alloc len too small %d\n", alloc_len); |
| 3649 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | return check_condition_result; |
| 3651 | } |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3652 | |
| 3653 | switch (select_report) { |
| 3654 | case 0: /* all LUNs apart from W-LUNs */ |
| 3655 | lun_cnt = sdebug_max_luns; |
| 3656 | wlun_cnt = 0; |
| 3657 | break; |
| 3658 | case 1: /* only W-LUNs */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3659 | lun_cnt = 0; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3660 | wlun_cnt = 1; |
| 3661 | break; |
| 3662 | case 2: /* all LUNs */ |
| 3663 | lun_cnt = sdebug_max_luns; |
| 3664 | wlun_cnt = 1; |
| 3665 | break; |
| 3666 | case 0x10: /* only administrative LUs */ |
| 3667 | case 0x11: /* see SPC-5 */ |
| 3668 | case 0x12: /* only subsiduary LUs owned by referenced LU */ |
| 3669 | default: |
| 3670 | pr_debug("select report invalid %d\n", select_report); |
| 3671 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); |
| 3672 | return check_condition_result; |
| 3673 | } |
| 3674 | |
| 3675 | if (sdebug_no_lun_0 && (lun_cnt > 0)) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3676 | --lun_cnt; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3677 | |
| 3678 | tlun_cnt = lun_cnt + wlun_cnt; |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3679 | rlen = tlun_cnt * sz_lun; /* excluding 8 byte header */ |
| 3680 | scsi_set_resid(scp, scsi_bufflen(scp)); |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3681 | pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n", |
| 3682 | select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0); |
| 3683 | |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3684 | /* loops rely on sizeof response header same as sizeof lun (both 8) */ |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3685 | lun = sdebug_no_lun_0 ? 1 : 0; |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3686 | for (k = 0, j = 0, res = 0; true; ++k, j = 0) { |
| 3687 | memset(arr, 0, sizeof(arr)); |
| 3688 | lun_p = (struct scsi_lun *)&arr[0]; |
| 3689 | if (k == 0) { |
| 3690 | put_unaligned_be32(rlen, &arr[0]); |
| 3691 | ++lun_p; |
| 3692 | j = 1; |
| 3693 | } |
| 3694 | for ( ; j < RL_BUCKET_ELEMS; ++j, ++lun_p) { |
| 3695 | if ((k * RL_BUCKET_ELEMS) + j > lun_cnt) |
| 3696 | break; |
| 3697 | int_to_scsilun(lun++, lun_p); |
| 3698 | } |
| 3699 | if (j < RL_BUCKET_ELEMS) |
| 3700 | break; |
| 3701 | n = j * sz_lun; |
| 3702 | res = p_fill_from_dev_buffer(scp, arr, n, off_rsp); |
| 3703 | if (res) |
| 3704 | return res; |
| 3705 | off_rsp += n; |
| 3706 | } |
| 3707 | if (wlun_cnt) { |
| 3708 | int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p); |
| 3709 | ++j; |
| 3710 | } |
| 3711 | if (j > 0) |
| 3712 | res = p_fill_from_dev_buffer(scp, arr, j * sz_lun, off_rsp); |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3713 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3714 | } |
| 3715 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3716 | static struct sdebug_queue *get_queue(struct scsi_cmnd *cmnd) |
| 3717 | { |
Bart Van Assche | 458df78 | 2018-01-26 08:52:19 -0800 | [diff] [blame] | 3718 | u32 tag = blk_mq_unique_tag(cmnd->request); |
| 3719 | u16 hwq = blk_mq_unique_tag_to_hwq(tag); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3720 | |
Bart Van Assche | 458df78 | 2018-01-26 08:52:19 -0800 | [diff] [blame] | 3721 | pr_debug("tag=%#x, hwq=%d\n", tag, hwq); |
| 3722 | if (WARN_ON_ONCE(hwq >= submit_queues)) |
| 3723 | hwq = 0; |
| 3724 | return sdebug_q_arr + hwq; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | /* Queued (deferred) command completions converge here. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3728 | static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | { |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 3730 | bool aborted = sd_dp->aborted; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3731 | int qc_idx; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3732 | int retiring = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | unsigned long iflags; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3734 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3735 | struct sdebug_queued_cmd *sqcp; |
| 3736 | struct scsi_cmnd *scp; |
| 3737 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3739 | sd_dp->defer_t = SDEB_DEFER_NONE; |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 3740 | if (unlikely(aborted)) |
| 3741 | sd_dp->aborted = false; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3742 | qc_idx = sd_dp->qc_idx; |
| 3743 | sqp = sdebug_q_arr + sd_dp->sqa_idx; |
| 3744 | if (sdebug_statistics) { |
| 3745 | atomic_inc(&sdebug_completions); |
| 3746 | if (raw_smp_processor_id() != sd_dp->issuing_cpu) |
| 3747 | atomic_inc(&sdebug_miss_cpus); |
| 3748 | } |
| 3749 | if (unlikely((qc_idx < 0) || (qc_idx >= SDEBUG_CANQUEUE))) { |
| 3750 | pr_err("wild qc_idx=%d\n", qc_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3751 | return; |
| 3752 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3753 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 3754 | sqcp = &sqp->qc_arr[qc_idx]; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3755 | scp = sqcp->a_cmnd; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3756 | if (unlikely(scp == NULL)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3757 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 3758 | pr_err("scp is NULL, sqa_idx=%d, qc_idx=%d\n", |
| 3759 | sd_dp->sqa_idx, qc_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | return; |
| 3761 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3762 | devip = (struct sdebug_dev_info *)scp->device->hostdata; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3763 | if (likely(devip)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3764 | atomic_dec(&devip->num_in_q); |
| 3765 | else |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3766 | pr_err("devip=NULL\n"); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3767 | if (unlikely(atomic_read(&retired_max_queue) > 0)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3768 | retiring = 1; |
| 3769 | |
| 3770 | sqcp->a_cmnd = NULL; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3771 | if (unlikely(!test_and_clear_bit(qc_idx, sqp->in_use_bm))) { |
| 3772 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3773 | pr_err("Unexpected completion\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3774 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3775 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3776 | |
| 3777 | if (unlikely(retiring)) { /* user has reduced max_queue */ |
| 3778 | int k, retval; |
| 3779 | |
| 3780 | retval = atomic_read(&retired_max_queue); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3781 | if (qc_idx >= retval) { |
| 3782 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3783 | pr_err("index %d too large\n", retval); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3784 | return; |
| 3785 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3786 | k = find_last_bit(sqp->in_use_bm, retval); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3787 | if ((k < sdebug_max_queue) || (k == retval)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3788 | atomic_set(&retired_max_queue, 0); |
| 3789 | else |
| 3790 | atomic_set(&retired_max_queue, k + 1); |
| 3791 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3792 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 3793 | if (unlikely(aborted)) { |
| 3794 | if (sdebug_verbose) |
| 3795 | pr_info("bypassing scsi_done() due to aborted cmd\n"); |
| 3796 | return; |
| 3797 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3798 | scp->scsi_done(scp); /* callback to mid level */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3799 | } |
| 3800 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3801 | /* When high resolution timer goes off this function is called. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3802 | static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3803 | { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3804 | struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer, |
| 3805 | hrt); |
| 3806 | sdebug_q_cmd_complete(sd_dp); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3807 | return HRTIMER_NORESTART; |
| 3808 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3809 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3810 | /* When work queue schedules work, it calls this function. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3811 | static void sdebug_q_cmd_wq_complete(struct work_struct *work) |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3812 | { |
| 3813 | struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer, |
| 3814 | ew.work); |
| 3815 | sdebug_q_cmd_complete(sd_dp); |
| 3816 | } |
| 3817 | |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3818 | static bool got_shared_uuid; |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 3819 | static uuid_t shared_uuid; |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3820 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3821 | static struct sdebug_dev_info *sdebug_device_create( |
| 3822 | struct sdebug_host_info *sdbg_host, gfp_t flags) |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3823 | { |
| 3824 | struct sdebug_dev_info *devip; |
| 3825 | |
| 3826 | devip = kzalloc(sizeof(*devip), flags); |
| 3827 | if (devip) { |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3828 | if (sdebug_uuid_ctl == 1) |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 3829 | uuid_gen(&devip->lu_name); |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3830 | else if (sdebug_uuid_ctl == 2) { |
| 3831 | if (got_shared_uuid) |
| 3832 | devip->lu_name = shared_uuid; |
| 3833 | else { |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 3834 | uuid_gen(&shared_uuid); |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3835 | got_shared_uuid = true; |
| 3836 | devip->lu_name = shared_uuid; |
| 3837 | } |
| 3838 | } |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3839 | devip->sdbg_host = sdbg_host; |
| 3840 | list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list); |
| 3841 | } |
| 3842 | return devip; |
| 3843 | } |
| 3844 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3845 | static struct sdebug_dev_info *find_build_dev_info(struct scsi_device *sdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3846 | { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3847 | struct sdebug_host_info *sdbg_host; |
| 3848 | struct sdebug_dev_info *open_devip = NULL; |
| 3849 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3850 | |
FUJITA Tomonori | d1e4c9c | 2008-03-02 18:30:18 +0900 | [diff] [blame] | 3851 | sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host); |
| 3852 | if (!sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3853 | pr_err("Host info NULL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3854 | return NULL; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 3855 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3856 | list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) { |
| 3857 | if ((devip->used) && (devip->channel == sdev->channel) && |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 3858 | (devip->target == sdev->id) && |
| 3859 | (devip->lun == sdev->lun)) |
| 3860 | return devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3861 | else { |
| 3862 | if ((!devip->used) && (!open_devip)) |
| 3863 | open_devip = devip; |
| 3864 | } |
| 3865 | } |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3866 | if (!open_devip) { /* try and make a new one */ |
| 3867 | open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC); |
| 3868 | if (!open_devip) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3869 | pr_err("out of memory at line %d\n", __LINE__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3870 | return NULL; |
| 3871 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3872 | } |
FUJITA Tomonori | a75869d | 2008-03-20 11:09:17 +0900 | [diff] [blame] | 3873 | |
| 3874 | open_devip->channel = sdev->channel; |
| 3875 | open_devip->target = sdev->id; |
| 3876 | open_devip->lun = sdev->lun; |
| 3877 | open_devip->sdbg_host = sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3878 | atomic_set(&open_devip->num_in_q, 0); |
| 3879 | set_bit(SDEBUG_UA_POR, open_devip->uas_bm); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3880 | open_devip->used = true; |
FUJITA Tomonori | a75869d | 2008-03-20 11:09:17 +0900 | [diff] [blame] | 3881 | return open_devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3882 | } |
| 3883 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3884 | static int scsi_debug_slave_alloc(struct scsi_device *sdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3886 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3887 | pr_info("slave_alloc <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3888 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3889 | return 0; |
| 3890 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3892 | static int scsi_debug_slave_configure(struct scsi_device *sdp) |
| 3893 | { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3894 | struct sdebug_dev_info *devip = |
| 3895 | (struct sdebug_dev_info *)sdp->hostdata; |
FUJITA Tomonori | a34c4e9 | 2008-03-25 09:26:50 +0900 | [diff] [blame] | 3896 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3897 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3898 | pr_info("slave_configure <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3899 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3900 | if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN) |
| 3901 | sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN; |
| 3902 | if (devip == NULL) { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3903 | devip = find_build_dev_info(sdp); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3904 | if (devip == NULL) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3905 | return 1; /* no resources, will be marked offline */ |
| 3906 | } |
Christoph Hellwig | c8b09f6 | 2014-11-03 20:15:14 +0100 | [diff] [blame] | 3907 | sdp->hostdata = devip; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3908 | if (sdebug_no_uld) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 3909 | sdp->no_uld_attach = 1; |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 3910 | config_cdb_len(sdp); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3911 | return 0; |
| 3912 | } |
| 3913 | |
| 3914 | static void scsi_debug_slave_destroy(struct scsi_device *sdp) |
| 3915 | { |
| 3916 | struct sdebug_dev_info *devip = |
| 3917 | (struct sdebug_dev_info *)sdp->hostdata; |
| 3918 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3919 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3920 | pr_info("slave_destroy <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3921 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
| 3922 | if (devip) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3923 | /* make this slot available for re-use */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3924 | devip->used = false; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3925 | sdp->hostdata = NULL; |
| 3926 | } |
| 3927 | } |
| 3928 | |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3929 | static void stop_qc_helper(struct sdebug_defer *sd_dp, |
| 3930 | enum sdeb_defer_type defer_t) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3931 | { |
| 3932 | if (!sd_dp) |
| 3933 | return; |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3934 | if (defer_t == SDEB_DEFER_HRT) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3935 | hrtimer_cancel(&sd_dp->hrt); |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3936 | else if (defer_t == SDEB_DEFER_WQ) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3937 | cancel_work_sync(&sd_dp->ew.work); |
| 3938 | } |
| 3939 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3940 | /* If @cmnd found deletes its timer or work queue and returns true; else |
| 3941 | returns false */ |
| 3942 | static bool stop_queued_cmnd(struct scsi_cmnd *cmnd) |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3943 | { |
| 3944 | unsigned long iflags; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3945 | int j, k, qmax, r_qmax; |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3946 | enum sdeb_defer_type l_defer_t; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3947 | struct sdebug_queue *sqp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3948 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3949 | struct sdebug_dev_info *devip; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3950 | struct sdebug_defer *sd_dp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3951 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3952 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 3953 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 3954 | qmax = sdebug_max_queue; |
| 3955 | r_qmax = atomic_read(&retired_max_queue); |
| 3956 | if (r_qmax > qmax) |
| 3957 | qmax = r_qmax; |
| 3958 | for (k = 0; k < qmax; ++k) { |
| 3959 | if (test_bit(k, sqp->in_use_bm)) { |
| 3960 | sqcp = &sqp->qc_arr[k]; |
| 3961 | if (cmnd != sqcp->a_cmnd) |
| 3962 | continue; |
| 3963 | /* found */ |
| 3964 | devip = (struct sdebug_dev_info *) |
| 3965 | cmnd->device->hostdata; |
| 3966 | if (devip) |
| 3967 | atomic_dec(&devip->num_in_q); |
| 3968 | sqcp->a_cmnd = NULL; |
| 3969 | sd_dp = sqcp->sd_dp; |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3970 | if (sd_dp) { |
| 3971 | l_defer_t = sd_dp->defer_t; |
| 3972 | sd_dp->defer_t = SDEB_DEFER_NONE; |
| 3973 | } else |
| 3974 | l_defer_t = SDEB_DEFER_NONE; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3975 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3976 | stop_qc_helper(sd_dp, l_defer_t); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3977 | clear_bit(k, sqp->in_use_bm); |
| 3978 | return true; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3979 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3980 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3981 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3982 | } |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3983 | return false; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3984 | } |
| 3985 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3986 | /* Deletes (stops) timers or work queues of all queued commands */ |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3987 | static void stop_all_queued(void) |
| 3988 | { |
| 3989 | unsigned long iflags; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3990 | int j, k; |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3991 | enum sdeb_defer_type l_defer_t; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3992 | struct sdebug_queue *sqp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3993 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3994 | struct sdebug_dev_info *devip; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3995 | struct sdebug_defer *sd_dp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3996 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3997 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 3998 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 3999 | for (k = 0; k < SDEBUG_CANQUEUE; ++k) { |
| 4000 | if (test_bit(k, sqp->in_use_bm)) { |
| 4001 | sqcp = &sqp->qc_arr[k]; |
| 4002 | if (sqcp->a_cmnd == NULL) |
| 4003 | continue; |
| 4004 | devip = (struct sdebug_dev_info *) |
| 4005 | sqcp->a_cmnd->device->hostdata; |
| 4006 | if (devip) |
| 4007 | atomic_dec(&devip->num_in_q); |
| 4008 | sqcp->a_cmnd = NULL; |
| 4009 | sd_dp = sqcp->sd_dp; |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4010 | if (sd_dp) { |
| 4011 | l_defer_t = sd_dp->defer_t; |
| 4012 | sd_dp->defer_t = SDEB_DEFER_NONE; |
| 4013 | } else |
| 4014 | l_defer_t = SDEB_DEFER_NONE; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4015 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4016 | stop_qc_helper(sd_dp, l_defer_t); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4017 | clear_bit(k, sqp->in_use_bm); |
| 4018 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4019 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 4020 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4021 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 4022 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4023 | } |
| 4024 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4025 | /* Free queued command memory on heap */ |
| 4026 | static void free_all_queued(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4027 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4028 | int j, k; |
| 4029 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4030 | struct sdebug_queued_cmd *sqcp; |
| 4031 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4032 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 4033 | for (k = 0; k < SDEBUG_CANQUEUE; ++k) { |
| 4034 | sqcp = &sqp->qc_arr[k]; |
| 4035 | kfree(sqcp->sd_dp); |
| 4036 | sqcp->sd_dp = NULL; |
| 4037 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4038 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4039 | } |
| 4040 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4041 | static int scsi_debug_abort(struct scsi_cmnd *SCpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4043 | bool ok; |
| 4044 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4045 | ++num_aborts; |
| 4046 | if (SCpnt) { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4047 | ok = stop_queued_cmnd(SCpnt); |
| 4048 | if (SCpnt->device && (SDEBUG_OPT_ALL_NOISE & sdebug_opts)) |
| 4049 | sdev_printk(KERN_INFO, SCpnt->device, |
| 4050 | "%s: command%s found\n", __func__, |
| 4051 | ok ? "" : " not"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4053 | return SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | } |
| 4055 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 4056 | static int scsi_debug_device_reset(struct scsi_cmnd *SCpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4058 | ++num_dev_resets; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4059 | if (SCpnt && SCpnt->device) { |
| 4060 | struct scsi_device *sdp = SCpnt->device; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4061 | struct sdebug_dev_info *devip = |
| 4062 | (struct sdebug_dev_info *)sdp->hostdata; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4063 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4064 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4065 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4066 | if (devip) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4067 | set_bit(SDEBUG_UA_POR, devip->uas_bm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4068 | } |
| 4069 | return SUCCESS; |
| 4070 | } |
| 4071 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4072 | static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt) |
| 4073 | { |
| 4074 | struct sdebug_host_info *sdbg_host; |
| 4075 | struct sdebug_dev_info *devip; |
| 4076 | struct scsi_device *sdp; |
| 4077 | struct Scsi_Host *hp; |
| 4078 | int k = 0; |
| 4079 | |
| 4080 | ++num_target_resets; |
| 4081 | if (!SCpnt) |
| 4082 | goto lie; |
| 4083 | sdp = SCpnt->device; |
| 4084 | if (!sdp) |
| 4085 | goto lie; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4086 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4087 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
| 4088 | hp = sdp->host; |
| 4089 | if (!hp) |
| 4090 | goto lie; |
| 4091 | sdbg_host = *(struct sdebug_host_info **)shost_priv(hp); |
| 4092 | if (sdbg_host) { |
| 4093 | list_for_each_entry(devip, |
| 4094 | &sdbg_host->dev_info_list, |
| 4095 | dev_list) |
| 4096 | if (devip->target == sdp->id) { |
| 4097 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 4098 | ++k; |
| 4099 | } |
| 4100 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4101 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4102 | sdev_printk(KERN_INFO, sdp, |
| 4103 | "%s: %d device(s) found in target\n", __func__, k); |
| 4104 | lie: |
| 4105 | return SUCCESS; |
| 4106 | } |
| 4107 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 4108 | static int scsi_debug_bus_reset(struct scsi_cmnd *SCpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4109 | { |
| 4110 | struct sdebug_host_info *sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4111 | struct sdebug_dev_info *devip; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4112 | struct scsi_device *sdp; |
| 4113 | struct Scsi_Host *hp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4114 | int k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | ++num_bus_resets; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4117 | if (!(SCpnt && SCpnt->device)) |
| 4118 | goto lie; |
| 4119 | sdp = SCpnt->device; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4120 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4121 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
| 4122 | hp = sdp->host; |
| 4123 | if (hp) { |
FUJITA Tomonori | d1e4c9c | 2008-03-02 18:30:18 +0900 | [diff] [blame] | 4124 | sdbg_host = *(struct sdebug_host_info **)shost_priv(hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | if (sdbg_host) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4126 | list_for_each_entry(devip, |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4127 | &sdbg_host->dev_info_list, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4128 | dev_list) { |
| 4129 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 4130 | ++k; |
| 4131 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4132 | } |
| 4133 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4134 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4135 | sdev_printk(KERN_INFO, sdp, |
| 4136 | "%s: %d device(s) found in host\n", __func__, k); |
| 4137 | lie: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4138 | return SUCCESS; |
| 4139 | } |
| 4140 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 4141 | static int scsi_debug_host_reset(struct scsi_cmnd *SCpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | { |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 4143 | struct sdebug_host_info *sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4144 | struct sdebug_dev_info *devip; |
| 4145 | int k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4147 | ++num_host_resets; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4148 | if ((SCpnt->device) && (SDEBUG_OPT_ALL_NOISE & sdebug_opts)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4149 | sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4150 | spin_lock(&sdebug_host_list_lock); |
| 4151 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4152 | list_for_each_entry(devip, &sdbg_host->dev_info_list, |
| 4153 | dev_list) { |
| 4154 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 4155 | ++k; |
| 4156 | } |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4157 | } |
| 4158 | spin_unlock(&sdebug_host_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | stop_all_queued(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4160 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4161 | sdev_printk(KERN_INFO, SCpnt->device, |
| 4162 | "%s: %d device(s) found\n", __func__, k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | return SUCCESS; |
| 4164 | } |
| 4165 | |
FUJITA Tomonori | f58b0ef | 2008-03-30 00:59:54 +0900 | [diff] [blame] | 4166 | static void __init sdebug_build_parts(unsigned char *ramp, |
FUJITA Tomonori | 5f2578e | 2008-03-30 00:59:57 +0900 | [diff] [blame] | 4167 | unsigned long store_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4168 | { |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 4169 | struct partition *pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | int starts[SDEBUG_MAX_PARTS + 2]; |
| 4171 | int sectors_per_part, num_sectors, k; |
| 4172 | int heads_by_sects, start_sec, end_sec; |
| 4173 | |
| 4174 | /* assume partition table already zeroed */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4175 | if ((sdebug_num_parts < 1) || (store_size < 1048576)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4176 | return; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4177 | if (sdebug_num_parts > SDEBUG_MAX_PARTS) { |
| 4178 | sdebug_num_parts = SDEBUG_MAX_PARTS; |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4179 | pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4181 | num_sectors = (int)sdebug_store_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | sectors_per_part = (num_sectors - sdebug_sectors_per) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4183 | / sdebug_num_parts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | heads_by_sects = sdebug_heads * sdebug_sectors_per; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4185 | starts[0] = sdebug_sectors_per; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4186 | for (k = 1; k < sdebug_num_parts; ++k) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | starts[k] = ((k * sectors_per_part) / heads_by_sects) |
| 4188 | * heads_by_sects; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4189 | starts[sdebug_num_parts] = num_sectors; |
| 4190 | starts[sdebug_num_parts + 1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | |
| 4192 | ramp[510] = 0x55; /* magic partition markings */ |
| 4193 | ramp[511] = 0xAA; |
| 4194 | pp = (struct partition *)(ramp + 0x1be); |
| 4195 | for (k = 0; starts[k + 1]; ++k, ++pp) { |
| 4196 | start_sec = starts[k]; |
| 4197 | end_sec = starts[k + 1] - 1; |
| 4198 | pp->boot_ind = 0; |
| 4199 | |
| 4200 | pp->cyl = start_sec / heads_by_sects; |
| 4201 | pp->head = (start_sec - (pp->cyl * heads_by_sects)) |
| 4202 | / sdebug_sectors_per; |
| 4203 | pp->sector = (start_sec % sdebug_sectors_per) + 1; |
| 4204 | |
| 4205 | pp->end_cyl = end_sec / heads_by_sects; |
| 4206 | pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects)) |
| 4207 | / sdebug_sectors_per; |
| 4208 | pp->end_sector = (end_sec % sdebug_sectors_per) + 1; |
| 4209 | |
Akinobu Mita | 150c354 | 2013-08-26 22:08:40 +0900 | [diff] [blame] | 4210 | pp->start_sect = cpu_to_le32(start_sec); |
| 4211 | pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4212 | pp->sys_ind = 0x83; /* plain Linux partition */ |
| 4213 | } |
| 4214 | } |
| 4215 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4216 | static void block_unblock_all_queues(bool block) |
| 4217 | { |
| 4218 | int j; |
| 4219 | struct sdebug_queue *sqp; |
| 4220 | |
| 4221 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) |
| 4222 | atomic_set(&sqp->blocked, (int)block); |
| 4223 | } |
| 4224 | |
| 4225 | /* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1 |
| 4226 | * commands will be processed normally before triggers occur. |
| 4227 | */ |
| 4228 | static void tweak_cmnd_count(void) |
| 4229 | { |
| 4230 | int count, modulo; |
| 4231 | |
| 4232 | modulo = abs(sdebug_every_nth); |
| 4233 | if (modulo < 2) |
| 4234 | return; |
| 4235 | block_unblock_all_queues(true); |
| 4236 | count = atomic_read(&sdebug_cmnd_count); |
| 4237 | atomic_set(&sdebug_cmnd_count, (count / modulo) * modulo); |
| 4238 | block_unblock_all_queues(false); |
| 4239 | } |
| 4240 | |
| 4241 | static void clear_queue_stats(void) |
| 4242 | { |
| 4243 | atomic_set(&sdebug_cmnd_count, 0); |
| 4244 | atomic_set(&sdebug_completions, 0); |
| 4245 | atomic_set(&sdebug_miss_cpus, 0); |
| 4246 | atomic_set(&sdebug_a_tsf, 0); |
| 4247 | } |
| 4248 | |
| 4249 | static void setup_inject(struct sdebug_queue *sqp, |
| 4250 | struct sdebug_queued_cmd *sqcp) |
| 4251 | { |
Martin Wilck | f9ba7af | 2018-01-30 00:35:52 +0100 | [diff] [blame] | 4252 | if ((atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) > 0) { |
| 4253 | if (sdebug_every_nth > 0) |
| 4254 | sqcp->inj_recovered = sqcp->inj_transport |
| 4255 | = sqcp->inj_dif |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 4256 | = sqcp->inj_dix = sqcp->inj_short |
| 4257 | = sqcp->inj_host_busy = sqcp->inj_cmd_abort = 0; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4258 | return; |
Martin Wilck | f9ba7af | 2018-01-30 00:35:52 +0100 | [diff] [blame] | 4259 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4260 | sqcp->inj_recovered = !!(SDEBUG_OPT_RECOVERED_ERR & sdebug_opts); |
| 4261 | sqcp->inj_transport = !!(SDEBUG_OPT_TRANSPORT_ERR & sdebug_opts); |
| 4262 | sqcp->inj_dif = !!(SDEBUG_OPT_DIF_ERR & sdebug_opts); |
| 4263 | sqcp->inj_dix = !!(SDEBUG_OPT_DIX_ERR & sdebug_opts); |
| 4264 | sqcp->inj_short = !!(SDEBUG_OPT_SHORT_TRANSFER & sdebug_opts); |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 4265 | sqcp->inj_host_busy = !!(SDEBUG_OPT_HOST_BUSY & sdebug_opts); |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 4266 | sqcp->inj_cmd_abort = !!(SDEBUG_OPT_CMD_ABORT & sdebug_opts); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | /* Complete the processing of the thread that queued a SCSI command to this |
| 4270 | * driver. It either completes the command by calling cmnd_done() or |
| 4271 | * schedules a hr timer or work queue then returns 0. Returns |
| 4272 | * SCSI_MLQUEUE_HOST_BUSY if temporarily out of resources. |
| 4273 | */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4274 | static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip, |
Martin Wilck | f66b851 | 2018-02-14 11:05:57 +0100 | [diff] [blame] | 4275 | int scsi_result, |
| 4276 | int (*pfp)(struct scsi_cmnd *, |
| 4277 | struct sdebug_dev_info *), |
| 4278 | int delta_jiff, int ndelay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4280 | unsigned long iflags; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4281 | int k, num_in_q, qdepth, inject; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4282 | struct sdebug_queue *sqp; |
| 4283 | struct sdebug_queued_cmd *sqcp; |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 4284 | struct scsi_device *sdp; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4285 | struct sdebug_defer *sd_dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4287 | if (unlikely(devip == NULL)) { |
| 4288 | if (scsi_result == 0) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4289 | scsi_result = DID_NO_CONNECT << 16; |
| 4290 | goto respond_in_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | } |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 4292 | sdp = cmnd->device; |
| 4293 | |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4294 | if (delta_jiff == 0) |
| 4295 | goto respond_in_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4297 | /* schedule the response at a later time if resources permit */ |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4298 | sqp = get_queue(cmnd); |
| 4299 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 4300 | if (unlikely(atomic_read(&sqp->blocked))) { |
| 4301 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 4302 | return SCSI_MLQUEUE_HOST_BUSY; |
| 4303 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4304 | num_in_q = atomic_read(&devip->num_in_q); |
| 4305 | qdepth = cmnd->device->queue_depth; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4306 | inject = 0; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4307 | if (unlikely((qdepth > 0) && (num_in_q >= qdepth))) { |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4308 | if (scsi_result) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4309 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4310 | goto respond_in_thread; |
| 4311 | } else |
| 4312 | scsi_result = device_qfull_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4313 | } else if (unlikely(sdebug_every_nth && |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4314 | (SDEBUG_OPT_RARE_TSF & sdebug_opts) && |
| 4315 | (scsi_result == 0))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4316 | if ((num_in_q == (qdepth - 1)) && |
| 4317 | (atomic_inc_return(&sdebug_a_tsf) >= |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4318 | abs(sdebug_every_nth))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4319 | atomic_set(&sdebug_a_tsf, 0); |
| 4320 | inject = 1; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4321 | scsi_result = device_qfull_result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4323 | } |
| 4324 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4325 | k = find_first_zero_bit(sqp->in_use_bm, sdebug_max_queue); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4326 | if (unlikely(k >= sdebug_max_queue)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4327 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4328 | if (scsi_result) |
| 4329 | goto respond_in_thread; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4330 | else if (SDEBUG_OPT_ALL_TSF & sdebug_opts) |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4331 | scsi_result = device_qfull_result; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4332 | if (SDEBUG_OPT_Q_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4333 | sdev_printk(KERN_INFO, sdp, |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4334 | "%s: max_queue=%d exceeded, %s\n", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4335 | __func__, sdebug_max_queue, |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4336 | (scsi_result ? "status: TASK SET FULL" : |
| 4337 | "report: host busy")); |
| 4338 | if (scsi_result) |
| 4339 | goto respond_in_thread; |
| 4340 | else |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4341 | return SCSI_MLQUEUE_HOST_BUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4343 | __set_bit(k, sqp->in_use_bm); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4344 | atomic_inc(&devip->num_in_q); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4345 | sqcp = &sqp->qc_arr[k]; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4346 | sqcp->a_cmnd = cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4347 | cmnd->host_scribble = (unsigned char *)sqcp; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4348 | sd_dp = sqcp->sd_dp; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4349 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 4350 | if (unlikely(sdebug_every_nth && sdebug_any_injecting_opt)) |
| 4351 | setup_inject(sqp, sqcp); |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4352 | if (sd_dp == NULL) { |
| 4353 | sd_dp = kzalloc(sizeof(*sd_dp), GFP_ATOMIC); |
| 4354 | if (sd_dp == NULL) |
| 4355 | return SCSI_MLQUEUE_HOST_BUSY; |
| 4356 | } |
Martin Wilck | f66b851 | 2018-02-14 11:05:57 +0100 | [diff] [blame] | 4357 | |
| 4358 | cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0; |
| 4359 | if (cmnd->result & SDEG_RES_IMMED_MASK) { |
| 4360 | /* |
| 4361 | * This is the F_DELAY_OVERR case. No delay. |
| 4362 | */ |
| 4363 | cmnd->result &= ~SDEG_RES_IMMED_MASK; |
| 4364 | delta_jiff = ndelay = 0; |
| 4365 | } |
| 4366 | if (cmnd->result == 0 && scsi_result != 0) |
| 4367 | cmnd->result = scsi_result; |
| 4368 | |
| 4369 | if (unlikely(sdebug_verbose && cmnd->result)) |
| 4370 | sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n", |
| 4371 | __func__, cmnd->result); |
| 4372 | |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4373 | if (delta_jiff > 0 || ndelay > 0) { |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 4374 | ktime_t kt; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4375 | |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 4376 | if (delta_jiff > 0) { |
Arnd Bergmann | 13f6b61 | 2017-11-27 12:36:25 +0100 | [diff] [blame] | 4377 | kt = ns_to_ktime((u64)delta_jiff * (NSEC_PER_SEC / HZ)); |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 4378 | } else |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4379 | kt = ndelay; |
| 4380 | if (!sd_dp->init_hrt) { |
| 4381 | sd_dp->init_hrt = true; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4382 | sqcp->sd_dp = sd_dp; |
| 4383 | hrtimer_init(&sd_dp->hrt, CLOCK_MONOTONIC, |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4384 | HRTIMER_MODE_REL_PINNED); |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4385 | sd_dp->hrt.function = sdebug_q_cmd_hrt_complete; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4386 | sd_dp->sqa_idx = sqp - sdebug_q_arr; |
| 4387 | sd_dp->qc_idx = k; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4388 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4389 | if (sdebug_statistics) |
| 4390 | sd_dp->issuing_cpu = raw_smp_processor_id(); |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4391 | sd_dp->defer_t = SDEB_DEFER_HRT; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4392 | hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL_PINNED); |
| 4393 | } else { /* jdelay < 0, use work queue */ |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4394 | if (!sd_dp->init_wq) { |
| 4395 | sd_dp->init_wq = true; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4396 | sqcp->sd_dp = sd_dp; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4397 | sd_dp->sqa_idx = sqp - sdebug_q_arr; |
| 4398 | sd_dp->qc_idx = k; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4399 | INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4400 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4401 | if (sdebug_statistics) |
| 4402 | sd_dp->issuing_cpu = raw_smp_processor_id(); |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4403 | sd_dp->defer_t = SDEB_DEFER_WQ; |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 4404 | if (unlikely(sqcp->inj_cmd_abort)) |
| 4405 | sd_dp->aborted = true; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4406 | schedule_work(&sd_dp->ew.work); |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 4407 | if (unlikely(sqcp->inj_cmd_abort)) { |
| 4408 | sdev_printk(KERN_INFO, sdp, "abort request tag %d\n", |
| 4409 | cmnd->request->tag); |
| 4410 | blk_abort_request(cmnd->request); |
| 4411 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4412 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4413 | if (unlikely((SDEBUG_OPT_Q_NOISE & sdebug_opts) && |
| 4414 | (scsi_result == device_qfull_result))) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4415 | sdev_printk(KERN_INFO, sdp, |
| 4416 | "%s: num_in_q=%d +1, %s%s\n", __func__, |
| 4417 | num_in_q, (inject ? "<inject> " : ""), |
| 4418 | "status: TASK SET FULL"); |
| 4419 | return 0; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4420 | |
| 4421 | respond_in_thread: /* call back to mid-layer using invocation thread */ |
Martin Wilck | f66b851 | 2018-02-14 11:05:57 +0100 | [diff] [blame] | 4422 | cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0; |
| 4423 | cmnd->result &= ~SDEG_RES_IMMED_MASK; |
| 4424 | if (cmnd->result == 0 && scsi_result != 0) |
| 4425 | cmnd->result = scsi_result; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4426 | cmnd->scsi_done(cmnd); |
| 4427 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4429 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4430 | /* Note: The following macros create attribute files in the |
| 4431 | /sys/module/scsi_debug/parameters directory. Unfortunately this |
| 4432 | driver is unaware of a change and cannot trigger auxiliary actions |
| 4433 | as it can when the corresponding attribute in the |
| 4434 | /sys/bus/pseudo/drivers/scsi_debug directory is changed. |
| 4435 | */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4436 | module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR); |
| 4437 | module_param_named(ato, sdebug_ato, int, S_IRUGO); |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 4438 | module_param_named(cdb_len, sdebug_cdb_len, int, 0644); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4439 | module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR); |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4440 | module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4441 | module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO); |
| 4442 | module_param_named(dif, sdebug_dif, int, S_IRUGO); |
| 4443 | module_param_named(dix, sdebug_dix, int, S_IRUGO); |
| 4444 | module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR); |
| 4445 | module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR); |
| 4446 | module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR); |
| 4447 | module_param_named(guard, sdebug_guard, uint, S_IRUGO); |
| 4448 | module_param_named(host_lock, sdebug_host_lock, bool, S_IRUGO | S_IWUSR); |
Hannes Reinecke | e5203cf | 2017-10-02 16:26:33 +0200 | [diff] [blame] | 4449 | module_param_string(inq_vendor, sdebug_inq_vendor_id, |
| 4450 | sizeof(sdebug_inq_vendor_id), S_IRUGO|S_IWUSR); |
| 4451 | module_param_string(inq_product, sdebug_inq_product_id, |
| 4452 | sizeof(sdebug_inq_product_id), S_IRUGO|S_IWUSR); |
| 4453 | module_param_string(inq_rev, sdebug_inq_product_rev, |
| 4454 | sizeof(sdebug_inq_product_rev), S_IRUGO|S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4455 | module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO); |
| 4456 | module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO); |
| 4457 | module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO); |
| 4458 | module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO); |
| 4459 | module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO); |
| 4460 | module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR); |
| 4461 | module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR); |
Laurence Oberman | d9da891 | 2018-02-03 13:38:35 -0500 | [diff] [blame] | 4462 | module_param_named(medium_error_start, sdebug_medium_error_start, int, S_IRUGO | S_IWUSR); |
| 4463 | module_param_named(medium_error_count, sdebug_medium_error_count, int, S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4464 | module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR); |
| 4465 | module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR); |
| 4466 | module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO); |
| 4467 | module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO); |
| 4468 | module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR); |
| 4469 | module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO); |
| 4470 | module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR); |
| 4471 | module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO); |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 4472 | module_param_named(opt_xferlen_exp, sdebug_opt_xferlen_exp, int, S_IRUGO); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4473 | module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR); |
| 4474 | module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR); |
| 4475 | module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO); |
| 4476 | module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4477 | module_param_named(statistics, sdebug_statistics, bool, S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4478 | module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4479 | module_param_named(submit_queues, submit_queues, int, S_IRUGO); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4480 | module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO); |
| 4481 | module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO); |
| 4482 | module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO); |
| 4483 | module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO); |
| 4484 | module_param_named(virtual_gb, sdebug_virtual_gb, int, S_IRUGO | S_IWUSR); |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 4485 | module_param_named(uuid_ctl, sdebug_uuid_ctl, int, S_IRUGO); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4486 | module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int, |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4487 | S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4488 | module_param_named(write_same_length, sdebug_write_same_length, int, |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4489 | S_IRUGO | S_IWUSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | |
| 4491 | MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert"); |
| 4492 | MODULE_DESCRIPTION("SCSI debug adapter driver"); |
| 4493 | MODULE_LICENSE("GPL"); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4494 | MODULE_VERSION(SDEBUG_VERSION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | |
| 4496 | MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4497 | MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)"); |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 4498 | MODULE_PARM_DESC(cdb_len, "suggest CDB lengths to drivers (def=10)"); |
Akinobu Mita | 0759c66 | 2014-02-26 22:57:04 +0900 | [diff] [blame] | 4499 | MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4500 | MODULE_PARM_DESC(delay, "response delay (def=1 jiffy); 0:imm, -1,-2:tiny"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4501 | MODULE_PARM_DESC(dev_size_mb, "size in MiB of ram shared by devs(def=8)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4502 | MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)"); |
| 4503 | MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4504 | MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)"); |
Randy Dunlap | beb87c3 | 2007-06-11 11:36:40 -0700 | [diff] [blame] | 4505 | MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)"); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4506 | MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4507 | MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)"); |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 4508 | MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)"); |
Hannes Reinecke | e5203cf | 2017-10-02 16:26:33 +0200 | [diff] [blame] | 4509 | MODULE_PARM_DESC(inq_vendor, "SCSI INQUIRY vendor string (def=\"Linux\")"); |
| 4510 | MODULE_PARM_DESC(inq_product, "SCSI INQUIRY product string (def=\"scsi_debug\")"); |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 4511 | MODULE_PARM_DESC(inq_rev, "SCSI INQUIRY revision string (def=\"" |
| 4512 | SDEBUG_VERSION "\")"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4513 | MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)"); |
| 4514 | MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)"); |
| 4515 | MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)"); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 4516 | MODULE_PARM_DESC(lbprz, |
| 4517 | "on read unmapped LBs return 0 when 1 (def), return 0xff when 2"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4518 | MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4519 | MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4520 | MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))"); |
Laurence Oberman | d9da891 | 2018-02-03 13:38:35 -0500 | [diff] [blame] | 4521 | MODULE_PARM_DESC(medium_error_start, "starting sector number to return MEDIUM error"); |
| 4522 | MODULE_PARM_DESC(medium_error_count, "count of sectors to return follow on MEDIUM error"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4523 | MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4524 | MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)"); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4525 | MODULE_PARM_DESC(no_uld, "stop ULD (e.g. sd driver) attaching (def=0))"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | MODULE_PARM_DESC(num_parts, "number of partitions(def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4527 | MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)"); |
Martin K. Petersen | 32c5844 | 2015-12-16 17:53:51 -0500 | [diff] [blame] | 4528 | MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)"); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 4529 | MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4530 | MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)"); |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 4531 | MODULE_PARM_DESC(opt_xferlen_exp, "optimal transfer length granularity exponent (def=physblk_exp)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])"); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4533 | MODULE_PARM_DESC(removable, "claim to have removable media (def=0)"); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 4534 | MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=7[SPC-5])"); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 4535 | MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4536 | MODULE_PARM_DESC(statistics, "collect statistics on commands, queues (def=0)"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4537 | MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4538 | MODULE_PARM_DESC(submit_queues, "support for block multi-queue (def=1)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4539 | MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)"); |
| 4540 | MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)"); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 4541 | MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)"); |
| 4542 | MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)"); |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 4543 | MODULE_PARM_DESC(uuid_ctl, |
| 4544 | "1->use uuid for lu name, 0->don't, 2->all use same (def=0)"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4545 | MODULE_PARM_DESC(virtual_gb, "virtual gigabyte (GiB) size (def=0 -> use dev_size_mb)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4546 | MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)"); |
| 4547 | MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 4549 | #define SDEBUG_INFO_LEN 256 |
| 4550 | static char sdebug_info[SDEBUG_INFO_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 4552 | static const char *scsi_debug_info(struct Scsi_Host *shp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4553 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4554 | int k; |
| 4555 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 4556 | k = scnprintf(sdebug_info, SDEBUG_INFO_LEN, "%s: version %s [%s]\n", |
| 4557 | my_name, SDEBUG_VERSION, sdebug_version_date); |
| 4558 | if (k >= (SDEBUG_INFO_LEN - 1)) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4559 | return sdebug_info; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 4560 | scnprintf(sdebug_info + k, SDEBUG_INFO_LEN - k, |
| 4561 | " dev_size_mb=%d, opts=0x%x, submit_queues=%d, %s=%d", |
| 4562 | sdebug_dev_size_mb, sdebug_opts, submit_queues, |
| 4563 | "statistics", (int)sdebug_statistics); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4564 | return sdebug_info; |
| 4565 | } |
| 4566 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4567 | /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4568 | static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer, |
| 4569 | int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | { |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4571 | char arr[16]; |
| 4572 | int opts; |
| 4573 | int minLen = length > 15 ? 15 : length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 | |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4575 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 4576 | return -EACCES; |
| 4577 | memcpy(arr, buffer, minLen); |
| 4578 | arr[minLen] = '\0'; |
| 4579 | if (1 != sscanf(arr, "%d", &opts)) |
| 4580 | return -EINVAL; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4581 | sdebug_opts = opts; |
| 4582 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); |
| 4583 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts); |
| 4584 | if (sdebug_every_nth != 0) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4585 | tweak_cmnd_count(); |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4586 | return length; |
| 4587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4589 | /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the |
| 4590 | * same for each scsi_debug host (if more than one). Some of the counters |
| 4591 | * output are not atomics so might be inaccurate in a busy system. */ |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4592 | static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host) |
| 4593 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4594 | int f, j, l; |
| 4595 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4596 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4597 | seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n", |
| 4598 | SDEBUG_VERSION, sdebug_version_date); |
| 4599 | seq_printf(m, "num_tgts=%d, %ssize=%d MB, opts=0x%x, every_nth=%d\n", |
| 4600 | sdebug_num_tgts, "shared (ram) ", sdebug_dev_size_mb, |
| 4601 | sdebug_opts, sdebug_every_nth); |
| 4602 | seq_printf(m, "delay=%d, ndelay=%d, max_luns=%d, sector_size=%d %s\n", |
| 4603 | sdebug_jdelay, sdebug_ndelay, sdebug_max_luns, |
| 4604 | sdebug_sector_size, "bytes"); |
| 4605 | seq_printf(m, "cylinders=%d, heads=%d, sectors=%d, command aborts=%d\n", |
| 4606 | sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per, |
| 4607 | num_aborts); |
| 4608 | seq_printf(m, "RESETs: device=%d, target=%d, bus=%d, host=%d\n", |
| 4609 | num_dev_resets, num_target_resets, num_bus_resets, |
| 4610 | num_host_resets); |
| 4611 | seq_printf(m, "dix_reads=%d, dix_writes=%d, dif_errors=%d\n", |
| 4612 | dix_reads, dix_writes, dif_errors); |
Bart Van Assche | 458df78 | 2018-01-26 08:52:19 -0800 | [diff] [blame] | 4613 | seq_printf(m, "usec_in_jiffy=%lu, statistics=%d\n", TICK_NSEC / 1000, |
| 4614 | sdebug_statistics); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4615 | seq_printf(m, "cmnd_count=%d, completions=%d, %s=%d, a_tsf=%d\n", |
| 4616 | atomic_read(&sdebug_cmnd_count), |
| 4617 | atomic_read(&sdebug_completions), |
| 4618 | "miss_cpus", atomic_read(&sdebug_miss_cpus), |
| 4619 | atomic_read(&sdebug_a_tsf)); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4620 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4621 | seq_printf(m, "submit_queues=%d\n", submit_queues); |
| 4622 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 4623 | seq_printf(m, " queue %d:\n", j); |
| 4624 | f = find_first_bit(sqp->in_use_bm, sdebug_max_queue); |
| 4625 | if (f != sdebug_max_queue) { |
| 4626 | l = find_last_bit(sqp->in_use_bm, sdebug_max_queue); |
| 4627 | seq_printf(m, " in_use_bm BUSY: %s: %d,%d\n", |
| 4628 | "first,last bits", f, l); |
| 4629 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4630 | } |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4631 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4632 | } |
| 4633 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4634 | static ssize_t delay_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | { |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4636 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4637 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4638 | /* Returns -EBUSY if jdelay is being changed and commands are queued. The unit |
| 4639 | * of delay is jiffies. |
| 4640 | */ |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4641 | static ssize_t delay_store(struct device_driver *ddp, const char *buf, |
| 4642 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | { |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4644 | int jdelay, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4646 | if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4647 | res = count; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4648 | if (sdebug_jdelay != jdelay) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4649 | int j, k; |
| 4650 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4651 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4652 | block_unblock_all_queues(true); |
| 4653 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; |
| 4654 | ++j, ++sqp) { |
| 4655 | k = find_first_bit(sqp->in_use_bm, |
| 4656 | sdebug_max_queue); |
| 4657 | if (k != sdebug_max_queue) { |
| 4658 | res = -EBUSY; /* queued commands */ |
| 4659 | break; |
| 4660 | } |
| 4661 | } |
| 4662 | if (res > 0) { |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4663 | sdebug_jdelay = jdelay; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4664 | sdebug_ndelay = 0; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4665 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4666 | block_unblock_all_queues(false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4668 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | } |
| 4670 | return -EINVAL; |
| 4671 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4672 | static DRIVER_ATTR_RW(delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4674 | static ssize_t ndelay_show(struct device_driver *ddp, char *buf) |
| 4675 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4676 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4677 | } |
| 4678 | /* Returns -EBUSY if ndelay is being changed and commands are queued */ |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4679 | /* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4680 | static ssize_t ndelay_store(struct device_driver *ddp, const char *buf, |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4681 | size_t count) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4682 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4683 | int ndelay, res; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4684 | |
| 4685 | if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) && |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4686 | (ndelay >= 0) && (ndelay < (1000 * 1000 * 1000))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4687 | res = count; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4688 | if (sdebug_ndelay != ndelay) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4689 | int j, k; |
| 4690 | struct sdebug_queue *sqp; |
| 4691 | |
| 4692 | block_unblock_all_queues(true); |
| 4693 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; |
| 4694 | ++j, ++sqp) { |
| 4695 | k = find_first_bit(sqp->in_use_bm, |
| 4696 | sdebug_max_queue); |
| 4697 | if (k != sdebug_max_queue) { |
| 4698 | res = -EBUSY; /* queued commands */ |
| 4699 | break; |
| 4700 | } |
| 4701 | } |
| 4702 | if (res > 0) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4703 | sdebug_ndelay = ndelay; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4704 | sdebug_jdelay = ndelay ? JDELAY_OVERRIDDEN |
| 4705 | : DEF_JDELAY; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4706 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4707 | block_unblock_all_queues(false); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4708 | } |
| 4709 | return res; |
| 4710 | } |
| 4711 | return -EINVAL; |
| 4712 | } |
| 4713 | static DRIVER_ATTR_RW(ndelay); |
| 4714 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4715 | static ssize_t opts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4716 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4717 | return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | } |
| 4719 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4720 | static ssize_t opts_store(struct device_driver *ddp, const char *buf, |
| 4721 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4723 | int opts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | char work[20]; |
| 4725 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4726 | if (sscanf(buf, "%10s", work) == 1) { |
| 4727 | if (strncasecmp(work, "0x", 2) == 0) { |
| 4728 | if (kstrtoint(work + 2, 16, &opts) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4729 | goto opts_done; |
| 4730 | } else { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4731 | if (kstrtoint(work, 10, &opts) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4732 | goto opts_done; |
| 4733 | } |
| 4734 | } |
| 4735 | return -EINVAL; |
| 4736 | opts_done: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4737 | sdebug_opts = opts; |
| 4738 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); |
| 4739 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4740 | tweak_cmnd_count(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4741 | return count; |
| 4742 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4743 | static DRIVER_ATTR_RW(opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4744 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4745 | static ssize_t ptype_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4747 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4749 | static ssize_t ptype_store(struct device_driver *ddp, const char *buf, |
| 4750 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4751 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4752 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | |
| 4754 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4755 | sdebug_ptype = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | return count; |
| 4757 | } |
| 4758 | return -EINVAL; |
| 4759 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4760 | static DRIVER_ATTR_RW(ptype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4762 | static ssize_t dsense_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4763 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4764 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4765 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4766 | static ssize_t dsense_store(struct device_driver *ddp, const char *buf, |
| 4767 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4768 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4769 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | |
| 4771 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4772 | sdebug_dsense = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4773 | return count; |
| 4774 | } |
| 4775 | return -EINVAL; |
| 4776 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4777 | static DRIVER_ATTR_RW(dsense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4778 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4779 | static ssize_t fake_rw_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4780 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4781 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4782 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4783 | static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf, |
| 4784 | size_t count) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4785 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4786 | int n; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4787 | |
| 4788 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4789 | n = (n > 0); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4790 | sdebug_fake_rw = (sdebug_fake_rw > 0); |
| 4791 | if (sdebug_fake_rw != n) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4792 | if ((0 == n) && (NULL == fake_storep)) { |
| 4793 | unsigned long sz = |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4794 | (unsigned long)sdebug_dev_size_mb * |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4795 | 1048576; |
| 4796 | |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 4797 | fake_storep = vzalloc(sz); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4798 | if (NULL == fake_storep) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4799 | pr_err("out of memory, 9\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4800 | return -ENOMEM; |
| 4801 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4802 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4803 | sdebug_fake_rw = n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4804 | } |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4805 | return count; |
| 4806 | } |
| 4807 | return -EINVAL; |
| 4808 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4809 | static DRIVER_ATTR_RW(fake_rw); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4810 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4811 | static ssize_t no_lun_0_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4812 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4813 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4814 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4815 | static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf, |
| 4816 | size_t count) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4817 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4818 | int n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4819 | |
| 4820 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4821 | sdebug_no_lun_0 = n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4822 | return count; |
| 4823 | } |
| 4824 | return -EINVAL; |
| 4825 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4826 | static DRIVER_ATTR_RW(no_lun_0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4827 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4828 | static ssize_t num_tgts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4830 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4832 | static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf, |
| 4833 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4835 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | |
| 4837 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4838 | sdebug_num_tgts = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | sdebug_max_tgts_luns(); |
| 4840 | return count; |
| 4841 | } |
| 4842 | return -EINVAL; |
| 4843 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4844 | static DRIVER_ATTR_RW(num_tgts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4846 | static ssize_t dev_size_mb_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4848 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4849 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4850 | static DRIVER_ATTR_RO(dev_size_mb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4852 | static ssize_t num_parts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4854 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4855 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4856 | static DRIVER_ATTR_RO(num_parts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4858 | static ssize_t every_nth_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4860 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4862 | static ssize_t every_nth_store(struct device_driver *ddp, const char *buf, |
| 4863 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4865 | int nth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4866 | |
| 4867 | if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4868 | sdebug_every_nth = nth; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4869 | if (nth && !sdebug_statistics) { |
| 4870 | pr_info("every_nth needs statistics=1, set it\n"); |
| 4871 | sdebug_statistics = true; |
| 4872 | } |
| 4873 | tweak_cmnd_count(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | return count; |
| 4875 | } |
| 4876 | return -EINVAL; |
| 4877 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4878 | static DRIVER_ATTR_RW(every_nth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4880 | static ssize_t max_luns_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4881 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4882 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4884 | static ssize_t max_luns_store(struct device_driver *ddp, const char *buf, |
| 4885 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4886 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4887 | int n; |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 4888 | bool changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | |
| 4890 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 4891 | if (n > 256) { |
| 4892 | pr_warn("max_luns can be no more than 256\n"); |
| 4893 | return -EINVAL; |
| 4894 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4895 | changed = (sdebug_max_luns != n); |
| 4896 | sdebug_max_luns = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4897 | sdebug_max_tgts_luns(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4898 | if (changed && (sdebug_scsi_level >= 5)) { /* >= SPC-3 */ |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 4899 | struct sdebug_host_info *sdhp; |
| 4900 | struct sdebug_dev_info *dp; |
| 4901 | |
| 4902 | spin_lock(&sdebug_host_list_lock); |
| 4903 | list_for_each_entry(sdhp, &sdebug_host_list, |
| 4904 | host_list) { |
| 4905 | list_for_each_entry(dp, &sdhp->dev_info_list, |
| 4906 | dev_list) { |
| 4907 | set_bit(SDEBUG_UA_LUNS_CHANGED, |
| 4908 | dp->uas_bm); |
| 4909 | } |
| 4910 | } |
| 4911 | spin_unlock(&sdebug_host_list_lock); |
| 4912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4913 | return count; |
| 4914 | } |
| 4915 | return -EINVAL; |
| 4916 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4917 | static DRIVER_ATTR_RW(max_luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4919 | static ssize_t max_queue_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4920 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4921 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4922 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4923 | /* N.B. max_queue can be changed while there are queued commands. In flight |
| 4924 | * commands beyond the new max_queue will be completed. */ |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4925 | static ssize_t max_queue_store(struct device_driver *ddp, const char *buf, |
| 4926 | size_t count) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4927 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4928 | int j, n, k, a; |
| 4929 | struct sdebug_queue *sqp; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4930 | |
| 4931 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) && |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4932 | (n <= SDEBUG_CANQUEUE)) { |
| 4933 | block_unblock_all_queues(true); |
| 4934 | k = 0; |
| 4935 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; |
| 4936 | ++j, ++sqp) { |
| 4937 | a = find_last_bit(sqp->in_use_bm, SDEBUG_CANQUEUE); |
| 4938 | if (a > k) |
| 4939 | k = a; |
| 4940 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4941 | sdebug_max_queue = n; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4942 | if (k == SDEBUG_CANQUEUE) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4943 | atomic_set(&retired_max_queue, 0); |
| 4944 | else if (k >= n) |
| 4945 | atomic_set(&retired_max_queue, k + 1); |
| 4946 | else |
| 4947 | atomic_set(&retired_max_queue, 0); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4948 | block_unblock_all_queues(false); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4949 | return count; |
| 4950 | } |
| 4951 | return -EINVAL; |
| 4952 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4953 | static DRIVER_ATTR_RW(max_queue); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4954 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4955 | static ssize_t no_uld_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4956 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4957 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4958 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4959 | static DRIVER_ATTR_RO(no_uld); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4960 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4961 | static ssize_t scsi_level_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4963 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4964 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4965 | static DRIVER_ATTR_RO(scsi_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4967 | static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4968 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4969 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4970 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4971 | static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf, |
| 4972 | size_t count) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4973 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4974 | int n; |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4975 | bool changed; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4976 | |
| 4977 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4978 | changed = (sdebug_virtual_gb != n); |
| 4979 | sdebug_virtual_gb = n; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4980 | sdebug_capacity = get_sdebug_capacity(); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4981 | if (changed) { |
| 4982 | struct sdebug_host_info *sdhp; |
| 4983 | struct sdebug_dev_info *dp; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4984 | |
Ewan D. Milne | 4bc6b63 | 2014-12-04 11:49:26 -0500 | [diff] [blame] | 4985 | spin_lock(&sdebug_host_list_lock); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4986 | list_for_each_entry(sdhp, &sdebug_host_list, |
| 4987 | host_list) { |
| 4988 | list_for_each_entry(dp, &sdhp->dev_info_list, |
| 4989 | dev_list) { |
| 4990 | set_bit(SDEBUG_UA_CAPACITY_CHANGED, |
| 4991 | dp->uas_bm); |
| 4992 | } |
| 4993 | } |
Ewan D. Milne | 4bc6b63 | 2014-12-04 11:49:26 -0500 | [diff] [blame] | 4994 | spin_unlock(&sdebug_host_list_lock); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4995 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4996 | return count; |
| 4997 | } |
| 4998 | return -EINVAL; |
| 4999 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5000 | static DRIVER_ATTR_RW(virtual_gb); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 5001 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5002 | static ssize_t add_host_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5003 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5004 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5005 | } |
| 5006 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 5007 | static int sdebug_add_adapter(void); |
| 5008 | static void sdebug_remove_adapter(void); |
| 5009 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5010 | static ssize_t add_host_store(struct device_driver *ddp, const char *buf, |
| 5011 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | { |
FUJITA Tomonori | f3df41c | 2008-03-20 11:09:15 +0900 | [diff] [blame] | 5013 | int delta_hosts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5014 | |
FUJITA Tomonori | f3df41c | 2008-03-20 11:09:15 +0900 | [diff] [blame] | 5015 | if (sscanf(buf, "%d", &delta_hosts) != 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5016 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5017 | if (delta_hosts > 0) { |
| 5018 | do { |
| 5019 | sdebug_add_adapter(); |
| 5020 | } while (--delta_hosts); |
| 5021 | } else if (delta_hosts < 0) { |
| 5022 | do { |
| 5023 | sdebug_remove_adapter(); |
| 5024 | } while (++delta_hosts); |
| 5025 | } |
| 5026 | return count; |
| 5027 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5028 | static DRIVER_ATTR_RW(add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5030 | static ssize_t vpd_use_hostno_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 5031 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5032 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 5033 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5034 | static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf, |
| 5035 | size_t count) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 5036 | { |
| 5037 | int n; |
| 5038 | |
| 5039 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5040 | sdebug_vpd_use_hostno = n; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 5041 | return count; |
| 5042 | } |
| 5043 | return -EINVAL; |
| 5044 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5045 | static DRIVER_ATTR_RW(vpd_use_hostno); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 5046 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5047 | static ssize_t statistics_show(struct device_driver *ddp, char *buf) |
| 5048 | { |
| 5049 | return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_statistics); |
| 5050 | } |
| 5051 | static ssize_t statistics_store(struct device_driver *ddp, const char *buf, |
| 5052 | size_t count) |
| 5053 | { |
| 5054 | int n; |
| 5055 | |
| 5056 | if ((count > 0) && (sscanf(buf, "%d", &n) == 1) && (n >= 0)) { |
| 5057 | if (n > 0) |
| 5058 | sdebug_statistics = true; |
| 5059 | else { |
| 5060 | clear_queue_stats(); |
| 5061 | sdebug_statistics = false; |
| 5062 | } |
| 5063 | return count; |
| 5064 | } |
| 5065 | return -EINVAL; |
| 5066 | } |
| 5067 | static DRIVER_ATTR_RW(statistics); |
| 5068 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5069 | static ssize_t sector_size_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 5070 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5071 | return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size); |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 5072 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5073 | static DRIVER_ATTR_RO(sector_size); |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 5074 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5075 | static ssize_t submit_queues_show(struct device_driver *ddp, char *buf) |
| 5076 | { |
| 5077 | return scnprintf(buf, PAGE_SIZE, "%d\n", submit_queues); |
| 5078 | } |
| 5079 | static DRIVER_ATTR_RO(submit_queues); |
| 5080 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5081 | static ssize_t dix_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5082 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5083 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5084 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5085 | static DRIVER_ATTR_RO(dix); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5086 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5087 | static ssize_t dif_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5088 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5089 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5090 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5091 | static DRIVER_ATTR_RO(dif); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5092 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5093 | static ssize_t guard_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5094 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5095 | return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5096 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5097 | static DRIVER_ATTR_RO(guard); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5098 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5099 | static ssize_t ato_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5100 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5101 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5102 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5103 | static DRIVER_ATTR_RO(ato); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5104 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5105 | static ssize_t map_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5106 | { |
| 5107 | ssize_t count; |
| 5108 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 5109 | if (!scsi_debug_lbp()) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5110 | return scnprintf(buf, PAGE_SIZE, "0-%u\n", |
| 5111 | sdebug_store_sectors); |
| 5112 | |
Tejun Heo | c7badc9 | 2015-02-13 14:37:51 -0800 | [diff] [blame] | 5113 | count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", |
| 5114 | (int)map_size, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5115 | buf[count++] = '\n'; |
Tejun Heo | c7badc9 | 2015-02-13 14:37:51 -0800 | [diff] [blame] | 5116 | buf[count] = '\0'; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5117 | |
| 5118 | return count; |
| 5119 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5120 | static DRIVER_ATTR_RO(map); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5121 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5122 | static ssize_t removable_show(struct device_driver *ddp, char *buf) |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 5123 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5124 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 5125 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5126 | static ssize_t removable_store(struct device_driver *ddp, const char *buf, |
| 5127 | size_t count) |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 5128 | { |
| 5129 | int n; |
| 5130 | |
| 5131 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5132 | sdebug_removable = (n > 0); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 5133 | return count; |
| 5134 | } |
| 5135 | return -EINVAL; |
| 5136 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5137 | static DRIVER_ATTR_RW(removable); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 5138 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5139 | static ssize_t host_lock_show(struct device_driver *ddp, char *buf) |
| 5140 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5141 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5142 | } |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 5143 | /* N.B. sdebug_host_lock does nothing, kept for backward compatibility */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5144 | static ssize_t host_lock_store(struct device_driver *ddp, const char *buf, |
| 5145 | size_t count) |
| 5146 | { |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 5147 | int n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5148 | |
| 5149 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 5150 | sdebug_host_lock = (n > 0); |
| 5151 | return count; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5152 | } |
| 5153 | return -EINVAL; |
| 5154 | } |
| 5155 | static DRIVER_ATTR_RW(host_lock); |
| 5156 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5157 | static ssize_t strict_show(struct device_driver *ddp, char *buf) |
| 5158 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5159 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5160 | } |
| 5161 | static ssize_t strict_store(struct device_driver *ddp, const char *buf, |
| 5162 | size_t count) |
| 5163 | { |
| 5164 | int n; |
| 5165 | |
| 5166 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5167 | sdebug_strict = (n > 0); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5168 | return count; |
| 5169 | } |
| 5170 | return -EINVAL; |
| 5171 | } |
| 5172 | static DRIVER_ATTR_RW(strict); |
| 5173 | |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 5174 | static ssize_t uuid_ctl_show(struct device_driver *ddp, char *buf) |
| 5175 | { |
| 5176 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_uuid_ctl); |
| 5177 | } |
| 5178 | static DRIVER_ATTR_RO(uuid_ctl); |
| 5179 | |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 5180 | static ssize_t cdb_len_show(struct device_driver *ddp, char *buf) |
| 5181 | { |
| 5182 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_cdb_len); |
| 5183 | } |
| 5184 | static ssize_t cdb_len_store(struct device_driver *ddp, const char *buf, |
| 5185 | size_t count) |
| 5186 | { |
| 5187 | int ret, n; |
| 5188 | |
| 5189 | ret = kstrtoint(buf, 0, &n); |
| 5190 | if (ret) |
| 5191 | return ret; |
| 5192 | sdebug_cdb_len = n; |
| 5193 | all_config_cdb_len(); |
| 5194 | return count; |
| 5195 | } |
| 5196 | static DRIVER_ATTR_RW(cdb_len); |
| 5197 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5198 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5199 | /* Note: The following array creates attribute files in the |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 5200 | /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these |
| 5201 | files (over those found in the /sys/module/scsi_debug/parameters |
| 5202 | directory) is that auxiliary actions can be triggered when an attribute |
| 5203 | is changed. For example see: sdebug_add_host_store() above. |
| 5204 | */ |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5205 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5206 | static struct attribute *sdebug_drv_attrs[] = { |
| 5207 | &driver_attr_delay.attr, |
| 5208 | &driver_attr_opts.attr, |
| 5209 | &driver_attr_ptype.attr, |
| 5210 | &driver_attr_dsense.attr, |
| 5211 | &driver_attr_fake_rw.attr, |
| 5212 | &driver_attr_no_lun_0.attr, |
| 5213 | &driver_attr_num_tgts.attr, |
| 5214 | &driver_attr_dev_size_mb.attr, |
| 5215 | &driver_attr_num_parts.attr, |
| 5216 | &driver_attr_every_nth.attr, |
| 5217 | &driver_attr_max_luns.attr, |
| 5218 | &driver_attr_max_queue.attr, |
| 5219 | &driver_attr_no_uld.attr, |
| 5220 | &driver_attr_scsi_level.attr, |
| 5221 | &driver_attr_virtual_gb.attr, |
| 5222 | &driver_attr_add_host.attr, |
| 5223 | &driver_attr_vpd_use_hostno.attr, |
| 5224 | &driver_attr_sector_size.attr, |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5225 | &driver_attr_statistics.attr, |
| 5226 | &driver_attr_submit_queues.attr, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5227 | &driver_attr_dix.attr, |
| 5228 | &driver_attr_dif.attr, |
| 5229 | &driver_attr_guard.attr, |
| 5230 | &driver_attr_ato.attr, |
| 5231 | &driver_attr_map.attr, |
| 5232 | &driver_attr_removable.attr, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5233 | &driver_attr_host_lock.attr, |
| 5234 | &driver_attr_ndelay.attr, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5235 | &driver_attr_strict.attr, |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 5236 | &driver_attr_uuid_ctl.attr, |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 5237 | &driver_attr_cdb_len.attr, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5238 | NULL, |
| 5239 | }; |
| 5240 | ATTRIBUTE_GROUPS(sdebug_drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | |
Akinobu Mita | 11ddcec | 2014-02-26 22:56:59 +0900 | [diff] [blame] | 5242 | static struct device *pseudo_primary; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | static int __init scsi_debug_init(void) |
| 5245 | { |
FUJITA Tomonori | 5f2578e | 2008-03-30 00:59:57 +0900 | [diff] [blame] | 5246 | unsigned long sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | int host_to_add; |
| 5248 | int k; |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5249 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5251 | atomic_set(&retired_max_queue, 0); |
| 5252 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5253 | if (sdebug_ndelay >= 1000 * 1000 * 1000) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5254 | pr_warn("ndelay must be less than 1 second, ignored\n"); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5255 | sdebug_ndelay = 0; |
| 5256 | } else if (sdebug_ndelay > 0) |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 5257 | sdebug_jdelay = JDELAY_OVERRIDDEN; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5258 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5259 | switch (sdebug_sector_size) { |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 5260 | case 512: |
| 5261 | case 1024: |
| 5262 | case 2048: |
| 5263 | case 4096: |
| 5264 | break; |
| 5265 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5266 | pr_err("invalid sector_size %d\n", sdebug_sector_size); |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 5267 | return -EINVAL; |
| 5268 | } |
| 5269 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5270 | switch (sdebug_dif) { |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5271 | case T10_PI_TYPE0_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5272 | break; |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5273 | case T10_PI_TYPE1_PROTECTION: |
| 5274 | case T10_PI_TYPE2_PROTECTION: |
| 5275 | case T10_PI_TYPE3_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5276 | have_dif_prot = true; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5277 | break; |
| 5278 | |
| 5279 | default: |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5280 | pr_err("dif must be 0, 1, 2 or 3\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5281 | return -EINVAL; |
| 5282 | } |
| 5283 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5284 | if (sdebug_guard > 1) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5285 | pr_err("guard must be 0 or 1\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5286 | return -EINVAL; |
| 5287 | } |
| 5288 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5289 | if (sdebug_ato > 1) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5290 | pr_err("ato must be 0 or 1\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5291 | return -EINVAL; |
| 5292 | } |
| 5293 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5294 | if (sdebug_physblk_exp > 15) { |
| 5295 | pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 5296 | return -EINVAL; |
| 5297 | } |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 5298 | if (sdebug_max_luns > 256) { |
| 5299 | pr_warn("max_luns can be no more than 256, use default\n"); |
| 5300 | sdebug_max_luns = DEF_MAX_LUNS; |
| 5301 | } |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 5302 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5303 | if (sdebug_lowest_aligned > 0x3fff) { |
| 5304 | pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 5305 | return -EINVAL; |
| 5306 | } |
| 5307 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5308 | if (submit_queues < 1) { |
| 5309 | pr_err("submit_queues must be 1 or more\n"); |
| 5310 | return -EINVAL; |
| 5311 | } |
| 5312 | sdebug_q_arr = kcalloc(submit_queues, sizeof(struct sdebug_queue), |
| 5313 | GFP_KERNEL); |
| 5314 | if (sdebug_q_arr == NULL) |
| 5315 | return -ENOMEM; |
| 5316 | for (k = 0; k < submit_queues; ++k) |
| 5317 | spin_lock_init(&sdebug_q_arr[k].qc_lock); |
| 5318 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5319 | if (sdebug_dev_size_mb < 1) |
| 5320 | sdebug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */ |
| 5321 | sz = (unsigned long)sdebug_dev_size_mb * 1048576; |
| 5322 | sdebug_store_sectors = sz / sdebug_sector_size; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 5323 | sdebug_capacity = get_sdebug_capacity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | |
| 5325 | /* play around with geometry, don't waste too much on track 0 */ |
| 5326 | sdebug_heads = 8; |
| 5327 | sdebug_sectors_per = 32; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5328 | if (sdebug_dev_size_mb >= 256) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5329 | sdebug_heads = 64; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5330 | else if (sdebug_dev_size_mb >= 16) |
Andy Shevchenko | fa785f0 | 2015-11-26 20:22:50 +0200 | [diff] [blame] | 5331 | sdebug_heads = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5332 | sdebug_cylinders_per = (unsigned long)sdebug_capacity / |
| 5333 | (sdebug_sectors_per * sdebug_heads); |
| 5334 | if (sdebug_cylinders_per >= 1024) { |
| 5335 | /* other LLDs do this; implies >= 1GB ram disk ... */ |
| 5336 | sdebug_heads = 255; |
| 5337 | sdebug_sectors_per = 63; |
| 5338 | sdebug_cylinders_per = (unsigned long)sdebug_capacity / |
| 5339 | (sdebug_sectors_per * sdebug_heads); |
| 5340 | } |
| 5341 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 5342 | if (sdebug_fake_rw == 0) { |
Douglas Gilbert | 7382f9d | 2018-07-21 01:10:04 -0400 | [diff] [blame] | 5343 | fake_storep = vzalloc(sz); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5344 | if (NULL == fake_storep) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5345 | pr_err("out of memory, 1\n"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5346 | ret = -ENOMEM; |
| 5347 | goto free_q_arr; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5348 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5349 | if (sdebug_num_parts > 0) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5350 | sdebug_build_parts(fake_storep, sz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5353 | if (sdebug_dix) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5354 | int dif_size; |
| 5355 | |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 5356 | dif_size = sdebug_store_sectors * sizeof(struct t10_pi_tuple); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5357 | dif_storep = vmalloc(dif_size); |
| 5358 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5359 | pr_err("dif_storep %u bytes @ %p\n", dif_size, dif_storep); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5360 | |
| 5361 | if (dif_storep == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5362 | pr_err("out of mem. (DIX)\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5363 | ret = -ENOMEM; |
| 5364 | goto free_vm; |
| 5365 | } |
| 5366 | |
| 5367 | memset(dif_storep, 0xff, dif_size); |
| 5368 | } |
| 5369 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 5370 | /* Logical Block Provisioning */ |
| 5371 | if (scsi_debug_lbp()) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5372 | sdebug_unmap_max_blocks = |
| 5373 | clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 5374 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5375 | sdebug_unmap_max_desc = |
| 5376 | clamp(sdebug_unmap_max_desc, 0U, 256U); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 5377 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5378 | sdebug_unmap_granularity = |
| 5379 | clamp(sdebug_unmap_granularity, 1U, 0xffffffffU); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 5380 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5381 | if (sdebug_unmap_alignment && |
| 5382 | sdebug_unmap_granularity <= |
| 5383 | sdebug_unmap_alignment) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5384 | pr_err("ERR: unmap_granularity <= unmap_alignment\n"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5385 | ret = -EINVAL; |
| 5386 | goto free_vm; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5387 | } |
| 5388 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 5389 | map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1; |
Kees Cook | 42bc47b | 2018-06-12 14:27:11 -0700 | [diff] [blame] | 5390 | map_storep = vmalloc(array_size(sizeof(long), |
| 5391 | BITS_TO_LONGS(map_size))); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5392 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5393 | pr_info("%lu provisioning blocks\n", map_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5394 | |
| 5395 | if (map_storep == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5396 | pr_err("out of mem. (MAP)\n"); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5397 | ret = -ENOMEM; |
| 5398 | goto free_vm; |
| 5399 | } |
| 5400 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 5401 | bitmap_zero(map_storep, map_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5402 | |
| 5403 | /* Map first 1KB for partition table */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5404 | if (sdebug_num_parts) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5405 | map_region(0, 2); |
| 5406 | } |
| 5407 | |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5408 | pseudo_primary = root_device_register("pseudo_0"); |
| 5409 | if (IS_ERR(pseudo_primary)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5410 | pr_warn("root_device_register() error\n"); |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5411 | ret = PTR_ERR(pseudo_primary); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5412 | goto free_vm; |
| 5413 | } |
| 5414 | ret = bus_register(&pseudo_lld_bus); |
| 5415 | if (ret < 0) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5416 | pr_warn("bus_register error: %d\n", ret); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5417 | goto dev_unreg; |
| 5418 | } |
| 5419 | ret = driver_register(&sdebug_driverfs_driver); |
| 5420 | if (ret < 0) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5421 | pr_warn("driver_register error: %d\n", ret); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5422 | goto bus_unreg; |
| 5423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5425 | host_to_add = sdebug_add_host; |
| 5426 | sdebug_add_host = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5428 | for (k = 0; k < host_to_add; k++) { |
| 5429 | if (sdebug_add_adapter()) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5430 | pr_err("sdebug_add_adapter failed k=%d\n", k); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5431 | break; |
| 5432 | } |
| 5433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5435 | if (sdebug_verbose) |
| 5436 | pr_info("built %d host(s)\n", sdebug_add_host); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | return 0; |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5439 | |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5440 | bus_unreg: |
| 5441 | bus_unregister(&pseudo_lld_bus); |
| 5442 | dev_unreg: |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5443 | root_device_unregister(pseudo_primary); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5444 | free_vm: |
Tomas Winkler | de232af | 2015-07-28 16:54:22 +0300 | [diff] [blame] | 5445 | vfree(map_storep); |
| 5446 | vfree(dif_storep); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5447 | vfree(fake_storep); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5448 | free_q_arr: |
| 5449 | kfree(sdebug_q_arr); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5450 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | } |
| 5452 | |
| 5453 | static void __exit scsi_debug_exit(void) |
| 5454 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5455 | int k = sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | |
| 5457 | stop_all_queued(); |
| 5458 | for (; k; k--) |
| 5459 | sdebug_remove_adapter(); |
Luis Henriques | 52ab976 | 2018-06-18 17:08:03 +0100 | [diff] [blame] | 5460 | free_all_queued(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | driver_unregister(&sdebug_driverfs_driver); |
| 5462 | bus_unregister(&pseudo_lld_bus); |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5463 | root_device_unregister(pseudo_primary); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | |
Ewan D. Milne | 4d2b496 | 2016-10-26 11:22:53 -0400 | [diff] [blame] | 5465 | vfree(map_storep); |
Tomas Winkler | de232af | 2015-07-28 16:54:22 +0300 | [diff] [blame] | 5466 | vfree(dif_storep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | vfree(fake_storep); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5468 | kfree(sdebug_q_arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | } |
| 5470 | |
| 5471 | device_initcall(scsi_debug_init); |
| 5472 | module_exit(scsi_debug_exit); |
| 5473 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 5474 | static void sdebug_release_adapter(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5476 | struct sdebug_host_info *sdbg_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5477 | |
| 5478 | sdbg_host = to_sdebug_host(dev); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5479 | kfree(sdbg_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5480 | } |
| 5481 | |
| 5482 | static int sdebug_add_adapter(void) |
| 5483 | { |
| 5484 | int k, devs_per_host; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5485 | int error = 0; |
| 5486 | struct sdebug_host_info *sdbg_host; |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5487 | struct sdebug_dev_info *sdbg_devinfo, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5489 | sdbg_host = kzalloc(sizeof(*sdbg_host), GFP_KERNEL); |
| 5490 | if (sdbg_host == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5491 | pr_err("out of memory at line %d\n", __LINE__); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5492 | return -ENOMEM; |
| 5493 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5495 | INIT_LIST_HEAD(&sdbg_host->dev_info_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5497 | devs_per_host = sdebug_num_tgts * sdebug_max_luns; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5498 | for (k = 0; k < devs_per_host; k++) { |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 5499 | sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL); |
| 5500 | if (!sdbg_devinfo) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5501 | pr_err("out of memory at line %d\n", __LINE__); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5502 | error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | goto clean; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5504 | } |
| 5505 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5507 | spin_lock(&sdebug_host_list_lock); |
| 5508 | list_add_tail(&sdbg_host->host_list, &sdebug_host_list); |
| 5509 | spin_unlock(&sdebug_host_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5511 | sdbg_host->dev.bus = &pseudo_lld_bus; |
| 5512 | sdbg_host->dev.parent = pseudo_primary; |
| 5513 | sdbg_host->dev.release = &sdebug_release_adapter; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5514 | dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5515 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5516 | error = device_register(&sdbg_host->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5517 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5518 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | goto clean; |
| 5520 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5521 | ++sdebug_add_host; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5522 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5523 | |
| 5524 | clean: |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5525 | list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, |
| 5526 | dev_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5527 | list_del(&sdbg_devinfo->dev_list); |
| 5528 | kfree(sdbg_devinfo); |
| 5529 | } |
| 5530 | |
| 5531 | kfree(sdbg_host); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5532 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | } |
| 5534 | |
| 5535 | static void sdebug_remove_adapter(void) |
| 5536 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5537 | struct sdebug_host_info *sdbg_host = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5538 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5539 | spin_lock(&sdebug_host_list_lock); |
| 5540 | if (!list_empty(&sdebug_host_list)) { |
| 5541 | sdbg_host = list_entry(sdebug_host_list.prev, |
| 5542 | struct sdebug_host_info, host_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5543 | list_del(&sdbg_host->host_list); |
| 5544 | } |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5545 | spin_unlock(&sdebug_host_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | |
| 5547 | if (!sdbg_host) |
| 5548 | return; |
| 5549 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5550 | device_unregister(&sdbg_host->dev); |
| 5551 | --sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | } |
| 5553 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 5554 | static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5555 | { |
| 5556 | int num_in_q = 0; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5557 | struct sdebug_dev_info *devip; |
| 5558 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5559 | block_unblock_all_queues(true); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5560 | devip = (struct sdebug_dev_info *)sdev->hostdata; |
| 5561 | if (NULL == devip) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5562 | block_unblock_all_queues(false); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5563 | return -ENODEV; |
| 5564 | } |
| 5565 | num_in_q = atomic_read(&devip->num_in_q); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5566 | |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5567 | if (qdepth < 1) |
| 5568 | qdepth = 1; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5569 | /* allow to exceed max host qc_arr elements for testing */ |
| 5570 | if (qdepth > SDEBUG_CANQUEUE + 10) |
| 5571 | qdepth = SDEBUG_CANQUEUE + 10; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 5572 | scsi_change_queue_depth(sdev, qdepth); |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5573 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5574 | if (SDEBUG_OPT_Q_NOISE & sdebug_opts) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5575 | sdev_printk(KERN_INFO, sdev, "%s: qdepth=%d, num_in_q=%d\n", |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5576 | __func__, qdepth, num_in_q); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5577 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5578 | block_unblock_all_queues(false); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5579 | return sdev->queue_depth; |
| 5580 | } |
| 5581 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5582 | static bool fake_timeout(struct scsi_cmnd *scp) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5583 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5584 | if (0 == (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5585 | if (sdebug_every_nth < -1) |
| 5586 | sdebug_every_nth = -1; |
| 5587 | if (SDEBUG_OPT_TIMEOUT & sdebug_opts) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5588 | return true; /* ignore command causing timeout */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5589 | else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts && |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5590 | scsi_medium_access_command(scp)) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5591 | return true; /* time out reads and writes */ |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5592 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5593 | return false; |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5594 | } |
| 5595 | |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 5596 | static bool fake_host_busy(struct scsi_cmnd *scp) |
| 5597 | { |
| 5598 | return (sdebug_opts & SDEBUG_OPT_HOST_BUSY) && |
| 5599 | (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) == 0; |
| 5600 | } |
| 5601 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 5602 | static int scsi_debug_queuecommand(struct Scsi_Host *shost, |
| 5603 | struct scsi_cmnd *scp) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5604 | { |
| 5605 | u8 sdeb_i; |
| 5606 | struct scsi_device *sdp = scp->device; |
| 5607 | const struct opcode_info_t *oip; |
| 5608 | const struct opcode_info_t *r_oip; |
| 5609 | struct sdebug_dev_info *devip; |
| 5610 | u8 *cmd = scp->cmnd; |
| 5611 | int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); |
Martin Wilck | f66b851 | 2018-02-14 11:05:57 +0100 | [diff] [blame] | 5612 | int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *) = NULL; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5613 | int k, na; |
| 5614 | int errsts = 0; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5615 | u32 flags; |
| 5616 | u16 sa; |
| 5617 | u8 opcode = cmd[0]; |
| 5618 | bool has_wlun_rl; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5619 | |
| 5620 | scsi_set_resid(scp, 0); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5621 | if (sdebug_statistics) |
| 5622 | atomic_inc(&sdebug_cmnd_count); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5623 | if (unlikely(sdebug_verbose && |
| 5624 | !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5625 | char b[120]; |
| 5626 | int n, len, sb; |
| 5627 | |
| 5628 | len = scp->cmd_len; |
| 5629 | sb = (int)sizeof(b); |
| 5630 | if (len > 32) |
| 5631 | strcpy(b, "too long, over 32 bytes"); |
| 5632 | else { |
| 5633 | for (k = 0, n = 0; k < len && n < sb; ++k) |
| 5634 | n += scnprintf(b + n, sb - n, "%02x ", |
| 5635 | (u32)cmd[k]); |
| 5636 | } |
Bart Van Assche | 458df78 | 2018-01-26 08:52:19 -0800 | [diff] [blame] | 5637 | sdev_printk(KERN_INFO, sdp, "%s: tag=%#x, cmd %s\n", my_name, |
| 5638 | blk_mq_unique_tag(scp->request), b); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5639 | } |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 5640 | if (fake_host_busy(scp)) |
| 5641 | return SCSI_MLQUEUE_HOST_BUSY; |
Tomas Winkler | 34d5543 | 2015-07-28 16:54:21 +0300 | [diff] [blame] | 5642 | has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5643 | if (unlikely((sdp->lun >= sdebug_max_luns) && !has_wlun_rl)) |
| 5644 | goto err_out; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5645 | |
| 5646 | sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */ |
| 5647 | oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */ |
| 5648 | devip = (struct sdebug_dev_info *)sdp->hostdata; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5649 | if (unlikely(!devip)) { |
| 5650 | devip = find_build_dev_info(sdp); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5651 | if (NULL == devip) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5652 | goto err_out; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5653 | } |
| 5654 | na = oip->num_attached; |
| 5655 | r_pfp = oip->pfp; |
| 5656 | if (na) { /* multiple commands with this opcode */ |
| 5657 | r_oip = oip; |
| 5658 | if (FF_SA & r_oip->flags) { |
| 5659 | if (F_SA_LOW & oip->flags) |
| 5660 | sa = 0x1f & cmd[1]; |
| 5661 | else |
| 5662 | sa = get_unaligned_be16(cmd + 8); |
| 5663 | for (k = 0; k <= na; oip = r_oip->arrp + k++) { |
| 5664 | if (opcode == oip->opcode && sa == oip->sa) |
| 5665 | break; |
| 5666 | } |
| 5667 | } else { /* since no service action only check opcode */ |
| 5668 | for (k = 0; k <= na; oip = r_oip->arrp + k++) { |
| 5669 | if (opcode == oip->opcode) |
| 5670 | break; |
| 5671 | } |
| 5672 | } |
| 5673 | if (k > na) { |
| 5674 | if (F_SA_LOW & r_oip->flags) |
| 5675 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4); |
| 5676 | else if (F_SA_HIGH & r_oip->flags) |
| 5677 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7); |
| 5678 | else |
| 5679 | mk_sense_invalid_opcode(scp); |
| 5680 | goto check_cond; |
| 5681 | } |
| 5682 | } /* else (when na==0) we assume the oip is a match */ |
| 5683 | flags = oip->flags; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5684 | if (unlikely(F_INV_OP & flags)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5685 | mk_sense_invalid_opcode(scp); |
| 5686 | goto check_cond; |
| 5687 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5688 | if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5689 | if (sdebug_verbose) |
| 5690 | sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n", |
| 5691 | my_name, opcode, " supported for wlun"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5692 | mk_sense_invalid_opcode(scp); |
| 5693 | goto check_cond; |
| 5694 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5695 | if (unlikely(sdebug_strict)) { /* check cdb against mask */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5696 | u8 rem; |
| 5697 | int j; |
| 5698 | |
| 5699 | for (k = 1; k < oip->len_mask[0] && k < 16; ++k) { |
| 5700 | rem = ~oip->len_mask[k] & cmd[k]; |
| 5701 | if (rem) { |
| 5702 | for (j = 7; j >= 0; --j, rem <<= 1) { |
| 5703 | if (0x80 & rem) |
| 5704 | break; |
| 5705 | } |
| 5706 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j); |
| 5707 | goto check_cond; |
| 5708 | } |
| 5709 | } |
| 5710 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5711 | if (unlikely(!(F_SKIP_UA & flags) && |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 5712 | find_first_bit(devip->uas_bm, |
| 5713 | SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5714 | errsts = make_ua(scp, devip); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5715 | if (errsts) |
| 5716 | goto check_cond; |
| 5717 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5718 | if (unlikely((F_M_ACCESS & flags) && atomic_read(&devip->stopped))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5719 | mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5720 | if (sdebug_verbose) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5721 | sdev_printk(KERN_INFO, sdp, "%s reports: Not ready: " |
| 5722 | "%s\n", my_name, "initializing command " |
| 5723 | "required"); |
| 5724 | errsts = check_condition_result; |
| 5725 | goto fini; |
| 5726 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5727 | if (sdebug_fake_rw && (F_FAKE_RW & flags)) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5728 | goto fini; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5729 | if (unlikely(sdebug_every_nth)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5730 | if (fake_timeout(scp)) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5731 | return 0; /* ignore command: make trouble */ |
| 5732 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5733 | if (likely(oip->pfp)) |
Martin Wilck | f66b851 | 2018-02-14 11:05:57 +0100 | [diff] [blame] | 5734 | pfp = oip->pfp; /* calls a resp_* function */ |
| 5735 | else |
| 5736 | pfp = r_pfp; /* if leaf function ptr NULL, try the root's */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5737 | |
| 5738 | fini: |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 5739 | if (F_DELAY_OVERR & flags) |
Martin Wilck | f66b851 | 2018-02-14 11:05:57 +0100 | [diff] [blame] | 5740 | return schedule_resp(scp, devip, errsts, pfp, 0, 0); |
Douglas Gilbert | 75aa320 | 2018-07-12 13:35:42 -0400 | [diff] [blame] | 5741 | else if ((flags & F_LONG_DELAY) && (sdebug_jdelay > 0 || |
| 5742 | sdebug_ndelay > 10000)) { |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 5743 | /* |
Douglas Gilbert | 75aa320 | 2018-07-12 13:35:42 -0400 | [diff] [blame] | 5744 | * Skip long delays if ndelay <= 10 microseconds. Otherwise |
| 5745 | * for Start Stop Unit (SSU) want at least 1 second delay and |
| 5746 | * if sdebug_jdelay>1 want a long delay of that many seconds. |
| 5747 | * For Synchronize Cache want 1/20 of SSU's delay. |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 5748 | */ |
| 5749 | int jdelay = (sdebug_jdelay < 2) ? 1 : sdebug_jdelay; |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 5750 | int denom = (flags & F_SYNC_DELAY) ? 20 : 1; |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 5751 | |
Douglas Gilbert | 4f2c8bf | 2018-04-10 13:00:36 -0400 | [diff] [blame] | 5752 | jdelay = mult_frac(USER_HZ * jdelay, HZ, denom * USER_HZ); |
Martin Wilck | f66b851 | 2018-02-14 11:05:57 +0100 | [diff] [blame] | 5753 | return schedule_resp(scp, devip, errsts, pfp, jdelay, 0); |
Douglas Gilbert | 80c4956 | 2018-02-09 21:36:39 -0500 | [diff] [blame] | 5754 | } else |
Martin Wilck | f66b851 | 2018-02-14 11:05:57 +0100 | [diff] [blame] | 5755 | return schedule_resp(scp, devip, errsts, pfp, sdebug_jdelay, |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 5756 | sdebug_ndelay); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5757 | check_cond: |
Martin Wilck | f66b851 | 2018-02-14 11:05:57 +0100 | [diff] [blame] | 5758 | return schedule_resp(scp, devip, check_condition_result, NULL, 0, 0); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5759 | err_out: |
Martin Wilck | f66b851 | 2018-02-14 11:05:57 +0100 | [diff] [blame] | 5760 | return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, NULL, 0, 0); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5761 | } |
| 5762 | |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5763 | static struct scsi_host_template sdebug_driver_template = { |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 5764 | .show_info = scsi_debug_show_info, |
| 5765 | .write_info = scsi_debug_write_info, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5766 | .proc_name = sdebug_proc_name, |
| 5767 | .name = "SCSI DEBUG", |
| 5768 | .info = scsi_debug_info, |
| 5769 | .slave_alloc = scsi_debug_slave_alloc, |
| 5770 | .slave_configure = scsi_debug_slave_configure, |
| 5771 | .slave_destroy = scsi_debug_slave_destroy, |
| 5772 | .ioctl = scsi_debug_ioctl, |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 5773 | .queuecommand = scsi_debug_queuecommand, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5774 | .change_queue_depth = sdebug_change_qdepth, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5775 | .eh_abort_handler = scsi_debug_abort, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5776 | .eh_device_reset_handler = scsi_debug_device_reset, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5777 | .eh_target_reset_handler = scsi_debug_target_reset, |
| 5778 | .eh_bus_reset_handler = scsi_debug_bus_reset, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5779 | .eh_host_reset_handler = scsi_debug_host_reset, |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5780 | .can_queue = SDEBUG_CANQUEUE, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5781 | .this_id = 7, |
Ming Lin | 65e8617 | 2016-04-04 14:48:10 -0700 | [diff] [blame] | 5782 | .sg_tablesize = SG_MAX_SEGMENTS, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5783 | .cmd_per_lun = DEF_CMD_PER_LUN, |
Akinobu Mita | 6bb5e6e | 2014-06-02 22:56:49 +0900 | [diff] [blame] | 5784 | .max_sectors = -1U, |
Christoph Hellwig | 50c2e91 | 2018-12-13 16:17:03 +0100 | [diff] [blame] | 5785 | .max_segment_size = -1U, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5786 | .module = THIS_MODULE, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5787 | .track_queue_depth = 1, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5788 | }; |
| 5789 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 5790 | static int sdebug_driver_probe(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5791 | { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 5792 | int error = 0; |
| 5793 | struct sdebug_host_info *sdbg_host; |
| 5794 | struct Scsi_Host *hpnt; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5795 | int hprot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5796 | |
| 5797 | sdbg_host = to_sdebug_host(dev); |
| 5798 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5799 | sdebug_driver_template.can_queue = sdebug_max_queue; |
Christoph Hellwig | 2a3d4eb | 2018-12-13 16:17:02 +0100 | [diff] [blame] | 5800 | if (!sdebug_clustering) |
Christoph Hellwig | 4af14d1 | 2018-12-13 16:17:09 +0100 | [diff] [blame] | 5801 | sdebug_driver_template.dma_boundary = PAGE_SIZE - 1; |
| 5802 | |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5803 | hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host)); |
| 5804 | if (NULL == hpnt) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5805 | pr_err("scsi_host_alloc failed\n"); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5806 | error = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | return error; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5808 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5809 | if (submit_queues > nr_cpu_ids) { |
Alexey Dobriyan | 9b130ad | 2017-09-08 16:14:18 -0700 | [diff] [blame] | 5810 | pr_warn("%s: trim submit_queues (was %d) to nr_cpu_ids=%u\n", |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5811 | my_name, submit_queues, nr_cpu_ids); |
| 5812 | submit_queues = nr_cpu_ids; |
| 5813 | } |
| 5814 | /* Decide whether to tell scsi subsystem that we want mq */ |
| 5815 | /* Following should give the same answer for each host */ |
Jens Axboe | f664a3c | 2018-11-01 16:36:27 -0600 | [diff] [blame] | 5816 | hpnt->nr_hw_queues = submit_queues; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5817 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5818 | sdbg_host->shost = hpnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 | *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5820 | if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id)) |
| 5821 | hpnt->max_id = sdebug_num_tgts + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5822 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5823 | hpnt->max_id = sdebug_num_tgts; |
| 5824 | /* = sdebug_max_luns; */ |
Tomas Winkler | f2d3fd2 | 2015-07-28 16:54:25 +0300 | [diff] [blame] | 5825 | hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5826 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5827 | hprot = 0; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5828 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5829 | switch (sdebug_dif) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5830 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5831 | case T10_PI_TYPE1_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5832 | hprot = SHOST_DIF_TYPE1_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5833 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5834 | hprot |= SHOST_DIX_TYPE1_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5835 | break; |
| 5836 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5837 | case T10_PI_TYPE2_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5838 | hprot = SHOST_DIF_TYPE2_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5839 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5840 | hprot |= SHOST_DIX_TYPE2_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5841 | break; |
| 5842 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5843 | case T10_PI_TYPE3_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5844 | hprot = SHOST_DIF_TYPE3_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5845 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5846 | hprot |= SHOST_DIX_TYPE3_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5847 | break; |
| 5848 | |
| 5849 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5850 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5851 | hprot |= SHOST_DIX_TYPE0_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5852 | break; |
| 5853 | } |
| 5854 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5855 | scsi_host_set_prot(hpnt, hprot); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5856 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5857 | if (have_dif_prot || sdebug_dix) |
| 5858 | pr_info("host protection%s%s%s%s%s%s%s\n", |
| 5859 | (hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "", |
| 5860 | (hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "", |
| 5861 | (hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "", |
| 5862 | (hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "", |
| 5863 | (hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "", |
| 5864 | (hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "", |
| 5865 | (hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : ""); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5866 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5867 | if (sdebug_guard == 1) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5868 | scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP); |
| 5869 | else |
| 5870 | scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC); |
| 5871 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5872 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts); |
| 5873 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & sdebug_opts); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5874 | if (sdebug_every_nth) /* need stats counters for every_nth */ |
| 5875 | sdebug_statistics = true; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5876 | error = scsi_add_host(hpnt, &sdbg_host->dev); |
| 5877 | if (error) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5878 | pr_err("scsi_add_host failed\n"); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5879 | error = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | scsi_host_put(hpnt); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5881 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5882 | scsi_scan_host(hpnt); |
| 5883 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5884 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | } |
| 5886 | |
John Pittman | 91d4c75 | 2018-02-09 21:12:43 -0500 | [diff] [blame] | 5887 | static int sdebug_driver_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5888 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5889 | struct sdebug_host_info *sdbg_host; |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5890 | struct sdebug_dev_info *sdbg_devinfo, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5891 | |
| 5892 | sdbg_host = to_sdebug_host(dev); |
| 5893 | |
| 5894 | if (!sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5895 | pr_err("Unable to locate host info\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5896 | return -ENODEV; |
| 5897 | } |
| 5898 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5899 | scsi_remove_host(sdbg_host->shost); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5901 | list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, |
| 5902 | dev_list) { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5903 | list_del(&sdbg_devinfo->dev_list); |
| 5904 | kfree(sdbg_devinfo); |
| 5905 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5906 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5907 | scsi_host_put(sdbg_host->shost); |
| 5908 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | } |
| 5910 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5911 | static int pseudo_lld_bus_match(struct device *dev, |
| 5912 | struct device_driver *dev_driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5913 | { |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5914 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5915 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5916 | |
| 5917 | static struct bus_type pseudo_lld_bus = { |
| 5918 | .name = "pseudo", |
| 5919 | .match = pseudo_lld_bus_match, |
| 5920 | .probe = sdebug_driver_probe, |
| 5921 | .remove = sdebug_driver_remove, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5922 | .drv_groups = sdebug_drv_groups, |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5923 | }; |