Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv |
| 3 | * Copyright (C) 1992 Eric Youngdale |
| 4 | * Simulate a host adapter with 2 disks attached. Do a lot of checking |
| 5 | * to make sure that we are not getting blocks mixed up, and PANIC if |
| 6 | * anything out of the ordinary is seen. |
| 7 | * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 8 | * |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 9 | * Copyright (C) 2001 - 2016 Douglas Gilbert |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2, or (at your option) |
| 14 | * any later version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 16 | * For documentation see http://sg.danny.cz/sg/sdebug26.html |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 20 | |
| 21 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | |
| 25 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/errno.h> |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 27 | #include <linux/jiffies.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/types.h> |
| 30 | #include <linux/string.h> |
| 31 | #include <linux/genhd.h> |
| 32 | #include <linux/fs.h> |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/proc_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/vmalloc.h> |
| 36 | #include <linux/moduleparam.h> |
Jens Axboe | 852e034 | 2007-07-16 10:19:24 +0200 | [diff] [blame] | 37 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/blkdev.h> |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 39 | #include <linux/crc-t10dif.h> |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 40 | #include <linux/spinlock.h> |
| 41 | #include <linux/interrupt.h> |
| 42 | #include <linux/atomic.h> |
| 43 | #include <linux/hrtimer.h> |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 44 | |
| 45 | #include <net/checksum.h> |
FUJITA Tomonori | 9ff26ee | 2008-03-02 18:30:15 +0900 | [diff] [blame] | 46 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 47 | #include <asm/unaligned.h> |
| 48 | |
FUJITA Tomonori | 9ff26ee | 2008-03-02 18:30:15 +0900 | [diff] [blame] | 49 | #include <scsi/scsi.h> |
| 50 | #include <scsi/scsi_cmnd.h> |
| 51 | #include <scsi/scsi_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <scsi/scsi_host.h> |
| 53 | #include <scsi/scsicam.h> |
FUJITA Tomonori | a34c4e9 | 2008-03-25 09:26:50 +0900 | [diff] [blame] | 54 | #include <scsi/scsi_eh.h> |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 55 | #include <scsi/scsi_tcq.h> |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 56 | #include <scsi/scsi_dbg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 58 | #include "sd.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include "scsi_logging.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 61 | /* make sure inq_product_rev string corresponds to this version */ |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 62 | #define SDEBUG_VERSION "1.86" |
| 63 | static const char *sdebug_version_date = "20160430"; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 64 | |
| 65 | #define MY_NAME "scsi_debug" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 67 | /* Additional Sense Code (ASC) */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 68 | #define NO_ADDITIONAL_SENSE 0x0 |
| 69 | #define LOGICAL_UNIT_NOT_READY 0x4 |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 70 | #define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #define UNRECOVERED_READ_ERR 0x11 |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 72 | #define PARAMETER_LIST_LENGTH_ERR 0x1a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | #define INVALID_OPCODE 0x20 |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 74 | #define LBA_OUT_OF_RANGE 0x21 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #define INVALID_FIELD_IN_CDB 0x24 |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 76 | #define INVALID_FIELD_IN_PARAM_LIST 0x26 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 77 | #define UA_RESET_ASC 0x29 |
| 78 | #define UA_CHANGED_ASC 0x2a |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 79 | #define TARGET_CHANGED_ASC 0x3f |
| 80 | #define LUNS_CHANGED_ASCQ 0x0e |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 81 | #define INSUFF_RES_ASC 0x55 |
| 82 | #define INSUFF_RES_ASCQ 0x3 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 83 | #define POWER_ON_RESET_ASCQ 0x0 |
| 84 | #define BUS_RESET_ASCQ 0x2 /* scsi bus reset occurred */ |
| 85 | #define MODE_CHANGED_ASCQ 0x1 /* mode parameters changed */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 86 | #define CAPACITY_CHANGED_ASCQ 0x9 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | #define SAVING_PARAMS_UNSUP 0x39 |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 88 | #define TRANSPORT_PROBLEM 0x4b |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 89 | #define THRESHOLD_EXCEEDED 0x5d |
| 90 | #define LOW_POWER_COND_ON 0x5e |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 91 | #define MISCOMPARE_VERIFY_ASC 0x1d |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 92 | #define MICROCODE_CHANGED_ASCQ 0x1 /* with TARGET_CHANGED_ASC */ |
| 93 | #define MICROCODE_CHANGED_WO_RESET_ASCQ 0x16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 95 | /* Additional Sense Code Qualifier (ASCQ) */ |
| 96 | #define ACK_NAK_TO 0x3 |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /* Default values for driver parameters */ |
| 99 | #define DEF_NUM_HOST 1 |
| 100 | #define DEF_NUM_TGTS 1 |
| 101 | #define DEF_MAX_LUNS 1 |
| 102 | /* With these defaults, this driver will make 1 host with 1 target |
| 103 | * (id 0) containing 1 logical unit (lun 0). That is 1 device. |
| 104 | */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 105 | #define DEF_ATO 1 |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 106 | #define DEF_JDELAY 1 /* if > 0 unit is a jiffy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | #define DEF_DEV_SIZE_MB 8 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 108 | #define DEF_DIF 0 |
| 109 | #define DEF_DIX 0 |
| 110 | #define DEF_D_SENSE 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | #define DEF_EVERY_NTH 0 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 112 | #define DEF_FAKE_RW 0 |
| 113 | #define DEF_GUARD 0 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 114 | #define DEF_HOST_LOCK 0 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 115 | #define DEF_LBPU 0 |
| 116 | #define DEF_LBPWS 0 |
| 117 | #define DEF_LBPWS10 0 |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 118 | #define DEF_LBPRZ 1 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 119 | #define DEF_LOWEST_ALIGNED 0 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 120 | #define DEF_NDELAY 0 /* if > 0 unit is a nanosecond */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 121 | #define DEF_NO_LUN_0 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | #define DEF_NUM_PARTS 0 |
| 123 | #define DEF_OPTS 0 |
Martin K. Petersen | 32c5844 | 2015-12-16 17:53:51 -0500 | [diff] [blame] | 124 | #define DEF_OPT_BLKS 1024 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 125 | #define DEF_PHYSBLK_EXP 0 |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 126 | #define DEF_PTYPE TYPE_DISK |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 127 | #define DEF_REMOVABLE false |
Douglas Gilbert | e46b034 | 2014-08-05 12:21:53 +0200 | [diff] [blame] | 128 | #define DEF_SCSI_LEVEL 6 /* INQUIRY, byte2 [6->SPC-4] */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 129 | #define DEF_SECTOR_SIZE 512 |
| 130 | #define DEF_UNMAP_ALIGNMENT 0 |
| 131 | #define DEF_UNMAP_GRANULARITY 1 |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 132 | #define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF |
| 133 | #define DEF_UNMAP_MAX_DESC 256 |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 134 | #define DEF_VIRTUAL_GB 0 |
| 135 | #define DEF_VPD_USE_HOSTNO 1 |
| 136 | #define DEF_WRITESAME_LENGTH 0xFFFF |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 137 | #define DEF_STRICT 0 |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 138 | #define JDELAY_OVERRIDDEN -9999 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 140 | #define SDEBUG_LUN_0_VAL 0 |
| 141 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 142 | /* bit mask values for sdebug_opts */ |
| 143 | #define SDEBUG_OPT_NOISE 1 |
| 144 | #define SDEBUG_OPT_MEDIUM_ERR 2 |
| 145 | #define SDEBUG_OPT_TIMEOUT 4 |
| 146 | #define SDEBUG_OPT_RECOVERED_ERR 8 |
| 147 | #define SDEBUG_OPT_TRANSPORT_ERR 16 |
| 148 | #define SDEBUG_OPT_DIF_ERR 32 |
| 149 | #define SDEBUG_OPT_DIX_ERR 64 |
| 150 | #define SDEBUG_OPT_MAC_TIMEOUT 128 |
| 151 | #define SDEBUG_OPT_SHORT_TRANSFER 0x100 |
| 152 | #define SDEBUG_OPT_Q_NOISE 0x200 |
| 153 | #define SDEBUG_OPT_ALL_TSF 0x400 |
| 154 | #define SDEBUG_OPT_RARE_TSF 0x800 |
| 155 | #define SDEBUG_OPT_N_WCE 0x1000 |
| 156 | #define SDEBUG_OPT_RESET_NOISE 0x2000 |
| 157 | #define SDEBUG_OPT_NO_CDB_NOISE 0x4000 |
| 158 | #define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \ |
| 159 | SDEBUG_OPT_RESET_NOISE) |
| 160 | #define SDEBUG_OPT_ALL_INJECTING (SDEBUG_OPT_RECOVERED_ERR | \ |
| 161 | SDEBUG_OPT_TRANSPORT_ERR | \ |
| 162 | SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR | \ |
| 163 | SDEBUG_OPT_SHORT_TRANSFER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | /* When "every_nth" > 0 then modulo "every_nth" commands: |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 165 | * - a missing response is simulated if SDEBUG_OPT_TIMEOUT is set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | * - a RECOVERED_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 167 | * commands if SDEBUG_OPT_RECOVERED_ERR is set. |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 168 | * - a TRANSPORT_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 169 | * commands if SDEBUG_OPT_TRANSPORT_ERR is set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | * |
| 171 | * When "every_nth" < 0 then after "- every_nth" commands: |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 172 | * - a missing response is simulated if SDEBUG_OPT_TIMEOUT is set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | * - a RECOVERED_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 174 | * commands if SDEBUG_OPT_RECOVERED_ERR is set. |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 175 | * - a TRANSPORT_ERROR is simulated on successful read and write |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 176 | * commands if _DEBUG_OPT_TRANSPORT_ERR is set. |
| 177 | * This will continue on every subsequent command until some other action |
| 178 | * occurs (e.g. the user * writing a new value (other than -1 or 1) to |
| 179 | * every_nth via sysfs). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | */ |
| 181 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 182 | /* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs) are returned in |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 183 | * priority order. In the subset implemented here lower numbers have higher |
| 184 | * priority. The UA numbers should be a sequence starting from 0 with |
| 185 | * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */ |
| 186 | #define SDEBUG_UA_POR 0 /* Power on, reset, or bus device reset */ |
| 187 | #define SDEBUG_UA_BUS_RESET 1 |
| 188 | #define SDEBUG_UA_MODE_CHANGED 2 |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 189 | #define SDEBUG_UA_CAPACITY_CHANGED 3 |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 190 | #define SDEBUG_UA_LUNS_CHANGED 4 |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 191 | #define SDEBUG_UA_MICROCODE_CHANGED 5 /* simulate firmware change */ |
| 192 | #define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 6 |
| 193 | #define SDEBUG_NUM_UAS 7 |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 194 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 195 | /* 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] | 196 | * sector on read commands: */ |
| 197 | #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */ |
Douglas Gilbert | 32f7ef7 | 2011-03-11 10:43:35 -0500 | [diff] [blame] | 198 | #define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1) |
| 201 | * or "peripheral device" addressing (value 0) */ |
| 202 | #define SAM2_LUN_ADDRESS_METHOD 0 |
| 203 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 204 | /* SCSI_DEBUG_CANQUEUE is the maximum number of commands that can be queued |
| 205 | * (for response) at one time. Can be reduced by max_queue option. Command |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 206 | * responses are not queued when jdelay=0 and ndelay=0. The per-device |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 207 | * DEF_CMD_PER_LUN can be changed via sysfs: |
| 208 | * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth but cannot exceed |
| 209 | * SCSI_DEBUG_CANQUEUE. */ |
| 210 | #define SCSI_DEBUG_CANQUEUE_WORDS 9 /* a WORD is bits in a long */ |
| 211 | #define SCSI_DEBUG_CANQUEUE (SCSI_DEBUG_CANQUEUE_WORDS * BITS_PER_LONG) |
| 212 | #define DEF_CMD_PER_LUN 255 |
| 213 | |
| 214 | #if DEF_CMD_PER_LUN > SCSI_DEBUG_CANQUEUE |
| 215 | #warning "Expect DEF_CMD_PER_LUN <= SCSI_DEBUG_CANQUEUE" |
| 216 | #endif |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 217 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 218 | #define F_D_IN 1 |
| 219 | #define F_D_OUT 2 |
| 220 | #define F_D_OUT_MAYBE 4 /* WRITE SAME, NDOB bit */ |
| 221 | #define F_D_UNKN 8 |
| 222 | #define F_RL_WLUN_OK 0x10 |
| 223 | #define F_SKIP_UA 0x20 |
| 224 | #define F_DELAY_OVERR 0x40 |
| 225 | #define F_SA_LOW 0x80 /* cdb byte 1, bits 4 to 0 */ |
| 226 | #define F_SA_HIGH 0x100 /* as used by variable length cdbs */ |
| 227 | #define F_INV_OP 0x200 |
| 228 | #define F_FAKE_RW 0x400 |
| 229 | #define F_M_ACCESS 0x800 /* media access */ |
| 230 | |
| 231 | #define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR) |
| 232 | #define FF_DIRECT_IO (F_M_ACCESS | F_FAKE_RW) |
| 233 | #define FF_SA (F_SA_HIGH | F_SA_LOW) |
| 234 | |
| 235 | #define SDEBUG_MAX_PARTS 4 |
| 236 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 237 | #define SDEBUG_MAX_CMD_LEN 32 |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 238 | |
| 239 | |
| 240 | struct sdebug_dev_info { |
| 241 | struct list_head dev_list; |
| 242 | unsigned int channel; |
| 243 | unsigned int target; |
| 244 | u64 lun; |
| 245 | struct sdebug_host_info *sdbg_host; |
| 246 | unsigned long uas_bm[1]; |
| 247 | atomic_t num_in_q; |
| 248 | char stopped; /* TODO: should be atomic */ |
| 249 | bool used; |
| 250 | }; |
| 251 | |
| 252 | struct sdebug_host_info { |
| 253 | struct list_head host_list; |
| 254 | struct Scsi_Host *shost; |
| 255 | struct device dev; |
| 256 | struct list_head dev_info_list; |
| 257 | }; |
| 258 | |
| 259 | #define to_sdebug_host(d) \ |
| 260 | container_of(d, struct sdebug_host_info, dev) |
| 261 | |
| 262 | struct sdebug_defer { |
| 263 | struct hrtimer hrt; |
| 264 | struct execute_work ew; |
| 265 | int qa_indx; |
| 266 | }; |
| 267 | |
| 268 | struct sdebug_queued_cmd { |
| 269 | /* in_use flagged by a bit in queued_in_use_bm[] */ |
| 270 | struct sdebug_defer *sd_dp; |
| 271 | struct scsi_cmnd *a_cmnd; |
| 272 | }; |
| 273 | |
| 274 | struct sdebug_scmd_extra_t { |
| 275 | bool inj_recovered; |
| 276 | bool inj_transport; |
| 277 | bool inj_dif; |
| 278 | bool inj_dix; |
| 279 | bool inj_short; |
| 280 | }; |
| 281 | |
| 282 | struct opcode_info_t { |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 283 | u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff */ |
| 284 | /* for terminating element */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 285 | u8 opcode; /* if num_attached > 0, preferred */ |
| 286 | u16 sa; /* service action */ |
| 287 | u32 flags; /* OR-ed set of SDEB_F_* */ |
| 288 | int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 289 | const struct opcode_info_t *arrp; /* num_attached elements or NULL */ |
| 290 | u8 len_mask[16]; /* len=len_mask[0], then mask for cdb[1]... */ |
| 291 | /* ignore cdb bytes after position 15 */ |
| 292 | }; |
| 293 | |
| 294 | /* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 295 | enum sdeb_opcode_index { |
| 296 | SDEB_I_INVALID_OPCODE = 0, |
| 297 | SDEB_I_INQUIRY = 1, |
| 298 | SDEB_I_REPORT_LUNS = 2, |
| 299 | SDEB_I_REQUEST_SENSE = 3, |
| 300 | SDEB_I_TEST_UNIT_READY = 4, |
| 301 | SDEB_I_MODE_SENSE = 5, /* 6, 10 */ |
| 302 | SDEB_I_MODE_SELECT = 6, /* 6, 10 */ |
| 303 | SDEB_I_LOG_SENSE = 7, |
| 304 | SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */ |
| 305 | SDEB_I_READ = 9, /* 6, 10, 12, 16 */ |
| 306 | SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */ |
| 307 | SDEB_I_START_STOP = 11, |
| 308 | SDEB_I_SERV_ACT_IN = 12, /* 12, 16 */ |
| 309 | SDEB_I_SERV_ACT_OUT = 13, /* 12, 16 */ |
| 310 | SDEB_I_MAINT_IN = 14, |
| 311 | SDEB_I_MAINT_OUT = 15, |
| 312 | SDEB_I_VERIFY = 16, /* 10 only */ |
| 313 | SDEB_I_VARIABLE_LEN = 17, |
| 314 | SDEB_I_RESERVE = 18, /* 6, 10 */ |
| 315 | SDEB_I_RELEASE = 19, /* 6, 10 */ |
| 316 | SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */ |
| 317 | SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */ |
| 318 | SDEB_I_ATA_PT = 22, /* 12, 16 */ |
| 319 | SDEB_I_SEND_DIAG = 23, |
| 320 | SDEB_I_UNMAP = 24, |
| 321 | SDEB_I_XDWRITEREAD = 25, /* 10 only */ |
| 322 | SDEB_I_WRITE_BUFFER = 26, |
| 323 | SDEB_I_WRITE_SAME = 27, /* 10, 16 */ |
| 324 | SDEB_I_SYNC_CACHE = 28, /* 10 only */ |
| 325 | SDEB_I_COMP_WRITE = 29, |
| 326 | SDEB_I_LAST_ELEMENT = 30, /* keep this last */ |
| 327 | }; |
| 328 | |
| 329 | static const unsigned char opcode_ind_arr[256] = { |
| 330 | /* 0x0; 0x0->0x1f: 6 byte cdbs */ |
| 331 | SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE, |
| 332 | 0, 0, 0, 0, |
| 333 | SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0, |
| 334 | 0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE, |
| 335 | SDEB_I_RELEASE, |
| 336 | 0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG, |
| 337 | SDEB_I_ALLOW_REMOVAL, 0, |
| 338 | /* 0x20; 0x20->0x3f: 10 byte cdbs */ |
| 339 | 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0, |
| 340 | SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY, |
| 341 | 0, 0, 0, 0, 0, SDEB_I_SYNC_CACHE, 0, 0, |
| 342 | 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0, |
| 343 | /* 0x40; 0x40->0x5f: 10 byte cdbs */ |
| 344 | 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0, |
| 345 | 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0, |
| 346 | 0, 0, 0, SDEB_I_XDWRITEREAD, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE, |
| 347 | SDEB_I_RELEASE, |
| 348 | 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0, |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 349 | /* 0x60; 0x60->0x7d are reserved, 0x7e is "extended cdb" */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 350 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 351 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 352 | 0, SDEB_I_VARIABLE_LEN, |
| 353 | /* 0x80; 0x80->0x9f: 16 byte cdbs */ |
| 354 | 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0, |
| 355 | SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0, 0, 0, 0, 0, |
| 356 | 0, 0, 0, SDEB_I_WRITE_SAME, 0, 0, 0, 0, |
| 357 | 0, 0, 0, 0, 0, 0, SDEB_I_SERV_ACT_IN, SDEB_I_SERV_ACT_OUT, |
| 358 | /* 0xa0; 0xa0->0xbf: 12 byte cdbs */ |
| 359 | SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN, |
| 360 | SDEB_I_MAINT_OUT, 0, 0, 0, |
| 361 | SDEB_I_READ, SDEB_I_SERV_ACT_OUT, SDEB_I_WRITE, SDEB_I_SERV_ACT_IN, |
| 362 | 0, 0, 0, 0, |
| 363 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 364 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 365 | /* 0xc0; 0xc0->0xff: vendor specific */ |
| 366 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 367 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 368 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 369 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 370 | }; |
| 371 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 372 | static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 373 | static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 374 | static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 375 | static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 376 | static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 377 | static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 378 | static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 379 | static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 380 | static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 381 | static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 382 | static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 383 | static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 384 | static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 385 | static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 386 | static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 387 | static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 388 | static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 389 | static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 390 | static int resp_xdwriteread_10(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 391 | 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] | 392 | static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 393 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 394 | static const struct opcode_info_t msense_iarr[1] = { |
| 395 | {0, 0x1a, 0, F_D_IN, NULL, NULL, |
| 396 | {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 397 | }; |
| 398 | |
| 399 | static const struct opcode_info_t mselect_iarr[1] = { |
| 400 | {0, 0x15, 0, F_D_OUT, NULL, NULL, |
| 401 | {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 402 | }; |
| 403 | |
| 404 | static const struct opcode_info_t read_iarr[3] = { |
| 405 | {0, 0x28, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL,/* READ(10) */ |
| 406 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0, |
| 407 | 0, 0, 0, 0} }, |
| 408 | {0, 0x8, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL, /* READ(6) */ |
| 409 | {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 410 | {0, 0xa8, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, NULL,/* READ(12) */ |
| 411 | {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f, |
| 412 | 0xc7, 0, 0, 0, 0} }, |
| 413 | }; |
| 414 | |
| 415 | static const struct opcode_info_t write_iarr[3] = { |
| 416 | {0, 0x2a, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 10 */ |
| 417 | {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0, |
| 418 | 0, 0, 0, 0} }, |
| 419 | {0, 0xa, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 6 */ |
| 420 | {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 421 | {0, 0xaa, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, NULL, /* 12 */ |
| 422 | {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f, |
| 423 | 0xc7, 0, 0, 0, 0} }, |
| 424 | }; |
| 425 | |
| 426 | static const struct opcode_info_t sa_in_iarr[1] = { |
| 427 | {0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL, |
| 428 | {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 429 | 0xff, 0xff, 0xff, 0, 0xc7} }, |
| 430 | }; |
| 431 | |
| 432 | static const struct opcode_info_t vl_iarr[1] = { /* VARIABLE LENGTH */ |
| 433 | {0, 0x7f, 0xb, F_SA_HIGH | F_D_OUT | FF_DIRECT_IO, resp_write_dt0, |
| 434 | NULL, {32, 0xc7, 0, 0, 0, 0, 0x1f, 0x18, 0x0, 0xb, 0xfa, |
| 435 | 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */ |
| 436 | }; |
| 437 | |
| 438 | static const struct opcode_info_t maint_in_iarr[2] = { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 439 | {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] | 440 | {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, |
| 441 | 0xc7, 0, 0, 0, 0} }, |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 442 | {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] | 443 | {12, 0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, |
| 444 | 0, 0} }, |
| 445 | }; |
| 446 | |
| 447 | static const struct opcode_info_t write_same_iarr[1] = { |
| 448 | {0, 0x93, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, resp_write_same_16, NULL, |
| 449 | {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 450 | 0xff, 0xff, 0xff, 0x1f, 0xc7} }, |
| 451 | }; |
| 452 | |
| 453 | static const struct opcode_info_t reserve_iarr[1] = { |
| 454 | {0, 0x16, 0, F_D_OUT, NULL, NULL, /* RESERVE(6) */ |
| 455 | {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 456 | }; |
| 457 | |
| 458 | static const struct opcode_info_t release_iarr[1] = { |
| 459 | {0, 0x17, 0, F_D_OUT, NULL, NULL, /* RELEASE(6) */ |
| 460 | {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 461 | }; |
| 462 | |
| 463 | |
| 464 | /* This array is accessed via SDEB_I_* values. Make sure all are mapped, |
| 465 | * plus the terminating elements for logic that scans this table such as |
| 466 | * REPORT SUPPORTED OPERATION CODES. */ |
| 467 | static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEMENT + 1] = { |
| 468 | /* 0 */ |
| 469 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, |
| 470 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 471 | {0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL, |
| 472 | {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 473 | {0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL, |
| 474 | {12, 0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, |
| 475 | 0, 0} }, |
| 476 | {0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL, |
| 477 | {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 478 | {0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */ |
| 479 | {6, 0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 480 | {1, 0x5a, 0, F_D_IN, resp_mode_sense, msense_iarr, |
| 481 | {10, 0xf8, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 482 | 0} }, |
| 483 | {1, 0x55, 0, F_D_OUT, resp_mode_select, mselect_iarr, |
| 484 | {10, 0xf1, 0, 0, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 485 | {0, 0x4d, 0, F_D_IN, resp_log_sense, NULL, |
| 486 | {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, |
| 487 | 0, 0, 0} }, |
| 488 | {0, 0x25, 0, F_D_IN, resp_readcap, NULL, |
| 489 | {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0, |
| 490 | 0, 0} }, |
| 491 | {3, 0x88, 0, F_D_IN | FF_DIRECT_IO, resp_read_dt0, read_iarr, |
| 492 | {16, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 493 | 0xff, 0xff, 0xff, 0x9f, 0xc7} }, /* READ(16) */ |
| 494 | /* 10 */ |
| 495 | {3, 0x8a, 0, F_D_OUT | FF_DIRECT_IO, resp_write_dt0, write_iarr, |
| 496 | {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 497 | 0xff, 0xff, 0xff, 0x9f, 0xc7} }, /* WRITE(16) */ |
| 498 | {0, 0x1b, 0, 0, resp_start_stop, NULL, /* START STOP UNIT */ |
| 499 | {6, 0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 500 | {1, 0x9e, 0x10, F_SA_LOW | F_D_IN, resp_readcap16, sa_in_iarr, |
| 501 | {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 502 | 0xff, 0xff, 0xff, 0x1, 0xc7} }, /* READ CAPACITY(16) */ |
| 503 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* SA OUT */ |
| 504 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 505 | {2, 0xa3, 0xa, F_SA_LOW | F_D_IN, resp_report_tgtpgs, maint_in_iarr, |
| 506 | {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0, 0, |
| 507 | 0} }, |
| 508 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */ |
| 509 | {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] | 510 | {0, 0x2f, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, NULL, NULL, /* VERIFY(10) */ |
| 511 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, |
| 512 | 0, 0, 0, 0, 0, 0} }, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 513 | {1, 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_DIRECT_IO, resp_read_dt0, |
| 514 | vl_iarr, {32, 0xc7, 0, 0, 0, 0, 0x1f, 0x18, 0x0, 0x9, 0xfe, 0, |
| 515 | 0xff, 0xff, 0xff, 0xff} },/* VARIABLE LENGTH, READ(32) */ |
| 516 | {1, 0x56, 0, F_D_OUT, NULL, reserve_iarr, /* RESERVE(10) */ |
| 517 | {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 518 | 0} }, |
| 519 | {1, 0x57, 0, F_D_OUT, NULL, release_iarr, /* RELEASE(10) */ |
| 520 | {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, |
| 521 | 0} }, |
| 522 | /* 20 */ |
Douglas Gilbert | f7f9f26 | 2015-11-22 12:11:28 -0500 | [diff] [blame] | 523 | {0, 0x1e, 0, 0, NULL, NULL, /* ALLOW REMOVAL */ |
| 524 | {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] | 525 | {0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */ |
| 526 | {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 527 | {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */ |
| 528 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 529 | {0, 0x1d, F_D_OUT, 0, NULL, NULL, /* SEND DIAGNOSTIC */ |
| 530 | {6, 0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 531 | {0, 0x42, 0, F_D_OUT | FF_DIRECT_IO, resp_unmap, NULL, /* UNMAP */ |
| 532 | {10, 0x1, 0, 0, 0, 0, 0x1f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 533 | {0, 0x53, 0, F_D_IN | F_D_OUT | FF_DIRECT_IO, resp_xdwriteread_10, |
| 534 | NULL, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, |
| 535 | 0, 0, 0, 0, 0, 0} }, |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 536 | {0, 0x3b, 0, F_D_OUT_MAYBE, resp_write_buffer, NULL, |
| 537 | {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, |
| 538 | 0, 0, 0, 0} }, /* WRITE_BUFFER */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 539 | {1, 0x41, 0, F_D_OUT_MAYBE | FF_DIRECT_IO, resp_write_same_10, |
| 540 | write_same_iarr, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, |
| 541 | 0xff, 0xc7, 0, 0, 0, 0, 0, 0} }, |
| 542 | {0, 0x35, 0, F_DELAY_OVERR | FF_DIRECT_IO, NULL, NULL, /* SYNC_CACHE */ |
| 543 | {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc7, 0, 0, |
| 544 | 0, 0, 0, 0} }, |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 545 | {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] | 546 | {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, |
| 547 | 0, 0xff, 0x1f, 0xc7} }, /* COMPARE AND WRITE */ |
| 548 | |
| 549 | /* 30 */ |
| 550 | {0xff, 0, 0, 0, NULL, NULL, /* terminating element */ |
| 551 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, |
| 552 | }; |
| 553 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 554 | static int sdebug_add_host = DEF_NUM_HOST; |
| 555 | static int sdebug_ato = DEF_ATO; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 556 | static int sdebug_jdelay = DEF_JDELAY; /* if > 0 then unit is jiffies */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 557 | static int sdebug_dev_size_mb = DEF_DEV_SIZE_MB; |
| 558 | static int sdebug_dif = DEF_DIF; |
| 559 | static int sdebug_dix = DEF_DIX; |
| 560 | static int sdebug_dsense = DEF_D_SENSE; |
| 561 | static int sdebug_every_nth = DEF_EVERY_NTH; |
| 562 | static int sdebug_fake_rw = DEF_FAKE_RW; |
| 563 | static unsigned int sdebug_guard = DEF_GUARD; |
| 564 | static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED; |
| 565 | static int sdebug_max_luns = DEF_MAX_LUNS; |
| 566 | static int sdebug_max_queue = SCSI_DEBUG_CANQUEUE; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 567 | static atomic_t retired_max_queue; /* if > 0 then was prior max_queue */ |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 568 | static int sdebug_ndelay = DEF_NDELAY; /* if > 0 then unit is nanoseconds */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 569 | static int sdebug_no_lun_0 = DEF_NO_LUN_0; |
| 570 | static int sdebug_no_uld; |
| 571 | static int sdebug_num_parts = DEF_NUM_PARTS; |
| 572 | static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */ |
| 573 | static int sdebug_opt_blks = DEF_OPT_BLKS; |
| 574 | static int sdebug_opts = DEF_OPTS; |
| 575 | static int sdebug_physblk_exp = DEF_PHYSBLK_EXP; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 576 | static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral device type */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 577 | static int sdebug_scsi_level = DEF_SCSI_LEVEL; |
| 578 | static int sdebug_sector_size = DEF_SECTOR_SIZE; |
| 579 | static int sdebug_virtual_gb = DEF_VIRTUAL_GB; |
| 580 | static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO; |
| 581 | static unsigned int sdebug_lbpu = DEF_LBPU; |
| 582 | static unsigned int sdebug_lbpws = DEF_LBPWS; |
| 583 | static unsigned int sdebug_lbpws10 = DEF_LBPWS10; |
| 584 | static unsigned int sdebug_lbprz = DEF_LBPRZ; |
| 585 | static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT; |
| 586 | static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY; |
| 587 | static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS; |
| 588 | static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC; |
| 589 | static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH; |
| 590 | static bool sdebug_removable = DEF_REMOVABLE; |
| 591 | static bool sdebug_clustering; |
| 592 | static bool sdebug_host_lock = DEF_HOST_LOCK; |
| 593 | static bool sdebug_strict = DEF_STRICT; |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 594 | static bool sdebug_any_injecting_opt; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 595 | static bool sdebug_verbose; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 596 | static bool have_dif_prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 598 | static atomic_t sdebug_cmnd_count; |
| 599 | static atomic_t sdebug_completions; |
| 600 | static atomic_t sdebug_a_tsf; /* counter of 'almost' TSFs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 602 | static unsigned int sdebug_store_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | static sector_t sdebug_capacity; /* in sectors */ |
| 604 | |
| 605 | /* old BIOS stuff, kernel may get rid of them but some mode sense pages |
| 606 | may still need them */ |
| 607 | static int sdebug_heads; /* heads per disk */ |
| 608 | static int sdebug_cylinders_per; /* cylinders per surface */ |
| 609 | static int sdebug_sectors_per; /* sectors per cylinder */ |
| 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | static LIST_HEAD(sdebug_host_list); |
| 612 | static DEFINE_SPINLOCK(sdebug_host_list_lock); |
| 613 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 614 | static unsigned char *fake_storep; /* ramdisk storage */ |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 615 | static struct sd_dif_tuple *dif_storep; /* protection info */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 616 | static void *map_storep; /* provisioning map */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 618 | static unsigned long map_size; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 619 | static int num_aborts; |
| 620 | static int num_dev_resets; |
| 621 | static int num_target_resets; |
| 622 | static int num_bus_resets; |
| 623 | static int num_host_resets; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 624 | static int dix_writes; |
| 625 | static int dix_reads; |
| 626 | static int dif_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 628 | static struct sdebug_queued_cmd queued_arr[SCSI_DEBUG_CANQUEUE]; |
| 629 | static unsigned long queued_in_use_bm[SCSI_DEBUG_CANQUEUE_WORDS]; |
| 630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | static DEFINE_SPINLOCK(queued_arr_lock); |
| 632 | static DEFINE_RWLOCK(atomic_rw); |
| 633 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 634 | static char sdebug_proc_name[] = MY_NAME; |
| 635 | static const char *my_name = MY_NAME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | static struct bus_type pseudo_lld_bus; |
| 638 | |
| 639 | static struct device_driver sdebug_driverfs_driver = { |
| 640 | .name = sdebug_proc_name, |
| 641 | .bus = &pseudo_lld_bus, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | }; |
| 643 | |
| 644 | static const int check_condition_result = |
| 645 | (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
| 646 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 647 | static const int illegal_condition_result = |
| 648 | (DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION; |
| 649 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 650 | static const int device_qfull_result = |
| 651 | (DID_OK << 16) | (COMMAND_COMPLETE << 8) | SAM_STAT_TASK_SET_FULL; |
| 652 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 653 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 654 | static inline unsigned int scsi_debug_lbp(void) |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 655 | { |
| 656 | return 0 == sdebug_fake_rw && |
| 657 | (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10); |
| 658 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 659 | |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 660 | static void *fake_store(unsigned long long lba) |
| 661 | { |
| 662 | lba = do_div(lba, sdebug_store_sectors); |
| 663 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 664 | return fake_storep + lba * sdebug_sector_size; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static struct sd_dif_tuple *dif_store(sector_t sector) |
| 668 | { |
Arnd Bergmann | 4941311 | 2015-11-20 17:38:28 +0100 | [diff] [blame] | 669 | sector = sector_div(sector, sdebug_store_sectors); |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 670 | |
| 671 | return dif_storep + sector; |
| 672 | } |
| 673 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 674 | static void sdebug_max_tgts_luns(void) |
| 675 | { |
| 676 | struct sdebug_host_info *sdbg_host; |
| 677 | struct Scsi_Host *hpnt; |
| 678 | |
| 679 | spin_lock(&sdebug_host_list_lock); |
| 680 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
| 681 | hpnt = sdbg_host->shost; |
| 682 | if ((hpnt->this_id >= 0) && |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 683 | (sdebug_num_tgts > hpnt->this_id)) |
| 684 | hpnt->max_id = sdebug_num_tgts + 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 685 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 686 | hpnt->max_id = sdebug_num_tgts; |
| 687 | /* sdebug_max_luns; */ |
Tomas Winkler | f2d3fd2 | 2015-07-28 16:54:25 +0300 | [diff] [blame] | 688 | hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 689 | } |
| 690 | spin_unlock(&sdebug_host_list_lock); |
| 691 | } |
| 692 | |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 693 | enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1}; |
| 694 | |
| 695 | /* Set in_bit to -1 to indicate no bit position of invalid field */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 696 | static void mk_sense_invalid_fld(struct scsi_cmnd *scp, |
| 697 | enum sdeb_cmd_data c_d, |
| 698 | int in_byte, int in_bit) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 699 | { |
| 700 | unsigned char *sbuff; |
| 701 | u8 sks[4]; |
| 702 | int sl, asc; |
| 703 | |
| 704 | sbuff = scp->sense_buffer; |
| 705 | if (!sbuff) { |
| 706 | sdev_printk(KERN_ERR, scp->device, |
| 707 | "%s: sense_buffer is NULL\n", __func__); |
| 708 | return; |
| 709 | } |
| 710 | asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST; |
| 711 | memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 712 | scsi_build_sense_buffer(sdebug_dsense, sbuff, ILLEGAL_REQUEST, asc, 0); |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 713 | memset(sks, 0, sizeof(sks)); |
| 714 | sks[0] = 0x80; |
| 715 | if (c_d) |
| 716 | sks[0] |= 0x40; |
| 717 | if (in_bit >= 0) { |
| 718 | sks[0] |= 0x8; |
| 719 | sks[0] |= 0x7 & in_bit; |
| 720 | } |
| 721 | put_unaligned_be16(in_byte, sks + 1); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 722 | if (sdebug_dsense) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 723 | sl = sbuff[7] + 8; |
| 724 | sbuff[7] = sl; |
| 725 | sbuff[sl] = 0x2; |
| 726 | sbuff[sl + 1] = 0x6; |
| 727 | memcpy(sbuff + sl + 4, sks, 3); |
| 728 | } else |
| 729 | memcpy(sbuff + 15, sks, 3); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 730 | if (sdebug_verbose) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 731 | sdev_printk(KERN_INFO, scp->device, "%s: [sense_key,asc,ascq" |
| 732 | "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n", |
| 733 | my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit); |
| 734 | } |
| 735 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 736 | 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] | 737 | { |
| 738 | unsigned char *sbuff; |
| 739 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 740 | sbuff = scp->sense_buffer; |
| 741 | if (!sbuff) { |
| 742 | sdev_printk(KERN_ERR, scp->device, |
| 743 | "%s: sense_buffer is NULL\n", __func__); |
| 744 | return; |
| 745 | } |
| 746 | memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 747 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 748 | scsi_build_sense_buffer(sdebug_dsense, sbuff, key, asc, asq); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 749 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 750 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 751 | sdev_printk(KERN_INFO, scp->device, |
| 752 | "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n", |
| 753 | my_name, key, asc, asq); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 754 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 756 | static void mk_sense_invalid_opcode(struct scsi_cmnd *scp) |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 757 | { |
| 758 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0); |
| 759 | } |
| 760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg) |
| 762 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 763 | if (sdebug_verbose) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 764 | if (0x1261 == cmd) |
| 765 | sdev_printk(KERN_INFO, dev, |
| 766 | "%s: BLKFLSBUF [0x1261]\n", __func__); |
| 767 | else if (0x5331 == cmd) |
| 768 | sdev_printk(KERN_INFO, dev, |
| 769 | "%s: CDROM_GET_CAPABILITY [0x5331]\n", |
| 770 | __func__); |
| 771 | else |
| 772 | sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n", |
| 773 | __func__, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
| 775 | return -EINVAL; |
| 776 | /* return -ENOTTY; // correct return but upsets fdisk */ |
| 777 | } |
| 778 | |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 779 | static void clear_luns_changed_on_target(struct sdebug_dev_info *devip) |
| 780 | { |
| 781 | struct sdebug_host_info *sdhp; |
| 782 | struct sdebug_dev_info *dp; |
| 783 | |
| 784 | spin_lock(&sdebug_host_list_lock); |
| 785 | list_for_each_entry(sdhp, &sdebug_host_list, host_list) { |
| 786 | list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) { |
| 787 | if ((devip->sdbg_host == dp->sdbg_host) && |
| 788 | (devip->target == dp->target)) |
| 789 | clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm); |
| 790 | } |
| 791 | } |
| 792 | spin_unlock(&sdebug_host_list_lock); |
| 793 | } |
| 794 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 795 | static int make_ua(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 797 | int k; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 798 | |
| 799 | k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS); |
| 800 | if (k != SDEBUG_NUM_UAS) { |
| 801 | const char *cp = NULL; |
| 802 | |
| 803 | switch (k) { |
| 804 | case SDEBUG_UA_POR: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 805 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC, |
| 806 | POWER_ON_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 807 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 808 | cp = "power on reset"; |
| 809 | break; |
| 810 | case SDEBUG_UA_BUS_RESET: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 811 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC, |
| 812 | BUS_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 813 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 814 | cp = "bus reset"; |
| 815 | break; |
| 816 | case SDEBUG_UA_MODE_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 817 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC, |
| 818 | MODE_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 819 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 820 | cp = "mode parameters changed"; |
| 821 | break; |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 822 | case SDEBUG_UA_CAPACITY_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 823 | mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC, |
| 824 | CAPACITY_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 825 | if (sdebug_verbose) |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 826 | cp = "capacity data changed"; |
Ewan D. Milne | f49accf | 2014-12-04 11:49:25 -0500 | [diff] [blame] | 827 | break; |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 828 | case SDEBUG_UA_MICROCODE_CHANGED: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 829 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 830 | TARGET_CHANGED_ASC, |
| 831 | MICROCODE_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 832 | if (sdebug_verbose) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 833 | cp = "microcode has been changed"; |
| 834 | break; |
| 835 | case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 836 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 837 | TARGET_CHANGED_ASC, |
| 838 | MICROCODE_CHANGED_WO_RESET_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 839 | if (sdebug_verbose) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 840 | cp = "microcode has been changed without reset"; |
| 841 | break; |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 842 | case SDEBUG_UA_LUNS_CHANGED: |
| 843 | /* |
| 844 | * SPC-3 behavior is to report a UNIT ATTENTION with |
| 845 | * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN |
| 846 | * on the target, until a REPORT LUNS command is |
| 847 | * received. SPC-4 behavior is to report it only once. |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 848 | * NOTE: sdebug_scsi_level does not use the same |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 849 | * values as struct scsi_device->scsi_level. |
| 850 | */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 851 | if (sdebug_scsi_level >= 6) /* SPC-4 and above */ |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 852 | clear_luns_changed_on_target(devip); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 853 | mk_sense_buffer(scp, UNIT_ATTENTION, |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 854 | TARGET_CHANGED_ASC, |
| 855 | LUNS_CHANGED_ASCQ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 856 | if (sdebug_verbose) |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 857 | cp = "reported luns data has changed"; |
| 858 | break; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 859 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 860 | pr_warn("unexpected unit attention code=%d\n", k); |
| 861 | if (sdebug_verbose) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 862 | cp = "unknown"; |
| 863 | break; |
| 864 | } |
| 865 | clear_bit(k, devip->uas_bm); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 866 | if (sdebug_verbose) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 867 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 868 | "%s reports: Unit attention: %s\n", |
| 869 | my_name, cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | return check_condition_result; |
| 871 | } |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | /* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */ |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 876 | 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] | 877 | int arr_len) |
| 878 | { |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 879 | int act_len; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 880 | struct scsi_data_buffer *sdb = scsi_in(scp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 882 | if (!sdb->length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | return 0; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 884 | if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE)) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 885 | return DID_ERROR << 16; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 886 | |
| 887 | act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents, |
| 888 | arr, arr_len); |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 889 | sdb->resid = scsi_bufflen(scp) - act_len; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | /* Returns number of bytes fetched into 'arr' or -1 if error. */ |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 895 | static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr, |
| 896 | int arr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | { |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 898 | if (!scsi_bufflen(scp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | return 0; |
FUJITA Tomonori | 072d0bb | 2008-01-23 01:32:00 +0900 | [diff] [blame] | 900 | if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_TO_DEVICE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | return -1; |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 902 | |
| 903 | return scsi_sg_copy_to_buffer(scp, arr, arr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | |
| 907 | static const char * inq_vendor_id = "Linux "; |
| 908 | static const char * inq_product_id = "scsi_debug "; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 909 | static const char *inq_product_rev = "0186"; /* version less '.' */ |
| 910 | static const u64 naa5_comp_a = 0x5222222000000000ULL; |
| 911 | static const u64 naa5_comp_b = 0x5333333000000000ULL; |
| 912 | static const u64 naa5_comp_c = 0x5111111000000000ULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 914 | /* Device identification VPD page. Returns number of bytes placed in arr */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 915 | static int inquiry_evpd_83(unsigned char * arr, int port_group_id, |
| 916 | int target_dev_id, int dev_id_num, |
| 917 | const char * dev_id_str, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 918 | int dev_id_str_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 920 | int num, port_a; |
| 921 | char b[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 923 | port_a = target_dev_id + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | /* T10 vendor identifier field format (faked) */ |
| 925 | arr[0] = 0x2; /* ASCII */ |
| 926 | arr[1] = 0x1; |
| 927 | arr[2] = 0x0; |
| 928 | memcpy(&arr[4], inq_vendor_id, 8); |
| 929 | memcpy(&arr[12], inq_product_id, 16); |
| 930 | memcpy(&arr[28], dev_id_str, dev_id_str_len); |
| 931 | num = 8 + 16 + dev_id_str_len; |
| 932 | arr[3] = num; |
| 933 | num += 4; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 934 | if (dev_id_num >= 0) { |
| 935 | /* NAA-5, Logical unit identifier (binary) */ |
| 936 | arr[num++] = 0x1; /* binary (not necessarily sas) */ |
| 937 | arr[num++] = 0x3; /* PIV=0, lu, naa */ |
| 938 | arr[num++] = 0x0; |
| 939 | arr[num++] = 0x8; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 940 | put_unaligned_be64(naa5_comp_b + dev_id_num, arr + num); |
| 941 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 942 | /* Target relative port number */ |
| 943 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 944 | arr[num++] = 0x94; /* PIV=1, target port, rel port */ |
| 945 | arr[num++] = 0x0; /* reserved */ |
| 946 | arr[num++] = 0x4; /* length */ |
| 947 | arr[num++] = 0x0; /* reserved */ |
| 948 | arr[num++] = 0x0; /* reserved */ |
| 949 | arr[num++] = 0x0; |
| 950 | arr[num++] = 0x1; /* relative port A */ |
| 951 | } |
| 952 | /* NAA-5, Target port identifier */ |
| 953 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 954 | arr[num++] = 0x93; /* piv=1, target port, naa */ |
| 955 | arr[num++] = 0x0; |
| 956 | arr[num++] = 0x8; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 957 | put_unaligned_be64(naa5_comp_a + port_a, arr + num); |
| 958 | num += 8; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 959 | /* NAA-5, Target port group identifier */ |
| 960 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 961 | arr[num++] = 0x95; /* piv=1, target port group id */ |
| 962 | arr[num++] = 0x0; |
| 963 | arr[num++] = 0x4; |
| 964 | arr[num++] = 0; |
| 965 | arr[num++] = 0; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 966 | put_unaligned_be16(port_group_id, arr + num); |
| 967 | num += 2; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 968 | /* NAA-5, Target device identifier */ |
| 969 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 970 | arr[num++] = 0xa3; /* piv=1, target device, naa */ |
| 971 | arr[num++] = 0x0; |
| 972 | arr[num++] = 0x8; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 973 | put_unaligned_be64(naa5_comp_a + target_dev_id, arr + num); |
| 974 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 975 | /* SCSI name string: Target device identifier */ |
| 976 | arr[num++] = 0x63; /* proto=sas, UTF-8 */ |
| 977 | arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */ |
| 978 | arr[num++] = 0x0; |
| 979 | arr[num++] = 24; |
| 980 | memcpy(arr + num, "naa.52222220", 12); |
| 981 | num += 12; |
| 982 | snprintf(b, sizeof(b), "%08X", target_dev_id); |
| 983 | memcpy(arr + num, b, 8); |
| 984 | num += 8; |
| 985 | memset(arr + num, 0, 4); |
| 986 | num += 4; |
| 987 | return num; |
| 988 | } |
| 989 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 990 | static unsigned char vpd84_data[] = { |
| 991 | /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0, |
| 992 | 0x22,0x22,0x22,0x0,0xbb,0x1, |
| 993 | 0x22,0x22,0x22,0x0,0xbb,0x2, |
| 994 | }; |
| 995 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 996 | /* Software interface identification VPD page */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 997 | static int inquiry_evpd_84(unsigned char * arr) |
| 998 | { |
| 999 | memcpy(arr, vpd84_data, sizeof(vpd84_data)); |
| 1000 | return sizeof(vpd84_data); |
| 1001 | } |
| 1002 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1003 | /* Management network addresses VPD page */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1004 | static int inquiry_evpd_85(unsigned char * arr) |
| 1005 | { |
| 1006 | int num = 0; |
| 1007 | const char * na1 = "https://www.kernel.org/config"; |
| 1008 | const char * na2 = "http://www.kernel.org/log"; |
| 1009 | int plen, olen; |
| 1010 | |
| 1011 | arr[num++] = 0x1; /* lu, storage config */ |
| 1012 | arr[num++] = 0x0; /* reserved */ |
| 1013 | arr[num++] = 0x0; |
| 1014 | olen = strlen(na1); |
| 1015 | plen = olen + 1; |
| 1016 | if (plen % 4) |
| 1017 | plen = ((plen / 4) + 1) * 4; |
| 1018 | arr[num++] = plen; /* length, null termianted, padded */ |
| 1019 | memcpy(arr + num, na1, olen); |
| 1020 | memset(arr + num + olen, 0, plen - olen); |
| 1021 | num += plen; |
| 1022 | |
| 1023 | arr[num++] = 0x4; /* lu, logging */ |
| 1024 | arr[num++] = 0x0; /* reserved */ |
| 1025 | arr[num++] = 0x0; |
| 1026 | olen = strlen(na2); |
| 1027 | plen = olen + 1; |
| 1028 | if (plen % 4) |
| 1029 | plen = ((plen / 4) + 1) * 4; |
| 1030 | arr[num++] = plen; /* length, null terminated, padded */ |
| 1031 | memcpy(arr + num, na2, olen); |
| 1032 | memset(arr + num + olen, 0, plen - olen); |
| 1033 | num += plen; |
| 1034 | |
| 1035 | return num; |
| 1036 | } |
| 1037 | |
| 1038 | /* SCSI ports VPD page */ |
| 1039 | static int inquiry_evpd_88(unsigned char * arr, int target_dev_id) |
| 1040 | { |
| 1041 | int num = 0; |
| 1042 | int port_a, port_b; |
| 1043 | |
| 1044 | port_a = target_dev_id + 1; |
| 1045 | port_b = port_a + 1; |
| 1046 | arr[num++] = 0x0; /* reserved */ |
| 1047 | arr[num++] = 0x0; /* reserved */ |
| 1048 | arr[num++] = 0x0; |
| 1049 | arr[num++] = 0x1; /* relative port 1 (primary) */ |
| 1050 | memset(arr + num, 0, 6); |
| 1051 | num += 6; |
| 1052 | arr[num++] = 0x0; |
| 1053 | arr[num++] = 12; /* length tp descriptor */ |
| 1054 | /* naa-5 target port identifier (A) */ |
| 1055 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1056 | arr[num++] = 0x93; /* PIV=1, target port, NAA */ |
| 1057 | arr[num++] = 0x0; /* reserved */ |
| 1058 | arr[num++] = 0x8; /* length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1059 | put_unaligned_be64(naa5_comp_a + port_a, arr + num); |
| 1060 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1061 | arr[num++] = 0x0; /* reserved */ |
| 1062 | arr[num++] = 0x0; /* reserved */ |
| 1063 | arr[num++] = 0x0; |
| 1064 | arr[num++] = 0x2; /* relative port 2 (secondary) */ |
| 1065 | memset(arr + num, 0, 6); |
| 1066 | num += 6; |
| 1067 | arr[num++] = 0x0; |
| 1068 | arr[num++] = 12; /* length tp descriptor */ |
| 1069 | /* naa-5 target port identifier (B) */ |
| 1070 | arr[num++] = 0x61; /* proto=sas, binary */ |
| 1071 | arr[num++] = 0x93; /* PIV=1, target port, NAA */ |
| 1072 | arr[num++] = 0x0; /* reserved */ |
| 1073 | arr[num++] = 0x8; /* length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1074 | put_unaligned_be64(naa5_comp_a + port_b, arr + num); |
| 1075 | num += 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1076 | |
| 1077 | return num; |
| 1078 | } |
| 1079 | |
| 1080 | |
| 1081 | static unsigned char vpd89_data[] = { |
| 1082 | /* from 4th byte */ 0,0,0,0, |
| 1083 | 'l','i','n','u','x',' ',' ',' ', |
| 1084 | 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ', |
| 1085 | '1','2','3','4', |
| 1086 | 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0, |
| 1087 | 0xec,0,0,0, |
| 1088 | 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0, |
| 1089 | 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20, |
| 1090 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33, |
| 1091 | 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31, |
| 1092 | 0x53,0x41, |
| 1093 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 1094 | 0x20,0x20, |
| 1095 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, |
| 1096 | 0x10,0x80, |
| 1097 | 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0, |
| 1098 | 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0, |
| 1099 | 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0, |
| 1100 | 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0, |
| 1101 | 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40, |
| 1102 | 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0, |
| 1103 | 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0, |
| 1104 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1105 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1106 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1107 | 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42, |
| 1108 | 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8, |
| 1109 | 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe, |
| 1110 | 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0, |
| 1111 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1112 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1113 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1114 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1115 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1116 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1117 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1118 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1119 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1120 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1121 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1122 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51, |
| 1123 | }; |
| 1124 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1125 | /* ATA Information VPD page */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1126 | static int inquiry_evpd_89(unsigned char * arr) |
| 1127 | { |
| 1128 | memcpy(arr, vpd89_data, sizeof(vpd89_data)); |
| 1129 | return sizeof(vpd89_data); |
| 1130 | } |
| 1131 | |
| 1132 | |
| 1133 | static unsigned char vpdb0_data[] = { |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1134 | /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64, |
| 1135 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1136 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1137 | 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] | 1138 | }; |
| 1139 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1140 | /* Block limits VPD page (SBC-3) */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1141 | static int inquiry_evpd_b0(unsigned char * arr) |
| 1142 | { |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 1143 | unsigned int gran; |
| 1144 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1145 | memcpy(arr, vpdb0_data, sizeof(vpdb0_data)); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1146 | |
| 1147 | /* Optimal transfer length granularity */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1148 | gran = 1 << sdebug_physblk_exp; |
| 1149 | put_unaligned_be16(gran, arr + 2); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1150 | |
| 1151 | /* Maximum Transfer Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1152 | if (sdebug_store_sectors > 0x400) |
| 1153 | put_unaligned_be32(sdebug_store_sectors, arr + 4); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1154 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1155 | /* Optimal Transfer Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1156 | put_unaligned_be32(sdebug_opt_blks, &arr[8]); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1157 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1158 | if (sdebug_lbpu) { |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1159 | /* Maximum Unmap LBA Count */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1160 | put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]); |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1161 | |
| 1162 | /* Maximum Unmap Block Descriptor Count */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1163 | put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1164 | } |
| 1165 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1166 | /* Unmap Granularity Alignment */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1167 | if (sdebug_unmap_alignment) { |
| 1168 | put_unaligned_be32(sdebug_unmap_alignment, &arr[28]); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1169 | arr[28] |= 0x80; /* UGAVALID */ |
| 1170 | } |
| 1171 | |
Martin K. Petersen | e308b3d | 2010-03-23 01:12:27 -0400 | [diff] [blame] | 1172 | /* Optimal Unmap Granularity */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1173 | put_unaligned_be32(sdebug_unmap_granularity, &arr[24]); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1174 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1175 | /* Maximum WRITE SAME Length */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1176 | put_unaligned_be64(sdebug_write_same_length, &arr[32]); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1177 | |
| 1178 | return 0x3c; /* Mandatory page length for Logical Block Provisioning */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1179 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1180 | return sizeof(vpdb0_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1183 | /* Block device characteristics VPD page (SBC-3) */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1184 | static int inquiry_evpd_b1(unsigned char *arr) |
| 1185 | { |
| 1186 | memset(arr, 0, 0x3c); |
| 1187 | arr[0] = 0; |
Douglas Gilbert | 1e49f78 | 2009-10-29 01:48:31 -0400 | [diff] [blame] | 1188 | arr[1] = 1; /* non rotating medium (e.g. solid state) */ |
| 1189 | arr[2] = 0; |
| 1190 | arr[3] = 5; /* less than 1.8" */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1191 | |
| 1192 | return 0x3c; |
| 1193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1195 | /* Logical block provisioning VPD page (SBC-3) */ |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1196 | static int inquiry_evpd_b2(unsigned char *arr) |
| 1197 | { |
Martin K. Petersen | 3f0bc3b | 2012-03-08 10:48:29 -0500 | [diff] [blame] | 1198 | memset(arr, 0, 0x4); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1199 | arr[0] = 0; /* threshold exponent */ |
| 1200 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1201 | if (sdebug_lbpu) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1202 | arr[1] = 1 << 7; |
| 1203 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1204 | if (sdebug_lbpws) |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1205 | arr[1] |= 1 << 6; |
| 1206 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1207 | if (sdebug_lbpws10) |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1208 | arr[1] |= 1 << 5; |
| 1209 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1210 | if (sdebug_lbprz) |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1211 | arr[1] |= 1 << 2; |
| 1212 | |
Martin K. Petersen | 3f0bc3b | 2012-03-08 10:48:29 -0500 | [diff] [blame] | 1213 | return 0x4; |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1214 | } |
| 1215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | #define SDEBUG_LONG_INQ_SZ 96 |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1217 | #define SDEBUG_MAX_INQ_ARR_SZ 584 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1219 | 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] | 1220 | { |
| 1221 | unsigned char pq_pdt; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1222 | unsigned char * arr; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1223 | unsigned char *cmd = scp->cmnd; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1224 | int alloc_len, n, ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1225 | bool have_wlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1227 | alloc_len = get_unaligned_be16(cmd + 3); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 1228 | arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC); |
| 1229 | if (! arr) |
| 1230 | return DID_REQUEUE << 16; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1231 | have_wlun = scsi_is_wlun(scp->device->lun); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1232 | if (have_wlun) |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1233 | pq_pdt = TYPE_WLUN; /* present, wlun */ |
| 1234 | else if (sdebug_no_lun_0 && (devip->lun == SDEBUG_LUN_0_VAL)) |
| 1235 | pq_pdt = 0x7f; /* not present, PQ=3, PDT=0x1f */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1236 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1237 | pq_pdt = (sdebug_ptype & 0x1f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | arr[0] = pq_pdt; |
| 1239 | if (0x2 & cmd[1]) { /* CMDDT bit set */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1240 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1241 | kfree(arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | return check_condition_result; |
| 1243 | } else if (0x1 & cmd[1]) { /* EVPD bit set */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1244 | int lu_id_num, port_group_id, target_dev_id, len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1245 | char lu_id_str[6]; |
| 1246 | int host_no = devip->sdbg_host->shost->host_no; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1248 | port_group_id = (((host_no + 1) & 0x7f) << 8) + |
| 1249 | (devip->channel & 0x7f); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1250 | if (sdebug_vpd_use_hostno == 0) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1251 | host_no = 0; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1252 | lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) + |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1253 | (devip->target * 1000) + devip->lun); |
| 1254 | target_dev_id = ((host_no + 1) * 2000) + |
| 1255 | (devip->target * 1000) - 3; |
| 1256 | len = scnprintf(lu_id_str, 6, "%d", lu_id_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | if (0 == cmd[2]) { /* supported vital product data pages */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1258 | arr[1] = cmd[2]; /*sanity */ |
| 1259 | n = 4; |
| 1260 | arr[n++] = 0x0; /* this page */ |
| 1261 | arr[n++] = 0x80; /* unit serial number */ |
| 1262 | arr[n++] = 0x83; /* device identification */ |
| 1263 | arr[n++] = 0x84; /* software interface ident. */ |
| 1264 | arr[n++] = 0x85; /* management network addresses */ |
| 1265 | arr[n++] = 0x86; /* extended inquiry */ |
| 1266 | arr[n++] = 0x87; /* mode page policy */ |
| 1267 | arr[n++] = 0x88; /* SCSI ports */ |
| 1268 | arr[n++] = 0x89; /* ATA information */ |
| 1269 | arr[n++] = 0xb0; /* Block limits (SBC) */ |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1270 | arr[n++] = 0xb1; /* Block characteristics (SBC) */ |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1271 | if (scsi_debug_lbp()) /* Logical Block Prov. (SBC) */ |
| 1272 | arr[n++] = 0xb2; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1273 | arr[3] = n - 4; /* number of supported VPD pages */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | } else if (0x80 == cmd[2]) { /* unit serial number */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1275 | arr[1] = cmd[2]; /*sanity */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | arr[3] = len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1277 | memcpy(&arr[4], lu_id_str, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | } else if (0x83 == cmd[2]) { /* device identification */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1279 | arr[1] = cmd[2]; /*sanity */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1280 | arr[3] = inquiry_evpd_83(&arr[4], port_group_id, |
| 1281 | target_dev_id, lu_id_num, |
| 1282 | lu_id_str, len); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1283 | } else if (0x84 == cmd[2]) { /* Software interface ident. */ |
| 1284 | arr[1] = cmd[2]; /*sanity */ |
| 1285 | arr[3] = inquiry_evpd_84(&arr[4]); |
| 1286 | } else if (0x85 == cmd[2]) { /* Management network addresses */ |
| 1287 | arr[1] = cmd[2]; /*sanity */ |
| 1288 | arr[3] = inquiry_evpd_85(&arr[4]); |
| 1289 | } else if (0x86 == cmd[2]) { /* extended inquiry */ |
| 1290 | arr[1] = cmd[2]; /*sanity */ |
| 1291 | arr[3] = 0x3c; /* number of following entries */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1292 | if (sdebug_dif == SD_DIF_TYPE3_PROTECTION) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1293 | arr[4] = 0x4; /* SPT: GRD_CHK:1 */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1294 | else if (sdebug_dif) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1295 | arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */ |
| 1296 | else |
| 1297 | arr[4] = 0x0; /* no protection stuff */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1298 | arr[5] = 0x7; /* head of q, ordered + simple q's */ |
| 1299 | } else if (0x87 == cmd[2]) { /* mode page policy */ |
| 1300 | arr[1] = cmd[2]; /*sanity */ |
| 1301 | arr[3] = 0x8; /* number of following entries */ |
| 1302 | arr[4] = 0x2; /* disconnect-reconnect mp */ |
| 1303 | arr[6] = 0x80; /* mlus, shared */ |
| 1304 | arr[8] = 0x18; /* protocol specific lu */ |
| 1305 | arr[10] = 0x82; /* mlus, per initiator port */ |
| 1306 | } else if (0x88 == cmd[2]) { /* SCSI Ports */ |
| 1307 | arr[1] = cmd[2]; /*sanity */ |
| 1308 | arr[3] = inquiry_evpd_88(&arr[4], target_dev_id); |
| 1309 | } else if (0x89 == cmd[2]) { /* ATA information */ |
| 1310 | arr[1] = cmd[2]; /*sanity */ |
| 1311 | n = inquiry_evpd_89(&arr[4]); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1312 | put_unaligned_be16(n, arr + 2); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1313 | } else if (0xb0 == cmd[2]) { /* Block limits (SBC) */ |
| 1314 | arr[1] = cmd[2]; /*sanity */ |
| 1315 | arr[3] = inquiry_evpd_b0(&arr[4]); |
Matthew Wilcox | eac6e8e4 | 2008-06-19 10:02:58 -0600 | [diff] [blame] | 1316 | } else if (0xb1 == cmd[2]) { /* Block characteristics (SBC) */ |
| 1317 | arr[1] = cmd[2]; /*sanity */ |
| 1318 | arr[3] = inquiry_evpd_b1(&arr[4]); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1319 | } else if (0xb2 == cmd[2]) { /* Logical Block Prov. (SBC) */ |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 1320 | arr[1] = cmd[2]; /*sanity */ |
| 1321 | arr[3] = inquiry_evpd_b2(&arr[4]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1323 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1324 | kfree(arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | return check_condition_result; |
| 1326 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1327 | len = min(get_unaligned_be16(arr + 2) + 4, alloc_len); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1328 | ret = fill_from_dev_buffer(scp, arr, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1329 | min(len, SDEBUG_MAX_INQ_ARR_SZ)); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1330 | kfree(arr); |
| 1331 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | } |
| 1333 | /* drops through here for a standard inquiry */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1334 | arr[1] = sdebug_removable ? 0x80 : 0; /* Removable disk */ |
| 1335 | arr[2] = sdebug_scsi_level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | arr[3] = 2; /* response_data_format==2 */ |
| 1337 | arr[4] = SDEBUG_LONG_INQ_SZ - 5; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 1338 | arr[5] = (int)have_dif_prot; /* PROTECT bit */ |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1339 | if (sdebug_vpd_use_hostno == 0) |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1340 | arr[5] = 0x10; /* claim: implicit TGPS */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1341 | arr[6] = 0x10; /* claim: MultiP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1343 | arr[7] = 0xa; /* claim: LINKED + CMDQUE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | memcpy(&arr[8], inq_vendor_id, 8); |
| 1345 | memcpy(&arr[16], inq_product_id, 16); |
| 1346 | memcpy(&arr[32], inq_product_rev, 4); |
| 1347 | /* version descriptors (2 bytes each) follow */ |
Douglas Gilbert | e46b034 | 2014-08-05 12:21:53 +0200 | [diff] [blame] | 1348 | arr[58] = 0x0; arr[59] = 0xa2; /* SAM-5 rev 4 */ |
| 1349 | arr[60] = 0x4; arr[61] = 0x68; /* SPC-4 rev 37 */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1350 | n = 62; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1351 | if (sdebug_ptype == TYPE_DISK) { |
Douglas Gilbert | e46b034 | 2014-08-05 12:21:53 +0200 | [diff] [blame] | 1352 | arr[n++] = 0x4; arr[n++] = 0xc5; /* SBC-4 rev 36 */ |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1353 | } else if (sdebug_ptype == TYPE_TAPE) { |
Douglas Gilbert | e46b034 | 2014-08-05 12:21:53 +0200 | [diff] [blame] | 1354 | arr[n++] = 0x5; arr[n++] = 0x25; /* SSC-4 rev 3 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | } |
Douglas Gilbert | e46b034 | 2014-08-05 12:21:53 +0200 | [diff] [blame] | 1356 | arr[n++] = 0x20; arr[n++] = 0xe6; /* SPL-3 rev 7 */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1357 | ret = fill_from_dev_buffer(scp, arr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | min(alloc_len, SDEBUG_LONG_INQ_SZ)); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1359 | kfree(arr); |
| 1360 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1363 | static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, |
| 1364 | 0, 0, 0x0, 0x0}; |
| 1365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | static int resp_requests(struct scsi_cmnd * scp, |
| 1367 | struct sdebug_dev_info * devip) |
| 1368 | { |
| 1369 | unsigned char * sbuff; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1370 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1371 | unsigned char arr[SCSI_SENSE_BUFFERSIZE]; |
Tomas Winkler | 2492fc0 | 2015-07-28 16:54:26 +0300 | [diff] [blame] | 1372 | bool dsense; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | int len = 18; |
| 1374 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1375 | memset(arr, 0, sizeof(arr)); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1376 | dsense = !!(cmd[1] & 1); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1377 | sbuff = scp->sense_buffer; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1378 | if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1379 | if (dsense) { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1380 | arr[0] = 0x72; |
| 1381 | arr[1] = 0x0; /* NO_SENSE in sense_key */ |
| 1382 | arr[2] = THRESHOLD_EXCEEDED; |
| 1383 | arr[3] = 0xff; /* TEST set and MRIE==6 */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1384 | len = 8; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1385 | } else { |
| 1386 | arr[0] = 0x70; |
| 1387 | arr[2] = 0x0; /* NO_SENSE in sense_key */ |
| 1388 | arr[7] = 0xa; /* 18 byte sense buffer */ |
| 1389 | arr[12] = THRESHOLD_EXCEEDED; |
| 1390 | arr[13] = 0xff; /* TEST set and MRIE==6 */ |
| 1391 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1392 | } else { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1393 | memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1394 | if (arr[0] >= 0x70 && dsense == sdebug_dsense) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1395 | ; /* have sense and formats match */ |
| 1396 | else if (arr[0] <= 0x70) { |
| 1397 | if (dsense) { |
| 1398 | memset(arr, 0, 8); |
| 1399 | arr[0] = 0x72; |
| 1400 | len = 8; |
| 1401 | } else { |
| 1402 | memset(arr, 0, 18); |
| 1403 | arr[0] = 0x70; |
| 1404 | arr[7] = 0xa; |
| 1405 | } |
| 1406 | } else if (dsense) { |
| 1407 | memset(arr, 0, 8); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1408 | arr[0] = 0x72; |
| 1409 | arr[1] = sbuff[2]; /* sense key */ |
| 1410 | arr[2] = sbuff[12]; /* asc */ |
| 1411 | arr[3] = sbuff[13]; /* ascq */ |
| 1412 | len = 8; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1413 | } else { |
| 1414 | memset(arr, 0, 18); |
| 1415 | arr[0] = 0x70; |
| 1416 | arr[2] = sbuff[1]; |
| 1417 | arr[7] = 0xa; |
| 1418 | arr[12] = sbuff[1]; |
| 1419 | arr[13] = sbuff[3]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1420 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1421 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1422 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1423 | mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | return fill_from_dev_buffer(scp, arr, len); |
| 1425 | } |
| 1426 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1427 | static int resp_start_stop(struct scsi_cmnd * scp, |
| 1428 | struct sdebug_dev_info * devip) |
| 1429 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1430 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1431 | int power_cond, start; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1432 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1433 | power_cond = (cmd[4] & 0xf0) >> 4; |
| 1434 | if (power_cond) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1435 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1436 | return check_condition_result; |
| 1437 | } |
| 1438 | start = cmd[4] & 1; |
| 1439 | if (start == devip->stopped) |
| 1440 | devip->stopped = !start; |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1444 | static sector_t get_sdebug_capacity(void) |
| 1445 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1446 | static const unsigned int gibibyte = 1073741824; |
| 1447 | |
| 1448 | if (sdebug_virtual_gb > 0) |
| 1449 | return (sector_t)sdebug_virtual_gb * |
| 1450 | (gibibyte / sdebug_sector_size); |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1451 | else |
| 1452 | return sdebug_store_sectors; |
| 1453 | } |
| 1454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | #define SDEBUG_READCAP_ARR_SZ 8 |
| 1456 | static int resp_readcap(struct scsi_cmnd * scp, |
| 1457 | struct sdebug_dev_info * devip) |
| 1458 | { |
| 1459 | unsigned char arr[SDEBUG_READCAP_ARR_SZ]; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1460 | unsigned int capac; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1462 | /* following just in case virtual_gb changed */ |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1463 | sdebug_capacity = get_sdebug_capacity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | memset(arr, 0, SDEBUG_READCAP_ARR_SZ); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1465 | if (sdebug_capacity < 0xffffffff) { |
| 1466 | capac = (unsigned int)sdebug_capacity - 1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1467 | put_unaligned_be32(capac, arr + 0); |
| 1468 | } else |
| 1469 | put_unaligned_be32(0xffffffff, arr + 0); |
| 1470 | put_unaligned_be16(sdebug_sector_size, arr + 6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ); |
| 1472 | } |
| 1473 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1474 | #define SDEBUG_READCAP16_ARR_SZ 32 |
| 1475 | static int resp_readcap16(struct scsi_cmnd * scp, |
| 1476 | struct sdebug_dev_info * devip) |
| 1477 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1478 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1479 | unsigned char arr[SDEBUG_READCAP16_ARR_SZ]; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1480 | int alloc_len; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1481 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1482 | alloc_len = get_unaligned_be32(cmd + 10); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1483 | /* following just in case virtual_gb changed */ |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1484 | sdebug_capacity = get_sdebug_capacity(); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1485 | memset(arr, 0, SDEBUG_READCAP16_ARR_SZ); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1486 | put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0); |
| 1487 | put_unaligned_be32(sdebug_sector_size, arr + 8); |
| 1488 | arr[13] = sdebug_physblk_exp & 0xf; |
| 1489 | arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1490 | |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1491 | if (scsi_debug_lbp()) { |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 1492 | arr[14] |= 0x80; /* LBPME */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1493 | if (sdebug_lbprz) |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 1494 | arr[14] |= 0x40; /* LBPRZ */ |
| 1495 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 1496 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1497 | arr[15] = sdebug_lowest_aligned & 0xff; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1498 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1499 | if (sdebug_dif) { |
| 1500 | arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */ |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1501 | arr[12] |= 1; /* PROT_EN */ |
| 1502 | } |
| 1503 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1504 | return fill_from_dev_buffer(scp, arr, |
| 1505 | min(alloc_len, SDEBUG_READCAP16_ARR_SZ)); |
| 1506 | } |
| 1507 | |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1508 | #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412 |
| 1509 | |
| 1510 | static int resp_report_tgtpgs(struct scsi_cmnd * scp, |
| 1511 | struct sdebug_dev_info * devip) |
| 1512 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1513 | unsigned char *cmd = scp->cmnd; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1514 | unsigned char * arr; |
| 1515 | int host_no = devip->sdbg_host->shost->host_no; |
| 1516 | int n, ret, alen, rlen; |
| 1517 | int port_group_a, port_group_b, port_a, port_b; |
| 1518 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1519 | alen = get_unaligned_be32(cmd + 6); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 1520 | arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC); |
| 1521 | if (! arr) |
| 1522 | return DID_REQUEUE << 16; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1523 | /* |
| 1524 | * EVPD page 0x88 states we have two ports, one |
| 1525 | * real and a fake port with no device connected. |
| 1526 | * So we create two port groups with one port each |
| 1527 | * and set the group with port B to unavailable. |
| 1528 | */ |
| 1529 | port_a = 0x1; /* relative port A */ |
| 1530 | port_b = 0x2; /* relative port B */ |
| 1531 | port_group_a = (((host_no + 1) & 0x7f) << 8) + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1532 | (devip->channel & 0x7f); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1533 | port_group_b = (((host_no + 1) & 0x7f) << 8) + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1534 | (devip->channel & 0x7f) + 0x80; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1535 | |
| 1536 | /* |
| 1537 | * The asymmetric access state is cycled according to the host_id. |
| 1538 | */ |
| 1539 | n = 4; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1540 | if (sdebug_vpd_use_hostno == 0) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1541 | arr[n++] = host_no % 3; /* Asymm access state */ |
| 1542 | arr[n++] = 0x0F; /* claim: all states are supported */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1543 | } else { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1544 | arr[n++] = 0x0; /* Active/Optimized path */ |
| 1545 | arr[n++] = 0x01; /* only support active/optimized paths */ |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1546 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1547 | put_unaligned_be16(port_group_a, arr + n); |
| 1548 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1549 | arr[n++] = 0; /* Reserved */ |
| 1550 | arr[n++] = 0; /* Status code */ |
| 1551 | arr[n++] = 0; /* Vendor unique */ |
| 1552 | arr[n++] = 0x1; /* One port per group */ |
| 1553 | arr[n++] = 0; /* Reserved */ |
| 1554 | arr[n++] = 0; /* Reserved */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1555 | put_unaligned_be16(port_a, arr + n); |
| 1556 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1557 | arr[n++] = 3; /* Port unavailable */ |
| 1558 | arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1559 | put_unaligned_be16(port_group_b, arr + n); |
| 1560 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1561 | arr[n++] = 0; /* Reserved */ |
| 1562 | arr[n++] = 0; /* Status code */ |
| 1563 | arr[n++] = 0; /* Vendor unique */ |
| 1564 | arr[n++] = 0x1; /* One port per group */ |
| 1565 | arr[n++] = 0; /* Reserved */ |
| 1566 | arr[n++] = 0; /* Reserved */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1567 | put_unaligned_be16(port_b, arr + n); |
| 1568 | n += 2; |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1569 | |
| 1570 | rlen = n - 4; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1571 | put_unaligned_be32(rlen, arr + 0); |
Hannes Reinecke | 5a09e39 | 2006-10-20 09:58:47 +0200 | [diff] [blame] | 1572 | |
| 1573 | /* |
| 1574 | * Return the smallest value of either |
| 1575 | * - The allocated length |
| 1576 | * - The constructed command length |
| 1577 | * - The maximum array size |
| 1578 | */ |
| 1579 | rlen = min(alen,n); |
| 1580 | ret = fill_from_dev_buffer(scp, arr, |
| 1581 | min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ)); |
| 1582 | kfree(arr); |
| 1583 | return ret; |
| 1584 | } |
| 1585 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1586 | static int resp_rsup_opcodes(struct scsi_cmnd *scp, |
| 1587 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1588 | { |
| 1589 | bool rctd; |
| 1590 | u8 reporting_opts, req_opcode, sdeb_i, supp; |
| 1591 | u16 req_sa, u; |
| 1592 | u32 alloc_len, a_len; |
| 1593 | int k, offset, len, errsts, count, bump, na; |
| 1594 | const struct opcode_info_t *oip; |
| 1595 | const struct opcode_info_t *r_oip; |
| 1596 | u8 *arr; |
| 1597 | u8 *cmd = scp->cmnd; |
| 1598 | |
| 1599 | rctd = !!(cmd[2] & 0x80); |
| 1600 | reporting_opts = cmd[2] & 0x7; |
| 1601 | req_opcode = cmd[3]; |
| 1602 | req_sa = get_unaligned_be16(cmd + 4); |
| 1603 | alloc_len = get_unaligned_be32(cmd + 6); |
Colin Ian King | 6d310df | 2015-01-22 11:20:40 +0000 | [diff] [blame] | 1604 | if (alloc_len < 4 || alloc_len > 0xffff) { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1605 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
| 1606 | return check_condition_result; |
| 1607 | } |
| 1608 | if (alloc_len > 8192) |
| 1609 | a_len = 8192; |
| 1610 | else |
| 1611 | a_len = alloc_len; |
Sasha Levin | 99531e6 | 2015-01-17 17:47:37 -0500 | [diff] [blame] | 1612 | arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1613 | if (NULL == arr) { |
| 1614 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 1615 | INSUFF_RES_ASCQ); |
| 1616 | return check_condition_result; |
| 1617 | } |
| 1618 | switch (reporting_opts) { |
| 1619 | case 0: /* all commands */ |
| 1620 | /* count number of commands */ |
| 1621 | for (count = 0, oip = opcode_info_arr; |
| 1622 | oip->num_attached != 0xff; ++oip) { |
| 1623 | if (F_INV_OP & oip->flags) |
| 1624 | continue; |
| 1625 | count += (oip->num_attached + 1); |
| 1626 | } |
| 1627 | bump = rctd ? 20 : 8; |
| 1628 | put_unaligned_be32(count * bump, arr); |
| 1629 | for (offset = 4, oip = opcode_info_arr; |
| 1630 | oip->num_attached != 0xff && offset < a_len; ++oip) { |
| 1631 | if (F_INV_OP & oip->flags) |
| 1632 | continue; |
| 1633 | na = oip->num_attached; |
| 1634 | arr[offset] = oip->opcode; |
| 1635 | put_unaligned_be16(oip->sa, arr + offset + 2); |
| 1636 | if (rctd) |
| 1637 | arr[offset + 5] |= 0x2; |
| 1638 | if (FF_SA & oip->flags) |
| 1639 | arr[offset + 5] |= 0x1; |
| 1640 | put_unaligned_be16(oip->len_mask[0], arr + offset + 6); |
| 1641 | if (rctd) |
| 1642 | put_unaligned_be16(0xa, arr + offset + 8); |
| 1643 | r_oip = oip; |
| 1644 | for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) { |
| 1645 | if (F_INV_OP & oip->flags) |
| 1646 | continue; |
| 1647 | offset += bump; |
| 1648 | arr[offset] = oip->opcode; |
| 1649 | put_unaligned_be16(oip->sa, arr + offset + 2); |
| 1650 | if (rctd) |
| 1651 | arr[offset + 5] |= 0x2; |
| 1652 | if (FF_SA & oip->flags) |
| 1653 | arr[offset + 5] |= 0x1; |
| 1654 | put_unaligned_be16(oip->len_mask[0], |
| 1655 | arr + offset + 6); |
| 1656 | if (rctd) |
| 1657 | put_unaligned_be16(0xa, |
| 1658 | arr + offset + 8); |
| 1659 | } |
| 1660 | oip = r_oip; |
| 1661 | offset += bump; |
| 1662 | } |
| 1663 | break; |
| 1664 | case 1: /* one command: opcode only */ |
| 1665 | case 2: /* one command: opcode plus service action */ |
| 1666 | case 3: /* one command: if sa==0 then opcode only else opcode+sa */ |
| 1667 | sdeb_i = opcode_ind_arr[req_opcode]; |
| 1668 | oip = &opcode_info_arr[sdeb_i]; |
| 1669 | if (F_INV_OP & oip->flags) { |
| 1670 | supp = 1; |
| 1671 | offset = 4; |
| 1672 | } else { |
| 1673 | if (1 == reporting_opts) { |
| 1674 | if (FF_SA & oip->flags) { |
| 1675 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, |
| 1676 | 2, 2); |
| 1677 | kfree(arr); |
| 1678 | return check_condition_result; |
| 1679 | } |
| 1680 | req_sa = 0; |
| 1681 | } else if (2 == reporting_opts && |
| 1682 | 0 == (FF_SA & oip->flags)) { |
| 1683 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1); |
| 1684 | kfree(arr); /* point at requested sa */ |
| 1685 | return check_condition_result; |
| 1686 | } |
| 1687 | if (0 == (FF_SA & oip->flags) && |
| 1688 | req_opcode == oip->opcode) |
| 1689 | supp = 3; |
| 1690 | else if (0 == (FF_SA & oip->flags)) { |
| 1691 | na = oip->num_attached; |
| 1692 | for (k = 0, oip = oip->arrp; k < na; |
| 1693 | ++k, ++oip) { |
| 1694 | if (req_opcode == oip->opcode) |
| 1695 | break; |
| 1696 | } |
| 1697 | supp = (k >= na) ? 1 : 3; |
| 1698 | } else if (req_sa != oip->sa) { |
| 1699 | na = oip->num_attached; |
| 1700 | for (k = 0, oip = oip->arrp; k < na; |
| 1701 | ++k, ++oip) { |
| 1702 | if (req_sa == oip->sa) |
| 1703 | break; |
| 1704 | } |
| 1705 | supp = (k >= na) ? 1 : 3; |
| 1706 | } else |
| 1707 | supp = 3; |
| 1708 | if (3 == supp) { |
| 1709 | u = oip->len_mask[0]; |
| 1710 | put_unaligned_be16(u, arr + 2); |
| 1711 | arr[4] = oip->opcode; |
| 1712 | for (k = 1; k < u; ++k) |
| 1713 | arr[4 + k] = (k < 16) ? |
| 1714 | oip->len_mask[k] : 0xff; |
| 1715 | offset = 4 + u; |
| 1716 | } else |
| 1717 | offset = 4; |
| 1718 | } |
| 1719 | arr[1] = (rctd ? 0x80 : 0) | supp; |
| 1720 | if (rctd) { |
| 1721 | put_unaligned_be16(0xa, arr + offset); |
| 1722 | offset += 12; |
| 1723 | } |
| 1724 | break; |
| 1725 | default: |
| 1726 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2); |
| 1727 | kfree(arr); |
| 1728 | return check_condition_result; |
| 1729 | } |
| 1730 | offset = (offset < a_len) ? offset : a_len; |
| 1731 | len = (offset < alloc_len) ? offset : alloc_len; |
| 1732 | errsts = fill_from_dev_buffer(scp, arr, len); |
| 1733 | kfree(arr); |
| 1734 | return errsts; |
| 1735 | } |
| 1736 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1737 | static int resp_rsup_tmfs(struct scsi_cmnd *scp, |
| 1738 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 1739 | { |
| 1740 | bool repd; |
| 1741 | u32 alloc_len, len; |
| 1742 | u8 arr[16]; |
| 1743 | u8 *cmd = scp->cmnd; |
| 1744 | |
| 1745 | memset(arr, 0, sizeof(arr)); |
| 1746 | repd = !!(cmd[2] & 0x80); |
| 1747 | alloc_len = get_unaligned_be32(cmd + 6); |
| 1748 | if (alloc_len < 4) { |
| 1749 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
| 1750 | return check_condition_result; |
| 1751 | } |
| 1752 | arr[0] = 0xc8; /* ATS | ATSS | LURS */ |
| 1753 | arr[1] = 0x1; /* ITNRS */ |
| 1754 | if (repd) { |
| 1755 | arr[3] = 0xc; |
| 1756 | len = 16; |
| 1757 | } else |
| 1758 | len = 4; |
| 1759 | |
| 1760 | len = (len < alloc_len) ? len : alloc_len; |
| 1761 | return fill_from_dev_buffer(scp, arr, len); |
| 1762 | } |
| 1763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | /* <<Following mode page info copied from ST318451LW>> */ |
| 1765 | |
| 1766 | static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target) |
| 1767 | { /* Read-Write Error Recovery page for mode_sense */ |
| 1768 | unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0, |
| 1769 | 5, 0, 0xff, 0xff}; |
| 1770 | |
| 1771 | memcpy(p, err_recov_pg, sizeof(err_recov_pg)); |
| 1772 | if (1 == pcontrol) |
| 1773 | memset(p + 2, 0, sizeof(err_recov_pg) - 2); |
| 1774 | return sizeof(err_recov_pg); |
| 1775 | } |
| 1776 | |
| 1777 | static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target) |
| 1778 | { /* Disconnect-Reconnect page for mode_sense */ |
| 1779 | unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0, |
| 1780 | 0, 0, 0, 0, 0, 0, 0, 0}; |
| 1781 | |
| 1782 | memcpy(p, disconnect_pg, sizeof(disconnect_pg)); |
| 1783 | if (1 == pcontrol) |
| 1784 | memset(p + 2, 0, sizeof(disconnect_pg) - 2); |
| 1785 | return sizeof(disconnect_pg); |
| 1786 | } |
| 1787 | |
| 1788 | static int resp_format_pg(unsigned char * p, int pcontrol, int target) |
| 1789 | { /* Format device page for mode_sense */ |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1790 | unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0, |
| 1791 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1792 | 0, 0, 0, 0, 0x40, 0, 0, 0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1794 | memcpy(p, format_pg, sizeof(format_pg)); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1795 | put_unaligned_be16(sdebug_sectors_per, p + 10); |
| 1796 | put_unaligned_be16(sdebug_sector_size, p + 12); |
| 1797 | if (sdebug_removable) |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 1798 | p[20] |= 0x20; /* should agree with INQUIRY */ |
| 1799 | if (1 == pcontrol) |
| 1800 | memset(p + 2, 0, sizeof(format_pg) - 2); |
| 1801 | return sizeof(format_pg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1804 | static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0, |
| 1805 | 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, |
| 1806 | 0, 0, 0, 0}; |
| 1807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | static int resp_caching_pg(unsigned char * p, int pcontrol, int target) |
| 1809 | { /* Caching page for mode_sense */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1810 | unsigned char ch_caching_pg[] = {/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0, |
| 1811 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 1812 | 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] | 1813 | 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0}; |
| 1814 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1815 | if (SDEBUG_OPT_N_WCE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1816 | caching_pg[2] &= ~0x4; /* set WCE=0 (default WCE=1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | memcpy(p, caching_pg, sizeof(caching_pg)); |
| 1818 | if (1 == pcontrol) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1819 | memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg)); |
| 1820 | else if (2 == pcontrol) |
| 1821 | memcpy(p, d_caching_pg, sizeof(d_caching_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | return sizeof(caching_pg); |
| 1823 | } |
| 1824 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1825 | static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0, |
| 1826 | 0, 0, 0x2, 0x4b}; |
| 1827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target) |
| 1829 | { /* Control mode page for mode_sense */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1830 | unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0, |
| 1831 | 0, 0, 0, 0}; |
| 1832 | 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] | 1833 | 0, 0, 0x2, 0x4b}; |
| 1834 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1835 | if (sdebug_dsense) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | ctrl_m_pg[2] |= 0x4; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1837 | else |
| 1838 | ctrl_m_pg[2] &= ~0x4; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1839 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1840 | if (sdebug_ato) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 1841 | ctrl_m_pg[5] |= 0x80; /* ATO=1 */ |
| 1842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg)); |
| 1844 | if (1 == pcontrol) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1845 | memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg)); |
| 1846 | else if (2 == pcontrol) |
| 1847 | memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | return sizeof(ctrl_m_pg); |
| 1849 | } |
| 1850 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target) |
| 1853 | { /* Informational Exceptions control mode page for mode_sense */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1854 | unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0, |
| 1855 | 0, 0, 0x0, 0x0}; |
| 1856 | unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0, |
| 1857 | 0, 0, 0x0, 0x0}; |
| 1858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | memcpy(p, iec_m_pg, sizeof(iec_m_pg)); |
| 1860 | if (1 == pcontrol) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1861 | memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg)); |
| 1862 | else if (2 == pcontrol) |
| 1863 | memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | return sizeof(iec_m_pg); |
| 1865 | } |
| 1866 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1867 | static int resp_sas_sf_m_pg(unsigned char * p, int pcontrol, int target) |
| 1868 | { /* SAS SSP mode page - short format for mode_sense */ |
| 1869 | unsigned char sas_sf_m_pg[] = {0x19, 0x6, |
| 1870 | 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0}; |
| 1871 | |
| 1872 | memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg)); |
| 1873 | if (1 == pcontrol) |
| 1874 | memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2); |
| 1875 | return sizeof(sas_sf_m_pg); |
| 1876 | } |
| 1877 | |
| 1878 | |
| 1879 | static int resp_sas_pcd_m_spg(unsigned char * p, int pcontrol, int target, |
| 1880 | int target_dev_id) |
| 1881 | { /* SAS phy control and discover mode page for mode_sense */ |
| 1882 | unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2, |
| 1883 | 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1884 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
| 1885 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1886 | 0x2, 0, 0, 0, 0, 0, 0, 0, |
| 1887 | 0x88, 0x99, 0, 0, 0, 0, 0, 0, |
| 1888 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1889 | 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1890 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
| 1891 | 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1892 | 0x3, 0, 0, 0, 0, 0, 0, 0, |
| 1893 | 0x88, 0x99, 0, 0, 0, 0, 0, 0, |
| 1894 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1895 | }; |
| 1896 | int port_a, port_b; |
| 1897 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1898 | put_unaligned_be64(naa5_comp_a, sas_pcd_m_pg + 16); |
| 1899 | put_unaligned_be64(naa5_comp_c + 1, sas_pcd_m_pg + 24); |
| 1900 | put_unaligned_be64(naa5_comp_a, sas_pcd_m_pg + 64); |
| 1901 | put_unaligned_be64(naa5_comp_c + 1, sas_pcd_m_pg + 72); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1902 | port_a = target_dev_id + 1; |
| 1903 | port_b = port_a + 1; |
| 1904 | memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg)); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1905 | put_unaligned_be32(port_a, p + 20); |
| 1906 | put_unaligned_be32(port_b, p + 48 + 20); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1907 | if (1 == pcontrol) |
| 1908 | memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4); |
| 1909 | return sizeof(sas_pcd_m_pg); |
| 1910 | } |
| 1911 | |
| 1912 | static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol) |
| 1913 | { /* SAS SSP shared protocol specific port mode subpage */ |
| 1914 | unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0, |
| 1915 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 1916 | }; |
| 1917 | |
| 1918 | memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg)); |
| 1919 | if (1 == pcontrol) |
| 1920 | memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4); |
| 1921 | return sizeof(sas_sha_m_pg); |
| 1922 | } |
| 1923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | #define SDEBUG_MAX_MSENSE_SZ 256 |
| 1925 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 1926 | static int resp_mode_sense(struct scsi_cmnd *scp, |
| 1927 | struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | { |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1929 | unsigned char dbd, llbaa; |
| 1930 | int pcontrol, pcode, subpcode, bd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | unsigned char dev_spec; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1932 | int alloc_len, msense_6, offset, len, target_dev_id; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 1933 | int target = scp->device->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | unsigned char * ap; |
| 1935 | unsigned char arr[SDEBUG_MAX_MSENSE_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 1936 | unsigned char *cmd = scp->cmnd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1938 | dbd = !!(cmd[1] & 0x8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | pcontrol = (cmd[2] & 0xc0) >> 6; |
| 1940 | pcode = cmd[2] & 0x3f; |
| 1941 | subpcode = cmd[3]; |
| 1942 | msense_6 = (MODE_SENSE == cmd[0]); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1943 | llbaa = msense_6 ? 0 : !!(cmd[1] & 0x10); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1944 | if ((sdebug_ptype == TYPE_DISK) && (dbd == 0)) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1945 | bd_len = llbaa ? 16 : 8; |
| 1946 | else |
| 1947 | bd_len = 0; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1948 | alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | memset(arr, 0, SDEBUG_MAX_MSENSE_SZ); |
| 1950 | if (0x3 == pcontrol) { /* Saving values not supported */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 1951 | mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | return check_condition_result; |
| 1953 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1954 | target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) + |
| 1955 | (devip->target * 1000) - 3; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 1956 | /* for disks set DPOFUA bit and clear write protect (WP) bit */ |
| 1957 | if (sdebug_ptype == TYPE_DISK) |
| 1958 | dev_spec = 0x10; /* =0x90 if WP=1 implies read-only */ |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1959 | else |
| 1960 | dev_spec = 0x0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | if (msense_6) { |
| 1962 | arr[2] = dev_spec; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1963 | arr[3] = bd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | offset = 4; |
| 1965 | } else { |
| 1966 | arr[3] = dev_spec; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1967 | if (16 == bd_len) |
| 1968 | arr[4] = 0x1; /* set LONGLBA bit */ |
| 1969 | arr[7] = bd_len; /* assume 255 or less */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | offset = 8; |
| 1971 | } |
| 1972 | ap = arr + offset; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 1973 | if ((bd_len > 0) && (!sdebug_capacity)) |
| 1974 | sdebug_capacity = get_sdebug_capacity(); |
| 1975 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1976 | if (8 == bd_len) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1977 | if (sdebug_capacity > 0xfffffffe) |
| 1978 | put_unaligned_be32(0xffffffff, ap + 0); |
| 1979 | else |
| 1980 | put_unaligned_be32(sdebug_capacity, ap + 0); |
| 1981 | put_unaligned_be16(sdebug_sector_size, ap + 6); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1982 | offset += bd_len; |
| 1983 | ap = arr + offset; |
| 1984 | } else if (16 == bd_len) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 1985 | put_unaligned_be64((u64)sdebug_capacity, ap + 0); |
| 1986 | put_unaligned_be32(sdebug_sector_size, ap + 12); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 1987 | offset += bd_len; |
| 1988 | ap = arr + offset; |
| 1989 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 1991 | if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) { |
| 1992 | /* TODO: Control Extension page */ |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 1993 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | return check_condition_result; |
| 1995 | } |
| 1996 | switch (pcode) { |
| 1997 | case 0x1: /* Read-Write error recovery page, direct access */ |
| 1998 | len = resp_err_recov_pg(ap, pcontrol, target); |
| 1999 | offset += len; |
| 2000 | break; |
| 2001 | case 0x2: /* Disconnect-Reconnect page, all devices */ |
| 2002 | len = resp_disconnect_pg(ap, pcontrol, target); |
| 2003 | offset += len; |
| 2004 | break; |
| 2005 | case 0x3: /* Format device page, direct access */ |
| 2006 | len = resp_format_pg(ap, pcontrol, target); |
| 2007 | offset += len; |
| 2008 | break; |
| 2009 | case 0x8: /* Caching page, direct access */ |
| 2010 | len = resp_caching_pg(ap, pcontrol, target); |
| 2011 | offset += len; |
| 2012 | break; |
| 2013 | case 0xa: /* Control Mode page, all devices */ |
| 2014 | len = resp_ctrl_m_pg(ap, pcontrol, target); |
| 2015 | offset += len; |
| 2016 | break; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2017 | case 0x19: /* if spc==1 then sas phy, control+discover */ |
| 2018 | if ((subpcode > 0x2) && (subpcode < 0xff)) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2019 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2020 | return check_condition_result; |
| 2021 | } |
| 2022 | len = 0; |
| 2023 | if ((0x0 == subpcode) || (0xff == subpcode)) |
| 2024 | len += resp_sas_sf_m_pg(ap + len, pcontrol, target); |
| 2025 | if ((0x1 == subpcode) || (0xff == subpcode)) |
| 2026 | len += resp_sas_pcd_m_spg(ap + len, pcontrol, target, |
| 2027 | target_dev_id); |
| 2028 | if ((0x2 == subpcode) || (0xff == subpcode)) |
| 2029 | len += resp_sas_sha_m_spg(ap + len, pcontrol); |
| 2030 | offset += len; |
| 2031 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | case 0x1c: /* Informational Exceptions Mode page, all devices */ |
| 2033 | len = resp_iec_m_pg(ap, pcontrol, target); |
| 2034 | offset += len; |
| 2035 | break; |
| 2036 | case 0x3f: /* Read all Mode pages */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2037 | if ((0 == subpcode) || (0xff == subpcode)) { |
| 2038 | len = resp_err_recov_pg(ap, pcontrol, target); |
| 2039 | len += resp_disconnect_pg(ap + len, pcontrol, target); |
| 2040 | len += resp_format_pg(ap + len, pcontrol, target); |
| 2041 | len += resp_caching_pg(ap + len, pcontrol, target); |
| 2042 | len += resp_ctrl_m_pg(ap + len, pcontrol, target); |
| 2043 | len += resp_sas_sf_m_pg(ap + len, pcontrol, target); |
| 2044 | if (0xff == subpcode) { |
| 2045 | len += resp_sas_pcd_m_spg(ap + len, pcontrol, |
| 2046 | target, target_dev_id); |
| 2047 | len += resp_sas_sha_m_spg(ap + len, pcontrol); |
| 2048 | } |
| 2049 | len += resp_iec_m_pg(ap + len, pcontrol, target); |
| 2050 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2051 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2052 | return check_condition_result; |
| 2053 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | offset += len; |
| 2055 | break; |
| 2056 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2057 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | return check_condition_result; |
| 2059 | } |
| 2060 | if (msense_6) |
| 2061 | arr[0] = offset - 1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2062 | else |
| 2063 | put_unaligned_be16((offset - 2), arr + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | return fill_from_dev_buffer(scp, arr, min(alloc_len, offset)); |
| 2065 | } |
| 2066 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2067 | #define SDEBUG_MAX_MSELECT_SZ 512 |
| 2068 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2069 | static int resp_mode_select(struct scsi_cmnd *scp, |
| 2070 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2071 | { |
| 2072 | int pf, sp, ps, md_len, bd_len, off, spf, pg_len; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2073 | int param_len, res, mpage; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2074 | unsigned char arr[SDEBUG_MAX_MSELECT_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2075 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2076 | int mselect6 = (MODE_SELECT == cmd[0]); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2077 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2078 | memset(arr, 0, sizeof(arr)); |
| 2079 | pf = cmd[1] & 0x10; |
| 2080 | sp = cmd[1] & 0x1; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2081 | param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2082 | if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2083 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2084 | return check_condition_result; |
| 2085 | } |
| 2086 | res = fetch_to_dev_buffer(scp, arr, param_len); |
| 2087 | if (-1 == res) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2088 | return DID_ERROR << 16; |
| 2089 | else if (sdebug_verbose && (res < param_len)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2090 | sdev_printk(KERN_INFO, scp->device, |
| 2091 | "%s: cdb indicated=%d, IO sent=%d bytes\n", |
| 2092 | __func__, param_len, res); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2093 | md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2); |
| 2094 | bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2095 | if (md_len > 2) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2096 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2097 | return check_condition_result; |
| 2098 | } |
| 2099 | off = bd_len + (mselect6 ? 4 : 8); |
| 2100 | mpage = arr[off] & 0x3f; |
| 2101 | ps = !!(arr[off] & 0x80); |
| 2102 | if (ps) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2103 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2104 | return check_condition_result; |
| 2105 | } |
| 2106 | spf = !!(arr[off] & 0x40); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2107 | pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) : |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2108 | (arr[off + 1] + 2); |
| 2109 | if ((pg_len + off) > param_len) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2110 | mk_sense_buffer(scp, ILLEGAL_REQUEST, |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2111 | PARAMETER_LIST_LENGTH_ERR, 0); |
| 2112 | return check_condition_result; |
| 2113 | } |
| 2114 | switch (mpage) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2115 | case 0x8: /* Caching Mode page */ |
| 2116 | if (caching_pg[1] == arr[off + 1]) { |
| 2117 | memcpy(caching_pg + 2, arr + off + 2, |
| 2118 | sizeof(caching_pg) - 2); |
| 2119 | goto set_mode_changed_ua; |
| 2120 | } |
| 2121 | break; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2122 | case 0xa: /* Control Mode page */ |
| 2123 | if (ctrl_m_pg[1] == arr[off + 1]) { |
| 2124 | memcpy(ctrl_m_pg + 2, arr + off + 2, |
| 2125 | sizeof(ctrl_m_pg) - 2); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2126 | sdebug_dsense = !!(ctrl_m_pg[2] & 0x4); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2127 | goto set_mode_changed_ua; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2128 | } |
| 2129 | break; |
| 2130 | case 0x1c: /* Informational Exceptions Mode page */ |
| 2131 | if (iec_m_pg[1] == arr[off + 1]) { |
| 2132 | memcpy(iec_m_pg + 2, arr + off + 2, |
| 2133 | sizeof(iec_m_pg) - 2); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2134 | goto set_mode_changed_ua; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2135 | } |
| 2136 | break; |
| 2137 | default: |
| 2138 | break; |
| 2139 | } |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2140 | mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2141 | return check_condition_result; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2142 | set_mode_changed_ua: |
| 2143 | set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm); |
| 2144 | return 0; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | static int resp_temp_l_pg(unsigned char * arr) |
| 2148 | { |
| 2149 | unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38, |
| 2150 | 0x0, 0x1, 0x3, 0x2, 0x0, 65, |
| 2151 | }; |
| 2152 | |
| 2153 | memcpy(arr, temp_l_pg, sizeof(temp_l_pg)); |
| 2154 | return sizeof(temp_l_pg); |
| 2155 | } |
| 2156 | |
| 2157 | static int resp_ie_l_pg(unsigned char * arr) |
| 2158 | { |
| 2159 | unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38, |
| 2160 | }; |
| 2161 | |
| 2162 | memcpy(arr, ie_l_pg, sizeof(ie_l_pg)); |
| 2163 | if (iec_m_pg[2] & 0x4) { /* TEST bit set */ |
| 2164 | arr[4] = THRESHOLD_EXCEEDED; |
| 2165 | arr[5] = 0xff; |
| 2166 | } |
| 2167 | return sizeof(ie_l_pg); |
| 2168 | } |
| 2169 | |
| 2170 | #define SDEBUG_MAX_LSENSE_SZ 512 |
| 2171 | |
| 2172 | static int resp_log_sense(struct scsi_cmnd * scp, |
| 2173 | struct sdebug_dev_info * devip) |
| 2174 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2175 | int ppc, sp, pcontrol, pcode, subpcode, alloc_len, len, n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2176 | unsigned char arr[SDEBUG_MAX_LSENSE_SZ]; |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 2177 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2178 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2179 | memset(arr, 0, sizeof(arr)); |
| 2180 | ppc = cmd[1] & 0x2; |
| 2181 | sp = cmd[1] & 0x1; |
| 2182 | if (ppc || sp) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2183 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2184 | return check_condition_result; |
| 2185 | } |
| 2186 | pcontrol = (cmd[2] & 0xc0) >> 6; |
| 2187 | pcode = cmd[2] & 0x3f; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2188 | subpcode = cmd[3] & 0xff; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2189 | alloc_len = get_unaligned_be16(cmd + 7); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2190 | arr[0] = pcode; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2191 | if (0 == subpcode) { |
| 2192 | switch (pcode) { |
| 2193 | case 0x0: /* Supported log pages log page */ |
| 2194 | n = 4; |
| 2195 | arr[n++] = 0x0; /* this page */ |
| 2196 | arr[n++] = 0xd; /* Temperature */ |
| 2197 | arr[n++] = 0x2f; /* Informational exceptions */ |
| 2198 | arr[3] = n - 4; |
| 2199 | break; |
| 2200 | case 0xd: /* Temperature log page */ |
| 2201 | arr[3] = resp_temp_l_pg(arr + 4); |
| 2202 | break; |
| 2203 | case 0x2f: /* Informational exceptions log page */ |
| 2204 | arr[3] = resp_ie_l_pg(arr + 4); |
| 2205 | break; |
| 2206 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2207 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2208 | return check_condition_result; |
| 2209 | } |
| 2210 | } else if (0xff == subpcode) { |
| 2211 | arr[0] |= 0x40; |
| 2212 | arr[1] = subpcode; |
| 2213 | switch (pcode) { |
| 2214 | case 0x0: /* Supported log pages and subpages log page */ |
| 2215 | n = 4; |
| 2216 | arr[n++] = 0x0; |
| 2217 | arr[n++] = 0x0; /* 0,0 page */ |
| 2218 | arr[n++] = 0x0; |
| 2219 | arr[n++] = 0xff; /* this page */ |
| 2220 | arr[n++] = 0xd; |
| 2221 | arr[n++] = 0x0; /* Temperature */ |
| 2222 | arr[n++] = 0x2f; |
| 2223 | arr[n++] = 0x0; /* Informational exceptions */ |
| 2224 | arr[3] = n - 4; |
| 2225 | break; |
| 2226 | case 0xd: /* Temperature subpages */ |
| 2227 | n = 4; |
| 2228 | arr[n++] = 0xd; |
| 2229 | arr[n++] = 0x0; /* Temperature */ |
| 2230 | arr[3] = n - 4; |
| 2231 | break; |
| 2232 | case 0x2f: /* Informational exceptions subpages */ |
| 2233 | n = 4; |
| 2234 | arr[n++] = 0x2f; |
| 2235 | arr[n++] = 0x0; /* Informational exceptions */ |
| 2236 | arr[3] = n - 4; |
| 2237 | break; |
| 2238 | default: |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2239 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 2240 | return check_condition_result; |
| 2241 | } |
| 2242 | } else { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2243 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2244 | return check_condition_result; |
| 2245 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2246 | len = min(get_unaligned_be16(arr + 2) + 4, alloc_len); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2247 | return fill_from_dev_buffer(scp, arr, |
| 2248 | min(len, SDEBUG_MAX_INQ_ARR_SZ)); |
| 2249 | } |
| 2250 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2251 | static int check_device_access_params(struct scsi_cmnd *scp, |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2252 | unsigned long long lba, unsigned int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2254 | if (lba + num > sdebug_capacity) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2255 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | return check_condition_result; |
| 2257 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2258 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 2259 | if (num > sdebug_store_sectors) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 2260 | /* needs work to find which cdb byte 'num' comes from */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2261 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2262 | return check_condition_result; |
| 2263 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2264 | return 0; |
| 2265 | } |
| 2266 | |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2267 | /* Returns number of bytes copied or -1 if error. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2268 | static int do_device_access(struct scsi_cmnd *scmd, u64 lba, u32 num, |
| 2269 | bool do_write) |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2270 | { |
| 2271 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2272 | u64 block, rest = 0; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2273 | struct scsi_data_buffer *sdb; |
| 2274 | enum dma_data_direction dir; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2275 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2276 | if (do_write) { |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2277 | sdb = scsi_out(scmd); |
| 2278 | dir = DMA_TO_DEVICE; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2279 | } else { |
| 2280 | sdb = scsi_in(scmd); |
| 2281 | dir = DMA_FROM_DEVICE; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | if (!sdb->length) |
| 2285 | return 0; |
| 2286 | if (!(scsi_bidi_cmnd(scmd) || scmd->sc_data_direction == dir)) |
| 2287 | return -1; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2288 | |
| 2289 | block = do_div(lba, sdebug_store_sectors); |
| 2290 | if (block + num > sdebug_store_sectors) |
| 2291 | rest = block + num - sdebug_store_sectors; |
| 2292 | |
Dave Gordon | 386ecb1 | 2015-06-30 14:58:57 -0700 | [diff] [blame] | 2293 | ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2294 | fake_storep + (block * sdebug_sector_size), |
| 2295 | (num - rest) * sdebug_sector_size, 0, do_write); |
| 2296 | if (ret != (num - rest) * sdebug_sector_size) |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2297 | return ret; |
| 2298 | |
| 2299 | if (rest) { |
Dave Gordon | 386ecb1 | 2015-06-30 14:58:57 -0700 | [diff] [blame] | 2300 | ret += sg_copy_buffer(sdb->table.sgl, sdb->table.nents, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2301 | fake_storep, rest * sdebug_sector_size, |
| 2302 | (num - rest) * sdebug_sector_size, do_write); |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2303 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2304 | |
| 2305 | return ret; |
| 2306 | } |
| 2307 | |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2308 | /* If fake_store(lba,num) compares equal to arr(num), then copy top half of |
| 2309 | * arr into fake_store(lba,num) and return true. If comparison fails then |
| 2310 | * return false. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2311 | static bool comp_write_worker(u64 lba, u32 num, const u8 *arr) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2312 | { |
| 2313 | bool res; |
| 2314 | u64 block, rest = 0; |
| 2315 | u32 store_blks = sdebug_store_sectors; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2316 | u32 lb_size = sdebug_sector_size; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 2317 | |
| 2318 | block = do_div(lba, store_blks); |
| 2319 | if (block + num > store_blks) |
| 2320 | rest = block + num - store_blks; |
| 2321 | |
| 2322 | res = !memcmp(fake_storep + (block * lb_size), arr, |
| 2323 | (num - rest) * lb_size); |
| 2324 | if (!res) |
| 2325 | return res; |
| 2326 | if (rest) |
| 2327 | res = memcmp(fake_storep, arr + ((num - rest) * lb_size), |
| 2328 | rest * lb_size); |
| 2329 | if (!res) |
| 2330 | return res; |
| 2331 | arr += num * lb_size; |
| 2332 | memcpy(fake_storep + (block * lb_size), arr, (num - rest) * lb_size); |
| 2333 | if (rest) |
| 2334 | memcpy(fake_storep, arr + ((num - rest) * lb_size), |
| 2335 | rest * lb_size); |
| 2336 | return res; |
| 2337 | } |
| 2338 | |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2339 | static __be16 dif_compute_csum(const void *buf, int len) |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2340 | { |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2341 | __be16 csum; |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2342 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2343 | if (sdebug_guard) |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2344 | csum = (__force __be16)ip_compute_csum(buf, len); |
| 2345 | else |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2346 | csum = cpu_to_be16(crc_t10dif(buf, len)); |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2347 | |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2348 | return csum; |
| 2349 | } |
| 2350 | |
| 2351 | static int dif_verify(struct sd_dif_tuple *sdt, const void *data, |
| 2352 | sector_t sector, u32 ei_lba) |
| 2353 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2354 | __be16 csum = dif_compute_csum(data, sdebug_sector_size); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2355 | |
| 2356 | if (sdt->guard_tag != csum) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2357 | 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] | 2358 | (unsigned long)sector, |
| 2359 | be16_to_cpu(sdt->guard_tag), |
| 2360 | be16_to_cpu(csum)); |
| 2361 | return 0x01; |
| 2362 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2363 | if (sdebug_dif == SD_DIF_TYPE1_PROTECTION && |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2364 | be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2365 | pr_err("REF check failed on sector %lu\n", |
| 2366 | (unsigned long)sector); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2367 | return 0x03; |
| 2368 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2369 | if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2370 | be32_to_cpu(sdt->ref_tag) != ei_lba) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 2371 | pr_err("REF check failed on sector %lu\n", |
| 2372 | (unsigned long)sector); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2373 | return 0x03; |
| 2374 | } |
| 2375 | return 0; |
| 2376 | } |
| 2377 | |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2378 | static void dif_copy_prot(struct scsi_cmnd *SCpnt, sector_t sector, |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2379 | unsigned int sectors, bool read) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2380 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2381 | size_t resid; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2382 | void *paddr; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2383 | const void *dif_store_end = dif_storep + sdebug_store_sectors; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2384 | struct sg_mapping_iter miter; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2385 | |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 2386 | /* Bytes of protection data to copy into sgl */ |
| 2387 | resid = sectors * sizeof(*dif_storep); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2388 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2389 | sg_miter_start(&miter, scsi_prot_sglist(SCpnt), |
| 2390 | scsi_prot_sg_count(SCpnt), SG_MITER_ATOMIC | |
| 2391 | (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG)); |
| 2392 | |
| 2393 | while (sg_miter_next(&miter) && resid > 0) { |
| 2394 | size_t len = min(miter.length, resid); |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2395 | void *start = dif_store(sector); |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2396 | size_t rest = 0; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2397 | |
| 2398 | if (dif_store_end < start + len) |
| 2399 | rest = start + len - dif_store_end; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2400 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2401 | paddr = miter.addr; |
Akinobu Mita | 14faa94 | 2013-09-18 21:27:24 +0900 | [diff] [blame] | 2402 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2403 | if (read) |
| 2404 | memcpy(paddr, start, len - rest); |
| 2405 | else |
| 2406 | memcpy(start, paddr, len - rest); |
| 2407 | |
| 2408 | if (rest) { |
| 2409 | if (read) |
| 2410 | memcpy(paddr + len - rest, dif_storep, rest); |
| 2411 | else |
| 2412 | memcpy(dif_storep, paddr + len - rest, rest); |
| 2413 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2414 | |
Akinobu Mita | e18d8be | 2013-06-29 17:59:18 +0900 | [diff] [blame] | 2415 | sector += len / sizeof(*dif_storep); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2416 | resid -= len; |
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 | sg_miter_stop(&miter); |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2419 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2420 | |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2421 | static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec, |
| 2422 | unsigned int sectors, u32 ei_lba) |
| 2423 | { |
| 2424 | unsigned int i; |
| 2425 | struct sd_dif_tuple *sdt; |
| 2426 | sector_t sector; |
| 2427 | |
Akinobu Mita | c45eabec | 2014-02-26 22:56:58 +0900 | [diff] [blame] | 2428 | for (i = 0; i < sectors; i++, ei_lba++) { |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2429 | int ret; |
| 2430 | |
| 2431 | sector = start_sec + i; |
| 2432 | sdt = dif_store(sector); |
| 2433 | |
Akinobu Mita | 51d648a | 2013-09-18 21:27:28 +0900 | [diff] [blame] | 2434 | if (sdt->app_tag == cpu_to_be16(0xffff)) |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2435 | continue; |
| 2436 | |
| 2437 | ret = dif_verify(sdt, fake_store(sector), sector, ei_lba); |
| 2438 | if (ret) { |
| 2439 | dif_errors++; |
| 2440 | return ret; |
| 2441 | } |
Akinobu Mita | bb8c063 | 2013-09-18 21:27:25 +0900 | [diff] [blame] | 2442 | } |
| 2443 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2444 | dif_copy_prot(SCpnt, start_sec, sectors, true); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2445 | dix_reads++; |
| 2446 | |
| 2447 | return 0; |
| 2448 | } |
| 2449 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2450 | static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2451 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2452 | u8 *cmd = scp->cmnd; |
| 2453 | u64 lba; |
| 2454 | u32 num; |
| 2455 | u32 ei_lba; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2456 | unsigned long iflags; |
| 2457 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2458 | bool check_prot; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2459 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2460 | switch (cmd[0]) { |
| 2461 | case READ_16: |
| 2462 | ei_lba = 0; |
| 2463 | lba = get_unaligned_be64(cmd + 2); |
| 2464 | num = get_unaligned_be32(cmd + 10); |
| 2465 | check_prot = true; |
| 2466 | break; |
| 2467 | case READ_10: |
| 2468 | ei_lba = 0; |
| 2469 | lba = get_unaligned_be32(cmd + 2); |
| 2470 | num = get_unaligned_be16(cmd + 7); |
| 2471 | check_prot = true; |
| 2472 | break; |
| 2473 | case READ_6: |
| 2474 | ei_lba = 0; |
| 2475 | lba = (u32)cmd[3] | (u32)cmd[2] << 8 | |
| 2476 | (u32)(cmd[1] & 0x1f) << 16; |
| 2477 | num = (0 == cmd[4]) ? 256 : cmd[4]; |
| 2478 | check_prot = true; |
| 2479 | break; |
| 2480 | case READ_12: |
| 2481 | ei_lba = 0; |
| 2482 | lba = get_unaligned_be32(cmd + 2); |
| 2483 | num = get_unaligned_be32(cmd + 6); |
| 2484 | check_prot = true; |
| 2485 | break; |
| 2486 | case XDWRITEREAD_10: |
| 2487 | ei_lba = 0; |
| 2488 | lba = get_unaligned_be32(cmd + 2); |
| 2489 | num = get_unaligned_be16(cmd + 7); |
| 2490 | check_prot = false; |
| 2491 | break; |
| 2492 | default: /* assume READ(32) */ |
| 2493 | lba = get_unaligned_be64(cmd + 12); |
| 2494 | ei_lba = get_unaligned_be32(cmd + 20); |
| 2495 | num = get_unaligned_be32(cmd + 28); |
| 2496 | check_prot = false; |
| 2497 | break; |
| 2498 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2499 | if (unlikely(have_dif_prot && check_prot)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2500 | if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2501 | (cmd[1] & 0xe0)) { |
| 2502 | mk_sense_invalid_opcode(scp); |
| 2503 | return check_condition_result; |
| 2504 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2505 | if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION || |
| 2506 | sdebug_dif == SD_DIF_TYPE3_PROTECTION) && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2507 | (cmd[1] & 0xe0) == 0) |
| 2508 | sdev_printk(KERN_ERR, scp->device, "Unprotected RD " |
| 2509 | "to DIF device\n"); |
| 2510 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2511 | if (unlikely(sdebug_any_injecting_opt)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2512 | struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp); |
| 2513 | |
| 2514 | if (ep->inj_short) |
| 2515 | num /= 2; |
| 2516 | } |
| 2517 | |
| 2518 | /* inline check_device_access_params() */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2519 | if (unlikely(lba + num > sdebug_capacity)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2520 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 2521 | return check_condition_result; |
| 2522 | } |
| 2523 | /* transfer length excessive (tie in to block limits VPD page) */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2524 | if (unlikely(num > sdebug_store_sectors)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2525 | /* needs work to find which cdb byte 'num' comes from */ |
| 2526 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 2527 | return check_condition_result; |
| 2528 | } |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2529 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2530 | if (unlikely((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) && |
| 2531 | (lba <= (OPT_MEDIUM_ERR_ADDR + OPT_MEDIUM_ERR_NUM - 1)) && |
| 2532 | ((lba + num) > OPT_MEDIUM_ERR_ADDR))) { |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2533 | /* claim unrecoverable read error */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2534 | mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2535 | /* set info field and valid bit for fixed descriptor */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2536 | if (0x70 == (scp->sense_buffer[0] & 0x7f)) { |
| 2537 | scp->sense_buffer[0] |= 0x80; /* Valid bit */ |
Douglas Gilbert | 32f7ef7 | 2011-03-11 10:43:35 -0500 | [diff] [blame] | 2538 | ret = (lba < OPT_MEDIUM_ERR_ADDR) |
| 2539 | ? OPT_MEDIUM_ERR_ADDR : (int)lba; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2540 | put_unaligned_be32(ret, scp->sense_buffer + 3); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 2541 | } |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2542 | scsi_set_resid(scp, scsi_bufflen(scp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | return check_condition_result; |
| 2544 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2545 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2546 | read_lock_irqsave(&atomic_rw, iflags); |
| 2547 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2548 | /* DIX + T10 DIF */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2549 | if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2550 | int prot_ret = prot_verify_read(scp, lba, num, ei_lba); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2551 | |
| 2552 | if (prot_ret) { |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2553 | read_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2554 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2555 | return illegal_condition_result; |
| 2556 | } |
| 2557 | } |
| 2558 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2559 | ret = do_device_access(scp, lba, num, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | read_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2561 | if (unlikely(ret == -1)) |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2562 | return DID_ERROR << 16; |
| 2563 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2564 | scsi_in(scp)->resid = scsi_bufflen(scp) - ret; |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2565 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2566 | if (unlikely(sdebug_any_injecting_opt)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2567 | struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp); |
| 2568 | |
| 2569 | if (ep->inj_recovered) { |
| 2570 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 2571 | THRESHOLD_EXCEEDED, 0); |
| 2572 | return check_condition_result; |
| 2573 | } else if (ep->inj_transport) { |
| 2574 | mk_sense_buffer(scp, ABORTED_COMMAND, |
| 2575 | TRANSPORT_PROBLEM, ACK_NAK_TO); |
| 2576 | return check_condition_result; |
| 2577 | } else if (ep->inj_dif) { |
| 2578 | /* Logical block guard check failed */ |
| 2579 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); |
| 2580 | return illegal_condition_result; |
| 2581 | } else if (ep->inj_dix) { |
| 2582 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); |
| 2583 | return illegal_condition_result; |
| 2584 | } |
| 2585 | } |
Akinobu Mita | a451751 | 2013-07-08 16:01:57 -0700 | [diff] [blame] | 2586 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | } |
| 2588 | |
Tomas Winkler | 58a8635 | 2015-07-28 16:54:23 +0300 | [diff] [blame] | 2589 | static void dump_sector(unsigned char *buf, int len) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2590 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2591 | int i, j, n; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2592 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2593 | pr_err(">>> Sector Dump <<<\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2594 | for (i = 0 ; i < len ; i += 16) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2595 | char b[128]; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2596 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2597 | for (j = 0, n = 0; j < 16; j++) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2598 | unsigned char c = buf[i+j]; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2599 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2600 | if (c >= 0x20 && c < 0x7e) |
| 2601 | n += scnprintf(b + n, sizeof(b) - n, |
| 2602 | " %c ", buf[i+j]); |
| 2603 | else |
| 2604 | n += scnprintf(b + n, sizeof(b) - n, |
| 2605 | "%02x ", buf[i+j]); |
| 2606 | } |
| 2607 | pr_err("%04d: %s\n", i, b); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | 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] | 2612 | unsigned int sectors, u32 ei_lba) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2613 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2614 | int ret; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2615 | struct sd_dif_tuple *sdt; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2616 | void *daddr; |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2617 | sector_t sector = start_sec; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2618 | int ppage_offset; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2619 | int dpage_offset; |
| 2620 | struct sg_mapping_iter diter; |
| 2621 | struct sg_mapping_iter piter; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2622 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2623 | BUG_ON(scsi_sg_count(SCpnt) == 0); |
| 2624 | BUG_ON(scsi_prot_sg_count(SCpnt) == 0); |
| 2625 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2626 | sg_miter_start(&piter, scsi_prot_sglist(SCpnt), |
| 2627 | scsi_prot_sg_count(SCpnt), |
| 2628 | SG_MITER_ATOMIC | SG_MITER_FROM_SG); |
| 2629 | sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt), |
| 2630 | SG_MITER_ATOMIC | SG_MITER_FROM_SG); |
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 | /* For each protection page */ |
| 2633 | while (sg_miter_next(&piter)) { |
| 2634 | dpage_offset = 0; |
| 2635 | if (WARN_ON(!sg_miter_next(&diter))) { |
| 2636 | ret = 0x01; |
| 2637 | goto out; |
| 2638 | } |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2639 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2640 | for (ppage_offset = 0; ppage_offset < piter.length; |
| 2641 | ppage_offset += sizeof(struct sd_dif_tuple)) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2642 | /* If we're at the end of the current |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2643 | * data page advance to the next one |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2644 | */ |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2645 | if (dpage_offset >= diter.length) { |
| 2646 | if (WARN_ON(!sg_miter_next(&diter))) { |
| 2647 | ret = 0x01; |
| 2648 | goto out; |
| 2649 | } |
| 2650 | dpage_offset = 0; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2651 | } |
| 2652 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2653 | sdt = piter.addr + ppage_offset; |
| 2654 | daddr = diter.addr + dpage_offset; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2655 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2656 | ret = dif_verify(sdt, daddr, sector, ei_lba); |
Akinobu Mita | beb40ea | 2013-06-29 17:59:19 +0900 | [diff] [blame] | 2657 | if (ret) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2658 | dump_sector(daddr, sdebug_sector_size); |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 2659 | goto out; |
| 2660 | } |
| 2661 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2662 | sector++; |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 2663 | ei_lba++; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2664 | dpage_offset += sdebug_sector_size; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2665 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2666 | diter.consumed = dpage_offset; |
| 2667 | sg_miter_stop(&diter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2668 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2669 | sg_miter_stop(&piter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2670 | |
Akinobu Mita | 65f72f2a | 2013-09-18 21:27:26 +0900 | [diff] [blame] | 2671 | dif_copy_prot(SCpnt, start_sec, sectors, false); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2672 | dix_writes++; |
| 2673 | |
| 2674 | return 0; |
| 2675 | |
| 2676 | out: |
| 2677 | dif_errors++; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 2678 | sg_miter_stop(&diter); |
| 2679 | sg_miter_stop(&piter); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2680 | return ret; |
| 2681 | } |
| 2682 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2683 | static unsigned long lba_to_map_index(sector_t lba) |
| 2684 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2685 | if (sdebug_unmap_alignment) |
| 2686 | lba += sdebug_unmap_granularity - sdebug_unmap_alignment; |
| 2687 | sector_div(lba, sdebug_unmap_granularity); |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2688 | return lba; |
| 2689 | } |
| 2690 | |
| 2691 | static sector_t map_index_to_lba(unsigned long index) |
| 2692 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2693 | sector_t lba = index * sdebug_unmap_granularity; |
Akinobu Mita | a027b5b | 2013-08-26 22:08:41 +0900 | [diff] [blame] | 2694 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2695 | if (sdebug_unmap_alignment) |
| 2696 | lba -= sdebug_unmap_granularity - sdebug_unmap_alignment; |
Akinobu Mita | a027b5b | 2013-08-26 22:08:41 +0900 | [diff] [blame] | 2697 | return lba; |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2698 | } |
| 2699 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2700 | static unsigned int map_state(sector_t lba, unsigned int *num) |
| 2701 | { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2702 | sector_t end; |
| 2703 | unsigned int mapped; |
| 2704 | unsigned long index; |
| 2705 | unsigned long next; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2706 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2707 | index = lba_to_map_index(lba); |
| 2708 | mapped = test_bit(index, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2709 | |
| 2710 | if (mapped) |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2711 | next = find_next_zero_bit(map_storep, map_size, index); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2712 | else |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2713 | next = find_next_bit(map_storep, map_size, index); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2714 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2715 | 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] | 2716 | *num = end - lba; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2717 | return mapped; |
| 2718 | } |
| 2719 | |
| 2720 | static void map_region(sector_t lba, unsigned int len) |
| 2721 | { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2722 | sector_t end = lba + len; |
| 2723 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2724 | while (lba < end) { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2725 | unsigned long index = lba_to_map_index(lba); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2726 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2727 | if (index < map_size) |
| 2728 | set_bit(index, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2729 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2730 | lba = map_index_to_lba(index + 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2731 | } |
| 2732 | } |
| 2733 | |
| 2734 | static void unmap_region(sector_t lba, unsigned int len) |
| 2735 | { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2736 | sector_t end = lba + len; |
| 2737 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2738 | while (lba < end) { |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2739 | unsigned long index = lba_to_map_index(lba); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2740 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2741 | if (lba == map_index_to_lba(index) && |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2742 | lba + sdebug_unmap_granularity <= end && |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2743 | index < map_size) { |
| 2744 | clear_bit(index, map_storep); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2745 | if (sdebug_lbprz) { |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 2746 | memset(fake_storep + |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2747 | lba * sdebug_sector_size, 0, |
| 2748 | sdebug_sector_size * |
| 2749 | sdebug_unmap_granularity); |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2750 | } |
Akinobu Mita | e9926b4 | 2013-06-29 17:59:17 +0900 | [diff] [blame] | 2751 | if (dif_storep) { |
| 2752 | memset(dif_storep + lba, 0xff, |
| 2753 | sizeof(*dif_storep) * |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2754 | sdebug_unmap_granularity); |
Akinobu Mita | e9926b4 | 2013-06-29 17:59:17 +0900 | [diff] [blame] | 2755 | } |
Eric Sandeen | be1dd78 | 2012-03-08 00:03:59 -0600 | [diff] [blame] | 2756 | } |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 2757 | lba = map_index_to_lba(index + 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2758 | } |
| 2759 | } |
| 2760 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2761 | static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2763 | u8 *cmd = scp->cmnd; |
| 2764 | u64 lba; |
| 2765 | u32 num; |
| 2766 | u32 ei_lba; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2767 | unsigned long iflags; |
FUJITA Tomonori | 1978910 | 2008-03-30 00:59:56 +0900 | [diff] [blame] | 2768 | int ret; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2769 | bool check_prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2771 | switch (cmd[0]) { |
| 2772 | case WRITE_16: |
| 2773 | ei_lba = 0; |
| 2774 | lba = get_unaligned_be64(cmd + 2); |
| 2775 | num = get_unaligned_be32(cmd + 10); |
| 2776 | check_prot = true; |
| 2777 | break; |
| 2778 | case WRITE_10: |
| 2779 | ei_lba = 0; |
| 2780 | lba = get_unaligned_be32(cmd + 2); |
| 2781 | num = get_unaligned_be16(cmd + 7); |
| 2782 | check_prot = true; |
| 2783 | break; |
| 2784 | case WRITE_6: |
| 2785 | ei_lba = 0; |
| 2786 | lba = (u32)cmd[3] | (u32)cmd[2] << 8 | |
| 2787 | (u32)(cmd[1] & 0x1f) << 16; |
| 2788 | num = (0 == cmd[4]) ? 256 : cmd[4]; |
| 2789 | check_prot = true; |
| 2790 | break; |
| 2791 | case WRITE_12: |
| 2792 | ei_lba = 0; |
| 2793 | lba = get_unaligned_be32(cmd + 2); |
| 2794 | num = get_unaligned_be32(cmd + 6); |
| 2795 | check_prot = true; |
| 2796 | break; |
| 2797 | case 0x53: /* XDWRITEREAD(10) */ |
| 2798 | ei_lba = 0; |
| 2799 | lba = get_unaligned_be32(cmd + 2); |
| 2800 | num = get_unaligned_be16(cmd + 7); |
| 2801 | check_prot = false; |
| 2802 | break; |
| 2803 | default: /* assume WRITE(32) */ |
| 2804 | lba = get_unaligned_be64(cmd + 12); |
| 2805 | ei_lba = get_unaligned_be32(cmd + 20); |
| 2806 | num = get_unaligned_be32(cmd + 28); |
| 2807 | check_prot = false; |
| 2808 | break; |
| 2809 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2810 | if (unlikely(have_dif_prot && check_prot)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2811 | if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2812 | (cmd[1] & 0xe0)) { |
| 2813 | mk_sense_invalid_opcode(scp); |
| 2814 | return check_condition_result; |
| 2815 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2816 | if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION || |
| 2817 | sdebug_dif == SD_DIF_TYPE3_PROTECTION) && |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2818 | (cmd[1] & 0xe0) == 0) |
| 2819 | sdev_printk(KERN_ERR, scp->device, "Unprotected WR " |
| 2820 | "to DIF device\n"); |
| 2821 | } |
| 2822 | |
| 2823 | /* inline check_device_access_params() */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2824 | if (unlikely(lba + num > sdebug_capacity)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2825 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 2826 | return check_condition_result; |
| 2827 | } |
| 2828 | /* transfer length excessive (tie in to block limits VPD page) */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2829 | if (unlikely(num > sdebug_store_sectors)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2830 | /* needs work to find which cdb byte 'num' comes from */ |
| 2831 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 2832 | return check_condition_result; |
| 2833 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2835 | write_lock_irqsave(&atomic_rw, iflags); |
| 2836 | |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2837 | /* DIX + T10 DIF */ |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2838 | if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2839 | int prot_ret = prot_verify_write(scp, lba, num, ei_lba); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2840 | |
| 2841 | if (prot_ret) { |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 2842 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2843 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 2844 | return illegal_condition_result; |
| 2845 | } |
| 2846 | } |
| 2847 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2848 | ret = do_device_access(scp, lba, num, true); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2849 | if (unlikely(scsi_debug_lbp())) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2850 | map_region(lba, num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2852 | if (unlikely(-1 == ret)) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2853 | return DID_ERROR << 16; |
| 2854 | else if (sdebug_verbose && (ret < (num * sdebug_sector_size))) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2855 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2856 | "%s: write: cdb indicated=%u, IO sent=%d bytes\n", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2857 | my_name, num * sdebug_sector_size, ret); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2858 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 2859 | if (unlikely(sdebug_any_injecting_opt)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2860 | struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp); |
| 2861 | |
| 2862 | if (ep->inj_recovered) { |
| 2863 | mk_sense_buffer(scp, RECOVERED_ERROR, |
| 2864 | THRESHOLD_EXCEEDED, 0); |
| 2865 | return check_condition_result; |
| 2866 | } else if (ep->inj_dif) { |
| 2867 | /* Logical block guard check failed */ |
| 2868 | mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1); |
| 2869 | return illegal_condition_result; |
| 2870 | } else if (ep->inj_dix) { |
| 2871 | mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1); |
| 2872 | return illegal_condition_result; |
| 2873 | } |
| 2874 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | return 0; |
| 2876 | } |
| 2877 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2878 | static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num, |
| 2879 | u32 ei_lba, bool unmap, bool ndob) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2880 | { |
| 2881 | unsigned long iflags; |
| 2882 | unsigned long long i; |
| 2883 | int ret; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2884 | u64 lba_off; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2885 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2886 | ret = check_device_access_params(scp, lba, num); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2887 | if (ret) |
| 2888 | return ret; |
| 2889 | |
| 2890 | write_lock_irqsave(&atomic_rw, iflags); |
| 2891 | |
Akinobu Mita | 9ed8d3d | 2013-04-16 22:11:55 +0900 | [diff] [blame] | 2892 | if (unmap && scsi_debug_lbp()) { |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2893 | unmap_region(lba, num); |
| 2894 | goto out; |
| 2895 | } |
| 2896 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2897 | lba_off = lba * sdebug_sector_size; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2898 | /* if ndob then zero 1 logical block, else fetch 1 logical block */ |
| 2899 | if (ndob) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2900 | memset(fake_storep + lba_off, 0, sdebug_sector_size); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2901 | ret = 0; |
| 2902 | } else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2903 | ret = fetch_to_dev_buffer(scp, fake_storep + lba_off, |
| 2904 | sdebug_sector_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2905 | |
| 2906 | if (-1 == ret) { |
| 2907 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2908 | return DID_ERROR << 16; |
| 2909 | } else if (sdebug_verbose && (ret < (num * sdebug_sector_size))) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2910 | sdev_printk(KERN_INFO, scp->device, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 2911 | "%s: %s: cdb indicated=%u, IO sent=%d bytes\n", |
| 2912 | my_name, "write same", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2913 | num * sdebug_sector_size, ret); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2914 | |
| 2915 | /* Copy first sector to remaining blocks */ |
| 2916 | for (i = 1 ; i < num ; i++) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2917 | memcpy(fake_storep + ((lba + i) * sdebug_sector_size), |
| 2918 | fake_storep + lba_off, |
| 2919 | sdebug_sector_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2920 | |
Akinobu Mita | 9ed8d3d | 2013-04-16 22:11:55 +0900 | [diff] [blame] | 2921 | if (scsi_debug_lbp()) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 2922 | map_region(lba, num); |
| 2923 | out: |
| 2924 | write_unlock_irqrestore(&atomic_rw, iflags); |
| 2925 | |
| 2926 | return 0; |
| 2927 | } |
| 2928 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2929 | static int resp_write_same_10(struct scsi_cmnd *scp, |
| 2930 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2931 | { |
| 2932 | u8 *cmd = scp->cmnd; |
| 2933 | u32 lba; |
| 2934 | u16 num; |
| 2935 | u32 ei_lba = 0; |
| 2936 | bool unmap = false; |
| 2937 | |
| 2938 | if (cmd[1] & 0x8) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2939 | if (sdebug_lbpws10 == 0) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2940 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); |
| 2941 | return check_condition_result; |
| 2942 | } else |
| 2943 | unmap = true; |
| 2944 | } |
| 2945 | lba = get_unaligned_be32(cmd + 2); |
| 2946 | num = get_unaligned_be16(cmd + 7); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2947 | if (num > sdebug_write_same_length) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2948 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); |
| 2949 | return check_condition_result; |
| 2950 | } |
| 2951 | return resp_write_same(scp, lba, num, ei_lba, unmap, false); |
| 2952 | } |
| 2953 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2954 | static int resp_write_same_16(struct scsi_cmnd *scp, |
| 2955 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2956 | { |
| 2957 | u8 *cmd = scp->cmnd; |
| 2958 | u64 lba; |
| 2959 | u32 num; |
| 2960 | u32 ei_lba = 0; |
| 2961 | bool unmap = false; |
| 2962 | bool ndob = false; |
| 2963 | |
| 2964 | if (cmd[1] & 0x8) { /* UNMAP */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2965 | if (sdebug_lbpws == 0) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2966 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3); |
| 2967 | return check_condition_result; |
| 2968 | } else |
| 2969 | unmap = true; |
| 2970 | } |
| 2971 | if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */ |
| 2972 | ndob = true; |
| 2973 | lba = get_unaligned_be64(cmd + 2); |
| 2974 | num = get_unaligned_be32(cmd + 10); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 2975 | if (num > sdebug_write_same_length) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 2976 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1); |
| 2977 | return check_condition_result; |
| 2978 | } |
| 2979 | return resp_write_same(scp, lba, num, ei_lba, unmap, ndob); |
| 2980 | } |
| 2981 | |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 2982 | /* Note the mode field is in the same position as the (lower) service action |
| 2983 | * field. For the Report supported operation codes command, SPC-4 suggests |
| 2984 | * each mode of this command should be reported separately; for future. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 2985 | static int resp_write_buffer(struct scsi_cmnd *scp, |
| 2986 | struct sdebug_dev_info *devip) |
Ewan D. Milne | acafd0b | 2014-12-04 11:49:28 -0500 | [diff] [blame] | 2987 | { |
| 2988 | u8 *cmd = scp->cmnd; |
| 2989 | struct scsi_device *sdp = scp->device; |
| 2990 | struct sdebug_dev_info *dp; |
| 2991 | u8 mode; |
| 2992 | |
| 2993 | mode = cmd[1] & 0x1f; |
| 2994 | switch (mode) { |
| 2995 | case 0x4: /* download microcode (MC) and activate (ACT) */ |
| 2996 | /* set UAs on this device only */ |
| 2997 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 2998 | set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm); |
| 2999 | break; |
| 3000 | case 0x5: /* download MC, save and ACT */ |
| 3001 | set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm); |
| 3002 | break; |
| 3003 | case 0x6: /* download MC with offsets and ACT */ |
| 3004 | /* set UAs on most devices (LUs) in this target */ |
| 3005 | list_for_each_entry(dp, |
| 3006 | &devip->sdbg_host->dev_info_list, |
| 3007 | dev_list) |
| 3008 | if (dp->target == sdp->id) { |
| 3009 | set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm); |
| 3010 | if (devip != dp) |
| 3011 | set_bit(SDEBUG_UA_MICROCODE_CHANGED, |
| 3012 | dp->uas_bm); |
| 3013 | } |
| 3014 | break; |
| 3015 | case 0x7: /* download MC with offsets, save, and ACT */ |
| 3016 | /* set UA on all devices (LUs) in this target */ |
| 3017 | list_for_each_entry(dp, |
| 3018 | &devip->sdbg_host->dev_info_list, |
| 3019 | dev_list) |
| 3020 | if (dp->target == sdp->id) |
| 3021 | set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, |
| 3022 | dp->uas_bm); |
| 3023 | break; |
| 3024 | default: |
| 3025 | /* do nothing for this command for other mode values */ |
| 3026 | break; |
| 3027 | } |
| 3028 | return 0; |
| 3029 | } |
| 3030 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3031 | static int resp_comp_write(struct scsi_cmnd *scp, |
| 3032 | struct sdebug_dev_info *devip) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3033 | { |
| 3034 | u8 *cmd = scp->cmnd; |
| 3035 | u8 *arr; |
| 3036 | u8 *fake_storep_hold; |
| 3037 | u64 lba; |
| 3038 | u32 dnum; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3039 | u32 lb_size = sdebug_sector_size; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3040 | u8 num; |
| 3041 | unsigned long iflags; |
| 3042 | int ret; |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3043 | int retval = 0; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3044 | |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3045 | lba = get_unaligned_be64(cmd + 2); |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3046 | num = cmd[13]; /* 1 to a maximum of 255 logical blocks */ |
| 3047 | if (0 == num) |
| 3048 | return 0; /* degenerate case, not an error */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3049 | if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3050 | (cmd[1] & 0xe0)) { |
| 3051 | mk_sense_invalid_opcode(scp); |
| 3052 | return check_condition_result; |
| 3053 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3054 | if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION || |
| 3055 | sdebug_dif == SD_DIF_TYPE3_PROTECTION) && |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3056 | (cmd[1] & 0xe0) == 0) |
| 3057 | sdev_printk(KERN_ERR, scp->device, "Unprotected WR " |
| 3058 | "to DIF device\n"); |
| 3059 | |
| 3060 | /* inline check_device_access_params() */ |
| 3061 | if (lba + num > sdebug_capacity) { |
| 3062 | mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0); |
| 3063 | return check_condition_result; |
| 3064 | } |
| 3065 | /* transfer length excessive (tie in to block limits VPD page) */ |
| 3066 | if (num > sdebug_store_sectors) { |
| 3067 | /* needs work to find which cdb byte 'num' comes from */ |
| 3068 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0); |
| 3069 | return check_condition_result; |
| 3070 | } |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3071 | dnum = 2 * num; |
| 3072 | arr = kzalloc(dnum * lb_size, GFP_ATOMIC); |
| 3073 | if (NULL == arr) { |
| 3074 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3075 | INSUFF_RES_ASCQ); |
| 3076 | return check_condition_result; |
| 3077 | } |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3078 | |
| 3079 | write_lock_irqsave(&atomic_rw, iflags); |
| 3080 | |
| 3081 | /* trick do_device_access() to fetch both compare and write buffers |
| 3082 | * from data-in into arr. Safe (atomic) since write_lock held. */ |
| 3083 | fake_storep_hold = fake_storep; |
| 3084 | fake_storep = arr; |
| 3085 | ret = do_device_access(scp, 0, dnum, true); |
| 3086 | fake_storep = fake_storep_hold; |
| 3087 | if (ret == -1) { |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3088 | retval = DID_ERROR << 16; |
| 3089 | goto cleanup; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3090 | } else if (sdebug_verbose && (ret < (dnum * lb_size))) |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3091 | sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb " |
| 3092 | "indicated=%u, IO sent=%d bytes\n", my_name, |
| 3093 | dnum * lb_size, ret); |
| 3094 | if (!comp_write_worker(lba, num, arr)) { |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3095 | mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3096 | retval = check_condition_result; |
| 3097 | goto cleanup; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3098 | } |
| 3099 | if (scsi_debug_lbp()) |
| 3100 | map_region(lba, num); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3101 | cleanup: |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3102 | write_unlock_irqrestore(&atomic_rw, iflags); |
Douglas Gilbert | d467d31 | 2014-11-26 12:33:48 -0500 | [diff] [blame] | 3103 | kfree(arr); |
| 3104 | return retval; |
Douglas Gilbert | 38d5c83 | 2014-11-24 21:27:12 -0500 | [diff] [blame] | 3105 | } |
| 3106 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3107 | struct unmap_block_desc { |
| 3108 | __be64 lba; |
| 3109 | __be32 blocks; |
| 3110 | __be32 __reserved; |
| 3111 | }; |
| 3112 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3113 | static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3114 | { |
| 3115 | unsigned char *buf; |
| 3116 | struct unmap_block_desc *desc; |
| 3117 | unsigned int i, payload_len, descriptors; |
| 3118 | int ret; |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3119 | unsigned long iflags; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3120 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3121 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3122 | if (!scsi_debug_lbp()) |
| 3123 | return 0; /* fib and say its done */ |
| 3124 | payload_len = get_unaligned_be16(scp->cmnd + 7); |
| 3125 | BUG_ON(scsi_bufflen(scp) != payload_len); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3126 | |
| 3127 | descriptors = (payload_len - 8) / 16; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3128 | if (descriptors > sdebug_unmap_max_desc) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3129 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3130 | return check_condition_result; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3131 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3132 | |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 3133 | buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3134 | if (!buf) { |
| 3135 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3136 | INSUFF_RES_ASCQ); |
| 3137 | return check_condition_result; |
| 3138 | } |
| 3139 | |
| 3140 | scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp)); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3141 | |
| 3142 | BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2); |
| 3143 | BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16); |
| 3144 | |
| 3145 | desc = (void *)&buf[8]; |
| 3146 | |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3147 | write_lock_irqsave(&atomic_rw, iflags); |
| 3148 | |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3149 | for (i = 0 ; i < descriptors ; i++) { |
| 3150 | unsigned long long lba = get_unaligned_be64(&desc[i].lba); |
| 3151 | unsigned int num = get_unaligned_be32(&desc[i].blocks); |
| 3152 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3153 | ret = check_device_access_params(scp, lba, num); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3154 | if (ret) |
| 3155 | goto out; |
| 3156 | |
| 3157 | unmap_region(lba, num); |
| 3158 | } |
| 3159 | |
| 3160 | ret = 0; |
| 3161 | |
| 3162 | out: |
Akinobu Mita | 6c78cc0 | 2014-02-26 22:57:03 +0900 | [diff] [blame] | 3163 | write_unlock_irqrestore(&atomic_rw, iflags); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3164 | kfree(buf); |
| 3165 | |
| 3166 | return ret; |
| 3167 | } |
| 3168 | |
| 3169 | #define SDEBUG_GET_LBA_STATUS_LEN 32 |
| 3170 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3171 | static int resp_get_lba_status(struct scsi_cmnd *scp, |
| 3172 | struct sdebug_dev_info *devip) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3173 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3174 | u8 *cmd = scp->cmnd; |
| 3175 | u64 lba; |
| 3176 | u32 alloc_len, mapped, num; |
| 3177 | u8 arr[SDEBUG_GET_LBA_STATUS_LEN]; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3178 | int ret; |
| 3179 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3180 | lba = get_unaligned_be64(cmd + 2); |
| 3181 | alloc_len = get_unaligned_be32(cmd + 10); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3182 | |
| 3183 | if (alloc_len < 24) |
| 3184 | return 0; |
| 3185 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3186 | ret = check_device_access_params(scp, lba, 1); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3187 | if (ret) |
| 3188 | return ret; |
| 3189 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3190 | if (scsi_debug_lbp()) |
| 3191 | mapped = map_state(lba, &num); |
| 3192 | else { |
| 3193 | mapped = 1; |
| 3194 | /* following just in case virtual_gb changed */ |
| 3195 | sdebug_capacity = get_sdebug_capacity(); |
| 3196 | if (sdebug_capacity - lba <= 0xffffffff) |
| 3197 | num = sdebug_capacity - lba; |
| 3198 | else |
| 3199 | num = 0xffffffff; |
| 3200 | } |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3201 | |
| 3202 | memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3203 | put_unaligned_be32(20, arr); /* Parameter Data Length */ |
| 3204 | put_unaligned_be64(lba, arr + 8); /* LBA */ |
| 3205 | put_unaligned_be32(num, arr + 16); /* Number of blocks */ |
| 3206 | arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */ |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 3207 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3208 | 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] | 3209 | } |
| 3210 | |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame^] | 3211 | /* Even though each pseudo target has a REPORT LUNS "well known logical unit" |
| 3212 | * (W-LUN), the normal Linux scanning logic does not associate it with a |
| 3213 | * device (e.g. /dev/sg7). The following magic will make that association: |
| 3214 | * "cd /sys/class/scsi_host/host<n> ; echo '- - 49409' > scan" |
| 3215 | * where <n> is a host number. If there are multiple targets in a host then |
| 3216 | * the above will associate a W-LUN to each target. To only get a W-LUN |
| 3217 | * for target 2, then use "echo '- 2 49409' > scan" . |
| 3218 | */ |
| 3219 | static int resp_report_luns(struct scsi_cmnd *scp, |
| 3220 | struct sdebug_dev_info *devip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | { |
Douglas Gilbert | 01123ef | 2014-08-05 12:20:02 +0200 | [diff] [blame] | 3222 | unsigned char *cmd = scp->cmnd; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame^] | 3223 | unsigned int alloc_len; |
| 3224 | unsigned char select_report; |
| 3225 | u64 lun; |
| 3226 | struct scsi_lun *lun_p; |
| 3227 | u8 *arr; |
| 3228 | unsigned int lun_cnt; /* normal LUN count (max: 256) */ |
| 3229 | unsigned int wlun_cnt; /* report luns W-LUN count */ |
| 3230 | unsigned int tlun_cnt; /* total LUN count */ |
| 3231 | unsigned int rlen; /* response length (in bytes) */ |
| 3232 | int i, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3233 | |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 3234 | clear_luns_changed_on_target(devip); |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame^] | 3235 | |
| 3236 | select_report = cmd[2]; |
| 3237 | alloc_len = get_unaligned_be32(cmd + 6); |
| 3238 | |
| 3239 | if (alloc_len < 4) { |
| 3240 | pr_err("alloc len too small %d\n", alloc_len); |
| 3241 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3242 | return check_condition_result; |
| 3243 | } |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame^] | 3244 | |
| 3245 | switch (select_report) { |
| 3246 | case 0: /* all LUNs apart from W-LUNs */ |
| 3247 | lun_cnt = sdebug_max_luns; |
| 3248 | wlun_cnt = 0; |
| 3249 | break; |
| 3250 | case 1: /* only W-LUNs */ |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3251 | lun_cnt = 0; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame^] | 3252 | wlun_cnt = 1; |
| 3253 | break; |
| 3254 | case 2: /* all LUNs */ |
| 3255 | lun_cnt = sdebug_max_luns; |
| 3256 | wlun_cnt = 1; |
| 3257 | break; |
| 3258 | case 0x10: /* only administrative LUs */ |
| 3259 | case 0x11: /* see SPC-5 */ |
| 3260 | case 0x12: /* only subsiduary LUs owned by referenced LU */ |
| 3261 | default: |
| 3262 | pr_debug("select report invalid %d\n", select_report); |
| 3263 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1); |
| 3264 | return check_condition_result; |
| 3265 | } |
| 3266 | |
| 3267 | if (sdebug_no_lun_0 && (lun_cnt > 0)) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3268 | --lun_cnt; |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame^] | 3269 | |
| 3270 | tlun_cnt = lun_cnt + wlun_cnt; |
| 3271 | |
| 3272 | rlen = (tlun_cnt * sizeof(struct scsi_lun)) + 8; |
| 3273 | arr = vmalloc(rlen); |
| 3274 | if (!arr) { |
| 3275 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3276 | INSUFF_RES_ASCQ); |
| 3277 | return check_condition_result; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3278 | } |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame^] | 3279 | memset(arr, 0, rlen); |
| 3280 | pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n", |
| 3281 | select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0); |
| 3282 | |
| 3283 | /* luns start at byte 8 in response following the header */ |
| 3284 | lun_p = (struct scsi_lun *)&arr[8]; |
| 3285 | |
| 3286 | /* LUNs use single level peripheral device addressing method */ |
| 3287 | lun = sdebug_no_lun_0 ? 1 : 0; |
| 3288 | for (i = 0; i < lun_cnt; i++) |
| 3289 | int_to_scsilun(lun++, lun_p++); |
| 3290 | |
| 3291 | if (wlun_cnt) |
| 3292 | int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p++); |
| 3293 | |
| 3294 | put_unaligned_be32(rlen - 8, &arr[0]); |
| 3295 | |
| 3296 | res = fill_from_dev_buffer(scp, arr, rlen); |
| 3297 | vfree(arr); |
| 3298 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | } |
| 3300 | |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3301 | static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba, |
| 3302 | unsigned int num, struct sdebug_dev_info *devip) |
| 3303 | { |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3304 | int j; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3305 | unsigned char *kaddr, *buf; |
| 3306 | unsigned int offset; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3307 | struct scsi_data_buffer *sdb = scsi_in(scp); |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3308 | struct sg_mapping_iter miter; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3309 | |
| 3310 | /* better not to use temporary buffer. */ |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 3311 | buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC); |
Akinobu Mita | c5af0db | 2014-02-26 22:57:01 +0900 | [diff] [blame] | 3312 | if (!buf) { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 3313 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3314 | INSUFF_RES_ASCQ); |
Akinobu Mita | c5af0db | 2014-02-26 22:57:01 +0900 | [diff] [blame] | 3315 | return check_condition_result; |
| 3316 | } |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3317 | |
FUJITA Tomonori | 21a6182 | 2008-03-09 13:44:30 +0900 | [diff] [blame] | 3318 | scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp)); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3319 | |
| 3320 | offset = 0; |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3321 | sg_miter_start(&miter, sdb->table.sgl, sdb->table.nents, |
| 3322 | SG_MITER_ATOMIC | SG_MITER_TO_SG); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3323 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3324 | while (sg_miter_next(&miter)) { |
| 3325 | kaddr = miter.addr; |
| 3326 | for (j = 0; j < miter.length; j++) |
| 3327 | *(kaddr + j) ^= *(buf + offset + j); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3328 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3329 | offset += miter.length; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3330 | } |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3331 | sg_miter_stop(&miter); |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3332 | kfree(buf); |
| 3333 | |
Akinobu Mita | be4e11b | 2014-02-26 22:57:02 +0900 | [diff] [blame] | 3334 | return 0; |
FUJITA Tomonori | c639d14 | 2008-01-23 01:32:01 +0900 | [diff] [blame] | 3335 | } |
| 3336 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3337 | static int resp_xdwriteread_10(struct scsi_cmnd *scp, |
| 3338 | struct sdebug_dev_info *devip) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3339 | { |
| 3340 | u8 *cmd = scp->cmnd; |
| 3341 | u64 lba; |
| 3342 | u32 num; |
| 3343 | int errsts; |
| 3344 | |
| 3345 | if (!scsi_bidi_cmnd(scp)) { |
| 3346 | mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, |
| 3347 | INSUFF_RES_ASCQ); |
| 3348 | return check_condition_result; |
| 3349 | } |
| 3350 | errsts = resp_read_dt0(scp, devip); |
| 3351 | if (errsts) |
| 3352 | return errsts; |
| 3353 | if (!(cmd[1] & 0x4)) { /* DISABLE_WRITE is not set */ |
| 3354 | errsts = resp_write_dt0(scp, devip); |
| 3355 | if (errsts) |
| 3356 | return errsts; |
| 3357 | } |
| 3358 | lba = get_unaligned_be32(cmd + 2); |
| 3359 | num = get_unaligned_be16(cmd + 7); |
| 3360 | return resp_xdwriteread(scp, lba, num, devip); |
| 3361 | } |
| 3362 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3363 | /* Queued command completions converge here. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3364 | static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3365 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3366 | int qa_indx; |
| 3367 | int retiring = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | unsigned long iflags; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3369 | struct sdebug_queued_cmd *sqcp; |
| 3370 | struct scsi_cmnd *scp; |
| 3371 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3372 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3373 | atomic_inc(&sdebug_completions); |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3374 | qa_indx = sd_dp->qa_indx; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3375 | if (unlikely((qa_indx < 0) || (qa_indx >= SCSI_DEBUG_CANQUEUE))) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3376 | pr_err("wild qa_indx=%d\n", qa_indx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | return; |
| 3378 | } |
| 3379 | spin_lock_irqsave(&queued_arr_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3380 | sqcp = &queued_arr[qa_indx]; |
| 3381 | scp = sqcp->a_cmnd; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3382 | if (unlikely(scp == NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3384 | pr_err("scp is NULL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | return; |
| 3386 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3387 | devip = (struct sdebug_dev_info *)scp->device->hostdata; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3388 | if (likely(devip)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3389 | atomic_dec(&devip->num_in_q); |
| 3390 | else |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3391 | pr_err("devip=NULL\n"); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3392 | if (unlikely(atomic_read(&retired_max_queue) > 0)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3393 | retiring = 1; |
| 3394 | |
| 3395 | sqcp->a_cmnd = NULL; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3396 | if (unlikely(!test_and_clear_bit(qa_indx, queued_in_use_bm))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3397 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3398 | pr_err("Unexpected completion\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3399 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3401 | |
| 3402 | if (unlikely(retiring)) { /* user has reduced max_queue */ |
| 3403 | int k, retval; |
| 3404 | |
| 3405 | retval = atomic_read(&retired_max_queue); |
| 3406 | if (qa_indx >= retval) { |
| 3407 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3408 | pr_err("index %d too large\n", retval); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3409 | return; |
| 3410 | } |
| 3411 | k = find_last_bit(queued_in_use_bm, retval); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3412 | if ((k < sdebug_max_queue) || (k == retval)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3413 | atomic_set(&retired_max_queue, 0); |
| 3414 | else |
| 3415 | atomic_set(&retired_max_queue, k + 1); |
| 3416 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3418 | scp->scsi_done(scp); /* callback to mid level */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3419 | } |
| 3420 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3421 | /* When high resolution timer goes off this function is called. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3422 | static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3423 | { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3424 | struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer, |
| 3425 | hrt); |
| 3426 | sdebug_q_cmd_complete(sd_dp); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3427 | return HRTIMER_NORESTART; |
| 3428 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3430 | /* When work queue schedules work, it calls this function. */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3431 | static void sdebug_q_cmd_wq_complete(struct work_struct *work) |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3432 | { |
| 3433 | struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer, |
| 3434 | ew.work); |
| 3435 | sdebug_q_cmd_complete(sd_dp); |
| 3436 | } |
| 3437 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3438 | static struct sdebug_dev_info *sdebug_device_create( |
| 3439 | struct sdebug_host_info *sdbg_host, gfp_t flags) |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3440 | { |
| 3441 | struct sdebug_dev_info *devip; |
| 3442 | |
| 3443 | devip = kzalloc(sizeof(*devip), flags); |
| 3444 | if (devip) { |
| 3445 | devip->sdbg_host = sdbg_host; |
| 3446 | list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list); |
| 3447 | } |
| 3448 | return devip; |
| 3449 | } |
| 3450 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3451 | static struct sdebug_dev_info *find_build_dev_info(struct scsi_device *sdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3453 | struct sdebug_host_info *sdbg_host; |
| 3454 | struct sdebug_dev_info *open_devip = NULL; |
| 3455 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | |
FUJITA Tomonori | d1e4c9c | 2008-03-02 18:30:18 +0900 | [diff] [blame] | 3457 | sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host); |
| 3458 | if (!sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3459 | pr_err("Host info NULL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | return NULL; |
| 3461 | } |
| 3462 | list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) { |
| 3463 | if ((devip->used) && (devip->channel == sdev->channel) && |
| 3464 | (devip->target == sdev->id) && |
| 3465 | (devip->lun == sdev->lun)) |
| 3466 | return devip; |
| 3467 | else { |
| 3468 | if ((!devip->used) && (!open_devip)) |
| 3469 | open_devip = devip; |
| 3470 | } |
| 3471 | } |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 3472 | if (!open_devip) { /* try and make a new one */ |
| 3473 | open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC); |
| 3474 | if (!open_devip) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3475 | pr_err("out of memory at line %d\n", __LINE__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3476 | return NULL; |
| 3477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3478 | } |
FUJITA Tomonori | a75869d | 2008-03-20 11:09:17 +0900 | [diff] [blame] | 3479 | |
| 3480 | open_devip->channel = sdev->channel; |
| 3481 | open_devip->target = sdev->id; |
| 3482 | open_devip->lun = sdev->lun; |
| 3483 | open_devip->sdbg_host = sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3484 | atomic_set(&open_devip->num_in_q, 0); |
| 3485 | set_bit(SDEBUG_UA_POR, open_devip->uas_bm); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3486 | open_devip->used = true; |
FUJITA Tomonori | a75869d | 2008-03-20 11:09:17 +0900 | [diff] [blame] | 3487 | return open_devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3488 | } |
| 3489 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3490 | static int scsi_debug_slave_alloc(struct scsi_device *sdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3491 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3492 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3493 | pr_info("slave_alloc <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3494 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 3495 | queue_flag_set_unlocked(QUEUE_FLAG_BIDI, sdp->request_queue); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3496 | return 0; |
| 3497 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3499 | static int scsi_debug_slave_configure(struct scsi_device *sdp) |
| 3500 | { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3501 | struct sdebug_dev_info *devip = |
| 3502 | (struct sdebug_dev_info *)sdp->hostdata; |
FUJITA Tomonori | a34c4e9 | 2008-03-25 09:26:50 +0900 | [diff] [blame] | 3503 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3504 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3505 | pr_info("slave_configure <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3506 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3507 | if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN) |
| 3508 | sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN; |
| 3509 | if (devip == NULL) { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3510 | devip = find_build_dev_info(sdp); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3511 | if (devip == NULL) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3512 | return 1; /* no resources, will be marked offline */ |
| 3513 | } |
Christoph Hellwig | c8b09f6 | 2014-11-03 20:15:14 +0100 | [diff] [blame] | 3514 | sdp->hostdata = devip; |
Akinobu Mita | 6bb5e6e | 2014-06-02 22:56:49 +0900 | [diff] [blame] | 3515 | blk_queue_max_segment_size(sdp->request_queue, -1U); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3516 | if (sdebug_no_uld) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 3517 | sdp->no_uld_attach = 1; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3518 | return 0; |
| 3519 | } |
| 3520 | |
| 3521 | static void scsi_debug_slave_destroy(struct scsi_device *sdp) |
| 3522 | { |
| 3523 | struct sdebug_dev_info *devip = |
| 3524 | (struct sdebug_dev_info *)sdp->hostdata; |
| 3525 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3526 | if (sdebug_verbose) |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3527 | pr_info("slave_destroy <%u %u %u %llu>\n", |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3528 | sdp->host->host_no, sdp->channel, sdp->id, sdp->lun); |
| 3529 | if (devip) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3530 | /* make this slot available for re-use */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 3531 | devip->used = false; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3532 | sdp->hostdata = NULL; |
| 3533 | } |
| 3534 | } |
| 3535 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3536 | /* If @cmnd found deletes its timer or work queue and returns true; else |
| 3537 | returns false */ |
| 3538 | static bool stop_queued_cmnd(struct scsi_cmnd *cmnd) |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3539 | { |
| 3540 | unsigned long iflags; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3541 | int k, qmax, r_qmax; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3542 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3543 | struct sdebug_dev_info *devip; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3544 | struct sdebug_defer *sd_dp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3545 | |
| 3546 | spin_lock_irqsave(&queued_arr_lock, iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3547 | qmax = sdebug_max_queue; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3548 | r_qmax = atomic_read(&retired_max_queue); |
| 3549 | if (r_qmax > qmax) |
| 3550 | qmax = r_qmax; |
| 3551 | for (k = 0; k < qmax; ++k) { |
| 3552 | if (test_bit(k, queued_in_use_bm)) { |
| 3553 | sqcp = &queued_arr[k]; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3554 | if (cmnd != sqcp->a_cmnd) |
| 3555 | continue; |
| 3556 | /* found command */ |
| 3557 | devip = (struct sdebug_dev_info *) |
| 3558 | cmnd->device->hostdata; |
| 3559 | if (devip) |
| 3560 | atomic_dec(&devip->num_in_q); |
| 3561 | sqcp->a_cmnd = NULL; |
| 3562 | sd_dp = sqcp->sd_dp; |
| 3563 | spin_unlock_irqrestore(&queued_arr_lock, |
| 3564 | iflags); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3565 | if (sdebug_jdelay > 0 || sdebug_ndelay > 0) { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3566 | if (sd_dp) |
| 3567 | hrtimer_cancel(&sd_dp->hrt); |
| 3568 | } else if (sdebug_jdelay < 0) { |
| 3569 | if (sd_dp) |
| 3570 | cancel_work_sync(&sd_dp->ew.work); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3571 | } |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3572 | clear_bit(k, queued_in_use_bm); |
| 3573 | return true; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3574 | } |
| 3575 | } |
| 3576 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3577 | return false; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3578 | } |
| 3579 | |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3580 | /* Deletes (stops) timers or work queues of all queued commands */ |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3581 | static void stop_all_queued(void) |
| 3582 | { |
| 3583 | unsigned long iflags; |
| 3584 | int k; |
| 3585 | struct sdebug_queued_cmd *sqcp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3586 | struct sdebug_dev_info *devip; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3587 | struct sdebug_defer *sd_dp; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3588 | |
| 3589 | spin_lock_irqsave(&queued_arr_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3590 | for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) { |
| 3591 | if (test_bit(k, queued_in_use_bm)) { |
| 3592 | sqcp = &queued_arr[k]; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3593 | if (NULL == sqcp->a_cmnd) |
| 3594 | continue; |
| 3595 | devip = (struct sdebug_dev_info *) |
| 3596 | sqcp->a_cmnd->device->hostdata; |
| 3597 | if (devip) |
| 3598 | atomic_dec(&devip->num_in_q); |
| 3599 | sqcp->a_cmnd = NULL; |
| 3600 | sd_dp = sqcp->sd_dp; |
| 3601 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3602 | if (sdebug_jdelay > 0 || sdebug_ndelay > 0) { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3603 | if (sd_dp) |
| 3604 | hrtimer_cancel(&sd_dp->hrt); |
| 3605 | } else if (sdebug_jdelay < 0) { |
| 3606 | if (sd_dp) |
| 3607 | cancel_work_sync(&sd_dp->ew.work); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3608 | } |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3609 | clear_bit(k, queued_in_use_bm); |
| 3610 | spin_lock_irqsave(&queued_arr_lock, iflags); |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 3611 | } |
| 3612 | } |
| 3613 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | } |
| 3615 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3616 | /* Free queued command memory on heap */ |
| 3617 | static void free_all_queued(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3619 | int k; |
| 3620 | struct sdebug_queued_cmd *sqcp; |
| 3621 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3622 | for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) { |
| 3623 | sqcp = &queued_arr[k]; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3624 | kfree(sqcp->sd_dp); |
| 3625 | sqcp->sd_dp = NULL; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3626 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | } |
| 3628 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3629 | static int scsi_debug_abort(struct scsi_cmnd *SCpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3631 | bool ok; |
| 3632 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3633 | ++num_aborts; |
| 3634 | if (SCpnt) { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3635 | ok = stop_queued_cmnd(SCpnt); |
| 3636 | if (SCpnt->device && (SDEBUG_OPT_ALL_NOISE & sdebug_opts)) |
| 3637 | sdev_printk(KERN_INFO, SCpnt->device, |
| 3638 | "%s: command%s found\n", __func__, |
| 3639 | ok ? "" : " not"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3641 | return SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt) |
| 3645 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3646 | ++num_dev_resets; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3647 | if (SCpnt && SCpnt->device) { |
| 3648 | struct scsi_device *sdp = SCpnt->device; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3649 | struct sdebug_dev_info *devip = |
| 3650 | (struct sdebug_dev_info *)sdp->hostdata; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3651 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3652 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3653 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | if (devip) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3655 | set_bit(SDEBUG_UA_POR, devip->uas_bm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3656 | } |
| 3657 | return SUCCESS; |
| 3658 | } |
| 3659 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3660 | static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt) |
| 3661 | { |
| 3662 | struct sdebug_host_info *sdbg_host; |
| 3663 | struct sdebug_dev_info *devip; |
| 3664 | struct scsi_device *sdp; |
| 3665 | struct Scsi_Host *hp; |
| 3666 | int k = 0; |
| 3667 | |
| 3668 | ++num_target_resets; |
| 3669 | if (!SCpnt) |
| 3670 | goto lie; |
| 3671 | sdp = SCpnt->device; |
| 3672 | if (!sdp) |
| 3673 | goto lie; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3674 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3675 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
| 3676 | hp = sdp->host; |
| 3677 | if (!hp) |
| 3678 | goto lie; |
| 3679 | sdbg_host = *(struct sdebug_host_info **)shost_priv(hp); |
| 3680 | if (sdbg_host) { |
| 3681 | list_for_each_entry(devip, |
| 3682 | &sdbg_host->dev_info_list, |
| 3683 | dev_list) |
| 3684 | if (devip->target == sdp->id) { |
| 3685 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3686 | ++k; |
| 3687 | } |
| 3688 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3689 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3690 | sdev_printk(KERN_INFO, sdp, |
| 3691 | "%s: %d device(s) found in target\n", __func__, k); |
| 3692 | lie: |
| 3693 | return SUCCESS; |
| 3694 | } |
| 3695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt) |
| 3697 | { |
| 3698 | struct sdebug_host_info *sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3699 | struct sdebug_dev_info *devip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3700 | struct scsi_device * sdp; |
| 3701 | struct Scsi_Host * hp; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3702 | int k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | ++num_bus_resets; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3705 | if (!(SCpnt && SCpnt->device)) |
| 3706 | goto lie; |
| 3707 | sdp = SCpnt->device; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3708 | if (SDEBUG_OPT_ALL_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3709 | sdev_printk(KERN_INFO, sdp, "%s\n", __func__); |
| 3710 | hp = sdp->host; |
| 3711 | if (hp) { |
FUJITA Tomonori | d1e4c9c | 2008-03-02 18:30:18 +0900 | [diff] [blame] | 3712 | sdbg_host = *(struct sdebug_host_info **)shost_priv(hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | if (sdbg_host) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3714 | list_for_each_entry(devip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3715 | &sdbg_host->dev_info_list, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3716 | dev_list) { |
| 3717 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3718 | ++k; |
| 3719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | } |
| 3721 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3722 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3723 | sdev_printk(KERN_INFO, sdp, |
| 3724 | "%s: %d device(s) found in host\n", __func__, k); |
| 3725 | lie: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3726 | return SUCCESS; |
| 3727 | } |
| 3728 | |
| 3729 | static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt) |
| 3730 | { |
| 3731 | struct sdebug_host_info * sdbg_host; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3732 | struct sdebug_dev_info *devip; |
| 3733 | int k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3735 | ++num_host_resets; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3736 | if ((SCpnt->device) && (SDEBUG_OPT_ALL_NOISE & sdebug_opts)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3737 | sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | spin_lock(&sdebug_host_list_lock); |
| 3739 | list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3740 | list_for_each_entry(devip, &sdbg_host->dev_info_list, |
| 3741 | dev_list) { |
| 3742 | set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm); |
| 3743 | ++k; |
| 3744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | } |
| 3746 | spin_unlock(&sdebug_host_list_lock); |
| 3747 | stop_all_queued(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3748 | if (SDEBUG_OPT_RESET_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3749 | sdev_printk(KERN_INFO, SCpnt->device, |
| 3750 | "%s: %d device(s) found\n", __func__, k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3751 | return SUCCESS; |
| 3752 | } |
| 3753 | |
FUJITA Tomonori | f58b0ef | 2008-03-30 00:59:54 +0900 | [diff] [blame] | 3754 | static void __init sdebug_build_parts(unsigned char *ramp, |
FUJITA Tomonori | 5f2578e | 2008-03-30 00:59:57 +0900 | [diff] [blame] | 3755 | unsigned long store_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | { |
| 3757 | struct partition * pp; |
| 3758 | int starts[SDEBUG_MAX_PARTS + 2]; |
| 3759 | int sectors_per_part, num_sectors, k; |
| 3760 | int heads_by_sects, start_sec, end_sec; |
| 3761 | |
| 3762 | /* assume partition table already zeroed */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3763 | if ((sdebug_num_parts < 1) || (store_size < 1048576)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | return; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3765 | if (sdebug_num_parts > SDEBUG_MAX_PARTS) { |
| 3766 | sdebug_num_parts = SDEBUG_MAX_PARTS; |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 3767 | pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3769 | num_sectors = (int)sdebug_store_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | sectors_per_part = (num_sectors - sdebug_sectors_per) |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3771 | / sdebug_num_parts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | heads_by_sects = sdebug_heads * sdebug_sectors_per; |
| 3773 | starts[0] = sdebug_sectors_per; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3774 | for (k = 1; k < sdebug_num_parts; ++k) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3775 | starts[k] = ((k * sectors_per_part) / heads_by_sects) |
| 3776 | * heads_by_sects; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3777 | starts[sdebug_num_parts] = num_sectors; |
| 3778 | starts[sdebug_num_parts + 1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3779 | |
| 3780 | ramp[510] = 0x55; /* magic partition markings */ |
| 3781 | ramp[511] = 0xAA; |
| 3782 | pp = (struct partition *)(ramp + 0x1be); |
| 3783 | for (k = 0; starts[k + 1]; ++k, ++pp) { |
| 3784 | start_sec = starts[k]; |
| 3785 | end_sec = starts[k + 1] - 1; |
| 3786 | pp->boot_ind = 0; |
| 3787 | |
| 3788 | pp->cyl = start_sec / heads_by_sects; |
| 3789 | pp->head = (start_sec - (pp->cyl * heads_by_sects)) |
| 3790 | / sdebug_sectors_per; |
| 3791 | pp->sector = (start_sec % sdebug_sectors_per) + 1; |
| 3792 | |
| 3793 | pp->end_cyl = end_sec / heads_by_sects; |
| 3794 | pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects)) |
| 3795 | / sdebug_sectors_per; |
| 3796 | pp->end_sector = (end_sec % sdebug_sectors_per) + 1; |
| 3797 | |
Akinobu Mita | 150c354 | 2013-08-26 22:08:40 +0900 | [diff] [blame] | 3798 | pp->start_sect = cpu_to_le32(start_sec); |
| 3799 | pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3800 | pp->sys_ind = 0x83; /* plain Linux partition */ |
| 3801 | } |
| 3802 | } |
| 3803 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 3804 | static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip, |
| 3805 | int scsi_result, int delta_jiff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3806 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3807 | unsigned long iflags; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3808 | int k, num_in_q, qdepth, inject; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3809 | struct sdebug_queued_cmd *sqcp = NULL; |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 3810 | struct scsi_device *sdp; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3811 | struct sdebug_defer *sd_dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3813 | if (unlikely(WARN_ON(!cmnd))) |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 3814 | return SCSI_MLQUEUE_HOST_BUSY; |
| 3815 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3816 | if (unlikely(devip == NULL)) { |
| 3817 | if (scsi_result == 0) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3818 | scsi_result = DID_NO_CONNECT << 16; |
| 3819 | goto respond_in_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3820 | } |
Tomas Winkler | 299b6c0 | 2015-07-28 16:54:24 +0300 | [diff] [blame] | 3821 | |
| 3822 | sdp = cmnd->device; |
| 3823 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3824 | if (unlikely(sdebug_verbose && scsi_result)) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3825 | sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n", |
| 3826 | __func__, scsi_result); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3827 | if (delta_jiff == 0) |
| 3828 | goto respond_in_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3830 | /* schedule the response at a later time if resources permit */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3831 | spin_lock_irqsave(&queued_arr_lock, iflags); |
| 3832 | num_in_q = atomic_read(&devip->num_in_q); |
| 3833 | qdepth = cmnd->device->queue_depth; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3834 | inject = 0; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3835 | if (unlikely((qdepth > 0) && (num_in_q >= qdepth))) { |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3836 | if (scsi_result) { |
| 3837 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
| 3838 | goto respond_in_thread; |
| 3839 | } else |
| 3840 | scsi_result = device_qfull_result; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3841 | } else if (unlikely((sdebug_every_nth != 0) && |
| 3842 | (SDEBUG_OPT_RARE_TSF & sdebug_opts) && |
| 3843 | (scsi_result == 0))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3844 | if ((num_in_q == (qdepth - 1)) && |
| 3845 | (atomic_inc_return(&sdebug_a_tsf) >= |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3846 | abs(sdebug_every_nth))) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3847 | atomic_set(&sdebug_a_tsf, 0); |
| 3848 | inject = 1; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3849 | scsi_result = device_qfull_result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3850 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3851 | } |
| 3852 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3853 | k = find_first_zero_bit(queued_in_use_bm, sdebug_max_queue); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3854 | if (unlikely(k >= sdebug_max_queue)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3856 | if (scsi_result) |
| 3857 | goto respond_in_thread; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3858 | else if (SDEBUG_OPT_ALL_TSF & sdebug_opts) |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3859 | scsi_result = device_qfull_result; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3860 | if (SDEBUG_OPT_Q_NOISE & sdebug_opts) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3861 | sdev_printk(KERN_INFO, sdp, |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3862 | "%s: max_queue=%d exceeded, %s\n", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3863 | __func__, sdebug_max_queue, |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3864 | (scsi_result ? "status: TASK SET FULL" : |
| 3865 | "report: host busy")); |
| 3866 | if (scsi_result) |
| 3867 | goto respond_in_thread; |
| 3868 | else |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3869 | return SCSI_MLQUEUE_HOST_BUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3870 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3871 | __set_bit(k, queued_in_use_bm); |
| 3872 | atomic_inc(&devip->num_in_q); |
| 3873 | sqcp = &queued_arr[k]; |
| 3874 | sqcp->a_cmnd = cmnd; |
| 3875 | cmnd->result = scsi_result; |
| 3876 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3877 | sd_dp = sqcp->sd_dp; |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3878 | if (delta_jiff > 0 || sdebug_ndelay > 0) { |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 3879 | ktime_t kt; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3880 | |
Douglas Gilbert | b333a81 | 2016-04-25 12:16:30 -0400 | [diff] [blame] | 3881 | if (delta_jiff > 0) { |
| 3882 | struct timespec ts; |
| 3883 | |
| 3884 | jiffies_to_timespec(delta_jiff, &ts); |
| 3885 | kt = ktime_set(ts.tv_sec, ts.tv_nsec); |
| 3886 | } else |
| 3887 | kt = ktime_set(0, sdebug_ndelay); |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3888 | if (NULL == sd_dp) { |
| 3889 | sd_dp = kzalloc(sizeof(*sd_dp), GFP_ATOMIC); |
| 3890 | if (NULL == sd_dp) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3891 | return SCSI_MLQUEUE_HOST_BUSY; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3892 | sqcp->sd_dp = sd_dp; |
| 3893 | hrtimer_init(&sd_dp->hrt, CLOCK_MONOTONIC, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3894 | HRTIMER_MODE_REL); |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3895 | sd_dp->hrt.function = sdebug_q_cmd_hrt_complete; |
| 3896 | sd_dp->qa_indx = k; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3897 | } |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3898 | hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL); |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 3899 | } else { /* jdelay < 0 */ |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3900 | if (NULL == sd_dp) { |
| 3901 | sd_dp = kzalloc(sizeof(*sqcp->sd_dp), GFP_ATOMIC); |
| 3902 | if (NULL == sd_dp) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3903 | return SCSI_MLQUEUE_HOST_BUSY; |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3904 | sqcp->sd_dp = sd_dp; |
| 3905 | sd_dp->qa_indx = k; |
| 3906 | INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3907 | } |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 3908 | schedule_work(&sd_dp->ew.work); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3909 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 3910 | if (unlikely((SDEBUG_OPT_Q_NOISE & sdebug_opts) && |
| 3911 | (scsi_result == device_qfull_result))) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3912 | sdev_printk(KERN_INFO, sdp, |
| 3913 | "%s: num_in_q=%d +1, %s%s\n", __func__, |
| 3914 | num_in_q, (inject ? "<inject> " : ""), |
| 3915 | "status: TASK SET FULL"); |
| 3916 | return 0; |
Douglas Gilbert | cd62b7d | 2014-08-05 12:20:46 +0200 | [diff] [blame] | 3917 | |
| 3918 | respond_in_thread: /* call back to mid-layer using invocation thread */ |
| 3919 | cmnd->result = scsi_result; |
| 3920 | cmnd->scsi_done(cmnd); |
| 3921 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3922 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3923 | |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 3924 | /* Note: The following macros create attribute files in the |
| 3925 | /sys/module/scsi_debug/parameters directory. Unfortunately this |
| 3926 | driver is unaware of a change and cannot trigger auxiliary actions |
| 3927 | as it can when the corresponding attribute in the |
| 3928 | /sys/bus/pseudo/drivers/scsi_debug directory is changed. |
| 3929 | */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3930 | module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR); |
| 3931 | module_param_named(ato, sdebug_ato, int, S_IRUGO); |
| 3932 | module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR); |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 3933 | module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3934 | module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO); |
| 3935 | module_param_named(dif, sdebug_dif, int, S_IRUGO); |
| 3936 | module_param_named(dix, sdebug_dix, int, S_IRUGO); |
| 3937 | module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR); |
| 3938 | module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR); |
| 3939 | module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR); |
| 3940 | module_param_named(guard, sdebug_guard, uint, S_IRUGO); |
| 3941 | module_param_named(host_lock, sdebug_host_lock, bool, S_IRUGO | S_IWUSR); |
| 3942 | module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO); |
| 3943 | module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO); |
| 3944 | module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO); |
| 3945 | module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO); |
| 3946 | module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO); |
| 3947 | module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR); |
| 3948 | module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR); |
| 3949 | module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR); |
| 3950 | module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR); |
| 3951 | module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO); |
| 3952 | module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO); |
| 3953 | module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR); |
| 3954 | module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO); |
| 3955 | module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR); |
| 3956 | module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO); |
| 3957 | module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR); |
| 3958 | module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR); |
| 3959 | module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO); |
| 3960 | module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO); |
| 3961 | module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR); |
| 3962 | module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO); |
| 3963 | module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO); |
| 3964 | module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO); |
| 3965 | module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO); |
| 3966 | module_param_named(virtual_gb, sdebug_virtual_gb, int, S_IRUGO | S_IWUSR); |
| 3967 | module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int, |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 3968 | S_IRUGO | S_IWUSR); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 3969 | module_param_named(write_same_length, sdebug_write_same_length, int, |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 3970 | S_IRUGO | S_IWUSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | |
| 3972 | MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert"); |
| 3973 | MODULE_DESCRIPTION("SCSI debug adapter driver"); |
| 3974 | MODULE_LICENSE("GPL"); |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 3975 | MODULE_VERSION(SDEBUG_VERSION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3976 | |
| 3977 | MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 3978 | 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] | 3979 | MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 3980 | 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] | 3981 | 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] | 3982 | MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)"); |
| 3983 | MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3984 | MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)"); |
Randy Dunlap | beb87c3 | 2007-06-11 11:36:40 -0700 | [diff] [blame] | 3985 | MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)"); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 3986 | 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] | 3987 | MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)"); |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 3988 | MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)"); |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 3989 | MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)"); |
| 3990 | MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)"); |
| 3991 | 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] | 3992 | 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] | 3993 | MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3994 | 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] | 3995 | MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))"); |
| 3996 | MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 3997 | 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] | 3998 | 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] | 3999 | MODULE_PARM_DESC(num_parts, "number of partitions(def=0)"); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4000 | 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] | 4001 | MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)"); |
Douglas Gilbert | 6f3cbf5 | 2007-01-05 00:05:25 -0500 | [diff] [blame] | 4002 | 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] | 4003 | MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4004 | MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])"); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4005 | MODULE_PARM_DESC(removable, "claim to have removable media (def=0)"); |
Douglas Gilbert | e46b034 | 2014-08-05 12:21:53 +0200 | [diff] [blame] | 4006 | 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] | 4007 | MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4008 | 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] | 4009 | MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)"); |
| 4010 | 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] | 4011 | MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)"); |
| 4012 | 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] | 4013 | 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] | 4014 | MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)"); |
| 4015 | 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] | 4016 | |
| 4017 | static char sdebug_info[256]; |
| 4018 | |
| 4019 | static const char * scsi_debug_info(struct Scsi_Host * shp) |
| 4020 | { |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4021 | sprintf(sdebug_info, |
| 4022 | "scsi_debug, version %s [%s], dev_size_mb=%d, opts=0x%x", |
| 4023 | SDEBUG_VERSION, sdebug_version_date, sdebug_dev_size_mb, |
| 4024 | sdebug_opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4025 | return sdebug_info; |
| 4026 | } |
| 4027 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4028 | /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */ |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4029 | static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer, |
| 4030 | int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | { |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4032 | char arr[16]; |
| 4033 | int opts; |
| 4034 | int minLen = length > 15 ? 15 : length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4035 | |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4036 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 4037 | return -EACCES; |
| 4038 | memcpy(arr, buffer, minLen); |
| 4039 | arr[minLen] = '\0'; |
| 4040 | if (1 != sscanf(arr, "%d", &opts)) |
| 4041 | return -EINVAL; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4042 | sdebug_opts = opts; |
| 4043 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); |
| 4044 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts); |
| 4045 | if (sdebug_every_nth != 0) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4046 | atomic_set(&sdebug_cmnd_count, 0); |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4047 | return length; |
| 4048 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4050 | /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the |
| 4051 | * same for each scsi_debug host (if more than one). Some of the counters |
| 4052 | * output are not atomics so might be inaccurate in a busy system. */ |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4053 | static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host) |
| 4054 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4055 | int f, l; |
| 4056 | char b[32]; |
| 4057 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4058 | if (sdebug_every_nth > 0) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4059 | snprintf(b, sizeof(b), " (curr:%d)", |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4060 | ((SDEBUG_OPT_RARE_TSF & sdebug_opts) ? |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4061 | atomic_read(&sdebug_a_tsf) : |
| 4062 | atomic_read(&sdebug_cmnd_count))); |
| 4063 | else |
| 4064 | b[0] = '\0'; |
| 4065 | |
| 4066 | seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n" |
| 4067 | "num_tgts=%d, shared (ram) size=%d MB, opts=0x%x, " |
| 4068 | "every_nth=%d%s\n" |
| 4069 | "delay=%d, ndelay=%d, max_luns=%d, q_completions=%d\n" |
| 4070 | "sector_size=%d bytes, cylinders=%d, heads=%d, sectors=%d\n" |
| 4071 | "command aborts=%d; RESETs: device=%d, target=%d, bus=%d, " |
| 4072 | "host=%d\ndix_reads=%d dix_writes=%d dif_errors=%d " |
| 4073 | "usec_in_jiffy=%lu\n", |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4074 | SDEBUG_VERSION, sdebug_version_date, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4075 | sdebug_num_tgts, sdebug_dev_size_mb, sdebug_opts, |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4076 | sdebug_every_nth, b, sdebug_jdelay, sdebug_ndelay, |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4077 | sdebug_max_luns, atomic_read(&sdebug_completions), |
| 4078 | sdebug_sector_size, sdebug_cylinders_per, sdebug_heads, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4079 | sdebug_sectors_per, num_aborts, num_dev_resets, |
| 4080 | num_target_resets, num_bus_resets, num_host_resets, |
| 4081 | dix_reads, dix_writes, dif_errors, TICK_NSEC / 1000); |
| 4082 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4083 | f = find_first_bit(queued_in_use_bm, sdebug_max_queue); |
| 4084 | if (f != sdebug_max_queue) { |
| 4085 | l = find_last_bit(queued_in_use_bm, sdebug_max_queue); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4086 | seq_printf(m, " %s BUSY: first,last bits set: %d,%d\n", |
| 4087 | "queued_in_use_bm", f, l); |
| 4088 | } |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 4089 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4090 | } |
| 4091 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4092 | static ssize_t delay_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | { |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4094 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4095 | } |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4096 | /* Returns -EBUSY if jdelay is being changed and commands are queued */ |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4097 | static ssize_t delay_store(struct device_driver *ddp, const char *buf, |
| 4098 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4099 | { |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4100 | int jdelay, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4102 | if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4103 | res = count; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4104 | if (sdebug_jdelay != jdelay) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4105 | unsigned long iflags; |
| 4106 | int k; |
| 4107 | |
| 4108 | spin_lock_irqsave(&queued_arr_lock, iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4109 | k = find_first_bit(queued_in_use_bm, sdebug_max_queue); |
| 4110 | if (k != sdebug_max_queue) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4111 | res = -EBUSY; /* have queued commands */ |
| 4112 | else { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4113 | /* make sure sdebug_defer instances get |
| 4114 | * re-allocated for new delay variant */ |
| 4115 | free_all_queued(); |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4116 | sdebug_jdelay = jdelay; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4117 | sdebug_ndelay = 0; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4118 | } |
| 4119 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4120 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4121 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4122 | } |
| 4123 | return -EINVAL; |
| 4124 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4125 | static DRIVER_ATTR_RW(delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4127 | static ssize_t ndelay_show(struct device_driver *ddp, char *buf) |
| 4128 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4129 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4130 | } |
| 4131 | /* Returns -EBUSY if ndelay is being changed and commands are queued */ |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4132 | /* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4133 | static ssize_t ndelay_store(struct device_driver *ddp, const char *buf, |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4134 | size_t count) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4135 | { |
| 4136 | unsigned long iflags; |
| 4137 | int ndelay, res, k; |
| 4138 | |
| 4139 | if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) && |
| 4140 | (ndelay >= 0) && (ndelay < 1000000000)) { |
| 4141 | res = count; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4142 | if (sdebug_ndelay != ndelay) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4143 | spin_lock_irqsave(&queued_arr_lock, iflags); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4144 | k = find_first_bit(queued_in_use_bm, sdebug_max_queue); |
| 4145 | if (k != sdebug_max_queue) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4146 | res = -EBUSY; /* have queued commands */ |
| 4147 | else { |
Douglas Gilbert | a10bc12 | 2016-04-25 12:16:32 -0400 | [diff] [blame] | 4148 | /* make sure sdebug_defer instances get |
| 4149 | * re-allocated for new delay variant */ |
| 4150 | free_all_queued(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4151 | sdebug_ndelay = ndelay; |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4152 | sdebug_jdelay = ndelay ? JDELAY_OVERRIDDEN |
| 4153 | : DEF_JDELAY; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4154 | } |
| 4155 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
| 4156 | } |
| 4157 | return res; |
| 4158 | } |
| 4159 | return -EINVAL; |
| 4160 | } |
| 4161 | static DRIVER_ATTR_RW(ndelay); |
| 4162 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4163 | static ssize_t opts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4164 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4165 | return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | } |
| 4167 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4168 | static ssize_t opts_store(struct device_driver *ddp, const char *buf, |
| 4169 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | { |
| 4171 | int opts; |
| 4172 | char work[20]; |
| 4173 | |
| 4174 | if (1 == sscanf(buf, "%10s", work)) { |
Rasmus Villemoes | 48a9687 | 2014-10-13 15:54:44 -0700 | [diff] [blame] | 4175 | if (0 == strncasecmp(work,"0x", 2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4176 | if (1 == sscanf(&work[2], "%x", &opts)) |
| 4177 | goto opts_done; |
| 4178 | } else { |
| 4179 | if (1 == sscanf(work, "%d", &opts)) |
| 4180 | goto opts_done; |
| 4181 | } |
| 4182 | } |
| 4183 | return -EINVAL; |
| 4184 | opts_done: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4185 | sdebug_opts = opts; |
| 4186 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts); |
| 4187 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4188 | atomic_set(&sdebug_cmnd_count, 0); |
| 4189 | atomic_set(&sdebug_a_tsf, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4190 | return count; |
| 4191 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4192 | static DRIVER_ATTR_RW(opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4194 | static ssize_t ptype_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4196 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4198 | static ssize_t ptype_store(struct device_driver *ddp, const char *buf, |
| 4199 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4200 | { |
| 4201 | int n; |
| 4202 | |
| 4203 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4204 | sdebug_ptype = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | return count; |
| 4206 | } |
| 4207 | return -EINVAL; |
| 4208 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4209 | static DRIVER_ATTR_RW(ptype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4211 | static ssize_t dsense_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4212 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4213 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4214 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4215 | static ssize_t dsense_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 n; |
| 4219 | |
| 4220 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4221 | sdebug_dsense = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4222 | return count; |
| 4223 | } |
| 4224 | return -EINVAL; |
| 4225 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4226 | static DRIVER_ATTR_RW(dsense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4228 | static ssize_t fake_rw_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4229 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4230 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4231 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4232 | static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf, |
| 4233 | size_t count) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4234 | { |
| 4235 | int n; |
| 4236 | |
| 4237 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4238 | n = (n > 0); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4239 | sdebug_fake_rw = (sdebug_fake_rw > 0); |
| 4240 | if (sdebug_fake_rw != n) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4241 | if ((0 == n) && (NULL == fake_storep)) { |
| 4242 | unsigned long sz = |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4243 | (unsigned long)sdebug_dev_size_mb * |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4244 | 1048576; |
| 4245 | |
| 4246 | fake_storep = vmalloc(sz); |
| 4247 | if (NULL == fake_storep) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4248 | pr_err("out of memory, 9\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4249 | return -ENOMEM; |
| 4250 | } |
| 4251 | memset(fake_storep, 0, sz); |
| 4252 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4253 | sdebug_fake_rw = n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4254 | } |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4255 | return count; |
| 4256 | } |
| 4257 | return -EINVAL; |
| 4258 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4259 | static DRIVER_ATTR_RW(fake_rw); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4260 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4261 | 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] | 4262 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4263 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4264 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4265 | static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf, |
| 4266 | size_t count) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4267 | { |
| 4268 | int n; |
| 4269 | |
| 4270 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4271 | sdebug_no_lun_0 = n; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4272 | return count; |
| 4273 | } |
| 4274 | return -EINVAL; |
| 4275 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4276 | static DRIVER_ATTR_RW(no_lun_0); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4277 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4278 | static ssize_t num_tgts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4280 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4282 | static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf, |
| 4283 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4284 | { |
| 4285 | int n; |
| 4286 | |
| 4287 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4288 | sdebug_num_tgts = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4289 | sdebug_max_tgts_luns(); |
| 4290 | return count; |
| 4291 | } |
| 4292 | return -EINVAL; |
| 4293 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4294 | static DRIVER_ATTR_RW(num_tgts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4296 | 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] | 4297 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4298 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4300 | static DRIVER_ATTR_RO(dev_size_mb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4302 | static ssize_t num_parts_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4304 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4306 | static DRIVER_ATTR_RO(num_parts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4308 | static ssize_t every_nth_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4310 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4312 | static ssize_t every_nth_store(struct device_driver *ddp, const char *buf, |
| 4313 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | { |
| 4315 | int nth; |
| 4316 | |
| 4317 | if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4318 | sdebug_every_nth = nth; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4319 | atomic_set(&sdebug_cmnd_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | return count; |
| 4321 | } |
| 4322 | return -EINVAL; |
| 4323 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4324 | static DRIVER_ATTR_RW(every_nth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4326 | static ssize_t max_luns_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4328 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4330 | static ssize_t max_luns_store(struct device_driver *ddp, const char *buf, |
| 4331 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | { |
| 4333 | int n; |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 4334 | bool changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | |
| 4336 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame^] | 4337 | if (n > 256) { |
| 4338 | pr_warn("max_luns can be no more than 256\n"); |
| 4339 | return -EINVAL; |
| 4340 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4341 | changed = (sdebug_max_luns != n); |
| 4342 | sdebug_max_luns = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | sdebug_max_tgts_luns(); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4344 | if (changed && (sdebug_scsi_level >= 5)) { /* >= SPC-3 */ |
Ewan D. Milne | 19c8ead | 2014-12-04 11:49:27 -0500 | [diff] [blame] | 4345 | struct sdebug_host_info *sdhp; |
| 4346 | struct sdebug_dev_info *dp; |
| 4347 | |
| 4348 | spin_lock(&sdebug_host_list_lock); |
| 4349 | list_for_each_entry(sdhp, &sdebug_host_list, |
| 4350 | host_list) { |
| 4351 | list_for_each_entry(dp, &sdhp->dev_info_list, |
| 4352 | dev_list) { |
| 4353 | set_bit(SDEBUG_UA_LUNS_CHANGED, |
| 4354 | dp->uas_bm); |
| 4355 | } |
| 4356 | } |
| 4357 | spin_unlock(&sdebug_host_list_lock); |
| 4358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4359 | return count; |
| 4360 | } |
| 4361 | return -EINVAL; |
| 4362 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4363 | static DRIVER_ATTR_RW(max_luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4364 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4365 | static ssize_t max_queue_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4366 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4367 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4368 | } |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4369 | /* N.B. max_queue can be changed while there are queued commands. In flight |
| 4370 | * commands beyond the new max_queue will be completed. */ |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4371 | static ssize_t max_queue_store(struct device_driver *ddp, const char *buf, |
| 4372 | size_t count) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4373 | { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4374 | unsigned long iflags; |
| 4375 | int n, k; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4376 | |
| 4377 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) && |
| 4378 | (n <= SCSI_DEBUG_CANQUEUE)) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4379 | spin_lock_irqsave(&queued_arr_lock, iflags); |
| 4380 | k = find_last_bit(queued_in_use_bm, SCSI_DEBUG_CANQUEUE); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4381 | sdebug_max_queue = n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4382 | if (SCSI_DEBUG_CANQUEUE == k) |
| 4383 | atomic_set(&retired_max_queue, 0); |
| 4384 | else if (k >= n) |
| 4385 | atomic_set(&retired_max_queue, k + 1); |
| 4386 | else |
| 4387 | atomic_set(&retired_max_queue, 0); |
| 4388 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4389 | return count; |
| 4390 | } |
| 4391 | return -EINVAL; |
| 4392 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4393 | static DRIVER_ATTR_RW(max_queue); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4394 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4395 | static ssize_t no_uld_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4396 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4397 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4398 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4399 | static DRIVER_ATTR_RO(no_uld); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 4400 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4401 | static ssize_t scsi_level_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4403 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4405 | static DRIVER_ATTR_RO(scsi_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4407 | static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4408 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4409 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4410 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4411 | static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf, |
| 4412 | size_t count) |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4413 | { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4414 | int n; |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4415 | bool changed; |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4416 | |
| 4417 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4418 | changed = (sdebug_virtual_gb != n); |
| 4419 | sdebug_virtual_gb = n; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4420 | sdebug_capacity = get_sdebug_capacity(); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4421 | if (changed) { |
| 4422 | struct sdebug_host_info *sdhp; |
| 4423 | struct sdebug_dev_info *dp; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4424 | |
Ewan D. Milne | 4bc6b63 | 2014-12-04 11:49:26 -0500 | [diff] [blame] | 4425 | spin_lock(&sdebug_host_list_lock); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4426 | list_for_each_entry(sdhp, &sdebug_host_list, |
| 4427 | host_list) { |
| 4428 | list_for_each_entry(dp, &sdhp->dev_info_list, |
| 4429 | dev_list) { |
| 4430 | set_bit(SDEBUG_UA_CAPACITY_CHANGED, |
| 4431 | dp->uas_bm); |
| 4432 | } |
| 4433 | } |
Ewan D. Milne | 4bc6b63 | 2014-12-04 11:49:26 -0500 | [diff] [blame] | 4434 | spin_unlock(&sdebug_host_list_lock); |
Douglas Gilbert | 0d01c5d | 2014-11-24 20:27:51 -0500 | [diff] [blame] | 4435 | } |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4436 | return count; |
| 4437 | } |
| 4438 | return -EINVAL; |
| 4439 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4440 | static DRIVER_ATTR_RW(virtual_gb); |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4441 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4442 | static ssize_t add_host_show(struct device_driver *ddp, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4444 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4445 | } |
| 4446 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4447 | static int sdebug_add_adapter(void); |
| 4448 | static void sdebug_remove_adapter(void); |
| 4449 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4450 | static ssize_t add_host_store(struct device_driver *ddp, const char *buf, |
| 4451 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | { |
FUJITA Tomonori | f3df41c | 2008-03-20 11:09:15 +0900 | [diff] [blame] | 4453 | int delta_hosts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | |
FUJITA Tomonori | f3df41c | 2008-03-20 11:09:15 +0900 | [diff] [blame] | 4455 | if (sscanf(buf, "%d", &delta_hosts) != 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | if (delta_hosts > 0) { |
| 4458 | do { |
| 4459 | sdebug_add_adapter(); |
| 4460 | } while (--delta_hosts); |
| 4461 | } else if (delta_hosts < 0) { |
| 4462 | do { |
| 4463 | sdebug_remove_adapter(); |
| 4464 | } while (++delta_hosts); |
| 4465 | } |
| 4466 | return count; |
| 4467 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4468 | static DRIVER_ATTR_RW(add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4470 | 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] | 4471 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4472 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4473 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4474 | static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf, |
| 4475 | size_t count) |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4476 | { |
| 4477 | int n; |
| 4478 | |
| 4479 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4480 | sdebug_vpd_use_hostno = n; |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4481 | return count; |
| 4482 | } |
| 4483 | return -EINVAL; |
| 4484 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4485 | static DRIVER_ATTR_RW(vpd_use_hostno); |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4486 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4487 | 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] | 4488 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4489 | return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size); |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4490 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4491 | static DRIVER_ATTR_RO(sector_size); |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4492 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4493 | static ssize_t dix_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4494 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4495 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4496 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4497 | static DRIVER_ATTR_RO(dix); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4498 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4499 | static ssize_t dif_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4500 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4501 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4502 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4503 | static DRIVER_ATTR_RO(dif); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4504 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4505 | static ssize_t guard_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4506 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4507 | return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4508 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4509 | static DRIVER_ATTR_RO(guard); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4510 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4511 | static ssize_t ato_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4512 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4513 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4514 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4515 | static DRIVER_ATTR_RO(ato); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4516 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4517 | static ssize_t map_show(struct device_driver *ddp, char *buf) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4518 | { |
| 4519 | ssize_t count; |
| 4520 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4521 | if (!scsi_debug_lbp()) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4522 | return scnprintf(buf, PAGE_SIZE, "0-%u\n", |
| 4523 | sdebug_store_sectors); |
| 4524 | |
Tejun Heo | c7badc9 | 2015-02-13 14:37:51 -0800 | [diff] [blame] | 4525 | count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", |
| 4526 | (int)map_size, map_storep); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4527 | buf[count++] = '\n'; |
Tejun Heo | c7badc9 | 2015-02-13 14:37:51 -0800 | [diff] [blame] | 4528 | buf[count] = '\0'; |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4529 | |
| 4530 | return count; |
| 4531 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4532 | static DRIVER_ATTR_RO(map); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4533 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4534 | static ssize_t removable_show(struct device_driver *ddp, char *buf) |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4535 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4536 | return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4537 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4538 | static ssize_t removable_store(struct device_driver *ddp, const char *buf, |
| 4539 | size_t count) |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4540 | { |
| 4541 | int n; |
| 4542 | |
| 4543 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4544 | sdebug_removable = (n > 0); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4545 | return count; |
| 4546 | } |
| 4547 | return -EINVAL; |
| 4548 | } |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4549 | static DRIVER_ATTR_RW(removable); |
Martin Pitt | d986788 | 2012-09-06 12:04:33 +0200 | [diff] [blame] | 4550 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4551 | static ssize_t host_lock_show(struct device_driver *ddp, char *buf) |
| 4552 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4553 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4554 | } |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 4555 | /* N.B. sdebug_host_lock does nothing, kept for backward compatibility */ |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4556 | static ssize_t host_lock_store(struct device_driver *ddp, const char *buf, |
| 4557 | size_t count) |
| 4558 | { |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 4559 | int n; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4560 | |
| 4561 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 4562 | sdebug_host_lock = (n > 0); |
| 4563 | return count; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4564 | } |
| 4565 | return -EINVAL; |
| 4566 | } |
| 4567 | static DRIVER_ATTR_RW(host_lock); |
| 4568 | |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4569 | static ssize_t strict_show(struct device_driver *ddp, char *buf) |
| 4570 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4571 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4572 | } |
| 4573 | static ssize_t strict_store(struct device_driver *ddp, const char *buf, |
| 4574 | size_t count) |
| 4575 | { |
| 4576 | int n; |
| 4577 | |
| 4578 | if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4579 | sdebug_strict = (n > 0); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4580 | return count; |
| 4581 | } |
| 4582 | return -EINVAL; |
| 4583 | } |
| 4584 | static DRIVER_ATTR_RW(strict); |
| 4585 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4586 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4587 | /* Note: The following array creates attribute files in the |
Douglas Gilbert | 2318391 | 2006-09-16 20:30:47 -0400 | [diff] [blame] | 4588 | /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these |
| 4589 | files (over those found in the /sys/module/scsi_debug/parameters |
| 4590 | directory) is that auxiliary actions can be triggered when an attribute |
| 4591 | is changed. For example see: sdebug_add_host_store() above. |
| 4592 | */ |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4593 | |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4594 | static struct attribute *sdebug_drv_attrs[] = { |
| 4595 | &driver_attr_delay.attr, |
| 4596 | &driver_attr_opts.attr, |
| 4597 | &driver_attr_ptype.attr, |
| 4598 | &driver_attr_dsense.attr, |
| 4599 | &driver_attr_fake_rw.attr, |
| 4600 | &driver_attr_no_lun_0.attr, |
| 4601 | &driver_attr_num_tgts.attr, |
| 4602 | &driver_attr_dev_size_mb.attr, |
| 4603 | &driver_attr_num_parts.attr, |
| 4604 | &driver_attr_every_nth.attr, |
| 4605 | &driver_attr_max_luns.attr, |
| 4606 | &driver_attr_max_queue.attr, |
| 4607 | &driver_attr_no_uld.attr, |
| 4608 | &driver_attr_scsi_level.attr, |
| 4609 | &driver_attr_virtual_gb.attr, |
| 4610 | &driver_attr_add_host.attr, |
| 4611 | &driver_attr_vpd_use_hostno.attr, |
| 4612 | &driver_attr_sector_size.attr, |
| 4613 | &driver_attr_dix.attr, |
| 4614 | &driver_attr_dif.attr, |
| 4615 | &driver_attr_guard.attr, |
| 4616 | &driver_attr_ato.attr, |
| 4617 | &driver_attr_map.attr, |
| 4618 | &driver_attr_removable.attr, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4619 | &driver_attr_host_lock.attr, |
| 4620 | &driver_attr_ndelay.attr, |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4621 | &driver_attr_strict.attr, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 4622 | NULL, |
| 4623 | }; |
| 4624 | ATTRIBUTE_GROUPS(sdebug_drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | |
Akinobu Mita | 11ddcec | 2014-02-26 22:56:59 +0900 | [diff] [blame] | 4626 | static struct device *pseudo_primary; |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 4627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | static int __init scsi_debug_init(void) |
| 4629 | { |
FUJITA Tomonori | 5f2578e | 2008-03-30 00:59:57 +0900 | [diff] [blame] | 4630 | unsigned long sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | int host_to_add; |
| 4632 | int k; |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4633 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4635 | atomic_set(&sdebug_cmnd_count, 0); |
| 4636 | atomic_set(&sdebug_completions, 0); |
| 4637 | atomic_set(&retired_max_queue, 0); |
| 4638 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4639 | if (sdebug_ndelay >= 1000 * 1000 * 1000) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4640 | pr_warn("ndelay must be less than 1 second, ignored\n"); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4641 | sdebug_ndelay = 0; |
| 4642 | } else if (sdebug_ndelay > 0) |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 4643 | sdebug_jdelay = JDELAY_OVERRIDDEN; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4644 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4645 | switch (sdebug_sector_size) { |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4646 | case 512: |
| 4647 | case 1024: |
| 4648 | case 2048: |
| 4649 | case 4096: |
| 4650 | break; |
| 4651 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4652 | pr_err("invalid sector_size %d\n", sdebug_sector_size); |
Martin K. Petersen | 597136ab | 2008-06-05 00:12:59 -0400 | [diff] [blame] | 4653 | return -EINVAL; |
| 4654 | } |
| 4655 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4656 | switch (sdebug_dif) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4657 | |
| 4658 | case SD_DIF_TYPE0_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4659 | break; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4660 | case SD_DIF_TYPE1_PROTECTION: |
Martin K. Petersen | 395cef0 | 2009-09-18 17:33:03 -0400 | [diff] [blame] | 4661 | case SD_DIF_TYPE2_PROTECTION: |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4662 | case SD_DIF_TYPE3_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 4663 | have_dif_prot = true; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4664 | break; |
| 4665 | |
| 4666 | default: |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4667 | pr_err("dif must be 0, 1, 2 or 3\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4668 | return -EINVAL; |
| 4669 | } |
| 4670 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4671 | if (sdebug_guard > 1) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4672 | pr_err("guard must be 0 or 1\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4673 | return -EINVAL; |
| 4674 | } |
| 4675 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4676 | if (sdebug_ato > 1) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4677 | pr_err("ato must be 0 or 1\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4678 | return -EINVAL; |
| 4679 | } |
| 4680 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4681 | if (sdebug_physblk_exp > 15) { |
| 4682 | pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 4683 | return -EINVAL; |
| 4684 | } |
Douglas Gilbert | 8d039e2 | 2016-04-30 22:44:43 -0400 | [diff] [blame^] | 4685 | if (sdebug_max_luns > 256) { |
| 4686 | pr_warn("max_luns can be no more than 256, use default\n"); |
| 4687 | sdebug_max_luns = DEF_MAX_LUNS; |
| 4688 | } |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 4689 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4690 | if (sdebug_lowest_aligned > 0x3fff) { |
| 4691 | pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned); |
Martin K. Petersen | ea61fca | 2009-05-15 00:40:33 -0400 | [diff] [blame] | 4692 | return -EINVAL; |
| 4693 | } |
| 4694 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4695 | if (sdebug_dev_size_mb < 1) |
| 4696 | sdebug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */ |
| 4697 | sz = (unsigned long)sdebug_dev_size_mb * 1048576; |
| 4698 | sdebug_store_sectors = sz / sdebug_sector_size; |
FUJITA Tomonori | 2889887 | 2008-03-30 00:59:55 +0900 | [diff] [blame] | 4699 | sdebug_capacity = get_sdebug_capacity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4700 | |
| 4701 | /* play around with geometry, don't waste too much on track 0 */ |
| 4702 | sdebug_heads = 8; |
| 4703 | sdebug_sectors_per = 32; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4704 | if (sdebug_dev_size_mb >= 256) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4705 | sdebug_heads = 64; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4706 | else if (sdebug_dev_size_mb >= 16) |
Andy Shevchenko | fa785f0 | 2015-11-26 20:22:50 +0200 | [diff] [blame] | 4707 | sdebug_heads = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4708 | sdebug_cylinders_per = (unsigned long)sdebug_capacity / |
| 4709 | (sdebug_sectors_per * sdebug_heads); |
| 4710 | if (sdebug_cylinders_per >= 1024) { |
| 4711 | /* other LLDs do this; implies >= 1GB ram disk ... */ |
| 4712 | sdebug_heads = 255; |
| 4713 | sdebug_sectors_per = 63; |
| 4714 | sdebug_cylinders_per = (unsigned long)sdebug_capacity / |
| 4715 | (sdebug_sectors_per * sdebug_heads); |
| 4716 | } |
| 4717 | |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 4718 | if (sdebug_fake_rw == 0) { |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4719 | fake_storep = vmalloc(sz); |
| 4720 | if (NULL == fake_storep) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4721 | pr_err("out of memory, 1\n"); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4722 | return -ENOMEM; |
| 4723 | } |
| 4724 | memset(fake_storep, 0, sz); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4725 | if (sdebug_num_parts > 0) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4726 | sdebug_build_parts(fake_storep, sz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4729 | if (sdebug_dix) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4730 | int dif_size; |
| 4731 | |
| 4732 | dif_size = sdebug_store_sectors * sizeof(struct sd_dif_tuple); |
| 4733 | dif_storep = vmalloc(dif_size); |
| 4734 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4735 | 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] | 4736 | |
| 4737 | if (dif_storep == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4738 | pr_err("out of mem. (DIX)\n"); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 4739 | ret = -ENOMEM; |
| 4740 | goto free_vm; |
| 4741 | } |
| 4742 | |
| 4743 | memset(dif_storep, 0xff, dif_size); |
| 4744 | } |
| 4745 | |
Martin K. Petersen | 5b94e23 | 2011-03-08 02:08:11 -0500 | [diff] [blame] | 4746 | /* Logical Block Provisioning */ |
| 4747 | if (scsi_debug_lbp()) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4748 | sdebug_unmap_max_blocks = |
| 4749 | clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 4750 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4751 | sdebug_unmap_max_desc = |
| 4752 | clamp(sdebug_unmap_max_desc, 0U, 256U); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 4753 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4754 | sdebug_unmap_granularity = |
| 4755 | clamp(sdebug_unmap_granularity, 1U, 0xffffffffU); |
Martin K. Petersen | 6014759 | 2010-08-19 11:49:00 -0400 | [diff] [blame] | 4756 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4757 | if (sdebug_unmap_alignment && |
| 4758 | sdebug_unmap_granularity <= |
| 4759 | sdebug_unmap_alignment) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4760 | pr_err("ERR: unmap_granularity <= unmap_alignment\n"); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4761 | return -EINVAL; |
| 4762 | } |
| 4763 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 4764 | map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1; |
| 4765 | map_storep = vmalloc(BITS_TO_LONGS(map_size) * sizeof(long)); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4766 | |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4767 | pr_info("%lu provisioning blocks\n", map_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4768 | |
| 4769 | if (map_storep == NULL) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4770 | pr_err("out of mem. (MAP)\n"); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4771 | ret = -ENOMEM; |
| 4772 | goto free_vm; |
| 4773 | } |
| 4774 | |
Akinobu Mita | b90ebc3 | 2013-04-16 22:11:58 +0900 | [diff] [blame] | 4775 | bitmap_zero(map_storep, map_size); |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4776 | |
| 4777 | /* Map first 1KB for partition table */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4778 | if (sdebug_num_parts) |
Martin K. Petersen | 44d9269 | 2009-10-15 14:45:27 -0400 | [diff] [blame] | 4779 | map_region(0, 2); |
| 4780 | } |
| 4781 | |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 4782 | pseudo_primary = root_device_register("pseudo_0"); |
| 4783 | if (IS_ERR(pseudo_primary)) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4784 | pr_warn("root_device_register() error\n"); |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 4785 | ret = PTR_ERR(pseudo_primary); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4786 | goto free_vm; |
| 4787 | } |
| 4788 | ret = bus_register(&pseudo_lld_bus); |
| 4789 | if (ret < 0) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4790 | pr_warn("bus_register error: %d\n", ret); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4791 | goto dev_unreg; |
| 4792 | } |
| 4793 | ret = driver_register(&sdebug_driverfs_driver); |
| 4794 | if (ret < 0) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4795 | pr_warn("driver_register error: %d\n", ret); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4796 | goto bus_unreg; |
| 4797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4799 | host_to_add = sdebug_add_host; |
| 4800 | sdebug_add_host = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | |
| 4802 | for (k = 0; k < host_to_add; k++) { |
| 4803 | if (sdebug_add_adapter()) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4804 | pr_err("sdebug_add_adapter failed k=%d\n", k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | break; |
| 4806 | } |
| 4807 | } |
| 4808 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4809 | if (sdebug_verbose) |
| 4810 | pr_info("built %d host(s)\n", sdebug_add_host); |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | return 0; |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4813 | |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4814 | bus_unreg: |
| 4815 | bus_unregister(&pseudo_lld_bus); |
| 4816 | dev_unreg: |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 4817 | root_device_unregister(pseudo_primary); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4818 | free_vm: |
Tomas Winkler | de232af | 2015-07-28 16:54:22 +0300 | [diff] [blame] | 4819 | vfree(map_storep); |
| 4820 | vfree(dif_storep); |
Randy Dunlap | 6ecaff7 | 2006-07-11 20:53:22 -0700 | [diff] [blame] | 4821 | vfree(fake_storep); |
| 4822 | |
| 4823 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4824 | } |
| 4825 | |
| 4826 | static void __exit scsi_debug_exit(void) |
| 4827 | { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4828 | int k = sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | |
| 4830 | stop_all_queued(); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4831 | free_all_queued(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4832 | for (; k; k--) |
| 4833 | sdebug_remove_adapter(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | driver_unregister(&sdebug_driverfs_driver); |
| 4835 | bus_unregister(&pseudo_lld_bus); |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 4836 | root_device_unregister(pseudo_primary); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4837 | |
Tomas Winkler | de232af | 2015-07-28 16:54:22 +0300 | [diff] [blame] | 4838 | vfree(dif_storep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | vfree(fake_storep); |
| 4840 | } |
| 4841 | |
| 4842 | device_initcall(scsi_debug_init); |
| 4843 | module_exit(scsi_debug_exit); |
| 4844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | static void sdebug_release_adapter(struct device * dev) |
| 4846 | { |
| 4847 | struct sdebug_host_info *sdbg_host; |
| 4848 | |
| 4849 | sdbg_host = to_sdebug_host(dev); |
| 4850 | kfree(sdbg_host); |
| 4851 | } |
| 4852 | |
| 4853 | static int sdebug_add_adapter(void) |
| 4854 | { |
| 4855 | int k, devs_per_host; |
| 4856 | int error = 0; |
| 4857 | struct sdebug_host_info *sdbg_host; |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 4858 | struct sdebug_dev_info *sdbg_devinfo, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | |
Douglas Gilbert | c65b144 | 2006-06-06 00:11:24 -0400 | [diff] [blame] | 4860 | sdbg_host = kzalloc(sizeof(*sdbg_host),GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | if (NULL == sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4862 | pr_err("out of memory at line %d\n", __LINE__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | return -ENOMEM; |
| 4864 | } |
| 4865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4866 | INIT_LIST_HEAD(&sdbg_host->dev_info_list); |
| 4867 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4868 | devs_per_host = sdebug_num_tgts * sdebug_max_luns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4869 | for (k = 0; k < devs_per_host; k++) { |
FUJITA Tomonori | 5cb2fc0 | 2008-03-20 11:09:16 +0900 | [diff] [blame] | 4870 | sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL); |
| 4871 | if (!sdbg_devinfo) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 4872 | pr_err("out of memory at line %d\n", __LINE__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | error = -ENOMEM; |
| 4874 | goto clean; |
| 4875 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | spin_lock(&sdebug_host_list_lock); |
| 4879 | list_add_tail(&sdbg_host->host_list, &sdebug_host_list); |
| 4880 | spin_unlock(&sdebug_host_list_lock); |
| 4881 | |
| 4882 | sdbg_host->dev.bus = &pseudo_lld_bus; |
Nicholas Bellinger | 9b90677 | 2010-09-06 17:24:28 -0700 | [diff] [blame] | 4883 | sdbg_host->dev.parent = pseudo_primary; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4884 | sdbg_host->dev.release = &sdebug_release_adapter; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4885 | dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_add_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4886 | |
| 4887 | error = device_register(&sdbg_host->dev); |
| 4888 | |
| 4889 | if (error) |
| 4890 | goto clean; |
| 4891 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4892 | ++sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4893 | return error; |
| 4894 | |
| 4895 | clean: |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 4896 | list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, |
| 4897 | dev_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | list_del(&sdbg_devinfo->dev_list); |
| 4899 | kfree(sdbg_devinfo); |
| 4900 | } |
| 4901 | |
| 4902 | kfree(sdbg_host); |
| 4903 | return error; |
| 4904 | } |
| 4905 | |
| 4906 | static void sdebug_remove_adapter(void) |
| 4907 | { |
| 4908 | struct sdebug_host_info * sdbg_host = NULL; |
| 4909 | |
| 4910 | spin_lock(&sdebug_host_list_lock); |
| 4911 | if (!list_empty(&sdebug_host_list)) { |
| 4912 | sdbg_host = list_entry(sdebug_host_list.prev, |
| 4913 | struct sdebug_host_info, host_list); |
| 4914 | list_del(&sdbg_host->host_list); |
| 4915 | } |
| 4916 | spin_unlock(&sdebug_host_list_lock); |
| 4917 | |
| 4918 | if (!sdbg_host) |
| 4919 | return; |
| 4920 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4921 | device_unregister(&sdbg_host->dev); |
| 4922 | --sdebug_add_host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | } |
| 4924 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4925 | static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth) |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4926 | { |
| 4927 | int num_in_q = 0; |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4928 | unsigned long iflags; |
| 4929 | struct sdebug_dev_info *devip; |
| 4930 | |
| 4931 | spin_lock_irqsave(&queued_arr_lock, iflags); |
| 4932 | devip = (struct sdebug_dev_info *)sdev->hostdata; |
| 4933 | if (NULL == devip) { |
| 4934 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
| 4935 | return -ENODEV; |
| 4936 | } |
| 4937 | num_in_q = atomic_read(&devip->num_in_q); |
| 4938 | spin_unlock_irqrestore(&queued_arr_lock, iflags); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4939 | |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 4940 | if (qdepth < 1) |
| 4941 | qdepth = 1; |
| 4942 | /* allow to exceed max host queued_arr elements for testing */ |
| 4943 | if (qdepth > SCSI_DEBUG_CANQUEUE + 10) |
| 4944 | qdepth = SCSI_DEBUG_CANQUEUE + 10; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4945 | scsi_change_queue_depth(sdev, qdepth); |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 4946 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4947 | if (SDEBUG_OPT_Q_NOISE & sdebug_opts) { |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 4948 | sdev_printk(KERN_INFO, sdev, |
| 4949 | "%s: qdepth=%d, num_in_q=%d\n", |
| 4950 | __func__, qdepth, num_in_q); |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 4951 | } |
| 4952 | return sdev->queue_depth; |
| 4953 | } |
| 4954 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4955 | static int check_inject(struct scsi_cmnd *scp) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 4956 | { |
| 4957 | struct sdebug_scmd_extra_t *ep = scsi_cmd_priv(scp); |
| 4958 | |
| 4959 | memset(ep, 0, sizeof(struct sdebug_scmd_extra_t)); |
| 4960 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4961 | if (atomic_inc_return(&sdebug_cmnd_count) >= abs(sdebug_every_nth)) { |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 4962 | atomic_set(&sdebug_cmnd_count, 0); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4963 | if (sdebug_every_nth < -1) |
| 4964 | sdebug_every_nth = -1; |
| 4965 | if (SDEBUG_OPT_TIMEOUT & sdebug_opts) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 4966 | return 1; /* ignore command causing timeout */ |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4967 | else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts && |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 4968 | scsi_medium_access_command(scp)) |
| 4969 | return 1; /* time out reads and writes */ |
| 4970 | if (sdebug_any_injecting_opt) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4971 | if (SDEBUG_OPT_RECOVERED_ERR & sdebug_opts) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 4972 | ep->inj_recovered = true; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4973 | if (SDEBUG_OPT_TRANSPORT_ERR & sdebug_opts) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 4974 | ep->inj_transport = true; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4975 | if (SDEBUG_OPT_DIF_ERR & sdebug_opts) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 4976 | ep->inj_dif = true; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4977 | if (SDEBUG_OPT_DIX_ERR & sdebug_opts) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 4978 | ep->inj_dix = true; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 4979 | if (SDEBUG_OPT_SHORT_TRANSFER & sdebug_opts) |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 4980 | ep->inj_short = true; |
| 4981 | } |
| 4982 | } |
| 4983 | return 0; |
| 4984 | } |
| 4985 | |
Douglas Gilbert | fd32119 | 2016-04-25 12:16:33 -0400 | [diff] [blame] | 4986 | static int scsi_debug_queuecommand(struct Scsi_Host *shost, |
| 4987 | struct scsi_cmnd *scp) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4988 | { |
| 4989 | u8 sdeb_i; |
| 4990 | struct scsi_device *sdp = scp->device; |
| 4991 | const struct opcode_info_t *oip; |
| 4992 | const struct opcode_info_t *r_oip; |
| 4993 | struct sdebug_dev_info *devip; |
| 4994 | u8 *cmd = scp->cmnd; |
| 4995 | int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *); |
| 4996 | int k, na; |
| 4997 | int errsts = 0; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 4998 | u32 flags; |
| 4999 | u16 sa; |
| 5000 | u8 opcode = cmd[0]; |
| 5001 | bool has_wlun_rl; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5002 | |
| 5003 | scsi_set_resid(scp, 0); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5004 | if (unlikely(sdebug_verbose && |
| 5005 | !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5006 | char b[120]; |
| 5007 | int n, len, sb; |
| 5008 | |
| 5009 | len = scp->cmd_len; |
| 5010 | sb = (int)sizeof(b); |
| 5011 | if (len > 32) |
| 5012 | strcpy(b, "too long, over 32 bytes"); |
| 5013 | else { |
| 5014 | for (k = 0, n = 0; k < len && n < sb; ++k) |
| 5015 | n += scnprintf(b + n, sb - n, "%02x ", |
| 5016 | (u32)cmd[k]); |
| 5017 | } |
| 5018 | sdev_printk(KERN_INFO, sdp, "%s: cmd %s\n", my_name, b); |
| 5019 | } |
Tomas Winkler | 34d5543 | 2015-07-28 16:54:21 +0300 | [diff] [blame] | 5020 | has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5021 | if (unlikely((sdp->lun >= sdebug_max_luns) && !has_wlun_rl)) |
| 5022 | goto err_out; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5023 | |
| 5024 | sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */ |
| 5025 | oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */ |
| 5026 | devip = (struct sdebug_dev_info *)sdp->hostdata; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5027 | if (unlikely(!devip)) { |
| 5028 | devip = find_build_dev_info(sdp); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5029 | if (NULL == devip) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5030 | goto err_out; |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5031 | } |
| 5032 | na = oip->num_attached; |
| 5033 | r_pfp = oip->pfp; |
| 5034 | if (na) { /* multiple commands with this opcode */ |
| 5035 | r_oip = oip; |
| 5036 | if (FF_SA & r_oip->flags) { |
| 5037 | if (F_SA_LOW & oip->flags) |
| 5038 | sa = 0x1f & cmd[1]; |
| 5039 | else |
| 5040 | sa = get_unaligned_be16(cmd + 8); |
| 5041 | for (k = 0; k <= na; oip = r_oip->arrp + k++) { |
| 5042 | if (opcode == oip->opcode && sa == oip->sa) |
| 5043 | break; |
| 5044 | } |
| 5045 | } else { /* since no service action only check opcode */ |
| 5046 | for (k = 0; k <= na; oip = r_oip->arrp + k++) { |
| 5047 | if (opcode == oip->opcode) |
| 5048 | break; |
| 5049 | } |
| 5050 | } |
| 5051 | if (k > na) { |
| 5052 | if (F_SA_LOW & r_oip->flags) |
| 5053 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4); |
| 5054 | else if (F_SA_HIGH & r_oip->flags) |
| 5055 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7); |
| 5056 | else |
| 5057 | mk_sense_invalid_opcode(scp); |
| 5058 | goto check_cond; |
| 5059 | } |
| 5060 | } /* else (when na==0) we assume the oip is a match */ |
| 5061 | flags = oip->flags; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5062 | if (unlikely(F_INV_OP & flags)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5063 | mk_sense_invalid_opcode(scp); |
| 5064 | goto check_cond; |
| 5065 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5066 | if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) { |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5067 | if (sdebug_verbose) |
| 5068 | sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n", |
| 5069 | my_name, opcode, " supported for wlun"); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5070 | mk_sense_invalid_opcode(scp); |
| 5071 | goto check_cond; |
| 5072 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5073 | if (unlikely(sdebug_strict)) { /* check cdb against mask */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5074 | u8 rem; |
| 5075 | int j; |
| 5076 | |
| 5077 | for (k = 1; k < oip->len_mask[0] && k < 16; ++k) { |
| 5078 | rem = ~oip->len_mask[k] & cmd[k]; |
| 5079 | if (rem) { |
| 5080 | for (j = 7; j >= 0; --j, rem <<= 1) { |
| 5081 | if (0x80 & rem) |
| 5082 | break; |
| 5083 | } |
| 5084 | mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j); |
| 5085 | goto check_cond; |
| 5086 | } |
| 5087 | } |
| 5088 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5089 | if (unlikely(!(F_SKIP_UA & flags) && |
Douglas Gilbert | b01f6f8 | 2016-04-30 22:44:42 -0400 | [diff] [blame] | 5090 | find_first_bit(devip->uas_bm, |
| 5091 | SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) { |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5092 | errsts = make_ua(scp, devip); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5093 | if (errsts) |
| 5094 | goto check_cond; |
| 5095 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5096 | if (unlikely((F_M_ACCESS & flags) && devip->stopped)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5097 | mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2); |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5098 | if (sdebug_verbose) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5099 | sdev_printk(KERN_INFO, sdp, "%s reports: Not ready: " |
| 5100 | "%s\n", my_name, "initializing command " |
| 5101 | "required"); |
| 5102 | errsts = check_condition_result; |
| 5103 | goto fini; |
| 5104 | } |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5105 | if (sdebug_fake_rw && (F_FAKE_RW & flags)) |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5106 | goto fini; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5107 | if (unlikely(sdebug_every_nth)) { |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5108 | if (check_inject(scp)) |
| 5109 | return 0; /* ignore command: make trouble */ |
| 5110 | } |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5111 | if (likely(oip->pfp)) |
| 5112 | errsts = oip->pfp(scp, devip); /* calls a resp_* function */ |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5113 | else if (r_pfp) /* if leaf function ptr NULL, try the root's */ |
| 5114 | errsts = r_pfp(scp, devip); |
| 5115 | |
| 5116 | fini: |
| 5117 | return schedule_resp(scp, devip, errsts, |
Douglas Gilbert | c220609 | 2016-04-25 12:16:31 -0400 | [diff] [blame] | 5118 | ((F_DELAY_OVERR & flags) ? 0 : sdebug_jdelay)); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5119 | check_cond: |
| 5120 | return schedule_resp(scp, devip, check_condition_result, 0); |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5121 | err_out: |
| 5122 | return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, 0); |
Douglas Gilbert | c2248fc | 2014-11-24 20:46:29 -0500 | [diff] [blame] | 5123 | } |
| 5124 | |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5125 | static struct scsi_host_template sdebug_driver_template = { |
Al Viro | c8ed555 | 2013-03-31 01:46:06 -0400 | [diff] [blame] | 5126 | .show_info = scsi_debug_show_info, |
| 5127 | .write_info = scsi_debug_write_info, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5128 | .proc_name = sdebug_proc_name, |
| 5129 | .name = "SCSI DEBUG", |
| 5130 | .info = scsi_debug_info, |
| 5131 | .slave_alloc = scsi_debug_slave_alloc, |
| 5132 | .slave_configure = scsi_debug_slave_configure, |
| 5133 | .slave_destroy = scsi_debug_slave_destroy, |
| 5134 | .ioctl = scsi_debug_ioctl, |
Douglas Gilbert | 185dd23 | 2016-04-25 12:16:29 -0400 | [diff] [blame] | 5135 | .queuecommand = scsi_debug_queuecommand, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5136 | .change_queue_depth = sdebug_change_qdepth, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5137 | .eh_abort_handler = scsi_debug_abort, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5138 | .eh_device_reset_handler = scsi_debug_device_reset, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5139 | .eh_target_reset_handler = scsi_debug_target_reset, |
| 5140 | .eh_bus_reset_handler = scsi_debug_bus_reset, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5141 | .eh_host_reset_handler = scsi_debug_host_reset, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5142 | .can_queue = SCSI_DEBUG_CANQUEUE, |
| 5143 | .this_id = 7, |
Ming Lin | 65e8617 | 2016-04-04 14:48:10 -0700 | [diff] [blame] | 5144 | .sg_tablesize = SG_MAX_SEGMENTS, |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5145 | .cmd_per_lun = DEF_CMD_PER_LUN, |
Akinobu Mita | 6bb5e6e | 2014-06-02 22:56:49 +0900 | [diff] [blame] | 5146 | .max_sectors = -1U, |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5147 | .use_clustering = DISABLE_CLUSTERING, |
| 5148 | .module = THIS_MODULE, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5149 | .track_queue_depth = 1, |
Douglas Gilbert | 817fd66 | 2014-11-24 20:18:02 -0500 | [diff] [blame] | 5150 | .cmd_size = sizeof(struct sdebug_scmd_extra_t), |
FUJITA Tomonori | 9e603ca | 2008-03-02 18:30:16 +0900 | [diff] [blame] | 5151 | }; |
| 5152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | static int sdebug_driver_probe(struct device * dev) |
| 5154 | { |
Douglas Gilbert | 22017ed | 2014-11-24 23:04:47 -0500 | [diff] [blame] | 5155 | int error = 0; |
| 5156 | struct sdebug_host_info *sdbg_host; |
| 5157 | struct Scsi_Host *hpnt; |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5158 | int hprot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | |
| 5160 | sdbg_host = to_sdebug_host(dev); |
| 5161 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5162 | sdebug_driver_template.can_queue = sdebug_max_queue; |
| 5163 | if (sdebug_clustering) |
Akinobu Mita | 0759c66 | 2014-02-26 22:57:04 +0900 | [diff] [blame] | 5164 | sdebug_driver_template.use_clustering = ENABLE_CLUSTERING; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5165 | hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host)); |
| 5166 | if (NULL == hpnt) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5167 | pr_err("scsi_host_alloc failed\n"); |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5168 | error = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | return error; |
Douglas Gilbert | 78d4e5a | 2010-03-25 17:29:05 -0400 | [diff] [blame] | 5170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 | |
| 5172 | sdbg_host->shost = hpnt; |
| 5173 | *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5174 | if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id)) |
| 5175 | hpnt->max_id = sdebug_num_tgts + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | else |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5177 | hpnt->max_id = sdebug_num_tgts; |
| 5178 | /* = sdebug_max_luns; */ |
Tomas Winkler | f2d3fd2 | 2015-07-28 16:54:25 +0300 | [diff] [blame] | 5179 | hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5181 | hprot = 0; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5182 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5183 | switch (sdebug_dif) { |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5184 | |
| 5185 | case SD_DIF_TYPE1_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5186 | hprot = SHOST_DIF_TYPE1_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5187 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5188 | hprot |= SHOST_DIX_TYPE1_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5189 | break; |
| 5190 | |
| 5191 | case SD_DIF_TYPE2_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5192 | hprot = SHOST_DIF_TYPE2_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5193 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5194 | hprot |= SHOST_DIX_TYPE2_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5195 | break; |
| 5196 | |
| 5197 | case SD_DIF_TYPE3_PROTECTION: |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5198 | hprot = SHOST_DIF_TYPE3_PROTECTION; |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5199 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5200 | hprot |= SHOST_DIX_TYPE3_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5201 | break; |
| 5202 | |
| 5203 | default: |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5204 | if (sdebug_dix) |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5205 | hprot |= SHOST_DIX_TYPE0_PROTECTION; |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5206 | break; |
| 5207 | } |
| 5208 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5209 | scsi_host_set_prot(hpnt, hprot); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5210 | |
Douglas Gilbert | f46eb0e | 2016-04-25 12:16:34 -0400 | [diff] [blame] | 5211 | if (have_dif_prot || sdebug_dix) |
| 5212 | pr_info("host protection%s%s%s%s%s%s%s\n", |
| 5213 | (hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "", |
| 5214 | (hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "", |
| 5215 | (hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "", |
| 5216 | (hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "", |
| 5217 | (hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "", |
| 5218 | (hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "", |
| 5219 | (hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : ""); |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5220 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5221 | if (sdebug_guard == 1) |
Martin K. Petersen | c6a4428 | 2009-01-04 03:08:19 -0500 | [diff] [blame] | 5222 | scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP); |
| 5223 | else |
| 5224 | scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC); |
| 5225 | |
Douglas Gilbert | 773642d | 2016-04-25 12:16:28 -0400 | [diff] [blame] | 5226 | sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts); |
| 5227 | sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & sdebug_opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | error = scsi_add_host(hpnt, &sdbg_host->dev); |
| 5229 | if (error) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5230 | pr_err("scsi_add_host failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | error = -ENODEV; |
| 5232 | scsi_host_put(hpnt); |
| 5233 | } else |
| 5234 | scsi_scan_host(hpnt); |
| 5235 | |
Douglas Gilbert | cbf6784 | 2014-07-26 11:55:35 -0400 | [diff] [blame] | 5236 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | } |
| 5238 | |
| 5239 | static int sdebug_driver_remove(struct device * dev) |
| 5240 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | struct sdebug_host_info *sdbg_host; |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5242 | struct sdebug_dev_info *sdbg_devinfo, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5243 | |
| 5244 | sdbg_host = to_sdebug_host(dev); |
| 5245 | |
| 5246 | if (!sdbg_host) { |
Tomas Winkler | c1287970 | 2015-07-28 16:54:20 +0300 | [diff] [blame] | 5247 | pr_err("Unable to locate host info\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | return -ENODEV; |
| 5249 | } |
| 5250 | |
| 5251 | scsi_remove_host(sdbg_host->shost); |
| 5252 | |
FUJITA Tomonori | 8b40228 | 2008-03-20 11:09:18 +0900 | [diff] [blame] | 5253 | list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list, |
| 5254 | dev_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | list_del(&sdbg_devinfo->dev_list); |
| 5256 | kfree(sdbg_devinfo); |
| 5257 | } |
| 5258 | |
| 5259 | scsi_host_put(sdbg_host->shost); |
| 5260 | return 0; |
| 5261 | } |
| 5262 | |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5263 | static int pseudo_lld_bus_match(struct device *dev, |
| 5264 | struct device_driver *dev_driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | { |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5266 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | } |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5268 | |
| 5269 | static struct bus_type pseudo_lld_bus = { |
| 5270 | .name = "pseudo", |
| 5271 | .match = pseudo_lld_bus_match, |
| 5272 | .probe = sdebug_driver_probe, |
| 5273 | .remove = sdebug_driver_remove, |
Akinobu Mita | 8206937 | 2013-10-14 22:48:04 +0900 | [diff] [blame] | 5274 | .drv_groups = sdebug_drv_groups, |
FUJITA Tomonori | 8dea0d0 | 2008-03-30 00:59:58 +0900 | [diff] [blame] | 5275 | }; |