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 | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 9 | * Copyright (C) 2001 - 2017 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 | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 64 | #define SDEBUG_VERSION "0187" /* format to fit INQUIRY revision field */ |
| 65 | static const char *sdebug_version_date = "20171202"; |
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 | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 167 | #define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \ |
| 168 | SDEBUG_OPT_RESET_NOISE) |
| 169 | #define SDEBUG_OPT_ALL_INJECTING (SDEBUG_OPT_RECOVERED_ERR | \ |
| 170 | SDEBUG_OPT_TRANSPORT_ERR | \ |
| 171 | SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR | \ |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 172 | SDEBUG_OPT_SHORT_TRANSFER | \ |
| 173 | SDEBUG_OPT_HOST_BUSY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | /* When "every_nth" > 0 then modulo "every_nth" commands: |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 175 | * - a missing response is simulated if SDEBUG_OPT_TIMEOUT is set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | * - a RECOVERED_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 177 | * commands if SDEBUG_OPT_RECOVERED_ERR is set. |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 178 | * - a TRANSPORT_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_TRANSPORT_ERR is set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | * |
| 181 | * When "every_nth" < 0 then after "- every_nth" commands: |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 182 | * - a missing response is simulated if SDEBUG_OPT_TIMEOUT is set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | * - a RECOVERED_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 184 | * commands if SDEBUG_OPT_RECOVERED_ERR is set. |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 185 | * - a TRANSPORT_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 186 | * commands if _DEBUG_OPT_TRANSPORT_ERR is set. |
| 187 | * This will continue on every subsequent command until some other action |
| 188 | * occurs (e.g. the user * writing a new value (other than -1 or 1) to |
| 189 | * every_nth via sysfs). |
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 */ |
| 237 | |
| 238 | #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] | 239 | #define FF_MEDIA_IO (F_M_ACCESS | F_FAKE_RW) |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 240 | #define FF_SA (F_SA_HIGH | F_SA_LOW) |
| 241 | |
| 242 | #define SDEBUG_MAX_PARTS 4 |
| 243 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 244 | #define SDEBUG_MAX_CMD_LEN 32 |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 245 | |
| 246 | |
| 247 | struct sdebug_dev_info { |
| 248 | struct list_head dev_list; |
| 249 | unsigned int channel; |
| 250 | unsigned int target; |
| 251 | u64 lun; |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 252 | uuid_t lu_name; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 253 | struct sdebug_host_info *sdbg_host; |
| 254 | unsigned long uas_bm[1]; |
| 255 | atomic_t num_in_q; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 256 | atomic_t stopped; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 257 | bool used; |
| 258 | }; |
| 259 | |
| 260 | struct sdebug_host_info { |
| 261 | struct list_head host_list; |
| 262 | struct Scsi_Host *shost; |
| 263 | struct device dev; |
| 264 | struct list_head dev_info_list; |
| 265 | }; |
| 266 | |
| 267 | #define to_sdebug_host(d) \ |
| 268 | container_of(d, struct sdebug_host_info, dev) |
| 269 | |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 270 | enum sdeb_defer_type {SDEB_DEFER_NONE = 0, SDEB_DEFER_HRT = 1, |
| 271 | SDEB_DEFER_WQ = 2}; |
| 272 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 273 | struct sdebug_defer { |
| 274 | struct hrtimer hrt; |
| 275 | struct execute_work ew; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 276 | int sqa_idx; /* index of sdebug_queue array */ |
| 277 | int qc_idx; /* index of sdebug_queued_cmd array within sqa_idx */ |
| 278 | int issuing_cpu; |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 279 | bool init_hrt; |
| 280 | bool init_wq; |
| 281 | enum sdeb_defer_type defer_t; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | struct sdebug_queued_cmd { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 285 | /* corresponding bit set in in_use_bm[] in owning struct sdebug_queue |
| 286 | * instance indicates this slot is in use. |
| 287 | */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 288 | struct sdebug_defer *sd_dp; |
| 289 | struct scsi_cmnd *a_cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 290 | unsigned int inj_recovered:1; |
| 291 | unsigned int inj_transport:1; |
| 292 | unsigned int inj_dif:1; |
| 293 | unsigned int inj_dix:1; |
| 294 | unsigned int inj_short:1; |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 295 | unsigned int inj_host_busy:1; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 296 | }; |
| 297 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 298 | struct sdebug_queue { |
| 299 | struct sdebug_queued_cmd qc_arr[SDEBUG_CANQUEUE]; |
| 300 | unsigned long in_use_bm[SDEBUG_CANQUEUE_WORDS]; |
| 301 | spinlock_t qc_lock; |
| 302 | atomic_t blocked; /* to temporarily stop more being queued */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 303 | }; |
| 304 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 305 | static atomic_t sdebug_cmnd_count; /* number of incoming commands */ |
| 306 | static atomic_t sdebug_completions; /* count of deferred completions */ |
| 307 | static atomic_t sdebug_miss_cpus; /* submission + completion cpus differ */ |
| 308 | static atomic_t sdebug_a_tsf; /* 'almost task set full' counter */ |
| 309 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 310 | struct opcode_info_t { |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 311 | u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff */ |
| 312 | /* for terminating element */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 313 | u8 opcode; /* if num_attached > 0, preferred */ |
| 314 | u16 sa; /* service action */ |
| 315 | u32 flags; /* OR-ed set of SDEB_F_* */ |
| 316 | int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 317 | const struct opcode_info_t *arrp; /* num_attached elements or NULL */ |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 318 | u8 len_mask[16]; /* len_mask[0]-->cdb_len, then mask for cdb */ |
| 319 | /* 1 to min(cdb_len, 15); ignore cdb[15...] */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | /* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 323 | enum sdeb_opcode_index { |
| 324 | SDEB_I_INVALID_OPCODE = 0, |
| 325 | SDEB_I_INQUIRY = 1, |
| 326 | SDEB_I_REPORT_LUNS = 2, |
| 327 | SDEB_I_REQUEST_SENSE = 3, |
| 328 | SDEB_I_TEST_UNIT_READY = 4, |
| 329 | SDEB_I_MODE_SENSE = 5, /* 6, 10 */ |
| 330 | SDEB_I_MODE_SELECT = 6, /* 6, 10 */ |
| 331 | SDEB_I_LOG_SENSE = 7, |
| 332 | SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */ |
| 333 | SDEB_I_READ = 9, /* 6, 10, 12, 16 */ |
| 334 | SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */ |
| 335 | SDEB_I_START_STOP = 11, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 336 | SDEB_I_SERV_ACT_IN_16 = 12, /* add ...SERV_ACT_IN_12 if needed */ |
| 337 | 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] | 338 | SDEB_I_MAINT_IN = 14, |
| 339 | SDEB_I_MAINT_OUT = 15, |
| 340 | SDEB_I_VERIFY = 16, /* 10 only */ |
Douglas Gilbert | 481b5e5 | 2017-12-23 12:48:14 -0500 | [diff] [blame] | 341 | SDEB_I_VARIABLE_LEN = 17, /* READ(32), WRITE(32), WR_SCAT(32) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 342 | SDEB_I_RESERVE = 18, /* 6, 10 */ |
| 343 | SDEB_I_RELEASE = 19, /* 6, 10 */ |
| 344 | SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */ |
| 345 | SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */ |
| 346 | SDEB_I_ATA_PT = 22, /* 12, 16 */ |
| 347 | SDEB_I_SEND_DIAG = 23, |
| 348 | SDEB_I_UNMAP = 24, |
| 349 | SDEB_I_XDWRITEREAD = 25, /* 10 only */ |
| 350 | SDEB_I_WRITE_BUFFER = 26, |
| 351 | SDEB_I_WRITE_SAME = 27, /* 10, 16 */ |
| 352 | SDEB_I_SYNC_CACHE = 28, /* 10 only */ |
| 353 | SDEB_I_COMP_WRITE = 29, |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 354 | SDEB_I_LAST_ELEMENT = 30, /* keep this last (previous + 1) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 355 | }; |
| 356 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 357 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 358 | static const unsigned char opcode_ind_arr[256] = { |
| 359 | /* 0x0; 0x0->0x1f: 6 byte cdbs */ |
| 360 | SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE, |
| 361 | 0, 0, 0, 0, |
| 362 | SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0, |
| 363 | 0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE, |
| 364 | SDEB_I_RELEASE, |
| 365 | 0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG, |
| 366 | SDEB_I_ALLOW_REMOVAL, 0, |
| 367 | /* 0x20; 0x20->0x3f: 10 byte cdbs */ |
| 368 | 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0, |
| 369 | SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY, |
| 370 | 0, 0, 0, 0, 0, SDEB_I_SYNC_CACHE, 0, 0, |
| 371 | 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0, |
| 372 | /* 0x40; 0x40->0x5f: 10 byte cdbs */ |
| 373 | 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0, |
| 374 | 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0, |
| 375 | 0, 0, 0, SDEB_I_XDWRITEREAD, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE, |
| 376 | SDEB_I_RELEASE, |
| 377 | 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0, |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 378 | /* 0x60; 0x60->0x7d are reserved, 0x7e is "extended cdb" */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 379 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 380 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 381 | 0, SDEB_I_VARIABLE_LEN, |
| 382 | /* 0x80; 0x80->0x9f: 16 byte cdbs */ |
| 383 | 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0, |
| 384 | SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0, 0, 0, 0, 0, |
| 385 | 0, 0, 0, SDEB_I_WRITE_SAME, 0, 0, 0, 0, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 386 | 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] | 387 | /* 0xa0; 0xa0->0xbf: 12 byte cdbs */ |
| 388 | SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN, |
| 389 | SDEB_I_MAINT_OUT, 0, 0, 0, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 390 | SDEB_I_READ, 0 /* SDEB_I_SERV_ACT_OUT_12 */, SDEB_I_WRITE, |
| 391 | 0 /* SDEB_I_SERV_ACT_IN_12 */, 0, 0, 0, 0, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 392 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 393 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 394 | /* 0xc0; 0xc0->0xff: vendor specific */ |
| 395 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 396 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 397 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 398 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 399 | }; |
| 400 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 401 | static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 402 | static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 403 | static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 404 | static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 405 | static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 406 | static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 407 | static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 408 | static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 409 | static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 481b5e5 | 2017-12-23 12:48:14 -0500 | [diff] [blame] | 410 | static int resp_write_scat(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 411 | static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 412 | static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 413 | static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 414 | static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 415 | static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 416 | static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 417 | static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 418 | static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 419 | static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 420 | static int resp_xdwriteread_10(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 421 | 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] | 422 | static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 423 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 424 | /* |
| 425 | * The following are overflow arrays for cdbs that "hit" the same index in |
| 426 | * the opcode_info_arr array. The most time sensitive (or commonly used) cdb |
| 427 | * should be placed in opcode_info_arr[], the others should be placed here. |
| 428 | */ |
| 429 | static const struct opcode_info_t msense_iarr[] = { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 430 | {0, 0x1a, 0, F_D_IN, NULL, NULL, |
| 431 | {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 432 | }; |
| 433 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 434 | static const struct opcode_info_t mselect_iarr[] = { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 435 | {0, 0x15, 0, F_D_OUT, NULL, NULL, |
| 436 | {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 437 | }; |
| 438 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 439 | static const struct opcode_info_t read_iarr[] = { |
| 440 | {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] | 441 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 442 | 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 443 | {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] | 444 | {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] | 445 | {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] | 446 | {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 447 | 0xc7, 0, 0, 0, 0} }, |
| 448 | }; |
| 449 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 450 | static const struct opcode_info_t write_iarr[] = { |
| 451 | {0, 0x2a, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(10) */ |
| 452 | NULL, {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, |
| 453 | 0, 0, 0, 0, 0, 0} }, |
| 454 | {0, 0xa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(6) */ |
| 455 | NULL, {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, |
| 456 | 0, 0, 0} }, |
| 457 | {0, 0xaa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(12) */ |
| 458 | NULL, {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 459 | 0xbf, 0xc7, 0, 0, 0, 0} }, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 460 | }; |
| 461 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 462 | static const struct opcode_info_t sa_in_16_iarr[] = { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 463 | {0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL, |
| 464 | {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 465 | 0xff, 0xff, 0xff, 0, 0xc7} }, /* GET LBA STATUS(16) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 466 | }; |
| 467 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 468 | static const struct opcode_info_t vl_iarr[] = { /* VARIABLE LENGTH */ |
| 469 | {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] | 470 | NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0xb, 0xfa, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 471 | 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */ |
Douglas Gilbert | 481b5e5 | 2017-12-23 12:48:14 -0500 | [diff] [blame] | 472 | {0, 0x7f, 0x11, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_scat, |
| 473 | NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x11, 0xf8, |
| 474 | 0, 0xff, 0xff, 0x0, 0x0} }, /* WRITE SCATTERED(32) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 475 | }; |
| 476 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 477 | static const struct opcode_info_t maint_in_iarr[] = { /* MAINT IN */ |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 478 | {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] | 479 | {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 480 | 0xc7, 0, 0, 0, 0} }, /* REPORT SUPPORTED OPERATION CODES */ |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 481 | {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] | 482 | {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] | 483 | 0, 0} }, /* REPORTED SUPPORTED TASK MANAGEMENT FUNCTIONS */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 484 | }; |
| 485 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 486 | static const struct opcode_info_t write_same_iarr[] = { |
| 487 | {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] | 488 | {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 489 | 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* WRITE SAME(16) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 490 | }; |
| 491 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 492 | static const struct opcode_info_t reserve_iarr[] = { |
| 493 | {0, 0x16, 0, F_D_OUT, NULL, NULL, /* RESERVE(6) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 494 | {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 495 | }; |
| 496 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 497 | static const struct opcode_info_t release_iarr[] = { |
| 498 | {0, 0x17, 0, F_D_OUT, NULL, NULL, /* RELEASE(6) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 499 | {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 500 | }; |
| 501 | |
| 502 | |
| 503 | /* This array is accessed via SDEB_I_* values. Make sure all are mapped, |
| 504 | * plus the terminating elements for logic that scans this table such as |
| 505 | * REPORT SUPPORTED OPERATION CODES. */ |
| 506 | static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEMENT + 1] = { |
| 507 | /* 0 */ |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 508 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* unknown opcodes */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 509 | {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] | 510 | {0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL, /* INQUIRY */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 511 | {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 512 | {0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL, |
| 513 | {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] | 514 | 0, 0} }, /* REPORT LUNS */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 515 | {0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL, |
| 516 | {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 517 | {0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */ |
| 518 | {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] | 519 | /* 5 */ |
| 520 | {ARRAY_SIZE(msense_iarr), 0x5a, 0, F_D_IN, /* MODE SENSE(10) */ |
| 521 | resp_mode_sense, msense_iarr, {10, 0xf8, 0xff, 0xff, 0, 0, 0, |
| 522 | 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 523 | {ARRAY_SIZE(mselect_iarr), 0x55, 0, F_D_OUT, /* MODE SELECT(10) */ |
| 524 | resp_mode_select, mselect_iarr, {10, 0xf1, 0, 0, 0, 0, 0, 0xff, |
| 525 | 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 526 | {0, 0x4d, 0, F_D_IN, resp_log_sense, NULL, /* LOG SENSE */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 527 | {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, |
| 528 | 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 529 | {0, 0x25, 0, F_D_IN, resp_readcap, NULL, /* READ CAPACITY(10) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 530 | {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0, |
| 531 | 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 532 | {ARRAY_SIZE(read_iarr), 0x88, 0, F_D_IN | FF_MEDIA_IO, /* READ(16) */ |
| 533 | resp_read_dt0, read_iarr, {16, 0xfe, 0xff, 0xff, 0xff, 0xff, |
| 534 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} }, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 535 | /* 10 */ |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 536 | {ARRAY_SIZE(write_iarr), 0x8a, 0, F_D_OUT | FF_MEDIA_IO, |
| 537 | resp_write_dt0, write_iarr, /* WRITE(16) */ |
| 538 | {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 539 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} }, /* WRITE(16) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 540 | {0, 0x1b, 0, 0, resp_start_stop, NULL, /* START STOP UNIT */ |
| 541 | {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] | 542 | {ARRAY_SIZE(sa_in_16_iarr), 0x9e, 0x10, F_SA_LOW | F_D_IN, |
| 543 | resp_readcap16, sa_in_16_iarr, /* SA_IN(16), READ CAPACITY(16) */ |
| 544 | {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 545 | 0xff, 0xff, 0xff, 0xff, 0x1, 0xc7} }, |
Douglas Gilbert | 481b5e5 | 2017-12-23 12:48:14 -0500 | [diff] [blame] | 546 | {0, 0x9f, 0x12, F_SA_LOW | F_D_OUT | FF_MEDIA_IO, resp_write_scat, |
| 547 | NULL, {16, 0x12, 0xf9, 0x0, 0xff, 0xff, 0, 0, 0xff, 0xff, 0xff, |
| 548 | 0xff, 0xff, 0xff, 0xff, 0xc7} }, /* SA_OUT(16), WRITE SCAT(16) */ |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 549 | {ARRAY_SIZE(maint_in_iarr), 0xa3, 0xa, F_SA_LOW | F_D_IN, |
| 550 | resp_report_tgtpgs, /* MAINT IN, REPORT TARGET PORT GROUPS */ |
| 551 | maint_in_iarr, {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff, |
| 552 | 0xff, 0, 0xc7, 0, 0, 0, 0} }, |
| 553 | /* 15 */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 554 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */ |
| 555 | {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] | 556 | {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] | 557 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, |
| 558 | 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 559 | {ARRAY_SIZE(vl_iarr), 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_MEDIA_IO, |
| 560 | resp_read_dt0, vl_iarr, /* VARIABLE LENGTH, READ(32) */ |
| 561 | {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x9, 0xfe, 0, 0xff, 0xff, |
| 562 | 0xff, 0xff} }, |
| 563 | {ARRAY_SIZE(reserve_iarr), 0x56, 0, F_D_OUT, |
| 564 | NULL, reserve_iarr, /* RESERVE(10) <no response function> */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 565 | {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 566 | 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 567 | {ARRAY_SIZE(release_iarr), 0x57, 0, F_D_OUT, |
| 568 | NULL, release_iarr, /* RELEASE(10) <no response function> */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 569 | {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 570 | 0} }, |
| 571 | /* 20 */ |
Douglas Gilbert | f7f9f26 | 2015-11-22 12:11:28 -0500 | [diff] [blame] | 572 | {0, 0x1e, 0, 0, NULL, NULL, /* ALLOW REMOVAL */ |
| 573 | {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] | 574 | {0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */ |
| 575 | {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 576 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */ |
| 577 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 578 | {0, 0x1d, F_D_OUT, 0, NULL, NULL, /* SEND DIAGNOSTIC */ |
| 579 | {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] | 580 | {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] | 581 | {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] | 582 | /* 25 */ |
| 583 | {0, 0x53, 0, F_D_IN | F_D_OUT | FF_MEDIA_IO, resp_xdwriteread_10, |
Douglas Gilbert | b7e2458 | 2017-12-23 12:48:11 -0500 | [diff] [blame] | 584 | NULL, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 585 | 0, 0, 0, 0, 0, 0} }, /* XDWRITEREAD(10) */ |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 586 | {0, 0x3b, 0, F_D_OUT_MAYBE, resp_write_buffer, NULL, |
| 587 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, |
| 588 | 0, 0, 0, 0} }, /* WRITE_BUFFER */ |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 589 | {ARRAY_SIZE(write_same_iarr), 0x41, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, |
| 590 | resp_write_same_10, write_same_iarr, /* WRITE SAME(10) */ |
| 591 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, |
| 592 | 0, 0, 0, 0, 0} }, |
| 593 | {0, 0x35, 0, F_DELAY_OVERR | FF_MEDIA_IO, NULL, NULL, /* SYNC_CACHE */ |
Douglas Gilbert | b7e2458 | 2017-12-23 12:48:11 -0500 | [diff] [blame] | 594 | {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 595 | 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame] | 596 | {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] | 597 | {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, |
Douglas Gilbert | b7e2458 | 2017-12-23 12:48:11 -0500 | [diff] [blame] | 598 | 0, 0xff, 0x3f, 0xc7} }, /* COMPARE AND WRITE */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 599 | |
| 600 | /* 30 */ |
| 601 | {0xff, 0, 0, 0, NULL, NULL, /* terminating element */ |
| 602 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 603 | }; |
| 604 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 605 | static int sdebug_add_host = DEF_NUM_HOST; |
| 606 | static int sdebug_ato = DEF_ATO; |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 607 | static int sdebug_cdb_len = DEF_CDB_LEN; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 608 | static int sdebug_jdelay = DEF_JDELAY; /* if > 0 then unit is jiffies */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 609 | static int sdebug_dev_size_mb = DEF_DEV_SIZE_MB; |
| 610 | static int sdebug_dif = DEF_DIF; |
| 611 | static int sdebug_dix = DEF_DIX; |
| 612 | static int sdebug_dsense = DEF_D_SENSE; |
| 613 | static int sdebug_every_nth = DEF_EVERY_NTH; |
| 614 | static int sdebug_fake_rw = DEF_FAKE_RW; |
| 615 | static unsigned int sdebug_guard = DEF_GUARD; |
| 616 | static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED; |
| 617 | static int sdebug_max_luns = DEF_MAX_LUNS; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 618 | static int sdebug_max_queue = SDEBUG_CANQUEUE; /* per submit queue */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 619 | 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] | 620 | static int sdebug_ndelay = DEF_NDELAY; /* if > 0 then unit is nanoseconds */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 621 | static int sdebug_no_lun_0 = DEF_NO_LUN_0; |
| 622 | static int sdebug_no_uld; |
| 623 | static int sdebug_num_parts = DEF_NUM_PARTS; |
| 624 | static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */ |
| 625 | static int sdebug_opt_blks = DEF_OPT_BLKS; |
| 626 | static int sdebug_opts = DEF_OPTS; |
| 627 | static int sdebug_physblk_exp = DEF_PHYSBLK_EXP; |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 628 | static int sdebug_opt_xferlen_exp = DEF_OPT_XFERLEN_EXP; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 629 | static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral device type */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 630 | static int sdebug_scsi_level = DEF_SCSI_LEVEL; |
| 631 | static int sdebug_sector_size = DEF_SECTOR_SIZE; |
| 632 | static int sdebug_virtual_gb = DEF_VIRTUAL_GB; |
| 633 | static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO; |
| 634 | static unsigned int sdebug_lbpu = DEF_LBPU; |
| 635 | static unsigned int sdebug_lbpws = DEF_LBPWS; |
| 636 | static unsigned int sdebug_lbpws10 = DEF_LBPWS10; |
| 637 | static unsigned int sdebug_lbprz = DEF_LBPRZ; |
| 638 | static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT; |
| 639 | static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY; |
| 640 | static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS; |
| 641 | static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC; |
| 642 | static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH; |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 643 | static int sdebug_uuid_ctl = DEF_UUID_CTL; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 644 | static bool sdebug_removable = DEF_REMOVABLE; |
| 645 | static bool sdebug_clustering; |
| 646 | static bool sdebug_host_lock = DEF_HOST_LOCK; |
| 647 | static bool sdebug_strict = DEF_STRICT; |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 648 | static bool sdebug_any_injecting_opt; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 649 | static bool sdebug_verbose; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 650 | static bool have_dif_prot; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 651 | static bool sdebug_statistics = DEF_STATISTICS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 653 | static unsigned int sdebug_store_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | static sector_t sdebug_capacity; /* in sectors */ |
| 655 | |
| 656 | /* old BIOS stuff, kernel may get rid of them but some mode sense pages |
| 657 | may still need them */ |
| 658 | static int sdebug_heads; /* heads per disk */ |
| 659 | static int sdebug_cylinders_per; /* cylinders per surface */ |
| 660 | static int sdebug_sectors_per; /* sectors per cylinder */ |
| 661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | static LIST_HEAD(sdebug_host_list); |
| 663 | static DEFINE_SPINLOCK(sdebug_host_list_lock); |
| 664 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 665 | static unsigned char *fake_storep; /* ramdisk storage */ |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 666 | static struct t10_pi_tuple *dif_storep; /* protection info */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 667 | static void *map_storep; /* provisioning map */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 669 | static unsigned long map_size; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 670 | static int num_aborts; |
| 671 | static int num_dev_resets; |
| 672 | static int num_target_resets; |
| 673 | static int num_bus_resets; |
| 674 | static int num_host_resets; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 675 | static int dix_writes; |
| 676 | static int dix_reads; |
| 677 | static int dif_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 679 | static int submit_queues = DEF_SUBMIT_QUEUES; /* > 1 for multi-queue (mq) */ |
| 680 | 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] | 681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | static DEFINE_RWLOCK(atomic_rw); |
| 683 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 684 | static char sdebug_proc_name[] = MY_NAME; |
| 685 | static const char *my_name = MY_NAME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | static struct bus_type pseudo_lld_bus; |
| 688 | |
| 689 | static struct device_driver sdebug_driverfs_driver = { |
| 690 | .name = sdebug_proc_name, |
| 691 | .bus = &pseudo_lld_bus, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | }; |
| 693 | |
| 694 | static const int check_condition_result = |
| 695 | (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
| 696 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 697 | static const int illegal_condition_result = |
| 698 | (DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION; |
| 699 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 700 | static const int device_qfull_result = |
| 701 | (DID_OK << 16) | (COMMAND_COMPLETE << 8) | SAM_STAT_TASK_SET_FULL; |
| 702 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 703 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 704 | /* Only do the extra work involved in logical block provisioning if one or |
| 705 | * more of the lbpu, lbpws or lbpws10 parameters are given and we are doing |
| 706 | * real reads and writes (i.e. not skipping them for speed). |
| 707 | */ |
| 708 | static inline bool scsi_debug_lbp(void) |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 709 | { |
| 710 | return 0 == sdebug_fake_rw && |
| 711 | (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10); |
| 712 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 713 | |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 714 | static void *fake_store(unsigned long long lba) |
| 715 | { |
| 716 | lba = do_div(lba, sdebug_store_sectors); |
| 717 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 718 | return fake_storep + lba * sdebug_sector_size; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 719 | } |
| 720 | |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 721 | static struct t10_pi_tuple *dif_store(sector_t sector) |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 722 | { |
Arnd Bergmann | 4941311 | 2015-11-20 17:38:28 +0100 | [diff] [blame] | 723 | sector = sector_div(sector, sdebug_store_sectors); |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 724 | |
| 725 | return dif_storep + sector; |
| 726 | } |
| 727 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 728 | static void sdebug_max_tgts_luns(void) |
| 729 | { |
| 730 | struct sdebug_host_info *sdbg_host; |
| 731 | struct Scsi_Host *hpnt; |
| 732 | |
| 733 | spin_lock(&sdebug_host_list_lock); |
| 734 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
| 735 | hpnt = sdbg_host->shost; |
| 736 | if ((hpnt->this_id >= 0) && |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 737 | (sdebug_num_tgts > hpnt->this_id)) |
| 738 | hpnt->max_id = sdebug_num_tgts + 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 739 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 740 | hpnt->max_id = sdebug_num_tgts; |
| 741 | /* sdebug_max_luns; */ |
Tomas Winkler | f2d3fd2 | 2015-07-28 16:54:25 +0300 | [diff] [blame] | 742 | hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 743 | } |
| 744 | spin_unlock(&sdebug_host_list_lock); |
| 745 | } |
| 746 | |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 747 | enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1}; |
| 748 | |
| 749 | /* 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] | 750 | static void mk_sense_invalid_fld(struct scsi_cmnd *scp, |
| 751 | enum sdeb_cmd_data c_d, |
| 752 | int in_byte, int in_bit) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 753 | { |
| 754 | unsigned char *sbuff; |
| 755 | u8 sks[4]; |
| 756 | int sl, asc; |
| 757 | |
| 758 | sbuff = scp->sense_buffer; |
| 759 | if (!sbuff) { |
| 760 | sdev_printk(KERN_ERR, scp->device, |
| 761 | "%s: sense_buffer is NULL\n", __func__); |
| 762 | return; |
| 763 | } |
| 764 | asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST; |
| 765 | memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 766 | scsi_build_sense_buffer(sdebug_dsense, sbuff, ILLEGAL_REQUEST, asc, 0); |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 767 | memset(sks, 0, sizeof(sks)); |
| 768 | sks[0] = 0x80; |
| 769 | if (c_d) |
| 770 | sks[0] |= 0x40; |
| 771 | if (in_bit >= 0) { |
| 772 | sks[0] |= 0x8; |
| 773 | sks[0] |= 0x7 & in_bit; |
| 774 | } |
| 775 | put_unaligned_be16(in_byte, sks + 1); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 776 | if (sdebug_dsense) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 777 | sl = sbuff[7] + 8; |
| 778 | sbuff[7] = sl; |
| 779 | sbuff[sl] = 0x2; |
| 780 | sbuff[sl + 1] = 0x6; |
| 781 | memcpy(sbuff + sl + 4, sks, 3); |
| 782 | } else |
| 783 | memcpy(sbuff + 15, sks, 3); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 784 | if (sdebug_verbose) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 785 | sdev_printk(KERN_INFO, scp->device, "%s: [sense_key,asc,ascq" |
| 786 | "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n", |
| 787 | my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit); |
| 788 | } |
| 789 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 790 | 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] | 791 | { |
| 792 | unsigned char *sbuff; |
| 793 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 794 | sbuff = scp->sense_buffer; |
| 795 | if (!sbuff) { |
| 796 | sdev_printk(KERN_ERR, scp->device, |
| 797 | "%s: sense_buffer is NULL\n", __func__); |
| 798 | return; |
| 799 | } |
| 800 | memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 801 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 802 | scsi_build_sense_buffer(sdebug_dsense, sbuff, key, asc, asq); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 803 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 804 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 805 | sdev_printk(KERN_INFO, scp->device, |
| 806 | "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n", |
| 807 | my_name, key, asc, asq); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 808 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 810 | static void mk_sense_invalid_opcode(struct scsi_cmnd *scp) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 811 | { |
| 812 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0); |
| 813 | } |
| 814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg) |
| 816 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 817 | if (sdebug_verbose) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 818 | if (0x1261 == cmd) |
| 819 | sdev_printk(KERN_INFO, dev, |
| 820 | "%s: BLKFLSBUF [0x1261]\n", __func__); |
| 821 | else if (0x5331 == cmd) |
| 822 | sdev_printk(KERN_INFO, dev, |
| 823 | "%s: CDROM_GET_CAPABILITY [0x5331]\n", |
| 824 | __func__); |
| 825 | else |
| 826 | sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n", |
| 827 | __func__, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | } |
| 829 | return -EINVAL; |
| 830 | /* return -ENOTTY; // correct return but upsets fdisk */ |
| 831 | } |
| 832 | |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 833 | static void config_cdb_len(struct scsi_device *sdev) |
| 834 | { |
| 835 | switch (sdebug_cdb_len) { |
| 836 | case 6: /* suggest 6 byte READ, WRITE and MODE SENSE/SELECT */ |
| 837 | sdev->use_10_for_rw = false; |
| 838 | sdev->use_16_for_rw = false; |
| 839 | sdev->use_10_for_ms = false; |
| 840 | break; |
| 841 | case 10: /* suggest 10 byte RWs and 6 byte MODE SENSE/SELECT */ |
| 842 | sdev->use_10_for_rw = true; |
| 843 | sdev->use_16_for_rw = false; |
| 844 | sdev->use_10_for_ms = false; |
| 845 | break; |
| 846 | case 12: /* suggest 10 byte RWs and 10 byte MODE SENSE/SELECT */ |
| 847 | sdev->use_10_for_rw = true; |
| 848 | sdev->use_16_for_rw = false; |
| 849 | sdev->use_10_for_ms = true; |
| 850 | break; |
| 851 | case 16: |
| 852 | sdev->use_10_for_rw = false; |
| 853 | sdev->use_16_for_rw = true; |
| 854 | sdev->use_10_for_ms = true; |
| 855 | break; |
| 856 | case 32: /* No knobs to suggest this so same as 16 for now */ |
| 857 | sdev->use_10_for_rw = false; |
| 858 | sdev->use_16_for_rw = true; |
| 859 | sdev->use_10_for_ms = true; |
| 860 | break; |
| 861 | default: |
| 862 | pr_warn("unexpected cdb_len=%d, force to 10\n", |
| 863 | sdebug_cdb_len); |
| 864 | sdev->use_10_for_rw = true; |
| 865 | sdev->use_16_for_rw = false; |
| 866 | sdev->use_10_for_ms = false; |
| 867 | sdebug_cdb_len = 10; |
| 868 | break; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | static void all_config_cdb_len(void) |
| 873 | { |
| 874 | struct sdebug_host_info *sdbg_host; |
| 875 | struct Scsi_Host *shost; |
| 876 | struct scsi_device *sdev; |
| 877 | |
| 878 | spin_lock(&sdebug_host_list_lock); |
| 879 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
| 880 | shost = sdbg_host->shost; |
| 881 | shost_for_each_device(sdev, shost) { |
| 882 | config_cdb_len(sdev); |
| 883 | } |
| 884 | } |
| 885 | spin_unlock(&sdebug_host_list_lock); |
| 886 | } |
| 887 | |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 888 | static void clear_luns_changed_on_target(struct sdebug_dev_info *devip) |
| 889 | { |
| 890 | struct sdebug_host_info *sdhp; |
| 891 | struct sdebug_dev_info *dp; |
| 892 | |
| 893 | spin_lock(&sdebug_host_list_lock); |
| 894 | list_for_each_entry(sdhp, &sdebug_host_list, host_list) { |
| 895 | list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) { |
| 896 | if ((devip->sdbg_host == dp->sdbg_host) && |
| 897 | (devip->target == dp->target)) |
| 898 | clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm); |
| 899 | } |
| 900 | } |
| 901 | spin_unlock(&sdebug_host_list_lock); |
| 902 | } |
| 903 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 904 | 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] | 905 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 906 | int k; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 907 | |
| 908 | k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS); |
| 909 | if (k != SDEBUG_NUM_UAS) { |
| 910 | const char *cp = NULL; |
| 911 | |
| 912 | switch (k) { |
| 913 | case SDEBUG_UA_POR: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 914 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC, |
| 915 | POWER_ON_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 916 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 917 | cp = "power on reset"; |
| 918 | break; |
| 919 | case SDEBUG_UA_BUS_RESET: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 920 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC, |
| 921 | BUS_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 922 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 923 | cp = "bus reset"; |
| 924 | break; |
| 925 | case SDEBUG_UA_MODE_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 926 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC, |
| 927 | MODE_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 928 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 929 | cp = "mode parameters changed"; |
| 930 | break; |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 931 | case SDEBUG_UA_CAPACITY_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 932 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC, |
| 933 | CAPACITY_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 934 | if (sdebug_verbose) |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 935 | cp = "capacity data changed"; |
Ewan D. Milne | f49accf | 2014-12-04 11:49:25 -0500 | [diff] [blame] | 936 | break; |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 937 | case SDEBUG_UA_MICROCODE_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 938 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 939 | TARGET_CHANGED_ASC, |
| 940 | MICROCODE_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 941 | if (sdebug_verbose) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 942 | cp = "microcode has been changed"; |
| 943 | break; |
| 944 | case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 945 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 946 | TARGET_CHANGED_ASC, |
| 947 | MICROCODE_CHANGED_WO_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 948 | if (sdebug_verbose) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 949 | cp = "microcode has been changed without reset"; |
| 950 | break; |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 951 | case SDEBUG_UA_LUNS_CHANGED: |
| 952 | /* |
| 953 | * SPC-3 behavior is to report a UNIT ATTENTION with |
| 954 | * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN |
| 955 | * on the target, until a REPORT LUNS command is |
| 956 | * received. SPC-4 behavior is to report it only once. |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 957 | * NOTE: sdebug_scsi_level does not use the same |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 958 | * values as struct scsi_device->scsi_level. |
| 959 | */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 960 | if (sdebug_scsi_level >= 6) /* SPC-4 and above */ |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 961 | clear_luns_changed_on_target(devip); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 962 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 963 | TARGET_CHANGED_ASC, |
| 964 | LUNS_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 965 | if (sdebug_verbose) |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 966 | cp = "reported luns data has changed"; |
| 967 | break; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 968 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 969 | pr_warn("unexpected unit attention code=%d\n", k); |
| 970 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 971 | cp = "unknown"; |
| 972 | break; |
| 973 | } |
| 974 | clear_bit(k, devip->uas_bm); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 975 | if (sdebug_verbose) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 976 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 977 | "%s reports: Unit attention: %s\n", |
| 978 | my_name, cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | return check_condition_result; |
| 980 | } |
| 981 | return 0; |
| 982 | } |
| 983 | |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 984 | /* 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] | 985 | 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] | 986 | int arr_len) |
| 987 | { |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 988 | int act_len; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 989 | struct scsi_data_buffer *sdb = scsi_in(scp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 991 | if (!sdb->length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | return 0; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 993 | if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE)) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 994 | return DID_ERROR << 16; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 995 | |
| 996 | act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents, |
| 997 | arr, arr_len); |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 998 | sdb->resid = scsi_bufflen(scp) - act_len; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | return 0; |
| 1001 | } |
| 1002 | |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 1003 | /* Partial build of SCSI "data-in" buffer. Returns 0 if ok else |
| 1004 | * (DID_ERROR << 16). Can write to offset in data-in buffer. If multiple |
| 1005 | * calls, not required to write in ascending offset order. Assumes resid |
| 1006 | * set to scsi_bufflen() prior to any calls. |
| 1007 | */ |
| 1008 | static int p_fill_from_dev_buffer(struct scsi_cmnd *scp, const void *arr, |
| 1009 | int arr_len, unsigned int off_dst) |
| 1010 | { |
| 1011 | int act_len, n; |
| 1012 | struct scsi_data_buffer *sdb = scsi_in(scp); |
| 1013 | off_t skip = off_dst; |
| 1014 | |
| 1015 | if (sdb->length <= off_dst) |
| 1016 | return 0; |
| 1017 | if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE)) |
| 1018 | return DID_ERROR << 16; |
| 1019 | |
| 1020 | act_len = sg_pcopy_from_buffer(sdb->table.sgl, sdb->table.nents, |
| 1021 | arr, arr_len, skip); |
| 1022 | pr_debug("%s: off_dst=%u, scsi_bufflen=%u, act_len=%u, resid=%d\n", |
| 1023 | __func__, off_dst, scsi_bufflen(scp), act_len, sdb->resid); |
| 1024 | n = (int)scsi_bufflen(scp) - ((int)off_dst + act_len); |
| 1025 | sdb->resid = min(sdb->resid, n); |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | /* Fetches from SCSI "data-out" buffer. Returns number of bytes fetched into |
| 1030 | * 'arr' or -1 if error. |
| 1031 | */ |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 1032 | static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr, |
| 1033 | int arr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | { |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 1035 | if (!scsi_bufflen(scp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | return 0; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 1037 | if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_TO_DEVICE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | return -1; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 1039 | |
| 1040 | return scsi_sg_copy_to_buffer(scp, arr, arr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | |
Hannes Reinecke | e5203cf | 2017-10-02 16:26:33 +0200 | [diff] [blame] | 1044 | static char sdebug_inq_vendor_id[9] = "Linux "; |
| 1045 | static char sdebug_inq_product_id[17] = "scsi_debug "; |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 1046 | static char sdebug_inq_product_rev[5] = SDEBUG_VERSION; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1047 | /* Use some locally assigned NAAs for SAS addresses. */ |
| 1048 | static const u64 naa3_comp_a = 0x3222222000000000ULL; |
| 1049 | static const u64 naa3_comp_b = 0x3333333000000000ULL; |
| 1050 | static const u64 naa3_comp_c = 0x3111111000000000ULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1052 | /* Device identification VPD page. Returns number of bytes placed in arr */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1053 | static int inquiry_vpd_83(unsigned char *arr, int port_group_id, |
| 1054 | int target_dev_id, int dev_id_num, |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1055 | const char *dev_id_str, int dev_id_str_len, |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 1056 | const uuid_t *lu_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1058 | int num, port_a; |
| 1059 | char b[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1061 | port_a = target_dev_id + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | /* T10 vendor identifier field format (faked) */ |
| 1063 | arr[0] = 0x2; /* ASCII */ |
| 1064 | arr[1] = 0x1; |
| 1065 | arr[2] = 0x0; |
Hannes Reinecke | e5203cf | 2017-10-02 16:26:33 +0200 | [diff] [blame] | 1066 | memcpy(&arr[4], sdebug_inq_vendor_id, 8); |
| 1067 | memcpy(&arr[12], sdebug_inq_product_id, 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | memcpy(&arr[28], dev_id_str, dev_id_str_len); |
| 1069 | num = 8 + 16 + dev_id_str_len; |
| 1070 | arr[3] = num; |
| 1071 | num += 4; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1072 | if (dev_id_num >= 0) { |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1073 | if (sdebug_uuid_ctl) { |
| 1074 | /* Locally assigned UUID */ |
| 1075 | arr[num++] = 0x1; /* binary (not necessarily sas) */ |
| 1076 | arr[num++] = 0xa; /* PIV=0, lu, naa */ |
| 1077 | arr[num++] = 0x0; |
| 1078 | arr[num++] = 0x12; |
| 1079 | arr[num++] = 0x10; /* uuid type=1, locally assigned */ |
| 1080 | arr[num++] = 0x0; |
| 1081 | memcpy(arr + num, lu_name, 16); |
| 1082 | num += 16; |
| 1083 | } else { |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1084 | /* NAA-3, Logical unit identifier (binary) */ |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1085 | arr[num++] = 0x1; /* binary (not necessarily sas) */ |
| 1086 | arr[num++] = 0x3; /* PIV=0, lu, naa */ |
| 1087 | arr[num++] = 0x0; |
| 1088 | arr[num++] = 0x8; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1089 | put_unaligned_be64(naa3_comp_b + dev_id_num, arr + num); |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1090 | num += 8; |
| 1091 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1092 | /* Target relative port number */ |
| 1093 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1094 | arr[num++] = 0x94; /* PIV=1, target port, rel port */ |
| 1095 | arr[num++] = 0x0; /* reserved */ |
| 1096 | arr[num++] = 0x4; /* length */ |
| 1097 | arr[num++] = 0x0; /* reserved */ |
| 1098 | arr[num++] = 0x0; /* reserved */ |
| 1099 | arr[num++] = 0x0; |
| 1100 | arr[num++] = 0x1; /* relative port A */ |
| 1101 | } |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1102 | /* NAA-3, Target port identifier */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1103 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1104 | arr[num++] = 0x93; /* piv=1, target port, naa */ |
| 1105 | arr[num++] = 0x0; |
| 1106 | arr[num++] = 0x8; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1107 | put_unaligned_be64(naa3_comp_a + port_a, arr + num); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1108 | num += 8; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1109 | /* NAA-3, Target port group identifier */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1110 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1111 | arr[num++] = 0x95; /* piv=1, target port group id */ |
| 1112 | arr[num++] = 0x0; |
| 1113 | arr[num++] = 0x4; |
| 1114 | arr[num++] = 0; |
| 1115 | arr[num++] = 0; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1116 | put_unaligned_be16(port_group_id, arr + num); |
| 1117 | num += 2; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1118 | /* NAA-3, Target device identifier */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1119 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1120 | arr[num++] = 0xa3; /* piv=1, target device, naa */ |
| 1121 | arr[num++] = 0x0; |
| 1122 | arr[num++] = 0x8; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1123 | put_unaligned_be64(naa3_comp_a + target_dev_id, arr + num); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1124 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1125 | /* SCSI name string: Target device identifier */ |
| 1126 | arr[num++] = 0x63; /* proto=sas, UTF-8 */ |
| 1127 | arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */ |
| 1128 | arr[num++] = 0x0; |
| 1129 | arr[num++] = 24; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1130 | memcpy(arr + num, "naa.32222220", 12); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1131 | num += 12; |
| 1132 | snprintf(b, sizeof(b), "%08X", target_dev_id); |
| 1133 | memcpy(arr + num, b, 8); |
| 1134 | num += 8; |
| 1135 | memset(arr + num, 0, 4); |
| 1136 | num += 4; |
| 1137 | return num; |
| 1138 | } |
| 1139 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1140 | static unsigned char vpd84_data[] = { |
| 1141 | /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0, |
| 1142 | 0x22,0x22,0x22,0x0,0xbb,0x1, |
| 1143 | 0x22,0x22,0x22,0x0,0xbb,0x2, |
| 1144 | }; |
| 1145 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1146 | /* Software interface identification VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1147 | static int inquiry_vpd_84(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1148 | { |
| 1149 | memcpy(arr, vpd84_data, sizeof(vpd84_data)); |
| 1150 | return sizeof(vpd84_data); |
| 1151 | } |
| 1152 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1153 | /* Management network addresses VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1154 | static int inquiry_vpd_85(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1155 | { |
| 1156 | int num = 0; |
| 1157 | const char * na1 = "https://www.kernel.org/config"; |
| 1158 | const char * na2 = "http://www.kernel.org/log"; |
| 1159 | int plen, olen; |
| 1160 | |
| 1161 | arr[num++] = 0x1; /* lu, storage config */ |
| 1162 | arr[num++] = 0x0; /* reserved */ |
| 1163 | arr[num++] = 0x0; |
| 1164 | olen = strlen(na1); |
| 1165 | plen = olen + 1; |
| 1166 | if (plen % 4) |
| 1167 | plen = ((plen / 4) + 1) * 4; |
| 1168 | arr[num++] = plen; /* length, null termianted, padded */ |
| 1169 | memcpy(arr + num, na1, olen); |
| 1170 | memset(arr + num + olen, 0, plen - olen); |
| 1171 | num += plen; |
| 1172 | |
| 1173 | arr[num++] = 0x4; /* lu, logging */ |
| 1174 | arr[num++] = 0x0; /* reserved */ |
| 1175 | arr[num++] = 0x0; |
| 1176 | olen = strlen(na2); |
| 1177 | plen = olen + 1; |
| 1178 | if (plen % 4) |
| 1179 | plen = ((plen / 4) + 1) * 4; |
| 1180 | arr[num++] = plen; /* length, null terminated, padded */ |
| 1181 | memcpy(arr + num, na2, olen); |
| 1182 | memset(arr + num + olen, 0, plen - olen); |
| 1183 | num += plen; |
| 1184 | |
| 1185 | return num; |
| 1186 | } |
| 1187 | |
| 1188 | /* SCSI ports VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1189 | static int inquiry_vpd_88(unsigned char *arr, int target_dev_id) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1190 | { |
| 1191 | int num = 0; |
| 1192 | int port_a, port_b; |
| 1193 | |
| 1194 | port_a = target_dev_id + 1; |
| 1195 | port_b = port_a + 1; |
| 1196 | arr[num++] = 0x0; /* reserved */ |
| 1197 | arr[num++] = 0x0; /* reserved */ |
| 1198 | arr[num++] = 0x0; |
| 1199 | arr[num++] = 0x1; /* relative port 1 (primary) */ |
| 1200 | memset(arr + num, 0, 6); |
| 1201 | num += 6; |
| 1202 | arr[num++] = 0x0; |
| 1203 | arr[num++] = 12; /* length tp descriptor */ |
| 1204 | /* naa-5 target port identifier (A) */ |
| 1205 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1206 | arr[num++] = 0x93; /* PIV=1, target port, NAA */ |
| 1207 | arr[num++] = 0x0; /* reserved */ |
| 1208 | arr[num++] = 0x8; /* length */ |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1209 | put_unaligned_be64(naa3_comp_a + port_a, arr + num); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1210 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1211 | arr[num++] = 0x0; /* reserved */ |
| 1212 | arr[num++] = 0x0; /* reserved */ |
| 1213 | arr[num++] = 0x0; |
| 1214 | arr[num++] = 0x2; /* relative port 2 (secondary) */ |
| 1215 | memset(arr + num, 0, 6); |
| 1216 | num += 6; |
| 1217 | arr[num++] = 0x0; |
| 1218 | arr[num++] = 12; /* length tp descriptor */ |
| 1219 | /* naa-5 target port identifier (B) */ |
| 1220 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1221 | arr[num++] = 0x93; /* PIV=1, target port, NAA */ |
| 1222 | arr[num++] = 0x0; /* reserved */ |
| 1223 | arr[num++] = 0x8; /* length */ |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1224 | put_unaligned_be64(naa3_comp_a + port_b, arr + num); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1225 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1226 | |
| 1227 | return num; |
| 1228 | } |
| 1229 | |
| 1230 | |
| 1231 | static unsigned char vpd89_data[] = { |
| 1232 | /* from 4th byte */ 0,0,0,0, |
| 1233 | 'l','i','n','u','x',' ',' ',' ', |
| 1234 | 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ', |
| 1235 | '1','2','3','4', |
| 1236 | 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0, |
| 1237 | 0xec,0,0,0, |
| 1238 | 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0, |
| 1239 | 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20, |
| 1240 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33, |
| 1241 | 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31, |
| 1242 | 0x53,0x41, |
| 1243 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 1244 | 0x20,0x20, |
| 1245 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 1246 | 0x10,0x80, |
| 1247 | 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0, |
| 1248 | 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0, |
| 1249 | 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0, |
| 1250 | 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0, |
| 1251 | 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40, |
| 1252 | 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0, |
| 1253 | 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0, |
| 1254 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1255 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1256 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1257 | 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42, |
| 1258 | 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8, |
| 1259 | 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe, |
| 1260 | 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0, |
| 1261 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1262 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1263 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1264 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1265 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1266 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1267 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1268 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1269 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1270 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1271 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1272 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51, |
| 1273 | }; |
| 1274 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1275 | /* ATA Information VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1276 | static int inquiry_vpd_89(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1277 | { |
| 1278 | memcpy(arr, vpd89_data, sizeof(vpd89_data)); |
| 1279 | return sizeof(vpd89_data); |
| 1280 | } |
| 1281 | |
| 1282 | |
| 1283 | static unsigned char vpdb0_data[] = { |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1284 | /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64, |
| 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, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1288 | }; |
| 1289 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1290 | /* Block limits VPD page (SBC-3) */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1291 | static int inquiry_vpd_b0(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1292 | { |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 1293 | unsigned int gran; |
| 1294 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1295 | memcpy(arr, vpdb0_data, sizeof(vpdb0_data)); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1296 | |
| 1297 | /* Optimal transfer length granularity */ |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 1298 | if (sdebug_opt_xferlen_exp != 0 && |
| 1299 | sdebug_physblk_exp < sdebug_opt_xferlen_exp) |
| 1300 | gran = 1 << sdebug_opt_xferlen_exp; |
| 1301 | else |
| 1302 | gran = 1 << sdebug_physblk_exp; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1303 | put_unaligned_be16(gran, arr + 2); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1304 | |
| 1305 | /* Maximum Transfer Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1306 | if (sdebug_store_sectors > 0x400) |
| 1307 | put_unaligned_be32(sdebug_store_sectors, arr + 4); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1308 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1309 | /* Optimal Transfer Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1310 | put_unaligned_be32(sdebug_opt_blks, &arr[8]); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1311 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1312 | if (sdebug_lbpu) { |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1313 | /* Maximum Unmap LBA Count */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1314 | put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1315 | |
| 1316 | /* Maximum Unmap Block Descriptor Count */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1317 | put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1318 | } |
| 1319 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1320 | /* Unmap Granularity Alignment */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1321 | if (sdebug_unmap_alignment) { |
| 1322 | put_unaligned_be32(sdebug_unmap_alignment, &arr[28]); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1323 | arr[28] |= 0x80; /* UGAVALID */ |
| 1324 | } |
| 1325 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1326 | /* Optimal Unmap Granularity */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1327 | put_unaligned_be32(sdebug_unmap_granularity, &arr[24]); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1328 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1329 | /* Maximum WRITE SAME Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1330 | put_unaligned_be64(sdebug_write_same_length, &arr[32]); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1331 | |
| 1332 | return 0x3c; /* Mandatory page length for Logical Block Provisioning */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1333 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1334 | return sizeof(vpdb0_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1337 | /* Block device characteristics VPD page (SBC-3) */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1338 | static int inquiry_vpd_b1(unsigned char *arr) |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1339 | { |
| 1340 | memset(arr, 0, 0x3c); |
| 1341 | arr[0] = 0; |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1342 | arr[1] = 1; /* non rotating medium (e.g. solid state) */ |
| 1343 | arr[2] = 0; |
| 1344 | arr[3] = 5; /* less than 1.8" */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1345 | |
| 1346 | return 0x3c; |
| 1347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1349 | /* Logical block provisioning VPD page (SBC-4) */ |
| 1350 | static int inquiry_vpd_b2(unsigned char *arr) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1351 | { |
Martin K. Petersen | 3f0bc3b | 2012-03-08 10:48:29 -0500 | [diff] [blame] | 1352 | memset(arr, 0, 0x4); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1353 | arr[0] = 0; /* threshold exponent */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1354 | if (sdebug_lbpu) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1355 | arr[1] = 1 << 7; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1356 | if (sdebug_lbpws) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1357 | arr[1] |= 1 << 6; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1358 | if (sdebug_lbpws10) |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1359 | arr[1] |= 1 << 5; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1360 | if (sdebug_lbprz && scsi_debug_lbp()) |
| 1361 | arr[1] |= (sdebug_lbprz & 0x7) << 2; /* sbc4r07 and later */ |
| 1362 | /* anc_sup=0; dp=0 (no provisioning group descriptor) */ |
| 1363 | /* minimum_percentage=0; provisioning_type=0 (unknown) */ |
| 1364 | /* threshold_percentage=0 */ |
Martin K. Petersen | 3f0bc3b | 2012-03-08 10:48:29 -0500 | [diff] [blame] | 1365 | return 0x4; |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1366 | } |
| 1367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | #define SDEBUG_LONG_INQ_SZ 96 |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1369 | #define SDEBUG_MAX_INQ_ARR_SZ 584 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1371 | 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] | 1372 | { |
| 1373 | unsigned char pq_pdt; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1374 | unsigned char * arr; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1375 | unsigned char *cmd = scp->cmnd; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1376 | int alloc_len, n, ret; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1377 | bool have_wlun, is_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1379 | alloc_len = get_unaligned_be16(cmd + 3); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 1380 | arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC); |
| 1381 | if (! arr) |
| 1382 | return DID_REQUEUE << 16; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1383 | is_disk = (sdebug_ptype == TYPE_DISK); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1384 | have_wlun = scsi_is_wlun(scp->device->lun); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1385 | if (have_wlun) |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1386 | pq_pdt = TYPE_WLUN; /* present, wlun */ |
| 1387 | else if (sdebug_no_lun_0 && (devip->lun == SDEBUG_LUN_0_VAL)) |
| 1388 | pq_pdt = 0x7f; /* not present, PQ=3, PDT=0x1f */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1389 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1390 | pq_pdt = (sdebug_ptype & 0x1f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | arr[0] = pq_pdt; |
| 1392 | if (0x2 & cmd[1]) { /* CMDDT bit set */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1393 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1394 | kfree(arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | return check_condition_result; |
| 1396 | } else if (0x1 & cmd[1]) { /* EVPD bit set */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1397 | int lu_id_num, port_group_id, target_dev_id, len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1398 | char lu_id_str[6]; |
| 1399 | int host_no = devip->sdbg_host->shost->host_no; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1401 | port_group_id = (((host_no + 1) & 0x7f) << 8) + |
| 1402 | (devip->channel & 0x7f); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1403 | if (sdebug_vpd_use_hostno == 0) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1404 | host_no = 0; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1405 | lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) + |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1406 | (devip->target * 1000) + devip->lun); |
| 1407 | target_dev_id = ((host_no + 1) * 2000) + |
| 1408 | (devip->target * 1000) - 3; |
| 1409 | len = scnprintf(lu_id_str, 6, "%d", lu_id_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | if (0 == cmd[2]) { /* supported vital product data pages */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1411 | arr[1] = cmd[2]; /*sanity */ |
| 1412 | n = 4; |
| 1413 | arr[n++] = 0x0; /* this page */ |
| 1414 | arr[n++] = 0x80; /* unit serial number */ |
| 1415 | arr[n++] = 0x83; /* device identification */ |
| 1416 | arr[n++] = 0x84; /* software interface ident. */ |
| 1417 | arr[n++] = 0x85; /* management network addresses */ |
| 1418 | arr[n++] = 0x86; /* extended inquiry */ |
| 1419 | arr[n++] = 0x87; /* mode page policy */ |
| 1420 | arr[n++] = 0x88; /* SCSI ports */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1421 | if (is_disk) { /* SBC only */ |
| 1422 | arr[n++] = 0x89; /* ATA information */ |
| 1423 | arr[n++] = 0xb0; /* Block limits */ |
| 1424 | arr[n++] = 0xb1; /* Block characteristics */ |
| 1425 | arr[n++] = 0xb2; /* Logical Block Prov */ |
| 1426 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1427 | arr[3] = n - 4; /* number of supported VPD pages */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | } else if (0x80 == cmd[2]) { /* unit serial number */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1429 | arr[1] = cmd[2]; /*sanity */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | arr[3] = len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1431 | memcpy(&arr[4], lu_id_str, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | } else if (0x83 == cmd[2]) { /* device identification */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1433 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1434 | arr[3] = inquiry_vpd_83(&arr[4], port_group_id, |
| 1435 | target_dev_id, lu_id_num, |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1436 | lu_id_str, len, |
| 1437 | &devip->lu_name); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1438 | } else if (0x84 == cmd[2]) { /* Software interface ident. */ |
| 1439 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1440 | arr[3] = inquiry_vpd_84(&arr[4]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1441 | } else if (0x85 == cmd[2]) { /* Management network addresses */ |
| 1442 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1443 | arr[3] = inquiry_vpd_85(&arr[4]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1444 | } else if (0x86 == cmd[2]) { /* extended inquiry */ |
| 1445 | arr[1] = cmd[2]; /*sanity */ |
| 1446 | arr[3] = 0x3c; /* number of following entries */ |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 1447 | if (sdebug_dif == T10_PI_TYPE3_PROTECTION) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1448 | arr[4] = 0x4; /* SPT: GRD_CHK:1 */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1449 | else if (have_dif_prot) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1450 | arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */ |
| 1451 | else |
| 1452 | arr[4] = 0x0; /* no protection stuff */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1453 | arr[5] = 0x7; /* head of q, ordered + simple q's */ |
| 1454 | } else if (0x87 == cmd[2]) { /* mode page policy */ |
| 1455 | arr[1] = cmd[2]; /*sanity */ |
| 1456 | arr[3] = 0x8; /* number of following entries */ |
| 1457 | arr[4] = 0x2; /* disconnect-reconnect mp */ |
| 1458 | arr[6] = 0x80; /* mlus, shared */ |
| 1459 | arr[8] = 0x18; /* protocol specific lu */ |
| 1460 | arr[10] = 0x82; /* mlus, per initiator port */ |
| 1461 | } else if (0x88 == cmd[2]) { /* SCSI Ports */ |
| 1462 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1463 | arr[3] = inquiry_vpd_88(&arr[4], target_dev_id); |
| 1464 | } else if (is_disk && 0x89 == cmd[2]) { /* ATA information */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1465 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1466 | n = inquiry_vpd_89(&arr[4]); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1467 | put_unaligned_be16(n, arr + 2); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1468 | } else if (is_disk && 0xb0 == cmd[2]) { /* Block limits */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1469 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1470 | arr[3] = inquiry_vpd_b0(&arr[4]); |
| 1471 | } else if (is_disk && 0xb1 == cmd[2]) { /* Block char. */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1472 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1473 | arr[3] = inquiry_vpd_b1(&arr[4]); |
| 1474 | } else if (is_disk && 0xb2 == cmd[2]) { /* LB Prov. */ |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1475 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1476 | arr[3] = inquiry_vpd_b2(&arr[4]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1478 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1479 | kfree(arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | return check_condition_result; |
| 1481 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1482 | len = min(get_unaligned_be16(arr + 2) + 4, alloc_len); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1483 | ret = fill_from_dev_buffer(scp, arr, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1484 | min(len, SDEBUG_MAX_INQ_ARR_SZ)); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1485 | kfree(arr); |
| 1486 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | } |
| 1488 | /* drops through here for a standard inquiry */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1489 | arr[1] = sdebug_removable ? 0x80 : 0; /* Removable disk */ |
| 1490 | arr[2] = sdebug_scsi_level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | arr[3] = 2; /* response_data_format==2 */ |
| 1492 | arr[4] = SDEBUG_LONG_INQ_SZ - 5; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 1493 | arr[5] = (int)have_dif_prot; /* PROTECT bit */ |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1494 | if (sdebug_vpd_use_hostno == 0) |
Martin K. Petersen | 70bdf20 | 2017-05-19 12:39:36 -0400 | [diff] [blame] | 1495 | arr[5] |= 0x10; /* claim: implicit TPGS */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1496 | arr[6] = 0x10; /* claim: MultiP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1498 | arr[7] = 0xa; /* claim: LINKED + CMDQUE */ |
Hannes Reinecke | e5203cf | 2017-10-02 16:26:33 +0200 | [diff] [blame] | 1499 | memcpy(&arr[8], sdebug_inq_vendor_id, 8); |
| 1500 | memcpy(&arr[16], sdebug_inq_product_id, 16); |
| 1501 | memcpy(&arr[32], sdebug_inq_product_rev, 4); |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 1502 | /* Use Vendor Specific area to place driver date in ASCII hex */ |
| 1503 | memcpy(&arr[36], sdebug_version_date, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | /* version descriptors (2 bytes each) follow */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1505 | put_unaligned_be16(0xc0, arr + 58); /* SAM-6 no version claimed */ |
| 1506 | put_unaligned_be16(0x5c0, arr + 60); /* SPC-5 no version claimed */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1507 | n = 62; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1508 | if (is_disk) { /* SBC-4 no version claimed */ |
| 1509 | put_unaligned_be16(0x600, arr + n); |
| 1510 | n += 2; |
| 1511 | } else if (sdebug_ptype == TYPE_TAPE) { /* SSC-4 rev 3 */ |
| 1512 | put_unaligned_be16(0x525, arr + n); |
| 1513 | n += 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | } |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1515 | put_unaligned_be16(0x2100, arr + n); /* SPL-4 no version claimed */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1516 | ret = fill_from_dev_buffer(scp, arr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | min(alloc_len, SDEBUG_LONG_INQ_SZ)); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1518 | kfree(arr); |
| 1519 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1522 | static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, |
| 1523 | 0, 0, 0x0, 0x0}; |
| 1524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | static int resp_requests(struct scsi_cmnd * scp, |
| 1526 | struct sdebug_dev_info * devip) |
| 1527 | { |
| 1528 | unsigned char * sbuff; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1529 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1530 | unsigned char arr[SCSI_SENSE_BUFFERSIZE]; |
Tomas Winkler | 2492fc0 | 2015-07-28 16:54:26 +0300 | [diff] [blame] | 1531 | bool dsense; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | int len = 18; |
| 1533 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1534 | memset(arr, 0, sizeof(arr)); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1535 | dsense = !!(cmd[1] & 1); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1536 | sbuff = scp->sense_buffer; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1537 | if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1538 | if (dsense) { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1539 | arr[0] = 0x72; |
| 1540 | arr[1] = 0x0; /* NO_SENSE in sense_key */ |
| 1541 | arr[2] = THRESHOLD_EXCEEDED; |
| 1542 | arr[3] = 0xff; /* TEST set and MRIE==6 */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1543 | len = 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1544 | } else { |
| 1545 | arr[0] = 0x70; |
| 1546 | arr[2] = 0x0; /* NO_SENSE in sense_key */ |
| 1547 | arr[7] = 0xa; /* 18 byte sense buffer */ |
| 1548 | arr[12] = THRESHOLD_EXCEEDED; |
| 1549 | arr[13] = 0xff; /* TEST set and MRIE==6 */ |
| 1550 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1551 | } else { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1552 | memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1553 | if (arr[0] >= 0x70 && dsense == sdebug_dsense) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1554 | ; /* have sense and formats match */ |
| 1555 | else if (arr[0] <= 0x70) { |
| 1556 | if (dsense) { |
| 1557 | memset(arr, 0, 8); |
| 1558 | arr[0] = 0x72; |
| 1559 | len = 8; |
| 1560 | } else { |
| 1561 | memset(arr, 0, 18); |
| 1562 | arr[0] = 0x70; |
| 1563 | arr[7] = 0xa; |
| 1564 | } |
| 1565 | } else if (dsense) { |
| 1566 | memset(arr, 0, 8); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1567 | arr[0] = 0x72; |
| 1568 | arr[1] = sbuff[2]; /* sense key */ |
| 1569 | arr[2] = sbuff[12]; /* asc */ |
| 1570 | arr[3] = sbuff[13]; /* ascq */ |
| 1571 | len = 8; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1572 | } else { |
| 1573 | memset(arr, 0, 18); |
| 1574 | arr[0] = 0x70; |
| 1575 | arr[2] = sbuff[1]; |
| 1576 | arr[7] = 0xa; |
| 1577 | arr[12] = sbuff[1]; |
| 1578 | arr[13] = sbuff[3]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1579 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1580 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1581 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1582 | mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | return fill_from_dev_buffer(scp, arr, len); |
| 1584 | } |
| 1585 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1586 | static int resp_start_stop(struct scsi_cmnd * scp, |
| 1587 | struct sdebug_dev_info * devip) |
| 1588 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1589 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 1590 | int power_cond, stop; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1591 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1592 | power_cond = (cmd[4] & 0xf0) >> 4; |
| 1593 | if (power_cond) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1594 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1595 | return check_condition_result; |
| 1596 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 1597 | stop = !(cmd[4] & 1); |
| 1598 | atomic_xchg(&devip->stopped, stop); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1599 | return 0; |
| 1600 | } |
| 1601 | |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1602 | static sector_t get_sdebug_capacity(void) |
| 1603 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1604 | static const unsigned int gibibyte = 1073741824; |
| 1605 | |
| 1606 | if (sdebug_virtual_gb > 0) |
| 1607 | return (sector_t)sdebug_virtual_gb * |
| 1608 | (gibibyte / sdebug_sector_size); |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1609 | else |
| 1610 | return sdebug_store_sectors; |
| 1611 | } |
| 1612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | #define SDEBUG_READCAP_ARR_SZ 8 |
| 1614 | static int resp_readcap(struct scsi_cmnd * scp, |
| 1615 | struct sdebug_dev_info * devip) |
| 1616 | { |
| 1617 | unsigned char arr[SDEBUG_READCAP_ARR_SZ]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1618 | unsigned int capac; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1620 | /* following just in case virtual_gb changed */ |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1621 | sdebug_capacity = get_sdebug_capacity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | memset(arr, 0, SDEBUG_READCAP_ARR_SZ); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1623 | if (sdebug_capacity < 0xffffffff) { |
| 1624 | capac = (unsigned int)sdebug_capacity - 1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1625 | put_unaligned_be32(capac, arr + 0); |
| 1626 | } else |
| 1627 | put_unaligned_be32(0xffffffff, arr + 0); |
| 1628 | put_unaligned_be16(sdebug_sector_size, arr + 6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ); |
| 1630 | } |
| 1631 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1632 | #define SDEBUG_READCAP16_ARR_SZ 32 |
| 1633 | static int resp_readcap16(struct scsi_cmnd * scp, |
| 1634 | struct sdebug_dev_info * devip) |
| 1635 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1636 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1637 | unsigned char arr[SDEBUG_READCAP16_ARR_SZ]; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1638 | int alloc_len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1639 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1640 | alloc_len = get_unaligned_be32(cmd + 10); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1641 | /* following just in case virtual_gb changed */ |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1642 | sdebug_capacity = get_sdebug_capacity(); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1643 | memset(arr, 0, SDEBUG_READCAP16_ARR_SZ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1644 | put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0); |
| 1645 | put_unaligned_be32(sdebug_sector_size, arr + 8); |
| 1646 | arr[13] = sdebug_physblk_exp & 0xf; |
| 1647 | arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1648 | |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1649 | if (scsi_debug_lbp()) { |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1650 | arr[14] |= 0x80; /* LBPME */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1651 | /* from sbc4r07, this LBPRZ field is 1 bit, but the LBPRZ in |
| 1652 | * the LB Provisioning VPD page is 3 bits. Note that lbprz=2 |
| 1653 | * in the wider field maps to 0 in this field. |
| 1654 | */ |
| 1655 | if (sdebug_lbprz & 1) /* precisely what the draft requires */ |
| 1656 | arr[14] |= 0x40; |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1657 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1658 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1659 | arr[15] = sdebug_lowest_aligned & 0xff; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1660 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1661 | if (have_dif_prot) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1662 | arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */ |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1663 | arr[12] |= 1; /* PROT_EN */ |
| 1664 | } |
| 1665 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1666 | return fill_from_dev_buffer(scp, arr, |
| 1667 | min(alloc_len, SDEBUG_READCAP16_ARR_SZ)); |
| 1668 | } |
| 1669 | |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1670 | #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412 |
| 1671 | |
| 1672 | static int resp_report_tgtpgs(struct scsi_cmnd * scp, |
| 1673 | struct sdebug_dev_info * devip) |
| 1674 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1675 | unsigned char *cmd = scp->cmnd; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1676 | unsigned char * arr; |
| 1677 | int host_no = devip->sdbg_host->shost->host_no; |
| 1678 | int n, ret, alen, rlen; |
| 1679 | int port_group_a, port_group_b, port_a, port_b; |
| 1680 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1681 | alen = get_unaligned_be32(cmd + 6); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 1682 | arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC); |
| 1683 | if (! arr) |
| 1684 | return DID_REQUEUE << 16; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1685 | /* |
| 1686 | * EVPD page 0x88 states we have two ports, one |
| 1687 | * real and a fake port with no device connected. |
| 1688 | * So we create two port groups with one port each |
| 1689 | * and set the group with port B to unavailable. |
| 1690 | */ |
| 1691 | port_a = 0x1; /* relative port A */ |
| 1692 | port_b = 0x2; /* relative port B */ |
| 1693 | port_group_a = (((host_no + 1) & 0x7f) << 8) + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1694 | (devip->channel & 0x7f); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1695 | port_group_b = (((host_no + 1) & 0x7f) << 8) + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1696 | (devip->channel & 0x7f) + 0x80; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1697 | |
| 1698 | /* |
| 1699 | * The asymmetric access state is cycled according to the host_id. |
| 1700 | */ |
| 1701 | n = 4; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1702 | if (sdebug_vpd_use_hostno == 0) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1703 | arr[n++] = host_no % 3; /* Asymm access state */ |
| 1704 | arr[n++] = 0x0F; /* claim: all states are supported */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1705 | } else { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1706 | arr[n++] = 0x0; /* Active/Optimized path */ |
| 1707 | arr[n++] = 0x01; /* only support active/optimized paths */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1708 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1709 | put_unaligned_be16(port_group_a, arr + n); |
| 1710 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1711 | arr[n++] = 0; /* Reserved */ |
| 1712 | arr[n++] = 0; /* Status code */ |
| 1713 | arr[n++] = 0; /* Vendor unique */ |
| 1714 | arr[n++] = 0x1; /* One port per group */ |
| 1715 | arr[n++] = 0; /* Reserved */ |
| 1716 | arr[n++] = 0; /* Reserved */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1717 | put_unaligned_be16(port_a, arr + n); |
| 1718 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1719 | arr[n++] = 3; /* Port unavailable */ |
| 1720 | arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1721 | put_unaligned_be16(port_group_b, arr + n); |
| 1722 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1723 | arr[n++] = 0; /* Reserved */ |
| 1724 | arr[n++] = 0; /* Status code */ |
| 1725 | arr[n++] = 0; /* Vendor unique */ |
| 1726 | arr[n++] = 0x1; /* One port per group */ |
| 1727 | arr[n++] = 0; /* Reserved */ |
| 1728 | arr[n++] = 0; /* Reserved */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1729 | put_unaligned_be16(port_b, arr + n); |
| 1730 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1731 | |
| 1732 | rlen = n - 4; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1733 | put_unaligned_be32(rlen, arr + 0); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1734 | |
| 1735 | /* |
| 1736 | * Return the smallest value of either |
| 1737 | * - The allocated length |
| 1738 | * - The constructed command length |
| 1739 | * - The maximum array size |
| 1740 | */ |
| 1741 | rlen = min(alen,n); |
| 1742 | ret = fill_from_dev_buffer(scp, arr, |
| 1743 | min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ)); |
| 1744 | kfree(arr); |
| 1745 | return ret; |
| 1746 | } |
| 1747 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1748 | static int resp_rsup_opcodes(struct scsi_cmnd *scp, |
| 1749 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1750 | { |
| 1751 | bool rctd; |
| 1752 | u8 reporting_opts, req_opcode, sdeb_i, supp; |
| 1753 | u16 req_sa, u; |
| 1754 | u32 alloc_len, a_len; |
| 1755 | int k, offset, len, errsts, count, bump, na; |
| 1756 | const struct opcode_info_t *oip; |
| 1757 | const struct opcode_info_t *r_oip; |
| 1758 | u8 *arr; |
| 1759 | u8 *cmd = scp->cmnd; |
| 1760 | |
| 1761 | rctd = !!(cmd[2] & 0x80); |
| 1762 | reporting_opts = cmd[2] & 0x7; |
| 1763 | req_opcode = cmd[3]; |
| 1764 | req_sa = get_unaligned_be16(cmd + 4); |
| 1765 | alloc_len = get_unaligned_be32(cmd + 6); |
Colin Ian King | 6d310df | 2015-01-22 11:20:40 +0000 | [diff] [blame] | 1766 | if (alloc_len < 4 || alloc_len > 0xffff) { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1767 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
| 1768 | return check_condition_result; |
| 1769 | } |
| 1770 | if (alloc_len > 8192) |
| 1771 | a_len = 8192; |
| 1772 | else |
| 1773 | a_len = alloc_len; |
Sasha Levin | 99531e6 | 2015-01-17 17:47:37 -0500 | [diff] [blame] | 1774 | arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1775 | if (NULL == arr) { |
| 1776 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 1777 | INSUFF_RES_ASCQ); |
| 1778 | return check_condition_result; |
| 1779 | } |
| 1780 | switch (reporting_opts) { |
| 1781 | case 0: /* all commands */ |
| 1782 | /* count number of commands */ |
| 1783 | for (count = 0, oip = opcode_info_arr; |
| 1784 | oip->num_attached != 0xff; ++oip) { |
| 1785 | if (F_INV_OP & oip->flags) |
| 1786 | continue; |
| 1787 | count += (oip->num_attached + 1); |
| 1788 | } |
| 1789 | bump = rctd ? 20 : 8; |
| 1790 | put_unaligned_be32(count * bump, arr); |
| 1791 | for (offset = 4, oip = opcode_info_arr; |
| 1792 | oip->num_attached != 0xff && offset < a_len; ++oip) { |
| 1793 | if (F_INV_OP & oip->flags) |
| 1794 | continue; |
| 1795 | na = oip->num_attached; |
| 1796 | arr[offset] = oip->opcode; |
| 1797 | put_unaligned_be16(oip->sa, arr + offset + 2); |
| 1798 | if (rctd) |
| 1799 | arr[offset + 5] |= 0x2; |
| 1800 | if (FF_SA & oip->flags) |
| 1801 | arr[offset + 5] |= 0x1; |
| 1802 | put_unaligned_be16(oip->len_mask[0], arr + offset + 6); |
| 1803 | if (rctd) |
| 1804 | put_unaligned_be16(0xa, arr + offset + 8); |
| 1805 | r_oip = oip; |
| 1806 | for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) { |
| 1807 | if (F_INV_OP & oip->flags) |
| 1808 | continue; |
| 1809 | offset += bump; |
| 1810 | arr[offset] = oip->opcode; |
| 1811 | put_unaligned_be16(oip->sa, arr + offset + 2); |
| 1812 | if (rctd) |
| 1813 | arr[offset + 5] |= 0x2; |
| 1814 | if (FF_SA & oip->flags) |
| 1815 | arr[offset + 5] |= 0x1; |
| 1816 | put_unaligned_be16(oip->len_mask[0], |
| 1817 | arr + offset + 6); |
| 1818 | if (rctd) |
| 1819 | put_unaligned_be16(0xa, |
| 1820 | arr + offset + 8); |
| 1821 | } |
| 1822 | oip = r_oip; |
| 1823 | offset += bump; |
| 1824 | } |
| 1825 | break; |
| 1826 | case 1: /* one command: opcode only */ |
| 1827 | case 2: /* one command: opcode plus service action */ |
| 1828 | case 3: /* one command: if sa==0 then opcode only else opcode+sa */ |
| 1829 | sdeb_i = opcode_ind_arr[req_opcode]; |
| 1830 | oip = &opcode_info_arr[sdeb_i]; |
| 1831 | if (F_INV_OP & oip->flags) { |
| 1832 | supp = 1; |
| 1833 | offset = 4; |
| 1834 | } else { |
| 1835 | if (1 == reporting_opts) { |
| 1836 | if (FF_SA & oip->flags) { |
| 1837 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, |
| 1838 | 2, 2); |
| 1839 | kfree(arr); |
| 1840 | return check_condition_result; |
| 1841 | } |
| 1842 | req_sa = 0; |
| 1843 | } else if (2 == reporting_opts && |
| 1844 | 0 == (FF_SA & oip->flags)) { |
| 1845 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1); |
| 1846 | kfree(arr); /* point at requested sa */ |
| 1847 | return check_condition_result; |
| 1848 | } |
| 1849 | if (0 == (FF_SA & oip->flags) && |
| 1850 | req_opcode == oip->opcode) |
| 1851 | supp = 3; |
| 1852 | else if (0 == (FF_SA & oip->flags)) { |
| 1853 | na = oip->num_attached; |
| 1854 | for (k = 0, oip = oip->arrp; k < na; |
| 1855 | ++k, ++oip) { |
| 1856 | if (req_opcode == oip->opcode) |
| 1857 | break; |
| 1858 | } |
| 1859 | supp = (k >= na) ? 1 : 3; |
| 1860 | } else if (req_sa != oip->sa) { |
| 1861 | na = oip->num_attached; |
| 1862 | for (k = 0, oip = oip->arrp; k < na; |
| 1863 | ++k, ++oip) { |
| 1864 | if (req_sa == oip->sa) |
| 1865 | break; |
| 1866 | } |
| 1867 | supp = (k >= na) ? 1 : 3; |
| 1868 | } else |
| 1869 | supp = 3; |
| 1870 | if (3 == supp) { |
| 1871 | u = oip->len_mask[0]; |
| 1872 | put_unaligned_be16(u, arr + 2); |
| 1873 | arr[4] = oip->opcode; |
| 1874 | for (k = 1; k < u; ++k) |
| 1875 | arr[4 + k] = (k < 16) ? |
| 1876 | oip->len_mask[k] : 0xff; |
| 1877 | offset = 4 + u; |
| 1878 | } else |
| 1879 | offset = 4; |
| 1880 | } |
| 1881 | arr[1] = (rctd ? 0x80 : 0) | supp; |
| 1882 | if (rctd) { |
| 1883 | put_unaligned_be16(0xa, arr + offset); |
| 1884 | offset += 12; |
| 1885 | } |
| 1886 | break; |
| 1887 | default: |
| 1888 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2); |
| 1889 | kfree(arr); |
| 1890 | return check_condition_result; |
| 1891 | } |
| 1892 | offset = (offset < a_len) ? offset : a_len; |
| 1893 | len = (offset < alloc_len) ? offset : alloc_len; |
| 1894 | errsts = fill_from_dev_buffer(scp, arr, len); |
| 1895 | kfree(arr); |
| 1896 | return errsts; |
| 1897 | } |
| 1898 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1899 | static int resp_rsup_tmfs(struct scsi_cmnd *scp, |
| 1900 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1901 | { |
| 1902 | bool repd; |
| 1903 | u32 alloc_len, len; |
| 1904 | u8 arr[16]; |
| 1905 | u8 *cmd = scp->cmnd; |
| 1906 | |
| 1907 | memset(arr, 0, sizeof(arr)); |
| 1908 | repd = !!(cmd[2] & 0x80); |
| 1909 | alloc_len = get_unaligned_be32(cmd + 6); |
| 1910 | if (alloc_len < 4) { |
| 1911 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
| 1912 | return check_condition_result; |
| 1913 | } |
| 1914 | arr[0] = 0xc8; /* ATS | ATSS | LURS */ |
| 1915 | arr[1] = 0x1; /* ITNRS */ |
| 1916 | if (repd) { |
| 1917 | arr[3] = 0xc; |
| 1918 | len = 16; |
| 1919 | } else |
| 1920 | len = 4; |
| 1921 | |
| 1922 | len = (len < alloc_len) ? len : alloc_len; |
| 1923 | return fill_from_dev_buffer(scp, arr, len); |
| 1924 | } |
| 1925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | /* <<Following mode page info copied from ST318451LW>> */ |
| 1927 | |
| 1928 | static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target) |
| 1929 | { /* Read-Write Error Recovery page for mode_sense */ |
| 1930 | unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0, |
| 1931 | 5, 0, 0xff, 0xff}; |
| 1932 | |
| 1933 | memcpy(p, err_recov_pg, sizeof(err_recov_pg)); |
| 1934 | if (1 == pcontrol) |
| 1935 | memset(p + 2, 0, sizeof(err_recov_pg) - 2); |
| 1936 | return sizeof(err_recov_pg); |
| 1937 | } |
| 1938 | |
| 1939 | static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target) |
| 1940 | { /* Disconnect-Reconnect page for mode_sense */ |
| 1941 | unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0, |
| 1942 | 0, 0, 0, 0, 0, 0, 0, 0}; |
| 1943 | |
| 1944 | memcpy(p, disconnect_pg, sizeof(disconnect_pg)); |
| 1945 | if (1 == pcontrol) |
| 1946 | memset(p + 2, 0, sizeof(disconnect_pg) - 2); |
| 1947 | return sizeof(disconnect_pg); |
| 1948 | } |
| 1949 | |
| 1950 | static int resp_format_pg(unsigned char * p, int pcontrol, int target) |
| 1951 | { /* Format device page for mode_sense */ |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1952 | unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0, |
| 1953 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1954 | 0, 0, 0, 0, 0x40, 0, 0, 0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1956 | memcpy(p, format_pg, sizeof(format_pg)); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1957 | put_unaligned_be16(sdebug_sectors_per, p + 10); |
| 1958 | put_unaligned_be16(sdebug_sector_size, p + 12); |
| 1959 | if (sdebug_removable) |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1960 | p[20] |= 0x20; /* should agree with INQUIRY */ |
| 1961 | if (1 == pcontrol) |
| 1962 | memset(p + 2, 0, sizeof(format_pg) - 2); |
| 1963 | return sizeof(format_pg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1966 | static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0, |
| 1967 | 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, |
| 1968 | 0, 0, 0, 0}; |
| 1969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | static int resp_caching_pg(unsigned char * p, int pcontrol, int target) |
| 1971 | { /* Caching page for mode_sense */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1972 | unsigned char ch_caching_pg[] = {/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0, |
| 1973 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 1974 | 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] | 1975 | 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0}; |
| 1976 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1977 | if (SDEBUG_OPT_N_WCE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1978 | caching_pg[2] &= ~0x4; /* set WCE=0 (default WCE=1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | memcpy(p, caching_pg, sizeof(caching_pg)); |
| 1980 | if (1 == pcontrol) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1981 | memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg)); |
| 1982 | else if (2 == pcontrol) |
| 1983 | memcpy(p, d_caching_pg, sizeof(d_caching_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | return sizeof(caching_pg); |
| 1985 | } |
| 1986 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1987 | static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0, |
| 1988 | 0, 0, 0x2, 0x4b}; |
| 1989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target) |
| 1991 | { /* Control mode page for mode_sense */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1992 | 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] | 1993 | 0, 0, 0, 0}; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1994 | 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] | 1995 | 0, 0, 0x2, 0x4b}; |
| 1996 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1997 | if (sdebug_dsense) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | ctrl_m_pg[2] |= 0x4; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1999 | else |
| 2000 | ctrl_m_pg[2] &= ~0x4; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2001 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2002 | if (sdebug_ato) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2003 | ctrl_m_pg[5] |= 0x80; /* ATO=1 */ |
| 2004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg)); |
| 2006 | if (1 == pcontrol) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2007 | memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg)); |
| 2008 | else if (2 == pcontrol) |
| 2009 | memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | return sizeof(ctrl_m_pg); |
| 2011 | } |
| 2012 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target) |
| 2015 | { /* Informational Exceptions control mode page for mode_sense */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2016 | unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0, |
| 2017 | 0, 0, 0x0, 0x0}; |
| 2018 | unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, |
| 2019 | 0, 0, 0x0, 0x0}; |
| 2020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | memcpy(p, iec_m_pg, sizeof(iec_m_pg)); |
| 2022 | if (1 == pcontrol) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2023 | memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg)); |
| 2024 | else if (2 == pcontrol) |
| 2025 | memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | return sizeof(iec_m_pg); |
| 2027 | } |
| 2028 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2029 | static int resp_sas_sf_m_pg(unsigned char * p, int pcontrol, int target) |
| 2030 | { /* SAS SSP mode page - short format for mode_sense */ |
| 2031 | unsigned char sas_sf_m_pg[] = {0x19, 0x6, |
| 2032 | 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0}; |
| 2033 | |
| 2034 | memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg)); |
| 2035 | if (1 == pcontrol) |
| 2036 | memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2); |
| 2037 | return sizeof(sas_sf_m_pg); |
| 2038 | } |
| 2039 | |
| 2040 | |
| 2041 | static int resp_sas_pcd_m_spg(unsigned char * p, int pcontrol, int target, |
| 2042 | int target_dev_id) |
| 2043 | { /* SAS phy control and discover mode page for mode_sense */ |
| 2044 | unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2, |
| 2045 | 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2046 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
| 2047 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2048 | 0x2, 0, 0, 0, 0, 0, 0, 0, |
| 2049 | 0x88, 0x99, 0, 0, 0, 0, 0, 0, |
| 2050 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 2051 | 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2052 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
| 2053 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2054 | 0x3, 0, 0, 0, 0, 0, 0, 0, |
| 2055 | 0x88, 0x99, 0, 0, 0, 0, 0, 0, |
| 2056 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 2057 | }; |
| 2058 | int port_a, port_b; |
| 2059 | |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 2060 | put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 16); |
| 2061 | put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 24); |
| 2062 | put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 64); |
| 2063 | put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 72); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2064 | port_a = target_dev_id + 1; |
| 2065 | port_b = port_a + 1; |
| 2066 | memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg)); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2067 | put_unaligned_be32(port_a, p + 20); |
| 2068 | put_unaligned_be32(port_b, p + 48 + 20); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2069 | if (1 == pcontrol) |
| 2070 | memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4); |
| 2071 | return sizeof(sas_pcd_m_pg); |
| 2072 | } |
| 2073 | |
| 2074 | static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol) |
| 2075 | { /* SAS SSP shared protocol specific port mode subpage */ |
| 2076 | unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0, |
| 2077 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 2078 | }; |
| 2079 | |
| 2080 | memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg)); |
| 2081 | if (1 == pcontrol) |
| 2082 | memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4); |
| 2083 | return sizeof(sas_sha_m_pg); |
| 2084 | } |
| 2085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | #define SDEBUG_MAX_MSENSE_SZ 256 |
| 2087 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2088 | static int resp_mode_sense(struct scsi_cmnd *scp, |
| 2089 | struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | { |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2091 | int pcontrol, pcode, subpcode, bd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | unsigned char dev_spec; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2093 | int alloc_len, offset, len, target_dev_id; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2094 | int target = scp->device->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | unsigned char * ap; |
| 2096 | unsigned char arr[SDEBUG_MAX_MSENSE_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2097 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2098 | bool dbd, llbaa, msense_6, is_disk, bad_pcode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2100 | dbd = !!(cmd[1] & 0x8); /* disable block descriptors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | pcontrol = (cmd[2] & 0xc0) >> 6; |
| 2102 | pcode = cmd[2] & 0x3f; |
| 2103 | subpcode = cmd[3]; |
| 2104 | msense_6 = (MODE_SENSE == cmd[0]); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2105 | llbaa = msense_6 ? false : !!(cmd[1] & 0x10); |
| 2106 | is_disk = (sdebug_ptype == TYPE_DISK); |
| 2107 | if (is_disk && !dbd) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2108 | bd_len = llbaa ? 16 : 8; |
| 2109 | else |
| 2110 | bd_len = 0; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2111 | alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | memset(arr, 0, SDEBUG_MAX_MSENSE_SZ); |
| 2113 | if (0x3 == pcontrol) { /* Saving values not supported */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2114 | mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | return check_condition_result; |
| 2116 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2117 | target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) + |
| 2118 | (devip->target * 1000) - 3; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 2119 | /* for disks set DPOFUA bit and clear write protect (WP) bit */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2120 | if (is_disk) |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 2121 | dev_spec = 0x10; /* =0x90 if WP=1 implies read-only */ |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2122 | else |
| 2123 | dev_spec = 0x0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | if (msense_6) { |
| 2125 | arr[2] = dev_spec; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2126 | arr[3] = bd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | offset = 4; |
| 2128 | } else { |
| 2129 | arr[3] = dev_spec; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2130 | if (16 == bd_len) |
| 2131 | arr[4] = 0x1; /* set LONGLBA bit */ |
| 2132 | arr[7] = bd_len; /* assume 255 or less */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | offset = 8; |
| 2134 | } |
| 2135 | ap = arr + offset; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 2136 | if ((bd_len > 0) && (!sdebug_capacity)) |
| 2137 | sdebug_capacity = get_sdebug_capacity(); |
| 2138 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2139 | if (8 == bd_len) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2140 | if (sdebug_capacity > 0xfffffffe) |
| 2141 | put_unaligned_be32(0xffffffff, ap + 0); |
| 2142 | else |
| 2143 | put_unaligned_be32(sdebug_capacity, ap + 0); |
| 2144 | put_unaligned_be16(sdebug_sector_size, ap + 6); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2145 | offset += bd_len; |
| 2146 | ap = arr + offset; |
| 2147 | } else if (16 == bd_len) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2148 | put_unaligned_be64((u64)sdebug_capacity, ap + 0); |
| 2149 | put_unaligned_be32(sdebug_sector_size, ap + 12); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2150 | offset += bd_len; |
| 2151 | ap = arr + offset; |
| 2152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2154 | if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) { |
| 2155 | /* TODO: Control Extension page */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2156 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | return check_condition_result; |
| 2158 | } |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2159 | bad_pcode = false; |
| 2160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | switch (pcode) { |
| 2162 | case 0x1: /* Read-Write error recovery page, direct access */ |
| 2163 | len = resp_err_recov_pg(ap, pcontrol, target); |
| 2164 | offset += len; |
| 2165 | break; |
| 2166 | case 0x2: /* Disconnect-Reconnect page, all devices */ |
| 2167 | len = resp_disconnect_pg(ap, pcontrol, target); |
| 2168 | offset += len; |
| 2169 | break; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2170 | case 0x3: /* Format device page, direct access */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2171 | if (is_disk) { |
| 2172 | len = resp_format_pg(ap, pcontrol, target); |
| 2173 | offset += len; |
| 2174 | } else |
| 2175 | bad_pcode = true; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2176 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | case 0x8: /* Caching page, direct access */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2178 | if (is_disk) { |
| 2179 | len = resp_caching_pg(ap, pcontrol, target); |
| 2180 | offset += len; |
| 2181 | } else |
| 2182 | bad_pcode = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | break; |
| 2184 | case 0xa: /* Control Mode page, all devices */ |
| 2185 | len = resp_ctrl_m_pg(ap, pcontrol, target); |
| 2186 | offset += len; |
| 2187 | break; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2188 | case 0x19: /* if spc==1 then sas phy, control+discover */ |
| 2189 | if ((subpcode > 0x2) && (subpcode < 0xff)) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2190 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2191 | return check_condition_result; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2192 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2193 | len = 0; |
| 2194 | if ((0x0 == subpcode) || (0xff == subpcode)) |
| 2195 | len += resp_sas_sf_m_pg(ap + len, pcontrol, target); |
| 2196 | if ((0x1 == subpcode) || (0xff == subpcode)) |
| 2197 | len += resp_sas_pcd_m_spg(ap + len, pcontrol, target, |
| 2198 | target_dev_id); |
| 2199 | if ((0x2 == subpcode) || (0xff == subpcode)) |
| 2200 | len += resp_sas_sha_m_spg(ap + len, pcontrol); |
| 2201 | offset += len; |
| 2202 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | case 0x1c: /* Informational Exceptions Mode page, all devices */ |
| 2204 | len = resp_iec_m_pg(ap, pcontrol, target); |
| 2205 | offset += len; |
| 2206 | break; |
| 2207 | case 0x3f: /* Read all Mode pages */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2208 | if ((0 == subpcode) || (0xff == subpcode)) { |
| 2209 | len = resp_err_recov_pg(ap, pcontrol, target); |
| 2210 | len += resp_disconnect_pg(ap + len, pcontrol, target); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2211 | if (is_disk) { |
| 2212 | len += resp_format_pg(ap + len, pcontrol, |
| 2213 | target); |
| 2214 | len += resp_caching_pg(ap + len, pcontrol, |
| 2215 | target); |
| 2216 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2217 | len += resp_ctrl_m_pg(ap + len, pcontrol, target); |
| 2218 | len += resp_sas_sf_m_pg(ap + len, pcontrol, target); |
| 2219 | if (0xff == subpcode) { |
| 2220 | len += resp_sas_pcd_m_spg(ap + len, pcontrol, |
| 2221 | target, target_dev_id); |
| 2222 | len += resp_sas_sha_m_spg(ap + len, pcontrol); |
| 2223 | } |
| 2224 | len += resp_iec_m_pg(ap + len, pcontrol, target); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2225 | offset += len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2226 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2227 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2228 | return check_condition_result; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | break; |
| 2231 | default: |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2232 | bad_pcode = true; |
| 2233 | break; |
| 2234 | } |
| 2235 | if (bad_pcode) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2236 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | return check_condition_result; |
| 2238 | } |
| 2239 | if (msense_6) |
| 2240 | arr[0] = offset - 1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2241 | else |
| 2242 | put_unaligned_be16((offset - 2), arr + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | return fill_from_dev_buffer(scp, arr, min(alloc_len, offset)); |
| 2244 | } |
| 2245 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2246 | #define SDEBUG_MAX_MSELECT_SZ 512 |
| 2247 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2248 | static int resp_mode_select(struct scsi_cmnd *scp, |
| 2249 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2250 | { |
| 2251 | int pf, sp, ps, md_len, bd_len, off, spf, pg_len; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2252 | int param_len, res, mpage; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2253 | unsigned char arr[SDEBUG_MAX_MSELECT_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2254 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2255 | int mselect6 = (MODE_SELECT == cmd[0]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2256 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2257 | memset(arr, 0, sizeof(arr)); |
| 2258 | pf = cmd[1] & 0x10; |
| 2259 | sp = cmd[1] & 0x1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2260 | param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2261 | if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2262 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2263 | return check_condition_result; |
| 2264 | } |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2265 | res = fetch_to_dev_buffer(scp, arr, param_len); |
| 2266 | if (-1 == res) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2267 | return DID_ERROR << 16; |
| 2268 | else if (sdebug_verbose && (res < param_len)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2269 | sdev_printk(KERN_INFO, scp->device, |
| 2270 | "%s: cdb indicated=%d, IO sent=%d bytes\n", |
| 2271 | __func__, param_len, res); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2272 | md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2); |
| 2273 | bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2274 | if (md_len > 2) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2275 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2276 | return check_condition_result; |
| 2277 | } |
| 2278 | off = bd_len + (mselect6 ? 4 : 8); |
| 2279 | mpage = arr[off] & 0x3f; |
| 2280 | ps = !!(arr[off] & 0x80); |
| 2281 | if (ps) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2282 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2283 | return check_condition_result; |
| 2284 | } |
| 2285 | spf = !!(arr[off] & 0x40); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2286 | pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) : |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2287 | (arr[off + 1] + 2); |
| 2288 | if ((pg_len + off) > param_len) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2289 | mk_sense_buffer(scp, ILLEGAL_REQUEST, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2290 | PARAMETER_LIST_LENGTH_ERR, 0); |
| 2291 | return check_condition_result; |
| 2292 | } |
| 2293 | switch (mpage) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2294 | case 0x8: /* Caching Mode page */ |
| 2295 | if (caching_pg[1] == arr[off + 1]) { |
| 2296 | memcpy(caching_pg + 2, arr + off + 2, |
| 2297 | sizeof(caching_pg) - 2); |
| 2298 | goto set_mode_changed_ua; |
| 2299 | } |
| 2300 | break; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2301 | case 0xa: /* Control Mode page */ |
| 2302 | if (ctrl_m_pg[1] == arr[off + 1]) { |
| 2303 | memcpy(ctrl_m_pg + 2, arr + off + 2, |
| 2304 | sizeof(ctrl_m_pg) - 2); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2305 | sdebug_dsense = !!(ctrl_m_pg[2] & 0x4); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2306 | goto set_mode_changed_ua; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2307 | } |
| 2308 | break; |
| 2309 | case 0x1c: /* Informational Exceptions Mode page */ |
| 2310 | if (iec_m_pg[1] == arr[off + 1]) { |
| 2311 | memcpy(iec_m_pg + 2, arr + off + 2, |
| 2312 | sizeof(iec_m_pg) - 2); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2313 | goto set_mode_changed_ua; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2314 | } |
| 2315 | break; |
| 2316 | default: |
| 2317 | break; |
| 2318 | } |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2319 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2320 | return check_condition_result; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2321 | set_mode_changed_ua: |
| 2322 | set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm); |
| 2323 | return 0; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | static int resp_temp_l_pg(unsigned char * arr) |
| 2327 | { |
| 2328 | unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38, |
| 2329 | 0x0, 0x1, 0x3, 0x2, 0x0, 65, |
| 2330 | }; |
| 2331 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2332 | memcpy(arr, temp_l_pg, sizeof(temp_l_pg)); |
| 2333 | return sizeof(temp_l_pg); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | static int resp_ie_l_pg(unsigned char * arr) |
| 2337 | { |
| 2338 | unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38, |
| 2339 | }; |
| 2340 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2341 | memcpy(arr, ie_l_pg, sizeof(ie_l_pg)); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2342 | if (iec_m_pg[2] & 0x4) { /* TEST bit set */ |
| 2343 | arr[4] = THRESHOLD_EXCEEDED; |
| 2344 | arr[5] = 0xff; |
| 2345 | } |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2346 | return sizeof(ie_l_pg); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | #define SDEBUG_MAX_LSENSE_SZ 512 |
| 2350 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2351 | static int resp_log_sense(struct scsi_cmnd *scp, |
| 2352 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2353 | { |
Bart Van Assche | ab17241 | 2017-08-25 13:46:42 -0700 | [diff] [blame] | 2354 | int ppc, sp, pcode, subpcode, alloc_len, len, n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2355 | unsigned char arr[SDEBUG_MAX_LSENSE_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2356 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2357 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2358 | memset(arr, 0, sizeof(arr)); |
| 2359 | ppc = cmd[1] & 0x2; |
| 2360 | sp = cmd[1] & 0x1; |
| 2361 | if (ppc || sp) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2362 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2363 | return check_condition_result; |
| 2364 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2365 | pcode = cmd[2] & 0x3f; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2366 | subpcode = cmd[3] & 0xff; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2367 | alloc_len = get_unaligned_be16(cmd + 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2368 | arr[0] = pcode; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2369 | if (0 == subpcode) { |
| 2370 | switch (pcode) { |
| 2371 | case 0x0: /* Supported log pages log page */ |
| 2372 | n = 4; |
| 2373 | arr[n++] = 0x0; /* this page */ |
| 2374 | arr[n++] = 0xd; /* Temperature */ |
| 2375 | arr[n++] = 0x2f; /* Informational exceptions */ |
| 2376 | arr[3] = n - 4; |
| 2377 | break; |
| 2378 | case 0xd: /* Temperature log page */ |
| 2379 | arr[3] = resp_temp_l_pg(arr + 4); |
| 2380 | break; |
| 2381 | case 0x2f: /* Informational exceptions log page */ |
| 2382 | arr[3] = resp_ie_l_pg(arr + 4); |
| 2383 | break; |
| 2384 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2385 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2386 | return check_condition_result; |
| 2387 | } |
| 2388 | } else if (0xff == subpcode) { |
| 2389 | arr[0] |= 0x40; |
| 2390 | arr[1] = subpcode; |
| 2391 | switch (pcode) { |
| 2392 | case 0x0: /* Supported log pages and subpages log page */ |
| 2393 | n = 4; |
| 2394 | arr[n++] = 0x0; |
| 2395 | arr[n++] = 0x0; /* 0,0 page */ |
| 2396 | arr[n++] = 0x0; |
| 2397 | arr[n++] = 0xff; /* this page */ |
| 2398 | arr[n++] = 0xd; |
| 2399 | arr[n++] = 0x0; /* Temperature */ |
| 2400 | arr[n++] = 0x2f; |
| 2401 | arr[n++] = 0x0; /* Informational exceptions */ |
| 2402 | arr[3] = n - 4; |
| 2403 | break; |
| 2404 | case 0xd: /* Temperature subpages */ |
| 2405 | n = 4; |
| 2406 | arr[n++] = 0xd; |
| 2407 | arr[n++] = 0x0; /* Temperature */ |
| 2408 | arr[3] = n - 4; |
| 2409 | break; |
| 2410 | case 0x2f: /* Informational exceptions subpages */ |
| 2411 | n = 4; |
| 2412 | arr[n++] = 0x2f; |
| 2413 | arr[n++] = 0x0; /* Informational exceptions */ |
| 2414 | arr[3] = n - 4; |
| 2415 | break; |
| 2416 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2417 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2418 | return check_condition_result; |
| 2419 | } |
| 2420 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2421 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2422 | return check_condition_result; |
| 2423 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2424 | len = min(get_unaligned_be16(arr + 2) + 4, alloc_len); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2425 | return fill_from_dev_buffer(scp, arr, |
| 2426 | min(len, SDEBUG_MAX_INQ_ARR_SZ)); |
| 2427 | } |
| 2428 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2429 | static int check_device_access_params(struct scsi_cmnd *scp, |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2430 | unsigned long long lba, unsigned int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2432 | if (lba + num > sdebug_capacity) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2433 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | return check_condition_result; |
| 2435 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2436 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 2437 | if (num > sdebug_store_sectors) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2438 | /* needs work to find which cdb byte 'num' comes from */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2439 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2440 | return check_condition_result; |
| 2441 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2442 | return 0; |
| 2443 | } |
| 2444 | |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2445 | /* Returns number of bytes copied or -1 if error. */ |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 2446 | static int do_device_access(struct scsi_cmnd *scmd, u32 sg_skip, u64 lba, |
| 2447 | u32 num, bool do_write) |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2448 | { |
| 2449 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2450 | u64 block, rest = 0; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2451 | struct scsi_data_buffer *sdb; |
| 2452 | enum dma_data_direction dir; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2453 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2454 | if (do_write) { |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2455 | sdb = scsi_out(scmd); |
| 2456 | dir = DMA_TO_DEVICE; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2457 | } else { |
| 2458 | sdb = scsi_in(scmd); |
| 2459 | dir = DMA_FROM_DEVICE; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | if (!sdb->length) |
| 2463 | return 0; |
| 2464 | if (!(scsi_bidi_cmnd(scmd) || scmd->sc_data_direction == dir)) |
| 2465 | return -1; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2466 | |
| 2467 | block = do_div(lba, sdebug_store_sectors); |
| 2468 | if (block + num > sdebug_store_sectors) |
| 2469 | rest = block + num - sdebug_store_sectors; |
| 2470 | |
Dave Gordon | 386ecb1 | 2015-06-30 14:58:57 -0700 | [diff] [blame] | 2471 | ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2472 | fake_storep + (block * sdebug_sector_size), |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 2473 | (num - rest) * sdebug_sector_size, sg_skip, do_write); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2474 | if (ret != (num - rest) * sdebug_sector_size) |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2475 | return ret; |
| 2476 | |
| 2477 | if (rest) { |
Dave Gordon | 386ecb1 | 2015-06-30 14:58:57 -0700 | [diff] [blame] | 2478 | ret += sg_copy_buffer(sdb->table.sgl, sdb->table.nents, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2479 | fake_storep, rest * sdebug_sector_size, |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 2480 | sg_skip + ((num - rest) * sdebug_sector_size), |
| 2481 | do_write); |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2482 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2483 | |
| 2484 | return ret; |
| 2485 | } |
| 2486 | |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2487 | /* If fake_store(lba,num) compares equal to arr(num), then copy top half of |
| 2488 | * arr into fake_store(lba,num) and return true. If comparison fails then |
| 2489 | * return false. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2490 | static bool comp_write_worker(u64 lba, u32 num, const u8 *arr) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2491 | { |
| 2492 | bool res; |
| 2493 | u64 block, rest = 0; |
| 2494 | u32 store_blks = sdebug_store_sectors; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2495 | u32 lb_size = sdebug_sector_size; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2496 | |
| 2497 | block = do_div(lba, store_blks); |
| 2498 | if (block + num > store_blks) |
| 2499 | rest = block + num - store_blks; |
| 2500 | |
| 2501 | res = !memcmp(fake_storep + (block * lb_size), arr, |
| 2502 | (num - rest) * lb_size); |
| 2503 | if (!res) |
| 2504 | return res; |
| 2505 | if (rest) |
| 2506 | res = memcmp(fake_storep, arr + ((num - rest) * lb_size), |
| 2507 | rest * lb_size); |
| 2508 | if (!res) |
| 2509 | return res; |
| 2510 | arr += num * lb_size; |
| 2511 | memcpy(fake_storep + (block * lb_size), arr, (num - rest) * lb_size); |
| 2512 | if (rest) |
| 2513 | memcpy(fake_storep, arr + ((num - rest) * lb_size), |
| 2514 | rest * lb_size); |
| 2515 | return res; |
| 2516 | } |
| 2517 | |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2518 | static __be16 dif_compute_csum(const void *buf, int len) |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2519 | { |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2520 | __be16 csum; |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2521 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2522 | if (sdebug_guard) |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2523 | csum = (__force __be16)ip_compute_csum(buf, len); |
| 2524 | else |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2525 | csum = cpu_to_be16(crc_t10dif(buf, len)); |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2526 | |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2527 | return csum; |
| 2528 | } |
| 2529 | |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 2530 | static int dif_verify(struct t10_pi_tuple *sdt, const void *data, |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2531 | sector_t sector, u32 ei_lba) |
| 2532 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2533 | __be16 csum = dif_compute_csum(data, sdebug_sector_size); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2534 | |
| 2535 | if (sdt->guard_tag != csum) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2536 | 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] | 2537 | (unsigned long)sector, |
| 2538 | be16_to_cpu(sdt->guard_tag), |
| 2539 | be16_to_cpu(csum)); |
| 2540 | return 0x01; |
| 2541 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2542 | if (sdebug_dif == T10_PI_TYPE1_PROTECTION && |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2543 | be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2544 | pr_err("REF check failed on sector %lu\n", |
| 2545 | (unsigned long)sector); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2546 | return 0x03; |
| 2547 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2548 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2549 | be32_to_cpu(sdt->ref_tag) != ei_lba) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2550 | pr_err("REF check failed on sector %lu\n", |
| 2551 | (unsigned long)sector); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2552 | return 0x03; |
| 2553 | } |
| 2554 | return 0; |
| 2555 | } |
| 2556 | |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2557 | static void dif_copy_prot(struct scsi_cmnd *SCpnt, sector_t sector, |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2558 | unsigned int sectors, bool read) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2559 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2560 | size_t resid; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2561 | void *paddr; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2562 | const void *dif_store_end = dif_storep + sdebug_store_sectors; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2563 | struct sg_mapping_iter miter; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2564 | |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 2565 | /* Bytes of protection data to copy into sgl */ |
| 2566 | resid = sectors * sizeof(*dif_storep); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2567 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2568 | sg_miter_start(&miter, scsi_prot_sglist(SCpnt), |
| 2569 | scsi_prot_sg_count(SCpnt), SG_MITER_ATOMIC | |
| 2570 | (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG)); |
| 2571 | |
| 2572 | while (sg_miter_next(&miter) && resid > 0) { |
| 2573 | size_t len = min(miter.length, resid); |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2574 | void *start = dif_store(sector); |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2575 | size_t rest = 0; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2576 | |
| 2577 | if (dif_store_end < start + len) |
| 2578 | rest = start + len - dif_store_end; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2579 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2580 | paddr = miter.addr; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2581 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2582 | if (read) |
| 2583 | memcpy(paddr, start, len - rest); |
| 2584 | else |
| 2585 | memcpy(start, paddr, len - rest); |
| 2586 | |
| 2587 | if (rest) { |
| 2588 | if (read) |
| 2589 | memcpy(paddr + len - rest, dif_storep, rest); |
| 2590 | else |
| 2591 | memcpy(dif_storep, paddr + len - rest, rest); |
| 2592 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2593 | |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 2594 | sector += len / sizeof(*dif_storep); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2595 | resid -= len; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2596 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2597 | sg_miter_stop(&miter); |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2598 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2599 | |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2600 | static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec, |
| 2601 | unsigned int sectors, u32 ei_lba) |
| 2602 | { |
| 2603 | unsigned int i; |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 2604 | struct t10_pi_tuple *sdt; |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2605 | sector_t sector; |
| 2606 | |
Akinobu Mita | c45eabec | 2014-02-26 22:56:58 +0900 | [diff] [blame] | 2607 | for (i = 0; i < sectors; i++, ei_lba++) { |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2608 | int ret; |
| 2609 | |
| 2610 | sector = start_sec + i; |
| 2611 | sdt = dif_store(sector); |
| 2612 | |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2613 | if (sdt->app_tag == cpu_to_be16(0xffff)) |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2614 | continue; |
| 2615 | |
| 2616 | ret = dif_verify(sdt, fake_store(sector), sector, ei_lba); |
| 2617 | if (ret) { |
| 2618 | dif_errors++; |
| 2619 | return ret; |
| 2620 | } |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2621 | } |
| 2622 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2623 | dif_copy_prot(SCpnt, start_sec, sectors, true); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2624 | dix_reads++; |
| 2625 | |
| 2626 | return 0; |
| 2627 | } |
| 2628 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2629 | 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] | 2630 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2631 | u8 *cmd = scp->cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2632 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2633 | u64 lba; |
| 2634 | u32 num; |
| 2635 | u32 ei_lba; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2636 | unsigned long iflags; |
| 2637 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2638 | bool check_prot; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2639 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2640 | switch (cmd[0]) { |
| 2641 | case READ_16: |
| 2642 | ei_lba = 0; |
| 2643 | lba = get_unaligned_be64(cmd + 2); |
| 2644 | num = get_unaligned_be32(cmd + 10); |
| 2645 | check_prot = true; |
| 2646 | break; |
| 2647 | case READ_10: |
| 2648 | ei_lba = 0; |
| 2649 | lba = get_unaligned_be32(cmd + 2); |
| 2650 | num = get_unaligned_be16(cmd + 7); |
| 2651 | check_prot = true; |
| 2652 | break; |
| 2653 | case READ_6: |
| 2654 | ei_lba = 0; |
| 2655 | lba = (u32)cmd[3] | (u32)cmd[2] << 8 | |
| 2656 | (u32)(cmd[1] & 0x1f) << 16; |
| 2657 | num = (0 == cmd[4]) ? 256 : cmd[4]; |
| 2658 | check_prot = true; |
| 2659 | break; |
| 2660 | case READ_12: |
| 2661 | ei_lba = 0; |
| 2662 | lba = get_unaligned_be32(cmd + 2); |
| 2663 | num = get_unaligned_be32(cmd + 6); |
| 2664 | check_prot = true; |
| 2665 | break; |
| 2666 | case XDWRITEREAD_10: |
| 2667 | ei_lba = 0; |
| 2668 | lba = get_unaligned_be32(cmd + 2); |
| 2669 | num = get_unaligned_be16(cmd + 7); |
| 2670 | check_prot = false; |
| 2671 | break; |
| 2672 | default: /* assume READ(32) */ |
| 2673 | lba = get_unaligned_be64(cmd + 12); |
| 2674 | ei_lba = get_unaligned_be32(cmd + 20); |
| 2675 | num = get_unaligned_be32(cmd + 28); |
| 2676 | check_prot = false; |
| 2677 | break; |
| 2678 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2679 | if (unlikely(have_dif_prot && check_prot)) { |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2680 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2681 | (cmd[1] & 0xe0)) { |
| 2682 | mk_sense_invalid_opcode(scp); |
| 2683 | return check_condition_result; |
| 2684 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2685 | if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || |
| 2686 | sdebug_dif == T10_PI_TYPE3_PROTECTION) && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2687 | (cmd[1] & 0xe0) == 0) |
| 2688 | sdev_printk(KERN_ERR, scp->device, "Unprotected RD " |
| 2689 | "to DIF device\n"); |
| 2690 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2691 | if (unlikely(sdebug_any_injecting_opt)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2692 | sqcp = (struct sdebug_queued_cmd *)scp->host_scribble; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2693 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2694 | if (sqcp) { |
| 2695 | if (sqcp->inj_short) |
| 2696 | num /= 2; |
| 2697 | } |
| 2698 | } else |
| 2699 | sqcp = NULL; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2700 | |
| 2701 | /* inline check_device_access_params() */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2702 | if (unlikely(lba + num > sdebug_capacity)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2703 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 2704 | return check_condition_result; |
| 2705 | } |
| 2706 | /* transfer length excessive (tie in to block limits VPD page) */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2707 | if (unlikely(num > sdebug_store_sectors)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2708 | /* needs work to find which cdb byte 'num' comes from */ |
| 2709 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 2710 | return check_condition_result; |
| 2711 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2712 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2713 | if (unlikely((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) && |
| 2714 | (lba <= (OPT_MEDIUM_ERR_ADDR + OPT_MEDIUM_ERR_NUM - 1)) && |
| 2715 | ((lba + num) > OPT_MEDIUM_ERR_ADDR))) { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2716 | /* claim unrecoverable read error */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2717 | mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2718 | /* set info field and valid bit for fixed descriptor */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2719 | if (0x70 == (scp->sense_buffer[0] & 0x7f)) { |
| 2720 | scp->sense_buffer[0] |= 0x80; /* Valid bit */ |
Douglas Gilbert | 32f7ef7 | 2011-03-11 10:43:35 -0500 | [diff] [blame] | 2721 | ret = (lba < OPT_MEDIUM_ERR_ADDR) |
| 2722 | ? OPT_MEDIUM_ERR_ADDR : (int)lba; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2723 | put_unaligned_be32(ret, scp->sense_buffer + 3); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2724 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2725 | scsi_set_resid(scp, scsi_bufflen(scp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | return check_condition_result; |
| 2727 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2728 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2729 | read_lock_irqsave(&atomic_rw, iflags); |
| 2730 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2731 | /* DIX + T10 DIF */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2732 | if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2733 | int prot_ret = prot_verify_read(scp, lba, num, ei_lba); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2734 | |
| 2735 | if (prot_ret) { |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2736 | read_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2737 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2738 | return illegal_condition_result; |
| 2739 | } |
| 2740 | } |
| 2741 | |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 2742 | ret = do_device_access(scp, 0, lba, num, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | read_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2744 | if (unlikely(ret == -1)) |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2745 | return DID_ERROR << 16; |
| 2746 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2747 | scsi_in(scp)->resid = scsi_bufflen(scp) - ret; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2748 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2749 | if (unlikely(sqcp)) { |
| 2750 | if (sqcp->inj_recovered) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2751 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 2752 | THRESHOLD_EXCEEDED, 0); |
| 2753 | return check_condition_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2754 | } else if (sqcp->inj_transport) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2755 | mk_sense_buffer(scp, ABORTED_COMMAND, |
| 2756 | TRANSPORT_PROBLEM, ACK_NAK_TO); |
| 2757 | return check_condition_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2758 | } else if (sqcp->inj_dif) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2759 | /* Logical block guard check failed */ |
| 2760 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); |
| 2761 | return illegal_condition_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2762 | } else if (sqcp->inj_dix) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2763 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); |
| 2764 | return illegal_condition_result; |
| 2765 | } |
| 2766 | } |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2767 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | } |
| 2769 | |
Tomas Winkler | 58a8635 | 2015-07-28 16:54:23 +0300 | [diff] [blame] | 2770 | static void dump_sector(unsigned char *buf, int len) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2771 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2772 | int i, j, n; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2773 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2774 | pr_err(">>> Sector Dump <<<\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2775 | for (i = 0 ; i < len ; i += 16) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2776 | char b[128]; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2777 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2778 | for (j = 0, n = 0; j < 16; j++) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2779 | unsigned char c = buf[i+j]; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2780 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2781 | if (c >= 0x20 && c < 0x7e) |
| 2782 | n += scnprintf(b + n, sizeof(b) - n, |
| 2783 | " %c ", buf[i+j]); |
| 2784 | else |
| 2785 | n += scnprintf(b + n, sizeof(b) - n, |
| 2786 | "%02x ", buf[i+j]); |
| 2787 | } |
| 2788 | pr_err("%04d: %s\n", i, b); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2789 | } |
| 2790 | } |
| 2791 | |
| 2792 | 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] | 2793 | unsigned int sectors, u32 ei_lba) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2794 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2795 | int ret; |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 2796 | struct t10_pi_tuple *sdt; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2797 | void *daddr; |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2798 | sector_t sector = start_sec; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2799 | int ppage_offset; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2800 | int dpage_offset; |
| 2801 | struct sg_mapping_iter diter; |
| 2802 | struct sg_mapping_iter piter; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2803 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2804 | BUG_ON(scsi_sg_count(SCpnt) == 0); |
| 2805 | BUG_ON(scsi_prot_sg_count(SCpnt) == 0); |
| 2806 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2807 | sg_miter_start(&piter, scsi_prot_sglist(SCpnt), |
| 2808 | scsi_prot_sg_count(SCpnt), |
| 2809 | SG_MITER_ATOMIC | SG_MITER_FROM_SG); |
| 2810 | sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt), |
| 2811 | SG_MITER_ATOMIC | SG_MITER_FROM_SG); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2812 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2813 | /* For each protection page */ |
| 2814 | while (sg_miter_next(&piter)) { |
| 2815 | dpage_offset = 0; |
| 2816 | if (WARN_ON(!sg_miter_next(&diter))) { |
| 2817 | ret = 0x01; |
| 2818 | goto out; |
| 2819 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2820 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2821 | for (ppage_offset = 0; ppage_offset < piter.length; |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 2822 | ppage_offset += sizeof(struct t10_pi_tuple)) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2823 | /* If we're at the end of the current |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2824 | * data page advance to the next one |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2825 | */ |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2826 | if (dpage_offset >= diter.length) { |
| 2827 | if (WARN_ON(!sg_miter_next(&diter))) { |
| 2828 | ret = 0x01; |
| 2829 | goto out; |
| 2830 | } |
| 2831 | dpage_offset = 0; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2832 | } |
| 2833 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2834 | sdt = piter.addr + ppage_offset; |
| 2835 | daddr = diter.addr + dpage_offset; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2836 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2837 | ret = dif_verify(sdt, daddr, sector, ei_lba); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2838 | if (ret) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2839 | dump_sector(daddr, sdebug_sector_size); |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 2840 | goto out; |
| 2841 | } |
| 2842 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2843 | sector++; |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 2844 | ei_lba++; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2845 | dpage_offset += sdebug_sector_size; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2846 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2847 | diter.consumed = dpage_offset; |
| 2848 | sg_miter_stop(&diter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2849 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2850 | sg_miter_stop(&piter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2851 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2852 | dif_copy_prot(SCpnt, start_sec, sectors, false); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2853 | dix_writes++; |
| 2854 | |
| 2855 | return 0; |
| 2856 | |
| 2857 | out: |
| 2858 | dif_errors++; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2859 | sg_miter_stop(&diter); |
| 2860 | sg_miter_stop(&piter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2861 | return ret; |
| 2862 | } |
| 2863 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2864 | static unsigned long lba_to_map_index(sector_t lba) |
| 2865 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2866 | if (sdebug_unmap_alignment) |
| 2867 | lba += sdebug_unmap_granularity - sdebug_unmap_alignment; |
| 2868 | sector_div(lba, sdebug_unmap_granularity); |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2869 | return lba; |
| 2870 | } |
| 2871 | |
| 2872 | static sector_t map_index_to_lba(unsigned long index) |
| 2873 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2874 | sector_t lba = index * sdebug_unmap_granularity; |
Akinobu Mita | a027b5b | 2013-08-26 22:08:41 +0900 | [diff] [blame] | 2875 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2876 | if (sdebug_unmap_alignment) |
| 2877 | lba -= sdebug_unmap_granularity - sdebug_unmap_alignment; |
Akinobu Mita | a027b5b | 2013-08-26 22:08:41 +0900 | [diff] [blame] | 2878 | return lba; |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2879 | } |
| 2880 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2881 | static unsigned int map_state(sector_t lba, unsigned int *num) |
| 2882 | { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2883 | sector_t end; |
| 2884 | unsigned int mapped; |
| 2885 | unsigned long index; |
| 2886 | unsigned long next; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2887 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2888 | index = lba_to_map_index(lba); |
| 2889 | mapped = test_bit(index, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2890 | |
| 2891 | if (mapped) |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2892 | next = find_next_zero_bit(map_storep, map_size, index); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2893 | else |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2894 | next = find_next_bit(map_storep, map_size, index); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2895 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2896 | 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] | 2897 | *num = end - lba; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2898 | return mapped; |
| 2899 | } |
| 2900 | |
| 2901 | static void map_region(sector_t lba, unsigned int len) |
| 2902 | { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2903 | sector_t end = lba + len; |
| 2904 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2905 | while (lba < end) { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2906 | unsigned long index = lba_to_map_index(lba); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2907 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2908 | if (index < map_size) |
| 2909 | set_bit(index, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2910 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2911 | lba = map_index_to_lba(index + 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | static void unmap_region(sector_t lba, unsigned int len) |
| 2916 | { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2917 | sector_t end = lba + len; |
| 2918 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2919 | while (lba < end) { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2920 | unsigned long index = lba_to_map_index(lba); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2921 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2922 | if (lba == map_index_to_lba(index) && |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2923 | lba + sdebug_unmap_granularity <= end && |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2924 | index < map_size) { |
| 2925 | clear_bit(index, map_storep); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2926 | if (sdebug_lbprz) { /* for LBPRZ=2 return 0xff_s */ |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 2927 | memset(fake_storep + |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2928 | lba * sdebug_sector_size, |
| 2929 | (sdebug_lbprz & 1) ? 0 : 0xff, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2930 | sdebug_sector_size * |
| 2931 | sdebug_unmap_granularity); |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2932 | } |
Akinobu Mita | e9926b4 | 2013-06-29 17:59:17 +0900 | [diff] [blame] | 2933 | if (dif_storep) { |
| 2934 | memset(dif_storep + lba, 0xff, |
| 2935 | sizeof(*dif_storep) * |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2936 | sdebug_unmap_granularity); |
Akinobu Mita | e9926b4 | 2013-06-29 17:59:17 +0900 | [diff] [blame] | 2937 | } |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 2938 | } |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2939 | lba = map_index_to_lba(index + 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2940 | } |
| 2941 | } |
| 2942 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2943 | 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] | 2944 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2945 | u8 *cmd = scp->cmnd; |
| 2946 | u64 lba; |
| 2947 | u32 num; |
| 2948 | u32 ei_lba; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | unsigned long iflags; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2950 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2951 | bool check_prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2953 | switch (cmd[0]) { |
| 2954 | case WRITE_16: |
| 2955 | ei_lba = 0; |
| 2956 | lba = get_unaligned_be64(cmd + 2); |
| 2957 | num = get_unaligned_be32(cmd + 10); |
| 2958 | check_prot = true; |
| 2959 | break; |
| 2960 | case WRITE_10: |
| 2961 | ei_lba = 0; |
| 2962 | lba = get_unaligned_be32(cmd + 2); |
| 2963 | num = get_unaligned_be16(cmd + 7); |
| 2964 | check_prot = true; |
| 2965 | break; |
| 2966 | case WRITE_6: |
| 2967 | ei_lba = 0; |
| 2968 | lba = (u32)cmd[3] | (u32)cmd[2] << 8 | |
| 2969 | (u32)(cmd[1] & 0x1f) << 16; |
| 2970 | num = (0 == cmd[4]) ? 256 : cmd[4]; |
| 2971 | check_prot = true; |
| 2972 | break; |
| 2973 | case WRITE_12: |
| 2974 | ei_lba = 0; |
| 2975 | lba = get_unaligned_be32(cmd + 2); |
| 2976 | num = get_unaligned_be32(cmd + 6); |
| 2977 | check_prot = true; |
| 2978 | break; |
| 2979 | case 0x53: /* XDWRITEREAD(10) */ |
| 2980 | ei_lba = 0; |
| 2981 | lba = get_unaligned_be32(cmd + 2); |
| 2982 | num = get_unaligned_be16(cmd + 7); |
| 2983 | check_prot = false; |
| 2984 | break; |
| 2985 | default: /* assume WRITE(32) */ |
| 2986 | lba = get_unaligned_be64(cmd + 12); |
| 2987 | ei_lba = get_unaligned_be32(cmd + 20); |
| 2988 | num = get_unaligned_be32(cmd + 28); |
| 2989 | check_prot = false; |
| 2990 | break; |
| 2991 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2992 | if (unlikely(have_dif_prot && check_prot)) { |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2993 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2994 | (cmd[1] & 0xe0)) { |
| 2995 | mk_sense_invalid_opcode(scp); |
| 2996 | return check_condition_result; |
| 2997 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2998 | if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || |
| 2999 | sdebug_dif == T10_PI_TYPE3_PROTECTION) && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3000 | (cmd[1] & 0xe0) == 0) |
| 3001 | sdev_printk(KERN_ERR, scp->device, "Unprotected WR " |
| 3002 | "to DIF device\n"); |
| 3003 | } |
| 3004 | |
| 3005 | /* inline check_device_access_params() */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3006 | if (unlikely(lba + num > sdebug_capacity)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3007 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 3008 | return check_condition_result; |
| 3009 | } |
| 3010 | /* transfer length excessive (tie in to block limits VPD page) */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3011 | if (unlikely(num > sdebug_store_sectors)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3012 | /* needs work to find which cdb byte 'num' comes from */ |
| 3013 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 3014 | return check_condition_result; |
| 3015 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3017 | write_lock_irqsave(&atomic_rw, iflags); |
| 3018 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 3019 | /* DIX + T10 DIF */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3020 | if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3021 | int prot_ret = prot_verify_write(scp, lba, num, ei_lba); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 3022 | |
| 3023 | if (prot_ret) { |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3024 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3025 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 3026 | return illegal_condition_result; |
| 3027 | } |
| 3028 | } |
| 3029 | |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 3030 | ret = do_device_access(scp, 0, lba, num, true); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3031 | if (unlikely(scsi_debug_lbp())) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3032 | map_region(lba, num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3034 | if (unlikely(-1 == ret)) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3035 | return DID_ERROR << 16; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3036 | else if (unlikely(sdebug_verbose && |
| 3037 | (ret < (num * sdebug_sector_size)))) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3038 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3039 | "%s: write: cdb indicated=%u, IO sent=%d bytes\n", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3040 | my_name, num * sdebug_sector_size, ret); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3041 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3042 | if (unlikely(sdebug_any_injecting_opt)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3043 | struct sdebug_queued_cmd *sqcp = |
| 3044 | (struct sdebug_queued_cmd *)scp->host_scribble; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3045 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3046 | if (sqcp) { |
| 3047 | if (sqcp->inj_recovered) { |
| 3048 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 3049 | THRESHOLD_EXCEEDED, 0); |
| 3050 | return check_condition_result; |
| 3051 | } else if (sqcp->inj_dif) { |
| 3052 | /* Logical block guard check failed */ |
| 3053 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); |
| 3054 | return illegal_condition_result; |
| 3055 | } else if (sqcp->inj_dix) { |
| 3056 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); |
| 3057 | return illegal_condition_result; |
| 3058 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3059 | } |
| 3060 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | return 0; |
| 3062 | } |
| 3063 | |
Douglas Gilbert | 481b5e5 | 2017-12-23 12:48:14 -0500 | [diff] [blame] | 3064 | /* |
| 3065 | * T10 has only specified WRITE SCATTERED(16) and WRITE SCATTERED(32). |
| 3066 | * No READ GATHERED yet (requires bidi or long cdb holding gather list). |
| 3067 | */ |
| 3068 | static int resp_write_scat(struct scsi_cmnd *scp, |
| 3069 | struct sdebug_dev_info *devip) |
| 3070 | { |
| 3071 | u8 *cmd = scp->cmnd; |
| 3072 | u8 *lrdp = NULL; |
| 3073 | u8 *up; |
| 3074 | u8 wrprotect; |
| 3075 | u16 lbdof, num_lrd, k; |
| 3076 | u32 num, num_by, bt_len, lbdof_blen, sg_off, cum_lb; |
| 3077 | u32 lb_size = sdebug_sector_size; |
| 3078 | u32 ei_lba; |
| 3079 | u64 lba; |
| 3080 | unsigned long iflags; |
| 3081 | int ret, res; |
| 3082 | bool is_16; |
| 3083 | static const u32 lrd_size = 32; /* + parameter list header size */ |
| 3084 | |
| 3085 | if (cmd[0] == VARIABLE_LENGTH_CMD) { |
| 3086 | is_16 = false; |
| 3087 | wrprotect = (cmd[10] >> 5) & 0x7; |
| 3088 | lbdof = get_unaligned_be16(cmd + 12); |
| 3089 | num_lrd = get_unaligned_be16(cmd + 16); |
| 3090 | bt_len = get_unaligned_be32(cmd + 28); |
| 3091 | } else { /* that leaves WRITE SCATTERED(16) */ |
| 3092 | is_16 = true; |
| 3093 | wrprotect = (cmd[2] >> 5) & 0x7; |
| 3094 | lbdof = get_unaligned_be16(cmd + 4); |
| 3095 | num_lrd = get_unaligned_be16(cmd + 8); |
| 3096 | bt_len = get_unaligned_be32(cmd + 10); |
| 3097 | if (unlikely(have_dif_prot)) { |
| 3098 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
| 3099 | wrprotect) { |
| 3100 | mk_sense_invalid_opcode(scp); |
| 3101 | return illegal_condition_result; |
| 3102 | } |
| 3103 | if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || |
| 3104 | sdebug_dif == T10_PI_TYPE3_PROTECTION) && |
| 3105 | wrprotect == 0) |
| 3106 | sdev_printk(KERN_ERR, scp->device, |
| 3107 | "Unprotected WR to DIF device\n"); |
| 3108 | } |
| 3109 | } |
| 3110 | if ((num_lrd == 0) || (bt_len == 0)) |
| 3111 | return 0; /* T10 says these do-nothings are not errors */ |
| 3112 | if (lbdof == 0) { |
| 3113 | if (sdebug_verbose) |
| 3114 | sdev_printk(KERN_INFO, scp->device, |
| 3115 | "%s: %s: LB Data Offset field bad\n", |
| 3116 | my_name, __func__); |
| 3117 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 3118 | return illegal_condition_result; |
| 3119 | } |
| 3120 | lbdof_blen = lbdof * lb_size; |
| 3121 | if ((lrd_size + (num_lrd * lrd_size)) > lbdof_blen) { |
| 3122 | if (sdebug_verbose) |
| 3123 | sdev_printk(KERN_INFO, scp->device, |
| 3124 | "%s: %s: LBA range descriptors don't fit\n", |
| 3125 | my_name, __func__); |
| 3126 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 3127 | return illegal_condition_result; |
| 3128 | } |
| 3129 | lrdp = kzalloc(lbdof_blen, GFP_ATOMIC); |
| 3130 | if (lrdp == NULL) |
| 3131 | return SCSI_MLQUEUE_HOST_BUSY; |
| 3132 | if (sdebug_verbose) |
| 3133 | sdev_printk(KERN_INFO, scp->device, |
| 3134 | "%s: %s: Fetch header+scatter_list, lbdof_blen=%u\n", |
| 3135 | my_name, __func__, lbdof_blen); |
| 3136 | res = fetch_to_dev_buffer(scp, lrdp, lbdof_blen); |
| 3137 | if (res == -1) { |
| 3138 | ret = DID_ERROR << 16; |
| 3139 | goto err_out; |
| 3140 | } |
| 3141 | |
| 3142 | write_lock_irqsave(&atomic_rw, iflags); |
| 3143 | sg_off = lbdof_blen; |
| 3144 | /* Spec says Buffer xfer Length field in number of LBs in dout */ |
| 3145 | cum_lb = 0; |
| 3146 | for (k = 0, up = lrdp + lrd_size; k < num_lrd; ++k, up += lrd_size) { |
| 3147 | lba = get_unaligned_be64(up + 0); |
| 3148 | num = get_unaligned_be32(up + 8); |
| 3149 | if (sdebug_verbose) |
| 3150 | sdev_printk(KERN_INFO, scp->device, |
| 3151 | "%s: %s: k=%d LBA=0x%llx num=%u sg_off=%u\n", |
| 3152 | my_name, __func__, k, lba, num, sg_off); |
| 3153 | if (num == 0) |
| 3154 | continue; |
| 3155 | ret = check_device_access_params(scp, lba, num); |
| 3156 | if (ret) |
| 3157 | goto err_out_unlock; |
| 3158 | num_by = num * lb_size; |
| 3159 | ei_lba = is_16 ? 0 : get_unaligned_be32(up + 12); |
| 3160 | |
| 3161 | if ((cum_lb + num) > bt_len) { |
| 3162 | if (sdebug_verbose) |
| 3163 | sdev_printk(KERN_INFO, scp->device, |
| 3164 | "%s: %s: sum of blocks > data provided\n", |
| 3165 | my_name, __func__); |
| 3166 | mk_sense_buffer(scp, ILLEGAL_REQUEST, WRITE_ERROR_ASC, |
| 3167 | 0); |
| 3168 | ret = illegal_condition_result; |
| 3169 | goto err_out_unlock; |
| 3170 | } |
| 3171 | |
| 3172 | /* DIX + T10 DIF */ |
| 3173 | if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { |
| 3174 | int prot_ret = prot_verify_write(scp, lba, num, |
| 3175 | ei_lba); |
| 3176 | |
| 3177 | if (prot_ret) { |
| 3178 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, |
| 3179 | prot_ret); |
| 3180 | ret = illegal_condition_result; |
| 3181 | goto err_out_unlock; |
| 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | ret = do_device_access(scp, sg_off, lba, num, true); |
| 3186 | if (unlikely(scsi_debug_lbp())) |
| 3187 | map_region(lba, num); |
| 3188 | if (unlikely(-1 == ret)) { |
| 3189 | ret = DID_ERROR << 16; |
| 3190 | goto err_out_unlock; |
| 3191 | } else if (unlikely(sdebug_verbose && (ret < num_by))) |
| 3192 | sdev_printk(KERN_INFO, scp->device, |
| 3193 | "%s: write: cdb indicated=%u, IO sent=%d bytes\n", |
| 3194 | my_name, num_by, ret); |
| 3195 | |
| 3196 | if (unlikely(sdebug_any_injecting_opt)) { |
| 3197 | struct sdebug_queued_cmd *sqcp = |
| 3198 | (struct sdebug_queued_cmd *)scp->host_scribble; |
| 3199 | |
| 3200 | if (sqcp) { |
| 3201 | if (sqcp->inj_recovered) { |
| 3202 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 3203 | THRESHOLD_EXCEEDED, 0); |
| 3204 | ret = illegal_condition_result; |
| 3205 | goto err_out_unlock; |
| 3206 | } else if (sqcp->inj_dif) { |
| 3207 | /* Logical block guard check failed */ |
| 3208 | mk_sense_buffer(scp, ABORTED_COMMAND, |
| 3209 | 0x10, 1); |
| 3210 | ret = illegal_condition_result; |
| 3211 | goto err_out_unlock; |
| 3212 | } else if (sqcp->inj_dix) { |
| 3213 | mk_sense_buffer(scp, ILLEGAL_REQUEST, |
| 3214 | 0x10, 1); |
| 3215 | ret = illegal_condition_result; |
| 3216 | goto err_out_unlock; |
| 3217 | } |
| 3218 | } |
| 3219 | } |
| 3220 | sg_off += num_by; |
| 3221 | cum_lb += num; |
| 3222 | } |
| 3223 | ret = 0; |
| 3224 | err_out_unlock: |
| 3225 | write_unlock_irqrestore(&atomic_rw, iflags); |
| 3226 | err_out: |
| 3227 | kfree(lrdp); |
| 3228 | return ret; |
| 3229 | } |
| 3230 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3231 | static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num, |
| 3232 | u32 ei_lba, bool unmap, bool ndob) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3233 | { |
| 3234 | unsigned long iflags; |
| 3235 | unsigned long long i; |
| 3236 | int ret; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3237 | u64 lba_off; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3238 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3239 | ret = check_device_access_params(scp, lba, num); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3240 | if (ret) |
| 3241 | return ret; |
| 3242 | |
| 3243 | write_lock_irqsave(&atomic_rw, iflags); |
| 3244 | |
Akinobu Mita | 9ed8d3d | 2013-04-16 22:11:55 +0900 | [diff] [blame] | 3245 | if (unmap && scsi_debug_lbp()) { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3246 | unmap_region(lba, num); |
| 3247 | goto out; |
| 3248 | } |
| 3249 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3250 | lba_off = lba * sdebug_sector_size; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3251 | /* if ndob then zero 1 logical block, else fetch 1 logical block */ |
| 3252 | if (ndob) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3253 | memset(fake_storep + lba_off, 0, sdebug_sector_size); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3254 | ret = 0; |
| 3255 | } else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3256 | ret = fetch_to_dev_buffer(scp, fake_storep + lba_off, |
| 3257 | sdebug_sector_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3258 | |
| 3259 | if (-1 == ret) { |
| 3260 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3261 | return DID_ERROR << 16; |
Douglas Gilbert | e33d7c5 | 2017-10-29 10:47:19 -0400 | [diff] [blame] | 3262 | } else if (sdebug_verbose && !ndob && (ret < sdebug_sector_size)) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3263 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | e33d7c5 | 2017-10-29 10:47:19 -0400 | [diff] [blame] | 3264 | "%s: %s: lb size=%u, IO sent=%d bytes\n", |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3265 | my_name, "write same", |
Douglas Gilbert | e33d7c5 | 2017-10-29 10:47:19 -0400 | [diff] [blame] | 3266 | sdebug_sector_size, ret); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3267 | |
| 3268 | /* Copy first sector to remaining blocks */ |
| 3269 | for (i = 1 ; i < num ; i++) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3270 | memcpy(fake_storep + ((lba + i) * sdebug_sector_size), |
| 3271 | fake_storep + lba_off, |
| 3272 | sdebug_sector_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3273 | |
Akinobu Mita | 9ed8d3d | 2013-04-16 22:11:55 +0900 | [diff] [blame] | 3274 | if (scsi_debug_lbp()) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3275 | map_region(lba, num); |
| 3276 | out: |
| 3277 | write_unlock_irqrestore(&atomic_rw, iflags); |
| 3278 | |
| 3279 | return 0; |
| 3280 | } |
| 3281 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3282 | static int resp_write_same_10(struct scsi_cmnd *scp, |
| 3283 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3284 | { |
| 3285 | u8 *cmd = scp->cmnd; |
| 3286 | u32 lba; |
| 3287 | u16 num; |
| 3288 | u32 ei_lba = 0; |
| 3289 | bool unmap = false; |
| 3290 | |
| 3291 | if (cmd[1] & 0x8) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3292 | if (sdebug_lbpws10 == 0) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3293 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); |
| 3294 | return check_condition_result; |
| 3295 | } else |
| 3296 | unmap = true; |
| 3297 | } |
| 3298 | lba = get_unaligned_be32(cmd + 2); |
| 3299 | num = get_unaligned_be16(cmd + 7); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3300 | if (num > sdebug_write_same_length) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3301 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); |
| 3302 | return check_condition_result; |
| 3303 | } |
| 3304 | return resp_write_same(scp, lba, num, ei_lba, unmap, false); |
| 3305 | } |
| 3306 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3307 | static int resp_write_same_16(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 | u64 lba; |
| 3312 | u32 num; |
| 3313 | u32 ei_lba = 0; |
| 3314 | bool unmap = false; |
| 3315 | bool ndob = false; |
| 3316 | |
| 3317 | if (cmd[1] & 0x8) { /* UNMAP */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3318 | if (sdebug_lbpws == 0) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3319 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); |
| 3320 | return check_condition_result; |
| 3321 | } else |
| 3322 | unmap = true; |
| 3323 | } |
| 3324 | if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */ |
| 3325 | ndob = true; |
| 3326 | lba = get_unaligned_be64(cmd + 2); |
| 3327 | num = get_unaligned_be32(cmd + 10); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3328 | if (num > sdebug_write_same_length) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3329 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1); |
| 3330 | return check_condition_result; |
| 3331 | } |
| 3332 | return resp_write_same(scp, lba, num, ei_lba, unmap, ndob); |
| 3333 | } |
| 3334 | |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 3335 | /* Note the mode field is in the same position as the (lower) service action |
| 3336 | * field. For the Report supported operation codes command, SPC-4 suggests |
| 3337 | * each mode of this command should be reported separately; for future. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3338 | static int resp_write_buffer(struct scsi_cmnd *scp, |
| 3339 | struct sdebug_dev_info *devip) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 3340 | { |
| 3341 | u8 *cmd = scp->cmnd; |
| 3342 | struct scsi_device *sdp = scp->device; |
| 3343 | struct sdebug_dev_info *dp; |
| 3344 | u8 mode; |
| 3345 | |
| 3346 | mode = cmd[1] & 0x1f; |
| 3347 | switch (mode) { |
| 3348 | case 0x4: /* download microcode (MC) and activate (ACT) */ |
| 3349 | /* set UAs on this device only */ |
| 3350 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3351 | set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm); |
| 3352 | break; |
| 3353 | case 0x5: /* download MC, save and ACT */ |
| 3354 | set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm); |
| 3355 | break; |
| 3356 | case 0x6: /* download MC with offsets and ACT */ |
| 3357 | /* set UAs on most devices (LUs) in this target */ |
| 3358 | list_for_each_entry(dp, |
| 3359 | &devip->sdbg_host->dev_info_list, |
| 3360 | dev_list) |
| 3361 | if (dp->target == sdp->id) { |
| 3362 | set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm); |
| 3363 | if (devip != dp) |
| 3364 | set_bit(SDEBUG_UA_MICROCODE_CHANGED, |
| 3365 | dp->uas_bm); |
| 3366 | } |
| 3367 | break; |
| 3368 | case 0x7: /* download MC with offsets, save, and ACT */ |
| 3369 | /* set UA on all devices (LUs) in this target */ |
| 3370 | list_for_each_entry(dp, |
| 3371 | &devip->sdbg_host->dev_info_list, |
| 3372 | dev_list) |
| 3373 | if (dp->target == sdp->id) |
| 3374 | set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, |
| 3375 | dp->uas_bm); |
| 3376 | break; |
| 3377 | default: |
| 3378 | /* do nothing for this command for other mode values */ |
| 3379 | break; |
| 3380 | } |
| 3381 | return 0; |
| 3382 | } |
| 3383 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3384 | static int resp_comp_write(struct scsi_cmnd *scp, |
| 3385 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3386 | { |
| 3387 | u8 *cmd = scp->cmnd; |
| 3388 | u8 *arr; |
| 3389 | u8 *fake_storep_hold; |
| 3390 | u64 lba; |
| 3391 | u32 dnum; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3392 | u32 lb_size = sdebug_sector_size; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3393 | u8 num; |
| 3394 | unsigned long iflags; |
| 3395 | int ret; |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3396 | int retval = 0; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3397 | |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3398 | lba = get_unaligned_be64(cmd + 2); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3399 | num = cmd[13]; /* 1 to a maximum of 255 logical blocks */ |
| 3400 | if (0 == num) |
| 3401 | return 0; /* degenerate case, not an error */ |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 3402 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3403 | (cmd[1] & 0xe0)) { |
| 3404 | mk_sense_invalid_opcode(scp); |
| 3405 | return check_condition_result; |
| 3406 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 3407 | if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || |
| 3408 | sdebug_dif == T10_PI_TYPE3_PROTECTION) && |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3409 | (cmd[1] & 0xe0) == 0) |
| 3410 | sdev_printk(KERN_ERR, scp->device, "Unprotected WR " |
| 3411 | "to DIF device\n"); |
| 3412 | |
| 3413 | /* inline check_device_access_params() */ |
| 3414 | if (lba + num > sdebug_capacity) { |
| 3415 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 3416 | return check_condition_result; |
| 3417 | } |
| 3418 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 3419 | if (num > sdebug_store_sectors) { |
| 3420 | /* needs work to find which cdb byte 'num' comes from */ |
| 3421 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 3422 | return check_condition_result; |
| 3423 | } |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3424 | dnum = 2 * num; |
| 3425 | arr = kzalloc(dnum * lb_size, GFP_ATOMIC); |
| 3426 | if (NULL == arr) { |
| 3427 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3428 | INSUFF_RES_ASCQ); |
| 3429 | return check_condition_result; |
| 3430 | } |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3431 | |
| 3432 | write_lock_irqsave(&atomic_rw, iflags); |
| 3433 | |
| 3434 | /* trick do_device_access() to fetch both compare and write buffers |
| 3435 | * from data-in into arr. Safe (atomic) since write_lock held. */ |
| 3436 | fake_storep_hold = fake_storep; |
| 3437 | fake_storep = arr; |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 3438 | ret = do_device_access(scp, 0, 0, dnum, true); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3439 | fake_storep = fake_storep_hold; |
| 3440 | if (ret == -1) { |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3441 | retval = DID_ERROR << 16; |
| 3442 | goto cleanup; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3443 | } else if (sdebug_verbose && (ret < (dnum * lb_size))) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3444 | sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb " |
| 3445 | "indicated=%u, IO sent=%d bytes\n", my_name, |
| 3446 | dnum * lb_size, ret); |
| 3447 | if (!comp_write_worker(lba, num, arr)) { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3448 | mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3449 | retval = check_condition_result; |
| 3450 | goto cleanup; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3451 | } |
| 3452 | if (scsi_debug_lbp()) |
| 3453 | map_region(lba, num); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3454 | cleanup: |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3455 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3456 | kfree(arr); |
| 3457 | return retval; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3458 | } |
| 3459 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3460 | struct unmap_block_desc { |
| 3461 | __be64 lba; |
| 3462 | __be32 blocks; |
| 3463 | __be32 __reserved; |
| 3464 | }; |
| 3465 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3466 | 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] | 3467 | { |
| 3468 | unsigned char *buf; |
| 3469 | struct unmap_block_desc *desc; |
| 3470 | unsigned int i, payload_len, descriptors; |
| 3471 | int ret; |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3472 | unsigned long iflags; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3473 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3474 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3475 | if (!scsi_debug_lbp()) |
| 3476 | return 0; /* fib and say its done */ |
| 3477 | payload_len = get_unaligned_be16(scp->cmnd + 7); |
| 3478 | BUG_ON(scsi_bufflen(scp) != payload_len); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3479 | |
| 3480 | descriptors = (payload_len - 8) / 16; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3481 | if (descriptors > sdebug_unmap_max_desc) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3482 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3483 | return check_condition_result; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3484 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3485 | |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 3486 | buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3487 | if (!buf) { |
| 3488 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3489 | INSUFF_RES_ASCQ); |
| 3490 | return check_condition_result; |
| 3491 | } |
| 3492 | |
| 3493 | scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp)); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3494 | |
| 3495 | BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2); |
| 3496 | BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16); |
| 3497 | |
| 3498 | desc = (void *)&buf[8]; |
| 3499 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3500 | write_lock_irqsave(&atomic_rw, iflags); |
| 3501 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3502 | for (i = 0 ; i < descriptors ; i++) { |
| 3503 | unsigned long long lba = get_unaligned_be64(&desc[i].lba); |
| 3504 | unsigned int num = get_unaligned_be32(&desc[i].blocks); |
| 3505 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3506 | ret = check_device_access_params(scp, lba, num); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3507 | if (ret) |
| 3508 | goto out; |
| 3509 | |
| 3510 | unmap_region(lba, num); |
| 3511 | } |
| 3512 | |
| 3513 | ret = 0; |
| 3514 | |
| 3515 | out: |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3516 | write_unlock_irqrestore(&atomic_rw, iflags); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3517 | kfree(buf); |
| 3518 | |
| 3519 | return ret; |
| 3520 | } |
| 3521 | |
| 3522 | #define SDEBUG_GET_LBA_STATUS_LEN 32 |
| 3523 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3524 | static int resp_get_lba_status(struct scsi_cmnd *scp, |
| 3525 | struct sdebug_dev_info *devip) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3526 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3527 | u8 *cmd = scp->cmnd; |
| 3528 | u64 lba; |
| 3529 | u32 alloc_len, mapped, num; |
| 3530 | u8 arr[SDEBUG_GET_LBA_STATUS_LEN]; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3531 | int ret; |
| 3532 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3533 | lba = get_unaligned_be64(cmd + 2); |
| 3534 | alloc_len = get_unaligned_be32(cmd + 10); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3535 | |
| 3536 | if (alloc_len < 24) |
| 3537 | return 0; |
| 3538 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3539 | ret = check_device_access_params(scp, lba, 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3540 | if (ret) |
| 3541 | return ret; |
| 3542 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3543 | if (scsi_debug_lbp()) |
| 3544 | mapped = map_state(lba, &num); |
| 3545 | else { |
| 3546 | mapped = 1; |
| 3547 | /* following just in case virtual_gb changed */ |
| 3548 | sdebug_capacity = get_sdebug_capacity(); |
| 3549 | if (sdebug_capacity - lba <= 0xffffffff) |
| 3550 | num = sdebug_capacity - lba; |
| 3551 | else |
| 3552 | num = 0xffffffff; |
| 3553 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3554 | |
| 3555 | memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3556 | put_unaligned_be32(20, arr); /* Parameter Data Length */ |
| 3557 | put_unaligned_be64(lba, arr + 8); /* LBA */ |
| 3558 | put_unaligned_be32(num, arr + 16); /* Number of blocks */ |
| 3559 | arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3560 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3561 | 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] | 3562 | } |
| 3563 | |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3564 | #define RL_BUCKET_ELEMS 8 |
| 3565 | |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3566 | /* Even though each pseudo target has a REPORT LUNS "well known logical unit" |
| 3567 | * (W-LUN), the normal Linux scanning logic does not associate it with a |
| 3568 | * device (e.g. /dev/sg7). The following magic will make that association: |
| 3569 | * "cd /sys/class/scsi_host/host<n> ; echo '- - 49409' > scan" |
| 3570 | * where <n> is a host number. If there are multiple targets in a host then |
| 3571 | * the above will associate a W-LUN to each target. To only get a W-LUN |
| 3572 | * for target 2, then use "echo '- 2 49409' > scan" . |
| 3573 | */ |
| 3574 | static int resp_report_luns(struct scsi_cmnd *scp, |
| 3575 | struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3576 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 3577 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3578 | unsigned int alloc_len; |
| 3579 | unsigned char select_report; |
| 3580 | u64 lun; |
| 3581 | struct scsi_lun *lun_p; |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3582 | u8 arr[RL_BUCKET_ELEMS * sizeof(struct scsi_lun)]; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3583 | unsigned int lun_cnt; /* normal LUN count (max: 256) */ |
| 3584 | unsigned int wlun_cnt; /* report luns W-LUN count */ |
| 3585 | unsigned int tlun_cnt; /* total LUN count */ |
| 3586 | unsigned int rlen; /* response length (in bytes) */ |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3587 | int k, j, n, res; |
| 3588 | unsigned int off_rsp = 0; |
| 3589 | const int sz_lun = sizeof(struct scsi_lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3590 | |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 3591 | clear_luns_changed_on_target(devip); |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3592 | |
| 3593 | select_report = cmd[2]; |
| 3594 | alloc_len = get_unaligned_be32(cmd + 6); |
| 3595 | |
| 3596 | if (alloc_len < 4) { |
| 3597 | pr_err("alloc len too small %d\n", alloc_len); |
| 3598 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | return check_condition_result; |
| 3600 | } |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3601 | |
| 3602 | switch (select_report) { |
| 3603 | case 0: /* all LUNs apart from W-LUNs */ |
| 3604 | lun_cnt = sdebug_max_luns; |
| 3605 | wlun_cnt = 0; |
| 3606 | break; |
| 3607 | case 1: /* only W-LUNs */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3608 | lun_cnt = 0; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3609 | wlun_cnt = 1; |
| 3610 | break; |
| 3611 | case 2: /* all LUNs */ |
| 3612 | lun_cnt = sdebug_max_luns; |
| 3613 | wlun_cnt = 1; |
| 3614 | break; |
| 3615 | case 0x10: /* only administrative LUs */ |
| 3616 | case 0x11: /* see SPC-5 */ |
| 3617 | case 0x12: /* only subsiduary LUs owned by referenced LU */ |
| 3618 | default: |
| 3619 | pr_debug("select report invalid %d\n", select_report); |
| 3620 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); |
| 3621 | return check_condition_result; |
| 3622 | } |
| 3623 | |
| 3624 | if (sdebug_no_lun_0 && (lun_cnt > 0)) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3625 | --lun_cnt; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3626 | |
| 3627 | tlun_cnt = lun_cnt + wlun_cnt; |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3628 | rlen = tlun_cnt * sz_lun; /* excluding 8 byte header */ |
| 3629 | scsi_set_resid(scp, scsi_bufflen(scp)); |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3630 | pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n", |
| 3631 | select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0); |
| 3632 | |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3633 | /* loops rely on sizeof response header same as sizeof lun (both 8) */ |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3634 | lun = sdebug_no_lun_0 ? 1 : 0; |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3635 | for (k = 0, j = 0, res = 0; true; ++k, j = 0) { |
| 3636 | memset(arr, 0, sizeof(arr)); |
| 3637 | lun_p = (struct scsi_lun *)&arr[0]; |
| 3638 | if (k == 0) { |
| 3639 | put_unaligned_be32(rlen, &arr[0]); |
| 3640 | ++lun_p; |
| 3641 | j = 1; |
| 3642 | } |
| 3643 | for ( ; j < RL_BUCKET_ELEMS; ++j, ++lun_p) { |
| 3644 | if ((k * RL_BUCKET_ELEMS) + j > lun_cnt) |
| 3645 | break; |
| 3646 | int_to_scsilun(lun++, lun_p); |
| 3647 | } |
| 3648 | if (j < RL_BUCKET_ELEMS) |
| 3649 | break; |
| 3650 | n = j * sz_lun; |
| 3651 | res = p_fill_from_dev_buffer(scp, arr, n, off_rsp); |
| 3652 | if (res) |
| 3653 | return res; |
| 3654 | off_rsp += n; |
| 3655 | } |
| 3656 | if (wlun_cnt) { |
| 3657 | int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p); |
| 3658 | ++j; |
| 3659 | } |
| 3660 | if (j > 0) |
| 3661 | 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] | 3662 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | } |
| 3664 | |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3665 | static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba, |
| 3666 | unsigned int num, struct sdebug_dev_info *devip) |
| 3667 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3668 | int j; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3669 | unsigned char *kaddr, *buf; |
| 3670 | unsigned int offset; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3671 | struct scsi_data_buffer *sdb = scsi_in(scp); |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3672 | struct sg_mapping_iter miter; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3673 | |
| 3674 | /* better not to use temporary buffer. */ |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 3675 | buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC); |
Akinobu Mita | c5af0db | 2014-02-26 22:57:01 +0900 | [diff] [blame] | 3676 | if (!buf) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 3677 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3678 | INSUFF_RES_ASCQ); |
Akinobu Mita | c5af0db | 2014-02-26 22:57:01 +0900 | [diff] [blame] | 3679 | return check_condition_result; |
| 3680 | } |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3681 | |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 3682 | scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp)); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3683 | |
| 3684 | offset = 0; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3685 | sg_miter_start(&miter, sdb->table.sgl, sdb->table.nents, |
| 3686 | SG_MITER_ATOMIC | SG_MITER_TO_SG); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3687 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3688 | while (sg_miter_next(&miter)) { |
| 3689 | kaddr = miter.addr; |
| 3690 | for (j = 0; j < miter.length; j++) |
| 3691 | *(kaddr + j) ^= *(buf + offset + j); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3692 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3693 | offset += miter.length; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3694 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3695 | sg_miter_stop(&miter); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3696 | kfree(buf); |
| 3697 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3698 | return 0; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3699 | } |
| 3700 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3701 | static int resp_xdwriteread_10(struct scsi_cmnd *scp, |
| 3702 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3703 | { |
| 3704 | u8 *cmd = scp->cmnd; |
| 3705 | u64 lba; |
| 3706 | u32 num; |
| 3707 | int errsts; |
| 3708 | |
| 3709 | if (!scsi_bidi_cmnd(scp)) { |
| 3710 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3711 | INSUFF_RES_ASCQ); |
| 3712 | return check_condition_result; |
| 3713 | } |
| 3714 | errsts = resp_read_dt0(scp, devip); |
| 3715 | if (errsts) |
| 3716 | return errsts; |
| 3717 | if (!(cmd[1] & 0x4)) { /* DISABLE_WRITE is not set */ |
| 3718 | errsts = resp_write_dt0(scp, devip); |
| 3719 | if (errsts) |
| 3720 | return errsts; |
| 3721 | } |
| 3722 | lba = get_unaligned_be32(cmd + 2); |
| 3723 | num = get_unaligned_be16(cmd + 7); |
| 3724 | return resp_xdwriteread(scp, lba, num, devip); |
| 3725 | } |
| 3726 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3727 | static struct sdebug_queue *get_queue(struct scsi_cmnd *cmnd) |
| 3728 | { |
Bart Van Assche | 458df78 | 2018-01-26 08:52:19 -0800 | [diff] [blame^] | 3729 | u32 tag = blk_mq_unique_tag(cmnd->request); |
| 3730 | u16 hwq = blk_mq_unique_tag_to_hwq(tag); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3731 | |
Bart Van Assche | 458df78 | 2018-01-26 08:52:19 -0800 | [diff] [blame^] | 3732 | pr_debug("tag=%#x, hwq=%d\n", tag, hwq); |
| 3733 | if (WARN_ON_ONCE(hwq >= submit_queues)) |
| 3734 | hwq = 0; |
| 3735 | return sdebug_q_arr + hwq; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3736 | } |
| 3737 | |
| 3738 | /* Queued (deferred) command completions converge here. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3739 | static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3740 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3741 | int qc_idx; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3742 | int retiring = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3743 | unsigned long iflags; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3744 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3745 | struct sdebug_queued_cmd *sqcp; |
| 3746 | struct scsi_cmnd *scp; |
| 3747 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3748 | |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3749 | sd_dp->defer_t = SDEB_DEFER_NONE; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3750 | qc_idx = sd_dp->qc_idx; |
| 3751 | sqp = sdebug_q_arr + sd_dp->sqa_idx; |
| 3752 | if (sdebug_statistics) { |
| 3753 | atomic_inc(&sdebug_completions); |
| 3754 | if (raw_smp_processor_id() != sd_dp->issuing_cpu) |
| 3755 | atomic_inc(&sdebug_miss_cpus); |
| 3756 | } |
| 3757 | if (unlikely((qc_idx < 0) || (qc_idx >= SDEBUG_CANQUEUE))) { |
| 3758 | pr_err("wild qc_idx=%d\n", qc_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | return; |
| 3760 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3761 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 3762 | sqcp = &sqp->qc_arr[qc_idx]; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3763 | scp = sqcp->a_cmnd; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3764 | if (unlikely(scp == NULL)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3765 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 3766 | pr_err("scp is NULL, sqa_idx=%d, qc_idx=%d\n", |
| 3767 | sd_dp->sqa_idx, qc_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | return; |
| 3769 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3770 | devip = (struct sdebug_dev_info *)scp->device->hostdata; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3771 | if (likely(devip)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3772 | atomic_dec(&devip->num_in_q); |
| 3773 | else |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3774 | pr_err("devip=NULL\n"); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3775 | if (unlikely(atomic_read(&retired_max_queue) > 0)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3776 | retiring = 1; |
| 3777 | |
| 3778 | sqcp->a_cmnd = NULL; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3779 | if (unlikely(!test_and_clear_bit(qc_idx, sqp->in_use_bm))) { |
| 3780 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3781 | pr_err("Unexpected completion\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3782 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3783 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3784 | |
| 3785 | if (unlikely(retiring)) { /* user has reduced max_queue */ |
| 3786 | int k, retval; |
| 3787 | |
| 3788 | retval = atomic_read(&retired_max_queue); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3789 | if (qc_idx >= retval) { |
| 3790 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3791 | pr_err("index %d too large\n", retval); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3792 | return; |
| 3793 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3794 | k = find_last_bit(sqp->in_use_bm, retval); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3795 | if ((k < sdebug_max_queue) || (k == retval)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3796 | atomic_set(&retired_max_queue, 0); |
| 3797 | else |
| 3798 | atomic_set(&retired_max_queue, k + 1); |
| 3799 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3800 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3801 | scp->scsi_done(scp); /* callback to mid level */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3802 | } |
| 3803 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3804 | /* When high resolution timer goes off this function is called. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3805 | static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3806 | { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3807 | struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer, |
| 3808 | hrt); |
| 3809 | sdebug_q_cmd_complete(sd_dp); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3810 | return HRTIMER_NORESTART; |
| 3811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3813 | /* When work queue schedules work, it calls this function. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3814 | static void sdebug_q_cmd_wq_complete(struct work_struct *work) |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3815 | { |
| 3816 | struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer, |
| 3817 | ew.work); |
| 3818 | sdebug_q_cmd_complete(sd_dp); |
| 3819 | } |
| 3820 | |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3821 | static bool got_shared_uuid; |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 3822 | static uuid_t shared_uuid; |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3823 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3824 | static struct sdebug_dev_info *sdebug_device_create( |
| 3825 | struct sdebug_host_info *sdbg_host, gfp_t flags) |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3826 | { |
| 3827 | struct sdebug_dev_info *devip; |
| 3828 | |
| 3829 | devip = kzalloc(sizeof(*devip), flags); |
| 3830 | if (devip) { |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3831 | if (sdebug_uuid_ctl == 1) |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 3832 | uuid_gen(&devip->lu_name); |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3833 | else if (sdebug_uuid_ctl == 2) { |
| 3834 | if (got_shared_uuid) |
| 3835 | devip->lu_name = shared_uuid; |
| 3836 | else { |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 3837 | uuid_gen(&shared_uuid); |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3838 | got_shared_uuid = true; |
| 3839 | devip->lu_name = shared_uuid; |
| 3840 | } |
| 3841 | } |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3842 | devip->sdbg_host = sdbg_host; |
| 3843 | list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list); |
| 3844 | } |
| 3845 | return devip; |
| 3846 | } |
| 3847 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3848 | 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] | 3849 | { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3850 | struct sdebug_host_info *sdbg_host; |
| 3851 | struct sdebug_dev_info *open_devip = NULL; |
| 3852 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | |
FUJITA Tomonori | d1e4c9c | 2008-03-02 18:30:18 +0900 | [diff] [blame] | 3854 | sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host); |
| 3855 | if (!sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3856 | pr_err("Host info NULL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3857 | return NULL; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 3858 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3859 | list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) { |
| 3860 | if ((devip->used) && (devip->channel == sdev->channel) && |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 3861 | (devip->target == sdev->id) && |
| 3862 | (devip->lun == sdev->lun)) |
| 3863 | return devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3864 | else { |
| 3865 | if ((!devip->used) && (!open_devip)) |
| 3866 | open_devip = devip; |
| 3867 | } |
| 3868 | } |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3869 | if (!open_devip) { /* try and make a new one */ |
| 3870 | open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC); |
| 3871 | if (!open_devip) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3872 | pr_err("out of memory at line %d\n", __LINE__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | return NULL; |
| 3874 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3875 | } |
FUJITA Tomonori | a75869d | 2008-03-20 11:09:17 +0900 | [diff] [blame] | 3876 | |
| 3877 | open_devip->channel = sdev->channel; |
| 3878 | open_devip->target = sdev->id; |
| 3879 | open_devip->lun = sdev->lun; |
| 3880 | open_devip->sdbg_host = sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3881 | atomic_set(&open_devip->num_in_q, 0); |
| 3882 | set_bit(SDEBUG_UA_POR, open_devip->uas_bm); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3883 | open_devip->used = true; |
FUJITA Tomonori | a75869d | 2008-03-20 11:09:17 +0900 | [diff] [blame] | 3884 | return open_devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | } |
| 3886 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3887 | static int scsi_debug_slave_alloc(struct scsi_device *sdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3888 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3889 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3890 | pr_info("slave_alloc <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3891 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 3892 | queue_flag_set_unlocked(QUEUE_FLAG_BIDI, sdp->request_queue); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3893 | return 0; |
| 3894 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3895 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3896 | static int scsi_debug_slave_configure(struct scsi_device *sdp) |
| 3897 | { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3898 | struct sdebug_dev_info *devip = |
| 3899 | (struct sdebug_dev_info *)sdp->hostdata; |
FUJITA Tomonori | a34c4e9 | 2008-03-25 09:26:50 +0900 | [diff] [blame] | 3900 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3901 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3902 | pr_info("slave_configure <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3903 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3904 | if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN) |
| 3905 | sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN; |
| 3906 | if (devip == NULL) { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3907 | devip = find_build_dev_info(sdp); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3908 | if (devip == NULL) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3909 | return 1; /* no resources, will be marked offline */ |
| 3910 | } |
Christoph Hellwig | c8b09f6 | 2014-11-03 20:15:14 +0100 | [diff] [blame] | 3911 | sdp->hostdata = devip; |
Akinobu Mita | 6bb5e6e | 2014-06-02 22:56:49 +0900 | [diff] [blame] | 3912 | blk_queue_max_segment_size(sdp->request_queue, -1U); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3913 | if (sdebug_no_uld) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 3914 | sdp->no_uld_attach = 1; |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 3915 | config_cdb_len(sdp); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3916 | return 0; |
| 3917 | } |
| 3918 | |
| 3919 | static void scsi_debug_slave_destroy(struct scsi_device *sdp) |
| 3920 | { |
| 3921 | struct sdebug_dev_info *devip = |
| 3922 | (struct sdebug_dev_info *)sdp->hostdata; |
| 3923 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3924 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3925 | pr_info("slave_destroy <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3926 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
| 3927 | if (devip) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3928 | /* make this slot available for re-use */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3929 | devip->used = false; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3930 | sdp->hostdata = NULL; |
| 3931 | } |
| 3932 | } |
| 3933 | |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3934 | static void stop_qc_helper(struct sdebug_defer *sd_dp, |
| 3935 | enum sdeb_defer_type defer_t) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3936 | { |
| 3937 | if (!sd_dp) |
| 3938 | return; |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3939 | if (defer_t == SDEB_DEFER_HRT) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3940 | hrtimer_cancel(&sd_dp->hrt); |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3941 | else if (defer_t == SDEB_DEFER_WQ) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3942 | cancel_work_sync(&sd_dp->ew.work); |
| 3943 | } |
| 3944 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3945 | /* If @cmnd found deletes its timer or work queue and returns true; else |
| 3946 | returns false */ |
| 3947 | static bool stop_queued_cmnd(struct scsi_cmnd *cmnd) |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3948 | { |
| 3949 | unsigned long iflags; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3950 | int j, k, qmax, r_qmax; |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3951 | enum sdeb_defer_type l_defer_t; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3952 | struct sdebug_queue *sqp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3953 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3954 | struct sdebug_dev_info *devip; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3955 | struct sdebug_defer *sd_dp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3956 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3957 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 3958 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 3959 | qmax = sdebug_max_queue; |
| 3960 | r_qmax = atomic_read(&retired_max_queue); |
| 3961 | if (r_qmax > qmax) |
| 3962 | qmax = r_qmax; |
| 3963 | for (k = 0; k < qmax; ++k) { |
| 3964 | if (test_bit(k, sqp->in_use_bm)) { |
| 3965 | sqcp = &sqp->qc_arr[k]; |
| 3966 | if (cmnd != sqcp->a_cmnd) |
| 3967 | continue; |
| 3968 | /* found */ |
| 3969 | devip = (struct sdebug_dev_info *) |
| 3970 | cmnd->device->hostdata; |
| 3971 | if (devip) |
| 3972 | atomic_dec(&devip->num_in_q); |
| 3973 | sqcp->a_cmnd = NULL; |
| 3974 | sd_dp = sqcp->sd_dp; |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3975 | if (sd_dp) { |
| 3976 | l_defer_t = sd_dp->defer_t; |
| 3977 | sd_dp->defer_t = SDEB_DEFER_NONE; |
| 3978 | } else |
| 3979 | l_defer_t = SDEB_DEFER_NONE; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3980 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3981 | stop_qc_helper(sd_dp, l_defer_t); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3982 | clear_bit(k, sqp->in_use_bm); |
| 3983 | return true; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3984 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3985 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3986 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3987 | } |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3988 | return false; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3989 | } |
| 3990 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3991 | /* Deletes (stops) timers or work queues of all queued commands */ |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3992 | static void stop_all_queued(void) |
| 3993 | { |
| 3994 | unsigned long iflags; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3995 | int j, k; |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 3996 | enum sdeb_defer_type l_defer_t; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3997 | struct sdebug_queue *sqp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3998 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3999 | struct sdebug_dev_info *devip; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4000 | struct sdebug_defer *sd_dp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 4001 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4002 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 4003 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 4004 | for (k = 0; k < SDEBUG_CANQUEUE; ++k) { |
| 4005 | if (test_bit(k, sqp->in_use_bm)) { |
| 4006 | sqcp = &sqp->qc_arr[k]; |
| 4007 | if (sqcp->a_cmnd == NULL) |
| 4008 | continue; |
| 4009 | devip = (struct sdebug_dev_info *) |
| 4010 | sqcp->a_cmnd->device->hostdata; |
| 4011 | if (devip) |
| 4012 | atomic_dec(&devip->num_in_q); |
| 4013 | sqcp->a_cmnd = NULL; |
| 4014 | sd_dp = sqcp->sd_dp; |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4015 | if (sd_dp) { |
| 4016 | l_defer_t = sd_dp->defer_t; |
| 4017 | sd_dp->defer_t = SDEB_DEFER_NONE; |
| 4018 | } else |
| 4019 | l_defer_t = SDEB_DEFER_NONE; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4020 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4021 | stop_qc_helper(sd_dp, l_defer_t); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4022 | clear_bit(k, sqp->in_use_bm); |
| 4023 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4024 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 4025 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4026 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 4027 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4028 | } |
| 4029 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4030 | /* Free queued command memory on heap */ |
| 4031 | static void free_all_queued(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4032 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4033 | int j, k; |
| 4034 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4035 | struct sdebug_queued_cmd *sqcp; |
| 4036 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4037 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 4038 | for (k = 0; k < SDEBUG_CANQUEUE; ++k) { |
| 4039 | sqcp = &sqp->qc_arr[k]; |
| 4040 | kfree(sqcp->sd_dp); |
| 4041 | sqcp->sd_dp = NULL; |
| 4042 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4043 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4044 | } |
| 4045 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4046 | static int scsi_debug_abort(struct scsi_cmnd *SCpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4047 | { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4048 | bool ok; |
| 4049 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4050 | ++num_aborts; |
| 4051 | if (SCpnt) { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4052 | ok = stop_queued_cmnd(SCpnt); |
| 4053 | if (SCpnt->device && (SDEBUG_OPT_ALL_NOISE & sdebug_opts)) |
| 4054 | sdev_printk(KERN_INFO, SCpnt->device, |
| 4055 | "%s: command%s found\n", __func__, |
| 4056 | ok ? "" : " not"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4058 | return SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | } |
| 4060 | |
| 4061 | static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt) |
| 4062 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4063 | ++num_dev_resets; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4064 | if (SCpnt && SCpnt->device) { |
| 4065 | struct scsi_device *sdp = SCpnt->device; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4066 | struct sdebug_dev_info *devip = |
| 4067 | (struct sdebug_dev_info *)sdp->hostdata; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4068 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4069 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4070 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | if (devip) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4072 | set_bit(SDEBUG_UA_POR, devip->uas_bm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4073 | } |
| 4074 | return SUCCESS; |
| 4075 | } |
| 4076 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4077 | static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt) |
| 4078 | { |
| 4079 | struct sdebug_host_info *sdbg_host; |
| 4080 | struct sdebug_dev_info *devip; |
| 4081 | struct scsi_device *sdp; |
| 4082 | struct Scsi_Host *hp; |
| 4083 | int k = 0; |
| 4084 | |
| 4085 | ++num_target_resets; |
| 4086 | if (!SCpnt) |
| 4087 | goto lie; |
| 4088 | sdp = SCpnt->device; |
| 4089 | if (!sdp) |
| 4090 | goto lie; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4091 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4092 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
| 4093 | hp = sdp->host; |
| 4094 | if (!hp) |
| 4095 | goto lie; |
| 4096 | sdbg_host = *(struct sdebug_host_info **)shost_priv(hp); |
| 4097 | if (sdbg_host) { |
| 4098 | list_for_each_entry(devip, |
| 4099 | &sdbg_host->dev_info_list, |
| 4100 | dev_list) |
| 4101 | if (devip->target == sdp->id) { |
| 4102 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 4103 | ++k; |
| 4104 | } |
| 4105 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4106 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4107 | sdev_printk(KERN_INFO, sdp, |
| 4108 | "%s: %d device(s) found in target\n", __func__, k); |
| 4109 | lie: |
| 4110 | return SUCCESS; |
| 4111 | } |
| 4112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt) |
| 4114 | { |
| 4115 | struct sdebug_host_info *sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4116 | struct sdebug_dev_info *devip; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4117 | struct scsi_device *sdp; |
| 4118 | struct Scsi_Host *hp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4119 | int k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | ++num_bus_resets; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4122 | if (!(SCpnt && SCpnt->device)) |
| 4123 | goto lie; |
| 4124 | sdp = SCpnt->device; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4125 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4126 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
| 4127 | hp = sdp->host; |
| 4128 | if (hp) { |
FUJITA Tomonori | d1e4c9c | 2008-03-02 18:30:18 +0900 | [diff] [blame] | 4129 | sdbg_host = *(struct sdebug_host_info **)shost_priv(hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | if (sdbg_host) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4131 | list_for_each_entry(devip, |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4132 | &sdbg_host->dev_info_list, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4133 | dev_list) { |
| 4134 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 4135 | ++k; |
| 4136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4137 | } |
| 4138 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4139 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4140 | sdev_printk(KERN_INFO, sdp, |
| 4141 | "%s: %d device(s) found in host\n", __func__, k); |
| 4142 | lie: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4143 | return SUCCESS; |
| 4144 | } |
| 4145 | |
| 4146 | static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt) |
| 4147 | { |
| 4148 | struct sdebug_host_info * sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4149 | struct sdebug_dev_info *devip; |
| 4150 | int k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | ++num_host_resets; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4153 | if ((SCpnt->device) && (SDEBUG_OPT_ALL_NOISE & sdebug_opts)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4154 | sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4155 | spin_lock(&sdebug_host_list_lock); |
| 4156 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4157 | list_for_each_entry(devip, &sdbg_host->dev_info_list, |
| 4158 | dev_list) { |
| 4159 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 4160 | ++k; |
| 4161 | } |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4162 | } |
| 4163 | spin_unlock(&sdebug_host_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4164 | stop_all_queued(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4165 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4166 | sdev_printk(KERN_INFO, SCpnt->device, |
| 4167 | "%s: %d device(s) found\n", __func__, k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4168 | return SUCCESS; |
| 4169 | } |
| 4170 | |
FUJITA Tomonori | f58b0ef | 2008-03-30 00:59:54 +0900 | [diff] [blame] | 4171 | static void __init sdebug_build_parts(unsigned char *ramp, |
FUJITA Tomonori | 5f2578e | 2008-03-30 00:59:57 +0900 | [diff] [blame] | 4172 | unsigned long store_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4173 | { |
| 4174 | struct partition * pp; |
| 4175 | int starts[SDEBUG_MAX_PARTS + 2]; |
| 4176 | int sectors_per_part, num_sectors, k; |
| 4177 | int heads_by_sects, start_sec, end_sec; |
| 4178 | |
| 4179 | /* assume partition table already zeroed */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4180 | if ((sdebug_num_parts < 1) || (store_size < 1048576)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | return; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4182 | if (sdebug_num_parts > SDEBUG_MAX_PARTS) { |
| 4183 | sdebug_num_parts = SDEBUG_MAX_PARTS; |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4184 | pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4186 | num_sectors = (int)sdebug_store_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | sectors_per_part = (num_sectors - sdebug_sectors_per) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4188 | / sdebug_num_parts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | heads_by_sects = sdebug_heads * sdebug_sectors_per; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4190 | starts[0] = sdebug_sectors_per; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4191 | for (k = 1; k < sdebug_num_parts; ++k) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | starts[k] = ((k * sectors_per_part) / heads_by_sects) |
| 4193 | * heads_by_sects; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4194 | starts[sdebug_num_parts] = num_sectors; |
| 4195 | starts[sdebug_num_parts + 1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | |
| 4197 | ramp[510] = 0x55; /* magic partition markings */ |
| 4198 | ramp[511] = 0xAA; |
| 4199 | pp = (struct partition *)(ramp + 0x1be); |
| 4200 | for (k = 0; starts[k + 1]; ++k, ++pp) { |
| 4201 | start_sec = starts[k]; |
| 4202 | end_sec = starts[k + 1] - 1; |
| 4203 | pp->boot_ind = 0; |
| 4204 | |
| 4205 | pp->cyl = start_sec / heads_by_sects; |
| 4206 | pp->head = (start_sec - (pp->cyl * heads_by_sects)) |
| 4207 | / sdebug_sectors_per; |
| 4208 | pp->sector = (start_sec % sdebug_sectors_per) + 1; |
| 4209 | |
| 4210 | pp->end_cyl = end_sec / heads_by_sects; |
| 4211 | pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects)) |
| 4212 | / sdebug_sectors_per; |
| 4213 | pp->end_sector = (end_sec % sdebug_sectors_per) + 1; |
| 4214 | |
Akinobu Mita | 150c354 | 2013-08-26 22:08:40 +0900 | [diff] [blame] | 4215 | pp->start_sect = cpu_to_le32(start_sec); |
| 4216 | pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | pp->sys_ind = 0x83; /* plain Linux partition */ |
| 4218 | } |
| 4219 | } |
| 4220 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4221 | static void block_unblock_all_queues(bool block) |
| 4222 | { |
| 4223 | int j; |
| 4224 | struct sdebug_queue *sqp; |
| 4225 | |
| 4226 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) |
| 4227 | atomic_set(&sqp->blocked, (int)block); |
| 4228 | } |
| 4229 | |
| 4230 | /* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1 |
| 4231 | * commands will be processed normally before triggers occur. |
| 4232 | */ |
| 4233 | static void tweak_cmnd_count(void) |
| 4234 | { |
| 4235 | int count, modulo; |
| 4236 | |
| 4237 | modulo = abs(sdebug_every_nth); |
| 4238 | if (modulo < 2) |
| 4239 | return; |
| 4240 | block_unblock_all_queues(true); |
| 4241 | count = atomic_read(&sdebug_cmnd_count); |
| 4242 | atomic_set(&sdebug_cmnd_count, (count / modulo) * modulo); |
| 4243 | block_unblock_all_queues(false); |
| 4244 | } |
| 4245 | |
| 4246 | static void clear_queue_stats(void) |
| 4247 | { |
| 4248 | atomic_set(&sdebug_cmnd_count, 0); |
| 4249 | atomic_set(&sdebug_completions, 0); |
| 4250 | atomic_set(&sdebug_miss_cpus, 0); |
| 4251 | atomic_set(&sdebug_a_tsf, 0); |
| 4252 | } |
| 4253 | |
| 4254 | static void setup_inject(struct sdebug_queue *sqp, |
| 4255 | struct sdebug_queued_cmd *sqcp) |
| 4256 | { |
| 4257 | if ((atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) > 0) |
| 4258 | return; |
| 4259 | sqcp->inj_recovered = !!(SDEBUG_OPT_RECOVERED_ERR & sdebug_opts); |
| 4260 | sqcp->inj_transport = !!(SDEBUG_OPT_TRANSPORT_ERR & sdebug_opts); |
| 4261 | sqcp->inj_dif = !!(SDEBUG_OPT_DIF_ERR & sdebug_opts); |
| 4262 | sqcp->inj_dix = !!(SDEBUG_OPT_DIX_ERR & sdebug_opts); |
| 4263 | sqcp->inj_short = !!(SDEBUG_OPT_SHORT_TRANSFER & sdebug_opts); |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 4264 | sqcp->inj_host_busy = !!(SDEBUG_OPT_HOST_BUSY & sdebug_opts); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4265 | } |
| 4266 | |
| 4267 | /* Complete the processing of the thread that queued a SCSI command to this |
| 4268 | * driver. It either completes the command by calling cmnd_done() or |
| 4269 | * schedules a hr timer or work queue then returns 0. Returns |
| 4270 | * SCSI_MLQUEUE_HOST_BUSY if temporarily out of resources. |
| 4271 | */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4272 | static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip, |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4273 | int scsi_result, int delta_jiff, int ndelay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4275 | unsigned long iflags; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4276 | int k, num_in_q, qdepth, inject; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4277 | struct sdebug_queue *sqp; |
| 4278 | struct sdebug_queued_cmd *sqcp; |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 4279 | struct scsi_device *sdp; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4280 | struct sdebug_defer *sd_dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4282 | if (unlikely(devip == NULL)) { |
| 4283 | if (scsi_result == 0) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4284 | scsi_result = DID_NO_CONNECT << 16; |
| 4285 | goto respond_in_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | } |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 4287 | sdp = cmnd->device; |
| 4288 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4289 | if (unlikely(sdebug_verbose && scsi_result)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4290 | sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n", |
| 4291 | __func__, scsi_result); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4292 | if (delta_jiff == 0) |
| 4293 | goto respond_in_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4295 | /* schedule the response at a later time if resources permit */ |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4296 | sqp = get_queue(cmnd); |
| 4297 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 4298 | if (unlikely(atomic_read(&sqp->blocked))) { |
| 4299 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 4300 | return SCSI_MLQUEUE_HOST_BUSY; |
| 4301 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4302 | num_in_q = atomic_read(&devip->num_in_q); |
| 4303 | qdepth = cmnd->device->queue_depth; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4304 | inject = 0; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4305 | if (unlikely((qdepth > 0) && (num_in_q >= qdepth))) { |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4306 | if (scsi_result) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4307 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4308 | goto respond_in_thread; |
| 4309 | } else |
| 4310 | scsi_result = device_qfull_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4311 | } else if (unlikely(sdebug_every_nth && |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4312 | (SDEBUG_OPT_RARE_TSF & sdebug_opts) && |
| 4313 | (scsi_result == 0))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4314 | if ((num_in_q == (qdepth - 1)) && |
| 4315 | (atomic_inc_return(&sdebug_a_tsf) >= |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4316 | abs(sdebug_every_nth))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4317 | atomic_set(&sdebug_a_tsf, 0); |
| 4318 | inject = 1; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4319 | scsi_result = device_qfull_result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4321 | } |
| 4322 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4323 | k = find_first_zero_bit(sqp->in_use_bm, sdebug_max_queue); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4324 | if (unlikely(k >= sdebug_max_queue)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4325 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4326 | if (scsi_result) |
| 4327 | goto respond_in_thread; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4328 | else if (SDEBUG_OPT_ALL_TSF & sdebug_opts) |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4329 | scsi_result = device_qfull_result; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4330 | if (SDEBUG_OPT_Q_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4331 | sdev_printk(KERN_INFO, sdp, |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4332 | "%s: max_queue=%d exceeded, %s\n", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4333 | __func__, sdebug_max_queue, |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4334 | (scsi_result ? "status: TASK SET FULL" : |
| 4335 | "report: host busy")); |
| 4336 | if (scsi_result) |
| 4337 | goto respond_in_thread; |
| 4338 | else |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4339 | return SCSI_MLQUEUE_HOST_BUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4340 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4341 | __set_bit(k, sqp->in_use_bm); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4342 | atomic_inc(&devip->num_in_q); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4343 | sqcp = &sqp->qc_arr[k]; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4344 | sqcp->a_cmnd = cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4345 | cmnd->host_scribble = (unsigned char *)sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4346 | cmnd->result = scsi_result; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4347 | sd_dp = sqcp->sd_dp; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4348 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 4349 | if (unlikely(sdebug_every_nth && sdebug_any_injecting_opt)) |
| 4350 | setup_inject(sqp, sqcp); |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4351 | if (sd_dp == NULL) { |
| 4352 | sd_dp = kzalloc(sizeof(*sd_dp), GFP_ATOMIC); |
| 4353 | if (sd_dp == NULL) |
| 4354 | return SCSI_MLQUEUE_HOST_BUSY; |
| 4355 | } |
| 4356 | if (delta_jiff > 0 || ndelay > 0) { |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 4357 | ktime_t kt; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4358 | |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 4359 | if (delta_jiff > 0) { |
Arnd Bergmann | 13f6b61 | 2017-11-27 12:36:25 +0100 | [diff] [blame] | 4360 | kt = ns_to_ktime((u64)delta_jiff * (NSEC_PER_SEC / HZ)); |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 4361 | } else |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4362 | kt = ndelay; |
| 4363 | if (!sd_dp->init_hrt) { |
| 4364 | sd_dp->init_hrt = true; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4365 | sqcp->sd_dp = sd_dp; |
| 4366 | hrtimer_init(&sd_dp->hrt, CLOCK_MONOTONIC, |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4367 | HRTIMER_MODE_REL_PINNED); |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4368 | sd_dp->hrt.function = sdebug_q_cmd_hrt_complete; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4369 | sd_dp->sqa_idx = sqp - sdebug_q_arr; |
| 4370 | sd_dp->qc_idx = k; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4371 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4372 | if (sdebug_statistics) |
| 4373 | sd_dp->issuing_cpu = raw_smp_processor_id(); |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4374 | sd_dp->defer_t = SDEB_DEFER_HRT; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4375 | hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL_PINNED); |
| 4376 | } else { /* jdelay < 0, use work queue */ |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4377 | if (!sd_dp->init_wq) { |
| 4378 | sd_dp->init_wq = true; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4379 | sqcp->sd_dp = sd_dp; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4380 | sd_dp->sqa_idx = sqp - sdebug_q_arr; |
| 4381 | sd_dp->qc_idx = k; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4382 | INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4383 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4384 | if (sdebug_statistics) |
| 4385 | sd_dp->issuing_cpu = raw_smp_processor_id(); |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 4386 | sd_dp->defer_t = SDEB_DEFER_WQ; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4387 | schedule_work(&sd_dp->ew.work); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4388 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4389 | if (unlikely((SDEBUG_OPT_Q_NOISE & sdebug_opts) && |
| 4390 | (scsi_result == device_qfull_result))) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4391 | sdev_printk(KERN_INFO, sdp, |
| 4392 | "%s: num_in_q=%d +1, %s%s\n", __func__, |
| 4393 | num_in_q, (inject ? "<inject> " : ""), |
| 4394 | "status: TASK SET FULL"); |
| 4395 | return 0; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4396 | |
| 4397 | respond_in_thread: /* call back to mid-layer using invocation thread */ |
| 4398 | cmnd->result = scsi_result; |
| 4399 | cmnd->scsi_done(cmnd); |
| 4400 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4402 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4403 | /* Note: The following macros create attribute files in the |
| 4404 | /sys/module/scsi_debug/parameters directory. Unfortunately this |
| 4405 | driver is unaware of a change and cannot trigger auxiliary actions |
| 4406 | as it can when the corresponding attribute in the |
| 4407 | /sys/bus/pseudo/drivers/scsi_debug directory is changed. |
| 4408 | */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4409 | module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR); |
| 4410 | module_param_named(ato, sdebug_ato, int, S_IRUGO); |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 4411 | module_param_named(cdb_len, sdebug_cdb_len, int, 0644); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4412 | module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR); |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4413 | module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4414 | module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO); |
| 4415 | module_param_named(dif, sdebug_dif, int, S_IRUGO); |
| 4416 | module_param_named(dix, sdebug_dix, int, S_IRUGO); |
| 4417 | module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR); |
| 4418 | module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR); |
| 4419 | module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR); |
| 4420 | module_param_named(guard, sdebug_guard, uint, S_IRUGO); |
| 4421 | 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] | 4422 | module_param_string(inq_vendor, sdebug_inq_vendor_id, |
| 4423 | sizeof(sdebug_inq_vendor_id), S_IRUGO|S_IWUSR); |
| 4424 | module_param_string(inq_product, sdebug_inq_product_id, |
| 4425 | sizeof(sdebug_inq_product_id), S_IRUGO|S_IWUSR); |
| 4426 | module_param_string(inq_rev, sdebug_inq_product_rev, |
| 4427 | sizeof(sdebug_inq_product_rev), S_IRUGO|S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4428 | module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO); |
| 4429 | module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO); |
| 4430 | module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO); |
| 4431 | module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO); |
| 4432 | module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO); |
| 4433 | module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR); |
| 4434 | module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR); |
| 4435 | module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR); |
| 4436 | module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR); |
| 4437 | module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO); |
| 4438 | module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO); |
| 4439 | module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR); |
| 4440 | module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO); |
| 4441 | module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR); |
| 4442 | module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO); |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 4443 | 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] | 4444 | module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR); |
| 4445 | module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR); |
| 4446 | module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO); |
| 4447 | module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4448 | module_param_named(statistics, sdebug_statistics, bool, S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4449 | module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4450 | module_param_named(submit_queues, submit_queues, int, S_IRUGO); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4451 | module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO); |
| 4452 | module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO); |
| 4453 | module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO); |
| 4454 | module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO); |
| 4455 | 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] | 4456 | module_param_named(uuid_ctl, sdebug_uuid_ctl, int, S_IRUGO); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4457 | module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int, |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4458 | S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4459 | module_param_named(write_same_length, sdebug_write_same_length, int, |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4460 | S_IRUGO | S_IWUSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | |
| 4462 | MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert"); |
| 4463 | MODULE_DESCRIPTION("SCSI debug adapter driver"); |
| 4464 | MODULE_LICENSE("GPL"); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4465 | MODULE_VERSION(SDEBUG_VERSION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | |
| 4467 | MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4468 | 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] | 4469 | MODULE_PARM_DESC(cdb_len, "suggest CDB lengths to drivers (def=10)"); |
Akinobu Mita | 0759c66 | 2014-02-26 22:57:04 +0900 | [diff] [blame] | 4470 | MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4471 | 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] | 4472 | 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] | 4473 | MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)"); |
| 4474 | MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4475 | MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)"); |
Randy Dunlap | beb87c3 | 2007-06-11 11:36:40 -0700 | [diff] [blame] | 4476 | MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)"); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4477 | 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] | 4478 | MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)"); |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 4479 | MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)"); |
Hannes Reinecke | e5203cf | 2017-10-02 16:26:33 +0200 | [diff] [blame] | 4480 | MODULE_PARM_DESC(inq_vendor, "SCSI INQUIRY vendor string (def=\"Linux\")"); |
| 4481 | MODULE_PARM_DESC(inq_product, "SCSI INQUIRY product string (def=\"scsi_debug\")"); |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 4482 | MODULE_PARM_DESC(inq_rev, "SCSI INQUIRY revision string (def=\"" |
| 4483 | SDEBUG_VERSION "\")"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4484 | MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)"); |
| 4485 | MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)"); |
| 4486 | 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] | 4487 | MODULE_PARM_DESC(lbprz, |
| 4488 | "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] | 4489 | MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4490 | 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] | 4491 | MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))"); |
| 4492 | MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4493 | 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] | 4494 | 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] | 4495 | MODULE_PARM_DESC(num_parts, "number of partitions(def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4496 | 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] | 4497 | MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)"); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 4498 | 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] | 4499 | MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)"); |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 4500 | 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] | 4501 | MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])"); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4502 | MODULE_PARM_DESC(removable, "claim to have removable media (def=0)"); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 4503 | 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] | 4504 | MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4505 | MODULE_PARM_DESC(statistics, "collect statistics on commands, queues (def=0)"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4506 | MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4507 | 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] | 4508 | MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)"); |
| 4509 | 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] | 4510 | MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)"); |
| 4511 | 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] | 4512 | MODULE_PARM_DESC(uuid_ctl, |
| 4513 | "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] | 4514 | 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] | 4515 | MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)"); |
| 4516 | 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] | 4517 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 4518 | #define SDEBUG_INFO_LEN 256 |
| 4519 | static char sdebug_info[SDEBUG_INFO_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | |
| 4521 | static const char * scsi_debug_info(struct Scsi_Host * shp) |
| 4522 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4523 | int k; |
| 4524 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 4525 | k = scnprintf(sdebug_info, SDEBUG_INFO_LEN, "%s: version %s [%s]\n", |
| 4526 | my_name, SDEBUG_VERSION, sdebug_version_date); |
| 4527 | if (k >= (SDEBUG_INFO_LEN - 1)) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4528 | return sdebug_info; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 4529 | scnprintf(sdebug_info + k, SDEBUG_INFO_LEN - k, |
| 4530 | " dev_size_mb=%d, opts=0x%x, submit_queues=%d, %s=%d", |
| 4531 | sdebug_dev_size_mb, sdebug_opts, submit_queues, |
| 4532 | "statistics", (int)sdebug_statistics); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4533 | return sdebug_info; |
| 4534 | } |
| 4535 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4536 | /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4537 | static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer, |
| 4538 | int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | { |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4540 | char arr[16]; |
| 4541 | int opts; |
| 4542 | int minLen = length > 15 ? 15 : length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4544 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 4545 | return -EACCES; |
| 4546 | memcpy(arr, buffer, minLen); |
| 4547 | arr[minLen] = '\0'; |
| 4548 | if (1 != sscanf(arr, "%d", &opts)) |
| 4549 | return -EINVAL; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4550 | sdebug_opts = opts; |
| 4551 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); |
| 4552 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts); |
| 4553 | if (sdebug_every_nth != 0) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4554 | tweak_cmnd_count(); |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4555 | return length; |
| 4556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4558 | /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the |
| 4559 | * same for each scsi_debug host (if more than one). Some of the counters |
| 4560 | * output are not atomics so might be inaccurate in a busy system. */ |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4561 | static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host) |
| 4562 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4563 | int f, j, l; |
| 4564 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4565 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4566 | seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n", |
| 4567 | SDEBUG_VERSION, sdebug_version_date); |
| 4568 | seq_printf(m, "num_tgts=%d, %ssize=%d MB, opts=0x%x, every_nth=%d\n", |
| 4569 | sdebug_num_tgts, "shared (ram) ", sdebug_dev_size_mb, |
| 4570 | sdebug_opts, sdebug_every_nth); |
| 4571 | seq_printf(m, "delay=%d, ndelay=%d, max_luns=%d, sector_size=%d %s\n", |
| 4572 | sdebug_jdelay, sdebug_ndelay, sdebug_max_luns, |
| 4573 | sdebug_sector_size, "bytes"); |
| 4574 | seq_printf(m, "cylinders=%d, heads=%d, sectors=%d, command aborts=%d\n", |
| 4575 | sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per, |
| 4576 | num_aborts); |
| 4577 | seq_printf(m, "RESETs: device=%d, target=%d, bus=%d, host=%d\n", |
| 4578 | num_dev_resets, num_target_resets, num_bus_resets, |
| 4579 | num_host_resets); |
| 4580 | seq_printf(m, "dix_reads=%d, dix_writes=%d, dif_errors=%d\n", |
| 4581 | dix_reads, dix_writes, dif_errors); |
Bart Van Assche | 458df78 | 2018-01-26 08:52:19 -0800 | [diff] [blame^] | 4582 | seq_printf(m, "usec_in_jiffy=%lu, statistics=%d\n", TICK_NSEC / 1000, |
| 4583 | sdebug_statistics); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4584 | seq_printf(m, "cmnd_count=%d, completions=%d, %s=%d, a_tsf=%d\n", |
| 4585 | atomic_read(&sdebug_cmnd_count), |
| 4586 | atomic_read(&sdebug_completions), |
| 4587 | "miss_cpus", atomic_read(&sdebug_miss_cpus), |
| 4588 | atomic_read(&sdebug_a_tsf)); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4589 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4590 | seq_printf(m, "submit_queues=%d\n", submit_queues); |
| 4591 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 4592 | seq_printf(m, " queue %d:\n", j); |
| 4593 | f = find_first_bit(sqp->in_use_bm, sdebug_max_queue); |
| 4594 | if (f != sdebug_max_queue) { |
| 4595 | l = find_last_bit(sqp->in_use_bm, sdebug_max_queue); |
| 4596 | seq_printf(m, " in_use_bm BUSY: %s: %d,%d\n", |
| 4597 | "first,last bits", f, l); |
| 4598 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4599 | } |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4600 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4601 | } |
| 4602 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4603 | static ssize_t delay_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | { |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4605 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4606 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4607 | /* Returns -EBUSY if jdelay is being changed and commands are queued. The unit |
| 4608 | * of delay is jiffies. |
| 4609 | */ |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4610 | static ssize_t delay_store(struct device_driver *ddp, const char *buf, |
| 4611 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | { |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4613 | int jdelay, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4615 | if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4616 | res = count; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4617 | if (sdebug_jdelay != jdelay) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4618 | int j, k; |
| 4619 | struct sdebug_queue *sqp; |
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 | block_unblock_all_queues(true); |
| 4622 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; |
| 4623 | ++j, ++sqp) { |
| 4624 | k = find_first_bit(sqp->in_use_bm, |
| 4625 | sdebug_max_queue); |
| 4626 | if (k != sdebug_max_queue) { |
| 4627 | res = -EBUSY; /* queued commands */ |
| 4628 | break; |
| 4629 | } |
| 4630 | } |
| 4631 | if (res > 0) { |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4632 | sdebug_jdelay = jdelay; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4633 | sdebug_ndelay = 0; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4634 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4635 | block_unblock_all_queues(false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4637 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | } |
| 4639 | return -EINVAL; |
| 4640 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4641 | static DRIVER_ATTR_RW(delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4643 | static ssize_t ndelay_show(struct device_driver *ddp, char *buf) |
| 4644 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4645 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4646 | } |
| 4647 | /* Returns -EBUSY if ndelay is being changed and commands are queued */ |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4648 | /* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4649 | static ssize_t ndelay_store(struct device_driver *ddp, const char *buf, |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4650 | size_t count) |
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 | int ndelay, res; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4653 | |
| 4654 | if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) && |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4655 | (ndelay >= 0) && (ndelay < (1000 * 1000 * 1000))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4656 | res = count; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4657 | if (sdebug_ndelay != ndelay) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4658 | int j, k; |
| 4659 | struct sdebug_queue *sqp; |
| 4660 | |
| 4661 | block_unblock_all_queues(true); |
| 4662 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; |
| 4663 | ++j, ++sqp) { |
| 4664 | k = find_first_bit(sqp->in_use_bm, |
| 4665 | sdebug_max_queue); |
| 4666 | if (k != sdebug_max_queue) { |
| 4667 | res = -EBUSY; /* queued commands */ |
| 4668 | break; |
| 4669 | } |
| 4670 | } |
| 4671 | if (res > 0) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4672 | sdebug_ndelay = ndelay; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4673 | sdebug_jdelay = ndelay ? JDELAY_OVERRIDDEN |
| 4674 | : DEF_JDELAY; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4675 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4676 | block_unblock_all_queues(false); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4677 | } |
| 4678 | return res; |
| 4679 | } |
| 4680 | return -EINVAL; |
| 4681 | } |
| 4682 | static DRIVER_ATTR_RW(ndelay); |
| 4683 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4684 | static ssize_t opts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4686 | return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | } |
| 4688 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4689 | static ssize_t opts_store(struct device_driver *ddp, const char *buf, |
| 4690 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4692 | int opts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | char work[20]; |
| 4694 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4695 | if (sscanf(buf, "%10s", work) == 1) { |
| 4696 | if (strncasecmp(work, "0x", 2) == 0) { |
| 4697 | if (kstrtoint(work + 2, 16, &opts) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4698 | goto opts_done; |
| 4699 | } else { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4700 | if (kstrtoint(work, 10, &opts) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | goto opts_done; |
| 4702 | } |
| 4703 | } |
| 4704 | return -EINVAL; |
| 4705 | opts_done: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4706 | sdebug_opts = opts; |
| 4707 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); |
| 4708 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4709 | tweak_cmnd_count(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4710 | return count; |
| 4711 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4712 | static DRIVER_ATTR_RW(opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4714 | static ssize_t ptype_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4715 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4716 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4717 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4718 | static ssize_t ptype_store(struct device_driver *ddp, const char *buf, |
| 4719 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4720 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4721 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | |
| 4723 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4724 | sdebug_ptype = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | return count; |
| 4726 | } |
| 4727 | return -EINVAL; |
| 4728 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4729 | static DRIVER_ATTR_RW(ptype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4730 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4731 | static ssize_t dsense_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4732 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4733 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4735 | static ssize_t dsense_store(struct device_driver *ddp, const char *buf, |
| 4736 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4738 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | |
| 4740 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4741 | sdebug_dsense = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | return count; |
| 4743 | } |
| 4744 | return -EINVAL; |
| 4745 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4746 | static DRIVER_ATTR_RW(dsense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4747 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4748 | static ssize_t fake_rw_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4749 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4750 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4751 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4752 | static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf, |
| 4753 | size_t count) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4754 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4755 | int n; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4756 | |
| 4757 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4758 | n = (n > 0); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4759 | sdebug_fake_rw = (sdebug_fake_rw > 0); |
| 4760 | if (sdebug_fake_rw != n) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4761 | if ((0 == n) && (NULL == fake_storep)) { |
| 4762 | unsigned long sz = |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4763 | (unsigned long)sdebug_dev_size_mb * |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4764 | 1048576; |
| 4765 | |
| 4766 | fake_storep = vmalloc(sz); |
| 4767 | if (NULL == fake_storep) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4768 | pr_err("out of memory, 9\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4769 | return -ENOMEM; |
| 4770 | } |
| 4771 | memset(fake_storep, 0, sz); |
| 4772 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4773 | sdebug_fake_rw = n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4774 | } |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4775 | return count; |
| 4776 | } |
| 4777 | return -EINVAL; |
| 4778 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4779 | static DRIVER_ATTR_RW(fake_rw); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4780 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4781 | 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] | 4782 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4783 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4784 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4785 | static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf, |
| 4786 | size_t count) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4787 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4788 | int n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4789 | |
| 4790 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4791 | sdebug_no_lun_0 = n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4792 | return count; |
| 4793 | } |
| 4794 | return -EINVAL; |
| 4795 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4796 | static DRIVER_ATTR_RW(no_lun_0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4797 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4798 | static ssize_t num_tgts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4799 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4800 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4802 | static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf, |
| 4803 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4805 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | |
| 4807 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4808 | sdebug_num_tgts = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | sdebug_max_tgts_luns(); |
| 4810 | return count; |
| 4811 | } |
| 4812 | return -EINVAL; |
| 4813 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4814 | static DRIVER_ATTR_RW(num_tgts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4815 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4816 | 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] | 4817 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4818 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4820 | static DRIVER_ATTR_RO(dev_size_mb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4822 | static ssize_t num_parts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4823 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4824 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4826 | static DRIVER_ATTR_RO(num_parts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4827 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4828 | static ssize_t every_nth_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_every_nth); |
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 every_nth_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 nth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | |
| 4837 | if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4838 | sdebug_every_nth = nth; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4839 | if (nth && !sdebug_statistics) { |
| 4840 | pr_info("every_nth needs statistics=1, set it\n"); |
| 4841 | sdebug_statistics = true; |
| 4842 | } |
| 4843 | tweak_cmnd_count(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | return count; |
| 4845 | } |
| 4846 | return -EINVAL; |
| 4847 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4848 | static DRIVER_ATTR_RW(every_nth); |
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 ssize_t max_luns_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4852 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4854 | static ssize_t max_luns_store(struct device_driver *ddp, const char *buf, |
| 4855 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4857 | int n; |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 4858 | bool changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | |
| 4860 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 4861 | if (n > 256) { |
| 4862 | pr_warn("max_luns can be no more than 256\n"); |
| 4863 | return -EINVAL; |
| 4864 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4865 | changed = (sdebug_max_luns != n); |
| 4866 | sdebug_max_luns = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4867 | sdebug_max_tgts_luns(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4868 | if (changed && (sdebug_scsi_level >= 5)) { /* >= SPC-3 */ |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 4869 | struct sdebug_host_info *sdhp; |
| 4870 | struct sdebug_dev_info *dp; |
| 4871 | |
| 4872 | spin_lock(&sdebug_host_list_lock); |
| 4873 | list_for_each_entry(sdhp, &sdebug_host_list, |
| 4874 | host_list) { |
| 4875 | list_for_each_entry(dp, &sdhp->dev_info_list, |
| 4876 | dev_list) { |
| 4877 | set_bit(SDEBUG_UA_LUNS_CHANGED, |
| 4878 | dp->uas_bm); |
| 4879 | } |
| 4880 | } |
| 4881 | spin_unlock(&sdebug_host_list_lock); |
| 4882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | return count; |
| 4884 | } |
| 4885 | return -EINVAL; |
| 4886 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4887 | static DRIVER_ATTR_RW(max_luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4889 | static ssize_t max_queue_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4890 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4891 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4892 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4893 | /* N.B. max_queue can be changed while there are queued commands. In flight |
| 4894 | * commands beyond the new max_queue will be completed. */ |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4895 | static ssize_t max_queue_store(struct device_driver *ddp, const char *buf, |
| 4896 | size_t count) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4897 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4898 | int j, n, k, a; |
| 4899 | struct sdebug_queue *sqp; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4900 | |
| 4901 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) && |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4902 | (n <= SDEBUG_CANQUEUE)) { |
| 4903 | block_unblock_all_queues(true); |
| 4904 | k = 0; |
| 4905 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; |
| 4906 | ++j, ++sqp) { |
| 4907 | a = find_last_bit(sqp->in_use_bm, SDEBUG_CANQUEUE); |
| 4908 | if (a > k) |
| 4909 | k = a; |
| 4910 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4911 | sdebug_max_queue = n; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4912 | if (k == SDEBUG_CANQUEUE) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4913 | atomic_set(&retired_max_queue, 0); |
| 4914 | else if (k >= n) |
| 4915 | atomic_set(&retired_max_queue, k + 1); |
| 4916 | else |
| 4917 | atomic_set(&retired_max_queue, 0); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4918 | block_unblock_all_queues(false); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4919 | return count; |
| 4920 | } |
| 4921 | return -EINVAL; |
| 4922 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4923 | static DRIVER_ATTR_RW(max_queue); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4924 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4925 | static ssize_t no_uld_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4926 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4927 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4928 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4929 | static DRIVER_ATTR_RO(no_uld); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4930 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4931 | static ssize_t scsi_level_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4933 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4934 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4935 | static DRIVER_ATTR_RO(scsi_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4937 | static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4938 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4939 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4940 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4941 | static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf, |
| 4942 | size_t count) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4943 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4944 | int n; |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4945 | bool changed; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4946 | |
| 4947 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4948 | changed = (sdebug_virtual_gb != n); |
| 4949 | sdebug_virtual_gb = n; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4950 | sdebug_capacity = get_sdebug_capacity(); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4951 | if (changed) { |
| 4952 | struct sdebug_host_info *sdhp; |
| 4953 | struct sdebug_dev_info *dp; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4954 | |
Ewan D. Milne | 4bc6b63 | 2014-12-04 11:49:26 -0500 | [diff] [blame] | 4955 | spin_lock(&sdebug_host_list_lock); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4956 | list_for_each_entry(sdhp, &sdebug_host_list, |
| 4957 | host_list) { |
| 4958 | list_for_each_entry(dp, &sdhp->dev_info_list, |
| 4959 | dev_list) { |
| 4960 | set_bit(SDEBUG_UA_CAPACITY_CHANGED, |
| 4961 | dp->uas_bm); |
| 4962 | } |
| 4963 | } |
Ewan D. Milne | 4bc6b63 | 2014-12-04 11:49:26 -0500 | [diff] [blame] | 4964 | spin_unlock(&sdebug_host_list_lock); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4965 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4966 | return count; |
| 4967 | } |
| 4968 | return -EINVAL; |
| 4969 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4970 | static DRIVER_ATTR_RW(virtual_gb); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4971 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4972 | static ssize_t add_host_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4974 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | } |
| 4976 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4977 | static int sdebug_add_adapter(void); |
| 4978 | static void sdebug_remove_adapter(void); |
| 4979 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4980 | static ssize_t add_host_store(struct device_driver *ddp, const char *buf, |
| 4981 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | { |
FUJITA Tomonori | f3df41c | 2008-03-20 11:09:15 +0900 | [diff] [blame] | 4983 | int delta_hosts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4984 | |
FUJITA Tomonori | f3df41c | 2008-03-20 11:09:15 +0900 | [diff] [blame] | 4985 | if (sscanf(buf, "%d", &delta_hosts) != 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4986 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4987 | if (delta_hosts > 0) { |
| 4988 | do { |
| 4989 | sdebug_add_adapter(); |
| 4990 | } while (--delta_hosts); |
| 4991 | } else if (delta_hosts < 0) { |
| 4992 | do { |
| 4993 | sdebug_remove_adapter(); |
| 4994 | } while (++delta_hosts); |
| 4995 | } |
| 4996 | return count; |
| 4997 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4998 | static DRIVER_ATTR_RW(add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4999 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5000 | 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] | 5001 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5002 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 5003 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5004 | static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf, |
| 5005 | size_t count) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 5006 | { |
| 5007 | int n; |
| 5008 | |
| 5009 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5010 | sdebug_vpd_use_hostno = n; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 5011 | return count; |
| 5012 | } |
| 5013 | return -EINVAL; |
| 5014 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5015 | static DRIVER_ATTR_RW(vpd_use_hostno); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 5016 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5017 | static ssize_t statistics_show(struct device_driver *ddp, char *buf) |
| 5018 | { |
| 5019 | return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_statistics); |
| 5020 | } |
| 5021 | static ssize_t statistics_store(struct device_driver *ddp, const char *buf, |
| 5022 | size_t count) |
| 5023 | { |
| 5024 | int n; |
| 5025 | |
| 5026 | if ((count > 0) && (sscanf(buf, "%d", &n) == 1) && (n >= 0)) { |
| 5027 | if (n > 0) |
| 5028 | sdebug_statistics = true; |
| 5029 | else { |
| 5030 | clear_queue_stats(); |
| 5031 | sdebug_statistics = false; |
| 5032 | } |
| 5033 | return count; |
| 5034 | } |
| 5035 | return -EINVAL; |
| 5036 | } |
| 5037 | static DRIVER_ATTR_RW(statistics); |
| 5038 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5039 | static ssize_t sector_size_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 5040 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5041 | return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size); |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 5042 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5043 | static DRIVER_ATTR_RO(sector_size); |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 5044 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5045 | static ssize_t submit_queues_show(struct device_driver *ddp, char *buf) |
| 5046 | { |
| 5047 | return scnprintf(buf, PAGE_SIZE, "%d\n", submit_queues); |
| 5048 | } |
| 5049 | static DRIVER_ATTR_RO(submit_queues); |
| 5050 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5051 | static ssize_t dix_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5052 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5053 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5054 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5055 | static DRIVER_ATTR_RO(dix); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5056 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5057 | static ssize_t dif_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5058 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5059 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5060 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5061 | static DRIVER_ATTR_RO(dif); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5062 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5063 | static ssize_t guard_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5064 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5065 | return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5066 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5067 | static DRIVER_ATTR_RO(guard); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5068 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5069 | static ssize_t ato_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5070 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5071 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5072 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5073 | static DRIVER_ATTR_RO(ato); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5074 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5075 | static ssize_t map_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5076 | { |
| 5077 | ssize_t count; |
| 5078 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 5079 | if (!scsi_debug_lbp()) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5080 | return scnprintf(buf, PAGE_SIZE, "0-%u\n", |
| 5081 | sdebug_store_sectors); |
| 5082 | |
Tejun Heo | c7badc9 | 2015-02-13 14:37:51 -0800 | [diff] [blame] | 5083 | count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", |
| 5084 | (int)map_size, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5085 | buf[count++] = '\n'; |
Tejun Heo | c7badc9 | 2015-02-13 14:37:51 -0800 | [diff] [blame] | 5086 | buf[count] = '\0'; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5087 | |
| 5088 | return count; |
| 5089 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5090 | static DRIVER_ATTR_RO(map); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5091 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5092 | static ssize_t removable_show(struct device_driver *ddp, char *buf) |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 5093 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5094 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 5095 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5096 | static ssize_t removable_store(struct device_driver *ddp, const char *buf, |
| 5097 | size_t count) |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 5098 | { |
| 5099 | int n; |
| 5100 | |
| 5101 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5102 | sdebug_removable = (n > 0); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 5103 | return count; |
| 5104 | } |
| 5105 | return -EINVAL; |
| 5106 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5107 | static DRIVER_ATTR_RW(removable); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 5108 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5109 | static ssize_t host_lock_show(struct device_driver *ddp, char *buf) |
| 5110 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5111 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5112 | } |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 5113 | /* N.B. sdebug_host_lock does nothing, kept for backward compatibility */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5114 | static ssize_t host_lock_store(struct device_driver *ddp, const char *buf, |
| 5115 | size_t count) |
| 5116 | { |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 5117 | int n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5118 | |
| 5119 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 5120 | sdebug_host_lock = (n > 0); |
| 5121 | return count; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5122 | } |
| 5123 | return -EINVAL; |
| 5124 | } |
| 5125 | static DRIVER_ATTR_RW(host_lock); |
| 5126 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5127 | static ssize_t strict_show(struct device_driver *ddp, char *buf) |
| 5128 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5129 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5130 | } |
| 5131 | static ssize_t strict_store(struct device_driver *ddp, const char *buf, |
| 5132 | size_t count) |
| 5133 | { |
| 5134 | int n; |
| 5135 | |
| 5136 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5137 | sdebug_strict = (n > 0); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5138 | return count; |
| 5139 | } |
| 5140 | return -EINVAL; |
| 5141 | } |
| 5142 | static DRIVER_ATTR_RW(strict); |
| 5143 | |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 5144 | static ssize_t uuid_ctl_show(struct device_driver *ddp, char *buf) |
| 5145 | { |
| 5146 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_uuid_ctl); |
| 5147 | } |
| 5148 | static DRIVER_ATTR_RO(uuid_ctl); |
| 5149 | |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 5150 | static ssize_t cdb_len_show(struct device_driver *ddp, char *buf) |
| 5151 | { |
| 5152 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_cdb_len); |
| 5153 | } |
| 5154 | static ssize_t cdb_len_store(struct device_driver *ddp, const char *buf, |
| 5155 | size_t count) |
| 5156 | { |
| 5157 | int ret, n; |
| 5158 | |
| 5159 | ret = kstrtoint(buf, 0, &n); |
| 5160 | if (ret) |
| 5161 | return ret; |
| 5162 | sdebug_cdb_len = n; |
| 5163 | all_config_cdb_len(); |
| 5164 | return count; |
| 5165 | } |
| 5166 | static DRIVER_ATTR_RW(cdb_len); |
| 5167 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5168 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5169 | /* Note: The following array creates attribute files in the |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 5170 | /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these |
| 5171 | files (over those found in the /sys/module/scsi_debug/parameters |
| 5172 | directory) is that auxiliary actions can be triggered when an attribute |
| 5173 | is changed. For example see: sdebug_add_host_store() above. |
| 5174 | */ |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5175 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5176 | static struct attribute *sdebug_drv_attrs[] = { |
| 5177 | &driver_attr_delay.attr, |
| 5178 | &driver_attr_opts.attr, |
| 5179 | &driver_attr_ptype.attr, |
| 5180 | &driver_attr_dsense.attr, |
| 5181 | &driver_attr_fake_rw.attr, |
| 5182 | &driver_attr_no_lun_0.attr, |
| 5183 | &driver_attr_num_tgts.attr, |
| 5184 | &driver_attr_dev_size_mb.attr, |
| 5185 | &driver_attr_num_parts.attr, |
| 5186 | &driver_attr_every_nth.attr, |
| 5187 | &driver_attr_max_luns.attr, |
| 5188 | &driver_attr_max_queue.attr, |
| 5189 | &driver_attr_no_uld.attr, |
| 5190 | &driver_attr_scsi_level.attr, |
| 5191 | &driver_attr_virtual_gb.attr, |
| 5192 | &driver_attr_add_host.attr, |
| 5193 | &driver_attr_vpd_use_hostno.attr, |
| 5194 | &driver_attr_sector_size.attr, |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5195 | &driver_attr_statistics.attr, |
| 5196 | &driver_attr_submit_queues.attr, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5197 | &driver_attr_dix.attr, |
| 5198 | &driver_attr_dif.attr, |
| 5199 | &driver_attr_guard.attr, |
| 5200 | &driver_attr_ato.attr, |
| 5201 | &driver_attr_map.attr, |
| 5202 | &driver_attr_removable.attr, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5203 | &driver_attr_host_lock.attr, |
| 5204 | &driver_attr_ndelay.attr, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5205 | &driver_attr_strict.attr, |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 5206 | &driver_attr_uuid_ctl.attr, |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 5207 | &driver_attr_cdb_len.attr, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5208 | NULL, |
| 5209 | }; |
| 5210 | ATTRIBUTE_GROUPS(sdebug_drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | |
Akinobu Mita | 11ddcec | 2014-02-26 22:56:59 +0900 | [diff] [blame] | 5212 | static struct device *pseudo_primary; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | static int __init scsi_debug_init(void) |
| 5215 | { |
FUJITA Tomonori | 5f2578e | 2008-03-30 00:59:57 +0900 | [diff] [blame] | 5216 | unsigned long sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | int host_to_add; |
| 5218 | int k; |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5219 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5221 | atomic_set(&retired_max_queue, 0); |
| 5222 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5223 | if (sdebug_ndelay >= 1000 * 1000 * 1000) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5224 | pr_warn("ndelay must be less than 1 second, ignored\n"); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5225 | sdebug_ndelay = 0; |
| 5226 | } else if (sdebug_ndelay > 0) |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 5227 | sdebug_jdelay = JDELAY_OVERRIDDEN; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5228 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5229 | switch (sdebug_sector_size) { |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 5230 | case 512: |
| 5231 | case 1024: |
| 5232 | case 2048: |
| 5233 | case 4096: |
| 5234 | break; |
| 5235 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5236 | pr_err("invalid sector_size %d\n", sdebug_sector_size); |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 5237 | return -EINVAL; |
| 5238 | } |
| 5239 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5240 | switch (sdebug_dif) { |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5241 | case T10_PI_TYPE0_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5242 | break; |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5243 | case T10_PI_TYPE1_PROTECTION: |
| 5244 | case T10_PI_TYPE2_PROTECTION: |
| 5245 | case T10_PI_TYPE3_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5246 | have_dif_prot = true; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5247 | break; |
| 5248 | |
| 5249 | default: |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5250 | pr_err("dif must be 0, 1, 2 or 3\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5251 | return -EINVAL; |
| 5252 | } |
| 5253 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5254 | if (sdebug_guard > 1) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5255 | pr_err("guard must be 0 or 1\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5256 | return -EINVAL; |
| 5257 | } |
| 5258 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5259 | if (sdebug_ato > 1) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5260 | pr_err("ato must be 0 or 1\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5261 | return -EINVAL; |
| 5262 | } |
| 5263 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5264 | if (sdebug_physblk_exp > 15) { |
| 5265 | pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 5266 | return -EINVAL; |
| 5267 | } |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 5268 | if (sdebug_max_luns > 256) { |
| 5269 | pr_warn("max_luns can be no more than 256, use default\n"); |
| 5270 | sdebug_max_luns = DEF_MAX_LUNS; |
| 5271 | } |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 5272 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5273 | if (sdebug_lowest_aligned > 0x3fff) { |
| 5274 | pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 5275 | return -EINVAL; |
| 5276 | } |
| 5277 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5278 | if (submit_queues < 1) { |
| 5279 | pr_err("submit_queues must be 1 or more\n"); |
| 5280 | return -EINVAL; |
| 5281 | } |
| 5282 | sdebug_q_arr = kcalloc(submit_queues, sizeof(struct sdebug_queue), |
| 5283 | GFP_KERNEL); |
| 5284 | if (sdebug_q_arr == NULL) |
| 5285 | return -ENOMEM; |
| 5286 | for (k = 0; k < submit_queues; ++k) |
| 5287 | spin_lock_init(&sdebug_q_arr[k].qc_lock); |
| 5288 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5289 | if (sdebug_dev_size_mb < 1) |
| 5290 | sdebug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */ |
| 5291 | sz = (unsigned long)sdebug_dev_size_mb * 1048576; |
| 5292 | sdebug_store_sectors = sz / sdebug_sector_size; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 5293 | sdebug_capacity = get_sdebug_capacity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | |
| 5295 | /* play around with geometry, don't waste too much on track 0 */ |
| 5296 | sdebug_heads = 8; |
| 5297 | sdebug_sectors_per = 32; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5298 | if (sdebug_dev_size_mb >= 256) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | sdebug_heads = 64; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5300 | else if (sdebug_dev_size_mb >= 16) |
Andy Shevchenko | fa785f0 | 2015-11-26 20:22:50 +0200 | [diff] [blame] | 5301 | sdebug_heads = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | sdebug_cylinders_per = (unsigned long)sdebug_capacity / |
| 5303 | (sdebug_sectors_per * sdebug_heads); |
| 5304 | if (sdebug_cylinders_per >= 1024) { |
| 5305 | /* other LLDs do this; implies >= 1GB ram disk ... */ |
| 5306 | sdebug_heads = 255; |
| 5307 | sdebug_sectors_per = 63; |
| 5308 | sdebug_cylinders_per = (unsigned long)sdebug_capacity / |
| 5309 | (sdebug_sectors_per * sdebug_heads); |
| 5310 | } |
| 5311 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 5312 | if (sdebug_fake_rw == 0) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5313 | fake_storep = vmalloc(sz); |
| 5314 | if (NULL == fake_storep) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5315 | pr_err("out of memory, 1\n"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5316 | ret = -ENOMEM; |
| 5317 | goto free_q_arr; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5318 | } |
| 5319 | memset(fake_storep, 0, sz); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5320 | if (sdebug_num_parts > 0) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5321 | sdebug_build_parts(fake_storep, sz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5323 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5324 | if (sdebug_dix) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5325 | int dif_size; |
| 5326 | |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 5327 | dif_size = sdebug_store_sectors * sizeof(struct t10_pi_tuple); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5328 | dif_storep = vmalloc(dif_size); |
| 5329 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5330 | 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] | 5331 | |
| 5332 | if (dif_storep == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5333 | pr_err("out of mem. (DIX)\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5334 | ret = -ENOMEM; |
| 5335 | goto free_vm; |
| 5336 | } |
| 5337 | |
| 5338 | memset(dif_storep, 0xff, dif_size); |
| 5339 | } |
| 5340 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 5341 | /* Logical Block Provisioning */ |
| 5342 | if (scsi_debug_lbp()) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5343 | sdebug_unmap_max_blocks = |
| 5344 | clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 5345 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5346 | sdebug_unmap_max_desc = |
| 5347 | clamp(sdebug_unmap_max_desc, 0U, 256U); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 5348 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5349 | sdebug_unmap_granularity = |
| 5350 | clamp(sdebug_unmap_granularity, 1U, 0xffffffffU); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 5351 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5352 | if (sdebug_unmap_alignment && |
| 5353 | sdebug_unmap_granularity <= |
| 5354 | sdebug_unmap_alignment) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5355 | pr_err("ERR: unmap_granularity <= unmap_alignment\n"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5356 | ret = -EINVAL; |
| 5357 | goto free_vm; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5358 | } |
| 5359 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 5360 | map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1; |
| 5361 | map_storep = vmalloc(BITS_TO_LONGS(map_size) * sizeof(long)); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5362 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5363 | pr_info("%lu provisioning blocks\n", map_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5364 | |
| 5365 | if (map_storep == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5366 | pr_err("out of mem. (MAP)\n"); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5367 | ret = -ENOMEM; |
| 5368 | goto free_vm; |
| 5369 | } |
| 5370 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 5371 | bitmap_zero(map_storep, map_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5372 | |
| 5373 | /* Map first 1KB for partition table */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5374 | if (sdebug_num_parts) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5375 | map_region(0, 2); |
| 5376 | } |
| 5377 | |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5378 | pseudo_primary = root_device_register("pseudo_0"); |
| 5379 | if (IS_ERR(pseudo_primary)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5380 | pr_warn("root_device_register() error\n"); |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5381 | ret = PTR_ERR(pseudo_primary); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5382 | goto free_vm; |
| 5383 | } |
| 5384 | ret = bus_register(&pseudo_lld_bus); |
| 5385 | if (ret < 0) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5386 | pr_warn("bus_register error: %d\n", ret); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5387 | goto dev_unreg; |
| 5388 | } |
| 5389 | ret = driver_register(&sdebug_driverfs_driver); |
| 5390 | if (ret < 0) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5391 | pr_warn("driver_register error: %d\n", ret); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5392 | goto bus_unreg; |
| 5393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5394 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5395 | host_to_add = sdebug_add_host; |
| 5396 | sdebug_add_host = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5398 | for (k = 0; k < host_to_add; k++) { |
| 5399 | if (sdebug_add_adapter()) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5400 | pr_err("sdebug_add_adapter failed k=%d\n", k); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5401 | break; |
| 5402 | } |
| 5403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5405 | if (sdebug_verbose) |
| 5406 | pr_info("built %d host(s)\n", sdebug_add_host); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | return 0; |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5409 | |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5410 | bus_unreg: |
| 5411 | bus_unregister(&pseudo_lld_bus); |
| 5412 | dev_unreg: |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5413 | root_device_unregister(pseudo_primary); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5414 | free_vm: |
Tomas Winkler | de232af | 2015-07-28 16:54:22 +0300 | [diff] [blame] | 5415 | vfree(map_storep); |
| 5416 | vfree(dif_storep); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5417 | vfree(fake_storep); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5418 | free_q_arr: |
| 5419 | kfree(sdebug_q_arr); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5420 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | } |
| 5422 | |
| 5423 | static void __exit scsi_debug_exit(void) |
| 5424 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5425 | int k = sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | |
| 5427 | stop_all_queued(); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5428 | free_all_queued(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | for (; k; k--) |
| 5430 | sdebug_remove_adapter(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | driver_unregister(&sdebug_driverfs_driver); |
| 5432 | bus_unregister(&pseudo_lld_bus); |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5433 | root_device_unregister(pseudo_primary); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | |
Ewan D. Milne | 4d2b496 | 2016-10-26 11:22:53 -0400 | [diff] [blame] | 5435 | vfree(map_storep); |
Tomas Winkler | de232af | 2015-07-28 16:54:22 +0300 | [diff] [blame] | 5436 | vfree(dif_storep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5437 | vfree(fake_storep); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5438 | kfree(sdebug_q_arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | } |
| 5440 | |
| 5441 | device_initcall(scsi_debug_init); |
| 5442 | module_exit(scsi_debug_exit); |
| 5443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | static void sdebug_release_adapter(struct device * dev) |
| 5445 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5446 | struct sdebug_host_info *sdbg_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5447 | |
| 5448 | sdbg_host = to_sdebug_host(dev); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5449 | kfree(sdbg_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | static int sdebug_add_adapter(void) |
| 5453 | { |
| 5454 | int k, devs_per_host; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5455 | int error = 0; |
| 5456 | struct sdebug_host_info *sdbg_host; |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5457 | struct sdebug_dev_info *sdbg_devinfo, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5458 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5459 | sdbg_host = kzalloc(sizeof(*sdbg_host), GFP_KERNEL); |
| 5460 | if (sdbg_host == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5461 | pr_err("out of memory at line %d\n", __LINE__); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5462 | return -ENOMEM; |
| 5463 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5465 | INIT_LIST_HEAD(&sdbg_host->dev_info_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5466 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5467 | devs_per_host = sdebug_num_tgts * sdebug_max_luns; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5468 | for (k = 0; k < devs_per_host; k++) { |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 5469 | sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL); |
| 5470 | if (!sdbg_devinfo) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5471 | pr_err("out of memory at line %d\n", __LINE__); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5472 | error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | goto clean; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5474 | } |
| 5475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5477 | spin_lock(&sdebug_host_list_lock); |
| 5478 | list_add_tail(&sdbg_host->host_list, &sdebug_host_list); |
| 5479 | spin_unlock(&sdebug_host_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5480 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5481 | sdbg_host->dev.bus = &pseudo_lld_bus; |
| 5482 | sdbg_host->dev.parent = pseudo_primary; |
| 5483 | sdbg_host->dev.release = &sdebug_release_adapter; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5484 | dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5486 | error = device_register(&sdbg_host->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5488 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 | goto clean; |
| 5490 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5491 | ++sdebug_add_host; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5492 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | |
| 5494 | clean: |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5495 | list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, |
| 5496 | dev_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | list_del(&sdbg_devinfo->dev_list); |
| 5498 | kfree(sdbg_devinfo); |
| 5499 | } |
| 5500 | |
| 5501 | kfree(sdbg_host); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5502 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | } |
| 5504 | |
| 5505 | static void sdebug_remove_adapter(void) |
| 5506 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5507 | struct sdebug_host_info *sdbg_host = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5509 | spin_lock(&sdebug_host_list_lock); |
| 5510 | if (!list_empty(&sdebug_host_list)) { |
| 5511 | sdbg_host = list_entry(sdebug_host_list.prev, |
| 5512 | struct sdebug_host_info, host_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | list_del(&sdbg_host->host_list); |
| 5514 | } |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5515 | spin_unlock(&sdebug_host_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5516 | |
| 5517 | if (!sdbg_host) |
| 5518 | return; |
| 5519 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5520 | device_unregister(&sdbg_host->dev); |
| 5521 | --sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5522 | } |
| 5523 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 5524 | static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5525 | { |
| 5526 | int num_in_q = 0; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5527 | struct sdebug_dev_info *devip; |
| 5528 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5529 | block_unblock_all_queues(true); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5530 | devip = (struct sdebug_dev_info *)sdev->hostdata; |
| 5531 | if (NULL == devip) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5532 | block_unblock_all_queues(false); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5533 | return -ENODEV; |
| 5534 | } |
| 5535 | num_in_q = atomic_read(&devip->num_in_q); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5536 | |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5537 | if (qdepth < 1) |
| 5538 | qdepth = 1; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5539 | /* allow to exceed max host qc_arr elements for testing */ |
| 5540 | if (qdepth > SDEBUG_CANQUEUE + 10) |
| 5541 | qdepth = SDEBUG_CANQUEUE + 10; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 5542 | scsi_change_queue_depth(sdev, qdepth); |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5543 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5544 | if (SDEBUG_OPT_Q_NOISE & sdebug_opts) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5545 | 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] | 5546 | __func__, qdepth, num_in_q); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5547 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5548 | block_unblock_all_queues(false); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5549 | return sdev->queue_depth; |
| 5550 | } |
| 5551 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5552 | static bool fake_timeout(struct scsi_cmnd *scp) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5553 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5554 | if (0 == (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5555 | if (sdebug_every_nth < -1) |
| 5556 | sdebug_every_nth = -1; |
| 5557 | if (SDEBUG_OPT_TIMEOUT & sdebug_opts) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5558 | return true; /* ignore command causing timeout */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5559 | else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts && |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5560 | scsi_medium_access_command(scp)) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5561 | return true; /* time out reads and writes */ |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5562 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5563 | return false; |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5564 | } |
| 5565 | |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 5566 | static bool fake_host_busy(struct scsi_cmnd *scp) |
| 5567 | { |
| 5568 | return (sdebug_opts & SDEBUG_OPT_HOST_BUSY) && |
| 5569 | (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) == 0; |
| 5570 | } |
| 5571 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 5572 | static int scsi_debug_queuecommand(struct Scsi_Host *shost, |
| 5573 | struct scsi_cmnd *scp) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5574 | { |
| 5575 | u8 sdeb_i; |
| 5576 | struct scsi_device *sdp = scp->device; |
| 5577 | const struct opcode_info_t *oip; |
| 5578 | const struct opcode_info_t *r_oip; |
| 5579 | struct sdebug_dev_info *devip; |
| 5580 | u8 *cmd = scp->cmnd; |
| 5581 | int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 5582 | int k, na; |
| 5583 | int errsts = 0; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5584 | u32 flags; |
| 5585 | u16 sa; |
| 5586 | u8 opcode = cmd[0]; |
| 5587 | bool has_wlun_rl; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5588 | |
| 5589 | scsi_set_resid(scp, 0); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5590 | if (sdebug_statistics) |
| 5591 | atomic_inc(&sdebug_cmnd_count); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5592 | if (unlikely(sdebug_verbose && |
| 5593 | !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5594 | char b[120]; |
| 5595 | int n, len, sb; |
| 5596 | |
| 5597 | len = scp->cmd_len; |
| 5598 | sb = (int)sizeof(b); |
| 5599 | if (len > 32) |
| 5600 | strcpy(b, "too long, over 32 bytes"); |
| 5601 | else { |
| 5602 | for (k = 0, n = 0; k < len && n < sb; ++k) |
| 5603 | n += scnprintf(b + n, sb - n, "%02x ", |
| 5604 | (u32)cmd[k]); |
| 5605 | } |
Bart Van Assche | 458df78 | 2018-01-26 08:52:19 -0800 | [diff] [blame^] | 5606 | sdev_printk(KERN_INFO, sdp, "%s: tag=%#x, cmd %s\n", my_name, |
| 5607 | blk_mq_unique_tag(scp->request), b); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5608 | } |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 5609 | if (fake_host_busy(scp)) |
| 5610 | return SCSI_MLQUEUE_HOST_BUSY; |
Tomas Winkler | 34d5543 | 2015-07-28 16:54:21 +0300 | [diff] [blame] | 5611 | has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5612 | if (unlikely((sdp->lun >= sdebug_max_luns) && !has_wlun_rl)) |
| 5613 | goto err_out; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5614 | |
| 5615 | sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */ |
| 5616 | oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */ |
| 5617 | devip = (struct sdebug_dev_info *)sdp->hostdata; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5618 | if (unlikely(!devip)) { |
| 5619 | devip = find_build_dev_info(sdp); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5620 | if (NULL == devip) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5621 | goto err_out; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5622 | } |
| 5623 | na = oip->num_attached; |
| 5624 | r_pfp = oip->pfp; |
| 5625 | if (na) { /* multiple commands with this opcode */ |
| 5626 | r_oip = oip; |
| 5627 | if (FF_SA & r_oip->flags) { |
| 5628 | if (F_SA_LOW & oip->flags) |
| 5629 | sa = 0x1f & cmd[1]; |
| 5630 | else |
| 5631 | sa = get_unaligned_be16(cmd + 8); |
| 5632 | for (k = 0; k <= na; oip = r_oip->arrp + k++) { |
| 5633 | if (opcode == oip->opcode && sa == oip->sa) |
| 5634 | break; |
| 5635 | } |
| 5636 | } else { /* since no service action only check opcode */ |
| 5637 | for (k = 0; k <= na; oip = r_oip->arrp + k++) { |
| 5638 | if (opcode == oip->opcode) |
| 5639 | break; |
| 5640 | } |
| 5641 | } |
| 5642 | if (k > na) { |
| 5643 | if (F_SA_LOW & r_oip->flags) |
| 5644 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4); |
| 5645 | else if (F_SA_HIGH & r_oip->flags) |
| 5646 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7); |
| 5647 | else |
| 5648 | mk_sense_invalid_opcode(scp); |
| 5649 | goto check_cond; |
| 5650 | } |
| 5651 | } /* else (when na==0) we assume the oip is a match */ |
| 5652 | flags = oip->flags; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5653 | if (unlikely(F_INV_OP & flags)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5654 | mk_sense_invalid_opcode(scp); |
| 5655 | goto check_cond; |
| 5656 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5657 | if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5658 | if (sdebug_verbose) |
| 5659 | sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n", |
| 5660 | my_name, opcode, " supported for wlun"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5661 | mk_sense_invalid_opcode(scp); |
| 5662 | goto check_cond; |
| 5663 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5664 | if (unlikely(sdebug_strict)) { /* check cdb against mask */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5665 | u8 rem; |
| 5666 | int j; |
| 5667 | |
| 5668 | for (k = 1; k < oip->len_mask[0] && k < 16; ++k) { |
| 5669 | rem = ~oip->len_mask[k] & cmd[k]; |
| 5670 | if (rem) { |
| 5671 | for (j = 7; j >= 0; --j, rem <<= 1) { |
| 5672 | if (0x80 & rem) |
| 5673 | break; |
| 5674 | } |
| 5675 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j); |
| 5676 | goto check_cond; |
| 5677 | } |
| 5678 | } |
| 5679 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5680 | if (unlikely(!(F_SKIP_UA & flags) && |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 5681 | find_first_bit(devip->uas_bm, |
| 5682 | SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5683 | errsts = make_ua(scp, devip); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5684 | if (errsts) |
| 5685 | goto check_cond; |
| 5686 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5687 | if (unlikely((F_M_ACCESS & flags) && atomic_read(&devip->stopped))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5688 | mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5689 | if (sdebug_verbose) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5690 | sdev_printk(KERN_INFO, sdp, "%s reports: Not ready: " |
| 5691 | "%s\n", my_name, "initializing command " |
| 5692 | "required"); |
| 5693 | errsts = check_condition_result; |
| 5694 | goto fini; |
| 5695 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5696 | if (sdebug_fake_rw && (F_FAKE_RW & flags)) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5697 | goto fini; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5698 | if (unlikely(sdebug_every_nth)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5699 | if (fake_timeout(scp)) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5700 | return 0; /* ignore command: make trouble */ |
| 5701 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5702 | if (likely(oip->pfp)) |
| 5703 | errsts = oip->pfp(scp, devip); /* calls a resp_* function */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5704 | else if (r_pfp) /* if leaf function ptr NULL, try the root's */ |
| 5705 | errsts = r_pfp(scp, devip); |
| 5706 | |
| 5707 | fini: |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 5708 | if (F_DELAY_OVERR & flags) |
| 5709 | return schedule_resp(scp, devip, errsts, 0, 0); |
| 5710 | else |
| 5711 | return schedule_resp(scp, devip, errsts, sdebug_jdelay, |
| 5712 | sdebug_ndelay); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5713 | check_cond: |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 5714 | return schedule_resp(scp, devip, check_condition_result, 0, 0); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5715 | err_out: |
Douglas Gilbert | 10bde98 | 2018-01-10 16:57:31 -0500 | [diff] [blame] | 5716 | return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, 0, 0); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5717 | } |
| 5718 | |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5719 | static struct scsi_host_template sdebug_driver_template = { |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 5720 | .show_info = scsi_debug_show_info, |
| 5721 | .write_info = scsi_debug_write_info, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5722 | .proc_name = sdebug_proc_name, |
| 5723 | .name = "SCSI DEBUG", |
| 5724 | .info = scsi_debug_info, |
| 5725 | .slave_alloc = scsi_debug_slave_alloc, |
| 5726 | .slave_configure = scsi_debug_slave_configure, |
| 5727 | .slave_destroy = scsi_debug_slave_destroy, |
| 5728 | .ioctl = scsi_debug_ioctl, |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 5729 | .queuecommand = scsi_debug_queuecommand, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5730 | .change_queue_depth = sdebug_change_qdepth, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5731 | .eh_abort_handler = scsi_debug_abort, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5732 | .eh_device_reset_handler = scsi_debug_device_reset, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5733 | .eh_target_reset_handler = scsi_debug_target_reset, |
| 5734 | .eh_bus_reset_handler = scsi_debug_bus_reset, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5735 | .eh_host_reset_handler = scsi_debug_host_reset, |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5736 | .can_queue = SDEBUG_CANQUEUE, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5737 | .this_id = 7, |
Ming Lin | 65e8617 | 2016-04-04 14:48:10 -0700 | [diff] [blame] | 5738 | .sg_tablesize = SG_MAX_SEGMENTS, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5739 | .cmd_per_lun = DEF_CMD_PER_LUN, |
Akinobu Mita | 6bb5e6e | 2014-06-02 22:56:49 +0900 | [diff] [blame] | 5740 | .max_sectors = -1U, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5741 | .use_clustering = DISABLE_CLUSTERING, |
| 5742 | .module = THIS_MODULE, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5743 | .track_queue_depth = 1, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5744 | }; |
| 5745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | static int sdebug_driver_probe(struct device * dev) |
| 5747 | { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 5748 | int error = 0; |
| 5749 | struct sdebug_host_info *sdbg_host; |
| 5750 | struct Scsi_Host *hpnt; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5751 | int hprot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5752 | |
| 5753 | sdbg_host = to_sdebug_host(dev); |
| 5754 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5755 | sdebug_driver_template.can_queue = sdebug_max_queue; |
| 5756 | if (sdebug_clustering) |
Akinobu Mita | 0759c66 | 2014-02-26 22:57:04 +0900 | [diff] [blame] | 5757 | sdebug_driver_template.use_clustering = ENABLE_CLUSTERING; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5758 | hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host)); |
| 5759 | if (NULL == hpnt) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5760 | pr_err("scsi_host_alloc failed\n"); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5761 | error = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | return error; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5763 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5764 | if (submit_queues > nr_cpu_ids) { |
Alexey Dobriyan | 9b130ad | 2017-09-08 16:14:18 -0700 | [diff] [blame] | 5765 | 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] | 5766 | my_name, submit_queues, nr_cpu_ids); |
| 5767 | submit_queues = nr_cpu_ids; |
| 5768 | } |
| 5769 | /* Decide whether to tell scsi subsystem that we want mq */ |
| 5770 | /* Following should give the same answer for each host */ |
Bart Van Assche | 458df78 | 2018-01-26 08:52:19 -0800 | [diff] [blame^] | 5771 | if (shost_use_blk_mq(hpnt)) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5772 | hpnt->nr_hw_queues = submit_queues; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5774 | sdbg_host->shost = hpnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5775 | *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5776 | if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id)) |
| 5777 | hpnt->max_id = sdebug_num_tgts + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5778 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5779 | hpnt->max_id = sdebug_num_tgts; |
| 5780 | /* = sdebug_max_luns; */ |
Tomas Winkler | f2d3fd2 | 2015-07-28 16:54:25 +0300 | [diff] [blame] | 5781 | hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5782 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5783 | hprot = 0; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5784 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5785 | switch (sdebug_dif) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5786 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5787 | case T10_PI_TYPE1_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5788 | hprot = SHOST_DIF_TYPE1_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5789 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5790 | hprot |= SHOST_DIX_TYPE1_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5791 | break; |
| 5792 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5793 | case T10_PI_TYPE2_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5794 | hprot = SHOST_DIF_TYPE2_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5795 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5796 | hprot |= SHOST_DIX_TYPE2_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5797 | break; |
| 5798 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5799 | case T10_PI_TYPE3_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5800 | hprot = SHOST_DIF_TYPE3_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5801 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5802 | hprot |= SHOST_DIX_TYPE3_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5803 | break; |
| 5804 | |
| 5805 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5806 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5807 | hprot |= SHOST_DIX_TYPE0_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5808 | break; |
| 5809 | } |
| 5810 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5811 | scsi_host_set_prot(hpnt, hprot); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5812 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5813 | if (have_dif_prot || sdebug_dix) |
| 5814 | pr_info("host protection%s%s%s%s%s%s%s\n", |
| 5815 | (hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "", |
| 5816 | (hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "", |
| 5817 | (hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "", |
| 5818 | (hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "", |
| 5819 | (hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "", |
| 5820 | (hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "", |
| 5821 | (hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : ""); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5822 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5823 | if (sdebug_guard == 1) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5824 | scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP); |
| 5825 | else |
| 5826 | scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC); |
| 5827 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5828 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts); |
| 5829 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & sdebug_opts); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5830 | if (sdebug_every_nth) /* need stats counters for every_nth */ |
| 5831 | sdebug_statistics = true; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5832 | error = scsi_add_host(hpnt, &sdbg_host->dev); |
| 5833 | if (error) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5834 | pr_err("scsi_add_host failed\n"); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5835 | error = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5836 | scsi_host_put(hpnt); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5837 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 | scsi_scan_host(hpnt); |
| 5839 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5840 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5841 | } |
| 5842 | |
| 5843 | static int sdebug_driver_remove(struct device * dev) |
| 5844 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5845 | struct sdebug_host_info *sdbg_host; |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5846 | struct sdebug_dev_info *sdbg_devinfo, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5847 | |
| 5848 | sdbg_host = to_sdebug_host(dev); |
| 5849 | |
| 5850 | if (!sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5851 | pr_err("Unable to locate host info\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | return -ENODEV; |
| 5853 | } |
| 5854 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5855 | scsi_remove_host(sdbg_host->shost); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5857 | list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, |
| 5858 | dev_list) { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5859 | list_del(&sdbg_devinfo->dev_list); |
| 5860 | kfree(sdbg_devinfo); |
| 5861 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5863 | scsi_host_put(sdbg_host->shost); |
| 5864 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5865 | } |
| 5866 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5867 | static int pseudo_lld_bus_match(struct device *dev, |
| 5868 | struct device_driver *dev_driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | { |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5870 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5871 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5872 | |
| 5873 | static struct bus_type pseudo_lld_bus = { |
| 5874 | .name = "pseudo", |
| 5875 | .match = pseudo_lld_bus_match, |
| 5876 | .probe = sdebug_driver_probe, |
| 5877 | .remove = sdebug_driver_remove, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5878 | .drv_groups = sdebug_drv_groups, |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5879 | }; |