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