1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
6 * This work is largely based on the "vhost-user-scsi" implementation by
7 * SPDK(https://github.com/spdk/spdk).
18 #include <rte_atomic.h>
19 #include <rte_cycles.h>
21 #include <rte_malloc.h>
22 #include <rte_byteorder.h>
23 #include <rte_string_fns.h>
25 #include "vhost_scsi.h"
26 #include "scsi_spec.h"
28 #define INQ_OFFSET(field) (offsetof(struct scsi_cdb_inquiry_data, field) + \
29 sizeof(((struct scsi_cdb_inquiry_data *)0x0)->field))
32 vhost_strcpy_pad(void *dst, const char *src, size_t size, int pad)
38 memcpy(dst, src, len);
39 memset((char *)dst + len, pad, size - len);
41 memcpy(dst, src, size);
46 vhost_hex2bin(char ch)
48 if ((ch >= '0') && (ch <= '9'))
51 if ((ch >= 'a') && (ch <= 'f'))
57 vhost_bdev_scsi_set_naa_ieee_extended(const char *name, uint8_t *buf)
59 int i, value, count = 0;
60 uint64_t *temp64, local_value;
62 for (i = 0; (i < 16) && (name[i] != '\0'); i++) {
63 value = vhost_hex2bin(name[i]);
65 buf[count++] |= value << 4;
70 local_value = *(uint64_t *)buf;
72 * see spc3r23 7.6.3.6.2,
73 * NAA IEEE Extended identifer format
75 local_value &= 0x0fff000000ffffffull;
76 /* NAA 02, and 00 03 47 for IEEE Intel */
77 local_value |= 0x2000000347000000ull;
79 temp64 = (uint64_t *)buf;
80 *temp64 = rte_cpu_to_be_64(local_value);
84 scsi_task_build_sense_data(struct vhost_scsi_task *task, int sk,
90 resp_code = 0x70; /* Current + Fixed format */
93 cp = (uint8_t *)task->resp->sense;
95 /* VALID(7) RESPONSE CODE(6-0) */
96 cp[0] = 0x80 | resp_code;
99 /* FILEMARK(7) EOM(6) ILI(5) SENSE KEY(3-0) */
102 memset(&cp[3], 0, 4);
104 /* ADDITIONAL SENSE LENGTH */
107 /* COMMAND-SPECIFIC INFORMATION */
108 memset(&cp[8], 0, 4);
109 /* ADDITIONAL SENSE CODE */
111 /* ADDITIONAL SENSE CODE QUALIFIER */
113 /* FIELD REPLACEABLE UNIT CODE */
116 /* SKSV(7) SENSE KEY SPECIFIC(6-0,7-0,7-0) */
122 task->resp->sense_len = 18;
126 scsi_task_set_status(struct vhost_scsi_task *task, int sc, int sk,
129 if (sc == SCSI_STATUS_CHECK_CONDITION)
130 scsi_task_build_sense_data(task, sk, asc, ascq);
131 task->resp->status = sc;
135 vhost_bdev_scsi_inquiry_command(struct vhost_block_dev *bdev,
136 struct vhost_scsi_task *task)
139 uint32_t alloc_len = 0;
147 struct scsi_cdb_inquiry *inq;
149 inq = (struct scsi_cdb_inquiry *)task->req->cdb;
151 assert(task->iovs_cnt == 1);
153 /* At least 36Bytes for inquiry command */
154 if (task->data_len < 0x24)
157 pd = SPC_PERIPHERAL_DEVICE_TYPE_DISK;
159 evpd = inq->evpd & 0x1;
165 struct scsi_vpd_page *vpage = (struct scsi_vpd_page *)
166 task->iovs[0].iov_base;
168 /* PERIPHERAL QUALIFIER(7-5) PERIPHERAL DEVICE TYPE(4-0) */
169 vpage->peripheral = pd;
171 vpage->page_code = pc;
174 case SPC_VPD_SUPPORTED_VPD_PAGES:
176 vpage->params[0] = SPC_VPD_SUPPORTED_VPD_PAGES;
177 vpage->params[1] = SPC_VPD_UNIT_SERIAL_NUMBER;
178 vpage->params[2] = SPC_VPD_DEVICE_IDENTIFICATION;
181 vpage->alloc_len = rte_cpu_to_be_16(len);
183 case SPC_VPD_UNIT_SERIAL_NUMBER:
185 strlcpy((char *)vpage->params, bdev->name,
186 sizeof(vpage->params));
187 vpage->alloc_len = rte_cpu_to_be_16(32);
189 case SPC_VPD_DEVICE_IDENTIFICATION:
191 struct scsi_desig_desc *desig;
195 desig = (struct scsi_desig_desc *)buf;
196 desig->code_set = SPC_VPD_CODE_SET_BINARY;
197 desig->protocol_id = SPC_PROTOCOL_IDENTIFIER_ISCSI;
198 desig->type = SPC_VPD_IDENTIFIER_TYPE_NAA;
199 desig->association = SPC_VPD_ASSOCIATION_LOGICAL_UNIT;
200 desig->reserved0 = 0;
202 desig->reserved1 = 0;
204 vhost_bdev_scsi_set_naa_ieee_extended(bdev->name,
206 len = sizeof(struct scsi_desig_desc) + 8;
208 buf += sizeof(struct scsi_desig_desc) + desig->len;
210 /* T10 Vendor ID designator */
211 desig = (struct scsi_desig_desc *)buf;
212 desig->code_set = SPC_VPD_CODE_SET_ASCII;
213 desig->protocol_id = SPC_PROTOCOL_IDENTIFIER_ISCSI;
214 desig->type = SPC_VPD_IDENTIFIER_TYPE_T10_VENDOR_ID;
215 desig->association = SPC_VPD_ASSOCIATION_LOGICAL_UNIT;
216 desig->reserved0 = 0;
218 desig->reserved1 = 0;
219 desig->len = 8 + 16 + 32;
220 strlcpy((char *)desig->desig, "INTEL", 8);
221 vhost_strcpy_pad((char *)&desig->desig[8],
222 bdev->product_name, 16, ' ');
223 strlcpy((char *)&desig->desig[24], bdev->name, 32);
224 len += sizeof(struct scsi_desig_desc) + 8 + 16 + 32;
226 buf += sizeof(struct scsi_desig_desc) + desig->len;
228 /* SCSI Device Name designator */
229 desig = (struct scsi_desig_desc *)buf;
230 desig->code_set = SPC_VPD_CODE_SET_UTF8;
231 desig->protocol_id = SPC_PROTOCOL_IDENTIFIER_ISCSI;
232 desig->type = SPC_VPD_IDENTIFIER_TYPE_SCSI_NAME;
233 desig->association = SPC_VPD_ASSOCIATION_TARGET_DEVICE;
234 desig->reserved0 = 0;
236 desig->reserved1 = 0;
237 desig->len = snprintf((char *)desig->desig,
238 255, "%s", bdev->name);
239 len += sizeof(struct scsi_desig_desc) + desig->len;
241 buf += sizeof(struct scsi_desig_desc) + desig->len;
242 vpage->alloc_len = rte_cpu_to_be_16(len);
249 struct scsi_cdb_inquiry_data *inqdata =
250 (struct scsi_cdb_inquiry_data *)task->iovs[0].iov_base;
251 /* Standard INQUIRY data */
252 /* PERIPHERAL QUALIFIER(7-5) PERIPHERAL DEVICE TYPE(4-0) */
253 inqdata->peripheral = pd;
259 /* See SPC3/SBC2/MMC4/SAM2 for more details */
260 inqdata->version = SPC_VERSION_SPC3;
262 /* NORMACA(5) HISUP(4) RESPONSE DATA FORMAT(3-0) */
263 /* format 2 */ /* hierarchical support */
264 inqdata->response = 2 | 1 << 4;
268 /* SCCS(7) ACC(6) TPGS(5-4) 3PC(3) PROTECT(0) */
269 /* Not support TPGS */
273 inqdata->flags2 = 0x10;
275 /* WBUS16(5) SYNC(4) LINKED(3) CMDQUE(1) VS(0) */
277 inqdata->flags3 = 0x2;
279 /* T10 VENDOR IDENTIFICATION */
280 strlcpy((char *)inqdata->t10_vendor_id, "INTEL",
281 sizeof(inqdata->t10_vendor_id));
283 /* PRODUCT IDENTIFICATION */
284 snprintf((char *)inqdata->product_id,
285 RTE_DIM(inqdata->product_id), "%s",
288 /* PRODUCT REVISION LEVEL */
289 strlcpy((char *)inqdata->product_rev, "0001",
290 sizeof(inqdata->product_rev));
292 /* Standard inquiry data ends here. Only populate
293 * remaining fields if alloc_len indicates enough
296 len = INQ_OFFSET(product_rev) - 5;
298 if (alloc_len >= INQ_OFFSET(vendor)) {
299 /* Vendor specific */
300 memset(inqdata->vendor, 0x20, 20);
301 len += sizeof(inqdata->vendor);
304 if (alloc_len >= INQ_OFFSET(ius)) {
305 /* CLOCKING(3-2) QAS(1) IUS(0) */
307 len += sizeof(inqdata->ius);
310 if (alloc_len >= INQ_OFFSET(reserved)) {
312 inqdata->reserved = 0;
313 len += sizeof(inqdata->reserved);
316 /* VERSION DESCRIPTOR 1-8 */
317 if (alloc_len >= INQ_OFFSET(reserved) + 2) {
318 temp16 = (uint16_t *)&inqdata->desc[0];
319 *temp16 = rte_cpu_to_be_16(0x0960);
323 if (alloc_len >= INQ_OFFSET(reserved) + 4) {
324 /* SPC-3 (no version claimed) */
325 temp16 = (uint16_t *)&inqdata->desc[2];
326 *temp16 = rte_cpu_to_be_16(0x0300);
330 if (alloc_len >= INQ_OFFSET(reserved) + 6) {
331 /* SBC-2 (no version claimed) */
332 temp16 = (uint16_t *)&inqdata->desc[4];
333 *temp16 = rte_cpu_to_be_16(0x0320);
337 if (alloc_len >= INQ_OFFSET(reserved) + 8) {
338 /* SAM-2 (no version claimed) */
339 temp16 = (uint16_t *)&inqdata->desc[6];
340 *temp16 = rte_cpu_to_be_16(0x0040);
344 if (alloc_len > INQ_OFFSET(reserved) + 8) {
345 i = alloc_len - (INQ_OFFSET(reserved) + 8);
348 memset(&inqdata->desc[8], 0, i);
352 /* ADDITIONAL LENGTH */
353 inqdata->add_len = len;
357 scsi_task_set_status(task, SCSI_STATUS_GOOD, 0, 0, 0);
361 scsi_task_set_status(task, SCSI_STATUS_CHECK_CONDITION,
362 SCSI_SENSE_ILLEGAL_REQUEST,
363 SCSI_ASC_INVALID_FIELD_IN_CDB,
364 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
369 vhost_bdev_scsi_readwrite(struct vhost_block_dev *bdev,
370 struct vhost_scsi_task *task,
371 uint64_t lba, __rte_unused uint32_t xfer_len)
377 offset = lba * bdev->blocklen;
379 for (i = 0; i < task->iovs_cnt; i++) {
380 if (task->dxfer_dir == SCSI_DIR_TO_DEV)
381 memcpy(bdev->data + offset, task->iovs[i].iov_base,
382 task->iovs[i].iov_len);
384 memcpy(task->iovs[i].iov_base, bdev->data + offset,
385 task->iovs[i].iov_len);
386 offset += task->iovs[i].iov_len;
387 nbytes += task->iovs[i].iov_len;
394 vhost_bdev_scsi_process_block(struct vhost_block_dev *bdev,
395 struct vhost_scsi_task *task)
397 uint64_t lba, *temp64;
398 uint32_t xfer_len, *temp32;
400 uint8_t *cdb = (uint8_t *)task->req->cdb;
405 lba = (uint64_t)cdb[1] << 16;
406 lba |= (uint64_t)cdb[2] << 8;
407 lba |= (uint64_t)cdb[3];
411 return vhost_bdev_scsi_readwrite(bdev, task, lba, xfer_len);
415 temp32 = (uint32_t *)&cdb[2];
416 lba = rte_be_to_cpu_32(*temp32);
417 temp16 = (uint16_t *)&cdb[7];
418 xfer_len = rte_be_to_cpu_16(*temp16);
419 return vhost_bdev_scsi_readwrite(bdev, task, lba, xfer_len);
423 temp32 = (uint32_t *)&cdb[2];
424 lba = rte_be_to_cpu_32(*temp32);
425 temp32 = (uint32_t *)&cdb[6];
426 xfer_len = rte_be_to_cpu_32(*temp32);
427 return vhost_bdev_scsi_readwrite(bdev, task, lba, xfer_len);
431 temp64 = (uint64_t *)&cdb[2];
432 lba = rte_be_to_cpu_64(*temp64);
433 temp32 = (uint32_t *)&cdb[10];
434 xfer_len = rte_be_to_cpu_32(*temp32);
435 return vhost_bdev_scsi_readwrite(bdev, task, lba, xfer_len);
437 case SBC_READ_CAPACITY_10: {
440 if (bdev->blockcnt - 1 > 0xffffffffULL)
441 memset(buffer, 0xff, 4);
443 temp32 = (uint32_t *)buffer;
444 *temp32 = rte_cpu_to_be_32(bdev->blockcnt - 1);
446 temp32 = (uint32_t *)&buffer[4];
447 *temp32 = rte_cpu_to_be_32(bdev->blocklen);
448 memcpy(task->iovs[0].iov_base, buffer, sizeof(buffer));
449 task->resp->status = SCSI_STATUS_GOOD;
450 return sizeof(buffer);
453 case SBC_SYNCHRONIZE_CACHE_10:
454 case SBC_SYNCHRONIZE_CACHE_16:
455 task->resp->status = SCSI_STATUS_GOOD;
459 scsi_task_set_status(task, SCSI_STATUS_CHECK_CONDITION,
460 SCSI_SENSE_ILLEGAL_REQUEST,
461 SCSI_ASC_INVALID_FIELD_IN_CDB,
462 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
467 vhost_bdev_process_scsi_commands(struct vhost_block_dev *bdev,
468 struct vhost_scsi_task *task)
472 uint64_t *temp64, fmt_lun = 0;
475 uint8_t *cdb = (uint8_t *)task->req->cdb;
477 lun = (const uint8_t *)task->req->lun;
478 /* only 1 LUN supported */
479 if (lun[0] != 1 || lun[1] >= 1)
484 len = vhost_bdev_scsi_inquiry_command(bdev, task);
485 task->data_len = len;
487 case SPC_REPORT_LUNS:
488 data = (uint8_t *)task->iovs[0].iov_base;
489 fmt_lun |= (0x0ULL & 0x00ffULL) << 48;
490 temp64 = (uint64_t *)&data[8];
491 *temp64 = rte_cpu_to_be_64(fmt_lun);
492 temp32 = (uint32_t *)data;
493 *temp32 = rte_cpu_to_be_32(8);
495 scsi_task_set_status(task, SCSI_STATUS_GOOD, 0, 0, 0);
497 case SPC_MODE_SELECT_6:
498 case SPC_MODE_SELECT_10:
499 /* don't support it now */
500 scsi_task_set_status(task, SCSI_STATUS_GOOD, 0, 0, 0);
502 case SPC_MODE_SENSE_6:
503 case SPC_MODE_SENSE_10:
504 /* don't support it now */
505 scsi_task_set_status(task, SCSI_STATUS_GOOD, 0, 0, 0);
507 case SPC_TEST_UNIT_READY:
508 scsi_task_set_status(task, SCSI_STATUS_GOOD, 0, 0, 0);
511 len = vhost_bdev_scsi_process_block(bdev, task);
512 task->data_len = len;