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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 97 | /* Additional Sense Code Qualifier (ASCQ) */ |
| 98 | #define ACK_NAK_TO 0x3 |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | /* Default values for driver parameters */ |
| 101 | #define DEF_NUM_HOST 1 |
| 102 | #define DEF_NUM_TGTS 1 |
| 103 | #define DEF_MAX_LUNS 1 |
| 104 | /* With these defaults, this driver will make 1 host with 1 target |
| 105 | * (id 0) containing 1 logical unit (lun 0). That is 1 device. |
| 106 | */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 107 | #define DEF_ATO 1 |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 108 | #define DEF_CDB_LEN 10 |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 109 | #define DEF_JDELAY 1 /* if > 0 unit is a jiffy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | #define DEF_DEV_SIZE_MB 8 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 111 | #define DEF_DIF 0 |
| 112 | #define DEF_DIX 0 |
| 113 | #define DEF_D_SENSE 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | #define DEF_EVERY_NTH 0 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 115 | #define DEF_FAKE_RW 0 |
| 116 | #define DEF_GUARD 0 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 117 | #define DEF_HOST_LOCK 0 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 118 | #define DEF_LBPU 0 |
| 119 | #define DEF_LBPWS 0 |
| 120 | #define DEF_LBPWS10 0 |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 121 | #define DEF_LBPRZ 1 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 122 | #define DEF_LOWEST_ALIGNED 0 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 123 | #define DEF_NDELAY 0 /* if > 0 unit is a nanosecond */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 124 | #define DEF_NO_LUN_0 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | #define DEF_NUM_PARTS 0 |
| 126 | #define DEF_OPTS 0 |
Martin K. Petersen | 32c5844 | 2015-12-16 17:53:51 -0500 | [diff] [blame] | 127 | #define DEF_OPT_BLKS 1024 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 128 | #define DEF_PHYSBLK_EXP 0 |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 129 | #define DEF_OPT_XFERLEN_EXP 0 |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 130 | #define DEF_PTYPE TYPE_DISK |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 131 | #define DEF_REMOVABLE false |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 132 | #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] | 133 | #define DEF_SECTOR_SIZE 512 |
| 134 | #define DEF_UNMAP_ALIGNMENT 0 |
| 135 | #define DEF_UNMAP_GRANULARITY 1 |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 136 | #define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF |
| 137 | #define DEF_UNMAP_MAX_DESC 256 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 138 | #define DEF_VIRTUAL_GB 0 |
| 139 | #define DEF_VPD_USE_HOSTNO 1 |
| 140 | #define DEF_WRITESAME_LENGTH 0xFFFF |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 141 | #define DEF_STRICT 0 |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 142 | #define DEF_STATISTICS false |
| 143 | #define DEF_SUBMIT_QUEUES 1 |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 144 | #define DEF_UUID_CTL 0 |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 145 | #define JDELAY_OVERRIDDEN -9999 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 147 | #define SDEBUG_LUN_0_VAL 0 |
| 148 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 149 | /* bit mask values for sdebug_opts */ |
| 150 | #define SDEBUG_OPT_NOISE 1 |
| 151 | #define SDEBUG_OPT_MEDIUM_ERR 2 |
| 152 | #define SDEBUG_OPT_TIMEOUT 4 |
| 153 | #define SDEBUG_OPT_RECOVERED_ERR 8 |
| 154 | #define SDEBUG_OPT_TRANSPORT_ERR 16 |
| 155 | #define SDEBUG_OPT_DIF_ERR 32 |
| 156 | #define SDEBUG_OPT_DIX_ERR 64 |
| 157 | #define SDEBUG_OPT_MAC_TIMEOUT 128 |
| 158 | #define SDEBUG_OPT_SHORT_TRANSFER 0x100 |
| 159 | #define SDEBUG_OPT_Q_NOISE 0x200 |
| 160 | #define SDEBUG_OPT_ALL_TSF 0x400 |
| 161 | #define SDEBUG_OPT_RARE_TSF 0x800 |
| 162 | #define SDEBUG_OPT_N_WCE 0x1000 |
| 163 | #define SDEBUG_OPT_RESET_NOISE 0x2000 |
| 164 | #define SDEBUG_OPT_NO_CDB_NOISE 0x4000 |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 165 | #define SDEBUG_OPT_HOST_BUSY 0x8000 |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 166 | #define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \ |
| 167 | SDEBUG_OPT_RESET_NOISE) |
| 168 | #define SDEBUG_OPT_ALL_INJECTING (SDEBUG_OPT_RECOVERED_ERR | \ |
| 169 | SDEBUG_OPT_TRANSPORT_ERR | \ |
| 170 | SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR | \ |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 171 | SDEBUG_OPT_SHORT_TRANSFER | \ |
| 172 | SDEBUG_OPT_HOST_BUSY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | /* When "every_nth" > 0 then modulo "every_nth" commands: |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 174 | * - a missing response is simulated if SDEBUG_OPT_TIMEOUT is set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | * - a RECOVERED_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 176 | * commands if SDEBUG_OPT_RECOVERED_ERR is set. |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 177 | * - a TRANSPORT_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 178 | * commands if SDEBUG_OPT_TRANSPORT_ERR is set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | * |
| 180 | * When "every_nth" < 0 then after "- every_nth" commands: |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 181 | * - a missing response is simulated if SDEBUG_OPT_TIMEOUT is set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | * - a RECOVERED_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 183 | * commands if SDEBUG_OPT_RECOVERED_ERR is set. |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 184 | * - a TRANSPORT_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 185 | * commands if _DEBUG_OPT_TRANSPORT_ERR is set. |
| 186 | * This will continue on every subsequent command until some other action |
| 187 | * occurs (e.g. the user * writing a new value (other than -1 or 1) to |
| 188 | * every_nth via sysfs). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | */ |
| 190 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 191 | /* 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] | 192 | * priority order. In the subset implemented here lower numbers have higher |
| 193 | * priority. The UA numbers should be a sequence starting from 0 with |
| 194 | * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */ |
| 195 | #define SDEBUG_UA_POR 0 /* Power on, reset, or bus device reset */ |
| 196 | #define SDEBUG_UA_BUS_RESET 1 |
| 197 | #define SDEBUG_UA_MODE_CHANGED 2 |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 198 | #define SDEBUG_UA_CAPACITY_CHANGED 3 |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 199 | #define SDEBUG_UA_LUNS_CHANGED 4 |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 200 | #define SDEBUG_UA_MICROCODE_CHANGED 5 /* simulate firmware change */ |
| 201 | #define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 6 |
| 202 | #define SDEBUG_NUM_UAS 7 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 203 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 204 | /* 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] | 205 | * sector on read commands: */ |
| 206 | #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */ |
Douglas Gilbert | 32f7ef7 | 2011-03-11 10:43:35 -0500 | [diff] [blame] | 207 | #define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1) |
| 210 | * or "peripheral device" addressing (value 0) */ |
| 211 | #define SAM2_LUN_ADDRESS_METHOD 0 |
| 212 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 213 | /* SDEBUG_CANQUEUE is the maximum number of commands that can be queued |
| 214 | * (for response) per submit queue at one time. Can be reduced by max_queue |
| 215 | * option. Command responses are not queued when jdelay=0 and ndelay=0. The |
| 216 | * per-device DEF_CMD_PER_LUN can be changed via sysfs: |
| 217 | * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth |
| 218 | * but cannot exceed SDEBUG_CANQUEUE . |
| 219 | */ |
| 220 | #define SDEBUG_CANQUEUE_WORDS 3 /* a WORD is bits in a long */ |
| 221 | #define SDEBUG_CANQUEUE (SDEBUG_CANQUEUE_WORDS * BITS_PER_LONG) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 222 | #define DEF_CMD_PER_LUN 255 |
| 223 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 224 | #define F_D_IN 1 |
| 225 | #define F_D_OUT 2 |
| 226 | #define F_D_OUT_MAYBE 4 /* WRITE SAME, NDOB bit */ |
| 227 | #define F_D_UNKN 8 |
| 228 | #define F_RL_WLUN_OK 0x10 |
| 229 | #define F_SKIP_UA 0x20 |
| 230 | #define F_DELAY_OVERR 0x40 |
| 231 | #define F_SA_LOW 0x80 /* cdb byte 1, bits 4 to 0 */ |
| 232 | #define F_SA_HIGH 0x100 /* as used by variable length cdbs */ |
| 233 | #define F_INV_OP 0x200 |
| 234 | #define F_FAKE_RW 0x400 |
| 235 | #define F_M_ACCESS 0x800 /* media access */ |
| 236 | |
| 237 | #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^] | 238 | #define FF_MEDIA_IO (F_M_ACCESS | F_FAKE_RW) |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 239 | #define FF_SA (F_SA_HIGH | F_SA_LOW) |
| 240 | |
| 241 | #define SDEBUG_MAX_PARTS 4 |
| 242 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 243 | #define SDEBUG_MAX_CMD_LEN 32 |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 244 | |
| 245 | |
| 246 | struct sdebug_dev_info { |
| 247 | struct list_head dev_list; |
| 248 | unsigned int channel; |
| 249 | unsigned int target; |
| 250 | u64 lun; |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 251 | uuid_t lu_name; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 252 | struct sdebug_host_info *sdbg_host; |
| 253 | unsigned long uas_bm[1]; |
| 254 | atomic_t num_in_q; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 255 | atomic_t stopped; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 256 | bool used; |
| 257 | }; |
| 258 | |
| 259 | struct sdebug_host_info { |
| 260 | struct list_head host_list; |
| 261 | struct Scsi_Host *shost; |
| 262 | struct device dev; |
| 263 | struct list_head dev_info_list; |
| 264 | }; |
| 265 | |
| 266 | #define to_sdebug_host(d) \ |
| 267 | container_of(d, struct sdebug_host_info, dev) |
| 268 | |
| 269 | struct sdebug_defer { |
| 270 | struct hrtimer hrt; |
| 271 | struct execute_work ew; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 272 | int sqa_idx; /* index of sdebug_queue array */ |
| 273 | int qc_idx; /* index of sdebug_queued_cmd array within sqa_idx */ |
| 274 | int issuing_cpu; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | struct sdebug_queued_cmd { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 278 | /* corresponding bit set in in_use_bm[] in owning struct sdebug_queue |
| 279 | * instance indicates this slot is in use. |
| 280 | */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 281 | struct sdebug_defer *sd_dp; |
| 282 | struct scsi_cmnd *a_cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 283 | unsigned int inj_recovered:1; |
| 284 | unsigned int inj_transport:1; |
| 285 | unsigned int inj_dif:1; |
| 286 | unsigned int inj_dix:1; |
| 287 | unsigned int inj_short:1; |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 288 | unsigned int inj_host_busy:1; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 289 | }; |
| 290 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 291 | struct sdebug_queue { |
| 292 | struct sdebug_queued_cmd qc_arr[SDEBUG_CANQUEUE]; |
| 293 | unsigned long in_use_bm[SDEBUG_CANQUEUE_WORDS]; |
| 294 | spinlock_t qc_lock; |
| 295 | atomic_t blocked; /* to temporarily stop more being queued */ |
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 | static atomic_t sdebug_cmnd_count; /* number of incoming commands */ |
| 299 | static atomic_t sdebug_completions; /* count of deferred completions */ |
| 300 | static atomic_t sdebug_miss_cpus; /* submission + completion cpus differ */ |
| 301 | static atomic_t sdebug_a_tsf; /* 'almost task set full' counter */ |
| 302 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 303 | struct opcode_info_t { |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 304 | u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff */ |
| 305 | /* for terminating element */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 306 | u8 opcode; /* if num_attached > 0, preferred */ |
| 307 | u16 sa; /* service action */ |
| 308 | u32 flags; /* OR-ed set of SDEB_F_* */ |
| 309 | int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 310 | const struct opcode_info_t *arrp; /* num_attached elements or NULL */ |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 311 | u8 len_mask[16]; /* len_mask[0]-->cdb_len, then mask for cdb */ |
| 312 | /* 1 to min(cdb_len, 15); ignore cdb[15...] */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 313 | }; |
| 314 | |
| 315 | /* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 316 | enum sdeb_opcode_index { |
| 317 | SDEB_I_INVALID_OPCODE = 0, |
| 318 | SDEB_I_INQUIRY = 1, |
| 319 | SDEB_I_REPORT_LUNS = 2, |
| 320 | SDEB_I_REQUEST_SENSE = 3, |
| 321 | SDEB_I_TEST_UNIT_READY = 4, |
| 322 | SDEB_I_MODE_SENSE = 5, /* 6, 10 */ |
| 323 | SDEB_I_MODE_SELECT = 6, /* 6, 10 */ |
| 324 | SDEB_I_LOG_SENSE = 7, |
| 325 | SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */ |
| 326 | SDEB_I_READ = 9, /* 6, 10, 12, 16 */ |
| 327 | SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */ |
| 328 | SDEB_I_START_STOP = 11, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 329 | SDEB_I_SERV_ACT_IN_16 = 12, /* add ...SERV_ACT_IN_12 if needed */ |
| 330 | 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] | 331 | SDEB_I_MAINT_IN = 14, |
| 332 | SDEB_I_MAINT_OUT = 15, |
| 333 | SDEB_I_VERIFY = 16, /* 10 only */ |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 334 | SDEB_I_VARIABLE_LEN = 17, /* READ(32), WRITE(32) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 335 | SDEB_I_RESERVE = 18, /* 6, 10 */ |
| 336 | SDEB_I_RELEASE = 19, /* 6, 10 */ |
| 337 | SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */ |
| 338 | SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */ |
| 339 | SDEB_I_ATA_PT = 22, /* 12, 16 */ |
| 340 | SDEB_I_SEND_DIAG = 23, |
| 341 | SDEB_I_UNMAP = 24, |
| 342 | SDEB_I_XDWRITEREAD = 25, /* 10 only */ |
| 343 | SDEB_I_WRITE_BUFFER = 26, |
| 344 | SDEB_I_WRITE_SAME = 27, /* 10, 16 */ |
| 345 | SDEB_I_SYNC_CACHE = 28, /* 10 only */ |
| 346 | SDEB_I_COMP_WRITE = 29, |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 347 | SDEB_I_LAST_ELEMENT = 30, /* keep this last (previous + 1) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 348 | }; |
| 349 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 350 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 351 | static const unsigned char opcode_ind_arr[256] = { |
| 352 | /* 0x0; 0x0->0x1f: 6 byte cdbs */ |
| 353 | SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE, |
| 354 | 0, 0, 0, 0, |
| 355 | SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0, |
| 356 | 0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE, |
| 357 | SDEB_I_RELEASE, |
| 358 | 0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG, |
| 359 | SDEB_I_ALLOW_REMOVAL, 0, |
| 360 | /* 0x20; 0x20->0x3f: 10 byte cdbs */ |
| 361 | 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0, |
| 362 | SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY, |
| 363 | 0, 0, 0, 0, 0, SDEB_I_SYNC_CACHE, 0, 0, |
| 364 | 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0, |
| 365 | /* 0x40; 0x40->0x5f: 10 byte cdbs */ |
| 366 | 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0, |
| 367 | 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0, |
| 368 | 0, 0, 0, SDEB_I_XDWRITEREAD, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE, |
| 369 | SDEB_I_RELEASE, |
| 370 | 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0, |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 371 | /* 0x60; 0x60->0x7d are reserved, 0x7e is "extended cdb" */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 372 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 373 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 374 | 0, SDEB_I_VARIABLE_LEN, |
| 375 | /* 0x80; 0x80->0x9f: 16 byte cdbs */ |
| 376 | 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0, |
| 377 | SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0, 0, 0, 0, 0, |
| 378 | 0, 0, 0, SDEB_I_WRITE_SAME, 0, 0, 0, 0, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 379 | 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] | 380 | /* 0xa0; 0xa0->0xbf: 12 byte cdbs */ |
| 381 | SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN, |
| 382 | SDEB_I_MAINT_OUT, 0, 0, 0, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 383 | SDEB_I_READ, 0 /* SDEB_I_SERV_ACT_OUT_12 */, SDEB_I_WRITE, |
| 384 | 0 /* SDEB_I_SERV_ACT_IN_12 */, 0, 0, 0, 0, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 385 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 386 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 387 | /* 0xc0; 0xc0->0xff: vendor specific */ |
| 388 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 389 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 390 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 391 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 392 | }; |
| 393 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 394 | static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 395 | static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 396 | static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 397 | static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 398 | static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 399 | static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 400 | static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 401 | static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 402 | static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 403 | static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 404 | static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 405 | static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 406 | static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 407 | static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 408 | static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 409 | static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 410 | static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 411 | static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 412 | static int resp_xdwriteread_10(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 413 | 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] | 414 | static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 415 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 416 | /* |
| 417 | * The following are overflow arrays for cdbs that "hit" the same index in |
| 418 | * the opcode_info_arr array. The most time sensitive (or commonly used) cdb |
| 419 | * should be placed in opcode_info_arr[], the others should be placed here. |
| 420 | */ |
| 421 | static const struct opcode_info_t msense_iarr[] = { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 422 | {0, 0x1a, 0, F_D_IN, NULL, NULL, |
| 423 | {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 424 | }; |
| 425 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 426 | static const struct opcode_info_t mselect_iarr[] = { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 427 | {0, 0x15, 0, F_D_OUT, NULL, NULL, |
| 428 | {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 429 | }; |
| 430 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 431 | static const struct opcode_info_t read_iarr[] = { |
| 432 | {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] | 433 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 434 | 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 435 | {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] | 436 | {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^] | 437 | {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] | 438 | {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 439 | 0xc7, 0, 0, 0, 0} }, |
| 440 | }; |
| 441 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 442 | static const struct opcode_info_t write_iarr[] = { |
| 443 | {0, 0x2a, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(10) */ |
| 444 | NULL, {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, |
| 445 | 0, 0, 0, 0, 0, 0} }, |
| 446 | {0, 0xa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(6) */ |
| 447 | NULL, {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, |
| 448 | 0, 0, 0} }, |
| 449 | {0, 0xaa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(12) */ |
| 450 | NULL, {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 451 | 0xbf, 0xc7, 0, 0, 0, 0} }, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 452 | }; |
| 453 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 454 | static const struct opcode_info_t sa_in_16_iarr[] = { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 455 | {0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL, |
| 456 | {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 457 | 0xff, 0xff, 0xff, 0, 0xc7} }, /* GET LBA STATUS(16) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 458 | }; |
| 459 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 460 | static const struct opcode_info_t vl_iarr[] = { /* VARIABLE LENGTH */ |
| 461 | {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] | 462 | NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0xb, 0xfa, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 463 | 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */ |
| 464 | }; |
| 465 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 466 | static const struct opcode_info_t maint_in_iarr[] = { /* MAINT IN */ |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 467 | {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] | 468 | {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 469 | 0xc7, 0, 0, 0, 0} }, /* REPORT SUPPORTED OPERATION CODES */ |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 470 | {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] | 471 | {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^] | 472 | 0, 0} }, /* REPORTED SUPPORTED TASK MANAGEMENT FUNCTIONS */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 473 | }; |
| 474 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 475 | static const struct opcode_info_t write_same_iarr[] = { |
| 476 | {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] | 477 | {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 478 | 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* WRITE SAME(16) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 479 | }; |
| 480 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 481 | static const struct opcode_info_t reserve_iarr[] = { |
| 482 | {0, 0x16, 0, F_D_OUT, NULL, NULL, /* RESERVE(6) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 483 | {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 484 | }; |
| 485 | |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 486 | static const struct opcode_info_t release_iarr[] = { |
| 487 | {0, 0x17, 0, F_D_OUT, NULL, NULL, /* RELEASE(6) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 488 | {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 489 | }; |
| 490 | |
| 491 | |
| 492 | /* This array is accessed via SDEB_I_* values. Make sure all are mapped, |
| 493 | * plus the terminating elements for logic that scans this table such as |
| 494 | * REPORT SUPPORTED OPERATION CODES. */ |
| 495 | static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEMENT + 1] = { |
| 496 | /* 0 */ |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 497 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* unknown opcodes */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 498 | {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^] | 499 | {0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL, /* INQUIRY */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 500 | {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 501 | {0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL, |
| 502 | {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^] | 503 | 0, 0} }, /* REPORT LUNS */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 504 | {0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL, |
| 505 | {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 506 | {0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */ |
| 507 | {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^] | 508 | /* 5 */ |
| 509 | {ARRAY_SIZE(msense_iarr), 0x5a, 0, F_D_IN, /* MODE SENSE(10) */ |
| 510 | resp_mode_sense, msense_iarr, {10, 0xf8, 0xff, 0xff, 0, 0, 0, |
| 511 | 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 512 | {ARRAY_SIZE(mselect_iarr), 0x55, 0, F_D_OUT, /* MODE SELECT(10) */ |
| 513 | resp_mode_select, mselect_iarr, {10, 0xf1, 0, 0, 0, 0, 0, 0xff, |
| 514 | 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 515 | {0, 0x4d, 0, F_D_IN, resp_log_sense, NULL, /* LOG SENSE */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 516 | {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, |
| 517 | 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 518 | {0, 0x25, 0, F_D_IN, resp_readcap, NULL, /* READ CAPACITY(10) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 519 | {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0, |
| 520 | 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 521 | {ARRAY_SIZE(read_iarr), 0x88, 0, F_D_IN | FF_MEDIA_IO, /* READ(16) */ |
| 522 | resp_read_dt0, read_iarr, {16, 0xfe, 0xff, 0xff, 0xff, 0xff, |
| 523 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} }, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 524 | /* 10 */ |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 525 | {ARRAY_SIZE(write_iarr), 0x8a, 0, F_D_OUT | FF_MEDIA_IO, |
| 526 | resp_write_dt0, write_iarr, /* WRITE(16) */ |
| 527 | {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 528 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} }, /* WRITE(16) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 529 | {0, 0x1b, 0, 0, resp_start_stop, NULL, /* START STOP UNIT */ |
| 530 | {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^] | 531 | {ARRAY_SIZE(sa_in_16_iarr), 0x9e, 0x10, F_SA_LOW | F_D_IN, |
| 532 | resp_readcap16, sa_in_16_iarr, /* SA_IN(16), READ CAPACITY(16) */ |
| 533 | {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 534 | 0xff, 0xff, 0xff, 0xff, 0x1, 0xc7} }, |
| 535 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* SA_OUT(16) */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 536 | {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^] | 537 | {ARRAY_SIZE(maint_in_iarr), 0xa3, 0xa, F_SA_LOW | F_D_IN, |
| 538 | resp_report_tgtpgs, /* MAINT IN, REPORT TARGET PORT GROUPS */ |
| 539 | maint_in_iarr, {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff, |
| 540 | 0xff, 0, 0xc7, 0, 0, 0, 0} }, |
| 541 | /* 15 */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 542 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */ |
| 543 | {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^] | 544 | {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] | 545 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, |
| 546 | 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 547 | {ARRAY_SIZE(vl_iarr), 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_MEDIA_IO, |
| 548 | resp_read_dt0, vl_iarr, /* VARIABLE LENGTH, READ(32) */ |
| 549 | {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x9, 0xfe, 0, 0xff, 0xff, |
| 550 | 0xff, 0xff} }, |
| 551 | {ARRAY_SIZE(reserve_iarr), 0x56, 0, F_D_OUT, |
| 552 | NULL, reserve_iarr, /* RESERVE(10) <no response function> */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 553 | {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 554 | 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 555 | {ARRAY_SIZE(release_iarr), 0x57, 0, F_D_OUT, |
| 556 | NULL, release_iarr, /* RELEASE(10) <no response function> */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 557 | {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 558 | 0} }, |
| 559 | /* 20 */ |
Douglas Gilbert | f7f9f26 | 2015-11-22 12:11:28 -0500 | [diff] [blame] | 560 | {0, 0x1e, 0, 0, NULL, NULL, /* ALLOW REMOVAL */ |
| 561 | {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] | 562 | {0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */ |
| 563 | {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 564 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */ |
| 565 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 566 | {0, 0x1d, F_D_OUT, 0, NULL, NULL, /* SEND DIAGNOSTIC */ |
| 567 | {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^] | 568 | {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] | 569 | {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^] | 570 | /* 25 */ |
| 571 | {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] | 572 | NULL, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 573 | 0, 0, 0, 0, 0, 0} }, /* XDWRITEREAD(10) */ |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 574 | {0, 0x3b, 0, F_D_OUT_MAYBE, resp_write_buffer, NULL, |
| 575 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, |
| 576 | 0, 0, 0, 0} }, /* WRITE_BUFFER */ |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 577 | {ARRAY_SIZE(write_same_iarr), 0x41, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, |
| 578 | resp_write_same_10, write_same_iarr, /* WRITE SAME(10) */ |
| 579 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, |
| 580 | 0, 0, 0, 0, 0} }, |
| 581 | {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] | 582 | {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 583 | 0, 0, 0, 0} }, |
Douglas Gilbert | 46f64e7 | 2017-12-23 12:48:13 -0500 | [diff] [blame^] | 584 | {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] | 585 | {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, |
Douglas Gilbert | b7e2458 | 2017-12-23 12:48:11 -0500 | [diff] [blame] | 586 | 0, 0xff, 0x3f, 0xc7} }, /* COMPARE AND WRITE */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 587 | |
| 588 | /* 30 */ |
| 589 | {0xff, 0, 0, 0, NULL, NULL, /* terminating element */ |
| 590 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 591 | }; |
| 592 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 593 | static int sdebug_add_host = DEF_NUM_HOST; |
| 594 | static int sdebug_ato = DEF_ATO; |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 595 | static int sdebug_cdb_len = DEF_CDB_LEN; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 596 | static int sdebug_jdelay = DEF_JDELAY; /* if > 0 then unit is jiffies */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 597 | static int sdebug_dev_size_mb = DEF_DEV_SIZE_MB; |
| 598 | static int sdebug_dif = DEF_DIF; |
| 599 | static int sdebug_dix = DEF_DIX; |
| 600 | static int sdebug_dsense = DEF_D_SENSE; |
| 601 | static int sdebug_every_nth = DEF_EVERY_NTH; |
| 602 | static int sdebug_fake_rw = DEF_FAKE_RW; |
| 603 | static unsigned int sdebug_guard = DEF_GUARD; |
| 604 | static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED; |
| 605 | static int sdebug_max_luns = DEF_MAX_LUNS; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 606 | static int sdebug_max_queue = SDEBUG_CANQUEUE; /* per submit queue */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 607 | 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] | 608 | static int sdebug_ndelay = DEF_NDELAY; /* if > 0 then unit is nanoseconds */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 609 | static int sdebug_no_lun_0 = DEF_NO_LUN_0; |
| 610 | static int sdebug_no_uld; |
| 611 | static int sdebug_num_parts = DEF_NUM_PARTS; |
| 612 | static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */ |
| 613 | static int sdebug_opt_blks = DEF_OPT_BLKS; |
| 614 | static int sdebug_opts = DEF_OPTS; |
| 615 | static int sdebug_physblk_exp = DEF_PHYSBLK_EXP; |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 616 | static int sdebug_opt_xferlen_exp = DEF_OPT_XFERLEN_EXP; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 617 | static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral device type */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 618 | static int sdebug_scsi_level = DEF_SCSI_LEVEL; |
| 619 | static int sdebug_sector_size = DEF_SECTOR_SIZE; |
| 620 | static int sdebug_virtual_gb = DEF_VIRTUAL_GB; |
| 621 | static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO; |
| 622 | static unsigned int sdebug_lbpu = DEF_LBPU; |
| 623 | static unsigned int sdebug_lbpws = DEF_LBPWS; |
| 624 | static unsigned int sdebug_lbpws10 = DEF_LBPWS10; |
| 625 | static unsigned int sdebug_lbprz = DEF_LBPRZ; |
| 626 | static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT; |
| 627 | static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY; |
| 628 | static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS; |
| 629 | static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC; |
| 630 | static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH; |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 631 | static int sdebug_uuid_ctl = DEF_UUID_CTL; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 632 | static bool sdebug_removable = DEF_REMOVABLE; |
| 633 | static bool sdebug_clustering; |
| 634 | static bool sdebug_host_lock = DEF_HOST_LOCK; |
| 635 | static bool sdebug_strict = DEF_STRICT; |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 636 | static bool sdebug_any_injecting_opt; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 637 | static bool sdebug_verbose; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 638 | static bool have_dif_prot; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 639 | static bool sdebug_statistics = DEF_STATISTICS; |
| 640 | static bool sdebug_mq_active; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 642 | static unsigned int sdebug_store_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | static sector_t sdebug_capacity; /* in sectors */ |
| 644 | |
| 645 | /* old BIOS stuff, kernel may get rid of them but some mode sense pages |
| 646 | may still need them */ |
| 647 | static int sdebug_heads; /* heads per disk */ |
| 648 | static int sdebug_cylinders_per; /* cylinders per surface */ |
| 649 | static int sdebug_sectors_per; /* sectors per cylinder */ |
| 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | static LIST_HEAD(sdebug_host_list); |
| 652 | static DEFINE_SPINLOCK(sdebug_host_list_lock); |
| 653 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 654 | static unsigned char *fake_storep; /* ramdisk storage */ |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 655 | static struct t10_pi_tuple *dif_storep; /* protection info */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 656 | static void *map_storep; /* provisioning map */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 658 | static unsigned long map_size; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 659 | static int num_aborts; |
| 660 | static int num_dev_resets; |
| 661 | static int num_target_resets; |
| 662 | static int num_bus_resets; |
| 663 | static int num_host_resets; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 664 | static int dix_writes; |
| 665 | static int dix_reads; |
| 666 | static int dif_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 668 | static int submit_queues = DEF_SUBMIT_QUEUES; /* > 1 for multi-queue (mq) */ |
| 669 | 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] | 670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | static DEFINE_RWLOCK(atomic_rw); |
| 672 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 673 | static char sdebug_proc_name[] = MY_NAME; |
| 674 | static const char *my_name = MY_NAME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | static struct bus_type pseudo_lld_bus; |
| 677 | |
| 678 | static struct device_driver sdebug_driverfs_driver = { |
| 679 | .name = sdebug_proc_name, |
| 680 | .bus = &pseudo_lld_bus, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | }; |
| 682 | |
| 683 | static const int check_condition_result = |
| 684 | (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
| 685 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 686 | static const int illegal_condition_result = |
| 687 | (DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION; |
| 688 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 689 | static const int device_qfull_result = |
| 690 | (DID_OK << 16) | (COMMAND_COMPLETE << 8) | SAM_STAT_TASK_SET_FULL; |
| 691 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 692 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 693 | /* Only do the extra work involved in logical block provisioning if one or |
| 694 | * more of the lbpu, lbpws or lbpws10 parameters are given and we are doing |
| 695 | * real reads and writes (i.e. not skipping them for speed). |
| 696 | */ |
| 697 | static inline bool scsi_debug_lbp(void) |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 698 | { |
| 699 | return 0 == sdebug_fake_rw && |
| 700 | (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10); |
| 701 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 702 | |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 703 | static void *fake_store(unsigned long long lba) |
| 704 | { |
| 705 | lba = do_div(lba, sdebug_store_sectors); |
| 706 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 707 | return fake_storep + lba * sdebug_sector_size; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 708 | } |
| 709 | |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 710 | static struct t10_pi_tuple *dif_store(sector_t sector) |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 711 | { |
Arnd Bergmann | 4941311 | 2015-11-20 17:38:28 +0100 | [diff] [blame] | 712 | sector = sector_div(sector, sdebug_store_sectors); |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 713 | |
| 714 | return dif_storep + sector; |
| 715 | } |
| 716 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 717 | static void sdebug_max_tgts_luns(void) |
| 718 | { |
| 719 | struct sdebug_host_info *sdbg_host; |
| 720 | struct Scsi_Host *hpnt; |
| 721 | |
| 722 | spin_lock(&sdebug_host_list_lock); |
| 723 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
| 724 | hpnt = sdbg_host->shost; |
| 725 | if ((hpnt->this_id >= 0) && |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 726 | (sdebug_num_tgts > hpnt->this_id)) |
| 727 | hpnt->max_id = sdebug_num_tgts + 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 728 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 729 | hpnt->max_id = sdebug_num_tgts; |
| 730 | /* sdebug_max_luns; */ |
Tomas Winkler | f2d3fd2 | 2015-07-28 16:54:25 +0300 | [diff] [blame] | 731 | hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 732 | } |
| 733 | spin_unlock(&sdebug_host_list_lock); |
| 734 | } |
| 735 | |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 736 | enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1}; |
| 737 | |
| 738 | /* 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] | 739 | static void mk_sense_invalid_fld(struct scsi_cmnd *scp, |
| 740 | enum sdeb_cmd_data c_d, |
| 741 | int in_byte, int in_bit) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 742 | { |
| 743 | unsigned char *sbuff; |
| 744 | u8 sks[4]; |
| 745 | int sl, asc; |
| 746 | |
| 747 | sbuff = scp->sense_buffer; |
| 748 | if (!sbuff) { |
| 749 | sdev_printk(KERN_ERR, scp->device, |
| 750 | "%s: sense_buffer is NULL\n", __func__); |
| 751 | return; |
| 752 | } |
| 753 | asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST; |
| 754 | memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 755 | scsi_build_sense_buffer(sdebug_dsense, sbuff, ILLEGAL_REQUEST, asc, 0); |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 756 | memset(sks, 0, sizeof(sks)); |
| 757 | sks[0] = 0x80; |
| 758 | if (c_d) |
| 759 | sks[0] |= 0x40; |
| 760 | if (in_bit >= 0) { |
| 761 | sks[0] |= 0x8; |
| 762 | sks[0] |= 0x7 & in_bit; |
| 763 | } |
| 764 | put_unaligned_be16(in_byte, sks + 1); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 765 | if (sdebug_dsense) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 766 | sl = sbuff[7] + 8; |
| 767 | sbuff[7] = sl; |
| 768 | sbuff[sl] = 0x2; |
| 769 | sbuff[sl + 1] = 0x6; |
| 770 | memcpy(sbuff + sl + 4, sks, 3); |
| 771 | } else |
| 772 | memcpy(sbuff + 15, sks, 3); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 773 | if (sdebug_verbose) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 774 | sdev_printk(KERN_INFO, scp->device, "%s: [sense_key,asc,ascq" |
| 775 | "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n", |
| 776 | my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit); |
| 777 | } |
| 778 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 779 | 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] | 780 | { |
| 781 | unsigned char *sbuff; |
| 782 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 783 | sbuff = scp->sense_buffer; |
| 784 | if (!sbuff) { |
| 785 | sdev_printk(KERN_ERR, scp->device, |
| 786 | "%s: sense_buffer is NULL\n", __func__); |
| 787 | return; |
| 788 | } |
| 789 | memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 790 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 791 | scsi_build_sense_buffer(sdebug_dsense, sbuff, key, asc, asq); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 792 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 793 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 794 | sdev_printk(KERN_INFO, scp->device, |
| 795 | "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n", |
| 796 | my_name, key, asc, asq); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 799 | static void mk_sense_invalid_opcode(struct scsi_cmnd *scp) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 800 | { |
| 801 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0); |
| 802 | } |
| 803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg) |
| 805 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 806 | if (sdebug_verbose) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 807 | if (0x1261 == cmd) |
| 808 | sdev_printk(KERN_INFO, dev, |
| 809 | "%s: BLKFLSBUF [0x1261]\n", __func__); |
| 810 | else if (0x5331 == cmd) |
| 811 | sdev_printk(KERN_INFO, dev, |
| 812 | "%s: CDROM_GET_CAPABILITY [0x5331]\n", |
| 813 | __func__); |
| 814 | else |
| 815 | sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n", |
| 816 | __func__, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | return -EINVAL; |
| 819 | /* return -ENOTTY; // correct return but upsets fdisk */ |
| 820 | } |
| 821 | |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 822 | static void config_cdb_len(struct scsi_device *sdev) |
| 823 | { |
| 824 | switch (sdebug_cdb_len) { |
| 825 | case 6: /* suggest 6 byte READ, WRITE and MODE SENSE/SELECT */ |
| 826 | sdev->use_10_for_rw = false; |
| 827 | sdev->use_16_for_rw = false; |
| 828 | sdev->use_10_for_ms = false; |
| 829 | break; |
| 830 | case 10: /* suggest 10 byte RWs and 6 byte MODE SENSE/SELECT */ |
| 831 | sdev->use_10_for_rw = true; |
| 832 | sdev->use_16_for_rw = false; |
| 833 | sdev->use_10_for_ms = false; |
| 834 | break; |
| 835 | case 12: /* suggest 10 byte RWs and 10 byte MODE SENSE/SELECT */ |
| 836 | sdev->use_10_for_rw = true; |
| 837 | sdev->use_16_for_rw = false; |
| 838 | sdev->use_10_for_ms = true; |
| 839 | break; |
| 840 | case 16: |
| 841 | sdev->use_10_for_rw = false; |
| 842 | sdev->use_16_for_rw = true; |
| 843 | sdev->use_10_for_ms = true; |
| 844 | break; |
| 845 | case 32: /* No knobs to suggest this so same as 16 for now */ |
| 846 | sdev->use_10_for_rw = false; |
| 847 | sdev->use_16_for_rw = true; |
| 848 | sdev->use_10_for_ms = true; |
| 849 | break; |
| 850 | default: |
| 851 | pr_warn("unexpected cdb_len=%d, force to 10\n", |
| 852 | sdebug_cdb_len); |
| 853 | sdev->use_10_for_rw = true; |
| 854 | sdev->use_16_for_rw = false; |
| 855 | sdev->use_10_for_ms = false; |
| 856 | sdebug_cdb_len = 10; |
| 857 | break; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | static void all_config_cdb_len(void) |
| 862 | { |
| 863 | struct sdebug_host_info *sdbg_host; |
| 864 | struct Scsi_Host *shost; |
| 865 | struct scsi_device *sdev; |
| 866 | |
| 867 | spin_lock(&sdebug_host_list_lock); |
| 868 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
| 869 | shost = sdbg_host->shost; |
| 870 | shost_for_each_device(sdev, shost) { |
| 871 | config_cdb_len(sdev); |
| 872 | } |
| 873 | } |
| 874 | spin_unlock(&sdebug_host_list_lock); |
| 875 | } |
| 876 | |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 877 | static void clear_luns_changed_on_target(struct sdebug_dev_info *devip) |
| 878 | { |
| 879 | struct sdebug_host_info *sdhp; |
| 880 | struct sdebug_dev_info *dp; |
| 881 | |
| 882 | spin_lock(&sdebug_host_list_lock); |
| 883 | list_for_each_entry(sdhp, &sdebug_host_list, host_list) { |
| 884 | list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) { |
| 885 | if ((devip->sdbg_host == dp->sdbg_host) && |
| 886 | (devip->target == dp->target)) |
| 887 | clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm); |
| 888 | } |
| 889 | } |
| 890 | spin_unlock(&sdebug_host_list_lock); |
| 891 | } |
| 892 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 893 | 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] | 894 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 895 | int k; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 896 | |
| 897 | k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS); |
| 898 | if (k != SDEBUG_NUM_UAS) { |
| 899 | const char *cp = NULL; |
| 900 | |
| 901 | switch (k) { |
| 902 | case SDEBUG_UA_POR: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 903 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC, |
| 904 | POWER_ON_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 905 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 906 | cp = "power on reset"; |
| 907 | break; |
| 908 | case SDEBUG_UA_BUS_RESET: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 909 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC, |
| 910 | BUS_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 911 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 912 | cp = "bus reset"; |
| 913 | break; |
| 914 | case SDEBUG_UA_MODE_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 915 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC, |
| 916 | MODE_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 917 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 918 | cp = "mode parameters changed"; |
| 919 | break; |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 920 | case SDEBUG_UA_CAPACITY_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 921 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC, |
| 922 | CAPACITY_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 923 | if (sdebug_verbose) |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 924 | cp = "capacity data changed"; |
Ewan D. Milne | f49accf | 2014-12-04 11:49:25 -0500 | [diff] [blame] | 925 | break; |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 926 | case SDEBUG_UA_MICROCODE_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 927 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 928 | TARGET_CHANGED_ASC, |
| 929 | MICROCODE_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 930 | if (sdebug_verbose) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 931 | cp = "microcode has been changed"; |
| 932 | break; |
| 933 | case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 934 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 935 | TARGET_CHANGED_ASC, |
| 936 | MICROCODE_CHANGED_WO_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 937 | if (sdebug_verbose) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 938 | cp = "microcode has been changed without reset"; |
| 939 | break; |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 940 | case SDEBUG_UA_LUNS_CHANGED: |
| 941 | /* |
| 942 | * SPC-3 behavior is to report a UNIT ATTENTION with |
| 943 | * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN |
| 944 | * on the target, until a REPORT LUNS command is |
| 945 | * received. SPC-4 behavior is to report it only once. |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 946 | * NOTE: sdebug_scsi_level does not use the same |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 947 | * values as struct scsi_device->scsi_level. |
| 948 | */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 949 | if (sdebug_scsi_level >= 6) /* SPC-4 and above */ |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 950 | clear_luns_changed_on_target(devip); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 951 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 952 | TARGET_CHANGED_ASC, |
| 953 | LUNS_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 954 | if (sdebug_verbose) |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 955 | cp = "reported luns data has changed"; |
| 956 | break; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 957 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 958 | pr_warn("unexpected unit attention code=%d\n", k); |
| 959 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 960 | cp = "unknown"; |
| 961 | break; |
| 962 | } |
| 963 | clear_bit(k, devip->uas_bm); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 964 | if (sdebug_verbose) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 965 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 966 | "%s reports: Unit attention: %s\n", |
| 967 | my_name, cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | return check_condition_result; |
| 969 | } |
| 970 | return 0; |
| 971 | } |
| 972 | |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 973 | /* 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] | 974 | 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] | 975 | int arr_len) |
| 976 | { |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 977 | int act_len; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 978 | struct scsi_data_buffer *sdb = scsi_in(scp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 980 | if (!sdb->length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | return 0; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 982 | if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE)) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 983 | return DID_ERROR << 16; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 984 | |
| 985 | act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents, |
| 986 | arr, arr_len); |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 987 | sdb->resid = scsi_bufflen(scp) - act_len; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | return 0; |
| 990 | } |
| 991 | |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 992 | /* Partial build of SCSI "data-in" buffer. Returns 0 if ok else |
| 993 | * (DID_ERROR << 16). Can write to offset in data-in buffer. If multiple |
| 994 | * calls, not required to write in ascending offset order. Assumes resid |
| 995 | * set to scsi_bufflen() prior to any calls. |
| 996 | */ |
| 997 | static int p_fill_from_dev_buffer(struct scsi_cmnd *scp, const void *arr, |
| 998 | int arr_len, unsigned int off_dst) |
| 999 | { |
| 1000 | int act_len, n; |
| 1001 | struct scsi_data_buffer *sdb = scsi_in(scp); |
| 1002 | off_t skip = off_dst; |
| 1003 | |
| 1004 | if (sdb->length <= off_dst) |
| 1005 | return 0; |
| 1006 | if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE)) |
| 1007 | return DID_ERROR << 16; |
| 1008 | |
| 1009 | act_len = sg_pcopy_from_buffer(sdb->table.sgl, sdb->table.nents, |
| 1010 | arr, arr_len, skip); |
| 1011 | pr_debug("%s: off_dst=%u, scsi_bufflen=%u, act_len=%u, resid=%d\n", |
| 1012 | __func__, off_dst, scsi_bufflen(scp), act_len, sdb->resid); |
| 1013 | n = (int)scsi_bufflen(scp) - ((int)off_dst + act_len); |
| 1014 | sdb->resid = min(sdb->resid, n); |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | /* Fetches from SCSI "data-out" buffer. Returns number of bytes fetched into |
| 1019 | * 'arr' or -1 if error. |
| 1020 | */ |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 1021 | static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr, |
| 1022 | int arr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | { |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 1024 | if (!scsi_bufflen(scp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | return 0; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 1026 | if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_TO_DEVICE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | return -1; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 1028 | |
| 1029 | return scsi_sg_copy_to_buffer(scp, arr, arr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | |
Hannes Reinecke | e5203cf | 2017-10-02 16:26:33 +0200 | [diff] [blame] | 1033 | static char sdebug_inq_vendor_id[9] = "Linux "; |
| 1034 | static char sdebug_inq_product_id[17] = "scsi_debug "; |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 1035 | static char sdebug_inq_product_rev[5] = SDEBUG_VERSION; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1036 | /* Use some locally assigned NAAs for SAS addresses. */ |
| 1037 | static const u64 naa3_comp_a = 0x3222222000000000ULL; |
| 1038 | static const u64 naa3_comp_b = 0x3333333000000000ULL; |
| 1039 | static const u64 naa3_comp_c = 0x3111111000000000ULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1041 | /* Device identification VPD page. Returns number of bytes placed in arr */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1042 | static int inquiry_vpd_83(unsigned char *arr, int port_group_id, |
| 1043 | int target_dev_id, int dev_id_num, |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1044 | const char *dev_id_str, int dev_id_str_len, |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 1045 | const uuid_t *lu_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1047 | int num, port_a; |
| 1048 | char b[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1050 | port_a = target_dev_id + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | /* T10 vendor identifier field format (faked) */ |
| 1052 | arr[0] = 0x2; /* ASCII */ |
| 1053 | arr[1] = 0x1; |
| 1054 | arr[2] = 0x0; |
Hannes Reinecke | e5203cf | 2017-10-02 16:26:33 +0200 | [diff] [blame] | 1055 | memcpy(&arr[4], sdebug_inq_vendor_id, 8); |
| 1056 | memcpy(&arr[12], sdebug_inq_product_id, 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | memcpy(&arr[28], dev_id_str, dev_id_str_len); |
| 1058 | num = 8 + 16 + dev_id_str_len; |
| 1059 | arr[3] = num; |
| 1060 | num += 4; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1061 | if (dev_id_num >= 0) { |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1062 | if (sdebug_uuid_ctl) { |
| 1063 | /* Locally assigned UUID */ |
| 1064 | arr[num++] = 0x1; /* binary (not necessarily sas) */ |
| 1065 | arr[num++] = 0xa; /* PIV=0, lu, naa */ |
| 1066 | arr[num++] = 0x0; |
| 1067 | arr[num++] = 0x12; |
| 1068 | arr[num++] = 0x10; /* uuid type=1, locally assigned */ |
| 1069 | arr[num++] = 0x0; |
| 1070 | memcpy(arr + num, lu_name, 16); |
| 1071 | num += 16; |
| 1072 | } else { |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1073 | /* NAA-3, Logical unit identifier (binary) */ |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1074 | arr[num++] = 0x1; /* binary (not necessarily sas) */ |
| 1075 | arr[num++] = 0x3; /* PIV=0, lu, naa */ |
| 1076 | arr[num++] = 0x0; |
| 1077 | arr[num++] = 0x8; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1078 | put_unaligned_be64(naa3_comp_b + dev_id_num, arr + num); |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1079 | num += 8; |
| 1080 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1081 | /* Target relative port number */ |
| 1082 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1083 | arr[num++] = 0x94; /* PIV=1, target port, rel port */ |
| 1084 | arr[num++] = 0x0; /* reserved */ |
| 1085 | arr[num++] = 0x4; /* length */ |
| 1086 | arr[num++] = 0x0; /* reserved */ |
| 1087 | arr[num++] = 0x0; /* reserved */ |
| 1088 | arr[num++] = 0x0; |
| 1089 | arr[num++] = 0x1; /* relative port A */ |
| 1090 | } |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1091 | /* NAA-3, Target port identifier */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1092 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1093 | arr[num++] = 0x93; /* piv=1, target port, naa */ |
| 1094 | arr[num++] = 0x0; |
| 1095 | arr[num++] = 0x8; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1096 | put_unaligned_be64(naa3_comp_a + port_a, arr + num); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1097 | num += 8; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1098 | /* NAA-3, Target port group identifier */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1099 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1100 | arr[num++] = 0x95; /* piv=1, target port group id */ |
| 1101 | arr[num++] = 0x0; |
| 1102 | arr[num++] = 0x4; |
| 1103 | arr[num++] = 0; |
| 1104 | arr[num++] = 0; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1105 | put_unaligned_be16(port_group_id, arr + num); |
| 1106 | num += 2; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1107 | /* NAA-3, Target device identifier */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1108 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1109 | arr[num++] = 0xa3; /* piv=1, target device, naa */ |
| 1110 | arr[num++] = 0x0; |
| 1111 | arr[num++] = 0x8; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1112 | put_unaligned_be64(naa3_comp_a + target_dev_id, arr + num); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1113 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1114 | /* SCSI name string: Target device identifier */ |
| 1115 | arr[num++] = 0x63; /* proto=sas, UTF-8 */ |
| 1116 | arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */ |
| 1117 | arr[num++] = 0x0; |
| 1118 | arr[num++] = 24; |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1119 | memcpy(arr + num, "naa.32222220", 12); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1120 | num += 12; |
| 1121 | snprintf(b, sizeof(b), "%08X", target_dev_id); |
| 1122 | memcpy(arr + num, b, 8); |
| 1123 | num += 8; |
| 1124 | memset(arr + num, 0, 4); |
| 1125 | num += 4; |
| 1126 | return num; |
| 1127 | } |
| 1128 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1129 | static unsigned char vpd84_data[] = { |
| 1130 | /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0, |
| 1131 | 0x22,0x22,0x22,0x0,0xbb,0x1, |
| 1132 | 0x22,0x22,0x22,0x0,0xbb,0x2, |
| 1133 | }; |
| 1134 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1135 | /* Software interface identification VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1136 | static int inquiry_vpd_84(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1137 | { |
| 1138 | memcpy(arr, vpd84_data, sizeof(vpd84_data)); |
| 1139 | return sizeof(vpd84_data); |
| 1140 | } |
| 1141 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1142 | /* Management network addresses VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1143 | static int inquiry_vpd_85(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1144 | { |
| 1145 | int num = 0; |
| 1146 | const char * na1 = "https://www.kernel.org/config"; |
| 1147 | const char * na2 = "http://www.kernel.org/log"; |
| 1148 | int plen, olen; |
| 1149 | |
| 1150 | arr[num++] = 0x1; /* lu, storage config */ |
| 1151 | arr[num++] = 0x0; /* reserved */ |
| 1152 | arr[num++] = 0x0; |
| 1153 | olen = strlen(na1); |
| 1154 | plen = olen + 1; |
| 1155 | if (plen % 4) |
| 1156 | plen = ((plen / 4) + 1) * 4; |
| 1157 | arr[num++] = plen; /* length, null termianted, padded */ |
| 1158 | memcpy(arr + num, na1, olen); |
| 1159 | memset(arr + num + olen, 0, plen - olen); |
| 1160 | num += plen; |
| 1161 | |
| 1162 | arr[num++] = 0x4; /* lu, logging */ |
| 1163 | arr[num++] = 0x0; /* reserved */ |
| 1164 | arr[num++] = 0x0; |
| 1165 | olen = strlen(na2); |
| 1166 | plen = olen + 1; |
| 1167 | if (plen % 4) |
| 1168 | plen = ((plen / 4) + 1) * 4; |
| 1169 | arr[num++] = plen; /* length, null terminated, padded */ |
| 1170 | memcpy(arr + num, na2, olen); |
| 1171 | memset(arr + num + olen, 0, plen - olen); |
| 1172 | num += plen; |
| 1173 | |
| 1174 | return num; |
| 1175 | } |
| 1176 | |
| 1177 | /* SCSI ports VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1178 | static int inquiry_vpd_88(unsigned char *arr, int target_dev_id) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1179 | { |
| 1180 | int num = 0; |
| 1181 | int port_a, port_b; |
| 1182 | |
| 1183 | port_a = target_dev_id + 1; |
| 1184 | port_b = port_a + 1; |
| 1185 | arr[num++] = 0x0; /* reserved */ |
| 1186 | arr[num++] = 0x0; /* reserved */ |
| 1187 | arr[num++] = 0x0; |
| 1188 | arr[num++] = 0x1; /* relative port 1 (primary) */ |
| 1189 | memset(arr + num, 0, 6); |
| 1190 | num += 6; |
| 1191 | arr[num++] = 0x0; |
| 1192 | arr[num++] = 12; /* length tp descriptor */ |
| 1193 | /* naa-5 target port identifier (A) */ |
| 1194 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1195 | arr[num++] = 0x93; /* PIV=1, target port, NAA */ |
| 1196 | arr[num++] = 0x0; /* reserved */ |
| 1197 | arr[num++] = 0x8; /* length */ |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1198 | put_unaligned_be64(naa3_comp_a + port_a, arr + num); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1199 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1200 | arr[num++] = 0x0; /* reserved */ |
| 1201 | arr[num++] = 0x0; /* reserved */ |
| 1202 | arr[num++] = 0x0; |
| 1203 | arr[num++] = 0x2; /* relative port 2 (secondary) */ |
| 1204 | memset(arr + num, 0, 6); |
| 1205 | num += 6; |
| 1206 | arr[num++] = 0x0; |
| 1207 | arr[num++] = 12; /* length tp descriptor */ |
| 1208 | /* naa-5 target port identifier (B) */ |
| 1209 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1210 | arr[num++] = 0x93; /* PIV=1, target port, NAA */ |
| 1211 | arr[num++] = 0x0; /* reserved */ |
| 1212 | arr[num++] = 0x8; /* length */ |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 1213 | put_unaligned_be64(naa3_comp_a + port_b, arr + num); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1214 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1215 | |
| 1216 | return num; |
| 1217 | } |
| 1218 | |
| 1219 | |
| 1220 | static unsigned char vpd89_data[] = { |
| 1221 | /* from 4th byte */ 0,0,0,0, |
| 1222 | 'l','i','n','u','x',' ',' ',' ', |
| 1223 | 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ', |
| 1224 | '1','2','3','4', |
| 1225 | 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0, |
| 1226 | 0xec,0,0,0, |
| 1227 | 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0, |
| 1228 | 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20, |
| 1229 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33, |
| 1230 | 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31, |
| 1231 | 0x53,0x41, |
| 1232 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 1233 | 0x20,0x20, |
| 1234 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 1235 | 0x10,0x80, |
| 1236 | 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0, |
| 1237 | 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0, |
| 1238 | 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0, |
| 1239 | 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0, |
| 1240 | 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40, |
| 1241 | 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0, |
| 1242 | 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0, |
| 1243 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1244 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1245 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1246 | 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42, |
| 1247 | 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8, |
| 1248 | 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe, |
| 1249 | 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0, |
| 1250 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1251 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1252 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1253 | 0,0,0,0,0,0,0,0,0,0,0,0,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 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1258 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1259 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1260 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1261 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51, |
| 1262 | }; |
| 1263 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1264 | /* ATA Information VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1265 | static int inquiry_vpd_89(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1266 | { |
| 1267 | memcpy(arr, vpd89_data, sizeof(vpd89_data)); |
| 1268 | return sizeof(vpd89_data); |
| 1269 | } |
| 1270 | |
| 1271 | |
| 1272 | static unsigned char vpdb0_data[] = { |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1273 | /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64, |
| 1274 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1275 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1276 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1277 | }; |
| 1278 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1279 | /* Block limits VPD page (SBC-3) */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1280 | static int inquiry_vpd_b0(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1281 | { |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 1282 | unsigned int gran; |
| 1283 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1284 | memcpy(arr, vpdb0_data, sizeof(vpdb0_data)); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1285 | |
| 1286 | /* Optimal transfer length granularity */ |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 1287 | if (sdebug_opt_xferlen_exp != 0 && |
| 1288 | sdebug_physblk_exp < sdebug_opt_xferlen_exp) |
| 1289 | gran = 1 << sdebug_opt_xferlen_exp; |
| 1290 | else |
| 1291 | gran = 1 << sdebug_physblk_exp; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1292 | put_unaligned_be16(gran, arr + 2); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1293 | |
| 1294 | /* Maximum Transfer Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1295 | if (sdebug_store_sectors > 0x400) |
| 1296 | put_unaligned_be32(sdebug_store_sectors, arr + 4); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1297 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1298 | /* Optimal Transfer Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1299 | put_unaligned_be32(sdebug_opt_blks, &arr[8]); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1300 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1301 | if (sdebug_lbpu) { |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1302 | /* Maximum Unmap LBA Count */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1303 | put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1304 | |
| 1305 | /* Maximum Unmap Block Descriptor Count */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1306 | put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1307 | } |
| 1308 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1309 | /* Unmap Granularity Alignment */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1310 | if (sdebug_unmap_alignment) { |
| 1311 | put_unaligned_be32(sdebug_unmap_alignment, &arr[28]); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1312 | arr[28] |= 0x80; /* UGAVALID */ |
| 1313 | } |
| 1314 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1315 | /* Optimal Unmap Granularity */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1316 | put_unaligned_be32(sdebug_unmap_granularity, &arr[24]); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1317 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1318 | /* Maximum WRITE SAME Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1319 | put_unaligned_be64(sdebug_write_same_length, &arr[32]); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1320 | |
| 1321 | return 0x3c; /* Mandatory page length for Logical Block Provisioning */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1322 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1323 | return sizeof(vpdb0_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1326 | /* Block device characteristics VPD page (SBC-3) */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1327 | static int inquiry_vpd_b1(unsigned char *arr) |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1328 | { |
| 1329 | memset(arr, 0, 0x3c); |
| 1330 | arr[0] = 0; |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1331 | arr[1] = 1; /* non rotating medium (e.g. solid state) */ |
| 1332 | arr[2] = 0; |
| 1333 | arr[3] = 5; /* less than 1.8" */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1334 | |
| 1335 | return 0x3c; |
| 1336 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1338 | /* Logical block provisioning VPD page (SBC-4) */ |
| 1339 | static int inquiry_vpd_b2(unsigned char *arr) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1340 | { |
Martin K. Petersen | 3f0bc3b | 2012-03-08 10:48:29 -0500 | [diff] [blame] | 1341 | memset(arr, 0, 0x4); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1342 | arr[0] = 0; /* threshold exponent */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1343 | if (sdebug_lbpu) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1344 | arr[1] = 1 << 7; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1345 | if (sdebug_lbpws) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1346 | arr[1] |= 1 << 6; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1347 | if (sdebug_lbpws10) |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1348 | arr[1] |= 1 << 5; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1349 | if (sdebug_lbprz && scsi_debug_lbp()) |
| 1350 | arr[1] |= (sdebug_lbprz & 0x7) << 2; /* sbc4r07 and later */ |
| 1351 | /* anc_sup=0; dp=0 (no provisioning group descriptor) */ |
| 1352 | /* minimum_percentage=0; provisioning_type=0 (unknown) */ |
| 1353 | /* threshold_percentage=0 */ |
Martin K. Petersen | 3f0bc3b | 2012-03-08 10:48:29 -0500 | [diff] [blame] | 1354 | return 0x4; |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1355 | } |
| 1356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | #define SDEBUG_LONG_INQ_SZ 96 |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1358 | #define SDEBUG_MAX_INQ_ARR_SZ 584 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1360 | 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] | 1361 | { |
| 1362 | unsigned char pq_pdt; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1363 | unsigned char * arr; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1364 | unsigned char *cmd = scp->cmnd; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1365 | int alloc_len, n, ret; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1366 | bool have_wlun, is_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1368 | alloc_len = get_unaligned_be16(cmd + 3); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 1369 | arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC); |
| 1370 | if (! arr) |
| 1371 | return DID_REQUEUE << 16; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1372 | is_disk = (sdebug_ptype == TYPE_DISK); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1373 | have_wlun = scsi_is_wlun(scp->device->lun); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1374 | if (have_wlun) |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1375 | pq_pdt = TYPE_WLUN; /* present, wlun */ |
| 1376 | else if (sdebug_no_lun_0 && (devip->lun == SDEBUG_LUN_0_VAL)) |
| 1377 | pq_pdt = 0x7f; /* not present, PQ=3, PDT=0x1f */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1378 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1379 | pq_pdt = (sdebug_ptype & 0x1f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | arr[0] = pq_pdt; |
| 1381 | if (0x2 & cmd[1]) { /* CMDDT bit set */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1382 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1383 | kfree(arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | return check_condition_result; |
| 1385 | } else if (0x1 & cmd[1]) { /* EVPD bit set */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1386 | int lu_id_num, port_group_id, target_dev_id, len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1387 | char lu_id_str[6]; |
| 1388 | int host_no = devip->sdbg_host->shost->host_no; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1390 | port_group_id = (((host_no + 1) & 0x7f) << 8) + |
| 1391 | (devip->channel & 0x7f); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1392 | if (sdebug_vpd_use_hostno == 0) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1393 | host_no = 0; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1394 | lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) + |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1395 | (devip->target * 1000) + devip->lun); |
| 1396 | target_dev_id = ((host_no + 1) * 2000) + |
| 1397 | (devip->target * 1000) - 3; |
| 1398 | len = scnprintf(lu_id_str, 6, "%d", lu_id_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | if (0 == cmd[2]) { /* supported vital product data pages */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1400 | arr[1] = cmd[2]; /*sanity */ |
| 1401 | n = 4; |
| 1402 | arr[n++] = 0x0; /* this page */ |
| 1403 | arr[n++] = 0x80; /* unit serial number */ |
| 1404 | arr[n++] = 0x83; /* device identification */ |
| 1405 | arr[n++] = 0x84; /* software interface ident. */ |
| 1406 | arr[n++] = 0x85; /* management network addresses */ |
| 1407 | arr[n++] = 0x86; /* extended inquiry */ |
| 1408 | arr[n++] = 0x87; /* mode page policy */ |
| 1409 | arr[n++] = 0x88; /* SCSI ports */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1410 | if (is_disk) { /* SBC only */ |
| 1411 | arr[n++] = 0x89; /* ATA information */ |
| 1412 | arr[n++] = 0xb0; /* Block limits */ |
| 1413 | arr[n++] = 0xb1; /* Block characteristics */ |
| 1414 | arr[n++] = 0xb2; /* Logical Block Prov */ |
| 1415 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1416 | arr[3] = n - 4; /* number of supported VPD pages */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | } else if (0x80 == cmd[2]) { /* unit serial number */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1418 | arr[1] = cmd[2]; /*sanity */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | arr[3] = len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1420 | memcpy(&arr[4], lu_id_str, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | } else if (0x83 == cmd[2]) { /* device identification */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1422 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1423 | arr[3] = inquiry_vpd_83(&arr[4], port_group_id, |
| 1424 | target_dev_id, lu_id_num, |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 1425 | lu_id_str, len, |
| 1426 | &devip->lu_name); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1427 | } else if (0x84 == cmd[2]) { /* Software interface ident. */ |
| 1428 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1429 | arr[3] = inquiry_vpd_84(&arr[4]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1430 | } else if (0x85 == cmd[2]) { /* Management network addresses */ |
| 1431 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1432 | arr[3] = inquiry_vpd_85(&arr[4]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1433 | } else if (0x86 == cmd[2]) { /* extended inquiry */ |
| 1434 | arr[1] = cmd[2]; /*sanity */ |
| 1435 | arr[3] = 0x3c; /* number of following entries */ |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 1436 | if (sdebug_dif == T10_PI_TYPE3_PROTECTION) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1437 | arr[4] = 0x4; /* SPT: GRD_CHK:1 */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1438 | else if (have_dif_prot) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1439 | arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */ |
| 1440 | else |
| 1441 | arr[4] = 0x0; /* no protection stuff */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1442 | arr[5] = 0x7; /* head of q, ordered + simple q's */ |
| 1443 | } else if (0x87 == cmd[2]) { /* mode page policy */ |
| 1444 | arr[1] = cmd[2]; /*sanity */ |
| 1445 | arr[3] = 0x8; /* number of following entries */ |
| 1446 | arr[4] = 0x2; /* disconnect-reconnect mp */ |
| 1447 | arr[6] = 0x80; /* mlus, shared */ |
| 1448 | arr[8] = 0x18; /* protocol specific lu */ |
| 1449 | arr[10] = 0x82; /* mlus, per initiator port */ |
| 1450 | } else if (0x88 == cmd[2]) { /* SCSI Ports */ |
| 1451 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1452 | arr[3] = inquiry_vpd_88(&arr[4], target_dev_id); |
| 1453 | } else if (is_disk && 0x89 == cmd[2]) { /* ATA information */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1454 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1455 | n = inquiry_vpd_89(&arr[4]); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1456 | put_unaligned_be16(n, arr + 2); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1457 | } else if (is_disk && 0xb0 == cmd[2]) { /* Block limits */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1458 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1459 | arr[3] = inquiry_vpd_b0(&arr[4]); |
| 1460 | } else if (is_disk && 0xb1 == cmd[2]) { /* Block char. */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1461 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1462 | arr[3] = inquiry_vpd_b1(&arr[4]); |
| 1463 | } else if (is_disk && 0xb2 == cmd[2]) { /* LB Prov. */ |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1464 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1465 | arr[3] = inquiry_vpd_b2(&arr[4]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1467 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1468 | kfree(arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | return check_condition_result; |
| 1470 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1471 | len = min(get_unaligned_be16(arr + 2) + 4, alloc_len); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1472 | ret = fill_from_dev_buffer(scp, arr, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1473 | min(len, SDEBUG_MAX_INQ_ARR_SZ)); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1474 | kfree(arr); |
| 1475 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | } |
| 1477 | /* drops through here for a standard inquiry */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1478 | arr[1] = sdebug_removable ? 0x80 : 0; /* Removable disk */ |
| 1479 | arr[2] = sdebug_scsi_level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | arr[3] = 2; /* response_data_format==2 */ |
| 1481 | arr[4] = SDEBUG_LONG_INQ_SZ - 5; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 1482 | arr[5] = (int)have_dif_prot; /* PROTECT bit */ |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1483 | if (sdebug_vpd_use_hostno == 0) |
Martin K. Petersen | 70bdf20 | 2017-05-19 12:39:36 -0400 | [diff] [blame] | 1484 | arr[5] |= 0x10; /* claim: implicit TPGS */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1485 | arr[6] = 0x10; /* claim: MultiP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1487 | arr[7] = 0xa; /* claim: LINKED + CMDQUE */ |
Hannes Reinecke | e5203cf | 2017-10-02 16:26:33 +0200 | [diff] [blame] | 1488 | memcpy(&arr[8], sdebug_inq_vendor_id, 8); |
| 1489 | memcpy(&arr[16], sdebug_inq_product_id, 16); |
| 1490 | memcpy(&arr[32], sdebug_inq_product_rev, 4); |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 1491 | /* Use Vendor Specific area to place driver date in ASCII hex */ |
| 1492 | memcpy(&arr[36], sdebug_version_date, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | /* version descriptors (2 bytes each) follow */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1494 | put_unaligned_be16(0xc0, arr + 58); /* SAM-6 no version claimed */ |
| 1495 | put_unaligned_be16(0x5c0, arr + 60); /* SPC-5 no version claimed */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1496 | n = 62; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1497 | if (is_disk) { /* SBC-4 no version claimed */ |
| 1498 | put_unaligned_be16(0x600, arr + n); |
| 1499 | n += 2; |
| 1500 | } else if (sdebug_ptype == TYPE_TAPE) { /* SSC-4 rev 3 */ |
| 1501 | put_unaligned_be16(0x525, arr + n); |
| 1502 | n += 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | } |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1504 | put_unaligned_be16(0x2100, arr + n); /* SPL-4 no version claimed */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1505 | ret = fill_from_dev_buffer(scp, arr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | min(alloc_len, SDEBUG_LONG_INQ_SZ)); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1507 | kfree(arr); |
| 1508 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1511 | static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, |
| 1512 | 0, 0, 0x0, 0x0}; |
| 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | static int resp_requests(struct scsi_cmnd * scp, |
| 1515 | struct sdebug_dev_info * devip) |
| 1516 | { |
| 1517 | unsigned char * sbuff; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1518 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1519 | unsigned char arr[SCSI_SENSE_BUFFERSIZE]; |
Tomas Winkler | 2492fc0 | 2015-07-28 16:54:26 +0300 | [diff] [blame] | 1520 | bool dsense; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | int len = 18; |
| 1522 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1523 | memset(arr, 0, sizeof(arr)); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1524 | dsense = !!(cmd[1] & 1); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1525 | sbuff = scp->sense_buffer; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1526 | if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1527 | if (dsense) { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1528 | arr[0] = 0x72; |
| 1529 | arr[1] = 0x0; /* NO_SENSE in sense_key */ |
| 1530 | arr[2] = THRESHOLD_EXCEEDED; |
| 1531 | arr[3] = 0xff; /* TEST set and MRIE==6 */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1532 | len = 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1533 | } else { |
| 1534 | arr[0] = 0x70; |
| 1535 | arr[2] = 0x0; /* NO_SENSE in sense_key */ |
| 1536 | arr[7] = 0xa; /* 18 byte sense buffer */ |
| 1537 | arr[12] = THRESHOLD_EXCEEDED; |
| 1538 | arr[13] = 0xff; /* TEST set and MRIE==6 */ |
| 1539 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1540 | } else { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1541 | memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1542 | if (arr[0] >= 0x70 && dsense == sdebug_dsense) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1543 | ; /* have sense and formats match */ |
| 1544 | else if (arr[0] <= 0x70) { |
| 1545 | if (dsense) { |
| 1546 | memset(arr, 0, 8); |
| 1547 | arr[0] = 0x72; |
| 1548 | len = 8; |
| 1549 | } else { |
| 1550 | memset(arr, 0, 18); |
| 1551 | arr[0] = 0x70; |
| 1552 | arr[7] = 0xa; |
| 1553 | } |
| 1554 | } else if (dsense) { |
| 1555 | memset(arr, 0, 8); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1556 | arr[0] = 0x72; |
| 1557 | arr[1] = sbuff[2]; /* sense key */ |
| 1558 | arr[2] = sbuff[12]; /* asc */ |
| 1559 | arr[3] = sbuff[13]; /* ascq */ |
| 1560 | len = 8; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1561 | } else { |
| 1562 | memset(arr, 0, 18); |
| 1563 | arr[0] = 0x70; |
| 1564 | arr[2] = sbuff[1]; |
| 1565 | arr[7] = 0xa; |
| 1566 | arr[12] = sbuff[1]; |
| 1567 | arr[13] = sbuff[3]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1568 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1569 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1570 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1571 | mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | return fill_from_dev_buffer(scp, arr, len); |
| 1573 | } |
| 1574 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1575 | static int resp_start_stop(struct scsi_cmnd * scp, |
| 1576 | struct sdebug_dev_info * devip) |
| 1577 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1578 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 1579 | int power_cond, stop; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1580 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1581 | power_cond = (cmd[4] & 0xf0) >> 4; |
| 1582 | if (power_cond) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1583 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1584 | return check_condition_result; |
| 1585 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 1586 | stop = !(cmd[4] & 1); |
| 1587 | atomic_xchg(&devip->stopped, stop); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1588 | return 0; |
| 1589 | } |
| 1590 | |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1591 | static sector_t get_sdebug_capacity(void) |
| 1592 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1593 | static const unsigned int gibibyte = 1073741824; |
| 1594 | |
| 1595 | if (sdebug_virtual_gb > 0) |
| 1596 | return (sector_t)sdebug_virtual_gb * |
| 1597 | (gibibyte / sdebug_sector_size); |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1598 | else |
| 1599 | return sdebug_store_sectors; |
| 1600 | } |
| 1601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | #define SDEBUG_READCAP_ARR_SZ 8 |
| 1603 | static int resp_readcap(struct scsi_cmnd * scp, |
| 1604 | struct sdebug_dev_info * devip) |
| 1605 | { |
| 1606 | unsigned char arr[SDEBUG_READCAP_ARR_SZ]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1607 | unsigned int capac; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1609 | /* following just in case virtual_gb changed */ |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1610 | sdebug_capacity = get_sdebug_capacity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | memset(arr, 0, SDEBUG_READCAP_ARR_SZ); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1612 | if (sdebug_capacity < 0xffffffff) { |
| 1613 | capac = (unsigned int)sdebug_capacity - 1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1614 | put_unaligned_be32(capac, arr + 0); |
| 1615 | } else |
| 1616 | put_unaligned_be32(0xffffffff, arr + 0); |
| 1617 | put_unaligned_be16(sdebug_sector_size, arr + 6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ); |
| 1619 | } |
| 1620 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1621 | #define SDEBUG_READCAP16_ARR_SZ 32 |
| 1622 | static int resp_readcap16(struct scsi_cmnd * scp, |
| 1623 | struct sdebug_dev_info * devip) |
| 1624 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1625 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1626 | unsigned char arr[SDEBUG_READCAP16_ARR_SZ]; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1627 | int alloc_len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1628 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1629 | alloc_len = get_unaligned_be32(cmd + 10); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1630 | /* following just in case virtual_gb changed */ |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1631 | sdebug_capacity = get_sdebug_capacity(); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1632 | memset(arr, 0, SDEBUG_READCAP16_ARR_SZ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1633 | put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0); |
| 1634 | put_unaligned_be32(sdebug_sector_size, arr + 8); |
| 1635 | arr[13] = sdebug_physblk_exp & 0xf; |
| 1636 | arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1637 | |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1638 | if (scsi_debug_lbp()) { |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1639 | arr[14] |= 0x80; /* LBPME */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1640 | /* from sbc4r07, this LBPRZ field is 1 bit, but the LBPRZ in |
| 1641 | * the LB Provisioning VPD page is 3 bits. Note that lbprz=2 |
| 1642 | * in the wider field maps to 0 in this field. |
| 1643 | */ |
| 1644 | if (sdebug_lbprz & 1) /* precisely what the draft requires */ |
| 1645 | arr[14] |= 0x40; |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1646 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1647 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1648 | arr[15] = sdebug_lowest_aligned & 0xff; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1649 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 1650 | if (have_dif_prot) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1651 | arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */ |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1652 | arr[12] |= 1; /* PROT_EN */ |
| 1653 | } |
| 1654 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1655 | return fill_from_dev_buffer(scp, arr, |
| 1656 | min(alloc_len, SDEBUG_READCAP16_ARR_SZ)); |
| 1657 | } |
| 1658 | |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1659 | #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412 |
| 1660 | |
| 1661 | static int resp_report_tgtpgs(struct scsi_cmnd * scp, |
| 1662 | struct sdebug_dev_info * devip) |
| 1663 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1664 | unsigned char *cmd = scp->cmnd; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1665 | unsigned char * arr; |
| 1666 | int host_no = devip->sdbg_host->shost->host_no; |
| 1667 | int n, ret, alen, rlen; |
| 1668 | int port_group_a, port_group_b, port_a, port_b; |
| 1669 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1670 | alen = get_unaligned_be32(cmd + 6); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 1671 | arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC); |
| 1672 | if (! arr) |
| 1673 | return DID_REQUEUE << 16; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1674 | /* |
| 1675 | * EVPD page 0x88 states we have two ports, one |
| 1676 | * real and a fake port with no device connected. |
| 1677 | * So we create two port groups with one port each |
| 1678 | * and set the group with port B to unavailable. |
| 1679 | */ |
| 1680 | port_a = 0x1; /* relative port A */ |
| 1681 | port_b = 0x2; /* relative port B */ |
| 1682 | port_group_a = (((host_no + 1) & 0x7f) << 8) + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1683 | (devip->channel & 0x7f); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1684 | port_group_b = (((host_no + 1) & 0x7f) << 8) + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1685 | (devip->channel & 0x7f) + 0x80; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1686 | |
| 1687 | /* |
| 1688 | * The asymmetric access state is cycled according to the host_id. |
| 1689 | */ |
| 1690 | n = 4; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1691 | if (sdebug_vpd_use_hostno == 0) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1692 | arr[n++] = host_no % 3; /* Asymm access state */ |
| 1693 | arr[n++] = 0x0F; /* claim: all states are supported */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1694 | } else { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1695 | arr[n++] = 0x0; /* Active/Optimized path */ |
| 1696 | arr[n++] = 0x01; /* only support active/optimized paths */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1697 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1698 | put_unaligned_be16(port_group_a, arr + n); |
| 1699 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1700 | arr[n++] = 0; /* Reserved */ |
| 1701 | arr[n++] = 0; /* Status code */ |
| 1702 | arr[n++] = 0; /* Vendor unique */ |
| 1703 | arr[n++] = 0x1; /* One port per group */ |
| 1704 | arr[n++] = 0; /* Reserved */ |
| 1705 | arr[n++] = 0; /* Reserved */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1706 | put_unaligned_be16(port_a, arr + n); |
| 1707 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1708 | arr[n++] = 3; /* Port unavailable */ |
| 1709 | arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1710 | put_unaligned_be16(port_group_b, arr + n); |
| 1711 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1712 | arr[n++] = 0; /* Reserved */ |
| 1713 | arr[n++] = 0; /* Status code */ |
| 1714 | arr[n++] = 0; /* Vendor unique */ |
| 1715 | arr[n++] = 0x1; /* One port per group */ |
| 1716 | arr[n++] = 0; /* Reserved */ |
| 1717 | arr[n++] = 0; /* Reserved */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1718 | put_unaligned_be16(port_b, arr + n); |
| 1719 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1720 | |
| 1721 | rlen = n - 4; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1722 | put_unaligned_be32(rlen, arr + 0); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1723 | |
| 1724 | /* |
| 1725 | * Return the smallest value of either |
| 1726 | * - The allocated length |
| 1727 | * - The constructed command length |
| 1728 | * - The maximum array size |
| 1729 | */ |
| 1730 | rlen = min(alen,n); |
| 1731 | ret = fill_from_dev_buffer(scp, arr, |
| 1732 | min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ)); |
| 1733 | kfree(arr); |
| 1734 | return ret; |
| 1735 | } |
| 1736 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1737 | static int resp_rsup_opcodes(struct scsi_cmnd *scp, |
| 1738 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1739 | { |
| 1740 | bool rctd; |
| 1741 | u8 reporting_opts, req_opcode, sdeb_i, supp; |
| 1742 | u16 req_sa, u; |
| 1743 | u32 alloc_len, a_len; |
| 1744 | int k, offset, len, errsts, count, bump, na; |
| 1745 | const struct opcode_info_t *oip; |
| 1746 | const struct opcode_info_t *r_oip; |
| 1747 | u8 *arr; |
| 1748 | u8 *cmd = scp->cmnd; |
| 1749 | |
| 1750 | rctd = !!(cmd[2] & 0x80); |
| 1751 | reporting_opts = cmd[2] & 0x7; |
| 1752 | req_opcode = cmd[3]; |
| 1753 | req_sa = get_unaligned_be16(cmd + 4); |
| 1754 | alloc_len = get_unaligned_be32(cmd + 6); |
Colin Ian King | 6d310df | 2015-01-22 11:20:40 +0000 | [diff] [blame] | 1755 | if (alloc_len < 4 || alloc_len > 0xffff) { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1756 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
| 1757 | return check_condition_result; |
| 1758 | } |
| 1759 | if (alloc_len > 8192) |
| 1760 | a_len = 8192; |
| 1761 | else |
| 1762 | a_len = alloc_len; |
Sasha Levin | 99531e6 | 2015-01-17 17:47:37 -0500 | [diff] [blame] | 1763 | arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1764 | if (NULL == arr) { |
| 1765 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 1766 | INSUFF_RES_ASCQ); |
| 1767 | return check_condition_result; |
| 1768 | } |
| 1769 | switch (reporting_opts) { |
| 1770 | case 0: /* all commands */ |
| 1771 | /* count number of commands */ |
| 1772 | for (count = 0, oip = opcode_info_arr; |
| 1773 | oip->num_attached != 0xff; ++oip) { |
| 1774 | if (F_INV_OP & oip->flags) |
| 1775 | continue; |
| 1776 | count += (oip->num_attached + 1); |
| 1777 | } |
| 1778 | bump = rctd ? 20 : 8; |
| 1779 | put_unaligned_be32(count * bump, arr); |
| 1780 | for (offset = 4, oip = opcode_info_arr; |
| 1781 | oip->num_attached != 0xff && offset < a_len; ++oip) { |
| 1782 | if (F_INV_OP & oip->flags) |
| 1783 | continue; |
| 1784 | na = oip->num_attached; |
| 1785 | arr[offset] = oip->opcode; |
| 1786 | put_unaligned_be16(oip->sa, arr + offset + 2); |
| 1787 | if (rctd) |
| 1788 | arr[offset + 5] |= 0x2; |
| 1789 | if (FF_SA & oip->flags) |
| 1790 | arr[offset + 5] |= 0x1; |
| 1791 | put_unaligned_be16(oip->len_mask[0], arr + offset + 6); |
| 1792 | if (rctd) |
| 1793 | put_unaligned_be16(0xa, arr + offset + 8); |
| 1794 | r_oip = oip; |
| 1795 | for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) { |
| 1796 | if (F_INV_OP & oip->flags) |
| 1797 | continue; |
| 1798 | offset += bump; |
| 1799 | arr[offset] = oip->opcode; |
| 1800 | put_unaligned_be16(oip->sa, arr + offset + 2); |
| 1801 | if (rctd) |
| 1802 | arr[offset + 5] |= 0x2; |
| 1803 | if (FF_SA & oip->flags) |
| 1804 | arr[offset + 5] |= 0x1; |
| 1805 | put_unaligned_be16(oip->len_mask[0], |
| 1806 | arr + offset + 6); |
| 1807 | if (rctd) |
| 1808 | put_unaligned_be16(0xa, |
| 1809 | arr + offset + 8); |
| 1810 | } |
| 1811 | oip = r_oip; |
| 1812 | offset += bump; |
| 1813 | } |
| 1814 | break; |
| 1815 | case 1: /* one command: opcode only */ |
| 1816 | case 2: /* one command: opcode plus service action */ |
| 1817 | case 3: /* one command: if sa==0 then opcode only else opcode+sa */ |
| 1818 | sdeb_i = opcode_ind_arr[req_opcode]; |
| 1819 | oip = &opcode_info_arr[sdeb_i]; |
| 1820 | if (F_INV_OP & oip->flags) { |
| 1821 | supp = 1; |
| 1822 | offset = 4; |
| 1823 | } else { |
| 1824 | if (1 == reporting_opts) { |
| 1825 | if (FF_SA & oip->flags) { |
| 1826 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, |
| 1827 | 2, 2); |
| 1828 | kfree(arr); |
| 1829 | return check_condition_result; |
| 1830 | } |
| 1831 | req_sa = 0; |
| 1832 | } else if (2 == reporting_opts && |
| 1833 | 0 == (FF_SA & oip->flags)) { |
| 1834 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1); |
| 1835 | kfree(arr); /* point at requested sa */ |
| 1836 | return check_condition_result; |
| 1837 | } |
| 1838 | if (0 == (FF_SA & oip->flags) && |
| 1839 | req_opcode == oip->opcode) |
| 1840 | supp = 3; |
| 1841 | else if (0 == (FF_SA & oip->flags)) { |
| 1842 | na = oip->num_attached; |
| 1843 | for (k = 0, oip = oip->arrp; k < na; |
| 1844 | ++k, ++oip) { |
| 1845 | if (req_opcode == oip->opcode) |
| 1846 | break; |
| 1847 | } |
| 1848 | supp = (k >= na) ? 1 : 3; |
| 1849 | } else if (req_sa != oip->sa) { |
| 1850 | na = oip->num_attached; |
| 1851 | for (k = 0, oip = oip->arrp; k < na; |
| 1852 | ++k, ++oip) { |
| 1853 | if (req_sa == oip->sa) |
| 1854 | break; |
| 1855 | } |
| 1856 | supp = (k >= na) ? 1 : 3; |
| 1857 | } else |
| 1858 | supp = 3; |
| 1859 | if (3 == supp) { |
| 1860 | u = oip->len_mask[0]; |
| 1861 | put_unaligned_be16(u, arr + 2); |
| 1862 | arr[4] = oip->opcode; |
| 1863 | for (k = 1; k < u; ++k) |
| 1864 | arr[4 + k] = (k < 16) ? |
| 1865 | oip->len_mask[k] : 0xff; |
| 1866 | offset = 4 + u; |
| 1867 | } else |
| 1868 | offset = 4; |
| 1869 | } |
| 1870 | arr[1] = (rctd ? 0x80 : 0) | supp; |
| 1871 | if (rctd) { |
| 1872 | put_unaligned_be16(0xa, arr + offset); |
| 1873 | offset += 12; |
| 1874 | } |
| 1875 | break; |
| 1876 | default: |
| 1877 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2); |
| 1878 | kfree(arr); |
| 1879 | return check_condition_result; |
| 1880 | } |
| 1881 | offset = (offset < a_len) ? offset : a_len; |
| 1882 | len = (offset < alloc_len) ? offset : alloc_len; |
| 1883 | errsts = fill_from_dev_buffer(scp, arr, len); |
| 1884 | kfree(arr); |
| 1885 | return errsts; |
| 1886 | } |
| 1887 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1888 | static int resp_rsup_tmfs(struct scsi_cmnd *scp, |
| 1889 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1890 | { |
| 1891 | bool repd; |
| 1892 | u32 alloc_len, len; |
| 1893 | u8 arr[16]; |
| 1894 | u8 *cmd = scp->cmnd; |
| 1895 | |
| 1896 | memset(arr, 0, sizeof(arr)); |
| 1897 | repd = !!(cmd[2] & 0x80); |
| 1898 | alloc_len = get_unaligned_be32(cmd + 6); |
| 1899 | if (alloc_len < 4) { |
| 1900 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
| 1901 | return check_condition_result; |
| 1902 | } |
| 1903 | arr[0] = 0xc8; /* ATS | ATSS | LURS */ |
| 1904 | arr[1] = 0x1; /* ITNRS */ |
| 1905 | if (repd) { |
| 1906 | arr[3] = 0xc; |
| 1907 | len = 16; |
| 1908 | } else |
| 1909 | len = 4; |
| 1910 | |
| 1911 | len = (len < alloc_len) ? len : alloc_len; |
| 1912 | return fill_from_dev_buffer(scp, arr, len); |
| 1913 | } |
| 1914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | /* <<Following mode page info copied from ST318451LW>> */ |
| 1916 | |
| 1917 | static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target) |
| 1918 | { /* Read-Write Error Recovery page for mode_sense */ |
| 1919 | unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0, |
| 1920 | 5, 0, 0xff, 0xff}; |
| 1921 | |
| 1922 | memcpy(p, err_recov_pg, sizeof(err_recov_pg)); |
| 1923 | if (1 == pcontrol) |
| 1924 | memset(p + 2, 0, sizeof(err_recov_pg) - 2); |
| 1925 | return sizeof(err_recov_pg); |
| 1926 | } |
| 1927 | |
| 1928 | static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target) |
| 1929 | { /* Disconnect-Reconnect page for mode_sense */ |
| 1930 | unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0, |
| 1931 | 0, 0, 0, 0, 0, 0, 0, 0}; |
| 1932 | |
| 1933 | memcpy(p, disconnect_pg, sizeof(disconnect_pg)); |
| 1934 | if (1 == pcontrol) |
| 1935 | memset(p + 2, 0, sizeof(disconnect_pg) - 2); |
| 1936 | return sizeof(disconnect_pg); |
| 1937 | } |
| 1938 | |
| 1939 | static int resp_format_pg(unsigned char * p, int pcontrol, int target) |
| 1940 | { /* Format device page for mode_sense */ |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1941 | unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0, |
| 1942 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1943 | 0, 0, 0, 0, 0x40, 0, 0, 0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1945 | memcpy(p, format_pg, sizeof(format_pg)); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1946 | put_unaligned_be16(sdebug_sectors_per, p + 10); |
| 1947 | put_unaligned_be16(sdebug_sector_size, p + 12); |
| 1948 | if (sdebug_removable) |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1949 | p[20] |= 0x20; /* should agree with INQUIRY */ |
| 1950 | if (1 == pcontrol) |
| 1951 | memset(p + 2, 0, sizeof(format_pg) - 2); |
| 1952 | return sizeof(format_pg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | } |
| 1954 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1955 | static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0, |
| 1956 | 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, |
| 1957 | 0, 0, 0, 0}; |
| 1958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | static int resp_caching_pg(unsigned char * p, int pcontrol, int target) |
| 1960 | { /* Caching page for mode_sense */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1961 | unsigned char ch_caching_pg[] = {/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0, |
| 1962 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 1963 | 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] | 1964 | 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0}; |
| 1965 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1966 | if (SDEBUG_OPT_N_WCE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1967 | caching_pg[2] &= ~0x4; /* set WCE=0 (default WCE=1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | memcpy(p, caching_pg, sizeof(caching_pg)); |
| 1969 | if (1 == pcontrol) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1970 | memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg)); |
| 1971 | else if (2 == pcontrol) |
| 1972 | memcpy(p, d_caching_pg, sizeof(d_caching_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | return sizeof(caching_pg); |
| 1974 | } |
| 1975 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1976 | static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0, |
| 1977 | 0, 0, 0x2, 0x4b}; |
| 1978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target) |
| 1980 | { /* Control mode page for mode_sense */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1981 | 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] | 1982 | 0, 0, 0, 0}; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1983 | 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] | 1984 | 0, 0, 0x2, 0x4b}; |
| 1985 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1986 | if (sdebug_dsense) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | ctrl_m_pg[2] |= 0x4; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1988 | else |
| 1989 | ctrl_m_pg[2] &= ~0x4; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1990 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1991 | if (sdebug_ato) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1992 | ctrl_m_pg[5] |= 0x80; /* ATO=1 */ |
| 1993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg)); |
| 1995 | if (1 == pcontrol) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1996 | memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg)); |
| 1997 | else if (2 == pcontrol) |
| 1998 | memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | return sizeof(ctrl_m_pg); |
| 2000 | } |
| 2001 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2002 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target) |
| 2004 | { /* Informational Exceptions control mode page for mode_sense */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2005 | unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0, |
| 2006 | 0, 0, 0x0, 0x0}; |
| 2007 | unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, |
| 2008 | 0, 0, 0x0, 0x0}; |
| 2009 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | memcpy(p, iec_m_pg, sizeof(iec_m_pg)); |
| 2011 | if (1 == pcontrol) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2012 | memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg)); |
| 2013 | else if (2 == pcontrol) |
| 2014 | memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | return sizeof(iec_m_pg); |
| 2016 | } |
| 2017 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2018 | static int resp_sas_sf_m_pg(unsigned char * p, int pcontrol, int target) |
| 2019 | { /* SAS SSP mode page - short format for mode_sense */ |
| 2020 | unsigned char sas_sf_m_pg[] = {0x19, 0x6, |
| 2021 | 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0}; |
| 2022 | |
| 2023 | memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg)); |
| 2024 | if (1 == pcontrol) |
| 2025 | memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2); |
| 2026 | return sizeof(sas_sf_m_pg); |
| 2027 | } |
| 2028 | |
| 2029 | |
| 2030 | static int resp_sas_pcd_m_spg(unsigned char * p, int pcontrol, int target, |
| 2031 | int target_dev_id) |
| 2032 | { /* SAS phy control and discover mode page for mode_sense */ |
| 2033 | unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2, |
| 2034 | 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2035 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
| 2036 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2037 | 0x2, 0, 0, 0, 0, 0, 0, 0, |
| 2038 | 0x88, 0x99, 0, 0, 0, 0, 0, 0, |
| 2039 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 2040 | 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2041 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
| 2042 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2043 | 0x3, 0, 0, 0, 0, 0, 0, 0, |
| 2044 | 0x88, 0x99, 0, 0, 0, 0, 0, 0, |
| 2045 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 2046 | }; |
| 2047 | int port_a, port_b; |
| 2048 | |
Douglas Gilbert | 1b37bd6 | 2016-05-06 00:40:29 -0400 | [diff] [blame] | 2049 | put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 16); |
| 2050 | put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 24); |
| 2051 | put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 64); |
| 2052 | put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 72); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2053 | port_a = target_dev_id + 1; |
| 2054 | port_b = port_a + 1; |
| 2055 | memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg)); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2056 | put_unaligned_be32(port_a, p + 20); |
| 2057 | put_unaligned_be32(port_b, p + 48 + 20); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2058 | if (1 == pcontrol) |
| 2059 | memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4); |
| 2060 | return sizeof(sas_pcd_m_pg); |
| 2061 | } |
| 2062 | |
| 2063 | static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol) |
| 2064 | { /* SAS SSP shared protocol specific port mode subpage */ |
| 2065 | unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0, |
| 2066 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 2067 | }; |
| 2068 | |
| 2069 | memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg)); |
| 2070 | if (1 == pcontrol) |
| 2071 | memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4); |
| 2072 | return sizeof(sas_sha_m_pg); |
| 2073 | } |
| 2074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | #define SDEBUG_MAX_MSENSE_SZ 256 |
| 2076 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2077 | static int resp_mode_sense(struct scsi_cmnd *scp, |
| 2078 | struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | { |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2080 | int pcontrol, pcode, subpcode, bd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | unsigned char dev_spec; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2082 | int alloc_len, offset, len, target_dev_id; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2083 | int target = scp->device->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | unsigned char * ap; |
| 2085 | unsigned char arr[SDEBUG_MAX_MSENSE_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2086 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2087 | bool dbd, llbaa, msense_6, is_disk, bad_pcode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2089 | dbd = !!(cmd[1] & 0x8); /* disable block descriptors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | pcontrol = (cmd[2] & 0xc0) >> 6; |
| 2091 | pcode = cmd[2] & 0x3f; |
| 2092 | subpcode = cmd[3]; |
| 2093 | msense_6 = (MODE_SENSE == cmd[0]); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2094 | llbaa = msense_6 ? false : !!(cmd[1] & 0x10); |
| 2095 | is_disk = (sdebug_ptype == TYPE_DISK); |
| 2096 | if (is_disk && !dbd) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2097 | bd_len = llbaa ? 16 : 8; |
| 2098 | else |
| 2099 | bd_len = 0; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2100 | alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | memset(arr, 0, SDEBUG_MAX_MSENSE_SZ); |
| 2102 | if (0x3 == pcontrol) { /* Saving values not supported */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2103 | mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | return check_condition_result; |
| 2105 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2106 | target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) + |
| 2107 | (devip->target * 1000) - 3; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 2108 | /* for disks set DPOFUA bit and clear write protect (WP) bit */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2109 | if (is_disk) |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 2110 | dev_spec = 0x10; /* =0x90 if WP=1 implies read-only */ |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2111 | else |
| 2112 | dev_spec = 0x0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | if (msense_6) { |
| 2114 | arr[2] = dev_spec; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2115 | arr[3] = bd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | offset = 4; |
| 2117 | } else { |
| 2118 | arr[3] = dev_spec; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2119 | if (16 == bd_len) |
| 2120 | arr[4] = 0x1; /* set LONGLBA bit */ |
| 2121 | arr[7] = bd_len; /* assume 255 or less */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | offset = 8; |
| 2123 | } |
| 2124 | ap = arr + offset; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 2125 | if ((bd_len > 0) && (!sdebug_capacity)) |
| 2126 | sdebug_capacity = get_sdebug_capacity(); |
| 2127 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2128 | if (8 == bd_len) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2129 | if (sdebug_capacity > 0xfffffffe) |
| 2130 | put_unaligned_be32(0xffffffff, ap + 0); |
| 2131 | else |
| 2132 | put_unaligned_be32(sdebug_capacity, ap + 0); |
| 2133 | put_unaligned_be16(sdebug_sector_size, ap + 6); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2134 | offset += bd_len; |
| 2135 | ap = arr + offset; |
| 2136 | } else if (16 == bd_len) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2137 | put_unaligned_be64((u64)sdebug_capacity, ap + 0); |
| 2138 | put_unaligned_be32(sdebug_sector_size, ap + 12); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2139 | offset += bd_len; |
| 2140 | ap = arr + offset; |
| 2141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2143 | if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) { |
| 2144 | /* TODO: Control Extension page */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2145 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | return check_condition_result; |
| 2147 | } |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2148 | bad_pcode = false; |
| 2149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | switch (pcode) { |
| 2151 | case 0x1: /* Read-Write error recovery page, direct access */ |
| 2152 | len = resp_err_recov_pg(ap, pcontrol, target); |
| 2153 | offset += len; |
| 2154 | break; |
| 2155 | case 0x2: /* Disconnect-Reconnect page, all devices */ |
| 2156 | len = resp_disconnect_pg(ap, pcontrol, target); |
| 2157 | offset += len; |
| 2158 | break; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2159 | case 0x3: /* Format device page, direct access */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2160 | if (is_disk) { |
| 2161 | len = resp_format_pg(ap, pcontrol, target); |
| 2162 | offset += len; |
| 2163 | } else |
| 2164 | bad_pcode = true; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2165 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | case 0x8: /* Caching page, direct access */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2167 | if (is_disk) { |
| 2168 | len = resp_caching_pg(ap, pcontrol, target); |
| 2169 | offset += len; |
| 2170 | } else |
| 2171 | bad_pcode = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | break; |
| 2173 | case 0xa: /* Control Mode page, all devices */ |
| 2174 | len = resp_ctrl_m_pg(ap, pcontrol, target); |
| 2175 | offset += len; |
| 2176 | break; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2177 | case 0x19: /* if spc==1 then sas phy, control+discover */ |
| 2178 | if ((subpcode > 0x2) && (subpcode < 0xff)) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2179 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2180 | return check_condition_result; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2181 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2182 | len = 0; |
| 2183 | if ((0x0 == subpcode) || (0xff == subpcode)) |
| 2184 | len += resp_sas_sf_m_pg(ap + len, pcontrol, target); |
| 2185 | if ((0x1 == subpcode) || (0xff == subpcode)) |
| 2186 | len += resp_sas_pcd_m_spg(ap + len, pcontrol, target, |
| 2187 | target_dev_id); |
| 2188 | if ((0x2 == subpcode) || (0xff == subpcode)) |
| 2189 | len += resp_sas_sha_m_spg(ap + len, pcontrol); |
| 2190 | offset += len; |
| 2191 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | case 0x1c: /* Informational Exceptions Mode page, all devices */ |
| 2193 | len = resp_iec_m_pg(ap, pcontrol, target); |
| 2194 | offset += len; |
| 2195 | break; |
| 2196 | case 0x3f: /* Read all Mode pages */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2197 | if ((0 == subpcode) || (0xff == subpcode)) { |
| 2198 | len = resp_err_recov_pg(ap, pcontrol, target); |
| 2199 | len += resp_disconnect_pg(ap + len, pcontrol, target); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2200 | if (is_disk) { |
| 2201 | len += resp_format_pg(ap + len, pcontrol, |
| 2202 | target); |
| 2203 | len += resp_caching_pg(ap + len, pcontrol, |
| 2204 | target); |
| 2205 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2206 | len += resp_ctrl_m_pg(ap + len, pcontrol, target); |
| 2207 | len += resp_sas_sf_m_pg(ap + len, pcontrol, target); |
| 2208 | if (0xff == subpcode) { |
| 2209 | len += resp_sas_pcd_m_spg(ap + len, pcontrol, |
| 2210 | target, target_dev_id); |
| 2211 | len += resp_sas_sha_m_spg(ap + len, pcontrol); |
| 2212 | } |
| 2213 | len += resp_iec_m_pg(ap + len, pcontrol, target); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2214 | offset += len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2215 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2216 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2217 | return check_condition_result; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | break; |
| 2220 | default: |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2221 | bad_pcode = true; |
| 2222 | break; |
| 2223 | } |
| 2224 | if (bad_pcode) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2225 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | return check_condition_result; |
| 2227 | } |
| 2228 | if (msense_6) |
| 2229 | arr[0] = offset - 1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2230 | else |
| 2231 | put_unaligned_be16((offset - 2), arr + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | return fill_from_dev_buffer(scp, arr, min(alloc_len, offset)); |
| 2233 | } |
| 2234 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2235 | #define SDEBUG_MAX_MSELECT_SZ 512 |
| 2236 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2237 | static int resp_mode_select(struct scsi_cmnd *scp, |
| 2238 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2239 | { |
| 2240 | int pf, sp, ps, md_len, bd_len, off, spf, pg_len; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2241 | int param_len, res, mpage; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2242 | unsigned char arr[SDEBUG_MAX_MSELECT_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2243 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2244 | int mselect6 = (MODE_SELECT == cmd[0]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2245 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2246 | memset(arr, 0, sizeof(arr)); |
| 2247 | pf = cmd[1] & 0x10; |
| 2248 | sp = cmd[1] & 0x1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2249 | param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2250 | if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2251 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2252 | return check_condition_result; |
| 2253 | } |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2254 | res = fetch_to_dev_buffer(scp, arr, param_len); |
| 2255 | if (-1 == res) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2256 | return DID_ERROR << 16; |
| 2257 | else if (sdebug_verbose && (res < param_len)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2258 | sdev_printk(KERN_INFO, scp->device, |
| 2259 | "%s: cdb indicated=%d, IO sent=%d bytes\n", |
| 2260 | __func__, param_len, res); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2261 | md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2); |
| 2262 | bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2263 | if (md_len > 2) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2264 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2265 | return check_condition_result; |
| 2266 | } |
| 2267 | off = bd_len + (mselect6 ? 4 : 8); |
| 2268 | mpage = arr[off] & 0x3f; |
| 2269 | ps = !!(arr[off] & 0x80); |
| 2270 | if (ps) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2271 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2272 | return check_condition_result; |
| 2273 | } |
| 2274 | spf = !!(arr[off] & 0x40); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2275 | pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) : |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2276 | (arr[off + 1] + 2); |
| 2277 | if ((pg_len + off) > param_len) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2278 | mk_sense_buffer(scp, ILLEGAL_REQUEST, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2279 | PARAMETER_LIST_LENGTH_ERR, 0); |
| 2280 | return check_condition_result; |
| 2281 | } |
| 2282 | switch (mpage) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2283 | case 0x8: /* Caching Mode page */ |
| 2284 | if (caching_pg[1] == arr[off + 1]) { |
| 2285 | memcpy(caching_pg + 2, arr + off + 2, |
| 2286 | sizeof(caching_pg) - 2); |
| 2287 | goto set_mode_changed_ua; |
| 2288 | } |
| 2289 | break; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2290 | case 0xa: /* Control Mode page */ |
| 2291 | if (ctrl_m_pg[1] == arr[off + 1]) { |
| 2292 | memcpy(ctrl_m_pg + 2, arr + off + 2, |
| 2293 | sizeof(ctrl_m_pg) - 2); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2294 | sdebug_dsense = !!(ctrl_m_pg[2] & 0x4); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2295 | goto set_mode_changed_ua; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2296 | } |
| 2297 | break; |
| 2298 | case 0x1c: /* Informational Exceptions Mode page */ |
| 2299 | if (iec_m_pg[1] == arr[off + 1]) { |
| 2300 | memcpy(iec_m_pg + 2, arr + off + 2, |
| 2301 | sizeof(iec_m_pg) - 2); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2302 | goto set_mode_changed_ua; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2303 | } |
| 2304 | break; |
| 2305 | default: |
| 2306 | break; |
| 2307 | } |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2308 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2309 | return check_condition_result; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2310 | set_mode_changed_ua: |
| 2311 | set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm); |
| 2312 | return 0; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | static int resp_temp_l_pg(unsigned char * arr) |
| 2316 | { |
| 2317 | unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38, |
| 2318 | 0x0, 0x1, 0x3, 0x2, 0x0, 65, |
| 2319 | }; |
| 2320 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2321 | memcpy(arr, temp_l_pg, sizeof(temp_l_pg)); |
| 2322 | return sizeof(temp_l_pg); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | static int resp_ie_l_pg(unsigned char * arr) |
| 2326 | { |
| 2327 | unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38, |
| 2328 | }; |
| 2329 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2330 | memcpy(arr, ie_l_pg, sizeof(ie_l_pg)); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2331 | if (iec_m_pg[2] & 0x4) { /* TEST bit set */ |
| 2332 | arr[4] = THRESHOLD_EXCEEDED; |
| 2333 | arr[5] = 0xff; |
| 2334 | } |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2335 | return sizeof(ie_l_pg); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | #define SDEBUG_MAX_LSENSE_SZ 512 |
| 2339 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 2340 | static int resp_log_sense(struct scsi_cmnd *scp, |
| 2341 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2342 | { |
Bart Van Assche | ab17241 | 2017-08-25 13:46:42 -0700 | [diff] [blame] | 2343 | int ppc, sp, pcode, subpcode, alloc_len, len, n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2344 | unsigned char arr[SDEBUG_MAX_LSENSE_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2345 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2346 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2347 | memset(arr, 0, sizeof(arr)); |
| 2348 | ppc = cmd[1] & 0x2; |
| 2349 | sp = cmd[1] & 0x1; |
| 2350 | if (ppc || sp) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2351 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2352 | return check_condition_result; |
| 2353 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2354 | pcode = cmd[2] & 0x3f; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2355 | subpcode = cmd[3] & 0xff; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2356 | alloc_len = get_unaligned_be16(cmd + 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2357 | arr[0] = pcode; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2358 | if (0 == subpcode) { |
| 2359 | switch (pcode) { |
| 2360 | case 0x0: /* Supported log pages log page */ |
| 2361 | n = 4; |
| 2362 | arr[n++] = 0x0; /* this page */ |
| 2363 | arr[n++] = 0xd; /* Temperature */ |
| 2364 | arr[n++] = 0x2f; /* Informational exceptions */ |
| 2365 | arr[3] = n - 4; |
| 2366 | break; |
| 2367 | case 0xd: /* Temperature log page */ |
| 2368 | arr[3] = resp_temp_l_pg(arr + 4); |
| 2369 | break; |
| 2370 | case 0x2f: /* Informational exceptions log page */ |
| 2371 | arr[3] = resp_ie_l_pg(arr + 4); |
| 2372 | break; |
| 2373 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2374 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2375 | return check_condition_result; |
| 2376 | } |
| 2377 | } else if (0xff == subpcode) { |
| 2378 | arr[0] |= 0x40; |
| 2379 | arr[1] = subpcode; |
| 2380 | switch (pcode) { |
| 2381 | case 0x0: /* Supported log pages and subpages log page */ |
| 2382 | n = 4; |
| 2383 | arr[n++] = 0x0; |
| 2384 | arr[n++] = 0x0; /* 0,0 page */ |
| 2385 | arr[n++] = 0x0; |
| 2386 | arr[n++] = 0xff; /* this page */ |
| 2387 | arr[n++] = 0xd; |
| 2388 | arr[n++] = 0x0; /* Temperature */ |
| 2389 | arr[n++] = 0x2f; |
| 2390 | arr[n++] = 0x0; /* Informational exceptions */ |
| 2391 | arr[3] = n - 4; |
| 2392 | break; |
| 2393 | case 0xd: /* Temperature subpages */ |
| 2394 | n = 4; |
| 2395 | arr[n++] = 0xd; |
| 2396 | arr[n++] = 0x0; /* Temperature */ |
| 2397 | arr[3] = n - 4; |
| 2398 | break; |
| 2399 | case 0x2f: /* Informational exceptions subpages */ |
| 2400 | n = 4; |
| 2401 | arr[n++] = 0x2f; |
| 2402 | arr[n++] = 0x0; /* Informational exceptions */ |
| 2403 | arr[3] = n - 4; |
| 2404 | break; |
| 2405 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2406 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2407 | return check_condition_result; |
| 2408 | } |
| 2409 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2410 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2411 | return check_condition_result; |
| 2412 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2413 | len = min(get_unaligned_be16(arr + 2) + 4, alloc_len); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2414 | return fill_from_dev_buffer(scp, arr, |
| 2415 | min(len, SDEBUG_MAX_INQ_ARR_SZ)); |
| 2416 | } |
| 2417 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2418 | static int check_device_access_params(struct scsi_cmnd *scp, |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2419 | unsigned long long lba, unsigned int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2421 | if (lba + num > sdebug_capacity) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2422 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | return check_condition_result; |
| 2424 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2425 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 2426 | if (num > sdebug_store_sectors) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2427 | /* needs work to find which cdb byte 'num' comes from */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2428 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2429 | return check_condition_result; |
| 2430 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2431 | return 0; |
| 2432 | } |
| 2433 | |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2434 | /* Returns number of bytes copied or -1 if error. */ |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 2435 | static int do_device_access(struct scsi_cmnd *scmd, u32 sg_skip, u64 lba, |
| 2436 | u32 num, bool do_write) |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2437 | { |
| 2438 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2439 | u64 block, rest = 0; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2440 | struct scsi_data_buffer *sdb; |
| 2441 | enum dma_data_direction dir; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2442 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2443 | if (do_write) { |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2444 | sdb = scsi_out(scmd); |
| 2445 | dir = DMA_TO_DEVICE; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2446 | } else { |
| 2447 | sdb = scsi_in(scmd); |
| 2448 | dir = DMA_FROM_DEVICE; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | if (!sdb->length) |
| 2452 | return 0; |
| 2453 | if (!(scsi_bidi_cmnd(scmd) || scmd->sc_data_direction == dir)) |
| 2454 | return -1; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2455 | |
| 2456 | block = do_div(lba, sdebug_store_sectors); |
| 2457 | if (block + num > sdebug_store_sectors) |
| 2458 | rest = block + num - sdebug_store_sectors; |
| 2459 | |
Dave Gordon | 386ecb1 | 2015-06-30 14:58:57 -0700 | [diff] [blame] | 2460 | ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2461 | fake_storep + (block * sdebug_sector_size), |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 2462 | (num - rest) * sdebug_sector_size, sg_skip, do_write); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2463 | if (ret != (num - rest) * sdebug_sector_size) |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2464 | return ret; |
| 2465 | |
| 2466 | if (rest) { |
Dave Gordon | 386ecb1 | 2015-06-30 14:58:57 -0700 | [diff] [blame] | 2467 | ret += sg_copy_buffer(sdb->table.sgl, sdb->table.nents, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2468 | fake_storep, rest * sdebug_sector_size, |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 2469 | sg_skip + ((num - rest) * sdebug_sector_size), |
| 2470 | do_write); |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2471 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2472 | |
| 2473 | return ret; |
| 2474 | } |
| 2475 | |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2476 | /* If fake_store(lba,num) compares equal to arr(num), then copy top half of |
| 2477 | * arr into fake_store(lba,num) and return true. If comparison fails then |
| 2478 | * return false. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2479 | static bool comp_write_worker(u64 lba, u32 num, const u8 *arr) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2480 | { |
| 2481 | bool res; |
| 2482 | u64 block, rest = 0; |
| 2483 | u32 store_blks = sdebug_store_sectors; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2484 | u32 lb_size = sdebug_sector_size; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2485 | |
| 2486 | block = do_div(lba, store_blks); |
| 2487 | if (block + num > store_blks) |
| 2488 | rest = block + num - store_blks; |
| 2489 | |
| 2490 | res = !memcmp(fake_storep + (block * lb_size), arr, |
| 2491 | (num - rest) * lb_size); |
| 2492 | if (!res) |
| 2493 | return res; |
| 2494 | if (rest) |
| 2495 | res = memcmp(fake_storep, arr + ((num - rest) * lb_size), |
| 2496 | rest * lb_size); |
| 2497 | if (!res) |
| 2498 | return res; |
| 2499 | arr += num * lb_size; |
| 2500 | memcpy(fake_storep + (block * lb_size), arr, (num - rest) * lb_size); |
| 2501 | if (rest) |
| 2502 | memcpy(fake_storep, arr + ((num - rest) * lb_size), |
| 2503 | rest * lb_size); |
| 2504 | return res; |
| 2505 | } |
| 2506 | |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2507 | static __be16 dif_compute_csum(const void *buf, int len) |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2508 | { |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2509 | __be16 csum; |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2510 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2511 | if (sdebug_guard) |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2512 | csum = (__force __be16)ip_compute_csum(buf, len); |
| 2513 | else |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2514 | csum = cpu_to_be16(crc_t10dif(buf, len)); |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2515 | |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2516 | return csum; |
| 2517 | } |
| 2518 | |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 2519 | static int dif_verify(struct t10_pi_tuple *sdt, const void *data, |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2520 | sector_t sector, u32 ei_lba) |
| 2521 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2522 | __be16 csum = dif_compute_csum(data, sdebug_sector_size); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2523 | |
| 2524 | if (sdt->guard_tag != csum) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2525 | 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] | 2526 | (unsigned long)sector, |
| 2527 | be16_to_cpu(sdt->guard_tag), |
| 2528 | be16_to_cpu(csum)); |
| 2529 | return 0x01; |
| 2530 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2531 | if (sdebug_dif == T10_PI_TYPE1_PROTECTION && |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2532 | be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2533 | pr_err("REF check failed on sector %lu\n", |
| 2534 | (unsigned long)sector); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2535 | return 0x03; |
| 2536 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2537 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2538 | be32_to_cpu(sdt->ref_tag) != ei_lba) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2539 | pr_err("REF check failed on sector %lu\n", |
| 2540 | (unsigned long)sector); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2541 | return 0x03; |
| 2542 | } |
| 2543 | return 0; |
| 2544 | } |
| 2545 | |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2546 | static void dif_copy_prot(struct scsi_cmnd *SCpnt, sector_t sector, |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2547 | unsigned int sectors, bool read) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2548 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2549 | size_t resid; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2550 | void *paddr; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2551 | const void *dif_store_end = dif_storep + sdebug_store_sectors; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2552 | struct sg_mapping_iter miter; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2553 | |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 2554 | /* Bytes of protection data to copy into sgl */ |
| 2555 | resid = sectors * sizeof(*dif_storep); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2556 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2557 | sg_miter_start(&miter, scsi_prot_sglist(SCpnt), |
| 2558 | scsi_prot_sg_count(SCpnt), SG_MITER_ATOMIC | |
| 2559 | (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG)); |
| 2560 | |
| 2561 | while (sg_miter_next(&miter) && resid > 0) { |
| 2562 | size_t len = min(miter.length, resid); |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2563 | void *start = dif_store(sector); |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2564 | size_t rest = 0; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2565 | |
| 2566 | if (dif_store_end < start + len) |
| 2567 | rest = start + len - dif_store_end; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2568 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2569 | paddr = miter.addr; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2570 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2571 | if (read) |
| 2572 | memcpy(paddr, start, len - rest); |
| 2573 | else |
| 2574 | memcpy(start, paddr, len - rest); |
| 2575 | |
| 2576 | if (rest) { |
| 2577 | if (read) |
| 2578 | memcpy(paddr + len - rest, dif_storep, rest); |
| 2579 | else |
| 2580 | memcpy(dif_storep, paddr + len - rest, rest); |
| 2581 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2582 | |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 2583 | sector += len / sizeof(*dif_storep); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2584 | resid -= len; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2585 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2586 | sg_miter_stop(&miter); |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2587 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2588 | |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2589 | static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec, |
| 2590 | unsigned int sectors, u32 ei_lba) |
| 2591 | { |
| 2592 | unsigned int i; |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 2593 | struct t10_pi_tuple *sdt; |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2594 | sector_t sector; |
| 2595 | |
Akinobu Mita | c45eabec | 2014-02-26 22:56:58 +0900 | [diff] [blame] | 2596 | for (i = 0; i < sectors; i++, ei_lba++) { |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2597 | int ret; |
| 2598 | |
| 2599 | sector = start_sec + i; |
| 2600 | sdt = dif_store(sector); |
| 2601 | |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2602 | if (sdt->app_tag == cpu_to_be16(0xffff)) |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2603 | continue; |
| 2604 | |
| 2605 | ret = dif_verify(sdt, fake_store(sector), sector, ei_lba); |
| 2606 | if (ret) { |
| 2607 | dif_errors++; |
| 2608 | return ret; |
| 2609 | } |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2610 | } |
| 2611 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2612 | dif_copy_prot(SCpnt, start_sec, sectors, true); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2613 | dix_reads++; |
| 2614 | |
| 2615 | return 0; |
| 2616 | } |
| 2617 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2618 | 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] | 2619 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2620 | u8 *cmd = scp->cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2621 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2622 | u64 lba; |
| 2623 | u32 num; |
| 2624 | u32 ei_lba; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2625 | unsigned long iflags; |
| 2626 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2627 | bool check_prot; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2628 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2629 | switch (cmd[0]) { |
| 2630 | case READ_16: |
| 2631 | ei_lba = 0; |
| 2632 | lba = get_unaligned_be64(cmd + 2); |
| 2633 | num = get_unaligned_be32(cmd + 10); |
| 2634 | check_prot = true; |
| 2635 | break; |
| 2636 | case READ_10: |
| 2637 | ei_lba = 0; |
| 2638 | lba = get_unaligned_be32(cmd + 2); |
| 2639 | num = get_unaligned_be16(cmd + 7); |
| 2640 | check_prot = true; |
| 2641 | break; |
| 2642 | case READ_6: |
| 2643 | ei_lba = 0; |
| 2644 | lba = (u32)cmd[3] | (u32)cmd[2] << 8 | |
| 2645 | (u32)(cmd[1] & 0x1f) << 16; |
| 2646 | num = (0 == cmd[4]) ? 256 : cmd[4]; |
| 2647 | check_prot = true; |
| 2648 | break; |
| 2649 | case READ_12: |
| 2650 | ei_lba = 0; |
| 2651 | lba = get_unaligned_be32(cmd + 2); |
| 2652 | num = get_unaligned_be32(cmd + 6); |
| 2653 | check_prot = true; |
| 2654 | break; |
| 2655 | case XDWRITEREAD_10: |
| 2656 | ei_lba = 0; |
| 2657 | lba = get_unaligned_be32(cmd + 2); |
| 2658 | num = get_unaligned_be16(cmd + 7); |
| 2659 | check_prot = false; |
| 2660 | break; |
| 2661 | default: /* assume READ(32) */ |
| 2662 | lba = get_unaligned_be64(cmd + 12); |
| 2663 | ei_lba = get_unaligned_be32(cmd + 20); |
| 2664 | num = get_unaligned_be32(cmd + 28); |
| 2665 | check_prot = false; |
| 2666 | break; |
| 2667 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2668 | if (unlikely(have_dif_prot && check_prot)) { |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2669 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2670 | (cmd[1] & 0xe0)) { |
| 2671 | mk_sense_invalid_opcode(scp); |
| 2672 | return check_condition_result; |
| 2673 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2674 | if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || |
| 2675 | sdebug_dif == T10_PI_TYPE3_PROTECTION) && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2676 | (cmd[1] & 0xe0) == 0) |
| 2677 | sdev_printk(KERN_ERR, scp->device, "Unprotected RD " |
| 2678 | "to DIF device\n"); |
| 2679 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2680 | if (unlikely(sdebug_any_injecting_opt)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2681 | sqcp = (struct sdebug_queued_cmd *)scp->host_scribble; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2682 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2683 | if (sqcp) { |
| 2684 | if (sqcp->inj_short) |
| 2685 | num /= 2; |
| 2686 | } |
| 2687 | } else |
| 2688 | sqcp = NULL; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2689 | |
| 2690 | /* inline check_device_access_params() */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2691 | if (unlikely(lba + num > sdebug_capacity)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2692 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 2693 | return check_condition_result; |
| 2694 | } |
| 2695 | /* transfer length excessive (tie in to block limits VPD page) */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2696 | if (unlikely(num > sdebug_store_sectors)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2697 | /* needs work to find which cdb byte 'num' comes from */ |
| 2698 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 2699 | return check_condition_result; |
| 2700 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2701 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2702 | if (unlikely((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) && |
| 2703 | (lba <= (OPT_MEDIUM_ERR_ADDR + OPT_MEDIUM_ERR_NUM - 1)) && |
| 2704 | ((lba + num) > OPT_MEDIUM_ERR_ADDR))) { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2705 | /* claim unrecoverable read error */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2706 | mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2707 | /* set info field and valid bit for fixed descriptor */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2708 | if (0x70 == (scp->sense_buffer[0] & 0x7f)) { |
| 2709 | scp->sense_buffer[0] |= 0x80; /* Valid bit */ |
Douglas Gilbert | 32f7ef7 | 2011-03-11 10:43:35 -0500 | [diff] [blame] | 2710 | ret = (lba < OPT_MEDIUM_ERR_ADDR) |
| 2711 | ? OPT_MEDIUM_ERR_ADDR : (int)lba; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2712 | put_unaligned_be32(ret, scp->sense_buffer + 3); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2713 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2714 | scsi_set_resid(scp, scsi_bufflen(scp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | return check_condition_result; |
| 2716 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2717 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2718 | read_lock_irqsave(&atomic_rw, iflags); |
| 2719 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2720 | /* DIX + T10 DIF */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2721 | if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2722 | int prot_ret = prot_verify_read(scp, lba, num, ei_lba); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2723 | |
| 2724 | if (prot_ret) { |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2725 | read_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2726 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2727 | return illegal_condition_result; |
| 2728 | } |
| 2729 | } |
| 2730 | |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 2731 | ret = do_device_access(scp, 0, lba, num, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | read_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2733 | if (unlikely(ret == -1)) |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2734 | return DID_ERROR << 16; |
| 2735 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2736 | scsi_in(scp)->resid = scsi_bufflen(scp) - ret; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2737 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2738 | if (unlikely(sqcp)) { |
| 2739 | if (sqcp->inj_recovered) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2740 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 2741 | THRESHOLD_EXCEEDED, 0); |
| 2742 | return check_condition_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2743 | } else if (sqcp->inj_transport) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2744 | mk_sense_buffer(scp, ABORTED_COMMAND, |
| 2745 | TRANSPORT_PROBLEM, ACK_NAK_TO); |
| 2746 | return check_condition_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2747 | } else if (sqcp->inj_dif) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2748 | /* Logical block guard check failed */ |
| 2749 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); |
| 2750 | return illegal_condition_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2751 | } else if (sqcp->inj_dix) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2752 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); |
| 2753 | return illegal_condition_result; |
| 2754 | } |
| 2755 | } |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2756 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | } |
| 2758 | |
Tomas Winkler | 58a8635 | 2015-07-28 16:54:23 +0300 | [diff] [blame] | 2759 | static void dump_sector(unsigned char *buf, int len) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2760 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2761 | int i, j, n; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2762 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2763 | pr_err(">>> Sector Dump <<<\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2764 | for (i = 0 ; i < len ; i += 16) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2765 | char b[128]; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2766 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2767 | for (j = 0, n = 0; j < 16; j++) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2768 | unsigned char c = buf[i+j]; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2769 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2770 | if (c >= 0x20 && c < 0x7e) |
| 2771 | n += scnprintf(b + n, sizeof(b) - n, |
| 2772 | " %c ", buf[i+j]); |
| 2773 | else |
| 2774 | n += scnprintf(b + n, sizeof(b) - n, |
| 2775 | "%02x ", buf[i+j]); |
| 2776 | } |
| 2777 | pr_err("%04d: %s\n", i, b); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2778 | } |
| 2779 | } |
| 2780 | |
| 2781 | 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] | 2782 | unsigned int sectors, u32 ei_lba) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2783 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2784 | int ret; |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 2785 | struct t10_pi_tuple *sdt; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2786 | void *daddr; |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2787 | sector_t sector = start_sec; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2788 | int ppage_offset; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2789 | int dpage_offset; |
| 2790 | struct sg_mapping_iter diter; |
| 2791 | struct sg_mapping_iter piter; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2792 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2793 | BUG_ON(scsi_sg_count(SCpnt) == 0); |
| 2794 | BUG_ON(scsi_prot_sg_count(SCpnt) == 0); |
| 2795 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2796 | sg_miter_start(&piter, scsi_prot_sglist(SCpnt), |
| 2797 | scsi_prot_sg_count(SCpnt), |
| 2798 | SG_MITER_ATOMIC | SG_MITER_FROM_SG); |
| 2799 | sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt), |
| 2800 | SG_MITER_ATOMIC | SG_MITER_FROM_SG); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2801 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2802 | /* For each protection page */ |
| 2803 | while (sg_miter_next(&piter)) { |
| 2804 | dpage_offset = 0; |
| 2805 | if (WARN_ON(!sg_miter_next(&diter))) { |
| 2806 | ret = 0x01; |
| 2807 | goto out; |
| 2808 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2809 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2810 | for (ppage_offset = 0; ppage_offset < piter.length; |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 2811 | ppage_offset += sizeof(struct t10_pi_tuple)) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2812 | /* If we're at the end of the current |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2813 | * data page advance to the next one |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2814 | */ |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2815 | if (dpage_offset >= diter.length) { |
| 2816 | if (WARN_ON(!sg_miter_next(&diter))) { |
| 2817 | ret = 0x01; |
| 2818 | goto out; |
| 2819 | } |
| 2820 | dpage_offset = 0; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2821 | } |
| 2822 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2823 | sdt = piter.addr + ppage_offset; |
| 2824 | daddr = diter.addr + dpage_offset; |
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 | ret = dif_verify(sdt, daddr, sector, ei_lba); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2827 | if (ret) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2828 | dump_sector(daddr, sdebug_sector_size); |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 2829 | goto out; |
| 2830 | } |
| 2831 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2832 | sector++; |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 2833 | ei_lba++; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2834 | dpage_offset += sdebug_sector_size; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2835 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2836 | diter.consumed = dpage_offset; |
| 2837 | sg_miter_stop(&diter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2838 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2839 | sg_miter_stop(&piter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2840 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2841 | dif_copy_prot(SCpnt, start_sec, sectors, false); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2842 | dix_writes++; |
| 2843 | |
| 2844 | return 0; |
| 2845 | |
| 2846 | out: |
| 2847 | dif_errors++; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2848 | sg_miter_stop(&diter); |
| 2849 | sg_miter_stop(&piter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2850 | return ret; |
| 2851 | } |
| 2852 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2853 | static unsigned long lba_to_map_index(sector_t lba) |
| 2854 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2855 | if (sdebug_unmap_alignment) |
| 2856 | lba += sdebug_unmap_granularity - sdebug_unmap_alignment; |
| 2857 | sector_div(lba, sdebug_unmap_granularity); |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2858 | return lba; |
| 2859 | } |
| 2860 | |
| 2861 | static sector_t map_index_to_lba(unsigned long index) |
| 2862 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2863 | sector_t lba = index * sdebug_unmap_granularity; |
Akinobu Mita | a027b5b | 2013-08-26 22:08:41 +0900 | [diff] [blame] | 2864 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2865 | if (sdebug_unmap_alignment) |
| 2866 | lba -= sdebug_unmap_granularity - sdebug_unmap_alignment; |
Akinobu Mita | a027b5b | 2013-08-26 22:08:41 +0900 | [diff] [blame] | 2867 | return lba; |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2868 | } |
| 2869 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2870 | static unsigned int map_state(sector_t lba, unsigned int *num) |
| 2871 | { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2872 | sector_t end; |
| 2873 | unsigned int mapped; |
| 2874 | unsigned long index; |
| 2875 | unsigned long next; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2876 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2877 | index = lba_to_map_index(lba); |
| 2878 | mapped = test_bit(index, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2879 | |
| 2880 | if (mapped) |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2881 | next = find_next_zero_bit(map_storep, map_size, index); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2882 | else |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2883 | next = find_next_bit(map_storep, map_size, index); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2884 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2885 | 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] | 2886 | *num = end - lba; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2887 | return mapped; |
| 2888 | } |
| 2889 | |
| 2890 | static void map_region(sector_t lba, unsigned int len) |
| 2891 | { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2892 | sector_t end = lba + len; |
| 2893 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2894 | while (lba < end) { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2895 | unsigned long index = lba_to_map_index(lba); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2896 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2897 | if (index < map_size) |
| 2898 | set_bit(index, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2899 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2900 | lba = map_index_to_lba(index + 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2901 | } |
| 2902 | } |
| 2903 | |
| 2904 | static void unmap_region(sector_t lba, unsigned int len) |
| 2905 | { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2906 | sector_t end = lba + len; |
| 2907 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2908 | while (lba < end) { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2909 | unsigned long index = lba_to_map_index(lba); |
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 | if (lba == map_index_to_lba(index) && |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2912 | lba + sdebug_unmap_granularity <= end && |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2913 | index < map_size) { |
| 2914 | clear_bit(index, map_storep); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2915 | if (sdebug_lbprz) { /* for LBPRZ=2 return 0xff_s */ |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 2916 | memset(fake_storep + |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 2917 | lba * sdebug_sector_size, |
| 2918 | (sdebug_lbprz & 1) ? 0 : 0xff, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2919 | sdebug_sector_size * |
| 2920 | sdebug_unmap_granularity); |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2921 | } |
Akinobu Mita | e9926b4 | 2013-06-29 17:59:17 +0900 | [diff] [blame] | 2922 | if (dif_storep) { |
| 2923 | memset(dif_storep + lba, 0xff, |
| 2924 | sizeof(*dif_storep) * |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2925 | sdebug_unmap_granularity); |
Akinobu Mita | e9926b4 | 2013-06-29 17:59:17 +0900 | [diff] [blame] | 2926 | } |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 2927 | } |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2928 | lba = map_index_to_lba(index + 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2929 | } |
| 2930 | } |
| 2931 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2932 | 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] | 2933 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2934 | u8 *cmd = scp->cmnd; |
| 2935 | u64 lba; |
| 2936 | u32 num; |
| 2937 | u32 ei_lba; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | unsigned long iflags; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2939 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2940 | bool check_prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2941 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2942 | switch (cmd[0]) { |
| 2943 | case WRITE_16: |
| 2944 | ei_lba = 0; |
| 2945 | lba = get_unaligned_be64(cmd + 2); |
| 2946 | num = get_unaligned_be32(cmd + 10); |
| 2947 | check_prot = true; |
| 2948 | break; |
| 2949 | case WRITE_10: |
| 2950 | ei_lba = 0; |
| 2951 | lba = get_unaligned_be32(cmd + 2); |
| 2952 | num = get_unaligned_be16(cmd + 7); |
| 2953 | check_prot = true; |
| 2954 | break; |
| 2955 | case WRITE_6: |
| 2956 | ei_lba = 0; |
| 2957 | lba = (u32)cmd[3] | (u32)cmd[2] << 8 | |
| 2958 | (u32)(cmd[1] & 0x1f) << 16; |
| 2959 | num = (0 == cmd[4]) ? 256 : cmd[4]; |
| 2960 | check_prot = true; |
| 2961 | break; |
| 2962 | case WRITE_12: |
| 2963 | ei_lba = 0; |
| 2964 | lba = get_unaligned_be32(cmd + 2); |
| 2965 | num = get_unaligned_be32(cmd + 6); |
| 2966 | check_prot = true; |
| 2967 | break; |
| 2968 | case 0x53: /* XDWRITEREAD(10) */ |
| 2969 | ei_lba = 0; |
| 2970 | lba = get_unaligned_be32(cmd + 2); |
| 2971 | num = get_unaligned_be16(cmd + 7); |
| 2972 | check_prot = false; |
| 2973 | break; |
| 2974 | default: /* assume WRITE(32) */ |
| 2975 | lba = get_unaligned_be64(cmd + 12); |
| 2976 | ei_lba = get_unaligned_be32(cmd + 20); |
| 2977 | num = get_unaligned_be32(cmd + 28); |
| 2978 | check_prot = false; |
| 2979 | break; |
| 2980 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2981 | if (unlikely(have_dif_prot && check_prot)) { |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2982 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2983 | (cmd[1] & 0xe0)) { |
| 2984 | mk_sense_invalid_opcode(scp); |
| 2985 | return check_condition_result; |
| 2986 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2987 | if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || |
| 2988 | sdebug_dif == T10_PI_TYPE3_PROTECTION) && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2989 | (cmd[1] & 0xe0) == 0) |
| 2990 | sdev_printk(KERN_ERR, scp->device, "Unprotected WR " |
| 2991 | "to DIF device\n"); |
| 2992 | } |
| 2993 | |
| 2994 | /* inline check_device_access_params() */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2995 | if (unlikely(lba + num > sdebug_capacity)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2996 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 2997 | return check_condition_result; |
| 2998 | } |
| 2999 | /* transfer length excessive (tie in to block limits VPD page) */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3000 | if (unlikely(num > sdebug_store_sectors)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3001 | /* needs work to find which cdb byte 'num' comes from */ |
| 3002 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 3003 | return check_condition_result; |
| 3004 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3006 | write_lock_irqsave(&atomic_rw, iflags); |
| 3007 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 3008 | /* DIX + T10 DIF */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3009 | if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3010 | int prot_ret = prot_verify_write(scp, lba, num, ei_lba); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 3011 | |
| 3012 | if (prot_ret) { |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3013 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3014 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 3015 | return illegal_condition_result; |
| 3016 | } |
| 3017 | } |
| 3018 | |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 3019 | ret = do_device_access(scp, 0, lba, num, true); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3020 | if (unlikely(scsi_debug_lbp())) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3021 | map_region(lba, num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3023 | if (unlikely(-1 == ret)) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3024 | return DID_ERROR << 16; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3025 | else if (unlikely(sdebug_verbose && |
| 3026 | (ret < (num * sdebug_sector_size)))) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3027 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3028 | "%s: write: cdb indicated=%u, IO sent=%d bytes\n", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3029 | my_name, num * sdebug_sector_size, ret); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3030 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3031 | if (unlikely(sdebug_any_injecting_opt)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3032 | struct sdebug_queued_cmd *sqcp = |
| 3033 | (struct sdebug_queued_cmd *)scp->host_scribble; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3034 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3035 | if (sqcp) { |
| 3036 | if (sqcp->inj_recovered) { |
| 3037 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 3038 | THRESHOLD_EXCEEDED, 0); |
| 3039 | return check_condition_result; |
| 3040 | } else if (sqcp->inj_dif) { |
| 3041 | /* Logical block guard check failed */ |
| 3042 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); |
| 3043 | return illegal_condition_result; |
| 3044 | } else if (sqcp->inj_dix) { |
| 3045 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); |
| 3046 | return illegal_condition_result; |
| 3047 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3048 | } |
| 3049 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3050 | return 0; |
| 3051 | } |
| 3052 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3053 | static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num, |
| 3054 | u32 ei_lba, bool unmap, bool ndob) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3055 | { |
| 3056 | unsigned long iflags; |
| 3057 | unsigned long long i; |
| 3058 | int ret; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3059 | u64 lba_off; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3060 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3061 | ret = check_device_access_params(scp, lba, num); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3062 | if (ret) |
| 3063 | return ret; |
| 3064 | |
| 3065 | write_lock_irqsave(&atomic_rw, iflags); |
| 3066 | |
Akinobu Mita | 9ed8d3d | 2013-04-16 22:11:55 +0900 | [diff] [blame] | 3067 | if (unmap && scsi_debug_lbp()) { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3068 | unmap_region(lba, num); |
| 3069 | goto out; |
| 3070 | } |
| 3071 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3072 | lba_off = lba * sdebug_sector_size; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3073 | /* if ndob then zero 1 logical block, else fetch 1 logical block */ |
| 3074 | if (ndob) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3075 | memset(fake_storep + lba_off, 0, sdebug_sector_size); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3076 | ret = 0; |
| 3077 | } else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3078 | ret = fetch_to_dev_buffer(scp, fake_storep + lba_off, |
| 3079 | sdebug_sector_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3080 | |
| 3081 | if (-1 == ret) { |
| 3082 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3083 | return DID_ERROR << 16; |
Douglas Gilbert | e33d7c5 | 2017-10-29 10:47:19 -0400 | [diff] [blame] | 3084 | } else if (sdebug_verbose && !ndob && (ret < sdebug_sector_size)) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3085 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | e33d7c5 | 2017-10-29 10:47:19 -0400 | [diff] [blame] | 3086 | "%s: %s: lb size=%u, IO sent=%d bytes\n", |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3087 | my_name, "write same", |
Douglas Gilbert | e33d7c5 | 2017-10-29 10:47:19 -0400 | [diff] [blame] | 3088 | sdebug_sector_size, ret); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3089 | |
| 3090 | /* Copy first sector to remaining blocks */ |
| 3091 | for (i = 1 ; i < num ; i++) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3092 | memcpy(fake_storep + ((lba + i) * sdebug_sector_size), |
| 3093 | fake_storep + lba_off, |
| 3094 | sdebug_sector_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3095 | |
Akinobu Mita | 9ed8d3d | 2013-04-16 22:11:55 +0900 | [diff] [blame] | 3096 | if (scsi_debug_lbp()) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3097 | map_region(lba, num); |
| 3098 | out: |
| 3099 | write_unlock_irqrestore(&atomic_rw, iflags); |
| 3100 | |
| 3101 | return 0; |
| 3102 | } |
| 3103 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3104 | static int resp_write_same_10(struct scsi_cmnd *scp, |
| 3105 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3106 | { |
| 3107 | u8 *cmd = scp->cmnd; |
| 3108 | u32 lba; |
| 3109 | u16 num; |
| 3110 | u32 ei_lba = 0; |
| 3111 | bool unmap = false; |
| 3112 | |
| 3113 | if (cmd[1] & 0x8) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3114 | if (sdebug_lbpws10 == 0) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3115 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); |
| 3116 | return check_condition_result; |
| 3117 | } else |
| 3118 | unmap = true; |
| 3119 | } |
| 3120 | lba = get_unaligned_be32(cmd + 2); |
| 3121 | num = get_unaligned_be16(cmd + 7); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3122 | if (num > sdebug_write_same_length) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3123 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); |
| 3124 | return check_condition_result; |
| 3125 | } |
| 3126 | return resp_write_same(scp, lba, num, ei_lba, unmap, false); |
| 3127 | } |
| 3128 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3129 | static int resp_write_same_16(struct scsi_cmnd *scp, |
| 3130 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3131 | { |
| 3132 | u8 *cmd = scp->cmnd; |
| 3133 | u64 lba; |
| 3134 | u32 num; |
| 3135 | u32 ei_lba = 0; |
| 3136 | bool unmap = false; |
| 3137 | bool ndob = false; |
| 3138 | |
| 3139 | if (cmd[1] & 0x8) { /* UNMAP */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3140 | if (sdebug_lbpws == 0) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3141 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); |
| 3142 | return check_condition_result; |
| 3143 | } else |
| 3144 | unmap = true; |
| 3145 | } |
| 3146 | if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */ |
| 3147 | ndob = true; |
| 3148 | lba = get_unaligned_be64(cmd + 2); |
| 3149 | num = get_unaligned_be32(cmd + 10); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3150 | if (num > sdebug_write_same_length) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3151 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1); |
| 3152 | return check_condition_result; |
| 3153 | } |
| 3154 | return resp_write_same(scp, lba, num, ei_lba, unmap, ndob); |
| 3155 | } |
| 3156 | |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 3157 | /* Note the mode field is in the same position as the (lower) service action |
| 3158 | * field. For the Report supported operation codes command, SPC-4 suggests |
| 3159 | * each mode of this command should be reported separately; for future. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3160 | static int resp_write_buffer(struct scsi_cmnd *scp, |
| 3161 | struct sdebug_dev_info *devip) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 3162 | { |
| 3163 | u8 *cmd = scp->cmnd; |
| 3164 | struct scsi_device *sdp = scp->device; |
| 3165 | struct sdebug_dev_info *dp; |
| 3166 | u8 mode; |
| 3167 | |
| 3168 | mode = cmd[1] & 0x1f; |
| 3169 | switch (mode) { |
| 3170 | case 0x4: /* download microcode (MC) and activate (ACT) */ |
| 3171 | /* set UAs on this device only */ |
| 3172 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3173 | set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm); |
| 3174 | break; |
| 3175 | case 0x5: /* download MC, save and ACT */ |
| 3176 | set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm); |
| 3177 | break; |
| 3178 | case 0x6: /* download MC with offsets and ACT */ |
| 3179 | /* set UAs on most devices (LUs) in this target */ |
| 3180 | list_for_each_entry(dp, |
| 3181 | &devip->sdbg_host->dev_info_list, |
| 3182 | dev_list) |
| 3183 | if (dp->target == sdp->id) { |
| 3184 | set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm); |
| 3185 | if (devip != dp) |
| 3186 | set_bit(SDEBUG_UA_MICROCODE_CHANGED, |
| 3187 | dp->uas_bm); |
| 3188 | } |
| 3189 | break; |
| 3190 | case 0x7: /* download MC with offsets, save, and ACT */ |
| 3191 | /* set UA on all devices (LUs) in this target */ |
| 3192 | list_for_each_entry(dp, |
| 3193 | &devip->sdbg_host->dev_info_list, |
| 3194 | dev_list) |
| 3195 | if (dp->target == sdp->id) |
| 3196 | set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, |
| 3197 | dp->uas_bm); |
| 3198 | break; |
| 3199 | default: |
| 3200 | /* do nothing for this command for other mode values */ |
| 3201 | break; |
| 3202 | } |
| 3203 | return 0; |
| 3204 | } |
| 3205 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3206 | static int resp_comp_write(struct scsi_cmnd *scp, |
| 3207 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3208 | { |
| 3209 | u8 *cmd = scp->cmnd; |
| 3210 | u8 *arr; |
| 3211 | u8 *fake_storep_hold; |
| 3212 | u64 lba; |
| 3213 | u32 dnum; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3214 | u32 lb_size = sdebug_sector_size; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3215 | u8 num; |
| 3216 | unsigned long iflags; |
| 3217 | int ret; |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3218 | int retval = 0; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3219 | |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3220 | lba = get_unaligned_be64(cmd + 2); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3221 | num = cmd[13]; /* 1 to a maximum of 255 logical blocks */ |
| 3222 | if (0 == num) |
| 3223 | return 0; /* degenerate case, not an error */ |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 3224 | if (sdebug_dif == T10_PI_TYPE2_PROTECTION && |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3225 | (cmd[1] & 0xe0)) { |
| 3226 | mk_sense_invalid_opcode(scp); |
| 3227 | return check_condition_result; |
| 3228 | } |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 3229 | if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || |
| 3230 | sdebug_dif == T10_PI_TYPE3_PROTECTION) && |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3231 | (cmd[1] & 0xe0) == 0) |
| 3232 | sdev_printk(KERN_ERR, scp->device, "Unprotected WR " |
| 3233 | "to DIF device\n"); |
| 3234 | |
| 3235 | /* inline check_device_access_params() */ |
| 3236 | if (lba + num > sdebug_capacity) { |
| 3237 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 3238 | return check_condition_result; |
| 3239 | } |
| 3240 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 3241 | if (num > sdebug_store_sectors) { |
| 3242 | /* needs work to find which cdb byte 'num' comes from */ |
| 3243 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 3244 | return check_condition_result; |
| 3245 | } |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3246 | dnum = 2 * num; |
| 3247 | arr = kzalloc(dnum * lb_size, GFP_ATOMIC); |
| 3248 | if (NULL == arr) { |
| 3249 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3250 | INSUFF_RES_ASCQ); |
| 3251 | return check_condition_result; |
| 3252 | } |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3253 | |
| 3254 | write_lock_irqsave(&atomic_rw, iflags); |
| 3255 | |
| 3256 | /* trick do_device_access() to fetch both compare and write buffers |
| 3257 | * from data-in into arr. Safe (atomic) since write_lock held. */ |
| 3258 | fake_storep_hold = fake_storep; |
| 3259 | fake_storep = arr; |
Douglas Gilbert | 0a7e69c | 2017-12-23 12:48:12 -0500 | [diff] [blame] | 3260 | ret = do_device_access(scp, 0, 0, dnum, true); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3261 | fake_storep = fake_storep_hold; |
| 3262 | if (ret == -1) { |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3263 | retval = DID_ERROR << 16; |
| 3264 | goto cleanup; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3265 | } else if (sdebug_verbose && (ret < (dnum * lb_size))) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3266 | sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb " |
| 3267 | "indicated=%u, IO sent=%d bytes\n", my_name, |
| 3268 | dnum * lb_size, ret); |
| 3269 | if (!comp_write_worker(lba, num, arr)) { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3270 | mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3271 | retval = check_condition_result; |
| 3272 | goto cleanup; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3273 | } |
| 3274 | if (scsi_debug_lbp()) |
| 3275 | map_region(lba, num); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3276 | cleanup: |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3277 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3278 | kfree(arr); |
| 3279 | return retval; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3280 | } |
| 3281 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3282 | struct unmap_block_desc { |
| 3283 | __be64 lba; |
| 3284 | __be32 blocks; |
| 3285 | __be32 __reserved; |
| 3286 | }; |
| 3287 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3288 | 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] | 3289 | { |
| 3290 | unsigned char *buf; |
| 3291 | struct unmap_block_desc *desc; |
| 3292 | unsigned int i, payload_len, descriptors; |
| 3293 | int ret; |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3294 | unsigned long iflags; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3295 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3296 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3297 | if (!scsi_debug_lbp()) |
| 3298 | return 0; /* fib and say its done */ |
| 3299 | payload_len = get_unaligned_be16(scp->cmnd + 7); |
| 3300 | BUG_ON(scsi_bufflen(scp) != payload_len); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3301 | |
| 3302 | descriptors = (payload_len - 8) / 16; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3303 | if (descriptors > sdebug_unmap_max_desc) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3304 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3305 | return check_condition_result; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3306 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3307 | |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 3308 | buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3309 | if (!buf) { |
| 3310 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3311 | INSUFF_RES_ASCQ); |
| 3312 | return check_condition_result; |
| 3313 | } |
| 3314 | |
| 3315 | scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp)); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3316 | |
| 3317 | BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2); |
| 3318 | BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16); |
| 3319 | |
| 3320 | desc = (void *)&buf[8]; |
| 3321 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3322 | write_lock_irqsave(&atomic_rw, iflags); |
| 3323 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3324 | for (i = 0 ; i < descriptors ; i++) { |
| 3325 | unsigned long long lba = get_unaligned_be64(&desc[i].lba); |
| 3326 | unsigned int num = get_unaligned_be32(&desc[i].blocks); |
| 3327 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3328 | ret = check_device_access_params(scp, lba, num); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3329 | if (ret) |
| 3330 | goto out; |
| 3331 | |
| 3332 | unmap_region(lba, num); |
| 3333 | } |
| 3334 | |
| 3335 | ret = 0; |
| 3336 | |
| 3337 | out: |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3338 | write_unlock_irqrestore(&atomic_rw, iflags); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3339 | kfree(buf); |
| 3340 | |
| 3341 | return ret; |
| 3342 | } |
| 3343 | |
| 3344 | #define SDEBUG_GET_LBA_STATUS_LEN 32 |
| 3345 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3346 | static int resp_get_lba_status(struct scsi_cmnd *scp, |
| 3347 | struct sdebug_dev_info *devip) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3348 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3349 | u8 *cmd = scp->cmnd; |
| 3350 | u64 lba; |
| 3351 | u32 alloc_len, mapped, num; |
| 3352 | u8 arr[SDEBUG_GET_LBA_STATUS_LEN]; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3353 | int ret; |
| 3354 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3355 | lba = get_unaligned_be64(cmd + 2); |
| 3356 | alloc_len = get_unaligned_be32(cmd + 10); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3357 | |
| 3358 | if (alloc_len < 24) |
| 3359 | return 0; |
| 3360 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3361 | ret = check_device_access_params(scp, lba, 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3362 | if (ret) |
| 3363 | return ret; |
| 3364 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3365 | if (scsi_debug_lbp()) |
| 3366 | mapped = map_state(lba, &num); |
| 3367 | else { |
| 3368 | mapped = 1; |
| 3369 | /* following just in case virtual_gb changed */ |
| 3370 | sdebug_capacity = get_sdebug_capacity(); |
| 3371 | if (sdebug_capacity - lba <= 0xffffffff) |
| 3372 | num = sdebug_capacity - lba; |
| 3373 | else |
| 3374 | num = 0xffffffff; |
| 3375 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3376 | |
| 3377 | memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3378 | put_unaligned_be32(20, arr); /* Parameter Data Length */ |
| 3379 | put_unaligned_be64(lba, arr + 8); /* LBA */ |
| 3380 | put_unaligned_be32(num, arr + 16); /* Number of blocks */ |
| 3381 | arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3382 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3383 | 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] | 3384 | } |
| 3385 | |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3386 | #define RL_BUCKET_ELEMS 8 |
| 3387 | |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3388 | /* Even though each pseudo target has a REPORT LUNS "well known logical unit" |
| 3389 | * (W-LUN), the normal Linux scanning logic does not associate it with a |
| 3390 | * device (e.g. /dev/sg7). The following magic will make that association: |
| 3391 | * "cd /sys/class/scsi_host/host<n> ; echo '- - 49409' > scan" |
| 3392 | * where <n> is a host number. If there are multiple targets in a host then |
| 3393 | * the above will associate a W-LUN to each target. To only get a W-LUN |
| 3394 | * for target 2, then use "echo '- 2 49409' > scan" . |
| 3395 | */ |
| 3396 | static int resp_report_luns(struct scsi_cmnd *scp, |
| 3397 | struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 3399 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3400 | unsigned int alloc_len; |
| 3401 | unsigned char select_report; |
| 3402 | u64 lun; |
| 3403 | struct scsi_lun *lun_p; |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3404 | u8 arr[RL_BUCKET_ELEMS * sizeof(struct scsi_lun)]; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3405 | unsigned int lun_cnt; /* normal LUN count (max: 256) */ |
| 3406 | unsigned int wlun_cnt; /* report luns W-LUN count */ |
| 3407 | unsigned int tlun_cnt; /* total LUN count */ |
| 3408 | unsigned int rlen; /* response length (in bytes) */ |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3409 | int k, j, n, res; |
| 3410 | unsigned int off_rsp = 0; |
| 3411 | const int sz_lun = sizeof(struct scsi_lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 3413 | clear_luns_changed_on_target(devip); |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3414 | |
| 3415 | select_report = cmd[2]; |
| 3416 | alloc_len = get_unaligned_be32(cmd + 6); |
| 3417 | |
| 3418 | if (alloc_len < 4) { |
| 3419 | pr_err("alloc len too small %d\n", alloc_len); |
| 3420 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3421 | return check_condition_result; |
| 3422 | } |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3423 | |
| 3424 | switch (select_report) { |
| 3425 | case 0: /* all LUNs apart from W-LUNs */ |
| 3426 | lun_cnt = sdebug_max_luns; |
| 3427 | wlun_cnt = 0; |
| 3428 | break; |
| 3429 | case 1: /* only W-LUNs */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3430 | lun_cnt = 0; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3431 | wlun_cnt = 1; |
| 3432 | break; |
| 3433 | case 2: /* all LUNs */ |
| 3434 | lun_cnt = sdebug_max_luns; |
| 3435 | wlun_cnt = 1; |
| 3436 | break; |
| 3437 | case 0x10: /* only administrative LUs */ |
| 3438 | case 0x11: /* see SPC-5 */ |
| 3439 | case 0x12: /* only subsiduary LUs owned by referenced LU */ |
| 3440 | default: |
| 3441 | pr_debug("select report invalid %d\n", select_report); |
| 3442 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); |
| 3443 | return check_condition_result; |
| 3444 | } |
| 3445 | |
| 3446 | if (sdebug_no_lun_0 && (lun_cnt > 0)) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3447 | --lun_cnt; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3448 | |
| 3449 | tlun_cnt = lun_cnt + wlun_cnt; |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3450 | rlen = tlun_cnt * sz_lun; /* excluding 8 byte header */ |
| 3451 | scsi_set_resid(scp, scsi_bufflen(scp)); |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3452 | pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n", |
| 3453 | select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0); |
| 3454 | |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3455 | /* loops rely on sizeof response header same as sizeof lun (both 8) */ |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3456 | lun = sdebug_no_lun_0 ? 1 : 0; |
Douglas Gilbert | fb0cc8d | 2016-05-31 17:15:07 -0400 | [diff] [blame] | 3457 | for (k = 0, j = 0, res = 0; true; ++k, j = 0) { |
| 3458 | memset(arr, 0, sizeof(arr)); |
| 3459 | lun_p = (struct scsi_lun *)&arr[0]; |
| 3460 | if (k == 0) { |
| 3461 | put_unaligned_be32(rlen, &arr[0]); |
| 3462 | ++lun_p; |
| 3463 | j = 1; |
| 3464 | } |
| 3465 | for ( ; j < RL_BUCKET_ELEMS; ++j, ++lun_p) { |
| 3466 | if ((k * RL_BUCKET_ELEMS) + j > lun_cnt) |
| 3467 | break; |
| 3468 | int_to_scsilun(lun++, lun_p); |
| 3469 | } |
| 3470 | if (j < RL_BUCKET_ELEMS) |
| 3471 | break; |
| 3472 | n = j * sz_lun; |
| 3473 | res = p_fill_from_dev_buffer(scp, arr, n, off_rsp); |
| 3474 | if (res) |
| 3475 | return res; |
| 3476 | off_rsp += n; |
| 3477 | } |
| 3478 | if (wlun_cnt) { |
| 3479 | int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p); |
| 3480 | ++j; |
| 3481 | } |
| 3482 | if (j > 0) |
| 3483 | 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] | 3484 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | } |
| 3486 | |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3487 | static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba, |
| 3488 | unsigned int num, struct sdebug_dev_info *devip) |
| 3489 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3490 | int j; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3491 | unsigned char *kaddr, *buf; |
| 3492 | unsigned int offset; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3493 | struct scsi_data_buffer *sdb = scsi_in(scp); |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3494 | struct sg_mapping_iter miter; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3495 | |
| 3496 | /* better not to use temporary buffer. */ |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 3497 | buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC); |
Akinobu Mita | c5af0db | 2014-02-26 22:57:01 +0900 | [diff] [blame] | 3498 | if (!buf) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 3499 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3500 | INSUFF_RES_ASCQ); |
Akinobu Mita | c5af0db | 2014-02-26 22:57:01 +0900 | [diff] [blame] | 3501 | return check_condition_result; |
| 3502 | } |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3503 | |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 3504 | scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp)); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3505 | |
| 3506 | offset = 0; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3507 | sg_miter_start(&miter, sdb->table.sgl, sdb->table.nents, |
| 3508 | SG_MITER_ATOMIC | SG_MITER_TO_SG); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3509 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3510 | while (sg_miter_next(&miter)) { |
| 3511 | kaddr = miter.addr; |
| 3512 | for (j = 0; j < miter.length; j++) |
| 3513 | *(kaddr + j) ^= *(buf + offset + j); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3514 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3515 | offset += miter.length; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3516 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3517 | sg_miter_stop(&miter); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3518 | kfree(buf); |
| 3519 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3520 | return 0; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3521 | } |
| 3522 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3523 | static int resp_xdwriteread_10(struct scsi_cmnd *scp, |
| 3524 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3525 | { |
| 3526 | u8 *cmd = scp->cmnd; |
| 3527 | u64 lba; |
| 3528 | u32 num; |
| 3529 | int errsts; |
| 3530 | |
| 3531 | if (!scsi_bidi_cmnd(scp)) { |
| 3532 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3533 | INSUFF_RES_ASCQ); |
| 3534 | return check_condition_result; |
| 3535 | } |
| 3536 | errsts = resp_read_dt0(scp, devip); |
| 3537 | if (errsts) |
| 3538 | return errsts; |
| 3539 | if (!(cmd[1] & 0x4)) { /* DISABLE_WRITE is not set */ |
| 3540 | errsts = resp_write_dt0(scp, devip); |
| 3541 | if (errsts) |
| 3542 | return errsts; |
| 3543 | } |
| 3544 | lba = get_unaligned_be32(cmd + 2); |
| 3545 | num = get_unaligned_be16(cmd + 7); |
| 3546 | return resp_xdwriteread(scp, lba, num, devip); |
| 3547 | } |
| 3548 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3549 | static struct sdebug_queue *get_queue(struct scsi_cmnd *cmnd) |
| 3550 | { |
| 3551 | struct sdebug_queue *sqp = sdebug_q_arr; |
| 3552 | |
| 3553 | if (sdebug_mq_active) { |
| 3554 | u32 tag = blk_mq_unique_tag(cmnd->request); |
| 3555 | u16 hwq = blk_mq_unique_tag_to_hwq(tag); |
| 3556 | |
| 3557 | if (unlikely(hwq >= submit_queues)) { |
| 3558 | pr_warn("Unexpected hwq=%d, apply modulo\n", hwq); |
| 3559 | hwq %= submit_queues; |
| 3560 | } |
| 3561 | pr_debug("tag=%u, hwq=%d\n", tag, hwq); |
| 3562 | return sqp + hwq; |
| 3563 | } else |
| 3564 | return sqp; |
| 3565 | } |
| 3566 | |
| 3567 | /* Queued (deferred) command completions converge here. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3568 | static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3569 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3570 | int qc_idx; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3571 | int retiring = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3572 | unsigned long iflags; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3573 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3574 | struct sdebug_queued_cmd *sqcp; |
| 3575 | struct scsi_cmnd *scp; |
| 3576 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3578 | qc_idx = sd_dp->qc_idx; |
| 3579 | sqp = sdebug_q_arr + sd_dp->sqa_idx; |
| 3580 | if (sdebug_statistics) { |
| 3581 | atomic_inc(&sdebug_completions); |
| 3582 | if (raw_smp_processor_id() != sd_dp->issuing_cpu) |
| 3583 | atomic_inc(&sdebug_miss_cpus); |
| 3584 | } |
| 3585 | if (unlikely((qc_idx < 0) || (qc_idx >= SDEBUG_CANQUEUE))) { |
| 3586 | pr_err("wild qc_idx=%d\n", qc_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3587 | return; |
| 3588 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3589 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 3590 | sqcp = &sqp->qc_arr[qc_idx]; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3591 | scp = sqcp->a_cmnd; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3592 | if (unlikely(scp == NULL)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3593 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 3594 | pr_err("scp is NULL, sqa_idx=%d, qc_idx=%d\n", |
| 3595 | sd_dp->sqa_idx, qc_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3596 | return; |
| 3597 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3598 | devip = (struct sdebug_dev_info *)scp->device->hostdata; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3599 | if (likely(devip)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3600 | atomic_dec(&devip->num_in_q); |
| 3601 | else |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3602 | pr_err("devip=NULL\n"); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3603 | if (unlikely(atomic_read(&retired_max_queue) > 0)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3604 | retiring = 1; |
| 3605 | |
| 3606 | sqcp->a_cmnd = NULL; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3607 | if (unlikely(!test_and_clear_bit(qc_idx, sqp->in_use_bm))) { |
| 3608 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3609 | pr_err("Unexpected completion\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3610 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3612 | |
| 3613 | if (unlikely(retiring)) { /* user has reduced max_queue */ |
| 3614 | int k, retval; |
| 3615 | |
| 3616 | retval = atomic_read(&retired_max_queue); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3617 | if (qc_idx >= retval) { |
| 3618 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3619 | pr_err("index %d too large\n", retval); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3620 | return; |
| 3621 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3622 | k = find_last_bit(sqp->in_use_bm, retval); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3623 | if ((k < sdebug_max_queue) || (k == retval)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3624 | atomic_set(&retired_max_queue, 0); |
| 3625 | else |
| 3626 | atomic_set(&retired_max_queue, k + 1); |
| 3627 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3628 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3629 | scp->scsi_done(scp); /* callback to mid level */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | } |
| 3631 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3632 | /* When high resolution timer goes off this function is called. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3633 | static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3634 | { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3635 | struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer, |
| 3636 | hrt); |
| 3637 | sdebug_q_cmd_complete(sd_dp); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3638 | return HRTIMER_NORESTART; |
| 3639 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3641 | /* When work queue schedules work, it calls this function. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3642 | static void sdebug_q_cmd_wq_complete(struct work_struct *work) |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3643 | { |
| 3644 | struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer, |
| 3645 | ew.work); |
| 3646 | sdebug_q_cmd_complete(sd_dp); |
| 3647 | } |
| 3648 | |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3649 | static bool got_shared_uuid; |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 3650 | static uuid_t shared_uuid; |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3651 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3652 | static struct sdebug_dev_info *sdebug_device_create( |
| 3653 | struct sdebug_host_info *sdbg_host, gfp_t flags) |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3654 | { |
| 3655 | struct sdebug_dev_info *devip; |
| 3656 | |
| 3657 | devip = kzalloc(sizeof(*devip), flags); |
| 3658 | if (devip) { |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3659 | if (sdebug_uuid_ctl == 1) |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 3660 | uuid_gen(&devip->lu_name); |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3661 | else if (sdebug_uuid_ctl == 2) { |
| 3662 | if (got_shared_uuid) |
| 3663 | devip->lu_name = shared_uuid; |
| 3664 | else { |
Christoph Hellwig | bf47643 | 2017-05-17 09:55:26 +0200 | [diff] [blame] | 3665 | uuid_gen(&shared_uuid); |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 3666 | got_shared_uuid = true; |
| 3667 | devip->lu_name = shared_uuid; |
| 3668 | } |
| 3669 | } |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3670 | devip->sdbg_host = sdbg_host; |
| 3671 | list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list); |
| 3672 | } |
| 3673 | return devip; |
| 3674 | } |
| 3675 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3676 | 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] | 3677 | { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3678 | struct sdebug_host_info *sdbg_host; |
| 3679 | struct sdebug_dev_info *open_devip = NULL; |
| 3680 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | |
FUJITA Tomonori | d1e4c9c | 2008-03-02 18:30:18 +0900 | [diff] [blame] | 3682 | sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host); |
| 3683 | if (!sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3684 | pr_err("Host info NULL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | return NULL; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 3686 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3687 | list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) { |
| 3688 | if ((devip->used) && (devip->channel == sdev->channel) && |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 3689 | (devip->target == sdev->id) && |
| 3690 | (devip->lun == sdev->lun)) |
| 3691 | return devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3692 | else { |
| 3693 | if ((!devip->used) && (!open_devip)) |
| 3694 | open_devip = devip; |
| 3695 | } |
| 3696 | } |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3697 | if (!open_devip) { /* try and make a new one */ |
| 3698 | open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC); |
| 3699 | if (!open_devip) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3700 | pr_err("out of memory at line %d\n", __LINE__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3701 | return NULL; |
| 3702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3703 | } |
FUJITA Tomonori | a75869d | 2008-03-20 11:09:17 +0900 | [diff] [blame] | 3704 | |
| 3705 | open_devip->channel = sdev->channel; |
| 3706 | open_devip->target = sdev->id; |
| 3707 | open_devip->lun = sdev->lun; |
| 3708 | open_devip->sdbg_host = sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3709 | atomic_set(&open_devip->num_in_q, 0); |
| 3710 | set_bit(SDEBUG_UA_POR, open_devip->uas_bm); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3711 | open_devip->used = true; |
FUJITA Tomonori | a75869d | 2008-03-20 11:09:17 +0900 | [diff] [blame] | 3712 | return open_devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | } |
| 3714 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3715 | static int scsi_debug_slave_alloc(struct scsi_device *sdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3717 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3718 | pr_info("slave_alloc <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3719 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 3720 | queue_flag_set_unlocked(QUEUE_FLAG_BIDI, sdp->request_queue); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3721 | return 0; |
| 3722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3723 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3724 | static int scsi_debug_slave_configure(struct scsi_device *sdp) |
| 3725 | { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3726 | struct sdebug_dev_info *devip = |
| 3727 | (struct sdebug_dev_info *)sdp->hostdata; |
FUJITA Tomonori | a34c4e9 | 2008-03-25 09:26:50 +0900 | [diff] [blame] | 3728 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3729 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3730 | pr_info("slave_configure <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3731 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3732 | if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN) |
| 3733 | sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN; |
| 3734 | if (devip == NULL) { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3735 | devip = find_build_dev_info(sdp); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3736 | if (devip == NULL) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3737 | return 1; /* no resources, will be marked offline */ |
| 3738 | } |
Christoph Hellwig | c8b09f6 | 2014-11-03 20:15:14 +0100 | [diff] [blame] | 3739 | sdp->hostdata = devip; |
Akinobu Mita | 6bb5e6e | 2014-06-02 22:56:49 +0900 | [diff] [blame] | 3740 | blk_queue_max_segment_size(sdp->request_queue, -1U); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3741 | if (sdebug_no_uld) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 3742 | sdp->no_uld_attach = 1; |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 3743 | config_cdb_len(sdp); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3744 | return 0; |
| 3745 | } |
| 3746 | |
| 3747 | static void scsi_debug_slave_destroy(struct scsi_device *sdp) |
| 3748 | { |
| 3749 | struct sdebug_dev_info *devip = |
| 3750 | (struct sdebug_dev_info *)sdp->hostdata; |
| 3751 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3752 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3753 | pr_info("slave_destroy <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3754 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
| 3755 | if (devip) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3756 | /* make this slot available for re-use */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3757 | devip->used = false; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3758 | sdp->hostdata = NULL; |
| 3759 | } |
| 3760 | } |
| 3761 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3762 | static void stop_qc_helper(struct sdebug_defer *sd_dp) |
| 3763 | { |
| 3764 | if (!sd_dp) |
| 3765 | return; |
| 3766 | if ((sdebug_jdelay > 0) || (sdebug_ndelay > 0)) |
| 3767 | hrtimer_cancel(&sd_dp->hrt); |
| 3768 | else if (sdebug_jdelay < 0) |
| 3769 | cancel_work_sync(&sd_dp->ew.work); |
| 3770 | } |
| 3771 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3772 | /* If @cmnd found deletes its timer or work queue and returns true; else |
| 3773 | returns false */ |
| 3774 | static bool stop_queued_cmnd(struct scsi_cmnd *cmnd) |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3775 | { |
| 3776 | unsigned long iflags; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3777 | int j, k, qmax, r_qmax; |
| 3778 | struct sdebug_queue *sqp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3779 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3780 | struct sdebug_dev_info *devip; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3781 | struct sdebug_defer *sd_dp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3782 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3783 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 3784 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 3785 | qmax = sdebug_max_queue; |
| 3786 | r_qmax = atomic_read(&retired_max_queue); |
| 3787 | if (r_qmax > qmax) |
| 3788 | qmax = r_qmax; |
| 3789 | for (k = 0; k < qmax; ++k) { |
| 3790 | if (test_bit(k, sqp->in_use_bm)) { |
| 3791 | sqcp = &sqp->qc_arr[k]; |
| 3792 | if (cmnd != sqcp->a_cmnd) |
| 3793 | continue; |
| 3794 | /* found */ |
| 3795 | devip = (struct sdebug_dev_info *) |
| 3796 | cmnd->device->hostdata; |
| 3797 | if (devip) |
| 3798 | atomic_dec(&devip->num_in_q); |
| 3799 | sqcp->a_cmnd = NULL; |
| 3800 | sd_dp = sqcp->sd_dp; |
| 3801 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 3802 | stop_qc_helper(sd_dp); |
| 3803 | clear_bit(k, sqp->in_use_bm); |
| 3804 | return true; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3805 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3806 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3807 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3808 | } |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3809 | return false; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3810 | } |
| 3811 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3812 | /* Deletes (stops) timers or work queues of all queued commands */ |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3813 | static void stop_all_queued(void) |
| 3814 | { |
| 3815 | unsigned long iflags; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3816 | int j, k; |
| 3817 | struct sdebug_queue *sqp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3818 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3819 | struct sdebug_dev_info *devip; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3820 | struct sdebug_defer *sd_dp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3821 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3822 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 3823 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 3824 | for (k = 0; k < SDEBUG_CANQUEUE; ++k) { |
| 3825 | if (test_bit(k, sqp->in_use_bm)) { |
| 3826 | sqcp = &sqp->qc_arr[k]; |
| 3827 | if (sqcp->a_cmnd == NULL) |
| 3828 | continue; |
| 3829 | devip = (struct sdebug_dev_info *) |
| 3830 | sqcp->a_cmnd->device->hostdata; |
| 3831 | if (devip) |
| 3832 | atomic_dec(&devip->num_in_q); |
| 3833 | sqcp->a_cmnd = NULL; |
| 3834 | sd_dp = sqcp->sd_dp; |
| 3835 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 3836 | stop_qc_helper(sd_dp); |
| 3837 | clear_bit(k, sqp->in_use_bm); |
| 3838 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3839 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3840 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3841 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3842 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3843 | } |
| 3844 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3845 | /* Free queued command memory on heap */ |
| 3846 | static void free_all_queued(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3847 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3848 | int j, k; |
| 3849 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3850 | struct sdebug_queued_cmd *sqcp; |
| 3851 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3852 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 3853 | for (k = 0; k < SDEBUG_CANQUEUE; ++k) { |
| 3854 | sqcp = &sqp->qc_arr[k]; |
| 3855 | kfree(sqcp->sd_dp); |
| 3856 | sqcp->sd_dp = NULL; |
| 3857 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3858 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3859 | } |
| 3860 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3861 | static int scsi_debug_abort(struct scsi_cmnd *SCpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3862 | { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3863 | bool ok; |
| 3864 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3865 | ++num_aborts; |
| 3866 | if (SCpnt) { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3867 | ok = stop_queued_cmnd(SCpnt); |
| 3868 | if (SCpnt->device && (SDEBUG_OPT_ALL_NOISE & sdebug_opts)) |
| 3869 | sdev_printk(KERN_INFO, SCpnt->device, |
| 3870 | "%s: command%s found\n", __func__, |
| 3871 | ok ? "" : " not"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3872 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3873 | return SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3874 | } |
| 3875 | |
| 3876 | static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt) |
| 3877 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3878 | ++num_dev_resets; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3879 | if (SCpnt && SCpnt->device) { |
| 3880 | struct scsi_device *sdp = SCpnt->device; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3881 | struct sdebug_dev_info *devip = |
| 3882 | (struct sdebug_dev_info *)sdp->hostdata; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3883 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3884 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3885 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3886 | if (devip) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3887 | set_bit(SDEBUG_UA_POR, devip->uas_bm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3888 | } |
| 3889 | return SUCCESS; |
| 3890 | } |
| 3891 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3892 | static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt) |
| 3893 | { |
| 3894 | struct sdebug_host_info *sdbg_host; |
| 3895 | struct sdebug_dev_info *devip; |
| 3896 | struct scsi_device *sdp; |
| 3897 | struct Scsi_Host *hp; |
| 3898 | int k = 0; |
| 3899 | |
| 3900 | ++num_target_resets; |
| 3901 | if (!SCpnt) |
| 3902 | goto lie; |
| 3903 | sdp = SCpnt->device; |
| 3904 | if (!sdp) |
| 3905 | goto lie; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3906 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3907 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
| 3908 | hp = sdp->host; |
| 3909 | if (!hp) |
| 3910 | goto lie; |
| 3911 | sdbg_host = *(struct sdebug_host_info **)shost_priv(hp); |
| 3912 | if (sdbg_host) { |
| 3913 | list_for_each_entry(devip, |
| 3914 | &sdbg_host->dev_info_list, |
| 3915 | dev_list) |
| 3916 | if (devip->target == sdp->id) { |
| 3917 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3918 | ++k; |
| 3919 | } |
| 3920 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3921 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3922 | sdev_printk(KERN_INFO, sdp, |
| 3923 | "%s: %d device(s) found in target\n", __func__, k); |
| 3924 | lie: |
| 3925 | return SUCCESS; |
| 3926 | } |
| 3927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3928 | static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt) |
| 3929 | { |
| 3930 | struct sdebug_host_info *sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3931 | struct sdebug_dev_info *devip; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 3932 | struct scsi_device *sdp; |
| 3933 | struct Scsi_Host *hp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3934 | int k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3936 | ++num_bus_resets; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3937 | if (!(SCpnt && SCpnt->device)) |
| 3938 | goto lie; |
| 3939 | sdp = SCpnt->device; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3940 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3941 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
| 3942 | hp = sdp->host; |
| 3943 | if (hp) { |
FUJITA Tomonori | d1e4c9c | 2008-03-02 18:30:18 +0900 | [diff] [blame] | 3944 | sdbg_host = *(struct sdebug_host_info **)shost_priv(hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3945 | if (sdbg_host) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3946 | list_for_each_entry(devip, |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 3947 | &sdbg_host->dev_info_list, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3948 | dev_list) { |
| 3949 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3950 | ++k; |
| 3951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3952 | } |
| 3953 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3954 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3955 | sdev_printk(KERN_INFO, sdp, |
| 3956 | "%s: %d device(s) found in host\n", __func__, k); |
| 3957 | lie: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3958 | return SUCCESS; |
| 3959 | } |
| 3960 | |
| 3961 | static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt) |
| 3962 | { |
| 3963 | struct sdebug_host_info * sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3964 | struct sdebug_dev_info *devip; |
| 3965 | int k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | ++num_host_resets; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3968 | if ((SCpnt->device) && (SDEBUG_OPT_ALL_NOISE & sdebug_opts)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3969 | sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 3970 | spin_lock(&sdebug_host_list_lock); |
| 3971 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3972 | list_for_each_entry(devip, &sdbg_host->dev_info_list, |
| 3973 | dev_list) { |
| 3974 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3975 | ++k; |
| 3976 | } |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 3977 | } |
| 3978 | spin_unlock(&sdebug_host_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | stop_all_queued(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3980 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3981 | sdev_printk(KERN_INFO, SCpnt->device, |
| 3982 | "%s: %d device(s) found\n", __func__, k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3983 | return SUCCESS; |
| 3984 | } |
| 3985 | |
FUJITA Tomonori | f58b0ef | 2008-03-30 00:59:54 +0900 | [diff] [blame] | 3986 | static void __init sdebug_build_parts(unsigned char *ramp, |
FUJITA Tomonori | 5f2578e | 2008-03-30 00:59:57 +0900 | [diff] [blame] | 3987 | unsigned long store_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3988 | { |
| 3989 | struct partition * pp; |
| 3990 | int starts[SDEBUG_MAX_PARTS + 2]; |
| 3991 | int sectors_per_part, num_sectors, k; |
| 3992 | int heads_by_sects, start_sec, end_sec; |
| 3993 | |
| 3994 | /* assume partition table already zeroed */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3995 | if ((sdebug_num_parts < 1) || (store_size < 1048576)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3996 | return; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3997 | if (sdebug_num_parts > SDEBUG_MAX_PARTS) { |
| 3998 | sdebug_num_parts = SDEBUG_MAX_PARTS; |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3999 | pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4000 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4001 | num_sectors = (int)sdebug_store_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | sectors_per_part = (num_sectors - sdebug_sectors_per) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4003 | / sdebug_num_parts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4004 | heads_by_sects = sdebug_heads * sdebug_sectors_per; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4005 | starts[0] = sdebug_sectors_per; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4006 | for (k = 1; k < sdebug_num_parts; ++k) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | starts[k] = ((k * sectors_per_part) / heads_by_sects) |
| 4008 | * heads_by_sects; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4009 | starts[sdebug_num_parts] = num_sectors; |
| 4010 | starts[sdebug_num_parts + 1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | |
| 4012 | ramp[510] = 0x55; /* magic partition markings */ |
| 4013 | ramp[511] = 0xAA; |
| 4014 | pp = (struct partition *)(ramp + 0x1be); |
| 4015 | for (k = 0; starts[k + 1]; ++k, ++pp) { |
| 4016 | start_sec = starts[k]; |
| 4017 | end_sec = starts[k + 1] - 1; |
| 4018 | pp->boot_ind = 0; |
| 4019 | |
| 4020 | pp->cyl = start_sec / heads_by_sects; |
| 4021 | pp->head = (start_sec - (pp->cyl * heads_by_sects)) |
| 4022 | / sdebug_sectors_per; |
| 4023 | pp->sector = (start_sec % sdebug_sectors_per) + 1; |
| 4024 | |
| 4025 | pp->end_cyl = end_sec / heads_by_sects; |
| 4026 | pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects)) |
| 4027 | / sdebug_sectors_per; |
| 4028 | pp->end_sector = (end_sec % sdebug_sectors_per) + 1; |
| 4029 | |
Akinobu Mita | 150c354 | 2013-08-26 22:08:40 +0900 | [diff] [blame] | 4030 | pp->start_sect = cpu_to_le32(start_sec); |
| 4031 | pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4032 | pp->sys_ind = 0x83; /* plain Linux partition */ |
| 4033 | } |
| 4034 | } |
| 4035 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4036 | static void block_unblock_all_queues(bool block) |
| 4037 | { |
| 4038 | int j; |
| 4039 | struct sdebug_queue *sqp; |
| 4040 | |
| 4041 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) |
| 4042 | atomic_set(&sqp->blocked, (int)block); |
| 4043 | } |
| 4044 | |
| 4045 | /* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1 |
| 4046 | * commands will be processed normally before triggers occur. |
| 4047 | */ |
| 4048 | static void tweak_cmnd_count(void) |
| 4049 | { |
| 4050 | int count, modulo; |
| 4051 | |
| 4052 | modulo = abs(sdebug_every_nth); |
| 4053 | if (modulo < 2) |
| 4054 | return; |
| 4055 | block_unblock_all_queues(true); |
| 4056 | count = atomic_read(&sdebug_cmnd_count); |
| 4057 | atomic_set(&sdebug_cmnd_count, (count / modulo) * modulo); |
| 4058 | block_unblock_all_queues(false); |
| 4059 | } |
| 4060 | |
| 4061 | static void clear_queue_stats(void) |
| 4062 | { |
| 4063 | atomic_set(&sdebug_cmnd_count, 0); |
| 4064 | atomic_set(&sdebug_completions, 0); |
| 4065 | atomic_set(&sdebug_miss_cpus, 0); |
| 4066 | atomic_set(&sdebug_a_tsf, 0); |
| 4067 | } |
| 4068 | |
| 4069 | static void setup_inject(struct sdebug_queue *sqp, |
| 4070 | struct sdebug_queued_cmd *sqcp) |
| 4071 | { |
| 4072 | if ((atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) > 0) |
| 4073 | return; |
| 4074 | sqcp->inj_recovered = !!(SDEBUG_OPT_RECOVERED_ERR & sdebug_opts); |
| 4075 | sqcp->inj_transport = !!(SDEBUG_OPT_TRANSPORT_ERR & sdebug_opts); |
| 4076 | sqcp->inj_dif = !!(SDEBUG_OPT_DIF_ERR & sdebug_opts); |
| 4077 | sqcp->inj_dix = !!(SDEBUG_OPT_DIX_ERR & sdebug_opts); |
| 4078 | sqcp->inj_short = !!(SDEBUG_OPT_SHORT_TRANSFER & sdebug_opts); |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 4079 | sqcp->inj_host_busy = !!(SDEBUG_OPT_HOST_BUSY & sdebug_opts); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | /* Complete the processing of the thread that queued a SCSI command to this |
| 4083 | * driver. It either completes the command by calling cmnd_done() or |
| 4084 | * schedules a hr timer or work queue then returns 0. Returns |
| 4085 | * SCSI_MLQUEUE_HOST_BUSY if temporarily out of resources. |
| 4086 | */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4087 | static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip, |
| 4088 | int scsi_result, int delta_jiff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4090 | unsigned long iflags; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4091 | int k, num_in_q, qdepth, inject; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4092 | struct sdebug_queue *sqp; |
| 4093 | struct sdebug_queued_cmd *sqcp; |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 4094 | struct scsi_device *sdp; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4095 | struct sdebug_defer *sd_dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4096 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4097 | if (unlikely(devip == NULL)) { |
| 4098 | if (scsi_result == 0) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4099 | scsi_result = DID_NO_CONNECT << 16; |
| 4100 | goto respond_in_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | } |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 4102 | sdp = cmnd->device; |
| 4103 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4104 | if (unlikely(sdebug_verbose && scsi_result)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4105 | sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n", |
| 4106 | __func__, scsi_result); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4107 | if (delta_jiff == 0) |
| 4108 | goto respond_in_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4109 | |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4110 | /* schedule the response at a later time if resources permit */ |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4111 | sqp = get_queue(cmnd); |
| 4112 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 4113 | if (unlikely(atomic_read(&sqp->blocked))) { |
| 4114 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 4115 | return SCSI_MLQUEUE_HOST_BUSY; |
| 4116 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4117 | num_in_q = atomic_read(&devip->num_in_q); |
| 4118 | qdepth = cmnd->device->queue_depth; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4119 | inject = 0; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4120 | if (unlikely((qdepth > 0) && (num_in_q >= qdepth))) { |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4121 | if (scsi_result) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4122 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4123 | goto respond_in_thread; |
| 4124 | } else |
| 4125 | scsi_result = device_qfull_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4126 | } else if (unlikely(sdebug_every_nth && |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4127 | (SDEBUG_OPT_RARE_TSF & sdebug_opts) && |
| 4128 | (scsi_result == 0))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4129 | if ((num_in_q == (qdepth - 1)) && |
| 4130 | (atomic_inc_return(&sdebug_a_tsf) >= |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4131 | abs(sdebug_every_nth))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4132 | atomic_set(&sdebug_a_tsf, 0); |
| 4133 | inject = 1; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4134 | scsi_result = device_qfull_result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4136 | } |
| 4137 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4138 | k = find_first_zero_bit(sqp->in_use_bm, sdebug_max_queue); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4139 | if (unlikely(k >= sdebug_max_queue)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4140 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4141 | if (scsi_result) |
| 4142 | goto respond_in_thread; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4143 | else if (SDEBUG_OPT_ALL_TSF & sdebug_opts) |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4144 | scsi_result = device_qfull_result; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4145 | if (SDEBUG_OPT_Q_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4146 | sdev_printk(KERN_INFO, sdp, |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4147 | "%s: max_queue=%d exceeded, %s\n", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4148 | __func__, sdebug_max_queue, |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4149 | (scsi_result ? "status: TASK SET FULL" : |
| 4150 | "report: host busy")); |
| 4151 | if (scsi_result) |
| 4152 | goto respond_in_thread; |
| 4153 | else |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4154 | return SCSI_MLQUEUE_HOST_BUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4155 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4156 | __set_bit(k, sqp->in_use_bm); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4157 | atomic_inc(&devip->num_in_q); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4158 | sqcp = &sqp->qc_arr[k]; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4159 | sqcp->a_cmnd = cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4160 | cmnd->host_scribble = (unsigned char *)sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4161 | cmnd->result = scsi_result; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4162 | sd_dp = sqcp->sd_dp; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4163 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 4164 | if (unlikely(sdebug_every_nth && sdebug_any_injecting_opt)) |
| 4165 | setup_inject(sqp, sqcp); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4166 | if (delta_jiff > 0 || sdebug_ndelay > 0) { |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 4167 | ktime_t kt; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4168 | |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 4169 | if (delta_jiff > 0) { |
Arnd Bergmann | 13f6b61 | 2017-11-27 12:36:25 +0100 | [diff] [blame] | 4170 | kt = ns_to_ktime((u64)delta_jiff * (NSEC_PER_SEC / HZ)); |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 4171 | } else |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 4172 | kt = sdebug_ndelay; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4173 | if (NULL == sd_dp) { |
| 4174 | sd_dp = kzalloc(sizeof(*sd_dp), GFP_ATOMIC); |
| 4175 | if (NULL == sd_dp) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4176 | return SCSI_MLQUEUE_HOST_BUSY; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4177 | sqcp->sd_dp = sd_dp; |
| 4178 | hrtimer_init(&sd_dp->hrt, CLOCK_MONOTONIC, |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4179 | HRTIMER_MODE_REL_PINNED); |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4180 | sd_dp->hrt.function = sdebug_q_cmd_hrt_complete; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4181 | sd_dp->sqa_idx = sqp - sdebug_q_arr; |
| 4182 | sd_dp->qc_idx = k; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4183 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4184 | if (sdebug_statistics) |
| 4185 | sd_dp->issuing_cpu = raw_smp_processor_id(); |
| 4186 | hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL_PINNED); |
| 4187 | } else { /* jdelay < 0, use work queue */ |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4188 | if (NULL == sd_dp) { |
| 4189 | sd_dp = kzalloc(sizeof(*sqcp->sd_dp), GFP_ATOMIC); |
| 4190 | if (NULL == sd_dp) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4191 | return SCSI_MLQUEUE_HOST_BUSY; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4192 | sqcp->sd_dp = sd_dp; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4193 | sd_dp->sqa_idx = sqp - sdebug_q_arr; |
| 4194 | sd_dp->qc_idx = k; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4195 | INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4196 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4197 | if (sdebug_statistics) |
| 4198 | sd_dp->issuing_cpu = raw_smp_processor_id(); |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4199 | schedule_work(&sd_dp->ew.work); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4200 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4201 | if (unlikely((SDEBUG_OPT_Q_NOISE & sdebug_opts) && |
| 4202 | (scsi_result == device_qfull_result))) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4203 | sdev_printk(KERN_INFO, sdp, |
| 4204 | "%s: num_in_q=%d +1, %s%s\n", __func__, |
| 4205 | num_in_q, (inject ? "<inject> " : ""), |
| 4206 | "status: TASK SET FULL"); |
| 4207 | return 0; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4208 | |
| 4209 | respond_in_thread: /* call back to mid-layer using invocation thread */ |
| 4210 | cmnd->result = scsi_result; |
| 4211 | cmnd->scsi_done(cmnd); |
| 4212 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4214 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4215 | /* Note: The following macros create attribute files in the |
| 4216 | /sys/module/scsi_debug/parameters directory. Unfortunately this |
| 4217 | driver is unaware of a change and cannot trigger auxiliary actions |
| 4218 | as it can when the corresponding attribute in the |
| 4219 | /sys/bus/pseudo/drivers/scsi_debug directory is changed. |
| 4220 | */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4221 | module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR); |
| 4222 | module_param_named(ato, sdebug_ato, int, S_IRUGO); |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 4223 | module_param_named(cdb_len, sdebug_cdb_len, int, 0644); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4224 | module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR); |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4225 | module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4226 | module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO); |
| 4227 | module_param_named(dif, sdebug_dif, int, S_IRUGO); |
| 4228 | module_param_named(dix, sdebug_dix, int, S_IRUGO); |
| 4229 | module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR); |
| 4230 | module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR); |
| 4231 | module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR); |
| 4232 | module_param_named(guard, sdebug_guard, uint, S_IRUGO); |
| 4233 | 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] | 4234 | module_param_string(inq_vendor, sdebug_inq_vendor_id, |
| 4235 | sizeof(sdebug_inq_vendor_id), S_IRUGO|S_IWUSR); |
| 4236 | module_param_string(inq_product, sdebug_inq_product_id, |
| 4237 | sizeof(sdebug_inq_product_id), S_IRUGO|S_IWUSR); |
| 4238 | module_param_string(inq_rev, sdebug_inq_product_rev, |
| 4239 | sizeof(sdebug_inq_product_rev), S_IRUGO|S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4240 | module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO); |
| 4241 | module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO); |
| 4242 | module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO); |
| 4243 | module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO); |
| 4244 | module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO); |
| 4245 | module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR); |
| 4246 | module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR); |
| 4247 | module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR); |
| 4248 | module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR); |
| 4249 | module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO); |
| 4250 | module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO); |
| 4251 | module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR); |
| 4252 | module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO); |
| 4253 | module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR); |
| 4254 | module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO); |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 4255 | 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] | 4256 | module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR); |
| 4257 | module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR); |
| 4258 | module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO); |
| 4259 | module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4260 | module_param_named(statistics, sdebug_statistics, bool, S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4261 | module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4262 | module_param_named(submit_queues, submit_queues, int, S_IRUGO); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4263 | module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO); |
| 4264 | module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO); |
| 4265 | module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO); |
| 4266 | module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO); |
| 4267 | 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] | 4268 | module_param_named(uuid_ctl, sdebug_uuid_ctl, int, S_IRUGO); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4269 | module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int, |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4270 | S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4271 | module_param_named(write_same_length, sdebug_write_same_length, int, |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4272 | S_IRUGO | S_IWUSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | |
| 4274 | MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert"); |
| 4275 | MODULE_DESCRIPTION("SCSI debug adapter driver"); |
| 4276 | MODULE_LICENSE("GPL"); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4277 | MODULE_VERSION(SDEBUG_VERSION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4278 | |
| 4279 | MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4280 | 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] | 4281 | MODULE_PARM_DESC(cdb_len, "suggest CDB lengths to drivers (def=10)"); |
Akinobu Mita | 0759c66 | 2014-02-26 22:57:04 +0900 | [diff] [blame] | 4282 | MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4283 | 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] | 4284 | 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] | 4285 | MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)"); |
| 4286 | MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4287 | MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)"); |
Randy Dunlap | beb87c3 | 2007-06-11 11:36:40 -0700 | [diff] [blame] | 4288 | MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)"); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4289 | 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] | 4290 | MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)"); |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 4291 | MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)"); |
Hannes Reinecke | e5203cf | 2017-10-02 16:26:33 +0200 | [diff] [blame] | 4292 | MODULE_PARM_DESC(inq_vendor, "SCSI INQUIRY vendor string (def=\"Linux\")"); |
| 4293 | MODULE_PARM_DESC(inq_product, "SCSI INQUIRY product string (def=\"scsi_debug\")"); |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 4294 | MODULE_PARM_DESC(inq_rev, "SCSI INQUIRY revision string (def=\"" |
| 4295 | SDEBUG_VERSION "\")"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4296 | MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)"); |
| 4297 | MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)"); |
| 4298 | 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] | 4299 | MODULE_PARM_DESC(lbprz, |
| 4300 | "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] | 4301 | MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4302 | 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] | 4303 | MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))"); |
| 4304 | MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4305 | 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] | 4306 | 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] | 4307 | MODULE_PARM_DESC(num_parts, "number of partitions(def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4308 | 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] | 4309 | MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)"); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 4310 | 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] | 4311 | MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)"); |
Lukas Herbolt | 86e6828 | 2017-01-26 10:00:37 +0100 | [diff] [blame] | 4312 | 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] | 4313 | MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])"); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4314 | MODULE_PARM_DESC(removable, "claim to have removable media (def=0)"); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 4315 | 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] | 4316 | MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4317 | MODULE_PARM_DESC(statistics, "collect statistics on commands, queues (def=0)"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4318 | MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4319 | 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] | 4320 | MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)"); |
| 4321 | 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] | 4322 | MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)"); |
| 4323 | 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] | 4324 | MODULE_PARM_DESC(uuid_ctl, |
| 4325 | "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] | 4326 | 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] | 4327 | MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)"); |
| 4328 | 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] | 4329 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 4330 | #define SDEBUG_INFO_LEN 256 |
| 4331 | static char sdebug_info[SDEBUG_INFO_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | |
| 4333 | static const char * scsi_debug_info(struct Scsi_Host * shp) |
| 4334 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4335 | int k; |
| 4336 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 4337 | k = scnprintf(sdebug_info, SDEBUG_INFO_LEN, "%s: version %s [%s]\n", |
| 4338 | my_name, SDEBUG_VERSION, sdebug_version_date); |
| 4339 | if (k >= (SDEBUG_INFO_LEN - 1)) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4340 | return sdebug_info; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame] | 4341 | scnprintf(sdebug_info + k, SDEBUG_INFO_LEN - k, |
| 4342 | " dev_size_mb=%d, opts=0x%x, submit_queues=%d, %s=%d", |
| 4343 | sdebug_dev_size_mb, sdebug_opts, submit_queues, |
| 4344 | "statistics", (int)sdebug_statistics); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | return sdebug_info; |
| 4346 | } |
| 4347 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4348 | /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4349 | static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer, |
| 4350 | int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4351 | { |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4352 | char arr[16]; |
| 4353 | int opts; |
| 4354 | int minLen = length > 15 ? 15 : length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4356 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 4357 | return -EACCES; |
| 4358 | memcpy(arr, buffer, minLen); |
| 4359 | arr[minLen] = '\0'; |
| 4360 | if (1 != sscanf(arr, "%d", &opts)) |
| 4361 | return -EINVAL; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4362 | sdebug_opts = opts; |
| 4363 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); |
| 4364 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts); |
| 4365 | if (sdebug_every_nth != 0) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4366 | tweak_cmnd_count(); |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4367 | return length; |
| 4368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4370 | /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the |
| 4371 | * same for each scsi_debug host (if more than one). Some of the counters |
| 4372 | * output are not atomics so might be inaccurate in a busy system. */ |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4373 | static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host) |
| 4374 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4375 | int f, j, l; |
| 4376 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4377 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4378 | seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n", |
| 4379 | SDEBUG_VERSION, sdebug_version_date); |
| 4380 | seq_printf(m, "num_tgts=%d, %ssize=%d MB, opts=0x%x, every_nth=%d\n", |
| 4381 | sdebug_num_tgts, "shared (ram) ", sdebug_dev_size_mb, |
| 4382 | sdebug_opts, sdebug_every_nth); |
| 4383 | seq_printf(m, "delay=%d, ndelay=%d, max_luns=%d, sector_size=%d %s\n", |
| 4384 | sdebug_jdelay, sdebug_ndelay, sdebug_max_luns, |
| 4385 | sdebug_sector_size, "bytes"); |
| 4386 | seq_printf(m, "cylinders=%d, heads=%d, sectors=%d, command aborts=%d\n", |
| 4387 | sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per, |
| 4388 | num_aborts); |
| 4389 | seq_printf(m, "RESETs: device=%d, target=%d, bus=%d, host=%d\n", |
| 4390 | num_dev_resets, num_target_resets, num_bus_resets, |
| 4391 | num_host_resets); |
| 4392 | seq_printf(m, "dix_reads=%d, dix_writes=%d, dif_errors=%d\n", |
| 4393 | dix_reads, dix_writes, dif_errors); |
| 4394 | seq_printf(m, "usec_in_jiffy=%lu, %s=%d, mq_active=%d\n", |
| 4395 | TICK_NSEC / 1000, "statistics", sdebug_statistics, |
| 4396 | sdebug_mq_active); |
| 4397 | seq_printf(m, "cmnd_count=%d, completions=%d, %s=%d, a_tsf=%d\n", |
| 4398 | atomic_read(&sdebug_cmnd_count), |
| 4399 | atomic_read(&sdebug_completions), |
| 4400 | "miss_cpus", atomic_read(&sdebug_miss_cpus), |
| 4401 | atomic_read(&sdebug_a_tsf)); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4402 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4403 | seq_printf(m, "submit_queues=%d\n", submit_queues); |
| 4404 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 4405 | seq_printf(m, " queue %d:\n", j); |
| 4406 | f = find_first_bit(sqp->in_use_bm, sdebug_max_queue); |
| 4407 | if (f != sdebug_max_queue) { |
| 4408 | l = find_last_bit(sqp->in_use_bm, sdebug_max_queue); |
| 4409 | seq_printf(m, " in_use_bm BUSY: %s: %d,%d\n", |
| 4410 | "first,last bits", f, l); |
| 4411 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4412 | } |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4413 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | } |
| 4415 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4416 | static ssize_t delay_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | { |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4418 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4420 | /* Returns -EBUSY if jdelay is being changed and commands are queued. The unit |
| 4421 | * of delay is jiffies. |
| 4422 | */ |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4423 | static ssize_t delay_store(struct device_driver *ddp, const char *buf, |
| 4424 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | { |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4426 | int jdelay, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4428 | if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4429 | res = count; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4430 | if (sdebug_jdelay != jdelay) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4431 | int j, k; |
| 4432 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4433 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4434 | block_unblock_all_queues(true); |
| 4435 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; |
| 4436 | ++j, ++sqp) { |
| 4437 | k = find_first_bit(sqp->in_use_bm, |
| 4438 | sdebug_max_queue); |
| 4439 | if (k != sdebug_max_queue) { |
| 4440 | res = -EBUSY; /* queued commands */ |
| 4441 | break; |
| 4442 | } |
| 4443 | } |
| 4444 | if (res > 0) { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4445 | /* make sure sdebug_defer instances get |
| 4446 | * re-allocated for new delay variant */ |
| 4447 | free_all_queued(); |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4448 | sdebug_jdelay = jdelay; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4449 | sdebug_ndelay = 0; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4450 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4451 | block_unblock_all_queues(false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4453 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | } |
| 4455 | return -EINVAL; |
| 4456 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4457 | static DRIVER_ATTR_RW(delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4459 | static ssize_t ndelay_show(struct device_driver *ddp, char *buf) |
| 4460 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4461 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4462 | } |
| 4463 | /* Returns -EBUSY if ndelay is being changed and commands are queued */ |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4464 | /* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4465 | static ssize_t ndelay_store(struct device_driver *ddp, const char *buf, |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4466 | size_t count) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4467 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4468 | int ndelay, res; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4469 | |
| 4470 | if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) && |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4471 | (ndelay >= 0) && (ndelay < (1000 * 1000 * 1000))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4472 | res = count; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4473 | if (sdebug_ndelay != ndelay) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4474 | int j, k; |
| 4475 | struct sdebug_queue *sqp; |
| 4476 | |
| 4477 | block_unblock_all_queues(true); |
| 4478 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; |
| 4479 | ++j, ++sqp) { |
| 4480 | k = find_first_bit(sqp->in_use_bm, |
| 4481 | sdebug_max_queue); |
| 4482 | if (k != sdebug_max_queue) { |
| 4483 | res = -EBUSY; /* queued commands */ |
| 4484 | break; |
| 4485 | } |
| 4486 | } |
| 4487 | if (res > 0) { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4488 | /* make sure sdebug_defer instances get |
| 4489 | * re-allocated for new delay variant */ |
| 4490 | free_all_queued(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4491 | sdebug_ndelay = ndelay; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4492 | sdebug_jdelay = ndelay ? JDELAY_OVERRIDDEN |
| 4493 | : DEF_JDELAY; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4494 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4495 | block_unblock_all_queues(false); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4496 | } |
| 4497 | return res; |
| 4498 | } |
| 4499 | return -EINVAL; |
| 4500 | } |
| 4501 | static DRIVER_ATTR_RW(ndelay); |
| 4502 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4503 | static ssize_t opts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4505 | return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | } |
| 4507 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4508 | static ssize_t opts_store(struct device_driver *ddp, const char *buf, |
| 4509 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4511 | int opts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4512 | char work[20]; |
| 4513 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4514 | if (sscanf(buf, "%10s", work) == 1) { |
| 4515 | if (strncasecmp(work, "0x", 2) == 0) { |
| 4516 | if (kstrtoint(work + 2, 16, &opts) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | goto opts_done; |
| 4518 | } else { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4519 | if (kstrtoint(work, 10, &opts) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | goto opts_done; |
| 4521 | } |
| 4522 | } |
| 4523 | return -EINVAL; |
| 4524 | opts_done: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4525 | sdebug_opts = opts; |
| 4526 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); |
| 4527 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4528 | tweak_cmnd_count(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | return count; |
| 4530 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4531 | static DRIVER_ATTR_RW(opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4533 | static ssize_t ptype_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4535 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4537 | static ssize_t ptype_store(struct device_driver *ddp, const char *buf, |
| 4538 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4540 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | |
| 4542 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4543 | sdebug_ptype = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | return count; |
| 4545 | } |
| 4546 | return -EINVAL; |
| 4547 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4548 | static DRIVER_ATTR_RW(ptype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4549 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4550 | static ssize_t dsense_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4552 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4553 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4554 | static ssize_t dsense_store(struct device_driver *ddp, const char *buf, |
| 4555 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4556 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4557 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | |
| 4559 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4560 | sdebug_dsense = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | return count; |
| 4562 | } |
| 4563 | return -EINVAL; |
| 4564 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4565 | static DRIVER_ATTR_RW(dsense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4567 | static ssize_t fake_rw_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4568 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4569 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4570 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4571 | static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf, |
| 4572 | size_t count) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4573 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4574 | int n; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4575 | |
| 4576 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4577 | n = (n > 0); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4578 | sdebug_fake_rw = (sdebug_fake_rw > 0); |
| 4579 | if (sdebug_fake_rw != n) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4580 | if ((0 == n) && (NULL == fake_storep)) { |
| 4581 | unsigned long sz = |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4582 | (unsigned long)sdebug_dev_size_mb * |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4583 | 1048576; |
| 4584 | |
| 4585 | fake_storep = vmalloc(sz); |
| 4586 | if (NULL == fake_storep) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4587 | pr_err("out of memory, 9\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4588 | return -ENOMEM; |
| 4589 | } |
| 4590 | memset(fake_storep, 0, sz); |
| 4591 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4592 | sdebug_fake_rw = n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4593 | } |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4594 | return count; |
| 4595 | } |
| 4596 | return -EINVAL; |
| 4597 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4598 | static DRIVER_ATTR_RW(fake_rw); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4599 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4600 | 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] | 4601 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4602 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4603 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4604 | static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf, |
| 4605 | size_t count) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4606 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4607 | int n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4608 | |
| 4609 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4610 | sdebug_no_lun_0 = n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4611 | return count; |
| 4612 | } |
| 4613 | return -EINVAL; |
| 4614 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4615 | static DRIVER_ATTR_RW(no_lun_0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4616 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4617 | static ssize_t num_tgts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4619 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4621 | static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf, |
| 4622 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4624 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | |
| 4626 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4627 | sdebug_num_tgts = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | sdebug_max_tgts_luns(); |
| 4629 | return count; |
| 4630 | } |
| 4631 | return -EINVAL; |
| 4632 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4633 | static DRIVER_ATTR_RW(num_tgts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4635 | 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] | 4636 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4637 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4639 | static DRIVER_ATTR_RO(dev_size_mb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4641 | static ssize_t num_parts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4643 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4645 | static DRIVER_ATTR_RO(num_parts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4647 | static ssize_t every_nth_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4649 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4651 | static ssize_t every_nth_store(struct device_driver *ddp, const char *buf, |
| 4652 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4654 | int nth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | |
| 4656 | if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4657 | sdebug_every_nth = nth; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4658 | if (nth && !sdebug_statistics) { |
| 4659 | pr_info("every_nth needs statistics=1, set it\n"); |
| 4660 | sdebug_statistics = true; |
| 4661 | } |
| 4662 | tweak_cmnd_count(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4663 | return count; |
| 4664 | } |
| 4665 | return -EINVAL; |
| 4666 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4667 | static DRIVER_ATTR_RW(every_nth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4668 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4669 | static ssize_t max_luns_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4670 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4671 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4672 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4673 | static ssize_t max_luns_store(struct device_driver *ddp, const char *buf, |
| 4674 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4676 | int n; |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 4677 | bool changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | |
| 4679 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 4680 | if (n > 256) { |
| 4681 | pr_warn("max_luns can be no more than 256\n"); |
| 4682 | return -EINVAL; |
| 4683 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4684 | changed = (sdebug_max_luns != n); |
| 4685 | sdebug_max_luns = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4686 | sdebug_max_tgts_luns(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4687 | if (changed && (sdebug_scsi_level >= 5)) { /* >= SPC-3 */ |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 4688 | struct sdebug_host_info *sdhp; |
| 4689 | struct sdebug_dev_info *dp; |
| 4690 | |
| 4691 | spin_lock(&sdebug_host_list_lock); |
| 4692 | list_for_each_entry(sdhp, &sdebug_host_list, |
| 4693 | host_list) { |
| 4694 | list_for_each_entry(dp, &sdhp->dev_info_list, |
| 4695 | dev_list) { |
| 4696 | set_bit(SDEBUG_UA_LUNS_CHANGED, |
| 4697 | dp->uas_bm); |
| 4698 | } |
| 4699 | } |
| 4700 | spin_unlock(&sdebug_host_list_lock); |
| 4701 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | return count; |
| 4703 | } |
| 4704 | return -EINVAL; |
| 4705 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4706 | static DRIVER_ATTR_RW(max_luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4707 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4708 | static ssize_t max_queue_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4709 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4710 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4711 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4712 | /* N.B. max_queue can be changed while there are queued commands. In flight |
| 4713 | * commands beyond the new max_queue will be completed. */ |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4714 | static ssize_t max_queue_store(struct device_driver *ddp, const char *buf, |
| 4715 | size_t count) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4716 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4717 | int j, n, k, a; |
| 4718 | struct sdebug_queue *sqp; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4719 | |
| 4720 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) && |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4721 | (n <= SDEBUG_CANQUEUE)) { |
| 4722 | block_unblock_all_queues(true); |
| 4723 | k = 0; |
| 4724 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; |
| 4725 | ++j, ++sqp) { |
| 4726 | a = find_last_bit(sqp->in_use_bm, SDEBUG_CANQUEUE); |
| 4727 | if (a > k) |
| 4728 | k = a; |
| 4729 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4730 | sdebug_max_queue = n; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4731 | if (k == SDEBUG_CANQUEUE) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4732 | atomic_set(&retired_max_queue, 0); |
| 4733 | else if (k >= n) |
| 4734 | atomic_set(&retired_max_queue, k + 1); |
| 4735 | else |
| 4736 | atomic_set(&retired_max_queue, 0); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4737 | block_unblock_all_queues(false); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4738 | return count; |
| 4739 | } |
| 4740 | return -EINVAL; |
| 4741 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4742 | static DRIVER_ATTR_RW(max_queue); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4743 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4744 | static ssize_t no_uld_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4745 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4746 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4747 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4748 | static DRIVER_ATTR_RO(no_uld); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4749 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4750 | static ssize_t scsi_level_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4751 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4752 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4754 | static DRIVER_ATTR_RO(scsi_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4756 | static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4757 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4758 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4759 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4760 | static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf, |
| 4761 | size_t count) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4762 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 4763 | int n; |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4764 | bool changed; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4765 | |
| 4766 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4767 | changed = (sdebug_virtual_gb != n); |
| 4768 | sdebug_virtual_gb = n; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4769 | sdebug_capacity = get_sdebug_capacity(); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4770 | if (changed) { |
| 4771 | struct sdebug_host_info *sdhp; |
| 4772 | struct sdebug_dev_info *dp; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4773 | |
Ewan D. Milne | 4bc6b63 | 2014-12-04 11:49:26 -0500 | [diff] [blame] | 4774 | spin_lock(&sdebug_host_list_lock); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4775 | list_for_each_entry(sdhp, &sdebug_host_list, |
| 4776 | host_list) { |
| 4777 | list_for_each_entry(dp, &sdhp->dev_info_list, |
| 4778 | dev_list) { |
| 4779 | set_bit(SDEBUG_UA_CAPACITY_CHANGED, |
| 4780 | dp->uas_bm); |
| 4781 | } |
| 4782 | } |
Ewan D. Milne | 4bc6b63 | 2014-12-04 11:49:26 -0500 | [diff] [blame] | 4783 | spin_unlock(&sdebug_host_list_lock); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4784 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4785 | return count; |
| 4786 | } |
| 4787 | return -EINVAL; |
| 4788 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4789 | static DRIVER_ATTR_RW(virtual_gb); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4790 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4791 | static ssize_t add_host_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4792 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4793 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | } |
| 4795 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4796 | static int sdebug_add_adapter(void); |
| 4797 | static void sdebug_remove_adapter(void); |
| 4798 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4799 | static ssize_t add_host_store(struct device_driver *ddp, const char *buf, |
| 4800 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | { |
FUJITA Tomonori | f3df41c | 2008-03-20 11:09:15 +0900 | [diff] [blame] | 4802 | int delta_hosts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | |
FUJITA Tomonori | f3df41c | 2008-03-20 11:09:15 +0900 | [diff] [blame] | 4804 | if (sscanf(buf, "%d", &delta_hosts) != 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | if (delta_hosts > 0) { |
| 4807 | do { |
| 4808 | sdebug_add_adapter(); |
| 4809 | } while (--delta_hosts); |
| 4810 | } else if (delta_hosts < 0) { |
| 4811 | do { |
| 4812 | sdebug_remove_adapter(); |
| 4813 | } while (++delta_hosts); |
| 4814 | } |
| 4815 | return count; |
| 4816 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4817 | static DRIVER_ATTR_RW(add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4818 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4819 | 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] | 4820 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4821 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4822 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4823 | static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf, |
| 4824 | size_t count) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4825 | { |
| 4826 | int n; |
| 4827 | |
| 4828 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4829 | sdebug_vpd_use_hostno = n; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4830 | return count; |
| 4831 | } |
| 4832 | return -EINVAL; |
| 4833 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4834 | static DRIVER_ATTR_RW(vpd_use_hostno); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4835 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4836 | static ssize_t statistics_show(struct device_driver *ddp, char *buf) |
| 4837 | { |
| 4838 | return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_statistics); |
| 4839 | } |
| 4840 | static ssize_t statistics_store(struct device_driver *ddp, const char *buf, |
| 4841 | size_t count) |
| 4842 | { |
| 4843 | int n; |
| 4844 | |
| 4845 | if ((count > 0) && (sscanf(buf, "%d", &n) == 1) && (n >= 0)) { |
| 4846 | if (n > 0) |
| 4847 | sdebug_statistics = true; |
| 4848 | else { |
| 4849 | clear_queue_stats(); |
| 4850 | sdebug_statistics = false; |
| 4851 | } |
| 4852 | return count; |
| 4853 | } |
| 4854 | return -EINVAL; |
| 4855 | } |
| 4856 | static DRIVER_ATTR_RW(statistics); |
| 4857 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4858 | 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] | 4859 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4860 | return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size); |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4861 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4862 | static DRIVER_ATTR_RO(sector_size); |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4863 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4864 | static ssize_t submit_queues_show(struct device_driver *ddp, char *buf) |
| 4865 | { |
| 4866 | return scnprintf(buf, PAGE_SIZE, "%d\n", submit_queues); |
| 4867 | } |
| 4868 | static DRIVER_ATTR_RO(submit_queues); |
| 4869 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4870 | static ssize_t dix_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4871 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4872 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4873 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4874 | static DRIVER_ATTR_RO(dix); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4875 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4876 | static ssize_t dif_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4877 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4878 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4879 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4880 | static DRIVER_ATTR_RO(dif); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4881 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4882 | static ssize_t guard_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4883 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4884 | return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4885 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4886 | static DRIVER_ATTR_RO(guard); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4887 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4888 | static ssize_t ato_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4889 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4890 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4891 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4892 | static DRIVER_ATTR_RO(ato); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4893 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4894 | static ssize_t map_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4895 | { |
| 4896 | ssize_t count; |
| 4897 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4898 | if (!scsi_debug_lbp()) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4899 | return scnprintf(buf, PAGE_SIZE, "0-%u\n", |
| 4900 | sdebug_store_sectors); |
| 4901 | |
Tejun Heo | c7badc9 | 2015-02-13 14:37:51 -0800 | [diff] [blame] | 4902 | count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", |
| 4903 | (int)map_size, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4904 | buf[count++] = '\n'; |
Tejun Heo | c7badc9 | 2015-02-13 14:37:51 -0800 | [diff] [blame] | 4905 | buf[count] = '\0'; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4906 | |
| 4907 | return count; |
| 4908 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4909 | static DRIVER_ATTR_RO(map); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4910 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4911 | static ssize_t removable_show(struct device_driver *ddp, char *buf) |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4912 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4913 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4914 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4915 | static ssize_t removable_store(struct device_driver *ddp, const char *buf, |
| 4916 | size_t count) |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4917 | { |
| 4918 | int n; |
| 4919 | |
| 4920 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4921 | sdebug_removable = (n > 0); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4922 | return count; |
| 4923 | } |
| 4924 | return -EINVAL; |
| 4925 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4926 | static DRIVER_ATTR_RW(removable); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4927 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4928 | static ssize_t host_lock_show(struct device_driver *ddp, char *buf) |
| 4929 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4930 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4931 | } |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 4932 | /* N.B. sdebug_host_lock does nothing, kept for backward compatibility */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4933 | static ssize_t host_lock_store(struct device_driver *ddp, const char *buf, |
| 4934 | size_t count) |
| 4935 | { |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 4936 | int n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4937 | |
| 4938 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 4939 | sdebug_host_lock = (n > 0); |
| 4940 | return count; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4941 | } |
| 4942 | return -EINVAL; |
| 4943 | } |
| 4944 | static DRIVER_ATTR_RW(host_lock); |
| 4945 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4946 | static ssize_t strict_show(struct device_driver *ddp, char *buf) |
| 4947 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4948 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4949 | } |
| 4950 | static ssize_t strict_store(struct device_driver *ddp, const char *buf, |
| 4951 | size_t count) |
| 4952 | { |
| 4953 | int n; |
| 4954 | |
| 4955 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4956 | sdebug_strict = (n > 0); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4957 | return count; |
| 4958 | } |
| 4959 | return -EINVAL; |
| 4960 | } |
| 4961 | static DRIVER_ATTR_RW(strict); |
| 4962 | |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 4963 | static ssize_t uuid_ctl_show(struct device_driver *ddp, char *buf) |
| 4964 | { |
| 4965 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_uuid_ctl); |
| 4966 | } |
| 4967 | static DRIVER_ATTR_RO(uuid_ctl); |
| 4968 | |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 4969 | static ssize_t cdb_len_show(struct device_driver *ddp, char *buf) |
| 4970 | { |
| 4971 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_cdb_len); |
| 4972 | } |
| 4973 | static ssize_t cdb_len_store(struct device_driver *ddp, const char *buf, |
| 4974 | size_t count) |
| 4975 | { |
| 4976 | int ret, n; |
| 4977 | |
| 4978 | ret = kstrtoint(buf, 0, &n); |
| 4979 | if (ret) |
| 4980 | return ret; |
| 4981 | sdebug_cdb_len = n; |
| 4982 | all_config_cdb_len(); |
| 4983 | return count; |
| 4984 | } |
| 4985 | static DRIVER_ATTR_RW(cdb_len); |
| 4986 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4987 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4988 | /* Note: The following array creates attribute files in the |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4989 | /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these |
| 4990 | files (over those found in the /sys/module/scsi_debug/parameters |
| 4991 | directory) is that auxiliary actions can be triggered when an attribute |
| 4992 | is changed. For example see: sdebug_add_host_store() above. |
| 4993 | */ |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4994 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4995 | static struct attribute *sdebug_drv_attrs[] = { |
| 4996 | &driver_attr_delay.attr, |
| 4997 | &driver_attr_opts.attr, |
| 4998 | &driver_attr_ptype.attr, |
| 4999 | &driver_attr_dsense.attr, |
| 5000 | &driver_attr_fake_rw.attr, |
| 5001 | &driver_attr_no_lun_0.attr, |
| 5002 | &driver_attr_num_tgts.attr, |
| 5003 | &driver_attr_dev_size_mb.attr, |
| 5004 | &driver_attr_num_parts.attr, |
| 5005 | &driver_attr_every_nth.attr, |
| 5006 | &driver_attr_max_luns.attr, |
| 5007 | &driver_attr_max_queue.attr, |
| 5008 | &driver_attr_no_uld.attr, |
| 5009 | &driver_attr_scsi_level.attr, |
| 5010 | &driver_attr_virtual_gb.attr, |
| 5011 | &driver_attr_add_host.attr, |
| 5012 | &driver_attr_vpd_use_hostno.attr, |
| 5013 | &driver_attr_sector_size.attr, |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5014 | &driver_attr_statistics.attr, |
| 5015 | &driver_attr_submit_queues.attr, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5016 | &driver_attr_dix.attr, |
| 5017 | &driver_attr_dif.attr, |
| 5018 | &driver_attr_guard.attr, |
| 5019 | &driver_attr_ato.attr, |
| 5020 | &driver_attr_map.attr, |
| 5021 | &driver_attr_removable.attr, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5022 | &driver_attr_host_lock.attr, |
| 5023 | &driver_attr_ndelay.attr, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5024 | &driver_attr_strict.attr, |
Douglas Gilbert | 09ba24c | 2016-05-06 00:40:28 -0400 | [diff] [blame] | 5025 | &driver_attr_uuid_ctl.attr, |
Douglas Gilbert | 9b760fd | 2017-12-05 00:05:49 -0500 | [diff] [blame] | 5026 | &driver_attr_cdb_len.attr, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5027 | NULL, |
| 5028 | }; |
| 5029 | ATTRIBUTE_GROUPS(sdebug_drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | |
Akinobu Mita | 11ddcec | 2014-02-26 22:56:59 +0900 | [diff] [blame] | 5031 | static struct device *pseudo_primary; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | static int __init scsi_debug_init(void) |
| 5034 | { |
FUJITA Tomonori | 5f2578e | 2008-03-30 00:59:57 +0900 | [diff] [blame] | 5035 | unsigned long sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | int host_to_add; |
| 5037 | int k; |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5038 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5039 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5040 | atomic_set(&retired_max_queue, 0); |
| 5041 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5042 | if (sdebug_ndelay >= 1000 * 1000 * 1000) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5043 | pr_warn("ndelay must be less than 1 second, ignored\n"); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5044 | sdebug_ndelay = 0; |
| 5045 | } else if (sdebug_ndelay > 0) |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 5046 | sdebug_jdelay = JDELAY_OVERRIDDEN; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5047 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5048 | switch (sdebug_sector_size) { |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 5049 | case 512: |
| 5050 | case 1024: |
| 5051 | case 2048: |
| 5052 | case 4096: |
| 5053 | break; |
| 5054 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5055 | pr_err("invalid sector_size %d\n", sdebug_sector_size); |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 5056 | return -EINVAL; |
| 5057 | } |
| 5058 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5059 | switch (sdebug_dif) { |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5060 | case T10_PI_TYPE0_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5061 | break; |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5062 | case T10_PI_TYPE1_PROTECTION: |
| 5063 | case T10_PI_TYPE2_PROTECTION: |
| 5064 | case T10_PI_TYPE3_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5065 | have_dif_prot = true; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5066 | break; |
| 5067 | |
| 5068 | default: |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5069 | pr_err("dif must be 0, 1, 2 or 3\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5070 | return -EINVAL; |
| 5071 | } |
| 5072 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5073 | if (sdebug_guard > 1) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5074 | pr_err("guard must be 0 or 1\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5075 | return -EINVAL; |
| 5076 | } |
| 5077 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5078 | if (sdebug_ato > 1) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5079 | pr_err("ato must be 0 or 1\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5080 | return -EINVAL; |
| 5081 | } |
| 5082 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5083 | if (sdebug_physblk_exp > 15) { |
| 5084 | pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 5085 | return -EINVAL; |
| 5086 | } |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 5087 | if (sdebug_max_luns > 256) { |
| 5088 | pr_warn("max_luns can be no more than 256, use default\n"); |
| 5089 | sdebug_max_luns = DEF_MAX_LUNS; |
| 5090 | } |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 5091 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5092 | if (sdebug_lowest_aligned > 0x3fff) { |
| 5093 | pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 5094 | return -EINVAL; |
| 5095 | } |
| 5096 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5097 | if (submit_queues < 1) { |
| 5098 | pr_err("submit_queues must be 1 or more\n"); |
| 5099 | return -EINVAL; |
| 5100 | } |
| 5101 | sdebug_q_arr = kcalloc(submit_queues, sizeof(struct sdebug_queue), |
| 5102 | GFP_KERNEL); |
| 5103 | if (sdebug_q_arr == NULL) |
| 5104 | return -ENOMEM; |
| 5105 | for (k = 0; k < submit_queues; ++k) |
| 5106 | spin_lock_init(&sdebug_q_arr[k].qc_lock); |
| 5107 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5108 | if (sdebug_dev_size_mb < 1) |
| 5109 | sdebug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */ |
| 5110 | sz = (unsigned long)sdebug_dev_size_mb * 1048576; |
| 5111 | sdebug_store_sectors = sz / sdebug_sector_size; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 5112 | sdebug_capacity = get_sdebug_capacity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | |
| 5114 | /* play around with geometry, don't waste too much on track 0 */ |
| 5115 | sdebug_heads = 8; |
| 5116 | sdebug_sectors_per = 32; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5117 | if (sdebug_dev_size_mb >= 256) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5118 | sdebug_heads = 64; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5119 | else if (sdebug_dev_size_mb >= 16) |
Andy Shevchenko | fa785f0 | 2015-11-26 20:22:50 +0200 | [diff] [blame] | 5120 | sdebug_heads = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | sdebug_cylinders_per = (unsigned long)sdebug_capacity / |
| 5122 | (sdebug_sectors_per * sdebug_heads); |
| 5123 | if (sdebug_cylinders_per >= 1024) { |
| 5124 | /* other LLDs do this; implies >= 1GB ram disk ... */ |
| 5125 | sdebug_heads = 255; |
| 5126 | sdebug_sectors_per = 63; |
| 5127 | sdebug_cylinders_per = (unsigned long)sdebug_capacity / |
| 5128 | (sdebug_sectors_per * sdebug_heads); |
| 5129 | } |
| 5130 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 5131 | if (sdebug_fake_rw == 0) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5132 | fake_storep = vmalloc(sz); |
| 5133 | if (NULL == fake_storep) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5134 | pr_err("out of memory, 1\n"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5135 | ret = -ENOMEM; |
| 5136 | goto free_q_arr; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5137 | } |
| 5138 | memset(fake_storep, 0, sz); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5139 | if (sdebug_num_parts > 0) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5140 | sdebug_build_parts(fake_storep, sz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5143 | if (sdebug_dix) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5144 | int dif_size; |
| 5145 | |
Christoph Hellwig | 6ebf105 | 2016-09-11 19:35:39 +0200 | [diff] [blame] | 5146 | dif_size = sdebug_store_sectors * sizeof(struct t10_pi_tuple); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5147 | dif_storep = vmalloc(dif_size); |
| 5148 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5149 | 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] | 5150 | |
| 5151 | if (dif_storep == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5152 | pr_err("out of mem. (DIX)\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5153 | ret = -ENOMEM; |
| 5154 | goto free_vm; |
| 5155 | } |
| 5156 | |
| 5157 | memset(dif_storep, 0xff, dif_size); |
| 5158 | } |
| 5159 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 5160 | /* Logical Block Provisioning */ |
| 5161 | if (scsi_debug_lbp()) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5162 | sdebug_unmap_max_blocks = |
| 5163 | clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 5164 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5165 | sdebug_unmap_max_desc = |
| 5166 | clamp(sdebug_unmap_max_desc, 0U, 256U); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 5167 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5168 | sdebug_unmap_granularity = |
| 5169 | clamp(sdebug_unmap_granularity, 1U, 0xffffffffU); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 5170 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5171 | if (sdebug_unmap_alignment && |
| 5172 | sdebug_unmap_granularity <= |
| 5173 | sdebug_unmap_alignment) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5174 | pr_err("ERR: unmap_granularity <= unmap_alignment\n"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5175 | ret = -EINVAL; |
| 5176 | goto free_vm; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5177 | } |
| 5178 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 5179 | map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1; |
| 5180 | map_storep = vmalloc(BITS_TO_LONGS(map_size) * sizeof(long)); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5181 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5182 | pr_info("%lu provisioning blocks\n", map_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5183 | |
| 5184 | if (map_storep == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5185 | pr_err("out of mem. (MAP)\n"); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5186 | ret = -ENOMEM; |
| 5187 | goto free_vm; |
| 5188 | } |
| 5189 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 5190 | bitmap_zero(map_storep, map_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5191 | |
| 5192 | /* Map first 1KB for partition table */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5193 | if (sdebug_num_parts) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 5194 | map_region(0, 2); |
| 5195 | } |
| 5196 | |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5197 | pseudo_primary = root_device_register("pseudo_0"); |
| 5198 | if (IS_ERR(pseudo_primary)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5199 | pr_warn("root_device_register() error\n"); |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5200 | ret = PTR_ERR(pseudo_primary); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5201 | goto free_vm; |
| 5202 | } |
| 5203 | ret = bus_register(&pseudo_lld_bus); |
| 5204 | if (ret < 0) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5205 | pr_warn("bus_register error: %d\n", ret); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5206 | goto dev_unreg; |
| 5207 | } |
| 5208 | ret = driver_register(&sdebug_driverfs_driver); |
| 5209 | if (ret < 0) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5210 | pr_warn("driver_register error: %d\n", ret); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5211 | goto bus_unreg; |
| 5212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5214 | host_to_add = sdebug_add_host; |
| 5215 | sdebug_add_host = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5217 | for (k = 0; k < host_to_add; k++) { |
| 5218 | if (sdebug_add_adapter()) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5219 | pr_err("sdebug_add_adapter failed k=%d\n", k); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5220 | break; |
| 5221 | } |
| 5222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5223 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5224 | if (sdebug_verbose) |
| 5225 | pr_info("built %d host(s)\n", sdebug_add_host); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5227 | return 0; |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5228 | |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5229 | bus_unreg: |
| 5230 | bus_unregister(&pseudo_lld_bus); |
| 5231 | dev_unreg: |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5232 | root_device_unregister(pseudo_primary); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5233 | free_vm: |
Tomas Winkler | de232af | 2015-07-28 16:54:22 +0300 | [diff] [blame] | 5234 | vfree(map_storep); |
| 5235 | vfree(dif_storep); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5236 | vfree(fake_storep); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5237 | free_q_arr: |
| 5238 | kfree(sdebug_q_arr); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5239 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | } |
| 5241 | |
| 5242 | static void __exit scsi_debug_exit(void) |
| 5243 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5244 | int k = sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5245 | |
| 5246 | stop_all_queued(); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5247 | free_all_queued(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | for (; k; k--) |
| 5249 | sdebug_remove_adapter(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | driver_unregister(&sdebug_driverfs_driver); |
| 5251 | bus_unregister(&pseudo_lld_bus); |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5252 | root_device_unregister(pseudo_primary); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | |
Ewan D. Milne | 4d2b496 | 2016-10-26 11:22:53 -0400 | [diff] [blame] | 5254 | vfree(map_storep); |
Tomas Winkler | de232af | 2015-07-28 16:54:22 +0300 | [diff] [blame] | 5255 | vfree(dif_storep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5256 | vfree(fake_storep); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5257 | kfree(sdebug_q_arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | } |
| 5259 | |
| 5260 | device_initcall(scsi_debug_init); |
| 5261 | module_exit(scsi_debug_exit); |
| 5262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | static void sdebug_release_adapter(struct device * dev) |
| 5264 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5265 | struct sdebug_host_info *sdbg_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | |
| 5267 | sdbg_host = to_sdebug_host(dev); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5268 | kfree(sdbg_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | static int sdebug_add_adapter(void) |
| 5272 | { |
| 5273 | int k, devs_per_host; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5274 | int error = 0; |
| 5275 | struct sdebug_host_info *sdbg_host; |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5276 | struct sdebug_dev_info *sdbg_devinfo, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5278 | sdbg_host = kzalloc(sizeof(*sdbg_host), GFP_KERNEL); |
| 5279 | if (sdbg_host == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5280 | pr_err("out of memory at line %d\n", __LINE__); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5281 | return -ENOMEM; |
| 5282 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5284 | INIT_LIST_HEAD(&sdbg_host->dev_info_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5285 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5286 | devs_per_host = sdebug_num_tgts * sdebug_max_luns; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5287 | for (k = 0; k < devs_per_host; k++) { |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 5288 | sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL); |
| 5289 | if (!sdbg_devinfo) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5290 | pr_err("out of memory at line %d\n", __LINE__); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5291 | error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | goto clean; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5293 | } |
| 5294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5296 | spin_lock(&sdebug_host_list_lock); |
| 5297 | list_add_tail(&sdbg_host->host_list, &sdebug_host_list); |
| 5298 | spin_unlock(&sdebug_host_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5300 | sdbg_host->dev.bus = &pseudo_lld_bus; |
| 5301 | sdbg_host->dev.parent = pseudo_primary; |
| 5302 | sdbg_host->dev.release = &sdebug_release_adapter; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5303 | dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5305 | error = device_register(&sdbg_host->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5307 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | goto clean; |
| 5309 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5310 | ++sdebug_add_host; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5311 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | |
| 5313 | clean: |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5314 | list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, |
| 5315 | dev_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | list_del(&sdbg_devinfo->dev_list); |
| 5317 | kfree(sdbg_devinfo); |
| 5318 | } |
| 5319 | |
| 5320 | kfree(sdbg_host); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5321 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | } |
| 5323 | |
| 5324 | static void sdebug_remove_adapter(void) |
| 5325 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5326 | struct sdebug_host_info *sdbg_host = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5328 | spin_lock(&sdebug_host_list_lock); |
| 5329 | if (!list_empty(&sdebug_host_list)) { |
| 5330 | sdbg_host = list_entry(sdebug_host_list.prev, |
| 5331 | struct sdebug_host_info, host_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5332 | list_del(&sdbg_host->host_list); |
| 5333 | } |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5334 | spin_unlock(&sdebug_host_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | |
| 5336 | if (!sdbg_host) |
| 5337 | return; |
| 5338 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5339 | device_unregister(&sdbg_host->dev); |
| 5340 | --sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | } |
| 5342 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 5343 | static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5344 | { |
| 5345 | int num_in_q = 0; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5346 | struct sdebug_dev_info *devip; |
| 5347 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5348 | block_unblock_all_queues(true); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5349 | devip = (struct sdebug_dev_info *)sdev->hostdata; |
| 5350 | if (NULL == devip) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5351 | block_unblock_all_queues(false); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5352 | return -ENODEV; |
| 5353 | } |
| 5354 | num_in_q = atomic_read(&devip->num_in_q); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5355 | |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5356 | if (qdepth < 1) |
| 5357 | qdepth = 1; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5358 | /* allow to exceed max host qc_arr elements for testing */ |
| 5359 | if (qdepth > SDEBUG_CANQUEUE + 10) |
| 5360 | qdepth = SDEBUG_CANQUEUE + 10; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 5361 | scsi_change_queue_depth(sdev, qdepth); |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5362 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5363 | if (SDEBUG_OPT_Q_NOISE & sdebug_opts) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5364 | 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] | 5365 | __func__, qdepth, num_in_q); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5366 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5367 | block_unblock_all_queues(false); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5368 | return sdev->queue_depth; |
| 5369 | } |
| 5370 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5371 | static bool fake_timeout(struct scsi_cmnd *scp) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5372 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5373 | if (0 == (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5374 | if (sdebug_every_nth < -1) |
| 5375 | sdebug_every_nth = -1; |
| 5376 | if (SDEBUG_OPT_TIMEOUT & sdebug_opts) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5377 | return true; /* ignore command causing timeout */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5378 | else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts && |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5379 | scsi_medium_access_command(scp)) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5380 | return true; /* time out reads and writes */ |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5381 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5382 | return false; |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5383 | } |
| 5384 | |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 5385 | static bool fake_host_busy(struct scsi_cmnd *scp) |
| 5386 | { |
| 5387 | return (sdebug_opts & SDEBUG_OPT_HOST_BUSY) && |
| 5388 | (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) == 0; |
| 5389 | } |
| 5390 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 5391 | static int scsi_debug_queuecommand(struct Scsi_Host *shost, |
| 5392 | struct scsi_cmnd *scp) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5393 | { |
| 5394 | u8 sdeb_i; |
| 5395 | struct scsi_device *sdp = scp->device; |
| 5396 | const struct opcode_info_t *oip; |
| 5397 | const struct opcode_info_t *r_oip; |
| 5398 | struct sdebug_dev_info *devip; |
| 5399 | u8 *cmd = scp->cmnd; |
| 5400 | int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 5401 | int k, na; |
| 5402 | int errsts = 0; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5403 | u32 flags; |
| 5404 | u16 sa; |
| 5405 | u8 opcode = cmd[0]; |
| 5406 | bool has_wlun_rl; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5407 | |
| 5408 | scsi_set_resid(scp, 0); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5409 | if (sdebug_statistics) |
| 5410 | atomic_inc(&sdebug_cmnd_count); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5411 | if (unlikely(sdebug_verbose && |
| 5412 | !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5413 | char b[120]; |
| 5414 | int n, len, sb; |
| 5415 | |
| 5416 | len = scp->cmd_len; |
| 5417 | sb = (int)sizeof(b); |
| 5418 | if (len > 32) |
| 5419 | strcpy(b, "too long, over 32 bytes"); |
| 5420 | else { |
| 5421 | for (k = 0, n = 0; k < len && n < sb; ++k) |
| 5422 | n += scnprintf(b + n, sb - n, "%02x ", |
| 5423 | (u32)cmd[k]); |
| 5424 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5425 | if (sdebug_mq_active) |
| 5426 | sdev_printk(KERN_INFO, sdp, "%s: tag=%u, cmd %s\n", |
| 5427 | my_name, blk_mq_unique_tag(scp->request), |
| 5428 | b); |
| 5429 | else |
| 5430 | sdev_printk(KERN_INFO, sdp, "%s: cmd %s\n", my_name, |
| 5431 | b); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5432 | } |
Bart Van Assche | 7ee6d1b | 2017-12-07 14:56:18 -0800 | [diff] [blame] | 5433 | if (fake_host_busy(scp)) |
| 5434 | return SCSI_MLQUEUE_HOST_BUSY; |
Tomas Winkler | 34d5543 | 2015-07-28 16:54:21 +0300 | [diff] [blame] | 5435 | has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5436 | if (unlikely((sdp->lun >= sdebug_max_luns) && !has_wlun_rl)) |
| 5437 | goto err_out; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5438 | |
| 5439 | sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */ |
| 5440 | oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */ |
| 5441 | devip = (struct sdebug_dev_info *)sdp->hostdata; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5442 | if (unlikely(!devip)) { |
| 5443 | devip = find_build_dev_info(sdp); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5444 | if (NULL == devip) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5445 | goto err_out; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5446 | } |
| 5447 | na = oip->num_attached; |
| 5448 | r_pfp = oip->pfp; |
| 5449 | if (na) { /* multiple commands with this opcode */ |
| 5450 | r_oip = oip; |
| 5451 | if (FF_SA & r_oip->flags) { |
| 5452 | if (F_SA_LOW & oip->flags) |
| 5453 | sa = 0x1f & cmd[1]; |
| 5454 | else |
| 5455 | sa = get_unaligned_be16(cmd + 8); |
| 5456 | for (k = 0; k <= na; oip = r_oip->arrp + k++) { |
| 5457 | if (opcode == oip->opcode && sa == oip->sa) |
| 5458 | break; |
| 5459 | } |
| 5460 | } else { /* since no service action only check opcode */ |
| 5461 | for (k = 0; k <= na; oip = r_oip->arrp + k++) { |
| 5462 | if (opcode == oip->opcode) |
| 5463 | break; |
| 5464 | } |
| 5465 | } |
| 5466 | if (k > na) { |
| 5467 | if (F_SA_LOW & r_oip->flags) |
| 5468 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4); |
| 5469 | else if (F_SA_HIGH & r_oip->flags) |
| 5470 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7); |
| 5471 | else |
| 5472 | mk_sense_invalid_opcode(scp); |
| 5473 | goto check_cond; |
| 5474 | } |
| 5475 | } /* else (when na==0) we assume the oip is a match */ |
| 5476 | flags = oip->flags; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5477 | if (unlikely(F_INV_OP & flags)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5478 | mk_sense_invalid_opcode(scp); |
| 5479 | goto check_cond; |
| 5480 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5481 | if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5482 | if (sdebug_verbose) |
| 5483 | sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n", |
| 5484 | my_name, opcode, " supported for wlun"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5485 | mk_sense_invalid_opcode(scp); |
| 5486 | goto check_cond; |
| 5487 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5488 | if (unlikely(sdebug_strict)) { /* check cdb against mask */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5489 | u8 rem; |
| 5490 | int j; |
| 5491 | |
| 5492 | for (k = 1; k < oip->len_mask[0] && k < 16; ++k) { |
| 5493 | rem = ~oip->len_mask[k] & cmd[k]; |
| 5494 | if (rem) { |
| 5495 | for (j = 7; j >= 0; --j, rem <<= 1) { |
| 5496 | if (0x80 & rem) |
| 5497 | break; |
| 5498 | } |
| 5499 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j); |
| 5500 | goto check_cond; |
| 5501 | } |
| 5502 | } |
| 5503 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5504 | if (unlikely(!(F_SKIP_UA & flags) && |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 5505 | find_first_bit(devip->uas_bm, |
| 5506 | SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5507 | errsts = make_ua(scp, devip); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5508 | if (errsts) |
| 5509 | goto check_cond; |
| 5510 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5511 | if (unlikely((F_M_ACCESS & flags) && atomic_read(&devip->stopped))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5512 | mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5513 | if (sdebug_verbose) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5514 | sdev_printk(KERN_INFO, sdp, "%s reports: Not ready: " |
| 5515 | "%s\n", my_name, "initializing command " |
| 5516 | "required"); |
| 5517 | errsts = check_condition_result; |
| 5518 | goto fini; |
| 5519 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5520 | if (sdebug_fake_rw && (F_FAKE_RW & flags)) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5521 | goto fini; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5522 | if (unlikely(sdebug_every_nth)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5523 | if (fake_timeout(scp)) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5524 | return 0; /* ignore command: make trouble */ |
| 5525 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5526 | if (likely(oip->pfp)) |
| 5527 | errsts = oip->pfp(scp, devip); /* calls a resp_* function */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5528 | else if (r_pfp) /* if leaf function ptr NULL, try the root's */ |
| 5529 | errsts = r_pfp(scp, devip); |
| 5530 | |
| 5531 | fini: |
| 5532 | return schedule_resp(scp, devip, errsts, |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 5533 | ((F_DELAY_OVERR & flags) ? 0 : sdebug_jdelay)); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5534 | check_cond: |
| 5535 | return schedule_resp(scp, devip, check_condition_result, 0); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5536 | err_out: |
| 5537 | return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, 0); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5538 | } |
| 5539 | |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5540 | static struct scsi_host_template sdebug_driver_template = { |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 5541 | .show_info = scsi_debug_show_info, |
| 5542 | .write_info = scsi_debug_write_info, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5543 | .proc_name = sdebug_proc_name, |
| 5544 | .name = "SCSI DEBUG", |
| 5545 | .info = scsi_debug_info, |
| 5546 | .slave_alloc = scsi_debug_slave_alloc, |
| 5547 | .slave_configure = scsi_debug_slave_configure, |
| 5548 | .slave_destroy = scsi_debug_slave_destroy, |
| 5549 | .ioctl = scsi_debug_ioctl, |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 5550 | .queuecommand = scsi_debug_queuecommand, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5551 | .change_queue_depth = sdebug_change_qdepth, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5552 | .eh_abort_handler = scsi_debug_abort, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5553 | .eh_device_reset_handler = scsi_debug_device_reset, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5554 | .eh_target_reset_handler = scsi_debug_target_reset, |
| 5555 | .eh_bus_reset_handler = scsi_debug_bus_reset, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5556 | .eh_host_reset_handler = scsi_debug_host_reset, |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5557 | .can_queue = SDEBUG_CANQUEUE, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5558 | .this_id = 7, |
Ming Lin | 65e8617 | 2016-04-04 14:48:10 -0700 | [diff] [blame] | 5559 | .sg_tablesize = SG_MAX_SEGMENTS, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5560 | .cmd_per_lun = DEF_CMD_PER_LUN, |
Akinobu Mita | 6bb5e6e | 2014-06-02 22:56:49 +0900 | [diff] [blame] | 5561 | .max_sectors = -1U, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5562 | .use_clustering = DISABLE_CLUSTERING, |
| 5563 | .module = THIS_MODULE, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5564 | .track_queue_depth = 1, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5565 | }; |
| 5566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | static int sdebug_driver_probe(struct device * dev) |
| 5568 | { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 5569 | int error = 0; |
| 5570 | struct sdebug_host_info *sdbg_host; |
| 5571 | struct Scsi_Host *hpnt; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5572 | int hprot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5573 | |
| 5574 | sdbg_host = to_sdebug_host(dev); |
| 5575 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5576 | sdebug_driver_template.can_queue = sdebug_max_queue; |
| 5577 | if (sdebug_clustering) |
Akinobu Mita | 0759c66 | 2014-02-26 22:57:04 +0900 | [diff] [blame] | 5578 | sdebug_driver_template.use_clustering = ENABLE_CLUSTERING; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5579 | hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host)); |
| 5580 | if (NULL == hpnt) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5581 | pr_err("scsi_host_alloc failed\n"); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5582 | error = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | return error; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5584 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5585 | if (submit_queues > nr_cpu_ids) { |
Alexey Dobriyan | 9b130ad | 2017-09-08 16:14:18 -0700 | [diff] [blame] | 5586 | 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] | 5587 | my_name, submit_queues, nr_cpu_ids); |
| 5588 | submit_queues = nr_cpu_ids; |
| 5589 | } |
| 5590 | /* Decide whether to tell scsi subsystem that we want mq */ |
| 5591 | /* Following should give the same answer for each host */ |
| 5592 | sdebug_mq_active = shost_use_blk_mq(hpnt) && (submit_queues > 1); |
| 5593 | if (sdebug_mq_active) |
| 5594 | hpnt->nr_hw_queues = submit_queues; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5595 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5596 | sdbg_host->shost = hpnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5597 | *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5598 | if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id)) |
| 5599 | hpnt->max_id = sdebug_num_tgts + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5601 | hpnt->max_id = sdebug_num_tgts; |
| 5602 | /* = sdebug_max_luns; */ |
Tomas Winkler | f2d3fd2 | 2015-07-28 16:54:25 +0300 | [diff] [blame] | 5603 | hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5605 | hprot = 0; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5606 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5607 | switch (sdebug_dif) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5608 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5609 | case T10_PI_TYPE1_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5610 | hprot = SHOST_DIF_TYPE1_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5611 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5612 | hprot |= SHOST_DIX_TYPE1_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5613 | break; |
| 5614 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5615 | case T10_PI_TYPE2_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5616 | hprot = SHOST_DIF_TYPE2_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5617 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5618 | hprot |= SHOST_DIX_TYPE2_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5619 | break; |
| 5620 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 5621 | case T10_PI_TYPE3_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5622 | hprot = SHOST_DIF_TYPE3_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5623 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5624 | hprot |= SHOST_DIX_TYPE3_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5625 | break; |
| 5626 | |
| 5627 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5628 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5629 | hprot |= SHOST_DIX_TYPE0_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5630 | break; |
| 5631 | } |
| 5632 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5633 | scsi_host_set_prot(hpnt, hprot); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5634 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5635 | if (have_dif_prot || sdebug_dix) |
| 5636 | pr_info("host protection%s%s%s%s%s%s%s\n", |
| 5637 | (hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "", |
| 5638 | (hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "", |
| 5639 | (hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "", |
| 5640 | (hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "", |
| 5641 | (hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "", |
| 5642 | (hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "", |
| 5643 | (hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : ""); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5644 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5645 | if (sdebug_guard == 1) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5646 | scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP); |
| 5647 | else |
| 5648 | scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC); |
| 5649 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5650 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts); |
| 5651 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & sdebug_opts); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5652 | if (sdebug_every_nth) /* need stats counters for every_nth */ |
| 5653 | sdebug_statistics = true; |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5654 | error = scsi_add_host(hpnt, &sdbg_host->dev); |
| 5655 | if (error) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5656 | pr_err("scsi_add_host failed\n"); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5657 | error = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | scsi_host_put(hpnt); |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5659 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | scsi_scan_host(hpnt); |
| 5661 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5662 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5663 | } |
| 5664 | |
| 5665 | static int sdebug_driver_remove(struct device * dev) |
| 5666 | { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5667 | struct sdebug_host_info *sdbg_host; |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5668 | struct sdebug_dev_info *sdbg_devinfo, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | |
| 5670 | sdbg_host = to_sdebug_host(dev); |
| 5671 | |
| 5672 | if (!sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5673 | pr_err("Unable to locate host info\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5674 | return -ENODEV; |
| 5675 | } |
| 5676 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5677 | scsi_remove_host(sdbg_host->shost); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5679 | list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, |
| 5680 | dev_list) { |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5681 | list_del(&sdbg_devinfo->dev_list); |
| 5682 | kfree(sdbg_devinfo); |
| 5683 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5684 | |
Douglas Gilbert | 9a05101 | 2017-12-23 12:48:10 -0500 | [diff] [blame] | 5685 | scsi_host_put(sdbg_host->shost); |
| 5686 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | } |
| 5688 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5689 | static int pseudo_lld_bus_match(struct device *dev, |
| 5690 | struct device_driver *dev_driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | { |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5692 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5693 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5694 | |
| 5695 | static struct bus_type pseudo_lld_bus = { |
| 5696 | .name = "pseudo", |
| 5697 | .match = pseudo_lld_bus_match, |
| 5698 | .probe = sdebug_driver_probe, |
| 5699 | .remove = sdebug_driver_remove, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5700 | .drv_groups = sdebug_drv_groups, |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5701 | }; |