net: imx: limit buffer descriptor count
[qemu/ar7.git] / hw / scsi / spapr_vscsi.c
blob6090a204a03d7999b167ce1e56e0e2c656e3aa25
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)
34 #include "qemu/osdep.h"
35 #include "qemu-common.h"
36 #include "cpu.h"
37 #include "hw/hw.h"
38 #include "hw/scsi/scsi.h"
39 #include "block/scsi.h"
40 #include "srp.h"
41 #include "hw/qdev.h"
42 #include "hw/ppc/spapr.h"
43 #include "hw/ppc/spapr_vio.h"
44 #include "viosrp.h"
45 #include "trace.h"
47 #include <libfdt.h>
50 * Virtual SCSI device
53 /* Random numbers */
54 #define VSCSI_MAX_SECTORS 4096
55 #define VSCSI_REQ_LIMIT 24
57 #define SRP_RSP_SENSE_DATA_LEN 18
59 #define SRP_REPORT_LUNS_WLUN 0xc10100000000000ULL
61 typedef union vscsi_crq {
62 struct viosrp_crq s;
63 uint8_t raw[16];
64 } vscsi_crq;
66 typedef struct vscsi_req {
67 vscsi_crq crq;
68 union viosrp_iu iu;
70 /* SCSI request tracking */
71 SCSIRequest *sreq;
72 uint32_t qtag; /* qemu tag != srp tag */
73 bool active;
74 bool writing;
75 bool dma_error;
76 uint32_t data_len;
77 uint32_t senselen;
78 uint8_t sense[SCSI_SENSE_BUF_SIZE];
80 /* RDMA related bits */
81 uint8_t dma_fmt;
82 uint16_t local_desc;
83 uint16_t total_desc;
84 uint16_t cdb_offset;
85 uint16_t cur_desc_num;
86 uint16_t cur_desc_offset;
87 } vscsi_req;
89 #define TYPE_VIO_SPAPR_VSCSI_DEVICE "spapr-vscsi"
90 #define VIO_SPAPR_VSCSI_DEVICE(obj) \
91 OBJECT_CHECK(VSCSIState, (obj), TYPE_VIO_SPAPR_VSCSI_DEVICE)
93 typedef struct {
94 VIOsPAPRDevice vdev;
95 SCSIBus bus;
96 vscsi_req reqs[VSCSI_REQ_LIMIT];
97 } VSCSIState;
99 static struct vscsi_req *vscsi_get_req(VSCSIState *s)
101 vscsi_req *req;
102 int i;
104 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
105 req = &s->reqs[i];
106 if (!req->active) {
107 memset(req, 0, sizeof(*req));
108 req->qtag = i;
109 req->active = 1;
110 return req;
113 return NULL;
116 static struct vscsi_req *vscsi_find_req(VSCSIState *s, uint64_t srp_tag)
118 vscsi_req *req;
119 int i;
121 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
122 req = &s->reqs[i];
123 if (req->iu.srp.cmd.tag == srp_tag) {
124 return req;
127 return NULL;
130 static void vscsi_put_req(vscsi_req *req)
132 if (req->sreq != NULL) {
133 scsi_req_unref(req->sreq);
135 req->sreq = NULL;
136 req->active = 0;
139 static SCSIDevice *vscsi_device_find(SCSIBus *bus, uint64_t srp_lun, int *lun)
141 int channel = 0, id = 0;
143 retry:
144 switch (srp_lun >> 62) {
145 case 0:
146 if ((srp_lun >> 56) != 0) {
147 channel = (srp_lun >> 56) & 0x3f;
148 id = (srp_lun >> 48) & 0xff;
149 srp_lun <<= 16;
150 goto retry;
152 *lun = (srp_lun >> 48) & 0xff;
153 break;
155 case 1:
156 *lun = (srp_lun >> 48) & 0x3fff;
157 break;
158 case 2:
159 channel = (srp_lun >> 53) & 0x7;
160 id = (srp_lun >> 56) & 0x3f;
161 *lun = (srp_lun >> 48) & 0x1f;
162 break;
163 case 3:
164 *lun = -1;
165 return NULL;
166 default:
167 abort();
170 return scsi_device_find(bus, channel, id, *lun);
173 static int vscsi_send_iu(VSCSIState *s, vscsi_req *req,
174 uint64_t length, uint8_t format)
176 long rc, rc1;
178 /* First copy the SRP */
179 rc = spapr_vio_dma_write(&s->vdev, req->crq.s.IU_data_ptr,
180 &req->iu, length);
181 if (rc) {
182 fprintf(stderr, "vscsi_send_iu: DMA write failure !\n");
185 req->crq.s.valid = 0x80;
186 req->crq.s.format = format;
187 req->crq.s.reserved = 0x00;
188 req->crq.s.timeout = cpu_to_be16(0x0000);
189 req->crq.s.IU_length = cpu_to_be16(length);
190 req->crq.s.IU_data_ptr = req->iu.srp.rsp.tag; /* right byte order */
192 if (rc == 0) {
193 req->crq.s.status = VIOSRP_OK;
194 } else {
195 req->crq.s.status = VIOSRP_ADAPTER_FAIL;
198 rc1 = spapr_vio_send_crq(&s->vdev, req->crq.raw);
199 if (rc1) {
200 fprintf(stderr, "vscsi_send_iu: Error sending response\n");
201 return rc1;
204 return rc;
207 static void vscsi_makeup_sense(VSCSIState *s, vscsi_req *req,
208 uint8_t key, uint8_t asc, uint8_t ascq)
210 req->senselen = SRP_RSP_SENSE_DATA_LEN;
212 /* Valid bit and 'current errors' */
213 req->sense[0] = (0x1 << 7 | 0x70);
214 /* Sense key */
215 req->sense[2] = key;
216 /* Additional sense length */
217 req->sense[7] = 0xa; /* 10 bytes */
218 /* Additional sense code */
219 req->sense[12] = asc;
220 req->sense[13] = ascq;
223 static int vscsi_send_rsp(VSCSIState *s, vscsi_req *req,
224 uint8_t status, int32_t res_in, int32_t res_out)
226 union viosrp_iu *iu = &req->iu;
227 uint64_t tag = iu->srp.rsp.tag;
228 int total_len = sizeof(iu->srp.rsp);
229 uint8_t sol_not = iu->srp.cmd.sol_not;
231 trace_spapr_vscsi_send_rsp(status, res_in, res_out);
233 memset(iu, 0, sizeof(struct srp_rsp));
234 iu->srp.rsp.opcode = SRP_RSP;
235 iu->srp.rsp.req_lim_delta = cpu_to_be32(1);
236 iu->srp.rsp.tag = tag;
238 /* Handle residuals */
239 if (res_in < 0) {
240 iu->srp.rsp.flags |= SRP_RSP_FLAG_DIUNDER;
241 res_in = -res_in;
242 } else if (res_in) {
243 iu->srp.rsp.flags |= SRP_RSP_FLAG_DIOVER;
245 if (res_out < 0) {
246 iu->srp.rsp.flags |= SRP_RSP_FLAG_DOUNDER;
247 res_out = -res_out;
248 } else if (res_out) {
249 iu->srp.rsp.flags |= SRP_RSP_FLAG_DOOVER;
251 iu->srp.rsp.data_in_res_cnt = cpu_to_be32(res_in);
252 iu->srp.rsp.data_out_res_cnt = cpu_to_be32(res_out);
254 /* We don't do response data */
255 /* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */
256 iu->srp.rsp.resp_data_len = cpu_to_be32(0);
258 /* Handle success vs. failure */
259 iu->srp.rsp.status = status;
260 if (status) {
261 iu->srp.rsp.sol_not = (sol_not & 0x04) >> 2;
262 if (req->senselen) {
263 req->iu.srp.rsp.flags |= SRP_RSP_FLAG_SNSVALID;
264 req->iu.srp.rsp.sense_data_len = cpu_to_be32(req->senselen);
265 memcpy(req->iu.srp.rsp.data, req->sense, req->senselen);
266 total_len += req->senselen;
268 } else {
269 iu->srp.rsp.sol_not = (sol_not & 0x02) >> 1;
272 vscsi_send_iu(s, req, total_len, VIOSRP_SRP_FORMAT);
273 return 0;
276 static inline struct srp_direct_buf vscsi_swap_desc(struct srp_direct_buf desc)
278 desc.va = be64_to_cpu(desc.va);
279 desc.len = be32_to_cpu(desc.len);
280 return desc;
283 static int vscsi_fetch_desc(VSCSIState *s, struct vscsi_req *req,
284 unsigned n, unsigned buf_offset,
285 struct srp_direct_buf *ret)
287 struct srp_cmd *cmd = &req->iu.srp.cmd;
289 switch (req->dma_fmt) {
290 case SRP_NO_DATA_DESC: {
291 trace_spapr_vscsi_fetch_desc_no_data();
292 return 0;
294 case SRP_DATA_DESC_DIRECT: {
295 memcpy(ret, cmd->add_data + req->cdb_offset, sizeof(*ret));
296 assert(req->cur_desc_num == 0);
297 trace_spapr_vscsi_fetch_desc_direct();
298 break;
300 case SRP_DATA_DESC_INDIRECT: {
301 struct srp_indirect_buf *tmp = (struct srp_indirect_buf *)
302 (cmd->add_data + req->cdb_offset);
303 if (n < req->local_desc) {
304 *ret = tmp->desc_list[n];
305 trace_spapr_vscsi_fetch_desc_indirect(req->qtag, n,
306 req->local_desc);
307 } else if (n < req->total_desc) {
308 int rc;
309 struct srp_direct_buf tbl_desc = vscsi_swap_desc(tmp->table_desc);
310 unsigned desc_offset = n * sizeof(struct srp_direct_buf);
312 if (desc_offset >= tbl_desc.len) {
313 trace_spapr_vscsi_fetch_desc_out_of_range(n, desc_offset);
314 return -1;
316 rc = spapr_vio_dma_read(&s->vdev, tbl_desc.va + desc_offset,
317 ret, sizeof(struct srp_direct_buf));
318 if (rc) {
319 trace_spapr_vscsi_fetch_desc_dma_read_error(rc);
320 return -1;
322 trace_spapr_vscsi_fetch_desc_indirect_seg_ext(req->qtag, n,
323 req->total_desc,
324 tbl_desc.va,
325 tbl_desc.len);
326 } else {
327 trace_spapr_vscsi_fetch_desc_out_of_desc();
328 return 0;
330 break;
332 default:
333 fprintf(stderr, "VSCSI: Unknown format %x\n", req->dma_fmt);
334 return -1;
337 *ret = vscsi_swap_desc(*ret);
338 if (buf_offset > ret->len) {
339 trace_spapr_vscsi_fetch_desc_out_of_desc_boundary(buf_offset,
340 req->cur_desc_num,
341 ret->len);
342 return -1;
344 ret->va += buf_offset;
345 ret->len -= buf_offset;
347 trace_spapr_vscsi_fetch_desc_done(req->cur_desc_num, req->cur_desc_offset,
348 ret->va, ret->len);
350 return ret->len ? 1 : 0;
353 static int vscsi_srp_direct_data(VSCSIState *s, vscsi_req *req,
354 uint8_t *buf, uint32_t len)
356 struct srp_direct_buf md;
357 uint32_t llen;
358 int rc = 0;
360 rc = vscsi_fetch_desc(s, req, req->cur_desc_num, req->cur_desc_offset, &md);
361 if (rc < 0) {
362 return -1;
363 } else if (rc == 0) {
364 return 0;
367 llen = MIN(len, md.len);
368 if (llen) {
369 if (req->writing) { /* writing = to device = reading from memory */
370 rc = spapr_vio_dma_read(&s->vdev, md.va, buf, llen);
371 } else {
372 rc = spapr_vio_dma_write(&s->vdev, md.va, buf, llen);
376 if (rc) {
377 return -1;
379 req->cur_desc_offset += llen;
381 return llen;
384 static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req,
385 uint8_t *buf, uint32_t len)
387 struct srp_direct_buf md;
388 int rc = 0;
389 uint32_t llen, total = 0;
391 trace_spapr_vscsi_srp_indirect_data(len);
393 /* While we have data ... */
394 while (len) {
395 rc = vscsi_fetch_desc(s, req, req->cur_desc_num, req->cur_desc_offset, &md);
396 if (rc < 0) {
397 return -1;
398 } else if (rc == 0) {
399 break;
402 /* Perform transfer */
403 llen = MIN(len, md.len);
404 if (req->writing) { /* writing = to device = reading from memory */
405 rc = spapr_vio_dma_read(&s->vdev, md.va, buf, llen);
406 } else {
407 rc = spapr_vio_dma_write(&s->vdev, md.va, buf, llen);
409 if (rc) {
410 trace_spapr_vscsi_srp_indirect_data_rw(req->writing, rc);
411 break;
413 trace_spapr_vscsi_srp_indirect_data_buf(buf[0], buf[1], buf[2], buf[3]);
415 len -= llen;
416 buf += llen;
418 total += llen;
420 /* Update current position in the current descriptor */
421 req->cur_desc_offset += llen;
422 if (md.len == llen) {
423 /* Go to the next descriptor if the current one finished */
424 ++req->cur_desc_num;
425 req->cur_desc_offset = 0;
429 return rc ? -1 : total;
432 static int vscsi_srp_transfer_data(VSCSIState *s, vscsi_req *req,
433 int writing, uint8_t *buf, uint32_t len)
435 int err = 0;
437 switch (req->dma_fmt) {
438 case SRP_NO_DATA_DESC:
439 trace_spapr_vscsi_srp_transfer_data(len);
440 break;
441 case SRP_DATA_DESC_DIRECT:
442 err = vscsi_srp_direct_data(s, req, buf, len);
443 break;
444 case SRP_DATA_DESC_INDIRECT:
445 err = vscsi_srp_indirect_data(s, req, buf, len);
446 break;
448 return err;
451 /* Bits from linux srp */
452 static int data_out_desc_size(struct srp_cmd *cmd)
454 int size = 0;
455 uint8_t fmt = cmd->buf_fmt >> 4;
457 switch (fmt) {
458 case SRP_NO_DATA_DESC:
459 break;
460 case SRP_DATA_DESC_DIRECT:
461 size = sizeof(struct srp_direct_buf);
462 break;
463 case SRP_DATA_DESC_INDIRECT:
464 size = sizeof(struct srp_indirect_buf) +
465 sizeof(struct srp_direct_buf)*cmd->data_out_desc_cnt;
466 break;
467 default:
468 break;
470 return size;
473 static int vscsi_preprocess_desc(vscsi_req *req)
475 struct srp_cmd *cmd = &req->iu.srp.cmd;
477 req->cdb_offset = cmd->add_cdb_len & ~3;
479 if (req->writing) {
480 req->dma_fmt = cmd->buf_fmt >> 4;
481 } else {
482 req->cdb_offset += data_out_desc_size(cmd);
483 req->dma_fmt = cmd->buf_fmt & ((1U << 4) - 1);
486 switch (req->dma_fmt) {
487 case SRP_NO_DATA_DESC:
488 break;
489 case SRP_DATA_DESC_DIRECT:
490 req->total_desc = req->local_desc = 1;
491 break;
492 case SRP_DATA_DESC_INDIRECT: {
493 struct srp_indirect_buf *ind_tmp = (struct srp_indirect_buf *)
494 (cmd->add_data + req->cdb_offset);
496 req->total_desc = be32_to_cpu(ind_tmp->table_desc.len) /
497 sizeof(struct srp_direct_buf);
498 req->local_desc = req->writing ? cmd->data_out_desc_cnt :
499 cmd->data_in_desc_cnt;
500 break;
502 default:
503 fprintf(stderr,
504 "vscsi_preprocess_desc: Unknown format %x\n", req->dma_fmt);
505 return -1;
508 return 0;
511 /* Callback to indicate that the SCSI layer has completed a transfer. */
512 static void vscsi_transfer_data(SCSIRequest *sreq, uint32_t len)
514 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
515 vscsi_req *req = sreq->hba_private;
516 uint8_t *buf;
517 int rc = 0;
519 trace_spapr_vscsi_transfer_data(sreq->tag, len, req);
520 if (req == NULL) {
521 fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
522 return;
525 if (len) {
526 buf = scsi_req_get_buf(sreq);
527 rc = vscsi_srp_transfer_data(s, req, req->writing, buf, len);
529 if (rc < 0) {
530 fprintf(stderr, "VSCSI: RDMA error rc=%d!\n", rc);
531 req->dma_error = true;
532 scsi_req_cancel(req->sreq);
533 return;
536 /* Start next chunk */
537 req->data_len -= rc;
538 scsi_req_continue(sreq);
541 /* Callback to indicate that the SCSI layer has completed a transfer. */
542 static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status, size_t resid)
544 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
545 vscsi_req *req = sreq->hba_private;
546 int32_t res_in = 0, res_out = 0;
548 trace_spapr_vscsi_command_complete(sreq->tag, status, req);
549 if (req == NULL) {
550 fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
551 return;
554 if (status == CHECK_CONDITION) {
555 req->senselen = scsi_req_get_sense(req->sreq, req->sense,
556 sizeof(req->sense));
557 trace_spapr_vscsi_command_complete_sense_data1(req->senselen,
558 req->sense[0], req->sense[1], req->sense[2], req->sense[3],
559 req->sense[4], req->sense[5], req->sense[6], req->sense[7]);
560 trace_spapr_vscsi_command_complete_sense_data2(
561 req->sense[8], req->sense[9], req->sense[10], req->sense[11],
562 req->sense[12], req->sense[13], req->sense[14], req->sense[15]);
565 trace_spapr_vscsi_command_complete_status(status);
566 if (status == 0) {
567 /* We handle overflows, not underflows for normal commands,
568 * but hopefully nobody cares
570 if (req->writing) {
571 res_out = req->data_len;
572 } else {
573 res_in = req->data_len;
576 vscsi_send_rsp(s, req, status, res_in, res_out);
577 vscsi_put_req(req);
580 static void vscsi_request_cancelled(SCSIRequest *sreq)
582 vscsi_req *req = sreq->hba_private;
584 if (req->dma_error) {
585 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
587 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
588 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
590 vscsi_put_req(req);
593 static const VMStateDescription vmstate_spapr_vscsi_req = {
594 .name = "spapr_vscsi_req",
595 .version_id = 1,
596 .minimum_version_id = 1,
597 .fields = (VMStateField[]) {
598 VMSTATE_BUFFER(crq.raw, vscsi_req),
599 VMSTATE_BUFFER(iu.srp.reserved, vscsi_req),
600 VMSTATE_UINT32(qtag, vscsi_req),
601 VMSTATE_BOOL(active, vscsi_req),
602 VMSTATE_UINT32(data_len, vscsi_req),
603 VMSTATE_BOOL(writing, vscsi_req),
604 VMSTATE_UINT32(senselen, vscsi_req),
605 VMSTATE_BUFFER(sense, vscsi_req),
606 VMSTATE_UINT8(dma_fmt, vscsi_req),
607 VMSTATE_UINT16(local_desc, vscsi_req),
608 VMSTATE_UINT16(total_desc, vscsi_req),
609 VMSTATE_UINT16(cdb_offset, vscsi_req),
610 /*Restart SCSI request from the beginning for now */
611 /*VMSTATE_UINT16(cur_desc_num, vscsi_req),
612 VMSTATE_UINT16(cur_desc_offset, vscsi_req),*/
613 VMSTATE_END_OF_LIST()
617 static void vscsi_save_request(QEMUFile *f, SCSIRequest *sreq)
619 vscsi_req *req = sreq->hba_private;
620 assert(req->active);
622 vmstate_save_state(f, &vmstate_spapr_vscsi_req, req, NULL);
624 trace_spapr_vscsi_save_request(req->qtag, req->cur_desc_num,
625 req->cur_desc_offset);
628 static void *vscsi_load_request(QEMUFile *f, SCSIRequest *sreq)
630 SCSIBus *bus = sreq->bus;
631 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(bus->qbus.parent);
632 vscsi_req *req;
633 int rc;
635 assert(sreq->tag < VSCSI_REQ_LIMIT);
636 req = &s->reqs[sreq->tag];
637 assert(!req->active);
639 memset(req, 0, sizeof(*req));
640 rc = vmstate_load_state(f, &vmstate_spapr_vscsi_req, req, 1);
641 if (rc) {
642 fprintf(stderr, "VSCSI: failed loading request tag#%u\n", sreq->tag);
643 return NULL;
645 assert(req->active);
647 req->sreq = scsi_req_ref(sreq);
649 trace_spapr_vscsi_load_request(req->qtag, req->cur_desc_num,
650 req->cur_desc_offset);
652 return req;
655 static void vscsi_process_login(VSCSIState *s, vscsi_req *req)
657 union viosrp_iu *iu = &req->iu;
658 struct srp_login_rsp *rsp = &iu->srp.login_rsp;
659 uint64_t tag = iu->srp.rsp.tag;
661 trace_spapr_vscsi_process_login();
663 /* TODO handle case that requested size is wrong and
664 * buffer format is wrong
666 memset(iu, 0, sizeof(struct srp_login_rsp));
667 rsp->opcode = SRP_LOGIN_RSP;
668 /* Don't advertise quite as many request as we support to
669 * keep room for management stuff etc...
671 rsp->req_lim_delta = cpu_to_be32(VSCSI_REQ_LIMIT-2);
672 rsp->tag = tag;
673 rsp->max_it_iu_len = cpu_to_be32(sizeof(union srp_iu));
674 rsp->max_ti_iu_len = cpu_to_be32(sizeof(union srp_iu));
675 /* direct and indirect */
676 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
678 vscsi_send_iu(s, req, sizeof(*rsp), VIOSRP_SRP_FORMAT);
681 static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
683 uint8_t *cdb = req->iu.srp.cmd.cdb;
684 uint8_t resp_data[36];
685 int rc, len, alen;
687 /* We don't do EVPD. Also check that page_code is 0 */
688 if ((cdb[1] & 0x01) || cdb[2] != 0) {
689 /* Send INVALID FIELD IN CDB */
690 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0);
691 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
692 return;
694 alen = cdb[3];
695 alen = (alen << 8) | cdb[4];
696 len = MIN(alen, 36);
698 /* Fake up inquiry using PQ=3 */
699 memset(resp_data, 0, 36);
700 resp_data[0] = 0x7f; /* Not capable of supporting a device here */
701 resp_data[2] = 0x06; /* SPS-4 */
702 resp_data[3] = 0x02; /* Resp data format */
703 resp_data[4] = 36 - 5; /* Additional length */
704 resp_data[7] = 0x10; /* Sync transfers */
705 memcpy(&resp_data[16], "QEMU EMPTY ", 16);
706 memcpy(&resp_data[8], "QEMU ", 8);
708 req->writing = 0;
709 vscsi_preprocess_desc(req);
710 rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
711 if (rc < 0) {
712 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
713 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
714 } else {
715 vscsi_send_rsp(s, req, 0, 36 - rc, 0);
719 static void vscsi_report_luns(VSCSIState *s, vscsi_req *req)
721 BusChild *kid;
722 int i, len, n, rc;
723 uint8_t *resp_data;
724 bool found_lun0;
726 n = 0;
727 found_lun0 = false;
728 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
729 SCSIDevice *dev = SCSI_DEVICE(kid->child);
731 n += 8;
732 if (dev->channel == 0 && dev->id == 0 && dev->lun == 0) {
733 found_lun0 = true;
736 if (!found_lun0) {
737 n += 8;
739 len = n+8;
741 resp_data = g_malloc0(len);
742 stl_be_p(resp_data, n);
743 i = found_lun0 ? 8 : 16;
744 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
745 DeviceState *qdev = kid->child;
746 SCSIDevice *dev = SCSI_DEVICE(qdev);
748 if (dev->id == 0 && dev->channel == 0) {
749 resp_data[i] = 0; /* Use simple LUN for 0 (SAM5 4.7.7.1) */
750 } else {
751 resp_data[i] = (2 << 6); /* Otherwise LUN addressing (4.7.7.4) */
753 resp_data[i] |= dev->id;
754 resp_data[i+1] = (dev->channel << 5);
755 resp_data[i+1] |= dev->lun;
756 i += 8;
759 vscsi_preprocess_desc(req);
760 rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
761 g_free(resp_data);
762 if (rc < 0) {
763 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
764 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
765 } else {
766 vscsi_send_rsp(s, req, 0, len - rc, 0);
770 static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req)
772 union srp_iu *srp = &req->iu.srp;
773 SCSIDevice *sdev;
774 int n, lun;
776 if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN)
777 && srp->cmd.cdb[0] == REPORT_LUNS) {
778 vscsi_report_luns(s, req);
779 return 0;
782 sdev = vscsi_device_find(&s->bus, be64_to_cpu(srp->cmd.lun), &lun);
783 if (!sdev) {
784 trace_spapr_vscsi_queue_cmd_no_drive(be64_to_cpu(srp->cmd.lun));
785 if (srp->cmd.cdb[0] == INQUIRY) {
786 vscsi_inquiry_no_target(s, req);
787 } else {
788 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0x00);
789 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
790 } return 1;
793 req->sreq = scsi_req_new(sdev, req->qtag, lun, srp->cmd.cdb, req);
794 n = scsi_req_enqueue(req->sreq);
796 trace_spapr_vscsi_queue_cmd(req->qtag, srp->cmd.cdb[0],
797 scsi_command_name(srp->cmd.cdb[0]), lun, n);
799 if (n) {
800 /* Transfer direction must be set before preprocessing the
801 * descriptors
803 req->writing = (n < 1);
805 /* Preprocess RDMA descriptors */
806 vscsi_preprocess_desc(req);
808 /* Get transfer direction and initiate transfer */
809 if (n > 0) {
810 req->data_len = n;
811 } else if (n < 0) {
812 req->data_len = -n;
814 scsi_req_continue(req->sreq);
816 /* Don't touch req here, it may have been recycled already */
818 return 0;
821 static int vscsi_process_tsk_mgmt(VSCSIState *s, vscsi_req *req)
823 union viosrp_iu *iu = &req->iu;
824 vscsi_req *tmpreq;
825 int i, lun = 0, resp = SRP_TSK_MGMT_COMPLETE;
826 SCSIDevice *d;
827 uint64_t tag = iu->srp.rsp.tag;
828 uint8_t sol_not = iu->srp.cmd.sol_not;
830 fprintf(stderr, "vscsi_process_tsk_mgmt %02x\n",
831 iu->srp.tsk_mgmt.tsk_mgmt_func);
833 d = vscsi_device_find(&s->bus, be64_to_cpu(req->iu.srp.tsk_mgmt.lun), &lun);
834 if (!d) {
835 resp = SRP_TSK_MGMT_FIELDS_INVALID;
836 } else {
837 switch (iu->srp.tsk_mgmt.tsk_mgmt_func) {
838 case SRP_TSK_ABORT_TASK:
839 if (d->lun != lun) {
840 resp = SRP_TSK_MGMT_FIELDS_INVALID;
841 break;
844 tmpreq = vscsi_find_req(s, req->iu.srp.tsk_mgmt.task_tag);
845 if (tmpreq && tmpreq->sreq) {
846 assert(tmpreq->sreq->hba_private);
847 scsi_req_cancel(tmpreq->sreq);
849 break;
851 case SRP_TSK_LUN_RESET:
852 if (d->lun != lun) {
853 resp = SRP_TSK_MGMT_FIELDS_INVALID;
854 break;
857 qdev_reset_all(&d->qdev);
858 break;
860 case SRP_TSK_ABORT_TASK_SET:
861 case SRP_TSK_CLEAR_TASK_SET:
862 if (d->lun != lun) {
863 resp = SRP_TSK_MGMT_FIELDS_INVALID;
864 break;
867 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
868 tmpreq = &s->reqs[i];
869 if (tmpreq->iu.srp.cmd.lun != req->iu.srp.tsk_mgmt.lun) {
870 continue;
872 if (!tmpreq->active || !tmpreq->sreq) {
873 continue;
875 assert(tmpreq->sreq->hba_private);
876 scsi_req_cancel(tmpreq->sreq);
878 break;
880 case SRP_TSK_CLEAR_ACA:
881 resp = SRP_TSK_MGMT_NOT_SUPPORTED;
882 break;
884 default:
885 resp = SRP_TSK_MGMT_FIELDS_INVALID;
886 break;
890 /* Compose the response here as */
891 memset(iu, 0, sizeof(struct srp_rsp) + 4);
892 iu->srp.rsp.opcode = SRP_RSP;
893 iu->srp.rsp.req_lim_delta = cpu_to_be32(1);
894 iu->srp.rsp.tag = tag;
895 iu->srp.rsp.flags |= SRP_RSP_FLAG_RSPVALID;
896 iu->srp.rsp.resp_data_len = cpu_to_be32(4);
897 if (resp) {
898 iu->srp.rsp.sol_not = (sol_not & 0x04) >> 2;
899 } else {
900 iu->srp.rsp.sol_not = (sol_not & 0x02) >> 1;
903 iu->srp.rsp.status = GOOD;
904 iu->srp.rsp.data[3] = resp;
906 vscsi_send_iu(s, req, sizeof(iu->srp.rsp) + 4, VIOSRP_SRP_FORMAT);
908 return 1;
911 static int vscsi_handle_srp_req(VSCSIState *s, vscsi_req *req)
913 union srp_iu *srp = &req->iu.srp;
914 int done = 1;
915 uint8_t opcode = srp->rsp.opcode;
917 switch (opcode) {
918 case SRP_LOGIN_REQ:
919 vscsi_process_login(s, req);
920 break;
921 case SRP_TSK_MGMT:
922 done = vscsi_process_tsk_mgmt(s, req);
923 break;
924 case SRP_CMD:
925 done = vscsi_queue_cmd(s, req);
926 break;
927 case SRP_LOGIN_RSP:
928 case SRP_I_LOGOUT:
929 case SRP_T_LOGOUT:
930 case SRP_RSP:
931 case SRP_CRED_REQ:
932 case SRP_CRED_RSP:
933 case SRP_AER_REQ:
934 case SRP_AER_RSP:
935 fprintf(stderr, "VSCSI: Unsupported opcode %02x\n", opcode);
936 break;
937 default:
938 fprintf(stderr, "VSCSI: Unknown type %02x\n", opcode);
941 return done;
944 static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
946 struct viosrp_adapter_info *sinfo;
947 struct mad_adapter_info_data info;
948 int rc;
950 sinfo = &req->iu.mad.adapter_info;
952 #if 0 /* What for ? */
953 rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(sinfo->buffer),
954 &info, be16_to_cpu(sinfo->common.length));
955 if (rc) {
956 fprintf(stderr, "vscsi_send_adapter_info: DMA read failure !\n");
958 #endif
959 memset(&info, 0, sizeof(info));
960 strcpy(info.srp_version, SRP_VERSION);
961 memcpy(info.partition_name, "qemu", sizeof("qemu"));
962 info.partition_number = cpu_to_be32(0);
963 info.mad_version = cpu_to_be32(1);
964 info.os_type = cpu_to_be32(2);
965 info.port_max_txu[0] = cpu_to_be32(VSCSI_MAX_SECTORS << 9);
967 rc = spapr_vio_dma_write(&s->vdev, be64_to_cpu(sinfo->buffer),
968 &info, be16_to_cpu(sinfo->common.length));
969 if (rc) {
970 fprintf(stderr, "vscsi_send_adapter_info: DMA write failure !\n");
973 sinfo->common.status = rc ? cpu_to_be32(1) : 0;
975 return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
978 static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
980 struct viosrp_capabilities *vcap;
981 struct capabilities cap = { };
982 uint16_t len, req_len;
983 uint64_t buffer;
984 int rc;
986 vcap = &req->iu.mad.capabilities;
987 req_len = len = be16_to_cpu(vcap->common.length);
988 buffer = be64_to_cpu(vcap->buffer);
989 if (len > sizeof(cap)) {
990 fprintf(stderr, "vscsi_send_capabilities: capabilities size mismatch !\n");
993 * Just read and populate the structure that is known.
994 * Zero rest of the structure.
996 len = sizeof(cap);
998 rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
999 if (rc) {
1000 fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");
1004 * Current implementation does not suppport any migration or
1005 * reservation capabilities. Construct the response telling the
1006 * guest not to use them.
1008 cap.flags = 0;
1009 cap.migration.ecl = 0;
1010 cap.reserve.type = 0;
1011 cap.migration.common.server_support = 0;
1012 cap.reserve.common.server_support = 0;
1014 rc = spapr_vio_dma_write(&s->vdev, buffer, &cap, len);
1015 if (rc) {
1016 fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
1018 if (req_len > len) {
1020 * Being paranoid and lets not worry about the error code
1021 * here. Actual write of the cap is done above.
1023 spapr_vio_dma_set(&s->vdev, (buffer + len), 0, (req_len - len));
1025 vcap->common.status = rc ? cpu_to_be32(1) : 0;
1026 return vscsi_send_iu(s, req, sizeof(*vcap), VIOSRP_MAD_FORMAT);
1029 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
1031 union mad_iu *mad = &req->iu.mad;
1032 bool request_handled = false;
1033 uint64_t retlen = 0;
1035 switch (be32_to_cpu(mad->empty_iu.common.type)) {
1036 case VIOSRP_EMPTY_IU_TYPE:
1037 fprintf(stderr, "Unsupported EMPTY MAD IU\n");
1038 retlen = sizeof(mad->empty_iu);
1039 break;
1040 case VIOSRP_ERROR_LOG_TYPE:
1041 fprintf(stderr, "Unsupported ERROR LOG MAD IU\n");
1042 retlen = sizeof(mad->error_log);
1043 break;
1044 case VIOSRP_ADAPTER_INFO_TYPE:
1045 vscsi_send_adapter_info(s, req);
1046 request_handled = true;
1047 break;
1048 case VIOSRP_HOST_CONFIG_TYPE:
1049 retlen = sizeof(mad->host_config);
1050 break;
1051 case VIOSRP_CAPABILITIES_TYPE:
1052 vscsi_send_capabilities(s, req);
1053 request_handled = true;
1054 break;
1055 default:
1056 fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
1057 be32_to_cpu(mad->empty_iu.common.type));
1059 * PAPR+ says that "The length field is set to the length
1060 * of the data structure(s) used in the command".
1061 * As we did not recognize the request type, put zero there.
1063 retlen = 0;
1066 if (!request_handled) {
1067 mad->empty_iu.common.status = cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED);
1068 vscsi_send_iu(s, req, retlen, VIOSRP_MAD_FORMAT);
1071 return 1;
1074 static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq)
1076 vscsi_req *req;
1077 int done;
1079 req = vscsi_get_req(s);
1080 if (req == NULL) {
1081 fprintf(stderr, "VSCSI: Failed to get a request !\n");
1082 return;
1085 /* We only support a limited number of descriptors, we know
1086 * the ibmvscsi driver uses up to 10 max, so it should fit
1087 * in our 256 bytes IUs. If not we'll have to increase the size
1088 * of the structure.
1090 if (crq->s.IU_length > sizeof(union viosrp_iu)) {
1091 fprintf(stderr, "VSCSI: SRP IU too long (%d bytes) !\n",
1092 crq->s.IU_length);
1093 vscsi_put_req(req);
1094 return;
1097 /* XXX Handle failure differently ? */
1098 if (spapr_vio_dma_read(&s->vdev, crq->s.IU_data_ptr, &req->iu,
1099 crq->s.IU_length)) {
1100 fprintf(stderr, "vscsi_got_payload: DMA read failure !\n");
1101 vscsi_put_req(req);
1102 return;
1104 memcpy(&req->crq, crq, sizeof(vscsi_crq));
1106 if (crq->s.format == VIOSRP_MAD_FORMAT) {
1107 done = vscsi_handle_mad_req(s, req);
1108 } else {
1109 done = vscsi_handle_srp_req(s, req);
1112 if (done) {
1113 vscsi_put_req(req);
1118 static int vscsi_do_crq(struct VIOsPAPRDevice *dev, uint8_t *crq_data)
1120 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
1121 vscsi_crq crq;
1123 memcpy(crq.raw, crq_data, 16);
1124 crq.s.timeout = be16_to_cpu(crq.s.timeout);
1125 crq.s.IU_length = be16_to_cpu(crq.s.IU_length);
1126 crq.s.IU_data_ptr = be64_to_cpu(crq.s.IU_data_ptr);
1128 trace_spapr_vscsi_do_crq(crq.raw[0], crq.raw[1]);
1130 switch (crq.s.valid) {
1131 case 0xc0: /* Init command/response */
1133 /* Respond to initialization request */
1134 if (crq.s.format == 0x01) {
1135 memset(crq.raw, 0, 16);
1136 crq.s.valid = 0xc0;
1137 crq.s.format = 0x02;
1138 spapr_vio_send_crq(dev, crq.raw);
1141 /* Note that in hotplug cases, we might get a 0x02
1142 * as a result of us emitting the init request
1145 break;
1146 case 0xff: /* Link event */
1148 /* Not handled for now */
1150 break;
1151 case 0x80: /* Payloads */
1152 switch (crq.s.format) {
1153 case VIOSRP_SRP_FORMAT: /* AKA VSCSI request */
1154 case VIOSRP_MAD_FORMAT: /* AKA VSCSI response */
1155 vscsi_got_payload(s, &crq);
1156 break;
1157 case VIOSRP_OS400_FORMAT:
1158 case VIOSRP_AIX_FORMAT:
1159 case VIOSRP_LINUX_FORMAT:
1160 case VIOSRP_INLINE_FORMAT:
1161 fprintf(stderr, "vscsi_do_srq: Unsupported payload format %02x\n",
1162 crq.s.format);
1163 break;
1164 default:
1165 fprintf(stderr, "vscsi_do_srq: Unknown payload format %02x\n",
1166 crq.s.format);
1168 break;
1169 default:
1170 fprintf(stderr, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
1171 crq.raw[0], crq.raw[1]);
1174 return 0;
1177 static const struct SCSIBusInfo vscsi_scsi_info = {
1178 .tcq = true,
1179 .max_channel = 7, /* logical unit addressing format */
1180 .max_target = 63,
1181 .max_lun = 31,
1183 .transfer_data = vscsi_transfer_data,
1184 .complete = vscsi_command_complete,
1185 .cancel = vscsi_request_cancelled,
1186 .save_request = vscsi_save_request,
1187 .load_request = vscsi_load_request,
1190 static void spapr_vscsi_reset(VIOsPAPRDevice *dev)
1192 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
1193 int i;
1195 memset(s->reqs, 0, sizeof(s->reqs));
1196 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
1197 s->reqs[i].qtag = i;
1201 static void spapr_vscsi_realize(VIOsPAPRDevice *dev, Error **errp)
1203 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
1205 dev->crq.SendFunc = vscsi_do_crq;
1207 scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev),
1208 &vscsi_scsi_info, NULL);
1209 if (!dev->qdev.hotplugged) {
1210 scsi_bus_legacy_handle_cmdline(&s->bus, errp);
1214 void spapr_vscsi_create(VIOsPAPRBus *bus)
1216 DeviceState *dev;
1218 dev = qdev_create(&bus->bus, "spapr-vscsi");
1220 qdev_init_nofail(dev);
1223 static int spapr_vscsi_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
1225 int ret;
1227 ret = fdt_setprop_cell(fdt, node_off, "#address-cells", 2);
1228 if (ret < 0) {
1229 return ret;
1232 ret = fdt_setprop_cell(fdt, node_off, "#size-cells", 0);
1233 if (ret < 0) {
1234 return ret;
1237 return 0;
1240 static Property spapr_vscsi_properties[] = {
1241 DEFINE_SPAPR_PROPERTIES(VSCSIState, vdev),
1242 DEFINE_PROP_END_OF_LIST(),
1245 static const VMStateDescription vmstate_spapr_vscsi = {
1246 .name = "spapr_vscsi",
1247 .version_id = 1,
1248 .minimum_version_id = 1,
1249 .fields = (VMStateField[]) {
1250 VMSTATE_SPAPR_VIO(vdev, VSCSIState),
1251 /* VSCSI state */
1252 /* ???? */
1254 VMSTATE_END_OF_LIST()
1258 static void spapr_vscsi_class_init(ObjectClass *klass, void *data)
1260 DeviceClass *dc = DEVICE_CLASS(klass);
1261 VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
1263 k->realize = spapr_vscsi_realize;
1264 k->reset = spapr_vscsi_reset;
1265 k->devnode = spapr_vscsi_devnode;
1266 k->dt_name = "v-scsi";
1267 k->dt_type = "vscsi";
1268 k->dt_compatible = "IBM,v-scsi";
1269 k->signal_mask = 0x00000001;
1270 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1271 dc->props = spapr_vscsi_properties;
1272 k->rtce_window_size = 0x10000000;
1273 dc->vmsd = &vmstate_spapr_vscsi;
1276 static const TypeInfo spapr_vscsi_info = {
1277 .name = TYPE_VIO_SPAPR_VSCSI_DEVICE,
1278 .parent = TYPE_VIO_SPAPR_DEVICE,
1279 .instance_size = sizeof(VSCSIState),
1280 .class_init = spapr_vscsi_class_init,
1283 static void spapr_vscsi_register_types(void)
1285 type_register_static(&spapr_vscsi_info);
1288 type_init(spapr_vscsi_register_types)