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 | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 9 | * Copyright (C) 2001 - 2016 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> |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 44 | |
| 45 | #include <net/checksum.h> |
FUJITA Tomonori | 9ff26ee | 2008-03-02 18:30:15 +0900 | [diff] [blame] | 46 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 47 | #include <asm/unaligned.h> |
| 48 | |
FUJITA Tomonori | 9ff26ee | 2008-03-02 18:30:15 +0900 | [diff] [blame] | 49 | #include <scsi/scsi.h> |
| 50 | #include <scsi/scsi_cmnd.h> |
| 51 | #include <scsi/scsi_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <scsi/scsi_host.h> |
| 53 | #include <scsi/scsicam.h> |
FUJITA Tomonori | a34c4e9 | 2008-03-25 09:26:50 +0900 | [diff] [blame] | 54 | #include <scsi/scsi_eh.h> |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 55 | #include <scsi/scsi_tcq.h> |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 56 | #include <scsi/scsi_dbg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 58 | #include "sd.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include "scsi_logging.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 61 | /* make sure inq_product_rev string corresponds to this version */ |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 62 | #define SDEBUG_VERSION "1.86" |
| 63 | static const char *sdebug_version_date = "20160430"; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 64 | |
| 65 | #define MY_NAME "scsi_debug" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 67 | /* Additional Sense Code (ASC) */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 68 | #define NO_ADDITIONAL_SENSE 0x0 |
| 69 | #define LOGICAL_UNIT_NOT_READY 0x4 |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 70 | #define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #define UNRECOVERED_READ_ERR 0x11 |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 72 | #define PARAMETER_LIST_LENGTH_ERR 0x1a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | #define INVALID_OPCODE 0x20 |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 74 | #define LBA_OUT_OF_RANGE 0x21 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #define INVALID_FIELD_IN_CDB 0x24 |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 76 | #define INVALID_FIELD_IN_PARAM_LIST 0x26 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 77 | #define UA_RESET_ASC 0x29 |
| 78 | #define UA_CHANGED_ASC 0x2a |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 79 | #define TARGET_CHANGED_ASC 0x3f |
| 80 | #define LUNS_CHANGED_ASCQ 0x0e |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 81 | #define INSUFF_RES_ASC 0x55 |
| 82 | #define INSUFF_RES_ASCQ 0x3 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 83 | #define POWER_ON_RESET_ASCQ 0x0 |
| 84 | #define BUS_RESET_ASCQ 0x2 /* scsi bus reset occurred */ |
| 85 | #define MODE_CHANGED_ASCQ 0x1 /* mode parameters changed */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 86 | #define CAPACITY_CHANGED_ASCQ 0x9 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | #define SAVING_PARAMS_UNSUP 0x39 |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 88 | #define TRANSPORT_PROBLEM 0x4b |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 89 | #define THRESHOLD_EXCEEDED 0x5d |
| 90 | #define LOW_POWER_COND_ON 0x5e |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 91 | #define MISCOMPARE_VERIFY_ASC 0x1d |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 92 | #define MICROCODE_CHANGED_ASCQ 0x1 /* with TARGET_CHANGED_ASC */ |
| 93 | #define MICROCODE_CHANGED_WO_RESET_ASCQ 0x16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 95 | /* Additional Sense Code Qualifier (ASCQ) */ |
| 96 | #define ACK_NAK_TO 0x3 |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /* Default values for driver parameters */ |
| 99 | #define DEF_NUM_HOST 1 |
| 100 | #define DEF_NUM_TGTS 1 |
| 101 | #define DEF_MAX_LUNS 1 |
| 102 | /* With these defaults, this driver will make 1 host with 1 target |
| 103 | * (id 0) containing 1 logical unit (lun 0). That is 1 device. |
| 104 | */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 105 | #define DEF_ATO 1 |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 106 | #define DEF_JDELAY 1 /* if > 0 unit is a jiffy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | #define DEF_DEV_SIZE_MB 8 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 108 | #define DEF_DIF 0 |
| 109 | #define DEF_DIX 0 |
| 110 | #define DEF_D_SENSE 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | #define DEF_EVERY_NTH 0 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 112 | #define DEF_FAKE_RW 0 |
| 113 | #define DEF_GUARD 0 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 114 | #define DEF_HOST_LOCK 0 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 115 | #define DEF_LBPU 0 |
| 116 | #define DEF_LBPWS 0 |
| 117 | #define DEF_LBPWS10 0 |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 118 | #define DEF_LBPRZ 1 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 119 | #define DEF_LOWEST_ALIGNED 0 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 120 | #define DEF_NDELAY 0 /* if > 0 unit is a nanosecond */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 121 | #define DEF_NO_LUN_0 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | #define DEF_NUM_PARTS 0 |
| 123 | #define DEF_OPTS 0 |
Martin K. Petersen | 32c5844 | 2015-12-16 17:53:51 -0500 | [diff] [blame] | 124 | #define DEF_OPT_BLKS 1024 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 125 | #define DEF_PHYSBLK_EXP 0 |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 126 | #define DEF_PTYPE TYPE_DISK |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 127 | #define DEF_REMOVABLE false |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 128 | #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] | 129 | #define DEF_SECTOR_SIZE 512 |
| 130 | #define DEF_UNMAP_ALIGNMENT 0 |
| 131 | #define DEF_UNMAP_GRANULARITY 1 |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 132 | #define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF |
| 133 | #define DEF_UNMAP_MAX_DESC 256 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 134 | #define DEF_VIRTUAL_GB 0 |
| 135 | #define DEF_VPD_USE_HOSTNO 1 |
| 136 | #define DEF_WRITESAME_LENGTH 0xFFFF |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 137 | #define DEF_STRICT 0 |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 138 | #define DEF_STATISTICS false |
| 139 | #define DEF_SUBMIT_QUEUES 1 |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 140 | #define JDELAY_OVERRIDDEN -9999 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 142 | #define SDEBUG_LUN_0_VAL 0 |
| 143 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 144 | /* bit mask values for sdebug_opts */ |
| 145 | #define SDEBUG_OPT_NOISE 1 |
| 146 | #define SDEBUG_OPT_MEDIUM_ERR 2 |
| 147 | #define SDEBUG_OPT_TIMEOUT 4 |
| 148 | #define SDEBUG_OPT_RECOVERED_ERR 8 |
| 149 | #define SDEBUG_OPT_TRANSPORT_ERR 16 |
| 150 | #define SDEBUG_OPT_DIF_ERR 32 |
| 151 | #define SDEBUG_OPT_DIX_ERR 64 |
| 152 | #define SDEBUG_OPT_MAC_TIMEOUT 128 |
| 153 | #define SDEBUG_OPT_SHORT_TRANSFER 0x100 |
| 154 | #define SDEBUG_OPT_Q_NOISE 0x200 |
| 155 | #define SDEBUG_OPT_ALL_TSF 0x400 |
| 156 | #define SDEBUG_OPT_RARE_TSF 0x800 |
| 157 | #define SDEBUG_OPT_N_WCE 0x1000 |
| 158 | #define SDEBUG_OPT_RESET_NOISE 0x2000 |
| 159 | #define SDEBUG_OPT_NO_CDB_NOISE 0x4000 |
| 160 | #define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \ |
| 161 | SDEBUG_OPT_RESET_NOISE) |
| 162 | #define SDEBUG_OPT_ALL_INJECTING (SDEBUG_OPT_RECOVERED_ERR | \ |
| 163 | SDEBUG_OPT_TRANSPORT_ERR | \ |
| 164 | SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR | \ |
| 165 | SDEBUG_OPT_SHORT_TRANSFER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* When "every_nth" > 0 then modulo "every_nth" commands: |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 167 | * - a missing response is simulated if SDEBUG_OPT_TIMEOUT is set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | * - a RECOVERED_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 169 | * commands if SDEBUG_OPT_RECOVERED_ERR is set. |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 170 | * - a TRANSPORT_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 171 | * commands if SDEBUG_OPT_TRANSPORT_ERR is set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | * |
| 173 | * When "every_nth" < 0 then after "- 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 _DEBUG_OPT_TRANSPORT_ERR is set. |
| 179 | * This will continue on every subsequent command until some other action |
| 180 | * occurs (e.g. the user * writing a new value (other than -1 or 1) to |
| 181 | * every_nth via sysfs). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | */ |
| 183 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 184 | /* 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] | 185 | * priority order. In the subset implemented here lower numbers have higher |
| 186 | * priority. The UA numbers should be a sequence starting from 0 with |
| 187 | * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */ |
| 188 | #define SDEBUG_UA_POR 0 /* Power on, reset, or bus device reset */ |
| 189 | #define SDEBUG_UA_BUS_RESET 1 |
| 190 | #define SDEBUG_UA_MODE_CHANGED 2 |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 191 | #define SDEBUG_UA_CAPACITY_CHANGED 3 |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 192 | #define SDEBUG_UA_LUNS_CHANGED 4 |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 193 | #define SDEBUG_UA_MICROCODE_CHANGED 5 /* simulate firmware change */ |
| 194 | #define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 6 |
| 195 | #define SDEBUG_NUM_UAS 7 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 196 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 197 | /* 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] | 198 | * sector on read commands: */ |
| 199 | #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */ |
Douglas Gilbert | 32f7ef7 | 2011-03-11 10:43:35 -0500 | [diff] [blame] | 200 | #define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1) |
| 203 | * or "peripheral device" addressing (value 0) */ |
| 204 | #define SAM2_LUN_ADDRESS_METHOD 0 |
| 205 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 206 | /* SDEBUG_CANQUEUE is the maximum number of commands that can be queued |
| 207 | * (for response) per submit queue at one time. Can be reduced by max_queue |
| 208 | * option. Command responses are not queued when jdelay=0 and ndelay=0. The |
| 209 | * per-device DEF_CMD_PER_LUN can be changed via sysfs: |
| 210 | * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth |
| 211 | * but cannot exceed SDEBUG_CANQUEUE . |
| 212 | */ |
| 213 | #define SDEBUG_CANQUEUE_WORDS 3 /* a WORD is bits in a long */ |
| 214 | #define SDEBUG_CANQUEUE (SDEBUG_CANQUEUE_WORDS * BITS_PER_LONG) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 215 | #define DEF_CMD_PER_LUN 255 |
| 216 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 217 | #define F_D_IN 1 |
| 218 | #define F_D_OUT 2 |
| 219 | #define F_D_OUT_MAYBE 4 /* WRITE SAME, NDOB bit */ |
| 220 | #define F_D_UNKN 8 |
| 221 | #define F_RL_WLUN_OK 0x10 |
| 222 | #define F_SKIP_UA 0x20 |
| 223 | #define F_DELAY_OVERR 0x40 |
| 224 | #define F_SA_LOW 0x80 /* cdb byte 1, bits 4 to 0 */ |
| 225 | #define F_SA_HIGH 0x100 /* as used by variable length cdbs */ |
| 226 | #define F_INV_OP 0x200 |
| 227 | #define F_FAKE_RW 0x400 |
| 228 | #define F_M_ACCESS 0x800 /* media access */ |
| 229 | |
| 230 | #define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR) |
| 231 | #define FF_DIRECT_IO (F_M_ACCESS | F_FAKE_RW) |
| 232 | #define FF_SA (F_SA_HIGH | F_SA_LOW) |
| 233 | |
| 234 | #define SDEBUG_MAX_PARTS 4 |
| 235 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 236 | #define SDEBUG_MAX_CMD_LEN 32 |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 237 | |
| 238 | |
| 239 | struct sdebug_dev_info { |
| 240 | struct list_head dev_list; |
| 241 | unsigned int channel; |
| 242 | unsigned int target; |
| 243 | u64 lun; |
| 244 | struct sdebug_host_info *sdbg_host; |
| 245 | unsigned long uas_bm[1]; |
| 246 | atomic_t num_in_q; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 247 | atomic_t stopped; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 248 | bool used; |
| 249 | }; |
| 250 | |
| 251 | struct sdebug_host_info { |
| 252 | struct list_head host_list; |
| 253 | struct Scsi_Host *shost; |
| 254 | struct device dev; |
| 255 | struct list_head dev_info_list; |
| 256 | }; |
| 257 | |
| 258 | #define to_sdebug_host(d) \ |
| 259 | container_of(d, struct sdebug_host_info, dev) |
| 260 | |
| 261 | struct sdebug_defer { |
| 262 | struct hrtimer hrt; |
| 263 | struct execute_work ew; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 264 | int sqa_idx; /* index of sdebug_queue array */ |
| 265 | int qc_idx; /* index of sdebug_queued_cmd array within sqa_idx */ |
| 266 | int issuing_cpu; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | struct sdebug_queued_cmd { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 270 | /* corresponding bit set in in_use_bm[] in owning struct sdebug_queue |
| 271 | * instance indicates this slot is in use. |
| 272 | */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 273 | struct sdebug_defer *sd_dp; |
| 274 | struct scsi_cmnd *a_cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 275 | unsigned int inj_recovered:1; |
| 276 | unsigned int inj_transport:1; |
| 277 | unsigned int inj_dif:1; |
| 278 | unsigned int inj_dix:1; |
| 279 | unsigned int inj_short:1; |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 280 | }; |
| 281 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 282 | struct sdebug_queue { |
| 283 | struct sdebug_queued_cmd qc_arr[SDEBUG_CANQUEUE]; |
| 284 | unsigned long in_use_bm[SDEBUG_CANQUEUE_WORDS]; |
| 285 | spinlock_t qc_lock; |
| 286 | atomic_t blocked; /* to temporarily stop more being queued */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 287 | }; |
| 288 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 289 | static atomic_t sdebug_cmnd_count; /* number of incoming commands */ |
| 290 | static atomic_t sdebug_completions; /* count of deferred completions */ |
| 291 | static atomic_t sdebug_miss_cpus; /* submission + completion cpus differ */ |
| 292 | static atomic_t sdebug_a_tsf; /* 'almost task set full' counter */ |
| 293 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 294 | struct opcode_info_t { |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 295 | u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff */ |
| 296 | /* for terminating element */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 297 | u8 opcode; /* if num_attached > 0, preferred */ |
| 298 | u16 sa; /* service action */ |
| 299 | u32 flags; /* OR-ed set of SDEB_F_* */ |
| 300 | int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 301 | const struct opcode_info_t *arrp; /* num_attached elements or NULL */ |
| 302 | u8 len_mask[16]; /* len=len_mask[0], then mask for cdb[1]... */ |
| 303 | /* ignore cdb bytes after position 15 */ |
| 304 | }; |
| 305 | |
| 306 | /* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 307 | enum sdeb_opcode_index { |
| 308 | SDEB_I_INVALID_OPCODE = 0, |
| 309 | SDEB_I_INQUIRY = 1, |
| 310 | SDEB_I_REPORT_LUNS = 2, |
| 311 | SDEB_I_REQUEST_SENSE = 3, |
| 312 | SDEB_I_TEST_UNIT_READY = 4, |
| 313 | SDEB_I_MODE_SENSE = 5, /* 6, 10 */ |
| 314 | SDEB_I_MODE_SELECT = 6, /* 6, 10 */ |
| 315 | SDEB_I_LOG_SENSE = 7, |
| 316 | SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */ |
| 317 | SDEB_I_READ = 9, /* 6, 10, 12, 16 */ |
| 318 | SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */ |
| 319 | SDEB_I_START_STOP = 11, |
| 320 | SDEB_I_SERV_ACT_IN = 12, /* 12, 16 */ |
| 321 | SDEB_I_SERV_ACT_OUT = 13, /* 12, 16 */ |
| 322 | SDEB_I_MAINT_IN = 14, |
| 323 | SDEB_I_MAINT_OUT = 15, |
| 324 | SDEB_I_VERIFY = 16, /* 10 only */ |
| 325 | SDEB_I_VARIABLE_LEN = 17, |
| 326 | SDEB_I_RESERVE = 18, /* 6, 10 */ |
| 327 | SDEB_I_RELEASE = 19, /* 6, 10 */ |
| 328 | SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */ |
| 329 | SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */ |
| 330 | SDEB_I_ATA_PT = 22, /* 12, 16 */ |
| 331 | SDEB_I_SEND_DIAG = 23, |
| 332 | SDEB_I_UNMAP = 24, |
| 333 | SDEB_I_XDWRITEREAD = 25, /* 10 only */ |
| 334 | SDEB_I_WRITE_BUFFER = 26, |
| 335 | SDEB_I_WRITE_SAME = 27, /* 10, 16 */ |
| 336 | SDEB_I_SYNC_CACHE = 28, /* 10 only */ |
| 337 | SDEB_I_COMP_WRITE = 29, |
| 338 | SDEB_I_LAST_ELEMENT = 30, /* keep this last */ |
| 339 | }; |
| 340 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 341 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 342 | static const unsigned char opcode_ind_arr[256] = { |
| 343 | /* 0x0; 0x0->0x1f: 6 byte cdbs */ |
| 344 | SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE, |
| 345 | 0, 0, 0, 0, |
| 346 | SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0, |
| 347 | 0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE, |
| 348 | SDEB_I_RELEASE, |
| 349 | 0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG, |
| 350 | SDEB_I_ALLOW_REMOVAL, 0, |
| 351 | /* 0x20; 0x20->0x3f: 10 byte cdbs */ |
| 352 | 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0, |
| 353 | SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY, |
| 354 | 0, 0, 0, 0, 0, SDEB_I_SYNC_CACHE, 0, 0, |
| 355 | 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0, |
| 356 | /* 0x40; 0x40->0x5f: 10 byte cdbs */ |
| 357 | 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0, |
| 358 | 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0, |
| 359 | 0, 0, 0, SDEB_I_XDWRITEREAD, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE, |
| 360 | SDEB_I_RELEASE, |
| 361 | 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0, |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 362 | /* 0x60; 0x60->0x7d are reserved, 0x7e is "extended cdb" */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 363 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 364 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 365 | 0, SDEB_I_VARIABLE_LEN, |
| 366 | /* 0x80; 0x80->0x9f: 16 byte cdbs */ |
| 367 | 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0, |
| 368 | SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0, 0, 0, 0, 0, |
| 369 | 0, 0, 0, SDEB_I_WRITE_SAME, 0, 0, 0, 0, |
| 370 | 0, 0, 0, 0, 0, 0, SDEB_I_SERV_ACT_IN, SDEB_I_SERV_ACT_OUT, |
| 371 | /* 0xa0; 0xa0->0xbf: 12 byte cdbs */ |
| 372 | SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN, |
| 373 | SDEB_I_MAINT_OUT, 0, 0, 0, |
| 374 | SDEB_I_READ, SDEB_I_SERV_ACT_OUT, SDEB_I_WRITE, SDEB_I_SERV_ACT_IN, |
| 375 | 0, 0, 0, 0, |
| 376 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 377 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 378 | /* 0xc0; 0xc0->0xff: vendor specific */ |
| 379 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 380 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 381 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 382 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 383 | }; |
| 384 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 385 | static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 386 | static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 387 | static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 388 | static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 389 | static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 390 | static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 391 | static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 392 | static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 393 | static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 394 | static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 395 | static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 396 | static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 397 | static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 398 | static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 399 | static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 400 | static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 401 | static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 402 | static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 403 | static int resp_xdwriteread_10(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 404 | 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] | 405 | static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 406 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 407 | static const struct opcode_info_t msense_iarr[1] = { |
| 408 | {0, 0x1a, 0, F_D_IN, NULL, NULL, |
| 409 | {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 410 | }; |
| 411 | |
| 412 | static const struct opcode_info_t mselect_iarr[1] = { |
| 413 | {0, 0x15, 0, F_D_OUT, NULL, NULL, |
| 414 | {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 415 | }; |
| 416 | |
| 417 | static const struct opcode_info_t read_iarr[3] = { |
| 418 | {0, 0x28, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL,/* READ(10) */ |
| 419 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0, |
| 420 | 0, 0, 0, 0} }, |
| 421 | {0, 0x8, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL, /* READ(6) */ |
| 422 | {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 423 | {0, 0xa8, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL,/* READ(12) */ |
| 424 | {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f, |
| 425 | 0xc7, 0, 0, 0, 0} }, |
| 426 | }; |
| 427 | |
| 428 | static const struct opcode_info_t write_iarr[3] = { |
| 429 | {0, 0x2a, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 10 */ |
| 430 | {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0, |
| 431 | 0, 0, 0, 0} }, |
| 432 | {0, 0xa, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 6 */ |
| 433 | {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 434 | {0, 0xaa, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 12 */ |
| 435 | {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f, |
| 436 | 0xc7, 0, 0, 0, 0} }, |
| 437 | }; |
| 438 | |
| 439 | static const struct opcode_info_t sa_in_iarr[1] = { |
| 440 | {0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL, |
| 441 | {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 442 | 0xff, 0xff, 0xff, 0, 0xc7} }, |
| 443 | }; |
| 444 | |
| 445 | static const struct opcode_info_t vl_iarr[1] = { /* VARIABLE LENGTH */ |
| 446 | {0, 0x7f, 0xb, F_SA_HIGH | F_D_OUT | FF_DIRECT_IO, resp_write_dt0, |
| 447 | NULL, {32, 0xc7, 0, 0, 0, 0, 0x1f, 0x18, 0x0, 0xb, 0xfa, |
| 448 | 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */ |
| 449 | }; |
| 450 | |
| 451 | static const struct opcode_info_t maint_in_iarr[2] = { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 452 | {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] | 453 | {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, |
| 454 | 0xc7, 0, 0, 0, 0} }, |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 455 | {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] | 456 | {12, 0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, |
| 457 | 0, 0} }, |
| 458 | }; |
| 459 | |
| 460 | static const struct opcode_info_t write_same_iarr[1] = { |
| 461 | {0, 0x93, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, resp_write_same_16, NULL, |
| 462 | {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 463 | 0xff, 0xff, 0xff, 0x1f, 0xc7} }, |
| 464 | }; |
| 465 | |
| 466 | static const struct opcode_info_t reserve_iarr[1] = { |
| 467 | {0, 0x16, 0, F_D_OUT, NULL, NULL, /* RESERVE(6) */ |
| 468 | {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 469 | }; |
| 470 | |
| 471 | static const struct opcode_info_t release_iarr[1] = { |
| 472 | {0, 0x17, 0, F_D_OUT, NULL, NULL, /* RELEASE(6) */ |
| 473 | {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 474 | }; |
| 475 | |
| 476 | |
| 477 | /* This array is accessed via SDEB_I_* values. Make sure all are mapped, |
| 478 | * plus the terminating elements for logic that scans this table such as |
| 479 | * REPORT SUPPORTED OPERATION CODES. */ |
| 480 | static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEMENT + 1] = { |
| 481 | /* 0 */ |
| 482 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, |
| 483 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 484 | {0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL, |
| 485 | {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 486 | {0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL, |
| 487 | {12, 0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, |
| 488 | 0, 0} }, |
| 489 | {0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL, |
| 490 | {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 491 | {0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */ |
| 492 | {6, 0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 493 | {1, 0x5a, 0, F_D_IN, resp_mode_sense, msense_iarr, |
| 494 | {10, 0xf8, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 495 | 0} }, |
| 496 | {1, 0x55, 0, F_D_OUT, resp_mode_select, mselect_iarr, |
| 497 | {10, 0xf1, 0, 0, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 498 | {0, 0x4d, 0, F_D_IN, resp_log_sense, NULL, |
| 499 | {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, |
| 500 | 0, 0, 0} }, |
| 501 | {0, 0x25, 0, F_D_IN, resp_readcap, NULL, |
| 502 | {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0, |
| 503 | 0, 0} }, |
| 504 | {3, 0x88, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, read_iarr, |
| 505 | {16, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 506 | 0xff, 0xff, 0xff, 0x9f, 0xc7} }, /* READ(16) */ |
| 507 | /* 10 */ |
| 508 | {3, 0x8a, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, write_iarr, |
| 509 | {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 510 | 0xff, 0xff, 0xff, 0x9f, 0xc7} }, /* WRITE(16) */ |
| 511 | {0, 0x1b, 0, 0, resp_start_stop, NULL, /* START STOP UNIT */ |
| 512 | {6, 0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 513 | {1, 0x9e, 0x10, F_SA_LOW | F_D_IN, resp_readcap16, sa_in_iarr, |
| 514 | {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 515 | 0xff, 0xff, 0xff, 0x1, 0xc7} }, /* READ CAPACITY(16) */ |
| 516 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* SA OUT */ |
| 517 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 518 | {2, 0xa3, 0xa, F_SA_LOW | F_D_IN, resp_report_tgtpgs, maint_in_iarr, |
| 519 | {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, 0, |
| 520 | 0} }, |
| 521 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */ |
| 522 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | f7f9f26 | 2015-11-22 12:11:28 -0500 | [diff] [blame] | 523 | {0, 0x2f, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, NULL, NULL, /* VERIFY(10) */ |
| 524 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, |
| 525 | 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 526 | {1, 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_DIRECT_IO, resp_read_dt0, |
| 527 | vl_iarr, {32, 0xc7, 0, 0, 0, 0, 0x1f, 0x18, 0x0, 0x9, 0xfe, 0, |
| 528 | 0xff, 0xff, 0xff, 0xff} },/* VARIABLE LENGTH, READ(32) */ |
| 529 | {1, 0x56, 0, F_D_OUT, NULL, reserve_iarr, /* RESERVE(10) */ |
| 530 | {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 531 | 0} }, |
| 532 | {1, 0x57, 0, F_D_OUT, NULL, release_iarr, /* RELEASE(10) */ |
| 533 | {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 534 | 0} }, |
| 535 | /* 20 */ |
Douglas Gilbert | f7f9f26 | 2015-11-22 12:11:28 -0500 | [diff] [blame] | 536 | {0, 0x1e, 0, 0, NULL, NULL, /* ALLOW REMOVAL */ |
| 537 | {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] | 538 | {0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */ |
| 539 | {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 540 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */ |
| 541 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 542 | {0, 0x1d, F_D_OUT, 0, NULL, NULL, /* SEND DIAGNOSTIC */ |
| 543 | {6, 0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 544 | {0, 0x42, 0, F_D_OUT | FF_DIRECT_IO, resp_unmap, NULL, /* UNMAP */ |
| 545 | {10, 0x1, 0, 0, 0, 0, 0x1f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 546 | {0, 0x53, 0, F_D_IN | F_D_OUT | FF_DIRECT_IO, resp_xdwriteread_10, |
| 547 | NULL, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, |
| 548 | 0, 0, 0, 0, 0, 0} }, |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 549 | {0, 0x3b, 0, F_D_OUT_MAYBE, resp_write_buffer, NULL, |
| 550 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, |
| 551 | 0, 0, 0, 0} }, /* WRITE_BUFFER */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 552 | {1, 0x41, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, resp_write_same_10, |
| 553 | write_same_iarr, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, |
| 554 | 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 555 | {0, 0x35, 0, F_DELAY_OVERR | FF_DIRECT_IO, NULL, NULL, /* SYNC_CACHE */ |
| 556 | {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0, |
| 557 | 0, 0, 0, 0} }, |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 558 | {0, 0x89, 0, F_D_OUT | FF_DIRECT_IO, resp_comp_write, NULL, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 559 | {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, |
| 560 | 0, 0xff, 0x1f, 0xc7} }, /* COMPARE AND WRITE */ |
| 561 | |
| 562 | /* 30 */ |
| 563 | {0xff, 0, 0, 0, NULL, NULL, /* terminating element */ |
| 564 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 565 | }; |
| 566 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 567 | static int sdebug_add_host = DEF_NUM_HOST; |
| 568 | static int sdebug_ato = DEF_ATO; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 569 | static int sdebug_jdelay = DEF_JDELAY; /* if > 0 then unit is jiffies */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 570 | static int sdebug_dev_size_mb = DEF_DEV_SIZE_MB; |
| 571 | static int sdebug_dif = DEF_DIF; |
| 572 | static int sdebug_dix = DEF_DIX; |
| 573 | static int sdebug_dsense = DEF_D_SENSE; |
| 574 | static int sdebug_every_nth = DEF_EVERY_NTH; |
| 575 | static int sdebug_fake_rw = DEF_FAKE_RW; |
| 576 | static unsigned int sdebug_guard = DEF_GUARD; |
| 577 | static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED; |
| 578 | static int sdebug_max_luns = DEF_MAX_LUNS; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 579 | static int sdebug_max_queue = SDEBUG_CANQUEUE; /* per submit queue */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 580 | 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] | 581 | static int sdebug_ndelay = DEF_NDELAY; /* if > 0 then unit is nanoseconds */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 582 | static int sdebug_no_lun_0 = DEF_NO_LUN_0; |
| 583 | static int sdebug_no_uld; |
| 584 | static int sdebug_num_parts = DEF_NUM_PARTS; |
| 585 | static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */ |
| 586 | static int sdebug_opt_blks = DEF_OPT_BLKS; |
| 587 | static int sdebug_opts = DEF_OPTS; |
| 588 | static int sdebug_physblk_exp = DEF_PHYSBLK_EXP; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 589 | static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral device type */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 590 | static int sdebug_scsi_level = DEF_SCSI_LEVEL; |
| 591 | static int sdebug_sector_size = DEF_SECTOR_SIZE; |
| 592 | static int sdebug_virtual_gb = DEF_VIRTUAL_GB; |
| 593 | static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO; |
| 594 | static unsigned int sdebug_lbpu = DEF_LBPU; |
| 595 | static unsigned int sdebug_lbpws = DEF_LBPWS; |
| 596 | static unsigned int sdebug_lbpws10 = DEF_LBPWS10; |
| 597 | static unsigned int sdebug_lbprz = DEF_LBPRZ; |
| 598 | static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT; |
| 599 | static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY; |
| 600 | static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS; |
| 601 | static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC; |
| 602 | static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH; |
| 603 | static bool sdebug_removable = DEF_REMOVABLE; |
| 604 | static bool sdebug_clustering; |
| 605 | static bool sdebug_host_lock = DEF_HOST_LOCK; |
| 606 | static bool sdebug_strict = DEF_STRICT; |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 607 | static bool sdebug_any_injecting_opt; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 608 | static bool sdebug_verbose; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 609 | static bool have_dif_prot; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 610 | static bool sdebug_statistics = DEF_STATISTICS; |
| 611 | static bool sdebug_mq_active; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 613 | static unsigned int sdebug_store_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | static sector_t sdebug_capacity; /* in sectors */ |
| 615 | |
| 616 | /* old BIOS stuff, kernel may get rid of them but some mode sense pages |
| 617 | may still need them */ |
| 618 | static int sdebug_heads; /* heads per disk */ |
| 619 | static int sdebug_cylinders_per; /* cylinders per surface */ |
| 620 | static int sdebug_sectors_per; /* sectors per cylinder */ |
| 621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | static LIST_HEAD(sdebug_host_list); |
| 623 | static DEFINE_SPINLOCK(sdebug_host_list_lock); |
| 624 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 625 | static unsigned char *fake_storep; /* ramdisk storage */ |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 626 | static struct sd_dif_tuple *dif_storep; /* protection info */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 627 | static void *map_storep; /* provisioning map */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 629 | static unsigned long map_size; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 630 | static int num_aborts; |
| 631 | static int num_dev_resets; |
| 632 | static int num_target_resets; |
| 633 | static int num_bus_resets; |
| 634 | static int num_host_resets; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 635 | static int dix_writes; |
| 636 | static int dix_reads; |
| 637 | static int dif_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 639 | static int submit_queues = DEF_SUBMIT_QUEUES; /* > 1 for multi-queue (mq) */ |
| 640 | 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] | 641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | static DEFINE_RWLOCK(atomic_rw); |
| 643 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 644 | static char sdebug_proc_name[] = MY_NAME; |
| 645 | static const char *my_name = MY_NAME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | static struct bus_type pseudo_lld_bus; |
| 648 | |
| 649 | static struct device_driver sdebug_driverfs_driver = { |
| 650 | .name = sdebug_proc_name, |
| 651 | .bus = &pseudo_lld_bus, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | }; |
| 653 | |
| 654 | static const int check_condition_result = |
| 655 | (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
| 656 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 657 | static const int illegal_condition_result = |
| 658 | (DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION; |
| 659 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 660 | static const int device_qfull_result = |
| 661 | (DID_OK << 16) | (COMMAND_COMPLETE << 8) | SAM_STAT_TASK_SET_FULL; |
| 662 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 663 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 664 | /* Only do the extra work involved in logical block provisioning if one or |
| 665 | * more of the lbpu, lbpws or lbpws10 parameters are given and we are doing |
| 666 | * real reads and writes (i.e. not skipping them for speed). |
| 667 | */ |
| 668 | static inline bool scsi_debug_lbp(void) |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 669 | { |
| 670 | return 0 == sdebug_fake_rw && |
| 671 | (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10); |
| 672 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 673 | |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 674 | static void *fake_store(unsigned long long lba) |
| 675 | { |
| 676 | lba = do_div(lba, sdebug_store_sectors); |
| 677 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 678 | return fake_storep + lba * sdebug_sector_size; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | static struct sd_dif_tuple *dif_store(sector_t sector) |
| 682 | { |
Arnd Bergmann | 4941311 | 2015-11-20 17:38:28 +0100 | [diff] [blame] | 683 | sector = sector_div(sector, sdebug_store_sectors); |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 684 | |
| 685 | return dif_storep + sector; |
| 686 | } |
| 687 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 688 | static void sdebug_max_tgts_luns(void) |
| 689 | { |
| 690 | struct sdebug_host_info *sdbg_host; |
| 691 | struct Scsi_Host *hpnt; |
| 692 | |
| 693 | spin_lock(&sdebug_host_list_lock); |
| 694 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
| 695 | hpnt = sdbg_host->shost; |
| 696 | if ((hpnt->this_id >= 0) && |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 697 | (sdebug_num_tgts > hpnt->this_id)) |
| 698 | hpnt->max_id = sdebug_num_tgts + 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 699 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 700 | hpnt->max_id = sdebug_num_tgts; |
| 701 | /* sdebug_max_luns; */ |
Tomas Winkler | f2d3fd2 | 2015-07-28 16:54:25 +0300 | [diff] [blame] | 702 | hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 703 | } |
| 704 | spin_unlock(&sdebug_host_list_lock); |
| 705 | } |
| 706 | |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 707 | enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1}; |
| 708 | |
| 709 | /* 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] | 710 | static void mk_sense_invalid_fld(struct scsi_cmnd *scp, |
| 711 | enum sdeb_cmd_data c_d, |
| 712 | int in_byte, int in_bit) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 713 | { |
| 714 | unsigned char *sbuff; |
| 715 | u8 sks[4]; |
| 716 | int sl, asc; |
| 717 | |
| 718 | sbuff = scp->sense_buffer; |
| 719 | if (!sbuff) { |
| 720 | sdev_printk(KERN_ERR, scp->device, |
| 721 | "%s: sense_buffer is NULL\n", __func__); |
| 722 | return; |
| 723 | } |
| 724 | asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST; |
| 725 | memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 726 | scsi_build_sense_buffer(sdebug_dsense, sbuff, ILLEGAL_REQUEST, asc, 0); |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 727 | memset(sks, 0, sizeof(sks)); |
| 728 | sks[0] = 0x80; |
| 729 | if (c_d) |
| 730 | sks[0] |= 0x40; |
| 731 | if (in_bit >= 0) { |
| 732 | sks[0] |= 0x8; |
| 733 | sks[0] |= 0x7 & in_bit; |
| 734 | } |
| 735 | put_unaligned_be16(in_byte, sks + 1); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 736 | if (sdebug_dsense) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 737 | sl = sbuff[7] + 8; |
| 738 | sbuff[7] = sl; |
| 739 | sbuff[sl] = 0x2; |
| 740 | sbuff[sl + 1] = 0x6; |
| 741 | memcpy(sbuff + sl + 4, sks, 3); |
| 742 | } else |
| 743 | memcpy(sbuff + 15, sks, 3); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 744 | if (sdebug_verbose) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 745 | sdev_printk(KERN_INFO, scp->device, "%s: [sense_key,asc,ascq" |
| 746 | "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n", |
| 747 | my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit); |
| 748 | } |
| 749 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 750 | 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] | 751 | { |
| 752 | unsigned char *sbuff; |
| 753 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 754 | sbuff = scp->sense_buffer; |
| 755 | if (!sbuff) { |
| 756 | sdev_printk(KERN_ERR, scp->device, |
| 757 | "%s: sense_buffer is NULL\n", __func__); |
| 758 | return; |
| 759 | } |
| 760 | memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 761 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 762 | scsi_build_sense_buffer(sdebug_dsense, sbuff, key, asc, asq); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 763 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 764 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 765 | sdev_printk(KERN_INFO, scp->device, |
| 766 | "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n", |
| 767 | my_name, key, asc, asq); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 770 | static void mk_sense_invalid_opcode(struct scsi_cmnd *scp) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 771 | { |
| 772 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0); |
| 773 | } |
| 774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg) |
| 776 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 777 | if (sdebug_verbose) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 778 | if (0x1261 == cmd) |
| 779 | sdev_printk(KERN_INFO, dev, |
| 780 | "%s: BLKFLSBUF [0x1261]\n", __func__); |
| 781 | else if (0x5331 == cmd) |
| 782 | sdev_printk(KERN_INFO, dev, |
| 783 | "%s: CDROM_GET_CAPABILITY [0x5331]\n", |
| 784 | __func__); |
| 785 | else |
| 786 | sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n", |
| 787 | __func__, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | } |
| 789 | return -EINVAL; |
| 790 | /* return -ENOTTY; // correct return but upsets fdisk */ |
| 791 | } |
| 792 | |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 793 | static void clear_luns_changed_on_target(struct sdebug_dev_info *devip) |
| 794 | { |
| 795 | struct sdebug_host_info *sdhp; |
| 796 | struct sdebug_dev_info *dp; |
| 797 | |
| 798 | spin_lock(&sdebug_host_list_lock); |
| 799 | list_for_each_entry(sdhp, &sdebug_host_list, host_list) { |
| 800 | list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) { |
| 801 | if ((devip->sdbg_host == dp->sdbg_host) && |
| 802 | (devip->target == dp->target)) |
| 803 | clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm); |
| 804 | } |
| 805 | } |
| 806 | spin_unlock(&sdebug_host_list_lock); |
| 807 | } |
| 808 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 809 | 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] | 810 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 811 | int k; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 812 | |
| 813 | k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS); |
| 814 | if (k != SDEBUG_NUM_UAS) { |
| 815 | const char *cp = NULL; |
| 816 | |
| 817 | switch (k) { |
| 818 | case SDEBUG_UA_POR: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 819 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC, |
| 820 | POWER_ON_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 821 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 822 | cp = "power on reset"; |
| 823 | break; |
| 824 | case SDEBUG_UA_BUS_RESET: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 825 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC, |
| 826 | BUS_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 827 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 828 | cp = "bus reset"; |
| 829 | break; |
| 830 | case SDEBUG_UA_MODE_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 831 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC, |
| 832 | MODE_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 833 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 834 | cp = "mode parameters changed"; |
| 835 | break; |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 836 | case SDEBUG_UA_CAPACITY_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 837 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC, |
| 838 | CAPACITY_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 839 | if (sdebug_verbose) |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 840 | cp = "capacity data changed"; |
Ewan D. Milne | f49accf | 2014-12-04 11:49:25 -0500 | [diff] [blame] | 841 | break; |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 842 | case SDEBUG_UA_MICROCODE_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 843 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 844 | TARGET_CHANGED_ASC, |
| 845 | MICROCODE_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 846 | if (sdebug_verbose) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 847 | cp = "microcode has been changed"; |
| 848 | break; |
| 849 | case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 850 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 851 | TARGET_CHANGED_ASC, |
| 852 | MICROCODE_CHANGED_WO_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 853 | if (sdebug_verbose) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 854 | cp = "microcode has been changed without reset"; |
| 855 | break; |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 856 | case SDEBUG_UA_LUNS_CHANGED: |
| 857 | /* |
| 858 | * SPC-3 behavior is to report a UNIT ATTENTION with |
| 859 | * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN |
| 860 | * on the target, until a REPORT LUNS command is |
| 861 | * received. SPC-4 behavior is to report it only once. |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 862 | * NOTE: sdebug_scsi_level does not use the same |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 863 | * values as struct scsi_device->scsi_level. |
| 864 | */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 865 | if (sdebug_scsi_level >= 6) /* SPC-4 and above */ |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 866 | clear_luns_changed_on_target(devip); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 867 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 868 | TARGET_CHANGED_ASC, |
| 869 | LUNS_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 870 | if (sdebug_verbose) |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 871 | cp = "reported luns data has changed"; |
| 872 | break; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 873 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 874 | pr_warn("unexpected unit attention code=%d\n", k); |
| 875 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 876 | cp = "unknown"; |
| 877 | break; |
| 878 | } |
| 879 | clear_bit(k, devip->uas_bm); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 880 | if (sdebug_verbose) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 881 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 882 | "%s reports: Unit attention: %s\n", |
| 883 | my_name, cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | return check_condition_result; |
| 885 | } |
| 886 | return 0; |
| 887 | } |
| 888 | |
| 889 | /* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */ |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 890 | 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] | 891 | int arr_len) |
| 892 | { |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 893 | int act_len; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 894 | struct scsi_data_buffer *sdb = scsi_in(scp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 896 | if (!sdb->length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | return 0; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 898 | if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE)) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 899 | return DID_ERROR << 16; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 900 | |
| 901 | act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents, |
| 902 | arr, arr_len); |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 903 | sdb->resid = scsi_bufflen(scp) - act_len; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | /* Returns number of bytes fetched into 'arr' or -1 if error. */ |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 909 | static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr, |
| 910 | int arr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | { |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 912 | if (!scsi_bufflen(scp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | return 0; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 914 | if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_TO_DEVICE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | return -1; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 916 | |
| 917 | return scsi_sg_copy_to_buffer(scp, arr, arr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | |
| 921 | static const char * inq_vendor_id = "Linux "; |
| 922 | static const char * inq_product_id = "scsi_debug "; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 923 | static const char *inq_product_rev = "0186"; /* version less '.' */ |
| 924 | static const u64 naa5_comp_a = 0x5222222000000000ULL; |
| 925 | static const u64 naa5_comp_b = 0x5333333000000000ULL; |
| 926 | static const u64 naa5_comp_c = 0x5111111000000000ULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 928 | /* Device identification VPD page. Returns number of bytes placed in arr */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 929 | static int inquiry_vpd_83(unsigned char *arr, int port_group_id, |
| 930 | int target_dev_id, int dev_id_num, |
| 931 | const char *dev_id_str, |
| 932 | int dev_id_str_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 934 | int num, port_a; |
| 935 | char b[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 937 | port_a = target_dev_id + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | /* T10 vendor identifier field format (faked) */ |
| 939 | arr[0] = 0x2; /* ASCII */ |
| 940 | arr[1] = 0x1; |
| 941 | arr[2] = 0x0; |
| 942 | memcpy(&arr[4], inq_vendor_id, 8); |
| 943 | memcpy(&arr[12], inq_product_id, 16); |
| 944 | memcpy(&arr[28], dev_id_str, dev_id_str_len); |
| 945 | num = 8 + 16 + dev_id_str_len; |
| 946 | arr[3] = num; |
| 947 | num += 4; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 948 | if (dev_id_num >= 0) { |
| 949 | /* NAA-5, Logical unit identifier (binary) */ |
| 950 | arr[num++] = 0x1; /* binary (not necessarily sas) */ |
| 951 | arr[num++] = 0x3; /* PIV=0, lu, naa */ |
| 952 | arr[num++] = 0x0; |
| 953 | arr[num++] = 0x8; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 954 | put_unaligned_be64(naa5_comp_b + dev_id_num, arr + num); |
| 955 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 956 | /* Target relative port number */ |
| 957 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 958 | arr[num++] = 0x94; /* PIV=1, target port, rel port */ |
| 959 | arr[num++] = 0x0; /* reserved */ |
| 960 | arr[num++] = 0x4; /* length */ |
| 961 | arr[num++] = 0x0; /* reserved */ |
| 962 | arr[num++] = 0x0; /* reserved */ |
| 963 | arr[num++] = 0x0; |
| 964 | arr[num++] = 0x1; /* relative port A */ |
| 965 | } |
| 966 | /* NAA-5, Target port identifier */ |
| 967 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 968 | arr[num++] = 0x93; /* piv=1, target port, naa */ |
| 969 | arr[num++] = 0x0; |
| 970 | arr[num++] = 0x8; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 971 | put_unaligned_be64(naa5_comp_a + port_a, arr + num); |
| 972 | num += 8; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 973 | /* NAA-5, Target port group identifier */ |
| 974 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 975 | arr[num++] = 0x95; /* piv=1, target port group id */ |
| 976 | arr[num++] = 0x0; |
| 977 | arr[num++] = 0x4; |
| 978 | arr[num++] = 0; |
| 979 | arr[num++] = 0; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 980 | put_unaligned_be16(port_group_id, arr + num); |
| 981 | num += 2; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 982 | /* NAA-5, Target device identifier */ |
| 983 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 984 | arr[num++] = 0xa3; /* piv=1, target device, naa */ |
| 985 | arr[num++] = 0x0; |
| 986 | arr[num++] = 0x8; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 987 | put_unaligned_be64(naa5_comp_a + target_dev_id, arr + num); |
| 988 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 989 | /* SCSI name string: Target device identifier */ |
| 990 | arr[num++] = 0x63; /* proto=sas, UTF-8 */ |
| 991 | arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */ |
| 992 | arr[num++] = 0x0; |
| 993 | arr[num++] = 24; |
| 994 | memcpy(arr + num, "naa.52222220", 12); |
| 995 | num += 12; |
| 996 | snprintf(b, sizeof(b), "%08X", target_dev_id); |
| 997 | memcpy(arr + num, b, 8); |
| 998 | num += 8; |
| 999 | memset(arr + num, 0, 4); |
| 1000 | num += 4; |
| 1001 | return num; |
| 1002 | } |
| 1003 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1004 | static unsigned char vpd84_data[] = { |
| 1005 | /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0, |
| 1006 | 0x22,0x22,0x22,0x0,0xbb,0x1, |
| 1007 | 0x22,0x22,0x22,0x0,0xbb,0x2, |
| 1008 | }; |
| 1009 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1010 | /* Software interface identification VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1011 | static int inquiry_vpd_84(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1012 | { |
| 1013 | memcpy(arr, vpd84_data, sizeof(vpd84_data)); |
| 1014 | return sizeof(vpd84_data); |
| 1015 | } |
| 1016 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1017 | /* Management network addresses VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1018 | static int inquiry_vpd_85(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1019 | { |
| 1020 | int num = 0; |
| 1021 | const char * na1 = "https://www.kernel.org/config"; |
| 1022 | const char * na2 = "http://www.kernel.org/log"; |
| 1023 | int plen, olen; |
| 1024 | |
| 1025 | arr[num++] = 0x1; /* lu, storage config */ |
| 1026 | arr[num++] = 0x0; /* reserved */ |
| 1027 | arr[num++] = 0x0; |
| 1028 | olen = strlen(na1); |
| 1029 | plen = olen + 1; |
| 1030 | if (plen % 4) |
| 1031 | plen = ((plen / 4) + 1) * 4; |
| 1032 | arr[num++] = plen; /* length, null termianted, padded */ |
| 1033 | memcpy(arr + num, na1, olen); |
| 1034 | memset(arr + num + olen, 0, plen - olen); |
| 1035 | num += plen; |
| 1036 | |
| 1037 | arr[num++] = 0x4; /* lu, logging */ |
| 1038 | arr[num++] = 0x0; /* reserved */ |
| 1039 | arr[num++] = 0x0; |
| 1040 | olen = strlen(na2); |
| 1041 | plen = olen + 1; |
| 1042 | if (plen % 4) |
| 1043 | plen = ((plen / 4) + 1) * 4; |
| 1044 | arr[num++] = plen; /* length, null terminated, padded */ |
| 1045 | memcpy(arr + num, na2, olen); |
| 1046 | memset(arr + num + olen, 0, plen - olen); |
| 1047 | num += plen; |
| 1048 | |
| 1049 | return num; |
| 1050 | } |
| 1051 | |
| 1052 | /* SCSI ports VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1053 | static int inquiry_vpd_88(unsigned char *arr, int target_dev_id) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1054 | { |
| 1055 | int num = 0; |
| 1056 | int port_a, port_b; |
| 1057 | |
| 1058 | port_a = target_dev_id + 1; |
| 1059 | port_b = port_a + 1; |
| 1060 | arr[num++] = 0x0; /* reserved */ |
| 1061 | arr[num++] = 0x0; /* reserved */ |
| 1062 | arr[num++] = 0x0; |
| 1063 | arr[num++] = 0x1; /* relative port 1 (primary) */ |
| 1064 | memset(arr + num, 0, 6); |
| 1065 | num += 6; |
| 1066 | arr[num++] = 0x0; |
| 1067 | arr[num++] = 12; /* length tp descriptor */ |
| 1068 | /* naa-5 target port identifier (A) */ |
| 1069 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1070 | arr[num++] = 0x93; /* PIV=1, target port, NAA */ |
| 1071 | arr[num++] = 0x0; /* reserved */ |
| 1072 | arr[num++] = 0x8; /* length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1073 | put_unaligned_be64(naa5_comp_a + port_a, arr + num); |
| 1074 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1075 | arr[num++] = 0x0; /* reserved */ |
| 1076 | arr[num++] = 0x0; /* reserved */ |
| 1077 | arr[num++] = 0x0; |
| 1078 | arr[num++] = 0x2; /* relative port 2 (secondary) */ |
| 1079 | memset(arr + num, 0, 6); |
| 1080 | num += 6; |
| 1081 | arr[num++] = 0x0; |
| 1082 | arr[num++] = 12; /* length tp descriptor */ |
| 1083 | /* naa-5 target port identifier (B) */ |
| 1084 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1085 | arr[num++] = 0x93; /* PIV=1, target port, NAA */ |
| 1086 | arr[num++] = 0x0; /* reserved */ |
| 1087 | arr[num++] = 0x8; /* length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1088 | put_unaligned_be64(naa5_comp_a + port_b, arr + num); |
| 1089 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1090 | |
| 1091 | return num; |
| 1092 | } |
| 1093 | |
| 1094 | |
| 1095 | static unsigned char vpd89_data[] = { |
| 1096 | /* from 4th byte */ 0,0,0,0, |
| 1097 | 'l','i','n','u','x',' ',' ',' ', |
| 1098 | 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ', |
| 1099 | '1','2','3','4', |
| 1100 | 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0, |
| 1101 | 0xec,0,0,0, |
| 1102 | 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0, |
| 1103 | 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20, |
| 1104 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33, |
| 1105 | 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31, |
| 1106 | 0x53,0x41, |
| 1107 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 1108 | 0x20,0x20, |
| 1109 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 1110 | 0x10,0x80, |
| 1111 | 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0, |
| 1112 | 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0, |
| 1113 | 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0, |
| 1114 | 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0, |
| 1115 | 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40, |
| 1116 | 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0, |
| 1117 | 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0, |
| 1118 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1119 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1120 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1121 | 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42, |
| 1122 | 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8, |
| 1123 | 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe, |
| 1124 | 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0, |
| 1125 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1126 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1127 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1128 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1129 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1130 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1131 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1132 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1133 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1134 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1135 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1136 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51, |
| 1137 | }; |
| 1138 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1139 | /* ATA Information VPD page */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1140 | static int inquiry_vpd_89(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1141 | { |
| 1142 | memcpy(arr, vpd89_data, sizeof(vpd89_data)); |
| 1143 | return sizeof(vpd89_data); |
| 1144 | } |
| 1145 | |
| 1146 | |
| 1147 | static unsigned char vpdb0_data[] = { |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1148 | /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64, |
| 1149 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1150 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1151 | 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] | 1152 | }; |
| 1153 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1154 | /* Block limits VPD page (SBC-3) */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1155 | static int inquiry_vpd_b0(unsigned char *arr) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1156 | { |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 1157 | unsigned int gran; |
| 1158 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1159 | memcpy(arr, vpdb0_data, sizeof(vpdb0_data)); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1160 | |
| 1161 | /* Optimal transfer length granularity */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1162 | gran = 1 << sdebug_physblk_exp; |
| 1163 | put_unaligned_be16(gran, arr + 2); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1164 | |
| 1165 | /* Maximum Transfer Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1166 | if (sdebug_store_sectors > 0x400) |
| 1167 | put_unaligned_be32(sdebug_store_sectors, arr + 4); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1168 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1169 | /* Optimal Transfer Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1170 | put_unaligned_be32(sdebug_opt_blks, &arr[8]); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1171 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1172 | if (sdebug_lbpu) { |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1173 | /* Maximum Unmap LBA Count */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1174 | put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1175 | |
| 1176 | /* Maximum Unmap Block Descriptor Count */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1177 | put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1178 | } |
| 1179 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1180 | /* Unmap Granularity Alignment */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1181 | if (sdebug_unmap_alignment) { |
| 1182 | put_unaligned_be32(sdebug_unmap_alignment, &arr[28]); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1183 | arr[28] |= 0x80; /* UGAVALID */ |
| 1184 | } |
| 1185 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1186 | /* Optimal Unmap Granularity */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1187 | put_unaligned_be32(sdebug_unmap_granularity, &arr[24]); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1188 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1189 | /* Maximum WRITE SAME Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1190 | put_unaligned_be64(sdebug_write_same_length, &arr[32]); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1191 | |
| 1192 | return 0x3c; /* Mandatory page length for Logical Block Provisioning */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1193 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1194 | return sizeof(vpdb0_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1197 | /* Block device characteristics VPD page (SBC-3) */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1198 | static int inquiry_vpd_b1(unsigned char *arr) |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1199 | { |
| 1200 | memset(arr, 0, 0x3c); |
| 1201 | arr[0] = 0; |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1202 | arr[1] = 1; /* non rotating medium (e.g. solid state) */ |
| 1203 | arr[2] = 0; |
| 1204 | arr[3] = 5; /* less than 1.8" */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1205 | |
| 1206 | return 0x3c; |
| 1207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1209 | /* Logical block provisioning VPD page (SBC-4) */ |
| 1210 | static int inquiry_vpd_b2(unsigned char *arr) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1211 | { |
Martin K. Petersen | 3f0bc3b | 2012-03-08 10:48:29 -0500 | [diff] [blame] | 1212 | memset(arr, 0, 0x4); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1213 | arr[0] = 0; /* threshold exponent */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1214 | if (sdebug_lbpu) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1215 | arr[1] = 1 << 7; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1216 | if (sdebug_lbpws) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1217 | arr[1] |= 1 << 6; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1218 | if (sdebug_lbpws10) |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1219 | arr[1] |= 1 << 5; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1220 | if (sdebug_lbprz && scsi_debug_lbp()) |
| 1221 | arr[1] |= (sdebug_lbprz & 0x7) << 2; /* sbc4r07 and later */ |
| 1222 | /* anc_sup=0; dp=0 (no provisioning group descriptor) */ |
| 1223 | /* minimum_percentage=0; provisioning_type=0 (unknown) */ |
| 1224 | /* threshold_percentage=0 */ |
Martin K. Petersen | 3f0bc3b | 2012-03-08 10:48:29 -0500 | [diff] [blame] | 1225 | return 0x4; |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1226 | } |
| 1227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | #define SDEBUG_LONG_INQ_SZ 96 |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1229 | #define SDEBUG_MAX_INQ_ARR_SZ 584 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1231 | 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] | 1232 | { |
| 1233 | unsigned char pq_pdt; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1234 | unsigned char * arr; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1235 | unsigned char *cmd = scp->cmnd; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1236 | int alloc_len, n, ret; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1237 | bool have_wlun, is_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1239 | alloc_len = get_unaligned_be16(cmd + 3); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 1240 | arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC); |
| 1241 | if (! arr) |
| 1242 | return DID_REQUEUE << 16; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1243 | is_disk = (sdebug_ptype == TYPE_DISK); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1244 | have_wlun = scsi_is_wlun(scp->device->lun); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1245 | if (have_wlun) |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1246 | pq_pdt = TYPE_WLUN; /* present, wlun */ |
| 1247 | else if (sdebug_no_lun_0 && (devip->lun == SDEBUG_LUN_0_VAL)) |
| 1248 | pq_pdt = 0x7f; /* not present, PQ=3, PDT=0x1f */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1249 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1250 | pq_pdt = (sdebug_ptype & 0x1f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | arr[0] = pq_pdt; |
| 1252 | if (0x2 & cmd[1]) { /* CMDDT bit set */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1253 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1254 | kfree(arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | return check_condition_result; |
| 1256 | } else if (0x1 & cmd[1]) { /* EVPD bit set */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1257 | int lu_id_num, port_group_id, target_dev_id, len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1258 | char lu_id_str[6]; |
| 1259 | int host_no = devip->sdbg_host->shost->host_no; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1261 | port_group_id = (((host_no + 1) & 0x7f) << 8) + |
| 1262 | (devip->channel & 0x7f); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1263 | if (sdebug_vpd_use_hostno == 0) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1264 | host_no = 0; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1265 | lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) + |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1266 | (devip->target * 1000) + devip->lun); |
| 1267 | target_dev_id = ((host_no + 1) * 2000) + |
| 1268 | (devip->target * 1000) - 3; |
| 1269 | len = scnprintf(lu_id_str, 6, "%d", lu_id_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | if (0 == cmd[2]) { /* supported vital product data pages */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1271 | arr[1] = cmd[2]; /*sanity */ |
| 1272 | n = 4; |
| 1273 | arr[n++] = 0x0; /* this page */ |
| 1274 | arr[n++] = 0x80; /* unit serial number */ |
| 1275 | arr[n++] = 0x83; /* device identification */ |
| 1276 | arr[n++] = 0x84; /* software interface ident. */ |
| 1277 | arr[n++] = 0x85; /* management network addresses */ |
| 1278 | arr[n++] = 0x86; /* extended inquiry */ |
| 1279 | arr[n++] = 0x87; /* mode page policy */ |
| 1280 | arr[n++] = 0x88; /* SCSI ports */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1281 | if (is_disk) { /* SBC only */ |
| 1282 | arr[n++] = 0x89; /* ATA information */ |
| 1283 | arr[n++] = 0xb0; /* Block limits */ |
| 1284 | arr[n++] = 0xb1; /* Block characteristics */ |
| 1285 | arr[n++] = 0xb2; /* Logical Block Prov */ |
| 1286 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1287 | arr[3] = n - 4; /* number of supported VPD pages */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | } else if (0x80 == cmd[2]) { /* unit serial number */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1289 | arr[1] = cmd[2]; /*sanity */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | arr[3] = len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1291 | memcpy(&arr[4], lu_id_str, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | } else if (0x83 == cmd[2]) { /* device identification */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1293 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1294 | arr[3] = inquiry_vpd_83(&arr[4], port_group_id, |
| 1295 | target_dev_id, lu_id_num, |
| 1296 | lu_id_str, len); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1297 | } else if (0x84 == cmd[2]) { /* Software interface ident. */ |
| 1298 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1299 | arr[3] = inquiry_vpd_84(&arr[4]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1300 | } else if (0x85 == cmd[2]) { /* Management network addresses */ |
| 1301 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1302 | arr[3] = inquiry_vpd_85(&arr[4]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1303 | } else if (0x86 == cmd[2]) { /* extended inquiry */ |
| 1304 | arr[1] = cmd[2]; /*sanity */ |
| 1305 | arr[3] = 0x3c; /* number of following entries */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1306 | if (sdebug_dif == SD_DIF_TYPE3_PROTECTION) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1307 | arr[4] = 0x4; /* SPT: GRD_CHK:1 */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1308 | else if (have_dif_prot) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1309 | arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */ |
| 1310 | else |
| 1311 | arr[4] = 0x0; /* no protection stuff */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1312 | arr[5] = 0x7; /* head of q, ordered + simple q's */ |
| 1313 | } else if (0x87 == cmd[2]) { /* mode page policy */ |
| 1314 | arr[1] = cmd[2]; /*sanity */ |
| 1315 | arr[3] = 0x8; /* number of following entries */ |
| 1316 | arr[4] = 0x2; /* disconnect-reconnect mp */ |
| 1317 | arr[6] = 0x80; /* mlus, shared */ |
| 1318 | arr[8] = 0x18; /* protocol specific lu */ |
| 1319 | arr[10] = 0x82; /* mlus, per initiator port */ |
| 1320 | } else if (0x88 == cmd[2]) { /* SCSI Ports */ |
| 1321 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1322 | arr[3] = inquiry_vpd_88(&arr[4], target_dev_id); |
| 1323 | } else if (is_disk && 0x89 == cmd[2]) { /* ATA information */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1324 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1325 | n = inquiry_vpd_89(&arr[4]); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1326 | put_unaligned_be16(n, arr + 2); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1327 | } else if (is_disk && 0xb0 == cmd[2]) { /* Block limits */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1328 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1329 | arr[3] = inquiry_vpd_b0(&arr[4]); |
| 1330 | } else if (is_disk && 0xb1 == cmd[2]) { /* Block char. */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1331 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1332 | arr[3] = inquiry_vpd_b1(&arr[4]); |
| 1333 | } else if (is_disk && 0xb2 == cmd[2]) { /* LB Prov. */ |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1334 | arr[1] = cmd[2]; /*sanity */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1335 | arr[3] = inquiry_vpd_b2(&arr[4]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1337 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1338 | kfree(arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | return check_condition_result; |
| 1340 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1341 | len = min(get_unaligned_be16(arr + 2) + 4, alloc_len); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1342 | ret = fill_from_dev_buffer(scp, arr, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1343 | min(len, SDEBUG_MAX_INQ_ARR_SZ)); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1344 | kfree(arr); |
| 1345 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | } |
| 1347 | /* drops through here for a standard inquiry */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1348 | arr[1] = sdebug_removable ? 0x80 : 0; /* Removable disk */ |
| 1349 | arr[2] = sdebug_scsi_level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | arr[3] = 2; /* response_data_format==2 */ |
| 1351 | arr[4] = SDEBUG_LONG_INQ_SZ - 5; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 1352 | arr[5] = (int)have_dif_prot; /* PROTECT bit */ |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1353 | if (sdebug_vpd_use_hostno == 0) |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1354 | arr[5] = 0x10; /* claim: implicit TGPS */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1355 | arr[6] = 0x10; /* claim: MultiP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1357 | arr[7] = 0xa; /* claim: LINKED + CMDQUE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | memcpy(&arr[8], inq_vendor_id, 8); |
| 1359 | memcpy(&arr[16], inq_product_id, 16); |
| 1360 | memcpy(&arr[32], inq_product_rev, 4); |
| 1361 | /* version descriptors (2 bytes each) follow */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1362 | put_unaligned_be16(0xc0, arr + 58); /* SAM-6 no version claimed */ |
| 1363 | put_unaligned_be16(0x5c0, arr + 60); /* SPC-5 no version claimed */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1364 | n = 62; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1365 | if (is_disk) { /* SBC-4 no version claimed */ |
| 1366 | put_unaligned_be16(0x600, arr + n); |
| 1367 | n += 2; |
| 1368 | } else if (sdebug_ptype == TYPE_TAPE) { /* SSC-4 rev 3 */ |
| 1369 | put_unaligned_be16(0x525, arr + n); |
| 1370 | n += 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | } |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1372 | put_unaligned_be16(0x2100, arr + n); /* SPL-4 no version claimed */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1373 | ret = fill_from_dev_buffer(scp, arr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | min(alloc_len, SDEBUG_LONG_INQ_SZ)); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1375 | kfree(arr); |
| 1376 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1379 | static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, |
| 1380 | 0, 0, 0x0, 0x0}; |
| 1381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | static int resp_requests(struct scsi_cmnd * scp, |
| 1383 | struct sdebug_dev_info * devip) |
| 1384 | { |
| 1385 | unsigned char * sbuff; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1386 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1387 | unsigned char arr[SCSI_SENSE_BUFFERSIZE]; |
Tomas Winkler | 2492fc0 | 2015-07-28 16:54:26 +0300 | [diff] [blame] | 1388 | bool dsense; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | int len = 18; |
| 1390 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1391 | memset(arr, 0, sizeof(arr)); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1392 | dsense = !!(cmd[1] & 1); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1393 | sbuff = scp->sense_buffer; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1394 | if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1395 | if (dsense) { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1396 | arr[0] = 0x72; |
| 1397 | arr[1] = 0x0; /* NO_SENSE in sense_key */ |
| 1398 | arr[2] = THRESHOLD_EXCEEDED; |
| 1399 | arr[3] = 0xff; /* TEST set and MRIE==6 */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1400 | len = 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1401 | } else { |
| 1402 | arr[0] = 0x70; |
| 1403 | arr[2] = 0x0; /* NO_SENSE in sense_key */ |
| 1404 | arr[7] = 0xa; /* 18 byte sense buffer */ |
| 1405 | arr[12] = THRESHOLD_EXCEEDED; |
| 1406 | arr[13] = 0xff; /* TEST set and MRIE==6 */ |
| 1407 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1408 | } else { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1409 | memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1410 | if (arr[0] >= 0x70 && dsense == sdebug_dsense) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1411 | ; /* have sense and formats match */ |
| 1412 | else if (arr[0] <= 0x70) { |
| 1413 | if (dsense) { |
| 1414 | memset(arr, 0, 8); |
| 1415 | arr[0] = 0x72; |
| 1416 | len = 8; |
| 1417 | } else { |
| 1418 | memset(arr, 0, 18); |
| 1419 | arr[0] = 0x70; |
| 1420 | arr[7] = 0xa; |
| 1421 | } |
| 1422 | } else if (dsense) { |
| 1423 | memset(arr, 0, 8); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1424 | arr[0] = 0x72; |
| 1425 | arr[1] = sbuff[2]; /* sense key */ |
| 1426 | arr[2] = sbuff[12]; /* asc */ |
| 1427 | arr[3] = sbuff[13]; /* ascq */ |
| 1428 | len = 8; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1429 | } else { |
| 1430 | memset(arr, 0, 18); |
| 1431 | arr[0] = 0x70; |
| 1432 | arr[2] = sbuff[1]; |
| 1433 | arr[7] = 0xa; |
| 1434 | arr[12] = sbuff[1]; |
| 1435 | arr[13] = sbuff[3]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1436 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1437 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1438 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1439 | mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | return fill_from_dev_buffer(scp, arr, len); |
| 1441 | } |
| 1442 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1443 | static int resp_start_stop(struct scsi_cmnd * scp, |
| 1444 | struct sdebug_dev_info * devip) |
| 1445 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1446 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 1447 | int power_cond, stop; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1448 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1449 | power_cond = (cmd[4] & 0xf0) >> 4; |
| 1450 | if (power_cond) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1451 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1452 | return check_condition_result; |
| 1453 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 1454 | stop = !(cmd[4] & 1); |
| 1455 | atomic_xchg(&devip->stopped, stop); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1456 | return 0; |
| 1457 | } |
| 1458 | |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1459 | static sector_t get_sdebug_capacity(void) |
| 1460 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1461 | static const unsigned int gibibyte = 1073741824; |
| 1462 | |
| 1463 | if (sdebug_virtual_gb > 0) |
| 1464 | return (sector_t)sdebug_virtual_gb * |
| 1465 | (gibibyte / sdebug_sector_size); |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1466 | else |
| 1467 | return sdebug_store_sectors; |
| 1468 | } |
| 1469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | #define SDEBUG_READCAP_ARR_SZ 8 |
| 1471 | static int resp_readcap(struct scsi_cmnd * scp, |
| 1472 | struct sdebug_dev_info * devip) |
| 1473 | { |
| 1474 | unsigned char arr[SDEBUG_READCAP_ARR_SZ]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1475 | unsigned int capac; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1477 | /* following just in case virtual_gb changed */ |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1478 | sdebug_capacity = get_sdebug_capacity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | memset(arr, 0, SDEBUG_READCAP_ARR_SZ); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1480 | if (sdebug_capacity < 0xffffffff) { |
| 1481 | capac = (unsigned int)sdebug_capacity - 1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1482 | put_unaligned_be32(capac, arr + 0); |
| 1483 | } else |
| 1484 | put_unaligned_be32(0xffffffff, arr + 0); |
| 1485 | put_unaligned_be16(sdebug_sector_size, arr + 6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ); |
| 1487 | } |
| 1488 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1489 | #define SDEBUG_READCAP16_ARR_SZ 32 |
| 1490 | static int resp_readcap16(struct scsi_cmnd * scp, |
| 1491 | struct sdebug_dev_info * devip) |
| 1492 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1493 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1494 | unsigned char arr[SDEBUG_READCAP16_ARR_SZ]; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1495 | int alloc_len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1496 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1497 | alloc_len = get_unaligned_be32(cmd + 10); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1498 | /* following just in case virtual_gb changed */ |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1499 | sdebug_capacity = get_sdebug_capacity(); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1500 | memset(arr, 0, SDEBUG_READCAP16_ARR_SZ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1501 | put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0); |
| 1502 | put_unaligned_be32(sdebug_sector_size, arr + 8); |
| 1503 | arr[13] = sdebug_physblk_exp & 0xf; |
| 1504 | arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1505 | |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1506 | if (scsi_debug_lbp()) { |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1507 | arr[14] |= 0x80; /* LBPME */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1508 | /* from sbc4r07, this LBPRZ field is 1 bit, but the LBPRZ in |
| 1509 | * the LB Provisioning VPD page is 3 bits. Note that lbprz=2 |
| 1510 | * in the wider field maps to 0 in this field. |
| 1511 | */ |
| 1512 | if (sdebug_lbprz & 1) /* precisely what the draft requires */ |
| 1513 | arr[14] |= 0x40; |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1514 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1515 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1516 | arr[15] = sdebug_lowest_aligned & 0xff; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1517 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1518 | if (have_dif_prot) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1519 | arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */ |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1520 | arr[12] |= 1; /* PROT_EN */ |
| 1521 | } |
| 1522 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1523 | return fill_from_dev_buffer(scp, arr, |
| 1524 | min(alloc_len, SDEBUG_READCAP16_ARR_SZ)); |
| 1525 | } |
| 1526 | |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1527 | #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412 |
| 1528 | |
| 1529 | static int resp_report_tgtpgs(struct scsi_cmnd * scp, |
| 1530 | struct sdebug_dev_info * devip) |
| 1531 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1532 | unsigned char *cmd = scp->cmnd; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1533 | unsigned char * arr; |
| 1534 | int host_no = devip->sdbg_host->shost->host_no; |
| 1535 | int n, ret, alen, rlen; |
| 1536 | int port_group_a, port_group_b, port_a, port_b; |
| 1537 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1538 | alen = get_unaligned_be32(cmd + 6); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 1539 | arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC); |
| 1540 | if (! arr) |
| 1541 | return DID_REQUEUE << 16; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1542 | /* |
| 1543 | * EVPD page 0x88 states we have two ports, one |
| 1544 | * real and a fake port with no device connected. |
| 1545 | * So we create two port groups with one port each |
| 1546 | * and set the group with port B to unavailable. |
| 1547 | */ |
| 1548 | port_a = 0x1; /* relative port A */ |
| 1549 | port_b = 0x2; /* relative port B */ |
| 1550 | port_group_a = (((host_no + 1) & 0x7f) << 8) + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1551 | (devip->channel & 0x7f); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1552 | port_group_b = (((host_no + 1) & 0x7f) << 8) + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1553 | (devip->channel & 0x7f) + 0x80; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1554 | |
| 1555 | /* |
| 1556 | * The asymmetric access state is cycled according to the host_id. |
| 1557 | */ |
| 1558 | n = 4; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1559 | if (sdebug_vpd_use_hostno == 0) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1560 | arr[n++] = host_no % 3; /* Asymm access state */ |
| 1561 | arr[n++] = 0x0F; /* claim: all states are supported */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1562 | } else { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1563 | arr[n++] = 0x0; /* Active/Optimized path */ |
| 1564 | arr[n++] = 0x01; /* only support active/optimized paths */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1565 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1566 | put_unaligned_be16(port_group_a, arr + n); |
| 1567 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1568 | arr[n++] = 0; /* Reserved */ |
| 1569 | arr[n++] = 0; /* Status code */ |
| 1570 | arr[n++] = 0; /* Vendor unique */ |
| 1571 | arr[n++] = 0x1; /* One port per group */ |
| 1572 | arr[n++] = 0; /* Reserved */ |
| 1573 | arr[n++] = 0; /* Reserved */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1574 | put_unaligned_be16(port_a, arr + n); |
| 1575 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1576 | arr[n++] = 3; /* Port unavailable */ |
| 1577 | arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1578 | put_unaligned_be16(port_group_b, arr + n); |
| 1579 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1580 | arr[n++] = 0; /* Reserved */ |
| 1581 | arr[n++] = 0; /* Status code */ |
| 1582 | arr[n++] = 0; /* Vendor unique */ |
| 1583 | arr[n++] = 0x1; /* One port per group */ |
| 1584 | arr[n++] = 0; /* Reserved */ |
| 1585 | arr[n++] = 0; /* Reserved */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1586 | put_unaligned_be16(port_b, arr + n); |
| 1587 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1588 | |
| 1589 | rlen = n - 4; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1590 | put_unaligned_be32(rlen, arr + 0); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1591 | |
| 1592 | /* |
| 1593 | * Return the smallest value of either |
| 1594 | * - The allocated length |
| 1595 | * - The constructed command length |
| 1596 | * - The maximum array size |
| 1597 | */ |
| 1598 | rlen = min(alen,n); |
| 1599 | ret = fill_from_dev_buffer(scp, arr, |
| 1600 | min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ)); |
| 1601 | kfree(arr); |
| 1602 | return ret; |
| 1603 | } |
| 1604 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1605 | static int resp_rsup_opcodes(struct scsi_cmnd *scp, |
| 1606 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1607 | { |
| 1608 | bool rctd; |
| 1609 | u8 reporting_opts, req_opcode, sdeb_i, supp; |
| 1610 | u16 req_sa, u; |
| 1611 | u32 alloc_len, a_len; |
| 1612 | int k, offset, len, errsts, count, bump, na; |
| 1613 | const struct opcode_info_t *oip; |
| 1614 | const struct opcode_info_t *r_oip; |
| 1615 | u8 *arr; |
| 1616 | u8 *cmd = scp->cmnd; |
| 1617 | |
| 1618 | rctd = !!(cmd[2] & 0x80); |
| 1619 | reporting_opts = cmd[2] & 0x7; |
| 1620 | req_opcode = cmd[3]; |
| 1621 | req_sa = get_unaligned_be16(cmd + 4); |
| 1622 | alloc_len = get_unaligned_be32(cmd + 6); |
Colin Ian King | 6d310df | 2015-01-22 11:20:40 +0000 | [diff] [blame] | 1623 | if (alloc_len < 4 || alloc_len > 0xffff) { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1624 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
| 1625 | return check_condition_result; |
| 1626 | } |
| 1627 | if (alloc_len > 8192) |
| 1628 | a_len = 8192; |
| 1629 | else |
| 1630 | a_len = alloc_len; |
Sasha Levin | 99531e6 | 2015-01-17 17:47:37 -0500 | [diff] [blame] | 1631 | arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1632 | if (NULL == arr) { |
| 1633 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 1634 | INSUFF_RES_ASCQ); |
| 1635 | return check_condition_result; |
| 1636 | } |
| 1637 | switch (reporting_opts) { |
| 1638 | case 0: /* all commands */ |
| 1639 | /* count number of commands */ |
| 1640 | for (count = 0, oip = opcode_info_arr; |
| 1641 | oip->num_attached != 0xff; ++oip) { |
| 1642 | if (F_INV_OP & oip->flags) |
| 1643 | continue; |
| 1644 | count += (oip->num_attached + 1); |
| 1645 | } |
| 1646 | bump = rctd ? 20 : 8; |
| 1647 | put_unaligned_be32(count * bump, arr); |
| 1648 | for (offset = 4, oip = opcode_info_arr; |
| 1649 | oip->num_attached != 0xff && offset < a_len; ++oip) { |
| 1650 | if (F_INV_OP & oip->flags) |
| 1651 | continue; |
| 1652 | na = oip->num_attached; |
| 1653 | arr[offset] = oip->opcode; |
| 1654 | put_unaligned_be16(oip->sa, arr + offset + 2); |
| 1655 | if (rctd) |
| 1656 | arr[offset + 5] |= 0x2; |
| 1657 | if (FF_SA & oip->flags) |
| 1658 | arr[offset + 5] |= 0x1; |
| 1659 | put_unaligned_be16(oip->len_mask[0], arr + offset + 6); |
| 1660 | if (rctd) |
| 1661 | put_unaligned_be16(0xa, arr + offset + 8); |
| 1662 | r_oip = oip; |
| 1663 | for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) { |
| 1664 | if (F_INV_OP & oip->flags) |
| 1665 | continue; |
| 1666 | offset += bump; |
| 1667 | arr[offset] = oip->opcode; |
| 1668 | put_unaligned_be16(oip->sa, arr + offset + 2); |
| 1669 | if (rctd) |
| 1670 | arr[offset + 5] |= 0x2; |
| 1671 | if (FF_SA & oip->flags) |
| 1672 | arr[offset + 5] |= 0x1; |
| 1673 | put_unaligned_be16(oip->len_mask[0], |
| 1674 | arr + offset + 6); |
| 1675 | if (rctd) |
| 1676 | put_unaligned_be16(0xa, |
| 1677 | arr + offset + 8); |
| 1678 | } |
| 1679 | oip = r_oip; |
| 1680 | offset += bump; |
| 1681 | } |
| 1682 | break; |
| 1683 | case 1: /* one command: opcode only */ |
| 1684 | case 2: /* one command: opcode plus service action */ |
| 1685 | case 3: /* one command: if sa==0 then opcode only else opcode+sa */ |
| 1686 | sdeb_i = opcode_ind_arr[req_opcode]; |
| 1687 | oip = &opcode_info_arr[sdeb_i]; |
| 1688 | if (F_INV_OP & oip->flags) { |
| 1689 | supp = 1; |
| 1690 | offset = 4; |
| 1691 | } else { |
| 1692 | if (1 == reporting_opts) { |
| 1693 | if (FF_SA & oip->flags) { |
| 1694 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, |
| 1695 | 2, 2); |
| 1696 | kfree(arr); |
| 1697 | return check_condition_result; |
| 1698 | } |
| 1699 | req_sa = 0; |
| 1700 | } else if (2 == reporting_opts && |
| 1701 | 0 == (FF_SA & oip->flags)) { |
| 1702 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1); |
| 1703 | kfree(arr); /* point at requested sa */ |
| 1704 | return check_condition_result; |
| 1705 | } |
| 1706 | if (0 == (FF_SA & oip->flags) && |
| 1707 | req_opcode == oip->opcode) |
| 1708 | supp = 3; |
| 1709 | else if (0 == (FF_SA & oip->flags)) { |
| 1710 | na = oip->num_attached; |
| 1711 | for (k = 0, oip = oip->arrp; k < na; |
| 1712 | ++k, ++oip) { |
| 1713 | if (req_opcode == oip->opcode) |
| 1714 | break; |
| 1715 | } |
| 1716 | supp = (k >= na) ? 1 : 3; |
| 1717 | } else if (req_sa != oip->sa) { |
| 1718 | na = oip->num_attached; |
| 1719 | for (k = 0, oip = oip->arrp; k < na; |
| 1720 | ++k, ++oip) { |
| 1721 | if (req_sa == oip->sa) |
| 1722 | break; |
| 1723 | } |
| 1724 | supp = (k >= na) ? 1 : 3; |
| 1725 | } else |
| 1726 | supp = 3; |
| 1727 | if (3 == supp) { |
| 1728 | u = oip->len_mask[0]; |
| 1729 | put_unaligned_be16(u, arr + 2); |
| 1730 | arr[4] = oip->opcode; |
| 1731 | for (k = 1; k < u; ++k) |
| 1732 | arr[4 + k] = (k < 16) ? |
| 1733 | oip->len_mask[k] : 0xff; |
| 1734 | offset = 4 + u; |
| 1735 | } else |
| 1736 | offset = 4; |
| 1737 | } |
| 1738 | arr[1] = (rctd ? 0x80 : 0) | supp; |
| 1739 | if (rctd) { |
| 1740 | put_unaligned_be16(0xa, arr + offset); |
| 1741 | offset += 12; |
| 1742 | } |
| 1743 | break; |
| 1744 | default: |
| 1745 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2); |
| 1746 | kfree(arr); |
| 1747 | return check_condition_result; |
| 1748 | } |
| 1749 | offset = (offset < a_len) ? offset : a_len; |
| 1750 | len = (offset < alloc_len) ? offset : alloc_len; |
| 1751 | errsts = fill_from_dev_buffer(scp, arr, len); |
| 1752 | kfree(arr); |
| 1753 | return errsts; |
| 1754 | } |
| 1755 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1756 | static int resp_rsup_tmfs(struct scsi_cmnd *scp, |
| 1757 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1758 | { |
| 1759 | bool repd; |
| 1760 | u32 alloc_len, len; |
| 1761 | u8 arr[16]; |
| 1762 | u8 *cmd = scp->cmnd; |
| 1763 | |
| 1764 | memset(arr, 0, sizeof(arr)); |
| 1765 | repd = !!(cmd[2] & 0x80); |
| 1766 | alloc_len = get_unaligned_be32(cmd + 6); |
| 1767 | if (alloc_len < 4) { |
| 1768 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
| 1769 | return check_condition_result; |
| 1770 | } |
| 1771 | arr[0] = 0xc8; /* ATS | ATSS | LURS */ |
| 1772 | arr[1] = 0x1; /* ITNRS */ |
| 1773 | if (repd) { |
| 1774 | arr[3] = 0xc; |
| 1775 | len = 16; |
| 1776 | } else |
| 1777 | len = 4; |
| 1778 | |
| 1779 | len = (len < alloc_len) ? len : alloc_len; |
| 1780 | return fill_from_dev_buffer(scp, arr, len); |
| 1781 | } |
| 1782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | /* <<Following mode page info copied from ST318451LW>> */ |
| 1784 | |
| 1785 | static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target) |
| 1786 | { /* Read-Write Error Recovery page for mode_sense */ |
| 1787 | unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0, |
| 1788 | 5, 0, 0xff, 0xff}; |
| 1789 | |
| 1790 | memcpy(p, err_recov_pg, sizeof(err_recov_pg)); |
| 1791 | if (1 == pcontrol) |
| 1792 | memset(p + 2, 0, sizeof(err_recov_pg) - 2); |
| 1793 | return sizeof(err_recov_pg); |
| 1794 | } |
| 1795 | |
| 1796 | static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target) |
| 1797 | { /* Disconnect-Reconnect page for mode_sense */ |
| 1798 | unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0, |
| 1799 | 0, 0, 0, 0, 0, 0, 0, 0}; |
| 1800 | |
| 1801 | memcpy(p, disconnect_pg, sizeof(disconnect_pg)); |
| 1802 | if (1 == pcontrol) |
| 1803 | memset(p + 2, 0, sizeof(disconnect_pg) - 2); |
| 1804 | return sizeof(disconnect_pg); |
| 1805 | } |
| 1806 | |
| 1807 | static int resp_format_pg(unsigned char * p, int pcontrol, int target) |
| 1808 | { /* Format device page for mode_sense */ |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1809 | unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0, |
| 1810 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1811 | 0, 0, 0, 0, 0x40, 0, 0, 0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1813 | memcpy(p, format_pg, sizeof(format_pg)); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1814 | put_unaligned_be16(sdebug_sectors_per, p + 10); |
| 1815 | put_unaligned_be16(sdebug_sector_size, p + 12); |
| 1816 | if (sdebug_removable) |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1817 | p[20] |= 0x20; /* should agree with INQUIRY */ |
| 1818 | if (1 == pcontrol) |
| 1819 | memset(p + 2, 0, sizeof(format_pg) - 2); |
| 1820 | return sizeof(format_pg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | } |
| 1822 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1823 | static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0, |
| 1824 | 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, |
| 1825 | 0, 0, 0, 0}; |
| 1826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | static int resp_caching_pg(unsigned char * p, int pcontrol, int target) |
| 1828 | { /* Caching page for mode_sense */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1829 | unsigned char ch_caching_pg[] = {/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0, |
| 1830 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 1831 | 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] | 1832 | 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0}; |
| 1833 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1834 | if (SDEBUG_OPT_N_WCE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1835 | caching_pg[2] &= ~0x4; /* set WCE=0 (default WCE=1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | memcpy(p, caching_pg, sizeof(caching_pg)); |
| 1837 | if (1 == pcontrol) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1838 | memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg)); |
| 1839 | else if (2 == pcontrol) |
| 1840 | memcpy(p, d_caching_pg, sizeof(d_caching_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | return sizeof(caching_pg); |
| 1842 | } |
| 1843 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1844 | static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0, |
| 1845 | 0, 0, 0x2, 0x4b}; |
| 1846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target) |
| 1848 | { /* Control mode page for mode_sense */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1849 | unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0, |
| 1850 | 0, 0, 0, 0}; |
| 1851 | 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] | 1852 | 0, 0, 0x2, 0x4b}; |
| 1853 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1854 | if (sdebug_dsense) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | ctrl_m_pg[2] |= 0x4; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1856 | else |
| 1857 | ctrl_m_pg[2] &= ~0x4; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1858 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1859 | if (sdebug_ato) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1860 | ctrl_m_pg[5] |= 0x80; /* ATO=1 */ |
| 1861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg)); |
| 1863 | if (1 == pcontrol) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1864 | memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg)); |
| 1865 | else if (2 == pcontrol) |
| 1866 | memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | return sizeof(ctrl_m_pg); |
| 1868 | } |
| 1869 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target) |
| 1872 | { /* Informational Exceptions control mode page for mode_sense */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1873 | unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0, |
| 1874 | 0, 0, 0x0, 0x0}; |
| 1875 | unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, |
| 1876 | 0, 0, 0x0, 0x0}; |
| 1877 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | memcpy(p, iec_m_pg, sizeof(iec_m_pg)); |
| 1879 | if (1 == pcontrol) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1880 | memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg)); |
| 1881 | else if (2 == pcontrol) |
| 1882 | memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | return sizeof(iec_m_pg); |
| 1884 | } |
| 1885 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1886 | static int resp_sas_sf_m_pg(unsigned char * p, int pcontrol, int target) |
| 1887 | { /* SAS SSP mode page - short format for mode_sense */ |
| 1888 | unsigned char sas_sf_m_pg[] = {0x19, 0x6, |
| 1889 | 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0}; |
| 1890 | |
| 1891 | memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg)); |
| 1892 | if (1 == pcontrol) |
| 1893 | memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2); |
| 1894 | return sizeof(sas_sf_m_pg); |
| 1895 | } |
| 1896 | |
| 1897 | |
| 1898 | static int resp_sas_pcd_m_spg(unsigned char * p, int pcontrol, int target, |
| 1899 | int target_dev_id) |
| 1900 | { /* SAS phy control and discover mode page for mode_sense */ |
| 1901 | unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2, |
| 1902 | 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1903 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
| 1904 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1905 | 0x2, 0, 0, 0, 0, 0, 0, 0, |
| 1906 | 0x88, 0x99, 0, 0, 0, 0, 0, 0, |
| 1907 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1908 | 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1909 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
| 1910 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1911 | 0x3, 0, 0, 0, 0, 0, 0, 0, |
| 1912 | 0x88, 0x99, 0, 0, 0, 0, 0, 0, |
| 1913 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1914 | }; |
| 1915 | int port_a, port_b; |
| 1916 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1917 | put_unaligned_be64(naa5_comp_a, sas_pcd_m_pg + 16); |
| 1918 | put_unaligned_be64(naa5_comp_c + 1, sas_pcd_m_pg + 24); |
| 1919 | put_unaligned_be64(naa5_comp_a, sas_pcd_m_pg + 64); |
| 1920 | put_unaligned_be64(naa5_comp_c + 1, sas_pcd_m_pg + 72); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1921 | port_a = target_dev_id + 1; |
| 1922 | port_b = port_a + 1; |
| 1923 | memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg)); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1924 | put_unaligned_be32(port_a, p + 20); |
| 1925 | put_unaligned_be32(port_b, p + 48 + 20); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1926 | if (1 == pcontrol) |
| 1927 | memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4); |
| 1928 | return sizeof(sas_pcd_m_pg); |
| 1929 | } |
| 1930 | |
| 1931 | static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol) |
| 1932 | { /* SAS SSP shared protocol specific port mode subpage */ |
| 1933 | unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0, |
| 1934 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1935 | }; |
| 1936 | |
| 1937 | memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg)); |
| 1938 | if (1 == pcontrol) |
| 1939 | memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4); |
| 1940 | return sizeof(sas_sha_m_pg); |
| 1941 | } |
| 1942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | #define SDEBUG_MAX_MSENSE_SZ 256 |
| 1944 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1945 | static int resp_mode_sense(struct scsi_cmnd *scp, |
| 1946 | struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | { |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1948 | int pcontrol, pcode, subpcode, bd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | unsigned char dev_spec; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1950 | int alloc_len, offset, len, target_dev_id; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1951 | int target = scp->device->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | unsigned char * ap; |
| 1953 | unsigned char arr[SDEBUG_MAX_MSENSE_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1954 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1955 | bool dbd, llbaa, msense_6, is_disk, bad_pcode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1957 | dbd = !!(cmd[1] & 0x8); /* disable block descriptors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | pcontrol = (cmd[2] & 0xc0) >> 6; |
| 1959 | pcode = cmd[2] & 0x3f; |
| 1960 | subpcode = cmd[3]; |
| 1961 | msense_6 = (MODE_SENSE == cmd[0]); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1962 | llbaa = msense_6 ? false : !!(cmd[1] & 0x10); |
| 1963 | is_disk = (sdebug_ptype == TYPE_DISK); |
| 1964 | if (is_disk && !dbd) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1965 | bd_len = llbaa ? 16 : 8; |
| 1966 | else |
| 1967 | bd_len = 0; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1968 | alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | memset(arr, 0, SDEBUG_MAX_MSENSE_SZ); |
| 1970 | if (0x3 == pcontrol) { /* Saving values not supported */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1971 | mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | return check_condition_result; |
| 1973 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1974 | target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) + |
| 1975 | (devip->target * 1000) - 3; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1976 | /* for disks set DPOFUA bit and clear write protect (WP) bit */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 1977 | if (is_disk) |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1978 | dev_spec = 0x10; /* =0x90 if WP=1 implies read-only */ |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1979 | else |
| 1980 | dev_spec = 0x0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | if (msense_6) { |
| 1982 | arr[2] = dev_spec; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1983 | arr[3] = bd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | offset = 4; |
| 1985 | } else { |
| 1986 | arr[3] = dev_spec; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1987 | if (16 == bd_len) |
| 1988 | arr[4] = 0x1; /* set LONGLBA bit */ |
| 1989 | arr[7] = bd_len; /* assume 255 or less */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | offset = 8; |
| 1991 | } |
| 1992 | ap = arr + offset; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1993 | if ((bd_len > 0) && (!sdebug_capacity)) |
| 1994 | sdebug_capacity = get_sdebug_capacity(); |
| 1995 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1996 | if (8 == bd_len) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1997 | if (sdebug_capacity > 0xfffffffe) |
| 1998 | put_unaligned_be32(0xffffffff, ap + 0); |
| 1999 | else |
| 2000 | put_unaligned_be32(sdebug_capacity, ap + 0); |
| 2001 | put_unaligned_be16(sdebug_sector_size, ap + 6); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2002 | offset += bd_len; |
| 2003 | ap = arr + offset; |
| 2004 | } else if (16 == bd_len) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2005 | put_unaligned_be64((u64)sdebug_capacity, ap + 0); |
| 2006 | put_unaligned_be32(sdebug_sector_size, ap + 12); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2007 | offset += bd_len; |
| 2008 | ap = arr + offset; |
| 2009 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2011 | if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) { |
| 2012 | /* TODO: Control Extension page */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2013 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | return check_condition_result; |
| 2015 | } |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 2016 | bad_pcode = false; |
| 2017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | switch (pcode) { |
| 2019 | case 0x1: /* Read-Write error recovery page, direct access */ |
| 2020 | len = resp_err_recov_pg(ap, pcontrol, target); |
| 2021 | offset += len; |
| 2022 | break; |
| 2023 | case 0x2: /* Disconnect-Reconnect page, all devices */ |
| 2024 | len = resp_disconnect_pg(ap, pcontrol, target); |
| 2025 | offset += len; |
| 2026 | break; |
| 2027 | case 0x3: /* Format device page, direct access */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 2028 | if (is_disk) { |
| 2029 | len = resp_format_pg(ap, pcontrol, target); |
| 2030 | offset += len; |
| 2031 | } else |
| 2032 | bad_pcode = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | break; |
| 2034 | case 0x8: /* Caching page, direct access */ |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 2035 | if (is_disk) { |
| 2036 | len = resp_caching_pg(ap, pcontrol, target); |
| 2037 | offset += len; |
| 2038 | } else |
| 2039 | bad_pcode = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | break; |
| 2041 | case 0xa: /* Control Mode page, all devices */ |
| 2042 | len = resp_ctrl_m_pg(ap, pcontrol, target); |
| 2043 | offset += len; |
| 2044 | break; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2045 | case 0x19: /* if spc==1 then sas phy, control+discover */ |
| 2046 | if ((subpcode > 0x2) && (subpcode < 0xff)) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2047 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2048 | return check_condition_result; |
| 2049 | } |
| 2050 | len = 0; |
| 2051 | if ((0x0 == subpcode) || (0xff == subpcode)) |
| 2052 | len += resp_sas_sf_m_pg(ap + len, pcontrol, target); |
| 2053 | if ((0x1 == subpcode) || (0xff == subpcode)) |
| 2054 | len += resp_sas_pcd_m_spg(ap + len, pcontrol, target, |
| 2055 | target_dev_id); |
| 2056 | if ((0x2 == subpcode) || (0xff == subpcode)) |
| 2057 | len += resp_sas_sha_m_spg(ap + len, pcontrol); |
| 2058 | offset += len; |
| 2059 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | case 0x1c: /* Informational Exceptions Mode page, all devices */ |
| 2061 | len = resp_iec_m_pg(ap, pcontrol, target); |
| 2062 | offset += len; |
| 2063 | break; |
| 2064 | case 0x3f: /* Read all Mode pages */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2065 | if ((0 == subpcode) || (0xff == subpcode)) { |
| 2066 | len = resp_err_recov_pg(ap, pcontrol, target); |
| 2067 | len += resp_disconnect_pg(ap + len, pcontrol, target); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 2068 | if (is_disk) { |
| 2069 | len += resp_format_pg(ap + len, pcontrol, |
| 2070 | target); |
| 2071 | len += resp_caching_pg(ap + len, pcontrol, |
| 2072 | target); |
| 2073 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2074 | len += resp_ctrl_m_pg(ap + len, pcontrol, target); |
| 2075 | len += resp_sas_sf_m_pg(ap + len, pcontrol, target); |
| 2076 | if (0xff == subpcode) { |
| 2077 | len += resp_sas_pcd_m_spg(ap + len, pcontrol, |
| 2078 | target, target_dev_id); |
| 2079 | len += resp_sas_sha_m_spg(ap + len, pcontrol); |
| 2080 | } |
| 2081 | len += resp_iec_m_pg(ap + len, pcontrol, target); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 2082 | offset += len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2083 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2084 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2085 | return check_condition_result; |
| 2086 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | break; |
| 2088 | default: |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 2089 | bad_pcode = true; |
| 2090 | break; |
| 2091 | } |
| 2092 | if (bad_pcode) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2093 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | return check_condition_result; |
| 2095 | } |
| 2096 | if (msense_6) |
| 2097 | arr[0] = offset - 1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2098 | else |
| 2099 | put_unaligned_be16((offset - 2), arr + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | return fill_from_dev_buffer(scp, arr, min(alloc_len, offset)); |
| 2101 | } |
| 2102 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2103 | #define SDEBUG_MAX_MSELECT_SZ 512 |
| 2104 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2105 | static int resp_mode_select(struct scsi_cmnd *scp, |
| 2106 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2107 | { |
| 2108 | int pf, sp, ps, md_len, bd_len, off, spf, pg_len; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2109 | int param_len, res, mpage; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2110 | unsigned char arr[SDEBUG_MAX_MSELECT_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2111 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2112 | int mselect6 = (MODE_SELECT == cmd[0]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2113 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2114 | memset(arr, 0, sizeof(arr)); |
| 2115 | pf = cmd[1] & 0x10; |
| 2116 | sp = cmd[1] & 0x1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2117 | param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2118 | if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2119 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2120 | return check_condition_result; |
| 2121 | } |
| 2122 | res = fetch_to_dev_buffer(scp, arr, param_len); |
| 2123 | if (-1 == res) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2124 | return DID_ERROR << 16; |
| 2125 | else if (sdebug_verbose && (res < param_len)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2126 | sdev_printk(KERN_INFO, scp->device, |
| 2127 | "%s: cdb indicated=%d, IO sent=%d bytes\n", |
| 2128 | __func__, param_len, res); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2129 | md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2); |
| 2130 | bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2131 | if (md_len > 2) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2132 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2133 | return check_condition_result; |
| 2134 | } |
| 2135 | off = bd_len + (mselect6 ? 4 : 8); |
| 2136 | mpage = arr[off] & 0x3f; |
| 2137 | ps = !!(arr[off] & 0x80); |
| 2138 | if (ps) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2139 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2140 | return check_condition_result; |
| 2141 | } |
| 2142 | spf = !!(arr[off] & 0x40); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2143 | pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) : |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2144 | (arr[off + 1] + 2); |
| 2145 | if ((pg_len + off) > param_len) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2146 | mk_sense_buffer(scp, ILLEGAL_REQUEST, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2147 | PARAMETER_LIST_LENGTH_ERR, 0); |
| 2148 | return check_condition_result; |
| 2149 | } |
| 2150 | switch (mpage) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2151 | case 0x8: /* Caching Mode page */ |
| 2152 | if (caching_pg[1] == arr[off + 1]) { |
| 2153 | memcpy(caching_pg + 2, arr + off + 2, |
| 2154 | sizeof(caching_pg) - 2); |
| 2155 | goto set_mode_changed_ua; |
| 2156 | } |
| 2157 | break; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2158 | case 0xa: /* Control Mode page */ |
| 2159 | if (ctrl_m_pg[1] == arr[off + 1]) { |
| 2160 | memcpy(ctrl_m_pg + 2, arr + off + 2, |
| 2161 | sizeof(ctrl_m_pg) - 2); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2162 | sdebug_dsense = !!(ctrl_m_pg[2] & 0x4); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2163 | goto set_mode_changed_ua; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2164 | } |
| 2165 | break; |
| 2166 | case 0x1c: /* Informational Exceptions Mode page */ |
| 2167 | if (iec_m_pg[1] == arr[off + 1]) { |
| 2168 | memcpy(iec_m_pg + 2, arr + off + 2, |
| 2169 | sizeof(iec_m_pg) - 2); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2170 | goto set_mode_changed_ua; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2171 | } |
| 2172 | break; |
| 2173 | default: |
| 2174 | break; |
| 2175 | } |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2176 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2177 | return check_condition_result; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2178 | set_mode_changed_ua: |
| 2179 | set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm); |
| 2180 | return 0; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | static int resp_temp_l_pg(unsigned char * arr) |
| 2184 | { |
| 2185 | unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38, |
| 2186 | 0x0, 0x1, 0x3, 0x2, 0x0, 65, |
| 2187 | }; |
| 2188 | |
| 2189 | memcpy(arr, temp_l_pg, sizeof(temp_l_pg)); |
| 2190 | return sizeof(temp_l_pg); |
| 2191 | } |
| 2192 | |
| 2193 | static int resp_ie_l_pg(unsigned char * arr) |
| 2194 | { |
| 2195 | unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38, |
| 2196 | }; |
| 2197 | |
| 2198 | memcpy(arr, ie_l_pg, sizeof(ie_l_pg)); |
| 2199 | if (iec_m_pg[2] & 0x4) { /* TEST bit set */ |
| 2200 | arr[4] = THRESHOLD_EXCEEDED; |
| 2201 | arr[5] = 0xff; |
| 2202 | } |
| 2203 | return sizeof(ie_l_pg); |
| 2204 | } |
| 2205 | |
| 2206 | #define SDEBUG_MAX_LSENSE_SZ 512 |
| 2207 | |
| 2208 | static int resp_log_sense(struct scsi_cmnd * scp, |
| 2209 | struct sdebug_dev_info * devip) |
| 2210 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2211 | int ppc, sp, pcontrol, pcode, subpcode, alloc_len, len, n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2212 | unsigned char arr[SDEBUG_MAX_LSENSE_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2213 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2214 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2215 | memset(arr, 0, sizeof(arr)); |
| 2216 | ppc = cmd[1] & 0x2; |
| 2217 | sp = cmd[1] & 0x1; |
| 2218 | if (ppc || sp) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2219 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2220 | return check_condition_result; |
| 2221 | } |
| 2222 | pcontrol = (cmd[2] & 0xc0) >> 6; |
| 2223 | pcode = cmd[2] & 0x3f; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2224 | subpcode = cmd[3] & 0xff; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2225 | alloc_len = get_unaligned_be16(cmd + 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2226 | arr[0] = pcode; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2227 | if (0 == subpcode) { |
| 2228 | switch (pcode) { |
| 2229 | case 0x0: /* Supported log pages log page */ |
| 2230 | n = 4; |
| 2231 | arr[n++] = 0x0; /* this page */ |
| 2232 | arr[n++] = 0xd; /* Temperature */ |
| 2233 | arr[n++] = 0x2f; /* Informational exceptions */ |
| 2234 | arr[3] = n - 4; |
| 2235 | break; |
| 2236 | case 0xd: /* Temperature log page */ |
| 2237 | arr[3] = resp_temp_l_pg(arr + 4); |
| 2238 | break; |
| 2239 | case 0x2f: /* Informational exceptions log page */ |
| 2240 | arr[3] = resp_ie_l_pg(arr + 4); |
| 2241 | break; |
| 2242 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2243 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2244 | return check_condition_result; |
| 2245 | } |
| 2246 | } else if (0xff == subpcode) { |
| 2247 | arr[0] |= 0x40; |
| 2248 | arr[1] = subpcode; |
| 2249 | switch (pcode) { |
| 2250 | case 0x0: /* Supported log pages and subpages log page */ |
| 2251 | n = 4; |
| 2252 | arr[n++] = 0x0; |
| 2253 | arr[n++] = 0x0; /* 0,0 page */ |
| 2254 | arr[n++] = 0x0; |
| 2255 | arr[n++] = 0xff; /* this page */ |
| 2256 | arr[n++] = 0xd; |
| 2257 | arr[n++] = 0x0; /* Temperature */ |
| 2258 | arr[n++] = 0x2f; |
| 2259 | arr[n++] = 0x0; /* Informational exceptions */ |
| 2260 | arr[3] = n - 4; |
| 2261 | break; |
| 2262 | case 0xd: /* Temperature subpages */ |
| 2263 | n = 4; |
| 2264 | arr[n++] = 0xd; |
| 2265 | arr[n++] = 0x0; /* Temperature */ |
| 2266 | arr[3] = n - 4; |
| 2267 | break; |
| 2268 | case 0x2f: /* Informational exceptions subpages */ |
| 2269 | n = 4; |
| 2270 | arr[n++] = 0x2f; |
| 2271 | arr[n++] = 0x0; /* Informational exceptions */ |
| 2272 | arr[3] = n - 4; |
| 2273 | break; |
| 2274 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2275 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2276 | return check_condition_result; |
| 2277 | } |
| 2278 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2279 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2280 | return check_condition_result; |
| 2281 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2282 | len = min(get_unaligned_be16(arr + 2) + 4, alloc_len); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2283 | return fill_from_dev_buffer(scp, arr, |
| 2284 | min(len, SDEBUG_MAX_INQ_ARR_SZ)); |
| 2285 | } |
| 2286 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2287 | static int check_device_access_params(struct scsi_cmnd *scp, |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2288 | unsigned long long lba, unsigned int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2290 | if (lba + num > sdebug_capacity) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2291 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | return check_condition_result; |
| 2293 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2294 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 2295 | if (num > sdebug_store_sectors) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2296 | /* needs work to find which cdb byte 'num' comes from */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2297 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2298 | return check_condition_result; |
| 2299 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2300 | return 0; |
| 2301 | } |
| 2302 | |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2303 | /* Returns number of bytes copied or -1 if error. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2304 | static int do_device_access(struct scsi_cmnd *scmd, u64 lba, u32 num, |
| 2305 | bool do_write) |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2306 | { |
| 2307 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2308 | u64 block, rest = 0; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2309 | struct scsi_data_buffer *sdb; |
| 2310 | enum dma_data_direction dir; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2311 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2312 | if (do_write) { |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2313 | sdb = scsi_out(scmd); |
| 2314 | dir = DMA_TO_DEVICE; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2315 | } else { |
| 2316 | sdb = scsi_in(scmd); |
| 2317 | dir = DMA_FROM_DEVICE; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | if (!sdb->length) |
| 2321 | return 0; |
| 2322 | if (!(scsi_bidi_cmnd(scmd) || scmd->sc_data_direction == dir)) |
| 2323 | return -1; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2324 | |
| 2325 | block = do_div(lba, sdebug_store_sectors); |
| 2326 | if (block + num > sdebug_store_sectors) |
| 2327 | rest = block + num - sdebug_store_sectors; |
| 2328 | |
Dave Gordon | 386ecb1 | 2015-06-30 14:58:57 -0700 | [diff] [blame] | 2329 | ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2330 | fake_storep + (block * sdebug_sector_size), |
| 2331 | (num - rest) * sdebug_sector_size, 0, do_write); |
| 2332 | if (ret != (num - rest) * sdebug_sector_size) |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2333 | return ret; |
| 2334 | |
| 2335 | if (rest) { |
Dave Gordon | 386ecb1 | 2015-06-30 14:58:57 -0700 | [diff] [blame] | 2336 | ret += sg_copy_buffer(sdb->table.sgl, sdb->table.nents, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2337 | fake_storep, rest * sdebug_sector_size, |
| 2338 | (num - rest) * sdebug_sector_size, do_write); |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2339 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2340 | |
| 2341 | return ret; |
| 2342 | } |
| 2343 | |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2344 | /* If fake_store(lba,num) compares equal to arr(num), then copy top half of |
| 2345 | * arr into fake_store(lba,num) and return true. If comparison fails then |
| 2346 | * return false. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2347 | static bool comp_write_worker(u64 lba, u32 num, const u8 *arr) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2348 | { |
| 2349 | bool res; |
| 2350 | u64 block, rest = 0; |
| 2351 | u32 store_blks = sdebug_store_sectors; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2352 | u32 lb_size = sdebug_sector_size; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2353 | |
| 2354 | block = do_div(lba, store_blks); |
| 2355 | if (block + num > store_blks) |
| 2356 | rest = block + num - store_blks; |
| 2357 | |
| 2358 | res = !memcmp(fake_storep + (block * lb_size), arr, |
| 2359 | (num - rest) * lb_size); |
| 2360 | if (!res) |
| 2361 | return res; |
| 2362 | if (rest) |
| 2363 | res = memcmp(fake_storep, arr + ((num - rest) * lb_size), |
| 2364 | rest * lb_size); |
| 2365 | if (!res) |
| 2366 | return res; |
| 2367 | arr += num * lb_size; |
| 2368 | memcpy(fake_storep + (block * lb_size), arr, (num - rest) * lb_size); |
| 2369 | if (rest) |
| 2370 | memcpy(fake_storep, arr + ((num - rest) * lb_size), |
| 2371 | rest * lb_size); |
| 2372 | return res; |
| 2373 | } |
| 2374 | |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2375 | static __be16 dif_compute_csum(const void *buf, int len) |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2376 | { |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2377 | __be16 csum; |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2378 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2379 | if (sdebug_guard) |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2380 | csum = (__force __be16)ip_compute_csum(buf, len); |
| 2381 | else |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2382 | csum = cpu_to_be16(crc_t10dif(buf, len)); |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2383 | |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2384 | return csum; |
| 2385 | } |
| 2386 | |
| 2387 | static int dif_verify(struct sd_dif_tuple *sdt, const void *data, |
| 2388 | sector_t sector, u32 ei_lba) |
| 2389 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2390 | __be16 csum = dif_compute_csum(data, sdebug_sector_size); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2391 | |
| 2392 | if (sdt->guard_tag != csum) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2393 | 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] | 2394 | (unsigned long)sector, |
| 2395 | be16_to_cpu(sdt->guard_tag), |
| 2396 | be16_to_cpu(csum)); |
| 2397 | return 0x01; |
| 2398 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2399 | if (sdebug_dif == SD_DIF_TYPE1_PROTECTION && |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2400 | be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2401 | pr_err("REF check failed on sector %lu\n", |
| 2402 | (unsigned long)sector); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2403 | return 0x03; |
| 2404 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2405 | if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2406 | be32_to_cpu(sdt->ref_tag) != ei_lba) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2407 | pr_err("REF check failed on sector %lu\n", |
| 2408 | (unsigned long)sector); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2409 | return 0x03; |
| 2410 | } |
| 2411 | return 0; |
| 2412 | } |
| 2413 | |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2414 | static void dif_copy_prot(struct scsi_cmnd *SCpnt, sector_t sector, |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2415 | unsigned int sectors, bool read) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2416 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2417 | size_t resid; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2418 | void *paddr; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2419 | const void *dif_store_end = dif_storep + sdebug_store_sectors; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2420 | struct sg_mapping_iter miter; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2421 | |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 2422 | /* Bytes of protection data to copy into sgl */ |
| 2423 | resid = sectors * sizeof(*dif_storep); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2424 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2425 | sg_miter_start(&miter, scsi_prot_sglist(SCpnt), |
| 2426 | scsi_prot_sg_count(SCpnt), SG_MITER_ATOMIC | |
| 2427 | (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG)); |
| 2428 | |
| 2429 | while (sg_miter_next(&miter) && resid > 0) { |
| 2430 | size_t len = min(miter.length, resid); |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2431 | void *start = dif_store(sector); |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2432 | size_t rest = 0; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2433 | |
| 2434 | if (dif_store_end < start + len) |
| 2435 | rest = start + len - dif_store_end; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2436 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2437 | paddr = miter.addr; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2438 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2439 | if (read) |
| 2440 | memcpy(paddr, start, len - rest); |
| 2441 | else |
| 2442 | memcpy(start, paddr, len - rest); |
| 2443 | |
| 2444 | if (rest) { |
| 2445 | if (read) |
| 2446 | memcpy(paddr + len - rest, dif_storep, rest); |
| 2447 | else |
| 2448 | memcpy(dif_storep, paddr + len - rest, rest); |
| 2449 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2450 | |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 2451 | sector += len / sizeof(*dif_storep); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2452 | resid -= len; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2453 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2454 | sg_miter_stop(&miter); |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2455 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2456 | |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2457 | static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec, |
| 2458 | unsigned int sectors, u32 ei_lba) |
| 2459 | { |
| 2460 | unsigned int i; |
| 2461 | struct sd_dif_tuple *sdt; |
| 2462 | sector_t sector; |
| 2463 | |
Akinobu Mita | c45eabec | 2014-02-26 22:56:58 +0900 | [diff] [blame] | 2464 | for (i = 0; i < sectors; i++, ei_lba++) { |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2465 | int ret; |
| 2466 | |
| 2467 | sector = start_sec + i; |
| 2468 | sdt = dif_store(sector); |
| 2469 | |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2470 | if (sdt->app_tag == cpu_to_be16(0xffff)) |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2471 | continue; |
| 2472 | |
| 2473 | ret = dif_verify(sdt, fake_store(sector), sector, ei_lba); |
| 2474 | if (ret) { |
| 2475 | dif_errors++; |
| 2476 | return ret; |
| 2477 | } |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2478 | } |
| 2479 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2480 | dif_copy_prot(SCpnt, start_sec, sectors, true); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2481 | dix_reads++; |
| 2482 | |
| 2483 | return 0; |
| 2484 | } |
| 2485 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2486 | 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] | 2487 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2488 | u8 *cmd = scp->cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2489 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2490 | u64 lba; |
| 2491 | u32 num; |
| 2492 | u32 ei_lba; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2493 | unsigned long iflags; |
| 2494 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2495 | bool check_prot; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2496 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2497 | switch (cmd[0]) { |
| 2498 | case READ_16: |
| 2499 | ei_lba = 0; |
| 2500 | lba = get_unaligned_be64(cmd + 2); |
| 2501 | num = get_unaligned_be32(cmd + 10); |
| 2502 | check_prot = true; |
| 2503 | break; |
| 2504 | case READ_10: |
| 2505 | ei_lba = 0; |
| 2506 | lba = get_unaligned_be32(cmd + 2); |
| 2507 | num = get_unaligned_be16(cmd + 7); |
| 2508 | check_prot = true; |
| 2509 | break; |
| 2510 | case READ_6: |
| 2511 | ei_lba = 0; |
| 2512 | lba = (u32)cmd[3] | (u32)cmd[2] << 8 | |
| 2513 | (u32)(cmd[1] & 0x1f) << 16; |
| 2514 | num = (0 == cmd[4]) ? 256 : cmd[4]; |
| 2515 | check_prot = true; |
| 2516 | break; |
| 2517 | case READ_12: |
| 2518 | ei_lba = 0; |
| 2519 | lba = get_unaligned_be32(cmd + 2); |
| 2520 | num = get_unaligned_be32(cmd + 6); |
| 2521 | check_prot = true; |
| 2522 | break; |
| 2523 | case XDWRITEREAD_10: |
| 2524 | ei_lba = 0; |
| 2525 | lba = get_unaligned_be32(cmd + 2); |
| 2526 | num = get_unaligned_be16(cmd + 7); |
| 2527 | check_prot = false; |
| 2528 | break; |
| 2529 | default: /* assume READ(32) */ |
| 2530 | lba = get_unaligned_be64(cmd + 12); |
| 2531 | ei_lba = get_unaligned_be32(cmd + 20); |
| 2532 | num = get_unaligned_be32(cmd + 28); |
| 2533 | check_prot = false; |
| 2534 | break; |
| 2535 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2536 | if (unlikely(have_dif_prot && check_prot)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2537 | if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2538 | (cmd[1] & 0xe0)) { |
| 2539 | mk_sense_invalid_opcode(scp); |
| 2540 | return check_condition_result; |
| 2541 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2542 | if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION || |
| 2543 | sdebug_dif == SD_DIF_TYPE3_PROTECTION) && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2544 | (cmd[1] & 0xe0) == 0) |
| 2545 | sdev_printk(KERN_ERR, scp->device, "Unprotected RD " |
| 2546 | "to DIF device\n"); |
| 2547 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2548 | if (unlikely(sdebug_any_injecting_opt)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2549 | sqcp = (struct sdebug_queued_cmd *)scp->host_scribble; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2550 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2551 | if (sqcp) { |
| 2552 | if (sqcp->inj_short) |
| 2553 | num /= 2; |
| 2554 | } |
| 2555 | } else |
| 2556 | sqcp = NULL; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2557 | |
| 2558 | /* inline check_device_access_params() */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2559 | if (unlikely(lba + num > sdebug_capacity)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2560 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 2561 | return check_condition_result; |
| 2562 | } |
| 2563 | /* transfer length excessive (tie in to block limits VPD page) */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2564 | if (unlikely(num > sdebug_store_sectors)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2565 | /* needs work to find which cdb byte 'num' comes from */ |
| 2566 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 2567 | return check_condition_result; |
| 2568 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2569 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2570 | if (unlikely((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) && |
| 2571 | (lba <= (OPT_MEDIUM_ERR_ADDR + OPT_MEDIUM_ERR_NUM - 1)) && |
| 2572 | ((lba + num) > OPT_MEDIUM_ERR_ADDR))) { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2573 | /* claim unrecoverable read error */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2574 | mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2575 | /* set info field and valid bit for fixed descriptor */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2576 | if (0x70 == (scp->sense_buffer[0] & 0x7f)) { |
| 2577 | scp->sense_buffer[0] |= 0x80; /* Valid bit */ |
Douglas Gilbert | 32f7ef7 | 2011-03-11 10:43:35 -0500 | [diff] [blame] | 2578 | ret = (lba < OPT_MEDIUM_ERR_ADDR) |
| 2579 | ? OPT_MEDIUM_ERR_ADDR : (int)lba; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2580 | put_unaligned_be32(ret, scp->sense_buffer + 3); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2581 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2582 | scsi_set_resid(scp, scsi_bufflen(scp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | return check_condition_result; |
| 2584 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2585 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2586 | read_lock_irqsave(&atomic_rw, iflags); |
| 2587 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2588 | /* DIX + T10 DIF */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2589 | if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2590 | int prot_ret = prot_verify_read(scp, lba, num, ei_lba); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2591 | |
| 2592 | if (prot_ret) { |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2593 | read_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2594 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2595 | return illegal_condition_result; |
| 2596 | } |
| 2597 | } |
| 2598 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2599 | ret = do_device_access(scp, lba, num, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | read_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2601 | if (unlikely(ret == -1)) |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2602 | return DID_ERROR << 16; |
| 2603 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2604 | scsi_in(scp)->resid = scsi_bufflen(scp) - ret; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2605 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2606 | if (unlikely(sqcp)) { |
| 2607 | if (sqcp->inj_recovered) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2608 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 2609 | THRESHOLD_EXCEEDED, 0); |
| 2610 | return check_condition_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2611 | } else if (sqcp->inj_transport) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2612 | mk_sense_buffer(scp, ABORTED_COMMAND, |
| 2613 | TRANSPORT_PROBLEM, ACK_NAK_TO); |
| 2614 | return check_condition_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2615 | } else if (sqcp->inj_dif) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2616 | /* Logical block guard check failed */ |
| 2617 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); |
| 2618 | return illegal_condition_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2619 | } else if (sqcp->inj_dix) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2620 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); |
| 2621 | return illegal_condition_result; |
| 2622 | } |
| 2623 | } |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2624 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | } |
| 2626 | |
Tomas Winkler | 58a8635 | 2015-07-28 16:54:23 +0300 | [diff] [blame] | 2627 | static void dump_sector(unsigned char *buf, int len) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2628 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2629 | int i, j, n; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2630 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2631 | pr_err(">>> Sector Dump <<<\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2632 | for (i = 0 ; i < len ; i += 16) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2633 | char b[128]; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2634 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2635 | for (j = 0, n = 0; j < 16; j++) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2636 | unsigned char c = buf[i+j]; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2637 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2638 | if (c >= 0x20 && c < 0x7e) |
| 2639 | n += scnprintf(b + n, sizeof(b) - n, |
| 2640 | " %c ", buf[i+j]); |
| 2641 | else |
| 2642 | n += scnprintf(b + n, sizeof(b) - n, |
| 2643 | "%02x ", buf[i+j]); |
| 2644 | } |
| 2645 | pr_err("%04d: %s\n", i, b); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | 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] | 2650 | unsigned int sectors, u32 ei_lba) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2651 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2652 | int ret; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2653 | struct sd_dif_tuple *sdt; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2654 | void *daddr; |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2655 | sector_t sector = start_sec; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2656 | int ppage_offset; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2657 | int dpage_offset; |
| 2658 | struct sg_mapping_iter diter; |
| 2659 | struct sg_mapping_iter piter; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2660 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2661 | BUG_ON(scsi_sg_count(SCpnt) == 0); |
| 2662 | BUG_ON(scsi_prot_sg_count(SCpnt) == 0); |
| 2663 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2664 | sg_miter_start(&piter, scsi_prot_sglist(SCpnt), |
| 2665 | scsi_prot_sg_count(SCpnt), |
| 2666 | SG_MITER_ATOMIC | SG_MITER_FROM_SG); |
| 2667 | sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt), |
| 2668 | SG_MITER_ATOMIC | SG_MITER_FROM_SG); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2669 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2670 | /* For each protection page */ |
| 2671 | while (sg_miter_next(&piter)) { |
| 2672 | dpage_offset = 0; |
| 2673 | if (WARN_ON(!sg_miter_next(&diter))) { |
| 2674 | ret = 0x01; |
| 2675 | goto out; |
| 2676 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2677 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2678 | for (ppage_offset = 0; ppage_offset < piter.length; |
| 2679 | ppage_offset += sizeof(struct sd_dif_tuple)) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2680 | /* If we're at the end of the current |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2681 | * data page advance to the next one |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2682 | */ |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2683 | if (dpage_offset >= diter.length) { |
| 2684 | if (WARN_ON(!sg_miter_next(&diter))) { |
| 2685 | ret = 0x01; |
| 2686 | goto out; |
| 2687 | } |
| 2688 | dpage_offset = 0; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2689 | } |
| 2690 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2691 | sdt = piter.addr + ppage_offset; |
| 2692 | daddr = diter.addr + dpage_offset; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2693 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2694 | ret = dif_verify(sdt, daddr, sector, ei_lba); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2695 | if (ret) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2696 | dump_sector(daddr, sdebug_sector_size); |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 2697 | goto out; |
| 2698 | } |
| 2699 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2700 | sector++; |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 2701 | ei_lba++; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2702 | dpage_offset += sdebug_sector_size; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2703 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2704 | diter.consumed = dpage_offset; |
| 2705 | sg_miter_stop(&diter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2706 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2707 | sg_miter_stop(&piter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2708 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2709 | dif_copy_prot(SCpnt, start_sec, sectors, false); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2710 | dix_writes++; |
| 2711 | |
| 2712 | return 0; |
| 2713 | |
| 2714 | out: |
| 2715 | dif_errors++; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2716 | sg_miter_stop(&diter); |
| 2717 | sg_miter_stop(&piter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2718 | return ret; |
| 2719 | } |
| 2720 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2721 | static unsigned long lba_to_map_index(sector_t lba) |
| 2722 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2723 | if (sdebug_unmap_alignment) |
| 2724 | lba += sdebug_unmap_granularity - sdebug_unmap_alignment; |
| 2725 | sector_div(lba, sdebug_unmap_granularity); |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2726 | return lba; |
| 2727 | } |
| 2728 | |
| 2729 | static sector_t map_index_to_lba(unsigned long index) |
| 2730 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2731 | sector_t lba = index * sdebug_unmap_granularity; |
Akinobu Mita | a027b5b | 2013-08-26 22:08:41 +0900 | [diff] [blame] | 2732 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2733 | if (sdebug_unmap_alignment) |
| 2734 | lba -= sdebug_unmap_granularity - sdebug_unmap_alignment; |
Akinobu Mita | a027b5b | 2013-08-26 22:08:41 +0900 | [diff] [blame] | 2735 | return lba; |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2736 | } |
| 2737 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2738 | static unsigned int map_state(sector_t lba, unsigned int *num) |
| 2739 | { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2740 | sector_t end; |
| 2741 | unsigned int mapped; |
| 2742 | unsigned long index; |
| 2743 | unsigned long next; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2744 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2745 | index = lba_to_map_index(lba); |
| 2746 | mapped = test_bit(index, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2747 | |
| 2748 | if (mapped) |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2749 | next = find_next_zero_bit(map_storep, map_size, index); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2750 | else |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2751 | next = find_next_bit(map_storep, map_size, index); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2752 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2753 | 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] | 2754 | *num = end - lba; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2755 | return mapped; |
| 2756 | } |
| 2757 | |
| 2758 | static void map_region(sector_t lba, unsigned int len) |
| 2759 | { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2760 | sector_t end = lba + len; |
| 2761 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2762 | while (lba < end) { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2763 | unsigned long index = lba_to_map_index(lba); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2764 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2765 | if (index < map_size) |
| 2766 | set_bit(index, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2767 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2768 | lba = map_index_to_lba(index + 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | static void unmap_region(sector_t lba, unsigned int len) |
| 2773 | { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2774 | sector_t end = lba + len; |
| 2775 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2776 | while (lba < end) { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2777 | unsigned long index = lba_to_map_index(lba); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2778 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2779 | if (lba == map_index_to_lba(index) && |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2780 | lba + sdebug_unmap_granularity <= end && |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2781 | index < map_size) { |
| 2782 | clear_bit(index, map_storep); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 2783 | if (sdebug_lbprz) { /* for LBPRZ=2 return 0xff_s */ |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 2784 | memset(fake_storep + |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 2785 | lba * sdebug_sector_size, |
| 2786 | (sdebug_lbprz & 1) ? 0 : 0xff, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2787 | sdebug_sector_size * |
| 2788 | sdebug_unmap_granularity); |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2789 | } |
Akinobu Mita | e9926b4 | 2013-06-29 17:59:17 +0900 | [diff] [blame] | 2790 | if (dif_storep) { |
| 2791 | memset(dif_storep + lba, 0xff, |
| 2792 | sizeof(*dif_storep) * |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2793 | sdebug_unmap_granularity); |
Akinobu Mita | e9926b4 | 2013-06-29 17:59:17 +0900 | [diff] [blame] | 2794 | } |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 2795 | } |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2796 | lba = map_index_to_lba(index + 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2797 | } |
| 2798 | } |
| 2799 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2800 | 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] | 2801 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2802 | u8 *cmd = scp->cmnd; |
| 2803 | u64 lba; |
| 2804 | u32 num; |
| 2805 | u32 ei_lba; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | unsigned long iflags; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2807 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2808 | bool check_prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2810 | switch (cmd[0]) { |
| 2811 | case WRITE_16: |
| 2812 | ei_lba = 0; |
| 2813 | lba = get_unaligned_be64(cmd + 2); |
| 2814 | num = get_unaligned_be32(cmd + 10); |
| 2815 | check_prot = true; |
| 2816 | break; |
| 2817 | case WRITE_10: |
| 2818 | ei_lba = 0; |
| 2819 | lba = get_unaligned_be32(cmd + 2); |
| 2820 | num = get_unaligned_be16(cmd + 7); |
| 2821 | check_prot = true; |
| 2822 | break; |
| 2823 | case WRITE_6: |
| 2824 | ei_lba = 0; |
| 2825 | lba = (u32)cmd[3] | (u32)cmd[2] << 8 | |
| 2826 | (u32)(cmd[1] & 0x1f) << 16; |
| 2827 | num = (0 == cmd[4]) ? 256 : cmd[4]; |
| 2828 | check_prot = true; |
| 2829 | break; |
| 2830 | case WRITE_12: |
| 2831 | ei_lba = 0; |
| 2832 | lba = get_unaligned_be32(cmd + 2); |
| 2833 | num = get_unaligned_be32(cmd + 6); |
| 2834 | check_prot = true; |
| 2835 | break; |
| 2836 | case 0x53: /* XDWRITEREAD(10) */ |
| 2837 | ei_lba = 0; |
| 2838 | lba = get_unaligned_be32(cmd + 2); |
| 2839 | num = get_unaligned_be16(cmd + 7); |
| 2840 | check_prot = false; |
| 2841 | break; |
| 2842 | default: /* assume WRITE(32) */ |
| 2843 | lba = get_unaligned_be64(cmd + 12); |
| 2844 | ei_lba = get_unaligned_be32(cmd + 20); |
| 2845 | num = get_unaligned_be32(cmd + 28); |
| 2846 | check_prot = false; |
| 2847 | break; |
| 2848 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2849 | if (unlikely(have_dif_prot && check_prot)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2850 | if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2851 | (cmd[1] & 0xe0)) { |
| 2852 | mk_sense_invalid_opcode(scp); |
| 2853 | return check_condition_result; |
| 2854 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2855 | if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION || |
| 2856 | sdebug_dif == SD_DIF_TYPE3_PROTECTION) && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2857 | (cmd[1] & 0xe0) == 0) |
| 2858 | sdev_printk(KERN_ERR, scp->device, "Unprotected WR " |
| 2859 | "to DIF device\n"); |
| 2860 | } |
| 2861 | |
| 2862 | /* inline check_device_access_params() */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2863 | if (unlikely(lba + num > sdebug_capacity)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2864 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 2865 | return check_condition_result; |
| 2866 | } |
| 2867 | /* transfer length excessive (tie in to block limits VPD page) */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2868 | if (unlikely(num > sdebug_store_sectors)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2869 | /* needs work to find which cdb byte 'num' comes from */ |
| 2870 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 2871 | return check_condition_result; |
| 2872 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2874 | write_lock_irqsave(&atomic_rw, iflags); |
| 2875 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2876 | /* DIX + T10 DIF */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2877 | if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2878 | int prot_ret = prot_verify_write(scp, lba, num, ei_lba); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2879 | |
| 2880 | if (prot_ret) { |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2881 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2882 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2883 | return illegal_condition_result; |
| 2884 | } |
| 2885 | } |
| 2886 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2887 | ret = do_device_access(scp, lba, num, true); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2888 | if (unlikely(scsi_debug_lbp())) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2889 | map_region(lba, num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2891 | if (unlikely(-1 == ret)) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2892 | return DID_ERROR << 16; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2893 | else if (unlikely(sdebug_verbose && |
| 2894 | (ret < (num * sdebug_sector_size)))) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2895 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2896 | "%s: write: cdb indicated=%u, IO sent=%d bytes\n", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2897 | my_name, num * sdebug_sector_size, ret); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2898 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2899 | if (unlikely(sdebug_any_injecting_opt)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2900 | struct sdebug_queued_cmd *sqcp = |
| 2901 | (struct sdebug_queued_cmd *)scp->host_scribble; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2902 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 2903 | if (sqcp) { |
| 2904 | if (sqcp->inj_recovered) { |
| 2905 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 2906 | THRESHOLD_EXCEEDED, 0); |
| 2907 | return check_condition_result; |
| 2908 | } else if (sqcp->inj_dif) { |
| 2909 | /* Logical block guard check failed */ |
| 2910 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); |
| 2911 | return illegal_condition_result; |
| 2912 | } else if (sqcp->inj_dix) { |
| 2913 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); |
| 2914 | return illegal_condition_result; |
| 2915 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2916 | } |
| 2917 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | return 0; |
| 2919 | } |
| 2920 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2921 | static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num, |
| 2922 | u32 ei_lba, bool unmap, bool ndob) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2923 | { |
| 2924 | unsigned long iflags; |
| 2925 | unsigned long long i; |
| 2926 | int ret; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2927 | u64 lba_off; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2928 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2929 | ret = check_device_access_params(scp, lba, num); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2930 | if (ret) |
| 2931 | return ret; |
| 2932 | |
| 2933 | write_lock_irqsave(&atomic_rw, iflags); |
| 2934 | |
Akinobu Mita | 9ed8d3d | 2013-04-16 22:11:55 +0900 | [diff] [blame] | 2935 | if (unmap && scsi_debug_lbp()) { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2936 | unmap_region(lba, num); |
| 2937 | goto out; |
| 2938 | } |
| 2939 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2940 | lba_off = lba * sdebug_sector_size; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2941 | /* if ndob then zero 1 logical block, else fetch 1 logical block */ |
| 2942 | if (ndob) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2943 | memset(fake_storep + lba_off, 0, sdebug_sector_size); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2944 | ret = 0; |
| 2945 | } else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2946 | ret = fetch_to_dev_buffer(scp, fake_storep + lba_off, |
| 2947 | sdebug_sector_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2948 | |
| 2949 | if (-1 == ret) { |
| 2950 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2951 | return DID_ERROR << 16; |
| 2952 | } else if (sdebug_verbose && (ret < (num * sdebug_sector_size))) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2953 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2954 | "%s: %s: cdb indicated=%u, IO sent=%d bytes\n", |
| 2955 | my_name, "write same", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2956 | num * sdebug_sector_size, ret); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2957 | |
| 2958 | /* Copy first sector to remaining blocks */ |
| 2959 | for (i = 1 ; i < num ; i++) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2960 | memcpy(fake_storep + ((lba + i) * sdebug_sector_size), |
| 2961 | fake_storep + lba_off, |
| 2962 | sdebug_sector_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2963 | |
Akinobu Mita | 9ed8d3d | 2013-04-16 22:11:55 +0900 | [diff] [blame] | 2964 | if (scsi_debug_lbp()) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2965 | map_region(lba, num); |
| 2966 | out: |
| 2967 | write_unlock_irqrestore(&atomic_rw, iflags); |
| 2968 | |
| 2969 | return 0; |
| 2970 | } |
| 2971 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2972 | static int resp_write_same_10(struct scsi_cmnd *scp, |
| 2973 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2974 | { |
| 2975 | u8 *cmd = scp->cmnd; |
| 2976 | u32 lba; |
| 2977 | u16 num; |
| 2978 | u32 ei_lba = 0; |
| 2979 | bool unmap = false; |
| 2980 | |
| 2981 | if (cmd[1] & 0x8) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2982 | if (sdebug_lbpws10 == 0) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2983 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); |
| 2984 | return check_condition_result; |
| 2985 | } else |
| 2986 | unmap = true; |
| 2987 | } |
| 2988 | lba = get_unaligned_be32(cmd + 2); |
| 2989 | num = get_unaligned_be16(cmd + 7); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2990 | if (num > sdebug_write_same_length) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2991 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); |
| 2992 | return check_condition_result; |
| 2993 | } |
| 2994 | return resp_write_same(scp, lba, num, ei_lba, unmap, false); |
| 2995 | } |
| 2996 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2997 | static int resp_write_same_16(struct scsi_cmnd *scp, |
| 2998 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2999 | { |
| 3000 | u8 *cmd = scp->cmnd; |
| 3001 | u64 lba; |
| 3002 | u32 num; |
| 3003 | u32 ei_lba = 0; |
| 3004 | bool unmap = false; |
| 3005 | bool ndob = false; |
| 3006 | |
| 3007 | if (cmd[1] & 0x8) { /* UNMAP */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3008 | if (sdebug_lbpws == 0) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3009 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); |
| 3010 | return check_condition_result; |
| 3011 | } else |
| 3012 | unmap = true; |
| 3013 | } |
| 3014 | if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */ |
| 3015 | ndob = true; |
| 3016 | lba = get_unaligned_be64(cmd + 2); |
| 3017 | num = get_unaligned_be32(cmd + 10); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3018 | if (num > sdebug_write_same_length) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3019 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1); |
| 3020 | return check_condition_result; |
| 3021 | } |
| 3022 | return resp_write_same(scp, lba, num, ei_lba, unmap, ndob); |
| 3023 | } |
| 3024 | |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 3025 | /* Note the mode field is in the same position as the (lower) service action |
| 3026 | * field. For the Report supported operation codes command, SPC-4 suggests |
| 3027 | * each mode of this command should be reported separately; for future. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3028 | static int resp_write_buffer(struct scsi_cmnd *scp, |
| 3029 | struct sdebug_dev_info *devip) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 3030 | { |
| 3031 | u8 *cmd = scp->cmnd; |
| 3032 | struct scsi_device *sdp = scp->device; |
| 3033 | struct sdebug_dev_info *dp; |
| 3034 | u8 mode; |
| 3035 | |
| 3036 | mode = cmd[1] & 0x1f; |
| 3037 | switch (mode) { |
| 3038 | case 0x4: /* download microcode (MC) and activate (ACT) */ |
| 3039 | /* set UAs on this device only */ |
| 3040 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3041 | set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm); |
| 3042 | break; |
| 3043 | case 0x5: /* download MC, save and ACT */ |
| 3044 | set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm); |
| 3045 | break; |
| 3046 | case 0x6: /* download MC with offsets and ACT */ |
| 3047 | /* set UAs on most devices (LUs) in this target */ |
| 3048 | list_for_each_entry(dp, |
| 3049 | &devip->sdbg_host->dev_info_list, |
| 3050 | dev_list) |
| 3051 | if (dp->target == sdp->id) { |
| 3052 | set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm); |
| 3053 | if (devip != dp) |
| 3054 | set_bit(SDEBUG_UA_MICROCODE_CHANGED, |
| 3055 | dp->uas_bm); |
| 3056 | } |
| 3057 | break; |
| 3058 | case 0x7: /* download MC with offsets, save, and ACT */ |
| 3059 | /* set UA on all devices (LUs) in this target */ |
| 3060 | list_for_each_entry(dp, |
| 3061 | &devip->sdbg_host->dev_info_list, |
| 3062 | dev_list) |
| 3063 | if (dp->target == sdp->id) |
| 3064 | set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, |
| 3065 | dp->uas_bm); |
| 3066 | break; |
| 3067 | default: |
| 3068 | /* do nothing for this command for other mode values */ |
| 3069 | break; |
| 3070 | } |
| 3071 | return 0; |
| 3072 | } |
| 3073 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3074 | static int resp_comp_write(struct scsi_cmnd *scp, |
| 3075 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3076 | { |
| 3077 | u8 *cmd = scp->cmnd; |
| 3078 | u8 *arr; |
| 3079 | u8 *fake_storep_hold; |
| 3080 | u64 lba; |
| 3081 | u32 dnum; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3082 | u32 lb_size = sdebug_sector_size; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3083 | u8 num; |
| 3084 | unsigned long iflags; |
| 3085 | int ret; |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3086 | int retval = 0; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3087 | |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3088 | lba = get_unaligned_be64(cmd + 2); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3089 | num = cmd[13]; /* 1 to a maximum of 255 logical blocks */ |
| 3090 | if (0 == num) |
| 3091 | return 0; /* degenerate case, not an error */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3092 | if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3093 | (cmd[1] & 0xe0)) { |
| 3094 | mk_sense_invalid_opcode(scp); |
| 3095 | return check_condition_result; |
| 3096 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3097 | if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION || |
| 3098 | sdebug_dif == SD_DIF_TYPE3_PROTECTION) && |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3099 | (cmd[1] & 0xe0) == 0) |
| 3100 | sdev_printk(KERN_ERR, scp->device, "Unprotected WR " |
| 3101 | "to DIF device\n"); |
| 3102 | |
| 3103 | /* inline check_device_access_params() */ |
| 3104 | if (lba + num > sdebug_capacity) { |
| 3105 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 3106 | return check_condition_result; |
| 3107 | } |
| 3108 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 3109 | if (num > sdebug_store_sectors) { |
| 3110 | /* needs work to find which cdb byte 'num' comes from */ |
| 3111 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 3112 | return check_condition_result; |
| 3113 | } |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3114 | dnum = 2 * num; |
| 3115 | arr = kzalloc(dnum * lb_size, GFP_ATOMIC); |
| 3116 | if (NULL == arr) { |
| 3117 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3118 | INSUFF_RES_ASCQ); |
| 3119 | return check_condition_result; |
| 3120 | } |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3121 | |
| 3122 | write_lock_irqsave(&atomic_rw, iflags); |
| 3123 | |
| 3124 | /* trick do_device_access() to fetch both compare and write buffers |
| 3125 | * from data-in into arr. Safe (atomic) since write_lock held. */ |
| 3126 | fake_storep_hold = fake_storep; |
| 3127 | fake_storep = arr; |
| 3128 | ret = do_device_access(scp, 0, dnum, true); |
| 3129 | fake_storep = fake_storep_hold; |
| 3130 | if (ret == -1) { |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3131 | retval = DID_ERROR << 16; |
| 3132 | goto cleanup; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3133 | } else if (sdebug_verbose && (ret < (dnum * lb_size))) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3134 | sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb " |
| 3135 | "indicated=%u, IO sent=%d bytes\n", my_name, |
| 3136 | dnum * lb_size, ret); |
| 3137 | if (!comp_write_worker(lba, num, arr)) { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3138 | mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3139 | retval = check_condition_result; |
| 3140 | goto cleanup; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3141 | } |
| 3142 | if (scsi_debug_lbp()) |
| 3143 | map_region(lba, num); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3144 | cleanup: |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3145 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3146 | kfree(arr); |
| 3147 | return retval; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3148 | } |
| 3149 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3150 | struct unmap_block_desc { |
| 3151 | __be64 lba; |
| 3152 | __be32 blocks; |
| 3153 | __be32 __reserved; |
| 3154 | }; |
| 3155 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3156 | 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] | 3157 | { |
| 3158 | unsigned char *buf; |
| 3159 | struct unmap_block_desc *desc; |
| 3160 | unsigned int i, payload_len, descriptors; |
| 3161 | int ret; |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3162 | unsigned long iflags; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3163 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3164 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3165 | if (!scsi_debug_lbp()) |
| 3166 | return 0; /* fib and say its done */ |
| 3167 | payload_len = get_unaligned_be16(scp->cmnd + 7); |
| 3168 | BUG_ON(scsi_bufflen(scp) != payload_len); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3169 | |
| 3170 | descriptors = (payload_len - 8) / 16; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3171 | if (descriptors > sdebug_unmap_max_desc) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3172 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3173 | return check_condition_result; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3174 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3175 | |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 3176 | buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3177 | if (!buf) { |
| 3178 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3179 | INSUFF_RES_ASCQ); |
| 3180 | return check_condition_result; |
| 3181 | } |
| 3182 | |
| 3183 | scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp)); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3184 | |
| 3185 | BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2); |
| 3186 | BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16); |
| 3187 | |
| 3188 | desc = (void *)&buf[8]; |
| 3189 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3190 | write_lock_irqsave(&atomic_rw, iflags); |
| 3191 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3192 | for (i = 0 ; i < descriptors ; i++) { |
| 3193 | unsigned long long lba = get_unaligned_be64(&desc[i].lba); |
| 3194 | unsigned int num = get_unaligned_be32(&desc[i].blocks); |
| 3195 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3196 | ret = check_device_access_params(scp, lba, num); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3197 | if (ret) |
| 3198 | goto out; |
| 3199 | |
| 3200 | unmap_region(lba, num); |
| 3201 | } |
| 3202 | |
| 3203 | ret = 0; |
| 3204 | |
| 3205 | out: |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3206 | write_unlock_irqrestore(&atomic_rw, iflags); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3207 | kfree(buf); |
| 3208 | |
| 3209 | return ret; |
| 3210 | } |
| 3211 | |
| 3212 | #define SDEBUG_GET_LBA_STATUS_LEN 32 |
| 3213 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3214 | static int resp_get_lba_status(struct scsi_cmnd *scp, |
| 3215 | struct sdebug_dev_info *devip) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3216 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3217 | u8 *cmd = scp->cmnd; |
| 3218 | u64 lba; |
| 3219 | u32 alloc_len, mapped, num; |
| 3220 | u8 arr[SDEBUG_GET_LBA_STATUS_LEN]; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3221 | int ret; |
| 3222 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3223 | lba = get_unaligned_be64(cmd + 2); |
| 3224 | alloc_len = get_unaligned_be32(cmd + 10); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3225 | |
| 3226 | if (alloc_len < 24) |
| 3227 | return 0; |
| 3228 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3229 | ret = check_device_access_params(scp, lba, 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3230 | if (ret) |
| 3231 | return ret; |
| 3232 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3233 | if (scsi_debug_lbp()) |
| 3234 | mapped = map_state(lba, &num); |
| 3235 | else { |
| 3236 | mapped = 1; |
| 3237 | /* following just in case virtual_gb changed */ |
| 3238 | sdebug_capacity = get_sdebug_capacity(); |
| 3239 | if (sdebug_capacity - lba <= 0xffffffff) |
| 3240 | num = sdebug_capacity - lba; |
| 3241 | else |
| 3242 | num = 0xffffffff; |
| 3243 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3244 | |
| 3245 | memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3246 | put_unaligned_be32(20, arr); /* Parameter Data Length */ |
| 3247 | put_unaligned_be64(lba, arr + 8); /* LBA */ |
| 3248 | put_unaligned_be32(num, arr + 16); /* Number of blocks */ |
| 3249 | arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3250 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3251 | 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] | 3252 | } |
| 3253 | |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3254 | /* Even though each pseudo target has a REPORT LUNS "well known logical unit" |
| 3255 | * (W-LUN), the normal Linux scanning logic does not associate it with a |
| 3256 | * device (e.g. /dev/sg7). The following magic will make that association: |
| 3257 | * "cd /sys/class/scsi_host/host<n> ; echo '- - 49409' > scan" |
| 3258 | * where <n> is a host number. If there are multiple targets in a host then |
| 3259 | * the above will associate a W-LUN to each target. To only get a W-LUN |
| 3260 | * for target 2, then use "echo '- 2 49409' > scan" . |
| 3261 | */ |
| 3262 | static int resp_report_luns(struct scsi_cmnd *scp, |
| 3263 | struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 3265 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3266 | unsigned int alloc_len; |
| 3267 | unsigned char select_report; |
| 3268 | u64 lun; |
| 3269 | struct scsi_lun *lun_p; |
| 3270 | u8 *arr; |
| 3271 | unsigned int lun_cnt; /* normal LUN count (max: 256) */ |
| 3272 | unsigned int wlun_cnt; /* report luns W-LUN count */ |
| 3273 | unsigned int tlun_cnt; /* total LUN count */ |
| 3274 | unsigned int rlen; /* response length (in bytes) */ |
| 3275 | int i, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 3277 | clear_luns_changed_on_target(devip); |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3278 | |
| 3279 | select_report = cmd[2]; |
| 3280 | alloc_len = get_unaligned_be32(cmd + 6); |
| 3281 | |
| 3282 | if (alloc_len < 4) { |
| 3283 | pr_err("alloc len too small %d\n", alloc_len); |
| 3284 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | return check_condition_result; |
| 3286 | } |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3287 | |
| 3288 | switch (select_report) { |
| 3289 | case 0: /* all LUNs apart from W-LUNs */ |
| 3290 | lun_cnt = sdebug_max_luns; |
| 3291 | wlun_cnt = 0; |
| 3292 | break; |
| 3293 | case 1: /* only W-LUNs */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3294 | lun_cnt = 0; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3295 | wlun_cnt = 1; |
| 3296 | break; |
| 3297 | case 2: /* all LUNs */ |
| 3298 | lun_cnt = sdebug_max_luns; |
| 3299 | wlun_cnt = 1; |
| 3300 | break; |
| 3301 | case 0x10: /* only administrative LUs */ |
| 3302 | case 0x11: /* see SPC-5 */ |
| 3303 | case 0x12: /* only subsiduary LUs owned by referenced LU */ |
| 3304 | default: |
| 3305 | pr_debug("select report invalid %d\n", select_report); |
| 3306 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); |
| 3307 | return check_condition_result; |
| 3308 | } |
| 3309 | |
| 3310 | if (sdebug_no_lun_0 && (lun_cnt > 0)) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3311 | --lun_cnt; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3312 | |
| 3313 | tlun_cnt = lun_cnt + wlun_cnt; |
| 3314 | |
| 3315 | rlen = (tlun_cnt * sizeof(struct scsi_lun)) + 8; |
| 3316 | arr = vmalloc(rlen); |
| 3317 | if (!arr) { |
| 3318 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3319 | INSUFF_RES_ASCQ); |
| 3320 | return check_condition_result; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3321 | } |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 3322 | memset(arr, 0, rlen); |
| 3323 | pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n", |
| 3324 | select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0); |
| 3325 | |
| 3326 | /* luns start at byte 8 in response following the header */ |
| 3327 | lun_p = (struct scsi_lun *)&arr[8]; |
| 3328 | |
| 3329 | /* LUNs use single level peripheral device addressing method */ |
| 3330 | lun = sdebug_no_lun_0 ? 1 : 0; |
| 3331 | for (i = 0; i < lun_cnt; i++) |
| 3332 | int_to_scsilun(lun++, lun_p++); |
| 3333 | |
| 3334 | if (wlun_cnt) |
| 3335 | int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p++); |
| 3336 | |
| 3337 | put_unaligned_be32(rlen - 8, &arr[0]); |
| 3338 | |
| 3339 | res = fill_from_dev_buffer(scp, arr, rlen); |
| 3340 | vfree(arr); |
| 3341 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | } |
| 3343 | |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3344 | static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba, |
| 3345 | unsigned int num, struct sdebug_dev_info *devip) |
| 3346 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3347 | int j; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3348 | unsigned char *kaddr, *buf; |
| 3349 | unsigned int offset; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3350 | struct scsi_data_buffer *sdb = scsi_in(scp); |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3351 | struct sg_mapping_iter miter; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3352 | |
| 3353 | /* better not to use temporary buffer. */ |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 3354 | buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC); |
Akinobu Mita | c5af0db | 2014-02-26 22:57:01 +0900 | [diff] [blame] | 3355 | if (!buf) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 3356 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3357 | INSUFF_RES_ASCQ); |
Akinobu Mita | c5af0db | 2014-02-26 22:57:01 +0900 | [diff] [blame] | 3358 | return check_condition_result; |
| 3359 | } |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3360 | |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 3361 | scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp)); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3362 | |
| 3363 | offset = 0; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3364 | sg_miter_start(&miter, sdb->table.sgl, sdb->table.nents, |
| 3365 | SG_MITER_ATOMIC | SG_MITER_TO_SG); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3366 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3367 | while (sg_miter_next(&miter)) { |
| 3368 | kaddr = miter.addr; |
| 3369 | for (j = 0; j < miter.length; j++) |
| 3370 | *(kaddr + j) ^= *(buf + offset + j); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3371 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3372 | offset += miter.length; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3373 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3374 | sg_miter_stop(&miter); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3375 | kfree(buf); |
| 3376 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3377 | return 0; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3378 | } |
| 3379 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3380 | static int resp_xdwriteread_10(struct scsi_cmnd *scp, |
| 3381 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3382 | { |
| 3383 | u8 *cmd = scp->cmnd; |
| 3384 | u64 lba; |
| 3385 | u32 num; |
| 3386 | int errsts; |
| 3387 | |
| 3388 | if (!scsi_bidi_cmnd(scp)) { |
| 3389 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3390 | INSUFF_RES_ASCQ); |
| 3391 | return check_condition_result; |
| 3392 | } |
| 3393 | errsts = resp_read_dt0(scp, devip); |
| 3394 | if (errsts) |
| 3395 | return errsts; |
| 3396 | if (!(cmd[1] & 0x4)) { /* DISABLE_WRITE is not set */ |
| 3397 | errsts = resp_write_dt0(scp, devip); |
| 3398 | if (errsts) |
| 3399 | return errsts; |
| 3400 | } |
| 3401 | lba = get_unaligned_be32(cmd + 2); |
| 3402 | num = get_unaligned_be16(cmd + 7); |
| 3403 | return resp_xdwriteread(scp, lba, num, devip); |
| 3404 | } |
| 3405 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3406 | static struct sdebug_queue *get_queue(struct scsi_cmnd *cmnd) |
| 3407 | { |
| 3408 | struct sdebug_queue *sqp = sdebug_q_arr; |
| 3409 | |
| 3410 | if (sdebug_mq_active) { |
| 3411 | u32 tag = blk_mq_unique_tag(cmnd->request); |
| 3412 | u16 hwq = blk_mq_unique_tag_to_hwq(tag); |
| 3413 | |
| 3414 | if (unlikely(hwq >= submit_queues)) { |
| 3415 | pr_warn("Unexpected hwq=%d, apply modulo\n", hwq); |
| 3416 | hwq %= submit_queues; |
| 3417 | } |
| 3418 | pr_debug("tag=%u, hwq=%d\n", tag, hwq); |
| 3419 | return sqp + hwq; |
| 3420 | } else |
| 3421 | return sqp; |
| 3422 | } |
| 3423 | |
| 3424 | /* Queued (deferred) command completions converge here. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3425 | static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3427 | int qc_idx; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3428 | int retiring = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | unsigned long iflags; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3430 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3431 | struct sdebug_queued_cmd *sqcp; |
| 3432 | struct scsi_cmnd *scp; |
| 3433 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3434 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3435 | qc_idx = sd_dp->qc_idx; |
| 3436 | sqp = sdebug_q_arr + sd_dp->sqa_idx; |
| 3437 | if (sdebug_statistics) { |
| 3438 | atomic_inc(&sdebug_completions); |
| 3439 | if (raw_smp_processor_id() != sd_dp->issuing_cpu) |
| 3440 | atomic_inc(&sdebug_miss_cpus); |
| 3441 | } |
| 3442 | if (unlikely((qc_idx < 0) || (qc_idx >= SDEBUG_CANQUEUE))) { |
| 3443 | pr_err("wild qc_idx=%d\n", qc_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | return; |
| 3445 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3446 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 3447 | sqcp = &sqp->qc_arr[qc_idx]; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3448 | scp = sqcp->a_cmnd; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3449 | if (unlikely(scp == NULL)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3450 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 3451 | pr_err("scp is NULL, sqa_idx=%d, qc_idx=%d\n", |
| 3452 | sd_dp->sqa_idx, qc_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | return; |
| 3454 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3455 | devip = (struct sdebug_dev_info *)scp->device->hostdata; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3456 | if (likely(devip)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3457 | atomic_dec(&devip->num_in_q); |
| 3458 | else |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3459 | pr_err("devip=NULL\n"); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3460 | if (unlikely(atomic_read(&retired_max_queue) > 0)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3461 | retiring = 1; |
| 3462 | |
| 3463 | sqcp->a_cmnd = NULL; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3464 | if (unlikely(!test_and_clear_bit(qc_idx, sqp->in_use_bm))) { |
| 3465 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3466 | pr_err("Unexpected completion\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3467 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3469 | |
| 3470 | if (unlikely(retiring)) { /* user has reduced max_queue */ |
| 3471 | int k, retval; |
| 3472 | |
| 3473 | retval = atomic_read(&retired_max_queue); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3474 | if (qc_idx >= retval) { |
| 3475 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3476 | pr_err("index %d too large\n", retval); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3477 | return; |
| 3478 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3479 | k = find_last_bit(sqp->in_use_bm, retval); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3480 | if ((k < sdebug_max_queue) || (k == retval)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3481 | atomic_set(&retired_max_queue, 0); |
| 3482 | else |
| 3483 | atomic_set(&retired_max_queue, k + 1); |
| 3484 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3485 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3486 | scp->scsi_done(scp); /* callback to mid level */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | } |
| 3488 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3489 | /* When high resolution timer goes off this function is called. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3490 | static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3491 | { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3492 | struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer, |
| 3493 | hrt); |
| 3494 | sdebug_q_cmd_complete(sd_dp); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3495 | return HRTIMER_NORESTART; |
| 3496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3498 | /* When work queue schedules work, it calls this function. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3499 | static void sdebug_q_cmd_wq_complete(struct work_struct *work) |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3500 | { |
| 3501 | struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer, |
| 3502 | ew.work); |
| 3503 | sdebug_q_cmd_complete(sd_dp); |
| 3504 | } |
| 3505 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3506 | static struct sdebug_dev_info *sdebug_device_create( |
| 3507 | struct sdebug_host_info *sdbg_host, gfp_t flags) |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3508 | { |
| 3509 | struct sdebug_dev_info *devip; |
| 3510 | |
| 3511 | devip = kzalloc(sizeof(*devip), flags); |
| 3512 | if (devip) { |
| 3513 | devip->sdbg_host = sdbg_host; |
| 3514 | list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list); |
| 3515 | } |
| 3516 | return devip; |
| 3517 | } |
| 3518 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3519 | 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] | 3520 | { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3521 | struct sdebug_host_info *sdbg_host; |
| 3522 | struct sdebug_dev_info *open_devip = NULL; |
| 3523 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | |
FUJITA Tomonori | d1e4c9c | 2008-03-02 18:30:18 +0900 | [diff] [blame] | 3525 | sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host); |
| 3526 | if (!sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3527 | pr_err("Host info NULL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | return NULL; |
| 3529 | } |
| 3530 | list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) { |
| 3531 | if ((devip->used) && (devip->channel == sdev->channel) && |
| 3532 | (devip->target == sdev->id) && |
| 3533 | (devip->lun == sdev->lun)) |
| 3534 | return devip; |
| 3535 | else { |
| 3536 | if ((!devip->used) && (!open_devip)) |
| 3537 | open_devip = devip; |
| 3538 | } |
| 3539 | } |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3540 | if (!open_devip) { /* try and make a new one */ |
| 3541 | open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC); |
| 3542 | if (!open_devip) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3543 | pr_err("out of memory at line %d\n", __LINE__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3544 | return NULL; |
| 3545 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | } |
FUJITA Tomonori | a75869d | 2008-03-20 11:09:17 +0900 | [diff] [blame] | 3547 | |
| 3548 | open_devip->channel = sdev->channel; |
| 3549 | open_devip->target = sdev->id; |
| 3550 | open_devip->lun = sdev->lun; |
| 3551 | open_devip->sdbg_host = sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3552 | atomic_set(&open_devip->num_in_q, 0); |
| 3553 | set_bit(SDEBUG_UA_POR, open_devip->uas_bm); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3554 | open_devip->used = true; |
FUJITA Tomonori | a75869d | 2008-03-20 11:09:17 +0900 | [diff] [blame] | 3555 | return open_devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3556 | } |
| 3557 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3558 | static int scsi_debug_slave_alloc(struct scsi_device *sdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3560 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3561 | pr_info("slave_alloc <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3562 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 3563 | queue_flag_set_unlocked(QUEUE_FLAG_BIDI, sdp->request_queue); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3564 | return 0; |
| 3565 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3567 | static int scsi_debug_slave_configure(struct scsi_device *sdp) |
| 3568 | { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3569 | struct sdebug_dev_info *devip = |
| 3570 | (struct sdebug_dev_info *)sdp->hostdata; |
FUJITA Tomonori | a34c4e9 | 2008-03-25 09:26:50 +0900 | [diff] [blame] | 3571 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3572 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3573 | pr_info("slave_configure <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3574 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3575 | if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN) |
| 3576 | sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN; |
| 3577 | if (devip == NULL) { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3578 | devip = find_build_dev_info(sdp); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3579 | if (devip == NULL) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3580 | return 1; /* no resources, will be marked offline */ |
| 3581 | } |
Christoph Hellwig | c8b09f6 | 2014-11-03 20:15:14 +0100 | [diff] [blame] | 3582 | sdp->hostdata = devip; |
Akinobu Mita | 6bb5e6e | 2014-06-02 22:56:49 +0900 | [diff] [blame] | 3583 | blk_queue_max_segment_size(sdp->request_queue, -1U); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3584 | if (sdebug_no_uld) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 3585 | sdp->no_uld_attach = 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3586 | return 0; |
| 3587 | } |
| 3588 | |
| 3589 | static void scsi_debug_slave_destroy(struct scsi_device *sdp) |
| 3590 | { |
| 3591 | struct sdebug_dev_info *devip = |
| 3592 | (struct sdebug_dev_info *)sdp->hostdata; |
| 3593 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3594 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3595 | pr_info("slave_destroy <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3596 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
| 3597 | if (devip) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3598 | /* make this slot available for re-use */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3599 | devip->used = false; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3600 | sdp->hostdata = NULL; |
| 3601 | } |
| 3602 | } |
| 3603 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3604 | static void stop_qc_helper(struct sdebug_defer *sd_dp) |
| 3605 | { |
| 3606 | if (!sd_dp) |
| 3607 | return; |
| 3608 | if ((sdebug_jdelay > 0) || (sdebug_ndelay > 0)) |
| 3609 | hrtimer_cancel(&sd_dp->hrt); |
| 3610 | else if (sdebug_jdelay < 0) |
| 3611 | cancel_work_sync(&sd_dp->ew.work); |
| 3612 | } |
| 3613 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3614 | /* If @cmnd found deletes its timer or work queue and returns true; else |
| 3615 | returns false */ |
| 3616 | static bool stop_queued_cmnd(struct scsi_cmnd *cmnd) |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3617 | { |
| 3618 | unsigned long iflags; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3619 | int j, k, qmax, r_qmax; |
| 3620 | struct sdebug_queue *sqp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3621 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3622 | struct sdebug_dev_info *devip; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3623 | struct sdebug_defer *sd_dp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3624 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3625 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 3626 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 3627 | qmax = sdebug_max_queue; |
| 3628 | r_qmax = atomic_read(&retired_max_queue); |
| 3629 | if (r_qmax > qmax) |
| 3630 | qmax = r_qmax; |
| 3631 | for (k = 0; k < qmax; ++k) { |
| 3632 | if (test_bit(k, sqp->in_use_bm)) { |
| 3633 | sqcp = &sqp->qc_arr[k]; |
| 3634 | if (cmnd != sqcp->a_cmnd) |
| 3635 | continue; |
| 3636 | /* found */ |
| 3637 | devip = (struct sdebug_dev_info *) |
| 3638 | cmnd->device->hostdata; |
| 3639 | if (devip) |
| 3640 | atomic_dec(&devip->num_in_q); |
| 3641 | sqcp->a_cmnd = NULL; |
| 3642 | sd_dp = sqcp->sd_dp; |
| 3643 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 3644 | stop_qc_helper(sd_dp); |
| 3645 | clear_bit(k, sqp->in_use_bm); |
| 3646 | return true; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3647 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3648 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3649 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3650 | } |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3651 | return false; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3652 | } |
| 3653 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3654 | /* Deletes (stops) timers or work queues of all queued commands */ |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3655 | static void stop_all_queued(void) |
| 3656 | { |
| 3657 | unsigned long iflags; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3658 | int j, k; |
| 3659 | struct sdebug_queue *sqp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3660 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3661 | struct sdebug_dev_info *devip; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3662 | struct sdebug_defer *sd_dp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3663 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3664 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 3665 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 3666 | for (k = 0; k < SDEBUG_CANQUEUE; ++k) { |
| 3667 | if (test_bit(k, sqp->in_use_bm)) { |
| 3668 | sqcp = &sqp->qc_arr[k]; |
| 3669 | if (sqcp->a_cmnd == NULL) |
| 3670 | continue; |
| 3671 | devip = (struct sdebug_dev_info *) |
| 3672 | sqcp->a_cmnd->device->hostdata; |
| 3673 | if (devip) |
| 3674 | atomic_dec(&devip->num_in_q); |
| 3675 | sqcp->a_cmnd = NULL; |
| 3676 | sd_dp = sqcp->sd_dp; |
| 3677 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 3678 | stop_qc_helper(sd_dp); |
| 3679 | clear_bit(k, sqp->in_use_bm); |
| 3680 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3681 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3682 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3683 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3684 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | } |
| 3686 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3687 | /* Free queued command memory on heap */ |
| 3688 | static void free_all_queued(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3689 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3690 | int j, k; |
| 3691 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3692 | struct sdebug_queued_cmd *sqcp; |
| 3693 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3694 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 3695 | for (k = 0; k < SDEBUG_CANQUEUE; ++k) { |
| 3696 | sqcp = &sqp->qc_arr[k]; |
| 3697 | kfree(sqcp->sd_dp); |
| 3698 | sqcp->sd_dp = NULL; |
| 3699 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3700 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3701 | } |
| 3702 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3703 | static int scsi_debug_abort(struct scsi_cmnd *SCpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3705 | bool ok; |
| 3706 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3707 | ++num_aborts; |
| 3708 | if (SCpnt) { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3709 | ok = stop_queued_cmnd(SCpnt); |
| 3710 | if (SCpnt->device && (SDEBUG_OPT_ALL_NOISE & sdebug_opts)) |
| 3711 | sdev_printk(KERN_INFO, SCpnt->device, |
| 3712 | "%s: command%s found\n", __func__, |
| 3713 | ok ? "" : " not"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3714 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3715 | return SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt) |
| 3719 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | ++num_dev_resets; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3721 | if (SCpnt && SCpnt->device) { |
| 3722 | struct scsi_device *sdp = SCpnt->device; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3723 | struct sdebug_dev_info *devip = |
| 3724 | (struct sdebug_dev_info *)sdp->hostdata; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3725 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3726 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3727 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | if (devip) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3729 | set_bit(SDEBUG_UA_POR, devip->uas_bm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | } |
| 3731 | return SUCCESS; |
| 3732 | } |
| 3733 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3734 | static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt) |
| 3735 | { |
| 3736 | struct sdebug_host_info *sdbg_host; |
| 3737 | struct sdebug_dev_info *devip; |
| 3738 | struct scsi_device *sdp; |
| 3739 | struct Scsi_Host *hp; |
| 3740 | int k = 0; |
| 3741 | |
| 3742 | ++num_target_resets; |
| 3743 | if (!SCpnt) |
| 3744 | goto lie; |
| 3745 | sdp = SCpnt->device; |
| 3746 | if (!sdp) |
| 3747 | goto lie; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3748 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3749 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
| 3750 | hp = sdp->host; |
| 3751 | if (!hp) |
| 3752 | goto lie; |
| 3753 | sdbg_host = *(struct sdebug_host_info **)shost_priv(hp); |
| 3754 | if (sdbg_host) { |
| 3755 | list_for_each_entry(devip, |
| 3756 | &sdbg_host->dev_info_list, |
| 3757 | dev_list) |
| 3758 | if (devip->target == sdp->id) { |
| 3759 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3760 | ++k; |
| 3761 | } |
| 3762 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3763 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3764 | sdev_printk(KERN_INFO, sdp, |
| 3765 | "%s: %d device(s) found in target\n", __func__, k); |
| 3766 | lie: |
| 3767 | return SUCCESS; |
| 3768 | } |
| 3769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt) |
| 3771 | { |
| 3772 | struct sdebug_host_info *sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3773 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3774 | struct scsi_device * sdp; |
| 3775 | struct Scsi_Host * hp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3776 | int k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3778 | ++num_bus_resets; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3779 | if (!(SCpnt && SCpnt->device)) |
| 3780 | goto lie; |
| 3781 | sdp = SCpnt->device; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3782 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3783 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
| 3784 | hp = sdp->host; |
| 3785 | if (hp) { |
FUJITA Tomonori | d1e4c9c | 2008-03-02 18:30:18 +0900 | [diff] [blame] | 3786 | sdbg_host = *(struct sdebug_host_info **)shost_priv(hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | if (sdbg_host) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3788 | list_for_each_entry(devip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | &sdbg_host->dev_info_list, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3790 | dev_list) { |
| 3791 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3792 | ++k; |
| 3793 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3794 | } |
| 3795 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3796 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3797 | sdev_printk(KERN_INFO, sdp, |
| 3798 | "%s: %d device(s) found in host\n", __func__, k); |
| 3799 | lie: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3800 | return SUCCESS; |
| 3801 | } |
| 3802 | |
| 3803 | static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt) |
| 3804 | { |
| 3805 | struct sdebug_host_info * sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3806 | struct sdebug_dev_info *devip; |
| 3807 | int k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3809 | ++num_host_resets; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3810 | if ((SCpnt->device) && (SDEBUG_OPT_ALL_NOISE & sdebug_opts)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3811 | sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | spin_lock(&sdebug_host_list_lock); |
| 3813 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3814 | list_for_each_entry(devip, &sdbg_host->dev_info_list, |
| 3815 | dev_list) { |
| 3816 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3817 | ++k; |
| 3818 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | } |
| 3820 | spin_unlock(&sdebug_host_list_lock); |
| 3821 | stop_all_queued(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3822 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3823 | sdev_printk(KERN_INFO, SCpnt->device, |
| 3824 | "%s: %d device(s) found\n", __func__, k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | return SUCCESS; |
| 3826 | } |
| 3827 | |
FUJITA Tomonori | f58b0ef | 2008-03-30 00:59:54 +0900 | [diff] [blame] | 3828 | static void __init sdebug_build_parts(unsigned char *ramp, |
FUJITA Tomonori | 5f2578e | 2008-03-30 00:59:57 +0900 | [diff] [blame] | 3829 | unsigned long store_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | { |
| 3831 | struct partition * pp; |
| 3832 | int starts[SDEBUG_MAX_PARTS + 2]; |
| 3833 | int sectors_per_part, num_sectors, k; |
| 3834 | int heads_by_sects, start_sec, end_sec; |
| 3835 | |
| 3836 | /* assume partition table already zeroed */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3837 | if ((sdebug_num_parts < 1) || (store_size < 1048576)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | return; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3839 | if (sdebug_num_parts > SDEBUG_MAX_PARTS) { |
| 3840 | sdebug_num_parts = SDEBUG_MAX_PARTS; |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3841 | pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3842 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3843 | num_sectors = (int)sdebug_store_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3844 | sectors_per_part = (num_sectors - sdebug_sectors_per) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3845 | / sdebug_num_parts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3846 | heads_by_sects = sdebug_heads * sdebug_sectors_per; |
| 3847 | starts[0] = sdebug_sectors_per; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3848 | for (k = 1; k < sdebug_num_parts; ++k) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3849 | starts[k] = ((k * sectors_per_part) / heads_by_sects) |
| 3850 | * heads_by_sects; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3851 | starts[sdebug_num_parts] = num_sectors; |
| 3852 | starts[sdebug_num_parts + 1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | |
| 3854 | ramp[510] = 0x55; /* magic partition markings */ |
| 3855 | ramp[511] = 0xAA; |
| 3856 | pp = (struct partition *)(ramp + 0x1be); |
| 3857 | for (k = 0; starts[k + 1]; ++k, ++pp) { |
| 3858 | start_sec = starts[k]; |
| 3859 | end_sec = starts[k + 1] - 1; |
| 3860 | pp->boot_ind = 0; |
| 3861 | |
| 3862 | pp->cyl = start_sec / heads_by_sects; |
| 3863 | pp->head = (start_sec - (pp->cyl * heads_by_sects)) |
| 3864 | / sdebug_sectors_per; |
| 3865 | pp->sector = (start_sec % sdebug_sectors_per) + 1; |
| 3866 | |
| 3867 | pp->end_cyl = end_sec / heads_by_sects; |
| 3868 | pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects)) |
| 3869 | / sdebug_sectors_per; |
| 3870 | pp->end_sector = (end_sec % sdebug_sectors_per) + 1; |
| 3871 | |
Akinobu Mita | 150c354 | 2013-08-26 22:08:40 +0900 | [diff] [blame] | 3872 | pp->start_sect = cpu_to_le32(start_sec); |
| 3873 | pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3874 | pp->sys_ind = 0x83; /* plain Linux partition */ |
| 3875 | } |
| 3876 | } |
| 3877 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3878 | static void block_unblock_all_queues(bool block) |
| 3879 | { |
| 3880 | int j; |
| 3881 | struct sdebug_queue *sqp; |
| 3882 | |
| 3883 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) |
| 3884 | atomic_set(&sqp->blocked, (int)block); |
| 3885 | } |
| 3886 | |
| 3887 | /* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1 |
| 3888 | * commands will be processed normally before triggers occur. |
| 3889 | */ |
| 3890 | static void tweak_cmnd_count(void) |
| 3891 | { |
| 3892 | int count, modulo; |
| 3893 | |
| 3894 | modulo = abs(sdebug_every_nth); |
| 3895 | if (modulo < 2) |
| 3896 | return; |
| 3897 | block_unblock_all_queues(true); |
| 3898 | count = atomic_read(&sdebug_cmnd_count); |
| 3899 | atomic_set(&sdebug_cmnd_count, (count / modulo) * modulo); |
| 3900 | block_unblock_all_queues(false); |
| 3901 | } |
| 3902 | |
| 3903 | static void clear_queue_stats(void) |
| 3904 | { |
| 3905 | atomic_set(&sdebug_cmnd_count, 0); |
| 3906 | atomic_set(&sdebug_completions, 0); |
| 3907 | atomic_set(&sdebug_miss_cpus, 0); |
| 3908 | atomic_set(&sdebug_a_tsf, 0); |
| 3909 | } |
| 3910 | |
| 3911 | static void setup_inject(struct sdebug_queue *sqp, |
| 3912 | struct sdebug_queued_cmd *sqcp) |
| 3913 | { |
| 3914 | if ((atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) > 0) |
| 3915 | return; |
| 3916 | sqcp->inj_recovered = !!(SDEBUG_OPT_RECOVERED_ERR & sdebug_opts); |
| 3917 | sqcp->inj_transport = !!(SDEBUG_OPT_TRANSPORT_ERR & sdebug_opts); |
| 3918 | sqcp->inj_dif = !!(SDEBUG_OPT_DIF_ERR & sdebug_opts); |
| 3919 | sqcp->inj_dix = !!(SDEBUG_OPT_DIX_ERR & sdebug_opts); |
| 3920 | sqcp->inj_short = !!(SDEBUG_OPT_SHORT_TRANSFER & sdebug_opts); |
| 3921 | } |
| 3922 | |
| 3923 | /* Complete the processing of the thread that queued a SCSI command to this |
| 3924 | * driver. It either completes the command by calling cmnd_done() or |
| 3925 | * schedules a hr timer or work queue then returns 0. Returns |
| 3926 | * SCSI_MLQUEUE_HOST_BUSY if temporarily out of resources. |
| 3927 | */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3928 | static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip, |
| 3929 | int scsi_result, int delta_jiff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3930 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3931 | unsigned long iflags; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3932 | int k, num_in_q, qdepth, inject; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3933 | struct sdebug_queue *sqp; |
| 3934 | struct sdebug_queued_cmd *sqcp; |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 3935 | struct scsi_device *sdp; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3936 | struct sdebug_defer *sd_dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3937 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3938 | if (unlikely(devip == NULL)) { |
| 3939 | if (scsi_result == 0) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3940 | scsi_result = DID_NO_CONNECT << 16; |
| 3941 | goto respond_in_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | } |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 3943 | sdp = cmnd->device; |
| 3944 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3945 | if (unlikely(sdebug_verbose && scsi_result)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3946 | sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n", |
| 3947 | __func__, scsi_result); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3948 | if (delta_jiff == 0) |
| 3949 | goto respond_in_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3951 | /* schedule the response at a later time if resources permit */ |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3952 | sqp = get_queue(cmnd); |
| 3953 | spin_lock_irqsave(&sqp->qc_lock, iflags); |
| 3954 | if (unlikely(atomic_read(&sqp->blocked))) { |
| 3955 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 3956 | return SCSI_MLQUEUE_HOST_BUSY; |
| 3957 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3958 | num_in_q = atomic_read(&devip->num_in_q); |
| 3959 | qdepth = cmnd->device->queue_depth; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3960 | inject = 0; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3961 | if (unlikely((qdepth > 0) && (num_in_q >= qdepth))) { |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3962 | if (scsi_result) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3963 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3964 | goto respond_in_thread; |
| 3965 | } else |
| 3966 | scsi_result = device_qfull_result; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3967 | } else if (unlikely(sdebug_every_nth && |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3968 | (SDEBUG_OPT_RARE_TSF & sdebug_opts) && |
| 3969 | (scsi_result == 0))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3970 | if ((num_in_q == (qdepth - 1)) && |
| 3971 | (atomic_inc_return(&sdebug_a_tsf) >= |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3972 | abs(sdebug_every_nth))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3973 | atomic_set(&sdebug_a_tsf, 0); |
| 3974 | inject = 1; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3975 | scsi_result = device_qfull_result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3976 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3977 | } |
| 3978 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3979 | k = find_first_zero_bit(sqp->in_use_bm, sdebug_max_queue); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3980 | if (unlikely(k >= sdebug_max_queue)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3981 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3982 | if (scsi_result) |
| 3983 | goto respond_in_thread; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3984 | else if (SDEBUG_OPT_ALL_TSF & sdebug_opts) |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3985 | scsi_result = device_qfull_result; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3986 | if (SDEBUG_OPT_Q_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3987 | sdev_printk(KERN_INFO, sdp, |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3988 | "%s: max_queue=%d exceeded, %s\n", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3989 | __func__, sdebug_max_queue, |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3990 | (scsi_result ? "status: TASK SET FULL" : |
| 3991 | "report: host busy")); |
| 3992 | if (scsi_result) |
| 3993 | goto respond_in_thread; |
| 3994 | else |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3995 | return SCSI_MLQUEUE_HOST_BUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3996 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3997 | __set_bit(k, sqp->in_use_bm); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3998 | atomic_inc(&devip->num_in_q); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 3999 | sqcp = &sqp->qc_arr[k]; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4000 | sqcp->a_cmnd = cmnd; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4001 | cmnd->host_scribble = (unsigned char *)sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4002 | cmnd->result = scsi_result; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4003 | sd_dp = sqcp->sd_dp; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4004 | spin_unlock_irqrestore(&sqp->qc_lock, iflags); |
| 4005 | if (unlikely(sdebug_every_nth && sdebug_any_injecting_opt)) |
| 4006 | setup_inject(sqp, sqcp); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4007 | if (delta_jiff > 0 || sdebug_ndelay > 0) { |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 4008 | ktime_t kt; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4009 | |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 4010 | if (delta_jiff > 0) { |
| 4011 | struct timespec ts; |
| 4012 | |
| 4013 | jiffies_to_timespec(delta_jiff, &ts); |
| 4014 | kt = ktime_set(ts.tv_sec, ts.tv_nsec); |
| 4015 | } else |
| 4016 | kt = ktime_set(0, sdebug_ndelay); |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4017 | if (NULL == sd_dp) { |
| 4018 | sd_dp = kzalloc(sizeof(*sd_dp), GFP_ATOMIC); |
| 4019 | if (NULL == sd_dp) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4020 | return SCSI_MLQUEUE_HOST_BUSY; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4021 | sqcp->sd_dp = sd_dp; |
| 4022 | hrtimer_init(&sd_dp->hrt, CLOCK_MONOTONIC, |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4023 | HRTIMER_MODE_REL_PINNED); |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4024 | sd_dp->hrt.function = sdebug_q_cmd_hrt_complete; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4025 | sd_dp->sqa_idx = sqp - sdebug_q_arr; |
| 4026 | sd_dp->qc_idx = k; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4027 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4028 | if (sdebug_statistics) |
| 4029 | sd_dp->issuing_cpu = raw_smp_processor_id(); |
| 4030 | hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL_PINNED); |
| 4031 | } else { /* jdelay < 0, use work queue */ |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4032 | if (NULL == sd_dp) { |
| 4033 | sd_dp = kzalloc(sizeof(*sqcp->sd_dp), GFP_ATOMIC); |
| 4034 | if (NULL == sd_dp) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4035 | return SCSI_MLQUEUE_HOST_BUSY; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4036 | sqcp->sd_dp = sd_dp; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4037 | sd_dp->sqa_idx = sqp - sdebug_q_arr; |
| 4038 | sd_dp->qc_idx = k; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4039 | INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4040 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4041 | if (sdebug_statistics) |
| 4042 | sd_dp->issuing_cpu = raw_smp_processor_id(); |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4043 | schedule_work(&sd_dp->ew.work); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4044 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4045 | if (unlikely((SDEBUG_OPT_Q_NOISE & sdebug_opts) && |
| 4046 | (scsi_result == device_qfull_result))) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4047 | sdev_printk(KERN_INFO, sdp, |
| 4048 | "%s: num_in_q=%d +1, %s%s\n", __func__, |
| 4049 | num_in_q, (inject ? "<inject> " : ""), |
| 4050 | "status: TASK SET FULL"); |
| 4051 | return 0; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 4052 | |
| 4053 | respond_in_thread: /* call back to mid-layer using invocation thread */ |
| 4054 | cmnd->result = scsi_result; |
| 4055 | cmnd->scsi_done(cmnd); |
| 4056 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4058 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4059 | /* Note: The following macros create attribute files in the |
| 4060 | /sys/module/scsi_debug/parameters directory. Unfortunately this |
| 4061 | driver is unaware of a change and cannot trigger auxiliary actions |
| 4062 | as it can when the corresponding attribute in the |
| 4063 | /sys/bus/pseudo/drivers/scsi_debug directory is changed. |
| 4064 | */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4065 | module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR); |
| 4066 | module_param_named(ato, sdebug_ato, int, S_IRUGO); |
| 4067 | module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR); |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4068 | module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4069 | module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO); |
| 4070 | module_param_named(dif, sdebug_dif, int, S_IRUGO); |
| 4071 | module_param_named(dix, sdebug_dix, int, S_IRUGO); |
| 4072 | module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR); |
| 4073 | module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR); |
| 4074 | module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR); |
| 4075 | module_param_named(guard, sdebug_guard, uint, S_IRUGO); |
| 4076 | module_param_named(host_lock, sdebug_host_lock, bool, S_IRUGO | S_IWUSR); |
| 4077 | module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO); |
| 4078 | module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO); |
| 4079 | module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO); |
| 4080 | module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO); |
| 4081 | module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO); |
| 4082 | module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR); |
| 4083 | module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR); |
| 4084 | module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR); |
| 4085 | module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR); |
| 4086 | module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO); |
| 4087 | module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO); |
| 4088 | module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR); |
| 4089 | module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO); |
| 4090 | module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR); |
| 4091 | module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO); |
| 4092 | module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR); |
| 4093 | module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR); |
| 4094 | module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO); |
| 4095 | module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4096 | module_param_named(statistics, sdebug_statistics, bool, S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4097 | module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4098 | module_param_named(submit_queues, submit_queues, int, S_IRUGO); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4099 | module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO); |
| 4100 | module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO); |
| 4101 | module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO); |
| 4102 | module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO); |
| 4103 | module_param_named(virtual_gb, sdebug_virtual_gb, int, S_IRUGO | S_IWUSR); |
| 4104 | module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int, |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4105 | S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4106 | module_param_named(write_same_length, sdebug_write_same_length, int, |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4107 | S_IRUGO | S_IWUSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | |
| 4109 | MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert"); |
| 4110 | MODULE_DESCRIPTION("SCSI debug adapter driver"); |
| 4111 | MODULE_LICENSE("GPL"); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4112 | MODULE_VERSION(SDEBUG_VERSION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | |
| 4114 | MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4115 | MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)"); |
Akinobu Mita | 0759c66 | 2014-02-26 22:57:04 +0900 | [diff] [blame] | 4116 | MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4117 | 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] | 4118 | 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] | 4119 | MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)"); |
| 4120 | MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4121 | MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)"); |
Randy Dunlap | beb87c3 | 2007-06-11 11:36:40 -0700 | [diff] [blame] | 4122 | MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)"); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4123 | 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] | 4124 | MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)"); |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 4125 | MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4126 | MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)"); |
| 4127 | MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)"); |
| 4128 | 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^] | 4129 | MODULE_PARM_DESC(lbprz, |
| 4130 | "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] | 4131 | MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4132 | 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] | 4133 | MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))"); |
| 4134 | MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4135 | 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] | 4136 | 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] | 4137 | MODULE_PARM_DESC(num_parts, "number of partitions(def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4138 | 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] | 4139 | MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)"); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 4140 | 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] | 4141 | MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])"); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4143 | MODULE_PARM_DESC(removable, "claim to have removable media (def=0)"); |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 4144 | 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] | 4145 | MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4146 | MODULE_PARM_DESC(statistics, "collect statistics on commands, queues (def=0)"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4147 | MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4148 | 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] | 4149 | MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)"); |
| 4150 | 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] | 4151 | MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)"); |
| 4152 | MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4153 | 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] | 4154 | MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)"); |
| 4155 | 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] | 4156 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 4157 | #define SDEBUG_INFO_LEN 256 |
| 4158 | static char sdebug_info[SDEBUG_INFO_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | |
| 4160 | static const char * scsi_debug_info(struct Scsi_Host * shp) |
| 4161 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4162 | int k; |
| 4163 | |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 4164 | k = scnprintf(sdebug_info, SDEBUG_INFO_LEN, "%s: version %s [%s]\n", |
| 4165 | my_name, SDEBUG_VERSION, sdebug_version_date); |
| 4166 | if (k >= (SDEBUG_INFO_LEN - 1)) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4167 | return sdebug_info; |
Douglas Gilbert | 760f3b0 | 2016-05-06 00:40:27 -0400 | [diff] [blame^] | 4168 | scnprintf(sdebug_info + k, SDEBUG_INFO_LEN - k, |
| 4169 | " dev_size_mb=%d, opts=0x%x, submit_queues=%d, %s=%d", |
| 4170 | sdebug_dev_size_mb, sdebug_opts, submit_queues, |
| 4171 | "statistics", (int)sdebug_statistics); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | return sdebug_info; |
| 4173 | } |
| 4174 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4175 | /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4176 | static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer, |
| 4177 | int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | { |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4179 | char arr[16]; |
| 4180 | int opts; |
| 4181 | int minLen = length > 15 ? 15 : length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4183 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 4184 | return -EACCES; |
| 4185 | memcpy(arr, buffer, minLen); |
| 4186 | arr[minLen] = '\0'; |
| 4187 | if (1 != sscanf(arr, "%d", &opts)) |
| 4188 | return -EINVAL; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4189 | sdebug_opts = opts; |
| 4190 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); |
| 4191 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts); |
| 4192 | if (sdebug_every_nth != 0) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4193 | tweak_cmnd_count(); |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4194 | return length; |
| 4195 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4197 | /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the |
| 4198 | * same for each scsi_debug host (if more than one). Some of the counters |
| 4199 | * output are not atomics so might be inaccurate in a busy system. */ |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4200 | static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host) |
| 4201 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4202 | int f, j, l; |
| 4203 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4204 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4205 | seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n", |
| 4206 | SDEBUG_VERSION, sdebug_version_date); |
| 4207 | seq_printf(m, "num_tgts=%d, %ssize=%d MB, opts=0x%x, every_nth=%d\n", |
| 4208 | sdebug_num_tgts, "shared (ram) ", sdebug_dev_size_mb, |
| 4209 | sdebug_opts, sdebug_every_nth); |
| 4210 | seq_printf(m, "delay=%d, ndelay=%d, max_luns=%d, sector_size=%d %s\n", |
| 4211 | sdebug_jdelay, sdebug_ndelay, sdebug_max_luns, |
| 4212 | sdebug_sector_size, "bytes"); |
| 4213 | seq_printf(m, "cylinders=%d, heads=%d, sectors=%d, command aborts=%d\n", |
| 4214 | sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per, |
| 4215 | num_aborts); |
| 4216 | seq_printf(m, "RESETs: device=%d, target=%d, bus=%d, host=%d\n", |
| 4217 | num_dev_resets, num_target_resets, num_bus_resets, |
| 4218 | num_host_resets); |
| 4219 | seq_printf(m, "dix_reads=%d, dix_writes=%d, dif_errors=%d\n", |
| 4220 | dix_reads, dix_writes, dif_errors); |
| 4221 | seq_printf(m, "usec_in_jiffy=%lu, %s=%d, mq_active=%d\n", |
| 4222 | TICK_NSEC / 1000, "statistics", sdebug_statistics, |
| 4223 | sdebug_mq_active); |
| 4224 | seq_printf(m, "cmnd_count=%d, completions=%d, %s=%d, a_tsf=%d\n", |
| 4225 | atomic_read(&sdebug_cmnd_count), |
| 4226 | atomic_read(&sdebug_completions), |
| 4227 | "miss_cpus", atomic_read(&sdebug_miss_cpus), |
| 4228 | atomic_read(&sdebug_a_tsf)); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4229 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4230 | seq_printf(m, "submit_queues=%d\n", submit_queues); |
| 4231 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { |
| 4232 | seq_printf(m, " queue %d:\n", j); |
| 4233 | f = find_first_bit(sqp->in_use_bm, sdebug_max_queue); |
| 4234 | if (f != sdebug_max_queue) { |
| 4235 | l = find_last_bit(sqp->in_use_bm, sdebug_max_queue); |
| 4236 | seq_printf(m, " in_use_bm BUSY: %s: %d,%d\n", |
| 4237 | "first,last bits", f, l); |
| 4238 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4239 | } |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4240 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | } |
| 4242 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4243 | static ssize_t delay_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4244 | { |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4245 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4247 | /* Returns -EBUSY if jdelay is being changed and commands are queued. The unit |
| 4248 | * of delay is jiffies. |
| 4249 | */ |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4250 | static ssize_t delay_store(struct device_driver *ddp, const char *buf, |
| 4251 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | { |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4253 | int jdelay, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4255 | if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4256 | res = count; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4257 | if (sdebug_jdelay != jdelay) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4258 | int j, k; |
| 4259 | struct sdebug_queue *sqp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4260 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4261 | block_unblock_all_queues(true); |
| 4262 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; |
| 4263 | ++j, ++sqp) { |
| 4264 | k = find_first_bit(sqp->in_use_bm, |
| 4265 | sdebug_max_queue); |
| 4266 | if (k != sdebug_max_queue) { |
| 4267 | res = -EBUSY; /* queued commands */ |
| 4268 | break; |
| 4269 | } |
| 4270 | } |
| 4271 | if (res > 0) { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4272 | /* make sure sdebug_defer instances get |
| 4273 | * re-allocated for new delay variant */ |
| 4274 | free_all_queued(); |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4275 | sdebug_jdelay = jdelay; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4276 | sdebug_ndelay = 0; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4277 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4278 | block_unblock_all_queues(false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4280 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | } |
| 4282 | return -EINVAL; |
| 4283 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4284 | static DRIVER_ATTR_RW(delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4286 | static ssize_t ndelay_show(struct device_driver *ddp, char *buf) |
| 4287 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4288 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4289 | } |
| 4290 | /* Returns -EBUSY if ndelay is being changed and commands are queued */ |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4291 | /* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4292 | static ssize_t ndelay_store(struct device_driver *ddp, const char *buf, |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4293 | size_t count) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4294 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4295 | int ndelay, res; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4296 | |
| 4297 | if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) && |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4298 | (ndelay >= 0) && (ndelay < (1000 * 1000 * 1000))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4299 | res = count; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4300 | if (sdebug_ndelay != ndelay) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4301 | int j, k; |
| 4302 | struct sdebug_queue *sqp; |
| 4303 | |
| 4304 | block_unblock_all_queues(true); |
| 4305 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; |
| 4306 | ++j, ++sqp) { |
| 4307 | k = find_first_bit(sqp->in_use_bm, |
| 4308 | sdebug_max_queue); |
| 4309 | if (k != sdebug_max_queue) { |
| 4310 | res = -EBUSY; /* queued commands */ |
| 4311 | break; |
| 4312 | } |
| 4313 | } |
| 4314 | if (res > 0) { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4315 | /* make sure sdebug_defer instances get |
| 4316 | * re-allocated for new delay variant */ |
| 4317 | free_all_queued(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4318 | sdebug_ndelay = ndelay; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4319 | sdebug_jdelay = ndelay ? JDELAY_OVERRIDDEN |
| 4320 | : DEF_JDELAY; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4321 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4322 | block_unblock_all_queues(false); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4323 | } |
| 4324 | return res; |
| 4325 | } |
| 4326 | return -EINVAL; |
| 4327 | } |
| 4328 | static DRIVER_ATTR_RW(ndelay); |
| 4329 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4330 | static ssize_t opts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4332 | return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | } |
| 4334 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4335 | static ssize_t opts_store(struct device_driver *ddp, const char *buf, |
| 4336 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4337 | { |
| 4338 | int opts; |
| 4339 | char work[20]; |
| 4340 | |
| 4341 | if (1 == sscanf(buf, "%10s", work)) { |
Rasmus Villemoes | 48a9687 | 2014-10-13 15:54:44 -0700 | [diff] [blame] | 4342 | if (0 == strncasecmp(work,"0x", 2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | if (1 == sscanf(&work[2], "%x", &opts)) |
| 4344 | goto opts_done; |
| 4345 | } else { |
| 4346 | if (1 == sscanf(work, "%d", &opts)) |
| 4347 | goto opts_done; |
| 4348 | } |
| 4349 | } |
| 4350 | return -EINVAL; |
| 4351 | opts_done: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4352 | sdebug_opts = opts; |
| 4353 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); |
| 4354 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4355 | tweak_cmnd_count(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | return count; |
| 4357 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4358 | static DRIVER_ATTR_RW(opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4359 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4360 | static ssize_t ptype_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4362 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4364 | static ssize_t ptype_store(struct device_driver *ddp, const char *buf, |
| 4365 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4366 | { |
| 4367 | int n; |
| 4368 | |
| 4369 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4370 | sdebug_ptype = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | return count; |
| 4372 | } |
| 4373 | return -EINVAL; |
| 4374 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4375 | static DRIVER_ATTR_RW(ptype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4377 | static ssize_t dsense_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4378 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4379 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4380 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4381 | static ssize_t dsense_store(struct device_driver *ddp, const char *buf, |
| 4382 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | { |
| 4384 | int n; |
| 4385 | |
| 4386 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4387 | sdebug_dsense = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | return count; |
| 4389 | } |
| 4390 | return -EINVAL; |
| 4391 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4392 | static DRIVER_ATTR_RW(dsense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4393 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4394 | static ssize_t fake_rw_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4395 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4396 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4397 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4398 | static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf, |
| 4399 | size_t count) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4400 | { |
| 4401 | int n; |
| 4402 | |
| 4403 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4404 | n = (n > 0); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4405 | sdebug_fake_rw = (sdebug_fake_rw > 0); |
| 4406 | if (sdebug_fake_rw != n) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4407 | if ((0 == n) && (NULL == fake_storep)) { |
| 4408 | unsigned long sz = |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4409 | (unsigned long)sdebug_dev_size_mb * |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4410 | 1048576; |
| 4411 | |
| 4412 | fake_storep = vmalloc(sz); |
| 4413 | if (NULL == fake_storep) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4414 | pr_err("out of memory, 9\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4415 | return -ENOMEM; |
| 4416 | } |
| 4417 | memset(fake_storep, 0, sz); |
| 4418 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4419 | sdebug_fake_rw = n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4420 | } |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4421 | return count; |
| 4422 | } |
| 4423 | return -EINVAL; |
| 4424 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4425 | static DRIVER_ATTR_RW(fake_rw); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4426 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4427 | 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] | 4428 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4429 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4430 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4431 | static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf, |
| 4432 | size_t count) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4433 | { |
| 4434 | int n; |
| 4435 | |
| 4436 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4437 | sdebug_no_lun_0 = n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4438 | return count; |
| 4439 | } |
| 4440 | return -EINVAL; |
| 4441 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4442 | static DRIVER_ATTR_RW(no_lun_0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4443 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4444 | static ssize_t num_tgts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4445 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4446 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4448 | static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf, |
| 4449 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | { |
| 4451 | int n; |
| 4452 | |
| 4453 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4454 | sdebug_num_tgts = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | sdebug_max_tgts_luns(); |
| 4456 | return count; |
| 4457 | } |
| 4458 | return -EINVAL; |
| 4459 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4460 | static DRIVER_ATTR_RW(num_tgts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4462 | 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] | 4463 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4464 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4466 | static DRIVER_ATTR_RO(dev_size_mb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4467 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4468 | static ssize_t num_parts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4470 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4472 | static DRIVER_ATTR_RO(num_parts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4474 | static ssize_t every_nth_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4476 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4478 | static ssize_t every_nth_store(struct device_driver *ddp, const char *buf, |
| 4479 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | { |
| 4481 | int nth; |
| 4482 | |
| 4483 | if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4484 | sdebug_every_nth = nth; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4485 | if (nth && !sdebug_statistics) { |
| 4486 | pr_info("every_nth needs statistics=1, set it\n"); |
| 4487 | sdebug_statistics = true; |
| 4488 | } |
| 4489 | tweak_cmnd_count(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | return count; |
| 4491 | } |
| 4492 | return -EINVAL; |
| 4493 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4494 | static DRIVER_ATTR_RW(every_nth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4496 | static ssize_t max_luns_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4498 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4500 | static ssize_t max_luns_store(struct device_driver *ddp, const char *buf, |
| 4501 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | { |
| 4503 | int n; |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 4504 | bool changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | |
| 4506 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 4507 | if (n > 256) { |
| 4508 | pr_warn("max_luns can be no more than 256\n"); |
| 4509 | return -EINVAL; |
| 4510 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4511 | changed = (sdebug_max_luns != n); |
| 4512 | sdebug_max_luns = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4513 | sdebug_max_tgts_luns(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4514 | if (changed && (sdebug_scsi_level >= 5)) { /* >= SPC-3 */ |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 4515 | struct sdebug_host_info *sdhp; |
| 4516 | struct sdebug_dev_info *dp; |
| 4517 | |
| 4518 | spin_lock(&sdebug_host_list_lock); |
| 4519 | list_for_each_entry(sdhp, &sdebug_host_list, |
| 4520 | host_list) { |
| 4521 | list_for_each_entry(dp, &sdhp->dev_info_list, |
| 4522 | dev_list) { |
| 4523 | set_bit(SDEBUG_UA_LUNS_CHANGED, |
| 4524 | dp->uas_bm); |
| 4525 | } |
| 4526 | } |
| 4527 | spin_unlock(&sdebug_host_list_lock); |
| 4528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | return count; |
| 4530 | } |
| 4531 | return -EINVAL; |
| 4532 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4533 | static DRIVER_ATTR_RW(max_luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4535 | static ssize_t max_queue_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4536 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4537 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4538 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4539 | /* N.B. max_queue can be changed while there are queued commands. In flight |
| 4540 | * commands beyond the new max_queue will be completed. */ |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4541 | static ssize_t max_queue_store(struct device_driver *ddp, const char *buf, |
| 4542 | size_t count) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4543 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4544 | int j, n, k, a; |
| 4545 | struct sdebug_queue *sqp; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4546 | |
| 4547 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) && |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4548 | (n <= SDEBUG_CANQUEUE)) { |
| 4549 | block_unblock_all_queues(true); |
| 4550 | k = 0; |
| 4551 | for (j = 0, sqp = sdebug_q_arr; j < submit_queues; |
| 4552 | ++j, ++sqp) { |
| 4553 | a = find_last_bit(sqp->in_use_bm, SDEBUG_CANQUEUE); |
| 4554 | if (a > k) |
| 4555 | k = a; |
| 4556 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4557 | sdebug_max_queue = n; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4558 | if (k == SDEBUG_CANQUEUE) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4559 | atomic_set(&retired_max_queue, 0); |
| 4560 | else if (k >= n) |
| 4561 | atomic_set(&retired_max_queue, k + 1); |
| 4562 | else |
| 4563 | atomic_set(&retired_max_queue, 0); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4564 | block_unblock_all_queues(false); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4565 | return count; |
| 4566 | } |
| 4567 | return -EINVAL; |
| 4568 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4569 | static DRIVER_ATTR_RW(max_queue); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4570 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4571 | static ssize_t no_uld_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4572 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4573 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4574 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4575 | static DRIVER_ATTR_RO(no_uld); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4576 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4577 | static ssize_t scsi_level_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4579 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4581 | static DRIVER_ATTR_RO(scsi_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4583 | static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4584 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4585 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4586 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4587 | static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf, |
| 4588 | size_t count) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4589 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4590 | int n; |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4591 | bool changed; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4592 | |
| 4593 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4594 | changed = (sdebug_virtual_gb != n); |
| 4595 | sdebug_virtual_gb = n; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4596 | sdebug_capacity = get_sdebug_capacity(); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4597 | if (changed) { |
| 4598 | struct sdebug_host_info *sdhp; |
| 4599 | struct sdebug_dev_info *dp; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4600 | |
Ewan D. Milne | 4bc6b63 | 2014-12-04 11:49:26 -0500 | [diff] [blame] | 4601 | spin_lock(&sdebug_host_list_lock); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4602 | list_for_each_entry(sdhp, &sdebug_host_list, |
| 4603 | host_list) { |
| 4604 | list_for_each_entry(dp, &sdhp->dev_info_list, |
| 4605 | dev_list) { |
| 4606 | set_bit(SDEBUG_UA_CAPACITY_CHANGED, |
| 4607 | dp->uas_bm); |
| 4608 | } |
| 4609 | } |
Ewan D. Milne | 4bc6b63 | 2014-12-04 11:49:26 -0500 | [diff] [blame] | 4610 | spin_unlock(&sdebug_host_list_lock); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4611 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4612 | return count; |
| 4613 | } |
| 4614 | return -EINVAL; |
| 4615 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4616 | static DRIVER_ATTR_RW(virtual_gb); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4617 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4618 | static ssize_t add_host_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4619 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4620 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | } |
| 4622 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4623 | static int sdebug_add_adapter(void); |
| 4624 | static void sdebug_remove_adapter(void); |
| 4625 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4626 | static ssize_t add_host_store(struct device_driver *ddp, const char *buf, |
| 4627 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | { |
FUJITA Tomonori | f3df41c | 2008-03-20 11:09:15 +0900 | [diff] [blame] | 4629 | int delta_hosts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | |
FUJITA Tomonori | f3df41c | 2008-03-20 11:09:15 +0900 | [diff] [blame] | 4631 | if (sscanf(buf, "%d", &delta_hosts) != 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4632 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | if (delta_hosts > 0) { |
| 4634 | do { |
| 4635 | sdebug_add_adapter(); |
| 4636 | } while (--delta_hosts); |
| 4637 | } else if (delta_hosts < 0) { |
| 4638 | do { |
| 4639 | sdebug_remove_adapter(); |
| 4640 | } while (++delta_hosts); |
| 4641 | } |
| 4642 | return count; |
| 4643 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4644 | static DRIVER_ATTR_RW(add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4646 | 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] | 4647 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4648 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4649 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4650 | static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf, |
| 4651 | size_t count) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4652 | { |
| 4653 | int n; |
| 4654 | |
| 4655 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4656 | sdebug_vpd_use_hostno = n; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4657 | return count; |
| 4658 | } |
| 4659 | return -EINVAL; |
| 4660 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4661 | static DRIVER_ATTR_RW(vpd_use_hostno); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4662 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4663 | static ssize_t statistics_show(struct device_driver *ddp, char *buf) |
| 4664 | { |
| 4665 | return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_statistics); |
| 4666 | } |
| 4667 | static ssize_t statistics_store(struct device_driver *ddp, const char *buf, |
| 4668 | size_t count) |
| 4669 | { |
| 4670 | int n; |
| 4671 | |
| 4672 | if ((count > 0) && (sscanf(buf, "%d", &n) == 1) && (n >= 0)) { |
| 4673 | if (n > 0) |
| 4674 | sdebug_statistics = true; |
| 4675 | else { |
| 4676 | clear_queue_stats(); |
| 4677 | sdebug_statistics = false; |
| 4678 | } |
| 4679 | return count; |
| 4680 | } |
| 4681 | return -EINVAL; |
| 4682 | } |
| 4683 | static DRIVER_ATTR_RW(statistics); |
| 4684 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4685 | 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] | 4686 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4687 | return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size); |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4688 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4689 | static DRIVER_ATTR_RO(sector_size); |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4690 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4691 | static ssize_t submit_queues_show(struct device_driver *ddp, char *buf) |
| 4692 | { |
| 4693 | return scnprintf(buf, PAGE_SIZE, "%d\n", submit_queues); |
| 4694 | } |
| 4695 | static DRIVER_ATTR_RO(submit_queues); |
| 4696 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4697 | static ssize_t dix_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4698 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4699 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4700 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4701 | static DRIVER_ATTR_RO(dix); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4702 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4703 | static ssize_t dif_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4704 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4705 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4706 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4707 | static DRIVER_ATTR_RO(dif); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4708 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4709 | static ssize_t guard_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4710 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4711 | return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4712 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4713 | static DRIVER_ATTR_RO(guard); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4714 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4715 | static ssize_t ato_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4716 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4717 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4718 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4719 | static DRIVER_ATTR_RO(ato); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4720 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4721 | static ssize_t map_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4722 | { |
| 4723 | ssize_t count; |
| 4724 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4725 | if (!scsi_debug_lbp()) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4726 | return scnprintf(buf, PAGE_SIZE, "0-%u\n", |
| 4727 | sdebug_store_sectors); |
| 4728 | |
Tejun Heo | c7badc9 | 2015-02-13 14:37:51 -0800 | [diff] [blame] | 4729 | count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", |
| 4730 | (int)map_size, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4731 | buf[count++] = '\n'; |
Tejun Heo | c7badc9 | 2015-02-13 14:37:51 -0800 | [diff] [blame] | 4732 | buf[count] = '\0'; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4733 | |
| 4734 | return count; |
| 4735 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4736 | static DRIVER_ATTR_RO(map); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4737 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4738 | static ssize_t removable_show(struct device_driver *ddp, char *buf) |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4739 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4740 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4741 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4742 | static ssize_t removable_store(struct device_driver *ddp, const char *buf, |
| 4743 | size_t count) |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4744 | { |
| 4745 | int n; |
| 4746 | |
| 4747 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4748 | sdebug_removable = (n > 0); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4749 | return count; |
| 4750 | } |
| 4751 | return -EINVAL; |
| 4752 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4753 | static DRIVER_ATTR_RW(removable); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4754 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4755 | static ssize_t host_lock_show(struct device_driver *ddp, char *buf) |
| 4756 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4757 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4758 | } |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 4759 | /* N.B. sdebug_host_lock does nothing, kept for backward compatibility */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4760 | static ssize_t host_lock_store(struct device_driver *ddp, const char *buf, |
| 4761 | size_t count) |
| 4762 | { |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 4763 | int n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4764 | |
| 4765 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 4766 | sdebug_host_lock = (n > 0); |
| 4767 | return count; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4768 | } |
| 4769 | return -EINVAL; |
| 4770 | } |
| 4771 | static DRIVER_ATTR_RW(host_lock); |
| 4772 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4773 | static ssize_t strict_show(struct device_driver *ddp, char *buf) |
| 4774 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4775 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4776 | } |
| 4777 | static ssize_t strict_store(struct device_driver *ddp, const char *buf, |
| 4778 | size_t count) |
| 4779 | { |
| 4780 | int n; |
| 4781 | |
| 4782 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4783 | sdebug_strict = (n > 0); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4784 | return count; |
| 4785 | } |
| 4786 | return -EINVAL; |
| 4787 | } |
| 4788 | static DRIVER_ATTR_RW(strict); |
| 4789 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4790 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4791 | /* Note: The following array creates attribute files in the |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4792 | /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these |
| 4793 | files (over those found in the /sys/module/scsi_debug/parameters |
| 4794 | directory) is that auxiliary actions can be triggered when an attribute |
| 4795 | is changed. For example see: sdebug_add_host_store() above. |
| 4796 | */ |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4797 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4798 | static struct attribute *sdebug_drv_attrs[] = { |
| 4799 | &driver_attr_delay.attr, |
| 4800 | &driver_attr_opts.attr, |
| 4801 | &driver_attr_ptype.attr, |
| 4802 | &driver_attr_dsense.attr, |
| 4803 | &driver_attr_fake_rw.attr, |
| 4804 | &driver_attr_no_lun_0.attr, |
| 4805 | &driver_attr_num_tgts.attr, |
| 4806 | &driver_attr_dev_size_mb.attr, |
| 4807 | &driver_attr_num_parts.attr, |
| 4808 | &driver_attr_every_nth.attr, |
| 4809 | &driver_attr_max_luns.attr, |
| 4810 | &driver_attr_max_queue.attr, |
| 4811 | &driver_attr_no_uld.attr, |
| 4812 | &driver_attr_scsi_level.attr, |
| 4813 | &driver_attr_virtual_gb.attr, |
| 4814 | &driver_attr_add_host.attr, |
| 4815 | &driver_attr_vpd_use_hostno.attr, |
| 4816 | &driver_attr_sector_size.attr, |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4817 | &driver_attr_statistics.attr, |
| 4818 | &driver_attr_submit_queues.attr, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4819 | &driver_attr_dix.attr, |
| 4820 | &driver_attr_dif.attr, |
| 4821 | &driver_attr_guard.attr, |
| 4822 | &driver_attr_ato.attr, |
| 4823 | &driver_attr_map.attr, |
| 4824 | &driver_attr_removable.attr, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4825 | &driver_attr_host_lock.attr, |
| 4826 | &driver_attr_ndelay.attr, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4827 | &driver_attr_strict.attr, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4828 | NULL, |
| 4829 | }; |
| 4830 | ATTRIBUTE_GROUPS(sdebug_drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | |
Akinobu Mita | 11ddcec | 2014-02-26 22:56:59 +0900 | [diff] [blame] | 4832 | static struct device *pseudo_primary; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 4833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | static int __init scsi_debug_init(void) |
| 4835 | { |
FUJITA Tomonori | 5f2578e | 2008-03-30 00:59:57 +0900 | [diff] [blame] | 4836 | unsigned long sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4837 | int host_to_add; |
| 4838 | int k; |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4839 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4841 | atomic_set(&retired_max_queue, 0); |
| 4842 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4843 | if (sdebug_ndelay >= 1000 * 1000 * 1000) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4844 | pr_warn("ndelay must be less than 1 second, ignored\n"); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4845 | sdebug_ndelay = 0; |
| 4846 | } else if (sdebug_ndelay > 0) |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4847 | sdebug_jdelay = JDELAY_OVERRIDDEN; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4848 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4849 | switch (sdebug_sector_size) { |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4850 | case 512: |
| 4851 | case 1024: |
| 4852 | case 2048: |
| 4853 | case 4096: |
| 4854 | break; |
| 4855 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4856 | pr_err("invalid sector_size %d\n", sdebug_sector_size); |
Martin K. Petersen | 597136a | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4857 | return -EINVAL; |
| 4858 | } |
| 4859 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4860 | switch (sdebug_dif) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4861 | |
| 4862 | case SD_DIF_TYPE0_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4863 | break; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4864 | case SD_DIF_TYPE1_PROTECTION: |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 4865 | case SD_DIF_TYPE2_PROTECTION: |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4866 | case SD_DIF_TYPE3_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4867 | have_dif_prot = true; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4868 | break; |
| 4869 | |
| 4870 | default: |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4871 | pr_err("dif must be 0, 1, 2 or 3\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4872 | return -EINVAL; |
| 4873 | } |
| 4874 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4875 | if (sdebug_guard > 1) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4876 | pr_err("guard must be 0 or 1\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4877 | return -EINVAL; |
| 4878 | } |
| 4879 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4880 | if (sdebug_ato > 1) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4881 | pr_err("ato must be 0 or 1\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4882 | return -EINVAL; |
| 4883 | } |
| 4884 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4885 | if (sdebug_physblk_exp > 15) { |
| 4886 | pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 4887 | return -EINVAL; |
| 4888 | } |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame] | 4889 | if (sdebug_max_luns > 256) { |
| 4890 | pr_warn("max_luns can be no more than 256, use default\n"); |
| 4891 | sdebug_max_luns = DEF_MAX_LUNS; |
| 4892 | } |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 4893 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4894 | if (sdebug_lowest_aligned > 0x3fff) { |
| 4895 | pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 4896 | return -EINVAL; |
| 4897 | } |
| 4898 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4899 | if (submit_queues < 1) { |
| 4900 | pr_err("submit_queues must be 1 or more\n"); |
| 4901 | return -EINVAL; |
| 4902 | } |
| 4903 | sdebug_q_arr = kcalloc(submit_queues, sizeof(struct sdebug_queue), |
| 4904 | GFP_KERNEL); |
| 4905 | if (sdebug_q_arr == NULL) |
| 4906 | return -ENOMEM; |
| 4907 | for (k = 0; k < submit_queues; ++k) |
| 4908 | spin_lock_init(&sdebug_q_arr[k].qc_lock); |
| 4909 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4910 | if (sdebug_dev_size_mb < 1) |
| 4911 | sdebug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */ |
| 4912 | sz = (unsigned long)sdebug_dev_size_mb * 1048576; |
| 4913 | sdebug_store_sectors = sz / sdebug_sector_size; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4914 | sdebug_capacity = get_sdebug_capacity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | |
| 4916 | /* play around with geometry, don't waste too much on track 0 */ |
| 4917 | sdebug_heads = 8; |
| 4918 | sdebug_sectors_per = 32; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4919 | if (sdebug_dev_size_mb >= 256) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4920 | sdebug_heads = 64; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4921 | else if (sdebug_dev_size_mb >= 16) |
Andy Shevchenko | fa785f0 | 2015-11-26 20:22:50 +0200 | [diff] [blame] | 4922 | sdebug_heads = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | sdebug_cylinders_per = (unsigned long)sdebug_capacity / |
| 4924 | (sdebug_sectors_per * sdebug_heads); |
| 4925 | if (sdebug_cylinders_per >= 1024) { |
| 4926 | /* other LLDs do this; implies >= 1GB ram disk ... */ |
| 4927 | sdebug_heads = 255; |
| 4928 | sdebug_sectors_per = 63; |
| 4929 | sdebug_cylinders_per = (unsigned long)sdebug_capacity / |
| 4930 | (sdebug_sectors_per * sdebug_heads); |
| 4931 | } |
| 4932 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4933 | if (sdebug_fake_rw == 0) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4934 | fake_storep = vmalloc(sz); |
| 4935 | if (NULL == fake_storep) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4936 | pr_err("out of memory, 1\n"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4937 | ret = -ENOMEM; |
| 4938 | goto free_q_arr; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4939 | } |
| 4940 | memset(fake_storep, 0, sz); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4941 | if (sdebug_num_parts > 0) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4942 | sdebug_build_parts(fake_storep, sz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4943 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4945 | if (sdebug_dix) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4946 | int dif_size; |
| 4947 | |
| 4948 | dif_size = sdebug_store_sectors * sizeof(struct sd_dif_tuple); |
| 4949 | dif_storep = vmalloc(dif_size); |
| 4950 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4951 | 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] | 4952 | |
| 4953 | if (dif_storep == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4954 | pr_err("out of mem. (DIX)\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4955 | ret = -ENOMEM; |
| 4956 | goto free_vm; |
| 4957 | } |
| 4958 | |
| 4959 | memset(dif_storep, 0xff, dif_size); |
| 4960 | } |
| 4961 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4962 | /* Logical Block Provisioning */ |
| 4963 | if (scsi_debug_lbp()) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4964 | sdebug_unmap_max_blocks = |
| 4965 | clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 4966 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4967 | sdebug_unmap_max_desc = |
| 4968 | clamp(sdebug_unmap_max_desc, 0U, 256U); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 4969 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4970 | sdebug_unmap_granularity = |
| 4971 | clamp(sdebug_unmap_granularity, 1U, 0xffffffffU); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 4972 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4973 | if (sdebug_unmap_alignment && |
| 4974 | sdebug_unmap_granularity <= |
| 4975 | sdebug_unmap_alignment) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4976 | pr_err("ERR: unmap_granularity <= unmap_alignment\n"); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 4977 | ret = -EINVAL; |
| 4978 | goto free_vm; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4979 | } |
| 4980 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 4981 | map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1; |
| 4982 | map_storep = vmalloc(BITS_TO_LONGS(map_size) * sizeof(long)); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4983 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4984 | pr_info("%lu provisioning blocks\n", map_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4985 | |
| 4986 | if (map_storep == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4987 | pr_err("out of mem. (MAP)\n"); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4988 | ret = -ENOMEM; |
| 4989 | goto free_vm; |
| 4990 | } |
| 4991 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 4992 | bitmap_zero(map_storep, map_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4993 | |
| 4994 | /* Map first 1KB for partition table */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4995 | if (sdebug_num_parts) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4996 | map_region(0, 2); |
| 4997 | } |
| 4998 | |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 4999 | pseudo_primary = root_device_register("pseudo_0"); |
| 5000 | if (IS_ERR(pseudo_primary)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5001 | pr_warn("root_device_register() error\n"); |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5002 | ret = PTR_ERR(pseudo_primary); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5003 | goto free_vm; |
| 5004 | } |
| 5005 | ret = bus_register(&pseudo_lld_bus); |
| 5006 | if (ret < 0) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5007 | pr_warn("bus_register error: %d\n", ret); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5008 | goto dev_unreg; |
| 5009 | } |
| 5010 | ret = driver_register(&sdebug_driverfs_driver); |
| 5011 | if (ret < 0) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5012 | pr_warn("driver_register error: %d\n", ret); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5013 | goto bus_unreg; |
| 5014 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5016 | host_to_add = sdebug_add_host; |
| 5017 | sdebug_add_host = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | |
| 5019 | for (k = 0; k < host_to_add; k++) { |
| 5020 | if (sdebug_add_adapter()) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5021 | pr_err("sdebug_add_adapter failed k=%d\n", k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5022 | break; |
| 5023 | } |
| 5024 | } |
| 5025 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5026 | if (sdebug_verbose) |
| 5027 | pr_info("built %d host(s)\n", sdebug_add_host); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5028 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 | return 0; |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5030 | |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5031 | bus_unreg: |
| 5032 | bus_unregister(&pseudo_lld_bus); |
| 5033 | dev_unreg: |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5034 | root_device_unregister(pseudo_primary); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5035 | free_vm: |
Tomas Winkler | de232af | 2015-07-28 16:54:22 +0300 | [diff] [blame] | 5036 | vfree(map_storep); |
| 5037 | vfree(dif_storep); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5038 | vfree(fake_storep); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5039 | free_q_arr: |
| 5040 | kfree(sdebug_q_arr); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 5041 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5042 | } |
| 5043 | |
| 5044 | static void __exit scsi_debug_exit(void) |
| 5045 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5046 | int k = sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5047 | |
| 5048 | stop_all_queued(); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5049 | free_all_queued(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | for (; k; k--) |
| 5051 | sdebug_remove_adapter(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5052 | driver_unregister(&sdebug_driverfs_driver); |
| 5053 | bus_unregister(&pseudo_lld_bus); |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5054 | root_device_unregister(pseudo_primary); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5055 | |
Tomas Winkler | de232af | 2015-07-28 16:54:22 +0300 | [diff] [blame] | 5056 | vfree(dif_storep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | vfree(fake_storep); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5058 | kfree(sdebug_q_arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | } |
| 5060 | |
| 5061 | device_initcall(scsi_debug_init); |
| 5062 | module_exit(scsi_debug_exit); |
| 5063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | static void sdebug_release_adapter(struct device * dev) |
| 5065 | { |
| 5066 | struct sdebug_host_info *sdbg_host; |
| 5067 | |
| 5068 | sdbg_host = to_sdebug_host(dev); |
| 5069 | kfree(sdbg_host); |
| 5070 | } |
| 5071 | |
| 5072 | static int sdebug_add_adapter(void) |
| 5073 | { |
| 5074 | int k, devs_per_host; |
| 5075 | int error = 0; |
| 5076 | struct sdebug_host_info *sdbg_host; |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5077 | struct sdebug_dev_info *sdbg_devinfo, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 5079 | sdbg_host = kzalloc(sizeof(*sdbg_host),GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | if (NULL == sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5081 | pr_err("out of memory at line %d\n", __LINE__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5082 | return -ENOMEM; |
| 5083 | } |
| 5084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | INIT_LIST_HEAD(&sdbg_host->dev_info_list); |
| 5086 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5087 | devs_per_host = sdebug_num_tgts * sdebug_max_luns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5088 | for (k = 0; k < devs_per_host; k++) { |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 5089 | sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL); |
| 5090 | if (!sdbg_devinfo) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5091 | pr_err("out of memory at line %d\n", __LINE__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5092 | error = -ENOMEM; |
| 5093 | goto clean; |
| 5094 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | spin_lock(&sdebug_host_list_lock); |
| 5098 | list_add_tail(&sdbg_host->host_list, &sdebug_host_list); |
| 5099 | spin_unlock(&sdebug_host_list_lock); |
| 5100 | |
| 5101 | sdbg_host->dev.bus = &pseudo_lld_bus; |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 5102 | sdbg_host->dev.parent = pseudo_primary; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5103 | sdbg_host->dev.release = &sdebug_release_adapter; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5104 | dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | |
| 5106 | error = device_register(&sdbg_host->dev); |
| 5107 | |
| 5108 | if (error) |
| 5109 | goto clean; |
| 5110 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5111 | ++sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | return error; |
| 5113 | |
| 5114 | clean: |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5115 | list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, |
| 5116 | dev_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | list_del(&sdbg_devinfo->dev_list); |
| 5118 | kfree(sdbg_devinfo); |
| 5119 | } |
| 5120 | |
| 5121 | kfree(sdbg_host); |
| 5122 | return error; |
| 5123 | } |
| 5124 | |
| 5125 | static void sdebug_remove_adapter(void) |
| 5126 | { |
| 5127 | struct sdebug_host_info * sdbg_host = NULL; |
| 5128 | |
| 5129 | spin_lock(&sdebug_host_list_lock); |
| 5130 | if (!list_empty(&sdebug_host_list)) { |
| 5131 | sdbg_host = list_entry(sdebug_host_list.prev, |
| 5132 | struct sdebug_host_info, host_list); |
| 5133 | list_del(&sdbg_host->host_list); |
| 5134 | } |
| 5135 | spin_unlock(&sdebug_host_list_lock); |
| 5136 | |
| 5137 | if (!sdbg_host) |
| 5138 | return; |
| 5139 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5140 | device_unregister(&sdbg_host->dev); |
| 5141 | --sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | } |
| 5143 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 5144 | static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5145 | { |
| 5146 | int num_in_q = 0; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5147 | struct sdebug_dev_info *devip; |
| 5148 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5149 | block_unblock_all_queues(true); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5150 | devip = (struct sdebug_dev_info *)sdev->hostdata; |
| 5151 | if (NULL == devip) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5152 | block_unblock_all_queues(false); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5153 | return -ENODEV; |
| 5154 | } |
| 5155 | num_in_q = atomic_read(&devip->num_in_q); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5156 | |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5157 | if (qdepth < 1) |
| 5158 | qdepth = 1; |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5159 | /* allow to exceed max host qc_arr elements for testing */ |
| 5160 | if (qdepth > SDEBUG_CANQUEUE + 10) |
| 5161 | qdepth = SDEBUG_CANQUEUE + 10; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 5162 | scsi_change_queue_depth(sdev, qdepth); |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5163 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5164 | if (SDEBUG_OPT_Q_NOISE & sdebug_opts) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5165 | 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] | 5166 | __func__, qdepth, num_in_q); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5167 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5168 | block_unblock_all_queues(false); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5169 | return sdev->queue_depth; |
| 5170 | } |
| 5171 | |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5172 | static bool fake_timeout(struct scsi_cmnd *scp) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5173 | { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5174 | if (0 == (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5175 | if (sdebug_every_nth < -1) |
| 5176 | sdebug_every_nth = -1; |
| 5177 | if (SDEBUG_OPT_TIMEOUT & sdebug_opts) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5178 | return true; /* ignore command causing timeout */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5179 | else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts && |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5180 | scsi_medium_access_command(scp)) |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5181 | return true; /* time out reads and writes */ |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5182 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5183 | return false; |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5184 | } |
| 5185 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 5186 | static int scsi_debug_queuecommand(struct Scsi_Host *shost, |
| 5187 | struct scsi_cmnd *scp) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5188 | { |
| 5189 | u8 sdeb_i; |
| 5190 | struct scsi_device *sdp = scp->device; |
| 5191 | const struct opcode_info_t *oip; |
| 5192 | const struct opcode_info_t *r_oip; |
| 5193 | struct sdebug_dev_info *devip; |
| 5194 | u8 *cmd = scp->cmnd; |
| 5195 | int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 5196 | int k, na; |
| 5197 | int errsts = 0; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5198 | u32 flags; |
| 5199 | u16 sa; |
| 5200 | u8 opcode = cmd[0]; |
| 5201 | bool has_wlun_rl; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5202 | |
| 5203 | scsi_set_resid(scp, 0); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5204 | if (sdebug_statistics) |
| 5205 | atomic_inc(&sdebug_cmnd_count); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5206 | if (unlikely(sdebug_verbose && |
| 5207 | !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5208 | char b[120]; |
| 5209 | int n, len, sb; |
| 5210 | |
| 5211 | len = scp->cmd_len; |
| 5212 | sb = (int)sizeof(b); |
| 5213 | if (len > 32) |
| 5214 | strcpy(b, "too long, over 32 bytes"); |
| 5215 | else { |
| 5216 | for (k = 0, n = 0; k < len && n < sb; ++k) |
| 5217 | n += scnprintf(b + n, sb - n, "%02x ", |
| 5218 | (u32)cmd[k]); |
| 5219 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5220 | if (sdebug_mq_active) |
| 5221 | sdev_printk(KERN_INFO, sdp, "%s: tag=%u, cmd %s\n", |
| 5222 | my_name, blk_mq_unique_tag(scp->request), |
| 5223 | b); |
| 5224 | else |
| 5225 | sdev_printk(KERN_INFO, sdp, "%s: cmd %s\n", my_name, |
| 5226 | b); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5227 | } |
Tomas Winkler | 34d5543 | 2015-07-28 16:54:21 +0300 | [diff] [blame] | 5228 | has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5229 | if (unlikely((sdp->lun >= sdebug_max_luns) && !has_wlun_rl)) |
| 5230 | goto err_out; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5231 | |
| 5232 | sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */ |
| 5233 | oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */ |
| 5234 | devip = (struct sdebug_dev_info *)sdp->hostdata; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5235 | if (unlikely(!devip)) { |
| 5236 | devip = find_build_dev_info(sdp); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5237 | if (NULL == devip) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5238 | goto err_out; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5239 | } |
| 5240 | na = oip->num_attached; |
| 5241 | r_pfp = oip->pfp; |
| 5242 | if (na) { /* multiple commands with this opcode */ |
| 5243 | r_oip = oip; |
| 5244 | if (FF_SA & r_oip->flags) { |
| 5245 | if (F_SA_LOW & oip->flags) |
| 5246 | sa = 0x1f & cmd[1]; |
| 5247 | else |
| 5248 | sa = get_unaligned_be16(cmd + 8); |
| 5249 | for (k = 0; k <= na; oip = r_oip->arrp + k++) { |
| 5250 | if (opcode == oip->opcode && sa == oip->sa) |
| 5251 | break; |
| 5252 | } |
| 5253 | } else { /* since no service action only check opcode */ |
| 5254 | for (k = 0; k <= na; oip = r_oip->arrp + k++) { |
| 5255 | if (opcode == oip->opcode) |
| 5256 | break; |
| 5257 | } |
| 5258 | } |
| 5259 | if (k > na) { |
| 5260 | if (F_SA_LOW & r_oip->flags) |
| 5261 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4); |
| 5262 | else if (F_SA_HIGH & r_oip->flags) |
| 5263 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7); |
| 5264 | else |
| 5265 | mk_sense_invalid_opcode(scp); |
| 5266 | goto check_cond; |
| 5267 | } |
| 5268 | } /* else (when na==0) we assume the oip is a match */ |
| 5269 | flags = oip->flags; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5270 | if (unlikely(F_INV_OP & flags)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5271 | mk_sense_invalid_opcode(scp); |
| 5272 | goto check_cond; |
| 5273 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5274 | if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5275 | if (sdebug_verbose) |
| 5276 | sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n", |
| 5277 | my_name, opcode, " supported for wlun"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5278 | mk_sense_invalid_opcode(scp); |
| 5279 | goto check_cond; |
| 5280 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5281 | if (unlikely(sdebug_strict)) { /* check cdb against mask */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5282 | u8 rem; |
| 5283 | int j; |
| 5284 | |
| 5285 | for (k = 1; k < oip->len_mask[0] && k < 16; ++k) { |
| 5286 | rem = ~oip->len_mask[k] & cmd[k]; |
| 5287 | if (rem) { |
| 5288 | for (j = 7; j >= 0; --j, rem <<= 1) { |
| 5289 | if (0x80 & rem) |
| 5290 | break; |
| 5291 | } |
| 5292 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j); |
| 5293 | goto check_cond; |
| 5294 | } |
| 5295 | } |
| 5296 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5297 | if (unlikely(!(F_SKIP_UA & flags) && |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 5298 | find_first_bit(devip->uas_bm, |
| 5299 | SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5300 | errsts = make_ua(scp, devip); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5301 | if (errsts) |
| 5302 | goto check_cond; |
| 5303 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5304 | if (unlikely((F_M_ACCESS & flags) && atomic_read(&devip->stopped))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5305 | mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5306 | if (sdebug_verbose) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5307 | sdev_printk(KERN_INFO, sdp, "%s reports: Not ready: " |
| 5308 | "%s\n", my_name, "initializing command " |
| 5309 | "required"); |
| 5310 | errsts = check_condition_result; |
| 5311 | goto fini; |
| 5312 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5313 | if (sdebug_fake_rw && (F_FAKE_RW & flags)) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5314 | goto fini; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5315 | if (unlikely(sdebug_every_nth)) { |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5316 | if (fake_timeout(scp)) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5317 | return 0; /* ignore command: make trouble */ |
| 5318 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5319 | if (likely(oip->pfp)) |
| 5320 | errsts = oip->pfp(scp, devip); /* calls a resp_* function */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5321 | else if (r_pfp) /* if leaf function ptr NULL, try the root's */ |
| 5322 | errsts = r_pfp(scp, devip); |
| 5323 | |
| 5324 | fini: |
| 5325 | return schedule_resp(scp, devip, errsts, |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 5326 | ((F_DELAY_OVERR & flags) ? 0 : sdebug_jdelay)); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5327 | check_cond: |
| 5328 | return schedule_resp(scp, devip, check_condition_result, 0); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5329 | err_out: |
| 5330 | return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, 0); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5331 | } |
| 5332 | |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5333 | static struct scsi_host_template sdebug_driver_template = { |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 5334 | .show_info = scsi_debug_show_info, |
| 5335 | .write_info = scsi_debug_write_info, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5336 | .proc_name = sdebug_proc_name, |
| 5337 | .name = "SCSI DEBUG", |
| 5338 | .info = scsi_debug_info, |
| 5339 | .slave_alloc = scsi_debug_slave_alloc, |
| 5340 | .slave_configure = scsi_debug_slave_configure, |
| 5341 | .slave_destroy = scsi_debug_slave_destroy, |
| 5342 | .ioctl = scsi_debug_ioctl, |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 5343 | .queuecommand = scsi_debug_queuecommand, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5344 | .change_queue_depth = sdebug_change_qdepth, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5345 | .eh_abort_handler = scsi_debug_abort, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5346 | .eh_device_reset_handler = scsi_debug_device_reset, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5347 | .eh_target_reset_handler = scsi_debug_target_reset, |
| 5348 | .eh_bus_reset_handler = scsi_debug_bus_reset, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5349 | .eh_host_reset_handler = scsi_debug_host_reset, |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5350 | .can_queue = SDEBUG_CANQUEUE, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5351 | .this_id = 7, |
Ming Lin | 65e8617 | 2016-04-04 14:48:10 -0700 | [diff] [blame] | 5352 | .sg_tablesize = SG_MAX_SEGMENTS, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5353 | .cmd_per_lun = DEF_CMD_PER_LUN, |
Akinobu Mita | 6bb5e6e | 2014-06-02 22:56:49 +0900 | [diff] [blame] | 5354 | .max_sectors = -1U, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5355 | .use_clustering = DISABLE_CLUSTERING, |
| 5356 | .module = THIS_MODULE, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5357 | .track_queue_depth = 1, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5358 | }; |
| 5359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | static int sdebug_driver_probe(struct device * dev) |
| 5361 | { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 5362 | int error = 0; |
| 5363 | struct sdebug_host_info *sdbg_host; |
| 5364 | struct Scsi_Host *hpnt; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5365 | int hprot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | |
| 5367 | sdbg_host = to_sdebug_host(dev); |
| 5368 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5369 | sdebug_driver_template.can_queue = sdebug_max_queue; |
| 5370 | if (sdebug_clustering) |
Akinobu Mita | 0759c66 | 2014-02-26 22:57:04 +0900 | [diff] [blame] | 5371 | sdebug_driver_template.use_clustering = ENABLE_CLUSTERING; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5372 | hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host)); |
| 5373 | if (NULL == hpnt) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5374 | pr_err("scsi_host_alloc failed\n"); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5375 | error = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5376 | return error; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5377 | } |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5378 | if (submit_queues > nr_cpu_ids) { |
| 5379 | pr_warn("%s: trim submit_queues (was %d) to nr_cpu_ids=%d\n", |
| 5380 | my_name, submit_queues, nr_cpu_ids); |
| 5381 | submit_queues = nr_cpu_ids; |
| 5382 | } |
| 5383 | /* Decide whether to tell scsi subsystem that we want mq */ |
| 5384 | /* Following should give the same answer for each host */ |
| 5385 | sdebug_mq_active = shost_use_blk_mq(hpnt) && (submit_queues > 1); |
| 5386 | if (sdebug_mq_active) |
| 5387 | hpnt->nr_hw_queues = submit_queues; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | |
| 5389 | sdbg_host->shost = hpnt; |
| 5390 | *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5391 | if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id)) |
| 5392 | hpnt->max_id = sdebug_num_tgts + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5394 | hpnt->max_id = sdebug_num_tgts; |
| 5395 | /* = sdebug_max_luns; */ |
Tomas Winkler | f2d3fd2 | 2015-07-28 16:54:25 +0300 | [diff] [blame] | 5396 | hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5398 | hprot = 0; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5399 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5400 | switch (sdebug_dif) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5401 | |
| 5402 | case SD_DIF_TYPE1_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5403 | hprot = SHOST_DIF_TYPE1_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5404 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5405 | hprot |= SHOST_DIX_TYPE1_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5406 | break; |
| 5407 | |
| 5408 | case SD_DIF_TYPE2_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5409 | hprot = SHOST_DIF_TYPE2_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5410 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5411 | hprot |= SHOST_DIX_TYPE2_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5412 | break; |
| 5413 | |
| 5414 | case SD_DIF_TYPE3_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5415 | hprot = SHOST_DIF_TYPE3_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5416 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5417 | hprot |= SHOST_DIX_TYPE3_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5418 | break; |
| 5419 | |
| 5420 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5421 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5422 | hprot |= SHOST_DIX_TYPE0_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5423 | break; |
| 5424 | } |
| 5425 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5426 | scsi_host_set_prot(hpnt, hprot); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5427 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5428 | if (have_dif_prot || sdebug_dix) |
| 5429 | pr_info("host protection%s%s%s%s%s%s%s\n", |
| 5430 | (hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "", |
| 5431 | (hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "", |
| 5432 | (hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "", |
| 5433 | (hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "", |
| 5434 | (hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "", |
| 5435 | (hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "", |
| 5436 | (hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : ""); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5437 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5438 | if (sdebug_guard == 1) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5439 | scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP); |
| 5440 | else |
| 5441 | scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC); |
| 5442 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5443 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts); |
| 5444 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & sdebug_opts); |
Douglas Gilbert | c483739 | 2016-05-06 00:40:26 -0400 | [diff] [blame] | 5445 | if (sdebug_every_nth) /* need stats counters for every_nth */ |
| 5446 | sdebug_statistics = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5447 | error = scsi_add_host(hpnt, &sdbg_host->dev); |
| 5448 | if (error) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5449 | pr_err("scsi_add_host failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | error = -ENODEV; |
| 5451 | scsi_host_put(hpnt); |
| 5452 | } else |
| 5453 | scsi_scan_host(hpnt); |
| 5454 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5455 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | static int sdebug_driver_remove(struct device * dev) |
| 5459 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | struct sdebug_host_info *sdbg_host; |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5461 | struct sdebug_dev_info *sdbg_devinfo, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5462 | |
| 5463 | sdbg_host = to_sdebug_host(dev); |
| 5464 | |
| 5465 | if (!sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5466 | pr_err("Unable to locate host info\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | return -ENODEV; |
| 5468 | } |
| 5469 | |
| 5470 | scsi_remove_host(sdbg_host->shost); |
| 5471 | |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5472 | list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, |
| 5473 | dev_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | list_del(&sdbg_devinfo->dev_list); |
| 5475 | kfree(sdbg_devinfo); |
| 5476 | } |
| 5477 | |
| 5478 | scsi_host_put(sdbg_host->shost); |
| 5479 | return 0; |
| 5480 | } |
| 5481 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5482 | static int pseudo_lld_bus_match(struct device *dev, |
| 5483 | struct device_driver *dev_driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | { |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5485 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5487 | |
| 5488 | static struct bus_type pseudo_lld_bus = { |
| 5489 | .name = "pseudo", |
| 5490 | .match = pseudo_lld_bus_match, |
| 5491 | .probe = sdebug_driver_probe, |
| 5492 | .remove = sdebug_driver_remove, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5493 | .drv_groups = sdebug_drv_groups, |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5494 | }; |