xref: /openbmc/qemu/hw/scsi/spapr_vscsi.c (revision 9c4218e9)
1 /*
2  * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
3  *
4  * PAPR Virtual SCSI, aka ibmvscsi
5  *
6  * Copyright (c) 2010,2011 Benjamin Herrenschmidt, IBM Corporation.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  *
26  * TODO:
27  *
28  *  - Cleanups :-)
29  *  - Sort out better how to assign devices to VSCSI instances
30  *  - Fix residual counts
31  *  - Add indirect descriptors support
32  *  - Maybe do autosense (PAPR seems to mandate it, linux doesn't care)
33  */
34 #include "qemu/osdep.h"
35 #include "hw/hw.h"
36 #include "hw/scsi/scsi.h"
37 #include "block/scsi.h"
38 #include "srp.h"
39 #include "hw/qdev.h"
40 #include "hw/ppc/spapr.h"
41 #include "hw/ppc/spapr_vio.h"
42 #include "viosrp.h"
43 
44 #include <libfdt.h>
45 
46 /*#define DEBUG_VSCSI*/
47 
48 #ifdef DEBUG_VSCSI
49 #define DPRINTF(fmt, ...) \
50     do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
51 #else
52 #define DPRINTF(fmt, ...) \
53     do { } while (0)
54 #endif
55 
56 /*
57  * Virtual SCSI device
58  */
59 
60 /* Random numbers */
61 #define VSCSI_MAX_SECTORS       4096
62 #define VSCSI_REQ_LIMIT         24
63 
64 #define SRP_RSP_SENSE_DATA_LEN  18
65 
66 #define SRP_REPORT_LUNS_WLUN    0xc10100000000000ULL
67 
68 typedef union vscsi_crq {
69     struct viosrp_crq s;
70     uint8_t raw[16];
71 } vscsi_crq;
72 
73 typedef struct vscsi_req {
74     vscsi_crq               crq;
75     union viosrp_iu         iu;
76 
77     /* SCSI request tracking */
78     SCSIRequest             *sreq;
79     uint32_t                qtag; /* qemu tag != srp tag */
80     bool                    active;
81     bool                    writing;
82     bool                    dma_error;
83     uint32_t                data_len;
84     uint32_t                senselen;
85     uint8_t                 sense[SCSI_SENSE_BUF_SIZE];
86 
87     /* RDMA related bits */
88     uint8_t                 dma_fmt;
89     uint16_t                local_desc;
90     uint16_t                total_desc;
91     uint16_t                cdb_offset;
92     uint16_t                cur_desc_num;
93     uint16_t                cur_desc_offset;
94 } vscsi_req;
95 
96 #define TYPE_VIO_SPAPR_VSCSI_DEVICE "spapr-vscsi"
97 #define VIO_SPAPR_VSCSI_DEVICE(obj) \
98      OBJECT_CHECK(VSCSIState, (obj), TYPE_VIO_SPAPR_VSCSI_DEVICE)
99 
100 typedef struct {
101     VIOsPAPRDevice vdev;
102     SCSIBus bus;
103     vscsi_req reqs[VSCSI_REQ_LIMIT];
104 } VSCSIState;
105 
106 static struct vscsi_req *vscsi_get_req(VSCSIState *s)
107 {
108     vscsi_req *req;
109     int i;
110 
111     for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
112         req = &s->reqs[i];
113         if (!req->active) {
114             memset(req, 0, sizeof(*req));
115             req->qtag = i;
116             req->active = 1;
117             return req;
118         }
119     }
120     return NULL;
121 }
122 
123 static struct vscsi_req *vscsi_find_req(VSCSIState *s, uint64_t srp_tag)
124 {
125     vscsi_req *req;
126     int i;
127 
128     for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
129         req = &s->reqs[i];
130         if (req->iu.srp.cmd.tag == srp_tag) {
131             return req;
132         }
133     }
134     return NULL;
135 }
136 
137 static void vscsi_put_req(vscsi_req *req)
138 {
139     if (req->sreq != NULL) {
140         scsi_req_unref(req->sreq);
141     }
142     req->sreq = NULL;
143     req->active = 0;
144 }
145 
146 static SCSIDevice *vscsi_device_find(SCSIBus *bus, uint64_t srp_lun, int *lun)
147 {
148     int channel = 0, id = 0;
149 
150 retry:
151     switch (srp_lun >> 62) {
152     case 0:
153         if ((srp_lun >> 56) != 0) {
154             channel = (srp_lun >> 56) & 0x3f;
155             id = (srp_lun >> 48) & 0xff;
156             srp_lun <<= 16;
157             goto retry;
158         }
159         *lun = (srp_lun >> 48) & 0xff;
160         break;
161 
162     case 1:
163         *lun = (srp_lun >> 48) & 0x3fff;
164         break;
165     case 2:
166         channel = (srp_lun >> 53) & 0x7;
167         id = (srp_lun >> 56) & 0x3f;
168         *lun = (srp_lun >> 48) & 0x1f;
169         break;
170     case 3:
171         *lun = -1;
172         return NULL;
173     default:
174         abort();
175     }
176 
177     return scsi_device_find(bus, channel, id, *lun);
178 }
179 
180 static int vscsi_send_iu(VSCSIState *s, vscsi_req *req,
181                          uint64_t length, uint8_t format)
182 {
183     long rc, rc1;
184 
185     /* First copy the SRP */
186     rc = spapr_vio_dma_write(&s->vdev, req->crq.s.IU_data_ptr,
187                              &req->iu, length);
188     if (rc) {
189         fprintf(stderr, "vscsi_send_iu: DMA write failure !\n");
190     }
191 
192     req->crq.s.valid = 0x80;
193     req->crq.s.format = format;
194     req->crq.s.reserved = 0x00;
195     req->crq.s.timeout = cpu_to_be16(0x0000);
196     req->crq.s.IU_length = cpu_to_be16(length);
197     req->crq.s.IU_data_ptr = req->iu.srp.rsp.tag; /* right byte order */
198 
199     if (rc == 0) {
200         req->crq.s.status = VIOSRP_OK;
201     } else {
202         req->crq.s.status = VIOSRP_ADAPTER_FAIL;
203     }
204 
205     rc1 = spapr_vio_send_crq(&s->vdev, req->crq.raw);
206     if (rc1) {
207         fprintf(stderr, "vscsi_send_iu: Error sending response\n");
208         return rc1;
209     }
210 
211     return rc;
212 }
213 
214 static void vscsi_makeup_sense(VSCSIState *s, vscsi_req *req,
215                                uint8_t key, uint8_t asc, uint8_t ascq)
216 {
217     req->senselen = SRP_RSP_SENSE_DATA_LEN;
218 
219     /* Valid bit and 'current errors' */
220     req->sense[0] = (0x1 << 7 | 0x70);
221     /* Sense key */
222     req->sense[2] = key;
223     /* Additional sense length */
224     req->sense[7] = 0xa; /* 10 bytes */
225     /* Additional sense code */
226     req->sense[12] = asc;
227     req->sense[13] = ascq;
228 }
229 
230 static int vscsi_send_rsp(VSCSIState *s, vscsi_req *req,
231                           uint8_t status, int32_t res_in, int32_t res_out)
232 {
233     union viosrp_iu *iu = &req->iu;
234     uint64_t tag = iu->srp.rsp.tag;
235     int total_len = sizeof(iu->srp.rsp);
236     uint8_t sol_not = iu->srp.cmd.sol_not;
237 
238     DPRINTF("VSCSI: Sending resp status: 0x%x, "
239             "res_in: %d, res_out: %d\n", status, res_in, res_out);
240 
241     memset(iu, 0, sizeof(struct srp_rsp));
242     iu->srp.rsp.opcode = SRP_RSP;
243     iu->srp.rsp.req_lim_delta = cpu_to_be32(1);
244     iu->srp.rsp.tag = tag;
245 
246     /* Handle residuals */
247     if (res_in < 0) {
248         iu->srp.rsp.flags |= SRP_RSP_FLAG_DIUNDER;
249         res_in = -res_in;
250     } else if (res_in) {
251         iu->srp.rsp.flags |= SRP_RSP_FLAG_DIOVER;
252     }
253     if (res_out < 0) {
254         iu->srp.rsp.flags |= SRP_RSP_FLAG_DOUNDER;
255         res_out = -res_out;
256     } else if (res_out) {
257         iu->srp.rsp.flags |= SRP_RSP_FLAG_DOOVER;
258     }
259     iu->srp.rsp.data_in_res_cnt = cpu_to_be32(res_in);
260     iu->srp.rsp.data_out_res_cnt = cpu_to_be32(res_out);
261 
262     /* We don't do response data */
263     /* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */
264     iu->srp.rsp.resp_data_len = cpu_to_be32(0);
265 
266     /* Handle success vs. failure */
267     iu->srp.rsp.status = status;
268     if (status) {
269         iu->srp.rsp.sol_not = (sol_not & 0x04) >> 2;
270         if (req->senselen) {
271             req->iu.srp.rsp.flags |= SRP_RSP_FLAG_SNSVALID;
272             req->iu.srp.rsp.sense_data_len = cpu_to_be32(req->senselen);
273             memcpy(req->iu.srp.rsp.data, req->sense, req->senselen);
274             total_len += req->senselen;
275         }
276     } else {
277         iu->srp.rsp.sol_not = (sol_not & 0x02) >> 1;
278     }
279 
280     vscsi_send_iu(s, req, total_len, VIOSRP_SRP_FORMAT);
281     return 0;
282 }
283 
284 static inline struct srp_direct_buf vscsi_swap_desc(struct srp_direct_buf desc)
285 {
286     desc.va = be64_to_cpu(desc.va);
287     desc.len = be32_to_cpu(desc.len);
288     return desc;
289 }
290 
291 static int vscsi_fetch_desc(VSCSIState *s, struct vscsi_req *req,
292                             unsigned n, unsigned buf_offset,
293                             struct srp_direct_buf *ret)
294 {
295     struct srp_cmd *cmd = &req->iu.srp.cmd;
296 
297     switch (req->dma_fmt) {
298     case SRP_NO_DATA_DESC: {
299         DPRINTF("VSCSI: no data descriptor\n");
300         return 0;
301     }
302     case SRP_DATA_DESC_DIRECT: {
303         memcpy(ret, cmd->add_data + req->cdb_offset, sizeof(*ret));
304         assert(req->cur_desc_num == 0);
305         DPRINTF("VSCSI: direct segment\n");
306         break;
307     }
308     case SRP_DATA_DESC_INDIRECT: {
309         struct srp_indirect_buf *tmp = (struct srp_indirect_buf *)
310                                        (cmd->add_data + req->cdb_offset);
311         if (n < req->local_desc) {
312             *ret = tmp->desc_list[n];
313             DPRINTF("VSCSI: indirect segment local tag=0x%x desc#%d/%d\n",
314                     req->qtag, n, req->local_desc);
315 
316         } else if (n < req->total_desc) {
317             int rc;
318             struct srp_direct_buf tbl_desc = vscsi_swap_desc(tmp->table_desc);
319             unsigned desc_offset = n * sizeof(struct srp_direct_buf);
320 
321             if (desc_offset >= tbl_desc.len) {
322                 DPRINTF("VSCSI:   #%d is ouf of range (%d bytes)\n",
323                         n, desc_offset);
324                 return -1;
325             }
326             rc = spapr_vio_dma_read(&s->vdev, tbl_desc.va + desc_offset,
327                                     ret, sizeof(struct srp_direct_buf));
328             if (rc) {
329                 DPRINTF("VSCSI: spapr_vio_dma_read -> %d reading ext_desc\n",
330                         rc);
331                 return -1;
332             }
333             DPRINTF("VSCSI: indirect segment ext. tag=0x%x desc#%d/%d { va=%"PRIx64" len=%x }\n",
334                     req->qtag, n, req->total_desc, tbl_desc.va, tbl_desc.len);
335         } else {
336             DPRINTF("VSCSI:   Out of descriptors !\n");
337             return 0;
338         }
339         break;
340     }
341     default:
342         fprintf(stderr, "VSCSI:   Unknown format %x\n", req->dma_fmt);
343         return -1;
344     }
345 
346     *ret = vscsi_swap_desc(*ret);
347     if (buf_offset > ret->len) {
348         DPRINTF("   offset=%x is out of a descriptor #%d boundary=%x\n",
349                 buf_offset, req->cur_desc_num, ret->len);
350         return -1;
351     }
352     ret->va += buf_offset;
353     ret->len -= buf_offset;
354 
355     DPRINTF("   cur=%d offs=%x ret { va=%"PRIx64" len=%x }\n",
356             req->cur_desc_num, req->cur_desc_offset, ret->va, ret->len);
357 
358     return ret->len ? 1 : 0;
359 }
360 
361 static int vscsi_srp_direct_data(VSCSIState *s, vscsi_req *req,
362                                  uint8_t *buf, uint32_t len)
363 {
364     struct srp_direct_buf md;
365     uint32_t llen;
366     int rc = 0;
367 
368     rc = vscsi_fetch_desc(s, req, req->cur_desc_num, req->cur_desc_offset, &md);
369     if (rc < 0) {
370         return -1;
371     } else if (rc == 0) {
372         return 0;
373     }
374 
375     llen = MIN(len, md.len);
376     if (llen) {
377         if (req->writing) { /* writing = to device = reading from memory */
378             rc = spapr_vio_dma_read(&s->vdev, md.va, buf, llen);
379         } else {
380             rc = spapr_vio_dma_write(&s->vdev, md.va, buf, llen);
381         }
382     }
383 
384     if (rc) {
385         return -1;
386     }
387     req->cur_desc_offset += llen;
388 
389     return llen;
390 }
391 
392 static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req,
393                                    uint8_t *buf, uint32_t len)
394 {
395     struct srp_direct_buf md;
396     int rc = 0;
397     uint32_t llen, total = 0;
398 
399     DPRINTF("VSCSI: indirect segment 0x%x bytes\n", len);
400 
401     /* While we have data ... */
402     while (len) {
403         rc = vscsi_fetch_desc(s, req, req->cur_desc_num, req->cur_desc_offset, &md);
404         if (rc < 0) {
405             return -1;
406         } else if (rc == 0) {
407             break;
408         }
409 
410         /* Perform transfer */
411         llen = MIN(len, md.len);
412         if (req->writing) { /* writing = to device = reading from memory */
413             rc = spapr_vio_dma_read(&s->vdev, md.va, buf, llen);
414         } else {
415             rc = spapr_vio_dma_write(&s->vdev, md.va, buf, llen);
416         }
417         if (rc) {
418             DPRINTF("VSCSI: spapr_vio_dma_r/w(%d) -> %d\n", req->writing, rc);
419             break;
420         }
421         DPRINTF("VSCSI:     data: %02x %02x %02x %02x...\n",
422                 buf[0], buf[1], buf[2], buf[3]);
423 
424         len -= llen;
425         buf += llen;
426 
427         total += llen;
428 
429         /* Update current position in the current descriptor */
430         req->cur_desc_offset += llen;
431         if (md.len == llen) {
432             /* Go to the next descriptor if the current one finished */
433             ++req->cur_desc_num;
434             req->cur_desc_offset = 0;
435         }
436     }
437 
438     return rc ? -1 : total;
439 }
440 
441 static int vscsi_srp_transfer_data(VSCSIState *s, vscsi_req *req,
442                                    int writing, uint8_t *buf, uint32_t len)
443 {
444     int err = 0;
445 
446     switch (req->dma_fmt) {
447     case SRP_NO_DATA_DESC:
448         DPRINTF("VSCSI: no data desc transfer, skipping 0x%x bytes\n", len);
449         break;
450     case SRP_DATA_DESC_DIRECT:
451         err = vscsi_srp_direct_data(s, req, buf, len);
452         break;
453     case SRP_DATA_DESC_INDIRECT:
454         err = vscsi_srp_indirect_data(s, req, buf, len);
455         break;
456     }
457     return err;
458 }
459 
460 /* Bits from linux srp */
461 static int data_out_desc_size(struct srp_cmd *cmd)
462 {
463     int size = 0;
464     uint8_t fmt = cmd->buf_fmt >> 4;
465 
466     switch (fmt) {
467     case SRP_NO_DATA_DESC:
468         break;
469     case SRP_DATA_DESC_DIRECT:
470         size = sizeof(struct srp_direct_buf);
471         break;
472     case SRP_DATA_DESC_INDIRECT:
473         size = sizeof(struct srp_indirect_buf) +
474             sizeof(struct srp_direct_buf)*cmd->data_out_desc_cnt;
475         break;
476     default:
477         break;
478     }
479     return size;
480 }
481 
482 static int vscsi_preprocess_desc(vscsi_req *req)
483 {
484     struct srp_cmd *cmd = &req->iu.srp.cmd;
485 
486     req->cdb_offset = cmd->add_cdb_len & ~3;
487 
488     if (req->writing) {
489         req->dma_fmt = cmd->buf_fmt >> 4;
490     } else {
491         req->cdb_offset += data_out_desc_size(cmd);
492         req->dma_fmt = cmd->buf_fmt & ((1U << 4) - 1);
493     }
494 
495     switch (req->dma_fmt) {
496     case SRP_NO_DATA_DESC:
497         break;
498     case SRP_DATA_DESC_DIRECT:
499         req->total_desc = req->local_desc = 1;
500         break;
501     case SRP_DATA_DESC_INDIRECT: {
502         struct srp_indirect_buf *ind_tmp = (struct srp_indirect_buf *)
503                 (cmd->add_data + req->cdb_offset);
504 
505         req->total_desc = be32_to_cpu(ind_tmp->table_desc.len) /
506                           sizeof(struct srp_direct_buf);
507         req->local_desc = req->writing ? cmd->data_out_desc_cnt :
508                           cmd->data_in_desc_cnt;
509         break;
510     }
511     default:
512         fprintf(stderr,
513                 "vscsi_preprocess_desc: Unknown format %x\n", req->dma_fmt);
514         return -1;
515     }
516 
517     return 0;
518 }
519 
520 /* Callback to indicate that the SCSI layer has completed a transfer.  */
521 static void vscsi_transfer_data(SCSIRequest *sreq, uint32_t len)
522 {
523     VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
524     vscsi_req *req = sreq->hba_private;
525     uint8_t *buf;
526     int rc = 0;
527 
528     DPRINTF("VSCSI: SCSI xfer complete tag=0x%x len=0x%x, req=%p\n",
529             sreq->tag, len, req);
530     if (req == NULL) {
531         fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
532         return;
533     }
534 
535     if (len) {
536         buf = scsi_req_get_buf(sreq);
537         rc = vscsi_srp_transfer_data(s, req, req->writing, buf, len);
538     }
539     if (rc < 0) {
540         fprintf(stderr, "VSCSI: RDMA error rc=%d!\n", rc);
541         req->dma_error = true;
542         scsi_req_cancel(req->sreq);
543         return;
544     }
545 
546     /* Start next chunk */
547     req->data_len -= rc;
548     scsi_req_continue(sreq);
549 }
550 
551 /* Callback to indicate that the SCSI layer has completed a transfer.  */
552 static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status, size_t resid)
553 {
554     VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
555     vscsi_req *req = sreq->hba_private;
556     int32_t res_in = 0, res_out = 0;
557 
558     DPRINTF("VSCSI: SCSI cmd complete, tag=0x%x status=0x%x, req=%p\n",
559             sreq->tag, status, req);
560     if (req == NULL) {
561         fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
562         return;
563     }
564 
565     if (status == CHECK_CONDITION) {
566         req->senselen = scsi_req_get_sense(req->sreq, req->sense,
567                                            sizeof(req->sense));
568         DPRINTF("VSCSI: Sense data, %d bytes:\n", req->senselen);
569         DPRINTF("       %02x  %02x  %02x  %02x  %02x  %02x  %02x  %02x\n",
570                 req->sense[0], req->sense[1], req->sense[2], req->sense[3],
571                 req->sense[4], req->sense[5], req->sense[6], req->sense[7]);
572         DPRINTF("       %02x  %02x  %02x  %02x  %02x  %02x  %02x  %02x\n",
573                 req->sense[8], req->sense[9], req->sense[10], req->sense[11],
574                 req->sense[12], req->sense[13], req->sense[14], req->sense[15]);
575     }
576 
577     DPRINTF("VSCSI: Command complete err=%d\n", status);
578     if (status == 0) {
579         /* We handle overflows, not underflows for normal commands,
580          * but hopefully nobody cares
581          */
582         if (req->writing) {
583             res_out = req->data_len;
584         } else {
585             res_in = req->data_len;
586         }
587     }
588     vscsi_send_rsp(s, req, status, res_in, res_out);
589     vscsi_put_req(req);
590 }
591 
592 static void vscsi_request_cancelled(SCSIRequest *sreq)
593 {
594     vscsi_req *req = sreq->hba_private;
595 
596     if (req->dma_error) {
597         VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
598 
599         vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
600         vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
601     }
602     vscsi_put_req(req);
603 }
604 
605 static const VMStateDescription vmstate_spapr_vscsi_req = {
606     .name = "spapr_vscsi_req",
607     .version_id = 1,
608     .minimum_version_id = 1,
609     .fields = (VMStateField[]) {
610         VMSTATE_BUFFER(crq.raw, vscsi_req),
611         VMSTATE_BUFFER(iu.srp.reserved, vscsi_req),
612         VMSTATE_UINT32(qtag, vscsi_req),
613         VMSTATE_BOOL(active, vscsi_req),
614         VMSTATE_UINT32(data_len, vscsi_req),
615         VMSTATE_BOOL(writing, vscsi_req),
616         VMSTATE_UINT32(senselen, vscsi_req),
617         VMSTATE_BUFFER(sense, vscsi_req),
618         VMSTATE_UINT8(dma_fmt, vscsi_req),
619         VMSTATE_UINT16(local_desc, vscsi_req),
620         VMSTATE_UINT16(total_desc, vscsi_req),
621         VMSTATE_UINT16(cdb_offset, vscsi_req),
622       /*Restart SCSI request from the beginning for now */
623       /*VMSTATE_UINT16(cur_desc_num, vscsi_req),
624         VMSTATE_UINT16(cur_desc_offset, vscsi_req),*/
625         VMSTATE_END_OF_LIST()
626     },
627 };
628 
629 static void vscsi_save_request(QEMUFile *f, SCSIRequest *sreq)
630 {
631     vscsi_req *req = sreq->hba_private;
632     assert(req->active);
633 
634     vmstate_save_state(f, &vmstate_spapr_vscsi_req, req, NULL);
635 
636     DPRINTF("VSCSI: saving tag=%u, current desc#%d, offset=%x\n",
637             req->qtag, req->cur_desc_num, req->cur_desc_offset);
638 }
639 
640 static void *vscsi_load_request(QEMUFile *f, SCSIRequest *sreq)
641 {
642     SCSIBus *bus = sreq->bus;
643     VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(bus->qbus.parent);
644     vscsi_req *req;
645     int rc;
646 
647     assert(sreq->tag < VSCSI_REQ_LIMIT);
648     req = &s->reqs[sreq->tag];
649     assert(!req->active);
650 
651     memset(req, 0, sizeof(*req));
652     rc = vmstate_load_state(f, &vmstate_spapr_vscsi_req, req, 1);
653     if (rc) {
654         fprintf(stderr, "VSCSI: failed loading request tag#%u\n", sreq->tag);
655         return NULL;
656     }
657     assert(req->active);
658 
659     req->sreq = scsi_req_ref(sreq);
660 
661     DPRINTF("VSCSI: restoring tag=%u, current desc#%d, offset=%x\n",
662             req->qtag, req->cur_desc_num, req->cur_desc_offset);
663 
664     return req;
665 }
666 
667 static void vscsi_process_login(VSCSIState *s, vscsi_req *req)
668 {
669     union viosrp_iu *iu = &req->iu;
670     struct srp_login_rsp *rsp = &iu->srp.login_rsp;
671     uint64_t tag = iu->srp.rsp.tag;
672 
673     DPRINTF("VSCSI: Got login, sendin response !\n");
674 
675     /* TODO handle case that requested size is wrong and
676      * buffer format is wrong
677      */
678     memset(iu, 0, sizeof(struct srp_login_rsp));
679     rsp->opcode = SRP_LOGIN_RSP;
680     /* Don't advertise quite as many request as we support to
681      * keep room for management stuff etc...
682      */
683     rsp->req_lim_delta = cpu_to_be32(VSCSI_REQ_LIMIT-2);
684     rsp->tag = tag;
685     rsp->max_it_iu_len = cpu_to_be32(sizeof(union srp_iu));
686     rsp->max_ti_iu_len = cpu_to_be32(sizeof(union srp_iu));
687     /* direct and indirect */
688     rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
689 
690     vscsi_send_iu(s, req, sizeof(*rsp), VIOSRP_SRP_FORMAT);
691 }
692 
693 static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
694 {
695     uint8_t *cdb = req->iu.srp.cmd.cdb;
696     uint8_t resp_data[36];
697     int rc, len, alen;
698 
699     /* We dont do EVPD. Also check that page_code is 0 */
700     if ((cdb[1] & 0x01) || cdb[2] != 0) {
701         /* Send INVALID FIELD IN CDB */
702         vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0);
703         vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
704         return;
705     }
706     alen = cdb[3];
707     alen = (alen << 8) | cdb[4];
708     len = MIN(alen, 36);
709 
710     /* Fake up inquiry using PQ=3 */
711     memset(resp_data, 0, 36);
712     resp_data[0] = 0x7f;   /* Not capable of supporting a device here */
713     resp_data[2] = 0x06;   /* SPS-4 */
714     resp_data[3] = 0x02;   /* Resp data format */
715     resp_data[4] = 36 - 5; /* Additional length */
716     resp_data[7] = 0x10;   /* Sync transfers */
717     memcpy(&resp_data[16], "QEMU EMPTY      ", 16);
718     memcpy(&resp_data[8], "QEMU    ", 8);
719 
720     req->writing = 0;
721     vscsi_preprocess_desc(req);
722     rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
723     if (rc < 0) {
724         vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
725         vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
726     } else {
727         vscsi_send_rsp(s, req, 0, 36 - rc, 0);
728     }
729 }
730 
731 static void vscsi_report_luns(VSCSIState *s, vscsi_req *req)
732 {
733     BusChild *kid;
734     int i, len, n, rc;
735     uint8_t *resp_data;
736     bool found_lun0;
737 
738     n = 0;
739     found_lun0 = false;
740     QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
741         SCSIDevice *dev = SCSI_DEVICE(kid->child);
742 
743         n += 8;
744         if (dev->channel == 0 && dev->id == 0 && dev->lun == 0) {
745             found_lun0 = true;
746         }
747     }
748     if (!found_lun0) {
749         n += 8;
750     }
751     len = n+8;
752 
753     resp_data = g_malloc0(len);
754     stl_be_p(resp_data, n);
755     i = found_lun0 ? 8 : 16;
756     QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
757         DeviceState *qdev = kid->child;
758         SCSIDevice *dev = SCSI_DEVICE(qdev);
759 
760         if (dev->id == 0 && dev->channel == 0) {
761             resp_data[i] = 0;         /* Use simple LUN for 0 (SAM5 4.7.7.1) */
762         } else {
763             resp_data[i] = (2 << 6);  /* Otherwise LUN addressing (4.7.7.4)  */
764         }
765         resp_data[i] |= dev->id;
766         resp_data[i+1] = (dev->channel << 5);
767         resp_data[i+1] |= dev->lun;
768         i += 8;
769     }
770 
771     vscsi_preprocess_desc(req);
772     rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
773     g_free(resp_data);
774     if (rc < 0) {
775         vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
776         vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
777     } else {
778         vscsi_send_rsp(s, req, 0, len - rc, 0);
779     }
780 }
781 
782 static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req)
783 {
784     union srp_iu *srp = &req->iu.srp;
785     SCSIDevice *sdev;
786     int n, lun;
787 
788     if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN)
789       && srp->cmd.cdb[0] == REPORT_LUNS) {
790         vscsi_report_luns(s, req);
791         return 0;
792     }
793 
794     sdev = vscsi_device_find(&s->bus, be64_to_cpu(srp->cmd.lun), &lun);
795     if (!sdev) {
796         DPRINTF("VSCSI: Command for lun %08" PRIx64 " with no drive\n",
797                 be64_to_cpu(srp->cmd.lun));
798         if (srp->cmd.cdb[0] == INQUIRY) {
799             vscsi_inquiry_no_target(s, req);
800         } else {
801             vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0x00);
802             vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
803         } return 1;
804     }
805 
806     req->sreq = scsi_req_new(sdev, req->qtag, lun, srp->cmd.cdb, req);
807     n = scsi_req_enqueue(req->sreq);
808 
809     DPRINTF("VSCSI: Queued command tag 0x%x CMD 0x%x=%s LUN %d ret: %d\n",
810             req->qtag, srp->cmd.cdb[0], scsi_command_name(srp->cmd.cdb[0]),
811             lun, n);
812 
813     if (n) {
814         /* Transfer direction must be set before preprocessing the
815          * descriptors
816          */
817         req->writing = (n < 1);
818 
819         /* Preprocess RDMA descriptors */
820         vscsi_preprocess_desc(req);
821 
822         /* Get transfer direction and initiate transfer */
823         if (n > 0) {
824             req->data_len = n;
825         } else if (n < 0) {
826             req->data_len = -n;
827         }
828         scsi_req_continue(req->sreq);
829     }
830     /* Don't touch req here, it may have been recycled already */
831 
832     return 0;
833 }
834 
835 static int vscsi_process_tsk_mgmt(VSCSIState *s, vscsi_req *req)
836 {
837     union viosrp_iu *iu = &req->iu;
838     vscsi_req *tmpreq;
839     int i, lun = 0, resp = SRP_TSK_MGMT_COMPLETE;
840     SCSIDevice *d;
841     uint64_t tag = iu->srp.rsp.tag;
842     uint8_t sol_not = iu->srp.cmd.sol_not;
843 
844     fprintf(stderr, "vscsi_process_tsk_mgmt %02x\n",
845             iu->srp.tsk_mgmt.tsk_mgmt_func);
846 
847     d = vscsi_device_find(&s->bus, be64_to_cpu(req->iu.srp.tsk_mgmt.lun), &lun);
848     if (!d) {
849         resp = SRP_TSK_MGMT_FIELDS_INVALID;
850     } else {
851         switch (iu->srp.tsk_mgmt.tsk_mgmt_func) {
852         case SRP_TSK_ABORT_TASK:
853             if (d->lun != lun) {
854                 resp = SRP_TSK_MGMT_FIELDS_INVALID;
855                 break;
856             }
857 
858             tmpreq = vscsi_find_req(s, req->iu.srp.tsk_mgmt.task_tag);
859             if (tmpreq && tmpreq->sreq) {
860                 assert(tmpreq->sreq->hba_private);
861                 scsi_req_cancel(tmpreq->sreq);
862             }
863             break;
864 
865         case SRP_TSK_LUN_RESET:
866             if (d->lun != lun) {
867                 resp = SRP_TSK_MGMT_FIELDS_INVALID;
868                 break;
869             }
870 
871             qdev_reset_all(&d->qdev);
872             break;
873 
874         case SRP_TSK_ABORT_TASK_SET:
875         case SRP_TSK_CLEAR_TASK_SET:
876             if (d->lun != lun) {
877                 resp = SRP_TSK_MGMT_FIELDS_INVALID;
878                 break;
879             }
880 
881             for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
882                 tmpreq = &s->reqs[i];
883                 if (tmpreq->iu.srp.cmd.lun != req->iu.srp.tsk_mgmt.lun) {
884                     continue;
885                 }
886                 if (!tmpreq->active || !tmpreq->sreq) {
887                     continue;
888                 }
889                 assert(tmpreq->sreq->hba_private);
890                 scsi_req_cancel(tmpreq->sreq);
891             }
892             break;
893 
894         case SRP_TSK_CLEAR_ACA:
895             resp = SRP_TSK_MGMT_NOT_SUPPORTED;
896             break;
897 
898         default:
899             resp = SRP_TSK_MGMT_FIELDS_INVALID;
900             break;
901         }
902     }
903 
904     /* Compose the response here as  */
905     memset(iu, 0, sizeof(struct srp_rsp) + 4);
906     iu->srp.rsp.opcode = SRP_RSP;
907     iu->srp.rsp.req_lim_delta = cpu_to_be32(1);
908     iu->srp.rsp.tag = tag;
909     iu->srp.rsp.flags |= SRP_RSP_FLAG_RSPVALID;
910     iu->srp.rsp.resp_data_len = cpu_to_be32(4);
911     if (resp) {
912         iu->srp.rsp.sol_not = (sol_not & 0x04) >> 2;
913     } else {
914         iu->srp.rsp.sol_not = (sol_not & 0x02) >> 1;
915     }
916 
917     iu->srp.rsp.status = GOOD;
918     iu->srp.rsp.data[3] = resp;
919 
920     vscsi_send_iu(s, req, sizeof(iu->srp.rsp) + 4, VIOSRP_SRP_FORMAT);
921 
922     return 1;
923 }
924 
925 static int vscsi_handle_srp_req(VSCSIState *s, vscsi_req *req)
926 {
927     union srp_iu *srp = &req->iu.srp;
928     int done = 1;
929     uint8_t opcode = srp->rsp.opcode;
930 
931     switch (opcode) {
932     case SRP_LOGIN_REQ:
933         vscsi_process_login(s, req);
934         break;
935     case SRP_TSK_MGMT:
936         done = vscsi_process_tsk_mgmt(s, req);
937         break;
938     case SRP_CMD:
939         done = vscsi_queue_cmd(s, req);
940         break;
941     case SRP_LOGIN_RSP:
942     case SRP_I_LOGOUT:
943     case SRP_T_LOGOUT:
944     case SRP_RSP:
945     case SRP_CRED_REQ:
946     case SRP_CRED_RSP:
947     case SRP_AER_REQ:
948     case SRP_AER_RSP:
949         fprintf(stderr, "VSCSI: Unsupported opcode %02x\n", opcode);
950         break;
951     default:
952         fprintf(stderr, "VSCSI: Unknown type %02x\n", opcode);
953     }
954 
955     return done;
956 }
957 
958 static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
959 {
960     struct viosrp_adapter_info *sinfo;
961     struct mad_adapter_info_data info;
962     int rc;
963 
964     sinfo = &req->iu.mad.adapter_info;
965 
966 #if 0 /* What for ? */
967     rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(sinfo->buffer),
968                             &info, be16_to_cpu(sinfo->common.length));
969     if (rc) {
970         fprintf(stderr, "vscsi_send_adapter_info: DMA read failure !\n");
971     }
972 #endif
973     memset(&info, 0, sizeof(info));
974     strcpy(info.srp_version, SRP_VERSION);
975     memcpy(info.partition_name, "qemu", sizeof("qemu"));
976     info.partition_number = cpu_to_be32(0);
977     info.mad_version = cpu_to_be32(1);
978     info.os_type = cpu_to_be32(2);
979     info.port_max_txu[0] = cpu_to_be32(VSCSI_MAX_SECTORS << 9);
980 
981     rc = spapr_vio_dma_write(&s->vdev, be64_to_cpu(sinfo->buffer),
982                              &info, be16_to_cpu(sinfo->common.length));
983     if (rc)  {
984         fprintf(stderr, "vscsi_send_adapter_info: DMA write failure !\n");
985     }
986 
987     sinfo->common.status = rc ? cpu_to_be32(1) : 0;
988 
989     return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
990 }
991 
992 static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
993 {
994     struct viosrp_capabilities *vcap;
995     struct capabilities cap = { };
996     uint16_t len, req_len;
997     uint64_t buffer;
998     int rc;
999 
1000     vcap = &req->iu.mad.capabilities;
1001     req_len = len = be16_to_cpu(vcap->common.length);
1002     buffer = be64_to_cpu(vcap->buffer);
1003     if (len > sizeof(cap)) {
1004         fprintf(stderr, "vscsi_send_capabilities: capabilities size mismatch !\n");
1005 
1006         /*
1007          * Just read and populate the structure that is known.
1008          * Zero rest of the structure.
1009          */
1010         len = sizeof(cap);
1011     }
1012     rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
1013     if (rc)  {
1014         fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");
1015     }
1016 
1017     /*
1018      * Current implementation does not suppport any migration or
1019      * reservation capabilities. Construct the response telling the
1020      * guest not to use them.
1021      */
1022     cap.flags = 0;
1023     cap.migration.ecl = 0;
1024     cap.reserve.type = 0;
1025     cap.migration.common.server_support = 0;
1026     cap.reserve.common.server_support = 0;
1027 
1028     rc = spapr_vio_dma_write(&s->vdev, buffer, &cap, len);
1029     if (rc)  {
1030         fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
1031     }
1032     if (req_len > len) {
1033         /*
1034          * Being paranoid and lets not worry about the error code
1035          * here. Actual write of the cap is done above.
1036          */
1037         spapr_vio_dma_set(&s->vdev, (buffer + len), 0, (req_len - len));
1038     }
1039     vcap->common.status = rc ? cpu_to_be32(1) : 0;
1040     return vscsi_send_iu(s, req, sizeof(*vcap), VIOSRP_MAD_FORMAT);
1041 }
1042 
1043 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
1044 {
1045     union mad_iu *mad = &req->iu.mad;
1046     bool request_handled = false;
1047     uint64_t retlen = 0;
1048 
1049     switch (be32_to_cpu(mad->empty_iu.common.type)) {
1050     case VIOSRP_EMPTY_IU_TYPE:
1051         fprintf(stderr, "Unsupported EMPTY MAD IU\n");
1052         retlen = sizeof(mad->empty_iu);
1053         break;
1054     case VIOSRP_ERROR_LOG_TYPE:
1055         fprintf(stderr, "Unsupported ERROR LOG MAD IU\n");
1056         retlen = sizeof(mad->error_log);
1057         break;
1058     case VIOSRP_ADAPTER_INFO_TYPE:
1059         vscsi_send_adapter_info(s, req);
1060         request_handled = true;
1061         break;
1062     case VIOSRP_HOST_CONFIG_TYPE:
1063         retlen = sizeof(mad->host_config);
1064         break;
1065     case VIOSRP_CAPABILITIES_TYPE:
1066         vscsi_send_capabilities(s, req);
1067         request_handled = true;
1068         break;
1069     default:
1070         fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
1071                 be32_to_cpu(mad->empty_iu.common.type));
1072         /*
1073          * PAPR+ says that "The length field is set to the length
1074          * of the data structure(s) used in the command".
1075          * As we did not recognize the request type, put zero there.
1076          */
1077         retlen = 0;
1078     }
1079 
1080     if (!request_handled) {
1081         mad->empty_iu.common.status = cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED);
1082         vscsi_send_iu(s, req, retlen, VIOSRP_MAD_FORMAT);
1083     }
1084 
1085     return 1;
1086 }
1087 
1088 static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq)
1089 {
1090     vscsi_req *req;
1091     int done;
1092 
1093     req = vscsi_get_req(s);
1094     if (req == NULL) {
1095         fprintf(stderr, "VSCSI: Failed to get a request !\n");
1096         return;
1097     }
1098 
1099     /* We only support a limited number of descriptors, we know
1100      * the ibmvscsi driver uses up to 10 max, so it should fit
1101      * in our 256 bytes IUs. If not we'll have to increase the size
1102      * of the structure.
1103      */
1104     if (crq->s.IU_length > sizeof(union viosrp_iu)) {
1105         fprintf(stderr, "VSCSI: SRP IU too long (%d bytes) !\n",
1106                 crq->s.IU_length);
1107         vscsi_put_req(req);
1108         return;
1109     }
1110 
1111     /* XXX Handle failure differently ? */
1112     if (spapr_vio_dma_read(&s->vdev, crq->s.IU_data_ptr, &req->iu,
1113                            crq->s.IU_length)) {
1114         fprintf(stderr, "vscsi_got_payload: DMA read failure !\n");
1115         vscsi_put_req(req);
1116         return;
1117     }
1118     memcpy(&req->crq, crq, sizeof(vscsi_crq));
1119 
1120     if (crq->s.format == VIOSRP_MAD_FORMAT) {
1121         done = vscsi_handle_mad_req(s, req);
1122     } else {
1123         done = vscsi_handle_srp_req(s, req);
1124     }
1125 
1126     if (done) {
1127         vscsi_put_req(req);
1128     }
1129 }
1130 
1131 
1132 static int vscsi_do_crq(struct VIOsPAPRDevice *dev, uint8_t *crq_data)
1133 {
1134     VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
1135     vscsi_crq crq;
1136 
1137     memcpy(crq.raw, crq_data, 16);
1138     crq.s.timeout = be16_to_cpu(crq.s.timeout);
1139     crq.s.IU_length = be16_to_cpu(crq.s.IU_length);
1140     crq.s.IU_data_ptr = be64_to_cpu(crq.s.IU_data_ptr);
1141 
1142     DPRINTF("VSCSI: do_crq %02x %02x ...\n", crq.raw[0], crq.raw[1]);
1143 
1144     switch (crq.s.valid) {
1145     case 0xc0: /* Init command/response */
1146 
1147         /* Respond to initialization request */
1148         if (crq.s.format == 0x01) {
1149             memset(crq.raw, 0, 16);
1150             crq.s.valid = 0xc0;
1151             crq.s.format = 0x02;
1152             spapr_vio_send_crq(dev, crq.raw);
1153         }
1154 
1155         /* Note that in hotplug cases, we might get a 0x02
1156          * as a result of us emitting the init request
1157          */
1158 
1159         break;
1160     case 0xff: /* Link event */
1161 
1162         /* Not handled for now */
1163 
1164         break;
1165     case 0x80: /* Payloads */
1166         switch (crq.s.format) {
1167         case VIOSRP_SRP_FORMAT: /* AKA VSCSI request */
1168         case VIOSRP_MAD_FORMAT: /* AKA VSCSI response */
1169             vscsi_got_payload(s, &crq);
1170             break;
1171         case VIOSRP_OS400_FORMAT:
1172         case VIOSRP_AIX_FORMAT:
1173         case VIOSRP_LINUX_FORMAT:
1174         case VIOSRP_INLINE_FORMAT:
1175             fprintf(stderr, "vscsi_do_srq: Unsupported payload format %02x\n",
1176                     crq.s.format);
1177             break;
1178         default:
1179             fprintf(stderr, "vscsi_do_srq: Unknown payload format %02x\n",
1180                     crq.s.format);
1181         }
1182         break;
1183     default:
1184         fprintf(stderr, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
1185                 crq.raw[0], crq.raw[1]);
1186     };
1187 
1188     return 0;
1189 }
1190 
1191 static const struct SCSIBusInfo vscsi_scsi_info = {
1192     .tcq = true,
1193     .max_channel = 7, /* logical unit addressing format */
1194     .max_target = 63,
1195     .max_lun = 31,
1196 
1197     .transfer_data = vscsi_transfer_data,
1198     .complete = vscsi_command_complete,
1199     .cancel = vscsi_request_cancelled,
1200     .save_request = vscsi_save_request,
1201     .load_request = vscsi_load_request,
1202 };
1203 
1204 static void spapr_vscsi_reset(VIOsPAPRDevice *dev)
1205 {
1206     VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
1207     int i;
1208 
1209     memset(s->reqs, 0, sizeof(s->reqs));
1210     for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
1211         s->reqs[i].qtag = i;
1212     }
1213 }
1214 
1215 static void spapr_vscsi_realize(VIOsPAPRDevice *dev, Error **errp)
1216 {
1217     VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
1218 
1219     dev->crq.SendFunc = vscsi_do_crq;
1220 
1221     scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev),
1222                  &vscsi_scsi_info, NULL);
1223     if (!dev->qdev.hotplugged) {
1224         scsi_bus_legacy_handle_cmdline(&s->bus, errp);
1225     }
1226 }
1227 
1228 void spapr_vscsi_create(VIOsPAPRBus *bus)
1229 {
1230     DeviceState *dev;
1231 
1232     dev = qdev_create(&bus->bus, "spapr-vscsi");
1233 
1234     qdev_init_nofail(dev);
1235 }
1236 
1237 static int spapr_vscsi_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
1238 {
1239     int ret;
1240 
1241     ret = fdt_setprop_cell(fdt, node_off, "#address-cells", 2);
1242     if (ret < 0) {
1243         return ret;
1244     }
1245 
1246     ret = fdt_setprop_cell(fdt, node_off, "#size-cells", 0);
1247     if (ret < 0) {
1248         return ret;
1249     }
1250 
1251     return 0;
1252 }
1253 
1254 static Property spapr_vscsi_properties[] = {
1255     DEFINE_SPAPR_PROPERTIES(VSCSIState, vdev),
1256     DEFINE_PROP_END_OF_LIST(),
1257 };
1258 
1259 static const VMStateDescription vmstate_spapr_vscsi = {
1260     .name = "spapr_vscsi",
1261     .version_id = 1,
1262     .minimum_version_id = 1,
1263     .fields = (VMStateField[]) {
1264         VMSTATE_SPAPR_VIO(vdev, VSCSIState),
1265         /* VSCSI state */
1266         /* ???? */
1267 
1268         VMSTATE_END_OF_LIST()
1269     },
1270 };
1271 
1272 static void spapr_vscsi_class_init(ObjectClass *klass, void *data)
1273 {
1274     DeviceClass *dc = DEVICE_CLASS(klass);
1275     VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
1276 
1277     k->realize = spapr_vscsi_realize;
1278     k->reset = spapr_vscsi_reset;
1279     k->devnode = spapr_vscsi_devnode;
1280     k->dt_name = "v-scsi";
1281     k->dt_type = "vscsi";
1282     k->dt_compatible = "IBM,v-scsi";
1283     k->signal_mask = 0x00000001;
1284     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1285     dc->props = spapr_vscsi_properties;
1286     k->rtce_window_size = 0x10000000;
1287     dc->vmsd = &vmstate_spapr_vscsi;
1288 }
1289 
1290 static const TypeInfo spapr_vscsi_info = {
1291     .name          = TYPE_VIO_SPAPR_VSCSI_DEVICE,
1292     .parent        = TYPE_VIO_SPAPR_DEVICE,
1293     .instance_size = sizeof(VSCSIState),
1294     .class_init    = spapr_vscsi_class_init,
1295 };
1296 
1297 static void spapr_vscsi_register_types(void)
1298 {
1299     type_register_static(&spapr_vscsi_info);
1300 }
1301 
1302 type_init(spapr_vscsi_register_types)
1303