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
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"
38 #include "hw/scsi/scsi.h"
39 #include "block/scsi.h"
42 #include "hw/ppc/spapr.h"
43 #include "hw/ppc/spapr_vio.h"
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
{
66 typedef struct vscsi_req
{
70 /* SCSI request tracking */
72 uint32_t qtag
; /* qemu tag != srp tag */
78 uint8_t sense
[SCSI_SENSE_BUF_SIZE
];
80 /* RDMA related bits */
85 uint16_t cur_desc_num
;
86 uint16_t cur_desc_offset
;
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)
96 vscsi_req reqs
[VSCSI_REQ_LIMIT
];
99 static struct vscsi_req
*vscsi_get_req(VSCSIState
*s
)
104 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
107 memset(req
, 0, sizeof(*req
));
116 static struct vscsi_req
*vscsi_find_req(VSCSIState
*s
, uint64_t srp_tag
)
121 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
123 if (req
->iu
.srp
.cmd
.tag
== srp_tag
) {
130 static void vscsi_put_req(vscsi_req
*req
)
132 if (req
->sreq
!= NULL
) {
133 scsi_req_unref(req
->sreq
);
139 static SCSIDevice
*vscsi_device_find(SCSIBus
*bus
, uint64_t srp_lun
, int *lun
)
141 int channel
= 0, id
= 0;
144 switch (srp_lun
>> 62) {
146 if ((srp_lun
>> 56) != 0) {
147 channel
= (srp_lun
>> 56) & 0x3f;
148 id
= (srp_lun
>> 48) & 0xff;
152 *lun
= (srp_lun
>> 48) & 0xff;
156 *lun
= (srp_lun
>> 48) & 0x3fff;
159 channel
= (srp_lun
>> 53) & 0x7;
160 id
= (srp_lun
>> 56) & 0x3f;
161 *lun
= (srp_lun
>> 48) & 0x1f;
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
)
178 /* First copy the SRP */
179 rc
= spapr_vio_dma_write(&s
->vdev
, req
->crq
.s
.IU_data_ptr
,
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 */
193 req
->crq
.s
.status
= VIOSRP_OK
;
195 req
->crq
.s
.status
= VIOSRP_ADAPTER_FAIL
;
198 rc1
= spapr_vio_send_crq(&s
->vdev
, req
->crq
.raw
);
200 fprintf(stderr
, "vscsi_send_iu: Error sending response\n");
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);
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 */
240 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DIUNDER
;
243 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DIOVER
;
246 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DOUNDER
;
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
;
261 iu
->srp
.rsp
.sol_not
= (sol_not
& 0x04) >> 2;
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
;
269 iu
->srp
.rsp
.sol_not
= (sol_not
& 0x02) >> 1;
272 vscsi_send_iu(s
, req
, total_len
, VIOSRP_SRP_FORMAT
);
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
);
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();
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();
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
,
307 } else if (n
< req
->total_desc
) {
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
);
316 rc
= spapr_vio_dma_read(&s
->vdev
, tbl_desc
.va
+ desc_offset
,
317 ret
, sizeof(struct srp_direct_buf
));
319 trace_spapr_vscsi_fetch_desc_dma_read_error(rc
);
322 trace_spapr_vscsi_fetch_desc_indirect_seg_ext(req
->qtag
, n
,
327 trace_spapr_vscsi_fetch_desc_out_of_desc();
333 fprintf(stderr
, "VSCSI: Unknown format %x\n", req
->dma_fmt
);
337 *ret
= vscsi_swap_desc(*ret
);
338 if (buf_offset
> ret
->len
) {
339 trace_spapr_vscsi_fetch_desc_out_of_desc_boundary(buf_offset
,
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
,
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
;
360 rc
= vscsi_fetch_desc(s
, req
, req
->cur_desc_num
, req
->cur_desc_offset
, &md
);
363 } else if (rc
== 0) {
367 llen
= MIN(len
, md
.len
);
369 if (req
->writing
) { /* writing = to device = reading from memory */
370 rc
= spapr_vio_dma_read(&s
->vdev
, md
.va
, buf
, llen
);
372 rc
= spapr_vio_dma_write(&s
->vdev
, md
.va
, buf
, llen
);
379 req
->cur_desc_offset
+= 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
;
389 uint32_t llen
, total
= 0;
391 trace_spapr_vscsi_srp_indirect_data(len
);
393 /* While we have data ... */
395 rc
= vscsi_fetch_desc(s
, req
, req
->cur_desc_num
, req
->cur_desc_offset
, &md
);
398 } else if (rc
== 0) {
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
);
407 rc
= spapr_vio_dma_write(&s
->vdev
, md
.va
, buf
, llen
);
410 trace_spapr_vscsi_srp_indirect_data_rw(req
->writing
, rc
);
413 trace_spapr_vscsi_srp_indirect_data_buf(buf
[0], buf
[1], buf
[2], buf
[3]);
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 */
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
)
437 switch (req
->dma_fmt
) {
438 case SRP_NO_DATA_DESC
:
439 trace_spapr_vscsi_srp_transfer_data(len
);
441 case SRP_DATA_DESC_DIRECT
:
442 err
= vscsi_srp_direct_data(s
, req
, buf
, len
);
444 case SRP_DATA_DESC_INDIRECT
:
445 err
= vscsi_srp_indirect_data(s
, req
, buf
, len
);
451 /* Bits from linux srp */
452 static int data_out_desc_size(struct srp_cmd
*cmd
)
455 uint8_t fmt
= cmd
->buf_fmt
>> 4;
458 case SRP_NO_DATA_DESC
:
460 case SRP_DATA_DESC_DIRECT
:
461 size
= sizeof(struct srp_direct_buf
);
463 case SRP_DATA_DESC_INDIRECT
:
464 size
= sizeof(struct srp_indirect_buf
) +
465 sizeof(struct srp_direct_buf
)*cmd
->data_out_desc_cnt
;
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;
480 req
->dma_fmt
= cmd
->buf_fmt
>> 4;
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
:
489 case SRP_DATA_DESC_DIRECT
:
490 req
->total_desc
= req
->local_desc
= 1;
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
;
504 "vscsi_preprocess_desc: Unknown format %x\n", req
->dma_fmt
);
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
;
519 trace_spapr_vscsi_transfer_data(sreq
->tag
, len
, req
);
521 fprintf(stderr
, "VSCSI: Can't find request for tag 0x%x\n", sreq
->tag
);
526 buf
= scsi_req_get_buf(sreq
);
527 rc
= vscsi_srp_transfer_data(s
, req
, req
->writing
, buf
, len
);
530 fprintf(stderr
, "VSCSI: RDMA error rc=%d!\n", rc
);
531 req
->dma_error
= true;
532 scsi_req_cancel(req
->sreq
);
536 /* Start next chunk */
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
);
550 fprintf(stderr
, "VSCSI: Can't find request for tag 0x%x\n", sreq
->tag
);
554 if (status
== CHECK_CONDITION
) {
555 req
->senselen
= scsi_req_get_sense(req
->sreq
, 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
);
567 /* We handle overflows, not underflows for normal commands,
568 * but hopefully nobody cares
571 res_out
= req
->data_len
;
573 res_in
= req
->data_len
;
576 vscsi_send_rsp(s
, req
, status
, res_in
, res_out
);
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);
593 static const VMStateDescription vmstate_spapr_vscsi_req
= {
594 .name
= "spapr_vscsi_req",
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
;
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
);
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);
642 fprintf(stderr
, "VSCSI: failed loading request tag#%u\n", sreq
->tag
);
647 req
->sreq
= scsi_req_ref(sreq
);
649 trace_spapr_vscsi_load_request(req
->qtag
, req
->cur_desc_num
,
650 req
->cur_desc_offset
);
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);
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];
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);
695 alen
= (alen
<< 8) | cdb
[4];
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);
709 vscsi_preprocess_desc(req
);
710 rc
= vscsi_srp_transfer_data(s
, req
, 0, resp_data
, len
);
712 vscsi_makeup_sense(s
, req
, HARDWARE_ERROR
, 0, 0);
713 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
715 vscsi_send_rsp(s
, req
, 0, 36 - rc
, 0);
719 static void vscsi_report_luns(VSCSIState
*s
, vscsi_req
*req
)
728 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
729 SCSIDevice
*dev
= SCSI_DEVICE(kid
->child
);
732 if (dev
->channel
== 0 && dev
->id
== 0 && dev
->lun
== 0) {
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) */
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
;
759 vscsi_preprocess_desc(req
);
760 rc
= vscsi_srp_transfer_data(s
, req
, 0, resp_data
, len
);
763 vscsi_makeup_sense(s
, req
, HARDWARE_ERROR
, 0, 0);
764 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
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
;
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
);
782 sdev
= vscsi_device_find(&s
->bus
, be64_to_cpu(srp
->cmd
.lun
), &lun
);
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
);
788 vscsi_makeup_sense(s
, req
, ILLEGAL_REQUEST
, 0x24, 0x00);
789 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
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
);
800 /* Transfer direction must be set before preprocessing the
803 req
->writing
= (n
< 1);
805 /* Preprocess RDMA descriptors */
806 vscsi_preprocess_desc(req
);
808 /* Get transfer direction and initiate transfer */
814 scsi_req_continue(req
->sreq
);
816 /* Don't touch req here, it may have been recycled already */
821 static int vscsi_process_tsk_mgmt(VSCSIState
*s
, vscsi_req
*req
)
823 union viosrp_iu
*iu
= &req
->iu
;
825 int i
, lun
= 0, resp
= SRP_TSK_MGMT_COMPLETE
;
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
);
835 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
837 switch (iu
->srp
.tsk_mgmt
.tsk_mgmt_func
) {
838 case SRP_TSK_ABORT_TASK
:
840 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
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
);
851 case SRP_TSK_LUN_RESET
:
853 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
857 qdev_reset_all(&d
->qdev
);
860 case SRP_TSK_ABORT_TASK_SET
:
861 case SRP_TSK_CLEAR_TASK_SET
:
863 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
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
) {
872 if (!tmpreq
->active
|| !tmpreq
->sreq
) {
875 assert(tmpreq
->sreq
->hba_private
);
876 scsi_req_cancel(tmpreq
->sreq
);
880 case SRP_TSK_CLEAR_ACA
:
881 resp
= SRP_TSK_MGMT_NOT_SUPPORTED
;
885 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
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);
898 iu
->srp
.rsp
.sol_not
= (sol_not
& 0x04) >> 2;
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
);
911 static int vscsi_handle_srp_req(VSCSIState
*s
, vscsi_req
*req
)
913 union srp_iu
*srp
= &req
->iu
.srp
;
915 uint8_t opcode
= srp
->rsp
.opcode
;
919 vscsi_process_login(s
, req
);
922 done
= vscsi_process_tsk_mgmt(s
, req
);
925 done
= vscsi_queue_cmd(s
, req
);
935 fprintf(stderr
, "VSCSI: Unsupported opcode %02x\n", opcode
);
938 fprintf(stderr
, "VSCSI: Unknown type %02x\n", opcode
);
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
;
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
));
956 fprintf(stderr
, "vscsi_send_adapter_info: DMA read failure !\n");
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
));
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
;
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.
998 rc
= spapr_vio_dma_read(&s
->vdev
, buffer
, &cap
, len
);
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.
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
);
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
);
1040 case VIOSRP_ERROR_LOG_TYPE
:
1041 fprintf(stderr
, "Unsupported ERROR LOG MAD IU\n");
1042 retlen
= sizeof(mad
->error_log
);
1044 case VIOSRP_ADAPTER_INFO_TYPE
:
1045 vscsi_send_adapter_info(s
, req
);
1046 request_handled
= true;
1048 case VIOSRP_HOST_CONFIG_TYPE
:
1049 retlen
= sizeof(mad
->host_config
);
1051 case VIOSRP_CAPABILITIES_TYPE
:
1052 vscsi_send_capabilities(s
, req
);
1053 request_handled
= true;
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.
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
);
1074 static void vscsi_got_payload(VSCSIState
*s
, vscsi_crq
*crq
)
1079 req
= vscsi_get_req(s
);
1081 fprintf(stderr
, "VSCSI: Failed to get a request !\n");
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
1090 if (crq
->s
.IU_length
> sizeof(union viosrp_iu
)) {
1091 fprintf(stderr
, "VSCSI: SRP IU too long (%d bytes) !\n",
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");
1104 memcpy(&req
->crq
, crq
, sizeof(vscsi_crq
));
1106 if (crq
->s
.format
== VIOSRP_MAD_FORMAT
) {
1107 done
= vscsi_handle_mad_req(s
, req
);
1109 done
= vscsi_handle_srp_req(s
, req
);
1118 static int vscsi_do_crq(struct VIOsPAPRDevice
*dev
, uint8_t *crq_data
)
1120 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(dev
);
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);
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
1146 case 0xff: /* Link event */
1148 /* Not handled for now */
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
);
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",
1165 fprintf(stderr
, "vscsi_do_srq: Unknown payload format %02x\n",
1170 fprintf(stderr
, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
1171 crq
.raw
[0], crq
.raw
[1]);
1177 static const struct SCSIBusInfo vscsi_scsi_info
= {
1179 .max_channel
= 7, /* logical unit addressing format */
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
);
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
)
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
)
1227 ret
= fdt_setprop_cell(fdt
, node_off
, "#address-cells", 2);
1232 ret
= fdt_setprop_cell(fdt
, node_off
, "#size-cells", 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",
1248 .minimum_version_id
= 1,
1249 .fields
= (VMStateField
[]) {
1250 VMSTATE_SPAPR_VIO(vdev
, VSCSIState
),
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
)