monitor/qmp: Update comment for commit 4eaca8de268
[qemu/armbru.git] / hw / scsi / spapr_vscsi.c
blob2120ceb051b5a75bb9bb55e40c9b33f3635606d5
1 /*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
4 * PAPR Virtual SCSI, aka ibmvscsi
6 * Copyright (c) 2010,2011 Benjamin Herrenschmidt, IBM Corporation.
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * TODO:
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)
35 #include "qemu/osdep.h"
36 #include "qemu/module.h"
37 #include "cpu.h"
38 #include "hw/scsi/scsi.h"
39 #include "migration/vmstate.h"
40 #include "scsi/constants.h"
41 #include "srp.h"
42 #include "hw/ppc/spapr.h"
43 #include "hw/ppc/spapr_vio.h"
44 #include "hw/qdev-properties.h"
45 #include "viosrp.h"
46 #include "trace.h"
48 #include <libfdt.h>
51 * Virtual SCSI device
54 /* Random numbers */
55 #define VSCSI_MAX_SECTORS 4096
56 #define VSCSI_REQ_LIMIT 24
58 #define SRP_RSP_SENSE_DATA_LEN 18
60 #define SRP_REPORT_LUNS_WLUN 0xc10100000000000ULL
62 typedef union vscsi_crq {
63 struct viosrp_crq s;
64 uint8_t raw[16];
65 } vscsi_crq;
67 typedef struct vscsi_req {
68 vscsi_crq crq;
69 union viosrp_iu iu;
71 /* SCSI request tracking */
72 SCSIRequest *sreq;
73 uint32_t qtag; /* qemu tag != srp tag */
74 bool active;
75 bool writing;
76 bool dma_error;
77 uint32_t data_len;
78 uint32_t senselen;
79 uint8_t sense[SCSI_SENSE_BUF_SIZE];
81 /* RDMA related bits */
82 uint8_t dma_fmt;
83 uint16_t local_desc;
84 uint16_t total_desc;
85 uint16_t cdb_offset;
86 uint16_t cur_desc_num;
87 uint16_t cur_desc_offset;
88 } vscsi_req;
90 #define TYPE_VIO_SPAPR_VSCSI_DEVICE "spapr-vscsi"
91 #define VIO_SPAPR_VSCSI_DEVICE(obj) \
92 OBJECT_CHECK(VSCSIState, (obj), TYPE_VIO_SPAPR_VSCSI_DEVICE)
94 typedef struct {
95 SpaprVioDevice vdev;
96 SCSIBus bus;
97 vscsi_req reqs[VSCSI_REQ_LIMIT];
98 } VSCSIState;
100 static struct vscsi_req *vscsi_get_req(VSCSIState *s)
102 vscsi_req *req;
103 int i;
105 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
106 req = &s->reqs[i];
107 if (!req->active) {
108 memset(req, 0, sizeof(*req));
109 req->qtag = i;
110 req->active = 1;
111 return req;
114 return NULL;
117 static struct vscsi_req *vscsi_find_req(VSCSIState *s, uint64_t srp_tag)
119 vscsi_req *req;
120 int i;
122 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
123 req = &s->reqs[i];
124 if (req->iu.srp.cmd.tag == srp_tag) {
125 return req;
128 return NULL;
131 static void vscsi_put_req(vscsi_req *req)
133 if (req->sreq != NULL) {
134 scsi_req_unref(req->sreq);
136 req->sreq = NULL;
137 req->active = 0;
140 static SCSIDevice *vscsi_device_find(SCSIBus *bus, uint64_t srp_lun, int *lun)
142 int channel = 0, id = 0;
144 retry:
145 switch (srp_lun >> 62) {
146 case 0:
147 if ((srp_lun >> 56) != 0) {
148 channel = (srp_lun >> 56) & 0x3f;
149 id = (srp_lun >> 48) & 0xff;
150 srp_lun <<= 16;
151 goto retry;
153 *lun = (srp_lun >> 48) & 0xff;
154 break;
156 case 1:
157 *lun = (srp_lun >> 48) & 0x3fff;
158 break;
159 case 2:
160 channel = (srp_lun >> 53) & 0x7;
161 id = (srp_lun >> 56) & 0x3f;
162 *lun = (srp_lun >> 48) & 0x1f;
163 break;
164 case 3:
165 *lun = -1;
166 return NULL;
167 default:
168 abort();
171 return scsi_device_find(bus, channel, id, *lun);
174 static int vscsi_send_iu(VSCSIState *s, vscsi_req *req,
175 uint64_t length, uint8_t format)
177 long rc, rc1;
179 /* First copy the SRP */
180 rc = spapr_vio_dma_write(&s->vdev, req->crq.s.IU_data_ptr,
181 &req->iu, length);
182 if (rc) {
183 fprintf(stderr, "vscsi_send_iu: DMA write failure !\n");
186 req->crq.s.valid = 0x80;
187 req->crq.s.format = format;
188 req->crq.s.reserved = 0x00;
189 req->crq.s.timeout = cpu_to_be16(0x0000);
190 req->crq.s.IU_length = cpu_to_be16(length);
191 req->crq.s.IU_data_ptr = req->iu.srp.rsp.tag; /* right byte order */
193 if (rc == 0) {
194 req->crq.s.status = VIOSRP_OK;
195 } else {
196 req->crq.s.status = VIOSRP_ADAPTER_FAIL;
199 rc1 = spapr_vio_send_crq(&s->vdev, req->crq.raw);
200 if (rc1) {
201 fprintf(stderr, "vscsi_send_iu: Error sending response\n");
202 return rc1;
205 return rc;
208 static void vscsi_makeup_sense(VSCSIState *s, vscsi_req *req,
209 uint8_t key, uint8_t asc, uint8_t ascq)
211 req->senselen = SRP_RSP_SENSE_DATA_LEN;
213 /* Valid bit and 'current errors' */
214 req->sense[0] = (0x1 << 7 | 0x70);
215 /* Sense key */
216 req->sense[2] = key;
217 /* Additional sense length */
218 req->sense[7] = 0xa; /* 10 bytes */
219 /* Additional sense code */
220 req->sense[12] = asc;
221 req->sense[13] = ascq;
224 static int vscsi_send_rsp(VSCSIState *s, vscsi_req *req,
225 uint8_t status, int32_t res_in, int32_t res_out)
227 union viosrp_iu *iu = &req->iu;
228 uint64_t tag = iu->srp.rsp.tag;
229 int total_len = sizeof(iu->srp.rsp);
230 uint8_t sol_not = iu->srp.cmd.sol_not;
232 trace_spapr_vscsi_send_rsp(status, res_in, res_out);
234 memset(iu, 0, sizeof(struct srp_rsp));
235 iu->srp.rsp.opcode = SRP_RSP;
236 iu->srp.rsp.req_lim_delta = cpu_to_be32(1);
237 iu->srp.rsp.tag = tag;
239 /* Handle residuals */
240 if (res_in < 0) {
241 iu->srp.rsp.flags |= SRP_RSP_FLAG_DIUNDER;
242 res_in = -res_in;
243 } else if (res_in) {
244 iu->srp.rsp.flags |= SRP_RSP_FLAG_DIOVER;
246 if (res_out < 0) {
247 iu->srp.rsp.flags |= SRP_RSP_FLAG_DOUNDER;
248 res_out = -res_out;
249 } else if (res_out) {
250 iu->srp.rsp.flags |= SRP_RSP_FLAG_DOOVER;
252 iu->srp.rsp.data_in_res_cnt = cpu_to_be32(res_in);
253 iu->srp.rsp.data_out_res_cnt = cpu_to_be32(res_out);
255 /* We don't do response data */
256 /* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */
257 iu->srp.rsp.resp_data_len = cpu_to_be32(0);
259 /* Handle success vs. failure */
260 iu->srp.rsp.status = status;
261 if (status) {
262 iu->srp.rsp.sol_not = (sol_not & 0x04) >> 2;
263 if (req->senselen) {
264 req->iu.srp.rsp.flags |= SRP_RSP_FLAG_SNSVALID;
265 req->iu.srp.rsp.sense_data_len = cpu_to_be32(req->senselen);
266 memcpy(req->iu.srp.rsp.data, req->sense, req->senselen);
267 total_len += req->senselen;
269 } else {
270 iu->srp.rsp.sol_not = (sol_not & 0x02) >> 1;
273 vscsi_send_iu(s, req, total_len, VIOSRP_SRP_FORMAT);
274 return 0;
277 static inline struct srp_direct_buf vscsi_swap_desc(struct srp_direct_buf desc)
279 desc.va = be64_to_cpu(desc.va);
280 desc.len = be32_to_cpu(desc.len);
281 return desc;
284 static int vscsi_fetch_desc(VSCSIState *s, struct vscsi_req *req,
285 unsigned n, unsigned buf_offset,
286 struct srp_direct_buf *ret)
288 struct srp_cmd *cmd = &req->iu.srp.cmd;
290 switch (req->dma_fmt) {
291 case SRP_NO_DATA_DESC: {
292 trace_spapr_vscsi_fetch_desc_no_data();
293 return 0;
295 case SRP_DATA_DESC_DIRECT: {
296 memcpy(ret, cmd->add_data + req->cdb_offset, sizeof(*ret));
297 assert(req->cur_desc_num == 0);
298 trace_spapr_vscsi_fetch_desc_direct();
299 break;
301 case SRP_DATA_DESC_INDIRECT: {
302 struct srp_indirect_buf *tmp = (struct srp_indirect_buf *)
303 (cmd->add_data + req->cdb_offset);
304 if (n < req->local_desc) {
305 *ret = tmp->desc_list[n];
306 trace_spapr_vscsi_fetch_desc_indirect(req->qtag, n,
307 req->local_desc);
308 } else if (n < req->total_desc) {
309 int rc;
310 struct srp_direct_buf tbl_desc = vscsi_swap_desc(tmp->table_desc);
311 unsigned desc_offset = n * sizeof(struct srp_direct_buf);
313 if (desc_offset >= tbl_desc.len) {
314 trace_spapr_vscsi_fetch_desc_out_of_range(n, desc_offset);
315 return -1;
317 rc = spapr_vio_dma_read(&s->vdev, tbl_desc.va + desc_offset,
318 ret, sizeof(struct srp_direct_buf));
319 if (rc) {
320 trace_spapr_vscsi_fetch_desc_dma_read_error(rc);
321 return -1;
323 trace_spapr_vscsi_fetch_desc_indirect_seg_ext(req->qtag, n,
324 req->total_desc,
325 tbl_desc.va,
326 tbl_desc.len);
327 } else {
328 trace_spapr_vscsi_fetch_desc_out_of_desc();
329 return 0;
331 break;
333 default:
334 fprintf(stderr, "VSCSI: Unknown format %x\n", req->dma_fmt);
335 return -1;
338 *ret = vscsi_swap_desc(*ret);
339 if (buf_offset > ret->len) {
340 trace_spapr_vscsi_fetch_desc_out_of_desc_boundary(buf_offset,
341 req->cur_desc_num,
342 ret->len);
343 return -1;
345 ret->va += buf_offset;
346 ret->len -= buf_offset;
348 trace_spapr_vscsi_fetch_desc_done(req->cur_desc_num, req->cur_desc_offset,
349 ret->va, ret->len);
351 return ret->len ? 1 : 0;
354 static int vscsi_srp_direct_data(VSCSIState *s, vscsi_req *req,
355 uint8_t *buf, uint32_t len)
357 struct srp_direct_buf md;
358 uint32_t llen;
359 int rc = 0;
361 rc = vscsi_fetch_desc(s, req, req->cur_desc_num, req->cur_desc_offset, &md);
362 if (rc < 0) {
363 return -1;
364 } else if (rc == 0) {
365 return 0;
368 llen = MIN(len, md.len);
369 if (llen) {
370 if (req->writing) { /* writing = to device = reading from memory */
371 rc = spapr_vio_dma_read(&s->vdev, md.va, buf, llen);
372 } else {
373 rc = spapr_vio_dma_write(&s->vdev, md.va, buf, llen);
377 if (rc) {
378 return -1;
380 req->cur_desc_offset += llen;
382 return llen;
385 static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req,
386 uint8_t *buf, uint32_t len)
388 struct srp_direct_buf md;
389 int rc = 0;
390 uint32_t llen, total = 0;
392 trace_spapr_vscsi_srp_indirect_data(len);
394 /* While we have data ... */
395 while (len) {
396 rc = vscsi_fetch_desc(s, req, req->cur_desc_num, req->cur_desc_offset, &md);
397 if (rc < 0) {
398 return -1;
399 } else if (rc == 0) {
400 break;
403 /* Perform transfer */
404 llen = MIN(len, md.len);
405 if (req->writing) { /* writing = to device = reading from memory */
406 rc = spapr_vio_dma_read(&s->vdev, md.va, buf, llen);
407 } else {
408 rc = spapr_vio_dma_write(&s->vdev, md.va, buf, llen);
410 if (rc) {
411 trace_spapr_vscsi_srp_indirect_data_rw(req->writing, rc);
412 break;
414 trace_spapr_vscsi_srp_indirect_data_buf(buf[0], buf[1], buf[2], buf[3]);
416 len -= llen;
417 buf += llen;
419 total += llen;
421 /* Update current position in the current descriptor */
422 req->cur_desc_offset += llen;
423 if (md.len == llen) {
424 /* Go to the next descriptor if the current one finished */
425 ++req->cur_desc_num;
426 req->cur_desc_offset = 0;
430 return rc ? -1 : total;
433 static int vscsi_srp_transfer_data(VSCSIState *s, vscsi_req *req,
434 int writing, uint8_t *buf, uint32_t len)
436 int err = 0;
438 switch (req->dma_fmt) {
439 case SRP_NO_DATA_DESC:
440 trace_spapr_vscsi_srp_transfer_data(len);
441 break;
442 case SRP_DATA_DESC_DIRECT:
443 err = vscsi_srp_direct_data(s, req, buf, len);
444 break;
445 case SRP_DATA_DESC_INDIRECT:
446 err = vscsi_srp_indirect_data(s, req, buf, len);
447 break;
449 return err;
452 /* Bits from linux srp */
453 static int data_out_desc_size(struct srp_cmd *cmd)
455 int size = 0;
456 uint8_t fmt = cmd->buf_fmt >> 4;
458 switch (fmt) {
459 case SRP_NO_DATA_DESC:
460 break;
461 case SRP_DATA_DESC_DIRECT:
462 size = sizeof(struct srp_direct_buf);
463 break;
464 case SRP_DATA_DESC_INDIRECT:
465 size = sizeof(struct srp_indirect_buf) +
466 sizeof(struct srp_direct_buf)*cmd->data_out_desc_cnt;
467 break;
468 default:
469 break;
471 return size;
474 static int vscsi_preprocess_desc(vscsi_req *req)
476 struct srp_cmd *cmd = &req->iu.srp.cmd;
478 req->cdb_offset = cmd->add_cdb_len & ~3;
480 if (req->writing) {
481 req->dma_fmt = cmd->buf_fmt >> 4;
482 } else {
483 req->cdb_offset += data_out_desc_size(cmd);
484 req->dma_fmt = cmd->buf_fmt & ((1U << 4) - 1);
487 switch (req->dma_fmt) {
488 case SRP_NO_DATA_DESC:
489 break;
490 case SRP_DATA_DESC_DIRECT:
491 req->total_desc = req->local_desc = 1;
492 break;
493 case SRP_DATA_DESC_INDIRECT: {
494 struct srp_indirect_buf *ind_tmp = (struct srp_indirect_buf *)
495 (cmd->add_data + req->cdb_offset);
497 req->total_desc = be32_to_cpu(ind_tmp->table_desc.len) /
498 sizeof(struct srp_direct_buf);
499 req->local_desc = req->writing ? cmd->data_out_desc_cnt :
500 cmd->data_in_desc_cnt;
501 break;
503 default:
504 fprintf(stderr,
505 "vscsi_preprocess_desc: Unknown format %x\n", req->dma_fmt);
506 return -1;
509 return 0;
512 /* Callback to indicate that the SCSI layer has completed a transfer. */
513 static void vscsi_transfer_data(SCSIRequest *sreq, uint32_t len)
515 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
516 vscsi_req *req = sreq->hba_private;
517 uint8_t *buf;
518 int rc = 0;
520 trace_spapr_vscsi_transfer_data(sreq->tag, len, req);
521 if (req == NULL) {
522 fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
523 return;
526 if (len) {
527 buf = scsi_req_get_buf(sreq);
528 rc = vscsi_srp_transfer_data(s, req, req->writing, buf, len);
530 if (rc < 0) {
531 fprintf(stderr, "VSCSI: RDMA error rc=%d!\n", rc);
532 req->dma_error = true;
533 scsi_req_cancel(req->sreq);
534 return;
537 /* Start next chunk */
538 req->data_len -= rc;
539 scsi_req_continue(sreq);
542 /* Callback to indicate that the SCSI layer has completed a transfer. */
543 static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status, size_t resid)
545 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
546 vscsi_req *req = sreq->hba_private;
547 int32_t res_in = 0, res_out = 0;
549 trace_spapr_vscsi_command_complete(sreq->tag, status, req);
550 if (req == NULL) {
551 fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
552 return;
555 if (status == CHECK_CONDITION) {
556 req->senselen = scsi_req_get_sense(req->sreq, req->sense,
557 sizeof(req->sense));
558 trace_spapr_vscsi_command_complete_sense_data1(req->senselen,
559 req->sense[0], req->sense[1], req->sense[2], req->sense[3],
560 req->sense[4], req->sense[5], req->sense[6], req->sense[7]);
561 trace_spapr_vscsi_command_complete_sense_data2(
562 req->sense[8], req->sense[9], req->sense[10], req->sense[11],
563 req->sense[12], req->sense[13], req->sense[14], req->sense[15]);
566 trace_spapr_vscsi_command_complete_status(status);
567 if (status == 0) {
568 /* We handle overflows, not underflows for normal commands,
569 * but hopefully nobody cares
571 if (req->writing) {
572 res_out = req->data_len;
573 } else {
574 res_in = req->data_len;
577 vscsi_send_rsp(s, req, status, res_in, res_out);
578 vscsi_put_req(req);
581 static void vscsi_request_cancelled(SCSIRequest *sreq)
583 vscsi_req *req = sreq->hba_private;
585 if (req->dma_error) {
586 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
588 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
589 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
591 vscsi_put_req(req);
594 static const VMStateDescription vmstate_spapr_vscsi_req = {
595 .name = "spapr_vscsi_req",
596 .version_id = 1,
597 .minimum_version_id = 1,
598 .fields = (VMStateField[]) {
599 VMSTATE_BUFFER(crq.raw, vscsi_req),
600 VMSTATE_BUFFER(iu.srp.reserved, vscsi_req),
601 VMSTATE_UINT32(qtag, vscsi_req),
602 VMSTATE_BOOL(active, vscsi_req),
603 VMSTATE_UINT32(data_len, vscsi_req),
604 VMSTATE_BOOL(writing, vscsi_req),
605 VMSTATE_UINT32(senselen, vscsi_req),
606 VMSTATE_BUFFER(sense, vscsi_req),
607 VMSTATE_UINT8(dma_fmt, vscsi_req),
608 VMSTATE_UINT16(local_desc, vscsi_req),
609 VMSTATE_UINT16(total_desc, vscsi_req),
610 VMSTATE_UINT16(cdb_offset, vscsi_req),
611 /*Restart SCSI request from the beginning for now */
612 /*VMSTATE_UINT16(cur_desc_num, vscsi_req),
613 VMSTATE_UINT16(cur_desc_offset, vscsi_req),*/
614 VMSTATE_END_OF_LIST()
618 static void vscsi_save_request(QEMUFile *f, SCSIRequest *sreq)
620 vscsi_req *req = sreq->hba_private;
621 assert(req->active);
623 vmstate_save_state(f, &vmstate_spapr_vscsi_req, req, NULL);
625 trace_spapr_vscsi_save_request(req->qtag, req->cur_desc_num,
626 req->cur_desc_offset);
629 static void *vscsi_load_request(QEMUFile *f, SCSIRequest *sreq)
631 SCSIBus *bus = sreq->bus;
632 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(bus->qbus.parent);
633 vscsi_req *req;
634 int rc;
636 assert(sreq->tag < VSCSI_REQ_LIMIT);
637 req = &s->reqs[sreq->tag];
638 assert(!req->active);
640 memset(req, 0, sizeof(*req));
641 rc = vmstate_load_state(f, &vmstate_spapr_vscsi_req, req, 1);
642 if (rc) {
643 fprintf(stderr, "VSCSI: failed loading request tag#%u\n", sreq->tag);
644 return NULL;
646 assert(req->active);
648 req->sreq = scsi_req_ref(sreq);
650 trace_spapr_vscsi_load_request(req->qtag, req->cur_desc_num,
651 req->cur_desc_offset);
653 return req;
656 static void vscsi_process_login(VSCSIState *s, vscsi_req *req)
658 union viosrp_iu *iu = &req->iu;
659 struct srp_login_rsp *rsp = &iu->srp.login_rsp;
660 uint64_t tag = iu->srp.rsp.tag;
662 trace_spapr_vscsi_process_login();
664 /* TODO handle case that requested size is wrong and
665 * buffer format is wrong
667 memset(iu, 0, sizeof(struct srp_login_rsp));
668 rsp->opcode = SRP_LOGIN_RSP;
669 /* Don't advertise quite as many request as we support to
670 * keep room for management stuff etc...
672 rsp->req_lim_delta = cpu_to_be32(VSCSI_REQ_LIMIT-2);
673 rsp->tag = tag;
674 rsp->max_it_iu_len = cpu_to_be32(sizeof(union srp_iu));
675 rsp->max_ti_iu_len = cpu_to_be32(sizeof(union srp_iu));
676 /* direct and indirect */
677 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
679 vscsi_send_iu(s, req, sizeof(*rsp), VIOSRP_SRP_FORMAT);
682 static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
684 uint8_t *cdb = req->iu.srp.cmd.cdb;
685 uint8_t resp_data[36];
686 int rc, len, alen;
688 /* We don't do EVPD. Also check that page_code is 0 */
689 if ((cdb[1] & 0x01) || cdb[2] != 0) {
690 /* Send INVALID FIELD IN CDB */
691 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0);
692 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
693 return;
695 alen = cdb[3];
696 alen = (alen << 8) | cdb[4];
697 len = MIN(alen, 36);
699 /* Fake up inquiry using PQ=3 */
700 memset(resp_data, 0, 36);
701 resp_data[0] = 0x7f; /* Not capable of supporting a device here */
702 resp_data[2] = 0x06; /* SPS-4 */
703 resp_data[3] = 0x02; /* Resp data format */
704 resp_data[4] = 36 - 5; /* Additional length */
705 resp_data[7] = 0x10; /* Sync transfers */
706 memcpy(&resp_data[16], "QEMU EMPTY ", 16);
707 memcpy(&resp_data[8], "QEMU ", 8);
709 req->writing = 0;
710 vscsi_preprocess_desc(req);
711 rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
712 if (rc < 0) {
713 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
714 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
715 } else {
716 vscsi_send_rsp(s, req, 0, 36 - rc, 0);
720 static void vscsi_report_luns(VSCSIState *s, vscsi_req *req)
722 BusChild *kid;
723 int i, len, n, rc;
724 uint8_t *resp_data;
725 bool found_lun0;
727 n = 0;
728 found_lun0 = false;
729 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
730 SCSIDevice *dev = SCSI_DEVICE(kid->child);
732 n += 8;
733 if (dev->channel == 0 && dev->id == 0 && dev->lun == 0) {
734 found_lun0 = true;
737 if (!found_lun0) {
738 n += 8;
740 len = n+8;
742 resp_data = g_malloc0(len);
743 stl_be_p(resp_data, n);
744 i = found_lun0 ? 8 : 16;
745 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
746 DeviceState *qdev = kid->child;
747 SCSIDevice *dev = SCSI_DEVICE(qdev);
749 if (dev->id == 0 && dev->channel == 0) {
750 resp_data[i] = 0; /* Use simple LUN for 0 (SAM5 4.7.7.1) */
751 } else {
752 resp_data[i] = (2 << 6); /* Otherwise LUN addressing (4.7.7.4) */
754 resp_data[i] |= dev->id;
755 resp_data[i+1] = (dev->channel << 5);
756 resp_data[i+1] |= dev->lun;
757 i += 8;
760 vscsi_preprocess_desc(req);
761 rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
762 g_free(resp_data);
763 if (rc < 0) {
764 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
765 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
766 } else {
767 vscsi_send_rsp(s, req, 0, len - rc, 0);
771 static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req)
773 union srp_iu *srp = &req->iu.srp;
774 SCSIDevice *sdev;
775 int n, lun;
777 if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN)
778 && srp->cmd.cdb[0] == REPORT_LUNS) {
779 vscsi_report_luns(s, req);
780 return 0;
783 sdev = vscsi_device_find(&s->bus, be64_to_cpu(srp->cmd.lun), &lun);
784 if (!sdev) {
785 trace_spapr_vscsi_queue_cmd_no_drive(be64_to_cpu(srp->cmd.lun));
786 if (srp->cmd.cdb[0] == INQUIRY) {
787 vscsi_inquiry_no_target(s, req);
788 } else {
789 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0x00);
790 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
791 } return 1;
794 req->sreq = scsi_req_new(sdev, req->qtag, lun, srp->cmd.cdb, req);
795 n = scsi_req_enqueue(req->sreq);
797 trace_spapr_vscsi_queue_cmd(req->qtag, srp->cmd.cdb[0],
798 scsi_command_name(srp->cmd.cdb[0]), lun, n);
800 if (n) {
801 /* Transfer direction must be set before preprocessing the
802 * descriptors
804 req->writing = (n < 1);
806 /* Preprocess RDMA descriptors */
807 vscsi_preprocess_desc(req);
809 /* Get transfer direction and initiate transfer */
810 if (n > 0) {
811 req->data_len = n;
812 } else if (n < 0) {
813 req->data_len = -n;
815 scsi_req_continue(req->sreq);
817 /* Don't touch req here, it may have been recycled already */
819 return 0;
822 static int vscsi_process_tsk_mgmt(VSCSIState *s, vscsi_req *req)
824 union viosrp_iu *iu = &req->iu;
825 vscsi_req *tmpreq;
826 int i, lun = 0, resp = SRP_TSK_MGMT_COMPLETE;
827 SCSIDevice *d;
828 uint64_t tag = iu->srp.rsp.tag;
829 uint8_t sol_not = iu->srp.cmd.sol_not;
831 fprintf(stderr, "vscsi_process_tsk_mgmt %02x\n",
832 iu->srp.tsk_mgmt.tsk_mgmt_func);
834 d = vscsi_device_find(&s->bus, be64_to_cpu(req->iu.srp.tsk_mgmt.lun), &lun);
835 if (!d) {
836 resp = SRP_TSK_MGMT_FIELDS_INVALID;
837 } else {
838 switch (iu->srp.tsk_mgmt.tsk_mgmt_func) {
839 case SRP_TSK_ABORT_TASK:
840 if (d->lun != lun) {
841 resp = SRP_TSK_MGMT_FIELDS_INVALID;
842 break;
845 tmpreq = vscsi_find_req(s, req->iu.srp.tsk_mgmt.task_tag);
846 if (tmpreq && tmpreq->sreq) {
847 assert(tmpreq->sreq->hba_private);
848 scsi_req_cancel(tmpreq->sreq);
850 break;
852 case SRP_TSK_LUN_RESET:
853 if (d->lun != lun) {
854 resp = SRP_TSK_MGMT_FIELDS_INVALID;
855 break;
858 qdev_reset_all(&d->qdev);
859 break;
861 case SRP_TSK_ABORT_TASK_SET:
862 case SRP_TSK_CLEAR_TASK_SET:
863 if (d->lun != lun) {
864 resp = SRP_TSK_MGMT_FIELDS_INVALID;
865 break;
868 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
869 tmpreq = &s->reqs[i];
870 if (tmpreq->iu.srp.cmd.lun != req->iu.srp.tsk_mgmt.lun) {
871 continue;
873 if (!tmpreq->active || !tmpreq->sreq) {
874 continue;
876 assert(tmpreq->sreq->hba_private);
877 scsi_req_cancel(tmpreq->sreq);
879 break;
881 case SRP_TSK_CLEAR_ACA:
882 resp = SRP_TSK_MGMT_NOT_SUPPORTED;
883 break;
885 default:
886 resp = SRP_TSK_MGMT_FIELDS_INVALID;
887 break;
891 /* Compose the response here as */
892 memset(iu, 0, sizeof(struct srp_rsp) + 4);
893 iu->srp.rsp.opcode = SRP_RSP;
894 iu->srp.rsp.req_lim_delta = cpu_to_be32(1);
895 iu->srp.rsp.tag = tag;
896 iu->srp.rsp.flags |= SRP_RSP_FLAG_RSPVALID;
897 iu->srp.rsp.resp_data_len = cpu_to_be32(4);
898 if (resp) {
899 iu->srp.rsp.sol_not = (sol_not & 0x04) >> 2;
900 } else {
901 iu->srp.rsp.sol_not = (sol_not & 0x02) >> 1;
904 iu->srp.rsp.status = GOOD;
905 iu->srp.rsp.data[3] = resp;
907 vscsi_send_iu(s, req, sizeof(iu->srp.rsp) + 4, VIOSRP_SRP_FORMAT);
909 return 1;
912 static int vscsi_handle_srp_req(VSCSIState *s, vscsi_req *req)
914 union srp_iu *srp = &req->iu.srp;
915 int done = 1;
916 uint8_t opcode = srp->rsp.opcode;
918 switch (opcode) {
919 case SRP_LOGIN_REQ:
920 vscsi_process_login(s, req);
921 break;
922 case SRP_TSK_MGMT:
923 done = vscsi_process_tsk_mgmt(s, req);
924 break;
925 case SRP_CMD:
926 done = vscsi_queue_cmd(s, req);
927 break;
928 case SRP_LOGIN_RSP:
929 case SRP_I_LOGOUT:
930 case SRP_T_LOGOUT:
931 case SRP_RSP:
932 case SRP_CRED_REQ:
933 case SRP_CRED_RSP:
934 case SRP_AER_REQ:
935 case SRP_AER_RSP:
936 fprintf(stderr, "VSCSI: Unsupported opcode %02x\n", opcode);
937 break;
938 default:
939 fprintf(stderr, "VSCSI: Unknown type %02x\n", opcode);
942 return done;
945 static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
947 struct viosrp_adapter_info *sinfo;
948 struct mad_adapter_info_data info;
949 int rc;
951 sinfo = &req->iu.mad.adapter_info;
953 #if 0 /* What for ? */
954 rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(sinfo->buffer),
955 &info, be16_to_cpu(sinfo->common.length));
956 if (rc) {
957 fprintf(stderr, "vscsi_send_adapter_info: DMA read failure !\n");
959 #endif
960 memset(&info, 0, sizeof(info));
961 strcpy(info.srp_version, SRP_VERSION);
962 memcpy(info.partition_name, "qemu", sizeof("qemu"));
963 info.partition_number = cpu_to_be32(0);
964 info.mad_version = cpu_to_be32(1);
965 info.os_type = cpu_to_be32(2);
966 info.port_max_txu[0] = cpu_to_be32(VSCSI_MAX_SECTORS << 9);
968 rc = spapr_vio_dma_write(&s->vdev, be64_to_cpu(sinfo->buffer),
969 &info, be16_to_cpu(sinfo->common.length));
970 if (rc) {
971 fprintf(stderr, "vscsi_send_adapter_info: DMA write failure !\n");
974 sinfo->common.status = rc ? cpu_to_be32(1) : 0;
976 return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
979 static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
981 struct viosrp_capabilities *vcap;
982 struct capabilities cap = { };
983 uint16_t len, req_len;
984 uint64_t buffer;
985 int rc;
987 vcap = &req->iu.mad.capabilities;
988 req_len = len = be16_to_cpu(vcap->common.length);
989 buffer = be64_to_cpu(vcap->buffer);
990 if (len > sizeof(cap)) {
991 fprintf(stderr, "vscsi_send_capabilities: capabilities size mismatch !\n");
994 * Just read and populate the structure that is known.
995 * Zero rest of the structure.
997 len = sizeof(cap);
999 rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
1000 if (rc) {
1001 fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");
1005 * Current implementation does not suppport any migration or
1006 * reservation capabilities. Construct the response telling the
1007 * guest not to use them.
1009 cap.flags = 0;
1010 cap.migration.ecl = 0;
1011 cap.reserve.type = 0;
1012 cap.migration.common.server_support = 0;
1013 cap.reserve.common.server_support = 0;
1015 rc = spapr_vio_dma_write(&s->vdev, buffer, &cap, len);
1016 if (rc) {
1017 fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
1019 if (req_len > len) {
1021 * Being paranoid and lets not worry about the error code
1022 * here. Actual write of the cap is done above.
1024 spapr_vio_dma_set(&s->vdev, (buffer + len), 0, (req_len - len));
1026 vcap->common.status = rc ? cpu_to_be32(1) : 0;
1027 return vscsi_send_iu(s, req, sizeof(*vcap), VIOSRP_MAD_FORMAT);
1030 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
1032 union mad_iu *mad = &req->iu.mad;
1033 bool request_handled = false;
1034 uint64_t retlen = 0;
1036 switch (be32_to_cpu(mad->empty_iu.common.type)) {
1037 case VIOSRP_EMPTY_IU_TYPE:
1038 fprintf(stderr, "Unsupported EMPTY MAD IU\n");
1039 retlen = sizeof(mad->empty_iu);
1040 break;
1041 case VIOSRP_ERROR_LOG_TYPE:
1042 fprintf(stderr, "Unsupported ERROR LOG MAD IU\n");
1043 retlen = sizeof(mad->error_log);
1044 break;
1045 case VIOSRP_ADAPTER_INFO_TYPE:
1046 vscsi_send_adapter_info(s, req);
1047 request_handled = true;
1048 break;
1049 case VIOSRP_HOST_CONFIG_TYPE:
1050 retlen = sizeof(mad->host_config);
1051 break;
1052 case VIOSRP_CAPABILITIES_TYPE:
1053 vscsi_send_capabilities(s, req);
1054 request_handled = true;
1055 break;
1056 default:
1057 fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
1058 be32_to_cpu(mad->empty_iu.common.type));
1060 * PAPR+ says that "The length field is set to the length
1061 * of the data structure(s) used in the command".
1062 * As we did not recognize the request type, put zero there.
1064 retlen = 0;
1067 if (!request_handled) {
1068 mad->empty_iu.common.status = cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED);
1069 vscsi_send_iu(s, req, retlen, VIOSRP_MAD_FORMAT);
1072 return 1;
1075 static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq)
1077 vscsi_req *req;
1078 int done;
1080 req = vscsi_get_req(s);
1081 if (req == NULL) {
1082 fprintf(stderr, "VSCSI: Failed to get a request !\n");
1083 return;
1086 /* We only support a limited number of descriptors, we know
1087 * the ibmvscsi driver uses up to 10 max, so it should fit
1088 * in our 256 bytes IUs. If not we'll have to increase the size
1089 * of the structure.
1091 if (crq->s.IU_length > sizeof(union viosrp_iu)) {
1092 fprintf(stderr, "VSCSI: SRP IU too long (%d bytes) !\n",
1093 crq->s.IU_length);
1094 vscsi_put_req(req);
1095 return;
1098 /* XXX Handle failure differently ? */
1099 if (spapr_vio_dma_read(&s->vdev, crq->s.IU_data_ptr, &req->iu,
1100 crq->s.IU_length)) {
1101 fprintf(stderr, "vscsi_got_payload: DMA read failure !\n");
1102 vscsi_put_req(req);
1103 return;
1105 memcpy(&req->crq, crq, sizeof(vscsi_crq));
1107 if (crq->s.format == VIOSRP_MAD_FORMAT) {
1108 done = vscsi_handle_mad_req(s, req);
1109 } else {
1110 done = vscsi_handle_srp_req(s, req);
1113 if (done) {
1114 vscsi_put_req(req);
1119 static int vscsi_do_crq(struct SpaprVioDevice *dev, uint8_t *crq_data)
1121 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
1122 vscsi_crq crq;
1124 memcpy(crq.raw, crq_data, 16);
1125 crq.s.timeout = be16_to_cpu(crq.s.timeout);
1126 crq.s.IU_length = be16_to_cpu(crq.s.IU_length);
1127 crq.s.IU_data_ptr = be64_to_cpu(crq.s.IU_data_ptr);
1129 trace_spapr_vscsi_do_crq(crq.raw[0], crq.raw[1]);
1131 switch (crq.s.valid) {
1132 case 0xc0: /* Init command/response */
1134 /* Respond to initialization request */
1135 if (crq.s.format == 0x01) {
1136 memset(crq.raw, 0, 16);
1137 crq.s.valid = 0xc0;
1138 crq.s.format = 0x02;
1139 spapr_vio_send_crq(dev, crq.raw);
1142 /* Note that in hotplug cases, we might get a 0x02
1143 * as a result of us emitting the init request
1146 break;
1147 case 0xff: /* Link event */
1149 /* Not handled for now */
1151 break;
1152 case 0x80: /* Payloads */
1153 switch (crq.s.format) {
1154 case VIOSRP_SRP_FORMAT: /* AKA VSCSI request */
1155 case VIOSRP_MAD_FORMAT: /* AKA VSCSI response */
1156 vscsi_got_payload(s, &crq);
1157 break;
1158 case VIOSRP_OS400_FORMAT:
1159 case VIOSRP_AIX_FORMAT:
1160 case VIOSRP_LINUX_FORMAT:
1161 case VIOSRP_INLINE_FORMAT:
1162 fprintf(stderr, "vscsi_do_srq: Unsupported payload format %02x\n",
1163 crq.s.format);
1164 break;
1165 default:
1166 fprintf(stderr, "vscsi_do_srq: Unknown payload format %02x\n",
1167 crq.s.format);
1169 break;
1170 default:
1171 fprintf(stderr, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
1172 crq.raw[0], crq.raw[1]);
1175 return 0;
1178 static const struct SCSIBusInfo vscsi_scsi_info = {
1179 .tcq = true,
1180 .max_channel = 7, /* logical unit addressing format */
1181 .max_target = 63,
1182 .max_lun = 31,
1184 .transfer_data = vscsi_transfer_data,
1185 .complete = vscsi_command_complete,
1186 .cancel = vscsi_request_cancelled,
1187 .save_request = vscsi_save_request,
1188 .load_request = vscsi_load_request,
1191 static void spapr_vscsi_reset(SpaprVioDevice *dev)
1193 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
1194 int i;
1196 memset(s->reqs, 0, sizeof(s->reqs));
1197 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
1198 s->reqs[i].qtag = i;
1202 static void spapr_vscsi_realize(SpaprVioDevice *dev, Error **errp)
1204 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
1206 dev->crq.SendFunc = vscsi_do_crq;
1208 scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev),
1209 &vscsi_scsi_info, NULL);
1212 void spapr_vscsi_create(SpaprVioBus *bus)
1214 DeviceState *dev;
1216 dev = qdev_create(&bus->bus, "spapr-vscsi");
1218 qdev_init_nofail(dev);
1219 scsi_bus_legacy_handle_cmdline(&VIO_SPAPR_VSCSI_DEVICE(dev)->bus);
1222 static int spapr_vscsi_devnode(SpaprVioDevice *dev, void *fdt, int node_off)
1224 int ret;
1226 ret = fdt_setprop_cell(fdt, node_off, "#address-cells", 2);
1227 if (ret < 0) {
1228 return ret;
1231 ret = fdt_setprop_cell(fdt, node_off, "#size-cells", 0);
1232 if (ret < 0) {
1233 return ret;
1236 return 0;
1239 static Property spapr_vscsi_properties[] = {
1240 DEFINE_SPAPR_PROPERTIES(VSCSIState, vdev),
1241 DEFINE_PROP_END_OF_LIST(),
1244 static const VMStateDescription vmstate_spapr_vscsi = {
1245 .name = "spapr_vscsi",
1246 .version_id = 1,
1247 .minimum_version_id = 1,
1248 .fields = (VMStateField[]) {
1249 VMSTATE_SPAPR_VIO(vdev, VSCSIState),
1250 /* VSCSI state */
1251 /* ???? */
1253 VMSTATE_END_OF_LIST()
1257 static void spapr_vscsi_class_init(ObjectClass *klass, void *data)
1259 DeviceClass *dc = DEVICE_CLASS(klass);
1260 SpaprVioDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
1262 k->realize = spapr_vscsi_realize;
1263 k->reset = spapr_vscsi_reset;
1264 k->devnode = spapr_vscsi_devnode;
1265 k->dt_name = "v-scsi";
1266 k->dt_type = "vscsi";
1267 k->dt_compatible = "IBM,v-scsi";
1268 k->signal_mask = 0x00000001;
1269 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1270 dc->props = spapr_vscsi_properties;
1271 k->rtce_window_size = 0x10000000;
1272 dc->vmsd = &vmstate_spapr_vscsi;
1275 static const TypeInfo spapr_vscsi_info = {
1276 .name = TYPE_VIO_SPAPR_VSCSI_DEVICE,
1277 .parent = TYPE_VIO_SPAPR_DEVICE,
1278 .instance_size = sizeof(VSCSIState),
1279 .class_init = spapr_vscsi_class_init,
1282 static void spapr_vscsi_register_types(void)
1284 type_register_static(&spapr_vscsi_info);
1287 type_init(spapr_vscsi_register_types)