xref: /openbmc/qemu/hw/scsi/spapr_vscsi.c (revision 8fb3c76c)
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 "hw/hw.h"
35 #include "hw/scsi/scsi.h"
36 #include "block/scsi.h"
37 #include "srp.h"
38 #include "hw/qdev.h"
39 #include "hw/ppc/spapr.h"
40 #include "hw/ppc/spapr_vio.h"
41 #include "viosrp.h"
42 
43 #include <libfdt.h>
44 
45 /*#define DEBUG_VSCSI*/
46 
47 #ifdef DEBUG_VSCSI
48 #define DPRINTF(fmt, ...) \
49     do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
50 #else
51 #define DPRINTF(fmt, ...) \
52     do { } while (0)
53 #endif
54 
55 /*
56  * Virtual SCSI device
57  */
58 
59 /* Random numbers */
60 #define VSCSI_MAX_SECTORS       4096
61 #define VSCSI_REQ_LIMIT         24
62 
63 #define SRP_RSP_SENSE_DATA_LEN  18
64 
65 #define SRP_REPORT_LUNS_WLUN    0xc10100000000000ULL
66 
67 typedef union vscsi_crq {
68     struct viosrp_crq s;
69     uint8_t raw[16];
70 } vscsi_crq;
71 
72 typedef struct vscsi_req {
73     vscsi_crq               crq;
74     union viosrp_iu         iu;
75 
76     /* SCSI request tracking */
77     SCSIRequest             *sreq;
78     uint32_t                qtag; /* qemu tag != srp tag */
79     bool                    active;
80     bool                    writing;
81     bool                    dma_error;
82     uint32_t                data_len;
83     uint32_t                senselen;
84     uint8_t                 sense[SCSI_SENSE_BUF_SIZE];
85 
86     /* RDMA related bits */
87     uint8_t                 dma_fmt;
88     uint16_t                local_desc;
89     uint16_t                total_desc;
90     uint16_t                cdb_offset;
91     uint16_t                cur_desc_num;
92     uint16_t                cur_desc_offset;
93 } vscsi_req;
94 
95 #define TYPE_VIO_SPAPR_VSCSI_DEVICE "spapr-vscsi"
96 #define VIO_SPAPR_VSCSI_DEVICE(obj) \
97      OBJECT_CHECK(VSCSIState, (obj), TYPE_VIO_SPAPR_VSCSI_DEVICE)
98 
99 typedef struct {
100     VIOsPAPRDevice vdev;
101     SCSIBus bus;
102     vscsi_req reqs[VSCSI_REQ_LIMIT];
103 } VSCSIState;
104 
105 static struct vscsi_req *vscsi_get_req(VSCSIState *s)
106 {
107     vscsi_req *req;
108     int i;
109 
110     for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
111         req = &s->reqs[i];
112         if (!req->active) {
113             memset(req, 0, sizeof(*req));
114             req->qtag = i;
115             req->active = 1;
116             return req;
117         }
118     }
119     return NULL;
120 }
121 
122 static struct vscsi_req *vscsi_find_req(VSCSIState *s, uint64_t srp_tag)
123 {
124     vscsi_req *req;
125     int i;
126 
127     for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
128         req = &s->reqs[i];
129         if (req->iu.srp.cmd.tag == srp_tag) {
130             return req;
131         }
132     }
133     return NULL;
134 }
135 
136 static void vscsi_put_req(vscsi_req *req)
137 {
138     if (req->sreq != NULL) {
139         scsi_req_unref(req->sreq);
140     }
141     req->sreq = NULL;
142     req->active = 0;
143 }
144 
145 static SCSIDevice *vscsi_device_find(SCSIBus *bus, uint64_t srp_lun, int *lun)
146 {
147     int channel = 0, id = 0;
148 
149 retry:
150     switch (srp_lun >> 62) {
151     case 0:
152         if ((srp_lun >> 56) != 0) {
153             channel = (srp_lun >> 56) & 0x3f;
154             id = (srp_lun >> 48) & 0xff;
155             srp_lun <<= 16;
156             goto retry;
157         }
158         *lun = (srp_lun >> 48) & 0xff;
159         break;
160 
161     case 1:
162         *lun = (srp_lun >> 48) & 0x3fff;
163         break;
164     case 2:
165         channel = (srp_lun >> 53) & 0x7;
166         id = (srp_lun >> 56) & 0x3f;
167         *lun = (srp_lun >> 48) & 0x1f;
168         break;
169     case 3:
170         *lun = -1;
171         return NULL;
172     default:
173         abort();
174     }
175 
176     return scsi_device_find(bus, channel, id, *lun);
177 }
178 
179 static int vscsi_send_iu(VSCSIState *s, vscsi_req *req,
180                          uint64_t length, uint8_t format)
181 {
182     long rc, rc1;
183 
184     /* First copy the SRP */
185     rc = spapr_vio_dma_write(&s->vdev, req->crq.s.IU_data_ptr,
186                              &req->iu, length);
187     if (rc) {
188         fprintf(stderr, "vscsi_send_iu: DMA write failure !\n");
189     }
190 
191     req->crq.s.valid = 0x80;
192     req->crq.s.format = format;
193     req->crq.s.reserved = 0x00;
194     req->crq.s.timeout = cpu_to_be16(0x0000);
195     req->crq.s.IU_length = cpu_to_be16(length);
196     req->crq.s.IU_data_ptr = req->iu.srp.rsp.tag; /* right byte order */
197 
198     if (rc == 0) {
199         req->crq.s.status = VIOSRP_OK;
200     } else {
201         req->crq.s.status = VIOSRP_ADAPTER_FAIL;
202     }
203 
204     rc1 = spapr_vio_send_crq(&s->vdev, req->crq.raw);
205     if (rc1) {
206         fprintf(stderr, "vscsi_send_iu: Error sending response\n");
207         return rc1;
208     }
209 
210     return rc;
211 }
212 
213 static void vscsi_makeup_sense(VSCSIState *s, vscsi_req *req,
214                                uint8_t key, uint8_t asc, uint8_t ascq)
215 {
216     req->senselen = SRP_RSP_SENSE_DATA_LEN;
217 
218     /* Valid bit and 'current errors' */
219     req->sense[0] = (0x1 << 7 | 0x70);
220     /* Sense key */
221     req->sense[2] = key;
222     /* Additional sense length */
223     req->sense[7] = 0xa; /* 10 bytes */
224     /* Additional sense code */
225     req->sense[12] = asc;
226     req->sense[13] = ascq;
227 }
228 
229 static int vscsi_send_rsp(VSCSIState *s, vscsi_req *req,
230                           uint8_t status, int32_t res_in, int32_t res_out)
231 {
232     union viosrp_iu *iu = &req->iu;
233     uint64_t tag = iu->srp.rsp.tag;
234     int total_len = sizeof(iu->srp.rsp);
235     uint8_t sol_not = iu->srp.cmd.sol_not;
236 
237     DPRINTF("VSCSI: Sending resp status: 0x%x, "
238             "res_in: %d, res_out: %d\n", status, res_in, res_out);
239 
240     memset(iu, 0, sizeof(struct srp_rsp));
241     iu->srp.rsp.opcode = SRP_RSP;
242     iu->srp.rsp.req_lim_delta = cpu_to_be32(1);
243     iu->srp.rsp.tag = tag;
244 
245     /* Handle residuals */
246     if (res_in < 0) {
247         iu->srp.rsp.flags |= SRP_RSP_FLAG_DIUNDER;
248         res_in = -res_in;
249     } else if (res_in) {
250         iu->srp.rsp.flags |= SRP_RSP_FLAG_DIOVER;
251     }
252     if (res_out < 0) {
253         iu->srp.rsp.flags |= SRP_RSP_FLAG_DOUNDER;
254         res_out = -res_out;
255     } else if (res_out) {
256         iu->srp.rsp.flags |= SRP_RSP_FLAG_DOOVER;
257     }
258     iu->srp.rsp.data_in_res_cnt = cpu_to_be32(res_in);
259     iu->srp.rsp.data_out_res_cnt = cpu_to_be32(res_out);
260 
261     /* We don't do response data */
262     /* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */
263     iu->srp.rsp.resp_data_len = cpu_to_be32(0);
264 
265     /* Handle success vs. failure */
266     iu->srp.rsp.status = status;
267     if (status) {
268         iu->srp.rsp.sol_not = (sol_not & 0x04) >> 2;
269         if (req->senselen) {
270             req->iu.srp.rsp.flags |= SRP_RSP_FLAG_SNSVALID;
271             req->iu.srp.rsp.sense_data_len = cpu_to_be32(req->senselen);
272             memcpy(req->iu.srp.rsp.data, req->sense, req->senselen);
273             total_len += req->senselen;
274         }
275     } else {
276         iu->srp.rsp.sol_not = (sol_not & 0x02) >> 1;
277     }
278 
279     vscsi_send_iu(s, req, total_len, VIOSRP_SRP_FORMAT);
280     return 0;
281 }
282 
283 static inline struct srp_direct_buf vscsi_swap_desc(struct srp_direct_buf desc)
284 {
285     desc.va = be64_to_cpu(desc.va);
286     desc.len = be32_to_cpu(desc.len);
287     return desc;
288 }
289 
290 static int vscsi_fetch_desc(VSCSIState *s, struct vscsi_req *req,
291                             unsigned n, unsigned buf_offset,
292                             struct srp_direct_buf *ret)
293 {
294     struct srp_cmd *cmd = &req->iu.srp.cmd;
295 
296     switch (req->dma_fmt) {
297     case SRP_NO_DATA_DESC: {
298         DPRINTF("VSCSI: no data descriptor\n");
299         return 0;
300     }
301     case SRP_DATA_DESC_DIRECT: {
302         memcpy(ret, cmd->add_data + req->cdb_offset, sizeof(*ret));
303         assert(req->cur_desc_num == 0);
304         DPRINTF("VSCSI: direct segment\n");
305         break;
306     }
307     case SRP_DATA_DESC_INDIRECT: {
308         struct srp_indirect_buf *tmp = (struct srp_indirect_buf *)
309                                        (cmd->add_data + req->cdb_offset);
310         if (n < req->local_desc) {
311             *ret = tmp->desc_list[n];
312             DPRINTF("VSCSI: indirect segment local tag=0x%x desc#%d/%d\n",
313                     req->qtag, n, req->local_desc);
314 
315         } else if (n < req->total_desc) {
316             int rc;
317             struct srp_direct_buf tbl_desc = vscsi_swap_desc(tmp->table_desc);
318             unsigned desc_offset = n * sizeof(struct srp_direct_buf);
319 
320             if (desc_offset >= tbl_desc.len) {
321                 DPRINTF("VSCSI:   #%d is ouf of range (%d bytes)\n",
322                         n, desc_offset);
323                 return -1;
324             }
325             rc = spapr_vio_dma_read(&s->vdev, tbl_desc.va + desc_offset,
326                                     ret, sizeof(struct srp_direct_buf));
327             if (rc) {
328                 DPRINTF("VSCSI: spapr_vio_dma_read -> %d reading ext_desc\n",
329                         rc);
330                 return -1;
331             }
332             DPRINTF("VSCSI: indirect segment ext. tag=0x%x desc#%d/%d { va=%"PRIx64" len=%x }\n",
333                     req->qtag, n, req->total_desc, tbl_desc.va, tbl_desc.len);
334         } else {
335             DPRINTF("VSCSI:   Out of descriptors !\n");
336             return 0;
337         }
338         break;
339     }
340     default:
341         fprintf(stderr, "VSCSI:   Unknown format %x\n", req->dma_fmt);
342         return -1;
343     }
344 
345     *ret = vscsi_swap_desc(*ret);
346     if (buf_offset > ret->len) {
347         DPRINTF("   offset=%x is out of a descriptor #%d boundary=%x\n",
348                 buf_offset, req->cur_desc_num, ret->len);
349         return -1;
350     }
351     ret->va += buf_offset;
352     ret->len -= buf_offset;
353 
354     DPRINTF("   cur=%d offs=%x ret { va=%"PRIx64" len=%x }\n",
355             req->cur_desc_num, req->cur_desc_offset, ret->va, ret->len);
356 
357     return ret->len ? 1 : 0;
358 }
359 
360 static int vscsi_srp_direct_data(VSCSIState *s, vscsi_req *req,
361                                  uint8_t *buf, uint32_t len)
362 {
363     struct srp_direct_buf md;
364     uint32_t llen;
365     int rc = 0;
366 
367     rc = vscsi_fetch_desc(s, req, req->cur_desc_num, req->cur_desc_offset, &md);
368     if (rc < 0) {
369         return -1;
370     } else if (rc == 0) {
371         return 0;
372     }
373 
374     llen = MIN(len, md.len);
375     if (llen) {
376         if (req->writing) { /* writing = to device = reading from memory */
377             rc = spapr_vio_dma_read(&s->vdev, md.va, buf, llen);
378         } else {
379             rc = spapr_vio_dma_write(&s->vdev, md.va, buf, llen);
380         }
381     }
382 
383     if (rc) {
384         return -1;
385     }
386     req->cur_desc_offset += llen;
387 
388     return llen;
389 }
390 
391 static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req,
392                                    uint8_t *buf, uint32_t len)
393 {
394     struct srp_direct_buf md;
395     int rc = 0;
396     uint32_t llen, total = 0;
397 
398     DPRINTF("VSCSI: indirect segment 0x%x bytes\n", len);
399 
400     /* While we have data ... */
401     while (len) {
402         rc = vscsi_fetch_desc(s, req, req->cur_desc_num, req->cur_desc_offset, &md);
403         if (rc < 0) {
404             return -1;
405         } else if (rc == 0) {
406             break;
407         }
408 
409         /* Perform transfer */
410         llen = MIN(len, md.len);
411         if (req->writing) { /* writing = to device = reading from memory */
412             rc = spapr_vio_dma_read(&s->vdev, md.va, buf, llen);
413         } else {
414             rc = spapr_vio_dma_write(&s->vdev, md.va, buf, llen);
415         }
416         if (rc) {
417             DPRINTF("VSCSI: spapr_vio_dma_r/w(%d) -> %d\n", req->writing, rc);
418             break;
419         }
420         DPRINTF("VSCSI:     data: %02x %02x %02x %02x...\n",
421                 buf[0], buf[1], buf[2], buf[3]);
422 
423         len -= llen;
424         buf += llen;
425 
426         total += llen;
427 
428         /* Update current position in the current descriptor */
429         req->cur_desc_offset += llen;
430         if (md.len == llen) {
431             /* Go to the next descriptor if the current one finished */
432             ++req->cur_desc_num;
433             req->cur_desc_offset = 0;
434         }
435     }
436 
437     return rc ? -1 : total;
438 }
439 
440 static int vscsi_srp_transfer_data(VSCSIState *s, vscsi_req *req,
441                                    int writing, uint8_t *buf, uint32_t len)
442 {
443     int err = 0;
444 
445     switch (req->dma_fmt) {
446     case SRP_NO_DATA_DESC:
447         DPRINTF("VSCSI: no data desc transfer, skipping 0x%x bytes\n", len);
448         break;
449     case SRP_DATA_DESC_DIRECT:
450         err = vscsi_srp_direct_data(s, req, buf, len);
451         break;
452     case SRP_DATA_DESC_INDIRECT:
453         err = vscsi_srp_indirect_data(s, req, buf, len);
454         break;
455     }
456     return err;
457 }
458 
459 /* Bits from linux srp */
460 static int data_out_desc_size(struct srp_cmd *cmd)
461 {
462     int size = 0;
463     uint8_t fmt = cmd->buf_fmt >> 4;
464 
465     switch (fmt) {
466     case SRP_NO_DATA_DESC:
467         break;
468     case SRP_DATA_DESC_DIRECT:
469         size = sizeof(struct srp_direct_buf);
470         break;
471     case SRP_DATA_DESC_INDIRECT:
472         size = sizeof(struct srp_indirect_buf) +
473             sizeof(struct srp_direct_buf)*cmd->data_out_desc_cnt;
474         break;
475     default:
476         break;
477     }
478     return size;
479 }
480 
481 static int vscsi_preprocess_desc(vscsi_req *req)
482 {
483     struct srp_cmd *cmd = &req->iu.srp.cmd;
484 
485     req->cdb_offset = cmd->add_cdb_len & ~3;
486 
487     if (req->writing) {
488         req->dma_fmt = cmd->buf_fmt >> 4;
489     } else {
490         req->cdb_offset += data_out_desc_size(cmd);
491         req->dma_fmt = cmd->buf_fmt & ((1U << 4) - 1);
492     }
493 
494     switch (req->dma_fmt) {
495     case SRP_NO_DATA_DESC:
496         break;
497     case SRP_DATA_DESC_DIRECT:
498         req->total_desc = req->local_desc = 1;
499         break;
500     case SRP_DATA_DESC_INDIRECT: {
501         struct srp_indirect_buf *ind_tmp = (struct srp_indirect_buf *)
502                 (cmd->add_data + req->cdb_offset);
503 
504         req->total_desc = be32_to_cpu(ind_tmp->table_desc.len) /
505                           sizeof(struct srp_direct_buf);
506         req->local_desc = req->writing ? cmd->data_out_desc_cnt :
507                           cmd->data_in_desc_cnt;
508         break;
509     }
510     default:
511         fprintf(stderr,
512                 "vscsi_preprocess_desc: Unknown format %x\n", req->dma_fmt);
513         return -1;
514     }
515 
516     return 0;
517 }
518 
519 /* Callback to indicate that the SCSI layer has completed a transfer.  */
520 static void vscsi_transfer_data(SCSIRequest *sreq, uint32_t len)
521 {
522     VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
523     vscsi_req *req = sreq->hba_private;
524     uint8_t *buf;
525     int rc = 0;
526 
527     DPRINTF("VSCSI: SCSI xfer complete tag=0x%x len=0x%x, req=%p\n",
528             sreq->tag, len, req);
529     if (req == NULL) {
530         fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
531         return;
532     }
533 
534     if (len) {
535         buf = scsi_req_get_buf(sreq);
536         rc = vscsi_srp_transfer_data(s, req, req->writing, buf, len);
537     }
538     if (rc < 0) {
539         fprintf(stderr, "VSCSI: RDMA error rc=%d!\n", rc);
540         req->dma_error = true;
541         scsi_req_cancel(req->sreq);
542         return;
543     }
544 
545     /* Start next chunk */
546     req->data_len -= rc;
547     scsi_req_continue(sreq);
548 }
549 
550 /* Callback to indicate that the SCSI layer has completed a transfer.  */
551 static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status, size_t resid)
552 {
553     VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
554     vscsi_req *req = sreq->hba_private;
555     int32_t res_in = 0, res_out = 0;
556 
557     DPRINTF("VSCSI: SCSI cmd complete, tag=0x%x status=0x%x, req=%p\n",
558             sreq->tag, status, req);
559     if (req == NULL) {
560         fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
561         return;
562     }
563 
564     if (status == CHECK_CONDITION) {
565         req->senselen = scsi_req_get_sense(req->sreq, req->sense,
566                                            sizeof(req->sense));
567         DPRINTF("VSCSI: Sense data, %d bytes:\n", req->senselen);
568         DPRINTF("       %02x  %02x  %02x  %02x  %02x  %02x  %02x  %02x\n",
569                 req->sense[0], req->sense[1], req->sense[2], req->sense[3],
570                 req->sense[4], req->sense[5], req->sense[6], req->sense[7]);
571         DPRINTF("       %02x  %02x  %02x  %02x  %02x  %02x  %02x  %02x\n",
572                 req->sense[8], req->sense[9], req->sense[10], req->sense[11],
573                 req->sense[12], req->sense[13], req->sense[14], req->sense[15]);
574     }
575 
576     DPRINTF("VSCSI: Command complete err=%d\n", status);
577     if (status == 0) {
578         /* We handle overflows, not underflows for normal commands,
579          * but hopefully nobody cares
580          */
581         if (req->writing) {
582             res_out = req->data_len;
583         } else {
584             res_in = req->data_len;
585         }
586     }
587     vscsi_send_rsp(s, req, status, res_in, res_out);
588     vscsi_put_req(req);
589 }
590 
591 static void vscsi_request_cancelled(SCSIRequest *sreq)
592 {
593     vscsi_req *req = sreq->hba_private;
594 
595     if (req->dma_error) {
596         VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
597 
598         vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
599         vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
600     }
601     vscsi_put_req(req);
602 }
603 
604 static const VMStateDescription vmstate_spapr_vscsi_req = {
605     .name = "spapr_vscsi_req",
606     .version_id = 1,
607     .minimum_version_id = 1,
608     .fields = (VMStateField[]) {
609         VMSTATE_BUFFER(crq.raw, vscsi_req),
610         VMSTATE_BUFFER(iu.srp.reserved, vscsi_req),
611         VMSTATE_UINT32(qtag, vscsi_req),
612         VMSTATE_BOOL(active, vscsi_req),
613         VMSTATE_UINT32(data_len, vscsi_req),
614         VMSTATE_BOOL(writing, vscsi_req),
615         VMSTATE_UINT32(senselen, vscsi_req),
616         VMSTATE_BUFFER(sense, vscsi_req),
617         VMSTATE_UINT8(dma_fmt, vscsi_req),
618         VMSTATE_UINT16(local_desc, vscsi_req),
619         VMSTATE_UINT16(total_desc, vscsi_req),
620         VMSTATE_UINT16(cdb_offset, vscsi_req),
621       /*Restart SCSI request from the beginning for now */
622       /*VMSTATE_UINT16(cur_desc_num, vscsi_req),
623         VMSTATE_UINT16(cur_desc_offset, vscsi_req),*/
624         VMSTATE_END_OF_LIST()
625     },
626 };
627 
628 static void vscsi_save_request(QEMUFile *f, SCSIRequest *sreq)
629 {
630     vscsi_req *req = sreq->hba_private;
631     assert(req->active);
632 
633     vmstate_save_state(f, &vmstate_spapr_vscsi_req, req);
634 
635     DPRINTF("VSCSI: saving tag=%u, current desc#%d, offset=%x\n",
636             req->qtag, req->cur_desc_num, req->cur_desc_offset);
637 }
638 
639 static void *vscsi_load_request(QEMUFile *f, SCSIRequest *sreq)
640 {
641     SCSIBus *bus = sreq->bus;
642     VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(bus->qbus.parent);
643     vscsi_req *req;
644     int rc;
645 
646     assert(sreq->tag < VSCSI_REQ_LIMIT);
647     req = &s->reqs[sreq->tag];
648     assert(!req->active);
649 
650     memset(req, 0, sizeof(*req));
651     rc = vmstate_load_state(f, &vmstate_spapr_vscsi_req, req, 1);
652     if (rc) {
653         fprintf(stderr, "VSCSI: failed loading request tag#%u\n", sreq->tag);
654         return NULL;
655     }
656     assert(req->active);
657 
658     req->sreq = scsi_req_ref(sreq);
659 
660     DPRINTF("VSCSI: restoring tag=%u, current desc#%d, offset=%x\n",
661             req->qtag, req->cur_desc_num, req->cur_desc_offset);
662 
663     return req;
664 }
665 
666 static void vscsi_process_login(VSCSIState *s, vscsi_req *req)
667 {
668     union viosrp_iu *iu = &req->iu;
669     struct srp_login_rsp *rsp = &iu->srp.login_rsp;
670     uint64_t tag = iu->srp.rsp.tag;
671 
672     DPRINTF("VSCSI: Got login, sendin response !\n");
673 
674     /* TODO handle case that requested size is wrong and
675      * buffer format is wrong
676      */
677     memset(iu, 0, sizeof(struct srp_login_rsp));
678     rsp->opcode = SRP_LOGIN_RSP;
679     /* Don't advertise quite as many request as we support to
680      * keep room for management stuff etc...
681      */
682     rsp->req_lim_delta = cpu_to_be32(VSCSI_REQ_LIMIT-2);
683     rsp->tag = tag;
684     rsp->max_it_iu_len = cpu_to_be32(sizeof(union srp_iu));
685     rsp->max_ti_iu_len = cpu_to_be32(sizeof(union srp_iu));
686     /* direct and indirect */
687     rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
688 
689     vscsi_send_iu(s, req, sizeof(*rsp), VIOSRP_SRP_FORMAT);
690 }
691 
692 static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
693 {
694     uint8_t *cdb = req->iu.srp.cmd.cdb;
695     uint8_t resp_data[36];
696     int rc, len, alen;
697 
698     /* We dont do EVPD. Also check that page_code is 0 */
699     if ((cdb[1] & 0x01) || cdb[2] != 0) {
700         /* Send INVALID FIELD IN CDB */
701         vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0);
702         vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
703         return;
704     }
705     alen = cdb[3];
706     alen = (alen << 8) | cdb[4];
707     len = MIN(alen, 36);
708 
709     /* Fake up inquiry using PQ=3 */
710     memset(resp_data, 0, 36);
711     resp_data[0] = 0x7f;   /* Not capable of supporting a device here */
712     resp_data[2] = 0x06;   /* SPS-4 */
713     resp_data[3] = 0x02;   /* Resp data format */
714     resp_data[4] = 36 - 5; /* Additional length */
715     resp_data[7] = 0x10;   /* Sync transfers */
716     memcpy(&resp_data[16], "QEMU EMPTY      ", 16);
717     memcpy(&resp_data[8], "QEMU    ", 8);
718 
719     req->writing = 0;
720     vscsi_preprocess_desc(req);
721     rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
722     if (rc < 0) {
723         vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
724         vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
725     } else {
726         vscsi_send_rsp(s, req, 0, 36 - rc, 0);
727     }
728 }
729 
730 static void vscsi_report_luns(VSCSIState *s, vscsi_req *req)
731 {
732     BusChild *kid;
733     int i, len, n, rc;
734     uint8_t *resp_data;
735     bool found_lun0;
736 
737     n = 0;
738     found_lun0 = false;
739     QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
740         SCSIDevice *dev = SCSI_DEVICE(kid->child);
741 
742         n += 8;
743         if (dev->channel == 0 && dev->id == 0 && dev->lun == 0) {
744             found_lun0 = true;
745         }
746     }
747     if (!found_lun0) {
748         n += 8;
749     }
750     len = n+8;
751 
752     resp_data = g_malloc0(len);
753     memset(resp_data, 0, 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 int spapr_vscsi_init(VIOsPAPRDevice *dev)
1216 {
1217     VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
1218     Error *err = NULL;
1219 
1220     dev->crq.SendFunc = vscsi_do_crq;
1221 
1222     scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev),
1223                  &vscsi_scsi_info, NULL);
1224     if (!dev->qdev.hotplugged) {
1225         scsi_bus_legacy_handle_cmdline(&s->bus, &err);
1226         if (err != NULL) {
1227             error_free(err);
1228             return -1;
1229         }
1230     }
1231 
1232     return 0;
1233 }
1234 
1235 void spapr_vscsi_create(VIOsPAPRBus *bus)
1236 {
1237     DeviceState *dev;
1238 
1239     dev = qdev_create(&bus->bus, "spapr-vscsi");
1240 
1241     qdev_init_nofail(dev);
1242 }
1243 
1244 static int spapr_vscsi_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
1245 {
1246     int ret;
1247 
1248     ret = fdt_setprop_cell(fdt, node_off, "#address-cells", 2);
1249     if (ret < 0) {
1250         return ret;
1251     }
1252 
1253     ret = fdt_setprop_cell(fdt, node_off, "#size-cells", 0);
1254     if (ret < 0) {
1255         return ret;
1256     }
1257 
1258     return 0;
1259 }
1260 
1261 static Property spapr_vscsi_properties[] = {
1262     DEFINE_SPAPR_PROPERTIES(VSCSIState, vdev),
1263     DEFINE_PROP_END_OF_LIST(),
1264 };
1265 
1266 static const VMStateDescription vmstate_spapr_vscsi = {
1267     .name = "spapr_vscsi",
1268     .version_id = 1,
1269     .minimum_version_id = 1,
1270     .fields = (VMStateField[]) {
1271         VMSTATE_SPAPR_VIO(vdev, VSCSIState),
1272         /* VSCSI state */
1273         /* ???? */
1274 
1275         VMSTATE_END_OF_LIST()
1276     },
1277 };
1278 
1279 static void spapr_vscsi_class_init(ObjectClass *klass, void *data)
1280 {
1281     DeviceClass *dc = DEVICE_CLASS(klass);
1282     VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
1283 
1284     k->init = spapr_vscsi_init;
1285     k->reset = spapr_vscsi_reset;
1286     k->devnode = spapr_vscsi_devnode;
1287     k->dt_name = "v-scsi";
1288     k->dt_type = "vscsi";
1289     k->dt_compatible = "IBM,v-scsi";
1290     k->signal_mask = 0x00000001;
1291     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1292     dc->props = spapr_vscsi_properties;
1293     k->rtce_window_size = 0x10000000;
1294     dc->vmsd = &vmstate_spapr_vscsi;
1295 }
1296 
1297 static const TypeInfo spapr_vscsi_info = {
1298     .name          = TYPE_VIO_SPAPR_VSCSI_DEVICE,
1299     .parent        = TYPE_VIO_SPAPR_DEVICE,
1300     .instance_size = sizeof(VSCSIState),
1301     .class_init    = spapr_vscsi_class_init,
1302 };
1303 
1304 static void spapr_vscsi_register_types(void)
1305 {
1306     type_register_static(&spapr_vscsi_info);
1307 }
1308 
1309 type_init(spapr_vscsi_register_types)
1310