Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1 | /* |
| 2 | * SCSI Block Commands (SBC) parsing and emulation. |
| 3 | * |
Nicholas Bellinger | 4c76251 | 2013-09-05 15:29:12 -0700 | [diff] [blame] | 4 | * (c) Copyright 2002-2013 Datera, Inc. |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 5 | * |
| 6 | * Nicholas A. Bellinger <nab@kernel.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/ratelimit.h> |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 26 | #include <linux/crc-t10dif.h> |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 27 | #include <asm/unaligned.h> |
| 28 | #include <scsi/scsi.h> |
Nicholas Bellinger | 68ff9b9 | 2013-08-19 15:20:28 -0700 | [diff] [blame] | 29 | #include <scsi/scsi_tcq.h> |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 30 | |
| 31 | #include <target/target_core_base.h> |
| 32 | #include <target/target_core_backend.h> |
| 33 | #include <target/target_core_fabric.h> |
| 34 | |
| 35 | #include "target_core_internal.h" |
| 36 | #include "target_core_ua.h" |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 37 | #include "target_core_alua.h" |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 38 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 39 | static sense_reason_t |
Nicholas Bellinger | afd73f1 | 2015-02-14 01:32:11 +0000 | [diff] [blame] | 40 | sbc_check_prot(struct se_device *, struct se_cmd *, unsigned char *, u32, bool); |
| 41 | |
| 42 | static sense_reason_t |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 43 | sbc_emulate_readcapacity(struct se_cmd *cmd) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 44 | { |
| 45 | struct se_device *dev = cmd->se_dev; |
Roland Dreier | 8dc8632 | 2013-06-26 17:36:19 -0700 | [diff] [blame] | 46 | unsigned char *cdb = cmd->t_task_cdb; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 47 | unsigned long long blocks_long = dev->transport->get_blocks(dev); |
Paolo Bonzini | a50da14 | 2012-09-07 17:30:40 +0200 | [diff] [blame] | 48 | unsigned char *rbuf; |
| 49 | unsigned char buf[8]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 50 | u32 blocks; |
| 51 | |
Roland Dreier | 8dc8632 | 2013-06-26 17:36:19 -0700 | [diff] [blame] | 52 | /* |
| 53 | * SBC-2 says: |
| 54 | * If the PMI bit is set to zero and the LOGICAL BLOCK |
| 55 | * ADDRESS field is not set to zero, the device server shall |
| 56 | * terminate the command with CHECK CONDITION status with |
| 57 | * the sense key set to ILLEGAL REQUEST and the additional |
| 58 | * sense code set to INVALID FIELD IN CDB. |
| 59 | * |
| 60 | * In SBC-3, these fields are obsolete, but some SCSI |
| 61 | * compliance tests actually check this, so we might as well |
| 62 | * follow SBC-2. |
| 63 | */ |
| 64 | if (!(cdb[8] & 1) && !!(cdb[2] | cdb[3] | cdb[4] | cdb[5])) |
| 65 | return TCM_INVALID_CDB_FIELD; |
| 66 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 67 | if (blocks_long >= 0x00000000ffffffff) |
| 68 | blocks = 0xffffffff; |
| 69 | else |
| 70 | blocks = (u32)blocks_long; |
| 71 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 72 | buf[0] = (blocks >> 24) & 0xff; |
| 73 | buf[1] = (blocks >> 16) & 0xff; |
| 74 | buf[2] = (blocks >> 8) & 0xff; |
| 75 | buf[3] = blocks & 0xff; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 76 | buf[4] = (dev->dev_attrib.block_size >> 24) & 0xff; |
| 77 | buf[5] = (dev->dev_attrib.block_size >> 16) & 0xff; |
| 78 | buf[6] = (dev->dev_attrib.block_size >> 8) & 0xff; |
| 79 | buf[7] = dev->dev_attrib.block_size & 0xff; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 80 | |
Paolo Bonzini | a50da14 | 2012-09-07 17:30:40 +0200 | [diff] [blame] | 81 | rbuf = transport_kmap_data_sg(cmd); |
Nicholas Bellinger | 8b4b0dc | 2013-01-29 14:18:00 -0800 | [diff] [blame] | 82 | if (rbuf) { |
| 83 | memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length)); |
| 84 | transport_kunmap_data_sg(cmd); |
| 85 | } |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 86 | |
Roland Dreier | 2426bd4 | 2014-06-10 11:07:47 -0700 | [diff] [blame] | 87 | target_complete_cmd_with_length(cmd, GOOD, 8); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 91 | static sense_reason_t |
| 92 | sbc_emulate_readcapacity_16(struct se_cmd *cmd) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 93 | { |
| 94 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | 2d33598 | 2014-04-02 13:34:04 -0700 | [diff] [blame] | 95 | struct se_session *sess = cmd->se_sess; |
Nicholas Bellinger | 9ef5466 | 2015-02-27 22:05:33 -0800 | [diff] [blame^] | 96 | int pi_prot_type = dev->dev_attrib.pi_prot_type; |
| 97 | |
Paolo Bonzini | a50da14 | 2012-09-07 17:30:40 +0200 | [diff] [blame] | 98 | unsigned char *rbuf; |
| 99 | unsigned char buf[32]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 100 | unsigned long long blocks = dev->transport->get_blocks(dev); |
| 101 | |
Paolo Bonzini | a50da14 | 2012-09-07 17:30:40 +0200 | [diff] [blame] | 102 | memset(buf, 0, sizeof(buf)); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 103 | buf[0] = (blocks >> 56) & 0xff; |
| 104 | buf[1] = (blocks >> 48) & 0xff; |
| 105 | buf[2] = (blocks >> 40) & 0xff; |
| 106 | buf[3] = (blocks >> 32) & 0xff; |
| 107 | buf[4] = (blocks >> 24) & 0xff; |
| 108 | buf[5] = (blocks >> 16) & 0xff; |
| 109 | buf[6] = (blocks >> 8) & 0xff; |
| 110 | buf[7] = blocks & 0xff; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 111 | buf[8] = (dev->dev_attrib.block_size >> 24) & 0xff; |
| 112 | buf[9] = (dev->dev_attrib.block_size >> 16) & 0xff; |
| 113 | buf[10] = (dev->dev_attrib.block_size >> 8) & 0xff; |
| 114 | buf[11] = dev->dev_attrib.block_size & 0xff; |
Nicholas Bellinger | 56dac14 | 2013-12-23 20:36:09 +0000 | [diff] [blame] | 115 | /* |
| 116 | * Set P_TYPE and PROT_EN bits for DIF support |
| 117 | */ |
Nicholas Bellinger | 2d33598 | 2014-04-02 13:34:04 -0700 | [diff] [blame] | 118 | if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) { |
Nicholas Bellinger | 9ef5466 | 2015-02-27 22:05:33 -0800 | [diff] [blame^] | 119 | /* |
| 120 | * Only override a device's pi_prot_type if no T10-PI is |
| 121 | * available, and sess_prot_type has been explicitly enabled. |
| 122 | */ |
| 123 | if (!pi_prot_type) |
| 124 | pi_prot_type = sess->sess_prot_type; |
| 125 | |
| 126 | if (pi_prot_type) |
| 127 | buf[12] = (pi_prot_type - 1) << 1 | 0x1; |
Nicholas Bellinger | 2d33598 | 2014-04-02 13:34:04 -0700 | [diff] [blame] | 128 | } |
Andy Grover | 7f7caf6 | 2013-11-11 08:59:17 -0800 | [diff] [blame] | 129 | |
| 130 | if (dev->transport->get_lbppbe) |
| 131 | buf[13] = dev->transport->get_lbppbe(dev) & 0x0f; |
| 132 | |
| 133 | if (dev->transport->get_alignment_offset_lbas) { |
| 134 | u16 lalba = dev->transport->get_alignment_offset_lbas(dev); |
| 135 | buf[14] = (lalba >> 8) & 0x3f; |
| 136 | buf[15] = lalba & 0xff; |
| 137 | } |
| 138 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 139 | /* |
| 140 | * Set Thin Provisioning Enable bit following sbc3r22 in section |
| 141 | * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled. |
| 142 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 143 | if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws) |
Andy Grover | 7f7caf6 | 2013-11-11 08:59:17 -0800 | [diff] [blame] | 144 | buf[14] |= 0x80; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 145 | |
Paolo Bonzini | a50da14 | 2012-09-07 17:30:40 +0200 | [diff] [blame] | 146 | rbuf = transport_kmap_data_sg(cmd); |
Nicholas Bellinger | 8b4b0dc | 2013-01-29 14:18:00 -0800 | [diff] [blame] | 147 | if (rbuf) { |
| 148 | memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length)); |
| 149 | transport_kunmap_data_sg(cmd); |
| 150 | } |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 151 | |
Roland Dreier | 2426bd4 | 2014-06-10 11:07:47 -0700 | [diff] [blame] | 152 | target_complete_cmd_with_length(cmd, GOOD, 32); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
Roland Dreier | 972b29c8 | 2013-02-22 09:52:57 -0800 | [diff] [blame] | 156 | sector_t sbc_get_write_same_sectors(struct se_cmd *cmd) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 157 | { |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 158 | u32 num_blocks; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 159 | |
| 160 | if (cmd->t_task_cdb[0] == WRITE_SAME) |
| 161 | num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]); |
| 162 | else if (cmd->t_task_cdb[0] == WRITE_SAME_16) |
| 163 | num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]); |
| 164 | else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */ |
| 165 | num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]); |
| 166 | |
| 167 | /* |
| 168 | * Use the explicit range when non zero is supplied, otherwise calculate |
| 169 | * the remaining range based on ->get_blocks() - starting LBA. |
| 170 | */ |
Christoph Hellwig | 6f974e8 | 2012-06-17 18:40:54 -0400 | [diff] [blame] | 171 | if (num_blocks) |
| 172 | return num_blocks; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 173 | |
Christoph Hellwig | 6f974e8 | 2012-06-17 18:40:54 -0400 | [diff] [blame] | 174 | return cmd->se_dev->transport->get_blocks(cmd->se_dev) - |
| 175 | cmd->t_task_lba + 1; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 176 | } |
Roland Dreier | 972b29c8 | 2013-02-22 09:52:57 -0800 | [diff] [blame] | 177 | EXPORT_SYMBOL(sbc_get_write_same_sectors); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 178 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 179 | static sense_reason_t |
Nicholas Bellinger | 1920ed6 | 2012-11-06 20:59:41 -0800 | [diff] [blame] | 180 | sbc_emulate_noop(struct se_cmd *cmd) |
Bernhard Kohl | 1a1ff38 | 2012-10-24 15:53:58 +0200 | [diff] [blame] | 181 | { |
| 182 | target_complete_cmd(cmd, GOOD); |
| 183 | return 0; |
| 184 | } |
| 185 | |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 186 | static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors) |
| 187 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 188 | return cmd->se_dev->dev_attrib.block_size * sectors; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 191 | static inline u32 transport_get_sectors_6(unsigned char *cdb) |
| 192 | { |
| 193 | /* |
| 194 | * Use 8-bit sector value. SBC-3 says: |
| 195 | * |
| 196 | * A TRANSFER LENGTH field set to zero specifies that 256 |
| 197 | * logical blocks shall be written. Any other value |
| 198 | * specifies the number of logical blocks that shall be |
| 199 | * written. |
| 200 | */ |
| 201 | return cdb[4] ? : 256; |
| 202 | } |
| 203 | |
| 204 | static inline u32 transport_get_sectors_10(unsigned char *cdb) |
| 205 | { |
| 206 | return (u32)(cdb[7] << 8) + cdb[8]; |
| 207 | } |
| 208 | |
| 209 | static inline u32 transport_get_sectors_12(unsigned char *cdb) |
| 210 | { |
| 211 | return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9]; |
| 212 | } |
| 213 | |
| 214 | static inline u32 transport_get_sectors_16(unsigned char *cdb) |
| 215 | { |
| 216 | return (u32)(cdb[10] << 24) + (cdb[11] << 16) + |
| 217 | (cdb[12] << 8) + cdb[13]; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants |
| 222 | */ |
| 223 | static inline u32 transport_get_sectors_32(unsigned char *cdb) |
| 224 | { |
| 225 | return (u32)(cdb[28] << 24) + (cdb[29] << 16) + |
| 226 | (cdb[30] << 8) + cdb[31]; |
| 227 | |
| 228 | } |
| 229 | |
| 230 | static inline u32 transport_lba_21(unsigned char *cdb) |
| 231 | { |
| 232 | return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3]; |
| 233 | } |
| 234 | |
| 235 | static inline u32 transport_lba_32(unsigned char *cdb) |
| 236 | { |
| 237 | return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; |
| 238 | } |
| 239 | |
| 240 | static inline unsigned long long transport_lba_64(unsigned char *cdb) |
| 241 | { |
| 242 | unsigned int __v1, __v2; |
| 243 | |
| 244 | __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; |
| 245 | __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; |
| 246 | |
| 247 | return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs |
| 252 | */ |
| 253 | static inline unsigned long long transport_lba_64_ext(unsigned char *cdb) |
| 254 | { |
| 255 | unsigned int __v1, __v2; |
| 256 | |
| 257 | __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15]; |
| 258 | __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19]; |
| 259 | |
| 260 | return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; |
| 261 | } |
| 262 | |
Nicholas Bellinger | cd063be | 2012-11-07 20:01:10 -0800 | [diff] [blame] | 263 | static sense_reason_t |
| 264 | sbc_setup_write_same(struct se_cmd *cmd, unsigned char *flags, struct sbc_ops *ops) |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 265 | { |
Nicholas Bellinger | 8e575c5 | 2015-02-13 22:09:47 +0000 | [diff] [blame] | 266 | struct se_device *dev = cmd->se_dev; |
| 267 | sector_t end_lba = dev->transport->get_blocks(dev) + 1; |
Roland Dreier | 972b29c8 | 2013-02-22 09:52:57 -0800 | [diff] [blame] | 268 | unsigned int sectors = sbc_get_write_same_sectors(cmd); |
Nicholas Bellinger | afd73f1 | 2015-02-14 01:32:11 +0000 | [diff] [blame] | 269 | sense_reason_t ret; |
Nicholas Bellinger | 773cbaf | 2012-11-15 11:02:49 -0800 | [diff] [blame] | 270 | |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 271 | if ((flags[0] & 0x04) || (flags[0] & 0x02)) { |
| 272 | pr_err("WRITE_SAME PBDATA and LBDATA" |
| 273 | " bits not supported for Block Discard" |
| 274 | " Emulation\n"); |
Nicholas Bellinger | cd063be | 2012-11-07 20:01:10 -0800 | [diff] [blame] | 275 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 276 | } |
Nicholas Bellinger | 773cbaf | 2012-11-15 11:02:49 -0800 | [diff] [blame] | 277 | if (sectors > cmd->se_dev->dev_attrib.max_write_same_len) { |
| 278 | pr_warn("WRITE_SAME sectors: %u exceeds max_write_same_len: %u\n", |
| 279 | sectors, cmd->se_dev->dev_attrib.max_write_same_len); |
| 280 | return TCM_INVALID_CDB_FIELD; |
| 281 | } |
Nicholas Bellinger | 8e575c5 | 2015-02-13 22:09:47 +0000 | [diff] [blame] | 282 | /* |
| 283 | * Sanity check for LBA wrap and request past end of device. |
| 284 | */ |
| 285 | if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) || |
| 286 | ((cmd->t_task_lba + sectors) > end_lba)) { |
| 287 | pr_err("WRITE_SAME exceeds last lba %llu (lba %llu, sectors %u)\n", |
| 288 | (unsigned long long)end_lba, cmd->t_task_lba, sectors); |
| 289 | return TCM_ADDRESS_OUT_OF_RANGE; |
| 290 | } |
| 291 | |
Roland Dreier | 5cb770b | 2013-10-14 15:49:23 -0700 | [diff] [blame] | 292 | /* We always have ANC_SUP == 0 so setting ANCHOR is always an error */ |
| 293 | if (flags[0] & 0x10) { |
| 294 | pr_warn("WRITE SAME with ANCHOR not supported\n"); |
| 295 | return TCM_INVALID_CDB_FIELD; |
| 296 | } |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 297 | /* |
Nicholas Bellinger | cd063be | 2012-11-07 20:01:10 -0800 | [diff] [blame] | 298 | * Special case for WRITE_SAME w/ UNMAP=1 that ends up getting |
| 299 | * translated into block discard requests within backend code. |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 300 | */ |
Nicholas Bellinger | cd063be | 2012-11-07 20:01:10 -0800 | [diff] [blame] | 301 | if (flags[0] & 0x08) { |
| 302 | if (!ops->execute_write_same_unmap) |
| 303 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 304 | |
Nicholas Bellinger | d0a9129 | 2015-02-14 01:56:19 +0000 | [diff] [blame] | 305 | if (!dev->dev_attrib.emulate_tpws) { |
| 306 | pr_err("Got WRITE_SAME w/ UNMAP=1, but backend device" |
| 307 | " has emulate_tpws disabled\n"); |
| 308 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
| 309 | } |
Nicholas Bellinger | cd063be | 2012-11-07 20:01:10 -0800 | [diff] [blame] | 310 | cmd->execute_cmd = ops->execute_write_same_unmap; |
| 311 | return 0; |
| 312 | } |
| 313 | if (!ops->execute_write_same) |
| 314 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
| 315 | |
Nicholas Bellinger | afd73f1 | 2015-02-14 01:32:11 +0000 | [diff] [blame] | 316 | ret = sbc_check_prot(dev, cmd, &cmd->t_task_cdb[0], sectors, true); |
| 317 | if (ret) |
| 318 | return ret; |
| 319 | |
Nicholas Bellinger | cd063be | 2012-11-07 20:01:10 -0800 | [diff] [blame] | 320 | cmd->execute_cmd = ops->execute_write_same; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
Nicholas Bellinger | a6b0133 | 2013-08-19 14:34:17 -0700 | [diff] [blame] | 324 | static sense_reason_t xdreadwrite_callback(struct se_cmd *cmd) |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 325 | { |
| 326 | unsigned char *buf, *addr; |
| 327 | struct scatterlist *sg; |
| 328 | unsigned int offset; |
Nicholas Bellinger | a6b0133 | 2013-08-19 14:34:17 -0700 | [diff] [blame] | 329 | sense_reason_t ret = TCM_NO_SENSE; |
| 330 | int i, count; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 331 | /* |
| 332 | * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command |
| 333 | * |
| 334 | * 1) read the specified logical block(s); |
| 335 | * 2) transfer logical blocks from the data-out buffer; |
| 336 | * 3) XOR the logical blocks transferred from the data-out buffer with |
| 337 | * the logical blocks read, storing the resulting XOR data in a buffer; |
| 338 | * 4) if the DISABLE WRITE bit is set to zero, then write the logical |
| 339 | * blocks transferred from the data-out buffer; and |
| 340 | * 5) transfer the resulting XOR data to the data-in buffer. |
| 341 | */ |
| 342 | buf = kmalloc(cmd->data_length, GFP_KERNEL); |
| 343 | if (!buf) { |
| 344 | pr_err("Unable to allocate xor_callback buf\n"); |
Nicholas Bellinger | a6b0133 | 2013-08-19 14:34:17 -0700 | [diff] [blame] | 345 | return TCM_OUT_OF_RESOURCES; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 346 | } |
| 347 | /* |
| 348 | * Copy the scatterlist WRITE buffer located at cmd->t_data_sg |
| 349 | * into the locally allocated *buf |
| 350 | */ |
| 351 | sg_copy_to_buffer(cmd->t_data_sg, |
| 352 | cmd->t_data_nents, |
| 353 | buf, |
| 354 | cmd->data_length); |
| 355 | |
| 356 | /* |
| 357 | * Now perform the XOR against the BIDI read memory located at |
| 358 | * cmd->t_mem_bidi_list |
| 359 | */ |
| 360 | |
| 361 | offset = 0; |
| 362 | for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) { |
| 363 | addr = kmap_atomic(sg_page(sg)); |
Nicholas Bellinger | a6b0133 | 2013-08-19 14:34:17 -0700 | [diff] [blame] | 364 | if (!addr) { |
| 365 | ret = TCM_OUT_OF_RESOURCES; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 366 | goto out; |
Nicholas Bellinger | a6b0133 | 2013-08-19 14:34:17 -0700 | [diff] [blame] | 367 | } |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 368 | |
| 369 | for (i = 0; i < sg->length; i++) |
| 370 | *(addr + sg->offset + i) ^= *(buf + offset + i); |
| 371 | |
| 372 | offset += sg->length; |
| 373 | kunmap_atomic(addr); |
| 374 | } |
| 375 | |
| 376 | out: |
| 377 | kfree(buf); |
Nicholas Bellinger | a6b0133 | 2013-08-19 14:34:17 -0700 | [diff] [blame] | 378 | return ret; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 379 | } |
| 380 | |
Nicholas Bellinger | a82a953 | 2013-08-19 23:57:30 -0700 | [diff] [blame] | 381 | static sense_reason_t |
| 382 | sbc_execute_rw(struct se_cmd *cmd) |
| 383 | { |
| 384 | return cmd->execute_rw(cmd, cmd->t_data_sg, cmd->t_data_nents, |
| 385 | cmd->data_direction); |
| 386 | } |
| 387 | |
Nicholas Bellinger | 68ff9b9 | 2013-08-19 15:20:28 -0700 | [diff] [blame] | 388 | static sense_reason_t compare_and_write_post(struct se_cmd *cmd) |
| 389 | { |
| 390 | struct se_device *dev = cmd->se_dev; |
| 391 | |
Nicholas Bellinger | d8855c1 | 2013-10-01 16:53:10 -0700 | [diff] [blame] | 392 | /* |
| 393 | * Only set SCF_COMPARE_AND_WRITE_POST to force a response fall-through |
| 394 | * within target_complete_ok_work() if the command was successfully |
| 395 | * sent to the backend driver. |
| 396 | */ |
| 397 | spin_lock_irq(&cmd->t_state_lock); |
| 398 | if ((cmd->transport_state & CMD_T_SENT) && !cmd->scsi_status) |
| 399 | cmd->se_cmd_flags |= SCF_COMPARE_AND_WRITE_POST; |
| 400 | spin_unlock_irq(&cmd->t_state_lock); |
| 401 | |
Nicholas Bellinger | 68ff9b9 | 2013-08-19 15:20:28 -0700 | [diff] [blame] | 402 | /* |
| 403 | * Unlock ->caw_sem originally obtained during sbc_compare_and_write() |
| 404 | * before the original READ I/O submission. |
| 405 | */ |
| 406 | up(&dev->caw_sem); |
| 407 | |
| 408 | return TCM_NO_SENSE; |
| 409 | } |
| 410 | |
| 411 | static sense_reason_t compare_and_write_callback(struct se_cmd *cmd) |
| 412 | { |
| 413 | struct se_device *dev = cmd->se_dev; |
| 414 | struct scatterlist *write_sg = NULL, *sg; |
Nicholas Bellinger | db60df8 | 2013-10-01 17:04:40 -0700 | [diff] [blame] | 415 | unsigned char *buf = NULL, *addr; |
Nicholas Bellinger | 68ff9b9 | 2013-08-19 15:20:28 -0700 | [diff] [blame] | 416 | struct sg_mapping_iter m; |
| 417 | unsigned int offset = 0, len; |
| 418 | unsigned int nlbas = cmd->t_task_nolb; |
| 419 | unsigned int block_size = dev->dev_attrib.block_size; |
| 420 | unsigned int compare_len = (nlbas * block_size); |
| 421 | sense_reason_t ret = TCM_NO_SENSE; |
| 422 | int rc, i; |
| 423 | |
Nicholas Bellinger | cf6d1f0 | 2013-08-21 19:34:43 -0700 | [diff] [blame] | 424 | /* |
| 425 | * Handle early failure in transport_generic_request_failure(), |
| 426 | * which will not have taken ->caw_mutex yet.. |
| 427 | */ |
| 428 | if (!cmd->t_data_sg || !cmd->t_bidi_data_sg) |
| 429 | return TCM_NO_SENSE; |
Nicholas Bellinger | db60df8 | 2013-10-01 17:04:40 -0700 | [diff] [blame] | 430 | /* |
| 431 | * Immediately exit + release dev->caw_sem if command has already |
| 432 | * been failed with a non-zero SCSI status. |
| 433 | */ |
| 434 | if (cmd->scsi_status) { |
| 435 | pr_err("compare_and_write_callback: non zero scsi_status:" |
| 436 | " 0x%02x\n", cmd->scsi_status); |
| 437 | goto out; |
| 438 | } |
Nicholas Bellinger | cf6d1f0 | 2013-08-21 19:34:43 -0700 | [diff] [blame] | 439 | |
Nicholas Bellinger | 68ff9b9 | 2013-08-19 15:20:28 -0700 | [diff] [blame] | 440 | buf = kzalloc(cmd->data_length, GFP_KERNEL); |
| 441 | if (!buf) { |
| 442 | pr_err("Unable to allocate compare_and_write buf\n"); |
Nicholas Bellinger | a289008 | 2013-08-21 18:10:04 -0700 | [diff] [blame] | 443 | ret = TCM_OUT_OF_RESOURCES; |
| 444 | goto out; |
Nicholas Bellinger | 68ff9b9 | 2013-08-19 15:20:28 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Martin Svec | a1e1774 | 2014-04-01 16:03:02 +0200 | [diff] [blame] | 447 | write_sg = kmalloc(sizeof(struct scatterlist) * cmd->t_data_nents, |
Nicholas Bellinger | 68ff9b9 | 2013-08-19 15:20:28 -0700 | [diff] [blame] | 448 | GFP_KERNEL); |
| 449 | if (!write_sg) { |
| 450 | pr_err("Unable to allocate compare_and_write sg\n"); |
| 451 | ret = TCM_OUT_OF_RESOURCES; |
| 452 | goto out; |
| 453 | } |
Martin Svec | a1e1774 | 2014-04-01 16:03:02 +0200 | [diff] [blame] | 454 | sg_init_table(write_sg, cmd->t_data_nents); |
Nicholas Bellinger | 68ff9b9 | 2013-08-19 15:20:28 -0700 | [diff] [blame] | 455 | /* |
| 456 | * Setup verify and write data payloads from total NumberLBAs. |
| 457 | */ |
| 458 | rc = sg_copy_to_buffer(cmd->t_data_sg, cmd->t_data_nents, buf, |
| 459 | cmd->data_length); |
| 460 | if (!rc) { |
| 461 | pr_err("sg_copy_to_buffer() failed for compare_and_write\n"); |
| 462 | ret = TCM_OUT_OF_RESOURCES; |
| 463 | goto out; |
| 464 | } |
| 465 | /* |
| 466 | * Compare against SCSI READ payload against verify payload |
| 467 | */ |
| 468 | for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, i) { |
| 469 | addr = (unsigned char *)kmap_atomic(sg_page(sg)); |
| 470 | if (!addr) { |
| 471 | ret = TCM_OUT_OF_RESOURCES; |
| 472 | goto out; |
| 473 | } |
| 474 | |
| 475 | len = min(sg->length, compare_len); |
| 476 | |
| 477 | if (memcmp(addr, buf + offset, len)) { |
| 478 | pr_warn("Detected MISCOMPARE for addr: %p buf: %p\n", |
| 479 | addr, buf + offset); |
| 480 | kunmap_atomic(addr); |
| 481 | goto miscompare; |
| 482 | } |
| 483 | kunmap_atomic(addr); |
| 484 | |
| 485 | offset += len; |
| 486 | compare_len -= len; |
| 487 | if (!compare_len) |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | i = 0; |
| 492 | len = cmd->t_task_nolb * block_size; |
| 493 | sg_miter_start(&m, cmd->t_data_sg, cmd->t_data_nents, SG_MITER_TO_SG); |
| 494 | /* |
| 495 | * Currently assumes NoLB=1 and SGLs are PAGE_SIZE.. |
| 496 | */ |
| 497 | while (len) { |
| 498 | sg_miter_next(&m); |
| 499 | |
| 500 | if (block_size < PAGE_SIZE) { |
| 501 | sg_set_page(&write_sg[i], m.page, block_size, |
| 502 | block_size); |
| 503 | } else { |
| 504 | sg_miter_next(&m); |
| 505 | sg_set_page(&write_sg[i], m.page, block_size, |
| 506 | 0); |
| 507 | } |
| 508 | len -= block_size; |
| 509 | i++; |
| 510 | } |
| 511 | sg_miter_stop(&m); |
| 512 | /* |
| 513 | * Save the original SGL + nents values before updating to new |
| 514 | * assignments, to be released in transport_free_pages() -> |
| 515 | * transport_reset_sgl_orig() |
| 516 | */ |
| 517 | cmd->t_data_sg_orig = cmd->t_data_sg; |
| 518 | cmd->t_data_sg = write_sg; |
| 519 | cmd->t_data_nents_orig = cmd->t_data_nents; |
| 520 | cmd->t_data_nents = 1; |
| 521 | |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 522 | cmd->sam_task_attr = TCM_HEAD_TAG; |
Nicholas Bellinger | 68ff9b9 | 2013-08-19 15:20:28 -0700 | [diff] [blame] | 523 | cmd->transport_complete_callback = compare_and_write_post; |
| 524 | /* |
| 525 | * Now reset ->execute_cmd() to the normal sbc_execute_rw() handler |
| 526 | * for submitting the adjusted SGL to write instance user-data. |
| 527 | */ |
| 528 | cmd->execute_cmd = sbc_execute_rw; |
| 529 | |
| 530 | spin_lock_irq(&cmd->t_state_lock); |
| 531 | cmd->t_state = TRANSPORT_PROCESSING; |
| 532 | cmd->transport_state |= CMD_T_ACTIVE|CMD_T_BUSY|CMD_T_SENT; |
| 533 | spin_unlock_irq(&cmd->t_state_lock); |
| 534 | |
| 535 | __target_execute_cmd(cmd); |
| 536 | |
| 537 | kfree(buf); |
| 538 | return ret; |
| 539 | |
| 540 | miscompare: |
| 541 | pr_warn("Target/%s: Send MISCOMPARE check condition and sense\n", |
| 542 | dev->transport->name); |
| 543 | ret = TCM_MISCOMPARE_VERIFY; |
| 544 | out: |
| 545 | /* |
| 546 | * In the MISCOMPARE or failure case, unlock ->caw_sem obtained in |
| 547 | * sbc_compare_and_write() before the original READ I/O submission. |
| 548 | */ |
| 549 | up(&dev->caw_sem); |
| 550 | kfree(write_sg); |
| 551 | kfree(buf); |
| 552 | return ret; |
| 553 | } |
| 554 | |
| 555 | static sense_reason_t |
| 556 | sbc_compare_and_write(struct se_cmd *cmd) |
| 557 | { |
| 558 | struct se_device *dev = cmd->se_dev; |
| 559 | sense_reason_t ret; |
| 560 | int rc; |
| 561 | /* |
| 562 | * Submit the READ first for COMPARE_AND_WRITE to perform the |
| 563 | * comparision using SGLs at cmd->t_bidi_data_sg.. |
| 564 | */ |
| 565 | rc = down_interruptible(&dev->caw_sem); |
| 566 | if ((rc != 0) || signal_pending(current)) { |
| 567 | cmd->transport_complete_callback = NULL; |
| 568 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 569 | } |
Nicholas Bellinger | b719125 | 2013-10-01 16:46:37 -0700 | [diff] [blame] | 570 | /* |
| 571 | * Reset cmd->data_length to individual block_size in order to not |
| 572 | * confuse backend drivers that depend on this value matching the |
| 573 | * size of the I/O being submitted. |
| 574 | */ |
| 575 | cmd->data_length = cmd->t_task_nolb * dev->dev_attrib.block_size; |
Nicholas Bellinger | 68ff9b9 | 2013-08-19 15:20:28 -0700 | [diff] [blame] | 576 | |
| 577 | ret = cmd->execute_rw(cmd, cmd->t_bidi_data_sg, cmd->t_bidi_data_nents, |
| 578 | DMA_FROM_DEVICE); |
| 579 | if (ret) { |
| 580 | cmd->transport_complete_callback = NULL; |
| 581 | up(&dev->caw_sem); |
| 582 | return ret; |
| 583 | } |
| 584 | /* |
| 585 | * Unlock of dev->caw_sem to occur in compare_and_write_callback() |
| 586 | * upon MISCOMPARE, or in compare_and_write_done() upon completion |
| 587 | * of WRITE instance user-data. |
| 588 | */ |
| 589 | return TCM_NO_SENSE; |
| 590 | } |
| 591 | |
Sagi Grimberg | 19f9361 | 2014-02-19 17:50:15 +0200 | [diff] [blame] | 592 | static int |
Nicholas Bellinger | 38b57f8 | 2015-02-27 22:05:21 -0800 | [diff] [blame] | 593 | sbc_set_prot_op_checks(u8 protect, bool fabric_prot, enum target_prot_type prot_type, |
Sagi Grimberg | 19f9361 | 2014-02-19 17:50:15 +0200 | [diff] [blame] | 594 | bool is_write, struct se_cmd *cmd) |
| 595 | { |
| 596 | if (is_write) { |
Nicholas Bellinger | 38b57f8 | 2015-02-27 22:05:21 -0800 | [diff] [blame] | 597 | cmd->prot_op = fabric_prot ? TARGET_PROT_DOUT_STRIP : |
| 598 | protect ? TARGET_PROT_DOUT_PASS : |
| 599 | TARGET_PROT_DOUT_INSERT; |
Sagi Grimberg | 19f9361 | 2014-02-19 17:50:15 +0200 | [diff] [blame] | 600 | switch (protect) { |
| 601 | case 0x0: |
| 602 | case 0x3: |
| 603 | cmd->prot_checks = 0; |
| 604 | break; |
| 605 | case 0x1: |
| 606 | case 0x5: |
| 607 | cmd->prot_checks = TARGET_DIF_CHECK_GUARD; |
| 608 | if (prot_type == TARGET_DIF_TYPE1_PROT) |
| 609 | cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG; |
| 610 | break; |
| 611 | case 0x2: |
| 612 | if (prot_type == TARGET_DIF_TYPE1_PROT) |
| 613 | cmd->prot_checks = TARGET_DIF_CHECK_REFTAG; |
| 614 | break; |
| 615 | case 0x4: |
| 616 | cmd->prot_checks = TARGET_DIF_CHECK_GUARD; |
| 617 | break; |
| 618 | default: |
| 619 | pr_err("Unsupported protect field %d\n", protect); |
| 620 | return -EINVAL; |
| 621 | } |
| 622 | } else { |
Nicholas Bellinger | 38b57f8 | 2015-02-27 22:05:21 -0800 | [diff] [blame] | 623 | cmd->prot_op = fabric_prot ? TARGET_PROT_DIN_INSERT : |
| 624 | protect ? TARGET_PROT_DIN_PASS : |
| 625 | TARGET_PROT_DIN_STRIP; |
Sagi Grimberg | 19f9361 | 2014-02-19 17:50:15 +0200 | [diff] [blame] | 626 | switch (protect) { |
| 627 | case 0x0: |
| 628 | case 0x1: |
| 629 | case 0x5: |
| 630 | cmd->prot_checks = TARGET_DIF_CHECK_GUARD; |
| 631 | if (prot_type == TARGET_DIF_TYPE1_PROT) |
| 632 | cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG; |
| 633 | break; |
| 634 | case 0x2: |
| 635 | if (prot_type == TARGET_DIF_TYPE1_PROT) |
| 636 | cmd->prot_checks = TARGET_DIF_CHECK_REFTAG; |
| 637 | break; |
| 638 | case 0x3: |
| 639 | cmd->prot_checks = 0; |
| 640 | break; |
| 641 | case 0x4: |
| 642 | cmd->prot_checks = TARGET_DIF_CHECK_GUARD; |
| 643 | break; |
| 644 | default: |
| 645 | pr_err("Unsupported protect field %d\n", protect); |
| 646 | return -EINVAL; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
Nicholas Bellinger | f7b7c06 | 2015-02-13 22:49:38 +0000 | [diff] [blame] | 653 | static sense_reason_t |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 654 | sbc_check_prot(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb, |
Sagi Grimberg | 19f9361 | 2014-02-19 17:50:15 +0200 | [diff] [blame] | 655 | u32 sectors, bool is_write) |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 656 | { |
Sagi Grimberg | 19f9361 | 2014-02-19 17:50:15 +0200 | [diff] [blame] | 657 | u8 protect = cdb[1] >> 5; |
Nicholas Bellinger | 38b57f8 | 2015-02-27 22:05:21 -0800 | [diff] [blame] | 658 | int sp_ops = cmd->se_sess->sup_prot_ops; |
| 659 | int pi_prot_type = dev->dev_attrib.pi_prot_type; |
| 660 | bool fabric_prot = false; |
Sagi Grimberg | 19f9361 | 2014-02-19 17:50:15 +0200 | [diff] [blame] | 661 | |
Nicholas Bellinger | f7b7c06 | 2015-02-13 22:49:38 +0000 | [diff] [blame] | 662 | if (!cmd->t_prot_sg || !cmd->t_prot_nents) { |
Nicholas Bellinger | 38b57f8 | 2015-02-27 22:05:21 -0800 | [diff] [blame] | 663 | if (unlikely(protect && |
| 664 | !dev->dev_attrib.pi_prot_type && !cmd->se_sess->sess_prot_type)) { |
| 665 | pr_err("CDB contains protect bit, but device + fabric does" |
| 666 | " not advertise PROTECT=1 feature bit\n"); |
Nicholas Bellinger | f7b7c06 | 2015-02-13 22:49:38 +0000 | [diff] [blame] | 667 | return TCM_INVALID_CDB_FIELD; |
| 668 | } |
| 669 | if (cmd->prot_pto) |
| 670 | return TCM_NO_SENSE; |
| 671 | } |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 672 | |
| 673 | switch (dev->dev_attrib.pi_prot_type) { |
| 674 | case TARGET_DIF_TYPE3_PROT: |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 675 | cmd->reftag_seed = 0xffffffff; |
| 676 | break; |
| 677 | case TARGET_DIF_TYPE2_PROT: |
Sagi Grimberg | 19f9361 | 2014-02-19 17:50:15 +0200 | [diff] [blame] | 678 | if (protect) |
Nicholas Bellinger | f7b7c06 | 2015-02-13 22:49:38 +0000 | [diff] [blame] | 679 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 680 | |
| 681 | cmd->reftag_seed = cmd->t_task_lba; |
| 682 | break; |
| 683 | case TARGET_DIF_TYPE1_PROT: |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 684 | cmd->reftag_seed = cmd->t_task_lba; |
| 685 | break; |
| 686 | case TARGET_DIF_TYPE0_PROT: |
Nicholas Bellinger | 38b57f8 | 2015-02-27 22:05:21 -0800 | [diff] [blame] | 687 | /* |
| 688 | * See if the fabric supports T10-PI, and the session has been |
| 689 | * configured to allow export PROTECT=1 feature bit with backend |
| 690 | * devices that don't support T10-PI. |
| 691 | */ |
| 692 | fabric_prot = is_write ? |
| 693 | !!(sp_ops & (TARGET_PROT_DOUT_PASS | TARGET_PROT_DOUT_STRIP)) : |
| 694 | !!(sp_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DIN_INSERT)); |
| 695 | |
| 696 | if (fabric_prot && cmd->se_sess->sess_prot_type) { |
| 697 | pi_prot_type = cmd->se_sess->sess_prot_type; |
| 698 | break; |
| 699 | } |
| 700 | /* Fallthrough */ |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 701 | default: |
Nicholas Bellinger | f7b7c06 | 2015-02-13 22:49:38 +0000 | [diff] [blame] | 702 | return TCM_NO_SENSE; |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Nicholas Bellinger | 38b57f8 | 2015-02-27 22:05:21 -0800 | [diff] [blame] | 705 | if (sbc_set_prot_op_checks(protect, fabric_prot, pi_prot_type, is_write, cmd)) |
Nicholas Bellinger | f7b7c06 | 2015-02-13 22:49:38 +0000 | [diff] [blame] | 706 | return TCM_INVALID_CDB_FIELD; |
Sagi Grimberg | 19f9361 | 2014-02-19 17:50:15 +0200 | [diff] [blame] | 707 | |
Nicholas Bellinger | 38b57f8 | 2015-02-27 22:05:21 -0800 | [diff] [blame] | 708 | cmd->prot_type = pi_prot_type; |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 709 | cmd->prot_length = dev->prot_length * sectors; |
Sagi Grimberg | e2a4f55 | 2014-06-11 12:09:59 +0300 | [diff] [blame] | 710 | |
| 711 | /** |
| 712 | * In case protection information exists over the wire |
| 713 | * we modify command data length to describe pure data. |
| 714 | * The actual transfer length is data length + protection |
| 715 | * length |
| 716 | **/ |
| 717 | if (protect) |
| 718 | cmd->data_length = sectors * dev->dev_attrib.block_size; |
| 719 | |
| 720 | pr_debug("%s: prot_type=%d, data_length=%d, prot_length=%d " |
| 721 | "prot_op=%d prot_checks=%d\n", |
| 722 | __func__, cmd->prot_type, cmd->data_length, cmd->prot_length, |
Sagi Grimberg | 03abad9 | 2014-02-19 17:50:21 +0200 | [diff] [blame] | 723 | cmd->prot_op, cmd->prot_checks); |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 724 | |
Nicholas Bellinger | f7b7c06 | 2015-02-13 22:49:38 +0000 | [diff] [blame] | 725 | return TCM_NO_SENSE; |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Nicholas Bellinger | fde9f50 | 2015-02-13 23:28:27 +0000 | [diff] [blame] | 728 | static int |
| 729 | sbc_check_dpofua(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb) |
| 730 | { |
| 731 | if (cdb[1] & 0x10) { |
| 732 | if (!dev->dev_attrib.emulate_dpo) { |
| 733 | pr_err("Got CDB: 0x%02x with DPO bit set, but device" |
| 734 | " does not advertise support for DPO\n", cdb[0]); |
| 735 | return -EINVAL; |
| 736 | } |
| 737 | } |
| 738 | if (cdb[1] & 0x8) { |
| 739 | if (!dev->dev_attrib.emulate_fua_write || |
| 740 | !dev->dev_attrib.emulate_write_cache) { |
| 741 | pr_err("Got CDB: 0x%02x with FUA bit set, but device" |
| 742 | " does not advertise support for FUA write\n", |
| 743 | cdb[0]); |
| 744 | return -EINVAL; |
| 745 | } |
| 746 | cmd->se_cmd_flags |= SCF_FUA; |
| 747 | } |
| 748 | return 0; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 749 | } |
| 750 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 751 | sense_reason_t |
| 752 | sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops) |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 753 | { |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 754 | struct se_device *dev = cmd->se_dev; |
| 755 | unsigned char *cdb = cmd->t_task_cdb; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 756 | unsigned int size; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 757 | u32 sectors = 0; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 758 | sense_reason_t ret; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 759 | |
| 760 | switch (cdb[0]) { |
| 761 | case READ_6: |
| 762 | sectors = transport_get_sectors_6(cdb); |
| 763 | cmd->t_task_lba = transport_lba_21(cdb); |
| 764 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
Nicholas Bellinger | a82a953 | 2013-08-19 23:57:30 -0700 | [diff] [blame] | 765 | cmd->execute_rw = ops->execute_rw; |
| 766 | cmd->execute_cmd = sbc_execute_rw; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 767 | break; |
| 768 | case READ_10: |
| 769 | sectors = transport_get_sectors_10(cdb); |
| 770 | cmd->t_task_lba = transport_lba_32(cdb); |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 771 | |
Nicholas Bellinger | fde9f50 | 2015-02-13 23:28:27 +0000 | [diff] [blame] | 772 | if (sbc_check_dpofua(dev, cmd, cdb)) |
| 773 | return TCM_INVALID_CDB_FIELD; |
| 774 | |
Nicholas Bellinger | f7b7c06 | 2015-02-13 22:49:38 +0000 | [diff] [blame] | 775 | ret = sbc_check_prot(dev, cmd, cdb, sectors, false); |
| 776 | if (ret) |
| 777 | return ret; |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 778 | |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 779 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
Nicholas Bellinger | a82a953 | 2013-08-19 23:57:30 -0700 | [diff] [blame] | 780 | cmd->execute_rw = ops->execute_rw; |
| 781 | cmd->execute_cmd = sbc_execute_rw; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 782 | break; |
| 783 | case READ_12: |
| 784 | sectors = transport_get_sectors_12(cdb); |
| 785 | cmd->t_task_lba = transport_lba_32(cdb); |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 786 | |
Nicholas Bellinger | fde9f50 | 2015-02-13 23:28:27 +0000 | [diff] [blame] | 787 | if (sbc_check_dpofua(dev, cmd, cdb)) |
| 788 | return TCM_INVALID_CDB_FIELD; |
| 789 | |
Nicholas Bellinger | f7b7c06 | 2015-02-13 22:49:38 +0000 | [diff] [blame] | 790 | ret = sbc_check_prot(dev, cmd, cdb, sectors, false); |
| 791 | if (ret) |
| 792 | return ret; |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 793 | |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 794 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
Nicholas Bellinger | a82a953 | 2013-08-19 23:57:30 -0700 | [diff] [blame] | 795 | cmd->execute_rw = ops->execute_rw; |
| 796 | cmd->execute_cmd = sbc_execute_rw; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 797 | break; |
| 798 | case READ_16: |
| 799 | sectors = transport_get_sectors_16(cdb); |
| 800 | cmd->t_task_lba = transport_lba_64(cdb); |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 801 | |
Nicholas Bellinger | fde9f50 | 2015-02-13 23:28:27 +0000 | [diff] [blame] | 802 | if (sbc_check_dpofua(dev, cmd, cdb)) |
| 803 | return TCM_INVALID_CDB_FIELD; |
| 804 | |
Nicholas Bellinger | f7b7c06 | 2015-02-13 22:49:38 +0000 | [diff] [blame] | 805 | ret = sbc_check_prot(dev, cmd, cdb, sectors, false); |
| 806 | if (ret) |
| 807 | return ret; |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 808 | |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 809 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
Nicholas Bellinger | a82a953 | 2013-08-19 23:57:30 -0700 | [diff] [blame] | 810 | cmd->execute_rw = ops->execute_rw; |
| 811 | cmd->execute_cmd = sbc_execute_rw; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 812 | break; |
| 813 | case WRITE_6: |
| 814 | sectors = transport_get_sectors_6(cdb); |
| 815 | cmd->t_task_lba = transport_lba_21(cdb); |
| 816 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
Nicholas Bellinger | a82a953 | 2013-08-19 23:57:30 -0700 | [diff] [blame] | 817 | cmd->execute_rw = ops->execute_rw; |
| 818 | cmd->execute_cmd = sbc_execute_rw; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 819 | break; |
| 820 | case WRITE_10: |
| 821 | case WRITE_VERIFY: |
| 822 | sectors = transport_get_sectors_10(cdb); |
| 823 | cmd->t_task_lba = transport_lba_32(cdb); |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 824 | |
Nicholas Bellinger | fde9f50 | 2015-02-13 23:28:27 +0000 | [diff] [blame] | 825 | if (sbc_check_dpofua(dev, cmd, cdb)) |
| 826 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 827 | |
Nicholas Bellinger | f7b7c06 | 2015-02-13 22:49:38 +0000 | [diff] [blame] | 828 | ret = sbc_check_prot(dev, cmd, cdb, sectors, true); |
| 829 | if (ret) |
| 830 | return ret; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 831 | |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 832 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
Nicholas Bellinger | a82a953 | 2013-08-19 23:57:30 -0700 | [diff] [blame] | 833 | cmd->execute_rw = ops->execute_rw; |
| 834 | cmd->execute_cmd = sbc_execute_rw; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 835 | break; |
| 836 | case WRITE_12: |
| 837 | sectors = transport_get_sectors_12(cdb); |
| 838 | cmd->t_task_lba = transport_lba_32(cdb); |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 839 | |
Nicholas Bellinger | fde9f50 | 2015-02-13 23:28:27 +0000 | [diff] [blame] | 840 | if (sbc_check_dpofua(dev, cmd, cdb)) |
| 841 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 842 | |
Nicholas Bellinger | f7b7c06 | 2015-02-13 22:49:38 +0000 | [diff] [blame] | 843 | ret = sbc_check_prot(dev, cmd, cdb, sectors, true); |
| 844 | if (ret) |
| 845 | return ret; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 846 | |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 847 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
Nicholas Bellinger | a82a953 | 2013-08-19 23:57:30 -0700 | [diff] [blame] | 848 | cmd->execute_rw = ops->execute_rw; |
| 849 | cmd->execute_cmd = sbc_execute_rw; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 850 | break; |
| 851 | case WRITE_16: |
| 852 | sectors = transport_get_sectors_16(cdb); |
| 853 | cmd->t_task_lba = transport_lba_64(cdb); |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 854 | |
Nicholas Bellinger | fde9f50 | 2015-02-13 23:28:27 +0000 | [diff] [blame] | 855 | if (sbc_check_dpofua(dev, cmd, cdb)) |
| 856 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | 499bf77 | 2013-12-23 20:30:03 +0000 | [diff] [blame] | 857 | |
Nicholas Bellinger | f7b7c06 | 2015-02-13 22:49:38 +0000 | [diff] [blame] | 858 | ret = sbc_check_prot(dev, cmd, cdb, sectors, true); |
| 859 | if (ret) |
| 860 | return ret; |
Nicholas Bellinger | cd063be | 2012-11-07 20:01:10 -0800 | [diff] [blame] | 861 | |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 862 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
| 863 | cmd->execute_rw = ops->execute_rw; |
| 864 | cmd->execute_cmd = sbc_execute_rw; |
| 865 | break; |
| 866 | case XDWRITEREAD_10: |
| 867 | if (cmd->data_direction != DMA_TO_DEVICE || |
| 868 | !(cmd->se_cmd_flags & SCF_BIDI)) |
| 869 | return TCM_INVALID_CDB_FIELD; |
| 870 | sectors = transport_get_sectors_10(cdb); |
| 871 | |
Nicholas Bellinger | fde9f50 | 2015-02-13 23:28:27 +0000 | [diff] [blame] | 872 | if (sbc_check_dpofua(dev, cmd, cdb)) |
| 873 | return TCM_INVALID_CDB_FIELD; |
| 874 | |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 875 | cmd->t_task_lba = transport_lba_32(cdb); |
| 876 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
| 877 | |
| 878 | /* |
| 879 | * Setup BIDI XOR callback to be run after I/O completion. |
| 880 | */ |
| 881 | cmd->execute_rw = ops->execute_rw; |
| 882 | cmd->execute_cmd = sbc_execute_rw; |
| 883 | cmd->transport_complete_callback = &xdreadwrite_callback; |
Christoph Hellwig | 0c2ad7d | 2012-06-17 18:40:52 -0400 | [diff] [blame] | 884 | break; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 885 | case VARIABLE_LENGTH_CMD: |
| 886 | { |
| 887 | u16 service_action = get_unaligned_be16(&cdb[8]); |
| 888 | switch (service_action) { |
| 889 | case XDWRITEREAD_32: |
Christoph Hellwig | 6f974e8 | 2012-06-17 18:40:54 -0400 | [diff] [blame] | 890 | sectors = transport_get_sectors_32(cdb); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 891 | |
Nicholas Bellinger | fde9f50 | 2015-02-13 23:28:27 +0000 | [diff] [blame] | 892 | if (sbc_check_dpofua(dev, cmd, cdb)) |
| 893 | return TCM_INVALID_CDB_FIELD; |
Christoph Hellwig | 6f974e8 | 2012-06-17 18:40:54 -0400 | [diff] [blame] | 894 | /* |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 895 | * Use WRITE_32 and READ_32 opcodes for the emulated |
| 896 | * XDWRITE_READ_32 logic. |
| 897 | */ |
| 898 | cmd->t_task_lba = transport_lba_64_ext(cdb); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 899 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 900 | |
| 901 | /* |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 902 | * Setup BIDI XOR callback to be run during after I/O |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 903 | * completion. |
| 904 | */ |
| 905 | cmd->execute_rw = ops->execute_rw; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 906 | cmd->execute_cmd = sbc_execute_rw; |
Christoph Hellwig | 6f974e8 | 2012-06-17 18:40:54 -0400 | [diff] [blame] | 907 | cmd->transport_complete_callback = &xdreadwrite_callback; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 908 | break; |
| 909 | case WRITE_SAME_32: |
| 910 | sectors = transport_get_sectors_32(cdb); |
| 911 | if (!sectors) { |
| 912 | pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not" |
| 913 | " supported\n"); |
| 914 | return TCM_INVALID_CDB_FIELD; |
| 915 | } |
| 916 | |
| 917 | size = sbc_get_size(cmd, 1); |
| 918 | cmd->t_task_lba = get_unaligned_be64(&cdb[12]); |
| 919 | |
| 920 | ret = sbc_setup_write_same(cmd, &cdb[10], ops); |
| 921 | if (ret) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 922 | return ret; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 923 | break; |
| 924 | default: |
| 925 | pr_err("VARIABLE_LENGTH_CMD service action" |
| 926 | " 0x%04x not supported\n", service_action); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 927 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 928 | } |
| 929 | break; |
| 930 | } |
Nicholas Bellinger | 68ff9b9 | 2013-08-19 15:20:28 -0700 | [diff] [blame] | 931 | case COMPARE_AND_WRITE: |
| 932 | sectors = cdb[13]; |
| 933 | /* |
| 934 | * Currently enforce COMPARE_AND_WRITE for a single sector |
| 935 | */ |
| 936 | if (sectors > 1) { |
| 937 | pr_err("COMPARE_AND_WRITE contains NoLB: %u greater" |
| 938 | " than 1\n", sectors); |
| 939 | return TCM_INVALID_CDB_FIELD; |
| 940 | } |
| 941 | /* |
| 942 | * Double size because we have two buffers, note that |
| 943 | * zero is not an error.. |
| 944 | */ |
| 945 | size = 2 * sbc_get_size(cmd, sectors); |
| 946 | cmd->t_task_lba = get_unaligned_be64(&cdb[2]); |
| 947 | cmd->t_task_nolb = sectors; |
| 948 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB | SCF_COMPARE_AND_WRITE; |
| 949 | cmd->execute_rw = ops->execute_rw; |
| 950 | cmd->execute_cmd = sbc_compare_and_write; |
| 951 | cmd->transport_complete_callback = compare_and_write_callback; |
| 952 | break; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 953 | case READ_CAPACITY: |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 954 | size = READ_CAP_LEN; |
| 955 | cmd->execute_cmd = sbc_emulate_readcapacity; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 956 | break; |
Hannes Reinecke | eb846d9 | 2014-11-17 14:25:19 +0100 | [diff] [blame] | 957 | case SERVICE_ACTION_IN_16: |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 958 | switch (cmd->t_task_cdb[1] & 0x1f) { |
| 959 | case SAI_READ_CAPACITY_16: |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 960 | cmd->execute_cmd = sbc_emulate_readcapacity_16; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 961 | break; |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 962 | case SAI_REPORT_REFERRALS: |
| 963 | cmd->execute_cmd = target_emulate_report_referrals; |
| 964 | break; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 965 | default: |
| 966 | pr_err("Unsupported SA: 0x%02x\n", |
| 967 | cmd->t_task_cdb[1] & 0x1f); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 968 | return TCM_INVALID_CDB_FIELD; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 969 | } |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 970 | size = (cdb[10] << 24) | (cdb[11] << 16) | |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 971 | (cdb[12] << 8) | cdb[13]; |
| 972 | break; |
| 973 | case SYNCHRONIZE_CACHE: |
| 974 | case SYNCHRONIZE_CACHE_16: |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 975 | if (cdb[0] == SYNCHRONIZE_CACHE) { |
| 976 | sectors = transport_get_sectors_10(cdb); |
| 977 | cmd->t_task_lba = transport_lba_32(cdb); |
| 978 | } else { |
| 979 | sectors = transport_get_sectors_16(cdb); |
| 980 | cmd->t_task_lba = transport_lba_64(cdb); |
| 981 | } |
Christophe Vu-Brugier | 6ef31dc | 2014-06-10 17:53:21 +0200 | [diff] [blame] | 982 | if (ops->execute_sync_cache) { |
| 983 | cmd->execute_cmd = ops->execute_sync_cache; |
| 984 | goto check_lba; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 985 | } |
Christophe Vu-Brugier | 6ef31dc | 2014-06-10 17:53:21 +0200 | [diff] [blame] | 986 | size = 0; |
| 987 | cmd->execute_cmd = sbc_emulate_noop; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 988 | break; |
| 989 | case UNMAP: |
Christoph Hellwig | 14150a6 | 2012-06-17 18:40:55 -0400 | [diff] [blame] | 990 | if (!ops->execute_unmap) |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 991 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
Christoph Hellwig | 14150a6 | 2012-06-17 18:40:55 -0400 | [diff] [blame] | 992 | |
Nicholas Bellinger | 61fdb4a | 2015-02-14 02:29:50 +0000 | [diff] [blame] | 993 | if (!dev->dev_attrib.emulate_tpu) { |
| 994 | pr_err("Got UNMAP, but backend device has" |
| 995 | " emulate_tpu disabled\n"); |
| 996 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
| 997 | } |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 998 | size = get_unaligned_be16(&cdb[7]); |
Christoph Hellwig | 14150a6 | 2012-06-17 18:40:55 -0400 | [diff] [blame] | 999 | cmd->execute_cmd = ops->execute_unmap; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1000 | break; |
| 1001 | case WRITE_SAME_16: |
| 1002 | sectors = transport_get_sectors_16(cdb); |
| 1003 | if (!sectors) { |
| 1004 | pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1005 | return TCM_INVALID_CDB_FIELD; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1006 | } |
| 1007 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1008 | size = sbc_get_size(cmd, 1); |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1009 | cmd->t_task_lba = get_unaligned_be64(&cdb[2]); |
| 1010 | |
Nicholas Bellinger | cd063be | 2012-11-07 20:01:10 -0800 | [diff] [blame] | 1011 | ret = sbc_setup_write_same(cmd, &cdb[1], ops); |
Dan Carpenter | 6b64e1f | 2012-11-27 17:27:01 +0300 | [diff] [blame] | 1012 | if (ret) |
Nicholas Bellinger | cd063be | 2012-11-07 20:01:10 -0800 | [diff] [blame] | 1013 | return ret; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1014 | break; |
| 1015 | case WRITE_SAME: |
| 1016 | sectors = transport_get_sectors_10(cdb); |
| 1017 | if (!sectors) { |
| 1018 | pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1019 | return TCM_INVALID_CDB_FIELD; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1020 | } |
| 1021 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1022 | size = sbc_get_size(cmd, 1); |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1023 | cmd->t_task_lba = get_unaligned_be32(&cdb[2]); |
| 1024 | |
| 1025 | /* |
| 1026 | * Follow sbcr26 with WRITE_SAME (10) and check for the existence |
| 1027 | * of byte 1 bit 3 UNMAP instead of original reserved field |
| 1028 | */ |
Nicholas Bellinger | cd063be | 2012-11-07 20:01:10 -0800 | [diff] [blame] | 1029 | ret = sbc_setup_write_same(cmd, &cdb[1], ops); |
Dan Carpenter | 6b64e1f | 2012-11-27 17:27:01 +0300 | [diff] [blame] | 1030 | if (ret) |
Nicholas Bellinger | cd063be | 2012-11-07 20:01:10 -0800 | [diff] [blame] | 1031 | return ret; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1032 | break; |
| 1033 | case VERIFY: |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1034 | size = 0; |
Christophe Vu-Brugier | c52716d | 2014-06-10 17:53:22 +0200 | [diff] [blame] | 1035 | sectors = transport_get_sectors_10(cdb); |
| 1036 | cmd->t_task_lba = transport_lba_32(cdb); |
Nicholas Bellinger | 1920ed6 | 2012-11-06 20:59:41 -0800 | [diff] [blame] | 1037 | cmd->execute_cmd = sbc_emulate_noop; |
Christophe Vu-Brugier | c52716d | 2014-06-10 17:53:22 +0200 | [diff] [blame] | 1038 | goto check_lba; |
Bernhard Kohl | 1a1ff38 | 2012-10-24 15:53:58 +0200 | [diff] [blame] | 1039 | case REZERO_UNIT: |
| 1040 | case SEEK_6: |
| 1041 | case SEEK_10: |
| 1042 | /* |
| 1043 | * There are still clients out there which use these old SCSI-2 |
| 1044 | * commands. This mainly happens when running VMs with legacy |
| 1045 | * guest systems, connected via SCSI command pass-through to |
| 1046 | * iSCSI targets. Make them happy and return status GOOD. |
| 1047 | */ |
| 1048 | size = 0; |
| 1049 | cmd->execute_cmd = sbc_emulate_noop; |
| 1050 | break; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1051 | default: |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1052 | ret = spc_parse_cdb(cmd, &size); |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1053 | if (ret) |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
| 1057 | /* reject any command that we don't have a handler for */ |
Andy Grover | 20959c4 | 2014-10-01 16:07:02 -0700 | [diff] [blame] | 1058 | if (!cmd->execute_cmd) |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1059 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1060 | |
| 1061 | if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) { |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1062 | unsigned long long end_lba; |
Christophe Vu-Brugier | 6ef31dc | 2014-06-10 17:53:21 +0200 | [diff] [blame] | 1063 | check_lba: |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1064 | end_lba = dev->transport->get_blocks(dev) + 1; |
Nicholas Bellinger | aa17993 | 2015-02-13 22:27:40 +0000 | [diff] [blame] | 1065 | if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) || |
| 1066 | ((cmd->t_task_lba + sectors) > end_lba)) { |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1067 | pr_err("cmd exceeds last lba %llu " |
| 1068 | "(lba %llu, sectors %u)\n", |
| 1069 | end_lba, cmd->t_task_lba, sectors); |
Roland Dreier | 09ceadc | 2013-06-26 17:36:18 -0700 | [diff] [blame] | 1070 | return TCM_ADDRESS_OUT_OF_RANGE; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1071 | } |
| 1072 | |
Nicholas Bellinger | 68ff9b9 | 2013-08-19 15:20:28 -0700 | [diff] [blame] | 1073 | if (!(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) |
| 1074 | size = sbc_get_size(cmd, sectors); |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1075 | } |
| 1076 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1077 | return target_cmd_size_check(cmd, size); |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1078 | } |
| 1079 | EXPORT_SYMBOL(sbc_parse_cdb); |
Christoph Hellwig | 6f23ac8 | 2012-10-07 10:55:53 -0400 | [diff] [blame] | 1080 | |
Christoph Hellwig | 6f23ac8 | 2012-10-07 10:55:53 -0400 | [diff] [blame] | 1081 | u32 sbc_get_device_type(struct se_device *dev) |
| 1082 | { |
| 1083 | return TYPE_DISK; |
| 1084 | } |
| 1085 | EXPORT_SYMBOL(sbc_get_device_type); |
Asias He | 86d7182 | 2013-02-25 14:03:46 +0800 | [diff] [blame] | 1086 | |
| 1087 | sense_reason_t |
| 1088 | sbc_execute_unmap(struct se_cmd *cmd, |
| 1089 | sense_reason_t (*do_unmap_fn)(struct se_cmd *, void *, |
| 1090 | sector_t, sector_t), |
| 1091 | void *priv) |
| 1092 | { |
| 1093 | struct se_device *dev = cmd->se_dev; |
| 1094 | unsigned char *buf, *ptr = NULL; |
| 1095 | sector_t lba; |
| 1096 | int size; |
| 1097 | u32 range; |
| 1098 | sense_reason_t ret = 0; |
| 1099 | int dl, bd_dl; |
| 1100 | |
| 1101 | /* We never set ANC_SUP */ |
| 1102 | if (cmd->t_task_cdb[1]) |
| 1103 | return TCM_INVALID_CDB_FIELD; |
| 1104 | |
| 1105 | if (cmd->data_length == 0) { |
| 1106 | target_complete_cmd(cmd, SAM_STAT_GOOD); |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | if (cmd->data_length < 8) { |
| 1111 | pr_warn("UNMAP parameter list length %u too small\n", |
| 1112 | cmd->data_length); |
| 1113 | return TCM_PARAMETER_LIST_LENGTH_ERROR; |
| 1114 | } |
| 1115 | |
| 1116 | buf = transport_kmap_data_sg(cmd); |
| 1117 | if (!buf) |
| 1118 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 1119 | |
| 1120 | dl = get_unaligned_be16(&buf[0]); |
| 1121 | bd_dl = get_unaligned_be16(&buf[2]); |
| 1122 | |
| 1123 | size = cmd->data_length - 8; |
| 1124 | if (bd_dl > size) |
| 1125 | pr_warn("UNMAP parameter list length %u too small, ignoring bd_dl %u\n", |
| 1126 | cmd->data_length, bd_dl); |
| 1127 | else |
| 1128 | size = bd_dl; |
| 1129 | |
| 1130 | if (size / 16 > dev->dev_attrib.max_unmap_block_desc_count) { |
| 1131 | ret = TCM_INVALID_PARAMETER_LIST; |
| 1132 | goto err; |
| 1133 | } |
| 1134 | |
| 1135 | /* First UNMAP block descriptor starts at 8 byte offset */ |
| 1136 | ptr = &buf[8]; |
| 1137 | pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u" |
| 1138 | " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr); |
| 1139 | |
| 1140 | while (size >= 16) { |
| 1141 | lba = get_unaligned_be64(&ptr[0]); |
| 1142 | range = get_unaligned_be32(&ptr[8]); |
| 1143 | pr_debug("UNMAP: Using lba: %llu and range: %u\n", |
| 1144 | (unsigned long long)lba, range); |
| 1145 | |
| 1146 | if (range > dev->dev_attrib.max_unmap_lba_count) { |
| 1147 | ret = TCM_INVALID_PARAMETER_LIST; |
| 1148 | goto err; |
| 1149 | } |
| 1150 | |
| 1151 | if (lba + range > dev->transport->get_blocks(dev) + 1) { |
| 1152 | ret = TCM_ADDRESS_OUT_OF_RANGE; |
| 1153 | goto err; |
| 1154 | } |
| 1155 | |
| 1156 | ret = do_unmap_fn(cmd, priv, lba, range); |
| 1157 | if (ret) |
| 1158 | goto err; |
| 1159 | |
| 1160 | ptr += 16; |
| 1161 | size -= 16; |
| 1162 | } |
| 1163 | |
| 1164 | err: |
| 1165 | transport_kunmap_data_sg(cmd); |
| 1166 | if (!ret) |
| 1167 | target_complete_cmd(cmd, GOOD); |
| 1168 | return ret; |
| 1169 | } |
| 1170 | EXPORT_SYMBOL(sbc_execute_unmap); |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1171 | |
Nicholas Bellinger | 66a3d5b | 2014-04-02 14:19:09 -0700 | [diff] [blame] | 1172 | void |
| 1173 | sbc_dif_generate(struct se_cmd *cmd) |
| 1174 | { |
| 1175 | struct se_device *dev = cmd->se_dev; |
| 1176 | struct se_dif_v1_tuple *sdt; |
| 1177 | struct scatterlist *dsg, *psg = cmd->t_prot_sg; |
| 1178 | sector_t sector = cmd->t_task_lba; |
| 1179 | void *daddr, *paddr; |
| 1180 | int i, j, offset = 0; |
| 1181 | |
| 1182 | for_each_sg(cmd->t_data_sg, dsg, cmd->t_data_nents, i) { |
| 1183 | daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; |
| 1184 | paddr = kmap_atomic(sg_page(psg)) + psg->offset; |
| 1185 | |
| 1186 | for (j = 0; j < dsg->length; j += dev->dev_attrib.block_size) { |
| 1187 | |
| 1188 | if (offset >= psg->length) { |
| 1189 | kunmap_atomic(paddr); |
| 1190 | psg = sg_next(psg); |
| 1191 | paddr = kmap_atomic(sg_page(psg)) + psg->offset; |
| 1192 | offset = 0; |
| 1193 | } |
| 1194 | |
| 1195 | sdt = paddr + offset; |
| 1196 | sdt->guard_tag = cpu_to_be16(crc_t10dif(daddr + j, |
| 1197 | dev->dev_attrib.block_size)); |
Nicholas Bellinger | 823ddd8 | 2015-02-27 22:42:11 -0800 | [diff] [blame] | 1198 | if (cmd->prot_type == TARGET_DIF_TYPE1_PROT) |
Nicholas Bellinger | 66a3d5b | 2014-04-02 14:19:09 -0700 | [diff] [blame] | 1199 | sdt->ref_tag = cpu_to_be32(sector & 0xffffffff); |
| 1200 | sdt->app_tag = 0; |
| 1201 | |
| 1202 | pr_debug("DIF WRITE INSERT sector: %llu guard_tag: 0x%04x" |
| 1203 | " app_tag: 0x%04x ref_tag: %u\n", |
| 1204 | (unsigned long long)sector, sdt->guard_tag, |
| 1205 | sdt->app_tag, be32_to_cpu(sdt->ref_tag)); |
| 1206 | |
| 1207 | sector++; |
| 1208 | offset += sizeof(struct se_dif_v1_tuple); |
| 1209 | } |
| 1210 | |
| 1211 | kunmap_atomic(paddr); |
| 1212 | kunmap_atomic(daddr); |
| 1213 | } |
| 1214 | } |
| 1215 | |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1216 | static sense_reason_t |
Nicholas Bellinger | 823ddd8 | 2015-02-27 22:42:11 -0800 | [diff] [blame] | 1217 | sbc_dif_v1_verify(struct se_cmd *cmd, struct se_dif_v1_tuple *sdt, |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1218 | const void *p, sector_t sector, unsigned int ei_lba) |
| 1219 | { |
Nicholas Bellinger | 823ddd8 | 2015-02-27 22:42:11 -0800 | [diff] [blame] | 1220 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1221 | int block_size = dev->dev_attrib.block_size; |
| 1222 | __be16 csum; |
| 1223 | |
| 1224 | csum = cpu_to_be16(crc_t10dif(p, block_size)); |
| 1225 | |
| 1226 | if (sdt->guard_tag != csum) { |
| 1227 | pr_err("DIFv1 checksum failed on sector %llu guard tag 0x%04x" |
| 1228 | " csum 0x%04x\n", (unsigned long long)sector, |
| 1229 | be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum)); |
| 1230 | return TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED; |
| 1231 | } |
| 1232 | |
Nicholas Bellinger | 823ddd8 | 2015-02-27 22:42:11 -0800 | [diff] [blame] | 1233 | if (cmd->prot_type == TARGET_DIF_TYPE1_PROT && |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1234 | be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) { |
| 1235 | pr_err("DIFv1 Type 1 reference failed on sector: %llu tag: 0x%08x" |
| 1236 | " sector MSB: 0x%08x\n", (unsigned long long)sector, |
| 1237 | be32_to_cpu(sdt->ref_tag), (u32)(sector & 0xffffffff)); |
| 1238 | return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; |
| 1239 | } |
| 1240 | |
Nicholas Bellinger | 823ddd8 | 2015-02-27 22:42:11 -0800 | [diff] [blame] | 1241 | if (cmd->prot_type == TARGET_DIF_TYPE2_PROT && |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1242 | be32_to_cpu(sdt->ref_tag) != ei_lba) { |
| 1243 | pr_err("DIFv1 Type 2 reference failed on sector: %llu tag: 0x%08x" |
| 1244 | " ei_lba: 0x%08x\n", (unsigned long long)sector, |
| 1245 | be32_to_cpu(sdt->ref_tag), ei_lba); |
| 1246 | return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; |
| 1247 | } |
| 1248 | |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | static void |
| 1253 | sbc_dif_copy_prot(struct se_cmd *cmd, unsigned int sectors, bool read, |
| 1254 | struct scatterlist *sg, int sg_off) |
| 1255 | { |
| 1256 | struct se_device *dev = cmd->se_dev; |
| 1257 | struct scatterlist *psg; |
| 1258 | void *paddr, *addr; |
| 1259 | unsigned int i, len, left; |
Nicholas Bellinger | 10762e8 | 2014-02-23 12:52:44 +0000 | [diff] [blame] | 1260 | unsigned int offset = sg_off; |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1261 | |
Nicholas Bellinger | 38b57f8 | 2015-02-27 22:05:21 -0800 | [diff] [blame] | 1262 | if (!sg) |
| 1263 | return; |
| 1264 | |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1265 | left = sectors * dev->prot_length; |
| 1266 | |
| 1267 | for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) { |
Sagi Grimberg | 16c0ae0 | 2014-03-05 14:05:09 +0200 | [diff] [blame] | 1268 | unsigned int psg_len, copied = 0; |
Sagi Grimberg | d6a65fd | 2014-02-12 11:40:25 +0200 | [diff] [blame] | 1269 | |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1270 | paddr = kmap_atomic(sg_page(psg)) + psg->offset; |
Sagi Grimberg | 16c0ae0 | 2014-03-05 14:05:09 +0200 | [diff] [blame] | 1271 | psg_len = min(left, psg->length); |
| 1272 | while (psg_len) { |
| 1273 | len = min(psg_len, sg->length - offset); |
| 1274 | addr = kmap_atomic(sg_page(sg)) + sg->offset + offset; |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1275 | |
Sagi Grimberg | 16c0ae0 | 2014-03-05 14:05:09 +0200 | [diff] [blame] | 1276 | if (read) |
| 1277 | memcpy(paddr + copied, addr, len); |
| 1278 | else |
| 1279 | memcpy(addr, paddr + copied, len); |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1280 | |
Sagi Grimberg | 16c0ae0 | 2014-03-05 14:05:09 +0200 | [diff] [blame] | 1281 | left -= len; |
| 1282 | offset += len; |
| 1283 | copied += len; |
| 1284 | psg_len -= len; |
| 1285 | |
| 1286 | if (offset >= sg->length) { |
| 1287 | sg = sg_next(sg); |
| 1288 | offset = 0; |
| 1289 | } |
| 1290 | kunmap_atomic(addr); |
| 1291 | } |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1292 | kunmap_atomic(paddr); |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | sense_reason_t |
| 1297 | sbc_dif_verify_write(struct se_cmd *cmd, sector_t start, unsigned int sectors, |
| 1298 | unsigned int ei_lba, struct scatterlist *sg, int sg_off) |
| 1299 | { |
| 1300 | struct se_device *dev = cmd->se_dev; |
| 1301 | struct se_dif_v1_tuple *sdt; |
| 1302 | struct scatterlist *dsg, *psg = cmd->t_prot_sg; |
| 1303 | sector_t sector = start; |
| 1304 | void *daddr, *paddr; |
| 1305 | int i, j, offset = 0; |
| 1306 | sense_reason_t rc; |
| 1307 | |
| 1308 | for_each_sg(cmd->t_data_sg, dsg, cmd->t_data_nents, i) { |
| 1309 | daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; |
| 1310 | paddr = kmap_atomic(sg_page(psg)) + psg->offset; |
| 1311 | |
| 1312 | for (j = 0; j < dsg->length; j += dev->dev_attrib.block_size) { |
| 1313 | |
| 1314 | if (offset >= psg->length) { |
| 1315 | kunmap_atomic(paddr); |
| 1316 | psg = sg_next(psg); |
| 1317 | paddr = kmap_atomic(sg_page(psg)) + psg->offset; |
| 1318 | offset = 0; |
| 1319 | } |
| 1320 | |
| 1321 | sdt = paddr + offset; |
| 1322 | |
| 1323 | pr_debug("DIF WRITE sector: %llu guard_tag: 0x%04x" |
| 1324 | " app_tag: 0x%04x ref_tag: %u\n", |
| 1325 | (unsigned long long)sector, sdt->guard_tag, |
| 1326 | sdt->app_tag, be32_to_cpu(sdt->ref_tag)); |
| 1327 | |
Nicholas Bellinger | 823ddd8 | 2015-02-27 22:42:11 -0800 | [diff] [blame] | 1328 | rc = sbc_dif_v1_verify(cmd, sdt, daddr + j, sector, |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1329 | ei_lba); |
| 1330 | if (rc) { |
| 1331 | kunmap_atomic(paddr); |
| 1332 | kunmap_atomic(daddr); |
Sagi Grimberg | 76736db | 2014-01-23 19:29:38 +0200 | [diff] [blame] | 1333 | cmd->bad_sector = sector; |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1334 | return rc; |
| 1335 | } |
| 1336 | |
| 1337 | sector++; |
| 1338 | ei_lba++; |
| 1339 | offset += sizeof(struct se_dif_v1_tuple); |
| 1340 | } |
| 1341 | |
| 1342 | kunmap_atomic(paddr); |
| 1343 | kunmap_atomic(daddr); |
| 1344 | } |
| 1345 | sbc_dif_copy_prot(cmd, sectors, false, sg, sg_off); |
| 1346 | |
| 1347 | return 0; |
| 1348 | } |
| 1349 | EXPORT_SYMBOL(sbc_dif_verify_write); |
| 1350 | |
Nicholas Bellinger | 395ccb2 | 2014-04-02 14:34:11 -0700 | [diff] [blame] | 1351 | static sense_reason_t |
| 1352 | __sbc_dif_verify_read(struct se_cmd *cmd, sector_t start, unsigned int sectors, |
| 1353 | unsigned int ei_lba, struct scatterlist *sg, int sg_off) |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1354 | { |
| 1355 | struct se_device *dev = cmd->se_dev; |
| 1356 | struct se_dif_v1_tuple *sdt; |
Sagi Grimberg | fc272ec | 2014-02-23 14:20:08 +0200 | [diff] [blame] | 1357 | struct scatterlist *dsg, *psg = sg; |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1358 | sector_t sector = start; |
| 1359 | void *daddr, *paddr; |
| 1360 | int i, j, offset = sg_off; |
| 1361 | sense_reason_t rc; |
| 1362 | |
| 1363 | for_each_sg(cmd->t_data_sg, dsg, cmd->t_data_nents, i) { |
| 1364 | daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; |
Sagi Grimberg | fc272ec | 2014-02-23 14:20:08 +0200 | [diff] [blame] | 1365 | paddr = kmap_atomic(sg_page(psg)) + sg->offset; |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1366 | |
| 1367 | for (j = 0; j < dsg->length; j += dev->dev_attrib.block_size) { |
| 1368 | |
Sagi Grimberg | fc272ec | 2014-02-23 14:20:08 +0200 | [diff] [blame] | 1369 | if (offset >= psg->length) { |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1370 | kunmap_atomic(paddr); |
Sagi Grimberg | fc272ec | 2014-02-23 14:20:08 +0200 | [diff] [blame] | 1371 | psg = sg_next(psg); |
| 1372 | paddr = kmap_atomic(sg_page(psg)) + psg->offset; |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1373 | offset = 0; |
| 1374 | } |
| 1375 | |
| 1376 | sdt = paddr + offset; |
| 1377 | |
| 1378 | pr_debug("DIF READ sector: %llu guard_tag: 0x%04x" |
| 1379 | " app_tag: 0x%04x ref_tag: %u\n", |
| 1380 | (unsigned long long)sector, sdt->guard_tag, |
| 1381 | sdt->app_tag, be32_to_cpu(sdt->ref_tag)); |
| 1382 | |
| 1383 | if (sdt->app_tag == cpu_to_be16(0xffff)) { |
| 1384 | sector++; |
| 1385 | offset += sizeof(struct se_dif_v1_tuple); |
| 1386 | continue; |
| 1387 | } |
| 1388 | |
Nicholas Bellinger | 823ddd8 | 2015-02-27 22:42:11 -0800 | [diff] [blame] | 1389 | rc = sbc_dif_v1_verify(cmd, sdt, daddr + j, sector, |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1390 | ei_lba); |
| 1391 | if (rc) { |
| 1392 | kunmap_atomic(paddr); |
| 1393 | kunmap_atomic(daddr); |
Sagi Grimberg | 76736db | 2014-01-23 19:29:38 +0200 | [diff] [blame] | 1394 | cmd->bad_sector = sector; |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1395 | return rc; |
| 1396 | } |
| 1397 | |
| 1398 | sector++; |
| 1399 | ei_lba++; |
| 1400 | offset += sizeof(struct se_dif_v1_tuple); |
| 1401 | } |
| 1402 | |
| 1403 | kunmap_atomic(paddr); |
| 1404 | kunmap_atomic(daddr); |
| 1405 | } |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1406 | |
| 1407 | return 0; |
| 1408 | } |
Nicholas Bellinger | 395ccb2 | 2014-04-02 14:34:11 -0700 | [diff] [blame] | 1409 | |
| 1410 | sense_reason_t |
| 1411 | sbc_dif_read_strip(struct se_cmd *cmd) |
| 1412 | { |
| 1413 | struct se_device *dev = cmd->se_dev; |
| 1414 | u32 sectors = cmd->prot_length / dev->prot_length; |
| 1415 | |
| 1416 | return __sbc_dif_verify_read(cmd, cmd->t_task_lba, sectors, 0, |
| 1417 | cmd->t_prot_sg, 0); |
| 1418 | } |
| 1419 | |
| 1420 | sense_reason_t |
| 1421 | sbc_dif_verify_read(struct se_cmd *cmd, sector_t start, unsigned int sectors, |
| 1422 | unsigned int ei_lba, struct scatterlist *sg, int sg_off) |
| 1423 | { |
| 1424 | sense_reason_t rc; |
| 1425 | |
| 1426 | rc = __sbc_dif_verify_read(cmd, start, sectors, ei_lba, sg, sg_off); |
| 1427 | if (rc) |
| 1428 | return rc; |
| 1429 | |
| 1430 | sbc_dif_copy_prot(cmd, sectors, true, sg, sg_off); |
| 1431 | return 0; |
| 1432 | } |
Nicholas Bellinger | 41861fa | 2014-01-07 22:44:57 +0000 | [diff] [blame] | 1433 | EXPORT_SYMBOL(sbc_dif_verify_read); |