Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1 | /* |
| 2 | * SCSI Primary Commands (SPC) 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 | 88455ec | 2012-05-20 11:59:13 -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 <asm/unaligned.h> |
| 26 | |
| 27 | #include <scsi/scsi.h> |
| 28 | #include <scsi/scsi_tcq.h> |
| 29 | |
| 30 | #include <target/target_core_base.h> |
| 31 | #include <target/target_core_backend.h> |
| 32 | #include <target/target_core_fabric.h> |
| 33 | |
| 34 | #include "target_core_internal.h" |
Nicholas Bellinger | eba2ca4 | 2012-05-30 14:09:10 -0700 | [diff] [blame] | 35 | #include "target_core_alua.h" |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 36 | #include "target_core_pr.h" |
| 37 | #include "target_core_ua.h" |
Nicholas Bellinger | 04b1b79 | 2013-08-22 12:29:59 -0700 | [diff] [blame] | 38 | #include "target_core_xcopy.h" |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 39 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 40 | static void spc_fill_alua_data(struct se_port *port, unsigned char *buf) |
| 41 | { |
| 42 | struct t10_alua_tg_pt_gp *tg_pt_gp; |
| 43 | struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; |
| 44 | |
| 45 | /* |
| 46 | * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS. |
| 47 | */ |
| 48 | buf[5] = 0x80; |
| 49 | |
| 50 | /* |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 51 | * Set TPGS field for explicit and/or implicit ALUA access type |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 52 | * and opteration. |
| 53 | * |
| 54 | * See spc4r17 section 6.4.2 Table 135 |
| 55 | */ |
| 56 | if (!port) |
| 57 | return; |
| 58 | tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; |
| 59 | if (!tg_pt_gp_mem) |
| 60 | return; |
| 61 | |
| 62 | spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); |
| 63 | tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; |
| 64 | if (tg_pt_gp) |
| 65 | buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type; |
| 66 | spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); |
| 67 | } |
| 68 | |
Hannes Reinecke | 0dfa1c5 | 2012-12-17 09:53:35 +0100 | [diff] [blame] | 69 | sense_reason_t |
| 70 | spc_emulate_inquiry_std(struct se_cmd *cmd, unsigned char *buf) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 71 | { |
| 72 | struct se_lun *lun = cmd->se_lun; |
| 73 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | d2c5304 | 2014-04-02 13:27:43 -0700 | [diff] [blame] | 74 | struct se_session *sess = cmd->se_sess; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 75 | |
| 76 | /* Set RMB (removable media) for tape devices */ |
| 77 | if (dev->transport->get_device_type(dev) == TYPE_TAPE) |
| 78 | buf[1] = 0x80; |
| 79 | |
Christoph Hellwig | 48c2567 | 2012-10-10 17:37:17 -0400 | [diff] [blame] | 80 | buf[2] = 0x05; /* SPC-3 */ |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2 |
| 84 | * |
| 85 | * SPC4 says: |
| 86 | * A RESPONSE DATA FORMAT field set to 2h indicates that the |
| 87 | * standard INQUIRY data is in the format defined in this |
| 88 | * standard. Response data format values less than 2h are |
| 89 | * obsolete. Response data format values greater than 2h are |
| 90 | * reserved. |
| 91 | */ |
| 92 | buf[3] = 2; |
| 93 | |
| 94 | /* |
| 95 | * Enable SCCS and TPGS fields for Emulated ALUA |
| 96 | */ |
Christoph Hellwig | c87fbd5 | 2012-10-10 17:37:16 -0400 | [diff] [blame] | 97 | spc_fill_alua_data(lun->lun_sep, buf); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 98 | |
Nicholas Bellinger | d397a44 | 2013-08-22 14:17:20 -0700 | [diff] [blame] | 99 | /* |
| 100 | * Set Third-Party Copy (3PC) bit to indicate support for EXTENDED_COPY |
| 101 | */ |
| 102 | if (dev->dev_attrib.emulate_3pc) |
| 103 | buf[5] |= 0x8; |
Nicholas Bellinger | bdbad2b | 2013-12-23 20:32:46 +0000 | [diff] [blame] | 104 | /* |
Nicholas Bellinger | d2c5304 | 2014-04-02 13:27:43 -0700 | [diff] [blame] | 105 | * Set Protection (PROTECT) bit when DIF has been enabled on the |
Nicholas Bellinger | 9ef5466 | 2015-02-27 22:05:33 -0800 | [diff] [blame^] | 106 | * device, and the fabric supports VERIFY + PASS. Also report |
| 107 | * PROTECT=1 if sess_prot_type has been configured to allow T10-PI |
| 108 | * to unprotected devices. |
Nicholas Bellinger | bdbad2b | 2013-12-23 20:32:46 +0000 | [diff] [blame] | 109 | */ |
Nicholas Bellinger | d2c5304 | 2014-04-02 13:27:43 -0700 | [diff] [blame] | 110 | 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^] | 111 | if (dev->dev_attrib.pi_prot_type || cmd->se_sess->sess_prot_type) |
Nicholas Bellinger | d2c5304 | 2014-04-02 13:27:43 -0700 | [diff] [blame] | 112 | buf[5] |= 0x1; |
| 113 | } |
Nicholas Bellinger | d397a44 | 2013-08-22 14:17:20 -0700 | [diff] [blame] | 114 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 115 | buf[7] = 0x2; /* CmdQue=1 */ |
| 116 | |
Nicholas Bellinger | ee60bdd | 2013-07-24 16:15:08 -0700 | [diff] [blame] | 117 | memcpy(&buf[8], "LIO-ORG ", 8); |
| 118 | memset(&buf[16], 0x20, 16); |
| 119 | memcpy(&buf[16], dev->t10_wwn.model, |
| 120 | min_t(size_t, strlen(dev->t10_wwn.model), 16)); |
| 121 | memcpy(&buf[32], dev->t10_wwn.revision, |
| 122 | min_t(size_t, strlen(dev->t10_wwn.revision), 4)); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 123 | buf[4] = 31; /* Set additional length to 31 */ |
| 124 | |
| 125 | return 0; |
| 126 | } |
Hannes Reinecke | 0dfa1c5 | 2012-12-17 09:53:35 +0100 | [diff] [blame] | 127 | EXPORT_SYMBOL(spc_emulate_inquiry_std); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 128 | |
| 129 | /* unit serial number */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 130 | static sense_reason_t |
| 131 | spc_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 132 | { |
| 133 | struct se_device *dev = cmd->se_dev; |
Christophe Vu-Brugier | 9aff64e | 2014-06-06 17:15:15 +0200 | [diff] [blame] | 134 | u16 len; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 135 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 136 | if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) { |
Christophe Vu-Brugier | 9aff64e | 2014-06-06 17:15:15 +0200 | [diff] [blame] | 137 | len = sprintf(&buf[4], "%s", dev->t10_wwn.unit_serial); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 138 | len++; /* Extra Byte for NULL Terminator */ |
| 139 | buf[3] = len; |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
Nicholas Bellinger | 6836602 | 2013-08-20 17:02:21 -0700 | [diff] [blame] | 144 | void spc_parse_naa_6h_vendor_specific(struct se_device *dev, |
| 145 | unsigned char *buf) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 146 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 147 | unsigned char *p = &dev->t10_wwn.unit_serial[0]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 148 | int cnt; |
| 149 | bool next = true; |
| 150 | |
| 151 | /* |
| 152 | * Generate up to 36 bits of VENDOR SPECIFIC IDENTIFIER starting on |
| 153 | * byte 3 bit 3-0 for NAA IEEE Registered Extended DESIGNATOR field |
| 154 | * format, followed by 64 bits of VENDOR SPECIFIC IDENTIFIER EXTENSION |
| 155 | * to complete the payload. These are based from VPD=0x80 PRODUCT SERIAL |
| 156 | * NUMBER set via vpd_unit_serial in target_core_configfs.c to ensure |
| 157 | * per device uniqeness. |
| 158 | */ |
| 159 | for (cnt = 0; *p && cnt < 13; p++) { |
| 160 | int val = hex_to_bin(*p); |
| 161 | |
| 162 | if (val < 0) |
| 163 | continue; |
| 164 | |
| 165 | if (next) { |
| 166 | next = false; |
| 167 | buf[cnt++] |= val; |
| 168 | } else { |
| 169 | next = true; |
| 170 | buf[cnt] = val << 4; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Device identification VPD, for a complete list of |
| 177 | * DESIGNATOR TYPEs see spc4r17 Table 459. |
| 178 | */ |
Hannes Reinecke | 0dfa1c5 | 2012-12-17 09:53:35 +0100 | [diff] [blame] | 179 | sense_reason_t |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 180 | spc_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 181 | { |
| 182 | struct se_device *dev = cmd->se_dev; |
| 183 | struct se_lun *lun = cmd->se_lun; |
| 184 | struct se_port *port = NULL; |
| 185 | struct se_portal_group *tpg = NULL; |
| 186 | struct t10_alua_lu_gp_member *lu_gp_mem; |
| 187 | struct t10_alua_tg_pt_gp *tg_pt_gp; |
| 188 | struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 189 | unsigned char *prod = &dev->t10_wwn.model[0]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 190 | u32 prod_len; |
| 191 | u32 unit_serial_len, off = 0; |
| 192 | u16 len = 0, id_len; |
| 193 | |
| 194 | off = 4; |
| 195 | |
| 196 | /* |
| 197 | * NAA IEEE Registered Extended Assigned designator format, see |
| 198 | * spc4r17 section 7.7.3.6.5 |
| 199 | * |
| 200 | * We depend upon a target_core_mod/ConfigFS provided |
| 201 | * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial |
| 202 | * value in order to return the NAA id. |
| 203 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 204 | if (!(dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL)) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 205 | goto check_t10_vend_desc; |
| 206 | |
| 207 | /* CODE SET == Binary */ |
| 208 | buf[off++] = 0x1; |
| 209 | |
| 210 | /* Set ASSOCIATION == addressed logical unit: 0)b */ |
| 211 | buf[off] = 0x00; |
| 212 | |
| 213 | /* Identifier/Designator type == NAA identifier */ |
| 214 | buf[off++] |= 0x3; |
| 215 | off++; |
| 216 | |
| 217 | /* Identifier/Designator length */ |
| 218 | buf[off++] = 0x10; |
| 219 | |
| 220 | /* |
| 221 | * Start NAA IEEE Registered Extended Identifier/Designator |
| 222 | */ |
| 223 | buf[off++] = (0x6 << 4); |
| 224 | |
| 225 | /* |
| 226 | * Use OpenFabrics IEEE Company ID: 00 14 05 |
| 227 | */ |
| 228 | buf[off++] = 0x01; |
| 229 | buf[off++] = 0x40; |
| 230 | buf[off] = (0x5 << 4); |
| 231 | |
| 232 | /* |
| 233 | * Return ConfigFS Unit Serial Number information for |
| 234 | * VENDOR_SPECIFIC_IDENTIFIER and |
| 235 | * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION |
| 236 | */ |
| 237 | spc_parse_naa_6h_vendor_specific(dev, &buf[off]); |
| 238 | |
| 239 | len = 20; |
| 240 | off = (len + 4); |
| 241 | |
| 242 | check_t10_vend_desc: |
| 243 | /* |
| 244 | * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4 |
| 245 | */ |
| 246 | id_len = 8; /* For Vendor field */ |
| 247 | prod_len = 4; /* For VPD Header */ |
| 248 | prod_len += 8; /* For Vendor field */ |
| 249 | prod_len += strlen(prod); |
| 250 | prod_len++; /* For : */ |
| 251 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 252 | if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) { |
| 253 | unit_serial_len = strlen(&dev->t10_wwn.unit_serial[0]); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 254 | unit_serial_len++; /* For NULL Terminator */ |
| 255 | |
| 256 | id_len += sprintf(&buf[off+12], "%s:%s", prod, |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 257 | &dev->t10_wwn.unit_serial[0]); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 258 | } |
| 259 | buf[off] = 0x2; /* ASCII */ |
| 260 | buf[off+1] = 0x1; /* T10 Vendor ID */ |
| 261 | buf[off+2] = 0x0; |
| 262 | memcpy(&buf[off+4], "LIO-ORG", 8); |
| 263 | /* Extra Byte for NULL Terminator */ |
| 264 | id_len++; |
| 265 | /* Identifier Length */ |
| 266 | buf[off+3] = id_len; |
| 267 | /* Header size for Designation descriptor */ |
| 268 | len += (id_len + 4); |
| 269 | off += (id_len + 4); |
| 270 | /* |
| 271 | * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD |
| 272 | */ |
| 273 | port = lun->lun_sep; |
| 274 | if (port) { |
| 275 | struct t10_alua_lu_gp *lu_gp; |
Hannes Reinecke | fbfe858 | 2013-12-17 09:18:48 +0100 | [diff] [blame] | 276 | u32 padding, scsi_name_len, scsi_target_len; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 277 | u16 lu_gp_id = 0; |
| 278 | u16 tg_pt_gp_id = 0; |
| 279 | u16 tpgt; |
| 280 | |
| 281 | tpg = port->sep_tpg; |
| 282 | /* |
| 283 | * Relative target port identifer, see spc4r17 |
| 284 | * section 7.7.3.7 |
| 285 | * |
| 286 | * Get the PROTOCOL IDENTIFIER as defined by spc4r17 |
| 287 | * section 7.5.1 Table 362 |
| 288 | */ |
| 289 | buf[off] = |
| 290 | (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4); |
| 291 | buf[off++] |= 0x1; /* CODE SET == Binary */ |
| 292 | buf[off] = 0x80; /* Set PIV=1 */ |
| 293 | /* Set ASSOCIATION == target port: 01b */ |
| 294 | buf[off] |= 0x10; |
| 295 | /* DESIGNATOR TYPE == Relative target port identifer */ |
| 296 | buf[off++] |= 0x4; |
| 297 | off++; /* Skip over Reserved */ |
| 298 | buf[off++] = 4; /* DESIGNATOR LENGTH */ |
| 299 | /* Skip over Obsolete field in RTPI payload |
| 300 | * in Table 472 */ |
| 301 | off += 2; |
| 302 | buf[off++] = ((port->sep_rtpi >> 8) & 0xff); |
| 303 | buf[off++] = (port->sep_rtpi & 0xff); |
| 304 | len += 8; /* Header size + Designation descriptor */ |
| 305 | /* |
| 306 | * Target port group identifier, see spc4r17 |
| 307 | * section 7.7.3.8 |
| 308 | * |
| 309 | * Get the PROTOCOL IDENTIFIER as defined by spc4r17 |
| 310 | * section 7.5.1 Table 362 |
| 311 | */ |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 312 | tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; |
| 313 | if (!tg_pt_gp_mem) |
| 314 | goto check_lu_gp; |
| 315 | |
| 316 | spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); |
| 317 | tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; |
| 318 | if (!tg_pt_gp) { |
| 319 | spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); |
| 320 | goto check_lu_gp; |
| 321 | } |
| 322 | tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id; |
| 323 | spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); |
| 324 | |
| 325 | buf[off] = |
| 326 | (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4); |
| 327 | buf[off++] |= 0x1; /* CODE SET == Binary */ |
| 328 | buf[off] = 0x80; /* Set PIV=1 */ |
| 329 | /* Set ASSOCIATION == target port: 01b */ |
| 330 | buf[off] |= 0x10; |
| 331 | /* DESIGNATOR TYPE == Target port group identifier */ |
| 332 | buf[off++] |= 0x5; |
| 333 | off++; /* Skip over Reserved */ |
| 334 | buf[off++] = 4; /* DESIGNATOR LENGTH */ |
| 335 | off += 2; /* Skip over Reserved Field */ |
| 336 | buf[off++] = ((tg_pt_gp_id >> 8) & 0xff); |
| 337 | buf[off++] = (tg_pt_gp_id & 0xff); |
| 338 | len += 8; /* Header size + Designation descriptor */ |
| 339 | /* |
| 340 | * Logical Unit Group identifier, see spc4r17 |
| 341 | * section 7.7.3.8 |
| 342 | */ |
| 343 | check_lu_gp: |
| 344 | lu_gp_mem = dev->dev_alua_lu_gp_mem; |
| 345 | if (!lu_gp_mem) |
| 346 | goto check_scsi_name; |
| 347 | |
| 348 | spin_lock(&lu_gp_mem->lu_gp_mem_lock); |
| 349 | lu_gp = lu_gp_mem->lu_gp; |
| 350 | if (!lu_gp) { |
| 351 | spin_unlock(&lu_gp_mem->lu_gp_mem_lock); |
| 352 | goto check_scsi_name; |
| 353 | } |
| 354 | lu_gp_id = lu_gp->lu_gp_id; |
| 355 | spin_unlock(&lu_gp_mem->lu_gp_mem_lock); |
| 356 | |
| 357 | buf[off++] |= 0x1; /* CODE SET == Binary */ |
| 358 | /* DESIGNATOR TYPE == Logical Unit Group identifier */ |
| 359 | buf[off++] |= 0x6; |
| 360 | off++; /* Skip over Reserved */ |
| 361 | buf[off++] = 4; /* DESIGNATOR LENGTH */ |
| 362 | off += 2; /* Skip over Reserved Field */ |
| 363 | buf[off++] = ((lu_gp_id >> 8) & 0xff); |
| 364 | buf[off++] = (lu_gp_id & 0xff); |
| 365 | len += 8; /* Header size + Designation descriptor */ |
| 366 | /* |
| 367 | * SCSI name string designator, see spc4r17 |
| 368 | * section 7.7.3.11 |
| 369 | * |
| 370 | * Get the PROTOCOL IDENTIFIER as defined by spc4r17 |
| 371 | * section 7.5.1 Table 362 |
| 372 | */ |
| 373 | check_scsi_name: |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 374 | buf[off] = |
| 375 | (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4); |
| 376 | buf[off++] |= 0x3; /* CODE SET == UTF-8 */ |
| 377 | buf[off] = 0x80; /* Set PIV=1 */ |
| 378 | /* Set ASSOCIATION == target port: 01b */ |
| 379 | buf[off] |= 0x10; |
| 380 | /* DESIGNATOR TYPE == SCSI name string */ |
| 381 | buf[off++] |= 0x8; |
| 382 | off += 2; /* Skip over Reserved and length */ |
| 383 | /* |
| 384 | * SCSI name string identifer containing, $FABRIC_MOD |
| 385 | * dependent information. For LIO-Target and iSCSI |
| 386 | * Target Port, this means "<iSCSI name>,t,0x<TPGT> in |
| 387 | * UTF-8 encoding. |
| 388 | */ |
| 389 | tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg); |
| 390 | scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x", |
| 391 | tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt); |
| 392 | scsi_name_len += 1 /* Include NULL terminator */; |
| 393 | /* |
| 394 | * The null-terminated, null-padded (see 4.4.2) SCSI |
| 395 | * NAME STRING field contains a UTF-8 format string. |
| 396 | * The number of bytes in the SCSI NAME STRING field |
| 397 | * (i.e., the value in the DESIGNATOR LENGTH field) |
| 398 | * shall be no larger than 256 and shall be a multiple |
| 399 | * of four. |
| 400 | */ |
Hannes Reinecke | 03ba84c | 2013-12-17 09:18:47 +0100 | [diff] [blame] | 401 | padding = ((-scsi_name_len) & 3); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 402 | if (padding) |
| 403 | scsi_name_len += padding; |
Hannes Reinecke | 03ba84c | 2013-12-17 09:18:47 +0100 | [diff] [blame] | 404 | if (scsi_name_len > 256) |
| 405 | scsi_name_len = 256; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 406 | |
| 407 | buf[off-1] = scsi_name_len; |
| 408 | off += scsi_name_len; |
| 409 | /* Header size + Designation descriptor */ |
| 410 | len += (scsi_name_len + 4); |
Hannes Reinecke | fbfe858 | 2013-12-17 09:18:48 +0100 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * Target device designator |
| 414 | */ |
| 415 | buf[off] = |
| 416 | (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4); |
| 417 | buf[off++] |= 0x3; /* CODE SET == UTF-8 */ |
| 418 | buf[off] = 0x80; /* Set PIV=1 */ |
| 419 | /* Set ASSOCIATION == target device: 10b */ |
| 420 | buf[off] |= 0x20; |
| 421 | /* DESIGNATOR TYPE == SCSI name string */ |
| 422 | buf[off++] |= 0x8; |
| 423 | off += 2; /* Skip over Reserved and length */ |
| 424 | /* |
| 425 | * SCSI name string identifer containing, $FABRIC_MOD |
| 426 | * dependent information. For LIO-Target and iSCSI |
| 427 | * Target Port, this means "<iSCSI name>" in |
| 428 | * UTF-8 encoding. |
| 429 | */ |
| 430 | scsi_target_len = sprintf(&buf[off], "%s", |
| 431 | tpg->se_tpg_tfo->tpg_get_wwn(tpg)); |
| 432 | scsi_target_len += 1 /* Include NULL terminator */; |
| 433 | /* |
| 434 | * The null-terminated, null-padded (see 4.4.2) SCSI |
| 435 | * NAME STRING field contains a UTF-8 format string. |
| 436 | * The number of bytes in the SCSI NAME STRING field |
| 437 | * (i.e., the value in the DESIGNATOR LENGTH field) |
| 438 | * shall be no larger than 256 and shall be a multiple |
| 439 | * of four. |
| 440 | */ |
| 441 | padding = ((-scsi_target_len) & 3); |
| 442 | if (padding) |
| 443 | scsi_target_len += padding; |
Roland Dreier | 6a16d7b | 2014-02-03 00:35:03 -0800 | [diff] [blame] | 444 | if (scsi_target_len > 256) |
| 445 | scsi_target_len = 256; |
Hannes Reinecke | fbfe858 | 2013-12-17 09:18:48 +0100 | [diff] [blame] | 446 | |
| 447 | buf[off-1] = scsi_target_len; |
| 448 | off += scsi_target_len; |
| 449 | |
| 450 | /* Header size + Designation descriptor */ |
| 451 | len += (scsi_target_len + 4); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 452 | } |
| 453 | buf[2] = ((len >> 8) & 0xff); |
| 454 | buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */ |
| 455 | return 0; |
| 456 | } |
Hannes Reinecke | 0dfa1c5 | 2012-12-17 09:53:35 +0100 | [diff] [blame] | 457 | EXPORT_SYMBOL(spc_emulate_evpd_83); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 458 | |
Nicholas Bellinger | d0c8b25 | 2013-01-29 22:10:06 -0800 | [diff] [blame] | 459 | static bool |
| 460 | spc_check_dev_wce(struct se_device *dev) |
| 461 | { |
| 462 | bool wce = false; |
| 463 | |
| 464 | if (dev->transport->get_write_cache) |
| 465 | wce = dev->transport->get_write_cache(dev); |
| 466 | else if (dev->dev_attrib.emulate_write_cache > 0) |
| 467 | wce = true; |
| 468 | |
| 469 | return wce; |
| 470 | } |
| 471 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 472 | /* Extended INQUIRY Data VPD Page */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 473 | static sense_reason_t |
| 474 | spc_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 475 | { |
Nicholas Bellinger | d0c8b25 | 2013-01-29 22:10:06 -0800 | [diff] [blame] | 476 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | d2c5304 | 2014-04-02 13:27:43 -0700 | [diff] [blame] | 477 | struct se_session *sess = cmd->se_sess; |
Nicholas Bellinger | d0c8b25 | 2013-01-29 22:10:06 -0800 | [diff] [blame] | 478 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 479 | buf[3] = 0x3c; |
Nicholas Bellinger | 43bb95c | 2013-12-23 20:33:21 +0000 | [diff] [blame] | 480 | /* |
| 481 | * Set GRD_CHK + REF_CHK for TYPE1 protection, or GRD_CHK |
| 482 | * only for TYPE3 protection. |
| 483 | */ |
Nicholas Bellinger | d2c5304 | 2014-04-02 13:27:43 -0700 | [diff] [blame] | 484 | 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^] | 485 | if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE1_PROT || |
| 486 | cmd->se_sess->sess_prot_type == TARGET_DIF_TYPE1_PROT) |
Nicholas Bellinger | d2c5304 | 2014-04-02 13:27:43 -0700 | [diff] [blame] | 487 | buf[4] = 0x5; |
Nicholas Bellinger | 9ef5466 | 2015-02-27 22:05:33 -0800 | [diff] [blame^] | 488 | else if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE3_PROT || |
| 489 | cmd->se_sess->sess_prot_type == TARGET_DIF_TYPE3_PROT) |
Nicholas Bellinger | d2c5304 | 2014-04-02 13:27:43 -0700 | [diff] [blame] | 490 | buf[4] = 0x4; |
| 491 | } |
Nicholas Bellinger | 43bb95c | 2013-12-23 20:33:21 +0000 | [diff] [blame] | 492 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 493 | /* Set HEADSUP, ORDSUP, SIMPSUP */ |
| 494 | buf[5] = 0x07; |
| 495 | |
| 496 | /* If WriteCache emulation is enabled, set V_SUP */ |
Nicholas Bellinger | d0c8b25 | 2013-01-29 22:10:06 -0800 | [diff] [blame] | 497 | if (spc_check_dev_wce(dev)) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 498 | buf[6] = 0x01; |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 499 | /* If an LBA map is present set R_SUP */ |
| 500 | spin_lock(&cmd->se_dev->t10_alua.lba_map_lock); |
| 501 | if (!list_empty(&dev->t10_alua.lba_map_list)) |
| 502 | buf[8] = 0x10; |
| 503 | spin_unlock(&cmd->se_dev->t10_alua.lba_map_lock); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | /* Block Limits VPD page */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 508 | static sense_reason_t |
| 509 | spc_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 510 | { |
| 511 | struct se_device *dev = cmd->se_dev; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 512 | int have_tp = 0; |
Andy Grover | 7f7caf6 | 2013-11-11 08:59:17 -0800 | [diff] [blame] | 513 | int opt, min; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 514 | |
| 515 | /* |
| 516 | * Following spc3r22 section 6.5.3 Block Limits VPD page, when |
| 517 | * emulate_tpu=1 or emulate_tpws=1 we will be expect a |
| 518 | * different page length for Thin Provisioning. |
| 519 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 520 | if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 521 | have_tp = 1; |
| 522 | |
| 523 | buf[0] = dev->transport->get_device_type(dev); |
| 524 | buf[3] = have_tp ? 0x3c : 0x10; |
| 525 | |
| 526 | /* Set WSNZ to 1 */ |
| 527 | buf[4] = 0x01; |
Nicholas Bellinger | 0123a9e | 2013-08-20 14:24:09 -0700 | [diff] [blame] | 528 | /* |
| 529 | * Set MAXIMUM COMPARE AND WRITE LENGTH |
| 530 | */ |
| 531 | if (dev->dev_attrib.emulate_caw) |
| 532 | buf[5] = 0x01; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 533 | |
| 534 | /* |
| 535 | * Set OPTIMAL TRANSFER LENGTH GRANULARITY |
| 536 | */ |
Andy Grover | 7f7caf6 | 2013-11-11 08:59:17 -0800 | [diff] [blame] | 537 | if (dev->transport->get_io_min && (min = dev->transport->get_io_min(dev))) |
| 538 | put_unaligned_be16(min / dev->dev_attrib.block_size, &buf[6]); |
| 539 | else |
| 540 | put_unaligned_be16(1, &buf[6]); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 541 | |
| 542 | /* |
| 543 | * Set MAXIMUM TRANSFER LENGTH |
| 544 | */ |
Nicholas Bellinger | 046ba64 | 2015-01-06 16:10:37 -0800 | [diff] [blame] | 545 | put_unaligned_be32(dev->dev_attrib.hw_max_sectors, &buf[8]); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 546 | |
| 547 | /* |
| 548 | * Set OPTIMAL TRANSFER LENGTH |
| 549 | */ |
Andy Grover | 7f7caf6 | 2013-11-11 08:59:17 -0800 | [diff] [blame] | 550 | if (dev->transport->get_io_opt && (opt = dev->transport->get_io_opt(dev))) |
| 551 | put_unaligned_be32(opt / dev->dev_attrib.block_size, &buf[12]); |
| 552 | else |
| 553 | put_unaligned_be32(dev->dev_attrib.optimal_sectors, &buf[12]); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 554 | |
| 555 | /* |
| 556 | * Exit now if we don't support TP. |
| 557 | */ |
| 558 | if (!have_tp) |
Nicholas Bellinger | 773cbaf | 2012-11-15 11:02:49 -0800 | [diff] [blame] | 559 | goto max_write_same; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 560 | |
| 561 | /* |
| 562 | * Set MAXIMUM UNMAP LBA COUNT |
| 563 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 564 | put_unaligned_be32(dev->dev_attrib.max_unmap_lba_count, &buf[20]); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 565 | |
| 566 | /* |
| 567 | * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT |
| 568 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 569 | put_unaligned_be32(dev->dev_attrib.max_unmap_block_desc_count, |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 570 | &buf[24]); |
| 571 | |
| 572 | /* |
| 573 | * Set OPTIMAL UNMAP GRANULARITY |
| 574 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 575 | put_unaligned_be32(dev->dev_attrib.unmap_granularity, &buf[28]); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 576 | |
| 577 | /* |
| 578 | * UNMAP GRANULARITY ALIGNMENT |
| 579 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 580 | put_unaligned_be32(dev->dev_attrib.unmap_granularity_alignment, |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 581 | &buf[32]); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 582 | if (dev->dev_attrib.unmap_granularity_alignment != 0) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 583 | buf[32] |= 0x80; /* Set the UGAVALID bit */ |
| 584 | |
Nicholas Bellinger | 773cbaf | 2012-11-15 11:02:49 -0800 | [diff] [blame] | 585 | /* |
| 586 | * MAXIMUM WRITE SAME LENGTH |
| 587 | */ |
| 588 | max_write_same: |
| 589 | put_unaligned_be64(dev->dev_attrib.max_write_same_len, &buf[36]); |
| 590 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | /* Block Device Characteristics VPD page */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 595 | static sense_reason_t |
| 596 | spc_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 597 | { |
| 598 | struct se_device *dev = cmd->se_dev; |
| 599 | |
| 600 | buf[0] = dev->transport->get_device_type(dev); |
| 601 | buf[3] = 0x3c; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 602 | buf[5] = dev->dev_attrib.is_nonrot ? 1 : 0; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | /* Thin Provisioning VPD */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 608 | static sense_reason_t |
| 609 | spc_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 610 | { |
| 611 | struct se_device *dev = cmd->se_dev; |
| 612 | |
| 613 | /* |
| 614 | * From spc3r22 section 6.5.4 Thin Provisioning VPD page: |
| 615 | * |
| 616 | * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to |
| 617 | * zero, then the page length shall be set to 0004h. If the DP bit |
| 618 | * is set to one, then the page length shall be set to the value |
| 619 | * defined in table 162. |
| 620 | */ |
| 621 | buf[0] = dev->transport->get_device_type(dev); |
| 622 | |
| 623 | /* |
| 624 | * Set Hardcoded length mentioned above for DP=0 |
| 625 | */ |
| 626 | put_unaligned_be16(0x0004, &buf[2]); |
| 627 | |
| 628 | /* |
| 629 | * The THRESHOLD EXPONENT field indicates the threshold set size in |
| 630 | * LBAs as a power of 2 (i.e., the threshold set size is equal to |
| 631 | * 2(threshold exponent)). |
| 632 | * |
| 633 | * Note that this is currently set to 0x00 as mkp says it will be |
| 634 | * changing again. We can enable this once it has settled in T10 |
| 635 | * and is actually used by Linux/SCSI ML code. |
| 636 | */ |
| 637 | buf[4] = 0x00; |
| 638 | |
| 639 | /* |
| 640 | * A TPU bit set to one indicates that the device server supports |
| 641 | * the UNMAP command (see 5.25). A TPU bit set to zero indicates |
| 642 | * that the device server does not support the UNMAP command. |
| 643 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 644 | if (dev->dev_attrib.emulate_tpu != 0) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 645 | buf[5] = 0x80; |
| 646 | |
| 647 | /* |
| 648 | * A TPWS bit set to one indicates that the device server supports |
| 649 | * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs. |
| 650 | * A TPWS bit set to zero indicates that the device server does not |
| 651 | * support the use of the WRITE SAME (16) command to unmap LBAs. |
| 652 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 653 | if (dev->dev_attrib.emulate_tpws != 0) |
Nicholas Bellinger | aa04dae | 2015-02-14 02:05:31 +0000 | [diff] [blame] | 654 | buf[5] |= 0x40 | 0x20; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 659 | /* Referrals VPD page */ |
| 660 | static sense_reason_t |
| 661 | spc_emulate_evpd_b3(struct se_cmd *cmd, unsigned char *buf) |
| 662 | { |
| 663 | struct se_device *dev = cmd->se_dev; |
| 664 | |
| 665 | buf[0] = dev->transport->get_device_type(dev); |
| 666 | buf[3] = 0x0c; |
| 667 | put_unaligned_be32(dev->t10_alua.lba_map_segment_size, &buf[8]); |
Sebastian Herbszt | 096b49951 | 2014-08-30 00:31:11 +0200 | [diff] [blame] | 668 | put_unaligned_be32(dev->t10_alua.lba_map_segment_multiplier, &buf[12]); |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 673 | static sense_reason_t |
| 674 | spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 675 | |
| 676 | static struct { |
| 677 | uint8_t page; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 678 | sense_reason_t (*emulate)(struct se_cmd *, unsigned char *); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 679 | } evpd_handlers[] = { |
| 680 | { .page = 0x00, .emulate = spc_emulate_evpd_00 }, |
| 681 | { .page = 0x80, .emulate = spc_emulate_evpd_80 }, |
| 682 | { .page = 0x83, .emulate = spc_emulate_evpd_83 }, |
| 683 | { .page = 0x86, .emulate = spc_emulate_evpd_86 }, |
| 684 | { .page = 0xb0, .emulate = spc_emulate_evpd_b0 }, |
| 685 | { .page = 0xb1, .emulate = spc_emulate_evpd_b1 }, |
| 686 | { .page = 0xb2, .emulate = spc_emulate_evpd_b2 }, |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 687 | { .page = 0xb3, .emulate = spc_emulate_evpd_b3 }, |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 688 | }; |
| 689 | |
| 690 | /* supported vital product data pages */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 691 | static sense_reason_t |
| 692 | spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 693 | { |
| 694 | int p; |
| 695 | |
| 696 | /* |
| 697 | * Only report the INQUIRY EVPD=1 pages after a valid NAA |
| 698 | * Registered Extended LUN WWN has been set via ConfigFS |
| 699 | * during device creation/restart. |
| 700 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 701 | if (cmd->se_dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) { |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 702 | buf[3] = ARRAY_SIZE(evpd_handlers); |
| 703 | for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) |
| 704 | buf[p + 4] = evpd_handlers[p].page; |
| 705 | } |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 710 | static sense_reason_t |
| 711 | spc_emulate_inquiry(struct se_cmd *cmd) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 712 | { |
| 713 | struct se_device *dev = cmd->se_dev; |
| 714 | struct se_portal_group *tpg = cmd->se_lun->lun_sep->sep_tpg; |
Paolo Bonzini | ffe7b0e | 2012-09-07 17:30:38 +0200 | [diff] [blame] | 715 | unsigned char *rbuf; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 716 | unsigned char *cdb = cmd->t_task_cdb; |
Nicholas Bellinger | f82f320 | 2013-12-19 14:13:28 -0800 | [diff] [blame] | 717 | unsigned char *buf; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 718 | sense_reason_t ret; |
| 719 | int p; |
Roland Dreier | 2426bd4 | 2014-06-10 11:07:47 -0700 | [diff] [blame] | 720 | int len = 0; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 721 | |
Nicholas Bellinger | f82f320 | 2013-12-19 14:13:28 -0800 | [diff] [blame] | 722 | buf = kzalloc(SE_INQUIRY_BUF, GFP_KERNEL); |
| 723 | if (!buf) { |
| 724 | pr_err("Unable to allocate response buffer for INQUIRY\n"); |
| 725 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 726 | } |
Nicholas Bellinger | dea5f09 | 2012-10-31 22:04:26 -0700 | [diff] [blame] | 727 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 728 | if (dev == tpg->tpg_virt_lun0.lun_se_dev) |
| 729 | buf[0] = 0x3f; /* Not connected */ |
| 730 | else |
| 731 | buf[0] = dev->transport->get_device_type(dev); |
| 732 | |
| 733 | if (!(cdb[1] & 0x1)) { |
| 734 | if (cdb[2]) { |
| 735 | pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n", |
| 736 | cdb[2]); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 737 | ret = TCM_INVALID_CDB_FIELD; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 738 | goto out; |
| 739 | } |
| 740 | |
| 741 | ret = spc_emulate_inquiry_std(cmd, buf); |
Roland Dreier | 2426bd4 | 2014-06-10 11:07:47 -0700 | [diff] [blame] | 742 | len = buf[4] + 5; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 743 | goto out; |
| 744 | } |
| 745 | |
| 746 | for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) { |
| 747 | if (cdb[2] == evpd_handlers[p].page) { |
| 748 | buf[1] = cdb[2]; |
| 749 | ret = evpd_handlers[p].emulate(cmd, buf); |
Roland Dreier | 2426bd4 | 2014-06-10 11:07:47 -0700 | [diff] [blame] | 750 | len = get_unaligned_be16(&buf[2]) + 4; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 751 | goto out; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | pr_err("Unknown VPD Code: 0x%02x\n", cdb[2]); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 756 | ret = TCM_INVALID_CDB_FIELD; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 757 | |
| 758 | out: |
Paolo Bonzini | ffe7b0e | 2012-09-07 17:30:38 +0200 | [diff] [blame] | 759 | rbuf = transport_kmap_data_sg(cmd); |
Nicholas Bellinger | 49df9fc | 2013-01-29 13:33:05 -0800 | [diff] [blame] | 760 | if (rbuf) { |
Nicholas Bellinger | f82f320 | 2013-12-19 14:13:28 -0800 | [diff] [blame] | 761 | memcpy(rbuf, buf, min_t(u32, SE_INQUIRY_BUF, cmd->data_length)); |
Nicholas Bellinger | 49df9fc | 2013-01-29 13:33:05 -0800 | [diff] [blame] | 762 | transport_kunmap_data_sg(cmd); |
| 763 | } |
Nicholas Bellinger | f82f320 | 2013-12-19 14:13:28 -0800 | [diff] [blame] | 764 | kfree(buf); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 765 | |
| 766 | if (!ret) |
Roland Dreier | 2426bd4 | 2014-06-10 11:07:47 -0700 | [diff] [blame] | 767 | target_complete_cmd_with_length(cmd, GOOD, len); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 768 | return ret; |
| 769 | } |
| 770 | |
Nicholas Bellinger | d45aca42a | 2014-04-02 13:28:49 -0700 | [diff] [blame] | 771 | static int spc_modesense_rwrecovery(struct se_cmd *cmd, u8 pc, u8 *p) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 772 | { |
| 773 | p[0] = 0x01; |
| 774 | p[1] = 0x0a; |
| 775 | |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 776 | /* No changeable values for now */ |
| 777 | if (pc == 1) |
| 778 | goto out; |
| 779 | |
| 780 | out: |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 781 | return 12; |
| 782 | } |
| 783 | |
Nicholas Bellinger | d45aca42a | 2014-04-02 13:28:49 -0700 | [diff] [blame] | 784 | static int spc_modesense_control(struct se_cmd *cmd, u8 pc, u8 *p) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 785 | { |
Nicholas Bellinger | d45aca42a | 2014-04-02 13:28:49 -0700 | [diff] [blame] | 786 | struct se_device *dev = cmd->se_dev; |
| 787 | struct se_session *sess = cmd->se_sess; |
| 788 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 789 | p[0] = 0x0a; |
| 790 | p[1] = 0x0a; |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 791 | |
| 792 | /* No changeable values for now */ |
| 793 | if (pc == 1) |
| 794 | goto out; |
| 795 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 796 | p[2] = 2; |
| 797 | /* |
| 798 | * From spc4r23, 7.4.7 Control mode page |
| 799 | * |
| 800 | * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies |
| 801 | * restrictions on the algorithm used for reordering commands |
| 802 | * having the SIMPLE task attribute (see SAM-4). |
| 803 | * |
| 804 | * Table 368 -- QUEUE ALGORITHM MODIFIER field |
| 805 | * Code Description |
| 806 | * 0h Restricted reordering |
| 807 | * 1h Unrestricted reordering allowed |
| 808 | * 2h to 7h Reserved |
| 809 | * 8h to Fh Vendor specific |
| 810 | * |
| 811 | * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that |
| 812 | * the device server shall order the processing sequence of commands |
| 813 | * having the SIMPLE task attribute such that data integrity is maintained |
| 814 | * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol |
| 815 | * requests is halted at any time, the final value of all data observable |
| 816 | * on the medium shall be the same as if all the commands had been processed |
| 817 | * with the ORDERED task attribute). |
| 818 | * |
| 819 | * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the |
| 820 | * device server may reorder the processing sequence of commands having the |
| 821 | * SIMPLE task attribute in any manner. Any data integrity exposures related to |
| 822 | * command sequence order shall be explicitly handled by the application client |
| 823 | * through the selection of appropriate ommands and task attributes. |
| 824 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 825 | p[3] = (dev->dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 826 | /* |
| 827 | * From spc4r17, section 7.4.6 Control mode Page |
| 828 | * |
| 829 | * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b |
| 830 | * |
| 831 | * 00b: The logical unit shall clear any unit attention condition |
| 832 | * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION |
| 833 | * status and shall not establish a unit attention condition when a com- |
| 834 | * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT |
| 835 | * status. |
| 836 | * |
| 837 | * 10b: The logical unit shall not clear any unit attention condition |
| 838 | * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION |
| 839 | * status and shall not establish a unit attention condition when |
| 840 | * a command is completed with BUSY, TASK SET FULL, or RESERVATION |
| 841 | * CONFLICT status. |
| 842 | * |
| 843 | * 11b a The logical unit shall not clear any unit attention condition |
| 844 | * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION |
| 845 | * status and shall establish a unit attention condition for the |
| 846 | * initiator port associated with the I_T nexus on which the BUSY, |
| 847 | * TASK SET FULL, or RESERVATION CONFLICT status is being returned. |
| 848 | * Depending on the status, the additional sense code shall be set to |
| 849 | * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS |
| 850 | * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE |
| 851 | * command, a unit attention condition shall be established only once |
| 852 | * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless |
| 853 | * to the number of commands completed with one of those status codes. |
| 854 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 855 | p[4] = (dev->dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 : |
| 856 | (dev->dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 857 | /* |
| 858 | * From spc4r17, section 7.4.6 Control mode Page |
| 859 | * |
| 860 | * Task Aborted Status (TAS) bit set to zero. |
| 861 | * |
| 862 | * A task aborted status (TAS) bit set to zero specifies that aborted |
| 863 | * tasks shall be terminated by the device server without any response |
| 864 | * to the application client. A TAS bit set to one specifies that tasks |
| 865 | * aborted by the actions of an I_T nexus other than the I_T nexus on |
| 866 | * which the command was received shall be completed with TASK ABORTED |
| 867 | * status (see SAM-4). |
| 868 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 869 | p[5] = (dev->dev_attrib.emulate_tas) ? 0x40 : 0x00; |
Nicholas Bellinger | 0c30f42 | 2013-12-23 21:23:34 +0000 | [diff] [blame] | 870 | /* |
| 871 | * From spc4r30, section 7.5.7 Control mode page |
| 872 | * |
| 873 | * Application Tag Owner (ATO) bit set to one. |
| 874 | * |
| 875 | * If the ATO bit is set to one the device server shall not modify the |
| 876 | * LOGICAL BLOCK APPLICATION TAG field and, depending on the protection |
| 877 | * type, shall not modify the contents of the LOGICAL BLOCK REFERENCE |
| 878 | * TAG field. |
| 879 | */ |
Nicholas Bellinger | d45aca42a | 2014-04-02 13:28:49 -0700 | [diff] [blame] | 880 | 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^] | 881 | if (dev->dev_attrib.pi_prot_type || sess->sess_prot_type) |
Nicholas Bellinger | d45aca42a | 2014-04-02 13:28:49 -0700 | [diff] [blame] | 882 | p[5] |= 0x80; |
| 883 | } |
Nicholas Bellinger | 0c30f42 | 2013-12-23 21:23:34 +0000 | [diff] [blame] | 884 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 885 | p[8] = 0xff; |
| 886 | p[9] = 0xff; |
| 887 | p[11] = 30; |
| 888 | |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 889 | out: |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 890 | return 12; |
| 891 | } |
| 892 | |
Nicholas Bellinger | d45aca42a | 2014-04-02 13:28:49 -0700 | [diff] [blame] | 893 | static int spc_modesense_caching(struct se_cmd *cmd, u8 pc, u8 *p) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 894 | { |
Nicholas Bellinger | d45aca42a | 2014-04-02 13:28:49 -0700 | [diff] [blame] | 895 | struct se_device *dev = cmd->se_dev; |
| 896 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 897 | p[0] = 0x08; |
| 898 | p[1] = 0x12; |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 899 | |
| 900 | /* No changeable values for now */ |
| 901 | if (pc == 1) |
| 902 | goto out; |
| 903 | |
Nicholas Bellinger | d0c8b25 | 2013-01-29 22:10:06 -0800 | [diff] [blame] | 904 | if (spc_check_dev_wce(dev)) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 905 | p[2] = 0x04; /* Write Cache Enable */ |
| 906 | p[12] = 0x20; /* Disabled Read Ahead */ |
| 907 | |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 908 | out: |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 909 | return 20; |
| 910 | } |
| 911 | |
Nicholas Bellinger | d45aca42a | 2014-04-02 13:28:49 -0700 | [diff] [blame] | 912 | static int spc_modesense_informational_exceptions(struct se_cmd *cmd, u8 pc, unsigned char *p) |
Roland Dreier | 0f6d64c | 2012-10-31 09:16:49 -0700 | [diff] [blame] | 913 | { |
| 914 | p[0] = 0x1c; |
| 915 | p[1] = 0x0a; |
| 916 | |
| 917 | /* No changeable values for now */ |
| 918 | if (pc == 1) |
| 919 | goto out; |
| 920 | |
| 921 | out: |
| 922 | return 12; |
| 923 | } |
| 924 | |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 925 | static struct { |
| 926 | uint8_t page; |
| 927 | uint8_t subpage; |
Nicholas Bellinger | d45aca42a | 2014-04-02 13:28:49 -0700 | [diff] [blame] | 928 | int (*emulate)(struct se_cmd *, u8, unsigned char *); |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 929 | } modesense_handlers[] = { |
| 930 | { .page = 0x01, .subpage = 0x00, .emulate = spc_modesense_rwrecovery }, |
| 931 | { .page = 0x08, .subpage = 0x00, .emulate = spc_modesense_caching }, |
| 932 | { .page = 0x0a, .subpage = 0x00, .emulate = spc_modesense_control }, |
Roland Dreier | 0f6d64c | 2012-10-31 09:16:49 -0700 | [diff] [blame] | 933 | { .page = 0x1c, .subpage = 0x00, .emulate = spc_modesense_informational_exceptions }, |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 934 | }; |
| 935 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 936 | static void spc_modesense_write_protect(unsigned char *buf, int type) |
| 937 | { |
| 938 | /* |
| 939 | * I believe that the WP bit (bit 7) in the mode header is the same for |
| 940 | * all device types.. |
| 941 | */ |
| 942 | switch (type) { |
| 943 | case TYPE_DISK: |
| 944 | case TYPE_TAPE: |
| 945 | default: |
| 946 | buf[0] |= 0x80; /* WP bit */ |
| 947 | break; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | static void spc_modesense_dpofua(unsigned char *buf, int type) |
| 952 | { |
| 953 | switch (type) { |
| 954 | case TYPE_DISK: |
| 955 | buf[0] |= 0x10; /* DPOFUA bit */ |
| 956 | break; |
| 957 | default: |
| 958 | break; |
| 959 | } |
| 960 | } |
| 961 | |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 962 | static int spc_modesense_blockdesc(unsigned char *buf, u64 blocks, u32 block_size) |
| 963 | { |
| 964 | *buf++ = 8; |
| 965 | put_unaligned_be32(min(blocks, 0xffffffffull), buf); |
| 966 | buf += 4; |
| 967 | put_unaligned_be32(block_size, buf); |
| 968 | return 9; |
| 969 | } |
| 970 | |
| 971 | static int spc_modesense_long_blockdesc(unsigned char *buf, u64 blocks, u32 block_size) |
| 972 | { |
| 973 | if (blocks <= 0xffffffff) |
| 974 | return spc_modesense_blockdesc(buf + 3, blocks, block_size) + 3; |
| 975 | |
| 976 | *buf++ = 1; /* LONGLBA */ |
| 977 | buf += 2; |
| 978 | *buf++ = 16; |
| 979 | put_unaligned_be64(blocks, buf); |
| 980 | buf += 12; |
| 981 | put_unaligned_be32(block_size, buf); |
| 982 | |
| 983 | return 17; |
| 984 | } |
| 985 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 986 | static sense_reason_t spc_emulate_modesense(struct se_cmd *cmd) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 987 | { |
| 988 | struct se_device *dev = cmd->se_dev; |
| 989 | char *cdb = cmd->t_task_cdb; |
Nicholas Bellinger | cab9609 | 2013-01-29 14:10:01 -0800 | [diff] [blame] | 990 | unsigned char buf[SE_MODE_PAGE_BUF], *rbuf; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 991 | int type = dev->transport->get_device_type(dev); |
| 992 | int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10); |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 993 | bool dbd = !!(cdb[1] & 0x08); |
| 994 | bool llba = ten ? !!(cdb[1] & 0x10) : false; |
| 995 | u8 pc = cdb[2] >> 6; |
| 996 | u8 page = cdb[2] & 0x3f; |
| 997 | u8 subpage = cdb[3]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 998 | int length = 0; |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 999 | int ret; |
| 1000 | int i; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1001 | |
Nicholas Bellinger | cab9609 | 2013-01-29 14:10:01 -0800 | [diff] [blame] | 1002 | memset(buf, 0, SE_MODE_PAGE_BUF); |
| 1003 | |
Nicholas Bellinger | fecae40 | 2012-11-01 18:43:03 -0700 | [diff] [blame] | 1004 | /* |
| 1005 | * Skip over MODE DATA LENGTH + MEDIUM TYPE fields to byte 3 for |
| 1006 | * MODE_SENSE_10 and byte 2 for MODE_SENSE (6). |
| 1007 | */ |
| 1008 | length = ten ? 3 : 2; |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 1009 | |
| 1010 | /* DEVICE-SPECIFIC PARAMETER */ |
| 1011 | if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) || |
| 1012 | (cmd->se_deve && |
| 1013 | (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY))) |
| 1014 | spc_modesense_write_protect(&buf[length], type); |
| 1015 | |
Nicholas Bellinger | d0c8b25 | 2013-01-29 22:10:06 -0800 | [diff] [blame] | 1016 | if ((spc_check_dev_wce(dev)) && |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 1017 | (dev->dev_attrib.emulate_fua_write > 0)) |
| 1018 | spc_modesense_dpofua(&buf[length], type); |
| 1019 | |
| 1020 | ++length; |
| 1021 | |
| 1022 | /* BLOCK DESCRIPTOR */ |
| 1023 | |
| 1024 | /* |
| 1025 | * For now we only include a block descriptor for disk (SBC) |
| 1026 | * devices; other command sets use a slightly different format. |
| 1027 | */ |
| 1028 | if (!dbd && type == TYPE_DISK) { |
| 1029 | u64 blocks = dev->transport->get_blocks(dev); |
| 1030 | u32 block_size = dev->dev_attrib.block_size; |
| 1031 | |
| 1032 | if (ten) { |
| 1033 | if (llba) { |
| 1034 | length += spc_modesense_long_blockdesc(&buf[length], |
| 1035 | blocks, block_size); |
| 1036 | } else { |
| 1037 | length += 3; |
| 1038 | length += spc_modesense_blockdesc(&buf[length], |
| 1039 | blocks, block_size); |
| 1040 | } |
| 1041 | } else { |
| 1042 | length += spc_modesense_blockdesc(&buf[length], blocks, |
| 1043 | block_size); |
| 1044 | } |
| 1045 | } else { |
| 1046 | if (ten) |
| 1047 | length += 4; |
| 1048 | else |
| 1049 | length += 1; |
Paolo Bonzini | 7a3f369 | 2012-09-07 17:30:39 +0200 | [diff] [blame] | 1050 | } |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1051 | |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 1052 | if (page == 0x3f) { |
| 1053 | if (subpage != 0x00 && subpage != 0xff) { |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1054 | pr_warn("MODE_SENSE: Invalid subpage code: 0x%02x\n", subpage); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1055 | return TCM_INVALID_CDB_FIELD; |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) { |
| 1059 | /* |
| 1060 | * Tricky way to say all subpage 00h for |
| 1061 | * subpage==0, all subpages for subpage==0xff |
| 1062 | * (and we just checked above that those are |
| 1063 | * the only two possibilities). |
| 1064 | */ |
| 1065 | if ((modesense_handlers[i].subpage & ~subpage) == 0) { |
Nicholas Bellinger | d45aca42a | 2014-04-02 13:28:49 -0700 | [diff] [blame] | 1066 | ret = modesense_handlers[i].emulate(cmd, pc, &buf[length]); |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 1067 | if (!ten && length + ret >= 255) |
| 1068 | break; |
| 1069 | length += ret; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | goto set_length; |
| 1074 | } |
| 1075 | |
| 1076 | for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) |
| 1077 | if (modesense_handlers[i].page == page && |
| 1078 | modesense_handlers[i].subpage == subpage) { |
Nicholas Bellinger | d45aca42a | 2014-04-02 13:28:49 -0700 | [diff] [blame] | 1079 | length += modesense_handlers[i].emulate(cmd, pc, &buf[length]); |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 1080 | goto set_length; |
| 1081 | } |
| 1082 | |
| 1083 | /* |
| 1084 | * We don't intend to implement: |
| 1085 | * - obsolete page 03h "format parameters" (checked by Solaris) |
| 1086 | */ |
| 1087 | if (page != 0x03) |
| 1088 | pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n", |
| 1089 | page, subpage); |
| 1090 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1091 | return TCM_UNKNOWN_MODE_PAGE; |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 1092 | |
| 1093 | set_length: |
| 1094 | if (ten) |
| 1095 | put_unaligned_be16(length - 2, buf); |
| 1096 | else |
| 1097 | buf[0] = length - 1; |
| 1098 | |
Nicholas Bellinger | cab9609 | 2013-01-29 14:10:01 -0800 | [diff] [blame] | 1099 | rbuf = transport_kmap_data_sg(cmd); |
| 1100 | if (rbuf) { |
| 1101 | memcpy(rbuf, buf, min_t(u32, SE_MODE_PAGE_BUF, cmd->data_length)); |
| 1102 | transport_kunmap_data_sg(cmd); |
Roland Dreier | d4b2b86 | 2012-10-31 09:16:48 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Roland Dreier | 2426bd4 | 2014-06-10 11:07:47 -0700 | [diff] [blame] | 1105 | target_complete_cmd_with_length(cmd, GOOD, length); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1106 | return 0; |
| 1107 | } |
| 1108 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1109 | static sense_reason_t spc_emulate_modeselect(struct se_cmd *cmd) |
Roland Dreier | 3a3c5e4 | 2012-10-31 09:16:50 -0700 | [diff] [blame] | 1110 | { |
Roland Dreier | 3a3c5e4 | 2012-10-31 09:16:50 -0700 | [diff] [blame] | 1111 | char *cdb = cmd->t_task_cdb; |
| 1112 | bool ten = cdb[0] == MODE_SELECT_10; |
| 1113 | int off = ten ? 8 : 4; |
| 1114 | bool pf = !!(cdb[1] & 0x10); |
| 1115 | u8 page, subpage; |
| 1116 | unsigned char *buf; |
| 1117 | unsigned char tbuf[SE_MODE_PAGE_BUF]; |
| 1118 | int length; |
Christoph Hellwig | 3abff1e | 2015-03-26 12:27:35 +0100 | [diff] [blame] | 1119 | sense_reason_t ret = 0; |
Roland Dreier | 3a3c5e4 | 2012-10-31 09:16:50 -0700 | [diff] [blame] | 1120 | int i; |
| 1121 | |
Roland Dreier | 71f41fe | 2013-02-08 15:18:40 -0800 | [diff] [blame] | 1122 | if (!cmd->data_length) { |
| 1123 | target_complete_cmd(cmd, GOOD); |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | if (cmd->data_length < off + 2) |
| 1128 | return TCM_PARAMETER_LIST_LENGTH_ERROR; |
| 1129 | |
Roland Dreier | 3a3c5e4 | 2012-10-31 09:16:50 -0700 | [diff] [blame] | 1130 | buf = transport_kmap_data_sg(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1131 | if (!buf) |
| 1132 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Roland Dreier | 3a3c5e4 | 2012-10-31 09:16:50 -0700 | [diff] [blame] | 1133 | |
| 1134 | if (!pf) { |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1135 | ret = TCM_INVALID_CDB_FIELD; |
Roland Dreier | 3a3c5e4 | 2012-10-31 09:16:50 -0700 | [diff] [blame] | 1136 | goto out; |
| 1137 | } |
| 1138 | |
| 1139 | page = buf[off] & 0x3f; |
| 1140 | subpage = buf[off] & 0x40 ? buf[off + 1] : 0; |
| 1141 | |
| 1142 | for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) |
| 1143 | if (modesense_handlers[i].page == page && |
| 1144 | modesense_handlers[i].subpage == subpage) { |
| 1145 | memset(tbuf, 0, SE_MODE_PAGE_BUF); |
Nicholas Bellinger | d45aca42a | 2014-04-02 13:28:49 -0700 | [diff] [blame] | 1146 | length = modesense_handlers[i].emulate(cmd, 0, tbuf); |
Roland Dreier | 3a3c5e4 | 2012-10-31 09:16:50 -0700 | [diff] [blame] | 1147 | goto check_contents; |
| 1148 | } |
| 1149 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1150 | ret = TCM_UNKNOWN_MODE_PAGE; |
Roland Dreier | 3a3c5e4 | 2012-10-31 09:16:50 -0700 | [diff] [blame] | 1151 | goto out; |
| 1152 | |
| 1153 | check_contents: |
Roland Dreier | 71f41fe | 2013-02-08 15:18:40 -0800 | [diff] [blame] | 1154 | if (cmd->data_length < off + length) { |
| 1155 | ret = TCM_PARAMETER_LIST_LENGTH_ERROR; |
| 1156 | goto out; |
| 1157 | } |
| 1158 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1159 | if (memcmp(buf + off, tbuf, length)) |
| 1160 | ret = TCM_INVALID_PARAMETER_LIST; |
Roland Dreier | 3a3c5e4 | 2012-10-31 09:16:50 -0700 | [diff] [blame] | 1161 | |
| 1162 | out: |
| 1163 | transport_kunmap_data_sg(cmd); |
| 1164 | |
| 1165 | if (!ret) |
| 1166 | target_complete_cmd(cmd, GOOD); |
| 1167 | return ret; |
| 1168 | } |
| 1169 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1170 | static sense_reason_t spc_emulate_request_sense(struct se_cmd *cmd) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1171 | { |
| 1172 | unsigned char *cdb = cmd->t_task_cdb; |
Paolo Bonzini | 32a8811 | 2012-09-07 17:30:36 +0200 | [diff] [blame] | 1173 | unsigned char *rbuf; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1174 | u8 ua_asc = 0, ua_ascq = 0; |
Paolo Bonzini | 32a8811 | 2012-09-07 17:30:36 +0200 | [diff] [blame] | 1175 | unsigned char buf[SE_SENSE_BUF]; |
| 1176 | |
| 1177 | memset(buf, 0, SE_SENSE_BUF); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1178 | |
| 1179 | if (cdb[1] & 0x01) { |
| 1180 | pr_err("REQUEST_SENSE description emulation not" |
| 1181 | " supported\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1182 | return TCM_INVALID_CDB_FIELD; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1183 | } |
| 1184 | |
Paolo Bonzini | 32a8811 | 2012-09-07 17:30:36 +0200 | [diff] [blame] | 1185 | rbuf = transport_kmap_data_sg(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1186 | if (!rbuf) |
| 1187 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 1188 | |
| 1189 | if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) { |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1190 | /* |
| 1191 | * CURRENT ERROR, UNIT ATTENTION |
| 1192 | */ |
| 1193 | buf[0] = 0x70; |
| 1194 | buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION; |
| 1195 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1196 | /* |
| 1197 | * The Additional Sense Code (ASC) from the UNIT ATTENTION |
| 1198 | */ |
| 1199 | buf[SPC_ASC_KEY_OFFSET] = ua_asc; |
| 1200 | buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq; |
| 1201 | buf[7] = 0x0A; |
| 1202 | } else { |
| 1203 | /* |
| 1204 | * CURRENT ERROR, NO SENSE |
| 1205 | */ |
| 1206 | buf[0] = 0x70; |
| 1207 | buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE; |
| 1208 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1209 | /* |
| 1210 | * NO ADDITIONAL SENSE INFORMATION |
| 1211 | */ |
| 1212 | buf[SPC_ASC_KEY_OFFSET] = 0x00; |
| 1213 | buf[7] = 0x0A; |
| 1214 | } |
| 1215 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1216 | memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length)); |
| 1217 | transport_kunmap_data_sg(cmd); |
Paolo Bonzini | 32a8811 | 2012-09-07 17:30:36 +0200 | [diff] [blame] | 1218 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1219 | target_complete_cmd(cmd, GOOD); |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1223 | sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd) |
Christoph Hellwig | d1b1f80 | 2012-10-07 10:55:51 -0400 | [diff] [blame] | 1224 | { |
| 1225 | struct se_dev_entry *deve; |
| 1226 | struct se_session *sess = cmd->se_sess; |
| 1227 | unsigned char *buf; |
| 1228 | u32 lun_count = 0, offset = 8, i; |
| 1229 | |
| 1230 | if (cmd->data_length < 16) { |
| 1231 | pr_warn("REPORT LUNS allocation length %u too small\n", |
| 1232 | cmd->data_length); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1233 | return TCM_INVALID_CDB_FIELD; |
Christoph Hellwig | d1b1f80 | 2012-10-07 10:55:51 -0400 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | buf = transport_kmap_data_sg(cmd); |
| 1237 | if (!buf) |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1238 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Christoph Hellwig | d1b1f80 | 2012-10-07 10:55:51 -0400 | [diff] [blame] | 1239 | |
| 1240 | /* |
| 1241 | * If no struct se_session pointer is present, this struct se_cmd is |
| 1242 | * coming via a target_core_mod PASSTHROUGH op, and not through |
| 1243 | * a $FABRIC_MOD. In that case, report LUN=0 only. |
| 1244 | */ |
| 1245 | if (!sess) { |
| 1246 | int_to_scsilun(0, (struct scsi_lun *)&buf[offset]); |
| 1247 | lun_count = 1; |
| 1248 | goto done; |
| 1249 | } |
| 1250 | |
| 1251 | spin_lock_irq(&sess->se_node_acl->device_list_lock); |
| 1252 | for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) { |
| 1253 | deve = sess->se_node_acl->device_list[i]; |
| 1254 | if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)) |
| 1255 | continue; |
| 1256 | /* |
| 1257 | * We determine the correct LUN LIST LENGTH even once we |
| 1258 | * have reached the initial allocation length. |
| 1259 | * See SPC2-R20 7.19. |
| 1260 | */ |
| 1261 | lun_count++; |
| 1262 | if ((offset + 8) > cmd->data_length) |
| 1263 | continue; |
| 1264 | |
| 1265 | int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]); |
| 1266 | offset += 8; |
| 1267 | } |
| 1268 | spin_unlock_irq(&sess->se_node_acl->device_list_lock); |
| 1269 | |
| 1270 | /* |
| 1271 | * See SPC3 r07, page 159. |
| 1272 | */ |
| 1273 | done: |
| 1274 | lun_count *= 8; |
| 1275 | buf[0] = ((lun_count >> 24) & 0xff); |
| 1276 | buf[1] = ((lun_count >> 16) & 0xff); |
| 1277 | buf[2] = ((lun_count >> 8) & 0xff); |
| 1278 | buf[3] = (lun_count & 0xff); |
| 1279 | transport_kunmap_data_sg(cmd); |
| 1280 | |
Roland Dreier | 2426bd4 | 2014-06-10 11:07:47 -0700 | [diff] [blame] | 1281 | target_complete_cmd_with_length(cmd, GOOD, 8 + lun_count * 8); |
Christoph Hellwig | d1b1f80 | 2012-10-07 10:55:51 -0400 | [diff] [blame] | 1282 | return 0; |
| 1283 | } |
Christoph Hellwig | 8de530a | 2012-10-07 10:55:52 -0400 | [diff] [blame] | 1284 | EXPORT_SYMBOL(spc_emulate_report_luns); |
Christoph Hellwig | d1b1f80 | 2012-10-07 10:55:51 -0400 | [diff] [blame] | 1285 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1286 | static sense_reason_t |
| 1287 | spc_emulate_testunitready(struct se_cmd *cmd) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1288 | { |
| 1289 | target_complete_cmd(cmd, GOOD); |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1293 | sense_reason_t |
| 1294 | spc_parse_cdb(struct se_cmd *cmd, unsigned int *size) |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1295 | { |
Nicholas Bellinger | eba2ca4 | 2012-05-30 14:09:10 -0700 | [diff] [blame] | 1296 | struct se_device *dev = cmd->se_dev; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1297 | unsigned char *cdb = cmd->t_task_cdb; |
| 1298 | |
| 1299 | switch (cdb[0]) { |
| 1300 | case MODE_SELECT: |
| 1301 | *size = cdb[4]; |
Roland Dreier | 3a3c5e4 | 2012-10-31 09:16:50 -0700 | [diff] [blame] | 1302 | cmd->execute_cmd = spc_emulate_modeselect; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1303 | break; |
| 1304 | case MODE_SELECT_10: |
| 1305 | *size = (cdb[7] << 8) + cdb[8]; |
Roland Dreier | 3a3c5e4 | 2012-10-31 09:16:50 -0700 | [diff] [blame] | 1306 | cmd->execute_cmd = spc_emulate_modeselect; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1307 | break; |
| 1308 | case MODE_SENSE: |
| 1309 | *size = cdb[4]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1310 | cmd->execute_cmd = spc_emulate_modesense; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1311 | break; |
| 1312 | case MODE_SENSE_10: |
| 1313 | *size = (cdb[7] << 8) + cdb[8]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1314 | cmd->execute_cmd = spc_emulate_modesense; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1315 | break; |
| 1316 | case LOG_SELECT: |
| 1317 | case LOG_SENSE: |
| 1318 | *size = (cdb[7] << 8) + cdb[8]; |
| 1319 | break; |
| 1320 | case PERSISTENT_RESERVE_IN: |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1321 | *size = (cdb[7] << 8) + cdb[8]; |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 1322 | cmd->execute_cmd = target_scsi3_emulate_pr_in; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1323 | break; |
| 1324 | case PERSISTENT_RESERVE_OUT: |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1325 | *size = (cdb[7] << 8) + cdb[8]; |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 1326 | cmd->execute_cmd = target_scsi3_emulate_pr_out; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1327 | break; |
| 1328 | case RELEASE: |
| 1329 | case RELEASE_10: |
| 1330 | if (cdb[0] == RELEASE_10) |
| 1331 | *size = (cdb[7] << 8) | cdb[8]; |
| 1332 | else |
| 1333 | *size = cmd->data_length; |
| 1334 | |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 1335 | cmd->execute_cmd = target_scsi2_reservation_release; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1336 | break; |
| 1337 | case RESERVE: |
| 1338 | case RESERVE_10: |
| 1339 | /* |
| 1340 | * The SPC-2 RESERVE does not contain a size in the SCSI CDB. |
| 1341 | * Assume the passthrough or $FABRIC_MOD will tell us about it. |
| 1342 | */ |
| 1343 | if (cdb[0] == RESERVE_10) |
| 1344 | *size = (cdb[7] << 8) | cdb[8]; |
| 1345 | else |
| 1346 | *size = cmd->data_length; |
| 1347 | |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 1348 | cmd->execute_cmd = target_scsi2_reservation_reserve; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1349 | break; |
| 1350 | case REQUEST_SENSE: |
| 1351 | *size = cdb[4]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1352 | cmd->execute_cmd = spc_emulate_request_sense; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1353 | break; |
| 1354 | case INQUIRY: |
| 1355 | *size = (cdb[3] << 8) + cdb[4]; |
| 1356 | |
| 1357 | /* |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 1358 | * Do implicit HEAD_OF_QUEUE processing for INQUIRY. |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1359 | * See spc4r17 section 5.3 |
| 1360 | */ |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1361 | cmd->sam_task_attr = TCM_HEAD_TAG; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1362 | cmd->execute_cmd = spc_emulate_inquiry; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1363 | break; |
| 1364 | case SECURITY_PROTOCOL_IN: |
| 1365 | case SECURITY_PROTOCOL_OUT: |
| 1366 | *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; |
| 1367 | break; |
| 1368 | case EXTENDED_COPY: |
Nicholas Bellinger | 04b1b79 | 2013-08-22 12:29:59 -0700 | [diff] [blame] | 1369 | *size = get_unaligned_be32(&cdb[10]); |
| 1370 | cmd->execute_cmd = target_do_xcopy; |
| 1371 | break; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1372 | case RECEIVE_COPY_RESULTS: |
Nicholas Bellinger | 04b1b79 | 2013-08-22 12:29:59 -0700 | [diff] [blame] | 1373 | *size = get_unaligned_be32(&cdb[10]); |
| 1374 | cmd->execute_cmd = target_do_receive_copy_results; |
| 1375 | break; |
| 1376 | case READ_ATTRIBUTE: |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1377 | case WRITE_ATTRIBUTE: |
| 1378 | *size = (cdb[10] << 24) | (cdb[11] << 16) | |
| 1379 | (cdb[12] << 8) | cdb[13]; |
| 1380 | break; |
| 1381 | case RECEIVE_DIAGNOSTIC: |
| 1382 | case SEND_DIAGNOSTIC: |
| 1383 | *size = (cdb[3] << 8) | cdb[4]; |
| 1384 | break; |
| 1385 | case WRITE_BUFFER: |
| 1386 | *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8]; |
| 1387 | break; |
| 1388 | case REPORT_LUNS: |
Christoph Hellwig | d1b1f80 | 2012-10-07 10:55:51 -0400 | [diff] [blame] | 1389 | cmd->execute_cmd = spc_emulate_report_luns; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1390 | *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; |
| 1391 | /* |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 1392 | * Do implicit HEAD_OF_QUEUE processing for REPORT_LUNS |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1393 | * See spc4r17 section 5.3 |
| 1394 | */ |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1395 | cmd->sam_task_attr = TCM_HEAD_TAG; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1396 | break; |
| 1397 | case TEST_UNIT_READY: |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1398 | cmd->execute_cmd = spc_emulate_testunitready; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1399 | *size = 0; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1400 | break; |
Nicholas Bellinger | eba2ca4 | 2012-05-30 14:09:10 -0700 | [diff] [blame] | 1401 | case MAINTENANCE_IN: |
| 1402 | if (dev->transport->get_device_type(dev) != TYPE_ROM) { |
| 1403 | /* |
| 1404 | * MAINTENANCE_IN from SCC-2 |
| 1405 | * Check for emulated MI_REPORT_TARGET_PGS |
| 1406 | */ |
Christoph Hellwig | c87fbd5 | 2012-10-10 17:37:16 -0400 | [diff] [blame] | 1407 | if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) { |
Nicholas Bellinger | eba2ca4 | 2012-05-30 14:09:10 -0700 | [diff] [blame] | 1408 | cmd->execute_cmd = |
| 1409 | target_emulate_report_target_port_groups; |
| 1410 | } |
| 1411 | *size = get_unaligned_be32(&cdb[6]); |
| 1412 | } else { |
| 1413 | /* |
| 1414 | * GPCMD_SEND_KEY from multi media commands |
| 1415 | */ |
| 1416 | *size = get_unaligned_be16(&cdb[8]); |
| 1417 | } |
| 1418 | break; |
| 1419 | case MAINTENANCE_OUT: |
| 1420 | if (dev->transport->get_device_type(dev) != TYPE_ROM) { |
| 1421 | /* |
| 1422 | * MAINTENANCE_OUT from SCC-2 |
| 1423 | * Check for emulated MO_SET_TARGET_PGS. |
| 1424 | */ |
Christoph Hellwig | c87fbd5 | 2012-10-10 17:37:16 -0400 | [diff] [blame] | 1425 | if (cdb[1] == MO_SET_TARGET_PGS) { |
Nicholas Bellinger | eba2ca4 | 2012-05-30 14:09:10 -0700 | [diff] [blame] | 1426 | cmd->execute_cmd = |
| 1427 | target_emulate_set_target_port_groups; |
| 1428 | } |
| 1429 | *size = get_unaligned_be32(&cdb[6]); |
| 1430 | } else { |
| 1431 | /* |
| 1432 | * GPCMD_SEND_KEY from multi media commands |
| 1433 | */ |
| 1434 | *size = get_unaligned_be16(&cdb[8]); |
| 1435 | } |
| 1436 | break; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1437 | default: |
| 1438 | pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode" |
| 1439 | " 0x%02x, sending CHECK_CONDITION.\n", |
| 1440 | cmd->se_tfo->get_fabric_name(), cdb[0]); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1441 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | return 0; |
| 1445 | } |
| 1446 | EXPORT_SYMBOL(spc_parse_cdb); |