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> |
| 27 | #include <linux/timer.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 */ |
| 62 | #define SCSI_DEBUG_VERSION "1.86" |
| 63 | static const char *sdebug_version_date = "20160422"; |
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 | |
| 99 | /* Default values for driver parameters */ |
| 100 | #define DEF_NUM_HOST 1 |
| 101 | #define DEF_NUM_TGTS 1 |
| 102 | #define DEF_MAX_LUNS 1 |
| 103 | /* With these defaults, this driver will make 1 host with 1 target |
| 104 | * (id 0) containing 1 logical unit (lun 0). That is 1 device. |
| 105 | */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 106 | #define DEF_ATO 1 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 107 | #define DEF_DELAY 1 /* if > 0 unit is a jiffy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | #define DEF_DEV_SIZE_MB 8 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 109 | #define DEF_DIF 0 |
| 110 | #define DEF_DIX 0 |
| 111 | #define DEF_D_SENSE 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | #define DEF_EVERY_NTH 0 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 113 | #define DEF_FAKE_RW 0 |
| 114 | #define DEF_GUARD 0 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 115 | #define DEF_HOST_LOCK 0 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 116 | #define DEF_LBPU 0 |
| 117 | #define DEF_LBPWS 0 |
| 118 | #define DEF_LBPWS10 0 |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 119 | #define DEF_LBPRZ 1 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 120 | #define DEF_LOWEST_ALIGNED 0 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 121 | #define DEF_NDELAY 0 /* if > 0 unit is a nanosecond */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 122 | #define DEF_NO_LUN_0 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | #define DEF_NUM_PARTS 0 |
| 124 | #define DEF_OPTS 0 |
Martin K. Petersen | 32c5844 | 2015-12-16 17:53:51 -0500 | [diff] [blame] | 125 | #define DEF_OPT_BLKS 1024 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 126 | #define DEF_PHYSBLK_EXP 0 |
| 127 | #define DEF_PTYPE 0 |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 128 | #define DEF_REMOVABLE false |
Douglas Gilbert | e46b034 | 2014-08-05 12:21:53 +0200 | [diff] [blame] | 129 | #define DEF_SCSI_LEVEL 6 /* INQUIRY, byte2 [6->SPC-4] */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 130 | #define DEF_SECTOR_SIZE 512 |
| 131 | #define DEF_UNMAP_ALIGNMENT 0 |
| 132 | #define DEF_UNMAP_GRANULARITY 1 |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 133 | #define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF |
| 134 | #define DEF_UNMAP_MAX_DESC 256 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 135 | #define DEF_VIRTUAL_GB 0 |
| 136 | #define DEF_VPD_USE_HOSTNO 1 |
| 137 | #define DEF_WRITESAME_LENGTH 0xFFFF |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 138 | #define DEF_STRICT 0 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 139 | #define DELAY_OVERRIDDEN -9999 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 141 | /* bit mask values for sdebug_opts */ |
| 142 | #define SDEBUG_OPT_NOISE 1 |
| 143 | #define SDEBUG_OPT_MEDIUM_ERR 2 |
| 144 | #define SDEBUG_OPT_TIMEOUT 4 |
| 145 | #define SDEBUG_OPT_RECOVERED_ERR 8 |
| 146 | #define SDEBUG_OPT_TRANSPORT_ERR 16 |
| 147 | #define SDEBUG_OPT_DIF_ERR 32 |
| 148 | #define SDEBUG_OPT_DIX_ERR 64 |
| 149 | #define SDEBUG_OPT_MAC_TIMEOUT 128 |
| 150 | #define SDEBUG_OPT_SHORT_TRANSFER 0x100 |
| 151 | #define SDEBUG_OPT_Q_NOISE 0x200 |
| 152 | #define SDEBUG_OPT_ALL_TSF 0x400 |
| 153 | #define SDEBUG_OPT_RARE_TSF 0x800 |
| 154 | #define SDEBUG_OPT_N_WCE 0x1000 |
| 155 | #define SDEBUG_OPT_RESET_NOISE 0x2000 |
| 156 | #define SDEBUG_OPT_NO_CDB_NOISE 0x4000 |
| 157 | #define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \ |
| 158 | SDEBUG_OPT_RESET_NOISE) |
| 159 | #define SDEBUG_OPT_ALL_INJECTING (SDEBUG_OPT_RECOVERED_ERR | \ |
| 160 | SDEBUG_OPT_TRANSPORT_ERR | \ |
| 161 | SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR | \ |
| 162 | SDEBUG_OPT_SHORT_TRANSFER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /* When "every_nth" > 0 then modulo "every_nth" commands: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 164 | * - a no response is simulated if SDEBUG_OPT_TIMEOUT is set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | * - a RECOVERED_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 166 | * commands if SDEBUG_OPT_RECOVERED_ERR is set. |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 167 | * - a TRANSPORT_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 168 | * commands if SDEBUG_OPT_TRANSPORT_ERR is set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | * |
| 170 | * When "every_nth" < 0 then after "- every_nth" commands: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 171 | * - a no response is simulated if SDEBUG_OPT_TIMEOUT is set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | * - a RECOVERED_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 173 | * commands if SDEBUG_OPT_RECOVERED_ERR is set. |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 174 | * - a TRANSPORT_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 175 | * commands if _DEBUG_OPT_TRANSPORT_ERR is set. |
| 176 | * This will continue on every subsequent command until some other action |
| 177 | * occurs (e.g. the user * writing a new value (other than -1 or 1) to |
| 178 | * every_nth via sysfs). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | */ |
| 180 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 181 | /* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs)are returned in |
| 182 | * priority order. In the subset implemented here lower numbers have higher |
| 183 | * priority. The UA numbers should be a sequence starting from 0 with |
| 184 | * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */ |
| 185 | #define SDEBUG_UA_POR 0 /* Power on, reset, or bus device reset */ |
| 186 | #define SDEBUG_UA_BUS_RESET 1 |
| 187 | #define SDEBUG_UA_MODE_CHANGED 2 |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 188 | #define SDEBUG_UA_CAPACITY_CHANGED 3 |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 189 | #define SDEBUG_UA_LUNS_CHANGED 4 |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 190 | #define SDEBUG_UA_MICROCODE_CHANGED 5 /* simulate firmware change */ |
| 191 | #define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 6 |
| 192 | #define SDEBUG_NUM_UAS 7 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 193 | |
| 194 | /* for check_readiness() */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 195 | #define UAS_ONLY 1 /* check for UAs only */ |
| 196 | #define UAS_TUR 0 /* if no UAs then check if media access possible */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 197 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 198 | /* 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] | 199 | * sector on read commands: */ |
| 200 | #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */ |
Douglas Gilbert | 32f7ef7 | 2011-03-11 10:43:35 -0500 | [diff] [blame] | 201 | #define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1) |
| 204 | * or "peripheral device" addressing (value 0) */ |
| 205 | #define SAM2_LUN_ADDRESS_METHOD 0 |
| 206 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 207 | /* SCSI_DEBUG_CANQUEUE is the maximum number of commands that can be queued |
| 208 | * (for response) at one time. Can be reduced by max_queue option. Command |
| 209 | * responses are not queued when delay=0 and ndelay=0. The per-device |
| 210 | * DEF_CMD_PER_LUN can be changed via sysfs: |
| 211 | * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth but cannot exceed |
| 212 | * SCSI_DEBUG_CANQUEUE. */ |
| 213 | #define SCSI_DEBUG_CANQUEUE_WORDS 9 /* a WORD is bits in a long */ |
| 214 | #define SCSI_DEBUG_CANQUEUE (SCSI_DEBUG_CANQUEUE_WORDS * BITS_PER_LONG) |
| 215 | #define DEF_CMD_PER_LUN 255 |
| 216 | |
| 217 | #if DEF_CMD_PER_LUN > SCSI_DEBUG_CANQUEUE |
| 218 | #warning "Expect DEF_CMD_PER_LUN <= SCSI_DEBUG_CANQUEUE" |
| 219 | #endif |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 220 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 221 | /* SCSI opcodes (first byte of cdb) mapped onto these indexes */ |
| 222 | enum sdeb_opcode_index { |
| 223 | SDEB_I_INVALID_OPCODE = 0, |
| 224 | SDEB_I_INQUIRY = 1, |
| 225 | SDEB_I_REPORT_LUNS = 2, |
| 226 | SDEB_I_REQUEST_SENSE = 3, |
| 227 | SDEB_I_TEST_UNIT_READY = 4, |
| 228 | SDEB_I_MODE_SENSE = 5, /* 6, 10 */ |
| 229 | SDEB_I_MODE_SELECT = 6, /* 6, 10 */ |
| 230 | SDEB_I_LOG_SENSE = 7, |
| 231 | SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */ |
| 232 | SDEB_I_READ = 9, /* 6, 10, 12, 16 */ |
| 233 | SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */ |
| 234 | SDEB_I_START_STOP = 11, |
| 235 | SDEB_I_SERV_ACT_IN = 12, /* 12, 16 */ |
| 236 | SDEB_I_SERV_ACT_OUT = 13, /* 12, 16 */ |
| 237 | SDEB_I_MAINT_IN = 14, |
| 238 | SDEB_I_MAINT_OUT = 15, |
| 239 | SDEB_I_VERIFY = 16, /* 10 only */ |
| 240 | SDEB_I_VARIABLE_LEN = 17, |
| 241 | SDEB_I_RESERVE = 18, /* 6, 10 */ |
| 242 | SDEB_I_RELEASE = 19, /* 6, 10 */ |
| 243 | SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */ |
| 244 | SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */ |
| 245 | SDEB_I_ATA_PT = 22, /* 12, 16 */ |
| 246 | SDEB_I_SEND_DIAG = 23, |
| 247 | SDEB_I_UNMAP = 24, |
| 248 | SDEB_I_XDWRITEREAD = 25, /* 10 only */ |
| 249 | SDEB_I_WRITE_BUFFER = 26, |
| 250 | SDEB_I_WRITE_SAME = 27, /* 10, 16 */ |
| 251 | SDEB_I_SYNC_CACHE = 28, /* 10 only */ |
| 252 | SDEB_I_COMP_WRITE = 29, |
| 253 | SDEB_I_LAST_ELEMENT = 30, /* keep this last */ |
| 254 | }; |
| 255 | |
| 256 | static const unsigned char opcode_ind_arr[256] = { |
| 257 | /* 0x0; 0x0->0x1f: 6 byte cdbs */ |
| 258 | SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE, |
| 259 | 0, 0, 0, 0, |
| 260 | SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0, |
| 261 | 0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE, |
| 262 | SDEB_I_RELEASE, |
| 263 | 0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG, |
| 264 | SDEB_I_ALLOW_REMOVAL, 0, |
| 265 | /* 0x20; 0x20->0x3f: 10 byte cdbs */ |
| 266 | 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0, |
| 267 | SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY, |
| 268 | 0, 0, 0, 0, 0, SDEB_I_SYNC_CACHE, 0, 0, |
| 269 | 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0, |
| 270 | /* 0x40; 0x40->0x5f: 10 byte cdbs */ |
| 271 | 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0, |
| 272 | 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0, |
| 273 | 0, 0, 0, SDEB_I_XDWRITEREAD, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE, |
| 274 | SDEB_I_RELEASE, |
| 275 | 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0, |
| 276 | /* 0x60; 0x60->0x7d are reserved */ |
| 277 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 278 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 279 | 0, SDEB_I_VARIABLE_LEN, |
| 280 | /* 0x80; 0x80->0x9f: 16 byte cdbs */ |
| 281 | 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0, |
| 282 | SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0, 0, 0, 0, 0, |
| 283 | 0, 0, 0, SDEB_I_WRITE_SAME, 0, 0, 0, 0, |
| 284 | 0, 0, 0, 0, 0, 0, SDEB_I_SERV_ACT_IN, SDEB_I_SERV_ACT_OUT, |
| 285 | /* 0xa0; 0xa0->0xbf: 12 byte cdbs */ |
| 286 | SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN, |
| 287 | SDEB_I_MAINT_OUT, 0, 0, 0, |
| 288 | SDEB_I_READ, SDEB_I_SERV_ACT_OUT, SDEB_I_WRITE, SDEB_I_SERV_ACT_IN, |
| 289 | 0, 0, 0, 0, |
| 290 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 291 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 292 | /* 0xc0; 0xc0->0xff: vendor specific */ |
| 293 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 294 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 295 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 296 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 297 | }; |
| 298 | |
| 299 | #define F_D_IN 1 |
| 300 | #define F_D_OUT 2 |
| 301 | #define F_D_OUT_MAYBE 4 /* WRITE SAME, NDOB bit */ |
| 302 | #define F_D_UNKN 8 |
| 303 | #define F_RL_WLUN_OK 0x10 |
| 304 | #define F_SKIP_UA 0x20 |
| 305 | #define F_DELAY_OVERR 0x40 |
| 306 | #define F_SA_LOW 0x80 /* cdb byte 1, bits 4 to 0 */ |
| 307 | #define F_SA_HIGH 0x100 /* as used by variable length cdbs */ |
| 308 | #define F_INV_OP 0x200 |
| 309 | #define F_FAKE_RW 0x400 |
| 310 | #define F_M_ACCESS 0x800 /* media access */ |
| 311 | |
| 312 | #define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR) |
| 313 | #define FF_DIRECT_IO (F_M_ACCESS | F_FAKE_RW) |
| 314 | #define FF_SA (F_SA_HIGH | F_SA_LOW) |
| 315 | |
| 316 | struct sdebug_dev_info; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 317 | static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 318 | static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 319 | static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 320 | static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 321 | static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 322 | static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 323 | static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 324 | static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 325 | static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 326 | static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 327 | static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 328 | static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 329 | static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 330 | static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 331 | static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 332 | static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 333 | static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 334 | static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 335 | static int resp_xdwriteread_10(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 336 | 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] | 337 | static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 338 | |
| 339 | struct opcode_info_t { |
| 340 | u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff |
| 341 | * for terminating element */ |
| 342 | u8 opcode; /* if num_attached > 0, preferred */ |
| 343 | u16 sa; /* service action */ |
| 344 | u32 flags; /* OR-ed set of SDEB_F_* */ |
| 345 | int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 346 | const struct opcode_info_t *arrp; /* num_attached elements or NULL */ |
| 347 | u8 len_mask[16]; /* len=len_mask[0], then mask for cdb[1]... */ |
| 348 | /* ignore cdb bytes after position 15 */ |
| 349 | }; |
| 350 | |
| 351 | static const struct opcode_info_t msense_iarr[1] = { |
| 352 | {0, 0x1a, 0, F_D_IN, NULL, NULL, |
| 353 | {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 354 | }; |
| 355 | |
| 356 | static const struct opcode_info_t mselect_iarr[1] = { |
| 357 | {0, 0x15, 0, F_D_OUT, NULL, NULL, |
| 358 | {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 359 | }; |
| 360 | |
| 361 | static const struct opcode_info_t read_iarr[3] = { |
| 362 | {0, 0x28, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL,/* READ(10) */ |
| 363 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0, |
| 364 | 0, 0, 0, 0} }, |
| 365 | {0, 0x8, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL, /* READ(6) */ |
| 366 | {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 367 | {0, 0xa8, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL,/* READ(12) */ |
| 368 | {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f, |
| 369 | 0xc7, 0, 0, 0, 0} }, |
| 370 | }; |
| 371 | |
| 372 | static const struct opcode_info_t write_iarr[3] = { |
| 373 | {0, 0x2a, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 10 */ |
| 374 | {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0, |
| 375 | 0, 0, 0, 0} }, |
| 376 | {0, 0xa, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 6 */ |
| 377 | {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 378 | {0, 0xaa, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 12 */ |
| 379 | {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f, |
| 380 | 0xc7, 0, 0, 0, 0} }, |
| 381 | }; |
| 382 | |
| 383 | static const struct opcode_info_t sa_in_iarr[1] = { |
| 384 | {0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL, |
| 385 | {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 386 | 0xff, 0xff, 0xff, 0, 0xc7} }, |
| 387 | }; |
| 388 | |
| 389 | static const struct opcode_info_t vl_iarr[1] = { /* VARIABLE LENGTH */ |
| 390 | {0, 0x7f, 0xb, F_SA_HIGH | F_D_OUT | FF_DIRECT_IO, resp_write_dt0, |
| 391 | NULL, {32, 0xc7, 0, 0, 0, 0, 0x1f, 0x18, 0x0, 0xb, 0xfa, |
| 392 | 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */ |
| 393 | }; |
| 394 | |
| 395 | static const struct opcode_info_t maint_in_iarr[2] = { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 396 | {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] | 397 | {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, |
| 398 | 0xc7, 0, 0, 0, 0} }, |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 399 | {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] | 400 | {12, 0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, |
| 401 | 0, 0} }, |
| 402 | }; |
| 403 | |
| 404 | static const struct opcode_info_t write_same_iarr[1] = { |
| 405 | {0, 0x93, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, resp_write_same_16, NULL, |
| 406 | {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 407 | 0xff, 0xff, 0xff, 0x1f, 0xc7} }, |
| 408 | }; |
| 409 | |
| 410 | static const struct opcode_info_t reserve_iarr[1] = { |
| 411 | {0, 0x16, 0, F_D_OUT, NULL, NULL, /* RESERVE(6) */ |
| 412 | {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 413 | }; |
| 414 | |
| 415 | static const struct opcode_info_t release_iarr[1] = { |
| 416 | {0, 0x17, 0, F_D_OUT, NULL, NULL, /* RELEASE(6) */ |
| 417 | {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 418 | }; |
| 419 | |
| 420 | |
| 421 | /* This array is accessed via SDEB_I_* values. Make sure all are mapped, |
| 422 | * plus the terminating elements for logic that scans this table such as |
| 423 | * REPORT SUPPORTED OPERATION CODES. */ |
| 424 | static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEMENT + 1] = { |
| 425 | /* 0 */ |
| 426 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, |
| 427 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 428 | {0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL, |
| 429 | {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 430 | {0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL, |
| 431 | {12, 0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, |
| 432 | 0, 0} }, |
| 433 | {0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL, |
| 434 | {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 435 | {0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */ |
| 436 | {6, 0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 437 | {1, 0x5a, 0, F_D_IN, resp_mode_sense, msense_iarr, |
| 438 | {10, 0xf8, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 439 | 0} }, |
| 440 | {1, 0x55, 0, F_D_OUT, resp_mode_select, mselect_iarr, |
| 441 | {10, 0xf1, 0, 0, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 442 | {0, 0x4d, 0, F_D_IN, resp_log_sense, NULL, |
| 443 | {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, |
| 444 | 0, 0, 0} }, |
| 445 | {0, 0x25, 0, F_D_IN, resp_readcap, NULL, |
| 446 | {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0, |
| 447 | 0, 0} }, |
| 448 | {3, 0x88, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, read_iarr, |
| 449 | {16, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 450 | 0xff, 0xff, 0xff, 0x9f, 0xc7} }, /* READ(16) */ |
| 451 | /* 10 */ |
| 452 | {3, 0x8a, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, write_iarr, |
| 453 | {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 454 | 0xff, 0xff, 0xff, 0x9f, 0xc7} }, /* WRITE(16) */ |
| 455 | {0, 0x1b, 0, 0, resp_start_stop, NULL, /* START STOP UNIT */ |
| 456 | {6, 0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 457 | {1, 0x9e, 0x10, F_SA_LOW | F_D_IN, resp_readcap16, sa_in_iarr, |
| 458 | {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 459 | 0xff, 0xff, 0xff, 0x1, 0xc7} }, /* READ CAPACITY(16) */ |
| 460 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* SA OUT */ |
| 461 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 462 | {2, 0xa3, 0xa, F_SA_LOW | F_D_IN, resp_report_tgtpgs, maint_in_iarr, |
| 463 | {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, 0, |
| 464 | 0} }, |
| 465 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */ |
| 466 | {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] | 467 | {0, 0x2f, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, NULL, NULL, /* VERIFY(10) */ |
| 468 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, |
| 469 | 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 470 | {1, 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_DIRECT_IO, resp_read_dt0, |
| 471 | vl_iarr, {32, 0xc7, 0, 0, 0, 0, 0x1f, 0x18, 0x0, 0x9, 0xfe, 0, |
| 472 | 0xff, 0xff, 0xff, 0xff} },/* VARIABLE LENGTH, READ(32) */ |
| 473 | {1, 0x56, 0, F_D_OUT, NULL, reserve_iarr, /* RESERVE(10) */ |
| 474 | {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 475 | 0} }, |
| 476 | {1, 0x57, 0, F_D_OUT, NULL, release_iarr, /* RELEASE(10) */ |
| 477 | {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 478 | 0} }, |
| 479 | /* 20 */ |
Douglas Gilbert | f7f9f26 | 2015-11-22 12:11:28 -0500 | [diff] [blame] | 480 | {0, 0x1e, 0, 0, NULL, NULL, /* ALLOW REMOVAL */ |
| 481 | {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] | 482 | {0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */ |
| 483 | {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 484 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */ |
| 485 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 486 | {0, 0x1d, F_D_OUT, 0, NULL, NULL, /* SEND DIAGNOSTIC */ |
| 487 | {6, 0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 488 | {0, 0x42, 0, F_D_OUT | FF_DIRECT_IO, resp_unmap, NULL, /* UNMAP */ |
| 489 | {10, 0x1, 0, 0, 0, 0, 0x1f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 490 | {0, 0x53, 0, F_D_IN | F_D_OUT | FF_DIRECT_IO, resp_xdwriteread_10, |
| 491 | NULL, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, |
| 492 | 0, 0, 0, 0, 0, 0} }, |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 493 | {0, 0x3b, 0, F_D_OUT_MAYBE, resp_write_buffer, NULL, |
| 494 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, |
| 495 | 0, 0, 0, 0} }, /* WRITE_BUFFER */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 496 | {1, 0x41, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, resp_write_same_10, |
| 497 | write_same_iarr, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, |
| 498 | 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 499 | {0, 0x35, 0, F_DELAY_OVERR | FF_DIRECT_IO, NULL, NULL, /* SYNC_CACHE */ |
| 500 | {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0, |
| 501 | 0, 0, 0, 0} }, |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 502 | {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] | 503 | {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, |
| 504 | 0, 0xff, 0x1f, 0xc7} }, /* COMPARE AND WRITE */ |
| 505 | |
| 506 | /* 30 */ |
| 507 | {0xff, 0, 0, 0, NULL, NULL, /* terminating element */ |
| 508 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 509 | }; |
| 510 | |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 511 | struct sdebug_scmd_extra_t { |
| 512 | bool inj_recovered; |
| 513 | bool inj_transport; |
| 514 | bool inj_dif; |
| 515 | bool inj_dix; |
| 516 | bool inj_short; |
| 517 | }; |
| 518 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 519 | static int sdebug_add_host = DEF_NUM_HOST; |
| 520 | static int sdebug_ato = DEF_ATO; |
| 521 | static int sdebug_delay = DEF_DELAY; |
| 522 | static int sdebug_dev_size_mb = DEF_DEV_SIZE_MB; |
| 523 | static int sdebug_dif = DEF_DIF; |
| 524 | static int sdebug_dix = DEF_DIX; |
| 525 | static int sdebug_dsense = DEF_D_SENSE; |
| 526 | static int sdebug_every_nth = DEF_EVERY_NTH; |
| 527 | static int sdebug_fake_rw = DEF_FAKE_RW; |
| 528 | static unsigned int sdebug_guard = DEF_GUARD; |
| 529 | static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED; |
| 530 | static int sdebug_max_luns = DEF_MAX_LUNS; |
| 531 | static int sdebug_max_queue = SCSI_DEBUG_CANQUEUE; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 532 | static atomic_t retired_max_queue; /* if > 0 then was prior max_queue */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 533 | static int sdebug_ndelay = DEF_NDELAY; |
| 534 | static int sdebug_no_lun_0 = DEF_NO_LUN_0; |
| 535 | static int sdebug_no_uld; |
| 536 | static int sdebug_num_parts = DEF_NUM_PARTS; |
| 537 | static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */ |
| 538 | static int sdebug_opt_blks = DEF_OPT_BLKS; |
| 539 | static int sdebug_opts = DEF_OPTS; |
| 540 | static int sdebug_physblk_exp = DEF_PHYSBLK_EXP; |
| 541 | static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral type (0==disk) */ |
| 542 | static int sdebug_scsi_level = DEF_SCSI_LEVEL; |
| 543 | static int sdebug_sector_size = DEF_SECTOR_SIZE; |
| 544 | static int sdebug_virtual_gb = DEF_VIRTUAL_GB; |
| 545 | static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO; |
| 546 | static unsigned int sdebug_lbpu = DEF_LBPU; |
| 547 | static unsigned int sdebug_lbpws = DEF_LBPWS; |
| 548 | static unsigned int sdebug_lbpws10 = DEF_LBPWS10; |
| 549 | static unsigned int sdebug_lbprz = DEF_LBPRZ; |
| 550 | static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT; |
| 551 | static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY; |
| 552 | static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS; |
| 553 | static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC; |
| 554 | static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH; |
| 555 | static bool sdebug_removable = DEF_REMOVABLE; |
| 556 | static bool sdebug_clustering; |
| 557 | static bool sdebug_host_lock = DEF_HOST_LOCK; |
| 558 | static bool sdebug_strict = DEF_STRICT; |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 559 | static bool sdebug_any_injecting_opt; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 560 | static bool sdebug_verbose; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 562 | static atomic_t sdebug_cmnd_count; |
| 563 | static atomic_t sdebug_completions; |
| 564 | static atomic_t sdebug_a_tsf; /* counter of 'almost' TSFs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
| 566 | #define DEV_READONLY(TGT) (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 568 | static unsigned int sdebug_store_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | static sector_t sdebug_capacity; /* in sectors */ |
| 570 | |
| 571 | /* old BIOS stuff, kernel may get rid of them but some mode sense pages |
| 572 | may still need them */ |
| 573 | static int sdebug_heads; /* heads per disk */ |
| 574 | static int sdebug_cylinders_per; /* cylinders per surface */ |
| 575 | static int sdebug_sectors_per; /* sectors per cylinder */ |
| 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | #define SDEBUG_MAX_PARTS 4 |
| 578 | |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 579 | #define SCSI_DEBUG_MAX_CMD_LEN 32 |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 580 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 581 | static unsigned int scsi_debug_lbp(void) |
| 582 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 583 | return 0 == sdebug_fake_rw && |
| 584 | (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 585 | } |
| 586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | struct sdebug_dev_info { |
| 588 | struct list_head dev_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | unsigned int channel; |
| 590 | unsigned int target; |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 591 | u64 lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | struct sdebug_host_info *sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 593 | unsigned long uas_bm[1]; |
| 594 | atomic_t num_in_q; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 595 | char stopped; /* TODO: should be atomic */ |
| 596 | bool used; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | }; |
| 598 | |
| 599 | struct sdebug_host_info { |
| 600 | struct list_head host_list; |
| 601 | struct Scsi_Host *shost; |
| 602 | struct device dev; |
| 603 | struct list_head dev_info_list; |
| 604 | }; |
| 605 | |
| 606 | #define to_sdebug_host(d) \ |
| 607 | container_of(d, struct sdebug_host_info, dev) |
| 608 | |
| 609 | static LIST_HEAD(sdebug_host_list); |
| 610 | static DEFINE_SPINLOCK(sdebug_host_list_lock); |
| 611 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 612 | |
| 613 | struct sdebug_hrtimer { /* ... is derived from hrtimer */ |
| 614 | struct hrtimer hrt; /* must be first element */ |
| 615 | int qa_indx; |
| 616 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
| 618 | struct sdebug_queued_cmd { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 619 | /* in_use flagged by a bit in queued_in_use_bm[] */ |
| 620 | struct timer_list *cmnd_timerp; |
| 621 | struct tasklet_struct *tletp; |
| 622 | struct sdebug_hrtimer *sd_hrtp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | struct scsi_cmnd * a_cmnd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | }; |
| 625 | static struct sdebug_queued_cmd queued_arr[SCSI_DEBUG_CANQUEUE]; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 626 | static unsigned long queued_in_use_bm[SCSI_DEBUG_CANQUEUE_WORDS]; |
| 627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | static unsigned char * fake_storep; /* ramdisk storage */ |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 630 | static struct sd_dif_tuple *dif_storep; /* protection info */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 631 | static void *map_storep; /* provisioning map */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 633 | static unsigned long map_size; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 634 | static int num_aborts; |
| 635 | static int num_dev_resets; |
| 636 | static int num_target_resets; |
| 637 | static int num_bus_resets; |
| 638 | static int num_host_resets; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 639 | static int dix_writes; |
| 640 | static int dix_reads; |
| 641 | static int dif_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
| 643 | static DEFINE_SPINLOCK(queued_arr_lock); |
| 644 | static DEFINE_RWLOCK(atomic_rw); |
| 645 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 646 | static char sdebug_proc_name[] = MY_NAME; |
| 647 | static const char *my_name = MY_NAME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | static struct bus_type pseudo_lld_bus; |
| 650 | |
| 651 | static struct device_driver sdebug_driverfs_driver = { |
| 652 | .name = sdebug_proc_name, |
| 653 | .bus = &pseudo_lld_bus, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | }; |
| 655 | |
| 656 | static const int check_condition_result = |
| 657 | (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
| 658 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 659 | static const int illegal_condition_result = |
| 660 | (DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION; |
| 661 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 662 | static const int device_qfull_result = |
| 663 | (DID_OK << 16) | (COMMAND_COMPLETE << 8) | SAM_STAT_TASK_SET_FULL; |
| 664 | |
| 665 | static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0, |
| 666 | 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, |
| 667 | 0, 0, 0, 0}; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 668 | static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0, |
| 669 | 0, 0, 0x2, 0x4b}; |
| 670 | static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, |
| 671 | 0, 0, 0x0, 0x0}; |
| 672 | |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 673 | static void *fake_store(unsigned long long lba) |
| 674 | { |
| 675 | lba = do_div(lba, sdebug_store_sectors); |
| 676 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 677 | return fake_storep + lba * sdebug_sector_size; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | static struct sd_dif_tuple *dif_store(sector_t sector) |
| 681 | { |
Arnd Bergmann | 4941311 | 2015-11-20 17:38:28 +0100 | [diff] [blame] | 682 | sector = sector_div(sector, sdebug_store_sectors); |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 683 | |
| 684 | return dif_storep + sector; |
| 685 | } |
| 686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | static int sdebug_add_adapter(void); |
| 688 | static void sdebug_remove_adapter(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 690 | static void sdebug_max_tgts_luns(void) |
| 691 | { |
| 692 | struct sdebug_host_info *sdbg_host; |
| 693 | struct Scsi_Host *hpnt; |
| 694 | |
| 695 | spin_lock(&sdebug_host_list_lock); |
| 696 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
| 697 | hpnt = sdbg_host->shost; |
| 698 | if ((hpnt->this_id >= 0) && |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 699 | (sdebug_num_tgts > hpnt->this_id)) |
| 700 | hpnt->max_id = sdebug_num_tgts + 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 701 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 702 | hpnt->max_id = sdebug_num_tgts; |
| 703 | /* sdebug_max_luns; */ |
Tomas Winkler | f2d3fd2 | 2015-07-28 16:54:25 +0300 | [diff] [blame] | 704 | hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 705 | } |
| 706 | spin_unlock(&sdebug_host_list_lock); |
| 707 | } |
| 708 | |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 709 | enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1}; |
| 710 | |
| 711 | /* Set in_bit to -1 to indicate no bit position of invalid field */ |
| 712 | static void |
| 713 | mk_sense_invalid_fld(struct scsi_cmnd *scp, enum sdeb_cmd_data c_d, |
| 714 | int in_byte, int in_bit) |
| 715 | { |
| 716 | unsigned char *sbuff; |
| 717 | u8 sks[4]; |
| 718 | int sl, asc; |
| 719 | |
| 720 | sbuff = scp->sense_buffer; |
| 721 | if (!sbuff) { |
| 722 | sdev_printk(KERN_ERR, scp->device, |
| 723 | "%s: sense_buffer is NULL\n", __func__); |
| 724 | return; |
| 725 | } |
| 726 | asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST; |
| 727 | memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 728 | scsi_build_sense_buffer(sdebug_dsense, sbuff, ILLEGAL_REQUEST, asc, 0); |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 729 | memset(sks, 0, sizeof(sks)); |
| 730 | sks[0] = 0x80; |
| 731 | if (c_d) |
| 732 | sks[0] |= 0x40; |
| 733 | if (in_bit >= 0) { |
| 734 | sks[0] |= 0x8; |
| 735 | sks[0] |= 0x7 & in_bit; |
| 736 | } |
| 737 | put_unaligned_be16(in_byte, sks + 1); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 738 | if (sdebug_dsense) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 739 | sl = sbuff[7] + 8; |
| 740 | sbuff[7] = sl; |
| 741 | sbuff[sl] = 0x2; |
| 742 | sbuff[sl + 1] = 0x6; |
| 743 | memcpy(sbuff + sl + 4, sks, 3); |
| 744 | } else |
| 745 | memcpy(sbuff + 15, sks, 3); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 746 | if (sdebug_verbose) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 747 | sdev_printk(KERN_INFO, scp->device, "%s: [sense_key,asc,ascq" |
| 748 | "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n", |
| 749 | my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit); |
| 750 | } |
| 751 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 752 | 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] | 753 | { |
| 754 | unsigned char *sbuff; |
| 755 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 756 | sbuff = scp->sense_buffer; |
| 757 | if (!sbuff) { |
| 758 | sdev_printk(KERN_ERR, scp->device, |
| 759 | "%s: sense_buffer is NULL\n", __func__); |
| 760 | return; |
| 761 | } |
| 762 | memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE); |
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 | scsi_build_sense_buffer(sdebug_dsense, sbuff, key, asc, asq); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 765 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 766 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 767 | sdev_printk(KERN_INFO, scp->device, |
| 768 | "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n", |
| 769 | my_name, key, asc, asq); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 772 | static void |
| 773 | mk_sense_invalid_opcode(struct scsi_cmnd *scp) |
| 774 | { |
| 775 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0); |
| 776 | } |
| 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg) |
| 779 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 780 | if (sdebug_verbose) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 781 | if (0x1261 == cmd) |
| 782 | sdev_printk(KERN_INFO, dev, |
| 783 | "%s: BLKFLSBUF [0x1261]\n", __func__); |
| 784 | else if (0x5331 == cmd) |
| 785 | sdev_printk(KERN_INFO, dev, |
| 786 | "%s: CDROM_GET_CAPABILITY [0x5331]\n", |
| 787 | __func__); |
| 788 | else |
| 789 | sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n", |
| 790 | __func__, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
| 792 | return -EINVAL; |
| 793 | /* return -ENOTTY; // correct return but upsets fdisk */ |
| 794 | } |
| 795 | |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 796 | static void clear_luns_changed_on_target(struct sdebug_dev_info *devip) |
| 797 | { |
| 798 | struct sdebug_host_info *sdhp; |
| 799 | struct sdebug_dev_info *dp; |
| 800 | |
| 801 | spin_lock(&sdebug_host_list_lock); |
| 802 | list_for_each_entry(sdhp, &sdebug_host_list, host_list) { |
| 803 | list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) { |
| 804 | if ((devip->sdbg_host == dp->sdbg_host) && |
| 805 | (devip->target == dp->target)) |
| 806 | clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm); |
| 807 | } |
| 808 | } |
| 809 | spin_unlock(&sdebug_host_list_lock); |
| 810 | } |
| 811 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 812 | static int check_readiness(struct scsi_cmnd *SCpnt, int uas_only, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 813 | struct sdebug_dev_info * devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 815 | int k; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 816 | |
| 817 | k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS); |
| 818 | if (k != SDEBUG_NUM_UAS) { |
| 819 | const char *cp = NULL; |
| 820 | |
| 821 | switch (k) { |
| 822 | case SDEBUG_UA_POR: |
| 823 | mk_sense_buffer(SCpnt, UNIT_ATTENTION, |
| 824 | UA_RESET_ASC, POWER_ON_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 825 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 826 | cp = "power on reset"; |
| 827 | break; |
| 828 | case SDEBUG_UA_BUS_RESET: |
| 829 | mk_sense_buffer(SCpnt, UNIT_ATTENTION, |
| 830 | UA_RESET_ASC, BUS_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 831 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 832 | cp = "bus reset"; |
| 833 | break; |
| 834 | case SDEBUG_UA_MODE_CHANGED: |
| 835 | mk_sense_buffer(SCpnt, UNIT_ATTENTION, |
| 836 | UA_CHANGED_ASC, MODE_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 837 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 838 | cp = "mode parameters changed"; |
| 839 | break; |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 840 | case SDEBUG_UA_CAPACITY_CHANGED: |
| 841 | mk_sense_buffer(SCpnt, UNIT_ATTENTION, |
| 842 | UA_CHANGED_ASC, CAPACITY_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 843 | if (sdebug_verbose) |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 844 | cp = "capacity data changed"; |
Ewan D. Milne | f49accf | 2014-12-04 11:49:25 -0500 | [diff] [blame] | 845 | break; |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 846 | case SDEBUG_UA_MICROCODE_CHANGED: |
| 847 | mk_sense_buffer(SCpnt, UNIT_ATTENTION, |
| 848 | TARGET_CHANGED_ASC, MICROCODE_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 849 | if (sdebug_verbose) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 850 | cp = "microcode has been changed"; |
| 851 | break; |
| 852 | case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET: |
| 853 | mk_sense_buffer(SCpnt, UNIT_ATTENTION, |
| 854 | TARGET_CHANGED_ASC, |
| 855 | MICROCODE_CHANGED_WO_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 856 | if (sdebug_verbose) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 857 | cp = "microcode has been changed without reset"; |
| 858 | break; |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 859 | case SDEBUG_UA_LUNS_CHANGED: |
| 860 | /* |
| 861 | * SPC-3 behavior is to report a UNIT ATTENTION with |
| 862 | * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN |
| 863 | * on the target, until a REPORT LUNS command is |
| 864 | * received. SPC-4 behavior is to report it only once. |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 865 | * NOTE: sdebug_scsi_level does not use the same |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 866 | * values as struct scsi_device->scsi_level. |
| 867 | */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 868 | if (sdebug_scsi_level >= 6) /* SPC-4 and above */ |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 869 | clear_luns_changed_on_target(devip); |
| 870 | mk_sense_buffer(SCpnt, UNIT_ATTENTION, |
| 871 | TARGET_CHANGED_ASC, |
| 872 | LUNS_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 873 | if (sdebug_verbose) |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 874 | cp = "reported luns data has changed"; |
| 875 | break; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 876 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 877 | pr_warn("unexpected unit attention code=%d\n", k); |
| 878 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 879 | cp = "unknown"; |
| 880 | break; |
| 881 | } |
| 882 | clear_bit(k, devip->uas_bm); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 883 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 884 | sdev_printk(KERN_INFO, SCpnt->device, |
| 885 | "%s reports: Unit attention: %s\n", |
| 886 | my_name, cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | return check_condition_result; |
| 888 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 889 | if ((UAS_TUR == uas_only) && devip->stopped) { |
| 890 | mk_sense_buffer(SCpnt, NOT_READY, LOGICAL_UNIT_NOT_READY, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 891 | 0x2); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 892 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 893 | sdev_printk(KERN_INFO, SCpnt->device, |
| 894 | "%s reports: Not ready: %s\n", my_name, |
| 895 | "initializing command required"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 896 | return check_condition_result; |
| 897 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | /* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */ |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 902 | 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] | 903 | int arr_len) |
| 904 | { |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 905 | int act_len; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 906 | struct scsi_data_buffer *sdb = scsi_in(scp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 908 | if (!sdb->length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | return 0; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 910 | if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE)) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 911 | return DID_ERROR << 16; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 912 | |
| 913 | act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents, |
| 914 | arr, arr_len); |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 915 | sdb->resid = scsi_bufflen(scp) - act_len; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | return 0; |
| 918 | } |
| 919 | |
| 920 | /* Returns number of bytes fetched into 'arr' or -1 if error. */ |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 921 | static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr, |
| 922 | int arr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | { |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 924 | if (!scsi_bufflen(scp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | return 0; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 926 | if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_TO_DEVICE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | return -1; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 928 | |
| 929 | return scsi_sg_copy_to_buffer(scp, arr, arr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | |
| 933 | static const char * inq_vendor_id = "Linux "; |
| 934 | static const char * inq_product_id = "scsi_debug "; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 935 | static const char *inq_product_rev = "0186"; /* version less '.' */ |
| 936 | static const u64 naa5_comp_a = 0x5222222000000000ULL; |
| 937 | static const u64 naa5_comp_b = 0x5333333000000000ULL; |
| 938 | static const u64 naa5_comp_c = 0x5111111000000000ULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 940 | /* Device identification VPD page. Returns number of bytes placed in arr */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 941 | static int inquiry_evpd_83(unsigned char * arr, int port_group_id, |
| 942 | int target_dev_id, int dev_id_num, |
| 943 | const char * dev_id_str, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 944 | int dev_id_str_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 946 | int num, port_a; |
| 947 | char b[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 949 | port_a = target_dev_id + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | /* T10 vendor identifier field format (faked) */ |
| 951 | arr[0] = 0x2; /* ASCII */ |
| 952 | arr[1] = 0x1; |
| 953 | arr[2] = 0x0; |
| 954 | memcpy(&arr[4], inq_vendor_id, 8); |
| 955 | memcpy(&arr[12], inq_product_id, 16); |
| 956 | memcpy(&arr[28], dev_id_str, dev_id_str_len); |
| 957 | num = 8 + 16 + dev_id_str_len; |
| 958 | arr[3] = num; |
| 959 | num += 4; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 960 | if (dev_id_num >= 0) { |
| 961 | /* NAA-5, Logical unit identifier (binary) */ |
| 962 | arr[num++] = 0x1; /* binary (not necessarily sas) */ |
| 963 | arr[num++] = 0x3; /* PIV=0, lu, naa */ |
| 964 | arr[num++] = 0x0; |
| 965 | arr[num++] = 0x8; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 966 | put_unaligned_be64(naa5_comp_b + dev_id_num, arr + num); |
| 967 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 968 | /* Target relative port number */ |
| 969 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 970 | arr[num++] = 0x94; /* PIV=1, target port, rel port */ |
| 971 | arr[num++] = 0x0; /* reserved */ |
| 972 | arr[num++] = 0x4; /* length */ |
| 973 | arr[num++] = 0x0; /* reserved */ |
| 974 | arr[num++] = 0x0; /* reserved */ |
| 975 | arr[num++] = 0x0; |
| 976 | arr[num++] = 0x1; /* relative port A */ |
| 977 | } |
| 978 | /* NAA-5, Target port identifier */ |
| 979 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 980 | arr[num++] = 0x93; /* piv=1, target port, naa */ |
| 981 | arr[num++] = 0x0; |
| 982 | arr[num++] = 0x8; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 983 | put_unaligned_be64(naa5_comp_a + port_a, arr + num); |
| 984 | num += 8; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 985 | /* NAA-5, Target port group identifier */ |
| 986 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 987 | arr[num++] = 0x95; /* piv=1, target port group id */ |
| 988 | arr[num++] = 0x0; |
| 989 | arr[num++] = 0x4; |
| 990 | arr[num++] = 0; |
| 991 | arr[num++] = 0; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 992 | put_unaligned_be16(port_group_id, arr + num); |
| 993 | num += 2; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 994 | /* NAA-5, Target device identifier */ |
| 995 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 996 | arr[num++] = 0xa3; /* piv=1, target device, naa */ |
| 997 | arr[num++] = 0x0; |
| 998 | arr[num++] = 0x8; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 999 | put_unaligned_be64(naa5_comp_a + target_dev_id, arr + num); |
| 1000 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1001 | /* SCSI name string: Target device identifier */ |
| 1002 | arr[num++] = 0x63; /* proto=sas, UTF-8 */ |
| 1003 | arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */ |
| 1004 | arr[num++] = 0x0; |
| 1005 | arr[num++] = 24; |
| 1006 | memcpy(arr + num, "naa.52222220", 12); |
| 1007 | num += 12; |
| 1008 | snprintf(b, sizeof(b), "%08X", target_dev_id); |
| 1009 | memcpy(arr + num, b, 8); |
| 1010 | num += 8; |
| 1011 | memset(arr + num, 0, 4); |
| 1012 | num += 4; |
| 1013 | return num; |
| 1014 | } |
| 1015 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1016 | static unsigned char vpd84_data[] = { |
| 1017 | /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0, |
| 1018 | 0x22,0x22,0x22,0x0,0xbb,0x1, |
| 1019 | 0x22,0x22,0x22,0x0,0xbb,0x2, |
| 1020 | }; |
| 1021 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1022 | /* Software interface identification VPD page */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1023 | static int inquiry_evpd_84(unsigned char * arr) |
| 1024 | { |
| 1025 | memcpy(arr, vpd84_data, sizeof(vpd84_data)); |
| 1026 | return sizeof(vpd84_data); |
| 1027 | } |
| 1028 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1029 | /* Management network addresses VPD page */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1030 | static int inquiry_evpd_85(unsigned char * arr) |
| 1031 | { |
| 1032 | int num = 0; |
| 1033 | const char * na1 = "https://www.kernel.org/config"; |
| 1034 | const char * na2 = "http://www.kernel.org/log"; |
| 1035 | int plen, olen; |
| 1036 | |
| 1037 | arr[num++] = 0x1; /* lu, storage config */ |
| 1038 | arr[num++] = 0x0; /* reserved */ |
| 1039 | arr[num++] = 0x0; |
| 1040 | olen = strlen(na1); |
| 1041 | plen = olen + 1; |
| 1042 | if (plen % 4) |
| 1043 | plen = ((plen / 4) + 1) * 4; |
| 1044 | arr[num++] = plen; /* length, null termianted, padded */ |
| 1045 | memcpy(arr + num, na1, olen); |
| 1046 | memset(arr + num + olen, 0, plen - olen); |
| 1047 | num += plen; |
| 1048 | |
| 1049 | arr[num++] = 0x4; /* lu, logging */ |
| 1050 | arr[num++] = 0x0; /* reserved */ |
| 1051 | arr[num++] = 0x0; |
| 1052 | olen = strlen(na2); |
| 1053 | plen = olen + 1; |
| 1054 | if (plen % 4) |
| 1055 | plen = ((plen / 4) + 1) * 4; |
| 1056 | arr[num++] = plen; /* length, null terminated, padded */ |
| 1057 | memcpy(arr + num, na2, olen); |
| 1058 | memset(arr + num + olen, 0, plen - olen); |
| 1059 | num += plen; |
| 1060 | |
| 1061 | return num; |
| 1062 | } |
| 1063 | |
| 1064 | /* SCSI ports VPD page */ |
| 1065 | static int inquiry_evpd_88(unsigned char * arr, int target_dev_id) |
| 1066 | { |
| 1067 | int num = 0; |
| 1068 | int port_a, port_b; |
| 1069 | |
| 1070 | port_a = target_dev_id + 1; |
| 1071 | port_b = port_a + 1; |
| 1072 | arr[num++] = 0x0; /* reserved */ |
| 1073 | arr[num++] = 0x0; /* reserved */ |
| 1074 | arr[num++] = 0x0; |
| 1075 | arr[num++] = 0x1; /* relative port 1 (primary) */ |
| 1076 | memset(arr + num, 0, 6); |
| 1077 | num += 6; |
| 1078 | arr[num++] = 0x0; |
| 1079 | arr[num++] = 12; /* length tp descriptor */ |
| 1080 | /* naa-5 target port identifier (A) */ |
| 1081 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1082 | arr[num++] = 0x93; /* PIV=1, target port, NAA */ |
| 1083 | arr[num++] = 0x0; /* reserved */ |
| 1084 | arr[num++] = 0x8; /* length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1085 | put_unaligned_be64(naa5_comp_a + port_a, arr + num); |
| 1086 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1087 | arr[num++] = 0x0; /* reserved */ |
| 1088 | arr[num++] = 0x0; /* reserved */ |
| 1089 | arr[num++] = 0x0; |
| 1090 | arr[num++] = 0x2; /* relative port 2 (secondary) */ |
| 1091 | memset(arr + num, 0, 6); |
| 1092 | num += 6; |
| 1093 | arr[num++] = 0x0; |
| 1094 | arr[num++] = 12; /* length tp descriptor */ |
| 1095 | /* naa-5 target port identifier (B) */ |
| 1096 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1097 | arr[num++] = 0x93; /* PIV=1, target port, NAA */ |
| 1098 | arr[num++] = 0x0; /* reserved */ |
| 1099 | arr[num++] = 0x8; /* length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1100 | put_unaligned_be64(naa5_comp_a + port_b, arr + num); |
| 1101 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1102 | |
| 1103 | return num; |
| 1104 | } |
| 1105 | |
| 1106 | |
| 1107 | static unsigned char vpd89_data[] = { |
| 1108 | /* from 4th byte */ 0,0,0,0, |
| 1109 | 'l','i','n','u','x',' ',' ',' ', |
| 1110 | 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ', |
| 1111 | '1','2','3','4', |
| 1112 | 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0, |
| 1113 | 0xec,0,0,0, |
| 1114 | 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0, |
| 1115 | 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20, |
| 1116 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33, |
| 1117 | 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31, |
| 1118 | 0x53,0x41, |
| 1119 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 1120 | 0x20,0x20, |
| 1121 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 1122 | 0x10,0x80, |
| 1123 | 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0, |
| 1124 | 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0, |
| 1125 | 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0, |
| 1126 | 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0, |
| 1127 | 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40, |
| 1128 | 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0, |
| 1129 | 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,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 | 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42, |
| 1134 | 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8, |
| 1135 | 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe, |
| 1136 | 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0, |
| 1137 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1138 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1139 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1140 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1141 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1142 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1143 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1144 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1145 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1146 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1147 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1148 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51, |
| 1149 | }; |
| 1150 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1151 | /* ATA Information VPD page */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1152 | static int inquiry_evpd_89(unsigned char * arr) |
| 1153 | { |
| 1154 | memcpy(arr, vpd89_data, sizeof(vpd89_data)); |
| 1155 | return sizeof(vpd89_data); |
| 1156 | } |
| 1157 | |
| 1158 | |
| 1159 | static unsigned char vpdb0_data[] = { |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1160 | /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64, |
| 1161 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1162 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1163 | 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] | 1164 | }; |
| 1165 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1166 | /* Block limits VPD page (SBC-3) */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1167 | static int inquiry_evpd_b0(unsigned char * arr) |
| 1168 | { |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 1169 | unsigned int gran; |
| 1170 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1171 | memcpy(arr, vpdb0_data, sizeof(vpdb0_data)); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1172 | |
| 1173 | /* Optimal transfer length granularity */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1174 | gran = 1 << sdebug_physblk_exp; |
| 1175 | put_unaligned_be16(gran, arr + 2); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1176 | |
| 1177 | /* Maximum Transfer Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1178 | if (sdebug_store_sectors > 0x400) |
| 1179 | put_unaligned_be32(sdebug_store_sectors, arr + 4); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1180 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1181 | /* Optimal Transfer Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1182 | put_unaligned_be32(sdebug_opt_blks, &arr[8]); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1183 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1184 | if (sdebug_lbpu) { |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1185 | /* Maximum Unmap LBA Count */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1186 | put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1187 | |
| 1188 | /* Maximum Unmap Block Descriptor Count */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1189 | put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1190 | } |
| 1191 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1192 | /* Unmap Granularity Alignment */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1193 | if (sdebug_unmap_alignment) { |
| 1194 | put_unaligned_be32(sdebug_unmap_alignment, &arr[28]); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1195 | arr[28] |= 0x80; /* UGAVALID */ |
| 1196 | } |
| 1197 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1198 | /* Optimal Unmap Granularity */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1199 | put_unaligned_be32(sdebug_unmap_granularity, &arr[24]); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1200 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1201 | /* Maximum WRITE SAME Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1202 | put_unaligned_be64(sdebug_write_same_length, &arr[32]); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1203 | |
| 1204 | return 0x3c; /* Mandatory page length for Logical Block Provisioning */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1205 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1206 | return sizeof(vpdb0_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1209 | /* Block device characteristics VPD page (SBC-3) */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1210 | static int inquiry_evpd_b1(unsigned char *arr) |
| 1211 | { |
| 1212 | memset(arr, 0, 0x3c); |
| 1213 | arr[0] = 0; |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1214 | arr[1] = 1; /* non rotating medium (e.g. solid state) */ |
| 1215 | arr[2] = 0; |
| 1216 | arr[3] = 5; /* less than 1.8" */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1217 | |
| 1218 | return 0x3c; |
| 1219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1221 | /* Logical block provisioning VPD page (SBC-3) */ |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1222 | static int inquiry_evpd_b2(unsigned char *arr) |
| 1223 | { |
Martin K. Petersen | 3f0bc3b | 2012-03-08 10:48:29 -0500 | [diff] [blame] | 1224 | memset(arr, 0, 0x4); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1225 | arr[0] = 0; /* threshold exponent */ |
| 1226 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1227 | if (sdebug_lbpu) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1228 | arr[1] = 1 << 7; |
| 1229 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1230 | if (sdebug_lbpws) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1231 | arr[1] |= 1 << 6; |
| 1232 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1233 | if (sdebug_lbpws10) |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1234 | arr[1] |= 1 << 5; |
| 1235 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1236 | if (sdebug_lbprz) |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1237 | arr[1] |= 1 << 2; |
| 1238 | |
Martin K. Petersen | 3f0bc3b | 2012-03-08 10:48:29 -0500 | [diff] [blame] | 1239 | return 0x4; |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1240 | } |
| 1241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | #define SDEBUG_LONG_INQ_SZ 96 |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1243 | #define SDEBUG_MAX_INQ_ARR_SZ 584 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1245 | 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] | 1246 | { |
| 1247 | unsigned char pq_pdt; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1248 | unsigned char * arr; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1249 | unsigned char *cmd = scp->cmnd; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1250 | int alloc_len, n, ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1251 | bool have_wlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1253 | alloc_len = get_unaligned_be16(cmd + 3); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 1254 | arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC); |
| 1255 | if (! arr) |
| 1256 | return DID_REQUEUE << 16; |
Tomas Winkler | 34d5543 | 2015-07-28 16:54:21 +0300 | [diff] [blame] | 1257 | have_wlun = (scp->device->lun == SCSI_W_LUN_REPORT_LUNS); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1258 | if (have_wlun) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1259 | pq_pdt = 0x1e; /* present, wlun */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1260 | else if (sdebug_no_lun_0 && (0 == devip->lun)) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1261 | pq_pdt = 0x7f; /* not present, no device type */ |
| 1262 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1263 | pq_pdt = (sdebug_ptype & 0x1f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | arr[0] = pq_pdt; |
| 1265 | if (0x2 & cmd[1]) { /* CMDDT bit set */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1266 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1267 | kfree(arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | return check_condition_result; |
| 1269 | } else if (0x1 & cmd[1]) { /* EVPD bit set */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1270 | int lu_id_num, port_group_id, target_dev_id, len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1271 | char lu_id_str[6]; |
| 1272 | int host_no = devip->sdbg_host->shost->host_no; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1274 | port_group_id = (((host_no + 1) & 0x7f) << 8) + |
| 1275 | (devip->channel & 0x7f); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1276 | if (0 == sdebug_vpd_use_hostno) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1277 | host_no = 0; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1278 | lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) + |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1279 | (devip->target * 1000) + devip->lun); |
| 1280 | target_dev_id = ((host_no + 1) * 2000) + |
| 1281 | (devip->target * 1000) - 3; |
| 1282 | len = scnprintf(lu_id_str, 6, "%d", lu_id_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | if (0 == cmd[2]) { /* supported vital product data pages */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1284 | arr[1] = cmd[2]; /*sanity */ |
| 1285 | n = 4; |
| 1286 | arr[n++] = 0x0; /* this page */ |
| 1287 | arr[n++] = 0x80; /* unit serial number */ |
| 1288 | arr[n++] = 0x83; /* device identification */ |
| 1289 | arr[n++] = 0x84; /* software interface ident. */ |
| 1290 | arr[n++] = 0x85; /* management network addresses */ |
| 1291 | arr[n++] = 0x86; /* extended inquiry */ |
| 1292 | arr[n++] = 0x87; /* mode page policy */ |
| 1293 | arr[n++] = 0x88; /* SCSI ports */ |
| 1294 | arr[n++] = 0x89; /* ATA information */ |
| 1295 | arr[n++] = 0xb0; /* Block limits (SBC) */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1296 | arr[n++] = 0xb1; /* Block characteristics (SBC) */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1297 | if (scsi_debug_lbp()) /* Logical Block Prov. (SBC) */ |
| 1298 | arr[n++] = 0xb2; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1299 | arr[3] = n - 4; /* number of supported VPD pages */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | } else if (0x80 == cmd[2]) { /* unit serial number */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1301 | arr[1] = cmd[2]; /*sanity */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | arr[3] = len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1303 | memcpy(&arr[4], lu_id_str, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | } else if (0x83 == cmd[2]) { /* device identification */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1305 | arr[1] = cmd[2]; /*sanity */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1306 | arr[3] = inquiry_evpd_83(&arr[4], port_group_id, |
| 1307 | target_dev_id, lu_id_num, |
| 1308 | lu_id_str, len); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1309 | } else if (0x84 == cmd[2]) { /* Software interface ident. */ |
| 1310 | arr[1] = cmd[2]; /*sanity */ |
| 1311 | arr[3] = inquiry_evpd_84(&arr[4]); |
| 1312 | } else if (0x85 == cmd[2]) { /* Management network addresses */ |
| 1313 | arr[1] = cmd[2]; /*sanity */ |
| 1314 | arr[3] = inquiry_evpd_85(&arr[4]); |
| 1315 | } else if (0x86 == cmd[2]) { /* extended inquiry */ |
| 1316 | arr[1] = cmd[2]; /*sanity */ |
| 1317 | arr[3] = 0x3c; /* number of following entries */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1318 | if (sdebug_dif == SD_DIF_TYPE3_PROTECTION) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1319 | arr[4] = 0x4; /* SPT: GRD_CHK:1 */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1320 | else if (sdebug_dif) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1321 | arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */ |
| 1322 | else |
| 1323 | arr[4] = 0x0; /* no protection stuff */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1324 | arr[5] = 0x7; /* head of q, ordered + simple q's */ |
| 1325 | } else if (0x87 == cmd[2]) { /* mode page policy */ |
| 1326 | arr[1] = cmd[2]; /*sanity */ |
| 1327 | arr[3] = 0x8; /* number of following entries */ |
| 1328 | arr[4] = 0x2; /* disconnect-reconnect mp */ |
| 1329 | arr[6] = 0x80; /* mlus, shared */ |
| 1330 | arr[8] = 0x18; /* protocol specific lu */ |
| 1331 | arr[10] = 0x82; /* mlus, per initiator port */ |
| 1332 | } else if (0x88 == cmd[2]) { /* SCSI Ports */ |
| 1333 | arr[1] = cmd[2]; /*sanity */ |
| 1334 | arr[3] = inquiry_evpd_88(&arr[4], target_dev_id); |
| 1335 | } else if (0x89 == cmd[2]) { /* ATA information */ |
| 1336 | arr[1] = cmd[2]; /*sanity */ |
| 1337 | n = inquiry_evpd_89(&arr[4]); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1338 | put_unaligned_be16(n, arr + 2); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1339 | } else if (0xb0 == cmd[2]) { /* Block limits (SBC) */ |
| 1340 | arr[1] = cmd[2]; /*sanity */ |
| 1341 | arr[3] = inquiry_evpd_b0(&arr[4]); |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1342 | } else if (0xb1 == cmd[2]) { /* Block characteristics (SBC) */ |
| 1343 | arr[1] = cmd[2]; /*sanity */ |
| 1344 | arr[3] = inquiry_evpd_b1(&arr[4]); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1345 | } else if (0xb2 == cmd[2]) { /* Logical Block Prov. (SBC) */ |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1346 | arr[1] = cmd[2]; /*sanity */ |
| 1347 | arr[3] = inquiry_evpd_b2(&arr[4]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1349 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1350 | kfree(arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | return check_condition_result; |
| 1352 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1353 | len = min(get_unaligned_be16(arr + 2) + 4, alloc_len); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1354 | ret = fill_from_dev_buffer(scp, arr, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1355 | min(len, SDEBUG_MAX_INQ_ARR_SZ)); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1356 | kfree(arr); |
| 1357 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | } |
| 1359 | /* drops through here for a standard inquiry */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1360 | arr[1] = sdebug_removable ? 0x80 : 0; /* Removable disk */ |
| 1361 | arr[2] = sdebug_scsi_level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | arr[3] = 2; /* response_data_format==2 */ |
| 1363 | arr[4] = SDEBUG_LONG_INQ_SZ - 5; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1364 | arr[5] = sdebug_dif ? 1 : 0; /* PROTECT bit */ |
| 1365 | if (0 == sdebug_vpd_use_hostno) |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1366 | arr[5] = 0x10; /* claim: implicit TGPS */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1367 | arr[6] = 0x10; /* claim: MultiP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1369 | arr[7] = 0xa; /* claim: LINKED + CMDQUE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | memcpy(&arr[8], inq_vendor_id, 8); |
| 1371 | memcpy(&arr[16], inq_product_id, 16); |
| 1372 | memcpy(&arr[32], inq_product_rev, 4); |
| 1373 | /* version descriptors (2 bytes each) follow */ |
Douglas Gilbert | e46b034 | 2014-08-05 12:21:53 +0200 | [diff] [blame] | 1374 | arr[58] = 0x0; arr[59] = 0xa2; /* SAM-5 rev 4 */ |
| 1375 | arr[60] = 0x4; arr[61] = 0x68; /* SPC-4 rev 37 */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1376 | n = 62; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1377 | if (sdebug_ptype == 0) { |
Douglas Gilbert | e46b034 | 2014-08-05 12:21:53 +0200 | [diff] [blame] | 1378 | arr[n++] = 0x4; arr[n++] = 0xc5; /* SBC-4 rev 36 */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1379 | } else if (sdebug_ptype == 1) { |
Douglas Gilbert | e46b034 | 2014-08-05 12:21:53 +0200 | [diff] [blame] | 1380 | arr[n++] = 0x5; arr[n++] = 0x25; /* SSC-4 rev 3 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | } |
Douglas Gilbert | e46b034 | 2014-08-05 12:21:53 +0200 | [diff] [blame] | 1382 | arr[n++] = 0x20; arr[n++] = 0xe6; /* SPL-3 rev 7 */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1383 | ret = fill_from_dev_buffer(scp, arr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | min(alloc_len, SDEBUG_LONG_INQ_SZ)); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1385 | kfree(arr); |
| 1386 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | static int resp_requests(struct scsi_cmnd * scp, |
| 1390 | struct sdebug_dev_info * devip) |
| 1391 | { |
| 1392 | unsigned char * sbuff; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1393 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1394 | unsigned char arr[SCSI_SENSE_BUFFERSIZE]; |
Tomas Winkler | 2492fc0 | 2015-07-28 16:54:26 +0300 | [diff] [blame] | 1395 | bool dsense; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | int len = 18; |
| 1397 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1398 | memset(arr, 0, sizeof(arr)); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1399 | dsense = !!(cmd[1] & 1); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1400 | sbuff = scp->sense_buffer; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1401 | if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1402 | if (dsense) { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1403 | arr[0] = 0x72; |
| 1404 | arr[1] = 0x0; /* NO_SENSE in sense_key */ |
| 1405 | arr[2] = THRESHOLD_EXCEEDED; |
| 1406 | arr[3] = 0xff; /* TEST set and MRIE==6 */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1407 | len = 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1408 | } else { |
| 1409 | arr[0] = 0x70; |
| 1410 | arr[2] = 0x0; /* NO_SENSE in sense_key */ |
| 1411 | arr[7] = 0xa; /* 18 byte sense buffer */ |
| 1412 | arr[12] = THRESHOLD_EXCEEDED; |
| 1413 | arr[13] = 0xff; /* TEST set and MRIE==6 */ |
| 1414 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1415 | } else { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1416 | memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1417 | if (arr[0] >= 0x70 && dsense == sdebug_dsense) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1418 | ; /* have sense and formats match */ |
| 1419 | else if (arr[0] <= 0x70) { |
| 1420 | if (dsense) { |
| 1421 | memset(arr, 0, 8); |
| 1422 | arr[0] = 0x72; |
| 1423 | len = 8; |
| 1424 | } else { |
| 1425 | memset(arr, 0, 18); |
| 1426 | arr[0] = 0x70; |
| 1427 | arr[7] = 0xa; |
| 1428 | } |
| 1429 | } else if (dsense) { |
| 1430 | memset(arr, 0, 8); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1431 | arr[0] = 0x72; |
| 1432 | arr[1] = sbuff[2]; /* sense key */ |
| 1433 | arr[2] = sbuff[12]; /* asc */ |
| 1434 | arr[3] = sbuff[13]; /* ascq */ |
| 1435 | len = 8; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1436 | } else { |
| 1437 | memset(arr, 0, 18); |
| 1438 | arr[0] = 0x70; |
| 1439 | arr[2] = sbuff[1]; |
| 1440 | arr[7] = 0xa; |
| 1441 | arr[12] = sbuff[1]; |
| 1442 | arr[13] = sbuff[3]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1443 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1444 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1445 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1446 | mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | return fill_from_dev_buffer(scp, arr, len); |
| 1448 | } |
| 1449 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1450 | static int resp_start_stop(struct scsi_cmnd * scp, |
| 1451 | struct sdebug_dev_info * devip) |
| 1452 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1453 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1454 | int power_cond, start; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1455 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1456 | power_cond = (cmd[4] & 0xf0) >> 4; |
| 1457 | if (power_cond) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1458 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1459 | return check_condition_result; |
| 1460 | } |
| 1461 | start = cmd[4] & 1; |
| 1462 | if (start == devip->stopped) |
| 1463 | devip->stopped = !start; |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1467 | static sector_t get_sdebug_capacity(void) |
| 1468 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1469 | static const unsigned int gibibyte = 1073741824; |
| 1470 | |
| 1471 | if (sdebug_virtual_gb > 0) |
| 1472 | return (sector_t)sdebug_virtual_gb * |
| 1473 | (gibibyte / sdebug_sector_size); |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1474 | else |
| 1475 | return sdebug_store_sectors; |
| 1476 | } |
| 1477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | #define SDEBUG_READCAP_ARR_SZ 8 |
| 1479 | static int resp_readcap(struct scsi_cmnd * scp, |
| 1480 | struct sdebug_dev_info * devip) |
| 1481 | { |
| 1482 | unsigned char arr[SDEBUG_READCAP_ARR_SZ]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1483 | unsigned int capac; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1485 | /* following just in case virtual_gb changed */ |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1486 | sdebug_capacity = get_sdebug_capacity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | memset(arr, 0, SDEBUG_READCAP_ARR_SZ); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1488 | if (sdebug_capacity < 0xffffffff) { |
| 1489 | capac = (unsigned int)sdebug_capacity - 1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1490 | put_unaligned_be32(capac, arr + 0); |
| 1491 | } else |
| 1492 | put_unaligned_be32(0xffffffff, arr + 0); |
| 1493 | put_unaligned_be16(sdebug_sector_size, arr + 6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ); |
| 1495 | } |
| 1496 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1497 | #define SDEBUG_READCAP16_ARR_SZ 32 |
| 1498 | static int resp_readcap16(struct scsi_cmnd * scp, |
| 1499 | struct sdebug_dev_info * devip) |
| 1500 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1501 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1502 | unsigned char arr[SDEBUG_READCAP16_ARR_SZ]; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1503 | int alloc_len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1504 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1505 | alloc_len = get_unaligned_be32(cmd + 10); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1506 | /* following just in case virtual_gb changed */ |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1507 | sdebug_capacity = get_sdebug_capacity(); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1508 | memset(arr, 0, SDEBUG_READCAP16_ARR_SZ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1509 | put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0); |
| 1510 | put_unaligned_be32(sdebug_sector_size, arr + 8); |
| 1511 | arr[13] = sdebug_physblk_exp & 0xf; |
| 1512 | arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1513 | |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1514 | if (scsi_debug_lbp()) { |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1515 | arr[14] |= 0x80; /* LBPME */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1516 | if (sdebug_lbprz) |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1517 | arr[14] |= 0x40; /* LBPRZ */ |
| 1518 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1519 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1520 | arr[15] = sdebug_lowest_aligned & 0xff; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1521 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1522 | if (sdebug_dif) { |
| 1523 | arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */ |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1524 | arr[12] |= 1; /* PROT_EN */ |
| 1525 | } |
| 1526 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1527 | return fill_from_dev_buffer(scp, arr, |
| 1528 | min(alloc_len, SDEBUG_READCAP16_ARR_SZ)); |
| 1529 | } |
| 1530 | |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1531 | #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412 |
| 1532 | |
| 1533 | static int resp_report_tgtpgs(struct scsi_cmnd * scp, |
| 1534 | struct sdebug_dev_info * devip) |
| 1535 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1536 | unsigned char *cmd = scp->cmnd; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1537 | unsigned char * arr; |
| 1538 | int host_no = devip->sdbg_host->shost->host_no; |
| 1539 | int n, ret, alen, rlen; |
| 1540 | int port_group_a, port_group_b, port_a, port_b; |
| 1541 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1542 | alen = get_unaligned_be32(cmd + 6); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 1543 | arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC); |
| 1544 | if (! arr) |
| 1545 | return DID_REQUEUE << 16; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1546 | /* |
| 1547 | * EVPD page 0x88 states we have two ports, one |
| 1548 | * real and a fake port with no device connected. |
| 1549 | * So we create two port groups with one port each |
| 1550 | * and set the group with port B to unavailable. |
| 1551 | */ |
| 1552 | port_a = 0x1; /* relative port A */ |
| 1553 | port_b = 0x2; /* relative port B */ |
| 1554 | port_group_a = (((host_no + 1) & 0x7f) << 8) + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1555 | (devip->channel & 0x7f); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1556 | port_group_b = (((host_no + 1) & 0x7f) << 8) + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1557 | (devip->channel & 0x7f) + 0x80; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1558 | |
| 1559 | /* |
| 1560 | * The asymmetric access state is cycled according to the host_id. |
| 1561 | */ |
| 1562 | n = 4; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1563 | if (0 == sdebug_vpd_use_hostno) { |
| 1564 | arr[n++] = host_no % 3; /* Asymm access state */ |
| 1565 | arr[n++] = 0x0F; /* claim: all states are supported */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1566 | } else { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1567 | arr[n++] = 0x0; /* Active/Optimized path */ |
| 1568 | arr[n++] = 0x01; /* only support active/optimized paths */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1569 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1570 | put_unaligned_be16(port_group_a, arr + n); |
| 1571 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1572 | arr[n++] = 0; /* Reserved */ |
| 1573 | arr[n++] = 0; /* Status code */ |
| 1574 | arr[n++] = 0; /* Vendor unique */ |
| 1575 | arr[n++] = 0x1; /* One port per group */ |
| 1576 | arr[n++] = 0; /* Reserved */ |
| 1577 | arr[n++] = 0; /* Reserved */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1578 | put_unaligned_be16(port_a, arr + n); |
| 1579 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1580 | arr[n++] = 3; /* Port unavailable */ |
| 1581 | arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1582 | put_unaligned_be16(port_group_b, arr + n); |
| 1583 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1584 | arr[n++] = 0; /* Reserved */ |
| 1585 | arr[n++] = 0; /* Status code */ |
| 1586 | arr[n++] = 0; /* Vendor unique */ |
| 1587 | arr[n++] = 0x1; /* One port per group */ |
| 1588 | arr[n++] = 0; /* Reserved */ |
| 1589 | arr[n++] = 0; /* Reserved */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1590 | put_unaligned_be16(port_b, arr + n); |
| 1591 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1592 | |
| 1593 | rlen = n - 4; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1594 | put_unaligned_be32(rlen, arr + 0); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1595 | |
| 1596 | /* |
| 1597 | * Return the smallest value of either |
| 1598 | * - The allocated length |
| 1599 | * - The constructed command length |
| 1600 | * - The maximum array size |
| 1601 | */ |
| 1602 | rlen = min(alen,n); |
| 1603 | ret = fill_from_dev_buffer(scp, arr, |
| 1604 | min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ)); |
| 1605 | kfree(arr); |
| 1606 | return ret; |
| 1607 | } |
| 1608 | |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1609 | static int |
| 1610 | resp_rsup_opcodes(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
| 1611 | { |
| 1612 | bool rctd; |
| 1613 | u8 reporting_opts, req_opcode, sdeb_i, supp; |
| 1614 | u16 req_sa, u; |
| 1615 | u32 alloc_len, a_len; |
| 1616 | int k, offset, len, errsts, count, bump, na; |
| 1617 | const struct opcode_info_t *oip; |
| 1618 | const struct opcode_info_t *r_oip; |
| 1619 | u8 *arr; |
| 1620 | u8 *cmd = scp->cmnd; |
| 1621 | |
| 1622 | rctd = !!(cmd[2] & 0x80); |
| 1623 | reporting_opts = cmd[2] & 0x7; |
| 1624 | req_opcode = cmd[3]; |
| 1625 | req_sa = get_unaligned_be16(cmd + 4); |
| 1626 | alloc_len = get_unaligned_be32(cmd + 6); |
Colin Ian King | 6d310df | 2015-01-22 11:20:40 +0000 | [diff] [blame] | 1627 | if (alloc_len < 4 || alloc_len > 0xffff) { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1628 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
| 1629 | return check_condition_result; |
| 1630 | } |
| 1631 | if (alloc_len > 8192) |
| 1632 | a_len = 8192; |
| 1633 | else |
| 1634 | a_len = alloc_len; |
Sasha Levin | 99531e6 | 2015-01-17 17:47:37 -0500 | [diff] [blame] | 1635 | arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1636 | if (NULL == arr) { |
| 1637 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 1638 | INSUFF_RES_ASCQ); |
| 1639 | return check_condition_result; |
| 1640 | } |
| 1641 | switch (reporting_opts) { |
| 1642 | case 0: /* all commands */ |
| 1643 | /* count number of commands */ |
| 1644 | for (count = 0, oip = opcode_info_arr; |
| 1645 | oip->num_attached != 0xff; ++oip) { |
| 1646 | if (F_INV_OP & oip->flags) |
| 1647 | continue; |
| 1648 | count += (oip->num_attached + 1); |
| 1649 | } |
| 1650 | bump = rctd ? 20 : 8; |
| 1651 | put_unaligned_be32(count * bump, arr); |
| 1652 | for (offset = 4, oip = opcode_info_arr; |
| 1653 | oip->num_attached != 0xff && offset < a_len; ++oip) { |
| 1654 | if (F_INV_OP & oip->flags) |
| 1655 | continue; |
| 1656 | na = oip->num_attached; |
| 1657 | arr[offset] = oip->opcode; |
| 1658 | put_unaligned_be16(oip->sa, arr + offset + 2); |
| 1659 | if (rctd) |
| 1660 | arr[offset + 5] |= 0x2; |
| 1661 | if (FF_SA & oip->flags) |
| 1662 | arr[offset + 5] |= 0x1; |
| 1663 | put_unaligned_be16(oip->len_mask[0], arr + offset + 6); |
| 1664 | if (rctd) |
| 1665 | put_unaligned_be16(0xa, arr + offset + 8); |
| 1666 | r_oip = oip; |
| 1667 | for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) { |
| 1668 | if (F_INV_OP & oip->flags) |
| 1669 | continue; |
| 1670 | offset += bump; |
| 1671 | arr[offset] = oip->opcode; |
| 1672 | put_unaligned_be16(oip->sa, arr + offset + 2); |
| 1673 | if (rctd) |
| 1674 | arr[offset + 5] |= 0x2; |
| 1675 | if (FF_SA & oip->flags) |
| 1676 | arr[offset + 5] |= 0x1; |
| 1677 | put_unaligned_be16(oip->len_mask[0], |
| 1678 | arr + offset + 6); |
| 1679 | if (rctd) |
| 1680 | put_unaligned_be16(0xa, |
| 1681 | arr + offset + 8); |
| 1682 | } |
| 1683 | oip = r_oip; |
| 1684 | offset += bump; |
| 1685 | } |
| 1686 | break; |
| 1687 | case 1: /* one command: opcode only */ |
| 1688 | case 2: /* one command: opcode plus service action */ |
| 1689 | case 3: /* one command: if sa==0 then opcode only else opcode+sa */ |
| 1690 | sdeb_i = opcode_ind_arr[req_opcode]; |
| 1691 | oip = &opcode_info_arr[sdeb_i]; |
| 1692 | if (F_INV_OP & oip->flags) { |
| 1693 | supp = 1; |
| 1694 | offset = 4; |
| 1695 | } else { |
| 1696 | if (1 == reporting_opts) { |
| 1697 | if (FF_SA & oip->flags) { |
| 1698 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, |
| 1699 | 2, 2); |
| 1700 | kfree(arr); |
| 1701 | return check_condition_result; |
| 1702 | } |
| 1703 | req_sa = 0; |
| 1704 | } else if (2 == reporting_opts && |
| 1705 | 0 == (FF_SA & oip->flags)) { |
| 1706 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1); |
| 1707 | kfree(arr); /* point at requested sa */ |
| 1708 | return check_condition_result; |
| 1709 | } |
| 1710 | if (0 == (FF_SA & oip->flags) && |
| 1711 | req_opcode == oip->opcode) |
| 1712 | supp = 3; |
| 1713 | else if (0 == (FF_SA & oip->flags)) { |
| 1714 | na = oip->num_attached; |
| 1715 | for (k = 0, oip = oip->arrp; k < na; |
| 1716 | ++k, ++oip) { |
| 1717 | if (req_opcode == oip->opcode) |
| 1718 | break; |
| 1719 | } |
| 1720 | supp = (k >= na) ? 1 : 3; |
| 1721 | } else if (req_sa != oip->sa) { |
| 1722 | na = oip->num_attached; |
| 1723 | for (k = 0, oip = oip->arrp; k < na; |
| 1724 | ++k, ++oip) { |
| 1725 | if (req_sa == oip->sa) |
| 1726 | break; |
| 1727 | } |
| 1728 | supp = (k >= na) ? 1 : 3; |
| 1729 | } else |
| 1730 | supp = 3; |
| 1731 | if (3 == supp) { |
| 1732 | u = oip->len_mask[0]; |
| 1733 | put_unaligned_be16(u, arr + 2); |
| 1734 | arr[4] = oip->opcode; |
| 1735 | for (k = 1; k < u; ++k) |
| 1736 | arr[4 + k] = (k < 16) ? |
| 1737 | oip->len_mask[k] : 0xff; |
| 1738 | offset = 4 + u; |
| 1739 | } else |
| 1740 | offset = 4; |
| 1741 | } |
| 1742 | arr[1] = (rctd ? 0x80 : 0) | supp; |
| 1743 | if (rctd) { |
| 1744 | put_unaligned_be16(0xa, arr + offset); |
| 1745 | offset += 12; |
| 1746 | } |
| 1747 | break; |
| 1748 | default: |
| 1749 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2); |
| 1750 | kfree(arr); |
| 1751 | return check_condition_result; |
| 1752 | } |
| 1753 | offset = (offset < a_len) ? offset : a_len; |
| 1754 | len = (offset < alloc_len) ? offset : alloc_len; |
| 1755 | errsts = fill_from_dev_buffer(scp, arr, len); |
| 1756 | kfree(arr); |
| 1757 | return errsts; |
| 1758 | } |
| 1759 | |
| 1760 | static int |
| 1761 | resp_rsup_tmfs(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
| 1762 | { |
| 1763 | bool repd; |
| 1764 | u32 alloc_len, len; |
| 1765 | u8 arr[16]; |
| 1766 | u8 *cmd = scp->cmnd; |
| 1767 | |
| 1768 | memset(arr, 0, sizeof(arr)); |
| 1769 | repd = !!(cmd[2] & 0x80); |
| 1770 | alloc_len = get_unaligned_be32(cmd + 6); |
| 1771 | if (alloc_len < 4) { |
| 1772 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
| 1773 | return check_condition_result; |
| 1774 | } |
| 1775 | arr[0] = 0xc8; /* ATS | ATSS | LURS */ |
| 1776 | arr[1] = 0x1; /* ITNRS */ |
| 1777 | if (repd) { |
| 1778 | arr[3] = 0xc; |
| 1779 | len = 16; |
| 1780 | } else |
| 1781 | len = 4; |
| 1782 | |
| 1783 | len = (len < alloc_len) ? len : alloc_len; |
| 1784 | return fill_from_dev_buffer(scp, arr, len); |
| 1785 | } |
| 1786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | /* <<Following mode page info copied from ST318451LW>> */ |
| 1788 | |
| 1789 | static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target) |
| 1790 | { /* Read-Write Error Recovery page for mode_sense */ |
| 1791 | unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0, |
| 1792 | 5, 0, 0xff, 0xff}; |
| 1793 | |
| 1794 | memcpy(p, err_recov_pg, sizeof(err_recov_pg)); |
| 1795 | if (1 == pcontrol) |
| 1796 | memset(p + 2, 0, sizeof(err_recov_pg) - 2); |
| 1797 | return sizeof(err_recov_pg); |
| 1798 | } |
| 1799 | |
| 1800 | static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target) |
| 1801 | { /* Disconnect-Reconnect page for mode_sense */ |
| 1802 | unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0, |
| 1803 | 0, 0, 0, 0, 0, 0, 0, 0}; |
| 1804 | |
| 1805 | memcpy(p, disconnect_pg, sizeof(disconnect_pg)); |
| 1806 | if (1 == pcontrol) |
| 1807 | memset(p + 2, 0, sizeof(disconnect_pg) - 2); |
| 1808 | return sizeof(disconnect_pg); |
| 1809 | } |
| 1810 | |
| 1811 | static int resp_format_pg(unsigned char * p, int pcontrol, int target) |
| 1812 | { /* Format device page for mode_sense */ |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1813 | unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0, |
| 1814 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1815 | 0, 0, 0, 0, 0x40, 0, 0, 0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1817 | memcpy(p, format_pg, sizeof(format_pg)); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1818 | put_unaligned_be16(sdebug_sectors_per, p + 10); |
| 1819 | put_unaligned_be16(sdebug_sector_size, p + 12); |
| 1820 | if (sdebug_removable) |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1821 | p[20] |= 0x20; /* should agree with INQUIRY */ |
| 1822 | if (1 == pcontrol) |
| 1823 | memset(p + 2, 0, sizeof(format_pg) - 2); |
| 1824 | return sizeof(format_pg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
| 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 | |
| 1844 | static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target) |
| 1845 | { /* Control mode page for mode_sense */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1846 | unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0, |
| 1847 | 0, 0, 0, 0}; |
| 1848 | 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] | 1849 | 0, 0, 0x2, 0x4b}; |
| 1850 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1851 | if (sdebug_dsense) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | ctrl_m_pg[2] |= 0x4; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1853 | else |
| 1854 | ctrl_m_pg[2] &= ~0x4; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1855 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1856 | if (sdebug_ato) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1857 | ctrl_m_pg[5] |= 0x80; /* ATO=1 */ |
| 1858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg)); |
| 1860 | if (1 == pcontrol) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1861 | memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg)); |
| 1862 | else if (2 == pcontrol) |
| 1863 | memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | return sizeof(ctrl_m_pg); |
| 1865 | } |
| 1866 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target) |
| 1869 | { /* Informational Exceptions control mode page for mode_sense */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1870 | unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0, |
| 1871 | 0, 0, 0x0, 0x0}; |
| 1872 | unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, |
| 1873 | 0, 0, 0x0, 0x0}; |
| 1874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | memcpy(p, iec_m_pg, sizeof(iec_m_pg)); |
| 1876 | if (1 == pcontrol) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1877 | memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg)); |
| 1878 | else if (2 == pcontrol) |
| 1879 | memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | return sizeof(iec_m_pg); |
| 1881 | } |
| 1882 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1883 | static int resp_sas_sf_m_pg(unsigned char * p, int pcontrol, int target) |
| 1884 | { /* SAS SSP mode page - short format for mode_sense */ |
| 1885 | unsigned char sas_sf_m_pg[] = {0x19, 0x6, |
| 1886 | 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0}; |
| 1887 | |
| 1888 | memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg)); |
| 1889 | if (1 == pcontrol) |
| 1890 | memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2); |
| 1891 | return sizeof(sas_sf_m_pg); |
| 1892 | } |
| 1893 | |
| 1894 | |
| 1895 | static int resp_sas_pcd_m_spg(unsigned char * p, int pcontrol, int target, |
| 1896 | int target_dev_id) |
| 1897 | { /* SAS phy control and discover mode page for mode_sense */ |
| 1898 | unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2, |
| 1899 | 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1900 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
| 1901 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1902 | 0x2, 0, 0, 0, 0, 0, 0, 0, |
| 1903 | 0x88, 0x99, 0, 0, 0, 0, 0, 0, |
| 1904 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1905 | 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1906 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
| 1907 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1908 | 0x3, 0, 0, 0, 0, 0, 0, 0, |
| 1909 | 0x88, 0x99, 0, 0, 0, 0, 0, 0, |
| 1910 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1911 | }; |
| 1912 | int port_a, port_b; |
| 1913 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1914 | put_unaligned_be64(naa5_comp_a, sas_pcd_m_pg + 16); |
| 1915 | put_unaligned_be64(naa5_comp_c + 1, sas_pcd_m_pg + 24); |
| 1916 | put_unaligned_be64(naa5_comp_a, sas_pcd_m_pg + 64); |
| 1917 | put_unaligned_be64(naa5_comp_c + 1, sas_pcd_m_pg + 72); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1918 | port_a = target_dev_id + 1; |
| 1919 | port_b = port_a + 1; |
| 1920 | memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg)); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1921 | put_unaligned_be32(port_a, p + 20); |
| 1922 | put_unaligned_be32(port_b, p + 48 + 20); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1923 | if (1 == pcontrol) |
| 1924 | memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4); |
| 1925 | return sizeof(sas_pcd_m_pg); |
| 1926 | } |
| 1927 | |
| 1928 | static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol) |
| 1929 | { /* SAS SSP shared protocol specific port mode subpage */ |
| 1930 | unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0, |
| 1931 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1932 | }; |
| 1933 | |
| 1934 | memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg)); |
| 1935 | if (1 == pcontrol) |
| 1936 | memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4); |
| 1937 | return sizeof(sas_sha_m_pg); |
| 1938 | } |
| 1939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | #define SDEBUG_MAX_MSENSE_SZ 256 |
| 1941 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1942 | static int |
| 1943 | resp_mode_sense(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | { |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1945 | unsigned char dbd, llbaa; |
| 1946 | int pcontrol, pcode, subpcode, bd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | unsigned char dev_spec; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1948 | int alloc_len, msense_6, offset, len, target_dev_id; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1949 | int target = scp->device->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | unsigned char * ap; |
| 1951 | unsigned char arr[SDEBUG_MAX_MSENSE_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1952 | unsigned char *cmd = scp->cmnd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1954 | dbd = !!(cmd[1] & 0x8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | pcontrol = (cmd[2] & 0xc0) >> 6; |
| 1956 | pcode = cmd[2] & 0x3f; |
| 1957 | subpcode = cmd[3]; |
| 1958 | msense_6 = (MODE_SENSE == cmd[0]); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1959 | llbaa = msense_6 ? 0 : !!(cmd[1] & 0x10); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1960 | if ((0 == sdebug_ptype) && (0 == dbd)) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1961 | bd_len = llbaa ? 16 : 8; |
| 1962 | else |
| 1963 | bd_len = 0; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1964 | alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | memset(arr, 0, SDEBUG_MAX_MSENSE_SZ); |
| 1966 | if (0x3 == pcontrol) { /* Saving values not supported */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1967 | mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | return check_condition_result; |
| 1969 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1970 | target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) + |
| 1971 | (devip->target * 1000) - 3; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1972 | /* set DPOFUA bit for disks */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1973 | if (0 == sdebug_ptype) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1974 | dev_spec = (DEV_READONLY(target) ? 0x80 : 0x0) | 0x10; |
| 1975 | else |
| 1976 | dev_spec = 0x0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | if (msense_6) { |
| 1978 | arr[2] = dev_spec; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1979 | arr[3] = bd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | offset = 4; |
| 1981 | } else { |
| 1982 | arr[3] = dev_spec; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1983 | if (16 == bd_len) |
| 1984 | arr[4] = 0x1; /* set LONGLBA bit */ |
| 1985 | arr[7] = bd_len; /* assume 255 or less */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | offset = 8; |
| 1987 | } |
| 1988 | ap = arr + offset; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1989 | if ((bd_len > 0) && (!sdebug_capacity)) |
| 1990 | sdebug_capacity = get_sdebug_capacity(); |
| 1991 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1992 | if (8 == bd_len) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1993 | if (sdebug_capacity > 0xfffffffe) |
| 1994 | put_unaligned_be32(0xffffffff, ap + 0); |
| 1995 | else |
| 1996 | put_unaligned_be32(sdebug_capacity, ap + 0); |
| 1997 | put_unaligned_be16(sdebug_sector_size, ap + 6); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1998 | offset += bd_len; |
| 1999 | ap = arr + offset; |
| 2000 | } else if (16 == bd_len) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2001 | put_unaligned_be64((u64)sdebug_capacity, ap + 0); |
| 2002 | put_unaligned_be32(sdebug_sector_size, ap + 12); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2003 | offset += bd_len; |
| 2004 | ap = arr + offset; |
| 2005 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2007 | if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) { |
| 2008 | /* TODO: Control Extension page */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2009 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | return check_condition_result; |
| 2011 | } |
| 2012 | switch (pcode) { |
| 2013 | case 0x1: /* Read-Write error recovery page, direct access */ |
| 2014 | len = resp_err_recov_pg(ap, pcontrol, target); |
| 2015 | offset += len; |
| 2016 | break; |
| 2017 | case 0x2: /* Disconnect-Reconnect page, all devices */ |
| 2018 | len = resp_disconnect_pg(ap, pcontrol, target); |
| 2019 | offset += len; |
| 2020 | break; |
| 2021 | case 0x3: /* Format device page, direct access */ |
| 2022 | len = resp_format_pg(ap, pcontrol, target); |
| 2023 | offset += len; |
| 2024 | break; |
| 2025 | case 0x8: /* Caching page, direct access */ |
| 2026 | len = resp_caching_pg(ap, pcontrol, target); |
| 2027 | offset += len; |
| 2028 | break; |
| 2029 | case 0xa: /* Control Mode page, all devices */ |
| 2030 | len = resp_ctrl_m_pg(ap, pcontrol, target); |
| 2031 | offset += len; |
| 2032 | break; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2033 | case 0x19: /* if spc==1 then sas phy, control+discover */ |
| 2034 | if ((subpcode > 0x2) && (subpcode < 0xff)) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2035 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2036 | return check_condition_result; |
| 2037 | } |
| 2038 | len = 0; |
| 2039 | if ((0x0 == subpcode) || (0xff == subpcode)) |
| 2040 | len += resp_sas_sf_m_pg(ap + len, pcontrol, target); |
| 2041 | if ((0x1 == subpcode) || (0xff == subpcode)) |
| 2042 | len += resp_sas_pcd_m_spg(ap + len, pcontrol, target, |
| 2043 | target_dev_id); |
| 2044 | if ((0x2 == subpcode) || (0xff == subpcode)) |
| 2045 | len += resp_sas_sha_m_spg(ap + len, pcontrol); |
| 2046 | offset += len; |
| 2047 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | case 0x1c: /* Informational Exceptions Mode page, all devices */ |
| 2049 | len = resp_iec_m_pg(ap, pcontrol, target); |
| 2050 | offset += len; |
| 2051 | break; |
| 2052 | case 0x3f: /* Read all Mode pages */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2053 | if ((0 == subpcode) || (0xff == subpcode)) { |
| 2054 | len = resp_err_recov_pg(ap, pcontrol, target); |
| 2055 | len += resp_disconnect_pg(ap + len, pcontrol, target); |
| 2056 | len += resp_format_pg(ap + len, pcontrol, target); |
| 2057 | len += resp_caching_pg(ap + len, pcontrol, target); |
| 2058 | len += resp_ctrl_m_pg(ap + len, pcontrol, target); |
| 2059 | len += resp_sas_sf_m_pg(ap + len, pcontrol, target); |
| 2060 | if (0xff == subpcode) { |
| 2061 | len += resp_sas_pcd_m_spg(ap + len, pcontrol, |
| 2062 | target, target_dev_id); |
| 2063 | len += resp_sas_sha_m_spg(ap + len, pcontrol); |
| 2064 | } |
| 2065 | len += resp_iec_m_pg(ap + len, pcontrol, target); |
| 2066 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2067 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2068 | return check_condition_result; |
| 2069 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | offset += len; |
| 2071 | break; |
| 2072 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2073 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | return check_condition_result; |
| 2075 | } |
| 2076 | if (msense_6) |
| 2077 | arr[0] = offset - 1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2078 | else |
| 2079 | put_unaligned_be16((offset - 2), arr + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | return fill_from_dev_buffer(scp, arr, min(alloc_len, offset)); |
| 2081 | } |
| 2082 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2083 | #define SDEBUG_MAX_MSELECT_SZ 512 |
| 2084 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2085 | static int |
| 2086 | resp_mode_select(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2087 | { |
| 2088 | int pf, sp, ps, md_len, bd_len, off, spf, pg_len; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2089 | int param_len, res, mpage; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2090 | unsigned char arr[SDEBUG_MAX_MSELECT_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2091 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2092 | int mselect6 = (MODE_SELECT == cmd[0]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2093 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2094 | memset(arr, 0, sizeof(arr)); |
| 2095 | pf = cmd[1] & 0x10; |
| 2096 | sp = cmd[1] & 0x1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2097 | param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2098 | if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2099 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2100 | return check_condition_result; |
| 2101 | } |
| 2102 | res = fetch_to_dev_buffer(scp, arr, param_len); |
| 2103 | if (-1 == res) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2104 | return DID_ERROR << 16; |
| 2105 | else if (sdebug_verbose && (res < param_len)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2106 | sdev_printk(KERN_INFO, scp->device, |
| 2107 | "%s: cdb indicated=%d, IO sent=%d bytes\n", |
| 2108 | __func__, param_len, res); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2109 | md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2); |
| 2110 | bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2111 | if (md_len > 2) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2112 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2113 | return check_condition_result; |
| 2114 | } |
| 2115 | off = bd_len + (mselect6 ? 4 : 8); |
| 2116 | mpage = arr[off] & 0x3f; |
| 2117 | ps = !!(arr[off] & 0x80); |
| 2118 | if (ps) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2119 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2120 | return check_condition_result; |
| 2121 | } |
| 2122 | spf = !!(arr[off] & 0x40); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2123 | pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) : |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2124 | (arr[off + 1] + 2); |
| 2125 | if ((pg_len + off) > param_len) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2126 | mk_sense_buffer(scp, ILLEGAL_REQUEST, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2127 | PARAMETER_LIST_LENGTH_ERR, 0); |
| 2128 | return check_condition_result; |
| 2129 | } |
| 2130 | switch (mpage) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2131 | case 0x8: /* Caching Mode page */ |
| 2132 | if (caching_pg[1] == arr[off + 1]) { |
| 2133 | memcpy(caching_pg + 2, arr + off + 2, |
| 2134 | sizeof(caching_pg) - 2); |
| 2135 | goto set_mode_changed_ua; |
| 2136 | } |
| 2137 | break; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2138 | case 0xa: /* Control Mode page */ |
| 2139 | if (ctrl_m_pg[1] == arr[off + 1]) { |
| 2140 | memcpy(ctrl_m_pg + 2, arr + off + 2, |
| 2141 | sizeof(ctrl_m_pg) - 2); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2142 | sdebug_dsense = !!(ctrl_m_pg[2] & 0x4); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2143 | goto set_mode_changed_ua; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2144 | } |
| 2145 | break; |
| 2146 | case 0x1c: /* Informational Exceptions Mode page */ |
| 2147 | if (iec_m_pg[1] == arr[off + 1]) { |
| 2148 | memcpy(iec_m_pg + 2, arr + off + 2, |
| 2149 | sizeof(iec_m_pg) - 2); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2150 | goto set_mode_changed_ua; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2151 | } |
| 2152 | break; |
| 2153 | default: |
| 2154 | break; |
| 2155 | } |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2156 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2157 | return check_condition_result; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2158 | set_mode_changed_ua: |
| 2159 | set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm); |
| 2160 | return 0; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | static int resp_temp_l_pg(unsigned char * arr) |
| 2164 | { |
| 2165 | unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38, |
| 2166 | 0x0, 0x1, 0x3, 0x2, 0x0, 65, |
| 2167 | }; |
| 2168 | |
| 2169 | memcpy(arr, temp_l_pg, sizeof(temp_l_pg)); |
| 2170 | return sizeof(temp_l_pg); |
| 2171 | } |
| 2172 | |
| 2173 | static int resp_ie_l_pg(unsigned char * arr) |
| 2174 | { |
| 2175 | unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38, |
| 2176 | }; |
| 2177 | |
| 2178 | memcpy(arr, ie_l_pg, sizeof(ie_l_pg)); |
| 2179 | if (iec_m_pg[2] & 0x4) { /* TEST bit set */ |
| 2180 | arr[4] = THRESHOLD_EXCEEDED; |
| 2181 | arr[5] = 0xff; |
| 2182 | } |
| 2183 | return sizeof(ie_l_pg); |
| 2184 | } |
| 2185 | |
| 2186 | #define SDEBUG_MAX_LSENSE_SZ 512 |
| 2187 | |
| 2188 | static int resp_log_sense(struct scsi_cmnd * scp, |
| 2189 | struct sdebug_dev_info * devip) |
| 2190 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2191 | int ppc, sp, pcontrol, pcode, subpcode, alloc_len, len, n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2192 | unsigned char arr[SDEBUG_MAX_LSENSE_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2193 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2194 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2195 | memset(arr, 0, sizeof(arr)); |
| 2196 | ppc = cmd[1] & 0x2; |
| 2197 | sp = cmd[1] & 0x1; |
| 2198 | if (ppc || sp) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2199 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2200 | return check_condition_result; |
| 2201 | } |
| 2202 | pcontrol = (cmd[2] & 0xc0) >> 6; |
| 2203 | pcode = cmd[2] & 0x3f; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2204 | subpcode = cmd[3] & 0xff; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2205 | alloc_len = get_unaligned_be16(cmd + 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2206 | arr[0] = pcode; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2207 | if (0 == subpcode) { |
| 2208 | switch (pcode) { |
| 2209 | case 0x0: /* Supported log pages log page */ |
| 2210 | n = 4; |
| 2211 | arr[n++] = 0x0; /* this page */ |
| 2212 | arr[n++] = 0xd; /* Temperature */ |
| 2213 | arr[n++] = 0x2f; /* Informational exceptions */ |
| 2214 | arr[3] = n - 4; |
| 2215 | break; |
| 2216 | case 0xd: /* Temperature log page */ |
| 2217 | arr[3] = resp_temp_l_pg(arr + 4); |
| 2218 | break; |
| 2219 | case 0x2f: /* Informational exceptions log page */ |
| 2220 | arr[3] = resp_ie_l_pg(arr + 4); |
| 2221 | break; |
| 2222 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2223 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2224 | return check_condition_result; |
| 2225 | } |
| 2226 | } else if (0xff == subpcode) { |
| 2227 | arr[0] |= 0x40; |
| 2228 | arr[1] = subpcode; |
| 2229 | switch (pcode) { |
| 2230 | case 0x0: /* Supported log pages and subpages log page */ |
| 2231 | n = 4; |
| 2232 | arr[n++] = 0x0; |
| 2233 | arr[n++] = 0x0; /* 0,0 page */ |
| 2234 | arr[n++] = 0x0; |
| 2235 | arr[n++] = 0xff; /* this page */ |
| 2236 | arr[n++] = 0xd; |
| 2237 | arr[n++] = 0x0; /* Temperature */ |
| 2238 | arr[n++] = 0x2f; |
| 2239 | arr[n++] = 0x0; /* Informational exceptions */ |
| 2240 | arr[3] = n - 4; |
| 2241 | break; |
| 2242 | case 0xd: /* Temperature subpages */ |
| 2243 | n = 4; |
| 2244 | arr[n++] = 0xd; |
| 2245 | arr[n++] = 0x0; /* Temperature */ |
| 2246 | arr[3] = n - 4; |
| 2247 | break; |
| 2248 | case 0x2f: /* Informational exceptions subpages */ |
| 2249 | n = 4; |
| 2250 | arr[n++] = 0x2f; |
| 2251 | arr[n++] = 0x0; /* Informational exceptions */ |
| 2252 | arr[3] = n - 4; |
| 2253 | break; |
| 2254 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2255 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2256 | return check_condition_result; |
| 2257 | } |
| 2258 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2259 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2260 | return check_condition_result; |
| 2261 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2262 | len = min(get_unaligned_be16(arr + 2) + 4, alloc_len); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2263 | return fill_from_dev_buffer(scp, arr, |
| 2264 | min(len, SDEBUG_MAX_INQ_ARR_SZ)); |
| 2265 | } |
| 2266 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2267 | static int check_device_access_params(struct scsi_cmnd *scp, |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2268 | unsigned long long lba, unsigned int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2270 | if (lba + num > sdebug_capacity) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2271 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | return check_condition_result; |
| 2273 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2274 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 2275 | if (num > sdebug_store_sectors) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2276 | /* needs work to find which cdb byte 'num' comes from */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2277 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2278 | return check_condition_result; |
| 2279 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2280 | return 0; |
| 2281 | } |
| 2282 | |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2283 | /* Returns number of bytes copied or -1 if error. */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2284 | static int |
| 2285 | do_device_access(struct scsi_cmnd *scmd, u64 lba, u32 num, bool do_write) |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2286 | { |
| 2287 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2288 | u64 block, rest = 0; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2289 | struct scsi_data_buffer *sdb; |
| 2290 | enum dma_data_direction dir; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2291 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2292 | if (do_write) { |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2293 | sdb = scsi_out(scmd); |
| 2294 | dir = DMA_TO_DEVICE; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2295 | } else { |
| 2296 | sdb = scsi_in(scmd); |
| 2297 | dir = DMA_FROM_DEVICE; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | if (!sdb->length) |
| 2301 | return 0; |
| 2302 | if (!(scsi_bidi_cmnd(scmd) || scmd->sc_data_direction == dir)) |
| 2303 | return -1; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2304 | |
| 2305 | block = do_div(lba, sdebug_store_sectors); |
| 2306 | if (block + num > sdebug_store_sectors) |
| 2307 | rest = block + num - sdebug_store_sectors; |
| 2308 | |
Dave Gordon | 386ecb1 | 2015-06-30 14:58:57 -0700 | [diff] [blame] | 2309 | ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2310 | fake_storep + (block * sdebug_sector_size), |
| 2311 | (num - rest) * sdebug_sector_size, 0, do_write); |
| 2312 | if (ret != (num - rest) * sdebug_sector_size) |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2313 | return ret; |
| 2314 | |
| 2315 | if (rest) { |
Dave Gordon | 386ecb1 | 2015-06-30 14:58:57 -0700 | [diff] [blame] | 2316 | ret += sg_copy_buffer(sdb->table.sgl, sdb->table.nents, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2317 | fake_storep, rest * sdebug_sector_size, |
| 2318 | (num - rest) * sdebug_sector_size, do_write); |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2319 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2320 | |
| 2321 | return ret; |
| 2322 | } |
| 2323 | |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2324 | /* If fake_store(lba,num) compares equal to arr(num), then copy top half of |
| 2325 | * arr into fake_store(lba,num) and return true. If comparison fails then |
| 2326 | * return false. */ |
| 2327 | static bool |
| 2328 | comp_write_worker(u64 lba, u32 num, const u8 *arr) |
| 2329 | { |
| 2330 | bool res; |
| 2331 | u64 block, rest = 0; |
| 2332 | u32 store_blks = sdebug_store_sectors; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2333 | u32 lb_size = sdebug_sector_size; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2334 | |
| 2335 | block = do_div(lba, store_blks); |
| 2336 | if (block + num > store_blks) |
| 2337 | rest = block + num - store_blks; |
| 2338 | |
| 2339 | res = !memcmp(fake_storep + (block * lb_size), arr, |
| 2340 | (num - rest) * lb_size); |
| 2341 | if (!res) |
| 2342 | return res; |
| 2343 | if (rest) |
| 2344 | res = memcmp(fake_storep, arr + ((num - rest) * lb_size), |
| 2345 | rest * lb_size); |
| 2346 | if (!res) |
| 2347 | return res; |
| 2348 | arr += num * lb_size; |
| 2349 | memcpy(fake_storep + (block * lb_size), arr, (num - rest) * lb_size); |
| 2350 | if (rest) |
| 2351 | memcpy(fake_storep, arr + ((num - rest) * lb_size), |
| 2352 | rest * lb_size); |
| 2353 | return res; |
| 2354 | } |
| 2355 | |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2356 | static __be16 dif_compute_csum(const void *buf, int len) |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2357 | { |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2358 | __be16 csum; |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2359 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2360 | if (sdebug_guard) |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2361 | csum = (__force __be16)ip_compute_csum(buf, len); |
| 2362 | else |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2363 | csum = cpu_to_be16(crc_t10dif(buf, len)); |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2364 | |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2365 | return csum; |
| 2366 | } |
| 2367 | |
| 2368 | static int dif_verify(struct sd_dif_tuple *sdt, const void *data, |
| 2369 | sector_t sector, u32 ei_lba) |
| 2370 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2371 | __be16 csum = dif_compute_csum(data, sdebug_sector_size); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2372 | |
| 2373 | if (sdt->guard_tag != csum) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2374 | 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] | 2375 | (unsigned long)sector, |
| 2376 | be16_to_cpu(sdt->guard_tag), |
| 2377 | be16_to_cpu(csum)); |
| 2378 | return 0x01; |
| 2379 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2380 | if (sdebug_dif == SD_DIF_TYPE1_PROTECTION && |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2381 | be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2382 | pr_err("REF check failed on sector %lu\n", |
| 2383 | (unsigned long)sector); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2384 | return 0x03; |
| 2385 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2386 | if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2387 | be32_to_cpu(sdt->ref_tag) != ei_lba) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2388 | pr_err("REF check failed on sector %lu\n", |
| 2389 | (unsigned long)sector); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2390 | return 0x03; |
| 2391 | } |
| 2392 | return 0; |
| 2393 | } |
| 2394 | |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2395 | static void dif_copy_prot(struct scsi_cmnd *SCpnt, sector_t sector, |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2396 | unsigned int sectors, bool read) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2397 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2398 | size_t resid; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2399 | void *paddr; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2400 | const void *dif_store_end = dif_storep + sdebug_store_sectors; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2401 | struct sg_mapping_iter miter; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2402 | |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 2403 | /* Bytes of protection data to copy into sgl */ |
| 2404 | resid = sectors * sizeof(*dif_storep); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2405 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2406 | sg_miter_start(&miter, scsi_prot_sglist(SCpnt), |
| 2407 | scsi_prot_sg_count(SCpnt), SG_MITER_ATOMIC | |
| 2408 | (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG)); |
| 2409 | |
| 2410 | while (sg_miter_next(&miter) && resid > 0) { |
| 2411 | size_t len = min(miter.length, resid); |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2412 | void *start = dif_store(sector); |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2413 | size_t rest = 0; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2414 | |
| 2415 | if (dif_store_end < start + len) |
| 2416 | rest = start + len - dif_store_end; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2417 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2418 | paddr = miter.addr; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2419 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2420 | if (read) |
| 2421 | memcpy(paddr, start, len - rest); |
| 2422 | else |
| 2423 | memcpy(start, paddr, len - rest); |
| 2424 | |
| 2425 | if (rest) { |
| 2426 | if (read) |
| 2427 | memcpy(paddr + len - rest, dif_storep, rest); |
| 2428 | else |
| 2429 | memcpy(dif_storep, paddr + len - rest, rest); |
| 2430 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2431 | |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 2432 | sector += len / sizeof(*dif_storep); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2433 | resid -= len; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2434 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2435 | sg_miter_stop(&miter); |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2436 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2437 | |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2438 | static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec, |
| 2439 | unsigned int sectors, u32 ei_lba) |
| 2440 | { |
| 2441 | unsigned int i; |
| 2442 | struct sd_dif_tuple *sdt; |
| 2443 | sector_t sector; |
| 2444 | |
Akinobu Mita | c45eabec | 2014-02-26 22:56:58 +0900 | [diff] [blame] | 2445 | for (i = 0; i < sectors; i++, ei_lba++) { |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2446 | int ret; |
| 2447 | |
| 2448 | sector = start_sec + i; |
| 2449 | sdt = dif_store(sector); |
| 2450 | |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2451 | if (sdt->app_tag == cpu_to_be16(0xffff)) |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2452 | continue; |
| 2453 | |
| 2454 | ret = dif_verify(sdt, fake_store(sector), sector, ei_lba); |
| 2455 | if (ret) { |
| 2456 | dif_errors++; |
| 2457 | return ret; |
| 2458 | } |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2459 | } |
| 2460 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2461 | dif_copy_prot(SCpnt, start_sec, sectors, true); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2462 | dix_reads++; |
| 2463 | |
| 2464 | return 0; |
| 2465 | } |
| 2466 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2467 | static int |
| 2468 | resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2469 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2470 | u8 *cmd = scp->cmnd; |
| 2471 | u64 lba; |
| 2472 | u32 num; |
| 2473 | u32 ei_lba; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2474 | unsigned long iflags; |
| 2475 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2476 | bool check_prot; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2477 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2478 | switch (cmd[0]) { |
| 2479 | case READ_16: |
| 2480 | ei_lba = 0; |
| 2481 | lba = get_unaligned_be64(cmd + 2); |
| 2482 | num = get_unaligned_be32(cmd + 10); |
| 2483 | check_prot = true; |
| 2484 | break; |
| 2485 | case READ_10: |
| 2486 | ei_lba = 0; |
| 2487 | lba = get_unaligned_be32(cmd + 2); |
| 2488 | num = get_unaligned_be16(cmd + 7); |
| 2489 | check_prot = true; |
| 2490 | break; |
| 2491 | case READ_6: |
| 2492 | ei_lba = 0; |
| 2493 | lba = (u32)cmd[3] | (u32)cmd[2] << 8 | |
| 2494 | (u32)(cmd[1] & 0x1f) << 16; |
| 2495 | num = (0 == cmd[4]) ? 256 : cmd[4]; |
| 2496 | check_prot = true; |
| 2497 | break; |
| 2498 | case READ_12: |
| 2499 | ei_lba = 0; |
| 2500 | lba = get_unaligned_be32(cmd + 2); |
| 2501 | num = get_unaligned_be32(cmd + 6); |
| 2502 | check_prot = true; |
| 2503 | break; |
| 2504 | case XDWRITEREAD_10: |
| 2505 | ei_lba = 0; |
| 2506 | lba = get_unaligned_be32(cmd + 2); |
| 2507 | num = get_unaligned_be16(cmd + 7); |
| 2508 | check_prot = false; |
| 2509 | break; |
| 2510 | default: /* assume READ(32) */ |
| 2511 | lba = get_unaligned_be64(cmd + 12); |
| 2512 | ei_lba = get_unaligned_be32(cmd + 20); |
| 2513 | num = get_unaligned_be32(cmd + 28); |
| 2514 | check_prot = false; |
| 2515 | break; |
| 2516 | } |
| 2517 | if (check_prot) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2518 | if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2519 | (cmd[1] & 0xe0)) { |
| 2520 | mk_sense_invalid_opcode(scp); |
| 2521 | return check_condition_result; |
| 2522 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2523 | if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION || |
| 2524 | sdebug_dif == SD_DIF_TYPE3_PROTECTION) && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2525 | (cmd[1] & 0xe0) == 0) |
| 2526 | sdev_printk(KERN_ERR, scp->device, "Unprotected RD " |
| 2527 | "to DIF device\n"); |
| 2528 | } |
| 2529 | if (sdebug_any_injecting_opt) { |
| 2530 | struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp); |
| 2531 | |
| 2532 | if (ep->inj_short) |
| 2533 | num /= 2; |
| 2534 | } |
| 2535 | |
| 2536 | /* inline check_device_access_params() */ |
| 2537 | if (lba + num > sdebug_capacity) { |
| 2538 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 2539 | return check_condition_result; |
| 2540 | } |
| 2541 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 2542 | if (num > sdebug_store_sectors) { |
| 2543 | /* needs work to find which cdb byte 'num' comes from */ |
| 2544 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 2545 | return check_condition_result; |
| 2546 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2547 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2548 | if ((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) && |
Douglas Gilbert | 32f7ef7 | 2011-03-11 10:43:35 -0500 | [diff] [blame] | 2549 | (lba <= (OPT_MEDIUM_ERR_ADDR + OPT_MEDIUM_ERR_NUM - 1)) && |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2550 | ((lba + num) > OPT_MEDIUM_ERR_ADDR)) { |
| 2551 | /* claim unrecoverable read error */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2552 | mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2553 | /* set info field and valid bit for fixed descriptor */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2554 | if (0x70 == (scp->sense_buffer[0] & 0x7f)) { |
| 2555 | scp->sense_buffer[0] |= 0x80; /* Valid bit */ |
Douglas Gilbert | 32f7ef7 | 2011-03-11 10:43:35 -0500 | [diff] [blame] | 2556 | ret = (lba < OPT_MEDIUM_ERR_ADDR) |
| 2557 | ? OPT_MEDIUM_ERR_ADDR : (int)lba; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2558 | put_unaligned_be32(ret, scp->sense_buffer + 3); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2559 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2560 | scsi_set_resid(scp, scsi_bufflen(scp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | return check_condition_result; |
| 2562 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2563 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2564 | read_lock_irqsave(&atomic_rw, iflags); |
| 2565 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2566 | /* DIX + T10 DIF */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2567 | if (sdebug_dix && scsi_prot_sg_count(scp)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2568 | int prot_ret = prot_verify_read(scp, lba, num, ei_lba); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2569 | |
| 2570 | if (prot_ret) { |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2571 | read_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2572 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2573 | return illegal_condition_result; |
| 2574 | } |
| 2575 | } |
| 2576 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2577 | ret = do_device_access(scp, lba, num, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | read_unlock_irqrestore(&atomic_rw, iflags); |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2579 | if (ret == -1) |
| 2580 | return DID_ERROR << 16; |
| 2581 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2582 | scsi_in(scp)->resid = scsi_bufflen(scp) - ret; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2583 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2584 | if (sdebug_any_injecting_opt) { |
| 2585 | struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp); |
| 2586 | |
| 2587 | if (ep->inj_recovered) { |
| 2588 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 2589 | THRESHOLD_EXCEEDED, 0); |
| 2590 | return check_condition_result; |
| 2591 | } else if (ep->inj_transport) { |
| 2592 | mk_sense_buffer(scp, ABORTED_COMMAND, |
| 2593 | TRANSPORT_PROBLEM, ACK_NAK_TO); |
| 2594 | return check_condition_result; |
| 2595 | } else if (ep->inj_dif) { |
| 2596 | /* Logical block guard check failed */ |
| 2597 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); |
| 2598 | return illegal_condition_result; |
| 2599 | } else if (ep->inj_dix) { |
| 2600 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); |
| 2601 | return illegal_condition_result; |
| 2602 | } |
| 2603 | } |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2604 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | } |
| 2606 | |
Tomas Winkler | 58a8635 | 2015-07-28 16:54:23 +0300 | [diff] [blame] | 2607 | static void dump_sector(unsigned char *buf, int len) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2608 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2609 | int i, j, n; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2610 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2611 | pr_err(">>> Sector Dump <<<\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2612 | for (i = 0 ; i < len ; i += 16) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2613 | char b[128]; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2614 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2615 | for (j = 0, n = 0; j < 16; j++) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2616 | unsigned char c = buf[i+j]; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2617 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2618 | if (c >= 0x20 && c < 0x7e) |
| 2619 | n += scnprintf(b + n, sizeof(b) - n, |
| 2620 | " %c ", buf[i+j]); |
| 2621 | else |
| 2622 | n += scnprintf(b + n, sizeof(b) - n, |
| 2623 | "%02x ", buf[i+j]); |
| 2624 | } |
| 2625 | pr_err("%04d: %s\n", i, b); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2626 | } |
| 2627 | } |
| 2628 | |
| 2629 | 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] | 2630 | unsigned int sectors, u32 ei_lba) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2631 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2632 | int ret; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2633 | struct sd_dif_tuple *sdt; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2634 | void *daddr; |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2635 | sector_t sector = start_sec; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2636 | int ppage_offset; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2637 | int dpage_offset; |
| 2638 | struct sg_mapping_iter diter; |
| 2639 | struct sg_mapping_iter piter; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2640 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2641 | BUG_ON(scsi_sg_count(SCpnt) == 0); |
| 2642 | BUG_ON(scsi_prot_sg_count(SCpnt) == 0); |
| 2643 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2644 | sg_miter_start(&piter, scsi_prot_sglist(SCpnt), |
| 2645 | scsi_prot_sg_count(SCpnt), |
| 2646 | SG_MITER_ATOMIC | SG_MITER_FROM_SG); |
| 2647 | sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt), |
| 2648 | SG_MITER_ATOMIC | SG_MITER_FROM_SG); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2649 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2650 | /* For each protection page */ |
| 2651 | while (sg_miter_next(&piter)) { |
| 2652 | dpage_offset = 0; |
| 2653 | if (WARN_ON(!sg_miter_next(&diter))) { |
| 2654 | ret = 0x01; |
| 2655 | goto out; |
| 2656 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2657 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2658 | for (ppage_offset = 0; ppage_offset < piter.length; |
| 2659 | ppage_offset += sizeof(struct sd_dif_tuple)) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2660 | /* If we're at the end of the current |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2661 | * data page advance to the next one |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2662 | */ |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2663 | if (dpage_offset >= diter.length) { |
| 2664 | if (WARN_ON(!sg_miter_next(&diter))) { |
| 2665 | ret = 0x01; |
| 2666 | goto out; |
| 2667 | } |
| 2668 | dpage_offset = 0; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2669 | } |
| 2670 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2671 | sdt = piter.addr + ppage_offset; |
| 2672 | daddr = diter.addr + dpage_offset; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2673 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2674 | ret = dif_verify(sdt, daddr, sector, ei_lba); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2675 | if (ret) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2676 | dump_sector(daddr, sdebug_sector_size); |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 2677 | goto out; |
| 2678 | } |
| 2679 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2680 | sector++; |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 2681 | ei_lba++; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2682 | dpage_offset += sdebug_sector_size; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2683 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2684 | diter.consumed = dpage_offset; |
| 2685 | sg_miter_stop(&diter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2686 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2687 | sg_miter_stop(&piter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2688 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2689 | dif_copy_prot(SCpnt, start_sec, sectors, false); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2690 | dix_writes++; |
| 2691 | |
| 2692 | return 0; |
| 2693 | |
| 2694 | out: |
| 2695 | dif_errors++; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2696 | sg_miter_stop(&diter); |
| 2697 | sg_miter_stop(&piter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2698 | return ret; |
| 2699 | } |
| 2700 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2701 | static unsigned long lba_to_map_index(sector_t lba) |
| 2702 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2703 | if (sdebug_unmap_alignment) |
| 2704 | lba += sdebug_unmap_granularity - sdebug_unmap_alignment; |
| 2705 | sector_div(lba, sdebug_unmap_granularity); |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2706 | return lba; |
| 2707 | } |
| 2708 | |
| 2709 | static sector_t map_index_to_lba(unsigned long index) |
| 2710 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2711 | sector_t lba = index * sdebug_unmap_granularity; |
Akinobu Mita | a027b5b | 2013-08-26 22:08:41 +0900 | [diff] [blame] | 2712 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2713 | if (sdebug_unmap_alignment) |
| 2714 | lba -= sdebug_unmap_granularity - sdebug_unmap_alignment; |
Akinobu Mita | a027b5b | 2013-08-26 22:08:41 +0900 | [diff] [blame] | 2715 | return lba; |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2716 | } |
| 2717 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2718 | static unsigned int map_state(sector_t lba, unsigned int *num) |
| 2719 | { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2720 | sector_t end; |
| 2721 | unsigned int mapped; |
| 2722 | unsigned long index; |
| 2723 | unsigned long next; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2724 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2725 | index = lba_to_map_index(lba); |
| 2726 | mapped = test_bit(index, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2727 | |
| 2728 | if (mapped) |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2729 | next = find_next_zero_bit(map_storep, map_size, index); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2730 | else |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2731 | next = find_next_bit(map_storep, map_size, index); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2732 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2733 | 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] | 2734 | *num = end - lba; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2735 | return mapped; |
| 2736 | } |
| 2737 | |
| 2738 | static void map_region(sector_t lba, unsigned int len) |
| 2739 | { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2740 | sector_t end = lba + len; |
| 2741 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2742 | while (lba < end) { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2743 | unsigned long index = lba_to_map_index(lba); |
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 | if (index < map_size) |
| 2746 | set_bit(index, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2747 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2748 | lba = map_index_to_lba(index + 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | static void unmap_region(sector_t lba, unsigned int len) |
| 2753 | { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2754 | sector_t end = lba + len; |
| 2755 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2756 | while (lba < end) { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2757 | unsigned long index = lba_to_map_index(lba); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2758 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2759 | if (lba == map_index_to_lba(index) && |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2760 | lba + sdebug_unmap_granularity <= end && |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2761 | index < map_size) { |
| 2762 | clear_bit(index, map_storep); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2763 | if (sdebug_lbprz) { |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 2764 | memset(fake_storep + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2765 | lba * sdebug_sector_size, 0, |
| 2766 | sdebug_sector_size * |
| 2767 | sdebug_unmap_granularity); |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2768 | } |
Akinobu Mita | e9926b4 | 2013-06-29 17:59:17 +0900 | [diff] [blame] | 2769 | if (dif_storep) { |
| 2770 | memset(dif_storep + lba, 0xff, |
| 2771 | sizeof(*dif_storep) * |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2772 | sdebug_unmap_granularity); |
Akinobu Mita | e9926b4 | 2013-06-29 17:59:17 +0900 | [diff] [blame] | 2773 | } |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 2774 | } |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2775 | lba = map_index_to_lba(index + 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2779 | static int |
| 2780 | resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2782 | u8 *cmd = scp->cmnd; |
| 2783 | u64 lba; |
| 2784 | u32 num; |
| 2785 | u32 ei_lba; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | unsigned long iflags; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2787 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2788 | bool check_prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2790 | switch (cmd[0]) { |
| 2791 | case WRITE_16: |
| 2792 | ei_lba = 0; |
| 2793 | lba = get_unaligned_be64(cmd + 2); |
| 2794 | num = get_unaligned_be32(cmd + 10); |
| 2795 | check_prot = true; |
| 2796 | break; |
| 2797 | case WRITE_10: |
| 2798 | ei_lba = 0; |
| 2799 | lba = get_unaligned_be32(cmd + 2); |
| 2800 | num = get_unaligned_be16(cmd + 7); |
| 2801 | check_prot = true; |
| 2802 | break; |
| 2803 | case WRITE_6: |
| 2804 | ei_lba = 0; |
| 2805 | lba = (u32)cmd[3] | (u32)cmd[2] << 8 | |
| 2806 | (u32)(cmd[1] & 0x1f) << 16; |
| 2807 | num = (0 == cmd[4]) ? 256 : cmd[4]; |
| 2808 | check_prot = true; |
| 2809 | break; |
| 2810 | case WRITE_12: |
| 2811 | ei_lba = 0; |
| 2812 | lba = get_unaligned_be32(cmd + 2); |
| 2813 | num = get_unaligned_be32(cmd + 6); |
| 2814 | check_prot = true; |
| 2815 | break; |
| 2816 | case 0x53: /* XDWRITEREAD(10) */ |
| 2817 | ei_lba = 0; |
| 2818 | lba = get_unaligned_be32(cmd + 2); |
| 2819 | num = get_unaligned_be16(cmd + 7); |
| 2820 | check_prot = false; |
| 2821 | break; |
| 2822 | default: /* assume WRITE(32) */ |
| 2823 | lba = get_unaligned_be64(cmd + 12); |
| 2824 | ei_lba = get_unaligned_be32(cmd + 20); |
| 2825 | num = get_unaligned_be32(cmd + 28); |
| 2826 | check_prot = false; |
| 2827 | break; |
| 2828 | } |
| 2829 | if (check_prot) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2830 | if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2831 | (cmd[1] & 0xe0)) { |
| 2832 | mk_sense_invalid_opcode(scp); |
| 2833 | return check_condition_result; |
| 2834 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2835 | if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION || |
| 2836 | sdebug_dif == SD_DIF_TYPE3_PROTECTION) && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2837 | (cmd[1] & 0xe0) == 0) |
| 2838 | sdev_printk(KERN_ERR, scp->device, "Unprotected WR " |
| 2839 | "to DIF device\n"); |
| 2840 | } |
| 2841 | |
| 2842 | /* inline check_device_access_params() */ |
| 2843 | if (lba + num > sdebug_capacity) { |
| 2844 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 2845 | return check_condition_result; |
| 2846 | } |
| 2847 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 2848 | if (num > sdebug_store_sectors) { |
| 2849 | /* needs work to find which cdb byte 'num' comes from */ |
| 2850 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 2851 | return check_condition_result; |
| 2852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2854 | write_lock_irqsave(&atomic_rw, iflags); |
| 2855 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2856 | /* DIX + T10 DIF */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2857 | if (sdebug_dix && scsi_prot_sg_count(scp)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2858 | int prot_ret = prot_verify_write(scp, lba, num, ei_lba); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2859 | |
| 2860 | if (prot_ret) { |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2861 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2862 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2863 | return illegal_condition_result; |
| 2864 | } |
| 2865 | } |
| 2866 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2867 | ret = do_device_access(scp, lba, num, true); |
Akinobu Mita | 9ed8d3d | 2013-04-16 22:11:55 +0900 | [diff] [blame] | 2868 | if (scsi_debug_lbp()) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2869 | map_region(lba, num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | write_unlock_irqrestore(&atomic_rw, iflags); |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2871 | if (-1 == ret) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2872 | return DID_ERROR << 16; |
| 2873 | else if (sdebug_verbose && (ret < (num * sdebug_sector_size))) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2874 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2875 | "%s: write: cdb indicated=%u, IO sent=%d bytes\n", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2876 | my_name, num * sdebug_sector_size, ret); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2877 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2878 | if (sdebug_any_injecting_opt) { |
| 2879 | struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp); |
| 2880 | |
| 2881 | if (ep->inj_recovered) { |
| 2882 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 2883 | THRESHOLD_EXCEEDED, 0); |
| 2884 | return check_condition_result; |
| 2885 | } else if (ep->inj_dif) { |
| 2886 | /* Logical block guard check failed */ |
| 2887 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); |
| 2888 | return illegal_condition_result; |
| 2889 | } else if (ep->inj_dix) { |
| 2890 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); |
| 2891 | return illegal_condition_result; |
| 2892 | } |
| 2893 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | return 0; |
| 2895 | } |
| 2896 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2897 | static int |
| 2898 | resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num, u32 ei_lba, |
| 2899 | bool unmap, bool ndob) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2900 | { |
| 2901 | unsigned long iflags; |
| 2902 | unsigned long long i; |
| 2903 | int ret; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2904 | u64 lba_off; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2905 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2906 | ret = check_device_access_params(scp, lba, num); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2907 | if (ret) |
| 2908 | return ret; |
| 2909 | |
| 2910 | write_lock_irqsave(&atomic_rw, iflags); |
| 2911 | |
Akinobu Mita | 9ed8d3d | 2013-04-16 22:11:55 +0900 | [diff] [blame] | 2912 | if (unmap && scsi_debug_lbp()) { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2913 | unmap_region(lba, num); |
| 2914 | goto out; |
| 2915 | } |
| 2916 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2917 | lba_off = lba * sdebug_sector_size; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2918 | /* if ndob then zero 1 logical block, else fetch 1 logical block */ |
| 2919 | if (ndob) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2920 | memset(fake_storep + lba_off, 0, sdebug_sector_size); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2921 | ret = 0; |
| 2922 | } else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2923 | ret = fetch_to_dev_buffer(scp, fake_storep + lba_off, |
| 2924 | sdebug_sector_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2925 | |
| 2926 | if (-1 == ret) { |
| 2927 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2928 | return DID_ERROR << 16; |
| 2929 | } else if (sdebug_verbose && (ret < (num * sdebug_sector_size))) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2930 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2931 | "%s: %s: cdb indicated=%u, IO sent=%d bytes\n", |
| 2932 | my_name, "write same", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2933 | num * sdebug_sector_size, ret); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2934 | |
| 2935 | /* Copy first sector to remaining blocks */ |
| 2936 | for (i = 1 ; i < num ; i++) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2937 | memcpy(fake_storep + ((lba + i) * sdebug_sector_size), |
| 2938 | fake_storep + lba_off, |
| 2939 | sdebug_sector_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2940 | |
Akinobu Mita | 9ed8d3d | 2013-04-16 22:11:55 +0900 | [diff] [blame] | 2941 | if (scsi_debug_lbp()) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2942 | map_region(lba, num); |
| 2943 | out: |
| 2944 | write_unlock_irqrestore(&atomic_rw, iflags); |
| 2945 | |
| 2946 | return 0; |
| 2947 | } |
| 2948 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2949 | static int |
| 2950 | resp_write_same_10(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
| 2951 | { |
| 2952 | u8 *cmd = scp->cmnd; |
| 2953 | u32 lba; |
| 2954 | u16 num; |
| 2955 | u32 ei_lba = 0; |
| 2956 | bool unmap = false; |
| 2957 | |
| 2958 | if (cmd[1] & 0x8) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2959 | if (sdebug_lbpws10 == 0) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2960 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); |
| 2961 | return check_condition_result; |
| 2962 | } else |
| 2963 | unmap = true; |
| 2964 | } |
| 2965 | lba = get_unaligned_be32(cmd + 2); |
| 2966 | num = get_unaligned_be16(cmd + 7); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2967 | if (num > sdebug_write_same_length) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2968 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); |
| 2969 | return check_condition_result; |
| 2970 | } |
| 2971 | return resp_write_same(scp, lba, num, ei_lba, unmap, false); |
| 2972 | } |
| 2973 | |
| 2974 | static int |
| 2975 | resp_write_same_16(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
| 2976 | { |
| 2977 | u8 *cmd = scp->cmnd; |
| 2978 | u64 lba; |
| 2979 | u32 num; |
| 2980 | u32 ei_lba = 0; |
| 2981 | bool unmap = false; |
| 2982 | bool ndob = false; |
| 2983 | |
| 2984 | if (cmd[1] & 0x8) { /* UNMAP */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2985 | if (sdebug_lbpws == 0) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2986 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); |
| 2987 | return check_condition_result; |
| 2988 | } else |
| 2989 | unmap = true; |
| 2990 | } |
| 2991 | if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */ |
| 2992 | ndob = true; |
| 2993 | lba = get_unaligned_be64(cmd + 2); |
| 2994 | num = get_unaligned_be32(cmd + 10); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2995 | if (num > sdebug_write_same_length) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2996 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1); |
| 2997 | return check_condition_result; |
| 2998 | } |
| 2999 | return resp_write_same(scp, lba, num, ei_lba, unmap, ndob); |
| 3000 | } |
| 3001 | |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 3002 | /* Note the mode field is in the same position as the (lower) service action |
| 3003 | * field. For the Report supported operation codes command, SPC-4 suggests |
| 3004 | * each mode of this command should be reported separately; for future. */ |
| 3005 | static int |
| 3006 | resp_write_buffer(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
| 3007 | { |
| 3008 | u8 *cmd = scp->cmnd; |
| 3009 | struct scsi_device *sdp = scp->device; |
| 3010 | struct sdebug_dev_info *dp; |
| 3011 | u8 mode; |
| 3012 | |
| 3013 | mode = cmd[1] & 0x1f; |
| 3014 | switch (mode) { |
| 3015 | case 0x4: /* download microcode (MC) and activate (ACT) */ |
| 3016 | /* set UAs on this device only */ |
| 3017 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3018 | set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm); |
| 3019 | break; |
| 3020 | case 0x5: /* download MC, save and ACT */ |
| 3021 | set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm); |
| 3022 | break; |
| 3023 | case 0x6: /* download MC with offsets and ACT */ |
| 3024 | /* set UAs on most devices (LUs) in this target */ |
| 3025 | list_for_each_entry(dp, |
| 3026 | &devip->sdbg_host->dev_info_list, |
| 3027 | dev_list) |
| 3028 | if (dp->target == sdp->id) { |
| 3029 | set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm); |
| 3030 | if (devip != dp) |
| 3031 | set_bit(SDEBUG_UA_MICROCODE_CHANGED, |
| 3032 | dp->uas_bm); |
| 3033 | } |
| 3034 | break; |
| 3035 | case 0x7: /* download MC with offsets, save, and ACT */ |
| 3036 | /* set UA on all devices (LUs) in this target */ |
| 3037 | list_for_each_entry(dp, |
| 3038 | &devip->sdbg_host->dev_info_list, |
| 3039 | dev_list) |
| 3040 | if (dp->target == sdp->id) |
| 3041 | set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, |
| 3042 | dp->uas_bm); |
| 3043 | break; |
| 3044 | default: |
| 3045 | /* do nothing for this command for other mode values */ |
| 3046 | break; |
| 3047 | } |
| 3048 | return 0; |
| 3049 | } |
| 3050 | |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3051 | static int |
| 3052 | resp_comp_write(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
| 3053 | { |
| 3054 | u8 *cmd = scp->cmnd; |
| 3055 | u8 *arr; |
| 3056 | u8 *fake_storep_hold; |
| 3057 | u64 lba; |
| 3058 | u32 dnum; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3059 | u32 lb_size = sdebug_sector_size; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3060 | u8 num; |
| 3061 | unsigned long iflags; |
| 3062 | int ret; |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3063 | int retval = 0; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3064 | |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3065 | lba = get_unaligned_be64(cmd + 2); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3066 | num = cmd[13]; /* 1 to a maximum of 255 logical blocks */ |
| 3067 | if (0 == num) |
| 3068 | return 0; /* degenerate case, not an error */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3069 | if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3070 | (cmd[1] & 0xe0)) { |
| 3071 | mk_sense_invalid_opcode(scp); |
| 3072 | return check_condition_result; |
| 3073 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3074 | if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION || |
| 3075 | sdebug_dif == SD_DIF_TYPE3_PROTECTION) && |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3076 | (cmd[1] & 0xe0) == 0) |
| 3077 | sdev_printk(KERN_ERR, scp->device, "Unprotected WR " |
| 3078 | "to DIF device\n"); |
| 3079 | |
| 3080 | /* inline check_device_access_params() */ |
| 3081 | if (lba + num > sdebug_capacity) { |
| 3082 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 3083 | return check_condition_result; |
| 3084 | } |
| 3085 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 3086 | if (num > sdebug_store_sectors) { |
| 3087 | /* needs work to find which cdb byte 'num' comes from */ |
| 3088 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 3089 | return check_condition_result; |
| 3090 | } |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3091 | dnum = 2 * num; |
| 3092 | arr = kzalloc(dnum * lb_size, GFP_ATOMIC); |
| 3093 | if (NULL == arr) { |
| 3094 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3095 | INSUFF_RES_ASCQ); |
| 3096 | return check_condition_result; |
| 3097 | } |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3098 | |
| 3099 | write_lock_irqsave(&atomic_rw, iflags); |
| 3100 | |
| 3101 | /* trick do_device_access() to fetch both compare and write buffers |
| 3102 | * from data-in into arr. Safe (atomic) since write_lock held. */ |
| 3103 | fake_storep_hold = fake_storep; |
| 3104 | fake_storep = arr; |
| 3105 | ret = do_device_access(scp, 0, dnum, true); |
| 3106 | fake_storep = fake_storep_hold; |
| 3107 | if (ret == -1) { |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3108 | retval = DID_ERROR << 16; |
| 3109 | goto cleanup; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3110 | } else if (sdebug_verbose && (ret < (dnum * lb_size))) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3111 | sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb " |
| 3112 | "indicated=%u, IO sent=%d bytes\n", my_name, |
| 3113 | dnum * lb_size, ret); |
| 3114 | if (!comp_write_worker(lba, num, arr)) { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3115 | mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3116 | retval = check_condition_result; |
| 3117 | goto cleanup; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3118 | } |
| 3119 | if (scsi_debug_lbp()) |
| 3120 | map_region(lba, num); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3121 | cleanup: |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3122 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3123 | kfree(arr); |
| 3124 | return retval; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3125 | } |
| 3126 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3127 | struct unmap_block_desc { |
| 3128 | __be64 lba; |
| 3129 | __be32 blocks; |
| 3130 | __be32 __reserved; |
| 3131 | }; |
| 3132 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3133 | static int |
| 3134 | resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3135 | { |
| 3136 | unsigned char *buf; |
| 3137 | struct unmap_block_desc *desc; |
| 3138 | unsigned int i, payload_len, descriptors; |
| 3139 | int ret; |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3140 | unsigned long iflags; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3141 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3142 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3143 | if (!scsi_debug_lbp()) |
| 3144 | return 0; /* fib and say its done */ |
| 3145 | payload_len = get_unaligned_be16(scp->cmnd + 7); |
| 3146 | BUG_ON(scsi_bufflen(scp) != payload_len); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3147 | |
| 3148 | descriptors = (payload_len - 8) / 16; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3149 | if (descriptors > sdebug_unmap_max_desc) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3150 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3151 | return check_condition_result; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3152 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3153 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3154 | buf = kmalloc(scsi_bufflen(scp), GFP_ATOMIC); |
| 3155 | if (!buf) { |
| 3156 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3157 | INSUFF_RES_ASCQ); |
| 3158 | return check_condition_result; |
| 3159 | } |
| 3160 | |
| 3161 | scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp)); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3162 | |
| 3163 | BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2); |
| 3164 | BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16); |
| 3165 | |
| 3166 | desc = (void *)&buf[8]; |
| 3167 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3168 | write_lock_irqsave(&atomic_rw, iflags); |
| 3169 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3170 | for (i = 0 ; i < descriptors ; i++) { |
| 3171 | unsigned long long lba = get_unaligned_be64(&desc[i].lba); |
| 3172 | unsigned int num = get_unaligned_be32(&desc[i].blocks); |
| 3173 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3174 | ret = check_device_access_params(scp, lba, num); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3175 | if (ret) |
| 3176 | goto out; |
| 3177 | |
| 3178 | unmap_region(lba, num); |
| 3179 | } |
| 3180 | |
| 3181 | ret = 0; |
| 3182 | |
| 3183 | out: |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3184 | write_unlock_irqrestore(&atomic_rw, iflags); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3185 | kfree(buf); |
| 3186 | |
| 3187 | return ret; |
| 3188 | } |
| 3189 | |
| 3190 | #define SDEBUG_GET_LBA_STATUS_LEN 32 |
| 3191 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3192 | static int |
| 3193 | resp_get_lba_status(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3194 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3195 | u8 *cmd = scp->cmnd; |
| 3196 | u64 lba; |
| 3197 | u32 alloc_len, mapped, num; |
| 3198 | u8 arr[SDEBUG_GET_LBA_STATUS_LEN]; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3199 | int ret; |
| 3200 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3201 | lba = get_unaligned_be64(cmd + 2); |
| 3202 | alloc_len = get_unaligned_be32(cmd + 10); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3203 | |
| 3204 | if (alloc_len < 24) |
| 3205 | return 0; |
| 3206 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3207 | ret = check_device_access_params(scp, lba, 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3208 | if (ret) |
| 3209 | return ret; |
| 3210 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3211 | if (scsi_debug_lbp()) |
| 3212 | mapped = map_state(lba, &num); |
| 3213 | else { |
| 3214 | mapped = 1; |
| 3215 | /* following just in case virtual_gb changed */ |
| 3216 | sdebug_capacity = get_sdebug_capacity(); |
| 3217 | if (sdebug_capacity - lba <= 0xffffffff) |
| 3218 | num = sdebug_capacity - lba; |
| 3219 | else |
| 3220 | num = 0xffffffff; |
| 3221 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3222 | |
| 3223 | memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3224 | put_unaligned_be32(20, arr); /* Parameter Data Length */ |
| 3225 | put_unaligned_be64(lba, arr + 8); /* LBA */ |
| 3226 | put_unaligned_be32(num, arr + 16); /* Number of blocks */ |
| 3227 | arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3228 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3229 | 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] | 3230 | } |
| 3231 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3232 | #define SDEBUG_RLUN_ARR_SZ 256 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3233 | |
| 3234 | static int resp_report_luns(struct scsi_cmnd * scp, |
| 3235 | struct sdebug_dev_info * devip) |
| 3236 | { |
| 3237 | unsigned int alloc_len; |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 3238 | int lun_cnt, i, upper, num, n, want_wlun, shortish; |
| 3239 | u64 lun; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 3240 | unsigned char *cmd = scp->cmnd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3241 | int select_report = (int)cmd[2]; |
| 3242 | struct scsi_lun *one_lun; |
| 3243 | unsigned char arr[SDEBUG_RLUN_ARR_SZ]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3244 | unsigned char * max_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 3246 | clear_luns_changed_on_target(devip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3247 | alloc_len = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24); |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 3248 | shortish = (alloc_len < 4); |
| 3249 | if (shortish || (select_report > 2)) { |
| 3250 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, shortish ? 6 : 2, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3251 | return check_condition_result; |
| 3252 | } |
| 3253 | /* can produce response with up to 16k luns (lun 0 to lun 16383) */ |
| 3254 | memset(arr, 0, SDEBUG_RLUN_ARR_SZ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3255 | lun_cnt = sdebug_max_luns; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3256 | if (1 == select_report) |
| 3257 | lun_cnt = 0; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3258 | else if (sdebug_no_lun_0 && (lun_cnt > 0)) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3259 | --lun_cnt; |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 3260 | want_wlun = (select_report > 0) ? 1 : 0; |
| 3261 | num = lun_cnt + want_wlun; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3262 | arr[2] = ((sizeof(struct scsi_lun) * num) >> 8) & 0xff; |
| 3263 | arr[3] = (sizeof(struct scsi_lun) * num) & 0xff; |
| 3264 | n = min((int)((SDEBUG_RLUN_ARR_SZ - 8) / |
| 3265 | sizeof(struct scsi_lun)), num); |
| 3266 | if (n < num) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 3267 | want_wlun = 0; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3268 | lun_cnt = n; |
| 3269 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | one_lun = (struct scsi_lun *) &arr[8]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3271 | max_addr = arr + SDEBUG_RLUN_ARR_SZ; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3272 | for (i = 0, lun = (sdebug_no_lun_0 ? 1 : 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3273 | ((i < lun_cnt) && ((unsigned char *)(one_lun + i) < max_addr)); |
| 3274 | i++, lun++) { |
| 3275 | upper = (lun >> 8) & 0x3f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | if (upper) |
| 3277 | one_lun[i].scsi_lun[0] = |
| 3278 | (upper | (SAM2_LUN_ADDRESS_METHOD << 6)); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3279 | one_lun[i].scsi_lun[1] = lun & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3280 | } |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 3281 | if (want_wlun) { |
Tomas Winkler | 34d5543 | 2015-07-28 16:54:21 +0300 | [diff] [blame] | 3282 | one_lun[i].scsi_lun[0] = (SCSI_W_LUN_REPORT_LUNS >> 8) & 0xff; |
| 3283 | one_lun[i].scsi_lun[1] = SCSI_W_LUN_REPORT_LUNS & 0xff; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3284 | i++; |
| 3285 | } |
| 3286 | alloc_len = (unsigned char *)(one_lun + i) - arr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | return fill_from_dev_buffer(scp, arr, |
| 3288 | min((int)alloc_len, SDEBUG_RLUN_ARR_SZ)); |
| 3289 | } |
| 3290 | |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3291 | static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba, |
| 3292 | unsigned int num, struct sdebug_dev_info *devip) |
| 3293 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3294 | int j; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3295 | unsigned char *kaddr, *buf; |
| 3296 | unsigned int offset; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3297 | struct scsi_data_buffer *sdb = scsi_in(scp); |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3298 | struct sg_mapping_iter miter; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3299 | |
| 3300 | /* better not to use temporary buffer. */ |
| 3301 | buf = kmalloc(scsi_bufflen(scp), GFP_ATOMIC); |
Akinobu Mita | c5af0db | 2014-02-26 22:57:01 +0900 | [diff] [blame] | 3302 | if (!buf) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 3303 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3304 | INSUFF_RES_ASCQ); |
Akinobu Mita | c5af0db | 2014-02-26 22:57:01 +0900 | [diff] [blame] | 3305 | return check_condition_result; |
| 3306 | } |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3307 | |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 3308 | scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp)); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3309 | |
| 3310 | offset = 0; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3311 | sg_miter_start(&miter, sdb->table.sgl, sdb->table.nents, |
| 3312 | SG_MITER_ATOMIC | SG_MITER_TO_SG); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3313 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3314 | while (sg_miter_next(&miter)) { |
| 3315 | kaddr = miter.addr; |
| 3316 | for (j = 0; j < miter.length; j++) |
| 3317 | *(kaddr + j) ^= *(buf + offset + j); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3318 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3319 | offset += miter.length; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3320 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3321 | sg_miter_stop(&miter); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3322 | kfree(buf); |
| 3323 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3324 | return 0; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3325 | } |
| 3326 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3327 | static int |
| 3328 | resp_xdwriteread_10(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
| 3329 | { |
| 3330 | u8 *cmd = scp->cmnd; |
| 3331 | u64 lba; |
| 3332 | u32 num; |
| 3333 | int errsts; |
| 3334 | |
| 3335 | if (!scsi_bidi_cmnd(scp)) { |
| 3336 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3337 | INSUFF_RES_ASCQ); |
| 3338 | return check_condition_result; |
| 3339 | } |
| 3340 | errsts = resp_read_dt0(scp, devip); |
| 3341 | if (errsts) |
| 3342 | return errsts; |
| 3343 | if (!(cmd[1] & 0x4)) { /* DISABLE_WRITE is not set */ |
| 3344 | errsts = resp_write_dt0(scp, devip); |
| 3345 | if (errsts) |
| 3346 | return errsts; |
| 3347 | } |
| 3348 | lba = get_unaligned_be32(cmd + 2); |
| 3349 | num = get_unaligned_be16(cmd + 7); |
| 3350 | return resp_xdwriteread(scp, lba, num, devip); |
| 3351 | } |
| 3352 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3353 | /* When timer or tasklet goes off this function is called. */ |
| 3354 | static void sdebug_q_cmd_complete(unsigned long indx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3355 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3356 | int qa_indx; |
| 3357 | int retiring = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3358 | unsigned long iflags; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3359 | struct sdebug_queued_cmd *sqcp; |
| 3360 | struct scsi_cmnd *scp; |
| 3361 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3363 | atomic_inc(&sdebug_completions); |
| 3364 | qa_indx = indx; |
| 3365 | if ((qa_indx < 0) || (qa_indx >= SCSI_DEBUG_CANQUEUE)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3366 | pr_err("wild qa_indx=%d\n", qa_indx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3367 | return; |
| 3368 | } |
| 3369 | spin_lock_irqsave(&queued_arr_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3370 | sqcp = &queued_arr[qa_indx]; |
| 3371 | scp = sqcp->a_cmnd; |
| 3372 | if (NULL == scp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3374 | pr_err("scp is NULL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | return; |
| 3376 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3377 | devip = (struct sdebug_dev_info *)scp->device->hostdata; |
| 3378 | if (devip) |
| 3379 | atomic_dec(&devip->num_in_q); |
| 3380 | else |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3381 | pr_err("devip=NULL\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3382 | if (atomic_read(&retired_max_queue) > 0) |
| 3383 | retiring = 1; |
| 3384 | |
| 3385 | sqcp->a_cmnd = NULL; |
| 3386 | if (!test_and_clear_bit(qa_indx, queued_in_use_bm)) { |
| 3387 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3388 | pr_err("Unexpected completion\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3389 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3391 | |
| 3392 | if (unlikely(retiring)) { /* user has reduced max_queue */ |
| 3393 | int k, retval; |
| 3394 | |
| 3395 | retval = atomic_read(&retired_max_queue); |
| 3396 | if (qa_indx >= retval) { |
| 3397 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3398 | pr_err("index %d too large\n", retval); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3399 | return; |
| 3400 | } |
| 3401 | k = find_last_bit(queued_in_use_bm, retval); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3402 | if ((k < sdebug_max_queue) || (k == retval)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3403 | atomic_set(&retired_max_queue, 0); |
| 3404 | else |
| 3405 | atomic_set(&retired_max_queue, k + 1); |
| 3406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3408 | scp->scsi_done(scp); /* callback to mid level */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | } |
| 3410 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3411 | /* When high resolution timer goes off this function is called. */ |
| 3412 | static enum hrtimer_restart |
| 3413 | sdebug_q_cmd_hrt_complete(struct hrtimer *timer) |
| 3414 | { |
| 3415 | int qa_indx; |
| 3416 | int retiring = 0; |
| 3417 | unsigned long iflags; |
| 3418 | struct sdebug_hrtimer *sd_hrtp = (struct sdebug_hrtimer *)timer; |
| 3419 | struct sdebug_queued_cmd *sqcp; |
| 3420 | struct scsi_cmnd *scp; |
| 3421 | struct sdebug_dev_info *devip; |
| 3422 | |
| 3423 | atomic_inc(&sdebug_completions); |
| 3424 | qa_indx = sd_hrtp->qa_indx; |
| 3425 | if ((qa_indx < 0) || (qa_indx >= SCSI_DEBUG_CANQUEUE)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3426 | pr_err("wild qa_indx=%d\n", qa_indx); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3427 | goto the_end; |
| 3428 | } |
| 3429 | spin_lock_irqsave(&queued_arr_lock, iflags); |
| 3430 | sqcp = &queued_arr[qa_indx]; |
| 3431 | scp = sqcp->a_cmnd; |
| 3432 | if (NULL == scp) { |
| 3433 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3434 | pr_err("scp is NULL\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3435 | goto the_end; |
| 3436 | } |
| 3437 | devip = (struct sdebug_dev_info *)scp->device->hostdata; |
| 3438 | if (devip) |
| 3439 | atomic_dec(&devip->num_in_q); |
| 3440 | else |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3441 | pr_err("devip=NULL\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3442 | if (atomic_read(&retired_max_queue) > 0) |
| 3443 | retiring = 1; |
| 3444 | |
| 3445 | sqcp->a_cmnd = NULL; |
| 3446 | if (!test_and_clear_bit(qa_indx, queued_in_use_bm)) { |
| 3447 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3448 | pr_err("Unexpected completion\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3449 | goto the_end; |
| 3450 | } |
| 3451 | |
| 3452 | if (unlikely(retiring)) { /* user has reduced max_queue */ |
| 3453 | int k, retval; |
| 3454 | |
| 3455 | retval = atomic_read(&retired_max_queue); |
| 3456 | if (qa_indx >= retval) { |
| 3457 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3458 | pr_err("index %d too large\n", retval); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3459 | goto the_end; |
| 3460 | } |
| 3461 | k = find_last_bit(queued_in_use_bm, retval); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3462 | if ((k < sdebug_max_queue) || (k == retval)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3463 | atomic_set(&retired_max_queue, 0); |
| 3464 | else |
| 3465 | atomic_set(&retired_max_queue, k + 1); |
| 3466 | } |
| 3467 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
| 3468 | scp->scsi_done(scp); /* callback to mid level */ |
| 3469 | the_end: |
| 3470 | return HRTIMER_NORESTART; |
| 3471 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3473 | static struct sdebug_dev_info * |
| 3474 | sdebug_device_create(struct sdebug_host_info *sdbg_host, gfp_t flags) |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3475 | { |
| 3476 | struct sdebug_dev_info *devip; |
| 3477 | |
| 3478 | devip = kzalloc(sizeof(*devip), flags); |
| 3479 | if (devip) { |
| 3480 | devip->sdbg_host = sdbg_host; |
| 3481 | list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list); |
| 3482 | } |
| 3483 | return devip; |
| 3484 | } |
| 3485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev) |
| 3487 | { |
| 3488 | struct sdebug_host_info * sdbg_host; |
| 3489 | struct sdebug_dev_info * open_devip = NULL; |
| 3490 | struct sdebug_dev_info * devip = |
| 3491 | (struct sdebug_dev_info *)sdev->hostdata; |
| 3492 | |
| 3493 | if (devip) |
| 3494 | return devip; |
FUJITA Tomonori | d1e4c9c | 2008-03-02 18:30:18 +0900 | [diff] [blame] | 3495 | sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host); |
| 3496 | if (!sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3497 | pr_err("Host info NULL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | return NULL; |
| 3499 | } |
| 3500 | list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) { |
| 3501 | if ((devip->used) && (devip->channel == sdev->channel) && |
| 3502 | (devip->target == sdev->id) && |
| 3503 | (devip->lun == sdev->lun)) |
| 3504 | return devip; |
| 3505 | else { |
| 3506 | if ((!devip->used) && (!open_devip)) |
| 3507 | open_devip = devip; |
| 3508 | } |
| 3509 | } |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3510 | if (!open_devip) { /* try and make a new one */ |
| 3511 | open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC); |
| 3512 | if (!open_devip) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3513 | pr_err("out of memory at line %d\n", __LINE__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | return NULL; |
| 3515 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | } |
FUJITA Tomonori | a75869d | 2008-03-20 11:09:17 +0900 | [diff] [blame] | 3517 | |
| 3518 | open_devip->channel = sdev->channel; |
| 3519 | open_devip->target = sdev->id; |
| 3520 | open_devip->lun = sdev->lun; |
| 3521 | open_devip->sdbg_host = sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3522 | atomic_set(&open_devip->num_in_q, 0); |
| 3523 | set_bit(SDEBUG_UA_POR, open_devip->uas_bm); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3524 | open_devip->used = true; |
FUJITA Tomonori | a75869d | 2008-03-20 11:09:17 +0900 | [diff] [blame] | 3525 | return open_devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | } |
| 3527 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3528 | static int scsi_debug_slave_alloc(struct scsi_device *sdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3530 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3531 | pr_info("slave_alloc <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3532 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 3533 | queue_flag_set_unlocked(QUEUE_FLAG_BIDI, sdp->request_queue); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3534 | return 0; |
| 3535 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3536 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3537 | static int scsi_debug_slave_configure(struct scsi_device *sdp) |
| 3538 | { |
| 3539 | struct sdebug_dev_info *devip; |
FUJITA Tomonori | a34c4e9 | 2008-03-25 09:26:50 +0900 | [diff] [blame] | 3540 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3541 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3542 | pr_info("slave_configure <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3543 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
| 3544 | if (sdp->host->max_cmd_len != SCSI_DEBUG_MAX_CMD_LEN) |
| 3545 | sdp->host->max_cmd_len = SCSI_DEBUG_MAX_CMD_LEN; |
| 3546 | devip = devInfoReg(sdp); |
| 3547 | if (NULL == devip) |
| 3548 | return 1; /* no resources, will be marked offline */ |
Christoph Hellwig | c8b09f6 | 2014-11-03 20:15:14 +0100 | [diff] [blame] | 3549 | sdp->hostdata = devip; |
Akinobu Mita | 6bb5e6e | 2014-06-02 22:56:49 +0900 | [diff] [blame] | 3550 | blk_queue_max_segment_size(sdp->request_queue, -1U); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3551 | if (sdebug_no_uld) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 3552 | sdp->no_uld_attach = 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3553 | return 0; |
| 3554 | } |
| 3555 | |
| 3556 | static void scsi_debug_slave_destroy(struct scsi_device *sdp) |
| 3557 | { |
| 3558 | struct sdebug_dev_info *devip = |
| 3559 | (struct sdebug_dev_info *)sdp->hostdata; |
| 3560 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3561 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3562 | pr_info("slave_destroy <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3563 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
| 3564 | if (devip) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3565 | /* make this slot available for re-use */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3566 | devip->used = false; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3567 | sdp->hostdata = NULL; |
| 3568 | } |
| 3569 | } |
| 3570 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3571 | /* Returns 1 if cmnd found (deletes its timer or tasklet), else returns 0 */ |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3572 | static int stop_queued_cmnd(struct scsi_cmnd *cmnd) |
| 3573 | { |
| 3574 | unsigned long iflags; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3575 | int k, qmax, r_qmax; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3576 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3577 | struct sdebug_dev_info *devip; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3578 | |
| 3579 | spin_lock_irqsave(&queued_arr_lock, iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3580 | qmax = sdebug_max_queue; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3581 | r_qmax = atomic_read(&retired_max_queue); |
| 3582 | if (r_qmax > qmax) |
| 3583 | qmax = r_qmax; |
| 3584 | for (k = 0; k < qmax; ++k) { |
| 3585 | if (test_bit(k, queued_in_use_bm)) { |
| 3586 | sqcp = &queued_arr[k]; |
| 3587 | if (cmnd == sqcp->a_cmnd) { |
Douglas Gilbert | db525fc | 2014-08-31 19:09:59 -0400 | [diff] [blame] | 3588 | devip = (struct sdebug_dev_info *) |
| 3589 | cmnd->device->hostdata; |
| 3590 | if (devip) |
| 3591 | atomic_dec(&devip->num_in_q); |
| 3592 | sqcp->a_cmnd = NULL; |
| 3593 | spin_unlock_irqrestore(&queued_arr_lock, |
| 3594 | iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3595 | if (sdebug_ndelay > 0) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3596 | if (sqcp->sd_hrtp) |
| 3597 | hrtimer_cancel( |
| 3598 | &sqcp->sd_hrtp->hrt); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3599 | } else if (sdebug_delay > 0) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3600 | if (sqcp->cmnd_timerp) |
| 3601 | del_timer_sync( |
| 3602 | sqcp->cmnd_timerp); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3603 | } else if (sdebug_delay < 0) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3604 | if (sqcp->tletp) |
| 3605 | tasklet_kill(sqcp->tletp); |
| 3606 | } |
Douglas Gilbert | db525fc | 2014-08-31 19:09:59 -0400 | [diff] [blame] | 3607 | clear_bit(k, queued_in_use_bm); |
| 3608 | return 1; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3609 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3610 | } |
| 3611 | } |
| 3612 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Douglas Gilbert | db525fc | 2014-08-31 19:09:59 -0400 | [diff] [blame] | 3613 | return 0; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3614 | } |
| 3615 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3616 | /* Deletes (stops) timers or tasklets of all queued commands */ |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3617 | static void stop_all_queued(void) |
| 3618 | { |
| 3619 | unsigned long iflags; |
| 3620 | int k; |
| 3621 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3622 | struct sdebug_dev_info *devip; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3623 | |
| 3624 | spin_lock_irqsave(&queued_arr_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3625 | for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) { |
| 3626 | if (test_bit(k, queued_in_use_bm)) { |
| 3627 | sqcp = &queued_arr[k]; |
| 3628 | if (sqcp->a_cmnd) { |
Douglas Gilbert | db525fc | 2014-08-31 19:09:59 -0400 | [diff] [blame] | 3629 | devip = (struct sdebug_dev_info *) |
| 3630 | sqcp->a_cmnd->device->hostdata; |
| 3631 | if (devip) |
| 3632 | atomic_dec(&devip->num_in_q); |
| 3633 | sqcp->a_cmnd = NULL; |
| 3634 | spin_unlock_irqrestore(&queued_arr_lock, |
| 3635 | iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3636 | if (sdebug_ndelay > 0) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3637 | if (sqcp->sd_hrtp) |
| 3638 | hrtimer_cancel( |
| 3639 | &sqcp->sd_hrtp->hrt); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3640 | } else if (sdebug_delay > 0) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3641 | if (sqcp->cmnd_timerp) |
| 3642 | del_timer_sync( |
| 3643 | sqcp->cmnd_timerp); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3644 | } else if (sdebug_delay < 0) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3645 | if (sqcp->tletp) |
| 3646 | tasklet_kill(sqcp->tletp); |
| 3647 | } |
Douglas Gilbert | db525fc | 2014-08-31 19:09:59 -0400 | [diff] [blame] | 3648 | clear_bit(k, queued_in_use_bm); |
| 3649 | spin_lock_irqsave(&queued_arr_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3650 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3651 | } |
| 3652 | } |
| 3653 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | } |
| 3655 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3656 | /* Free queued command memory on heap */ |
| 3657 | static void free_all_queued(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3658 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3659 | unsigned long iflags; |
| 3660 | int k; |
| 3661 | struct sdebug_queued_cmd *sqcp; |
| 3662 | |
| 3663 | spin_lock_irqsave(&queued_arr_lock, iflags); |
| 3664 | for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) { |
| 3665 | sqcp = &queued_arr[k]; |
| 3666 | kfree(sqcp->cmnd_timerp); |
| 3667 | sqcp->cmnd_timerp = NULL; |
| 3668 | kfree(sqcp->tletp); |
| 3669 | sqcp->tletp = NULL; |
| 3670 | kfree(sqcp->sd_hrtp); |
| 3671 | sqcp->sd_hrtp = NULL; |
| 3672 | } |
| 3673 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3674 | } |
| 3675 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3676 | static int scsi_debug_abort(struct scsi_cmnd *SCpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3678 | ++num_aborts; |
| 3679 | if (SCpnt) { |
| 3680 | if (SCpnt->device && |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3681 | (SDEBUG_OPT_ALL_NOISE & sdebug_opts)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3682 | sdev_printk(KERN_INFO, SCpnt->device, "%s\n", |
| 3683 | __func__); |
| 3684 | stop_queued_cmnd(SCpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3686 | return SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt) |
| 3690 | { |
| 3691 | struct sdebug_dev_info * devip; |
| 3692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | ++num_dev_resets; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3694 | if (SCpnt && SCpnt->device) { |
| 3695 | struct scsi_device *sdp = SCpnt->device; |
| 3696 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3697 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3698 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
| 3699 | devip = devInfoReg(sdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3700 | if (devip) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3701 | set_bit(SDEBUG_UA_POR, devip->uas_bm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | } |
| 3703 | return SUCCESS; |
| 3704 | } |
| 3705 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3706 | static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt) |
| 3707 | { |
| 3708 | struct sdebug_host_info *sdbg_host; |
| 3709 | struct sdebug_dev_info *devip; |
| 3710 | struct scsi_device *sdp; |
| 3711 | struct Scsi_Host *hp; |
| 3712 | int k = 0; |
| 3713 | |
| 3714 | ++num_target_resets; |
| 3715 | if (!SCpnt) |
| 3716 | goto lie; |
| 3717 | sdp = SCpnt->device; |
| 3718 | if (!sdp) |
| 3719 | goto lie; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3720 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3721 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
| 3722 | hp = sdp->host; |
| 3723 | if (!hp) |
| 3724 | goto lie; |
| 3725 | sdbg_host = *(struct sdebug_host_info **)shost_priv(hp); |
| 3726 | if (sdbg_host) { |
| 3727 | list_for_each_entry(devip, |
| 3728 | &sdbg_host->dev_info_list, |
| 3729 | dev_list) |
| 3730 | if (devip->target == sdp->id) { |
| 3731 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3732 | ++k; |
| 3733 | } |
| 3734 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3735 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3736 | sdev_printk(KERN_INFO, sdp, |
| 3737 | "%s: %d device(s) found in target\n", __func__, k); |
| 3738 | lie: |
| 3739 | return SUCCESS; |
| 3740 | } |
| 3741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3742 | static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt) |
| 3743 | { |
| 3744 | struct sdebug_host_info *sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3745 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3746 | struct scsi_device * sdp; |
| 3747 | struct Scsi_Host * hp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3748 | int k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | ++num_bus_resets; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3751 | if (!(SCpnt && SCpnt->device)) |
| 3752 | goto lie; |
| 3753 | sdp = SCpnt->device; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3754 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3755 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
| 3756 | hp = sdp->host; |
| 3757 | if (hp) { |
FUJITA Tomonori | d1e4c9c | 2008-03-02 18:30:18 +0900 | [diff] [blame] | 3758 | sdbg_host = *(struct sdebug_host_info **)shost_priv(hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | if (sdbg_host) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3760 | list_for_each_entry(devip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | &sdbg_host->dev_info_list, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3762 | dev_list) { |
| 3763 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3764 | ++k; |
| 3765 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3766 | } |
| 3767 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3768 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3769 | sdev_printk(KERN_INFO, sdp, |
| 3770 | "%s: %d device(s) found in host\n", __func__, k); |
| 3771 | lie: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | return SUCCESS; |
| 3773 | } |
| 3774 | |
| 3775 | static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt) |
| 3776 | { |
| 3777 | struct sdebug_host_info * sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3778 | struct sdebug_dev_info *devip; |
| 3779 | int k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3781 | ++num_host_resets; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3782 | if ((SCpnt->device) && (SDEBUG_OPT_ALL_NOISE & sdebug_opts)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3783 | sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | spin_lock(&sdebug_host_list_lock); |
| 3785 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3786 | list_for_each_entry(devip, &sdbg_host->dev_info_list, |
| 3787 | dev_list) { |
| 3788 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3789 | ++k; |
| 3790 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3791 | } |
| 3792 | spin_unlock(&sdebug_host_list_lock); |
| 3793 | stop_all_queued(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3794 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3795 | sdev_printk(KERN_INFO, SCpnt->device, |
| 3796 | "%s: %d device(s) found\n", __func__, k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3797 | return SUCCESS; |
| 3798 | } |
| 3799 | |
FUJITA Tomonori | f58b0ef | 2008-03-30 00:59:54 +0900 | [diff] [blame] | 3800 | static void __init sdebug_build_parts(unsigned char *ramp, |
FUJITA Tomonori | 5f2578e | 2008-03-30 00:59:57 +0900 | [diff] [blame] | 3801 | unsigned long store_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3802 | { |
| 3803 | struct partition * pp; |
| 3804 | int starts[SDEBUG_MAX_PARTS + 2]; |
| 3805 | int sectors_per_part, num_sectors, k; |
| 3806 | int heads_by_sects, start_sec, end_sec; |
| 3807 | |
| 3808 | /* assume partition table already zeroed */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3809 | if ((sdebug_num_parts < 1) || (store_size < 1048576)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3810 | return; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3811 | if (sdebug_num_parts > SDEBUG_MAX_PARTS) { |
| 3812 | sdebug_num_parts = SDEBUG_MAX_PARTS; |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3813 | pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3815 | num_sectors = (int)sdebug_store_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3816 | sectors_per_part = (num_sectors - sdebug_sectors_per) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3817 | / sdebug_num_parts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3818 | heads_by_sects = sdebug_heads * sdebug_sectors_per; |
| 3819 | starts[0] = sdebug_sectors_per; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3820 | for (k = 1; k < sdebug_num_parts; ++k) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | starts[k] = ((k * sectors_per_part) / heads_by_sects) |
| 3822 | * heads_by_sects; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3823 | starts[sdebug_num_parts] = num_sectors; |
| 3824 | starts[sdebug_num_parts + 1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | |
| 3826 | ramp[510] = 0x55; /* magic partition markings */ |
| 3827 | ramp[511] = 0xAA; |
| 3828 | pp = (struct partition *)(ramp + 0x1be); |
| 3829 | for (k = 0; starts[k + 1]; ++k, ++pp) { |
| 3830 | start_sec = starts[k]; |
| 3831 | end_sec = starts[k + 1] - 1; |
| 3832 | pp->boot_ind = 0; |
| 3833 | |
| 3834 | pp->cyl = start_sec / heads_by_sects; |
| 3835 | pp->head = (start_sec - (pp->cyl * heads_by_sects)) |
| 3836 | / sdebug_sectors_per; |
| 3837 | pp->sector = (start_sec % sdebug_sectors_per) + 1; |
| 3838 | |
| 3839 | pp->end_cyl = end_sec / heads_by_sects; |
| 3840 | pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects)) |
| 3841 | / sdebug_sectors_per; |
| 3842 | pp->end_sector = (end_sec % sdebug_sectors_per) + 1; |
| 3843 | |
Akinobu Mita | 150c354 | 2013-08-26 22:08:40 +0900 | [diff] [blame] | 3844 | pp->start_sect = cpu_to_le32(start_sec); |
| 3845 | pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3846 | pp->sys_ind = 0x83; /* plain Linux partition */ |
| 3847 | } |
| 3848 | } |
| 3849 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3850 | static int |
| 3851 | schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip, |
| 3852 | int scsi_result, int delta_jiff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3854 | unsigned long iflags; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3855 | int k, num_in_q, qdepth, inject; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3856 | struct sdebug_queued_cmd *sqcp = NULL; |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 3857 | struct scsi_device *sdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 3859 | /* this should never happen */ |
| 3860 | if (WARN_ON(!cmnd)) |
| 3861 | return SCSI_MLQUEUE_HOST_BUSY; |
| 3862 | |
| 3863 | if (NULL == devip) { |
| 3864 | pr_warn("called devip == NULL\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3865 | /* no particularly good error to report back */ |
| 3866 | return SCSI_MLQUEUE_HOST_BUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3867 | } |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 3868 | |
| 3869 | sdp = cmnd->device; |
| 3870 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3871 | if (sdebug_verbose && scsi_result) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3872 | sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n", |
| 3873 | __func__, scsi_result); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3874 | if (delta_jiff == 0) |
| 3875 | goto respond_in_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3876 | |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3877 | /* schedule the response at a later time if resources permit */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3878 | spin_lock_irqsave(&queued_arr_lock, iflags); |
| 3879 | num_in_q = atomic_read(&devip->num_in_q); |
| 3880 | qdepth = cmnd->device->queue_depth; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3881 | inject = 0; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3882 | if ((qdepth > 0) && (num_in_q >= qdepth)) { |
| 3883 | if (scsi_result) { |
| 3884 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
| 3885 | goto respond_in_thread; |
| 3886 | } else |
| 3887 | scsi_result = device_qfull_result; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3888 | } else if ((sdebug_every_nth != 0) && |
| 3889 | (SDEBUG_OPT_RARE_TSF & sdebug_opts) && |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3890 | (scsi_result == 0)) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3891 | if ((num_in_q == (qdepth - 1)) && |
| 3892 | (atomic_inc_return(&sdebug_a_tsf) >= |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3893 | abs(sdebug_every_nth))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3894 | atomic_set(&sdebug_a_tsf, 0); |
| 3895 | inject = 1; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3896 | scsi_result = device_qfull_result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3897 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3898 | } |
| 3899 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3900 | k = find_first_zero_bit(queued_in_use_bm, sdebug_max_queue); |
| 3901 | if (k >= sdebug_max_queue) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3902 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3903 | if (scsi_result) |
| 3904 | goto respond_in_thread; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3905 | else if (SDEBUG_OPT_ALL_TSF & sdebug_opts) |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3906 | scsi_result = device_qfull_result; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3907 | if (SDEBUG_OPT_Q_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3908 | sdev_printk(KERN_INFO, sdp, |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3909 | "%s: max_queue=%d exceeded, %s\n", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3910 | __func__, sdebug_max_queue, |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3911 | (scsi_result ? "status: TASK SET FULL" : |
| 3912 | "report: host busy")); |
| 3913 | if (scsi_result) |
| 3914 | goto respond_in_thread; |
| 3915 | else |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3916 | return SCSI_MLQUEUE_HOST_BUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3917 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3918 | __set_bit(k, queued_in_use_bm); |
| 3919 | atomic_inc(&devip->num_in_q); |
| 3920 | sqcp = &queued_arr[k]; |
| 3921 | sqcp->a_cmnd = cmnd; |
| 3922 | cmnd->result = scsi_result; |
| 3923 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
| 3924 | if (delta_jiff > 0) { |
| 3925 | if (NULL == sqcp->cmnd_timerp) { |
| 3926 | sqcp->cmnd_timerp = kmalloc(sizeof(struct timer_list), |
| 3927 | GFP_ATOMIC); |
| 3928 | if (NULL == sqcp->cmnd_timerp) |
| 3929 | return SCSI_MLQUEUE_HOST_BUSY; |
| 3930 | init_timer(sqcp->cmnd_timerp); |
| 3931 | } |
| 3932 | sqcp->cmnd_timerp->function = sdebug_q_cmd_complete; |
| 3933 | sqcp->cmnd_timerp->data = k; |
| 3934 | sqcp->cmnd_timerp->expires = get_jiffies_64() + delta_jiff; |
| 3935 | add_timer(sqcp->cmnd_timerp); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3936 | } else if (sdebug_ndelay > 0) { |
| 3937 | ktime_t kt = ktime_set(0, sdebug_ndelay); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3938 | struct sdebug_hrtimer *sd_hp = sqcp->sd_hrtp; |
| 3939 | |
| 3940 | if (NULL == sd_hp) { |
| 3941 | sd_hp = kmalloc(sizeof(*sd_hp), GFP_ATOMIC); |
| 3942 | if (NULL == sd_hp) |
| 3943 | return SCSI_MLQUEUE_HOST_BUSY; |
| 3944 | sqcp->sd_hrtp = sd_hp; |
| 3945 | hrtimer_init(&sd_hp->hrt, CLOCK_MONOTONIC, |
| 3946 | HRTIMER_MODE_REL); |
| 3947 | sd_hp->hrt.function = sdebug_q_cmd_hrt_complete; |
| 3948 | sd_hp->qa_indx = k; |
| 3949 | } |
| 3950 | hrtimer_start(&sd_hp->hrt, kt, HRTIMER_MODE_REL); |
| 3951 | } else { /* delay < 0 */ |
| 3952 | if (NULL == sqcp->tletp) { |
| 3953 | sqcp->tletp = kmalloc(sizeof(*sqcp->tletp), |
| 3954 | GFP_ATOMIC); |
| 3955 | if (NULL == sqcp->tletp) |
| 3956 | return SCSI_MLQUEUE_HOST_BUSY; |
| 3957 | tasklet_init(sqcp->tletp, |
| 3958 | sdebug_q_cmd_complete, k); |
| 3959 | } |
| 3960 | if (-1 == delta_jiff) |
| 3961 | tasklet_hi_schedule(sqcp->tletp); |
| 3962 | else |
| 3963 | tasklet_schedule(sqcp->tletp); |
| 3964 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3965 | if ((SDEBUG_OPT_Q_NOISE & sdebug_opts) && |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3966 | (scsi_result == device_qfull_result)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3967 | sdev_printk(KERN_INFO, sdp, |
| 3968 | "%s: num_in_q=%d +1, %s%s\n", __func__, |
| 3969 | num_in_q, (inject ? "<inject> " : ""), |
| 3970 | "status: TASK SET FULL"); |
| 3971 | return 0; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3972 | |
| 3973 | respond_in_thread: /* call back to mid-layer using invocation thread */ |
| 3974 | cmnd->result = scsi_result; |
| 3975 | cmnd->scsi_done(cmnd); |
| 3976 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3977 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3978 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 3979 | /* Note: The following macros create attribute files in the |
| 3980 | /sys/module/scsi_debug/parameters directory. Unfortunately this |
| 3981 | driver is unaware of a change and cannot trigger auxiliary actions |
| 3982 | as it can when the corresponding attribute in the |
| 3983 | /sys/bus/pseudo/drivers/scsi_debug directory is changed. |
| 3984 | */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3985 | module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR); |
| 3986 | module_param_named(ato, sdebug_ato, int, S_IRUGO); |
| 3987 | module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR); |
| 3988 | module_param_named(delay, sdebug_delay, int, S_IRUGO | S_IWUSR); |
| 3989 | module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO); |
| 3990 | module_param_named(dif, sdebug_dif, int, S_IRUGO); |
| 3991 | module_param_named(dix, sdebug_dix, int, S_IRUGO); |
| 3992 | module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR); |
| 3993 | module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR); |
| 3994 | module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR); |
| 3995 | module_param_named(guard, sdebug_guard, uint, S_IRUGO); |
| 3996 | module_param_named(host_lock, sdebug_host_lock, bool, S_IRUGO | S_IWUSR); |
| 3997 | module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO); |
| 3998 | module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO); |
| 3999 | module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO); |
| 4000 | module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO); |
| 4001 | module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO); |
| 4002 | module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR); |
| 4003 | module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR); |
| 4004 | module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR); |
| 4005 | module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR); |
| 4006 | module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO); |
| 4007 | module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO); |
| 4008 | module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR); |
| 4009 | module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO); |
| 4010 | module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR); |
| 4011 | module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO); |
| 4012 | module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR); |
| 4013 | module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR); |
| 4014 | module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO); |
| 4015 | module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO); |
| 4016 | module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR); |
| 4017 | module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO); |
| 4018 | module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO); |
| 4019 | module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO); |
| 4020 | module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO); |
| 4021 | module_param_named(virtual_gb, sdebug_virtual_gb, int, S_IRUGO | S_IWUSR); |
| 4022 | module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int, |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4023 | S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4024 | module_param_named(write_same_length, sdebug_write_same_length, int, |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4025 | S_IRUGO | S_IWUSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4026 | |
| 4027 | MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert"); |
| 4028 | MODULE_DESCRIPTION("SCSI debug adapter driver"); |
| 4029 | MODULE_LICENSE("GPL"); |
| 4030 | MODULE_VERSION(SCSI_DEBUG_VERSION); |
| 4031 | |
| 4032 | MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4033 | 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] | 4034 | MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4035 | 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] | 4036 | 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] | 4037 | MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)"); |
| 4038 | MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4039 | MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)"); |
Randy Dunlap | beb87c3 | 2007-06-11 11:36:40 -0700 | [diff] [blame] | 4040 | MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)"); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4041 | 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] | 4042 | MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)"); |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame^] | 4043 | MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4044 | MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)"); |
| 4045 | MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)"); |
| 4046 | MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)"); |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 4047 | MODULE_PARM_DESC(lbprz, "unmapped blocks return 0 on read (def=1)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4048 | MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4049 | 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] | 4050 | MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))"); |
| 4051 | MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4052 | 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] | 4053 | 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] | 4054 | MODULE_PARM_DESC(num_parts, "number of partitions(def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4055 | 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] | 4056 | MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)"); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 4057 | 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] | 4058 | MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])"); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4060 | MODULE_PARM_DESC(removable, "claim to have removable media (def=0)"); |
Douglas Gilbert | e46b034 | 2014-08-05 12:21:53 +0200 | [diff] [blame] | 4061 | MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=6[SPC-4])"); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 4062 | MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4063 | MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4064 | MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)"); |
| 4065 | 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] | 4066 | MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)"); |
| 4067 | 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] | 4068 | 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] | 4069 | MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)"); |
| 4070 | 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] | 4071 | |
| 4072 | static char sdebug_info[256]; |
| 4073 | |
| 4074 | static const char * scsi_debug_info(struct Scsi_Host * shp) |
| 4075 | { |
| 4076 | sprintf(sdebug_info, "scsi_debug, version %s [%s], " |
| 4077 | "dev_size_mb=%d, opts=0x%x", SCSI_DEBUG_VERSION, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4078 | sdebug_version_date, sdebug_dev_size_mb, sdebug_opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | return sdebug_info; |
| 4080 | } |
| 4081 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4082 | /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */ |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4083 | static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer, int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4084 | { |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4085 | char arr[16]; |
| 4086 | int opts; |
| 4087 | int minLen = length > 15 ? 15 : length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4088 | |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4089 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 4090 | return -EACCES; |
| 4091 | memcpy(arr, buffer, minLen); |
| 4092 | arr[minLen] = '\0'; |
| 4093 | if (1 != sscanf(arr, "%d", &opts)) |
| 4094 | return -EINVAL; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4095 | sdebug_opts = opts; |
| 4096 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); |
| 4097 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts); |
| 4098 | if (sdebug_every_nth != 0) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4099 | atomic_set(&sdebug_cmnd_count, 0); |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4100 | return length; |
| 4101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4102 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4103 | /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the |
| 4104 | * same for each scsi_debug host (if more than one). Some of the counters |
| 4105 | * output are not atomics so might be inaccurate in a busy system. */ |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4106 | static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host) |
| 4107 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4108 | int f, l; |
| 4109 | char b[32]; |
| 4110 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4111 | if (sdebug_every_nth > 0) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4112 | snprintf(b, sizeof(b), " (curr:%d)", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4113 | ((SDEBUG_OPT_RARE_TSF & sdebug_opts) ? |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4114 | atomic_read(&sdebug_a_tsf) : |
| 4115 | atomic_read(&sdebug_cmnd_count))); |
| 4116 | else |
| 4117 | b[0] = '\0'; |
| 4118 | |
| 4119 | seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n" |
| 4120 | "num_tgts=%d, shared (ram) size=%d MB, opts=0x%x, " |
| 4121 | "every_nth=%d%s\n" |
| 4122 | "delay=%d, ndelay=%d, max_luns=%d, q_completions=%d\n" |
| 4123 | "sector_size=%d bytes, cylinders=%d, heads=%d, sectors=%d\n" |
| 4124 | "command aborts=%d; RESETs: device=%d, target=%d, bus=%d, " |
| 4125 | "host=%d\ndix_reads=%d dix_writes=%d dif_errors=%d " |
| 4126 | "usec_in_jiffy=%lu\n", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4127 | SCSI_DEBUG_VERSION, sdebug_version_date, |
| 4128 | sdebug_num_tgts, sdebug_dev_size_mb, sdebug_opts, |
| 4129 | sdebug_every_nth, b, sdebug_delay, sdebug_ndelay, |
| 4130 | sdebug_max_luns, atomic_read(&sdebug_completions), |
| 4131 | sdebug_sector_size, sdebug_cylinders_per, sdebug_heads, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4132 | sdebug_sectors_per, num_aborts, num_dev_resets, |
| 4133 | num_target_resets, num_bus_resets, num_host_resets, |
| 4134 | dix_reads, dix_writes, dif_errors, TICK_NSEC / 1000); |
| 4135 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4136 | f = find_first_bit(queued_in_use_bm, sdebug_max_queue); |
| 4137 | if (f != sdebug_max_queue) { |
| 4138 | l = find_last_bit(queued_in_use_bm, sdebug_max_queue); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4139 | seq_printf(m, " %s BUSY: first,last bits set: %d,%d\n", |
| 4140 | "queued_in_use_bm", f, l); |
| 4141 | } |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4142 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4143 | } |
| 4144 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4145 | static ssize_t delay_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4147 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4148 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4149 | /* Returns -EBUSY if delay is being changed and commands are queued */ |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4150 | static ssize_t delay_store(struct device_driver *ddp, const char *buf, |
| 4151 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4153 | int delay, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4155 | if ((count > 0) && (1 == sscanf(buf, "%d", &delay))) { |
| 4156 | res = count; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4157 | if (sdebug_delay != delay) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4158 | unsigned long iflags; |
| 4159 | int k; |
| 4160 | |
| 4161 | spin_lock_irqsave(&queued_arr_lock, iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4162 | k = find_first_bit(queued_in_use_bm, sdebug_max_queue); |
| 4163 | if (k != sdebug_max_queue) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4164 | res = -EBUSY; /* have queued commands */ |
| 4165 | else { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4166 | sdebug_delay = delay; |
| 4167 | sdebug_ndelay = 0; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4168 | } |
| 4169 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4171 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | } |
| 4173 | return -EINVAL; |
| 4174 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4175 | static DRIVER_ATTR_RW(delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4176 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4177 | static ssize_t ndelay_show(struct device_driver *ddp, char *buf) |
| 4178 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4179 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4180 | } |
| 4181 | /* Returns -EBUSY if ndelay is being changed and commands are queued */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4182 | /* If > 0 and accepted then sdebug_delay is set to DELAY_OVERRIDDEN */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4183 | static ssize_t ndelay_store(struct device_driver *ddp, const char *buf, |
| 4184 | size_t count) |
| 4185 | { |
| 4186 | unsigned long iflags; |
| 4187 | int ndelay, res, k; |
| 4188 | |
| 4189 | if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) && |
| 4190 | (ndelay >= 0) && (ndelay < 1000000000)) { |
| 4191 | res = count; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4192 | if (sdebug_ndelay != ndelay) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4193 | spin_lock_irqsave(&queued_arr_lock, iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4194 | k = find_first_bit(queued_in_use_bm, sdebug_max_queue); |
| 4195 | if (k != sdebug_max_queue) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4196 | res = -EBUSY; /* have queued commands */ |
| 4197 | else { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4198 | sdebug_ndelay = ndelay; |
| 4199 | sdebug_delay = ndelay ? DELAY_OVERRIDDEN |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4200 | : DEF_DELAY; |
| 4201 | } |
| 4202 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
| 4203 | } |
| 4204 | return res; |
| 4205 | } |
| 4206 | return -EINVAL; |
| 4207 | } |
| 4208 | static DRIVER_ATTR_RW(ndelay); |
| 4209 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4210 | static ssize_t opts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4212 | return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | } |
| 4214 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4215 | static ssize_t opts_store(struct device_driver *ddp, const char *buf, |
| 4216 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | { |
| 4218 | int opts; |
| 4219 | char work[20]; |
| 4220 | |
| 4221 | if (1 == sscanf(buf, "%10s", work)) { |
Rasmus Villemoes | 48a9687 | 2014-10-13 15:54:44 -0700 | [diff] [blame] | 4222 | if (0 == strncasecmp(work,"0x", 2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | if (1 == sscanf(&work[2], "%x", &opts)) |
| 4224 | goto opts_done; |
| 4225 | } else { |
| 4226 | if (1 == sscanf(work, "%d", &opts)) |
| 4227 | goto opts_done; |
| 4228 | } |
| 4229 | } |
| 4230 | return -EINVAL; |
| 4231 | opts_done: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4232 | sdebug_opts = opts; |
| 4233 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); |
| 4234 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4235 | atomic_set(&sdebug_cmnd_count, 0); |
| 4236 | atomic_set(&sdebug_a_tsf, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | return count; |
| 4238 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4239 | static DRIVER_ATTR_RW(opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4241 | static ssize_t ptype_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4243 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4244 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4245 | static ssize_t ptype_store(struct device_driver *ddp, const char *buf, |
| 4246 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | { |
| 4248 | int n; |
| 4249 | |
| 4250 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4251 | sdebug_ptype = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | return count; |
| 4253 | } |
| 4254 | return -EINVAL; |
| 4255 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4256 | static DRIVER_ATTR_RW(ptype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4258 | static ssize_t dsense_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4260 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4262 | static ssize_t dsense_store(struct device_driver *ddp, const char *buf, |
| 4263 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | { |
| 4265 | int n; |
| 4266 | |
| 4267 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4268 | sdebug_dsense = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4269 | return count; |
| 4270 | } |
| 4271 | return -EINVAL; |
| 4272 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4273 | static DRIVER_ATTR_RW(dsense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4275 | static ssize_t fake_rw_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4276 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4277 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4278 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4279 | static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf, |
| 4280 | size_t count) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4281 | { |
| 4282 | int n; |
| 4283 | |
| 4284 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4285 | n = (n > 0); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4286 | sdebug_fake_rw = (sdebug_fake_rw > 0); |
| 4287 | if (sdebug_fake_rw != n) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4288 | if ((0 == n) && (NULL == fake_storep)) { |
| 4289 | unsigned long sz = |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4290 | (unsigned long)sdebug_dev_size_mb * |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4291 | 1048576; |
| 4292 | |
| 4293 | fake_storep = vmalloc(sz); |
| 4294 | if (NULL == fake_storep) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4295 | pr_err("out of memory, 9\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4296 | return -ENOMEM; |
| 4297 | } |
| 4298 | memset(fake_storep, 0, sz); |
| 4299 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4300 | sdebug_fake_rw = n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4301 | } |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4302 | return count; |
| 4303 | } |
| 4304 | return -EINVAL; |
| 4305 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4306 | static DRIVER_ATTR_RW(fake_rw); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4307 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4308 | 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] | 4309 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4310 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4311 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4312 | static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf, |
| 4313 | size_t count) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4314 | { |
| 4315 | int n; |
| 4316 | |
| 4317 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4318 | sdebug_no_lun_0 = n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4319 | return count; |
| 4320 | } |
| 4321 | return -EINVAL; |
| 4322 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4323 | static DRIVER_ATTR_RW(no_lun_0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4324 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4325 | static ssize_t num_tgts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4326 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4327 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4329 | static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf, |
| 4330 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | { |
| 4332 | int n; |
| 4333 | |
| 4334 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4335 | sdebug_num_tgts = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4336 | sdebug_max_tgts_luns(); |
| 4337 | return count; |
| 4338 | } |
| 4339 | return -EINVAL; |
| 4340 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4341 | static DRIVER_ATTR_RW(num_tgts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4343 | 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] | 4344 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4345 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4347 | static DRIVER_ATTR_RO(dev_size_mb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4349 | static ssize_t num_parts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4350 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4351 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4353 | static DRIVER_ATTR_RO(num_parts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4355 | static ssize_t every_nth_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4357 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4358 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4359 | static ssize_t every_nth_store(struct device_driver *ddp, const char *buf, |
| 4360 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 | { |
| 4362 | int nth; |
| 4363 | |
| 4364 | if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4365 | sdebug_every_nth = nth; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4366 | atomic_set(&sdebug_cmnd_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | return count; |
| 4368 | } |
| 4369 | return -EINVAL; |
| 4370 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4371 | static DRIVER_ATTR_RW(every_nth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4373 | static ssize_t max_luns_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4375 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns); |
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 max_luns_store(struct device_driver *ddp, const char *buf, |
| 4378 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | { |
| 4380 | int n; |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 4381 | bool changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4382 | |
| 4383 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4384 | changed = (sdebug_max_luns != n); |
| 4385 | sdebug_max_luns = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | sdebug_max_tgts_luns(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4387 | if (changed && (sdebug_scsi_level >= 5)) { /* >= SPC-3 */ |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 4388 | struct sdebug_host_info *sdhp; |
| 4389 | struct sdebug_dev_info *dp; |
| 4390 | |
| 4391 | spin_lock(&sdebug_host_list_lock); |
| 4392 | list_for_each_entry(sdhp, &sdebug_host_list, |
| 4393 | host_list) { |
| 4394 | list_for_each_entry(dp, &sdhp->dev_info_list, |
| 4395 | dev_list) { |
| 4396 | set_bit(SDEBUG_UA_LUNS_CHANGED, |
| 4397 | dp->uas_bm); |
| 4398 | } |
| 4399 | } |
| 4400 | spin_unlock(&sdebug_host_list_lock); |
| 4401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | return count; |
| 4403 | } |
| 4404 | return -EINVAL; |
| 4405 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4406 | static DRIVER_ATTR_RW(max_luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4408 | static ssize_t max_queue_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4409 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4410 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4411 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4412 | /* N.B. max_queue can be changed while there are queued commands. In flight |
| 4413 | * commands beyond the new max_queue will be completed. */ |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4414 | static ssize_t max_queue_store(struct device_driver *ddp, const char *buf, |
| 4415 | size_t count) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4416 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4417 | unsigned long iflags; |
| 4418 | int n, k; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4419 | |
| 4420 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) && |
| 4421 | (n <= SCSI_DEBUG_CANQUEUE)) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4422 | spin_lock_irqsave(&queued_arr_lock, iflags); |
| 4423 | k = find_last_bit(queued_in_use_bm, SCSI_DEBUG_CANQUEUE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4424 | sdebug_max_queue = n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4425 | if (SCSI_DEBUG_CANQUEUE == k) |
| 4426 | atomic_set(&retired_max_queue, 0); |
| 4427 | else if (k >= n) |
| 4428 | atomic_set(&retired_max_queue, k + 1); |
| 4429 | else |
| 4430 | atomic_set(&retired_max_queue, 0); |
| 4431 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4432 | return count; |
| 4433 | } |
| 4434 | return -EINVAL; |
| 4435 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4436 | static DRIVER_ATTR_RW(max_queue); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4437 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4438 | static ssize_t no_uld_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4439 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4440 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4441 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4442 | static DRIVER_ATTR_RO(no_uld); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4443 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4444 | static ssize_t scsi_level_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_scsi_level); |
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 DRIVER_ATTR_RO(scsi_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4450 | static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4451 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4452 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4453 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4454 | static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf, |
| 4455 | size_t count) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4456 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4457 | int n; |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4458 | bool changed; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4459 | |
| 4460 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4461 | changed = (sdebug_virtual_gb != n); |
| 4462 | sdebug_virtual_gb = n; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4463 | sdebug_capacity = get_sdebug_capacity(); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4464 | if (changed) { |
| 4465 | struct sdebug_host_info *sdhp; |
| 4466 | struct sdebug_dev_info *dp; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4467 | |
Ewan D. Milne | 4bc6b63 | 2014-12-04 11:49:26 -0500 | [diff] [blame] | 4468 | spin_lock(&sdebug_host_list_lock); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4469 | list_for_each_entry(sdhp, &sdebug_host_list, |
| 4470 | host_list) { |
| 4471 | list_for_each_entry(dp, &sdhp->dev_info_list, |
| 4472 | dev_list) { |
| 4473 | set_bit(SDEBUG_UA_CAPACITY_CHANGED, |
| 4474 | dp->uas_bm); |
| 4475 | } |
| 4476 | } |
Ewan D. Milne | 4bc6b63 | 2014-12-04 11:49:26 -0500 | [diff] [blame] | 4477 | spin_unlock(&sdebug_host_list_lock); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4478 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4479 | return count; |
| 4480 | } |
| 4481 | return -EINVAL; |
| 4482 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4483 | static DRIVER_ATTR_RW(virtual_gb); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4484 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4485 | static ssize_t add_host_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4486 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4487 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | } |
| 4489 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4490 | static ssize_t add_host_store(struct device_driver *ddp, const char *buf, |
| 4491 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | { |
FUJITA Tomonori | f3df41c | 2008-03-20 11:09:15 +0900 | [diff] [blame] | 4493 | int delta_hosts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | |
FUJITA Tomonori | f3df41c | 2008-03-20 11:09:15 +0900 | [diff] [blame] | 4495 | if (sscanf(buf, "%d", &delta_hosts) != 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | if (delta_hosts > 0) { |
| 4498 | do { |
| 4499 | sdebug_add_adapter(); |
| 4500 | } while (--delta_hosts); |
| 4501 | } else if (delta_hosts < 0) { |
| 4502 | do { |
| 4503 | sdebug_remove_adapter(); |
| 4504 | } while (++delta_hosts); |
| 4505 | } |
| 4506 | return count; |
| 4507 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4508 | static DRIVER_ATTR_RW(add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4510 | 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] | 4511 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4512 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4513 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4514 | static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf, |
| 4515 | size_t count) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4516 | { |
| 4517 | int n; |
| 4518 | |
| 4519 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4520 | sdebug_vpd_use_hostno = n; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4521 | return count; |
| 4522 | } |
| 4523 | return -EINVAL; |
| 4524 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4525 | static DRIVER_ATTR_RW(vpd_use_hostno); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4526 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4527 | static ssize_t sector_size_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4528 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4529 | return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size); |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4530 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4531 | static DRIVER_ATTR_RO(sector_size); |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4532 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4533 | static ssize_t dix_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4534 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4535 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4536 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4537 | static DRIVER_ATTR_RO(dix); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4538 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4539 | static ssize_t dif_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4540 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4541 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4542 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4543 | static DRIVER_ATTR_RO(dif); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4544 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4545 | static ssize_t guard_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4546 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4547 | return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4548 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4549 | static DRIVER_ATTR_RO(guard); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4550 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4551 | static ssize_t ato_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4552 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4553 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4554 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4555 | static DRIVER_ATTR_RO(ato); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4556 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4557 | static ssize_t map_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4558 | { |
| 4559 | ssize_t count; |
| 4560 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4561 | if (!scsi_debug_lbp()) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4562 | return scnprintf(buf, PAGE_SIZE, "0-%u\n", |
| 4563 | sdebug_store_sectors); |
| 4564 | |
Tejun Heo | c7badc9 | 2015-02-13 14:37:51 -0800 | [diff] [blame] | 4565 | count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", |
| 4566 | (int)map_size, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4567 | buf[count++] = '\n'; |
Tejun Heo | c7badc9 | 2015-02-13 14:37:51 -0800 | [diff] [blame] | 4568 | buf[count] = '\0'; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4569 | |
| 4570 | return count; |
| 4571 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4572 | static DRIVER_ATTR_RO(map); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4573 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4574 | static ssize_t removable_show(struct device_driver *ddp, char *buf) |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4575 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4576 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4577 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4578 | static ssize_t removable_store(struct device_driver *ddp, const char *buf, |
| 4579 | size_t count) |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4580 | { |
| 4581 | int n; |
| 4582 | |
| 4583 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4584 | sdebug_removable = (n > 0); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4585 | return count; |
| 4586 | } |
| 4587 | return -EINVAL; |
| 4588 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4589 | static DRIVER_ATTR_RW(removable); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4590 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4591 | static ssize_t host_lock_show(struct device_driver *ddp, char *buf) |
| 4592 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4593 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4594 | } |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame^] | 4595 | /* N.B. sdebug_host_lock does nothing, kept for backward compatibility */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4596 | static ssize_t host_lock_store(struct device_driver *ddp, const char *buf, |
| 4597 | size_t count) |
| 4598 | { |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame^] | 4599 | int n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4600 | |
| 4601 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame^] | 4602 | sdebug_host_lock = (n > 0); |
| 4603 | return count; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4604 | } |
| 4605 | return -EINVAL; |
| 4606 | } |
| 4607 | static DRIVER_ATTR_RW(host_lock); |
| 4608 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4609 | static ssize_t strict_show(struct device_driver *ddp, char *buf) |
| 4610 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4611 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4612 | } |
| 4613 | static ssize_t strict_store(struct device_driver *ddp, const char *buf, |
| 4614 | size_t count) |
| 4615 | { |
| 4616 | int n; |
| 4617 | |
| 4618 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4619 | sdebug_strict = (n > 0); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4620 | return count; |
| 4621 | } |
| 4622 | return -EINVAL; |
| 4623 | } |
| 4624 | static DRIVER_ATTR_RW(strict); |
| 4625 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4626 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4627 | /* Note: The following array creates attribute files in the |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4628 | /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these |
| 4629 | files (over those found in the /sys/module/scsi_debug/parameters |
| 4630 | directory) is that auxiliary actions can be triggered when an attribute |
| 4631 | is changed. For example see: sdebug_add_host_store() above. |
| 4632 | */ |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4633 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4634 | static struct attribute *sdebug_drv_attrs[] = { |
| 4635 | &driver_attr_delay.attr, |
| 4636 | &driver_attr_opts.attr, |
| 4637 | &driver_attr_ptype.attr, |
| 4638 | &driver_attr_dsense.attr, |
| 4639 | &driver_attr_fake_rw.attr, |
| 4640 | &driver_attr_no_lun_0.attr, |
| 4641 | &driver_attr_num_tgts.attr, |
| 4642 | &driver_attr_dev_size_mb.attr, |
| 4643 | &driver_attr_num_parts.attr, |
| 4644 | &driver_attr_every_nth.attr, |
| 4645 | &driver_attr_max_luns.attr, |
| 4646 | &driver_attr_max_queue.attr, |
| 4647 | &driver_attr_no_uld.attr, |
| 4648 | &driver_attr_scsi_level.attr, |
| 4649 | &driver_attr_virtual_gb.attr, |
| 4650 | &driver_attr_add_host.attr, |
| 4651 | &driver_attr_vpd_use_hostno.attr, |
| 4652 | &driver_attr_sector_size.attr, |
| 4653 | &driver_attr_dix.attr, |
| 4654 | &driver_attr_dif.attr, |
| 4655 | &driver_attr_guard.attr, |
| 4656 | &driver_attr_ato.attr, |
| 4657 | &driver_attr_map.attr, |
| 4658 | &driver_attr_removable.attr, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4659 | &driver_attr_host_lock.attr, |
| 4660 | &driver_attr_ndelay.attr, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4661 | &driver_attr_strict.attr, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4662 | NULL, |
| 4663 | }; |
| 4664 | ATTRIBUTE_GROUPS(sdebug_drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | |
Akinobu Mita | 11ddcec | 2014-02-26 22:56:59 +0900 | [diff] [blame] | 4666 | static struct device *pseudo_primary; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 4667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4668 | static int __init scsi_debug_init(void) |
| 4669 | { |
FUJITA Tomonori | 5f2578e | 2008-03-30 00:59:57 +0900 | [diff] [blame] | 4670 | unsigned long sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | int host_to_add; |
| 4672 | int k; |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4673 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4674 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4675 | atomic_set(&sdebug_cmnd_count, 0); |
| 4676 | atomic_set(&sdebug_completions, 0); |
| 4677 | atomic_set(&retired_max_queue, 0); |
| 4678 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4679 | if (sdebug_ndelay >= 1000 * 1000 * 1000) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4680 | pr_warn("ndelay must be less than 1 second, ignored\n"); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4681 | sdebug_ndelay = 0; |
| 4682 | } else if (sdebug_ndelay > 0) |
| 4683 | sdebug_delay = DELAY_OVERRIDDEN; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4684 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4685 | switch (sdebug_sector_size) { |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4686 | case 512: |
| 4687 | case 1024: |
| 4688 | case 2048: |
| 4689 | case 4096: |
| 4690 | break; |
| 4691 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4692 | pr_err("invalid sector_size %d\n", sdebug_sector_size); |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4693 | return -EINVAL; |
| 4694 | } |
| 4695 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4696 | switch (sdebug_dif) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4697 | |
| 4698 | case SD_DIF_TYPE0_PROTECTION: |
| 4699 | case SD_DIF_TYPE1_PROTECTION: |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 4700 | case SD_DIF_TYPE2_PROTECTION: |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4701 | case SD_DIF_TYPE3_PROTECTION: |
| 4702 | break; |
| 4703 | |
| 4704 | default: |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4705 | pr_err("dif must be 0, 1, 2 or 3\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4706 | return -EINVAL; |
| 4707 | } |
| 4708 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4709 | if (sdebug_guard > 1) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4710 | pr_err("guard must be 0 or 1\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4711 | return -EINVAL; |
| 4712 | } |
| 4713 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4714 | if (sdebug_ato > 1) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4715 | pr_err("ato must be 0 or 1\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4716 | return -EINVAL; |
| 4717 | } |
| 4718 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4719 | if (sdebug_physblk_exp > 15) { |
| 4720 | pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 4721 | return -EINVAL; |
| 4722 | } |
| 4723 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4724 | if (sdebug_lowest_aligned > 0x3fff) { |
| 4725 | pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 4726 | return -EINVAL; |
| 4727 | } |
| 4728 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4729 | if (sdebug_dev_size_mb < 1) |
| 4730 | sdebug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */ |
| 4731 | sz = (unsigned long)sdebug_dev_size_mb * 1048576; |
| 4732 | sdebug_store_sectors = sz / sdebug_sector_size; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4733 | sdebug_capacity = get_sdebug_capacity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | |
| 4735 | /* play around with geometry, don't waste too much on track 0 */ |
| 4736 | sdebug_heads = 8; |
| 4737 | sdebug_sectors_per = 32; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4738 | if (sdebug_dev_size_mb >= 256) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | sdebug_heads = 64; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4740 | else if (sdebug_dev_size_mb >= 16) |
Andy Shevchenko | fa785f0 | 2015-11-26 20:22:50 +0200 | [diff] [blame] | 4741 | sdebug_heads = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | sdebug_cylinders_per = (unsigned long)sdebug_capacity / |
| 4743 | (sdebug_sectors_per * sdebug_heads); |
| 4744 | if (sdebug_cylinders_per >= 1024) { |
| 4745 | /* other LLDs do this; implies >= 1GB ram disk ... */ |
| 4746 | sdebug_heads = 255; |
| 4747 | sdebug_sectors_per = 63; |
| 4748 | sdebug_cylinders_per = (unsigned long)sdebug_capacity / |
| 4749 | (sdebug_sectors_per * sdebug_heads); |
| 4750 | } |
| 4751 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4752 | if (0 == sdebug_fake_rw) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4753 | fake_storep = vmalloc(sz); |
| 4754 | if (NULL == fake_storep) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4755 | pr_err("out of memory, 1\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4756 | return -ENOMEM; |
| 4757 | } |
| 4758 | memset(fake_storep, 0, sz); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4759 | if (sdebug_num_parts > 0) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4760 | sdebug_build_parts(fake_storep, sz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4763 | if (sdebug_dix) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4764 | int dif_size; |
| 4765 | |
| 4766 | dif_size = sdebug_store_sectors * sizeof(struct sd_dif_tuple); |
| 4767 | dif_storep = vmalloc(dif_size); |
| 4768 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4769 | 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] | 4770 | |
| 4771 | if (dif_storep == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4772 | pr_err("out of mem. (DIX)\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4773 | ret = -ENOMEM; |
| 4774 | goto free_vm; |
| 4775 | } |
| 4776 | |
| 4777 | memset(dif_storep, 0xff, dif_size); |
| 4778 | } |
| 4779 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4780 | /* Logical Block Provisioning */ |
| 4781 | if (scsi_debug_lbp()) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4782 | sdebug_unmap_max_blocks = |
| 4783 | clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 4784 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4785 | sdebug_unmap_max_desc = |
| 4786 | clamp(sdebug_unmap_max_desc, 0U, 256U); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 4787 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4788 | sdebug_unmap_granularity = |
| 4789 | clamp(sdebug_unmap_granularity, 1U, 0xffffffffU); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 4790 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4791 | if (sdebug_unmap_alignment && |
| 4792 | sdebug_unmap_granularity <= |
| 4793 | sdebug_unmap_alignment) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4794 | pr_err("ERR: unmap_granularity <= unmap_alignment\n"); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4795 | return -EINVAL; |
| 4796 | } |
| 4797 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 4798 | map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1; |
| 4799 | map_storep = vmalloc(BITS_TO_LONGS(map_size) * sizeof(long)); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4800 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4801 | pr_info("%lu provisioning blocks\n", map_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4802 | |
| 4803 | if (map_storep == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4804 | pr_err("out of mem. (MAP)\n"); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4805 | ret = -ENOMEM; |
| 4806 | goto free_vm; |
| 4807 | } |
| 4808 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 4809 | bitmap_zero(map_storep, map_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4810 | |
| 4811 | /* Map first 1KB for partition table */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4812 | if (sdebug_num_parts) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4813 | map_region(0, 2); |
| 4814 | } |
| 4815 | |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 4816 | pseudo_primary = root_device_register("pseudo_0"); |
| 4817 | if (IS_ERR(pseudo_primary)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4818 | pr_warn("root_device_register() error\n"); |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 4819 | ret = PTR_ERR(pseudo_primary); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4820 | goto free_vm; |
| 4821 | } |
| 4822 | ret = bus_register(&pseudo_lld_bus); |
| 4823 | if (ret < 0) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4824 | pr_warn("bus_register error: %d\n", ret); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4825 | goto dev_unreg; |
| 4826 | } |
| 4827 | ret = driver_register(&sdebug_driverfs_driver); |
| 4828 | if (ret < 0) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4829 | pr_warn("driver_register error: %d\n", ret); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4830 | goto bus_unreg; |
| 4831 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4832 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4833 | host_to_add = sdebug_add_host; |
| 4834 | sdebug_add_host = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | |
| 4836 | for (k = 0; k < host_to_add; k++) { |
| 4837 | if (sdebug_add_adapter()) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4838 | pr_err("sdebug_add_adapter failed k=%d\n", k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | break; |
| 4840 | } |
| 4841 | } |
| 4842 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4843 | if (sdebug_verbose) |
| 4844 | pr_info("built %d host(s)\n", sdebug_add_host); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | return 0; |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4847 | |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4848 | bus_unreg: |
| 4849 | bus_unregister(&pseudo_lld_bus); |
| 4850 | dev_unreg: |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 4851 | root_device_unregister(pseudo_primary); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4852 | free_vm: |
Tomas Winkler | de232af | 2015-07-28 16:54:22 +0300 | [diff] [blame] | 4853 | vfree(map_storep); |
| 4854 | vfree(dif_storep); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4855 | vfree(fake_storep); |
| 4856 | |
| 4857 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | } |
| 4859 | |
| 4860 | static void __exit scsi_debug_exit(void) |
| 4861 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4862 | int k = sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | |
| 4864 | stop_all_queued(); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4865 | free_all_queued(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4866 | for (; k; k--) |
| 4867 | sdebug_remove_adapter(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | driver_unregister(&sdebug_driverfs_driver); |
| 4869 | bus_unregister(&pseudo_lld_bus); |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 4870 | root_device_unregister(pseudo_primary); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | |
Tomas Winkler | de232af | 2015-07-28 16:54:22 +0300 | [diff] [blame] | 4872 | vfree(dif_storep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | vfree(fake_storep); |
| 4874 | } |
| 4875 | |
| 4876 | device_initcall(scsi_debug_init); |
| 4877 | module_exit(scsi_debug_exit); |
| 4878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | static void sdebug_release_adapter(struct device * dev) |
| 4880 | { |
| 4881 | struct sdebug_host_info *sdbg_host; |
| 4882 | |
| 4883 | sdbg_host = to_sdebug_host(dev); |
| 4884 | kfree(sdbg_host); |
| 4885 | } |
| 4886 | |
| 4887 | static int sdebug_add_adapter(void) |
| 4888 | { |
| 4889 | int k, devs_per_host; |
| 4890 | int error = 0; |
| 4891 | struct sdebug_host_info *sdbg_host; |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 4892 | struct sdebug_dev_info *sdbg_devinfo, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4893 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4894 | sdbg_host = kzalloc(sizeof(*sdbg_host),GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4895 | if (NULL == sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4896 | pr_err("out of memory at line %d\n", __LINE__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4897 | return -ENOMEM; |
| 4898 | } |
| 4899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4900 | INIT_LIST_HEAD(&sdbg_host->dev_info_list); |
| 4901 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4902 | devs_per_host = sdebug_num_tgts * sdebug_max_luns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | for (k = 0; k < devs_per_host; k++) { |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 4904 | sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL); |
| 4905 | if (!sdbg_devinfo) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4906 | pr_err("out of memory at line %d\n", __LINE__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | error = -ENOMEM; |
| 4908 | goto clean; |
| 4909 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | spin_lock(&sdebug_host_list_lock); |
| 4913 | list_add_tail(&sdbg_host->host_list, &sdebug_host_list); |
| 4914 | spin_unlock(&sdebug_host_list_lock); |
| 4915 | |
| 4916 | sdbg_host->dev.bus = &pseudo_lld_bus; |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 4917 | sdbg_host->dev.parent = pseudo_primary; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | sdbg_host->dev.release = &sdebug_release_adapter; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4919 | dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4920 | |
| 4921 | error = device_register(&sdbg_host->dev); |
| 4922 | |
| 4923 | if (error) |
| 4924 | goto clean; |
| 4925 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4926 | ++sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | return error; |
| 4928 | |
| 4929 | clean: |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 4930 | list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, |
| 4931 | dev_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | list_del(&sdbg_devinfo->dev_list); |
| 4933 | kfree(sdbg_devinfo); |
| 4934 | } |
| 4935 | |
| 4936 | kfree(sdbg_host); |
| 4937 | return error; |
| 4938 | } |
| 4939 | |
| 4940 | static void sdebug_remove_adapter(void) |
| 4941 | { |
| 4942 | struct sdebug_host_info * sdbg_host = NULL; |
| 4943 | |
| 4944 | spin_lock(&sdebug_host_list_lock); |
| 4945 | if (!list_empty(&sdebug_host_list)) { |
| 4946 | sdbg_host = list_entry(sdebug_host_list.prev, |
| 4947 | struct sdebug_host_info, host_list); |
| 4948 | list_del(&sdbg_host->host_list); |
| 4949 | } |
| 4950 | spin_unlock(&sdebug_host_list_lock); |
| 4951 | |
| 4952 | if (!sdbg_host) |
| 4953 | return; |
| 4954 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4955 | device_unregister(&sdbg_host->dev); |
| 4956 | --sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | } |
| 4958 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4959 | static int |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4960 | sdebug_change_qdepth(struct scsi_device *sdev, int qdepth) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4961 | { |
| 4962 | int num_in_q = 0; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4963 | unsigned long iflags; |
| 4964 | struct sdebug_dev_info *devip; |
| 4965 | |
| 4966 | spin_lock_irqsave(&queued_arr_lock, iflags); |
| 4967 | devip = (struct sdebug_dev_info *)sdev->hostdata; |
| 4968 | if (NULL == devip) { |
| 4969 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
| 4970 | return -ENODEV; |
| 4971 | } |
| 4972 | num_in_q = atomic_read(&devip->num_in_q); |
| 4973 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4974 | |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 4975 | if (qdepth < 1) |
| 4976 | qdepth = 1; |
| 4977 | /* allow to exceed max host queued_arr elements for testing */ |
| 4978 | if (qdepth > SCSI_DEBUG_CANQUEUE + 10) |
| 4979 | qdepth = SCSI_DEBUG_CANQUEUE + 10; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4980 | scsi_change_queue_depth(sdev, qdepth); |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 4981 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4982 | if (SDEBUG_OPT_Q_NOISE & sdebug_opts) { |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 4983 | sdev_printk(KERN_INFO, sdev, |
| 4984 | "%s: qdepth=%d, num_in_q=%d\n", |
| 4985 | __func__, qdepth, num_in_q); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4986 | } |
| 4987 | return sdev->queue_depth; |
| 4988 | } |
| 4989 | |
| 4990 | static int |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 4991 | check_inject(struct scsi_cmnd *scp) |
| 4992 | { |
| 4993 | struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp); |
| 4994 | |
| 4995 | memset(ep, 0, sizeof(struct sdebug_scmd_extra_t)); |
| 4996 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4997 | if (atomic_inc_return(&sdebug_cmnd_count) >= abs(sdebug_every_nth)) { |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 4998 | atomic_set(&sdebug_cmnd_count, 0); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4999 | if (sdebug_every_nth < -1) |
| 5000 | sdebug_every_nth = -1; |
| 5001 | if (SDEBUG_OPT_TIMEOUT & sdebug_opts) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5002 | return 1; /* ignore command causing timeout */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5003 | else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts && |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5004 | scsi_medium_access_command(scp)) |
| 5005 | return 1; /* time out reads and writes */ |
| 5006 | if (sdebug_any_injecting_opt) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5007 | if (SDEBUG_OPT_RECOVERED_ERR & sdebug_opts) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5008 | ep->inj_recovered = true; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5009 | if (SDEBUG_OPT_TRANSPORT_ERR & sdebug_opts) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5010 | ep->inj_transport = true; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5011 | if (SDEBUG_OPT_DIF_ERR & sdebug_opts) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5012 | ep->inj_dif = true; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5013 | if (SDEBUG_OPT_DIX_ERR & sdebug_opts) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5014 | ep->inj_dix = true; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5015 | if (SDEBUG_OPT_SHORT_TRANSFER & sdebug_opts) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5016 | ep->inj_short = true; |
| 5017 | } |
| 5018 | } |
| 5019 | return 0; |
| 5020 | } |
| 5021 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5022 | static int |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame^] | 5023 | scsi_debug_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scp) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5024 | { |
| 5025 | u8 sdeb_i; |
| 5026 | struct scsi_device *sdp = scp->device; |
| 5027 | const struct opcode_info_t *oip; |
| 5028 | const struct opcode_info_t *r_oip; |
| 5029 | struct sdebug_dev_info *devip; |
| 5030 | u8 *cmd = scp->cmnd; |
| 5031 | int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 5032 | int k, na; |
| 5033 | int errsts = 0; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5034 | u32 flags; |
| 5035 | u16 sa; |
| 5036 | u8 opcode = cmd[0]; |
| 5037 | bool has_wlun_rl; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5038 | |
| 5039 | scsi_set_resid(scp, 0); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5040 | if (sdebug_verbose && !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5041 | char b[120]; |
| 5042 | int n, len, sb; |
| 5043 | |
| 5044 | len = scp->cmd_len; |
| 5045 | sb = (int)sizeof(b); |
| 5046 | if (len > 32) |
| 5047 | strcpy(b, "too long, over 32 bytes"); |
| 5048 | else { |
| 5049 | for (k = 0, n = 0; k < len && n < sb; ++k) |
| 5050 | n += scnprintf(b + n, sb - n, "%02x ", |
| 5051 | (u32)cmd[k]); |
| 5052 | } |
| 5053 | sdev_printk(KERN_INFO, sdp, "%s: cmd %s\n", my_name, b); |
| 5054 | } |
Tomas Winkler | 34d5543 | 2015-07-28 16:54:21 +0300 | [diff] [blame] | 5055 | has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5056 | if ((sdp->lun >= sdebug_max_luns) && !has_wlun_rl) |
| 5057 | return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, 0); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5058 | |
| 5059 | sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */ |
| 5060 | oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */ |
| 5061 | devip = (struct sdebug_dev_info *)sdp->hostdata; |
| 5062 | if (!devip) { |
| 5063 | devip = devInfoReg(sdp); |
| 5064 | if (NULL == devip) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5065 | return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, |
| 5066 | 0); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5067 | } |
| 5068 | na = oip->num_attached; |
| 5069 | r_pfp = oip->pfp; |
| 5070 | if (na) { /* multiple commands with this opcode */ |
| 5071 | r_oip = oip; |
| 5072 | if (FF_SA & r_oip->flags) { |
| 5073 | if (F_SA_LOW & oip->flags) |
| 5074 | sa = 0x1f & cmd[1]; |
| 5075 | else |
| 5076 | sa = get_unaligned_be16(cmd + 8); |
| 5077 | for (k = 0; k <= na; oip = r_oip->arrp + k++) { |
| 5078 | if (opcode == oip->opcode && sa == oip->sa) |
| 5079 | break; |
| 5080 | } |
| 5081 | } else { /* since no service action only check opcode */ |
| 5082 | for (k = 0; k <= na; oip = r_oip->arrp + k++) { |
| 5083 | if (opcode == oip->opcode) |
| 5084 | break; |
| 5085 | } |
| 5086 | } |
| 5087 | if (k > na) { |
| 5088 | if (F_SA_LOW & r_oip->flags) |
| 5089 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4); |
| 5090 | else if (F_SA_HIGH & r_oip->flags) |
| 5091 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7); |
| 5092 | else |
| 5093 | mk_sense_invalid_opcode(scp); |
| 5094 | goto check_cond; |
| 5095 | } |
| 5096 | } /* else (when na==0) we assume the oip is a match */ |
| 5097 | flags = oip->flags; |
| 5098 | if (F_INV_OP & flags) { |
| 5099 | mk_sense_invalid_opcode(scp); |
| 5100 | goto check_cond; |
| 5101 | } |
| 5102 | if (has_wlun_rl && !(F_RL_WLUN_OK & flags)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5103 | if (sdebug_verbose) |
| 5104 | sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n", |
| 5105 | my_name, opcode, " supported for wlun"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5106 | mk_sense_invalid_opcode(scp); |
| 5107 | goto check_cond; |
| 5108 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5109 | if (sdebug_strict) { /* check cdb against mask */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5110 | u8 rem; |
| 5111 | int j; |
| 5112 | |
| 5113 | for (k = 1; k < oip->len_mask[0] && k < 16; ++k) { |
| 5114 | rem = ~oip->len_mask[k] & cmd[k]; |
| 5115 | if (rem) { |
| 5116 | for (j = 7; j >= 0; --j, rem <<= 1) { |
| 5117 | if (0x80 & rem) |
| 5118 | break; |
| 5119 | } |
| 5120 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j); |
| 5121 | goto check_cond; |
| 5122 | } |
| 5123 | } |
| 5124 | } |
| 5125 | if (!(F_SKIP_UA & flags) && |
| 5126 | SDEBUG_NUM_UAS != find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS)) { |
| 5127 | errsts = check_readiness(scp, UAS_ONLY, devip); |
| 5128 | if (errsts) |
| 5129 | goto check_cond; |
| 5130 | } |
| 5131 | if ((F_M_ACCESS & flags) && devip->stopped) { |
| 5132 | mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5133 | if (sdebug_verbose) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5134 | sdev_printk(KERN_INFO, sdp, "%s reports: Not ready: " |
| 5135 | "%s\n", my_name, "initializing command " |
| 5136 | "required"); |
| 5137 | errsts = check_condition_result; |
| 5138 | goto fini; |
| 5139 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5140 | if (sdebug_fake_rw && (F_FAKE_RW & flags)) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5141 | goto fini; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5142 | if (sdebug_every_nth) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5143 | if (check_inject(scp)) |
| 5144 | return 0; /* ignore command: make trouble */ |
| 5145 | } |
| 5146 | if (oip->pfp) /* if this command has a resp_* function, call it */ |
| 5147 | errsts = oip->pfp(scp, devip); |
| 5148 | else if (r_pfp) /* if leaf function ptr NULL, try the root's */ |
| 5149 | errsts = r_pfp(scp, devip); |
| 5150 | |
| 5151 | fini: |
| 5152 | return schedule_resp(scp, devip, errsts, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5153 | ((F_DELAY_OVERR & flags) ? 0 : sdebug_delay)); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5154 | check_cond: |
| 5155 | return schedule_resp(scp, devip, check_condition_result, 0); |
| 5156 | } |
| 5157 | |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5158 | static struct scsi_host_template sdebug_driver_template = { |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 5159 | .show_info = scsi_debug_show_info, |
| 5160 | .write_info = scsi_debug_write_info, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5161 | .proc_name = sdebug_proc_name, |
| 5162 | .name = "SCSI DEBUG", |
| 5163 | .info = scsi_debug_info, |
| 5164 | .slave_alloc = scsi_debug_slave_alloc, |
| 5165 | .slave_configure = scsi_debug_slave_configure, |
| 5166 | .slave_destroy = scsi_debug_slave_destroy, |
| 5167 | .ioctl = scsi_debug_ioctl, |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame^] | 5168 | .queuecommand = scsi_debug_queuecommand, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5169 | .change_queue_depth = sdebug_change_qdepth, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5170 | .eh_abort_handler = scsi_debug_abort, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5171 | .eh_device_reset_handler = scsi_debug_device_reset, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5172 | .eh_target_reset_handler = scsi_debug_target_reset, |
| 5173 | .eh_bus_reset_handler = scsi_debug_bus_reset, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5174 | .eh_host_reset_handler = scsi_debug_host_reset, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5175 | .can_queue = SCSI_DEBUG_CANQUEUE, |
| 5176 | .this_id = 7, |
Ming Lin | 65e8617 | 2016-04-04 14:48:10 -0700 | [diff] [blame] | 5177 | .sg_tablesize = SG_MAX_SEGMENTS, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5178 | .cmd_per_lun = DEF_CMD_PER_LUN, |
Akinobu Mita | 6bb5e6e | 2014-06-02 22:56:49 +0900 | [diff] [blame] | 5179 | .max_sectors = -1U, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5180 | .use_clustering = DISABLE_CLUSTERING, |
| 5181 | .module = THIS_MODULE, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5182 | .track_queue_depth = 1, |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5183 | .cmd_size = sizeof(struct sdebug_scmd_extra_t), |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5184 | }; |
| 5185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | static int sdebug_driver_probe(struct device * dev) |
| 5187 | { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 5188 | int error = 0; |
| 5189 | struct sdebug_host_info *sdbg_host; |
| 5190 | struct Scsi_Host *hpnt; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5191 | int host_prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | |
| 5193 | sdbg_host = to_sdebug_host(dev); |
| 5194 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5195 | sdebug_driver_template.can_queue = sdebug_max_queue; |
| 5196 | if (sdebug_clustering) |
Akinobu Mita | 0759c66 | 2014-02-26 22:57:04 +0900 | [diff] [blame] | 5197 | sdebug_driver_template.use_clustering = ENABLE_CLUSTERING; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5198 | hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host)); |
| 5199 | if (NULL == hpnt) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5200 | pr_err("scsi_host_alloc failed\n"); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5201 | error = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5202 | return error; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | |
| 5205 | sdbg_host->shost = hpnt; |
| 5206 | *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5207 | if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id)) |
| 5208 | hpnt->max_id = sdebug_num_tgts + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5210 | hpnt->max_id = sdebug_num_tgts; |
| 5211 | /* = sdebug_max_luns; */ |
Tomas Winkler | f2d3fd2 | 2015-07-28 16:54:25 +0300 | [diff] [blame] | 5212 | hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5214 | host_prot = 0; |
| 5215 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5216 | switch (sdebug_dif) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5217 | |
| 5218 | case SD_DIF_TYPE1_PROTECTION: |
| 5219 | host_prot = SHOST_DIF_TYPE1_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5220 | if (sdebug_dix) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5221 | host_prot |= SHOST_DIX_TYPE1_PROTECTION; |
| 5222 | break; |
| 5223 | |
| 5224 | case SD_DIF_TYPE2_PROTECTION: |
| 5225 | host_prot = SHOST_DIF_TYPE2_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5226 | if (sdebug_dix) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5227 | host_prot |= SHOST_DIX_TYPE2_PROTECTION; |
| 5228 | break; |
| 5229 | |
| 5230 | case SD_DIF_TYPE3_PROTECTION: |
| 5231 | host_prot = SHOST_DIF_TYPE3_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5232 | if (sdebug_dix) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5233 | host_prot |= SHOST_DIX_TYPE3_PROTECTION; |
| 5234 | break; |
| 5235 | |
| 5236 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5237 | if (sdebug_dix) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5238 | host_prot |= SHOST_DIX_TYPE0_PROTECTION; |
| 5239 | break; |
| 5240 | } |
| 5241 | |
| 5242 | scsi_host_set_prot(hpnt, host_prot); |
| 5243 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5244 | pr_info("host protection%s%s%s%s%s%s%s\n", |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5245 | (host_prot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "", |
| 5246 | (host_prot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "", |
| 5247 | (host_prot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "", |
| 5248 | (host_prot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "", |
| 5249 | (host_prot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "", |
| 5250 | (host_prot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "", |
| 5251 | (host_prot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : ""); |
| 5252 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5253 | if (sdebug_guard == 1) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5254 | scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP); |
| 5255 | else |
| 5256 | scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC); |
| 5257 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5258 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts); |
| 5259 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & sdebug_opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | error = scsi_add_host(hpnt, &sdbg_host->dev); |
| 5261 | if (error) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5262 | pr_err("scsi_add_host failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | error = -ENODEV; |
| 5264 | scsi_host_put(hpnt); |
| 5265 | } else |
| 5266 | scsi_scan_host(hpnt); |
| 5267 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5268 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | static int sdebug_driver_remove(struct device * dev) |
| 5272 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | struct sdebug_host_info *sdbg_host; |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5274 | struct sdebug_dev_info *sdbg_devinfo, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | |
| 5276 | sdbg_host = to_sdebug_host(dev); |
| 5277 | |
| 5278 | if (!sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5279 | pr_err("Unable to locate host info\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | return -ENODEV; |
| 5281 | } |
| 5282 | |
| 5283 | scsi_remove_host(sdbg_host->shost); |
| 5284 | |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5285 | list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, |
| 5286 | dev_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | list_del(&sdbg_devinfo->dev_list); |
| 5288 | kfree(sdbg_devinfo); |
| 5289 | } |
| 5290 | |
| 5291 | scsi_host_put(sdbg_host->shost); |
| 5292 | return 0; |
| 5293 | } |
| 5294 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5295 | static int pseudo_lld_bus_match(struct device *dev, |
| 5296 | struct device_driver *dev_driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | { |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5298 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5300 | |
| 5301 | static struct bus_type pseudo_lld_bus = { |
| 5302 | .name = "pseudo", |
| 5303 | .match = pseudo_lld_bus_match, |
| 5304 | .probe = sdebug_driver_probe, |
| 5305 | .remove = sdebug_driver_remove, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5306 | .drv_groups = sdebug_drv_groups, |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5307 | }; |