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)
35 #include "qemu/osdep.h"
36 #include "qemu/module.h"
38 #include "hw/scsi/scsi.h"
39 #include "migration/vmstate.h"
40 #include "scsi/constants.h"
42 #include "hw/ppc/spapr.h"
43 #include "hw/ppc/spapr_vio.h"
44 #include "hw/qdev-properties.h"
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
{
67 typedef struct vscsi_req
{
71 /* SCSI request tracking */
73 uint32_t qtag
; /* qemu tag != srp tag */
79 uint8_t sense
[SCSI_SENSE_BUF_SIZE
];
81 /* RDMA related bits */
86 uint16_t cur_desc_num
;
87 uint16_t cur_desc_offset
;
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)
97 vscsi_req reqs
[VSCSI_REQ_LIMIT
];
100 static struct vscsi_req
*vscsi_get_req(VSCSIState
*s
)
105 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
108 memset(req
, 0, sizeof(*req
));
117 static struct vscsi_req
*vscsi_find_req(VSCSIState
*s
, uint64_t srp_tag
)
122 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
124 if (req
->iu
.srp
.cmd
.tag
== srp_tag
) {
131 static void vscsi_put_req(vscsi_req
*req
)
133 if (req
->sreq
!= NULL
) {
134 scsi_req_unref(req
->sreq
);
140 static SCSIDevice
*vscsi_device_find(SCSIBus
*bus
, uint64_t srp_lun
, int *lun
)
142 int channel
= 0, id
= 0;
145 switch (srp_lun
>> 62) {
147 if ((srp_lun
>> 56) != 0) {
148 channel
= (srp_lun
>> 56) & 0x3f;
149 id
= (srp_lun
>> 48) & 0xff;
153 *lun
= (srp_lun
>> 48) & 0xff;
157 *lun
= (srp_lun
>> 48) & 0x3fff;
160 channel
= (srp_lun
>> 53) & 0x7;
161 id
= (srp_lun
>> 56) & 0x3f;
162 *lun
= (srp_lun
>> 48) & 0x1f;
171 return scsi_device_find(bus
, channel
, id
, *lun
);
174 static int vscsi_send_iu(VSCSIState
*s
, vscsi_req
*req
,
175 uint64_t length
, uint8_t format
)
179 /* First copy the SRP */
180 rc
= spapr_vio_dma_write(&s
->vdev
, req
->crq
.s
.IU_data_ptr
,
183 fprintf(stderr
, "vscsi_send_iu: DMA write failure !\n");
186 req
->crq
.s
.valid
= 0x80;
187 req
->crq
.s
.format
= format
;
188 req
->crq
.s
.reserved
= 0x00;
189 req
->crq
.s
.timeout
= cpu_to_be16(0x0000);
190 req
->crq
.s
.IU_length
= cpu_to_be16(length
);
191 req
->crq
.s
.IU_data_ptr
= req
->iu
.srp
.rsp
.tag
; /* right byte order */
194 req
->crq
.s
.status
= VIOSRP_OK
;
196 req
->crq
.s
.status
= VIOSRP_ADAPTER_FAIL
;
199 rc1
= spapr_vio_send_crq(&s
->vdev
, req
->crq
.raw
);
201 fprintf(stderr
, "vscsi_send_iu: Error sending response\n");
208 static void vscsi_makeup_sense(VSCSIState
*s
, vscsi_req
*req
,
209 uint8_t key
, uint8_t asc
, uint8_t ascq
)
211 req
->senselen
= SRP_RSP_SENSE_DATA_LEN
;
213 /* Valid bit and 'current errors' */
214 req
->sense
[0] = (0x1 << 7 | 0x70);
217 /* Additional sense length */
218 req
->sense
[7] = 0xa; /* 10 bytes */
219 /* Additional sense code */
220 req
->sense
[12] = asc
;
221 req
->sense
[13] = ascq
;
224 static int vscsi_send_rsp(VSCSIState
*s
, vscsi_req
*req
,
225 uint8_t status
, int32_t res_in
, int32_t res_out
)
227 union viosrp_iu
*iu
= &req
->iu
;
228 uint64_t tag
= iu
->srp
.rsp
.tag
;
229 int total_len
= sizeof(iu
->srp
.rsp
);
230 uint8_t sol_not
= iu
->srp
.cmd
.sol_not
;
232 trace_spapr_vscsi_send_rsp(status
, res_in
, res_out
);
234 memset(iu
, 0, sizeof(struct srp_rsp
));
235 iu
->srp
.rsp
.opcode
= SRP_RSP
;
236 iu
->srp
.rsp
.req_lim_delta
= cpu_to_be32(1);
237 iu
->srp
.rsp
.tag
= tag
;
239 /* Handle residuals */
241 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DIUNDER
;
244 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DIOVER
;
247 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DOUNDER
;
249 } else if (res_out
) {
250 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DOOVER
;
252 iu
->srp
.rsp
.data_in_res_cnt
= cpu_to_be32(res_in
);
253 iu
->srp
.rsp
.data_out_res_cnt
= cpu_to_be32(res_out
);
255 /* We don't do response data */
256 /* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */
257 iu
->srp
.rsp
.resp_data_len
= cpu_to_be32(0);
259 /* Handle success vs. failure */
260 iu
->srp
.rsp
.status
= status
;
262 iu
->srp
.rsp
.sol_not
= (sol_not
& 0x04) >> 2;
264 req
->iu
.srp
.rsp
.flags
|= SRP_RSP_FLAG_SNSVALID
;
265 req
->iu
.srp
.rsp
.sense_data_len
= cpu_to_be32(req
->senselen
);
266 memcpy(req
->iu
.srp
.rsp
.data
, req
->sense
, req
->senselen
);
267 total_len
+= req
->senselen
;
270 iu
->srp
.rsp
.sol_not
= (sol_not
& 0x02) >> 1;
273 vscsi_send_iu(s
, req
, total_len
, VIOSRP_SRP_FORMAT
);
277 static inline struct srp_direct_buf
vscsi_swap_desc(struct srp_direct_buf desc
)
279 desc
.va
= be64_to_cpu(desc
.va
);
280 desc
.len
= be32_to_cpu(desc
.len
);
284 static int vscsi_fetch_desc(VSCSIState
*s
, struct vscsi_req
*req
,
285 unsigned n
, unsigned buf_offset
,
286 struct srp_direct_buf
*ret
)
288 struct srp_cmd
*cmd
= &req
->iu
.srp
.cmd
;
290 switch (req
->dma_fmt
) {
291 case SRP_NO_DATA_DESC
: {
292 trace_spapr_vscsi_fetch_desc_no_data();
295 case SRP_DATA_DESC_DIRECT
: {
296 memcpy(ret
, cmd
->add_data
+ req
->cdb_offset
, sizeof(*ret
));
297 assert(req
->cur_desc_num
== 0);
298 trace_spapr_vscsi_fetch_desc_direct();
301 case SRP_DATA_DESC_INDIRECT
: {
302 struct srp_indirect_buf
*tmp
= (struct srp_indirect_buf
*)
303 (cmd
->add_data
+ req
->cdb_offset
);
304 if (n
< req
->local_desc
) {
305 *ret
= tmp
->desc_list
[n
];
306 trace_spapr_vscsi_fetch_desc_indirect(req
->qtag
, n
,
308 } else if (n
< req
->total_desc
) {
310 struct srp_direct_buf tbl_desc
= vscsi_swap_desc(tmp
->table_desc
);
311 unsigned desc_offset
= n
* sizeof(struct srp_direct_buf
);
313 if (desc_offset
>= tbl_desc
.len
) {
314 trace_spapr_vscsi_fetch_desc_out_of_range(n
, desc_offset
);
317 rc
= spapr_vio_dma_read(&s
->vdev
, tbl_desc
.va
+ desc_offset
,
318 ret
, sizeof(struct srp_direct_buf
));
320 trace_spapr_vscsi_fetch_desc_dma_read_error(rc
);
323 trace_spapr_vscsi_fetch_desc_indirect_seg_ext(req
->qtag
, n
,
328 trace_spapr_vscsi_fetch_desc_out_of_desc();
334 fprintf(stderr
, "VSCSI: Unknown format %x\n", req
->dma_fmt
);
338 *ret
= vscsi_swap_desc(*ret
);
339 if (buf_offset
> ret
->len
) {
340 trace_spapr_vscsi_fetch_desc_out_of_desc_boundary(buf_offset
,
345 ret
->va
+= buf_offset
;
346 ret
->len
-= buf_offset
;
348 trace_spapr_vscsi_fetch_desc_done(req
->cur_desc_num
, req
->cur_desc_offset
,
351 return ret
->len
? 1 : 0;
354 static int vscsi_srp_direct_data(VSCSIState
*s
, vscsi_req
*req
,
355 uint8_t *buf
, uint32_t len
)
357 struct srp_direct_buf md
;
361 rc
= vscsi_fetch_desc(s
, req
, req
->cur_desc_num
, req
->cur_desc_offset
, &md
);
364 } else if (rc
== 0) {
368 llen
= MIN(len
, md
.len
);
370 if (req
->writing
) { /* writing = to device = reading from memory */
371 rc
= spapr_vio_dma_read(&s
->vdev
, md
.va
, buf
, llen
);
373 rc
= spapr_vio_dma_write(&s
->vdev
, md
.va
, buf
, llen
);
380 req
->cur_desc_offset
+= llen
;
385 static int vscsi_srp_indirect_data(VSCSIState
*s
, vscsi_req
*req
,
386 uint8_t *buf
, uint32_t len
)
388 struct srp_direct_buf md
;
390 uint32_t llen
, total
= 0;
392 trace_spapr_vscsi_srp_indirect_data(len
);
394 /* While we have data ... */
396 rc
= vscsi_fetch_desc(s
, req
, req
->cur_desc_num
, req
->cur_desc_offset
, &md
);
399 } else if (rc
== 0) {
403 /* Perform transfer */
404 llen
= MIN(len
, md
.len
);
405 if (req
->writing
) { /* writing = to device = reading from memory */
406 rc
= spapr_vio_dma_read(&s
->vdev
, md
.va
, buf
, llen
);
408 rc
= spapr_vio_dma_write(&s
->vdev
, md
.va
, buf
, llen
);
411 trace_spapr_vscsi_srp_indirect_data_rw(req
->writing
, rc
);
414 trace_spapr_vscsi_srp_indirect_data_buf(buf
[0], buf
[1], buf
[2], buf
[3]);
421 /* Update current position in the current descriptor */
422 req
->cur_desc_offset
+= llen
;
423 if (md
.len
== llen
) {
424 /* Go to the next descriptor if the current one finished */
426 req
->cur_desc_offset
= 0;
430 return rc
? -1 : total
;
433 static int vscsi_srp_transfer_data(VSCSIState
*s
, vscsi_req
*req
,
434 int writing
, uint8_t *buf
, uint32_t len
)
438 switch (req
->dma_fmt
) {
439 case SRP_NO_DATA_DESC
:
440 trace_spapr_vscsi_srp_transfer_data(len
);
442 case SRP_DATA_DESC_DIRECT
:
443 err
= vscsi_srp_direct_data(s
, req
, buf
, len
);
445 case SRP_DATA_DESC_INDIRECT
:
446 err
= vscsi_srp_indirect_data(s
, req
, buf
, len
);
452 /* Bits from linux srp */
453 static int data_out_desc_size(struct srp_cmd
*cmd
)
456 uint8_t fmt
= cmd
->buf_fmt
>> 4;
459 case SRP_NO_DATA_DESC
:
461 case SRP_DATA_DESC_DIRECT
:
462 size
= sizeof(struct srp_direct_buf
);
464 case SRP_DATA_DESC_INDIRECT
:
465 size
= sizeof(struct srp_indirect_buf
) +
466 sizeof(struct srp_direct_buf
)*cmd
->data_out_desc_cnt
;
474 static int vscsi_preprocess_desc(vscsi_req
*req
)
476 struct srp_cmd
*cmd
= &req
->iu
.srp
.cmd
;
478 req
->cdb_offset
= cmd
->add_cdb_len
& ~3;
481 req
->dma_fmt
= cmd
->buf_fmt
>> 4;
483 req
->cdb_offset
+= data_out_desc_size(cmd
);
484 req
->dma_fmt
= cmd
->buf_fmt
& ((1U << 4) - 1);
487 switch (req
->dma_fmt
) {
488 case SRP_NO_DATA_DESC
:
490 case SRP_DATA_DESC_DIRECT
:
491 req
->total_desc
= req
->local_desc
= 1;
493 case SRP_DATA_DESC_INDIRECT
: {
494 struct srp_indirect_buf
*ind_tmp
= (struct srp_indirect_buf
*)
495 (cmd
->add_data
+ req
->cdb_offset
);
497 req
->total_desc
= be32_to_cpu(ind_tmp
->table_desc
.len
) /
498 sizeof(struct srp_direct_buf
);
499 req
->local_desc
= req
->writing
? cmd
->data_out_desc_cnt
:
500 cmd
->data_in_desc_cnt
;
505 "vscsi_preprocess_desc: Unknown format %x\n", req
->dma_fmt
);
512 /* Callback to indicate that the SCSI layer has completed a transfer. */
513 static void vscsi_transfer_data(SCSIRequest
*sreq
, uint32_t len
)
515 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(sreq
->bus
->qbus
.parent
);
516 vscsi_req
*req
= sreq
->hba_private
;
520 trace_spapr_vscsi_transfer_data(sreq
->tag
, len
, req
);
522 fprintf(stderr
, "VSCSI: Can't find request for tag 0x%x\n", sreq
->tag
);
527 buf
= scsi_req_get_buf(sreq
);
528 rc
= vscsi_srp_transfer_data(s
, req
, req
->writing
, buf
, len
);
531 fprintf(stderr
, "VSCSI: RDMA error rc=%d!\n", rc
);
532 req
->dma_error
= true;
533 scsi_req_cancel(req
->sreq
);
537 /* Start next chunk */
539 scsi_req_continue(sreq
);
542 /* Callback to indicate that the SCSI layer has completed a transfer. */
543 static void vscsi_command_complete(SCSIRequest
*sreq
, uint32_t status
, size_t resid
)
545 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(sreq
->bus
->qbus
.parent
);
546 vscsi_req
*req
= sreq
->hba_private
;
547 int32_t res_in
= 0, res_out
= 0;
549 trace_spapr_vscsi_command_complete(sreq
->tag
, status
, req
);
551 fprintf(stderr
, "VSCSI: Can't find request for tag 0x%x\n", sreq
->tag
);
555 if (status
== CHECK_CONDITION
) {
556 req
->senselen
= scsi_req_get_sense(req
->sreq
, req
->sense
,
558 trace_spapr_vscsi_command_complete_sense_data1(req
->senselen
,
559 req
->sense
[0], req
->sense
[1], req
->sense
[2], req
->sense
[3],
560 req
->sense
[4], req
->sense
[5], req
->sense
[6], req
->sense
[7]);
561 trace_spapr_vscsi_command_complete_sense_data2(
562 req
->sense
[8], req
->sense
[9], req
->sense
[10], req
->sense
[11],
563 req
->sense
[12], req
->sense
[13], req
->sense
[14], req
->sense
[15]);
566 trace_spapr_vscsi_command_complete_status(status
);
568 /* We handle overflows, not underflows for normal commands,
569 * but hopefully nobody cares
572 res_out
= req
->data_len
;
574 res_in
= req
->data_len
;
577 vscsi_send_rsp(s
, req
, status
, res_in
, res_out
);
581 static void vscsi_request_cancelled(SCSIRequest
*sreq
)
583 vscsi_req
*req
= sreq
->hba_private
;
585 if (req
->dma_error
) {
586 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(sreq
->bus
->qbus
.parent
);
588 vscsi_makeup_sense(s
, req
, HARDWARE_ERROR
, 0, 0);
589 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
594 static const VMStateDescription vmstate_spapr_vscsi_req
= {
595 .name
= "spapr_vscsi_req",
597 .minimum_version_id
= 1,
598 .fields
= (VMStateField
[]) {
599 VMSTATE_BUFFER(crq
.raw
, vscsi_req
),
600 VMSTATE_BUFFER(iu
.srp
.reserved
, vscsi_req
),
601 VMSTATE_UINT32(qtag
, vscsi_req
),
602 VMSTATE_BOOL(active
, vscsi_req
),
603 VMSTATE_UINT32(data_len
, vscsi_req
),
604 VMSTATE_BOOL(writing
, vscsi_req
),
605 VMSTATE_UINT32(senselen
, vscsi_req
),
606 VMSTATE_BUFFER(sense
, vscsi_req
),
607 VMSTATE_UINT8(dma_fmt
, vscsi_req
),
608 VMSTATE_UINT16(local_desc
, vscsi_req
),
609 VMSTATE_UINT16(total_desc
, vscsi_req
),
610 VMSTATE_UINT16(cdb_offset
, vscsi_req
),
611 /*Restart SCSI request from the beginning for now */
612 /*VMSTATE_UINT16(cur_desc_num, vscsi_req),
613 VMSTATE_UINT16(cur_desc_offset, vscsi_req),*/
614 VMSTATE_END_OF_LIST()
618 static void vscsi_save_request(QEMUFile
*f
, SCSIRequest
*sreq
)
620 vscsi_req
*req
= sreq
->hba_private
;
623 vmstate_save_state(f
, &vmstate_spapr_vscsi_req
, req
, NULL
);
625 trace_spapr_vscsi_save_request(req
->qtag
, req
->cur_desc_num
,
626 req
->cur_desc_offset
);
629 static void *vscsi_load_request(QEMUFile
*f
, SCSIRequest
*sreq
)
631 SCSIBus
*bus
= sreq
->bus
;
632 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(bus
->qbus
.parent
);
636 assert(sreq
->tag
< VSCSI_REQ_LIMIT
);
637 req
= &s
->reqs
[sreq
->tag
];
638 assert(!req
->active
);
640 memset(req
, 0, sizeof(*req
));
641 rc
= vmstate_load_state(f
, &vmstate_spapr_vscsi_req
, req
, 1);
643 fprintf(stderr
, "VSCSI: failed loading request tag#%u\n", sreq
->tag
);
648 req
->sreq
= scsi_req_ref(sreq
);
650 trace_spapr_vscsi_load_request(req
->qtag
, req
->cur_desc_num
,
651 req
->cur_desc_offset
);
656 static void vscsi_process_login(VSCSIState
*s
, vscsi_req
*req
)
658 union viosrp_iu
*iu
= &req
->iu
;
659 struct srp_login_rsp
*rsp
= &iu
->srp
.login_rsp
;
660 uint64_t tag
= iu
->srp
.rsp
.tag
;
662 trace_spapr_vscsi_process_login();
664 /* TODO handle case that requested size is wrong and
665 * buffer format is wrong
667 memset(iu
, 0, sizeof(struct srp_login_rsp
));
668 rsp
->opcode
= SRP_LOGIN_RSP
;
669 /* Don't advertise quite as many request as we support to
670 * keep room for management stuff etc...
672 rsp
->req_lim_delta
= cpu_to_be32(VSCSI_REQ_LIMIT
-2);
674 rsp
->max_it_iu_len
= cpu_to_be32(sizeof(union srp_iu
));
675 rsp
->max_ti_iu_len
= cpu_to_be32(sizeof(union srp_iu
));
676 /* direct and indirect */
677 rsp
->buf_fmt
= cpu_to_be16(SRP_BUF_FORMAT_DIRECT
| SRP_BUF_FORMAT_INDIRECT
);
679 vscsi_send_iu(s
, req
, sizeof(*rsp
), VIOSRP_SRP_FORMAT
);
682 static void vscsi_inquiry_no_target(VSCSIState
*s
, vscsi_req
*req
)
684 uint8_t *cdb
= req
->iu
.srp
.cmd
.cdb
;
685 uint8_t resp_data
[36];
688 /* We don't do EVPD. Also check that page_code is 0 */
689 if ((cdb
[1] & 0x01) || cdb
[2] != 0) {
690 /* Send INVALID FIELD IN CDB */
691 vscsi_makeup_sense(s
, req
, ILLEGAL_REQUEST
, 0x24, 0);
692 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
696 alen
= (alen
<< 8) | cdb
[4];
699 /* Fake up inquiry using PQ=3 */
700 memset(resp_data
, 0, 36);
701 resp_data
[0] = 0x7f; /* Not capable of supporting a device here */
702 resp_data
[2] = 0x06; /* SPS-4 */
703 resp_data
[3] = 0x02; /* Resp data format */
704 resp_data
[4] = 36 - 5; /* Additional length */
705 resp_data
[7] = 0x10; /* Sync transfers */
706 memcpy(&resp_data
[16], "QEMU EMPTY ", 16);
707 memcpy(&resp_data
[8], "QEMU ", 8);
710 vscsi_preprocess_desc(req
);
711 rc
= vscsi_srp_transfer_data(s
, req
, 0, resp_data
, len
);
713 vscsi_makeup_sense(s
, req
, HARDWARE_ERROR
, 0, 0);
714 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
716 vscsi_send_rsp(s
, req
, 0, 36 - rc
, 0);
720 static void vscsi_report_luns(VSCSIState
*s
, vscsi_req
*req
)
729 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
730 SCSIDevice
*dev
= SCSI_DEVICE(kid
->child
);
733 if (dev
->channel
== 0 && dev
->id
== 0 && dev
->lun
== 0) {
742 resp_data
= g_malloc0(len
);
743 stl_be_p(resp_data
, n
);
744 i
= found_lun0
? 8 : 16;
745 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
746 DeviceState
*qdev
= kid
->child
;
747 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
749 if (dev
->id
== 0 && dev
->channel
== 0) {
750 resp_data
[i
] = 0; /* Use simple LUN for 0 (SAM5 4.7.7.1) */
752 resp_data
[i
] = (2 << 6); /* Otherwise LUN addressing (4.7.7.4) */
754 resp_data
[i
] |= dev
->id
;
755 resp_data
[i
+1] = (dev
->channel
<< 5);
756 resp_data
[i
+1] |= dev
->lun
;
760 vscsi_preprocess_desc(req
);
761 rc
= vscsi_srp_transfer_data(s
, req
, 0, resp_data
, len
);
764 vscsi_makeup_sense(s
, req
, HARDWARE_ERROR
, 0, 0);
765 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
767 vscsi_send_rsp(s
, req
, 0, len
- rc
, 0);
771 static int vscsi_queue_cmd(VSCSIState
*s
, vscsi_req
*req
)
773 union srp_iu
*srp
= &req
->iu
.srp
;
777 if ((srp
->cmd
.lun
== 0 || be64_to_cpu(srp
->cmd
.lun
) == SRP_REPORT_LUNS_WLUN
)
778 && srp
->cmd
.cdb
[0] == REPORT_LUNS
) {
779 vscsi_report_luns(s
, req
);
783 sdev
= vscsi_device_find(&s
->bus
, be64_to_cpu(srp
->cmd
.lun
), &lun
);
785 trace_spapr_vscsi_queue_cmd_no_drive(be64_to_cpu(srp
->cmd
.lun
));
786 if (srp
->cmd
.cdb
[0] == INQUIRY
) {
787 vscsi_inquiry_no_target(s
, req
);
789 vscsi_makeup_sense(s
, req
, ILLEGAL_REQUEST
, 0x24, 0x00);
790 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
794 req
->sreq
= scsi_req_new(sdev
, req
->qtag
, lun
, srp
->cmd
.cdb
, req
);
795 n
= scsi_req_enqueue(req
->sreq
);
797 trace_spapr_vscsi_queue_cmd(req
->qtag
, srp
->cmd
.cdb
[0],
798 scsi_command_name(srp
->cmd
.cdb
[0]), lun
, n
);
801 /* Transfer direction must be set before preprocessing the
804 req
->writing
= (n
< 1);
806 /* Preprocess RDMA descriptors */
807 vscsi_preprocess_desc(req
);
809 /* Get transfer direction and initiate transfer */
815 scsi_req_continue(req
->sreq
);
817 /* Don't touch req here, it may have been recycled already */
822 static int vscsi_process_tsk_mgmt(VSCSIState
*s
, vscsi_req
*req
)
824 union viosrp_iu
*iu
= &req
->iu
;
826 int i
, lun
= 0, resp
= SRP_TSK_MGMT_COMPLETE
;
828 uint64_t tag
= iu
->srp
.rsp
.tag
;
829 uint8_t sol_not
= iu
->srp
.cmd
.sol_not
;
831 fprintf(stderr
, "vscsi_process_tsk_mgmt %02x\n",
832 iu
->srp
.tsk_mgmt
.tsk_mgmt_func
);
834 d
= vscsi_device_find(&s
->bus
, be64_to_cpu(req
->iu
.srp
.tsk_mgmt
.lun
), &lun
);
836 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
838 switch (iu
->srp
.tsk_mgmt
.tsk_mgmt_func
) {
839 case SRP_TSK_ABORT_TASK
:
841 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
845 tmpreq
= vscsi_find_req(s
, req
->iu
.srp
.tsk_mgmt
.task_tag
);
846 if (tmpreq
&& tmpreq
->sreq
) {
847 assert(tmpreq
->sreq
->hba_private
);
848 scsi_req_cancel(tmpreq
->sreq
);
852 case SRP_TSK_LUN_RESET
:
854 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
858 qdev_reset_all(&d
->qdev
);
861 case SRP_TSK_ABORT_TASK_SET
:
862 case SRP_TSK_CLEAR_TASK_SET
:
864 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
868 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
869 tmpreq
= &s
->reqs
[i
];
870 if (tmpreq
->iu
.srp
.cmd
.lun
!= req
->iu
.srp
.tsk_mgmt
.lun
) {
873 if (!tmpreq
->active
|| !tmpreq
->sreq
) {
876 assert(tmpreq
->sreq
->hba_private
);
877 scsi_req_cancel(tmpreq
->sreq
);
881 case SRP_TSK_CLEAR_ACA
:
882 resp
= SRP_TSK_MGMT_NOT_SUPPORTED
;
886 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
891 /* Compose the response here as */
892 memset(iu
, 0, sizeof(struct srp_rsp
) + 4);
893 iu
->srp
.rsp
.opcode
= SRP_RSP
;
894 iu
->srp
.rsp
.req_lim_delta
= cpu_to_be32(1);
895 iu
->srp
.rsp
.tag
= tag
;
896 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_RSPVALID
;
897 iu
->srp
.rsp
.resp_data_len
= cpu_to_be32(4);
899 iu
->srp
.rsp
.sol_not
= (sol_not
& 0x04) >> 2;
901 iu
->srp
.rsp
.sol_not
= (sol_not
& 0x02) >> 1;
904 iu
->srp
.rsp
.status
= GOOD
;
905 iu
->srp
.rsp
.data
[3] = resp
;
907 vscsi_send_iu(s
, req
, sizeof(iu
->srp
.rsp
) + 4, VIOSRP_SRP_FORMAT
);
912 static int vscsi_handle_srp_req(VSCSIState
*s
, vscsi_req
*req
)
914 union srp_iu
*srp
= &req
->iu
.srp
;
916 uint8_t opcode
= srp
->rsp
.opcode
;
920 vscsi_process_login(s
, req
);
923 done
= vscsi_process_tsk_mgmt(s
, req
);
926 done
= vscsi_queue_cmd(s
, req
);
936 fprintf(stderr
, "VSCSI: Unsupported opcode %02x\n", opcode
);
939 fprintf(stderr
, "VSCSI: Unknown type %02x\n", opcode
);
945 static int vscsi_send_adapter_info(VSCSIState
*s
, vscsi_req
*req
)
947 struct viosrp_adapter_info
*sinfo
;
948 struct mad_adapter_info_data info
;
951 sinfo
= &req
->iu
.mad
.adapter_info
;
953 #if 0 /* What for ? */
954 rc
= spapr_vio_dma_read(&s
->vdev
, be64_to_cpu(sinfo
->buffer
),
955 &info
, be16_to_cpu(sinfo
->common
.length
));
957 fprintf(stderr
, "vscsi_send_adapter_info: DMA read failure !\n");
960 memset(&info
, 0, sizeof(info
));
961 strcpy(info
.srp_version
, SRP_VERSION
);
962 memcpy(info
.partition_name
, "qemu", sizeof("qemu"));
963 info
.partition_number
= cpu_to_be32(0);
964 info
.mad_version
= cpu_to_be32(1);
965 info
.os_type
= cpu_to_be32(2);
966 info
.port_max_txu
[0] = cpu_to_be32(VSCSI_MAX_SECTORS
<< 9);
968 rc
= spapr_vio_dma_write(&s
->vdev
, be64_to_cpu(sinfo
->buffer
),
969 &info
, be16_to_cpu(sinfo
->common
.length
));
971 fprintf(stderr
, "vscsi_send_adapter_info: DMA write failure !\n");
974 sinfo
->common
.status
= rc
? cpu_to_be32(1) : 0;
976 return vscsi_send_iu(s
, req
, sizeof(*sinfo
), VIOSRP_MAD_FORMAT
);
979 static int vscsi_send_capabilities(VSCSIState
*s
, vscsi_req
*req
)
981 struct viosrp_capabilities
*vcap
;
982 struct capabilities cap
= { };
983 uint16_t len
, req_len
;
987 vcap
= &req
->iu
.mad
.capabilities
;
988 req_len
= len
= be16_to_cpu(vcap
->common
.length
);
989 buffer
= be64_to_cpu(vcap
->buffer
);
990 if (len
> sizeof(cap
)) {
991 fprintf(stderr
, "vscsi_send_capabilities: capabilities size mismatch !\n");
994 * Just read and populate the structure that is known.
995 * Zero rest of the structure.
999 rc
= spapr_vio_dma_read(&s
->vdev
, buffer
, &cap
, len
);
1001 fprintf(stderr
, "vscsi_send_capabilities: DMA read failure !\n");
1005 * Current implementation does not suppport any migration or
1006 * reservation capabilities. Construct the response telling the
1007 * guest not to use them.
1010 cap
.migration
.ecl
= 0;
1011 cap
.reserve
.type
= 0;
1012 cap
.migration
.common
.server_support
= 0;
1013 cap
.reserve
.common
.server_support
= 0;
1015 rc
= spapr_vio_dma_write(&s
->vdev
, buffer
, &cap
, len
);
1017 fprintf(stderr
, "vscsi_send_capabilities: DMA write failure !\n");
1019 if (req_len
> len
) {
1021 * Being paranoid and lets not worry about the error code
1022 * here. Actual write of the cap is done above.
1024 spapr_vio_dma_set(&s
->vdev
, (buffer
+ len
), 0, (req_len
- len
));
1026 vcap
->common
.status
= rc
? cpu_to_be32(1) : 0;
1027 return vscsi_send_iu(s
, req
, sizeof(*vcap
), VIOSRP_MAD_FORMAT
);
1030 static int vscsi_handle_mad_req(VSCSIState
*s
, vscsi_req
*req
)
1032 union mad_iu
*mad
= &req
->iu
.mad
;
1033 bool request_handled
= false;
1034 uint64_t retlen
= 0;
1036 switch (be32_to_cpu(mad
->empty_iu
.common
.type
)) {
1037 case VIOSRP_EMPTY_IU_TYPE
:
1038 fprintf(stderr
, "Unsupported EMPTY MAD IU\n");
1039 retlen
= sizeof(mad
->empty_iu
);
1041 case VIOSRP_ERROR_LOG_TYPE
:
1042 fprintf(stderr
, "Unsupported ERROR LOG MAD IU\n");
1043 retlen
= sizeof(mad
->error_log
);
1045 case VIOSRP_ADAPTER_INFO_TYPE
:
1046 vscsi_send_adapter_info(s
, req
);
1047 request_handled
= true;
1049 case VIOSRP_HOST_CONFIG_TYPE
:
1050 retlen
= sizeof(mad
->host_config
);
1052 case VIOSRP_CAPABILITIES_TYPE
:
1053 vscsi_send_capabilities(s
, req
);
1054 request_handled
= true;
1057 fprintf(stderr
, "VSCSI: Unknown MAD type %02x\n",
1058 be32_to_cpu(mad
->empty_iu
.common
.type
));
1060 * PAPR+ says that "The length field is set to the length
1061 * of the data structure(s) used in the command".
1062 * As we did not recognize the request type, put zero there.
1067 if (!request_handled
) {
1068 mad
->empty_iu
.common
.status
= cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED
);
1069 vscsi_send_iu(s
, req
, retlen
, VIOSRP_MAD_FORMAT
);
1075 static void vscsi_got_payload(VSCSIState
*s
, vscsi_crq
*crq
)
1080 req
= vscsi_get_req(s
);
1082 fprintf(stderr
, "VSCSI: Failed to get a request !\n");
1086 /* We only support a limited number of descriptors, we know
1087 * the ibmvscsi driver uses up to 10 max, so it should fit
1088 * in our 256 bytes IUs. If not we'll have to increase the size
1091 if (crq
->s
.IU_length
> sizeof(union viosrp_iu
)) {
1092 fprintf(stderr
, "VSCSI: SRP IU too long (%d bytes) !\n",
1098 /* XXX Handle failure differently ? */
1099 if (spapr_vio_dma_read(&s
->vdev
, crq
->s
.IU_data_ptr
, &req
->iu
,
1100 crq
->s
.IU_length
)) {
1101 fprintf(stderr
, "vscsi_got_payload: DMA read failure !\n");
1105 memcpy(&req
->crq
, crq
, sizeof(vscsi_crq
));
1107 if (crq
->s
.format
== VIOSRP_MAD_FORMAT
) {
1108 done
= vscsi_handle_mad_req(s
, req
);
1110 done
= vscsi_handle_srp_req(s
, req
);
1119 static int vscsi_do_crq(struct SpaprVioDevice
*dev
, uint8_t *crq_data
)
1121 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(dev
);
1124 memcpy(crq
.raw
, crq_data
, 16);
1125 crq
.s
.timeout
= be16_to_cpu(crq
.s
.timeout
);
1126 crq
.s
.IU_length
= be16_to_cpu(crq
.s
.IU_length
);
1127 crq
.s
.IU_data_ptr
= be64_to_cpu(crq
.s
.IU_data_ptr
);
1129 trace_spapr_vscsi_do_crq(crq
.raw
[0], crq
.raw
[1]);
1131 switch (crq
.s
.valid
) {
1132 case 0xc0: /* Init command/response */
1134 /* Respond to initialization request */
1135 if (crq
.s
.format
== 0x01) {
1136 memset(crq
.raw
, 0, 16);
1138 crq
.s
.format
= 0x02;
1139 spapr_vio_send_crq(dev
, crq
.raw
);
1142 /* Note that in hotplug cases, we might get a 0x02
1143 * as a result of us emitting the init request
1147 case 0xff: /* Link event */
1149 /* Not handled for now */
1152 case 0x80: /* Payloads */
1153 switch (crq
.s
.format
) {
1154 case VIOSRP_SRP_FORMAT
: /* AKA VSCSI request */
1155 case VIOSRP_MAD_FORMAT
: /* AKA VSCSI response */
1156 vscsi_got_payload(s
, &crq
);
1158 case VIOSRP_OS400_FORMAT
:
1159 case VIOSRP_AIX_FORMAT
:
1160 case VIOSRP_LINUX_FORMAT
:
1161 case VIOSRP_INLINE_FORMAT
:
1162 fprintf(stderr
, "vscsi_do_srq: Unsupported payload format %02x\n",
1166 fprintf(stderr
, "vscsi_do_srq: Unknown payload format %02x\n",
1171 fprintf(stderr
, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
1172 crq
.raw
[0], crq
.raw
[1]);
1178 static const struct SCSIBusInfo vscsi_scsi_info
= {
1180 .max_channel
= 7, /* logical unit addressing format */
1184 .transfer_data
= vscsi_transfer_data
,
1185 .complete
= vscsi_command_complete
,
1186 .cancel
= vscsi_request_cancelled
,
1187 .save_request
= vscsi_save_request
,
1188 .load_request
= vscsi_load_request
,
1191 static void spapr_vscsi_reset(SpaprVioDevice
*dev
)
1193 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(dev
);
1196 memset(s
->reqs
, 0, sizeof(s
->reqs
));
1197 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
1198 s
->reqs
[i
].qtag
= i
;
1202 static void spapr_vscsi_realize(SpaprVioDevice
*dev
, Error
**errp
)
1204 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(dev
);
1206 dev
->crq
.SendFunc
= vscsi_do_crq
;
1208 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), DEVICE(dev
),
1209 &vscsi_scsi_info
, NULL
);
1212 void spapr_vscsi_create(SpaprVioBus
*bus
)
1216 dev
= qdev_create(&bus
->bus
, "spapr-vscsi");
1218 qdev_init_nofail(dev
);
1219 scsi_bus_legacy_handle_cmdline(&VIO_SPAPR_VSCSI_DEVICE(dev
)->bus
);
1222 static int spapr_vscsi_devnode(SpaprVioDevice
*dev
, void *fdt
, int node_off
)
1226 ret
= fdt_setprop_cell(fdt
, node_off
, "#address-cells", 2);
1231 ret
= fdt_setprop_cell(fdt
, node_off
, "#size-cells", 0);
1239 static Property spapr_vscsi_properties
[] = {
1240 DEFINE_SPAPR_PROPERTIES(VSCSIState
, vdev
),
1241 DEFINE_PROP_END_OF_LIST(),
1244 static const VMStateDescription vmstate_spapr_vscsi
= {
1245 .name
= "spapr_vscsi",
1247 .minimum_version_id
= 1,
1248 .fields
= (VMStateField
[]) {
1249 VMSTATE_SPAPR_VIO(vdev
, VSCSIState
),
1253 VMSTATE_END_OF_LIST()
1257 static void spapr_vscsi_class_init(ObjectClass
*klass
, void *data
)
1259 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1260 SpaprVioDeviceClass
*k
= VIO_SPAPR_DEVICE_CLASS(klass
);
1262 k
->realize
= spapr_vscsi_realize
;
1263 k
->reset
= spapr_vscsi_reset
;
1264 k
->devnode
= spapr_vscsi_devnode
;
1265 k
->dt_name
= "v-scsi";
1266 k
->dt_type
= "vscsi";
1267 k
->dt_compatible
= "IBM,v-scsi";
1268 k
->signal_mask
= 0x00000001;
1269 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1270 dc
->props
= spapr_vscsi_properties
;
1271 k
->rtce_window_size
= 0x10000000;
1272 dc
->vmsd
= &vmstate_spapr_vscsi
;
1275 static const TypeInfo spapr_vscsi_info
= {
1276 .name
= TYPE_VIO_SPAPR_VSCSI_DEVICE
,
1277 .parent
= TYPE_VIO_SPAPR_DEVICE
,
1278 .instance_size
= sizeof(VSCSIState
),
1279 .class_init
= spapr_vscsi_class_init
,
1282 static void spapr_vscsi_register_types(void)
1284 type_register_static(&spapr_vscsi_info
);
1287 type_init(spapr_vscsi_register_types
)