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 "hw/scsi/scsi.h"
36 #include "block/scsi.h"
39 #include "hw/ppc/spapr.h"
40 #include "hw/ppc/spapr_vio.h"
45 /*#define DEBUG_VSCSI*/
48 #define DPRINTF(fmt, ...) \
49 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
51 #define DPRINTF(fmt, ...) \
60 #define VSCSI_MAX_SECTORS 4096
61 #define VSCSI_REQ_LIMIT 24
63 #define SRP_RSP_SENSE_DATA_LEN 18
65 #define SRP_REPORT_LUNS_WLUN 0xc10100000000000ULL
67 typedef union vscsi_crq
{
72 typedef struct vscsi_req
{
76 /* SCSI request tracking */
78 uint32_t qtag
; /* qemu tag != srp tag */
83 uint8_t sense
[SCSI_SENSE_BUF_SIZE
];
85 /* RDMA related bits */
90 uint16_t cur_desc_num
;
91 uint16_t cur_desc_offset
;
94 #define TYPE_VIO_SPAPR_VSCSI_DEVICE "spapr-vscsi"
95 #define VIO_SPAPR_VSCSI_DEVICE(obj) \
96 OBJECT_CHECK(VSCSIState, (obj), TYPE_VIO_SPAPR_VSCSI_DEVICE)
101 vscsi_req reqs
[VSCSI_REQ_LIMIT
];
104 static struct vscsi_req
*vscsi_get_req(VSCSIState
*s
)
109 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
112 memset(req
, 0, sizeof(*req
));
121 static struct vscsi_req
*vscsi_find_req(VSCSIState
*s
, uint64_t srp_tag
)
126 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
128 if (req
->iu
.srp
.cmd
.tag
== srp_tag
) {
135 static void vscsi_put_req(vscsi_req
*req
)
137 if (req
->sreq
!= NULL
) {
138 scsi_req_unref(req
->sreq
);
144 static SCSIDevice
*vscsi_device_find(SCSIBus
*bus
, uint64_t srp_lun
, int *lun
)
146 int channel
= 0, id
= 0;
149 switch (srp_lun
>> 62) {
151 if ((srp_lun
>> 56) != 0) {
152 channel
= (srp_lun
>> 56) & 0x3f;
153 id
= (srp_lun
>> 48) & 0xff;
157 *lun
= (srp_lun
>> 48) & 0xff;
161 *lun
= (srp_lun
>> 48) & 0x3fff;
164 channel
= (srp_lun
>> 53) & 0x7;
165 id
= (srp_lun
>> 56) & 0x3f;
166 *lun
= (srp_lun
>> 48) & 0x1f;
175 return scsi_device_find(bus
, channel
, id
, *lun
);
178 static int vscsi_send_iu(VSCSIState
*s
, vscsi_req
*req
,
179 uint64_t length
, uint8_t format
)
183 /* First copy the SRP */
184 rc
= spapr_vio_dma_write(&s
->vdev
, req
->crq
.s
.IU_data_ptr
,
187 fprintf(stderr
, "vscsi_send_iu: DMA write failure !\n");
190 req
->crq
.s
.valid
= 0x80;
191 req
->crq
.s
.format
= format
;
192 req
->crq
.s
.reserved
= 0x00;
193 req
->crq
.s
.timeout
= cpu_to_be16(0x0000);
194 req
->crq
.s
.IU_length
= cpu_to_be16(length
);
195 req
->crq
.s
.IU_data_ptr
= req
->iu
.srp
.rsp
.tag
; /* right byte order */
198 req
->crq
.s
.status
= 0x99; /* Just needs to be non-zero */
200 req
->crq
.s
.status
= 0x00;
203 rc1
= spapr_vio_send_crq(&s
->vdev
, req
->crq
.raw
);
205 fprintf(stderr
, "vscsi_send_iu: Error sending response\n");
212 static void vscsi_makeup_sense(VSCSIState
*s
, vscsi_req
*req
,
213 uint8_t key
, uint8_t asc
, uint8_t ascq
)
215 req
->senselen
= SRP_RSP_SENSE_DATA_LEN
;
217 /* Valid bit and 'current errors' */
218 req
->sense
[0] = (0x1 << 7 | 0x70);
221 /* Additional sense length */
222 req
->sense
[7] = 0xa; /* 10 bytes */
223 /* Additional sense code */
224 req
->sense
[12] = asc
;
225 req
->sense
[13] = ascq
;
228 static int vscsi_send_rsp(VSCSIState
*s
, vscsi_req
*req
,
229 uint8_t status
, int32_t res_in
, int32_t res_out
)
231 union viosrp_iu
*iu
= &req
->iu
;
232 uint64_t tag
= iu
->srp
.rsp
.tag
;
233 int total_len
= sizeof(iu
->srp
.rsp
);
234 uint8_t sol_not
= iu
->srp
.cmd
.sol_not
;
236 DPRINTF("VSCSI: Sending resp status: 0x%x, "
237 "res_in: %d, res_out: %d\n", status
, res_in
, res_out
);
239 memset(iu
, 0, sizeof(struct srp_rsp
));
240 iu
->srp
.rsp
.opcode
= SRP_RSP
;
241 iu
->srp
.rsp
.req_lim_delta
= cpu_to_be32(1);
242 iu
->srp
.rsp
.tag
= tag
;
244 /* Handle residuals */
246 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DIUNDER
;
249 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DIOVER
;
252 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DOUNDER
;
254 } else if (res_out
) {
255 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DOOVER
;
257 iu
->srp
.rsp
.data_in_res_cnt
= cpu_to_be32(res_in
);
258 iu
->srp
.rsp
.data_out_res_cnt
= cpu_to_be32(res_out
);
260 /* We don't do response data */
261 /* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */
262 iu
->srp
.rsp
.resp_data_len
= cpu_to_be32(0);
264 /* Handle success vs. failure */
265 iu
->srp
.rsp
.status
= status
;
267 iu
->srp
.rsp
.sol_not
= (sol_not
& 0x04) >> 2;
269 req
->iu
.srp
.rsp
.flags
|= SRP_RSP_FLAG_SNSVALID
;
270 req
->iu
.srp
.rsp
.sense_data_len
= cpu_to_be32(req
->senselen
);
271 memcpy(req
->iu
.srp
.rsp
.data
, req
->sense
, req
->senselen
);
272 total_len
+= req
->senselen
;
275 iu
->srp
.rsp
.sol_not
= (sol_not
& 0x02) >> 1;
278 vscsi_send_iu(s
, req
, total_len
, VIOSRP_SRP_FORMAT
);
282 static inline struct srp_direct_buf
vscsi_swap_desc(struct srp_direct_buf desc
)
284 desc
.va
= be64_to_cpu(desc
.va
);
285 desc
.len
= be32_to_cpu(desc
.len
);
289 static int vscsi_fetch_desc(VSCSIState
*s
, struct vscsi_req
*req
,
290 unsigned n
, unsigned buf_offset
,
291 struct srp_direct_buf
*ret
)
293 struct srp_cmd
*cmd
= &req
->iu
.srp
.cmd
;
295 switch (req
->dma_fmt
) {
296 case SRP_NO_DATA_DESC
: {
297 DPRINTF("VSCSI: no data descriptor\n");
300 case SRP_DATA_DESC_DIRECT
: {
301 memcpy(ret
, cmd
->add_data
+ req
->cdb_offset
, sizeof(*ret
));
302 assert(req
->cur_desc_num
== 0);
303 DPRINTF("VSCSI: direct segment\n");
306 case SRP_DATA_DESC_INDIRECT
: {
307 struct srp_indirect_buf
*tmp
= (struct srp_indirect_buf
*)
308 (cmd
->add_data
+ req
->cdb_offset
);
309 if (n
< req
->local_desc
) {
310 *ret
= tmp
->desc_list
[n
];
311 DPRINTF("VSCSI: indirect segment local tag=0x%x desc#%d/%d\n",
312 req
->qtag
, n
, req
->local_desc
);
314 } else if (n
< req
->total_desc
) {
316 struct srp_direct_buf tbl_desc
= vscsi_swap_desc(tmp
->table_desc
);
317 unsigned desc_offset
= n
* sizeof(struct srp_direct_buf
);
319 if (desc_offset
>= tbl_desc
.len
) {
320 DPRINTF("VSCSI: #%d is ouf of range (%d bytes)\n",
324 rc
= spapr_vio_dma_read(&s
->vdev
, tbl_desc
.va
+ desc_offset
,
325 ret
, sizeof(struct srp_direct_buf
));
327 DPRINTF("VSCSI: spapr_vio_dma_read -> %d reading ext_desc\n",
331 DPRINTF("VSCSI: indirect segment ext. tag=0x%x desc#%d/%d { va=%"PRIx64
" len=%x }\n",
332 req
->qtag
, n
, req
->total_desc
, tbl_desc
.va
, tbl_desc
.len
);
334 DPRINTF("VSCSI: Out of descriptors !\n");
340 fprintf(stderr
, "VSCSI: Unknown format %x\n", req
->dma_fmt
);
344 *ret
= vscsi_swap_desc(*ret
);
345 if (buf_offset
> ret
->len
) {
346 DPRINTF(" offset=%x is out of a descriptor #%d boundary=%x\n",
347 buf_offset
, req
->cur_desc_num
, ret
->len
);
350 ret
->va
+= buf_offset
;
351 ret
->len
-= buf_offset
;
353 DPRINTF(" cur=%d offs=%x ret { va=%"PRIx64
" len=%x }\n",
354 req
->cur_desc_num
, req
->cur_desc_offset
, ret
->va
, ret
->len
);
356 return ret
->len
? 1 : 0;
359 static int vscsi_srp_direct_data(VSCSIState
*s
, vscsi_req
*req
,
360 uint8_t *buf
, uint32_t len
)
362 struct srp_direct_buf md
;
366 rc
= vscsi_fetch_desc(s
, req
, req
->cur_desc_num
, req
->cur_desc_offset
, &md
);
369 } else if (rc
== 0) {
373 llen
= MIN(len
, md
.len
);
375 if (req
->writing
) { /* writing = to device = reading from memory */
376 rc
= spapr_vio_dma_read(&s
->vdev
, md
.va
, buf
, llen
);
378 rc
= spapr_vio_dma_write(&s
->vdev
, md
.va
, buf
, llen
);
385 req
->cur_desc_offset
+= llen
;
390 static int vscsi_srp_indirect_data(VSCSIState
*s
, vscsi_req
*req
,
391 uint8_t *buf
, uint32_t len
)
393 struct srp_direct_buf md
;
395 uint32_t llen
, total
= 0;
397 DPRINTF("VSCSI: indirect segment 0x%x bytes\n", len
);
399 /* While we have data ... */
401 rc
= vscsi_fetch_desc(s
, req
, req
->cur_desc_num
, req
->cur_desc_offset
, &md
);
404 } else if (rc
== 0) {
408 /* Perform transfer */
409 llen
= MIN(len
, md
.len
);
410 if (req
->writing
) { /* writing = to device = reading from memory */
411 rc
= spapr_vio_dma_read(&s
->vdev
, md
.va
, buf
, llen
);
413 rc
= spapr_vio_dma_write(&s
->vdev
, md
.va
, buf
, llen
);
416 DPRINTF("VSCSI: spapr_vio_dma_r/w(%d) -> %d\n", req
->writing
, rc
);
419 DPRINTF("VSCSI: data: %02x %02x %02x %02x...\n",
420 buf
[0], buf
[1], buf
[2], buf
[3]);
427 /* Update current position in the current descriptor */
428 req
->cur_desc_offset
+= llen
;
429 if (md
.len
== llen
) {
430 /* Go to the next descriptor if the current one finished */
432 req
->cur_desc_offset
= 0;
436 return rc
? -1 : total
;
439 static int vscsi_srp_transfer_data(VSCSIState
*s
, vscsi_req
*req
,
440 int writing
, uint8_t *buf
, uint32_t len
)
444 switch (req
->dma_fmt
) {
445 case SRP_NO_DATA_DESC
:
446 DPRINTF("VSCSI: no data desc transfer, skipping 0x%x bytes\n", len
);
448 case SRP_DATA_DESC_DIRECT
:
449 err
= vscsi_srp_direct_data(s
, req
, buf
, len
);
451 case SRP_DATA_DESC_INDIRECT
:
452 err
= vscsi_srp_indirect_data(s
, req
, buf
, len
);
458 /* Bits from linux srp */
459 static int data_out_desc_size(struct srp_cmd
*cmd
)
462 uint8_t fmt
= cmd
->buf_fmt
>> 4;
465 case SRP_NO_DATA_DESC
:
467 case SRP_DATA_DESC_DIRECT
:
468 size
= sizeof(struct srp_direct_buf
);
470 case SRP_DATA_DESC_INDIRECT
:
471 size
= sizeof(struct srp_indirect_buf
) +
472 sizeof(struct srp_direct_buf
)*cmd
->data_out_desc_cnt
;
480 static int vscsi_preprocess_desc(vscsi_req
*req
)
482 struct srp_cmd
*cmd
= &req
->iu
.srp
.cmd
;
484 req
->cdb_offset
= cmd
->add_cdb_len
& ~3;
487 req
->dma_fmt
= cmd
->buf_fmt
>> 4;
489 req
->cdb_offset
+= data_out_desc_size(cmd
);
490 req
->dma_fmt
= cmd
->buf_fmt
& ((1U << 4) - 1);
493 switch (req
->dma_fmt
) {
494 case SRP_NO_DATA_DESC
:
496 case SRP_DATA_DESC_DIRECT
:
497 req
->total_desc
= req
->local_desc
= 1;
499 case SRP_DATA_DESC_INDIRECT
: {
500 struct srp_indirect_buf
*ind_tmp
= (struct srp_indirect_buf
*)
501 (cmd
->add_data
+ req
->cdb_offset
);
503 req
->total_desc
= be32_to_cpu(ind_tmp
->table_desc
.len
) /
504 sizeof(struct srp_direct_buf
);
505 req
->local_desc
= req
->writing
? cmd
->data_out_desc_cnt
:
506 cmd
->data_in_desc_cnt
;
511 "vscsi_preprocess_desc: Unknown format %x\n", req
->dma_fmt
);
518 /* Callback to indicate that the SCSI layer has completed a transfer. */
519 static void vscsi_transfer_data(SCSIRequest
*sreq
, uint32_t len
)
521 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(sreq
->bus
->qbus
.parent
);
522 vscsi_req
*req
= sreq
->hba_private
;
526 DPRINTF("VSCSI: SCSI xfer complete tag=0x%x len=0x%x, req=%p\n",
527 sreq
->tag
, len
, req
);
529 fprintf(stderr
, "VSCSI: Can't find request for tag 0x%x\n", sreq
->tag
);
534 buf
= scsi_req_get_buf(sreq
);
535 rc
= vscsi_srp_transfer_data(s
, req
, req
->writing
, buf
, len
);
538 fprintf(stderr
, "VSCSI: RDMA error rc=%d!\n", rc
);
539 vscsi_makeup_sense(s
, req
, HARDWARE_ERROR
, 0, 0);
540 scsi_req_abort(req
->sreq
, CHECK_CONDITION
);
544 /* Start next chunk */
546 scsi_req_continue(sreq
);
549 /* Callback to indicate that the SCSI layer has completed a transfer. */
550 static void vscsi_command_complete(SCSIRequest
*sreq
, uint32_t status
, size_t resid
)
552 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(sreq
->bus
->qbus
.parent
);
553 vscsi_req
*req
= sreq
->hba_private
;
554 int32_t res_in
= 0, res_out
= 0;
556 DPRINTF("VSCSI: SCSI cmd complete, tag=0x%x status=0x%x, req=%p\n",
557 sreq
->tag
, status
, req
);
559 fprintf(stderr
, "VSCSI: Can't find request for tag 0x%x\n", sreq
->tag
);
563 if (status
== CHECK_CONDITION
) {
564 req
->senselen
= scsi_req_get_sense(req
->sreq
, req
->sense
,
566 DPRINTF("VSCSI: Sense data, %d bytes:\n", req
->senselen
);
567 DPRINTF(" %02x %02x %02x %02x %02x %02x %02x %02x\n",
568 req
->sense
[0], req
->sense
[1], req
->sense
[2], req
->sense
[3],
569 req
->sense
[4], req
->sense
[5], req
->sense
[6], req
->sense
[7]);
570 DPRINTF(" %02x %02x %02x %02x %02x %02x %02x %02x\n",
571 req
->sense
[8], req
->sense
[9], req
->sense
[10], req
->sense
[11],
572 req
->sense
[12], req
->sense
[13], req
->sense
[14], req
->sense
[15]);
575 DPRINTF("VSCSI: Command complete err=%d\n", status
);
577 /* We handle overflows, not underflows for normal commands,
578 * but hopefully nobody cares
581 res_out
= req
->data_len
;
583 res_in
= req
->data_len
;
586 vscsi_send_rsp(s
, req
, status
, res_in
, res_out
);
590 static void vscsi_request_cancelled(SCSIRequest
*sreq
)
592 vscsi_req
*req
= sreq
->hba_private
;
597 static const VMStateDescription vmstate_spapr_vscsi_req
= {
598 .name
= "spapr_vscsi_req",
600 .minimum_version_id
= 1,
601 .minimum_version_id_old
= 1,
602 .fields
= (VMStateField
[]) {
603 VMSTATE_BUFFER(crq
.raw
, vscsi_req
),
604 VMSTATE_BUFFER(iu
.srp
.reserved
, vscsi_req
),
605 VMSTATE_UINT32(qtag
, vscsi_req
),
606 VMSTATE_BOOL(active
, vscsi_req
),
607 VMSTATE_UINT32(data_len
, vscsi_req
),
608 VMSTATE_BOOL(writing
, vscsi_req
),
609 VMSTATE_UINT32(senselen
, vscsi_req
),
610 VMSTATE_BUFFER(sense
, vscsi_req
),
611 VMSTATE_UINT8(dma_fmt
, vscsi_req
),
612 VMSTATE_UINT16(local_desc
, vscsi_req
),
613 VMSTATE_UINT16(total_desc
, vscsi_req
),
614 VMSTATE_UINT16(cdb_offset
, vscsi_req
),
615 /*Restart SCSI request from the beginning for now */
616 /*VMSTATE_UINT16(cur_desc_num, vscsi_req),
617 VMSTATE_UINT16(cur_desc_offset, vscsi_req),*/
618 VMSTATE_END_OF_LIST()
622 static void vscsi_save_request(QEMUFile
*f
, SCSIRequest
*sreq
)
624 vscsi_req
*req
= sreq
->hba_private
;
627 vmstate_save_state(f
, &vmstate_spapr_vscsi_req
, req
);
629 DPRINTF("VSCSI: saving tag=%u, current desc#%d, offset=%x\n",
630 req
->qtag
, req
->cur_desc_num
, req
->cur_desc_offset
);
633 static void *vscsi_load_request(QEMUFile
*f
, SCSIRequest
*sreq
)
635 SCSIBus
*bus
= sreq
->bus
;
636 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(bus
->qbus
.parent
);
640 assert(sreq
->tag
< VSCSI_REQ_LIMIT
);
641 req
= &s
->reqs
[sreq
->tag
];
642 assert(!req
->active
);
644 memset(req
, 0, sizeof(*req
));
645 rc
= vmstate_load_state(f
, &vmstate_spapr_vscsi_req
, req
, 1);
647 fprintf(stderr
, "VSCSI: failed loading request tag#%u\n", sreq
->tag
);
652 req
->sreq
= scsi_req_ref(sreq
);
654 DPRINTF("VSCSI: restoring tag=%u, current desc#%d, offset=%x\n",
655 req
->qtag
, req
->cur_desc_num
, req
->cur_desc_offset
);
660 static void vscsi_process_login(VSCSIState
*s
, vscsi_req
*req
)
662 union viosrp_iu
*iu
= &req
->iu
;
663 struct srp_login_rsp
*rsp
= &iu
->srp
.login_rsp
;
664 uint64_t tag
= iu
->srp
.rsp
.tag
;
666 DPRINTF("VSCSI: Got login, sendin response !\n");
668 /* TODO handle case that requested size is wrong and
669 * buffer format is wrong
671 memset(iu
, 0, sizeof(struct srp_login_rsp
));
672 rsp
->opcode
= SRP_LOGIN_RSP
;
673 /* Don't advertise quite as many request as we support to
674 * keep room for management stuff etc...
676 rsp
->req_lim_delta
= cpu_to_be32(VSCSI_REQ_LIMIT
-2);
678 rsp
->max_it_iu_len
= cpu_to_be32(sizeof(union srp_iu
));
679 rsp
->max_ti_iu_len
= cpu_to_be32(sizeof(union srp_iu
));
680 /* direct and indirect */
681 rsp
->buf_fmt
= cpu_to_be16(SRP_BUF_FORMAT_DIRECT
| SRP_BUF_FORMAT_INDIRECT
);
683 vscsi_send_iu(s
, req
, sizeof(*rsp
), VIOSRP_SRP_FORMAT
);
686 static void vscsi_inquiry_no_target(VSCSIState
*s
, vscsi_req
*req
)
688 uint8_t *cdb
= req
->iu
.srp
.cmd
.cdb
;
689 uint8_t resp_data
[36];
692 /* We dont do EVPD. Also check that page_code is 0 */
693 if ((cdb
[1] & 0x01) || (cdb
[1] & 0x01) || cdb
[2] != 0) {
694 /* Send INVALID FIELD IN CDB */
695 vscsi_makeup_sense(s
, req
, ILLEGAL_REQUEST
, 0x24, 0);
696 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
700 alen
= (alen
<< 8) | cdb
[4];
703 /* Fake up inquiry using PQ=3 */
704 memset(resp_data
, 0, 36);
705 resp_data
[0] = 0x7f; /* Not capable of supporting a device here */
706 resp_data
[2] = 0x06; /* SPS-4 */
707 resp_data
[3] = 0x02; /* Resp data format */
708 resp_data
[4] = 36 - 5; /* Additional length */
709 resp_data
[7] = 0x10; /* Sync transfers */
710 memcpy(&resp_data
[16], "QEMU EMPTY ", 16);
711 memcpy(&resp_data
[8], "QEMU ", 8);
714 vscsi_preprocess_desc(req
);
715 rc
= vscsi_srp_transfer_data(s
, req
, 0, resp_data
, len
);
717 vscsi_makeup_sense(s
, req
, HARDWARE_ERROR
, 0, 0);
718 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
720 vscsi_send_rsp(s
, req
, 0, 36 - rc
, 0);
724 static void vscsi_report_luns(VSCSIState
*s
, vscsi_req
*req
)
733 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
734 SCSIDevice
*dev
= SCSI_DEVICE(kid
->child
);
737 if (dev
->channel
== 0 && dev
->id
== 0 && dev
->lun
== 0) {
746 resp_data
= g_malloc0(len
);
747 memset(resp_data
, 0, len
);
748 stl_be_p(resp_data
, n
);
749 i
= found_lun0
? 8 : 16;
750 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
751 DeviceState
*qdev
= kid
->child
;
752 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
754 if (dev
->id
== 0 && dev
->channel
== 0) {
755 resp_data
[i
] = 0; /* Use simple LUN for 0 (SAM5 4.7.7.1) */
757 resp_data
[i
] = (2 << 6); /* Otherwise LUN addressing (4.7.7.4) */
759 resp_data
[i
] |= dev
->id
;
760 resp_data
[i
+1] = (dev
->channel
<< 5);
761 resp_data
[i
+1] |= dev
->lun
;
765 vscsi_preprocess_desc(req
);
766 rc
= vscsi_srp_transfer_data(s
, req
, 0, resp_data
, len
);
769 vscsi_makeup_sense(s
, req
, HARDWARE_ERROR
, 0, 0);
770 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
772 vscsi_send_rsp(s
, req
, 0, len
- rc
, 0);
776 static int vscsi_queue_cmd(VSCSIState
*s
, vscsi_req
*req
)
778 union srp_iu
*srp
= &req
->iu
.srp
;
782 if ((srp
->cmd
.lun
== 0 || be64_to_cpu(srp
->cmd
.lun
) == SRP_REPORT_LUNS_WLUN
)
783 && srp
->cmd
.cdb
[0] == REPORT_LUNS
) {
784 vscsi_report_luns(s
, req
);
788 sdev
= vscsi_device_find(&s
->bus
, be64_to_cpu(srp
->cmd
.lun
), &lun
);
790 DPRINTF("VSCSI: Command for lun %08" PRIx64
" with no drive\n",
791 be64_to_cpu(srp
->cmd
.lun
));
792 if (srp
->cmd
.cdb
[0] == INQUIRY
) {
793 vscsi_inquiry_no_target(s
, req
);
795 vscsi_makeup_sense(s
, req
, ILLEGAL_REQUEST
, 0x24, 0x00);
796 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
800 req
->sreq
= scsi_req_new(sdev
, req
->qtag
, lun
, srp
->cmd
.cdb
, req
);
801 n
= scsi_req_enqueue(req
->sreq
);
803 DPRINTF("VSCSI: Queued command tag 0x%x CMD 0x%x LUN %d ret: %d\n",
804 req
->qtag
, srp
->cmd
.cdb
[0], lun
, n
);
807 /* Transfer direction must be set before preprocessing the
810 req
->writing
= (n
< 1);
812 /* Preprocess RDMA descriptors */
813 vscsi_preprocess_desc(req
);
815 /* Get transfer direction and initiate transfer */
821 scsi_req_continue(req
->sreq
);
823 /* Don't touch req here, it may have been recycled already */
828 static int vscsi_process_tsk_mgmt(VSCSIState
*s
, vscsi_req
*req
)
830 union viosrp_iu
*iu
= &req
->iu
;
832 int i
, lun
= 0, resp
= SRP_TSK_MGMT_COMPLETE
;
834 uint64_t tag
= iu
->srp
.rsp
.tag
;
835 uint8_t sol_not
= iu
->srp
.cmd
.sol_not
;
837 fprintf(stderr
, "vscsi_process_tsk_mgmt %02x\n",
838 iu
->srp
.tsk_mgmt
.tsk_mgmt_func
);
840 d
= vscsi_device_find(&s
->bus
, be64_to_cpu(req
->iu
.srp
.tsk_mgmt
.lun
), &lun
);
842 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
844 switch (iu
->srp
.tsk_mgmt
.tsk_mgmt_func
) {
845 case SRP_TSK_ABORT_TASK
:
847 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
851 tmpreq
= vscsi_find_req(s
, req
->iu
.srp
.tsk_mgmt
.task_tag
);
852 if (tmpreq
&& tmpreq
->sreq
) {
853 assert(tmpreq
->sreq
->hba_private
);
854 scsi_req_cancel(tmpreq
->sreq
);
858 case SRP_TSK_LUN_RESET
:
860 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
864 qdev_reset_all(&d
->qdev
);
867 case SRP_TSK_ABORT_TASK_SET
:
868 case SRP_TSK_CLEAR_TASK_SET
:
870 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
874 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
875 tmpreq
= &s
->reqs
[i
];
876 if (tmpreq
->iu
.srp
.cmd
.lun
!= req
->iu
.srp
.tsk_mgmt
.lun
) {
879 if (!tmpreq
->active
|| !tmpreq
->sreq
) {
882 assert(tmpreq
->sreq
->hba_private
);
883 scsi_req_cancel(tmpreq
->sreq
);
887 case SRP_TSK_CLEAR_ACA
:
888 resp
= SRP_TSK_MGMT_NOT_SUPPORTED
;
892 resp
= SRP_TSK_MGMT_FIELDS_INVALID
;
897 /* Compose the response here as */
898 memset(iu
, 0, sizeof(struct srp_rsp
) + 4);
899 iu
->srp
.rsp
.opcode
= SRP_RSP
;
900 iu
->srp
.rsp
.req_lim_delta
= cpu_to_be32(1);
901 iu
->srp
.rsp
.tag
= tag
;
902 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_RSPVALID
;
903 iu
->srp
.rsp
.resp_data_len
= cpu_to_be32(4);
905 iu
->srp
.rsp
.sol_not
= (sol_not
& 0x04) >> 2;
907 iu
->srp
.rsp
.sol_not
= (sol_not
& 0x02) >> 1;
910 iu
->srp
.rsp
.status
= GOOD
;
911 iu
->srp
.rsp
.data
[3] = resp
;
913 vscsi_send_iu(s
, req
, sizeof(iu
->srp
.rsp
) + 4, VIOSRP_SRP_FORMAT
);
918 static int vscsi_handle_srp_req(VSCSIState
*s
, vscsi_req
*req
)
920 union srp_iu
*srp
= &req
->iu
.srp
;
922 uint8_t opcode
= srp
->rsp
.opcode
;
926 vscsi_process_login(s
, req
);
929 done
= vscsi_process_tsk_mgmt(s
, req
);
932 done
= vscsi_queue_cmd(s
, req
);
942 fprintf(stderr
, "VSCSI: Unsupported opcode %02x\n", opcode
);
945 fprintf(stderr
, "VSCSI: Unknown type %02x\n", opcode
);
951 static int vscsi_send_adapter_info(VSCSIState
*s
, vscsi_req
*req
)
953 struct viosrp_adapter_info
*sinfo
;
954 struct mad_adapter_info_data info
;
957 sinfo
= &req
->iu
.mad
.adapter_info
;
959 #if 0 /* What for ? */
960 rc
= spapr_vio_dma_read(&s
->vdev
, be64_to_cpu(sinfo
->buffer
),
961 &info
, be16_to_cpu(sinfo
->common
.length
));
963 fprintf(stderr
, "vscsi_send_adapter_info: DMA read failure !\n");
966 memset(&info
, 0, sizeof(info
));
967 strcpy(info
.srp_version
, SRP_VERSION
);
968 memcpy(info
.partition_name
, "qemu", sizeof("qemu"));
969 info
.partition_number
= cpu_to_be32(0);
970 info
.mad_version
= cpu_to_be32(1);
971 info
.os_type
= cpu_to_be32(2);
972 info
.port_max_txu
[0] = cpu_to_be32(VSCSI_MAX_SECTORS
<< 9);
974 rc
= spapr_vio_dma_write(&s
->vdev
, be64_to_cpu(sinfo
->buffer
),
975 &info
, be16_to_cpu(sinfo
->common
.length
));
977 fprintf(stderr
, "vscsi_send_adapter_info: DMA write failure !\n");
980 sinfo
->common
.status
= rc
? cpu_to_be32(1) : 0;
982 return vscsi_send_iu(s
, req
, sizeof(*sinfo
), VIOSRP_MAD_FORMAT
);
985 static int vscsi_send_capabilities(VSCSIState
*s
, vscsi_req
*req
)
987 struct viosrp_capabilities
*vcap
;
988 struct capabilities cap
= { };
989 uint16_t len
, req_len
;
993 vcap
= &req
->iu
.mad
.capabilities
;
994 req_len
= len
= be16_to_cpu(vcap
->common
.length
);
995 buffer
= be64_to_cpu(vcap
->buffer
);
996 if (len
> sizeof(cap
)) {
997 fprintf(stderr
, "vscsi_send_capabilities: capabilities size mismatch !\n");
1000 * Just read and populate the structure that is known.
1001 * Zero rest of the structure.
1005 rc
= spapr_vio_dma_read(&s
->vdev
, buffer
, &cap
, len
);
1007 fprintf(stderr
, "vscsi_send_capabilities: DMA read failure !\n");
1011 * Current implementation does not suppport any migration or
1012 * reservation capabilities. Construct the response telling the
1013 * guest not to use them.
1016 cap
.migration
.ecl
= 0;
1017 cap
.reserve
.type
= 0;
1018 cap
.migration
.common
.server_support
= 0;
1019 cap
.reserve
.common
.server_support
= 0;
1021 rc
= spapr_vio_dma_write(&s
->vdev
, buffer
, &cap
, len
);
1023 fprintf(stderr
, "vscsi_send_capabilities: DMA write failure !\n");
1025 if (req_len
> len
) {
1027 * Being paranoid and lets not worry about the error code
1028 * here. Actual write of the cap is done above.
1030 spapr_vio_dma_set(&s
->vdev
, (buffer
+ len
), 0, (req_len
- len
));
1032 vcap
->common
.status
= rc
? cpu_to_be32(1) : 0;
1033 return vscsi_send_iu(s
, req
, sizeof(*vcap
), VIOSRP_MAD_FORMAT
);
1036 static int vscsi_handle_mad_req(VSCSIState
*s
, vscsi_req
*req
)
1038 union mad_iu
*mad
= &req
->iu
.mad
;
1039 bool request_handled
= false;
1040 uint64_t retlen
= 0;
1042 switch (be32_to_cpu(mad
->empty_iu
.common
.type
)) {
1043 case VIOSRP_EMPTY_IU_TYPE
:
1044 fprintf(stderr
, "Unsupported EMPTY MAD IU\n");
1045 retlen
= sizeof(mad
->empty_iu
);
1047 case VIOSRP_ERROR_LOG_TYPE
:
1048 fprintf(stderr
, "Unsupported ERROR LOG MAD IU\n");
1049 retlen
= sizeof(mad
->error_log
);
1051 case VIOSRP_ADAPTER_INFO_TYPE
:
1052 vscsi_send_adapter_info(s
, req
);
1053 request_handled
= true;
1055 case VIOSRP_HOST_CONFIG_TYPE
:
1056 retlen
= sizeof(mad
->host_config
);
1058 case VIOSRP_CAPABILITIES_TYPE
:
1059 vscsi_send_capabilities(s
, req
);
1060 request_handled
= true;
1063 fprintf(stderr
, "VSCSI: Unknown MAD type %02x\n",
1064 be32_to_cpu(mad
->empty_iu
.common
.type
));
1066 * PAPR+ says that "The length field is set to the length
1067 * of the data structure(s) used in the command".
1068 * As we did not recognize the request type, put zero there.
1073 if (!request_handled
) {
1074 mad
->empty_iu
.common
.status
= cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED
);
1075 vscsi_send_iu(s
, req
, retlen
, VIOSRP_MAD_FORMAT
);
1081 static void vscsi_got_payload(VSCSIState
*s
, vscsi_crq
*crq
)
1086 req
= vscsi_get_req(s
);
1088 fprintf(stderr
, "VSCSI: Failed to get a request !\n");
1092 /* We only support a limited number of descriptors, we know
1093 * the ibmvscsi driver uses up to 10 max, so it should fit
1094 * in our 256 bytes IUs. If not we'll have to increase the size
1097 if (crq
->s
.IU_length
> sizeof(union viosrp_iu
)) {
1098 fprintf(stderr
, "VSCSI: SRP IU too long (%d bytes) !\n",
1104 /* XXX Handle failure differently ? */
1105 if (spapr_vio_dma_read(&s
->vdev
, crq
->s
.IU_data_ptr
, &req
->iu
,
1106 crq
->s
.IU_length
)) {
1107 fprintf(stderr
, "vscsi_got_payload: DMA read failure !\n");
1111 memcpy(&req
->crq
, crq
, sizeof(vscsi_crq
));
1113 if (crq
->s
.format
== VIOSRP_MAD_FORMAT
) {
1114 done
= vscsi_handle_mad_req(s
, req
);
1116 done
= vscsi_handle_srp_req(s
, req
);
1125 static int vscsi_do_crq(struct VIOsPAPRDevice
*dev
, uint8_t *crq_data
)
1127 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(dev
);
1130 memcpy(crq
.raw
, crq_data
, 16);
1131 crq
.s
.timeout
= be16_to_cpu(crq
.s
.timeout
);
1132 crq
.s
.IU_length
= be16_to_cpu(crq
.s
.IU_length
);
1133 crq
.s
.IU_data_ptr
= be64_to_cpu(crq
.s
.IU_data_ptr
);
1135 DPRINTF("VSCSI: do_crq %02x %02x ...\n", crq
.raw
[0], crq
.raw
[1]);
1137 switch (crq
.s
.valid
) {
1138 case 0xc0: /* Init command/response */
1140 /* Respond to initialization request */
1141 if (crq
.s
.format
== 0x01) {
1142 memset(crq
.raw
, 0, 16);
1144 crq
.s
.format
= 0x02;
1145 spapr_vio_send_crq(dev
, crq
.raw
);
1148 /* Note that in hotplug cases, we might get a 0x02
1149 * as a result of us emitting the init request
1153 case 0xff: /* Link event */
1155 /* Not handled for now */
1158 case 0x80: /* Payloads */
1159 switch (crq
.s
.format
) {
1160 case VIOSRP_SRP_FORMAT
: /* AKA VSCSI request */
1161 case VIOSRP_MAD_FORMAT
: /* AKA VSCSI response */
1162 vscsi_got_payload(s
, &crq
);
1164 case VIOSRP_OS400_FORMAT
:
1165 case VIOSRP_AIX_FORMAT
:
1166 case VIOSRP_LINUX_FORMAT
:
1167 case VIOSRP_INLINE_FORMAT
:
1168 fprintf(stderr
, "vscsi_do_srq: Unsupported payload format %02x\n",
1172 fprintf(stderr
, "vscsi_do_srq: Unknown payload format %02x\n",
1177 fprintf(stderr
, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
1178 crq
.raw
[0], crq
.raw
[1]);
1184 static const struct SCSIBusInfo vscsi_scsi_info
= {
1186 .max_channel
= 7, /* logical unit addressing format */
1190 .transfer_data
= vscsi_transfer_data
,
1191 .complete
= vscsi_command_complete
,
1192 .cancel
= vscsi_request_cancelled
,
1193 .save_request
= vscsi_save_request
,
1194 .load_request
= vscsi_load_request
,
1197 static void spapr_vscsi_reset(VIOsPAPRDevice
*dev
)
1199 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(dev
);
1202 memset(s
->reqs
, 0, sizeof(s
->reqs
));
1203 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
1204 s
->reqs
[i
].qtag
= i
;
1208 static int spapr_vscsi_init(VIOsPAPRDevice
*dev
)
1210 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(dev
);
1213 dev
->crq
.SendFunc
= vscsi_do_crq
;
1215 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), DEVICE(dev
),
1216 &vscsi_scsi_info
, NULL
);
1217 if (!dev
->qdev
.hotplugged
) {
1218 scsi_bus_legacy_handle_cmdline(&s
->bus
, &err
);
1228 void spapr_vscsi_create(VIOsPAPRBus
*bus
)
1232 dev
= qdev_create(&bus
->bus
, "spapr-vscsi");
1234 qdev_init_nofail(dev
);
1237 static int spapr_vscsi_devnode(VIOsPAPRDevice
*dev
, void *fdt
, int node_off
)
1241 ret
= fdt_setprop_cell(fdt
, node_off
, "#address-cells", 2);
1246 ret
= fdt_setprop_cell(fdt
, node_off
, "#size-cells", 0);
1254 static Property spapr_vscsi_properties
[] = {
1255 DEFINE_SPAPR_PROPERTIES(VSCSIState
, vdev
),
1256 DEFINE_PROP_END_OF_LIST(),
1259 static const VMStateDescription vmstate_spapr_vscsi
= {
1260 .name
= "spapr_vscsi",
1262 .minimum_version_id
= 1,
1263 .minimum_version_id_old
= 1,
1264 .fields
= (VMStateField
[]) {
1265 VMSTATE_SPAPR_VIO(vdev
, VSCSIState
),
1269 VMSTATE_END_OF_LIST()
1273 static void spapr_vscsi_class_init(ObjectClass
*klass
, void *data
)
1275 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1276 VIOsPAPRDeviceClass
*k
= VIO_SPAPR_DEVICE_CLASS(klass
);
1278 k
->init
= spapr_vscsi_init
;
1279 k
->reset
= spapr_vscsi_reset
;
1280 k
->devnode
= spapr_vscsi_devnode
;
1281 k
->dt_name
= "v-scsi";
1282 k
->dt_type
= "vscsi";
1283 k
->dt_compatible
= "IBM,v-scsi";
1284 k
->signal_mask
= 0x00000001;
1285 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1286 dc
->props
= spapr_vscsi_properties
;
1287 k
->rtce_window_size
= 0x10000000;
1288 dc
->vmsd
= &vmstate_spapr_vscsi
;
1291 static const TypeInfo spapr_vscsi_info
= {
1292 .name
= TYPE_VIO_SPAPR_VSCSI_DEVICE
,
1293 .parent
= TYPE_VIO_SPAPR_DEVICE
,
1294 .instance_size
= sizeof(VSCSIState
),
1295 .class_init
= spapr_vscsi_class_init
,
1298 static void spapr_vscsi_register_types(void)
1300 type_register_static(&spapr_vscsi_info
);
1303 type_init(spapr_vscsi_register_types
)