Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1 | /* |
| 2 | * SCSI Primary Commands (SPC) parsing and emulation. |
| 3 | * |
| 4 | * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc. |
| 5 | * Copyright (c) 2005, 2006, 2007 SBE, Inc. |
| 6 | * Copyright (c) 2007-2010 Rising Tide Systems |
| 7 | * Copyright (c) 2008-2010 Linux-iSCSI.org |
| 8 | * |
| 9 | * Nicholas A. Bellinger <nab@kernel.org> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <scsi/scsi.h> |
| 31 | #include <scsi/scsi_tcq.h> |
| 32 | |
| 33 | #include <target/target_core_base.h> |
| 34 | #include <target/target_core_backend.h> |
| 35 | #include <target/target_core_fabric.h> |
| 36 | |
| 37 | #include "target_core_internal.h" |
Nicholas Bellinger | eba2ca4 | 2012-05-30 14:09:10 -0700 | [diff] [blame] | 38 | #include "target_core_alua.h" |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 39 | #include "target_core_pr.h" |
| 40 | #include "target_core_ua.h" |
| 41 | |
| 42 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 43 | static void spc_fill_alua_data(struct se_port *port, unsigned char *buf) |
| 44 | { |
| 45 | struct t10_alua_tg_pt_gp *tg_pt_gp; |
| 46 | struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; |
| 47 | |
| 48 | /* |
| 49 | * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS. |
| 50 | */ |
| 51 | buf[5] = 0x80; |
| 52 | |
| 53 | /* |
| 54 | * Set TPGS field for explict and/or implict ALUA access type |
| 55 | * and opteration. |
| 56 | * |
| 57 | * See spc4r17 section 6.4.2 Table 135 |
| 58 | */ |
| 59 | if (!port) |
| 60 | return; |
| 61 | tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; |
| 62 | if (!tg_pt_gp_mem) |
| 63 | return; |
| 64 | |
| 65 | spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); |
| 66 | tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; |
| 67 | if (tg_pt_gp) |
| 68 | buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type; |
| 69 | spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); |
| 70 | } |
| 71 | |
| 72 | static int spc_emulate_inquiry_std(struct se_cmd *cmd, char *buf) |
| 73 | { |
| 74 | struct se_lun *lun = cmd->se_lun; |
| 75 | struct se_device *dev = cmd->se_dev; |
| 76 | |
| 77 | /* Set RMB (removable media) for tape devices */ |
| 78 | if (dev->transport->get_device_type(dev) == TYPE_TAPE) |
| 79 | buf[1] = 0x80; |
| 80 | |
| 81 | buf[2] = dev->transport->get_device_rev(dev); |
| 82 | |
| 83 | /* |
| 84 | * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2 |
| 85 | * |
| 86 | * SPC4 says: |
| 87 | * A RESPONSE DATA FORMAT field set to 2h indicates that the |
| 88 | * standard INQUIRY data is in the format defined in this |
| 89 | * standard. Response data format values less than 2h are |
| 90 | * obsolete. Response data format values greater than 2h are |
| 91 | * reserved. |
| 92 | */ |
| 93 | buf[3] = 2; |
| 94 | |
| 95 | /* |
| 96 | * Enable SCCS and TPGS fields for Emulated ALUA |
| 97 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 98 | if (dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 99 | spc_fill_alua_data(lun->lun_sep, buf); |
| 100 | |
| 101 | buf[7] = 0x2; /* CmdQue=1 */ |
| 102 | |
| 103 | snprintf(&buf[8], 8, "LIO-ORG"); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 104 | snprintf(&buf[16], 16, "%s", dev->t10_wwn.model); |
| 105 | snprintf(&buf[32], 4, "%s", dev->t10_wwn.revision); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 106 | buf[4] = 31; /* Set additional length to 31 */ |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /* unit serial number */ |
| 112 | static int spc_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf) |
| 113 | { |
| 114 | struct se_device *dev = cmd->se_dev; |
| 115 | u16 len = 0; |
| 116 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 117 | if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) { |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 118 | u32 unit_serial_len; |
| 119 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 120 | unit_serial_len = strlen(dev->t10_wwn.unit_serial); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 121 | unit_serial_len++; /* For NULL Terminator */ |
| 122 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 123 | len += sprintf(&buf[4], "%s", dev->t10_wwn.unit_serial); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 124 | len++; /* Extra Byte for NULL Terminator */ |
| 125 | buf[3] = len; |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static void spc_parse_naa_6h_vendor_specific(struct se_device *dev, |
| 131 | unsigned char *buf) |
| 132 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 133 | unsigned char *p = &dev->t10_wwn.unit_serial[0]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 134 | int cnt; |
| 135 | bool next = true; |
| 136 | |
| 137 | /* |
| 138 | * Generate up to 36 bits of VENDOR SPECIFIC IDENTIFIER starting on |
| 139 | * byte 3 bit 3-0 for NAA IEEE Registered Extended DESIGNATOR field |
| 140 | * format, followed by 64 bits of VENDOR SPECIFIC IDENTIFIER EXTENSION |
| 141 | * to complete the payload. These are based from VPD=0x80 PRODUCT SERIAL |
| 142 | * NUMBER set via vpd_unit_serial in target_core_configfs.c to ensure |
| 143 | * per device uniqeness. |
| 144 | */ |
| 145 | for (cnt = 0; *p && cnt < 13; p++) { |
| 146 | int val = hex_to_bin(*p); |
| 147 | |
| 148 | if (val < 0) |
| 149 | continue; |
| 150 | |
| 151 | if (next) { |
| 152 | next = false; |
| 153 | buf[cnt++] |= val; |
| 154 | } else { |
| 155 | next = true; |
| 156 | buf[cnt] = val << 4; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Device identification VPD, for a complete list of |
| 163 | * DESIGNATOR TYPEs see spc4r17 Table 459. |
| 164 | */ |
| 165 | static int spc_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf) |
| 166 | { |
| 167 | struct se_device *dev = cmd->se_dev; |
| 168 | struct se_lun *lun = cmd->se_lun; |
| 169 | struct se_port *port = NULL; |
| 170 | struct se_portal_group *tpg = NULL; |
| 171 | struct t10_alua_lu_gp_member *lu_gp_mem; |
| 172 | struct t10_alua_tg_pt_gp *tg_pt_gp; |
| 173 | struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 174 | unsigned char *prod = &dev->t10_wwn.model[0]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 175 | u32 prod_len; |
| 176 | u32 unit_serial_len, off = 0; |
| 177 | u16 len = 0, id_len; |
| 178 | |
| 179 | off = 4; |
| 180 | |
| 181 | /* |
| 182 | * NAA IEEE Registered Extended Assigned designator format, see |
| 183 | * spc4r17 section 7.7.3.6.5 |
| 184 | * |
| 185 | * We depend upon a target_core_mod/ConfigFS provided |
| 186 | * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial |
| 187 | * value in order to return the NAA id. |
| 188 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 189 | if (!(dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL)) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 190 | goto check_t10_vend_desc; |
| 191 | |
| 192 | /* CODE SET == Binary */ |
| 193 | buf[off++] = 0x1; |
| 194 | |
| 195 | /* Set ASSOCIATION == addressed logical unit: 0)b */ |
| 196 | buf[off] = 0x00; |
| 197 | |
| 198 | /* Identifier/Designator type == NAA identifier */ |
| 199 | buf[off++] |= 0x3; |
| 200 | off++; |
| 201 | |
| 202 | /* Identifier/Designator length */ |
| 203 | buf[off++] = 0x10; |
| 204 | |
| 205 | /* |
| 206 | * Start NAA IEEE Registered Extended Identifier/Designator |
| 207 | */ |
| 208 | buf[off++] = (0x6 << 4); |
| 209 | |
| 210 | /* |
| 211 | * Use OpenFabrics IEEE Company ID: 00 14 05 |
| 212 | */ |
| 213 | buf[off++] = 0x01; |
| 214 | buf[off++] = 0x40; |
| 215 | buf[off] = (0x5 << 4); |
| 216 | |
| 217 | /* |
| 218 | * Return ConfigFS Unit Serial Number information for |
| 219 | * VENDOR_SPECIFIC_IDENTIFIER and |
| 220 | * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION |
| 221 | */ |
| 222 | spc_parse_naa_6h_vendor_specific(dev, &buf[off]); |
| 223 | |
| 224 | len = 20; |
| 225 | off = (len + 4); |
| 226 | |
| 227 | check_t10_vend_desc: |
| 228 | /* |
| 229 | * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4 |
| 230 | */ |
| 231 | id_len = 8; /* For Vendor field */ |
| 232 | prod_len = 4; /* For VPD Header */ |
| 233 | prod_len += 8; /* For Vendor field */ |
| 234 | prod_len += strlen(prod); |
| 235 | prod_len++; /* For : */ |
| 236 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 237 | if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) { |
| 238 | unit_serial_len = strlen(&dev->t10_wwn.unit_serial[0]); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 239 | unit_serial_len++; /* For NULL Terminator */ |
| 240 | |
| 241 | id_len += sprintf(&buf[off+12], "%s:%s", prod, |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 242 | &dev->t10_wwn.unit_serial[0]); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 243 | } |
| 244 | buf[off] = 0x2; /* ASCII */ |
| 245 | buf[off+1] = 0x1; /* T10 Vendor ID */ |
| 246 | buf[off+2] = 0x0; |
| 247 | memcpy(&buf[off+4], "LIO-ORG", 8); |
| 248 | /* Extra Byte for NULL Terminator */ |
| 249 | id_len++; |
| 250 | /* Identifier Length */ |
| 251 | buf[off+3] = id_len; |
| 252 | /* Header size for Designation descriptor */ |
| 253 | len += (id_len + 4); |
| 254 | off += (id_len + 4); |
| 255 | /* |
| 256 | * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD |
| 257 | */ |
| 258 | port = lun->lun_sep; |
| 259 | if (port) { |
| 260 | struct t10_alua_lu_gp *lu_gp; |
| 261 | u32 padding, scsi_name_len; |
| 262 | u16 lu_gp_id = 0; |
| 263 | u16 tg_pt_gp_id = 0; |
| 264 | u16 tpgt; |
| 265 | |
| 266 | tpg = port->sep_tpg; |
| 267 | /* |
| 268 | * Relative target port identifer, see spc4r17 |
| 269 | * section 7.7.3.7 |
| 270 | * |
| 271 | * Get the PROTOCOL IDENTIFIER as defined by spc4r17 |
| 272 | * section 7.5.1 Table 362 |
| 273 | */ |
| 274 | buf[off] = |
| 275 | (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4); |
| 276 | buf[off++] |= 0x1; /* CODE SET == Binary */ |
| 277 | buf[off] = 0x80; /* Set PIV=1 */ |
| 278 | /* Set ASSOCIATION == target port: 01b */ |
| 279 | buf[off] |= 0x10; |
| 280 | /* DESIGNATOR TYPE == Relative target port identifer */ |
| 281 | buf[off++] |= 0x4; |
| 282 | off++; /* Skip over Reserved */ |
| 283 | buf[off++] = 4; /* DESIGNATOR LENGTH */ |
| 284 | /* Skip over Obsolete field in RTPI payload |
| 285 | * in Table 472 */ |
| 286 | off += 2; |
| 287 | buf[off++] = ((port->sep_rtpi >> 8) & 0xff); |
| 288 | buf[off++] = (port->sep_rtpi & 0xff); |
| 289 | len += 8; /* Header size + Designation descriptor */ |
| 290 | /* |
| 291 | * Target port group identifier, see spc4r17 |
| 292 | * section 7.7.3.8 |
| 293 | * |
| 294 | * Get the PROTOCOL IDENTIFIER as defined by spc4r17 |
| 295 | * section 7.5.1 Table 362 |
| 296 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 297 | if (dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 298 | goto check_scsi_name; |
| 299 | |
| 300 | tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; |
| 301 | if (!tg_pt_gp_mem) |
| 302 | goto check_lu_gp; |
| 303 | |
| 304 | spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); |
| 305 | tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; |
| 306 | if (!tg_pt_gp) { |
| 307 | spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); |
| 308 | goto check_lu_gp; |
| 309 | } |
| 310 | tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id; |
| 311 | spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); |
| 312 | |
| 313 | buf[off] = |
| 314 | (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4); |
| 315 | buf[off++] |= 0x1; /* CODE SET == Binary */ |
| 316 | buf[off] = 0x80; /* Set PIV=1 */ |
| 317 | /* Set ASSOCIATION == target port: 01b */ |
| 318 | buf[off] |= 0x10; |
| 319 | /* DESIGNATOR TYPE == Target port group identifier */ |
| 320 | buf[off++] |= 0x5; |
| 321 | off++; /* Skip over Reserved */ |
| 322 | buf[off++] = 4; /* DESIGNATOR LENGTH */ |
| 323 | off += 2; /* Skip over Reserved Field */ |
| 324 | buf[off++] = ((tg_pt_gp_id >> 8) & 0xff); |
| 325 | buf[off++] = (tg_pt_gp_id & 0xff); |
| 326 | len += 8; /* Header size + Designation descriptor */ |
| 327 | /* |
| 328 | * Logical Unit Group identifier, see spc4r17 |
| 329 | * section 7.7.3.8 |
| 330 | */ |
| 331 | check_lu_gp: |
| 332 | lu_gp_mem = dev->dev_alua_lu_gp_mem; |
| 333 | if (!lu_gp_mem) |
| 334 | goto check_scsi_name; |
| 335 | |
| 336 | spin_lock(&lu_gp_mem->lu_gp_mem_lock); |
| 337 | lu_gp = lu_gp_mem->lu_gp; |
| 338 | if (!lu_gp) { |
| 339 | spin_unlock(&lu_gp_mem->lu_gp_mem_lock); |
| 340 | goto check_scsi_name; |
| 341 | } |
| 342 | lu_gp_id = lu_gp->lu_gp_id; |
| 343 | spin_unlock(&lu_gp_mem->lu_gp_mem_lock); |
| 344 | |
| 345 | buf[off++] |= 0x1; /* CODE SET == Binary */ |
| 346 | /* DESIGNATOR TYPE == Logical Unit Group identifier */ |
| 347 | buf[off++] |= 0x6; |
| 348 | off++; /* Skip over Reserved */ |
| 349 | buf[off++] = 4; /* DESIGNATOR LENGTH */ |
| 350 | off += 2; /* Skip over Reserved Field */ |
| 351 | buf[off++] = ((lu_gp_id >> 8) & 0xff); |
| 352 | buf[off++] = (lu_gp_id & 0xff); |
| 353 | len += 8; /* Header size + Designation descriptor */ |
| 354 | /* |
| 355 | * SCSI name string designator, see spc4r17 |
| 356 | * section 7.7.3.11 |
| 357 | * |
| 358 | * Get the PROTOCOL IDENTIFIER as defined by spc4r17 |
| 359 | * section 7.5.1 Table 362 |
| 360 | */ |
| 361 | check_scsi_name: |
| 362 | scsi_name_len = strlen(tpg->se_tpg_tfo->tpg_get_wwn(tpg)); |
| 363 | /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */ |
| 364 | scsi_name_len += 10; |
| 365 | /* Check for 4-byte padding */ |
| 366 | padding = ((-scsi_name_len) & 3); |
| 367 | if (padding != 0) |
| 368 | scsi_name_len += padding; |
| 369 | /* Header size + Designation descriptor */ |
| 370 | scsi_name_len += 4; |
| 371 | |
| 372 | buf[off] = |
| 373 | (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4); |
| 374 | buf[off++] |= 0x3; /* CODE SET == UTF-8 */ |
| 375 | buf[off] = 0x80; /* Set PIV=1 */ |
| 376 | /* Set ASSOCIATION == target port: 01b */ |
| 377 | buf[off] |= 0x10; |
| 378 | /* DESIGNATOR TYPE == SCSI name string */ |
| 379 | buf[off++] |= 0x8; |
| 380 | off += 2; /* Skip over Reserved and length */ |
| 381 | /* |
| 382 | * SCSI name string identifer containing, $FABRIC_MOD |
| 383 | * dependent information. For LIO-Target and iSCSI |
| 384 | * Target Port, this means "<iSCSI name>,t,0x<TPGT> in |
| 385 | * UTF-8 encoding. |
| 386 | */ |
| 387 | tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg); |
| 388 | scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x", |
| 389 | tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt); |
| 390 | scsi_name_len += 1 /* Include NULL terminator */; |
| 391 | /* |
| 392 | * The null-terminated, null-padded (see 4.4.2) SCSI |
| 393 | * NAME STRING field contains a UTF-8 format string. |
| 394 | * The number of bytes in the SCSI NAME STRING field |
| 395 | * (i.e., the value in the DESIGNATOR LENGTH field) |
| 396 | * shall be no larger than 256 and shall be a multiple |
| 397 | * of four. |
| 398 | */ |
| 399 | if (padding) |
| 400 | scsi_name_len += padding; |
| 401 | |
| 402 | buf[off-1] = scsi_name_len; |
| 403 | off += scsi_name_len; |
| 404 | /* Header size + Designation descriptor */ |
| 405 | len += (scsi_name_len + 4); |
| 406 | } |
| 407 | buf[2] = ((len >> 8) & 0xff); |
| 408 | buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */ |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | /* Extended INQUIRY Data VPD Page */ |
| 413 | static int spc_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf) |
| 414 | { |
| 415 | buf[3] = 0x3c; |
| 416 | /* Set HEADSUP, ORDSUP, SIMPSUP */ |
| 417 | buf[5] = 0x07; |
| 418 | |
| 419 | /* If WriteCache emulation is enabled, set V_SUP */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 420 | if (cmd->se_dev->dev_attrib.emulate_write_cache > 0) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 421 | buf[6] = 0x01; |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | /* Block Limits VPD page */ |
| 426 | static int spc_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf) |
| 427 | { |
| 428 | struct se_device *dev = cmd->se_dev; |
| 429 | u32 max_sectors; |
| 430 | int have_tp = 0; |
| 431 | |
| 432 | /* |
| 433 | * Following spc3r22 section 6.5.3 Block Limits VPD page, when |
| 434 | * emulate_tpu=1 or emulate_tpws=1 we will be expect a |
| 435 | * different page length for Thin Provisioning. |
| 436 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 437 | if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 438 | have_tp = 1; |
| 439 | |
| 440 | buf[0] = dev->transport->get_device_type(dev); |
| 441 | buf[3] = have_tp ? 0x3c : 0x10; |
| 442 | |
| 443 | /* Set WSNZ to 1 */ |
| 444 | buf[4] = 0x01; |
| 445 | |
| 446 | /* |
| 447 | * Set OPTIMAL TRANSFER LENGTH GRANULARITY |
| 448 | */ |
| 449 | put_unaligned_be16(1, &buf[6]); |
| 450 | |
| 451 | /* |
| 452 | * Set MAXIMUM TRANSFER LENGTH |
| 453 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 454 | max_sectors = min(dev->dev_attrib.fabric_max_sectors, |
| 455 | dev->dev_attrib.hw_max_sectors); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 456 | put_unaligned_be32(max_sectors, &buf[8]); |
| 457 | |
| 458 | /* |
| 459 | * Set OPTIMAL TRANSFER LENGTH |
| 460 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 461 | put_unaligned_be32(dev->dev_attrib.optimal_sectors, &buf[12]); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 462 | |
| 463 | /* |
| 464 | * Exit now if we don't support TP. |
| 465 | */ |
| 466 | if (!have_tp) |
| 467 | return 0; |
| 468 | |
| 469 | /* |
| 470 | * Set MAXIMUM UNMAP LBA COUNT |
| 471 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 472 | put_unaligned_be32(dev->dev_attrib.max_unmap_lba_count, &buf[20]); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 473 | |
| 474 | /* |
| 475 | * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT |
| 476 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 477 | put_unaligned_be32(dev->dev_attrib.max_unmap_block_desc_count, |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 478 | &buf[24]); |
| 479 | |
| 480 | /* |
| 481 | * Set OPTIMAL UNMAP GRANULARITY |
| 482 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 483 | put_unaligned_be32(dev->dev_attrib.unmap_granularity, &buf[28]); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 484 | |
| 485 | /* |
| 486 | * UNMAP GRANULARITY ALIGNMENT |
| 487 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 488 | put_unaligned_be32(dev->dev_attrib.unmap_granularity_alignment, |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 489 | &buf[32]); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 490 | if (dev->dev_attrib.unmap_granularity_alignment != 0) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 491 | buf[32] |= 0x80; /* Set the UGAVALID bit */ |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | /* Block Device Characteristics VPD page */ |
| 497 | static int spc_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf) |
| 498 | { |
| 499 | struct se_device *dev = cmd->se_dev; |
| 500 | |
| 501 | buf[0] = dev->transport->get_device_type(dev); |
| 502 | buf[3] = 0x3c; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 503 | buf[5] = dev->dev_attrib.is_nonrot ? 1 : 0; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | /* Thin Provisioning VPD */ |
| 509 | static int spc_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf) |
| 510 | { |
| 511 | struct se_device *dev = cmd->se_dev; |
| 512 | |
| 513 | /* |
| 514 | * From spc3r22 section 6.5.4 Thin Provisioning VPD page: |
| 515 | * |
| 516 | * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to |
| 517 | * zero, then the page length shall be set to 0004h. If the DP bit |
| 518 | * is set to one, then the page length shall be set to the value |
| 519 | * defined in table 162. |
| 520 | */ |
| 521 | buf[0] = dev->transport->get_device_type(dev); |
| 522 | |
| 523 | /* |
| 524 | * Set Hardcoded length mentioned above for DP=0 |
| 525 | */ |
| 526 | put_unaligned_be16(0x0004, &buf[2]); |
| 527 | |
| 528 | /* |
| 529 | * The THRESHOLD EXPONENT field indicates the threshold set size in |
| 530 | * LBAs as a power of 2 (i.e., the threshold set size is equal to |
| 531 | * 2(threshold exponent)). |
| 532 | * |
| 533 | * Note that this is currently set to 0x00 as mkp says it will be |
| 534 | * changing again. We can enable this once it has settled in T10 |
| 535 | * and is actually used by Linux/SCSI ML code. |
| 536 | */ |
| 537 | buf[4] = 0x00; |
| 538 | |
| 539 | /* |
| 540 | * A TPU bit set to one indicates that the device server supports |
| 541 | * the UNMAP command (see 5.25). A TPU bit set to zero indicates |
| 542 | * that the device server does not support the UNMAP command. |
| 543 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 544 | if (dev->dev_attrib.emulate_tpu != 0) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 545 | buf[5] = 0x80; |
| 546 | |
| 547 | /* |
| 548 | * A TPWS bit set to one indicates that the device server supports |
| 549 | * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs. |
| 550 | * A TPWS bit set to zero indicates that the device server does not |
| 551 | * support the use of the WRITE SAME (16) command to unmap LBAs. |
| 552 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 553 | if (dev->dev_attrib.emulate_tpws != 0) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 554 | buf[5] |= 0x40; |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | static int spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf); |
| 560 | |
| 561 | static struct { |
| 562 | uint8_t page; |
| 563 | int (*emulate)(struct se_cmd *, unsigned char *); |
| 564 | } evpd_handlers[] = { |
| 565 | { .page = 0x00, .emulate = spc_emulate_evpd_00 }, |
| 566 | { .page = 0x80, .emulate = spc_emulate_evpd_80 }, |
| 567 | { .page = 0x83, .emulate = spc_emulate_evpd_83 }, |
| 568 | { .page = 0x86, .emulate = spc_emulate_evpd_86 }, |
| 569 | { .page = 0xb0, .emulate = spc_emulate_evpd_b0 }, |
| 570 | { .page = 0xb1, .emulate = spc_emulate_evpd_b1 }, |
| 571 | { .page = 0xb2, .emulate = spc_emulate_evpd_b2 }, |
| 572 | }; |
| 573 | |
| 574 | /* supported vital product data pages */ |
| 575 | static int spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf) |
| 576 | { |
| 577 | int p; |
| 578 | |
| 579 | /* |
| 580 | * Only report the INQUIRY EVPD=1 pages after a valid NAA |
| 581 | * Registered Extended LUN WWN has been set via ConfigFS |
| 582 | * during device creation/restart. |
| 583 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 584 | if (cmd->se_dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) { |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 585 | buf[3] = ARRAY_SIZE(evpd_handlers); |
| 586 | for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) |
| 587 | buf[p + 4] = evpd_handlers[p].page; |
| 588 | } |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static int spc_emulate_inquiry(struct se_cmd *cmd) |
| 594 | { |
| 595 | struct se_device *dev = cmd->se_dev; |
| 596 | struct se_portal_group *tpg = cmd->se_lun->lun_sep->sep_tpg; |
Paolo Bonzini | ffe7b0e | 2012-09-07 17:30:38 +0200 | [diff] [blame] | 597 | unsigned char *rbuf; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 598 | unsigned char *cdb = cmd->t_task_cdb; |
Paolo Bonzini | ffe7b0e | 2012-09-07 17:30:38 +0200 | [diff] [blame] | 599 | unsigned char buf[SE_INQUIRY_BUF]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 600 | int p, ret; |
| 601 | |
Nicholas Bellinger | dea5f09 | 2012-10-31 22:04:26 -0700 | [diff] [blame] | 602 | memset(buf, 0, SE_INQUIRY_BUF); |
| 603 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 604 | if (dev == tpg->tpg_virt_lun0.lun_se_dev) |
| 605 | buf[0] = 0x3f; /* Not connected */ |
| 606 | else |
| 607 | buf[0] = dev->transport->get_device_type(dev); |
| 608 | |
| 609 | if (!(cdb[1] & 0x1)) { |
| 610 | if (cdb[2]) { |
| 611 | pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n", |
| 612 | cdb[2]); |
| 613 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; |
| 614 | ret = -EINVAL; |
| 615 | goto out; |
| 616 | } |
| 617 | |
| 618 | ret = spc_emulate_inquiry_std(cmd, buf); |
| 619 | goto out; |
| 620 | } |
| 621 | |
| 622 | for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) { |
| 623 | if (cdb[2] == evpd_handlers[p].page) { |
| 624 | buf[1] = cdb[2]; |
| 625 | ret = evpd_handlers[p].emulate(cmd, buf); |
| 626 | goto out; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | pr_err("Unknown VPD Code: 0x%02x\n", cdb[2]); |
| 631 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; |
| 632 | ret = -EINVAL; |
| 633 | |
| 634 | out: |
Paolo Bonzini | ffe7b0e | 2012-09-07 17:30:38 +0200 | [diff] [blame] | 635 | rbuf = transport_kmap_data_sg(cmd); |
| 636 | if (rbuf) { |
| 637 | memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length)); |
| 638 | transport_kunmap_data_sg(cmd); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 639 | } |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 640 | |
| 641 | if (!ret) |
| 642 | target_complete_cmd(cmd, GOOD); |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | static int spc_modesense_rwrecovery(unsigned char *p) |
| 647 | { |
| 648 | p[0] = 0x01; |
| 649 | p[1] = 0x0a; |
| 650 | |
| 651 | return 12; |
| 652 | } |
| 653 | |
| 654 | static int spc_modesense_control(struct se_device *dev, unsigned char *p) |
| 655 | { |
| 656 | p[0] = 0x0a; |
| 657 | p[1] = 0x0a; |
| 658 | p[2] = 2; |
| 659 | /* |
| 660 | * From spc4r23, 7.4.7 Control mode page |
| 661 | * |
| 662 | * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies |
| 663 | * restrictions on the algorithm used for reordering commands |
| 664 | * having the SIMPLE task attribute (see SAM-4). |
| 665 | * |
| 666 | * Table 368 -- QUEUE ALGORITHM MODIFIER field |
| 667 | * Code Description |
| 668 | * 0h Restricted reordering |
| 669 | * 1h Unrestricted reordering allowed |
| 670 | * 2h to 7h Reserved |
| 671 | * 8h to Fh Vendor specific |
| 672 | * |
| 673 | * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that |
| 674 | * the device server shall order the processing sequence of commands |
| 675 | * having the SIMPLE task attribute such that data integrity is maintained |
| 676 | * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol |
| 677 | * requests is halted at any time, the final value of all data observable |
| 678 | * on the medium shall be the same as if all the commands had been processed |
| 679 | * with the ORDERED task attribute). |
| 680 | * |
| 681 | * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the |
| 682 | * device server may reorder the processing sequence of commands having the |
| 683 | * SIMPLE task attribute in any manner. Any data integrity exposures related to |
| 684 | * command sequence order shall be explicitly handled by the application client |
| 685 | * through the selection of appropriate ommands and task attributes. |
| 686 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 687 | p[3] = (dev->dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 688 | /* |
| 689 | * From spc4r17, section 7.4.6 Control mode Page |
| 690 | * |
| 691 | * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b |
| 692 | * |
| 693 | * 00b: The logical unit shall clear any unit attention condition |
| 694 | * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION |
| 695 | * status and shall not establish a unit attention condition when a com- |
| 696 | * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT |
| 697 | * status. |
| 698 | * |
| 699 | * 10b: The logical unit shall not clear any unit attention condition |
| 700 | * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION |
| 701 | * status and shall not establish a unit attention condition when |
| 702 | * a command is completed with BUSY, TASK SET FULL, or RESERVATION |
| 703 | * CONFLICT status. |
| 704 | * |
| 705 | * 11b a The logical unit shall not clear any unit attention condition |
| 706 | * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION |
| 707 | * status and shall establish a unit attention condition for the |
| 708 | * initiator port associated with the I_T nexus on which the BUSY, |
| 709 | * TASK SET FULL, or RESERVATION CONFLICT status is being returned. |
| 710 | * Depending on the status, the additional sense code shall be set to |
| 711 | * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS |
| 712 | * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE |
| 713 | * command, a unit attention condition shall be established only once |
| 714 | * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless |
| 715 | * to the number of commands completed with one of those status codes. |
| 716 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 717 | p[4] = (dev->dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 : |
| 718 | (dev->dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 719 | /* |
| 720 | * From spc4r17, section 7.4.6 Control mode Page |
| 721 | * |
| 722 | * Task Aborted Status (TAS) bit set to zero. |
| 723 | * |
| 724 | * A task aborted status (TAS) bit set to zero specifies that aborted |
| 725 | * tasks shall be terminated by the device server without any response |
| 726 | * to the application client. A TAS bit set to one specifies that tasks |
| 727 | * aborted by the actions of an I_T nexus other than the I_T nexus on |
| 728 | * which the command was received shall be completed with TASK ABORTED |
| 729 | * status (see SAM-4). |
| 730 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 731 | p[5] = (dev->dev_attrib.emulate_tas) ? 0x40 : 0x00; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 732 | p[8] = 0xff; |
| 733 | p[9] = 0xff; |
| 734 | p[11] = 30; |
| 735 | |
| 736 | return 12; |
| 737 | } |
| 738 | |
| 739 | static int spc_modesense_caching(struct se_device *dev, unsigned char *p) |
| 740 | { |
| 741 | p[0] = 0x08; |
| 742 | p[1] = 0x12; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 743 | if (dev->dev_attrib.emulate_write_cache > 0) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 744 | p[2] = 0x04; /* Write Cache Enable */ |
| 745 | p[12] = 0x20; /* Disabled Read Ahead */ |
| 746 | |
| 747 | return 20; |
| 748 | } |
| 749 | |
| 750 | static void spc_modesense_write_protect(unsigned char *buf, int type) |
| 751 | { |
| 752 | /* |
| 753 | * I believe that the WP bit (bit 7) in the mode header is the same for |
| 754 | * all device types.. |
| 755 | */ |
| 756 | switch (type) { |
| 757 | case TYPE_DISK: |
| 758 | case TYPE_TAPE: |
| 759 | default: |
| 760 | buf[0] |= 0x80; /* WP bit */ |
| 761 | break; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | static void spc_modesense_dpofua(unsigned char *buf, int type) |
| 766 | { |
| 767 | switch (type) { |
| 768 | case TYPE_DISK: |
| 769 | buf[0] |= 0x10; /* DPOFUA bit */ |
| 770 | break; |
| 771 | default: |
| 772 | break; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | static int spc_emulate_modesense(struct se_cmd *cmd) |
| 777 | { |
| 778 | struct se_device *dev = cmd->se_dev; |
| 779 | char *cdb = cmd->t_task_cdb; |
| 780 | unsigned char *rbuf; |
| 781 | int type = dev->transport->get_device_type(dev); |
| 782 | int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10); |
Paolo Bonzini | 7a3f369 | 2012-09-07 17:30:39 +0200 | [diff] [blame] | 783 | u32 offset = ten ? 8 : 4; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 784 | int length = 0; |
| 785 | unsigned char buf[SE_MODE_PAGE_BUF]; |
| 786 | |
| 787 | memset(buf, 0, SE_MODE_PAGE_BUF); |
| 788 | |
| 789 | switch (cdb[2] & 0x3f) { |
| 790 | case 0x01: |
| 791 | length = spc_modesense_rwrecovery(&buf[offset]); |
| 792 | break; |
| 793 | case 0x08: |
| 794 | length = spc_modesense_caching(dev, &buf[offset]); |
| 795 | break; |
| 796 | case 0x0a: |
| 797 | length = spc_modesense_control(dev, &buf[offset]); |
| 798 | break; |
| 799 | case 0x3f: |
| 800 | length = spc_modesense_rwrecovery(&buf[offset]); |
| 801 | length += spc_modesense_caching(dev, &buf[offset+length]); |
| 802 | length += spc_modesense_control(dev, &buf[offset+length]); |
| 803 | break; |
| 804 | default: |
| 805 | pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n", |
| 806 | cdb[2] & 0x3f, cdb[3]); |
| 807 | cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE; |
| 808 | return -EINVAL; |
| 809 | } |
| 810 | offset += length; |
| 811 | |
| 812 | if (ten) { |
| 813 | offset -= 2; |
| 814 | buf[0] = (offset >> 8) & 0xff; |
| 815 | buf[1] = offset & 0xff; |
Paolo Bonzini | 7a3f369 | 2012-09-07 17:30:39 +0200 | [diff] [blame] | 816 | offset += 2; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 817 | |
| 818 | if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) || |
| 819 | (cmd->se_deve && |
| 820 | (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY))) |
| 821 | spc_modesense_write_protect(&buf[3], type); |
| 822 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 823 | if ((dev->dev_attrib.emulate_write_cache > 0) && |
| 824 | (dev->dev_attrib.emulate_fua_write > 0)) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 825 | spc_modesense_dpofua(&buf[3], type); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 826 | } else { |
| 827 | offset -= 1; |
| 828 | buf[0] = offset & 0xff; |
Paolo Bonzini | 7a3f369 | 2012-09-07 17:30:39 +0200 | [diff] [blame] | 829 | offset += 1; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 830 | |
| 831 | if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) || |
| 832 | (cmd->se_deve && |
| 833 | (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY))) |
| 834 | spc_modesense_write_protect(&buf[2], type); |
| 835 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 836 | if ((dev->dev_attrib.emulate_write_cache > 0) && |
| 837 | (dev->dev_attrib.emulate_fua_write > 0)) |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 838 | spc_modesense_dpofua(&buf[2], type); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | rbuf = transport_kmap_data_sg(cmd); |
Paolo Bonzini | 7a3f369 | 2012-09-07 17:30:39 +0200 | [diff] [blame] | 842 | if (rbuf) { |
| 843 | memcpy(rbuf, buf, min(offset, cmd->data_length)); |
| 844 | transport_kunmap_data_sg(cmd); |
| 845 | } |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 846 | |
| 847 | target_complete_cmd(cmd, GOOD); |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | static int spc_emulate_request_sense(struct se_cmd *cmd) |
| 852 | { |
| 853 | unsigned char *cdb = cmd->t_task_cdb; |
Paolo Bonzini | 32a8811 | 2012-09-07 17:30:36 +0200 | [diff] [blame] | 854 | unsigned char *rbuf; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 855 | u8 ua_asc = 0, ua_ascq = 0; |
Paolo Bonzini | 32a8811 | 2012-09-07 17:30:36 +0200 | [diff] [blame] | 856 | unsigned char buf[SE_SENSE_BUF]; |
| 857 | |
| 858 | memset(buf, 0, SE_SENSE_BUF); |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 859 | |
| 860 | if (cdb[1] & 0x01) { |
| 861 | pr_err("REQUEST_SENSE description emulation not" |
| 862 | " supported\n"); |
| 863 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; |
| 864 | return -ENOSYS; |
| 865 | } |
| 866 | |
Paolo Bonzini | 32a8811 | 2012-09-07 17:30:36 +0200 | [diff] [blame] | 867 | rbuf = transport_kmap_data_sg(cmd); |
| 868 | if (cmd->scsi_sense_reason != 0) { |
| 869 | /* |
| 870 | * Out of memory. We will fail with CHECK CONDITION, so |
| 871 | * we must not clear the unit attention condition. |
| 872 | */ |
| 873 | target_complete_cmd(cmd, CHECK_CONDITION); |
| 874 | return 0; |
| 875 | } else 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] | 876 | /* |
| 877 | * CURRENT ERROR, UNIT ATTENTION |
| 878 | */ |
| 879 | buf[0] = 0x70; |
| 880 | buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION; |
| 881 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 882 | /* |
| 883 | * The Additional Sense Code (ASC) from the UNIT ATTENTION |
| 884 | */ |
| 885 | buf[SPC_ASC_KEY_OFFSET] = ua_asc; |
| 886 | buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq; |
| 887 | buf[7] = 0x0A; |
| 888 | } else { |
| 889 | /* |
| 890 | * CURRENT ERROR, NO SENSE |
| 891 | */ |
| 892 | buf[0] = 0x70; |
| 893 | buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE; |
| 894 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 895 | /* |
| 896 | * NO ADDITIONAL SENSE INFORMATION |
| 897 | */ |
| 898 | buf[SPC_ASC_KEY_OFFSET] = 0x00; |
| 899 | buf[7] = 0x0A; |
| 900 | } |
| 901 | |
Paolo Bonzini | 32a8811 | 2012-09-07 17:30:36 +0200 | [diff] [blame] | 902 | if (rbuf) { |
| 903 | memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length)); |
| 904 | transport_kunmap_data_sg(cmd); |
| 905 | } |
| 906 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 907 | target_complete_cmd(cmd, GOOD); |
| 908 | return 0; |
| 909 | } |
| 910 | |
Christoph Hellwig | 8de530a | 2012-10-07 10:55:52 -0400 | [diff] [blame] | 911 | int spc_emulate_report_luns(struct se_cmd *cmd) |
Christoph Hellwig | d1b1f80 | 2012-10-07 10:55:51 -0400 | [diff] [blame] | 912 | { |
| 913 | struct se_dev_entry *deve; |
| 914 | struct se_session *sess = cmd->se_sess; |
| 915 | unsigned char *buf; |
| 916 | u32 lun_count = 0, offset = 8, i; |
| 917 | |
| 918 | if (cmd->data_length < 16) { |
| 919 | pr_warn("REPORT LUNS allocation length %u too small\n", |
| 920 | cmd->data_length); |
| 921 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; |
| 922 | return -EINVAL; |
| 923 | } |
| 924 | |
| 925 | buf = transport_kmap_data_sg(cmd); |
| 926 | if (!buf) |
| 927 | return -ENOMEM; |
| 928 | |
| 929 | /* |
| 930 | * If no struct se_session pointer is present, this struct se_cmd is |
| 931 | * coming via a target_core_mod PASSTHROUGH op, and not through |
| 932 | * a $FABRIC_MOD. In that case, report LUN=0 only. |
| 933 | */ |
| 934 | if (!sess) { |
| 935 | int_to_scsilun(0, (struct scsi_lun *)&buf[offset]); |
| 936 | lun_count = 1; |
| 937 | goto done; |
| 938 | } |
| 939 | |
| 940 | spin_lock_irq(&sess->se_node_acl->device_list_lock); |
| 941 | for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) { |
| 942 | deve = sess->se_node_acl->device_list[i]; |
| 943 | if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)) |
| 944 | continue; |
| 945 | /* |
| 946 | * We determine the correct LUN LIST LENGTH even once we |
| 947 | * have reached the initial allocation length. |
| 948 | * See SPC2-R20 7.19. |
| 949 | */ |
| 950 | lun_count++; |
| 951 | if ((offset + 8) > cmd->data_length) |
| 952 | continue; |
| 953 | |
| 954 | int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]); |
| 955 | offset += 8; |
| 956 | } |
| 957 | spin_unlock_irq(&sess->se_node_acl->device_list_lock); |
| 958 | |
| 959 | /* |
| 960 | * See SPC3 r07, page 159. |
| 961 | */ |
| 962 | done: |
| 963 | lun_count *= 8; |
| 964 | buf[0] = ((lun_count >> 24) & 0xff); |
| 965 | buf[1] = ((lun_count >> 16) & 0xff); |
| 966 | buf[2] = ((lun_count >> 8) & 0xff); |
| 967 | buf[3] = (lun_count & 0xff); |
| 968 | transport_kunmap_data_sg(cmd); |
| 969 | |
| 970 | target_complete_cmd(cmd, GOOD); |
| 971 | return 0; |
| 972 | } |
Christoph Hellwig | 8de530a | 2012-10-07 10:55:52 -0400 | [diff] [blame] | 973 | EXPORT_SYMBOL(spc_emulate_report_luns); |
Christoph Hellwig | d1b1f80 | 2012-10-07 10:55:51 -0400 | [diff] [blame] | 974 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 975 | static int spc_emulate_testunitready(struct se_cmd *cmd) |
| 976 | { |
| 977 | target_complete_cmd(cmd, GOOD); |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | int spc_parse_cdb(struct se_cmd *cmd, unsigned int *size) |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 982 | { |
Nicholas Bellinger | eba2ca4 | 2012-05-30 14:09:10 -0700 | [diff] [blame] | 983 | struct se_device *dev = cmd->se_dev; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 984 | unsigned char *cdb = cmd->t_task_cdb; |
| 985 | |
| 986 | switch (cdb[0]) { |
| 987 | case MODE_SELECT: |
| 988 | *size = cdb[4]; |
| 989 | break; |
| 990 | case MODE_SELECT_10: |
| 991 | *size = (cdb[7] << 8) + cdb[8]; |
| 992 | break; |
| 993 | case MODE_SENSE: |
| 994 | *size = cdb[4]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 995 | cmd->execute_cmd = spc_emulate_modesense; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 996 | break; |
| 997 | case MODE_SENSE_10: |
| 998 | *size = (cdb[7] << 8) + cdb[8]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 999 | cmd->execute_cmd = spc_emulate_modesense; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1000 | break; |
| 1001 | case LOG_SELECT: |
| 1002 | case LOG_SENSE: |
| 1003 | *size = (cdb[7] << 8) + cdb[8]; |
| 1004 | break; |
| 1005 | case PERSISTENT_RESERVE_IN: |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1006 | *size = (cdb[7] << 8) + cdb[8]; |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame^] | 1007 | cmd->execute_cmd = target_scsi3_emulate_pr_in; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1008 | break; |
| 1009 | case PERSISTENT_RESERVE_OUT: |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1010 | *size = (cdb[7] << 8) + cdb[8]; |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame^] | 1011 | cmd->execute_cmd = target_scsi3_emulate_pr_out; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1012 | break; |
| 1013 | case RELEASE: |
| 1014 | case RELEASE_10: |
| 1015 | if (cdb[0] == RELEASE_10) |
| 1016 | *size = (cdb[7] << 8) | cdb[8]; |
| 1017 | else |
| 1018 | *size = cmd->data_length; |
| 1019 | |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame^] | 1020 | cmd->execute_cmd = target_scsi2_reservation_release; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1021 | break; |
| 1022 | case RESERVE: |
| 1023 | case RESERVE_10: |
| 1024 | /* |
| 1025 | * The SPC-2 RESERVE does not contain a size in the SCSI CDB. |
| 1026 | * Assume the passthrough or $FABRIC_MOD will tell us about it. |
| 1027 | */ |
| 1028 | if (cdb[0] == RESERVE_10) |
| 1029 | *size = (cdb[7] << 8) | cdb[8]; |
| 1030 | else |
| 1031 | *size = cmd->data_length; |
| 1032 | |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame^] | 1033 | cmd->execute_cmd = target_scsi2_reservation_reserve; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1034 | break; |
| 1035 | case REQUEST_SENSE: |
| 1036 | *size = cdb[4]; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1037 | cmd->execute_cmd = spc_emulate_request_sense; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1038 | break; |
| 1039 | case INQUIRY: |
| 1040 | *size = (cdb[3] << 8) + cdb[4]; |
| 1041 | |
| 1042 | /* |
| 1043 | * Do implict HEAD_OF_QUEUE processing for INQUIRY. |
| 1044 | * See spc4r17 section 5.3 |
| 1045 | */ |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 1046 | cmd->sam_task_attr = MSG_HEAD_TAG; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1047 | cmd->execute_cmd = spc_emulate_inquiry; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1048 | break; |
| 1049 | case SECURITY_PROTOCOL_IN: |
| 1050 | case SECURITY_PROTOCOL_OUT: |
| 1051 | *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; |
| 1052 | break; |
| 1053 | case EXTENDED_COPY: |
| 1054 | case READ_ATTRIBUTE: |
| 1055 | case RECEIVE_COPY_RESULTS: |
| 1056 | case WRITE_ATTRIBUTE: |
| 1057 | *size = (cdb[10] << 24) | (cdb[11] << 16) | |
| 1058 | (cdb[12] << 8) | cdb[13]; |
| 1059 | break; |
| 1060 | case RECEIVE_DIAGNOSTIC: |
| 1061 | case SEND_DIAGNOSTIC: |
| 1062 | *size = (cdb[3] << 8) | cdb[4]; |
| 1063 | break; |
| 1064 | case WRITE_BUFFER: |
| 1065 | *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8]; |
| 1066 | break; |
| 1067 | case REPORT_LUNS: |
Christoph Hellwig | d1b1f80 | 2012-10-07 10:55:51 -0400 | [diff] [blame] | 1068 | cmd->execute_cmd = spc_emulate_report_luns; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1069 | *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; |
| 1070 | /* |
| 1071 | * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS |
| 1072 | * See spc4r17 section 5.3 |
| 1073 | */ |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 1074 | cmd->sam_task_attr = MSG_HEAD_TAG; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1075 | break; |
| 1076 | case TEST_UNIT_READY: |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1077 | cmd->execute_cmd = spc_emulate_testunitready; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1078 | *size = 0; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1079 | break; |
Nicholas Bellinger | eba2ca4 | 2012-05-30 14:09:10 -0700 | [diff] [blame] | 1080 | case MAINTENANCE_IN: |
| 1081 | if (dev->transport->get_device_type(dev) != TYPE_ROM) { |
| 1082 | /* |
| 1083 | * MAINTENANCE_IN from SCC-2 |
| 1084 | * Check for emulated MI_REPORT_TARGET_PGS |
| 1085 | */ |
| 1086 | if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS && |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1087 | dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) { |
Nicholas Bellinger | eba2ca4 | 2012-05-30 14:09:10 -0700 | [diff] [blame] | 1088 | cmd->execute_cmd = |
| 1089 | target_emulate_report_target_port_groups; |
| 1090 | } |
| 1091 | *size = get_unaligned_be32(&cdb[6]); |
| 1092 | } else { |
| 1093 | /* |
| 1094 | * GPCMD_SEND_KEY from multi media commands |
| 1095 | */ |
| 1096 | *size = get_unaligned_be16(&cdb[8]); |
| 1097 | } |
| 1098 | break; |
| 1099 | case MAINTENANCE_OUT: |
| 1100 | if (dev->transport->get_device_type(dev) != TYPE_ROM) { |
| 1101 | /* |
| 1102 | * MAINTENANCE_OUT from SCC-2 |
| 1103 | * Check for emulated MO_SET_TARGET_PGS. |
| 1104 | */ |
| 1105 | if (cdb[1] == MO_SET_TARGET_PGS && |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1106 | dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) { |
Nicholas Bellinger | eba2ca4 | 2012-05-30 14:09:10 -0700 | [diff] [blame] | 1107 | cmd->execute_cmd = |
| 1108 | target_emulate_set_target_port_groups; |
| 1109 | } |
| 1110 | *size = get_unaligned_be32(&cdb[6]); |
| 1111 | } else { |
| 1112 | /* |
| 1113 | * GPCMD_SEND_KEY from multi media commands |
| 1114 | */ |
| 1115 | *size = get_unaligned_be16(&cdb[8]); |
| 1116 | } |
| 1117 | break; |
Christoph Hellwig | 88455ec | 2012-05-20 11:59:13 -0400 | [diff] [blame] | 1118 | default: |
| 1119 | pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode" |
| 1120 | " 0x%02x, sending CHECK_CONDITION.\n", |
| 1121 | cmd->se_tfo->get_fabric_name(), cdb[0]); |
| 1122 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 1123 | cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE; |
| 1124 | return -EINVAL; |
| 1125 | } |
| 1126 | |
| 1127 | return 0; |
| 1128 | } |
| 1129 | EXPORT_SYMBOL(spc_parse_cdb); |