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