power: fix frequency list buffer validation
[dpdk.git] / examples / vhost_scsi / scsi.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 /**
6  * This work is largely based on the "vhost-user-scsi" implementation by
7  * SPDK(https://github.com/spdk/spdk).
8  */
9
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <unistd.h>
13 #include <assert.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <stddef.h>
17
18 #include <rte_atomic.h>
19 #include <rte_cycles.h>
20 #include <rte_log.h>
21 #include <rte_malloc.h>
22 #include <rte_byteorder.h>
23 #include <rte_string_fns.h>
24
25 #include "vhost_scsi.h"
26 #include "scsi_spec.h"
27
28 #define INQ_OFFSET(field) (offsetof(struct scsi_cdb_inquiry_data, field) + \
29                           sizeof(((struct scsi_cdb_inquiry_data *)0x0)->field))
30
31 static void
32 vhost_strcpy_pad(void *dst, const char *src, size_t size, int pad)
33 {
34         size_t len;
35
36         len = strlen(src);
37         if (len < size) {
38                 memcpy(dst, src, len);
39                 memset((char *)dst + len, pad, size - len);
40         } else {
41                 memcpy(dst, src, size);
42         }
43 }
44
45 static int
46 vhost_hex2bin(char ch)
47 {
48         if ((ch >= '0') && (ch <= '9'))
49                 return ch - '0';
50         ch = tolower(ch);
51         if ((ch >= 'a') && (ch <= 'f'))
52                 return ch - 'a' + 10;
53         return (int)ch;
54 }
55
56 static void
57 vhost_bdev_scsi_set_naa_ieee_extended(const char *name, uint8_t *buf)
58 {
59         int i, value, count = 0;
60         uint64_t *temp64, local_value;
61
62         for (i = 0; (i < 16) && (name[i] != '\0'); i++) {
63                 value = vhost_hex2bin(name[i]);
64                 if (i % 2)
65                         buf[count++] |= value << 4;
66                 else
67                         buf[count] = value;
68         }
69
70         local_value = *(uint64_t *)buf;
71         /*
72          * see spc3r23 7.6.3.6.2,
73          *  NAA IEEE Extended identifer format
74          */
75         local_value &= 0x0fff000000ffffffull;
76         /* NAA 02, and 00 03 47 for IEEE Intel */
77         local_value |= 0x2000000347000000ull;
78
79         temp64 = (uint64_t *)buf;
80         *temp64 = rte_cpu_to_be_64(local_value);
81 }
82
83 static void
84 scsi_task_build_sense_data(struct vhost_scsi_task *task, int sk,
85                            int asc, int ascq)
86 {
87         uint8_t *cp;
88         int resp_code;
89
90         resp_code = 0x70; /* Current + Fixed format */
91
92         /* Sense Data */
93         cp = (uint8_t *)task->resp->sense;
94
95         /* VALID(7) RESPONSE CODE(6-0) */
96         cp[0] = 0x80 | resp_code;
97         /* Obsolete */
98         cp[1] = 0;
99         /* FILEMARK(7) EOM(6) ILI(5) SENSE KEY(3-0) */
100         cp[2] = sk & 0xf;
101         /* INFORMATION */
102         memset(&cp[3], 0, 4);
103
104         /* ADDITIONAL SENSE LENGTH */
105         cp[7] = 10;
106
107         /* COMMAND-SPECIFIC INFORMATION */
108         memset(&cp[8], 0, 4);
109         /* ADDITIONAL SENSE CODE */
110         cp[12] = asc;
111         /* ADDITIONAL SENSE CODE QUALIFIER */
112         cp[13] = ascq;
113         /* FIELD REPLACEABLE UNIT CODE */
114         cp[14] = 0;
115
116         /* SKSV(7) SENSE KEY SPECIFIC(6-0,7-0,7-0) */
117         cp[15] = 0;
118         cp[16] = 0;
119         cp[17] = 0;
120
121         /* SenseLength */
122         task->resp->sense_len = 18;
123 }
124
125 static void
126 scsi_task_set_status(struct vhost_scsi_task *task, int sc, int sk,
127                      int asc, int ascq)
128 {
129         if (sc == SCSI_STATUS_CHECK_CONDITION)
130                 scsi_task_build_sense_data(task, sk, asc, ascq);
131         task->resp->status = sc;
132 }
133
134 static int
135 vhost_bdev_scsi_inquiry_command(struct vhost_block_dev *bdev,
136                                 struct vhost_scsi_task *task)
137 {
138         int hlen = 0;
139         uint32_t alloc_len = 0;
140         uint16_t len = 0;
141         uint16_t *temp16;
142         int pc;
143         int pd;
144         int evpd;
145         int i;
146         uint8_t *buf;
147         struct scsi_cdb_inquiry *inq;
148
149         inq = (struct scsi_cdb_inquiry *)task->req->cdb;
150
151         assert(task->iovs_cnt == 1);
152
153         /* At least 36Bytes for inquiry command */
154         if (task->data_len < 0x24)
155                 goto inq_error;
156
157         pd = SPC_PERIPHERAL_DEVICE_TYPE_DISK;
158         pc = inq->page_code;
159         evpd = inq->evpd & 0x1;
160
161         if (!evpd && pc)
162                 goto inq_error;
163
164         if (evpd) {
165                 struct scsi_vpd_page *vpage = (struct scsi_vpd_page *)
166                                               task->iovs[0].iov_base;
167
168                 /* PERIPHERAL QUALIFIER(7-5) PERIPHERAL DEVICE TYPE(4-0) */
169                 vpage->peripheral = pd;
170                 /* PAGE CODE */
171                 vpage->page_code = pc;
172
173                 switch (pc) {
174                 case SPC_VPD_SUPPORTED_VPD_PAGES:
175                         hlen = 4;
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;
179                         len = 3;
180                         /* PAGE LENGTH */
181                         vpage->alloc_len = rte_cpu_to_be_16(len);
182                         break;
183                 case SPC_VPD_UNIT_SERIAL_NUMBER:
184                         hlen = 4;
185                         strlcpy((char *)vpage->params, bdev->name,
186                                         sizeof(vpage->params));
187                         vpage->alloc_len = rte_cpu_to_be_16(32);
188                         break;
189                 case SPC_VPD_DEVICE_IDENTIFICATION:
190                         buf = vpage->params;
191                         struct scsi_desig_desc *desig;
192
193                         hlen = 4;
194                         /* NAA designator */
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;
201                         desig->piv = 1;
202                         desig->reserved1 = 0;
203                         desig->len = 8;
204                         vhost_bdev_scsi_set_naa_ieee_extended(bdev->name,
205                                                               desig->desig);
206                         len = sizeof(struct scsi_desig_desc) + 8;
207
208                         buf += sizeof(struct scsi_desig_desc) + desig->len;
209
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;
217                         desig->piv = 1;
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;
225
226                         buf += sizeof(struct scsi_desig_desc) + desig->len;
227
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;
235                         desig->piv = 1;
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;
240
241                         buf += sizeof(struct scsi_desig_desc) + desig->len;
242                         vpage->alloc_len = rte_cpu_to_be_16(len);
243                         break;
244                 default:
245                         goto inq_error;
246                 }
247
248         } else {
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;
254
255                 /* RMB(7) */
256                 inqdata->rmb = 0;
257
258                 /* VERSION */
259                 /* See SPC3/SBC2/MMC4/SAM2 for more details */
260                 inqdata->version = SPC_VERSION_SPC3;
261
262                 /* NORMACA(5) HISUP(4) RESPONSE DATA FORMAT(3-0) */
263                 /* format 2 */ /* hierarchical support */
264                 inqdata->response = 2 | 1 << 4;
265
266                 hlen = 5;
267
268                 /* SCCS(7) ACC(6) TPGS(5-4) 3PC(3) PROTECT(0) */
269                 /* Not support TPGS */
270                 inqdata->flags = 0;
271
272                 /* MULTIP */
273                 inqdata->flags2 = 0x10;
274
275                 /* WBUS16(5) SYNC(4) LINKED(3) CMDQUE(1) VS(0) */
276                 /* CMDQUE */
277                 inqdata->flags3 = 0x2;
278
279                 /* T10 VENDOR IDENTIFICATION */
280                 strlcpy((char *)inqdata->t10_vendor_id, "INTEL",
281                         sizeof(inqdata->t10_vendor_id));
282
283                 /* PRODUCT IDENTIFICATION */
284                 snprintf((char *)inqdata->product_id,
285                                 RTE_DIM(inqdata->product_id), "%s",
286                                 bdev->product_name);
287
288                 /* PRODUCT REVISION LEVEL */
289                 strlcpy((char *)inqdata->product_rev, "0001",
290                         sizeof(inqdata->product_rev));
291
292                 /* Standard inquiry data ends here. Only populate
293                  * remaining fields if alloc_len indicates enough
294                  * space to hold it.
295                  */
296                 len = INQ_OFFSET(product_rev) - 5;
297
298                 if (alloc_len >= INQ_OFFSET(vendor)) {
299                         /* Vendor specific */
300                         memset(inqdata->vendor, 0x20, 20);
301                         len += sizeof(inqdata->vendor);
302                 }
303
304                 if (alloc_len >= INQ_OFFSET(ius)) {
305                         /* CLOCKING(3-2) QAS(1) IUS(0) */
306                         inqdata->ius = 0;
307                         len += sizeof(inqdata->ius);
308                 }
309
310                 if (alloc_len >= INQ_OFFSET(reserved)) {
311                         /* Reserved */
312                         inqdata->reserved = 0;
313                         len += sizeof(inqdata->reserved);
314                 }
315
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);
320                         len += 2;
321                 }
322
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);
327                         len += 2;
328                 }
329
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);
334                         len += 2;
335                 }
336
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);
341                         len += 2;
342                 }
343
344                 if (alloc_len > INQ_OFFSET(reserved) + 8) {
345                         i = alloc_len - (INQ_OFFSET(reserved) + 8);
346                         if (i > 30)
347                                 i = 30;
348                         memset(&inqdata->desc[8], 0, i);
349                         len += i;
350                 }
351
352                 /* ADDITIONAL LENGTH */
353                 inqdata->add_len = len;
354         }
355
356         /* STATUS GOOD */
357         scsi_task_set_status(task, SCSI_STATUS_GOOD, 0, 0, 0);
358         return hlen + len;
359
360 inq_error:
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);
365         return 0;
366 }
367
368 static int
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)
372 {
373         uint32_t i;
374         uint64_t offset;
375         uint32_t nbytes = 0;
376
377         offset = lba * bdev->blocklen;
378
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);
383                 else
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;
388         }
389
390         return nbytes;
391 }
392
393 static int
394 vhost_bdev_scsi_process_block(struct vhost_block_dev *bdev,
395                               struct vhost_scsi_task *task)
396 {
397         uint64_t lba, *temp64;
398         uint32_t xfer_len, *temp32;
399         uint16_t *temp16;
400         uint8_t *cdb = (uint8_t *)task->req->cdb;
401
402         switch (cdb[0]) {
403         case SBC_READ_6:
404         case SBC_WRITE_6:
405                 lba = (uint64_t)cdb[1] << 16;
406                 lba |= (uint64_t)cdb[2] << 8;
407                 lba |= (uint64_t)cdb[3];
408                 xfer_len = cdb[4];
409                 if (xfer_len == 0)
410                         xfer_len = 256;
411                 return vhost_bdev_scsi_readwrite(bdev, task, lba, xfer_len);
412
413         case SBC_READ_10:
414         case SBC_WRITE_10:
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);
420
421         case SBC_READ_12:
422         case SBC_WRITE_12:
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);
428
429         case SBC_READ_16:
430         case SBC_WRITE_16:
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);
436
437         case SBC_READ_CAPACITY_10: {
438                 uint8_t buffer[8];
439
440                 if (bdev->blockcnt - 1 > 0xffffffffULL)
441                         memset(buffer, 0xff, 4);
442                 else {
443                         temp32 = (uint32_t *)buffer;
444                         *temp32 = rte_cpu_to_be_32(bdev->blockcnt - 1);
445                 }
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);
451         }
452
453         case SBC_SYNCHRONIZE_CACHE_10:
454         case SBC_SYNCHRONIZE_CACHE_16:
455                 task->resp->status = SCSI_STATUS_GOOD;
456                 return 0;
457         }
458
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);
463         return 0;
464 }
465
466 int
467 vhost_bdev_process_scsi_commands(struct vhost_block_dev *bdev,
468                                  struct vhost_scsi_task *task)
469 {
470         int len;
471         uint8_t *data;
472         uint64_t *temp64, fmt_lun = 0;
473         uint32_t *temp32;
474         const uint8_t *lun;
475         uint8_t *cdb = (uint8_t *)task->req->cdb;
476
477         lun = (const uint8_t *)task->req->lun;
478         /* only 1 LUN supported */
479         if (lun[0] != 1 || lun[1] >= 1)
480                 return -1;
481
482         switch (cdb[0]) {
483         case SPC_INQUIRY:
484                 len = vhost_bdev_scsi_inquiry_command(bdev, task);
485                 task->data_len = len;
486                 break;
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);
494                 task->data_len = 16;
495                 scsi_task_set_status(task, SCSI_STATUS_GOOD, 0, 0, 0);
496                 break;
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);
501                 break;
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);
506                 break;
507         case SPC_TEST_UNIT_READY:
508                 scsi_task_set_status(task, SCSI_STATUS_GOOD, 0, 0, 0);
509                 break;
510         default:
511                 len = vhost_bdev_scsi_process_block(bdev, task);
512                 task->data_len = len;
513         }
514
515         return 0;
516 }