hw/scsi/spapr_vscsi: Do not mix SRP IU size with DMA buffer size
[qemu/ar7.git] / hw / scsi / spapr_vscsi.c
blobacf9bb50bce1a72d37af7ebe2b5c9470aaf5226b
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 uint8_t viosrp_iu_buf[SRP_MAX_IU_LEN];
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 union viosrp_iu *req_iu(vscsi_req *req)
102 return (union viosrp_iu *)req->viosrp_iu_buf;
105 static struct vscsi_req *vscsi_get_req(VSCSIState *s)
107 vscsi_req *req;
108 int i;
110 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
111 req = &s->reqs[i];
112 if (!req->active) {
113 memset(req, 0, sizeof(*req));
114 req->qtag = i;
115 req->active = 1;
116 return req;
119 return NULL;
122 static struct vscsi_req *vscsi_find_req(VSCSIState *s, uint64_t srp_tag)
124 vscsi_req *req;
125 int i;
127 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
128 req = &s->reqs[i];
129 if (req_iu(req)->srp.cmd.tag == srp_tag) {
130 return req;
133 return NULL;
136 static void vscsi_put_req(vscsi_req *req)
138 if (req->sreq != NULL) {
139 scsi_req_unref(req->sreq);
141 req->sreq = NULL;
142 req->active = 0;
145 static SCSIDevice *vscsi_device_find(SCSIBus *bus, uint64_t srp_lun, int *lun)
147 int channel = 0, id = 0;
149 retry:
150 switch (srp_lun >> 62) {
151 case 0:
152 if ((srp_lun >> 56) != 0) {
153 channel = (srp_lun >> 56) & 0x3f;
154 id = (srp_lun >> 48) & 0xff;
155 srp_lun <<= 16;
156 goto retry;
158 *lun = (srp_lun >> 48) & 0xff;
159 break;
161 case 1:
162 *lun = (srp_lun >> 48) & 0x3fff;
163 break;
164 case 2:
165 channel = (srp_lun >> 53) & 0x7;
166 id = (srp_lun >> 56) & 0x3f;
167 *lun = (srp_lun >> 48) & 0x1f;
168 break;
169 case 3:
170 *lun = -1;
171 return NULL;
172 default:
173 abort();
176 return scsi_device_find(bus, channel, id, *lun);
179 static int vscsi_send_iu(VSCSIState *s, vscsi_req *req,
180 uint64_t length, uint8_t format)
182 long rc, rc1;
184 /* First copy the SRP */
185 rc = spapr_vio_dma_write(&s->vdev, req->crq.s.IU_data_ptr,
186 &req->viosrp_iu_buf, length);
187 if (rc) {
188 fprintf(stderr, "vscsi_send_iu: DMA write failure !\n");
191 req->crq.s.valid = 0x80;
192 req->crq.s.format = format;
193 req->crq.s.reserved = 0x00;
194 req->crq.s.timeout = cpu_to_be16(0x0000);
195 req->crq.s.IU_length = cpu_to_be16(length);
196 req->crq.s.IU_data_ptr = req_iu(req)->srp.rsp.tag; /* right byte order */
198 if (rc == 0) {
199 req->crq.s.status = VIOSRP_OK;
200 } else {
201 req->crq.s.status = VIOSRP_ADAPTER_FAIL;
204 rc1 = spapr_vio_send_crq(&s->vdev, req->crq.raw);
205 if (rc1) {
206 fprintf(stderr, "vscsi_send_iu: Error sending response\n");
207 return rc1;
210 return rc;
213 static void vscsi_makeup_sense(VSCSIState *s, vscsi_req *req,
214 uint8_t key, uint8_t asc, uint8_t ascq)
216 req->senselen = SRP_RSP_SENSE_DATA_LEN;
218 /* Valid bit and 'current errors' */
219 req->sense[0] = (0x1 << 7 | 0x70);
220 /* Sense key */
221 req->sense[2] = key;
222 /* Additional sense length */
223 req->sense[7] = 0xa; /* 10 bytes */
224 /* Additional sense code */
225 req->sense[12] = asc;
226 req->sense[13] = ascq;
229 static int vscsi_send_rsp(VSCSIState *s, vscsi_req *req,
230 uint8_t status, int32_t res_in, int32_t res_out)
232 union viosrp_iu *iu = req_iu(req);
233 uint64_t tag = iu->srp.rsp.tag;
234 int total_len = sizeof(iu->srp.rsp);
235 uint8_t sol_not = iu->srp.cmd.sol_not;
237 trace_spapr_vscsi_send_rsp(status, res_in, res_out);
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;
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;
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;
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);
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);
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 iu->srp.rsp.flags |= SRP_RSP_FLAG_SNSVALID;
270 iu->srp.rsp.sense_data_len = cpu_to_be32(req->senselen);
271 memcpy(iu->srp.rsp.data, req->sense, req->senselen);
272 total_len += req->senselen;
274 } else {
275 iu->srp.rsp.sol_not = (sol_not & 0x02) >> 1;
278 vscsi_send_iu(s, req, total_len, VIOSRP_SRP_FORMAT);
279 return 0;
282 static inline struct srp_direct_buf vscsi_swap_desc(struct srp_direct_buf desc)
284 desc.va = be64_to_cpu(desc.va);
285 desc.len = be32_to_cpu(desc.len);
286 return desc;
289 static int vscsi_fetch_desc(VSCSIState *s, struct vscsi_req *req,
290 unsigned n, unsigned buf_offset,
291 struct srp_direct_buf *ret)
293 struct srp_cmd *cmd = &req_iu(req)->srp.cmd;
295 switch (req->dma_fmt) {
296 case SRP_NO_DATA_DESC: {
297 trace_spapr_vscsi_fetch_desc_no_data();
298 return 0;
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 trace_spapr_vscsi_fetch_desc_direct();
304 break;
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 trace_spapr_vscsi_fetch_desc_indirect(req->qtag, n,
312 req->local_desc);
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);
318 if (desc_offset >= tbl_desc.len) {
319 trace_spapr_vscsi_fetch_desc_out_of_range(n, desc_offset);
320 return -1;
322 rc = spapr_vio_dma_read(&s->vdev, tbl_desc.va + desc_offset,
323 ret, sizeof(struct srp_direct_buf));
324 if (rc) {
325 trace_spapr_vscsi_fetch_desc_dma_read_error(rc);
326 return -1;
328 trace_spapr_vscsi_fetch_desc_indirect_seg_ext(req->qtag, n,
329 req->total_desc,
330 tbl_desc.va,
331 tbl_desc.len);
332 } else {
333 trace_spapr_vscsi_fetch_desc_out_of_desc();
334 return 0;
336 break;
338 default:
339 fprintf(stderr, "VSCSI: Unknown format %x\n", req->dma_fmt);
340 return -1;
343 *ret = vscsi_swap_desc(*ret);
344 if (buf_offset > ret->len) {
345 trace_spapr_vscsi_fetch_desc_out_of_desc_boundary(buf_offset,
346 req->cur_desc_num,
347 ret->len);
348 return -1;
350 ret->va += buf_offset;
351 ret->len -= buf_offset;
353 trace_spapr_vscsi_fetch_desc_done(req->cur_desc_num, req->cur_desc_offset,
354 ret->va, ret->len);
356 return ret->len ? 1 : 0;
359 static int vscsi_srp_direct_data(VSCSIState *s, vscsi_req *req,
360 uint8_t *buf, uint32_t len)
362 struct srp_direct_buf md;
363 uint32_t llen;
364 int rc = 0;
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;
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);
382 if (rc) {
383 return -1;
385 req->cur_desc_offset += llen;
387 return llen;
390 static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req,
391 uint8_t *buf, uint32_t len)
393 struct srp_direct_buf md;
394 int rc = 0;
395 uint32_t llen, total = 0;
397 trace_spapr_vscsi_srp_indirect_data(len);
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;
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);
415 if (rc) {
416 trace_spapr_vscsi_srp_indirect_data_rw(req->writing, rc);
417 break;
419 trace_spapr_vscsi_srp_indirect_data_buf(buf[0], buf[1], buf[2], buf[3]);
421 len -= llen;
422 buf += llen;
424 total += llen;
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;
435 return rc ? -1 : total;
438 static int vscsi_srp_transfer_data(VSCSIState *s, vscsi_req *req,
439 int writing, uint8_t *buf, uint32_t len)
441 int err = 0;
443 switch (req->dma_fmt) {
444 case SRP_NO_DATA_DESC:
445 trace_spapr_vscsi_srp_transfer_data(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;
454 return err;
457 /* Bits from linux srp */
458 static int data_out_desc_size(struct srp_cmd *cmd)
460 int size = 0;
461 uint8_t fmt = cmd->buf_fmt >> 4;
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;
476 return size;
479 static int vscsi_preprocess_desc(vscsi_req *req)
481 struct srp_cmd *cmd = &req_iu(req)->srp.cmd;
483 req->cdb_offset = cmd->add_cdb_len & ~3;
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);
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);
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;
508 default:
509 fprintf(stderr,
510 "vscsi_preprocess_desc: Unknown format %x\n", req->dma_fmt);
511 return -1;
514 return 0;
517 /* Callback to indicate that the SCSI layer has completed a transfer. */
518 static void vscsi_transfer_data(SCSIRequest *sreq, uint32_t len)
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;
525 trace_spapr_vscsi_transfer_data(sreq->tag, len, req);
526 if (req == NULL) {
527 fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
528 return;
531 if (len) {
532 buf = scsi_req_get_buf(sreq);
533 rc = vscsi_srp_transfer_data(s, req, req->writing, buf, len);
535 if (rc < 0) {
536 fprintf(stderr, "VSCSI: RDMA error rc=%d!\n", rc);
537 req->dma_error = true;
538 scsi_req_cancel(req->sreq);
539 return;
542 /* Start next chunk */
543 req->data_len -= rc;
544 scsi_req_continue(sreq);
547 /* Callback to indicate that the SCSI layer has completed a transfer. */
548 static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status, size_t resid)
550 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
551 vscsi_req *req = sreq->hba_private;
552 int32_t res_in = 0, res_out = 0;
554 trace_spapr_vscsi_command_complete(sreq->tag, status, req);
555 if (req == NULL) {
556 fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
557 return;
560 if (status == CHECK_CONDITION) {
561 req->senselen = scsi_req_get_sense(req->sreq, req->sense,
562 sizeof(req->sense));
563 trace_spapr_vscsi_command_complete_sense_data1(req->senselen,
564 req->sense[0], req->sense[1], req->sense[2], req->sense[3],
565 req->sense[4], req->sense[5], req->sense[6], req->sense[7]);
566 trace_spapr_vscsi_command_complete_sense_data2(
567 req->sense[8], req->sense[9], req->sense[10], req->sense[11],
568 req->sense[12], req->sense[13], req->sense[14], req->sense[15]);
571 trace_spapr_vscsi_command_complete_status(status);
572 if (status == 0) {
573 /* We handle overflows, not underflows for normal commands,
574 * but hopefully nobody cares
576 if (req->writing) {
577 res_out = req->data_len;
578 } else {
579 res_in = req->data_len;
582 vscsi_send_rsp(s, req, status, res_in, res_out);
583 vscsi_put_req(req);
586 static void vscsi_request_cancelled(SCSIRequest *sreq)
588 vscsi_req *req = sreq->hba_private;
590 if (req->dma_error) {
591 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
593 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
594 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
596 vscsi_put_req(req);
599 static const VMStateDescription vmstate_spapr_vscsi_req = {
600 .name = "spapr_vscsi_req",
601 .version_id = 1,
602 .minimum_version_id = 1,
603 .fields = (VMStateField[]) {
604 VMSTATE_BUFFER(crq.raw, vscsi_req),
605 VMSTATE_BUFFER(viosrp_iu_buf, vscsi_req),
606 VMSTATE_UINT32(qtag, vscsi_req),
607 VMSTATE_BOOL(active, vscsi_req),
608 VMSTATE_UINT32(data_len, vscsi_req),
609 VMSTATE_BOOL(writing, vscsi_req),
610 VMSTATE_UINT32(senselen, vscsi_req),
611 VMSTATE_BUFFER(sense, vscsi_req),
612 VMSTATE_UINT8(dma_fmt, vscsi_req),
613 VMSTATE_UINT16(local_desc, vscsi_req),
614 VMSTATE_UINT16(total_desc, vscsi_req),
615 VMSTATE_UINT16(cdb_offset, vscsi_req),
616 /*Restart SCSI request from the beginning for now */
617 /*VMSTATE_UINT16(cur_desc_num, vscsi_req),
618 VMSTATE_UINT16(cur_desc_offset, vscsi_req),*/
619 VMSTATE_END_OF_LIST()
623 static void vscsi_save_request(QEMUFile *f, SCSIRequest *sreq)
625 vscsi_req *req = sreq->hba_private;
626 assert(req->active);
628 vmstate_save_state(f, &vmstate_spapr_vscsi_req, req, NULL);
630 trace_spapr_vscsi_save_request(req->qtag, req->cur_desc_num,
631 req->cur_desc_offset);
634 static void *vscsi_load_request(QEMUFile *f, SCSIRequest *sreq)
636 SCSIBus *bus = sreq->bus;
637 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(bus->qbus.parent);
638 vscsi_req *req;
639 int rc;
641 assert(sreq->tag < VSCSI_REQ_LIMIT);
642 req = &s->reqs[sreq->tag];
643 assert(!req->active);
645 memset(req, 0, sizeof(*req));
646 rc = vmstate_load_state(f, &vmstate_spapr_vscsi_req, req, 1);
647 if (rc) {
648 fprintf(stderr, "VSCSI: failed loading request tag#%u\n", sreq->tag);
649 return NULL;
651 assert(req->active);
653 req->sreq = scsi_req_ref(sreq);
655 trace_spapr_vscsi_load_request(req->qtag, req->cur_desc_num,
656 req->cur_desc_offset);
658 return req;
661 static void vscsi_process_login(VSCSIState *s, vscsi_req *req)
663 union viosrp_iu *iu = req_iu(req);
664 struct srp_login_rsp *rsp = &iu->srp.login_rsp;
665 uint64_t tag = iu->srp.rsp.tag;
667 trace_spapr_vscsi_process_login();
669 /* TODO handle case that requested size is wrong and
670 * buffer format is wrong
672 memset(iu, 0, sizeof(struct srp_login_rsp));
673 rsp->opcode = SRP_LOGIN_RSP;
674 /* Don't advertise quite as many request as we support to
675 * keep room for management stuff etc...
677 rsp->req_lim_delta = cpu_to_be32(VSCSI_REQ_LIMIT-2);
678 rsp->tag = tag;
679 rsp->max_it_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
680 rsp->max_ti_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
681 /* direct and indirect */
682 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
684 vscsi_send_iu(s, req, sizeof(*rsp), VIOSRP_SRP_FORMAT);
687 static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
689 uint8_t *cdb = req_iu(req)->srp.cmd.cdb;
690 uint8_t resp_data[36];
691 int rc, len, alen;
693 /* We don't do EVPD. Also check that page_code is 0 */
694 if ((cdb[1] & 0x01) || cdb[2] != 0) {
695 /* Send INVALID FIELD IN CDB */
696 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0);
697 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
698 return;
700 alen = cdb[3];
701 alen = (alen << 8) | cdb[4];
702 len = MIN(alen, 36);
704 /* Fake up inquiry using PQ=3 */
705 memset(resp_data, 0, 36);
706 resp_data[0] = 0x7f; /* Not capable of supporting a device here */
707 resp_data[2] = 0x06; /* SPS-4 */
708 resp_data[3] = 0x02; /* Resp data format */
709 resp_data[4] = 36 - 5; /* Additional length */
710 resp_data[7] = 0x10; /* Sync transfers */
711 memcpy(&resp_data[16], "QEMU EMPTY ", 16);
712 memcpy(&resp_data[8], "QEMU ", 8);
714 req->writing = 0;
715 vscsi_preprocess_desc(req);
716 rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
717 if (rc < 0) {
718 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
719 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
720 } else {
721 vscsi_send_rsp(s, req, 0, 36 - rc, 0);
725 static void vscsi_report_luns(VSCSIState *s, vscsi_req *req)
727 BusChild *kid;
728 int i, len, n, rc;
729 uint8_t *resp_data;
730 bool found_lun0;
732 n = 0;
733 found_lun0 = false;
734 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
735 SCSIDevice *dev = SCSI_DEVICE(kid->child);
737 n += 8;
738 if (dev->channel == 0 && dev->id == 0 && dev->lun == 0) {
739 found_lun0 = true;
742 if (!found_lun0) {
743 n += 8;
745 len = n+8;
747 resp_data = g_malloc0(len);
748 stl_be_p(resp_data, n);
749 i = found_lun0 ? 8 : 16;
750 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
751 DeviceState *qdev = kid->child;
752 SCSIDevice *dev = SCSI_DEVICE(qdev);
754 if (dev->id == 0 && dev->channel == 0) {
755 resp_data[i] = 0; /* Use simple LUN for 0 (SAM5 4.7.7.1) */
756 } else {
757 resp_data[i] = (2 << 6); /* Otherwise LUN addressing (4.7.7.4) */
759 resp_data[i] |= dev->id;
760 resp_data[i+1] = (dev->channel << 5);
761 resp_data[i+1] |= dev->lun;
762 i += 8;
765 vscsi_preprocess_desc(req);
766 rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
767 g_free(resp_data);
768 if (rc < 0) {
769 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
770 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
771 } else {
772 vscsi_send_rsp(s, req, 0, len - rc, 0);
776 static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req)
778 union srp_iu *srp = &req_iu(req)->srp;
779 SCSIDevice *sdev;
780 int n, lun;
782 if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN)
783 && srp->cmd.cdb[0] == REPORT_LUNS) {
784 vscsi_report_luns(s, req);
785 return 0;
788 sdev = vscsi_device_find(&s->bus, be64_to_cpu(srp->cmd.lun), &lun);
789 if (!sdev) {
790 trace_spapr_vscsi_queue_cmd_no_drive(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;
799 req->sreq = scsi_req_new(sdev, req->qtag, lun, srp->cmd.cdb, req);
800 n = scsi_req_enqueue(req->sreq);
802 trace_spapr_vscsi_queue_cmd(req->qtag, srp->cmd.cdb[0],
803 scsi_command_name(srp->cmd.cdb[0]), lun, n);
805 if (n) {
806 /* Transfer direction must be set before preprocessing the
807 * descriptors
809 req->writing = (n < 1);
811 /* Preprocess RDMA descriptors */
812 vscsi_preprocess_desc(req);
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;
820 scsi_req_continue(req->sreq);
822 /* Don't touch req here, it may have been recycled already */
824 return 0;
827 static int vscsi_process_tsk_mgmt(VSCSIState *s, vscsi_req *req)
829 union viosrp_iu *iu = req_iu(req);
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;
836 fprintf(stderr, "vscsi_process_tsk_mgmt %02x\n",
837 iu->srp.tsk_mgmt.tsk_mgmt_func);
839 d = vscsi_device_find(&s->bus,
840 be64_to_cpu(req_iu(req)->srp.tsk_mgmt.lun), &lun);
841 if (!d) {
842 resp = SRP_TSK_MGMT_FIELDS_INVALID;
843 } else {
844 switch (iu->srp.tsk_mgmt.tsk_mgmt_func) {
845 case SRP_TSK_ABORT_TASK:
846 if (d->lun != lun) {
847 resp = SRP_TSK_MGMT_FIELDS_INVALID;
848 break;
851 tmpreq = vscsi_find_req(s, req_iu(req)->srp.tsk_mgmt.task_tag);
852 if (tmpreq && tmpreq->sreq) {
853 assert(tmpreq->sreq->hba_private);
854 scsi_req_cancel(tmpreq->sreq);
856 break;
858 case SRP_TSK_LUN_RESET:
859 if (d->lun != lun) {
860 resp = SRP_TSK_MGMT_FIELDS_INVALID;
861 break;
864 qdev_reset_all(&d->qdev);
865 break;
867 case SRP_TSK_ABORT_TASK_SET:
868 case SRP_TSK_CLEAR_TASK_SET:
869 if (d->lun != lun) {
870 resp = SRP_TSK_MGMT_FIELDS_INVALID;
871 break;
874 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
875 tmpreq = &s->reqs[i];
876 if (req_iu(tmpreq)->srp.cmd.lun
877 != req_iu(req)->srp.tsk_mgmt.lun) {
878 continue;
880 if (!tmpreq->active || !tmpreq->sreq) {
881 continue;
883 assert(tmpreq->sreq->hba_private);
884 scsi_req_cancel(tmpreq->sreq);
886 break;
888 case SRP_TSK_CLEAR_ACA:
889 resp = SRP_TSK_MGMT_NOT_SUPPORTED;
890 break;
892 default:
893 resp = SRP_TSK_MGMT_FIELDS_INVALID;
894 break;
898 /* Compose the response here as */
899 memset(iu, 0, sizeof(struct srp_rsp) + 4);
900 iu->srp.rsp.opcode = SRP_RSP;
901 iu->srp.rsp.req_lim_delta = cpu_to_be32(1);
902 iu->srp.rsp.tag = tag;
903 iu->srp.rsp.flags |= SRP_RSP_FLAG_RSPVALID;
904 iu->srp.rsp.resp_data_len = cpu_to_be32(4);
905 if (resp) {
906 iu->srp.rsp.sol_not = (sol_not & 0x04) >> 2;
907 } else {
908 iu->srp.rsp.sol_not = (sol_not & 0x02) >> 1;
911 iu->srp.rsp.status = GOOD;
912 iu->srp.rsp.data[3] = resp;
914 vscsi_send_iu(s, req, sizeof(iu->srp.rsp) + 4, VIOSRP_SRP_FORMAT);
916 return 1;
919 static int vscsi_handle_srp_req(VSCSIState *s, vscsi_req *req)
921 union srp_iu *srp = &req_iu(req)->srp;
922 int done = 1;
923 uint8_t opcode = srp->rsp.opcode;
925 switch (opcode) {
926 case SRP_LOGIN_REQ:
927 vscsi_process_login(s, req);
928 break;
929 case SRP_TSK_MGMT:
930 done = vscsi_process_tsk_mgmt(s, req);
931 break;
932 case SRP_CMD:
933 done = vscsi_queue_cmd(s, req);
934 break;
935 case SRP_LOGIN_RSP:
936 case SRP_I_LOGOUT:
937 case SRP_T_LOGOUT:
938 case SRP_RSP:
939 case SRP_CRED_REQ:
940 case SRP_CRED_RSP:
941 case SRP_AER_REQ:
942 case SRP_AER_RSP:
943 fprintf(stderr, "VSCSI: Unsupported opcode %02x\n", opcode);
944 break;
945 default:
946 fprintf(stderr, "VSCSI: Unknown type %02x\n", opcode);
949 return done;
952 static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
954 struct viosrp_adapter_info *sinfo;
955 struct mad_adapter_info_data info;
956 int rc;
958 sinfo = &req_iu(req)->mad.adapter_info;
960 #if 0 /* What for ? */
961 rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(sinfo->buffer),
962 &info, be16_to_cpu(sinfo->common.length));
963 if (rc) {
964 fprintf(stderr, "vscsi_send_adapter_info: DMA read failure !\n");
966 #endif
967 memset(&info, 0, sizeof(info));
968 strcpy(info.srp_version, SRP_VERSION);
969 memcpy(info.partition_name, "qemu", sizeof("qemu"));
970 info.partition_number = cpu_to_be32(0);
971 info.mad_version = cpu_to_be32(1);
972 info.os_type = cpu_to_be32(2);
973 info.port_max_txu[0] = cpu_to_be32(VSCSI_MAX_SECTORS << 9);
975 rc = spapr_vio_dma_write(&s->vdev, be64_to_cpu(sinfo->buffer),
976 &info, be16_to_cpu(sinfo->common.length));
977 if (rc) {
978 fprintf(stderr, "vscsi_send_adapter_info: DMA write failure !\n");
981 sinfo->common.status = rc ? cpu_to_be32(1) : 0;
983 return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
986 static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
988 struct viosrp_capabilities *vcap;
989 struct capabilities cap = { };
990 uint16_t len, req_len;
991 uint64_t buffer;
992 int rc;
994 vcap = &req_iu(req)->mad.capabilities;
995 req_len = len = be16_to_cpu(vcap->common.length);
996 buffer = be64_to_cpu(vcap->buffer);
997 if (len > sizeof(cap)) {
998 fprintf(stderr, "vscsi_send_capabilities: capabilities size mismatch !\n");
1001 * Just read and populate the structure that is known.
1002 * Zero rest of the structure.
1004 len = sizeof(cap);
1006 rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
1007 if (rc) {
1008 fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");
1012 * Current implementation does not suppport any migration or
1013 * reservation capabilities. Construct the response telling the
1014 * guest not to use them.
1016 cap.flags = 0;
1017 cap.migration.ecl = 0;
1018 cap.reserve.type = 0;
1019 cap.migration.common.server_support = 0;
1020 cap.reserve.common.server_support = 0;
1022 rc = spapr_vio_dma_write(&s->vdev, buffer, &cap, len);
1023 if (rc) {
1024 fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
1026 if (req_len > len) {
1028 * Being paranoid and lets not worry about the error code
1029 * here. Actual write of the cap is done above.
1031 spapr_vio_dma_set(&s->vdev, (buffer + len), 0, (req_len - len));
1033 vcap->common.status = rc ? cpu_to_be32(1) : 0;
1034 return vscsi_send_iu(s, req, sizeof(*vcap), VIOSRP_MAD_FORMAT);
1037 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
1039 union mad_iu *mad = &req_iu(req)->mad;
1040 bool request_handled = false;
1041 uint64_t retlen = 0;
1043 switch (be32_to_cpu(mad->empty_iu.common.type)) {
1044 case VIOSRP_EMPTY_IU_TYPE:
1045 fprintf(stderr, "Unsupported EMPTY MAD IU\n");
1046 retlen = sizeof(mad->empty_iu);
1047 break;
1048 case VIOSRP_ERROR_LOG_TYPE:
1049 fprintf(stderr, "Unsupported ERROR LOG MAD IU\n");
1050 retlen = sizeof(mad->error_log);
1051 break;
1052 case VIOSRP_ADAPTER_INFO_TYPE:
1053 vscsi_send_adapter_info(s, req);
1054 request_handled = true;
1055 break;
1056 case VIOSRP_HOST_CONFIG_TYPE:
1057 retlen = sizeof(mad->host_config);
1058 break;
1059 case VIOSRP_CAPABILITIES_TYPE:
1060 vscsi_send_capabilities(s, req);
1061 request_handled = true;
1062 break;
1063 default:
1064 fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
1065 be32_to_cpu(mad->empty_iu.common.type));
1067 * PAPR+ says that "The length field is set to the length
1068 * of the data structure(s) used in the command".
1069 * As we did not recognize the request type, put zero there.
1071 retlen = 0;
1074 if (!request_handled) {
1075 mad->empty_iu.common.status = cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED);
1076 vscsi_send_iu(s, req, retlen, VIOSRP_MAD_FORMAT);
1079 return 1;
1082 static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq)
1084 vscsi_req *req;
1085 int done;
1087 req = vscsi_get_req(s);
1088 if (req == NULL) {
1089 fprintf(stderr, "VSCSI: Failed to get a request !\n");
1090 return;
1093 /* We only support a limited number of descriptors, we know
1094 * the ibmvscsi driver uses up to 10 max, so it should fit
1095 * in our 256 bytes IUs. If not we'll have to increase the size
1096 * of the structure.
1098 if (crq->s.IU_length > SRP_MAX_IU_LEN) {
1099 fprintf(stderr, "VSCSI: SRP IU too long (%d bytes) !\n",
1100 crq->s.IU_length);
1101 vscsi_put_req(req);
1102 return;
1105 /* XXX Handle failure differently ? */
1106 if (spapr_vio_dma_read(&s->vdev, crq->s.IU_data_ptr, &req->viosrp_iu_buf,
1107 crq->s.IU_length)) {
1108 fprintf(stderr, "vscsi_got_payload: DMA read failure !\n");
1109 vscsi_put_req(req);
1110 return;
1112 memcpy(&req->crq, crq, sizeof(vscsi_crq));
1114 if (crq->s.format == VIOSRP_MAD_FORMAT) {
1115 done = vscsi_handle_mad_req(s, req);
1116 } else {
1117 done = vscsi_handle_srp_req(s, req);
1120 if (done) {
1121 vscsi_put_req(req);
1126 static int vscsi_do_crq(struct SpaprVioDevice *dev, uint8_t *crq_data)
1128 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
1129 vscsi_crq crq;
1131 memcpy(crq.raw, crq_data, 16);
1132 crq.s.timeout = be16_to_cpu(crq.s.timeout);
1133 crq.s.IU_length = be16_to_cpu(crq.s.IU_length);
1134 crq.s.IU_data_ptr = be64_to_cpu(crq.s.IU_data_ptr);
1136 trace_spapr_vscsi_do_crq(crq.raw[0], crq.raw[1]);
1138 switch (crq.s.valid) {
1139 case 0xc0: /* Init command/response */
1141 /* Respond to initialization request */
1142 if (crq.s.format == 0x01) {
1143 memset(crq.raw, 0, 16);
1144 crq.s.valid = 0xc0;
1145 crq.s.format = 0x02;
1146 spapr_vio_send_crq(dev, crq.raw);
1149 /* Note that in hotplug cases, we might get a 0x02
1150 * as a result of us emitting the init request
1153 break;
1154 case 0xff: /* Link event */
1156 /* Not handled for now */
1158 break;
1159 case 0x80: /* Payloads */
1160 switch (crq.s.format) {
1161 case VIOSRP_SRP_FORMAT: /* AKA VSCSI request */
1162 case VIOSRP_MAD_FORMAT: /* AKA VSCSI response */
1163 vscsi_got_payload(s, &crq);
1164 break;
1165 case VIOSRP_OS400_FORMAT:
1166 case VIOSRP_AIX_FORMAT:
1167 case VIOSRP_LINUX_FORMAT:
1168 case VIOSRP_INLINE_FORMAT:
1169 fprintf(stderr, "vscsi_do_srq: Unsupported payload format %02x\n",
1170 crq.s.format);
1171 break;
1172 default:
1173 fprintf(stderr, "vscsi_do_srq: Unknown payload format %02x\n",
1174 crq.s.format);
1176 break;
1177 default:
1178 fprintf(stderr, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
1179 crq.raw[0], crq.raw[1]);
1182 return 0;
1185 static const struct SCSIBusInfo vscsi_scsi_info = {
1186 .tcq = true,
1187 .max_channel = 7, /* logical unit addressing format */
1188 .max_target = 63,
1189 .max_lun = 31,
1191 .transfer_data = vscsi_transfer_data,
1192 .complete = vscsi_command_complete,
1193 .cancel = vscsi_request_cancelled,
1194 .save_request = vscsi_save_request,
1195 .load_request = vscsi_load_request,
1198 static void spapr_vscsi_reset(SpaprVioDevice *dev)
1200 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
1201 int i;
1203 memset(s->reqs, 0, sizeof(s->reqs));
1204 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
1205 s->reqs[i].qtag = i;
1209 static void spapr_vscsi_realize(SpaprVioDevice *dev, Error **errp)
1211 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
1213 dev->crq.SendFunc = vscsi_do_crq;
1215 scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev),
1216 &vscsi_scsi_info, NULL);
1219 void spapr_vscsi_create(SpaprVioBus *bus)
1221 DeviceState *dev;
1223 dev = qdev_create(&bus->bus, "spapr-vscsi");
1225 qdev_init_nofail(dev);
1226 scsi_bus_legacy_handle_cmdline(&VIO_SPAPR_VSCSI_DEVICE(dev)->bus);
1229 static int spapr_vscsi_devnode(SpaprVioDevice *dev, void *fdt, int node_off)
1231 int ret;
1233 ret = fdt_setprop_cell(fdt, node_off, "#address-cells", 2);
1234 if (ret < 0) {
1235 return ret;
1238 ret = fdt_setprop_cell(fdt, node_off, "#size-cells", 0);
1239 if (ret < 0) {
1240 return ret;
1243 return 0;
1246 static Property spapr_vscsi_properties[] = {
1247 DEFINE_SPAPR_PROPERTIES(VSCSIState, vdev),
1248 DEFINE_PROP_END_OF_LIST(),
1251 static const VMStateDescription vmstate_spapr_vscsi = {
1252 .name = "spapr_vscsi",
1253 .version_id = 1,
1254 .minimum_version_id = 1,
1255 .fields = (VMStateField[]) {
1256 VMSTATE_SPAPR_VIO(vdev, VSCSIState),
1257 /* VSCSI state */
1258 /* ???? */
1260 VMSTATE_END_OF_LIST()
1264 static void spapr_vscsi_class_init(ObjectClass *klass, void *data)
1266 DeviceClass *dc = DEVICE_CLASS(klass);
1267 SpaprVioDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
1269 k->realize = spapr_vscsi_realize;
1270 k->reset = spapr_vscsi_reset;
1271 k->devnode = spapr_vscsi_devnode;
1272 k->dt_name = "v-scsi";
1273 k->dt_type = "vscsi";
1274 k->dt_compatible = "IBM,v-scsi";
1275 k->signal_mask = 0x00000001;
1276 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1277 device_class_set_props(dc, spapr_vscsi_properties);
1278 k->rtce_window_size = 0x10000000;
1279 dc->vmsd = &vmstate_spapr_vscsi;
1282 static const TypeInfo spapr_vscsi_info = {
1283 .name = TYPE_VIO_SPAPR_VSCSI_DEVICE,
1284 .parent = TYPE_VIO_SPAPR_DEVICE,
1285 .instance_size = sizeof(VSCSIState),
1286 .class_init = spapr_vscsi_class_init,
1289 static void spapr_vscsi_register_types(void)
1291 type_register_static(&spapr_vscsi_info);
1294 type_init(spapr_vscsi_register_types)